diff --git a/sdk/xtdk/amd64/mmtypes.h b/sdk/xtdk/amd64/mmtypes.h index 96e7aa4..c5313e6 100644 --- a/sdk/xtdk/amd64/mmtypes.h +++ b/sdk/xtdk/amd64/mmtypes.h @@ -18,18 +18,23 @@ #define MM_PAGE_SHIFT 12L #define MM_PAGE_SIZE 4096 +/* Page directory and page base addresses */ #define MM_PTE_BASE 0xFFFFF68000000000UI64 #define MM_PDE_BASE 0xFFFFF6FB40000000UI64 #define MM_PPE_BASE 0xFFFFF6FB7DA00000UI64 #define MM_PXE_BASE 0xFFFFF6FB7DBED000UI64 +/* PTE shift values */ +#define MM_PTE_SHIFT 3 #define MM_PTI_SHIFT 12 #define MM_PDI_SHIFT 21 #define MM_PPI_SHIFT 30 #define MM_PXI_SHIFT 39 -#define MM_PTE_SHIFT 3 - +/* Number of PTEs per page */ +#define MM_PTE_PER_PAGE 512 +#define MM_PDE_PER_PAGE 512 +#define MM_PPE_PER_PAGE 512 #define MM_PXE_PER_PAGE 512 /* Minimum number of physical pages needed by the system */ diff --git a/sdk/xtdk/i686/mmtypes.h b/sdk/xtdk/i686/mmtypes.h index fc7a750..b7621ab 100644 --- a/sdk/xtdk/i686/mmtypes.h +++ b/sdk/xtdk/i686/mmtypes.h @@ -18,6 +18,20 @@ #define MM_PAGE_SHIFT 12 #define MM_PAGE_SIZE 4096 +/* Page directory and page base addresses */ +#define MM_PTE_BASE 0xC0000000 +#define MM_PDE_BASE 0xC0300000 +#define MM_PDE_PAE_BASE 0xC0600000 + +/* PTE shift values */ +#define MM_PTE_SHIFT 2 +#define MM_PTI_SHIFT 12 +#define MM_PDI_SHIFT 22 +#define MM_PPI_SHIFT 0 +#define MM_PTE_PAE_SHIFT 3 +#define MM_PDI_PAE_SHIFT 21 +#define MM_PPI_PAE_SHIFT 30 + /* Minimum number of physical pages needed by the system */ #define MM_MINIMUM_PHYSICAL_PAGES 1100 diff --git a/xtoskrnl/includes/amd64/mmi.h b/xtoskrnl/includes/amd64/mmi.h index b95b0e0..9d9ab6a 100644 --- a/xtoskrnl/includes/amd64/mmi.h +++ b/xtoskrnl/includes/amd64/mmi.h @@ -38,4 +38,8 @@ XTAPI VOID MmpInitializeArchitecture(VOID); +XTAPI +BOOLEAN +MmpMemoryExtensionEnabled(VOID); + #endif /* __XTOSKRNL_AMD64_MMI_H */ diff --git a/xtoskrnl/includes/globals.h b/xtoskrnl/includes/globals.h index 196a3a8..1a97e6f 100644 --- a/xtoskrnl/includes/globals.h +++ b/xtoskrnl/includes/globals.h @@ -75,6 +75,9 @@ EXTERN LOADER_MEMORY_MAPPING MmpHalAllocationDescriptors[MM_HAL_ALLOCATION_DESCR /* Live address of kernel HAL heap */ EXTERN PVOID MmpHalHeapStart; +/* Architecture-specific memory extension */ +EXTERN BOOLEAN MmpMemoryExtension; + /* Number of used HAL allocation descriptors */ EXTERN ULONG MmpUsedHalAllocationDescriptors; diff --git a/xtoskrnl/includes/i686/mmi.h b/xtoskrnl/includes/i686/mmi.h index 3580ac5..31c0c10 100644 --- a/xtoskrnl/includes/i686/mmi.h +++ b/xtoskrnl/includes/i686/mmi.h @@ -12,14 +12,26 @@ #include -/* I686 Memory Manager routines forward references */ +/* i686 Memory Manager routines forward references */ XTFASTCALL VOID MmZeroPages(IN PVOID Address, IN ULONG Size); +XTAPI +PMMPTE +MmpGetPdeAddress(PVOID Address); + +XTAPI +PMMPTE +MmpGetPteAddress(PVOID Address); + XTAPI VOID MmpInitializeArchitecture(VOID); +XTAPI +BOOLEAN +MmpMemoryExtensionEnabled(VOID); + #endif /* __XTOSKRNL_I686_MMI_H */ diff --git a/xtoskrnl/includes/mmi.h b/xtoskrnl/includes/mmi.h index f72ef2f..f9105c2 100644 --- a/xtoskrnl/includes/mmi.h +++ b/xtoskrnl/includes/mmi.h @@ -43,6 +43,10 @@ XTAPI VOID MmFreeProcessorStructures(IN PVOID StructuresData); +XTAPI +VOID +MmInitializeHardware(VOID); + XTAPI VOID MmInitializeMemoryManager(VOID); diff --git a/xtoskrnl/mm/amd64/init.c b/xtoskrnl/mm/amd64/init.c index 53e8d71..f746c4d 100644 --- a/xtoskrnl/mm/amd64/init.c +++ b/xtoskrnl/mm/amd64/init.c @@ -102,3 +102,18 @@ MmpInitializeArchitecture(VOID) { UNIMPLEMENTED; } + +/** + * Checks if LA57 (PML5) is enabled. + * + * @return This routine returns TRUE if LA57 is enabled, or FALSE otherwise. + * + * @since XT 1.0 + */ +XTAPI +BOOLEAN +MmpMemoryExtensionEnabled(VOID) +{ + /* Check if LA57 (PML5) is enabled */ + return ((ArReadControlRegister(4) & CR4_LA57) != 0) ? TRUE : FALSE; +} diff --git a/xtoskrnl/mm/globals.c b/xtoskrnl/mm/globals.c index c3f10fa..1c1b9ba 100644 --- a/xtoskrnl/mm/globals.c +++ b/xtoskrnl/mm/globals.c @@ -36,5 +36,8 @@ LOADER_MEMORY_MAPPING MmpHalAllocationDescriptors[MM_HAL_ALLOCATION_DESCRIPTORS] /* Live address of kernel HAL heap */ PVOID MmpHalHeapStart = MM_HAL_HEAP_START_ADDRESS; +/* Architecture-specific memory extension */ +BOOLEAN MmpMemoryExtension; + /* Number of used HAL allocation descriptors */ ULONG MmpUsedHalAllocationDescriptors = 0; diff --git a/xtoskrnl/mm/i686/init.c b/xtoskrnl/mm/i686/init.c index 26aa75b..100f6f6 100644 --- a/xtoskrnl/mm/i686/init.c +++ b/xtoskrnl/mm/i686/init.c @@ -9,6 +9,67 @@ #include +/** + * Gets the address of the PDE (Page Directory Entry), that maps given address. + * + * @param Address + * Specifies the address to find the PDE for. + * + * @return This routine returns the address of the PDE. + * + * @since XT 1.0 + */ +XTAPI +PMMPTE +MmpGetPdeAddress(PVOID Address) +{ + ULONG Offset, PdeBase, PdiShift, PteShift; + + /* Get PDI and PTE shifts based on memory extension flag */ + if(MmpMemoryExtension) + { + /* Get bit shifts for PAE system */ + PdeBase = MM_PDE_PAE_BASE; + PdiShift = MM_PDI_PAE_SHIFT; + PteShift = MM_PTE_PAE_SHIFT; + } + else + { + /* Get bit shifts for non-PAE system */ + PdeBase = MM_PDE_BASE; + PdiShift = MM_PDI_SHIFT; + PteShift = MM_PTE_SHIFT; + } + + /* Calculate offset and return PTE address */ + Offset = ((((ULONG)(Address)) >> PdiShift) << PteShift); + return (PMMPTE)(PdeBase + Offset); +} + +/** + * Gets the address of the PTE (Page Table Entry), that maps given address. + * + * @param Address + * Specifies the address to find the PTE for. + * + * @return This routine returns the address of the PTE. + * + * @since XT 1.0 + */ +XTAPI +PMMPTE +MmpGetPteAddress(PVOID Address) +{ + ULONG Offset, PteShift; + + /* Get PTE shift based on memory extension flag */ + PteShift = MmpMemoryExtension ? MM_PTE_PAE_SHIFT : MM_PTE_SHIFT; + + /* Calculate offset and return PTE address */ + Offset = ((((ULONG)(Address)) >> MM_PTI_SHIFT) << PteShift); + return (PMMPTE)(MM_PTE_BASE + Offset); +} + /** * Performs architecture specific initialization of the XTOS Memory Manager. * @@ -22,3 +83,18 @@ MmpInitializeArchitecture(VOID) { UNIMPLEMENTED; } + +/** + * Checks if PAE (Physical Address Extension) is enabled. + * + * @return This routine returns TRUE if PAE is enabled, or FALSE otherwise. + * + * @since XT 1.0 + */ +XTAPI +BOOLEAN +MmpMemoryExtensionEnabled(VOID) +{ + /* Check if PAE is enabled */ + return ((ArReadControlRegister(4) & CR4_PAE) != 0) ? TRUE : FALSE; +} diff --git a/xtoskrnl/mm/init.c b/xtoskrnl/mm/init.c index 6a51123..4116891 100644 --- a/xtoskrnl/mm/init.c +++ b/xtoskrnl/mm/init.c @@ -9,6 +9,21 @@ #include +/** + * Performs an early pre-initialization of hardware-related structures. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +MmInitializeHardware(VOID) +{ + /* Check for architecture-specific memory extensions enabled by the firmware or boot loader */ + MmpMemoryExtension = MmpMemoryExtensionEnabled(); +} + /** * Performs an early initialization of the XTOS Memory Manager. *