]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
KVM: x86: Drop kvm_x86_ops.set_dr6() in favor of a new KVM_RUN flag
authorSean Christopherson <seanjc@google.com>
Fri, 15 Aug 2025 00:25:35 +0000 (17:25 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 28 Aug 2025 14:28:13 +0000 (16:28 +0200)
[ Upstream commit 80c64c7afea1da6a93ebe88d3d29d8a60377ef80 ]

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>
[sean: account for lack of vmx/main.c]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/x86/include/asm/kvm-x86-ops.h
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/svm/svm.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/x86.c

index 8fe6667d945f5d73c95630d8b68e779321b63af6..a0a4fc684e63b39806e8de57b751100aa5b5ce58 100644 (file)
@@ -48,7 +48,6 @@ KVM_X86_OP(set_idt)
 KVM_X86_OP(get_gdt)
 KVM_X86_OP(set_gdt)
 KVM_X86_OP(sync_dirty_debug_regs)
-KVM_X86_OP(set_dr6)
 KVM_X86_OP(set_dr7)
 KVM_X86_OP(cache_reg)
 KVM_X86_OP(get_rflags)
index aa6d04cd9ee6850274f91b254ab39e77be7883db..7373b22c02a7c354d2ad5aef226e3b02085e334c 100644 (file)
@@ -1552,6 +1552,7 @@ static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)
 
 enum kvm_x86_run_flags {
        KVM_RUN_FORCE_IMMEDIATE_EXIT    = BIT(0),
+       KVM_RUN_LOAD_GUEST_DR6          = BIT(1),
 };
 
 struct kvm_x86_ops {
@@ -1600,7 +1601,6 @@ struct kvm_x86_ops {
        void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
        void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
        void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
-       void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
        void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
        void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
        unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
index 61e5e261cde252d9824e1d0047297ee6db1c6a77..abff6d45ae3334341219804e2edea7c51023df6e 100644 (file)
@@ -4241,10 +4241,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
        svm_hv_update_vp_id(svm->vmcb, vcpu);
 
        /*
-        * Run with all-zero DR6 unless needed, so that we can get the exact cause
-        * of a #DB.
+        * Run with all-zero DR6 unless the guest can write DR6 freely, so that
+        * KVM can get the exact cause of a #DB.  Note, loading guest DR6 from
+        * KVM's snapshot is only necessary when DR accesses won't exit.
         */
-       if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
+       if (unlikely(run_flags & KVM_RUN_LOAD_GUEST_DR6))
+               svm_set_dr6(vcpu, vcpu->arch.dr6);
+       else if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
                svm_set_dr6(vcpu, DR6_ACTIVE_LOW);
 
        clgi();
@@ -5021,7 +5024,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
        .set_idt = svm_set_idt,
        .get_gdt = svm_get_gdt,
        .set_gdt = svm_set_gdt,
-       .set_dr6 = svm_set_dr6,
        .set_dr7 = svm_set_dr7,
        .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
        .cache_reg = svm_cache_reg,
index 065aac2f4bcebf8669e77260ef322805f6b7a4f0..08ca218ee8587b87272ff167dbd82319fc2abb88 100644 (file)
@@ -5616,12 +5616,6 @@ static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
        set_debugreg(DR6_RESERVED, 6);
 }
 
-static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
-{
-       lockdep_assert_irqs_disabled();
-       set_debugreg(vcpu->arch.dr6, 6);
-}
-
 static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
 {
        vmcs_writel(GUEST_DR7, val);
@@ -7392,6 +7386,9 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
                vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
        vcpu->arch.regs_dirty = 0;
 
+       if (run_flags & KVM_RUN_LOAD_GUEST_DR6)
+               set_debugreg(vcpu->arch.dr6, 6);
+
        /*
         * Refresh vmcs.HOST_CR3 if necessary.  This must be done immediately
         * prior to VM-Enter, as the kernel may load a new ASID (PCID) any time
@@ -8337,7 +8334,6 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
        .set_idt = vmx_set_idt,
        .get_gdt = vmx_get_gdt,
        .set_gdt = vmx_set_gdt,
-       .set_dr6 = vmx_set_dr6,
        .set_dr7 = vmx_set_dr7,
        .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
        .cache_reg = vmx_cache_reg,
index ecc1513973411bdede4ebb0c9b5e6c542c002f72..fbb2e70e30315176235eebeaad51ff3e3b56e813 100644 (file)
@@ -10772,7 +10772,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                set_debugreg(vcpu->arch.eff_db[3], 3);
                /* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */
                if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
-                       static_call(kvm_x86_set_dr6)(vcpu, vcpu->arch.dr6);
+                       run_flags |= KVM_RUN_LOAD_GUEST_DR6;
        } else if (unlikely(hw_breakpoint_active())) {
                set_debugreg(0, 7);
        }