2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2015, 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>
42 #include "PiSmmCorePrivateData.h"
45 // Function prototypes from produced protocols
49 Indicate whether the driver is currently executing in the SMM Initialization phase.
51 @param This The EFI_SMM_BASE2_PROTOCOL instance.
52 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
53 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
55 @retval EFI_INVALID_PARAMETER InSmram was NULL.
56 @retval EFI_SUCCESS The call returned successfully.
62 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
67 Retrieves the location of the System Management System Table (SMST).
69 @param This The EFI_SMM_BASE2_PROTOCOL instance.
70 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
72 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
73 @retval EFI_SUCCESS The memory was returned to the system.
74 @retval EFI_UNSUPPORTED Not in SMM.
79 SmmBase2GetSmstLocation (
80 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
81 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
85 Communicates with a registered handler.
87 This function provides a service to send and receive messages from a registered
88 UEFI service. This function is part of the SMM Communication Protocol that may
89 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
90 after SetVirtualAddressMap().
92 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
93 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
94 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
95 being returned. Zero if the handler does not wish to reply with any data.
97 @retval EFI_SUCCESS The message was successfully posted.
98 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
102 SmmCommunicationCommunicate (
103 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
104 IN OUT VOID
*CommBuffer
,
105 IN OUT UINTN
*CommSize
109 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
111 @param Event The Event that is being processed, not used.
112 @param Context Event Context, not used.
117 SmmIplSmmConfigurationEventNotify (
123 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
124 or if gEfiEventReadyToBootGuid is signalled.
126 @param Event The Event that is being processed, not used.
127 @param Context Event Context, not used.
132 SmmIplReadyToLockEventNotify (
138 Event notification that is fired when DxeDispatch Event Group is signaled.
140 @param Event The Event that is being processed, not used.
141 @param Context Event Context, not used.
146 SmmIplDxeDispatchEventNotify (
152 Event notification that is fired when a GUIDed Event Group is signaled.
154 @param Event The Event that is being processed, not used.
155 @param Context Event Context, not used.
160 SmmIplGuidedEventNotify (
166 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
168 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
169 It convers pointer to new virtual address.
171 @param Event Event whose notification function is being invoked.
172 @param Context Pointer to the notification function's context.
177 SmmIplSetVirtualAddressNotify (
183 // Data structure used to declare a table of protocol notifications and event
184 // notifications required by the SMM IPL
190 EFI_EVENT_NOTIFY NotifyFunction
;
194 } SMM_IPL_EVENT_NOTIFICATION
;
197 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
199 EFI_HANDLE mSmmIplHandle
= NULL
;
202 // SMM Base 2 Protocol instance
204 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
206 SmmBase2GetSmstLocation
210 // SMM Communication Protocol instance
212 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
213 SmmCommunicationCommunicate
217 // SMM Core Private Data structure that contains the data shared between
218 // the SMM IPL and the SMM Core.
220 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
221 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
222 NULL
, // SmmIplImageHandle
223 0, // SmramRangeCount
225 NULL
, // SmmEntryPoint
226 FALSE
, // SmmEntryPointRegistered
229 NULL
, // CommunicationBuffer
231 EFI_SUCCESS
// ReturnStatus
235 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
237 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
240 // SMM IPL global variables
242 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
243 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
244 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
245 BOOLEAN mSmmLocked
= FALSE
;
246 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
247 UINT64 mSmramCacheSize
;
250 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
252 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
254 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
255 // the associated event is immediately signalled, so the notification function will be executed and the
256 // SMM Configuration Protocol will be found if it is already in the handle database.
258 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, TPL_NOTIFY
, NULL
},
260 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
261 // the associated event is immediately signalled, so the notification function will be executed and the
262 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
264 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, TPL_CALLBACK
, NULL
},
266 // Declare event notification on EndOfDxe event. When this notification is etablished,
267 // the associated event is immediately signalled, so the notification function will be executed and the
268 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
270 { FALSE
, FALSE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplGuidedEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
272 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
273 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
274 // if notified, so the SMM Core can dispatch SMM drivers.
276 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplDxeDispatchEventNotify
, &gEfiEventDxeDispatchGuid
, TPL_CALLBACK
, NULL
},
278 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
279 // used to make sure SMRAM is locked before any boot options are processed.
281 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
283 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
284 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
285 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
286 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
288 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, TPL_CALLBACK
, NULL
},
290 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
291 // to notify SMM driver that system enter exit boot services.
293 { FALSE
, FALSE
, &gEfiEventExitBootServicesGuid
, SmmIplGuidedEventNotify
, &gEfiEventExitBootServicesGuid
, TPL_CALLBACK
, NULL
},
295 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
296 // to notify SMM driver that system enter ready to boot.
298 { FALSE
, FALSE
, &gEfiEventReadyToBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
300 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
301 // and mSmmControl2 from physical addresses to virtual addresses.
303 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, TPL_CALLBACK
, NULL
},
305 // Terminate the table of event notifications
307 { FALSE
, FALSE
, NULL
, NULL
, NULL
, TPL_CALLBACK
, NULL
}
311 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
313 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
315 @param SmramRange The SMRAM range to search from.
316 @param SmramCacheBase The returned cache range base.
317 @param SmramCacheSize The returned cache range size.
322 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
323 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
324 OUT UINT64
*SmramCacheSize
328 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
329 UINT64 RangePhysicalSize
;
330 BOOLEAN FoundAjacentRange
;
332 *SmramCacheBase
= SmramRange
->CpuStart
;
333 *SmramCacheSize
= SmramRange
->PhysicalSize
;
336 FoundAjacentRange
= FALSE
;
337 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
338 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
339 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
340 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
341 *SmramCacheBase
= RangeCpuStart
;
342 *SmramCacheSize
+= RangePhysicalSize
;
343 FoundAjacentRange
= TRUE
;
344 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
345 *SmramCacheSize
+= RangePhysicalSize
;
346 FoundAjacentRange
= TRUE
;
349 } while (FoundAjacentRange
);
354 Indicate whether the driver is currently executing in the SMM Initialization phase.
356 @param This The EFI_SMM_BASE2_PROTOCOL instance.
357 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
358 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
360 @retval EFI_INVALID_PARAMETER InSmram was NULL.
361 @retval EFI_SUCCESS The call returned successfully.
367 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
371 if (InSmram
== NULL
) {
372 return EFI_INVALID_PARAMETER
;
375 *InSmram
= gSmmCorePrivate
->InSmm
;
381 Retrieves the location of the System Management System Table (SMST).
383 @param This The EFI_SMM_BASE2_PROTOCOL instance.
384 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
386 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
387 @retval EFI_SUCCESS The memory was returned to the system.
388 @retval EFI_UNSUPPORTED Not in SMM.
393 SmmBase2GetSmstLocation (
394 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
395 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
398 if ((This
== NULL
) ||(Smst
== NULL
)) {
399 return EFI_INVALID_PARAMETER
;
402 if (!gSmmCorePrivate
->InSmm
) {
403 return EFI_UNSUPPORTED
;
406 *Smst
= gSmmCorePrivate
->Smst
;
412 Communicates with a registered handler.
414 This function provides a service to send and receive messages from a registered
415 UEFI service. This function is part of the SMM Communication Protocol that may
416 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
417 after SetVirtualAddressMap().
419 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
420 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
421 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
422 being returned. Zero if the handler does not wish to reply with any data.
424 @retval EFI_SUCCESS The message was successfully posted.
425 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
429 SmmCommunicationCommunicate (
430 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
431 IN OUT VOID
*CommBuffer
,
432 IN OUT UINTN
*CommSize
436 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
442 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
443 return EFI_INVALID_PARAMETER
;
447 // CommSize must hold HeaderGuid and MessageLength
449 if (*CommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
450 return EFI_INVALID_PARAMETER
;
454 // If not already in SMM, then generate a Software SMI
456 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
458 // Put arguments for Software SMI in gSmmCorePrivate
460 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
461 gSmmCorePrivate
->BufferSize
= *CommSize
;
464 // Generate Software SMI
466 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
467 if (EFI_ERROR (Status
)) {
468 return EFI_UNSUPPORTED
;
472 // Return status from software SMI
474 *CommSize
= gSmmCorePrivate
->BufferSize
;
475 return gSmmCorePrivate
->ReturnStatus
;
479 // If we are in SMM, then the execution mode must be physical, which means that
480 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
481 // has been called, then a direct invocation of the Software SMI is not
482 // not allowed so return EFI_INVALID_PARAMETER.
484 if (EfiGoneVirtual()) {
485 return EFI_INVALID_PARAMETER
;
489 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
491 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
492 return EFI_INVALID_PARAMETER
;
496 // Save current InSmm state and set InSmm state to TRUE
498 OldInSmm
= gSmmCorePrivate
->InSmm
;
499 gSmmCorePrivate
->InSmm
= TRUE
;
502 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
504 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
505 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
506 Status
= gSmmCorePrivate
->Smst
->SmiManage (
507 &CommunicateHeader
->HeaderGuid
,
509 CommunicateHeader
->Data
,
514 // Update CommunicationBuffer, BufferSize and ReturnStatus
515 // Communicate service finished, reset the pointer to CommBuffer to NULL
517 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
520 // Restore original InSmm state
522 gSmmCorePrivate
->InSmm
= OldInSmm
;
524 return (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
528 Event notification that is fired when GUIDed Event Group is signaled.
530 @param Event The Event that is being processed, not used.
531 @param Context Event Context, not used.
536 SmmIplGuidedEventNotify (
541 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
545 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
547 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
548 CommunicateHeader
.MessageLength
= 1;
549 CommunicateHeader
.Data
[0] = 0;
552 // Generate the Software SMI and return the result
554 Size
= sizeof (CommunicateHeader
);
555 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
559 Event notification that is fired when DxeDispatch Event Group is signaled.
561 @param Event The Event that is being processed, not used.
562 @param Context Event Context, not used.
567 SmmIplDxeDispatchEventNotify (
572 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
577 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
581 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
582 // Clear the buffer passed into the Software SMI. This buffer will return
583 // the status of the SMM Core Dispatcher.
585 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
586 CommunicateHeader
.MessageLength
= 1;
587 CommunicateHeader
.Data
[0] = 0;
590 // Generate the Software SMI and return the result
592 Size
= sizeof (CommunicateHeader
);
593 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
596 // Return if there is no request to restart the SMM Core Dispatcher
598 if (CommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
603 // Attempt to reset SMRAM cacheability to UC
604 // Assume CPU AP is available at this time
606 Status
= gDS
->SetMemorySpaceAttributes(
611 if (EFI_ERROR (Status
)) {
612 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
616 // Close all SMRAM ranges to protect SMRAM
618 Status
= mSmmAccess
->Close (mSmmAccess
);
619 ASSERT_EFI_ERROR (Status
);
622 // Print debug message that the SMRAM window is now closed.
624 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
629 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
631 @param Event The Event that is being processed, not used.
632 @param Context Event Context, not used.
637 SmmIplSmmConfigurationEventNotify (
643 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
646 // Make sure this notification is for this handler
648 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
649 if (EFI_ERROR (Status
)) {
654 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
656 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
657 ASSERT_EFI_ERROR (Status
);
660 // Set flag to indicate that the SMM Entry Point has been registered which
661 // means that SMIs are now fully operational.
663 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
666 // Print debug message showing SMM Core entry point address.
668 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
672 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
673 or if gEfiEventReadyToBootGuid is signaled.
675 @param Event The Event that is being processed, not used.
676 @param Context Event Context, not used.
681 SmmIplReadyToLockEventNotify (
691 // See if we are already locked
698 // Make sure this notification is for this handler
700 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
701 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
702 if (EFI_ERROR (Status
)) {
707 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
708 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
709 // Print a warning on debug builds.
711 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
715 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
717 mSmmAccess
->Lock (mSmmAccess
);
720 // Close protocol and event notification events that do not apply after the
721 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
722 // event has been signalled.
724 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
725 if (mSmmIplEvents
[Index
].CloseOnLock
) {
726 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
731 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
733 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
736 // Print debug message that the SMRAM window is now locked.
738 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
741 // Set flag so this operation will not be performed again
747 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
749 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
750 It convers pointer to new virtual address.
752 @param Event Event whose notification function is being invoked.
753 @param Context Pointer to the notification function's context.
758 SmmIplSetVirtualAddressNotify (
763 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
767 Get the fixed loadding address from image header assigned by build tool. This function only be called
768 when Loading module at Fixed address feature enabled.
770 @param ImageContext Pointer to the image context structure that describes the PE/COFF
771 image that needs to be examined by this function.
772 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
773 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
776 GetPeCoffImageFixLoadingAssignedAddress(
777 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
780 UINTN SectionHeaderOffset
;
782 EFI_IMAGE_SECTION_HEADER SectionHeader
;
783 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
784 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
787 UINT16 NumberOfSections
;
788 EFI_PHYSICAL_ADDRESS SmramBase
;
790 UINT64 ValueInSectionHeader
;
792 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
794 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
796 FixLoaddingAddress
= 0;
797 Status
= EFI_NOT_FOUND
;
798 SmramBase
= mCurrentSmramRange
->CpuStart
;
800 // Get PeHeader pointer
802 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
803 SectionHeaderOffset
= (UINTN
)(
804 ImageContext
->PeCoffHeaderOffset
+
806 sizeof (EFI_IMAGE_FILE_HEADER
) +
807 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
809 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
812 // Get base address from the first section header that doesn't point to code section.
814 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
816 // Read section header from file
818 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
819 Status
= ImageContext
->ImageRead (
820 ImageContext
->Handle
,
825 if (EFI_ERROR (Status
)) {
829 Status
= EFI_NOT_FOUND
;
831 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
833 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
834 // first section header that doesn't point to code section in image header. And there is an assumption that when the
835 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
836 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero
838 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
839 if (ValueInSectionHeader
!= 0) {
841 // Found first section header that doesn't point to code section in which uild tool saves the
842 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
844 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
846 if (SmramBase
+ SmmCodeSize
> FixLoaddingAddress
&& SmramBase
<= FixLoaddingAddress
) {
848 // The assigned address is valid. Return the specified loadding address
850 ImageContext
->ImageAddress
= FixLoaddingAddress
;
851 Status
= EFI_SUCCESS
;
856 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
858 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress
, Status
));
862 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
864 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
865 currently executing image.
866 @param[in] Context Context to pass into SMM Core
872 ExecuteSmmCoreFromSmram (
873 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
880 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
882 EFI_PHYSICAL_ADDRESS DestinationBuffer
;
883 EFI_IMAGE_ENTRY_POINT EntryPoint
;
886 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
888 Status
= GetSectionFromAnyFvByFileType (
889 EFI_FV_FILETYPE_SMM_CORE
,
896 if (EFI_ERROR (Status
)) {
901 // Initilize ImageContext
903 ImageContext
.Handle
= SourceBuffer
;
904 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
907 // Get information about the image being loaded
909 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
910 if (EFI_ERROR (Status
)) {
914 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
915 // the address assigned by build tool.
917 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
919 // Get the fixed loading address assigned by Build tool
921 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
922 if (!EFI_ERROR (Status
)) {
924 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
928 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
930 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
931 // specified by SmramRange
933 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
935 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
936 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
938 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
939 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
942 // Align buffer on section boundry
944 ImageContext
.ImageAddress
= DestinationBuffer
;
948 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
949 // specified by SmramRange
951 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
953 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
954 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
956 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
957 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
960 // Align buffer on section boundry
962 ImageContext
.ImageAddress
= DestinationBuffer
;
965 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
966 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)(ImageContext
.SectionAlignment
- 1));
969 // Print debug message showing SMM Core load address.
971 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
974 // Load the image to our new buffer
976 Status
= PeCoffLoaderLoadImage (&ImageContext
);
977 if (!EFI_ERROR (Status
)) {
979 // Relocate the image in our new buffer
981 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
982 if (!EFI_ERROR (Status
)) {
984 // Flush the instruction cache so the image data are written before we execute it
986 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
989 // Print debug message showing SMM Core entry point address.
991 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
993 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
994 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
995 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
996 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
998 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1003 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1004 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1009 // If the load operation, relocate operation, or the image execution return an
1010 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
1013 if (EFI_ERROR (Status
)) {
1014 SmramRange
->PhysicalSize
+= EFI_PAGES_TO_SIZE (PageCount
);
1018 // Always free memory allocted by GetFileBufferByFilePath ()
1020 FreePool (SourceBuffer
);
1026 The Entry Point for SMM IPL
1028 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1029 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1030 critical events required to coordinate between DXE and SMM environments.
1032 @param ImageHandle The firmware allocated handle for the EFI image.
1033 @param SystemTable A pointer to the EFI System Table.
1035 @retval EFI_SUCCESS The entry point is executed successfully.
1036 @retval Other Some error occurred when executing this entry point.
1042 IN EFI_HANDLE ImageHandle
,
1043 IN EFI_SYSTEM_TABLE
*SystemTable
1047 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1050 EFI_SMM_RESERVED_SMRAM_REGION
*SmramResRegion
;
1054 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
1055 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1056 EFI_STATUS SetAttrStatus
;
1059 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1060 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1063 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1066 // Get SMM Access Protocol
1068 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1069 ASSERT_EFI_ERROR (Status
);
1072 // Get SMM Control2 Protocol
1074 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1075 ASSERT_EFI_ERROR (Status
);
1078 // Get SMM Configuration Protocol if it is present
1080 SmmConfiguration
= NULL
;
1081 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1084 // Get SMRAM information
1087 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1088 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1090 gSmmCorePrivate
->SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1091 ASSERT (gSmmCorePrivate
->SmramRanges
!= NULL
);
1093 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, gSmmCorePrivate
->SmramRanges
);
1094 ASSERT_EFI_ERROR (Status
);
1096 gSmmCorePrivate
->SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1101 gSmmCorePrivate
->FullSmramRangeCount
= gSmmCorePrivate
->SmramRangeCount
;
1102 gSmmCorePrivate
->FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1103 ASSERT (gSmmCorePrivate
->FullSmramRanges
!= NULL
);
1104 CopyMem (gSmmCorePrivate
->FullSmramRanges
, gSmmCorePrivate
->SmramRanges
, Size
);
1107 // Open all SMRAM ranges
1109 Status
= mSmmAccess
->Open (mSmmAccess
);
1110 ASSERT_EFI_ERROR (Status
);
1113 // Print debug message that the SMRAM window is now open.
1115 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1118 // Subtract SMRAM any reserved SMRAM regions.
1120 if (SmmConfiguration
!= NULL
) {
1121 SmramResRegion
= SmmConfiguration
->SmramReservedRegions
;
1122 while (SmramResRegion
->SmramReservedSize
!= 0) {
1123 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1124 if ((SmramResRegion
->SmramReservedStart
>= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
) && \
1125 ((SmramResRegion
->SmramReservedStart
+ SmramResRegion
->SmramReservedSize
) <= \
1126 (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
))) {
1128 // This range has reserved area, calculate the left free size
1130 gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
= SmramResRegion
->SmramReservedStart
- gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
1138 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1140 mCurrentSmramRange
= NULL
;
1141 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1143 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1145 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1149 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1150 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
1151 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1152 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1153 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1159 if (mCurrentSmramRange
!= NULL
) {
1161 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1163 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1164 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1165 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1168 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1170 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1171 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1172 // is not available here.
1175 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1176 if (!EFI_ERROR (Status
)) {
1177 Status
= gDS
->SetMemorySpaceAttributes(
1182 if (EFI_ERROR (Status
)) {
1183 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1187 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1188 // Modules At Fixed Address Configuration Table.
1190 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1192 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1194 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1196 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1198 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1200 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1202 Status
= EfiGetSystemConfigurationTable (
1203 &gLoadFixedAddressConfigurationTableGuid
,
1204 (VOID
**) &LMFAConfigurationTable
1206 if (!EFI_ERROR (Status
) && LMFAConfigurationTable
!= NULL
) {
1207 LMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1209 // Print the SMRAM base
1211 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable
->SmramBase
));
1215 // Load SMM Core into SMRAM and execute it from SMRAM
1217 Status
= ExecuteSmmCoreFromSmram (mCurrentSmramRange
, gSmmCorePrivate
);
1218 if (EFI_ERROR (Status
)) {
1220 // Print error message that the SMM Core failed to be loaded and executed.
1222 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1225 // Attempt to reset SMRAM cacheability to UC
1227 if (CpuArch
!= NULL
) {
1228 SetAttrStatus
= gDS
->SetMemorySpaceAttributes(
1233 if (EFI_ERROR (SetAttrStatus
)) {
1234 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1240 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1242 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1246 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1247 // resources, and return an error so SMM IPL will be unloaded.
1249 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1251 // Close all SMRAM ranges
1253 Status
= mSmmAccess
->Close (mSmmAccess
);
1254 ASSERT_EFI_ERROR (Status
);
1257 // Print debug message that the SMRAM window is now closed.
1259 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1262 // Free all allocated resources
1264 FreePool (gSmmCorePrivate
->SmramRanges
);
1265 FreePool (gSmmCorePrivate
->FullSmramRanges
);
1267 return EFI_UNSUPPORTED
;
1271 // Install SMM Base2 Protocol and SMM Communication Protocol
1273 Status
= gBS
->InstallMultipleProtocolInterfaces (
1275 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1276 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1279 ASSERT_EFI_ERROR (Status
);
1282 // Create the set of protocol and event notififcations that the SMM IPL requires
1284 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1285 if (mSmmIplEvents
[Index
].Protocol
) {
1286 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1287 mSmmIplEvents
[Index
].Guid
,
1288 mSmmIplEvents
[Index
].NotifyTpl
,
1289 mSmmIplEvents
[Index
].NotifyFunction
,
1290 mSmmIplEvents
[Index
].NotifyContext
,
1294 Status
= gBS
->CreateEventEx (
1296 mSmmIplEvents
[Index
].NotifyTpl
,
1297 mSmmIplEvents
[Index
].NotifyFunction
,
1298 mSmmIplEvents
[Index
].NotifyContext
,
1299 mSmmIplEvents
[Index
].Guid
,
1300 &mSmmIplEvents
[Index
].Event
1302 ASSERT_EFI_ERROR (Status
);