]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / BaseMemEncryptSevLib / SecMemEncryptSevLibInternal.c
1 /** @file
2
3 Secure Encrypted Virtualization (SEV) library helper function
4
5 Copyright (c) 2020, Advanced Micro Devices, Inc. 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 <Library/PcdLib.h>
15 #include <Register/Amd/Cpuid.h>
16 #include <Register/Amd/Msr.h>
17 #include <Register/Cpuid.h>
18 #include <Uefi/UefiBaseType.h>
19
20 /**
21 Reads and sets the status of SEV features.
22
23 **/
24 STATIC
25 UINT32
26 EFIAPI
27 InternalMemEncryptSevStatus (
28 VOID
29 )
30 {
31 UINT32 RegEax;
32 CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax;
33 BOOLEAN ReadSevMsr;
34 SEC_SEV_ES_WORK_AREA *SevEsWorkArea;
35
36 ReadSevMsr = FALSE;
37
38 SevEsWorkArea = (SEC_SEV_ES_WORK_AREA *)FixedPcdGet32 (PcdSevEsWorkAreaBase);
39 if ((SevEsWorkArea != NULL) && (SevEsWorkArea->EncryptionMask != 0)) {
40 //
41 // The MSR has been read before, so it is safe to read it again and avoid
42 // having to validate the CPUID information.
43 //
44 ReadSevMsr = TRUE;
45 } else {
46 //
47 // Check if memory encryption leaf exist
48 //
49 AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL);
50 if (RegEax >= CPUID_MEMORY_ENCRYPTION_INFO) {
51 //
52 // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported)
53 //
54 AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &Eax.Uint32, NULL, NULL, NULL);
55
56 if (Eax.Bits.SevBit) {
57 ReadSevMsr = TRUE;
58 }
59 }
60 }
61
62 return ReadSevMsr ? AsmReadMsr32 (MSR_SEV_STATUS) : 0;
63 }
64
65 /**
66 Returns a boolean to indicate whether SEV-ES is enabled.
67
68 @retval TRUE SEV-ES is enabled
69 @retval FALSE SEV-ES is not enabled
70 **/
71 BOOLEAN
72 EFIAPI
73 MemEncryptSevEsIsEnabled (
74 VOID
75 )
76 {
77 MSR_SEV_STATUS_REGISTER Msr;
78
79 Msr.Uint32 = InternalMemEncryptSevStatus ();
80
81 return Msr.Bits.SevEsBit ? TRUE : FALSE;
82 }
83
84 /**
85 Returns a boolean to indicate whether SEV is enabled.
86
87 @retval TRUE SEV is enabled
88 @retval FALSE SEV is not enabled
89 **/
90 BOOLEAN
91 EFIAPI
92 MemEncryptSevIsEnabled (
93 VOID
94 )
95 {
96 MSR_SEV_STATUS_REGISTER Msr;
97
98 Msr.Uint32 = InternalMemEncryptSevStatus ();
99
100 return Msr.Bits.SevBit ? TRUE : FALSE;
101 }
102
103 /**
104 Returns the SEV encryption mask.
105
106 @return The SEV pagtable encryption mask
107 **/
108 UINT64
109 EFIAPI
110 MemEncryptSevGetEncryptionMask (
111 VOID
112 )
113 {
114 CPUID_MEMORY_ENCRYPTION_INFO_EBX Ebx;
115 SEC_SEV_ES_WORK_AREA *SevEsWorkArea;
116 UINT64 EncryptionMask;
117
118 SevEsWorkArea = (SEC_SEV_ES_WORK_AREA *)FixedPcdGet32 (PcdSevEsWorkAreaBase);
119 if (SevEsWorkArea != NULL) {
120 EncryptionMask = SevEsWorkArea->EncryptionMask;
121 } else {
122 //
123 // CPUID Fn8000_001F[EBX] Bit 0:5 (memory encryption bit position)
124 //
125 AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, NULL, &Ebx.Uint32, NULL, NULL);
126 EncryptionMask = LShiftU64 (1, Ebx.Bits.PtePosBits);
127 }
128
129 return EncryptionMask;
130 }
131
132 /**
133 Locate the page range that covers the initial (pre-SMBASE-relocation) SMRAM
134 Save State Map.
135
136 @param[out] BaseAddress The base address of the lowest-address page that
137 covers the initial SMRAM Save State Map.
138
139 @param[out] NumberOfPages The number of pages in the page range that covers
140 the initial SMRAM Save State Map.
141
142 @retval RETURN_SUCCESS BaseAddress and NumberOfPages have been set on
143 output.
144
145 @retval RETURN_UNSUPPORTED SMM is unavailable.
146 **/
147 RETURN_STATUS
148 EFIAPI
149 MemEncryptSevLocateInitialSmramSaveStateMapPages (
150 OUT UINTN *BaseAddress,
151 OUT UINTN *NumberOfPages
152 )
153 {
154 return RETURN_UNSUPPORTED;
155 }