]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
31 @retval RETURN_SUCCESS The attributes were cleared for the
32 memory region.
33 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
34 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
35 is not supported
36 **/
37 RETURN_STATUS
38 EFIAPI
39 MemEncryptSevClearPageEncMask (
40 IN PHYSICAL_ADDRESS Cr3BaseAddress,
41 IN PHYSICAL_ADDRESS BaseAddress,
42 IN UINTN NumPages
43 )
44 {
45 return InternalMemEncryptSevSetMemoryDecrypted (
46 Cr3BaseAddress,
47 BaseAddress,
48 EFI_PAGES_TO_SIZE (NumPages)
49 );
50 }
51
52 /**
53 This function sets memory encryption bit for the memory region specified by
54 BaseAddress and NumPages from the current page table context.
55
56 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
57 current CR3)
58 @param[in] BaseAddress The physical address that is the start
59 address of a memory region.
60 @param[in] NumPages The number of pages from start memory
61 region.
62
63 @retval RETURN_SUCCESS The attributes were set for the memory
64 region.
65 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
66 @retval RETURN_UNSUPPORTED Setting the memory encryption attribute
67 is not supported
68 **/
69 RETURN_STATUS
70 EFIAPI
71 MemEncryptSevSetPageEncMask (
72 IN PHYSICAL_ADDRESS Cr3BaseAddress,
73 IN PHYSICAL_ADDRESS BaseAddress,
74 IN UINTN NumPages
75 )
76 {
77 return InternalMemEncryptSevSetMemoryEncrypted (
78 Cr3BaseAddress,
79 BaseAddress,
80 EFI_PAGES_TO_SIZE (NumPages)
81 );
82 }
83
84 /**
85 Returns the encryption state of the specified virtual address range.
86
87 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
88 current CR3)
89 @param[in] BaseAddress Base address to check
90 @param[in] Length Length of virtual address range
91
92 @retval MemEncryptSevAddressRangeUnencrypted Address range is mapped
93 unencrypted
94 @retval MemEncryptSevAddressRangeEncrypted Address range is mapped
95 encrypted
96 @retval MemEncryptSevAddressRangeMixed Address range is mapped mixed
97 @retval MemEncryptSevAddressRangeError Address range is not mapped
98 **/
99 MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
100 EFIAPI
101 MemEncryptSevGetAddressRangeState (
102 IN PHYSICAL_ADDRESS Cr3BaseAddress,
103 IN PHYSICAL_ADDRESS BaseAddress,
104 IN UINTN Length
105 )
106 {
107 return InternalMemEncryptSevGetAddressRangeState (
108 Cr3BaseAddress,
109 BaseAddress,
110 Length
111 );
112 }
113
114 /**
115 This function clears memory encryption bit for the mmio region specified by
116 BaseAddress and NumPages.
117
118 @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use
119 current CR3)
120 @param[in] BaseAddress The physical address that is the start
121 address of a mmio region.
122 @param[in] NumPages The number of pages from start memory
123 region.
124
125 @retval RETURN_SUCCESS The attributes were cleared for the
126 memory region.
127 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
128 @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute
129 is not supported
130 **/
131 RETURN_STATUS
132 EFIAPI
133 MemEncryptSevClearMmioPageEncMask (
134 IN PHYSICAL_ADDRESS Cr3BaseAddress,
135 IN PHYSICAL_ADDRESS BaseAddress,
136 IN UINTN NumPages
137 )
138 {
139 return InternalMemEncryptSevClearMmioPageEncMask (
140 Cr3BaseAddress,
141 BaseAddress,
142 EFI_PAGES_TO_SIZE (NumPages)
143 );
144 }