3 Virtual Memory Management Services to set or clear the memory encryption bit
5 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
10 Code is derived from MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
14 #ifndef __VIRTUAL_MEMORY__
15 #define __VIRTUAL_MEMORY__
17 #include <IndustryStandard/PageTable.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/CacheMaintenanceLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
25 #define SYS_CODE64_SEL 0x38
27 #define PAGE_TABLE_POOL_ALIGNMENT BASE_2MB
28 #define PAGE_TABLE_POOL_UNIT_SIZE SIZE_2MB
29 #define PAGE_TABLE_POOL_UNIT_PAGES \
30 EFI_SIZE_TO_PAGES (PAGE_TABLE_POOL_UNIT_SIZE)
31 #define PAGE_TABLE_POOL_ALIGN_MASK \
32 (~(EFI_PHYSICAL_ADDRESS)(PAGE_TABLE_POOL_ALIGNMENT - 1))
41 Return the pagetable memory encryption mask.
43 @return The pagetable memory encryption mask.
48 InternalGetMemEncryptionAddressMask (
53 This function clears memory encryption bit for the memory region specified by
54 PhysicalAddress and Length from the current page table context.
56 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
58 @param[in] PhysicalAddress The physical address that is the start
59 address of a memory region.
60 @param[in] Length The length of memory region
62 @retval RETURN_SUCCESS The attributes were cleared for the
64 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
65 @retval RETURN_UNSUPPORTED Clearing the memory encyrption attribute
70 InternalMemEncryptSevSetMemoryDecrypted (
71 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
72 IN PHYSICAL_ADDRESS PhysicalAddress
,
77 This function sets memory encryption bit for the memory region specified by
78 PhysicalAddress and Length from the current page table context.
80 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
82 @param[in] PhysicalAddress The physical address that is the start
83 address of a memory region.
84 @param[in] Length The length of memory region
86 @retval RETURN_SUCCESS The attributes were set for the memory
88 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
89 @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute
94 InternalMemEncryptSevSetMemoryEncrypted (
95 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
96 IN PHYSICAL_ADDRESS PhysicalAddress
,
101 Returns the encryption state of the specified virtual address range.
103 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
105 @param[in] BaseAddress Base address to check
106 @param[in] Length Length of virtual address range
108 @retval MemEncryptSevAddressRangeUnencrypted Address range is mapped
110 @retval MemEncryptSevAddressRangeEncrypted Address range is mapped
112 @retval MemEncryptSevAddressRangeMixed Address range is mapped mixed
113 @retval MemEncryptSevAddressRangeError Address range is not mapped
115 MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
117 InternalMemEncryptSevGetAddressRangeState (
118 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
119 IN PHYSICAL_ADDRESS BaseAddress
,
124 This function clears memory encryption bit for the MMIO region specified by
125 PhysicalAddress and Length.
127 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
129 @param[in] PhysicalAddress The physical address that is the start
130 address of a MMIO region.
131 @param[in] Length The length of memory region
133 @retval RETURN_SUCCESS The attributes were cleared for the
135 @retval RETURN_INVALID_PARAMETER Length is zero.
136 @retval RETURN_UNSUPPORTED Clearing the memory encyrption attribute
141 InternalMemEncryptSevClearMmioPageEncMask (
142 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
143 IN PHYSICAL_ADDRESS PhysicalAddress
,