From 5e3fb7a5a31fe7e50397173991787cea35470bf5 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Thu, 29 Jan 2026 22:26:31 +0100 Subject: [PATCH] Move PFN database size tracking to memory layout --- xtoskrnl/includes/mm/pfn.hh | 4 +--- xtoskrnl/mm/amd64/mmgr.cc | 7 +++---- xtoskrnl/mm/amd64/pfn.cc | 7 ++++--- xtoskrnl/mm/data.cc | 3 --- xtoskrnl/mm/i686/mmgr.cc | 8 ++++---- xtoskrnl/mm/i686/pfn.cc | 10 +++++----- xtoskrnl/mm/mmgr.cc | 3 --- xtoskrnl/mm/pfn.cc | 25 +++++++++---------------- 8 files changed, 26 insertions(+), 41 deletions(-) diff --git a/xtoskrnl/includes/mm/pfn.hh b/xtoskrnl/includes/mm/pfn.hh index efca193..65996f7 100644 --- a/xtoskrnl/includes/mm/pfn.hh +++ b/xtoskrnl/includes/mm/pfn.hh @@ -30,7 +30,6 @@ namespace MM STATIC ULONGLONG NumberOfPhysicalPages; STATIC LOADER_MEMORY_DESCRIPTOR OriginalFreeDescriptor; STATIC PMMPFNLIST PageLocationList[]; - STATIC PFN_NUMBER PfnDatabaseSize; STATIC MMPFNLIST RomPagesList; STATIC MMPFNLIST StandbyPagesList; STATIC MMPFNLIST ZeroedPagesList; @@ -38,7 +37,7 @@ namespace MM public: STATIC XTAPI PFN_NUMBER AllocateBootstrapPages(IN PFN_NUMBER NumberOfPages); STATIC XTAPI PFN_NUMBER AllocatePhysicalPage(IN ULONG Color); - STATIC XTAPI VOID ComputePfnDatabaseSize(VOID); + STATIC XTAPI VOID ComputePfnDatabaseSize(OUT PPFN_NUMBER DatabaseSize); STATIC XTAPI VOID DecrementReferenceCount(IN PMMPFN Pfn1, IN PFN_NUMBER PageFrameIndex, IN BOOLEAN BeginStandbyList = FALSE); @@ -48,7 +47,6 @@ namespace MM STATIC XTAPI VOID FreePhysicalPage(IN PMMPTE PointerPte); STATIC XTAPI ULONG_PTR GetHighestPhysicalPage(VOID); STATIC XTAPI ULONGLONG GetNumberOfPhysicalPages(VOID); - STATIC XTAPI PFN_NUMBER GetPfnDatabaseSize(VOID); STATIC XTAPI PMMPFN GetPfnEntry(IN PFN_NUMBER Pfn); STATIC XTAPI VOID InitializePfnDatabase(VOID); STATIC VOID XTAPI LinkPfnToPte(IN PFN_NUMBER PageFrameIndex, diff --git a/xtoskrnl/mm/amd64/mmgr.cc b/xtoskrnl/mm/amd64/mmgr.cc index 843039b..ff6551a 100644 --- a/xtoskrnl/mm/amd64/mmgr.cc +++ b/xtoskrnl/mm/amd64/mmgr.cc @@ -21,7 +21,6 @@ VOID MM::Manager::InitializeMemoryLayout(VOID) { ULONG_PTR PagedPoolSize, PteCount; - PFN_NUMBER PfnDatabaseSize; ULONG PtesPerPage; /* Get the number of PTEs per page and calculate size of paged pool (at least 32MiB) */ @@ -30,7 +29,7 @@ MM::Manager::InitializeMemoryLayout(VOID) PagedPoolSize = PteCount * PtesPerPage * MM_PAGE_SIZE; /* Retrieve the PFN database size */ - PfnDatabaseSize = MM::Pfn::GetPfnDatabaseSize(); + MM::Pfn::GetPfnDatabaseSize(&MemoryLayout.PfnDatabaseSize); /* Define the number of system PTEs */ NumberOfSystemPtes = MM_DEFAULT_NUMBER_SYSTEM_PTES; @@ -43,7 +42,7 @@ MM::Manager::InitializeMemoryLayout(VOID) MemoryLayout.SelfMapAddress = (PVOID)MM_P5E_LA57_BASE; /* Define the non-paged and paged pool regions */ - MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + PfnDatabaseSize * MM_PAGE_SIZE); + MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + MemoryLayout.PfnDatabaseSize * MM_PAGE_SIZE); MemoryLayout.NonPagedPoolEnd = (PVOID)0xFFFEFFFFFFBFFFFFULL; MemoryLayout.PagedPoolStart = (PVOID)0xFFFEF8A000000000ULL; MemoryLayout.PagedPoolEnd = (PVOID)(((ULONG_PTR)MemoryLayout.PagedPoolStart + PagedPoolSize) - 1); @@ -60,7 +59,7 @@ MM::Manager::InitializeMemoryLayout(VOID) MemoryLayout.SelfMapAddress = (PVOID)MM_PXE_BASE; /* Define the non-paged and paged pool regions */ - MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + PfnDatabaseSize * MM_PAGE_SIZE); + MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + MemoryLayout.PfnDatabaseSize * MM_PAGE_SIZE); MemoryLayout.NonPagedPoolEnd = (PVOID)0xFFFFFFFFFFBFFFFFULL; MemoryLayout.PagedPoolStart = (PVOID)0xFFFFF8A000000000ULL; MemoryLayout.PagedPoolEnd = (PVOID)(((ULONG_PTR)MemoryLayout.PagedPoolStart + PagedPoolSize) - 1); diff --git a/xtoskrnl/mm/amd64/pfn.cc b/xtoskrnl/mm/amd64/pfn.cc index d9bbc7f..03f0183 100644 --- a/xtoskrnl/mm/amd64/pfn.cc +++ b/xtoskrnl/mm/amd64/pfn.cc @@ -40,7 +40,7 @@ MM::Pfn::InitializePfnDatabase(VOID) MemoryLayout = MM::Manager::GetMemoryLayout(); /* Get the PFN database size and calculate the end of the PFN database virtual address space */ - PfnDatabaseEnd = (PUCHAR)MemoryLayout->PfnDatabase + (PfnDatabaseSize * MM_PAGE_SIZE) - 1; + PfnDatabaseEnd = (PUCHAR)MemoryLayout->PfnDatabase + (MemoryLayout->PfnDatabaseSize * MM_PAGE_SIZE) - 1; /* Get a template PTE for mapping the PFN database pages */ ValidPte = MM::Pte::GetValidPte(); @@ -88,10 +88,11 @@ MM::Pfn::InitializePfnDatabase(VOID) if(Descriptor == FreeDescriptor) { /* Initialize PFNs for the remaining free memory */ - ProcessMemoryDescriptor(BasePage + PfnDatabaseSize, PageCount - PfnDatabaseSize, LoaderFree); + ProcessMemoryDescriptor(BasePage + MemoryLayout->PfnDatabaseSize, + PageCount - MemoryLayout->PfnDatabaseSize, LoaderFree); /* Initialize PFNs for the physical pages backing the PFN database */ - ProcessMemoryDescriptor(BasePage, PfnDatabaseSize, LoaderMemoryData); + ProcessMemoryDescriptor(BasePage, MemoryLayout->PfnDatabaseSize, LoaderMemoryData); } else { diff --git a/xtoskrnl/mm/data.cc b/xtoskrnl/mm/data.cc index ffbd85a..6586526 100644 --- a/xtoskrnl/mm/data.cc +++ b/xtoskrnl/mm/data.cc @@ -82,9 +82,6 @@ PMMPFNLIST MM::Pfn::PageLocationList[] = {&ZeroedPagesList, NULLPTR, NULLPTR}; -/* Size of the PFN database in pages */ -PFN_NUMBER MM::Pfn::PfnDatabaseSize; - /* List containing pages mapped as Read-Only (ROM) */ MMPFNLIST MM::Pfn::RomPagesList = {0, StandbyPageList, MAXULONG_PTR, MAXULONG_PTR}; diff --git a/xtoskrnl/mm/i686/mmgr.cc b/xtoskrnl/mm/i686/mmgr.cc index c51b4db..cc0032a 100644 --- a/xtoskrnl/mm/i686/mmgr.cc +++ b/xtoskrnl/mm/i686/mmgr.cc @@ -21,7 +21,7 @@ VOID MM::Manager::InitializeMemoryLayout(VOID) { ULONG_PTR PagedPoolSize, PteCount; - PFN_NUMBER PfnDatabaseSize, PhysicalPages; + PFN_NUMBER PhysicalPages; ULONG PtesPerPage; /* Define the number of system PTEs */ @@ -48,7 +48,7 @@ MM::Manager::InitializeMemoryLayout(VOID) PagedPoolSize = PteCount * PtesPerPage * MM_PAGE_SIZE; /* Retrieve the PFN database size */ - PfnDatabaseSize = MM::Pfn::GetPfnDatabaseSize(); + MM::Pfn::GetPfnDatabaseSize(&MemoryLayout.PfnDatabaseSize); /* Check if 3-level paging (PAE) is enabled */ if(MM::Paging::GetXpaStatus()) @@ -58,7 +58,7 @@ MM::Manager::InitializeMemoryLayout(VOID) MemoryLayout.SelfMapAddress = (PVOID)MM_PTE_BASE; /* Define the non-paged and paged pool regions */ - MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + PfnDatabaseSize * MM_PAGE_SIZE); + MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + MemoryLayout.PfnDatabaseSize * MM_PAGE_SIZE); MemoryLayout.NonPagedPoolEnd = (PVOID)0xEEFFFFFF; MemoryLayout.PagedPoolStart = (PVOID)0xE2000000; MemoryLayout.PagedPoolEnd = (PVOID)(((ULONG_PTR)MemoryLayout.PagedPoolStart + PagedPoolSize) - 1); @@ -75,7 +75,7 @@ MM::Manager::InitializeMemoryLayout(VOID) MemoryLayout.SelfMapAddress = (PVOID)MM_PTE_BASE; /* Define the non-paged and paged pool regions */ - MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + PfnDatabaseSize * MM_PAGE_SIZE); + MemoryLayout.NonPagedPoolStart = (PVOID)((ULONG_PTR)MemoryLayout.PfnDatabase + MemoryLayout.PfnDatabaseSize * MM_PAGE_SIZE); MemoryLayout.NonPagedPoolEnd = (PVOID)0xFFBE0000; MemoryLayout.PagedPoolStart = (PVOID)0xE1000000; MemoryLayout.PagedPoolEnd = (PVOID)(((ULONG_PTR)MemoryLayout.PagedPoolStart + PagedPoolSize) - 1); diff --git a/xtoskrnl/mm/i686/pfn.cc b/xtoskrnl/mm/i686/pfn.cc index 840066d..a939809 100644 --- a/xtoskrnl/mm/i686/pfn.cc +++ b/xtoskrnl/mm/i686/pfn.cc @@ -39,7 +39,7 @@ MM::Pfn::InitializePfnDatabase(VOID) MemoryLayout = MM::Manager::GetMemoryLayout(); /* Get the PFN database size and calculate the end of the PFN database virtual address space */ - PfnDatabaseEnd = (PUCHAR)MemoryLayout->PfnDatabase + (PfnDatabaseSize * MM_PAGE_SIZE) - 1; + PfnDatabaseEnd = (PUCHAR)MemoryLayout->PfnDatabase + (MemoryLayout->PfnDatabaseSize * MM_PAGE_SIZE) - 1; /* Get a template PTE for mapping the PFN database pages */ ValidPte = MM::Pte::GetValidPte(); @@ -49,7 +49,7 @@ MM::Pfn::InitializePfnDatabase(VOID) MM::Pte::MapPTE(MemoryLayout->PfnDatabase, PfnDatabaseEnd, ValidPte); /* Zero PFN database virtual space */ - RTL::Memory::ZeroMemory(MemoryLayout->PfnDatabase, PfnDatabaseSize * MM_PAGE_SIZE); + RTL::Memory::ZeroMemory(MemoryLayout->PfnDatabase, MemoryLayout->PfnDatabaseSize * MM_PAGE_SIZE); /* Initialize the color tables */ MM::Colors::InitializeColorTables(); @@ -73,12 +73,12 @@ MM::Pfn::InitializePfnDatabase(VOID) if(Descriptor == FreeDescriptor) { /* Initialize PFNs for the remaining free memory after the PFN database */ - ProcessMemoryDescriptor(OriginalFreeDescriptor.BasePage + PfnDatabaseSize, - OriginalFreeDescriptor.PageCount - PfnDatabaseSize, + ProcessMemoryDescriptor(OriginalFreeDescriptor.BasePage + MemoryLayout->PfnDatabaseSize, + OriginalFreeDescriptor.PageCount - MemoryLayout->PfnDatabaseSize, LoaderFree); /* Initialize PFNs for the physical pages backing the PFN database itself */ - ProcessMemoryDescriptor(OriginalFreeDescriptor.BasePage, PfnDatabaseSize, LoaderMemoryData); + ProcessMemoryDescriptor(OriginalFreeDescriptor.BasePage, MemoryLayout->PfnDatabaseSize, LoaderMemoryData); } else { diff --git a/xtoskrnl/mm/mmgr.cc b/xtoskrnl/mm/mmgr.cc index 23620b6..6d11779 100644 --- a/xtoskrnl/mm/mmgr.cc +++ b/xtoskrnl/mm/mmgr.cc @@ -112,9 +112,6 @@ MM::Manager::InitializeMemoryManager(VOID) /* Compute page colors to reduce CPU cache conflicts */ MM::Colors::ComputePageColoring(); - /* Compute allocation size for the PFN database */ - MM::Pfn::ComputePfnDatabaseSize(); - /* Initialize memory layout */ InitializeMemoryLayout(); diff --git a/xtoskrnl/mm/pfn.cc b/xtoskrnl/mm/pfn.cc index 6ce37be..c7de33e 100644 --- a/xtoskrnl/mm/pfn.cc +++ b/xtoskrnl/mm/pfn.cc @@ -99,19 +99,27 @@ MM::Pfn::AllocatePhysicalPage(IN ULONG Color) /** * Calculates the total number of pages required for the PFN database and its associated color tables. * + * @param DatabaseSize + * A pointer to a variable that will receive the number of pages required for the PFN database. + * * @return This routine does not return any value. * * @since XT 1.0 */ XTAPI VOID -MM::Pfn::ComputePfnDatabaseSize(VOID) +MM::Pfn::ComputePfnDatabaseSize(OUT PPFN_NUMBER DatabaseSize) { + PFN_NUMBER PfnDatabaseSize; + /* Calculate the total number of pages required for the PFN database */ PfnDatabaseSize = (HighestPhysicalPage + 1) * sizeof(MMPFN); PfnDatabaseSize += (MM::Colors::GetPagingColors() * sizeof(MMCOLOR_TABLES) * 2); PfnDatabaseSize = ROUND_UP(PfnDatabaseSize, MM_PAGE_SIZE); PfnDatabaseSize >>= MM_PAGE_SHIFT; + + /* Return the PFN database size */ + *DatabaseSize = PfnDatabaseSize; } /** @@ -373,21 +381,6 @@ MM::Pfn::GetNumberOfPhysicalPages(VOID) return NumberOfPhysicalPages; } -/** - * Gets the size of the PFN database and its associated structures, in pages. - * - * @return This routine returns the total number of pages required for the PFN database and its associated structures. - * - * @since XT 1.0 - */ -XTAPI -PFN_NUMBER -MM::Pfn::GetPfnDatabaseSize(VOID) -{ - /* Return the pre-calculated size of the PFN database in pages */ - return PfnDatabaseSize; -} - /** * Retrieves a pointer to the PFN database entry for a given physical page. *