]>
Commit | Line | Data |
---|---|---|
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 |
22 | EFI_STATUS\r | |
23 | EFIAPI\r | |
24 | AmdSevDxeEntryPoint (\r | |
ac0a286f MK |
25 | IN EFI_HANDLE ImageHandle,\r |
26 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
24e4ad75 BS |
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 | |
ac0a286f | 51 | CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r |
24e4ad75 BS |
52 | \r |
53 | Desc = &AllDescMap[Index];\r | |
ac0a286f MK |
54 | if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) ||\r |
55 | (Desc->GcdMemoryType == EfiGcdMemoryTypeNonExistent))\r | |
56 | {\r | |
c394fa4c | 57 | Status = MemEncryptSevClearMmioPageEncMask (\r |
c16d4e35 LE |
58 | 0,\r |
59 | Desc->BaseAddress,\r | |
c394fa4c | 60 | EFI_SIZE_TO_PAGES (Desc->Length)\r |
c16d4e35 | 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 | |
c394fa4c | 76 | Status = MemEncryptSevClearMmioPageEncMask (\r |
84cddd70 TL |
77 | 0,\r |
78 | FixedPcdGet64 (PcdPciExpressBaseAddress),\r | |
c394fa4c | 79 | EFI_SIZE_TO_PAGES (SIZE_256MB)\r |
84cddd70 TL |
80 | );\r |
81 | \r | |
82 | ASSERT_EFI_ERROR (Status);\r | |
83 | }\r | |
84 | \r | |
5e2e5647 LE |
85 | //\r |
86 | // When SMM is enabled, clear the C-bit from SMM Saved State Area\r | |
87 | //\r | |
88 | // NOTES: The SavedStateArea address cleared here is before SMBASE\r | |
89 | // relocation. Currently, we do not clear the SavedStateArea address after\r | |
90 | // SMBASE is relocated due to the following reasons:\r | |
91 | //\r | |
92 | // 1) Guest BIOS never access the relocated SavedStateArea.\r | |
93 | //\r | |
94 | // 2) The C-bit works on page-aligned address, but the SavedStateArea\r | |
95 | // address is not a page-aligned. Theoretically, we could roundup the address\r | |
96 | // and clear the C-bit of aligned address but looking carefully we found\r | |
97 | // that some portion of the page contains code -- which will causes a bigger\r | |
98 | // issues for SEV guest. When SEV is enabled, all the code must be encrypted\r | |
99 | // otherwise hardware will cause trap.\r | |
100 | //\r | |
101 | // We restore the C-bit for this SMM Saved State Area after SMBASE relocation\r | |
102 | // is completed (See OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c).\r | |
103 | //\r | |
104 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
ac0a286f MK |
105 | UINTN MapPagesBase;\r |
106 | UINTN MapPagesCount;\r | |
5e2e5647 LE |
107 | \r |
108 | Status = MemEncryptSevLocateInitialSmramSaveStateMapPages (\r | |
109 | &MapPagesBase,\r | |
110 | &MapPagesCount\r | |
111 | );\r | |
112 | ASSERT_EFI_ERROR (Status);\r | |
113 | \r | |
114 | //\r | |
115 | // Although these pages were set aside (i.e., allocated) by PlatformPei, we\r | |
116 | // could be after a warm reboot from the OS. Don't leak any stale OS data\r | |
117 | // to the hypervisor.\r | |
118 | //\r | |
119 | ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount));\r | |
120 | \r | |
121 | Status = MemEncryptSevClearPageEncMask (\r | |
122 | 0, // Cr3BaseAddress -- use current CR3\r | |
123 | MapPagesBase, // BaseAddress\r | |
adfa3327 | 124 | MapPagesCount // NumPages\r |
5e2e5647 LE |
125 | );\r |
126 | if (EFI_ERROR (Status)) {\r | |
ac0a286f MK |
127 | DEBUG ((\r |
128 | DEBUG_ERROR,\r | |
129 | "%a: MemEncryptSevClearPageEncMask(): %r\n",\r | |
130 | __FUNCTION__,\r | |
131 | Status\r | |
132 | ));\r | |
5e2e5647 LE |
133 | ASSERT (FALSE);\r |
134 | CpuDeadLoop ();\r | |
135 | }\r | |
136 | }\r | |
137 | \r | |
24e4ad75 BS |
138 | return EFI_SUCCESS;\r |
139 | }\r |