From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 9A242780091 for ; Sat, 25 May 2024 01:32:28 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=5Q441AttF8h1o+qOc020FGXzquP0eqOMwXlkTj297L8=; c=relaxed/simple; d=groups.io; h=From:To:Subject:Thread-Topic:Thread-Index:Date:Message-ID:References:In-Reply-To:Accept-Language:msip_labels:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type; s=20240206; t=1716600747; v=1; b=Sv7i0Hq9t9X3fWcaFGFda5FqjfoxlJh8wvNmREPJB/LcZaJfBWjE98ZmWV5RrGzDMJFYFIUs 7hZwmCqSC1HKIhwshoRDITtO2T6PqBcUJ/AttPZ+9liOIHlZIpnFQNn6u0ViwUdvIvHENPsZsiE Ru+JsvrVwJ2F2Ar3vW4T0Btic5jwE13cfi43MpwcP/XRNnBpEWrzzTFrlUWu21RCzT7bGvf+lez suCwSP/ofb4amd0pVw7oTo3GNEm3qD8bbV1yG1x8c5MBWMb/AMVAkpmcgFns30g1JGiy8mLjc33 KXijZ5NrGhCK7TB8bPta1dvy1Z9DnRjIaf4Mznhq7jLOw== X-Received: by 127.0.0.2 with SMTP id pHQNYY7687511xIDVPN6dIrU; Fri, 24 May 2024 18:32:27 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by mx.groups.io with SMTP id smtpd.web11.3575.1716600745944882024 for ; Fri, 24 May 2024 18:32:26 -0700 X-CSE-ConnectionGUID: 3rCrQahySTWOJvIp6KDl+A== X-CSE-MsgGUID: R9TGMStWTl+GO0RrmtiGQQ== X-IronPort-AV: E=McAfee;i="6600,9927,11082"; a="12776043" X-IronPort-AV: E=Sophos;i="6.08,187,1712646000"; d="scan'208,217";a="12776043" X-Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 May 2024 18:32:25 -0700 X-CSE-ConnectionGUID: rFSN7RkkTeKgROvIOPreBw== X-CSE-MsgGUID: IPB4zq2SR0+3bIcBMpqxtg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,187,1712646000"; d="scan'208,217";a="38614548" X-Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by fmviesa003.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 24 May 2024 18:32:25 -0700 X-Received: from fmsmsx610.amr.corp.intel.com (10.18.126.90) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 24 May 2024 18:32:25 -0700 X-Received: from fmsedg602.ED.cps.intel.com (10.1.192.136) by fmsmsx610.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39 via Frontend Transport; Fri, 24 May 2024 18:32:25 -0700 X-Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.170) by edgegateway.intel.com (192.55.55.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 24 May 2024 18:32:25 -0700 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by DM6PR11MB4724.namprd11.prod.outlook.com (2603:10b6:5:2ad::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.36; Sat, 25 May 2024 01:32:22 +0000 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::41a4:c775:32e6:76a8]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::41a4:c775:32e6:76a8%4]) with mapi id 15.20.7587.035; Sat, 25 May 2024 01:32:22 +0000 From: "Ni, Ray" To: "Feng, Ning" , "devel@edk2.groups.io" Subject: Re: [edk2-devel] [PATCH] UefiCpuPkg/MpLib:Do not assume BSP is #0. Thread-Topic: [PATCH] UefiCpuPkg/MpLib:Do not assume BSP is #0. Thread-Index: AQHarjCN0uJNmp/a9kao0ZDr8qKuQLGnKlH3 Date: Sat, 25 May 2024 01:32:22 +0000 Message-ID: References: <20240525074209.1672006-1-ning.feng@intel.com> In-Reply-To: <20240525074209.1672006-1-ning.feng@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|DM6PR11MB4724:EE_ x-ms-office365-filtering-correlation-id: 8a6d2bc0-6be6-482d-9580-08dc7c5a8686 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: =?us-ascii?Q?qxkss0gDk9VYwSwg1SN5hT3T/aFA39APJ0LKBTuf5rvrynkAvQi865x2Fglg?= =?us-ascii?Q?ptyYa/I9NEgAd57L46VfHndYg8DWMvVzhNBc18RwBRTSsNm7b+uuazoYkioi?= =?us-ascii?Q?f69e/VYWHx40jmvwYSCtt+qH2knhn6Pyjs5VNvQhoNt9xe7xghHJMduA8I/U?= =?us-ascii?Q?U21WM+5fZzG+HZJR0OVjiKclHQi3iSiEtYYdNohCfH7cu6+BTRDvMnOy4hrd?= =?us-ascii?Q?9QxYqK+5idy/jZZS91ieW3BFdFdrlGh3uTnIzrAQSkvi0mqsyZc8+dn0tALj?= =?us-ascii?Q?Zb7s8kk1uWg65n5Z1w75gIF7tNd6eeSC9THG0PWfOjoQNbDjCxYphWWcTJl6?= =?us-ascii?Q?fCatXwKgeTS8I8E3gjlDY53smJjo6L7NMvxH3BNNOMb6Xf6O2exOTwuSfp8G?= =?us-ascii?Q?2on1EZNt87au7wjrfOMSGMROpQIQ/X9sEECGes4DHlpvWqJCs93wW0au/6Qg?= =?us-ascii?Q?iJgxxyZmy0r933Wm5tPHKwehVCAzSjR+Db6f6bN2/Qd7eVCd5mcvfzNFHTfQ?= =?us-ascii?Q?uuK9wLAs972GGbdRUDtYGWiWlr3Bqov9arMoH8RHklTI+rdPtSfZRwys88Ix?= =?us-ascii?Q?VlrYTn7dovi5kfeN6IgO+4ME/BnSu/Nzm8JV42UTNZxV4OSUC1M5nySsaQMP?= =?us-ascii?Q?qfeNMxxEZ5h5IzQu/qnsxFGbOcAzA5uAWKDxWEKUWBKKFIId5SI/f0cTaZAC?= =?us-ascii?Q?awWrS+SJ/flZAmGuMukPWgwirXnJuQQZcjYYGLAqHlgXGvPBoKgkSMqgAXvZ?= =?us-ascii?Q?TysbMvB92gU8RMe9MwSnKQYx+xd0bjWWvVfWlxjTLqi4UFtATaSQs2171RgX?= =?us-ascii?Q?ezcSIDo4E7DcZ+7JNz/kk+2XjCzEzKuSqO0JzDaNo8uCFIC70iqXOT9W8RRq?= =?us-ascii?Q?lS3BnartDXluAjXp5MOPmMMZlRiIJyYIMZR5aGNMXIyj/84e8SnJ+qwwKm5Q?= =?us-ascii?Q?JrEA3Vj0KYsDQXuvUvfpx5BA0Y3qnZwcBpQcg6FrtC5nD8shV96dEqiDUrfU?= =?us-ascii?Q?emGNbfFn0mEi0nSt6y3jCutkjj6u2yqFkUmxNoc1ADL38z6cvTlNFduOZNnA?= =?us-ascii?Q?9ZkspzEvnA9Zawi31L2yv77z88YpJwhI6V2PNgcgi5OWBNO6OfR/mkyHz8v7?= =?us-ascii?Q?LqVX80uryNAVRCr/puMGot2mp+TBJGsDqoi8FuRIPqAJU81yCKTa5SBiGgXj?= =?us-ascii?Q?5QNOPfx2mOw+Xqg06fVbZnCQySgKK99G9SriO1TT326mbcvwjMBmKci/Lkkq?= =?us-ascii?Q?P/Df2sbFnRagm2JBj4O7Wk27V9sz46IblDQCIB6bqg=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?NrUFRRh1XbJy0p5/sDNR6F0dPJbSWXUM6TE+I+OK5RyoevrULJeqgLYKbVRT?= =?us-ascii?Q?ejXjAM2HZP9UcrebV88RWmY0IX+rt6yvexX/8GGOm2cB+0vGRiibwcswDNjq?= =?us-ascii?Q?TDWJz25Nm59ioxvpMFi27H/4cc/d8joUnTFJPoVyeBNhuGzm4W4AbB9F8Kr+?= =?us-ascii?Q?8pCGqzBBwCIBuCSA6+Aql8NnuE8yPMxkz4nn/KluRb20k7fKy5Jtn5mlk/Vv?= =?us-ascii?Q?f7bSHCOeBYgwwmcswIg0lCY3EgW671oKXDJkEh6g+xlEUR8KynQhgi05IFSf?= =?us-ascii?Q?FMyveo0j1vEZROJoRVyLTdjNPRUExf1wYkdB6UtwoiQfB2svjftfoq9x6ofO?= =?us-ascii?Q?Lei9BltkcTcxKgbCbrdHMppIhaOitP7NNSsdNmUOHeipsCnaQRJhIde2fbnc?= =?us-ascii?Q?Hxn7JYz2q5ctwJOBac4hJ3P/CEIhJw0Ezw+TstkvKX8hN6MMa/qhiDL4reh2?= =?us-ascii?Q?7q/ed4UGwntglIygcnprMYQMUe6LuuaWjLVRNdbJckv6aF+fp2dsMiTT2CS3?= =?us-ascii?Q?kgCjiqPt3PwjYWUzgRJv4oYjzc8Rtr1OnHJlnDTb6lVI3AwB0bT/Es3BRrnk?= =?us-ascii?Q?U1BL5Zs2cMRFYcv+63oayDr2NInoySMvPJ1WKYPMuVBmKT9BZnU6FYhYJ3ht?= =?us-ascii?Q?H5nOSQtTENn4q8TJYSDDgVlQedKgmtcuhBk328noteMeYkim0t+tRzE0yHwr?= =?us-ascii?Q?/9iyq2ct+i0Gx3wx+i11KN+fuDpYK0oJudbbwFcm0p1ZQFqih890ULlZvjnh?= =?us-ascii?Q?lEPMoq7mpo/w00k6RYtd+iIhhPkBh6ctfyJuFA6ieOurJfN6djq6SfTmaIkH?= =?us-ascii?Q?fgGESZZpcvfFdO8ahS5pALTb1+o8LbXwlAehyOISnQkQNOW8iMlUzFT2lvzM?= =?us-ascii?Q?3kA6o/yyK9lPhmVzi3hfeVytqWiRm0tg9yJzGGsyEqgg0eLQ9TnaLYs2Xj6J?= =?us-ascii?Q?RN3vFZ4RXESm9wAotrlCcyvIQstedM6neLshr9I0NPT1Jq8DdXbn4FYVUqnb?= =?us-ascii?Q?Tq/UQInk1eKMzHzs6LL/IpRh8AfLk5KS2YJpRIVJU+Mq6pHeJaBaaLfHrT1E?= =?us-ascii?Q?VB1RNH0Bth+WvIqGPBPFK4sVInkvWeJdLrrdSw0/yPyvprmrRFF+8YwNu1ma?= =?us-ascii?Q?PYosYzrqN1TfFiqkE7MD4frJQNPsxP8VR8yGy056S6XQcb4yu+fXgxuKUdIr?= =?us-ascii?Q?Sw92YS5bw+2gDBR0OuIzo2zrenHA3vsqFJvZTUmBAqHTfVoX/xZsn6CBFdIs?= =?us-ascii?Q?fW4lwYeHFEJwO1xd3BeVt71V3dTTh2n6aaWlWpc6xe/sgW11BXyDmzzIASTy?= =?us-ascii?Q?074RQpLGg1dQlUZX0MoGtl7z2t5jcg0jqBx26CMXJ5ycgKjODW1naozA01Oy?= =?us-ascii?Q?8NxaVYhDttKuxZoO24bJb1blA598vSeaDUfXtTME+Eu8B5F0L610AcK6C1KW?= =?us-ascii?Q?VUeZWXOgEJe/G8Tp82z68iygtgTUgJ/bVkbmXWKczheiQu3Qpaom335RdL7T?= =?us-ascii?Q?ghWO1w6Q2ghrvqkvJoi5OptY3VfUwdmV20kxR78OCjYcbkU92XcN5wjDqG0r?= =?us-ascii?Q?sPeS7hPFW/6Ct6fs59E=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: 8a6d2bc0-6be6-482d-9580-08dc7c5a8686 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 May 2024 01:32:22.5302 (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: rTFugfF2hhBs8Z51gAO+jjpKwqz/KuHu21oA9IF2As/nUucfn8JTYpcCMLmiswIqy0uTJvMECpzl12dchiwPQg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB4724 X-OriginatorOrg: intel.com Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 24 May 2024 18:32:26 -0700 Resent-From: ray.ni@intel.com Reply-To: devel@edk2.groups.io,ray.ni@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: NUOJefvRCbG2NC3u2c2gSPb7x7686176AA= Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MN6PR11MB824419777DEB09F8BC5350318CF62MN6PR11MB8244namp_" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=Sv7i0Hq9; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io --_000_MN6PR11MB824419777DEB09F8BC5350318CF62MN6PR11MB8244namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ray Ni Thanks, Ray ________________________________ From: Feng, Ning Sent: Saturday, May 25, 2024 15:42 To: devel@edk2.groups.io Cc: Feng, Ning ; Ni, Ray Subject: [PATCH] UefiCpuPkg/MpLib:Do not assume BSP is #0. REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D4778 MPInitlib have wrong expectation that BSP index should always be 0 in MpInitLibInitialize(), SwitchBsp(),ApWakeupFunction(). That will cause the data mismatch, if the initial BSP is not 0. Cc: Ray Ni Signed-off-by: Ning Feng --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 47 ++++++++++++++++++---------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index d724456502..ba497cbfd9 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -114,6 +114,10 @@ FutureBSPProc ( SaveVolatileRegisters (&DataInHob->APInfo.VolatileRegisters); AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo); RestoreVolatileRegisters (&DataInHob->APInfo.VolatileRegisters, FALSE); + // + // Restore VolatileReg saved in CpuMpData->CpuData + // + CopyMem (&DataInHob->CpuData[DataInHob->BspNumber].VolatileRegisters, &D= ataInHob->APInfo.VolatileRegisters, sizeof (CPU_VOLATILE_REGISTERS)); } /** @@ -761,11 +765,11 @@ ApWakeupFunction ( BistData =3D (UINT32)ApStackData->Bist; // - // CpuMpData->CpuData[0].VolatileRegisters is initialized based on B= SP environment, + // CpuMpData->CpuData[BspNumber].VolatileRegisters is initialized ba= sed on BSP environment, // to initialize AP in InitConfig path. - // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters= points to a different IDT shared by all APs. + // NOTE: IDTR.BASE stored in CpuMpData->CpuData[BspNumber].VolatileR= egisters points to a different IDT shared by all APs. // - RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, = FALSE); + RestoreVolatileRegisters (&CpuMpData->CpuData[CpuMpData->BspNumber].= VolatileRegisters, FALSE); InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack= ); ApStartupSignalBuffer =3D CpuMpData->CpuData[ProcessorNumber].Startu= pApSignal; } else { @@ -798,10 +802,10 @@ ApWakeupFunction ( // 1. AP is re-enabled after it's disabled, in either PEI or DXE p= hase. // 2. AP is initialized in DXE phase. // In either case, use the volatile registers value derived from B= SP. - // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegiste= rs points to a + // NOTE: IDTR.BASE stored in CpuMpData->CpuData[BspNumber].Volatil= eRegisters points to a // different IDT shared by all APs. // - RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters= , FALSE); + RestoreVolatileRegisters (&CpuMpData->CpuData[CpuMpData->BspNumber= ].VolatileRegisters, FALSE); } else { if (CpuMpData->ApLoopMode =3D=3D ApInHltLoop) { // @@ -927,7 +931,7 @@ DxeApEntryPoint ( AsmWriteMsr64 (MSR_IA32_EFER, EferMsr.Uint64); } - RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALS= E); + RestoreVolatileRegisters (&CpuMpData->CpuData[CpuMpData->BspNumber].Vola= tileRegisters, FALSE); InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCount); PlaceAPInMwaitLoopOrRunLoop ( CpuMpData->ApLoopMode, @@ -2151,11 +2155,16 @@ MpInitLibInitialize ( CpuMpData->BackupBufferSize =3D ApResetVectorSizeBelow1Mb; CpuMpData->WakeupBuffer =3D (UINTN)-1; CpuMpData->CpuCount =3D 1; - CpuMpData->BspNumber =3D 0; - CpuMpData->WaitEvent =3D NULL; - CpuMpData->SwitchBspFlag =3D FALSE; - CpuMpData->CpuData =3D (CPU_AP_DATA *)(CpuMpData + 1); - CpuMpData->CpuInfoInHob =3D (UINT64)(UINTN)(CpuMpData->CpuData + Max= LogicalProcessorNumber); + if (FirstMpHandOff =3D=3D NULL) { + CpuMpData->BspNumber =3D 0; + } else { + CpuMpData->BspNumber =3D GetBspNumber (FirstMpHandOff); + } + + CpuMpData->WaitEvent =3D NULL; + CpuMpData->SwitchBspFlag =3D FALSE; + CpuMpData->CpuData =3D (CPU_AP_DATA *)(CpuMpData + 1); + CpuMpData->CpuInfoInHob =3D (UINT64)(UINTN)(CpuMpData->CpuData + MaxLog= icalProcessorNumber); InitializeSpinLock (&CpuMpData->MpLock); CpuMpData->SevEsIsEnabled =3D ConfidentialComputingGuestHas (CCAttrAmd= SevEs); CpuMpData->SevSnpIsEnabled =3D ConfidentialComputingGuestHas (CCAttrAmd= SevSnp); @@ -2186,11 +2195,11 @@ MpInitLibInitialize ( // Don't pass BSP's TR to APs to avoid AP init failure. // VolatileRegisters.Tr =3D 0; - CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &VolatileRegisters, s= izeof (VolatileRegisters)); + CopyMem (&CpuMpData->CpuData[CpuMpData->BspNumber].VolatileRegisters, &V= olatileRegisters, sizeof (VolatileRegisters)); // // Set BSP basic information // - InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSize); + InitializeApData (CpuMpData, CpuMpData->BspNumber, 0, CpuMpData->Buffer = + ApStackSize * (CpuMpData->BspNumber + 1)); // // Save assembly code information // @@ -2245,9 +2254,8 @@ MpInitLibInitialize ( AmdSevUpdateCpuMpData (CpuMpData); } - CpuMpData->CpuCount =3D MaxLogicalProcessorNumber; - CpuMpData->BspNumber =3D GetBspNumber (FirstMpHandOff); - CpuInfoInHob =3D (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoI= nHob; + CpuMpData->CpuCount =3D MaxLogicalProcessorNumber; + CpuInfoInHob =3D (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoIn= Hob; for (MpHandOff =3D FirstMpHandOff; MpHandOff !=3D NULL; MpHandOff =3D GetNextMpHandOffHob (MpHandOff)) @@ -2615,7 +2623,12 @@ SwitchBSPWorker ( SaveVolatileRegisters (&CpuMpData->BSPInfo.VolatileRegisters); AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo); RestoreVolatileRegisters (&CpuMpData->BSPInfo.VolatileRegisters, FALSE); - + // + // Restore VolatileRegs saved in CpuMpData->CpuData + // Don't pass BSP's TR to APs to avoid AP init failure. + // + CopyMem (&CpuMpData->CpuData[CpuMpData->NewBspNumber].VolatileRegisters,= &CpuMpData->BSPInfo.VolatileRegisters, sizeof (CPU_VOLATILE_REGISTERS)); + CpuMpData->CpuData[CpuMpData->NewBspNumber].VolatileRegisters.Tr =3D 0; // // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP // -- 2.25.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 (#119263): https://edk2.groups.io/g/devel/message/119263 Mute This Topic: https://groups.io/mt/106291092/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --_000_MN6PR11MB824419777DEB09F8BC5350318CF62MN6PR11MB8244namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Reviewed-by: Ray Ni <ray.ni@intel.com>

