From nobody Tue Apr 7 06:55:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70801ECAAD5 for ; Tue, 30 Aug 2022 23:18:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231571AbiH3XSA (ORCPT ); Tue, 30 Aug 2022 19:18:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231744AbiH3XRR (ORCPT ); Tue, 30 Aug 2022 19:17:17 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BEC5A024F for ; Tue, 30 Aug 2022 16:16:27 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id s3-20020a17090a5d0300b001fb3ac54a03so5229706pji.2 for ; Tue, 30 Aug 2022 16:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=cNQuNeWyRHQgOj4jrxbhlCOXcJ4T7+kRTqVJB33Ymrw=; b=pwcbOpDxqsQS1Fv94mfH9hIu542GuS2+Fnk+N/X1e0buw7zBEcus1yP5JbKiryySbo 0ty0C7RbSZdYAQ3Vn9cpHaVMDf7XRGSN9BunvsybMV6qFxOpIo+vojMkNU8yaLiAsm3z DR1zpsMAD0Dd8M3bwm+dSKfT67/wAUf4AZauAhuUHP/LavD/9Nz66nDbMmGZi3d/4dKK kAamSIE7uJbaolhD1ExJ9QABS8v2oVT+fY4qpNc7/ZgCOkby0kevQxS/NwHoh6Oymt8z cPBcZalzqN5aoaSygfp3QwiYhb1xCYC7l7uEvoe1X5LqK99KErnXraZZSjhkK5iU9IQB Bgcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=cNQuNeWyRHQgOj4jrxbhlCOXcJ4T7+kRTqVJB33Ymrw=; b=fZgpS+Olkow65SgLW1q+UXleBeqGsl1P+Cq1F+1Sj350ifg/q/S6HH8L1fbYvsJBXN iDk5dnAP56MUkvb18gX56ZWgRl1NUYoKjM8lGnBNHouFViRyUZTZXXEJrfdhrv3N/Joj /fIkhxl6aM/S+Ci6N8mVboXrv5MsDSIITqUGeTNujmWraQbUszbednjq+RIIz5qplHzl uIw//B1ijqpqIMZb4K1ZHDeMdEqi02KBhaKpJaKEwwHXjNj3MveMN+E3mlWGIWgCpRjr JhiTedsmTlI1SqFuTTTqADCZiOyGl5mf+A993jSVAWUerWoWqvF4XLhK61S/1fbqdVoE zl6Q== X-Gm-Message-State: ACgBeo2vRVoFmbLOd4W0IigLEqM5geNnJlj1hsZHINjYuEvGKdDWgJ9N UR+XVMQdZpzkjx655ecYk1uBP/seMBo= X-Google-Smtp-Source: AA6agR4m02pis1qh+TDoZtmCLoo4gyWr8tA9X0my22KIOYFNgMvUeqsuvznp0p9kvjwvgzhImeawt1Fkuxg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1943:b0:53a:8602:6758 with SMTP id s3-20020a056a00194300b0053a86026758mr3548761pfk.47.1661901386796; Tue, 30 Aug 2022 16:16:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 30 Aug 2022 23:15:53 +0000 In-Reply-To: <20220830231614.3580124-1-seanjc@google.com> Mime-Version: 1.0 References: <20220830231614.3580124-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220830231614.3580124-7-seanjc@google.com> Subject: [PATCH v5 06/27] KVM: nVMX: Treat General Detect #DB (DR7.GD=1) as fault-like From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Maxim Levitsky , Oliver Upton , Peter Shier Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exclude General Detect #DBs, which have fault-like behavior but also have a non-zero payload (DR6.BD=3D1), from nVMX's handling of pending debug traps. Opportunistically rewrite the comment to better document what is being checked, i.e. "has a non-zero payload" vs. "has a payload", and to call out the many caveats surrounding #DBs that KVM dodges one way or another. Cc: Oliver Upton Cc: Peter Shier Fixes: 684c0422da71 ("KVM: nVMX: Handle pending #DB when injecting INIT VM-= exit") Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/vmx/nested.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 2ca8f1ad9c59..b540c7bf4753 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3853,16 +3853,29 @@ static void nested_vmx_inject_exception_vmexit(stru= ct kvm_vcpu *vcpu, } =20 /* - * Returns true if a debug trap is pending delivery. + * Returns true if a debug trap is (likely) pending delivery. Infer the c= lass + * of a #DB (trap-like vs. fault-like) from the exception payload (to-be-D= R6). + * Using the payload is flawed because code breakpoints (fault-like) and d= ata + * breakpoints (trap-like) set the same bits in DR6 (breakpoint detected),= i.e. + * this will return false positives if a to-be-injected code breakpoint #D= B is + * pending (from KVM's perspective, but not "pending" across an instruction + * boundary). ICEBP, a.k.a. INT1, is also not reflected here even though = it + * too is trap-like. * - * In KVM, debug traps bear an exception payload. As such, the class of a = #DB - * exception may be inferred from the presence of an exception payload. + * KVM "works" despite these flaws as ICEBP isn't currently supported by t= he + * emulator, Monitor Trap Flag is not marked pending on intercepted #DBs (= the + * #DB has already happened), and MTF isn't marked pending on code breakpo= ints + * from the emulator (because such #DBs are fault-like and thus don't trig= ger + * actions that fire on instruction retire). */ -static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) +static inline unsigned long vmx_get_pending_dbg_trap(struct kvm_vcpu *vcpu) { - return vcpu->arch.exception.pending && - vcpu->arch.exception.nr =3D=3D DB_VECTOR && - vcpu->arch.exception.payload; + if (!vcpu->arch.exception.pending || + vcpu->arch.exception.nr !=3D DB_VECTOR) + return 0; + + /* General Detect #DBs are always fault-like. */ + return vcpu->arch.exception.payload & ~DR6_BD; } =20 /* @@ -3874,9 +3887,10 @@ static inline bool vmx_pending_dbg_trap(struct kvm_v= cpu *vcpu) */ static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) { - if (vmx_pending_dbg_trap(vcpu)) - vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, - vcpu->arch.exception.payload); + unsigned long pending_dbg =3D vmx_get_pending_dbg_trap(vcpu); + + if (pending_dbg) + vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, pending_dbg); } =20 static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) @@ -3933,7 +3947,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *v= cpu) * while delivering the pending exception. */ =20 - if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { + if (vcpu->arch.exception.pending && !vmx_get_pending_dbg_trap(vcpu)) { if (vmx->nested.nested_run_pending) return -EBUSY; if (!nested_vmx_check_exception(vcpu, &exit_qual)) --=20 2.37.2.672.g94769d06f0-goog