From 5cf3dfa8447cc939a7389ec8e0397e35dd051507 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Mon, 15 Dec 2025 12:38:08 +0100 Subject: [PATCH] Add bootstrap physical page allocator --- xtoskrnl/includes/mm/pfn.hh | 1 + xtoskrnl/mm/pfn.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/xtoskrnl/includes/mm/pfn.hh b/xtoskrnl/includes/mm/pfn.hh index 8682d0d..3fc0eff 100644 --- a/xtoskrnl/includes/mm/pfn.hh +++ b/xtoskrnl/includes/mm/pfn.hh @@ -26,6 +26,7 @@ namespace MM STATIC PFN_NUMBER PfnDatabaseSize; public: + STATIC XTAPI PFN_NUMBER AllocateBootstrapPages(IN PFN_NUMBER NumberOfPages); STATIC XTAPI VOID ComputePfnDatabaseSize(VOID); STATIC XTAPI ULONG GetNumberOfPhysicalPages(VOID); STATIC XTAPI PFN_NUMBER GetPfnDatabaseSize(VOID); diff --git a/xtoskrnl/mm/pfn.cc b/xtoskrnl/mm/pfn.cc index 84ead54..324f569 100644 --- a/xtoskrnl/mm/pfn.cc +++ b/xtoskrnl/mm/pfn.cc @@ -10,6 +10,39 @@ #include +/** + * Allocates a block of physical pages for early kernel initialization. + * + * @param NumberOfPages + * The number of physical pages to allocate. + * + * @return This routine returns the base page frame number (PFN) of the allocated block. + * + * @since XT 1.0 + */ +XTAPI +PFN_NUMBER +MM::Pfn::AllocateBootstrapPages(IN PFN_NUMBER NumberOfPages) +{ + PFN_NUMBER Pfn; + + /* Check if the largest free memory block has enough pages */ + if(NumberOfPages > FreeDescriptor->PageCount) + { + /* Not enough physical memory available, kernel panic */ + DebugPrint(L"Insufficient physical pages! Install additional memory\n"); + KE::Crash::Panic(0); + } + + /* Allocate pages from the beginning of the free descriptor */ + Pfn = FreeDescriptor->BasePage; + FreeDescriptor->BasePage += NumberOfPages; + FreeDescriptor->PageCount -= NumberOfPages; + + /* Return the base page frame number of the allocated block */ + return Pfn; +} + /** * Calculates the total number of pages required for the PFN database and its associated color tables. *