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 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "StandaloneMmCore.h"
19 MmCoreFfsFindMmDriver (
20 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
29 // Globals used to initialize the protocol
31 EFI_HANDLE mMmCpuHandle
= NULL
;
34 // Physical pointer to private structure shared between MM IPL and the MM Core
36 MM_CORE_PRIVATE_DATA
*gMmCorePrivate
;
39 // MM Core global variable for MM System Table. Only accessed as a physical structure in MMRAM.
41 EFI_MM_SYSTEM_TABLE gMmCoreMmst
= {
43 // The table header for the MMST.
46 EFI_MM_SYSTEM_TABLE_REVISION
,
47 sizeof (gMmCoreMmst
.Hdr
)
53 // MmInstallConfigurationTable
54 MmInstallConfigurationTable
,
58 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
, // MmMemRead
59 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
// MmMemWrite
62 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
, // MmIoRead
63 (EFI_MM_CPU_IO
) MmEfiNotAvailableYetArg5
// MmIoWrite
66 // Runtime memory services
72 NULL
, // MmStartupThisAp
73 0, // CurrentlyExecutingCpu
75 NULL
, // CpuSaveStateSize
77 0, // NumberOfTableEntries
78 NULL
, // MmConfigurationTable
79 MmInstallProtocolInterface
,
80 MmUninstallProtocolInterface
,
82 MmRegisterProtocolNotify
,
91 // Flag to determine if the platform has performed a legacy boot.
92 // If this flag is TRUE, then the runtime code and runtime data associated with the
93 // MM IPL are converted to free memory, so the MM Core must guarantee that is
94 // does not touch of the code/data associated with the MM IPL if this flag is TRUE.
96 BOOLEAN mInLegacyBoot
= FALSE
;
99 // Table of MMI Handlers that are registered by the MM Core when it is initialized
101 MM_CORE_MMI_HANDLERS mMmCoreMmiHandlers
[] = {
102 { MmFvDispatchHandler
, &gMmFvDispatchGuid
, NULL
, TRUE
},
103 { MmDriverDispatchHandler
, &gEfiEventDxeDispatchGuid
, NULL
, TRUE
},
104 { MmReadyToLockHandler
, &gEfiDxeMmReadyToLockProtocolGuid
, NULL
, TRUE
},
105 { MmEndOfDxeHandler
, &gEfiEndOfDxeEventGroupGuid
, NULL
, FALSE
},
106 { MmLegacyBootHandler
, &gEfiEventLegacyBootGuid
, NULL
, FALSE
},
107 { MmExitBootServiceHandler
,&gEfiEventExitBootServicesGuid
, NULL
, FALSE
},
108 { MmReadyToBootHandler
, &gEfiEventReadyToBootGuid
, NULL
, FALSE
},
109 { NULL
, NULL
, NULL
, FALSE
},
112 EFI_SYSTEM_TABLE
*mEfiSystemTable
;
113 UINTN mMmramRangeCount
;
114 EFI_MMRAM_DESCRIPTOR
*mMmramRanges
;
117 Place holder function until all the MM System Table Service are available.
119 Note: This function is only used by MMRAM invocation. It is never used by DXE invocation.
121 @param Arg1 Undefined
122 @param Arg2 Undefined
123 @param Arg3 Undefined
124 @param Arg4 Undefined
125 @param Arg5 Undefined
127 @return EFI_NOT_AVAILABLE_YET
132 MmEfiNotAvailableYetArg5 (
141 // This function should never be executed. If it does, then the architectural protocols
142 // have not been designed correctly.
144 return EFI_NOT_AVAILABLE_YET
;
148 Software MMI handler that is called when a Legacy Boot event is signaled. The MM
149 Core uses this signal to know that a Legacy Boot has been performed and that
150 gMmCorePrivate that is shared between the UEFI and MM execution environments can
151 not be accessed from MM anymore since that structure is considered free memory by
154 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
155 @param Context Points to an optional handler context which was specified when the handler was registered.
156 @param CommBuffer A pointer to a collection of data in memory that will
157 be conveyed from a non-MM environment into an MM environment.
158 @param CommBufferSize The size of the CommBuffer.
165 MmLegacyBootHandler (
166 IN EFI_HANDLE DispatchHandle
,
167 IN CONST VOID
*Context
, OPTIONAL
168 IN OUT VOID
*CommBuffer
, OPTIONAL
169 IN OUT UINTN
*CommBufferSize OPTIONAL
173 EFI_STATUS Status
= EFI_SUCCESS
;
175 if (!mInLegacyBoot
) {
177 Status
= MmInstallProtocolInterface (
179 &gEfiEventLegacyBootGuid
,
180 EFI_NATIVE_INTERFACE
,
184 mInLegacyBoot
= TRUE
;
189 Software MMI handler that is called when a ExitBoot Service event is signaled.
191 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
192 @param Context Points to an optional handler context which was specified when the handler was registered.
193 @param CommBuffer A pointer to a collection of data in memory that will
194 be conveyed from a non-MM environment into an MM environment.
195 @param CommBufferSize The size of the CommBuffer.
202 MmExitBootServiceHandler (
203 IN EFI_HANDLE DispatchHandle
,
204 IN CONST VOID
*Context
, OPTIONAL
205 IN OUT VOID
*CommBuffer
, OPTIONAL
206 IN OUT UINTN
*CommBufferSize OPTIONAL
210 EFI_STATUS Status
= EFI_SUCCESS
;
211 STATIC BOOLEAN mInExitBootServices
= FALSE
;
213 if (!mInExitBootServices
) {
215 Status
= MmInstallProtocolInterface (
217 &gEfiEventExitBootServicesGuid
,
218 EFI_NATIVE_INTERFACE
,
222 mInExitBootServices
= TRUE
;
227 Software MMI handler that is called when a ExitBoot Service event is signaled.
229 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
230 @param Context Points to an optional handler context which was specified when the handler was registered.
231 @param CommBuffer A pointer to a collection of data in memory that will
232 be conveyed from a non-MM environment into an MM environment.
233 @param CommBufferSize The size of the CommBuffer.
240 MmReadyToBootHandler (
241 IN EFI_HANDLE DispatchHandle
,
242 IN CONST VOID
*Context
, OPTIONAL
243 IN OUT VOID
*CommBuffer
, OPTIONAL
244 IN OUT UINTN
*CommBufferSize OPTIONAL
248 EFI_STATUS Status
= EFI_SUCCESS
;
249 STATIC BOOLEAN mInReadyToBoot
= FALSE
;
251 if (!mInReadyToBoot
) {
253 Status
= MmInstallProtocolInterface (
255 &gEfiEventReadyToBootGuid
,
256 EFI_NATIVE_INTERFACE
,
260 mInReadyToBoot
= TRUE
;
265 Software MMI handler that is called when the DxeMmReadyToLock protocol is added
266 or if gEfiEventReadyToBootGuid is signaled. This function unregisters the
267 Software SMIs that are nor required after MMRAM is locked and installs the
268 MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
271 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
272 @param Context Points to an optional handler context which was specified when the handler was registered.
273 @param CommBuffer A pointer to a collection of data in memory that will
274 be conveyed from a non-MM environment into an MM environment.
275 @param CommBufferSize The size of the CommBuffer.
282 MmReadyToLockHandler (
283 IN EFI_HANDLE DispatchHandle
,
284 IN CONST VOID
*Context
, OPTIONAL
285 IN OUT VOID
*CommBuffer
, OPTIONAL
286 IN OUT UINTN
*CommBufferSize OPTIONAL
293 DEBUG ((DEBUG_INFO
, "MmReadyToLockHandler\n"));
296 // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
298 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
299 if (mMmCoreMmiHandlers
[Index
].UnRegister
) {
300 MmiHandlerUnRegister (mMmCoreMmiHandlers
[Index
].DispatchHandle
);
305 // Install MM Ready to lock protocol
308 Status
= MmInstallProtocolInterface (
310 &gEfiMmReadyToLockProtocolGuid
,
311 EFI_NATIVE_INTERFACE
,
316 // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
318 //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
321 // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
323 //if (EFI_ERROR (Status)) {
324 //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
329 // Assert if the CPU I/O 2 Protocol is not installed
331 //ASSERT_EFI_ERROR (Status);
334 // Display any drivers that were not dispatched because dependency expression
335 // evaluated to false if this is a debug build
337 //MmDisplayDiscoveredNotDispatched ();
343 Software MMI handler that is called when the EndOfDxe event is signaled.
344 This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
345 platform code will invoke 3rd part code.
347 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
348 @param Context Points to an optional handler context which was specified when the handler was registered.
349 @param CommBuffer A pointer to a collection of data in memory that will
350 be conveyed from a non-MM environment into an MM environment.
351 @param CommBufferSize The size of the CommBuffer.
359 IN EFI_HANDLE DispatchHandle
,
360 IN CONST VOID
*Context
, OPTIONAL
361 IN OUT VOID
*CommBuffer
, OPTIONAL
362 IN OUT UINTN
*CommBufferSize OPTIONAL
368 DEBUG ((DEBUG_INFO
, "MmEndOfDxeHandler\n"));
370 // Install MM EndOfDxe protocol
373 Status
= MmInstallProtocolInterface (
375 &gEfiMmEndOfDxeProtocolGuid
,
376 EFI_NATIVE_INTERFACE
,
385 The main entry point to MM Foundation.
387 Note: This function is only used by MMRAM invocation. It is never used by DXE invocation.
389 @param MmEntryContext Processor information and functionality
390 needed by MM Foundation.
396 IN CONST EFI_MM_ENTRY_CONTEXT
*MmEntryContext
400 EFI_MM_COMMUNICATE_HEADER
*CommunicateHeader
;
401 BOOLEAN InLegacyBoot
;
403 DEBUG ((DEBUG_INFO
, "MmEntryPoint ...\n"));
406 // Update MMST using the context
408 CopyMem (&gMmCoreMmst
.MmStartupThisAp
, MmEntryContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
411 // Call platform hook before Mm Dispatch
413 //PlatformHookBeforeMmDispatch ();
416 // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
418 InLegacyBoot
= mInLegacyBoot
;
421 // TBD: Mark the InMm flag as TRUE
423 gMmCorePrivate
->InMm
= TRUE
;
426 // Check to see if this is a Synchronous MMI sent through the MM Communication
427 // Protocol or an Asynchronous MMI
429 if (gMmCorePrivate
->CommunicationBuffer
!= 0) {
431 // Synchronous MMI for MM Core or request from Communicate protocol
433 if (!MmIsBufferOutsideMmValid ((UINTN
)gMmCorePrivate
->CommunicationBuffer
, gMmCorePrivate
->BufferSize
)) {
435 // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
437 gMmCorePrivate
->CommunicationBuffer
= 0;
438 gMmCorePrivate
->ReturnStatus
= EFI_INVALID_PARAMETER
;
440 CommunicateHeader
= (EFI_MM_COMMUNICATE_HEADER
*)(UINTN
)gMmCorePrivate
->CommunicationBuffer
;
441 gMmCorePrivate
->BufferSize
-= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER
, Data
);
443 &CommunicateHeader
->HeaderGuid
,
445 CommunicateHeader
->Data
,
446 (UINTN
*)&gMmCorePrivate
->BufferSize
449 // Update CommunicationBuffer, BufferSize and ReturnStatus
450 // Communicate service finished, reset the pointer to CommBuffer to NULL
452 gMmCorePrivate
->BufferSize
+= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER
, Data
);
453 gMmCorePrivate
->CommunicationBuffer
= 0;
454 gMmCorePrivate
->ReturnStatus
= (Status
== EFI_SUCCESS
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
460 // Process Asynchronous MMI sources
462 MmiManage (NULL
, NULL
, NULL
, NULL
);
465 // TBD: Do not use private data structure ?
469 // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
473 // Clear the InMm flag as we are going to leave MM
475 gMmCorePrivate
->InMm
= FALSE
;
478 DEBUG ((DEBUG_INFO
, "MmEntryPoint Done\n"));
483 MmConfigurationMmNotify (
484 IN CONST EFI_GUID
*Protocol
,
490 EFI_MM_CONFIGURATION_PROTOCOL
*MmConfiguration
;
492 DEBUG ((DEBUG_INFO
, "MmConfigurationMmNotify(%g) - %x\n", Protocol
, Interface
));
494 MmConfiguration
= Interface
;
497 // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
499 Status
= MmConfiguration
->RegisterMmEntry (MmConfiguration
, (EFI_MM_ENTRY_POINT
)(UINTN
)gMmCorePrivate
->MmEntryPoint
);
500 ASSERT_EFI_ERROR (Status
);
503 // Set flag to indicate that the MM Entry Point has been registered which
504 // means that MMIs are now fully operational.
506 gMmCorePrivate
->MmEntryPointRegistered
= TRUE
;
509 // Print debug message showing MM Core entry point address.
511 DEBUG ((DEBUG_INFO
, "MM Core registered MM Entry Point address %p\n", (VOID
*)(UINTN
)gMmCorePrivate
->MmEntryPoint
));
520 EFI_PEI_HOB_POINTERS Hob
;
522 ASSERT (HobStart
!= NULL
);
524 Hob
.Raw
= (UINT8
*) HobStart
;
525 while (!END_OF_HOB_LIST (Hob
)) {
526 Hob
.Raw
= GET_NEXT_HOB (Hob
);
529 // Need plus END_OF_HOB_LIST
531 return (UINTN
)Hob
.Raw
- (UINTN
)HobStart
+ sizeof (EFI_HOB_GENERIC_HEADER
);
535 The Entry Point for MM Core
537 Install DXE Protocols and reload MM Core into MMRAM and register MM Core
538 EntryPoint on the MMI vector.
540 Note: This function is called for both DXE invocation and MMRAM invocation.
542 @param ImageHandle The firmware allocated handle for the EFI image.
543 @param SystemTable A pointer to the EFI System Table.
545 @retval EFI_SUCCESS The entry point is executed successfully.
546 @retval Other Some error occurred when executing this entry point.
560 EFI_HOB_GUID_TYPE
*GuidHob
;
561 MM_CORE_DATA_HOB_DATA
*DataInHob
;
562 EFI_HOB_GUID_TYPE
*MmramRangesHob
;
563 EFI_MMRAM_HOB_DESCRIPTOR_BLOCK
*MmramRangesHobData
;
564 EFI_MMRAM_DESCRIPTOR
*MmramRanges
;
565 UINT32 MmramRangeCount
;
566 EFI_HOB_FIRMWARE_VOLUME
*BfvHob
;
568 ProcessLibraryConstructorList (HobStart
, &gMmCoreMmst
);
570 DEBUG ((DEBUG_INFO
, "MmMain - 0x%x\n", HobStart
));
573 // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
574 // structure in the Hoblist. This choice will govern how boot information is
577 GuidHob
= GetNextGuidHob (&gMmCoreDataHobGuid
, HobStart
);
578 if (GuidHob
== NULL
) {
580 // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
583 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof (MM_CORE_PRIVATE_DATA
)));
584 SetMem ((VOID
*)(UINTN
)gMmCorePrivate
, sizeof (MM_CORE_PRIVATE_DATA
), 0);
585 gMmCorePrivate
->Signature
= MM_CORE_PRIVATE_DATA_SIGNATURE
;
586 gMmCorePrivate
->MmEntryPointRegistered
= FALSE
;
587 gMmCorePrivate
->InMm
= FALSE
;
588 gMmCorePrivate
->ReturnStatus
= EFI_SUCCESS
;
591 // Extract the MMRAM ranges from the MMRAM descriptor HOB
593 MmramRangesHob
= GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid
, HobStart
);
594 if (MmramRangesHob
== NULL
)
595 return EFI_UNSUPPORTED
;
597 MmramRangesHobData
= GET_GUID_HOB_DATA (MmramRangesHob
);
598 ASSERT (MmramRangesHobData
!= NULL
);
599 MmramRanges
= MmramRangesHobData
->Descriptor
;
600 MmramRangeCount
= MmramRangesHobData
->NumberOfMmReservedRegions
;
601 ASSERT (MmramRanges
);
602 ASSERT (MmramRangeCount
);
605 // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
606 // code relies on them being present there
608 gMmCorePrivate
->MmramRangeCount
= MmramRangeCount
;
609 gMmCorePrivate
->MmramRanges
=
610 (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
611 ASSERT (gMmCorePrivate
->MmramRanges
!= 0);
613 (VOID
*)(UINTN
)gMmCorePrivate
->MmramRanges
,
615 MmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
)
618 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
619 gMmCorePrivate
= (MM_CORE_PRIVATE_DATA
*)(UINTN
)DataInHob
->Address
;
620 MmramRanges
= (EFI_MMRAM_DESCRIPTOR
*)(UINTN
)gMmCorePrivate
->MmramRanges
;
621 MmramRangeCount
= gMmCorePrivate
->MmramRangeCount
;
625 // Print the MMRAM ranges passed by the caller
627 DEBUG ((DEBUG_INFO
, "MmramRangeCount - 0x%x\n", MmramRangeCount
));
628 for (Index
= 0; Index
< MmramRangeCount
; Index
++) {
629 DEBUG ((DEBUG_INFO
, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index
,
630 MmramRanges
[Index
].CpuStart
,
631 MmramRanges
[Index
].PhysicalSize
));
635 // Copy the MMRAM ranges into private MMRAM
637 mMmramRangeCount
= MmramRangeCount
;
638 DEBUG ((DEBUG_INFO
, "mMmramRangeCount - 0x%x\n", mMmramRangeCount
));
639 mMmramRanges
= AllocatePool (mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
640 DEBUG ((DEBUG_INFO
, "mMmramRanges - 0x%x\n", mMmramRanges
));
641 ASSERT (mMmramRanges
!= NULL
);
642 CopyMem (mMmramRanges
, (VOID
*)(UINTN
)MmramRanges
, mMmramRangeCount
* sizeof (EFI_MMRAM_DESCRIPTOR
));
645 // Get Boot Firmware Volume address from the BFV Hob
647 BfvHob
= GetFirstHob (EFI_HOB_TYPE_FV
);
648 if (BfvHob
!= NULL
) {
649 DEBUG ((DEBUG_INFO
, "BFV address - 0x%x\n", BfvHob
->BaseAddress
));
650 DEBUG ((DEBUG_INFO
, "BFV size - 0x%x\n", BfvHob
->Length
));
651 gMmCorePrivate
->StandaloneBfvAddress
= BfvHob
->BaseAddress
;
654 gMmCorePrivate
->Mmst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&gMmCoreMmst
;
655 gMmCorePrivate
->MmEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MmEntryPoint
;
658 // No need to initialize memory service.
659 // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
660 // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
663 DEBUG ((DEBUG_INFO
, "MmInstallConfigurationTable For HobList\n"));
667 HobSize
= GetHobListSize (HobStart
);
668 DEBUG ((DEBUG_INFO
, "HobSize - 0x%x\n", HobSize
));
669 MmHobStart
= AllocatePool (HobSize
);
670 DEBUG ((DEBUG_INFO
, "MmHobStart - 0x%x\n", MmHobStart
));
671 ASSERT (MmHobStart
!= NULL
);
672 CopyMem (MmHobStart
, HobStart
, HobSize
);
673 Status
= MmInstallConfigurationTable (&gMmCoreMmst
, &gEfiHobListGuid
, MmHobStart
, HobSize
);
674 ASSERT_EFI_ERROR (Status
);
677 // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
678 // use it to register the MM Foundation entrypoint
680 DEBUG ((DEBUG_INFO
, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
681 Status
= MmRegisterProtocolNotify (
682 &gEfiMmConfigurationProtocolGuid
,
683 MmConfigurationMmNotify
,
686 ASSERT_EFI_ERROR (Status
);
689 // Dispatch standalone BFV
691 DEBUG ((DEBUG_INFO
, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate
->StandaloneBfvAddress
));
692 if (gMmCorePrivate
->StandaloneBfvAddress
!= 0) {
693 MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)gMmCorePrivate
->StandaloneBfvAddress
);
698 // Register all handlers in the core table
700 for (Index
= 0; mMmCoreMmiHandlers
[Index
].HandlerType
!= NULL
; Index
++) {
701 Status
= MmiHandlerRegister (
702 mMmCoreMmiHandlers
[Index
].Handler
,
703 mMmCoreMmiHandlers
[Index
].HandlerType
,
704 &mMmCoreMmiHandlers
[Index
].DispatchHandle
706 DEBUG ((DEBUG_INFO
, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers
[Index
].HandlerType
, Status
));
709 DEBUG ((DEBUG_INFO
, "MmMain Done!\n"));