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