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