]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Core/Dispatcher.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Core / Dispatcher.c
CommitLineData
6b46d772
SV
1/** @file\r
2 MM Driver Dispatcher.\r
3\r
4 Step #1 - When a FV protocol is added to the system every driver in the FV\r
5 is added to the mDiscoveredList. The Before, and After Depex are\r
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori\r
7 file exists in the FV those drivers are addeded to the\r
8 mScheduledQueue. The mFvHandleList is used to make sure a\r
9 FV is only processed once.\r
10\r
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and\r
12 start it. After mScheduledQueue is drained check the\r
13 mDiscoveredList to see if any item has a Depex that is ready to\r
14 be placed on the mScheduledQueue.\r
15\r
16 Step #3 - Adding to the mScheduledQueue requires that you process Before\r
17 and After dependencies. This is done recursively as the call to add\r
18 to the mScheduledQueue checks for Before and recursively adds\r
19 all Befores. It then addes the item that was passed in and then\r
20 processess the After dependecies by recursively calling the routine.\r
21\r
22 Dispatcher Rules:\r
23 The rules for the dispatcher are similar to the DXE dispatcher.\r
24\r
25 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3\r
26 is the state diagram for the DXE dispatcher\r
27\r
28 Depex - Dependency Expresion.\r
29\r
30 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.\r
31 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
32 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>\r
33\r
86094561 34 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6b46d772
SV
35\r
36**/\r
37\r
38#include "StandaloneMmCore.h"\r
39\r
40//\r
41// MM Dispatcher Data structures\r
42//\r
43#define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')\r
44\r
45typedef struct {\r
46 UINTN Signature;\r
47 LIST_ENTRY Link; // mFvHandleList\r
48 EFI_HANDLE Handle;\r
49} KNOWN_HANDLE;\r
50\r
51//\r
52// Function Prototypes\r
53//\r
54\r
55EFI_STATUS\r
56MmCoreFfsFindMmDriver (\r
57 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r
58 );\r
59\r
60/**\r
61 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
62 must add any driver with a before dependency on InsertedDriverEntry first.\r
63 You do this by recursively calling this routine. After all the Befores are\r
64 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
65 Then you can add any driver with an After dependency on InsertedDriverEntry\r
66 by recursively calling this routine.\r
67\r
68 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
69\r
70**/\r
71VOID\r
72MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
73 IN EFI_MM_DRIVER_ENTRY *InsertedDriverEntry\r
74 );\r
75\r
76//\r
77// The Driver List contains one copy of every driver that has been discovered.\r
78// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY\r
79//\r
80LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);\r
81\r
82//\r
83// Queue of drivers that are ready to dispatch. This queue is a subset of the\r
84// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.\r
85//\r
86LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);\r
87\r
88//\r
89// List of handles who's Fv's have been parsed and added to the mFwDriverList.\r
90//\r
91LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);\r
92\r
93//\r
94// Flag for the MM Dispacher. TRUE if dispatcher is execuing.\r
95//\r
96BOOLEAN gDispatcherRunning = FALSE;\r
97\r
98//\r
99// Flag for the MM Dispacher. TRUE if there is one or more MM drivers ready to be dispatched\r
100//\r
101BOOLEAN gRequestDispatch = FALSE;\r
102\r
103//\r
104// The global variable is defined for Loading modules at fixed address feature to track the MM code\r
105// memory range usage. It is a bit mapped array in which every bit indicates the correspoding\r
106// memory page available or not.\r
107//\r
108GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mMmCodeMemoryRangeUsageBitMap=NULL;\r
109\r
110/**\r
111 To check memory usage bit map array to figure out if the memory range in which the image will be loaded\r
112 is available or not. If memory range is avaliable, the function will mark the correponding bits to 1\r
113 which indicates the memory range is used. The function is only invoked when load modules at fixed address\r
114 feature is enabled.\r
115\r
116 @param ImageBase The base addres the image will be loaded at.\r
117 @param ImageSize The size of the image\r
118\r
119 @retval EFI_SUCCESS The memory range the image will be loaded in is available\r
120 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r
121**/\r
122EFI_STATUS\r
123CheckAndMarkFixLoadingMemoryUsageBitMap (\r
124 IN EFI_PHYSICAL_ADDRESS ImageBase,\r
125 IN UINTN ImageSize\r
126 )\r
127{\r
128 UINT32 MmCodePageNumber;\r
129 UINT64 MmCodeSize;\r
130 EFI_PHYSICAL_ADDRESS MmCodeBase;\r
131 UINTN BaseOffsetPageNumber;\r
132 UINTN TopOffsetPageNumber;\r
133 UINTN Index;\r
134\r
135 //\r
136 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber\r
137 //\r
138 MmCodePageNumber = 0;\r
139 MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);\r
140 MmCodeBase = gLoadModuleAtFixAddressMmramBase;\r
141\r
142 //\r
143 // If the memory usage bit map is not initialized, do it. Every bit in the array\r
144 // indicate the status of the corresponding memory page, available or not\r
145 //\r
146 if (mMmCodeMemoryRangeUsageBitMap == NULL) {\r
147 mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool (((MmCodePageNumber / 64) + 1) * sizeof (UINT64));\r
148 }\r
149\r
150 //\r
151 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
152 //\r
153 if (mMmCodeMemoryRangeUsageBitMap == NULL) {\r
154 return EFI_NOT_FOUND;\r
155 }\r
156\r
157 //\r
158 // see if the memory range for loading the image is in the MM code range.\r
159 //\r
160 if (MmCodeBase + MmCodeSize < ImageBase + ImageSize || MmCodeBase > ImageBase) {\r
161 return EFI_NOT_FOUND;\r
162 }\r
163\r
164 //\r
165 // Test if the memory is avalaible or not.\r
166 //\r
167 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES ((UINT32)(ImageBase - MmCodeBase));\r
168 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES ((UINT32)(ImageBase + ImageSize - MmCodeBase));\r
169 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
170 if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64 (1, (Index % 64))) != 0) {\r
171 //\r
172 // This page is already used.\r
173 //\r
174 return EFI_NOT_FOUND;\r
175 }\r
176 }\r
177\r
178 //\r
179 // Being here means the memory range is available. So mark the bits for the memory range\r
180 //\r
181 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
182 mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64 (1, (Index % 64));\r
183 }\r
184 return EFI_SUCCESS;\r
185}\r
186\r
187/**\r
188 Get the fixed loading address from image header assigned by build tool. This function only be called\r
189 when Loading module at Fixed address feature enabled.\r
190\r
191 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
192 image that needs to be examined by this function.\r
193 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
194 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
195\r
196**/\r
197EFI_STATUS\r
198GetPeCoffImageFixLoadingAssignedAddress(\r
199 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
200 )\r
201{\r
202 UINTN SectionHeaderOffset;\r
203 EFI_STATUS Status;\r
204 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
205 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
206 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
207 UINT16 Index;\r
208 UINTN Size;\r
209 UINT16 NumberOfSections;\r
210 UINT64 ValueInSectionHeader;\r
211\r
212 FixLoadingAddress = 0;\r
213 Status = EFI_NOT_FOUND;\r
214\r
215 //\r
216 // Get PeHeader pointer\r
217 //\r
218 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
219 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) +\r
220 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
221 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
222\r
223 //\r
224 // Get base address from the first section header that doesn't point to code section.\r
225 //\r
226 for (Index = 0; Index < NumberOfSections; Index++) {\r
227 //\r
228 // Read section header from file\r
229 //\r
230 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
231 Status = ImageContext->ImageRead (\r
232 ImageContext->Handle,\r
233 SectionHeaderOffset,\r
234 &Size,\r
235 &SectionHeader\r
236 );\r
237 if (EFI_ERROR (Status)) {\r
238 return Status;\r
239 }\r
240\r
241 Status = EFI_NOT_FOUND;\r
242\r
243 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
244 //\r
245 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields\r
246 // in the first section header that doesn't point to code section in image header. So there\r
247 // is an assumption that when the feature is enabled, if a module with a loading address\r
248 // assigned by tools, the PointerToRelocations & PointerToLineNumbers fields should not be\r
249 // Zero, or else, these 2 fields should be set to Zero\r
250 //\r
251 ValueInSectionHeader = ReadUnaligned64 ((UINT64*)&SectionHeader.PointerToRelocations);\r
252 if (ValueInSectionHeader != 0) {\r
253 //\r
254 // Found first section header that doesn't point to code section in which build tool saves the\r
255 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
256 //\r
257 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);\r
258 //\r
259 // Check if the memory range is available.\r
260 //\r
261 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
262 if (!EFI_ERROR(Status)) {\r
263 //\r
264 // The assigned address is valid. Return the specified loading address\r
265 //\r
266 ImageContext->ImageAddress = FixLoadingAddress;\r
267 }\r
268 }\r
269 break;\r
270 }\r
271 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
272 }\r
273 DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n",\r
274 FixLoadingAddress, Status));\r
275 return Status;\r
276}\r
277/**\r
278 Loads an EFI image into SMRAM.\r
279\r
280 @param DriverEntry EFI_MM_DRIVER_ENTRY instance\r
281\r
282 @return EFI_STATUS\r
283\r
284**/\r
285EFI_STATUS\r
286EFIAPI\r
287MmLoadImage (\r
288 IN OUT EFI_MM_DRIVER_ENTRY *DriverEntry\r
289 )\r
290{\r
6b46d772
SV
291 UINTN PageCount;\r
292 EFI_STATUS Status;\r
293 EFI_PHYSICAL_ADDRESS DstBuffer;\r
294 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
295\r
296 DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));\r
297\r
6b46d772
SV
298 Status = EFI_SUCCESS;\r
299\r
300 //\r
301 // Initialize ImageContext\r
302 //\r
877013d0 303 ImageContext.Handle = DriverEntry->Pe32Data;\r
6b46d772
SV
304 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
305\r
306 //\r
307 // Get information about the image being loaded\r
308 //\r
309 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
310 if (EFI_ERROR (Status)) {\r
6b46d772
SV
311 return Status;\r
312 }\r
313\r
314 PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
315 DstBuffer = (UINTN)(-1);\r
316\r
317 Status = MmAllocatePages (\r
318 AllocateMaxAddress,\r
319 EfiRuntimeServicesCode,\r
320 PageCount,\r
321 &DstBuffer\r
322 );\r
323 if (EFI_ERROR (Status)) {\r
6b46d772
SV
324 return Status;\r
325 }\r
326\r
327 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
328\r
329 //\r
330 // Align buffer on section boundry\r
331 //\r
332 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
333 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));\r
334\r
335 //\r
336 // Load the image to our new buffer\r
337 //\r
338 Status = PeCoffLoaderLoadImage (&ImageContext);\r
339 if (EFI_ERROR (Status)) {\r
6b46d772
SV
340 MmFreePages (DstBuffer, PageCount);\r
341 return Status;\r
342 }\r
343\r
344 //\r
345 // Relocate the image in our new buffer\r
346 //\r
347 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
348 if (EFI_ERROR (Status)) {\r
6b46d772
SV
349 MmFreePages (DstBuffer, PageCount);\r
350 return Status;\r
351 }\r
352\r
353 //\r
354 // Flush the instruction cache so the image data are written before we execute it\r
355 //\r
356 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);\r
357\r
358 //\r
359 // Save Image EntryPoint in DriverEntry\r
360 //\r
361 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;\r
362 DriverEntry->ImageBuffer = DstBuffer;\r
363 DriverEntry->NumberOfPage = PageCount;\r
364\r
365 if (mEfiSystemTable != NULL) {\r
366 Status = mEfiSystemTable->BootServices->AllocatePool (\r
367 EfiBootServicesData,\r
368 sizeof (EFI_LOADED_IMAGE_PROTOCOL),\r
369 (VOID **)&DriverEntry->LoadedImage\r
370 );\r
371 if (EFI_ERROR (Status)) {\r
6b46d772
SV
372 MmFreePages (DstBuffer, PageCount);\r
373 return Status;\r
374 }\r
375\r
376 ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
377 //\r
378 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
379 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
380 //\r
381 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
382 DriverEntry->LoadedImage->ParentHandle = NULL;\r
383 DriverEntry->LoadedImage->SystemTable = mEfiSystemTable;\r
384 DriverEntry->LoadedImage->DeviceHandle = NULL;\r
385 DriverEntry->LoadedImage->FilePath = NULL;\r
386\r
387 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
388 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;\r
389 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
390 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
391\r
392 //\r
393 // Create a new image handle in the UEFI handle database for the MM Driver\r
394 //\r
395 DriverEntry->ImageHandle = NULL;\r
396 Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (\r
397 &DriverEntry->ImageHandle,\r
398 &gEfiLoadedImageProtocolGuid,\r
399 DriverEntry->LoadedImage,\r
400 NULL\r
401 );\r
402 }\r
403\r
404 //\r
405 // Print the load address and the PDB file name if it is available\r
406 //\r
407 DEBUG_CODE_BEGIN ();\r
408\r
409 UINTN Index;\r
410 UINTN StartIndex;\r
411 CHAR8 EfiFileName[256];\r
412\r
413 DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
414 "Loading MM driver at 0x%11p EntryPoint=0x%11p ",\r
415 (VOID *)(UINTN) ImageContext.ImageAddress,\r
416 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
417\r
418 //\r
419 // Print Module Name by Pdb file path.\r
420 // Windows and Unix style file path are all trimmed correctly.\r
421 //\r
422 if (ImageContext.PdbPointer != NULL) {\r
423 StartIndex = 0;\r
424 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
425 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
426 StartIndex = Index + 1;\r
427 }\r
428 }\r
429\r
430 //\r
431 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
432 // The PDB file name is limited in the range of 0~255.\r
433 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
434 //\r
435 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
436 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
437 if (EfiFileName[Index] == 0) {\r
438 EfiFileName[Index] = '.';\r
439 }\r
440 if (EfiFileName[Index] == '.') {\r
441 EfiFileName[Index + 1] = 'e';\r
442 EfiFileName[Index + 2] = 'f';\r
443 EfiFileName[Index + 3] = 'i';\r
444 EfiFileName[Index + 4] = 0;\r
445 break;\r
446 }\r
447 }\r
448\r
449 if (Index == sizeof (EfiFileName) - 4) {\r
450 EfiFileName[Index] = 0;\r
451 }\r
452 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));\r
453 }\r
454 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
455\r
456 DEBUG_CODE_END ();\r
457\r
6b46d772
SV
458 return Status;\r
459}\r
460\r
461/**\r
462 Preprocess dependency expression and update DriverEntry to reflect the\r
463 state of Before and After dependencies. If DriverEntry->Before\r
464 or DriverEntry->After is set it will never be cleared.\r
465\r
466 @param DriverEntry DriverEntry element to update .\r
467\r
468 @retval EFI_SUCCESS It always works.\r
469\r
470**/\r
471EFI_STATUS\r
472MmPreProcessDepex (\r
473 IN EFI_MM_DRIVER_ENTRY *DriverEntry\r
474 )\r
475{\r
476 UINT8 *Iterator;\r
477\r
478 Iterator = DriverEntry->Depex;\r
479 DriverEntry->Dependent = TRUE;\r
480\r
481 if (*Iterator == EFI_DEP_BEFORE) {\r
482 DriverEntry->Before = TRUE;\r
483 } else if (*Iterator == EFI_DEP_AFTER) {\r
484 DriverEntry->After = TRUE;\r
485 }\r
486\r
487 if (DriverEntry->Before || DriverEntry->After) {\r
488 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
489 }\r
490\r
491 return EFI_SUCCESS;\r
492}\r
493\r
494/**\r
495 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
496 protocol returns an error via ReadSection defer the reading of the Depex.\r
497\r
498 @param DriverEntry Driver to work on.\r
499\r
500 @retval EFI_SUCCESS Depex read and preprossesed\r
501 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
502 and Depex reading needs to be retried.\r
503 @retval Error DEPEX not found.\r
504\r
505**/\r
506EFI_STATUS\r
507MmGetDepexSectionAndPreProccess (\r
508 IN EFI_MM_DRIVER_ENTRY *DriverEntry\r
509 )\r
510{\r
511 EFI_STATUS Status;\r
512\r
513 //\r
514 // Data already read\r
515 //\r
516 if (DriverEntry->Depex == NULL) {\r
517 Status = EFI_NOT_FOUND;\r
518 } else {\r
519 Status = EFI_SUCCESS;\r
520 }\r
521 if (EFI_ERROR (Status)) {\r
522 if (Status == EFI_PROTOCOL_ERROR) {\r
523 //\r
524 // The section extraction protocol failed so set protocol error flag\r
525 //\r
526 DriverEntry->DepexProtocolError = TRUE;\r
527 } else {\r
528 //\r
529 // If no Depex assume depend on all architectural protocols\r
530 //\r
531 DriverEntry->Depex = NULL;\r
532 DriverEntry->Dependent = TRUE;\r
533 DriverEntry->DepexProtocolError = FALSE;\r
534 }\r
535 } else {\r
536 //\r
537 // Set Before and After state information based on Depex\r
538 // Driver will be put in Dependent state\r
539 //\r
540 MmPreProcessDepex (DriverEntry);\r
541 DriverEntry->DepexProtocolError = FALSE;\r
542 }\r
543\r
544 return Status;\r
545}\r
546\r
547/**\r
548 This is the main Dispatcher for MM and it exits when there are no more\r
549 drivers to run. Drain the mScheduledQueue and load and start a PE\r
550 image for each driver. Search the mDiscoveredList to see if any driver can\r
551 be placed on the mScheduledQueue. If no drivers are placed on the\r
552 mScheduledQueue exit the function.\r
553\r
554 @retval EFI_SUCCESS All of the MM Drivers that could be dispatched\r
555 have been run and the MM Entry Point has been\r
556 registered.\r
557 @retval EFI_NOT_READY The MM Driver that registered the MM Entry Point\r
558 was just dispatched.\r
559 @retval EFI_NOT_FOUND There are no MM Drivers available to be dispatched.\r
560 @retval EFI_ALREADY_STARTED The MM Dispatcher is already running\r
561\r
562**/\r
563EFI_STATUS\r
564MmDispatcher (\r
565 VOID\r
566 )\r
567{\r
568 EFI_STATUS Status;\r
569 LIST_ENTRY *Link;\r
570 EFI_MM_DRIVER_ENTRY *DriverEntry;\r
571 BOOLEAN ReadyToRun;\r
6b46d772
SV
572\r
573 DEBUG ((DEBUG_INFO, "MmDispatcher\n"));\r
574\r
575 if (!gRequestDispatch) {\r
576 DEBUG ((DEBUG_INFO, " !gRequestDispatch\n"));\r
577 return EFI_NOT_FOUND;\r
578 }\r
579\r
580 if (gDispatcherRunning) {\r
581 DEBUG ((DEBUG_INFO, " gDispatcherRunning\n"));\r
582 //\r
583 // If the dispatcher is running don't let it be restarted.\r
584 //\r
585 return EFI_ALREADY_STARTED;\r
586 }\r
587\r
588 gDispatcherRunning = TRUE;\r
589\r
590 do {\r
591 //\r
592 // Drain the Scheduled Queue\r
593 //\r
594 DEBUG ((DEBUG_INFO, " Drain the Scheduled Queue\n"));\r
595 while (!IsListEmpty (&mScheduledQueue)) {\r
596 DriverEntry = CR (\r
597 mScheduledQueue.ForwardLink,\r
598 EFI_MM_DRIVER_ENTRY,\r
599 ScheduledLink,\r
600 EFI_MM_DRIVER_ENTRY_SIGNATURE\r
601 );\r
602 DEBUG ((DEBUG_INFO, " DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));\r
603\r
604 //\r
605 // Load the MM Driver image into memory. If the Driver was transitioned from\r
606 // Untrused to Scheduled it would have already been loaded so we may need to\r
607 // skip the LoadImage\r
608 //\r
609 if (DriverEntry->ImageHandle == NULL) {\r
610 Status = MmLoadImage (DriverEntry);\r
611\r
612 //\r
613 // Update the driver state to reflect that it's been loaded\r
614 //\r
615 if (EFI_ERROR (Status)) {\r
616 //\r
617 // The MM Driver could not be loaded, and do not attempt to load or start it again.\r
618 // Take driver from Scheduled to Initialized.\r
619 //\r
620 DriverEntry->Initialized = TRUE;\r
621 DriverEntry->Scheduled = FALSE;\r
622 RemoveEntryList (&DriverEntry->ScheduledLink);\r
623\r
624 //\r
625 // If it's an error don't try the StartImage\r
626 //\r
627 continue;\r
628 }\r
629 }\r
630\r
631 DriverEntry->Scheduled = FALSE;\r
632 DriverEntry->Initialized = TRUE;\r
633 RemoveEntryList (&DriverEntry->ScheduledLink);\r
634\r
6b46d772
SV
635 //\r
636 // For each MM driver, pass NULL as ImageHandle\r
637 //\r
638 if (mEfiSystemTable == NULL) {\r
639 DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));\r
640 Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint) (DriverEntry->ImageHandle, &gMmCoreMmst);\r
641 } else {\r
642 DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));\r
643 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint) (\r
644 DriverEntry->ImageHandle,\r
645 mEfiSystemTable\r
646 );\r
647 }\r
648 if (EFI_ERROR(Status)) {\r
649 DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));\r
650 MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
651 }\r
6b46d772
SV
652 }\r
653\r
654 //\r
655 // Search DriverList for items to place on Scheduled Queue\r
656 //\r
657 DEBUG ((DEBUG_INFO, " Search DriverList for items to place on Scheduled Queue\n"));\r
658 ReadyToRun = FALSE;\r
659 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
660 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);\r
661 DEBUG ((DEBUG_INFO, " DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));\r
662\r
663 if (DriverEntry->DepexProtocolError) {\r
664 //\r
665 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
666 //\r
667 Status = MmGetDepexSectionAndPreProccess (DriverEntry);\r
668 }\r
669\r
670 if (DriverEntry->Dependent) {\r
671 if (MmIsSchedulable (DriverEntry)) {\r
672 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
673 ReadyToRun = TRUE;\r
674 }\r
675 }\r
676 }\r
677 } while (ReadyToRun);\r
678\r
679 //\r
680 // If there is no more MM driver to dispatch, stop the dispatch request\r
681 //\r
682 DEBUG ((DEBUG_INFO, " no more MM driver to dispatch, stop the dispatch request\n"));\r
683 gRequestDispatch = FALSE;\r
684 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
685 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);\r
686 DEBUG ((DEBUG_INFO, " DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));\r
687\r
688 if (!DriverEntry->Initialized) {\r
689 //\r
690 // We have MM driver pending to dispatch\r
691 //\r
692 gRequestDispatch = TRUE;\r
693 break;\r
694 }\r
695 }\r
696\r
697 gDispatcherRunning = FALSE;\r
698\r
699 return EFI_SUCCESS;\r
700}\r
701\r
702/**\r
703 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
704 must add any driver with a before dependency on InsertedDriverEntry first.\r
705 You do this by recursively calling this routine. After all the Befores are\r
706 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
707 Then you can add any driver with an After dependency on InsertedDriverEntry\r
708 by recursively calling this routine.\r
709\r
710 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
711\r
712**/\r
713VOID\r
714MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
715 IN EFI_MM_DRIVER_ENTRY *InsertedDriverEntry\r
716 )\r
717{\r
718 LIST_ENTRY *Link;\r
719 EFI_MM_DRIVER_ENTRY *DriverEntry;\r
720\r
721 //\r
722 // Process Before Dependency\r
723 //\r
724 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
725 DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);\r
726 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
727 DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
728 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
729 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
730 //\r
731 // Recursively process BEFORE\r
732 //\r
733 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
734 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
735 } else {\r
736 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
737 }\r
738 }\r
739 }\r
740\r
741 //\r
742 // Convert driver from Dependent to Scheduled state\r
743 //\r
744\r
745 InsertedDriverEntry->Dependent = FALSE;\r
746 InsertedDriverEntry->Scheduled = TRUE;\r
747 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
748\r
749\r
750 //\r
751 // Process After Dependency\r
752 //\r
753 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
754 DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);\r
755 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
756 DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
757 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
758 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
759 //\r
760 // Recursively process AFTER\r
761 //\r
762 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
763 MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
764 } else {\r
765 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
766 }\r
767 }\r
768 }\r
769}\r
770\r
771/**\r
772 Return TRUE if the Fv has been processed, FALSE if not.\r
773\r
774 @param FvHandle The handle of a FV that's being tested\r
775\r
776 @retval TRUE Fv protocol on FvHandle has been processed\r
777 @retval FALSE Fv protocol on FvHandle has not yet been\r
778 processed\r
779\r
780**/\r
781BOOLEAN\r
782FvHasBeenProcessed (\r
783 IN EFI_HANDLE FvHandle\r
784 )\r
785{\r
786 LIST_ENTRY *Link;\r
787 KNOWN_HANDLE *KnownHandle;\r
788\r
789 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
790 KnownHandle = CR (Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
791 if (KnownHandle->Handle == FvHandle) {\r
792 return TRUE;\r
793 }\r
794 }\r
795 return FALSE;\r
796}\r
797\r
798/**\r
799 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
800 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
801 never removed/freed from the mFvHandleList.\r
802\r
803 @param FvHandle The handle of a FV that has been processed\r
804\r
805**/\r
806VOID\r
807FvIsBeingProcesssed (\r
808 IN EFI_HANDLE FvHandle\r
809 )\r
810{\r
811 KNOWN_HANDLE *KnownHandle;\r
812\r
813 DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));\r
814\r
815 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
816 ASSERT (KnownHandle != NULL);\r
817\r
818 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
819 KnownHandle->Handle = FvHandle;\r
820 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
821}\r
822\r
823/**\r
824 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
825 and initilize any state variables. Read the Depex from the FV and store it\r
826 in DriverEntry. Pre-process the Depex to set the Before and After state.\r
827 The Discovered list is never free'ed and contains booleans that represent the\r
828 other possible MM driver states.\r
829\r
830 @param Fv Fv protocol, needed to read Depex info out of\r
831 FLASH.\r
832 @param FvHandle Handle for Fv, needed in the\r
833 EFI_MM_DRIVER_ENTRY so that the PE image can be\r
834 read out of the FV at a later time.\r
835 @param DriverName Name of driver to add to mDiscoveredList.\r
836\r
837 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
838 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
839 DriverName may be active in the system at any one\r
840 time.\r
841\r
842**/\r
843EFI_STATUS\r
844MmAddToDriverList (\r
845 IN EFI_HANDLE FvHandle,\r
846 IN VOID *Pe32Data,\r
847 IN UINTN Pe32DataSize,\r
848 IN VOID *Depex,\r
849 IN UINTN DepexSize,\r
850 IN EFI_GUID *DriverName\r
851 )\r
852{\r
853 EFI_MM_DRIVER_ENTRY *DriverEntry;\r
854\r
855 DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));\r
856\r
857 //\r
858 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
859 // NULL or FALSE.\r
860 //\r
861 DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));\r
862 ASSERT (DriverEntry != NULL);\r
863\r
864 DriverEntry->Signature = EFI_MM_DRIVER_ENTRY_SIGNATURE;\r
865 CopyGuid (&DriverEntry->FileName, DriverName);\r
866 DriverEntry->FvHandle = FvHandle;\r
867 DriverEntry->Pe32Data = Pe32Data;\r
868 DriverEntry->Pe32DataSize = Pe32DataSize;\r
869 DriverEntry->Depex = Depex;\r
870 DriverEntry->DepexSize = DepexSize;\r
871\r
872 MmGetDepexSectionAndPreProccess (DriverEntry);\r
873\r
874 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
875 gRequestDispatch = TRUE;\r
876\r
877 return EFI_SUCCESS;\r
878}\r
879\r
6b46d772
SV
880/**\r
881 Traverse the discovered list for any drivers that were discovered but not loaded\r
882 because the dependency experessions evaluated to false.\r
883\r
884**/\r
885VOID\r
886MmDisplayDiscoveredNotDispatched (\r
887 VOID\r
888 )\r
889{\r
890 LIST_ENTRY *Link;\r
891 EFI_MM_DRIVER_ENTRY *DriverEntry;\r
892\r
893 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
894 DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);\r
895 if (DriverEntry->Dependent) {\r
896 DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
897 }\r
898 }\r
899}\r