From nobody Sun May 19 12:46:00 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+103385+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+103385+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066206; cv=none; d=zohomail.com; s=zohoarc; b=NzVMTwmK3bmuLqSw92GDJUBgv1gvknfsG2Cd8fyz5ksIptlJPqfJ2M8varUHL3xvltaNlMT5lziBH2WMgI4ZN8NruGhM0V6lv3X+DLAISbBCIlIHczTmoTUEupyN6WoNYwmtv55l/+/C8Ut+0ISDEhE8Y7a2kHrrGuuoTEO9gRI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066206; 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=qtIPfH++nLRhcvi4VtXaBk5pTVbqr9sDjgAZG/JdzvM=; b=Cxfx8c0hyPK4tlN78Z5aJwXA6nPvkZN8UqvTEgAgS9IFdHo9eaQMpmMYmjSSrTEx/ybGvbFd7dZDCDA7zkQqVEb6YPGJqVxkk7pgtuccNpyh0Z4ZuHjMb18hm07QS9qAp89UbxaG4ShuZhh1JeekrfON0U7DY2Q+9y2OadJTNAI= 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+103385+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 1682066206791201.7531986049588; Fri, 21 Apr 2023 01:36:46 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id SQubYY1788612xwMhPqHqHqO; Fri, 21 Apr 2023 01:36:46 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7046.1682066202952586572 for ; Fri, 21 Apr 2023 01:36:45 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869605" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869605" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650454" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650454" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:43 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Ray Ni Subject: [edk2-devel] [Patch V3 01/11] OvmfPkg: Add CpuPageTableLib required by PiSmmCpuDxe Date: Fri, 21 Apr 2023 16:36:18 +0800 Message-Id: <20230421083628.1408-2-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: XOpCd7kBTgY4CL1wusHpbRH6x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066206; bh=1f12b0lOjQ8Y7ad8KVHe1JOSPyGCfyOwWn1eUf0OHD8=; h=Cc:Date:From:Reply-To:Subject:To; b=dBGsn56RYNA3ax2xKaYQz+smAhZi48Fdidr7DJaqUQq3GYg5du5ZG9VWxp0gaQynU7T nd68GGohyk/FkXH5Y0rgLQosDIeWjhKIEPPuShXsFcPnFOKBDWXkLUCrpvevFyjwbJrY5 VBpCetROGjxW+VXHNIH7pn48CRAqoMb0jC8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066208740100007 Content-Type: text/plain; charset="utf-8" Add CpuPageTableLib instance required by PiSmmCpuDxe in corresponding DSC files of OvmfPkg. Signed-off-by: Dun Tan Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Ray Ni --- OvmfPkg/CloudHv/CloudHvX64.dsc | 2 +- OvmfPkg/OvmfPkgIa32.dsc | 3 ++- OvmfPkg/OvmfPkgIa32X64.dsc | 2 +- OvmfPkg/OvmfPkgX64.dsc | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index cc2dd925bc..8a2fb8049f 100644 --- a/OvmfPkg/CloudHv/CloudHvX64.dsc +++ b/OvmfPkg/CloudHv/CloudHvX64.dsc @@ -190,6 +190,7 @@ MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif @@ -399,7 +400,6 @@ DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf !endif PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf - CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterr= uptTplLib.inf QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 86177bb948..77f10e2c86 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -1,7 +1,7 @@ ## @file # EFI/Framework Open Virtual Machine Firmware (OVMF) platform # -# Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.
+# Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
# Copyright (c) Microsoft Corporation. # @@ -193,6 +193,7 @@ MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 065b544506..156d6c1434 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -197,6 +197,7 @@ MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif @@ -409,7 +410,6 @@ DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf !endif PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf - CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterr= uptTplLib.inf QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 3d405cd4ad..35bb011212 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -210,6 +210,7 @@ MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf =20 !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf @@ -430,7 +431,6 @@ DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf !endif PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf - CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterr= uptTplLib.inf QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf --=20 2.39.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 (#103385): https://edk2.groups.io/g/devel/message/103385 Mute This Topic: https://groups.io/mt/98406584/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 19 12:46:00 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+103386+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+103386+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066209; cv=none; d=zohomail.com; s=zohoarc; b=jaNCgnHV2TRlMiMNAEZEHjLkfC5ckESpVsfzKrVWDndtsNphsKWsB2NKG3RmOjnHpjiBITLcO0BRGEj9KRd3admquZy+4dfixIXs/78qHezZmEpuvbYyhOi8cyCCeroHIochSlggpyk8WhH2YB+3gAjyFvf0fCOXZiMeqIpf45Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066209; 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=zga+rgj9M88ExDLJ7cnkDYf2+1b5XF3ZkwGup46lzSY=; b=LnIR81zHkyAex225OSaDibhvk9rFaWbL5aivxq7ZbjBZxTfA/QbKXJW3Jrfmdf/5GzIxj1MmD4pGPAUELOUw0eMCtmfR48FIW4nkY6cbOSIhmC+AF9NlJHaC0Fxps8Pkk1x3+5O/oH5430RFoEuxbeUM9Y8NxCZ6PDkZ+Z5BXVE= 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+103386+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 1682066209271169.80038100009017; Fri, 21 Apr 2023 01:36:49 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id HY58YY1788612xDw9XYJHAac; Fri, 21 Apr 2023 01:36:48 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7046.1682066202952586572 for ; Fri, 21 Apr 2023 01:36:48 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869636" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869636" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650458" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650458" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:46 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Sean Rhodes , James Lu , Gua Guo Subject: [edk2-devel] [Patch V3 02/11] UefiPayloadPkg: Add CpuPageTableLib required by PiSmmCpuDxe Date: Fri, 21 Apr 2023 16:36:19 +0800 Message-Id: <20230421083628.1408-3-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: SAYnHgKRywrl6QnBvM8uaoD8x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066208; bh=IyR+9j7DROJQM/DGkNv71JfKuituXNA70dN9r37EIDE=; h=Cc:Date:From:Reply-To:Subject:To; b=cbaTuVhRWqeKq1lvb892vK7psniU4AOy9WmEDf2F3RUeIWvmti4U3TzuZ3daa3o5c7v GVRXRlEOTrUJzA4TSxtwmmagKubGDrdGXPbDLI9Cz99/o/R+WPsC7ZO6WH8xre1DgSzQT 4bzrHidXe7fWk8v/OlJONvYn0YIgcc479Jg= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066209871100009 Content-Type: text/plain; charset="utf-8" Add CpuPageTableLib required by PiSmmCpuDxeSmm in UefiPayloadPkg.dsc. Signed-off-by: Dun Tan Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo --- UefiPayloadPkg/UefiPayloadPkg.dsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 9847f189ff..f3b94432a9 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -206,6 +206,7 @@ OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLib.inf RngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf HobLib|UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf =20 # # UEFI & PI @@ -338,7 +339,6 @@ DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf !endif CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuE= xceptionHandlerLib.inf - CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf !if $(PERFORMANCE_MEASUREMENT_ENABLE) PerformanceLib|MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.= inf --=20 2.39.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 (#103386): https://edk2.groups.io/g/devel/message/103386 Mute This Topic: https://groups.io/mt/98406585/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 19 12:46:00 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+103387+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+103387+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066214; cv=none; d=zohomail.com; s=zohoarc; b=K/T7QULfYhfRg4znOu1tHk7aunQmXi6FrluErZY2M+ZBOFi53m872ihoRKrx1kWo/iSZ5sRpOD96UkCtkMANjANO3glK18Qbb/HlrWX3TCtoC1DEz5aJxNFcMq5XZfmi7sSn79s29UmquR2zrNA5lYBwrESIZBWsDyWp3PxV5Tw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066214; 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=Quat2np0Kc1/oGZbRGhgeybTRwPJz0J7owjNsknZIn0=; b=gdpXVY5uleeGHCU8SttRFx2Ibwqi44Q1tTSDI0aPnN5sivAf/vqvr9XSdqNmSvdINZf68/1b15B+kkqaoPA4SHN6HkFNuU8SiweiSRfuWrlRqngyefAtR7n7CB0jFmgTNmNY6rwj1L8rDlBKin/+R27pcYrRQHBXKW3P5fQvVEE= 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+103387+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 1682066214832989.7846569309951; Fri, 21 Apr 2023 01:36:54 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id oDtMYY1788612xFU3ZZRj8vx; Fri, 21 Apr 2023 01:36:54 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7047.1682066213863432703 for ; Fri, 21 Apr 2023 01:36:53 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869677" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869677" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650464" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650464" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:51 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Tom Lendacky , Ray Ni Subject: [edk2-devel] [Patch V3 03/11] OvmfPkg:Remove code that apply AddressEncMask to non-leaf entry Date: Fri, 21 Apr 2023 16:36:20 +0800 Message-Id: <20230421083628.1408-4-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: 2Pff6W76MkjmMbC1kuoF6uX8x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066214; bh=PDfHsUCEcVGkKNn3TZzXPoeN72QWdG9DziVd75LZxqA=; h=Cc:Date:From:Reply-To:Subject:To; b=wjhI6Xc912Ht5IOmq4m7JC5Zhc+T74TfzO0uxU0FK/w3mQK/p6GDOWq9v2VFX/RlC8c jzkrfq2ehfKNZUDYkRcglkSreeIbKxwNUH7QfDTxQQYAj89kMgvwG/YebQ2OwS22BjD3j yn91BMs2rwpNhfEI+uzVOms1g4uvEtxitJw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066215738100001 Content-Type: text/plain; charset="utf-8" Remove code that apply AddressEncMask to non-leaf entry when split smm page table by MemEncryptSevLib. In FvbServicesSmm driver, it calls MemEncryptSevClearMmioPageEncMask to clear AddressEncMask bit in page table for a specific range. In AMD SEV feature, this AddressEncMask bit in page table is used to indicate if the memory is guest private memory or shared memory. But all memory used by page table are treated as encrypted regardless of encryption bit. So remove the EncMask bit for smm non-leaf page table entry doesn't impact AMD SEV feature. If page split happens in the AddressEncMask bit clear process, there will be some new non-leaf entries with AddressEncMask applied in smm page table. When ReadyToLock, code in PiSmmCpuDxe module will use CpuPageTableLib to modify smm page table. So remove code to apply AddressEncMask for new non-leaf entries since CpuPageTableLib doesn't consume the EncMask PCD. Signed-off-by: Dun Tan Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Tom Lendacky Cc: Ray Ni --- OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c= b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index a1f6e61c1e..f2b821f6d9 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -233,7 +233,7 @@ Split2MPageTo4K ( // Fill in 2M page entry. // *PageEntry2M =3D ((UINT64)(UINTN)PageTableEntry1 | - IA32_PG_P | IA32_PG_RW | AddressEncMask); + IA32_PG_P | IA32_PG_RW); } =20 /** @@ -352,7 +352,7 @@ SetPageTablePoolReadOnly ( PhysicalAddress +=3D LevelSize[Level - 1]; } =20 - PageTable[Index] =3D (UINT64)(UINTN)NewPageTable | AddressEncMask | + PageTable[Index] =3D (UINT64)(UINTN)NewPageTable | IA32_PG_P | IA32_PG_RW; PageTable =3D NewPageTable; } @@ -440,7 +440,7 @@ Split1GPageTo2M ( // Fill in 1G page entry. // *PageEntry1G =3D ((UINT64)(UINTN)PageDirectoryEntry | - IA32_PG_P | IA32_PG_RW | AddressEncMask); + IA32_PG_P | IA32_PG_RW); =20 PhysicalAddress2M =3D PhysicalAddress; for (IndexOfPageDirectoryEntries =3D 0; --=20 2.39.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 (#103387): https://edk2.groups.io/g/devel/message/103387 Mute This Topic: https://groups.io/mt/98406586/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 19 12:46:00 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+103388+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+103388+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066217; cv=none; d=zohomail.com; s=zohoarc; b=njEXeqTjfXdnhCfWO/LXgYFWwXveFGF5375IZNfGf6JuThI8AIkMvajVTCHYMB3JKsqUw6nnV6IlyUQRBdFi9FK7VGU5Id9uq1emlM93qoleOQG+RCYyTW2WFvXG7YvrlkZ1f+7uVoX4kvWOuBM/Ezj7nP1KGQ9nupt2kOYal14= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066217; 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=oaAGki8mRjebXreFWqEvg3b3Uj2WVWYhCna7bV0J1aY=; b=Q+wFv7EYQIpRmkuE2zvJuBb0izYFpnb2qO8+RO2oqEXnCjuhd8QiFIaEFY8y7HyKBPb7VIDB39l1jo4mk6/L7jsSpdJz26LD3Z8SndrwYygHQWgBlTJ+rKhOMDRk2Z8ankzVP+wHvyACHic0DBdcMacg/RqenPUeQZKG4+Fnapk= 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+103388+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 1682066217134656.5682626143532; Fri, 21 Apr 2023 01:36:57 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id jtZQYY1788612xqyc13afnZO; Fri, 21 Apr 2023 01:36:56 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7047.1682066213863432703 for ; Fri, 21 Apr 2023 01:36:56 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869705" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869705" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650467" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650467" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:54 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 04/11] UefiCpuPkg: Use CpuPageTableLib to convert SMM paging attribute. Date: Fri, 21 Apr 2023 16:36:21 +0800 Message-Id: <20230421083628.1408-5-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: H3h1zHHKtwmrAcy0jSn6ihI3x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066216; bh=nOO0X+qgq3lTpvMuKlLzYxRK358ltRzWL3x4FzGS6s4=; h=Cc:Date:From:Reply-To:Subject:To; b=fsMRmerpIcChyW+DBl1+qo0dODHnlp41B8KPgZAs36gamBvJHeTKSVrz64EtY6wfxb6 94YXhBNqtYBCRUr8dOItPOEWOtMxL0xdOYqDM13SX+rZLx0nQOFmKDVWjSqoZJeflqyFO nTSDhK+lqqClc4O2aQfEG3L2MSYt0cE205A= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066218802100001 Content-Type: text/plain; charset="utf-8" Simplify the ConvertMemoryPageAttributes API to convert paging attribute by CpuPageTableLib. In the new API, it calls PageTableMap() to update the page attributes of a memory range. With the PageTableMap() API in CpuPageTableLib, we can remove the complicated page table manipulating code. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 3 ++- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 28 +++++++++++++----= ----------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 1 + UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 403 +++++++++++++++++= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ----------- UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 9 +++++++-- 5 files changed, 114 insertions(+), 330 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c b/UefiCpuPkg/PiSmmCpu= DxeSmm/Ia32/PageTbl.c index 34bf6e1a25..9c8107080a 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c @@ -1,7 +1,7 @@ /** @file Page table manipulation functions for IA-32 processors =20 -Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2023, Intel Corporation. All rights reserved.
Copyright (c) 2017, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -31,6 +31,7 @@ SmmInitPageTable ( InitializeSpinLock (mPFLock); =20 mPhysicalAddressBits =3D 32; + mPagingMode =3D PagingPae; =20 if (FeaturePcdGet (PcdCpuSmmProfileEnable) || HEAP_GUARD_NONSTOP_MODE || diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index a5c2bdd971..ba341cadc6 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -50,6 +50,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include =20 #include #include @@ -260,6 +261,7 @@ extern UINTN mNumberOfCpus; extern EFI_SMM_CPU_PROTOCOL mSmmCpu; extern EFI_MM_MP_PROTOCOL mSmmMp; extern BOOLEAN m5LevelPagingNeeded; +extern PAGING_MODE mPagingMode; =20 /// /// The mode of the CPU at the time an SMI occurs @@ -1008,11 +1010,10 @@ SetPageTableAttributes ( 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] PagingMode The paging mode. @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. - @param[out] IsSplitted TRUE means page table splitted. FALSE mean= s page table not splitted. =20 @retval EFI_SUCCESS The attributes were set for the memory reg= ion. @retval EFI_ACCESS_DENIED The attributes for the memory resource ran= ge specified by @@ -1030,12 +1031,11 @@ SetPageTableAttributes ( **/ EFI_STATUS SmmSetMemoryAttributesEx ( - IN UINTN PageTableBase, - IN BOOLEAN EnablePML5Paging, - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length, - IN UINT64 Attributes, - OUT BOOLEAN *IsSplitted OPTIONAL + IN UINTN PageTableBase, + IN PAGING_MODE PagingMode, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes ); =20 /** @@ -1043,34 +1043,32 @@ SmmSetMemoryAttributesEx ( 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] PagingMode The paging mode. @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. - @param[out] IsSplitted TRUE means page table splitted. FALSE mean= s page table not splitted. =20 @retval EFI_SUCCESS The attributes were cleared for the memory= region. @retval EFI_ACCESS_DENIED The attributes for the memory resource ran= ge specified by BaseAddress and Length cannot be modified. @retval EFI_INVALID_PARAMETER Length is zero. Attributes specified an illegal combinatio= n of attributes that - cannot be set together. + cannot be cleared together. @retval EFI_OUT_OF_RESOURCES There are not enough system resources to m= odify the attributes of the memory resource range. @retval EFI_UNSUPPORTED The processor does not support one or more= bytes of the memory resource range specified by BaseAddress an= d Length. - The bit mask of attributes is not support = for the memory resource + The bit mask of attributes is not supporte= d for the memory resource range specified by BaseAddress and Length. =20 **/ EFI_STATUS SmmClearMemoryAttributesEx ( IN UINTN PageTableBase, - IN BOOLEAN EnablePML5Paging, + IN PAGING_MODE PagingMode, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, - IN UINT64 Attributes, - OUT BOOLEAN *IsSplitted OPTIONAL + IN UINT64 Attributes ); =20 /** diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf b/UefiCpuPkg/PiSm= mCpuDxeSmm/PiSmmCpuDxeSmm.inf index 158e05e264..38d4e950a4 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf @@ -97,6 +97,7 @@ ReportStatusCodeLib SmmCpuFeaturesLib PeCoffGetEntryPointLib + CpuPageTableLib =20 [Protocols] gEfiSmmAccess2ProtocolGuid ## CONSUMES diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 834a756061..deb5895d83 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -1,6 +1,6 @@ /** @file =20 -Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -26,14 +26,9 @@ UINTN mGcdMemNumberOfDesc =3D= 0; =20 EFI_MEMORY_ATTRIBUTES_TABLE *mUefiMemoryAttributesTable =3D NULL; =20 -PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { - { Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64 }, - { Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64 }, - { Page1G, SIZE_1GB, PAGING_1G_ADDRESS_MASK_64 }, -}; - -BOOLEAN mIsShadowStack =3D FALSE; -BOOLEAN m5LevelPagingNeeded =3D FALSE; +BOOLEAN mIsShadowStack =3D FALSE; +BOOLEAN m5LevelPagingNeeded =3D FALSE; +PAGING_MODE mPagingMode =3D PagingModeMax; =20 // // Global variable to keep track current available memory used as page tab= le. @@ -185,52 +180,6 @@ AllocatePageTableMemory ( return Buffer; } =20 -/** - Return length according to page attributes. - - @param[in] PageAttributes The page attribute of the page entry. - - @return The length of page entry. -**/ -UINTN -PageAttributeToLength ( - IN PAGE_ATTRIBUTE PageAttribute - ) -{ - UINTN Index; - - for (Index =3D 0; Index < sizeof (mPageAttributeTable)/sizeof (mPageAttr= ibuteTable[0]); Index++) { - if (PageAttribute =3D=3D mPageAttributeTable[Index].Attribute) { - return (UINTN)mPageAttributeTable[Index].Length; - } - } - - return 0; -} - -/** - Return address mask according to page attributes. - - @param[in] PageAttributes The page attribute of the page entry. - - @return The address mask of page entry. -**/ -UINTN -PageAttributeToMask ( - IN PAGE_ATTRIBUTE PageAttribute - ) -{ - UINTN Index; - - for (Index =3D 0; Index < sizeof (mPageAttributeTable)/sizeof (mPageAttr= ibuteTable[0]); Index++) { - if (PageAttribute =3D=3D mPageAttributeTable[Index].Attribute) { - return (UINTN)mPageAttributeTable[Index].AddressMask; - } - } - - return 0; -} - /** Return page table entry to match the address. =20 @@ -353,181 +302,6 @@ GetAttributesFromPageEntry ( return Attributes; } =20 -/** - Modify memory attributes of page entry. - - @param[in] PageEntry The page entry. - @param[in] Attributes The bit mask of attributes to modify for t= he memory region. - @param[in] IsSet TRUE means to set attributes. FALSE means = to clear attributes. - @param[out] IsModified TRUE means page table modified. FALSE mean= s page table not modified. -**/ -VOID -ConvertPageEntryAttribute ( - IN UINT64 *PageEntry, - IN UINT64 Attributes, - IN BOOLEAN IsSet, - OUT BOOLEAN *IsModified - ) -{ - UINT64 CurrentPageEntry; - UINT64 NewPageEntry; - - CurrentPageEntry =3D *PageEntry; - NewPageEntry =3D CurrentPageEntry; - if ((Attributes & EFI_MEMORY_RP) !=3D 0) { - if (IsSet) { - NewPageEntry &=3D ~(UINT64)IA32_PG_P; - } else { - NewPageEntry |=3D IA32_PG_P; - } - } - - if ((Attributes & EFI_MEMORY_RO) !=3D 0) { - if (IsSet) { - NewPageEntry &=3D ~(UINT64)IA32_PG_RW; - if (mIsShadowStack) { - // Environment setup - // ReadOnly page need set Dirty bit for shadow stack - NewPageEntry |=3D IA32_PG_D; - // Clear user bit for supervisor shadow stack - NewPageEntry &=3D ~(UINT64)IA32_PG_U; - } else { - // Runtime update - // Clear dirty bit for non shadow stack, to protect RO page. - NewPageEntry &=3D ~(UINT64)IA32_PG_D; - } - } else { - NewPageEntry |=3D IA32_PG_RW; - } - } - - if ((Attributes & EFI_MEMORY_XP) !=3D 0) { - if (mXdSupported) { - if (IsSet) { - NewPageEntry |=3D IA32_PG_NX; - } else { - NewPageEntry &=3D ~IA32_PG_NX; - } - } - } - - *PageEntry =3D NewPageEntry; - if (CurrentPageEntry !=3D NewPageEntry) { - *IsModified =3D TRUE; - DEBUG ((DEBUG_VERBOSE, "ConvertPageEntryAttribute 0x%lx", CurrentPageE= ntry)); - DEBUG ((DEBUG_VERBOSE, "->0x%lx\n", NewPageEntry)); - } else { - *IsModified =3D FALSE; - } -} - -/** - This function returns if there is need to split page entry. - - @param[in] BaseAddress The base address to be checked. - @param[in] Length The length to be checked. - @param[in] PageEntry The page entry to be checked. - @param[in] PageAttribute The page attribute of the page entry. - - @retval SplitAttributes on if there is need to split page entry. -**/ -PAGE_ATTRIBUTE -NeedSplitPage ( - IN PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length, - IN UINT64 *PageEntry, - IN PAGE_ATTRIBUTE PageAttribute - ) -{ - UINT64 PageEntryLength; - - PageEntryLength =3D PageAttributeToLength (PageAttribute); - - if (((BaseAddress & (PageEntryLength - 1)) =3D=3D 0) && (Length >=3D Pag= eEntryLength)) { - return PageNone; - } - - if (((BaseAddress & PAGING_2M_MASK) !=3D 0) || (Length < SIZE_2MB)) { - return Page4K; - } - - return Page2M; -} - -/** - This function splits one page entry to small page entries. - - @param[in] PageEntry The page entry to be splitted. - @param[in] PageAttribute The page attribute of the page entry. - @param[in] SplitAttribute How to split the page entry. - - @retval RETURN_SUCCESS The page entry is splitted. - @retval RETURN_UNSUPPORTED The page entry does not support to be = splitted. - @retval RETURN_OUT_OF_RESOURCES No resource to split page entry. -**/ -RETURN_STATUS -SplitPage ( - IN UINT64 *PageEntry, - IN PAGE_ATTRIBUTE PageAttribute, - IN PAGE_ATTRIBUTE SplitAttribute - ) -{ - UINT64 BaseAddress; - UINT64 *NewPageEntry; - UINTN Index; - - ASSERT (PageAttribute =3D=3D Page2M || PageAttribute =3D=3D Page1G); - - if (PageAttribute =3D=3D Page2M) { - // - // Split 2M to 4K - // - ASSERT (SplitAttribute =3D=3D Page4K); - if (SplitAttribute =3D=3D Page4K) { - NewPageEntry =3D AllocatePageTableMemory (1); - DEBUG ((DEBUG_VERBOSE, "Split - 0x%x\n", NewPageEntry)); - if (NewPageEntry =3D=3D NULL) { - return RETURN_OUT_OF_RESOURCES; - } - - BaseAddress =3D *PageEntry & PAGING_2M_ADDRESS_MASK_64; - for (Index =3D 0; Index < SIZE_4KB / sizeof (UINT64); Index++) { - NewPageEntry[Index] =3D (BaseAddress + SIZE_4KB * Index) | mAddres= sEncMask | ((*PageEntry) & PAGE_PROGATE_BITS); - } - - (*PageEntry) =3D (UINT64)(UINTN)NewPageEntry | mAddressEncMask | PAG= E_ATTRIBUTE_BITS; - return RETURN_SUCCESS; - } else { - return RETURN_UNSUPPORTED; - } - } else if (PageAttribute =3D=3D Page1G) { - // - // Split 1G to 2M - // No need support 1G->4K directly, we should use 1G->2M, then 2M->4K = to get more compact page table. - // - ASSERT (SplitAttribute =3D=3D Page2M || SplitAttribute =3D=3D Page4K); - if (((SplitAttribute =3D=3D Page2M) || (SplitAttribute =3D=3D Page4K))= ) { - NewPageEntry =3D AllocatePageTableMemory (1); - DEBUG ((DEBUG_VERBOSE, "Split - 0x%x\n", NewPageEntry)); - if (NewPageEntry =3D=3D NULL) { - return RETURN_OUT_OF_RESOURCES; - } - - BaseAddress =3D *PageEntry & PAGING_1G_ADDRESS_MASK_64; - for (Index =3D 0; Index < SIZE_4KB / sizeof (UINT64); Index++) { - NewPageEntry[Index] =3D (BaseAddress + SIZE_2MB * Index) | mAddres= sEncMask | IA32_PG_PS | ((*PageEntry) & PAGE_PROGATE_BITS); - } - - (*PageEntry) =3D (UINT64)(UINTN)NewPageEntry | mAddressEncMask | PAG= E_ATTRIBUTE_BITS; - return RETURN_SUCCESS; - } else { - return RETURN_UNSUPPORTED; - } - } else { - return RETURN_UNSUPPORTED; - } -} - /** This function modifies the page attributes for the memory region specifi= ed by BaseAddress and Length from their current attributes to the attributes specified by Attr= ibutes. @@ -535,12 +309,11 @@ SplitPage ( 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] PagingMode The paging mode. @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. @param[in] IsSet TRUE means to set attributes. FALSE means = to clear attributes. - @param[out] IsSplitted TRUE means page table splitted. FALSE mean= s page table not splitted. @param[out] IsModified TRUE means page table modified. FALSE mean= s page table not modified. =20 @retval RETURN_SUCCESS The attributes were modified for the me= mory region. @@ -559,21 +332,19 @@ SplitPage ( RETURN_STATUS ConvertMemoryPageAttributes ( IN UINTN PageTableBase, - IN BOOLEAN EnablePML5Paging, + IN PAGING_MODE PagingMode, IN PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, IN BOOLEAN IsSet, - OUT BOOLEAN *IsSplitted OPTIONAL, OUT BOOLEAN *IsModified OPTIONAL ) { - UINT64 *PageEntry; - PAGE_ATTRIBUTE PageAttribute; - UINTN PageEntryLength; - PAGE_ATTRIBUTE SplitAttribute; RETURN_STATUS Status; - BOOLEAN IsEntryModified; + IA32_MAP_ATTRIBUTE PagingAttribute; + IA32_MAP_ATTRIBUTE PagingAttrMask; + UINTN PageTableBufferSize; + VOID *PageTableBuffer; EFI_PHYSICAL_ADDRESS MaximumSupportMemAddress; =20 ASSERT (Attributes !=3D 0); @@ -581,6 +352,7 @@ ConvertMemoryPageAttributes ( =20 ASSERT ((BaseAddress & (SIZE_4KB - 1)) =3D=3D 0); ASSERT ((Length & (SIZE_4KB - 1)) =3D=3D 0); + ASSERT (PageTableBase !=3D 0); =20 if (Length =3D=3D 0) { return RETURN_INVALID_PARAMETER; @@ -599,61 +371,80 @@ ConvertMemoryPageAttributes ( return RETURN_UNSUPPORTED; } =20 - // DEBUG ((DEBUG_ERROR, "ConvertMemoryPageAttributes(%x) - %016lx, %016= lx, %02lx\n", IsSet, BaseAddress, Length, Attributes)); - - if (IsSplitted !=3D NULL) { - *IsSplitted =3D FALSE; - } + PagingAttribute.Uint64 =3D 0; + PagingAttribute.Uint64 =3D mAddressEncMask | BaseAddress; + PagingAttrMask.Uint64 =3D 0; =20 - if (IsModified !=3D NULL) { - *IsModified =3D FALSE; + if ((Attributes & EFI_MEMORY_RO) !=3D 0) { + PagingAttrMask.Bits.ReadWrite =3D 1; + if (IsSet) { + PagingAttribute.Bits.ReadWrite =3D 0; + PagingAttrMask.Bits.Dirty =3D 1; + if (mIsShadowStack) { + // Environment setup + // ReadOnly page need set Dirty bit for shadow stack + PagingAttribute.Bits.Dirty =3D 1; + // Clear user bit for supervisor shadow stack + PagingAttribute.Bits.UserSupervisor =3D 0; + PagingAttrMask.Bits.UserSupervisor =3D 1; + } else { + // Runtime update + // Clear dirty bit for non shadow stack, to protect RO page. + PagingAttribute.Bits.Dirty =3D 0; + } + } else { + PagingAttribute.Bits.ReadWrite =3D 1; + } } =20 - // - // Below logic is to check 2M/4K page to make sure we do not waste memor= y. - // - while (Length !=3D 0) { - PageEntry =3D GetPageTableEntry (PageTableBase, EnablePML5Paging, Base= Address, &PageAttribute); - if (PageEntry =3D=3D NULL) { - return RETURN_UNSUPPORTED; + if ((Attributes & EFI_MEMORY_XP) !=3D 0) { + if (mXdSupported) { + PagingAttribute.Bits.Nx =3D IsSet ? 1 : 0; + PagingAttrMask.Bits.Nx =3D 1; } + } =20 - PageEntryLength =3D PageAttributeToLength (PageAttribute); - SplitAttribute =3D NeedSplitPage (BaseAddress, Length, PageEntry, Pag= eAttribute); - if (SplitAttribute =3D=3D PageNone) { - ConvertPageEntryAttribute (PageEntry, Attributes, IsSet, &IsEntryMod= ified); - if (IsEntryModified) { - if (IsModified !=3D NULL) { - *IsModified =3D TRUE; - } - } - + if ((Attributes & EFI_MEMORY_RP) !=3D 0) { + if (IsSet) { + PagingAttribute.Bits.Present =3D 0; // - // Convert success, move to next + // When map a range to non-present, all attributes except Present sh= ould not be provided. // - BaseAddress +=3D PageEntryLength; - Length -=3D PageEntryLength; + PagingAttrMask.Uint64 =3D 0; + PagingAttrMask.Bits.Present =3D 1; } else { - Status =3D SplitPage (PageEntry, PageAttribute, SplitAttribute); - if (RETURN_ERROR (Status)) { - return RETURN_UNSUPPORTED; - } - - if (IsSplitted !=3D NULL) { - *IsSplitted =3D TRUE; - } - - if (IsModified !=3D NULL) { - *IsModified =3D TRUE; - } - // - // Just split current page - // Convert success in next around + // When map range to present range, provide all attributes. // + PagingAttribute.Bits.Present =3D 1; + PagingAttrMask.Uint64 =3D MAX_UINT64; } } =20 + if (PagingAttrMask.Uint64 =3D=3D 0) { + return RETURN_SUCCESS; + } + + PageTableBufferSize =3D 0; + Status =3D PageTableMap (&PageTableBase, PagingMode, NULL, = &PageTableBufferSize, BaseAddress, Length, &PagingAttribute, &PagingAttrMas= k, IsModified); + + if (Status =3D=3D RETURN_INVALID_PARAMETER) { + // + // The only reason that PageTableMap returns RETURN_INVALID_PARAMETER = here is to modify other attributes + // of non-present range but remains the non-present range still as non= -present. + // + DEBUG ((DEBUG_ERROR, "SMM ConvertMemoryPageAttributes: Non-present ran= ge in [0x%lx, 0x%lx] needs to be removed\n", BaseAddress, BaseAddress + Len= gth)); + } + + if (Status =3D=3D RETURN_BUFFER_TOO_SMALL) { + PageTableBuffer =3D AllocatePageTableMemory (EFI_SIZE_TO_PAGES (PageTa= bleBufferSize)); + ASSERT (PageTableBuffer !=3D NULL); + Status =3D PageTableMap (&PageTableBase, PagingMode, PageTableBuffer, = &PageTableBufferSize, BaseAddress, Length, &PagingAttribute, &PagingAttrMas= k, IsModified); + } + + ASSERT_RETURN_ERROR (Status); + ASSERT (PageTableBufferSize =3D=3D 0); + return RETURN_SUCCESS; } =20 @@ -697,11 +488,10 @@ FlushTlbForAll ( 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] PagingMode The paging mode. @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. - @param[out] IsSplitted TRUE means page table splitted. FALSE mean= s page table not splitted. =20 @retval EFI_SUCCESS The attributes were set for the memory reg= ion. @retval EFI_ACCESS_DENIED The attributes for the memory resource ran= ge specified by @@ -720,17 +510,16 @@ FlushTlbForAll ( EFI_STATUS SmmSetMemoryAttributesEx ( IN UINTN PageTableBase, - IN BOOLEAN EnablePML5Paging, + IN PAGING_MODE PagingMode, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, - IN UINT64 Attributes, - OUT BOOLEAN *IsSplitted OPTIONAL + IN UINT64 Attributes ) { EFI_STATUS Status; BOOLEAN IsModified; =20 - Status =3D ConvertMemoryPageAttributes (PageTableBase, EnablePML5Paging,= BaseAddress, Length, Attributes, TRUE, IsSplitted, &IsModified); + Status =3D ConvertMemoryPageAttributes (PageTableBase, PagingMode, BaseA= ddress, Length, Attributes, TRUE, &IsModified); if (!EFI_ERROR (Status)) { if (IsModified) { // @@ -748,11 +537,10 @@ SmmSetMemoryAttributesEx ( 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] PagingMode The paging mode. @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. - @param[out] IsSplitted TRUE means page table splitted. FALSE mean= s page table not splitted. =20 @retval EFI_SUCCESS The attributes were cleared for the memory= region. @retval EFI_ACCESS_DENIED The attributes for the memory resource ran= ge specified by @@ -771,17 +559,16 @@ SmmSetMemoryAttributesEx ( EFI_STATUS SmmClearMemoryAttributesEx ( IN UINTN PageTableBase, - IN BOOLEAN EnablePML5Paging, + IN PAGING_MODE PagingMode, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, - IN UINT64 Attributes, - OUT BOOLEAN *IsSplitted OPTIONAL + IN UINT64 Attributes ) { EFI_STATUS Status; BOOLEAN IsModified; =20 - Status =3D ConvertMemoryPageAttributes (PageTableBase, EnablePML5Paging,= BaseAddress, Length, Attributes, FALSE, IsSplitted, &IsModified); + Status =3D ConvertMemoryPageAttributes (PageTableBase, PagingMode, BaseA= ddress, Length, Attributes, FALSE, &IsModified); if (!EFI_ERROR (Status)) { if (IsModified) { // @@ -823,14 +610,10 @@ SmmSetMemoryAttributes ( IN UINT64 Attributes ) { - 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); + UINTN PageTableBase; + + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + return SmmSetMemoryAttributesEx (PageTableBase, mPagingMode, BaseAddress= , Length, Attributes); } =20 /** @@ -862,14 +645,10 @@ SmmClearMemoryAttributes ( IN UINT64 Attributes ) { - 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); + UINTN PageTableBase; + + PageTableBase =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + return SmmClearMemoryAttributesEx (PageTableBase, mPagingMode, BaseAddre= ss, Length, Attributes); } =20 /** @@ -891,7 +670,7 @@ SetShadowStack ( EFI_STATUS Status; =20 mIsShadowStack =3D TRUE; - Status =3D SmmSetMemoryAttributesEx (Cr3, m5LevelPagingNeeded, B= aseAddress, Length, EFI_MEMORY_RO, NULL); + Status =3D SmmSetMemoryAttributesEx (Cr3, mPagingMode, BaseAddre= ss, Length, EFI_MEMORY_RO); mIsShadowStack =3D FALSE; =20 return Status; @@ -915,7 +694,7 @@ SetNotPresentPage ( { EFI_STATUS Status; =20 - Status =3D SmmSetMemoryAttributesEx (Cr3, m5LevelPagingNeeded, BaseAddre= ss, Length, EFI_MEMORY_RP, NULL); + Status =3D SmmSetMemoryAttributesEx (Cr3, mPagingMode, BaseAddress, Leng= th, EFI_MEMORY_RP); return Status; } =20 @@ -1799,7 +1578,7 @@ EnablePageTableProtection ( // // Set entire pool including header, used-memory and left free-memory = as ReadOnly in SMM page table. // - ConvertMemoryPageAttributes (PageTableBase, m5LevelPagingNeeded, Addre= ss, PoolSize, EFI_MEMORY_RO, TRUE, NULL, NULL); + ConvertMemoryPageAttributes (PageTableBase, mPagingMode, Address, Pool= Size, EFI_MEMORY_RO, TRUE, NULL); Pool =3D Pool->NextPool; } while (Pool !=3D HeadPool); } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuD= xeSmm/X64/PageTbl.c index 3deb1ffd67..a25a96f68c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -1,7 +1,7 @@ /** @file Page Fault (#PF) handler for X64 processors =20 -Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2023, Intel Corporation. All rights reserved.
Copyright (c) 2017, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -353,7 +353,12 @@ SmmInitPageTable ( m1GPageTableSupport =3D Is1GPageSupport (); m5LevelPagingNeeded =3D Is5LevelPagingNeeded (); mPhysicalAddressBits =3D CalculateMaximumSupportAddress (); - PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1); + if (m5LevelPagingNeeded) { + mPagingMode =3D m1GPageTableSupport ? Paging5Level1GB : Paging5Level; + PatchInstructionX86 (gPatch5LevelPagingNeeded, TRUE, 1); + } else { + mPagingMode =3D m1GPageTableSupport ? Paging4Level1GB : Paging4Level; + } DEBUG ((DEBUG_INFO, "5LevelPaging Needed - %d\n", m5LevelPag= ingNeeded)); DEBUG ((DEBUG_INFO, "1GPageTable Support - %d\n", m1GPageTab= leSupport)); DEBUG ((DEBUG_INFO, "PcdCpuSmmRestrictedMemoryAccess - %d\n", mCpuSmmRes= trictedMemoryAccess)); --=20 2.39.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 (#103388): https://edk2.groups.io/g/devel/message/103388 Mute This Topic: https://groups.io/mt/98406587/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 19 12:46:00 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+103389+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+103389+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066219; cv=none; d=zohomail.com; s=zohoarc; b=RrspKtCILqRjxNqTmN45kUTxUQAxmFleyoiHdAGrqqilpCNBFwE/3FxwgBQOJeLrPAMh9M/imdgtLoSWjRTHKVgc0p2THWK/b/1E+91LZQdeG1Eh9z2tLm6FSFfI066GgDvXtHbsba2BfBG/HipHIQ10S0DOA5aJXJhBufqyh9o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066219; 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=PFISbYtPZUvdh4r4FH4+1ovQW5XPt0UmsaHGrXEV038=; b=mWgMmQyOKj08+NKX/t2SK+PyPqmzbRw5M/R50wTmK58kfOrQ8bQ7mkbXgIia3MEyI0zulEsz/25KWT/jdA6Bc2gZj0th3Z+OtGGd9CTpYpEnVowcQcezFC+69FyPtl5QNx36hekZTUMcKnuGVQPDzpNFRTlzgxnEnbsVCuSfMUE= 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+103389+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 1682066219720153.38428982219045; Fri, 21 Apr 2023 01:36:59 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id i2hvYY1788612xACvkhKJgrN; Fri, 21 Apr 2023 01:36:59 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7047.1682066213863432703 for ; Fri, 21 Apr 2023 01:36:58 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869722" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869722" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650472" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650472" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:56 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 05/11] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid setting non-present range to RO/NX Date: Fri, 21 Apr 2023 16:36:22 +0800 Message-Id: <20230421083628.1408-6-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: HZtWD1BVldHFASoF9pnZUO98x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066219; bh=Blh0RVuTHy0D5lUKa494EDFcimqQiNbT6uWWjoFXSEE=; h=Cc:Date:From:Reply-To:Subject:To; b=Z16E0YgTK5Wc9FQKC7iUxogFDeqjd9nGxNedJ5lb965XDyAC8nIf+csrg/9VChPOwF9 hKHrUCUzQL88ae2JWfaq8zfLhIYa/lmIHrzV20Ar+KbJjc9l6HgDH0akIjK6SJwRfiIUH 8mRorgfCvb8Xl7i9jXfs7IkBTiPWfe81/D0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066220801100006 Content-Type: text/plain; charset="utf-8" In PiSmmCpuDxeSmm code, SetMemMapAttributes() marks memory ranges in SmmMemoryAttributesTable to RO/NX. There may exist non-present range in these memory ranges. Set other attributes for a non-present range is not permitted in CpuPageTableMapLib. So add code to handle this case. Only map the present ranges in SmmMemoryAttributesTable to RO or NX. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 141 +++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index deb5895d83..89040d386e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -858,6 +858,89 @@ PatchGdtIdtMap ( ); } =20 +/** + This function remove the non-present range in [MemMapStart, MemMapLimit]= and + set [MemMapStart, NonPresentRangeStart] as MemoryAttribute in page table. + + @param MemMapStart Pointer to the start address of range. + @param MemMapLimit Limit address of range. + @param NonPresentRangeStart Start address of non-present range. + @param NonPresentRangeLimit Limit address of non-present range. + @param MemoryAttribute The bit mask of attributes to modify = for the memory region. + +**/ +VOID +RemoveNonPresentRange ( + UINT64 *MemMapStart, + UINT64 MemMapLimit, + UINT64 NonPresentRangeStart, + UINT64 NonPresentRangeLimit, + UINT64 MemoryAttribute + ) +{ + if (*MemMapStart < NonPresentRangeStart) { + SmmSetMemoryAttributes ( + *MemMapStart, + NonPresentRangeStart - *MemMapStart, + MemoryAttribute + ); + } + + *MemMapStart =3D NonPresentRangeLimit; +} + +/** + This function set [MemMapStart, MemMapLimit] to the input MemoryAttribut= e. + + @param MemMapStart Start address of range. + @param MemMapLimit Limit address of range. + @param Map Pointer to the array of Cr3 IA32_MAP_ENTRY. + @param Count Count of IA32_MAP_ENTRY in Map. + @param MemoryAttribute The bit mask of attributes to modify for the= memory region. + +**/ +VOID +SetMemMapWithNonPresentRange ( + UINT64 MemMapStart, + UINT64 MemMapLimit, + IA32_MAP_ENTRY *Map, + UINTN Count, + UINT64 MemoryAttribute + ) +{ + UINTN Index; + UINT64 NonPresentRangeStart; + + NonPresentRangeStart =3D 0; + + for (Index =3D 0; Index < Count; Index++) { + if ((Map[Index].LinearAddress > NonPresentRangeStart) && + (MemMapStart < Map[Index].LinearAddress) && (MemMapLimit > NonPres= entRangeStart)) + { + // + // [NonPresentRangeStart, Map[Index].LinearAddress] is non-present. + // + RemoveNonPresentRange (&MemMapStart, MemMapLimit, NonPresentRangeSta= rt, Map[Index].LinearAddress, MemoryAttribute); + } + + NonPresentRangeStart =3D Map[Index].LinearAddress + Map[Index].Length; + if (NonPresentRangeStart >=3D MemMapLimit) { + break; + } + } + + // + // There is no non-present in current [MemMapStart, MemMapLimit] anymore. + // + if (MemMapStart < MemMapLimit) { + SmmSetMemoryAttributes ( + MemMapStart, + MemMapLimit - MemMapStart, + MemoryAttribute + ); + } +} + /** This function sets memory attribute according to MemoryAttributesTable. **/ @@ -872,6 +955,21 @@ SetMemMapAttributes ( UINTN DescriptorSize; UINTN Index; EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN PageTable; + EFI_STATUS Status; + IA32_MAP_ENTRY *Map; + UINTN Count; + UINT64 MemoryAttribute; + + Count =3D 0; + Map =3D NULL; + PageTable =3D AsmReadCr3 (); + Status =3D PageTableParse (PageTable, mPagingMode, NULL, &Count); + ASSERT (Status =3D=3D RETURN_BUFFER_TOO_SMALL); + Map =3D AllocatePool (Count * sizeof (IA32_MAP_ENTRY)); + ASSERT (Map !=3D NULL); + Status =3D PageTableParse (PageTable, mPagingMode, Map, &Count); + ASSERT_RETURN_ERROR (Status); =20 SmmGetSystemConfigurationTable (&gEdkiiPiSmmMemoryAttributesTableGuid, (= VOID **)&MemoryAttributesTable); if (MemoryAttributesTable =3D=3D NULL) { @@ -901,33 +999,32 @@ SetMemMapAttributes ( MemoryMap =3D MemoryMapStart; for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", M= emoryMap->PhysicalStart, MemoryMap->NumberOfPages)); - switch (MemoryMap->Type) { - case EfiRuntimeServicesCode: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_RO - ); - break; - case EfiRuntimeServicesData: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_XP - ); - break; - default: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_XP - ); - break; + if (MemoryMap->Type =3D=3D EfiRuntimeServicesCode) { + MemoryAttribute =3D EFI_MEMORY_RO; + } else { + // + // Set other type memory as NX. + // + MemoryAttribute =3D EFI_MEMORY_XP; } =20 + // + // There may exist non-present range overlaps with the MemoryMap range. + // Do not change other attributes of non-present range while still rem= aining it as non-present + // + SetMemMapWithNonPresentRange ( + MemoryMap->PhysicalStart, + MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->Numb= erOfPages), + Map, + Count, + MemoryAttribute + ); + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } =20 + FreePool (Map); + PatchSmmSaveStateMap (); PatchGdtIdtMap (); =20 --=20 2.39.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 (#103389): https://edk2.groups.io/g/devel/message/103389 Mute This Topic: https://groups.io/mt/98406589/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 19 12:46:00 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+103390+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+103390+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066222; cv=none; d=zohomail.com; s=zohoarc; b=I3w1jCq6M8AzcNswOGKub3UhHzU2gVzv5qk2oBU+fQXuPt6X3XFywVs2ZX8e1+u3Cgz1w3kZesDZEP3OkI+271nop1RGB0EzLnUva6ZiC+50vTP2J+oGm+YUXWvkdalzqnVVGWZ8CGQxB57DEqJBq9YT1NM0Cdw2JpmUAfQCkyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066222; 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=kMYdqAnvQh8tZVsPpeYwMUqhOvxd2EtVttV1Li1iAw0=; b=mwZRv5dEUJwodygr+9jFpYRdxJaxWu2siiQwPD7ZyJSpQcEMLiBu8OOSbzAgAIwHRWuDbI56OheWIqxGvjQkzBUkZ8RLrcsoG0l80zyP4USiDjJvyUPHecgKpGkNlWko2GE1wGt4IED1wBZO7VTHyAcNDNiJPXxD1azZBolL5nE= 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+103390+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 1682066222030734.3105215597724; Fri, 21 Apr 2023 01:37:02 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id smnTYY1788612xbNvj0jKSo5; Fri, 21 Apr 2023 01:37:01 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.7047.1682066213863432703 for ; Fri, 21 Apr 2023 01:37:01 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869741" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869741" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650478" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650478" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:36:59 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 06/11] UefiCpuPkg: Extern mSmmShadowStackSize in PiSmmCpuDxeSmm.h Date: Fri, 21 Apr 2023 16:36:23 +0800 Message-Id: <20230421083628.1408-7-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: B0vM39UQescrGqj427vAaOxNx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066221; bh=qMMzdO5GfOonYsd88lejuYhLyBvz7F8lZPem1ar5LUs=; h=Cc:Date:From:Reply-To:Subject:To; b=n5GGKVAE2F479TBkWPrAwPjRnml2oLn9ZQYFPbj2jjm6pk5bFU7p4soV1JayqE2VDvu z9jRsmmNVlT2MZRFsqtYAN0RKxHfTN1S/iCFj5OmQ2d61TZ08j65J51mkrYpJDcR8yai+ LBFeKi0NTxZtwbALN/GlHA8O22sRd8vBL+8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066222742100009 Content-Type: text/plain; charset="utf-8" Extern mSmmShadowStackSize in PiSmmCpuDxeSmm.h and remove extern for mSmmShadowStackSize in c files to simplify code. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c | 3 +-- UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 2 -- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 1 + UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 2 -- UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c | 3 +-- 5 files changed, 3 insertions(+), 8 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c b/UefiCpuPkg/PiS= mmCpuDxeSmm/Ia32/SmmFuncsArch.c index 6c48a53f67..636dc8d92f 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c @@ -1,7 +1,7 @@ /** @file SMM CPU misc functions for Ia32 arch specific. =20 -Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2023, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -14,7 +14,6 @@ EFI_PHYSICAL_ADDRESS mGdtBuffer; UINTN mGdtBufferSize; =20 extern BOOLEAN mCetSupported; -extern UINTN mSmmShadowStackSize; =20 X86_ASSEMBLY_PATCH_LABEL mPatchCetPl0Ssp; X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSsp; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index baf827cf9d..1878252eac 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -29,8 +29,6 @@ MM_COMPLETION mSmmStartupThisApToken; // UINT32 *mPackageFirstThreadIndex =3D NULL; =20 -extern UINTN mSmmShadowStackSize; - /** Performs an atomic compare exchange operation to get semaphore. The compare exchange operation must be performed using diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index ba341cadc6..a155e09200 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -262,6 +262,7 @@ extern EFI_SMM_CPU_PROTOCOL mSmmCpu; extern EFI_MM_MP_PROTOCOL mSmmMp; extern BOOLEAN m5LevelPagingNeeded; extern PAGING_MODE mPagingMode; +extern UINTN mSmmShadowStackSize; =20 /// /// The mode of the CPU at the time an SMI occurs diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuD= xeSmm/X64/PageTbl.c index a25a96f68c..25ced50955 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -13,8 +13,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #define PAGE_TABLE_PAGES 8 #define ACC_MAX_BIT BIT3 =20 -extern UINTN mSmmShadowStackSize; - LIST_ENTRY mPagePool =3D INITIALIZE_LIST_HEAD_VAR= IABLE (mPagePool); BOOLEAN m1GPageTableSupport =3D FALSE; BOOLEAN mCpuSmmRestrictedMemoryAccess; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c b/UefiCpuPkg/PiSm= mCpuDxeSmm/X64/SmmFuncsArch.c index 00a284c369..c4f21e2155 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c @@ -1,7 +1,7 @@ /** @file SMM CPU misc functions for x64 arch specific. =20 -Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2023, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -12,7 +12,6 @@ EFI_PHYSICAL_ADDRESS mGdtBuffer; UINTN mGdtBufferSize; =20 extern BOOLEAN mCetSupported; -extern UINTN mSmmShadowStackSize; =20 X86_ASSEMBLY_PATCH_LABEL mPatchCetPl0Ssp; X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSsp; --=20 2.39.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 (#103390): https://edk2.groups.io/g/devel/message/103390 Mute This Topic: https://groups.io/mt/98406590/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 19 12:46:00 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+103391+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+103391+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066250; cv=none; d=zohomail.com; s=zohoarc; b=cn9tjW7D+GVXRzmW12aSX7ioRKKOHv68jJixkM/mWrimzd1EQ9ALkWwX+QLEEgdcHCaBPcILekPXo59RzdpQG5sx0N8aHCpqT/xnPMb7jBkiQryQrx/aVC59I8ZzPBlGeau5EBPRazBCrkAYErYsEI7Q3XJdSmXG0luT606YGfA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066250; 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=h7xyLvYDjr7pm08pDCth2Znd6CSKgLxkGxPYWoOjeW0=; b=HPbGlTPVjoCXCSwAcPfI/ZXT8gL5R0kSJV+Q4jGZnj2ZHnBeYpSgmaeDFvGmGuUBIPewOFWdn7YreLvzA0vzcpBRBP7mDOZL3xC6b6ZhVsm+W4ZC8/M+1ypBwled+bg/qAbAAYH4POlBx/PXyjv7EKDvo7vKgPPBRmmhw1IL5DY= 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+103391+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 1682066250799455.62631219829643; Fri, 21 Apr 2023 01:37:30 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id iEytYY1788612xXAgsP4TcxE; Fri, 21 Apr 2023 01:37:30 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.7066.1682066249739051879 for ; Fri, 21 Apr 2023 01:37:29 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869762" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869762" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650483" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650483" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:01 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 07/11] UefiCpuPkg/PiSmmCpuDxeSmm: Add 2 function to disable/enable CR0.WP Date: Fri, 21 Apr 2023 16:36:24 +0800 Message-Id: <20230421083628.1408-8-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: uVi3SD1UXpYSyLqt4jYGPahNx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066250; bh=7lp5PCizOCcVGhVRAk6o5qb4MYefvuMu/FpJ592QhFw=; h=Cc:Date:From:Reply-To:Subject:To; b=OZ3HdytJGN8U9AVMCOoYSSRVP05+mPczTBf0HllMx2ZrKjUP28CN7CnVAZLHuOCC1KZ mpTekvCmT7nDPSSC5Q0pyzb8H6V7mfDgDvaMizxln7AD6+C+vAF10C8kEZAiortfR3T47 OiaAxAWv/SMtTwjsk3+fmlVXKuWwXQjcLVc= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066252135100004 Content-Type: text/plain; charset="utf-8" Add two functions to disable/enable CR0.WP. These two unctions will also be used in later commits. This commit doesn't change any functionality. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 24 +++++++++++++++++= +++++++ UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 115 +++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++--------------------------= ----------------------- 2 files changed, 90 insertions(+), 49 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index a155e09200..a7da9673a5 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -1566,4 +1566,28 @@ SmmWaitForApArrival ( VOID ); =20 +/** + Disable Write Protect on pages marked as read-only if Cr0.Bits.WP is 1. + + @param[out] WpEnabled If Cr0.WP is enabled. + @param[out] CetEnabled If CET is enabled. +**/ +VOID +DisableReadOnlyPageWriteProtect ( + OUT BOOLEAN *WpEnabled, + OUT BOOLEAN *CetEnabled + ); + +/** + Enable Write Protect on pages marked as read-only. + + @param[out] WpEnabled If Cr0.WP should be enabled. + @param[out] CetEnabled If CET should be enabled. +**/ +VOID +EnableReadOnlyPageWriteProtect ( + BOOLEAN WpEnabled, + BOOLEAN CetEnabled + ); + #endif diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 89040d386e..eb3547247d 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -40,6 +40,64 @@ PAGE_TABLE_POOL *mPageTablePool =3D NULL; // BOOLEAN mIsReadOnlyPageTable =3D FALSE; =20 +/** + Disable Write Protect on pages marked as read-only if Cr0.Bits.WP is 1. + + @param[out] WpEnabled If Cr0.WP is enabled. + @param[out] CetEnabled If CET is enabled. +**/ +VOID +DisableReadOnlyPageWriteProtect ( + OUT BOOLEAN *WpEnabled, + OUT BOOLEAN *CetEnabled + ) +{ + IA32_CR0 Cr0; + + *CetEnabled =3D ((AsmReadCr4 () & CR4_CET_ENABLE) !=3D 0) ? TRUE : FALSE; + 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 cleari= ng CR0.WP. + // + DisableCet (); + } + + Cr0.Bits.WP =3D 0; + AsmWriteCr0 (Cr0.UintN); + } +} + +/** + Enable Write Protect on pages marked as read-only. + + @param[out] WpEnabled If Cr0.WP should be enabled. + @param[out] CetEnabled If CET should be enabled. +**/ +VOID +EnableReadOnlyPageWriteProtect ( + BOOLEAN WpEnabled, + BOOLEAN CetEnabled + ) +{ + IA32_CR0 Cr0; + + if (WpEnabled) { + Cr0.UintN =3D AsmReadCr0 (); + Cr0.Bits.WP =3D 1; + AsmWriteCr0 (Cr0.UintN); + + if (CetEnabled) { + // + // re-enable CET. + // + EnableCet (); + } + } +} + /** Initialize a buffer pool for page table use only. =20 @@ -62,10 +120,9 @@ InitializePageTablePool ( IN UINTN PoolPages ) { - VOID *Buffer; - BOOLEAN CetEnabled; - BOOLEAN WpEnabled; - IA32_CR0 Cr0; + VOID *Buffer; + BOOLEAN WpEnabled; + BOOLEAN CetEnabled; =20 // // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one pag= e for @@ -102,34 +159,9 @@ InitializePageTablePool ( // If page table memory has been marked as RO, mark the new pool pages a= s read-only. // if (mIsReadOnlyPageTable) { - CetEnabled =3D ((AsmReadCr4 () & CR4_CET_ENABLE) !=3D 0) ? TRUE : FALS= E; - 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. - // - DisableCet (); - } - - Cr0.Bits.WP =3D 0; - AsmWriteCr0 (Cr0.UintN); - } - + DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); SmmSetMemoryAttributes ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, EFI_PAGES= _TO_SIZE (PoolPages), EFI_MEMORY_RO); - if (WpEnabled) { - Cr0.UintN =3D AsmReadCr0 (); - Cr0.Bits.WP =3D 1; - AsmWriteCr0 (Cr0.UintN); - - if (CetEnabled) { - // - // re-enable CET. - // - EnableCet (); - } - } + EnableReadOnlyPageWriteProtect (WpEnabled, CetEnabled); } =20 return TRUE; @@ -1732,6 +1764,7 @@ SetPageTableAttributes ( VOID ) { + BOOLEAN WpEnabled; BOOLEAN CetEnabled; =20 if (!IfReadOnlyPageTableNeeded ()) { @@ -1744,15 +1777,7 @@ SetPageTableAttributes ( // Disable write protection, because we need mark page table to be write= protected. // We need *write* page table memory, to mark itself to be *read only*. // - CetEnabled =3D ((AsmReadCr4 () & CR4_CET_ENABLE) !=3D 0) ? TRUE : FALSE; - if (CetEnabled) { - // - // CET must be disabled if WP is disabled. - // - DisableCet (); - } - - AsmWriteCr0 (AsmReadCr0 () & ~CR0_WP); + DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); =20 // Set memory used by page table as Read Only. DEBUG ((DEBUG_INFO, "Start...\n")); @@ -1761,20 +1786,12 @@ SetPageTableAttributes ( // // Enable write protection, after page table attribute updated. // - AsmWriteCr0 (AsmReadCr0 () | CR0_WP); + EnableReadOnlyPageWriteProtect (TRUE, CetEnabled); mIsReadOnlyPageTable =3D TRUE; =20 // // Flush TLB after mark all page table pool as read only. // FlushTlbForAll (); - - if (CetEnabled) { - // - // re-enable CET. - // - EnableCet (); - } - return; } --=20 2.39.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 (#103391): https://edk2.groups.io/g/devel/message/103391 Mute This Topic: https://groups.io/mt/98406592/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 19 12:46:00 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+103392+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+103392+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066251; cv=none; d=zohomail.com; s=zohoarc; b=Wfjou7xKwRWaYwRaITadK+MBv91v6L9yLkJA0S8CIPXucC+umMSf9ekKS0N5k264UCCVJqEvxAcYCRAB+LskgMAB12vFlXa3RCwfZFRPprLqhd6IAK177P+3tITsYeGIiTBv+VygtUjRDjZ5tc/vlAV2S/0wxF7QW0c8iXYtcjU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066251; 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=bXbFk8zbv9aEwkYM7dEYnwA8mLOTAOJb7erWzc53pLs=; b=E9ElqoolIxoyNts1BWLaA0aW7W3b3AoXrCK2VY8FdR9aN5J+hRQ+LldlDZgjcgRFAWGYYEFsF5pACILcflrBIQ7mtdIXu3YjiMQn8Cy4VgwBW4fLml2qHEkRA8aYwmDy7h5SBfrYZUzZswEl7Lj0z04xU2ZCw7/4MHX8CRykgVc= 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+103392+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 1682066251168139.47836613444565; Fri, 21 Apr 2023 01:37:31 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id UbZIYY1788612xU0j7pFIqfI; Fri, 21 Apr 2023 01:37:30 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.7066.1682066249739051879 for ; Fri, 21 Apr 2023 01:37:30 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869766" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869766" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650489" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650489" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:04 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 08/11] UefiCpuPkg/PiSmmCpuDxeSmm: Clear CR0.WP before modify page table Date: Fri, 21 Apr 2023 16:36:25 +0800 Message-Id: <20230421083628.1408-9-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: vuZeUmNxO23Pix1HXMOA4QFPx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066250; bh=4Qqb4ttxgDN+eAZLot7eHvjWPYjrVSvFcogkZb+SEnI=; h=Cc:Date:From:Reply-To:Subject:To; b=kGy9sSHVb4VNHGXWAgw/stVo3wdvCT1Iv4EYK7D0/LCHzbMwq1h5QUSB3J81yi514d5 GA72+08set9bASL4H7PnMeiyKiZb0fs4L5HQG2URSu+GEhwaIeNZe6bhUcT5w9a59EdGw XkmS+mU4SDyMBgorf2lYmbzxwwv1f8XvcFw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066252126100003 Content-Type: text/plain; charset="utf-8" Clear CR0.WP before modify smm page table. Currently, there is an assumption that smm pagetable is always RW before ReadyToLock. However, when AMD SEV is enabled, FvbServicesSmm driver calls MemEncryptSevClearMmioPageEncMask to clear AddressEncMask bit in smm page table for this range: [PcdOvmfFdBaseAddress,PcdOvmfFdBaseAddress+PcdOvmfFirmwareFdSize] If page slpit happens in this process, new memory for smm page table is allocated. Then the newly allocated page table memory is marked as RO in smm page table in this FvbServicesSmm driver, which may lead to PF if smm code doesn't clear CR0.WP before modify smm page table when ReadyToLock. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 11 +++++++++++ UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index eb3547247d..110a8f3d81 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -992,6 +992,8 @@ SetMemMapAttributes ( IA32_MAP_ENTRY *Map; UINTN Count; UINT64 MemoryAttribute; + BOOLEAN WpEnabled; + BOOLEAN CetEnabled; =20 Count =3D 0; Map =3D NULL; @@ -1028,6 +1030,8 @@ SetMemMapAttributes ( MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } =20 + DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); + MemoryMap =3D MemoryMapStart; for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", M= emoryMap->PhysicalStart, MemoryMap->NumberOfPages)); @@ -1055,6 +1059,7 @@ SetMemMapAttributes ( MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } =20 + EnableReadOnlyPageWriteProtect (WpEnabled, CetEnabled); FreePool (Map); =20 PatchSmmSaveStateMap (); @@ -1361,9 +1366,13 @@ SetUefiMemMapAttributes ( UINTN MemoryMapEntryCount; UINTN Index; EFI_MEMORY_DESCRIPTOR *Entry; + BOOLEAN WpEnabled; + BOOLEAN CetEnabled; =20 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); =20 + DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); + if (mUefiMemoryMap !=3D NULL) { MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; MemoryMap =3D mUefiMemoryMap; @@ -1442,6 +1451,8 @@ SetUefiMemMapAttributes ( } } =20 + EnableReadOnlyPageWriteProtect (WpEnabled, CetEnabled); + // // Do not free mUefiMemoryAttributesTable, it will be checked in IsSmmCo= mmBufferForbiddenAddress(). // diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c b/UefiCpuPkg/PiSmmCpuDx= eSmm/SmmProfile.c index 1b0b6673e1..5625ba0cac 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c @@ -574,6 +574,8 @@ InitPaging ( BOOLEAN Nx; IA32_CR4 Cr4; BOOLEAN Enable5LevelPaging; + BOOLEAN WpEnabled; + BOOLEAN CetEnabled; =20 Cr4.UintN =3D AsmReadCr4 (); Enable5LevelPaging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); @@ -620,6 +622,7 @@ InitPaging ( NumberOfPdptEntries =3D 4; } =20 + DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); // // Go through page table and change 2MB-page into 4KB-page. // @@ -800,6 +803,8 @@ InitPaging ( } // end for PML4 } // end for PML5 =20 + EnableReadOnlyPageWriteProtect (WpEnabled, CetEnabled); + // // Flush TLB // --=20 2.39.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 (#103392): https://edk2.groups.io/g/devel/message/103392 Mute This Topic: https://groups.io/mt/98406593/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 19 12:46:00 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+103393+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+103393+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066251; cv=none; d=zohomail.com; s=zohoarc; b=Jqc+JBUFrp4m/0IKrwWuORx6u1w0AhSGdYKPKEhudflNztkCaZEKJ9G0aVyLaAG9ZM7/HW0sWn5qkk4hvPKbPeD+e8bhSRUoChZFrB4z2u+kFgdGd/g3J9FUWjSBeSe5BKvq6j3kuqxzID+i9l8Ybay6bASXfBrO156D/aCuKkc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066251; 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=qbQOEi8HLRxeAYJX/M0iL4tIDezAbgXl/FMgz+lYwos=; b=I4TzqxXyYgnMSG6DjHuCfA9HpP9Zpx0L5Z6JQbEgTXY8Wbi5nhtOGTdYtJaT9IRxS6ZJ0Sy525fjJb4tegYy++vWK/qPLKwgjUzFCnPrQd4fiJyNuMWM/Xu/vpiIy2wRGshD2Y2CHo2xdwsQgtdXARIsekW42eN8W7jixt6Xuzo= 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+103393+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 1682066251677551.327051659963; Fri, 21 Apr 2023 01:37:31 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id tnFpYY1788612xLquN8U8K1j; Fri, 21 Apr 2023 01:37:31 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.7066.1682066249739051879 for ; Fri, 21 Apr 2023 01:37:30 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869780" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869780" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650492" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650492" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:07 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 09/11] UefiCpuPkg: Refinement to current smm page table generation code Date: Fri, 21 Apr 2023 16:36:26 +0800 Message-Id: <20230421083628.1408-10-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: P6P0ypwe1SNmGyEfcR4cwnfpx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066251; bh=k2cc6RYYIK2Jn1YCP28fXNwUO7gcZLnWEEDyd6YvtTA=; h=Cc:Date:From:Reply-To:Subject:To; b=Z1/zkJdIl/l2/KEB+roPQPwTiNJd+8b/YTRw/wJX9sygbuj5GCeUUs9lUeX5Bdv7QI3 aJeztsKUKVEUOMS680Kqsc9+zVKoKil06+ym8DxWdh7x8V+YLCPUsaAeeYvL47A33pmNO Hmfp/5oYCPjsqX6I00F7PIsEylsD+PjJDq0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066252167100005 Content-Type: text/plain; charset="utf-8" This commit is code refinement to current smm pagetable generation code. Add a new GenSmmPageTable() API to create page table for smm based on the PageTableMap() API in CpuPageTableLib. Caller only needs to specify the paging mode and the PhysicalAddressBits to map. This function can be used to create both IA32 pae paging and X64 5level, 4level paging. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 2 +- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmProfileArch.c | 2 +- UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 130 -----------------= ---------------------------------------------------------------------------= -------------------------------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 15 +++++++++------ UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 65 +++++++++++++++++= ++++++++++++++++++++++++++++++++++++++++++++++++ UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 218 +++++++++++++++++= ++++++---------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------------------------------------- UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmProfileArch.c | 19 ++---------------= -- 7 files changed, 101 insertions(+), 350 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c b/UefiCpuPkg/PiSmmCpu= DxeSmm/Ia32/PageTbl.c index 9c8107080a..b11264ce4a 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c @@ -63,7 +63,7 @@ SmmInitPageTable ( InitializeIDTSmmStackGuard (); } =20 - return Gen4GPageTable (TRUE); + return GenSmmPageTable (PagingPae, mPhysicalAddressBits); } =20 /** diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmProfileArch.c b/UefiCpuPkg/P= iSmmCpuDxeSmm/Ia32/SmmProfileArch.c index bba4a6f058..650090e534 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmProfileArch.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmProfileArch.c @@ -18,7 +18,7 @@ InitSmmS3Cr3 ( VOID ) { - mSmmS3ResumeState->SmmS3Cr3 =3D Gen4GPageTable (TRUE); + mSmmS3ResumeState->SmmS3Cr3 =3D GenSmmPageTable (PagingPae, mPhysicalAdd= ressBits); =20 return; } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 1878252eac..f8b81fc96e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -999,136 +999,6 @@ APHandler ( ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); } =20 -/** - Create 4G PageTable in SMRAM. - - @param[in] Is32BitPageTable Whether the page table is 32-bit PAE - @return PageTable Address - -**/ -UINT32 -Gen4GPageTable ( - IN BOOLEAN Is32BitPageTable - ) -{ - VOID *PageTable; - UINTN Index; - UINT64 *Pte; - UINTN PagesNeeded; - UINTN Low2MBoundary; - UINTN High2MBoundary; - UINTN Pages; - UINTN GuardPage; - UINT64 *Pdpte; - UINTN PageIndex; - UINTN PageAddress; - - Low2MBoundary =3D 0; - High2MBoundary =3D 0; - PagesNeeded =3D 0; - if (FeaturePcdGet (PcdCpuSmmStackGuard)) { - // - // Add one more page for known good stack, then find the lower 2MB ali= gned address. - // - Low2MBoundary =3D (mSmmStackArrayBase + EFI_PAGE_SIZE) & ~(SIZE_2MB-1); - // - // Add two more pages for known good stack and stack guard page, - // then find the lower 2MB aligned address. - // - High2MBoundary =3D (mSmmStackArrayEnd - mSmmStackSize - mSmmShadowStac= kSize + EFI_PAGE_SIZE * 2) & ~(SIZE_2MB-1); - PagesNeeded =3D ((High2MBoundary - Low2MBoundary) / SIZE_2MB) + 1; - } - - // - // Allocate the page table - // - PageTable =3D AllocatePageTableMemory (5 + PagesNeeded); - ASSERT (PageTable !=3D NULL); - - PageTable =3D (VOID *)((UINTN)PageTable); - Pte =3D (UINT64 *)PageTable; - - // - // Zero out all page table entries first - // - ZeroMem (Pte, EFI_PAGES_TO_SIZE (1)); - - // - // Set Page Directory Pointers - // - for (Index =3D 0; Index < 4; Index++) { - Pte[Index] =3D ((UINTN)PageTable + EFI_PAGE_SIZE * (Index + 1)) | mAdd= ressEncMask | - (Is32BitPageTable ? IA32_PAE_PDPTE_ATTRIBUTE_BITS : PAGE_= ATTRIBUTE_BITS); - } - - Pte +=3D EFI_PAGE_SIZE / sizeof (*Pte); - - // - // Fill in Page Directory Entries - // - for (Index =3D 0; Index < EFI_PAGE_SIZE * 4 / sizeof (*Pte); Index++) { - Pte[Index] =3D (Index << 21) | mAddressEncMask | IA32_PG_PS | PAGE_ATT= RIBUTE_BITS; - } - - Pdpte =3D (UINT64 *)PageTable; - if (FeaturePcdGet (PcdCpuSmmStackGuard)) { - Pages =3D (UINTN)PageTable + EFI_PAGES_TO_SIZE (5); - GuardPage =3D mSmmStackArrayBase + EFI_PAGE_SIZE; - for (PageIndex =3D Low2MBoundary; PageIndex <=3D High2MBoundary; PageI= ndex +=3D SIZE_2MB) { - Pte =3D (UINT64 *)(UINTN= )(Pdpte[BitFieldRead32 ((UINT32)PageIndex, 30, 31)] & ~mAddressEncMask & ~(= EFI_PAGE_SIZE - 1)); - Pte[BitFieldRead32 ((UINT32)PageIndex, 21, 29)] =3D (UINT64)Pages | = mAddressEncMask | PAGE_ATTRIBUTE_BITS; - // - // Fill in Page Table Entries - // - Pte =3D (UINT64 *)Pages; - PageAddress =3D PageIndex; - for (Index =3D 0; Index < EFI_PAGE_SIZE / sizeof (*Pte); Index++) { - if (PageAddress =3D=3D GuardPage) { - // - // Mark the guard page as non-present - // - Pte[Index] =3D PageAddress | mAddressEncMask; - GuardPage +=3D (mSmmStackSize + mSmmShadowStackSize); - if (GuardPage > mSmmStackArrayEnd) { - GuardPage =3D 0; - } - } else { - Pte[Index] =3D PageAddress | mAddressEncMask | PAGE_ATTRIBUTE_BI= TS; - } - - PageAddress +=3D EFI_PAGE_SIZE; - } - - Pages +=3D EFI_PAGE_SIZE; - } - } - - if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT1) !=3D 0) { - Pte =3D (UINT64 *)(UINTN)(Pdpte[0] & ~mAddressEncMask & ~(EFI_PAGE_SIZ= E - 1)); - if ((Pte[0] & IA32_PG_PS) =3D=3D 0) { - // 4K-page entries are already mapped. Just hide the first one anywa= y. - Pte =3D (UINT64 *)(UINTN)(Pte[0] & ~mAddressEncMask & ~(EFI_PAGE= _SIZE - 1)); - Pte[0] &=3D ~(UINT64)IA32_PG_P; // Hide page 0 - } else { - // Create 4K-page entries - Pages =3D (UINTN)AllocatePageTableMemory (1); - ASSERT (Pages !=3D 0); - - Pte[0] =3D (UINT64)(Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS); - - Pte =3D (UINT64 *)Pages; - PageAddress =3D 0; - Pte[0] =3D PageAddress | mAddressEncMask; // Hide page 0 but pr= esent left - for (Index =3D 1; Index < EFI_PAGE_SIZE / sizeof (*Pte); Index++) { - PageAddress +=3D EFI_PAGE_SIZE; - Pte[Index] =3D PageAddress | mAddressEncMask | PAGE_ATTRIBUTE_BI= TS; - } - } - } - - return (UINT32)(UINTN)PageTable; -} - /** Checks whether the input token is the current used token. =20 diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index a7da9673a5..258955119c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -542,15 +542,18 @@ extern UINT8 mPhysicalAddress= Bits; extern UINT64 mAddressEncMask; =20 /** - Create 4G PageTable in SMRAM. + Create page table based on input PagingMode and PhysicalAddressBits in s= mm. =20 - @param[in] Is32BitPageTable Whether the page table is 32-bit PAE - @return PageTable Address + @param[in] PagingMode The paging mode. + @param[in] PhysicalAddressBits The bits of physical address to map. + + @retval PageTable Address =20 **/ -UINT32 -Gen4GPageTable ( - IN BOOLEAN Is32BitPageTable +UINTN +GenSmmPageTable ( + IN PAGING_MODE PagingMode, + IN UINT8 PhysicalAddressBits ); =20 /** diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 110a8f3d81..9a0619ce51 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -1592,6 +1592,71 @@ EdkiiSmmClearMemoryAttributes ( return SmmClearMemoryAttributes (BaseAddress, Length, Attributes); } =20 +/** + Create page table based on input PagingMode and PhysicalAddressBits in s= mm. + + @param[in] PagingMode The paging mode. + @param[in] PhysicalAddressBits The bits of physical address to map. + + @retval PageTable Address + +**/ +UINTN +GenSmmPageTable ( + IN PAGING_MODE PagingMode, + IN UINT8 PhysicalAddressBits + ) +{ + UINTN PageTableBufferSize; + UINTN PageTable; + VOID *PageTableBuffer; + IA32_MAP_ATTRIBUTE MapAttribute; + IA32_MAP_ATTRIBUTE MapMask; + RETURN_STATUS Status; + UINTN GuardPage; + UINTN Index; + UINT64 Length; + + Length =3D LShiftU64 (1, PhysicalAddressBits); + PageTable =3D 0; + PageTableBufferSize =3D 0; + MapMask.Uint64 =3D MAX_UINT64; + MapAttribute.Uint64 =3D mAddressEncMask; + MapAttribute.Bits.Present =3D 1; + MapAttribute.Bits.ReadWrite =3D 1; + MapAttribute.Bits.UserSupervisor =3D 1; + MapAttribute.Bits.Accessed =3D 1; + MapAttribute.Bits.Dirty =3D 1; + + Status =3D PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferS= ize, 0, Length, &MapAttribute, &MapMask, NULL); + ASSERT (Status =3D=3D RETURN_BUFFER_TOO_SMALL); + DEBUG ((DEBUG_INFO, "GenSMMPageTable: 0x%x bytes needed for initial SMM = page table\n", PageTableBufferSize)); + PageTableBuffer =3D AllocatePageTableMemory (EFI_SIZE_TO_PAGES (PageTabl= eBufferSize)); + ASSERT (PageTableBuffer !=3D NULL); + Status =3D PageTableMap (&PageTable, PagingMode, PageTableBuffer, &PageT= ableBufferSize, 0, Length, &MapAttribute, &MapMask, NULL); + ASSERT (Status =3D=3D RETURN_SUCCESS); + ASSERT (PageTableBufferSize =3D=3D 0); + + if (FeaturePcdGet (PcdCpuSmmStackGuard)) { + // + // Mark the guard page at the bottom of smm stack as non-present + // + for (Index =3D 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOf= Cpus; Index++) { + GuardPage =3D mSmmStackArrayBase + EFI_PAGE_SIZE + Index * (mSmmStac= kSize + mSmmShadowStackSize); + Status =3D ConvertMemoryPageAttributes (PageTable, PagingMode, Gu= ardPage, SIZE_4KB, EFI_MEMORY_RP, TRUE, NULL); + } + } + + if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT1) !=3D 0) { + // + // Mark [0, 4k] as non-present + // + Status =3D ConvertMemoryPageAttributes (PageTable, PagingMode, 0, SIZE= _4KB, EFI_MEMORY_RP, TRUE, NULL); + } + + return (UINTN)PageTable; +} + /** This function retrieves the attributes of the memory region specified by BaseAddress and Length. If different attributes are got from different p= art diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuD= xeSmm/X64/PageTbl.c index 25ced50955..cdbf52ae77 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -167,160 +167,6 @@ CalculateMaximumSupportAddress ( return PhysicalAddressBits; } =20 -/** - Set static page table. - - @param[in] PageTable Address of page table. - @param[in] PhysicalAddressBits The maximum physical address bits supp= orted. -**/ -VOID -SetStaticPageTable ( - IN UINTN PageTable, - IN UINT8 PhysicalAddressBits - ) -{ - UINT64 PageAddress; - UINTN NumberOfPml5EntriesNeeded; - UINTN NumberOfPml4EntriesNeeded; - UINTN NumberOfPdpEntriesNeeded; - UINTN IndexOfPml5Entries; - UINTN IndexOfPml4Entries; - UINTN IndexOfPdpEntries; - UINTN IndexOfPageDirectoryEntries; - UINT64 *PageMapLevel5Entry; - UINT64 *PageMapLevel4Entry; - UINT64 *PageMap; - UINT64 *PageDirectoryPointerEntry; - UINT64 *PageDirectory1GEntry; - UINT64 *PageDirectoryEntry; - - // - // IA-32e paging translates 48-bit linear addresses to 52-bit physical a= ddresses - // when 5-Level Paging is disabled. - // - ASSERT (PhysicalAddressBits <=3D 52); - if (!m5LevelPagingNeeded && (PhysicalAddressBits > 48)) { - PhysicalAddressBits =3D 48; - } - - NumberOfPml5EntriesNeeded =3D 1; - if (PhysicalAddressBits > 48) { - NumberOfPml5EntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBits= - 48); - PhysicalAddressBits =3D 48; - } - - NumberOfPml4EntriesNeeded =3D 1; - if (PhysicalAddressBits > 39) { - NumberOfPml4EntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBits= - 39); - PhysicalAddressBits =3D 39; - } - - NumberOfPdpEntriesNeeded =3D 1; - ASSERT (PhysicalAddressBits > 30); - NumberOfPdpEntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBits - = 30); - - // - // By architecture only one PageMapLevel4 exists - so lets allocate stor= age for it. - // - PageMap =3D (VOID *)PageTable; - - PageMapLevel4Entry =3D PageMap; - PageMapLevel5Entry =3D NULL; - if (m5LevelPagingNeeded) { - // - // By architecture only one PageMapLevel5 exists - so lets allocate st= orage for it. - // - PageMapLevel5Entry =3D PageMap; - } - - PageAddress =3D 0; - - for ( IndexOfPml5Entries =3D 0 - ; IndexOfPml5Entries < NumberOfPml5EntriesNeeded - ; IndexOfPml5Entries++, PageMapLevel5Entry++) - { - // - // Each PML5 entry points to a page of PML4 entires. - // So lets allocate space for them and fill them in in the IndexOfPml4= Entries loop. - // When 5-Level Paging is disabled, below allocation happens only once. - // - if (m5LevelPagingNeeded) { - PageMapLevel4Entry =3D (UINT64 *)((*PageMapLevel5Entry) & ~mAddressE= ncMask & gPhyMask); - if (PageMapLevel4Entry =3D=3D NULL) { - PageMapLevel4Entry =3D AllocatePageTableMemory (1); - ASSERT (PageMapLevel4Entry !=3D NULL); - ZeroMem (PageMapLevel4Entry, EFI_PAGES_TO_SIZE (1)); - - *PageMapLevel5Entry =3D (UINT64)(UINTN)PageMapLevel4Entry | mAddre= ssEncMask | PAGE_ATTRIBUTE_BITS; - } - } - - for (IndexOfPml4Entries =3D 0; IndexOfPml4Entries < (NumberOfPml5Entri= esNeeded =3D=3D 1 ? NumberOfPml4EntriesNeeded : 512); IndexOfPml4Entries++,= PageMapLevel4Entry++) { - // - // Each PML4 entry points to a page of Page Directory Pointer entrie= s. - // - PageDirectoryPointerEntry =3D (UINT64 *)((*PageMapLevel4Entry) & ~mA= ddressEncMask & gPhyMask); - if (PageDirectoryPointerEntry =3D=3D NULL) { - PageDirectoryPointerEntry =3D AllocatePageTableMemory (1); - ASSERT (PageDirectoryPointerEntry !=3D NULL); - ZeroMem (PageDirectoryPointerEntry, EFI_PAGES_TO_SIZE (1)); - - *PageMapLevel4Entry =3D (UINT64)(UINTN)PageDirectoryPointerEntry |= mAddressEncMask | PAGE_ATTRIBUTE_BITS; - } - - if (m1GPageTableSupport) { - PageDirectory1GEntry =3D PageDirectoryPointerEntry; - for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntrie= s < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress= +=3D SIZE_1GB) { - if ((IndexOfPml4Entries =3D=3D 0) && (IndexOfPageDirectoryEntrie= s < 4)) { - // - // Skip the < 4G entries - // - continue; - } - - // - // Fill in the Page Directory entries - // - *PageDirectory1GEntry =3D PageAddress | mAddressEncMask | IA32_P= G_PS | PAGE_ATTRIBUTE_BITS; - } - } else { - PageAddress =3D BASE_4GB; - for (IndexOfPdpEntries =3D 0; IndexOfPdpEntries < (NumberOfPml4Ent= riesNeeded =3D=3D 1 ? NumberOfPdpEntriesNeeded : 512); IndexOfPdpEntries++,= PageDirectoryPointerEntry++) { - if ((IndexOfPml4Entries =3D=3D 0) && (IndexOfPdpEntries < 4)) { - // - // Skip the < 4G entries - // - continue; - } - - // - // Each Directory Pointer entries points to a page of Page Direc= tory entires. - // So allocate space for them and fill them in in the IndexOfPag= eDirectoryEntries loop. - // - PageDirectoryEntry =3D (UINT64 *)((*PageDirectoryPointerEntry) &= ~mAddressEncMask & gPhyMask); - if (PageDirectoryEntry =3D=3D NULL) { - PageDirectoryEntry =3D AllocatePageTableMemory (1); - ASSERT (PageDirectoryEntry !=3D NULL); - ZeroMem (PageDirectoryEntry, EFI_PAGES_TO_SIZE (1)); - - // - // Fill in a Page Directory Pointer Entries - // - *PageDirectoryPointerEntry =3D (UINT64)(UINTN)PageDirectoryEnt= ry | mAddressEncMask | PAGE_ATTRIBUTE_BITS; - } - - for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntr= ies < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress= +=3D SIZE_2MB) { - // - // Fill in the Page Directory entries - // - *PageDirectoryEntry =3D PageAddress | mAddressEncMask | IA32_P= G_PS | PAGE_ATTRIBUTE_BITS; - } - } - } - } - } -} - /** Create PageTable for SMM use. =20 @@ -332,15 +178,16 @@ SmmInitPageTable ( VOID ) { - EFI_PHYSICAL_ADDRESS Pages; - UINT64 *PTEntry; + UINTN PageTable; LIST_ENTRY *FreePage; UINTN Index; UINTN PageFaultHandlerHookAddress; IA32_IDT_GATE_DESCRIPTOR *IdtEntry; EFI_STATUS Status; + UINT64 *Pml3Entry; UINT64 *Pml4Entry; UINT64 *Pml5Entry; + UINT8 PhysicalAddressBits; =20 // // Initialize spin lock @@ -357,59 +204,40 @@ SmmInitPageTable ( } else { mPagingMode =3D m1GPageTableSupport ? Paging4Level1GB : Paging4Level; } + DEBUG ((DEBUG_INFO, "5LevelPaging Needed - %d\n", m5LevelPag= ingNeeded)); DEBUG ((DEBUG_INFO, "1GPageTable Support - %d\n", m1GPageTab= leSupport)); DEBUG ((DEBUG_INFO, "PcdCpuSmmRestrictedMemoryAccess - %d\n", mCpuSmmRes= trictedMemoryAccess)); DEBUG ((DEBUG_INFO, "PhysicalAddressBits - %d\n", mPhysicalA= ddressBits)); - // - // Generate PAE page table for the first 4GB memory space - // - Pages =3D Gen4GPageTable (FALSE); =20 // - // Set IA32_PG_PMNT bit to mask this entry + // Generate initial SMM page table // - PTEntry =3D (UINT64 *)(UINTN)Pages; - for (Index =3D 0; Index < 4; Index++) { - PTEntry[Index] |=3D IA32_PG_PMNT; - } + PhysicalAddressBits =3D mCpuSmmRestrictedMemoryAccess ? mPhysicalAddress= Bits : 32; + PageTable =3D GenSmmPageTable (mPagingMode, PhysicalAddressBit= s); =20 - // - // Fill Page-Table-Level4 (PML4) entry - // - Pml4Entry =3D (UINT64 *)AllocatePageTableMemory (1); - ASSERT (Pml4Entry !=3D NULL); - *Pml4Entry =3D Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS; - ZeroMem (Pml4Entry + 1, EFI_PAGE_SIZE - sizeof (*Pml4Entry)); + if (m5LevelPagingNeeded) { + Pml5Entry =3D (UINT64 *)PageTable; + SetSubEntriesNum (Pml5Entry, 1); + Pml4Entry =3D (UINT64 *)((*Pml5Entry) & ~mAddressEncMask & gPhyMask); + } else { + Pml4Entry =3D (UINT64 *)PageTable; + } =20 // - // Set sub-entries number + // Set IA32_PG_PMNT bit to mask first 4 Pml3Entry entry // - SetSubEntriesNum (Pml4Entry, 3); - PTEntry =3D Pml4Entry; - - if (m5LevelPagingNeeded) { - // - // Fill PML5 entry - // - Pml5Entry =3D (UINT64 *)AllocatePageTableMemory (1); - ASSERT (Pml5Entry !=3D NULL); - *Pml5Entry =3D (UINTN)Pml4Entry | mAddressEncMask | PAGE_ATTRIBUTE_BIT= S; - ZeroMem (Pml5Entry + 1, EFI_PAGE_SIZE - sizeof (*Pml5Entry)); - // - // Set sub-entries number - // - SetSubEntriesNum (Pml5Entry, 1); - PTEntry =3D Pml5Entry; + Pml3Entry =3D (UINT64 *)((*Pml4Entry) & ~mAddressEncMask & gPhyMask); + for (Index =3D 0; Index < 4; Index++) { + Pml3Entry[Index] |=3D IA32_PG_PMNT; } =20 - if (mCpuSmmRestrictedMemoryAccess) { + if (!mCpuSmmRestrictedMemoryAccess) { // - // When access to non-SMRAM memory is restricted, create page table - // that covers all memory space. + // Set Pml4Entry sub-entries number // - SetStaticPageTable ((UINTN)PTEntry, mPhysicalAddressBits); - } else { + SetSubEntriesNum (Pml4Entry, 3); + // // Add pages to page pool // @@ -466,7 +294,7 @@ SmmInitPageTable ( // // Return the address of PML4/PML5 (to set CR3) // - return (UINT32)(UINTN)PTEntry; + return (UINT32)PageTable; } =20 /** diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmProfileArch.c b/UefiCpuPkg/Pi= SmmCpuDxeSmm/X64/SmmProfileArch.c index cb7a691745..0805b2e780 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmProfileArch.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmProfileArch.c @@ -35,26 +35,11 @@ InitSmmS3Cr3 ( VOID ) { - EFI_PHYSICAL_ADDRESS Pages; - UINT64 *PTEntry; - - // - // Generate PAE page table for the first 4GB memory space - // - Pages =3D Gen4GPageTable (FALSE); - - // - // Fill Page-Table-Level4 (PML4) entry - // - PTEntry =3D (UINT64 *)AllocatePageTableMemory (1); - ASSERT (PTEntry !=3D NULL); - *PTEntry =3D Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS; - ZeroMem (PTEntry + 1, EFI_PAGE_SIZE - sizeof (*PTEntry)); - // + // Generate level4 page table for the first 4GB memory space // Return the address of PML4 (to set CR3) // - mSmmS3ResumeState->SmmS3Cr3 =3D (UINT32)(UINTN)PTEntry; + mSmmS3ResumeState->SmmS3Cr3 =3D (UINT32)GenSmmPageTable (Paging4Level, 3= 2); =20 return; } --=20 2.39.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 (#103393): https://edk2.groups.io/g/devel/message/103393 Mute This Topic: https://groups.io/mt/98406594/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 19 12:46:00 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+103394+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+103394+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066252; cv=none; d=zohomail.com; s=zohoarc; b=h2dwbZAa5G2FXSrGgyHRukhnaYM7pGamrslG2NpcfXQjMcZRw0XnxRPm/o0j1RL4nhYoFdH++aj+C5QVoEQxyEagNg+ztD1IjTjqHp1Ydq9hG13iAHZ/BjOKa0+UIp7Yo+8sWr+tIhR6v44+cd6nEDvBta6e8auD4LmEKMgponE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066252; 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=tw2ER2E641MfsdaNAmvbOzi1vzh0yzQw6OlETYlGDO4=; b=Rd9SajBgc/CBYWSnyGJUywfoDaTX3gjJizF322/feLosWtWZOW7bg0DouUX8za3GP+u6WkAwyesI1RSlrYt+0XIFP9+Vp7BI5XAyzHRV/sn0NnwiHAjauJxGMGFtMXWltWgd+i31CogpcpA7GKfsTzB/T4i0cSKGd1oHV273pc8= 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+103394+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 1682066252586356.77239700289977; Fri, 21 Apr 2023 01:37:32 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id vLRlYY1788612xDMqEA8uki0; Fri, 21 Apr 2023 01:37:32 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.7066.1682066249739051879 for ; Fri, 21 Apr 2023 01:37:31 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869787" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869787" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650496" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650496" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:10 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 10/11] UefiCpuPkg: Refinement to code about updating smm page table Date: Fri, 21 Apr 2023 16:36:27 +0800 Message-Id: <20230421083628.1408-11-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: bdjXylRnUnIo9G9nTVg5ZhLcx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066252; bh=zB06Ow6SxEQqlDOP0qxiRObURVpv0/uqVzS4cm8pM+g=; h=Cc:Date:From:Reply-To:Subject:To; b=xZKxR5/PeCcgCe5D9SMetgP+6xKalEufUIeD0+vGbNlHlFz9x/AXrAYfxe7Krk5RWi2 6TeZur+j+rzdFPxqJ0PUh332/JBZCp618rrCp1gRl6q7UMq+0Pml415tca+wb7Mhqao0c kltvgHHGb/H+QkGP3x/nfM2+PMY+RO4SgYw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066254404100018 Content-Type: text/plain; charset="utf-8" This commit is code refinement to current smm runtime pagetable update code. In InitPaging() function, sort mProtectionMemRange or mSmmCpuSmramRanges at first. If PcdCpuSmmProfileEnable is TRUE, use ConvertMemoryPageAttributes() API to map the range in mProtectionMemRange to the attrbute recorded in the attribute field of mProtectionMemRange, map the range outside mProtectionMemRange as non-present. If PcdCpuSmmProfileEnable is FALSE, set the ranges not in mSmmCpuSmramRanges as NX. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 37 +++++++++++++++++++++++++= ++++++++++++ UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c | 352 +++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------------- 2 files changed, 164 insertions(+), 225 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 258955119c..37366d6803 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -713,6 +713,43 @@ SmmBlockingStartupThisAp ( IN OUT VOID *ProcArguments OPTIONAL ); =20 +/** + This function modifies the page attributes for the memory region specifi= ed by BaseAddress and + Length from their current attributes to the attributes specified by Attr= ibutes. + + Caller should make sure BaseAddress and Length is at page boundary. + + @param[in] PageTableBase The page table base. + @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. + @param[in] IsSet TRUE means to set attributes. FALSE means = to clear attributes. + @param[out] IsModified TRUE means page table modified. FALSE mean= s page table not modified. + + @retval RETURN_SUCCESS The attributes were modified for the me= mory region. + @retval RETURN_ACCESS_DENIED The attributes for the memory resource = range specified by + BaseAddress and Length cannot be modifi= ed. + @retval RETURN_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combina= tion of attributes that + cannot be set together. + @retval RETURN_OUT_OF_RESOURCES There are not enough system resources t= o modify the attributes of + the memory resource range. + @retval RETURN_UNSUPPORTED The processor does not support one or m= ore bytes of the memory + resource range specified by BaseAddress= and Length. + The bit mask of attributes is not suppo= rt for the memory resource + range specified by BaseAddress and Leng= th. +**/ +RETURN_STATUS +ConvertMemoryPageAttributes ( + IN UINTN PageTableBase, + IN PAGING_MODE PagingMode, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes, + IN BOOLEAN IsSet, + OUT BOOLEAN *IsModified 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. diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c b/UefiCpuPkg/PiSmmCpuDx= eSmm/SmmProfile.c index 5625ba0cac..0fc0985837 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c @@ -546,262 +546,164 @@ InitProtectedMemRange ( } =20 /** - Update page table according to protected memory ranges and the 4KB-page = mapped memory ranges. + Function to compare 2 MEMORY_PROTECTION_RANGE based on range base. + + @param[in] Buffer1 pointer to Device Path poiner to compare + @param[in] Buffer2 pointer to second DevicePath pointer to co= mpare =20 + @retval 0 Buffer1 equal to Buffer2 + @retval <0 Buffer1 is less than Buffer2 + @retval >0 Buffer1 is greater than Buffer2 **/ -VOID -InitPaging ( - VOID +INTN +EFIAPI +ProtectionRangeCompare ( + IN CONST VOID *Buffer1, + IN CONST VOID *Buffer2 ) { - UINT64 Pml5Entry; - UINT64 Pml4Entry; - UINT64 *Pml5; - UINT64 *Pml4; - UINT64 *Pdpt; - UINT64 *Pd; - UINT64 *Pt; - UINTN Address; - UINTN Pml5Index; - UINTN Pml4Index; - UINTN PdptIndex; - UINTN PdIndex; - UINTN PtIndex; - UINTN NumberOfPdptEntries; - UINTN NumberOfPml4Entries; - UINTN NumberOfPml5Entries; - UINTN SizeOfMemorySpace; - BOOLEAN Nx; - IA32_CR4 Cr4; - BOOLEAN Enable5LevelPaging; - BOOLEAN WpEnabled; - BOOLEAN CetEnabled; + if (((MEMORY_PROTECTION_RANGE *)Buffer1)->Range.Base > ((MEMORY_PROTECTI= ON_RANGE *)Buffer2)->Range.Base) { + return 1; + } else if (((MEMORY_PROTECTION_RANGE *)Buffer1)->Range.Base < ((MEMORY_P= ROTECTION_RANGE *)Buffer2)->Range.Base) { + return -1; + } =20 - Cr4.UintN =3D AsmReadCr4 (); - Enable5LevelPaging =3D (BOOLEAN)(Cr4.Bits.LA57 =3D=3D 1); + return 0; +} =20 - if (sizeof (UINTN) =3D=3D sizeof (UINT64)) { - if (!Enable5LevelPaging) { - Pml5Entry =3D (UINTN)mSmmProfileCr3 | IA32_PG_P; - Pml5 =3D &Pml5Entry; - } else { - Pml5 =3D (UINT64 *)(UINTN)mSmmProfileCr3; - } +/** + Function to compare 2 EFI_SMRAM_DESCRIPTOR based on CpuStart. =20 - SizeOfMemorySpace =3D HighBitSet64 (gPhyMask) + 1; - ASSERT (SizeOfMemorySpace <=3D 52); + @param[in] Buffer1 pointer to Device Path poiner to compare + @param[in] Buffer2 pointer to second DevicePath pointer to co= mpare =20 - // - // Calculate the table entries of PML5E, PML4E and PDPTE. - // - NumberOfPml5Entries =3D 1; - if (SizeOfMemorySpace > 48) { - if (Enable5LevelPaging) { - NumberOfPml5Entries =3D (UINTN)LShiftU64 (1, SizeOfMemorySpace - 4= 8); - } + @retval 0 Buffer1 equal to Buffer2 + @retval <0 Buffer1 is less than Buffer2 + @retval >0 Buffer1 is greater than Buffer2 +**/ +INTN +EFIAPI +CpuSmramRangeCompare ( + IN CONST VOID *Buffer1, + IN CONST VOID *Buffer2 + ) +{ + if (((EFI_SMRAM_DESCRIPTOR *)Buffer1)->CpuStart > ((EFI_SMRAM_DESCRIPTOR= *)Buffer2)->CpuStart) { + return 1; + } else if (((EFI_SMRAM_DESCRIPTOR *)Buffer1)->CpuStart < ((EFI_SMRAM_DES= CRIPTOR *)Buffer2)->CpuStart) { + return -1; + } =20 - SizeOfMemorySpace =3D 48; - } + return 0; +} =20 - NumberOfPml4Entries =3D 1; - if (SizeOfMemorySpace > 39) { - NumberOfPml4Entries =3D (UINTN)LShiftU64 (1, SizeOfMemorySpace - 39); - SizeOfMemorySpace =3D 39; - } +/** + Update page table according to protected memory ranges and the 4KB-page = mapped memory ranges. =20 - NumberOfPdptEntries =3D 1; - ASSERT (SizeOfMemorySpace > 30); - NumberOfPdptEntries =3D (UINTN)LShiftU64 (1, SizeOfMemorySpace - 30); +**/ +VOID +InitPaging ( + VOID + ) +{ + RETURN_STATUS Status; + UINTN Index; + UINTN PageTable; + UINT64 Base; + UINT64 Length; + UINT64 Limit; + UINT64 PreviousAddress; + UINT64 MemoryAttrMask; + VOID *Buffer; + BOOLEAN WpEnabled; + BOOLEAN CetEnabled; + + PageTable =3D AsmReadCr3 (); + if (sizeof (UINTN) =3D=3D sizeof (UINT32)) { + Limit =3D BASE_4GB; } else { - Pml4Entry =3D (UINTN)mSmmProfileCr3 | IA32_PG_P; - Pml4 =3D &Pml4Entry; - Pml5Entry =3D (UINTN)Pml4 | IA32_PG_P; - Pml5 =3D &Pml5Entry; - NumberOfPml5Entries =3D 1; - NumberOfPml4Entries =3D 1; - NumberOfPdptEntries =3D 4; + Limit =3D (IsRestrictedMemoryAccess ()) ? LShiftU64 (1, mPhysicalAddre= ssBits) : BASE_4GB; } =20 DisableReadOnlyPageWriteProtect (&WpEnabled, &CetEnabled); // - // Go through page table and change 2MB-page into 4KB-page. + // [0, 4k] may be non-present. // - for (Pml5Index =3D 0; Pml5Index < NumberOfPml5Entries; Pml5Index++) { - if ((Pml5[Pml5Index] & IA32_PG_P) =3D=3D 0) { - // - // If PML5 entry does not exist, skip it - // - continue; - } + PreviousAddress =3D ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BI= T1) !=3D 0) ? BASE_4KB : 0; =20 - Pml4 =3D (UINT64 *)(UINTN)(Pml5[Pml5Index] & PHYSICAL_ADDRESS_MASK); - for (Pml4Index =3D 0; Pml4Index < NumberOfPml4Entries; Pml4Index++) { - if ((Pml4[Pml4Index] & IA32_PG_P) =3D=3D 0) { - // - // If PML4 entry does not exist, skip it - // - continue; + DEBUG ((DEBUG_INFO, "Patch page table start ...\n")); + if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + // + // Sort the mProtectionMemRange + // + Buffer =3D AllocateZeroPool (sizeof (MEMORY_PROTECTION_RANGE)); + ASSERT (Buffer !=3D NULL); + QuickSort (mProtectionMemRange, mProtectionMemRangeCount, sizeof (MEMO= RY_PROTECTION_RANGE), (BASE_SORT_COMPARE)ProtectionRangeCompare, Buffer); + for (Index =3D 0; Index < mProtectionMemRangeCount; Index++) { + MemoryAttrMask =3D 0; + if ((mProtectionMemRange[Index].Nx =3D=3D 1) && mXdSupported) { + MemoryAttrMask |=3D EFI_MEMORY_XP; } =20 - Pdpt =3D (UINT64 *)(UINTN)(Pml4[Pml4Index] & ~mAddressEncMask & PHYS= ICAL_ADDRESS_MASK); - for (PdptIndex =3D 0; PdptIndex < NumberOfPdptEntries; PdptIndex++, = Pdpt++) { - if ((*Pdpt & IA32_PG_P) =3D=3D 0) { - // - // If PDPT entry does not exist, skip it - // - continue; - } - - if ((*Pdpt & IA32_PG_PS) !=3D 0) { - // - // This is 1G entry, skip it - // - continue; - } - - Pd =3D (UINT64 *)(UINTN)(*Pdpt & ~mAddressEncMask & PHYSICAL_ADDRE= SS_MASK); - if (Pd =3D=3D 0) { - continue; - } - - for (PdIndex =3D 0; PdIndex < SIZE_4KB / sizeof (*Pd); PdIndex++, = Pd++) { - if ((*Pd & IA32_PG_P) =3D=3D 0) { - // - // If PD entry does not exist, skip it - // - continue; - } - - Address =3D (UINTN)LShiftU64 ( - LShiftU64 ( - LShiftU64 ((Pml5Index << 9) + Pml4Index, 9)= + PdptIndex, - 9 - ) + PdIndex, - 21 - ); - - // - // If it is 2M page, check IsAddressSplit() - // - if (((*Pd & IA32_PG_PS) !=3D 0) && IsAddressSplit (Address)) { - // - // Based on current page table, create 4KB page table for spli= t area. - // - ASSERT (Address =3D=3D (*Pd & PHYSICAL_ADDRESS_MASK)); - - Pt =3D AllocatePageTableMemory (1); - ASSERT (Pt !=3D NULL); + if (mProtectionMemRange[Index].Present =3D=3D 0) { + MemoryAttrMask =3D EFI_MEMORY_RP; + } =20 - // Split it - for (PtIndex =3D 0; PtIndex < SIZE_4KB / sizeof (*Pt); PtIndex= ++) { - Pt[PtIndex] =3D Address + ((PtIndex << 12) | mAddressEncMask= | PAGE_ATTRIBUTE_BITS); - } // end for PT + Base =3D mProtectionMemRange[Index].Range.Base; + Length =3D mProtectionMemRange[Index].Range.Top - Base; + if (MemoryAttrMask !=3D 0) { + Status =3D ConvertMemoryPageAttributes (PageTable, mPagingMode, Ba= se, Length, MemoryAttrMask, TRUE, NULL); + ASSERT_RETURN_ERROR (Status); + } =20 - *Pd =3D (UINT64)(UINTN)Pt | mAddressEncMask | PAGE_ATTRIBUTE_B= ITS; - } // end if IsAddressSplit - } // end for PD - } // end for PDPT - } // end for PML4 - } // end for PML5 + if (Base > PreviousAddress) { + // + // Mark the ranges not in mProtectionMemRange as non-present. + // + MemoryAttrMask =3D EFI_MEMORY_RP; + Status =3D ConvertMemoryPageAttributes (PageTable, mPaging= Mode, PreviousAddress, Base - PreviousAddress, MemoryAttrMask, TRUE, NULL); + ASSERT_RETURN_ERROR (Status); + } =20 - // - // Go through page table and set several page table entries to absent or= execute-disable. - // - DEBUG ((DEBUG_INFO, "Patch page table start ...\n")); - for (Pml5Index =3D 0; Pml5Index < NumberOfPml5Entries; Pml5Index++) { - if ((Pml5[Pml5Index] & IA32_PG_P) =3D=3D 0) { - // - // If PML5 entry does not exist, skip it - // - continue; + PreviousAddress =3D Base + Length; } =20 - Pml4 =3D (UINT64 *)(UINTN)(Pml5[Pml5Index] & PHYSICAL_ADDRESS_MASK); - for (Pml4Index =3D 0; Pml4Index < NumberOfPml4Entries; Pml4Index++) { - if ((Pml4[Pml4Index] & IA32_PG_P) =3D=3D 0) { + // + // This assignment is for setting the last remaining range + // + MemoryAttrMask =3D EFI_MEMORY_RP; + } else { + // + // Sort the mSmmCpuSmramRanges + // + Buffer =3D AllocateZeroPool (sizeof (EFI_SMRAM_DESCRIPTOR)); + ASSERT (Buffer !=3D NULL); + QuickSort (mSmmCpuSmramRanges, mSmmCpuSmramRangeCount, sizeof (EFI_SMR= AM_DESCRIPTOR), (BASE_SORT_COMPARE)CpuSmramRangeCompare, Buffer); + MemoryAttrMask =3D EFI_MEMORY_XP; + for (Index =3D 0; Index < mSmmCpuSmramRangeCount; Index++) { + Base =3D mSmmCpuSmramRanges[Index].CpuStart; + if ((Base > PreviousAddress) && mXdSupported) { // - // If PML4 entry does not exist, skip it + // Mark the ranges not in mSmmCpuSmramRanges as NX. // - continue; + Status =3D ConvertMemoryPageAttributes (PageTable, mPagingMode, Pr= eviousAddress, Base - PreviousAddress, MemoryAttrMask, TRUE, NULL); + ASSERT_RETURN_ERROR (Status); } =20 - Pdpt =3D (UINT64 *)(UINTN)(Pml4[Pml4Index] & ~mAddressEncMask & PHYS= ICAL_ADDRESS_MASK); - for (PdptIndex =3D 0; PdptIndex < NumberOfPdptEntries; PdptIndex++, = Pdpt++) { - if ((*Pdpt & IA32_PG_P) =3D=3D 0) { - // - // If PDPT entry does not exist, skip it - // - continue; - } - - if ((*Pdpt & IA32_PG_PS) !=3D 0) { - // - // This is 1G entry, set NX bit and skip it - // - if (mXdSupported) { - *Pdpt =3D *Pdpt | IA32_PG_NX; - } - - continue; - } + PreviousAddress =3D mSmmCpuSmramRanges[Index].CpuStart + mSmmCpuSmra= mRanges[Index].PhysicalSize; + } + } =20 - Pd =3D (UINT64 *)(UINTN)(*Pdpt & ~mAddressEncMask & PHYSICAL_ADDRE= SS_MASK); - if (Pd =3D=3D 0) { - continue; - } + FreePool (Buffer); =20 - for (PdIndex =3D 0; PdIndex < SIZE_4KB / sizeof (*Pd); PdIndex++, = Pd++) { - if ((*Pd & IA32_PG_P) =3D=3D 0) { - // - // If PD entry does not exist, skip it - // - continue; - } - - Address =3D (UINTN)LShiftU64 ( - LShiftU64 ( - LShiftU64 ((Pml5Index << 9) + Pml4Index, 9)= + PdptIndex, - 9 - ) + PdIndex, - 21 - ); - - if ((*Pd & IA32_PG_PS) !=3D 0) { - // 2MB page - - if (!IsAddressValid (Address, &Nx)) { - // - // Patch to remove Present flag and RW flag - // - *Pd =3D *Pd & (INTN)(INT32)(~PAGE_ATTRIBUTE_BITS); - } - - if (Nx && mXdSupported) { - *Pd =3D *Pd | IA32_PG_NX; - } - } else { - // 4KB page - Pt =3D (UINT64 *)(UINTN)(*Pd & ~mAddressEncMask & PHYSICAL_ADD= RESS_MASK); - if (Pt =3D=3D 0) { - continue; - } - - for (PtIndex =3D 0; PtIndex < SIZE_4KB / sizeof (*Pt); PtIndex= ++, Pt++) { - if (!IsAddressValid (Address, &Nx)) { - *Pt =3D *Pt & (INTN)(INT32)(~PAGE_ATTRIBUTE_BITS); - } - - if (Nx && mXdSupported) { - *Pt =3D *Pt | IA32_PG_NX; - } - - Address +=3D SIZE_4KB; - } // end for PT - } // end if PS - } // end for PD - } // end for PDPT - } // end for PML4 - } // end for PML5 + if (PreviousAddress < Limit) { + // + // Set the last remaining range to EFI_MEMORY_RP/EFI_MEMORY_XP. + // This path applies to both SmmProfile enable/disable case. + // + Status =3D ConvertMemoryPageAttributes (PageTable, mPagingMode, Previo= usAddress, Limit - PreviousAddress, MemoryAttrMask, TRUE, NULL); + ASSERT_RETURN_ERROR (Status); + } =20 EnableReadOnlyPageWriteProtect (WpEnabled, CetEnabled); =20 --=20 2.39.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 (#103394): https://edk2.groups.io/g/devel/message/103394 Mute This Topic: https://groups.io/mt/98406595/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 19 12:46:00 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+103395+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+103395+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1682066252; cv=none; d=zohomail.com; s=zohoarc; b=S9HE0PzRcRFg0iewCw6WIdzHKhSOjhRjETF4k4fe5X4MeeTiTpF+/B5ArzJRUKHtpp14yXUpfEJLKaRb5knunzV3m1iZHIkWgPrasxuvhKQd+O750HMVU2Y/fbFEPLiQ7EsDg6o3plmTak1PzWPxS4SAI9uFTGAnzNUexKFS+JU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682066252; 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=4JV+5ZpSBUc3pyv+EzS6j1xpdrCsA0xFADrHAJYUSVU=; b=e4Kga7ApteCndWICRoO6QCbQrKrLP4LDYB2inMWRAfcXLjXsnWOs2BNK4mZag5s8qCBAMiU1m9oS6+TbT/vJNDSa1Sj/WQvVIYO0V8BR94Oaf1lsc2PaiuOOOHDgj1Ob/aBC9MV9Jl7ABByKgvxeuZwYwsdqyw32RarfYtEK7I0= 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+103395+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 16820662529581008.1618930681009; Fri, 21 Apr 2023 01:37:32 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id YQH5YY1788612xlpI8iWGJXt; Fri, 21 Apr 2023 01:37:32 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.7066.1682066249739051879 for ; Fri, 21 Apr 2023 01:37:31 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="373869799" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="373869799" X-Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10686"; a="669650499" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="669650499" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 01:37:12 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V3 11/11] UefiCpuPkg/PiSmmCpuDxeSmm: Remove unnecessary function Date: Fri, 21 Apr 2023 16:36:28 +0800 Message-Id: <20230421083628.1408-12-dun.tan@intel.com> In-Reply-To: <20230421083628.1408-1-dun.tan@intel.com> References: <20230421083628.1408-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: YjgNKr4oOSIOXYG5oq7OBUAax1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1682066252; bh=Ru4nj+iz/5dfYHa4y/cDSjRmGDhcwKQkIdeFeyO2Y/c=; h=Cc:Date:From:Reply-To:Subject:To; b=kknL3WJmk4KApJTisq4L4VfmbtCu5sej5gw4i4GExUjj+GTaTPINkHM91kEzvXjWzvI 7waCzcot4ENaCC9P3tzoJdUOT8XgO4uq9gMCluWzVCyZpoQklP2W+xA+m9xE5vyajSAxM I0amRGymwlyx+MwSkRJuLptFbZm26SKwRz0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1682066254373100017 Content-Type: text/plain; charset="utf-8" Remove unnecessary function SetNotPresentPage(). We can directly use ConvertMemoryPageAttributes to set a range to non-present. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 8 ++++++-- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 16 ---------------- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 22 ------------------= ---- 3 files changed, 6 insertions(+), 40 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.c index c0e368ea94..5316ba7b5a 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c @@ -1074,10 +1074,14 @@ PiCpuSmmEntry ( mSmmShadowStackSize ); if (FeaturePcdGet (PcdCpuSmmStackGuard)) { - SetNotPresentPage ( + ConvertMemoryPageAttributes ( Cr3, + mPagingMode, (EFI_PHYSICAL_ADDRESS)(UINTN)Stacks + mSmmStackSize + EFI_PAGES_= TO_SIZE (1) + (mSmmStackSize + mSmmShadowStackSize) * Index, - EFI_PAGES_TO_SIZE (1) + EFI_PAGES_TO_SIZE (1), + EFI_MEMORY_RP, + TRUE, + NULL ); } } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 37366d6803..5f1f1698fa 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -1235,22 +1235,6 @@ SetShadowStack ( IN UINT64 Length ); =20 -/** - Set not present memory. - - @param[in] Cr3 The page table base address. - @param[in] BaseAddress The physical address that is the start addr= ess of a memory region. - @param[in] Length The size in bytes of the memory region. - - @retval EFI_SUCCESS The not present memory is set. -**/ -EFI_STATUS -SetNotPresentPage ( - IN UINTN Cr3, - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ); - /** Initialize the shadow stack related data structure. =20 diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 9a0619ce51..7cebc16a1d 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -708,28 +708,6 @@ SetShadowStack ( return Status; } =20 -/** - Set not present memory. - - @param[in] Cr3 The page table base address. - @param[in] BaseAddress The physical address that is the start addr= ess of a memory region. - @param[in] Length The size in bytes of the memory region. - - @retval EFI_SUCCESS The not present memory is set. -**/ -EFI_STATUS -SetNotPresentPage ( - IN UINTN Cr3, - IN EFI_PHYSICAL_ADDRESS BaseAddress, - IN UINT64 Length - ) -{ - EFI_STATUS Status; - - Status =3D SmmSetMemoryAttributesEx (Cr3, mPagingMode, BaseAddress, Leng= th, EFI_MEMORY_RP); - return Status; -} - /** Retrieves a pointer to the system configuration table from the SMM Syste= m Table based on a specified GUID. --=20 2.39.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 (#103395): https://edk2.groups.io/g/devel/message/103395 Mute This Topic: https://groups.io/mt/98406596/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-