]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c
OvmfPkg/BaseMemEncryptSevLib: introduce MemEncryptSevClearMmioPageEncMask()
[mirror_edk2.git] / OvmfPkg / Library / BaseMemEncryptSevLib / X64 / MemEncryptSevLib.c
1 /** @file
2
3 Secure Encrypted Virtualization (SEV) library helper function
4
5 Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
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>
17
18 #include "VirtualMemory.h"
19
20 /**
21 This function clears memory encryption bit for the memory region specified by
22 BaseAddress and NumPages from the current page table context.
23
24 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
25 current CR3)
26 @param[in] BaseAddress The physical address that is the start
27 address of a memory region.
28 @param[in] NumPages The number of pages from start memory
29 region.
30 @param[in] Flush Flush the caches before clearing the bit
31 (mostly TRUE except MMIO addresses)
32
33 @retval RETURN_SUCCESS The attributes were cleared for the
34 memory region.
35 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
36 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
37 is not supported
38 **/
39 RETURN_STATUS
40 EFIAPI
41 MemEncryptSevClearPageEncMask (
42 IN PHYSICAL_ADDRESS Cr3BaseAddress,
43 IN PHYSICAL_ADDRESS BaseAddress,
44 IN UINTN NumPages,
45 IN BOOLEAN Flush
46 )
47 {
48 return InternalMemEncryptSevSetMemoryDecrypted (
49 Cr3BaseAddress,
50 BaseAddress,
51 EFI_PAGES_TO_SIZE (NumPages),
52 Flush
53 );
54 }
55
56 /**
57 This function sets memory encryption bit for the memory region specified by
58 BaseAddress and NumPages from the current page table context.
59
60 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
61 current CR3)
62 @param[in] BaseAddress The physical address that is the start
63 address of a memory region.
64 @param[in] NumPages The number of pages from start memory
65 region.
66 @param[in] Flush Flush the caches before setting the bit
67 (mostly TRUE except MMIO addresses)
68
69 @retval RETURN_SUCCESS The attributes were set for the memory
70 region.
71 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
72 @retval RETURN_UNSUPPORTED Setting the memory encryption attribute
73 is not supported
74 **/
75 RETURN_STATUS
76 EFIAPI
77 MemEncryptSevSetPageEncMask (
78 IN PHYSICAL_ADDRESS Cr3BaseAddress,
79 IN PHYSICAL_ADDRESS BaseAddress,
80 IN UINTN NumPages,
81 IN BOOLEAN Flush
82 )
83 {
84 return InternalMemEncryptSevSetMemoryEncrypted (
85 Cr3BaseAddress,
86 BaseAddress,
87 EFI_PAGES_TO_SIZE (NumPages),
88 Flush
89 );
90 }
91
92 /**
93 Returns the encryption state of the specified virtual address range.
94
95 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
96 current CR3)
97 @param[in] BaseAddress Base address to check
98 @param[in] Length Length of virtual address range
99
100 @retval MemEncryptSevAddressRangeUnencrypted Address range is mapped
101 unencrypted
102 @retval MemEncryptSevAddressRangeEncrypted Address range is mapped
103 encrypted
104 @retval MemEncryptSevAddressRangeMixed Address range is mapped mixed
105 @retval MemEncryptSevAddressRangeError Address range is not mapped
106 **/
107 MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
108 EFIAPI
109 MemEncryptSevGetAddressRangeState (
110 IN PHYSICAL_ADDRESS Cr3BaseAddress,
111 IN PHYSICAL_ADDRESS BaseAddress,
112 IN UINTN Length
113 )
114 {
115 return InternalMemEncryptSevGetAddressRangeState (
116 Cr3BaseAddress,
117 BaseAddress,
118 Length
119 );
120 }
121
122 /**
123 This function clears memory encryption bit for the mmio region specified by
124 BaseAddress and NumPages.
125
126 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
127 current CR3)
128 @param[in] BaseAddress The physical address that is the start
129 address of a mmio region.
130 @param[in] NumPages The number of pages from start memory
131 region.
132
133 @retval RETURN_SUCCESS The attributes were cleared for the
134 memory region.
135 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
136 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
137 is not supported
138 **/
139 RETURN_STATUS
140 EFIAPI
141 MemEncryptSevClearMmioPageEncMask (
142 IN PHYSICAL_ADDRESS Cr3BaseAddress,
143 IN PHYSICAL_ADDRESS BaseAddress,
144 IN UINTN NumPages
145 )
146 {
147 return InternalMemEncryptSevClearMmioPageEncMask (
148 Cr3BaseAddress,
149 BaseAddress,
150 EFI_PAGES_TO_SIZE (NumPages)
151 );
152
153 }