]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
After PeiCore is shadowed into permanent memory, the pointers to build-in Ffs2 FvPpi...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
1 /** @file
2 EFI PEI Core dispatch services
3
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PeiMain.h"
16
17 ///
18 /// temporary memory is filled with this initial value during SEC phase
19 ///
20 #define INIT_CAR_VALUE 0x5AA55AA5
21
22 typedef struct {
23 EFI_STATUS_CODE_DATA DataHeader;
24 EFI_HANDLE Handle;
25 } PEIM_FILE_HANDLE_EXTENDED_DATA;
26
27 /**
28
29 Discover all Peims and optional Apriori file in one FV. There is at most one
30 Apriori file in one FV.
31
32
33 @param Private Pointer to the private data passed in from caller
34 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.
35
36 **/
37 VOID
38 DiscoverPeimsAndOrderWithApriori (
39 IN PEI_CORE_INSTANCE *Private,
40 IN PEI_CORE_FV_HANDLE *CoreFileHandle
41 )
42 {
43 EFI_STATUS Status;
44 EFI_PEI_FV_HANDLE FileHandle;
45 EFI_PEI_FILE_HANDLE AprioriFileHandle;
46 EFI_GUID *Apriori;
47 UINTN Index;
48 UINTN Index2;
49 UINTN PeimIndex;
50 UINTN PeimCount;
51 EFI_GUID *Guid;
52 EFI_PEI_FV_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];
54 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
55 EFI_FV_FILE_INFO FileInfo;
56
57 FvPpi = CoreFileHandle->FvPpi;
58
59 //
60 // Walk the FV and find all the PEIMs and the Apriori file.
61 //
62 AprioriFileHandle = NULL;
63 Private->CurrentFvFileHandles[0] = NULL;
64 Guid = NULL;
65 FileHandle = NULL;
66
67 //
68 // If the current Fv has been scanned, directly get its cachable record.
69 //
70 if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {
71 CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (Private->CurrentFvFileHandles));
72 return;
73 }
74
75 //
76 // Go ahead to scan this Fv, and cache FileHandles within it.
77 //
78 for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {
79 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);
80 if (Status != EFI_SUCCESS) {
81 break;
82 }
83
84 Private->CurrentFvFileHandles[PeimCount] = FileHandle;
85 }
86
87 //
88 // Check whether the count of Peims exceeds the max support PEIMs in a FV image
89 // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.
90 //
91 ASSERT (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv));
92
93 //
94 // Get Apriori File handle
95 //
96 Private->AprioriCount = 0;
97 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);
98 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {
99 //
100 // Read the Apriori file
101 //
102 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);
103 if (!EFI_ERROR (Status)) {
104 //
105 // Calculate the number of PEIMs in the A Priori list
106 //
107 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);
108 ASSERT_EFI_ERROR (Status);
109 Private->AprioriCount = FileInfo.BufferSize & 0x00FFFFFF;
110 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);
111 Private->AprioriCount /= sizeof (EFI_GUID);
112
113 ZeroMem (FileGuid, sizeof (FileGuid));
114 for (Index = 0; Index < PeimCount; Index++) {
115 //
116 // Make an array of file name guids that matches the FileHandle array so we can convert
117 // quickly from file name to file handle
118 //
119 Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);
120 CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));
121 }
122
123 //
124 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.
125 // Add avalible PEIMs in Apriori file into TempFileHandles array at first.
126 //
127 Index2 = 0;
128 for (Index = 0; Index2 < Private->AprioriCount; Index++) {
129 while (Index2 < Private->AprioriCount) {
130 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);
131 if (Guid != NULL) {
132 break;
133 }
134 }
135 if (Guid == NULL) {
136 break;
137 }
138 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);
139 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];
140
141 //
142 // Since we have copied the file handle we can remove it from this list.
143 //
144 Private->CurrentFvFileHandles[PeimIndex] = NULL;
145 }
146
147 //
148 // Update valid Aprioricount
149 //
150 Private->AprioriCount = Index;
151
152 //
153 // Add in any PEIMs not in the Apriori file
154 //
155 for (;Index < PeimCount; Index++) {
156 for (Index2 = 0; Index2 < PeimCount; Index2++) {
157 if (Private->CurrentFvFileHandles[Index2] != NULL) {
158 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];
159 Private->CurrentFvFileHandles[Index2] = NULL;
160 break;
161 }
162 }
163 }
164 //
165 //Index the end of array contains re-range Pei moudle.
166 //
167 TempFileHandles[Index] = NULL;
168
169 //
170 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.
171 // We need to update it to start with files in the A Priori list and
172 // then the remaining files in PEIM order.
173 //
174 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));
175 }
176 }
177 //
178 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.
179 // Instead, we can retrieve the file handles within this Fv from cachable data.
180 //
181 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;
182 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));
183
184 }
185
186 /**
187 Shadow PeiCore module from flash to installed memory.
188
189 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
190 @param PrivateInMem PeiCore's private data structure
191
192 @return PeiCore function address after shadowing.
193 **/
194 VOID*
195 ShadowPeiCore(
196 IN CONST EFI_PEI_SERVICES **PeiServices,
197 IN PEI_CORE_INSTANCE *PrivateInMem
198 )
199 {
200 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;
201 EFI_PHYSICAL_ADDRESS EntryPoint;
202 EFI_STATUS Status;
203 UINT32 AuthenticationState;
204
205 PeiCoreFileHandle = NULL;
206
207 //
208 // Find the PEI Core in the BFV
209 //
210 Status = PrivateInMem->Fv[0].FvPpi->FindFileByType (
211 PrivateInMem->Fv[0].FvPpi,
212 EFI_FV_FILETYPE_PEI_CORE,
213 PrivateInMem->Fv[0].FvHandle,
214 &PeiCoreFileHandle
215 );
216 ASSERT_EFI_ERROR (Status);
217
218 //
219 // Shadow PEI Core into memory so it will run faster
220 //
221 Status = PeiLoadImage (
222 PeiServices,
223 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),
224 &EntryPoint,
225 &AuthenticationState
226 );
227 ASSERT_EFI_ERROR (Status);
228
229 //
230 // Compute the PeiCore's function address after shaowed PeiCore.
231 // _ModuleEntryPoint is PeiCore main function entry
232 //
233 return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);
234 }
235
236 /**
237 Conduct PEIM dispatch.
238
239 @param SecCoreData Points to a data structure containing information about the PEI core's operating
240 environment, such as the size and location of temporary RAM, the stack location and
241 the BFV location.
242 @param Private Pointer to the private data passed in from caller
243
244 **/
245 VOID
246 PeiDispatcher (
247 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
248 IN PEI_CORE_INSTANCE *Private
249 )
250 {
251 EFI_STATUS Status;
252 UINT32 Index1;
253 UINT32 Index2;
254 CONST EFI_PEI_SERVICES **PeiServices;
255 EFI_PEI_FILE_HANDLE PeimFileHandle;
256 UINTN FvCount;
257 UINTN PeimCount;
258 UINT32 AuthenticationState;
259 EFI_PHYSICAL_ADDRESS EntryPoint;
260 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;
261 UINTN SaveCurrentPeimCount;
262 UINTN SaveCurrentFvCount;
263 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;
264 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;
265 EFI_PHYSICAL_ADDRESS NewPermenentMemoryBase;
266 TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;
267 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffTable;
268 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffTable;
269 INTN StackOffset;
270 INTN HeapOffset;
271 PEI_CORE_INSTANCE *PrivateInMem;
272 UINT64 NewPeiStackSize;
273 UINT64 OldPeiStackSize;
274 UINT64 StackGap;
275 EFI_FV_FILE_INFO FvFileInfo;
276 UINTN OldCheckingTop;
277 UINTN OldCheckingBottom;
278 PEI_CORE_FV_HANDLE *CoreFvHandle;
279
280 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->PS;
281 PeimEntryPoint = NULL;
282 PeimFileHandle = NULL;
283 EntryPoint = 0;
284
285 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
286 //
287 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile
288 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.
289 //
290 SaveCurrentPeimCount = Private->CurrentPeimCount;
291 SaveCurrentFvCount = Private->CurrentPeimFvCount;
292 SaveCurrentFileHandle = Private->CurrentFileHandle;
293
294 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {
295 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {
296 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {
297 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];
298 Status = PeiLoadImage (
299 (CONST EFI_PEI_SERVICES **) &Private->PS,
300 PeimFileHandle,
301 &EntryPoint,
302 &AuthenticationState
303 );
304 if (Status == EFI_SUCCESS) {
305 //
306 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
307 //
308 Private->Fv[Index1].PeimState[Index2]++;
309 Private->CurrentFileHandle = PeimFileHandle;
310 Private->CurrentPeimFvCount = Index1;
311 Private->CurrentPeimCount = Index2;
312 //
313 // Call the PEIM entry point
314 //
315 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
316
317 PERF_START (0, "PEIM", NULL, 0);
318 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->PS);
319 PERF_END (0, "PEIM", NULL, 0);
320 }
321
322 //
323 // Process the Notify list and dispatch any notifies for
324 // newly installed PPIs.
325 //
326 ProcessNotifyList (Private);
327 }
328 }
329 }
330 Private->CurrentFileHandle = SaveCurrentFileHandle;
331 Private->CurrentPeimFvCount = SaveCurrentFvCount;
332 Private->CurrentPeimCount = SaveCurrentPeimCount;
333 }
334
335 //
336 // This is the main dispatch loop. It will search known FVs for PEIMs and
337 // attempt to dispatch them. If any PEIM gets dispatched through a single
338 // pass of the dispatcher, it will start over from the Bfv again to see
339 // if any new PEIMs dependencies got satisfied. With a well ordered
340 // FV where PEIMs are found in the order their dependencies are also
341 // satisfied, this dipatcher should run only once.
342 //
343 do {
344 //
345 // In case that reenter PeiCore happens, the last pass record is still available.
346 //
347 if (!Private->PeimDispatcherReenter) {
348 Private->PeimNeedingDispatch = FALSE;
349 Private->PeimDispatchOnThisPass = FALSE;
350 } else {
351 Private->PeimDispatcherReenter = FALSE;
352 }
353
354 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {
355 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);
356 ASSERT (CoreFvHandle != NULL);
357
358 //
359 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.
360 //
361 if (CoreFvHandle->FvPpi == NULL) {
362 continue;
363 }
364
365 Private->CurrentPeimFvCount = FvCount;
366
367 if (Private->CurrentPeimCount == 0) {
368 //
369 // When going through each FV, at first, search Apriori file to
370 // reorder all PEIMs to ensure the PEIMs in Apriori file to get
371 // dispatch at first.
372 //
373 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);
374 }
375
376 //
377 // Start to dispatch all modules within the current Fv.
378 //
379 for (PeimCount = Private->CurrentPeimCount;
380 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);
381 PeimCount++) {
382 Private->CurrentPeimCount = PeimCount;
383 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];
384
385 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {
386 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {
387 Private->PeimNeedingDispatch = TRUE;
388 } else {
389 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);
390 ASSERT_EFI_ERROR (Status);
391 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
392 //
393 // For Fv type file, Produce new FV PPI and FV hob
394 //
395 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);
396 AuthenticationState = 0;
397 } else {
398 //
399 // For PEIM driver, Load its entry point
400 //
401 Status = PeiLoadImage (
402 PeiServices,
403 PeimFileHandle,
404 &EntryPoint,
405 &AuthenticationState
406 );
407 }
408
409 if ((Status == EFI_SUCCESS)) {
410 //
411 // The PEIM has its dependencies satisfied, and its entry point
412 // has been found, so invoke it.
413 //
414 PERF_START (0, "PEIM", NULL, 0);
415
416 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;
417
418 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
419 EFI_PROGRESS_CODE,
420 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),
421 (VOID *)(&ExtendedData),
422 sizeof (ExtendedData)
423 );
424
425 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);
426 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {
427 //
428 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
429 //
430 Private->Fv[FvCount].PeimState[PeimCount]++;
431
432 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
433 //
434 // Call the PEIM entry point for PEIM driver
435 //
436 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
437 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);
438 }
439
440 Private->PeimDispatchOnThisPass = TRUE;
441 }
442
443 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
444 EFI_PROGRESS_CODE,
445 FixedPcdGet32(PcdStatusCodeValuePeimDispatch),
446 (VOID *)(&ExtendedData),
447 sizeof (ExtendedData)
448 );
449 PERF_END (0, "PEIM", NULL, 0);
450
451 }
452
453 if (Private->SwitchStackSignal) {
454 //
455 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack
456 // usage in temporary memory for debuging.
457 //
458 DEBUG_CODE_BEGIN ();
459 UINT32 *StackPointer;
460
461 for (StackPointer = (UINT32*)SecCoreData->StackBase;
462 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \
463 && (*StackPointer == INIT_CAR_VALUE);
464 StackPointer ++);
465
466 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));
467 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",
468 (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))
469 ));
470 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",
471 ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -
472 (UINTN) Private->HobList.Raw)
473 ));
474 DEBUG_CODE_END ();
475
476 //
477 // Reserve the size of new stack at bottom of physical memory
478 //
479 OldPeiStackSize = (UINT64) SecCoreData->StackSize;
480 NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
481 if (FixedPcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {
482 Private->StackSize = NewPeiStackSize;
483 } else {
484 Private->StackSize = FixedPcdGet32(PcdPeiCoreMaxPeiStackSize);
485 }
486
487 //
488 // In theory, the size of new stack in permenent memory should large than
489 // size of old stack in temporary memory.
490 // But if new stack is smaller than the size of old stack, we also reserve
491 // the size of old stack at bottom of permenent memory.
492 //
493 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));
494 ASSERT (Private->StackSize >= OldPeiStackSize);
495 StackGap = Private->StackSize - OldPeiStackSize;
496
497 //
498 // Update HandOffHob for new installed permenent memory
499 //
500 OldHandOffTable = Private->HobList.HandoffInformationTable;
501 OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);
502 OldCheckingTop = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);
503
504 //
505 // The whole temporary memory will be migrated to physical memory.
506 // CAUTION: The new base is computed accounding to gap of new stack.
507 //
508 NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;
509
510 //
511 // Caculate stack offset and heap offset between temporary memory and new permement
512 // memory seperately.
513 //
514 StackOffset = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;
515 HeapOffset = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \
516 (UINTN) SecCoreData->PeiTemporaryRamBase);
517 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));
518
519 //
520 // Caculate new HandOffTable and PrivateData address in permenet memory's stack
521 //
522 NewHandOffTable = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);
523 PrivateInMem = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);
524
525 //
526 // TemporaryRamSupportPpi is produced by platform's SEC
527 //
528 Status = PeiLocatePpi (
529 (CONST EFI_PEI_SERVICES **) PeiServices,
530 &gEfiTemporaryRamSupportPpiGuid,
531 0,
532 NULL,
533 (VOID**)&TemporaryRamSupportPpi
534 );
535
536
537 if (!EFI_ERROR (Status)) {
538 //
539 // Temporary Ram support Ppi is provided by platform, it will copy
540 // temporary memory to permenent memory and do stack switching.
541 // After invoken temporary Ram support, following code's stack is in
542 // memory but not in temporary memory.
543 //
544 TemporaryRamSupportPpi->TemporaryRamMigration (
545 (CONST EFI_PEI_SERVICES **) PeiServices,
546 (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,
547 (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,
548 SecCoreData->TemporaryRamSize
549 );
550
551 } else {
552 //
553 // In IA32/x64/Itanium architecture, we need platform provide
554 // TEMPORAY_RAM_MIGRATION_PPI.
555 //
556 ASSERT (FALSE);
557 }
558
559
560 //
561 //
562 // Fixup the PeiCore's private data
563 //
564 PrivateInMem->PS = &PrivateInMem->ServiceTableShadow;
565 PrivateInMem->CpuIo = &PrivateInMem->ServiceTableShadow.CpuIo;
566 PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);
567 PrivateInMem->StackBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);
568
569 PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->PS;
570
571 //
572 // Fixup for PeiService's address
573 //
574 SetPeiServicesTablePointer(PeiServices);
575
576 //
577 // Update HandOffHob for new installed permenent memory
578 //
579 NewHandOffTable->EfiEndOfHobList =
580 (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);
581 NewHandOffTable->EfiMemoryTop = PrivateInMem->PhysicalMemoryBegin +
582 PrivateInMem->PhysicalMemoryLength;
583 NewHandOffTable->EfiMemoryBottom = PrivateInMem->PhysicalMemoryBegin;
584 NewHandOffTable->EfiFreeMemoryTop = PrivateInMem->FreePhysicalMemoryTop;
585 NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +
586 sizeof (EFI_HOB_GENERIC_HEADER);
587
588 //
589 // We need convert the PPI desciptor's pointer
590 //
591 ConvertPpiPointers (PrivateInMem,
592 OldCheckingBottom,
593 OldCheckingTop,
594 HeapOffset
595 );
596
597 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",
598 PrivateInMem->StackBase,
599 PrivateInMem->StackSize));
600 BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);
601
602 //
603 // After the whole temporary memory is migrated, then we can allocate page in
604 // permenent memory.
605 //
606 PrivateInMem->PeiMemoryInstalled = TRUE;
607
608 //
609 // Indicate that PeiCore reenter
610 //
611 PrivateInMem->PeimDispatcherReenter = TRUE;
612
613 //
614 // Shadow PEI Core. When permanent memory is avaiable, shadow
615 // PEI Core and PEIMs to get high performance.
616 //
617 PrivateInMem->ShadowedPeiCore = ShadowPeiCore (
618 PeiServices,
619 PrivateInMem
620 );
621 //
622 // Process the Notify list and dispatch any notifies for
623 // newly installed PPIs.
624 //
625 ProcessNotifyList (PrivateInMem);
626
627 //
628 // Entry PEI Phase 2
629 //
630 PeiCore (SecCoreData, NULL, PrivateInMem);
631
632 //
633 // Code should not come here
634 //
635 ASSERT_EFI_ERROR(FALSE);
636 }
637
638 //
639 // Process the Notify list and dispatch any notifies for
640 // newly installed PPIs.
641 //
642 ProcessNotifyList (Private);
643
644 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \
645 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
646 //
647 // If memory is availble we shadow images by default for performance reasons.
648 // We call the entry point a 2nd time so the module knows it's shadowed.
649 //
650 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
651 ASSERT (PeimEntryPoint != NULL);
652 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);
653 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
654
655 //
656 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
657 //
658 Private->Fv[FvCount].PeimState[PeimCount]++;
659
660 //
661 // Process the Notify list and dispatch any notifies for
662 // newly installed PPIs.
663 //
664 ProcessNotifyList (Private);
665 }
666 }
667 }
668 }
669
670 //
671 // We set to NULL here to optimize the 2nd entry to this routine after
672 // memory is found. This reprevents rescanning of the FV. We set to
673 // NULL here so we start at the begining of the next FV
674 //
675 Private->CurrentFileHandle = NULL;
676 Private->CurrentPeimCount = 0;
677 //
678 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL
679 //
680 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);
681 }
682
683 //
684 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go
685 // through all the FV.
686 //
687 Private->CurrentPeimFvCount = 0;
688
689 //
690 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get
691 // dispatched. So we need to make another pass
692 //
693 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this
694 // pass. If we did not dispatch a PEIM there is no point in trying again
695 // as it will fail the next time too (nothing has changed).
696 //
697 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);
698
699 }
700
701 /**
702 Initialize the Dispatcher's data members
703
704 @param PrivateData PeiCore's private data structure
705 @param OldCoreData Old data from SecCore
706 NULL if being run in non-permament memory mode.
707 @param SecCoreData Points to a data structure containing information about the PEI core's operating
708 environment, such as the size and location of temporary RAM, the stack location and
709 the BFV location.
710
711 @return None.
712
713 **/
714 VOID
715 InitializeDispatcherData (
716 IN PEI_CORE_INSTANCE *PrivateData,
717 IN PEI_CORE_INSTANCE *OldCoreData,
718 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
719 )
720 {
721 if (OldCoreData == NULL) {
722 PrivateData->PeimDispatcherReenter = FALSE;
723 PeiInitializeFv (PrivateData, SecCoreData);
724 } else {
725 PeiReinitializeFv (PrivateData);
726 }
727
728 return;
729 }
730
731 /**
732 This routine parses the Dependency Expression, if available, and
733 decides if the module can be executed.
734
735
736 @param Private PeiCore's private data structure
737 @param FileHandle PEIM's file handle
738 @param PeimCount Peim count in all dispatched PEIMs.
739
740 @retval TRUE Can be dispatched
741 @retval FALSE Cannot be dispatched
742
743 **/
744 BOOLEAN
745 DepexSatisfied (
746 IN PEI_CORE_INSTANCE *Private,
747 IN EFI_PEI_FILE_HANDLE FileHandle,
748 IN UINTN PeimCount
749 )
750 {
751 EFI_STATUS Status;
752 VOID *DepexData;
753
754 if (PeimCount < Private->AprioriCount) {
755 //
756 // If its in the A priori file then we set Depex to TRUE
757 //
758 return TRUE;
759 }
760
761 //
762 // Depex section not in the encapsulated section.
763 //
764 Status = PeiServicesFfsFindSectionData (
765 EFI_SECTION_PEI_DEPEX,
766 FileHandle,
767 (VOID **)&DepexData
768 );
769
770 if (EFI_ERROR (Status)) {
771 //
772 // If there is no DEPEX, assume the module can be executed
773 //
774 return TRUE;
775 }
776
777 //
778 // Evaluate a given DEPEX
779 //
780 return PeimDispatchReadiness (&Private->PS, DepexData);
781 }
782
783 /**
784 This routine enable a PEIM to register itself to shadow when PEI Foundation
785 discovery permanent memory.
786
787 @param FileHandle File handle of a PEIM.
788
789 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
790 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
791 @retval EFI_SUCCESS Successfully to register itself.
792
793 **/
794 EFI_STATUS
795 EFIAPI
796 PeiRegisterForShadow (
797 IN EFI_PEI_FILE_HANDLE FileHandle
798 )
799 {
800 PEI_CORE_INSTANCE *Private;
801 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
802
803 if (Private->CurrentFileHandle != FileHandle) {
804 //
805 // The FileHandle must be for the current PEIM
806 //
807 return EFI_NOT_FOUND;
808 }
809
810 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {
811 //
812 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
813 //
814 return EFI_ALREADY_STARTED;
815 }
816
817 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;
818
819 return EFI_SUCCESS;
820 }
821
822