From nobody Sun May 5 09:21:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92268+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92268+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660095955; cv=none; d=zohomail.com; s=zohoarc; b=lN+P8gcWPMOIvmklgH1KNhcQG45h60lXWP77ejXvVIb/NB3vJPX66fwgE2fl60xs1u/JUk5oB4XAyZfXDiIk9FsnkKN6dIR+BhfVmVf77LJLT1lKsttwlo1d5m+VhAIe1fADvururZDCG+8adG/nijGRyyiFen6VDhOHma9pZ9E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660095955; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=4IfVP7XVqAwetREF6Ka1OApS8jPvR2n6ThjOQ/HNCro=; b=hRu4CxyQAivctReinWEHjxlblvwQwAC0+aikN1zxOQ0RYYfNcng02PimvEAvh+CxgfRjslPCDi+eBlE1M6gHJqqp70kvzJ0iXkyFbtBKOQJHbFVI8Nm9dJId+uMuMdHVYm8ilnfnDTw1yTnukzj/Tt2hnNXMb5U7U94Bt5CDElI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92268+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1660095955930781.3974508413476; Tue, 9 Aug 2022 18:45:55 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id X8jCYY1788612xdmSyAY3WxS; Tue, 09 Aug 2022 18:45:55 -0700 X-Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web11.1038.1660095953631651306 for ; Tue, 09 Aug 2022 18:45:54 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10434"; a="277911996" X-IronPort-AV: E=Sophos;i="5.93,225,1654585200"; d="scan'208";a="277911996" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 18:45:54 -0700 X-IronPort-AV: E=Sophos;i="5.93,225,1654585200"; d="scan'208";a="664678126" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.47]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 18:45:53 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [PATCH 1/2] UefiCpuPkg/PiSmmCpuDxeSmm: Add a new IsShadowStack flag Date: Wed, 10 Aug 2022 09:45:31 +0800 Message-Id: <20220810014532.117-2-dun.tan@intel.com> In-Reply-To: <20220810014532.117-1-dun.tan@intel.com> References: <20220810014532.117-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dun.tan@intel.com X-Gm-Message-State: Ka5UZxXzdMFA4RPj07akxX7hx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660095955; bh=LNJeOAn/wK95V5lP0cRDqqsoQKlDfereyQ3bNwcS9to=; h=Cc:Date:From:Reply-To:Subject:To; b=LFyY5UXTRVEGqOybh3OICdZRvk9V6t5HhcjDVyd3bwujXtnCFKxv8OP/KSmKZDbNNE7 1yN7WyddRiRBWcRq0cdY+e4ZwUTP0o6ozFh9aYOKnEd61oLt8fKO6JuCkO1bzKZM4K8mi xuQYfwiK5/EAb9W0M9Ep8mlGgkXmLkLW+1k= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660095956627100001 Content-Type: text/plain; charset="utf-8" Add a new IsShadowStack flag to identify whether current memory is shadow stack. The dirty bit in page table entry for this memory will be set if IsShadowStack is TRUE, instead of depending on mInternalCr3. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 1f7cc15727..b369c0c435 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -33,6 +33,7 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { }; =20 UINTN mInternalCr3; +UINTN IsShadowStack =3D FALSE; =20 /** Set the internal page table base address. @@ -249,7 +250,7 @@ ConvertPageEntryAttribute ( if ((Attributes & EFI_MEMORY_RO) !=3D 0) { if (IsSet) { NewPageEntry &=3D ~(UINT64)IA32_PG_RW; - if (mInternalCr3 !=3D 0) { + if (IsShadowStack) { // Environment setup // ReadOnly page need set Dirty bit for shadow stack NewPageEntry |=3D IA32_PG_D; @@ -734,10 +735,11 @@ SetShadowStack ( EFI_STATUS Status; =20 SetPageTableBase (Cr3); - - Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_RO); + IsShadowStack =3D TRUE; + Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMOR= Y_RO); =20 SetPageTableBase (0); + IsShadowStack =3D FALSE; =20 return Status; } --=20 2.31.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#92268): https://edk2.groups.io/g/devel/message/92268 Mute This Topic: https://groups.io/mt/92928945/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Sun May 5 09:21:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92269+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92269+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660095967; cv=none; d=zohomail.com; s=zohoarc; b=UyMzXwUBUe+MGLfQ1a8pO8/qfyEeFLrjumuc/IoUOVZ9t84gKeRFgPnVc12U5Ja0izdF8RhXsFy6XyhPtaPTHwFjcoRAnrs7LPyouK5upResG5CExsUE4Xdxpc5LoqA67mfLtD6W711srl66VYYYSIKmvVBFR7in5aOxjFMWgoE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660095967; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=O9lZNopXCSxbX2czHeZjsZGgDbKJwHDtdjhY59OPANo=; b=BPLwjJqbNYykxG4aaEo+APYKuKrta9ehfdVp6WBVSTB7JWrD5PY3L4Qpvr1ptiKUC/RX13Kv5dW8WO8XsmV0/mBY8xAHHkZf6dYqqscG1/t2qDiYisozcn3O6ZnW/yjhX8GpXzFx9hyBWrpP+JtpYv7FmcBECpG7t5Y5bpbku7U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92269+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1660095967361544.6050992146712; Tue, 9 Aug 2022 18:46:07 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id bTAzYY1788612xIkzhxqrXEV; Tue, 09 Aug 2022 18:46:05 -0700 X-Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web09.1047.1660095964956259574 for ; Tue, 09 Aug 2022 18:46:05 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10434"; a="274024945" X-IronPort-AV: E=Sophos;i="5.93,225,1654585200"; d="scan'208";a="274024945" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 18:46:03 -0700 X-IronPort-AV: E=Sophos;i="5.93,225,1654585200"; d="scan'208";a="664678197" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.47]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 18:46:02 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [PATCH 2/2] UefiCpuPkg/PiSmmCpuDxeSmm: Remove mInternalCr3 in PiSmmCpuDxeSmm Date: Wed, 10 Aug 2022 09:45:32 +0800 Message-Id: <20220810014532.117-3-dun.tan@intel.com> In-Reply-To: <20220810014532.117-1-dun.tan@intel.com> References: <20220810014532.117-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dun.tan@intel.com X-Gm-Message-State: Ffw7eJsMTEAX00oIWCJwYfSVx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660095965; bh=EvYO+jBf3NlN6oqu8QxVXBdvgeTHYxSOXMzgp4cmXS8=; h=Cc:Date:From:Reply-To:Subject:To; b=emPkP5ZXJZdkW0viq+Q81IscGk6xpWKjH4nc6ppXietkC+tI8z7JlYNHK+Y7GHN6yv3 BNsFYQvaAJS8w5UO8+UMO/mED8lbCi08pIOHEOCM/FXwLm0lKkNJPPpHe8dBAICSl+DNQ YdrjIMFH7CJQi6x2uEEd+rlFwLZLZ2fvDM4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660095968973100003 Content-Type: text/plain; charset="utf-8" Remove mInternalCr3 in PiSmmCpuDxe pagetable related code. Currently, mInternalCr3 is used to pass address of pagetable which is different from Cr3 register. Now remove it and pass the page table base address from the root function to simplify the code logic. Change-Id: I8f58158b94a01cf829f1b4fb2b8c763dcdda0662 Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Signed-off-by: Dun Tan Reviewed-by: Ray Ni --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 30 +++++------------= ------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 26 +++++++++--------= --------- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 107 +++++++++++++++++= ++++++++++++++++++++++++++++++++++++++++-----------------------------------= --------------- UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 73 +++++++++++++++++= ++++++-------------------------------------------------- 4 files changed, 94 insertions(+), 142 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c b/UefiCpuPkg/PiSmmCpu= DxeSmm/Ia32/PageTbl.c index 8ec8790c05..97058a2810 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c @@ -28,26 +28,6 @@ EnableCet ( VOID ); =20 -/** - Get page table base address and the depth of the page table. - - @param[out] Base Page table base address. - @param[out] FiveLevels TRUE means 5 level paging. FALSE means 4 level p= aging. -**/ -VOID -GetPageTable ( - OUT UINTN *Base, - OUT BOOLEAN *FiveLevels OPTIONAL - ) -{ - *Base =3D ((mInternalCr3 =3D=3D 0) ? - (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64) : - mInternalCr3); - if (FiveLevels !=3D NULL) { - *FiveLevels =3D FALSE; - } -} - /** Create PageTable for SMM use. =20 @@ -297,10 +277,10 @@ SetPageTableAttributes ( DEBUG ((DEBUG_INFO, "Start...\n")); PageTableSplitted =3D FALSE; =20 - GetPageTable (&PageTableBase, NULL); - L3PageTable =3D (UINT64 *)PageTableBase; + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + L3PageTable =3D (UINT64 *)PageTableBase; =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)PageTableBase, SIZE_4K= B, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, FALSE, (EFI_PHYSICAL_ADDRESS)= PageTableBase, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); =20 for (Index3 =3D 0; Index3 < 4; Index3++) { @@ -309,7 +289,7 @@ SetPageTableAttributes ( continue; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L2PageTable, = SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, FALSE, (EFI_PHYSICAL_ADDRES= S)(UINTN)L2PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); =20 for (Index2 =3D 0; Index2 < SIZE_4KB/sizeof (UINT64); Index2++) { @@ -323,7 +303,7 @@ SetPageTableAttributes ( continue; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L1PageTable= , SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, FALSE, (EFI_PHYSICAL_ADDR= ESS)(UINTN)L1PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); } } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index dfeceec2aa..ef8bf5947d 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -264,7 +264,7 @@ extern UINTN mMaxNumberOfCpus; extern UINTN mNumberOfCpus; extern EFI_SMM_CPU_PROTOCOL mSmmCpu; extern EFI_MM_MP_PROTOCOL mSmmMp; -extern UINTN mInternalCr3; +extern BOOLEAN m5LevelPagingNeeded; =20 /// /// The mode of the CPU at the time an SMI occurs @@ -682,7 +682,6 @@ SmmBlockingStartupThisAp ( =20 **/ EFI_STATUS -EFIAPI SmmSetMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, @@ -712,7 +711,6 @@ SmmSetMemoryAttributes ( =20 **/ EFI_STATUS -EFIAPI SmmClearMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, @@ -957,22 +955,12 @@ SetPageTableAttributes ( VOID ); =20 -/** - Get page table base address and the depth of the page table. - - @param[out] Base Page table base address. - @param[out] FiveLevels TRUE means 5 level paging. FALSE means 4 level p= aging. -**/ -VOID -GetPageTable ( - OUT UINTN *Base, - OUT BOOLEAN *FiveLevels OPTIONAL - ); - /** This function sets the attributes for the memory region specified by Bas= eAddress and Length from their current attributes to the attributes specified by Attr= ibutes. =20 + @param[in] PageTableBase The page table base. + @param[in] EnablePML5Paging If PML5 paging is enabled. @param[in] BaseAddress The physical address that is the start add= ress of a memory region. @param[in] Length The size in bytes of the memory region. @param[in] Attributes The bit mask of attributes to set for the = memory region. @@ -993,8 +981,9 @@ GetPageTable ( =20 **/ EFI_STATUS -EFIAPI SmmSetMemoryAttributesEx ( + IN UINTN PageTableBase, + IN BOOLEAN EnablePML5Paging, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, @@ -1005,6 +994,8 @@ SmmSetMemoryAttributesEx ( This function clears the attributes for the memory region specified by B= aseAddress and Length from their current attributes to the attributes specified by Attr= ibutes. =20 + @param[in] PageTableBase The page table base. + @param[in] EnablePML5Paging If PML5 paging is enabled. @param[in] BaseAddress The physical address that is the start add= ress of a memory region. @param[in] Length The size in bytes of the memory region. @param[in] Attributes The bit mask of attributes to clear for th= e memory region. @@ -1025,8 +1016,9 @@ SmmSetMemoryAttributesEx ( =20 **/ EFI_STATUS -EFIAPI SmmClearMemoryAttributesEx ( + IN UINTN PageTableBase, + IN BOOLEAN EnablePML5Paging, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index b369c0c435..ce513c476f 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -32,24 +32,8 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { { Page1G, SIZE_1GB, PAGING_1G_ADDRESS_MASK_64 }, }; =20 -UINTN mInternalCr3; UINTN IsShadowStack =3D FALSE; =20 -/** - Set the internal page table base address. - If it is non zero, further MemoryAttribute modification will be on this = page table. - If it is zero, further MemoryAttribute modification will be on real page= table. - - @param Cr3 page table base. -**/ -VOID -SetPageTableBase ( - IN UINTN Cr3 - ) -{ - mInternalCr3 =3D Cr3; -} - /** Return length according to page attributes. =20 @@ -99,31 +83,31 @@ PageAttributeToMask ( /** Return page table entry to match the address. =20 - @param[in] Address The address to be checked. - @param[out] PageAttributes The page attribute of the page entry. + @param[in] PageTableBase The page table base. + @param[in] Enable5LevelPaging If PML5 paging is enabled. + @param[in] Address The address to be checked. + @param[out] PageAttributes The page attribute of the page entry. =20 @return The page entry. **/ VOID * GetPageTableEntry ( + IN UINTN PageTableBase, + IN BOOLEAN Enable5LevelPaging, IN PHYSICAL_ADDRESS Address, OUT PAGE_ATTRIBUTE *PageAttribute ) { - UINTN Index1; - UINTN Index2; - UINTN Index3; - UINTN Index4; - UINTN Index5; - UINT64 *L1PageTable; - UINT64 *L2PageTable; - UINT64 *L3PageTable; - UINT64 *L4PageTable; - UINT64 *L5PageTable; - UINTN PageTableBase; - BOOLEAN Enable5LevelPaging; - - GetPageTable (&PageTableBase, &Enable5LevelPaging); + UINTN Index1; + UINTN Index2; + UINTN Index3; + UINTN Index4; + UINTN Index5; + UINT64 *L1PageTable; + UINT64 *L2PageTable; + UINT64 *L3PageTable; + UINT64 *L4PageTable; + UINT64 *L5PageTable; =20 Index5 =3D ((UINTN)RShiftU64 (Address, 48)) & PAGING_PAE_INDEX_MASK; Index4 =3D ((UINTN)RShiftU64 (Address, 39)) & PAGING_PAE_INDEX_MASK; @@ -399,6 +383,8 @@ SplitPage ( =20 Caller should make sure BaseAddress and Length is at page boundary. =20 + @param[in] PageTableBase The page table base. + @param[in] EnablePML5Paging If PML5 paging is enabled. @param[in] BaseAddress The physical address that is the start add= ress of a memory region. @param[in] Length The size in bytes of the memory region. @param[in] Attributes The bit mask of attributes to modify for t= he memory region. @@ -420,8 +406,9 @@ SplitPage ( range specified by BaseAddress and Leng= th. **/ RETURN_STATUS -EFIAPI ConvertMemoryPageAttributes ( + IN UINTN PageTableBase, + IN BOOLEAN EnablePML5Paging, IN PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, @@ -475,7 +462,7 @@ ConvertMemoryPageAttributes ( // Below logic is to check 2M/4K page to make sure we do not waste memor= y. // while (Length !=3D 0) { - PageEntry =3D GetPageTableEntry (BaseAddress, &PageAttribute); + PageEntry =3D GetPageTableEntry (PageTableBase, EnablePML5Paging, Base= Address, &PageAttribute); if (PageEntry =3D=3D NULL) { return RETURN_UNSUPPORTED; } @@ -558,6 +545,8 @@ FlushTlbForAll ( This function sets the attributes for the memory region specified by Bas= eAddress and Length from their current attributes to the attributes specified by Attr= ibutes. =20 + @param[in] PageTableBase The page table base. + @param[in] EnablePML5Paging If PML5 paging is enabled. @param[in] BaseAddress The physical address that is the start add= ress of a memory region. @param[in] Length The size in bytes of the memory region. @param[in] Attributes The bit mask of attributes to set for the = memory region. @@ -578,8 +567,9 @@ FlushTlbForAll ( =20 **/ EFI_STATUS -EFIAPI SmmSetMemoryAttributesEx ( + IN UINTN PageTableBase, + IN BOOLEAN EnablePML5Paging, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, @@ -589,7 +579,7 @@ SmmSetMemoryAttributesEx ( EFI_STATUS Status; BOOLEAN IsModified; =20 - Status =3D ConvertMemoryPageAttributes (BaseAddress, Length, Attributes,= TRUE, IsSplitted, &IsModified); + Status =3D ConvertMemoryPageAttributes (PageTableBase, EnablePML5Paging,= BaseAddress, Length, Attributes, TRUE, IsSplitted, &IsModified); if (!EFI_ERROR (Status)) { if (IsModified) { // @@ -606,6 +596,8 @@ SmmSetMemoryAttributesEx ( This function clears the attributes for the memory region specified by B= aseAddress and Length from their current attributes to the attributes specified by Attr= ibutes. =20 + @param[in] PageTableBase The page table base. + @param[in] EnablePML5Paging If PML5 paging is enabled. @param[in] BaseAddress The physical address that is the start add= ress of a memory region. @param[in] Length The size in bytes of the memory region. @param[in] Attributes The bit mask of attributes to clear for th= e memory region. @@ -626,8 +618,9 @@ SmmSetMemoryAttributesEx ( =20 **/ EFI_STATUS -EFIAPI SmmClearMemoryAttributesEx ( + IN UINTN PageTableBase, + IN BOOLEAN EnablePML5Paging, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, @@ -637,7 +630,7 @@ SmmClearMemoryAttributesEx ( EFI_STATUS Status; BOOLEAN IsModified; =20 - Status =3D ConvertMemoryPageAttributes (BaseAddress, Length, Attributes,= FALSE, IsSplitted, &IsModified); + Status =3D ConvertMemoryPageAttributes (PageTableBase, EnablePML5Paging,= BaseAddress, Length, Attributes, FALSE, IsSplitted, &IsModified); if (!EFI_ERROR (Status)) { if (IsModified) { // @@ -673,14 +666,20 @@ SmmClearMemoryAttributesEx ( =20 **/ EFI_STATUS -EFIAPI SmmSetMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes ) { - return SmmSetMemoryAttributesEx (BaseAddress, Length, Attributes, NULL); + IA32_CR4 Cr4; + UINTN PageTableBase; + BOOLEAN Enable5LevelPaging; + + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + Cr4.UintN =3D AsmReadCr4 (); + Enable5LevelPaging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); + return SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, Base= Address, Length, Attributes, NULL); } =20 /** @@ -706,14 +705,20 @@ SmmSetMemoryAttributes ( =20 **/ EFI_STATUS -EFIAPI SmmClearMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes ) { - return SmmClearMemoryAttributesEx (BaseAddress, Length, Attributes, NULL= ); + IA32_CR4 Cr4; + UINTN PageTableBase; + BOOLEAN Enable5LevelPaging; + + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + Cr4.UintN =3D AsmReadCr4 (); + Enable5LevelPaging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); + return SmmClearMemoryAttributesEx (PageTableBase, Enable5LevelPaging, Ba= seAddress, Length, Attributes, NULL); } =20 /** @@ -736,7 +741,7 @@ SetShadowStack ( =20 SetPageTableBase (Cr3); IsShadowStack =3D TRUE; - Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMOR= Y_RO); + Status =3D SmmSetMemoryAttributesEx (Cr3, m5LevelPagingNeeded, Ba= seAddress, Length, EFI_MEMORY_RO, NULL); =20 SetPageTableBase (0); IsShadowStack =3D FALSE; @@ -762,12 +767,7 @@ SetNotPresentPage ( { EFI_STATUS Status; =20 - SetPageTableBase (Cr3); - - Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_RP); - - SetPageTableBase (0); - + Status =3D SmmSetMemoryAttributesEx (Cr3, m5LevelPagingNeeded, BaseAddre= ss, Length, EFI_MEMORY_RP, NULL); return Status; } =20 @@ -1560,6 +1560,9 @@ EdkiiSmmGetMemoryAttributes ( UINT64 MemAttr; PAGE_ATTRIBUTE PageAttr; INT64 Size; + UINTN PageTableBase; + BOOLEAN EnablePML5Paging; + IA32_CR4 Cr4; =20 if ((Length < SIZE_4KB) || (Attributes =3D=3D NULL)) { return EFI_INVALID_PARAMETER; @@ -1568,8 +1571,12 @@ EdkiiSmmGetMemoryAttributes ( Size =3D (INT64)Length; MemAttr =3D (UINT64)-1; =20 + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + Cr4.UintN =3D AsmReadCr4 (); + EnablePML5Paging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); + do { - PageEntry =3D GetPageTableEntry (BaseAddress, &PageAttr); + PageEntry =3D GetPageTableEntry (PageTableBase, EnablePML5Paging, Base= Address, &PageAttr); if ((PageEntry =3D=3D NULL) || (PageAttr =3D=3D PageNone)) { return EFI_UNSUPPORTED; } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuD= xeSmm/X64/PageTbl.c index 538394f239..6e920b32af 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -113,36 +113,6 @@ Is5LevelPagingNeeded ( } } =20 -/** - Get page table base address and the depth of the page table. - - @param[out] Base Page table base address. - @param[out] FiveLevels TRUE means 5 level paging. FALSE means 4 level p= aging. -**/ -VOID -GetPageTable ( - OUT UINTN *Base, - OUT BOOLEAN *FiveLevels OPTIONAL - ) -{ - IA32_CR4 Cr4; - - if (mInternalCr3 =3D=3D 0) { - *Base =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; - if (FiveLevels !=3D NULL) { - Cr4.UintN =3D AsmReadCr4 (); - *FiveLevels =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); - } - - return; - } - - *Base =3D mInternalCr3; - if (FiveLevels !=3D NULL) { - *FiveLevels =3D m5LevelPagingNeeded; - } -} - /** Set sub-entries number in entry. =20 @@ -1195,20 +1165,21 @@ SetPageTableAttributes ( VOID ) { - UINTN Index2; - UINTN Index3; - UINTN Index4; - UINTN Index5; - UINT64 *L1PageTable; - UINT64 *L2PageTable; - UINT64 *L3PageTable; - UINT64 *L4PageTable; - UINT64 *L5PageTable; - UINTN PageTableBase; - BOOLEAN IsSplitted; - BOOLEAN PageTableSplitted; - BOOLEAN CetEnabled; - BOOLEAN Enable5LevelPaging; + UINTN Index2; + UINTN Index3; + UINTN Index4; + UINTN Index5; + UINT64 *L1PageTable; + UINT64 *L2PageTable; + UINT64 *L3PageTable; + UINT64 *L4PageTable; + UINT64 *L5PageTable; + UINTN PageTableBase; + BOOLEAN IsSplitted; + BOOLEAN PageTableSplitted; + BOOLEAN CetEnabled; + BOOLEAN Enable5LevelPaging; + IA32_CR4 Cr4; =20 // // Don't mark page table memory as read-only if @@ -1258,11 +1229,13 @@ SetPageTableAttributes ( PageTableSplitted =3D FALSE; L5PageTable =3D NULL; =20 - GetPageTable (&PageTableBase, &Enable5LevelPaging); + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + Cr4.UintN =3D AsmReadCr4 (); + Enable5LevelPaging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); =20 if (Enable5LevelPaging) { L5PageTable =3D (UINT64 *)PageTableBase; - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)PageTableBase, SIZE_= 4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, (EFI_PH= YSICAL_ADDRESS)PageTableBase, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); } =20 @@ -1276,7 +1249,7 @@ SetPageTableAttributes ( L4PageTable =3D (UINT64 *)PageTableBase; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L4PageTable, = SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, (EFI_PH= YSICAL_ADDRESS)(UINTN)L4PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); =20 for (Index4 =3D 0; Index4 < SIZE_4KB/sizeof (UINT64); Index4++) { @@ -1285,7 +1258,7 @@ SetPageTableAttributes ( continue; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L3PageTable= , SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, (EFI_= PHYSICAL_ADDRESS)(UINTN)L3PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); PageTableSplitted =3D (PageTableSplitted || IsSplitted); =20 for (Index3 =3D 0; Index3 < SIZE_4KB/sizeof (UINT64); Index3++) { @@ -1299,7 +1272,7 @@ SetPageTableAttributes ( continue; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L2PageTab= le, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, (EF= I_PHYSICAL_ADDRESS)(UINTN)L2PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted= ); PageTableSplitted =3D (PageTableSplitted || IsSplitted); =20 for (Index2 =3D 0; Index2 < SIZE_4KB/sizeof (UINT64); Index2++) { @@ -1313,7 +1286,7 @@ SetPageTableAttributes ( continue; } =20 - SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L1PageT= able, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted); + SmmSetMemoryAttributesEx (PageTableBase, Enable5LevelPaging, (= EFI_PHYSICAL_ADDRESS)(UINTN)L1PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitt= ed); PageTableSplitted =3D (PageTableSplitted || IsSplitted); } } --=20 2.31.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#92269): https://edk2.groups.io/g/devel/message/92269 Mute This Topic: https://groups.io/mt/92928946/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-