2 MM Core Main Entry Point
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 - 2018, 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
= {
37 // The table header for the MMST.
40 EFI_MM_SYSTEM_TABLE_REVISION
,
41 sizeof (gMmCoreMmst
.Hdr
)
47 // MmInstallConfigurationTable
48 MmInstallConfigurationTable
,
52 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
, // MmMemRead
53 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
// MmMemWrite
56 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
, // MmIoRead
57 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
// MmIoWrite
60 // Runtime memory services
66 NULL
, // MmStartupThisAp
67 0, // CurrentlyExecutingCpu
69 NULL
, // CpuSaveStateSize
71 0, // NumberOfTableEntries
72 NULL
, // MmConfigurationTable
73 MmInstallProtocolInterface
,
74 MmUninstallProtocolInterface
,
76 MmRegisterProtocolNotify
,
85 // Table of MMI Handlers that are registered by the MM Core when it is initialized
87 MM_CORE_MMI_HANDLERS mMmCoreMmiHandlers
[] = {
88 { MmReadyToLockHandler
, &gEfiDxeMmReadyToLockProtocolGuid
, NULL
, TRUE
},
89 { MmEndOfDxeHandler
, &gEfiEndOfDxeEventGroupGuid
, NULL
, FALSE
},
90 { MmExitBootServiceHandler
,&gEfiEventExitBootServicesGuid
, NULL
, FALSE
},
91 { MmReadyToBootHandler
, &gEfiEventReadyToBootGuid
, NULL
, FALSE
},
92 { NULL
, NULL
, NULL
, FALSE
},
95 EFI_SYSTEM_TABLE
*mEfiSystemTable
;
96 UINTN mMmramRangeCount
;
97 EFI_MMRAM_DESCRIPTOR
*mMmramRanges
;
100 Place holder function until all the MM System Table Service are available.
102 Note: This function is only used by MMRAM invocation. It is never used by DXE invocation.
104 @param Arg1 Undefined
105 @param Arg2 Undefined
106 @param Arg3 Undefined
107 @param Arg4 Undefined
108 @param Arg5 Undefined
110 @return EFI_NOT_AVAILABLE_YET
115 MmEfiNotAvailableYetArg5 (
124 // This function should never be executed. If it does, then the architectural protocols
125 // have not been designed correctly.
127 return EFI_NOT_AVAILABLE_YET
;
131 Software MMI handler that is called when a ExitBoot Service event is signaled.
133 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
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-MM environment into an MM environment.
137 @param CommBufferSize The size of the CommBuffer.
144 MmExitBootServiceHandler (
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_STATUS Status
= EFI_SUCCESS
;
153 STATIC BOOLEAN mInExitBootServices
= FALSE
;
155 if (!mInExitBootServices
) {
157 Status
= MmInstallProtocolInterface (
159 &gEfiEventExitBootServicesGuid
,
160 EFI_NATIVE_INTERFACE
,
164 mInExitBootServices
= TRUE
;
169 Software MMI handler that is called when a ExitBoot Service event is signaled.
171 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
172 @param Context Points to an optional handler context which was specified when the handler was registered.
173 @param CommBuffer A pointer to a collection of data in memory that will
174 be conveyed from a non-MM environment into an MM environment.
175 @param CommBufferSize The size of the CommBuffer.
182 MmReadyToBootHandler (
183 IN EFI_HANDLE DispatchHandle
,
184 IN CONST VOID
*Context
, OPTIONAL
185 IN OUT VOID
*CommBuffer
, OPTIONAL
186 IN OUT UINTN
*CommBufferSize OPTIONAL
190 EFI_STATUS Status
= EFI_SUCCESS
;
191 STATIC BOOLEAN mInReadyToBoot
= FALSE
;
193 if (!mInReadyToBoot
) {
195 Status
= MmInstallProtocolInterface (
197 &gEfiEventReadyToBootGuid
,
198 EFI_NATIVE_INTERFACE
,
202 mInReadyToBoot
= TRUE
;
207 Software MMI handler that is called when the DxeMmReadyToLock protocol is added
208 or if gEfiEventReadyToBootGuid is signaled. This function unregisters the
209 Software SMIs that are nor required after MMRAM is locked and installs the
210 MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
213 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
214 @param Context Points to an optional handler context which was specified when the handler was registered.
215 @param CommBuffer A pointer to a collection of data in memory that will
216 be conveyed from a non-MM environment into an MM environment.
217 @param CommBufferSize The size of the CommBuffer.
224 MmReadyToLockHandler (
225 IN EFI_HANDLE DispatchHandle
,
226 IN CONST VOID
*Context
, OPTIONAL
227 IN OUT VOID
*CommBuffer
, OPTIONAL
228 IN OUT UINTN
*CommBufferSize OPTIONAL
235 DEBUG ((DEBUG_INFO
, "MmReadyToLockHandler\n"));
238 // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
240 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
241 if (mMmCoreMmiHandlers
[Index
].UnRegister
) {
242 MmiHandlerUnRegister (mMmCoreMmiHandlers
[Index
].DispatchHandle
);
247 // Install MM Ready to lock protocol
250 Status
= MmInstallProtocolInterface (
252 &gEfiMmReadyToLockProtocolGuid
,
253 EFI_NATIVE_INTERFACE
,
258 // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
260 //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
263 // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
265 //if (EFI_ERROR (Status)) {
266 //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
271 // Assert if the CPU I/O 2 Protocol is not installed
273 //ASSERT_EFI_ERROR (Status);
276 // Display any drivers that were not dispatched because dependency expression
277 // evaluated to false if this is a debug build
279 //MmDisplayDiscoveredNotDispatched ();
285 Software MMI handler that is called when the EndOfDxe event is signaled.
286 This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
287 platform code will invoke 3rd part code.
289 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
290 @param Context Points to an optional handler context which was specified when the handler was registered.
291 @param CommBuffer A pointer to a collection of data in memory that will
292 be conveyed from a non-MM environment into an MM environment.
293 @param CommBufferSize The size of the CommBuffer.
301 IN EFI_HANDLE DispatchHandle
,
302 IN CONST VOID
*Context
, OPTIONAL
303 IN OUT VOID
*CommBuffer
, OPTIONAL
304 IN OUT UINTN
*CommBufferSize OPTIONAL
310 DEBUG ((DEBUG_INFO
, "MmEndOfDxeHandler\n"));
312 // Install MM EndOfDxe protocol
315 Status
= MmInstallProtocolInterface (
317 &gEfiMmEndOfDxeProtocolGuid
,
318 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 occured, 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"));
417 MmConfigurationMmNotify (
418 IN CONST EFI_GUID
*Protocol
,
424 EFI_MM_CONFIGURATION_PROTOCOL
*MmConfiguration
;
426 DEBUG ((DEBUG_INFO
, "MmConfigurationMmNotify(%g) - %x\n", Protocol
, Interface
));
428 MmConfiguration
= Interface
;
431 // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
433 Status
= MmConfiguration
->RegisterMmEntry (MmConfiguration
, (EFI_MM_ENTRY_POINT
)(UINTN
)gMmCorePrivate
->MmEntryPoint
);
434 ASSERT_EFI_ERROR (Status
);
437 // Set flag to indicate that the MM Entry Point has been registered which
438 // means that MMIs are now fully operational.
440 gMmCorePrivate
->MmEntryPointRegistered
= TRUE
;
443 // Print debug message showing MM Core entry point address.
445 DEBUG ((DEBUG_INFO
, "MM Core registered MM Entry Point address %p\n", (VOID
*)(UINTN
)gMmCorePrivate
->MmEntryPoint
));
454 EFI_PEI_HOB_POINTERS Hob
;
456 ASSERT (HobStart
!= NULL
);
458 Hob
.Raw
= (UINT8
*) HobStart
;
459 while (!END_OF_HOB_LIST (Hob
)) {
460 Hob
.Raw
= GET_NEXT_HOB (Hob
);
463 // Need plus END_OF_HOB_LIST
465 return (UINTN
)Hob
.Raw
- (UINTN
)HobStart
+ sizeof (EFI_HOB_GENERIC_HEADER
);
469 The Entry Point for MM Core
471 Install DXE Protocols and reload MM Core into MMRAM and register MM Core
472 EntryPoint on the MMI vector.
474 Note: This function is called for both DXE invocation and MMRAM invocation.
476 @param ImageHandle The firmware allocated handle for the EFI image.
477 @param SystemTable A pointer to the EFI System Table.
479 @retval EFI_SUCCESS The entry point is executed successfully.
480 @retval Other Some error occurred when executing this entry point.
494 EFI_HOB_GUID_TYPE
*GuidHob
;
495 MM_CORE_DATA_HOB_DATA
*DataInHob
;
496 EFI_HOB_GUID_TYPE
*MmramRangesHob
;
497 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK
*MmramRangesHobData
;
498 EFI_MMRAM_DESCRIPTOR
*MmramRanges
;
499 UINT32 MmramRangeCount
;
500 EFI_HOB_FIRMWARE_VOLUME
*BfvHob
;
502 ProcessLibraryConstructorList (HobStart
, &gMmCoreMmst
);
504 DEBUG ((DEBUG_INFO
, "MmMain - 0x%x\n", HobStart
));
507 // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
508 // structure in the Hoblist. This choice will govern how boot information is
511 GuidHob
= GetNextGuidHob (&gMmCoreDataHobGuid
, HobStart
);
512 if (GuidHob
== NULL
) {
514 // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
517 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof (MM_CORE_PRIVATE_DATA
)));
518 SetMem ((VOID
*)(UINTN
)gMmCorePrivate
, sizeof (MM_CORE_PRIVATE_DATA
), 0);
519 gMmCorePrivate
->Signature
= MM_CORE_PRIVATE_DATA_SIGNATURE
;
520 gMmCorePrivate
->MmEntryPointRegistered
= FALSE
;
521 gMmCorePrivate
->InMm
= FALSE
;
522 gMmCorePrivate
->ReturnStatus
= EFI_SUCCESS
;
525 // Extract the MMRAM ranges from the MMRAM descriptor HOB
527 MmramRangesHob
= GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid
, HobStart
);
528 if (MmramRangesHob
== NULL
)
529 return EFI_UNSUPPORTED
;
531 MmramRangesHobData
= GET_GUID_HOB_DATA (MmramRangesHob
);
532 ASSERT (MmramRangesHobData
!= NULL
);
533 MmramRanges
= MmramRangesHobData
->Descriptor
;
534 MmramRangeCount
= MmramRangesHobData
->NumberOfMmReservedRegions
;
535 ASSERT (MmramRanges
);
536 ASSERT (MmramRangeCount
);
539 // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
540 // code relies on them being present there
542 gMmCorePrivate
->MmramRangeCount
= MmramRangeCount
;
543 gMmCorePrivate
->MmramRanges
=
544 (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
545 ASSERT (gMmCorePrivate
->MmramRanges
!= 0);
547 (VOID
*)(UINTN
)gMmCorePrivate
->MmramRanges
,
549 MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
)
552 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
553 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*)(UINTN
)DataInHob
->Address
;
554 MmramRanges
= (EFI_MMRAM_DESCRIPTOR
*)(UINTN
)gMmCorePrivate
->MmramRanges
;
555 MmramRangeCount
= gMmCorePrivate
->MmramRangeCount
;
559 // Print the MMRAM ranges passed by the caller
561 DEBUG ((DEBUG_INFO
, "MmramRangeCount - 0x%x\n", MmramRangeCount
));
562 for (Index
= 0; Index
< MmramRangeCount
; Index
++) {
563 DEBUG ((DEBUG_INFO
, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index
,
564 MmramRanges
[Index
].CpuStart
,
565 MmramRanges
[Index
].PhysicalSize
));
569 // Copy the MMRAM ranges into private MMRAM
571 mMmramRangeCount
= MmramRangeCount
;
572 DEBUG ((DEBUG_INFO
, "mMmramRangeCount - 0x%x\n", mMmramRangeCount
));
573 mMmramRanges
= AllocatePool (mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
574 DEBUG ((DEBUG_INFO
, "mMmramRanges - 0x%x\n", mMmramRanges
));
575 ASSERT (mMmramRanges
!= NULL
);
576 CopyMem (mMmramRanges
, (VOID
*)(UINTN
)MmramRanges
, mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
579 // Get Boot Firmware Volume address from the BFV Hob
581 BfvHob
= GetFirstHob (EFI_HOB_TYPE_FV
);
582 if (BfvHob
!= NULL
) {
583 DEBUG ((DEBUG_INFO
, "BFV address - 0x%x\n", BfvHob
->BaseAddress
));
584 DEBUG ((DEBUG_INFO
, "BFV size - 0x%x\n", BfvHob
->Length
));
585 gMmCorePrivate
->StandaloneBfvAddress
= BfvHob
->BaseAddress
;
588 gMmCorePrivate
->Mmst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&gMmCoreMmst
;
589 gMmCorePrivate
->MmEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MmEntryPoint
;
592 // No need to initialize memory service.
593 // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
594 // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
597 DEBUG ((DEBUG_INFO
, "MmInstallConfigurationTable For HobList\n"));
601 HobSize
= GetHobListSize (HobStart
);
602 DEBUG ((DEBUG_INFO
, "HobSize - 0x%x\n", HobSize
));
603 MmHobStart
= AllocatePool (HobSize
);
604 DEBUG ((DEBUG_INFO
, "MmHobStart - 0x%x\n", MmHobStart
));
605 ASSERT (MmHobStart
!= NULL
);
606 CopyMem (MmHobStart
, HobStart
, HobSize
);
607 Status
= MmInstallConfigurationTable (&gMmCoreMmst
, &gEfiHobListGuid
, MmHobStart
, HobSize
);
608 ASSERT_EFI_ERROR (Status
);
611 // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
612 // use it to register the MM Foundation entrypoint
614 DEBUG ((DEBUG_INFO
, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
615 Status
= MmRegisterProtocolNotify (
616 &gEfiMmConfigurationProtocolGuid
,
617 MmConfigurationMmNotify
,
620 ASSERT_EFI_ERROR (Status
);
623 // Dispatch standalone BFV
625 DEBUG ((DEBUG_INFO
, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate
->StandaloneBfvAddress
));
626 if (gMmCorePrivate
->StandaloneBfvAddress
!= 0) {
627 MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)gMmCorePrivate
->StandaloneBfvAddress
);
632 // Register all handlers in the core table
634 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
635 Status
= MmiHandlerRegister (
636 mMmCoreMmiHandlers
[Index
].Handler
,
637 mMmCoreMmiHandlers
[Index
].HandlerType
,
638 &mMmCoreMmiHandlers
[Index
].DispatchHandle
640 DEBUG ((DEBUG_INFO
, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers
[Index
].HandlerType
, Status
));
643 DEBUG ((DEBUG_INFO
, "MmMain Done!\n"));