summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Core/PiSmmCore/HeapGuard.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Core/PiSmmCore/HeapGuard.c')
-rw-r--r--MdeModulePkg/Core/PiSmmCore/HeapGuard.c602
1 files changed, 307 insertions, 295 deletions
diff --git a/MdeModulePkg/Core/PiSmmCore/HeapGuard.c b/MdeModulePkg/Core/PiSmmCore/HeapGuard.c
index b0d0f72044..8f3bab6fee 100644
--- a/MdeModulePkg/Core/PiSmmCore/HeapGuard.c
+++ b/MdeModulePkg/Core/PiSmmCore/HeapGuard.c
@@ -12,34 +12,34 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
// Global to avoid infinite reentrance of memory allocation when updating
// page table attributes, which may need allocating pages for new PDE/PTE.
//
-GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE;
+GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE;
//
// Pointer to table tracking the Guarded memory with bitmap, in which '1'
// is used to indicate memory guarded. '0' might be free memory or Guard
// page itself, depending on status of memory adjacent to it.
//
-GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap = 0;
+GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap = 0;
//
// Current depth level of map table pointed by mGuardedMemoryMap.
// mMapLevel must be initialized at least by 1. It will be automatically
// updated according to the address of memory just tracked.
//
-GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel = 1;
+GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel = 1;
//
// Shift and mask for each level of map table
//
-GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelShift[GUARDED_HEAP_MAP_TABLE_DEPTH]
- = GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS;
-GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelMask[GUARDED_HEAP_MAP_TABLE_DEPTH]
- = GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS;
+GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelShift[GUARDED_HEAP_MAP_TABLE_DEPTH]
+ = GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS;
+GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelMask[GUARDED_HEAP_MAP_TABLE_DEPTH]
+ = GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS;
//
// SMM memory attribute protocol
//
-EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *mSmmMemoryAttribute = NULL;
+EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *mSmmMemoryAttribute = NULL;
/**
Set corresponding bits in bitmap table to 1 according to the address.
@@ -53,29 +53,29 @@ EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *mSmmMemoryAttribute = NULL;
STATIC
VOID
SetBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN BitNumber,
- IN UINT64 *BitMap
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN BitNumber,
+ IN UINT64 *BitMap
)
{
- UINTN Lsbs;
- UINTN Qwords;
- UINTN Msbs;
- UINTN StartBit;
- UINTN EndBit;
+ UINTN Lsbs;
+ UINTN Qwords;
+ UINTN Msbs;
+ UINTN StartBit;
+ UINTN EndBit;
- StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
- EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
+ StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
+ EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
if ((StartBit + BitNumber) >= GUARDED_HEAP_MAP_ENTRY_BITS) {
- Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) %
- GUARDED_HEAP_MAP_ENTRY_BITS;
- Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
- Qwords = (BitNumber - Msbs) / GUARDED_HEAP_MAP_ENTRY_BITS;
+ Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) %
+ GUARDED_HEAP_MAP_ENTRY_BITS;
+ Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
+ Qwords = (BitNumber - Msbs) / GUARDED_HEAP_MAP_ENTRY_BITS;
} else {
- Msbs = BitNumber;
- Lsbs = 0;
- Qwords = 0;
+ Msbs = BitNumber;
+ Lsbs = 0;
+ Qwords = 0;
}
if (Msbs > 0) {
@@ -84,8 +84,11 @@ SetBits (
}
if (Qwords > 0) {
- SetMem64 ((VOID *)BitMap, Qwords * GUARDED_HEAP_MAP_ENTRY_BYTES,
- (UINT64)-1);
+ SetMem64 (
+ (VOID *)BitMap,
+ Qwords * GUARDED_HEAP_MAP_ENTRY_BYTES,
+ (UINT64)-1
+ );
BitMap += Qwords;
}
@@ -106,29 +109,29 @@ SetBits (
STATIC
VOID
ClearBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN BitNumber,
- IN UINT64 *BitMap
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN BitNumber,
+ IN UINT64 *BitMap
)
{
- UINTN Lsbs;
- UINTN Qwords;
- UINTN Msbs;
- UINTN StartBit;
- UINTN EndBit;
+ UINTN Lsbs;
+ UINTN Qwords;
+ UINTN Msbs;
+ UINTN StartBit;
+ UINTN EndBit;
- StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
- EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
+ StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
+ EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
if ((StartBit + BitNumber) >= GUARDED_HEAP_MAP_ENTRY_BITS) {
- Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) %
- GUARDED_HEAP_MAP_ENTRY_BITS;
- Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
- Qwords = (BitNumber - Msbs) / GUARDED_HEAP_MAP_ENTRY_BITS;
+ Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) %
+ GUARDED_HEAP_MAP_ENTRY_BITS;
+ Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
+ Qwords = (BitNumber - Msbs) / GUARDED_HEAP_MAP_ENTRY_BITS;
} else {
- Msbs = BitNumber;
- Lsbs = 0;
- Qwords = 0;
+ Msbs = BitNumber;
+ Lsbs = 0;
+ Qwords = 0;
}
if (Msbs > 0) {
@@ -161,21 +164,21 @@ ClearBits (
STATIC
UINT64
GetBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN BitNumber,
- IN UINT64 *BitMap
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN BitNumber,
+ IN UINT64 *BitMap
)
{
- UINTN StartBit;
- UINTN EndBit;
- UINTN Lsbs;
- UINTN Msbs;
- UINT64 Result;
+ UINTN StartBit;
+ UINTN EndBit;
+ UINTN Lsbs;
+ UINTN Msbs;
+ UINT64 Result;
ASSERT (BitNumber <= GUARDED_HEAP_MAP_ENTRY_BITS);
- StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
- EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
+ StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
+ EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
if ((StartBit + BitNumber) > GUARDED_HEAP_MAP_ENTRY_BITS) {
Msbs = GUARDED_HEAP_MAP_ENTRY_BITS - StartBit;
@@ -185,13 +188,13 @@ GetBits (
Lsbs = 0;
}
- if (StartBit == 0 && BitNumber == GUARDED_HEAP_MAP_ENTRY_BITS) {
+ if ((StartBit == 0) && (BitNumber == GUARDED_HEAP_MAP_ENTRY_BITS)) {
Result = *BitMap;
} else {
- Result = RShiftU64((*BitMap), StartBit) & (LShiftU64(1, Msbs) - 1);
+ Result = RShiftU64 ((*BitMap), StartBit) & (LShiftU64 (1, Msbs) - 1);
if (Lsbs > 0) {
- BitMap += 1;
- Result |= LShiftU64 ((*BitMap) & (LShiftU64 (1, Lsbs) - 1), Msbs);
+ BitMap += 1;
+ Result |= LShiftU64 ((*BitMap) & (LShiftU64 (1, Lsbs) - 1), Msbs);
}
}
@@ -210,11 +213,16 @@ PageAlloc (
IN UINTN Pages
)
{
- EFI_STATUS Status;
- EFI_PHYSICAL_ADDRESS Memory;
-
- Status = SmmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData,
- Pages, &Memory, FALSE);
+ EFI_STATUS Status;
+ EFI_PHYSICAL_ADDRESS Memory;
+
+ Status = SmmInternalAllocatePages (
+ AllocateAnyPages,
+ EfiRuntimeServicesData,
+ Pages,
+ &Memory,
+ FALSE
+ );
if (EFI_ERROR (Status)) {
Memory = 0;
}
@@ -234,17 +242,17 @@ PageAlloc (
**/
UINTN
FindGuardedMemoryMap (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN BOOLEAN AllocMapUnit,
- OUT UINT64 **BitMap
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN BOOLEAN AllocMapUnit,
+ OUT UINT64 **BitMap
)
{
- UINTN Level;
- UINT64 *GuardMap;
- UINT64 MapMemory;
- UINTN Index;
- UINTN Size;
- UINTN BitsToUnitEnd;
+ UINTN Level;
+ UINT64 *GuardMap;
+ UINT64 MapMemory;
+ UINTN Index;
+ UINTN Size;
+ UINTN BitsToUnitEnd;
//
// Adjust current map table depth according to the address to access
@@ -254,8 +262,8 @@ FindGuardedMemoryMap (
RShiftU64 (
Address,
mLevelShift[GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel - 1]
- ) != 0) {
-
+ ) != 0)
+ {
if (mGuardedMemoryMap != 0) {
Size = (mLevelMask[GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel - 1] + 1)
* GUARDED_HEAP_MAP_ENTRY_BYTES;
@@ -265,25 +273,24 @@ FindGuardedMemoryMap (
SetMem ((VOID *)(UINTN)MapMemory, Size, 0);
*(UINT64 *)(UINTN)MapMemory = mGuardedMemoryMap;
- mGuardedMemoryMap = MapMemory;
+ mGuardedMemoryMap = MapMemory;
}
mMapLevel++;
-
}
GuardMap = &mGuardedMemoryMap;
for (Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
Level < GUARDED_HEAP_MAP_TABLE_DEPTH;
- ++Level) {
-
+ ++Level)
+ {
if (*GuardMap == 0) {
if (!AllocMapUnit) {
GuardMap = NULL;
break;
}
- Size = (mLevelMask[Level] + 1) * GUARDED_HEAP_MAP_ENTRY_BYTES;
+ Size = (mLevelMask[Level] + 1) * GUARDED_HEAP_MAP_ENTRY_BYTES;
MapMemory = (UINT64)(UINTN)PageAlloc (EFI_SIZE_TO_PAGES (Size));
ASSERT (MapMemory != 0);
@@ -291,10 +298,9 @@ FindGuardedMemoryMap (
*GuardMap = MapMemory;
}
- Index = (UINTN)RShiftU64 (Address, mLevelShift[Level]);
- Index &= mLevelMask[Level];
- GuardMap = (UINT64 *)(UINTN)((*GuardMap) + Index * sizeof (UINT64));
-
+ Index = (UINTN)RShiftU64 (Address, mLevelShift[Level]);
+ Index &= mLevelMask[Level];
+ GuardMap = (UINT64 *)(UINTN)((*GuardMap) + Index * sizeof (UINT64));
}
BitsToUnitEnd = GUARDED_HEAP_MAP_BITS - GUARDED_HEAP_MAP_BIT_INDEX (Address);
@@ -314,13 +320,13 @@ FindGuardedMemoryMap (
VOID
EFIAPI
SetGuardedMemoryBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN NumberOfPages
)
{
- UINT64 *BitMap;
- UINTN Bits;
- UINTN BitsToUnitEnd;
+ UINT64 *BitMap;
+ UINTN Bits;
+ UINTN BitsToUnitEnd;
while (NumberOfPages > 0) {
BitsToUnitEnd = FindGuardedMemoryMap (Address, TRUE, &BitMap);
@@ -330,7 +336,7 @@ SetGuardedMemoryBits (
// Cross map unit
Bits = BitsToUnitEnd;
} else {
- Bits = NumberOfPages;
+ Bits = NumberOfPages;
}
SetBits (Address, Bits, BitMap);
@@ -351,13 +357,13 @@ SetGuardedMemoryBits (
VOID
EFIAPI
ClearGuardedMemoryBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN NumberOfPages
)
{
- UINT64 *BitMap;
- UINTN Bits;
- UINTN BitsToUnitEnd;
+ UINT64 *BitMap;
+ UINTN Bits;
+ UINTN BitsToUnitEnd;
while (NumberOfPages > 0) {
BitsToUnitEnd = FindGuardedMemoryMap (Address, TRUE, &BitMap);
@@ -367,7 +373,7 @@ ClearGuardedMemoryBits (
// Cross map unit
Bits = BitsToUnitEnd;
} else {
- Bits = NumberOfPages;
+ Bits = NumberOfPages;
}
ClearBits (Address, Bits, BitMap);
@@ -387,15 +393,15 @@ ClearGuardedMemoryBits (
**/
UINTN
GetGuardedMemoryBits (
- IN EFI_PHYSICAL_ADDRESS Address,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS Address,
+ IN UINTN NumberOfPages
)
{
- UINT64 *BitMap;
- UINTN Bits;
- UINTN Result;
- UINTN Shift;
- UINTN BitsToUnitEnd;
+ UINT64 *BitMap;
+ UINTN Bits;
+ UINTN Result;
+ UINTN Shift;
+ UINTN BitsToUnitEnd;
ASSERT (NumberOfPages <= GUARDED_HEAP_MAP_ENTRY_BITS);
@@ -406,9 +412,9 @@ GetGuardedMemoryBits (
if (NumberOfPages > BitsToUnitEnd) {
// Cross map unit
- Bits = BitsToUnitEnd;
+ Bits = BitsToUnitEnd;
} else {
- Bits = NumberOfPages;
+ Bits = NumberOfPages;
}
if (BitMap != NULL) {
@@ -433,15 +439,18 @@ GetGuardedMemoryBits (
UINTN
EFIAPI
GetGuardMapBit (
- IN EFI_PHYSICAL_ADDRESS Address
+ IN EFI_PHYSICAL_ADDRESS Address
)
{
- UINT64 *GuardMap;
+ UINT64 *GuardMap;
FindGuardedMemoryMap (Address, FALSE, &GuardMap);
if (GuardMap != NULL) {
- if (RShiftU64 (*GuardMap,
- GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address)) & 1) {
+ if (RShiftU64 (
+ *GuardMap,
+ GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address)
+ ) & 1)
+ {
return 1;
}
}
@@ -449,7 +458,6 @@ GetGuardMapBit (
return 0;
}
-
/**
Check to see if the page at the given address is a Guard page or not.
@@ -461,10 +469,10 @@ GetGuardMapBit (
BOOLEAN
EFIAPI
IsGuardPage (
- IN EFI_PHYSICAL_ADDRESS Address
-)
+ IN EFI_PHYSICAL_ADDRESS Address
+ )
{
- UINTN BitMap;
+ UINTN BitMap;
//
// There must be at least one guarded page before and/or after given
@@ -475,8 +483,6 @@ IsGuardPage (
return ((BitMap == BIT0) || (BitMap == BIT2) || (BitMap == (BIT2 | BIT0)));
}
-
-
/**
Check to see if the page at the given address is guarded or not.
@@ -488,7 +494,7 @@ IsGuardPage (
BOOLEAN
EFIAPI
IsMemoryGuarded (
- IN EFI_PHYSICAL_ADDRESS Address
+ IN EFI_PHYSICAL_ADDRESS Address
)
{
return (GetGuardMapBit (Address) == 1);
@@ -506,19 +512,19 @@ IsMemoryGuarded (
VOID
EFIAPI
SetGuardPage (
- IN EFI_PHYSICAL_ADDRESS BaseAddress
+ IN EFI_PHYSICAL_ADDRESS BaseAddress
)
{
- EFI_STATUS Status;
+ EFI_STATUS Status;
if (mSmmMemoryAttribute != NULL) {
mOnGuarding = TRUE;
- Status = mSmmMemoryAttribute->SetMemoryAttributes (
- mSmmMemoryAttribute,
- BaseAddress,
- EFI_PAGE_SIZE,
- EFI_MEMORY_RP
- );
+ Status = mSmmMemoryAttribute->SetMemoryAttributes (
+ mSmmMemoryAttribute,
+ BaseAddress,
+ EFI_PAGE_SIZE,
+ EFI_MEMORY_RP
+ );
ASSERT_EFI_ERROR (Status);
mOnGuarding = FALSE;
}
@@ -536,19 +542,19 @@ SetGuardPage (
VOID
EFIAPI
UnsetGuardPage (
- IN EFI_PHYSICAL_ADDRESS BaseAddress
+ IN EFI_PHYSICAL_ADDRESS BaseAddress
)
{
- EFI_STATUS Status;
+ EFI_STATUS Status;
if (mSmmMemoryAttribute != NULL) {
mOnGuarding = TRUE;
- Status = mSmmMemoryAttribute->ClearMemoryAttributes (
- mSmmMemoryAttribute,
- BaseAddress,
- EFI_PAGE_SIZE,
- EFI_MEMORY_RP
- );
+ Status = mSmmMemoryAttribute->ClearMemoryAttributes (
+ mSmmMemoryAttribute,
+ BaseAddress,
+ EFI_PAGE_SIZE,
+ EFI_MEMORY_RP
+ );
ASSERT_EFI_ERROR (Status);
mOnGuarding = FALSE;
}
@@ -567,17 +573,18 @@ UnsetGuardPage (
**/
BOOLEAN
IsMemoryTypeToGuard (
- IN EFI_MEMORY_TYPE MemoryType,
- IN EFI_ALLOCATE_TYPE AllocateType,
- IN UINT8 PageOrPool
+ IN EFI_MEMORY_TYPE MemoryType,
+ IN EFI_ALLOCATE_TYPE AllocateType,
+ IN UINT8 PageOrPool
)
{
- UINT64 TestBit;
- UINT64 ConfigBit;
+ UINT64 TestBit;
+ UINT64 ConfigBit;
- if ((PcdGet8 (PcdHeapGuardPropertyMask) & PageOrPool) == 0
- || mOnGuarding
- || AllocateType == AllocateAddress) {
+ if ( ((PcdGet8 (PcdHeapGuardPropertyMask) & PageOrPool) == 0)
+ || mOnGuarding
+ || (AllocateType == AllocateAddress))
+ {
return FALSE;
}
@@ -590,8 +597,9 @@ IsMemoryTypeToGuard (
ConfigBit |= PcdGet64 (PcdHeapGuardPageType);
}
- if (MemoryType == EfiRuntimeServicesData ||
- MemoryType == EfiRuntimeServicesCode) {
+ if ((MemoryType == EfiRuntimeServicesData) ||
+ (MemoryType == EfiRuntimeServicesCode))
+ {
TestBit = LShiftU64 (1, MemoryType);
} else if (MemoryType == EfiMaxMemoryType) {
TestBit = (UINT64)-1;
@@ -613,11 +621,14 @@ IsMemoryTypeToGuard (
**/
BOOLEAN
IsPoolTypeToGuard (
- IN EFI_MEMORY_TYPE MemoryType
+ IN EFI_MEMORY_TYPE MemoryType
)
{
- return IsMemoryTypeToGuard (MemoryType, AllocateAnyPages,
- GUARD_HEAP_TYPE_POOL);
+ return IsMemoryTypeToGuard (
+ MemoryType,
+ AllocateAnyPages,
+ GUARD_HEAP_TYPE_POOL
+ );
}
/**
@@ -631,8 +642,8 @@ IsPoolTypeToGuard (
**/
BOOLEAN
IsPageTypeToGuard (
- IN EFI_MEMORY_TYPE MemoryType,
- IN EFI_ALLOCATE_TYPE AllocateType
+ IN EFI_MEMORY_TYPE MemoryType,
+ IN EFI_ALLOCATE_TYPE AllocateType
)
{
return IsMemoryTypeToGuard (MemoryType, AllocateType, GUARD_HEAP_TYPE_PAGE);
@@ -648,8 +659,11 @@ IsHeapGuardEnabled (
VOID
)
{
- return IsMemoryTypeToGuard (EfiMaxMemoryType, AllocateAnyPages,
- GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_PAGE);
+ return IsMemoryTypeToGuard (
+ EfiMaxMemoryType,
+ AllocateAnyPages,
+ GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_PAGE
+ );
}
/**
@@ -662,11 +676,11 @@ IsHeapGuardEnabled (
**/
VOID
SetGuardForMemory (
- IN EFI_PHYSICAL_ADDRESS Memory,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS Memory,
+ IN UINTN NumberOfPages
)
{
- EFI_PHYSICAL_ADDRESS GuardPage;
+ EFI_PHYSICAL_ADDRESS GuardPage;
//
// Set tail Guard
@@ -698,8 +712,8 @@ SetGuardForMemory (
**/
VOID
UnsetGuardForMemory (
- IN EFI_PHYSICAL_ADDRESS Memory,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS Memory,
+ IN UINTN NumberOfPages
)
{
EFI_PHYSICAL_ADDRESS GuardPage;
@@ -721,7 +735,7 @@ UnsetGuardForMemory (
// -------------------
// Start -> -1 -2
//
- GuardPage = Memory - EFI_PAGES_TO_SIZE (1);
+ GuardPage = Memory - EFI_PAGES_TO_SIZE (1);
GuardBitmap = GetGuardedMemoryBits (Memory - EFI_PAGES_TO_SIZE (2), 2);
if ((GuardBitmap & BIT1) == 0) {
//
@@ -754,7 +768,7 @@ UnsetGuardForMemory (
// --------------------
// +1 +0 <- End
//
- GuardPage = Memory + EFI_PAGES_TO_SIZE (NumberOfPages);
+ GuardPage = Memory + EFI_PAGES_TO_SIZE (NumberOfPages);
GuardBitmap = GetGuardedMemoryBits (GuardPage, 2);
if ((GuardBitmap & BIT0) == 0) {
//
@@ -778,11 +792,9 @@ UnsetGuardForMemory (
//
// No matter what, we just clear the mark of the Guarded memory.
//
- ClearGuardedMemoryBits(Memory, NumberOfPages);
+ ClearGuardedMemoryBits (Memory, NumberOfPages);
}
-
-
/**
Adjust the start address and number of pages to free according to Guard.
@@ -797,8 +809,8 @@ UnsetGuardForMemory (
**/
VOID
AdjustMemoryF (
- IN OUT EFI_PHYSICAL_ADDRESS *Memory,
- IN OUT UINTN *NumberOfPages
+ IN OUT EFI_PHYSICAL_ADDRESS *Memory,
+ IN OUT UINTN *NumberOfPages
)
{
EFI_PHYSICAL_ADDRESS Start;
@@ -807,11 +819,11 @@ AdjustMemoryF (
UINT64 GuardBitmap;
UINT64 Attributes;
- if (Memory == NULL || NumberOfPages == NULL || *NumberOfPages == 0) {
+ if ((Memory == NULL) || (NumberOfPages == NULL) || (*NumberOfPages == 0)) {
return;
}
- Start = *Memory;
+ Start = *Memory;
PagesToFree = *NumberOfPages;
//
@@ -848,7 +860,7 @@ AdjustMemoryF (
// Start -> -1 -2
//
MemoryToTest = Start - EFI_PAGES_TO_SIZE (2);
- GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
+ GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
if ((GuardBitmap & BIT1) == 0) {
//
// Head Guard exists.
@@ -883,7 +895,7 @@ AdjustMemoryF (
// +1 +0 <- End
//
MemoryToTest = Start + EFI_PAGES_TO_SIZE (PagesToFree);
- GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
+ GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
if ((GuardBitmap & BIT0) == 0) {
//
// Tail Guard exists.
@@ -903,11 +915,10 @@ AdjustMemoryF (
PagesToFree -= 1;
}
- *Memory = Start;
- *NumberOfPages = PagesToFree;
+ *Memory = Start;
+ *NumberOfPages = PagesToFree;
}
-
/**
Adjust the pool head position to make sure the Guard page is adjavent to
pool tail or pool head.
@@ -921,12 +932,12 @@ AdjustMemoryF (
**/
VOID *
AdjustPoolHeadA (
- IN EFI_PHYSICAL_ADDRESS Memory,
- IN UINTN NoPages,
- IN UINTN Size
+ IN EFI_PHYSICAL_ADDRESS Memory,
+ IN UINTN NoPages,
+ IN UINTN Size
)
{
- if (Memory == 0 || (PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0) {
+ if ((Memory == 0) || ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0)) {
//
// Pool head is put near the head Guard
//
@@ -949,10 +960,10 @@ AdjustPoolHeadA (
**/
VOID *
AdjustPoolHeadF (
- IN EFI_PHYSICAL_ADDRESS Memory
+ IN EFI_PHYSICAL_ADDRESS Memory
)
{
- if (Memory == 0 || (PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0) {
+ if ((Memory == 0) || ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0)) {
//
// Pool head is put near the head Guard
//
@@ -977,10 +988,10 @@ AdjustPoolHeadF (
**/
UINTN
InternalAllocMaxAddressWithGuard (
- IN OUT LIST_ENTRY *FreePageList,
- IN UINTN NumberOfPages,
- IN UINTN MaxAddress,
- IN EFI_MEMORY_TYPE MemoryType
+ IN OUT LIST_ENTRY *FreePageList,
+ IN UINTN NumberOfPages,
+ IN UINTN MaxAddress,
+ IN EFI_MEMORY_TYPE MemoryType
)
{
@@ -992,16 +1003,17 @@ InternalAllocMaxAddressWithGuard (
UINTN Address;
for (Node = FreePageList->BackLink; Node != FreePageList;
- Node = Node->BackLink) {
+ Node = Node->BackLink)
+ {
Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
- if (Pages->NumberOfPages >= NumberOfPages &&
- (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
-
+ if ((Pages->NumberOfPages >= NumberOfPages) &&
+ ((UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress))
+ {
//
// We may need 1 or 2 more pages for Guard. Check it out.
//
PagesToAlloc = NumberOfPages;
- TailGuard = (UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages);
+ TailGuard = (UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages);
if (!IsGuardPage (TailGuard)) {
//
// Add one if no Guard at the end of current free memory block.
@@ -1027,12 +1039,13 @@ InternalAllocMaxAddressWithGuard (
}
Address = InternalAllocPagesOnOneNode (Pages, PagesToAlloc, MaxAddress);
- ConvertSmmMemoryMapEntry(MemoryType, Address, PagesToAlloc, FALSE);
- CoreFreeMemoryMapStack();
+ ConvertSmmMemoryMapEntry (MemoryType, Address, PagesToAlloc, FALSE);
+ CoreFreeMemoryMapStack ();
if (HeadGuard == 0) {
// Don't pass the Guard page to user.
Address += EFI_PAGE_SIZE;
}
+
SetGuardForMemory (Address, NumberOfPages);
return Address;
}
@@ -1059,15 +1072,15 @@ SmmInternalFreePagesExWithGuard (
IN BOOLEAN AddRegion
)
{
- EFI_PHYSICAL_ADDRESS MemoryToFree;
- UINTN PagesToFree;
+ EFI_PHYSICAL_ADDRESS MemoryToFree;
+ UINTN PagesToFree;
if (((Memory & EFI_PAGE_MASK) != 0) || (Memory == 0) || (NumberOfPages == 0)) {
return EFI_INVALID_PARAMETER;
}
- MemoryToFree = Memory;
- PagesToFree = NumberOfPages;
+ MemoryToFree = Memory;
+ PagesToFree = NumberOfPages;
AdjustMemoryF (&MemoryToFree, &PagesToFree);
UnsetGuardForMemory (Memory, NumberOfPages);
@@ -1086,30 +1099,31 @@ SetAllGuardPages (
VOID
)
{
- UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 TableEntry;
- UINT64 Address;
- UINT64 GuardPage;
- INTN Level;
- UINTN Index;
- BOOLEAN OnGuarding;
-
- if (mGuardedMemoryMap == 0 ||
- mMapLevel == 0 ||
- mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH) {
+ UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 TableEntry;
+ UINT64 Address;
+ UINT64 GuardPage;
+ INTN Level;
+ UINTN Index;
+ BOOLEAN OnGuarding;
+
+ if ((mGuardedMemoryMap == 0) ||
+ (mMapLevel == 0) ||
+ (mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH))
+ {
return;
}
CopyMem (Entries, mLevelMask, sizeof (Entries));
CopyMem (Shifts, mLevelShift, sizeof (Shifts));
- SetMem (Tables, sizeof(Tables), 0);
- SetMem (Addresses, sizeof(Addresses), 0);
- SetMem (Indices, sizeof(Indices), 0);
+ SetMem (Tables, sizeof (Tables), 0);
+ SetMem (Addresses, sizeof (Addresses), 0);
+ SetMem (Indices, sizeof (Indices), 0);
Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
Tables[Level] = mGuardedMemoryMap;
@@ -1118,32 +1132,26 @@ SetAllGuardPages (
DEBUG_CODE (
DumpGuardedMemoryBitmap ();
- );
+ );
while (TRUE) {
if (Indices[Level] > Entries[Level]) {
Tables[Level] = 0;
Level -= 1;
} else {
-
- TableEntry = ((UINT64 *)(UINTN)(Tables[Level]))[Indices[Level]];
- Address = Addresses[Level];
+ TableEntry = ((UINT64 *)(UINTN)(Tables[Level]))[Indices[Level]];
+ Address = Addresses[Level];
if (TableEntry == 0) {
-
OnGuarding = FALSE;
-
} else if (Level < GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
-
- Level += 1;
- Tables[Level] = TableEntry;
- Addresses[Level] = Address;
- Indices[Level] = 0;
+ Level += 1;
+ Tables[Level] = TableEntry;
+ Addresses[Level] = Address;
+ Indices[Level] = 0;
continue;
-
} else {
-
Index = 0;
while (Index < GUARDED_HEAP_MAP_ENTRY_BITS) {
if ((TableEntry & 1) == 1) {
@@ -1152,6 +1160,7 @@ SetAllGuardPages (
} else {
GuardPage = Address - EFI_PAGE_SIZE;
}
+
OnGuarding = TRUE;
} else {
if (OnGuarding) {
@@ -1159,6 +1168,7 @@ SetAllGuardPages (
} else {
GuardPage = 0;
}
+
OnGuarding = FALSE;
}
@@ -1181,10 +1191,9 @@ SetAllGuardPages (
break;
}
- Indices[Level] += 1;
- Address = (Level == 0) ? 0 : Addresses[Level - 1];
- Addresses[Level] = Address | LShiftU64(Indices[Level], Shifts[Level]);
-
+ Indices[Level] += 1;
+ Address = (Level == 0) ? 0 : Addresses[Level - 1];
+ Addresses[Level] = Address | LShiftU64 (Indices[Level], Shifts[Level]);
}
}
@@ -1204,7 +1213,7 @@ SmmEntryPointMemoryManagementHook (
NULL,
(VOID **)&mSmmMemoryAttribute
);
- if (!EFI_ERROR(Status)) {
+ if (!EFI_ERROR (Status)) {
SetAllGuardPages ();
}
}
@@ -1220,11 +1229,11 @@ SmmEntryPointMemoryManagementHook (
**/
VOID
Uint64ToBinString (
- IN UINT64 Value,
- OUT CHAR8 *BinString
+ IN UINT64 Value,
+ OUT CHAR8 *BinString
)
{
- UINTN Index;
+ UINTN Index;
if (BinString == NULL) {
return;
@@ -1232,8 +1241,9 @@ Uint64ToBinString (
for (Index = 64; Index > 0; --Index) {
BinString[Index - 1] = '0' + (Value & 1);
- Value = RShiftU64 (Value, 1);
+ Value = RShiftU64 (Value, 1);
}
+
BinString[64] = '\0';
}
@@ -1246,40 +1256,44 @@ DumpGuardedMemoryBitmap (
VOID
)
{
- UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
- UINT64 TableEntry;
- UINT64 Address;
- INTN Level;
- UINTN RepeatZero;
- CHAR8 String[GUARDED_HEAP_MAP_ENTRY_BITS + 1];
- CHAR8 *Ruler1;
- CHAR8 *Ruler2;
-
- if (mGuardedMemoryMap == 0 ||
- mMapLevel == 0 ||
- mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH) {
+ UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
+ UINT64 TableEntry;
+ UINT64 Address;
+ INTN Level;
+ UINTN RepeatZero;
+ CHAR8 String[GUARDED_HEAP_MAP_ENTRY_BITS + 1];
+ CHAR8 *Ruler1;
+ CHAR8 *Ruler2;
+
+ if ((mGuardedMemoryMap == 0) ||
+ (mMapLevel == 0) ||
+ (mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH))
+ {
return;
}
Ruler1 = " 3 2 1 0";
Ruler2 = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
- DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "============================="
- " Guarded Memory Bitmap "
- "==============================\r\n"));
+ DEBUG ((
+ HEAP_GUARD_DEBUG_LEVEL,
+ "============================="
+ " Guarded Memory Bitmap "
+ "==============================\r\n"
+ ));
DEBUG ((HEAP_GUARD_DEBUG_LEVEL, " %a\r\n", Ruler1));
DEBUG ((HEAP_GUARD_DEBUG_LEVEL, " %a\r\n", Ruler2));
CopyMem (Entries, mLevelMask, sizeof (Entries));
CopyMem (Shifts, mLevelShift, sizeof (Shifts));
- SetMem (Indices, sizeof(Indices), 0);
- SetMem (Tables, sizeof(Tables), 0);
- SetMem (Addresses, sizeof(Addresses), 0);
+ SetMem (Indices, sizeof (Indices), 0);
+ SetMem (Tables, sizeof (Tables), 0);
+ SetMem (Addresses, sizeof (Addresses), 0);
Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
Tables[Level] = mGuardedMemoryMap;
@@ -1288,7 +1302,6 @@ DumpGuardedMemoryBitmap (
while (TRUE) {
if (Indices[Level] > Entries[Level]) {
-
Tables[Level] = 0;
Level -= 1;
RepeatZero = 0;
@@ -1298,40 +1311,33 @@ DumpGuardedMemoryBitmap (
"========================================="
"=========================================\r\n"
));
-
} else {
-
- TableEntry = ((UINT64 *)(UINTN)Tables[Level])[Indices[Level]];
- Address = Addresses[Level];
+ TableEntry = ((UINT64 *)(UINTN)Tables[Level])[Indices[Level]];
+ Address = Addresses[Level];
if (TableEntry == 0) {
-
if (Level == GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
if (RepeatZero == 0) {
- Uint64ToBinString(TableEntry, String);
+ Uint64ToBinString (TableEntry, String);
DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "%016lx: %a\r\n", Address, String));
} else if (RepeatZero == 1) {
DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "... : ...\r\n"));
}
+
RepeatZero += 1;
}
-
} else if (Level < GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
-
- Level += 1;
- Tables[Level] = TableEntry;
- Addresses[Level] = Address;
- Indices[Level] = 0;
- RepeatZero = 0;
+ Level += 1;
+ Tables[Level] = TableEntry;
+ Addresses[Level] = Address;
+ Indices[Level] = 0;
+ RepeatZero = 0;
continue;
-
} else {
-
RepeatZero = 0;
- Uint64ToBinString(TableEntry, String);
+ Uint64ToBinString (TableEntry, String);
DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "%016lx: %a\r\n", Address, String));
-
}
}
@@ -1339,10 +1345,9 @@ DumpGuardedMemoryBitmap (
break;
}
- Indices[Level] += 1;
- Address = (Level == 0) ? 0 : Addresses[Level - 1];
- Addresses[Level] = Address | LShiftU64(Indices[Level], Shifts[Level]);
-
+ Indices[Level] += 1;
+ Address = (Level == 0) ? 0 : Addresses[Level - 1];
+ Addresses[Level] = Address | LShiftU64 (Indices[Level], Shifts[Level]);
}
}
@@ -1357,8 +1362,8 @@ DumpGuardedMemoryBitmap (
**/
BOOLEAN
VerifyMemoryGuard (
- IN EFI_PHYSICAL_ADDRESS BaseAddress,
- IN UINTN NumberOfPages
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,
+ IN UINTN NumberOfPages
)
{
EFI_STATUS Status;
@@ -1370,35 +1375,42 @@ VerifyMemoryGuard (
}
Attribute = 0;
- Address = BaseAddress - EFI_PAGE_SIZE;
- Status = mSmmMemoryAttribute->GetMemoryAttributes (
- mSmmMemoryAttribute,
- Address,
- EFI_PAGE_SIZE,
- &Attribute
- );
- if (EFI_ERROR (Status) || (Attribute & EFI_MEMORY_RP) == 0) {
- DEBUG ((DEBUG_ERROR, "Head Guard is not set at: %016lx (%016lX)!!!\r\n",
- Address, Attribute));
+ Address = BaseAddress - EFI_PAGE_SIZE;
+ Status = mSmmMemoryAttribute->GetMemoryAttributes (
+ mSmmMemoryAttribute,
+ Address,
+ EFI_PAGE_SIZE,
+ &Attribute
+ );
+ if (EFI_ERROR (Status) || ((Attribute & EFI_MEMORY_RP) == 0)) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "Head Guard is not set at: %016lx (%016lX)!!!\r\n",
+ Address,
+ Attribute
+ ));
DumpGuardedMemoryBitmap ();
return FALSE;
}
Attribute = 0;
- Address = BaseAddress + EFI_PAGES_TO_SIZE (NumberOfPages);
- Status = mSmmMemoryAttribute->GetMemoryAttributes (
- mSmmMemoryAttribute,
- Address,
- EFI_PAGE_SIZE,
- &Attribute
- );
- if (EFI_ERROR (Status) || (Attribute & EFI_MEMORY_RP) == 0) {
- DEBUG ((DEBUG_ERROR, "Tail Guard is not set at: %016lx (%016lX)!!!\r\n",
- Address, Attribute));
+ Address = BaseAddress + EFI_PAGES_TO_SIZE (NumberOfPages);
+ Status = mSmmMemoryAttribute->GetMemoryAttributes (
+ mSmmMemoryAttribute,
+ Address,
+ EFI_PAGE_SIZE,
+ &Attribute
+ );
+ if (EFI_ERROR (Status) || ((Attribute & EFI_MEMORY_RP) == 0)) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "Tail Guard is not set at: %016lx (%016lX)!!!\r\n",
+ Address,
+ Attribute
+ ));
DumpGuardedMemoryBitmap ();
return FALSE;
}
return TRUE;
}
-