2 SMM Core Main Entry Point
4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
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
, FALSE
},
84 { NULL
, NULL
, NULL
, FALSE
}
87 UINTN mFullSmramRangeCount
;
88 EFI_SMRAM_DESCRIPTOR
*mFullSmramRanges
;
91 Place holder function until all the SMM System Table Service are available.
93 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
101 @return EFI_NOT_AVAILABLE_YET
106 SmmEfiNotAvailableYetArg5 (
115 // This function should never be executed. If it does, then the architectural protocols
116 // have not been designed correctly.
118 return EFI_NOT_AVAILABLE_YET
;
122 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
123 Core uses this signal to know that a Legacy Boot has been performed and that
124 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
125 not be accessed from SMM anymore since that structure is considered free memory by
126 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM
127 driver that system enter legacy boot.
129 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
130 @param Context Points to an optional handler context which was specified when the handler was registered.
131 @param CommBuffer A pointer to a collection of data in memory that will
132 be conveyed from a non-SMM environment into an SMM environment.
133 @param CommBufferSize The size of the CommBuffer.
140 SmmLegacyBootHandler (
141 IN EFI_HANDLE DispatchHandle
,
142 IN CONST VOID
*Context
, OPTIONAL
143 IN OUT VOID
*CommBuffer
, OPTIONAL
144 IN OUT UINTN
*CommBufferSize OPTIONAL
148 EFI_HANDLE SmmHandle
;
151 // Install SMM Legacy Boot protocol.
154 Status
= SmmInstallProtocolInterface (
156 &gEdkiiSmmLegacyBootProtocolGuid
,
157 EFI_NATIVE_INTERFACE
,
161 mInLegacyBoot
= TRUE
;
163 SmiHandlerUnRegister (DispatchHandle
);
169 Software SMI handler that is called when an Exit Boot Services event is signalled.
170 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver
171 that system enter exit boot services.
173 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
174 @param Context Points to an optional handler context which was specified when the handler was registered.
175 @param CommBuffer A pointer to a collection of data in memory that will
176 be conveyed from a non-SMM environment into an SMM environment.
177 @param CommBufferSize The size of the CommBuffer.
184 SmmExitBootServicesHandler (
185 IN EFI_HANDLE DispatchHandle
,
186 IN CONST VOID
*Context
, OPTIONAL
187 IN OUT VOID
*CommBuffer
, OPTIONAL
188 IN OUT UINTN
*CommBufferSize OPTIONAL
192 EFI_HANDLE SmmHandle
;
195 // Install SMM Exit Boot Services protocol.
198 Status
= SmmInstallProtocolInterface (
200 &gEdkiiSmmExitBootServicesProtocolGuid
,
201 EFI_NATIVE_INTERFACE
,
205 SmiHandlerUnRegister (DispatchHandle
);
211 Software SMI handler that is called when an Ready To Boot event is signalled.
212 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver
213 that system enter ready to boot.
215 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
216 @param Context Points to an optional handler context which was specified when the handler was registered.
217 @param CommBuffer A pointer to a collection of data in memory that will
218 be conveyed from a non-SMM environment into an SMM environment.
219 @param CommBufferSize The size of the CommBuffer.
226 SmmReadyToBootHandler (
227 IN EFI_HANDLE DispatchHandle
,
228 IN CONST VOID
*Context
, OPTIONAL
229 IN OUT VOID
*CommBuffer
, OPTIONAL
230 IN OUT UINTN
*CommBufferSize OPTIONAL
234 EFI_HANDLE SmmHandle
;
237 // Install SMM Ready To Boot protocol.
240 Status
= SmmInstallProtocolInterface (
242 &gEdkiiSmmReadyToBootProtocolGuid
,
243 EFI_NATIVE_INTERFACE
,
247 SmiHandlerUnRegister (DispatchHandle
);
253 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
254 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
255 Software SMIs that are nor required after SMRAM is locked and installs the
256 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
257 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed
258 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
260 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
261 @param Context Points to an optional handler context which was specified when the handler was registered.
262 @param CommBuffer A pointer to a collection of data in memory that will
263 be conveyed from a non-SMM environment into an SMM environment.
264 @param CommBufferSize The size of the CommBuffer.
271 SmmReadyToLockHandler (
272 IN EFI_HANDLE DispatchHandle
,
273 IN CONST VOID
*Context
, OPTIONAL
274 IN OUT VOID
*CommBuffer
, OPTIONAL
275 IN OUT UINTN
*CommBufferSize OPTIONAL
280 EFI_HANDLE SmmHandle
;
284 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
286 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
287 if (mSmmCoreSmiHandlers
[Index
].UnRegister
) {
288 SmiHandlerUnRegister (mSmmCoreSmiHandlers
[Index
].DispatchHandle
);
293 // Install SMM Ready to lock protocol
296 Status
= SmmInstallProtocolInterface (
298 &gEfiSmmReadyToLockProtocolGuid
,
299 EFI_NATIVE_INTERFACE
,
304 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
306 Status
= SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid
, NULL
, &Interface
);
309 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
312 if (EFI_ERROR (Status
)) {
313 DEBUG ((DEBUG_ERROR
, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
318 // Assert if the CPU I/O 2 Protocol is not installed
320 ASSERT_EFI_ERROR (Status
);
323 // Display any drivers that were not dispatched because dependency expression
324 // evaluated to false if this is a debug build
327 SmmDisplayDiscoveredNotDispatched ();
331 // Not allowed to use gST or gBS after lock
336 SmramProfileReadyToLock ();
342 Software SMI handler that is called when the EndOfDxe event is signalled.
343 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
344 platform code will invoke 3rd part code.
346 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
347 @param Context Points to an optional handler context which was specified when the handler was registered.
348 @param CommBuffer A pointer to a collection of data in memory that will
349 be conveyed from a non-SMM environment into an SMM environment.
350 @param CommBufferSize The size of the CommBuffer.
358 IN EFI_HANDLE DispatchHandle
,
359 IN CONST VOID
*Context
, OPTIONAL
360 IN OUT VOID
*CommBuffer
, OPTIONAL
361 IN OUT UINTN
*CommBufferSize OPTIONAL
365 EFI_HANDLE SmmHandle
;
367 DEBUG ((EFI_D_INFO
, "SmmEndOfDxeHandler\n"));
369 // Install SMM EndOfDxe protocol
372 Status
= SmmInstallProtocolInterface (
374 &gEfiSmmEndOfDxeProtocolGuid
,
375 EFI_NATIVE_INTERFACE
,
382 The main entry point to SMM Foundation.
384 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
386 @param SmmEntryContext Processor information and functionality
387 needed by SMM Foundation.
393 IN CONST EFI_SMM_ENTRY_CONTEXT
*SmmEntryContext
397 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
398 BOOLEAN InLegacyBoot
;
400 PERF_START (NULL
, "SMM", NULL
, 0) ;
403 // Update SMST using the context
405 CopyMem (&gSmmCoreSmst
.SmmStartupThisAp
, SmmEntryContext
, sizeof (EFI_SMM_ENTRY_CONTEXT
));
408 // Call platform hook before Smm Dispatch
410 PlatformHookBeforeSmmDispatch ();
413 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
415 InLegacyBoot
= mInLegacyBoot
;
418 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
420 gSmmCorePrivate
->InSmm
= TRUE
;
423 // Check to see if this is a Synchronous SMI sent through the SMM Communication
424 // Protocol or an Asynchronous SMI
426 if (gSmmCorePrivate
->CommunicationBuffer
!= NULL
) {
428 // Synchronous SMI for SMM Core or request from Communicate protocol
430 if (!SmmIsBufferOutsideSmmValid ((UINTN
)gSmmCorePrivate
->CommunicationBuffer
, gSmmCorePrivate
->BufferSize
)) {
432 // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
434 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
435 gSmmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
437 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)gSmmCorePrivate
->CommunicationBuffer
;
438 gSmmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
440 &CommunicateHeader
->HeaderGuid
,
442 CommunicateHeader
->Data
,
443 &gSmmCorePrivate
->BufferSize
446 // Update CommunicationBuffer, BufferSize and ReturnStatus
447 // Communicate service finished, reset the pointer to CommBuffer to NULL
449 gSmmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
450 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
451 gSmmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
457 // Process Asynchronous SMI sources
459 SmiManage (NULL
, NULL
, NULL
, NULL
);
462 // Call platform hook after Smm Dispatch
464 PlatformHookAfterSmmDispatch ();
467 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
471 // Clear the InSmm flag as we are going to leave SMM
473 gSmmCorePrivate
->InSmm
= FALSE
;
476 PERF_END (NULL
, "SMM", NULL
, 0) ;
480 The Entry Point for SMM Core
482 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
483 EntryPoint on the SMI vector.
485 Note: This function is called for both DXE invocation and SMRAM invocation.
487 @param ImageHandle The firmware allocated handle for the EFI image.
488 @param SystemTable A pointer to the EFI System Table.
490 @retval EFI_SUCCESS The entry point is executed successfully.
491 @retval Other Some error occurred when executing this entry point.
497 IN EFI_HANDLE ImageHandle
,
498 IN EFI_SYSTEM_TABLE
*SystemTable
505 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
507 gSmmCorePrivate
= (SMM_CORE_PRIVATE_DATA
*)ImageHandle
;
510 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
512 gSmmCorePrivate
->Smst
= &gSmmCoreSmst
;
513 gSmmCorePrivate
->SmmEntryPoint
= SmmEntryPoint
;
516 // No need to initialize memory service.
517 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
518 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
524 // Copy FullSmramRanges to SMRAM
526 mFullSmramRangeCount
= gSmmCorePrivate
->FullSmramRangeCount
;
527 mFullSmramRanges
= AllocatePool (mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
528 ASSERT (mFullSmramRanges
!= NULL
);
529 CopyMem (mFullSmramRanges
, gSmmCorePrivate
->FullSmramRanges
, mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
532 // Register all SMI Handlers required by the SMM Core
534 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
535 Status
= SmiHandlerRegister (
536 mSmmCoreSmiHandlers
[Index
].Handler
,
537 mSmmCoreSmiHandlers
[Index
].HandlerType
,
538 &mSmmCoreSmiHandlers
[Index
].DispatchHandle
540 ASSERT_EFI_ERROR (Status
);
543 RegisterSmramProfileHandler ();