MdeModulePkg: Moved IsHeapGuardEnabled() and IsMemoryTypeToGuard() to MemoryPoolLib.

This commit is contained in:
Mikhail Krichanov 2024-02-27 14:44:49 +03:00
parent abdfba8835
commit 8a7bd316ef
6 changed files with 108 additions and 102 deletions

View File

@ -572,58 +572,6 @@ UnsetGuardPage (
mOnGuarding = FALSE;
}
/**
Check to see if the memory at the given address should be guarded or not.
@param[in] MemoryType Memory type to check.
@param[in] AllocateType Allocation type to check.
@param[in] PageOrPool Indicate a page allocation or pool allocation.
@return TRUE The given type of memory should be guarded.
@return FALSE The given type of memory should not be guarded.
**/
BOOLEAN
IsMemoryTypeToGuard (
IN EFI_MEMORY_TYPE MemoryType,
IN EFI_ALLOCATE_TYPE AllocateType,
IN UINT8 PageOrPool
)
{
UINT64 TestBit;
UINT64 ConfigBit;
if (AllocateType == AllocateAddress) {
return FALSE;
}
if ((PcdGet8 (PcdHeapGuardPropertyMask) & PageOrPool) == 0) {
return FALSE;
}
if (PageOrPool == GUARD_HEAP_TYPE_POOL) {
ConfigBit = PcdGet64 (PcdHeapGuardPoolType);
} else if (PageOrPool == GUARD_HEAP_TYPE_PAGE) {
ConfigBit = PcdGet64 (PcdHeapGuardPageType);
} else {
ConfigBit = (UINT64)-1;
}
if ((UINT32)MemoryType >= MEMORY_TYPE_OS_RESERVED_MIN) {
TestBit = BIT63;
} else if ((UINT32)MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
TestBit = BIT62;
} else if (MemoryType < EfiMaxMemoryType) {
TestBit = LShiftU64 (1, MemoryType);
} else if (MemoryType == EfiMaxMemoryType) {
TestBit = (UINT64)-1;
} else {
TestBit = 0;
}
return ((ConfigBit & TestBit) != 0);
}
/**
Check to see if the pool at the given address should be guarded or not.
@ -663,21 +611,6 @@ IsPageTypeToGuard (
return IsMemoryTypeToGuard (MemoryType, AllocateType, GUARD_HEAP_TYPE_PAGE);
}
/**
Check to see if the heap guard is enabled for page and/or pool allocation.
@param[in] GuardType Specify the sub-type(s) of Heap Guard.
@return TRUE/FALSE.
**/
BOOLEAN
IsHeapGuardEnabled (
UINT8 GuardType
)
{
return IsMemoryTypeToGuard (EfiMaxMemoryType, AllocateAnyPages, GuardType);
}
/**
Set head Guard and tail Guard for the given memory range.

View File

@ -149,15 +149,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
(1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1 \
}
//
// Memory type to guard (matching the related PCD definition)
//
#define GUARD_HEAP_TYPE_PAGE BIT0
#define GUARD_HEAP_TYPE_POOL BIT1
#define GUARD_HEAP_TYPE_FREED BIT4
#define GUARD_HEAP_TYPE_ALL \
(GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED)
//
// Debug message level
//
@ -391,18 +382,6 @@ AdjustPoolHeadF (
IN UINTN Size
);
/**
Check to see if the heap guard is enabled for page and/or pool allocation.
@param[in] GuardType Specify the sub-type(s) of Heap Guard.
@return TRUE/FALSE.
**/
BOOLEAN
IsHeapGuardEnabled (
UINT8 GuardType
);
/**
Notify function used to set all Guard pages after CPU Arch Protocol installed.
**/

View File

