PiSmmIpl.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
Copyright (c) 2009 - 2011, Intel Corporation. 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
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 <PiDxe.h>
#include <Protocol/SmmBase2.h>
#include <Protocol/SmmCommunication.h>
#include <Protocol/SmmAccess2.h>
#include <Protocol/SmmConfiguration.h>
#include <Protocol/SmmControl2.h>
#include <Protocol/DxeSmmReadyToLock.h>
#include <Guid/EventGroup.h>
#include <Guid/EventLegacyBios.h>
#include <Guid/LoadModuleAtFixedAddress.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PeCoffLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/DxeServicesLib.h>
#include <Library/UefiRuntimeLib.h>
#include "PiSmmCorePrivateData.h"
//
// Function prototypes from produced protocols
//
/**
Indicate whether the driver is currently executing in the SMM Initialization phase.
@param This The EFI_SMM_BASE2_PROTOCOL instance.
@param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
@retval EFI_INVALID_PARAMETER InSmram was NULL.
@retval EFI_SUCCESS The call returned successfully.
**/
);
/**
Retrieves the location of the System Management System Table (SMST).
@param This The EFI_SMM_BASE2_PROTOCOL instance.
@param Smst On return, points to a pointer to the System Management Service Table (SMST).
@retval EFI_INVALID_PARAMETER Smst or This was invalid.
@retval EFI_SUCCESS The memory was returned to the system.
@retval EFI_UNSUPPORTED Not in SMM.
**/
);
/**
Communicates with a registered handler.
This function provides a service to send and receive messages from a registered
UEFI service. This function is part of the SMM Communication Protocol that may
be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
after SetVirtualAddressMap().
@param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
@param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
@param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
being returned. Zero if the handler does not wish to reply with any data.
@retval EFI_SUCCESS The message was successfully posted.
@retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
**/
);
/**
Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
);
/**
Event notification that is fired every time a DxeSmmReadyToLock protocol is added
or if gEfiEventReadyToBootGuid is signalled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
);
/**
Event notification that is fired when DxeDispatch Event Group is signaled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
);
/**
Event notification that is fired when a GUIDed Event Group is signaled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
);
/**
Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
It convers pointer to new virtual address.
@param Event Event whose notification function is being invoked.
@param Context Pointer to the notification function's context.
**/
);
//
// Data structure used to declare a table of protocol notifications and event
// notifications required by the SMM IPL
//
typedef struct {
//
// Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
//
//
// SMM Base 2 Protocol instance
//
};
//
// SMM Communication Protocol instance
//
};
//
// SMM Core Private Data structure that contains the data shared between
// the SMM IPL and the SMM Core.
//
SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
NULL, // SmmIplImageHandle
0, // SmramRangeCount
NULL, // SmramRanges
NULL, // SmmEntryPoint
FALSE, // SmmEntryPointRegistered
FALSE, // InSmm
NULL, // Smst
NULL, // CommunicationBuffer
0, // BufferSize
EFI_SUCCESS // ReturnStatus
};
//
// Global pointer used to access mSmmCorePrivateData from outside and inside SMM
//
//
// SMM IPL global variables
//
//
// Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
//
//
// Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
// the associated event is immediately signalled, so the notification function will be executed and the
// SMM Configuration Protocol will be found if it is already in the handle database.
//
{ TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },
//
// Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
// the associated event is immediately signalled, so the notification function will be executed and the
// DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
//
{ TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },
//
// Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
// each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
// if notified, so the SMM Core can dispatch SMM drivers.
//
{ FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },
//
// Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
// used to make sure SMRAM is locked before any boot options are processed.
//
{ FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
//
// Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
// is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
// must guarantee that it does not access any UEFI related structures outside of SMRAM.
//
{ FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },
//
// Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
// and mSmmControl2 from physical addresses to virtual addresses.
//
{ FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },
//
// Terminate the table of event notifications
//
};
/**
Find the maximum SMRAM cache range that covers the range specified by SmramRange.
This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
@param SmramRange The SMRAM range to search from.
@param SmramCacheBase The returned cache range base.
@param SmramCacheSize The returned cache range size.
**/
)
{
do {
}
}
} while (FoundAjacentRange);
}
/**
Indicate whether the driver is currently executing in the SMM Initialization phase.
@param This The EFI_SMM_BASE2_PROTOCOL instance.
@param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
@retval EFI_INVALID_PARAMETER InSmram was NULL.
@retval EFI_SUCCESS The call returned successfully.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Retrieves the location of the System Management System Table (SMST).
@param This The EFI_SMM_BASE2_PROTOCOL instance.
@param Smst On return, points to a pointer to the System Management Service Table (SMST).
@retval EFI_INVALID_PARAMETER Smst or This was invalid.
@retval EFI_SUCCESS The memory was returned to the system.
@retval EFI_UNSUPPORTED Not in SMM.
**/
)
{
return EFI_INVALID_PARAMETER;
}
if (!gSmmCorePrivate->InSmm) {
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
/**
Communicates with a registered handler.
This function provides a service to send and receive messages from a registered
UEFI service. This function is part of the SMM Communication Protocol that may
be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
after SetVirtualAddressMap().
@param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
@param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
@param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
being returned. Zero if the handler does not wish to reply with any data.
@retval EFI_SUCCESS The message was successfully posted.
@retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
**/
)
{
//
// Check parameters
//
return EFI_INVALID_PARAMETER;
}
//
// CommSize must hold HeaderGuid and MessageLength
//
return EFI_INVALID_PARAMETER;
}
//
// If not already in SMM, then generate a Software SMI
//
//
// Put arguments for Software SMI in gSmmCorePrivate
//
//
// Generate Software SMI
//
return EFI_UNSUPPORTED;
}
//
// Return status from software SMI
//
return gSmmCorePrivate->ReturnStatus;
}
//
// If we are in SMM, then the execution mode must be physical, which means that
// OS established virtual addresses can not be used. If SetVirtualAddressMap()
// has been called, then a direct invocation of the Software SMI is not
// not allowed so return EFI_INVALID_PARAMETER.
//
if (EfiGoneVirtual()) {
return EFI_INVALID_PARAMETER;
}
//
// If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
//
return EFI_INVALID_PARAMETER;
}
//
// Save current InSmm state and set InSmm state to TRUE
//
//
// Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
//
NULL,
);
//
// Update CommunicationBuffer, BufferSize and ReturnStatus
// Communicate service finished, reset the pointer to CommBuffer to NULL
//
//
// Restore original InSmm state
//
}
/**
Event notification that is fired when GUIDed Event Group is signaled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
)
{
//
// Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
//
CommunicateHeader.Data[0] = 0;
//
// Generate the Software SMI and return the result
//
Size = sizeof (CommunicateHeader);
}
/**
Event notification that is fired when DxeDispatch Event Group is signaled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
)
{
//
// Keep calling the SMM Core Dispatcher until there is no request to restart it.
//
while (TRUE) {
//
// Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
// Clear the buffer passed into the Software SMI. This buffer will return
// the status of the SMM Core Dispatcher.
//
CommunicateHeader.Data[0] = 0;
//
// Generate the Software SMI and return the result
//
Size = sizeof (CommunicateHeader);
//
// Return if there is no request to restart the SMM Core Dispatcher
//
return;
}
//
// Attempt to reset SMRAM cacheability to UC
// Assume CPU AP is available at this time
//
);
}
//
// Close all SMRAM ranges to protect SMRAM
//
//
// Print debug message that the SMRAM window is now closed.
//
}
}
/**
Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
)
{
//
// Make sure this notification is for this handler
//
return;
}
//
// Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
//
//
// Set flag to indicate that the SMM Entry Point has been registered which
// means that SMIs are now fully operational.
//
//
// Print debug message showing SMM Core entry point address.
//
DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
}
/**
Event notification that is fired every time a DxeSmmReadyToLock protocol is added
or if gEfiEventReadyToBootGuid is signalled.
@param Event The Event that is being processed, not used.
@param Context Event Context, not used.
**/
)
{
//
// See if we are already locked
//
if (mSmmLocked) {
return;
}
//
// Make sure this notification is for this handler
//
return;
}
} else {
//
// If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
// signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
// Print a warning on debug builds.
//
DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
}
//
// Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
//
//
// Close protocol and event notification events that do not apply after the
// DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
// event has been signalled.
//
}
}
//
// Inform SMM Core that the DxeSmmReadyToLock protocol was installed
//
//
// Print debug message that the SMRAM window is now locked.
//
//
// Set flag so this operation will not be performed again
//
mSmmLocked = TRUE;
}
/**
Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
It convers pointer to new virtual address.
@param Event Event whose notification function is being invoked.
@param Context Pointer to the notification function's context.
**/
)
{
}
/**
Get the fixed loadding address from image header assigned by build tool. This function only be called
when Loading module at Fixed address feature enabled.
image that needs to be examined by this function.
@retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
@retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
**/
)
{
//
// Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
//
FixLoaddingAddress = 0;
//
// Get PeHeader pointer
//
ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
SectionHeaderOffset = (UINTN)(
sizeof (UINT32) +
sizeof (EFI_IMAGE_FILE_HEADER) +
);
//
// Get base address from the first section header that doesn't point to code section.
//
//
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
&Size,
);
return Status;
}
//
// Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
// first section header that doesn't point to code section in image header. And there is an assumption that when the
// feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
// fields should NOT be Zero, or else, these 2 fileds should be set to Zero
//
if (ValueInSectionHeader != 0) {
//
// Found first section header that doesn't point to code section in which uild tool saves the
// offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
//
//
// The assigned address is valid. Return the specified loadding address
//
}
}
break;
}
SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
}
DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));
return Status;
}
/**
Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
@param[in] SmramRange Descriptor for the range of SMRAM to reload the
currently executing image.
@param[in] Context Context to pass into SMM Core
@return EFI_STATUS
**/
)
{
//
//
0,
0,
);
return Status;
}
//
// Initilize ImageContext
//
//
// Get information about the image being loaded
//
return Status;
}
//
// if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
// the address assigned by build tool.
//
if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
//
// Get the fixed loading address assigned by Build tool
//
//
// Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
//
PageCount = 0;
} else {
DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
//
// Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
// specified by SmramRange
//
PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
//
// Align buffer on section boundry
//
}
} else {
//
// Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
// specified by SmramRange
//
PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
//
// Align buffer on section boundry
//
}
//
// Print debug message showing SMM Core load address.
//
DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
//
// Load the image to our new buffer
//
//
// Relocate the image in our new buffer
//
//
// Flush the instruction cache so the image data are written before we execute it
//
InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
//
// Print debug message showing SMM Core entry point address.
//
DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
//
// Execute image
//
}
}
//
// If the load operation, relocate operation, or the image execution return an
// error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
// SmramRange
//
}
//
// Always free memory allocted by GetFileBufferByFilePath ()
//
return Status;
}
/**
The Entry Point for SMM IPL
Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
SMM Base 2 Protocol and SMM Communication Protocol, and register for the
critical events required to coordinate between DXE and SMM environments.
@param ImageHandle The firmware allocated handle for the EFI image.
@param SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval Other Some error occurred when executing this entry point.
**/
)
{
//
// Fill in the image handle of the SMM IPL so the SMM Core can use this as the
// ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
// by the SMM Core
//
//
// Get SMM Access Protocol
//
//
// Get SMM Control2 Protocol
//
//
// Get SMM Configuration Protocol if it is present
//
Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
//
// Get SMRAM information
//
Size = 0;
//
// Open all SMRAM ranges
//
//
// Print debug message that the SMRAM window is now open.
//
//
// Subtract SMRAM any reserved SMRAM regions.
//
if (SmmConfiguration != NULL) {
while (SmramResRegion->SmramReservedSize != 0) {
(gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {
//
// This range has reserved area, calculate the left free size
//
gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;
}
}
}
}
//
// Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
//
for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
//
// Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
//
if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
continue;
}
if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {
}
}
}
}
if (mCurrentSmramRange != NULL) {
//
// Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
//
));
//
// If CPU AP is present, attempt to set SMRAM cacheability to WB
// Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
// is not available here.
//
);
}
}
//
// if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
// Modules At Fixed Address Configuration Table.
//
if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
//
// Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
//
//
// The SMRAM available memory is assumed to be larger than SmmCodeSize
//
//
// Retrieve Load modules At fixed address configuration table and save the SMRAM base.
//
(VOID **) &LMFAConfigurationTable
);
//
// Print the SMRAM base
//
DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));
}
}
//
// Load SMM Core into SMRAM and execute it from SMRAM
//
//
// Print error message that the SMM Core failed to be loaded and executed.
//
//
// Attempt to reset SMRAM cacheability to UC
//
);
}
}
}
} else {
//
// Print error message that there are not enough SMRAM resources to load the SMM Core.
//
}
//
// If the SMM Core could not be loaded then close SMRAM window, free allocated
// resources, and return an error so SMM IPL will be unloaded.
//
//
// Close all SMRAM ranges
//
//
// Print debug message that the SMRAM window is now closed.
//
//
// Free all allocated resources
//
return EFI_UNSUPPORTED;
}
//
// Install SMM Base2 Protocol and SMM Communication Protocol
//
);
//
// Create the set of protocol and event notififcations that the SMM IPL requires
//
);
} else {
);
}
}
return EFI_SUCCESS;
}