From nobody Sun May 19 11:31:06 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+75132+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+75132+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one); dmarc=fail(p=none dis=none) header.from=amd.com Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1621024144608567.361598379208; Fri, 14 May 2021 13:29:04 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id NtXRYY1788612xaEf7WiEJbz; Fri, 14 May 2021 13:29:04 -0700 X-Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.72]) by mx.groups.io with SMTP id smtpd.web12.747.1621024138310234454 for ; Fri, 14 May 2021 13:28:58 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R26y2NF+H+C7UCbhvZDYVB+75MiEw6soWPajd8YE/EeTlD2STT1Z8kwTpagEb26XCHgn+Azjv2vx0xN7VvXNByeKYKrjkUyKuyDrgteah/XMDRLCJh4o94aNFVvt+R2l4MAcwNxympqxPUzRwGldbtnoUMqxQuOmmesZgEKWGm8WUsomxVnYBtHIkucOu4Xte+pSSnyf67VCEmSYwwkt+ukMCjJIQoRfIdTOEC7HSWt4s1MR1WD3JDglUx+cVlCF32tE56mKnpihPjMJOqC3FtvvJjTu6uR/5p55QZ8cKhUfuBtuqA2V7MkPHgyGiDDAhO5jTnw4fZ53OaDgKst/Lg== 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-SenderADCheck; bh=gNPt/6ShS+7EBY2nWqNid/kqEiFF+9QJv3ZuOikjyCc=; b=RxDcAYN1EDrwfpy3Kfno8KJkxaFxSgWuOe8TZJJKFy+1xsbFzwJY66Z1qQ6/RfLLpZjfPSDszgkj4Z1rJ7vyNmLVys5xYXRiYklJHYkt9TJxoeV5VQV41POu+nL7wC7j/+8tWj0w5vn0hu029J3Q2TtG3UbZQgsXhiIs8cpWsq6yfrK2YrycV5LAvFBhQIePEoZUylL1u8U3C7HCZWdVITlIPm/veEvDFRmtXtqIDorYn41ALzfefFEdgjjYtGKTUv6J1LtHG794AUjoHqFUwzHJohLZ8bqgIrsbwXhoa9nCx63Wu9NCK0TpNNKXjyyyj83bATf3twVX1TkEJLShAg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB4677.namprd12.prod.outlook.com (2603:10b6:4:a4::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4129.26; Fri, 14 May 2021 20:28:56 +0000 X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9%12]) with mapi id 15.20.4129.025; Fri, 14 May 2021 20:28:56 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , Eric Dong , Ray Ni , Laszlo Ersek , Rahul Kumar , =?UTF-8?q?Marvin=20H=C3=A4user?= Subject: [edk2-devel] [PATCH v2] UefiCpuPkg/MpInitLib: Allocate a separate SEV-ES AP reset stack area Date: Fri, 14 May 2021 15:28:45 -0500 Message-ID: <3cae2ac836884b131725866264e0a0e1897052de.1621024125.git.thomas.lendacky@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR12CA0002.namprd12.prod.outlook.com (2603:10b6:806:6f::7) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-Received: from tlendack-t1.amd.com (165.204.77.1) by SA0PR12CA0002.namprd12.prod.outlook.com (2603:10b6:806:6f::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4108.25 via Frontend Transport; Fri, 14 May 2021 20:28:55 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 61303a40-d951-4583-2d45-08d91716e5bf X-MS-TrafficTypeDiagnostic: DM5PR12MB4677: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: HwcYHJ4QgH79xM3xRy+gjlgutThml6cf/0t/N4CQxaEPZFgnb0Xjw5tUW8sw/v38FCK0gQ0U4kzSbsRCjnsCP4R3gu5Z1Ziv8TOhihAcHst+uc9QG7DNkjBFSWBh0dlw0R8UltpwK1UWKK0py26TR/CXwPSgHhzCEdqtZqmpdDhIIFml/jz20UsYIkIsUVl2S3mX07RCoX4LfqU5EsvyeSJeoVOk/YyTW8qgV3s9xEBz1+RzLoBB8TfcKbloncoyioKxlrKY+A3C23fELDwdFN1RZwkNHRPk/WUfi4oDoCNBt5ZKvVlieuFGwotzIsOg+Sbe8aEntKKKRe1TAH+TB9yVpG/eDJoJYbne0rRRD/KtPiyt9Gq9kL1kcYKKrit37wOXY0ImLaljJIO7dA8ecShKo6YUOsCVRJH7fcRmDNdgsN/t41Q3mLrFujnky6JJCHlV66/AWIKEqCSpyPFhzNinWV5izTS9zZ6ehGggmRhL0Ae/VeU5NtbkYaNlufCTF6sRWKKBwqibC78hL3K/bRSnZAU6d5AV6723n+U7nE9ekpMv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DvvfplRDqEqwrKvS8VSwUaIdGgB1bfjbHS/U3Kb?= =?us-ascii?Q?DXgJyziIulQW/Wa+6dtA2DebWqZmWVtc5+0vGegjffUDz7qgm2HZ7+ptDxmZ?= =?us-ascii?Q?9dEc92anJHu+EkpPqU326FEUekXrCf1JeY+DjEj99V5Yv66ka8M82oWYb5az?= =?us-ascii?Q?T3+3AdePSNkFsBI1lgnpUCRxiJw/7Bki1Qv6pUD1ENc9D1RBK0o186nvB2xu?= =?us-ascii?Q?Da7VYTDBF/E5j6u8o5gQDnFZE1VKsRTS+eAQN/zL31ASUOryBP6jMSx1b/dF?= =?us-ascii?Q?g1gJnx1GP5gn9UhgkefuhiuoGF3w3f9QTAo2Tz6FArzkMskWVJXJYaL/9OX4?= =?us-ascii?Q?0wGesO9/1Hs9eTHPywgnc36SeiiqtDibariSP4GYsOGoOdgTxNMpWeaw9gA9?= =?us-ascii?Q?YGe8E7pehq3YbY5p5GTOARgX1k9TxA3n02u079LvCSSPPRcQ++3oNE0A6zKx?= =?us-ascii?Q?jjDzCSzL40vHQElFWuMRLxA6NLacFojrkSY9GsF4neq/INPgNZWX4FyjiIZ7?= =?us-ascii?Q?+BBeF2WLAy7wrE7Zuj0Q80d/Nk4SQpd3MO9zIxMIIjmD+cZJjQbKhCh6t5zN?= =?us-ascii?Q?ZBUA+H4sw69qDMPZopcSV/yBnI3p60GEdgB4km98kBd0XuVHt6teP9WZaSxU?= =?us-ascii?Q?2ZSU2PrJw+XY2R7UINr3vdnG?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61303a40-d951-4583-2d45-08d91716e5bf X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 May 2021 20:28:56.3455 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: X0LpHLteXhQmKHearZmckw8ta1ZWjENsFCLXggt/Cq0TqsPfgh+gGZsr5TRhMzQDtqPW9/1sw7FioU/SzOuCZw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB4677 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,thomas.lendacky@amd.com X-Gm-Message-State: ccNF0WmqRMVOZT0bSGNttCxrx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1621024144; bh=vz9qo25mGfVCdhH8woZeHCmuHIV1qJPh/6CT4kQCReI=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=TprwCaNVTu2fKm2yC4uuBNmc4dvNccT6Ng95an3/OZ0obESpGu8s1NW668jH1TtXjRU 9/2ta/KXWYPF4j2Uzof1M4dCdnrCc3qMh21Jqy8K0UZWWcu2WYBGX5qafp3tksPKjkjuJ YEwwY24YIlj4u95Ex52NBIckAarxBGqfTzA= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3324 The SEV-ES stacks currently share a page with the reset code and data. Separate the SEV-ES stacks from the reset vector code and data to avoid possible stack overflows from overwriting the code and/or data. When SEV-ES is enabled, invoke the GetWakeupBuffer() routine a second time to allocate a new area, below the reset vector and data. Both the PEI and DXE versions of GetWakeupBuffer() are changed so that when PcdSevEsIsEnabled is true, they will track the previous reset buffer allocation in order to ensure that the new buffer allocation is below the previous allocation. When PcdSevEsIsEnabled is false, the original logic is followed. Fixes: 7b7508ad784d16a5208c8d12dff43aef6df0835b Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Marvin H=C3=A4user Signed-off-by: Tom Lendacky Acked-by: Laszlo Ersek --- Changes since v1: - Renamed the wakeup buffer variables to be unique in the PEI and DXE libraries and to make it obvious they are SEV-ES specific. - Use PcdGetBool (PcdSevEsIsEnabled) to make the changes regression-free so that the new support is only use for SEV-ES guests. --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 19 +++++++- UefiCpuPkg/Library/MpInitLib/MpLib.c | 49 +++++++++++++------- UefiCpuPkg/Library/MpInitLib/PeiMpLib.c | 19 +++++++- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/M= pInitLib/DxeMpLib.c index 7839c249760e..93fc63bf93e3 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -29,6 +29,11 @@ VOID *mReservedApLoopFunc =3D NULL; UINTN mReservedTopOfApStack; volatile UINT32 mNumberToFinish =3D 0; =20 +// +// Begin wakeup buffer allocation below 0x88000 +// +STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer =3D 0x88000; + /** Enable Debug Agent to support source debugging on AP function. =20 @@ -102,7 +107,14 @@ GetWakeupBuffer ( // LagacyBios driver depends on CPU Arch protocol which guarantees below // allocation runs earlier than LegacyBios driver. // - StartAddress =3D 0x88000; + if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // SEV-ES Wakeup buffer should be under 0x88000 and under any previous= one + // + StartAddress =3D mSevEsDxeWakeupBuffer; + } else { + StartAddress =3D 0x88000; + } Status =3D gBS->AllocatePages ( AllocateMaxAddress, MemoryType, @@ -112,6 +124,11 @@ GetWakeupBuffer ( ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { StartAddress =3D (EFI_PHYSICAL_ADDRESS) -1; + } else if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // Next SEV-ES wakeup buffer allocation must be below this allocation + // + mSevEsDxeWakeupBuffer =3D StartAddress; } =20 DEBUG ((DEBUG_INFO, "WakeupBufferStart =3D %x, WakeupBufferSize =3D %x\n= ", diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index dc2a54aa31e8..b9a06747edbf 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -1164,20 +1164,6 @@ GetApResetVectorSize ( AddressMap->SwitchToRealSize + sizeof (MP_CPU_EXCHANGE_INFO); =20 - // - // The AP reset stack is only used by SEV-ES guests. Do not add to the - // allocation if SEV-ES is not enabled. - // - if (PcdGetBool (PcdSevEsIsEnabled)) { - // - // Stack location is based on APIC ID, so use the total number of - // processors for calculating the total stack area. - // - Size +=3D AP_RESET_STACK_SIZE * PcdGet32 (PcdCpuMaxLogicalProcessorNum= ber); - - Size =3D ALIGN_VALUE (Size, CPU_STACK_ALIGNMENT); - } - return Size; } =20 @@ -1192,6 +1178,7 @@ AllocateResetVector ( ) { UINTN ApResetVectorSize; + UINTN ApResetStackSize; =20 if (CpuMpData->WakeupBuffer =3D=3D (UINTN) -1) { ApResetVectorSize =3D GetApResetVectorSize (&CpuMpData->AddressMap); @@ -1207,9 +1194,39 @@ AllocateResetVector ( CpuMpData->AddressMap.ModeTransitionOf= fset ); // - // The reset stack starts at the end of the buffer. + // The AP reset stack is only used by SEV-ES guests. Do not allocate it + // if SEV-ES is not enabled. // - CpuMpData->SevEsAPResetStackStart =3D CpuMpData->WakeupBuffer + ApRese= tVectorSize; + if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // Stack location is based on ProcessorNumber, so use the total numb= er + // of processors for calculating the total stack area. + // + ApResetStackSize =3D (AP_RESET_STACK_SIZE * + PcdGet32 (PcdCpuMaxLogicalProcessorNumber)); + + // + // Invoke GetWakeupBuffer a second time to allocate the stack area + // below 1MB. The returned buffer will be page aligned and sized and + // below the previously allocated buffer. + // + CpuMpData->SevEsAPResetStackStart =3D GetWakeupBuffer (ApResetStackS= ize); + + // + // Check to be sure that the "allocate below" behavior hasn't change= d. + // This will also catch a failed allocation, as "-1" is returned on + // failure. + // + if (CpuMpData->SevEsAPResetStackStart >=3D CpuMpData->WakeupBuffer) { + DEBUG (( + DEBUG_ERROR, + "SEV-ES AP reset stack is not below wakeup buffer\n" + )); + + ASSERT (FALSE); + CpuDeadLoop (); + } + } } BackupAndPrepareWakeupBuffer (CpuMpData); } diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c b/UefiCpuPkg/Library/M= pInitLib/PeiMpLib.c index 3989bd6a7a9f..90015c650c68 100644 --- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c @@ -11,6 +11,8 @@ #include #include =20 +STATIC UINT64 mSevEsPeiWakeupBuffer =3D BASE_1MB; + /** S3 SMM Init Done notification function. =20 @@ -220,7 +222,13 @@ GetWakeupBuffer ( // Need memory under 1MB to be collected here // WakeupBufferEnd =3D Hob.ResourceDescriptor->PhysicalStart + Hob.Re= sourceDescriptor->ResourceLength; - if (WakeupBufferEnd > BASE_1MB) { + if (PcdGetBool (PcdSevEsIsEnabled) && + WakeupBufferEnd > mSevEsPeiWakeupBuffer) { + // + // SEV-ES Wakeup buffer should be under 1MB and under any previo= us one + // + WakeupBufferEnd =3D mSevEsPeiWakeupBuffer; + } else if (WakeupBufferEnd > BASE_1MB) { // // Wakeup buffer should be under 1MB // @@ -244,6 +252,15 @@ GetWakeupBuffer ( } DEBUG ((DEBUG_INFO, "WakeupBufferStart =3D %x, WakeupBufferSize = =3D %x\n", WakeupBufferStart, WakeupBufferSize)); + + if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // Next SEV-ES wakeup buffer allocation must be below this + // allocation + // + mSevEsPeiWakeupBuffer =3D WakeupBufferStart; + } + return (UINTN)WakeupBufferStart; } } --=20 2.31.0 -=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 (#75132): https://edk2.groups.io/g/devel/message/75132 Mute This Topic: https://groups.io/mt/82833645/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-