2008-05-21 03:40:12 +02:00
|
|
|
/** @file
|
2008-04-09 09:07:50 +02:00
|
|
|
UEFI Memory pool management functions.
|
|
|
|
|
2018-10-24 06:47:45 +02:00
|
|
|
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:05:13 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-04-09 09:07:50 +02:00
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-08-27 16:29:23 +02:00
|
|
|
#include "DxeMain.h"
|
2008-09-23 09:35:34 +02:00
|
|
|
#include "Imem.h"
|
2017-11-20 09:08:28 +01:00
|
|
|
#include "HeapGuard.h"
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2017-02-24 15:21:18 +01:00
|
|
|
STATIC EFI_LOCK mPoolMemoryLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
|
|
|
|
|
2008-12-16 16:34:21 +01:00
|
|
|
#define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
|
2007-07-04 12:51:54 +02:00
|
|
|
typedef struct {
|
|
|
|
UINT32 Signature;
|
|
|
|
UINT32 Index;
|
|
|
|
LIST_ENTRY Link;
|
|
|
|
} POOL_FREE;
|
|
|
|
|
2018-10-24 06:47:45 +02:00
|
|
|
#define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
|
|
|
|
#define POOLPAGE_HEAD_SIGNATURE SIGNATURE_32('p','h','d','1')
|
2007-07-04 12:51:54 +02:00
|
|
|
typedef struct {
|
|
|
|
UINT32 Signature;
|
2013-10-30 05:54:53 +01:00
|
|
|
UINT32 Reserved;
|
2007-07-04 12:51:54 +02:00
|
|
|
EFI_MEMORY_TYPE Type;
|
2013-10-30 05:54:53 +01:00
|
|
|
UINTN Size;
|
2007-07-04 12:51:54 +02:00
|
|
|
CHAR8 Data[1];
|
|
|
|
} POOL_HEAD;
|
|
|
|
|
2008-12-16 16:34:21 +01:00
|
|
|
#define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-12-16 16:34:21 +01:00
|
|
|
#define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
|
2007-07-04 12:51:54 +02:00
|
|
|
typedef struct {
|
|
|
|
UINT32 Signature;
|
2013-10-30 05:54:53 +01:00
|
|
|
UINT32 Reserved;
|
|
|
|
UINTN Size;
|
2007-07-04 12:51:54 +02:00
|
|
|
} POOL_TAIL;
|
|
|
|
|
|
|
|
#define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
|
|
|
|
|
|
|
|
#define HEAD_TO_TAIL(a) \
|
|
|
|
((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
|
|
|
|
|
2015-03-06 03:54:50 +01:00
|
|
|
//
|
|
|
|
// Each element is the sum of the 2 previous ones: this allows us to migrate
|
|
|
|
// blocks between bins by splitting them up, while not wasting too much memory
|
|
|
|
// as we would in a strict power-of-2 sequence
|
|
|
|
//
|
|
|
|
STATIC CONST UINT16 mPoolSizeTable[] = {
|
2016-04-08 08:34:03 +02:00
|
|
|
128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824
|
2015-03-06 03:54:50 +01:00
|
|
|
};
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2015-03-06 03:54:50 +01:00
|
|
|
#define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
|
|
|
|
#define LIST_TO_SIZE(a) (mPoolSizeTable [a])
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2016-10-26 18:56:35 +02:00
|
|
|
#define MAX_POOL_LIST (ARRAY_SIZE (mPoolSizeTable))
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
#define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
|
|
|
|
|
|
|
|
//
|
|
|
|
// Globals
|
|
|
|
//
|
|
|
|
|
2008-12-16 16:34:21 +01:00
|
|
|
#define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
|
2007-07-04 12:51:54 +02:00
|
|
|
typedef struct {
|
|
|
|
INTN Signature;
|
|
|
|
UINTN Used;
|
|
|
|
EFI_MEMORY_TYPE MemoryType;
|
|
|
|
LIST_ENTRY FreeList[MAX_POOL_LIST];
|
|
|
|
LIST_ENTRY Link;
|
2008-07-24 04:54:45 +02:00
|
|
|
} POOL;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
2008-07-18 11:50:09 +02:00
|
|
|
// Pool header for each memory type.
|
|
|
|
//
|
|
|
|
POOL mPoolHead[EfiMaxMemoryType];
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2008-07-18 11:50:09 +02:00
|
|
|
// List of pool header to search for the appropriate memory type.
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2008-09-17 15:29:44 +02:00
|
|
|
LIST_ENTRY mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2015-03-10 07:57:04 +01:00
|
|
|
/**
|
|
|
|
Get pool size table index from the specified size.
|
|
|
|
|
|
|
|
@param Size The specified size to get index from pool table.
|
|
|
|
|
|
|
|
@return The index of pool size table.
|
|
|
|
|
|
|
|
**/
|
2015-03-06 03:54:50 +01:00
|
|
|
STATIC
|
|
|
|
UINTN
|
|
|
|
GetPoolIndexFromSize (
|
|
|
|
UINTN Size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Index;
|
|
|
|
|
|
|
|
for (Index = 0; Index < MAX_POOL_LIST; Index++) {
|
|
|
|
if (mPoolSizeTable[Index] >= Size) {
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2015-03-06 03:54:50 +01:00
|
|
|
return MAX_POOL_LIST;
|
|
|
|
}
|
2008-05-09 09:08:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Called to initialize the pool.
|
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
VOID
|
|
|
|
CoreInitializePool (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Type;
|
|
|
|
UINTN Index;
|
|
|
|
|
|
|
|
for (Type = 0; Type < EfiMaxMemoryType; Type++) {
|
2008-07-18 11:50:09 +02:00
|
|
|
mPoolHead[Type].Signature = 0;
|
|
|
|
mPoolHead[Type].Used = 0;
|
|
|
|
mPoolHead[Type].MemoryType = (EFI_MEMORY_TYPE)Type;
|
2007-07-04 12:51:54 +02:00
|
|
|
for (Index = 0; Index < MAX_POOL_LIST; Index++) {
|
2013-10-30 05:54:53 +01:00
|
|
|
InitializeListHead (&mPoolHead[Type].FreeList[Index]);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
2007-07-04 12:51:54 +02:00
|
|
|
Look up pool head for specified memory type.
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param MemoryType Memory type of which pool head is looked for
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
@return Pointer of Corresponding pool head.
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
**/
|
|
|
|
POOL *
|
|
|
|
LookupPoolHead (
|
|
|
|
IN EFI_MEMORY_TYPE MemoryType
|
|
|
|
)
|
2007-07-04 12:51:54 +02:00
|
|
|
{
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
POOL *Pool;
|
|
|
|
UINTN Index;
|
|
|
|
|
2012-08-28 08:48:28 +02:00
|
|
|
if ((UINT32)MemoryType < EfiMaxMemoryType) {
|
2008-07-18 11:50:09 +02:00
|
|
|
return &mPoolHead[MemoryType];
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 16:49:45 +02:00
|
|
|
//
|
2015-05-18 03:28:24 +02:00
|
|
|
// MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
|
|
|
|
// OS loaders that are provided by operating system vendors.
|
|
|
|
// MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use.
|
2008-09-23 16:49:45 +02:00
|
|
|
//
|
2015-05-18 03:28:24 +02:00
|
|
|
if ((UINT32)MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
|
2008-07-18 11:50:09 +02:00
|
|
|
for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {
|
2007-07-04 12:51:54 +02:00
|
|
|
Pool = CR (Link, POOL, Link, POOL_SIGNATURE);
|
|
|
|
if (Pool->MemoryType == MemoryType) {
|
|
|
|
return Pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
Pool = CoreAllocatePoolI (EfiBootServicesData, sizeof (POOL), FALSE);
|
2007-07-04 12:51:54 +02:00
|
|
|
if (Pool == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pool->Signature = POOL_SIGNATURE;
|
|
|
|
Pool->Used = 0;
|
|
|
|
Pool->MemoryType = MemoryType;
|
|
|
|
for (Index = 0; Index < MAX_POOL_LIST; Index++) {
|
|
|
|
InitializeListHead (&Pool->FreeList[Index]);
|
|
|
|
}
|
|
|
|
|
2008-07-18 11:50:09 +02:00
|
|
|
InsertHeadList (&mPoolHeadList, &Pool->Link);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
return Pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
|
|
|
Allocate pool of a particular type.
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param PoolType Type of pool to allocate
|
|
|
|
@param Size The amount of pool to allocate
|
|
|
|
@param Buffer The address to return a pointer to the allocated
|
|
|
|
pool
|
2008-05-09 09:08:30 +02:00
|
|
|
|
2016-01-29 09:32:54 +01:00
|
|
|
@retval EFI_INVALID_PARAMETER Buffer is NULL.
|
|
|
|
PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
|
|
|
|
PoolType is EfiPersistentMemory.
|
2008-07-24 04:54:45 +02:00
|
|
|
@retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
|
2008-05-09 09:08:30 +02:00
|
|
|
@retval EFI_SUCCESS Pool successfully allocated.
|
|
|
|
|
|
|
|
**/
|
2007-07-04 12:51:54 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2014-11-12 04:27:48 +01:00
|
|
|
CoreInternalAllocatePool (
|
2007-07-04 12:51:54 +02:00
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
|
|
|
IN UINTN Size,
|
|
|
|
OUT VOID **Buffer
|
|
|
|
)
|
|
|
|
{
|
2017-11-20 09:08:28 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
BOOLEAN NeedGuard;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// If it's not a valid type, fail it
|
|
|
|
//
|
2015-05-18 03:28:24 +02:00
|
|
|
if (((PoolType >= EfiMaxMemoryType) && (PoolType < MEMORY_TYPE_OEM_RESERVED_MIN)) ||
|
2023-01-18 09:41:58 +01:00
|
|
|
(PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory) || (PoolType == EfiUnacceptedMemoryType))
|
2015-04-29 03:37:05 +02:00
|
|
|
{
|
2007-07-04 12:51:54 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2012-04-27 04:48:46 +02:00
|
|
|
if (Buffer == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
*Buffer = NULL;
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// If size is too large, fail it
|
|
|
|
// Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
|
|
|
|
//
|
|
|
|
if (Size > MAX_POOL_SIZE) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
NeedGuard = IsPoolTypeToGuard (PoolType) && !mOnGuarding;
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Acquire the memory lock and make the allocation
|
|
|
|
//
|
2017-02-24 15:21:18 +01:00
|
|
|
Status = CoreAcquireLockOrFail (&mPoolMemoryLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
*Buffer = CoreAllocatePoolI (PoolType, Size, NeedGuard);
|
2017-02-24 15:21:18 +01:00
|
|
|
CoreReleaseLock (&mPoolMemoryLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:48 +01:00
|
|
|
/**
|
|
|
|
Allocate pool of a particular type.
|
|
|
|
|
|
|
|
@param PoolType Type of pool to allocate
|
|
|
|
@param Size The amount of pool to allocate
|
|
|
|
@param Buffer The address to return a pointer to the allocated
|
|
|
|
pool
|
|
|
|
|
2016-01-29 09:32:54 +01:00
|
|
|
@retval EFI_INVALID_PARAMETER Buffer is NULL.
|
|
|
|
PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.
|
|
|
|
PoolType is EfiPersistentMemory.
|
2014-11-12 04:27:48 +01:00
|
|
|
@retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
|
|
|
|
@retval EFI_SUCCESS Pool successfully allocated.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
CoreAllocatePool (
|
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
|
|
|
IN UINTN Size,
|
|
|
|
OUT VOID **Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2014-11-12 04:27:48 +01:00
|
|
|
Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
2016-06-18 03:03:20 +02:00
|
|
|
CoreUpdateProfile (
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),
|
|
|
|
MemoryProfileActionAllocatePool,
|
|
|
|
PoolType,
|
|
|
|
Size,
|
|
|
|
*Buffer,
|
|
|
|
NULL
|
|
|
|
);
|
2016-04-20 11:27:40 +02:00
|
|
|
InstallMemoryAttributesTableOnMemoryAllocation (PoolType);
|
2014-11-12 04:27:48 +01:00
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2014-11-12 04:27:48 +01:00
|
|
|
return Status;
|
|
|
|
}
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2017-03-03 05:44:46 +01:00
|
|
|
/**
|
|
|
|
Internal function. Used by the pool functions to allocate pages
|
|
|
|
to back pool allocation requests.
|
|
|
|
|
|
|
|
@param PoolType The type of memory for the new pool pages
|
|
|
|
@param NoPages No of pages to allocate
|
|
|
|
@param Granularity Bits to align.
|
2017-11-20 09:08:28 +01:00
|
|
|
@param NeedGuard Flag to indicate Guard page is needed or not
|
2017-03-03 05:44:46 +01:00
|
|
|
|
|
|
|
@return The allocated memory, or NULL
|
|
|
|
|
|
|
|
**/
|
2017-02-24 15:21:18 +01:00
|
|
|
STATIC
|
|
|
|
VOID *
|
|
|
|
CoreAllocatePoolPagesI (
|
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
|
|
|
IN UINTN NoPages,
|
2017-11-20 09:08:28 +01:00
|
|
|
IN UINTN Granularity,
|
|
|
|
IN BOOLEAN NeedGuard
|
2017-02-24 15:21:18 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
VOID *Buffer;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
|
|
|
Status = CoreAcquireLockOrFail (&gMemoryLock);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity, NeedGuard);
|
2017-02-24 15:21:18 +01:00
|
|
|
CoreReleaseMemoryLock ();
|
|
|
|
|
2017-02-24 15:51:33 +01:00
|
|
|
if (Buffer != NULL) {
|
2017-11-20 09:08:28 +01:00
|
|
|
if (NeedGuard) {
|
|
|
|
SetGuardForMemory ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, NoPages);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
ApplyMemoryProtectionPolicy (
|
|
|
|
EfiConventionalMemory,
|
|
|
|
PoolType,
|
2017-02-24 15:51:33 +01:00
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
|
|
|
|
EFI_PAGES_TO_SIZE (NoPages)
|
|
|
|
);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2017-02-24 15:21:18 +01:00
|
|
|
return Buffer;
|
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
2007-07-04 12:51:54 +02:00
|
|
|
Internal function to allocate pool of a particular type.
|
|
|
|
Caller must have the memory lock held
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param PoolType Type of pool to allocate
|
|
|
|
@param Size The amount of pool to allocate
|
2017-11-20 09:08:28 +01:00
|
|
|
@param NeedGuard Flag to indicate Guard page is needed or not
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
@return The allocate pool, or NULL
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
**/
|
|
|
|
VOID *
|
|
|
|
CoreAllocatePoolI (
|
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
2017-11-20 09:08:28 +01:00
|
|
|
IN UINTN Size,
|
|
|
|
IN BOOLEAN NeedGuard
|
2008-05-09 09:08:30 +02:00
|
|
|
)
|
2007-07-04 12:51:54 +02:00
|
|
|
{
|
|
|
|
POOL *Pool;
|
|
|
|
POOL_FREE *Free;
|
|
|
|
POOL_HEAD *Head;
|
|
|
|
POOL_TAIL *Tail;
|
|
|
|
CHAR8 *NewPage;
|
|
|
|
VOID *Buffer;
|
|
|
|
UINTN Index;
|
|
|
|
UINTN FSize;
|
2015-03-06 03:57:11 +01:00
|
|
|
UINTN Offset, MaxOffset;
|
2007-07-04 12:51:54 +02:00
|
|
|
UINTN NoPages;
|
2015-03-06 03:54:05 +01:00
|
|
|
UINTN Granularity;
|
2017-11-20 09:08:28 +01:00
|
|
|
BOOLEAN HasPoolTail;
|
2018-10-24 06:47:45 +02:00
|
|
|
BOOLEAN PageAsPool;
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2017-02-24 15:21:18 +01:00
|
|
|
ASSERT_LOCKED (&mPoolMemoryLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2024-03-09 20:06:03 +01:00
|
|
|
if ((PoolType == EfiReservedMemoryType) ||
|
2015-03-06 03:54:05 +01:00
|
|
|
(PoolType == EfiACPIMemoryNVS) ||
|
|
|
|
(PoolType == EfiRuntimeServicesCode) ||
|
|
|
|
(PoolType == EfiRuntimeServicesData))
|
|
|
|
{
|
2017-03-03 16:11:32 +01:00
|
|
|
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
|
2015-03-06 03:54:05 +01:00
|
|
|
} else {
|
2017-03-03 16:11:32 +01:00
|
|
|
Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
|
2015-03-06 03:54:05 +01:00
|
|
|
}
|
|
|
|
|
MdeModulePkg: DxeCore: Do Not Apply Guards to Unsupported Types
Currently, there are multiple issues when page or pool guards are
allocated for runtime memory regions that are aligned to
non-EFI_PAGE_SIZE alignments. Multiple other issues have been fixed for
these same systems (notably ARM64 which has a 64k runtime page
allocation granularity) recently. The heap guard system is only built to
support 4k guard pages and 4k alignment.
Today, the address returned to a caller of AllocatePages will not be
aligned correctly to the runtime page allocation granularity, because
the heap guard system does not take non-4k alignment requirements into
consideration.
However, even with this bug fixed, the Memory Allocation Table cannot be
produced and an OS with a larger than 4k page granularity will not have
aligned memory regions because the guard pages are reported as part of
the same memory allocation. So what would have been, on an ARM64 system,
a 64k runtime memory allocation is actually a 72k memory allocation as
tracked by the Page.c code because the guard pages are tracked as part
of the same allocation. This is a core function of the current heap
guard architecture.
This could also be fixed with rearchitecting the heap guard system to
respect alignment requirements and shift the guard pages inside of the
outer rounded allocation or by having guard pages be the runtime
granularity. Both of these approaches have issues. In the former case,
we break UEFI spec 2.10 section 2.3.6 for AARCH64, which states that
each 64k page for runtime memory regions may not have mixed memory
attributes, which pushing the guard pages inside would create. In the
latter case, an immense amount of memory is wasted to support such large
guard pages, and with pool guard many systems could not support an
additional 128k allocation for all runtime memory.
The simpler and safer solution is to disallow page and pool guards for
runtime memory allocations for systems that have a runtime granularity
greater than the EFI_PAGE_SIZE (4k). The usefulness of such guards is
limited, as OSes do not map guard pages today, so there is only boot
time protection of these ranges. This also prevents other bugs from
being exposed by using guards for regions that have a non-4k alignment
requirement, as again, multiple have cropped up because the heap guard
system was not built to support it.
This patch adds both a static assert to ensure that either the runtime
granularity is the EFI_PAGE_SIZE or that the PCD bits are not set to
enable heap guard for runtime memory regions. It also adds a check in
the page and pool allocation system to ensure that at runtime we are not
allocating a runtime region and attempt to guard it (the PCDs are close
to being removed in favor of dynamic heap guard configurations).
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4674
Github PR: https://github.com/tianocore/edk2/pull/5382
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2024-03-09 20:06:03 +01:00
|
|
|
//
|
|
|
|
// The heap guard system does not support non-EFI_PAGE_SIZE alignments.
|
|
|
|
// Architectures that require larger RUNTIME_PAGE_ALLOCATION_GRANULARITY
|
|
|
|
// will have the runtime memory regions unguarded. OSes do not
|
|
|
|
// map guard pages anyway, so this is a minimal loss. Not guarding prevents
|
|
|
|
// alignment mismatches
|
|
|
|
//
|
|
|
|
if (Granularity != EFI_PAGE_SIZE) {
|
|
|
|
NeedGuard = FALSE;
|
|
|
|
}
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Adjust the size by the pool header & tail overhead
|
|
|
|
//
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
HasPoolTail = !(NeedGuard &&
|
|
|
|
((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));
|
2018-10-24 06:47:45 +02:00
|
|
|
PageAsPool = (IsHeapGuardEnabled (GUARD_HEAP_TYPE_FREED) && !mOnGuarding);
|
2017-11-20 09:08:28 +01:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Adjusting the Size to be of proper alignment so that
|
|
|
|
// we don't get an unaligned access fault later when
|
|
|
|
// pool_Tail is being initialized
|
|
|
|
//
|
2008-09-10 18:05:24 +02:00
|
|
|
Size = ALIGN_VARIABLE (Size);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
Size += POOL_OVERHEAD;
|
|
|
|
Index = SIZE_TO_LIST (Size);
|
|
|
|
Pool = LookupPoolHead (PoolType);
|
|
|
|
if (Pool == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
Head = NULL;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If allocation is over max size, just allocate pages for the request
|
|
|
|
// (slow)
|
|
|
|
//
|
2018-10-24 06:47:45 +02:00
|
|
|
if ((Index >= SIZE_TO_LIST (Granularity)) || NeedGuard || PageAsPool) {
|
2017-11-20 09:08:28 +01:00
|
|
|
if (!HasPoolTail) {
|
|
|
|
Size -= sizeof (POOL_TAIL);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
NoPages = EFI_SIZE_TO_PAGES (Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
|
2015-03-06 03:54:05 +01:00
|
|
|
NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
|
2017-11-20 09:08:28 +01:00
|
|
|
Head = CoreAllocatePoolPagesI (PoolType, NoPages, Granularity, NeedGuard);
|
|
|
|
if (NeedGuard) {
|
|
|
|
Head = AdjustPoolHeadA ((EFI_PHYSICAL_ADDRESS)(UINTN)Head, NoPages, Size);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If there's no free pool in the proper list size, go get some more pages
|
|
|
|
//
|
|
|
|
if (IsListEmpty (&Pool->FreeList[Index])) {
|
2015-03-06 03:57:11 +01:00
|
|
|
Offset = LIST_TO_SIZE (Index);
|
|
|
|
MaxOffset = Granularity;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check the bins holding larger blocks, and carve one up if needed
|
|
|
|
//
|
|
|
|
while (++Index < SIZE_TO_LIST (Granularity)) {
|
|
|
|
if (!IsListEmpty (&Pool->FreeList[Index])) {
|
|
|
|
Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
|
|
|
|
RemoveEntryList (&Free->Link);
|
|
|
|
NewPage = (VOID *)Free;
|
|
|
|
MaxOffset = LIST_TO_SIZE (Index);
|
|
|
|
goto Carve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Get another page
|
|
|
|
//
|
2017-11-20 09:08:28 +01:00
|
|
|
NewPage = CoreAllocatePoolPagesI (
|
|
|
|
PoolType,
|
|
|
|
EFI_SIZE_TO_PAGES (Granularity),
|
|
|
|
Granularity,
|
|
|
|
NeedGuard
|
|
|
|
);
|
2007-07-04 12:51:54 +02:00
|
|
|
if (NewPage == NULL) {
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-03-06 03:56:20 +01:00
|
|
|
// Serve the allocation request from the head of the allocated block
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2015-03-06 03:57:11 +01:00
|
|
|
Carve:
|
2015-03-06 03:56:20 +01:00
|
|
|
Head = (POOL_HEAD *)NewPage;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Carve up remaining space into free pool blocks
|
|
|
|
//
|
2015-03-06 03:57:11 +01:00
|
|
|
Index--;
|
|
|
|
while (Offset < MaxOffset) {
|
2007-07-04 12:51:54 +02:00
|
|
|
ASSERT (Index < MAX_POOL_LIST);
|
|
|
|
FSize = LIST_TO_SIZE (Index);
|
|
|
|
|
2015-03-06 03:57:11 +01:00
|
|
|
while (Offset + FSize <= MaxOffset) {
|
2008-07-24 04:54:45 +02:00
|
|
|
Free = (POOL_FREE *)&NewPage[Offset];
|
2007-07-04 12:51:54 +02:00
|
|
|
Free->Signature = POOL_FREE_SIGNATURE;
|
|
|
|
Free->Index = (UINT32)Index;
|
|
|
|
InsertHeadList (&Pool->FreeList[Index], &Free->Link);
|
2008-05-09 09:08:30 +02:00
|
|
|
Offset += FSize;
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
Index -= 1;
|
|
|
|
}
|
|
|
|
|
2015-03-06 03:57:11 +01:00
|
|
|
ASSERT (Offset == MaxOffset);
|
2015-03-06 03:56:20 +01:00
|
|
|
goto Done;
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Remove entry from free pool list
|
|
|
|
//
|
|
|
|
Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
|
|
|
|
RemoveEntryList (&Free->Link);
|
|
|
|
|
|
|
|
Head = (POOL_HEAD *)Free;
|
|
|
|
|
|
|
|
Done:
|
|
|
|
Buffer = NULL;
|
|
|
|
|
|
|
|
if (Head != NULL) {
|
2017-11-20 09:08:28 +01:00
|
|
|
//
|
|
|
|
// Account the allocation
|
|
|
|
//
|
|
|
|
Pool->Used += Size;
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// If we have a pool buffer, fill in the header & tail info
|
|
|
|
//
|
2018-10-24 06:47:45 +02:00
|
|
|
Head->Signature = (PageAsPool) ? POOLPAGE_HEAD_SIGNATURE : POOL_HEAD_SIGNATURE;
|
2013-10-30 05:54:53 +01:00
|
|
|
Head->Size = Size;
|
2007-07-04 12:51:54 +02:00
|
|
|
Head->Type = (EFI_MEMORY_TYPE)PoolType;
|
|
|
|
Buffer = Head->Data;
|
2017-11-20 09:08:28 +01:00
|
|
|
|
|
|
|
if (HasPoolTail) {
|
|
|
|
Tail = HEAD_TO_TAIL (Head);
|
|
|
|
Tail->Signature = POOL_TAIL_SIGNATURE;
|
|
|
|
Tail->Size = Size;
|
|
|
|
|
|
|
|
Size -= POOL_OVERHEAD;
|
|
|
|
} else {
|
|
|
|
Size -= SIZE_OF_POOL_HEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_CLEAR_MEMORY (Buffer, Size);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-07-18 11:50:09 +02:00
|
|
|
DEBUG ((
|
|
|
|
DEBUG_POOL,
|
2008-09-17 15:29:44 +02:00
|
|
|
"AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n",
|
|
|
|
PoolType,
|
2008-07-24 04:54:45 +02:00
|
|
|
Buffer,
|
2017-11-20 09:08:28 +01:00
|
|
|
(UINT64)Size,
|
2008-09-17 15:29:44 +02:00
|
|
|
(UINT64)Pool->Used
|
2008-07-18 11:50:09 +02:00
|
|
|
));
|
2007-07-04 12:51:54 +02:00
|
|
|
} else {
|
2008-09-17 15:29:44 +02:00
|
|
|
DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64)Size));
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Buffer;
|
|
|
|
}
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
2007-07-04 12:51:54 +02:00
|
|
|
Frees pool.
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param Buffer The allocated pool entry to free
|
2016-04-11 05:00:47 +02:00
|
|
|
@param PoolType Pointer to pool type
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@retval EFI_INVALID_PARAMETER Buffer is not a valid value.
|
2008-05-09 09:08:30 +02:00
|
|
|
@retval EFI_SUCCESS Pool successfully freed.
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2014-11-12 04:27:48 +01:00
|
|
|
CoreInternalFreePool (
|
2016-04-11 05:00:47 +02:00
|
|
|
IN VOID *Buffer,
|
|
|
|
OUT EFI_MEMORY_TYPE *PoolType OPTIONAL
|
2008-05-09 09:08:30 +02:00
|
|
|
)
|
2007-07-04 12:51:54 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
2008-07-18 11:50:09 +02:00
|
|
|
if (Buffer == NULL) {
|
2007-07-04 12:51:54 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2017-02-24 15:21:18 +01:00
|
|
|
CoreAcquireLock (&mPoolMemoryLock);
|
2016-04-11 05:00:47 +02:00
|
|
|
Status = CoreFreePoolI (Buffer, PoolType);
|
2017-02-24 15:21:18 +01:00
|
|
|
CoreReleaseLock (&mPoolMemoryLock);
|
2007-07-04 12:51:54 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:48 +01:00
|
|
|
/**
|
|
|
|
Frees pool.
|
|
|
|
|
|
|
|
@param Buffer The allocated pool entry to free
|
|
|
|
|
|
|
|
@retval EFI_INVALID_PARAMETER Buffer is not a valid value.
|
|
|
|
@retval EFI_SUCCESS Pool successfully freed.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
CoreFreePool (
|
|
|
|
IN VOID *Buffer
|
|
|
|
)
|
|
|
|
{
|
2016-04-11 05:00:47 +02:00
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_MEMORY_TYPE PoolType;
|
2014-12-23 09:33:06 +01:00
|
|
|
|
2016-04-11 05:00:47 +02:00
|
|
|
Status = CoreInternalFreePool (Buffer, &PoolType);
|
2014-12-23 09:33:06 +01:00
|
|
|
if (!EFI_ERROR (Status)) {
|
2016-06-18 03:03:20 +02:00
|
|
|
CoreUpdateProfile (
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),
|
|
|
|
MemoryProfileActionFreePool,
|
|
|
|
PoolType,
|
|
|
|
0,
|
|
|
|
Buffer,
|
|
|
|
NULL
|
|
|
|
);
|
2016-04-20 11:27:40 +02:00
|
|
|
InstallMemoryAttributesTableOnMemoryAllocation (PoolType);
|
2014-12-23 09:33:06 +01:00
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2014-12-23 09:33:06 +01:00
|
|
|
return Status;
|
|
|
|
}
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2017-03-03 05:44:46 +01:00
|
|
|
/**
|
|
|
|
Internal function. Frees pool pages allocated via CoreAllocatePoolPagesI().
|
|
|
|
|
|
|
|
@param PoolType The type of memory for the pool pages
|
|
|
|
@param Memory The base address to free
|
|
|
|
@param NoPages The number of pages to free
|
|
|
|
|
|
|
|
**/
|
2017-02-24 15:21:18 +01:00
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
CoreFreePoolPagesI (
|
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
|
|
|
IN EFI_PHYSICAL_ADDRESS Memory,
|
|
|
|
IN UINTN NoPages
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CoreAcquireMemoryLock ();
|
|
|
|
CoreFreePoolPages (Memory, NoPages);
|
|
|
|
CoreReleaseMemoryLock ();
|
2017-02-24 15:51:33 +01:00
|
|
|
|
2018-10-24 06:47:45 +02:00
|
|
|
GuardFreedPagesChecked (Memory, NoPages);
|
2017-02-24 15:51:33 +01:00
|
|
|
ApplyMemoryProtectionPolicy (
|
|
|
|
PoolType,
|
|
|
|
EfiConventionalMemory,
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)Memory,
|
|
|
|
EFI_PAGES_TO_SIZE (NoPages)
|
|
|
|
);
|
2017-02-24 15:21:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
/**
|
|
|
|
Internal function. Frees guarded pool pages.
|
|
|
|
|
|
|
|
@param PoolType The type of memory for the pool pages
|
|
|
|
@param Memory The base address to free
|
|
|
|
@param NoPages The number of pages to free
|
|
|
|
|
|
|
|
**/
|
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
CoreFreePoolPagesWithGuard (
|
|
|
|
IN EFI_MEMORY_TYPE PoolType,
|
|
|
|
IN EFI_PHYSICAL_ADDRESS Memory,
|
|
|
|
IN UINTN NoPages
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_PHYSICAL_ADDRESS MemoryGuarded;
|
|
|
|
UINTN NoPagesGuarded;
|
|
|
|
|
|
|
|
MemoryGuarded = Memory;
|
|
|
|
NoPagesGuarded = NoPages;
|
|
|
|
|
|
|
|
AdjustMemoryF (&Memory, &NoPages);
|
2018-01-26 12:47:14 +01:00
|
|
|
//
|
|
|
|
// It's safe to unset Guard page inside memory lock because there should
|
|
|
|
// be no memory allocation occurred in updating memory page attribute at
|
|
|
|
// this point. And unsetting Guard page before free will prevent Guard
|
|
|
|
// page just freed back to pool from being allocated right away before
|
|
|
|
// marking it usable (from non-present to present).
|
|
|
|
//
|
|
|
|
UnsetGuardForMemory (MemoryGuarded, NoPagesGuarded);
|
2017-12-09 12:15:49 +01:00
|
|
|
if (NoPages > 0) {
|
|
|
|
CoreFreePoolPagesI (PoolType, Memory, NoPages);
|
|
|
|
}
|
2017-11-20 09:08:28 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
/**
|
2007-07-04 12:51:54 +02:00
|
|
|
Internal function to free a pool entry.
|
|
|
|
Caller must have the memory lock held
|
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@param Buffer The allocated pool entry to free
|
2016-04-11 05:00:47 +02:00
|
|
|
@param PoolType Pointer to pool type
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-07-24 04:54:45 +02:00
|
|
|
@retval EFI_INVALID_PARAMETER Buffer not valid
|
2008-05-09 09:08:30 +02:00
|
|
|
@retval EFI_SUCCESS Buffer successfully freed.
|
2007-07-04 12:51:54 +02:00
|
|
|
|
2008-05-09 09:08:30 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
CoreFreePoolI (
|
2016-04-11 05:00:47 +02:00
|
|
|
IN VOID *Buffer,
|
|
|
|
OUT EFI_MEMORY_TYPE *PoolType OPTIONAL
|
2008-05-09 09:08:30 +02:00
|
|
|
)
|
2007-07-04 12:51:54 +02:00
|
|
|
{
|
|
|
|
POOL *Pool;
|
|
|
|
POOL_HEAD *Head;
|
|
|
|
POOL_TAIL *Tail;
|
|
|
|
POOL_FREE *Free;
|
|
|
|
UINTN Index;
|
|
|
|
UINTN NoPages;
|
|
|
|
UINTN Size;
|
|
|
|
CHAR8 *NewPage;
|
2008-05-09 09:08:30 +02:00
|
|
|
UINTN Offset;
|
2007-07-04 12:51:54 +02:00
|
|
|
BOOLEAN AllFree;
|
2015-03-06 03:54:05 +01:00
|
|
|
UINTN Granularity;
|
2017-11-20 09:08:28 +01:00
|
|
|
BOOLEAN IsGuarded;
|
|
|
|
BOOLEAN HasPoolTail;
|
2018-10-24 06:47:45 +02:00
|
|
|
BOOLEAN PageAsPool;
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2008-09-17 15:29:44 +02:00
|
|
|
ASSERT (Buffer != NULL);
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Get the head & tail of the pool entry
|
|
|
|
//
|
2018-10-24 06:47:45 +02:00
|
|
|
Head = BASE_CR (Buffer, POOL_HEAD, Data);
|
2008-09-17 15:29:44 +02:00
|
|
|
ASSERT (Head != NULL);
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2018-10-24 06:47:45 +02:00
|
|
|
if ((Head->Signature != POOL_HEAD_SIGNATURE) &&
|
|
|
|
(Head->Signature != POOLPAGE_HEAD_SIGNATURE))
|
|
|
|
{
|
|
|
|
ASSERT (
|
|
|
|
Head->Signature == POOL_HEAD_SIGNATURE ||
|
|
|
|
Head->Signature == POOLPAGE_HEAD_SIGNATURE
|
|
|
|
);
|
2007-07-04 12:51:54 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
IsGuarded = IsPoolTypeToGuard (Head->Type) &&
|
|
|
|
IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS)(UINTN)Head);
|
|
|
|
HasPoolTail = !(IsGuarded &&
|
|
|
|
((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));
|
2018-10-24 06:47:45 +02:00
|
|
|
PageAsPool = (Head->Signature == POOLPAGE_HEAD_SIGNATURE);
|
2017-11-20 09:08:28 +01:00
|
|
|
|
|
|
|
if (HasPoolTail) {
|
|
|
|
Tail = HEAD_TO_TAIL (Head);
|
|
|
|
ASSERT (Tail != NULL);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Debug
|
|
|
|
//
|
|
|
|
ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);
|
|
|
|
ASSERT (Head->Size == Tail->Size);
|
|
|
|
|
|
|
|
if (Tail->Signature != POOL_TAIL_SIGNATURE) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Head->Size != Tail->Size) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 09:08:28 +01:00
|
|
|
ASSERT_LOCKED (&mPoolMemoryLock);
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Determine the pool type and account for it
|
|
|
|
//
|
|
|
|
Size = Head->Size;
|
|
|
|
Pool = LookupPoolHead (Head->Type);
|
|
|
|
if (Pool == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2015-03-06 03:54:05 +01:00
|
|
|
Pool->Used -= Size;
|
|
|
|
DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64)Pool->Used));
|
|
|
|
|
2024-03-09 20:06:03 +01:00
|
|
|
if ((Head->Type == EfiReservedMemoryType) ||
|
2015-03-06 03:54:05 +01:00
|
|
|
(Head->Type == EfiACPIMemoryNVS) ||
|
|
|
|
(Head->Type == EfiRuntimeServicesCode) ||
|
|
|
|
(Head->Type == EfiRuntimeServicesData))
|
2021-12-05 23:54:02 +01:00
|
|
|
{
|
2017-03-03 16:11:32 +01:00
|
|
|
Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
|
2015-03-06 03:54:05 +01:00
|
|
|
} else {
|
2017-03-03 16:11:32 +01:00
|
|
|
Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
|
2015-03-06 03:54:05 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:00:47 +02:00
|
|
|
if (PoolType != NULL) {
|
|
|
|
*PoolType = Head->Type;
|
|
|
|
}
|
|
|
|
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
2008-07-24 04:54:45 +02:00
|
|
|
// Determine the pool list
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
Index = SIZE_TO_LIST (Size);
|
|
|
|
DEBUG_CLEAR_MEMORY (Head, Size);
|
|
|
|
|
|
|
|
//
|
|
|
|
// If it's not on the list, it must be pool pages
|
|
|
|
//
|
2018-10-24 06:47:45 +02:00
|
|
|
if ((Index >= SIZE_TO_LIST (Granularity)) || IsGuarded || PageAsPool) {
|
2007-07-04 12:51:54 +02:00
|
|
|
//
|
|
|
|
// Return the memory pages back to free memory
|
|
|
|
//
|
2017-11-20 09:08:28 +01:00
|
|
|
NoPages = EFI_SIZE_TO_PAGES (Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
|
2015-03-06 03:54:05 +01:00
|
|
|
NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
|
2017-11-20 09:08:28 +01:00
|
|
|
if (IsGuarded) {
|
MdeModulePkg: HeapGuard: Don't Assume Pool Head Allocated In First Page
Currently, HeapGuard, when in the GuardAlignedToTail mode, assumes that
the pool head has been allocated in the first page of memory that was
allocated. This is not the case for ARM64 platforms when allocating
runtime pools, as RUNTIME_PAGE_ALLOCATION_GRANULARITY is 64k, unlike
X64, which has RUNTIME_PAGE_ALLOCATION_GRANULARITY as 4k.
When a runtime pool is allocated on ARM64, the minimum number of pages
allocated is 16, to match the runtime granularity. When a small pool is
allocated and GuardAlignedToTail is true, HeapGuard instructs the pool
head to be placed as (MemoryAllocated + EFI_PAGES_TO_SIZE(Number of Pages)
- SizeRequiredForPool).
This gives this scenario:
|Head Guard|Large Free Number of Pages|PoolHead|TailGuard|
When this pool goes to be freed, HeapGuard instructs the pool code to
free from (PoolHead & ~EFI_PAGE_MASK). However, this assumes that the
PoolHead is in the first page allocated, which as shown above is not true
in this case. For the 4k granularity case (i.e. where the correct number of
pages are allocated for this pool), this logic does work.
In this failing case, HeapGuard then instructs the pool code to free 16
(or more depending) pages from the page the pool head was allocated on,
which as seen above means we overrun the pool and attempt to free memory
far past the pool. We end up running into the tail guard and getting an
access flag fault.
This causes ArmVirtQemu to fail to boot with an access flag fault when
GuardAlignedToTail is set to true (and pool guard enabled for runtime
memory). It should also cause all ARM64 platforms to fail in this
configuration, for exactly the same reason, as this is core code making
the assumption.
This patch removes HeapGuard's assumption that the pool head is allocated
on the first page and instead undoes the same logic that HeapGuard did
when allocating the pool head in the first place.
With this patch in place, ArmVirtQemu boots with GuardAlignedToTail
set to true (and when it is false, also).
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4521
Github PR: https://github.com/tianocore/edk2/pull/4731
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Dandan Bi <dandan.bi@intel.com>
Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
2023-08-09 23:34:57 +02:00
|
|
|
Head = AdjustPoolHeadF ((EFI_PHYSICAL_ADDRESS)(UINTN)Head, NoPages, Size);
|
2017-11-20 09:08:28 +01:00
|
|
|
CoreFreePoolPagesWithGuard (
|
|
|
|
Pool->MemoryType,
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)Head,
|
|
|
|
NoPages
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
CoreFreePoolPagesI (
|
|
|
|
Pool->MemoryType,
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)Head,
|
|
|
|
NoPages
|
|
|
|
);
|
|
|
|
}
|
2007-07-04 12:51:54 +02:00
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Put the pool entry onto the free pool list
|
|
|
|
//
|
|
|
|
Free = (POOL_FREE *)Head;
|
2008-09-17 15:29:44 +02:00
|
|
|
ASSERT (Free != NULL);
|
2007-07-04 12:51:54 +02:00
|
|
|
Free->Signature = POOL_FREE_SIGNATURE;
|
|
|
|
Free->Index = (UINT32)Index;
|
|
|
|
InsertHeadList (&Pool->FreeList[Index], &Free->Link);
|
|
|
|
|
|
|
|
//
|
2008-07-24 04:54:45 +02:00
|
|
|
// See if all the pool entries in the same page as Free are freed pool
|
2007-07-04 12:51:54 +02:00
|
|
|
// entries
|
|
|
|
//
|
2015-03-06 03:54:05 +01:00
|
|
|
NewPage = (CHAR8 *)((UINTN)Free & ~(Granularity - 1));
|
2007-07-04 12:51:54 +02:00
|
|
|
Free = (POOL_FREE *)&NewPage[0];
|
2008-07-18 11:50:09 +02:00
|
|
|
ASSERT (Free != NULL);
|
2007-07-04 12:51:54 +02:00
|
|
|
|
|
|
|
if (Free->Signature == POOL_FREE_SIGNATURE) {
|
|
|
|
AllFree = TRUE;
|
2008-05-09 09:08:30 +02:00
|
|
|
Offset = 0;
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2015-03-06 03:54:05 +01:00
|
|
|
while ((Offset < Granularity) && (AllFree)) {
|
2015-03-06 03:55:35 +01:00
|
|
|
Free = (POOL_FREE *)&NewPage[Offset];
|
|
|
|
ASSERT (Free != NULL);
|
|
|
|
if (Free->Signature != POOL_FREE_SIGNATURE) {
|
|
|
|
AllFree = FALSE;
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2015-03-06 03:55:35 +01:00
|
|
|
Offset += LIST_TO_SIZE (Free->Index);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AllFree) {
|
|
|
|
//
|
2008-07-24 04:54:45 +02:00
|
|
|
// All of the pool entries in the same page as Free are free pool
|
2007-07-04 12:51:54 +02:00
|
|
|
// entries
|
|
|
|
// Remove all of these pool entries from the free loop lists.
|
|
|
|
//
|
|
|
|
Free = (POOL_FREE *)&NewPage[0];
|
2008-09-17 15:29:44 +02:00
|
|
|
ASSERT (Free != NULL);
|
2008-05-09 09:08:30 +02:00
|
|
|
Offset = 0;
|
2008-07-24 04:54:45 +02:00
|
|
|
|
2015-03-06 03:54:05 +01:00
|
|
|
while (Offset < Granularity) {
|
2015-03-06 03:55:35 +01:00
|
|
|
Free = (POOL_FREE *)&NewPage[Offset];
|
|
|
|
ASSERT (Free != NULL);
|
|
|
|
RemoveEntryList (&Free->Link);
|
|
|
|
Offset += LIST_TO_SIZE (Free->Index);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Free the page
|
|
|
|
//
|
2017-02-24 15:21:18 +01:00
|
|
|
CoreFreePoolPagesI (
|
|
|
|
Pool->MemoryType,
|
|
|
|
(EFI_PHYSICAL_ADDRESS)(UINTN)NewPage,
|
|
|
|
EFI_SIZE_TO_PAGES (Granularity)
|
|
|
|
);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2016-01-29 09:24:10 +01:00
|
|
|
// If this is an OS/OEM specific memory type, then check to see if the last
|
2007-07-04 12:51:54 +02:00
|
|
|
// portion of that memory type has been freed. If it has, then free the
|
|
|
|
// list entry for that memory type
|
|
|
|
//
|
2016-01-29 09:24:10 +01:00
|
|
|
if (((UINT32)Pool->MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) && (Pool->Used == 0)) {
|
2007-07-04 12:51:54 +02:00
|
|
|
RemoveEntryList (&Pool->Link);
|
2016-04-11 05:00:47 +02:00
|
|
|
CoreFreePoolI (Pool, NULL);
|
2007-07-04 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|