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
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.
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).
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.
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.
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.
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
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.
The MmPageMapLevel global has been removed, as this information is now contained within the MmpPageMapInfo structure.
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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
AMD64: 5-Level paging (LA57 / PML5) implementation
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