TianoCore EDK2 master
Loading...
Searching...
No Matches
HeapGuard.h File Reference
#include "PiSmmCore.h"

Go to the source code of this file.

Data Structures

struct  HEAP_GUARD_NODE
 

Macros

#define BYTE_LENGTH_SHIFT   3
 
#define GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT    (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT)
 
#define GUARDED_HEAP_MAP_TABLE_DEPTH   5
 
#define GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT   6
 
#define GUARDED_HEAP_MAP_ENTRY_BITS    (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)
 
#define GUARDED_HEAP_MAP_ENTRY_BYTES    (GUARDED_HEAP_MAP_ENTRY_BITS / 8)
 
#define GUARDED_HEAP_MAP_ENTRY_SHIFT
 
#define GUARDED_HEAP_MAP_ENTRY_MASK    ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1)
 
#define GUARDED_HEAP_MAP_SIZE    ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) * GUARDED_HEAP_MAP_ENTRY_BYTES)
 
#define GUARDED_HEAP_MAP_UNIT_SIZE    (GUARDED_HEAP_MAP_SIZE * 8 * EFI_PAGE_SIZE)
 
#define GUARDED_HEAP_MAP_ENTRIES_PER_UNIT    (GUARDED_HEAP_MAP_SIZE / GUARDED_HEAP_MAP_ENTRY_BYTES)
 
#define GUARDED_HEAP_MAP_ENTRY_INDEX(Address)
 
#define GUARDED_HEAP_MAP_ENTRY_BIT_INDEX(Address)
 
#define GUARDED_HEAP_MAP_BITS
 
#define GUARDED_HEAP_MAP_BIT_INDEX(Address)
 
#define GUARDED_HEAP_MAP_TABLE_SHIFT
 
#define GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS
 
#define GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS
 
#define GUARD_HEAP_TYPE_PAGE   BIT2
 
#define GUARD_HEAP_TYPE_POOL   BIT3
 
#define HEAP_GUARD_DEBUG_LEVEL   (DEBUG_POOL|DEBUG_PAGE)
 

Functions

VOID SetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
 
VOID UnsetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
 
VOID AdjustMemoryA (IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN OUT UINTN *NumberOfPages)
 
VOID AdjustMemoryF (IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN OUT UINTN *NumberOfPages)
 
BOOLEAN IsPoolTypeToGuard (IN EFI_MEMORY_TYPE MemoryType)
 
BOOLEAN IsPageTypeToGuard (IN EFI_MEMORY_TYPE MemoryType, IN EFI_ALLOCATE_TYPE AllocateType)
 
BOOLEAN EFIAPI IsMemoryGuarded (IN EFI_PHYSICAL_ADDRESS Address)
 
BOOLEAN EFIAPI IsGuardPage (IN EFI_PHYSICAL_ADDRESS Address)
 
VOID EFIAPI DumpGuardedMemoryBitmap (VOID)
 
VOID * AdjustPoolHeadA (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NoPages, IN UINTN Size)
 
VOID * AdjustPoolHeadF (IN EFI_PHYSICAL_ADDRESS Memory)
 
UINTN InternalAllocMaxAddressWithGuard (IN OUT LIST_ENTRY *FreePageList, IN UINTN NumberOfPages, IN UINTN MaxAddress, IN EFI_MEMORY_TYPE MemoryType)
 
EFI_STATUS SmmInternalFreePagesExWithGuard (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages, IN BOOLEAN AddRegion)
 
BOOLEAN IsHeapGuardEnabled (VOID)
 
BOOLEAN VerifyMemoryGuard (IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINTN NumberOfPages)
 

Variables

BOOLEAN mOnGuarding
 

Detailed Description

Data structure and functions to allocate and free memory space.

Copyright (c) 2017, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file HeapGuard.h.

Macro Definition Documentation

◆ BYTE_LENGTH_SHIFT

