Skip to content

Commit 80c64c7

Browse files
committed
KVM: x86: Drop kvm_x86_ops.set_dr6() in favor of a new KVM_RUN flag
Instruct vendor code to load the guest's DR6 into hardware via a new KVM_RUN flag, and remove kvm_x86_ops.set_dr6(), whose sole purpose was to load vcpu->arch.dr6 into hardware when DR6 can be read/written directly by the guest. Note, TDX already WARNs on any run_flag being set, i.e. will yell if KVM thinks DR6 needs to be reloaded. TDX vCPUs force KVM_DEBUGREG_AUTO_SWITCH and never clear the flag, i.e. should never observe KVM_RUN_LOAD_GUEST_DR6. Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20250610232010.162191-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 2478b1b commit 80c64c7

File tree

6 files changed

+11
-22
lines changed

6 files changed

+11
-22
lines changed

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ KVM_X86_OP(set_idt)
4949
KVM_X86_OP(get_gdt)
5050
KVM_X86_OP(set_gdt)
5151
KVM_X86_OP(sync_dirty_debug_regs)
52-
KVM_X86_OP(set_dr6)
5352
KVM_X86_OP(set_dr7)
5453
KVM_X86_OP(cache_reg)
5554
KVM_X86_OP(get_rflags)

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)
16761676

16771677
enum kvm_x86_run_flags {
16781678
KVM_RUN_FORCE_IMMEDIATE_EXIT = BIT(0),
1679+
KVM_RUN_LOAD_GUEST_DR6 = BIT(1),
16791680
};
16801681

16811682
struct kvm_x86_ops {
@@ -1728,7 +1729,6 @@ struct kvm_x86_ops {
17281729
void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
17291730
void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
17301731
void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
1731-
void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
17321732
void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
17331733
void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
17341734
unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);

arch/x86/kvm/svm/svm.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,10 +4438,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
44384438
svm_hv_update_vp_id(svm->vmcb, vcpu);
44394439

44404440
/*
4441-
* Run with all-zero DR6 unless needed, so that we can get the exact cause
4442-
* of a #DB.
4441+
* Run with all-zero DR6 unless the guest can write DR6 freely, so that
4442+
* KVM can get the exact cause of a #DB. Note, loading guest DR6 from
4443+
* KVM's snapshot is only necessary when DR accesses won't exit.
44434444
*/
4444-
if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
4445+
if (unlikely(run_flags & KVM_RUN_LOAD_GUEST_DR6))
4446+
svm_set_dr6(vcpu, vcpu->arch.dr6);
4447+
else if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
44454448
svm_set_dr6(vcpu, DR6_ACTIVE_LOW);
44464449

44474450
clgi();
@@ -5252,7 +5255,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
52525255
.set_idt = svm_set_idt,
52535256
.get_gdt = svm_get_gdt,
52545257
.set_gdt = svm_set_gdt,
5255-
.set_dr6 = svm_set_dr6,
52565258
.set_dr7 = svm_set_dr7,
52575259
.sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
52585260
.cache_reg = svm_cache_reg,

arch/x86/kvm/vmx/main.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,6 @@ static void vt_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
489489
vmx_set_gdt(vcpu, dt);
490490
}
491491

492-
static void vt_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
493-
{
494-
if (is_td_vcpu(vcpu))
495-
return;
496-
497-
vmx_set_dr6(vcpu, val);
498-
}
499-
500492
static void vt_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
501493
{
502494
if (is_td_vcpu(vcpu))
@@ -943,7 +935,6 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
943935
.set_idt = vt_op(set_idt),
944936
.get_gdt = vt_op(get_gdt),
945937
.set_gdt = vt_op(set_gdt),
946-
.set_dr6 = vt_op(set_dr6),
947938
.set_dr7 = vt_op(set_dr7),
948939
.sync_dirty_debug_regs = vt_op(sync_dirty_debug_regs),
949940
.cache_reg = vt_op(cache_reg),

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,12 +5606,6 @@ void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
56065606
set_debugreg(DR6_RESERVED, 6);
56075607
}
56085608

5609-
void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5610-
{
5611-
lockdep_assert_irqs_disabled();
5612-
set_debugreg(vcpu->arch.dr6, 6);
5613-
}
5614-
56155609
void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
56165610
{
56175611
vmcs_writel(GUEST_DR7, val);
@@ -7370,6 +7364,9 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
73707364
vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
73717365
vcpu->arch.regs_dirty = 0;
73727366

7367+
if (run_flags & KVM_RUN_LOAD_GUEST_DR6)
7368+
set_debugreg(vcpu->arch.dr6, 6);
7369+
73737370
/*
73747371
* Refresh vmcs.HOST_CR3 if necessary. This must be done immediately
73757372
* prior to VM-Enter, as the kernel may load a new ASID (PCID) any time

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11046,7 +11046,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1104611046
set_debugreg(vcpu->arch.eff_db[3], 3);
1104711047
/* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */
1104811048
if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
11049-
kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6);
11049+
run_flags |= KVM_RUN_LOAD_GUEST_DR6;
1105011050
} else if (unlikely(hw_breakpoint_active())) {
1105111051
set_debugreg(0, 7);
1105211052
}

0 commit comments

Comments
 (0)