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
, TRUE
},
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 Determine if two buffers overlap in memory.
384 @param[in] Buff1 Pointer to first buffer
385 @param[in] Size1 Size of Buff1
386 @param[in] Buff2 Pointer to second buffer
387 @param[in] Size2 Size of Buff2
389 @retval TRUE Buffers overlap in memory.
390 @retval FALSE Buffer doesn't overlap.
394 InternalIsBufferOverlapped (
402 // If buff1's end is less than the start of buff2, then it's ok.
403 // Also, if buff1's start is beyond buff2's end, then it's ok.
405 if (((Buff1
+ Size1
) <= Buff2
) || (Buff1
>= (Buff2
+ Size2
))) {
413 The main entry point to SMM Foundation.
415 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
417 @param SmmEntryContext Processor information and functionality
418 needed by SMM Foundation.
424 IN CONST EFI_SMM_ENTRY_CONTEXT
*SmmEntryContext
428 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
429 BOOLEAN InLegacyBoot
;
430 BOOLEAN IsOverlapped
;
432 PERF_START (NULL
, "SMM", NULL
, 0) ;
435 // Update SMST using the context
437 CopyMem (&gSmmCoreSmst
.SmmStartupThisAp
, SmmEntryContext
, sizeof (EFI_SMM_ENTRY_CONTEXT
));
440 // Call platform hook before Smm Dispatch
442 PlatformHookBeforeSmmDispatch ();
445 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
447 InLegacyBoot
= mInLegacyBoot
;
450 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
452 gSmmCorePrivate
->InSmm
= TRUE
;
455 // Check to see if this is a Synchronous SMI sent through the SMM Communication
456 // Protocol or an Asynchronous SMI
458 if (gSmmCorePrivate
->CommunicationBuffer
!= NULL
) {
460 // Synchronous SMI for SMM Core or request from Communicate protocol
462 IsOverlapped
= InternalIsBufferOverlapped (
463 (UINT8
*) gSmmCorePrivate
->CommunicationBuffer
,
464 gSmmCorePrivate
->BufferSize
,
465 (UINT8
*) gSmmCorePrivate
,
466 sizeof (*gSmmCorePrivate
)
468 if (!SmmIsBufferOutsideSmmValid ((UINTN
)gSmmCorePrivate
->CommunicationBuffer
, gSmmCorePrivate
->BufferSize
) || IsOverlapped
) {
470 // If CommunicationBuffer is not in valid address scope,
471 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,
472 // return EFI_INVALID_PARAMETER
474 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
475 gSmmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
477 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)gSmmCorePrivate
->CommunicationBuffer
;
478 gSmmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
480 &CommunicateHeader
->HeaderGuid
,
482 CommunicateHeader
->Data
,
483 &gSmmCorePrivate
->BufferSize
486 // Update CommunicationBuffer, BufferSize and ReturnStatus
487 // Communicate service finished, reset the pointer to CommBuffer to NULL
489 gSmmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
490 gSmmCorePrivate
->CommunicationBuffer
= NULL
;
491 gSmmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
497 // Process Asynchronous SMI sources
499 SmiManage (NULL
, NULL
, NULL
, NULL
);
502 // Call platform hook after Smm Dispatch
504 PlatformHookAfterSmmDispatch ();
507 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
511 // Clear the InSmm flag as we are going to leave SMM
513 gSmmCorePrivate
->InSmm
= FALSE
;
516 PERF_END (NULL
, "SMM", NULL
, 0) ;
520 The Entry Point for SMM Core
522 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
523 EntryPoint on the SMI vector.
525 Note: This function is called for both DXE invocation and SMRAM invocation.
527 @param ImageHandle The firmware allocated handle for the EFI image.
528 @param SystemTable A pointer to the EFI System Table.
530 @retval EFI_SUCCESS The entry point is executed successfully.
531 @retval Other Some error occurred when executing this entry point.
537 IN EFI_HANDLE ImageHandle
,
538 IN EFI_SYSTEM_TABLE
*SystemTable
545 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
547 gSmmCorePrivate
= (SMM_CORE_PRIVATE_DATA
*)ImageHandle
;
550 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
552 gSmmCorePrivate
->Smst
= &gSmmCoreSmst
;
553 gSmmCorePrivate
->SmmEntryPoint
= SmmEntryPoint
;
556 // No need to initialize memory service.
557 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
558 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.
564 // Copy FullSmramRanges to SMRAM
566 mFullSmramRangeCount
= gSmmCorePrivate
->SmramRangeCount
;
567 mFullSmramRanges
= AllocatePool (mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
568 ASSERT (mFullSmramRanges
!= NULL
);
569 CopyMem (mFullSmramRanges
, gSmmCorePrivate
->SmramRanges
, mFullSmramRangeCount
* sizeof (EFI_SMRAM_DESCRIPTOR
));
572 // Register all SMI Handlers required by the SMM Core
574 for (Index
= 0; mSmmCoreSmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
575 Status
= SmiHandlerRegister (
576 mSmmCoreSmiHandlers
[Index
].Handler
,
577 mSmmCoreSmiHandlers
[Index
].HandlerType
,
578 &mSmmCoreSmiHandlers
[Index
].DispatchHandle
580 ASSERT_EFI_ERROR (Status
);
583 RegisterSmramProfileHandler ();