From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.79]) by mx.groups.io with SMTP id smtpd.web10.5646.1595366506640805999 for ; Tue, 21 Jul 2020 14:21:46 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=Ro1Z17ys; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.223.79, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h7jZVsZ/48sTgv7ZEfXssUaOJawGgq/2+TQEVH1nDy6CZ1pvRFdtHI9IaFiizH3lMi9QqKdCNM3UMm8kWr/3VXj6nGufQY+0qVQNHEk9BSq6A5DHUNnIDfGPDMOx9w4Luss+SdbopBWvGDl0/L1hxSVopwcAf9Dek2y/InrhD2LeKNba+JYgX9gY/OlDRX2GOlmWOyYaCZ9N/PyZmsA9ppy6FJPKtlJayA4N05tpeJLPvp5nK6arqTwxLVXME8uuh42VmPgNbR7G+Uja30j9mB7/3UWRr4OFUSh5gbWGs2H7BA9ts5yRYmlAhvSaViq/WiI7+wjue5XkKkGuYB+NBg== 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=DefcxGXcet0YpbciDB6cqSIPAFoRtyFhEUPqxpHT3rE=; b=AcvBFnPWd63YE1CywwEPnzgMCpsH4RXjGccCRfOa4SFYszbcJlFKIF1aeZm4TSNhK2fqjoqoTYBlmuHz/1KZYZ4FPeSToomHakx6rGkPVkrE0e/GSs2ycaU7MXM0FqAKEZH1PWYm7UrSCrzmGvk/XrBgNIwzcrAxNv+0xlZ6FDX47AgQYhiZUF7FQYlH5baGMFu11+4qEnmQjkN29oCw/4UbdHJVuhjhGccFPFvv9nnZ0XwhbV9V0J+LjxEuLb18kXroBVlUWSy3f2qpdiXgGI7iPOHmwCRxbXxJPbNFQ0TBEWWtoy4QJXtC2Bo9pzssJzK4fDD5gCvRvt3bejd48w== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DefcxGXcet0YpbciDB6cqSIPAFoRtyFhEUPqxpHT3rE=; b=Ro1Z17ysBBQFP05vYVU5m+JwRPeLI+fHd7GwMfqssHoqQdATz7Cuzvv6RoVF5nD9mtkrME5IR4jK6ayHWzOP/2OrOV8JU78igWn28DboeOalCqKHAnYxMN2LhxqGn1HOuKwyRMn+P4ejDnFv1NV0UXEYnQJoty63KCtZGWV1lQs= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) by CY4PR12MB1606.namprd12.prod.outlook.com (2603:10b6:910:10::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.23; Tue, 21 Jul 2020 21:21:44 +0000 Received: from CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b]) by CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b%5]) with mapi id 15.20.3216.020; Tue, 21 Jul 2020 21:21:44 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , Ard Biesheuvel , Eric Dong , Jordan Justen , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni Subject: [PATCH v11 15/46] OvmfPkg/VmgExitLib: Add support for CPUID NAE events Date: Tue, 21 Jul 2020 16:18:52 -0500 Message-ID: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: SN6PR08CA0021.namprd08.prod.outlook.com (2603:10b6:805:66::34) To CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from tlendack-t1.amd.com (165.204.77.1) by SN6PR08CA0021.namprd08.prod.outlook.com (2603:10b6:805:66::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.21 via Frontend Transport; Tue, 21 Jul 2020 21:21:43 +0000 X-Mailer: git-send-email 2.27.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8428ef09-70ac-47c4-49a0-08d82dbc118e X-MS-TrafficTypeDiagnostic: CY4PR12MB1606: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JXtHF/OrMIH4N//8Z4Hd/alU8UgXSRAzqatVGi5UMKnhZK+3sMFQZH86OZZqH/TF1b/sfobptSHXlU/JKu/qGPYeaJYLtaPHIj81a3wLdoUs5VGpocf5qkN/c7dlh76W8MC+o8gr+c/L1ZhjbaIU5Qem9uNM7ky7UiqapAbHWyG0uMdUYr3ShEepeJqvRvJzeV6PpGr9TDD++X+UDnuf3k3gV+k3lBd4vNCywGXawZsTxLYh2uFRBud9SzkqIKl0vN+9qo/OcuaY29MXHLmcqBF4QfhzVcFbpKixuKoWkG6MKEO1dWLEqenmDFQepJbSX6iCnsJkN6yjUSfVmWjaWfbK2kMAyQXEqab+Hbo/vk03Hrj+G1K71j1MshRu+8UUVpBnx+QNfjlE+SkBh1cQlmAmw9IlzOH4k/Emvm6o8Dpq+9G1y5Y+Qlqomg0CyNZaQHrIT12VjfHWibqghZp0Tg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY4PR12MB1352.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(136003)(39860400002)(346002)(396003)(366004)(376002)(6916009)(54906003)(2616005)(956004)(316002)(6666004)(26005)(4326008)(186003)(66476007)(36756003)(16526019)(66556008)(66946007)(2906002)(6486002)(8676002)(52116002)(83380400001)(966005)(5660300002)(478600001)(86362001)(8936002)(7696005)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: fpWwEVV1LpkxN1JQsehdwQndafGqnIIPHi8Ea5Awi+Ql+X81QBYy7kmEGsJp0qKElepp+113+yzAt4t9FxBOMuc9IpcdkvHz5ajYBJ/QNhl443EFBt/fXdREiBbzCFG4Hcs1WTTxdXRcLTWjpmxL+Ol0teRbx9l8bs/5Mofi6+iptBxse4jPEZNWWt21atbmYzCuoxUjocR0YPWfa1lvh8TKQ2sBY+Cs6iNXETKK6sXmqsiSJ/Vjcjx1pVckFrBq0oi+xhaGUm9O4wFMy673uXhzCcg82YzkWtVpi8vsVKKLoCoyBX40ImGBLLraZHM9hExfIdRinSlPuPxLbkuddWaHQ7/IPkl5lpjTfHMsmtzCTikz09KJ2dZL7XgmhhDm3XpCq1RCGn04IizBqAZuIlx9efN6+gy/7vvpimatwV1VyDX+FR38YQ7U8JzUvQXCMVQCER1LU7EmAD7xO3SpwxapDs8lNtpo5AK8Sqjb2Is= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8428ef09-70ac-47c4-49a0-08d82dbc118e X-MS-Exchange-CrossTenant-AuthSource: CY4PR12MB1352.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jul 2020 21:21:44.6563 (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: iIvaYInVrW7C99sZXE3Ggf5tJeyI1PE/3goyeExHpQsO8pHGRqyXDTqKoxDmCkkZj/sAto6zkBL29xcPGQ6HGg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1606 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 Under SEV-ES, a CPUID intercept generates a #VC exception. VMGEXIT must be used to allow the hypervisor to handle this intercept. Add support to construct the required GHCB values to support a CPUID NAE event. Additionally, CPUID 0x0000_000d (CPUID_EXTENDED_STATE) requires XCR0 to be supplied in the GHCB, so add support to issue the XGETBV instruction. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index b6ac3552894f..1c9c272a250e 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -11,6 +11,7 @@ #include #include #include +#include #include =20 // @@ -597,6 +598,61 @@ IoioExit ( return 0; } =20 +/** + Handle a CPUID event. + + Use the VMGEXIT instruction to handle a CPUID event. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communi= cation + Block + @param[in, out] Regs x64 processor context + @param[in] InstructionData Instruction parsing context + + @retval 0 Event handled successfully + @return New exception value to propagate + +**/ +STATIC +UINT64 +CpuidExit ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT_X64 *Regs, + IN SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + UINT64 Status; + + Ghcb->SaveArea.Rax =3D Regs->Rax; + GhcbSetRegValid (Ghcb, GhcbRax); + Ghcb->SaveArea.Rcx =3D Regs->Rcx; + GhcbSetRegValid (Ghcb, GhcbRcx); + if (Regs->Rax =3D=3D CPUID_EXTENDED_STATE) { + IA32_CR4 Cr4; + + Cr4.UintN =3D AsmReadCr4 (); + Ghcb->SaveArea.XCr0 =3D (Cr4.Bits.OSXSAVE =3D=3D 1) ? AsmXGetBv (0) : = 1; + GhcbSetRegValid (Ghcb, GhcbXCr0); + } + + Status =3D VmgExit (Ghcb, SVM_EXIT_CPUID, 0, 0); + if (Status !=3D 0) { + return Status; + } + + if (!GhcbIsRegValid (Ghcb, GhcbRax) || + !GhcbIsRegValid (Ghcb, GhcbRbx) || + !GhcbIsRegValid (Ghcb, GhcbRcx) || + !GhcbIsRegValid (Ghcb, GhcbRdx)) { + return UnsupportedExit (Ghcb, Regs, InstructionData); + } + Regs->Rax =3D Ghcb->SaveArea.Rax; + Regs->Rbx =3D Ghcb->SaveArea.Rbx; + Regs->Rcx =3D Ghcb->SaveArea.Rcx; + Regs->Rdx =3D Ghcb->SaveArea.Rdx; + + return 0; +} + /** Handle a #VC exception. =20 @@ -641,6 +697,10 @@ VmgExitHandleVc ( =20 ExitCode =3D Regs->ExceptionData; switch (ExitCode) { + case SVM_EXIT_CPUID: + NaeExit =3D CpuidExit; + break; + case SVM_EXIT_IOIO_PROT: NaeExit =3D IoioExit; break; --=20 2.27.0