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 | #include <ConfidentialComputingGuestAttr.h>
|
---|
20 |
|
---|
21 | STATIC UINT64 mCurrentAttr = 0;
|
---|
22 | STATIC BOOLEAN mCurrentAttrRead = FALSE;
|
---|
23 | STATIC UINT64 mSevEncryptionMask = 0;
|
---|
24 | STATIC BOOLEAN mSevEncryptionMaskSaved = FALSE;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | The function check if the specified Attr is set.
|
---|
28 |
|
---|
29 | @param[in] CurrentAttr The current attribute.
|
---|
30 | @param[in] Attr The attribute to check.
|
---|
31 |
|
---|
32 | @retval TRUE The specified Attr is set.
|
---|
33 | @retval FALSE The specified Attr is not set.
|
---|
34 |
|
---|
35 | **/
|
---|
36 | STATIC
|
---|
37 | BOOLEAN
|
---|
38 | AmdMemEncryptionAttrCheck (
|
---|
39 | IN UINT64 CurrentAttr,
|
---|
40 | IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
|
---|
41 | )
|
---|
42 | {
|
---|
43 | UINT64 CurrentLevel;
|
---|
44 |
|
---|
45 | CurrentLevel = CurrentAttr & CCAttrTypeMask;
|
---|
46 |
|
---|
47 | switch (Attr) {
|
---|
48 | case CCAttrAmdSev:
|
---|
49 | //
|
---|
50 | // SEV is automatically enabled if SEV-ES or SEV-SNP is active.
|
---|
51 | //
|
---|
52 | return CurrentLevel >= CCAttrAmdSev;
|
---|
53 | case CCAttrAmdSevEs:
|
---|
54 | //
|
---|
55 | // SEV-ES is automatically enabled if SEV-SNP is active.
|
---|
56 | //
|
---|
57 | return CurrentLevel >= CCAttrAmdSevEs;
|
---|
58 | case CCAttrAmdSevSnp:
|
---|
59 | return CurrentLevel == CCAttrAmdSevSnp;
|
---|
60 | case CCAttrFeatureAmdSevEsDebugVirtualization:
|
---|
61 | return !!(CurrentAttr & CCAttrFeatureAmdSevEsDebugVirtualization);
|
---|
62 | default:
|
---|
63 | return FALSE;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | Check if the specified confidential computing attribute is active.
|
---|
69 |
|
---|
70 | @param[in] Attr The attribute to check.
|
---|
71 |
|
---|
72 | @retval TRUE The specified Attr is active.
|
---|
73 | @retval FALSE The specified Attr is not active.
|
---|
74 |
|
---|
75 | **/
|
---|
76 | STATIC
|
---|
77 | BOOLEAN
|
---|
78 | EFIAPI
|
---|
79 | ConfidentialComputingGuestHas (
|
---|
80 | IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
|
---|
81 | )
|
---|
82 | {
|
---|
83 | //
|
---|
84 | // Get the current CC attribute.
|
---|
85 | //
|
---|
86 | // We avoid reading the PCD on every check because this routine could be indirectly
|
---|
87 | // called during the virtual pointer conversion. And its not safe to access the
|
---|
88 | // PCDs during the virtual pointer conversion.
|
---|
89 | //
|
---|
90 | if (!mCurrentAttrRead) {
|
---|
91 | mCurrentAttr = PcdGet64 (PcdConfidentialComputingGuestAttr);
|
---|
92 | mCurrentAttrRead = TRUE;
|
---|
93 | }
|
---|
94 |
|
---|
95 | //
|
---|
96 | // If attr is for the AMD group then call AMD specific checks.
|
---|
97 | //
|
---|
98 | if (((RShiftU64 (mCurrentAttr, 8)) & 0xff) == 1) {
|
---|
99 | return AmdMemEncryptionAttrCheck (mCurrentAttr, Attr);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return (mCurrentAttr == Attr);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | Returns a boolean to indicate whether SEV-SNP is enabled.
|
---|
107 |
|
---|
108 | @retval TRUE SEV-SNP is enabled
|
---|
109 | @retval FALSE SEV-SNP is not enabled
|
---|
110 | **/
|
---|
111 | BOOLEAN
|
---|
112 | EFIAPI
|
---|
113 | MemEncryptSevSnpIsEnabled (
|
---|
114 | VOID
|
---|
115 | )
|
---|
116 | {
|
---|
117 | return ConfidentialComputingGuestHas (CCAttrAmdSevSnp);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | Returns a boolean to indicate whether SEV-ES is enabled.
|
---|
122 |
|
---|
123 | @retval TRUE SEV-ES is enabled
|
---|
124 | @retval FALSE SEV-ES is not enabled
|
---|
125 | **/
|
---|
126 | BOOLEAN
|
---|
127 | EFIAPI
|
---|
128 | MemEncryptSevEsIsEnabled (
|
---|
129 | VOID
|
---|
130 | )
|
---|
131 | {
|
---|
132 | return ConfidentialComputingGuestHas (CCAttrAmdSevEs);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | Returns a boolean to indicate whether SEV is enabled.
|
---|
137 |
|
---|
138 | @retval TRUE SEV is enabled
|
---|
139 | @retval FALSE SEV is not enabled
|
---|
140 | **/
|
---|
141 | BOOLEAN
|
---|
142 | EFIAPI
|
---|
143 | MemEncryptSevIsEnabled (
|
---|
144 | VOID
|
---|
145 | )
|
---|
146 | {
|
---|
147 | return ConfidentialComputingGuestHas (CCAttrAmdSev);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | Returns the SEV encryption mask.
|
---|
152 |
|
---|
153 | @return The SEV pagtable encryption mask
|
---|
154 | **/
|
---|
155 | UINT64
|
---|
156 | EFIAPI
|
---|
157 | MemEncryptSevGetEncryptionMask (
|
---|
158 | VOID
|
---|
159 | )
|
---|
160 | {
|
---|
161 | if (!mSevEncryptionMaskSaved) {
|
---|
162 | mSevEncryptionMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask);
|
---|
163 | mSevEncryptionMaskSaved = TRUE;
|
---|
164 | }
|
---|
165 |
|
---|
166 | return mSevEncryptionMask;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | Returns a boolean to indicate whether DebugVirtualization is enabled.
|
---|
171 |
|
---|
172 | @retval TRUE DebugVirtualization is enabled
|
---|
173 | @retval FALSE DebugVirtualization is not enabled
|
---|
174 | **/
|
---|
175 | BOOLEAN
|
---|
176 | EFIAPI
|
---|
177 | MemEncryptSevEsDebugVirtualizationIsEnabled (
|
---|
178 | VOID
|
---|
179 | )
|
---|
180 | {
|
---|
181 | return ConfidentialComputingGuestHas (CCAttrFeatureAmdSevEsDebugVirtualization);
|
---|
182 | }
|
---|