From nobody Mon Nov 25 12:17:31 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+69871+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+69871+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 160996824190218.566864584178916; Wed, 6 Jan 2021 13:24:01 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 4K6GYY1788612xDPnANKSQaz; Wed, 06 Jan 2021 13:24:01 -0800 X-Received: from NAM10-MW2-obe.outbound.protection.outlook.com (NAM10-MW2-obe.outbound.protection.outlook.com [40.107.94.87]) by mx.groups.io with SMTP id smtpd.web10.469.1609968240900160784 for ; Wed, 06 Jan 2021 13:24:01 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VSvc5qLF27b/5+yY0zlMLsd+Qqa5ft8TJXW5lMvFE6bXaEM7Qm3enxc+SEUf3pDjmSldyLG/QirYMynE1CHKo+B7eHlRGTJLNCniJ3SpQ2yrOA12W5SuGDjfgTWmgcw4Ustsv5UULWn4teiG4Int8D+jeDsrpmB2kdQwkzN/qfsV2YwHRCvbQ0FDJxjUNUqjytf25Cq454MUC38vwponoTD9/Po6yGydh2DOqXMBxKfTN4x5Jbcv294t9Ly1xaw9oZmCMD7vo9Gus9jtzNcmtcOtAta4YmyN37CbHUn5xDg9L60Qr5lsmdwvYufb0MWqv0n5J8EUQbkJDgc5ixKcaw== 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=ffHI3AmyEM4stUfCNSJ1IvNLlojYatXSvZa/W83VfSA=; b=m5W+aJo9xtG9FJRoH5TY2AYqERooY7btmYSP2owFGtrAblfl5AUj3c0WUDROh/s2cg6SYh9IWavIKaYFxC225MhDAAm3Kb2fcei/We/lL54e4weXeuTMD/84Her0YkJwJTMVucLpGKv812/uRnL93mR1t2iWUOruo2hgpbjPSauU6489QlfszihiT1jwQyhZJIkO9w5XiHyLmEh0ew2dp3m91IVCSsajNiDP4oRB6V/kyPEcOx2QLy0PpkjFAhFjJBUcTXrV9LQ63CHYxC8PytQ3X0TCk1w0SaNgF6WiUbbAcDzQrnvggXmbRTQh4t9kKQK4MKgMnS/+j6/ti+2nSA== 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 DM5PR12MB1259.namprd12.prod.outlook.com (2603:10b6:3:75::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Wed, 6 Jan 2021 21:23:59 +0000 X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845%12]) with mapi id 15.20.3721.024; Wed, 6 Jan 2021 21:23:59 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Jordan Justen , Laszlo Ersek , Ard Biesheuvel Subject: [edk2-devel] [PATCH v2 13/15] OvmfPkg/VmgExitLib: Support nested #VCs Date: Wed, 6 Jan 2021 15:21:39 -0600 Message-ID: In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN6PR05CA0022.namprd05.prod.outlook.com (2603:10b6:805:de::35) 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 SN6PR05CA0022.namprd05.prod.outlook.com (2603:10b6:805:de::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.2 via Frontend Transport; Wed, 6 Jan 2021 21:23:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 310c2f81-fa8d-4cbd-b7e2-08d8b28961c3 X-MS-TrafficTypeDiagnostic: DM5PR12MB1259: 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: wq6R7RRNC1GrlqEH9DJrI+D4pMV3iEik6Mw34IE2Du+FlXNJ0aiuuCoVx0jE4Y+rMtEMCFkVVA/sKps0eKiSnYnfMKyYV5pgoNQTn7GZn6omK2WWAzj4JC1sLHHQy5Me+Gq74PxR1ZfqBgfJQv+pPBGByvfTkk8yT7fyzRJpBcLVsmbtgfysGrDWR+uuLqSvVdMp8MBKiuFsUjnmnVorgReyk3sfAXdL6Dr4xZEb0uCiawEAurYecPAHw5vT2A2CnsV+G4YvBxZkI1IVqA/lq0l9pe9aH5lY7pg2+s+A5Ray6S13M501MrBmQLM3UY6tgdzVhLRciu2SUO+H4KgoE9yRq9dyejNQTxc6uDbJjPV/pVpThbsngb1IgSoQiX9Dsf0cvxWaJI9/vAp3AT7XSlVrVMCSeYX9EIb3K6NqvoH930XNJgHjt3luQW8a6tOG+FY9hEfU19cA9ca6kPmEvA== X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?ie9VQaIPFQa/D2rw0lrGutPBQnDuWByZ12VoYkuXG6Exw7UO+uZAyZY89s+r?= =?us-ascii?Q?/cwVEPUWGb3scSutkb+WIOmun5gPK0Aus94aohu55vIX6FZAbtsrh1JeaqSv?= =?us-ascii?Q?JBSdxPrqL4bQ3xkAYIYgRNJiNnvxw0Mg86tvwkeuAIW2/YG45v1Z/IbDlwmD?= =?us-ascii?Q?/Q1R58a+ESXXIwYL78G62gBUMZ64zLeF/VftpDmiLlFpHbIzChw6Twd/hwdu?= =?us-ascii?Q?VOBV6TZyI8danB+HpkOS4oFHJkxHgrqJYa0stNXsxNhrJBnvACrcCtLFLJas?= =?us-ascii?Q?bjVGAxbPdHH/E2FIGL31E/DL8jHfXamh1BT/bl+wOCdMzE8gmRURocmqWsWI?= =?us-ascii?Q?yOMJnf9N5EWsztbjhQksY0MGqRisiGnazfNelqHfjf5YU+YHWZP4vwXxHaEv?= =?us-ascii?Q?DZoXOmEwrgLrKw7o3Xg+qt5DVEy5DklAzXXHJJuYZdli4NkmzHHbYAoTlWix?= =?us-ascii?Q?1trR6mlTbzVGHOJxTmIz5j81xuuVAoaS2uVaGva4O9WkjISSvydRqk8w5mFX?= =?us-ascii?Q?paHoQfdoboETQN2vQqZt5P7pyP00+l+1AxVjmjpTqz2NN1MDfEj7HtYIlUKJ?= =?us-ascii?Q?uh1mzs9maxtBKxH9nG/9syQ3EUsuSaaUhISho1+e7QTaQTfOSlkh+fELADtN?= =?us-ascii?Q?V+QRtCYsfff+/8T2r4+4t9h+L56zki57+wZd5EyrPNIfTw40LbpmAe8lKNek?= =?us-ascii?Q?ywXFAgIleG/k4fG1mKLs2rZyakQOkJAT4lN+5tCqNMUngBhuJaPMLj6Nlnty?= =?us-ascii?Q?0K5YepXWygHD4ta8Pvg1sEdhvogt8nI5qzrSQJOUH00SNoZcL/OyDzN+Z5b3?= =?us-ascii?Q?XjEo4SEJDUqFhajb5U8IS9bewFKF48fO6fWKLGID19uiKDHoSZI4jLjzprTM?= =?us-ascii?Q?/kD4ots7WIZZV8MGQKBypev0Epot9e7D4T6Jj9lwlCmSJuyFAGif7jSvP7IR?= =?us-ascii?Q?SvA0zIYXk+93U19baDjX2kBoBWPVFsMXWMhA+WV0tqo6mH5exzj7wkKJDVeZ?= =?us-ascii?Q?ri2A?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jan 2021 21:23:59.2385 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-Network-Message-Id: 310c2f81-fa8d-4cbd-b7e2-08d8b28961c3 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hjII9GoiNWLdsiMrZk8y8bP0S1oX91kkpBrub/YKR1xzj6rmiDgP8vNGBNbvG1BjkoXl3USHgBos1FEZA2lu3A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1259 Precedence: Bulk List-Unsubscribe: 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: xTY0pv1w5clP59Tu6Or9m3bYx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1609968241; bh=9iv6yRR1XNet8es8n6QTft9VLtMF78yGu2ZH9O7yKIc=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=LAo3kVsvNCKyrUhkQr9xo/6o1eJP3ao5WaUYwXizvGOo+VtCz1Bz4YjKqB7vQSr9MDA kzFe0KVDt4+TB2uxOK9+IzBlJnjmfnTsLmYtCxAdXPj19r2xx93jybvxcU/BfbQQUtTNw f/Vegcm+IA7FzW8DExQt4AdlducosyHTAhc= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3108 In order to be able to issue messages or make interface calls that cause another #VC (e.g. GetLocalApicBaseAddress () issues RDMSR), add support for nested #VCs. In order to support nested #VCs, GHCB backup pages are required. If a #VC is received while currently processing a #VC, a backup of the current GHCB content is made. This allows the #VC handler to continue processing the new #VC. Upon completion of the new #VC, the GHCB is restored from the backup page. The #VC recursion level is tracked in the per-vCPU variable area. Support is added to handle up to one nested #VC (or two #VCs total). If a second nested #VC is encountered, an ASSERT will be issued and the vCPU will enter CpuDeadLoop (). For SEC, the GHCB backup pages are reserved in the OvmfPkgX64.fdf memory layout, with two new fixed PCDs to provide the address and size of the backup area. For PEI/DXE, the GHCB backup pages are allocated as boot services pages using the memory allocation library. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Brijesh Singh Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/OvmfPkg.dec | 2 + OvmfPkg/AmdSev/AmdSevX64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/AmdSev/AmdSevX64.fdf | 3 + OvmfPkg/OvmfPkgX64.fdf | 3 + OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf | 43 ++++++++ OvmfPkg/Library/VmgExitLib/VmgExitLib.inf | 4 +- OvmfPkg/Include/Library/MemEncryptSevLib.h | 23 +++++ OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.h | 53 ++++++++++ OvmfPkg/Library/VmgExitLib/PeiDxeVmgExitVcHandler.c | 103 ++++++++++++++++= ++ OvmfPkg/Library/VmgExitLib/SecVmgExitVcHandler.c | 109 ++++++++++++++++= ++++ OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 48 +++++---- OvmfPkg/PlatformPei/AmdSev.c | 38 +++++-- 13 files changed, 404 insertions(+), 27 deletions(-) diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index 50d7b27d941c..54804962ec02 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -304,6 +304,8 @@ [PcdsFixedAtBuild] ## The base address of the SEC GHCB page used by SEV-ES. gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBase|0|UINT32|0x40 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbSize|0|UINT32|0x41 + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase|0|UINT32|0x44 + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupSize|0|UINT32|0x45 =20 ## The base address and size of the SEV Launch Secret Area provisioned # after remote attestation. If this is set in the .fdf, the platform diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index d39436e7afc9..c4d93f39b9f1 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -236,6 +236,7 @@ [LibraryClasses.common.SEC] !else CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiC= puExceptionHandlerLib.inf !endif + VmgExitLib|OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf =20 [LibraryClasses.common.PEI_CORE] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 799468e08e92..bfa9dd7cac1f 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -265,6 +265,7 @@ [LibraryClasses.common.SEC] !else CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiC= puExceptionHandlerLib.inf !endif + VmgExitLib|OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf =20 [LibraryClasses.common.PEI_CORE] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf index e8fd4b8c7b89..c0098502aa90 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.fdf +++ b/OvmfPkg/AmdSev/AmdSevX64.fdf @@ -62,6 +62,9 @@ [FD.MEMFD] 0x00C000|0x001000 gUefiOvmfPkgTokenSpaceGuid.PcdSevLaunchSecretBase|gUefiOvmfPkgTokenSpaceGu= id.PcdSevLaunchSecretSize =20 +0x00D000|0x001000 +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase|gUefiOvmfPkgTokenSpace= Guid.PcdOvmfSecGhcbBackupSize + 0x010000|0x010000 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpace= Guid.PcdOvmfSecPeiTempRamSize =20 diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf index 1a2ef5bf2ae3..d519f8532822 100644 --- a/OvmfPkg/OvmfPkgX64.fdf +++ b/OvmfPkg/OvmfPkgX64.fdf @@ -85,6 +85,9 @@ [FD.MEMFD] 0x00B000|0x001000 gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase|gUefiCpuPkgTokenSpaceGuid.P= cdSevEsWorkAreaSize =20 +0x00C000|0x001000 +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase|gUefiOvmfPkgTokenSpace= Guid.PcdOvmfSecGhcbBackupSize + 0x010000|0x010000 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpace= Guid.PcdOvmfSecPeiTempRamSize =20 diff --git a/OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf b/OvmfPkg/Library= /VmgExitLib/SecVmgExitLib.inf new file mode 100644 index 000000000000..df14de3c21bc --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf @@ -0,0 +1,43 @@ +## @file +# VMGEXIT Support Library. +# +# Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D SecVmgExitLib + FILE_GUID =3D dafff819-f86c-4cff-a70e-83161e5bcf9a + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D VmgExitLib|SEC + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D X64 +# + +[Sources.common] + VmgExitLib.c + VmgExitVcHandler.c + VmgExitVcHandler.h + SecVmgExitVcHandler.c + +[Packages] + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + PcdLib + +[FixedPcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupSize + diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf b/OvmfPkg/Library/Vm= gExitLib/VmgExitLib.inf index d003ac63173e..b3c3e56ecff8 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf @@ -12,7 +12,7 @@ [Defines] FILE_GUID =3D 0e923c25-13cd-430b-8714-ffe85652a97b MODULE_TYPE =3D BASE VERSION_STRING =3D 1.0 - LIBRARY_CLASS =3D VmgExitLib + LIBRARY_CLASS =3D VmgExitLib|PEIM DXE_CORE DXE_DRIVER D= XE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER =20 # # The following information is for reference only and not required by the = build tools. @@ -23,6 +23,8 @@ [Defines] [Sources.common] VmgExitLib.c VmgExitVcHandler.c + VmgExitVcHandler.h + PeiDxeVmgExitVcHandler.c =20 [Packages] MdePkg/MdePkg.dec diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/L= ibrary/MemEncryptSevLib.h index ec470b8d0363..99f15a7d1271 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -13,6 +13,29 @@ =20 #include =20 +// +// Define the maximum number of #VCs allowed (e.g. the level of nesting +// that is allowed =3D> 2 allows for 1 nested #VCs). I this value is chang= ed, +// be sure to increase the size of +// gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupSize +// in any FDF file using this PCD. +// +#define VMGEXIT_MAXIMUM_VC_COUNT 2 + +// +// Per-CPU data mapping structure +// Use UINT32 for cached indicators and compare to a specific value +// so that the hypervisor can't indicate a value is cached by just +// writing random data to that area. +// +typedef struct { + UINT32 Dr7Cached; + UINT64 Dr7; + + UINTN VcCount; + VOID *GhcbBackupPages; +} SEV_ES_PER_CPU_DATA; + // // Internal structure for holding SEV-ES information needed during SEC pha= se // and valid only during SEC phase and early PEI during platform diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.h b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.h new file mode 100644 index 000000000000..3a37cb04f616 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.h @@ -0,0 +1,53 @@ +/** @file + X64 #VC Exception Handler functon header file. + + Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __VMG_EXIT_VC_HANDLER_H__ +#define __VMG_EXIT_VC_HANDLER_H__ + +#include +#include +#include + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + @param[in, out] Ghcb Pointer to the GHCB + @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et + as value to use on error. + @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT + + @retval EFI_SUCCESS Exception handled + @retval EFI_UNSUPPORTED #VC not supported, (new) exception value= to + propagate provided + @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception val= ue to + propagate provided + +**/ +EFI_STATUS +EFIAPI +InternalVmgExitHandleVc ( + IN OUT GHCB *Ghcb, + IN OUT EFI_EXCEPTION_TYPE *ExceptionType, + IN OUT EFI_SYSTEM_CONTEXT SystemContext + ); + +/** + Routine to allow ASSERT from within #VC. + + @param[in, out] SevEsData Pointer to the per-CPU data + +**/ +VOID +EFIAPI +VmgExitIssueAssert ( + IN OUT SEV_ES_PER_CPU_DATA *SevEsData + ); + +#endif diff --git a/OvmfPkg/Library/VmgExitLib/PeiDxeVmgExitVcHandler.c b/OvmfPkg/= Library/VmgExitLib/PeiDxeVmgExitVcHandler.c new file mode 100644 index 000000000000..fb4942df37ca --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/PeiDxeVmgExitVcHandler.c @@ -0,0 +1,103 @@ +/** @file + X64 #VC Exception Handler functon. + + Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include + +#include "VmgExitVcHandler.h" + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et + as value to use on error. + @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT + + @retval EFI_SUCCESS Exception handled + @retval EFI_UNSUPPORTED #VC not supported, (new) exception value= to + propagate provided + @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception val= ue to + propagate provided + +**/ +EFI_STATUS +EFIAPI +VmgExitHandleVc ( + IN OUT EFI_EXCEPTION_TYPE *ExceptionType, + IN OUT EFI_SYSTEM_CONTEXT SystemContext + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + GHCB *GhcbBackup; + EFI_STATUS VcRet; + BOOLEAN InterruptState; + SEV_ES_PER_CPU_DATA *SevEsData; + + InterruptState =3D GetInterruptState (); + if (InterruptState) { + DisableInterrupts (); + } + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + ASSERT (Msr.GhcbInfo.Function =3D=3D 0); + ASSERT (Msr.Ghcb !=3D 0); + + Ghcb =3D Msr.Ghcb; + GhcbBackup =3D NULL; + + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + SevEsData->VcCount++; + + // + // Check for maximum PEI/DXE #VC nesting. + // + if (SevEsData->VcCount > VMGEXIT_MAXIMUM_VC_COUNT) { + VmgExitIssueAssert (SevEsData); + } else if (SevEsData->VcCount > 1) { + // + // Nested #VC + // + if (SevEsData->GhcbBackupPages =3D=3D NULL) { + VmgExitIssueAssert (SevEsData); + } + + // + // Save the active GHCB to a backup page. + // To access the correct backup page, increment the backup page poin= ter + // based on the current VcCount. + // + GhcbBackup =3D (GHCB *) SevEsData->GhcbBackupPages; + GhcbBackup +=3D (SevEsData->VcCount - 2); + + CopyMem (GhcbBackup, Ghcb, sizeof (*Ghcb)); + } + + VcRet =3D InternalVmgExitHandleVc (Ghcb, ExceptionType, SystemContext); + + if (GhcbBackup !=3D NULL) { + // + // Restore the active GHCB from the backup page. + // + CopyMem (Ghcb, GhcbBackup, sizeof (*Ghcb)); + } + + SevEsData->VcCount--; + + if (InterruptState) { + EnableInterrupts (); + } + + return VcRet; +} diff --git a/OvmfPkg/Library/VmgExitLib/SecVmgExitVcHandler.c b/OvmfPkg/Lib= rary/VmgExitLib/SecVmgExitVcHandler.c new file mode 100644 index 000000000000..85853d334b35 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/SecVmgExitVcHandler.c @@ -0,0 +1,109 @@ +/** @file + X64 #VC Exception Handler functon. + + Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include + +#include "VmgExitVcHandler.h" + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et + as value to use on error. + @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT + + @retval EFI_SUCCESS Exception handled + @retval EFI_UNSUPPORTED #VC not supported, (new) exception value= to + propagate provided + @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception val= ue to + propagate provided + +**/ +EFI_STATUS +EFIAPI +VmgExitHandleVc ( + IN OUT EFI_EXCEPTION_TYPE *ExceptionType, + IN OUT EFI_SYSTEM_CONTEXT SystemContext + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + GHCB *GhcbBackup; + EFI_STATUS VcRet; + BOOLEAN InterruptState; + SEV_ES_PER_CPU_DATA *SevEsData; + + InterruptState =3D GetInterruptState (); + if (InterruptState) { + DisableInterrupts (); + } + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + ASSERT (Msr.GhcbInfo.Function =3D=3D 0); + ASSERT (Msr.Ghcb !=3D 0); + + Ghcb =3D Msr.Ghcb; + GhcbBackup =3D NULL; + + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + SevEsData->VcCount++; + + // + // Check for maximum SEC #VC nesting. + // + if (SevEsData->VcCount > VMGEXIT_MAXIMUM_VC_COUNT) { + VmgExitIssueAssert (SevEsData); + } else if (SevEsData->VcCount > 1) { + UINTN GhcbBackupSize; + + // + // Be sure that the proper amount of pages are allocated + // + GhcbBackupSize =3D (VMGEXIT_MAXIMUM_VC_COUNT - 1) * sizeof (*Ghcb); + if (GhcbBackupSize > FixedPcdGet32 (PcdOvmfSecGhcbBackupSize)) { + // + // Not enough SEC backup pages allocated. + // + VmgExitIssueAssert (SevEsData); + } + + // + // Save the active GHCB to a backup page. + // To access the correct backup page, increment the backup page poin= ter + // based on the current VcCount. + // + GhcbBackup =3D (GHCB *) FixedPcdGet32 (PcdOvmfSecGhcbBackupBase); + GhcbBackup +=3D (SevEsData->VcCount - 2); + + CopyMem (GhcbBackup, Ghcb, sizeof (*Ghcb)); + } + + VcRet =3D InternalVmgExitHandleVc (Ghcb, ExceptionType, SystemContext); + + if (GhcbBackup !=3D NULL) { + // + // Restore the active GHCB from the backup page. + // + CopyMem (Ghcb, GhcbBackup, sizeof (*Ghcb)); + } + + SevEsData->VcCount--; + + if (InterruptState) { + EnableInterrupts (); + } + + return VcRet; +} diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index 5149ab2bc989..ce577e4677eb 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -9,11 +9,14 @@ #include #include #include +#include #include #include #include #include =20 +#include "VmgExitVcHandler.h" + // // Instruction execution mode definition // @@ -126,18 +129,6 @@ UINT64 SEV_ES_INSTRUCTION_DATA *InstructionData ); =20 -// -// Per-CPU data mapping structure -// Use UINT32 for cached indicators and compare to a specific value -// so that the hypervisor can't indicate a value is cached by just -// writing random data to that area. -// -typedef struct { - UINT32 Dr7Cached; - UINT64 Dr7; -} SEV_ES_PER_CPU_DATA; - - /** Return a pointer to the contents of the specified register. =20 @@ -1546,6 +1537,7 @@ Dr7ReadExit ( =20 Performs the necessary processing to handle a #VC exception. =20 + @param[in, out] Ghcb Pointer to the GHCB @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et as value to use on error. @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT @@ -1559,14 +1551,13 @@ Dr7ReadExit ( **/ EFI_STATUS EFIAPI -VmgExitHandleVc ( +InternalVmgExitHandleVc ( + IN OUT GHCB *Ghcb, IN OUT EFI_EXCEPTION_TYPE *ExceptionType, IN OUT EFI_SYSTEM_CONTEXT SystemContext ) { - MSR_SEV_ES_GHCB_REGISTER Msr; EFI_SYSTEM_CONTEXT_X64 *Regs; - GHCB *Ghcb; NAE_EXIT NaeExit; SEV_ES_INSTRUCTION_DATA InstructionData; UINT64 ExitCode, Status; @@ -1575,12 +1566,7 @@ VmgExitHandleVc ( =20 VcRet =3D EFI_SUCCESS; =20 - Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); - ASSERT (Msr.GhcbInfo.Function =3D=3D 0); - ASSERT (Msr.Ghcb !=3D 0); - Regs =3D SystemContext.SystemContextX64; - Ghcb =3D Msr.Ghcb; =20 VmgInit (Ghcb, &InterruptState); =20 @@ -1670,3 +1656,25 @@ VmgExitHandleVc ( =20 return VcRet; } + +/** + Routine to allow ASSERT from within #VC. + + @param[in, out] SevEsData Pointer to the per-CPU data + +**/ +VOID +EFIAPI +VmgExitIssueAssert ( + IN OUT SEV_ES_PER_CPU_DATA *SevEsData + ) +{ + // + // Progress will be halted, so set VcCount to allow for ASSERT output + // to be seen. + // + SevEsData->VcCount =3D 0; + + ASSERT (FALSE); + CpuDeadLoop (); +} diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index 954d53eba4e8..dddffdebda4b 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -33,12 +33,17 @@ AmdSevEsInitialize ( VOID ) { - VOID *GhcbBase; - PHYSICAL_ADDRESS GhcbBasePa; - UINTN GhcbPageCount, PageCount; - RETURN_STATUS PcdStatus, DecryptStatus; - IA32_DESCRIPTOR Gdtr; - VOID *Gdt; + UINT8 *GhcbBase; + PHYSICAL_ADDRESS GhcbBasePa; + UINTN GhcbPageCount; + UINT8 *GhcbBackupBase; + UINT8 *GhcbBackupPages; + UINTN GhcbBackupPageCount; + SEV_ES_PER_CPU_DATA *SevEsData; + UINTN PageCount; + RETURN_STATUS PcdStatus, DecryptStatus; + IA32_DESCRIPTOR Gdtr; + VOID *Gdt; =20 if (!MemEncryptSevEsIsEnabled ()) { return; @@ -84,6 +89,27 @@ AmdSevEsInitialize ( "SEV-ES is enabled, %lu GHCB pages allocated starting at 0x%p\n", (UINT64)GhcbPageCount, GhcbBase)); =20 + // + // Allocate #VC recursion backup pages. The number of backup pages neede= d is + // one less than the maximum VC count. + // + GhcbBackupPageCount =3D mMaxCpuCount * (VMGEXIT_MAXIMUM_VC_COUNT - 1); + GhcbBackupBase =3D AllocatePages (GhcbBackupPageCount); + ASSERT (GhcbBackupBase !=3D NULL); + + GhcbBackupPages =3D GhcbBackupBase; + for (PageCount =3D 1; PageCount < GhcbPageCount; PageCount +=3D 2) { + SevEsData =3D + (SEV_ES_PER_CPU_DATA *)(GhcbBase + EFI_PAGES_TO_SIZE (PageCount)); + SevEsData->GhcbBackupPages =3D GhcbBackupPages; + + GhcbBackupPages +=3D EFI_PAGE_SIZE * (VMGEXIT_MAXIMUM_VC_COUNT - 1); + } + + DEBUG ((DEBUG_INFO, + "SEV-ES is enabled, %lu GHCB backup pages allocated starting at 0x%p\n= ", + (UINT64)GhcbBackupPageCount, GhcbBackupBase)); + AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa); =20 // --=20 2.30.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 (#69871): https://edk2.groups.io/g/devel/message/69871 Mute This Topic: https://groups.io/mt/79485080/1787277 Mute #vcs:https://edk2.groups.io/g/devel/mutehashtag/vcs 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-