Thanks,
Ray

From: Feng, Ning <ning.f= eng@intel.com>
Sent: Saturday, May 25, 2024 15:42
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Feng, Ning <ning.feng@intel.com>; Ni, Ray <ray.ni@intel= .com>
Subject: [PATCH] UefiCpuPkg/MpLib:Do not assume BSP is #0.
 
REF:https://bugzilla.tianocore.org/show_bug.cgi?id= =3D4778
MPInitlib have wrong expectation that BSP index should always be 0 in
MpInitLibInitialize(), SwitchBsp(),ApWakeupFunction().
That will cause the data mismatch, if the initial BSP is not 0.
Cc: Ray Ni <ray.ni@intel.com>
Signed-off-by: Ning Feng <ning.feng@intel.com>
---
 UefiCpuPkg/Library/MpInitLib/MpLib.c | 47 ++++++++++++++++++---------= -
 1 file changed, 30 insertions(+), 17 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c
index d724456502..ba497cbfd9 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -114,6 +114,10 @@ FutureBSPProc (
   SaveVolatileRegisters (&DataInHob->APInfo.VolatileRegis= ters);

   AsmExchangeRole (&DataInHob->APInfo, &DataInHob->= ;BSPInfo);

   RestoreVolatileRegisters (&DataInHob->APInfo.VolatileRe= gisters, FALSE);

+  //

+  // Restore VolatileReg saved in CpuMpData->CpuData

+  //

+  CopyMem (&DataInHob->CpuData[DataInHob->BspNumber].Volati= leRegisters, &DataInHob->APInfo.VolatileRegisters, sizeof (CPU_VOLAT= ILE_REGISTERS));

 }

 

 /**

@@ -761,11 +765,11 @@ ApWakeupFunction (
       BistData     =3D (= UINT32)ApStackData->Bist;

 

       //

-      // CpuMpData->CpuData[0].VolatileRegiste= rs is initialized based on BSP environment,

+      // CpuMpData->CpuData[BspNumber].Volatil= eRegisters is initialized based on BSP environment,

       //   to initialize AP in Ini= tConfig path.

-      // NOTE: IDTR.BASE stored in CpuMpData->= CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
+      // NOTE: IDTR.BASE stored in CpuMpData->= CpuData[BspNumber].VolatileRegisters points to a different IDT shared by al= l APs.

       //

-      RestoreVolatileRegisters (&CpuMpData-&g= t;CpuData[0].VolatileRegisters, FALSE);

+      RestoreVolatileRegisters (&CpuMpData-&g= t;CpuData[CpuMpData->BspNumber].VolatileRegisters, FALSE);

       InitializeApData (CpuMpData, Processor= Number, BistData, ApTopOfStack);

       ApStartupSignalBuffer =3D CpuMpData-&g= t;CpuData[ProcessorNumber].StartupApSignal;

     } else {

@@ -798,10 +802,10 @@ ApWakeupFunction (
         // 1. AP is re-enabled aft= er it's disabled, in either PEI or DXE phase.

         // 2. AP is initialized in= DXE phase.

         // In either case, use the= volatile registers value derived from BSP.

-        // NOTE: IDTR.BASE stored in Cp= uMpData->CpuData[0].VolatileRegisters points to a

+        // NOTE: IDTR.BASE stored in Cp= uMpData->CpuData[BspNumber].VolatileRegisters points to a

         //   different I= DT shared by all APs.

         //

-        RestoreVolatileRegisters (&= CpuMpData->CpuData[0].VolatileRegisters, FALSE);

+        RestoreVolatileRegisters (&= CpuMpData->CpuData[CpuMpData->BspNumber].VolatileRegisters, FALSE);
       } else {

         if (CpuMpData->ApLoopMo= de =3D=3D ApInHltLoop) {

           //

@@ -927,7 +931,7 @@ DxeApEntryPoint (
     AsmWriteMsr64 (MSR_IA32_EFER, EferMsr.Uint64);

   }

 

-  RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileReg= isters, FALSE);

+  RestoreVolatileRegisters (&CpuMpData->CpuData[CpuMpData->= BspNumber].VolatileRegisters, FALSE);

   InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCou= nt);

   PlaceAPInMwaitLoopOrRunLoop (

     CpuMpData->ApLoopMode,

@@ -2151,11 +2155,16 @@ MpInitLibInitialize (
   CpuMpData->BackupBufferSize =3D ApResetVectorSizeBelow1Mb;<= br>
   CpuMpData->WakeupBuffer     =3D (UINTN)= -1;

   CpuMpData->CpuCount      &nbs= p;  =3D 1;

-  CpuMpData->BspNumber        = =3D 0;

-  CpuMpData->WaitEvent        = =3D NULL;

-  CpuMpData->SwitchBspFlag    =3D FALSE;

-  CpuMpData->CpuData       &nbs= p;  =3D (CPU_AP_DATA *)(CpuMpData + 1);

-  CpuMpData->CpuInfoInHob     =3D (UINT64)(UIN= TN)(CpuMpData->CpuData + MaxLogicalProcessorNumber);

+  if (FirstMpHandOff =3D=3D NULL) {

+    CpuMpData->BspNumber =3D 0;

+  } else {

+    CpuMpData->BspNumber =3D GetBspNumber (FirstMpHandOf= f);

+  }

+

+  CpuMpData->WaitEvent     =3D NULL;

+  CpuMpData->SwitchBspFlag =3D FALSE;

+  CpuMpData->CpuData       =3D (CPU_= AP_DATA *)(CpuMpData + 1);

+  CpuMpData->CpuInfoInHob  =3D (UINT64)(UINTN)(CpuMpData->= CpuData + MaxLogicalProcessorNumber);

   InitializeSpinLock (&CpuMpData->MpLock);

   CpuMpData->SevEsIsEnabled   =3D ConfidentialCompu= tingGuestHas (CCAttrAmdSevEs);

   CpuMpData->SevSnpIsEnabled  =3D ConfidentialComputingG= uestHas (CCAttrAmdSevSnp);

@@ -2186,11 +2195,11 @@ MpInitLibInitialize (
   // Don't pass BSP's TR to APs to avoid AP init failure.

   //

   VolatileRegisters.Tr =3D 0;

-  CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &Vola= tileRegisters, sizeof (VolatileRegisters));

+  CopyMem (&CpuMpData->CpuData[CpuMpData->BspNumber].Volati= leRegisters, &VolatileRegisters, sizeof (VolatileRegisters));

   //

   // Set BSP basic information

   //

-  InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSi= ze);

+  InitializeApData (CpuMpData, CpuMpData->BspNumber, 0, CpuMpData-= >Buffer + ApStackSize * (CpuMpData->BspNumber + 1));

   //

   // Save assembly code information

   //

@@ -2245,9 +2254,8 @@ MpInitLibInitialize (
       AmdSevUpdateCpuMpData (CpuMpData);

     }

 

-    CpuMpData->CpuCount  =3D MaxLogicalProcessorNum= ber;

-    CpuMpData->BspNumber =3D GetBspNumber (FirstMpHandOf= f);

-    CpuInfoInHob       &= nbsp; =3D (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;

+    CpuMpData->CpuCount =3D MaxLogicalProcessorNumber;
+    CpuInfoInHob        = =3D (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;

     for (MpHandOff =3D FirstMpHandOff;

          MpHandOff !=3D NULL;=

          MpHandOff =3D GetNex= tMpHandOffHob (MpHandOff))

@@ -2615,7 +2623,12 @@ SwitchBSPWorker (
   SaveVolatileRegisters (&CpuMpData->BSPInfo.VolatileRegi= sters);

   AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData-&g= t;APInfo);

   RestoreVolatileRegisters (&CpuMpData->BSPInfo.VolatileR= egisters, FALSE);

-

+  //

+  // Restore VolatileRegs saved in CpuMpData->CpuData

+  // Don't pass BSP's TR to APs to avoid AP init failure.

+  //

+  CopyMem (&CpuMpData->CpuData[CpuMpData->NewBspNumber].Vol= atileRegisters, &CpuMpData->BSPInfo.VolatileRegisters, sizeof (CPU_V= OLATILE_REGISTERS));

+  CpuMpData->CpuData[CpuMpData->NewBspNumber].VolatileRegisters= .Tr =3D 0;

   //

   // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP

   //

--
2.25.1

_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#119263) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--_000_MN6PR11MB824419777DEB09F8BC5350318CF62MN6PR11MB8244namp_--