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

Go to the source code of this file.

Functions

STATIC VOID SetBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN BitNumber, IN UINT64 *BitMap)
 
STATIC VOID ClearBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN BitNumber, IN UINT64 *BitMap)
 
STATIC UINT64 GetBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN BitNumber, IN UINT64 *BitMap)
 
VOID * PageAlloc (IN UINTN Pages)
 
UINTN FindGuardedMemoryMap (IN EFI_PHYSICAL_ADDRESS Address, IN BOOLEAN AllocMapUnit, OUT UINT64 **BitMap)
 
VOID EFIAPI SetGuardedMemoryBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN NumberOfPages)
 
VOID EFIAPI ClearGuardedMemoryBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN NumberOfPages)
 
UINTN GetGuardedMemoryBits (IN EFI_PHYSICAL_ADDRESS Address, IN UINTN NumberOfPages)
 
UINTN EFIAPI GetGuardMapBit (IN EFI_PHYSICAL_ADDRESS Address)
 
BOOLEAN EFIAPI IsGuardPage (IN EFI_PHYSICAL_ADDRESS Address)
 
BOOLEAN EFIAPI IsMemoryGuarded (IN EFI_PHYSICAL_ADDRESS Address)
 
VOID EFIAPI SetGuardPage (IN EFI_PHYSICAL_ADDRESS BaseAddress)
 
VOID EFIAPI UnsetGuardPage (IN EFI_PHYSICAL_ADDRESS BaseAddress)
 
BOOLEAN IsMemoryTypeToGuard (IN EFI_MEMORY_TYPE MemoryType, IN EFI_ALLOCATE_TYPE AllocateType, IN UINT8 PageOrPool)
 
BOOLEAN IsPoolTypeToGuard (IN EFI_MEMORY_TYPE MemoryType)
 
BOOLEAN IsPageTypeToGuard (IN EFI_MEMORY_TYPE MemoryType, IN EFI_ALLOCATE_TYPE AllocateType)
 
BOOLEAN IsHeapGuardEnabled (VOID)
 
VOID SetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
 
VOID UnsetGuardForMemory (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN NumberOfPages)
 
VOID AdjustMemoryF (IN OUT EFI_PHYSICAL_ADDRESS *Memory, IN OUT UINTN *NumberOfPages)
 
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)
 
VOID SetAllGuardPages (VOID)
 
VOID SmmEntryPointMemoryManagementHook (VOID)
 
VOID Uint64ToBinString (IN UINT64 Value, OUT CHAR8 *BinString)
 
VOID EFIAPI DumpGuardedMemoryBitmap (VOID)
 
BOOLEAN VerifyMemoryGuard (IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINTN NumberOfPages)
 

Variables

GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE
 
GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap = 0
 
GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel = 1
 
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
 
EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOLmSmmMemoryAttribute = NULL
 

Detailed Description

UEFI Heap Guard functions.

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

Definition in file HeapGuard.c.

Function Documentation

◆ 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 825 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

Definition at line 948 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.

◆ ClearBits()

STATIC VOID ClearBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  BitNumber,
IN UINT64 *  BitMap 
)

Set corresponding bits in bitmap table to 0 according to the address.

Parameters
[in]AddressStart address to set for.
[in]BitNumberNumber of bits to set.
[in]BitMapPointer to bitmap which covers the Address.
Returns
VOID.

Definition at line 111 of file HeapGuard.c.

◆ ClearGuardedMemoryBits()

VOID EFIAPI ClearGuardedMemoryBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  NumberOfPages 
)

Clear corresponding bits in bitmap table according to given memory range.

Parameters
[in]AddressMemory address to unset from.
[in]NumberOfPagesNumber of pages to unset guard.
Returns
VOID

Definition at line 359 of file HeapGuard.c.

◆ DumpGuardedMemoryBitmap()

VOID EFIAPI DumpGuardedMemoryBitmap ( VOID  )

Dump the guarded memory bit map.

Definition at line 1269 of file HeapGuard.c.

◆ FindGuardedMemoryMap()

UINTN FindGuardedMemoryMap ( IN EFI_PHYSICAL_ADDRESS  Address,
IN BOOLEAN  AllocMapUnit,
OUT UINT64 **  BitMap 
)

Locate the pointer of bitmap from the guarded memory bitmap tables, which covers the given Address.

Parameters
[in]AddressStart address to search the bitmap for.
[in]AllocMapUnitFlag to indicate memory allocation for the table.
[out]BitMapPointer to bitmap which covers the Address.
Returns
The bit number from given Address to the end of current map table.

Definition at line 244 of file HeapGuard.c.

◆ GetBits()

STATIC UINT64 GetBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  BitNumber,
IN UINT64 *  BitMap 
)

Get corresponding bits in bitmap table according to the address.

The value of bit 0 corresponds to the status of memory at given Address. No more than 64 bits can be retrieved in one call.

