Add Checking for MTRR existence.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core dispatch services\r
3 \r
f4391d63 4Copyright (c) 2006 - 2010, Intel Corporation\r
192f6d4c 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
b1f6a7c6 17///\r
3d4d0c34 18/// temporary memory is filled with this initial value during SEC phase\r
b1f6a7c6 19///\r
a7715e73 20#define INIT_CAR_VALUE 0x5AA55AA5\r
21\r
797a9d67 22typedef struct {\r
23 EFI_STATUS_CODE_DATA DataHeader;\r
24 EFI_HANDLE Handle;\r
25} PEIM_FILE_HANDLE_EXTENDED_DATA;\r
26\r
b1f6a7c6 27/**\r
b0d803fe 28\r
29 Discover all Peims and optional Apriori file in one FV. There is at most one\r
30 Apriori file in one FV.\r
31\r
b0d803fe 32\r
3b428ade 33 @param Private Pointer to the private data passed in from caller\r
34 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.\r
b0d803fe 35\r
b1f6a7c6 36**/\r
37VOID\r
38DiscoverPeimsAndOrderWithApriori (\r
39 IN PEI_CORE_INSTANCE *Private,\r
3b428ade 40 IN PEI_CORE_FV_HANDLE *CoreFileHandle\r
b1f6a7c6 41 )\r
b0d803fe 42{\r
43 EFI_STATUS Status;\r
44 EFI_PEI_FV_HANDLE FileHandle;\r
177aabe6 45 EFI_PEI_FILE_HANDLE AprioriFileHandle;\r
b0d803fe 46 EFI_GUID *Apriori;\r
47 UINTN Index;\r
48 UINTN Index2;\r
49 UINTN PeimIndex;\r
50 UINTN PeimCount;\r
51 EFI_GUID *Guid;\r
177aabe6 52 EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];\r
3b428ade 54 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
55 EFI_FV_FILE_INFO FileInfo;\r
56 \r
57 FvPpi = CoreFileHandle->FvPpi;\r
58 \r
b0d803fe 59 //\r
60 // Walk the FV and find all the PEIMs and the Apriori file.\r
61 //\r
62 AprioriFileHandle = NULL;\r
63 Private->CurrentFvFileHandles[0] = NULL;\r
64 Guid = NULL;\r
65 FileHandle = NULL;\r
66\r
67 //\r
68 // If the current Fv has been scanned, directly get its cachable record.\r
69 //\r
70 if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {\r
71 CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
72 return;\r
73 }\r
74\r
75 //\r
76 // Go ahead to scan this Fv, and cache FileHandles within it.\r
77 //\r
4140a663 78 for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {\r
3b428ade 79 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
b0d803fe 80 if (Status != EFI_SUCCESS) {\r
81 break;\r
82 }\r
58dcdada 83\r
b0d803fe 84 Private->CurrentFvFileHandles[PeimCount] = FileHandle;\r
85 }\r
97b2c9b5
LG
86 \r
87 //\r
88 // Check whether the count of Peims exceeds the max support PEIMs in a FV image\r
89 // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.\r
90 //\r
4140a663 91 ASSERT (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
b0d803fe 92\r
3b428ade 93 //\r
94 // Get Apriori File handle\r
95 //\r
58dcdada 96 Private->AprioriCount = 0;\r
3b428ade 97 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);\r
98 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {\r
b0d803fe 99 //\r
100 // Read the Apriori file\r
101 //\r
3b428ade 102 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
b0d803fe 103 if (!EFI_ERROR (Status)) {\r
104 //\r
105 // Calculate the number of PEIMs in the A Priori list\r
106 //\r
3b428ade 107 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
108 ASSERT_EFI_ERROR (Status);\r
109 Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF;\r
595d4b4c 110 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);\r
b0d803fe 111 Private->AprioriCount /= sizeof (EFI_GUID);\r
58dcdada 112\r
82b8c8df 113 ZeroMem (FileGuid, sizeof (FileGuid));\r
b0d803fe 114 for (Index = 0; Index < PeimCount; Index++) {\r
115 //\r
116 // Make an array of file name guids that matches the FileHandle array so we can convert\r
117 // quickly from file name to file handle\r
118 //\r
3b428ade 119 Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);\r
120 CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
b0d803fe 121 }\r
122\r
123 //\r
124 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
58dcdada 125 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.\r
b0d803fe 126 //\r
127 Index2 = 0;\r
128 for (Index = 0; Index2 < Private->AprioriCount; Index++) {\r
129 while (Index2 < Private->AprioriCount) {\r
130 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);\r
131 if (Guid != NULL) {\r
132 break;\r
133 }\r
134 }\r
135 if (Guid == NULL) {\r
58dcdada 136 break;\r
b0d803fe 137 }\r
138 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);\r
139 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];\r
140\r
141 //\r
142 // Since we have copied the file handle we can remove it from this list.\r
143 //\r
144 Private->CurrentFvFileHandles[PeimIndex] = NULL;\r
145 }\r
146\r
147 //\r
148 // Update valid Aprioricount\r
149 //\r
150 Private->AprioriCount = Index;\r
58dcdada 151\r
b0d803fe 152 //\r
153 // Add in any PEIMs not in the Apriori file\r
154 //\r
155 for (;Index < PeimCount; Index++) {\r
156 for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
157 if (Private->CurrentFvFileHandles[Index2] != NULL) {\r
158 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];\r
159 Private->CurrentFvFileHandles[Index2] = NULL;\r
160 break;\r
161 }\r
162 }\r
163 }\r
164 //\r
165 //Index the end of array contains re-range Pei moudle.\r
166 //\r
167 TempFileHandles[Index] = NULL;\r
58dcdada 168\r
b0d803fe 169 //\r
170 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
58dcdada 171 // We need to update it to start with files in the A Priori list and\r
172 // then the remaining files in PEIM order.\r
b0d803fe 173 //\r
174 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));\r
175 }\r
176 }\r
177 //\r
178 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.\r
179 // Instead, we can retrieve the file handles within this Fv from cachable data.\r
180 //\r
181 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;\r
182 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));\r
58dcdada 183\r
184}\r
185\r
b1f6a7c6 186/**\r
187 Shadow PeiCore module from flash to installed memory.\r
188 \r
d73d93c3 189 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 190 @param PrivateInMem PeiCore's private data structure\r
191\r
82b8c8df 192 @return PeiCore function address after shadowing.\r
b1f6a7c6 193**/\r
58dcdada 194VOID*\r
195ShadowPeiCore(\r
6c7a807a 196 IN CONST EFI_PEI_SERVICES **PeiServices,\r
197 IN PEI_CORE_INSTANCE *PrivateInMem\r
58dcdada 198 )\r
199{\r
200 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;\r
201 EFI_PHYSICAL_ADDRESS EntryPoint;\r
202 EFI_STATUS Status;\r
203 UINT32 AuthenticationState;\r
204\r
205 PeiCoreFileHandle = NULL;\r
206\r
207 //\r
208 // Find the PEI Core in the BFV\r
209 //\r
3b428ade 210 Status = PrivateInMem->Fv[0].FvPpi->FindFileByType (\r
211 PrivateInMem->Fv[0].FvPpi,\r
212 EFI_FV_FILETYPE_PEI_CORE,\r
213 PrivateInMem->Fv[0].FvHandle,\r
214 &PeiCoreFileHandle\r
215 );\r
58dcdada 216 ASSERT_EFI_ERROR (Status);\r
217\r
218 //\r
219 // Shadow PEI Core into memory so it will run faster\r
220 //\r
221 Status = PeiLoadImage (\r
222 PeiServices,\r
223 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
341a658f 224 PEIM_STATE_REGISITER_FOR_SHADOW,\r
58dcdada 225 &EntryPoint,\r
226 &AuthenticationState\r
227 );\r
228 ASSERT_EFI_ERROR (Status);\r
229\r
82b8c8df 230 //\r
231 // Compute the PeiCore's function address after shaowed PeiCore.\r
232 // _ModuleEntryPoint is PeiCore main function entry\r
233 //\r
58dcdada 234 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
b0d803fe 235}\r
54ea99a7 236//\r
237// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
238// This part of memory still need reserved on the very top of memory so that the DXE Core could \r
239// use these memory for data initialization. This macro should be sync with the same marco\r
240// defined in DXE Core.\r
241//\r
242#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
243/**\r
244 Hook function for Loading Module at Fixed Address feature\r
245 \r
246 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
247 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When \r
248 feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general. \r
249 And also the function will re-install PEI memory. \r
b0d803fe 250\r
54ea99a7 251 @param PrivateData Pointer to the private data passed in from caller\r
252\r
253**/\r
254VOID\r
255PeiLoadFixAddressHook(\r
256 IN PEI_CORE_INSTANCE *PrivateData\r
257 )\r
258{\r
259 EFI_PHYSICAL_ADDRESS TopLoadingAddress;\r
260 UINT64 PeiMemorySize;\r
261 UINT64 TotalReservedMemorySize;\r
262 UINT64 MemoryRangeEnd;\r
263 EFI_PHYSICAL_ADDRESS HighAddress; \r
264 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
265 EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob;\r
266 EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob;\r
267 EFI_PEI_HOB_POINTERS CurrentHob;\r
268 EFI_PEI_HOB_POINTERS Hob;\r
269 EFI_PEI_HOB_POINTERS NextHob;\r
270 EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;\r
271 UINT64 MaxMemoryLength;\r
272 //\r
273 // Initialize Local Variables\r
274 //\r
275 CurrentResourceHob = NULL;\r
276 ResourceHob = NULL;\r
277 NextResourceHob = NULL;\r
278 MaxMemoryBaseAddress = 0;\r
279 MaxMemoryLength = 0;\r
280 HighAddress = 0;\r
281 TopLoadingAddress = 0;\r
282 MemoryRangeEnd = 0;\r
283 CurrentHob.Raw = PrivateData->HobList.Raw;\r
284 PeiMemorySize = PrivateData->PhysicalMemoryLength;\r
285 //\r
286 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE\r
287 // then RuntimeCodePage range and Boot time code range.\r
288 // \r
e18fa167 289 TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));\r
290 TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ; \r
54ea99a7 291 //\r
292 // PEI memory range lies below the top reserved memory\r
293 // \r
294 TotalReservedMemorySize += PeiMemorySize;\r
e18fa167 295 \r
852081fc 296 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));\r
297 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));\r
298 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber))); \r
299 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));\r
54ea99a7 300 //\r
301 // Loop through the system memory typed hob to merge the adjacent memory range \r
302 //\r
303 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
304 // \r
305 // See if this is a resource descriptor HOB \r
306 //\r
307 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
308 \r
309 ResourceHob = Hob.ResourceDescriptor; \r
310 //\r
311 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
312 //\r
313 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY &&\r
314 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {\r
315 continue;\r
316 } \r
317 \r
318 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) { \r
319 if (NextHob.Raw == Hob.Raw){\r
320 continue;\r
321 } \r
322 //\r
323 // See if this is a resource descriptor HOB\r
324 //\r
325 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
326 \r
327 NextResourceHob = NextHob.ResourceDescriptor;\r
328 //\r
329 // test if range described in this NextResourceHob is system memory and have the same attribute.\r
330 // Note: Here is a assumption that system memory should always be healthy even without test.\r
331 // \r
332 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
333 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
334 \r
335 //\r
336 // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
337 //\r
338 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart && \r
339 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)|| \r
340 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
341 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
342 \r
343 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
344 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
345 \r
346 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ? \r
347 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
348 \r
349 \r
350 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
351 \r
352 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);\r
353 //\r
354 // Delete the NextResourceHob by marking it as unused.\r
355 //\r
356 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;\r
357 \r
358 }\r
359 }\r
360 } \r
361 }\r
362 } \r
363 }\r
364 //\r
365 // Try to find and validate the TOP address.\r
366 // \r
852081fc 367 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
54ea99a7 368 //\r
369 // The LMFA feature is enabled as load module at fixed absolute address.\r
370 //\r
852081fc 371 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable);\r
54ea99a7 372 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));\r
373 //\r
374 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range\r
375 //\r
376 if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {\r
852081fc 377 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress)); \r
54ea99a7 378 ASSERT (FALSE); \r
379 }\r
380 //\r
381 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies \r
382 //\r
383 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
384 //\r
385 // See if this is a resource descriptor HOB\r
386 //\r
387 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
388\r
389 ResourceHob = Hob.ResourceDescriptor;\r
390 //\r
391 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
392 // \r
393 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
394 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
395 //\r
396 // See if Top address specified by user is valid.\r
397 //\r
398 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress && \r
399 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress) {\r
400 CurrentResourceHob = ResourceHob; \r
401 CurrentHob = Hob;\r
402 break;\r
403 }\r
404 }\r
405 } \r
406 } \r
407 if (CurrentResourceHob != NULL) {\r
852081fc 408 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress));\r
54ea99a7 409 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE; \r
410 } else {\r
852081fc 411 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress)); \r
54ea99a7 412 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n")); \r
413 //\r
414 // Print the recomended Top address range.\r
415 // \r
416 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
417 //\r
418 // See if this is a resource descriptor HOB\r
419 //\r
420 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
421 \r
422 ResourceHob = Hob.ResourceDescriptor;\r
423 //\r
424 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
425 // \r
426 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
427 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
428 //\r
429 // See if Top address specified by user is valid.\r
430 //\r
431 if (ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
852081fc 432 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n", \r
54ea99a7 433 (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE), \r
434 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE) \r
435 )); \r
436 }\r
437 }\r
438 }\r
439 } \r
440 //\r
441 // Assert here \r
442 //\r
443 ASSERT (FALSE); \r
444 } \r
445 } else {\r
446 //\r
447 // The LMFA feature is enabled as load module at fixed offset relative to TOLM\r
448 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)\r
449 //\r
450 //\r
451 // Search for a tested memory region that is below MAX_ADDRESS\r
452 //\r
453 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
454 //\r
455 // See if this is a resource descriptor HOB \r
456 //\r
457 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
458 \r
459 ResourceHob = Hob.ResourceDescriptor; \r
460 //\r
461 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
462 //\r
463 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && \r
464 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
465 ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
466 //\r
467 // See if this is the highest largest system memory region below MaxAddress\r
468 //\r
469 if (ResourceHob->PhysicalStart > HighAddress) {\r
470 CurrentResourceHob = ResourceHob;\r
471 CurrentHob = Hob;\r
472 HighAddress = CurrentResourceHob->PhysicalStart;\r
473 }\r
474 }\r
475 } \r
476 }\r
477 if (CurrentResourceHob == NULL) {\r
478 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n")); \r
479 //\r
480 // Assert here \r
481 //\r
482 ASSERT (FALSE); \r
483 } else {\r
484 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ; \r
485 } \r
486 }\r
487 \r
488 if (CurrentResourceHob != NULL) {\r
489 //\r
490 // rebuild hob for PEI memmory and reserved memory\r
491 //\r
492 BuildResourceDescriptorHob (\r
493 EFI_RESOURCE_SYSTEM_MEMORY, // MemoryType,\r
494 (\r
495 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
496 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
497 EFI_RESOURCE_ATTRIBUTE_TESTED |\r
498 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
499 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
500 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
501 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
502 ),\r
503 (TopLoadingAddress - TotalReservedMemorySize), // MemoryBegin\r
504 TotalReservedMemorySize // MemoryLength\r
505 );\r
506 //\r
507 // rebuild hob for the remain memory if necessary\r
508 //\r
509 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
510 BuildResourceDescriptorHob (\r
511 EFI_RESOURCE_SYSTEM_MEMORY, // MemoryType,\r
512 (\r
513 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
514 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
515 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
516 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
517 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
518 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
519 ),\r
520 CurrentResourceHob->PhysicalStart, // MemoryBegin\r
521 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart) // MemoryLength\r
522 );\r
523 }\r
524 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {\r
525 BuildResourceDescriptorHob (\r
526 EFI_RESOURCE_SYSTEM_MEMORY, \r
527 (\r
528 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
529 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
530 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
531 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
532 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
533 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
534 ),\r
535 TopLoadingAddress, \r
536 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress) \r
537 );\r
538 }\r
539 //\r
540 // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.\r
541 //\r
542 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED; \r
543 }\r
544\r
545 //\r
546 // Cache the top address for Loading Module at Fixed Address feature\r
547 //\r
548 PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;\r
852081fc 549 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress)); \r
54ea99a7 550 //\r
551 // reinstall the PEI memory relative to TopLoadingAddress\r
552 //\r
553 PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize;\r
554 PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize;\r
555}\r
b1f6a7c6 556/**\r
192f6d4c 557 Conduct PEIM dispatch.\r
558\r
b1f6a7c6 559 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 560 environment, such as the size and location of temporary RAM, the stack location and\r
561 the BFV location.\r
b1f6a7c6 562 @param Private Pointer to the private data passed in from caller\r
192f6d4c 563\r
b1f6a7c6 564**/\r
565VOID\r
566PeiDispatcher (\r
567 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
568 IN PEI_CORE_INSTANCE *Private\r
569 )\r
192f6d4c 570{\r
b0d803fe 571 EFI_STATUS Status;\r
572 UINT32 Index1;\r
573 UINT32 Index2;\r
6c7a807a 574 CONST EFI_PEI_SERVICES **PeiServices;\r
b0d803fe 575 EFI_PEI_FILE_HANDLE PeimFileHandle;\r
576 UINTN FvCount;\r
577 UINTN PeimCount;\r
578 UINT32 AuthenticationState;\r
579 EFI_PHYSICAL_ADDRESS EntryPoint;\r
797a9d67 580 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;\r
b0d803fe 581 UINTN SaveCurrentPeimCount;\r
1053e0c5 582 UINTN SaveCurrentFvCount;\r
b0d803fe 583 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;\r
797a9d67 584 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;\r
58dcdada 585 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;\r
586 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;\r
587 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;\r
588 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;\r
66c69dea 589 INTN StackOffset;\r
590 INTN HeapOffset;\r
58dcdada 591 PEI_CORE_INSTANCE *PrivateInMem;\r
592 UINT64 NewPeiStackSize;\r
593 UINT64 OldPeiStackSize;\r
594 UINT64 StackGap;\r
288f9b38 595 EFI_FV_FILE_INFO FvFileInfo;\r
58dcdada 596 UINTN OldCheckingTop;\r
597 UINTN OldCheckingBottom;\r
3b428ade 598 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
54ea99a7 599 VOID *LoadFixPeiCodeBegin;\r
b0d803fe 600\r
4140a663 601 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
b0d803fe 602 PeimEntryPoint = NULL;\r
603 PeimFileHandle = NULL;\r
288f9b38 604 EntryPoint = 0;\r
b0d803fe 605\r
606 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
607 //\r
608 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
609 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
610 //\r
611 SaveCurrentPeimCount = Private->CurrentPeimCount;\r
1053e0c5 612 SaveCurrentFvCount = Private->CurrentPeimFvCount;\r
b0d803fe 613 SaveCurrentFileHandle = Private->CurrentFileHandle;\r
614\r
1053e0c5 615 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
4140a663 616 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
b0d803fe 617 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
58dcdada 618 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
b0d803fe 619 Status = PeiLoadImage (\r
4140a663 620 (CONST EFI_PEI_SERVICES **) &Private->Ps,\r
58dcdada 621 PeimFileHandle,\r
341a658f 622 PEIM_STATE_REGISITER_FOR_SHADOW,\r
58dcdada 623 &EntryPoint,\r
b0d803fe 624 &AuthenticationState\r
625 );\r
626 if (Status == EFI_SUCCESS) {\r
627 //\r
628 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
629 //\r
630 Private->Fv[Index1].PeimState[Index2]++;\r
1053e0c5 631 Private->CurrentFileHandle = PeimFileHandle;\r
58dcdada 632 Private->CurrentPeimFvCount = Index1;\r
633 Private->CurrentPeimCount = Index2;\r
b0d803fe 634 //\r
635 // Call the PEIM entry point\r
636 //\r
797a9d67 637 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
58dcdada 638\r
087e13cb 639 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
4140a663 640 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);\r
087e13cb 641 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
58dcdada 642 }\r
643\r
b0d803fe 644 //\r
645 // Process the Notify list and dispatch any notifies for\r
646 // newly installed PPIs.\r
647 //\r
648 ProcessNotifyList (Private);\r
649 }\r
650 }\r
651 }\r
58dcdada 652 Private->CurrentFileHandle = SaveCurrentFileHandle;\r
653 Private->CurrentPeimFvCount = SaveCurrentFvCount;\r
654 Private->CurrentPeimCount = SaveCurrentPeimCount;\r
b0d803fe 655 }\r
192f6d4c 656\r
657 //\r
658 // This is the main dispatch loop. It will search known FVs for PEIMs and\r
659 // attempt to dispatch them. If any PEIM gets dispatched through a single\r
660 // pass of the dispatcher, it will start over from the Bfv again to see\r
661 // if any new PEIMs dependencies got satisfied. With a well ordered\r
662 // FV where PEIMs are found in the order their dependencies are also\r
663 // satisfied, this dipatcher should run only once.\r
664 //\r
b0d803fe 665 do {\r
82b8c8df 666 //\r
667 // In case that reenter PeiCore happens, the last pass record is still available. \r
668 //\r
669 if (!Private->PeimDispatcherReenter) {\r
670 Private->PeimNeedingDispatch = FALSE;\r
671 Private->PeimDispatchOnThisPass = FALSE;\r
672 } else {\r
673 Private->PeimDispatcherReenter = FALSE;\r
674 }\r
675 \r
b0d803fe 676 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
3b428ade 677 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
678 ASSERT (CoreFvHandle != NULL);\r
679 \r
2a00326e 680 //\r
3b428ade 681 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
2a00326e 682 //\r
3b428ade 683 if (CoreFvHandle->FvPpi == NULL) {\r
684 continue;\r
685 }\r
686 \r
687 Private->CurrentPeimFvCount = FvCount;\r
192f6d4c 688\r
b0d803fe 689 if (Private->CurrentPeimCount == 0) {\r
690 //\r
691 // When going through each FV, at first, search Apriori file to\r
58dcdada 692 // reorder all PEIMs to ensure the PEIMs in Apriori file to get\r
b0d803fe 693 // dispatch at first.\r
694 //\r
3b428ade 695 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);\r
b0d803fe 696 }\r
192f6d4c 697\r
698 //\r
b0d803fe 699 // Start to dispatch all modules within the current Fv.\r
192f6d4c 700 //\r
58dcdada 701 for (PeimCount = Private->CurrentPeimCount;\r
4140a663 702 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
b0d803fe 703 PeimCount++) {\r
704 Private->CurrentPeimCount = PeimCount;\r
705 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
706\r
707 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {\r
708 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {\r
82b8c8df 709 Private->PeimNeedingDispatch = TRUE;\r
b0d803fe 710 } else {\r
3b428ade 711 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);\r
288f9b38
LG
712 ASSERT_EFI_ERROR (Status);\r
713 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
714 //\r
715 // For Fv type file, Produce new FV PPI and FV hob\r
716 //\r
3b428ade 717 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);\r
718 AuthenticationState = 0;\r
288f9b38
LG
719 } else {\r
720 //\r
721 // For PEIM driver, Load its entry point\r
722 //\r
723 Status = PeiLoadImage (\r
58dcdada 724 PeiServices,\r
725 PeimFileHandle,\r
341a658f 726 PEIM_STATE_NOT_DISPATCHED,\r
58dcdada 727 &EntryPoint,\r
288f9b38
LG
728 &AuthenticationState\r
729 );\r
730 }\r
731\r
b0d803fe 732 if ((Status == EFI_SUCCESS)) {\r
192f6d4c 733 //\r
58dcdada 734 // The PEIM has its dependencies satisfied, and its entry point\r
735 // has been found, so invoke it.\r
192f6d4c 736 //\r
087e13cb 737 PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
192f6d4c 738\r
b0d803fe 739 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;\r
192f6d4c 740\r
741 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
742 EFI_PROGRESS_CODE,\r
f9876ecf 743 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
192f6d4c 744 (VOID *)(&ExtendedData),\r
745 sizeof (ExtendedData)\r
746 );\r
747\r
3b428ade 748 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);\r
b0d803fe 749 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {\r
750 //\r
751 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED\r
752 //\r
753 Private->Fv[FvCount].PeimState[PeimCount]++;\r
58dcdada 754\r
288f9b38
LG
755 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
756 //\r
757 // Call the PEIM entry point for PEIM driver\r
758 //\r
797a9d67 759 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
760 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
288f9b38 761 }\r
797a9d67 762\r
82b8c8df 763 Private->PeimDispatchOnThisPass = TRUE;\r
192f6d4c 764 }\r
765\r
766 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
767 EFI_PROGRESS_CODE,\r
f9876ecf 768 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),\r
192f6d4c 769 (VOID *)(&ExtendedData),\r
770 sizeof (ExtendedData)\r
771 );\r
087e13cb 772 PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
b0d803fe 773\r
58dcdada 774 }\r
775\r
776 if (Private->SwitchStackSignal) {\r
a7715e73 777 //\r
3d4d0c34 778 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack\r
a7715e73 779 // usage in temporary memory for debuging.\r
780 //\r
781 DEBUG_CODE_BEGIN ();\r
96317468 782 UINT32 *StackPointer;\r
a7715e73 783 \r
96317468 784 for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
785 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
a7715e73 786 && (*StackPointer == INIT_CAR_VALUE);\r
787 StackPointer ++);\r
788 \r
3d4d0c34 789 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
790 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",\r
a7715e73 791 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
792 ));\r
3d4d0c34 793 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",\r
a7715e73 794 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
795 (UINTN) Private->HobList.Raw)\r
796 ));\r
797 DEBUG_CODE_END ();\r
798 \r
f73b3df8 799 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 800 //\r
801 // Loading Module at Fixed Address is enabled\r
802 //\r
803 PeiLoadFixAddressHook(Private);\r
4fb72076 804 //\r
805 // if Loading Module at Fixed Address is enabled, This is the first invoke to page\r
806 // allocation for Pei Code range. This memory range should be reserved for loading PEIMs\r
807 //\r
808 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
3978f5d9 809 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
54ea99a7 810 }\r
811 \r
a3a15d21 812 //\r
58dcdada 813 // Reserve the size of new stack at bottom of physical memory\r
a3a15d21 814 //\r
63b62331 815 OldPeiStackSize = (UINT64) SecCoreData->StackSize;\r
58dcdada 816 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
f4391d63 817 if (PcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
58dcdada 818 Private->StackSize = NewPeiStackSize;\r
819 } else {\r
f4391d63 820 Private->StackSize = PcdGet32(PcdPeiCoreMaxPeiStackSize);\r
58dcdada 821 }\r
192f6d4c 822\r
58dcdada 823 //\r
824 // In theory, the size of new stack in permenent memory should large than\r
825 // size of old stack in temporary memory.\r
826 // But if new stack is smaller than the size of old stack, we also reserve\r
827 // the size of old stack at bottom of permenent memory.\r
828 //\r
90e128e2 829 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));\r
28127cc7
LG
830 ASSERT (Private->StackSize >= OldPeiStackSize);\r
831 StackGap = Private->StackSize - OldPeiStackSize;\r
d74eeda8 832\r
58dcdada 833 //\r
834 // Update HandOffHob for new installed permenent memory\r
835 //\r
836 OldHandOffTable = Private->HobList.HandoffInformationTable;\r
5c5a0601 837 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
58dcdada 838 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
192f6d4c 839\r
840 //\r
58dcdada 841 // The whole temporary memory will be migrated to physical memory.\r
842 // CAUTION: The new base is computed accounding to gap of new stack.\r
192f6d4c 843 //\r
58dcdada 844 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
40f26b8f 845 \r
846 //\r
3d4d0c34 847 // Caculate stack offset and heap offset between temporary memory and new permement \r
40f26b8f 848 // memory seperately.\r
849 //\r
d74eeda8 850 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
66c69dea 851 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
852 (UINTN) SecCoreData->PeiTemporaryRamBase);\r
7df7393f 853 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));\r
66c69dea 854 \r
40f26b8f 855 //\r
856 // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
857 //\r
66c69dea 858 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
859 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
192f6d4c 860\r
861 //\r
58dcdada 862 // TemporaryRamSupportPpi is produced by platform's SEC\r
192f6d4c 863 //\r
58dcdada 864 Status = PeiLocatePpi (\r
865 (CONST EFI_PEI_SERVICES **) PeiServices,\r
866 &gEfiTemporaryRamSupportPpiGuid,\r
867 0,\r
868 NULL,\r
869 (VOID**)&TemporaryRamSupportPpi\r
870 );\r
871\r
81c7803c 872\r
58dcdada 873 if (!EFI_ERROR (Status)) {\r
40f26b8f 874 //\r
875 // Temporary Ram support Ppi is provided by platform, it will copy \r
876 // temporary memory to permenent memory and do stack switching.\r
877 // After invoken temporary Ram support, following code's stack is in \r
3d4d0c34 878 // memory but not in temporary memory.\r
40f26b8f 879 //\r
58dcdada 880 TemporaryRamSupportPpi->TemporaryRamMigration (\r
881 (CONST EFI_PEI_SERVICES **) PeiServices,\r
882 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
883 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
884 SecCoreData->TemporaryRamSize\r
885 );\r
886\r
887 } else {\r
b414ea4b 888 //\r
889 // In IA32/x64/Itanium architecture, we need platform provide\r
890 // TEMPORAY_RAM_MIGRATION_PPI.\r
891 //\r
892 ASSERT (FALSE);\r
58dcdada 893 }\r
894\r
895\r
896 //\r
b0d803fe 897 //\r
58dcdada 898 // Fixup the PeiCore's private data\r
b0d803fe 899 //\r
4140a663 900 PrivateInMem->Ps = &PrivateInMem->ServiceTableShadow;\r
58dcdada 901 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;\r
66c69dea 902 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
58dcdada 903 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
904\r
4140a663 905 PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->Ps;\r
58dcdada 906\r
907 //\r
908 // Fixup for PeiService's address\r
909 //\r
910 SetPeiServicesTablePointer(PeiServices);\r
911\r
912 //\r
913 // Update HandOffHob for new installed permenent memory\r
914 //\r
915 NewHandOffTable->EfiEndOfHobList =\r
66c69dea 916 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
58dcdada 917 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +\r
918 PrivateInMem->PhysicalMemoryLength;\r
919 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;\r
920 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;\r
921 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
922 sizeof (EFI_HOB_GENERIC_HEADER);\r
923\r
924 //\r
925 // We need convert the PPI desciptor's pointer\r
926 //\r
40f26b8f 927 ConvertPpiPointers (PrivateInMem, \r
58dcdada 928 OldCheckingBottom, \r
929 OldCheckingTop, \r
5c5a0601 930 HeapOffset\r
931 );\r
58dcdada 932\r
7df7393f 933 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",\r
934 PrivateInMem->StackBase,\r
58dcdada 935 PrivateInMem->StackSize));\r
936 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
937\r
938 //\r
939 // After the whole temporary memory is migrated, then we can allocate page in\r
940 // permenent memory.\r
941 //\r
942 PrivateInMem->PeiMemoryInstalled = TRUE;\r
943\r
1bd90a4c 944 //\r
82b8c8df 945 // Indicate that PeiCore reenter\r
1bd90a4c 946 //\r
82b8c8df 947 PrivateInMem->PeimDispatcherReenter = TRUE;\r
948 \r
def75b5d 949 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 950 //\r
951 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.\r
4fb72076 952 // Every bit in the array indicate the status of the corresponding memory page available or not\r
54ea99a7 953 //\r
954 PrivateInMem->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));\r
955 }\r
192f6d4c 956 //\r
b0d803fe 957 // Shadow PEI Core. When permanent memory is avaiable, shadow\r
958 // PEI Core and PEIMs to get high performance.\r
192f6d4c 959 //\r
58dcdada 960 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
961 PeiServices,\r
962 PrivateInMem\r
963 );\r
b0d803fe 964 //\r
58dcdada 965 // Process the Notify list and dispatch any notifies for\r
966 // newly installed PPIs.\r
b0d803fe 967 //\r
58dcdada 968 ProcessNotifyList (PrivateInMem);\r
969\r
b0d803fe 970 //\r
58dcdada 971 // Entry PEI Phase 2\r
b0d803fe 972 //\r
58dcdada 973 PeiCore (SecCoreData, NULL, PrivateInMem);\r
b0d803fe 974\r
58dcdada 975 //\r
976 // Code should not come here\r
977 //\r
978 ASSERT_EFI_ERROR(FALSE);\r
192f6d4c 979 }\r
192f6d4c 980\r
58dcdada 981 //\r
982 // Process the Notify list and dispatch any notifies for\r
983 // newly installed PPIs.\r
984 //\r
985 ProcessNotifyList (Private);\r
986\r
b0d803fe 987 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \\r
988 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
989 //\r
58dcdada 990 // If memory is availble we shadow images by default for performance reasons.\r
991 // We call the entry point a 2nd time so the module knows it's shadowed.\r
b0d803fe 992 //\r
993 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);\r
e67ca95c 994 ASSERT (PeimEntryPoint != NULL);\r
797a9d67 995 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);\r
b0d803fe 996 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
58dcdada 997\r
b0d803fe 998 //\r
999 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
1000 //\r
1001 Private->Fv[FvCount].PeimState[PeimCount]++;\r
192f6d4c 1002\r
192f6d4c 1003 //\r
b0d803fe 1004 // Process the Notify list and dispatch any notifies for\r
1005 // newly installed PPIs.\r
192f6d4c 1006 //\r
b0d803fe 1007 ProcessNotifyList (Private);\r
192f6d4c 1008 }\r
1009 }\r
1010 }\r
192f6d4c 1011 }\r
192f6d4c 1012\r
b0d803fe 1013 //\r
1014 // We set to NULL here to optimize the 2nd entry to this routine after\r
1015 // memory is found. This reprevents rescanning of the FV. We set to\r
1016 // NULL here so we start at the begining of the next FV\r
1017 //\r
1018 Private->CurrentFileHandle = NULL;\r
1019 Private->CurrentPeimCount = 0;\r
1020 //\r
1021 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
1022 //\r
1023 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);\r
192f6d4c 1024 }\r
1025\r
1026 //\r
58dcdada 1027 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
b0d803fe 1028 // through all the FV.\r
192f6d4c 1029 //\r
b0d803fe 1030 Private->CurrentPeimFvCount = 0;\r
192f6d4c 1031\r
1032 //\r
58dcdada 1033 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get\r
b0d803fe 1034 // dispatched. So we need to make another pass\r
192f6d4c 1035 //\r
58dcdada 1036 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this\r
b0d803fe 1037 // pass. If we did not dispatch a PEIM there is no point in trying again\r
1038 // as it will fail the next time too (nothing has changed).\r
192f6d4c 1039 //\r
82b8c8df 1040 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);\r
192f6d4c 1041\r
192f6d4c 1042}\r
1043\r
b1f6a7c6 1044/**\r
192f6d4c 1045 Initialize the Dispatcher's data members\r
1046\r
b1f6a7c6 1047 @param PrivateData PeiCore's private data structure\r
1048 @param OldCoreData Old data from SecCore\r
192f6d4c 1049 NULL if being run in non-permament memory mode.\r
b1f6a7c6 1050 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 1051 environment, such as the size and location of temporary RAM, the stack location and\r
1052 the BFV location.\r
192f6d4c 1053\r
b1f6a7c6 1054 @return None.\r
192f6d4c 1055\r
b1f6a7c6 1056**/\r
1057VOID\r
1058InitializeDispatcherData (\r
1059 IN PEI_CORE_INSTANCE *PrivateData,\r
1060 IN PEI_CORE_INSTANCE *OldCoreData,\r
1061 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
1062 )\r
192f6d4c 1063{\r
192f6d4c 1064 if (OldCoreData == NULL) {\r
82b8c8df 1065 PrivateData->PeimDispatcherReenter = FALSE;\r
b0d803fe 1066 PeiInitializeFv (PrivateData, SecCoreData);\r
8e0e40ed 1067 } else {\r
7ec93917 1068 PeiReinitializeFv (PrivateData);\r
192f6d4c 1069 }\r
1070\r
1071 return;\r
1072}\r
1073\r
b1f6a7c6 1074/**\r
1075 This routine parses the Dependency Expression, if available, and\r
1076 decides if the module can be executed.\r
1077\r
1078\r
1079 @param Private PeiCore's private data structure\r
1080 @param FileHandle PEIM's file handle\r
1081 @param PeimCount Peim count in all dispatched PEIMs.\r
192f6d4c 1082\r
b1f6a7c6 1083 @retval TRUE Can be dispatched\r
1084 @retval FALSE Cannot be dispatched\r
1085\r
1086**/\r
192f6d4c 1087BOOLEAN\r
1088DepexSatisfied (\r
b0d803fe 1089 IN PEI_CORE_INSTANCE *Private,\r
1090 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1091 IN UINTN PeimCount\r
192f6d4c 1092 )\r
192f6d4c 1093{\r
288f9b38
LG
1094 EFI_STATUS Status;\r
1095 VOID *DepexData;\r
b0d803fe 1096\r
1097 if (PeimCount < Private->AprioriCount) {\r
1098 //\r
1099 // If its in the A priori file then we set Depex to TRUE\r
1100 //\r
1101 return TRUE;\r
1102 }\r
58dcdada 1103\r
288f9b38 1104 //\r
58dcdada 1105 // Depex section not in the encapsulated section.\r
288f9b38
LG
1106 //\r
1107 Status = PeiServicesFfsFindSectionData (\r
1108 EFI_SECTION_PEI_DEPEX,\r
58dcdada 1109 FileHandle,\r
288f9b38
LG
1110 (VOID **)&DepexData\r
1111 );\r
b0d803fe 1112\r
192f6d4c 1113 if (EFI_ERROR (Status)) {\r
b0d803fe 1114 //\r
1115 // If there is no DEPEX, assume the module can be executed\r
1116 //\r
192f6d4c 1117 return TRUE;\r
1118 }\r
1119\r
1120 //\r
1121 // Evaluate a given DEPEX\r
1122 //\r
4140a663 1123 return PeimDispatchReadiness (&Private->Ps, DepexData);\r
192f6d4c 1124}\r
1125\r
14e8823a 1126/**\r
1127 This routine enable a PEIM to register itself to shadow when PEI Foundation\r
1128 discovery permanent memory.\r
1129\r
b1f6a7c6 1130 @param FileHandle File handle of a PEIM.\r
58dcdada 1131\r
b1f6a7c6 1132 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.\r
1133 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.\r
1134 @retval EFI_SUCCESS Successfully to register itself.\r
14e8823a 1135\r
58dcdada 1136**/\r
14e8823a 1137EFI_STATUS\r
1138EFIAPI\r
1139PeiRegisterForShadow (\r
1140 IN EFI_PEI_FILE_HANDLE FileHandle\r
1141 )\r
1142{\r
1143 PEI_CORE_INSTANCE *Private;\r
1144 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
1145\r
1146 if (Private->CurrentFileHandle != FileHandle) {\r
1147 //\r
1148 // The FileHandle must be for the current PEIM\r
1149 //\r
1150 return EFI_NOT_FOUND;\r
1151 }\r
1152\r
1153 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
1154 //\r
1155 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
1156 //\r
1157 return EFI_ALREADY_STARTED;\r
1158 }\r
58dcdada 1159\r
14e8823a 1160 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
1161\r
1162 return EFI_SUCCESS;\r
1163}\r
1164\r
3b428ade 1165\r
341a658f 1166\r