From nobody Tue May 7 13:05:21 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+92289+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+92289+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660109914; cv=none; d=zohomail.com; s=zohoarc; b=TPqa4FTCYLCQkdifj8x3cRmf6xxKJV5331xJ8Ux4Xkm+OkFMYwn5Ugg8ohw+TlMyvJVuiAEI44f1y81t+O/Yy0J8mHKhGe/7Dd/WJS5Uqxl1uJ8rc9/t15fPn1guhGaqKuBc9jhXXGR92bmTFHA8W+ofAHFO9kfeh4OR1raT9QE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660109914; 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=HKEIej3DDXk0OMhhjdLov9IkQy527oKaz4oMeniczzo=; b=E1XG/PRfLjQbRF0C2PT0NaaccYGJjEfEuv2wmo8VEp40gaAaM3QHmZ1kvi8/6cZjualTyF2AAau1PK70yKRYFJJ1IxvTDAqpASq2kV5k7gWXvTMWwEb/nkTlb9rcRO/i7Jn96tv/cgeYfGM6i6W1rwv/1eHZBMMXNICF/4TzkxQ= 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+92289+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 1660109914793540.6750314773432; Tue, 9 Aug 2022 22:38:34 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id sSuMYY1788612xlR1aZQF4uC; Tue, 09 Aug 2022 22:38:34 -0700 X-Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web09.2557.1660109910932314511 for ; Tue, 09 Aug 2022 22:38:34 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10434"; a="277940669" X-IronPort-AV: E=Sophos;i="5.93,226,1654585200"; d="scan'208";a="277940669" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 22:38:33 -0700 X-IronPort-AV: E=Sophos;i="5.93,226,1654585200"; d="scan'208";a="581094663" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.47]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 22:38:32 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [Patch V2 1/2] UefiCpuPkg/PiSmmCpuDxeSmm: Add a new mIsShadowStack flag Date: Wed, 10 Aug 2022 13:37:12 +0800 Message-Id: <20220810053713.378-2-dun.tan@intel.com> In-Reply-To: <20220810053713.378-1-dun.tan@intel.com> References: <20220810053713.378-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: DdjKSx1o9vYV3Zwc1tcDublbx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660109914; bh=JBnAfb3+S5oXdRx4AtzuG2XSYyy2LYXGhPTOAFU5ddE=; h=Cc:Date:From:Reply-To:Subject:To; b=eGM5f2RmHISz6gZauQcyxUVTRwAZXApYFiFqtZVcgr1V9IaqurQ4cigVwhgeFfiDEa3 HcSpMexNE1SL9HPN4ZBqB/LfRpj8V04Men9x4E6rkDpClxHTBU4Dk+tz8a1yGyzjWateT ypE/4Vl02GOLKxqyef5chQVElsVx+zCi7bU= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660109916613100002 Content-Type: text/plain; charset="utf-8" This patch is code refactoring and doesn't change any functionality. Add a new IsShadowStack flag to identify whether current memory is shadow stack. Previous smm code logic regards a RO range as shadow stack and set the dirty bit in corresponding page table entry if mInternalCr3 is not 0, which may be confusing. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Reviewed-by: Ray Ni --- 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..237742d7e6 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -33,6 +33,7 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { }; =20 UINTN mInternalCr3; +UINTN mIsShadowStack =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 (mIsShadowStack) { // 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); + mIsShadowStack =3D TRUE; + Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMO= RY_RO); =20 SetPageTableBase (0); + mIsShadowStack =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 (#92289): https://edk2.groups.io/g/devel/message/92289 Mute This Topic: https://groups.io/mt/92931344/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 Tue May 7 13:05:21 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+92290+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+92290+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660109918; cv=none; d=zohomail.com; s=zohoarc; b=llM+p0Du8HyVzLz4P0dLgTblztuMXlvsVIVin/f6kjca4DFEguzMsOYneT1vNGwtmsjnbW4oGPPcnvAetF1qBD+xIWqknyJoRRir81cm3Um2Ph8C4wTYL8xuy6tMzIeUaIrhFRWF2zLXcQyTT6fhkA1RjO55Tb9v4cwQl+wxYLY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660109918; 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=SJKGXLdMUH4tZ9ugyMjn/446pJ8E4GuWxBKErNOaO4M=; b=n3EWARwS1EVPwN3kPEXF/LpY3kbKi7Csw8H43cnmqaNSSjh2J6wWGzVB8QeWc7ca1oGjDy5UdGj02FnMsg5Hu4C3t3v+QYR5Q+W3R9E8IIKh6/SZuQys7s2BSeb5hWIPmTxtPlMG6fNgophxUP7nyUwaHiqTU6KeAFk4eYZctGg= 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+92290+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 166010991821338.39447625816399; Tue, 9 Aug 2022 22:38:38 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id tTBsYY1788612xiCckw9CWyM; Tue, 09 Aug 2022 22:38:37 -0700 X-Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web09.2557.1660109910932314511 for ; Tue, 09 Aug 2022 22:38:37 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10434"; a="277940686" X-IronPort-AV: E=Sophos;i="5.93,226,1654585200"; d="scan'208";a="277940686" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 22:38:36 -0700 X-IronPort-AV: E=Sophos;i="5.93,226,1654585200"; d="scan'208";a="581094678" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.47]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2022 22:38:35 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [Patch V2 2/2] UefiCpuPkg/PiSmmCpuDxeSmm: Remove mInternalCr3 in PiSmmCpuDxeSmm Date: Wed, 10 Aug 2022 13:37:13 +0800 Message-Id: <20220810053713.378-3-dun.tan@intel.com> In-Reply-To: <20220810053713.378-1-dun.tan@intel.com> References: <20220810053713.378-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: wZ8wK7kuPaohBe8wCU3CaDWEx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660109917; bh=cqacNLT8hYx1jvZ2ka1eOYbBizaoatA4oQSYQjqy8S0=; h=Cc:Date:From:Reply-To:Subject:To; b=RBehew2nGMJmvIrOwY5qe6WdxmyyNOYUlzPA3laWNMP2hTyocZCzwhpGOBYV1yJNpYm iELb+hFsJMo97DxA/V4vy9X3CiPpjicCQ/O2p/4YfnaWR/DeokMmifCN/UWqj/OxZ0tuu pRtIMQyTzao+KOlWTR6ZDvR0Wht3FCZLt3E= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660109918694100005 Content-Type: text/plain; charset="utf-8" This patch is code refactoring and doesn't change any functionality. Remove mInternalCr3 in PiSmmCpuDxe pagetable related code. In previous code, mInternalCr3 is used to pass address of page table which is different from Cr3 register in different level of SetMemoryAttributes function. Now remove it and pass the page table base address from the root function parameter to simplify the code logic. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Reviewed-by: Ray Ni --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 30 +++++------------= ------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 26 +++++++++--------= --------- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 110 +++++++++++++++++= ++++++++++++++++++++++++++++++++++++++++-----------------------------------= ------------------ UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 73 +++++++++++++++++= ++++++-------------------------------------------------- 4 files changed, 94 insertions(+), 145 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 237742d7e6..3602d99fc4 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 mIsShadowStack =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 /** @@ -734,11 +739,8 @@ SetShadowStack ( { EFI_STATUS Status; =20 - SetPageTableBase (Cr3); mIsShadowStack =3D TRUE; - Status =3D SmmSetMemoryAttributes (BaseAddress, Length, EFI_MEMO= RY_RO); - - SetPageTableBase (0); + Status =3D SmmSetMemoryAttributesEx (Cr3, m5LevelPagingNeeded, B= aseAddress, Length, EFI_MEMORY_RO, NULL); mIsShadowStack =3D FALSE; =20 return Status; @@ -762,12 +764,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 +1557,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 +1568,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 (#92290): https://edk2.groups.io/g/devel/message/92290 Mute This Topic: https://groups.io/mt/92931346/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-