From 8a7bd316ef7a37322a8c23a26dfcd1c68f78b267 Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Tue, 27 Feb 2024 14:44:49 +0300 Subject: [PATCH] MdeModulePkg: Moved IsHeapGuardEnabled() and IsMemoryTypeToGuard() to MemoryPoolLib. --- MdeModulePkg/Core/Dxe/Mem/HeapGuard.c | 67 ------------------- MdeModulePkg/Core/Dxe/Mem/HeapGuard.h | 21 ------ MdeModulePkg/Include/Library/MemoryPoolLib.h | 39 +++++++++++ .../Library/MemoryPoolLib/InternalPool.h | 14 ---- .../Library/MemoryPoolLib/MemoryPoolLib.inf | 2 + MdeModulePkg/Library/MemoryPoolLib/Pool.c | 67 +++++++++++++++++++ 6 files changed, 108 insertions(+), 102 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Mem/HeapGuard.c b/MdeModulePkg/Core/Dxe/Mem/HeapGuard.c index 6457741683..aea858d127 100644 --- a/MdeModulePkg/Core/Dxe/Mem/HeapGuard.c +++ b/MdeModulePkg/Core/Dxe/Mem/HeapGuard.c @@ -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. diff --git a/MdeModulePkg/Core/Dxe/Mem/HeapGuard.h b/MdeModulePkg/Core/Dxe/Mem/HeapGuard.h index 578e857465..a4a5088b65 100644 --- a/MdeModulePkg/Core/Dxe/Mem/HeapGuard.h +++ b/MdeModulePkg/Core/Dxe/Mem/HeapGuard.h @@ -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. **/ diff --git a/MdeModulePkg/Include/Library/MemoryPoolLib.h b/MdeModulePkg/Include/Library/MemoryPoolLib.h index b80dc71a70..a285040287 100644 --- a/MdeModulePkg/Include/Library/MemoryPoolLib.h +++ b/MdeModulePkg/Include/Library/MemoryPoolLib.h @@ -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 diff --git a/MdeModulePkg/Library/MemoryPoolLib/InternalPool.h b/MdeModulePkg/Library/MemoryPoolLib/InternalPool.h index 019b4541c3..9a62050831 100644 --- a/MdeModulePkg/Library/MemoryPoolLib/InternalPool.h +++ b/MdeModulePkg/Library/MemoryPoolLib/InternalPool.h @@ -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. diff --git a/MdeModulePkg/Library/MemoryPoolLib/MemoryPoolLib.inf b/MdeModulePkg/Library/MemoryPoolLib/MemoryPoolLib.inf index a0bda89d15..91bbda57a3 100644 --- a/MdeModulePkg/Library/MemoryPoolLib/MemoryPoolLib.inf +++ b/MdeModulePkg/Library/MemoryPoolLib/MemoryPoolLib.inf @@ -36,4 +36,6 @@ UefiLib [Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPageType ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPoolType ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ## CONSUMES diff --git a/MdeModulePkg/Library/MemoryPoolLib/Pool.c b/MdeModulePkg/Library/MemoryPoolLib/Pool.c index 0e9197fd6c..83d53286a3 100644 --- a/MdeModulePkg/Library/MemoryPoolLib/Pool.c +++ b/MdeModulePkg/Library/MemoryPoolLib/Pool.c @@ -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); +}