From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.88]) by mx.groups.io with SMTP id smtpd.web10.921.1589925107171473827 for ; Tue, 19 May 2020 14:51:47 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=XKl8rug6; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.220.88, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jTnJECVLfXdBGdLkdb7n8SuushMFuwdSOWE2+pKBcRbuAejIN5K3xtymDtmj2mfkLijNBY7SWN4dvcRoMHBDj26aeSPAnBbumSoZfLsfNx0cuulHm6Hj4KIoepW9J9N+tFwR/qZcgr/NpGpBmseNhE/phfrRPaO3wmxlDh2pLEYBbESPIBH34bdFkfMUFlBJ+G4WPWUMP5TEBHGs3kBXyomN7oYe/yC8T82Avs9jKgL8tAxwLuyCEuTqxK8jHhyasa5KQatB+8UuqJSBW6CCaEmAclQ9wbud/R7Mmy46/WwZn90F6Uj2rcccsqGAcp32BISGNqpZ8XuKG81ViotBBA== 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=pHdzE836zVc4g0nsIYL6nk36Ff1OB0kMjtgKa8Xd3DE=; b=ivl/hlrdBWZojDnx+YxdyjszAEjFGqprWcHeMU7IweD2BBdFWaTlf6xwWByT4LtutDveXvQdEWfY1EGyWtCRCDgVsN1G4rGmpqQH8lVNvm7MAxPgccTUf1KoSf181V6LcVzHeY7V5doBi/0E6SxVEzuEC6niRSugvOso4aEiLCm9aaxSfdmLmgAzhHIWsH/jvEw1WuF4EGE8QdmrjJCdp6Db+c3G1DMLosh6aaz9bCw8vmB5dxCwU10dUmuVdwAUci8Gs3vfOTmG2XFe8xgdd4JH4rUAjg1c3N6iNL2rhYm51jZFjOwNzVjW6NApczyGPl/CZ660ezrzHj0gFd45dg== 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=pHdzE836zVc4g0nsIYL6nk36Ff1OB0kMjtgKa8Xd3DE=; b=XKl8rug61IAqgbSLqMgiOdi/zCRp1KulSTVf2TqtMYBGwiy/lIE0GvwSQBO2Fy9SEhzH3sVp9bgEGmbOaMbRmxuAVanFsURjUZ+ubMnBev2a7oN6AyoETNyCmzBM74HfkwaaknPAWTILPyv4QRlLRoNZFojwRjWZtPY2mh9bqh0= 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 DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.20; Tue, 19 May 2020 21:51:44 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1%10]) with mapi id 15.20.3000.034; Tue, 19 May 2020 21:51:44 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io Cc: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Michael D Kinney , Liming Gao , Eric Dong , Ray Ni , Brijesh Singh , Ard Biesheuvel Subject: [PATCH v8 15/46] OvmfPkg/VmgExitLib: Add support for CPUID NAE events Date: Tue, 19 May 2020 16:50:43 -0500 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) 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 DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.25 via Frontend Transport; Tue, 19 May 2020 21:51:43 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 019f11ef-0b76-42df-8758-08d7fc3ed26e X-MS-TrafficTypeDiagnostic: DM5PR12MB2504: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-Forefront-PRVS: 040866B734 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xGKeKfiaQ8cPIVCz3HqA1me4QEGEUQiZNgoH5rJdd32j3LPsyHKDXfhgYMaSIu8nh/9It5NHvfNkAeGMKXVwjSAFPU5m28SbIzcPcJtjfRf/dN7TxIf4gLERqROSxAgx8+seTV2uFxu9kIu5WMM6mStiPGhZ5b77PAOYakKyhaGFUyj504cblsGY+DCLRZXZCcS/ih1prL0WT9WLM+Nv+pXuPkQSYokuIN2UFWYRBp8JL/YyU92DfybxNqpVTNt8TKlBxvWzB5tV7hH7vHuRTeSkl5/htktd4CJ3URSGFyIh++mcE/Jf0aWiU8eeplgMzFYNVuWUySSdi+J0OMcSKYaTM+ZXxYFn9oa2EfROsF8RMnnHQHW/d1p7JnR4z1LB4BxSFSd17cB/SR8aDFecwUGUtfPiiOBnpsZsHEkFVafaunFpf/vz6EvGclYSeXYa35R6wrjcGOJea6Ukw7H6cLuB/tG/vr/LTbiUGCGFegDGzvdccXKH9YXbqqO+qn9yrrMxQDCVPlinQF/WMGPRQbJsu4sY3cpXdh9TEsUj42og8BjNwQEUjIwppbJGWB1ikPq8eyrOIJ0oyb5Dj9/icQ== 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)(136003)(346002)(396003)(366004)(316002)(86362001)(2906002)(54906003)(7696005)(52116002)(2616005)(956004)(66946007)(66476007)(19627235002)(66556008)(36756003)(26005)(16526019)(186003)(4326008)(8676002)(5660300002)(966005)(6486002)(8936002)(6916009)(478600001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: sNgGI3KXfCwuuT1fQ+V6SjE4Yz/BPJawR6qp9/4aMvRMsxxEznQHIf6z/vdvvQBdrr22sixvXJNJaUSJ3Il+ChNGhJdvg1IeOoD/eB+X91EV5hvY2dw8cJ92FpGdF7T9oYnu8kxyhgJTva8CofqFSDwLWM4k5OT3vPlx5sHUxgC/9+2kWnUPuLi0d39TZuXcKuObgEfBhfghk3Mf+Wz1ESyU4XiVeDfDkZBbxJwXRwIqpwUjtsxFVla8uIdrA0nD+GKaWFkT4au9+oFFyn7rjffqWxjkUiXLEZ+rd7VleexByboHv43M40Um6t2XWhTiGBLv1L+G9ZMaJwfF+08i6L0WtSrFzU+ttxr/6t0N9kSZrRGiQKFKPt6uTEHQEBx5wmFLKRTNjfQe58NDwrmClV9a6qCmb6VWXdlh2TFRwnB0u0ITwdpM3HU/7fT80xmtXguuG+TD0Fcv86ovTc6Z1GknnAdje0XRjGcHfao2rFo= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 019f11ef-0b76-42df-8758-08d7fc3ed26e X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2020 21:51:44.6604 (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: FzPtk/QPsq4O72ZQHLzFkpPNJDMk7JhTtjh4HN97L9vZRRbedAeMJuKLViuOyWvGKWDhI4AApDVZrie2FJahWQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2504 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 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 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 Signed-off-by: Tom Lendacky --- .../Library/VmgExitLib/X64/VmgExitVcHandler.c | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c b/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c index 906b32e93d53..2f62795edf61 100644 --- a/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c @@ -12,6 +12,8 @@ #include #include +#define CR4_OSXSAVE (1 << 18) + // // Instruction execution mode definition // @@ -637,6 +639,58 @@ IoioExit ( return 0; } +/** + Handle a CPUID event. + + Use the VMGEXIT instruction to handle a CPUID event. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communication + Block + @param[in, out] Regs x64 processor context + @param[in] InstructionData Instruction parsing context + + @retval 0 Event handled successfully + @retval Others 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 = Regs->Rax; + GhcbSetRegValid (Ghcb, GhcbRax); + Ghcb->SaveArea.Rcx = Regs->Rcx; + GhcbSetRegValid (Ghcb, GhcbRcx); + if (Regs->Rax == 0x0000000d) { + Ghcb->SaveArea.XCr0 = (AsmReadCr4 () & CR4_OSXSAVE) ? AsmXGetBv (0) : 1; + GhcbSetRegValid (Ghcb, GhcbXCr0); + } + + Status = VmgExit (Ghcb, SVM_EXIT_CPUID, 0, 0); + if (Status) { + return Status; + } + + if (!GhcbIsRegValid (Ghcb, GhcbRax) || + !GhcbIsRegValid (Ghcb, GhcbRbx) || + !GhcbIsRegValid (Ghcb, GhcbRcx) || + !GhcbIsRegValid (Ghcb, GhcbRdx)) { + return UnsupportedExit (Ghcb, Regs, InstructionData); + } + Regs->Rax = Ghcb->SaveArea.Rax; + Regs->Rbx = Ghcb->SaveArea.Rbx; + Regs->Rcx = Ghcb->SaveArea.Rcx; + Regs->Rdx = Ghcb->SaveArea.Rdx; + + return 0; +} + /** Handle a #VC exception. @@ -681,6 +735,10 @@ VmgExitHandleVc ( ExitCode = Regs->ExceptionData; switch (ExitCode) { + case SVM_EXIT_CPUID: + NaeExit = CpuidExit; + break; + case SVM_EXIT_IOIO_PROT: NaeExit = IoioExit; break; -- 2.17.1