#define BYTE_LENGTH_SHIFT   3

Definition at line 56 of file HeapGuard.h.

◆ GUARD_HEAP_TYPE_PAGE

#define GUARD_HEAP_TYPE_PAGE   BIT2

Definition at line 157 of file HeapGuard.h.

◆ GUARD_HEAP_TYPE_POOL

#define GUARD_HEAP_TYPE_POOL   BIT3

Definition at line 158 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_BIT_INDEX

#define GUARDED_HEAP_MAP_BIT_INDEX (   Address)
Value:
(RShiftU64 (Address, EFI_PAGE_SHIFT) \
& ((1 << (GUARDED_HEAP_MAP_ENTRY_SHIFT \
+ GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)) - 1))
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: RShiftU64.c:28

Definition at line 116 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_BITS

#define GUARDED_HEAP_MAP_BITS
Value:
(1 << (GUARDED_HEAP_MAP_ENTRY_SHIFT \
+ GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT))

Definition at line 109 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRIES_PER_UNIT

#define GUARDED_HEAP_MAP_ENTRIES_PER_UNIT    (GUARDED_HEAP_MAP_SIZE / GUARDED_HEAP_MAP_ENTRY_BYTES)

Definition at line 92 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_BIT_INDEX

#define GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (   Address)
Value:
(RShiftU64 (Address, EFI_PAGE_SHIFT) \
& ((1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) - 1))

Definition at line 102 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT

#define GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT   6

Definition at line 64 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_BITS

#define GUARDED_HEAP_MAP_ENTRY_BITS    (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)

Definition at line 66 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_BYTES

#define GUARDED_HEAP_MAP_ENTRY_BYTES    (GUARDED_HEAP_MAP_ENTRY_BITS / 8)

Definition at line 69 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_INDEX

#define GUARDED_HEAP_MAP_ENTRY_INDEX (   Address)
Value:
(RShiftU64 (Address, EFI_PAGE_SHIFT \
+ GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) \
& GUARDED_HEAP_MAP_ENTRY_MASK)

Definition at line 96 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_MASK

#define GUARDED_HEAP_MAP_ENTRY_MASK    ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1)

Definition at line 80 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_ENTRY_SHIFT

#define GUARDED_HEAP_MAP_ENTRY_SHIFT
Value:
(GUARDED_HEAP_MAP_ENTRY_BITS \
- GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 4 \
- GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \
- EFI_PAGE_SHIFT)

Definition at line 73 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_SIZE

#define GUARDED_HEAP_MAP_SIZE    ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) * GUARDED_HEAP_MAP_ENTRY_BYTES)

Definition at line 84 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_TABLE_DEPTH

#define GUARDED_HEAP_MAP_TABLE_DEPTH   5

Definition at line 61 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS

