TianoCore EDK2 master
Loading...
Searching...
No Matches
PeiDxeVirtualMemory.c File Reference

Go to the source code of this file.

Enumerations

enum  MAP_RANGE_MODE { SetCBit , ClearCBit }
 

Functions

UINT64 EFIAPI InternalGetMemEncryptionAddressMask (VOID)
 
STATIC BOOLEAN InitializePageTablePool (IN UINTN PoolPages)
 
STATIC VOID *EFIAPI AllocatePageTableMemory (IN UINTN Pages)
 
STATIC VOID Split2MPageTo4K (IN PHYSICAL_ADDRESS PhysicalAddress, IN OUT UINT64 *PageEntry2M, IN PHYSICAL_ADDRESS StackBase, IN UINTN StackSize)
 
STATIC VOID SetPageTablePoolReadOnly (IN UINTN PageTableBase, IN EFI_PHYSICAL_ADDRESS Address, IN BOOLEAN Level4Paging)
 
STATIC VOID EnablePageTableProtection (IN UINTN PageTableBase, IN BOOLEAN Level4Paging)
 
STATIC VOID Split1GPageTo2M (IN PHYSICAL_ADDRESS PhysicalAddress, IN OUT UINT64 *PageEntry1G, IN PHYSICAL_ADDRESS StackBase, IN UINTN StackSize)
 
STATIC VOID SetOrClearCBit (IN OUT UINT64 *PageTablePointer, IN MAP_RANGE_MODE Mode)
 
STATIC BOOLEAN IsReadOnlyPageWriteProtected (VOID)
 
STATIC VOID DisableReadOnlyPageWriteProtect (VOID)
 
STATIC VOID EnableReadOnlyPageWriteProtect (VOID)
 
RETURN_STATUS EFIAPI InternalMemEncryptSevCreateIdentityMap1G (IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length)
 
STATIC RETURN_STATUS EFIAPI SetMemoryEncDec (IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length, IN MAP_RANGE_MODE Mode, IN BOOLEAN CacheFlush, IN BOOLEAN Mmio)
 
RETURN_STATUS EFIAPI InternalMemEncryptSevSetMemoryDecrypted (IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length)
 
RETURN_STATUS EFIAPI InternalMemEncryptSevSetMemoryEncrypted (IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length)
 
RETURN_STATUS EFIAPI InternalMemEncryptSevClearMmioPageEncMask (IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length)
 

Variables

STATIC BOOLEAN mAddressEncMaskChecked = FALSE
 
STATIC UINT64 mAddressEncMask
 
STATIC PAGE_TABLE_POOLmPageTablePool = NULL
 
STATIC VOID * mPscBuffer = NULL
 

Detailed Description

Virtual Memory Management Services to set or clear the memory encryption bit

Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
Copyright (c) 2017 - 2024, AMD Incorporated. All rights reserved.

SPDX-License-Identifier: BSD-2-Clause-Patent

Code is derived from MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c

Definition in file PeiDxeVirtualMemory.c.

Enumeration Type Documentation

◆ MAP_RANGE_MODE

enum MAP_RANGE_MODE

Definition at line 28 of file PeiDxeVirtualMemory.c.

Function Documentation

◆ AllocatePageTableMemory()

STATIC VOID *EFIAPI AllocatePageTableMemory ( IN UINTN  Pages)

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

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

Allocates the number of 4KB pages 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 139 of file PeiDxeVirtualMemory.c.

◆ DisableReadOnlyPageWriteProtect()

STATIC VOID DisableReadOnlyPageWriteProtect ( VOID  )

Disable Write Protect on pages marked as read-only.

Definition at line 534 of file PeiDxeVirtualMemory.c.

◆ EnablePageTableProtection()

STATIC VOID EnablePageTableProtection ( IN UINTN  PageTableBase,
IN BOOLEAN  Level4Paging 
)

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

Parameters
[in]PageTableBaseBase address of page table (CR3).
[in]Level4PagingLevel 4 paging flag.

Definition at line 382 of file PeiDxeVirtualMemory.c.

◆ EnableReadOnlyPageWriteProtect()

STATIC VOID EnableReadOnlyPageWriteProtect ( VOID  )

Enable Write Protect on pages marked as read-only.

Definition at line 546 of file PeiDxeVirtualMemory.c.

◆ InitializePageTablePool()

STATIC 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. Usually this won't happen often in practice.