@ -25,6 +25,15 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define MEMORY_TYPE_OEM_RESERVED_MIN 0x70000000
#define MEMORY_TYPE_OEM_RESERVED_MAX 0x7FFFFFFF
//
// Memory type to guard (matching the related PCD definition)
//
#define GUARD_HEAP_TYPE_PAGE BIT0
#define GUARD_HEAP_TYPE_POOL BIT1
#define GUARD_HEAP_TYPE_FREED BIT4
#define GUARD_HEAP_TYPE_ALL \
(GUARD_HEAP_TYPE_PAGE|GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_FREED)
/**
Called to initialize the pool.
@ -143,4 +152,34 @@ CoreFreePoolI (
OUT EFI_MEMORY_TYPE *PoolType OPTIONAL
);
/**
Check to see if the heap guard is enabled for page and/or pool allocation.
@param[in] GuardType Specify the sub-type(s) of Heap Guard.
@return TRUE/FALSE.
**/
BOOLEAN
IsHeapGuardEnabled (
UINT8 GuardType
);
/**
Check to see if the memory at the given address should be guarded or not.
@param[in] MemoryType Memory type to check.
@param[in] AllocateType Allocation type to check.
@param[in] PageOrPool Indicate a page allocation or pool allocation.
@return TRUE The given type of memory should be guarded.
@return FALSE The given type of memory should not be guarded.
**/
BOOLEAN
IsMemoryTypeToGuard (
IN EFI_MEMORY_TYPE MemoryType,
IN EFI_ALLOCATE_TYPE AllocateType,
IN UINT8 PageOrPool
);
#endif

View File

@ -11,8 +11,6 @@
extern BOOLEAN mOnGuarding;
#define GUARD_HEAP_TYPE_FREED BIT4
/**
Check to see if the pool at the given address should be guarded or not.
@ -102,18 +100,6 @@ InstallMemoryAttributesTableOnMemoryAllocation (
IN EFI_MEMORY_TYPE MemoryType
);
/**
Check to see if the heap guard is enabled for page and/or pool allocation.
@param[in] GuardType Specify the sub-type(s) of Heap Guard.
@return TRUE/FALSE.
**/
BOOLEAN
IsHeapGuardEnabled (
UINT8 GuardType
);
/**
Adjust the pool head position to make sure the Guard page is adjavent to
pool tail or pool head.

View File

@ -36,4 +36,6 @@
UefiLib
[Pcd]
gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPageType ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPoolType ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ## CONSUMES

View File

@ -809,3 +809,70 @@ CoreFreePoolI (
return EFI_SUCCESS;
}
/**
Check to see if the heap guard is enabled for page and/or pool allocation.
@param[in] GuardType Specify the sub-type(s) of Heap Guard.
@return TRUE/FALSE.
**/
BOOLEAN
IsHeapGuardEnabled (
UINT8 GuardType
)
{
return IsMemoryTypeToGuard (EfiMaxMemoryType, AllocateAnyPages, GuardType);
}
/**
Check to see if the memory at the given address should be guarded or not.
@param[in] MemoryType Memory type to check.
@param[in] AllocateType Allocation type to check.
@param[in] PageOrPool Indicate a page allocation or pool allocation.
@return TRUE The given type of memory should be guarded.
@return FALSE The given type of memory should not be guarded.
**/
BOOLEAN
IsMemoryTypeToGuard (
IN EFI_MEMORY_TYPE MemoryType,
IN EFI_ALLOCATE_TYPE AllocateType,
IN UINT8 PageOrPool
)
{
UINT64 TestBit;
UINT64 ConfigBit;
if (AllocateType == AllocateAddress) {
return FALSE;
}
if ((PcdGet8 (PcdHeapGuardPropertyMask) & PageOrPool) == 0) {
return FALSE;
}
if (PageOrPool == GUARD_HEAP_TYPE_POOL) {
ConfigBit = PcdGet64 (PcdHeapGuardPoolType);
} else if (PageOrPool == GUARD_HEAP_TYPE_PAGE) {
ConfigBit = PcdGet64 (PcdHeapGuardPageType);
} else {
ConfigBit = (UINT64)-1;
}
if ((UINT32)MemoryType >= MEMORY_TYPE_OS_RESERVED_MIN) {
TestBit = BIT63;
} else if ((UINT32)MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
TestBit = BIT62;
} else if (MemoryType < EfiMaxMemoryType) {
TestBit = LShiftU64 (1, MemoryType);
} else if (MemoryType == EfiMaxMemoryType) {
TestBit = (UINT64)-1;
} else {
TestBit = 0;
}
return ((ConfigBit & TestBit) != 0);
}