From 6b652ab025dd60e11247ec56ea823fb6c6fdac3a Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Mon, 7 Apr 2025 12:16:51 +0300 Subject: [PATCH] MdePkg/MemoryAllocationLib: Add Allocate(Aligned)CodePages --- EmbeddedPkg/Library/PrePiLib/PrePiLib.c | 33 -------------- .../MemoryAllocationLib.c | 1 + .../MemoryAllocationLib.c | 1 + .../Core/PiSmmCore/MemoryAllocation.c | 1 + .../BaseMemoryAllocationLibNull.c | 1 + .../CommonMemoryAllocationLib.c | 40 +++++++++++++++++ .../MemoryAllocationLib.c | 1 + MdePkg/Include/Library/MemoryAllocationLib.h | 44 +++++++++++++++++++ .../Library/PhaseMemoryAllocationLib.h | 5 +++ .../MemoryAllocationLib.c | 1 + .../MemoryAllocationLib.c | 1 + .../MemoryAllocationLib.c | 1 + .../StandaloneMmCoreMemoryAllocationLib.c | 1 + .../StandaloneMmMemoryAllocationLib.c | 1 + UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c | 27 ------------ UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h | 12 ----- .../MemoryAllocationLib.c | 1 + UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c | 37 ---------------- .../MemoryAllocationLibPosix.c | 1 + 19 files changed, 101 insertions(+), 109 deletions(-) diff --git a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c index 650d864264..98d5eee1cc 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c +++ b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c @@ -6,7 +6,6 @@ **/ -#include "ProcessorBind.h" #include // @@ -23,38 +22,6 @@ SecWinNtPeiLoadFile ( IN EFI_PHYSICAL_ADDRESS *EntryPoint ); -STATIC -VOID * -EFIAPI -AllocateCodePages ( - IN UINTN Pages - ) -{ - VOID *Alloc; - EFI_PEI_HOB_POINTERS Hob; - - Alloc = AllocatePages (Pages); - if (Alloc == NULL) { - return NULL; - } - - // find the HOB we just created, and change the type to EfiBootServicesCode - Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); - while (Hob.Raw != NULL) { - if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == (UINTN)Alloc) { - Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiBootServicesCode; - return Alloc; - } - - Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob)); - } - - ASSERT (FALSE); - - FreePages (Alloc, Pages); - return NULL; -} - EFI_STATUS EFIAPI LoadUefiImage ( diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c index e056cdb9ed..24bf5142d9 100644 --- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c +++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c @@ -16,6 +16,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; EFI_STATUS EFIAPI diff --git a/EmulatorPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c b/EmulatorPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c index 23aa0b1b7b..dea5363463 100644 --- a/EmulatorPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c +++ b/EmulatorPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c @@ -24,6 +24,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/MdeModulePkg/Core/PiSmmCore/MemoryAllocation.c b/MdeModulePkg/Core/PiSmmCore/MemoryAllocation.c index 66f075531e..e3779c6291 100644 --- a/MdeModulePkg/Core/PiSmmCore/MemoryAllocation.c +++ b/MdeModulePkg/Core/PiSmmCore/MemoryAllocation.c @@ -28,6 +28,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiRuntimeServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiRuntimeServicesCode; EFI_SMRAM_DESCRIPTOR *mSmmCoreMemoryAllocLibSmramRanges = NULL; UINTN mSmmCoreMemoryAllocLibSmramRangeCount = 0; diff --git a/MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLibNull.c b/MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLibNull.c index bd661ca428..2dbb465a7d 100644 --- a/MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLibNull.c +++ b/MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLibNull.c @@ -13,6 +13,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c index 6eed53cfc8..18365a70b9 100644 --- a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c @@ -143,6 +143,36 @@ AllocateReservedPages ( return Buffer; } +/** + Allocates one or more 4KB pages of type EfiBootServicesCode. + + Allocates the number of 4KB pages of type EfiBootServicesCode and returns a pointer to the + allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL + is returned. If there is not enough memory remaining to satisfy the request, then NULL is + returned. + + @param Pages The number of 4 KB pages to allocate. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +EFIAPI +AllocateCodePages ( + IN UINTN Pages + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + + Status = PhaseAllocatePages (AllocateAnyPages, gPhaseDefaultCodeType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + + return (VOID *)(UINTN)Memory; +} + /** Frees one or more 4KB pages that were previously allocated with one of the page allocation functions in the Memory Allocation Library. @@ -342,6 +372,16 @@ AllocateAlignedReservedPages ( return Buffer; } +VOID * +EFIAPI +AllocateAlignedCodePages ( + IN UINTN Pages, + IN UINTN Alignment + ) +{ + return InternalAllocateAlignedPages (gPhaseDefaultCodeType, Pages, Alignment); +} + /** Frees one or more 4KB pages that were previously allocated with one of the aligned page allocation functions in the Memory Allocation Library. diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c index 1d8a263947..1b82e7672c 100644 --- a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c @@ -16,6 +16,7 @@ #include "DxeCoreMemoryAllocationServices.h" GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/MdePkg/Include/Library/MemoryAllocationLib.h b/MdePkg/Include/Library/MemoryAllocationLib.h index 9dd841004d..59d4b29c44 100644 --- a/MdePkg/Include/Library/MemoryAllocationLib.h +++ b/MdePkg/Include/Library/MemoryAllocationLib.h @@ -71,6 +71,25 @@ AllocateReservedPages ( IN UINTN Pages ); +/** + Allocates one or more 4KB pages of type EfiBootServicesCode. + + Allocates the number of 4KB pages of type EfiBootServicesCode and returns a pointer to the + allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL + is returned. If there is not enough memory remaining to satisfy the request, then NULL is + returned. + + @param Pages The number of 4 KB pages to allocate. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +EFIAPI +AllocateCodePages ( + IN UINTN Pages + ); + /** Frees one or more 4KB pages that were previously allocated with one of the page allocation functions in the Memory Allocation Library. @@ -170,6 +189,31 @@ AllocateAlignedReservedPages ( IN UINTN Alignment ); +/** + Allocates one or more 4KB pages of type EfiBootServicesCode at a specified alignment. + + Allocates the number of 4KB pages specified by Pages of type EfiBootServicesCode with an + alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is + returned. If there is not enough memory at the specified alignment remaining to satisfy the + request, then NULL is returned. + + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). + + @param Pages The number of 4 KB pages to allocate. + @param Alignment The requested alignment of the allocation. Must be a power of two. + If Alignment is zero, then byte alignment is used. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +EFIAPI +AllocateAlignedCodePages ( + IN UINTN Pages, + IN UINTN Alignment + ); + /** Frees one or more 4KB pages that were previously allocated with one of the aligned page allocation functions in the Memory Allocation Library. diff --git a/MdePkg/Include/Library/PhaseMemoryAllocationLib.h b/MdePkg/Include/Library/PhaseMemoryAllocationLib.h index fbe1ad6e1f..a7c45dd9a4 100644 --- a/MdePkg/Include/Library/PhaseMemoryAllocationLib.h +++ b/MdePkg/Include/Library/PhaseMemoryAllocationLib.h @@ -114,4 +114,9 @@ PhaseFreePool ( /// extern CONST EFI_MEMORY_TYPE gPhaseDefaultDataType; +/// +/// The memory type to allocate for calls to AllocateCodePages(). +/// +extern CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType; + #endif diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 619a3664f7..4e5ad5b1bb 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -17,6 +17,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c index 0dfd86bedd..bf69b15330 100644 --- a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c @@ -28,6 +28,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiRuntimeServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiRuntimeServicesCode; EFI_SMRAM_DESCRIPTOR *mSmramRanges; UINTN mSmramRangeCount; diff --git a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c index 4b7e18f2e0..9768a3c301 100644 --- a/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c @@ -15,6 +15,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c index 63ee134159..33ce184e2b 100644 --- a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c +++ b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c @@ -18,6 +18,7 @@ #include "StandaloneMmCoreMemoryAllocationServices.h" GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiRuntimeServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiRuntimeServicesCode; static EFI_MM_SYSTEM_TABLE *mMemoryAllocationMmst = NULL; diff --git a/StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.c b/StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.c index b3b38a1568..996acdb6d3 100644 --- a/StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.c +++ b/StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.c @@ -16,6 +16,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiRuntimeServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiRuntimeServicesCode; /** Allocates one or more 4KB pages of a certain memory type. diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c index 5e24c12548..291759f479 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c @@ -1484,33 +1484,6 @@ ConfigSmmCodeAccessCheck ( PERF_FUNCTION_END (); } -/** - Allocate pages for code. - - @param[in] Pages Number of pages to be allocated. - - @return Allocated memory. -**/ -VOID * -AllocateCodePages ( - IN UINTN Pages - ) -{ - EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; - - if (Pages == 0) { - return NULL; - } - - Status = gMmst->MmAllocatePages (AllocateAnyPages, EfiRuntimeServicesCode, Pages, &Memory); - if (EFI_ERROR (Status)) { - return NULL; - } - - return (VOID *)(UINTN)Memory; -} - /** Perform the pre tasks. diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h index d0ad3be63a..2bde958e18 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h @@ -1144,18 +1144,6 @@ AllocatePageTableMemory ( IN UINTN Pages ); -/** - Allocate pages for code. - - @param[in] Pages Number of pages to be allocated. - - @return Allocated memory. -**/ -VOID * -AllocateCodePages ( - IN UINTN Pages - ); - // // S3 related global variable and function prototype. // diff --git a/UefiPayloadPkg/Library/UefiPayloadMemoryAllocationLib/MemoryAllocationLib.c b/UefiPayloadPkg/Library/UefiPayloadMemoryAllocationLib/MemoryAllocationLib.c index c82a8c7c4e..42b5b776ce 100755 --- a/UefiPayloadPkg/Library/UefiPayloadMemoryAllocationLib/MemoryAllocationLib.c +++ b/UefiPayloadPkg/Library/UefiPayloadMemoryAllocationLib/MemoryAllocationLib.c @@ -19,6 +19,7 @@ #include GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Add a new HOB to the HOB List. diff --git a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c index 5904d9cb51..bdec06e974 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c +++ b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c @@ -8,43 +8,6 @@ #include "UefiPayloadEntry.h" -/** - Allocate pages for code. - - @param[in] Pages Number of pages to be allocated. - - @return Allocated memory. -**/ -VOID * -AllocateCodePages ( - IN UINTN Pages - ) -{ - VOID *Alloc; - EFI_PEI_HOB_POINTERS Hob; - - Alloc = AllocatePages (Pages); - if (Alloc == NULL) { - return NULL; - } - - // find the HOB we just created, and change the type to EfiBootServicesCode - Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); - while (Hob.Raw != NULL) { - if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == (UINTN)Alloc) { - Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiBootServicesCode; - return Alloc; - } - - Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob)); - } - - ASSERT (FALSE); - - FreePages (Alloc, Pages); - return NULL; -} - /** Loads and relocates a PE/COFF image diff --git a/UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.c b/UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.c index a4e89b0af8..b44452d879 100644 --- a/UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.c +++ b/UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.c @@ -41,6 +41,7 @@ typedef struct { } POOL_HEAD; GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; /** Allocates one or more 4KB pages of a certain memory type.