]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformPei/MemTypeInfo.c
OvmfPkg/PlatformPei: rewrite MemTypeInfo HOB production logic
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemTypeInfo.c
1 /** @file
2 Produce the memory type information HOB.
3
4 Copyright (C) 2017-2020, Red Hat, Inc.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include <Guid/MemoryTypeInformation.h>
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/HobLib.h>
13 #include <Library/PcdLib.h>
14 #include <Library/PeiServicesLib.h>
15 #include <Ppi/ReadOnlyVariable2.h>
16 #include <Uefi/UefiMultiPhase.h>
17
18 #include "Platform.h"
19
20 //
21 // The NumberOfPages values below are ad-hoc. They are updated sporadically at
22 // best (please refer to git-blame for past updates). The values capture a set
23 // of BIN hints that made sense at a particular time, for some (now likely
24 // unknown) workloads / boot paths.
25 //
26 STATIC EFI_MEMORY_TYPE_INFORMATION mMemoryTypeInformation[] = {
27 { EfiACPIMemoryNVS, 0x004 },
28 { EfiACPIReclaimMemory, 0x008 },
29 { EfiReservedMemoryType, 0x004 },
30 { EfiRuntimeServicesData, 0x024 },
31 { EfiRuntimeServicesCode, 0x030 },
32 { EfiMaxMemoryType, 0x000 }
33 };
34
35 STATIC
36 VOID
37 BuildMemTypeInfoHob (
38 VOID
39 )
40 {
41 BuildGuidDataHob (
42 &gEfiMemoryTypeInformationGuid,
43 mMemoryTypeInformation,
44 sizeof mMemoryTypeInformation
45 );
46 }
47
48 /**
49 Refresh the mMemoryTypeInformation array (which we'll turn into the
50 MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.
51
52 Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does
53 so *transparently*. Instead, we consider the UEFI variable as a list of
54 hints, for updating our HOB defaults:
55
56 - Record types not covered in mMemoryTypeInformation are ignored. In
57 particular, this hides record types from the UEFI variable that may lead to
58 reboots without benefiting SMM security, such as EfiBootServicesData.
59
60 - Records that would lower the defaults in mMemoryTypeInformation are also
61 ignored.
62
63 @param[in] ReadOnlyVariable2 The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for
64 retrieving the MemoryTypeInformation UEFI
65 variable.
66 **/
67 STATIC
68 VOID
69 RefreshMemTypeInfo (
70 IN EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2
71 )
72 {
73 UINTN DataSize;
74 EFI_MEMORY_TYPE_INFORMATION Entries[EfiMaxMemoryType + 1];
75 EFI_STATUS Status;
76 UINTN NumEntries;
77 UINTN HobRecordIdx;
78
79 //
80 // Read the MemoryTypeInformation UEFI variable from the
81 // gEfiMemoryTypeInformationGuid namespace.
82 //
83 DataSize = sizeof Entries;
84 Status = ReadOnlyVariable2->GetVariable (
85 ReadOnlyVariable2,
86 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
87 &gEfiMemoryTypeInformationGuid,
88 NULL,
89 &DataSize,
90 Entries
91 );
92 if (EFI_ERROR (Status)) {
93 //
94 // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for
95 // udpating mMemoryTypeInformation.
96 //
97 // If the UEFI variable exists but Entries is too small to hold it
98 // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.
99 // That's because Entries has room for every distinct EFI_MEMORY_TYPE,
100 // including the terminator record with EfiMaxMemoryType. Thus, we can't
101 // use the UEFI variable for updating mMemoryTypeInformation.
102 //
103 // If the UEFI variable couldn't be read for some other reason, we
104 // similarly can't use it for udpating mMemoryTypeInformation.
105 //
106 DEBUG ((DEBUG_ERROR, "%a: GetVariable(): %r\n", __FUNCTION__, Status));
107 return;
108 }
109
110 //
111 // Sanity-check the UEFI variable size against the record size.
112 //
113 if (DataSize % sizeof Entries[0] != 0) {
114 DEBUG ((DEBUG_ERROR, "%a: invalid UEFI variable size %Lu\n", __FUNCTION__,
115 (UINT64)DataSize));
116 return;
117 }
118 NumEntries = DataSize / sizeof Entries[0];
119
120 //
121 // For each record in mMemoryTypeInformation, except the terminator record,
122 // look up the first match (if any) in the UEFI variable, based on the memory
123 // type.
124 //
125 for (HobRecordIdx = 0;
126 HobRecordIdx < ARRAY_SIZE (mMemoryTypeInformation) - 1;
127 HobRecordIdx++) {
128 EFI_MEMORY_TYPE_INFORMATION *HobRecord;
129 UINTN Idx;
130 EFI_MEMORY_TYPE_INFORMATION *VariableRecord;
131
132 HobRecord = &mMemoryTypeInformation[HobRecordIdx];
133
134 for (Idx = 0; Idx < NumEntries; Idx++) {
135 VariableRecord = &Entries[Idx];
136
137 if (VariableRecord->Type == HobRecord->Type) {
138 break;
139 }
140 }
141
142 //
143 // If there is a match, allow the UEFI variable to increase NumberOfPages.
144 //
145 if (Idx < NumEntries &&
146 HobRecord->NumberOfPages < VariableRecord->NumberOfPages) {
147 DEBUG ((DEBUG_VERBOSE, "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",
148 __FUNCTION__, HobRecord->Type, HobRecord->NumberOfPages,
149 VariableRecord->NumberOfPages));
150
151 HobRecord->NumberOfPages = VariableRecord->NumberOfPages;
152 }
153 }
154 }
155
156 /**
157 Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes
158 available.
159
160 @param[in] PeiServices Indirect reference to the PEI Services Table.
161 @param[in] NotifyDescriptor Address of the notification descriptor data
162 structure.
163 @param[in] Ppi Address of the PPI that was installed.
164
165 @return Status of the notification. The status code returned from this
166 function is ignored.
167 **/
168 STATIC
169 EFI_STATUS
170 EFIAPI
171 OnReadOnlyVariable2Available (
172 IN EFI_PEI_SERVICES **PeiServices,
173 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
174 IN VOID *Ppi
175 )
176 {
177 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
178
179 RefreshMemTypeInfo (Ppi);
180 BuildMemTypeInfoHob ();
181 return EFI_SUCCESS;
182 }
183
184 //
185 // Notification object for registering the callback, for when
186 // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.
187 //
188 STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {
189 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |
190 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), // Flags
191 &gEfiPeiReadOnlyVariable2PpiGuid, // Guid
192 OnReadOnlyVariable2Available // Notify
193 };
194
195 VOID
196 MemTypeInfoInitialization (
197 VOID
198 )
199 {
200 EFI_STATUS Status;
201
202 if (!FeaturePcdGet (PcdSmmSmramRequire)) {
203 //
204 // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install
205 // the default memory type information HOB right away.
206 //
207 BuildMemTypeInfoHob ();
208 return;
209 }
210
211 Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);
212 if (EFI_ERROR (Status)) {
213 DEBUG ((DEBUG_ERROR, "%a: failed to set up R/O Variable 2 callback: %r\n",
214 __FUNCTION__, Status));
215 ASSERT (FALSE);
216 CpuDeadLoop ();
217 }
218 }