From nobody Sun Feb 8 19:25:50 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+64202+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+64202+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1597325230926356.24318242461004; Thu, 13 Aug 2020 06:27:10 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id xxZxYY1788612xkdFxQp59Nl; Thu, 13 Aug 2020 06:27:10 -0700 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.66]) by mx.groups.io with SMTP id smtpd.web11.9860.1597325229751688157 for ; Thu, 13 Aug 2020 06:27:10 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OWWVhLthMTDseN7qR2vt1hnPzSeHQhIy86v+mwJD3fF7R1H8jnYPOEEw2NsRW8kwrJGqA1m4B47hIX45QhtylU9CccPdGqOme0W2JLnzZCpiiq3/Vi0kifk/bIg8dLB5kL02gvVGbpOqYw3IZjl0ZGHvsAAA5L47K2u/nmUGqL/0+8Rx86qHCfmURbD6UAGVd24CBEoI+Ft2UXmw3uLfTOu1Ll3nihJHFuearCLdPHPNzZix+hQF6axR4degQXeVtbX06fLnlPF/ts3cGSzzMqZaEPSK8YZ/JGCKwRW5umgGKLIpeZB2qRmVk9kcsSVD+Sh95XOHS+zNtB9SsHRD/g== 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=MpmAVwK3O2BHUO+rODtPUiNd4cAWVuC9AkscEg0fvl8=; b=Ub9ZhZqI0czscvvqQt8uOhAYsGc0m7AJl0jM9oIHg9So6UPg7Mjc8thsCPYPbOV1Kf6vkwnWLkJRXemsT7zDyEnar9VEx8a4+4hGapvlO3pClFK9qAoRNy07a4CVz2qjHDzBOFkM+ASjJKLvcQ9M8QZo0cfsiKJZDM3GPBD1y0ZW828aE9iR5MK6o31OIM/P83cHP0zvnGiZpW86c+cRk5BcgqkT8Z9VQizMPliuTraXopGRaHqQBARZ+QYp0Fm7XBZgumF7Dhfl4DHRmzDx7TCt9l1PX2NWEn2Pa6soKzYiAetxqXRH9y+x/LmvUkUm1f0EmMzdnDI6Z6Dhao7ITw== 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 X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM6PR12MB4220.namprd12.prod.outlook.com (2603:10b6:5:21d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15; Thu, 13 Aug 2020 13:27:08 +0000 X-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.3261.025; Thu, 13 Aug 2020 13:27: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: [edk2-devel] [PATCH v15 26/46] OvmfPkg/VmgExitLib: Add support for DR7 Read/Write NAE events Date: Thu, 13 Aug 2020 08:23:13 -0500 Message-ID: <48f6265d7128411f9e0faffd3d42f39bbc2bb32a.1597325013.git.thomas.lendacky@amd.com> In-Reply-To: References: X-ClientProxiedBy: DM5PR07CA0126.namprd07.prod.outlook.com (2603:10b6:3:13e::16) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-Received: from tlendack-t1.amd.com (165.204.77.1) by DM5PR07CA0126.namprd07.prod.outlook.com (2603:10b6:3:13e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15 via Frontend Transport; Thu, 13 Aug 2020 13:27:07 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 34659e1c-ec18-49af-b6fb-08d83f8c939e X-MS-TrafficTypeDiagnostic: DM6PR12MB4220: 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-Message-Info: /nXu+KKVSwUMk3STZNtmMZ76Ni+WUHot3mw0hs2kbfVAsEUCEvwuD3fIc/uEA45jytA6RlBYjgU2uGmNDtDHumVVxaK3GnjdH3hMMNyNCyLYmjb/6SuIApIB3eyPn7PPzlcs4LQQaB4PVAEfCMHdZQZubwh3ZWvwy/nPbtLEypaNiRpNwrmbi28q498F9FkBlP7669Qzyj0019EzhMC3J4fN7//6UXJx80JmYt1Vs+TxxBud3f5WnIPyKiN9RSg+wd1g/g7A6BYG/HQOsLyFJZhVpMyzY8jtyic01F2wdYJfLKyApyJ98FH1iSVK5cc+8S81Zv3ScAODChhZHhCmh007opnTiu7ww9submI878MnKca+kYi8+Gk6N201ikzW6+GwcaOkF9AJRQ+vfTW0IE5JiSg3mTaryAsIFmNoxXkIFebbU++bWzoe1yr1kHMnidpgZGHeAfDzE0rQQJfLhw== X-MS-Exchange-AntiSpam-MessageData: fOPUsry6SKAIDVTtNzBgMHe4XsNybEHexieLpjm9kpJ+7aIzdX3wkN2i4xrH3Nzs5em/onZiGGFOwM5MhidltuT0aIyDkPMKVM76bmNxJsa4aFZOt2y5FcZj3Ag0nhcUVYdte95ey/HDWaXqS1t/H9tcu8qjbFzCqOMZnhw+3QufORntCyNvyVLNUPFMmetnpiTbanlnzJQO3WaO+EADzuiRkttdWJIQ4QdlNha41sN/+kIGTwasq7RucpSrOYQpHN6GAsvMDJHEqyzMs04pQKLTKf6LXkPQjJVw4NbKgtmQ6iF8zQYZz69JSZfHDN3xHA6FdhFhN9qjx7BZlL/Fy0sRv5cgsbX6Xv2/QLxMjBJ9QlMvg7PRVxwXGDYtxok3WEGcJll2FA/Djzkf10mY5OuVbaudMsw7iPrYIEFTDTwBpOaVzp7mPHGBy0c2/f4+cos0q/QWmkaSKFaBdZtR6+Cs+sa8NHj8RQ8BM896XdjDcG16DTHZqByGA3jpf0aCCrwabqmVE3RxCCtohdHrEhO8Uyd8UVm/d5ORUJg3c8cQcF/lrR2IQACweTMZZ49vwzM9wk+uU1nXdvy1SYlIm5tHFHkq2zQqg/9UkS4euyBnvsJWTy4jrdFj/BC8YFvGNr8rBzAaGBoyAIDKUf4MLw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 34659e1c-ec18-49af-b6fb-08d83f8c939e X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Aug 2020 13:27:08.1317 (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: yRiJRJaytI+vmzmgCZAVRt5WhB1O4TSFevaBYJBRDHVLJV7X1Os2PS8U124+GN/5Mi3mLz6rAwYXwxO4zeB8Kw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4220 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,thomas.lendacky@amd.com X-Gm-Message-State: 3tG9X6qOIWERIXqqY36ZkX4xx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1597325230; bh=QeA4OwRVJpCdRONeb4EFYhDYjvEywjwEI5eflFzjwhM=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=hwSKiG7e6irXbSNGqmEkxpcErKPKZcrMgLeWZcx3lypLtsmBnJJNvbtXqiS1HvaGSmN 3a+qyWBBQ00MdABiN74RiS5ROvZgKzhXunlvoTRablhItqipdiB8uAm/QK5Ck/l237s/3 gxpSd7KlsRy/t0HF1UwFS9cnO/xW97fsRws= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" 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 a741b080dd1a..8e42b305e83c 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. @@ -1482,6 +1490,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 the + // DR7 standard reset value of 0x400 (no debug breakpoints set). + // + *Register =3D (SevEsData->Dr7Cached) ? SevEsData->Dr7 : 0x400; + + return 0; +} + /** Handle a #VC exception. =20 @@ -1526,6 +1632,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.28.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#64202): https://edk2.groups.io/g/devel/message/64202 Mute This Topic: https://groups.io/mt/76167010/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-