From 783a9eea3a49d880ffaafa401d36adea72c4bb2d Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Sat, 13 Dec 2025 20:42:48 +0100 Subject: [PATCH] Extract PFN management into separate module --- xtoskrnl/CMakeLists.txt | 2 +- xtoskrnl/includes/mm.hh | 1 + xtoskrnl/includes/mm/mmgr.hh | 10 ---- xtoskrnl/includes/mm/pfn.hh | 32 +++++++++++++ xtoskrnl/mm/amd64/init.cc | 25 ---------- xtoskrnl/mm/data.cc | 14 +++--- xtoskrnl/mm/i686/init.cc | 25 ---------- xtoskrnl/mm/mmgr.cc | 81 -------------------------------- xtoskrnl/mm/pfn.cc | 89 ++++++++++++++++++++++++++++++++++++ 9 files changed, 130 insertions(+), 149 deletions(-) create mode 100644 xtoskrnl/includes/mm/pfn.hh delete mode 100644 xtoskrnl/mm/amd64/init.cc delete mode 100644 xtoskrnl/mm/i686/init.cc create mode 100644 xtoskrnl/mm/pfn.cc diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 64841e8..082b386 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -51,7 +51,6 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/ke/spinlock.cc ${XTOSKRNL_SOURCE_DIR}/ke/sysres.cc ${XTOSKRNL_SOURCE_DIR}/ke/timer.cc - ${XTOSKRNL_SOURCE_DIR}/mm/${ARCH}/init.cc ${XTOSKRNL_SOURCE_DIR}/mm/${ARCH}/pagemap.cc ${XTOSKRNL_SOURCE_DIR}/mm/${ARCH}/paging.cc ${XTOSKRNL_SOURCE_DIR}/mm/data.cc @@ -59,6 +58,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/mm/kpool.cc ${XTOSKRNL_SOURCE_DIR}/mm/mmgr.cc ${XTOSKRNL_SOURCE_DIR}/mm/paging.cc + ${XTOSKRNL_SOURCE_DIR}/mm/pfn.cc ${XTOSKRNL_SOURCE_DIR}/po/idle.cc ${XTOSKRNL_SOURCE_DIR}/rtl/${ARCH}/dispatch.cc ${XTOSKRNL_SOURCE_DIR}/rtl/${ARCH}/exsup.cc diff --git a/xtoskrnl/includes/mm.hh b/xtoskrnl/includes/mm.hh index 2e3ef60..2a25fd6 100644 --- a/xtoskrnl/includes/mm.hh +++ b/xtoskrnl/includes/mm.hh @@ -17,5 +17,6 @@ #include #include #include +#include #endif /* __XTOSKRNL_MM_HH */ diff --git a/xtoskrnl/includes/mm/mmgr.hh b/xtoskrnl/includes/mm/mmgr.hh index 2fc0e84..9a05a9d 100644 --- a/xtoskrnl/includes/mm/mmgr.hh +++ b/xtoskrnl/includes/mm/mmgr.hh @@ -17,20 +17,10 @@ namespace MM { class Manager { - private: - STATIC PLOADER_MEMORY_DESCRIPTOR FreeDescriptor; - STATIC ULONG_PTR HighestPhysicalPage; - STATIC ULONG_PTR LowestPhysicalPage; - STATIC ULONG NumberOfPhysicalPages; - STATIC LOADER_MEMORY_DESCRIPTOR OldFreeDescriptor; - public: STATIC XTAPI VOID InitializeMemoryManager(VOID); - STATIC XTAPI VOID InitializePageMapSupport(VOID); - STATIC XTAPI VOID ScanMemoryDescriptors(VOID); private: - STATIC XTAPI VOID InitializeArchitecture(VOID); STATIC XTAPI BOOLEAN VerifyMemoryTypeFree(LOADER_MEMORY_TYPE MemoryType); STATIC XTAPI BOOLEAN VerifyMemoryTypeInvisible(LOADER_MEMORY_TYPE MemoryType); }; diff --git a/xtoskrnl/includes/mm/pfn.hh b/xtoskrnl/includes/mm/pfn.hh new file mode 100644 index 0000000..d8806d9 --- /dev/null +++ b/xtoskrnl/includes/mm/pfn.hh @@ -0,0 +1,32 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/mm/pfn.hh + * DESCRIPTION: Physical Frame Number (PFN) support + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_MM_PFN_HH +#define __XTOSKRNL_MM_PFN_HH + +#include + + +/* Memory Manager */ +namespace MM +{ + class Pfn + { + private: + STATIC PLOADER_MEMORY_DESCRIPTOR FreeDescriptor; + STATIC ULONG_PTR HighestPhysicalPage; + STATIC ULONG_PTR LowestPhysicalPage; + STATIC ULONG NumberOfPhysicalPages; + STATIC LOADER_MEMORY_DESCRIPTOR OldFreeDescriptor; + + public: + STATIC XTAPI VOID ScanMemoryDescriptors(VOID); + }; +} + +#endif /* __XTOSKRNL_MM_PFN_HH */ diff --git a/xtoskrnl/mm/amd64/init.cc b/xtoskrnl/mm/amd64/init.cc deleted file mode 100644 index 7e7a9d8..0000000 --- a/xtoskrnl/mm/amd64/init.cc +++ /dev/null @@ -1,25 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtoskrnl/mm/amd64/init.cc - * DESCRIPTION: Architecture specific Memory Manager initialization routines - * DEVELOPERS: Rafal Kupiec - * Aiken Harris - */ - -#include - - -/** - * Performs architecture specific initialization of the XTOS Memory Manager. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTAPI -VOID -MM::Init::InitializeArchitecture(VOID) -{ - UNIMPLEMENTED; -} diff --git a/xtoskrnl/mm/data.cc b/xtoskrnl/mm/data.cc index 7869827..19ea38c 100644 --- a/xtoskrnl/mm/data.cc +++ b/xtoskrnl/mm/data.cc @@ -18,23 +18,23 @@ PVOID MM::HardwarePool::HardwareHeapStart = MM_HARDWARE_HEAP_START_ADDRESS; /* Number of used hardware allocation descriptors */ ULONG MM::HardwarePool::UsedHardwareAllocationDescriptors = 0; +/* Processor structures data (THIS IS A TEMPORARY HACK) */ +UCHAR MM::KernelPool::ProcessorStructuresData[MAXIMUM_PROCESSORS][KPROCESSOR_STRUCTURES_SIZE] = {{0}}; + /* Biggest free memory descriptor */ PLOADER_MEMORY_DESCRIPTOR MM::Init::FreeDescriptor; /* Highest physical page number */ -ULONG_PTR MM::Init::HighestPhysicalPage; +ULONG_PTR MM::Pfn::HighestPhysicalPage; /* Lowest physical page number */ -ULONG_PTR MM::Init::LowestPhysicalPage = -1; +ULONG_PTR MM::Pfn::LowestPhysicalPage = -1; /* Number of physical pages */ -ULONG MM::Init::NumberOfPhysicalPages; +ULONG MM::Pfn::NumberOfPhysicalPages; /* Old biggest free memory descriptor */ -LOADER_MEMORY_DESCRIPTOR MM::Init::OldFreeDescriptor; - -/* Processor structures data (THIS IS A TEMPORARY HACK) */ -UCHAR MM::KernelPool::ProcessorStructuresData[MAXIMUM_PROCESSORS][KPROCESSOR_STRUCTURES_SIZE] = {{0}}; +LOADER_MEMORY_DESCRIPTOR MM::Pfn::OldFreeDescriptor; /* Instance of the page map routines for the current PML level */ MM::PPAGEMAP MM::Paging::PmlRoutines; diff --git a/xtoskrnl/mm/i686/init.cc b/xtoskrnl/mm/i686/init.cc deleted file mode 100644 index ab1892d..0000000 --- a/xtoskrnl/mm/i686/init.cc +++ /dev/null @@ -1,25 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtoskrnl/mm/i686/init.cc - * DESCRIPTION: Architecture specific Memory Manager initialization routines - * DEVELOPERS: Rafal Kupiec - * Aiken Harris - */ - -#include - - -/** - * Performs architecture specific initialization of the XTOS Memory Manager. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTAPI -VOID -MM::Init::InitializeArchitecture(VOID) -{ - UNIMPLEMENTED; -} diff --git a/xtoskrnl/mm/mmgr.cc b/xtoskrnl/mm/mmgr.cc index 24135a5..218eab9 100644 --- a/xtoskrnl/mm/mmgr.cc +++ b/xtoskrnl/mm/mmgr.cc @@ -31,87 +31,6 @@ MM::Manager::InitializeMemoryManager(VOID) DebugPrint(L"Insufficient physical pages! Install additional memory\n"); KE::Crash::Panic(0); } - - /* Proceed with architecture specific initialization */ - InitializeArchitecture(); -} - -/** - * Scans memory descriptors provided by the boot loader. - * - * @return This routine does not return any value. - * - * @since XT 1.0 - */ -XTAPI -VOID -MM::Manager::ScanMemoryDescriptors(VOID) -{ - PLIST_ENTRY LoaderMemoryDescriptors, MemoryMappings; - PLOADER_MEMORY_DESCRIPTOR MemoryDescriptor; - PFN_NUMBER FreePages; - - /* Initially, set number of free pages to 0 */ - FreePages = 0; - - /* Get a list of memory descriptors provided by the boot loader */ - LoaderMemoryDescriptors = KE::BootInformation::GetMemoryDescriptors(); - - /* Iterate through memory mappings provided by the boot loader */ - MemoryMappings = LoaderMemoryDescriptors->Flink; - while(MemoryMappings != LoaderMemoryDescriptors) - { - /* Get memory descriptor */ - MemoryDescriptor = CONTAIN_RECORD(MemoryMappings, LOADER_MEMORY_DESCRIPTOR, ListEntry); - - /* Check if memory type is invisible or cached */ - if(VerifyMemoryTypeInvisible(MemoryDescriptor->MemoryType) || - (MemoryDescriptor->MemoryType == LoaderHardwareCachedMemory)) - { - /* Skip this mapping */ - MemoryMappings = MemoryMappings->Flink; - continue; - } - - /* Make sure that memory type is not bad */ - if(MemoryDescriptor->MemoryType != LoaderBad) - { - /* Increment number of physical pages */ - NumberOfPhysicalPages += MemoryDescriptor->PageCount; - } - - /* Find lowest physical page */ - if(MemoryDescriptor->BasePage < LowestPhysicalPage) - { - /* Update lowest physical page */ - LowestPhysicalPage = MemoryDescriptor->BasePage; - } - - /* Find highest physical page */ - if(MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > HighestPhysicalPage) - { - /* Update highest physical page */ - HighestPhysicalPage = (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount) - 1; - } - - /* Check if memory type should be considered as free */ - if(VerifyMemoryTypeFree(MemoryDescriptor->MemoryType)) - { - /* Check if this descriptor contains more free pages */ - if(MemoryDescriptor->PageCount >= FreePages) - { - /* Update free descriptor */ - FreePages = MemoryDescriptor->PageCount; - FreeDescriptor = MemoryDescriptor; - } - } - - /* Get next memory descriptor */ - MemoryMappings = MemoryMappings->Flink; - } - - /* Store original free descriptor */ - RTL::Memory::CopyMemory(&OldFreeDescriptor, FreeDescriptor, sizeof(LOADER_MEMORY_DESCRIPTOR)); } /** Checks whether the specified memory type should be considered as free. diff --git a/xtoskrnl/mm/pfn.cc b/xtoskrnl/mm/pfn.cc new file mode 100644 index 0000000..78f6713 --- /dev/null +++ b/xtoskrnl/mm/pfn.cc @@ -0,0 +1,89 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/mm/pfn.cc + * DESCRIPTION: Physical Frame Number (PFN) support + * DEVELOPERS: Aiken Harris + * Rafal Kupiec + */ + +#include + + +/** + * Scans memory descriptors provided by the boot loader. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MM::Pfn::ScanMemoryDescriptors(VOID) +{ + PLIST_ENTRY LoaderMemoryDescriptors, MemoryMappings; + PLOADER_MEMORY_DESCRIPTOR MemoryDescriptor; + PFN_NUMBER FreePages; + + /* Initially, set number of free pages to 0 */ + FreePages = 0; + + /* Get a list of memory descriptors provided by the boot loader */ + LoaderMemoryDescriptors = KE::BootInformation::GetMemoryDescriptors(); + + /* Iterate through memory mappings provided by the boot loader */ + MemoryMappings = LoaderMemoryDescriptors->Flink; + while(MemoryMappings != LoaderMemoryDescriptors) + { + /* Get memory descriptor */ + MemoryDescriptor = CONTAIN_RECORD(MemoryMappings, LOADER_MEMORY_DESCRIPTOR, ListEntry); + + /* Check if memory type is invisible or cached */ + if(VerifyMemoryTypeInvisible(MemoryDescriptor->MemoryType) || + (MemoryDescriptor->MemoryType == LoaderHardwareCachedMemory)) + { + /* Skip this mapping */ + MemoryMappings = MemoryMappings->Flink; + continue; + } + + /* Make sure that memory type is not bad */ + if(MemoryDescriptor->MemoryType != LoaderBad) + { + /* Increment number of physical pages */ + NumberOfPhysicalPages += MemoryDescriptor->PageCount; + } + + /* Find lowest physical page */ + if(MemoryDescriptor->BasePage < LowestPhysicalPage) + { + /* Update lowest physical page */ + LowestPhysicalPage = MemoryDescriptor->BasePage; + } + + /* Find highest physical page */ + if(MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > HighestPhysicalPage) + { + /* Update highest physical page */ + HighestPhysicalPage = (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount) - 1; + } + + /* Check if memory type should be considered as free */ + if(VerifyMemoryTypeFree(MemoryDescriptor->MemoryType)) + { + /* Check if this descriptor contains more free pages */ + if(MemoryDescriptor->PageCount >= FreePages) + { + /* Update free descriptor */ + FreePages = MemoryDescriptor->PageCount; + FreeDescriptor = MemoryDescriptor; + } + } + + /* Get next memory descriptor */ + MemoryMappings = MemoryMappings->Flink; + } + + /* Store original free descriptor */ + RTL::Memory::CopyMemory(&OldFreeDescriptor, FreeDescriptor, sizeof(LOADER_MEMORY_DESCRIPTOR)); +}