Skip to contentSkip to conversation

KVM: x86: Fix single-step debugging

PATCH Applied Alexander Popov 4 participants 6 messages Jan 21, 2019 – Feb 5, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: x86: Fix single-step debugging

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Let's fix it. Please consider that for -stable.

---
 arch/x86/kvm/x86.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/x86.c +1 -2
index f049ecf..9686068 100644
@@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 		toggle_interruptibility(vcpu, ctxt->interruptibility);
 		vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
 		kvm_rip_write(vcpu, ctxt->eip);
-		if (r == EMULATE_DONE &&
-		    (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
+		if (r == EMULATE_DONE && ctxt->tf)
 			kvm_vcpu_do_singlestep(vcpu, &r);
 		if (!ctxt->have_exception ||
 		    exception_type(ctxt->exception.vector) == EXCPT_TRAP)
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · Alexander Popov

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Quoted 6 lines

Let's fix it. Please consider that for -stable.

---
 arch/x86/kvm/x86.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

maintainer
#2 lore raw
Quoted 18 lines · Greg KH
Quoted 10 lines · Alexander Popov

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Quoted 6 lines

Let's fix it. Please consider that for -stable.

---
 arch/x86/kvm/x86.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 9 lines

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

Yup, will take care of doing this properly. :)

Paolo

#3 lore raw
In reply to Alexander Popov
Quoted 11 lines · Alexander Popov

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Let's fix it. Please consider that for -stable.

Hello everyone!

Just a friendly ping about this fix.
Looking forward to the feedback.

Best regards,
Alexander

maintainer Applied
#4 lore raw
In reply to Alexander Popov
Quoted 23 lines · Alexander Popov

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Let's fix it. Please consider that for -stable.

---
 arch/x86/kvm/x86.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index f049ecf..9686068 100644

arch/x86/kvm/x86.c int x86_emulate_instruction(struct kvm_vcpu *vcpu, · line 6413

Quoted 9 lines
arch/x86/kvm/x86.c +1 -2
@@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 		toggle_interruptibility(vcpu, ctxt->interruptibility);
 		vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
 		kvm_rip_write(vcpu, ctxt->eip);
-		if (r == EMULATE_DONE &&
-		    (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
+		if (r == EMULATE_DONE && ctxt->tf)
 			kvm_vcpu_do_singlestep(vcpu, &r);
 		if (!ctxt->have_exception ||
 		    exception_type(ctxt->exception.vector) == EXCPT_TRAP)

Queued, with Cc to stable and the right "Fixes" trailer.

Paolo

#5 lore raw
Quoted 37 lines · Paolo Bonzini
Quoted 23 lines · Alexander Popov

The single-step debugging of KVM guests on x86 is broken: if we run gdb 'stepi' command at the breakpoint when the guest interrupts are enabled, RIP always jumps to native_apic_mem_write(). Then other nasty effects follow.

Long investigation showed that on Jun 7, 2017 the
commit c8401dda2f0a00cd25c0 ("KVM: x86: fix singlestepping over syscall") introduced the kvm_run.debug corruption: kvm_vcpu_do_singlestep() can be called without X86_EFLAGS_TF set.

Let's fix it. Please consider that for -stable.

---
 arch/x86/kvm/x86.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index f049ecf..9686068 100644

arch/x86/kvm/x86.c int x86_emulate_instruction(struct kvm_vcpu *vcpu, · line 6413

Quoted 9 lines
arch/x86/kvm/x86.c +1 -2
@@ -6407,8 +6407,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
              toggle_interruptibility(vcpu, ctxt->interruptibility);
              vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
              kvm_rip_write(vcpu, ctxt->eip);
-             if (r == EMULATE_DONE &&
-                 (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
+             if (r == EMULATE_DONE && ctxt->tf)
                      kvm_vcpu_do_singlestep(vcpu, &r);
              if (!ctxt->have_exception ||
                  exception_type(ctxt->exception.vector) == EXCPT_TRAP)

Queued, with Cc to stable and the right "Fixes" trailer.

Quoted 1 line

Paolo

This patch seems to imply that userspace is expected to set the guest's RFLAGS.TF when requesting KVM_GUEST_SINGLESTEP, but the documentation for KVM_GUEST_SINGLESTEP is not clear about this. Can someone clarify how KVM_GUEST_SINGLESTEP is supposed to work?