From 5224dc315f41d6354d9c12c9304aa79013eed935 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Sat, 13 Dec 2025 22:50:27 +0100 Subject: [PATCH] Compute PFN database size during MM initialization --- xtoskrnl/includes/mm/pfn.hh | 3 +++ xtoskrnl/mm/mmgr.cc | 3 +++ xtoskrnl/mm/pfn.cc | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/xtoskrnl/includes/mm/pfn.hh b/xtoskrnl/includes/mm/pfn.hh index 543d958..8682d0d 100644 --- a/xtoskrnl/includes/mm/pfn.hh +++ b/xtoskrnl/includes/mm/pfn.hh @@ -23,9 +23,12 @@ namespace MM STATIC ULONG_PTR LowestPhysicalPage; STATIC ULONG NumberOfPhysicalPages; STATIC LOADER_MEMORY_DESCRIPTOR OriginalFreeDescriptor; + STATIC PFN_NUMBER PfnDatabaseSize; public: + STATIC XTAPI VOID ComputePfnDatabaseSize(VOID); STATIC XTAPI ULONG GetNumberOfPhysicalPages(VOID); + STATIC XTAPI PFN_NUMBER GetPfnDatabaseSize(VOID); STATIC XTAPI VOID ScanMemoryDescriptors(VOID); }; } diff --git a/xtoskrnl/mm/mmgr.cc b/xtoskrnl/mm/mmgr.cc index 5297ab8..9c9aea1 100644 --- a/xtoskrnl/mm/mmgr.cc +++ b/xtoskrnl/mm/mmgr.cc @@ -31,6 +31,9 @@ MM::Manager::InitializeMemoryManager(VOID) DebugPrint(L"Insufficient physical pages! Install additional memory\n"); KE::Crash::Panic(0); } + + /* Compute allocation size for the PFN database */ + MM::Pfn::ComputePfnDatabaseSize(); } /** diff --git a/xtoskrnl/mm/pfn.cc b/xtoskrnl/mm/pfn.cc index ac58496..84ead54 100644 --- a/xtoskrnl/mm/pfn.cc +++ b/xtoskrnl/mm/pfn.cc @@ -10,6 +10,23 @@ #include +/** + * Calculates the total number of pages required for the PFN database and its associated color tables. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MM::Pfn::ComputePfnDatabaseSize(VOID) +{ + /* Calculate the total number of pages required for the PFN database */ + PfnDatabaseSize = (HighestPhysicalPage + 1) * sizeof(MMPFN); + PfnDatabaseSize = ROUND_UP(PfnDatabaseSize, MM_PAGE_SIZE); + PfnDatabaseSize >>= MM_PAGE_SHIFT; +} + /** * Retrieves the total number of physical pages managed by the system. * @@ -25,6 +42,21 @@ 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; +} + /** * Scans memory descriptors provided by the boot loader. *