Skip to content

Commit 4d54f90

Browse files
sean-jcmehmetb0
authored andcommitted
KVM: x86: Load DR6 with guest value only before entering .vcpu_run() loop
BugLink: https://bugs.launchpad.net/bugs/2104873 commit c2fee09 upstream. Move the conditional loading of hardware DR6 with the guest's DR6 value out of the core .vcpu_run() loop to fix a bug where KVM can load hardware with a stale vcpu->arch.dr6. When the guest accesses a DR and host userspace isn't debugging the guest, KVM disables DR interception and loads the guest's values into hardware on VM-Enter and saves them on VM-Exit. This allows the guest to access DRs at will, e.g. so that a sequence of DR accesses to configure a breakpoint only generates one VM-Exit. For DR0-DR3, the logic/behavior is identical between VMX and SVM, and also identical between KVM_DEBUGREG_BP_ENABLED (userspace debugging the guest) and KVM_DEBUGREG_WONT_EXIT (guest using DRs), and so KVM handles loading DR0-DR3 in common code, _outside_ of the core kvm_x86_ops.vcpu_run() loop. But for DR6, the guest's value doesn't need to be loaded into hardware for KVM_DEBUGREG_BP_ENABLED, and SVM provides a dedicated VMCB field whereas VMX requires software to manually load the guest value, and so loading the guest's value into DR6 is handled by {svm,vmx}_vcpu_run(), i.e. is done _inside_ the core run loop. Unfortunately, saving the guest values on VM-Exit is initiated by common x86, again outside of the core run loop. If the guest modifies DR6 (in hardware, when DR interception is disabled), and then the next VM-Exit is a fastpath VM-Exit, KVM will reload hardware DR6 with vcpu->arch.dr6 and clobber the guest's actual value. The bug shows up primarily with nested VMX because KVM handles the VMX preemption timer in the fastpath, and the window between hardware DR6 being modified (in guest context) and DR6 being read by guest software is orders of magnitude larger in a nested setup. E.g. in non-nested, the VMX preemption timer would need to fire precisely between #DB injection and the #DB handler's read of DR6, whereas with a KVM-on-KVM setup, the window where hardware DR6 is "dirty" extends all the way from L1 writing DR6 to VMRESUME (in L1). L1's view: ========== <L1 disables DR interception> CPU 0/KVM-7289 [023] d.... 2925.640961: kvm_entry: vcpu 0 A: L1 Writes DR6 CPU 0/KVM-7289 [023] d.... 2925.640963: <hack>: Set DRs, DR6 = 0xffff0ff1 B: CPU 0/KVM-7289 [023] d.... 2925.640967: kvm_exit: vcpu 0 reason EXTERNAL_INTERRUPT intr_info 0x800000ec D: L1 reads DR6, arch.dr6 = 0 CPU 0/KVM-7289 [023] d.... 2925.640969: <hack>: Sync DRs, DR6 = 0xffff0ff0 CPU 0/KVM-7289 [023] d.... 2925.640976: kvm_entry: vcpu 0 L2 reads DR6, L1 disables DR interception CPU 0/KVM-7289 [023] d.... 2925.640980: kvm_exit: vcpu 0 reason DR_ACCESS info1 0x0000000000000216 CPU 0/KVM-7289 [023] d.... 2925.640983: kvm_entry: vcpu 0 CPU 0/KVM-7289 [023] d.... 2925.640983: <hack>: Set DRs, DR6 = 0xffff0ff0 L2 detects failure CPU 0/KVM-7289 [023] d.... 2925.640987: kvm_exit: vcpu 0 reason HLT L1 reads DR6 (confirms failure) CPU 0/KVM-7289 [023] d.... 2925.640990: <hack>: Sync DRs, DR6 = 0xffff0ff0 L0's view: ========== L2 reads DR6, arch.dr6 = 0 CPU 23/KVM-5046 [001] d.... 3410.005610: kvm_exit: vcpu 23 reason DR_ACCESS info1 0x0000000000000216 CPU 23/KVM-5046 [001] ..... 3410.005610: kvm_nested_vmexit: vcpu 23 reason DR_ACCESS info1 0x0000000000000216 L2 => L1 nested VM-Exit CPU 23/KVM-5046 [001] ..... 3410.005610: kvm_nested_vmexit_inject: reason: DR_ACCESS ext_inf1: 0x0000000000000216 CPU 23/KVM-5046 [001] d.... 3410.005610: kvm_entry: vcpu 23 CPU 23/KVM-5046 [001] d.... 3410.005611: kvm_exit: vcpu 23 reason VMREAD CPU 23/KVM-5046 [001] d.... 3410.005611: kvm_entry: vcpu 23 CPU 23/KVM-5046 [001] d.... 3410.005612: kvm_exit: vcpu 23 reason VMREAD CPU 23/KVM-5046 [001] d.... 3410.005612: kvm_entry: vcpu 23 L1 writes DR7, L0 disables DR interception CPU 23/KVM-5046 [001] d.... 3410.005612: kvm_exit: vcpu 23 reason DR_ACCESS info1 0x0000000000000007 CPU 23/KVM-5046 [001] d.... 3410.005613: kvm_entry: vcpu 23 L0 writes DR6 = 0 (arch.dr6) CPU 23/KVM-5046 [001] d.... 3410.005613: <hack>: Set DRs, DR6 = 0xffff0ff0 A: <L1 writes DR6 = 1, no interception, arch.dr6 is still '0'> B: CPU 23/KVM-5046 [001] d.... 3410.005614: kvm_exit: vcpu 23 reason PREEMPTION_TIMER CPU 23/KVM-5046 [001] d.... 3410.005614: kvm_entry: vcpu 23 C: L0 writes DR6 = 0 (arch.dr6) CPU 23/KVM-5046 [001] d.... 3410.005614: <hack>: Set DRs, DR6 = 0xffff0ff0 L1 => L2 nested VM-Enter CPU 23/KVM-5046 [001] d.... 3410.005616: kvm_exit: vcpu 23 reason VMRESUME L0 reads DR6, arch.dr6 = 0 Reported-by: John Stultz <jstultz@google.com> Closes: https://lkml.kernel.org/r/CANDhNCq5_F3HfFYABqFGCA1bPd_%2BxgNj-iDQhH4tDk%2Bwi8iZZg%40mail.gmail.com Fixes: 375e28f ("KVM: X86: Set host DR6 only on VMX and for KVM_DEBUGREG_WONT_EXIT") Fixes: d67668e ("KVM: x86, SVM: isolate vcpu->arch.dr6 from vmcb->save.dr6") Cc: stable@vger.kernel.org Cc: Jim Mattson <jmattson@google.com> Tested-by: John Stultz <jstultz@google.com> Link: https://lore.kernel.org/r/20250125011833.3644371-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Noah Wager <noah.wager@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent 72c0167 commit 4d54f90

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ KVM_X86_OP(set_idt)
4747
KVM_X86_OP(get_gdt)
4848
KVM_X86_OP(set_gdt)
4949
KVM_X86_OP(sync_dirty_debug_regs)
50+
KVM_X86_OP(set_dr6)
5051
KVM_X86_OP(set_dr7)
5152
KVM_X86_OP(cache_reg)
5253
KVM_X86_OP(get_rflags)

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ struct kvm_x86_ops {
16741674
void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
16751675
void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
16761676
void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
1677+
void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
16771678
void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
16781679
void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
16791680
unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);

