MCPcopy Create free account
hub / github.com/WinDurango/WinDurango / MapTitlePhysicalPages

Function MapTitlePhysicalPages

projects/WinDurango.KernelX/src/kernelx.cpp:497–543  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

495 // Mappable memory requires special handling because of placeholder splits.
496 // VirtualProtect will normally only work on pages that belong to the same allocation.
497 if (AllocationBase)
498 {
499 // TODO: Validate all pages are committed before changing protections
500 ULONG_PTR PageSize = Mappable.Is4MBPages ? PAGE_SIZE_2MB : PAGE_SIZE_4KB;
501 ULONG_PTR BasePage = (ULONG_PTR)lpAddress & ~(PageSize - 1);
502 ULONG_PTR LastPage = ((ULONG_PTR)lpAddress + dwSize) & ~(PageSize - 1);
503 BOOL Status = VirtualProtectEx(hProcess, (LPVOID)BasePage, PageSize, flNewProtect,
504 lpflOldProtect ? lpflOldProtect : &flOldProtect);
505
506 for (ULONG_PTR Page = BasePage + PageSize; Page <= LastPage; Page += PageSize)
507 Status |= VirtualProtectEx(hProcess, (LPVOID)Page, PageSize, flNewProtect, &flOldProtect);
508
509 return Status;
510 }
511
512 return VirtualProtectEx(hProcess, lpAddress, dwSize, flNewProtect, lpflOldProtect ? lpflOldProtect : &flOldProtect);
513}
514
515EXTERN_C BOOL __stdcall EraVirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
516{
517 return EraVirtualProtectEx(GetCurrentProcess(), lpAddress, dwSize, flNewProtect, lpflOldProtect);
518}
519
520EXTERN_C PVOID __stdcall MapTitlePhysicalPages(PVOID VirtualAddress, ULONG_PTR NumberOfPages, DWORD flAllocationType,
521 DWORD flProtect, PULONG_PTR PageArray)
522{
523 // TODO: Validate that PageArray contains contiguous 4MB blocks of 64K pages for MEM_4MB_PAGES
524
525 if (!PageArray || (flAllocationType & (MEM_LARGE_PAGES | MEM_4MB_PAGES)) == 0 ||
526 (flAllocationType & (MEM_LARGE_PAGES | MEM_4MB_PAGES)) == (MEM_LARGE_PAGES | MEM_4MB_PAGES) ||
527 ((flAllocationType & MEM_4MB_PAGES) && (NumberOfPages & 63)))
528 {
529 SetLastError(ERROR_INVALID_PARAMETER);
530 return nullptr;
531 }
532
533 ULONG_PTR dwPageSize = (flAllocationType & MEM_LARGE_PAGES) ? PAGE_SIZE_64K : PAGE_SIZE_4MB;
534 ULONG_PTR RegionSize = (flAllocationType & MEM_4MB_PAGES) ? 64 : 1;
535
536 if (VirtualAddress && ((ULONG_PTR)VirtualAddress & (dwPageSize - 1)))
537 {
538 SetLastError(ERROR_INVALID_PARAMETER);
539 return nullptr;
540 }
541
542 if (MEMORY_BASIC_INFORMATION mbi;
543 !VirtualAddress || !VirtualQuery(VirtualAddress, &mbi, sizeof(mbi)) || mbi.State == MEM_FREE)
544 {
545 VirtualAddress = EraVirtualAlloc(VirtualAddress, NumberOfPages * PAGE_SIZE_64K, MEM_RESERVE | flAllocationType,
546 PAGE_NOACCESS);

Callers

nothing calls this directly

Calls 2

SetLastErrorFunction · 0.85
EraVirtualAllocFunction · 0.85

Tested by

no test coverage detected