Re: [PATCH v3 03/10] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation
From: Yosry Ahmed
Date: Mon May 18 2026 - 14:30:56 EST
On Fri, May 15, 2026 at 03:26:31PM -0700, Sean Christopherson wrote:
> From: Hou Wenlong <houwenlong.hwl@xxxxxxxxxxxx>
>
> 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 <houwenlong.hwl@xxxxxxxxxxxx>
> [sean: fix e vs. p goof, add kvm_inject_emulated_db() right away]
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> ---
> arch/x86/kvm/emulate.c | 14 ++++----------
> arch/x86/kvm/kvm_emulate.h | 6 +++++-
> arch/x86/kvm/x86.c | 10 +++++++++-
> 3 files changed, 18 insertions(+), 12 deletions(-)
>
> 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);
For other readers of this patch, the manipulations done to DR6 here are
no longer needed because kvm_deliver_exception_payload() will apply them
when delivering the exception.
Not including that in the changelog and leaving it as an exercise to the
reader to chase it down is just mean :P
Anyway, it looks correct:
Reviewed-by: Yosry Ahmed <yosry@xxxxxxxxxx>