2 MM Core Main Entry Point
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "StandaloneMmCore.h"
13 MmCoreFfsFindMmDriver (
14 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
23 // Globals used to initialize the protocol
25 EFI_HANDLE mMmCpuHandle
= NULL
;
28 // Physical pointer to private structure shared between MM IPL and the MM Core
30 MM_CORE_PRIVATE_DATA
*gMmCorePrivate
;
33 // MM Core global variable for MM System Table. Only accessed as a physical structure in MMRAM.
35 EFI_MM_SYSTEM_TABLE gMmCoreMmst
= {
36 // The table header for the MMST.
39 EFI_MM_SYSTEM_TABLE_REVISION
,
40 sizeof (gMmCoreMmst
.Hdr
)
46 // MmInstallConfigurationTable
47 MmInstallConfigurationTable
,
51 (EFI_MM_CPU_IO
)MmEfiNotAvailableYetArg5
, // MmMemRead
52 (EFI_MM_CPU_IO
)MmEfiNotAvailableYetArg5
// MmMemWrite
55 (EFI_MM_CPU_IO
)MmEfiNotAvailableYetArg5
, // MmIoRead
56 (EFI_MM_CPU_IO
)MmEfiNotAvailableYetArg5
// MmIoWrite
59 // Runtime memory services
65 NULL
, // MmStartupThisAp
66 0, // CurrentlyExecutingCpu
68 NULL
, // CpuSaveStateSize
70 0, // NumberOfTableEntries
71 NULL
, // MmConfigurationTable
72 MmInstallProtocolInterface
,
73 MmUninstallProtocolInterface
,
75 MmRegisterProtocolNotify
,
84 // Table of MMI Handlers that are registered by the MM Core when it is initialized
86 MM_CORE_MMI_HANDLERS mMmCoreMmiHandlers
[] = {
87 { MmReadyToLockHandler
, &gEfiDxeMmReadyToLockProtocolGuid
, NULL
, TRUE
},
88 { MmEndOfDxeHandler
, &gEfiEndOfDxeEventGroupGuid
, NULL
, FALSE
},
89 { MmExitBootServiceHandler
, &gEfiEventExitBootServicesGuid
, NULL
, FALSE
},
90 { MmReadyToBootHandler
, &gEfiEventReadyToBootGuid
, NULL
, FALSE
},
91 { NULL
, NULL
, NULL
, FALSE
},
94 EFI_SYSTEM_TABLE
*mEfiSystemTable
;
95 UINTN mMmramRangeCount
;
96 EFI_MMRAM_DESCRIPTOR
*mMmramRanges
;
99 Place holder function until all the MM System Table Service are available.
101 Note: This function is only used by MMRAM invocation. It is never used by DXE invocation.
103 @param Arg1 Undefined
104 @param Arg2 Undefined
105 @param Arg3 Undefined
106 @param Arg4 Undefined
107 @param Arg5 Undefined
109 @return EFI_NOT_AVAILABLE_YET
114 MmEfiNotAvailableYetArg5 (
123 // This function should never be executed. If it does, then the architectural protocols
124 // have not been designed correctly.
126 return EFI_NOT_AVAILABLE_YET
;
130 Software MMI handler that is called when a ExitBoot Service event is signaled.
132 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
133 @param Context Points to an optional handler context which was specified when the handler was registered.
134 @param CommBuffer A pointer to a collection of data in memory that will
135 be conveyed from a non-MM environment into an MM environment.
136 @param CommBufferSize The size of the CommBuffer.
143 MmExitBootServiceHandler (
144 IN EFI_HANDLE DispatchHandle
,
145 IN CONST VOID
*Context OPTIONAL
,
146 IN OUT VOID
*CommBuffer OPTIONAL
,
147 IN OUT UINTN
*CommBufferSize OPTIONAL
152 STATIC BOOLEAN mInExitBootServices
= FALSE
;
154 Status
= EFI_SUCCESS
;
155 if (!mInExitBootServices
) {
157 Status
= MmInstallProtocolInterface (
159 &gEfiEventExitBootServicesGuid
,
160 EFI_NATIVE_INTERFACE
,
165 mInExitBootServices
= TRUE
;
170 Software MMI handler that is called when a ExitBoot Service event is signaled.
172 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
173 @param Context Points to an optional handler context which was specified when the handler was registered.
174 @param CommBuffer A pointer to a collection of data in memory that will
175 be conveyed from a non-MM environment into an MM environment.
176 @param CommBufferSize The size of the CommBuffer.
183 MmReadyToBootHandler (
184 IN EFI_HANDLE DispatchHandle
,
185 IN CONST VOID
*Context OPTIONAL
,
186 IN OUT VOID
*CommBuffer OPTIONAL
,
187 IN OUT UINTN
*CommBufferSize OPTIONAL
192 STATIC BOOLEAN mInReadyToBoot
= FALSE
;
194 Status
= EFI_SUCCESS
;
195 if (!mInReadyToBoot
) {
197 Status
= MmInstallProtocolInterface (
199 &gEfiEventReadyToBootGuid
,
200 EFI_NATIVE_INTERFACE
,
205 mInReadyToBoot
= TRUE
;
210 Software MMI handler that is called when the DxeMmReadyToLock protocol is added
211 or if gEfiEventReadyToBootGuid is signaled. This function unregisters the
212 Software SMIs that are nor required after MMRAM is locked and installs the
213 MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
216 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
217 @param Context Points to an optional handler context which was specified when the handler was registered.
218 @param CommBuffer A pointer to a collection of data in memory that will
219 be conveyed from a non-MM environment into an MM environment.
220 @param CommBufferSize The size of the CommBuffer.
227 MmReadyToLockHandler (
228 IN EFI_HANDLE DispatchHandle
,
229 IN CONST VOID
*Context OPTIONAL
,
230 IN OUT VOID
*CommBuffer OPTIONAL
,
231 IN OUT UINTN
*CommBufferSize OPTIONAL
238 DEBUG ((DEBUG_INFO
, "MmReadyToLockHandler\n"));
241 // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
243 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
244 if (mMmCoreMmiHandlers
[Index
].UnRegister
) {
245 MmiHandlerUnRegister (mMmCoreMmiHandlers
[Index
].DispatchHandle
);
250 // Install MM Ready to lock protocol
253 Status
= MmInstallProtocolInterface (
255 &gEfiMmReadyToLockProtocolGuid
,
256 EFI_NATIVE_INTERFACE
,
261 // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
263 // Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
266 // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
268 // if (EFI_ERROR (Status)) {
269 // DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
273 // Assert if the CPU I/O 2 Protocol is not installed
275 // ASSERT_EFI_ERROR (Status);
278 // Display any drivers that were not dispatched because dependency expression
279 // evaluated to false if this is a debug build
281 // MmDisplayDiscoveredNotDispatched ();
287 Software MMI handler that is called when the EndOfDxe event is signaled.
288 This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
289 platform code will invoke 3rd part code.
291 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
292 @param Context Points to an optional handler context which was specified when the handler was registered.
293 @param CommBuffer A pointer to a collection of data in memory that will
294 be conveyed from a non-MM environment into an MM environment.
295 @param CommBufferSize The size of the CommBuffer.
303 IN EFI_HANDLE DispatchHandle
,
304 IN CONST VOID
*Context OPTIONAL
,
305 IN OUT VOID
*CommBuffer OPTIONAL
,
306 IN OUT UINTN
*CommBufferSize OPTIONAL
312 DEBUG ((DEBUG_INFO
, "MmEndOfDxeHandler\n"));
314 // Install MM EndOfDxe protocol
317 Status
= MmInstallProtocolInterface (
319 &gEfiMmEndOfDxeProtocolGuid
,
320 EFI_NATIVE_INTERFACE
,
327 The main entry point to MM Foundation.
329 Note: This function is only used by MMRAM invocation. It is never used by DXE invocation.
331 @param MmEntryContext Processor information and functionality
332 needed by MM Foundation.
338 IN CONST EFI_MM_ENTRY_CONTEXT
*MmEntryContext
342 EFI_MM_COMMUNICATE_HEADER
*CommunicateHeader
;
344 DEBUG ((DEBUG_INFO
, "MmEntryPoint ...\n"));
347 // Update MMST using the context
349 CopyMem (&gMmCoreMmst
.MmStartupThisAp
, MmEntryContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
352 // Call platform hook before Mm Dispatch
354 // PlatformHookBeforeMmDispatch ();
357 // If a legacy boot has occurred, then make sure gMmCorePrivate is not accessed
361 // TBD: Mark the InMm flag as TRUE
363 gMmCorePrivate
->InMm
= TRUE
;
366 // Check to see if this is a Synchronous MMI sent through the MM Communication
367 // Protocol or an Asynchronous MMI
369 if (gMmCorePrivate
->CommunicationBuffer
!= 0) {
371 // Synchronous MMI for MM Core or request from Communicate protocol
373 if (!MmIsBufferOutsideMmValid ((UINTN
)gMmCorePrivate
->CommunicationBuffer
, gMmCorePrivate
->BufferSize
)) {
375 // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
377 gMmCorePrivate
->CommunicationBuffer
= 0;
378 gMmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
380 CommunicateHeader
= (EFI_MM_COMMUNICATE_HEADER
*)(UINTN
)gMmCorePrivate
->CommunicationBuffer
;
381 gMmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER
, Data
);
383 &CommunicateHeader
->HeaderGuid
,
385 CommunicateHeader
->Data
,
386 (UINTN
*)&gMmCorePrivate
->BufferSize
389 // Update CommunicationBuffer, BufferSize and ReturnStatus
390 // Communicate service finished, reset the pointer to CommBuffer to NULL
392 gMmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER
, Data
);
393 gMmCorePrivate
->CommunicationBuffer
= 0;
394 gMmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
399 // Process Asynchronous MMI sources
401 MmiManage (NULL
, NULL
, NULL
, NULL
);
404 // TBD: Do not use private data structure ?
408 // Clear the InMm flag as we are going to leave MM
410 gMmCorePrivate
->InMm
= FALSE
;
412 DEBUG ((DEBUG_INFO
, "MmEntryPoint Done\n"));
415 /** Register the MM Entry Point provided by the MM Core with the
416 MM Configuration protocol.
418 @param [in] Protocol Pointer to the protocol.
419 @param [in] Interface Pointer to the MM Configuration protocol.
420 @param [in] Handle Handle.
422 @retval EFI_SUCCESS Success.
426 MmConfigurationMmNotify (
427 IN CONST EFI_GUID
*Protocol
,
433 EFI_MM_CONFIGURATION_PROTOCOL
*MmConfiguration
;
435 DEBUG ((DEBUG_INFO
, "MmConfigurationMmNotify(%g) - %x\n", Protocol
, Interface
));
437 MmConfiguration
= Interface
;
440 // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
442 Status
= MmConfiguration
->RegisterMmEntry (MmConfiguration
, (EFI_MM_ENTRY_POINT
)(UINTN
)gMmCorePrivate
->MmEntryPoint
);
443 ASSERT_EFI_ERROR (Status
);
446 // Set flag to indicate that the MM Entry Point has been registered which
447 // means that MMIs are now fully operational.
449 gMmCorePrivate
->MmEntryPointRegistered
= TRUE
;
452 // Print debug message showing MM Core entry point address.
454 DEBUG ((DEBUG_INFO
, "MM Core registered MM Entry Point address %p\n", (VOID
*)(UINTN
)gMmCorePrivate
->MmEntryPoint
));
458 /** Returns the HOB list size.
460 @param [in] HobStart Pointer to the start of the HOB list.
462 @retval Size of the HOB list.
469 EFI_PEI_HOB_POINTERS Hob
;
471 ASSERT (HobStart
!= NULL
);
473 Hob
.Raw
= (UINT8
*)HobStart
;
474 while (!END_OF_HOB_LIST (Hob
)) {
475 Hob
.Raw
= GET_NEXT_HOB (Hob
);
479 // Need plus END_OF_HOB_LIST
481 return (UINTN
)Hob
.Raw
- (UINTN
)HobStart
+ sizeof (EFI_HOB_GENERIC_HEADER
);
485 The Entry Point for MM Core
487 Install DXE Protocols and reload MM Core into MMRAM and register MM Core
488 EntryPoint on the MMI vector.
490 Note: This function is called for both DXE invocation and MMRAM invocation.
492 @param HobStart Pointer to the start of the HOB list.
494 @retval EFI_SUCCESS Success.
495 @retval EFI_UNSUPPORTED Unsupported operation.
508 EFI_HOB_GUID_TYPE
*GuidHob
;
509 MM_CORE_DATA_HOB_DATA
*DataInHob
;
510 EFI_HOB_GUID_TYPE
*MmramRangesHob
;
511 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK
*MmramRangesHobData
;
512 EFI_MMRAM_DESCRIPTOR
*MmramRanges
;
513 UINTN MmramRangeCount
;
514 EFI_HOB_FIRMWARE_VOLUME
*BfvHob
;
516 ProcessLibraryConstructorList (HobStart
, &gMmCoreMmst
);
518 DEBUG ((DEBUG_INFO
, "MmMain - 0x%x\n", HobStart
));
521 // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
522 // structure in the Hoblist. This choice will govern how boot information is
525 GuidHob
= GetNextGuidHob (&gMmCoreDataHobGuid
, HobStart
);
526 if (GuidHob
== NULL
) {
528 // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
531 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*)AllocateRuntimePages (EFI_SIZE_TO_PAGES (sizeof (MM_CORE_PRIVATE_DATA
)));
532 SetMem ((VOID
*)(UINTN
)gMmCorePrivate
, sizeof (MM_CORE_PRIVATE_DATA
), 0);
533 gMmCorePrivate
->Signature
= MM_CORE_PRIVATE_DATA_SIGNATURE
;
534 gMmCorePrivate
->MmEntryPointRegistered
= FALSE
;
535 gMmCorePrivate
->InMm
= FALSE
;
536 gMmCorePrivate
->ReturnStatus
= EFI_SUCCESS
;
539 // Extract the MMRAM ranges from the MMRAM descriptor HOB
541 MmramRangesHob
= GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid
, HobStart
);
542 if (MmramRangesHob
== NULL
) {
543 return EFI_UNSUPPORTED
;
546 MmramRangesHobData
= GET_GUID_HOB_DATA (MmramRangesHob
);
547 ASSERT (MmramRangesHobData
!= NULL
);
548 MmramRanges
= MmramRangesHobData
->Descriptor
;
549 MmramRangeCount
= (UINTN
)MmramRangesHobData
->NumberOfMmReservedRegions
;
550 ASSERT (MmramRanges
);
551 ASSERT (MmramRangeCount
);
554 // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
555 // code relies on them being present there
557 gMmCorePrivate
->MmramRangeCount
= (UINT64
)MmramRangeCount
;
558 gMmCorePrivate
->MmramRanges
=
559 (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
560 ASSERT (gMmCorePrivate
->MmramRanges
!= 0);
562 (VOID
*)(UINTN
)gMmCorePrivate
->MmramRanges
,
564 MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
)
567 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
568 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*)(UINTN
)DataInHob
->Address
;
569 MmramRanges
= (EFI_MMRAM_DESCRIPTOR
*)(UINTN
)gMmCorePrivate
->MmramRanges
;
570 MmramRangeCount
= (UINTN
)gMmCorePrivate
->MmramRangeCount
;
574 // Print the MMRAM ranges passed by the caller
576 DEBUG ((DEBUG_INFO
, "MmramRangeCount - 0x%x\n", MmramRangeCount
));
577 for (Index
= 0; Index
< MmramRangeCount
; Index
++) {
580 "MmramRanges[%d]: 0x%016lx - 0x%lx\n",
582 MmramRanges
[Index
].CpuStart
,
583 MmramRanges
[Index
].PhysicalSize
588 // Copy the MMRAM ranges into private MMRAM
590 mMmramRangeCount
= MmramRangeCount
;
591 DEBUG ((DEBUG_INFO
, "mMmramRangeCount - 0x%x\n", mMmramRangeCount
));
592 mMmramRanges
= AllocatePool (mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
593 DEBUG ((DEBUG_INFO
, "mMmramRanges - 0x%x\n", mMmramRanges
));
594 ASSERT (mMmramRanges
!= NULL
);
595 CopyMem (mMmramRanges
, (VOID
*)(UINTN
)MmramRanges
, mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
598 // Get Boot Firmware Volume address from the BFV Hob
600 BfvHob
= GetFirstHob (EFI_HOB_TYPE_FV
);
601 if (BfvHob
!= NULL
) {
602 DEBUG ((DEBUG_INFO
, "BFV address - 0x%x\n", BfvHob
->BaseAddress
));
603 DEBUG ((DEBUG_INFO
, "BFV size - 0x%x\n", BfvHob
->Length
));
604 gMmCorePrivate
->StandaloneBfvAddress
= BfvHob
->BaseAddress
;
607 gMmCorePrivate
->Mmst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&gMmCoreMmst
;
608 gMmCorePrivate
->MmEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MmEntryPoint
;
611 // No need to initialize memory service.
612 // It is done in the constructor of StandaloneMmCoreMemoryAllocationLib(),
613 // so that the library linked with StandaloneMmCore can use AllocatePool() in
616 DEBUG ((DEBUG_INFO
, "MmInstallConfigurationTable For HobList\n"));
620 HobSize
= GetHobListSize (HobStart
);
621 DEBUG ((DEBUG_INFO
, "HobSize - 0x%x\n", HobSize
));
622 MmHobStart
= AllocatePool (HobSize
);
623 DEBUG ((DEBUG_INFO
, "MmHobStart - 0x%x\n", MmHobStart
));
624 ASSERT (MmHobStart
!= NULL
);
625 CopyMem (MmHobStart
, HobStart
, HobSize
);
626 Status
= MmInstallConfigurationTable (&gMmCoreMmst
, &gEfiHobListGuid
, MmHobStart
, HobSize
);
627 ASSERT_EFI_ERROR (Status
);
630 // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
631 // use it to register the MM Foundation entrypoint
633 DEBUG ((DEBUG_INFO
, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
634 Status
= MmRegisterProtocolNotify (
635 &gEfiMmConfigurationProtocolGuid
,
636 MmConfigurationMmNotify
,
639 ASSERT_EFI_ERROR (Status
);
642 // Dispatch standalone BFV
644 DEBUG ((DEBUG_INFO
, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate
->StandaloneBfvAddress
));
645 if (gMmCorePrivate
->StandaloneBfvAddress
!= 0) {
646 MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)gMmCorePrivate
->StandaloneBfvAddress
);
651 // Register all handlers in the core table
653 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
654 Status
= MmiHandlerRegister (
655 mMmCoreMmiHandlers
[Index
].Handler
,
656 mMmCoreMmiHandlers
[Index
].HandlerType
,
657 &mMmCoreMmiHandlers
[Index
].DispatchHandle
659 DEBUG ((DEBUG_INFO
, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers
[Index
].HandlerType
, Status
));
662 DEBUG ((DEBUG_INFO
, "MmMain Done!\n"));