2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2010, 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 SmmIplGuidedEventNotify (
152 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
154 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
155 It convers pointer to new virtual address.
157 @param Event Event whose notification function is being invoked.
158 @param Context Pointer to the notification function's context.
163 SmmIplSetVirtualAddressNotify (
169 // Data structure used to declare a table of protocol notifications and event
170 // notifications required by the SMM IPL
176 EFI_EVENT_NOTIFY NotifyFunction
;
179 } SMM_IPL_EVENT_NOTIFICATION
;
182 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
184 EFI_HANDLE mSmmIplHandle
= NULL
;
187 // SMM Base 2 Protocol instance
189 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
191 SmmBase2GetSmstLocation
195 // SMM Communication Protocol instance
197 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
198 SmmCommunicationCommunicate
202 // SMM Core Private Data structure that contains the data shared between
203 // the SMM IPL and the SMM Core.
205 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
206 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
207 NULL
, // SmmIplImageHandle
208 0, // SmramRangeCount
210 NULL
, // SmmEntryPoint
211 FALSE
, // SmmEntryPointRegistered
214 NULL
, // CommunicationBuffer
216 EFI_SUCCESS
// ReturnStatus
220 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
222 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
225 // SMM IPL global variables
227 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
228 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
229 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
230 BOOLEAN mSmmLocked
= FALSE
;
231 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
232 UINT64 mSmramCacheSize
;
235 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
237 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
239 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
240 // the associated event is immediately signalled, so the notification function will be executed and the
241 // SMM Configuration Protocol will be found if it is already in the handle database.
243 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, NULL
},
245 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
246 // the associated event is immediately signalled, so the notification function will be executed and the
247 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
249 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, NULL
},
251 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
252 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
253 // if notified, so the SMM Core can dispatch SMM drivers.
255 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplGuidedEventNotify
, &gEfiEventDxeDispatchGuid
, NULL
},
257 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
258 // used to make sure SMRAM is locked before any boot options are processed.
260 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, NULL
},
262 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
263 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
264 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
266 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, NULL
},
268 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
269 // and mSmmControl2 from physical addresses to virtual addresses.
271 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, NULL
},
273 // Terminate the table of event notifications
275 { FALSE
, FALSE
, NULL
, NULL
, NULL
, NULL
}
279 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
281 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
283 @param SmramRange The SMRAM range to search from.
284 @param SmramCacheBase The returned cache range base.
285 @param SmramCacheSize The returned cache range size.
290 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
291 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
292 OUT UINT64
*SmramCacheSize
296 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
297 UINT64 RangePhysicalSize
;
298 BOOLEAN FoundAjacentRange
;
300 *SmramCacheBase
= SmramRange
->CpuStart
;
301 *SmramCacheSize
= SmramRange
->PhysicalSize
;
304 FoundAjacentRange
= FALSE
;
305 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
306 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
307 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
308 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
309 *SmramCacheBase
= RangeCpuStart
;
310 *SmramCacheSize
+= RangePhysicalSize
;
311 FoundAjacentRange
= TRUE
;
312 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
313 *SmramCacheSize
+= RangePhysicalSize
;
314 FoundAjacentRange
= TRUE
;
317 } while (FoundAjacentRange
);
322 Indicate whether the driver is currently executing in the SMM Initialization phase.
324 @param This The EFI_SMM_BASE2_PROTOCOL instance.
325 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
326 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
328 @retval EFI_INVALID_PARAMETER InSmram was NULL.
329 @retval EFI_SUCCESS The call returned successfully.
335 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
339 if (InSmram
== NULL
) {
340 return EFI_INVALID_PARAMETER
;
343 *InSmram
= gSmmCorePrivate
->InSmm
;
349 Retrieves the location of the System Management System Table (SMST).
351 @param This The EFI_SMM_BASE2_PROTOCOL instance.
352 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
354 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
355 @retval EFI_SUCCESS The memory was returned to the system.
356 @retval EFI_UNSUPPORTED Not in SMM.
361 SmmBase2GetSmstLocation (
362 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
363 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
366 if ((This
== NULL
) ||(Smst
== NULL
)) {
367 return EFI_INVALID_PARAMETER
;
370 if (!gSmmCorePrivate
->InSmm
) {
371 return EFI_UNSUPPORTED
;
374 *Smst
= gSmmCorePrivate
->Smst
;
380 Communicates with a registered handler.
382 This function provides a service to send and receive messages from a registered
383 UEFI service. This function is part of the SMM Communication Protocol that may
384 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
385 after SetVirtualAddressMap().
387 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
388 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
389 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
390 being returned. Zero if the handler does not wish to reply with any data.
392 @retval EFI_SUCCESS The message was successfully posted.
393 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
397 SmmCommunicationCommunicate (
398 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
399 IN OUT VOID
*CommBuffer
,
400 IN OUT UINTN
*CommSize
404 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
410 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
411 return EFI_INVALID_PARAMETER
;
415 // CommSize must hold HeaderGuid and MessageLength
417 if (*CommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
418 return EFI_INVALID_PARAMETER
;
422 // If not already in SMM, then generate a Software SMI
424 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
426 // Put arguments for Software SMI in gSmmCorePrivate
428 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
429 gSmmCorePrivate
->BufferSize
= *CommSize
;
432 // Generate Software SMI
434 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
435 if (EFI_ERROR (Status
)) {
436 return EFI_UNSUPPORTED
;
440 // Return status from software SMI
442 *CommSize
= gSmmCorePrivate
->BufferSize
;
443 return gSmmCorePrivate
->ReturnStatus
;
447 // If we are in SMM, then the execution mode must be physical, which means that
448 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
449 // has been called, then a direct invocation of the Software SMI is not
450 // not allowed so return EFI_INVALID_PARAMETER.
452 if (EfiGoneVirtual()) {
453 return EFI_INVALID_PARAMETER
;
457 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
459 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
460 return EFI_INVALID_PARAMETER
;
464 // Save current InSmm state and set InSmm state to TRUE
466 OldInSmm
= gSmmCorePrivate
->InSmm
;
467 gSmmCorePrivate
->InSmm
= TRUE
;
470 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
472 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
473 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
474 Status
= gSmmCorePrivate
->Smst
->SmiManage (
475 &CommunicateHeader
->HeaderGuid
,
477 CommunicateHeader
->Data
,
482 // Update CommunicationBuffer, BufferSize and ReturnStatus
483 // Communicate service finished, reset the pointer to CommBuffer to NULL
485 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
488 // Restore original InSmm state
490 gSmmCorePrivate
->InSmm
= OldInSmm
;
492 return (Status
== EFI_WARN_INTERRUPT_SOURCE_QUIESCED
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
496 Event notification that is fired when DxeDispatch Event Group is signaled.
498 @param Event The Event that is being processed, not used.
499 @param Context Event Context, not used.
504 SmmIplGuidedEventNotify (
509 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
513 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
515 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
516 CommunicateHeader
.MessageLength
= 1;
517 CommunicateHeader
.Data
[0] = 0;
520 // Generate the Software SMI and return the result
522 Size
= sizeof (CommunicateHeader
);
523 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
527 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
529 @param Event The Event that is being processed, not used.
530 @param Context Event Context, not used.
535 SmmIplSmmConfigurationEventNotify (
541 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
544 // Make sure this notification is for this handler
546 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
547 if (EFI_ERROR (Status
)) {
552 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
554 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
555 ASSERT_EFI_ERROR (Status
);
558 // Set flag to indicate that the SM< Entry Point has been registered which
559 // means that SMIs are now fully operational.
561 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
564 // Print debug message showing SMM Core entry point address.
566 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
569 // Attempt to reset SMRAM cacheability to UC
570 // Assume CPU AP is available at this time
572 Status
= gDS
->SetMemorySpaceAttributes(
577 if (EFI_ERROR (Status
)) {
578 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
582 // Close all SMRAM ranges to protect SMRAM
584 Status
= mSmmAccess
->Close (mSmmAccess
);
585 ASSERT_EFI_ERROR (Status
);
588 // Print debug message that the SMRAM window is now closed.
590 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
594 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
595 or if gEfiEventReadyToBootGuid is signalled.
597 @param Event The Event that is being processed, not used.
598 @param Context Event Context, not used.
603 SmmIplReadyToLockEventNotify (
613 // See if we are already locked
620 // Make sure this notification is for this handler
622 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
623 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
624 if (EFI_ERROR (Status
)) {
629 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
630 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
631 // Print a warning on debug builds.
633 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
637 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
639 mSmmAccess
->Lock (mSmmAccess
);
642 // Close protocol and event notification events that do not apply after the
643 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
644 // event has been signalled.
646 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
647 if (mSmmIplEvents
[Index
].CloseOnLock
) {
648 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
653 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
655 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
658 // Print debug message that the SMRAM window is now locked.
660 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
663 // Set flag so this operation will not be performed again
669 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
671 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
672 It convers pointer to new virtual address.
674 @param Event Event whose notification function is being invoked.
675 @param Context Pointer to the notification function's context.
680 SmmIplSetVirtualAddressNotify (
685 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
689 Get the fixed loadding address from image header assigned by build tool. This function only be called
690 when Loading module at Fixed address feature enabled.
692 @param ImageContext Pointer to the image context structure that describes the PE/COFF
693 image that needs to be examined by this function.
694 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
695 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
698 GetPeCoffImageFixLoadingAssignedAddress(
699 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
702 UINTN SectionHeaderOffset
;
704 EFI_IMAGE_SECTION_HEADER SectionHeader
;
705 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
706 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
709 UINT16 NumberOfSections
;
710 EFI_PHYSICAL_ADDRESS SmramBase
;
712 UINT64 ValueInSectionHeader
;
714 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
716 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
718 FixLoaddingAddress
= 0;
719 Status
= EFI_NOT_FOUND
;
720 SmramBase
= mCurrentSmramRange
->CpuStart
;
722 // Get PeHeader pointer
724 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
725 SectionHeaderOffset
= (UINTN
)(
726 ImageContext
->PeCoffHeaderOffset
+
728 sizeof (EFI_IMAGE_FILE_HEADER
) +
729 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
731 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
734 // Get base address from the first section header that doesn't point to code section.
736 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
738 // Read section header from file
740 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
741 Status
= ImageContext
->ImageRead (
742 ImageContext
->Handle
,
747 if (EFI_ERROR (Status
)) {
751 Status
= EFI_NOT_FOUND
;
753 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
755 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
756 // first section header that doesn't point to code section in image header. And there is an assumption that when the
757 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
758 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero
760 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
761 if (ValueInSectionHeader
!= 0) {
763 // Found first section header that doesn't point to code section in which uild tool saves the
764 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
766 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
768 if (SmramBase
+ SmmCodeSize
> FixLoaddingAddress
&& SmramBase
<= FixLoaddingAddress
) {
770 // The assigned address is valid. Return the specified loadding address
772 ImageContext
->ImageAddress
= FixLoaddingAddress
;
773 Status
= EFI_SUCCESS
;
778 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
780 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress
, Status
));
784 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
786 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
787 currently executing image.
788 @param[in] Context Context to pass into SMM Core
794 ExecuteSmmCoreFromSmram (
795 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
802 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
804 EFI_PHYSICAL_ADDRESS DestinationBuffer
;
805 EFI_IMAGE_ENTRY_POINT EntryPoint
;
808 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
810 Status
= GetSectionFromAnyFvByFileType (
811 EFI_FV_FILETYPE_SMM_CORE
,
818 if (EFI_ERROR (Status
)) {
823 // Initilize ImageContext
825 ImageContext
.Handle
= SourceBuffer
;
826 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
829 // Get information about the image being loaded
831 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
832 if (EFI_ERROR (Status
)) {
836 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
837 // the address assigned by build tool.
839 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
841 // Get the fixed loading address assigned by Build tool
843 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
844 if (!EFI_ERROR (Status
)) {
846 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
850 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
852 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
853 // specified by SmramRange
855 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
857 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
858 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
860 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
861 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
864 // Align buffer on section boundry
866 ImageContext
.ImageAddress
= DestinationBuffer
;
870 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
871 // specified by SmramRange
873 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
875 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
876 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
878 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
879 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
882 // Align buffer on section boundry
884 ImageContext
.ImageAddress
= DestinationBuffer
;
887 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
888 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
891 // Print debug message showing SMM Core load address.
893 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
896 // Load the image to our new buffer
898 Status
= PeCoffLoaderLoadImage (&ImageContext
);
899 if (!EFI_ERROR (Status
)) {
901 // Relocate the image in our new buffer
903 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
904 if (!EFI_ERROR (Status
)) {
906 // Flush the instruction cache so the image data are written before we execute it
908 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
911 // Print debug message showing SMM Core entry point address.
913 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
918 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
919 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
924 // If the load operation, relocate operation, or the image execution return an
925 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
928 if (EFI_ERROR (Status
)) {
929 SmramRange
->PhysicalSize
+= EFI_PAGES_TO_SIZE (PageCount
);
933 // Always free memory allocted by GetFileBufferByFilePath ()
935 FreePool (SourceBuffer
);
941 The Entry Point for SMM IPL
943 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
944 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
945 critical events required to coordinate between DXE and SMM environments.
947 @param ImageHandle The firmware allocated handle for the EFI image.
948 @param SystemTable A pointer to the EFI System Table.
950 @retval EFI_SUCCESS The entry point is executed successfully.
951 @retval Other Some error occurred when executing this entry point.
957 IN EFI_HANDLE ImageHandle
,
958 IN EFI_SYSTEM_TABLE
*SystemTable
962 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
965 EFI_SMM_RESERVED_SMRAM_REGION
*SmramResRegion
;
969 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
970 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
973 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
974 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
977 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
980 // Get SMM Access Protocol
982 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
983 ASSERT_EFI_ERROR (Status
);
986 // Get SMM Control2 Protocol
988 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
989 ASSERT_EFI_ERROR (Status
);
992 // Get SMM Configuration Protocol if it is present
994 SmmConfiguration
= NULL
;
995 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
998 // Get SMRAM information
1001 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1002 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1004 gSmmCorePrivate
->SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1005 ASSERT (gSmmCorePrivate
->SmramRanges
!= NULL
);
1007 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, gSmmCorePrivate
->SmramRanges
);
1008 ASSERT_EFI_ERROR (Status
);
1010 gSmmCorePrivate
->SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1013 // Open all SMRAM ranges
1015 Status
= mSmmAccess
->Open (mSmmAccess
);
1016 ASSERT_EFI_ERROR (Status
);
1019 // Print debug message that the SMRAM window is now open.
1021 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1024 // Subtract SMRAM any reserved SMRAM regions.
1026 if (SmmConfiguration
!= NULL
) {
1027 SmramResRegion
= SmmConfiguration
->SmramReservedRegions
;
1028 while (SmramResRegion
->SmramReservedSize
!= 0) {
1029 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1030 if ((SmramResRegion
->SmramReservedStart
>= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
) && \
1031 ((SmramResRegion
->SmramReservedStart
+ SmramResRegion
->SmramReservedSize
) <= \
1032 (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
))) {
1034 // This range has reserved area, calculate the left free size
1036 gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
= SmramResRegion
->SmramReservedStart
- gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
1044 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1046 mCurrentSmramRange
= NULL
;
1047 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1048 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1049 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
1050 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1051 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1052 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1058 if (mCurrentSmramRange
!= NULL
) {
1060 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1062 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1063 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1064 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1067 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1069 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1070 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1071 // is not available here.
1074 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1075 if (!EFI_ERROR (Status
)) {
1076 Status
= gDS
->SetMemorySpaceAttributes(
1081 if (EFI_ERROR (Status
)) {
1082 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1086 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1087 // Modules At Fixed Address Configuration Table.
1089 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1091 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1093 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1095 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1097 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1099 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1101 Status
= EfiGetSystemConfigurationTable (
1102 &gLoadFixedAddressConfigurationTableGuid
,
1103 (VOID
**) &LMFAConfigurationTable
1105 if (!EFI_ERROR (Status
) && LMFAConfigurationTable
!= NULL
) {
1106 LMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1108 // Print the SMRAM base
1110 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable
->SmramBase
));
1114 // Load SMM Core into SMRAM and execute it from SMRAM
1116 Status
= ExecuteSmmCoreFromSmram (mCurrentSmramRange
, gSmmCorePrivate
);
1117 if (EFI_ERROR (Status
)) {
1119 // Print error message that the SMM Core failed to be loaded and executed.
1121 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1124 // Attempt to reset SMRAM cacheability to UC
1126 if (CpuArch
!= NULL
) {
1127 Status
= gDS
->SetMemorySpaceAttributes(
1132 if (EFI_ERROR (Status
)) {
1133 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1139 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1141 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1145 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1146 // resources, and return an error so SMM IPL will be unloaded.
1148 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1150 // Close all SMRAM ranges
1152 Status
= mSmmAccess
->Close (mSmmAccess
);
1153 ASSERT_EFI_ERROR (Status
);
1156 // Print debug message that the SMRAM window is now closed.
1158 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1161 // Free all allocated resources
1163 FreePool (gSmmCorePrivate
->SmramRanges
);
1165 return EFI_UNSUPPORTED
;
1169 // Install SMM Base2 Protocol and SMM Communication Protocol
1171 Status
= gBS
->InstallMultipleProtocolInterfaces (
1173 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1174 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1177 ASSERT_EFI_ERROR (Status
);
1180 // Create the set of protocol and event notififcations that the SMM IPL requires
1182 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1183 if (mSmmIplEvents
[Index
].Protocol
) {
1184 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1185 mSmmIplEvents
[Index
].Guid
,
1187 mSmmIplEvents
[Index
].NotifyFunction
,
1188 mSmmIplEvents
[Index
].NotifyContext
,
1192 Status
= gBS
->CreateEventEx (
1195 mSmmIplEvents
[Index
].NotifyFunction
,
1196 mSmmIplEvents
[Index
].NotifyContext
,
1197 mSmmIplEvents
[Index
].Guid
,
1198 &mSmmIplEvents
[Index
].Event
1200 ASSERT_EFI_ERROR (Status
);