X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FPiSmmCore%2FPiSmmCore.c;h=45f40e0e60c7839ee731114a7b430d16a631094e;hb=67e9ab84ef042bd59c4297fdad7f6aece6b7bbca;hp=551560b1fce1e92466e271065c83855f39d14565;hpb=e524f680647679ff7bf9b22dfba1dea4291a9cfe;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c index 551560b1fc..45f40e0e60 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 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2018, 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 @@ -71,22 +71,32 @@ EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = { // BOOLEAN mInLegacyBoot = FALSE; +// +// Flag to determine if it is during S3 resume. +// It will be set in S3 entry callback and cleared at EndOfS3Resume. +// +BOOLEAN mDuringS3Resume = 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 }, - { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE }, - { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE }, - { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE }, - { 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 }, + { SmmS3SmmInitDoneHandler, &gEdkiiS3SmmInitDoneGuid, NULL, FALSE }, + { SmmEndOfS3ResumeHandler, &gEdkiiEndOfS3ResumeGuid, NULL, FALSE }, + { NULL, NULL, NULL, FALSE } }; UINTN mFullSmramRangeCount; EFI_SMRAM_DESCRIPTOR *mFullSmramRanges; +EFI_SMM_DRIVER_ENTRY *mSmmCoreDriverEntry; + EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage; /** @@ -148,6 +158,7 @@ SmmLegacyBootHandler ( { EFI_STATUS Status; EFI_HANDLE SmmHandle; + UINTN Index; // // Install SMM Legacy Boot protocol. @@ -164,6 +175,16 @@ SmmLegacyBootHandler ( SmiHandlerUnRegister (DispatchHandle); + // + // It is legacy boot, unregister ExitBootService SMI handler. + // + for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) { + if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventExitBootServicesGuid)) { + SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle); + break; + } + } + return Status; } @@ -192,6 +213,7 @@ SmmExitBootServicesHandler ( { EFI_STATUS Status; EFI_HANDLE SmmHandle; + UINTN Index; // // Install SMM Exit Boot Services protocol. @@ -206,9 +228,50 @@ SmmExitBootServicesHandler ( SmiHandlerUnRegister (DispatchHandle); + // + // It is UEFI boot, unregister LegacyBoot SMI handler. + // + for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) { + if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventLegacyBootGuid)) { + SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle); + break; + } + } + return Status; } +/** + Main entry point for an SMM handler dispatch or communicate-based callback. + + @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). + @param[in] Context Points to an optional handler context which was specified when the + handler was registered. + @param[in,out] CommBuffer A pointer to a collection of data in memory that will + be conveyed from a non-SMM environment into an SMM environment. + @param[in,out] CommBufferSize The size of the CommBuffer. + + @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers + should still be called. + @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should + still be called. + @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still + be called. + @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced. +**/ +EFI_STATUS +EFIAPI +SmmS3EntryCallBack ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context OPTIONAL, + IN OUT VOID *CommBuffer OPTIONAL, + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + mDuringS3Resume = TRUE; + return EFI_SUCCESS; +} + /** 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 @@ -232,8 +295,8 @@ SmmReadyToBootHandler ( IN OUT UINTN *CommBufferSize OPTIONAL ) { - EFI_STATUS Status; - EFI_HANDLE SmmHandle; + EFI_STATUS Status; + EFI_HANDLE SmmHandle; // // Install SMM Ready To Boot protocol. @@ -256,7 +319,7 @@ SmmReadyToBootHandler ( 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(). @@ -363,10 +426,14 @@ SmmEndOfDxeHandler ( IN OUT UINTN *CommBufferSize OPTIONAL ) { - EFI_STATUS Status; - EFI_HANDLE SmmHandle; + EFI_STATUS Status; + EFI_HANDLE SmmHandle; + EFI_SMM_SX_DISPATCH2_PROTOCOL *SxDispatch; + EFI_SMM_SX_REGISTER_CONTEXT EntryRegisterContext; + EFI_HANDLE S3EntryHandle; DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n")); + // // Install SMM EndOfDxe protocol // @@ -377,6 +444,150 @@ SmmEndOfDxeHandler ( EFI_NATIVE_INTERFACE, NULL ); + + // + // Locate SmmSxDispatch2 protocol. + // + Status = SmmLocateProtocol ( + &gEfiSmmSxDispatch2ProtocolGuid, + NULL, + (VOID **)&SxDispatch + ); + if (!EFI_ERROR (Status) && (SxDispatch != NULL)) { + // + // Register a S3 entry callback function to + // determine if it will be during S3 resume. + // + EntryRegisterContext.Type = SxS3; + EntryRegisterContext.Phase = SxEntry; + Status = SxDispatch->Register ( + SxDispatch, + SmmS3EntryCallBack, + &EntryRegisterContext, + &S3EntryHandle + ); + ASSERT_EFI_ERROR (Status); + } + + return EFI_SUCCESS; +} + +/** + Software SMI handler that is called when the S3SmmInitDone signal is triggered. + This function installs the SMM S3SmmInitDone Protocol so SMM Drivers are informed that + S3 SMM initialization has been done. + + @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 +SmmS3SmmInitDoneHandler ( + 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 ((DEBUG_INFO, "SmmS3SmmInitDoneHandler\n")); + + if (!mDuringS3Resume) { + DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n")); + return EFI_SUCCESS; + } + + // + // Install SMM S3SmmInitDone protocol + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiS3SmmInitDoneGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Uninstall the protocol here because the comsumer just hook the + // installation event. + // + Status = SmmUninstallProtocolInterface ( + SmmHandle, + &gEdkiiS3SmmInitDoneGuid, + NULL + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} + +/** + Software SMI handler that is called when the EndOfS3Resume signal is triggered. + 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 ((DEBUG_INFO, "SmmEndOfS3ResumeHandler\n")); + + if (!mDuringS3Resume) { + DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n")); + return EFI_SUCCESS; + } + + // + // Install SMM EndOfS3Resume protocol + // + SmmHandle = NULL; + Status = SmmInstallProtocolInterface ( + &SmmHandle, + &gEdkiiEndOfS3ResumeGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Uninstall the protocol here because the comsumer just hook the + // installation event. + // + Status = SmmUninstallProtocolInterface ( + SmmHandle, + &gEdkiiEndOfS3ResumeGuid, + NULL + ); + ASSERT_EFI_ERROR (Status); + + mDuringS3Resume = FALSE; return Status; } @@ -430,8 +641,10 @@ SmmEntryPoint ( EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader; BOOLEAN InLegacyBoot; BOOLEAN IsOverlapped; + VOID *CommunicationBuffer; + UINTN BufferSize; - PERF_START (NULL, "SMM", NULL, 0) ; + PERF_INMODULE_BEGIN ("SMM"); // // Update SMST with contents of the SmmEntryContext structure @@ -447,6 +660,11 @@ SmmEntryPoint ( // PlatformHookBeforeSmmDispatch (); + // + // Call memory management hook function + // + SmmEntryPointMemoryManagementHook (); + // // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed // @@ -461,38 +679,40 @@ SmmEntryPoint ( // Check to see if this is a Synchronous SMI sent through the SMM Communication // Protocol or an Asynchronous SMI // - if (gSmmCorePrivate->CommunicationBuffer != NULL) { + CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer; + BufferSize = gSmmCorePrivate->BufferSize; + if (CommunicationBuffer != NULL) { // // Synchronous SMI for SMM Core or request from Communicate protocol // IsOverlapped = InternalIsBufferOverlapped ( - (UINT8 *) gSmmCorePrivate->CommunicationBuffer, - gSmmCorePrivate->BufferSize, + (UINT8 *) CommunicationBuffer, + BufferSize, (UINT8 *) gSmmCorePrivate, sizeof (*gSmmCorePrivate) ); - if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) { + 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; + gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED; } else { - CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer; - gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); + CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer; + BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); Status = SmiManage ( &CommunicateHeader->HeaderGuid, NULL, CommunicateHeader->Data, - &gSmmCorePrivate->BufferSize + &BufferSize ); // // 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->BufferSize = BufferSize + OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data); gSmmCorePrivate->CommunicationBuffer = NULL; gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND; } @@ -519,7 +739,7 @@ SmmEntryPoint ( gSmmCorePrivate->InSmm = FALSE; } - PERF_END (NULL, "SMM", NULL, 0) ; + PERF_INMODULE_END ("SMM"); } /** @@ -564,6 +784,42 @@ SmmCoreInstallLoadedImage ( ); 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 ; } @@ -602,7 +858,7 @@ SmmMain ( // gSmmCorePrivate->Smst = &gSmmCoreSmst; gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint; - + // // No need to initialize memory service. // It is done in constructor of PiSmmCoreMemoryAllocationLib(), @@ -636,5 +892,9 @@ SmmMain ( SmmCoreInstallLoadedImage (); + SmmCoreInitializeMemoryAttributesTable (); + + SmmCoreInitializeSmiHandlerProfile (); + return EFI_SUCCESS; }