KVM: guest debug support for SVM
Hi,
this patch set implements guest debugging support for the kvm-amd module. It moves the code from VMX which is common for x86 to the generic code and keeps the machine dependent parts in the architecture files.
Since guest debugging in the qemu part seems to be broken atm these patches are not really tested. Therefore I post them only for commenting, not for inclusion. Also some people asked for these patches, so here they are.
Joerg
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 1/2 VMX: move common guest debug stuff to generic code
This patch moves the parts of the VMX guest debug code which can be reused for SVM to the generic x86 code.
--- arch/x86/kvm/svm.c | 3 ++- arch/x86/kvm/vmx.c | 26 +++++--------------------- arch/x86/kvm/x86.c | 28 ++++++++++++++++++++++++++-- include/asm-x86/kvm_host.h | 3 ++- 4 files changed, 35 insertions(+), 25 deletions(-)
arch/x86/kvm/svm.c +2 -1
@@ -916,7 +916,8 @@ static void svm_set_segment(struct kvm_vcpu *vcpu,*/ -static int svm_guest_debug (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg) +static int svm_guest_debug (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg, +unsigned long dr7) {return -EOPNOTSUPP ; }
arch/x86/kvm/vmx.c +5 -21
@@ -858,28 +858,17 @@ static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)vmcs_writel (GUEST_RIP , vcpu->arch.rip); } -static int set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg) +static int set_guest_debug (struct kvm_vcpu *vcpu, +struct kvm_debug_guest *dbg, +unsigned long dr7) { -unsigned long dr7 =0x400 ;int old_singlestep; old_singlestep = vcpu->guest_debug.singlestep; - vcpu->guest_debug.enabled = dbg->enabled; -if (vcpu->guest_debug.enabled) { -int i; - - dr7 |=0x200 ;/* exact */ -for (i =0 ; i <4 ; ++i) { -if (!dbg->breakpoints[i].enabled) -continue ; - vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; - dr7 |=2 << (i*2 );/* global enable */ - dr7 |=0 << (i*4 +16 );/* execution breakpoint */ - } - +if (vcpu->guest_debug.enabled) vcpu->guest_debug.singlestep = dbg->singlestep; - }else +else vcpu->guest_debug.singlestep =0 ;if (old_singlestep && !vcpu->guest_debug.singlestep) {
@@ -1868,11 +1857,6 @@ static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) {struct kvm_guest_debug *dbg = &vcpu->guest_debug; -set_debugreg (dbg->bp[0 ],0 ); -set_debugreg (dbg->bp[1 ],1 ); -set_debugreg (dbg->bp[2 ],2 ); -set_debugreg (dbg->bp[3 ],3 ); -if (dbg->singlestep) {unsigned long flags;
arch/x86/kvm/x86.c +26 -2
@@ -2530,8 +2530,16 @@ static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)vapic_enter (vcpu); preempted: -if (vcpu->guest_debug.enabled) +if (vcpu->guest_debug.enabled) { +struct kvm_guest_debug *dbg = &vcpu->guest_debug; + +set_debugreg (dbg->bp[0 ],0 ); +set_debugreg (dbg->bp[1 ],1 ); +set_debugreg (dbg->bp[2 ],2 ); +set_debugreg (dbg->bp[3 ],3 ); + kvm_x86_ops->guest_debug_pre (vcpu); + } again: r =kvm_mmu_reload (vcpu);
@@ -2928,11 +2936,27 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_debug_guest (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg) { +unsigned long dr7 =0x400 ;int r;vcpu_load (vcpu); - r = kvm_x86_ops->set_guest_debug (vcpu, dbg); + vcpu->guest_debug.enabled = dbg->enabled; + +if (dbg->enabled) { +int i; + + dr7 |=0x200 ;/* exact */ +for (i =0 ; i <4 ; ++i) { +if (!dbg->breakpoints[i].enabled) +continue ; + vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; + dr7 |=2 << (i*2 );/* global enable */ + dr7 |=0 << (i*4 +16 );/* execution breakpoint */ + } + } + + r = kvm_x86_ops->set_guest_debug (vcpu, dbg, dr7);vcpu_put (vcpu);
include/asm-x86/kvm_host.h +2 -1
@@ -348,7 +348,8 @@ struct kvm_x86_ops {void (*vcpu_decache)(struct kvm_vcpu *vcpu);int (*set_guest_debug)(struct kvm_vcpu *vcpu, -struct kvm_debug_guest *dbg); +struct kvm_debug_guest *dbg, +unsigned long dr7);void (*guest_debug_pre)(struct kvm_vcpu *vcpu);int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 *pdata);int (*set_msr)(struct kvm_vcpu *vcpu,u32 msr_index,u64 data);
Signature · 9 lines
1.5.3.7 ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 2/2 KVM: SVM: Guest debug support for SVM
In reply to Joerg RoedelThis patch implements guest debug support for KVM running on AMD hardware.
--- arch/x86/kvm/svm.c | 37 ++++++++++++++++++++++++++++++++++--- 1 files changed, 34 insertions(+), 3 deletions(-)
arch/x86/kvm/svm.c +34 -3
@@ -919,7 +919,30 @@ static void svm_set_segment(struct kvm_vcpu *vcpu,static int svm_guest_debug (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg,unsigned long dr7) { -return -EOPNOTSUPP ; +struct vcpu_svm *svm =to_svm (vcpu); + +if (vcpu->guest_debug.enabled) { + vcpu->guest_debug.singlestep = dbg->singlestep; + svm->vmcb->control.intercept_exceptions |= (1 <<DB_VECTOR ); + }else { + vcpu->guest_debug.singlestep =0 ; + svm->vmcb->control.intercept_exceptions &= ~(1 <<DB_VECTOR ); + } + +if (!vcpu->guest_debug.singlestep) + svm->vmcb->save.rflags &= ~(X86_EFLAGS_TF |X86_EFLAGS_RF ); + + svm->vmcb->save.dr7 = dr7; + +return 0 ; +} + +static void svm_guest_debug_pre (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +if (vcpu->guest_debug.singlestep) + svm->vmcb->save.rflags |=X86_EFLAGS_TF |X86_EFLAGS_RF ; }static int svm_get_irq (struct kvm_vcpu *vcpu)
@@ -1019,6 +1042,12 @@ static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)return kvm_mmu_page_fault (&svm->vcpu, fault_address, error_code); } +static int db_interception (struct vcpu_svm *svm,struct kvm_run *kvm_run) +{ + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return 0 ; +} +static int ud_interception (struct vcpu_svm *svm,struct kvm_run *kvm_run) {int er;
@@ -1350,6 +1379,7 @@ static int (*svm_exit_handlers[])(struct vcpu_svm *svm, [SVM_EXIT_WRITE_DR3] = emulate_on_interception, [SVM_EXIT_WRITE_DR5] = emulate_on_interception, [SVM_EXIT_WRITE_DR7] = emulate_on_interception, + [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
@@ -1612,7 +1642,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)if (npt_enabled) svm->vmcb->save.cr3 = vcpu->arch.cr3; -if (svm->vmcb->save.dr7 &0xff ) { +if (!vcpu->guest_debug.enabled && svm->vmcb->save.dr7 &0xff ) {write_dr7 (0 );save_db_regs (svm->host_db_regs);load_db_regs (svm->db_regs);
@@ -1727,7 +1757,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)#endif ); -if ((svm->vmcb->save.dr7 &0xff )) +if (!vcpu->guest_debug.enabled && (svm->vmcb->save.dr7 &0xff ))load_db_regs (svm->host_db_regs); vcpu->arch.cr2 = svm->vmcb->save.cr2;
@@ -1822,6 +1852,7 @@ static struct kvm_x86_ops svm_x86_ops = { .vcpu_decache = svm_vcpu_decache, .set_guest_debug = svm_guest_debug, + .guest_debug_pre = svm_guest_debug_pre, .get_msr = svm_get_msr, .set_msr = svm_set_msr, .get_segment_base = svm_get_segment_base,
Signature · 9 lines
1.5.3.7 ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/