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