Refactor MMU for multi-paging support and add 5-Level paging #16

Open
harraiken wants to merge 35 commits from harraiken_mm into master
Owner

This pull request introduces a major architectural refactoring of the Memory Management Unit (MMU) to support multiple paging modes within a single kernel, adds full support for 5-level paging (LA57) on AMD64, and fixes a critical address calculation bug.

Architectural refactoring

  1. A CMMPAGEMAP_ROUTINES structure has been introduced, containing function pointers for essential PTE manipulations (ClearPte, PteValid, SetPteCaching, SetPte). This allows the high-level MMU code to operate on an abstraction, while the low-level, mode-specific implementation is determined at runtime.
  2. Dynamic paging mode selection:
    • On i686, the kernel now detects at boot whether PAE is enabled (CR4.PAE). Based on this, it assigns either MmpPml2Routines (for legacy 32-bit, 2-level paging) or MmpPml3Routines (for PAE, 3-level paging) to the global MmpPageMapRoutines pointer. This enables a single kernel binary to run on systems both with and without PAE.
    • On AMD64, a similar mechanism detects if 5-level paging is active (CR4.LA57) and selects between MmpPml4Routines (4-level) and MmpPml5Routines (5-level).
  3. The PTE structures for i686 have been refactored. MMPTE is now a union that can represent both MMPML2_PTE (non-PAE) and MMPML3_PTE (PAE) formats, simplifying code that needs to handle both.

AMD64: 5-Level paging (LA57 / PML5) implementation

  1. A new assembly routine, ArEnableExtendedPhysicalAddressing, is introduced. This code runs from a low-memory trampoline (< 1MB) to safely transition the CPU into 5-level paging mode. This involves temporarily entering 32-bit compatibility mode to set CR4.LA57, as required by the AMD64 architecture, and then re-enabling long mode.
  2. XTLDR now maps the trampoline code, and upon exiting EFI Boot Services, executes it to enable LA57 before loading the kernel's new PML5 page table into CR3.
  3. The page table setup logic now correctly handles self-mapping for the PML5 table, which is essential for the kernel's operation.

Critical bug fix: AMD64 MMU base address overflow

The previous MM_PTE_LA57_BASE address (0xFFFFF68000000000ULL) was too high. When calculating PTE addresses for high virtual memory regions, adding the virtual address offset resulted in a 64-bit integer overflow, leading to page faults at incorrect, non-canonical addresses. The new MM_PTE_LA57_BASE is 0xFFFF000000000000ULL, which prevents the overflow and ensures correct address translation for the entire 57-bit address space.

Code consolidation and cleanup

  1. Paging-related global variables (like base addresses and VA bit width) have been consolidated into a central MmpPageMapInfo structure. This structure is initialized at boot by MmInitializePageMapSupport based on the detected paging mode, cleaning up global state and improving code organization.
  2. The MmPageMapLevel global has been removed, as this information is now contained within the MmpPageMapInfo structure.
  3. Low-level page mapping functions have been moved from init.c to new, dedicated pmap.c files for each architecture.
