From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web11.21252.1594737490560993734 for ; Tue, 14 Jul 2020 07:38:11 -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=fbmW/9v7; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZCODR9/UPZ76cHHnGmdVSoT8HSmJwX/qGY3wd/BNCL9Nrxy3m7+RE49RS0B1PVFiLPGGMUdYe6Bm8ROqX65f8Ce6regFge1icNYXLXRfBGS/q28XbFc18AIarnEGD/eKu4YZ6eU/FsdhBF0up1skmCvDFOgYYMDWX2veeLXMLHnDyj1s/dpxTBaK/hjXb6zNe/Z9ffyc/BO5E7oOQU1JBN6E78QnUjMQ25hBIZGbOqQK/g3GzCzE3owYRpGRwjuuIPHbLn3UeKDMC+oaaNtJOPBBVjxMS1u4bn0nkJtorAR2h1rI60VoeP8uyi8UGPWab0+6uMC5wvCmy2Z5zba1rQ== 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=eqxKEl1jkDkoQSy0tcmI5Mo0oyJK/DNNn+LhL12PTF0=; b=a/fiOrxuW3tLSoZ74Q+Ckq7ashgq2Ic6FdlJte2+X9MGs0bKk9dlYX+c10X+LMUVGFrz1TbxP+L1b3JGO1hKiiD59IqiUzBcp+SY+0oAr+TTBFAEK+Wo0RpeZXXjw8YETVMvUC3sWrrwva2XWOtp3TSe7QFBQQFaYjUoVNHZw+vFDXXgZQ+0dmSGNTAIHtDq8/Cg3ka+ZPsuZ7vFMtqMg69Br84lD9pKIijiaIFJ6GG0TkSenTQ2Y7MhI+OtqNq5bnp4Oyx51JL3tIJAzTh6nC0Z25203koeml4VhcndpxcHK5AE5D/BLBaQrRGqtAB7M3tCxIwZCTiE4my7B4433Q== 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=eqxKEl1jkDkoQSy0tcmI5Mo0oyJK/DNNn+LhL12PTF0=; b=fbmW/9v7U6ejSuvCI+7FFvLJ4cdybKLHocNSPw10JpahLgOl8eV6cJfZb8j8Q1QwrRZjDZi94X86XKyyb96UmGz3telzYPcYUHEZqdNABF9RBkIEmYhciRFrmRjT9ukYJ56hXlYjYjzprCJizlRdIVUADHv4oFmUbE50kJPhDXU= 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 DM6PR12MB4219.namprd12.prod.outlook.com (2603:10b6:5:217::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.17; Tue, 14 Jul 2020 14:38:09 +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.3174.026; Tue, 14 Jul 2020 14:38:08 +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 v10 26/46] OvmfPkg/VmgExitLib: Add support for DR7 Read/Write NAE events Date: Tue, 14 Jul 2020 09:37:33 -0500 Message-ID: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: DM5PR20CA0032.namprd20.prod.outlook.com (2603:10b6:3:13d::18) 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 DM5PR20CA0032.namprd20.prod.outlook.com (2603:10b6:3:13d::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.21 via Frontend Transport; Tue, 14 Jul 2020 14:38:07 +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: 551e2c5c-9500-4373-68f7-08d8280386b2 X-MS-TrafficTypeDiagnostic: DM6PR12MB4219: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dY9sju8WOJjF2SmI/g+XdOQ/WPqHO92RAQb/5NeVfgh55Y/ITRnuJRuCq8PDzZV8jNM7Dx+nftlzjUpPRw5FmqxgXJyQI6YJNgoRT4WlCRPZt51SNmrHK/uNcpUGRtglgwh/UtltuENa8XJdxCKyBBdq9DNUGlLHUcUHQ/xT7UUc5LjroWpJ6Y6Dn1qWRq4Ayc4LAwtMgGYJT7LeEw4EUeB6KjGsmEKPbztXpIBxYocdx0racxEcCTmuvXV4S39xJVkrEh7zzcvbucvimksi/DPvxS7bLfRX4x1A3/4urRAJMyaODA5Wk3AaMQwr7ffsr16f1CMWlFRfXIHarM0jj4m5tvhZMroPOV1eODK/M0FP+cFRmsvTAuA5sRcLP10S+8QOD9ZJd2eteH29aHvIMOMzBr46QQvtcaE+NbVR9H0phs+HDARSaMZDTt4RMWqYkd89BpfbQzn9k3I0QtNZ9A== 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)(346002)(136003)(39860400002)(366004)(376002)(396003)(4326008)(316002)(66476007)(2616005)(6916009)(36756003)(2906002)(66556008)(66946007)(54906003)(52116002)(7696005)(6486002)(956004)(8676002)(86362001)(5660300002)(6666004)(186003)(966005)(16526019)(26005)(83380400001)(478600001)(8936002)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: lfwsgfPKCq4i8TXDLDxCASkloLfHm+Izw4rUmtOuJb2G5y08f5n8o1YliwW/xnN29J8x3fHMXqu/o1D+H/jWwFmAwZGscQYeIQ21giVdnSqbN9i/WFo5D+HqviNuwZWgLx7d9W3Q61bAmBp2va5w16PRHQEh5P9UCfVTejH72/spFIIobIxjrzcCxMwBOgAwbAfIsIU6FrmgjvF9/gt/YMtOlwkOUBHQW8aPLA1SRdZoRls3t6/Enh5O8Csq47h1L2L3lb94BXq2O+9NCF0jZ3IDzLtRyBZn4H0KbTfYBOOZ3XOn6iwwlItrMySezdCBpqZf61/tC+rIma1aaJ59ZHZ4olU+JaQDSiGEwKwtK8WOfjC1OHu8jO0S4wbgduLqhunQD1MxQ0+/mJWsPfJvFuEEXaxuhqgZ8ES/zAVzcn4aHbQujh8PN2tHHGKnJ4m98ognwa2qf/7/L6fr00s27cNo9eOcNHYMIPA2EC15mAM= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 551e2c5c-9500-4373-68f7-08d8280386b2 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2020 14:38:08.5556 (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: 7KlqsR3waAbxC+QmPQx43bUXTF+qspgSy941h5yByCYeJMH7Bp035731k3W2AsyM7Q76JJLNuy6rGwLtXdL6mg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4219 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 DR7 read or write intercept generates a #VC exception. The #VC handler must provide special support to the guest for this. On a DR7 write, the #VC handler must cache the value and issue a VMGEXIT to notify the hypervisor of the write. However, the #VC handler must not actually set the value of the DR7 register. On a DR7 read, the #VC handler must return the cached value of the DR7 register to the guest. VMGEXIT is not invoked for a DR7 register read. The caching of the DR7 values will make use of the per-CPU data pages that are allocated along with the GHCB pages. The per-CPU page for a vCPU is the page that immediately follows the vCPU's GHCB page. Since each GHCB page is unique for a vCPU, the page that follows becomes unique for that vCPU. The SEC phase will reserves an area of memory for a single GHCB and per-CPU page for use by the BSP. After transitioning to the PEI phase, new GHCB and per-CPU pages are allocated for the BSP and all APs. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index e70e0ef82f68..c57c8c4ba203 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -126,6 +126,14 @@ UINT64 SEV_ES_INSTRUCTION_DATA *InstructionData ); =20 +// +// Per-CPU data mapping structure +// +typedef struct { + BOOLEAN Dr7Cached; + UINT64 Dr7; +} SEV_ES_PER_CPU_DATA; + =20 /** Checks the GHCB to determine if the specified register has been marked v= alid. @@ -1480,6 +1488,104 @@ RdtscExit ( return 0; } =20 +/** + Handle a DR7 register write event. + + Use the VMGEXIT instruction to handle a DR7 write 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 +Dr7WriteExit ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT_X64 *Regs, + IN SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext; + SEV_ES_PER_CPU_DATA *SevEsData; + UINT64 *Register; + UINT64 Status; + + Ext =3D &InstructionData->Ext; + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + + DecodeModRm (Regs, InstructionData); + + // + // MOV DRn always treats MOD =3D=3D 3 no matter how encoded + // + Register =3D GetRegisterPointer (Regs, Ext->ModRm.Rm); + + // + // Using a value of 0 for ExitInfo1 means RAX holds the value + // + Ghcb->SaveArea.Rax =3D *Register; + GhcbSetRegValid (Ghcb, GhcbRax); + + Status =3D VmgExit (Ghcb, SVM_EXIT_DR7_WRITE, 0, 0); + if (Status !=3D 0) { + return Status; + } + + SevEsData->Dr7 =3D *Register; + SevEsData->Dr7Cached =3D TRUE; + + return 0; +} + +/** + Handle a DR7 register read event. + + Use the VMGEXIT instruction to handle a DR7 read 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 + +**/ +STATIC +UINT64 +Dr7ReadExit ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT_X64 *Regs, + IN SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext; + SEV_ES_PER_CPU_DATA *SevEsData; + UINT64 *Register; + + Ext =3D &InstructionData->Ext; + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + + DecodeModRm (Regs, InstructionData); + + // + // MOV DRn always treats MOD =3D=3D 3 no matter how encoded + // + Register =3D GetRegisterPointer (Regs, Ext->ModRm.Rm); + + // + // If there is a cached valued for DR7, return that. Otherwise return th= e + // DR7 standard reset value of 0x400 (no debug breakpoints set). + // + *Register =3D (SevEsData->Dr7Cached) ? SevEsData->Dr7 : 0x400; + + return 0; +} + /** Handle a #VC exception. =20 @@ -1524,6 +1630,14 @@ VmgExitHandleVc ( =20 ExitCode =3D Regs->ExceptionData; switch (ExitCode) { + case SVM_EXIT_DR7_READ: + NaeExit =3D Dr7ReadExit; + break; + + case SVM_EXIT_DR7_WRITE: + NaeExit =3D Dr7WriteExit; + break; + case SVM_EXIT_RDTSC: NaeExit =3D RdtscExit; break; --=20 2.27.0