From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.92.40.54]) by mx.groups.io with SMTP id smtpd.web11.18283.1611690448617484737 for ; Tue, 26 Jan 2021 11:47:28 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@outlook.com header.s=selector1 header.b=pdgsLzH4; spf=pass (domain: outlook.com, ip: 40.92.40.54, mailfrom: kun.q@outlook.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ihi56Ydvd99GOy4SPhrnyeqGEzOdr66U7TThtYxzI4/LdJjAWdltdzUigCQS7dHruniYasxuxxdjJ6K8R7KosZav5DGCwd+hOfmx1C3oygExo7Q0OsqKil9OAVt9XmjSbwtPTLofZP/3UNYU0FUcg+y3SlpwojqYY/4WinjOy0EORwneXebTKRmy7+GtPLma8D0FCwqYnbBbyYGqiWHEBFQnwFJfO0zZAqDNZI31iAXip7Fpni/6vXxmDyAOR77Z6mhZyl250TnkXRJ6wLHjVqkofykMzCuH5Ix1spKWFWbRMUFtKSn6TGKlXAF0sB9Q4wsCVrPpNR/WYGEVFm6dsw== 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=6QqI7aTWKFVEWRVVQhqky8X+U98SqxwhYNRdYwyyQ1M=; b=fUOs0NsorUKpcJSz0t6Fr/Hrkii5ZpBEw/WyXOUaXYulsvHfB6I9VrROu6uocBfyRTXxxadKnuGpIynsqhHFPr69lv0YrZMEc0R5h4WaSHv6+qNWDBwb8vGmZUGWbg7waqSEf58ZpyZ1z0u7jW+w32ZOSr3krblwv9C44+ut1nS1lxH+YkFxIje4hcOw46HUL618dOgvaFDviRseCkwSCUchureH8f5BLQ74TASw/vpJmDOoN2942v51tm4TantSm1DLTirGUUU8Mv8bnxwbAFAU9EEyfaKgqxC7K/wcork03XNc9uKIYABG6jYRaoyA3/l4drwYr2SCuAuXfPy0wQ== 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=6QqI7aTWKFVEWRVVQhqky8X+U98SqxwhYNRdYwyyQ1M=; b=pdgsLzH4VNFtG6QYs64mrr8DJ3/Mt0MPvcdvZShjqJGXnz1OzF5I4nKVhmct2MMPS3sXBJpHQa7Ev/JQfnOcy5LI4wfSQJjmJd6CdGeAte+bKXfFvpFRJjXmCuGDRcG6yPETpolZGCmR9bccyjKtQ/zQSIQdEEXeHpzDOwRnB0EaAJMkZRosTEXBZnmkAPBhHABSnTM8w3vmLikwz3QlZNPhfXOt2sU8snmjtwe/5H6mLoFdndC7xwudbhnGninhOYfDNbL++9K8UT3jvKtINeWQa5b2jlL2K2yYRY4RwOjltr2aZV8npZiDaPMduBcxKFNMKOLa3Y6SZOUyaPwb/w== Received: from BN7NAM10FT059.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e8f::4b) by BN7NAM10HT067.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e8f::210) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11; Tue, 26 Jan 2021 19:47:27 +0000 Received: from MWHPR06MB3102.namprd06.prod.outlook.com (2a01:111:e400:7e8f::4c) by BN7NAM10FT059.mail.protection.outlook.com (2a01:111:e400:7e8f::447) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11 via Frontend Transport; Tue, 26 Jan 2021 19:47:27 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:C768B9F5D97178B97C1028DE028B46970F5600167912070C737FBDEA0CC8FD2B;UpperCasedChecksum:D9226CF4EF60C5CAC38707015B28FDDC8B638097EB53141942F2575AE8B1E532;SizeAsReceived:7556;Count:47 Received: from MWHPR06MB3102.namprd06.prod.outlook.com ([fe80::d4ee:1260:6f53:3f7b]) by MWHPR06MB3102.namprd06.prod.outlook.com ([fe80::d4ee:1260:6f53:3f7b%6]) with mapi id 15.20.3784.017; Tue, 26 Jan 2021 19:47:27 +0000 From: "Kun Qin" To: devel@edk2.groups.io CC: Ard Biesheuvel , Sami Mujawar , Jiewen Yao , Supreeth Venkatesh , Jiewen Yao Subject: [PATCH v4 05/20] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Date: Tue, 26 Jan 2021 11:46:55 -0800 Message-ID: X-Mailer: git-send-email 2.30.0.windows.1 In-Reply-To: <20210126194710.2248-1-kun.q@outlook.com> References: <20210126194710.2248-1-kun.q@outlook.com> X-TMN: [XbWg7ssMl8c0SF87Hzn97ne8/Eop8/mC] X-ClientProxiedBy: MWHPR11CA0001.namprd11.prod.outlook.com (2603:10b6:301:1::11) To MWHPR06MB3102.namprd06.prod.outlook.com (2603:10b6:301:3e::35) Return-Path: kun.q@outlook.com X-Microsoft-Original-Message-ID: <20210126194710.2248-3-kun.q@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (50.35.88.161) by MWHPR11CA0001.namprd11.prod.outlook.com (2603:10b6:301:1::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.14 via Frontend Transport; Tue, 26 Jan 2021 19:47:26 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 47 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 7f26285a-b32f-49c5-450d-08d8c2333571 X-MS-TrafficTypeDiagnostic: BN7NAM10HT067: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +nAR8xQWnMpgcksnJWwx3fQTXHC7tfDOZjlMK9nMAkcLATNzs4x0FU9ji4PefL5/DdE9GLletcdz4Khk7IIZi5dIta6cS5lkSpSK/Q0QwmeOQYCWBRqMgoScbfGNzp52SOZ438MRqf/Lx46QaKglV9k/xN13lUZIak8JYPEJO1hXpqseXEdoMeAYLRqFw9Oq+jauYCAfm8H/mUrtsqgN1Iy+hXwNHNb4YSOmqMAOl9zM57a+9pVQDJCJNeaT+HMyYGjIPQ0Z7wRB9WuODHWA+XjOlTX8VdY7A3PAARFVHoYxNopqmspJ8ddWuev/zxcncoeOSHzqc6oHq+C+GaAbR0cWTXEk9DIqMTgKnIO+dXnUCS1sCGMDbz0oZHmpN5BgkGD4JbUXtzGrkqnk/4iNIw== X-MS-Exchange-AntiSpam-MessageData: Tsq2AmFJFLMInP3qdtIwckD09T9/Ag1NnyYIGI16T5KFd/+6Z3IdSX7S9Tsf7V5+rS2APjWcW7THd6Qis6hF1y8VZyK+lk4MQ9mz9DSNWbBXvFTT24nu9ZuB3bYryH8Z3RDdWiMUT4NKufRW9UAJuA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7f26285a-b32f-49c5-450d-08d8c2333571 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2021 19:47:27.0980 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: BN7NAM10FT059.eop-nam10.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: BN7NAM10HT067 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 Reviewed-by: Jiewen Yao --- Notes: v4: - Added reviewed-by tag [Jiewen] =20 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