Re: [PATCH 2/7] KVM: x86: Check guest debug in DR access instruction emulation

From: Sean Christopherson

Date: Fri Dec 05 2025 - 12:51:48 EST


On Wed, Sep 10, 2025, Hou Wenlong wrote:
> @@ -8606,19 +8628,38 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
> }
> }
>
> -static void inject_emulated_exception(struct kvm_vcpu *vcpu)
> +static int kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6)
> +{
> + struct kvm_run *kvm_run = vcpu->run;
> +
> + if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
> + kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
> + kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
> + kvm_run->debug.arch.exception = DB_VECTOR;
> + kvm_run->exit_reason = KVM_EXIT_DEBUG;
> + return 0;
> + }
> +
> + kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
> + return 1;
> +}
> +
> +static int inject_emulated_exception(struct kvm_vcpu *vcpu)
> {
> + int r = 1;
> struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
>
> if (ctxt->exception.vector == PF_VECTOR)
> kvm_inject_emulated_page_fault(vcpu, &ctxt->exception);
> else if (ctxt->exception.vector == DB_VECTOR)
> - kvm_queue_exception_p(vcpu, DB_VECTOR, ctxt->exception.dr6);
> + r = kvm_inject_emulated_db(vcpu, ctxt->exception.dr6);
> else if (ctxt->exception.error_code_valid)
> kvm_queue_exception_e(vcpu, ctxt->exception.vector,
> ctxt->exception.error_code);
> else
> kvm_queue_exception(vcpu, ctxt->exception.vector);
> +
> + return r;

Hmm, I think I'd rather make the DB_VECTOR case an early termination, and keep
the rest largely as-is. And while you're modifying this code, maybe add a patch
to capture "struct x86_exception" locally instead of the context? E.g. to end
up with:

static int inject_emulated_exception(struct kvm_vcpu *vcpu)
{
struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception;

if (ex->vector == DB_VECTOR)
return kvm_inject_emulated_db(vcpu, ex->dr6);

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);
else
kvm_queue_exception(vcpu, ex->vector);
return 1;
}