X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPiSmmCore%2FPiSmmCore.c;h=a7467aca201229dfb9511540ec7f02641e285945;hp=90812ac76a797e729695b87fdf34ec5449d141e4;hb=e63da9f033274843163908ccefa95c892d7944e5;hpb=d5c3d1be01593c996f20d19637c40cb04f8f3a5c diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c index 90812ac76a..a7467aca20 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c @@ -1,7 +1,7 @@ /** @file SMM Core Main Entry Point - Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
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 @@ -66,7 +66,7 @@ EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = { // // Flag to determine if the platform has performed a legacy boot. // If this flag is TRUE, then the runtime code and runtime data associated with the -// SMM IPL are converted to free memory, so the SMM COre must guarantee that is +// SMM IPL are converted to free memory, so the SMM Core must guarantee that is // does not touch of the code/data associated with the SMM IPL if this flag is TRUE. // BOOLEAN mInLegacyBoot = FALSE; @@ -75,12 +75,23 @@ BOOLEAN mInLegacyBoot = FALSE; // Table of SMI Handlers that are registered by the SMM Core when it is initialized // SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = { - { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE }, - { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, - { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE }, - { NULL, NULL, NULL, FALSE } + { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE }, + { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, + { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE }, + { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE }, + { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE }, + { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE }, + { SmmEndOfS3ResumeHandler, &gEdkiiSmmEndOfS3ResumeProtocolGuid, NULL, FALSE }, + { NULL, NULL, NULL, FALSE } }; +UINTN mFullSmramRangeCount; +EFI_SMRAM_DESCRIPTOR *mFullSmramRanges; + +EFI_SMM_DRIVER_ENTRY *mSmmCoreDriverEntry; + +EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage; + /** Place holder function until all the SMM System Table Service are available. @@ -117,7 +128,8 @@ SmmEfiNotAvailableYetArg5 ( Core uses this signal to know that a Legacy Boot has been performed and that gSmmCorePrivate that is shared between the UEFI and SMM execution environments can not be accessed from SMM anymore since that structure is considered free memory by - a legacy OS. + a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM + driver that system enter legacy boot. @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). @param Context Points to an optional handler context which was specified when the handler was registered. @@ -137,8 +149,109 @@ SmmLegacyBootHandler ( IN OUT UINTN *CommBufferSize OPTIONAL ) { + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + + // + // Install SMM Legacy Boot protocol. + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiSmmLegacyBootProtocolGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + mInLegacyBoot = TRUE; - return EFI_SUCCESS; + + SmiHandlerUnRegister (DispatchHandle); + + return Status; +} + +/** + Software SMI handler that is called when an Exit Boot Services event is signalled. + Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver + that system enter exit boot services. + + @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). + @param Context Points to an optional handler context which was specified when the handler was registered. + @param CommBuffer A pointer to a collection of data in memory that will + be conveyed from a non-SMM environment into an SMM environment. + @param CommBufferSize The size of the CommBuffer. + + @return Status Code + +**/ +EFI_STATUS +EFIAPI +SmmExitBootServicesHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, OPTIONAL + IN OUT VOID *CommBuffer, OPTIONAL + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + + // + // Install SMM Exit Boot Services protocol. + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiSmmExitBootServicesProtocolGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + + SmiHandlerUnRegister (DispatchHandle); + + return Status; +} + +/** + Software SMI handler that is called when an Ready To Boot event is signalled. + Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver + that system enter ready to boot. + + @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). + @param Context Points to an optional handler context which was specified when the handler was registered. + @param CommBuffer A pointer to a collection of data in memory that will + be conveyed from a non-SMM environment into an SMM environment. + @param CommBufferSize The size of the CommBuffer. + + @return Status Code + +**/ +EFI_STATUS +EFIAPI +SmmReadyToBootHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, OPTIONAL + IN OUT VOID *CommBuffer, OPTIONAL + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + + // + // Install SMM Ready To Boot protocol. + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiSmmReadyToBootProtocolGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + + SmiHandlerUnRegister (DispatchHandle); + + return Status; } /** @@ -146,7 +259,7 @@ SmmLegacyBootHandler ( or if gEfiEventReadyToBootGuid is signalled. This function unregisters the Software SMIs that are nor required after SMRAM is locked and installs the SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about - to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed + to be locked. It also verifies the SMM CPU I/O 2 Protocol has been installed and NULLs gBS and gST because they can not longer be used after SMRAM is locked. @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). @@ -225,9 +338,136 @@ SmmReadyToLockHandler ( gST = NULL; gBS = NULL; + SmramProfileReadyToLock (); + return Status; } +/** + Software SMI handler that is called when the EndOfDxe event is signalled. + This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that + platform code will invoke 3rd part code. + + @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). + @param Context Points to an optional handler context which was specified when the handler was registered. + @param CommBuffer A pointer to a collection of data in memory that will + be conveyed from a non-SMM environment into an SMM environment. + @param CommBufferSize The size of the CommBuffer. + + @return Status Code + +**/ +EFI_STATUS +EFIAPI +SmmEndOfDxeHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, OPTIONAL + IN OUT VOID *CommBuffer, OPTIONAL + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + + DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n")); + // + // Install SMM EndOfDxe protocol + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEfiSmmEndOfDxeProtocolGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + return Status; +} + +/** + Software SMI handler that is called when the EndOfS3Resume event is trigged. + This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that + S3 resume has finished. + + @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). + @param Context Points to an optional handler context which was specified when the handler was registered. + @param CommBuffer A pointer to a collection of data in memory that will + be conveyed from a non-SMM environment into an SMM environment. + @param CommBufferSize The size of the CommBuffer. + + @return Status Code + +**/ +EFI_STATUS +EFIAPI +SmmEndOfS3ResumeHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, OPTIONAL + IN OUT VOID *CommBuffer, OPTIONAL + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + + DEBUG ((EFI_D_INFO, "SmmEndOfS3ResumeHandler\n")); + + // + // Install SMM EndOfS3Resume protocol + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiSmmEndOfS3ResumeProtocolGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Uninstall the protocol here because the comsume just hook the + // installation event. + // + Status = SmmUninstallProtocolInterface ( + SmmHandle, + &gEdkiiSmmEndOfS3ResumeProtocolGuid, + NULL + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} + +/** + Determine if two buffers overlap in memory. + + @param[in] Buff1 Pointer to first buffer + @param[in] Size1 Size of Buff1 + @param[in] Buff2 Pointer to second buffer + @param[in] Size2 Size of Buff2 + + @retval TRUE Buffers overlap in memory. + @retval FALSE Buffer doesn't overlap. + +**/ +BOOLEAN +InternalIsBufferOverlapped ( + IN UINT8 *Buff1, + IN UINTN Size1, + IN UINT8 *Buff2, + IN UINTN Size2 + ) +{ + // + // If buff1's end is less than the start of buff2, then it's ok. + // Also, if buff1's start is beyond buff2's end, then it's ok. + // + if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) { + return FALSE; + } + + return TRUE; +} + /** The main entry point to SMM Foundation. @@ -245,13 +485,21 @@ SmmEntryPoint ( { EFI_STATUS Status; EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader; + BOOLEAN InLegacyBoot; + BOOLEAN IsOverlapped; + VOID *CommunicationBuffer; + UINTN BufferSize; PERF_START (NULL, "SMM", NULL, 0) ; // - // Update SMST using the context + // Update SMST with contents of the SmmEntryContext structure // - CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT)); + gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp; + gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu; + gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus; + gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize; + gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState; // // Call platform hook before Smm Dispatch @@ -259,51 +507,68 @@ SmmEntryPoint ( PlatformHookBeforeSmmDispatch (); // - // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed - // - if (mInLegacyBoot) { - // - // Asynchronous SMI - // - SmiManage (NULL, NULL, NULL, NULL); - return; - } - - // - // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol + // Call memory management hook function // - gSmmCorePrivate->InSmm = TRUE; + SmmEntryPointMemoryManagementHook (); // - // Check to see if this is a Synchronous SMI sent through the SMM Communication - // Protocol or an Asynchronous SMI + // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed // - if (gSmmCorePrivate->CommunicationBuffer != NULL) { + InLegacyBoot = mInLegacyBoot; + if (!InLegacyBoot) { // - // Synchronous SMI for SMM Core or request from Communicate protocol + // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol // - CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer; - gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); - Status = SmiManage ( - &CommunicateHeader->HeaderGuid, - NULL, - CommunicateHeader->Data, - &gSmmCorePrivate->BufferSize - ); + gSmmCorePrivate->InSmm = TRUE; // - // Update CommunicationBuffer, BufferSize and ReturnStatus - // Communicate service finished, reset the pointer to CommBuffer to NULL - // - gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); - gSmmCorePrivate->CommunicationBuffer = NULL; - gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND; - } else { - // - // Asynchronous SMI + // Check to see if this is a Synchronous SMI sent through the SMM Communication + // Protocol or an Asynchronous SMI // - SmiManage (NULL, NULL, NULL, NULL); + CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer; + BufferSize = gSmmCorePrivate->BufferSize; + if (CommunicationBuffer != NULL) { + // + // Synchronous SMI for SMM Core or request from Communicate protocol + // + IsOverlapped = InternalIsBufferOverlapped ( + (UINT8 *) CommunicationBuffer, + BufferSize, + (UINT8 *) gSmmCorePrivate, + sizeof (*gSmmCorePrivate) + ); + if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, BufferSize) || IsOverlapped) { + // + // If CommunicationBuffer is not in valid address scope, + // or there is overlap between gSmmCorePrivate and CommunicationBuffer, + // return EFI_INVALID_PARAMETER + // + gSmmCorePrivate->CommunicationBuffer = NULL; + gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER; + } else { + CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer; + BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); + Status = SmiManage ( + &CommunicateHeader->HeaderGuid, + NULL, + CommunicateHeader->Data, + &BufferSize + ); + // + // Update CommunicationBuffer, BufferSize and ReturnStatus + // Communicate service finished, reset the pointer to CommBuffer to NULL + // + gSmmCorePrivate->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); + gSmmCorePrivate->CommunicationBuffer = NULL; + gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND; + } + } } + + // + // Process Asynchronous SMI sources + // + SmiManage (NULL, NULL, NULL, NULL); // // Call platform hook after Smm Dispatch @@ -311,13 +576,99 @@ SmmEntryPoint ( PlatformHookAfterSmmDispatch (); // - // Clear the InSmm flag as we are going to leave SMM + // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed // - gSmmCorePrivate->InSmm = FALSE; + if (!InLegacyBoot) { + // + // Clear the InSmm flag as we are going to leave SMM + // + gSmmCorePrivate->InSmm = FALSE; + } PERF_END (NULL, "SMM", NULL, 0) ; } +/** + Install LoadedImage protocol for SMM Core. +**/ +VOID +SmmCoreInstallLoadedImage ( + VOID + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + + // + // Allocate a Loaded Image Protocol in EfiBootServicesData + // + Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage); + ASSERT_EFI_ERROR (Status); + + ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL)); + // + // Fill in the remaining fields of the Loaded Image Protocol instance. + // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed. + // + mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; + mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle; + mSmmCoreLoadedImage->SystemTable = gST; + + mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase; + mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize; + mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode; + mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData; + + // + // Create a new image handle in the UEFI handle database for the SMM Driver + // + Handle = NULL; + Status = gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Allocate a Loaded Image Protocol in SMM + // + Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry); + ASSERT_EFI_ERROR(Status); + + ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY)); + // + // Fill in the remaining fields of the Loaded Image Protocol instance. + // + mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE; + mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; + mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle; + mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST; + + mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase; + mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize; + mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode; + mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData; + + mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint; + mSmmCoreDriverEntry->ImageBuffer = gSmmCorePrivate->PiSmmCoreImageBase; + mSmmCoreDriverEntry->NumberOfPage = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize); + + // + // Create a new image handle in the SMM handle database for the SMM Driver + // + mSmmCoreDriverEntry->SmmImageHandle = NULL; + Status = SmmInstallProtocolInterface ( + &mSmmCoreDriverEntry->SmmImageHandle, + &gEfiLoadedImageProtocolGuid, + EFI_NATIVE_INTERFACE, + &mSmmCoreDriverEntry->SmmLoadedImage + ); + ASSERT_EFI_ERROR(Status); + + return ; +} + /** The Entry Point for SMM Core @@ -353,11 +704,22 @@ SmmMain ( // gSmmCorePrivate->Smst = &gSmmCoreSmst; gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint; - + + // + // No need to initialize memory service. + // It is done in constructor of PiSmmCoreMemoryAllocationLib(), + // so that the library linked with PiSmmCore can use AllocatePool() in constuctor. + // + + SmramProfileInit (); + // - // Initialize memory service using free SMRAM + // Copy FullSmramRanges to SMRAM // - SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, gSmmCorePrivate->SmramRanges); + mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount; + mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR)); + ASSERT (mFullSmramRanges != NULL); + CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR)); // // Register all SMI Handlers required by the SMM Core @@ -370,6 +732,15 @@ SmmMain ( ); ASSERT_EFI_ERROR (Status); } - + + RegisterSmramProfileHandler (); + SmramProfileInstallProtocol (); + + SmmCoreInstallLoadedImage (); + + SmmCoreInitializeMemoryAttributesTable (); + + SmmCoreInitializeSmiHandlerProfile (); + return EFI_SUCCESS; }