]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/AmdSevDxe/AmdSevDxe.c
OvmfPkg/VirtNorFlashDxe: map flash memory as uncacheable
[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
24e4ad75 23\r
67484aed
BS
24STATIC CONFIDENTIAL_COMPUTING_SNP_BLOB_LOCATION mSnpBootDxeTable = {\r
25 SIGNATURE_32 ('A', 'M', 'D', 'E'),\r
26 1,\r
27 0,\r
28 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfSnpSecretsBase),\r
29 FixedPcdGet32 (PcdOvmfSnpSecretsSize),\r
30 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfCpuidBase),\r
31 FixedPcdGet32 (PcdOvmfCpuidSize),\r
32};\r
33\r
24e4ad75
BS
34EFI_STATUS\r
35EFIAPI\r
36AmdSevDxeEntryPoint (\r
ac0a286f
MK
37 IN EFI_HANDLE ImageHandle,\r
38 IN EFI_SYSTEM_TABLE *SystemTable\r
24e4ad75
BS
39 )\r
40{\r
41 EFI_STATUS Status;\r
42 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;\r
43 UINTN NumEntries;\r
44 UINTN Index;\r
45\r
46 //\r
47 // Do nothing when SEV is not enabled\r
48 //\r
49 if (!MemEncryptSevIsEnabled ()) {\r
50 return EFI_UNSUPPORTED;\r
51 }\r
52\r
53 //\r
54 // Iterate through the GCD map and clear the C-bit from MMIO and NonExistent\r
c16d4e35
LE
55 // memory space. The NonExistent memory space will be used for mapping the\r
56 // MMIO space added later (eg PciRootBridge). By clearing both known MMIO and\r
24e4ad75
BS
57 // NonExistent memory space can gurantee that current and furture MMIO adds\r
58 // will have C-bit cleared.\r
59 //\r
60 Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);\r
61 if (!EFI_ERROR (Status)) {\r
62 for (Index = 0; Index < NumEntries; Index++) {\r
ac0a286f 63 CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r
24e4ad75
BS
64\r
65 Desc = &AllDescMap[Index];\r
ac0a286f
MK
66 if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) ||\r
67 (Desc->GcdMemoryType == EfiGcdMemoryTypeNonExistent))\r
68 {\r
c394fa4c 69 Status = MemEncryptSevClearMmioPageEncMask (\r
c16d4e35
LE
70 0,\r
71 Desc->BaseAddress,\r
c394fa4c 72 EFI_SIZE_TO_PAGES (Desc->Length)\r
c16d4e35 73 );\r
24e4ad75
BS
74 ASSERT_EFI_ERROR (Status);\r
75 }\r
76 }\r
77\r
78 FreePool (AllDescMap);\r
79 }\r
80\r
84cddd70
TL
81 //\r
82 // If PCI Express is enabled, the MMCONFIG area has been reserved, rather\r
83 // than marked as MMIO, and so the C-bit won't be cleared by the above walk\r
84 // through the GCD map. Check for the MMCONFIG area and clear the C-bit for\r
85 // the range.\r
86 //\r
87 if (PcdGet16 (PcdOvmfHostBridgePciDevId) == INTEL_Q35_MCH_DEVICE_ID) {\r
c394fa4c 88 Status = MemEncryptSevClearMmioPageEncMask (\r
84cddd70
TL
89 0,\r
90 FixedPcdGet64 (PcdPciExpressBaseAddress),\r
c394fa4c 91 EFI_SIZE_TO_PAGES (SIZE_256MB)\r
84cddd70
TL
92 );\r
93\r
94 ASSERT_EFI_ERROR (Status);\r
95 }\r
96\r
5e2e5647
LE
97 //\r
98 // When SMM is enabled, clear the C-bit from SMM Saved State Area\r
99 //\r
100 // NOTES: The SavedStateArea address cleared here is before SMBASE\r
101 // relocation. Currently, we do not clear the SavedStateArea address after\r
102 // SMBASE is relocated due to the following reasons:\r
103 //\r
104 // 1) Guest BIOS never access the relocated SavedStateArea.\r
105 //\r
106 // 2) The C-bit works on page-aligned address, but the SavedStateArea\r
107 // address is not a page-aligned. Theoretically, we could roundup the address\r
108 // and clear the C-bit of aligned address but looking carefully we found\r
109 // that some portion of the page contains code -- which will causes a bigger\r
110 // issues for SEV guest. When SEV is enabled, all the code must be encrypted\r
111 // otherwise hardware will cause trap.\r
112 //\r
113 // We restore the C-bit for this SMM Saved State Area after SMBASE relocation\r
114 // is completed (See OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c).\r
115 //\r
116 if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
ac0a286f
MK
117 UINTN MapPagesBase;\r
118 UINTN MapPagesCount;\r
5e2e5647
LE
119\r
120 Status = MemEncryptSevLocateInitialSmramSaveStateMapPages (\r
121 &MapPagesBase,\r
122 &MapPagesCount\r
123 );\r
124 ASSERT_EFI_ERROR (Status);\r
125\r
126 //\r
127 // Although these pages were set aside (i.e., allocated) by PlatformPei, we\r
128 // could be after a warm reboot from the OS. Don't leak any stale OS data\r
129 // to the hypervisor.\r
130 //\r
131 ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount));\r
132\r
133 Status = MemEncryptSevClearPageEncMask (\r
134 0, // Cr3BaseAddress -- use current CR3\r
135 MapPagesBase, // BaseAddress\r
adfa3327 136 MapPagesCount // NumPages\r
5e2e5647
LE
137 );\r
138 if (EFI_ERROR (Status)) {\r
ac0a286f
MK
139 DEBUG ((\r
140 DEBUG_ERROR,\r
141 "%a: MemEncryptSevClearPageEncMask(): %r\n",\r
142 __FUNCTION__,\r
143 Status\r
144 ));\r
5e2e5647
LE
145 ASSERT (FALSE);\r
146 CpuDeadLoop ();\r
147 }\r
148 }\r
149\r
67484aed
BS
150 //\r
151 // If its SEV-SNP active guest then install the CONFIDENTIAL_COMPUTING_SEV_SNP_BLOB.\r
152 // It contains the location for both the Secrets and CPUID page.\r
153 //\r
154 if (MemEncryptSevSnpIsEnabled ()) {\r
155 return gBS->InstallConfigurationTable (\r
156 &gConfidentialComputingSevSnpBlobGuid,\r
157 &mSnpBootDxeTable\r
158 );\r
159 }\r
160\r
24e4ad75
BS
161 return EFI_SUCCESS;\r
162}\r