3 Secure Encrypted Virtualization (SEV) library helper function
5 Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/MemEncryptSevLib.h>
14 #include <Register/Amd/Cpuid.h>
15 #include <Register/Amd/Msr.h>
16 #include <Register/Cpuid.h>
19 This function clears memory encryption bit for the memory region specified by
20 BaseAddress and NumPages from the current page table context.
22 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
24 @param[in] BaseAddress The physical address that is the start
25 address of a memory region.
26 @param[in] NumPages The number of pages from start memory
29 @retval RETURN_SUCCESS The attributes were cleared for the
31 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
32 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
37 MemEncryptSevClearPageEncMask (
38 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
39 IN PHYSICAL_ADDRESS BaseAddress
,
44 // Memory encryption bit is not accessible in 32-bit mode
46 return RETURN_UNSUPPORTED
;
50 This function sets memory encryption bit for the memory region specified by
51 BaseAddress and NumPages from the current page table context.
53 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
55 @param[in] BaseAddress The physical address that is the start
56 address of a memory region.
57 @param[in] NumPages The number of pages from start memory
60 @retval RETURN_SUCCESS The attributes were set for the memory
62 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
63 @retval RETURN_UNSUPPORTED Setting the memory encryption attribute
68 MemEncryptSevSetPageEncMask (
69 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
70 IN PHYSICAL_ADDRESS BaseAddress
,
75 // Memory encryption bit is not accessible in 32-bit mode
77 return RETURN_UNSUPPORTED
;
81 Returns the encryption state of the specified virtual address range.
83 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
85 @param[in] BaseAddress Base address to check
86 @param[in] Length Length of virtual address range
88 @retval MemEncryptSevAddressRangeUnencrypted Address range is mapped
90 @retval MemEncryptSevAddressRangeEncrypted Address range is mapped
92 @retval MemEncryptSevAddressRangeMixed Address range is mapped mixed
93 @retval MemEncryptSevAddressRangeError Address range is not mapped
95 MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
97 MemEncryptSevGetAddressRangeState (
98 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
99 IN PHYSICAL_ADDRESS BaseAddress
,
104 // Memory is always encrypted in 32-bit mode
106 return MemEncryptSevAddressRangeEncrypted
;
110 This function clears memory encryption bit for the MMIO region specified by
111 BaseAddress and NumPages.
113 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
115 @param[in] BaseAddress The physical address that is the start
116 address of a MMIO region.
117 @param[in] NumPages The number of pages from start memory
120 @retval RETURN_SUCCESS The attributes were cleared for the
122 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
123 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
128 MemEncryptSevClearMmioPageEncMask (
129 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
130 IN PHYSICAL_ADDRESS BaseAddress
,
135 // Memory encryption bit is not accessible in 32-bit mode
137 return RETURN_UNSUPPORTED
;
141 Pre-validate the system RAM when SEV-SNP is enabled in the guest VM.
143 @param[in] BaseAddress Base address
144 @param[in] NumPages Number of pages starting from the base address
149 MemEncryptSevSnpPreValidateSystemRam (
150 IN PHYSICAL_ADDRESS BaseAddress
,