#define GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS
Value:
{ \
(1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \
(1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \
(1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \
(1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \
(1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1 \
}

Definition at line 145 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS

#define GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS
Value:
{ \
GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 3, \
GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 2, \
GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT, \
GUARDED_HEAP_MAP_TABLE_SHIFT, \
EFI_PAGE_SHIFT + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \
}

Definition at line 132 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT

#define GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT    (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT)

Definition at line 58 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_TABLE_SHIFT

#define GUARDED_HEAP_MAP_TABLE_SHIFT
Value:
(GUARDED_HEAP_MAP_ENTRY_SHIFT + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \
+ EFI_PAGE_SHIFT)

Definition at line 124 of file HeapGuard.h.

◆ GUARDED_HEAP_MAP_UNIT_SIZE

#define GUARDED_HEAP_MAP_UNIT_SIZE    (GUARDED_HEAP_MAP_SIZE * 8 * EFI_PAGE_SIZE)

Definition at line 88 of file HeapGuard.h.

◆ HEAP_GUARD_DEBUG_LEVEL

#define HEAP_GUARD_DEBUG_LEVEL   (DEBUG_POOL|DEBUG_PAGE)

Definition at line 163 of file HeapGuard.h.

Function Documentation

◆ AdjustMemoryA()

VOID AdjustMemoryA ( IN OUT EFI_PHYSICAL_ADDRESS Memory,
IN OUT UINTN NumberOfPages 
)

Adjust the base and number of pages to really allocate according to Guard.

Parameters
[in,out]MemoryBase address of free memory.
[in,out]NumberOfPagesSize of memory to allocate.
Returns
VOID.

Definition at line 982 of file HeapGuard.c.

◆ AdjustMemoryF()

VOID AdjustMemoryF ( IN OUT EFI_PHYSICAL_ADDRESS Memory,
IN OUT UINTN NumberOfPages 
)

Adjust the start address and number of pages to free according to Guard.

The purpose of this function is to keep the shared Guard page with adjacent memory block if it's still in guard, or free it if no more sharing. Another is to reserve pages as Guard pages in partial page free situation.

Parameters
[in,out]MemoryBase address of memory to free.
[in,out]NumberOfPagesSize of memory to free.
Returns
VOID.

Definition at line 884 of file HeapGuard.c.

◆ AdjustPoolHeadA()

VOID * AdjustPoolHeadA ( IN EFI_PHYSICAL_ADDRESS  Memory,
IN UINTN  NoPages,
IN UINTN  Size 
)

Adjust the pool head position to make sure the Guard page is adjavent to pool tail or pool head.

Parameters
[in]MemoryBase address of memory allocated.
[in]NoPagesNumber of pages actually allocated.
[in]SizeSize of memory requested. (plus pool head/tail overhead)
Returns
Address of pool head.

Adjust the pool head position to make sure the Guard page is adjavent to pool tail or pool head.

Parameters
[in]MemoryBase address of memory allocated.
[in]NoPagesNumber of pages actually allocated.
[in]SizeSize of memory requested. (plus pool head/tail overhead)
Returns
Address of pool head

Definition at line 1016 of file HeapGuard.c.

◆ AdjustPoolHeadF()

VOID * AdjustPoolHeadF ( IN EFI_PHYSICAL_ADDRESS  Memory)

Get the page base address according to pool head address.

Parameters
[in]MemoryHead address of pool to free.
Returns
Address of pool head.

Definition at line 976 of file HeapGuard.c.

◆ DumpGuardedMemoryBitmap()

VOID EFIAPI DumpGuardedMemoryBitmap ( VOID  )

Dump the guarded memory bit map.

Definition at line 1659 of file HeapGuard.c.

◆ InternalAllocMaxAddressWithGuard()

UINTN InternalAllocMaxAddressWithGuard ( IN OUT LIST_ENTRY FreePageList,
IN UINTN  NumberOfPages,
IN UINTN  MaxAddress,
IN EFI_MEMORY_TYPE  MemoryType 
)

Helper function of memory allocation with Guard pages.

Parameters
FreePageListThe free page node.
NumberOfPagesNumber of pages to be allocated.
MaxAddressRequest to allocate memory below this address.
MemoryTypeType of memory requested.
Returns
Memory address of allocated pages.

Definition at line 1004 of file HeapGuard.c.

◆ IsGuardPage()

BOOLEAN EFIAPI IsGuardPage ( IN EFI_PHYSICAL_ADDRESS  Address)

Check to see if the page at the given address is a Guard page or not.

Parameters
[in]AddressThe address to check for.
Returns
TRUE The page at Address is a Guard page.
FALSE The page at Address is not a Guard page.

Definition at line 461 of file HeapGuard.c.

◆ IsHeapGuardEnabled()

BOOLEAN IsHeapGuardEnabled ( VOID  )

Check to see if the heap guard is enabled for page and/or pool allocation.

Returns
TRUE/FALSE.

Definition at line 672 of file HeapGuard.c.

◆ IsMemoryGuarded()

BOOLEAN EFIAPI IsMemoryGuarded ( IN EFI_PHYSICAL_ADDRESS  Address)

Check to see if the page at the given address is guarded or not.

Parameters
[in]AddressThe address to check for.
Returns
TRUE The page at Address is guarded.
FALSE The page at Address is not guarded.

Definition at line 486 of file HeapGuard.c.

◆ IsPageTypeToGuard()

BOOLEAN IsPageTypeToGuard ( IN EFI_MEMORY_TYPE  MemoryType,
IN EFI_ALLOCATE_TYPE  AllocateType 
)

Check to see if the page at the given address should be guarded or not.

Parameters
[in]MemoryTypePage type to check.
[in]AllocateTypeAllocation type to check.
Returns
TRUE The given type of page should be guarded.
FALSE The given type of page should not be guarded.

Definition at line 658 of file HeapGuard.c.

◆ IsPoolTypeToGuard()

BOOLEAN IsPoolTypeToGuard ( IN EFI_MEMORY_TYPE  MemoryType)

Check to see if the pool at the given address should be guarded or not.

Parameters
[in]MemoryTypePool type to check.
Returns
TRUE The given type of pool should be guarded.
FALSE The given type of pool should not be guarded.

Definition at line 637 of file HeapGuard.c.

◆ SetGuardForMemory()

VOID SetGuardForMemory ( IN EFI_PHYSICAL_ADDRESS  Memory,
IN UINTN  NumberOfPages 
)

Set head Guard and tail Guard for the given memory range.

Parameters
[in]MemoryBase address of memory to set guard for.
[in]NumberOfPagesMemory size in pages.
Returns
VOID.

Set head Guard and tail Guard for the given memory range.

Parameters
[in]MemoryBase address of memory to set guard for.
[in]NumberOfPagesMemory size in pages.
Returns
VOID

Definition at line 690 of file HeapGuard.c.

◆ SmmInternalFreePagesExWithGuard()

EFI_STATUS SmmInternalFreePagesExWithGuard ( IN EFI_PHYSICAL_ADDRESS  Memory,
IN UINTN  NumberOfPages,
IN BOOLEAN  AddRegion 
)

Helper function of memory free with Guard pages.

Parameters
[in]MemoryBase address of memory being freed.
[in]NumberOfPagesThe number of pages to free.
[in]AddRegionIf this memory is new added region.
Return values
EFI_NOT_FOUNDCould not find the entry that covers the range.
EFI_INVALID_PARAMETERAddress not aligned, Address is zero or NumberOfPages is zero.
Returns
EFI_SUCCESS Pages successfully freed.

Definition at line 1083 of file HeapGuard.c.

◆ UnsetGuardForMemory()

VOID UnsetGuardForMemory ( IN EFI_PHYSICAL_ADDRESS  Memory,
IN UINTN  NumberOfPages 
)

Unset head Guard and tail Guard for the given memory range.

Parameters
[in]MemoryBase address of memory to unset guard for.
[in]NumberOfPagesMemory size in pages.
Returns
VOID.

Unset head Guard and tail Guard for the given memory range.

Parameters
[in]MemoryBase address of memory to unset guard for.
[in]NumberOfPagesMemory size in pages.
Returns
VOID

Definition at line 726 of file HeapGuard.c.

◆ VerifyMemoryGuard()

BOOLEAN VerifyMemoryGuard ( IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINTN  NumberOfPages 
)

Debug function used to verify if the Guard page is well set or not.

Parameters
[in]BaseAddressAddress of memory to check.
[in]NumberOfPagesSize of memory in pages.
Returns
TRUE The head Guard and tail Guard are both well set.
FALSE The head Guard and/or tail Guard are not well set.

Definition at line 1378 of file HeapGuard.c.

Variable Documentation

◆ mOnGuarding

BOOLEAN mOnGuarding
extern

Definition at line 17 of file HeapGuard.c.