From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM02-CY1-obe.outbound.protection.outlook.com (NAM02-CY1-obe.outbound.protection.outlook.com [40.92.4.19]) by mx.groups.io with SMTP id smtpd.web09.1128.1610663814755763055 for ; Thu, 14 Jan 2021 14:36:54 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@outlook.com header.s=selector1 header.b=Fgon5Mdk; spf=pass (domain: outlook.com, ip: 40.92.4.19, mailfrom: kun.q@outlook.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MEiNzwDwZUrC7rOnCOEF+2FZnpKb69tLAKHtsPrlAYK7mVqg54bDpehw4DIzIAxDJOoYg/XJFBbO0hZ0F3yYehfwLSIDFxDLm8MOFurroRrkQ7ajZJ+MOiKw40UUVYLxoyqdOvZCrl1TUnRc2tE2s7mmXogFAaMreB5qUGjWDkdvazXLs7iod19lgEXpyEsJQLQuTa6L8XIK8XKz8h9aVducfTTLdranbKftxElEgNpUs2b3F4R+NfnqIsDmsJie11kJDWQNaRGdBfctNEypbyvatMTD8euTt4FjMl4gs6EChdXMJgDeDH8M8prb7Df7NnNH055Rcqd0Z+qzr8/4zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LWNOoFOcMMec8WWXgHxQUQycQ3k7kOwKB2mrypdUXjo=; b=B0nbysCJwISA9YbfQRbHf+X1cFHB2eWJpqZJjlrcVoYDeQFIzPDRU+piLGlAdsMzCWB8O+a9AIX10qRKbUG4emL9Xol3nGVdYQa6SEL+G6VnKYgMgjDf1VVQnBS0p1PPnsZb+Ig2aNADA9wQyowRpjevgCKe1zK9E1vuUUsZQTLoIw7KbMoTiSLIko5APpRQvV6Wmuin4ZSiXyx9aW/5xwMr2gLNqL+Ci3duWnNIANX9jzG88BIYxTJrs7Vplj/iKPPseUuW4BJ7+OxJYoaRnX3Y3jE8tVT6k32bZZDdupymi9uSlVJXGEroF23N5+PwqAw021+D9CqGa0+q0452pw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LWNOoFOcMMec8WWXgHxQUQycQ3k7kOwKB2mrypdUXjo=; b=Fgon5Mdk6gz2+Qj0kO8Ys5pNUsUSUahRUwPYnT65aNWOL8KlrjnQFcnjhhCXWTkxUBiwcLXzj4iEV8zARXMrJ095JlMGZg3zPfgjxnOglh7TmGLg+hSafWh9nOzpxn2j3VXAZnfFsVarX0AmY98+ZxT7+4SL6kMrin09jqg5pZRJDYGgmHIdNc3Jm5Oki2xV0uKy3Ai+6CxcXbDaMKu21PZaLGa3sYu+6xRmWUW9OmklFDCvIQhaUIwl2TiVUWWtfDDyaXHDMLQ/1VpDiKxhFde8pP/uHzcl1jFf+DberRk4QwLrxafDgynISYGoYxzbJD29hdU6VTx/e8AiIlRfig== Received: from CY1NAM02FT023.eop-nam02.prod.protection.outlook.com (10.152.74.60) by CY1NAM02HT150.eop-nam02.prod.protection.outlook.com (10.152.74.215) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Thu, 14 Jan 2021 22:36:53 +0000 Received: from MWHPR06MB3102.namprd06.prod.outlook.com (2a01:111:e400:7e45::41) by CY1NAM02FT023.mail.protection.outlook.com (2a01:111:e400:7e45::237) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Thu, 14 Jan 2021 22:36:53 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:B53037027FED5E79E40D308D28BB8B6CDB31DFA7DD191088C5659AD070439935;UpperCasedChecksum:160E72451929EC1511085A587A6E2392E2566B94E554B4D54979994526A1318E;SizeAsReceived:7408;Count:45 Received: from MWHPR06MB3102.namprd06.prod.outlook.com ([fe80::acb3:ab69:563d:b0d6]) by MWHPR06MB3102.namprd06.prod.outlook.com ([fe80::acb3:ab69:563d:b0d6%5]) with mapi id 15.20.3742.012; Thu, 14 Jan 2021 22:36:53 +0000 From: "Kun Qin" To: devel@edk2.groups.io CC: Ard Biesheuvel , Sami Mujawar , Jiewen Yao , Supreeth Venkatesh Subject: [PATCH v3 05/18] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Date: Thu, 14 Jan 2021 14:36:24 -0800 Message-ID: X-Mailer: git-send-email 2.30.0.windows.1 X-TMN: [4MWkXVB+xUDIlL96UBQ0Ff7O9UWVWaR4] X-ClientProxiedBy: MW4PR04CA0389.namprd04.prod.outlook.com (2603:10b6:303:81::34) To MWHPR06MB3102.namprd06.prod.outlook.com (2603:10b6:301:3e::35) Return-Path: kun.q@outlook.com X-Microsoft-Original-Message-ID: <20210114223637.2737-1-kun.q@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (50.35.88.161) by MW4PR04CA0389.namprd04.prod.outlook.com (2603:10b6:303:81::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10 via Frontend Transport; Thu, 14 Jan 2021 22:36:53 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 45 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: ddd33a4a-80b5-469b-4b58-08d8b8dce476 X-MS-TrafficTypeDiagnostic: CY1NAM02HT150: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rix5ACOzykCPN6gWlc22jItKLyt4HpJzS1BWNZLt9/ggszZ7gFVCkmmMxI2eQHA78iEBGGsnjtfCMcO0es/UAR2/UjyXMG8/FrxLIrrmEKFHwhFX/8a98hI8Qmqum+Ri/NDJMzqNwCUjZJSlD1MtMrW0IJ1sgD641eEZtKoH+2ApEwy14sbOSSsa5+QVjBuNU3IJNhWb+KuFCLVFjG7Ktzv/JvqxWlb9oU25hqLuqBjDmmAPJ6GX+t7D+LY7DwCc X-MS-Exchange-AntiSpam-MessageData: 3MKaIB+XbN1vvxzRrRqFwi7+8LoL/elP9Lq24iV4+gp7kfORtMDd1g35K9QvEgg+I3TbchZ/d9v7ipKr7CcSRhha/5iRghDgwBjxsMVnO3UaxbahvbZLqYEbnV6Y4Wy02ZSr100WFh74xvCMG6Dvwg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jan 2021 22:36:53.7579 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-Network-Message-Id: ddd33a4a-80b5-469b-4b58-08d8b8dce476 X-MS-Exchange-CrossTenant-AuthSource: CY1NAM02FT023.eop-nam02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1NAM02HT150 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain This change extends StandaloneMmMemLib library to support X64 architecture. The implementation is ported from MdePkg/Library/SmmMemLib. Cc: Ard Biesheuvel Cc: Sami Mujawar Cc: Jiewen Yao Cc: Supreeth Venkatesh Signed-off-by: Kun Qin --- Notes: v3: - Updated destructor description of varibales to pass CI build. =20 v2: - Added routine to fix bug of not initializing MmRanges [Jiewen] - Extends support to x86 instead of x64 only [Hao] StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInter= nal.c | 27 ++++ StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c = | 52 +++++++ StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c= | 155 ++++++++++++++++++++ StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf = | 13 +- 4 files changed, 246 insertions(+), 1 deletion(-) diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneM= mMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Stan= daloneMmMemLibInternal.c index cb7c5e677a6b..4124959e0435 100644 --- a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLib= Internal.c +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLib= Internal.c @@ -40,4 +40,31 @@ MmMemLibInternalCalculateMaximumSupportAddress ( DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress =3D 0x%lx\n"= , mMmMemLibInternalMaximumSupportAddress)); } =20 +/** + Initialize cached Mmram Ranges from HOB. + + @retval EFI_UNSUPPORTED The routine is unable to extract MMRAM informa= tion. + @retval EFI_SUCCESS MmRanges are populated successfully. + +**/ +EFI_STATUS +MmMemLibInternalPopulateMmramRanges ( + VOID + ) +{ + // Not implemented for AARCH64. + return EFI_SUCCESS; +} + +/** + Deinitialize cached Mmram Ranges. + +**/ +VOID +MmMemLibInternalFreeMmramRanges ( + VOID + ) +{ + // Not implemented for AARCH64. +} =20 diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.= c b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c index b533bd8390cd..2737f95315eb 100644 --- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c @@ -37,6 +37,27 @@ MmMemLibInternalCalculateMaximumSupportAddress ( VOID ); =20 +/** + Initialize cached Mmram Ranges from HOB. + + @retval EFI_UNSUPPORTED The routine is unable to extract MMRAM informa= tion. + @retval EFI_SUCCESS MmRanges are populated successfully. + +**/ +EFI_STATUS +MmMemLibInternalPopulateMmramRanges ( + VOID + ); + +/** + Deinitialize cached Mmram Ranges. + +**/ +VOID +MmMemLibInternalFreeMmramRanges ( + VOID + ); + /** This function check if the buffer is valid per processor architecture an= d not overlap with MMRAM. =20 @@ -253,11 +274,42 @@ MemLibConstructor ( IN EFI_MM_SYSTEM_TABLE *MmSystemTable ) { + EFI_STATUS Status; =20 // // Calculate and save maximum support address // MmMemLibInternalCalculateMaximumSupportAddress (); =20 + // + // Initialize cached Mmram Ranges from HOB. + // + Status =3D MmMemLibInternalPopulateMmramRanges (); + + return Status; +} + +/** + Destructor for Mm Mem library. + + @param ImageHandle The image handle of the process. + @param MmSystemTable The EFI System Table pointer. + + @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +MemLibDestructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_MM_SYSTEM_TABLE *MmSystemTable + ) +{ + + // + // Deinitialize cached Mmram Ranges. + // + MmMemLibInternalFreeMmramRanges (); + return EFI_SUCCESS; } diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemL= ibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMe= mLibInternal.c new file mode 100644 index 000000000000..1a978541716a --- /dev/null +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInter= nal.c @@ -0,0 +1,155 @@ +/** @file + Internal ARCH Specific file of MM memory check library. + + MM memory check library implementation. This library consumes MM_ACCESS_= PROTOCOL + to get MMRAM information. In order to use this library instance, the pla= tform should produce + all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware= (like MM Core + and MM driver) and/or specific dedicated hardware. + + Copyright (c) 2015, Intel Corporation. All rights reserved.
+ Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
+ Copyright (c) Microsoft Corporation. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include +#include + +#include +#include + +// +// Maximum support address used to check input buffer +// +extern EFI_PHYSICAL_ADDRESS mMmMemLibInternalMaximumSupportAddress; +extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges; +extern UINTN mMmMemLibInternalMmramCount; + +/** + Calculate and save the maximum support address. + +**/ +VOID +MmMemLibInternalCalculateMaximumSupportAddress ( + VOID + ) +{ + VOID *Hob; + UINT32 RegEax; + UINT8 PhysicalAddressBits; + + // + // Get physical address bits supported. + // + Hob =3D GetFirstHob (EFI_HOB_TYPE_CPU); + if (Hob !=3D NULL) { + PhysicalAddressBits =3D ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace; + } else { + AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL); + if (RegEax >=3D 0x80000008) { + AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL); + PhysicalAddressBits =3D (UINT8) RegEax; + } else { + PhysicalAddressBits =3D 36; + } + } + // + // IA-32e paging translates 48-bit linear addresses to 52-bit physical a= ddresses. + // + ASSERT (PhysicalAddressBits <=3D 52); + if (PhysicalAddressBits > 48) { + PhysicalAddressBits =3D 48; + } + + // + // Save the maximum support address in one global variable + // + mMmMemLibInternalMaximumSupportAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)= (LShiftU64 (1, PhysicalAddressBits) - 1); + DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress =3D 0x%lx\n"= , mMmMemLibInternalMaximumSupportAddress)); +} + +/** + Initialize cached Mmram Ranges from HOB. + + @retval EFI_UNSUPPORTED The routine is unable to extract MMRAM informa= tion. + @retval EFI_SUCCESS MmRanges are populated successfully. + +**/ +EFI_STATUS +MmMemLibInternalPopulateMmramRanges ( + VOID + ) +{ + VOID *HobStart; + EFI_HOB_GUID_TYPE *GuidHob; + MM_CORE_DATA_HOB_DATA *DataInHob; + MM_CORE_PRIVATE_DATA *MmCorePrivateData; + EFI_HOB_GUID_TYPE *MmramRangesHob; + EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData; + EFI_MMRAM_DESCRIPTOR *MmramDescriptors; + + HobStart =3D GetHobList (); + DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart)); + + // + // Extract MM Core Private context from the Hob. If absent search for + // a Hob containing the MMRAM ranges + // + GuidHob =3D GetNextGuidHob (&gMmCoreDataHobGuid, HobStart); + if (GuidHob =3D=3D NULL) { + MmramRangesHob =3D GetFirstGuidHob (&gEfiMmPeiMmramMemoryReserveGuid); + if (MmramRangesHob =3D=3D NULL) { + return EFI_UNSUPPORTED; + } + + MmramRangesHobData =3D GET_GUID_HOB_DATA (MmramRangesHob); + if (MmramRangesHobData =3D=3D NULL || MmramRangesHobData->Descriptor = =3D=3D NULL) { + return EFI_UNSUPPORTED; + } + + mMmMemLibInternalMmramCount =3D MmramRangesHobData->NumberOfMmReserved= Regions; + MmramDescriptors =3D MmramRangesHobData->Descriptor; + } else { + DataInHob =3D GET_GUID_HOB_DATA (GuidHob); + if (DataInHob =3D=3D NULL) { + return EFI_UNSUPPORTED; + } + + MmCorePrivateData =3D (MM_CORE_PRIVATE_DATA *) (UINTN) DataInHob->Addr= ess; + if (MmCorePrivateData =3D=3D NULL || MmCorePrivateData->MmramRanges = =3D=3D 0) { + return EFI_UNSUPPORTED; + } + + mMmMemLibInternalMmramCount =3D (UINTN) MmCorePrivateData->MmramRangeC= ount; + MmramDescriptors =3D (EFI_MMRAM_DESCRIPTOR *) (UINTN) MmCorePrivateDat= a->MmramRanges; + } + + mMmMemLibInternalMmramRanges =3D AllocatePool (mMmMemLibInternalMmramCou= nt * sizeof (EFI_MMRAM_DESCRIPTOR)); + if (mMmMemLibInternalMmramRanges) { + CopyMem (mMmMemLibInternalMmramRanges, + MmramDescriptors, + mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR)); + } + + return EFI_SUCCESS; +} + +/** + Deinitialize cached Mmram Ranges. + +**/ +VOID +MmMemLibInternalFreeMmramRanges ( + VOID + ) +{ + if (mMmMemLibInternalMmramRanges !=3D NULL) { + FreePool (mMmMemLibInternalMmramRanges); + } +} + diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.= inf b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf index 49da02e54e6d..062b0d7a1161 100644 --- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf @@ -8,6 +8,7 @@ # # Copyright (c) 2015, Intel Corporation. All rights reserved.
# Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
+# Copyright (c) Microsoft Corporation. # # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -22,16 +23,20 @@ [Defines] PI_SPECIFICATION_VERSION =3D 0x00010032 LIBRARY_CLASS =3D MemLib|MM_STANDALONE MM_CORE_STANDALO= NE CONSTRUCTOR =3D MemLibConstructor + DESTRUCTOR =3D MemLibDestructor =20 # # The following information is for reference only and not required by the = build tools. # -# VALID_ARCHITECTURES =3D AARCH64 +# VALID_ARCHITECTURES =3D IA32 X64 AARCH64 # =20 [Sources.Common] StandaloneMmMemLib.c =20 +[Sources.IA32, Sources.X64] + X86StandaloneMmMemLibInternal.c + [Sources.AARCH64] AArch64/StandaloneMmMemLibInternal.c =20 @@ -42,3 +47,9 @@ [Packages] [LibraryClasses] BaseMemoryLib DebugLib + HobLib + MemoryAllocationLib + +[Guids] + gMmCoreDataHobGuid ## SOMETIMES_CONSUMES ## HOB + gEfiMmPeiMmramMemoryReserveGuid ## SOMETIMES_CONSUMES ## HOB --=20 2.30.0.windows.1