]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLibInternal.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / BaseMemEncryptSevLib / DxeMemEncryptSevLibInternal.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 <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 STATIC BOOLEAN mSevStatus = FALSE;
21 STATIC BOOLEAN mSevEsStatus = FALSE;
22 STATIC BOOLEAN mSevStatusChecked = FALSE;
23
24 STATIC UINT64 mSevEncryptionMask = 0;
25 STATIC BOOLEAN mSevEncryptionMaskSaved = FALSE;
26
27 /**
28 Reads and sets the status of SEV features.
29
30 **/
31 STATIC
32 VOID
33 EFIAPI
34 InternalMemEncryptSevStatus (
35 VOID
36 )
37 {
38 UINT32 RegEax;
39 MSR_SEV_STATUS_REGISTER Msr;
40 CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax;
41 BOOLEAN ReadSevMsr;
42 UINT64 EncryptionMask;
43
44 ReadSevMsr = FALSE;
45
46 EncryptionMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask);
47 if (EncryptionMask != 0) {
48 //
49 // The MSR has been read before, so it is safe to read it again and avoid
50 // having to validate the CPUID information.
51 //
52 ReadSevMsr = TRUE;
53 } else {
54 //
55 // Check if memory encryption leaf exist
56 //
57 AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL);
58 if (RegEax >= CPUID_MEMORY_ENCRYPTION_INFO) {
59 //
60 // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported)
61 //
62 AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &Eax.Uint32, NULL, NULL, NULL);
63
64 if (Eax.Bits.SevBit) {
65 ReadSevMsr = TRUE;
66 }
67 }
68 }
69
70 if (ReadSevMsr) {
71 //
72 // Check MSR_0xC0010131 Bit 0 (Sev Enabled)
73 //
74 Msr.Uint32 = AsmReadMsr32 (MSR_SEV_STATUS);
75 if (Msr.Bits.SevBit) {
76 mSevStatus = TRUE;
77 }
78
79 //
80 // Check MSR_0xC0010131 Bit 1 (Sev-Es Enabled)
81 //
82 if (Msr.Bits.SevEsBit) {
83 mSevEsStatus = TRUE;
84 }
85 }
86
87 mSevStatusChecked = TRUE;
88 }
89
90 /**
91 Returns a boolean to indicate whether SEV-ES is enabled.
92
93 @retval TRUE SEV-ES is enabled
94 @retval FALSE SEV-ES is not enabled
95 **/
96 BOOLEAN
97 EFIAPI
98 MemEncryptSevEsIsEnabled (
99 VOID
100 )
101 {
102 if (!mSevStatusChecked) {
103 InternalMemEncryptSevStatus ();
104 }
105
106 return mSevEsStatus;
107 }
108
109 /**
110 Returns a boolean to indicate whether SEV is enabled.
111
112 @retval TRUE SEV is enabled
113 @retval FALSE SEV is not enabled
114 **/
115 BOOLEAN
116 EFIAPI
117 MemEncryptSevIsEnabled (
118 VOID
119 )
120 {
121 if (!mSevStatusChecked) {
122 InternalMemEncryptSevStatus ();
123 }
124
125 return mSevStatus;
126 }
127
128 /**
129 Returns the SEV encryption mask.
130
131 @return The SEV pagtable encryption mask
132 **/
133 UINT64
134 EFIAPI
135 MemEncryptSevGetEncryptionMask (
136 VOID
137 )
138 {
139 if (!mSevEncryptionMaskSaved) {
140 mSevEncryptionMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask);
141 mSevEncryptionMaskSaved = TRUE;
142 }
143
144 return mSevEncryptionMask;
145 }