audk/StandaloneMmPkg/Core/Mmi.c

338 lines
8.8 KiB
C
Raw Normal View History

StandaloneMmPkg/Core: Implementation of Standalone MM Core Module. Management Mode (MM) is a generic term used to describe a secure execution environment provided by the CPU and related silicon that is entered when the CPU detects a MMI. For x86 systems, this can be implemented with System Management Mode (SMM). For ARM systems, this can be implemented with TrustZone (TZ). A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a CPU will jump to the MM Entry Point and save some portion of its state (the "save state") such that execution can be resumed. The MMI can be generated synchronously by software or asynchronously by a hardware event. Each MMI source can be detected, cleared and disabled. Some systems provide for special memory (Management Mode RAM or MMRAM) which is set aside for software running in MM. Usually the MMRAM is hidden during normal CPU execution, but this is not required. Usually, after MMRAM is hidden it cannot be exposed until the next system reset. The MM Core Interface Specification describes three pieces of the PI Management Mode architecture: 1. MM Dispatch During DXE, the DXE Foundation works with the MM Foundation to schedule MM drivers for execution in the discovered firmware volumes. 2. MM Initialization MM related code opens MMRAM, creates the MMRAM memory map, and launches the MM Foundation, which provides the necessary services to launch MM-related drivers. Then, sometime before boot, MMRAM is closed and locked. This piece may be completed during the SEC, PEI or DXE phases. 3. MMI Management When an MMI generated, the MM environment is created and then the MMI sources are detected and MMI handlers called. This patch implements the MM Core. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Sughosh Ganu <sughosh.ganu@arm.com> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com> Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
2018-07-13 17:05:27 +02:00
/** @file
MMI management.
Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "StandaloneMmCore.h"
//
// MM_HANDLER_STATE_NOTIFIER
//
//
// MM_HANDLER - used for each MM handler
//
#define MMI_ENTRY_SIGNATURE SIGNATURE_32('m','m','i','e')
typedef struct {
UINTN Signature;
LIST_ENTRY AllEntries; // All entries
EFI_GUID HandlerType; // Type of interrupt
LIST_ENTRY MmiHandlers; // All handlers
} MMI_ENTRY;
#define MMI_HANDLER_SIGNATURE SIGNATURE_32('m','m','i','h')
typedef struct {
UINTN Signature;
LIST_ENTRY Link; // Link on MMI_ENTRY.MmiHandlers
EFI_MM_HANDLER_ENTRY_POINT Handler; // The mm handler's entry point
MMI_ENTRY *MmiEntry;
} MMI_HANDLER;
LIST_ENTRY mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
LIST_ENTRY mMmiEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
/**
Finds the MMI entry for the requested handler type.
@param HandlerType The type of the interrupt
@param Create Create a new entry if not found
@return MMI entry
**/
MMI_ENTRY *
EFIAPI
MmCoreFindMmiEntry (
IN EFI_GUID *HandlerType,
IN BOOLEAN Create
)
{
LIST_ENTRY *Link;
MMI_ENTRY *Item;
MMI_ENTRY *MmiEntry;
//
// Search the MMI entry list for the matching GUID
//
MmiEntry = NULL;
for (Link = mMmiEntryList.ForwardLink;
Link != &mMmiEntryList;
Link = Link->ForwardLink) {
Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
if (CompareGuid (&Item->HandlerType, HandlerType)) {
//
// This is the MMI entry
//
MmiEntry = Item;
break;
}
}
//
// If the protocol entry was not found and Create is TRUE, then
// allocate a new entry
//
if ((MmiEntry == NULL) && Create) {
MmiEntry = AllocatePool (sizeof (MMI_ENTRY));
if (MmiEntry != NULL) {
//
// Initialize new MMI entry structure
//
MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
InitializeListHead (&MmiEntry->MmiHandlers);
//
// Add it to MMI entry list
//
InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
}
}
return MmiEntry;
}
/**
Manage MMI of a particular type.
@param HandlerType Points to the handler type or NULL for root MMI handlers.
@param Context Points to an optional context buffer.
@param CommBuffer Points to the optional communication buffer.
@param CommBufferSize Points to the size of the optional communication buffer.
@retval EFI_WARN_INTERRUPT_SOURCE_PENDING Interrupt source was processed successfully but not quiesced.
@retval EFI_INTERRUPT_PENDING One or more MMI sources could not be quiesced.
@retval EFI_NOT_FOUND Interrupt source was not handled or quiesced.
@retval EFI_SUCCESS Interrupt source was handled and quiesced.
**/
EFI_STATUS
EFIAPI
MmiManage (
IN CONST EFI_GUID *HandlerType,
IN CONST VOID *Context OPTIONAL,
IN OUT VOID *CommBuffer OPTIONAL,
IN OUT UINTN *CommBufferSize OPTIONAL
)
{
LIST_ENTRY *Link;
LIST_ENTRY *Head;
MMI_ENTRY *MmiEntry;
MMI_HANDLER *MmiHandler;
BOOLEAN SuccessReturn;
EFI_STATUS Status;
Status = EFI_NOT_FOUND;
SuccessReturn = FALSE;
if (HandlerType == NULL) {
//
// Root MMI handler
//
Head = &mRootMmiHandlerList;
} else {
//
// Non-root MMI handler
//
MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
if (MmiEntry == NULL) {
//
// There is no handler registered for this interrupt source
//
return Status;
}
Head = &MmiEntry->MmiHandlers;
}
for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
Status = MmiHandler->Handler (
(EFI_HANDLE) MmiHandler,
Context,
CommBuffer,
CommBufferSize
);
switch (Status) {
case EFI_INTERRUPT_PENDING:
//
// If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
// no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
//
if (HandlerType != NULL) {
return EFI_INTERRUPT_PENDING;
}
break;
case EFI_SUCCESS:
//
// If at least one of the handlers returns EFI_SUCCESS then the function will return
// EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
// additional handlers will be processed.
//
if (HandlerType != NULL) {
return EFI_SUCCESS;
}
SuccessReturn = TRUE;
break;
case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
//
// If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
// then the function will return EFI_SUCCESS.
//
SuccessReturn = TRUE;
break;
case EFI_WARN_INTERRUPT_SOURCE_PENDING:
//
// If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
// then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
//
break;
default:
//
// Unexpected status code returned.
//
ASSERT (FALSE);
break;
}
}
if (SuccessReturn) {
Status = EFI_SUCCESS;
}
return Status;
}
/**
Registers a handler to execute within MM.
@param Handler Handler service funtion pointer.
@param HandlerType Points to the handler type or NULL for root MMI handlers.
@param DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
@retval EFI_SUCCESS Handler register success.
@retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
**/
EFI_STATUS
EFIAPI
MmiHandlerRegister (
IN EFI_MM_HANDLER_ENTRY_POINT Handler,
IN CONST EFI_GUID *HandlerType OPTIONAL,
OUT EFI_HANDLE *DispatchHandle
)
{
MMI_HANDLER *MmiHandler;
MMI_ENTRY *MmiEntry;
LIST_ENTRY *List;
if (Handler == NULL || DispatchHandle == NULL) {
return EFI_INVALID_PARAMETER;
}
MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
if (MmiHandler == NULL) {
return EFI_OUT_OF_RESOURCES;
}
MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
MmiHandler->Handler = Handler;
if (HandlerType == NULL) {
//
// This is root MMI handler
//
MmiEntry = NULL;
List = &mRootMmiHandlerList;
} else {
//
// None root MMI handler
//
MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
if (MmiEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
List = &MmiEntry->MmiHandlers;
}
MmiHandler->MmiEntry = MmiEntry;
InsertTailList (List, &MmiHandler->Link);
*DispatchHandle = (EFI_HANDLE) MmiHandler;
return EFI_SUCCESS;
}
/**
Unregister a handler in MM.
@param DispatchHandle The handle that was specified when the handler was registered.
@retval EFI_SUCCESS Handler function was successfully unregistered.
@retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
**/
EFI_STATUS
EFIAPI
MmiHandlerUnRegister (
IN EFI_HANDLE DispatchHandle
)
{
MMI_HANDLER *MmiHandler;
MMI_ENTRY *MmiEntry;
MmiHandler = (MMI_HANDLER *) DispatchHandle;
if (MmiHandler == NULL) {
return EFI_INVALID_PARAMETER;
}
if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
return EFI_INVALID_PARAMETER;
}
MmiEntry = MmiHandler->MmiEntry;
RemoveEntryList (&MmiHandler->Link);
FreePool (MmiHandler);
if (MmiEntry == NULL) {
//
// This is root MMI handler
//
return EFI_SUCCESS;
}
if (IsListEmpty (&MmiEntry->MmiHandlers)) {
//
// No handler registered for this interrupt now, remove the MMI_ENTRY
//
RemoveEntryList (&MmiEntry->AllEntries);
FreePool (MmiEntry);
}
return EFI_SUCCESS;
}