From nobody Sun Feb 8 13:53:50 2026 Delivered-To: importer@patchew.org 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+55267+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 1583190478897121.79630456810037; Mon, 2 Mar 2020 15:07:58 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 7R6gYY1788612xqMroBqwsDX; Mon, 02 Mar 2020 15:07:57 -0800 X-Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web11.694.1583190475799820802 for ; Mon, 02 Mar 2020 15:07:57 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K0+Nw3lttyRZJrV+zGmSOMFil+ljOt39vIKr3I3xY9DEA8nciGvccrPwPGpI3li3JRhgZvOgIB95bK4KxqP242JChwzrpDiVYaE7+wDyzHoSqELrMCxPBFqnlmdFhrS9HxDKSx2DGD+Fx2sVs/fJ2LVm/ZPa0vrbBL4UuA5lCUggSRXieHLEuRUKOhZeICCGeclWywA5wn/ZUIx/hRU4V2aNR+CqVJ/v8dYnPznt1CUrms70JWhT/QLiwreN2uaAmz+kBY2lNeOc1kT3kWVOXEGhxtoI427RGS/ncicj3yTWr+cQK4YB3aRcu9P1x3MxiUxowxLysNj7Klnb8ZfZ0A== 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=AcU+TK3iJ5IgPQuehzCG/+j2mPgOSwVHvaqQ9oafhA0=; b=DIExGd+EP0GhIHMEo3u/tUGLuHBRGIfygnWpyxujDUwxa8Udzp7XsV/7AYrzGEokWffwnkCyrbZQEvyGF9GMLo3KGHRCHhRlCD4HlsMqXIWg73j17ErjfxysZFSgWKCFs9FyXju+QQUb9vVEVCiwDHg6IZdO3iV9CPj5wKgfYTt8maVcwjjcv80GqYJ7mfjnp6FLYPAjaEnO9y/oCuA4TMD7aPGmpFWmW/t5shVko+Cko1sPiL/6xpmR6G91iYobkEG07Yn3JbTbOYUdyOaf2UA84OI6ovCV7Jjx8ZaTkF6whYVlzd3kMgqRfOuDnaJDRmg6++F6gUqM+6tcz3fx/w== 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 (2603:10b6:5:15e::26) by DM6PR12MB4201.namprd12.prod.outlook.com (2603:10b6:5:216::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2772.18; Mon, 2 Mar 2020 23:07:51 +0000 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::f0f9:a88f:f840:2733]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::f0f9:a88f:f840:2733%7]) with mapi id 15.20.2772.019; Mon, 2 Mar 2020 23:07:51 +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] [PATCH v5 24/42] UefiCpuPkg/CpuExceptionHandler: Add support for DR7 Read/Write NAE events Date: Mon, 2 Mar 2020 17:06:55 -0600 Message-Id: In-Reply-To: References: X-ClientProxiedBy: DM5PR2001CA0013.namprd20.prod.outlook.com (2603:10b6:4:16::23) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-Received: from tlendack-t1.amd.com (165.204.77.1) by DM5PR2001CA0013.namprd20.prod.outlook.com (2603:10b6:4:16::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2772.16 via Frontend Transport; Mon, 2 Mar 2020 23:07:50 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8394c731-4b7e-482b-df52-08d7befe8839 X-MS-TrafficTypeDiagnostic: DM6PR12MB4201:|DM6PR12MB4201: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; 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+55267+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: Ep8t7oiShyByKwu+QyoT+hCVY/n88KoEcKu2hCsA+szqu8Ok8KSVpir5QdhVyx1vqcw4yvHBrYPxVlkc00EvgrRcbB75tzvPNhHO2iCpKGxa01AeJnKZx3vGI6tNfb2q80YPSYAOBDq/p68wrj15iw1xeAHOTA7jKUtS6sxiFA8E/agJplJYtryLGziP75F9DlDwXVh9qsi/pWPEh0d68zr8OP9t092JuCWg256GybTk1MTeC6qRJpBT4bqEZS8ApBcIe1lW8bmlW7Zk0aRTwf/LEht/Ie0/SM7L87ir45rx7tUDMONJCDhFXQwm7R6ZwmY3n6xJz7MxZd2tBvZgab6SU6hdpv6L/AvJSUCzuHSnIHGSWUpewkvXcupfjz+RLx/Kr6dNsjscJXX8gp1EZqA5M+P1ERyJIY6zNKTLaUpV4OeZRhpWCAcQubtW7y7tj16u4/GW38gkPIgTLThJV/yQxkftqhomIJr6p1Bq5qbRJHYdCLIZl0VjD1EU5SXtP/KHJ/GRrZGceBOj5Rr/Dw== X-MS-Exchange-AntiSpam-MessageData: T5N9o/kvJ8SP7duFVO4kkibb0e43ojq/Hsmb8h/yiDUHZ01PmzlBWJYUBiYwGXTmIFG02eTUbzoKxogK+6zmPS4SntZzc0iATzwWi90QTcNruKcha9bZ3JpZaA7g9Gv1AhZRuwMbkZ4i8eADu++I6w== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8394c731-4b7e-482b-df52-08d7befe8839 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2020 23:07:51.5497 (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: qrJdu/3HBDesZ0qgxHvHcWUizLp/h8fMeN62Cn2NWL9zjp2DsShRO2CBbfmQ0wsFy1eLKI5CgjVa2u1KFlwT1g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4201 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: fICl7G3wcRrjnlfqEWuulspPx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1583190477; bh=cd0wh7WBpVcPqotihPBLaDHahrCM2VCfz7w2340cxIk=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=FaJHKB45ABfBR8RjHpGY9MgOyRPQ1ru8QFUtRubRdcLrcy69B3lAGZeA9m6UmDBfr/r Z4AGjr57uGEy21ZHTaeqc228abIOxURcr5eJskc4DGJ24dZKpaIM0sfpVvXINIw7qYFr0 LL3XhaGjntj5LqYNCMrZU+N40q5sD6JqhDE= 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/ArchAMDSevVcHandler.c | 68 +++++++++++++++++++ .../X64/ExceptionHandlerAsm.nasm | 17 +++++ 2 files changed, 85 insertions(+) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHand= ler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c index c35cfabd4069..253eeb54f65a 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.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, @@ -1076,6 +1082,60 @@ RdtscExit ( return 0; } =20 +STATIC +UINT64 +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; + UINT64 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 +UINT64 +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, @@ -1092,6 +1152,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..26cae56cc5cf 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm @@ -18,6 +18,8 @@ ; CommonExceptionHandler() ; =20 +%define VC_EXCEPTION 29 + extern ASM_PFX(mErrorCodeFlag) ; Error code flags for exceptions extern ASM_PFX(mDoFarReturnFlag) ; Do far return flag extern ASM_PFX(CommonExceptionHandler) @@ -225,6 +227,9 @@ HasErrorCode: push rax =20 ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7; + cmp qword [rbp + 8], VC_EXCEPTION + je VcDebugRegs ; For SEV-ES (#VC) Debug registers ignored + mov rax, dr7 push rax mov rax, dr6 @@ -237,7 +242,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 (#55267): https://edk2.groups.io/g/devel/message/55267 Mute This Topic: https://groups.io/mt/71687824/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-