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>
38 #include "PiSmmCorePrivateData.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
);
424 Indicate whether the driver is currently executing in the SMM Initialization phase.
426 @param This The EFI_SMM_BASE2_PROTOCOL instance.
427 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
428 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
430 @retval EFI_INVALID_PARAMETER InSmram was NULL.
431 @retval EFI_SUCCESS The call returned successfully.
437 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
441 if (InSmram
== NULL
) {
442 return EFI_INVALID_PARAMETER
;
445 *InSmram
= gSmmCorePrivate
->InSmm
;
451 Retrieves the location of the System Management System Table (SMST).
453 @param This The EFI_SMM_BASE2_PROTOCOL instance.
454 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
456 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
457 @retval EFI_SUCCESS The memory was returned to the system.
458 @retval EFI_UNSUPPORTED Not in SMM.
463 SmmBase2GetSmstLocation (
464 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
465 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
468 if ((This
== NULL
) ||(Smst
== NULL
)) {
469 return EFI_INVALID_PARAMETER
;
472 if (!gSmmCorePrivate
->InSmm
) {
473 return EFI_UNSUPPORTED
;
476 *Smst
= gSmmCorePrivate
->Smst
;
482 Communicates with a registered handler.
484 This function provides a service to send and receive messages from a registered
485 UEFI service. This function is part of the SMM Communication Protocol that may
486 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
487 after SetVirtualAddressMap().
489 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
490 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
491 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
492 being returned. Zero if the handler does not wish to reply with any data.
493 This parameter is optional and may be NULL.
495 @retval EFI_SUCCESS The message was successfully posted.
496 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
497 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
498 If this error is returned, the MessageLength field
499 in the CommBuffer header or the integer pointed by
500 CommSize, are updated to reflect the maximum payload
501 size the implementation can accommodate.
502 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
503 if not omitted, are in address range that cannot be
504 accessed by the MM environment.
509 SmmCommunicationCommunicate (
510 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
511 IN OUT VOID
*CommBuffer
,
512 IN OUT UINTN
*CommSize OPTIONAL
516 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
523 if (CommBuffer
== NULL
) {
524 return EFI_INVALID_PARAMETER
;
527 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
529 if (CommSize
== NULL
) {
530 TempCommSize
= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
) + CommunicateHeader
->MessageLength
;
532 TempCommSize
= *CommSize
;
534 // CommSize must hold HeaderGuid and MessageLength
536 if (TempCommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
537 return EFI_INVALID_PARAMETER
;
542 // If not already in SMM, then generate a Software SMI
544 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
546 // Put arguments for Software SMI in gSmmCorePrivate
548 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
549 gSmmCorePrivate
->BufferSize
= TempCommSize
;
552 // Generate Software SMI
554 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
555 if (EFI_ERROR (Status
)) {
556 return EFI_UNSUPPORTED
;
560 // Return status from software SMI
562 if (CommSize
!= NULL
) {
563 *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 (&mSmmCommunication
,
653 Event notification that is fired when GUIDed Event Group is signaled.
655 @param Event The Event that is being processed, not used.
656 @param Context Event Context, not used.
661 SmmIplGuidedEventNotify (
669 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
671 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
672 mCommunicateHeader
.MessageLength
= 1;
673 mCommunicateHeader
.Data
[0] = 0;
676 // Generate the Software SMI and return the result
678 Size
= sizeof (mCommunicateHeader
);
679 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
683 Event notification that is fired when EndOfDxe Event Group is signaled.
685 @param Event The Event that is being processed, not used.
686 @param Context Event Context, not used.
691 SmmIplEndOfDxeEventNotify (
700 Event notification that is fired when DxeDispatch Event Group is signaled.
702 @param Event The Event that is being processed, not used.
703 @param Context Event Context, not used.
708 SmmIplDxeDispatchEventNotify (
717 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
721 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
722 // Clear the buffer passed into the Software SMI. This buffer will return
723 // the status of the SMM Core Dispatcher.
725 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
726 mCommunicateHeader
.MessageLength
= 1;
727 mCommunicateHeader
.Data
[0] = 0;
730 // Generate the Software SMI and return the result
732 Size
= sizeof (mCommunicateHeader
);
733 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
736 // Return if there is no request to restart the SMM Core Dispatcher
738 if (mCommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
743 // Close all SMRAM ranges to protect SMRAM
744 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
745 // so no need to reset the SMRAM to UC in MTRR.
747 Status
= mSmmAccess
->Close (mSmmAccess
);
748 ASSERT_EFI_ERROR (Status
);
751 // Print debug message that the SMRAM window is now closed.
753 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
758 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
760 @param Event The Event that is being processed, not used.
761 @param Context Event Context, not used.
766 SmmIplSmmConfigurationEventNotify (
772 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
775 // Make sure this notification is for this handler
777 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
778 if (EFI_ERROR (Status
)) {
783 // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
785 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
786 ASSERT_EFI_ERROR (Status
);
789 // Set flag to indicate that the SMM Entry Point has been registered which
790 // means that SMIs are now fully operational.
792 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
795 // Print debug message showing SMM Core entry point address.
797 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
801 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
802 or if gEfiEventReadyToBootGuid is signaled.
804 @param Event The Event that is being processed, not used.
805 @param Context Event Context, not used.
810 SmmIplReadyToLockEventNotify (
820 // See if we are already locked
827 // Make sure this notification is for this handler
829 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
830 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
831 if (EFI_ERROR (Status
)) {
836 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
837 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
838 // Print a warning on debug builds.
840 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
844 DEBUG ((DEBUG_ERROR
, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
846 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
847 (EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
853 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
855 mSmmAccess
->Lock (mSmmAccess
);
858 // Close protocol and event notification events that do not apply after the
859 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
860 // event has been signalled.
862 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
863 if (mSmmIplEvents
[Index
].CloseOnLock
) {
864 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
869 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
871 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
874 // Print debug message that the SMRAM window is now locked.
876 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
879 // Set flag so this operation will not be performed again
885 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
887 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
888 It convers pointer to new virtual address.
890 @param Event Event whose notification function is being invoked.
891 @param Context Pointer to the notification function's context.
896 SmmIplSetVirtualAddressNotify (
901 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
905 Get the fixed loading address from image header assigned by build tool. This function only be called
906 when Loading module at Fixed address feature enabled.
908 @param ImageContext Pointer to the image context structure that describes the PE/COFF
909 image that needs to be examined by this function.
910 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
911 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
914 GetPeCoffImageFixLoadingAssignedAddress(
915 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
918 UINTN SectionHeaderOffset
;
920 EFI_IMAGE_SECTION_HEADER SectionHeader
;
921 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
922 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
925 UINT16 NumberOfSections
;
926 EFI_PHYSICAL_ADDRESS SmramBase
;
928 UINT64 ValueInSectionHeader
;
930 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
932 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
934 FixLoadingAddress
= 0;
935 Status
= EFI_NOT_FOUND
;
936 SmramBase
= mLMFAConfigurationTable
->SmramBase
;
938 // Get PeHeader pointer
940 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
941 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
943 sizeof (EFI_IMAGE_FILE_HEADER
) +
944 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
945 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
948 // Get base address from the first section header that doesn't point to code section.
950 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
952 // Read section header from file
954 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
955 Status
= ImageContext
->ImageRead (
956 ImageContext
->Handle
,
961 if (EFI_ERROR (Status
)) {
965 Status
= EFI_NOT_FOUND
;
967 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
969 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
970 // first section header that doesn't point to code section in image header. And there is an assumption that when the
971 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
972 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
974 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
975 if (ValueInSectionHeader
!= 0) {
977 // Found first section header that doesn't point to code section in which build tool saves the
978 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
980 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
982 if (SmramBase
+ SmmCodeSize
> FixLoadingAddress
&& SmramBase
<= FixLoadingAddress
) {
984 // The assigned address is valid. Return the specified loading address
986 ImageContext
->ImageAddress
= FixLoadingAddress
;
987 Status
= EFI_SUCCESS
;
992 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
994 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress
, Status
));
998 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
1000 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
1001 currently executing image, the rang of SMRAM to
1002 hold SMM Core will be excluded.
1003 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
1005 @param[in] Context Context to pass into SMM Core
1011 ExecuteSmmCoreFromSmram (
1012 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRange
,
1013 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmCore
,
1020 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1022 EFI_IMAGE_ENTRY_POINT EntryPoint
;
1025 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
1027 Status
= GetSectionFromAnyFvByFileType (
1028 EFI_FV_FILETYPE_SMM_CORE
,
1035 if (EFI_ERROR (Status
)) {
1040 // Initialize ImageContext
1042 ImageContext
.Handle
= SourceBuffer
;
1043 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
1046 // Get information about the image being loaded
1048 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1049 if (EFI_ERROR (Status
)) {
1053 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
1054 // the address assigned by build tool.
1056 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1058 // Get the fixed loading address assigned by Build tool
1060 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
1061 if (!EFI_ERROR (Status
)) {
1063 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
1067 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
1069 gSmmCorePrivate
->SmramRangeCount
--;
1071 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
1073 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1074 // specified by SmramRange
1076 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1078 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1079 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1081 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1082 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1083 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1084 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1085 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1088 // Align buffer on section boundary
1090 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1094 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1095 // specified by SmramRange
1097 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1099 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1100 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1102 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1103 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1104 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1105 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1106 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1109 // Align buffer on section boundary
1111 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1114 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
1115 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
1118 // Print debug message showing SMM Core load address.
1120 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
1123 // Load the image to our new buffer
1125 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1126 if (!EFI_ERROR (Status
)) {
1128 // Relocate the image in our new buffer
1130 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1131 if (!EFI_ERROR (Status
)) {
1133 // Flush the instruction cache so the image data are written before we execute it
1135 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
1138 // Print debug message showing SMM Core entry point address.
1140 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
1142 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
1143 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
1144 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
1145 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
1147 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1152 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1153 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1158 // Always free memory allocated by GetFileBufferByFilePath ()
1160 FreePool (SourceBuffer
);
1166 SMM split SMRAM entry.
1168 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1169 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1170 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1171 @param[in, out] RangeCount Pointer to range count.
1172 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1173 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1174 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1175 that no need to be split anymore.
1176 @param[in, out] FinalRangeCount Pointer to final range count.
1180 SmmSplitSmramEntry (
1181 IN OUT EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1182 IN OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
,
1183 OUT EFI_SMRAM_DESCRIPTOR
*Ranges
,
1184 IN OUT UINTN
*RangeCount
,
1185 OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRanges
,
1186 IN OUT UINTN
*ReservedRangeCount
,
1187 OUT EFI_SMRAM_DESCRIPTOR
*FinalRanges
,
1188 IN OUT UINTN
*FinalRangeCount
1191 UINT64 RangeToCompareEnd
;
1192 UINT64 ReservedRangeToCompareEnd
;
1194 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1195 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1197 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1198 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1199 if (RangeToCompareEnd
< ReservedRangeToCompareEnd
) {
1201 // RangeToCompare ReservedRangeToCompare
1202 // ---- ---- --------------------------------------
1203 // | | | | -> 1. ReservedRangeToCompare
1204 // ---- | | |--| --------------------------------------
1206 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1207 // | | | | | | RangeToCompare->PhysicalSize = 0
1208 // ---- | | |--| --------------------------------------
1209 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1210 // ---- ---- --------------------------------------
1214 // 1. Update ReservedRangeToCompare.
1216 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1218 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1219 // Zero RangeToCompare->PhysicalSize.
1221 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1222 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1223 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1224 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompare
->PhysicalSize
;
1225 *FinalRangeCount
+= 1;
1226 RangeToCompare
->PhysicalSize
= 0;
1228 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1230 ReservedRanges
[*ReservedRangeCount
].SmramReservedStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1231 ReservedRanges
[*ReservedRangeCount
].SmramReservedSize
= ReservedRangeToCompareEnd
- RangeToCompareEnd
;
1232 *ReservedRangeCount
+= 1;
1235 // RangeToCompare ReservedRangeToCompare
1236 // ---- ---- --------------------------------------
1237 // | | | | -> 1. ReservedRangeToCompare
1238 // ---- | | |--| --------------------------------------
1240 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1242 // | | ---- |--| --------------------------------------
1243 // | | | | -> 3. RangeToCompare
1244 // ---- ---- --------------------------------------
1248 // 1. Update ReservedRangeToCompare.
1250 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1252 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1254 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1255 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1256 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1257 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompareEnd
- RangeToCompare
->CpuStart
;
1258 *FinalRangeCount
+= 1;
1260 // 3. Update RangeToCompare.
1262 RangeToCompare
->CpuStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1263 RangeToCompare
->PhysicalStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1264 RangeToCompare
->PhysicalSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1266 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1267 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1268 if (ReservedRangeToCompareEnd
< RangeToCompareEnd
) {
1270 // RangeToCompare ReservedRangeToCompare
1271 // ---- ---- --------------------------------------
1272 // | | | | -> 1. RangeToCompare
1273 // | | ---- |--| --------------------------------------
1275 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1276 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1277 // | | ---- |--| --------------------------------------
1278 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1279 // ---- ---- --------------------------------------
1283 // 1. Update RangeToCompare.
1285 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1287 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1288 // ReservedRangeToCompare->SmramReservedSize = 0
1290 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1291 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1292 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1293 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompare
->SmramReservedSize
;
1294 *FinalRangeCount
+= 1;
1295 ReservedRangeToCompare
->SmramReservedSize
= 0;
1297 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1299 Ranges
[*RangeCount
].CpuStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1300 Ranges
[*RangeCount
].PhysicalStart
= FinalRanges
[*FinalRangeCount
- 1].PhysicalStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1301 Ranges
[*RangeCount
].RegionState
= RangeToCompare
->RegionState
;
1302 Ranges
[*RangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompareEnd
;
1306 // RangeToCompare ReservedRangeToCompare
1307 // ---- ---- --------------------------------------
1308 // | | | | -> 1. RangeToCompare
1309 // | | ---- |--| --------------------------------------
1311 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1313 // ---- | | |--| --------------------------------------
1314 // | | | | -> 3. ReservedRangeToCompare
1315 // ---- ---- --------------------------------------
1319 // 1. Update RangeToCompare.
1321 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1323 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1324 // ReservedRangeToCompare->SmramReservedSize = 0
1326 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1327 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1328 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1329 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompare
->SmramReservedStart
;
1330 *FinalRangeCount
+= 1;
1332 // 3. Update ReservedRangeToCompare.
1334 ReservedRangeToCompare
->SmramReservedStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1335 ReservedRangeToCompare
->SmramReservedSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1341 Returns if SMRAM range and SMRAM reserved range are overlapped.
1343 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1344 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1346 @retval TRUE There is overlap.
1347 @retval FALSE There is no overlap.
1352 IN EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1353 IN EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
1356 UINT64 RangeToCompareEnd
;
1357 UINT64 ReservedRangeToCompareEnd
;
1359 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1360 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1362 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1363 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1365 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1366 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1373 Get full SMRAM ranges.
1375 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1376 SmmConfiguration protocol, split the entries if there is overlap between them.
1377 It will also reserve one entry for SMM core.
1379 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1381 @return Pointer to full SMRAM ranges.
1384 EFI_SMRAM_DESCRIPTOR
*
1385 GetFullSmramRanges (
1386 OUT UINTN
*FullSmramRangeCount
1390 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1394 EFI_SMRAM_DESCRIPTOR
*FullSmramRanges
;
1395 UINTN TempSmramRangeCount
;
1396 UINTN AdditionSmramRangeCount
;
1397 EFI_SMRAM_DESCRIPTOR
*TempSmramRanges
;
1398 UINTN SmramRangeCount
;
1399 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1400 UINTN SmramReservedCount
;
1401 EFI_SMM_RESERVED_SMRAM_REGION
*SmramReservedRanges
;
1406 // Get SMM Configuration Protocol if it is present.
1408 SmmConfiguration
= NULL
;
1409 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1412 // Get SMRAM information.
1415 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1416 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1418 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1421 // Get SMRAM reserved region count.
1423 SmramReservedCount
= 0;
1424 if (SmmConfiguration
!= NULL
) {
1425 while (SmmConfiguration
->SmramReservedRegions
[SmramReservedCount
].SmramReservedSize
!= 0) {
1426 SmramReservedCount
++;
1431 // Reserve one entry for SMM Core in the full SMRAM ranges.
1433 AdditionSmramRangeCount
= 1;
1434 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1436 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1438 AdditionSmramRangeCount
= 2;
1441 if (SmramReservedCount
== 0) {
1443 // No reserved SMRAM entry from SMM Configuration Protocol.
1445 *FullSmramRangeCount
= SmramRangeCount
+ AdditionSmramRangeCount
;
1446 Size
= (*FullSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
);
1447 FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocateZeroPool (Size
);
1448 ASSERT (FullSmramRanges
!= NULL
);
1450 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, FullSmramRanges
);
1451 ASSERT_EFI_ERROR (Status
);
1453 return FullSmramRanges
;
1457 // Why MaxCount = X + 2 * Y?
1458 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1460 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1467 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1478 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1491 MaxCount
= SmramRangeCount
+ 2 * SmramReservedCount
;
1493 Size
= MaxCount
* sizeof (EFI_SMM_RESERVED_SMRAM_REGION
);
1494 SmramReservedRanges
= (EFI_SMM_RESERVED_SMRAM_REGION
*) AllocatePool (Size
);
1495 ASSERT (SmramReservedRanges
!= NULL
);
1496 for (Index
= 0; Index
< SmramReservedCount
; Index
++) {
1497 CopyMem (&SmramReservedRanges
[Index
], &SmmConfiguration
->SmramReservedRegions
[Index
], sizeof (EFI_SMM_RESERVED_SMRAM_REGION
));
1500 Size
= MaxCount
* sizeof (EFI_SMRAM_DESCRIPTOR
);
1501 TempSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1502 ASSERT (TempSmramRanges
!= NULL
);
1503 TempSmramRangeCount
= 0;
1505 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1506 ASSERT (SmramRanges
!= NULL
);
1507 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, SmramRanges
);
1508 ASSERT_EFI_ERROR (Status
);
1512 for (Index
= 0; (Index
< SmramRangeCount
) && !Rescan
; Index
++) {
1514 // Skip zero size entry.
1516 if (SmramRanges
[Index
].PhysicalSize
!= 0) {
1517 for (Index2
= 0; (Index2
< SmramReservedCount
) && !Rescan
; Index2
++) {
1519 // Skip zero size entry.
1521 if (SmramReservedRanges
[Index2
].SmramReservedSize
!= 0) {
1522 if (SmmIsSmramOverlap (
1523 &SmramRanges
[Index
],
1524 &SmramReservedRanges
[Index2
]
1527 // There is overlap, need to split entry and then rescan.
1529 SmmSplitSmramEntry (
1530 &SmramRanges
[Index
],
1531 &SmramReservedRanges
[Index2
],
1534 SmramReservedRanges
,
1535 &SmramReservedCount
,
1537 &TempSmramRangeCount
1545 // No any overlap, copy the entry to the temp SMRAM ranges.
1546 // Zero SmramRanges[Index].PhysicalSize = 0;
1548 CopyMem (&TempSmramRanges
[TempSmramRangeCount
++], &SmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1549 SmramRanges
[Index
].PhysicalSize
= 0;
1554 ASSERT (TempSmramRangeCount
<= MaxCount
);
1559 FullSmramRanges
= AllocateZeroPool ((TempSmramRangeCount
+ AdditionSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
));
1560 ASSERT (FullSmramRanges
!= NULL
);
1561 *FullSmramRangeCount
= 0;
1563 for (Index
= 0; Index
< TempSmramRangeCount
; Index
++) {
1564 if (TempSmramRanges
[Index
].PhysicalSize
!= 0) {
1568 ASSERT (Index
< TempSmramRangeCount
);
1569 for (Index2
= 0; Index2
< TempSmramRangeCount
; Index2
++) {
1570 if ((Index2
!= Index
) && (TempSmramRanges
[Index2
].PhysicalSize
!= 0) && (TempSmramRanges
[Index2
].CpuStart
< TempSmramRanges
[Index
].CpuStart
)) {
1574 CopyMem (&FullSmramRanges
[*FullSmramRangeCount
], &TempSmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1575 *FullSmramRangeCount
+= 1;
1576 TempSmramRanges
[Index
].PhysicalSize
= 0;
1577 } while (*FullSmramRangeCount
< TempSmramRangeCount
);
1578 ASSERT (*FullSmramRangeCount
== TempSmramRangeCount
);
1579 *FullSmramRangeCount
+= AdditionSmramRangeCount
;
1581 FreePool (SmramRanges
);
1582 FreePool (SmramReservedRanges
);
1583 FreePool (TempSmramRanges
);
1585 return FullSmramRanges
;
1589 The Entry Point for SMM IPL
1591 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1592 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1593 critical events required to coordinate between DXE and SMM environments.
1595 @param ImageHandle The firmware allocated handle for the EFI image.
1596 @param SystemTable A pointer to the EFI System Table.
1598 @retval EFI_SUCCESS The entry point is executed successfully.
1599 @retval Other Some error occurred when executing this entry point.
1605 IN EFI_HANDLE ImageHandle
,
1606 IN EFI_SYSTEM_TABLE
*SystemTable
1614 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1615 EFI_STATUS SetAttrStatus
;
1616 EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmDriver
;
1617 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
1620 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1621 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1624 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1627 // Get SMM Access Protocol
1629 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1630 ASSERT_EFI_ERROR (Status
);
1633 // Get SMM Control2 Protocol
1635 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1636 ASSERT_EFI_ERROR (Status
);
1638 gSmmCorePrivate
->SmramRanges
= GetFullSmramRanges (&gSmmCorePrivate
->SmramRangeCount
);
1641 // Open all SMRAM ranges
1643 Status
= mSmmAccess
->Open (mSmmAccess
);
1644 ASSERT_EFI_ERROR (Status
);
1647 // Print debug message that the SMRAM window is now open.
1649 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1652 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1654 mCurrentSmramRange
= NULL
;
1655 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1657 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1659 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1663 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1664 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
- 1) <= MAX_ADDRESS
) {
1665 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1666 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1667 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1673 if (mCurrentSmramRange
!= NULL
) {
1675 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1677 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1678 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1679 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1682 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1684 // Make sure we can change the desired memory attributes.
1686 Status
= gDS
->GetMemorySpaceDescriptor (
1690 ASSERT_EFI_ERROR (Status
);
1691 if ((MemDesc
.Capabilities
& SMRAM_CAPABILITIES
) != SMRAM_CAPABILITIES
) {
1692 gDS
->SetMemorySpaceCapabilities (
1695 MemDesc
.Capabilities
| SMRAM_CAPABILITIES
1699 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1700 // all paging attributes.
1701 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1702 // is not available here.
1705 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1706 if (!EFI_ERROR (Status
)) {
1707 MemDesc
.Attributes
&= ~(EFI_CACHE_ATTRIBUTE_MASK
| EFI_MEMORY_ATTRIBUTE_MASK
);
1708 MemDesc
.Attributes
|= EFI_MEMORY_WB
;
1709 Status
= gDS
->SetMemorySpaceAttributes (
1714 if (EFI_ERROR (Status
)) {
1715 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1719 gDS
->GetMemorySpaceDescriptor (
1723 DEBUG ((DEBUG_INFO
, "SMRAM attributes: %016lx\n", MemDesc
.Attributes
));
1724 ASSERT ((MemDesc
.Attributes
& EFI_MEMORY_ATTRIBUTE_MASK
) == 0);
1728 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1729 // Modules At Fixed Address Configuration Table.
1731 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1733 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1735 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1737 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1739 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1741 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1743 Status
= EfiGetSystemConfigurationTable (
1744 &gLoadFixedAddressConfigurationTableGuid
,
1745 (VOID
**) &mLMFAConfigurationTable
1747 if (!EFI_ERROR (Status
) && mLMFAConfigurationTable
!= NULL
) {
1748 mLMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1750 // Print the SMRAM base
1752 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable
->SmramBase
));
1756 // Fill the Smram range for all SMM code
1758 SmramRangeSmmDriver
= &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 2];
1759 SmramRangeSmmDriver
->CpuStart
= mCurrentSmramRange
->CpuStart
;
1760 SmramRangeSmmDriver
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
;
1761 SmramRangeSmmDriver
->RegionState
= mCurrentSmramRange
->RegionState
| EFI_ALLOCATED
;
1762 SmramRangeSmmDriver
->PhysicalSize
= SmmCodeSize
;
1764 mCurrentSmramRange
->PhysicalSize
-= SmmCodeSize
;
1765 mCurrentSmramRange
->CpuStart
= mCurrentSmramRange
->CpuStart
+ SmmCodeSize
;
1766 mCurrentSmramRange
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
+ SmmCodeSize
;
1769 // Load SMM Core into SMRAM and execute it from SMRAM
1771 Status
= ExecuteSmmCoreFromSmram (
1773 &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 1],
1776 if (EFI_ERROR (Status
)) {
1778 // Print error message that the SMM Core failed to be loaded and executed.
1780 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1783 // Attempt to reset SMRAM cacheability to UC
1785 if (CpuArch
!= NULL
) {
1786 SetAttrStatus
= gDS
->SetMemorySpaceAttributes(
1791 if (EFI_ERROR (SetAttrStatus
)) {
1792 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1798 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1800 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1804 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1805 // resources, and return an error so SMM IPL will be unloaded.
1807 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1809 // Close all SMRAM ranges
1811 Status
= mSmmAccess
->Close (mSmmAccess
);
1812 ASSERT_EFI_ERROR (Status
);
1815 // Print debug message that the SMRAM window is now closed.
1817 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1820 // Free all allocated resources
1822 FreePool (gSmmCorePrivate
->SmramRanges
);
1824 return EFI_UNSUPPORTED
;
1828 // Install SMM Base2 Protocol and SMM Communication Protocol
1830 Status
= gBS
->InstallMultipleProtocolInterfaces (
1832 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1833 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1834 &gEfiMmCommunication2ProtocolGuid
, &mMmCommunication2
,
1837 ASSERT_EFI_ERROR (Status
);
1840 // Create the set of protocol and event notifications that the SMM IPL requires
1842 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1843 if (mSmmIplEvents
[Index
].Protocol
) {
1844 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1845 mSmmIplEvents
[Index
].Guid
,
1846 mSmmIplEvents
[Index
].NotifyTpl
,
1847 mSmmIplEvents
[Index
].NotifyFunction
,
1848 mSmmIplEvents
[Index
].NotifyContext
,
1852 Status
= gBS
->CreateEventEx (
1854 mSmmIplEvents
[Index
].NotifyTpl
,
1855 mSmmIplEvents
[Index
].NotifyFunction
,
1856 mSmmIplEvents
[Index
].NotifyContext
,
1857 mSmmIplEvents
[Index
].Guid
,
1858 &mSmmIplEvents
[Index
].Event
1860 ASSERT_EFI_ERROR (Status
);