2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/SmmBase2.h>
18 #include <Protocol/SmmCommunication.h>
19 #include <Protocol/SmmAccess2.h>
20 #include <Protocol/SmmConfiguration.h>
21 #include <Protocol/SmmControl2.h>
22 #include <Protocol/DxeSmmReadyToLock.h>
23 #include <Protocol/Cpu.h>
25 #include <Guid/EventGroup.h>
26 #include <Guid/EventLegacyBios.h>
27 #include <Guid/LoadModuleAtFixedAddress.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/PeCoffLib.h>
32 #include <Library/CacheMaintenanceLib.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/DxeServicesTableLib.h>
37 #include <Library/DxeServicesLib.h>
38 #include <Library/UefiLib.h>
39 #include <Library/UefiRuntimeLib.h>
40 #include <Library/PcdLib.h>
41 #include <Library/ReportStatusCodeLib.h>
43 #include "PiSmmCorePrivateData.h"
46 // Function prototypes from produced protocols
50 Indicate whether the driver is currently executing in the SMM Initialization phase.
52 @param This The EFI_SMM_BASE2_PROTOCOL instance.
53 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
54 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
56 @retval EFI_INVALID_PARAMETER InSmram was NULL.
57 @retval EFI_SUCCESS The call returned successfully.
63 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
68 Retrieves the location of the System Management System Table (SMST).
70 @param This The EFI_SMM_BASE2_PROTOCOL instance.
71 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
73 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
74 @retval EFI_SUCCESS The memory was returned to the system.
75 @retval EFI_UNSUPPORTED Not in SMM.
80 SmmBase2GetSmstLocation (
81 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
82 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
86 Communicates with a registered handler.
88 This function provides a service to send and receive messages from a registered
89 UEFI service. This function is part of the SMM Communication Protocol that may
90 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
91 after SetVirtualAddressMap().
93 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
94 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
95 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
96 being returned. Zero if the handler does not wish to reply with any data.
98 @retval EFI_SUCCESS The message was successfully posted.
99 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
103 SmmCommunicationCommunicate (
104 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
105 IN OUT VOID
*CommBuffer
,
106 IN OUT UINTN
*CommSize
110 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
112 @param Event The Event that is being processed, not used.
113 @param Context Event Context, not used.
118 SmmIplSmmConfigurationEventNotify (
124 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
125 or if gEfiEventReadyToBootGuid is signalled.
127 @param Event The Event that is being processed, not used.
128 @param Context Event Context, not used.
133 SmmIplReadyToLockEventNotify (
139 Event notification that is fired when DxeDispatch Event Group is signaled.
141 @param Event The Event that is being processed, not used.
142 @param Context Event Context, not used.
147 SmmIplDxeDispatchEventNotify (
153 Event notification that is fired when a GUIDed Event Group is signaled.
155 @param Event The Event that is being processed, not used.
156 @param Context Event Context, not used.
161 SmmIplGuidedEventNotify (
167 Event notification that is fired when EndOfDxe Event Group is signaled.
169 @param Event The Event that is being processed, not used.
170 @param Context Event Context, not used.
175 SmmIplEndOfDxeEventNotify (
181 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
183 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
184 It convers pointer to new virtual address.
186 @param Event Event whose notification function is being invoked.
187 @param Context Pointer to the notification function's context.
192 SmmIplSetVirtualAddressNotify (
198 // Data structure used to declare a table of protocol notifications and event
199 // notifications required by the SMM IPL
205 EFI_EVENT_NOTIFY NotifyFunction
;
209 } SMM_IPL_EVENT_NOTIFICATION
;
212 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
214 EFI_HANDLE mSmmIplHandle
= NULL
;
217 // SMM Base 2 Protocol instance
219 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
221 SmmBase2GetSmstLocation
225 // SMM Communication Protocol instance
227 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
228 SmmCommunicationCommunicate
232 // SMM Core Private Data structure that contains the data shared between
233 // the SMM IPL and the SMM Core.
235 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
236 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
237 NULL
, // SmmIplImageHandle
238 0, // SmramRangeCount
240 NULL
, // SmmEntryPoint
241 FALSE
, // SmmEntryPointRegistered
244 NULL
, // CommunicationBuffer
246 EFI_SUCCESS
// ReturnStatus
250 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
252 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
255 // SMM IPL global variables
257 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
258 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
259 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
260 BOOLEAN mSmmLocked
= FALSE
;
261 BOOLEAN mEndOfDxe
= FALSE
;
262 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
263 UINT64 mSmramCacheSize
;
265 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader
;
268 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
270 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
272 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
273 // the associated event is immediately signalled, so the notification function will be executed and the
274 // SMM Configuration Protocol will be found if it is already in the handle database.
276 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, TPL_NOTIFY
, NULL
},
278 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
279 // the associated event is immediately signalled, so the notification function will be executed and the
280 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
282 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, TPL_CALLBACK
, NULL
},
284 // Declare event notification on EndOfDxe event. When this notification is established,
285 // the associated event is immediately signalled, so the notification function will be executed and the
286 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
288 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplGuidedEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
290 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
292 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplEndOfDxeEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
294 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
295 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
296 // if notified, so the SMM Core can dispatch SMM drivers.
298 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplDxeDispatchEventNotify
, &gEfiEventDxeDispatchGuid
, TPL_CALLBACK
, NULL
},
300 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
301 // used to make sure SMRAM is locked before any boot options are processed.
303 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
305 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
306 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
307 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
308 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
310 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, TPL_CALLBACK
, NULL
},
312 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
313 // to notify SMM driver that system enter exit boot services.
315 { FALSE
, FALSE
, &gEfiEventExitBootServicesGuid
, SmmIplGuidedEventNotify
, &gEfiEventExitBootServicesGuid
, TPL_CALLBACK
, NULL
},
317 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
318 // to notify SMM driver that system enter ready to boot.
320 { FALSE
, FALSE
, &gEfiEventReadyToBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
322 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
323 // and mSmmControl2 from physical addresses to virtual addresses.
325 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, TPL_CALLBACK
, NULL
},
327 // Terminate the table of event notifications
329 { FALSE
, FALSE
, NULL
, NULL
, NULL
, TPL_CALLBACK
, NULL
}
333 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
335 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
337 @param SmramRange The SMRAM range to search from.
338 @param SmramCacheBase The returned cache range base.
339 @param SmramCacheSize The returned cache range size.
344 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
345 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
346 OUT UINT64
*SmramCacheSize
350 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
351 UINT64 RangePhysicalSize
;
352 BOOLEAN FoundAjacentRange
;
354 *SmramCacheBase
= SmramRange
->CpuStart
;
355 *SmramCacheSize
= SmramRange
->PhysicalSize
;
358 FoundAjacentRange
= FALSE
;
359 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
360 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
361 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
362 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
363 *SmramCacheBase
= RangeCpuStart
;
364 *SmramCacheSize
+= RangePhysicalSize
;
365 FoundAjacentRange
= TRUE
;
366 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
367 *SmramCacheSize
+= RangePhysicalSize
;
368 FoundAjacentRange
= TRUE
;
371 } while (FoundAjacentRange
);
376 Indicate whether the driver is currently executing in the SMM Initialization phase.
378 @param This The EFI_SMM_BASE2_PROTOCOL instance.
379 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
380 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
382 @retval EFI_INVALID_PARAMETER InSmram was NULL.
383 @retval EFI_SUCCESS The call returned successfully.
389 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
393 if (InSmram
== NULL
) {
394 return EFI_INVALID_PARAMETER
;
397 *InSmram
= gSmmCorePrivate
->InSmm
;
403 Retrieves the location of the System Management System Table (SMST).
405 @param This The EFI_SMM_BASE2_PROTOCOL instance.
406 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
408 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
409 @retval EFI_SUCCESS The memory was returned to the system.
410 @retval EFI_UNSUPPORTED Not in SMM.
415 SmmBase2GetSmstLocation (
416 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
417 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
420 if ((This
== NULL
) ||(Smst
== NULL
)) {
421 return EFI_INVALID_PARAMETER
;
424 if (!gSmmCorePrivate
->InSmm
) {
425 return EFI_UNSUPPORTED
;
428 *Smst
= gSmmCorePrivate
->Smst
;
434 Communicates with a registered handler.
436 This function provides a service to send and receive messages from a registered
437 UEFI service. This function is part of the SMM Communication Protocol that may
438 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
439 after SetVirtualAddressMap().
441 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
442 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
443 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
444 being returned. Zero if the handler does not wish to reply with any data.
446 @retval EFI_SUCCESS The message was successfully posted.
447 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
451 SmmCommunicationCommunicate (
452 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
453 IN OUT VOID
*CommBuffer
,
454 IN OUT UINTN
*CommSize
458 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
464 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
465 return EFI_INVALID_PARAMETER
;
469 // CommSize must hold HeaderGuid and MessageLength
471 if (*CommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
472 return EFI_INVALID_PARAMETER
;
476 // If not already in SMM, then generate a Software SMI
478 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
480 // Put arguments for Software SMI in gSmmCorePrivate
482 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
483 gSmmCorePrivate
->BufferSize
= *CommSize
;
486 // Generate Software SMI
488 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
489 if (EFI_ERROR (Status
)) {
490 return EFI_UNSUPPORTED
;
494 // Return status from software SMI
496 *CommSize
= gSmmCorePrivate
->BufferSize
;
497 return gSmmCorePrivate
->ReturnStatus
;
501 // If we are in SMM, then the execution mode must be physical, which means that
502 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
503 // has been called, then a direct invocation of the Software SMI is not
504 // not allowed so return EFI_INVALID_PARAMETER.
506 if (EfiGoneVirtual()) {
507 return EFI_INVALID_PARAMETER
;
511 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
513 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
514 return EFI_INVALID_PARAMETER
;
518 // Save current InSmm state and set InSmm state to TRUE
520 OldInSmm
= gSmmCorePrivate
->InSmm
;
521 gSmmCorePrivate
->InSmm
= TRUE
;
524 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
526 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
527 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
528 Status
= gSmmCorePrivate
->Smst
->SmiManage (
529 &CommunicateHeader
->HeaderGuid
,
531 CommunicateHeader
->Data
,
536 // Update CommunicationBuffer, BufferSize and ReturnStatus
537 // Communicate service finished, reset the pointer to CommBuffer to NULL
539 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
542 // Restore original InSmm state
544 gSmmCorePrivate
->InSmm
= OldInSmm
;
546 return (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
550 Event notification that is fired when GUIDed Event Group is signaled.
552 @param Event The Event that is being processed, not used.
553 @param Context Event Context, not used.
558 SmmIplGuidedEventNotify (
566 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
568 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
569 mCommunicateHeader
.MessageLength
= 1;
570 mCommunicateHeader
.Data
[0] = 0;
573 // Generate the Software SMI and return the result
575 Size
= sizeof (mCommunicateHeader
);
576 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
580 Event notification that is fired when EndOfDxe Event Group is signaled.
582 @param Event The Event that is being processed, not used.
583 @param Context Event Context, not used.
588 SmmIplEndOfDxeEventNotify (
597 Event notification that is fired when DxeDispatch Event Group is signaled.
599 @param Event The Event that is being processed, not used.
600 @param Context Event Context, not used.
605 SmmIplDxeDispatchEventNotify (
614 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
618 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
619 // Clear the buffer passed into the Software SMI. This buffer will return
620 // the status of the SMM Core Dispatcher.
622 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
623 mCommunicateHeader
.MessageLength
= 1;
624 mCommunicateHeader
.Data
[0] = 0;
627 // Generate the Software SMI and return the result
629 Size
= sizeof (mCommunicateHeader
);
630 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
633 // Return if there is no request to restart the SMM Core Dispatcher
635 if (mCommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
640 // Attempt to reset SMRAM cacheability to UC
641 // Assume CPU AP is available at this time
643 Status
= gDS
->SetMemorySpaceAttributes(
648 if (EFI_ERROR (Status
)) {
649 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
653 // Close all SMRAM ranges to protect SMRAM
655 Status
= mSmmAccess
->Close (mSmmAccess
);
656 ASSERT_EFI_ERROR (Status
);
659 // Print debug message that the SMRAM window is now closed.
661 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
666 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
668 @param Event The Event that is being processed, not used.
669 @param Context Event Context, not used.
674 SmmIplSmmConfigurationEventNotify (
680 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
683 // Make sure this notification is for this handler
685 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
686 if (EFI_ERROR (Status
)) {
691 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
693 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
694 ASSERT_EFI_ERROR (Status
);
697 // Set flag to indicate that the SMM Entry Point has been registered which
698 // means that SMIs are now fully operational.
700 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
703 // Print debug message showing SMM Core entry point address.
705 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
709 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
710 or if gEfiEventReadyToBootGuid is signaled.
712 @param Event The Event that is being processed, not used.
713 @param Context Event Context, not used.
718 SmmIplReadyToLockEventNotify (
728 // See if we are already locked
735 // Make sure this notification is for this handler
737 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
738 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
739 if (EFI_ERROR (Status
)) {
744 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
745 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
746 // Print a warning on debug builds.
748 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
752 DEBUG ((DEBUG_ERROR
, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
754 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
755 (EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
761 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
763 mSmmAccess
->Lock (mSmmAccess
);
766 // Close protocol and event notification events that do not apply after the
767 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
768 // event has been signalled.
770 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
771 if (mSmmIplEvents
[Index
].CloseOnLock
) {
772 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
777 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
779 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
782 // Print debug message that the SMRAM window is now locked.
784 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
787 // Set flag so this operation will not be performed again
793 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
795 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
796 It convers pointer to new virtual address.
798 @param Event Event whose notification function is being invoked.
799 @param Context Pointer to the notification function's context.
804 SmmIplSetVirtualAddressNotify (
809 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
813 Get the fixed loading address from image header assigned by build tool. This function only be called
814 when Loading module at Fixed address feature enabled.
816 @param ImageContext Pointer to the image context structure that describes the PE/COFF
817 image that needs to be examined by this function.
818 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
819 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
822 GetPeCoffImageFixLoadingAssignedAddress(
823 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
826 UINTN SectionHeaderOffset
;
828 EFI_IMAGE_SECTION_HEADER SectionHeader
;
829 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
830 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
833 UINT16 NumberOfSections
;
834 EFI_PHYSICAL_ADDRESS SmramBase
;
836 UINT64 ValueInSectionHeader
;
838 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
840 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
842 FixLoadingAddress
= 0;
843 Status
= EFI_NOT_FOUND
;
844 SmramBase
= mCurrentSmramRange
->CpuStart
;
846 // Get PeHeader pointer
848 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
849 SectionHeaderOffset
= (UINTN
)(
850 ImageContext
->PeCoffHeaderOffset
+
852 sizeof (EFI_IMAGE_FILE_HEADER
) +
853 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
855 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
858 // Get base address from the first section header that doesn't point to code section.
860 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
862 // Read section header from file
864 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
865 Status
= ImageContext
->ImageRead (
866 ImageContext
->Handle
,
871 if (EFI_ERROR (Status
)) {
875 Status
= EFI_NOT_FOUND
;
877 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
879 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
880 // first section header that doesn't point to code section in image header. And there is an assumption that when the
881 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
882 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
884 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
885 if (ValueInSectionHeader
!= 0) {
887 // Found first section header that doesn't point to code section in which build tool saves the
888 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
890 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
892 if (SmramBase
+ SmmCodeSize
> FixLoadingAddress
&& SmramBase
<= FixLoadingAddress
) {
894 // The assigned address is valid. Return the specified loading address
896 ImageContext
->ImageAddress
= FixLoadingAddress
;
897 Status
= EFI_SUCCESS
;
902 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
904 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress
, Status
));
908 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
910 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
911 currently executing image, the rang of SMRAM to
912 hold SMM Core will be excluded.
913 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
915 @param[in] Context Context to pass into SMM Core
921 ExecuteSmmCoreFromSmram (
922 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRange
,
923 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmCore
,
930 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
932 EFI_IMAGE_ENTRY_POINT EntryPoint
;
935 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
937 Status
= GetSectionFromAnyFvByFileType (
938 EFI_FV_FILETYPE_SMM_CORE
,
945 if (EFI_ERROR (Status
)) {
950 // Initilize ImageContext
952 ImageContext
.Handle
= SourceBuffer
;
953 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
956 // Get information about the image being loaded
958 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
959 if (EFI_ERROR (Status
)) {
963 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
964 // the address assigned by build tool.
966 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
968 // Get the fixed loading address assigned by Build tool
970 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
971 if (!EFI_ERROR (Status
)) {
973 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
977 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
979 gSmmCorePrivate
->SmramRangeCount
--;
981 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
983 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
984 // specified by SmramRange
986 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
988 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
989 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
991 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
992 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
993 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
994 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
995 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
998 // Align buffer on section boundary
1000 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1004 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1005 // specified by SmramRange
1007 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1009 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1010 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1012 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1013 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1014 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1015 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1016 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1019 // Align buffer on section boundary
1021 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1024 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
1025 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)(ImageContext
.SectionAlignment
- 1));
1028 // Print debug message showing SMM Core load address.
1030 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
1033 // Load the image to our new buffer
1035 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1036 if (!EFI_ERROR (Status
)) {
1038 // Relocate the image in our new buffer
1040 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1041 if (!EFI_ERROR (Status
)) {
1043 // Flush the instruction cache so the image data are written before we execute it
1045 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
1048 // Print debug message showing SMM Core entry point address.
1050 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
1052 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
1053 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
1054 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
1055 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
1057 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1062 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1063 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1068 // Always free memory allocted by GetFileBufferByFilePath ()
1070 FreePool (SourceBuffer
);
1076 SMM split SMRAM entry.
1078 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1079 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1080 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1081 @param[in, out] RangeCount Pointer to range count.
1082 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1083 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1084 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1085 that no need to be split anymore.
1086 @param[in, out] FinalRangeCount Pointer to final range count.
1090 SmmSplitSmramEntry (
1091 IN OUT EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1092 IN OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
,
1093 OUT EFI_SMRAM_DESCRIPTOR
*Ranges
,
1094 IN OUT UINTN
*RangeCount
,
1095 OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRanges
,
1096 IN OUT UINTN
*ReservedRangeCount
,
1097 OUT EFI_SMRAM_DESCRIPTOR
*FinalRanges
,
1098 IN OUT UINTN
*FinalRangeCount
1101 UINT64 RangeToCompareEnd
;
1102 UINT64 ReservedRangeToCompareEnd
;
1104 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1105 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1107 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1108 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1109 if (RangeToCompareEnd
< ReservedRangeToCompareEnd
) {
1111 // RangeToCompare ReservedRangeToCompare
1112 // ---- ---- --------------------------------------
1113 // | | | | -> 1. ReservedRangeToCompare
1114 // ---- | | |--| --------------------------------------
1116 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1117 // | | | | | | RangeToCompare->PhysicalSize = 0
1118 // ---- | | |--| --------------------------------------
1119 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1120 // ---- ---- --------------------------------------
1124 // 1. Update ReservedRangeToCompare.
1126 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1128 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1129 // Zero RangeToCompare->PhysicalSize.
1131 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1132 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1133 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1134 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompare
->PhysicalSize
;
1135 *FinalRangeCount
+= 1;
1136 RangeToCompare
->PhysicalSize
= 0;
1138 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1140 ReservedRanges
[*ReservedRangeCount
].SmramReservedStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1141 ReservedRanges
[*ReservedRangeCount
].SmramReservedSize
= ReservedRangeToCompareEnd
- RangeToCompareEnd
;
1142 *ReservedRangeCount
+= 1;
1145 // RangeToCompare ReservedRangeToCompare
1146 // ---- ---- --------------------------------------
1147 // | | | | -> 1. ReservedRangeToCompare
1148 // ---- | | |--| --------------------------------------
1150 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1152 // | | ---- |--| --------------------------------------
1153 // | | | | -> 3. RangeToCompare
1154 // ---- ---- --------------------------------------
1158 // 1. Update ReservedRangeToCompare.
1160 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1162 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1164 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1165 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1166 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1167 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompareEnd
- RangeToCompare
->CpuStart
;
1168 *FinalRangeCount
+= 1;
1170 // 3. Update RangeToCompare.
1172 RangeToCompare
->CpuStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1173 RangeToCompare
->PhysicalStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1174 RangeToCompare
->PhysicalSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1176 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1177 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1178 if (ReservedRangeToCompareEnd
< RangeToCompareEnd
) {
1180 // RangeToCompare ReservedRangeToCompare
1181 // ---- ---- --------------------------------------
1182 // | | | | -> 1. RangeToCompare
1183 // | | ---- |--| --------------------------------------
1185 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1186 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1187 // | | ---- |--| --------------------------------------
1188 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1189 // ---- ---- --------------------------------------
1193 // 1. Update RangeToCompare.
1195 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1197 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1198 // ReservedRangeToCompare->SmramReservedSize = 0
1200 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1201 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1202 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1203 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompare
->SmramReservedSize
;
1204 *FinalRangeCount
+= 1;
1205 ReservedRangeToCompare
->SmramReservedSize
= 0;
1207 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1209 Ranges
[*RangeCount
].CpuStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1210 Ranges
[*RangeCount
].PhysicalStart
= FinalRanges
[*FinalRangeCount
- 1].PhysicalStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1211 Ranges
[*RangeCount
].RegionState
= RangeToCompare
->RegionState
;
1212 Ranges
[*RangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompareEnd
;
1216 // RangeToCompare ReservedRangeToCompare
1217 // ---- ---- --------------------------------------
1218 // | | | | -> 1. RangeToCompare
1219 // | | ---- |--| --------------------------------------
1221 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1223 // ---- | | |--| --------------------------------------
1224 // | | | | -> 3. ReservedRangeToCompare
1225 // ---- ---- --------------------------------------
1229 // 1. Update RangeToCompare.
1231 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1233 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1234 // ReservedRangeToCompare->SmramReservedSize = 0
1236 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1237 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1238 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1239 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompare
->SmramReservedStart
;
1240 *FinalRangeCount
+= 1;
1242 // 3. Update ReservedRangeToCompare.
1244 ReservedRangeToCompare
->SmramReservedStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1245 ReservedRangeToCompare
->SmramReservedSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1251 Returns if SMRAM range and SMRAM reserved range are overlapped.
1253 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1254 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1256 @retval TRUE There is overlap.
1257 @retval FALSE There is no overlap.
1262 IN EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1263 IN EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
1266 UINT64 RangeToCompareEnd
;
1267 UINT64 ReservedRangeToCompareEnd
;
1269 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1270 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1272 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1273 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1275 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1276 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1283 Get full SMRAM ranges.
1285 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1286 SmmConfiguration protocol, split the entries if there is overlap between them.
1287 It will also reserve one entry for SMM core.
1289 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1291 @return Pointer to full SMRAM ranges.
1294 EFI_SMRAM_DESCRIPTOR
*
1295 GetFullSmramRanges (
1296 OUT UINTN
*FullSmramRangeCount
1300 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1304 EFI_SMRAM_DESCRIPTOR
*FullSmramRanges
;
1305 UINTN TempSmramRangeCount
;
1306 UINTN AdditionSmramRangeCount
;
1307 EFI_SMRAM_DESCRIPTOR
*TempSmramRanges
;
1308 UINTN SmramRangeCount
;
1309 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1310 UINTN SmramReservedCount
;
1311 EFI_SMM_RESERVED_SMRAM_REGION
*SmramReservedRanges
;
1316 // Get SMM Configuration Protocol if it is present.
1318 SmmConfiguration
= NULL
;
1319 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1322 // Get SMRAM information.
1325 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1326 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1328 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1331 // Get SMRAM reserved region count.
1333 SmramReservedCount
= 0;
1334 if (SmmConfiguration
!= NULL
) {
1335 while (SmmConfiguration
->SmramReservedRegions
[SmramReservedCount
].SmramReservedSize
!= 0) {
1336 SmramReservedCount
++;
1341 // Reserve one entry for SMM Core in the full SMRAM ranges.
1343 AdditionSmramRangeCount
= 1;
1344 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1346 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1348 AdditionSmramRangeCount
= 2;
1351 if (SmramReservedCount
== 0) {
1353 // No reserved SMRAM entry from SMM Configuration Protocol.
1355 *FullSmramRangeCount
= SmramRangeCount
+ AdditionSmramRangeCount
;
1356 Size
= (*FullSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
);
1357 FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocateZeroPool (Size
);
1358 ASSERT (FullSmramRanges
!= NULL
);
1360 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, FullSmramRanges
);
1361 ASSERT_EFI_ERROR (Status
);
1363 return FullSmramRanges
;
1367 // Why MaxCount = X + 2 * Y?
1368 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1370 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1377 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1388 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1401 MaxCount
= SmramRangeCount
+ 2 * SmramReservedCount
;
1403 Size
= MaxCount
* sizeof (EFI_SMM_RESERVED_SMRAM_REGION
);
1404 SmramReservedRanges
= (EFI_SMM_RESERVED_SMRAM_REGION
*) AllocatePool (Size
);
1405 ASSERT (SmramReservedRanges
!= NULL
);
1406 for (Index
= 0; Index
< SmramReservedCount
; Index
++) {
1407 CopyMem (&SmramReservedRanges
[Index
], &SmmConfiguration
->SmramReservedRegions
[Index
], sizeof (EFI_SMM_RESERVED_SMRAM_REGION
));
1410 Size
= MaxCount
* sizeof (EFI_SMRAM_DESCRIPTOR
);
1411 TempSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1412 ASSERT (TempSmramRanges
!= NULL
);
1413 TempSmramRangeCount
= 0;
1415 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1416 ASSERT (SmramRanges
!= NULL
);
1417 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, SmramRanges
);
1418 ASSERT_EFI_ERROR (Status
);
1422 for (Index
= 0; (Index
< SmramRangeCount
) && !Rescan
; Index
++) {
1424 // Skip zero size entry.
1426 if (SmramRanges
[Index
].PhysicalSize
!= 0) {
1427 for (Index2
= 0; (Index2
< SmramReservedCount
) && !Rescan
; Index2
++) {
1429 // Skip zero size entry.
1431 if (SmramReservedRanges
[Index2
].SmramReservedSize
!= 0) {
1432 if (SmmIsSmramOverlap (
1433 &SmramRanges
[Index
],
1434 &SmramReservedRanges
[Index2
]
1437 // There is overlap, need to split entry and then rescan.
1439 SmmSplitSmramEntry (
1440 &SmramRanges
[Index
],
1441 &SmramReservedRanges
[Index2
],
1444 SmramReservedRanges
,
1445 &SmramReservedCount
,
1447 &TempSmramRangeCount
1455 // No any overlap, copy the entry to the temp SMRAM ranges.
1456 // Zero SmramRanges[Index].PhysicalSize = 0;
1458 CopyMem (&TempSmramRanges
[TempSmramRangeCount
++], &SmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1459 SmramRanges
[Index
].PhysicalSize
= 0;
1464 ASSERT (TempSmramRangeCount
<= MaxCount
);
1469 FullSmramRanges
= AllocateZeroPool ((TempSmramRangeCount
+ AdditionSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
));
1470 ASSERT (FullSmramRanges
!= NULL
);
1471 *FullSmramRangeCount
= 0;
1473 for (Index
= 0; Index
< TempSmramRangeCount
; Index
++) {
1474 if (TempSmramRanges
[Index
].PhysicalSize
!= 0) {
1478 ASSERT (Index
< TempSmramRangeCount
);
1479 for (Index2
= 0; Index2
< TempSmramRangeCount
; Index2
++) {
1480 if ((Index2
!= Index
) && (TempSmramRanges
[Index2
].PhysicalSize
!= 0) && (TempSmramRanges
[Index2
].CpuStart
< TempSmramRanges
[Index
].CpuStart
)) {
1484 CopyMem (&FullSmramRanges
[*FullSmramRangeCount
], &TempSmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1485 *FullSmramRangeCount
+= 1;
1486 TempSmramRanges
[Index
].PhysicalSize
= 0;
1487 } while (*FullSmramRangeCount
< TempSmramRangeCount
);
1488 ASSERT (*FullSmramRangeCount
== TempSmramRangeCount
);
1489 *FullSmramRangeCount
+= AdditionSmramRangeCount
;
1491 FreePool (SmramRanges
);
1492 FreePool (SmramReservedRanges
);
1493 FreePool (TempSmramRanges
);
1495 return FullSmramRanges
;
1499 The Entry Point for SMM IPL
1501 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1502 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1503 critical events required to coordinate between DXE and SMM environments.
1505 @param ImageHandle The firmware allocated handle for the EFI image.
1506 @param SystemTable A pointer to the EFI System Table.
1508 @retval EFI_SUCCESS The entry point is executed successfully.
1509 @retval Other Some error occurred when executing this entry point.
1515 IN EFI_HANDLE ImageHandle
,
1516 IN EFI_SYSTEM_TABLE
*SystemTable
1524 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
1525 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1526 EFI_STATUS SetAttrStatus
;
1527 EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmDriver
;
1530 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1531 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1534 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1537 // Get SMM Access Protocol
1539 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1540 ASSERT_EFI_ERROR (Status
);
1543 // Get SMM Control2 Protocol
1545 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1546 ASSERT_EFI_ERROR (Status
);
1548 gSmmCorePrivate
->SmramRanges
= GetFullSmramRanges (&gSmmCorePrivate
->SmramRangeCount
);
1551 // Open all SMRAM ranges
1553 Status
= mSmmAccess
->Open (mSmmAccess
);
1554 ASSERT_EFI_ERROR (Status
);
1557 // Print debug message that the SMRAM window is now open.
1559 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1562 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1564 mCurrentSmramRange
= NULL
;
1565 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1567 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1569 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1573 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1574 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
- 1) <= MAX_ADDRESS
) {
1575 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1576 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1577 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1583 if (mCurrentSmramRange
!= NULL
) {
1585 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1587 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1588 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1589 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1592 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1594 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1595 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1596 // is not available here.
1599 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1600 if (!EFI_ERROR (Status
)) {
1601 Status
= gDS
->SetMemorySpaceAttributes(
1606 if (EFI_ERROR (Status
)) {
1607 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1611 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1612 // Modules At Fixed Address Configuration Table.
1614 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1616 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1618 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1620 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1622 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1624 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1626 Status
= EfiGetSystemConfigurationTable (
1627 &gLoadFixedAddressConfigurationTableGuid
,
1628 (VOID
**) &LMFAConfigurationTable
1630 if (!EFI_ERROR (Status
) && LMFAConfigurationTable
!= NULL
) {
1631 LMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1633 // Print the SMRAM base
1635 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable
->SmramBase
));
1639 // Fill the Smram range for all SMM code
1641 SmramRangeSmmDriver
= &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 2];
1642 SmramRangeSmmDriver
->CpuStart
= mCurrentSmramRange
->CpuStart
;
1643 SmramRangeSmmDriver
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
;
1644 SmramRangeSmmDriver
->RegionState
= mCurrentSmramRange
->RegionState
| EFI_ALLOCATED
;
1645 SmramRangeSmmDriver
->PhysicalSize
= SmmCodeSize
;
1647 mCurrentSmramRange
->PhysicalSize
-= SmmCodeSize
;
1648 mCurrentSmramRange
->CpuStart
= mCurrentSmramRange
->CpuStart
+ SmmCodeSize
;
1649 mCurrentSmramRange
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
+ SmmCodeSize
;
1652 // Load SMM Core into SMRAM and execute it from SMRAM
1654 Status
= ExecuteSmmCoreFromSmram (
1656 &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 1],
1659 if (EFI_ERROR (Status
)) {
1661 // Print error message that the SMM Core failed to be loaded and executed.
1663 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1666 // Attempt to reset SMRAM cacheability to UC
1668 if (CpuArch
!= NULL
) {
1669 SetAttrStatus
= gDS
->SetMemorySpaceAttributes(
1674 if (EFI_ERROR (SetAttrStatus
)) {
1675 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1681 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1683 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1687 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1688 // resources, and return an error so SMM IPL will be unloaded.
1690 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1692 // Close all SMRAM ranges
1694 Status
= mSmmAccess
->Close (mSmmAccess
);
1695 ASSERT_EFI_ERROR (Status
);
1698 // Print debug message that the SMRAM window is now closed.
1700 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1703 // Free all allocated resources
1705 FreePool (gSmmCorePrivate
->SmramRanges
);
1707 return EFI_UNSUPPORTED
;
1711 // Install SMM Base2 Protocol and SMM Communication Protocol
1713 Status
= gBS
->InstallMultipleProtocolInterfaces (
1715 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1716 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1719 ASSERT_EFI_ERROR (Status
);
1722 // Create the set of protocol and event notififcations that the SMM IPL requires
1724 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1725 if (mSmmIplEvents
[Index
].Protocol
) {
1726 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1727 mSmmIplEvents
[Index
].Guid
,
1728 mSmmIplEvents
[Index
].NotifyTpl
,
1729 mSmmIplEvents
[Index
].NotifyFunction
,
1730 mSmmIplEvents
[Index
].NotifyContext
,
1734 Status
= gBS
->CreateEventEx (
1736 mSmmIplEvents
[Index
].NotifyTpl
,
1737 mSmmIplEvents
[Index
].NotifyFunction
,
1738 mSmmIplEvents
[Index
].NotifyContext
,
1739 mSmmIplEvents
[Index
].Guid
,
1740 &mSmmIplEvents
[Index
].Event
1742 ASSERT_EFI_ERROR (Status
);