From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (NAM10-MW2-obe.outbound.protection.outlook.com [40.107.94.52]) by mx.groups.io with SMTP id smtpd.web10.1747.1596134800912590658 for ; Thu, 30 Jul 2020 11:46:41 -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=4IMJi67s; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.94.52, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GVsGbFwwh8hidS7Ak8zlrww7jNtXEPFvZCLTFvqTqnh1vMebCGASSU0rdPT8dl1KRJLit5/fyKh5aM+sgPmtA+yxObyg727sOrdSN3rw84UItuz6tzON24ZJv+mW1bDQdF9hA/ZJx+wnit/RMR+dMVsK3pEm8aNseANoRgAES7J5jTAy2F2m7VQgrVTM2aXqzXNqM716ED6knExYrJjHZzHT//MDYy2BKjAMTE0YfgFCgIE4sApp0i2lUgT/hlGjbfurylwL+CcaW0mTDQGTXNSrntpBQH9KMKYE7kDMLebjuE1raYCLjlPLH3rkFaJ1+WEicG48HMs599sX+qKbGA== 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=Dmq0NdDcTiPJzP2XPNMSQNXsS3YmJoSL89On3eDzjgsrc4OIoQd6YZSuDGcLeDLXyKNQOqYt4C8D9D3fqkpprlYtcOigGX301DebG/nHa+VWV3TKlBjD21ixbV54MeLRQYwBJIaDQx+oAHqTdquv5adIQJiM1avs2qoClC/B1L2JIwQscwXPGydvr/JX+IGaPka3Mg1WDoNCDY/Zpw2oYPAjuIIqVhC52ZR9yOn+2Rle6F5J7gplHtvVAI1ny42v+dFPiqF8kAa9CYjTeVVz4j99q6Kuv+z/tzymVvVS0indHxRfhbT6lZVk2IkVNwkkm3vB/e8S2GUYd7CQa7Sv6w== 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=4IMJi67smrROY75OeOWJn3/XnftMDtABPw3iHtuOX89dLT+roJtgUx5PoTQ79kgNTbVdDLQWBFcG7KugdfSti11FkcXh7IJnvRdoYOZA74mCC1AVSTQrhNOqjDqP9bFIlc/l8i/F0Xd1K20NG0S9oHb7RAwNRLNvk/SeQB/pQYM= 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 DM6PR12MB4092.namprd12.prod.outlook.com (2603:10b6:5:214::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.16; Thu, 30 Jul 2020 18:46: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.3239.020; Thu, 30 Jul 2020 18:46: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 v13 15/46] OvmfPkg/VmgExitLib: Add support for CPUID NAE events Date: Thu, 30 Jul 2020 13:43:27 -0500 Message-ID: <0c08e8f63f46386bf9e0fd36521cdb726cbee4d7.1596134638.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: DM5PR10CA0023.namprd10.prod.outlook.com (2603:10b6:4:2::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 DM5PR10CA0023.namprd10.prod.outlook.com (2603:10b6:4:2::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.18 via Frontend Transport; Thu, 30 Jul 2020 18:46: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: fcfec5c0-774f-4943-99f9-08d834b8e500 X-MS-TrafficTypeDiagnostic: DM6PR12MB4092: 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: 3V1cEaz35fskI/L1wuS1S9Graics3V5KtEn16kJLtiS1JzhoXC9ehFTPxJ9/ukBi9VPc4w+HLv1flUjRkFn75x5C+Fh4R2IN0NrPbnPK7q10DOeeHHAffiLw1JZXzka/5pNOiPp8Iz7xsNrnI0pKpm36HGiUs5Lb7m8rUauRWVDMbKMhi4NHYciMwQs4gRjGoWdgwtaNaevaFN+Cupdkavu2G3rGenpIgZnnfNI/80ZHeyUE979gl3rqQhgcEXMUUb2uMxwuib8BI/i01/6pVUwFR2Am2gjp56e56XP8Xk98b6DXPMQ5Djtv0760lo5yw76gxfxeli4g9iI7X9CDcvyjj/uXEIIp0akIBhVgDEaNVPHRAYxvsRzn631VGBhbgtVuyocjdB3pu3koNG6vKQAX4GtR7SK0HOREPbYHfCkD7j+9cnpXtkkN9mG9xyYQg7NhlvuNkQC/k5I/A77tuQ== 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)(366004)(346002)(39860400002)(376002)(396003)(136003)(6486002)(52116002)(7696005)(26005)(5660300002)(186003)(16526019)(54906003)(66556008)(4326008)(66476007)(66946007)(316002)(6666004)(86362001)(2906002)(2616005)(6916009)(36756003)(966005)(8676002)(83380400001)(478600001)(956004)(8936002)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: HI7jK/Au3ylAesT2UZgjpzg8d5VndmgPQ1S1cPcMluVBohUBGz3NHkGr5S4qfqOihUd0dWbdSA6WiBoG3qlMGKFWlpyu3agtP5MP/aIjH0EeCVyQrCE8VBI3Etsqef2e1q02QmX0RG4aVxlssSPQ+x/kAiSde55TrvY1ZSj08jf8wYdFdDyq21driMXJzvxUdM2xoGjDLM4JFd8dvvFBdfUrQkCBII5ZZQEx6pdue3Y5S9IWC3U82mS6CkHBrJjpxbR1JxhGP8Tb3qxz4O3TScCmOpNRuHLg5bgdgGtl7Ikz/0Adj9oEU0R55juqlit2+g3Fn0uKatb3j5+Go1vjmimS8L8L26WmbLv95vU7hUdAAufkDWm2rYKM3QizMtDQyqbdtFZk9YOwuzpNvU/b7LTUrZrDMHPYlAstdhFH5hiFCbdykhAyrNrx/GMBmT7DN8am1lZO8m9iCbPw1j9sGHFIfx50qQQboRnZJ6pkzD8FsueqZOUrBvCD5HRxBl9QG6ODtT3NsK7sDodKIIXoKDqKw9uE6tpSK1dnlvnvJZani4skFRWhQXG1eylstz5JjlTMc0KNcmFEofhudW5OSkET9M1N02I6n6hE0I+7uzWFBeEN1U0HNek2Js9qv0rmh3mi1uEuIjmSvo2KO1qcZw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fcfec5c0-774f-4943-99f9-08d834b8e500 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jul 2020 18:46:39.6972 (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: XAZmk6BozEHy/oxi19gmOHX/ZSKTeIun0CjfKdg4HyUreWDbYuvUfCL7/B7Qce1Vg1AFpYnmAVZha9x/0+JJ6A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4092 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