]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Add InvokePeiCore function to invoke the PeiCore in new stack.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Dispatcher.c
15
16 Abstract:
17
18 EFI PEI Core dispatch services
19
20 Revision History
21
22 --*/
23
24 #include <PeiMain.h>
25
26 STATIC
27 VOID *
28 TransferOldDataToNewDataRange (
29 IN PEI_CORE_INSTANCE *PrivateData
30 );
31
32 STATIC
33 VOID
34 InvokePeiCore (
35 VOID *Context1,
36 VOID *Context2
37 );
38
39 EFI_STATUS
40 PeiDispatcher (
41 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
42 IN PEI_CORE_INSTANCE *PrivateData,
43 IN PEI_CORE_DISPATCH_DATA *DispatchData
44 )
45
46 /*++
47
48 Routine Description:
49
50 Conduct PEIM dispatch.
51
52 Arguments:
53
54 SecCoreData - Points to a data structure containing information about the PEI core's operating
55 environment, such as the size and location of temporary RAM, the stack location and
56 the BFV location.
57 PrivateData - Pointer to the private data passed in from caller
58 DispatchData - Pointer to PEI_CORE_DISPATCH_DATA data.
59
60 Returns:
61
62 EFI_SUCCESS - Successfully dispatched PEIM.
63 EFI_NOT_FOUND - The dispatch failed.
64
65 --*/
66 {
67 EFI_STATUS Status;
68 PEI_CORE_TEMP_POINTERS TempPtr;
69 BOOLEAN NextFvFound;
70 EFI_FIRMWARE_VOLUME_HEADER *NextFvAddress;
71 EFI_FIRMWARE_VOLUME_HEADER *DefaultFvAddress;
72 VOID *TopOfStack;
73 PEI_CORE_PARAMETERS PeiCoreParameters;
74
75 //
76 // Debug data for uninstalled Peim list
77 //
78 EFI_GUID DebugFoundPeimList[32];
79 EFI_DEVICE_HANDLE_EXTENDED_DATA ExtendedData;
80
81 //
82 // save the Current FV Address so that we will not process it again if FindFv returns it later
83 //
84 DefaultFvAddress = DispatchData->BootFvAddress;
85
86 //
87 // This is the main dispatch loop. It will search known FVs for PEIMs and
88 // attempt to dispatch them. If any PEIM gets dispatched through a single
89 // pass of the dispatcher, it will start over from the Bfv again to see
90 // if any new PEIMs dependencies got satisfied. With a well ordered
91 // FV where PEIMs are found in the order their dependencies are also
92 // satisfied, this dipatcher should run only once.
93 //
94 for (;;) {
95 //
96 // This is the PEIM search loop. It will scan through all PEIMs it can find
97 // looking for PEIMs to dispatch, and will dipatch them if they have not
98 // already been dispatched and all of their dependencies are met.
99 // If no more PEIMs can be found in this pass through all known FVs,
100 // then it will break out of this loop.
101 //
102 for (;;) {
103
104 Status = FindNextPeim (
105 &PrivateData->PS,
106 DispatchData->CurrentFvAddress,
107 &DispatchData->CurrentPeimAddress
108 );
109
110 //
111 // If we found a PEIM, check if it is dispatched. If so, go to the
112 // next PEIM. If not, dispatch it if its dependencies are satisfied.
113 // If its dependencies are not satisfied, go to the next PEIM.
114 //
115 if (Status == EFI_SUCCESS) {
116
117 DEBUG_CODE_BEGIN ();
118
119 //
120 // Fill list of found Peims for later list of those not installed
121 //
122 CopyMem (
123 &DebugFoundPeimList[DispatchData->CurrentPeim],
124 &DispatchData->CurrentPeimAddress->Name,
125 sizeof (EFI_GUID)
126 );
127
128 DEBUG_CODE_END ();
129
130 if (!Dispatched (
131 DispatchData->CurrentPeim,
132 DispatchData->DispatchedPeimBitMap
133 )) {
134 if (DepexSatisfied (&PrivateData->PS, DispatchData->CurrentPeimAddress)) {
135 Status = PeiLoadImage (
136 &PrivateData->PS,
137 DispatchData->CurrentPeimAddress,
138 &TempPtr.Raw
139 );
140 if (Status == EFI_SUCCESS) {
141
142 //
143 // The PEIM has its dependencies satisfied, and its entry point
144 // has been found, so invoke it.
145 //
146 PERF_START (
147 (VOID *) (UINTN) (DispatchData->CurrentPeimAddress),
148 "PEIM",
149 NULL,
150 0
151 );
152
153 //
154 // BUGBUG: Used to be EFI_PEI_REPORT_STATUS_CODE_CODE
155 //
156 ExtendedData.Handle = (EFI_HANDLE)DispatchData->CurrentPeimAddress;
157
158 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
159 EFI_PROGRESS_CODE,
160 EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN,
161 (VOID *)(&ExtendedData),
162 sizeof (ExtendedData)
163 );
164
165 //
166 // Is this a authentic image
167 //
168 Status = VerifyPeim (
169 &PrivateData->PS,
170 DispatchData->CurrentPeimAddress
171 );
172
173 if (Status != EFI_SECURITY_VIOLATION) {
174
175 //
176 // BUGBUG: Before enable PI, we need cast EFI_FFS_FILE_HEADER* to EFI_PEI_FILE_HANDLE*
177 // Because we use new MdePkg's definition, but they are binary compatible in fact.
178 //
179 Status = TempPtr.PeimEntry (
180 (EFI_PEI_FILE_HANDLE*)DispatchData->CurrentPeimAddress,
181 &PrivateData->PS
182 );
183 }
184
185 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
186 EFI_PROGRESS_CODE,
187 EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END,
188 (VOID *)(&ExtendedData),
189 sizeof (ExtendedData)
190 );
191
192 PERF_END ((VOID *) (UINTN) (DispatchData->CurrentPeimAddress), "PEIM", NULL, 0);
193
194 //
195 // Mark the PEIM as dispatched so we don't attempt to run it again
196 //
197 SetDispatched (
198 &PrivateData->PS,
199 DispatchData->CurrentPeim,
200 &DispatchData->DispatchedPeimBitMap
201 );
202
203 //
204 // Process the Notify list and dispatch any notifies for
205 // newly installed PPIs.
206 //
207 ProcessNotifyList (&PrivateData->PS);
208
209 //
210 // If real system memory was discovered and installed by this
211 // PEIM, switch the stacks to the new memory. Since we are
212 // at dispatch level, only the Core's private data is preserved,
213 // nobody else should have any data on the stack.
214 //
215 if (PrivateData->SwitchStackSignal) {
216 //
217 // Adjust the top of stack to be aligned at CPU_STACK_ALIGNMENT
218 //
219 TopOfStack = (VOID *)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize - CPU_STACK_ALIGNMENT);
220 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
221
222 PeiCoreParameters.SecCoreData = SecCoreData;
223 PeiCoreParameters.PpiList = NULL;
224 PeiCoreParameters.Data = TransferOldDataToNewDataRange (PrivateData);
225 ASSERT (PeiCoreParameters.Data != 0);
226
227 PeiSwitchStacks (
228 InvokePeiCore,
229 (VOID*) (UINTN) PeiCore,
230 (VOID*) &PeiCoreParameters,
231 TopOfStack,
232 (VOID*)(UINTN)PrivateData->StackBase
233 );
234 }
235 }
236 }
237 }
238 DispatchData->CurrentPeim++;
239 continue;
240
241 } else {
242
243 //
244 // If we could not find another PEIM in the current FV, go try
245 // the FindFv PPI to look in other FVs for more PEIMs. If we can
246 // not locate the FindFv PPI, or if the FindFv PPI can not find
247 // anymore FVs, then exit the PEIM search loop.
248 //
249 if (DispatchData->FindFv == NULL) {
250 Status = PeiServicesLocatePpi (
251 &gEfiFindFvPpiGuid,
252 0,
253 NULL,
254 (VOID **)&DispatchData->FindFv
255 );
256 if (Status != EFI_SUCCESS) {
257 break;
258 }
259 }
260 NextFvFound = FALSE;
261 while (!NextFvFound) {
262 Status = DispatchData->FindFv->FindFv (
263 DispatchData->FindFv,
264 &PrivateData->PS,
265 &DispatchData->CurrentFv,
266 &NextFvAddress
267 );
268 //
269 // if there is no next fv, get out of this loop of finding FVs
270 //
271 if (Status != EFI_SUCCESS) {
272 break;
273 }
274 //
275 // don't process the default Fv again. (we don't know the order in which the hobs were created)
276 //
277 if ((NextFvAddress != DefaultFvAddress) &&
278 (NextFvAddress != DispatchData->CurrentFvAddress)) {
279
280 //
281 // VerifyFv() is currently returns SUCCESS all the time, add code to it to
282 // actually verify the given FV
283 //
284 Status = VerifyFv (NextFvAddress);
285 if (Status == EFI_SUCCESS) {
286 NextFvFound = TRUE;
287 DispatchData->CurrentFvAddress = NextFvAddress;
288 DispatchData->CurrentPeimAddress = NULL;
289 //
290 // current PRIM number (CurrentPeim) must continue as is, don't reset it here
291 //
292 }
293 }
294 }
295 //
296 // if there is no next fv, get out of this loop of dispatching PEIMs
297 //
298 if (!NextFvFound) {
299 break;
300 }
301 //
302 // continue in the inner for(;;) loop with a new FV;
303 //
304 }
305 }
306
307 //
308 // If all the PEIMs that we have found have been dispatched, then
309 // there is nothing left to dispatch and we don't need to go search
310 // through all PEIMs again.
311 //
312 if ((~(DispatchData->DispatchedPeimBitMap) &
313 ((1 << DispatchData->CurrentPeim)-1)) == 0) {
314 break;
315 }
316
317 //
318 // Check if no more PEIMs that depex was satisfied
319 //
320 if (DispatchData->DispatchedPeimBitMap == DispatchData->PreviousPeimBitMap) {
321 break;
322 }
323
324 //
325 // Case when Depex is not satisfied and has to traverse the list again
326 //
327 DispatchData->CurrentPeim = 0;
328 DispatchData->CurrentPeimAddress = 0;
329 DispatchData->PreviousPeimBitMap = DispatchData->DispatchedPeimBitMap;
330
331 //
332 // don't go back to the loop without making sure that the CurrentFvAddress is the
333 // same as the 1st (or default) FV we started with. otherwise we will interpret the bimap wrongly and
334 // mess it up, always start processing the PEIMs from the default FV just like in the first time around.
335 //
336 DispatchData->CurrentFv = 0;
337 DispatchData->CurrentFvAddress = DefaultFvAddress;
338 }
339
340 DEBUG_CODE_BEGIN ();
341 //
342 // Debug data for uninstalled Peim list
343 //
344 UINT32 DebugNotDispatchedBitmap;
345 UINT8 DebugFoundPeimPoint;
346
347 DebugFoundPeimPoint = 0;
348 //
349 // Get bitmap of Peims that were not dispatched,
350 //
351
352 DebugNotDispatchedBitmap = ((DispatchData->DispatchedPeimBitMap) ^ ((1 << DispatchData->CurrentPeim)-1));
353 //
354 // Scan bitmap of Peims not installed and print GUIDS
355 //
356 while (DebugNotDispatchedBitmap != 0) {
357 if ((DebugNotDispatchedBitmap & 1) != 0) {
358 DEBUG ((EFI_D_INFO, "WARNING -> InstallPpi: Not Installed: %g\n",
359 &DebugFoundPeimList[DebugFoundPeimPoint]
360 ));
361 }
362 DebugFoundPeimPoint++;
363 DebugNotDispatchedBitmap >>= 1;
364 }
365
366 DEBUG_CODE_END ();
367
368 return EFI_NOT_FOUND;
369 }
370
371 VOID
372 InitializeDispatcherData (
373 IN EFI_PEI_SERVICES **PeiServices,
374 IN PEI_CORE_INSTANCE *OldCoreData,
375 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
376 )
377 /*++
378
379 Routine Description:
380
381 Initialize the Dispatcher's data members
382
383 Arguments:
384
385 PeiServices - The PEI core services table.
386 OldCoreData - Pointer to old core data (before switching stack).
387 NULL if being run in non-permament memory mode.
388 SecCoreData - Points to a data structure containing information about the PEI core's operating
389 environment, such as the size and location of temporary RAM, the stack location and
390 the BFV location.
391
392 Returns:
393
394 None.
395
396 --*/
397 {
398 PEI_CORE_INSTANCE *PrivateData;
399
400 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
401
402 if (OldCoreData == NULL) {
403 PrivateData->DispatchData.CurrentFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase;
404 PrivateData->DispatchData.BootFvAddress = (EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase;
405 } else {
406
407 //
408 // Current peim has been dispatched, but not count
409 //
410 PrivateData->DispatchData.CurrentPeim = (UINT8)(OldCoreData->DispatchData.CurrentPeim + 1);
411 }
412
413 return;
414 }
415
416
417 BOOLEAN
418 Dispatched (
419 IN UINT8 CurrentPeim,
420 IN UINT32 DispatchedPeimBitMap
421 )
422 /*++
423
424 Routine Description:
425
426 This routine checks to see if a particular PEIM has been dispatched during
427 the PEI core dispatch.
428
429 Arguments:
430 CurrentPeim - The PEIM/FV in the bit array to check.
431 DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.
432
433 Returns:
434 TRUE - PEIM already dispatched
435 FALSE - Otherwise
436
437 --*/
438 {
439 return (BOOLEAN)((DispatchedPeimBitMap & (1 << CurrentPeim)) != 0);
440 }
441
442 VOID
443 SetDispatched (
444 IN EFI_PEI_SERVICES **PeiServices,
445 IN UINT8 CurrentPeim,
446 OUT UINT32 *DispatchedPeimBitMap
447 )
448 /*++
449
450 Routine Description:
451
452 This routine sets a PEIM as having been dispatched once its entry
453 point has been invoked.
454
455 Arguments:
456
457 PeiServices - The PEI core services table.
458 CurrentPeim - The PEIM/FV in the bit array to check.
459 DispatchedPeimBitMap - Bit array, each bit corresponds to a PEIM/FV.
460
461 Returns:
462 None
463
464 --*/
465 {
466 //
467 // Check if the total number of PEIMs exceed the bitmap.
468 // CurrentPeim is 0-based
469 //
470 ASSERT (CurrentPeim < (sizeof (*DispatchedPeimBitMap) * 8));
471 *DispatchedPeimBitMap |= (1 << CurrentPeim);
472 return;
473 }
474
475 BOOLEAN
476 DepexSatisfied (
477 IN EFI_PEI_SERVICES **PeiServices,
478 IN VOID *CurrentPeimAddress
479 )
480 /*++
481
482 Routine Description:
483
484 This routine parses the Dependency Expression, if available, and
485 decides if the module can be executed.
486
487 Arguments:
488 PeiServices - The PEI Service Table
489 CurrentPeimAddress - Address of the PEIM Firmware File under investigation
490
491 Returns:
492 TRUE - Can be dispatched
493 FALSE - Cannot be dispatched
494
495 --*/
496 {
497 EFI_STATUS Status;
498 INT8 *DepexData;
499 BOOLEAN Runnable;
500
501 Status = PeiServicesFfsFindSectionData (
502 EFI_SECTION_PEI_DEPEX,
503 CurrentPeimAddress,
504 (VOID **)&DepexData
505 );
506 //
507 // If there is no DEPEX, assume the module can be executed
508 //
509 if (EFI_ERROR (Status)) {
510 return TRUE;
511 }
512
513 //
514 // Evaluate a given DEPEX
515 //
516 Status = PeimDispatchReadiness (
517 PeiServices,
518 DepexData,
519 &Runnable
520 );
521
522 return Runnable;
523 }
524
525 STATIC
526 VOID *
527 TransferOldDataToNewDataRange (
528 IN PEI_CORE_INSTANCE *PrivateData
529 )
530 /*++
531
532 Routine Description:
533
534 This routine transfers the contents of the pre-permanent memory
535 PEI Core private data to a post-permanent memory data location.
536
537 Arguments:
538
539 PrivateData - Pointer to the current PEI Core private data pre-permanent memory
540
541 Returns:
542
543 Pointer to the PrivateData once the private data has been transferred to permanent memory
544
545 --*/
546 {
547 //
548 //Build private HOB to PEI core to transfer old NEM-range data to new NEM-range
549 //
550 return BuildGuidDataHob (&gEfiPeiCorePrivateGuid, PrivateData, sizeof (PEI_CORE_INSTANCE));
551 }
552
553 /**
554 This routine enable a PEIM to register itself to shadow when PEI Foundation
555 discovery permanent memory.
556
557 @param FileHandle File handle of a PEIM.
558
559 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
560 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
561 @retval EFI_SUCCESS Successfully to register itself.
562
563 **/
564 EFI_STATUS
565 EFIAPI
566 PeiRegisterForShadow (
567 IN EFI_PEI_FILE_HANDLE FileHandle
568 )
569 {
570 PEI_CORE_INSTANCE *Private;
571 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
572
573 if (Private->CurrentFileHandle != FileHandle) {
574 //
575 // The FileHandle must be for the current PEIM
576 //
577 return EFI_NOT_FOUND;
578 }
579
580 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {
581 //
582 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
583 //
584 return EFI_ALREADY_STARTED;
585 }
586
587 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;
588
589 return EFI_SUCCESS;
590 }
591
592 /**
593 This routine invoke the PeiCore's entry in new stack environment.
594
595 @param Context1 The first context parameter is entry of PeiCore
596 @param Context2 The second context parameter is parameter structure point for PeiCore
597
598 **/
599 STATIC
600 VOID
601 InvokePeiCore (
602 VOID *Context1,
603 VOID *Context2
604 )
605 {
606 PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;
607 PEI_CORE_PARAMETERS *PeiCoreParameters;
608
609 //
610 // Running on new stack in SEC Core
611 //
612
613 PeiCoreEntryPoint = (PEI_CORE_ENTRY_POINT) (UINTN) Context1;
614 PeiCoreParameters = (PEI_CORE_PARAMETERS *)Context2;
615
616 //
617 // Call PEI Core using new stack
618 //
619 PeiCoreEntryPoint (
620 PeiCoreParameters->SecCoreData,
621 PeiCoreParameters->PpiList,
622 PeiCoreParameters->Data
623 );
624
625 //
626 // Never returns
627 //
628 ASSERT_EFI_ERROR (FALSE);
629 }
630
631
632
633