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/MmCommunication2.h>
14 #include <Protocol/SmmAccess2.h>
15 #include <Protocol/SmmConfiguration.h>
16 #include <Protocol/SmmControl2.h>
17 #include <Protocol/DxeSmmReadyToLock.h>
18 #include <Protocol/Cpu.h>
20 #include <Guid/EventGroup.h>
21 #include <Guid/EventLegacyBios.h>
22 #include <Guid/LoadModuleAtFixedAddress.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/PeCoffLib.h>
27 #include <Library/CacheMaintenanceLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/DxeServicesTableLib.h>
32 #include <Library/DxeServicesLib.h>
33 #include <Library/UefiLib.h>
34 #include <Library/UefiRuntimeLib.h>
35 #include <Library/PcdLib.h>
36 #include <Library/ReportStatusCodeLib.h>
37 #include "PiSmmCorePrivateData.h"
38 #include <Library/SafeIntLib.h>
40 #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)
43 // Function prototypes from produced protocols
47 Indicate whether the driver is currently executing in the SMM Initialization phase.
49 @param This The EFI_SMM_BASE2_PROTOCOL instance.
50 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
51 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
53 @retval EFI_INVALID_PARAMETER InSmram was NULL.
54 @retval EFI_SUCCESS The call returned successfully.
60 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
65 Retrieves the location of the System Management System Table (SMST).
67 @param This The EFI_SMM_BASE2_PROTOCOL instance.
68 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
70 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
71 @retval EFI_SUCCESS The memory was returned to the system.
72 @retval EFI_UNSUPPORTED Not in SMM.
77 SmmBase2GetSmstLocation (
78 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
79 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
83 Communicates with a registered handler.
85 This function provides a service to send and receive messages from a registered
86 UEFI service. This function is part of the SMM Communication Protocol that may
87 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
88 after SetVirtualAddressMap().
90 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
91 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
92 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
93 being returned. Zero if the handler does not wish to reply with any data.
94 This parameter is optional and may be NULL.
96 @retval EFI_SUCCESS The message was successfully posted.
97 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
98 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
99 If this error is returned, the MessageLength field
100 in the CommBuffer header or the integer pointed by
101 CommSize, are updated to reflect the maximum payload
102 size the implementation can accommodate.
103 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
104 if not omitted, are in address range that cannot be
105 accessed by the MM environment.
110 SmmCommunicationCommunicate (
111 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
112 IN OUT VOID
*CommBuffer
,
113 IN OUT UINTN
*CommSize OPTIONAL
117 Communicates with a registered handler.
119 This function provides a service to send and receive messages from a registered UEFI service.
121 @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.
122 @param[in] CommBufferPhysical Physical address of the MM communication buffer
123 @param[in] CommBufferVirtual Virtual address of the MM communication buffer
124 @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data
125 being returned. Zero if the handler does not wish to reply with any data.
126 This parameter is optional and may be NULL.
128 @retval EFI_SUCCESS The message was successfully posted.
129 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
130 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
131 If this error is returned, the MessageLength field
132 in the CommBuffer header or the integer pointed by
133 CommSize, are updated to reflect the maximum payload
134 size the implementation can accommodate.
135 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
136 if not omitted, are in address range that cannot be
137 accessed by the MM environment.
142 SmmCommunicationMmCommunicate2 (
143 IN CONST EFI_MM_COMMUNICATION2_PROTOCOL
*This
,
144 IN OUT VOID
*CommBufferPhysical
,
145 IN OUT VOID
*CommBufferVirtual
,
146 IN OUT UINTN
*CommSize OPTIONAL
150 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
152 @param Event The Event that is being processed, not used.
153 @param Context Event Context, not used.
158 SmmIplSmmConfigurationEventNotify (
164 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
165 or if gEfiEventReadyToBootGuid is signalled.
167 @param Event The Event that is being processed, not used.
168 @param Context Event Context, not used.
173 SmmIplReadyToLockEventNotify (
179 Event notification that is fired when DxeDispatch Event Group is signaled.
181 @param Event The Event that is being processed, not used.
182 @param Context Event Context, not used.
187 SmmIplDxeDispatchEventNotify (
193 Event notification that is fired when a GUIDed Event Group is signaled.
195 @param Event The Event that is being processed, not used.
196 @param Context Event Context, not used.
201 SmmIplGuidedEventNotify (
207 Event notification that is fired when EndOfDxe Event Group is signaled.
209 @param Event The Event that is being processed, not used.
210 @param Context Event Context, not used.
215 SmmIplEndOfDxeEventNotify (
221 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
223 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
224 It convers pointer to new virtual address.
226 @param Event Event whose notification function is being invoked.
227 @param Context Pointer to the notification function's context.
232 SmmIplSetVirtualAddressNotify (
238 // Data structure used to declare a table of protocol notifications and event
239 // notifications required by the SMM IPL
245 EFI_EVENT_NOTIFY NotifyFunction
;
249 } SMM_IPL_EVENT_NOTIFICATION
;
252 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
254 EFI_HANDLE mSmmIplHandle
= NULL
;
257 // SMM Base 2 Protocol instance
259 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
261 SmmBase2GetSmstLocation
265 // SMM Communication Protocol instance
267 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
268 SmmCommunicationCommunicate
272 // PI 1.7 MM Communication Protocol 2 instance
274 EFI_MM_COMMUNICATION2_PROTOCOL mMmCommunication2
= {
275 SmmCommunicationMmCommunicate2
279 // SMM Core Private Data structure that contains the data shared between
280 // the SMM IPL and the SMM Core.
282 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
283 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
284 NULL
, // SmmIplImageHandle
285 0, // SmramRangeCount
287 NULL
, // SmmEntryPoint
288 FALSE
, // SmmEntryPointRegistered
291 NULL
, // CommunicationBuffer
293 EFI_SUCCESS
// ReturnStatus
297 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
299 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
302 // SMM IPL global variables
304 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
305 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
306 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
307 BOOLEAN mSmmLocked
= FALSE
;
308 BOOLEAN mEndOfDxe
= FALSE
;
309 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
310 UINT64 mSmramCacheSize
;
312 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader
;
313 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*mLMFAConfigurationTable
= NULL
;
316 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
318 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
320 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
321 // the associated event is immediately signalled, so the notification function will be executed and the
322 // SMM Configuration Protocol will be found if it is already in the handle database.
324 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, TPL_NOTIFY
, NULL
},
326 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
327 // the associated event is immediately signalled, so the notification function will be executed and the
328 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
330 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, TPL_CALLBACK
, NULL
},
332 // Declare event notification on EndOfDxe event. When this notification is established,
333 // the associated event is immediately signalled, so the notification function will be executed and the
334 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
336 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplGuidedEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
338 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
340 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplEndOfDxeEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
342 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
343 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
344 // if notified, so the SMM Core can dispatch SMM drivers.
346 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplDxeDispatchEventNotify
, &gEfiEventDxeDispatchGuid
, TPL_CALLBACK
, NULL
},
348 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
349 // used to make sure SMRAM is locked before any boot options are processed.
351 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
353 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
354 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
355 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
356 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
358 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, TPL_CALLBACK
, NULL
},
360 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
361 // to notify SMM driver that system enter exit boot services.
363 { FALSE
, FALSE
, &gEfiEventExitBootServicesGuid
, SmmIplGuidedEventNotify
, &gEfiEventExitBootServicesGuid
, TPL_CALLBACK
, NULL
},
365 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
366 // to notify SMM driver that system enter ready to boot.
368 { FALSE
, FALSE
, &gEfiEventReadyToBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
370 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
371 // and mSmmControl2 from physical addresses to virtual addresses.
373 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, TPL_CALLBACK
, NULL
},
375 // Terminate the table of event notifications
377 { FALSE
, FALSE
, NULL
, NULL
, NULL
, TPL_CALLBACK
, NULL
}
381 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
383 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
385 @param SmramRange The SMRAM range to search from.
386 @param SmramCacheBase The returned cache range base.
387 @param SmramCacheSize The returned cache range size.
392 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
393 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
394 OUT UINT64
*SmramCacheSize
398 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
399 UINT64 RangePhysicalSize
;
400 BOOLEAN FoundAjacentRange
;
402 *SmramCacheBase
= SmramRange
->CpuStart
;
403 *SmramCacheSize
= SmramRange
->PhysicalSize
;
406 FoundAjacentRange
= FALSE
;
407 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
408 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
409 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
410 if ((RangeCpuStart
< *SmramCacheBase
) && (*SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
))) {
411 *SmramCacheBase
= RangeCpuStart
;
412 *SmramCacheSize
+= RangePhysicalSize
;
413 FoundAjacentRange
= TRUE
;
414 } else if (((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
) && (RangePhysicalSize
> 0)) {
415 *SmramCacheSize
+= RangePhysicalSize
;
416 FoundAjacentRange
= TRUE
;
419 } while (FoundAjacentRange
);
423 Indicate whether the driver is currently executing in the SMM Initialization phase.
425 @param This The EFI_SMM_BASE2_PROTOCOL instance.
426 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
427 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
429 @retval EFI_INVALID_PARAMETER InSmram was NULL.
430 @retval EFI_SUCCESS The call returned successfully.
436 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
440 if (InSmram
== NULL
) {
441 return EFI_INVALID_PARAMETER
;
444 *InSmram
= gSmmCorePrivate
->InSmm
;
450 Retrieves the location of the System Management System Table (SMST).
452 @param This The EFI_SMM_BASE2_PROTOCOL instance.
453 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
455 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
456 @retval EFI_SUCCESS The memory was returned to the system.
457 @retval EFI_UNSUPPORTED Not in SMM.
462 SmmBase2GetSmstLocation (
463 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
464 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
467 if ((This
== NULL
) || (Smst
== NULL
)) {
468 return EFI_INVALID_PARAMETER
;
471 if (!gSmmCorePrivate
->InSmm
) {
472 return EFI_UNSUPPORTED
;
475 *Smst
= gSmmCorePrivate
->Smst
;
481 Communicates with a registered handler.
483 This function provides a service to send and receive messages from a registered
484 UEFI service. This function is part of the SMM Communication Protocol that may
485 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
486 after SetVirtualAddressMap().
488 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
489 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
490 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
491 being returned. Zero if the handler does not wish to reply with any data.
492 This parameter is optional and may be NULL.
494 @retval EFI_SUCCESS The message was successfully posted.
495 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
496 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
497 If this error is returned, the MessageLength field
498 in the CommBuffer header or the integer pointed by
499 CommSize, are updated to reflect the maximum payload
500 size the implementation can accommodate.
501 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
502 if not omitted, are in address range that cannot be
503 accessed by the MM environment.
508 SmmCommunicationCommunicate (
509 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
510 IN OUT VOID
*CommBuffer
,
511 IN OUT UINTN
*CommSize OPTIONAL
515 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
522 if (CommBuffer
== NULL
) {
523 return EFI_INVALID_PARAMETER
;
526 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
528 if (CommSize
== NULL
) {
529 TempCommSize
= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
) + CommunicateHeader
->MessageLength
;
531 TempCommSize
= *CommSize
;
533 // CommSize must hold HeaderGuid and MessageLength
535 if (TempCommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
536 return EFI_INVALID_PARAMETER
;
541 // If not already in SMM, then generate a Software SMI
543 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
545 // Put arguments for Software SMI in gSmmCorePrivate
547 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
548 gSmmCorePrivate
->BufferSize
= TempCommSize
;
551 // Generate Software SMI
553 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
554 if (EFI_ERROR (Status
)) {
555 return EFI_UNSUPPORTED
;
559 // Return status from software SMI
561 if (CommSize
!= NULL
) {
562 *CommSize
= gSmmCorePrivate
->BufferSize
;
565 return gSmmCorePrivate
->ReturnStatus
;
569 // If we are in SMM, then the execution mode must be physical, which means that
570 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
571 // has been called, then a direct invocation of the Software SMI is not allowed,
572 // so return EFI_INVALID_PARAMETER.
574 if (EfiGoneVirtual ()) {
575 return EFI_INVALID_PARAMETER
;
579 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
581 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
582 return EFI_INVALID_PARAMETER
;
586 // Save current InSmm state and set InSmm state to TRUE
588 OldInSmm
= gSmmCorePrivate
->InSmm
;
589 gSmmCorePrivate
->InSmm
= TRUE
;
592 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
594 TempCommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
595 Status
= gSmmCorePrivate
->Smst
->SmiManage (
596 &CommunicateHeader
->HeaderGuid
,
598 CommunicateHeader
->Data
,
601 TempCommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
602 if (CommSize
!= NULL
) {
603 *CommSize
= TempCommSize
;
607 // Restore original InSmm state
609 gSmmCorePrivate
->InSmm
= OldInSmm
;
611 return (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
615 Communicates with a registered handler.
617 This function provides a service to send and receive messages from a registered UEFI service.
619 @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.
620 @param[in] CommBufferPhysical Physical address of the MM communication buffer
621 @param[in] CommBufferVirtual Virtual address of the MM communication buffer
622 @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data
623 being returned. Zero if the handler does not wish to reply with any data.
624 This parameter is optional and may be NULL.
626 @retval EFI_SUCCESS The message was successfully posted.
627 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
628 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
629 If this error is returned, the MessageLength field
630 in the CommBuffer header or the integer pointed by
631 CommSize, are updated to reflect the maximum payload
632 size the implementation can accommodate.
633 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
634 if not omitted, are in address range that cannot be
635 accessed by the MM environment.
640 SmmCommunicationMmCommunicate2 (
641 IN CONST EFI_MM_COMMUNICATION2_PROTOCOL
*This
,
642 IN OUT VOID
*CommBufferPhysical
,
643 IN OUT VOID
*CommBufferVirtual
,
644 IN OUT UINTN
*CommSize OPTIONAL
647 return SmmCommunicationCommunicate (
655 Event notification that is fired when GUIDed Event Group is signaled.
657 @param Event The Event that is being processed, not used.
658 @param Context Event Context, not used.
663 SmmIplGuidedEventNotify (
671 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
673 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
674 mCommunicateHeader
.MessageLength
= 1;
675 mCommunicateHeader
.Data
[0] = 0;
678 // Generate the Software SMI and return the result
680 Size
= sizeof (mCommunicateHeader
);
681 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
685 Event notification that is fired when EndOfDxe Event Group is signaled.
687 @param Event The Event that is being processed, not used.
688 @param Context Event Context, not used.
693 SmmIplEndOfDxeEventNotify (
702 Event notification that is fired when DxeDispatch Event Group is signaled.
704 @param Event The Event that is being processed, not used.
705 @param Context Event Context, not used.
710 SmmIplDxeDispatchEventNotify (
719 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
723 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
724 // Clear the buffer passed into the Software SMI. This buffer will return
725 // the status of the SMM Core Dispatcher.
727 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
728 mCommunicateHeader
.MessageLength
= 1;
729 mCommunicateHeader
.Data
[0] = 0;
732 // Generate the Software SMI and return the result
734 Size
= sizeof (mCommunicateHeader
);
735 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
738 // Return if there is no request to restart the SMM Core Dispatcher
740 if (mCommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
745 // Close all SMRAM ranges to protect SMRAM
746 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
747 // so no need to reset the SMRAM to UC in MTRR.
749 Status
= mSmmAccess
->Close (mSmmAccess
);
750 ASSERT_EFI_ERROR (Status
);
753 // Print debug message that the SMRAM window is now closed.
755 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
760 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
762 @param Event The Event that is being processed, not used.
763 @param Context Event Context, not used.
768 SmmIplSmmConfigurationEventNotify (
774 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
777 // Make sure this notification is for this handler
779 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
780 if (EFI_ERROR (Status
)) {
785 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
787 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
788 ASSERT_EFI_ERROR (Status
);
791 // Set flag to indicate that the SMM Entry Point has been registered which
792 // means that SMIs are now fully operational.
794 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
797 // Print debug message showing SMM Core entry point address.
799 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
803 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
804 or if gEfiEventReadyToBootGuid is signaled.
806 @param Event The Event that is being processed, not used.
807 @param Context Event Context, not used.
812 SmmIplReadyToLockEventNotify (
822 // See if we are already locked
829 // Make sure this notification is for this handler
831 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
832 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
833 if (EFI_ERROR (Status
)) {
838 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
839 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
840 // Print a warning on debug builds.
842 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
846 DEBUG ((DEBUG_ERROR
, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
848 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
849 (EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
855 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
857 mSmmAccess
->Lock (mSmmAccess
);
860 // Close protocol and event notification events that do not apply after the
861 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
862 // event has been signalled.
864 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
865 if (mSmmIplEvents
[Index
].CloseOnLock
) {
866 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
871 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
873 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
876 // Print debug message that the SMRAM window is now locked.
878 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
881 // Set flag so this operation will not be performed again
887 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
889 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
890 It convers pointer to new virtual address.
892 @param Event Event whose notification function is being invoked.
893 @param Context Pointer to the notification function's context.
898 SmmIplSetVirtualAddressNotify (
903 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
907 Get the fixed loading address from image header assigned by build tool. This function only be called
908 when Loading module at Fixed address feature enabled.
910 @param ImageContext Pointer to the image context structure that describes the PE/COFF
911 image that needs to be examined by this function.
912 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
913 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
916 GetPeCoffImageFixLoadingAssignedAddress (
917 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
920 UINTN SectionHeaderOffset
;
922 EFI_IMAGE_SECTION_HEADER SectionHeader
;
923 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
924 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
927 UINT16 NumberOfSections
;
928 EFI_PHYSICAL_ADDRESS SmramBase
;
930 UINT64 ValueInSectionHeader
;
933 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
935 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber
));
937 FixLoadingAddress
= 0;
938 Status
= EFI_NOT_FOUND
;
939 SmramBase
= mLMFAConfigurationTable
->SmramBase
;
941 // Get PeHeader pointer
943 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
*)ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
944 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
946 sizeof (EFI_IMAGE_FILE_HEADER
) +
947 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
948 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
951 // Get base address from the first section header that doesn't point to code section.
953 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
955 // Read section header from file
957 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
958 Status
= ImageContext
->ImageRead (
959 ImageContext
->Handle
,
964 if (EFI_ERROR (Status
)) {
968 Status
= EFI_NOT_FOUND
;
970 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
972 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
973 // first section header that doesn't point to code section in image header. And there is an assumption that when the
974 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
975 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
977 ValueInSectionHeader
= ReadUnaligned64 ((UINT64
*)&SectionHeader
.PointerToRelocations
);
978 if (ValueInSectionHeader
!= 0) {
980 // Found first section header that doesn't point to code section in which build tool saves the
981 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
983 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
985 if ((SmramBase
+ SmmCodeSize
> FixLoadingAddress
) && (SmramBase
<= FixLoadingAddress
)) {
987 // The assigned address is valid. Return the specified loading address
989 ImageContext
->ImageAddress
= FixLoadingAddress
;
990 Status
= EFI_SUCCESS
;
997 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
1000 DEBUG ((DEBUG_INFO
|DEBUG_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress
, Status
));
1005 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
1007 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
1008 currently executing image, the rang of SMRAM to
1009 hold SMM Core will be excluded.
1010 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
1012 @param[in] Context Context to pass into SMM Core
1018 ExecuteSmmCoreFromSmram (
1019 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRange
,
1020 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmCore
,
1027 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1029 EFI_IMAGE_ENTRY_POINT EntryPoint
;
1032 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
1034 Status
= GetSectionFromAnyFvByFileType (
1035 EFI_FV_FILETYPE_SMM_CORE
,
1042 if (EFI_ERROR (Status
)) {
1047 // Initialize ImageContext
1049 ImageContext
.Handle
= SourceBuffer
;
1050 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
1053 // Get information about the image being loaded
1055 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1056 if (EFI_ERROR (Status
)) {
1061 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
1062 // the address assigned by build tool.
1064 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable
) != 0) {
1066 // Get the fixed loading address assigned by Build tool
1068 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
1069 if (!EFI_ERROR (Status
)) {
1071 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
1075 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
1077 gSmmCorePrivate
->SmramRangeCount
--;
1079 DEBUG ((DEBUG_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
1081 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1082 // specified by SmramRange
1084 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES ((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1086 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1087 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1089 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1090 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1091 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1092 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1093 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1096 // Align buffer on section boundary
1098 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1102 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1103 // specified by SmramRange
1105 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES ((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1107 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1108 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1110 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1111 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1112 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1113 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1114 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1117 // Align buffer on section boundary
1119 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1122 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
1123 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
1126 // Print debug message showing SMM Core load address.
1128 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
1131 // Load the image to our new buffer
1133 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1134 if (!EFI_ERROR (Status
)) {
1136 // Relocate the image in our new buffer
1138 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1139 if (!EFI_ERROR (Status
)) {
1141 // Flush the instruction cache so the image data are written before we execute it
1143 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
1146 // Print debug message showing SMM Core entry point address.
1148 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
1150 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
1151 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
1152 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
1153 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
1155 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1160 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1161 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1166 // Always free memory allocated by GetFileBufferByFilePath ()
1168 FreePool (SourceBuffer
);
1174 SMM split SMRAM entry.
1176 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1177 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1178 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1179 @param[in, out] RangeCount Pointer to range count.
1180 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1181 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1182 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1183 that no need to be split anymore.
1184 @param[in, out] FinalRangeCount Pointer to final range count.
1188 SmmSplitSmramEntry (
1189 IN OUT EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1190 IN OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
,
1191 OUT EFI_SMRAM_DESCRIPTOR
*Ranges
,
1192 IN OUT UINTN
*RangeCount
,
1193 OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRanges
,
1194 IN OUT UINTN
*ReservedRangeCount
,
1195 OUT EFI_SMRAM_DESCRIPTOR
*FinalRanges
,
1196 IN OUT UINTN
*FinalRangeCount
1199 UINT64 RangeToCompareEnd
;
1200 UINT64 ReservedRangeToCompareEnd
;
1202 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1203 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1205 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1206 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
))
1208 if (RangeToCompareEnd
< ReservedRangeToCompareEnd
) {
1210 // RangeToCompare ReservedRangeToCompare
1211 // ---- ---- --------------------------------------
1212 // | | | | -> 1. ReservedRangeToCompare
1213 // ---- | | |--| --------------------------------------
1215 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1216 // | | | | | | RangeToCompare->PhysicalSize = 0
1217 // ---- | | |--| --------------------------------------
1218 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1219 // ---- ---- --------------------------------------
1223 // 1. Update ReservedRangeToCompare.
1225 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1227 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1228 // Zero RangeToCompare->PhysicalSize.
1230 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1231 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1232 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1233 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompare
->PhysicalSize
;
1234 *FinalRangeCount
+= 1;
1235 RangeToCompare
->PhysicalSize
= 0;
1237 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1239 ReservedRanges
[*ReservedRangeCount
].SmramReservedStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1240 ReservedRanges
[*ReservedRangeCount
].SmramReservedSize
= ReservedRangeToCompareEnd
- RangeToCompareEnd
;
1241 *ReservedRangeCount
+= 1;
1244 // RangeToCompare ReservedRangeToCompare
1245 // ---- ---- --------------------------------------
1246 // | | | | -> 1. ReservedRangeToCompare
1247 // ---- | | |--| --------------------------------------
1249 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1251 // | | ---- |--| --------------------------------------
1252 // | | | | -> 3. RangeToCompare
1253 // ---- ---- --------------------------------------
1257 // 1. Update ReservedRangeToCompare.
1259 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1261 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1263 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1264 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1265 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1266 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompareEnd
- RangeToCompare
->CpuStart
;
1267 *FinalRangeCount
+= 1;
1269 // 3. Update RangeToCompare.
1271 RangeToCompare
->CpuStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1272 RangeToCompare
->PhysicalStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1273 RangeToCompare
->PhysicalSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1275 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1276 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
))
1278 if (ReservedRangeToCompareEnd
< RangeToCompareEnd
) {
1280 // RangeToCompare ReservedRangeToCompare
1281 // ---- ---- --------------------------------------
1282 // | | | | -> 1. RangeToCompare
1283 // | | ---- |--| --------------------------------------
1285 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1286 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1287 // | | ---- |--| --------------------------------------
1288 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1289 // ---- ---- --------------------------------------
1293 // 1. Update RangeToCompare.
1295 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1297 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1298 // ReservedRangeToCompare->SmramReservedSize = 0
1300 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1301 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1302 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1303 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompare
->SmramReservedSize
;
1304 *FinalRangeCount
+= 1;
1305 ReservedRangeToCompare
->SmramReservedSize
= 0;
1307 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1309 Ranges
[*RangeCount
].CpuStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1310 Ranges
[*RangeCount
].PhysicalStart
= FinalRanges
[*FinalRangeCount
- 1].PhysicalStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1311 Ranges
[*RangeCount
].RegionState
= RangeToCompare
->RegionState
;
1312 Ranges
[*RangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompareEnd
;
1316 // RangeToCompare ReservedRangeToCompare
1317 // ---- ---- --------------------------------------
1318 // | | | | -> 1. RangeToCompare
1319 // | | ---- |--| --------------------------------------
1321 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1323 // ---- | | |--| --------------------------------------
1324 // | | | | -> 3. ReservedRangeToCompare
1325 // ---- ---- --------------------------------------
1329 // 1. Update RangeToCompare.
1331 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1333 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1334 // ReservedRangeToCompare->SmramReservedSize = 0
1336 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1337 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1338 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1339 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompare
->SmramReservedStart
;
1340 *FinalRangeCount
+= 1;
1342 // 3. Update ReservedRangeToCompare.
1344 ReservedRangeToCompare
->SmramReservedStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1345 ReservedRangeToCompare
->SmramReservedSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1351 Returns if SMRAM range and SMRAM reserved range are overlapped.
1353 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1354 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1356 @retval TRUE There is overlap.
1357 @retval TRUE Math error.
1358 @retval FALSE There is no overlap.
1363 IN EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1364 IN EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
1367 UINT64 RangeToCompareEnd
;
1368 UINT64 ReservedRangeToCompareEnd
;
1369 BOOLEAN IsOverUnderflow1
;
1370 BOOLEAN IsOverUnderflow2
;
1372 // Check for over or underflow.
1373 IsOverUnderflow1
= EFI_ERROR (
1375 (UINT64
)RangeToCompare
->CpuStart
,
1376 RangeToCompare
->PhysicalSize
,
1380 IsOverUnderflow2
= EFI_ERROR (
1382 (UINT64
)ReservedRangeToCompare
->SmramReservedStart
,
1383 ReservedRangeToCompare
->SmramReservedSize
,
1384 &ReservedRangeToCompareEnd
1387 if (IsOverUnderflow1
|| IsOverUnderflow2
) {
1391 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1392 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
))
1395 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1396 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
))
1405 Get full SMRAM ranges.
1407 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1408 SmmConfiguration protocol, split the entries if there is overlap between them.
1409 It will also reserve one entry for SMM core.
1411 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1413 @return Pointer to full SMRAM ranges.
1416 EFI_SMRAM_DESCRIPTOR
*
1417 GetFullSmramRanges (
1418 OUT UINTN
*FullSmramRangeCount
1422 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1426 EFI_SMRAM_DESCRIPTOR
*FullSmramRanges
;
1427 UINTN TempSmramRangeCount
;
1428 UINTN AdditionSmramRangeCount
;
1429 EFI_SMRAM_DESCRIPTOR
*TempSmramRanges
;
1430 UINTN SmramRangeCount
;
1431 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1432 UINTN SmramReservedCount
;
1433 EFI_SMM_RESERVED_SMRAM_REGION
*SmramReservedRanges
;
1438 // Get SMM Configuration Protocol if it is present.
1440 SmmConfiguration
= NULL
;
1441 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**)&SmmConfiguration
);
1444 // Get SMRAM information.
1447 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1448 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1450 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1453 // Get SMRAM reserved region count.
1455 SmramReservedCount
= 0;
1456 if (SmmConfiguration
!= NULL
) {
1457 while (SmmConfiguration
->SmramReservedRegions
[SmramReservedCount
].SmramReservedSize
!= 0) {
1458 SmramReservedCount
++;
1463 // Reserve one entry for SMM Core in the full SMRAM ranges.
1465 AdditionSmramRangeCount
= 1;
1466 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable
) != 0) {
1468 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1470 AdditionSmramRangeCount
= 2;
1473 if (SmramReservedCount
== 0) {
1475 // No reserved SMRAM entry from SMM Configuration Protocol.
1477 *FullSmramRangeCount
= SmramRangeCount
+ AdditionSmramRangeCount
;
1478 Size
= (*FullSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
);
1479 FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocateZeroPool (Size
);
1480 ASSERT (FullSmramRanges
!= NULL
);
1482 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, FullSmramRanges
);
1483 ASSERT_EFI_ERROR (Status
);
1485 return FullSmramRanges
;
1489 // Why MaxCount = X + 2 * Y?
1490 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1492 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1499 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1510 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1523 MaxCount
= SmramRangeCount
+ 2 * SmramReservedCount
;
1525 Size
= MaxCount
* sizeof (EFI_SMM_RESERVED_SMRAM_REGION
);
1526 SmramReservedRanges
= (EFI_SMM_RESERVED_SMRAM_REGION
*)AllocatePool (Size
);
1527 ASSERT (SmramReservedRanges
!= NULL
);
1528 for (Index
= 0; Index
< SmramReservedCount
; Index
++) {
1529 CopyMem (&SmramReservedRanges
[Index
], &SmmConfiguration
->SmramReservedRegions
[Index
], sizeof (EFI_SMM_RESERVED_SMRAM_REGION
));
1532 Size
= MaxCount
* sizeof (EFI_SMRAM_DESCRIPTOR
);
1533 TempSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1534 ASSERT (TempSmramRanges
!= NULL
);
1535 TempSmramRangeCount
= 0;
1537 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1538 ASSERT (SmramRanges
!= NULL
);
1539 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, SmramRanges
);
1540 ASSERT_EFI_ERROR (Status
);
1544 for (Index
= 0; (Index
< SmramRangeCount
) && !Rescan
; Index
++) {
1546 // Skip zero size entry.
1548 if (SmramRanges
[Index
].PhysicalSize
!= 0) {
1549 for (Index2
= 0; (Index2
< SmramReservedCount
) && !Rescan
; Index2
++) {
1551 // Skip zero size entry.
1553 if (SmramReservedRanges
[Index2
].SmramReservedSize
!= 0) {
1554 if (SmmIsSmramOverlap (
1555 &SmramRanges
[Index
],
1556 &SmramReservedRanges
[Index2
]
1560 // There is overlap, need to split entry and then rescan.
1562 SmmSplitSmramEntry (
1563 &SmramRanges
[Index
],
1564 &SmramReservedRanges
[Index2
],
1567 SmramReservedRanges
,
1568 &SmramReservedCount
,
1570 &TempSmramRangeCount
1579 // No any overlap, copy the entry to the temp SMRAM ranges.
1580 // Zero SmramRanges[Index].PhysicalSize = 0;
1582 CopyMem (&TempSmramRanges
[TempSmramRangeCount
++], &SmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1583 SmramRanges
[Index
].PhysicalSize
= 0;
1589 ASSERT (TempSmramRangeCount
<= MaxCount
);
1594 FullSmramRanges
= AllocateZeroPool ((TempSmramRangeCount
+ AdditionSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
));
1595 ASSERT (FullSmramRanges
!= NULL
);
1596 *FullSmramRangeCount
= 0;
1598 for (Index
= 0; Index
< TempSmramRangeCount
; Index
++) {
1599 if (TempSmramRanges
[Index
].PhysicalSize
!= 0) {
1604 ASSERT (Index
< TempSmramRangeCount
);
1605 for (Index2
= 0; Index2
< TempSmramRangeCount
; Index2
++) {
1606 if ((Index2
!= Index
) && (TempSmramRanges
[Index2
].PhysicalSize
!= 0) && (TempSmramRanges
[Index2
].CpuStart
< TempSmramRanges
[Index
].CpuStart
)) {
1611 CopyMem (&FullSmramRanges
[*FullSmramRangeCount
], &TempSmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1612 *FullSmramRangeCount
+= 1;
1613 TempSmramRanges
[Index
].PhysicalSize
= 0;
1614 } while (*FullSmramRangeCount
< TempSmramRangeCount
);
1616 ASSERT (*FullSmramRangeCount
== TempSmramRangeCount
);
1617 *FullSmramRangeCount
+= AdditionSmramRangeCount
;
1619 FreePool (SmramRanges
);
1620 FreePool (SmramReservedRanges
);
1621 FreePool (TempSmramRanges
);
1623 return FullSmramRanges
;
1627 The Entry Point for SMM IPL
1629 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1630 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1631 critical events required to coordinate between DXE and SMM environments.
1633 @param ImageHandle The firmware allocated handle for the EFI image.
1634 @param SystemTable A pointer to the EFI System Table.
1636 @retval EFI_SUCCESS The entry point is executed successfully.
1637 @retval Other Some error occurred when executing this entry point.
1643 IN EFI_HANDLE ImageHandle
,
1644 IN EFI_SYSTEM_TABLE
*SystemTable
1652 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1653 EFI_STATUS SetAttrStatus
;
1654 EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmDriver
;
1655 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
1658 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1659 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1662 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1665 // Get SMM Access Protocol
1667 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1668 ASSERT_EFI_ERROR (Status
);
1671 // Get SMM Control2 Protocol
1673 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1674 ASSERT_EFI_ERROR (Status
);
1676 gSmmCorePrivate
->SmramRanges
= GetFullSmramRanges (&gSmmCorePrivate
->SmramRangeCount
);
1679 // Open all SMRAM ranges
1681 Status
= mSmmAccess
->Open (mSmmAccess
);
1682 ASSERT_EFI_ERROR (Status
);
1685 // Print debug message that the SMRAM window is now open.
1687 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1690 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1692 mCurrentSmramRange
= NULL
;
1693 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1695 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1697 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1701 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1702 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
- 1) <= MAX_ADDRESS
) {
1703 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1704 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1705 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1711 if (mCurrentSmramRange
!= NULL
) {
1713 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1717 "SMM IPL found SMRAM window %p - %p\n",
1718 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1719 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1722 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1724 // Make sure we can change the desired memory attributes.
1726 Status
= gDS
->GetMemorySpaceDescriptor (
1730 ASSERT_EFI_ERROR (Status
);
1731 if ((MemDesc
.Capabilities
& SMRAM_CAPABILITIES
) != SMRAM_CAPABILITIES
) {
1732 gDS
->SetMemorySpaceCapabilities (
1735 MemDesc
.Capabilities
| SMRAM_CAPABILITIES
1740 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1741 // all paging attributes.
1742 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1743 // is not available here.
1746 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1747 if (!EFI_ERROR (Status
)) {
1748 MemDesc
.Attributes
&= ~(EFI_CACHE_ATTRIBUTE_MASK
| EFI_MEMORY_ATTRIBUTE_MASK
);
1749 MemDesc
.Attributes
|= EFI_MEMORY_WB
;
1750 Status
= gDS
->SetMemorySpaceAttributes (
1755 if (EFI_ERROR (Status
)) {
1756 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1760 gDS
->GetMemorySpaceDescriptor (
1764 DEBUG ((DEBUG_INFO
, "SMRAM attributes: %016lx\n", MemDesc
.Attributes
));
1765 ASSERT ((MemDesc
.Attributes
& EFI_MEMORY_ATTRIBUTE_MASK
) == 0);
1770 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1771 // Modules At Fixed Address Configuration Table.
1773 if (PcdGet64 (PcdLoadModuleAtFixAddressEnable
) != 0) {
1775 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1777 SmmCodeSize
= LShiftU64 (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1779 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1781 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1783 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1785 Status
= EfiGetSystemConfigurationTable (
1786 &gLoadFixedAddressConfigurationTableGuid
,
1787 (VOID
**)&mLMFAConfigurationTable
1789 if (!EFI_ERROR (Status
) && (mLMFAConfigurationTable
!= NULL
)) {
1790 mLMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1792 // Print the SMRAM base
1794 DEBUG ((DEBUG_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable
->SmramBase
));
1798 // Fill the Smram range for all SMM code
1800 SmramRangeSmmDriver
= &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 2];
1801 SmramRangeSmmDriver
->CpuStart
= mCurrentSmramRange
->CpuStart
;
1802 SmramRangeSmmDriver
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
;
1803 SmramRangeSmmDriver
->RegionState
= mCurrentSmramRange
->RegionState
| EFI_ALLOCATED
;
1804 SmramRangeSmmDriver
->PhysicalSize
= SmmCodeSize
;
1806 mCurrentSmramRange
->PhysicalSize
-= SmmCodeSize
;
1807 mCurrentSmramRange
->CpuStart
= mCurrentSmramRange
->CpuStart
+ SmmCodeSize
;
1808 mCurrentSmramRange
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
+ SmmCodeSize
;
1812 // Load SMM Core into SMRAM and execute it from SMRAM
1814 Status
= ExecuteSmmCoreFromSmram (
1816 &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 1],
1819 if (EFI_ERROR (Status
)) {
1821 // Print error message that the SMM Core failed to be loaded and executed.
1823 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1826 // Attempt to reset SMRAM cacheability to UC
1828 if (CpuArch
!= NULL
) {
1829 SetAttrStatus
= gDS
->SetMemorySpaceAttributes (
1834 if (EFI_ERROR (SetAttrStatus
)) {
1835 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1841 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1843 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1847 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1848 // resources, and return an error so SMM IPL will be unloaded.
1850 if ((mCurrentSmramRange
== NULL
) || EFI_ERROR (Status
)) {
1852 // Close all SMRAM ranges
1854 Status
= mSmmAccess
->Close (mSmmAccess
);
1855 ASSERT_EFI_ERROR (Status
);
1858 // Print debug message that the SMRAM window is now closed.
1860 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1863 // Free all allocated resources
1865 FreePool (gSmmCorePrivate
->SmramRanges
);
1867 return EFI_UNSUPPORTED
;
1871 // Install SMM Base2 Protocol and SMM Communication Protocol
1873 Status
= gBS
->InstallMultipleProtocolInterfaces (
1875 &gEfiSmmBase2ProtocolGuid
,
1877 &gEfiSmmCommunicationProtocolGuid
,
1879 &gEfiMmCommunication2ProtocolGuid
,
1883 ASSERT_EFI_ERROR (Status
);
1886 // Create the set of protocol and event notifications that the SMM IPL requires
1888 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1889 if (mSmmIplEvents
[Index
].Protocol
) {
1890 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1891 mSmmIplEvents
[Index
].Guid
,
1892 mSmmIplEvents
[Index
].NotifyTpl
,
1893 mSmmIplEvents
[Index
].NotifyFunction
,
1894 mSmmIplEvents
[Index
].NotifyContext
,
1898 Status
= gBS
->CreateEventEx (
1900 mSmmIplEvents
[Index
].NotifyTpl
,
1901 mSmmIplEvents
[Index
].NotifyFunction
,
1902 mSmmIplEvents
[Index
].NotifyContext
,
1903 mSmmIplEvents
[Index
].Guid
,
1904 &mSmmIplEvents
[Index
].Event
1906 ASSERT_EFI_ERROR (Status
);