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