From nobody Thu May 2 20:35:14 2024 Delivered-To: importer@patchew.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; 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 Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1505801423729418.76266426863833; Mon, 18 Sep 2017 23:10:23 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 623CC21E945F3; Mon, 18 Sep 2017 23:07:18 -0700 (PDT) Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (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 EBBB121D046BF for ; Mon, 18 Sep 2017 23:07:17 -0700 (PDT) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Sep 2017 23:10:22 -0700 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.46]) by orsmga005.jf.intel.com with ESMTP; 18 Sep 2017 23:10:20 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,416,1500966000"; d="scan'208,223";a="150779163" From: Jian J Wang To: edk2-devel@lists.01.org Date: Tue, 19 Sep 2017 14:10:12 +0800 Message-Id: <20170919061013.15976-2-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20170919061013.15976-1-jian.j.wang@intel.com> References: <20170919061013.15976-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH 1/2] UefiCpuPkg/CpuDxe: Fix out-of-sync issue in CpuDxe X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Kinney , Laszlo Ersek , Jiewen Yao , Eric Dong , 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 CpuDxe driver perspective, it doesn't update GCD memory attributes from current page table setup during its initialization. So the memory attributes in GCD might not reflect all memory attributes in real world. Cc: Eric Dong Cc: Jiewen Yao Cc: Star Zeng Cc: Laszlo Ersek Cc: Michael Kinney Suggested-by: Jiewen Yao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- UefiCpuPkg/CpuDxe/CpuDxe.c | 5 ++ UefiCpuPkg/CpuDxe/CpuDxe.h | 9 ++++ UefiCpuPkg/CpuDxe/CpuPageTable.c | 99 ++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 113 insertions(+) diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c index b386f3b677..4e8fa100e0 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.c +++ b/UefiCpuPkg/CpuDxe/CpuDxe.c @@ -863,6 +863,11 @@ RefreshGcdMemoryAttributes ( FreePool (MemorySpaceMap); } =20 + // + // Update page attributes + // + RefreshGcdMemoryAttributesFromPaging(); + mIsFlushingGCD =3D FALSE; } =20 diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.h b/UefiCpuPkg/CpuDxe/CpuDxe.h index 4861abee76..a25b35c6eb 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.h +++ b/UefiCpuPkg/CpuDxe/CpuDxe.h @@ -52,6 +52,10 @@ EFI_MEMORY_UCE \ ) =20 +#define EFI_MEMORY_PAGETYPE_MASK (EFI_MEMORY_RP | \ + EFI_MEMORY_XP | \ + EFI_MEMORY_RO \ + ) =20 /** Flush CPU data cache. If the instruction cache is fully coherent @@ -261,5 +265,10 @@ SetDataSelectors ( UINT16 Selector ); =20 +VOID +RefreshGcdMemoryAttributesFromPaging ( + VOID + ); + #endif =20 diff --git a/UefiCpuPkg/CpuDxe/CpuPageTable.c b/UefiCpuPkg/CpuDxe/CpuPageTa= ble.c index 2c61e7503e..ae93f3f553 100644 --- a/UefiCpuPkg/CpuDxe/CpuPageTable.c +++ b/UefiCpuPkg/CpuDxe/CpuPageTable.c @@ -23,6 +23,8 @@ #include #include #include + +#include "CpuDxe.h" #include "CpuPageTable.h" =20 /// @@ -767,6 +769,103 @@ AssignMemoryPageAttributes ( return Status; } =20 +/** + Update GCD memory space attributes according to current page table setup. +**/ +VOID +RefreshGcdMemoryAttributesFromPaging ( + VOID + ) +{ + EFI_STATUS Status; + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap; + PAGE_TABLE_LIB_PAGING_CONTEXT PagingContext; + PAGE_ATTRIBUTE PageAttribute; + UINT64 *PageEntry; + UINT64 PageLength; + UINT64 MemorySpaceLength; + UINT64 Length; + UINT64 BaseAddress; + UINT64 PageStartAddress; + UINT64 Attributes; + UINT64 Capabilities; + BOOLEAN DoUpdate; + UINTN Index; + + // + // Assuming that memory space map returned is sorted already; otherwise = sort + // them in the order of lowest address to highest address. + // + Status =3D gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap= ); + ASSERT_EFI_ERROR (Status); + + GetCurrentPagingContext (&PagingContext); + + BaseAddress =3D 0; + PageLength =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemorySpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeNonExis= tent) { + continue; + } + + if (MemorySpaceMap[Index].BaseAddress >=3D (BaseAddress + PageLength))= { + // + // Current memory space starts at a new page. Resetting PageLength w= ill + // trigger a retrieval of page attributes at new address. + // + PageLength =3D 0; + } else { + // + // In case current memory space is not adjacent to last one + // + PageLength -=3D (MemorySpaceMap[Index].BaseAddress - BaseAddress); + } + + // Sync real page attributes to GCD + BaseAddress =3D MemorySpaceMap[Index].BaseAddress; + MemorySpaceLength =3D MemorySpaceMap[Index].Length; + while (MemorySpaceLength > 0) { + if (PageLength =3D=3D 0) { + PageEntry =3D GetPageTableEntry (&PagingContext, BaseAddress, &Pag= eAttribute); + if (PageEntry =3D=3D NULL) { + break; + } + + // + // Note current memory space might start in the middle of a page + // + PageStartAddress =3D (*PageEntry) & (UINT64)PageAttributeToMask(P= ageAttribute); + PageLength =3D PageAttributeToLength (PageAttribute) - (Bas= eAddress - PageStartAddress); + Attributes =3D GetAttributesFromPageEntry (PageEntry); + + if (Attributes !=3D (MemorySpaceMap[Index].Attributes & EFI_MEMORY= _PAGETYPE_MASK)) { + DoUpdate =3D TRUE; + Attributes |=3D (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_= PAGETYPE_MASK); + Capabilities =3D Attributes | MemorySpaceMap[Index].Capabilities; + } else { + DoUpdate =3D FALSE; + } + } + + Length =3D MIN (PageLength, MemorySpaceLength); + if (DoUpdate) { + gDS->SetMemorySpaceCapabilities (BaseAddress, Length, Capabilities= ); + gDS->SetMemorySpaceAttributes (BaseAddress, Length, Attributes); + DEBUG ((DEBUG_INFO, "Update memory space attribute: [%02d] %016lx = - %016lx (%08lx -> %08lx)\r\n", + Index, BaseAddress, BaseAddress + Length - 1, + MemorySpaceMap[Index].Attributes, Attributes)= ); + } + + PageLength -=3D Length; + MemorySpaceLength -=3D Length; + BaseAddress +=3D Length; + } + } + + FreePool (MemorySpaceMap); +} + /** Initialize the Page Table lib. **/ --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Thu May 2 20:35:14 2024 Delivered-To: importer@patchew.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; 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 Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1505801426868985.1395668652539; Mon, 18 Sep 2017 23:10:26 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A32E921E945F7; Mon, 18 Sep 2017 23:07:21 -0700 (PDT) Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (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 F094021D046BF for ; Mon, 18 Sep 2017 23:07:20 -0700 (PDT) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Sep 2017 23:10:25 -0700 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.46]) by orsmga005.jf.intel.com with ESMTP; 18 Sep 2017 23:10:23 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,416,1500966000"; d="scan'208,223";a="150779173" From: Jian J Wang To: edk2-devel@lists.01.org Date: Tue, 19 Sep 2017 14:10:13 +0800 Message-Id: <20170919061013.15976-3-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20170919061013.15976-1-jian.j.wang@intel.com> References: <20170919061013.15976-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH 2/2] MdeModulePkg/Core: Fix out-of-sync issue in GCD X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Kinney , Laszlo Ersek , Jiewen Yao , Eric Dong , 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 GCD perspective, its SetMemorySpaceAttributes() method doesn't accept page related attributes. That means users cannot use it to change page attributes, and have to turn to CPU arch protocol to do it, which is not be allowed by PI spec. Cc: Jiewen Yao Cc: Eric Dong Cc: Star Zeng Cc: Laszlo Ersek Cc: Michael Kinney Suggested-by: Jiewen Yao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang Reviewed-by: Star Zeng for this patch. --- MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 45 ++++++++++++++++++++++++-------------= ---- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gc= d.c index a06f8bb77c..e9d1d5b612 100644 --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c @@ -40,6 +40,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHE= R EXPRESS OR IMPLIED. =20 #define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT) =20 +#define EXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \ + EFI_MEMORY_WT | EFI_MEMORY_WB | \ + EFI_MEMORY_WP | EFI_MEMORY_UCE) + +#define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \ + EFI_MEMORY_RO) + #define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff =20 // @@ -654,28 +661,30 @@ ConverToCpuArchAttributes ( UINT64 Attributes ) { - if ( (Attributes & EFI_MEMORY_UC) =3D=3D EFI_MEMORY_UC) { - return EFI_MEMORY_UC; - } + UINT64 CpuArchAttributes; =20 - if ( (Attributes & EFI_MEMORY_WC ) =3D=3D EFI_MEMORY_WC) { - return EFI_MEMORY_WC; + if ((Attributes & ~(EXCLUSIVE_MEMORY_ATTRIBUTES | + NONEXCLUSIVE_MEMORY_ATTRIBUTES)) !=3D 0) { + return INVALID_CPU_ARCH_ATTRIBUTES; } =20 - if ( (Attributes & EFI_MEMORY_WT ) =3D=3D EFI_MEMORY_WT) { - return EFI_MEMORY_WT; - } - - if ( (Attributes & EFI_MEMORY_WB) =3D=3D EFI_MEMORY_WB) { - return EFI_MEMORY_WB; - } - - if ( (Attributes & EFI_MEMORY_WP) =3D=3D EFI_MEMORY_WP) { - return EFI_MEMORY_WP; - } - - return INVALID_CPU_ARCH_ATTRIBUTES; + CpuArchAttributes =3D Attributes & NONEXCLUSIVE_MEMORY_ATTRIBUTES; =20 + if ( (Attributes & EFI_MEMORY_UC) =3D=3D EFI_MEMORY_UC) { + CpuArchAttributes |=3D EFI_MEMORY_UC; + } else if ( (Attributes & EFI_MEMORY_WC ) =3D=3D EFI_MEMORY_WC) { + CpuArchAttributes |=3D EFI_MEMORY_WC; + } else if ( (Attributes & EFI_MEMORY_WT ) =3D=3D EFI_MEMORY_WT) { + CpuArchAttributes |=3D EFI_MEMORY_WT; + } else if ( (Attributes & EFI_MEMORY_WB) =3D=3D EFI_MEMORY_WB) { + CpuArchAttributes |=3D EFI_MEMORY_WB; + } else if ( (Attributes & EFI_MEMORY_UCE) =3D=3D EFI_MEMORY_UCE) { + CpuArchAttributes |=3D EFI_MEMORY_UCE; + } else if ( (Attributes & EFI_MEMORY_WP) =3D=3D EFI_MEMORY_WP) { + CpuArchAttributes |=3D EFI_MEMORY_WP; + } + + return CpuArchAttributes; } =20 =20 --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel