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