From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064395955671.0542567343527; Thu, 19 Jul 2018 22:26:35 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 6E0922098EAB1; Thu, 19 Jul 2018 22:26:34 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 C61BA21BADAB3 for ; Thu, 19 Jul 2018 22:26:32 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:32 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:29 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331890" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:21 +0800 Message-Id: <20180720052626.24932-2-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 1/6] MdePkg/SmmMemLib: Check for untested memory in GCD X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao It treats GCD untested memory as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- MdePkg/Library/SmmMemLib/SmmMemLib.c | 96 +++++++++++++++++++- MdePkg/Library/SmmMemLib/SmmMemLib.inf | 1 + 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLi= b/SmmMemLib.c index 8c78a0b426..3f79e46d46 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.c +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.c @@ -25,12 +25,20 @@ #include #include #include +#include #include #include #include #include #include =20 +// +// attributes for reserved memory before it is promoted to system memory +// +#define EFI_MEMORY_PRESENT 0x0100000000000000ULL +#define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL +#define EFI_MEMORY_TESTED 0x0400000000000000ULL + #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) =20 @@ -46,10 +54,13 @@ UINTN mMemoryMapEntryCount; EFI_MEMORY_DESCRIPTOR *mMemoryMap; UINTN mDescriptorSize; =20 +EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmMemLibGcdMemSpace =3D NULL; +UINTN mSmmMemLibGcdMemNumberOfDesc =3D 0; + VOID *mRegistrationEndOfDxe; VOID *mRegistrationReadyToLock; =20 -BOOLEAN mSmmReadyToLock =3D FALSE; +BOOLEAN mSmmMemLibSmmReadyToLock =3D FALSE; =20 /** Calculate and save the maximum support address. @@ -154,7 +165,7 @@ SmmIsBufferOutsideSmmValid ( // // Check override for Valid Communication Region // - if (mSmmReadyToLock) { + if (mSmmMemLibSmmReadyToLock) { EFI_MEMORY_DESCRIPTOR *MemoryMap; BOOLEAN InValidCommunicationRegion; =20 @@ -171,12 +182,28 @@ SmmIsBufferOutsideSmmValid ( if (!InValidCommunicationRegion) { DEBUG (( EFI_D_ERROR, - "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buff= er (0x%lx) - Length (0x%lx), ", + "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buff= er (0x%lx) - Length (0x%lx)\n", Buffer, Length )); return FALSE; } + + // + // Check untested memory as invalid communication buffer. + // + for (Index =3D 0; Index < mSmmMemLibGcdMemNumberOfDesc; Index++) { + if (((Buffer >=3D mSmmMemLibGcdMemSpace[Index].BaseAddress) && (Buff= er < mSmmMemLibGcdMemSpace[Index].BaseAddress + mSmmMemLibGcdMemSpace[Index= ].Length)) || + ((mSmmMemLibGcdMemSpace[Index].BaseAddress >=3D Buffer) && (mSmm= MemLibGcdMemSpace[Index].BaseAddress < Buffer + Length))) { + DEBUG (( + EFI_D_ERROR, + "SmmIsBufferOutsideSmmValid: In Untested Memory Region: Buffer (= 0x%lx) - Length (0x%lx)\n", + Buffer, + Length + )); + return FALSE; + } + } } return TRUE; } @@ -317,6 +344,61 @@ SmmSetMem ( return EFI_SUCCESS; } =20 +/** + Get GCD memory map. + Only record untested memory as invalid communication buffer. +**/ +VOID +SmmMemLibInternalGetGcdMemoryMap ( + VOID + ) +{ + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap; + EFI_STATUS Status; + UINTN Index; + + Status =3D gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap); + if (EFI_ERROR (Status)) { + return ; + } + + mSmmMemLibGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + mSmmMemLibGcdMemNumberOfDesc++; + } + } + + mSmmMemLibGcdMemSpace =3D AllocateZeroPool (mSmmMemLibGcdMemNumberOfDesc= * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR)); + ASSERT (mSmmMemLibGcdMemSpace !=3D NULL); + if (mSmmMemLibGcdMemSpace =3D=3D NULL) { + mSmmMemLibGcdMemNumberOfDesc =3D 0; + gBS->FreePool (MemSpaceMap); + return ; + } + + mSmmMemLibGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + CopyMem ( + &mSmmMemLibGcdMemSpace[mSmmMemLibGcdMemNumberOfDesc], + &MemSpaceMap[Index], + sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR) + ); + mSmmMemLibGcdMemNumberOfDesc++; + } + } + + gBS->FreePool (MemSpaceMap); +} + /** Notification for SMM EndOfDxe protocol. =20 @@ -415,10 +497,14 @@ SmmLibInternalEndOfDxeNotify ( =20 gBS->FreePool (MemoryMap); =20 + // + // Get additional information from GCD memory map. + // + SmmMemLibInternalGetGcdMemoryMap (); + return EFI_SUCCESS; } =20 - /** Notification for SMM ReadyToLock protocol. =20 @@ -436,7 +522,7 @@ SmmLibInternalReadyToLockNotify ( IN EFI_HANDLE Handle ) { - mSmmReadyToLock =3D TRUE; + mSmmMemLibSmmReadyToLock =3D TRUE; return EFI_SUCCESS; } /** diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.inf b/MdePkg/Library/SmmMem= Lib/SmmMemLib.inf index e4edad3af2..36576a4f2f 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf @@ -43,6 +43,7 @@ [LibraryClasses] SmmServicesTableLib UefiBootServicesTableLib + DxeServicesTableLib DebugLib BaseMemoryLib HobLib --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064398862270.3466048953411; Thu, 19 Jul 2018 22:26:38 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 97C552098EAB6; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 0A5B421BADAB3 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:35 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:31 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331895" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:22 +0800 Message-Id: <20180720052626.24932-3-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 2/6] UefiCpuPkg/PiSmmCpu: Check for untested memory in GCD X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao It treats GCD untested memory as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 144 ++++++++++++++++-= --- 1 file changed, 120 insertions(+), 24 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 85e06b2e6a..b2ace6334e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -13,6 +13,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHE= R EXPRESS OR IMPLIED. =20 #include "PiSmmCpuDxeSmm.h" =20 +// +// attributes for reserved memory before it is promoted to system memory +// +#define EFI_MEMORY_PRESENT 0x0100000000000000ULL +#define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL +#define EFI_MEMORY_TESTED 0x0400000000000000ULL + #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) =20 @@ -23,6 +30,9 @@ EFI_MEMORY_DESCRIPTOR *mUefiMemoryMap; UINTN mUefiMemoryMapSize; UINTN mUefiDescriptorSize; =20 +EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace =3D NULL; +UINTN mGcdMemNumberOfDesc =3D 0; + PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64}, {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64}, @@ -1022,6 +1032,60 @@ MergeMemoryMapForNotPresentEntry ( return ; } =20 +/** + This function caches the GCD memory map information. +**/ +VOID +GetGcdMemoryMap ( + VOID + ) +{ + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap; + EFI_STATUS Status; + UINTN Index; + + Status =3D gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap); + if (EFI_ERROR (Status)) { + return ; + } + + mGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + mGcdMemNumberOfDesc++; + } + } + + mGcdMemSpace =3D AllocateZeroPool (mGcdMemNumberOfDesc * sizeof (EFI_GCD= _MEMORY_SPACE_DESCRIPTOR)); + ASSERT (mGcdMemSpace !=3D NULL); + if (mGcdMemSpace =3D=3D NULL) { + mGcdMemNumberOfDesc =3D 0; + gBS->FreePool (MemSpaceMap); + return ; + } + + mGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + CopyMem ( + &mGcdMemSpace[mGcdMemNumberOfDesc], + &MemSpaceMap[Index], + sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR) + ); + mGcdMemNumberOfDesc++; + } + } + + gBS->FreePool (MemSpaceMap); +} + /** This function caches the UEFI memory map information. **/ @@ -1081,6 +1145,11 @@ GetUefiMemoryMap ( ASSERT (mUefiMemoryMap !=3D NULL); =20 gBS->FreePool (MemoryMap); + + // + // Get additional information from GCD memory map. + // + GetGcdMemoryMap (); } =20 /** @@ -1102,33 +1171,52 @@ SetUefiMemMapAttributes ( =20 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); =20 - if (mUefiMemoryMap =3D=3D NULL) { - DEBUG ((DEBUG_INFO, "UefiMemoryMap - NULL\n")); - return ; + if (mUefiMemoryMap !=3D NULL) { + MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; + MemoryMap =3D mUefiMemoryMap; + for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { + if (IsUefiPageNotPresent(MemoryMap)) { + Status =3D SmmSetMemoryAttributes ( + MemoryMap->PhysicalStart, + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), + EFI_MEMORY_RP + ); + DEBUG (( + DEBUG_INFO, + "UefiMemory protection: 0x%lx - 0x%lx %r\n", + MemoryMap->PhysicalStart, + MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Memo= ryMap->NumberOfPages), + Status + )); + } + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); + } } + // + // Do not free mUefiMemoryMap, it will be checked in IsSmmCommBufferForb= iddenAddress(). + // =20 - MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; - MemoryMap =3D mUefiMemoryMap; - for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { - if (IsUefiPageNotPresent(MemoryMap)) { + // + // Set untested memory as not present. + // + if (mGcdMemSpace !=3D NULL) { + for (Index =3D 0; Index < mGcdMemNumberOfDesc; Index++) { Status =3D SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), + mGcdMemSpace[Index].BaseAddress, + mGcdMemSpace[Index].Length, EFI_MEMORY_RP ); DEBUG (( DEBUG_INFO, - "UefiMemory protection: 0x%lx - 0x%lx %r\n", - MemoryMap->PhysicalStart, - MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Memory= Map->NumberOfPages), + "GcdMemory protection: 0x%lx - 0x%lx %r\n", + mGcdMemSpace[Index].BaseAddress, + mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index].Length, Status )); } - MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); } - // - // Do free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbidde= nAddress(). + // Do not free mGcdMemSpace, it will be checked in IsSmmCommBufferForbid= denAddress(). // } =20 @@ -1149,21 +1237,29 @@ IsSmmCommBufferForbiddenAddress ( UINTN MemoryMapEntryCount; UINTN Index; =20 - if (mUefiMemoryMap =3D=3D NULL) { - return FALSE; + if (mUefiMemoryMap !=3D NULL) { + MemoryMap =3D mUefiMemoryMap; + MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; + for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { + if (IsUefiPageNotPresent (MemoryMap)) { + if ((Address >=3D MemoryMap->PhysicalStart) && + (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN= )MemoryMap->NumberOfPages)) ) { + return TRUE; + } + } + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); + } } =20 - MemoryMap =3D mUefiMemoryMap; - MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; - for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { - if (IsUefiPageNotPresent (MemoryMap)) { - if ((Address >=3D MemoryMap->PhysicalStart) && - (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)M= emoryMap->NumberOfPages)) ) { + if (mGcdMemSpace !=3D NULL) { + for (Index =3D 0; Index < mGcdMemNumberOfDesc; Index++) { + if ((Address >=3D mGcdMemSpace[Index].BaseAddress) && + (Address < mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index]= .Length) ) { return TRUE; } } - MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); } + return FALSE; } =20 --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064401767238.9083563427007; Thu, 19 Jul 2018 22:26:41 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id C193F2098EABA; Thu, 19 Jul 2018 22:26:37 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 32F7121BADAB6 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:35 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:32 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331901" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:23 +0800 Message-Id: <20180720052626.24932-4-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 3/6] MdeModulePkg/DxeCore: Install UEFI mem attrib table at EndOfDxe. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao So that the SMM can consume it to set page protection for the UEFI runtime page with EFI_MEMORY_RO attribute. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c | 36 ++++++++++++++++++= +- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 43e5be8b54..60557faa1c 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -240,6 +240,23 @@ InstallMemoryAttributesTableOnReadyToBoot ( mMemoryAttributesTableReadyToBoot =3D TRUE; } =20 +/** + Install initial MemoryAttributesTable on EndOfDxe. + Then SMM can consume this information. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registered to the Even= t. +**/ +VOID +EFIAPI +InstallMemoryAttributesTableOnEndOfDxe ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + InstallMemoryAttributesTable (); +} + /** Initialize MemoryAttrubutesTable support. **/ @@ -251,18 +268,35 @@ CoreInitializeMemoryAttributesTable ( { EFI_STATUS Status; EFI_EVENT ReadyToBootEvent; + EFI_EVENT EndOfDxeEvent; =20 // // Construct the table at ReadyToBoot. // Status =3D CoreCreateEventInternal ( EVT_NOTIFY_SIGNAL, - TPL_CALLBACK - 1, + TPL_CALLBACK, InstallMemoryAttributesTableOnReadyToBoot, NULL, &gEfiEventReadyToBootGuid, &ReadyToBootEvent ); ASSERT_EFI_ERROR (Status); + + // + // Construct the initial table at EndOfDxe, + // then SMM can consume this information. + // Use TPL_NOTIFY here, as such SMM code (TPL_CALLBACK) + // can run after it. + // + Status =3D CoreCreateEventInternal ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + InstallMemoryAttributesTableOnEndOfDxe, + NULL, + &gEfiEndOfDxeEventGroupGuid, + &EndOfDxeEvent + ); + ASSERT_EFI_ERROR (Status); return ; } --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064407859417.43287733689317; Thu, 19 Jul 2018 22:26:47 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 231452098EAC0; Thu, 19 Jul 2018 22:26:38 -0700 (PDT) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (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 AC2F32098EAB3 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:36 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:33 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.88; helo=mga01.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331904" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:24 +0800 Message-Id: <20180720052626.24932-5-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 4/6] MdePkg/SmmMemLib: Check EFI_MEMORY_RO in UEFI mem attrib table. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao It treats the UEFI runtime page with EFI_MEMORY_RO attribute as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- MdePkg/Library/SmmMemLib/SmmMemLib.c | 58 +++++++++++++++++++- MdePkg/Library/SmmMemLib/SmmMemLib.inf | 4 ++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLi= b/SmmMemLib.c index 3f79e46d46..3409ddf87c 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.c +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.c @@ -27,10 +27,12 @@ #include #include #include +#include #include #include #include #include +#include =20 // // attributes for reserved memory before it is promoted to system memory @@ -39,9 +41,6 @@ #define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL #define EFI_MEMORY_TESTED 0x0400000000000000ULL =20 -#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ - ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) - EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges; UINTN mSmmMemLibInternalSmramCount; =20 @@ -57,6 +56,8 @@ UINTN mDescriptorSize; EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmMemLibGcdMemSpace =3D NULL; UINTN mSmmMemLibGcdMemNumberOfDesc =3D 0; =20 +EFI_MEMORY_ATTRIBUTES_TABLE *mSmmMemLibMemoryAttributesTable =3D NULL; + VOID *mRegistrationEndOfDxe; VOID *mRegistrationReadyToLock; =20 @@ -204,6 +205,32 @@ SmmIsBufferOutsideSmmValid ( return FALSE; } } + + // + // Check UEFI runtime memory with EFI_MEMORY_RO as invalid communicati= on buffer. + // + if (mSmmMemLibMemoryAttributesTable !=3D NULL) { + EFI_MEMORY_DESCRIPTOR *Entry; + + Entry =3D (EFI_MEMORY_DESCRIPTOR *)(mSmmMemLibMemoryAttributesTable = + 1); + for (Index =3D 0; Index < mSmmMemLibMemoryAttributesTable->NumberOfE= ntries; Index++) { + if (Entry->Type =3D=3D EfiRuntimeServicesCode || Entry->Type =3D= =3D EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) !=3D 0) { + if (((Buffer >=3D Entry->PhysicalStart) && (Buffer < Entry->Ph= ysicalStart + LShiftU64 (Entry->NumberOfPages, EFI_PAGE_SHIFT))) || + ((Entry->PhysicalStart >=3D Buffer) && (Entry->PhysicalSta= rt < Buffer + Length))) { + DEBUG (( + EFI_D_ERROR, + "SmmIsBufferOutsideSmmValid: In RuntimeCode Region: Buffer= (0x%lx) - Length (0x%lx)\n", + Buffer, + Length + )); + return FALSE; + } + } + } + Entry =3D NEXT_MEMORY_DESCRIPTOR (Entry, mSmmMemLibMemoryAttribute= sTable->DescriptorSize); + } + } } return TRUE; } @@ -399,6 +426,26 @@ SmmMemLibInternalGetGcdMemoryMap ( gBS->FreePool (MemSpaceMap); } =20 +/** + Get UEFI MemoryAttributesTable. +**/ +VOID +SmmMemLibInternalGetUefiMemoryAttributesTable ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN MemoryAttributesTableSize; + + Status =3D EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGui= d, (VOID **)&MemoryAttributesTable); + if (!EFI_ERROR (Status)) { + MemoryAttributesTableSize =3D sizeof(EFI_MEMORY_ATTRIBUTES_TABLE) + Me= moryAttributesTable->DescriptorSize * MemoryAttributesTable->NumberOfEntrie= s; + mSmmMemLibMemoryAttributesTable =3D AllocateCopyPool (MemoryAttributes= TableSize, MemoryAttributesTable); + ASSERT (mSmmMemLibMemoryAttributesTable !=3D NULL); + } +} + /** Notification for SMM EndOfDxe protocol. =20 @@ -502,6 +549,11 @@ SmmLibInternalEndOfDxeNotify ( // SmmMemLibInternalGetGcdMemoryMap (); =20 + // + // Get UEFI memory attributes table. + // + SmmMemLibInternalGetUefiMemoryAttributesTable (); + return EFI_SUCCESS; } =20 diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.inf b/MdePkg/Library/SmmMem= Lib/SmmMemLib.inf index 36576a4f2f..525449c25c 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf @@ -48,11 +48,15 @@ BaseMemoryLib HobLib MemoryAllocationLib + UefiLib =20 [Protocols] gEfiSmmAccess2ProtocolGuid ## CONSUMES gEfiSmmReadyToLockProtocolGuid ## CONSUMES gEfiSmmEndOfDxeProtocolGuid ## CONSUMES =20 +[Guids] + gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable + [Depex] gEfiSmmAccess2ProtocolGuid --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064404714942.4217952939655; Thu, 19 Jul 2018 22:26:44 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id ECBC32098EABE; Thu, 19 Jul 2018 22:26:37 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 610EE2098EAB3 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:36 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:35 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331912" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:25 +0800 Message-Id: <20180720052626.24932-6-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 5/6] UefiCpuPkg/PiSmmCpu: Check EFI_RUNTIME_RO in UEFI mem attrib table. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao It treats the UEFI runtime page with EFI_MEMORY_RO attribute as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 2 + UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 1 + UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 75 ++++++++++++++++++= +- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 1d016594e0..e3c7cff81c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -28,6 +28,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #include =20 #include +#include #include =20 #include @@ -45,6 +46,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #include #include #include +#include #include #include #include diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf b/UefiCpuPkg/PiSm= mCpuDxeSmm/PiSmmCpuDxeSmm.inf index 52d8c55075..a7fb7b0b14 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf @@ -117,6 +117,7 @@ gEfiAcpi20TableGuid ## SOMETIMES_CONSUMES ## System= Table gEfiAcpi10TableGuid ## SOMETIMES_CONSUMES ## System= Table gEdkiiPiSmmMemoryAttributesTableGuid ## CONSUMES ## SystemTable + gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable =20 [FeaturePcd] gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmDebug ## CONS= UMES diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index b2ace6334e..7d99f23426 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -20,9 +20,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL #define EFI_MEMORY_TESTED 0x0400000000000000ULL =20 -#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ - ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) - #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) =20 @@ -33,6 +30,8 @@ UINTN mUefiDescriptorSize; EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace =3D NULL; UINTN mGcdMemNumberOfDesc =3D 0; =20 +EFI_MEMORY_ATTRIBUTES_TABLE *mUefiMemoryAttributesTable =3D NULL; + PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64}, {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64}, @@ -1086,6 +1085,26 @@ GetGcdMemoryMap ( gBS->FreePool (MemSpaceMap); } =20 +/** + Get UEFI MemoryAttributesTable. +**/ +VOID +GetUefiMemoryAttributesTable ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN MemoryAttributesTableSize; + + Status =3D EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGui= d, (VOID **)&MemoryAttributesTable); + if (!EFI_ERROR (Status)) { + MemoryAttributesTableSize =3D sizeof(EFI_MEMORY_ATTRIBUTES_TABLE) + Me= moryAttributesTable->DescriptorSize * MemoryAttributesTable->NumberOfEntrie= s; + mUefiMemoryAttributesTable =3D AllocateCopyPool (MemoryAttributesTable= Size, MemoryAttributesTable); + ASSERT (mUefiMemoryAttributesTable !=3D NULL); + } +} + /** This function caches the UEFI memory map information. **/ @@ -1150,6 +1169,11 @@ GetUefiMemoryMap ( // Get additional information from GCD memory map. // GetGcdMemoryMap (); + + // + // Get UEFI memory attributes table. + // + GetUefiMemoryAttributesTable (); } =20 /** @@ -1168,6 +1192,7 @@ SetUefiMemMapAttributes ( EFI_MEMORY_DESCRIPTOR *MemoryMap; UINTN MemoryMapEntryCount; UINTN Index; + EFI_MEMORY_DESCRIPTOR *Entry; =20 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); =20 @@ -1218,6 +1243,35 @@ SetUefiMemMapAttributes ( // // Do not free mGcdMemSpace, it will be checked in IsSmmCommBufferForbid= denAddress(). // + + // + // Set UEFI runtime memory with EFI_MEMORY_RO as not present. + // + if (mUefiMemoryAttributesTable !=3D NULL) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)(mUefiMemoryAttributesTable + 1); + for (Index =3D 0; Index < mUefiMemoryAttributesTable->NumberOfEntries;= Index++) { + if (Entry->Type =3D=3D EfiRuntimeServicesCode || Entry->Type =3D=3D = EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) !=3D 0) { + Status =3D SmmSetMemoryAttributes ( + Entry->PhysicalStart, + EFI_PAGES_TO_SIZE((UINTN)Entry->NumberOfPages), + EFI_MEMORY_RP + ); + DEBUG (( + DEBUG_INFO, + "UefiMemoryAttribute protection: 0x%lx - 0x%lx %r\n", + Entry->PhysicalStart, + Entry->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Entry-= >NumberOfPages), + Status + )); + } + } + Entry =3D NEXT_MEMORY_DESCRIPTOR (Entry, mUefiMemoryAttributesTable-= >DescriptorSize); + } + } + // + // Do not free mUefiMemoryAttributesTable, it will be checked in IsSmmCo= mmBufferForbiddenAddress(). + // } =20 /** @@ -1236,6 +1290,7 @@ IsSmmCommBufferForbiddenAddress ( EFI_MEMORY_DESCRIPTOR *MemoryMap; UINTN MemoryMapEntryCount; UINTN Index; + EFI_MEMORY_DESCRIPTOR *Entry; =20 if (mUefiMemoryMap !=3D NULL) { MemoryMap =3D mUefiMemoryMap; @@ -1260,6 +1315,20 @@ IsSmmCommBufferForbiddenAddress ( } } =20 + if (mUefiMemoryAttributesTable !=3D NULL) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)(mUefiMemoryAttributesTable + 1); + for (Index =3D 0; Index < mUefiMemoryAttributesTable->NumberOfEntries;= Index++) { + if (Entry->Type =3D=3D EfiRuntimeServicesCode || Entry->Type =3D=3D = EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) !=3D 0) { + if ((Address >=3D Entry->PhysicalStart) && + (Address < Entry->PhysicalStart + LShiftU64 (Entry->NumberOf= Pages, EFI_PAGE_SHIFT))) { + return TRUE; + } + Entry =3D NEXT_MEMORY_DESCRIPTOR (Entry, mUefiMemoryAttributesTa= ble->DescriptorSize); + } + } + } + } return FALSE; } =20 --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri May 3 09:52:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.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; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064410958366.3135345956954; Thu, 19 Jul 2018 22:26:50 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5142B2098EAB9; Thu, 19 Jul 2018 22:26:44 -0700 (PDT) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 355E82098EAA8 for ; Thu, 19 Jul 2018 22:26:42 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:41 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:36 -0700 X-Original-To: edk2-devel@lists.01.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; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331916" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:26 +0800 Message-Id: <20180720052626.24932-7-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 6/6] MdeModulePkg/DxeCore: Not update RtCode in MemAttrTable after EndOfDxe X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , 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" From: Jiewen Yao We want to provide precise info in MemAttribTable to both OS and SMM, and SMM only gets the info at EndOfDxe. So we do not update RtCode entry in EndOfDxe. The impact is that if 3rd part OPROM is runtime, it cannot be executed at UEFI runtime phase. Currently, we do not see compatibility issue, because the only runtime OPROM we found before in UNDI, and UEFI OS will not use UNDI interface in OS. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao Reviewed-by: Star Zeng to this patch series. --- MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c b/MdeModulePkg/Co= re/Dxe/Misc/PropertiesTable.c index a84507df95..a96d442fbc 100644 --- a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c @@ -62,6 +62,8 @@ EFI_LOCK mPropertiesTableLock =3D EFI_INITIALIZ= E_LOCK_VARIABLE (TPL_NOTI =20 BOOLEAN mPropertiesTableEnable; =20 +BOOLEAN mPropertiesTableEndOfDxe =3D FALSE; + // // Below functions are for MemoryMap // @@ -1079,6 +1081,11 @@ InsertImageRecord ( DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage)); DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); =20 + if (mPropertiesTableEndOfDxe) { + DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe= \n")); + return ; + } + ImageRecord =3D AllocatePool (sizeof(*ImageRecord)); if (ImageRecord =3D=3D NULL) { return ; @@ -1296,6 +1303,11 @@ RemoveImageRecord ( DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage)); DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); =20 + if (mPropertiesTableEndOfDxe) { + DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe= \n")); + return ; + } + ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize); if (ImageRecord =3D=3D NULL) { DEBUG ((EFI_D_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); @@ -1333,6 +1345,7 @@ InstallPropertiesTable ( VOID *Context ) { + mPropertiesTableEndOfDxe =3D TRUE; if (PcdGetBool (PcdPropertiesTableEnable)) { EFI_STATUS Status; =20 --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel