From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.63]) by mx.groups.io with SMTP id smtpd.web12.2357.1636582521002060095 for ; Wed, 10 Nov 2021 14:15:21 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=LRZFtKok; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.92.63, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bX11HAvYAEJVGj+Xecn8bLUP5SZGvHEWHRopmh2+hMyUFEgqfDaqZZs+FFaqgHbXU4+iHPnlsr/bR0P+lEKNRYIxIkwFgnW6HR+eGqEWl+1wYqjnZJPeoaEfgPEnkKzEFzjtA16jFn96oNSzVingeHzigf9jKlwbHxTwIGzwJ8d9kr4lHLkcH3FOQspeObOyfjwhBaglxlepSJq4otQQZojqRmNf0U2hMwr/0TZFIwjubCJKwvmDFxpkXtBTWO60CQ9teZnZuU5qnEheqLV08hcCUDexcAGpki8L1WQD/6fbAHMNErUn4z/BgtdiHg2miBgqepMu0EdL/YoFfMQyog== 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=bMqzMpHGTRGBDWtYLicLCZ8c92fCQmBUuOqJdRIj8JM=; b=aDM6P8j0WpNHNjf1nB+6ny+hKuI4RqymNPb1uB+hUJeN+1qRql7wgJHeB/Ll9wNGMlx5iBLOTsJjthj/Gj86pHUweMbVxiTm71VVEcJSR6G1je15CJ7qKlQqeVWicRzZsp0NO7TrtHYiik1zhXxPaluD0XPQEe/r5e77vcM7dRVSyT4V/VQk3i3KY1m3Gh5ygLczGPnGmg1ouBIY8+ogyssWlEgnaV6MEIdsgFB6SKZC1Lwg5HBQcb+jI1yQvg5wE3+0/C57EF1jEskiLX8KIOd1iyzE1vlqr8Xa6l6jNrXBEGMJiwOIj60K7rXZuzK22YQwW8Fod8+FTWEEXn2kKA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bMqzMpHGTRGBDWtYLicLCZ8c92fCQmBUuOqJdRIj8JM=; b=LRZFtKokybyZp0zKOk/PaxZmjIBz6sKKjh8RoX8lKYuQ094zcV//fbmRezqavbbSmxMHex3DKMy12TtbF4jiO7xIlIDIltsGo5JT6Peihgg/YXNNd15aJS4xURuXYp92hLMJuHG1l35y3XLGO27tJEaM76U7I0UJMxfPxj2zYkg= Received: from BN0PR03CA0044.namprd03.prod.outlook.com (2603:10b6:408:e7::19) by BL0PR12MB4609.namprd12.prod.outlook.com (2603:10b6:208:8d::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4690.16; Wed, 10 Nov 2021 22:15:19 +0000 Received: from BN8NAM11FT058.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e7:cafe::35) by BN0PR03CA0044.outlook.office365.com (2603:10b6:408:e7::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.13 via Frontend Transport; Wed, 10 Nov 2021 22:15:19 +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; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT058.mail.protection.outlook.com (10.13.177.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4690.15 via Frontend Transport; Wed, 10 Nov 2021 22:15:18 +0000 Received: from sbrijesh-desktop.amd.com (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.2375.17; Wed, 10 Nov 2021 16:15:17 -0600 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , "Michael D Kinney" , Liming Gao , Zhiguang Liu , Ray Ni , Rahul Kumar , Eric Dong , Brijesh Singh , Michael Roth , Jiewen Yao Subject: [PATCH v12 10/32] OvmfPkg/SecMain: register GHCB gpa for the SEV-SNP guest Date: Wed, 10 Nov 2021 16:14:35 -0600 Message-ID: <20211110221457.2397234-11-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211110221457.2397234-1-brijesh.singh@amd.com> References: <20211110221457.2397234-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 52faf052-19ee-4808-1ac6-08d9a4979480 X-MS-TrafficTypeDiagnostic: BL0PR12MB4609: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yOi4gFQGORK3nheC8NnxSLv3jA01T0d+PH1SGNgi7fjCS94qbHbtRhT3w5aBxvDYAdh70l5/jF6DwDZ3/rSfSNZVMYaweWesPs11qoCdwlJA9JUFv0kPri1BvgObKjcCMHwpQa+M68afyyM2iJAAE8iukKHYou2iBOojIee7gIHMDtxihWcg9EHfa0B5djRSMq9YdftwIE705irtMOupHXtOnGPjDcNQa8VcjbwT2Pb08mjXfgPyqlw6sedV6X6OnCNx8tk7JrP8W3RPVJVuW221zBfxxs6fcUoWOzZAg+P4nx9/fgBx2zTl2rWvDf0cL0QNKOSgzb5TsYa5PiMbyXPWnRNvi3DcvSMz2S8/q779rT4He54ViNWd26T5LNoQcKlPLI+joPfcNwRUAMlovWQTzOro+FrJyGP567a9rQz3XALcwOejFs34JTV9opg4ooeMx8L77nypkPfFMqsBUN794dizfp2kTsP2X3iP+CpQA8aBStoFtYnf0NIVKpOTgMdC5CWeNH+C/CoO4JAGD0uh+ofiHb/tZRIRdfh4vsWfprBC1ywg/3aw3fRPrSRsCpD4XQfMnLui9J4dZ4rIwX8j/NUvRBdAHx2kpN60xFQGFubqXuw0u6VQw/FcDltQ9P+wR6GRSOnDz9OL4T9nTU1dqFYlyJnAT+wHAKW0xrRlwA48FU2h3DKVLOrBeQ1Dv+ot3P8DqYf+Kthl1Osfuu+eUPO0gbzOvphfyuIm0saJ+sqPdngqdvyMX5697sOy6QG+/1/jLBrYZFbe28vdCfYtV2IgrbQflxb/wmCgCCA88QejUT0KYkx0YTYI8fHn X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(44832011)(316002)(82310400003)(26005)(356005)(2616005)(5660300002)(6916009)(54906003)(7416002)(86362001)(36860700001)(36756003)(2906002)(70586007)(6666004)(8676002)(7696005)(186003)(83380400001)(4326008)(70206006)(966005)(1076003)(47076005)(8936002)(426003)(508600001)(336012)(16526019)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Nov 2021 22:15:18.7646 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 52faf052-19ee-4808-1ac6-08d9a4979480 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: BN8NAM11FT058.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4609 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The SEV-SNP guest requires that GHCB GPA must be registered before using. See the GHCB specification section 2.3.2 for more details. Cc: Michael Roth Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Acked-by: Jiewen Yao Acked-by: Gerd Hoffmann Signed-off-by: Brijesh Singh --- OvmfPkg/Sec/AmdSev.c | 117 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/OvmfPkg/Sec/AmdSev.c b/OvmfPkg/Sec/AmdSev.c index 7f74e8bfe88e..58f054ea4418 100644 --- a/OvmfPkg/Sec/AmdSev.c +++ b/OvmfPkg/Sec/AmdSev.c @@ -48,6 +48,102 @@ SevEsProtocolFailure ( CpuDeadLoop (); } =20 +/** + Determine if SEV-SNP is active. + + @retval TRUE SEV-SNP is enabled + @retval FALSE SEV-SNP is not enabled + +**/ +STATIC +BOOLEAN +SevSnpIsEnabled ( + VOID + ) +{ + MSR_SEV_STATUS_REGISTER Msr; + + // + // Read the SEV_STATUS MSR to determine whether SEV-SNP is active. + // + Msr.Uint32 =3D AsmReadMsr32 (MSR_SEV_STATUS); + + // + // Check MSR_0xC0010131 Bit 2 (Sev-Snp Enabled) + // + if (Msr.Bits.SevSnpBit) { + return TRUE; + } + + return FALSE; +} + +/** + Register the GHCB GPA + +*/ +STATIC +VOID +SevSnpGhcbRegister ( + EFI_PHYSICAL_ADDRESS Address + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + + // + // Use the GHCB MSR Protocol to request to register the GPA. + // + Msr.GhcbPhysicalAddress =3D Address & ~EFI_PAGE_MASK; + Msr.GhcbGpaRegister.Function =3D GHCB_INFO_GHCB_GPA_REGISTER_REQUEST; + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + AsmVmgExit (); + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + + // + // If hypervisor responded with a different GPA than requested then fail= . + // + if ((Msr.GhcbGpaRegister.Function !=3D GHCB_INFO_GHCB_GPA_REGISTER_RESPO= NSE) || + ((Msr.GhcbPhysicalAddress & ~EFI_PAGE_MASK) !=3D Address)) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_GENERAL); + } +} + +/** + Verify that Hypervisor supports the SNP feature. + + */ +STATIC +BOOLEAN +HypervisorSnpFeatureCheck ( + VOID + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + UINT64 Features; + + // + // Use the GHCB MSR Protocol to query the hypervisor capabilities + // + Msr.GhcbPhysicalAddress =3D 0; + Msr.GhcbHypervisorFeatures.Function =3D GHCB_HYPERVISOR_FEATURES_REQUEST= ; + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + AsmVmgExit (); + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + + Features =3D RShiftU64 (Msr.GhcbPhysicalAddress, 12); + + if ((Msr.GhcbHypervisorFeatures.Function !=3D GHCB_HYPERVISOR_FEATURES_R= ESPONSE) || + (!(Features & GHCB_HV_FEATURES_SNP))) { + return FALSE; + } + + return TRUE; +} + /** Validate the SEV-ES/GHCB protocol level. =20 @@ -88,6 +184,27 @@ SevEsProtocolCheck ( SevEsProtocolFailure (GHCB_TERMINATE_GHCB_PROTOCOL); } =20 + // + // We cannot use the MemEncryptSevSnpIsEnabled () because the + // ProcessLibraryConstructorList () is not called yet. + // + if (SevSnpIsEnabled ()) { + // + // Check if hypervisor supports the SNP feature + // + if (!HypervisorSnpFeatureCheck ()) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_PROTOCOL); + } + + // + // Unlike the SEV-ES guest, the SNP requires that GHCB GPA must be + // registered with the Hypervisor before the use. This can be done + // using the new VMGEXIT defined in the GHCB v2. Register the GPA + // before it is used. + // + SevSnpGhcbRegister ((EFI_PHYSICAL_ADDRESS) (UINTN) FixedPcdGet32 (PcdO= vmfSecGhcbBase)); + } + // // SEV-ES protocol checking succeeded, set the initial GHCB address // --=20 2.25.1