Parameters
[in]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 79 of file PeiDxeVirtualMemory.c.

◆ InternalGetMemEncryptionAddressMask()

UINT64 EFIAPI InternalGetMemEncryptionAddressMask ( VOID  )

Return the pagetable memory encryption mask.

Returns
The pagetable memory encryption mask.

Definition at line 41 of file PeiDxeVirtualMemory.c.

◆ InternalMemEncryptSevClearMmioPageEncMask()

RETURN_STATUS EFIAPI InternalMemEncryptSevClearMmioPageEncMask ( IN PHYSICAL_ADDRESS  Cr3BaseAddress,
IN PHYSICAL_ADDRESS  PhysicalAddress,
IN UINTN  Length 
)

This function clears memory encryption bit for the MMIO region specified by PhysicalAddress and Length.

Parameters
[in]Cr3BaseAddressCr3 Base Address (if zero then use current CR3)
[in]PhysicalAddressThe physical address that is the start address of a MMIO region.
[in]LengthThe length of memory region
Return values
RETURN_SUCCESSThe attributes were cleared for the memory region.
RETURN_INVALID_PARAMETERLength is zero.
RETURN_UNSUPPORTEDClearing the memory encyrption attribute is not supported

Definition at line 1104 of file PeiDxeVirtualMemory.c.

◆ InternalMemEncryptSevCreateIdentityMap1G()

RETURN_STATUS EFIAPI InternalMemEncryptSevCreateIdentityMap1G ( IN PHYSICAL_ADDRESS  Cr3BaseAddress,
IN PHYSICAL_ADDRESS  PhysicalAddress,
IN UINTN  Length 
)

Create 1GB identity mapping for the specified virtual address range.

The function is preliminary used by the SEV-SNP page state change APIs to build the page table required before issuing the PVALIDATE instruction. The function must be removed after the EDK2 core is enhanced to do the lazy validation.

Parameters
[in]Cr3BaseAddressCr3 Base Address (if zero then use current CR3)
[in]VirtualAddressVirtual address
[in]LengthLength of virtual address range
Return values
RETURN_INVALID_PARAMETERNumber of pages is zero.

Definition at line 555 of file PeiDxeVirtualMemory.c.

◆ InternalMemEncryptSevSetMemoryDecrypted()

RETURN_STATUS EFIAPI InternalMemEncryptSevSetMemoryDecrypted ( IN PHYSICAL_ADDRESS  Cr3BaseAddress,
IN PHYSICAL_ADDRESS  PhysicalAddress,
IN UINTN  Length 
)

This function clears memory encryption bit for the memory region specified by PhysicalAddress and Length from the current page table context.

Parameters
[in]Cr3BaseAddressCr3 Base Address (if zero then use current CR3)
[in]PhysicalAddressThe physical address that is the start address of a memory region.
[in]LengthThe length of memory region
Return values
RETURN_SUCCESSThe attributes were cleared for the memory region.
RETURN_INVALID_PARAMETERNumber of pages is zero.
RETURN_UNSUPPORTEDClearing the memory encyrption attribute is not supported

Definition at line 1036 of file PeiDxeVirtualMemory.c.

◆ InternalMemEncryptSevSetMemoryEncrypted()

RETURN_STATUS EFIAPI InternalMemEncryptSevSetMemoryEncrypted ( IN PHYSICAL_ADDRESS  Cr3BaseAddress,
IN PHYSICAL_ADDRESS  PhysicalAddress,
IN UINTN  Length 
)

This function sets memory encryption bit for the memory region specified by PhysicalAddress and Length from the current page table context.

Parameters
[in]Cr3BaseAddressCr3 Base Address (if zero then use current CR3)
[in]PhysicalAddressThe physical address that is the start address of a memory region.
[in]LengthThe length of memory region
Return values
RETURN_SUCCESSThe attributes were set for the memory region.
RETURN_INVALID_PARAMETERNumber of pages is zero.
RETURN_UNSUPPORTEDSetting the memory encyrption attribute is not supported

Definition at line 1070 of file PeiDxeVirtualMemory.c.

◆ IsReadOnlyPageWriteProtected()

STATIC BOOLEAN IsReadOnlyPageWriteProtected ( VOID  )

Check the WP status in CR0 register. This bit is used to lock or unlock write access to pages marked as read-only.

Return values
TRUEWrite protection is enabled.
FALSEWrite protection is disabled.