arch/x86/kvm/svm/svm.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,11 +1993,11 @@ static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
19931993
svm->asid = sd->next_asid++;
19941994
}
19951995

1996-
static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
1996+
static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
19971997
{
1998-
struct vmcb *vmcb = svm->vmcb;
1998+
struct vmcb *vmcb = to_svm(vcpu)->vmcb;
19991999

2000-
if (svm->vcpu.arch.guest_state_protected)
2000+
if (vcpu->arch.guest_state_protected)
20012001
return;
20022002

20032003
if (unlikely(value != vmcb->save.dr6)) {
@@ -4231,10 +4231,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu,
42314231
* Run with all-zero DR6 unless needed, so that we can get the exact cause
42324232
* of a #DB.
42334233
*/
4234-
if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
4235-
svm_set_dr6(svm, vcpu->arch.dr6);
4236-
else
4237-
svm_set_dr6(svm, DR6_ACTIVE_LOW);
4234+
if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)))
4235+
svm_set_dr6(vcpu, DR6_ACTIVE_LOW);
42384236

42394237
clgi();
42404238
kvm_load_guest_xsave_state(vcpu);
@@ -5029,6 +5027,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
50295027
.set_idt = svm_set_idt,
50305028
.get_gdt = svm_get_gdt,
50315029
.set_gdt = svm_set_gdt,
5030+
.set_dr6 = svm_set_dr6,
50325031
.set_dr7 = svm_set_dr7,
50335032
.sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
50345033
.cache_reg = svm_cache_reg,

arch/x86/kvm/vmx/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
5858
.set_idt = vmx_set_idt,
5959
.get_gdt = vmx_get_gdt,
6060
.set_gdt = vmx_set_gdt,
61+
.set_dr6 = vmx_set_dr6,
6162
.set_dr7 = vmx_set_dr7,
6263
.sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
6364
.cache_reg = vmx_cache_reg,

arch/x86/kvm/vmx/vmx.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5636,6 +5636,12 @@ void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
56365636
set_debugreg(DR6_RESERVED, 6);
56375637
}
56385638

5639+
void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5640+
{
5641+
lockdep_assert_irqs_disabled();
5642+
set_debugreg(vcpu->arch.dr6, 6);
5643+
}
5644+
56395645
void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
56405646
{
56415647
vmcs_writel(GUEST_DR7, val);
@@ -7394,10 +7400,6 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
73947400
vmx->loaded_vmcs->host_state.cr4 = cr4;
73957401
}
73967402

7397-
/* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */
7398-
if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
7399-
set_debugreg(vcpu->arch.dr6, 6);
7400-
74017403
/* When single-stepping over STI and MOV SS, we must clear the
74027404
* corresponding interruptibility bits in the guest state. Otherwise
74037405
* vmentry fails as it then expects bit 14 (BS) in pending debug

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
7373
void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
7474
void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
7575
void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
76+
void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val);
7677
void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val);
7778
void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu);
7879
void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg);

arch/x86/kvm/x86.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11085,6 +11085,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1108511085
set_debugreg(vcpu->arch.eff_db[1], 1);
1108611086
set_debugreg(vcpu->arch.eff_db[2], 2);
1108711087
set_debugreg(vcpu->arch.eff_db[3], 3);
11088+
/* When KVM_DEBUGREG_WONT_EXIT, dr6 is accessible in guest. */
11089+
if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
11090+
kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6);
1108811091
} else if (unlikely(hw_breakpoint_active())) {
1108911092
set_debugreg(0, 7);
1109011093
}

0 commit comments

Comments
 (0)