From nobody Sun Feb 8 17:41:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+50962+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 1574280461655122.90929284314655; Wed, 20 Nov 2019 12:07:41 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id LRbPYY1788612xjaQiICLKkR; Wed, 20 Nov 2019 12:07:40 -0800 X-Received: from NAM02-CY1-obe.outbound.protection.outlook.com (NAM02-CY1-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web10.13521.1574280459809158591 for ; Wed, 20 Nov 2019 12:07:40 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W19IvKNcURFfl/654WjMNGz8JU6Kh0mJuAuY7SefeyXkBBbqQPb67darEQLeZWWE3gw/92Eu8QEFEAuzn7p1z1R0EgoBFLJRGuwKtfPdj2KLkc1970H/i80uZcQIdroU09zJBujAMZ55fDlYNE1JCRRrBXJltKqANrqoKdMU8CaoQg+F9LNU++rnbJcxmujza1qcStUCQRru8bgfOrFjyLU6oOFt2lNYezbYzyY65+gG2XE38E0wNgmWNdt5OHzE7LdKB9zftcRs9+Ok/808zaU8eDgi5wGZ7w/NClyGyvwHmerD5zjuItk/1NtT+wwpsh6nQXAseFY2yhFbRKVYWQ== 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=8q+NdfHOUszfn2bpKULnkTtXOSx6c5BPWzg/JDQ6WfU=; b=MG9kamve5K35TxDXf/6PLSxutOJRhTSvlTU7warSMIY6TZPhHeNDrfI1Wbt0bKZuZn1sQqqjcirVBbArCqbJxnLxGNunC17xU96zJlAtw86uCJte8QyRowCx4D2urTI8bUs0Sin//6V7+X/VJFg3RMQwTLLAyRz6xaPRdccMT74iiLbCZ9hJy+ce3kFjO3SdQbXrYJyP5pCxn1skiJODIV+2Nj0WL64xnnuH4ECbvrmW/TKihNRCRUrSvg50/XUxK7MtjnOoMbgjYTwcKLgSjuFC7TFlsJFhyI84XMS5ndXPHtkxrVwSOl2SUzfUAxfkS/3TqFDM6f5KPP6+vH9T7Q== 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 DM6PR12MB3163.namprd12.prod.outlook.com (20.179.71.154) by DM6PR12MB3675.namprd12.prod.outlook.com (10.255.76.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2451.28; Wed, 20 Nov 2019 20:07:39 +0000 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::dd0c:8e53:4913:8ef4]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::dd0c:8e53:4913:8ef4%5]) with mapi id 15.20.2451.031; Wed, 20 Nov 2019 20:07:39 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io Cc: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Michael D Kinney , Liming Gao , Eric Dong , Ray Ni , Brijesh Singh Subject: [edk2-devel] [RFC PATCH v3 22/43] UefiCpuPkg/CpuExceptionHandler: Add support for DR7 Read/Write NAE events Date: Wed, 20 Nov 2019 14:06:44 -0600 Message-Id: In-Reply-To: References: X-ClientProxiedBy: DM3PR12CA0087.namprd12.prod.outlook.com (2603:10b6:0:57::31) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 89d6619a-7c38-42c8-c150-08d76df54b35 X-MS-TrafficTypeDiagnostic: DM6PR12MB3675: X-MS-Exchange-PUrlCount: 1 X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+50962+1787277+3901457@groups.io; helo=web01.groups.io; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: tpyx6auSBtEqoaOePjNuSSKNY7I/mpTJJbR3IkU32CRWu1Kfn1nmzLxn6BzuQS4YFSTvylM0mDuAxQa2YatLbFhCGl0P/8URdyRfWXX2gMpQo5VY2l7Li+UE2wZp8/aWijfTl6b9sTeNe+KhZdW64UMASjRwvbkMrrGCZsVHG7UkQ+K2KEYadu9GU+LNcMThv7wFkHUwI4eSaKkqrqCQGW65rMlUTgk3SJ1QC312qMokT08P+fywCOzzD84h1ILWcFCVmGzp7uwYoGERX0CuwELlPCtvYA7ObfuxKfpQdwYAwbZvcZA3Sj1vuukTjpGEuAQPtFK5svPqNzYE55GOv7o9jfUzG1mQ4qwSaCwh8O0exVrqEdV2sjjwadA57PWyL4ST6ae6tYTInhSUueeO18yVtT7u1WpYFYRSMKBcjyuXZaCqR+0q12FVmUUY0pg8+uqDZ9x8TtSMVWVbX/fIafozWmiyw0x8p37sFa0fYFM= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89d6619a-7c38-42c8-c150-08d76df54b35 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Nov 2019 20:07:39.3962 (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: N7miz8e91fjl88TwXUJ/ywWtLJEXkl9kdp6KIZBZOaoZtsBxsLiW21wTW2NKz2Fty8yhEkNUl06tohOebQDjsw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3675 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: cq0J39AAA8TmsM5RJAC7IYlex1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1574280460; bh=ARyVI/sO7R1gBUmuQ/tcz57TJ/WM2+Bel7I3V+GwWbY=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=FyCrDrZ90Tt4LA8TnEx/0COgAzCWfKWARi35c3XmwsqMRoQR4IsxVGEcAtgir74lwss t6bsI/EBaiGiOoKaXr2VhpxHYBIf7RFKaT/2HpeFScRbeb6vZWZ9F+t0FgDJNT+0swxzw ggYhkcFCBxo96JDqmH6zG7SiDFz/KGjol8s= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. To avoid exception recursion, a #VC exception will not try to read and push the actual debug registers into the EFI_SYSTEM_CONTEXT_X64 struct and instead push zeroes. The #VC exception handler does not make use of the debug registers from saved context. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Tom Lendacky --- .../X64/AMDSevVcCommon.c | 68 +++++++++++++++++++ .../X64/ExceptionHandlerAsm.nasm | 15 ++++ 2 files changed, 83 insertions(+) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c index 1d7c34e7e442..22393f72d795 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c @@ -13,6 +13,12 @@ =20 #define CR4_OSXSAVE (1 << 18) =20 +#define DR7_RESET_VALUE 0x400 +typedef struct { + BOOLEAN Dr7Cached; + UINT64 Dr7; +} SEV_ES_PER_CPU_DATA; + typedef enum { LongMode64Bit =3D 0, LongModeCompat32Bit, @@ -1081,6 +1087,60 @@ RdtscExit ( return 0; } =20 +STATIC +UINTN +Dr7WriteExit ( + GHCB *Ghcb, + EFI_SYSTEM_CONTEXT_X64 *Regs, + SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext =3D &InstructionData->Ext; + SEV_ES_PER_CPU_DATA *SevEsData =3D (SEV_ES_PER_CPU_DATA *) (G= hcb + 1); + INTN *Register; + UINTN Status; + + 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, SvmExitDr7Write, 0, 0); + if (Status) { + return Status; + } + + SevEsData->Dr7 =3D *Register; + SevEsData->Dr7Cached =3D TRUE; + + return 0; +} + +STATIC +UINTN +Dr7ReadExit ( + GHCB *Ghcb, + EFI_SYSTEM_CONTEXT_X64 *Regs, + SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext =3D &InstructionData->Ext; + SEV_ES_PER_CPU_DATA *SevEsData =3D (SEV_ES_PER_CPU_DATA *) (G= hcb + 1); + INTN *Register; + + DecodeModRm (Regs, InstructionData); + + /* MOV DRn always treats MOD =3D=3D 3 no matter how encoded */ + Register =3D GetRegisterPointer (Regs, Ext->ModRm.Rm); + *Register =3D (SevEsData->Dr7Cached) ? SevEsData->Dr7 : DR7_RESET_VALUE; + + return 0; +} + UINTN DoVcCommon ( GHCB *Ghcb, @@ -1097,6 +1157,14 @@ DoVcCommon ( =20 ExitCode =3D Regs->ExceptionData; switch (ExitCode) { + case SvmExitDr7Read: + NaeExit =3D Dr7ReadExit; + break; + + case SvmExitDr7Write: + NaeExit =3D Dr7WriteExit; + break; + case SvmExitRdtsc: NaeExit =3D RdtscExit; break; diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandler= Asm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAs= m.nasm index 19198f273137..a0549f7ae6bd 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm @@ -225,6 +225,9 @@ HasErrorCode: push rax =20 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7; + cmp qword [rbp + 8], 29 + je VcDebugRegs ; For SEV-ES (#VC) Debug registers ignored + mov rax, dr7 push rax mov rax, dr6 @@ -237,7 +240,19 @@ HasErrorCode: push rax mov rax, dr0 push rax + jmp DrFinish =20 +VcDebugRegs: +;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7 are skipped for #VC to avoid excep= tion recursion + xor rax, rax + push rax + push rax + push rax + push rax + push rax + push rax + +DrFinish: ;; FX_SAVE_STATE_X64 FxSaveState; sub rsp, 512 mov rdi, rsp --=20 2.17.1 -=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 (#50962): https://edk2.groups.io/g/devel/message/50962 Mute This Topic: https://groups.io/mt/60973119/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-