2 SMM Core Main Entry Point
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PiSmmCore.h"
18 // Physical pointer to private structure shared between SMM IPL and the SMM Core
20 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
;
23 // SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.
25 EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst
= {
28 EFI_SMM_SYSTEM_TABLE2_REVISION
,
29 sizeof (gSmmCoreSmst
.Hdr
)
31 NULL
, // SmmFirmwareVendor
32 0, // SmmFirmwareRevision
33 SmmInstallConfigurationTable
,
36 (EFI_SMM_CPU_IO2
) SmmEfiNotAvailableYetArg5
, // SmmMemRead
37 (EFI_SMM_CPU_IO2
) SmmEfiNotAvailableYetArg5
// SmmMemWrite
40 (EFI_SMM_CPU_IO2
) SmmEfiNotAvailableYetArg5
, // SmmIoRead
41 (EFI_SMM_CPU_IO2
) SmmEfiNotAvailableYetArg5
// SmmIoWrite
48 NULL
, // SmmStartupThisAp
49 0, // CurrentlyExecutingCpu
51 NULL
, // CpuSaveStateSize
53 0, // NumberOfTableEntries
54 NULL
, // SmmConfigurationTable
55 SmmInstallProtocolInterface
,
56 SmmUninstallProtocolInterface
,
58 SmmRegisterProtocolNotify
,
67 // Flag to determine if the platform has performed a legacy boot.
68 // If this flag is TRUE, then the runtime code and runtime data associated with the
69 // SMM IPL are converted to free memory, so the SMM Core must guarantee that is
70 // does not touch of the code/data associated with the SMM IPL if this flag is TRUE.
72 BOOLEAN mInLegacyBoot
= FALSE
;
75 // Table of SMI Handlers that are registered by the SMM Core when it is initialized
77 SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers
[] = {
78 { SmmDriverDispatchHandler
, &gEfiEventDxeDispatchGuid
, NULL
, TRUE
},
79 { SmmReadyToLockHandler
, &gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, TRUE
},
80 { SmmLegacyBootHandler
, &gEfiEventLegacyBootGuid
, NULL
, FALSE
},
81 { SmmExitBootServicesHandler
, &gEfiEventExitBootServicesGuid
, NULL
, FALSE
},
82 { SmmReadyToBootHandler
, &gEfiEventReadyToBootGuid
, NULL
, FALSE
},
83 { SmmEndOfDxeHandler
, &gEfiEndOfDxeEventGroupGuid
, NULL
, TRUE
},
84 { NULL
, NULL
, NULL
, FALSE
}
87 UINTN mFullSmramRangeCount
;
88 EFI_SMRAM_DESCRIPTOR
*mFullSmramRanges
;
90 EFI_SMM_DRIVER_ENTRY
*mSmmCoreDriverEntry
;
92 EFI_LOADED_IMAGE_PROTOCOL
*mSmmCoreLoadedImage
;
95 Place holder function until all the SMM System Table Service are available.
97 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
100 @param Arg2 Undefined
101 @param Arg3 Undefined
102 @param Arg4 Undefined
103 @param Arg5 Undefined
105 @return EFI_NOT_AVAILABLE_YET
110 SmmEfiNotAvailableYetArg5 (
119 // This function should never be executed. If it does, then the architectural protocols
120 // have not been designed correctly.
122 return EFI_NOT_AVAILABLE_YET
;
126 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
127 Core uses this signal to know that a Legacy Boot has been performed and that
128 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
129 not be accessed from SMM anymore since that structure is considered free memory by
130 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM
131 driver that system enter legacy boot.
133 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
134 @param Context Points to an optional handler context which was specified when the handler was registered.
135 @param CommBuffer A pointer to a collection of data in memory that will
136 be conveyed from a non-SMM environment into an SMM environment.
137 @param CommBufferSize The size of the CommBuffer.
144 SmmLegacyBootHandler (
145 IN EFI_HANDLE DispatchHandle
,
146 IN CONST VOID
*Context
, OPTIONAL
147 IN OUT VOID
*CommBuffer
, OPTIONAL
148 IN OUT UINTN
*CommBufferSize OPTIONAL
152 EFI_HANDLE SmmHandle
;
155 // Install SMM Legacy Boot protocol.
158 Status
= SmmInstallProtocolInterface (
160 &gEdkiiSmmLegacyBootProtocolGuid
,
161 EFI_NATIVE_INTERFACE
,
165 mInLegacyBoot
= TRUE
;
167 SmiHandlerUnRegister (DispatchHandle
);
173 Software SMI handler that is called when an Exit Boot Services event is signalled.
174 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver
175 that system enter exit boot services.
177 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
178 @param Context Points to an optional handler context which was specified when the handler was registered.
179 @param CommBuffer A pointer to a collection of data in memory that will
180 be conveyed from a non-SMM environment into an SMM environment.
181 @param CommBufferSize The size of the CommBuffer.
188 SmmExitBootServicesHandler (
189 IN EFI_HANDLE DispatchHandle
,
190 IN CONST VOID
*Context
, OPTIONAL
191 IN OUT VOID
*CommBuffer
, OPTIONAL
192 IN OUT UINTN
*CommBufferSize OPTIONAL
196 EFI_HANDLE SmmHandle
;
199 // Install SMM Exit Boot Services protocol.
202 Status
= SmmInstallProtocolInterface (
204 &gEdkiiSmmExitBootServicesProtocolGuid
,
205 EFI_NATIVE_INTERFACE
,
209 SmiHandlerUnRegister (DispatchHandle
);
215 Software SMI handler that is called when an Ready To Boot event is signalled.
216 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver
217 that system enter ready to boot.
219 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
220 @param Context Points to an optional handler context which was specified when the handler was registered.
221 @param CommBuffer A pointer to a collection of data in memory that will
222 be conveyed from a non-SMM environment into an SMM environment.
223 @param CommBufferSize The size of the CommBuffer.
230 SmmReadyToBootHandler (
231 IN EFI_HANDLE DispatchHandle
,
232 IN CONST VOID
*Context
, OPTIONAL
233 IN OUT VOID
*CommBuffer
, OPTIONAL
234 IN OUT UINTN
*CommBufferSize OPTIONAL
238 EFI_HANDLE SmmHandle
;
241 // Install SMM Ready To Boot protocol.
244 Status
= SmmInstallProtocolInterface (
246 &gEdkiiSmmReadyToBootProtocolGuid
,
247 EFI_NATIVE_INTERFACE
,
251 SmiHandlerUnRegister (DispatchHandle
);
257 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
258 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
259 Software SMIs that are nor required after SMRAM is locked and installs the
260 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
261 to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed
262 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
264 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
265 @param Context Points to an optional handler context which was specified when the handler was registered.
266 @param CommBuffer A pointer to a collection of data in memory that will
267 be conveyed from a non-SMM environment into an SMM environment.
268 @param CommBufferSize The size of the CommBuffer.
275 SmmReadyToLockHandler (
276 IN EFI_HANDLE DispatchHandle
,
277 IN CONST VOID
*Context
, OPTIONAL
278 IN OUT VOID
*CommBuffer
, OPTIONAL
279 IN OUT UINTN
*CommBufferSize OPTIONAL
284 EFI_HANDLE SmmHandle
;
288 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
290 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
291 if (mSmmCoreSmiHandlers
[Index
].UnRegister
) {
292 SmiHandlerUnRegister (mSmmCoreSmiHandlers
[Index
].DispatchHandle
);
297 // Install SMM Ready to lock protocol
300 Status
= SmmInstallProtocolInterface (
302 &gEfiSmmReadyToLockProtocolGuid
,
303 EFI_NATIVE_INTERFACE
,
308 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
310 Status
= SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid
, NULL
, &Interface
);
313 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
316 if (EFI_ERROR (Status
)) {
317 DEBUG ((DEBUG_ERROR
, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
322 // Assert if the CPU I/O 2 Protocol is not installed
324 ASSERT_EFI_ERROR (Status
);
327 // Display any drivers that were not dispatched because dependency expression
328 // evaluated to false if this is a debug build
331 SmmDisplayDiscoveredNotDispatched ();
335 // Not allowed to use gST or gBS after lock
340 SmramProfileReadyToLock ();
346 Software SMI handler that is called when the EndOfDxe event is signalled.
347 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
348 platform code will invoke 3rd part code.
350 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
351 @param Context Points to an optional handler context which was specified when the handler was registered.
352 @param CommBuffer A pointer to a collection of data in memory that will
353 be conveyed from a non-SMM environment into an SMM environment.
354 @param CommBufferSize The size of the CommBuffer.
362 IN EFI_HANDLE DispatchHandle
,
363 IN CONST VOID
*Context
, OPTIONAL
364 IN OUT VOID
*CommBuffer
, OPTIONAL
365 IN OUT UINTN
*CommBufferSize OPTIONAL
369 EFI_HANDLE SmmHandle
;
371 DEBUG ((EFI_D_INFO
, "SmmEndOfDxeHandler\n"));
373 // Install SMM EndOfDxe protocol
376 Status
= SmmInstallProtocolInterface (
378 &gEfiSmmEndOfDxeProtocolGuid
,
379 EFI_NATIVE_INTERFACE
,
386 Determine if two buffers overlap in memory.
388 @param[in] Buff1 Pointer to first buffer
389 @param[in] Size1 Size of Buff1
390 @param[in] Buff2 Pointer to second buffer
391 @param[in] Size2 Size of Buff2
393 @retval TRUE Buffers overlap in memory.
394 @retval FALSE Buffer doesn't overlap.
398 InternalIsBufferOverlapped (
406 // If buff1's end is less than the start of buff2, then it's ok.
407 // Also, if buff1's start is beyond buff2's end, then it's ok.
409 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
417 The main entry point to SMM Foundation.
419 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
421 @param SmmEntryContext Processor information and functionality
422 needed by SMM Foundation.
428 IN CONST EFI_SMM_ENTRY_CONTEXT
*SmmEntryContext
432 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
433 BOOLEAN InLegacyBoot
;
434 BOOLEAN IsOverlapped
;
436 PERF_START (NULL
, "SMM", NULL
, 0) ;
439 // Update SMST with contents of the SmmEntryContext structure
441 gSmmCoreSmst
.SmmStartupThisAp
= SmmEntryContext
->SmmStartupThisAp
;
442 gSmmCoreSmst
.CurrentlyExecutingCpu
= SmmEntryContext
->CurrentlyExecutingCpu
;
443 gSmmCoreSmst
.NumberOfCpus
= SmmEntryContext
->NumberOfCpus
;
444 gSmmCoreSmst
.CpuSaveStateSize
= SmmEntryContext
->CpuSaveStateSize
;
445 gSmmCoreSmst
.CpuSaveState
= SmmEntryContext
->CpuSaveState
;
448 // Call platform hook before Smm Dispatch
450 PlatformHookBeforeSmmDispatch ();
453 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
455 InLegacyBoot
= mInLegacyBoot
;
458 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
460 gSmmCorePrivate
->InSmm
= TRUE
;
463 // Check to see if this is a Synchronous SMI sent through the SMM Communication
464 // Protocol or an Asynchronous SMI
466 if (gSmmCorePrivate
->CommunicationBuffer
!= NULL
) {
468 // Synchronous SMI for SMM Core or request from Communicate protocol
470 IsOverlapped
= InternalIsBufferOverlapped (
471 (UINT8
*) gSmmCorePrivate
->CommunicationBuffer
,
472 gSmmCorePrivate
->BufferSize
,
473 (UINT8
*) gSmmCorePrivate
,
474 sizeof (*gSmmCorePrivate
)
476 if (!SmmIsBufferOutsideSmmValid ((UINTN
)gSmmCorePrivate
->CommunicationBuffer
, gSmmCorePrivate
->BufferSize
) || IsOverlapped
) {
478 // If CommunicationBuffer is not in valid address scope,
479 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
480 // return EFI_INVALID_PARAMETER
482 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
483 gSmmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
485 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)gSmmCorePrivate
->CommunicationBuffer
;
486 gSmmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
488 &CommunicateHeader
->HeaderGuid
,
490 CommunicateHeader
->Data
,
491 &gSmmCorePrivate
->BufferSize
494 // Update CommunicationBuffer, BufferSize and ReturnStatus
495 // Communicate service finished, reset the pointer to CommBuffer to NULL
497 gSmmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
498 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
499 gSmmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
505 // Process Asynchronous SMI sources
507 SmiManage (NULL
, NULL
, NULL
, NULL
);
510 // Call platform hook after Smm Dispatch
512 PlatformHookAfterSmmDispatch ();
515 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
519 // Clear the InSmm flag as we are going to leave SMM
521 gSmmCorePrivate
->InSmm
= FALSE
;
524 PERF_END (NULL
, "SMM", NULL
, 0) ;
528 Install LoadedImage protocol for SMM Core.
531 SmmCoreInstallLoadedImage (
539 // Allocate a Loaded Image Protocol in EfiBootServicesData
541 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof(EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&mSmmCoreLoadedImage
);
542 ASSERT_EFI_ERROR (Status
);
544 ZeroMem (mSmmCoreLoadedImage
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
));
546 // Fill in the remaining fields of the Loaded Image Protocol instance.
547 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
549 mSmmCoreLoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
550 mSmmCoreLoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
551 mSmmCoreLoadedImage
->SystemTable
= gST
;
553 mSmmCoreLoadedImage
->ImageBase
= (VOID
*)(UINTN
)gSmmCorePrivate
->PiSmmCoreImageBase
;
554 mSmmCoreLoadedImage
->ImageSize
= gSmmCorePrivate
->PiSmmCoreImageSize
;
555 mSmmCoreLoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
556 mSmmCoreLoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
559 // Create a new image handle in the UEFI handle database for the SMM Driver
562 Status
= gBS
->InstallMultipleProtocolInterfaces (
564 &gEfiLoadedImageProtocolGuid
, mSmmCoreLoadedImage
,
567 ASSERT_EFI_ERROR (Status
);
570 // Allocate a Loaded Image Protocol in SMM
572 Status
= SmmAllocatePool (EfiRuntimeServicesData
, sizeof(EFI_SMM_DRIVER_ENTRY
), (VOID
**)&mSmmCoreDriverEntry
);
573 ASSERT_EFI_ERROR(Status
);
575 ZeroMem (mSmmCoreDriverEntry
, sizeof(EFI_SMM_DRIVER_ENTRY
));
577 // Fill in the remaining fields of the Loaded Image Protocol instance.
579 mSmmCoreDriverEntry
->Signature
= EFI_SMM_DRIVER_ENTRY_SIGNATURE
;
580 mSmmCoreDriverEntry
->SmmLoadedImage
.Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
581 mSmmCoreDriverEntry
->SmmLoadedImage
.ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
582 mSmmCoreDriverEntry
->SmmLoadedImage
.SystemTable
= gST
;
584 mSmmCoreDriverEntry
->SmmLoadedImage
.ImageBase
= (VOID
*)(UINTN
)gSmmCorePrivate
->PiSmmCoreImageBase
;
585 mSmmCoreDriverEntry
->SmmLoadedImage
.ImageSize
= gSmmCorePrivate
->PiSmmCoreImageSize
;
586 mSmmCoreDriverEntry
->SmmLoadedImage
.ImageCodeType
= EfiRuntimeServicesCode
;
587 mSmmCoreDriverEntry
->SmmLoadedImage
.ImageDataType
= EfiRuntimeServicesData
;
589 mSmmCoreDriverEntry
->ImageEntryPoint
= gSmmCorePrivate
->PiSmmCoreEntryPoint
;
590 mSmmCoreDriverEntry
->ImageBuffer
= gSmmCorePrivate
->PiSmmCoreImageBase
;
591 mSmmCoreDriverEntry
->NumberOfPage
= EFI_SIZE_TO_PAGES((UINTN
)gSmmCorePrivate
->PiSmmCoreImageSize
);
594 // Create a new image handle in the SMM handle database for the SMM Driver
596 mSmmCoreDriverEntry
->SmmImageHandle
= NULL
;
597 Status
= SmmInstallProtocolInterface (
598 &mSmmCoreDriverEntry
->SmmImageHandle
,
599 &gEfiLoadedImageProtocolGuid
,
600 EFI_NATIVE_INTERFACE
,
601 &mSmmCoreDriverEntry
->SmmLoadedImage
603 ASSERT_EFI_ERROR(Status
);
609 The Entry Point for SMM Core
611 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
612 EntryPoint on the SMI vector.
614 Note: This function is called for both DXE invocation and SMRAM invocation.
616 @param ImageHandle The firmware allocated handle for the EFI image.
617 @param SystemTable A pointer to the EFI System Table.
619 @retval EFI_SUCCESS The entry point is executed successfully.
620 @retval Other Some error occurred when executing this entry point.
626 IN EFI_HANDLE ImageHandle
,
627 IN EFI_SYSTEM_TABLE
*SystemTable
634 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
636 gSmmCorePrivate
= (SMM_CORE_PRIVATE_DATA
*)ImageHandle
;
639 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
641 gSmmCorePrivate
->Smst
= &gSmmCoreSmst
;
642 gSmmCorePrivate
->SmmEntryPoint
= SmmEntryPoint
;
645 // No need to initialize memory service.
646 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
647 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
653 // Copy FullSmramRanges to SMRAM
655 mFullSmramRangeCount
= gSmmCorePrivate
->SmramRangeCount
;
656 mFullSmramRanges
= AllocatePool (mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
657 ASSERT (mFullSmramRanges
!= NULL
);
658 CopyMem (mFullSmramRanges
, gSmmCorePrivate
->SmramRanges
, mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
661 // Register all SMI Handlers required by the SMM Core
663 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
664 Status
= SmiHandlerRegister (
665 mSmmCoreSmiHandlers
[Index
].Handler
,
666 mSmmCoreSmiHandlers
[Index
].HandlerType
,
667 &mSmmCoreSmiHandlers
[Index
].DispatchHandle
669 ASSERT_EFI_ERROR (Status
);
672 RegisterSmramProfileHandler ();
673 SmramProfileInstallProtocol ();
675 SmmCoreInstallLoadedImage ();
677 SmmCoreInitializeMemoryAttributesTable ();