]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Update code to pass build on VS2008 with /Od compiler option.
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
CommitLineData
e42e9404 1/** @file\r
2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM\r
3\r
2c0f06f0 4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
e42e9404 5 This program and the accompanying materials are licensed and made available \r
6 under the terms and conditions of the BSD License which accompanies this \r
7 distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/SmmBase2.h>\r
18#include <Protocol/SmmCommunication.h>\r
19#include <Protocol/SmmAccess2.h>\r
20#include <Protocol/SmmConfiguration.h>\r
21#include <Protocol/SmmControl2.h>\r
22#include <Protocol/DxeSmmReadyToLock.h>\r
07d9dc83 23#include <Protocol/Cpu.h>\r
e42e9404 24\r
25#include <Guid/EventGroup.h>\r
26#include <Guid/EventLegacyBios.h>\r
3c447c27 27#include <Guid/LoadModuleAtFixedAddress.h>\r
e42e9404 28\r
29#include <Library/BaseLib.h>\r
30#include <Library/BaseMemoryLib.h>\r
31#include <Library/PeCoffLib.h>\r
32#include <Library/CacheMaintenanceLib.h>\r
33#include <Library/MemoryAllocationLib.h>\r
34#include <Library/DebugLib.h>\r
35#include <Library/UefiBootServicesTableLib.h>\r
36#include <Library/DxeServicesTableLib.h>\r
d7aaf1dc 37#include <Library/DxeServicesLib.h>\r
e42e9404 38#include <Library/UefiLib.h>\r
39#include <Library/UefiRuntimeLib.h>\r
3c447c27 40#include <Library/PcdLib.h>\r
e42e9404 41\r
42#include "PiSmmCorePrivateData.h"\r
43\r
44//\r
45// Function prototypes from produced protocols\r
46//\r
47\r
48/**\r
49 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
50\r
51 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
52 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
53 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
54\r
55 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
56 @retval EFI_SUCCESS The call returned successfully.\r
57\r
58**/\r
59EFI_STATUS\r
60EFIAPI\r
61SmmBase2InSmram (\r
62 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
63 OUT BOOLEAN *InSmram\r
64 );\r
65\r
66/**\r
67 Retrieves the location of the System Management System Table (SMST).\r
68\r
69 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
70 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
71\r
72 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
73 @retval EFI_SUCCESS The memory was returned to the system.\r
74 @retval EFI_UNSUPPORTED Not in SMM.\r
75\r
76**/\r
77EFI_STATUS\r
78EFIAPI\r
79SmmBase2GetSmstLocation (\r
80 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
81 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
82 );\r
83\r
84/**\r
85 Communicates with a registered handler.\r
86 \r
87 This function provides a service to send and receive messages from a registered \r
88 UEFI service. This function is part of the SMM Communication Protocol that may \r
89 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
90 after SetVirtualAddressMap().\r
91\r
92 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
2292758d 93 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
94 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
e42e9404 95 being returned. Zero if the handler does not wish to reply with any data.\r
96\r
97 @retval EFI_SUCCESS The message was successfully posted.\r
98 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
99**/\r
100EFI_STATUS\r
101EFIAPI\r
102SmmCommunicationCommunicate (\r
103 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
104 IN OUT VOID *CommBuffer,\r
105 IN OUT UINTN *CommSize\r
106 );\r
107\r
108/**\r
109 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
110\r
111 @param Event The Event that is being processed, not used.\r
112 @param Context Event Context, not used.\r
113\r
114**/\r
115VOID\r
116EFIAPI\r
117SmmIplSmmConfigurationEventNotify (\r
118 IN EFI_EVENT Event,\r
119 IN VOID *Context\r
120 );\r
121\r
122/**\r
123 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
124 or if gEfiEventReadyToBootGuid is signalled.\r
125\r
126 @param Event The Event that is being processed, not used.\r
127 @param Context Event Context, not used.\r
128\r
129**/\r
130VOID\r
131EFIAPI\r
132SmmIplReadyToLockEventNotify (\r
133 IN EFI_EVENT Event,\r
134 IN VOID *Context\r
135 );\r
136\r
137/**\r
138 Event notification that is fired when DxeDispatch Event Group is signaled.\r
139\r
140 @param Event The Event that is being processed, not used.\r
141 @param Context Event Context, not used.\r
142\r
5657b268 143**/\r
144VOID\r
145EFIAPI\r
146SmmIplDxeDispatchEventNotify (\r
147 IN EFI_EVENT Event,\r
148 IN VOID *Context\r
149 );\r
150\r
151/**\r
152 Event notification that is fired when a GUIDed Event Group is signaled.\r
153\r
154 @param Event The Event that is being processed, not used.\r
155 @param Context Event Context, not used.\r
156\r
e42e9404 157**/\r
158VOID\r
159EFIAPI\r
160SmmIplGuidedEventNotify (\r
161 IN EFI_EVENT Event,\r
162 IN VOID *Context\r
163 );\r
164\r
165/**\r
166 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
167\r
168 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
169 It convers pointer to new virtual address.\r
170\r
171 @param Event Event whose notification function is being invoked.\r
172 @param Context Pointer to the notification function's context.\r
173\r
174**/\r
175VOID\r
176EFIAPI\r
177SmmIplSetVirtualAddressNotify (\r
178 IN EFI_EVENT Event,\r
179 IN VOID *Context\r
180 );\r
181\r
182//\r
183// Data structure used to declare a table of protocol notifications and event \r
184// notifications required by the SMM IPL\r
185//\r
186typedef struct {\r
187 BOOLEAN Protocol;\r
188 BOOLEAN CloseOnLock;\r
189 EFI_GUID *Guid;\r
190 EFI_EVENT_NOTIFY NotifyFunction;\r
191 VOID *NotifyContext;\r
5657b268 192 EFI_TPL NotifyTpl;\r
e42e9404 193 EFI_EVENT Event;\r
194} SMM_IPL_EVENT_NOTIFICATION;\r
195\r
196//\r
197// Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r
198//\r
199EFI_HANDLE mSmmIplHandle = NULL;\r
200\r
201//\r
202// SMM Base 2 Protocol instance\r
203//\r
204EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r
205 SmmBase2InSmram,\r
206 SmmBase2GetSmstLocation\r
207};\r
208\r
209//\r
210// SMM Communication Protocol instance\r
211//\r
212EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r
213 SmmCommunicationCommunicate\r
214};\r
215\r
216//\r
217// SMM Core Private Data structure that contains the data shared between\r
218// the SMM IPL and the SMM Core.\r
219//\r
220SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r
221 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r
222 NULL, // SmmIplImageHandle\r
223 0, // SmramRangeCount\r
224 NULL, // SmramRanges\r
225 NULL, // SmmEntryPoint\r
226 FALSE, // SmmEntryPointRegistered\r
227 FALSE, // InSmm\r
228 NULL, // Smst\r
e42e9404 229 NULL, // CommunicationBuffer\r
ab780ebf 230 0, // BufferSize\r
e42e9404 231 EFI_SUCCESS // ReturnStatus\r
232};\r
233\r
234//\r
235// Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r
236//\r
237SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r
238\r
239//\r
240// SMM IPL global variables\r
241//\r
242EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r
243EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r
244EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r
245BOOLEAN mSmmLocked = FALSE;\r
40e8cca5 246EFI_PHYSICAL_ADDRESS mSmramCacheBase;\r
247UINT64 mSmramCacheSize;\r
e42e9404 248\r
249//\r
250// Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
251//\r
252SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r
253 //\r
254 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished, \r
255 // the associated event is immediately signalled, so the notification function will be executed and the \r
256 // SMM Configuration Protocol will be found if it is already in the handle database.\r
257 //\r
5657b268 258 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },\r
e42e9404 259 //\r
260 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished, \r
261 // the associated event is immediately signalled, so the notification function will be executed and the \r
262 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r
263 //\r
5657b268 264 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r
e42e9404 265 //\r
266 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r
267 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r
268 // if notified, so the SMM Core can dispatch SMM drivers.\r
269 //\r
5657b268 270 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },\r
e42e9404 271 //\r
272 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r
273 // used to make sure SMRAM is locked before any boot options are processed.\r
274 //\r
5657b268 275 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r
e42e9404 276 //\r
277 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform \r
278 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core \r
279 // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r
280 //\r
5657b268 281 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },\r
e42e9404 282 //\r
283 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate \r
284 // and mSmmControl2 from physical addresses to virtual addresses.\r
285 //\r
5657b268 286 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },\r
e42e9404 287 //\r
288 // Terminate the table of event notifications\r
289 //\r
5657b268 290 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }\r
e42e9404 291};\r
292\r
40e8cca5 293/**\r
294 Find the maximum SMRAM cache range that covers the range specified by SmramRange.\r
295 \r
296 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.\r
297\r
298 @param SmramRange The SMRAM range to search from.\r
299 @param SmramCacheBase The returned cache range base.\r
300 @param SmramCacheSize The returned cache range size.\r
301\r
302**/\r
303VOID\r
304GetSmramCacheRange (\r
305 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
306 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,\r
307 OUT UINT64 *SmramCacheSize\r
308 )\r
309{\r
310 UINTN Index;\r
311 EFI_PHYSICAL_ADDRESS RangeCpuStart;\r
312 UINT64 RangePhysicalSize;\r
313 BOOLEAN FoundAjacentRange;\r
314\r
315 *SmramCacheBase = SmramRange->CpuStart;\r
316 *SmramCacheSize = SmramRange->PhysicalSize;\r
317\r
318 do {\r
319 FoundAjacentRange = FALSE;\r
320 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
321 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
322 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
323 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {\r
324 *SmramCacheBase = RangeCpuStart;\r
325 *SmramCacheSize += RangePhysicalSize;\r
326 FoundAjacentRange = TRUE;\r
327 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {\r
328 *SmramCacheSize += RangePhysicalSize;\r
329 FoundAjacentRange = TRUE;\r
330 }\r
331 }\r
332 } while (FoundAjacentRange);\r
333 \r
334}\r
335\r
e42e9404 336/**\r
337 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
338\r
339 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
340 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
341 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
342\r
343 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
344 @retval EFI_SUCCESS The call returned successfully.\r
345\r
346**/\r
347EFI_STATUS\r
348EFIAPI\r
349SmmBase2InSmram (\r
350 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
351 OUT BOOLEAN *InSmram\r
352 )\r
353{\r
354 if (InSmram == NULL) {\r
355 return EFI_INVALID_PARAMETER;\r
356 }\r
357\r
358 *InSmram = gSmmCorePrivate->InSmm;\r
359\r
360 return EFI_SUCCESS;\r
361}\r
362\r
363/**\r
364 Retrieves the location of the System Management System Table (SMST).\r
365\r
366 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
367 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
368\r
369 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
370 @retval EFI_SUCCESS The memory was returned to the system.\r
371 @retval EFI_UNSUPPORTED Not in SMM.\r
372\r
373**/\r
374EFI_STATUS\r
375EFIAPI\r
376SmmBase2GetSmstLocation (\r
377 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
378 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
379 )\r
380{\r
381 if ((This == NULL) ||(Smst == NULL)) {\r
382 return EFI_INVALID_PARAMETER;\r
383 }\r
384 \r
385 if (!gSmmCorePrivate->InSmm) {\r
386 return EFI_UNSUPPORTED;\r
387 }\r
388 \r
389 *Smst = gSmmCorePrivate->Smst;\r
390\r
391 return EFI_SUCCESS;\r
392}\r
393\r
394/**\r
395 Communicates with a registered handler.\r
396 \r
397 This function provides a service to send and receive messages from a registered \r
398 UEFI service. This function is part of the SMM Communication Protocol that may \r
399 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
400 after SetVirtualAddressMap().\r
401\r
402 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
2292758d 403 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
404 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
e42e9404 405 being returned. Zero if the handler does not wish to reply with any data.\r
406\r
407 @retval EFI_SUCCESS The message was successfully posted.\r
408 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
409**/\r
410EFI_STATUS\r
411EFIAPI\r
412SmmCommunicationCommunicate (\r
413 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
414 IN OUT VOID *CommBuffer,\r
415 IN OUT UINTN *CommSize\r
416 )\r
417{\r
418 EFI_STATUS Status;\r
419 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
420 BOOLEAN OldInSmm;\r
421\r
422 //\r
423 // Check parameters\r
424 //\r
425 if ((CommBuffer == NULL) || (CommSize == NULL)) {\r
426 return EFI_INVALID_PARAMETER;\r
427 }\r
428\r
ab780ebf
JY
429 //\r
430 // CommSize must hold HeaderGuid and MessageLength\r
431 //\r
432 if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
433 return EFI_INVALID_PARAMETER;\r
434 }\r
435\r
e42e9404 436 //\r
437 // If not already in SMM, then generate a Software SMI\r
438 //\r
439 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
440 //\r
441 // Put arguments for Software SMI in gSmmCorePrivate\r
442 //\r
443 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
ab780ebf 444 gSmmCorePrivate->BufferSize = *CommSize;\r
e42e9404 445\r
446 //\r
447 // Generate Software SMI\r
448 //\r
449 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
450 if (EFI_ERROR (Status)) {\r
451 return EFI_UNSUPPORTED;\r
452 }\r
453\r
454 //\r
455 // Return status from software SMI \r
456 //\r
ab780ebf 457 *CommSize = gSmmCorePrivate->BufferSize;\r
e42e9404 458 return gSmmCorePrivate->ReturnStatus;\r
459 }\r
460\r
461 //\r
462 // If we are in SMM, then the execution mode must be physical, which means that\r
463 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
464 // has been called, then a direct invocation of the Software SMI is not \r
465 // not allowed so return EFI_INVALID_PARAMETER.\r
466 //\r
467 if (EfiGoneVirtual()) {\r
468 return EFI_INVALID_PARAMETER;\r
469 }\r
470\r
3c5963cf 471 //\r
96756716 472 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.\r
3c5963cf 473 //\r
96756716 474 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r
3c5963cf 475 return EFI_INVALID_PARAMETER;\r
476 }\r
477 \r
e42e9404 478 //\r
479 // Save current InSmm state and set InSmm state to TRUE\r
480 //\r
481 OldInSmm = gSmmCorePrivate->InSmm;\r
482 gSmmCorePrivate->InSmm = TRUE;\r
483\r
484 //\r
485 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
486 //\r
487 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
488 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
489 Status = gSmmCorePrivate->Smst->SmiManage (\r
490 &CommunicateHeader->HeaderGuid, \r
491 NULL, \r
492 CommunicateHeader->Data, \r
493 CommSize\r
494 );\r
495\r
496 //\r
497 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
498 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
499 //\r
500 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
501\r
502 //\r
503 // Restore original InSmm state\r
504 //\r
505 gSmmCorePrivate->InSmm = OldInSmm;\r
506\r
507 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
508}\r
509\r
510/**\r
5657b268 511 Event notification that is fired when GUIDed Event Group is signaled.\r
e42e9404 512\r
513 @param Event The Event that is being processed, not used.\r
514 @param Context Event Context, not used.\r
515\r
516**/\r
517VOID\r
518EFIAPI\r
519SmmIplGuidedEventNotify (\r
520 IN EFI_EVENT Event,\r
521 IN VOID *Context\r
522 )\r
523{\r
524 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
525 UINTN Size;\r
526\r
527 //\r
528 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
529 //\r
530 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
531 CommunicateHeader.MessageLength = 1;\r
532 CommunicateHeader.Data[0] = 0;\r
533\r
534 //\r
535 // Generate the Software SMI and return the result\r
536 //\r
537 Size = sizeof (CommunicateHeader);\r
538 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
539}\r
540\r
5657b268 541/**\r
542 Event notification that is fired when DxeDispatch Event Group is signaled.\r
543\r
544 @param Event The Event that is being processed, not used.\r
545 @param Context Event Context, not used.\r
546\r
547**/\r
548VOID\r
549EFIAPI\r
550SmmIplDxeDispatchEventNotify (\r
551 IN EFI_EVENT Event,\r
552 IN VOID *Context\r
553 )\r
554{\r
555 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
556 UINTN Size;\r
557 EFI_STATUS Status;\r
558\r
559 //\r
560 // Keep calling the SMM Core Dispatcher until there is no request to restart it.\r
561 //\r
562 while (TRUE) {\r
563 //\r
564 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r
565 // Clear the buffer passed into the Software SMI. This buffer will return\r
566 // the status of the SMM Core Dispatcher.\r
567 //\r
568 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
569 CommunicateHeader.MessageLength = 1;\r
570 CommunicateHeader.Data[0] = 0;\r
571\r
572 //\r
573 // Generate the Software SMI and return the result\r
574 //\r
575 Size = sizeof (CommunicateHeader);\r
576 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
577 \r
578 //\r
579 // Return if there is no request to restart the SMM Core Dispatcher\r
580 //\r
581 if (CommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
582 return;\r
583 }\r
584 \r
585 //\r
586 // Attempt to reset SMRAM cacheability to UC\r
587 // Assume CPU AP is available at this time\r
588 //\r
589 Status = gDS->SetMemorySpaceAttributes(\r
590 mSmramCacheBase, \r
591 mSmramCacheSize,\r
592 EFI_MEMORY_UC\r
593 );\r
594 if (EFI_ERROR (Status)) {\r
595 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
596 } \r
597\r
598 //\r
599 // Close all SMRAM ranges to protect SMRAM\r
600 //\r
601 Status = mSmmAccess->Close (mSmmAccess);\r
602 ASSERT_EFI_ERROR (Status);\r
603\r
604 //\r
605 // Print debug message that the SMRAM window is now closed.\r
606 //\r
607 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
5657b268 608 }\r
609}\r
610\r
e42e9404 611/**\r
612 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
613\r
614 @param Event The Event that is being processed, not used.\r
615 @param Context Event Context, not used.\r
616\r
617**/\r
618VOID\r
619EFIAPI\r
620SmmIplSmmConfigurationEventNotify (\r
621 IN EFI_EVENT Event,\r
622 IN VOID *Context\r
623 )\r
624{\r
625 EFI_STATUS Status;\r
626 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
627\r
628 //\r
629 // Make sure this notification is for this handler\r
630 //\r
631 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
632 if (EFI_ERROR (Status)) {\r
633 return;\r
634 }\r
635\r
636 //\r
637 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
638 //\r
639 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
640 ASSERT_EFI_ERROR (Status);\r
641\r
642 //\r
5657b268 643 // Set flag to indicate that the SMM Entry Point has been registered which \r
e42e9404 644 // means that SMIs are now fully operational.\r
645 //\r
646 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
647\r
648 //\r
649 // Print debug message showing SMM Core entry point address.\r
650 //\r
651 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
e42e9404 652}\r
653\r
654/**\r
655 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
656 or if gEfiEventReadyToBootGuid is signalled.\r
657\r
658 @param Event The Event that is being processed, not used.\r
659 @param Context Event Context, not used.\r
660\r
661**/\r
662VOID\r
663EFIAPI\r
664SmmIplReadyToLockEventNotify (\r
665 IN EFI_EVENT Event,\r
666 IN VOID *Context\r
667 )\r
668{\r
669 EFI_STATUS Status;\r
670 VOID *Interface;\r
671 UINTN Index;\r
672\r
673 //\r
674 // See if we are already locked\r
675 //\r
676 if (mSmmLocked) {\r
677 return;\r
678 }\r
679 \r
680 //\r
681 // Make sure this notification is for this handler\r
682 //\r
683 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
684 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
685 if (EFI_ERROR (Status)) {\r
686 return;\r
687 }\r
688 } else {\r
689 //\r
690 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
691 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
692 // Print a warning on debug builds.\r
693 //\r
694 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
695 }\r
696\r
697 //\r
698 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
699 //\r
700 mSmmAccess->Lock (mSmmAccess);\r
f02dfb5a 701 \r
e42e9404 702 //\r
703 // Close protocol and event notification events that do not apply after the \r
704 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
705 // event has been signalled.\r
706 //\r
707 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
708 if (mSmmIplEvents[Index].CloseOnLock) {\r
709 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
710 }\r
711 }\r
712\r
713 //\r
714 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
715 //\r
716 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
717\r
718 //\r
719 // Print debug message that the SMRAM window is now locked.\r
720 //\r
721 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
722 \r
723 //\r
724 // Set flag so this operation will not be performed again\r
725 //\r
726 mSmmLocked = TRUE;\r
727}\r
728\r
729/**\r
730 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
731\r
732 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
733 It convers pointer to new virtual address.\r
734\r
735 @param Event Event whose notification function is being invoked.\r
736 @param Context Pointer to the notification function's context.\r
737\r
738**/\r
739VOID\r
740EFIAPI\r
741SmmIplSetVirtualAddressNotify (\r
742 IN EFI_EVENT Event,\r
743 IN VOID *Context\r
744 )\r
745{\r
746 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
747}\r
748\r
3c447c27 749/**\r
750 Get the fixed loadding address from image header assigned by build tool. This function only be called\r
751 when Loading module at Fixed address feature enabled.\r
e42e9404 752\r
3c447c27 753 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
754 image that needs to be examined by this function.\r
755 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
756 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
757**/\r
758EFI_STATUS\r
759GetPeCoffImageFixLoadingAssignedAddress(\r
760 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
761 )\r
762{\r
763 UINTN SectionHeaderOffset;\r
764 EFI_STATUS Status;\r
765 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
766 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
767 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
768 UINT16 Index;\r
769 UINTN Size;\r
770 UINT16 NumberOfSections;\r
771 EFI_PHYSICAL_ADDRESS SmramBase;\r
772 UINT64 SmmCodeSize;\r
773 UINT64 ValueInSectionHeader;\r
774 //\r
775 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
776 //\r
777 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
778 \r
779 FixLoaddingAddress = 0;\r
780 Status = EFI_NOT_FOUND;\r
781 SmramBase = mCurrentSmramRange->CpuStart;\r
782 //\r
783 // Get PeHeader pointer\r
784 //\r
785 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
786 SectionHeaderOffset = (UINTN)(\r
787 ImageContext->PeCoffHeaderOffset +\r
788 sizeof (UINT32) +\r
789 sizeof (EFI_IMAGE_FILE_HEADER) +\r
790 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
791 );\r
792 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
793\r
794 //\r
795 // Get base address from the first section header that doesn't point to code section.\r
796 //\r
797 for (Index = 0; Index < NumberOfSections; Index++) {\r
798 //\r
799 // Read section header from file\r
800 //\r
801 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
802 Status = ImageContext->ImageRead (\r
803 ImageContext->Handle,\r
804 SectionHeaderOffset,\r
805 &Size,\r
806 &SectionHeader\r
807 );\r
808 if (EFI_ERROR (Status)) {\r
809 return Status;\r
810 }\r
811 \r
812 Status = EFI_NOT_FOUND;\r
813 \r
814 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
815 //\r
816 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
817 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
818 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
819 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
820 //\r
821 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
822 if (ValueInSectionHeader != 0) {\r
823 //\r
824 // Found first section header that doesn't point to code section in which uild tool saves the\r
825 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
826 //\r
827 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
828\r
829 if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <= FixLoaddingAddress) {\r
830 //\r
831 // The assigned address is valid. Return the specified loadding address\r
832 //\r
833 ImageContext->ImageAddress = FixLoaddingAddress;\r
834 Status = EFI_SUCCESS;\r
835 }\r
836 }\r
837 break;\r
838 }\r
839 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
840 }\r
841 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));\r
842 return Status;\r
843}\r
e42e9404 844/**\r
845 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
846\r
847 @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r
848 currently executing image.\r
849 @param[in] Context Context to pass into SMM Core\r
850\r
851 @return EFI_STATUS\r
852\r
853**/\r
854EFI_STATUS\r
855ExecuteSmmCoreFromSmram (\r
856 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
857 IN VOID *Context\r
858 )\r
859{\r
860 EFI_STATUS Status;\r
861 VOID *SourceBuffer;\r
862 UINTN SourceSize;\r
863 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
864 UINTN PageCount;\r
865 EFI_PHYSICAL_ADDRESS DestinationBuffer;\r
866 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
867\r
868 //\r
869 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
870 // \r
d7aaf1dc
LG
871 Status = GetSectionFromAnyFvByFileType (\r
872 EFI_FV_FILETYPE_SMM_CORE, \r
873 0,\r
874 EFI_SECTION_PE32, \r
875 0,\r
876 &SourceBuffer, \r
877 &SourceSize\r
878 );\r
879 if (EFI_ERROR (Status)) {\r
880 return Status;\r
e42e9404 881 }\r
882 \r
883 //\r
884 // Initilize ImageContext\r
885 //\r
886 ImageContext.Handle = SourceBuffer;\r
887 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
888\r
889 //\r
890 // Get information about the image being loaded\r
891 //\r
892 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
893 if (EFI_ERROR (Status)) {\r
894 return Status;\r
895 }\r
e42e9404 896 //\r
3c447c27 897 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
898 // the address assigned by build tool.\r
e42e9404 899 //\r
3c447c27 900 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
901 //\r
902 // Get the fixed loading address assigned by Build tool\r
903 //\r
904 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
905 if (!EFI_ERROR (Status)) {\r
906 //\r
907 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range\r
908 //\r
909 PageCount = 0;\r
910 } else {\r
911 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
912 //\r
913 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
914 // specified by SmramRange\r
915 //\r
916 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 917\r
3c447c27 918 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
919 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 920\r
3c447c27 921 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
922 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
e42e9404 923\r
3c447c27 924 //\r
925 // Align buffer on section boundry\r
926 //\r
927 ImageContext.ImageAddress = DestinationBuffer;\r
928 }\r
929 } else {\r
930 //\r
931 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
932 // specified by SmramRange\r
933 //\r
934 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
935\r
936 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
937 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
938\r
939 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
940 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
941\r
942 //\r
943 // Align buffer on section boundry\r
944 //\r
945 ImageContext.ImageAddress = DestinationBuffer;\r
946 }\r
947 \r
e42e9404 948 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
949 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
950\r
951 //\r
952 // Print debug message showing SMM Core load address.\r
953 //\r
954 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
955\r
956 //\r
957 // Load the image to our new buffer\r
958 //\r
959 Status = PeCoffLoaderLoadImage (&ImageContext);\r
960 if (!EFI_ERROR (Status)) {\r
961 //\r
962 // Relocate the image in our new buffer\r
963 //\r
964 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
965 if (!EFI_ERROR (Status)) {\r
966 //\r
967 // Flush the instruction cache so the image data are written before we execute it\r
968 //\r
969 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
970\r
971 //\r
972 // Print debug message showing SMM Core entry point address.\r
973 //\r
974 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
975\r
976 //\r
977 // Execute image\r
978 //\r
979 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
980 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
981 }\r
982 }\r
983\r
984 //\r
985 // If the load operation, relocate operation, or the image execution return an\r
986 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r
987 // SmramRange\r
988 //\r
989 if (EFI_ERROR (Status)) {\r
990 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r
991 }\r
992\r
993 //\r
994 // Always free memory allocted by GetFileBufferByFilePath ()\r
995 //\r
996 FreePool (SourceBuffer);\r
997\r
998 return Status;\r
999}\r
1000\r
1001/**\r
1002 The Entry Point for SMM IPL\r
1003\r
1004 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
1005 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
1006 critical events required to coordinate between DXE and SMM environments.\r
1007 \r
1008 @param ImageHandle The firmware allocated handle for the EFI image.\r
1009 @param SystemTable A pointer to the EFI System Table.\r
1010\r
1011 @retval EFI_SUCCESS The entry point is executed successfully.\r
1012 @retval Other Some error occurred when executing this entry point.\r
1013\r
1014**/\r
1015EFI_STATUS\r
1016EFIAPI\r
1017SmmIplEntry (\r
1018 IN EFI_HANDLE ImageHandle,\r
1019 IN EFI_SYSTEM_TABLE *SystemTable\r
1020 )\r
1021{\r
1022 EFI_STATUS Status;\r
1023 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1024 UINTN Size;\r
1025 UINTN Index;\r
1026 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r
1027 UINT64 MaxSize;\r
1028 VOID *Registration;\r
07d9dc83 1029 UINT64 SmmCodeSize;\r
3c447c27 1030 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r
07d9dc83 1031 EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
e42e9404 1032\r
1033 //\r
1034 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
1035 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
1036 // by the SMM Core\r
1037 //\r
1038 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1039\r
1040 //\r
1041 // Get SMM Access Protocol\r
1042 //\r
1043 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1044 ASSERT_EFI_ERROR (Status);\r
1045\r
1046 //\r
1047 // Get SMM Control2 Protocol\r
1048 //\r
1049 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1050 ASSERT_EFI_ERROR (Status);\r
1051\r
1052 //\r
1053 // Get SMM Configuration Protocol if it is present\r
1054 //\r
1055 SmmConfiguration = NULL;\r
1056 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1057\r
1058 //\r
1059 // Get SMRAM information\r
1060 //\r
1061 Size = 0;\r
1062 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1063 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1064\r
1065 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
1066 ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r
1067\r
1068 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r
1069 ASSERT_EFI_ERROR (Status);\r
1070\r
1071 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1072\r
1073 //\r
1074 // Open all SMRAM ranges\r
1075 //\r
1076 Status = mSmmAccess->Open (mSmmAccess);\r
1077 ASSERT_EFI_ERROR (Status);\r
1078\r
1079 //\r
1080 // Print debug message that the SMRAM window is now open.\r
1081 //\r
1082 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
1083\r
1084 //\r
1085 // Subtract SMRAM any reserved SMRAM regions.\r
1086 //\r
1087 if (SmmConfiguration != NULL) {\r
1088 SmramResRegion = SmmConfiguration->SmramReservedRegions;\r
1089 while (SmramResRegion->SmramReservedSize != 0) {\r
1090 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r
1091 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r
1092 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r
1093 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r
1094 //\r
1095 // This range has reserved area, calculate the left free size\r
1096 //\r
1097 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
1098 }\r
1099 }\r
1100 SmramResRegion++;\r
1101 }\r
1102 }\r
1103 \r
1104 //\r
06b07ce3 1105 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1106 //\r
1107 mCurrentSmramRange = NULL;\r
06b07ce3 1108 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
2c0f06f0 1109 //\r
1110 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
1111 //\r
1112 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
1113 continue;\r
1114 }\r
1115\r
e42e9404 1116 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
1117 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
1118 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1119 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1120 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1121 }\r
1122 }\r
1123 }\r
1124 }\r
1125\r
1126 if (mCurrentSmramRange != NULL) {\r
1127 //\r
1128 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1129 //\r
1130 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1131 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1132 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1133 ));\r
1134\r
40e8cca5 1135 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1136 //\r
07d9dc83 1137 // If CPU AP is present, attempt to set SMRAM cacheability to WB\r
1138 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
1139 // is not available here.\r
e42e9404 1140 //\r
0a6c0905 1141 CpuArch = NULL;\r
07d9dc83 1142 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
1143 if (!EFI_ERROR (Status)) {\r
1144 Status = gDS->SetMemorySpaceAttributes(\r
1145 mSmramCacheBase, \r
1146 mSmramCacheSize,\r
1147 EFI_MEMORY_WB\r
1148 );\r
1149 if (EFI_ERROR (Status)) {\r
1150 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1151 } \r
1152 }\r
3c447c27 1153 //\r
1154 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1155 // Modules At Fixed Address Configuration Table.\r
1156 //\r
1157 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1158 //\r
1159 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1160 //\r
1161 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1162 //\r
1163 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1164 //\r
1165 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1166 //\r
1167 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1168 //\r
1169 Status = EfiGetSystemConfigurationTable (\r
1170 &gLoadFixedAddressConfigurationTableGuid,\r
1171 (VOID **) &LMFAConfigurationTable\r
1172 );\r
1173 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
1174 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1175 //\r
1176 // Print the SMRAM base\r
1177 //\r
1178 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
3c447c27 1179 }\r
3c447c27 1180 }\r
e42e9404 1181 //\r
1182 // Load SMM Core into SMRAM and execute it from SMRAM\r
1183 //\r
1184 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r
1185 if (EFI_ERROR (Status)) {\r
1186 //\r
1187 // Print error message that the SMM Core failed to be loaded and executed.\r
1188 //\r
1189 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1190\r
1191 //\r
1192 // Attempt to reset SMRAM cacheability to UC\r
1193 //\r
0a6c0905 1194 if (CpuArch != NULL) {\r
1195 Status = gDS->SetMemorySpaceAttributes(\r
1196 mSmramCacheBase, \r
1197 mSmramCacheSize,\r
1198 EFI_MEMORY_UC\r
1199 );\r
1200 if (EFI_ERROR (Status)) {\r
1201 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1202 } \r
1203 }\r
e42e9404 1204 }\r
1205 } else {\r
1206 //\r
1207 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1208 //\r
1209 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1210 }\r
1211\r
1212 //\r
1213 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1214 // resources, and return an error so SMM IPL will be unloaded.\r
1215 //\r
1216 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1217 //\r
1218 // Close all SMRAM ranges\r
1219 //\r
1220 Status = mSmmAccess->Close (mSmmAccess);\r
1221 ASSERT_EFI_ERROR (Status);\r
1222\r
1223 //\r
1224 // Print debug message that the SMRAM window is now closed.\r
1225 //\r
1226 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1227\r
1228 //\r
1229 // Free all allocated resources\r
1230 //\r
1231 FreePool (gSmmCorePrivate->SmramRanges);\r
1232 \r
1233 return EFI_UNSUPPORTED;\r
1234 }\r
1235 \r
1236 //\r
1237 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1238 //\r
1239 Status = gBS->InstallMultipleProtocolInterfaces (\r
1240 &mSmmIplHandle,\r
1241 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1242 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1243 NULL\r
1244 );\r
1245 ASSERT_EFI_ERROR (Status);\r
1246\r
1247 //\r
1248 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1249 //\r
1250 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1251 if (mSmmIplEvents[Index].Protocol) {\r
1252 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1253 mSmmIplEvents[Index].Guid,\r
5657b268 1254 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1255 mSmmIplEvents[Index].NotifyFunction,\r
1256 mSmmIplEvents[Index].NotifyContext,\r
1257 &Registration\r
1258 );\r
1259 } else {\r
1260 Status = gBS->CreateEventEx (\r
1261 EVT_NOTIFY_SIGNAL,\r
5657b268 1262 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1263 mSmmIplEvents[Index].NotifyFunction,\r
1264 mSmmIplEvents[Index].NotifyContext,\r
1265 mSmmIplEvents[Index].Guid,\r
1266 &mSmmIplEvents[Index].Event\r
1267 );\r
1268 ASSERT_EFI_ERROR (Status);\r
1269 }\r
1270 }\r
1271\r
1272 return EFI_SUCCESS;\r
1273}\r