From nobody Sat May 18 22:54:03 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+97854+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+97854+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1672720552; cv=none; d=zohomail.com; s=zohoarc; b=G+DOa0XXShOeexmtswbgMCxIMMzhM4nY99bQFVoGcyOu45iqT6oHcnMBYj5In1u6SpmaJ7kiAoNVhoWG8n9NvWH1VH14jlPkmyacdYhVf4DCMRDnCzcZRs316oPxSXwli+p68XpttcYIangwJn9yqZ34Q93yB0eiF4IZT8Xw3M8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672720552; 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=N0aem0Es9L7rBCOvT2X9fGOBoNguEkrIpjxWontAJ8A=; b=euZexr4kFl6r7lote4WddIWqQ0A0p94wcTU4+qlh6ZpupXE5FaHaG4qTEAJB3yod7XtTHWxHrg5m9upSpFgmUFP6dE08PWhjq3FUU/00GxM/Uj+9cqt8lV5YFMuTp3qQEVLjbqCsFhOOM3Dr1cmpqM7gMPcxl0VOx5V2OJziq4s= 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+97854+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 1672720552495450.5528953770589; Mon, 2 Jan 2023 20:35:52 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id h5zQYY1788612xgqY92BUAr0; Mon, 02 Jan 2023 20:35:51 -0800 X-Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web11.56554.1672720550848838343 for ; Mon, 02 Jan 2023 20:35:51 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="322815049" X-IronPort-AV: E=Sophos;i="5.96,295,1665471600"; d="scan'208";a="322815049" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 20:35:50 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="685258098" X-IronPort-AV: E=Sophos;i="5.96,295,1665471600"; d="scan'208";a="685258098" X-Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.24]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 20:35:48 -0800 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [Patch V2] UefiCpuPkg/PiSmmCpuDxeSmm:Fix PF issue caused by smm page table code Date: Tue, 3 Jan 2023 12:35:18 +0800 Message-Id: <20230103043518.1970-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: 5JFZnoefmnojxEt9EG7qeVFNx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1672720551; bh=yy9TBKi+aitD4nnnTBmIxrAyXfI0V0/bwECQBPrrXD4=; h=Cc:Date:From:Reply-To:Subject:To; b=Zaz7oLiVz2zT4HmzJcx4gZj8GW3GjmwVnl+IrWj7z5XVW40ns5syazf5azzomfqV7qT cxOUSHs4Wijqvtgc6O/RWHcTUR+Q4tLJiN7RwH009Ep1LgwLiBP4EGJlzf+Jeam7gJViL F4HbP22qI+sOzPfl5iPHXToVc2+jokLcIYo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1672720553981100001 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..0a5e68ccf4 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. + // + 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 (#97854): https://edk2.groups.io/g/devel/message/97854 Mute This Topic: https://groups.io/mt/96023301/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-