Files
slimbootloader/BootloaderCorePkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
T
Maurice Ma 0e1098d7b2 Add DMA protection in core code
This patch added DMA memory type into memory allocation pool for payloads.
This DMA memory buffer with PcdDmaBufferSize is located at address
aligned at PcdDmaBufferAlignment after Payload reserved memory. Memory
type EfiRuntimeServicesData is used to indicate DMA memory type.

Stage1B calculates the DMA memory location using fixed PCDs so that
platform can set up DMA protection as early as possible after memory is
ready. In Stage1B or Stage2 platform code should use platform VTd
information to setup PMR to protect all low memory except for the DMA
buffer range. DMA memory will be added into memory pool at the entry
point of the payload. Before transfering to OS, the DMA memory protection
can be disabled, and the DMA memory pool can be reclaimed for OS usage.

Currently only boot media device will utilize the DMA buffer range for
block access operations. So it should only be required by payloads. GFX,
when enabled, will also use DMA. It will be targeted to the system stolen
memory which is not protected by PMR.

Signed-off-by: Maurice Ma <maurice.ma@intel.com>
2020-03-17 18:04:44 -07:00

333 lines
9.3 KiB
C

/** @file
Support routines for memory allocation routines
based on PeiService for PEI phase drivers.
Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiPei.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/BootloaderCoreLib.h>
#define POOL_MIN_ALIGNMENT 0x10
/**
Update the Memory pool top address.
@param [in] Top Top address to update.
**/
VOID
InternalUpdateMemPoolTop (
IN UINT32 Top
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
LdrGlobal = GetLoaderGlobalDataPointer();
ASSERT (Top >= LdrGlobal->MemPoolCurrBottom);
LdrGlobal->MemPoolCurrTop = Top;
}
/**
Update the Memory pool bottom address.
@param [in] Bottom bottom address to update.
**/
VOID
InternalUpdateMemPoolBottom (
IN UINT32 Bottom
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
LdrGlobal = GetLoaderGlobalDataPointer();
ASSERT (LdrGlobal->MemPoolCurrTop >= Bottom);
LdrGlobal->MemPoolCurrBottom = Bottom;
}
/**
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, LdrGlobal->MemPoolCurrTop is returned.
If there is not enough memory remaining to satisfy the request, InternalUpdateMemPoolTop ASSERTS
and the function does not return.
@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
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
UINT32 Top;
LdrGlobal = GetLoaderGlobalDataPointer();
Top = LdrGlobal->MemPoolCurrTop;
Top -= AllocationSize;
Top = ALIGN_DOWN (Top, POOL_MIN_ALIGNMENT);
InternalUpdateMemPoolTop (Top);
return (VOID *)Top;
}
/**
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,
LdrGlobal->MemPoolCurrTop is returned. If there is not enough memory remaining to satisfy the
request, InternalUpdateMemPoolTop ASSERTS and the function does not return.
@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 *Memory;
Memory = AllocatePool (AllocationSize);
if (Memory != NULL) {
Memory = ZeroMem (Memory, AllocationSize);
}
return Memory;
}
/**
Allocates one or more 4KB pages of type EfiBootServicesData.
Allocates the number of 4KB pages of type EfiBootServicesData 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,
InternalUpdateMemPoolTop ASSERTS and the function does not return.
@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
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
UINT32 Top;
if (Pages == 0) {
return NULL;
}
LdrGlobal = GetLoaderGlobalDataPointer();
Top = LdrGlobal->MemPoolCurrTop;
Top = ALIGN_DOWN (Top, EFI_PAGE_SIZE);
Top -= Pages * EFI_PAGE_SIZE;
InternalUpdateMemPoolTop (Top);
return (VOID *)Top;
}
/**
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 remaining to satisfy the request,
InternalUpdateMemPoolTop ASSERTS and the function does not return.
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
AllocateAlignedPages (
IN UINTN Pages,
IN UINTN Alignment
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
UINT32 Top;
if (Pages == 0) {
return NULL;
}
LdrGlobal = GetLoaderGlobalDataPointer();
Top = LdrGlobal->MemPoolCurrTop;
Top = ALIGN_DOWN (Top, EFI_PAGE_SIZE);
Top -= Pages * EFI_PAGE_SIZE;
if (Alignment > EFI_PAGE_SIZE) {
Top = ALIGN_DOWN (Top, Alignment);
}
Top = ALIGN_DOWN (Top, EFI_PAGE_SIZE);
InternalUpdateMemPoolTop (Top);
return (VOID *)Top;
}
/**
This function allocates temporary memory pool.
@param[in] AllocationSize The memory pool size to allocate. If AllocationSize is 0,
LdrGlobal->MemPoolCurrBottom is returned. If there is not enough memory remaining to satisfy the
request, InternalUpdateMemPoolBottom ASSERTS and the function does not return.
@retval A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocateTemporaryMemory (
IN UINTN AllocationSize
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
UINT32 Bottom;
UINT32 NewBottom;
LdrGlobal = GetLoaderGlobalDataPointer();
Bottom = LdrGlobal->MemPoolCurrBottom;
Bottom = ALIGN_UP (Bottom, POOL_MIN_ALIGNMENT);
NewBottom = Bottom + AllocationSize;
InternalUpdateMemPoolBottom (NewBottom);
return (VOID *)Bottom;
}
/**
This function frees temporary memory pool.
@param[in] Buffer Temporary memory pool to free.
NULL indicates to free all temporary memory pool previously allocated
**/
VOID
EFIAPI
FreeTemporaryMemory (
IN VOID *Buffer
)
{
LOADER_GLOBAL_DATA *LdrGlobal;
UINT32 NewBottom;
LdrGlobal = GetLoaderGlobalDataPointer();
if (Buffer == NULL) {
LdrGlobal->MemPoolCurrBottom = LdrGlobal->MemPoolStart;
} else {
NewBottom = (UINT32)Buffer;
if (NewBottom < LdrGlobal->MemPoolCurrBottom) {
InternalUpdateMemPoolBottom (NewBottom);
}
}
}
/**
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 The 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
)
{
}
/**
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 The pointer to the buffer to free.
**/
VOID
EFIAPI
FreePool (
IN VOID *Buffer
)
{
}
/**
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 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
AllocateRuntimePages (
IN UINTN Pages
)
{
return NULL;
}
/**
Allocates a buffer of type EfiRuntimeServicesData.
Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData 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
AllocateRuntimePool (
IN UINTN AllocationSize
)
{
return NULL;
}