]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/AmdSevDxe/AmdSevDxe.c
.devcontainer/devcontainer.json: Add devcontainer file
[mirror_edk2.git] / OvmfPkg / AmdSevDxe / AmdSevDxe.c
CommitLineData
24e4ad75
BS
1/** @file\r
2\r
3 AMD Sev Dxe driver. This driver is dispatched early in DXE, due to being list\r
c16d4e35
LE
4 in APRIORI. It clears C-bit from MMIO and NonExistent Memory space when SEV\r
5 is enabled.\r
24e4ad75 6\r
84cddd70 7 Copyright (c) 2017 - 2020, AMD Inc. All rights reserved.<BR>\r
24e4ad75 8\r
b26f0cf9 9 SPDX-License-Identifier: BSD-2-Clause-Patent\r
24e4ad75
BS
10\r
11**/\r
12\r
84cddd70 13#include <IndustryStandard/Q35MchIch9.h>\r
5e2e5647
LE
14#include <Library/BaseLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
c6073a0e 16#include <Library/DebugLib.h>\r
24e4ad75
BS
17#include <Library/DxeServicesTableLib.h>\r
18#include <Library/MemEncryptSevLib.h>\r
c6073a0e 19#include <Library/MemoryAllocationLib.h>\r
67484aed
BS
20#include <Library/UefiBootServicesTableLib.h>\r
21#include <Guid/ConfidentialComputingSevSnpBlob.h>\r
5e2e5647 22#include <Library/PcdLib.h>\r
a00e2e55 23#include <Pi/PrePiDxeCis.h>\r
466d8f65 24#include <Protocol/SevMemoryAcceptance.h>\r
59aa48bb 25#include <Protocol/MemoryAccept.h>\r
24e4ad75 26\r
67484aed
BS
27STATIC CONFIDENTIAL_COMPUTING_SNP_BLOB_LOCATION mSnpBootDxeTable = {\r
28 SIGNATURE_32 ('A', 'M', 'D', 'E'),\r
29 1,\r
30 0,\r
31 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfSnpSecretsBase),\r
32 FixedPcdGet32 (PcdOvmfSnpSecretsSize),\r
33 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfCpuidBase),\r
34 FixedPcdGet32 (PcdOvmfCpuidSize),\r
35};\r
36\r
59aa48bb
SW
37STATIC EFI_HANDLE mAmdSevDxeHandle = NULL;\r
38\r
a00e2e55
DG
39STATIC BOOLEAN mAcceptAllMemoryAtEBS = TRUE;\r
40\r
41STATIC EFI_EVENT mAcceptAllMemoryEvent = NULL;\r
42\r
59aa48bb
SW
43#define IS_ALIGNED(x, y) ((((x) & ((y) - 1)) == 0))\r
44\r
45STATIC\r
46EFI_STATUS\r
47EFIAPI\r
48AmdSevMemoryAccept (\r
49 IN EDKII_MEMORY_ACCEPT_PROTOCOL *This,\r
50 IN EFI_PHYSICAL_ADDRESS StartAddress,\r
51 IN UINTN Size\r
52 )\r
53{\r
54 //\r
55 // The StartAddress must be page-aligned, and the Size must be a positive\r
56 // multiple of SIZE_4KB. Use an assert instead of returning an erros since\r
57 // this is an EDK2-internal protocol.\r
58 //\r
59 ASSERT (IS_ALIGNED (StartAddress, SIZE_4KB));\r
60 ASSERT (IS_ALIGNED (Size, SIZE_4KB));\r
61 ASSERT (Size != 0);\r
62\r
63 MemEncryptSevSnpPreValidateSystemRam (\r
64 StartAddress,\r
65 EFI_SIZE_TO_PAGES (Size)\r
66 );\r
67\r
68 return EFI_SUCCESS;\r
69}\r
70\r
a00e2e55
DG
71STATIC\r
72EFI_STATUS\r
73AcceptAllMemory (\r
74 VOID\r
75 )\r
76{\r
77 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;\r
78 UINTN NumEntries;\r
79 UINTN Index;\r
80 EFI_STATUS Status;\r
81\r
82 DEBUG ((DEBUG_INFO, "Accepting all memory\n"));\r
83\r
84 /*\r
85 * Get a copy of the memory space map to iterate over while\r
86 * changing the map.\r
87 */\r
88 Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);\r
89 if (EFI_ERROR (Status)) {\r
90 return Status;\r
91 }\r
92\r
93 for (Index = 0; Index < NumEntries; Index++) {\r
94 CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r
95\r
96 Desc = &AllDescMap[Index];\r
97 if (Desc->GcdMemoryType != EFI_GCD_MEMORY_TYPE_UNACCEPTED) {\r
98 continue;\r
99 }\r
100\r
101 Status = AmdSevMemoryAccept (\r
102 NULL,\r
103 Desc->BaseAddress,\r
104 Desc->Length\r
105 );\r
106 if (EFI_ERROR (Status)) {\r
107 break;\r
108 }\r
109\r
110 Status = gDS->RemoveMemorySpace (Desc->BaseAddress, Desc->Length);\r
111 if (EFI_ERROR (Status)) {\r
112 break;\r
113 }\r
114\r
115 Status = gDS->AddMemorySpace (\r
116 EfiGcdMemoryTypeSystemMemory,\r
117 Desc->BaseAddress,\r
118 Desc->Length,\r
119 EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_XP | EFI_MEMORY_RO | EFI_MEMORY_RP\r
120 );\r
121 if (EFI_ERROR (Status)) {\r
122 break;\r
123 }\r
124 }\r
125\r
126 gBS->FreePool (AllDescMap);\r
127 return Status;\r
128}\r
129\r
130VOID\r
131EFIAPI\r
132ResolveUnacceptedMemory (\r
133 IN EFI_EVENT Event,\r
134 IN VOID *Context\r
135 )\r
136{\r
137 EFI_STATUS Status;\r
138\r
139 if (!mAcceptAllMemoryAtEBS) {\r
140 return;\r
141 }\r
142\r
143 Status = AcceptAllMemory ();\r
144 ASSERT_EFI_ERROR (Status);\r
145}\r
146\r
466d8f65
DG
147STATIC\r
148EFI_STATUS\r
149EFIAPI\r
150AllowUnacceptedMemory (\r
151 IN OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL *This\r
152 )\r
153{\r
154 mAcceptAllMemoryAtEBS = FALSE;\r
155 return EFI_SUCCESS;\r
156}\r
157\r
158STATIC\r
159OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL\r
160 mMemoryAcceptanceProtocol = { AllowUnacceptedMemory };\r
161\r
59aa48bb
SW
162STATIC EDKII_MEMORY_ACCEPT_PROTOCOL mMemoryAcceptProtocol = {\r
163 AmdSevMemoryAccept\r
164};\r
165\r
24e4ad75
BS
166EFI_STATUS\r
167EFIAPI\r
168AmdSevDxeEntryPoint (\r
ac0a286f
MK
169 IN EFI_HANDLE ImageHandle,\r
170 IN EFI_SYSTEM_TABLE *SystemTable\r
24e4ad75
BS
171 )\r
172{\r
173 EFI_STATUS Status;\r
174 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;\r
175 UINTN NumEntries;\r
176 UINTN Index;\r
177\r
178 //\r
179 // Do nothing when SEV is not enabled\r
180 //\r
181 if (!MemEncryptSevIsEnabled ()) {\r
182 return EFI_UNSUPPORTED;\r
183 }\r
184\r
185 //\r
186 // Iterate through the GCD map and clear the C-bit from MMIO and NonExistent\r
c16d4e35
LE
187 // memory space. The NonExistent memory space will be used for mapping the\r
188 // MMIO space added later (eg PciRootBridge). By clearing both known MMIO and\r
24e4ad75
BS
189 // NonExistent memory space can gurantee that current and furture MMIO adds\r
190 // will have C-bit cleared.\r
191 //\r
192 Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);\r
193 if (!EFI_ERROR (Status)) {\r
194 for (Index = 0; Index < NumEntries; Index++) {\r
ac0a286f 195 CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r
24e4ad75
BS
196\r
197 Desc = &AllDescMap[Index];\r
ac0a286f
MK
198 if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) ||\r
199 (Desc->GcdMemoryType == EfiGcdMemoryTypeNonExistent))\r
200 {\r
c394fa4c 201 Status = MemEncryptSevClearMmioPageEncMask (\r
c16d4e35
LE
202 0,\r
203 Desc->BaseAddress,\r
c394fa4c 204 EFI_SIZE_TO_PAGES (Desc->Length)\r
c16d4e35 205 );\r
24e4ad75
BS
206 ASSERT_EFI_ERROR (Status);\r
207 }\r
208 }\r
209\r
210 FreePool (AllDescMap);\r
211 }\r
212\r
84cddd70
TL
213 //\r
214 // If PCI Express is enabled, the MMCONFIG area has been reserved, rather\r
215 // than marked as MMIO, and so the C-bit won't be cleared by the above walk\r
216 // through the GCD map. Check for the MMCONFIG area and clear the C-bit for\r
217 // the range.\r
218 //\r
219 if (PcdGet16 (PcdOvmfHostBridgePciDevId) == INTEL_Q35_MCH_DEVICE_ID) {\r
c394fa4c 220 Status = MemEncryptSevClearMmioPageEncMask (\r
84cddd70
TL
221 0,\r
222 FixedPcdGet64 (PcdPciExpressBaseAddress),\r
c394fa4c 223 EFI_SIZE_TO_PAGES (SIZE_256MB)\r
84cddd70
TL
224 );\r
225\r
226 ASSERT_EFI_ERROR (Status);\r
227 }\r
228\r
5e2e5647
LE
229 //\r
230 // When SMM is enabled, clear the C-bit from SMM Saved State Area\r
231 //\r
232 // NOTES: The SavedStateArea address cleared here is before SMBASE\r
233 // relocation. Currently, we do not clear the SavedStateArea address after\r
234 // SMBASE is relocated due to the following reasons:\r
235 //\r
236 // 1) Guest BIOS never access the relocated SavedStateArea.\r
237 //\r
238 // 2) The C-bit works on page-aligned address, but the SavedStateArea\r
239 // address is not a page-aligned. Theoretically, we could roundup the address\r
240 // and clear the C-bit of aligned address but looking carefully we found\r
241 // that some portion of the page contains code -- which will causes a bigger\r
242 // issues for SEV guest. When SEV is enabled, all the code must be encrypted\r
243 // otherwise hardware will cause trap.\r
244 //\r
245 // We restore the C-bit for this SMM Saved State Area after SMBASE relocation\r
246 // is completed (See OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c).\r
247 //\r
248 if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
ac0a286f
MK
249 UINTN MapPagesBase;\r
250 UINTN MapPagesCount;\r
5e2e5647
LE
251\r
252 Status = MemEncryptSevLocateInitialSmramSaveStateMapPages (\r
253 &MapPagesBase,\r
254 &MapPagesCount\r
255 );\r
256 ASSERT_EFI_ERROR (Status);\r
257\r
258 //\r
259 // Although these pages were set aside (i.e., allocated) by PlatformPei, we\r
260 // could be after a warm reboot from the OS. Don't leak any stale OS data\r
261 // to the hypervisor.\r
262 //\r
263 ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount));\r
264\r
265 Status = MemEncryptSevClearPageEncMask (\r
266 0, // Cr3BaseAddress -- use current CR3\r
267 MapPagesBase, // BaseAddress\r
adfa3327 268 MapPagesCount // NumPages\r
5e2e5647
LE
269 );\r
270 if (EFI_ERROR (Status)) {\r
ac0a286f
MK
271 DEBUG ((\r
272 DEBUG_ERROR,\r
273 "%a: MemEncryptSevClearPageEncMask(): %r\n",\r
274 __FUNCTION__,\r
275 Status\r
276 ));\r
5e2e5647
LE
277 ASSERT (FALSE);\r
278 CpuDeadLoop ();\r
279 }\r
280 }\r
281\r
67484aed 282 if (MemEncryptSevSnpIsEnabled ()) {\r
59aa48bb
SW
283 //\r
284 // Memory acceptance began being required in SEV-SNP, so install the\r
285 // memory accept protocol implementation for a SEV-SNP active guest.\r
286 //\r
466d8f65 287 Status = gBS->InstallMultipleProtocolInterfaces (\r
59aa48bb
SW
288 &mAmdSevDxeHandle,\r
289 &gEdkiiMemoryAcceptProtocolGuid,\r
466d8f65
DG
290 &mMemoryAcceptProtocol,\r
291 &gOvmfSevMemoryAcceptanceProtocolGuid,\r
292 &mMemoryAcceptanceProtocol,\r
293 NULL\r
59aa48bb
SW
294 );\r
295 ASSERT_EFI_ERROR (Status);\r
296\r
a00e2e55
DG
297 // SEV-SNP support does not automatically imply unaccepted memory support,\r
298 // so make ExitBootServices accept all unaccepted memory if support is\r
299 // not communicated.\r
300 Status = gBS->CreateEventEx (\r
301 EVT_NOTIFY_SIGNAL,\r
302 TPL_CALLBACK,\r
303 ResolveUnacceptedMemory,\r
304 NULL,\r
305 &gEfiEventBeforeExitBootServicesGuid,\r
306 &mAcceptAllMemoryEvent\r
307 );\r
308\r
309 if (EFI_ERROR (Status)) {\r
310 DEBUG ((DEBUG_ERROR, "AllowUnacceptedMemory event creation for EventBeforeExitBootServices failed.\n"));\r
311 }\r
312\r
59aa48bb
SW
313 //\r
314 // If its SEV-SNP active guest then install the CONFIDENTIAL_COMPUTING_SEV_SNP_BLOB.\r
315 // It contains the location for both the Secrets and CPUID page.\r
316 //\r
67484aed
BS
317 return gBS->InstallConfigurationTable (\r
318 &gConfidentialComputingSevSnpBlobGuid,\r
319 &mSnpBootDxeTable\r
320 );\r
321 }\r
322\r
24e4ad75
BS
323 return EFI_SUCCESS;\r
324}\r