From 6176ca38a86b67b5d05dfe77e1c199e70c5c8962 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 2 Jun 2024 17:29:31 +0200 Subject: [PATCH] Cleanup hardware allocation memory pool related code --- sdk/xtdk/amd64/mmtypes.h | 6 ++--- sdk/xtdk/i686/mmtypes.h | 6 ++--- sdk/xtdk/xtfw.h | 2 +- xtldr/modules/xtos_o/amd64/memory.c | 28 +++++++++---------- xtoskrnl/includes/globals.h | 12 ++++----- xtoskrnl/mm/globals.c | 12 ++++----- xtoskrnl/mm/hlpool.c | 42 ++++++++++++++--------------- xtoskrnl/mm/init.c | 2 +- 8 files changed, 55 insertions(+), 55 deletions(-) diff --git a/sdk/xtdk/amd64/mmtypes.h b/sdk/xtdk/amd64/mmtypes.h index f6c91c1..a961e37 100644 --- a/sdk/xtdk/amd64/mmtypes.h +++ b/sdk/xtdk/amd64/mmtypes.h @@ -45,13 +45,13 @@ #define MM_DEFAULT_SECONDARY_COLORS 64 /* Number of HAL allocation descriptors */ -#define MM_HAL_ALLOCATION_DESCRIPTORS 64 +#define MM_HARDWARE_ALLOCATION_DESCRIPTORS 64 /* Kernel HAL heap initial start address */ -#define MM_HAL_HEAP_START_ADDRESS ((PVOID)(((ULONG_PTR)MM_HAL_VA_START) + 1024 * 1024)) +#define MM_HARDWARE_HEAP_START_ADDRESS ((PVOID)(((ULONG_PTR)MM_HARDWARE_VA_START) + 1024 * 1024)) /* HAL memory pool virtual address start */ -#define MM_HAL_VA_START 0xFFFFFFFFFFC00000ULL +#define MM_HARDWARE_VA_START 0xFFFFFFFFFFC00000ULL /* Maximum physical address used by HAL allocations */ #define MM_MAXIMUM_PHYSICAL_ADDRESS 0x00000000FFFFFFFF diff --git a/sdk/xtdk/i686/mmtypes.h b/sdk/xtdk/i686/mmtypes.h index ffeae5c..eb97748 100644 --- a/sdk/xtdk/i686/mmtypes.h +++ b/sdk/xtdk/i686/mmtypes.h @@ -38,13 +38,13 @@ #define MM_DEFAULT_SECONDARY_COLORS 64 /* Number of HAL allocation descriptors */ -#define MM_HAL_ALLOCATION_DESCRIPTORS 64 +#define MM_HARDWARE_ALLOCATION_DESCRIPTORS 64 /* Kernel HAL heap initial start address */ -#define MM_HAL_HEAP_START_ADDRESS ((PVOID)(((ULONG_PTR)MM_HAL_VA_START) + 1024 * 1024)) +#define MM_HARDWARE_HEAP_START_ADDRESS ((PVOID)(((ULONG_PTR)MM_HARDWARE_VA_START) + 1024 * 1024)) /* HAL memory pool virtual address start */ -#define MM_HAL_VA_START 0xFFC00000 +#define MM_HARDWARE_VA_START 0xFFC00000 /* Maximum physical address used by HAL allocations */ #define MM_MAXIMUM_PHYSICAL_ADDRESS 0xFFFFFFFF diff --git a/sdk/xtdk/xtfw.h b/sdk/xtdk/xtfw.h index 82b54da..925d429 100644 --- a/sdk/xtdk/xtfw.h +++ b/sdk/xtdk/xtfw.h @@ -48,7 +48,7 @@ typedef enum _LOADER_MEMORY_TYPE LoaderBBTMemory, LoaderReserve, LoaderXIPRom, - LoaderHALCachedMemory, + LoaderHardwareCachedMemory, LoaderMaximum } LOADER_MEMORY_TYPE, *PLOADER_MEMORY_TYPE; diff --git a/xtldr/modules/xtos_o/amd64/memory.c b/xtldr/modules/xtos_o/amd64/memory.c index 0ae55a3..09e19dd 100644 --- a/xtldr/modules/xtos_o/amd64/memory.c +++ b/xtldr/modules/xtos_o/amd64/memory.c @@ -38,7 +38,7 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) PxeBase = ((PHARDWARE_PTE)(PageMap->PtePointer)); /* Check if PXE entry already exists */ - if(!PxeBase[(MM_HAL_VA_START >> MM_PXI_SHIFT) & 0x1FF].Valid) + if(!PxeBase[(MM_HARDWARE_VA_START >> MM_PXI_SHIFT) & 0x1FF].Valid) { /* No valid PXE, allocate memory */ Status = XtLdrProtocol->Memory.AllocatePages(1, &Address); @@ -52,9 +52,9 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) RtlZeroMemory((PVOID)Address, EFI_PAGE_SIZE); /* Make PXE valid */ - PxeBase[(MM_HAL_VA_START >> MM_PXI_SHIFT) & 0x1FF].Valid = 1; - PxeBase[(MM_HAL_VA_START >> MM_PXI_SHIFT) & 0x1FF].PageFrameNumber = Address / EFI_PAGE_SIZE; - PxeBase[(MM_HAL_VA_START >> MM_PXI_SHIFT) & 0x1FF].Writable = 1; + PxeBase[(MM_HARDWARE_VA_START >> MM_PXI_SHIFT) & 0x1FF].Valid = 1; + PxeBase[(MM_HARDWARE_VA_START >> MM_PXI_SHIFT) & 0x1FF].PageFrameNumber = Address / EFI_PAGE_SIZE; + PxeBase[(MM_HARDWARE_VA_START >> MM_PXI_SHIFT) & 0x1FF].Writable = 1; /* Set PPE base address */ PpeBase = (PHARDWARE_PTE)(UINT_PTR)Address; @@ -62,11 +62,11 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) else { /* Set PPE base address based on existing PXE */ - PpeBase = (PHARDWARE_PTE)((PxeBase[(MM_HAL_VA_START >> MM_PXI_SHIFT) & 0x1FF].PageFrameNumber) << EFI_PAGE_SHIFT); + PpeBase = (PHARDWARE_PTE)((PxeBase[(MM_HARDWARE_VA_START >> MM_PXI_SHIFT) & 0x1FF].PageFrameNumber) << EFI_PAGE_SHIFT); } /* Check if PPE entry already exists */ - if(!PpeBase[(MM_HAL_VA_START >> MM_PPI_SHIFT) & 0x1FF].Valid) + if(!PpeBase[(MM_HARDWARE_VA_START >> MM_PPI_SHIFT) & 0x1FF].Valid) { /* No valid PPE, allocate memory */ Status = XtLdrProtocol->Memory.AllocatePages(1, &Address); @@ -80,9 +80,9 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) RtlZeroMemory((PVOID)Address, EFI_PAGE_SIZE); /* Make PPE valid */ - PpeBase[(MM_HAL_VA_START >> MM_PPI_SHIFT) & 0x1FF].Valid = 1; - PpeBase[(MM_HAL_VA_START >> MM_PPI_SHIFT) & 0x1FF].PageFrameNumber = Address / EFI_PAGE_SIZE; - PpeBase[(MM_HAL_VA_START >> MM_PPI_SHIFT) & 0x1FF].Writable = 1; + PpeBase[(MM_HARDWARE_VA_START >> MM_PPI_SHIFT) & 0x1FF].Valid = 1; + PpeBase[(MM_HARDWARE_VA_START >> MM_PPI_SHIFT) & 0x1FF].PageFrameNumber = Address / EFI_PAGE_SIZE; + PpeBase[(MM_HARDWARE_VA_START >> MM_PPI_SHIFT) & 0x1FF].Writable = 1; /* Set PDE base address */ PdeBase = (PHARDWARE_PTE)Address; @@ -90,14 +90,14 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) else { /* Set PDE base address, based on existing PPE */ - PdeBase = (PHARDWARE_PTE)((PpeBase[(MM_HAL_VA_START >> MM_PPI_SHIFT) & 0x1FF].PageFrameNumber) << EFI_PAGE_SHIFT); + PdeBase = (PHARDWARE_PTE)((PpeBase[(MM_HARDWARE_VA_START >> MM_PPI_SHIFT) & 0x1FF].PageFrameNumber) << EFI_PAGE_SHIFT); } /* Loop through 2 PDE entries */ for(UINT Index = 0 ; Index < 2 ; Index++) { /* Check if PDE entry already exists */ - if(!PdeBase[((MM_HAL_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Valid) + if(!PdeBase[((MM_HARDWARE_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Valid) { /* No valid PDE, allocate memory */ Status = XtLdrProtocol->Memory.AllocatePages(1, &Address); @@ -111,9 +111,9 @@ XtMapHalMemory(IN PXTBL_PAGE_MAPPING PageMap) RtlZeroMemory((PVOID)Address, EFI_PAGE_SIZE); /* Make PDE valid */ - PdeBase[((MM_HAL_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Valid = 1; - PdeBase[((MM_HAL_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].PageFrameNumber = Address / EFI_PAGE_SIZE; - PdeBase[((MM_HAL_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Writable = 1; + PdeBase[((MM_HARDWARE_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Valid = 1; + PdeBase[((MM_HARDWARE_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].PageFrameNumber = Address / EFI_PAGE_SIZE; + PdeBase[((MM_HARDWARE_VA_START >> MM_PDI_SHIFT) & 0x1FF) + Index].Writable = 1; } } diff --git a/xtoskrnl/includes/globals.h b/xtoskrnl/includes/globals.h index ef2ea1f..4cf9f95 100644 --- a/xtoskrnl/includes/globals.h +++ b/xtoskrnl/includes/globals.h @@ -69,16 +69,16 @@ EXTERN ULONG MmPageMapLevel; /* Processor structures data (THIS IS A TEMPORARY HACK) */ EXTERN UCHAR MmProcessorStructuresData[MAXIMUM_PROCESSORS][KPROCESSOR_STRUCTURES_SIZE]; -/* Allocation descriptors dedicated for HAL */ -EXTERN LOADER_MEMORY_DESCRIPTOR MmpHalAllocationDescriptors[MM_HAL_ALLOCATION_DESCRIPTORS]; +/* Allocation descriptors dedicated for hardware layer */ +EXTERN LOADER_MEMORY_DESCRIPTOR MmpHardwareAllocationDescriptors[MM_HARDWARE_ALLOCATION_DESCRIPTORS]; -/* Live address of kernel HAL heap */ -EXTERN PVOID MmpHalHeapStart; +/* Live address of kernel's hardware heap */ +EXTERN PVOID MmpHardwareHeapStart; /* Architecture-specific memory extension */ EXTERN BOOLEAN MmpMemoryExtension; -/* Number of used HAL allocation descriptors */ -EXTERN ULONG MmpUsedHalAllocationDescriptors; +/* Number of used hardware allocation descriptors */ +EXTERN ULONG MmpUsedHardwareAllocationDescriptors; #endif /* __XTOSKRNL_GLOBALS_H */ diff --git a/xtoskrnl/mm/globals.c b/xtoskrnl/mm/globals.c index 78e2515..723c6c9 100644 --- a/xtoskrnl/mm/globals.c +++ b/xtoskrnl/mm/globals.c @@ -30,14 +30,14 @@ ULONG MmPageMapLevel; /* Processor structures data (THIS IS A TEMPORARY HACK) */ UCHAR MmProcessorStructuresData[MAXIMUM_PROCESSORS][KPROCESSOR_STRUCTURES_SIZE] = {0}; -/* Allocation descriptors dedicated for HAL */ -LOADER_MEMORY_DESCRIPTOR MmpHalAllocationDescriptors[MM_HAL_ALLOCATION_DESCRIPTORS]; +/* Allocation descriptors dedicated for hardware layer */ +LOADER_MEMORY_DESCRIPTOR MmpHardwareAllocationDescriptors[MM_HARDWARE_ALLOCATION_DESCRIPTORS]; -/* Live address of kernel HAL heap */ -PVOID MmpHalHeapStart = MM_HAL_HEAP_START_ADDRESS; +/* Live address of kernel's hardware heap */ +PVOID MmpHardwareHeapStart = MM_HARDWARE_HEAP_START_ADDRESS; /* Architecture-specific memory extension */ BOOLEAN MmpMemoryExtension; -/* Number of used HAL allocation descriptors */ -ULONG MmpUsedHalAllocationDescriptors = 0; +/* Number of used hardware allocation descriptors */ +ULONG MmpUsedHardwareAllocationDescriptors = 0; diff --git a/xtoskrnl/mm/hlpool.c b/xtoskrnl/mm/hlpool.c index a184010..904f111 100644 --- a/xtoskrnl/mm/hlpool.c +++ b/xtoskrnl/mm/hlpool.c @@ -31,7 +31,7 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, IN BOOLEAN Aligned, OUT PULONG_PTR Buffer) { - PLOADER_MEMORY_DESCRIPTOR Descriptor, ExtraDescriptor, HalDescriptor; + PLOADER_MEMORY_DESCRIPTOR Descriptor, ExtraDescriptor, HardwareDescriptor; PFN_NUMBER Alignment, MaxPage; ULONGLONG PhysicalAddress; PLIST_ENTRY ListEntry; @@ -43,7 +43,7 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, MaxPage = MM_MAXIMUM_PHYSICAL_ADDRESS >> MM_PAGE_SHIFT; /* Make sure there are at least 2 descriptors available */ - if((MmpUsedHalAllocationDescriptors + 2) > MM_HAL_ALLOCATION_DESCRIPTORS) + if((MmpUsedHardwareAllocationDescriptors + 2) > MM_HARDWARE_ALLOCATION_DESCRIPTORS) { /* Not enough descriptors, return error */ return STATUS_INSUFFICIENT_RESOURCES; @@ -84,13 +84,13 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, } /* Allocate new descriptor */ - HalDescriptor = &MmpHalAllocationDescriptors[MmpUsedHalAllocationDescriptors]; - HalDescriptor->BasePage = Descriptor->BasePage + Alignment; - HalDescriptor->MemoryType = LoaderHALCachedMemory; - HalDescriptor->PageCount = PageCount; + HardwareDescriptor = &MmpHardwareAllocationDescriptors[MmpUsedHardwareAllocationDescriptors]; + HardwareDescriptor->BasePage = Descriptor->BasePage + Alignment; + HardwareDescriptor->MemoryType = LoaderHardwareCachedMemory; + HardwareDescriptor->PageCount = PageCount; - /* Update HAL allocation descriptors count */ - MmpUsedHalAllocationDescriptors++; + /* Update hardware allocation descriptors count */ + MmpUsedHardwareAllocationDescriptors++; /* Check if alignment was done */ if(Alignment) @@ -99,13 +99,13 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, if(Descriptor->PageCount > (PageCount + Alignment)) { /* Initialize extra descriptor */ - ExtraDescriptor = &MmpHalAllocationDescriptors[MmpUsedHalAllocationDescriptors]; + ExtraDescriptor = &MmpHardwareAllocationDescriptors[MmpUsedHardwareAllocationDescriptors]; ExtraDescriptor->BasePage = Descriptor->BasePage + Alignment + (ULONG)PageCount; ExtraDescriptor->MemoryType = LoaderFree; ExtraDescriptor->PageCount = Descriptor->PageCount - (Alignment + (ULONG)PageCount); - /* Update HAL allocation descriptors count */ - MmpUsedHalAllocationDescriptors++; + /* Update hardware allocation descriptors count */ + MmpUsedHardwareAllocationDescriptors++; /* Insert extra descriptor in the list */ RtlInsertHeadList(&Descriptor->ListEntry, &ExtraDescriptor->ListEntry); @@ -115,7 +115,7 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, Descriptor->PageCount = Alignment; /* Insert new descriptor in the list */ - RtlInsertHeadList(&Descriptor->ListEntry, &HalDescriptor->ListEntry); + RtlInsertHeadList(&Descriptor->ListEntry, &HardwareDescriptor->ListEntry); } else { @@ -124,7 +124,7 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, Descriptor->PageCount -= (ULONG)PageCount; /* Insert new descriptor in the list */ - RtlInsertTailList(&Descriptor->ListEntry, &HalDescriptor->ListEntry); + RtlInsertTailList(&Descriptor->ListEntry, &HardwareDescriptor->ListEntry); /* Check if source descriptor is fully consumed */ if(Descriptor->PageCount == 0) @@ -140,7 +140,7 @@ MmAllocateHardwareMemory(IN PFN_NUMBER PageCount, } /** - * Maps physical address to the virtual memory area used by kernel hardware layer (HAL). + * Maps physical address to the virtual memory area used by kernel hardware layer. * * @param PhysicalAddress * Supplies the physical address to map. @@ -170,7 +170,7 @@ MmMapHardwareMemory(IN PHYSICAL_ADDRESS PhysicalAddress, PHARDWARE_PTE PtePointer; /* Initialize variables */ - BaseAddress = MmpHalHeapStart; + BaseAddress = MmpHardwareHeapStart; MappedPages = 0; ReturnAddress = BaseAddress; *VirtualAddress = NULL; @@ -206,10 +206,10 @@ MmMapHardwareMemory(IN PHYSICAL_ADDRESS PhysicalAddress, ReturnAddress = (PVOID)(ULONG_PTR)(BaseAddress + PAGE_OFFSET(PhysicalAddress.LowPart)); /* Check if base address starts at the beginning of the heap */ - if(BaseAddress == MmpHalHeapStart) + if(BaseAddress == MmpHardwareHeapStart) { /* Move heap beyond base address */ - MmpHalHeapStart = (PVOID)((ULONG_PTR)BaseAddress + ((ULONG_PTR)PageCount << MM_PAGE_SHIFT)); + MmpHardwareHeapStart = (PVOID)((ULONG_PTR)BaseAddress + ((ULONG_PTR)PageCount << MM_PAGE_SHIFT)); } /* Iterate through mapped pages */ @@ -339,8 +339,8 @@ MmUnmapHardwareMemory(IN PVOID VirtualAddress, PHARDWARE_PTE PtePointer; PFN_NUMBER Page; - /* Check if address is valid HAL memory */ - if(VirtualAddress < (PVOID)MM_HAL_VA_START) + /* Check if address is valid hardware memory */ + if(VirtualAddress < (PVOID)MM_HARDWARE_VA_START) { /* Invalid address, return error */ return STATUS_INVALID_PARAMETER; @@ -372,10 +372,10 @@ MmUnmapHardwareMemory(IN PVOID VirtualAddress, } /* Check if heap can be reused */ - if(MmpHalHeapStart > VirtualAddress) + if(MmpHardwareHeapStart > VirtualAddress) { /* Free VA space */ - MmpHalHeapStart = VirtualAddress; + MmpHardwareHeapStart = VirtualAddress; } /* Return success */ diff --git a/xtoskrnl/mm/init.c b/xtoskrnl/mm/init.c index 2aa5ae0..a7d5afd 100644 --- a/xtoskrnl/mm/init.c +++ b/xtoskrnl/mm/init.c @@ -65,7 +65,7 @@ MmpScanMemoryDescriptors(VOID) /* Check if memory type is invisible or cached */ if(MmpVerifyMemoryTypeInvisible(MemoryDescriptor->MemoryType) || - (MemoryDescriptor->MemoryType == LoaderHALCachedMemory)) + (MemoryDescriptor->MemoryType == LoaderHardwareCachedMemory)) { /* Skip this mapping */ MemoryMappings = MemoryMappings->Flink;