Definition at line 522 of file PeiDxeVirtualMemory.c.

◆ SetMemoryEncDec()

STATIC RETURN_STATUS EFIAPI SetMemoryEncDec ( IN PHYSICAL_ADDRESS  Cr3BaseAddress,
IN PHYSICAL_ADDRESS  PhysicalAddress,
IN UINTN  Length,
IN MAP_RANGE_MODE  Mode,
IN BOOLEAN  CacheFlush,
IN BOOLEAN  Mmio 
)

This function either sets or clears memory encryption bit for the memory region specified by PhysicalAddress and Length from the current page table context.

The function iterates through the PhysicalAddress one page at a time, and set or clears the memory encryption mask in the page table. If it encounters that a given physical address range is part of large page then it attempts to change the attribute at one go (based on size), otherwise it splits the large pages into smaller (e.g 2M page into 4K pages) and then try to set or clear the encryption bit on the smallest page size.

Parameters
[in]Cr3BaseAddressCr3 Base Address (if zero then use current CR3)
[in]PhysicalAddressThe physical address that is the start address of a memory region.
[in]LengthThe length of memory region
[in]ModeSet or Clear mode
[in]CacheFlushFlush the caches before applying the encryption mask
[in]MmioThe physical address specified is Mmio
Return values
RETURN_SUCCESSThe attributes were cleared for the memory region.
RETURN_INVALID_PARAMETERNumber of pages is zero.
RETURN_UNSUPPORTEDSetting the memory encyrption attribute is not supported

Definition at line 711 of file PeiDxeVirtualMemory.c.

◆ SetOrClearCBit()

STATIC VOID SetOrClearCBit ( IN OUT UINT64 *  PageTablePointer,
IN MAP_RANGE_MODE  Mode 
)

Set or Clear the memory encryption bit

Parameters
[in,out]PageTablePointerPage table entry pointer (PTE).
[in]ModeSet or Clear encryption bit

Definition at line 497 of file PeiDxeVirtualMemory.c.

◆ SetPageTablePoolReadOnly()

STATIC VOID SetPageTablePoolReadOnly ( IN UINTN  PageTableBase,
IN EFI_PHYSICAL_ADDRESS  Address,
IN BOOLEAN  Level4Paging 
)

Set one page of page table pool memory to be read-only.

Parameters
[in]PageTableBaseBase address of page table (CR3).
[in]AddressStart address of a page to be set as read-only.
[in]Level4PagingLevel 4 paging flag.

Definition at line 257 of file PeiDxeVirtualMemory.c.

◆ Split1GPageTo2M()

STATIC VOID Split1GPageTo2M ( IN PHYSICAL_ADDRESS  PhysicalAddress,
IN OUT UINT64 *  PageEntry1G,
IN PHYSICAL_ADDRESS  StackBase,
IN UINTN  StackSize 
)

Split 1G page to 2M.

Parameters
[in]PhysicalAddressStart physical address the 1G page covered.
[in,out]PageEntry1GPointer to 1G page entry.
[in]StackBaseStack base address.
[in]StackSizeStack size.

Definition at line 433 of file PeiDxeVirtualMemory.c.

◆ Split2MPageTo4K()

STATIC VOID Split2MPageTo4K ( IN PHYSICAL_ADDRESS  PhysicalAddress,
IN OUT UINT64 *  PageEntry2M,
IN PHYSICAL_ADDRESS  StackBase,
IN UINTN  StackSize 
)

Split 2M page to 4K.

Parameters
[in]PhysicalAddressStart physical address the 2M page covered.
[in,out]PageEntry2MPointer to 2M page entry.
[in]StackBaseStack base address.
[in]StackSizeStack size.

Definition at line 189 of file PeiDxeVirtualMemory.c.

Variable Documentation

◆ mAddressEncMask

STATIC UINT64 mAddressEncMask

Definition at line 23 of file PeiDxeVirtualMemory.c.

◆ mAddressEncMaskChecked

STATIC BOOLEAN mAddressEncMaskChecked = FALSE

Definition at line 22 of file PeiDxeVirtualMemory.c.

◆ mPageTablePool

STATIC PAGE_TABLE_POOL* mPageTablePool = NULL

Definition at line 24 of file PeiDxeVirtualMemory.c.

◆ mPscBuffer

STATIC VOID* mPscBuffer = NULL

Definition at line 26 of file PeiDxeVirtualMemory.c.