TianoCore EDK2 master
|
#include <Library/CpuLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Register/Amd/Cpuid.h>
#include <Register/Cpuid.h>
#include "VirtualMemory.h"
#include "SnpPageStateChange.h"
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_POOL * | mPageTablePool = NULL |
STATIC VOID * | mPscBuffer = NULL |
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.
enum MAP_RANGE_MODE |
Definition at line 28 of file PeiDxeVirtualMemory.c.
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.
Pages | The number of 4 KB pages to allocate. |
Definition at line 139 of file PeiDxeVirtualMemory.c.
STATIC VOID DisableReadOnlyPageWriteProtect | ( | VOID | ) |
Disable Write Protect on pages marked as read-only.
Definition at line 534 of file PeiDxeVirtualMemory.c.
Prevent the memory pages used for page table from been overwritten.
[in] | PageTableBase | Base address of page table (CR3). |
[in] | Level4Paging | Level 4 paging flag. |
Definition at line 382 of file PeiDxeVirtualMemory.c.
STATIC VOID EnableReadOnlyPageWriteProtect | ( | VOID | ) |
Enable Write Protect on pages marked as read-only.
Definition at line 546 of file PeiDxeVirtualMemory.c.
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.
[in] | PoolPages | The least page number of the pool to be created. |
TRUE | The pool is initialized successfully. |
FALSE | The memory is out of resource. |
Definition at line 79 of file PeiDxeVirtualMemory.c.
UINT64 EFIAPI InternalGetMemEncryptionAddressMask | ( | VOID | ) |
Return the pagetable memory encryption mask.
Definition at line 41 of file PeiDxeVirtualMemory.c.
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.
[in] | Cr3BaseAddress | Cr3 Base Address (if zero then use current CR3) |
[in] | PhysicalAddress | The physical address that is the start address of a MMIO region. |
[in] | Length | The length of memory region |
RETURN_SUCCESS | The attributes were cleared for the memory region. |
RETURN_INVALID_PARAMETER | Length is zero. |
RETURN_UNSUPPORTED | Clearing the memory encyrption attribute is not supported |
Definition at line 1104 of file PeiDxeVirtualMemory.c.
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.
[in] | Cr3BaseAddress | Cr3 Base Address (if zero then use current CR3) |
[in] | VirtualAddress | Virtual address |
[in] | Length | Length of virtual address range |
RETURN_INVALID_PARAMETER | Number of pages is zero. |
Definition at line 555 of file PeiDxeVirtualMemory.c.
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.
[in] | Cr3BaseAddress | Cr3 Base Address (if zero then use current CR3) |
[in] | PhysicalAddress | The physical address that is the start address of a memory region. |
[in] | Length | The length of memory region |
RETURN_SUCCESS | The attributes were cleared for the memory region. |
RETURN_INVALID_PARAMETER | Number of pages is zero. |
RETURN_UNSUPPORTED | Clearing the memory encyrption attribute is not supported |
Definition at line 1036 of file PeiDxeVirtualMemory.c.
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.
[in] | Cr3BaseAddress | Cr3 Base Address (if zero then use current CR3) |
[in] | PhysicalAddress | The physical address that is the start address of a memory region. |
[in] | Length | The length of memory region |
RETURN_SUCCESS | The attributes were set for the memory region. |
RETURN_INVALID_PARAMETER | Number of pages is zero. |
RETURN_UNSUPPORTED | Setting the memory encyrption attribute is not supported |
Definition at line 1070 of file PeiDxeVirtualMemory.c.
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.
TRUE | Write protection is enabled. |
FALSE | Write protection is disabled. |
Definition at line 522 of file PeiDxeVirtualMemory.c.
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.
[in] | Cr3BaseAddress | Cr3 Base Address (if zero then use current CR3) |
[in] | PhysicalAddress | The physical address that is the start address of a memory region. |
[in] | Length | The length of memory region |
[in] | Mode | Set or Clear mode |
[in] | CacheFlush | Flush the caches before applying the encryption mask |
[in] | Mmio | The physical address specified is Mmio |
RETURN_SUCCESS | The attributes were cleared for the memory region. |
RETURN_INVALID_PARAMETER | Number of pages is zero. |
RETURN_UNSUPPORTED | Setting the memory encyrption attribute is not supported |
Definition at line 711 of file PeiDxeVirtualMemory.c.
Set or Clear the memory encryption bit
[in,out] | PageTablePointer | Page table entry pointer (PTE). |
[in] | Mode | Set or Clear encryption bit |
Definition at line 497 of file PeiDxeVirtualMemory.c.
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.
[in] | PageTableBase | Base address of page table (CR3). |
[in] | Address | Start address of a page to be set as read-only. |
[in] | Level4Paging | Level 4 paging flag. |
Definition at line 257 of file PeiDxeVirtualMemory.c.
STATIC VOID Split1GPageTo2M | ( | IN PHYSICAL_ADDRESS | PhysicalAddress, |
IN OUT UINT64 * | PageEntry1G, | ||
IN PHYSICAL_ADDRESS | StackBase, | ||
IN UINTN | StackSize | ||
) |
Split 1G page to 2M.
[in] | PhysicalAddress | Start physical address the 1G page covered. |
[in,out] | PageEntry1G | Pointer to 1G page entry. |
[in] | StackBase | Stack base address. |
[in] | StackSize | Stack size. |
Definition at line 433 of file PeiDxeVirtualMemory.c.
STATIC VOID Split2MPageTo4K | ( | IN PHYSICAL_ADDRESS | PhysicalAddress, |
IN OUT UINT64 * | PageEntry2M, | ||
IN PHYSICAL_ADDRESS | StackBase, | ||
IN UINTN | StackSize | ||
) |
Split 2M page to 4K.
[in] | PhysicalAddress | Start physical address the 2M page covered. |
[in,out] | PageEntry2M | Pointer to 2M page entry. |
[in] | StackBase | Stack base address. |
[in] | StackSize | Stack size. |
Definition at line 189 of file PeiDxeVirtualMemory.c.
STATIC UINT64 mAddressEncMask |
Definition at line 23 of file PeiDxeVirtualMemory.c.
Definition at line 22 of file PeiDxeVirtualMemory.c.
STATIC PAGE_TABLE_POOL* mPageTablePool = NULL |
Definition at line 24 of file PeiDxeVirtualMemory.c.
Definition at line 26 of file PeiDxeVirtualMemory.c.