2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 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"
45 #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)
47 #define MEMORY_CACHE_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \
48 EFI_MEMORY_WT | EFI_MEMORY_WB | \
49 EFI_MEMORY_WP | EFI_MEMORY_UCE)
51 #define MEMORY_PAGE_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | EFI_MEMORY_RO)
54 // Function prototypes from produced protocols
58 Indicate whether the driver is currently executing in the SMM Initialization phase.
60 @param This The EFI_SMM_BASE2_PROTOCOL instance.
61 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
62 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
64 @retval EFI_INVALID_PARAMETER InSmram was NULL.
65 @retval EFI_SUCCESS The call returned successfully.
71 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
76 Retrieves the location of the System Management System Table (SMST).
78 @param This The EFI_SMM_BASE2_PROTOCOL instance.
79 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
81 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
82 @retval EFI_SUCCESS The memory was returned to the system.
83 @retval EFI_UNSUPPORTED Not in SMM.
88 SmmBase2GetSmstLocation (
89 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
90 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
94 Communicates with a registered handler.
96 This function provides a service to send and receive messages from a registered
97 UEFI service. This function is part of the SMM Communication Protocol that may
98 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
99 after SetVirtualAddressMap().
101 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
102 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
103 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
104 being returned. Zero if the handler does not wish to reply with any data.
105 This parameter is optional and may be NULL.
107 @retval EFI_SUCCESS The message was successfully posted.
108 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
109 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
110 If this error is returned, the MessageLength field
111 in the CommBuffer header or the integer pointed by
112 CommSize, are updated to reflect the maximum payload
113 size the implementation can accommodate.
114 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
115 if not omitted, are in address range that cannot be
116 accessed by the MM environment.
121 SmmCommunicationCommunicate (
122 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
123 IN OUT VOID
*CommBuffer
,
124 IN OUT UINTN
*CommSize OPTIONAL
128 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
130 @param Event The Event that is being processed, not used.
131 @param Context Event Context, not used.
136 SmmIplSmmConfigurationEventNotify (
142 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
143 or if gEfiEventReadyToBootGuid is signalled.
145 @param Event The Event that is being processed, not used.
146 @param Context Event Context, not used.
151 SmmIplReadyToLockEventNotify (
157 Event notification that is fired when DxeDispatch Event Group is signaled.
159 @param Event The Event that is being processed, not used.
160 @param Context Event Context, not used.
165 SmmIplDxeDispatchEventNotify (
171 Event notification that is fired when a GUIDed Event Group is signaled.
173 @param Event The Event that is being processed, not used.
174 @param Context Event Context, not used.
179 SmmIplGuidedEventNotify (
185 Event notification that is fired when EndOfDxe Event Group is signaled.
187 @param Event The Event that is being processed, not used.
188 @param Context Event Context, not used.
193 SmmIplEndOfDxeEventNotify (
199 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
201 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
202 It convers pointer to new virtual address.
204 @param Event Event whose notification function is being invoked.
205 @param Context Pointer to the notification function's context.
210 SmmIplSetVirtualAddressNotify (
216 // Data structure used to declare a table of protocol notifications and event
217 // notifications required by the SMM IPL
223 EFI_EVENT_NOTIFY NotifyFunction
;
227 } SMM_IPL_EVENT_NOTIFICATION
;
230 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
232 EFI_HANDLE mSmmIplHandle
= NULL
;
235 // SMM Base 2 Protocol instance
237 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
239 SmmBase2GetSmstLocation
243 // SMM Communication Protocol instance
245 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
246 SmmCommunicationCommunicate
250 // SMM Core Private Data structure that contains the data shared between
251 // the SMM IPL and the SMM Core.
253 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
254 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
255 NULL
, // SmmIplImageHandle
256 0, // SmramRangeCount
258 NULL
, // SmmEntryPoint
259 FALSE
, // SmmEntryPointRegistered
262 NULL
, // CommunicationBuffer
264 EFI_SUCCESS
// ReturnStatus
268 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
270 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
273 // SMM IPL global variables
275 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
276 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
277 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
278 BOOLEAN mSmmLocked
= FALSE
;
279 BOOLEAN mEndOfDxe
= FALSE
;
280 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
281 UINT64 mSmramCacheSize
;
283 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader
;
284 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*mLMFAConfigurationTable
= NULL
;
287 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
289 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
291 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
292 // the associated event is immediately signalled, so the notification function will be executed and the
293 // SMM Configuration Protocol will be found if it is already in the handle database.
295 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, TPL_NOTIFY
, NULL
},
297 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
298 // the associated event is immediately signalled, so the notification function will be executed and the
299 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
301 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, TPL_CALLBACK
, NULL
},
303 // Declare event notification on EndOfDxe event. When this notification is established,
304 // the associated event is immediately signalled, so the notification function will be executed and the
305 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
307 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplGuidedEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
309 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
311 { FALSE
, TRUE
, &gEfiEndOfDxeEventGroupGuid
, SmmIplEndOfDxeEventNotify
, &gEfiEndOfDxeEventGroupGuid
, TPL_CALLBACK
, NULL
},
313 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
314 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
315 // if notified, so the SMM Core can dispatch SMM drivers.
317 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplDxeDispatchEventNotify
, &gEfiEventDxeDispatchGuid
, TPL_CALLBACK
, NULL
},
319 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
320 // used to make sure SMRAM is locked before any boot options are processed.
322 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
324 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
325 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
326 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
327 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
329 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, TPL_CALLBACK
, NULL
},
331 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
332 // to notify SMM driver that system enter exit boot services.
334 { FALSE
, FALSE
, &gEfiEventExitBootServicesGuid
, SmmIplGuidedEventNotify
, &gEfiEventExitBootServicesGuid
, TPL_CALLBACK
, NULL
},
336 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
337 // to notify SMM driver that system enter ready to boot.
339 { FALSE
, FALSE
, &gEfiEventReadyToBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventReadyToBootGuid
, TPL_CALLBACK
, NULL
},
341 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
342 // and mSmmControl2 from physical addresses to virtual addresses.
344 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, TPL_CALLBACK
, NULL
},
346 // Terminate the table of event notifications
348 { FALSE
, FALSE
, NULL
, NULL
, NULL
, TPL_CALLBACK
, NULL
}
352 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
354 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
356 @param SmramRange The SMRAM range to search from.
357 @param SmramCacheBase The returned cache range base.
358 @param SmramCacheSize The returned cache range size.
363 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
364 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
365 OUT UINT64
*SmramCacheSize
369 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
370 UINT64 RangePhysicalSize
;
371 BOOLEAN FoundAjacentRange
;
373 *SmramCacheBase
= SmramRange
->CpuStart
;
374 *SmramCacheSize
= SmramRange
->PhysicalSize
;
377 FoundAjacentRange
= FALSE
;
378 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
379 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
380 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
381 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
382 *SmramCacheBase
= RangeCpuStart
;
383 *SmramCacheSize
+= RangePhysicalSize
;
384 FoundAjacentRange
= TRUE
;
385 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
386 *SmramCacheSize
+= RangePhysicalSize
;
387 FoundAjacentRange
= TRUE
;
390 } while (FoundAjacentRange
);
395 Indicate whether the driver is currently executing in the SMM Initialization phase.
397 @param This The EFI_SMM_BASE2_PROTOCOL instance.
398 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
399 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
401 @retval EFI_INVALID_PARAMETER InSmram was NULL.
402 @retval EFI_SUCCESS The call returned successfully.
408 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
412 if (InSmram
== NULL
) {
413 return EFI_INVALID_PARAMETER
;
416 *InSmram
= gSmmCorePrivate
->InSmm
;
422 Retrieves the location of the System Management System Table (SMST).
424 @param This The EFI_SMM_BASE2_PROTOCOL instance.
425 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
427 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
428 @retval EFI_SUCCESS The memory was returned to the system.
429 @retval EFI_UNSUPPORTED Not in SMM.
434 SmmBase2GetSmstLocation (
435 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
436 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
439 if ((This
== NULL
) ||(Smst
== NULL
)) {
440 return EFI_INVALID_PARAMETER
;
443 if (!gSmmCorePrivate
->InSmm
) {
444 return EFI_UNSUPPORTED
;
447 *Smst
= gSmmCorePrivate
->Smst
;
453 Communicates with a registered handler.
455 This function provides a service to send and receive messages from a registered
456 UEFI service. This function is part of the SMM Communication Protocol that may
457 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
458 after SetVirtualAddressMap().
460 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
461 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
462 @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
463 being returned. Zero if the handler does not wish to reply with any data.
464 This parameter is optional and may be NULL.
466 @retval EFI_SUCCESS The message was successfully posted.
467 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
468 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
469 If this error is returned, the MessageLength field
470 in the CommBuffer header or the integer pointed by
471 CommSize, are updated to reflect the maximum payload
472 size the implementation can accommodate.
473 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
474 if not omitted, are in address range that cannot be
475 accessed by the MM environment.
480 SmmCommunicationCommunicate (
481 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
482 IN OUT VOID
*CommBuffer
,
483 IN OUT UINTN
*CommSize OPTIONAL
487 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
494 if (CommBuffer
== NULL
) {
495 return EFI_INVALID_PARAMETER
;
498 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
500 if (CommSize
== NULL
) {
501 TempCommSize
= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
) + CommunicateHeader
->MessageLength
;
503 TempCommSize
= *CommSize
;
505 // CommSize must hold HeaderGuid and MessageLength
507 if (TempCommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
508 return EFI_INVALID_PARAMETER
;
513 // If not already in SMM, then generate a Software SMI
515 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
517 // Put arguments for Software SMI in gSmmCorePrivate
519 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
520 gSmmCorePrivate
->BufferSize
= TempCommSize
;
523 // Generate Software SMI
525 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
526 if (EFI_ERROR (Status
)) {
527 return EFI_UNSUPPORTED
;
531 // Return status from software SMI
533 if (CommSize
!= NULL
) {
534 *CommSize
= gSmmCorePrivate
->BufferSize
;
536 return gSmmCorePrivate
->ReturnStatus
;
540 // If we are in SMM, then the execution mode must be physical, which means that
541 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
542 // has been called, then a direct invocation of the Software SMI is not allowed,
543 // so return EFI_INVALID_PARAMETER.
545 if (EfiGoneVirtual()) {
546 return EFI_INVALID_PARAMETER
;
550 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
552 if ((!gSmmCorePrivate
->InSmm
) && (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
)) {
553 return EFI_INVALID_PARAMETER
;
557 // Save current InSmm state and set InSmm state to TRUE
559 OldInSmm
= gSmmCorePrivate
->InSmm
;
560 gSmmCorePrivate
->InSmm
= TRUE
;
563 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
565 TempCommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
566 Status
= gSmmCorePrivate
->Smst
->SmiManage (
567 &CommunicateHeader
->HeaderGuid
,
569 CommunicateHeader
->Data
,
572 TempCommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
573 if (CommSize
!= NULL
) {
574 *CommSize
= TempCommSize
;
578 // Restore original InSmm state
580 gSmmCorePrivate
->InSmm
= OldInSmm
;
582 return (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
586 Event notification that is fired when GUIDed Event Group is signaled.
588 @param Event The Event that is being processed, not used.
589 @param Context Event Context, not used.
594 SmmIplGuidedEventNotify (
602 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
604 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
605 mCommunicateHeader
.MessageLength
= 1;
606 mCommunicateHeader
.Data
[0] = 0;
609 // Generate the Software SMI and return the result
611 Size
= sizeof (mCommunicateHeader
);
612 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
616 Event notification that is fired when EndOfDxe Event Group is signaled.
618 @param Event The Event that is being processed, not used.
619 @param Context Event Context, not used.
624 SmmIplEndOfDxeEventNotify (
633 Event notification that is fired when DxeDispatch Event Group is signaled.
635 @param Event The Event that is being processed, not used.
636 @param Context Event Context, not used.
641 SmmIplDxeDispatchEventNotify (
650 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
654 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
655 // Clear the buffer passed into the Software SMI. This buffer will return
656 // the status of the SMM Core Dispatcher.
658 CopyGuid (&mCommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
659 mCommunicateHeader
.MessageLength
= 1;
660 mCommunicateHeader
.Data
[0] = 0;
663 // Generate the Software SMI and return the result
665 Size
= sizeof (mCommunicateHeader
);
666 SmmCommunicationCommunicate (&mSmmCommunication
, &mCommunicateHeader
, &Size
);
669 // Return if there is no request to restart the SMM Core Dispatcher
671 if (mCommunicateHeader
.Data
[0] != COMM_BUFFER_SMM_DISPATCH_RESTART
) {
676 // Close all SMRAM ranges to protect SMRAM
677 // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
678 // so no need to reset the SMRAM to UC in MTRR.
680 Status
= mSmmAccess
->Close (mSmmAccess
);
681 ASSERT_EFI_ERROR (Status
);
684 // Print debug message that the SMRAM window is now closed.
686 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
691 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
693 @param Event The Event that is being processed, not used.
694 @param Context Event Context, not used.
699 SmmIplSmmConfigurationEventNotify (
705 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
708 // Make sure this notification is for this handler
710 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
711 if (EFI_ERROR (Status
)) {
716 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
718 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
719 ASSERT_EFI_ERROR (Status
);
722 // Set flag to indicate that the SMM Entry Point has been registered which
723 // means that SMIs are now fully operational.
725 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
728 // Print debug message showing SMM Core entry point address.
730 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
734 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
735 or if gEfiEventReadyToBootGuid is signaled.
737 @param Event The Event that is being processed, not used.
738 @param Context Event Context, not used.
743 SmmIplReadyToLockEventNotify (
753 // See if we are already locked
760 // Make sure this notification is for this handler
762 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
763 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
764 if (EFI_ERROR (Status
)) {
769 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
770 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
771 // Print a warning on debug builds.
773 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
777 DEBUG ((DEBUG_ERROR
, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
779 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
780 (EFI_SOFTWARE_SMM_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
786 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
788 mSmmAccess
->Lock (mSmmAccess
);
791 // Close protocol and event notification events that do not apply after the
792 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
793 // event has been signalled.
795 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
796 if (mSmmIplEvents
[Index
].CloseOnLock
) {
797 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
802 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
804 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
807 // Print debug message that the SMRAM window is now locked.
809 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
812 // Set flag so this operation will not be performed again
818 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
820 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
821 It convers pointer to new virtual address.
823 @param Event Event whose notification function is being invoked.
824 @param Context Pointer to the notification function's context.
829 SmmIplSetVirtualAddressNotify (
834 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
838 Get the fixed loading address from image header assigned by build tool. This function only be called
839 when Loading module at Fixed address feature enabled.
841 @param ImageContext Pointer to the image context structure that describes the PE/COFF
842 image that needs to be examined by this function.
843 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
844 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
847 GetPeCoffImageFixLoadingAssignedAddress(
848 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
851 UINTN SectionHeaderOffset
;
853 EFI_IMAGE_SECTION_HEADER SectionHeader
;
854 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
855 EFI_PHYSICAL_ADDRESS FixLoadingAddress
;
858 UINT16 NumberOfSections
;
859 EFI_PHYSICAL_ADDRESS SmramBase
;
861 UINT64 ValueInSectionHeader
;
863 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
865 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
867 FixLoadingAddress
= 0;
868 Status
= EFI_NOT_FOUND
;
869 SmramBase
= mLMFAConfigurationTable
->SmramBase
;
871 // Get PeHeader pointer
873 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
874 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
876 sizeof (EFI_IMAGE_FILE_HEADER
) +
877 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
;
878 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
881 // Get base address from the first section header that doesn't point to code section.
883 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
885 // Read section header from file
887 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
888 Status
= ImageContext
->ImageRead (
889 ImageContext
->Handle
,
894 if (EFI_ERROR (Status
)) {
898 Status
= EFI_NOT_FOUND
;
900 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
902 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
903 // first section header that doesn't point to code section in image header. And there is an assumption that when the
904 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
905 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
907 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
908 if (ValueInSectionHeader
!= 0) {
910 // Found first section header that doesn't point to code section in which build tool saves the
911 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
913 FixLoadingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
915 if (SmramBase
+ SmmCodeSize
> FixLoadingAddress
&& SmramBase
<= FixLoadingAddress
) {
917 // The assigned address is valid. Return the specified loading address
919 ImageContext
->ImageAddress
= FixLoadingAddress
;
920 Status
= EFI_SUCCESS
;
925 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
927 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress
, Status
));
931 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
933 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
934 currently executing image, the rang of SMRAM to
935 hold SMM Core will be excluded.
936 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
938 @param[in] Context Context to pass into SMM Core
944 ExecuteSmmCoreFromSmram (
945 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRange
,
946 IN OUT EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmCore
,
953 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
955 EFI_IMAGE_ENTRY_POINT EntryPoint
;
958 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
960 Status
= GetSectionFromAnyFvByFileType (
961 EFI_FV_FILETYPE_SMM_CORE
,
968 if (EFI_ERROR (Status
)) {
973 // Initilize ImageContext
975 ImageContext
.Handle
= SourceBuffer
;
976 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
979 // Get information about the image being loaded
981 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
982 if (EFI_ERROR (Status
)) {
986 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
987 // the address assigned by build tool.
989 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
991 // Get the fixed loading address assigned by Build tool
993 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
994 if (!EFI_ERROR (Status
)) {
996 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
1000 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
1002 gSmmCorePrivate
->SmramRangeCount
--;
1004 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
1006 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1007 // specified by SmramRange
1009 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1011 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1012 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1014 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1015 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1016 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1017 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1018 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1021 // Align buffer on section boundary
1023 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1027 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1028 // specified by SmramRange
1030 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES((UINTN
)ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1032 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
1033 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
1035 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
1036 SmramRangeSmmCore
->CpuStart
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
1037 SmramRangeSmmCore
->PhysicalStart
= SmramRange
->PhysicalStart
+ SmramRange
->PhysicalSize
;
1038 SmramRangeSmmCore
->RegionState
= SmramRange
->RegionState
| EFI_ALLOCATED
;
1039 SmramRangeSmmCore
->PhysicalSize
= EFI_PAGES_TO_SIZE (PageCount
);
1042 // Align buffer on section boundary
1044 ImageContext
.ImageAddress
= SmramRangeSmmCore
->CpuStart
;
1047 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
1048 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
1051 // Print debug message showing SMM Core load address.
1053 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
1056 // Load the image to our new buffer
1058 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1059 if (!EFI_ERROR (Status
)) {
1061 // Relocate the image in our new buffer
1063 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1064 if (!EFI_ERROR (Status
)) {
1066 // Flush the instruction cache so the image data are written before we execute it
1068 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
1071 // Print debug message showing SMM Core entry point address.
1073 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
1075 gSmmCorePrivate
->PiSmmCoreImageBase
= ImageContext
.ImageAddress
;
1076 gSmmCorePrivate
->PiSmmCoreImageSize
= ImageContext
.ImageSize
;
1077 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageBase
));
1078 DEBUG ((DEBUG_INFO
, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate
->PiSmmCoreImageSize
));
1080 gSmmCorePrivate
->PiSmmCoreEntryPoint
= ImageContext
.EntryPoint
;
1085 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
1086 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
1091 // Always free memory allocted by GetFileBufferByFilePath ()
1093 FreePool (SourceBuffer
);
1099 SMM split SMRAM entry.
1101 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1102 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1103 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1104 @param[in, out] RangeCount Pointer to range count.
1105 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1106 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1107 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1108 that no need to be split anymore.
1109 @param[in, out] FinalRangeCount Pointer to final range count.
1113 SmmSplitSmramEntry (
1114 IN OUT EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1115 IN OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
,
1116 OUT EFI_SMRAM_DESCRIPTOR
*Ranges
,
1117 IN OUT UINTN
*RangeCount
,
1118 OUT EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRanges
,
1119 IN OUT UINTN
*ReservedRangeCount
,
1120 OUT EFI_SMRAM_DESCRIPTOR
*FinalRanges
,
1121 IN OUT UINTN
*FinalRangeCount
1124 UINT64 RangeToCompareEnd
;
1125 UINT64 ReservedRangeToCompareEnd
;
1127 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1128 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1130 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1131 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1132 if (RangeToCompareEnd
< ReservedRangeToCompareEnd
) {
1134 // RangeToCompare ReservedRangeToCompare
1135 // ---- ---- --------------------------------------
1136 // | | | | -> 1. ReservedRangeToCompare
1137 // ---- | | |--| --------------------------------------
1139 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1140 // | | | | | | RangeToCompare->PhysicalSize = 0
1141 // ---- | | |--| --------------------------------------
1142 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1143 // ---- ---- --------------------------------------
1147 // 1. Update ReservedRangeToCompare.
1149 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1151 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1152 // Zero RangeToCompare->PhysicalSize.
1154 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1155 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1156 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1157 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompare
->PhysicalSize
;
1158 *FinalRangeCount
+= 1;
1159 RangeToCompare
->PhysicalSize
= 0;
1161 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1163 ReservedRanges
[*ReservedRangeCount
].SmramReservedStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1164 ReservedRanges
[*ReservedRangeCount
].SmramReservedSize
= ReservedRangeToCompareEnd
- RangeToCompareEnd
;
1165 *ReservedRangeCount
+= 1;
1168 // RangeToCompare ReservedRangeToCompare
1169 // ---- ---- --------------------------------------
1170 // | | | | -> 1. ReservedRangeToCompare
1171 // ---- | | |--| --------------------------------------
1173 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1175 // | | ---- |--| --------------------------------------
1176 // | | | | -> 3. RangeToCompare
1177 // ---- ---- --------------------------------------
1181 // 1. Update ReservedRangeToCompare.
1183 ReservedRangeToCompare
->SmramReservedSize
= RangeToCompare
->CpuStart
- ReservedRangeToCompare
->SmramReservedStart
;
1185 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1187 FinalRanges
[*FinalRangeCount
].CpuStart
= RangeToCompare
->CpuStart
;
1188 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
;
1189 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1190 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompareEnd
- RangeToCompare
->CpuStart
;
1191 *FinalRangeCount
+= 1;
1193 // 3. Update RangeToCompare.
1195 RangeToCompare
->CpuStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1196 RangeToCompare
->PhysicalStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1197 RangeToCompare
->PhysicalSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1199 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1200 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1201 if (ReservedRangeToCompareEnd
< RangeToCompareEnd
) {
1203 // RangeToCompare ReservedRangeToCompare
1204 // ---- ---- --------------------------------------
1205 // | | | | -> 1. RangeToCompare
1206 // | | ---- |--| --------------------------------------
1208 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1209 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1210 // | | ---- |--| --------------------------------------
1211 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1212 // ---- ---- --------------------------------------
1216 // 1. Update RangeToCompare.
1218 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1220 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1221 // ReservedRangeToCompare->SmramReservedSize = 0
1223 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1224 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1225 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1226 FinalRanges
[*FinalRangeCount
].PhysicalSize
= ReservedRangeToCompare
->SmramReservedSize
;
1227 *FinalRangeCount
+= 1;
1228 ReservedRangeToCompare
->SmramReservedSize
= 0;
1230 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1232 Ranges
[*RangeCount
].CpuStart
= FinalRanges
[*FinalRangeCount
- 1].CpuStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1233 Ranges
[*RangeCount
].PhysicalStart
= FinalRanges
[*FinalRangeCount
- 1].PhysicalStart
+ FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1234 Ranges
[*RangeCount
].RegionState
= RangeToCompare
->RegionState
;
1235 Ranges
[*RangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompareEnd
;
1239 // RangeToCompare ReservedRangeToCompare
1240 // ---- ---- --------------------------------------
1241 // | | | | -> 1. RangeToCompare
1242 // | | ---- |--| --------------------------------------
1244 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1246 // ---- | | |--| --------------------------------------
1247 // | | | | -> 3. ReservedRangeToCompare
1248 // ---- ---- --------------------------------------
1252 // 1. Update RangeToCompare.
1254 RangeToCompare
->PhysicalSize
= ReservedRangeToCompare
->SmramReservedStart
- RangeToCompare
->CpuStart
;
1256 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1257 // ReservedRangeToCompare->SmramReservedSize = 0
1259 FinalRanges
[*FinalRangeCount
].CpuStart
= ReservedRangeToCompare
->SmramReservedStart
;
1260 FinalRanges
[*FinalRangeCount
].PhysicalStart
= RangeToCompare
->PhysicalStart
+ RangeToCompare
->PhysicalSize
;
1261 FinalRanges
[*FinalRangeCount
].RegionState
= RangeToCompare
->RegionState
| EFI_ALLOCATED
;
1262 FinalRanges
[*FinalRangeCount
].PhysicalSize
= RangeToCompareEnd
- ReservedRangeToCompare
->SmramReservedStart
;
1263 *FinalRangeCount
+= 1;
1265 // 3. Update ReservedRangeToCompare.
1267 ReservedRangeToCompare
->SmramReservedStart
+= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1268 ReservedRangeToCompare
->SmramReservedSize
-= FinalRanges
[*FinalRangeCount
- 1].PhysicalSize
;
1274 Returns if SMRAM range and SMRAM reserved range are overlapped.
1276 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1277 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1279 @retval TRUE There is overlap.
1280 @retval FALSE There is no overlap.
1285 IN EFI_SMRAM_DESCRIPTOR
*RangeToCompare
,
1286 IN EFI_SMM_RESERVED_SMRAM_REGION
*ReservedRangeToCompare
1289 UINT64 RangeToCompareEnd
;
1290 UINT64 ReservedRangeToCompareEnd
;
1292 RangeToCompareEnd
= RangeToCompare
->CpuStart
+ RangeToCompare
->PhysicalSize
;
1293 ReservedRangeToCompareEnd
= ReservedRangeToCompare
->SmramReservedStart
+ ReservedRangeToCompare
->SmramReservedSize
;
1295 if ((RangeToCompare
->CpuStart
>= ReservedRangeToCompare
->SmramReservedStart
) &&
1296 (RangeToCompare
->CpuStart
< ReservedRangeToCompareEnd
)) {
1298 } else if ((ReservedRangeToCompare
->SmramReservedStart
>= RangeToCompare
->CpuStart
) &&
1299 (ReservedRangeToCompare
->SmramReservedStart
< RangeToCompareEnd
)) {
1306 Get full SMRAM ranges.
1308 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1309 SmmConfiguration protocol, split the entries if there is overlap between them.
1310 It will also reserve one entry for SMM core.
1312 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1314 @return Pointer to full SMRAM ranges.
1317 EFI_SMRAM_DESCRIPTOR
*
1318 GetFullSmramRanges (
1319 OUT UINTN
*FullSmramRangeCount
1323 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
1327 EFI_SMRAM_DESCRIPTOR
*FullSmramRanges
;
1328 UINTN TempSmramRangeCount
;
1329 UINTN AdditionSmramRangeCount
;
1330 EFI_SMRAM_DESCRIPTOR
*TempSmramRanges
;
1331 UINTN SmramRangeCount
;
1332 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1333 UINTN SmramReservedCount
;
1334 EFI_SMM_RESERVED_SMRAM_REGION
*SmramReservedRanges
;
1339 // Get SMM Configuration Protocol if it is present.
1341 SmmConfiguration
= NULL
;
1342 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1345 // Get SMRAM information.
1348 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1349 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1351 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1354 // Get SMRAM reserved region count.
1356 SmramReservedCount
= 0;
1357 if (SmmConfiguration
!= NULL
) {
1358 while (SmmConfiguration
->SmramReservedRegions
[SmramReservedCount
].SmramReservedSize
!= 0) {
1359 SmramReservedCount
++;
1364 // Reserve one entry for SMM Core in the full SMRAM ranges.
1366 AdditionSmramRangeCount
= 1;
1367 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1369 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1371 AdditionSmramRangeCount
= 2;
1374 if (SmramReservedCount
== 0) {
1376 // No reserved SMRAM entry from SMM Configuration Protocol.
1378 *FullSmramRangeCount
= SmramRangeCount
+ AdditionSmramRangeCount
;
1379 Size
= (*FullSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
);
1380 FullSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocateZeroPool (Size
);
1381 ASSERT (FullSmramRanges
!= NULL
);
1383 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, FullSmramRanges
);
1384 ASSERT_EFI_ERROR (Status
);
1386 return FullSmramRanges
;
1390 // Why MaxCount = X + 2 * Y?
1391 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1393 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1400 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1411 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1424 MaxCount
= SmramRangeCount
+ 2 * SmramReservedCount
;
1426 Size
= MaxCount
* sizeof (EFI_SMM_RESERVED_SMRAM_REGION
);
1427 SmramReservedRanges
= (EFI_SMM_RESERVED_SMRAM_REGION
*) AllocatePool (Size
);
1428 ASSERT (SmramReservedRanges
!= NULL
);
1429 for (Index
= 0; Index
< SmramReservedCount
; Index
++) {
1430 CopyMem (&SmramReservedRanges
[Index
], &SmmConfiguration
->SmramReservedRegions
[Index
], sizeof (EFI_SMM_RESERVED_SMRAM_REGION
));
1433 Size
= MaxCount
* sizeof (EFI_SMRAM_DESCRIPTOR
);
1434 TempSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1435 ASSERT (TempSmramRanges
!= NULL
);
1436 TempSmramRangeCount
= 0;
1438 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
1439 ASSERT (SmramRanges
!= NULL
);
1440 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, SmramRanges
);
1441 ASSERT_EFI_ERROR (Status
);
1445 for (Index
= 0; (Index
< SmramRangeCount
) && !Rescan
; Index
++) {
1447 // Skip zero size entry.
1449 if (SmramRanges
[Index
].PhysicalSize
!= 0) {
1450 for (Index2
= 0; (Index2
< SmramReservedCount
) && !Rescan
; Index2
++) {
1452 // Skip zero size entry.
1454 if (SmramReservedRanges
[Index2
].SmramReservedSize
!= 0) {
1455 if (SmmIsSmramOverlap (
1456 &SmramRanges
[Index
],
1457 &SmramReservedRanges
[Index2
]
1460 // There is overlap, need to split entry and then rescan.
1462 SmmSplitSmramEntry (
1463 &SmramRanges
[Index
],
1464 &SmramReservedRanges
[Index2
],
1467 SmramReservedRanges
,
1468 &SmramReservedCount
,
1470 &TempSmramRangeCount
1478 // No any overlap, copy the entry to the temp SMRAM ranges.
1479 // Zero SmramRanges[Index].PhysicalSize = 0;
1481 CopyMem (&TempSmramRanges
[TempSmramRangeCount
++], &SmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1482 SmramRanges
[Index
].PhysicalSize
= 0;
1487 ASSERT (TempSmramRangeCount
<= MaxCount
);
1492 FullSmramRanges
= AllocateZeroPool ((TempSmramRangeCount
+ AdditionSmramRangeCount
) * sizeof (EFI_SMRAM_DESCRIPTOR
));
1493 ASSERT (FullSmramRanges
!= NULL
);
1494 *FullSmramRangeCount
= 0;
1496 for (Index
= 0; Index
< TempSmramRangeCount
; Index
++) {
1497 if (TempSmramRanges
[Index
].PhysicalSize
!= 0) {
1501 ASSERT (Index
< TempSmramRangeCount
);
1502 for (Index2
= 0; Index2
< TempSmramRangeCount
; Index2
++) {
1503 if ((Index2
!= Index
) && (TempSmramRanges
[Index2
].PhysicalSize
!= 0) && (TempSmramRanges
[Index2
].CpuStart
< TempSmramRanges
[Index
].CpuStart
)) {
1507 CopyMem (&FullSmramRanges
[*FullSmramRangeCount
], &TempSmramRanges
[Index
], sizeof (EFI_SMRAM_DESCRIPTOR
));
1508 *FullSmramRangeCount
+= 1;
1509 TempSmramRanges
[Index
].PhysicalSize
= 0;
1510 } while (*FullSmramRangeCount
< TempSmramRangeCount
);
1511 ASSERT (*FullSmramRangeCount
== TempSmramRangeCount
);
1512 *FullSmramRangeCount
+= AdditionSmramRangeCount
;
1514 FreePool (SmramRanges
);
1515 FreePool (SmramReservedRanges
);
1516 FreePool (TempSmramRanges
);
1518 return FullSmramRanges
;
1522 The Entry Point for SMM IPL
1524 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1525 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1526 critical events required to coordinate between DXE and SMM environments.
1528 @param ImageHandle The firmware allocated handle for the EFI image.
1529 @param SystemTable A pointer to the EFI System Table.
1531 @retval EFI_SUCCESS The entry point is executed successfully.
1532 @retval Other Some error occurred when executing this entry point.
1538 IN EFI_HANDLE ImageHandle
,
1539 IN EFI_SYSTEM_TABLE
*SystemTable
1547 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
1548 EFI_STATUS SetAttrStatus
;
1549 EFI_SMRAM_DESCRIPTOR
*SmramRangeSmmDriver
;
1550 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
1553 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1554 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1557 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
1560 // Get SMM Access Protocol
1562 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
1563 ASSERT_EFI_ERROR (Status
);
1566 // Get SMM Control2 Protocol
1568 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1569 ASSERT_EFI_ERROR (Status
);
1571 gSmmCorePrivate
->SmramRanges
= GetFullSmramRanges (&gSmmCorePrivate
->SmramRangeCount
);
1574 // Open all SMRAM ranges
1576 Status
= mSmmAccess
->Open (mSmmAccess
);
1577 ASSERT_EFI_ERROR (Status
);
1580 // Print debug message that the SMRAM window is now open.
1582 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1585 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1587 mCurrentSmramRange
= NULL
;
1588 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1590 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1592 if ((gSmmCorePrivate
->SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1596 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1597 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
- 1) <= MAX_ADDRESS
) {
1598 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1599 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1600 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1606 if (mCurrentSmramRange
!= NULL
) {
1608 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1610 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1611 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1612 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1615 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1617 // Make sure we can change the desired memory attributes.
1619 Status
= gDS
->GetMemorySpaceDescriptor (
1623 ASSERT_EFI_ERROR (Status
);
1624 if ((MemDesc
.Capabilities
& SMRAM_CAPABILITIES
) != SMRAM_CAPABILITIES
) {
1625 gDS
->SetMemorySpaceCapabilities (
1628 MemDesc
.Capabilities
| SMRAM_CAPABILITIES
1632 // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
1633 // all paging attributes.
1634 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1635 // is not available here.
1638 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1639 if (!EFI_ERROR (Status
)) {
1640 MemDesc
.Attributes
&= ~(MEMORY_CACHE_ATTRIBUTES
| MEMORY_PAGE_ATTRIBUTES
);
1641 MemDesc
.Attributes
|= EFI_MEMORY_WB
;
1642 Status
= gDS
->SetMemorySpaceAttributes (
1647 if (EFI_ERROR (Status
)) {
1648 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1652 gDS
->GetMemorySpaceDescriptor (
1656 DEBUG ((DEBUG_INFO
, "SMRAM attributes: %016lx\n", MemDesc
.Attributes
));
1657 ASSERT ((MemDesc
.Attributes
& MEMORY_PAGE_ATTRIBUTES
) == 0);
1661 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1662 // Modules At Fixed Address Configuration Table.
1664 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1666 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1668 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1670 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1672 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1674 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1676 Status
= EfiGetSystemConfigurationTable (
1677 &gLoadFixedAddressConfigurationTableGuid
,
1678 (VOID
**) &mLMFAConfigurationTable
1680 if (!EFI_ERROR (Status
) && mLMFAConfigurationTable
!= NULL
) {
1681 mLMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1683 // Print the SMRAM base
1685 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable
->SmramBase
));
1689 // Fill the Smram range for all SMM code
1691 SmramRangeSmmDriver
= &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 2];
1692 SmramRangeSmmDriver
->CpuStart
= mCurrentSmramRange
->CpuStart
;
1693 SmramRangeSmmDriver
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
;
1694 SmramRangeSmmDriver
->RegionState
= mCurrentSmramRange
->RegionState
| EFI_ALLOCATED
;
1695 SmramRangeSmmDriver
->PhysicalSize
= SmmCodeSize
;
1697 mCurrentSmramRange
->PhysicalSize
-= SmmCodeSize
;
1698 mCurrentSmramRange
->CpuStart
= mCurrentSmramRange
->CpuStart
+ SmmCodeSize
;
1699 mCurrentSmramRange
->PhysicalStart
= mCurrentSmramRange
->PhysicalStart
+ SmmCodeSize
;
1702 // Load SMM Core into SMRAM and execute it from SMRAM
1704 Status
= ExecuteSmmCoreFromSmram (
1706 &gSmmCorePrivate
->SmramRanges
[gSmmCorePrivate
->SmramRangeCount
- 1],
1709 if (EFI_ERROR (Status
)) {
1711 // Print error message that the SMM Core failed to be loaded and executed.
1713 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1716 // Attempt to reset SMRAM cacheability to UC
1718 if (CpuArch
!= NULL
) {
1719 SetAttrStatus
= gDS
->SetMemorySpaceAttributes(
1724 if (EFI_ERROR (SetAttrStatus
)) {
1725 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1731 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1733 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1737 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1738 // resources, and return an error so SMM IPL will be unloaded.
1740 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1742 // Close all SMRAM ranges
1744 Status
= mSmmAccess
->Close (mSmmAccess
);
1745 ASSERT_EFI_ERROR (Status
);
1748 // Print debug message that the SMRAM window is now closed.
1750 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1753 // Free all allocated resources
1755 FreePool (gSmmCorePrivate
->SmramRanges
);
1757 return EFI_UNSUPPORTED
;
1761 // Install SMM Base2 Protocol and SMM Communication Protocol
1763 Status
= gBS
->InstallMultipleProtocolInterfaces (
1765 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1766 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1769 ASSERT_EFI_ERROR (Status
);
1772 // Create the set of protocol and event notififcations that the SMM IPL requires
1774 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1775 if (mSmmIplEvents
[Index
].Protocol
) {
1776 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1777 mSmmIplEvents
[Index
].Guid
,
1778 mSmmIplEvents
[Index
].NotifyTpl
,
1779 mSmmIplEvents
[Index
].NotifyFunction
,
1780 mSmmIplEvents
[Index
].NotifyContext
,
1784 Status
= gBS
->CreateEventEx (
1786 mSmmIplEvents
[Index
].NotifyTpl
,
1787 mSmmIplEvents
[Index
].NotifyFunction
,
1788 mSmmIplEvents
[Index
].NotifyContext
,
1789 mSmmIplEvents
[Index
].Guid
,
1790 &mSmmIplEvents
[Index
].Event
1792 ASSERT_EFI_ERROR (Status
);