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