2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/SmmBase2.h>
12 #include <Protocol/SmmCommunication.h>
13 #include <Protocol/SmmAccess2.h>
14 #include <Protocol/SmmConfiguration.h>
15 #include <Protocol/SmmControl2.h>
16 #include <Protocol/DxeSmmReadyToLock.h>
17 #include <Protocol/Cpu.h>
19 #include <Guid/EventGroup.h>
20 #include <Guid/EventLegacyBios.h>
21 #include <Guid/LoadModuleAtFixedAddress.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/PeCoffLib.h>
26 #include <Library/CacheMaintenanceLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/DxeServicesTableLib.h>
31 #include <Library/DxeServicesLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/UefiRuntimeLib.h>
34 #include <Library/PcdLib.h>
35 #include <Library/ReportStatusCodeLib.h>
37 #include "PiSmmCorePrivateData.h"
39 #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)
41 #define MEMORY_CACHE_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \
42 EFI_MEMORY_WT | EFI_MEMORY_WB | \
43 EFI_MEMORY_WP | EFI_MEMORY_UCE)
45 #define MEMORY_PAGE_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | EFI_MEMORY_RO)
48 // Function prototypes from produced protocols
52 Indicate whether the driver is currently executing in the SMM Initialization phase.
54 @param This The EFI_SMM_BASE2_PROTOCOL instance.
55 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
56 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
58 @retval EFI_INVALID_PARAMETER InSmram was NULL.
59 @retval EFI_SUCCESS The call returned successfully.
65 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
70 Retrieves the location of the System Management System Table (SMST).
72 @param This The EFI_SMM_BASE2_PROTOCOL instance.
73 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
75 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
76 @retval EFI_SUCCESS The memory was returned to the system.
77 @retval EFI_UNSUPPORTED Not in SMM.
82 SmmBase2GetSmstLocation (
83 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
84 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
88 Communicates with a registered handler.
90 This function provides a service to send and receive messages from a registered
91 UEFI service. This function is part of the SMM Communication Protocol that may
92 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
93 after SetVirtualAddressMap().
95 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
96 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
97 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
98 being returned. Zero if the handler does not wish to reply with any data.
99 This parameter is optional and may be NULL.
101 @retval EFI_SUCCESS The message was successfully posted.
102 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
103 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
104 If this error is returned, the MessageLength field
105 in the CommBuffer header or the integer pointed by
106 CommSize, are updated to reflect the maximum payload
107 size the implementation can accommodate.
108 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
109 if not omitted, are in address range that cannot be
110 accessed by the MM environment.
115 SmmCommunicationCommunicate (
116 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
117 IN OUT VOID
*CommBuffer
,
118 IN OUT UINTN
*CommSize OPTIONAL
122 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
124 @param Event The Event that is being processed, not used.
125 @param Context Event Context, not used.
130 SmmIplSmmConfigurationEventNotify (
136 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
137 or if gEfiEventReadyToBootGuid is signalled.
139 @param Event The Event that is being processed, not used.
140 @param Context Event Context, not used.
145 SmmIplReadyToLockEventNotify (
151 Event notification that is fired when DxeDispatch Event Group is signaled.
153 @param Event The Event that is being processed, not used.
154 @param Context Event Context, not used.
159 SmmIplDxeDispatchEventNotify (
165 Event notification that is fired when a GUIDed Event Group is signaled.
167 @param Event The Event that is being processed, not used.
168 @param Context Event Context, not used.
173 SmmIplGuidedEventNotify (
179 Event notification that is fired when EndOfDxe Event Group is signaled.
181 @param Event The Event that is being processed, not used.
182 @param Context Event Context, not used.
187 SmmIplEndOfDxeEventNotify (
193 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
195 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
196 It convers pointer to new virtual address.
198 @param Event Event whose notification function is being invoked.
199 @param Context Pointer to the notification function's context.
204 SmmIplSetVirtualAddressNotify (
210 // Data structure used to declare a table of protocol notifications and event
211 // notifications required by the SMM IPL
217 EFI_EVENT_NOTIFY NotifyFunction
;
221 } SMM_IPL_EVENT_NOTIFICATION
;
224 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
226 EFI_HANDLE mSmmIplHandle
= NULL
;
229 // SMM Base 2 Protocol instance
231 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
233 SmmBase2GetSmstLocation
237 // SMM Communication Protocol instance
239 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
240 SmmCommunicationCommunicate
244 // SMM Core Private Data structure that contains the data shared between
245 // the SMM IPL and the SMM Core.
247 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
248 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
249 NULL
, // SmmIplImageHandle
250 0, // SmramRangeCount
252 NULL
, // SmmEntryPoint
253 FALSE
, // SmmEntryPointRegistered
256 NULL
, // CommunicationBuffer
258 EFI_SUCCESS
// ReturnStatus
262 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
264 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
267 // SMM IPL global variables
269 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
270 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
271 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
272 BOOLEAN mSmmLocked
= FALSE
;
273 BOOLEAN mEndOfDxe
= FALSE
;
274 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
275 UINT64 mSmramCacheSize
;
277 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader
;
278 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*mLMFAConfigurationTable
= NULL
;
281 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
283 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
285 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
286 // the associated event is immediately signalled, so the notification function will be executed and the
287 // SMM Configuration Protocol will be found if it is already in the handle database.
289 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, TPL_NOTIFY
, NULL
},
291 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
292 // the associated event is immediately signalled, so the notification function will be executed and the
293 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
295 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, TPL_CALLBACK
, NULL
},
297 // Declare event notification on EndOfDxe event. When this notification is established,
298 // the associated event is immediately signalled, so the notification function will be executed and the
299 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
301 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplGuidedEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
303 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
305 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplEndOfDxeEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
307 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
308 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
309 // if notified, so the SMM Core can dispatch SMM drivers.
311 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplDxeDispatchEventNotify
, &gEfiEventDxeDispatchGuid
, TPL_CALLBACK
, NULL
},
313 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
314 // used to make sure SMRAM is locked before any boot options are processed.
316 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
318 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
319 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
320 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
321 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
323 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, TPL_CALLBACK
, NULL
},
325 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
326 // to notify SMM driver that system enter exit boot services.
328 { FALSE
, FALSE
, &gEfiEventExitBootServicesGuid
, SmmIplGuidedEventNotify
, &gEfiEventExitBootServicesGuid
, TPL_CALLBACK
, NULL
},
330 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
331 // to notify SMM driver that system enter ready to boot.
333 { FALSE
, FALSE
, &gEfiEventReadyToBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
335 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
336 // and mSmmControl2 from physical addresses to virtual addresses.
338 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, TPL_CALLBACK
, NULL
},
340 // Terminate the table of event notifications
342 { FALSE
, FALSE
, NULL
, NULL
, NULL
, TPL_CALLBACK
, NULL
}
346 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
348 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
350 @param SmramRange The SMRAM range to search from.
351 @param SmramCacheBase The returned cache range base.
352 @param SmramCacheSize The returned cache range size.
357 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
358 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
359 OUT UINT64
*SmramCacheSize
363 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
364 UINT64 RangePhysicalSize
;
365 BOOLEAN FoundAjacentRange
;
367 *SmramCacheBase
= SmramRange
->CpuStart
;
368 *SmramCacheSize
= SmramRange
->PhysicalSize
;
371 FoundAjacentRange
= FALSE
;
372 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
373 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
374 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
375 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
376 *SmramCacheBase
= RangeCpuStart
;
377 *SmramCacheSize
+= RangePhysicalSize
;
378 FoundAjacentRange
= TRUE
;
379 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
380 *SmramCacheSize
+= RangePhysicalSize
;
381 FoundAjacentRange
= TRUE
;
384 } while (FoundAjacentRange
);
389 Indicate whether the driver is currently executing in the SMM Initialization phase.
391 @param This The EFI_SMM_BASE2_PROTOCOL instance.
392 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
393 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
395 @retval EFI_INVALID_PARAMETER InSmram was NULL.
396 @retval EFI_SUCCESS The call returned successfully.
402 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
406 if (InSmram
== NULL
) {
407 return EFI_INVALID_PARAMETER
;
410 *InSmram
= gSmmCorePrivate
->InSmm
;
416 Retrieves the location of the System Management System Table (SMST).
418 @param This The EFI_SMM_BASE2_PROTOCOL instance.
419 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
421 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
422 @retval EFI_SUCCESS The memory was returned to the system.
423 @retval EFI_UNSUPPORTED Not in SMM.
428 SmmBase2GetSmstLocation (
429 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
430 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
433 if ((This
== NULL
) ||(Smst
== NULL
)) {
434 return EFI_INVALID_PARAMETER
;
437 if (!gSmmCorePrivate
->InSmm
) {
438 return EFI_UNSUPPORTED
;
441 *Smst
= gSmmCorePrivate
->Smst
;
447 Communicates with a registered handler.
449 This function provides a service to send and receive messages from a registered
450 UEFI service. This function is part of the SMM Communication Protocol that may
451 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
452 after SetVirtualAddressMap().
454 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
455 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
456 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
457 being returned. Zero if the handler does not wish to reply with any data.
458 This parameter is optional and may be NULL.
460 @retval EFI_SUCCESS The message was successfully posted.
461 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
462 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
463 If this error is returned, the MessageLength field
464 in the CommBuffer header or the integer pointed by
465 CommSize, are updated to reflect the maximum payload
466 size the implementation can accommodate.
467 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
468 if not omitted, are in address range that cannot be
469 accessed by the MM environment.
474 SmmCommunicationCommunicate (
475 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
476 IN OUT VOID
*CommBuffer
,
477 IN OUT UINTN
*CommSize OPTIONAL
481 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
488 if (CommBuffer
== NULL
) {
489 return EFI_INVALID_PARAMETER
;
492 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
494 if (CommSize
== NULL
) {
495 TempCommSize
= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
) + CommunicateHeader
->MessageLength
;
497 TempCommSize
= *CommSize
;
499 // CommSize must hold HeaderGuid and MessageLength
501 if (TempCommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
502 return EFI_INVALID_PARAMETER
;
507 // If not already in SMM, then generate a Software SMI
509 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
511 // Put arguments for Software SMI in gSmmCorePrivate
513 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
514 gSmmCorePrivate
->BufferSize
= TempCommSize
;
517 // Generate Software SMI
519 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
520 if (EFI_ERROR (Status
)) {
521 return EFI_UNSUPPORTED
;
525 // Return status from software SMI
527 if (CommSize
!= NULL
) {
528 *CommSize
= gSmmCorePrivate
->BufferSize
;
530 return gSmmCorePrivate
->ReturnStatus
;
534 // If we are in SMM, then the execution mode must be physical, which means that
535 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
536 // has been called, then a direct invocation of the Software SMI is not allowed,
537 // so return EFI_INVALID_PARAMETER.
539 if (EfiGoneVirtual()) {
540 return EFI_INVALID_PARAMETER
;
544 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
546 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
547 return EFI_INVALID_PARAMETER
;
551 // Save current InSmm state and set InSmm state to TRUE
553 OldInSmm
= gSmmCorePrivate
->InSmm
;
554 gSmmCorePrivate
->InSmm
= TRUE
;
557 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
559 TempCommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
560 Status
= gSmmCorePrivate
->Smst
->SmiManage (
561 &CommunicateHeader
->HeaderGuid
,
563 CommunicateHeader
->Data
,
566 TempCommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
567 if (CommSize
!= NULL
) {
568 *CommSize
= TempCommSize
;
572 // Restore original InSmm state
574 gSmmCorePrivate
->InSmm
= OldInSmm
;
576 return (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
580 Event notification that is fired when GUIDed Event Group is signaled.
582 @param Event The Event that is being processed, not used.
583 @param Context Event Context, not used.
588 SmmIplGuidedEventNotify (
596 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
598 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
599 mCommunicateHeader
.MessageLength
= 1;
600 mCommunicateHeader
.Data
[0] = 0;
603 // Generate the Software SMI and return the result
605 Size
= sizeof (mCommunicateHeader
);
606 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
610 Event notification that is fired when EndOfDxe Event Group is signaled.
612 @param Event The Event that is being processed, not used.
613 @param Context Event Context, not used.
618 SmmIplEndOfDxeEventNotify (
627 Event notification that is fired when DxeDispatch Event Group is signaled.
629 @param Event The Event that is being processed, not used.
630 @param Context Event Context, not used.
635 SmmIplDxeDispatchEventNotify (
644 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
648 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
649 // Clear the buffer passed into the Software SMI. This buffer will return
650 // the status of the SMM Core Dispatcher.
652 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
653 mCommunicateHeader
.MessageLength
= 1;
654 mCommunicateHeader
.Data
[0] = 0;
657 // Generate the Software SMI and return the result
659 Size
= sizeof (mCommunicateHeader
);
660 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
663 // Return if there is no request to restart the SMM Core Dispatcher
665 if (mCommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
670 // Close all SMRAM ranges to protect SMRAM
671 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
672 // so no need to reset the SMRAM to UC in MTRR.
674 Status
= mSmmAccess
->Close (mSmmAccess
);
675 ASSERT_EFI_ERROR (Status
);
678 // Print debug message that the SMRAM window is now closed.
680 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
685 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
687 @param Event The Event that is being processed, not used.
688 @param Context Event Context, not used.
693 SmmIplSmmConfigurationEventNotify (
699 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
702 // Make sure this notification is for this handler
704 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
705 if (EFI_ERROR (Status
)) {
710 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
712 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
713 ASSERT_EFI_ERROR (Status
);
716 // Set flag to indicate that the SMM Entry Point has been registered which
717 // means that SMIs are now fully operational.
719 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
722 // Print debug message showing SMM Core entry point address.
724 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
728 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
729 or if gEfiEventReadyToBootGuid is signaled.
731 @param Event The Event that is being processed, not used.
732 @param Context Event Context, not used.
737 SmmIplReadyToLockEventNotify (
747 // See if we are already locked
754 // Make sure this notification is for this handler
756 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
757 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
758 if (EFI_ERROR (Status
)) {
763 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
764 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
765 // Print a warning on debug builds.
767 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
771 DEBUG ((DEBUG_ERROR
, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
773 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
774 (EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
780 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
782 mSmmAccess
->Lock (mSmmAccess
);
785 // Close protocol and event notification events that do not apply after the
786 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
787 // event has been signalled.
789 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
790 if (mSmmIplEvents
[Index
].CloseOnLock
) {
791 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
796 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
798 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
801 // Print debug message that the SMRAM window is now locked.
803 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
806 // Set flag so this operation will not be performed again
812 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
814 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
815 It convers pointer to new virtual address.
817 @param Event Event whose notification function is being invoked.
818 @param Context Pointer to the notification function's context.
823 SmmIplSetVirtualAddressNotify (
828 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
832 Get the fixed loading address from image header assigned by build tool. This function only be called
833 when Loading module at Fixed address feature enabled.
835 @param ImageContext Pointer to the image context structure that describes the PE/COFF
836 image that needs to be examined by this function.
837 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
838 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
841 GetPeCoffImageFixLoadingAssignedAddress(
842 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
845 UINTN SectionHeaderOffset
;
847 EFI_IMAGE_SECTION_HEADER SectionHeader
;
848 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
849 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
852 UINT16 NumberOfSections
;
853 EFI_PHYSICAL_ADDRESS SmramBase
;
855 UINT64 ValueInSectionHeader
;
857 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
859 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
861 FixLoadingAddress
= 0;
862 Status
= EFI_NOT_FOUND
;
863 SmramBase
= mLMFAConfigurationTable
->SmramBase
;
865 // Get PeHeader pointer
867 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
868 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
870 sizeof (EFI_IMAGE_FILE_HEADER
) +
871 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
872 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
875 // Get base address from the first section header that doesn't point to code section.
877 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
879 // Read section header from file
881 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
882 Status
= ImageContext
->ImageRead (
883 ImageContext
->Handle
,
888 if (EFI_ERROR (Status
)) {
892 Status
= EFI_NOT_FOUND
;
894 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
896 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
897 // first section header that doesn't point to code section in image header. And there is an assumption that when the
898 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
899 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
901 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
902 if (ValueInSectionHeader
!= 0) {
904 // Found first section header that doesn't point to code section in which build tool saves the
905 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
907 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
909 if (SmramBase
+ SmmCodeSize
> FixLoadingAddress
&& SmramBase
<= FixLoadingAddress
) {
911 // The assigned address is valid. Return the specified loading address
913 ImageContext
->ImageAddress
= FixLoadingAddress
;
914 Status
= EFI_SUCCESS
;
919 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
921 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress
, Status
));
925 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
927 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
928 currently executing image, the rang of SMRAM to
929 hold SMM Core will be excluded.
930 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
932 @param[in] Context Context to pass into SMM Core
938 ExecuteSmmCoreFromSmram (
939 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRange
,
940 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmCore
,
947 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
949 EFI_IMAGE_ENTRY_POINT EntryPoint
;
952 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
954 Status
= GetSectionFromAnyFvByFileType (
955 EFI_FV_FILETYPE_SMM_CORE
,
962 if (EFI_ERROR (Status
)) {
967 // Initialize ImageContext
969 ImageContext
.Handle
= SourceBuffer
;
970 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
973 // Get information about the image being loaded
975 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
976 if (EFI_ERROR (Status
)) {
980 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
981 // the address assigned by build tool.
983 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
985 // Get the fixed loading address assigned by Build tool
987 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
988 if (!EFI_ERROR (Status
)) {
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
994 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
996 gSmmCorePrivate
->SmramRangeCount
--;
998 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
1000 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1001 // specified by SmramRange
1003 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1005 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1006 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1008 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1009 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1010 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1011 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1012 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1015 // Align buffer on section boundary
1017 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1021 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1022 // specified by SmramRange
1024 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1026 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1027 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1029 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1030 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1031 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1032 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1033 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1036 // Align buffer on section boundary
1038 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1041 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
1042 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
1045 // Print debug message showing SMM Core load address.
1047 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
1050 // Load the image to our new buffer
1052 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1053 if (!EFI_ERROR (Status
)) {
1055 // Relocate the image in our new buffer
1057 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1058 if (!EFI_ERROR (Status
)) {
1060 // Flush the instruction cache so the image data are written before we execute it
1062 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
1065 // Print debug message showing SMM Core entry point address.
1067 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
1069 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
1070 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
1071 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
1072 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
1074 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1079 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1080 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1085 // Always free memory allocated by GetFileBufferByFilePath ()
1087 FreePool (SourceBuffer
);
1093 SMM split SMRAM entry.
1095 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1096 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1097 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1098 @param[in, out] RangeCount Pointer to range count.
1099 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1100 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1101 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1102 that no need to be split anymore.
1103 @param[in, out] FinalRangeCount Pointer to final range count.
1107 SmmSplitSmramEntry (
1108 IN OUT EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1109 IN OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
,
1110 OUT EFI_SMRAM_DESCRIPTOR
*Ranges
,
1111 IN OUT UINTN
*RangeCount
,
1112 OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRanges
,
1113 IN OUT UINTN
*ReservedRangeCount
,
1114 OUT EFI_SMRAM_DESCRIPTOR
*FinalRanges
,
1115 IN OUT UINTN
*FinalRangeCount
1118 UINT64 RangeToCompareEnd
;
1119 UINT64 ReservedRangeToCompareEnd
;
1121 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1122 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1124 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1125 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1126 if (RangeToCompareEnd
< ReservedRangeToCompareEnd
) {
1128 // RangeToCompare ReservedRangeToCompare
1129 // ---- ---- --------------------------------------
1130 // | | | | -> 1. ReservedRangeToCompare
1131 // ---- | | |--| --------------------------------------
1133 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1134 // | | | | | | RangeToCompare->PhysicalSize = 0
1135 // ---- | | |--| --------------------------------------
1136 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1137 // ---- ---- --------------------------------------
1141 // 1. Update ReservedRangeToCompare.
1143 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1145 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1146 // Zero RangeToCompare->PhysicalSize.
1148 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1149 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1150 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1151 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompare
->PhysicalSize
;
1152 *FinalRangeCount
+= 1;
1153 RangeToCompare
->PhysicalSize
= 0;
1155 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1157 ReservedRanges
[*ReservedRangeCount
].SmramReservedStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1158 ReservedRanges
[*ReservedRangeCount
].SmramReservedSize
= ReservedRangeToCompareEnd
- RangeToCompareEnd
;
1159 *ReservedRangeCount
+= 1;
1162 // RangeToCompare ReservedRangeToCompare
1163 // ---- ---- --------------------------------------
1164 // | | | | -> 1. ReservedRangeToCompare
1165 // ---- | | |--| --------------------------------------
1167 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1169 // | | ---- |--| --------------------------------------
1170 // | | | | -> 3. RangeToCompare
1171 // ---- ---- --------------------------------------
1175 // 1. Update ReservedRangeToCompare.
1177 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1179 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1181 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1182 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1183 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1184 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompareEnd
- RangeToCompare
->CpuStart
;
1185 *FinalRangeCount
+= 1;
1187 // 3. Update RangeToCompare.
1189 RangeToCompare
->CpuStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1190 RangeToCompare
->PhysicalStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1191 RangeToCompare
->PhysicalSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1193 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1194 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1195 if (ReservedRangeToCompareEnd
< RangeToCompareEnd
) {
1197 // RangeToCompare ReservedRangeToCompare
1198 // ---- ---- --------------------------------------
1199 // | | | | -> 1. RangeToCompare
1200 // | | ---- |--| --------------------------------------
1202 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1203 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1204 // | | ---- |--| --------------------------------------
1205 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1206 // ---- ---- --------------------------------------
1210 // 1. Update RangeToCompare.
1212 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1214 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1215 // ReservedRangeToCompare->SmramReservedSize = 0
1217 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1218 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1219 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1220 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompare
->SmramReservedSize
;
1221 *FinalRangeCount
+= 1;
1222 ReservedRangeToCompare
->SmramReservedSize
= 0;
1224 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1226 Ranges
[*RangeCount
].CpuStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1227 Ranges
[*RangeCount
].PhysicalStart
= FinalRanges
[*FinalRangeCount
- 1].PhysicalStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1228 Ranges
[*RangeCount
].RegionState
= RangeToCompare
->RegionState
;
1229 Ranges
[*RangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompareEnd
;
1233 // RangeToCompare ReservedRangeToCompare
1234 // ---- ---- --------------------------------------
1235 // | | | | -> 1. RangeToCompare
1236 // | | ---- |--| --------------------------------------
1238 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1240 // ---- | | |--| --------------------------------------
1241 // | | | | -> 3. ReservedRangeToCompare
1242 // ---- ---- --------------------------------------
1246 // 1. Update RangeToCompare.
1248 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1250 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1251 // ReservedRangeToCompare->SmramReservedSize = 0
1253 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1254 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1255 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1256 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompare
->SmramReservedStart
;
1257 *FinalRangeCount
+= 1;
1259 // 3. Update ReservedRangeToCompare.
1261 ReservedRangeToCompare
->SmramReservedStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1262 ReservedRangeToCompare
->SmramReservedSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1268 Returns if SMRAM range and SMRAM reserved range are overlapped.
1270 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1271 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1273 @retval TRUE There is overlap.
1274 @retval FALSE There is no overlap.
1279 IN EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1280 IN EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
1283 UINT64 RangeToCompareEnd
;
1284 UINT64 ReservedRangeToCompareEnd
;
1286 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1287 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1289 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1290 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1292 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1293 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1300 Get full SMRAM ranges.
1302 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1303 SmmConfiguration protocol, split the entries if there is overlap between them.
1304 It will also reserve one entry for SMM core.
1306 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1308 @return Pointer to full SMRAM ranges.
1311 EFI_SMRAM_DESCRIPTOR
*
1312 GetFullSmramRanges (
1313 OUT UINTN
*FullSmramRangeCount
1317 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1321 EFI_SMRAM_DESCRIPTOR
*FullSmramRanges
;
1322 UINTN TempSmramRangeCount
;
1323 UINTN AdditionSmramRangeCount
;
1324 EFI_SMRAM_DESCRIPTOR
*TempSmramRanges
;
1325 UINTN SmramRangeCount
;
1326 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1327 UINTN SmramReservedCount
;
1328 EFI_SMM_RESERVED_SMRAM_REGION
*SmramReservedRanges
;
1333 // Get SMM Configuration Protocol if it is present.
1335 SmmConfiguration
= NULL
;
1336 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1339 // Get SMRAM information.
1342 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1343 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1345 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1348 // Get SMRAM reserved region count.
1350 SmramReservedCount
= 0;
1351 if (SmmConfiguration
!= NULL
) {
1352 while (SmmConfiguration
->SmramReservedRegions
[SmramReservedCount
].SmramReservedSize
!= 0) {
1353 SmramReservedCount
++;
1358 // Reserve one entry for SMM Core in the full SMRAM ranges.
1360 AdditionSmramRangeCount
= 1;
1361 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1363 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1365 AdditionSmramRangeCount
= 2;
1368 if (SmramReservedCount
== 0) {
1370 // No reserved SMRAM entry from SMM Configuration Protocol.
1372 *FullSmramRangeCount
= SmramRangeCount
+ AdditionSmramRangeCount
;
1373 Size
= (*FullSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
);
1374 FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocateZeroPool (Size
);
1375 ASSERT (FullSmramRanges
!= NULL
);
1377 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, FullSmramRanges
);
1378 ASSERT_EFI_ERROR (Status
);
1380 return FullSmramRanges
;
1384 // Why MaxCount = X + 2 * Y?
1385 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1387 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1394 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1405 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1418 MaxCount
= SmramRangeCount
+ 2 * SmramReservedCount
;
1420 Size
= MaxCount
* sizeof (EFI_SMM_RESERVED_SMRAM_REGION
);
1421 SmramReservedRanges
= (EFI_SMM_RESERVED_SMRAM_REGION
*) AllocatePool (Size
);
1422 ASSERT (SmramReservedRanges
!= NULL
);
1423 for (Index
= 0; Index
< SmramReservedCount
; Index
++) {
1424 CopyMem (&SmramReservedRanges
[Index
], &SmmConfiguration
->SmramReservedRegions
[Index
], sizeof (EFI_SMM_RESERVED_SMRAM_REGION
));
1427 Size
= MaxCount
* sizeof (EFI_SMRAM_DESCRIPTOR
);
1428 TempSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1429 ASSERT (TempSmramRanges
!= NULL
);
1430 TempSmramRangeCount
= 0;
1432 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1433 ASSERT (SmramRanges
!= NULL
);
1434 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, SmramRanges
);
1435 ASSERT_EFI_ERROR (Status
);
1439 for (Index
= 0; (Index
< SmramRangeCount
) && !Rescan
; Index
++) {
1441 // Skip zero size entry.
1443 if (SmramRanges
[Index
].PhysicalSize
!= 0) {
1444 for (Index2
= 0; (Index2
< SmramReservedCount
) && !Rescan
; Index2
++) {
1446 // Skip zero size entry.
1448 if (SmramReservedRanges
[Index2
].SmramReservedSize
!= 0) {
1449 if (SmmIsSmramOverlap (
1450 &SmramRanges
[Index
],
1451 &SmramReservedRanges
[Index2
]
1454 // There is overlap, need to split entry and then rescan.
1456 SmmSplitSmramEntry (
1457 &SmramRanges
[Index
],
1458 &SmramReservedRanges
[Index2
],
1461 SmramReservedRanges
,
1462 &SmramReservedCount
,
1464 &TempSmramRangeCount
1472 // No any overlap, copy the entry to the temp SMRAM ranges.
1473 // Zero SmramRanges[Index].PhysicalSize = 0;
1475 CopyMem (&TempSmramRanges
[TempSmramRangeCount
++], &SmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1476 SmramRanges
[Index
].PhysicalSize
= 0;
1481 ASSERT (TempSmramRangeCount
<= MaxCount
);
1486 FullSmramRanges
= AllocateZeroPool ((TempSmramRangeCount
+ AdditionSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
));
1487 ASSERT (FullSmramRanges
!= NULL
);
1488 *FullSmramRangeCount
= 0;
1490 for (Index
= 0; Index
< TempSmramRangeCount
; Index
++) {
1491 if (TempSmramRanges
[Index
].PhysicalSize
!= 0) {
1495 ASSERT (Index
< TempSmramRangeCount
);
1496 for (Index2
= 0; Index2
< TempSmramRangeCount
; Index2
++) {
1497 if ((Index2
!= Index
) && (TempSmramRanges
[Index2
].PhysicalSize
!= 0) && (TempSmramRanges
[Index2
].CpuStart
< TempSmramRanges
[Index
].CpuStart
)) {
1501 CopyMem (&FullSmramRanges
[*FullSmramRangeCount
], &TempSmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1502 *FullSmramRangeCount
+= 1;
1503 TempSmramRanges
[Index
].PhysicalSize
= 0;
1504 } while (*FullSmramRangeCount
< TempSmramRangeCount
);
1505 ASSERT (*FullSmramRangeCount
== TempSmramRangeCount
);
1506 *FullSmramRangeCount
+= AdditionSmramRangeCount
;
1508 FreePool (SmramRanges
);
1509 FreePool (SmramReservedRanges
);
1510 FreePool (TempSmramRanges
);
1512 return FullSmramRanges
;
1516 The Entry Point for SMM IPL
1518 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1519 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1520 critical events required to coordinate between DXE and SMM environments.
1522 @param ImageHandle The firmware allocated handle for the EFI image.
1523 @param SystemTable A pointer to the EFI System Table.
1525 @retval EFI_SUCCESS The entry point is executed successfully.
1526 @retval Other Some error occurred when executing this entry point.
1532 IN EFI_HANDLE ImageHandle
,
1533 IN EFI_SYSTEM_TABLE
*SystemTable
1541 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1542 EFI_STATUS SetAttrStatus
;
1543 EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmDriver
;
1544 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
1547 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1548 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1551 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1554 // Get SMM Access Protocol
1556 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1557 ASSERT_EFI_ERROR (Status
);
1560 // Get SMM Control2 Protocol
1562 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1563 ASSERT_EFI_ERROR (Status
);
1565 gSmmCorePrivate
->SmramRanges
= GetFullSmramRanges (&gSmmCorePrivate
->SmramRangeCount
);
1568 // Open all SMRAM ranges
1570 Status
= mSmmAccess
->Open (mSmmAccess
);
1571 ASSERT_EFI_ERROR (Status
);
1574 // Print debug message that the SMRAM window is now open.
1576 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1579 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1581 mCurrentSmramRange
= NULL
;
1582 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1584 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1586 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1590 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1591 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
- 1) <= MAX_ADDRESS
) {
1592 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1593 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1594 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1600 if (mCurrentSmramRange
!= NULL
) {
1602 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1604 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1605 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1606 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1609 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1611 // Make sure we can change the desired memory attributes.
1613 Status
= gDS
->GetMemorySpaceDescriptor (
1617 ASSERT_EFI_ERROR (Status
);
1618 if ((MemDesc
.Capabilities
& SMRAM_CAPABILITIES
) != SMRAM_CAPABILITIES
) {
1619 gDS
->SetMemorySpaceCapabilities (
1622 MemDesc
.Capabilities
| SMRAM_CAPABILITIES
1626 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1627 // all paging attributes.
1628 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1629 // is not available here.
1632 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1633 if (!EFI_ERROR (Status
)) {
1634 MemDesc
.Attributes
&= ~(MEMORY_CACHE_ATTRIBUTES
| MEMORY_PAGE_ATTRIBUTES
);
1635 MemDesc
.Attributes
|= EFI_MEMORY_WB
;
1636 Status
= gDS
->SetMemorySpaceAttributes (
1641 if (EFI_ERROR (Status
)) {
1642 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1646 gDS
->GetMemorySpaceDescriptor (
1650 DEBUG ((DEBUG_INFO
, "SMRAM attributes: %016lx\n", MemDesc
.Attributes
));
1651 ASSERT ((MemDesc
.Attributes
& MEMORY_PAGE_ATTRIBUTES
) == 0);
1655 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1656 // Modules At Fixed Address Configuration Table.
1658 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1660 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1662 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1664 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1666 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1668 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1670 Status
= EfiGetSystemConfigurationTable (
1671 &gLoadFixedAddressConfigurationTableGuid
,
1672 (VOID
**) &mLMFAConfigurationTable
1674 if (!EFI_ERROR (Status
) && mLMFAConfigurationTable
!= NULL
) {
1675 mLMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1677 // Print the SMRAM base
1679 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable
->SmramBase
));
1683 // Fill the Smram range for all SMM code
1685 SmramRangeSmmDriver
= &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 2];
1686 SmramRangeSmmDriver
->CpuStart
= mCurrentSmramRange
->CpuStart
;
1687 SmramRangeSmmDriver
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
;
1688 SmramRangeSmmDriver
->RegionState
= mCurrentSmramRange
->RegionState
| EFI_ALLOCATED
;
1689 SmramRangeSmmDriver
->PhysicalSize
= SmmCodeSize
;
1691 mCurrentSmramRange
->PhysicalSize
-= SmmCodeSize
;
1692 mCurrentSmramRange
->CpuStart
= mCurrentSmramRange
->CpuStart
+ SmmCodeSize
;
1693 mCurrentSmramRange
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
+ SmmCodeSize
;
1696 // Load SMM Core into SMRAM and execute it from SMRAM
1698 Status
= ExecuteSmmCoreFromSmram (
1700 &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 1],
1703 if (EFI_ERROR (Status
)) {
1705 // Print error message that the SMM Core failed to be loaded and executed.
1707 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1710 // Attempt to reset SMRAM cacheability to UC
1712 if (CpuArch
!= NULL
) {
1713 SetAttrStatus
= gDS
->SetMemorySpaceAttributes(
1718 if (EFI_ERROR (SetAttrStatus
)) {
1719 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1725 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1727 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1731 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1732 // resources, and return an error so SMM IPL will be unloaded.
1734 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1736 // Close all SMRAM ranges
1738 Status
= mSmmAccess
->Close (mSmmAccess
);
1739 ASSERT_EFI_ERROR (Status
);
1742 // Print debug message that the SMRAM window is now closed.
1744 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1747 // Free all allocated resources
1749 FreePool (gSmmCorePrivate
->SmramRanges
);
1751 return EFI_UNSUPPORTED
;
1755 // Install SMM Base2 Protocol and SMM Communication Protocol
1757 Status
= gBS
->InstallMultipleProtocolInterfaces (
1759 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1760 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1763 ASSERT_EFI_ERROR (Status
);
1766 // Create the set of protocol and event notifications that the SMM IPL requires
1768 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1769 if (mSmmIplEvents
[Index
].Protocol
) {
1770 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1771 mSmmIplEvents
[Index
].Guid
,
1772 mSmmIplEvents
[Index
].NotifyTpl
,
1773 mSmmIplEvents
[Index
].NotifyFunction
,
1774 mSmmIplEvents
[Index
].NotifyContext
,
1778 Status
= gBS
->CreateEventEx (
1780 mSmmIplEvents
[Index
].NotifyTpl
,
1781 mSmmIplEvents
[Index
].NotifyFunction
,
1782 mSmmIplEvents
[Index
].NotifyContext
,
1783 mSmmIplEvents
[Index
].Guid
,
1784 &mSmmIplEvents
[Index
].Event
1786 ASSERT_EFI_ERROR (Status
);