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