From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (NAM04-SN1-obe.outbound.protection.outlook.com [40.107.70.81]) by mx.groups.io with SMTP id smtpd.web12.58091.1595863720734281419 for ; Mon, 27 Jul 2020 08:28:40 -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=r+akFkNH; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.70.81, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IMYj7kOTRJkB/lKCq6BYeazCNTDQNhqJ2R3nRpDG5pZ6M4bUY/g4PEtIFwCHUr+wSfJb5c1/EIvvwsQKb3sdyUuS14nCi7ZQNifQWSq0aGdZ+5nQbSvQ7zr6e00sIPsAxJlKubab6dvvus6o57uWkapRYhN2zwT61BTMlvmwri2wEGue2IR3akq5FJDUg63tSlGPw1RS87uN9PH0w2m0Jg+9aOj6LUwcLDqkHdDOYh2dPtjDB9O/z4DtIJgXshXL+9uc0SC8eE6iEZulTPpz+Fsflb/c3AiuPBHzILrdSwOR1X2ljN/wdQ+ckvX8fepJjfH4qDr6OCSFidRHwJdsZQ== 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=FWKCrEtHWYlJzpCKEIN5WbS/IQRqjS58NRPQFFkaiIm1bfFJwg89X0Jx6aKM4vYfgKUzyrzPTNCIHKaXo8fdBBoKZPZmDoPLc6g+zPO6dsUrSXml69+qhFlDdjum1nIkO0/tJn/aKbwgkavl7jbXmsKj8TxSLFQKqWgms3cCJ6Enitne0HqLWXikjU2+XChQQ/uaai/mPUo+bv1CaxFsax6o8rbRS8WL/7tFZ+VxJB4zs07nkC/5loeR8q+ugPbFRxBg2B0lrC5fR7YsCr7ZferPJfqo54/gYbsVUaw+H1S09l4YRUM15RxVBVk751OC82OQr/cBL4p0KACSF1HVOQ== 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=r+akFkNHuDivGVwcfAWNP55KBzc7AFMJu/IX85TFX5pF4bSv4GIzvA2mCobxXwGhfEBWH1pgGYFTPXwNuLz0mCScU3/OGEdC+uHcz/ioNA7/XV2LgsY20GmNvy8ZULjzwbSoInv+RWhGjTRzvAdqqTZYMgFHPcMKpdwEc4ovEJI= 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 DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB1163.namprd12.prod.outlook.com (2603:10b6:3:7a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.25; Mon, 27 Jul 2020 15:28:39 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0%8]) with mapi id 15.20.3216.031; Mon, 27 Jul 2020 15:28:39 +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 v12 15/46] OvmfPkg/VmgExitLib: Add support for CPUID NAE events Date: Mon, 27 Jul 2020 10:25:56 -0500 Message-ID: <3db24fead886864ebc31ccd5c73598faaca86706.1595863587.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: SA9PR03CA0028.namprd03.prod.outlook.com (2603:10b6:806:20::33) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) 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 SA9PR03CA0028.namprd03.prod.outlook.com (2603:10b6:806:20::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.24 via Frontend Transport; Mon, 27 Jul 2020 15:28:38 +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: fcf75822-d3ee-4d62-446a-08d83241bc8c X-MS-TrafficTypeDiagnostic: DM5PR12MB1163: 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: LH3Y5ptfgkb8UBZaF472iVimY2GWsWYitfb1ihsQtiUCBQTr29geLdfjbwwK/PiLJTCDLVUY4A4O1nQ8m6dPnGaSVE36lXZ4S43Tkf3OSkdThuoBBtVx6Ax/4nG0HJYOy3ekm+4C7s8M0mrmJKg9zvtXn2lArblayVr8ZEjAJsWIxhZ6jRsOwiGc9OReQmiHPXNVmwgLa3ddxpG/CwNMjCwCANjGfPNQ0wFSSBaRsxyDCOt6JO/gx5wToCS3GAf6MGH0L/gFBnX8wtBVEltEpJqDMdN36LjB6kODlOvfS33+TZykcOezGUkU6M1nHnvcebj+7R+u1YVvE57nmyr/OllzUfO+uaGH2LPFXuMhm8IiwolobeyiGfc23Ve08VGWiCsdDdhkcMgtzpl5ev6eQ2iFIXFaznf4lb9xyhqE6vBs3u8p8tG+GUFtAHsyCG3AVgY4DGW5vD6+YoQk6PL06g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(376002)(39860400002)(346002)(396003)(136003)(366004)(26005)(7696005)(16526019)(52116002)(186003)(66946007)(2616005)(66556008)(316002)(36756003)(956004)(966005)(5660300002)(66476007)(54906003)(6666004)(478600001)(2906002)(4326008)(6486002)(8676002)(8936002)(6916009)(83380400001)(86362001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 6jIKD4/7AclktBgbfJ63gOV2OwGScfjKdhhUvijqNtWJOrwh95L0khbzRz2l7xrUzG3/+MOT8cCML6ene/4IWuk1sgesvVkyO1hcwACmc0TVlNJilfLScQ0wGR4bQccKMdzo/TKuPA2zRDAvp+LekYPG1IcI+L1MOb3+lFQAtX/lM6+6TPnErdH5Rbx3K6U+Q/qDRwIPn7tIGljE38iQZVaYr/zb8Iz5cI3zFgS/WzjJytjBLU1RoaJV8I83weEQaZRhgXwCjzdIn+kaI2bTlqMaMWW9DPpkSAW1bDqTdqODqnL6FlAQAzILOflbqjxs4X99CwoMWPGd/XT4XGoSMmf9JGO0e8qFD/tRu2xXdKKC+BOdQ2gQUpF18FgzdL0v7lw7gc+rfaRerKVmIDqEtJUz61ejyqYiBbmS2UmCINK9Kk2AAR659mNG6y6ah6o6kCUjOQlnYidi3FWckMQhd78QZiWyyDbCV8q3GFtm/C8= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fcf75822-d3ee-4d62-446a-08d83241bc8c X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2020 15:28:39.1806 (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: f7JK2IdZ0ltkkdRelprYVZnFidUGwHudZRci8aIIHj+UYaUCUE+D0OWtL56+Eb1vuKbbHOgnuEwqQeAXDZvzWg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1163 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