From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com [40.92.23.63]) by mx.groups.io with SMTP id smtpd.web09.1094.1611193465827275149 for ; Wed, 20 Jan 2021 17:44:26 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@outlook.com header.s=selector1 header.b=ZN4KYwoh; spf=pass (domain: outlook.com, ip: 40.92.23.63, mailfrom: kun.q@outlook.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lIe6ukGpvMAZKaXcG89sRL4O+EaLw/sBJbv2+KmY3zXuO2qwXpTrYm9YuIKuDM6FM8/6F1/Rx4XQSb2HKpJnNYnuxHzuR56nEZmFpMHzuV9IUYqcObKvbzogBhdaEUS4qB0vbz0Sv032dbJ+UdejjDTTp3t5x9AVQ9bD93/3CumZFaR/pTFIdj6RUARzJXVw/HUZBIqHfDaUuNvznZeqK0fgHxxZYoyPABNPNske85JNwqE/Y5HWLSjtidwx9CO19KmaJBq+edde1Pw0T8EnCYdoduJDh65mU5psjAN0iWoDuL0J+wXor4bViWPgB2U6fWUnJFDNoereI9xvUEF4Ew== 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=jBEqcnXY4wZC2npv+mTAQ54wtatWGPtAiLgMdayi7Rw=; b=JuX+vUmTaxZ8NPRUvlVfAc26eMO+SHC/HRLFgwN8iwo2TEGD63rI8BpeU1B6venOUlIJnsdCC4f3gDBxyYpx42n2ytB0SeO9PAygLcUQUqQdFkJxV6jpyICVmELT+fj8JUFNKe3PjFzFBS1cnbNWC6GrCBJM+kGXQXBRXOCKubILVPSB0Sa1/1K9pksOKsuAUWa2qXtkwkLANbjiPmIm022h1TdWd+HIdTQEIDhRG81BZbSlO1cg6KlE96B7henLneaigG08887Ib4baHzHH+z8ozb+XJ9mzGBzzSK5gpaMAmBz0CRt0JgxyJEqK/o2CB4kfE4ktNlUs4Lmq4JADuA== 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=jBEqcnXY4wZC2npv+mTAQ54wtatWGPtAiLgMdayi7Rw=; b=ZN4KYwoh3lj8FT0p+2QPkHxXYLr+QZi6ImdEV+kYFn3O06OnDUUUMj9yCQZZ9p1AN9mlZOsSIW/6KPX5tZafvM4SVAPGHkZu48MaIXDIrZn8dViS8nIujYO7cNiTaEK9bhP+xHeK1YuKMRXiYun8cawqPEbhTAYEEtHERrYXtXWGwbCJ9k5E1s/xyl0zqguDGrprKynhuXnwQgl7rUDVhYkJduIw7HH/8Ys7ltilOST3SXe8CRZXzNiGCbR1C0Gp5Prb71Cf+ax/p/y/mh7ih+ScTAW6HLu9AZCu8QistIx+wLyZTqhS4nCQa1HxbS1fJug7R6+G0g4MQercSfgIaw== Received: from DM6NAM12FT040.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc64::41) by DM6NAM12HT025.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc64::333) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.7; Thu, 21 Jan 2021 01:44:24 +0000 Received: from MWHPR06MB3102.namprd06.prod.outlook.com (2a01:111:e400:fc64::4e) by DM6NAM12FT040.mail.protection.outlook.com (2a01:111:e400:fc64::329) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3805.6 via Frontend Transport; Thu, 21 Jan 2021 01:44:24 +0000 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.3763.014; Thu, 21 Jan 2021 01:44:24 +0000 From: "Kun Qin" To: "devel@edk2.groups.io" , Jiewen Yao CC: Ard Biesheuvel , Sami Mujawar , Supreeth Venkatesh Subject: Re: [edk2-devel] [PATCH v3 05/18] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Thread-Topic: [edk2-devel] [PATCH v3 05/18] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Thread-Index: AQHW6sXDOs4B7xa80ECsU/RT/OSneKoxVl5B Date: Thu, 21 Jan 2021 01:44:24 +0000 Message-ID: References: <165A3A34C1B605DD.30138@groups.io> In-Reply-To: <165A3A34C1B605DD.30138@groups.io> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:03C5590BCB84E15F6B8FAB50B8DA84434174EC33798B9C557868C8229EAB3786;UpperCasedChecksum:7F199A4FC2172A422F80E7ED47377D14D60295F41282027DCB65013D67D438EB;SizeAsReceived:7029;Count:44 x-tmn: [Me2bmDmkL0yK/1GEPToh80JwHvqp9yaO] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: b24d882d-3af7-4835-b53f-08d8bdae14f6 x-ms-traffictypediagnostic: DM6NAM12HT025: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: gMyQcuN6qSvAHtDBNJIyO83totJXrnyTkyBXL0NQH9IGuqFwpFvdKC7qaKBdIMyKP9aJDh0MlQYcsE158crpMciqVic62BeAQHUxaAzfEMvyKCPo3kX6pckVjBnyBuOOADfs81JmtDFYuDjrMhTwUw7BUEPwntr7DhDAY27h7IaHoQHdHaC7pXO0dF6NfLUXYN4qdrSZXJhmrZ+Jv0hZ/45cJ+hIT+3sxK68LlF9+G0r8QDDOhyzf+fVUNgt/dqqb99yOG6F6KgKc5KZYKeCLhZzeDcrHNjGOpip4cbFGaM= x-ms-exchange-antispam-messagedata: vjJ+tEHDBcoPpmT1Srho7ylmlj7L32+LKdf+g4x03DBYP4ZS7t5p191cU4SKL9TczcSJGUGzJk6/7R22J2YHHTt/8Hy0ne8BF95btNe8Aa9gT0hwKjFIEaEaLmgMe1Q/FsDtfaO5DGiW60+BTx4aHg== x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: DM6NAM12FT040.eop-nam12.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: b24d882d-3af7-4835-b53f-08d8bdae14f6 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Jan 2021 01:44:24.4126 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6NAM12HT025 Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MWHPR06MB31028FBEA8E921B436FC70A7F3A10MWHPR06MB3102namp_" --_000_MWHPR06MB31028FBEA8E921B436FC70A7F3A10MWHPR06MB3102namp_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Hi Jiewen, Do you have further concerns about this specific patch? I did created a Bu= gzilla ticket to track the OS memory protection concern here: 3168 =96 Add = non-MMRAM memory protection for Standalone MM environment (tianocore.org). It introduces a new = proposal to allow access of DXE/RT regions from MMRAM, but requires non tri= vial change. Please let me know if you hold different opinions towards the = proposal or this patch. Thanks, Kun From: Kun Qin Sent: Thursday, January 14, 2021 14:36 To: devel@edk2.groups.io Cc: Ard Biesheuvel; Sami Mujawar; Jiewen Yao; Supreeth Venka= tesh Subject: [edk2-devel] [PATCH v3 05/18] StandaloneMmPkg: StandaloneMmMemLib= : Extends support for X64 architecture 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. v2: - Added routine to fix bug of not initializing MmRanges [Jiewen] - Extends support to x86 instead of x64 only [Hao] StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInte= rnal.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/Standalone= MmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Sta= ndaloneMmMemLibInternal.c index cb7c5e677a6b..4124959e0435 100644 --- a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLi= bInternal.c +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLi= bInternal.c @@ -40,4 +40,31 @@ MmMemLibInternalCalculateMaximumSupportAddress ( 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 inform= ation. + @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. +} 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 ); +/** + Initialize cached Mmram Ranges from HOB. + + @retval EFI_UNSUPPORTED The routine is unable to extract MMRAM inform= ation. + @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 a= nd not overlap with MMRAM. @@ -253,11 +274,42 @@ MemLibConstructor ( IN EFI_MM_SYSTEM_TABLE *MmSystemTable ) { + EFI_STATUS Status; // // Calculate and save maximum support address // MmMemLibInternalCalculateMaximumSupportAddress (); + // + // 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/X86StandaloneMmMem= LibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmM= emLibInternal.c new file mode 100644 index 000000000000..1a978541716a --- /dev/null +++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInte= rnal.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 pl= atform should produce + all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmwar= e (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 = addresses. + // + 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 inform= ation. + @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->NumberOfMmReserve= dRegions; + 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->Add= ress; + if (MmCorePrivateData =3D=3D NULL || MmCorePrivateData->MmramRanges = =3D=3D 0) { + return EFI_UNSUPPORTED; + } + + mMmMemLibInternalMmramCount =3D (UINTN) MmCorePrivateData->MmramRange= Count; + MmramDescriptors =3D (EFI_MMRAM_DESCRIPTOR *) (UINTN) MmCorePrivateDa= ta->MmramRanges; + } + + mMmMemLibInternalMmramRanges =3D AllocatePool (mMmMemLibInternalMmramCo= unt * 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_STANDAL= ONE CONSTRUCTOR =3D MemLibConstructor + DESTRUCTOR =3D MemLibDestructor # # The following information is for reference only and not required by the= build tools. # -# VALID_ARCHITECTURES =3D AARCH64 +# VALID_ARCHITECTURES =3D IA32 X64 AARCH64 # [Sources.Common] StandaloneMmMemLib.c +[Sources.IA32, Sources.X64] + X86StandaloneMmMemLibInternal.c + [Sources.AARCH64] AArch64/StandaloneMmMemLibInternal.c @@ -42,3 +47,9 @@ [Packages] [LibraryClasses] BaseMemoryLib DebugLib + HobLib + MemoryAllocationLib + +[Guids] + gMmCoreDataHobGuid ## SOMETIMES_CONSUMES ## HOB + gEfiMmPeiMmramMemoryReserveGuid ## SOMETIMES_CONSUMES ## HOB -- 2.30.0.windows.1 --_000_MWHPR06MB31028FBEA8E921B436FC70A7F3A10MWHPR06MB3102namp_ Content-Type: text/html; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable

Hi Jiewen,

 

Do you have further concerns about this specific pa= tch? I did created a Bugzilla ticket to track the OS memory protection conc= ern here: 3168 =96= Add non-MMRAM memory protection for Standalone MM environment (tianocore.o= rg). It introduces a new proposal to allow access of DXE/RT regions fro= m MMRAM, but requires non trivial change. Please let me know if you hold different opinions towards the proposal or= this patch.

 

Thanks,

Kun

 

From: Kun Qin
Sent: Thursday, January 14, 2021 14:36
To: devel@edk2.groups.io
Cc:
Ard Biesheuvel; <= a href=3D"mailto:sami.mujawar@arm.com"> Sami Mujawar; Jiewen Yao; = Supreeth Venkatesh
Subject: [edk2-devel] [PATCH v3 05/18] StandaloneMmPkg: StandaloneM= mMemLib: Extends support for X64 architecture

 

This change extends = StandaloneMmMemLib library to support X64
architecture. The implementation is ported from MdePkg/Library/SmmMemLib.<= br>
Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v3:
    - Updated destructor description of varibales to pass C= I build.
   
    v2:
    - Added routine to fix bug of not initializing MmRanges= [Jiewen]
    - Extends support to x86 instead of x64 only [Hao]

 StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLi= bInternal.c |  27 ++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c = ;            &n= bsp;   |  52 +++++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInte= rnal.c      | 155 ++++++++++++++++++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf&nb= sp;            =   |  13 +-
 4 files changed, 246 insertions(+), 1 deletion(-)

diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone= MmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Sta= ndaloneMmMemLibInternal.c
index cb7c5e677a6b..4124959e0435 100644
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLi= bInternal.c
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLi= bInternal.c
@@ -40,4 +40,31 @@ MmMemLibInternalCalculateMaximumSupportAddress (
   DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddr= ess =3D 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
 }
 
+/**
+  Initialize cached Mmram Ranges from HOB.
+
+  @retval EFI_UNSUPPORTED   The routine is unable to extra= ct MMRAM information.
+  @retval EFI_SUCCESS       MmRanges a= re populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  )
+{
+  // Not implemented for AARCH64.
+  return EFI_SUCCESS;
+}
+
+/**
+  Deinitialize cached Mmram Ranges.
+
+**/
+VOID
+MmMemLibInternalFreeMmramRanges (
+  VOID
+  )
+{
+  // Not implemented for AARCH64.
+}
 
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
   );
 
+/**
+  Initialize cached Mmram Ranges from HOB.
+
+  @retval EFI_UNSUPPORTED   The routine is unable to extra= ct MMRAM information.
+  @retval EFI_SUCCESS       MmRanges a= re populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  );
+
+/**
+  Deinitialize cached Mmram Ranges.
+
+**/
+VOID
+MmMemLibInternalFreeMmramRanges (
+  VOID
+  );
+
 /**
   This function check if the buffer is valid per processor arch= itecture and not overlap with MMRAM.
 
@@ -253,11 +274,42 @@ MemLibConstructor (
   IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
   )
 {
+  EFI_STATUS Status;
 
   //
   // Calculate and save maximum support address
   //
   MmMemLibInternalCalculateMaximumSupportAddress ();
 
+  //
+  // Initialize cached Mmram Ranges from HOB.
+  //
+  Status =3D MmMemLibInternalPopulateMmramRanges ();
+
+  return Status;
+}
+
+/**
+  Destructor for Mm Mem library.
+
+  @param ImageHandle    The image handle of the proce= ss.
+  @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/X86StandaloneMmMem= LibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmM= emLibInternal.c
new file mode 100644
index 000000000000..1a978541716a
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInte= rnal.c
@@ -0,0 +1,155 @@
+/** @file
+  Internal ARCH Specific file of MM memory check library.
+
+  MM memory check library implementation. This library consumes MM_A= CCESS_PROTOCOL
+  to get MMRAM information. In order to use this library instance, t= he platform should produce
+  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for fi= rmware (like MM Core
+  and MM driver) and/or specific dedicated hardware.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR&= gt;
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR= >
+  Copyright (c) Microsoft Corporation.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+#include <PiMm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+
+#include <Guid/MmCoreData.h>
+#include <Guid/MmramMemoryReserve.h>
+
+//
+// Maximum support address used to check input buffer
+//
+extern EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress;=
+extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
+extern UINTN          &= nbsp;      mMmMemLibInternalMmramCount;
+
+/**
+  Calculate and save the maximum support address.
+
+**/
+VOID
+MmMemLibInternalCalculateMaximumSupportAddress (
+  VOID
+  )
+{
+  VOID         *Hob;
+  UINT32       RegEax;
+  UINT8        PhysicalAddressBit= s;
+
+  //
+  // Get physical address bits supported.
+  //
+  Hob =3D GetFirstHob (EFI_HOB_TYPE_CPU);
+  if (Hob !=3D NULL) {
+    PhysicalAddressBits =3D ((EFI_HOB_CPU *) Hob)->Size= OfMemorySpace;
+  } else {
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);<= br> +    if (RegEax >=3D 0x80000008) {
+      AsmCpuid (0x80000008, &RegEax, NULL, N= ULL, NULL);
+      PhysicalAddressBits =3D (UINT8) RegEax; +    } else {
+      PhysicalAddressBits =3D 36;
+    }
+  }
+  //
+  // IA-32e paging translates 48-bit linear addresses to 52-bit phys= ical addresses.
+  //
+  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 extra= ct MMRAM information.
+  @retval EFI_SUCCESS       MmRanges a= re populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  )
+{
+  VOID          &n= bsp;            = ;     *HobStart;
+  EFI_HOB_GUID_TYPE        &= nbsp;      *GuidHob;
+  MM_CORE_DATA_HOB_DATA       &nb= sp;   *DataInHob;
+  MM_CORE_PRIVATE_DATA       &nbs= p;    *MmCorePrivateData;
+  EFI_HOB_GUID_TYPE        &= nbsp;      *MmramRangesHob;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
+  EFI_MMRAM_DESCRIPTOR       &nbs= p;    *MmramDescriptors;
+
+  HobStart =3D GetHobList ();
+  DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStar= t));
+
+  //
+  // 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 (&gEfiMmPeiMmra= mMemoryReserveGuid);
+    if (MmramRangesHob =3D=3D NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangesHobData =3D GET_GUID_HOB_DATA (MmramRangesH= ob);
+    if (MmramRangesHobData =3D=3D NULL || MmramRangesHobDa= ta->Descriptor =3D=3D NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    mMmMemLibInternalMmramCount =3D MmramRangesHobData->= ;NumberOfMmReservedRegions;
+    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->Address;
+    if (MmCorePrivateData =3D=3D NULL || MmCorePrivateData= ->MmramRanges =3D=3D 0) {
+      return EFI_UNSUPPORTED;
+    }
+
+    mMmMemLibInternalMmramCount =3D (UINTN) MmCorePrivateD= ata->MmramRangeCount;
+    MmramDescriptors =3D (EFI_MMRAM_DESCRIPTOR *) (UINTN) = MmCorePrivateData->MmramRanges;
+  }
+
+  mMmMemLibInternalMmramRanges =3D AllocatePool (mMmMemLibInternalMm= ramCount * 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.&= lt;BR>
 #  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.= <BR>
+#  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_STANDA= LONE MM_CORE_STANDALONE
   CONSTRUCTOR        &n= bsp;           =3D MemLib= Constructor
+  DESTRUCTOR         &n= bsp;           =3D MemLib= Destructor
 
 #
 # The following information is for reference only and not required b= y the build tools.
 #
-#  VALID_ARCHITECTURES       &nbs= p;   =3D AARCH64
+#  VALID_ARCHITECTURES       &nbs= p;   =3D IA32 X64 AARCH64
 #
 
 [Sources.Common]
   StandaloneMmMemLib.c
 
+[Sources.IA32, Sources.X64]
+  X86StandaloneMmMemLibInternal.c
+
 [Sources.AARCH64]
   AArch64/StandaloneMmMemLibInternal.c
 
@@ -42,3 +47,9 @@ [Packages]
 [LibraryClasses]
   BaseMemoryLib
   DebugLib
+  HobLib
+  MemoryAllocationLib
+
+[Guids]
+  gMmCoreDataHobGuid        =           ## SOMETIMES_CONSUME= S ## HOB
+  gEfiMmPeiMmramMemoryReserveGuid     ## SOMETIM= ES_CONSUMES ## HOB
--
2.30.0.windows.1





 

--_000_MWHPR06MB31028FBEA8E921B436FC70A7F3A10MWHPR06MB3102namp_--