This pull request introduces a major architectural refactoring of the Memory Management Unit (MMU) to support multiple paging modes within a single kernel, adds full support for 5-level paging (LA57) on AMD64, and fixes a critical address calculation bug. ## Architectural refactoring 1. A CMMPAGEMAP_ROUTINES structure has been introduced, containing function pointers for essential PTE manipulations (ClearPte, PteValid, SetPteCaching, SetPte). This allows the high-level MMU code to operate on an abstraction, while the low-level, mode-specific implementation is determined at runtime. 2. Dynamic paging mode selection: - On i686, the kernel now detects at boot whether PAE is enabled (CR4.PAE). Based on this, it assigns either MmpPml2Routines (for legacy 32-bit, 2-level paging) or MmpPml3Routines (for PAE, 3-level paging) to the global MmpPageMapRoutines pointer. This enables a single kernel binary to run on systems both with and without PAE. - On AMD64, a similar mechanism detects if 5-level paging is active (CR4.LA57) and selects between MmpPml4Routines (4-level) and MmpPml5Routines (5-level). 3. The PTE structures for i686 have been refactored. MMPTE is now a union that can represent both MMPML2_PTE (non-PAE) and MMPML3_PTE (PAE) formats, simplifying code that needs to handle both. ## AMD64: 5-Level paging (LA57 / PML5) implementation 1. A new assembly routine, ArEnableExtendedPhysicalAddressing, is introduced. This code runs from a low-memory trampoline (< 1MB) to safely transition the CPU into 5-level paging mode. This involves temporarily entering 32-bit compatibility mode to set CR4.LA57, as required by the AMD64 architecture, and then re-enabling long mode. 2. XTLDR now maps the trampoline code, and upon exiting EFI Boot Services, executes it to enable LA57 before loading the kernel's new PML5 page table into CR3. 3. The page table setup logic now correctly handles self-mapping for the PML5 table, which is essential for the kernel's operation. ## Critical bug fix: AMD64 MMU base address overflow The previous MM_PTE_LA57_BASE address (0xFFFFF68000000000ULL) was too high. When calculating PTE addresses for high virtual memory regions, adding the virtual address offset resulted in a 64-bit integer overflow, leading to page faults at incorrect, non-canonical addresses. The new MM_PTE_LA57_BASE is 0xFFFF000000000000ULL, which prevents the overflow and ensures correct address translation for the entire 57-bit address space. ## Code consolidation and cleanup 1. Paging-related global variables (like base addresses and VA bit width) have been consolidated into a central MmpPageMapInfo structure. This structure is initialized at boot by MmInitializePageMapSupport based on the detected paging mode, cleaning up global state and improving code organization. 2. The MmPageMapLevel global has been removed, as this information is now contained within the MmpPageMapInfo structure. 3. Low-level page mapping functions have been moved from init.c to new, dedicated pmap.c files for each architecture.
harraiken added 35 commits 2025-08-21 10:43:24 +02:00
Use __asm__ to comply with disabled GNU extensions
All checks were successful
Builds / ExectOS (i686, debug) (push) Successful in 27s
Builds / ExectOS (amd64, release) (push) Successful in 28s
Builds / ExectOS (i686, release) (push) Successful in 39s
Builds / ExectOS (amd64, debug) (push) Successful in 43s
e8771dfc5b
Merge branch 'master' into harraiken_mm
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 22s
Builds / ExectOS (i686, debug) (push) Successful in 23s
Builds / ExectOS (i686, release) (push) Successful in 40s
Builds / ExectOS (amd64, release) (push) Successful in 42s
8a23cc444f
Rename MM_LA57_SHIFT to MM_P5I_SHIFT for consistency
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 31s
Builds / ExectOS (i686, debug) (push) Successful in 30s
Builds / ExectOS (amd64, debug) (push) Successful in 42s
Builds / ExectOS (i686, release) (push) Successful in 40s
e57985da8d
Add definitions for 5-level paging and refactor constants
All checks were successful
Builds / ExectOS (i686, debug) (push) Successful in 25s
Builds / ExectOS (amd64, debug) (push) Successful in 28s
Builds / ExectOS (i686, release) (push) Successful in 37s
Builds / ExectOS (amd64, release) (push) Successful in 42s
3ca6d04f6b
Separate types for legacy (PML2) and PAE (PML3) paging
Some checks failed
Builds / ExectOS (i686, debug) (push) Failing after 20s
Builds / ExectOS (amd64, debug) (push) Successful in 22s
Builds / ExectOS (amd64, release) (push) Successful in 36s
Builds / ExectOS (i686, release) (push) Failing after 32s
7e08dc286e
Update forward declarations for PML2/PML3 types
Some checks failed
Builds / ExectOS (amd64, debug) (push) Successful in 21s
Builds / ExectOS (i686, debug) (push) Failing after 20s
Builds / ExectOS (i686, release) (push) Failing after 34s
Builds / ExectOS (amd64, release) (push) Successful in 36s
22f81a106b
Adapt i686 memory mapping to new PML3 types
Some checks failed
Builds / ExectOS (i686, debug) (push) Failing after 19s
Builds / ExectOS (amd64, debug) (push) Successful in 22s
Builds / ExectOS (amd64, release) (push) Successful in 37s
Builds / ExectOS (i686, release) (push) Failing after 35s
f85fe31b38
Prepare for architecture-specific paging initialization
Some checks failed
Builds / ExectOS (amd64, debug) (push) Successful in 22s
Builds / ExectOS (i686, release) (push) Failing after 21s
Builds / ExectOS (amd64, release) (push) Successful in 35s
Builds / ExectOS (i686, debug) (push) Failing after 33s
5768d4bba6
Define page map information structure for both supported architectures
Some checks failed
Builds / ExectOS (amd64, debug) (push) Successful in 25s
Builds / ExectOS (i686, release) (push) Failing after 33s
Builds / ExectOS (i686, debug) (push) Failing after 23s
Builds / ExectOS (amd64, release) (push) Successful in 34s
1dcd3fceed
Consolidate paging-related globals into MmpPageMapInfo
Some checks failed
Builds / ExectOS (amd64, release) (push) Successful in 25s
Builds / ExectOS (i686, release) (push) Failing after 21s
Builds / ExectOS (i686, debug) (push) Failing after 35s
Builds / ExectOS (amd64, debug) (push) Successful in 38s
6a330e38f2
Remove PageMapLevel from the loader information block
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 24s
Builds / ExectOS (amd64, release) (push) Successful in 26s
Builds / ExectOS (amd64, debug) (push) Successful in 36s
Builds / ExectOS (i686, debug) (push) Failing after 34s
8491e5fed1
Implement page map info initialization
Some checks failed
Builds / ExectOS (amd64, debug) (push) Successful in 24s
Builds / ExectOS (i686, debug) (push) Failing after 21s
Builds / ExectOS (amd64, release) (push) Successful in 35s
Builds / ExectOS (i686, release) (push) Failing after 33s
de2973ac42
Relocate page mapping helpers and add PML5 support
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 23s
Builds / ExectOS (amd64, release) (push) Successful in 26s
Builds / ExectOS (amd64, debug) (push) Successful in 37s
Builds / ExectOS (i686, debug) (push) Failing after 35s
0ed59f223c
Introduce architecture-specific page map routines
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 23s
Builds / ExectOS (amd64, debug) (push) Successful in 27s
Builds / ExectOS (i686, debug) (push) Failing after 31s
Builds / ExectOS (amd64, release) (push) Successful in 33s
f77f2bbf92
Assign page map routines
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 21s
Builds / ExectOS (i686, debug) (push) Failing after 21s
Builds / ExectOS (amd64, debug) (push) Successful in 38s
Builds / ExectOS (amd64, release) (push) Successful in 37s
720d525b95
Implement PML2/PML3 page table routines
Some checks failed
Builds / ExectOS (amd64, debug) (push) Successful in 21s
Builds / ExectOS (i686, release) (push) Failing after 19s
Builds / ExectOS (amd64, release) (push) Successful in 34s
Builds / ExectOS (i686, debug) (push) Failing after 32s
57193eecc0
Refactor hardware memory mapping to use page map routine callbacks
Some checks failed
Builds / ExectOS (i686, release) (push) Failing after 21s
Builds / ExectOS (i686, debug) (push) Failing after 23s
Builds / ExectOS (amd64, debug) (push) Successful in 41s
Builds / ExectOS (amd64, release) (push) Successful in 39s
1e11acee72
Fix type usage in XtpMapHardwareMemoryPool
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 22s
Builds / ExectOS (i686, release) (push) Successful in 25s
Builds / ExectOS (i686, debug) (push) Successful in 40s
Builds / ExectOS (amd64, release) (push) Successful in 42s
c3ece4f317
Implement PTE manipulation functions for AMD64 architecture
Some checks failed
Builds / ExectOS (amd64, debug) (push) Failing after 18s
Builds / ExectOS (i686, debug) (push) Successful in 22s
Builds / ExectOS (amd64, release) (push) Failing after 34s
Builds / ExectOS (i686, release) (push) Successful in 31s
f30d3df5b3
Implement MmpSetPteCaching function for AMD64 architecture
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 26s
Builds / ExectOS (i686, release) (push) Successful in 25s
Builds / ExectOS (amd64, debug) (push) Successful in 42s
Builds / ExectOS (i686, debug) (push) Successful in 40s
a9dd1eaacd
Align parameters in PTE manipulation functions
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 23s
Builds / ExectOS (i686, debug) (push) Successful in 25s
Builds / ExectOS (amd64, release) (push) Successful in 40s
Builds / ExectOS (i686, release) (push) Successful in 40s
017b8603d5
Temporarily disable LA57 paging
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 24s
Builds / ExectOS (i686, debug) (push) Successful in 24s
Builds / ExectOS (amd64, release) (push) Successful in 42s
Builds / ExectOS (i686, release) (push) Successful in 38s
d602038858
Correct VA masking in AMD64 page mapping functions
All checks were successful
Builds / ExectOS (i686, release) (push) Successful in 26s
Builds / ExectOS (amd64, release) (push) Successful in 27s
Builds / ExectOS (amd64, debug) (push) Successful in 42s
Builds / ExectOS (i686, debug) (push) Successful in 40s
c409400cbf
Implement PML5 self-mapping
All checks were successful
Builds / ExectOS (i686, release) (push) Successful in 27s
Builds / ExectOS (amd64, release) (push) Successful in 29s
Builds / ExectOS (i686, debug) (push) Successful in 41s
Builds / ExectOS (amd64, debug) (push) Successful in 43s
b639bf3077
Implement PML5 support in XtpMapHardwareMemoryPool
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 29s
Builds / ExectOS (i686, debug) (push) Successful in 27s
Builds / ExectOS (i686, release) (push) Successful in 39s
Builds / ExectOS (amd64, debug) (push) Successful in 43s
91a5db2ee4
Update and correct CR4 bit definitions
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 34s
Builds / ExectOS (i686, release) (push) Successful in 41s
Builds / ExectOS (i686, debug) (push) Successful in 32s
Builds / ExectOS (amd64, debug) (push) Successful in 43s
1a0bc7f65f
Expand CR4, MSR, and EFER register definitions
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 24s
Builds / ExectOS (i686, debug) (push) Successful in 25s
Builds / ExectOS (amd64, release) (push) Successful in 42s
Builds / ExectOS (i686, release) (push) Successful in 41s
ebae8c655c
Add trampoline to enable 5-level paging
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 24s
Builds / ExectOS (i686, debug) (push) Successful in 24s
Builds / ExectOS (amd64, debug) (push) Successful in 44s
Builds / ExectOS (i686, release) (push) Successful in 40s
2468d80078
Extract trampoline code into a separate file
All checks were successful
Builds / ExectOS (i686, debug) (push) Successful in 20s
Builds / ExectOS (i686, release) (push) Successful in 24s
Builds / ExectOS (amd64, debug) (push) Successful in 46s
Builds / ExectOS (amd64, release) (push) Successful in 45s
c4a7df6f38
Expose ArEnableExtendedPhysicalAddressing function in XTDK
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 28s
Builds / ExectOS (i686, debug) (push) Successful in 28s
Builds / ExectOS (i686, release) (push) Successful in 45s
Builds / ExectOS (amd64, release) (push) Successful in 48s
4a7ea6009d
Map the physical page for trampoline code
All checks were successful
Builds / ExectOS (i686, release) (push) Successful in 26s
Builds / ExectOS (amd64, release) (push) Successful in 28s
Builds / ExectOS (i686, debug) (push) Successful in 44s
Builds / ExectOS (amd64, debug) (push) Successful in 45s
9f6121e9b2
Allow specifying an allocation type when allocating pages
Some checks failed
Builds / ExectOS (i686, debug) (push) Failing after 19s
Builds / ExectOS (amd64, debug) (push) Failing after 21s
Builds / ExectOS (amd64, release) (push) Failing after 33s
Builds / ExectOS (i686, release) (push) Failing after 31s
88b3a57962
Resolve build issues caused by the last commit
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 28s
Builds / ExectOS (i686, release) (push) Successful in 26s
Builds / ExectOS (i686, debug) (push) Successful in 43s
Builds / ExectOS (amd64, debug) (push) Successful in 46s
d1b14fccdd
Enable LA57 by invoking the trampoline code
All checks were successful
Builds / ExectOS (amd64, release) (push) Successful in 31s
Builds / ExectOS (i686, release) (push) Successful in 29s
Builds / ExectOS (i686, debug) (push) Successful in 45s
Builds / ExectOS (amd64, debug) (push) Successful in 47s
1ef2560ef6
Adjust LA57 base addresses to prevent overflow
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 27s
Builds / ExectOS (i686, debug) (push) Successful in 25s
Builds / ExectOS (amd64, release) (push) Successful in 45s
Builds / ExectOS (i686, release) (push) Successful in 43s
a84ef21571
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 27s
Builds / ExectOS (i686, debug) (push) Successful in 25s
Builds / ExectOS (amd64, release) (push) Successful in 45s
Builds / ExectOS (i686, release) (push) Successful in 43s
This pull request doesn't have enough required approvals yet. 0 of 1 approvals granted from users or teams on the allowlist.
You are not authorized to merge this pull request.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin harraiken_mm:harraiken_mm
git checkout harraiken_mm
Sign in to join this conversation.
No description provided.