From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67E1D30ACEE for ; Mon, 11 May 2026 15:23:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778513038; cv=none; b=gCtmj2x5vB0wmP0bD21hn8FbBK88WlNKecYYZOy3DBv6KhmwfkygRWCMJeW/plckuqR2rb3f91ZoKgWZ0hdKy1PW6AJY+HHs5T3jx/lwUrM7C2sQd16Dax7spRRQGC1yViA5gPIGLNxRBcCK28mg/huG7mDGDzQltTt3jgwxuvw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778513038; c=relaxed/simple; bh=s/UJ8hPzD5D9QR4hg0mEoFlzx2GCqf77Zmajfb305kg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jzl2iayFlFouNziDNopTjPtD92h4WhiP7f4Au54ZCfPYXIUtq8pnxwSHObYzhk8o5FwPoXC3A/ApzSFucDa/Hon7D11pIEJZUf4Grh+NPYS6Mkt8228eDV+HYzh/ZN9CFUp1nIVP0xVeHUN1DbqEMeoHtTB6onITVW7ZJGcr1mY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cXPpdHZY; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cXPpdHZY" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bc977e6aedso18630215ad.2 for ; Mon, 11 May 2026 08:23:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778513036; x=1779117836; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G2MLFADvnNOuv2hkdSJ3+9BTtkHlP1NDEUIePsCBd6w=; b=cXPpdHZY9VOYf6A+UKJRBkk7YmdwWrQYbW3LiQqWTIgAn4JLZm8sRgOCxFxg2lnBt8 kIzgtCMRG5/aoaxvwfV4Xj1BN6xH66Tb6aqw1lJF+MnUZ0BH7hhuRdzLpV/LVkp+EFzS zIpcIkX+6VSDB6ufw/a0KXnvs7Z6n4ilLTRrAn94PrfW7AWA2hOHQEnaf0PsyWDNEnnz b3Qgkzt3M3MktXFrvobRx3uXKQ0Sdm6papQhfm3TkjYPMM6dFxk8Flpxh9MxH8UR6YUN 9pOEaYsb3CwdwHVGDWSi5hFbWUssnND8NpCe/a+FjNH7by3kZ6/x43/Y+QjJvNHxFEO/ sAlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778513036; x=1779117836; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G2MLFADvnNOuv2hkdSJ3+9BTtkHlP1NDEUIePsCBd6w=; b=roqg40XDrn9zUEtapHA0QuTQmfmjlAjTHISJlXFtW3Do359cW7qlFTYVtetS/skBcb et6dXgt4SNDsXEfoiwvpn8z9MbxAibEXBgu45I8jRWmCQGMcSud2nNCtRkE1q/WWRqsa FKFQBU1HDn/WnYCRSLctX/S/C/9KukGssKnu5SGu5iQIjZ3mv4MoibWBBKXrFvbia7oq AcyT3v3M4yCaGjogHw0m8J+jmqdzG5EdcORssclQIrtRJmLPI4yhMAbpUWNZhudy6QaJ eYAcwbJGAVIsa03v8ojz3ZxalEzefRCSpeig+hzmK+BBg4W3VDIv1U2Ef1X1nTwGLitL V53w== X-Forwarded-Encrypted: i=1; AFNElJ8Uo8LP9u448UoJNK2UoTgI7Of+VJe3mEBtu7rdBQ4Hlgt4xSOViu35/UQ5nVF/juZJkiIeBYJZpY226P8=@vger.kernel.org X-Gm-Message-State: AOJu0YwQ/lyDkj5GJnUysEm4oj5upkxfSMQyq2paACnNYMq/hf3z0vcp Derk/kayM3n8urFG0ubwF/tZPjs4pyKKDbuZY3K+NmoArsoJ2g7uQFZe5ZC1LvCja/iRLWxmAGx 4P48qaQ== X-Received: from plhu11.prod.google.com ([2002:a17:903:124b:b0:2ba:792a:18a7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ce0e:b0:2b4:5e0f:c239 with SMTP id d9443c01a7336-2baf0d14b48mr148681805ad.10.1778513035464; Mon, 11 May 2026 08:23:55 -0700 (PDT) Date: Mon, 11 May 2026 08:23:54 -0700 In-Reply-To: <9b859ab6a6b59e5ccfdac741459117996fe2da6e.1766066076.git.houwenlong.hwl@antgroup.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <9b859ab6a6b59e5ccfdac741459117996fe2da6e.1766066076.git.houwenlong.hwl@antgroup.com> Message-ID: Subject: Re: [PATCH v2 2/9] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation From: Sean Christopherson To: Hou Wenlong Cc: kvm@vger.kernel.org, Lai Jiangshan , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Thu, Dec 18, 2025, Hou Wenlong wrote: > Record DR6 in emulate_db() and use kvm_queue_exception_p() to set DR6 > instead of directly using kvm_set_dr6() in emulation, which keeps the > handling of DR6 during #DB injection consistent with other code paths. > > No functional change intended. > > Signed-off-by: Hou Wenlong > --- > arch/x86/kvm/emulate.c | 14 ++++---------- > arch/x86/kvm/kvm_emulate.h | 6 +++++- > arch/x86/kvm/x86.c | 5 ++++- > 3 files changed, 13 insertions(+), 12 deletions(-) > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index c8e292e9a24d..997cd6e46d90 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -540,8 +540,9 @@ static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, > return X86EMUL_PROPAGATE_FAULT; > } > > -static int emulate_db(struct x86_emulate_ctxt *ctxt) > +static int emulate_db(struct x86_emulate_ctxt *ctxt, unsigned long dr6) > { > + ctxt->exception.dr6 = dr6; > return emulate_exception(ctxt, DB_VECTOR, 0, false); > } > > @@ -3834,15 +3835,8 @@ static int check_dr_read(struct x86_emulate_ctxt *ctxt) > if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) > return emulate_ud(ctxt); > > - if (ctxt->ops->get_dr(ctxt, 7) & DR7_GD) { > - ulong dr6; > - > - dr6 = ctxt->ops->get_dr(ctxt, 6); > - dr6 &= ~DR_TRAP_BITS; > - dr6 |= DR6_BD | DR6_ACTIVE_LOW; > - ctxt->ops->set_dr(ctxt, 6, dr6); > - return emulate_db(ctxt); > - } > + if (ctxt->ops->get_dr(ctxt, 7) & DR7_GD) > + return emulate_db(ctxt, DR6_BD); > > return X86EMUL_CONTINUE; > } > diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h > index fb3dab4b5a53..7fe38b174e18 100644 > --- a/arch/x86/kvm/kvm_emulate.h > +++ b/arch/x86/kvm/kvm_emulate.h > @@ -24,7 +24,11 @@ struct x86_exception { > bool error_code_valid; > u16 error_code; > bool nested_page_fault; > - u64 address; /* cr2 or nested page fault gpa */ > + union { > + u64 address; /* cr2 or nested page fault gpa */ > + unsigned long dr6; > + u64 payload; Please split the introduction of the union to a separate patch, mainly so that the effectively zeroing of ctxt.exception.address in init_emulate_ctxt() is isolated, e.g. in case it somehow causes problems. But that will also allow introducing the inject_emulated_exception() change separately from the check_dr_read() change. > + }; > u8 async_page_fault; > unsigned long exit_qualification; > }; > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index ab298bfa7d9f..f33ce947633e 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -8925,7 +8925,9 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) > { > struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; > > - if (ex->vector == PF_VECTOR) > + if (ex->vector == DB_VECTOR) > + kvm_queue_exception_e(vcpu, DB_VECTOR, ex->dr6); This should be kvm_queue_exception_p(). I also think pivoting on DB_VECTOR is the wrong approach. Rather than key off the vector, add payload_valid (to match error_code_valid), and then do: struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; WARN_ON_ONCE(ex->vector != PF_VECTOR && ex->payload_valid && ex->error_code_valid); if (ex->vector == PF_VECTOR) kvm_inject_emulated_page_fault(vcpu, ex); else if (ex->payload_valid) kvm_queue_exception_p(vcpu, DB_VECTOR, ex->payload); else if (ex->error_code_valid) kvm_queue_exception_e(vcpu, ex->vector, ex->error_code); else kvm_queue_exception(vcpu, ex->vector); PF_VECTOR is special because it has both an error code and a payload, and because it needs additional handling on multiple fronts. > + else if (ex->vector == PF_VECTOR) > kvm_inject_emulated_page_fault(vcpu, ex); > else if (ex->error_code_valid) > kvm_queue_exception_e(vcpu, ex->vector, ex->error_code); > @@ -8970,6 +8972,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) > ctxt->interruptibility = 0; > ctxt->have_exception = false; > ctxt->exception.vector = -1; > + ctxt->exception.payload = 0; > ctxt->perm_ok = false; > > init_decode_cache(ctxt); > -- > 2.31.1 >