From mboxrd@z Thu Jan 1 00:00:00 1970 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 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=Nwc+CsTg; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.92.66, mailfrom: thomas.lendacky@amd.com) 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 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=MpmAVwK3O2BHUO+rODtPUiNd4cAWVuC9AkscEg0fvl8=; b=Nwc+CsTgvjuFO95T3pwkYhy2/HrCoJoJK2sV7legXRkK+rvmVBebGTyV3TywOod9QaCD/rHNe//qB8gwln4BOkCRea4lrwx39YZy6mz9+gVhqxqIDxpq8r4YBFNq6pwHyIWrG+blrlBTy8JdxyGe7nuW3YwCkLO570Sj9TVr9lA= 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 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 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: [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> X-Mailer: git-send-email 2.28.0 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) 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 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-Mailer: git-send-email 2.28.0 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: BCL:0; X-Microsoft-Antispam-Message-Info: /nXu+KKVSwUMk3STZNtmMZ76Ni+WUHot3mw0hs2kbfVAsEUCEvwuD3fIc/uEA45jytA6RlBYjgU2uGmNDtDHumVVxaK3GnjdH3hMMNyNCyLYmjb/6SuIApIB3eyPn7PPzlcs4LQQaB4PVAEfCMHdZQZubwh3ZWvwy/nPbtLEypaNiRpNwrmbi28q498F9FkBlP7669Qzyj0019EzhMC3J4fN7//6UXJx80JmYt1Vs+TxxBud3f5WnIPyKiN9RSg+wd1g/g7A6BYG/HQOsLyFJZhVpMyzY8jtyic01F2wdYJfLKyApyJ98FH1iSVK5cc+8S81Zv3ScAODChhZHhCmh007opnTiu7ww9submI878MnKca+kYi8+Gk6N201ikzW6+GwcaOkF9AJRQ+vfTW0IE5JiSg3mTaryAsIFmNoxXkIFebbU++bWzoe1yr1kHMnidpgZGHeAfDzE0rQQJfLhw== 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;SFS:(4636009)(366004)(136003)(396003)(346002)(39860400002)(376002)(2906002)(956004)(36756003)(66556008)(54906003)(86362001)(966005)(2616005)(6666004)(6916009)(478600001)(8676002)(66476007)(316002)(83380400001)(52116002)(6486002)(7696005)(8936002)(66946007)(5660300002)(186003)(4326008)(16526019)(26005)(136400200001);DIR:OUT;SFP:1101; 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 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 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 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 @@ -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