From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id A09CF9414D6 for ; Fri, 8 Mar 2024 15:30:35 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=dUE2BbriPRhkssTxIs3+aiLYCKwYJPbloZieZ8GZ8uY=; c=relaxed/simple; d=groups.io; h=Received-SPF:From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20240206; t=1709911834; v=1; b=Ioi/TDiiBrOBREd3LhYxjH1T7ZKMk/XuBhzXuhSb1OflGHOeFwna3E2aUvtVpM2XlqvXeAfi 3LlZDMEqTASu7WMRxwZ/J/hVlGjmcZjZ1khxhYvPcLHF4lCW1tGkf1UbKzzQ2PLt4XESqgwjXNZ y4+CVDRSd4rft7BqqA0M5XnNsE1ZLV/P2lhicyk2ZfSs8elC9/PnN+vVIeqcrV2+ir5DAZVweU+ R/u6MHL0WLGloSze91gPdalHSFBDZy7tCv42tYUUgfVL9sFjsWY94Fr7JorVoqrTgTrQ7kZ2+/p zCO9RVNGenEFWQPPGIy/rtDgbsNGhJI7RBtJ0nAE7W+ew== X-Received: by 127.0.0.2 with SMTP id ZoCDYY7687511xpdO7VVJnJt; Fri, 08 Mar 2024 07:30:34 -0800 X-Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com [40.107.236.41]) by mx.groups.io with SMTP id smtpd.web11.24821.1709911832849889054 for ; Fri, 08 Mar 2024 07:30:33 -0800 X-Received: from DM6PR07CA0088.namprd07.prod.outlook.com (2603:10b6:5:337::21) by CH3PR12MB8995.namprd12.prod.outlook.com (2603:10b6:610:17e::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.24; Fri, 8 Mar 2024 15:30:26 +0000 X-Received: from CY4PEPF0000E9D8.namprd05.prod.outlook.com (2603:10b6:5:337:cafe::3f) by DM6PR07CA0088.outlook.office365.com (2603:10b6:5:337::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.27 via Frontend Transport; Fri, 8 Mar 2024 15:30:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C X-Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D8.mail.protection.outlook.com (10.167.241.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7362.11 via Frontend Transport; Fri, 8 Mar 2024 15:30:24 +0000 X-Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Fri, 8 Mar 2024 09:30:23 -0600 From: "Lendacky, Thomas via groups.io" To: CC: Ard Biesheuvel , Erdem Aktas , Gerd Hoffmann , Jiewen Yao , Laszlo Ersek , Liming Gao , Michael D Kinney , Min Xu , Zhiguang Liu , "Rahul Kumar" , Ray Ni , Michael Roth Subject: [edk2-devel] [PATCH v3 03/24] UefiCpuPkg/MpInitLib: Always use AP Create if GhcbApicIds HOB is present Date: Fri, 08 Mar 2024 07:30:33 -0800 Message-ID: <6523f9e8c6d12fca800eb0db4eba6d763c074c27.1709911792.git.thomas.lendacky@amd.com> In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D8:EE_|CH3PR12MB8995:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a71eff2-1d63-4833-fba5-08dc3f84ad04 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: 8M7ESfrbbSjqgcOSyR6cZ3P7WnNyzU2l/cbvuau70Ael0hmk0MiEz1pQK4YLv9+IDHPoy6Wx+mxVR3WIHg446d5osTp2OnocGJBBj6YI5J0g/5vWxJjmnILcGpilU19oTBKNYgHPdEAapXVFVlx9yqIFnhMA3qyHs/sP/CUtjJTKtz7NzqImBBhpzAnJBgcXU1fHkfq6pew6Kiw52i6KTfz9UuAN5z0R2LuakhRaVKLmp0gpoHeJy10V5bVGR2Rd4czTMkn2g0sGwWX0BvlRAMtnLP3T3oG3IBz3HtIk/WtDE/lXljuqgt5Lbexp6nmjOimogb0/qbyWv1k0NgQeCpRCJzdx0lAFdQ4kdHXctyFhyA3kONneTpVRFrIzy8z06fY07CpkYsU4OeiizJ2f6eaW6XITROwKIdfJbcEA4BOCmdk91xOp2YxxHEbcxDr5a81OHUjN3+K/LrO9WZWl8vdWKdOdgBjoJlWmVxcQtQlyIJfrKtV78tTvJlt/HBPjPWGKVF63BxBSZw4nNRNtdZ+KCg6QrOBbSc5zPz7DR2q6tf1pYkG0JJXMgQARw9DX+IyqJAlShYBVJ9O/y6JIObyRQhZ0mTgGVDGarNexcvntaN75X1ow6n6qnIV7og6TIcrkaxPaoKkMB7U/pp3B2+XsLdXM7q9mja0/ickn7Vbubl/gxbilXFxKj6vwe8WmyeRvMyGaSJiigZnZqOyB3Q== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2024 15:30:24.9067 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1a71eff2-1d63-4833-fba5-08dc3f84ad04 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8995 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 Reply-To: devel@edk2.groups.io,thomas.lendacky@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: eFzJKhIvF5aPSOydfQFm7zsnx7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b="Ioi/TDii"; dmarc=pass (policy=none) header.from=groups.io; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4654 Currently, the first time an AP is started for an SEV-SNP guest, it relies on the VMSA as set by the hypervisor. If the list of APIC IDs has been retrieved, this is not necessary. The list of APIC IDs will be identified by a GUIDed HOB. If the GUIDed HOB is present, use the SEV-SNP AP Create protocol to start the AP for the first time and each time thereafter. Cc: Gerd Hoffmann Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Ray Ni Reviewed-by: Gerd Hoffmann Signed-off-by: Tom Lendacky --- UefiCpuPkg/UefiCpuPkg.dec | 5 +- UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf | 1 + UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf | 1 + UefiCpuPkg/Include/Guid/GhcbApicIds.h | 17 +++++ UefiCpuPkg/Library/MpInitLib/MpLib.h | 15 +++- UefiCpuPkg/Library/MpInitLib/Ia32/AmdSev.c | 21 +++++- UefiCpuPkg/Library/MpInitLib/MpLib.c | 9 ++- UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c | 78 ++++++++++++++++++-- 8 files changed, 133 insertions(+), 14 deletions(-) diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index 571b59b36f0a..c31d8b6736cf 100644 --- a/UefiCpuPkg/UefiCpuPkg.dec +++ b/UefiCpuPkg/UefiCpuPkg.dec @@ -2,7 +2,7 @@ # This Package provides UEFI compatible CPU modules and libraries. # # Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.
-# Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.
+# Copyright (C) 2023 - 2024, Advanced Micro Devices, Inc. All rights reser= ved.
# # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -91,6 +91,9 @@ [Guids] ## Include/Guid/MpInformation2.h gMpInformation2HobGuid =3D { 0x417a7f64, 0xf4e9, 0x4b32, {0x84, = 0x6a, 0x5c, 0xc4, 0xd8, 0x62, 0x18, 0x79 }} =20 + ## Include/Guid/GhcbApicIds.h + gGhcbApicIdsGuid =3D { 0xbc964338, 0xee39, 0x4fc8, { 0xa2,= 0x24, 0x10, 0x10, 0x8b, 0x17, 0x80, 0x1b }} + [Protocols] ## Include/Protocol/SmmCpuService.h gEfiSmmCpuServiceProtocolGuid =3D { 0x1d202cab, 0xc8ab, 0x4d5c, { 0x94= , 0xf7, 0x3c, 0xfc, 0xc0, 0xd3, 0xd3, 0x35 }} diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf b/UefiCpuPkg/Lib= rary/MpInitLib/DxeMpInitLib.inf index 55e46d4a1fad..69950fcd1289 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf @@ -68,6 +68,7 @@ [Guids] gEfiEventExitBootServicesGuid ## CONSUMES ## Event gEfiEventLegacyBootGuid ## SOMETIMES_CONSUMES ## = Event gEdkiiMicrocodePatchHobGuid ## SOMETIMES_CONSUMES ## = HOB + gGhcbApicIdsGuid ## SOMETIMES_CONSUMES ## = HOB =20 [Pcd] gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber ## = CONSUMES diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf b/UefiCpuPkg/Lib= rary/MpInitLib/PeiMpInitLib.inf index bc3d716aa951..22f74a814534 100644 --- a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf +++ b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf @@ -76,3 +76,4 @@ [Ppis] [Guids] gEdkiiS3SmmInitDoneGuid gEdkiiMicrocodePatchHobGuid + gGhcbApicIdsGuid ## SOMETIMES_CONSUMES diff --git a/UefiCpuPkg/Include/Guid/GhcbApicIds.h b/UefiCpuPkg/Include/Gui= d/GhcbApicIds.h new file mode 100644 index 000000000000..9d5bfcb0de22 --- /dev/null +++ b/UefiCpuPkg/Include/Guid/GhcbApicIds.h @@ -0,0 +1,17 @@ +/** @file + APIC ID list retrieved for an SEV-ES/SEV-SNP guest via the GHCB. + + Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef GHCB_APIC_IDS_H_ +#define GHCB_APIC_IDS_H_ + +#define GHCB_APIC_IDS_GUID \ + { 0xbc964338, 0xee39, 0x4fc8, { 0xa2, 0x24, 0x10, 0x10, 0x8b, 0x17, 0x80= , 0x1b }} + +extern EFI_GUID gGhcbApicIdsGuid; + +#endif diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index d26035559f22..65e05c4806f5 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -2,7 +2,7 @@ Common header file for MP Initialize Library. =20 Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.
- Copyright (c) 2020, AMD Inc. All rights reserved.
+ Copyright (c) 2020 - 2024, AMD Inc. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -924,6 +924,19 @@ SevSnpCreateAP ( IN INTN ProcessorNumber ); =20 +/** + Determine if the SEV-SNP AP Create protocol should be used. + + @param[in] CpuMpData Pointer to CPU MP Data + + @retval TRUE Use SEV-SNP AP Create protocol + @retval FALSE Do not use SEV-SNP AP Create protocol +**/ +BOOLEAN +CanUseSevSnpCreateAP ( + IN CPU_MP_DATA *CpuMpData + ); + /** Get pointer to CPU MP Data structure from GUIDed HOB. =20 diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/AmdSev.c b/UefiCpuPkg/Librar= y/MpInitLib/Ia32/AmdSev.c index c83144285b68..0478e92317f1 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/AmdSev.c +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/AmdSev.c @@ -2,7 +2,7 @@ =20 AMD SEV helper function. =20 - Copyright (c) 2021, AMD Incorporated. All rights reserved.
+ Copyright (c) 2021 - 2024, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -68,3 +68,22 @@ SevSnpRmpAdjust ( // return RETURN_UNSUPPORTED; } + +/** + Determine if the SEV-SNP AP Create protocol should be used. + + @param[in] CpuMpData Pointer to CPU MP Data + + @retval TRUE Use SEV-SNP AP Create protocol + @retval FALSE Do not use SEV-SNP AP Create protocol +**/ +BOOLEAN +CanUseSevSnpCreateAP ( + IN CPU_MP_DATA *CpuMpData + ) +{ + // + // SEV-SNP is not supported on 32-bit build. + // + return FALSE; +} diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 9bac62f289e0..d7244565029d 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -2,7 +2,7 @@ CPU MP Initialize Library common functions. =20 Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.
- Copyright (c) 2020, AMD Inc. All rights reserved.
+ Copyright (c) 2020 - 2024, AMD Inc. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -1303,9 +1303,10 @@ WakeUpAP ( // // Wakeup all APs // Must use the INIT-SIPI-SIPI method for initial configuration in - // order to obtain the APIC ID. + // order to obtain the APIC ID if not an SEV-SNP guest and the + // list of APIC IDs is not available. // - if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag !=3D ApInitCo= nfig)) { + if (CanUseSevSnpCreateAP (CpuMpData)) { SevSnpCreateAP (CpuMpData, -1); } else { if ((CpuMpData->InitFlag =3D=3D ApInitConfig) && FixedPcdGetBool (= PcdFirstTimeWakeUpAPsBySipi)) { @@ -1415,7 +1416,7 @@ WakeUpAP ( SetSevEsJumpTable (ExchangeInfo->BufferStart); } =20 - if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag !=3D ApInitCo= nfig)) { + if (CanUseSevSnpCreateAP (CpuMpData)) { SevSnpCreateAP (CpuMpData, (INTN)ProcessorNumber); } else { SendInitSipiSipi ( diff --git a/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c b/UefiCpuPkg/Library= /MpInitLib/X64/AmdSev.c index c9f0984f41a2..bd12a5ee2fcb 100644 --- a/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c +++ b/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c @@ -2,7 +2,7 @@ =20 AMD SEV helper function. =20 - Copyright (c) 2021, AMD Incorporated. All rights reserved.
+ Copyright (c) 2021 - 2024, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -268,20 +268,55 @@ SevSnpCreateAP ( IN INTN ProcessorNumber ) { - CPU_INFO_IN_HOB *CpuInfoInHob; - CPU_AP_DATA *CpuData; - UINTN Index; - UINT32 ApicId; + CPU_INFO_IN_HOB *CpuInfoInHob; + CPU_AP_DATA *CpuData; + UINTN Index; + UINTN MaxIndex; + UINT32 ApicId; + EFI_HOB_GUID_TYPE *GuidHob; + GHCB_APIC_IDS *GhcbApicIds; =20 ASSERT (CpuMpData->MpCpuExchangeInfo->BufferStart < 0x100000); =20 CpuInfoInHob =3D (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; =20 if (ProcessorNumber < 0) { - for (Index =3D 0; Index < CpuMpData->CpuCount; Index++) { + if (CpuMpData->InitFlag =3D=3D ApInitConfig) { + // + // APs have not been started, so CpuCount is not "known" yet. Use th= e + // retrieved APIC IDs to start the APs and fill out the MpLib CPU + // information properly. CanUseSevSnpCreateAP() guarantees we have a + // HOB when InitFlag is ApInitConfig. + // + GuidHob =3D GetFirstGuidHob (&gGhcbApicIdsGuid); + GhcbApicIds =3D (GHCB_APIC_IDS *)(*(UINTN *)GET_GUID_HOB_DATA (GuidH= ob)); + MaxIndex =3D MIN (GhcbApicIds->NumEntries, PcdGet32 (PcdCpuMaxLog= icalProcessorNumber)); + } else { + // + // APs have been previously started. + // + MaxIndex =3D CpuMpData->CpuCount; + } + + for (Index =3D 0; Index < MaxIndex; Index++) { if (Index !=3D CpuMpData->BspNumber) { CpuData =3D &CpuMpData->CpuData[Index]; - ApicId =3D CpuInfoInHob[Index].ApicId, + + if (CpuMpData->InitFlag =3D=3D ApInitConfig) { + ApicId =3D GhcbApicIds->ApicIds[Index]; + + // + // For the first boot, use the BSP register information. + // + CopyMem ( + &CpuData->VolatileRegisters, + &CpuMpData->CpuData[0].VolatileRegisters, + sizeof (CpuData->VolatileRegisters) + ); + } else { + ApicId =3D CpuInfoInHob[Index].ApicId; + } + SevSnpCreateSaveArea (CpuMpData, CpuData, ApicId); } } @@ -325,3 +360,32 @@ SevSnpRmpAdjust ( =20 return AsmRmpAdjust ((UINT64)PageAddress, 0, Rdx); } + +/** + Determine if the SEV-SNP AP Create protocol should be used. + + @param[in] CpuMpData Pointer to CPU MP Data + + @retval TRUE Use SEV-SNP AP Create protocol + @retval FALSE Do not use SEV-SNP AP Create protocol +**/ +BOOLEAN +CanUseSevSnpCreateAP ( + IN CPU_MP_DATA *CpuMpData + ) +{ + // + // The AP Create protocol is used for an SEV-SNP guest if + // - The initial configuration has been performed already or + // - The APIC IDs GUIDed HOB is non-zero. + // + if (!CpuMpData->SevSnpIsEnabled) { + return FALSE; + } + + if ((CpuMpData->InitFlag =3D=3D ApInitConfig) && (GetFirstGuidHob (&gGhc= bApicIdsGuid) =3D=3D NULL)) { + return FALSE; + } + + return TRUE; +} --=20 2.43.2 -=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 (#116528): https://edk2.groups.io/g/devel/message/116528 Mute This Topic: https://groups.io/mt/104810684/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-