From nobody Sat May 18 22:31:29 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+97857+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+97857+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1672725184; cv=none; d=zohomail.com; s=zohoarc; b=MhISOW++U/UNP+WDWOHs5cBSY0wTD21ussSI2m+ymbDqppqYlMZ9gY9rwwXwkqo0n5zRATzRR1YZAZ3pQEDa8M3VhH/YwAWbmlxEosesFd49lxdBQ0lmQldH5fqdhLRYDAe/aeUmi0UhQzmRfNf2tFLhd1DQ6I3GRa3+oEGaNCU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672725184; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=wcQCVch3hNptaq9HpNXIDKc7AWAZ2yBRZfVZIXecVmE=; b=g+lW7lwu5o++VSgmLsMt5pQ4O/8trIOMnWm10qe4nl1V8stFdkYjy3xwx17OLBZz0aGq5VmIjRc9US9pwkrhbyaXuPGyzCfJiWcsYm0tva7jRDA68Qm1g75a/fTcoAsT/2NwWDCHiENV1hjDz4XVp00UNxtetx3AQVcoMOomIUg= 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+97857+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 1672725184729711.4341970772508; Mon, 2 Jan 2023 21:53:04 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id KlMOYY1788612xJWKmMki9I3; Mon, 02 Jan 2023 21:53:04 -0800 X-Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web11.57377.1672725183740248968 for ; Mon, 02 Jan 2023 21:53:03 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="301275535" X-IronPort-AV: E=Sophos;i="5.96,296,1665471600"; d="scan'208";a="301275535" X-Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 21:52:48 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="828744224" X-IronPort-AV: E=Sophos;i="5.96,296,1665471600"; d="scan'208";a="828744224" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.24]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 21:52:46 -0800 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [Patch V3] UefiCpuPkg/PiSmmCpuDxeSmm:Fix PF issue caused by smm page table code Date: Tue, 3 Jan 2023 13:52:32 +0800 Message-Id: <20230103055232.2037-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: eypKM2wO5YdSyuAztUJy9jeGx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1672725184; bh=5vW0AwuT5XW/GYMSF2x/BvBibCRyYsWslmjqPogrx04=; h=Cc:Date:From:Reply-To:Subject:To; b=AFliIHTFhdTi5sD1c9Gxx1HVZj6ma/pMsi/fcvi1f7nCh9QKN2h8A6opIDWBYH0mi6U 6FxMQgz3TnZ91B4pd4gricpsbqIlxZL8qdFDkPZnY6+qZ43jeqU0RSPQpI6jQymQiuNjP 0Rp0LtuYE6tJ1ZwxG+muTgLE7vNxu4Ecwo0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1672725186751100001 Content-Type: text/plain; charset="utf-8" When setting new page table pool to RO, only disable/enable WP when Cr0.WP has been set to 1 to fix potential PF caused by b822be1a20 (UefiCpuPkg/PiSmmCpuDxeSmm: Introduce page table pool mechanism). With previous code, if someone want to modify the page table and Cr0.WP has been cleared before modify page table, Cr0.WP may be set to 1 again since new pool may be generated during this process Then PF fault may happens. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 40 ++++++++++++++++++= ++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 4bb23f6920..9519f12654 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -67,8 +67,10 @@ InitializePageTablePool ( IN UINTN PoolPages ) { - VOID *Buffer; - BOOLEAN CetEnabled; + VOID *Buffer; + BOOLEAN CetEnabled; + BOOLEAN WpEnabled; + IA32_CR0 Cr0; =20 // // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one pag= e for @@ -106,21 +108,35 @@ InitializePageTablePool ( // if (mIsReadOnlyPageTable) { CetEnabled =3D ((AsmReadCr4 () & CR4_CET_ENABLE) !=3D 0) ? TRUE : FALS= E; - if (CetEnabled) { + Cr0.UintN =3D AsmReadCr0 (); + WpEnabled =3D (Cr0.Bits.WP !=3D 0) ? TRUE : FALSE; + if (WpEnabled) { + if (CetEnabled) { + // + // CET must be disabled if WP is disabled. Disable CET before clea= ring CR0.WP.=20 + // + DisableCet (); + } + // - // CET must be disabled if WP is disabled. + // Only disable/enable WP when Cr0.Bits.WP has been set to 1. // - DisableCet (); + Cr0.Bits.WP =3D 0; + AsmWriteCr0 (Cr0.UintN); } =20 - AsmWriteCr0 (AsmReadCr0 () & ~CR0_WP); SmmSetMemoryAttributes ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, EFI_PAGES= _TO_SIZE (PoolPages), EFI_MEMORY_RO); - AsmWriteCr0 (AsmReadCr0 () | CR0_WP); - if (CetEnabled) { - // - // re-enable CET. - // - EnableCet (); + if (WpEnabled) { + Cr0.UintN =3D AsmReadCr0 (); + Cr0.Bits.WP =3D 1; + AsmWriteCr0 (Cr0.UintN); + + if (CetEnabled) { + // + // re-enable CET. + // + EnableCet (); + } } } =20 --=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 (#97857): https://edk2.groups.io/g/devel/message/97857 Mute This Topic: https://groups.io/mt/96023863/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-