From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web10.3644.1676523683066833668 for ; Wed, 15 Feb 2023 21:01:23 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=MB29EYLC; spf=pass (domain: intel.com, ip: 134.134.136.126, mailfrom: ray.ni@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676523683; x=1708059683; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=xxli/Ol2fvHzAaZfB565wqm9ibZUihzeljlJ4bJ38F4=; b=MB29EYLCaqpRzB3RyqUG2V6hK41lBnGYxhdx9+G/YSylerX1ddqa8AMa qJONRkQ2/SGbOGnHfxMpR5cWUoa9AtLyYUgaU86Vy6F36LeoZ2YC1qfFQ Sn+T+bRYETg/5QmChoHNKg0WZNR0yuEaw0S4gX5oQP5cAnQHSU8TRNwUw INOHj84ys9ko+9l7q+EkL3LLl+HDAAmMTGr6+j9k2PMIC3WbvPYaGSgLz EZCXLabjuyWa7F49AdK15IjCIzYoH+IQssgTKd6ddwGebERj5jwy3UT2j l83EKTdWfjcqOor7S1K05AJZcIbqz7oc8umblp29PhwrE3WOliQmRNL1b A==; X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="315299641" X-IronPort-AV: E=Sophos;i="5.97,301,1669104000"; d="scan'208";a="315299641" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2023 21:01:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="619868265" X-IronPort-AV: E=Sophos;i="5.97,301,1669104000"; d="scan'208";a="619868265" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by orsmga003.jf.intel.com with ESMTP; 15 Feb 2023 21:01:22 -0800 Received: from orsmsx601.amr.corp.intel.com (10.22.229.14) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Wed, 15 Feb 2023 21:01:22 -0800 Received: from ORSEDG602.ED.cps.intel.com (10.7.248.7) by orsmsx601.amr.corp.intel.com (10.22.229.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16 via Frontend Transport; Wed, 15 Feb 2023 21:01:22 -0800 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (104.47.73.172) by edgegateway.intel.com (134.134.137.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.16; Wed, 15 Feb 2023 21:01:21 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k2sYYeM08xG0kX6Wyctqz7sX1ci4N+iL1PCbYylmJ361XQy7wr5Gcm67GXuXJqYwz5RdfKI3f7+r9S6xuydwG83USxVAOCan1l6eO+NSwYmCEKgKTuHxkIJGhyFnILEQ7H+o07KzXMPao8Vkqzw6HdYC7M/Qup13riHX6lCN4muJKr2IXrV6PbysUlXRup8MhuXtYFYKHWJxStTNZQ427AIvKoK3PNLwZ/ZmxjeOMeZGrpM1DD8IvL/kdvHYOHS+crldyruYrkpDqATbemtx1ATglikCZdB1uUxKH3wyZF3N2GXtou9dH9fLVFrwi3MVDBiHcwh6AkUoJxaTtvI90Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=F5bpSzfnP8lNBxuwbzyXLKnxodG+rWKfrbQrIJr4BM8=; b=fVjaVTGPPDI7x/Tlo/a7nwDVj6LhVhW07TUtHKDwfhu9H9vpcjsQWfPajNJsOPw1JSHzUzTT3ZQx2Sv6wh61hEd9UkKI0q7iH0+77hIrCs5qmOwqzQucrgvbzrF8eLMm8zRX4FeVP/MByRRfgfBYKysd67mZXAsIFA8ewZVV/pHd6hjOwy3QRS0cdel5RFP58oHBMMD+80hWi/NW4MzwxtlNkCE5qZJGUz017MC3thgwJVaCmntL5rqeP/3wEAenyM1jHeOWRk34fZyZLkDYkJ5rcgqlkscw/hRlwmubnfCeNPx0JnXG9hZX1rOlBCDt2I80s2iaFtTftvh+NTG64g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by IA1PR11MB7173.namprd11.prod.outlook.com (2603:10b6:208:41b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.24; Thu, 16 Feb 2023 05:01:20 +0000 Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::e342:cbf5:6c57:e8d6]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::e342:cbf5:6c57:e8d6%7]) with mapi id 15.20.6111.013; Thu, 16 Feb 2023 05:01:19 +0000 From: "Ni, Ray" To: "Wu, Jiaxin" , "devel@edk2.groups.io" CC: "Dong, Eric" , "Zeng, Star" , Laszlo Ersek , Gerd Hoffmann , "Kumar, Rahul R" Subject: Re: [PATCH v8 4/6] UefiCpuPkg/PiSmmCpuDxeSmm: Consume SMM Base Hob for SmBase info Thread-Topic: [PATCH v8 4/6] UefiCpuPkg/PiSmmCpuDxeSmm: Consume SMM Base Hob for SmBase info Thread-Index: AQHZQbDuvO6d1PJk/kKwNv/nRhTyMa7RBBdA Date: Thu, 16 Feb 2023 05:01:19 +0000 Message-ID: References: <20230216024635.9316-1-jiaxin.wu@intel.com> <20230216024635.9316-5-jiaxin.wu@intel.com> In-Reply-To: <20230216024635.9316-5-jiaxin.wu@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|IA1PR11MB7173:EE_ x-ms-office365-filtering-correlation-id: 5797a60f-36a6-421b-b604-08db0fdad7a2 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ik6dXoQ+SuO5DGZqUFX6p14R29NpFrPpPbVBAvn+xClUOqCgMXeaLt0EYbevV6WpkE2M3N8S378QudY1l8DUHLOQb7iUVWH88lFiGS/BkpGbZx6cuCxXhVSR1n1PfgVSctxbaQgPT3xbLXMg/uuagOyOUJGS0fNqhWoFHUfkycOVvttlYaoRLO0CqI6YxlbfqBCTRfZvjVFIKn/DZBFep07cpkGC+1eZXQi4onEOaU1hHus3+CmM2CWsDVrP2bhiGRctWnvV+PfIsAROkSnJcMA123deWm/CEDm0Y5DTsddb9Oipkw6Bs+nSVqi2rGjGwECPc/lirhOXoFE5QfyRYfN4aXK2JBo+/obNaSB/h44zSYbFuL8zsnmtS6IG8JqzLGt5GfZLJwBzQBpz93o6OJtf4Do+OER35omnaR8LQlq+zk8/QnL5E7h64/6g9ez1XNKIWSGVfBTO/jAAQJ6QiZPy15gXD7U1HX94wNLWX/fXOvbWYzT7NVHs15qnUNc2XK9m4IfgcEcGcVkgoKlsW8DC9vyMcXDJLVYaoR+4s2F6vBgqCjBeGEIOFdyeW1H6uHO5PVPFcilJZNYgAE/4qAG0ICryUeap92TTVI+apx6y8YuX7Aznv7tUPL1oSCHOpQU+jqUos5Dmj92ghJS5YHRaq7+mfadsj0CSB06WGqHVdzYowAIH53BBjW2zUsYI x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN6PR11MB8244.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(6029001)(376002)(346002)(396003)(366004)(39860400002)(136003)(451199018)(966005)(6506007)(30864003)(33656002)(41300700001)(478600001)(71200400001)(7696005)(52536014)(38100700002)(8936002)(82960400001)(86362001)(186003)(9686003)(76116006)(4326008)(66946007)(53546011)(8676002)(66446008)(64756008)(66476007)(55016003)(66556008)(5660300002)(316002)(122000001)(38070700005)(83380400001)(54906003)(107886003)(2906002)(19627235002)(110136005)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?zab779DzgHmpv2hPVfIkdFSmnbiBLMt49DocE7PAgItCQWfJgkMLITMh2eQp?= =?us-ascii?Q?2NKiXZ3DB8vHdlZZE8n8l4XV8hL5ckr/zkS26r4fg7C9IqFFNTWoWKPsiF2p?= =?us-ascii?Q?YAPburaXdVKWgzeiT7NCQ2ElEKzMFhS7gGRgWHNDnbhBgtqAwdjac/q31PpG?= =?us-ascii?Q?m0VilA77BNHD9Dr8IC+PwleLDo+A7Tyc1kk7d2Q2NlOjEOs2jRz+BG1vBpCJ?= =?us-ascii?Q?wTRGObzBrJEiqQqgf2zt810DigPRNrZpHDMSPT2Ng4Rmr+YdFNaxEswqwg3W?= =?us-ascii?Q?+b4Mn5NTh4YGg9lxlQ/So/+edS9caD4OBK1TE1lOyshX7JBVxVp5J6c1ledO?= =?us-ascii?Q?oBxQ/0u5SjSgOd/31kgQr5TeA1PtI7TY7CHQegZcrbP6SYVArC2GRVAtt01R?= =?us-ascii?Q?5glxwao9PAL2HkzUhpzGgzi60Mr+X3r7JU4cf8xERrbHqEmtf85gSqKW0nAJ?= =?us-ascii?Q?yDcIeKYCTR+W5jPyh8afDnIz/4cGQzcC2x27VGxOsk0COvpnAKmmaZgyHXTq?= =?us-ascii?Q?kRSYFbSJxYYPiaBVokBMDzX7SnGSm6ucCZT6Z3+qX5pFukN7ux8u+cb8rDLX?= =?us-ascii?Q?eWqw9nbEfY/+goQVthVVzqH+t4mBKd3EzOlCHz8wkJTbo9dFwrGcxP2+iruf?= =?us-ascii?Q?3IhjF7j070CKjtVBcvvjojYKeqAPa3cRgVuBMHdVFZP1AuMetG1a4hxKM+g0?= =?us-ascii?Q?MduEjbUtVxWODOtfz1MgqRtKv/BETihb4+TvRVj1vtDs1sJ8nH8Jo+OWOaDT?= =?us-ascii?Q?UOHLP8QZl4KVza3UyO7Nbg4QcPtuJ2fce/olckmGKwD6ibNPqEVoM2vy6Zv2?= =?us-ascii?Q?zEU6G9xrvj8bhLHqxKrP6X2n3RxY+hlqVP/Jrq27UapYk755NRzITrDTmo6b?= =?us-ascii?Q?DrENG1dmhpo1fYDBdPN0Ck/GSzTV34DfPoyqNa2ci3M32bzC9GcWhFp1IHuc?= =?us-ascii?Q?CSz3on+Orn2SEaYc5fbyaOb4S1xAnvGDQoXVmqnlDFVI0dco1qyZeHNMFatg?= =?us-ascii?Q?KwiCdZEmqOprm0k88mTaojhUL8CY2egBZZed2/p1GIILxB4BjeA//Kq87wtD?= =?us-ascii?Q?aan3VlVvhTzign580rwefDul2I3zi+Ptf0Z2qBVcZ7B5Qtn3pW8tI0CypF5h?= =?us-ascii?Q?Xfl8pQ+8w8ufvm/G8SbfRk8lzwy/bLGeCTJ+Ji4uHOXpX4dVOQ9OrUbkmToX?= =?us-ascii?Q?muVsz2w4tf3bg91AfGVqLMSwVSW4rXcRaCAzkwByxtX1Co5zHdYbbAaCVS69?= =?us-ascii?Q?Dy2Uqc5+ebSQ8M5fsko4aHZ28jGmDkSzPmWipT1kw9K8TMGAK3oLZEuvhCHB?= =?us-ascii?Q?wBJQqTaXBtg/zzMS29NWuN21mO19iw5D7sYD9Y0Q8aEUw/De7Wsddcdmt4K2?= =?us-ascii?Q?b6rFxbZd4AvOHt1JVjQWfJKi5GZkAY1sI15wK60+0trL55BWq5C+sAODpCLr?= =?us-ascii?Q?cPj5zoazDMxNKdEzgjJB/FeqGYHCFdHS9Pq/yS4bOen/yRJSXpOBsJfXLUep?= =?us-ascii?Q?EDO5SwDO3CAeRSevKs3fZVoOzhP8mqNk8INVqute4JmhyirL865HMuJkhw?= =?us-ascii?Q?=3D=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN6PR11MB8244.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5797a60f-36a6-421b-b604-08db0fdad7a2 X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Feb 2023 05:01:19.7796 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: vRODRnNko7eX8+yUG7uVLOJ1aXYD30DudZa7aGryQPgKJI0bVZJXwIMgiD3lTuWtOlM2JQAQsTG5xtucCQ3mBw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR11MB7173 Return-Path: ray.ni@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ray Ni > -----Original Message----- > From: Wu, Jiaxin > Sent: Thursday, February 16, 2023 10:47 AM > To: devel@edk2.groups.io > Cc: Dong, Eric ; Ni, Ray ; Zeng, S= tar > ; Laszlo Ersek ; Gerd Hoffmann > ; Kumar, Rahul R > Subject: [PATCH v8 4/6] UefiCpuPkg/PiSmmCpuDxeSmm: Consume SMM > Base Hob for SmBase info >=20 > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4337 >=20 > Existing SMBASE Relocation is in the PiSmmCpuDxeSmm driver, which > will relocate the SMBASE of each processor by setting the SMBASE > field in the saved state map (at offset 7EF8h) to a new value. > The RSM instruction reloads the internal SMBASE register with the > value in SMBASE field when each time it exits SMM. All subsequent > SMI requests will use the new SMBASE to find the starting address > for the SMI handler (at SMBASE + 8000h). >=20 > Due to the default SMBASE for all x86 processors is 0x30000, the > APs' 1st SMI for rebase has to be executed one by one to avoid > the CPUs over-writing each other's SMM Save State Area (see > existing SmmRelocateBases() function), which means the next AP has > to wait for the previous AP to finish its 1st SMI, then it can call > into its 1st SMI for rebase via Smi Ipi command, thus leading the > existing SMBASE Relocation has to be running in series. Besides, it > needs very complex code to handle the AP exit semaphore > (mRebased[Index]), which will hook return address of SMM Save State > so that semaphore code can be executed immediately after AP exits > SMM for SMBASE relocation (see existing SemaphoreHook() function). >=20 > With SMM Base Hob support, PiSmmCpuDxeSmm does not need the RSM > instruction to do the SMBASE Relocation. SMBASE Register for each > processors have already been programmed and all SMBASE address have > recorded in SMM Base Hob. So the same default SMBASE Address > (0x30000) will not be used, thus the CPUs over-writing each other's > SMM Save State Area will not happen in PiSmmCpuDxeSmm driver. This > way makes the first SMI init can be executed in parallel and save > boot time on multi-core system. Besides, Semaphore Hook code logic > is also not required, which will greatly simplify the SMBASE > Relocation flow. >=20 > Mainly changes as below: > * Assume the biggest possibility of tile size is 8k. > * Combine 2 SMIs (gcSmmInitTemplate & gcSmiHandlerTemplate) into one > (gcSmiHandlerTemplate), the new SMI handler needs to run to 2 paths: > one to SmmCpuFeaturesInitializeProcessor(), the other to SMM Core > Entry Point. > * Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) for first > SMI init before normal SMI sources happen. > * Call SmmCpuFeaturesInitializeProcessor() in parallel. >=20 > Cc: Eric Dong > Cc: Ray Ni > Cc: Zeng Star > Cc: Laszlo Ersek > Cc: Gerd Hoffmann > Cc: Rahul Kumar > Signed-off-by: Jiaxin Wu > --- > UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 31 ++++- > UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 25 +++- > UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 166 > ++++++++++++++++++++++----- > UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 26 ++++- > UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 3 +- > 5 files changed, 214 insertions(+), 37 deletions(-) >=20 > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > index fb4a44eab6..d408b3f9f7 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > @@ -1,9 +1,9 @@ > /** @file > Code for Processor S3 restoration >=20 > -Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.
> SPDX-License-Identifier: BSD-2-Clause-Patent >=20 > **/ >=20 > #include "PiSmmCpuDxeSmm.h" > @@ -822,13 +822,38 @@ SmmRestoreCpu ( > // > InitializeCpuBeforeRebase (); > } >=20 > // > - // Restore SMBASE for BSP and all APs > + // Make sure the gSmmBaseHobGuid existence status is the same > between normal and S3 boot. > // > - SmmRelocateBases (); > + ASSERT (mSmmRelocated =3D=3D (BOOLEAN)(GetFirstGuidHob > (&gSmmBaseHobGuid) !=3D NULL)); > + if (mSmmRelocated !=3D (BOOLEAN)(GetFirstGuidHob > (&gSmmBaseHobGuid) !=3D NULL)) { > + DEBUG (( > + DEBUG_ERROR, > + "gSmmBaseHobGuid %a produced in normal boot but %a in S3 boot!", > + mSmmRelocated ? "is" : "is not", > + mSmmRelocated ? "is not" : "is" > + )); > + CpuDeadLoop (); > + } > + > + // > + // Check whether Smm Relocation is done or not. > + // If not, will do the SmmBases Relocation here!!! > + // > + if (!mSmmRelocated) { > + // > + // Restore SMBASE for BSP and all APs > + // > + SmmRelocateBases (); > + } else { > + // > + // Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) to exec= ute > first SMI init. > + // > + ExecuteFirstSmiInit (); > + } >=20 > // > // Skip initialization if mAcpiCpuData is not valid > // > if (mAcpiCpuData.NumberOfCpus > 0) { > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c > index a0967eb69c..baf827cf9d 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c > @@ -1,9 +1,9 @@ > /** @file > SMM MP service implementation >=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 >=20 > **/ > @@ -1721,17 +1721,40 @@ SmiRendezvous ( > UINTN Index; > UINTN Cr2; >=20 > ASSERT (CpuIndex < mMaxNumberOfCpus); >=20 > + if (mSmmRelocated) { > + ASSERT (mSmmInitialized !=3D NULL); > + } > + > // > // Save Cr2 because Page Fault exception in SMM may override its value= , > // when using on-demand paging for above 4G memory. > // > Cr2 =3D 0; > SaveCr2 (&Cr2); >=20 > + if (mSmmRelocated && !mSmmInitialized[CpuIndex]) { > + // > + // Perform SmmInitHandler for CpuIndex > + // > + SmmInitHandler (); > + > + // > + // Restore Cr2 > + // > + RestoreCr2 (Cr2); > + > + // > + // Mark the first SMI init for CpuIndex has been done so as to avoid= the > reentry. > + // > + mSmmInitialized[CpuIndex] =3D TRUE; > + > + return; > + } > + > // > // Call the user register Startup function first. > // > if (mSmmMpSyncData->StartupProcedure !=3D NULL) { > mSmmMpSyncData->StartupProcedure (mSmmMpSyncData- > >StartupProcArgs); > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c > index 6e795d1756..d7a51d7e80 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c > @@ -82,10 +82,12 @@ EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL > mSmmMemoryAttribute =3D { > EdkiiSmmClearMemoryAttributes > }; >=20 > EFI_CPU_INTERRUPT_HANDLER > mExternalVectorTable[EXCEPTION_VECTOR_NUMBER]; >=20 > +BOOLEAN mSmmRelocated =3D FALSE; > +volatile BOOLEAN *mSmmInitialized =3D NULL; > UINT32 mBspApicId =3D 0; >=20 > // > // SMM stack information > // > @@ -381,22 +383,69 @@ SmmInitHandler ( > // Initialize private data during S3 resume > // > InitializeMpSyncData (); > } >=20 > - // > - // Hook return after RSM to set SMM re-based flag > - // > - SemaphoreHook (Index, &mRebased[Index]); > + if (!mSmmRelocated) { > + // > + // Hook return after RSM to set SMM re-based flag > + // > + SemaphoreHook (Index, &mRebased[Index]); > + } >=20 > return; > } > } >=20 > ASSERT (FALSE); > } >=20 > +/** > + Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) to execute fi= rst > SMI init. > + > +**/ > +VOID > +ExecuteFirstSmiInit ( > + VOID > + ) > +{ > + UINTN Index; > + > + if (mSmmInitialized =3D=3D NULL) { > + mSmmInitialized =3D (BOOLEAN *)AllocatePool (sizeof (BOOLEAN) * > mMaxNumberOfCpus); > + } > + > + ASSERT (mSmmInitialized !=3D NULL); > + if (mSmmInitialized =3D=3D NULL) { > + return; > + } > + > + // > + // Reset the mSmmInitialized to false. > + // > + ZeroMem ((VOID *)mSmmInitialized, sizeof (BOOLEAN) * > mMaxNumberOfCpus); > + > + // > + // Get the BSP ApicId. > + // > + mBspApicId =3D GetApicId (); > + > + // > + // Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) for SMM in= it > + // > + SendSmiIpi (mBspApicId); > + SendSmiIpiAllExcludingSelf (); > + > + // > + // Wait for all processors to finish its 1st SMI > + // > + for (Index =3D 0; Index < mNumberOfCpus; Index++) { > + while (!(BOOLEAN)mSmmInitialized[Index]) { > + } > + } > +} > + > /** > Relocate SmmBases for each processor. >=20 > Execute on first boot and all S3 resumes >=20 > @@ -560,10 +609,15 @@ PiCpuSmmEntry ( > UINT32 RegEcx; > UINT32 RegEdx; > UINTN FamilyId; > UINTN ModelId; > UINT32 Cr3; > + EFI_HOB_GUID_TYPE *GuidHob; > + SMM_BASE_HOB_DATA *SmmBaseHobData; > + > + GuidHob =3D NULL; > + SmmBaseHobData =3D NULL; >=20 > // > // Initialize address fixup > // > PiSmmCpuSmmInitFixupAddress (); > @@ -788,30 +842,58 @@ PiCpuSmmEntry ( > // context must be reduced. > // > ASSERT (TileSize <=3D (SMRAM_SAVE_STATE_MAP_OFFSET + sizeof > (SMRAM_SAVE_STATE_MAP) - SMM_HANDLER_OFFSET)); >=20 > // > - // Allocate buffer for all of the tiles. > - // > - // Intel(R) 64 and IA-32 Architectures Software Developer's Manual > - // Volume 3C, Section 34.11 SMBASE Relocation > - // For Pentium and Intel486 processors, the SMBASE values must be > - // aligned on a 32-KByte boundary or the processor will enter shutdo= wn > - // state during the execution of a RSM instruction. > - // > - // Intel486 processors: FamilyId is 4 > - // Pentium processors : FamilyId is 5 > + // Retrive the allocated SmmBase from gSmmBaseHobGuid. If found, > + // means the SmBase relocation has been done. > // > - BufferPages =3D EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * > (mMaxNumberOfCpus - 1)); > - if ((FamilyId =3D=3D 4) || (FamilyId =3D=3D 5)) { > - Buffer =3D AllocateAlignedCodePages (BufferPages, SIZE_32KB); > + GuidHob =3D GetFirstGuidHob (&gSmmBaseHobGuid); > + if (GuidHob !=3D NULL) { > + // > + // Check whether the Required TileSize is enough. > + // > + if (TileSize > SIZE_8KB) { > + DEBUG ((DEBUG_ERROR, "The Range of Smbase in SMRAM is not > enough -- Required TileSize =3D 0x%08x, Actual TileSize =3D 0x%08x\n", Ti= leSize, > SIZE_8KB)); > + CpuDeadLoop (); > + return RETURN_BUFFER_TOO_SMALL; > + } > + > + SmmBaseHobData =3D GET_GUID_HOB_DATA (GuidHob); > + > + // > + // Assume single instance of HOB produced, expect the > HOB.NumberOfProcessors equals to the mMaxNumberOfCpus. > + // > + ASSERT (SmmBaseHobData->NumberOfProcessors =3D=3D > (UINT32)mMaxNumberOfCpus && SmmBaseHobData->CpuIndex =3D=3D 0); > + mSmmRelocated =3D TRUE; > } else { > - Buffer =3D AllocateAlignedCodePages (BufferPages, SIZE_4KB); > - } > + // > + // When the HOB doesn't exist, allocate new SMBASE itself. > + // > + DEBUG ((DEBUG_INFO, "PiCpuSmmEntry: gSmmBaseHobGuid not > found!\n")); > + // > + // Allocate buffer for all of the tiles. > + // > + // Intel(R) 64 and IA-32 Architectures Software Developer's Manual > + // Volume 3C, Section 34.11 SMBASE Relocation > + // For Pentium and Intel486 processors, the SMBASE values must be > + // aligned on a 32-KByte boundary or the processor will enter shut= down > + // state during the execution of a RSM instruction. > + // > + // Intel486 processors: FamilyId is 4 > + // Pentium processors : FamilyId is 5 > + // > + BufferPages =3D EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * > (mMaxNumberOfCpus - 1)); > + if ((FamilyId =3D=3D 4) || (FamilyId =3D=3D 5)) { > + Buffer =3D AllocateAlignedCodePages (BufferPages, SIZE_32KB); > + } else { > + Buffer =3D AllocateAlignedCodePages (BufferPages, SIZE_4KB); > + } >=20 > - ASSERT (Buffer !=3D NULL); > - DEBUG ((DEBUG_INFO, "SMRAM SaveState Buffer (0x%08x, 0x%08x)\n", > Buffer, EFI_PAGES_TO_SIZE (BufferPages))); > + ASSERT (Buffer !=3D NULL); > + DEBUG ((DEBUG_INFO, "New Allcoated SMRAM SaveState Buffer > (0x%08x, 0x%08x)\n", Buffer, EFI_PAGES_TO_SIZE (BufferPages))); > + } >=20 > // > // Allocate buffer for pointers to array in SMM_CPU_PRIVATE_DATA. > // > gSmmCpuPrivate->ProcessorInfo =3D (EFI_PROCESSOR_INFORMATION > *)AllocatePool (sizeof (EFI_PROCESSOR_INFORMATION) * > mMaxNumberOfCpus); > @@ -842,11 +924,12 @@ PiCpuSmmEntry ( > // Retrieve APIC ID of each enabled processor from the MP Services > protocol. > // Also compute the SMBASE address, CPU Save State address, and CPU > Save state > // size for each CPU in the platform > // > for (Index =3D 0; Index < mMaxNumberOfCpus; Index++) { > - mCpuHotPlugData.SmBase[Index] =3D (UINTN)Buffer + Index * = TileSize > - SMM_HANDLER_OFFSET; > + mCpuHotPlugData.SmBase[Index] =3D mSmmRelocated ? > (UINTN)SmmBaseHobData->SmBase[Index] : (UINTN)Buffer + Index * > TileSize - SMM_HANDLER_OFFSET; > + > gSmmCpuPrivate->CpuSaveStateSize[Index] =3D sizeof > (SMRAM_SAVE_STATE_MAP); > gSmmCpuPrivate->CpuSaveState[Index] =3D (VOID > *)(mCpuHotPlugData.SmBase[Index] + > SMRAM_SAVE_STATE_MAP_OFFSET); > gSmmCpuPrivate->Operation[Index] =3D SmmCpuNone; >=20 > if (Index < mNumberOfCpus) { > @@ -955,21 +1038,27 @@ PiCpuSmmEntry ( > // Initialize IDT > // > InitializeSmmIdt (); >=20 > // > - // Relocate SMM Base addresses to the ones allocated from SMRAM > + // Check whether Smm Relocation is done or not. > + // If not, will do the SmmBases Relocation here!!! > // > - mRebased =3D (BOOLEAN *)AllocateZeroPool (sizeof (BOOLEAN) * > mMaxNumberOfCpus); > - ASSERT (mRebased !=3D NULL); > - SmmRelocateBases (); > + if (!mSmmRelocated) { > + // > + // Relocate SMM Base addresses to the ones allocated from SMRAM > + // > + mRebased =3D (BOOLEAN *)AllocateZeroPool (sizeof (BOOLEAN) * > mMaxNumberOfCpus); > + ASSERT (mRebased !=3D NULL); > + SmmRelocateBases (); >=20 > - // > - // Call hook for BSP to perform extra actions in normal mode after all > - // SMM base addresses have been relocated on all CPUs > - // > - SmmCpuFeaturesSmmRelocationComplete (); > + // > + // Call hook for BSP to perform extra actions in normal mode after a= ll > + // SMM base addresses have been relocated on all CPUs > + // > + SmmCpuFeaturesSmmRelocationComplete (); > + } >=20 > DEBUG ((DEBUG_INFO, "mXdSupported - 0x%x\n", mXdSupported)); >=20 > // > // SMM Time initialization > @@ -996,10 +1085,25 @@ PiCpuSmmEntry ( > ); > } > } > } >=20 > + // > + // For relocated SMBASE, some MSRs & CSRs are still required to be > configured in SMM Mode for SMM Initialization. > + // Those MSRs & CSRs must be configured before normal SMI sources > happen. > + // So, here is to issue SMI IPI (All Excluding Self SMM IPI + BSP SMM= IPI) to > execute first SMI init. > + // > + if (mSmmRelocated) { > + ExecuteFirstSmiInit (); > + > + // > + // Call hook for BSP to perform extra actions in normal mode after a= ll > + // SMM base addresses have been relocated on all CPUs > + // > + SmmCpuFeaturesSmmRelocationComplete (); > + } > + > // > // Fill in SMM Reserved Regions > // > gSmmCpuPrivate->SmmReservedSmramRegion[0].SmramReservedStart =3D > 0; > gSmmCpuPrivate->SmmReservedSmramRegion[0].SmramReservedSize =3D > 0; > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > index 5f0a38e400..c3731f174b 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > @@ -1,9 +1,9 @@ > /** @file > Agent Module to load other modules to deploy SMM Entry Vector for X86 > CPU. >=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 >=20 > **/ > @@ -23,10 +23,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent > #include >=20 > #include > #include > #include > +#include >=20 > #include > #include > #include > #include > @@ -346,10 +347,29 @@ SmmWriteSaveState ( > IN EFI_SMM_SAVE_STATE_REGISTER Register, > IN UINTN CpuIndex, > IN CONST VOID *Buffer > ); >=20 > +/** > + C function for SMI handler. To change all processor's SMMBase Register= . > + > +**/ > +VOID > +EFIAPI > +SmmInitHandler ( > + VOID > + ); > + > +/** > + Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) to execute fi= rst > SMI init. > + > +**/ > +VOID > +ExecuteFirstSmiInit ( > + VOID > + ); > + > /** > Read a CPU Save State register on the target processor. >=20 > This function abstracts the differences that whether the CPU Save State > register is in the > IA32 CPU Save State Map or X64 CPU Save State Map. > @@ -400,10 +420,14 @@ WriteSaveStateRegister ( > IN EFI_SMM_SAVE_STATE_REGISTER Register, > IN UINTN Width, > IN CONST VOID *Buffer > ); >=20 > +extern BOOLEAN mSmmRelocated; > +extern volatile BOOLEAN *mSmmInitialized; > +extern UINT32 mBspApicId; > + > extern CONST UINT8 gcSmmInitTemplate[]; > extern CONST UINT16 gcSmmInitSize; > X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr0; > extern UINT32 mSmmCr0; > X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr3; > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf > b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf > index b4b327f60c..9bfa8c1a76 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf > @@ -2,11 +2,11 @@ > # CPU SMM driver. > # > # This SMM driver performs SMM initialization, deploy SMM Entry Vector, > # provides CPU specific services in SMM. > # > -# 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.
> # > # SPDX-License-Identifier: BSD-2-Clause-Patent > # > ## > @@ -112,10 +112,11 @@ >=20 > [Guids] > gEfiAcpiVariableGuid ## SOMETIMES_CONSUMES ## HOB = # it is > used for S3 boot. > gEdkiiPiSmmMemoryAttributesTableGuid ## CONSUMES ## SystemTable > gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable > + gSmmBaseHobGuid ## CONSUMES >=20 > [FeaturePcd] > gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmDebug ## > CONSUMES > gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmBlockStartupThisAp ## > CONSUMES > gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection ## > CONSUMES > -- > 2.16.2.windows.1