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