]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg/Core/PiSmmCore: Fix various typos
[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
d1102dba 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e42e9404 6\r
7**/\r
8\r
9#include <PiDxe.h>\r
10\r
11#include <Protocol/SmmBase2.h>\r
12#include <Protocol/SmmCommunication.h>\r
13#include <Protocol/SmmAccess2.h>\r
14#include <Protocol/SmmConfiguration.h>\r
15#include <Protocol/SmmControl2.h>\r
16#include <Protocol/DxeSmmReadyToLock.h>\r
07d9dc83 17#include <Protocol/Cpu.h>\r
e42e9404 18\r
19#include <Guid/EventGroup.h>\r
20#include <Guid/EventLegacyBios.h>\r
3c447c27 21#include <Guid/LoadModuleAtFixedAddress.h>\r
e42e9404 22\r
23#include <Library/BaseLib.h>\r
24#include <Library/BaseMemoryLib.h>\r
25#include <Library/PeCoffLib.h>\r
26#include <Library/CacheMaintenanceLib.h>\r
27#include <Library/MemoryAllocationLib.h>\r
28#include <Library/DebugLib.h>\r
29#include <Library/UefiBootServicesTableLib.h>\r
30#include <Library/DxeServicesTableLib.h>\r
d7aaf1dc 31#include <Library/DxeServicesLib.h>\r
e42e9404 32#include <Library/UefiLib.h>\r
33#include <Library/UefiRuntimeLib.h>\r
3c447c27 34#include <Library/PcdLib.h>\r
265fff39 35#include <Library/ReportStatusCodeLib.h>\r
e42e9404 36\r
37#include "PiSmmCorePrivateData.h"\r
38\r
54ec85dd
JW
39#define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)\r
40\r
41#define MEMORY_CACHE_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \\r
42 EFI_MEMORY_WT | EFI_MEMORY_WB | \\r
43 EFI_MEMORY_WP | EFI_MEMORY_UCE)\r
44\r
45#define MEMORY_PAGE_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | EFI_MEMORY_RO)\r
46\r
e42e9404 47//\r
48// Function prototypes from produced protocols\r
49//\r
50\r
51/**\r
52 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
53\r
54 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
55 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
56 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
57\r
58 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
59 @retval EFI_SUCCESS The call returned successfully.\r
60\r
61**/\r
62EFI_STATUS\r
63EFIAPI\r
64SmmBase2InSmram (\r
65 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
66 OUT BOOLEAN *InSmram\r
67 );\r
68\r
69/**\r
70 Retrieves the location of the System Management System Table (SMST).\r
71\r
72 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
73 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
74\r
75 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
76 @retval EFI_SUCCESS The memory was returned to the system.\r
77 @retval EFI_UNSUPPORTED Not in SMM.\r
78\r
79**/\r
80EFI_STATUS\r
81EFIAPI\r
82SmmBase2GetSmstLocation (\r
83 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
84 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
85 );\r
86\r
87/**\r
88 Communicates with a registered handler.\r
d1102dba
LG
89\r
90 This function provides a service to send and receive messages from a registered\r
91 UEFI service. This function is part of the SMM Communication Protocol that may\r
92 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r
e42e9404 93 after SetVirtualAddressMap().\r
94\r
8c721557
SZ
95 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
96 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
97 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data\r
98 being returned. Zero if the handler does not wish to reply with any data.\r
99 This parameter is optional and may be NULL.\r
100\r
101 @retval EFI_SUCCESS The message was successfully posted.\r
102 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
103 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r
104 If this error is returned, the MessageLength field\r
105 in the CommBuffer header or the integer pointed by\r
106 CommSize, are updated to reflect the maximum payload\r
107 size the implementation can accommodate.\r
108 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r
109 if not omitted, are in address range that cannot be\r
110 accessed by the MM environment.\r
e42e9404 111\r
e42e9404 112**/\r
113EFI_STATUS\r
114EFIAPI\r
115SmmCommunicationCommunicate (\r
116 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
117 IN OUT VOID *CommBuffer,\r
8c721557 118 IN OUT UINTN *CommSize OPTIONAL\r
e42e9404 119 );\r
120\r
121/**\r
122 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
123\r
124 @param Event The Event that is being processed, not used.\r
125 @param Context Event Context, not used.\r
126\r
127**/\r
128VOID\r
129EFIAPI\r
130SmmIplSmmConfigurationEventNotify (\r
131 IN EFI_EVENT Event,\r
132 IN VOID *Context\r
133 );\r
134\r
135/**\r
136 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
137 or if gEfiEventReadyToBootGuid is signalled.\r
138\r
139 @param Event The Event that is being processed, not used.\r
140 @param Context Event Context, not used.\r
141\r
142**/\r
143VOID\r
144EFIAPI\r
145SmmIplReadyToLockEventNotify (\r
146 IN EFI_EVENT Event,\r
147 IN VOID *Context\r
148 );\r
149\r
150/**\r
151 Event notification that is fired when DxeDispatch Event Group is signaled.\r
152\r
153 @param Event The Event that is being processed, not used.\r
154 @param Context Event Context, not used.\r
155\r
5657b268 156**/\r
157VOID\r
158EFIAPI\r
159SmmIplDxeDispatchEventNotify (\r
160 IN EFI_EVENT Event,\r
161 IN VOID *Context\r
162 );\r
163\r
164/**\r
165 Event notification that is fired when a GUIDed Event Group is signaled.\r
166\r
167 @param Event The Event that is being processed, not used.\r
168 @param Context Event Context, not used.\r
169\r
e42e9404 170**/\r
171VOID\r
172EFIAPI\r
173SmmIplGuidedEventNotify (\r
174 IN EFI_EVENT Event,\r
175 IN VOID *Context\r
176 );\r
177\r
265fff39
JF
178/**\r
179 Event notification that is fired when EndOfDxe Event Group is signaled.\r
180\r
181 @param Event The Event that is being processed, not used.\r
182 @param Context Event Context, not used.\r
183\r
184**/\r
185VOID\r
186EFIAPI\r
187SmmIplEndOfDxeEventNotify (\r
188 IN EFI_EVENT Event,\r
189 IN VOID *Context\r
190 );\r
191\r
e42e9404 192/**\r
193 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
194\r
195 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
196 It convers pointer to new virtual address.\r
197\r
198 @param Event Event whose notification function is being invoked.\r
199 @param Context Pointer to the notification function's context.\r
200\r
201**/\r
202VOID\r
203EFIAPI\r
204SmmIplSetVirtualAddressNotify (\r
205 IN EFI_EVENT Event,\r
206 IN VOID *Context\r
207 );\r
208\r
209//\r
d1102dba 210// Data structure used to declare a table of protocol notifications and event\r
e42e9404 211// notifications required by the SMM IPL\r
212//\r
213typedef struct {\r
214 BOOLEAN Protocol;\r
215 BOOLEAN CloseOnLock;\r
216 EFI_GUID *Guid;\r
217 EFI_EVENT_NOTIFY NotifyFunction;\r
218 VOID *NotifyContext;\r
5657b268 219 EFI_TPL NotifyTpl;\r
e42e9404 220 EFI_EVENT Event;\r
221} SMM_IPL_EVENT_NOTIFICATION;\r
222\r
223//\r
224// Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r
225//\r
226EFI_HANDLE mSmmIplHandle = NULL;\r
227\r
228//\r
229// SMM Base 2 Protocol instance\r
230//\r
231EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r
232 SmmBase2InSmram,\r
233 SmmBase2GetSmstLocation\r
234};\r
235\r
236//\r
237// SMM Communication Protocol instance\r
238//\r
239EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r
240 SmmCommunicationCommunicate\r
241};\r
242\r
243//\r
244// SMM Core Private Data structure that contains the data shared between\r
245// the SMM IPL and the SMM Core.\r
246//\r
247SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r
248 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r
249 NULL, // SmmIplImageHandle\r
250 0, // SmramRangeCount\r
251 NULL, // SmramRanges\r
252 NULL, // SmmEntryPoint\r
253 FALSE, // SmmEntryPointRegistered\r
254 FALSE, // InSmm\r
255 NULL, // Smst\r
e42e9404 256 NULL, // CommunicationBuffer\r
ab780ebf 257 0, // BufferSize\r
e42e9404 258 EFI_SUCCESS // ReturnStatus\r
259};\r
260\r
261//\r
262// Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r
263//\r
264SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r
265\r
266//\r
267// SMM IPL global variables\r
268//\r
269EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r
270EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r
271EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r
272BOOLEAN mSmmLocked = FALSE;\r
265fff39 273BOOLEAN mEndOfDxe = FALSE;\r
40e8cca5 274EFI_PHYSICAL_ADDRESS mSmramCacheBase;\r
275UINT64 mSmramCacheSize;\r
e42e9404 276\r
670a563f 277EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;\r
c2aeb66f 278EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *mLMFAConfigurationTable = NULL;\r
670a563f 279\r
e42e9404 280//\r
281// Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
282//\r
283SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r
284 //\r
2048c585 285 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,\r
d1102dba 286 // the associated event is immediately signalled, so the notification function will be executed and the\r
e42e9404 287 // SMM Configuration Protocol will be found if it is already in the handle database.\r
288 //\r
5657b268 289 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },\r
e42e9404 290 //\r
d1102dba
LG
291 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,\r
292 // the associated event is immediately signalled, so the notification function will be executed and the\r
e42e9404 293 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r
294 //\r
5657b268 295 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r
e42e9404 296 //\r
2048c585 297 // Declare event notification on EndOfDxe event. When this notification is established,\r
d1102dba 298 // the associated event is immediately signalled, so the notification function will be executed and the\r
46ece1ff
JY
299 // SMM End Of Dxe Protocol will be found if it is already in the handle database.\r
300 //\r
dc157845 301 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },\r
46ece1ff 302 //\r
265fff39
JF
303 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.\r
304 //\r
305 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplEndOfDxeEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },\r
306 //\r
e42e9404 307 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r
308 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r
309 // if notified, so the SMM Core can dispatch SMM drivers.\r
310 //\r
5657b268 311 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },\r
e42e9404 312 //\r
313 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r
314 // used to make sure SMRAM is locked before any boot options are processed.\r
315 //\r
5657b268 316 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r
e42e9404 317 //\r
d1102dba
LG
318 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform\r
319 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core\r
e42e9404 320 // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r
53ec4d7f 321 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.\r
e42e9404 322 //\r
5657b268 323 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },\r
e42e9404 324 //\r
53ec4d7f
SZ
325 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core\r
326 // to notify SMM driver that system enter exit boot services.\r
327 //\r
328 { FALSE, FALSE, &gEfiEventExitBootServicesGuid, SmmIplGuidedEventNotify, &gEfiEventExitBootServicesGuid, TPL_CALLBACK, NULL },\r
329 //\r
330 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core\r
331 // to notify SMM driver that system enter ready to boot.\r
332 //\r
333 { FALSE, FALSE, &gEfiEventReadyToBootGuid, SmmIplGuidedEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r
334 //\r
d1102dba 335 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate\r
e42e9404 336 // and mSmmControl2 from physical addresses to virtual addresses.\r
337 //\r
5657b268 338 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },\r
e42e9404 339 //\r
340 // Terminate the table of event notifications\r
341 //\r
5657b268 342 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }\r
e42e9404 343};\r
344\r
40e8cca5 345/**\r
346 Find the maximum SMRAM cache range that covers the range specified by SmramRange.\r
d1102dba 347\r
40e8cca5 348 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.\r
349\r
350 @param SmramRange The SMRAM range to search from.\r
351 @param SmramCacheBase The returned cache range base.\r
352 @param SmramCacheSize The returned cache range size.\r
353\r
354**/\r
355VOID\r
356GetSmramCacheRange (\r
357 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
358 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,\r
359 OUT UINT64 *SmramCacheSize\r
360 )\r
361{\r
362 UINTN Index;\r
363 EFI_PHYSICAL_ADDRESS RangeCpuStart;\r
364 UINT64 RangePhysicalSize;\r
365 BOOLEAN FoundAjacentRange;\r
366\r
367 *SmramCacheBase = SmramRange->CpuStart;\r
368 *SmramCacheSize = SmramRange->PhysicalSize;\r
369\r
370 do {\r
371 FoundAjacentRange = FALSE;\r
372 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
373 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
374 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
375 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {\r
376 *SmramCacheBase = RangeCpuStart;\r
377 *SmramCacheSize += RangePhysicalSize;\r
378 FoundAjacentRange = TRUE;\r
379 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {\r
380 *SmramCacheSize += RangePhysicalSize;\r
381 FoundAjacentRange = TRUE;\r
382 }\r
383 }\r
384 } while (FoundAjacentRange);\r
d1102dba 385\r
40e8cca5 386}\r
387\r
e42e9404 388/**\r
389 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
390\r
391 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
392 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
393 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
394\r
395 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
396 @retval EFI_SUCCESS The call returned successfully.\r
397\r
398**/\r
399EFI_STATUS\r
400EFIAPI\r
401SmmBase2InSmram (\r
402 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
403 OUT BOOLEAN *InSmram\r
404 )\r
405{\r
406 if (InSmram == NULL) {\r
407 return EFI_INVALID_PARAMETER;\r
408 }\r
409\r
410 *InSmram = gSmmCorePrivate->InSmm;\r
411\r
412 return EFI_SUCCESS;\r
413}\r
414\r
415/**\r
416 Retrieves the location of the System Management System Table (SMST).\r
417\r
418 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
419 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
420\r
421 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
422 @retval EFI_SUCCESS The memory was returned to the system.\r
423 @retval EFI_UNSUPPORTED Not in SMM.\r
424\r
425**/\r
426EFI_STATUS\r
427EFIAPI\r
428SmmBase2GetSmstLocation (\r
429 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
430 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
431 )\r
432{\r
433 if ((This == NULL) ||(Smst == NULL)) {\r
434 return EFI_INVALID_PARAMETER;\r
435 }\r
d1102dba 436\r
e42e9404 437 if (!gSmmCorePrivate->InSmm) {\r
438 return EFI_UNSUPPORTED;\r
439 }\r
d1102dba 440\r
e42e9404 441 *Smst = gSmmCorePrivate->Smst;\r
442\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446/**\r
447 Communicates with a registered handler.\r
d1102dba
LG
448\r
449 This function provides a service to send and receive messages from a registered\r
450 UEFI service. This function is part of the SMM Communication Protocol that may\r
451 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r
e42e9404 452 after SetVirtualAddressMap().\r
453\r
454 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
d1632f69
SZ
455 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
456 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data\r
e42e9404 457 being returned. Zero if the handler does not wish to reply with any data.\r
d1632f69 458 This parameter is optional and may be NULL.\r
e42e9404 459\r
460 @retval EFI_SUCCESS The message was successfully posted.\r
461 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
d1632f69
SZ
462 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r
463 If this error is returned, the MessageLength field\r
464 in the CommBuffer header or the integer pointed by\r
465 CommSize, are updated to reflect the maximum payload\r
466 size the implementation can accommodate.\r
467 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r
468 if not omitted, are in address range that cannot be\r
469 accessed by the MM environment.\r
470\r
e42e9404 471**/\r
472EFI_STATUS\r
473EFIAPI\r
474SmmCommunicationCommunicate (\r
475 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
476 IN OUT VOID *CommBuffer,\r
d1632f69 477 IN OUT UINTN *CommSize OPTIONAL\r
e42e9404 478 )\r
479{\r
480 EFI_STATUS Status;\r
481 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
482 BOOLEAN OldInSmm;\r
d1632f69 483 UINTN TempCommSize;\r
e42e9404 484\r
485 //\r
486 // Check parameters\r
487 //\r
d1632f69 488 if (CommBuffer == NULL) {\r
e42e9404 489 return EFI_INVALID_PARAMETER;\r
490 }\r
491\r
d1632f69
SZ
492 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
493\r
494 if (CommSize == NULL) {\r
495 TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;\r
496 } else {\r
497 TempCommSize = *CommSize;\r
498 //\r
499 // CommSize must hold HeaderGuid and MessageLength\r
500 //\r
501 if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
502 return EFI_INVALID_PARAMETER;\r
503 }\r
ab780ebf
JY
504 }\r
505\r
e42e9404 506 //\r
507 // If not already in SMM, then generate a Software SMI\r
508 //\r
509 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
510 //\r
511 // Put arguments for Software SMI in gSmmCorePrivate\r
512 //\r
513 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
d1632f69 514 gSmmCorePrivate->BufferSize = TempCommSize;\r
e42e9404 515\r
516 //\r
517 // Generate Software SMI\r
518 //\r
519 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
520 if (EFI_ERROR (Status)) {\r
521 return EFI_UNSUPPORTED;\r
522 }\r
523\r
524 //\r
d1102dba 525 // Return status from software SMI\r
e42e9404 526 //\r
d1632f69
SZ
527 if (CommSize != NULL) {\r
528 *CommSize = gSmmCorePrivate->BufferSize;\r
529 }\r
e42e9404 530 return gSmmCorePrivate->ReturnStatus;\r
531 }\r
532\r
533 //\r
534 // If we are in SMM, then the execution mode must be physical, which means that\r
535 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
d1632f69
SZ
536 // has been called, then a direct invocation of the Software SMI is not allowed,\r
537 // so return EFI_INVALID_PARAMETER.\r
e42e9404 538 //\r
539 if (EfiGoneVirtual()) {\r
540 return EFI_INVALID_PARAMETER;\r
541 }\r
542\r
3c5963cf 543 //\r
96756716 544 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.\r
3c5963cf 545 //\r
96756716 546 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r
3c5963cf 547 return EFI_INVALID_PARAMETER;\r
548 }\r
d1102dba 549\r
e42e9404 550 //\r
551 // Save current InSmm state and set InSmm state to TRUE\r
552 //\r
553 OldInSmm = gSmmCorePrivate->InSmm;\r
554 gSmmCorePrivate->InSmm = TRUE;\r
555\r
556 //\r
4b5d69d3 557 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r
e42e9404 558 //\r
d1632f69 559 TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
e42e9404 560 Status = gSmmCorePrivate->Smst->SmiManage (\r
d1102dba
LG
561 &CommunicateHeader->HeaderGuid,\r
562 NULL,\r
563 CommunicateHeader->Data,\r
d1632f69 564 &TempCommSize\r
e42e9404 565 );\r
d1632f69
SZ
566 TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
567 if (CommSize != NULL) {\r
568 *CommSize = TempCommSize;\r
569 }\r
e42e9404 570\r
571 //\r
572 // Restore original InSmm state\r
573 //\r
574 gSmmCorePrivate->InSmm = OldInSmm;\r
575\r
d5b339a9 576 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
e42e9404 577}\r
578\r
579/**\r
5657b268 580 Event notification that is fired when GUIDed Event Group is signaled.\r
e42e9404 581\r
582 @param Event The Event that is being processed, not used.\r
583 @param Context Event Context, not used.\r
584\r
585**/\r
586VOID\r
587EFIAPI\r
588SmmIplGuidedEventNotify (\r
589 IN EFI_EVENT Event,\r
590 IN VOID *Context\r
591 )\r
592{\r
e42e9404 593 UINTN Size;\r
594\r
595 //\r
d1102dba 596 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r
e42e9404 597 //\r
670a563f
JY
598 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
599 mCommunicateHeader.MessageLength = 1;\r
600 mCommunicateHeader.Data[0] = 0;\r
e42e9404 601\r
602 //\r
603 // Generate the Software SMI and return the result\r
604 //\r
670a563f
JY
605 Size = sizeof (mCommunicateHeader);\r
606 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
e42e9404 607}\r
608\r
265fff39
JF
609/**\r
610 Event notification that is fired when EndOfDxe Event Group is signaled.\r
611\r
612 @param Event The Event that is being processed, not used.\r
613 @param Context Event Context, not used.\r
614\r
615**/\r
616VOID\r
617EFIAPI\r
618SmmIplEndOfDxeEventNotify (\r
619 IN EFI_EVENT Event,\r
620 IN VOID *Context\r
621 )\r
622{\r
623 mEndOfDxe = TRUE;\r
624}\r
625\r
5657b268 626/**\r
627 Event notification that is fired when DxeDispatch Event Group is signaled.\r
628\r
629 @param Event The Event that is being processed, not used.\r
630 @param Context Event Context, not used.\r
631\r
632**/\r
633VOID\r
634EFIAPI\r
635SmmIplDxeDispatchEventNotify (\r
636 IN EFI_EVENT Event,\r
637 IN VOID *Context\r
638 )\r
639{\r
5657b268 640 UINTN Size;\r
641 EFI_STATUS Status;\r
642\r
643 //\r
644 // Keep calling the SMM Core Dispatcher until there is no request to restart it.\r
645 //\r
646 while (TRUE) {\r
647 //\r
648 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r
649 // Clear the buffer passed into the Software SMI. This buffer will return\r
650 // the status of the SMM Core Dispatcher.\r
651 //\r
670a563f
JY
652 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
653 mCommunicateHeader.MessageLength = 1;\r
654 mCommunicateHeader.Data[0] = 0;\r
5657b268 655\r
656 //\r
657 // Generate the Software SMI and return the result\r
658 //\r
670a563f
JY
659 Size = sizeof (mCommunicateHeader);\r
660 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
d1102dba 661\r
5657b268 662 //\r
663 // Return if there is no request to restart the SMM Core Dispatcher\r
664 //\r
670a563f 665 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
5657b268 666 return;\r
667 }\r
d1102dba 668\r
5657b268 669 //\r
670 // Close all SMRAM ranges to protect SMRAM\r
bfc87aa7
RN
671 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib\r
672 // so no need to reset the SMRAM to UC in MTRR.\r
5657b268 673 //\r
674 Status = mSmmAccess->Close (mSmmAccess);\r
675 ASSERT_EFI_ERROR (Status);\r
676\r
677 //\r
678 // Print debug message that the SMRAM window is now closed.\r
679 //\r
680 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
5657b268 681 }\r
682}\r
683\r
e42e9404 684/**\r
685 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
686\r
687 @param Event The Event that is being processed, not used.\r
688 @param Context Event Context, not used.\r
689\r
690**/\r
691VOID\r
692EFIAPI\r
693SmmIplSmmConfigurationEventNotify (\r
694 IN EFI_EVENT Event,\r
695 IN VOID *Context\r
696 )\r
697{\r
698 EFI_STATUS Status;\r
699 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
700\r
701 //\r
702 // Make sure this notification is for this handler\r
703 //\r
704 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
705 if (EFI_ERROR (Status)) {\r
706 return;\r
707 }\r
708\r
709 //\r
4be497df 710 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol\r
e42e9404 711 //\r
712 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
713 ASSERT_EFI_ERROR (Status);\r
714\r
715 //\r
d1102dba 716 // Set flag to indicate that the SMM Entry Point has been registered which\r
e42e9404 717 // means that SMIs are now fully operational.\r
718 //\r
719 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
720\r
721 //\r
722 // Print debug message showing SMM Core entry point address.\r
723 //\r
724 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
e42e9404 725}\r
726\r
727/**\r
728 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
01331951 729 or if gEfiEventReadyToBootGuid is signaled.\r
e42e9404 730\r
731 @param Event The Event that is being processed, not used.\r
732 @param Context Event Context, not used.\r
733\r
734**/\r
735VOID\r
736EFIAPI\r
737SmmIplReadyToLockEventNotify (\r
738 IN EFI_EVENT Event,\r
739 IN VOID *Context\r
740 )\r
741{\r
742 EFI_STATUS Status;\r
743 VOID *Interface;\r
744 UINTN Index;\r
745\r
746 //\r
747 // See if we are already locked\r
748 //\r
749 if (mSmmLocked) {\r
750 return;\r
751 }\r
d1102dba 752\r
e42e9404 753 //\r
754 // Make sure this notification is for this handler\r
755 //\r
756 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
757 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
758 if (EFI_ERROR (Status)) {\r
759 return;\r
760 }\r
761 } else {\r
762 //\r
d1102dba 763 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being\r
01331951 764 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
e42e9404 765 // Print a warning on debug builds.\r
766 //\r
767 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
768 }\r
769\r
265fff39
JF
770 if (!mEndOfDxe) {\r
771 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));\r
772 REPORT_STATUS_CODE (\r
773 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
774 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r
775 );\r
776 ASSERT (FALSE);\r
777 }\r
778\r
e42e9404 779 //\r
780 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
781 //\r
782 mSmmAccess->Lock (mSmmAccess);\r
d1102dba 783\r
e42e9404 784 //\r
d1102dba
LG
785 // Close protocol and event notification events that do not apply after the\r
786 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot\r
e42e9404 787 // event has been signalled.\r
788 //\r
789 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
790 if (mSmmIplEvents[Index].CloseOnLock) {\r
791 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
792 }\r
793 }\r
794\r
795 //\r
796 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
797 //\r
798 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
799\r
800 //\r
801 // Print debug message that the SMRAM window is now locked.\r
802 //\r
803 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
d1102dba 804\r
e42e9404 805 //\r
806 // Set flag so this operation will not be performed again\r
807 //\r
808 mSmmLocked = TRUE;\r
809}\r
810\r
811/**\r
812 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
813\r
814 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
815 It convers pointer to new virtual address.\r
816\r
817 @param Event Event whose notification function is being invoked.\r
818 @param Context Pointer to the notification function's context.\r
819\r
820**/\r
821VOID\r
822EFIAPI\r
823SmmIplSetVirtualAddressNotify (\r
824 IN EFI_EVENT Event,\r
825 IN VOID *Context\r
826 )\r
827{\r
828 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
829}\r
830\r
3c447c27 831/**\r
2048c585 832 Get the fixed loading address from image header assigned by build tool. This function only be called\r
3c447c27 833 when Loading module at Fixed address feature enabled.\r
e42e9404 834\r
3c447c27 835 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
836 image that needs to be examined by this function.\r
837 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
2048c585 838 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
3c447c27 839**/\r
840EFI_STATUS\r
841GetPeCoffImageFixLoadingAssignedAddress(\r
842 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
843 )\r
844{\r
845 UINTN SectionHeaderOffset;\r
846 EFI_STATUS Status;\r
847 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
848 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
2048c585 849 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
3c447c27 850 UINT16 Index;\r
851 UINTN Size;\r
852 UINT16 NumberOfSections;\r
853 EFI_PHYSICAL_ADDRESS SmramBase;\r
854 UINT64 SmmCodeSize;\r
855 UINT64 ValueInSectionHeader;\r
856 //\r
857 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
858 //\r
859 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
d1102dba 860\r
2048c585 861 FixLoadingAddress = 0;\r
3c447c27 862 Status = EFI_NOT_FOUND;\r
c2aeb66f 863 SmramBase = mLMFAConfigurationTable->SmramBase;\r
3c447c27 864 //\r
865 // Get PeHeader pointer\r
866 //\r
867 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
16f69227
HW
868 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
869 sizeof (UINT32) +\r
870 sizeof (EFI_IMAGE_FILE_HEADER) +\r
871 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
3c447c27 872 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
873\r
874 //\r
875 // Get base address from the first section header that doesn't point to code section.\r
876 //\r
877 for (Index = 0; Index < NumberOfSections; Index++) {\r
878 //\r
879 // Read section header from file\r
880 //\r
881 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
882 Status = ImageContext->ImageRead (\r
883 ImageContext->Handle,\r
884 SectionHeaderOffset,\r
885 &Size,\r
886 &SectionHeader\r
887 );\r
888 if (EFI_ERROR (Status)) {\r
889 return Status;\r
890 }\r
d1102dba 891\r
3c447c27 892 Status = EFI_NOT_FOUND;\r
d1102dba 893\r
3c447c27 894 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
895 //\r
896 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
897 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
898 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
2048c585 899 // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
3c447c27 900 //\r
901 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
902 if (ValueInSectionHeader != 0) {\r
903 //\r
2048c585 904 // Found first section header that doesn't point to code section in which build tool saves the\r
3c447c27 905 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
906 //\r
2048c585 907 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
3c447c27 908\r
2048c585 909 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {\r
3c447c27 910 //\r
2048c585 911 // The assigned address is valid. Return the specified loading address\r
3c447c27 912 //\r
2048c585 913 ImageContext->ImageAddress = FixLoadingAddress;\r
3c447c27 914 Status = EFI_SUCCESS;\r
915 }\r
916 }\r
917 break;\r
918 }\r
919 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
920 }\r
2048c585 921 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r
3c447c27 922 return Status;\r
923}\r
e42e9404 924/**\r
925 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
926\r
d1102dba 927 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the\r
c03beb76
SZ
928 currently executing image, the rang of SMRAM to\r
929 hold SMM Core will be excluded.\r
930 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.\r
931\r
932 @param[in] Context Context to pass into SMM Core\r
e42e9404 933\r
934 @return EFI_STATUS\r
935\r
936**/\r
937EFI_STATUS\r
938ExecuteSmmCoreFromSmram (\r
c03beb76
SZ
939 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,\r
940 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,\r
941 IN VOID *Context\r
e42e9404 942 )\r
943{\r
944 EFI_STATUS Status;\r
945 VOID *SourceBuffer;\r
946 UINTN SourceSize;\r
947 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
948 UINTN PageCount;\r
e42e9404 949 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
950\r
951 //\r
952 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
d1102dba 953 //\r
d7aaf1dc 954 Status = GetSectionFromAnyFvByFileType (\r
d1102dba 955 EFI_FV_FILETYPE_SMM_CORE,\r
d7aaf1dc 956 0,\r
d1102dba 957 EFI_SECTION_PE32,\r
d7aaf1dc 958 0,\r
d1102dba 959 &SourceBuffer,\r
d7aaf1dc
LG
960 &SourceSize\r
961 );\r
962 if (EFI_ERROR (Status)) {\r
963 return Status;\r
e42e9404 964 }\r
d1102dba 965\r
e42e9404 966 //\r
4be497df 967 // Initialize ImageContext\r
e42e9404 968 //\r
969 ImageContext.Handle = SourceBuffer;\r
970 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
971\r
972 //\r
973 // Get information about the image being loaded\r
974 //\r
975 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
976 if (EFI_ERROR (Status)) {\r
977 return Status;\r
978 }\r
e42e9404 979 //\r
d1102dba 980 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to\r
3c447c27 981 // the address assigned by build tool.\r
e42e9404 982 //\r
3c447c27 983 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
984 //\r
985 // Get the fixed loading address assigned by Build tool\r
986 //\r
987 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
988 if (!EFI_ERROR (Status)) {\r
989 //\r
990 // 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
991 //\r
992 PageCount = 0;\r
a7d10bb0
LG
993 //\r
994 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
995 //\r
996 gSmmCorePrivate->SmramRangeCount --;\r
c03beb76 997 } else {\r
3c447c27 998 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
999 //\r
d1102dba 1000 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r
3c447c27 1001 // specified by SmramRange\r
1002 //\r
e0e7f80c 1003 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 1004\r
3c447c27 1005 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1006 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 1007\r
3c447c27 1008 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
1009 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1010 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1011 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1012 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
e42e9404 1013\r
3c447c27 1014 //\r
c03beb76 1015 // Align buffer on section boundary\r
3c447c27 1016 //\r
c03beb76 1017 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 1018 }\r
1019 } else {\r
1020 //\r
d1102dba 1021 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r
3c447c27 1022 // specified by SmramRange\r
1023 //\r
e0e7f80c 1024 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 1025\r
1026 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1027 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
1028\r
1029 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
1030 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1031 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1032 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1033 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
3c447c27 1034\r
1035 //\r
c03beb76 1036 // Align buffer on section boundary\r
3c447c27 1037 //\r
c03beb76 1038 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 1039 }\r
d1102dba 1040\r
e42e9404 1041 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
16f69227 1042 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
e42e9404 1043\r
1044 //\r
1045 // Print debug message showing SMM Core load address.\r
1046 //\r
1047 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
1048\r
1049 //\r
1050 // Load the image to our new buffer\r
1051 //\r
1052 Status = PeCoffLoaderLoadImage (&ImageContext);\r
1053 if (!EFI_ERROR (Status)) {\r
1054 //\r
1055 // Relocate the image in our new buffer\r
1056 //\r
1057 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
1058 if (!EFI_ERROR (Status)) {\r
1059 //\r
1060 // Flush the instruction cache so the image data are written before we execute it\r
1061 //\r
1062 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
1063\r
1064 //\r
1065 // Print debug message showing SMM Core entry point address.\r
1066 //\r
1067 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
1068\r
84edd20b
SZ
1069 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;\r
1070 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;\r
1071 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));\r
1072 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));\r
1073\r
1074 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;\r
1075\r
e42e9404 1076 //\r
1077 // Execute image\r
1078 //\r
1079 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
1080 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
1081 }\r
1082 }\r
1083\r
1084 //\r
4be497df 1085 // Always free memory allocated by GetFileBufferByFilePath ()\r
e42e9404 1086 //\r
c03beb76
SZ
1087 FreePool (SourceBuffer);\r
1088\r
1089 return Status;\r
1090}\r
1091\r
1092/**\r
1093 SMM split SMRAM entry.\r
1094\r
1095 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1096 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1097 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.\r
1098 @param[in, out] RangeCount Pointer to range count.\r
1099 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.\r
1100 @param[in, out] ReservedRangeCount Pointer to reserved range count.\r
1101 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry\r
1102 that no need to be split anymore.\r
1103 @param[in, out] FinalRangeCount Pointer to final range count.\r
1104\r
1105**/\r
1106VOID\r
1107SmmSplitSmramEntry (\r
1108 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1109 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,\r
1110 OUT EFI_SMRAM_DESCRIPTOR *Ranges,\r
1111 IN OUT UINTN *RangeCount,\r
1112 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,\r
1113 IN OUT UINTN *ReservedRangeCount,\r
1114 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,\r
1115 IN OUT UINTN *FinalRangeCount\r
1116 )\r
1117{\r
1118 UINT64 RangeToCompareEnd;\r
1119 UINT64 ReservedRangeToCompareEnd;\r
1120\r
1121 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1122 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1123\r
1124 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1125 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1126 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {\r
1127 //\r
1128 // RangeToCompare ReservedRangeToCompare\r
1129 // ---- ---- --------------------------------------\r
1130 // | | | | -> 1. ReservedRangeToCompare\r
1131 // ---- | | |--| --------------------------------------\r
1132 // | | | | | |\r
1133 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1134 // | | | | | | RangeToCompare->PhysicalSize = 0\r
1135 // ---- | | |--| --------------------------------------\r
1136 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount\r
1137 // ---- ---- --------------------------------------\r
1138 //\r
1139\r
1140 //\r
1141 // 1. Update ReservedRangeToCompare.\r
1142 //\r
1143 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1144 //\r
1145 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1146 // Zero RangeToCompare->PhysicalSize.\r
1147 //\r
1148 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1149 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1150 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1151 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;\r
1152 *FinalRangeCount += 1;\r
1153 RangeToCompare->PhysicalSize = 0;\r
1154 //\r
1155 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.\r
1156 //\r
1157 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1158 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;\r
1159 *ReservedRangeCount += 1;\r
1160 } else {\r
1161 //\r
1162 // RangeToCompare ReservedRangeToCompare\r
1163 // ---- ---- --------------------------------------\r
1164 // | | | | -> 1. ReservedRangeToCompare\r
1165 // ---- | | |--| --------------------------------------\r
1166 // | | | | | |\r
1167 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1168 // | | | | | |\r
1169 // | | ---- |--| --------------------------------------\r
1170 // | | | | -> 3. RangeToCompare\r
1171 // ---- ---- --------------------------------------\r
1172 //\r
1173\r
1174 //\r
1175 // 1. Update ReservedRangeToCompare.\r
1176 //\r
1177 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1178 //\r
1179 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1180 //\r
1181 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1182 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1183 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1184 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;\r
1185 *FinalRangeCount += 1;\r
1186 //\r
1187 // 3. Update RangeToCompare.\r
1188 //\r
1189 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1190 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1191 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1192 }\r
1193 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1194 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1195 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {\r
1196 //\r
1197 // RangeToCompare ReservedRangeToCompare\r
1198 // ---- ---- --------------------------------------\r
1199 // | | | | -> 1. RangeToCompare\r
1200 // | | ---- |--| --------------------------------------\r
1201 // | | | | | |\r
1202 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1203 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0\r
1204 // | | ---- |--| --------------------------------------\r
1205 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount\r
1206 // ---- ---- --------------------------------------\r
1207 //\r
1208\r
1209 //\r
1210 // 1. Update RangeToCompare.\r
1211 //\r
1212 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1213 //\r
1214 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1215 // ReservedRangeToCompare->SmramReservedSize = 0\r
1216 //\r
1217 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1218 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1219 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1220 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;\r
1221 *FinalRangeCount += 1;\r
1222 ReservedRangeToCompare->SmramReservedSize = 0;\r
1223 //\r
1224 // 3. Update Ranges[*RangeCount] and increment *RangeCount.\r
1225 //\r
1226 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1227 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1228 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;\r
1229 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;\r
1230 *RangeCount += 1;\r
1231 } else {\r
1232 //\r
1233 // RangeToCompare ReservedRangeToCompare\r
1234 // ---- ---- --------------------------------------\r
1235 // | | | | -> 1. RangeToCompare\r
1236 // | | ---- |--| --------------------------------------\r
1237 // | | | | | |\r
1238 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1239 // | | | | | |\r
1240 // ---- | | |--| --------------------------------------\r
1241 // | | | | -> 3. ReservedRangeToCompare\r
1242 // ---- ---- --------------------------------------\r
1243 //\r
1244\r
1245 //\r
1246 // 1. Update RangeToCompare.\r
1247 //\r
1248 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1249 //\r
1250 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1251 // ReservedRangeToCompare->SmramReservedSize = 0\r
1252 //\r
1253 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1254 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1255 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1256 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;\r
1257 *FinalRangeCount += 1;\r
1258 //\r
1259 // 3. Update ReservedRangeToCompare.\r
1260 //\r
1261 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1262 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1263 }\r
1264 }\r
1265}\r
1266\r
1267/**\r
1268 Returns if SMRAM range and SMRAM reserved range are overlapped.\r
1269\r
1270 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1271 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1272\r
1273 @retval TRUE There is overlap.\r
1274 @retval FALSE There is no overlap.\r
1275\r
1276**/\r
1277BOOLEAN\r
1278SmmIsSmramOverlap (\r
1279 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1280 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare\r
1281 )\r
1282{\r
1283 UINT64 RangeToCompareEnd;\r
1284 UINT64 ReservedRangeToCompareEnd;\r
1285\r
1286 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1287 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1288\r
1289 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1290 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1291 return TRUE;\r
1292 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1293 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1294 return TRUE;\r
e42e9404 1295 }\r
c03beb76
SZ
1296 return FALSE;\r
1297}\r
1298\r
1299/**\r
1300 Get full SMRAM ranges.\r
e42e9404 1301\r
c03beb76
SZ
1302 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from\r
1303 SmmConfiguration protocol, split the entries if there is overlap between them.\r
1304 It will also reserve one entry for SMM core.\r
1305\r
1306 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.\r
1307\r
1308 @return Pointer to full SMRAM ranges.\r
1309\r
1310**/\r
1311EFI_SMRAM_DESCRIPTOR *\r
1312GetFullSmramRanges (\r
1313 OUT UINTN *FullSmramRangeCount\r
1314 )\r
1315{\r
1316 EFI_STATUS Status;\r
1317 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1318 UINTN Size;\r
1319 UINTN Index;\r
1320 UINTN Index2;\r
1321 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;\r
1322 UINTN TempSmramRangeCount;\r
a7d10bb0 1323 UINTN AdditionSmramRangeCount;\r
c03beb76
SZ
1324 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;\r
1325 UINTN SmramRangeCount;\r
1326 EFI_SMRAM_DESCRIPTOR *SmramRanges;\r
1327 UINTN SmramReservedCount;\r
1328 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;\r
1329 UINTN MaxCount;\r
1330 BOOLEAN Rescan;\r
1331\r
1332 //\r
1333 // Get SMM Configuration Protocol if it is present.\r
e42e9404 1334 //\r
c03beb76
SZ
1335 SmmConfiguration = NULL;\r
1336 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1337\r
e42e9404 1338 //\r
c03beb76
SZ
1339 // Get SMRAM information.\r
1340 //\r
1341 Size = 0;\r
1342 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1343 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
e42e9404 1344\r
c03beb76
SZ
1345 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1346\r
1347 //\r
1348 // Get SMRAM reserved region count.\r
1349 //\r
1350 SmramReservedCount = 0;\r
1351 if (SmmConfiguration != NULL) {\r
1352 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {\r
1353 SmramReservedCount++;\r
1354 }\r
1355 }\r
1356\r
a7d10bb0
LG
1357 //\r
1358 // Reserve one entry for SMM Core in the full SMRAM ranges.\r
1359 //\r
1360 AdditionSmramRangeCount = 1;\r
1361 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1362 //\r
1363 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
1364 //\r
1365 AdditionSmramRangeCount = 2;\r
1366 }\r
1367\r
c03beb76
SZ
1368 if (SmramReservedCount == 0) {\r
1369 //\r
1370 // No reserved SMRAM entry from SMM Configuration Protocol.\r
c03beb76 1371 //\r
a7d10bb0 1372 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
c03beb76 1373 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
08308fc9 1374 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
c03beb76
SZ
1375 ASSERT (FullSmramRanges != NULL);\r
1376\r
1377 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r
1378 ASSERT_EFI_ERROR (Status);\r
1379\r
1380 return FullSmramRanges;\r
1381 }\r
1382\r
1383 //\r
1384 // Why MaxCount = X + 2 * Y?\r
1385 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.\r
1386 //\r
1387 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1\r
1388 // ---- ----\r
1389 // | | ---- |--|\r
1390 // | | | | -> | |\r
1391 // | | ---- |--|\r
1392 // ---- ----\r
1393 //\r
1394 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1\r
1395 // ---- ----\r
1396 // | | | |\r
1397 // | | ---- |--|\r
1398 // | | | | | |\r
1399 // |--| | | -> |--|\r
1400 // | | | | | |\r
1401 // | | ---- |--|\r
1402 // | | | |\r
1403 // ---- ----\r
1404 //\r
1405 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1\r
1406 // ---- ----\r
1407 // | | | |\r
1408 // | | ---- |--|\r
1409 // |--| | | |--|\r
1410 // | | | | -> | |\r
1411 // |--| | | |--|\r
1412 // | | ---- |--|\r
1413 // | | | |\r
1414 // ---- ----\r
1415 //\r
1416 // ......\r
1417 //\r
1418 MaxCount = SmramRangeCount + 2 * SmramReservedCount;\r
1419\r
1420 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);\r
1421 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);\r
1422 ASSERT (SmramReservedRanges != NULL);\r
1423 for (Index = 0; Index < SmramReservedCount; Index++) {\r
1424 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));\r
1425 }\r
1426\r
1427 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
1428 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1429 ASSERT (TempSmramRanges != NULL);\r
1430 TempSmramRangeCount = 0;\r
1431\r
1432 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1433 ASSERT (SmramRanges != NULL);\r
1434 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);\r
1435 ASSERT_EFI_ERROR (Status);\r
1436\r
1437 do {\r
1438 Rescan = FALSE;\r
1439 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {\r
1440 //\r
1441 // Skip zero size entry.\r
1442 //\r
1443 if (SmramRanges[Index].PhysicalSize != 0) {\r
1444 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {\r
1445 //\r
1446 // Skip zero size entry.\r
1447 //\r
1448 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {\r
1449 if (SmmIsSmramOverlap (\r
1450 &SmramRanges[Index],\r
1451 &SmramReservedRanges[Index2]\r
1452 )) {\r
1453 //\r
1454 // There is overlap, need to split entry and then rescan.\r
1455 //\r
1456 SmmSplitSmramEntry (\r
1457 &SmramRanges[Index],\r
1458 &SmramReservedRanges[Index2],\r
1459 SmramRanges,\r
1460 &SmramRangeCount,\r
1461 SmramReservedRanges,\r
1462 &SmramReservedCount,\r
1463 TempSmramRanges,\r
1464 &TempSmramRangeCount\r
1465 );\r
1466 Rescan = TRUE;\r
1467 }\r
1468 }\r
1469 }\r
1470 if (!Rescan) {\r
1471 //\r
1472 // No any overlap, copy the entry to the temp SMRAM ranges.\r
1473 // Zero SmramRanges[Index].PhysicalSize = 0;\r
1474 //\r
1475 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1476 SmramRanges[Index].PhysicalSize = 0;\r
1477 }\r
1478 }\r
1479 }\r
1480 } while (Rescan);\r
1481 ASSERT (TempSmramRangeCount <= MaxCount);\r
1482\r
1483 //\r
a7d10bb0 1484 // Sort the entries\r
c03beb76 1485 //\r
a7d10bb0 1486 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
c03beb76
SZ
1487 ASSERT (FullSmramRanges != NULL);\r
1488 *FullSmramRangeCount = 0;\r
1489 do {\r
1490 for (Index = 0; Index < TempSmramRangeCount; Index++) {\r
1491 if (TempSmramRanges[Index].PhysicalSize != 0) {\r
1492 break;\r
1493 }\r
1494 }\r
1495 ASSERT (Index < TempSmramRangeCount);\r
1496 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {\r
1497 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {\r
1498 Index = Index2;\r
1499 }\r
1500 }\r
1501 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1502 *FullSmramRangeCount += 1;\r
1503 TempSmramRanges[Index].PhysicalSize = 0;\r
1504 } while (*FullSmramRangeCount < TempSmramRangeCount);\r
1505 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
a7d10bb0 1506 *FullSmramRangeCount += AdditionSmramRangeCount;\r
c03beb76
SZ
1507\r
1508 FreePool (SmramRanges);\r
1509 FreePool (SmramReservedRanges);\r
1510 FreePool (TempSmramRanges);\r
1511\r
1512 return FullSmramRanges;\r
e42e9404 1513}\r
1514\r
1515/**\r
1516 The Entry Point for SMM IPL\r
1517\r
d1102dba
LG
1518 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install\r
1519 SMM Base 2 Protocol and SMM Communication Protocol, and register for the\r
e42e9404 1520 critical events required to coordinate between DXE and SMM environments.\r
d1102dba 1521\r
e42e9404 1522 @param ImageHandle The firmware allocated handle for the EFI image.\r
1523 @param SystemTable A pointer to the EFI System Table.\r
1524\r
1525 @retval EFI_SUCCESS The entry point is executed successfully.\r
1526 @retval Other Some error occurred when executing this entry point.\r
1527\r
1528**/\r
1529EFI_STATUS\r
1530EFIAPI\r
1531SmmIplEntry (\r
1532 IN EFI_HANDLE ImageHandle,\r
1533 IN EFI_SYSTEM_TABLE *SystemTable\r
1534 )\r
1535{\r
1536 EFI_STATUS Status;\r
e42e9404 1537 UINTN Index;\r
e42e9404 1538 UINT64 MaxSize;\r
1539 VOID *Registration;\r
07d9dc83 1540 UINT64 SmmCodeSize;\r
07d9dc83 1541 EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
b07ea4c1 1542 EFI_STATUS SetAttrStatus;\r
a7d10bb0 1543 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;\r
94c0129d 1544 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
e42e9404 1545\r
1546 //\r
d1102dba
LG
1547 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the\r
1548 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded\r
e42e9404 1549 // by the SMM Core\r
1550 //\r
1551 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1552\r
1553 //\r
1554 // Get SMM Access Protocol\r
1555 //\r
1556 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1557 ASSERT_EFI_ERROR (Status);\r
1558\r
1559 //\r
1560 // Get SMM Control2 Protocol\r
1561 //\r
1562 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1563 ASSERT_EFI_ERROR (Status);\r
1564\r
c03beb76 1565 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);\r
84edd20b 1566\r
e42e9404 1567 //\r
1568 // Open all SMRAM ranges\r
1569 //\r
1570 Status = mSmmAccess->Open (mSmmAccess);\r
1571 ASSERT_EFI_ERROR (Status);\r
1572\r
1573 //\r
1574 // Print debug message that the SMRAM window is now open.\r
1575 //\r
1576 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
d1102dba 1577\r
e42e9404 1578 //\r
06b07ce3 1579 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1580 //\r
1581 mCurrentSmramRange = NULL;\r
06b07ce3 1582 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
2c0f06f0 1583 //\r
1584 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
1585 //\r
1586 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
1587 continue;\r
1588 }\r
1589\r
e42e9404 1590 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
10e4e4f6 1591 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
e42e9404 1592 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1593 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1594 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1595 }\r
1596 }\r
1597 }\r
1598 }\r
1599\r
1600 if (mCurrentSmramRange != NULL) {\r
1601 //\r
1602 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1603 //\r
d1102dba
LG
1604 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",\r
1605 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,\r
e42e9404 1606 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1607 ));\r
1608\r
40e8cca5 1609 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1610 //\r
54ec85dd
JW
1611 // Make sure we can change the desired memory attributes.\r
1612 //\r
1613 Status = gDS->GetMemorySpaceDescriptor (\r
1614 mSmramCacheBase,\r
1615 &MemDesc\r
1616 );\r
1617 ASSERT_EFI_ERROR (Status);\r
1618 if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {\r
1619 gDS->SetMemorySpaceCapabilities (\r
1620 mSmramCacheBase,\r
1621 mSmramCacheSize,\r
1622 MemDesc.Capabilities | SMRAM_CAPABILITIES\r
1623 );\r
1624 }\r
1625 //\r
94c0129d 1626 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear\r
54ec85dd 1627 // all paging attributes.\r
07d9dc83 1628 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
1629 // is not available here.\r
e42e9404 1630 //\r
0a6c0905 1631 CpuArch = NULL;\r
07d9dc83 1632 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
1633 if (!EFI_ERROR (Status)) {\r
54ec85dd
JW
1634 MemDesc.Attributes &= ~(MEMORY_CACHE_ATTRIBUTES | MEMORY_PAGE_ATTRIBUTES);\r
1635 MemDesc.Attributes |= EFI_MEMORY_WB;\r
1636 Status = gDS->SetMemorySpaceAttributes (\r
1637 mSmramCacheBase,\r
07d9dc83 1638 mSmramCacheSize,\r
54ec85dd 1639 MemDesc.Attributes\r
07d9dc83 1640 );\r
1641 if (EFI_ERROR (Status)) {\r
1642 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
94c0129d
JW
1643 }\r
1644\r
54ec85dd
JW
1645 DEBUG_CODE (\r
1646 gDS->GetMemorySpaceDescriptor (\r
1647 mSmramCacheBase,\r
1648 &MemDesc\r
94c0129d 1649 );\r
54ec85dd
JW
1650 DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));\r
1651 ASSERT ((MemDesc.Attributes & MEMORY_PAGE_ATTRIBUTES) == 0);\r
1652 );\r
07d9dc83 1653 }\r
3c447c27 1654 //\r
1655 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1656 // Modules At Fixed Address Configuration Table.\r
1657 //\r
1658 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1659 //\r
1660 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1661 //\r
1662 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1663 //\r
1664 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1665 //\r
1666 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1667 //\r
1668 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1669 //\r
1670 Status = EfiGetSystemConfigurationTable (\r
1671 &gLoadFixedAddressConfigurationTableGuid,\r
c2aeb66f 1672 (VOID **) &mLMFAConfigurationTable\r
3c447c27 1673 );\r
c2aeb66f
LG
1674 if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {\r
1675 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1676 //\r
1677 // Print the SMRAM base\r
1678 //\r
c2aeb66f 1679 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));\r
3c447c27 1680 }\r
a7d10bb0
LG
1681\r
1682 //\r
1683 // Fill the Smram range for all SMM code\r
1684 //\r
1685 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
1686 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;\r
1687 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
1688 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
1689 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;\r
1690\r
1691 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;\r
1692 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
1693 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
3c447c27 1694 }\r
e42e9404 1695 //\r
1696 // Load SMM Core into SMRAM and execute it from SMRAM\r
1697 //\r
c03beb76
SZ
1698 Status = ExecuteSmmCoreFromSmram (\r
1699 mCurrentSmramRange,\r
1700 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],\r
1701 gSmmCorePrivate\r
1702 );\r
e42e9404 1703 if (EFI_ERROR (Status)) {\r
1704 //\r
1705 // Print error message that the SMM Core failed to be loaded and executed.\r
1706 //\r
1707 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1708\r
1709 //\r
1710 // Attempt to reset SMRAM cacheability to UC\r
1711 //\r
0a6c0905 1712 if (CpuArch != NULL) {\r
b07ea4c1 1713 SetAttrStatus = gDS->SetMemorySpaceAttributes(\r
d1102dba 1714 mSmramCacheBase,\r
b07ea4c1
LE
1715 mSmramCacheSize,\r
1716 EFI_MEMORY_UC\r
1717 );\r
1718 if (EFI_ERROR (SetAttrStatus)) {\r
0a6c0905 1719 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
d1102dba 1720 }\r
0a6c0905 1721 }\r
e42e9404 1722 }\r
1723 } else {\r
1724 //\r
1725 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1726 //\r
1727 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1728 }\r
1729\r
1730 //\r
d1102dba 1731 // If the SMM Core could not be loaded then close SMRAM window, free allocated\r
e42e9404 1732 // resources, and return an error so SMM IPL will be unloaded.\r
1733 //\r
1734 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1735 //\r
1736 // Close all SMRAM ranges\r
1737 //\r
1738 Status = mSmmAccess->Close (mSmmAccess);\r
1739 ASSERT_EFI_ERROR (Status);\r
1740\r
1741 //\r
1742 // Print debug message that the SMRAM window is now closed.\r
1743 //\r
1744 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1745\r
1746 //\r
1747 // Free all allocated resources\r
1748 //\r
1749 FreePool (gSmmCorePrivate->SmramRanges);\r
63aa86b0 1750\r
e42e9404 1751 return EFI_UNSUPPORTED;\r
1752 }\r
d1102dba 1753\r
e42e9404 1754 //\r
1755 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1756 //\r
1757 Status = gBS->InstallMultipleProtocolInterfaces (\r
1758 &mSmmIplHandle,\r
1759 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1760 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1761 NULL\r
1762 );\r
1763 ASSERT_EFI_ERROR (Status);\r
1764\r
1765 //\r
4be497df 1766 // Create the set of protocol and event notifications that the SMM IPL requires\r
e42e9404 1767 //\r
1768 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1769 if (mSmmIplEvents[Index].Protocol) {\r
1770 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1771 mSmmIplEvents[Index].Guid,\r
5657b268 1772 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1773 mSmmIplEvents[Index].NotifyFunction,\r
1774 mSmmIplEvents[Index].NotifyContext,\r
1775 &Registration\r
1776 );\r
1777 } else {\r
1778 Status = gBS->CreateEventEx (\r
1779 EVT_NOTIFY_SIGNAL,\r
5657b268 1780 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1781 mSmmIplEvents[Index].NotifyFunction,\r
1782 mSmmIplEvents[Index].NotifyContext,\r
1783 mSmmIplEvents[Index].Guid,\r
1784 &mSmmIplEvents[Index].Event\r
1785 );\r
1786 ASSERT_EFI_ERROR (Status);\r
1787 }\r
1788 }\r
1789\r
1790 return EFI_SUCCESS;\r
1791}\r