From 288b2f8b248afab372240954c2d0f82417b50cd1 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Tue, 23 Dec 2025 14:27:12 +0100 Subject: [PATCH] Introduce page coloring support to memory manager --- xtoskrnl/includes/mm.hh | 1 + xtoskrnl/includes/mm/colors.hh | 35 +++++++++++++ xtoskrnl/mm/colors.cc | 95 ++++++++++++++++++++++++++++++++++ xtoskrnl/mm/data.cc | 9 ++++ xtoskrnl/mm/mmgr.cc | 3 ++ 5 files changed, 143 insertions(+) create mode 100644 xtoskrnl/includes/mm/colors.hh create mode 100644 xtoskrnl/mm/colors.cc diff --git a/xtoskrnl/includes/mm.hh b/xtoskrnl/includes/mm.hh index bd94683e..b34863dc 100644 --- a/xtoskrnl/includes/mm.hh +++ b/xtoskrnl/includes/mm.hh @@ -15,6 +15,7 @@ #include XTOS_ARCH_HEADER(mm, paging.hh) #include XTOS_ARCH_HEADER(mm, pte.hh) +#include #include #include #include diff --git a/xtoskrnl/includes/mm/colors.hh b/xtoskrnl/includes/mm/colors.hh new file mode 100644 index 00000000..229b6498 --- /dev/null +++ b/xtoskrnl/includes/mm/colors.hh @@ -0,0 +1,35 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/mm/colors.hh + * DESCRIPTION: Memory manager page coloring subsystem + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_MM_COLORS_HH +#define __XTOSKRNL_MM_COLORS_HH + +#include + + +/* Memory Manager */ +namespace MM +{ + class Colors + { + private: + STATIC PMMCOLOR_TABLES FreePages[FreePageList + 1]; + STATIC ULONG PagingColors; + STATIC ULONG PagingColorsMask; + + public: + STATIC XTAPI VOID ComputePageColoring(VOID); + STATIC XTAPI PMMCOLOR_TABLES GetFreePages(MMPAGELISTS PageList, + ULONG Color); + STATIC XTAPI ULONG GetNextColor(); + STATIC XTAPI ULONG GetPagingColors(); + STATIC XTAPI ULONG GetPagingColorsMask(); + }; +} + +#endif /* __XTOSKRNL_MM_PFN_HH */ diff --git a/xtoskrnl/mm/colors.cc b/xtoskrnl/mm/colors.cc new file mode 100644 index 00000000..e2258466 --- /dev/null +++ b/xtoskrnl/mm/colors.cc @@ -0,0 +1,95 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/mm/colors.cc + * DESCRIPTION: Memory manager page coloring subsystem + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Computes & initializes the system's page coloring. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MM::Colors::ComputePageColoring(VOID) +{ + UNIMPLEMENTED; + + /* Compute L2 paging colors and mask */ + PagingColors = MM_PAGING_COLORS; + PagingColorsMask = PagingColors - 1; +} + +/** + * Retrieves a pointer to the color table for a specific page list and color. + * + * @param PageList + * The page list type (e.g., FreePageList, ZeroedPageList). + * + * @param Color + * Supplies the specific color index. + * + * @return This routine returns a pointer to the corresponding MMCOLOR_TABLES structure. + * + * @since XT 1.0 + */ +XTAPI +PMMCOLOR_TABLES +MM::Colors::GetFreePages(MMPAGELISTS PageList, + ULONG Color) +{ + /* Return a pointer to the requested color table entry */ + return &FreePages[PageList][Color]; +} + +/** + * Retrieves the next available color for page coloring. + * + * @return This routine returns the next color value, ensuring it stays within the valid color range. + * + * @since XT 1.0 + */ +XTAPI +ULONG +MM::Colors::GetNextColor() +{ + /* Increment the color counter and wrap it around using the mask */ + return ((++PagingColors) & PagingColorsMask); +} + +/** + * Retrieves the total number of page colors configured in the system. + * + * @return This routine returns the number of page colors. + * + * @since XT 1.0 + */ +XTAPI +ULONG +MM::Colors::GetPagingColors() +{ + /* Return the total number of page colors */ + return PagingColors; +} + +/** + * Retrieves the bitmask used for calculating a page's color. + * + * @return This routine returns the page color mask. + * + * @since XT 1.0 + */ +XTAPI +ULONG +MM::Colors::GetPagingColorsMask() +{ + /* Return the mask used for page coloring calculations */ + return PagingColorsMask; +} diff --git a/xtoskrnl/mm/data.cc b/xtoskrnl/mm/data.cc index 296c0d4f..de5fe417 100644 --- a/xtoskrnl/mm/data.cc +++ b/xtoskrnl/mm/data.cc @@ -9,6 +9,15 @@ #include +/* Array of free page lists segregated by cache color */ +PMMCOLOR_TABLES MM::Colors::FreePages[FreePageList + 1]; + +/* Number of supported page colors */ +ULONG MM::Colors::PagingColors; + +/* Bitmask used to calculate the cache color index */ +ULONG MM::Colors::PagingColorsMask; + /* Allocation descriptors dedicated for hardware layer */ LOADER_MEMORY_DESCRIPTOR MM::HardwarePool::HardwareAllocationDescriptors[MM_HARDWARE_ALLOCATION_DESCRIPTORS]; diff --git a/xtoskrnl/mm/mmgr.cc b/xtoskrnl/mm/mmgr.cc index 5aa17ed2..c305c04e 100644 --- a/xtoskrnl/mm/mmgr.cc +++ b/xtoskrnl/mm/mmgr.cc @@ -54,6 +54,9 @@ MM::Manager::InitializeMemoryManager(VOID) KE::Crash::Panic(0); } + /* Compute page colors to reduce CPU cache conflicts */ + MM::Colors::ComputePageColoring(); + /* Compute allocation size for the PFN database */ MM::Pfn::ComputePfnDatabaseSize();