MdePkg/MemoryAllocationLib: Add Allocate(Aligned)CodePages

This commit is contained in:
Mikhail Krichanov 2023-07-07 09:41:57 +03:00
parent e23139949b
commit 091bca8bc8
19 changed files with 101 additions and 109 deletions

View File

@ -6,7 +6,6 @@
**/
#include "ProcessorBind.h"
#include <PrePi.h>
//
@ -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 (

View File

@ -16,6 +16,7 @@
#include <Library/DebugLib.h>
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData;
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode;
EFI_STATUS
EFIAPI

View File

@ -24,6 +24,7 @@
#include <Library/EmuThunkLib.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.

View File

@ -28,6 +28,7 @@
#include <Library/MemoryProfileLib.h>
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;

View File

@ -13,6 +13,7 @@
#include <Library/PhaseMemoryAllocationLib.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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -17,6 +17,7 @@
#include <Library/HobLib.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.

View File

@ -28,6 +28,7 @@
#include <Library/DebugLib.h>
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;

View File

@ -15,6 +15,7 @@
#include <Library/DebugLib.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.

View File

@ -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;
EFI_MM_SYSTEM_TABLE *gMmst = NULL;

View File

@ -16,6 +16,7 @@
#include <Library/MmServicesTableLib.h>
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.

View File

@ -1688,33 +1688,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 = gSmst->SmmAllocatePages (AllocateAnyPages, EfiRuntimeServicesCode, Pages, &Memory);
if (EFI_ERROR (Status)) {
return NULL;
}
return (VOID *)(UINTN)Memory;
}
/**
Perform the remaining tasks.

View File

@ -1019,18 +1019,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.
//

View File

@ -19,6 +19,7 @@
#include <Guid/MemoryAllocationHob.h>
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.

View File

@ -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

View File

@ -34,6 +34,7 @@ typedef struct {
} PAGE_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.