From: Hou Wenlong Date: Fri, 15 May 2026 22:26:31 +0000 (-0700) Subject: KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=dfc1c4687e9cb33a53536a0cb7fc4011c419dd85;p=thirdparty%2Fkernel%2Flinux.git KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation Record DR6 in emulate_db() and use kvm_queue_exception_p() to set DR6 instead of directly using kvm_set_dr6() in emulation, i.e. rely on the standard exception path to set DR6 via kvm_deliver_exception_payload(). This keeps the handling of DR6 during #DB injection consistent with other code paths. No functional change intended. Signed-off-by: Hou Wenlong [sean: fix e vs. p goof, add kvm_inject_emulated_db() right away] Reviewed-by: Yosry Ahmed Link: https://patch.msgid.link/20260515222638.1949982-4-seanjc@google.com Signed-off-by: Sean Christopherson --- diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index c8c6cc0406d6..510244555a74 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); } @@ -3847,15 +3848,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 0abff36d0994..bb2a2aee0e13 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; + }; u8 async_page_fault; unsigned long exit_qualification; }; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6934c0d01419..af6f3b5d2f3d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8977,11 +8977,18 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) } } +static void kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6) +{ + kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); +} + 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_inject_emulated_db(vcpu, ex->dr6); + 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); @@ -9026,6 +9033,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);