From nobody Sat Nov 2 16:31:13 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1486571459392419.41420708779515; Wed, 8 Feb 2017 08:30:59 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5B29882058; Wed, 8 Feb 2017 08:30:42 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id D6832820BA for ; Wed, 8 Feb 2017 08:30:39 -0800 (PST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP; 08 Feb 2017 08:30:39 -0800 Received: from jyao1-mobl.ccr.corp.intel.com ([10.254.185.79]) by fmsmga002.fm.intel.com with ESMTP; 08 Feb 2017 08:30:39 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,348,1477983600"; d="scan'208";a="1123833898" From: Jiewen Yao To: edk2-devel@lists.01.org Date: Wed, 8 Feb 2017 08:30:32 -0800 Message-Id: <1486571434-20000-8-git-send-email-jiewen.yao@intel.com> X-Mailer: git-send-email 2.7.4.windows.1 In-Reply-To: <1486571434-20000-1-git-send-email-jiewen.yao@intel.com> References: <1486571434-20000-1-git-send-email-jiewen.yao@intel.com> Subject: [edk2] [PATCH 07/12] MdeModulePkg/PiSmmCore: Add SmiHandlerProfile support. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael D Kinney , Feng Tian , Laszlo Ersek , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 1) SmmCore maintains the root SMI handler and NULL SMI handler database. 2) SmmCore consumes PcdSmiHandlerProfilePropertyMask to decide if SmmCore need support SMI handler profile. If SMI handler profile is supported, the SmmCore installs SMI handler profile protocol and SMI handler profile communication handler. 3) SMI handler profile protocol will record the hardware SMI handler profile registered by SmmChildDispatcher. 4) SMI handler profile communication handler will return all SMI handler profile info (NULL SMI handler, GUID SMI handler, and hardware SMI handler) Cc: Feng Tian Cc: Star Zeng Cc: Michael D Kinney Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jiewen Yao --- MdeModulePkg/Core/PiSmmCore/PiSmmCore.c | 4 +- MdeModulePkg/Core/PiSmmCore/PiSmmCore.h | 89 +- MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf | 17 +- MdeModulePkg/Core/PiSmmCore/Smi.c | 46 +- MdeModulePkg/Core/PiSmmCore/SmiHandlerProfile.c | 1261 ++++++++++++++++++++ 5 files changed, 1381 insertions(+), 36 deletions(-) diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c b/MdeModulePkg/Core/Pi= SmmCore/PiSmmCore.c index de8db65..9e4390e 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c @@ -1,7 +1,7 @@ /** @file SMM Core Main Entry Point =20 - Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made availa= ble=20 under the terms and conditions of the BSD License which accompanies this=20 distribution. The full text of the license may be found at =20 @@ -680,5 +680,7 @@ SmmMain ( =20 SmmCoreInitializeMemoryAttributesTable (); =20 + SmmCoreInitializeSmiHandlerProfile (); + return EFI_SUCCESS; } diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h b/MdeModulePkg/Core/Pi= SmmCore/PiSmmCore.h index 58590d5..bdef026 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h @@ -2,7 +2,7 @@ The internal header file includes the common header files, defines internal structure and functions used by SmmCore module. =20 - Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made availa= ble=20 under the terms and conditions of the BSD License which accompanies this=20 distribution. The full text of the license may be found at =20 @@ -38,6 +38,7 @@ #include #include #include +#include =20 #include #include @@ -70,6 +71,32 @@ typedef struct { } SMM_CORE_SMI_HANDLERS; =20 // +// SMM_HANDLER - used for each SMM handler +// + +#define SMI_ENTRY_SIGNATURE SIGNATURE_32('s','m','i','e') + + typedef struct { + UINTN Signature; + LIST_ENTRY AllEntries; // All entries + + EFI_GUID HandlerType; // Type of interrupt + LIST_ENTRY SmiHandlers; // All handlers +} SMI_ENTRY; + +#define SMI_HANDLER_SIGNATURE SIGNATURE_32('s','m','i','h') + + typedef struct { + UINTN Signature; + LIST_ENTRY Link; // Link on SMI_ENTRY.SmiHandl= ers + EFI_SMM_HANDLER_ENTRY_POINT2 Handler; // The smm handler's entry po= int + UINTN CallerAddr; // The address of caller who = register the SMI handler. + SMI_ENTRY *SmiEntry; + VOID *Context; // for profile + UINTN ContextSize; // for profile +} SMI_HANDLER; + +// // Structure for recording the state of an SMM Driver // #define EFI_SMM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v') @@ -1064,6 +1091,66 @@ SmmCoreGetMemoryMap ( OUT UINT32 *DescriptorVersion ); =20 +/** + Initialize SmiHandler profile feature. +**/ +VOID +SmmCoreInitializeSmiHandlerProfile ( + VOID + ); + +/** + This function is called by SmmChildDispatcher module to report + a new SMI handler is registered, to SmmCore. + + @param This The protocol instance + @param HandlerGuid The GUID to identify the type of the handler. + For the SmmChildDispatch protocol, the HandlerGuid + must be the GUID of SmmChildDispatch protocol. + @param Handler The SMI handler. + @param CallerAddress The address of the module who registers the SMI h= andler. + @param Context The context of the SMI handler. + For the SmmChildDispatch protocol, the Context + must match the one defined for SmmChildDispatch p= rotocol. + @param ContextSize The size of the context in bytes. + For the SmmChildDispatch protocol, the Context + must match the one defined for SmmChildDispatch p= rotocol. + + @retval EFI_SUCCESS The information is recorded. + @retval EFI_OUT_OF_RESOURCES There is no enough resource to record the = information. +**/ +EFI_STATUS +EFIAPI +SmiHandlerProfileRegisterHandler ( + IN SMI_HANDLER_PROFILE_PROTOCOL *This, + IN EFI_GUID *HandlerGuid, + IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler, + IN PHYSICAL_ADDRESS CallerAddress, + IN VOID *Context, OPTIONAL + IN UINTN ContextSize OPTIONAL + ); + +/** + This function is called by SmmChildDispatcher module to report + an existing SMI handler is unregistered, to SmmCore. + + @param This The protocol instance + @param HandlerGuid The GUID to identify the type of the handler. + For the SmmChildDispatch protocol, the HandlerGuid + must be the GUID of SmmChildDispatch protocol. + @param Handler The SMI handler. + + @retval EFI_SUCCESS The original record is removed. + @retval EFI_NOT_FOUND There is no record for the HandlerGuid and= handler. +**/ +EFI_STATUS +EFIAPI +SmiHandlerProfileUnregisterHandler ( + IN SMI_HANDLER_PROFILE_PROTOCOL *This, + IN EFI_GUID *HandlerGuid, + IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler + ); + /// /// For generic EFI machines make the default allocations 4K aligned /// diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf b/MdeModulePkg/Core/= PiSmmCore/PiSmmCore.inf index f380fc5..95e34bd 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf @@ -1,7 +1,7 @@ ## @file # This module provide an SMM CIS compliant implementation of SMM Core. # -# Copyright (c) 2009 - 2016, 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 BS= D License @@ -39,6 +39,7 @@ InstallConfigurationTable.c SmramProfileRecord.c MemoryAttributesTable.c + SmiHandlerProfile.c =20 [Packages] MdePkg/MdePkg.dec @@ -63,6 +64,7 @@ TimerLib HobLib SmmMemLib + DxeServicesLib =20 [Protocols] gEfiDxeSmmReadyToLockProtocolGuid ## UNDEFINED # SmiHandlerR= egister @@ -78,12 +80,22 @@ gEdkiiSmmLegacyBootProtocolGuid ## SOMETIMES_PRODUCES gEdkiiSmmReadyToBootProtocolGuid ## PRODUCES =20 + gEfiSmmSwDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmSxDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmPowerButtonDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmStandbyButtonDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmPeriodicTimerDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmGpiDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmIoTrapDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiSmmUsbDispatch2ProtocolGuid ## SOMETIMES_CONSUMES + [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdLoadFixAddressSmmCodePageNumber ##= SOMETIMES_CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdLoadModuleAtFixAddressEnable ##= CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfileMemoryType ##= CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfilePropertyMask ##= CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfileDriverPath ##= CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdSmiHandlerProfilePropertyMask ##= CONSUMES =20 [Guids] gAprioriGuid ## SOMETIMES_CONSUMES ##= File @@ -100,6 +112,9 @@ gEdkiiPiSmmMemoryAttributesTableGuid ## SOMETIMES_PRODUCES ##= SystemTable ## SOMETIMES_CONSUMES ## SystemTable gLoadFixedAddressConfigurationTableGuid + ## SOMETIMES_PRODUCES ## GUID # Install protocol + ## SOMETIMES_PRODUCES ## GUID # SmiHandlerRegister + gSmiHandlerProfileGuid =20 [UserExtensions.TianoCore."ExtraFiles"] PiSmmCoreExtra.uni diff --git a/MdeModulePkg/Core/PiSmmCore/Smi.c b/MdeModulePkg/Core/PiSmmCor= e/Smi.c index 816d0f5..ad483a1 100644 --- a/MdeModulePkg/Core/PiSmmCore/Smi.c +++ b/MdeModulePkg/Core/PiSmmCore/Smi.c @@ -1,7 +1,7 @@ /** @file SMI management. =20 - Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made availa= ble=20 under the terms and conditions of the BSD License which accompanies this=20 distribution. The full text of the license may be found at =20 @@ -14,32 +14,15 @@ =20 #include "PiSmmCore.h" =20 -// -// SMM_HANDLER - used for each SMM handler -// - -#define SMI_ENTRY_SIGNATURE SIGNATURE_32('s','m','i','e') - - typedef struct { - UINTN Signature; - LIST_ENTRY AllEntries; // All entries - - EFI_GUID HandlerType; // Type of interrupt - LIST_ENTRY SmiHandlers; // All handlers -} SMI_ENTRY; - -#define SMI_HANDLER_SIGNATURE SIGNATURE_32('s','m','i','h') - - typedef struct { - UINTN Signature; - LIST_ENTRY Link; // Link on SMI_ENTRY.SmiHandl= ers - EFI_SMM_HANDLER_ENTRY_POINT2 Handler; // The smm handler's entry po= int - SMI_ENTRY *SmiEntry; -} SMI_HANDLER; - -LIST_ENTRY mRootSmiHandlerList =3D INITIALIZE_LIST_HEAD_VARIABLE (mRootSm= iHandlerList); LIST_ENTRY mSmiEntryList =3D INITIALIZE_LIST_HEAD_VARIABLE (mSmiEnt= ryList); =20 +SMI_ENTRY mRootSmiEntry =3D { + SMI_ENTRY_SIGNATURE, + INITIALIZE_LIST_HEAD_VARIABLE (mRootSmiEntry.AllEntries), + {0}, + INITIALIZE_LIST_HEAD_VARIABLE (mRootSmiEntry.SmiHandlers), +}; + /** Finds the SMI entry for the requested handler type. =20 @@ -137,8 +120,7 @@ SmiManage ( // // Root SMI handler // - - Head =3D &mRootSmiHandlerList; + SmiEntry =3D &mRootSmiEntry; } else { // // Non-root SMI handler @@ -150,9 +132,8 @@ SmiManage ( // return Status; } - - Head =3D &SmiEntry->SmiHandlers; } + Head =3D &SmiEntry->SmiHandlers; =20 for (Link =3D Head->ForwardLink; Link !=3D Head; Link =3D Link->ForwardL= ink) { SmiHandler =3D CR (Link, SMI_HANDLER, Link, SMI_HANDLER_SIGNATURE); @@ -252,13 +233,13 @@ SmiHandlerRegister ( =20 SmiHandler->Signature =3D SMI_HANDLER_SIGNATURE; SmiHandler->Handler =3D Handler; + SmiHandler->CallerAddr =3D (UINTN)RETURN_ADDRESS (0); =20 if (HandlerType =3D=3D NULL) { // // This is root SMI handler // - SmiEntry =3D NULL; - List =3D &mRootSmiHandlerList; + SmiEntry =3D &mRootSmiEntry; } else { // // None root SMI handler @@ -267,9 +248,8 @@ SmiHandlerRegister ( if (SmiEntry =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; } - - List =3D &SmiEntry->SmiHandlers; } + List =3D &SmiEntry->SmiHandlers; =20 SmiHandler->SmiEntry =3D SmiEntry; InsertTailList (List, &SmiHandler->Link); diff --git a/MdeModulePkg/Core/PiSmmCore/SmiHandlerProfile.c b/MdeModulePkg= /Core/PiSmmCore/SmiHandlerProfile.c new file mode 100644 index 0000000..f85c0f0 --- /dev/null +++ b/MdeModulePkg/Core/PiSmmCore/SmiHandlerProfile.c @@ -0,0 +1,1261 @@ +/** @file + SMI handler profile support. + +Copyright (c) 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 +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 IMPLI= ED. + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "PiSmmCore.h" + +typedef struct { + EFI_GUID FileGuid; + UINTN ImageRef; + UINTN EntryPoint; + UINTN ImageBase; + UINTN ImageSize; + UINTN PdbStringSize; + CHAR8 *PdbString; +} IMAGE_STRUCT; + +/** + Register SMI handler profile handler. +**/ +VOID +RegisterSmiHandlerProfileHandler( + VOID + ); + +/** + Retrieves and returns a pointer to the entry point to a PE/COFF image th= at has been loaded + into system memory with the PE/COFF Loader Library functions. + + Retrieves the entry point to the PE/COFF image specified by Pe32Data and= returns this entry + point in EntryPoint. If the entry point could not be retrieved from the= PE/COFF image, then + return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS. + If Pe32Data is NULL, then ASSERT(). + If EntryPoint is NULL, then ASSERT(). + + @param Pe32Data The pointer to the PE/COFF image that = is loaded in system memory. + @param EntryPoint The pointer to entry point to the PE/C= OFF image to return. + + @retval RETURN_SUCCESS EntryPoint was returned. + @retval RETURN_INVALID_PARAMETER The entry point could not be found in = the PE/COFF image. + +**/ +RETURN_STATUS +InternalPeCoffGetEntryPoint ( + IN VOID *Pe32Data, + OUT VOID **EntryPoint + ); + +extern LIST_ENTRY mSmiEntryList; +extern LIST_ENTRY mHardwareSmiEntryList; +extern SMI_ENTRY mRootSmiEntry; + +extern SMI_HANDLER_PROFILE_PROTOCOL mSmiHandlerProfile; + +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY mHardwareSmiEntryList =3D IN= ITIALIZE_LIST_HEAD_VARIABLE (mHardwareSmiEntryList); + +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY mRootSmiEntryList =3D INITIA= LIZE_LIST_HEAD_VARIABLE (mRootSmiEntryList); + +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY *mSmmCoreRootSmiEntryList = =3D &mRootSmiEntryList; +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY *mSmmCoreSmiEntryList =3D &m= SmiEntryList; +GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY *mSmmCoreHardwareSmiEntryLis= t =3D &mHardwareSmiEntryList; + +GLOBAL_REMOVE_IF_UNREFERENCED IMAGE_STRUCT *mImageStruct; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mImageStructCountMax; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mImageStructCount; + +GLOBAL_REMOVE_IF_UNREFERENCED VOID *mSmiHandlerProfileDatabase; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmiHandlerProfileDatabaseSize; + +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmmImageDatabaseSize; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmmRootSmiDatabaseSize; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmmSmiDatabaseSize; +GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmmHardwareSmiDatabaseSize; + +GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSmiHandlerProfileRecordingStatus; + +GLOBAL_REMOVE_IF_UNREFERENCED SMI_HANDLER_PROFILE_PROTOCOL mSmiHandlerPro= file =3D { + SmiHandlerProfileRegisterHandler, + SmiHandlerProfileUnregisterHandler, +}; + +/** + This function dump raw data. + + @param Data raw data + @param Size raw data size +**/ +VOID +InternalDumpData ( + IN UINT8 *Data, + IN UINTN Size + ) +{ + UINTN Index; + for (Index =3D 0; Index < Size; Index++) { + DEBUG ((DEBUG_INFO, "%02x ", (UINTN)Data[Index])); + } +} + +/** + Get GUID name for an image. + + @param[in] LoadedImage LoadedImage protocol. + @param[out] Guid Guid of the FFS +**/ +VOID +GetDriverGuid ( + IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage, + OUT EFI_GUID *Guid + ) +{ + EFI_GUID *FileName; + + FileName =3D NULL; + if ((DevicePathType(LoadedImage->FilePath) =3D=3D MEDIA_DEVICE_PATH) && + (DevicePathSubType(LoadedImage->FilePath) =3D=3D MEDIA_PIWG_FW_FILE_= DP)) { + FileName =3D &((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)LoadedImage->FileP= ath)->FvFileName; + } + if (FileName !=3D NULL) { + CopyGuid(Guid, FileName); + } else { + ZeroMem(Guid, sizeof(EFI_GUID)); + } +} + +/** + Add image structure. + + @param ImageBase image base + @param ImageSize image size + @param EntryPoint image entry point + @param Guid FFS GUID of the image + @param PdbString image PDB string +**/ +VOID +AddImageStruct( + IN UINTN ImageBase, + IN UINTN ImageSize, + IN UINTN EntryPoint, + IN EFI_GUID *Guid, + IN CHAR8 *PdbString + ) +{ + UINTN PdbStringSize; + + if (mImageStructCount >=3D mImageStructCountMax) { + ASSERT(FALSE); + return; + } + + CopyGuid(&mImageStruct[mImageStructCount].FileGuid, Guid); + mImageStruct[mImageStructCount].ImageRef =3D mImageStructCount; + mImageStruct[mImageStructCount].ImageBase =3D ImageBase; + mImageStruct[mImageStructCount].ImageSize =3D ImageSize; + mImageStruct[mImageStructCount].EntryPoint =3D EntryPoint; + if (PdbString !=3D NULL) { + PdbStringSize =3D AsciiStrSize(PdbString); + mImageStruct[mImageStructCount].PdbString =3D AllocateCopyPool (PdbStr= ingSize, PdbString); + if (mImageStruct[mImageStructCount].PdbString !=3D NULL) { + mImageStruct[mImageStructCount].PdbStringSize =3D PdbStringSize; + } + } + + mImageStructCount++; +} + +/** + return an image structure based upon image address. + + @param Address image address + + @return image structure +**/ +IMAGE_STRUCT * +AddressToImageStruct( + IN UINTN Address + ) +{ + UINTN Index; + + for (Index =3D 0; Index < mImageStructCount; Index++) { + if ((Address >=3D mImageStruct[Index].ImageBase) && + (Address < mImageStruct[Index].ImageBase + mImageStruct[Index].Ima= geSize)) { + return &mImageStruct[Index]; + } + } + return NULL; +} + +/** + return an image reference index based upon image address. + + @param Address image address + + @return image reference index +**/ +UINTN +AddressToImageRef( + IN UINTN Address + ) +{ + IMAGE_STRUCT *ImageStruct; + + ImageStruct =3D AddressToImageStruct(Address); + if (ImageStruct !=3D NULL) { + return ImageStruct->ImageRef; + } + return (UINTN)-1; +} + +/** + Collect SMM image information based upon loaded image protocol. +**/ +VOID +GetSmmLoadedImage( + VOID + ) +{ + EFI_STATUS Status; + UINTN NoHandles; + UINTN HandleBufferSize; + EFI_HANDLE *HandleBuffer; + UINTN Index; + EFI_LOADED_IMAGE_PROTOCOL *LoadedImage; + CHAR16 *PathStr; + EFI_SMM_DRIVER_ENTRY *LoadedImagePrivate; + UINTN EntryPoint; + VOID *EntryPointInImage; + EFI_GUID Guid; + CHAR8 *PdbString; + UINTN RealImageBase; + + HandleBufferSize =3D 0; + HandleBuffer =3D NULL; + Status =3D gSmst->SmmLocateHandle( + ByProtocol, + &gEfiLoadedImageProtocolGuid, + NULL, + &HandleBufferSize, + HandleBuffer + ); + if (Status !=3D EFI_BUFFER_TOO_SMALL) { + return; + } + HandleBuffer =3D AllocateZeroPool (HandleBufferSize); + if (HandleBuffer =3D=3D NULL) { + return; + } + Status =3D gSmst->SmmLocateHandle( + ByProtocol, + &gEfiLoadedImageProtocolGuid, + NULL, + &HandleBufferSize, + HandleBuffer + ); + if (EFI_ERROR(Status)) { + return; + } + + NoHandles =3D HandleBufferSize/sizeof(EFI_HANDLE); + mImageStructCountMax =3D NoHandles; + mImageStruct =3D AllocateZeroPool(mImageStructCountMax * sizeof(IMAGE_ST= RUCT)); + if (mImageStruct =3D=3D NULL) { + goto Done; + } + + for (Index =3D 0; Index < NoHandles; Index++) { + Status =3D gSmst->SmmHandleProtocol( + HandleBuffer[Index], + &gEfiLoadedImageProtocolGuid, + (VOID **)&LoadedImage + ); + if (EFI_ERROR(Status)) { + continue; + } + PathStr =3D ConvertDevicePathToText(LoadedImage->FilePath, TRUE, TRUE); + GetDriverGuid(LoadedImage, &Guid); + DEBUG ((DEBUG_INFO, "Image: %g ", &Guid)); + + EntryPoint =3D 0; + LoadedImagePrivate =3D BASE_CR(LoadedImage, EFI_SMM_DRIVER_ENTRY, SmmL= oadedImage); + RealImageBase =3D (UINTN)LoadedImage->ImageBase; + if (LoadedImagePrivate->Signature =3D=3D EFI_SMM_DRIVER_ENTRY_SIGNATUR= E) { + EntryPoint =3D (UINTN)LoadedImagePrivate->ImageEntryPoint; + if ((EntryPoint !=3D 0) && ((EntryPoint < (UINTN)LoadedImage->ImageB= ase) || (EntryPoint >=3D ((UINTN)LoadedImage->ImageBase + (UINTN)LoadedImag= e->ImageSize)))) { + // + // If the EntryPoint is not in the range of image buffer, it shoul= d come from emulation environment. + // So patch ImageBuffer here to align the EntryPoint. + // + Status =3D InternalPeCoffGetEntryPoint(LoadedImage->ImageBase, &En= tryPointInImage); + ASSERT_EFI_ERROR(Status); + RealImageBase =3D (UINTN)LoadedImage->ImageBase + EntryPoint - (UI= NTN)EntryPointInImage; + } + } + DEBUG ((DEBUG_INFO, "(0x%x - 0x%x", RealImageBase, (UINTN)LoadedImage-= >ImageSize)); + if (EntryPoint !=3D 0) { + DEBUG ((DEBUG_INFO, ", EntryPoint:0x%x", EntryPoint)); + } + DEBUG ((DEBUG_INFO, ")\n")); + + if (RealImageBase !=3D 0) { + PdbString =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) RealImageBa= se); + DEBUG ((DEBUG_INFO, " pdb - %a\n", PdbString)); + } else { + PdbString =3D NULL; + } + DEBUG ((DEBUG_INFO, " (%s)\n", PathStr)); + + AddImageStruct((UINTN)RealImageBase, (UINTN)LoadedImage->ImageSize, En= tryPoint, &Guid, PdbString); + } + +Done: + FreePool(HandleBuffer); + return; +} + +/** + Dump SMI child context. + + @param HandlerType the handler type + @param Context the handler context + @param ContextSize the handler context size +**/ +VOID +DumpSmiChildContext ( + IN EFI_GUID *HandlerType, + IN VOID *Context, + IN UINTN ContextSize + ) +{ + if (CompareGuid (HandlerType, &gEfiSmmSwDispatch2ProtocolGuid)) { + DEBUG ((DEBUG_INFO, " SwSmi - 0x%x\n", ((EFI_SMM_SW_REGISTER_CONTEXT = *)Context)->SwSmiInputValue)); + } else if (CompareGuid (HandlerType, &gEfiSmmSxDispatch2ProtocolGuid)) { + DEBUG ((DEBUG_INFO, " SxType - 0x%x\n", ((EFI_SMM_SX_REGISTER_CONTEXT= *)Context)->Type)); + DEBUG ((DEBUG_INFO, " SxPhase - 0x%x\n", ((EFI_SMM_SX_REGISTER_CONTEX= T *)Context)->Phase)); + } else if (CompareGuid (HandlerType, &gEfiSmmPowerButtonDispatch2Protoco= lGuid)) { + DEBUG ((DEBUG_INFO, " PowerButtonPhase - 0x%x\n", ((EFI_SMM_POWER_BUT= TON_REGISTER_CONTEXT *)Context)->Phase)); + } else if (CompareGuid (HandlerType, &gEfiSmmStandbyButtonDispatch2Proto= colGuid)) { + DEBUG ((DEBUG_INFO, " StandbyButtonPhase - 0x%x\n", ((EFI_SMM_STANDBY= _BUTTON_REGISTER_CONTEXT *)Context)->Phase)); + } else if (CompareGuid (HandlerType, &gEfiSmmPeriodicTimerDispatch2Proto= colGuid)) { + DEBUG ((DEBUG_INFO, " PeriodicTimerPeriod - %ld\n", ((EFI_SMM_PERIODI= C_TIMER_REGISTER_CONTEXT *)Context)->Period)); + DEBUG ((DEBUG_INFO, " PeriodicTimerSmiTickInterval - %ld\n", ((EFI_SM= M_PERIODIC_TIMER_REGISTER_CONTEXT *)Context)->SmiTickInterval)); + } else if (CompareGuid (HandlerType, &gEfiSmmGpiDispatch2ProtocolGuid)) { + DEBUG ((DEBUG_INFO, " GpiNum - 0x%lx\n", ((EFI_SMM_GPI_REGISTER_CONTE= XT *)Context)->GpiNum)); + } else if (CompareGuid (HandlerType, &gEfiSmmIoTrapDispatch2ProtocolGuid= )) { + DEBUG ((DEBUG_INFO, " IoTrapAddress - 0x%x\n", ((EFI_SMM_IO_TRAP_REGI= STER_CONTEXT *)Context)->Address)); + DEBUG ((DEBUG_INFO, " IoTrapLength - 0x%x\n", ((EFI_SMM_IO_TRAP_REGIS= TER_CONTEXT *)Context)->Length)); + DEBUG ((DEBUG_INFO, " IoTrapType - 0x%x\n", ((EFI_SMM_IO_TRAP_REGISTE= R_CONTEXT *)Context)->Type)); + } else if (CompareGuid (HandlerType, &gEfiSmmUsbDispatch2ProtocolGuid)) { + DEBUG ((DEBUG_INFO, " UsbType - 0x%x\n", ((SMI_HANDLER_PROFILE_USB_RE= GISTER_CONTEXT *)Context)->Type)); + DEBUG ((DEBUG_INFO, " UsbDevicePath - %s\n", ConvertDevicePathToText(= (EFI_DEVICE_PATH_PROTOCOL *)(((SMI_HANDLER_PROFILE_USB_REGISTER_CONTEXT *)C= ontext) + 1), TRUE, TRUE))); + } else { + DEBUG ((DEBUG_INFO, " Context - ")); + InternalDumpData (Context, ContextSize); + DEBUG ((DEBUG_INFO, "\n")); + } +} + +/** + Dump all SMI handlers associated with SmiEntry. + + @param SmiEntry SMI entry. +**/ +VOID +DumpSmiHandlerOnSmiEntry( + IN SMI_ENTRY *SmiEntry + ) +{ + LIST_ENTRY *ListEntry; + SMI_HANDLER *SmiHandler; + IMAGE_STRUCT *ImageStruct; + + ListEntry =3D &SmiEntry->SmiHandlers; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D &SmiEntry->SmiHandlers; + ListEntry =3D ListEntry->ForwardLink) { + SmiHandler =3D CR(ListEntry, SMI_HANDLER, Link, SMI_HANDLER_SIGNATURE); + ImageStruct =3D AddressToImageStruct((UINTN)SmiHandler->Handler); + if (ImageStruct !=3D NULL) { + DEBUG ((DEBUG_INFO, " Module - %g", &ImageStruct->FileGuid)); + } + if ((ImageStruct !=3D NULL) && (ImageStruct->PdbString[0] !=3D 0)) { + DEBUG ((DEBUG_INFO, " (Pdb - %a)", ImageStruct->PdbString)); + } + DEBUG ((DEBUG_INFO, "\n")); + if (SmiHandler->ContextSize !=3D 0) { + DumpSmiChildContext (&SmiEntry->HandlerType, SmiHandler->Context, Sm= iHandler->ContextSize); + } + DEBUG ((DEBUG_INFO, " Handler - 0x%x", SmiHandler->Handler)); + if (ImageStruct !=3D NULL) { + DEBUG ((DEBUG_INFO, " <=3D=3D RVA - 0x%x", (UINTN)SmiHandler->Handle= r - ImageStruct->ImageBase)); + } + DEBUG ((DEBUG_INFO, "\n")); + DEBUG ((DEBUG_INFO, " CallerAddr - 0x%x", SmiHandler->CallerAddr)); + if (ImageStruct !=3D NULL) { + DEBUG ((DEBUG_INFO, " <=3D=3D RVA - 0x%x", SmiHandler->CallerAddr - = ImageStruct->ImageBase)); + } + DEBUG ((DEBUG_INFO, "\n")); + } + + return; +} + +/** + Dump all SMI entry on the list. + + @param SmiEntryList a list of SMI entry. +**/ +VOID +DumpSmiEntryList( + IN LIST_ENTRY *SmiEntryList + ) +{ + LIST_ENTRY *ListEntry; + SMI_ENTRY *SmiEntry; + + ListEntry =3D SmiEntryList; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D SmiEntryList; + ListEntry =3D ListEntry->ForwardLink) { + SmiEntry =3D CR(ListEntry, SMI_ENTRY, AllEntries, SMI_ENTRY_SIGNATURE); + DEBUG ((DEBUG_INFO, "SmiEntry - %g\n", &SmiEntry->HandlerType)); + DumpSmiHandlerOnSmiEntry(SmiEntry); + } + + return; +} + +/** + SMM Ready To Lock event notification handler. + + This function collects all SMM image information and build SmiHandleProf= ile database, + and register SmiHandlerProfile SMI handler. + + @param[in] Protocol Points to the protocol's unique identifier. + @param[in] Interface Points to the interface instance. + @param[in] Handle The handle on which the interface was installed. + + @retval EFI_SUCCESS Notification handler runs successfully. +**/ +EFI_STATUS +EFIAPI +SmmReadyToLockInSmiHandlerProfile ( + IN CONST EFI_GUID *Protocol, + IN VOID *Interface, + IN EFI_HANDLE Handle + ) +{ + // + // Dump all image + // + DEBUG ((DEBUG_INFO, "##################\n")); + DEBUG ((DEBUG_INFO, "# IMAGE DATABASE #\n")); + DEBUG ((DEBUG_INFO, "##################\n")); + GetSmmLoadedImage (); + DEBUG ((DEBUG_INFO, "\n")); + + // + // Dump SMI Handler + // + DEBUG ((DEBUG_INFO, "########################\n")); + DEBUG ((DEBUG_INFO, "# SMI Handler DATABASE #\n")); + DEBUG ((DEBUG_INFO, "########################\n")); + + DEBUG ((DEBUG_INFO, "# 1. ROOT SMI Handler #\n")); + DEBUG_CODE ( + DumpSmiEntryList(mSmmCoreRootSmiEntryList); + ); + + DEBUG ((DEBUG_INFO, "# 2. GUID SMI Handler #\n")); + DEBUG_CODE ( + DumpSmiEntryList(mSmmCoreSmiEntryList); + ); + + DEBUG ((DEBUG_INFO, "# 3. Hardware SMI Handler #\n")); + DEBUG_CODE ( + DumpSmiEntryList(mSmmCoreHardwareSmiEntryList); + ); + + DEBUG ((DEBUG_INFO, "\n")); + + RegisterSmiHandlerProfileHandler(); + + if (mImageStruct !=3D NULL) { + FreePool(mImageStruct); + } + + return EFI_SUCCESS; +} + +/** + returns SMM image data base size. + + @return SMM image data base size. +**/ +UINTN +GetSmmImageDatabaseSize( + VOID + ) +{ + UINTN Size; + UINTN Index; + + Size =3D (sizeof(SMM_CORE_IMAGE_DATABASE_STRUCTURE)) * mImageStructCount; + for (Index =3D 0; Index < mImageStructCount; Index++) { + Size +=3D mImageStruct[Index].PdbStringSize; + } + return Size; +} + +/** + returns all SMI handlers' size associated with SmiEntry. + + @param SmiEntry SMI entry. + + @return all SMI handlers' size associated with SmiEntry. +**/ +UINTN +GetSmmSmiHandlerSizeOnSmiEntry( + IN SMI_ENTRY *SmiEntry + ) +{ + LIST_ENTRY *ListEntry; + SMI_HANDLER *SmiHandler; + UINTN Size; + + Size =3D 0; + ListEntry =3D &SmiEntry->SmiHandlers; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D &SmiEntry->SmiHandlers; + ListEntry =3D ListEntry->ForwardLink) { + SmiHandler =3D CR(ListEntry, SMI_HANDLER, Link, SMI_HANDLER_SIGNATURE); + Size +=3D sizeof(SMM_CORE_SMI_HANDLER_STRUCTURE) + SmiHandler->Context= Size; + } + + return Size; +} + +/** + return all SMI handler database size on the SMI entry list. + + @param SmiEntryList a list of SMI entry. + + @return all SMI handler database size on the SMI entry list. +**/ +UINTN +GetSmmSmiDatabaseSize( + IN LIST_ENTRY *SmiEntryList + ) +{ + LIST_ENTRY *ListEntry; + SMI_ENTRY *SmiEntry; + UINTN Size; + + Size =3D 0; + ListEntry =3D SmiEntryList; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D SmiEntryList; + ListEntry =3D ListEntry->ForwardLink) { + SmiEntry =3D CR(ListEntry, SMI_ENTRY, AllEntries, SMI_ENTRY_SIGNATURE); + Size +=3D sizeof(SMM_CORE_SMI_DATABASE_STRUCTURE); + Size +=3D GetSmmSmiHandlerSizeOnSmiEntry(SmiEntry); + } + return Size; +} + +/** + return SMI handler profile database size. + + @return SMI handler profile database size. +**/ +UINTN +GetSmiHandlerProfileDatabaseSize ( + VOID + ) +{ + mSmmImageDatabaseSize =3D GetSmmImageDatabaseSize(); + mSmmRootSmiDatabaseSize =3D GetSmmSmiDatabaseSize(mSmmCoreRootSmiEntryLi= st); + mSmmSmiDatabaseSize =3D GetSmmSmiDatabaseSize(mSmmCoreSmiEntryList); + mSmmHardwareSmiDatabaseSize =3D GetSmmSmiDatabaseSize(mSmmCoreHardwareSm= iEntryList); + + return mSmmImageDatabaseSize + mSmmSmiDatabaseSize + mSmmRootSmiDatabase= Size + mSmmHardwareSmiDatabaseSize; +} + +/** + get SMM image database. + + @param Data The buffer to hold SMM image database + @param ExpectedSize The expected size of the SMM image database + + @return SMM image data base size. +**/ +UINTN +GetSmmImageDatabaseData ( + IN OUT VOID *Data, + IN UINTN ExpectedSize + ) +{ + SMM_CORE_IMAGE_DATABASE_STRUCTURE *ImageStruct; + UINTN Size; + UINTN Index; + + ImageStruct =3D Data; + Size =3D 0; + for (Index =3D 0; Index < mImageStructCount; Index++) { + if (Size >=3D ExpectedSize) { + return 0; + } + if (sizeof(SMM_CORE_IMAGE_DATABASE_STRUCTURE) + mImageStruct[Index].Pd= bStringSize > ExpectedSize - Size) { + return 0; + } + ImageStruct->Header.Signature =3D SMM_CORE_IMAGE_DATABASE_SIGNATURE; + ImageStruct->Header.Length =3D (UINT32)(sizeof(SMM_CORE_IMAGE_DATABASE= _STRUCTURE) + mImageStruct[Index].PdbStringSize); + ImageStruct->Header.Revision =3D SMM_CORE_IMAGE_DATABASE_REVISION; + CopyGuid(&ImageStruct->FileGuid, &mImageStruct[Index].FileGuid); + ImageStruct->ImageRef =3D mImageStruct[Index].ImageRef; + ImageStruct->EntryPoint =3D mImageStruct[Index].EntryPoint; + ImageStruct->ImageBase =3D mImageStruct[Index].ImageBase; + ImageStruct->ImageSize =3D mImageStruct[Index].ImageSize; + ImageStruct->PdbStringOffset =3D sizeof(SMM_CORE_IMAGE_DATABASE_STRUCT= URE); + CopyMem ((VOID *)((UINTN)ImageStruct + ImageStruct->PdbStringOffset), = mImageStruct[Index].PdbString, mImageStruct[Index].PdbStringSize); + ImageStruct =3D (SMM_CORE_IMAGE_DATABASE_STRUCTURE *)((UINTN)ImageStru= ct + ImageStruct->Header.Length); + Size +=3D sizeof(SMM_CORE_IMAGE_DATABASE_STRUCTURE) + mImageStruct[Ind= ex].PdbStringSize; + } + + if (ExpectedSize !=3D Size) { + return 0; + } + return Size; +} + +/** + get all SMI handler data associated with SmiEntry. + + @param SmiEntry SMI entry. + @param Data The buffer to hold all SMI handler data + @param MaxSize The max size of the SMM image database + @param Count The count of the SMI handler. + + @return SMM image data base size. +**/ +UINTN +GetSmmSmiHandlerDataOnSmiEntry( + IN SMI_ENTRY *SmiEntry, + IN OUT VOID *Data, + IN UINTN MaxSize, + OUT UINTN *Count + ) +{ + SMM_CORE_SMI_HANDLER_STRUCTURE *SmiHandlerStruct; + LIST_ENTRY *ListEntry; + SMI_HANDLER *SmiHandler; + UINTN Size; + + SmiHandlerStruct =3D Data; + Size =3D 0; + *Count =3D 0; + ListEntry =3D &SmiEntry->SmiHandlers; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D &SmiEntry->SmiHandlers; + ListEntry =3D ListEntry->ForwardLink) { + SmiHandler =3D CR(ListEntry, SMI_HANDLER, Link, SMI_HANDLER_SIGNATURE); + if (Size >=3D MaxSize) { + *Count =3D 0; + return 0; + } + if (sizeof(SMM_CORE_SMI_HANDLER_STRUCTURE) + SmiHandler->ContextSize >= MaxSize - Size) { + *Count =3D 0; + return 0; + } + SmiHandlerStruct->Length =3D (UINT32)(sizeof(SMM_CORE_SMI_HANDLER_STRU= CTURE) + SmiHandler->ContextSize); + SmiHandlerStruct->CallerAddr =3D (UINTN)SmiHandler->CallerAddr; + SmiHandlerStruct->Handler =3D (UINTN)SmiHandler->Handler; + SmiHandlerStruct->ImageRef =3D AddressToImageRef((UINTN)SmiHandler->Ha= ndler); + SmiHandlerStruct->ContextBufferSize =3D (UINT32)SmiHandler->ContextSiz= e; + if (SmiHandler->ContextSize !=3D 0) { + SmiHandlerStruct->ContextBufferOffset =3D sizeof(SMM_CORE_SMI_HANDLE= R_STRUCTURE); + CopyMem ((UINT8 *)SmiHandlerStruct + SmiHandlerStruct->ContextBuffer= Offset, SmiHandler->Context, SmiHandler->ContextSize); + } else { + SmiHandlerStruct->ContextBufferOffset =3D 0; + } + Size +=3D sizeof(SMM_CORE_SMI_HANDLER_STRUCTURE) + SmiHandler->Context= Size; + SmiHandlerStruct =3D (SMM_CORE_SMI_HANDLER_STRUCTURE *)((UINTN)SmiHand= lerStruct + SmiHandlerStruct->Length); + *Count =3D *Count + 1; + } + + return Size; +} + +/** + get all SMI handler database on the SMI entry list. + + @param SmiEntryList a list of SMI entry. + @param HandlerCategory The handler category + @param Data The buffer to hold all SMI handler database + @param ExpectedSize The expected size of the SMM image database + + @return all SMI database size on the SMI entry list. +**/ +UINTN +GetSmmSmiDatabaseData( + IN LIST_ENTRY *SmiEntryList, + IN UINT32 HandlerCategory, + IN OUT VOID *Data, + IN UINTN ExpectedSize + ) +{ + SMM_CORE_SMI_DATABASE_STRUCTURE *SmiStruct; + LIST_ENTRY *ListEntry; + SMI_ENTRY *SmiEntry; + UINTN Size; + UINTN SmiHandlerSize; + UINTN SmiHandlerCount; + + SmiStruct =3D Data; + Size =3D 0; + ListEntry =3D SmiEntryList; + for (ListEntry =3D ListEntry->ForwardLink; + ListEntry !=3D SmiEntryList; + ListEntry =3D ListEntry->ForwardLink) { + SmiEntry =3D CR(ListEntry, SMI_ENTRY, AllEntries, SMI_ENTRY_SIGNATURE); + if (Size >=3D ExpectedSize) { + return 0; + } + if (sizeof(SMM_CORE_SMI_DATABASE_STRUCTURE) > ExpectedSize - Size) { + return 0; + } + + SmiStruct->Header.Signature =3D SMM_CORE_SMI_DATABASE_SIGNATURE; + SmiStruct->Header.Length =3D sizeof(SMM_CORE_SMI_DATABASE_STRUCTURE); + SmiStruct->Header.Revision =3D SMM_CORE_SMI_DATABASE_REVISION; + SmiStruct->HandlerCategory =3D HandlerCategory; + CopyGuid(&SmiStruct->HandlerType, &SmiEntry->HandlerType); + Size +=3D sizeof(SMM_CORE_SMI_DATABASE_STRUCTURE); + SmiHandlerSize =3D GetSmmSmiHandlerDataOnSmiEntry(SmiEntry, (UINT8 *)S= miStruct + SmiStruct->Header.Length, ExpectedSize - Size, &SmiHandlerCount); + SmiStruct->HandlerCount =3D SmiHandlerCount; + Size +=3D SmiHandlerSize; + SmiStruct->Header.Length +=3D (UINT32)SmiHandlerSize; + SmiStruct =3D (VOID *)((UINTN)SmiStruct + SmiStruct->Header.Length); + } + if (ExpectedSize !=3D Size) { + return 0; + } + return Size; +} + +/** + Get SMI handler profile database. + + @param Data the buffer to hold SMI handler profile database + + @retval EFI_SUCCESS the database is got. + @retval EFI_INVALID_PARAMETER the database size mismatch. +**/ +EFI_STATUS +GetSmiHandlerProfileDatabaseData( + IN OUT VOID *Data + ) +{ + UINTN SmmImageDatabaseSize; + UINTN SmmSmiDatabaseSize; + UINTN SmmRootSmiDatabaseSize; + UINTN SmmHardwareSmiDatabaseSize; + + DEBUG((DEBUG_VERBOSE, "GetSmiHandlerProfileDatabaseData\n")); + SmmImageDatabaseSize =3D GetSmmImageDatabaseData(Data, mSmmImageDatabase= Size); + if (SmmImageDatabaseSize !=3D mSmmImageDatabaseSize) { + DEBUG((DEBUG_ERROR, "GetSmiHandlerProfileDatabaseData - SmmImageDataba= seSize mismatch!\n")); + return EFI_INVALID_PARAMETER; + } + SmmRootSmiDatabaseSize =3D GetSmmSmiDatabaseData(mSmmCoreRootSmiEntryLis= t, SmmCoreSmiHandlerCategoryRootHandler, (UINT8 *)Data + SmmImageDatabaseSi= ze, mSmmRootSmiDatabaseSize); + if (SmmRootSmiDatabaseSize !=3D mSmmRootSmiDatabaseSize) { + DEBUG((DEBUG_ERROR, "GetSmiHandlerProfileDatabaseData - SmmRootSmiData= baseSize mismatch!\n")); + return EFI_INVALID_PARAMETER; + } + SmmSmiDatabaseSize =3D GetSmmSmiDatabaseData(mSmmCoreSmiEntryList, SmmCo= reSmiHandlerCategoryGuidHandler, (UINT8 *)Data + SmmImageDatabaseSize + mSm= mRootSmiDatabaseSize, mSmmSmiDatabaseSize); + if (SmmSmiDatabaseSize !=3D mSmmSmiDatabaseSize) { + DEBUG((DEBUG_ERROR, "GetSmiHandlerProfileDatabaseData - SmmSmiDatabase= Size mismatch!\n")); + return EFI_INVALID_PARAMETER; + } + SmmHardwareSmiDatabaseSize =3D GetSmmSmiDatabaseData(mSmmCoreHardwareSmi= EntryList, SmmCoreSmiHandlerCategoryHardwareHandler, (UINT8 *)Data + SmmIma= geDatabaseSize + SmmRootSmiDatabaseSize + SmmSmiDatabaseSize, mSmmHardwareS= miDatabaseSize); + if (SmmHardwareSmiDatabaseSize !=3D mSmmHardwareSmiDatabaseSize) { + DEBUG((DEBUG_ERROR, "GetSmiHandlerProfileDatabaseData - SmmHardwareSmi= DatabaseSize mismatch!\n")); + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + build SMI handler profile database. +**/ +VOID +BuildSmiHandlerProfileDatabase( + VOID + ) +{ + EFI_STATUS Status; + mSmiHandlerProfileDatabaseSize =3D GetSmiHandlerProfileDatabaseSize(); + mSmiHandlerProfileDatabase =3D AllocatePool(mSmiHandlerProfileDatabaseSi= ze); + if (mSmiHandlerProfileDatabase =3D=3D NULL) { + return; + } + Status =3D GetSmiHandlerProfileDatabaseData(mSmiHandlerProfileDatabase); + if (EFI_ERROR(Status)) { + FreePool(mSmiHandlerProfileDatabase); + mSmiHandlerProfileDatabase =3D NULL; + } +} + +/** + Copy SMI handler profile data. + + @param DataBuffer The buffer to hold SMI handler profile data. + @param DataSize On input, data buffer size. + On output, actual data buffer size copied. + @param DataOffset On input, data buffer offset to copy. + On output, next time data buffer offset to copy. + +**/ +VOID +SmiHandlerProfileCopyData( + OUT VOID *DataBuffer, + IN OUT UINT64 *DataSize, + IN OUT UINT64 *DataOffset + ) +{ + if (*DataOffset >=3D mSmiHandlerProfileDatabaseSize) { + *DataOffset =3D mSmiHandlerProfileDatabaseSize; + return; + } + if (mSmiHandlerProfileDatabaseSize - *DataOffset < *DataSize) { + *DataSize =3D mSmiHandlerProfileDatabaseSize - *DataOffset; + } + + CopyMem( + DataBuffer, + (UINT8 *)mSmiHandlerProfileDatabase + *DataOffset, + (UINTN)*DataSize + ); + *DataOffset =3D *DataOffset + *DataSize; +} + +/** + SMI handler profile handler to get info. + + @param SmiHandlerProfileParameterGetInfo The parameter of SMI handler pr= ofile get info. + +**/ +VOID +SmiHandlerProfileHandlerGetInfo( + IN SMI_HANDLER_PROFILE_PARAMETER_GET_INFO *SmiHandlerProfileParameterG= etInfo + ) +{ + BOOLEAN SmiHandlerProfileRecordingStatus; + + SmiHandlerProfileRecordingStatus =3D mSmiHandlerProfileRecordingStatus; + mSmiHandlerProfileRecordingStatus =3D FALSE; + + SmiHandlerProfileParameterGetInfo->DataSize =3D mSmiHandlerProfileDataba= seSize; + SmiHandlerProfileParameterGetInfo->Header.ReturnStatus =3D 0; + + mSmiHandlerProfileRecordingStatus =3D SmiHandlerProfileRecordingStatus; +} + +/** + SMI handler profile handler to get data by offset. + + @param SmiHandlerProfileParameterGetDataByOffset The parameter of SMI = handler profile get data by offset. + +**/ +VOID +SmiHandlerProfileHandlerGetDataByOffset( + IN SMI_HANDLER_PROFILE_PARAMETER_GET_DATA_BY_OFFSET *SmiHandlerProfi= leParameterGetDataByOffset + ) +{ + SMI_HANDLER_PROFILE_PARAMETER_GET_DATA_BY_OFFSET SmiHandlerProfileGet= DataByOffset; + BOOLEAN SmiHandlerProfileRec= ordingStatus; + + SmiHandlerProfileRecordingStatus =3D mSmiHandlerProfileRecordingStatus; + mSmiHandlerProfileRecordingStatus =3D FALSE; + + CopyMem(&SmiHandlerProfileGetDataByOffset, SmiHandlerProfileParameterGet= DataByOffset, sizeof(SmiHandlerProfileGetDataByOffset)); + + // + // Sanity check + // + if (!SmmIsBufferOutsideSmmValid((UINTN)SmiHandlerProfileGetDataByOffset.= DataBuffer, (UINTN)SmiHandlerProfileGetDataByOffset.DataSize)) { + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandlerGetDataByOffset: SMI hand= ler profile get data in SMRAM or overflow!\n")); + SmiHandlerProfileParameterGetDataByOffset->Header.ReturnStatus =3D (UI= NT64)(INT64)(INTN)EFI_ACCESS_DENIED; + goto Done; + } + + SmiHandlerProfileCopyData((VOID *)(UINTN)SmiHandlerProfileGetDataByOffse= t.DataBuffer, &SmiHandlerProfileGetDataByOffset.DataSize, &SmiHandlerProfil= eGetDataByOffset.DataOffset); + CopyMem(SmiHandlerProfileParameterGetDataByOffset, &SmiHandlerProfileGet= DataByOffset, sizeof(SmiHandlerProfileGetDataByOffset)); + SmiHandlerProfileParameterGetDataByOffset->Header.ReturnStatus =3D 0; + +Done: + mSmiHandlerProfileRecordingStatus =3D SmiHandlerProfileRecordingStatus; +} + +/** + Dispatch function for a Software SMI handler. + + Caution: This function may receive untrusted input. + Communicate buffer and buffer size are external input, so this function = will do basic validation. + + @param DispatchHandle The unique handle assigned to this handler by Smi= HandlerRegister(). + @param Context Points to an optional handler context which was s= pecified 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 SM= M environment. + @param CommBufferSize The size of the CommBuffer. + + @retval EFI_SUCCESS Command is handled successfully. +**/ +EFI_STATUS +EFIAPI +SmiHandlerProfileHandler( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context OPTIONAL, + IN OUT VOID *CommBuffer OPTIONAL, + IN OUT UINTN *CommBufferSize OPTIONAL + ) +{ + SMI_HANDLER_PROFILE_PARAMETER_HEADER *SmiHandlerProfileParamet= erHeader; + UINTN TempCommBufferSize; + + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler Enter\n")); + + if (mSmiHandlerProfileDatabase =3D=3D NULL) { + return EFI_SUCCESS; + } + + // + // If input is invalid, stop processing this SMI + // + if (CommBuffer =3D=3D NULL || CommBufferSize =3D=3D NULL) { + return EFI_SUCCESS; + } + + TempCommBufferSize =3D *CommBufferSize; + + if (TempCommBufferSize < sizeof(SMI_HANDLER_PROFILE_PARAMETER_HEADER)) { + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler: SMM communication buffe= r size invalid!\n")); + return EFI_SUCCESS; + } + + if (!SmmIsBufferOutsideSmmValid((UINTN)CommBuffer, TempCommBufferSize)) { + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler: SMM communication buffe= r in SMRAM or overflow!\n")); + return EFI_SUCCESS; + } + + SmiHandlerProfileParameterHeader =3D (SMI_HANDLER_PROFILE_PARAMETER_HEAD= ER *)((UINTN)CommBuffer); + SmiHandlerProfileParameterHeader->ReturnStatus =3D (UINT64)-1; + + switch (SmiHandlerProfileParameterHeader->Command) { + case SMI_HANDLER_PROFILE_COMMAND_GET_INFO: + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandlerGetInfo\n")); + if (TempCommBufferSize !=3D sizeof(SMI_HANDLER_PROFILE_PARAMETER_GET_I= NFO)) { + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler: SMM communication buf= fer size invalid!\n")); + return EFI_SUCCESS; + } + SmiHandlerProfileHandlerGetInfo((SMI_HANDLER_PROFILE_PARAMETER_GET_INF= O *)(UINTN)CommBuffer); + break; + case SMI_HANDLER_PROFILE_COMMAND_GET_DATA_BY_OFFSET: + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandlerGetDataByOffset\n")); + if (TempCommBufferSize !=3D sizeof(SMI_HANDLER_PROFILE_PARAMETER_GET_D= ATA_BY_OFFSET)) { + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler: SMM communication buf= fer size invalid!\n")); + return EFI_SUCCESS; + } + SmiHandlerProfileHandlerGetDataByOffset((SMI_HANDLER_PROFILE_PARAMETER= _GET_DATA_BY_OFFSET *)(UINTN)CommBuffer); + break; + default: + break; + } + + DEBUG((DEBUG_ERROR, "SmiHandlerProfileHandler Exit\n")); + + return EFI_SUCCESS; +} + +/** + Register SMI handler profile handler. +**/ +VOID +RegisterSmiHandlerProfileHandler ( + VOID + ) +{ + EFI_STATUS Status; + EFI_HANDLE DispatchHandle; + + Status =3D gSmst->SmiHandlerRegister ( + SmiHandlerProfileHandler, + &gSmiHandlerProfileGuid, + &DispatchHandle + ); + ASSERT_EFI_ERROR (Status); + + BuildSmiHandlerProfileDatabase(); +} + +/** + Finds the SMI entry for the requested handler type. + + @param HandlerType The type of the interrupt + @param Create Create a new entry if not found + + @return SMI entry +**/ +SMI_ENTRY * +SmmCoreFindHardwareSmiEntry ( + IN EFI_GUID *HandlerType, + IN BOOLEAN Create + ) +{ + LIST_ENTRY *Link; + SMI_ENTRY *Item; + SMI_ENTRY *SmiEntry; + + // + // Search the SMI entry list for the matching GUID + // + SmiEntry =3D NULL; + for (Link =3D mHardwareSmiEntryList.ForwardLink; + Link !=3D &mHardwareSmiEntryList; + Link =3D Link->ForwardLink) { + + Item =3D CR (Link, SMI_ENTRY, AllEntries, SMI_ENTRY_SIGNATURE); + if (CompareGuid (&Item->HandlerType, HandlerType)) { + // + // This is the SMI entry + // + SmiEntry =3D Item; + break; + } + } + + // + // If the protocol entry was not found and Create is TRUE, then + // allocate a new entry + // + if ((SmiEntry =3D=3D NULL) && Create) { + SmiEntry =3D AllocatePool (sizeof(SMI_ENTRY)); + if (SmiEntry !=3D NULL) { + // + // Initialize new SMI entry structure + // + SmiEntry->Signature =3D SMI_ENTRY_SIGNATURE; + CopyGuid ((VOID *)&SmiEntry->HandlerType, HandlerType); + InitializeListHead (&SmiEntry->SmiHandlers); + + // + // Add it to SMI entry list + // + InsertTailList (&mHardwareSmiEntryList, &SmiEntry->AllEntries); + } + } + return SmiEntry; +} + +/** + This function is called by SmmChildDispatcher module to report + a new SMI handler is registered, to SmmCore. + + @param This The protocol instance + @param HandlerGuid The GUID to identify the type of the handler. + For the SmmChildDispatch protocol, the HandlerGuid + must be the GUID of SmmChildDispatch protocol. + @param Handler The SMI handler. + @param CallerAddress The address of the module who registers the SMI h= andler. + @param Context The context of the SMI handler. + For the SmmChildDispatch protocol, the Context + must match the one defined for SmmChildDispatch p= rotocol. + @param ContextSize The size of the context in bytes. + For the SmmChildDispatch protocol, the Context + must match the one defined for SmmChildDispatch p= rotocol. + + @retval EFI_SUCCESS The information is recorded. + @retval EFI_OUT_OF_RESOURCES There is no enough resource to record the = information. +**/ +EFI_STATUS +EFIAPI +SmiHandlerProfileRegisterHandler ( + IN SMI_HANDLER_PROFILE_PROTOCOL *This, + IN EFI_GUID *HandlerGuid, + IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler, + IN PHYSICAL_ADDRESS CallerAddress, + IN VOID *Context, OPTIONAL + IN UINTN ContextSize OPTIONAL + ) +{ + SMI_HANDLER *SmiHandler; + SMI_ENTRY *SmiEntry; + LIST_ENTRY *List; + + SmiHandler =3D AllocateZeroPool (sizeof (SMI_HANDLER)); + if (SmiHandler =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + SmiHandler->Signature =3D SMI_HANDLER_SIGNATURE; + SmiHandler->Handler =3D Handler; + SmiHandler->CallerAddr =3D (UINTN)CallerAddress; + if (ContextSize !=3D 0 && Context !=3D NULL) { + if (CompareGuid (HandlerGuid, &gEfiSmmUsbDispatch2ProtocolGuid)) { + EFI_SMM_USB_REGISTER_CONTEXT *UsbContext; + UINTN DevicePathSize; + SMI_HANDLER_PROFILE_USB_REGISTER_CONTEXT *SmiHandlerUsbContext; + + ASSERT (ContextSize =3D=3D sizeof(EFI_SMM_USB_REGISTER_CONTEXT)); + + UsbContext =3D (EFI_SMM_USB_REGISTER_CONTEXT *)Context; + DevicePathSize =3D GetDevicePathSize (UsbContext->Device); + SmiHandlerUsbContext =3D AllocatePool (sizeof (SMI_HANDLER_PROFILE_U= SB_REGISTER_CONTEXT) + DevicePathSize); + if (SmiHandlerUsbContext !=3D NULL) { + SmiHandlerUsbContext->Type =3D UsbContext->Type; + SmiHandlerUsbContext->DevicePathSize =3D (UINT32)DevicePathSize; + CopyMem (SmiHandlerUsbContext + 1, UsbContext->Device, DevicePathS= ize); + SmiHandler->Context =3D SmiHandlerUsbContext; + } + } else { + SmiHandler->Context =3D AllocateCopyPool (ContextSize, Context); + } + } + if (SmiHandler->Context !=3D NULL) { + SmiHandler->ContextSize =3D ContextSize; + } + + SmiEntry =3D SmmCoreFindHardwareSmiEntry (HandlerGuid, TRUE); + if (SmiEntry =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + List =3D &SmiEntry->SmiHandlers; + + SmiHandler->SmiEntry =3D SmiEntry; + InsertTailList (List, &SmiHandler->Link); + + return EFI_SUCCESS; +} + +/** + This function is called by SmmChildDispatcher module to report + an existing SMI handler is unregistered, to SmmCore. + + @param This The protocol instance + @param HandlerGuid The GUID to identify the type of the handler. + For the SmmChildDispatch protocol, the HandlerGuid + must be the GUID of SmmChildDispatch protocol. + @param Handler The SMI handler. + + @retval EFI_SUCCESS The original record is removed. + @retval EFI_NOT_FOUND There is no record for the HandlerGuid and= handler. +**/ +EFI_STATUS +EFIAPI +SmiHandlerProfileUnregisterHandler ( + IN SMI_HANDLER_PROFILE_PROTOCOL *This, + IN EFI_GUID *HandlerGuid, + IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler + ) +{ + LIST_ENTRY *Link; + LIST_ENTRY *Head; + SMI_HANDLER *SmiHandler; + SMI_ENTRY *SmiEntry; + SMI_HANDLER *TargetSmiHandler; + + SmiEntry =3D SmmCoreFindHardwareSmiEntry (HandlerGuid, FALSE); + if (SmiEntry =3D=3D NULL) { + return EFI_NOT_FOUND; + } + + TargetSmiHandler =3D NULL; + Head =3D &SmiEntry->SmiHandlers; + for (Link =3D Head->ForwardLink; Link !=3D Head; Link =3D Link->ForwardL= ink) { + SmiHandler =3D CR (Link, SMI_HANDLER, Link, SMI_HANDLER_SIGNATURE); + if (SmiHandler->Handler =3D=3D Handler) { + TargetSmiHandler =3D SmiHandler; + break; + } + } + if (TargetSmiHandler =3D=3D NULL) { + return EFI_NOT_FOUND; + } + SmiHandler =3D TargetSmiHandler; + + RemoveEntryList (&SmiHandler->Link); + FreePool (SmiHandler); + + if (IsListEmpty (&SmiEntry->SmiHandlers)) { + RemoveEntryList (&SmiEntry->AllEntries); + FreePool (SmiEntry); + } + + return EFI_SUCCESS; +} + +/** + Initialize SmiHandler profile feature. +**/ +VOID +SmmCoreInitializeSmiHandlerProfile ( + VOID + ) +{ + EFI_STATUS Status; + VOID *Registration; + EFI_HANDLE Handle; + + if ((PcdGet8 (PcdSmiHandlerProfilePropertyMask) & 0x1) !=3D 0) { + InsertTailList (&mRootSmiEntryList, &mRootSmiEntry.AllEntries); + + Status =3D gSmst->SmmRegisterProtocolNotify ( + &gEfiSmmReadyToLockProtocolGuid, + SmmReadyToLockInSmiHandlerProfile, + &Registration + ); + ASSERT_EFI_ERROR (Status); + + Handle =3D NULL; + Status =3D gSmst->SmmInstallProtocolInterface ( + &Handle, + &gSmiHandlerProfileGuid, + EFI_NATIVE_INTERFACE, + &mSmiHandlerProfile + ); + ASSERT_EFI_ERROR (Status); + } +} + --=20 2.7.4.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel