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_LOADED_IMAGE_PROTOCOL
*mSmmCoreLoadedImage
;
93 Place holder function until all the SMM System Table Service are available.
95 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
100 @param Arg4 Undefined
101 @param Arg5 Undefined
103 @return EFI_NOT_AVAILABLE_YET
108 SmmEfiNotAvailableYetArg5 (
117 // This function should never be executed. If it does, then the architectural protocols
118 // have not been designed correctly.
120 return EFI_NOT_AVAILABLE_YET
;
124 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
125 Core uses this signal to know that a Legacy Boot has been performed and that
126 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
127 not be accessed from SMM anymore since that structure is considered free memory by
128 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM
129 driver that system enter legacy boot.
131 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
132 @param Context Points to an optional handler context which was specified when the handler was registered.
133 @param CommBuffer A pointer to a collection of data in memory that will
134 be conveyed from a non-SMM environment into an SMM environment.
135 @param CommBufferSize The size of the CommBuffer.
142 SmmLegacyBootHandler (
143 IN EFI_HANDLE DispatchHandle
,
144 IN CONST VOID
*Context
, OPTIONAL
145 IN OUT VOID
*CommBuffer
, OPTIONAL
146 IN OUT UINTN
*CommBufferSize OPTIONAL
150 EFI_HANDLE SmmHandle
;
153 // Install SMM Legacy Boot protocol.
156 Status
= SmmInstallProtocolInterface (
158 &gEdkiiSmmLegacyBootProtocolGuid
,
159 EFI_NATIVE_INTERFACE
,
163 mInLegacyBoot
= TRUE
;
165 SmiHandlerUnRegister (DispatchHandle
);
171 Software SMI handler that is called when an Exit Boot Services event is signalled.
172 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver
173 that system enter exit boot services.
175 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
176 @param Context Points to an optional handler context which was specified when the handler was registered.
177 @param CommBuffer A pointer to a collection of data in memory that will
178 be conveyed from a non-SMM environment into an SMM environment.
179 @param CommBufferSize The size of the CommBuffer.
186 SmmExitBootServicesHandler (
187 IN EFI_HANDLE DispatchHandle
,
188 IN CONST VOID
*Context
, OPTIONAL
189 IN OUT VOID
*CommBuffer
, OPTIONAL
190 IN OUT UINTN
*CommBufferSize OPTIONAL
194 EFI_HANDLE SmmHandle
;
197 // Install SMM Exit Boot Services protocol.
200 Status
= SmmInstallProtocolInterface (
202 &gEdkiiSmmExitBootServicesProtocolGuid
,
203 EFI_NATIVE_INTERFACE
,
207 SmiHandlerUnRegister (DispatchHandle
);
213 Software SMI handler that is called when an Ready To Boot event is signalled.
214 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver
215 that system enter ready to boot.
217 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
218 @param Context Points to an optional handler context which was specified when the handler was registered.
219 @param CommBuffer A pointer to a collection of data in memory that will
220 be conveyed from a non-SMM environment into an SMM environment.
221 @param CommBufferSize The size of the CommBuffer.
228 SmmReadyToBootHandler (
229 IN EFI_HANDLE DispatchHandle
,
230 IN CONST VOID
*Context
, OPTIONAL
231 IN OUT VOID
*CommBuffer
, OPTIONAL
232 IN OUT UINTN
*CommBufferSize OPTIONAL
236 EFI_HANDLE SmmHandle
;
239 // Install SMM Ready To Boot protocol.
242 Status
= SmmInstallProtocolInterface (
244 &gEdkiiSmmReadyToBootProtocolGuid
,
245 EFI_NATIVE_INTERFACE
,
249 SmiHandlerUnRegister (DispatchHandle
);
255 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
256 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
257 Software SMIs that are nor required after SMRAM is locked and installs the
258 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
259 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed
260 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
262 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
263 @param Context Points to an optional handler context which was specified when the handler was registered.
264 @param CommBuffer A pointer to a collection of data in memory that will
265 be conveyed from a non-SMM environment into an SMM environment.
266 @param CommBufferSize The size of the CommBuffer.
273 SmmReadyToLockHandler (
274 IN EFI_HANDLE DispatchHandle
,
275 IN CONST VOID
*Context
, OPTIONAL
276 IN OUT VOID
*CommBuffer
, OPTIONAL
277 IN OUT UINTN
*CommBufferSize OPTIONAL
282 EFI_HANDLE SmmHandle
;
286 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
288 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
289 if (mSmmCoreSmiHandlers
[Index
].UnRegister
) {
290 SmiHandlerUnRegister (mSmmCoreSmiHandlers
[Index
].DispatchHandle
);
295 // Install SMM Ready to lock protocol
298 Status
= SmmInstallProtocolInterface (
300 &gEfiSmmReadyToLockProtocolGuid
,
301 EFI_NATIVE_INTERFACE
,
306 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
308 Status
= SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid
, NULL
, &Interface
);
311 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
314 if (EFI_ERROR (Status
)) {
315 DEBUG ((DEBUG_ERROR
, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
320 // Assert if the CPU I/O 2 Protocol is not installed
322 ASSERT_EFI_ERROR (Status
);
325 // Display any drivers that were not dispatched because dependency expression
326 // evaluated to false if this is a debug build
329 SmmDisplayDiscoveredNotDispatched ();
333 // Not allowed to use gST or gBS after lock
338 SmramProfileReadyToLock ();
344 Software SMI handler that is called when the EndOfDxe event is signalled.
345 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
346 platform code will invoke 3rd part code.
348 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
349 @param Context Points to an optional handler context which was specified when the handler was registered.
350 @param CommBuffer A pointer to a collection of data in memory that will
351 be conveyed from a non-SMM environment into an SMM environment.
352 @param CommBufferSize The size of the CommBuffer.
360 IN EFI_HANDLE DispatchHandle
,
361 IN CONST VOID
*Context
, OPTIONAL
362 IN OUT VOID
*CommBuffer
, OPTIONAL
363 IN OUT UINTN
*CommBufferSize OPTIONAL
367 EFI_HANDLE SmmHandle
;
369 DEBUG ((EFI_D_INFO
, "SmmEndOfDxeHandler\n"));
371 // Install SMM EndOfDxe protocol
374 Status
= SmmInstallProtocolInterface (
376 &gEfiSmmEndOfDxeProtocolGuid
,
377 EFI_NATIVE_INTERFACE
,
384 Determine if two buffers overlap in memory.
386 @param[in] Buff1 Pointer to first buffer
387 @param[in] Size1 Size of Buff1
388 @param[in] Buff2 Pointer to second buffer
389 @param[in] Size2 Size of Buff2
391 @retval TRUE Buffers overlap in memory.
392 @retval FALSE Buffer doesn't overlap.
396 InternalIsBufferOverlapped (
404 // If buff1's end is less than the start of buff2, then it's ok.
405 // Also, if buff1's start is beyond buff2's end, then it's ok.
407 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
415 The main entry point to SMM Foundation.
417 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
419 @param SmmEntryContext Processor information and functionality
420 needed by SMM Foundation.
426 IN CONST EFI_SMM_ENTRY_CONTEXT
*SmmEntryContext
430 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
431 BOOLEAN InLegacyBoot
;
432 BOOLEAN IsOverlapped
;
434 PERF_START (NULL
, "SMM", NULL
, 0) ;
437 // Update SMST with contents of the SmmEntryContext structure
439 gSmmCoreSmst
.SmmStartupThisAp
= SmmEntryContext
->SmmStartupThisAp
;
440 gSmmCoreSmst
.CurrentlyExecutingCpu
= SmmEntryContext
->CurrentlyExecutingCpu
;
441 gSmmCoreSmst
.NumberOfCpus
= SmmEntryContext
->NumberOfCpus
;
442 gSmmCoreSmst
.CpuSaveStateSize
= SmmEntryContext
->CpuSaveStateSize
;
443 gSmmCoreSmst
.CpuSaveState
= SmmEntryContext
->CpuSaveState
;
446 // Call platform hook before Smm Dispatch
448 PlatformHookBeforeSmmDispatch ();
451 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
453 InLegacyBoot
= mInLegacyBoot
;
456 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
458 gSmmCorePrivate
->InSmm
= TRUE
;
461 // Check to see if this is a Synchronous SMI sent through the SMM Communication
462 // Protocol or an Asynchronous SMI
464 if (gSmmCorePrivate
->CommunicationBuffer
!= NULL
) {
466 // Synchronous SMI for SMM Core or request from Communicate protocol
468 IsOverlapped
= InternalIsBufferOverlapped (
469 (UINT8
*) gSmmCorePrivate
->CommunicationBuffer
,
470 gSmmCorePrivate
->BufferSize
,
471 (UINT8
*) gSmmCorePrivate
,
472 sizeof (*gSmmCorePrivate
)
474 if (!SmmIsBufferOutsideSmmValid ((UINTN
)gSmmCorePrivate
->CommunicationBuffer
, gSmmCorePrivate
->BufferSize
) || IsOverlapped
) {
476 // If CommunicationBuffer is not in valid address scope,
477 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
478 // return EFI_INVALID_PARAMETER
480 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
481 gSmmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
483 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)gSmmCorePrivate
->CommunicationBuffer
;
484 gSmmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
486 &CommunicateHeader
->HeaderGuid
,
488 CommunicateHeader
->Data
,
489 &gSmmCorePrivate
->BufferSize
492 // Update CommunicationBuffer, BufferSize and ReturnStatus
493 // Communicate service finished, reset the pointer to CommBuffer to NULL
495 gSmmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
496 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
497 gSmmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
503 // Process Asynchronous SMI sources
505 SmiManage (NULL
, NULL
, NULL
, NULL
);
508 // Call platform hook after Smm Dispatch
510 PlatformHookAfterSmmDispatch ();
513 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
517 // Clear the InSmm flag as we are going to leave SMM
519 gSmmCorePrivate
->InSmm
= FALSE
;
522 PERF_END (NULL
, "SMM", NULL
, 0) ;
526 Install LoadedImage protocol for SMM Core.
529 SmmCoreInstallLoadedImage (
537 // Allocate a Loaded Image Protocol in EfiBootServicesData
539 Status
= gBS
->AllocatePool (EfiBootServicesData
, sizeof(EFI_LOADED_IMAGE_PROTOCOL
), (VOID
**)&mSmmCoreLoadedImage
);
540 ASSERT_EFI_ERROR (Status
);
542 ZeroMem (mSmmCoreLoadedImage
, sizeof (EFI_LOADED_IMAGE_PROTOCOL
));
544 // Fill in the remaining fields of the Loaded Image Protocol instance.
545 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
547 mSmmCoreLoadedImage
->Revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
548 mSmmCoreLoadedImage
->ParentHandle
= gSmmCorePrivate
->SmmIplImageHandle
;
549 mSmmCoreLoadedImage
->SystemTable
= gST
;
551 mSmmCoreLoadedImage
->ImageBase
= (VOID
*)(UINTN
)gSmmCorePrivate
->PiSmmCoreImageBase
;
552 mSmmCoreLoadedImage
->ImageSize
= gSmmCorePrivate
->PiSmmCoreImageSize
;
553 mSmmCoreLoadedImage
->ImageCodeType
= EfiRuntimeServicesCode
;
554 mSmmCoreLoadedImage
->ImageDataType
= EfiRuntimeServicesData
;
557 // Create a new image handle in the UEFI handle database for the SMM Driver
560 Status
= gBS
->InstallMultipleProtocolInterfaces (
562 &gEfiLoadedImageProtocolGuid
, mSmmCoreLoadedImage
,
565 ASSERT_EFI_ERROR (Status
);
571 The Entry Point for SMM Core
573 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
574 EntryPoint on the SMI vector.
576 Note: This function is called for both DXE invocation and SMRAM invocation.
578 @param ImageHandle The firmware allocated handle for the EFI image.
579 @param SystemTable A pointer to the EFI System Table.
581 @retval EFI_SUCCESS The entry point is executed successfully.
582 @retval Other Some error occurred when executing this entry point.
588 IN EFI_HANDLE ImageHandle
,
589 IN EFI_SYSTEM_TABLE
*SystemTable
596 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
598 gSmmCorePrivate
= (SMM_CORE_PRIVATE_DATA
*)ImageHandle
;
601 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
603 gSmmCorePrivate
->Smst
= &gSmmCoreSmst
;
604 gSmmCorePrivate
->SmmEntryPoint
= SmmEntryPoint
;
607 // No need to initialize memory service.
608 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
609 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
615 // Copy FullSmramRanges to SMRAM
617 mFullSmramRangeCount
= gSmmCorePrivate
->SmramRangeCount
;
618 mFullSmramRanges
= AllocatePool (mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
619 ASSERT (mFullSmramRanges
!= NULL
);
620 CopyMem (mFullSmramRanges
, gSmmCorePrivate
->SmramRanges
, mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
623 // Register all SMI Handlers required by the SMM Core
625 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
626 Status
= SmiHandlerRegister (
627 mSmmCoreSmiHandlers
[Index
].Handler
,
628 mSmmCoreSmiHandlers
[Index
].HandlerType
,
629 &mSmmCoreSmiHandlers
[Index
].DispatchHandle
631 ASSERT_EFI_ERROR (Status
);
634 RegisterSmramProfileHandler ();
635 SmramProfileInstallProtocol ();
637 SmmCoreInstallLoadedImage ();