]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
... / ...
CommitLineData
1/** @file\r
2 EFI PEI Core dispatch services\r
3\r
4Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
6SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include "PeiMain.h"\r
11\r
12/**\r
13\r
14 Discover all PEIMs and optional Apriori file in one FV. There is at most one\r
15 Apriori file in one FV.\r
16\r
17\r
18 @param Private Pointer to the private data passed in from caller\r
19 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
20\r
21**/\r
22VOID\r
23DiscoverPeimsAndOrderWithApriori (\r
24 IN PEI_CORE_INSTANCE *Private,\r
25 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
26 )\r
27{\r
28 EFI_STATUS Status;\r
29 EFI_PEI_FILE_HANDLE FileHandle;\r
30 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
31 EFI_GUID *Apriori;\r
32 UINTN Index;\r
33 UINTN Index2;\r
34 UINTN PeimIndex;\r
35 UINTN PeimCount;\r
36 EFI_GUID *Guid;\r
37 EFI_PEI_FILE_HANDLE *TempFileHandles;\r
38 EFI_GUID *TempFileGuid;\r
39 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
40 EFI_FV_FILE_INFO FileInfo;\r
41\r
42 FvPpi = CoreFileHandle->FvPpi;\r
43\r
44 //\r
45 // Walk the FV and find all the PEIMs and the Apriori file.\r
46 //\r
47 AprioriFileHandle = NULL;\r
48 Private->CurrentFvFileHandles = NULL;\r
49 Guid = NULL;\r
50\r
51 //\r
52 // If the current FV has been scanned, directly get its cached records.\r
53 //\r
54 if (CoreFileHandle->ScanFv) {\r
55 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
56 return;\r
57 }\r
58\r
59 TempFileHandles = Private->TempFileHandles;\r
60 TempFileGuid = Private->TempFileGuid;\r
61\r
62 //\r
63 // Go ahead to scan this FV, get PeimCount and cache FileHandles within it to TempFileHandles.\r
64 //\r
65 PeimCount = 0;\r
66 FileHandle = NULL;\r
67 do {\r
68 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
69 if (!EFI_ERROR (Status)) {\r
70 if (PeimCount >= Private->TempPeimCount) {\r
71 //\r
72 // Run out of room, grow the buffer.\r
73 //\r
74 TempFileHandles = AllocatePool (\r
75 sizeof (EFI_PEI_FILE_HANDLE) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
76 ASSERT (TempFileHandles != NULL);\r
77 CopyMem (\r
78 TempFileHandles,\r
79 Private->TempFileHandles,\r
80 sizeof (EFI_PEI_FILE_HANDLE) * Private->TempPeimCount\r
81 );\r
82 Private->TempFileHandles = TempFileHandles;\r
83 TempFileGuid = AllocatePool (\r
84 sizeof (EFI_GUID) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
85 ASSERT (TempFileGuid != NULL);\r
86 CopyMem (\r
87 TempFileGuid,\r
88 Private->TempFileGuid,\r
89 sizeof (EFI_GUID) * Private->TempPeimCount\r
90 );\r
91 Private->TempFileGuid = TempFileGuid;\r
92 Private->TempPeimCount = Private->TempPeimCount + TEMP_FILE_GROWTH_STEP;\r
93 }\r
94\r
95 TempFileHandles[PeimCount++] = FileHandle;\r
96 }\r
97 } while (!EFI_ERROR (Status));\r
98\r
99 DEBUG ((\r
100 DEBUG_INFO,\r
101 "%a(): Found 0x%x PEI FFS files in the %dth FV\n",\r
102 __FUNCTION__,\r
103 PeimCount,\r
104 Private->CurrentPeimFvCount\r
105 ));\r
106\r
107 if (PeimCount == 0) {\r
108 //\r
109 // No PEIM FFS file is found, set ScanFv flag and return.\r
110 //\r
111 CoreFileHandle->ScanFv = TRUE;\r
112 return;\r
113 }\r
114\r
115 //\r
116 // Record PeimCount, allocate buffer for PeimState and FvFileHandles.\r
117 //\r
118 CoreFileHandle->PeimCount = PeimCount;\r
119 CoreFileHandle->PeimState = AllocateZeroPool (sizeof (UINT8) * PeimCount);\r
120 ASSERT (CoreFileHandle->PeimState != NULL);\r
121 CoreFileHandle->FvFileHandles = AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
122 ASSERT (CoreFileHandle->FvFileHandles != NULL);\r
123\r
124 //\r
125 // Get Apriori File handle\r
126 //\r
127 Private->AprioriCount = 0;\r
128 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
129 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
130 //\r
131 // Read the Apriori file\r
132 //\r
133 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
134 if (!EFI_ERROR (Status)) {\r
135 //\r
136 // Calculate the number of PEIMs in the Apriori file\r
137 //\r
138 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
139 ASSERT_EFI_ERROR (Status);\r
140 Private->AprioriCount = FileInfo.BufferSize;\r
141 if (IS_SECTION2 (FileInfo.Buffer)) {\r
142 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER2);\r
143 } else {\r
144 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);\r
145 }\r
146 Private->AprioriCount /= sizeof (EFI_GUID);\r
147\r
148 for (Index = 0; Index < PeimCount; Index++) {\r
149 //\r
150 // Make an array of file name GUIDs that matches the FileHandle array so we can convert\r
151 // quickly from file name to file handle\r
152 //\r
153 Status = FvPpi->GetFileInfo (FvPpi, TempFileHandles[Index], &FileInfo);\r
154 ASSERT_EFI_ERROR (Status);\r
155 CopyMem (&TempFileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
156 }\r
157\r
158 //\r
159 // Walk through TempFileGuid array to find out who is invalid PEIM GUID in Apriori file.\r
160 // Add available PEIMs in Apriori file into FvFileHandles array.\r
161 //\r
162 Index = 0;\r
163 for (Index2 = 0; Index2 < Private->AprioriCount; Index2++) {\r
164 Guid = ScanGuid (TempFileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2]);\r
165 if (Guid != NULL) {\r
166 PeimIndex = ((UINTN)Guid - (UINTN)&TempFileGuid[0])/sizeof (EFI_GUID);\r
167 CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[PeimIndex];\r
168\r
169 //\r
170 // Since we have copied the file handle we can remove it from this list.\r
171 //\r
172 TempFileHandles[PeimIndex] = NULL;\r
173 }\r
174 }\r
175\r
176 //\r
177 // Update valid AprioriCount\r
178 //\r
179 Private->AprioriCount = Index;\r
180\r
181 //\r
182 // Add in any PEIMs not in the Apriori file\r
183 //\r
184 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
185 if (TempFileHandles[Index2] != NULL) {\r
186 CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[Index2];\r
187 TempFileHandles[Index2] = NULL;\r
188 }\r
189 }\r
190 ASSERT (Index == PeimCount);\r
191 }\r
192 } else {\r
193 CopyMem (CoreFileHandle->FvFileHandles, TempFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
194 }\r
195\r
196 //\r
197 // The current FV File Handles have been cached. So that we don't have to scan the FV again.\r
198 // Instead, we can retrieve the file handles within this FV from cached records.\r
199 //\r
200 CoreFileHandle->ScanFv = TRUE;\r
201 Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
202}\r
203\r
204//\r
205// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
206// This part of memory still need reserved on the very top of memory so that the DXE Core could\r
207// use these memory for data initialization. This macro should be sync with the same marco\r
208// defined in DXE Core.\r
209//\r
210#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
211/**\r
212 This function is to test if the memory range described in resource HOB is available or not.\r
213\r
214 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the\r
215 memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is\r
216 available or not.\r
217\r
218 @param PrivateData Pointer to the private data passed in from caller\r
219 @param ResourceHob Pointer to a resource HOB which described the memory range described by the input resource HOB\r
220**/\r
221BOOLEAN\r
222PeiLoadFixAddressIsMemoryRangeAvailable (\r
223 IN PEI_CORE_INSTANCE *PrivateData,\r
224 IN EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob\r
225 )\r
226{\r
227 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
228 BOOLEAN IsAvailable;\r
229 EFI_PEI_HOB_POINTERS Hob;\r
230\r
231 IsAvailable = TRUE;\r
232 if (PrivateData == NULL || ResourceHob == NULL) {\r
233 return FALSE;\r
234 }\r
235 //\r
236 // test if the memory range describe in the HOB is already allocated.\r
237 //\r
238 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
239 //\r
240 // See if this is a memory allocation HOB\r
241 //\r
242 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
243 MemoryHob = Hob.MemoryAllocation;\r
244 if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart &&\r
245 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {\r
246 IsAvailable = FALSE;\r
247 break;\r
248 }\r
249 }\r
250 }\r
251\r
252 return IsAvailable;\r
253\r
254}\r
255/**\r
256 Hook function for Loading Module at Fixed Address feature\r
257\r
258 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
259 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When\r
260 feature is configured as Load Modules at Fixed Offset, the function is to find the top address which is TOLM-TSEG in general.\r
261 And also the function will re-install PEI memory.\r
262\r
263 @param PrivateData Pointer to the private data passed in from caller\r
264\r
265**/\r
266VOID\r
267PeiLoadFixAddressHook(\r
268 IN PEI_CORE_INSTANCE *PrivateData\r
269 )\r
270{\r
271 EFI_PHYSICAL_ADDRESS TopLoadingAddress;\r
272 UINT64 PeiMemorySize;\r
273 UINT64 TotalReservedMemorySize;\r
274 UINT64 MemoryRangeEnd;\r
275 EFI_PHYSICAL_ADDRESS HighAddress;\r
276 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
277 EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob;\r
278 EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob;\r
279 EFI_PEI_HOB_POINTERS CurrentHob;\r
280 EFI_PEI_HOB_POINTERS Hob;\r
281 EFI_PEI_HOB_POINTERS NextHob;\r
282 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
283 //\r
284 // Initialize Local Variables\r
285 //\r
286 CurrentResourceHob = NULL;\r
287 ResourceHob = NULL;\r
288 NextResourceHob = NULL;\r
289 HighAddress = 0;\r
290 TopLoadingAddress = 0;\r
291 MemoryRangeEnd = 0;\r
292 CurrentHob.Raw = PrivateData->HobList.Raw;\r
293 PeiMemorySize = PrivateData->PhysicalMemoryLength;\r
294 //\r
295 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE\r
296 // then RuntimeCodePage range and Boot time code range.\r
297 //\r
298 TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));\r
299 TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;\r
300 //\r
301 // PEI memory range lies below the top reserved memory\r
302 //\r
303 TotalReservedMemorySize += PeiMemorySize;\r
304\r
305 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));\r
306 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));\r
307 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));\r
308 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));\r
309 //\r
310 // Loop through the system memory typed HOB to merge the adjacent memory range\r
311 //\r
312 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
313 //\r
314 // See if this is a resource descriptor HOB\r
315 //\r
316 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
317\r
318 ResourceHob = Hob.ResourceDescriptor;\r
319 //\r
320 // If range described in this HOB is not system memory or higher than MAX_ADDRESS, ignored.\r
321 //\r
322 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY ||\r
323 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {\r
324 continue;\r
325 }\r
326\r
327 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
328 if (NextHob.Raw == Hob.Raw){\r
329 continue;\r
330 }\r
331 //\r
332 // See if this is a resource descriptor HOB\r
333 //\r
334 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
335\r
336 NextResourceHob = NextHob.ResourceDescriptor;\r
337 //\r
338 // test if range described in this NextResourceHob is system memory and have the same attribute.\r
339 // Note: Here is a assumption that system memory should always be healthy even without test.\r
340 //\r
341 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
342 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
343\r
344 //\r
345 // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
346 //\r
347 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart &&\r
348 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)||\r
349 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
350 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
351\r
352 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
353 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
354\r
355 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ?\r
356 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
357\r
358\r
359 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
360\r
361 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);\r
362 //\r
363 // Delete the NextResourceHob by marking it as unused.\r
364 //\r
365 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;\r
366\r
367 }\r
368 }\r
369 }\r
370 }\r
371 }\r
372 }\r
373 //\r
374 // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe\r
375 // the allocated memory range\r
376 //\r
377 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
378 //\r
379 // See if this is a memory allocation HOB\r
380 //\r
381 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
382 MemoryHob = Hob.MemoryAllocation;\r
383 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
384 //\r
385 // See if this is a resource descriptor HOB\r
386 //\r
387 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
388 NextResourceHob = NextHob.ResourceDescriptor;\r
389 //\r
390 // If range described in this HOB is not system memory or higher than MAX_ADDRESS, ignored.\r
391 //\r
392 if (NextResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY || NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) {\r
393 continue;\r
394 }\r
395 //\r
396 // If the range describe in memory allocation HOB belongs to the memory range described by the resource HOB\r
397 //\r
398 if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart &&\r
399 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
400 //\r
401 // Build separate resource HOB for this allocated range\r
402 //\r
403 if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {\r
404 BuildResourceDescriptorHob (\r
405 EFI_RESOURCE_SYSTEM_MEMORY,\r
406 NextResourceHob->ResourceAttribute,\r
407 NextResourceHob->PhysicalStart,\r
408 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)\r
409 );\r
410 }\r
411 if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
412 BuildResourceDescriptorHob (\r
413 EFI_RESOURCE_SYSTEM_MEMORY,\r
414 NextResourceHob->ResourceAttribute,\r
415 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,\r
416 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))\r
417 );\r
418 }\r
419 NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
420 NextResourceHob->ResourceLength = MemoryHob->AllocDescriptor.MemoryLength;\r
421 break;\r
422 }\r
423 }\r
424 }\r
425 }\r
426 }\r
427\r
428 //\r
429 // Try to find and validate the TOP address.\r
430 //\r
431 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
432 //\r
433 // The LMFA feature is enabled as load module at fixed absolute address.\r
434 //\r
435 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable);\r
436 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));\r
437 //\r
438 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range\r
439 //\r
440 if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {\r
441 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress));\r
442 ASSERT (FALSE);\r
443 }\r
444 //\r
445 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies\r
446 //\r
447 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
448 //\r
449 // See if this is a resource descriptor HOB\r
450 //\r
451 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
452\r
453 ResourceHob = Hob.ResourceDescriptor;\r
454 //\r
455 // See if this resource descriptor HOB describes tested system memory below MAX_ADDRESS\r
456 //\r
457 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
458 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
459 //\r
460 // See if Top address specified by user is valid.\r
461 //\r
462 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress &&\r
463 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress &&\r
464 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
465 CurrentResourceHob = ResourceHob;\r
466 CurrentHob = Hob;\r
467 break;\r
468 }\r
469 }\r
470 }\r
471 }\r
472 if (CurrentResourceHob != NULL) {\r
473 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress));\r
474 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE;\r
475 } else {\r
476 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress));\r
477 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n"));\r
478 //\r
479 // Print the recommended Top address range.\r
480 //\r
481 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
482 //\r
483 // See if this is a resource descriptor HOB\r
484 //\r
485 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
486\r
487 ResourceHob = Hob.ResourceDescriptor;\r
488 //\r
489 // See if this resource descriptor HOB describes tested system memory below MAX_ADDRESS\r
490 //\r
491 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
492 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
493 //\r
494 // See if Top address specified by user is valid.\r
495 //\r
496 if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
497 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",\r
498 (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE),\r
499 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE)\r
500 ));\r
501 }\r
502 }\r
503 }\r
504 }\r
505 //\r
506 // Assert here\r
507 //\r
508 ASSERT (FALSE);\r
509 return;\r
510 }\r
511 } else {\r
512 //\r
513 // The LMFA feature is enabled as load module at fixed offset relative to TOLM\r
514 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)\r
515 //\r
516 //\r
517 // Search for a tested memory region that is below MAX_ADDRESS\r
518 //\r
519 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
520 //\r
521 // See if this is a resource descriptor HOB\r
522 //\r
523 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
524\r
525 ResourceHob = Hob.ResourceDescriptor;\r
526 //\r
527 // See if this resource descriptor HOB describes tested system memory below MAX_ADDRESS\r
528 //\r
529 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
530 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
531 ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
532 //\r
533 // See if this is the highest largest system memory region below MaxAddress\r
534 //\r
535 if (ResourceHob->PhysicalStart > HighAddress) {\r
536 CurrentResourceHob = ResourceHob;\r
537 CurrentHob = Hob;\r
538 HighAddress = CurrentResourceHob->PhysicalStart;\r
539 }\r
540 }\r
541 }\r
542 }\r
543 if (CurrentResourceHob == NULL) {\r
544 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));\r
545 //\r
546 // Assert here\r
547 //\r
548 ASSERT (FALSE);\r
549 return;\r
550 } else {\r
551 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ;\r
552 }\r
553 }\r
554\r
555 if (CurrentResourceHob != NULL) {\r
556 //\r
557 // rebuild resource HOB for PEI memory and reserved memory\r
558 //\r
559 BuildResourceDescriptorHob (\r
560 EFI_RESOURCE_SYSTEM_MEMORY,\r
561 (\r
562 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
563 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
564 EFI_RESOURCE_ATTRIBUTE_TESTED |\r
565 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
566 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
567 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
568 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
569 ),\r
570 (TopLoadingAddress - TotalReservedMemorySize),\r
571 TotalReservedMemorySize\r
572 );\r
573 //\r
574 // rebuild resource for the remain memory if necessary\r
575 //\r
576 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
577 BuildResourceDescriptorHob (\r
578 EFI_RESOURCE_SYSTEM_MEMORY,\r
579 (\r
580 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
581 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
582 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
583 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
584 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
585 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
586 ),\r
587 CurrentResourceHob->PhysicalStart,\r
588 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)\r
589 );\r
590 }\r
591 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {\r
592 BuildResourceDescriptorHob (\r
593 EFI_RESOURCE_SYSTEM_MEMORY,\r
594 (\r
595 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
596 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
597 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
598 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
599 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
600 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
601 ),\r
602 TopLoadingAddress,\r
603 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress)\r
604 );\r
605 }\r
606 //\r
607 // Delete CurrentHob by marking it as unused since the memory range described by is rebuilt.\r
608 //\r
609 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;\r
610 }\r
611\r
612 //\r
613 // Cache the top address for Loading Module at Fixed Address feature\r
614 //\r
615 PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;\r
616 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress));\r
617 //\r
618 // reinstall the PEI memory relative to TopLoadingAddress\r
619 //\r
620 PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize;\r
621 PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize;\r
622}\r
623\r
624/**\r
625 This routine is invoked in switch stack as PeiCore Entry.\r
626\r
627 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
628 environment, such as the size and location of temporary RAM, the stack location and\r
629 the BFV location.\r
630 @param Private Pointer to old core data that is used to initialize the\r
631 core's data areas.\r
632**/\r
633VOID\r
634EFIAPI\r
635PeiCoreEntry (\r
636 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
637 IN PEI_CORE_INSTANCE *Private\r
638 )\r
639{\r
640 //\r
641 // Entry PEI Phase 2\r
642 //\r
643 PeiCore (SecCoreData, NULL, Private);\r
644}\r
645\r
646/**\r
647 Check SwitchStackSignal and switch stack if SwitchStackSignal is TRUE.\r
648\r
649 @param[in] SecCoreData Points to a data structure containing information about the PEI core's operating\r
650 environment, such as the size and location of temporary RAM, the stack location and\r
651 the BFV location.\r
652 @param[in] Private Pointer to the private data passed in from caller.\r
653\r
654**/\r
655VOID\r
656PeiCheckAndSwitchStack (\r
657 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
658 IN PEI_CORE_INSTANCE *Private\r
659 )\r
660{\r
661 VOID *LoadFixPeiCodeBegin;\r
662 EFI_STATUS Status;\r
663 CONST EFI_PEI_SERVICES **PeiServices;\r
664 UINT64 NewStackSize;\r
665 EFI_PHYSICAL_ADDRESS TopOfOldStack;\r
666 EFI_PHYSICAL_ADDRESS TopOfNewStack;\r
667 UINTN StackOffset;\r
668 BOOLEAN StackOffsetPositive;\r
669 EFI_PHYSICAL_ADDRESS TemporaryRamBase;\r
670 UINTN TemporaryRamSize;\r
671 UINTN TemporaryStackSize;\r
672 VOID *TemporaryStackBase;\r
673 UINTN PeiTemporaryRamSize;\r
674 VOID *PeiTemporaryRamBase;\r
675 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
676 EFI_PHYSICAL_ADDRESS BaseOfNewHeap;\r
677 EFI_PHYSICAL_ADDRESS HoleMemBase;\r
678 UINTN HoleMemSize;\r
679 UINTN HeapTemporaryRamSize;\r
680 EFI_PHYSICAL_ADDRESS TempBase1;\r
681 UINTN TempSize1;\r
682 EFI_PHYSICAL_ADDRESS TempBase2;\r
683 UINTN TempSize2;\r
684 UINTN Index;\r
685\r
686 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
687\r
688 if (Private->SwitchStackSignal) {\r
689 //\r
690 // Before switch stack from temporary memory to permanent memory, calculate the heap and stack\r
691 // usage in temporary memory for debugging.\r
692 //\r
693 DEBUG_CODE_BEGIN ();\r
694 UINT32 *StackPointer;\r
695 EFI_PEI_HOB_POINTERS Hob;\r
696\r
697 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
698 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
699 && (*StackPointer == PcdGet32 (PcdInitValueInTempStack));\r
700 StackPointer ++) {\r
701 }\r
702\r
703 DEBUG ((DEBUG_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
704 DEBUG ((DEBUG_INFO, "Temp Heap : BaseAddress=0x%p Length=0x%X\n", SecCoreData->PeiTemporaryRamBase, (UINT32)SecCoreData->PeiTemporaryRamSize));\r
705 DEBUG ((DEBUG_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
706 DEBUG ((DEBUG_INFO, " temporary memory stack ever used: %d bytes.\n",\r
707 (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
708 ));\r
709 DEBUG ((DEBUG_INFO, " temporary memory heap used for HobList: %d bytes.\n",\r
710 (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
711 ));\r
712 DEBUG ((DEBUG_INFO, " temporary memory heap occupied by memory pages: %d bytes.\n",\r
713 (UINT32)(UINTN)(Private->HobList.HandoffInformationTable->EfiMemoryTop - Private->HobList.HandoffInformationTable->EfiFreeMemoryTop)\r
714 ));\r
715 for (Hob.Raw = Private->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
716 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
717 DEBUG ((DEBUG_INFO, "Memory Allocation 0x%08x 0x%0lx - 0x%0lx\n", \\r
718 Hob.MemoryAllocation->AllocDescriptor.MemoryType, \\r
719 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress, \\r
720 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength - 1));\r
721 }\r
722 }\r
723 DEBUG_CODE_END ();\r
724\r
725 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
726 //\r
727 // Loading Module at Fixed Address is enabled\r
728 //\r
729 PeiLoadFixAddressHook (Private);\r
730\r
731 //\r
732 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.\r
733 //\r
734 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
735 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PeiCodeBegin = 0x%lX, PeiCodeTop= 0x%lX\n", (UINT64)(UINTN)LoadFixPeiCodeBegin, (UINT64)((UINTN)LoadFixPeiCodeBegin + PcdGet32(PcdLoadFixAddressPeiCodePageNumber) * EFI_PAGE_SIZE)));\r
736 }\r
737\r
738 //\r
739 // Reserve the size of new stack at bottom of physical memory\r
740 //\r
741 // The size of new stack in permanent memory must be the same size\r
742 // or larger than the size of old stack in temporary memory.\r
743 // But if new stack is smaller than the size of old stack, we also reserve\r
744 // the size of old stack at bottom of permanent memory.\r
745 //\r
746 NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1);\r
747 NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE);\r
748 NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize);\r
749 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize));\r
750 ASSERT (NewStackSize >= SecCoreData->StackSize);\r
751\r
752 //\r
753 // Calculate stack offset and heap offset between temporary memory and new permanent\r
754 // memory separately.\r
755 //\r
756 TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;\r
757 TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize;\r
758 if (TopOfNewStack >= TopOfOldStack) {\r
759 StackOffsetPositive = TRUE;\r
760 StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack);\r
761 } else {\r
762 StackOffsetPositive = FALSE;\r
763 StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack);\r
764 }\r
765 Private->StackOffsetPositive = StackOffsetPositive;\r
766 Private->StackOffset = StackOffset;\r
767\r
768 //\r
769 // Build Stack HOB that describes the permanent memory stack\r
770 //\r
771 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize));\r
772 BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize);\r
773\r
774 //\r
775 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address\r
776 //\r
777 TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase;\r
778 TemporaryRamSize = SecCoreData->TemporaryRamSize;\r
779 TemporaryStackSize = SecCoreData->StackSize;\r
780 TemporaryStackBase = SecCoreData->StackBase;\r
781 PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize;\r
782 PeiTemporaryRamBase = SecCoreData->PeiTemporaryRamBase;\r
783\r
784 //\r
785 // TemporaryRamSupportPpi is produced by platform's SEC\r
786 //\r
787 Status = PeiServicesLocatePpi (\r
788 &gEfiTemporaryRamSupportPpiGuid,\r
789 0,\r
790 NULL,\r
791 (VOID**)&TemporaryRamSupportPpi\r
792 );\r
793 if (!EFI_ERROR (Status)) {\r
794 //\r
795 // Heap Offset\r
796 //\r
797 BaseOfNewHeap = TopOfNewStack;\r
798 if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
799 Private->HeapOffsetPositive = TRUE;\r
800 Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
801 } else {\r
802 Private->HeapOffsetPositive = FALSE;\r
803 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap);\r
804 }\r
805\r
806 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64) Private->HeapOffset, (UINT64) Private->StackOffset));\r
807\r
808 //\r
809 // Calculate new HandOffTable and PrivateData address in permanent memory's stack\r
810 //\r
811 if (StackOffsetPositive) {\r
812 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset);\r
813 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset);\r
814 } else {\r
815 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset);\r
816 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset);\r
817 }\r
818\r
819 //\r
820 // Temporary Ram Support PPI is provided by platform, it will copy\r
821 // temporary memory to permanent memory and do stack switching.\r
822 // After invoking Temporary Ram Support PPI, the following code's\r
823 // stack is in permanent memory.\r
824 //\r
825 TemporaryRamSupportPpi->TemporaryRamMigration (\r
826 PeiServices,\r
827 TemporaryRamBase,\r
828 (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize),\r
829 TemporaryRamSize\r
830 );\r
831\r
832 //\r
833 // Migrate memory pages allocated in pre-memory phase.\r
834 // It could not be called before calling TemporaryRamSupportPpi->TemporaryRamMigration()\r
835 // as the migrated memory pages may be overridden by TemporaryRamSupportPpi->TemporaryRamMigration().\r
836 //\r
837 MigrateMemoryPages (Private, TRUE);\r
838\r
839 //\r
840 // Entry PEI Phase 2\r
841 //\r
842 PeiCore (SecCoreData, NULL, Private);\r
843 } else {\r
844 //\r
845 // Migrate memory pages allocated in pre-memory phase.\r
846 //\r
847 MigrateMemoryPages (Private, FALSE);\r
848\r
849 //\r
850 // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.\r
851 //\r
852 MigratePeiServicesTablePointer ();\r
853\r
854 //\r
855 // Heap Offset\r
856 //\r
857 BaseOfNewHeap = TopOfNewStack;\r
858 HoleMemBase = TopOfNewStack;\r
859 HoleMemSize = TemporaryRamSize - PeiTemporaryRamSize - TemporaryStackSize;\r
860 if (HoleMemSize != 0) {\r
861 //\r
862 // Make sure HOB List start address is 8 byte alignment.\r
863 //\r
864 BaseOfNewHeap = ALIGN_VALUE (BaseOfNewHeap + HoleMemSize, 8);\r
865 }\r
866 if (BaseOfNewHeap >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
867 Private->HeapOffsetPositive = TRUE;\r
868 Private->HeapOffset = (UINTN)(BaseOfNewHeap - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
869 } else {\r
870 Private->HeapOffsetPositive = FALSE;\r
871 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - BaseOfNewHeap);\r
872 }\r
873\r
874 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64) Private->HeapOffset, (UINT64) Private->StackOffset));\r
875\r
876 //\r
877 // Migrate Heap\r
878 //\r
879 HeapTemporaryRamSize = (UINTN) (Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - Private->HobList.HandoffInformationTable->EfiMemoryBottom);\r
880 ASSERT (BaseOfNewHeap + HeapTemporaryRamSize <= Private->FreePhysicalMemoryTop);\r
881 CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, PeiTemporaryRamBase, HeapTemporaryRamSize);\r
882\r
883 //\r
884 // Migrate Stack\r
885 //\r
886 CopyMem ((UINT8 *) (UINTN) (TopOfNewStack - TemporaryStackSize), TemporaryStackBase, TemporaryStackSize);\r
887\r
888 //\r
889 // Copy Hole Range Data\r
890 //\r
891 if (HoleMemSize != 0) {\r
892 //\r
893 // Prepare Hole\r
894 //\r
895 if (PeiTemporaryRamBase < TemporaryStackBase) {\r
896 TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase;\r
897 TempSize1 = PeiTemporaryRamSize;\r
898 TempBase2 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase;\r
899 TempSize2 = TemporaryStackSize;\r
900 } else {\r
901 TempBase1 = (EFI_PHYSICAL_ADDRESS) (UINTN) TemporaryStackBase;\r
902 TempSize1 = TemporaryStackSize;\r
903 TempBase2 =(EFI_PHYSICAL_ADDRESS) (UINTN) PeiTemporaryRamBase;\r
904 TempSize2 = PeiTemporaryRamSize;\r
905 }\r
906 if (TemporaryRamBase < TempBase1) {\r
907 Private->HoleData[0].Base = TemporaryRamBase;\r
908 Private->HoleData[0].Size = (UINTN) (TempBase1 - TemporaryRamBase);\r
909 }\r
910 if (TempBase1 + TempSize1 < TempBase2) {\r
911 Private->HoleData[1].Base = TempBase1 + TempSize1;\r
912 Private->HoleData[1].Size = (UINTN) (TempBase2 - TempBase1 - TempSize1);\r
913 }\r
914 if (TempBase2 + TempSize2 < TemporaryRamBase + TemporaryRamSize) {\r
915 Private->HoleData[2].Base = TempBase2 + TempSize2;\r
916 Private->HoleData[2].Size = (UINTN) (TemporaryRamBase + TemporaryRamSize - TempBase2 - TempSize2);\r
917 }\r
918\r
919 //\r
920 // Copy Hole Range data.\r
921 //\r
922 for (Index = 0; Index < HOLE_MAX_NUMBER; Index ++) {\r
923 if (Private->HoleData[Index].Size > 0) {\r
924 if (HoleMemBase > Private->HoleData[Index].Base) {\r
925 Private->HoleData[Index].OffsetPositive = TRUE;\r
926 Private->HoleData[Index].Offset = (UINTN) (HoleMemBase - Private->HoleData[Index].Base);\r
927 } else {\r
928 Private->HoleData[Index].OffsetPositive = FALSE;\r
929 Private->HoleData[Index].Offset = (UINTN) (Private->HoleData[Index].Base - HoleMemBase);\r
930 }\r
931 CopyMem ((VOID *) (UINTN) HoleMemBase, (VOID *) (UINTN) Private->HoleData[Index].Base, Private->HoleData[Index].Size);\r
932 HoleMemBase = HoleMemBase + Private->HoleData[Index].Size;\r
933 }\r
934 }\r
935 }\r
936\r
937 //\r
938 // Switch new stack\r
939 //\r
940 SwitchStack (\r
941 (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiCoreEntry,\r
942 (VOID *) SecCoreData,\r
943 (VOID *) Private,\r
944 (VOID *) (UINTN) TopOfNewStack\r
945 );\r
946 }\r
947\r
948 //\r
949 // Code should not come here\r
950 //\r
951 ASSERT (FALSE);\r
952 }\r
953}\r
954\r
955/**\r
956 Conduct PEIM dispatch.\r
957\r
958 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
959 environment, such as the size and location of temporary RAM, the stack location and\r
960 the BFV location.\r
961 @param Private Pointer to the private data passed in from caller\r
962\r
963**/\r
964VOID\r
965PeiDispatcher (\r
966 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
967 IN PEI_CORE_INSTANCE *Private\r
968 )\r
969{\r
970 EFI_STATUS Status;\r
971 UINT32 Index1;\r
972 UINT32 Index2;\r
973 CONST EFI_PEI_SERVICES **PeiServices;\r
974 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
975 UINTN FvCount;\r
976 UINTN PeimCount;\r
977 UINT32 AuthenticationState;\r
978 EFI_PHYSICAL_ADDRESS EntryPoint;\r
979 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
980 UINTN SaveCurrentPeimCount;\r
981 UINTN SaveCurrentFvCount;\r
982 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
983 EFI_FV_FILE_INFO FvFileInfo;\r
984 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
985\r
986 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
987 PeimEntryPoint = NULL;\r
988 PeimFileHandle = NULL;\r
989 EntryPoint = 0;\r
990\r
991 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
992 //\r
993 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
994 // update the modules' status from PEIM_STATE_REGISTER_FOR_SHADOW to PEIM_STATE_DONE.\r
995 //\r
996 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
997 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
998 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
999\r
1000 for (Index1 = 0; Index1 < Private->FvCount; Index1++) {\r
1001 for (Index2 = 0; Index2 < Private->Fv[Index1].PeimCount; Index2++) {\r
1002 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
1003 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
1004 Private->CurrentFileHandle = PeimFileHandle;\r
1005 Private->CurrentPeimFvCount = Index1;\r
1006 Private->CurrentPeimCount = Index2;\r
1007 Status = PeiLoadImage (\r
1008 (CONST EFI_PEI_SERVICES **) &Private->Ps,\r
1009 PeimFileHandle,\r
1010 PEIM_STATE_REGISTER_FOR_SHADOW,\r
1011 &EntryPoint,\r
1012 &AuthenticationState\r
1013 );\r
1014 if (Status == EFI_SUCCESS) {\r
1015 //\r
1016 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
1017 //\r
1018 Private->Fv[Index1].PeimState[Index2]++;\r
1019 //\r
1020 // Call the PEIM entry point\r
1021 //\r
1022 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1023\r
1024 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
1025 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);\r
1026 PERF_START_IMAGE_END (PeimFileHandle);\r
1027 }\r
1028\r
1029 //\r
1030 // Process the Notify list and dispatch any notifies for\r
1031 // newly installed PPIs.\r
1032 //\r
1033 ProcessDispatchNotifyList (Private);\r
1034 }\r
1035 }\r
1036 }\r
1037 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
1038 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
1039 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
1040 }\r
1041\r
1042 //\r
1043 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
1044 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
1045 // pass of the dispatcher, it will start over from the BFV again to see\r
1046 // if any new PEIMs dependencies got satisfied. With a well ordered\r
1047 // FV where PEIMs are found in the order their dependencies are also\r
1048 // satisfied, this dispatcher should run only once.\r
1049 //\r
1050 do {\r
1051 //\r
1052 // In case that reenter PeiCore happens, the last pass record is still available.\r
1053 //\r
1054 if (!Private->PeimDispatcherReenter) {\r
1055 Private->PeimNeedingDispatch = FALSE;\r
1056 Private->PeimDispatchOnThisPass = FALSE;\r
1057 } else {\r
1058 Private->PeimDispatcherReenter = FALSE;\r
1059 }\r
1060\r
1061 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
1062 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
1063 ASSERT (CoreFvHandle != NULL);\r
1064\r
1065 //\r
1066 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
1067 //\r
1068 if (CoreFvHandle->FvPpi == NULL) {\r
1069 continue;\r
1070 }\r
1071\r
1072 Private->CurrentPeimFvCount = FvCount;\r
1073\r
1074 if (Private->CurrentPeimCount == 0) {\r
1075 //\r
1076 // When going through each FV, at first, search Apriori file to\r
1077 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
1078 // dispatch at first.\r
1079 //\r
1080 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
1081 }\r
1082\r
1083 //\r
1084 // Start to dispatch all modules within the current FV.\r
1085 //\r
1086 for (PeimCount = Private->CurrentPeimCount;\r
1087 PeimCount < Private->Fv[FvCount].PeimCount;\r
1088 PeimCount++) {\r
1089 Private->CurrentPeimCount = PeimCount;\r
1090 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
1091\r
1092 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
1093 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
1094 Private->PeimNeedingDispatch = TRUE;\r
1095 } else {\r
1096 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
1097 ASSERT_EFI_ERROR (Status);\r
1098 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
1099 //\r
1100 // For FV type file, Produce new FvInfo PPI and FV HOB\r
1101 //\r
1102 Status = ProcessFvFile (Private, &Private->Fv[FvCount], PeimFileHandle);\r
1103 if (Status == EFI_SUCCESS) {\r
1104 //\r
1105 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
1106 //\r
1107 Private->Fv[FvCount].PeimState[PeimCount]++;\r
1108 Private->PeimDispatchOnThisPass = TRUE;\r
1109 } else {\r
1110 //\r
1111 // The related GuidedSectionExtraction/Decompress PPI for the\r
1112 // encapsulated FV image section may be installed in the rest\r
1113 // of this do-while loop, so need to make another pass.\r
1114 //\r
1115 Private->PeimNeedingDispatch = TRUE;\r
1116 }\r
1117 } else {\r
1118 //\r
1119 // For PEIM driver, Load its entry point\r
1120 //\r
1121 Status = PeiLoadImage (\r
1122 PeiServices,\r
1123 PeimFileHandle,\r
1124 PEIM_STATE_NOT_DISPATCHED,\r
1125 &EntryPoint,\r
1126 &AuthenticationState\r
1127 );\r
1128 if (Status == EFI_SUCCESS) {\r
1129 //\r
1130 // The PEIM has its dependencies satisfied, and its entry point\r
1131 // has been found, so invoke it.\r
1132 //\r
1133 PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
1134\r
1135 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1136 EFI_PROGRESS_CODE,\r
1137 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
1138 (VOID *)(&PeimFileHandle),\r
1139 sizeof (PeimFileHandle)\r
1140 );\r
1141\r
1142 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle, AuthenticationState);\r
1143 if (Status != EFI_SECURITY_VIOLATION) {\r
1144 //\r
1145 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
1146 //\r
1147 Private->Fv[FvCount].PeimState[PeimCount]++;\r
1148 //\r
1149 // Call the PEIM entry point for PEIM driver\r
1150 //\r
1151 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1152 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
1153 Private->PeimDispatchOnThisPass = TRUE;\r
1154 } else {\r
1155 //\r
1156 // The related GuidedSectionExtraction PPI for the\r
1157 // signed PEIM image section may be installed in the rest\r
1158 // of this do-while loop, so need to make another pass.\r
1159 //\r
1160 Private->PeimNeedingDispatch = TRUE;\r
1161 }\r
1162\r
1163 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
1164 EFI_PROGRESS_CODE,\r
1165 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END),\r
1166 (VOID *)(&PeimFileHandle),\r
1167 sizeof (PeimFileHandle)\r
1168 );\r
1169 PERF_START_IMAGE_END (PeimFileHandle);\r
1170\r
1171 }\r
1172 }\r
1173\r
1174 PeiCheckAndSwitchStack (SecCoreData, Private);\r
1175\r
1176 //\r
1177 // Process the Notify list and dispatch any notifies for\r
1178 // newly installed PPIs.\r
1179 //\r
1180 ProcessDispatchNotifyList (Private);\r
1181\r
1182 //\r
1183 // Recheck SwitchStackSignal after ProcessDispatchNotifyList()\r
1184 // in case PeiInstallPeiMemory() is done in a callback with\r
1185 // EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH.\r
1186 //\r
1187 PeiCheckAndSwitchStack (SecCoreData, Private);\r
1188\r
1189 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW) && \\r
1190 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
1191 //\r
1192 // If memory is available we shadow images by default for performance reasons.\r
1193 // We call the entry point a 2nd time so the module knows it's shadowed.\r
1194 //\r
1195 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
1196 if ((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && !PcdGetBool (PcdShadowPeimOnBoot)) {\r
1197 //\r
1198 // Load PEIM into Memory for Register for shadow PEIM.\r
1199 //\r
1200 Status = PeiLoadImage (\r
1201 PeiServices,\r
1202 PeimFileHandle,\r
1203 PEIM_STATE_REGISTER_FOR_SHADOW,\r
1204 &EntryPoint,\r
1205 &AuthenticationState\r
1206 );\r
1207 if (Status == EFI_SUCCESS) {\r
1208 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
1209 }\r
1210 }\r
1211 ASSERT (PeimEntryPoint != NULL);\r
1212 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
1213 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
1214\r
1215 //\r
1216 // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
1217 //\r
1218 Private->Fv[FvCount].PeimState[PeimCount]++;\r
1219\r
1220 //\r
1221 // Process the Notify list and dispatch any notifies for\r
1222 // newly installed PPIs.\r
1223 //\r
1224 ProcessDispatchNotifyList (Private);\r
1225 }\r
1226 }\r
1227 }\r
1228 }\r
1229\r
1230 //\r
1231 // Before walking through the next FV, we should set them to NULL/0 to\r
1232 // start at the beginning of the next FV.\r
1233 //\r
1234 Private->CurrentFileHandle = NULL;\r
1235 Private->CurrentPeimCount = 0;\r
1236 Private->CurrentFvFileHandles = NULL;\r
1237 }\r
1238\r
1239 //\r
1240 // Before making another pass, we should set it to 0 to\r
1241 // go through all the FVs.\r
1242 //\r
1243 Private->CurrentPeimFvCount = 0;\r
1244\r
1245 //\r
1246 // PeimNeedingDispatch being TRUE means we found a PEIM/FV that did not get\r
1247 // dispatched. So we need to make another pass\r
1248 //\r
1249 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM/FV on this\r
1250 // pass. If we did not dispatch a PEIM/FV there is no point in trying again\r
1251 // as it will fail the next time too (nothing has changed).\r
1252 //\r
1253 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
1254\r
1255}\r
1256\r
1257/**\r
1258 Initialize the Dispatcher's data members\r
1259\r
1260 @param PrivateData PeiCore's private data structure\r
1261 @param OldCoreData Old data from SecCore\r
1262 NULL if being run in non-permanent memory mode.\r
1263 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
1264 environment, such as the size and location of temporary RAM, the stack location and\r
1265 the BFV location.\r
1266\r
1267 @return None.\r
1268\r
1269**/\r
1270VOID\r
1271InitializeDispatcherData (\r
1272 IN PEI_CORE_INSTANCE *PrivateData,\r
1273 IN PEI_CORE_INSTANCE *OldCoreData,\r
1274 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
1275 )\r
1276{\r
1277 if (OldCoreData == NULL) {\r
1278 PrivateData->PeimDispatcherReenter = FALSE;\r
1279 PeiInitializeFv (PrivateData, SecCoreData);\r
1280 } else {\r
1281 PeiReinitializeFv (PrivateData);\r
1282 }\r
1283\r
1284 return;\r
1285}\r
1286\r
1287/**\r
1288 This routine parses the Dependency Expression, if available, and\r
1289 decides if the module can be executed.\r
1290\r
1291\r
1292 @param Private PeiCore's private data structure\r
1293 @param FileHandle PEIM's file handle\r
1294 @param PeimCount Peim count in all dispatched PEIMs.\r
1295\r
1296 @retval TRUE Can be dispatched\r
1297 @retval FALSE Cannot be dispatched\r
1298\r
1299**/\r
1300BOOLEAN\r
1301DepexSatisfied (\r
1302 IN PEI_CORE_INSTANCE *Private,\r
1303 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1304 IN UINTN PeimCount\r
1305 )\r
1306{\r
1307 EFI_STATUS Status;\r
1308 VOID *DepexData;\r
1309 EFI_FV_FILE_INFO FileInfo;\r
1310\r
1311 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
1312 if (EFI_ERROR (Status)) {\r
1313 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n"));\r
1314 } else {\r
1315 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));\r
1316 }\r
1317\r
1318 if (PeimCount < Private->AprioriCount) {\r
1319 //\r
1320 // If it's in the Apriori file then we set DEPEX to TRUE\r
1321 //\r
1322 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
1323 return TRUE;\r
1324 }\r
1325\r
1326 //\r
1327 // Depex section not in the encapsulated section.\r
1328 //\r
1329 Status = PeiServicesFfsFindSectionData (\r
1330 EFI_SECTION_PEI_DEPEX,\r
1331 FileHandle,\r
1332 (VOID **)&DepexData\r
1333 );\r
1334\r
1335 if (EFI_ERROR (Status)) {\r
1336 //\r
1337 // If there is no DEPEX, assume the module can be executed\r
1338 //\r
1339 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n"));\r
1340 return TRUE;\r
1341 }\r
1342\r
1343 //\r
1344 // Evaluate a given DEPEX\r
1345 //\r
1346 return PeimDispatchReadiness (&Private->Ps, DepexData);\r
1347}\r
1348\r
1349/**\r
1350 This routine enables a PEIM to register itself for shadow when the PEI Foundation\r
1351 discovers permanent memory.\r
1352\r
1353 @param FileHandle File handle of a PEIM.\r
1354\r
1355 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
1356 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
1357 @retval EFI_SUCCESS Successfully to register itself.\r
1358\r
1359**/\r
1360EFI_STATUS\r
1361EFIAPI\r
1362PeiRegisterForShadow (\r
1363 IN EFI_PEI_FILE_HANDLE FileHandle\r
1364 )\r
1365{\r
1366 PEI_CORE_INSTANCE *Private;\r
1367 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
1368\r
1369 if (Private->CurrentFileHandle != FileHandle) {\r
1370 //\r
1371 // The FileHandle must be for the current PEIM\r
1372 //\r
1373 return EFI_NOT_FOUND;\r
1374 }\r
1375\r
1376 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISTER_FOR_SHADOW) {\r
1377 //\r
1378 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
1379 //\r
1380 return EFI_ALREADY_STARTED;\r
1381 }\r
1382\r
1383 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISTER_FOR_SHADOW;\r
1384\r
1385 return EFI_SUCCESS;\r
1386}\r
1387\r
1388\r
1389\r