Parameters
[in]AddressStart address to retrieve bits for.
[in]BitNumberNumber of bits to get.
[in]BitMapPointer to bitmap which covers the Address.
Returns
An integer containing the bits information.

Definition at line 166 of file HeapGuard.c.

◆ GetGuardedMemoryBits()

UINTN GetGuardedMemoryBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  NumberOfPages 
)

Retrieve corresponding bits in bitmap table according to given memory range.

Parameters
[in]AddressMemory address to retrieve from.
[in]NumberOfPagesNumber of pages to retrieve.
Returns
An integer containing the guarded memory bitmap.

Definition at line 395 of file HeapGuard.c.

◆ GetGuardMapBit()

UINTN EFIAPI GetGuardMapBit ( IN EFI_PHYSICAL_ADDRESS  Address)

Get bit value in bitmap table for the given address.

Parameters
[in]AddressThe address to retrieve for.
Returns
1 or 0.

Definition at line 441 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 471 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 496 of file HeapGuard.c.

◆ IsMemoryTypeToGuard()

BOOLEAN IsMemoryTypeToGuard ( IN EFI_MEMORY_TYPE  MemoryType,
IN EFI_ALLOCATE_TYPE  AllocateType,
IN UINT8  PageOrPool 
)

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

Parameters
[in]MemoryTypeMemory type to check.
[in]AllocateTypeAllocation type to check.
[in]PageOrPoolIndicate a page allocation or pool allocation.
Returns
TRUE The given type of memory should be guarded.
FALSE The given type of memory should not be guarded.

Definition at line 589 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.

◆ PageAlloc()

VOID * PageAlloc ( IN UINTN  Pages)

Helper function to allocate pages without Guard for internal uses.

Parameters
[in]PagesPage number.
Returns
Address of memory allocated.

Definition at line 212 of file HeapGuard.c.

◆ SetAllGuardPages()

VOID SetAllGuardPages ( VOID  )

Set all Guard pages which cannot be set during the non-SMM mode time.

Definition at line 1112 of file HeapGuard.c.

◆ SetBits()

STATIC VOID SetBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  BitNumber,
IN UINT64 *  BitMap 
)

Set corresponding bits in bitmap table to 1 according to the address.

Parameters
[in]AddressStart address to set for.
[in]BitNumberNumber of bits to set.
[in]BitMapPointer to bitmap which covers the Address.
Returns
VOID

Definition at line 55 of file HeapGuard.c.

◆ SetGuardedMemoryBits()

VOID EFIAPI SetGuardedMemoryBits ( IN EFI_PHYSICAL_ADDRESS  Address,
IN UINTN  NumberOfPages 
)

Set corresponding bits in bitmap table to 1 according to given memory range.

Parameters
[in]AddressMemory address to guard from.
[in]NumberOfPagesNumber of pages to guard.
Returns
VOID

Definition at line 322 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.

Definition at line 692 of file HeapGuard.c.

◆ SetGuardPage()

VOID EFIAPI SetGuardPage ( IN EFI_PHYSICAL_ADDRESS  BaseAddress)

Set the page at the given address to be a Guard page.

This is done by changing the page table attribute to be NOT PRESENT.

Parameters
[in]BaseAddressPage address to Guard at.
Returns
VOID.

Definition at line 514 of file HeapGuard.c.

◆ SmmEntryPointMemoryManagementHook()

VOID SmmEntryPointMemoryManagementHook ( VOID  )

Hook function used to set all Guard pages after entering SMM mode.

Definition at line 1218 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.

◆ Uint64ToBinString()

VOID Uint64ToBinString ( IN UINT64  Value,
OUT CHAR8 *  BinString 
)

Helper function to convert a UINT64 value in binary to a string.

Parameters
[in]ValueValue of a UINT64 integer.
[out]BinStringString buffer to contain the conversion result.
Returns
VOID.

Definition at line 1245 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.

Definition at line 728 of file HeapGuard.c.

◆ UnsetGuardPage()

VOID EFIAPI UnsetGuardPage ( IN EFI_PHYSICAL_ADDRESS  BaseAddress)

Unset the Guard page at the given address to the normal memory.

This is done by changing the page table attribute to be PRESENT.

Parameters
[in]BaseAddressPage address to Guard at.
Returns
VOID.

Definition at line 544 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

◆ mGuardedMemoryMap

GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap = 0

Definition at line 22 of file HeapGuard.c.

◆ mLevelMask

GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelMask[GUARDED_HEAP_MAP_TABLE_DEPTH] = GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS

Definition at line 36 of file HeapGuard.c.

◆ mLevelShift

GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelShift[GUARDED_HEAP_MAP_TABLE_DEPTH] = GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS

Definition at line 34 of file HeapGuard.c.

◆ mMapLevel

Definition at line 29 of file HeapGuard.c.

◆ mOnGuarding

GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE

Definition at line 15 of file HeapGuard.c.

◆ mSmmMemoryAttribute

EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL* mSmmMemoryAttribute = NULL

Definition at line 42 of file HeapGuard.c.