From nobody Thu May 16 17:29:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+106041+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+106041+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1686631461; cv=none; d=zohomail.com; s=zohoarc; b=BdiGs0c2V9FoVHY0LUQDi5/siWHjtrcpB+xv66NEElo59TojXC/MZT36LpRXlf65DjEr6THUvbXfm1ZJjqaMBXu3ECc11oGqRXbrUmG+yNFdSLTOkQJQgfiR2H2QhUSKLkEVq67/ccN4eAbkNG/aTpfSpaPUL7Z/WfqIyqqRUx4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686631461; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=/lbWQRQYI1e6lEF/Z0LSPVlDJWIji9VlMYwkQTH0If0=; b=eNY8y7LlnwdpWJXYR8OAJpmPUExUkCKsTHQ/WA2RImwj9yz3MDGluOSYbrZGLTFhFciE5zjXZbHwtEAF+ZQSls6Iw2bXwCH9UgZFfQbQdrvpqbVwWpAe2qJOfTuizHfGJPcbqCTBDKr0Fr4xjFs73Y4MdreGf9xQmNg6U0f0qlQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+106041+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1686631461228669.0524369710655; Mon, 12 Jun 2023 21:44:21 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id D3LqYY1788612xlJi2fk5FHF; Mon, 12 Jun 2023 21:44:20 -0700 X-Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web11.4672.1686623385072916190 for ; Mon, 12 Jun 2023 19:29:45 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="342900524" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="342900524" X-Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 19:29:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="855912993" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="855912993" X-Received: from hongbin1-mobl1.ccr.corp.intel.com ([10.254.210.115]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 19:29:41 -0700 From: "Zhang, Hongbin1" To: devel@edk2.groups.io Cc: "Zhang, Hongbin1" , Jiewen Yao , Ray Ni , Star Zeng , Jiaxin Wu , Sami Mujawar , Ard Biesheuvel , Supreeth Venkatesh Subject: [edk2-devel] [PATCH v3] StandaloneMmPkg: Add StandaloneMmIplPei driver. Date: Tue, 13 Jun 2023 10:29:36 +0800 Message-Id: <20230613022936.1384-1-hongbin1.zhang@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,hongbin1.zhang@intel.com X-Gm-Message-State: 08tpN2EaUwghYsnNCrsZiwMjx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1686631460; bh=4koq7lVY300QcVkSxOlxdv71XdenhVgu04cnboWum9s=; h=Cc:Date:From:Reply-To:Subject:To; b=T9KA7XwTO/1YzvzUXGSSI1dt0242M7zWVmjjhkIN2GYZHqaNyRbpH3rd1oIpz3cjiVn A7Jznzts5E3GJUwJ/77BACMc68vBKQjQW7tFtWbwe2ICLWA5EkhWYyboRL+25XO8SE1+S dBkdfN4KFHh4thqLTtxxDSxd2cWsxl0726E= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1686631463194100003 Content-Type: text/plain; charset="utf-8" Add StandaloneMmIplPei IA32/X64 driver at PEI stage. FSP will use this driver to load Standalone MM code to dispatch other Standalone MM drivers. And this is the 1st patch to implement the entrypoint to find the correct SMRAM range and dump it Signed-off-by: Hongbin1 Zhang Cc: Jiewen Yao Cc: Ray Ni Cc: Star Zeng Cc: Jiaxin Wu Cc: Sami Mujawar Cc: Ard Biesheuvel Cc: Supreeth Venkatesh --- StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c | 266 ++= ++++++++++++++++++ StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf | 54 ++= ++ StandaloneMmPkg/StandaloneMmPkg.dsc | 15 +- 3 files changed, 333 insertions(+), 2 deletions(-) diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.= c b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c new file mode 100644 index 0000000000..16e7d59d0e --- /dev/null +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c @@ -0,0 +1,266 @@ +/** @file + SMM IPL that load the SMM Core into SMRAM at PEI stage + + Copyright (c) 2023, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// MM Core Private Data structure that contains the data shared between +// the SMM IPL and the Standalone MM Core. +// +MM_CORE_PRIVATE_DATA mMmCorePrivateData =3D { + MM_CORE_PRIVATE_DATA_SIGNATURE, // Signature + 0, // MmramRangeCount + 0, // MmramRanges + 0, // MmEntryPoint + FALSE, // MmEntryPointRegistered + FALSE, // InMm + 0, // Mmst + 0, // CommunicationBuffer + 0, // BufferSize + EFI_SUCCESS, // ReturnStatus + 0, // MmCoreImageBase + 0, // MmCoreImageSize + 0, // MmCoreEntryPoint + 0, // StandaloneBfvAddress +}; + +// +// Global pointer used to access mMmCorePrivateData from outside and insid= e SMM +// +MM_CORE_PRIVATE_DATA *gMmCorePrivate; + +// +// SMM IPL global variables +// +PEI_SMM_ACCESS_PPI *mSmmAccess; +EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange; +EFI_PHYSICAL_ADDRESS mSmramCacheBase; +UINT64 mSmramCacheSize; +UINTN mSmramRangeCount; + +/** + Find the maximum SMRAM cache range that covers the range specified by Sm= ramRange. + + 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. +**/ +VOID +GetSmramCacheRange ( + IN EFI_SMRAM_DESCRIPTOR *SmramRange, + OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase, + OUT UINT64 *SmramCacheSize + ) +{ + UINTN Index; + EFI_PHYSICAL_ADDRESS RangeCpuStart; + UINT64 RangePhysicalSize; + BOOLEAN FoundAdjacentRange; + EFI_SMRAM_DESCRIPTOR *SmramRanges; + + *SmramCacheBase =3D SmramRange->CpuStart; + *SmramCacheSize =3D SmramRange->PhysicalSize; + + SmramRanges =3D (EFI_SMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRang= es; + do { + FoundAdjacentRange =3D FALSE; + for (Index =3D 0; Index < gMmCorePrivate->MmramRangeCount; Index++) { + RangeCpuStart =3D SmramRanges[Index].CpuStart; + RangePhysicalSize =3D SmramRanges[Index].PhysicalSize; + if ((RangeCpuStart < *SmramCacheBase) && (*SmramCacheBase =3D=3D (Ra= ngeCpuStart + RangePhysicalSize))) { + *SmramCacheBase =3D RangeCpuStart; + *SmramCacheSize +=3D RangePhysicalSize; + FoundAdjacentRange =3D TRUE; + } else if (((*SmramCacheBase + *SmramCacheSize) =3D=3D RangeCpuStart= ) && (RangePhysicalSize > 0)) { + *SmramCacheSize +=3D RangePhysicalSize; + FoundAdjacentRange =3D TRUE; + } + } + } while (FoundAdjacentRange); +} + +/** + Get full SMRAM ranges. + + It will get SMRAM ranges from SmmAccess PPI. It will also reserve one en= try + for SMM core. + + @param[in] PeiServices Describes the list of possible PEI Ser= vices. + @param[out] FullSmramRangeCount Output pointer to full SMRAM range cou= nt. + + @return Pointer to full SMRAM ranges. + +**/ +EFI_SMRAM_DESCRIPTOR * +GetFullSmramRanges ( + IN CONST EFI_PEI_SERVICES **PeiServices, + OUT UINTN *FullSmramRangeCount + ) +{ + EFI_STATUS Status; + UINTN Size; + EFI_SMRAM_DESCRIPTOR *FullSmramRanges; + UINTN AdditionSmramRangeCount; + + // + // Get SMRAM information. + // + Size =3D 0; + Status =3D mSmmAccess->GetCapabilities ((EFI_PEI_SERVICES **)PeiServices= , mSmmAccess, &Size, NULL); + ASSERT (Status =3D=3D EFI_BUFFER_TOO_SMALL); + + mSmramRangeCount =3D Size / sizeof (EFI_SMRAM_DESCRIPTOR); + + // + // Reserve one entry SMM Core in the full SMRAM ranges. + // + AdditionSmramRangeCount =3D 1; + + *FullSmramRangeCount =3D mSmramRangeCount + AdditionSmramRangeCount; + Size =3D (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESC= RIPTOR); + FullSmramRanges =3D (EFI_SMRAM_DESCRIPTOR *)AllocateZeroPool (Size); + ASSERT (FullSmramRanges !=3D NULL); + if (FullSmramRanges =3D=3D NULL) { + return NULL; + } + + Status =3D mSmmAccess->GetCapabilities ((EFI_PEI_SERVICES **)PeiServices= , mSmmAccess, &Size, FullSmramRanges); + + ASSERT_EFI_ERROR (Status); + + return FullSmramRanges; +} + +/** + The Entry Point for SMM IPL at PEI stage + + Load SMM Core into SMRAM. + + @param FileHandle Handle of the file being invoked. + @param PeiServices Describes the list of possible PEI Services. + + @retval EFI_SUCCESS The entry point is executed successfully. + @retval Other Some error occurred when executing this entry poi= nt. + +**/ +EFI_STATUS +EFIAPI +StandaloneMmIplPeiEntry ( + IN EFI_PEI_FILE_HANDLE FileHandle, + IN CONST EFI_PEI_SERVICES **PeiServices + ) +{ + EFI_STATUS Status; + UINTN Index; + UINT64 MaxSize; + MM_CORE_DATA_HOB_DATA SmmCoreDataHobData; + EFI_SMRAM_DESCRIPTOR *MmramRanges; + + // + // Build Hob for SMM and DXE phase + // + SmmCoreDataHobData.Address =3D (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateRunt= imePages (EFI_SIZE_TO_PAGES (sizeof (mMmCorePrivateData))); + ASSERT (SmmCoreDataHobData.Address !=3D 0); + gMmCorePrivate =3D (VOID *)(UINTN)SmmCoreDataHobData.Address; + CopyMem ((VOID *)(UINTN)SmmCoreDataHobData.Address, &mMmCorePrivateData,= sizeof (mMmCorePrivateData)); + DEBUG ((DEBUG_INFO, "gMmCorePrivate - 0x%x\n", gMmCorePrivate)); + + BuildGuidDataHob ( + &gMmCoreDataHobGuid, + (VOID *)&SmmCoreDataHobData, + sizeof (SmmCoreDataHobData) + ); + + // + // Get SMM Access PPI + // + Status =3D PeiServicesLocatePpi (&gPeiSmmAccessPpiGuid, 0, NULL, (VOID *= *)&mSmmAccess); + ASSERT_EFI_ERROR (Status); + + // + // Get SMRAM information + // + gMmCorePrivate->MmramRanges =3D (EFI_PHYSICAL_ADDRESS)(UINTN)GetFullSmra= mRanges (PeiServices, (UINTN *)&gMmCorePrivate->MmramRangeCount); + ASSERT (gMmCorePrivate->MmramRanges !=3D 0); + if (gMmCorePrivate->MmramRanges =3D=3D 0) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Open all SMRAM ranges + // + for (Index =3D 0; Index < mSmramRangeCount; Index++) { + Status =3D mSmmAccess->Open ((EFI_PEI_SERVICES **)PeiServices, mSmmAcc= ess, Index); + ASSERT_EFI_ERROR (Status); + } + + // + // Print debug message that the SMRAM window is now open. + // + DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n")); + + // + // Find the largest SMRAM range between 1MB and 4GB that is at least 256= KB - 4K in size + // + mCurrentSmramRange =3D NULL; + MmramRanges =3D (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->Mm= ramRanges; + if (MmramRanges =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Fail to retrieve MmramRanges\n")); + return EFI_UNSUPPORTED; + } + + for (Index =3D 0, MaxSize =3D SIZE_256KB - EFI_PAGE_SIZE; Index < gMmCor= ePrivate->MmramRangeCount; Index++) { + // + // Skip any SMRAM region that is already allocated, needs testing, or = needs ECC initialization + // + if ((MmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTI= NG | EFI_NEEDS_ECC_INITIALIZATION)) !=3D 0) { + continue; + } + + if (MmramRanges[Index].CpuStart >=3D BASE_1MB) { + if ((MmramRanges[Index].CpuStart + MmramRanges[Index].PhysicalSize) = <=3D BASE_4GB) { + if (MmramRanges[Index].PhysicalSize >=3D MaxSize) { + MaxSize =3D MmramRanges[Index].PhysicalSize; + mCurrentSmramRange =3D &MmramRanges[Index]; + } + } + } + } + + if (mCurrentSmramRange !=3D NULL) { + // + // Print debug message showing SMRAM window that will be used by SMM I= PL and SMM Core + // + DEBUG (( + DEBUG_INFO, + "SMM IPL found SMRAM window %p - %p\n", + (VOID *)(UINTN)mCurrentSmramRange->CpuStart, + (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->P= hysicalSize - 1) + )); + + GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCache= Size); + } else { + // + // Print error message that there are not enough SMRAM resources to lo= ad the SMM Core. + // + DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM regi= on to load SMM Core\n")); + } + + return EFI_SUCCESS; +} diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.= inf b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf new file mode 100644 index 0000000000..372c59c1fa --- /dev/null +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf @@ -0,0 +1,54 @@ +## @file +# This module provide a Standalone SMM compliant implementation of SMM IP= L PEIM. +# +# Copyright (c) 2023, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D StandaloneMmIplPei + FILE_GUID =3D 578A0D17-2DC0-4C7D-A121-D8D771923BB0 + MODULE_TYPE =3D PEIM + VERSION_STRING =3D 1.0 + PI_SPECIFICATION_VERSION =3D 0x0001000A + ENTRY_POINT =3D StandaloneMmIplPeiEntry + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + StandaloneMmIplPei.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + StandaloneMmPkg/StandaloneMmPkg.dec + +[LibraryClasses] + PeimEntryPoint + PeiServicesTablePointerLib + PeiServicesLib + BaseLib + BaseMemoryLib + MemoryAllocationLib + DebugLib + HobLib + IntrinsicLib + +[Guids] + gMmCoreDataHobGuid + +[Ppis] + gPeiSmmAccessPpiGuid ## CONSUMES + +[Pcd] + +[Depex] + gPeiSmmAccessPpiGuid + diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc b/StandaloneMmPkg/Standalo= neMmPkg.dsc index 8012f93b7d..d88471fe82 100644 --- a/StandaloneMmPkg/StandaloneMmPkg.dsc +++ b/StandaloneMmPkg/StandaloneMmPkg.dsc @@ -20,7 +20,7 @@ PLATFORM_VERSION =3D 1.0 DSC_SPECIFICATION =3D 0x00010011 OUTPUT_DIRECTORY =3D Build/StandaloneMm - SUPPORTED_ARCHITECTURES =3D AARCH64|X64|ARM + SUPPORTED_ARCHITECTURES =3D AARCH64|X64|ARM|IA32 BUILD_TARGETS =3D DEBUG|RELEASE SKUID_IDENTIFIER =3D DEFAULT =20 @@ -60,6 +60,14 @@ StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint= /StandaloneMmDriverEntryPoint.inf VariableMmDependency|StandaloneMmPkg/Library/VariableMmDependency/Variab= leMmDependency.inf =20 +[LibraryClasses.common.PEIM] + HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf + PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf + MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAlloc= ationLib.inf + PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf + PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLib/Pei= ServicesTablePointerLib.inf + IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf + [LibraryClasses.AARCH64, LibraryClasses.ARM] ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf StandaloneMmMmuLib|ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmL= ib.inf @@ -104,7 +112,7 @@ # generated for it, but the binary will not be put into any firmware= volume. # ##########################################################################= ######################### -[Components.common] +[Components.AARCH64, Components.ARM, Components.X64] # # MM Core # @@ -122,6 +130,9 @@ StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf StandaloneMmPkg/Library/StandaloneMmPeCoffExtraActionLib/StandaloneMmPeC= offExtraActionLib.inf =20 +[Components.X64, Components.IA32] + StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf + ##########################################################################= ######################### # # BuildOptions Section - Define the module specific tool chain flags that = should be used as --=20 2.37.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#106041): https://edk2.groups.io/g/devel/message/106041 Mute This Topic: https://groups.io/mt/99499490/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-