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

Go to the source code of this file.

Functions

VOID SmmWriteUnprotectReadOnlyPage (OUT BOOLEAN *WriteProtect)
 
VOID SmmWriteProtectReadOnlyPage (IN BOOLEAN WriteProtect)
 
BOOLEAN InitializePageTablePool (IN UINTN PoolPages)
 
VOID * AllocatePageTableMemory (IN UINTN Pages)
 
VOID * GetPageTableEntry (IN UINTN PageTableBase, IN BOOLEAN Enable5LevelPaging, IN PHYSICAL_ADDRESS Address, OUT PAGE_ATTRIBUTE *PageAttribute)
 
UINT64 GetAttributesFromPageEntry (IN UINT64 *PageEntry)
 
RETURN_STATUS ConvertMemoryPageAttributes (IN UINTN PageTableBase, IN PAGING_MODE PagingMode, IN PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, IN BOOLEAN IsSet, OUT BOOLEAN *IsModified OPTIONAL)
 
VOID EFIAPI FlushTlbOnCurrentProcessor (IN OUT VOID *Buffer)
 
VOID FlushTlbForAll (VOID)
 
EFI_STATUS SmmSetMemoryAttributesEx (IN UINTN PageTableBase, IN PAGING_MODE PagingMode, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
EFI_STATUS SmmClearMemoryAttributesEx (IN UINTN PageTableBase, IN PAGING_MODE PagingMode, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
EFI_STATUS SmmSetMemoryAttributes (IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
EFI_STATUS SmmClearMemoryAttributes (IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
EFI_STATUS SetShadowStack (IN UINTN Cr3, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length)
 
EFI_STATUS EFIAPI SmmGetSystemConfigurationTable (IN EFI_GUID *TableGuid, OUT VOID **Table)
 
VOID PatchSmmSaveStateMap (VOID)
 
VOID PatchGdtIdtMap (VOID)
 
VOID SetMemMapWithNonPresentRange (UINT64 Base, UINT64 Limit, UINT64 Attribute, IA32_MAP_ENTRY *Map, UINTN Count)
 
VOID SetMemMapAttributes (EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable)
 
EFI_STATUS EFIAPI EdkiiSmmSetMemoryAttributes (IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
EFI_STATUS EFIAPI EdkiiSmmClearMemoryAttributes (IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
 
VOID GenPageTable (IN OUT UINTN *PageTable, IN PAGING_MODE PagingMode, IN UINT64 LinearAddress, IN UINT64 Length, IN IA32_MAP_ATTRIBUTE MapAttribute, IN IA32_MAP_ATTRIBUTE MapMask)
 
UINTN GenSmmPageTable (IN PAGING_MODE PagingMode, IN UINT8 PhysicalAddressBits)
 
EFI_STATUS EFIAPI EdkiiSmmGetMemoryAttributes (IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, OUT UINT64 *Attributes)
 
VOID EnablePageTableProtection (VOID)
 
BOOLEAN IfReadOnlyPageTableNeeded (VOID)
 
VOID SetPageTableAttributes (VOID)
 

Variables

BOOLEAN mIsShadowStack = FALSE
 
BOOLEAN m5LevelPagingNeeded = FALSE
 
PAGING_MODE mPagingMode = PagingModeMax
 
PAGE_TABLE_POOLmPageTablePool = NULL
 
BOOLEAN mIsReadOnlyPageTable = FALSE
 

Detailed Description

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

Definition in file SmmCpuMemoryManagement.c.

Function Documentation

◆ AllocatePageTableMemory()

VOID * AllocatePageTableMemory ( IN UINTN  Pages)

This API provides a way to allocate memory for page table.

This API can be called more once to allocate memory for page tables.

Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.

Parameters
PagesThe number of 4 KB pages to allocate.
Returns
A pointer to the allocated buffer or NULL if allocation fails.

Definition at line 152 of file SmmCpuMemoryManagement.c.

◆ ConvertMemoryPageAttributes()

RETURN_STATUS ConvertMemoryPageAttributes ( IN UINTN  PageTableBase,
IN PAGING_MODE  PagingMode,
IN PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes,
IN BOOLEAN  IsSet,
OUT BOOLEAN *IsModified  OPTIONAL 
)

This function modifies the page attributes for the memory region specified by BaseAddress and Length from their current attributes to the attributes specified by Attributes.

Caller should make sure BaseAddress and Length is at page boundary.

Parameters
[in]PageTableBaseThe page table base.
[in]PagingModeThe paging mode.
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
[in]AttributesThe bit mask of attributes to modify for the memory region.
[in]IsSetTRUE means to set attributes. FALSE means to clear attributes.
[out]IsModifiedTRUE means page table modified. FALSE means page table not modified.
Return values
RETURN_SUCCESSThe attributes were modified for the memory region.
RETURN_ACCESS_DENIEDThe attributes for the memory resource range specified by BaseAddress and Length cannot be modified.
RETURN_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be set together.
RETURN_OUT_OF_RESOURCESThere are not enough system resources to modify the attributes of the memory resource range.
RETURN_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not support for the memory resource range specified by BaseAddress and Length.

Definition at line 331 of file SmmCpuMemoryManagement.c.

◆ EdkiiSmmClearMemoryAttributes()

EFI_STATUS EFIAPI EdkiiSmmClearMemoryAttributes ( IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL This,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function clears given attributes of the memory region specified by BaseAddress and Length.

Parameters
ThisThe EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
BaseAddressThe physical address that is the start address of a memory region.
LengthThe size in bytes of the memory region.
AttributesThe bit mask of attributes to clear for the memory region.
Return values
EFI_SUCCESSThe attributes were cleared for the memory region.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be cleared together.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not supported for the memory resource range specified by BaseAddress and Length.

Definition at line 1160 of file SmmCpuMemoryManagement.c.

◆ EdkiiSmmGetMemoryAttributes()

EFI_STATUS EFIAPI EdkiiSmmGetMemoryAttributes ( IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL This,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
OUT UINT64 *  Attributes 
)

This function retrieves the attributes of the memory region specified by BaseAddress and Length. If different attributes are got from different part of the memory region, EFI_NO_MAPPING will be returned.

Parameters
ThisThe EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
BaseAddressThe physical address that is the start address of a memory region.
LengthThe size in bytes of the memory region.
AttributesPointer to attributes returned.
Return values
EFI_SUCCESSThe attributes got for the memory region.
EFI_INVALID_PARAMETERLength is zero. Attributes is NULL.
EFI_NO_MAPPINGAttributes are not consistent cross the memory region.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length.

Definition at line 1368 of file SmmCpuMemoryManagement.c.

◆ EdkiiSmmSetMemoryAttributes()

EFI_STATUS EFIAPI EdkiiSmmSetMemoryAttributes ( IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL This,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function set given attributes of the memory region specified by BaseAddress and Length.

Parameters
ThisThe EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
BaseAddressThe physical address that is the start address of a memory region.
LengthThe size in bytes of the memory region.
AttributesThe bit mask of attributes to set for the memory region.
Return values
EFI_SUCCESSThe attributes were set for the memory region.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be set together.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not supported for the memory resource range specified by BaseAddress and Length.

Definition at line 1125 of file SmmCpuMemoryManagement.c.

◆ EnablePageTableProtection()

VOID EnablePageTableProtection ( VOID  )

Prevent the memory pages used for SMM page table from been overwritten.

Definition at line 1443 of file SmmCpuMemoryManagement.c.

◆ FlushTlbForAll()

VOID FlushTlbForAll ( VOID  )

FlushTlb for all processors.

Definition at line 527 of file SmmCpuMemoryManagement.c.

◆ FlushTlbOnCurrentProcessor()

VOID EFIAPI FlushTlbOnCurrentProcessor ( IN OUT VOID *  Buffer)

FlushTlb on current processor.

Parameters
[in,out]BufferPointer to private data buffer.

Definition at line 516 of file SmmCpuMemoryManagement.c.

◆ GenPageTable()

VOID GenPageTable ( IN OUT UINTN PageTable,
IN PAGING_MODE  PagingMode,
IN UINT64  LinearAddress,
IN UINT64  Length,
IN IA32_MAP_ATTRIBUTE  MapAttribute,
IN IA32_MAP_ATTRIBUTE  MapMask 
)

Create page table based on input PagingMode, LinearAddress and Length.

Parameters
[in,out]PageTableThe pointer to the page table.
[in]PagingModeThe paging mode.
[in]LinearAddressThe start of the linear address range.
[in]LengthThe length of the linear address range.
[in]MapAttributeThe MapAttribute of the linear address range
[in]MapMaskThe MapMask used for attribute. The corresponding field in Attribute is ignored if that in MapMask is 0.

Definition at line 1182 of file SmmCpuMemoryManagement.c.

◆ GenSmmPageTable()

UINTN GenSmmPageTable ( IN PAGING_MODE  PagingMode,
IN UINT8  PhysicalAddressBits 
)

Create page table based on input PagingMode and PhysicalAddressBits in smm.

Parameters
[in]PagingModeThe paging mode.
[in]PhysicalAddressBitsThe bits of physical address to map.
Return values
PageTableAddress

Definition at line 1238 of file SmmCpuMemoryManagement.c.

◆ GetAttributesFromPageEntry()

UINT64 GetAttributesFromPageEntry ( IN UINT64 *  PageEntry)

Return memory attributes of page entry.

Parameters
[in]PageEntryThe page entry.
Returns
Memory attributes of page entry.

Definition at line 281 of file SmmCpuMemoryManagement.c.

◆ GetPageTableEntry()

VOID * GetPageTableEntry ( IN UINTN  PageTableBase,
IN BOOLEAN  Enable5LevelPaging,
IN PHYSICAL_ADDRESS  Address,
OUT PAGE_ATTRIBUTE *  PageAttribute 
)

Return page table entry to match the address.

Parameters
[in]PageTableBaseThe page table base.
[in]Enable5LevelPagingIf PML5 paging is enabled.
[in]AddressThe address to be checked.
[out]PageAttributesThe page attribute of the page entry.
Returns
The page entry.

Definition at line 192 of file SmmCpuMemoryManagement.c.

◆ IfReadOnlyPageTableNeeded()

BOOLEAN IfReadOnlyPageTableNeeded ( VOID  )

Return whether memory used by SMM page table need to be set as Read Only.

Return values
TRUENeed to set SMM page table as Read Only.
FALSEDo not set SMM page table as Read Only.

Definition at line 1483 of file SmmCpuMemoryManagement.c.

◆ InitializePageTablePool()

BOOLEAN InitializePageTablePool ( IN UINTN  PoolPages)

Initialize a buffer pool for page table use only.

To reduce the potential split operation on page table, the pages reserved for page table should be allocated in the times of PAGE_TABLE_POOL_UNIT_PAGES and at the boundary of PAGE_TABLE_POOL_ALIGNMENT. So the page pool is always initialized with number of pages greater than or equal to the given PoolPages.

Once the pages in the pool are used up, this method should be called again to reserve at least another PAGE_TABLE_POOL_UNIT_PAGES. But usually this won't happen in practice.

Parameters
PoolPagesThe least page number of the pool to be created.
Return values
TRUEThe pool is initialized successfully.
FALSEThe memory is out of resource.

Definition at line 83 of file SmmCpuMemoryManagement.c.

◆ PatchGdtIdtMap()

VOID PatchGdtIdtMap ( VOID  )

This function sets GDT/IDT buffer to be RO and XP.

Definition at line 888 of file SmmCpuMemoryManagement.c.

◆ PatchSmmSaveStateMap()

VOID PatchSmmSaveStateMap ( VOID  )

This function sets SMM save state buffer to be RW and XP.

Definition at line 772 of file SmmCpuMemoryManagement.c.

◆ SetMemMapAttributes()

VOID SetMemMapAttributes ( EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE MemoryAttributesTable)

This function sets memory attribute according to MemoryAttributesTable.

Parameters
MemoryAttributesTableA pointer to the buffer of SmmMemoryAttributesTable.

Definition at line 1001 of file SmmCpuMemoryManagement.c.

◆ SetMemMapWithNonPresentRange()

VOID SetMemMapWithNonPresentRange ( UINT64  Base,
UINT64  Limit,
UINT64  Attribute,
IA32_MAP_ENTRY Map,
UINTN  Count 
)

This function set [Base, Limit] to the input MemoryAttribute.

Parameters
BaseStart address of range.
LimitLimit address of range.
AttributeThe bit mask of attributes to modify for the memory region.
MapPointer to the array of Cr3 IA32_MAP_ENTRY.
CountCount of IA32_MAP_ENTRY in Map.

Definition at line 940 of file SmmCpuMemoryManagement.c.

◆ SetPageTableAttributes()

VOID SetPageTableAttributes ( VOID  )

This function sets memory attribute for page table.

Definition at line 1507 of file SmmCpuMemoryManagement.c.

◆ SetShadowStack()

EFI_STATUS SetShadowStack ( IN UINTN  Cr3,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length 
)

Set ShadowStack memory.

Parameters
[in]Cr3The page table base address.
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
Return values
EFI_SUCCESSThe shadow stack memory is set.

Definition at line 719 of file SmmCpuMemoryManagement.c.

◆ SmmClearMemoryAttributes()

EFI_STATUS SmmClearMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function clears the attributes for the memory region specified by BaseAddress and Length from their current attributes to the attributes specified by Attributes.

Parameters
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
[in]AttributesThe bit mask of attributes to clear for the memory region.
Return values
EFI_SUCCESSThe attributes were cleared for the memory region.
EFI_ACCESS_DENIEDThe attributes for the memory resource range specified by BaseAddress and Length cannot be modified.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be cleared together.
EFI_OUT_OF_RESOURCESThere are not enough system resources to modify the attributes of the memory resource range.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not supported for the memory resource range specified by BaseAddress and Length.

Definition at line 697 of file SmmCpuMemoryManagement.c.

◆ SmmClearMemoryAttributesEx()

EFI_STATUS SmmClearMemoryAttributesEx ( IN UINTN  PageTableBase,
IN PAGING_MODE  PagingMode,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function clears the attributes for the memory region specified by BaseAddress and Length from their current attributes to the attributes specified by Attributes.

Parameters
[in]PageTableBaseThe page table base.
[in]PagingModeThe paging mode.
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
[in]AttributesThe bit mask of attributes to clear for the memory region.
Return values
EFI_SUCCESSThe attributes were cleared for the memory region.
EFI_ACCESS_DENIEDThe attributes for the memory resource range specified by BaseAddress and Length cannot be modified.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be cleared together.
EFI_OUT_OF_RESOURCESThere are not enough system resources to modify the attributes of the memory resource range.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not supported for the memory resource range specified by BaseAddress and Length.

Definition at line 615 of file SmmCpuMemoryManagement.c.

◆ SmmGetSystemConfigurationTable()

EFI_STATUS EFIAPI SmmGetSystemConfigurationTable ( IN EFI_GUID TableGuid,
OUT VOID **  Table 
)

Retrieves a pointer to the system configuration table from the SMM System Table based on a specified GUID.

Parameters
[in]TableGuidThe pointer to table's GUID type.
[out]TableThe pointer to the table associated with TableGuid in the EFI System Table.
Return values
EFI_SUCCESSA configuration table matching TableGuid was found.
EFI_NOT_FOUNDA configuration table matching TableGuid could not be found.

Definition at line 747 of file SmmCpuMemoryManagement.c.

◆ SmmSetMemoryAttributes()

EFI_STATUS SmmSetMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function sets the attributes for the memory region specified by BaseAddress and Length from their current attributes to the attributes specified by Attributes.

Parameters
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
[in]AttributesThe bit mask of attributes to set for the memory region.
Return values
EFI_SUCCESSThe attributes were set for the memory region.
EFI_ACCESS_DENIEDThe attributes for the memory resource range specified by BaseAddress and Length cannot be modified.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be set together.
EFI_OUT_OF_RESOURCESThere are not enough system resources to modify the attributes of the memory resource range.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not supported for the memory resource range specified by BaseAddress and Length.

Definition at line 662 of file SmmCpuMemoryManagement.c.

◆ SmmSetMemoryAttributesEx()

EFI_STATUS SmmSetMemoryAttributesEx ( IN UINTN  PageTableBase,
IN PAGING_MODE  PagingMode,
IN EFI_PHYSICAL_ADDRESS  BaseAddress,
IN UINT64  Length,
IN UINT64  Attributes 
)

This function sets the attributes for the memory region specified by BaseAddress and Length from their current attributes to the attributes specified by Attributes.

Parameters
[in]PageTableBaseThe page table base.
[in]PagingModeThe paging mode.
[in]BaseAddressThe physical address that is the start address of a memory region.
[in]LengthThe size in bytes of the memory region.
[in]AttributesThe bit mask of attributes to set for the memory region.
Return values
EFI_SUCCESSThe attributes were set for the memory region.
EFI_ACCESS_DENIEDThe attributes for the memory resource range specified by BaseAddress and Length cannot be modified.
EFI_INVALID_PARAMETERLength is zero. Attributes specified an illegal combination of attributes that cannot be set together.
EFI_OUT_OF_RESOURCESThere are not enough system resources to modify the attributes of the memory resource range.
EFI_UNSUPPORTEDThe processor does not support one or more bytes of the memory resource range specified by BaseAddress and Length. The bit mask of attributes is not support for the memory resource range specified by BaseAddress and Length.

Definition at line 566 of file SmmCpuMemoryManagement.c.

◆ SmmWriteProtectReadOnlyPage()

VOID SmmWriteProtectReadOnlyPage ( IN BOOLEAN  WriteProtect)

Write protect read-only pages.

Parameters
[in]WriteProtectIf Cr0.Bits.WP should be enabled.

Definition at line 52 of file SmmCpuMemoryManagement.c.

◆ SmmWriteUnprotectReadOnlyPage()

VOID SmmWriteUnprotectReadOnlyPage ( OUT BOOLEAN *  WriteProtect)

Write unprotect read-only pages if Cr0.Bits.WP is 1.

Parameters
[out]WriteProtectIf Cr0.Bits.WP is enabled.

Definition at line 31 of file SmmCpuMemoryManagement.c.

Variable Documentation

◆ m5LevelPagingNeeded

BOOLEAN m5LevelPagingNeeded = FALSE

Definition at line 11 of file SmmCpuMemoryManagement.c.

◆ mIsReadOnlyPageTable

BOOLEAN mIsReadOnlyPageTable = FALSE

Definition at line 22 of file SmmCpuMemoryManagement.c.

◆ mIsShadowStack

BOOLEAN mIsShadowStack = FALSE

Definition at line 10 of file SmmCpuMemoryManagement.c.

◆ mPageTablePool

PAGE_TABLE_POOL* mPageTablePool = NULL

Definition at line 17 of file SmmCpuMemoryManagement.c.

◆ mPagingMode

PAGING_MODE mPagingMode = PagingModeMax

Definition at line 12 of file SmmCpuMemoryManagement.c.