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.79]) by mx.groups.io with SMTP id smtpd.web11.5544.1595366598591878279 for ; Tue, 21 Jul 2020 14:23:18 -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=F6ZfKMDc; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.94.79, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BAy/dlih5ymzWxcw9wFUuLCRmRWDk6ss44EgmTAHsQYgu64V/4SUBm+r6ZF0mp3j3nLwCFILxNKmXGjnqx0S8QgrdiM+NbhyaSv9fknSonscbaSAngxrHrITNPGpN9EdqLBvYy9e9SM8UQzMM3QYczMDot3BaHxluB5EQ8jOLW89V70BXtKdXacUPJrNcBFUeNMlUjClhZudOoRNKVLbOd2ysqO19vHN9zFBqP4hjjcxK5FoOV1wM14r4uk8iqgwRyyaM394CV8CDKvy9/PtL2drPc9+SkhIJq2fEsdWrwJBm3BZOBvmjJivJjcbsAO1K3MzEIPvEpaITnPGiFEqEQ== 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=KgjUFrfPNkr/v6RntFuo7DZ4BqRdRWyt6vr06PzXK3ohb1bmRl7eNO9GZgDyAuThdeyCnpAqmxymVuXaIwnz3KWZiUC1fwRbvRcXA5db1fdTQXV01zjP93FuD8aIz8wa7/F6+3NX6va163ljEkU6oezaC41QiVpUUe81j2WeiOAbkCws+waaVajfoVrCpcynJ3Nb9cFhssWSfWSzHrMXMuC54XEf8Kj6FSPF2uYA0m6tdeNPp5S6JQn0d0XcFoDu0iBVKC/b16qIGxI0Ce+GXcwT+K7aGfflDeYnb+6E8kapP+K7h6Eggb4ssk2Dc1HNdJxKzXShJHzo/S80hpBsqw== 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=F6ZfKMDcMCBx9ujIIPDo8tUHYUutzTJVg1i18XPKAWgMCgFP47WTTl3BP6ExSpsLZ63Hcmw2moz07DF2U4Faj5p2PNxACiaufWvEEpTazBEoksgCeboccfoua00W4cxyF6E8WAqZvpm1K7a+XHb+rtu7PkjyGML2LseaRJPMdqs= 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 CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) by CY4PR12MB1143.namprd12.prod.outlook.com (2603:10b6:903:38::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.25; Tue, 21 Jul 2020 21:23:17 +0000 Received: from CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b]) by CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b%5]) with mapi id 15.20.3216.020; Tue, 21 Jul 2020 21:23:17 +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 v11 26/46] OvmfPkg/VmgExitLib: Add support for DR7 Read/Write NAE events Date: Tue, 21 Jul 2020 16:19:03 -0500 Message-ID: <2343dd7cfbe0c9eb8fb19c252d3a189b15290e4a.1595366363.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: DM5PR1101CA0007.namprd11.prod.outlook.com (2603:10b6:4:4c::17) To CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) 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 DM5PR1101CA0007.namprd11.prod.outlook.com (2603:10b6:4:4c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.21 via Frontend Transport; Tue, 21 Jul 2020 21:23:16 +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: 5499f3fc-c227-4ff5-2e6f-08d82dbc48c3 X-MS-TrafficTypeDiagnostic: CY4PR12MB1143: 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: Ar9/IYz8OcP7gSfF2vLbCMdSYWIExABK2a5DBsOT4DItAdFCCUfHVMEWEzewjaeMZDJKqCJE4RYmL1PtGPgR6menQqA7MSv1vzInH4MCsh97o/CLt39Rp7TjVx0jdA8n+nbEWF93Ldd0ItnA9mIt90XTORZupHTBlv0A6xea39TqreXiTR2rZ+7scLDRocsCf+9YLtNlbznu1+wllydbv73V7/b17po8RPNsXgSzR24pKL7C9LVFQMwl9eT5Fc63VNt5fySDAGtka15JLNEqki1cpIANlyxo2ePKp7YLUGx0QbV6fRNiAA+ZsYTHwpnt+vgfdktxQ96255VRA3vhgaKUlTKNAHSe82lJsDx3R6HqGDb8ot7z7mqE70NWfeBSeAWvccTq+f3RPQ2EE6RwCzJ3QKxWtTu/Ct72GnfT04eVanKAEtjtkj5YvRVDAPxW6HZavBC1PKFReuJGc08nJA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY4PR12MB1352.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(396003)(346002)(136003)(39860400002)(376002)(366004)(966005)(6916009)(26005)(6486002)(86362001)(8936002)(2906002)(66946007)(36756003)(8676002)(16526019)(186003)(2616005)(956004)(54906003)(478600001)(4326008)(66556008)(66476007)(7696005)(6666004)(52116002)(5660300002)(83380400001)(316002)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: MMGSASQ2AfzIa69nJ/HpXsgQCLjt6YIzBxIMDzIl4Op4Su5WDjDW+hK7wh0TcVpa2VH1c9p9aekoSWZpmYBxW453UvcF9rAqqTfSlGg5blkgfz19MFVxFcUBh4pCN7KwM+bgf1x2ap5QSaNggxjSfFzOasWcEjOb52u9Nhr+THhCy13qvuas2MHNstnyC012zrKNZ6XQuFKQiqupQc5KNJO8za/zUMDewSva8W+vNu1nVKbzuHtrXxYoDp1/8KCfZRZB/iFFpZ4lno5Eo0DaQgvZgNcr0ydJGp7W1RrYvmtqVpCK2Y1t8GseswDqPxF/wMrFvxDzaEkdjUz5jfBdBr90wiuf47Nv+xoxbFmEp5y766bBDD80cR4u34BDg+hkJxpA0KZ2/QwO/aSr6C6xGrt17zgTfbNbn/WtJNOfZvTr/Fxozcww2o/Mw6o4mub13G60ArQe2KOMl2mWqQ90ikybRYOPjHJLAADX63BbYNE= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5499f3fc-c227-4ff5-2e6f-08d82dbc48c3 X-MS-Exchange-CrossTenant-AuthSource: CY4PR12MB1352.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jul 2020 21:23:17.2676 (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: 0igwiVf24H+2pDdt6CSCHi/QkLwx1KCmDrNBmU7QF1Qau0CVDa0ESvcOjQNwwJgwa1VMXNKYj0jGAHqCmHx46w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1143 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