From 7d2b41a04425dd7aacfba5d1666714134f14015d Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Fri, 13 Mar 2026 19:35:29 +0100 Subject: [PATCH] Calculate virtual address per page when initializing PFN entries --- xtoskrnl/mm/pfn.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/xtoskrnl/mm/pfn.cc b/xtoskrnl/mm/pfn.cc index be4cdc3..3ab1498 100644 --- a/xtoskrnl/mm/pfn.cc +++ b/xtoskrnl/mm/pfn.cc @@ -1034,7 +1034,7 @@ MM::Pfn::ProcessMemoryDescriptor(IN PFN_NUMBER BasePage, IN PFN_NUMBER PageCount, IN LOADER_MEMORY_TYPE MemoryType) { - PVOID VirtualRangeStart, VirtualRangeEnd; + PVOID VirtualAddress, VirtualRangeStart, VirtualRangeEnd; PFN_NUMBER PageNumber; PMMPDE PointerPde; PMMPFN Pfn; @@ -1087,8 +1087,12 @@ MM::Pfn::ProcessMemoryDescriptor(IN PFN_NUMBER BasePage, /* Ensure that the page is not already in-use */ if(Pfn->u3.e2.ReferenceCount == 0) { + /* Calculate the virtual address for this page */ + VirtualAddress = (PVOID)(KSEG0_BASE + ((BasePage + PageNumber) << MM_PAGE_SHIFT)); + PointerPde = MM::Paging::GetPdeAddress(VirtualAddress); + /* Initialize the PFN entry to represent a ROM page */ - Pfn->PteAddress = MM::Paging::GetPteAddress(VirtualRangeStart); + Pfn->PteAddress = MM::Paging::GetPteAddress(VirtualAddress); Pfn->u1.Flink = 0; Pfn->u2.ShareCount = 0; Pfn->u3.e1.CacheAttribute = PfnCached; @@ -1117,8 +1121,12 @@ MM::Pfn::ProcessMemoryDescriptor(IN PFN_NUMBER BasePage, /* Ensure that the page is not already in-use */ if(Pfn->u3.e2.ReferenceCount == 0) { + /* Calculate the virtual address for this page */ + VirtualAddress = (PVOID)(KSEG0_BASE + ((BasePage + PageNumber) << MM_PAGE_SHIFT)); + PointerPde = MM::Paging::GetPdeAddress(VirtualAddress); + /* Initialize the PFN entry to represent an in-use page and prevent it from being allocated */ - Pfn->PteAddress = MM::Paging::GetPteAddress(VirtualRangeStart); + Pfn->PteAddress = MM::Paging::GetPteAddress(VirtualAddress); Pfn->u2.ShareCount++; Pfn->u3.e1.CacheAttribute = PfnCached; Pfn->u3.e1.PageLocation = ActiveAndValid;