You've already forked edk2-upstream
mirror of
https://github.com/Dasharo/edk2-upstream.git
synced 2026-03-06 15:03:57 -08:00
When allocating Runtime memory the MemoryAllocation HOB's length needs to meet the alignment requirements of Runtime memory. On AARCH64 architectures this alignment is not the default of 4KB. Instead it is set by the RUNTIME_PAGE_ALLOCATION_GRANULARITY definition, which is 64KB. This change enforces the RUNTIME alignment as part of generating the MemoryAllocationHob for runtime memory types. Signed-off-by: Antaeus Kleinert-Strand <antklein@microsoft.com>
469 lines
14 KiB
C
469 lines
14 KiB
C
/** @file
|
|
Implementation of the 6 PEI Ffs (FV) APIs in library form.
|
|
|
|
Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
|
|
|
**/
|
|
|
|
#include <PiPei.h>
|
|
|
|
#include <Library/BaseLib.h>
|
|
#include <Library/BaseMemoryLib.h>
|
|
#include <Library/PrePiLib.h>
|
|
#include <Library/DebugLib.h>
|
|
|
|
STATIC
|
|
VOID *
|
|
EFIAPI
|
|
InternalAllocatePages (
|
|
IN UINTN Pages,
|
|
IN EFI_MEMORY_TYPE MemoryType
|
|
)
|
|
{
|
|
EFI_PEI_HOB_POINTERS Hob;
|
|
EFI_PHYSICAL_ADDRESS NewTop;
|
|
UINTN PagesSize;
|
|
|
|
Hob.Raw = GetHobList ();
|
|
|
|
if (Pages == 0) {
|
|
return NULL;
|
|
}
|
|
|
|
//
|
|
// Calculate the new top of memory based on the memory type
|
|
//
|
|
switch (MemoryType) {
|
|
case EfiReservedMemoryType:
|
|
case EfiRuntimeServicesCode:
|
|
case EfiRuntimeServicesData:
|
|
case EfiACPIMemoryNVS:
|
|
PagesSize = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (RUNTIME_PAGE_ALLOCATION_GRANULARITY)) * EFI_PAGE_SIZE;
|
|
NewTop = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)(RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1);
|
|
break;
|
|
|
|
default:
|
|
PagesSize = Pages * EFI_PAGE_SIZE;
|
|
NewTop = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Verify that the subtraction does not underflow NewTop
|
|
//
|
|
if (PagesSize > NewTop) {
|
|
return NULL;
|
|
}
|
|
|
|
NewTop -= PagesSize;
|
|
|
|
//
|
|
// Verify that there is sufficient memory to satisfy the allocation
|
|
//
|
|
if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
|
|
return NULL;
|
|
}
|
|
|
|
//
|
|
// Update the PHIT to reflect the memory usage
|
|
//
|
|
Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
|
|
|
|
//
|
|
// Create a memory allocation HOB.
|
|
//
|
|
BuildMemoryAllocationHob (
|
|
Hob.HandoffInformationTable->EfiFreeMemoryTop,
|
|
PagesSize,
|
|
MemoryType
|
|
);
|
|
|
|
return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
|
|
}
|
|
|
|
/**
|
|
Allocates one or more 4KB pages of type EfiBootServicesData.
|
|
|
|
Allocates the number of 4KB pages of MemoryType 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
|
|
AllocatePages (
|
|
IN UINTN Pages
|
|
)
|
|
{
|
|
return InternalAllocatePages (Pages, EfiBootServicesData);
|
|
}
|
|
|
|
/**
|
|
Allocates one or more 4KB pages of type EfiRuntimeServicesData.
|
|
|
|
Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
|
|
allocated buffer. The buffer returned is aligned on a RUNTIME_PAGE_ALLOCATION_GRANULARITY 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
|
|
AllocateRuntimePages (
|
|
IN UINTN Pages
|
|
)
|
|
{
|
|
return InternalAllocatePages (Pages, EfiRuntimeServicesData);
|
|
}
|
|
|
|
/**
|
|
Allocates one or more 4KB pages of type EfiReservedMemoryType.
|
|
|
|
Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
|
|
allocated buffer. The buffer returned is aligned on a RUNTIME_PAGE_ALLOCATION_GRANULARITY 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
|
|
AllocateReservedPages (
|
|
IN UINTN Pages
|
|
)
|
|
{
|
|
return InternalAllocatePages (Pages, EfiReservedMemoryType);
|
|
}
|
|
|
|
/**
|
|
Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
|
|
|
|
Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData 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().
|
|
|
|
@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
|
|
AllocateAlignedPages (
|
|
IN UINTN Pages,
|
|
IN UINTN Alignment
|
|
)
|
|
{
|
|
VOID *Memory;
|
|
UINTN AlignmentMask;
|
|
|
|
//
|
|
// Alignment must be a power of two or zero.
|
|
//
|
|
ASSERT ((Alignment & (Alignment - 1)) == 0);
|
|
|
|
if (Pages == 0) {
|
|
return NULL;
|
|
}
|
|
|
|
//
|
|
// Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
|
|
//
|
|
ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
|
|
//
|
|
// We would rather waste some memory to save PEI code size.
|
|
//
|
|
Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
|
|
if (Alignment == 0) {
|
|
AlignmentMask = Alignment;
|
|
} else {
|
|
AlignmentMask = Alignment - 1;
|
|
}
|
|
|
|
return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
|
|
}
|
|
|
|
/**
|
|
Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
|
|
|
|
Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType 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().
|
|
|
|
@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
|
|
AllocateAlignedReservedPages (
|
|
IN UINTN Pages,
|
|
IN UINTN Alignment
|
|
)
|
|
{
|
|
VOID *Memory;
|
|
UINTN AlignmentMask;
|
|
|
|
//
|
|
// Alignment must be a power of two or zero.
|
|
//
|
|
ASSERT ((Alignment & (Alignment - 1)) == 0);
|
|
|
|
if (Pages == 0) {
|
|
return NULL;
|
|
}
|
|
|
|
//
|
|
// Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
|
|
//
|
|
ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
|
|
//
|
|
// We would rather waste some memory to save PEI code size.
|
|
//
|
|
Memory = (VOID *)(UINTN)AllocateReservedPages (Pages + EFI_SIZE_TO_PAGES (Alignment));
|
|
if (Alignment == 0) {
|
|
AlignmentMask = Alignment;
|
|
} else {
|
|
AlignmentMask = Alignment - 1;
|
|
}
|
|
|
|
return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
|
|
}
|
|
|
|
/**
|
|
Frees one or more 4KB pages that were previously allocated with one of the page allocation
|
|
functions in the Memory Allocation Library.
|
|
|
|
Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
|
|
must have been allocated on a previous call to the page allocation services of the Memory
|
|
Allocation Library. If it is not possible to free allocated pages, then this function will
|
|
perform no actions.
|
|
|
|
If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
|
|
then ASSERT().
|
|
If Pages is zero, then ASSERT().
|
|
|
|
@param Buffer Pointer to the buffer of pages to free.
|
|
@param Pages The number of 4 KB pages to free.
|
|
|
|
**/
|
|
VOID
|
|
EFIAPI
|
|
FreePages (
|
|
IN VOID *Buffer,
|
|
IN UINTN Pages
|
|
)
|
|
{
|
|
// For now, we do not support the ability to free pages in the PrePei Memory Allocator.
|
|
// The allocated memory is lost.
|
|
}
|
|
|
|
/**
|
|
Allocates a buffer of type EfiBootServicesData.
|
|
|
|
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
|
|
pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
|
|
returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
|
|
|
|
@param AllocationSize The number of bytes to allocate.
|
|
|
|
@return A pointer to the allocated buffer or NULL if allocation fails.
|
|
|
|
**/
|
|
VOID *
|
|
EFIAPI
|
|
AllocatePool (
|
|
IN UINTN AllocationSize
|
|
)
|
|
{
|
|
EFI_HOB_MEMORY_POOL *Hob;
|
|
|
|
Hob = GetHobList ();
|
|
|
|
//
|
|
// Verify that there is sufficient memory to satisfy the allocation
|
|
//
|
|
if (AllocationSize > 0x10000) {
|
|
// Please call AllocatePages for big allocations
|
|
return 0;
|
|
} else {
|
|
Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (
|
|
EFI_HOB_TYPE_MEMORY_POOL,
|
|
(UINT16)(sizeof (EFI_HOB_MEMORY_POOL) +
|
|
AllocationSize)
|
|
);
|
|
return (VOID *)(Hob + 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
Allocates and zeros a buffer of type EfiBootServicesData.
|
|
|
|
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
|
|
buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
|
|
valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
|
|
request, then NULL is returned.
|
|
|
|
@param AllocationSize The number of bytes to allocate and zero.
|
|
|
|
@return A pointer to the allocated buffer or NULL if allocation fails.
|
|
|
|
**/
|
|
VOID *
|
|
EFIAPI
|
|
AllocateZeroPool (
|
|
IN UINTN AllocationSize
|
|
)
|
|
{
|
|
VOID *Buffer;
|
|
|
|
Buffer = AllocatePool (AllocationSize);
|
|
if (Buffer == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
ZeroMem (Buffer, AllocationSize);
|
|
|
|
return Buffer;
|
|
}
|
|
|
|
/**
|
|
Frees a buffer that was previously allocated with one of the pool allocation functions in the
|
|
Memory Allocation Library.
|
|
|
|
Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
|
|
pool allocation services of the Memory Allocation Library. If it is not possible to free pool
|
|
resources, then this function will perform no actions.
|
|
|
|
If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
|
|
then ASSERT().
|
|
|
|
@param Buffer Pointer to the buffer to free.
|
|
|
|
**/
|
|
VOID
|
|
EFIAPI
|
|
FreePool (
|
|
IN VOID *Buffer
|
|
)
|
|
{
|
|
// Not implemented yet
|
|
}
|
|
|
|
/**
|
|
Reallocates a buffer of type EfiBootServicesData.
|
|
|
|
Allocates and zeros the number bytes specified by NewSize from memory of type
|
|
EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
|
|
NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
|
|
OldBuffer is freed. A pointer to the newly allocated buffer is returned.
|
|
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
|
|
enough memory remaining to satisfy the request, then NULL is returned.
|
|
|
|
If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
|
|
is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
|
|
|
|
@param OldSize The size, in bytes, of OldBuffer.
|
|
@param NewSize The size, in bytes, of the buffer to reallocate.
|
|
@param OldBuffer The buffer to copy to the allocated buffer. This is an optional
|
|
parameter that may be NULL.
|
|
|
|
@return A pointer to the allocated buffer or NULL if allocation fails.
|
|
|
|
**/
|
|
VOID *
|
|
EFIAPI
|
|
ReallocatePool (
|
|
IN UINTN OldSize,
|
|
IN UINTN NewSize,
|
|
IN VOID *OldBuffer OPTIONAL
|
|
)
|
|
{
|
|
VOID *NewBuffer;
|
|
|
|
// Validate the OldBuffer is HobAllocated.
|
|
DEBUG_CODE_BEGIN ();
|
|
EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
|
|
|
|
if (OldBuffer != NULL) {
|
|
HandOffHob = GetHobList ();
|
|
ASSERT (((EFI_PHYSICAL_ADDRESS)(UINTN)OldBuffer >= HandOffHob->EfiMemoryBottom));
|
|
ASSERT (((EFI_PHYSICAL_ADDRESS)((UINTN)OldBuffer + OldSize) <= HandOffHob->EfiFreeMemoryBottom));
|
|
}
|
|
|
|
DEBUG_CODE_END ();
|
|
|
|
// If new buffer would be smaller just return old buffer as FreePool isn't supported.
|
|
if ((OldBuffer != NULL) && (OldSize >= NewSize)) {
|
|
return OldBuffer;
|
|
}
|
|
|
|
NewBuffer = AllocateZeroPool (NewSize);
|
|
if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
|
|
CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
|
|
FreePool (OldBuffer);
|
|
}
|
|
|
|
return NewBuffer;
|
|
}
|
|
|
|
/**
|
|
Copies a buffer to an allocated buffer of type EfiBootServicesData.
|
|
|
|
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
|
|
AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
|
|
allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
|
|
is not enough memory remaining to satisfy the request, then NULL is returned.
|
|
|
|
If Buffer is NULL, then ASSERT().
|
|
If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
|
|
|
|
@param AllocationSize The number of bytes to allocate and zero.
|
|
@param Buffer The buffer to copy to the allocated buffer.
|
|
|
|
@return A pointer to the allocated buffer or NULL if allocation fails.
|
|
|
|
**/
|
|
VOID *
|
|
EFIAPI
|
|
AllocateCopyPool (
|
|
IN UINTN AllocationSize,
|
|
IN CONST VOID *Buffer
|
|
)
|
|
{
|
|
VOID *Memory;
|
|
|
|
ASSERT (Buffer != NULL);
|
|
ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));
|
|
|
|
Memory = AllocatePool (AllocationSize);
|
|
if (Memory != NULL) {
|
|
Memory = CopyMem (Memory, Buffer, AllocationSize);
|
|
}
|
|
|
|
return Memory;
|
|
}
|