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
61 @param[in] Flush Flush the caches before applying the
64 @retval RETURN_SUCCESS The attributes were cleared for the
66 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
67 @retval RETURN_UNSUPPORTED Clearing the memory encyrption attribute
72 InternalMemEncryptSevSetMemoryDecrypted (
73 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
74 IN PHYSICAL_ADDRESS PhysicalAddress
,
80 This function sets memory encryption bit for the memory region specified by
81 PhysicalAddress and Length from the current page table context.
83 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
85 @param[in] PhysicalAddress The physical address that is the start
86 address of a memory region.
87 @param[in] Length The length of memory region
88 @param[in] Flush Flush the caches before applying the
91 @retval RETURN_SUCCESS The attributes were set for the memory
93 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
94 @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute
99 InternalMemEncryptSevSetMemoryEncrypted (
100 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
101 IN PHYSICAL_ADDRESS PhysicalAddress
,
107 Returns the encryption state of the specified virtual address range.
109 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
111 @param[in] BaseAddress Base address to check
112 @param[in] Length Length of virtual address range
114 @retval MemEncryptSevAddressRangeUnencrypted Address range is mapped
116 @retval MemEncryptSevAddressRangeEncrypted Address range is mapped
118 @retval MemEncryptSevAddressRangeMixed Address range is mapped mixed
119 @retval MemEncryptSevAddressRangeError Address range is not mapped
121 MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
123 InternalMemEncryptSevGetAddressRangeState (
124 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
125 IN PHYSICAL_ADDRESS BaseAddress
,
130 This function clears memory encryption bit for the MMIO region specified by
131 PhysicalAddress and Length.
133 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
135 @param[in] PhysicalAddress The physical address that is the start
136 address of a MMIO region.
137 @param[in] Length The length of memory region
139 @retval RETURN_SUCCESS The attributes were cleared for the
141 @retval RETURN_INVALID_PARAMETER Length is zero.
142 @retval RETURN_UNSUPPORTED Clearing the memory encyrption attribute
147 InternalMemEncryptSevClearMmioPageEncMask (
148 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
149 IN PHYSICAL_ADDRESS PhysicalAddress
,