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.41]) by mx.groups.io with SMTP id smtpd.web12.114.1634144273424880717 for ; Wed, 13 Oct 2021 09:57:53 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=eV+f7pnx; 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.41, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S/UkIi/qLjrP4XqXeyCl80rrRG1TTPMzmUmZR9/2KQdWDlsGha5Q/sLaMQQZ93lqZYfhI3LZ4XyEZtcVJlzXWs+P7TqqJXgXJVbLDrjRV9TPv4DvciidBWX7ou+vbmcEzrY+UrK0kMnA4SzhThuweoc2L/oRU26WWb/cw0KwXssKxOMVU06Z2R5f/rLwU3HhSRHH08fDCUdZTaazBrPuaXLjxUmG1vCufAb044gDCnhI7baTkras3pNcDyXVJzGqcp9IZYaRmfoP09JnXnIHdLgDBaM0+QZbXCMxdDV6X1WsNI6H7sjhpy13mippq4mM2Uxb1GuhL0wJksDcHz7ppA== 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=HF2W9/+B9SXNSLgA1UL2ZD5ts2exPEOIUKLe7KETuMA=; b=HBn17RS5Z3SuCvmADip63wiRcb0rDCnCd7XjaJfOx6mqIehJI1Xrm/11vllrLfMHnfKsF3123rDUJ5CBJfndrS8L1qOb+zjpiX7LdVOrNVpSK3q1FgIqd0nZsgQ9b/9uNzvgQrIrCPYuGqG3BiGiroTowoPKb79LTGfxdQk2XJDK2eUJgddvglIplXHsIs+CjquH4uTQavZD2iE2TndFQ+ckQYK+4uUV1z5vyYPwLXDZZOScTqSFVN9K21664yX2K4YaV8JpkpS0JnX0nc4yLO05bUlUncRT6I2K1dRrtwoZ1DNBBtJDs+T8UBFda+foW/SWpEs9pFvYVpmAq+e4kQ== 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=HF2W9/+B9SXNSLgA1UL2ZD5ts2exPEOIUKLe7KETuMA=; b=eV+f7pnxJ5d8uQf5JHvL9e9+eckIDXUpmfk81zCAHaamzrdmd35qR9oYtS7hzHJWwkQuAFkQWjgUYimrFqLbURiuHn5wz4DSHcrCAQoHEIB6CkfzS+H5h496IAG3LYZx9pQwkBkiR+MrPSJLqbRQadDpg4RmPUMa3moFhWoOWZo= Received: from MW4PR04CA0032.namprd04.prod.outlook.com (2603:10b6:303:6a::7) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 16:57:50 +0000 Received: from CO1NAM11FT039.eop-nam11.prod.protection.outlook.com (2603:10b6:303:6a:cafe::78) by MW4PR04CA0032.outlook.office365.com (2603:10b6:303:6a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 16:57:50 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; 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 CO1NAM11FT039.mail.protection.outlook.com (10.13.174.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 16:57:50 +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.2308.8; Wed, 13 Oct 2021 11:57:43 -0500 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , Brijesh Singh , Michael Roth , Jiewen Yao Subject: [PATCH v9 10/32] OvmfPkg/SecMain: register GHCB gpa for the SEV-SNP guest Date: Wed, 13 Oct 2021 11:56:51 -0500 Message-ID: <20211013165713.727815-11-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211013165713.727815-1-brijesh.singh@amd.com> References: <20211013165713.727815-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com 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-Office365-Filtering-Correlation-Id: fe698e06-216f-4b17-478d-08d98e6a9716 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: LwXD0IA6JdX2do8yCr6vi7lRdLZcA/oZi+vI/W8WuLcfGasYu8U5E/dgJ8ueWm67WNwqX2ZirypUEE2Tg8ev+SIgBCjH4ym4dQdNlEOxcS7h6K/jTYrdRtrbuhgYKUlfxVs5FEPyEtspTM7mrZq9rxm56qZJwl8zIaSbkRQbZAk9gjMY1dkP5w/S3mzsOTVtan6wJN5A/C3tJSMd2L5ea18fK6TmHSpdyvZOgvcr5R9TVbDVZdKM1v6wIORUaYybTWRhJ9H8BUdBZah5FzYKc+WqXm3V9wNGAqQkYNaf95RL6G/6QQdfFKwqs7/0J/iT/AAf2Catr67Nkb2XrZdr9PRWcgPaTFsdG/NipupJwPfaQ2Nw41CQEzHu8siYDyEYebqos0r8k6ERO2lXc5fajXg9iXrsEGJweTxa4RwuSG7edpkZCqdncWpEJyiSpcicqmIld/oIjwmIPv0Keu33Nbnbf0RJvHszN0rdIyv2cVlDG8bFdfFq8Thj+SfecVyzI3tSrV68M6ODZ48R19JUD901gk1q+hzUPWbELad7rjyhvHljivFEBYiuhHbpgQrzU7gvJ0ZlnxcQX3qJLA/oM2yZhC4IFknhMfmrUjTuNLjztwZBhluBhDDRsiOSkjKDVKRV6uXWnCi0mOyDxtcbybsFe8AYzdxwyXYBARfGjMSSrBAXLCV6S+TcCdrUSmD8KG/fxvbWhtN3OVlI6PhCu2qMZzksZgKrPXx8jzr/L+IhUfCT34xxprP/A3/Q9Zud6tyXWvsPe4kWC1tDO2rXoC4TJ8yBZEAucu2sRsAefZfWOuZld14VfJiX3b8sq64a 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)(356005)(83380400001)(1076003)(26005)(5660300002)(86362001)(966005)(336012)(81166007)(16526019)(186003)(36860700001)(8676002)(44832011)(2906002)(47076005)(70206006)(82310400003)(36756003)(8936002)(508600001)(316002)(70586007)(6666004)(426003)(6916009)(4326008)(54906003)(7696005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 16:57:50.0633 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fe698e06-216f-4b17-478d-08d98e6a9716 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: CO1NAM11FT039.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 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 Signed-off-by: Brijesh Singh --- OvmfPkg/Sec/AmdSev.c | 118 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/OvmfPkg/Sec/AmdSev.c b/OvmfPkg/Sec/AmdSev.c index 7f74e8bfe88e..9dd42b195785 100644 --- a/OvmfPkg/Sec/AmdSev.c +++ b/OvmfPkg/Sec/AmdSev.c @@ -48,6 +48,103 @@ 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 ( + UINTN Address + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + EFI_PHYSICAL_ADDRESS GuestFrameNumber; + + GuestFrameNumber =3D Address >> EFI_PAGE_SHIFT; + + // + // Use the GHCB MSR Protocol to request to register the GPA. + // + Msr.GhcbPhysicalAddress =3D 0; + Msr.GhcbGpaRegister.Function =3D GHCB_INFO_GHCB_GPA_REGISTER_REQUEST; + Msr.GhcbGpaRegister.GuestFrameNumber =3D GuestFrameNumber; + 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.GhcbGpaRegister.GuestFrameNumber !=3D GuestFrameNumber)) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_GENERAL); + } +} + +/** + Verify that Hypervisor supports the SNP feature. + + */ +STATIC +BOOLEAN +HypervisorSnpFeatureCheck ( + VOID + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + + // + // 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); + + if ((Msr.GhcbHypervisorFeatures.Function !=3D GHCB_HYPERVISOR_FEATURES_R= ESPONSE) || + (!(Msr.GhcbHypervisorFeatures.Features & GHCB_HV_FEATURES_SNP))) { + return FALSE; + } + + return TRUE; +} + /** Validate the SEV-ES/GHCB protocol level. =20 @@ -88,6 +185,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 (FixedPcdGet32 (PcdOvmfSecGhcbBase)); + } + // // SEV-ES protocol checking succeeded, set the initial GHCB address // --=20 2.25.1