KVM: x86: minimal debugging support during emulation
Paolo Bonzini
maintainercover KVM: x86: minimal debugging support during emulation
The switch to emulate_invalid_guest_state brought one slightly unwelcome change; the debugging interface does not work in the initial boot phase because KVM_SET_GUEST_DEBUG is basically ignored.
These two patches bring some initial support for debugging, namely for hardware breakpoints and single-stepping. Software breakpoints and hardware watchpoints are still ignored.
Paolo
Paolo Bonzini (2):
KVM: x86: handle hardware breakpoints during emulation
KVM: x86: handle singlestep during emulation
arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/emulate.c | 1 + arch/x86/kvm/x86.c | 106 ++++++++++++++++++++++++++++++++++++- 4 files changed, 109 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.1.4
Paolo Bonzini
maintainerPATCH 2/2 KVM: x86: handle singlestep during emulation
This lets debugging work better during emulation of invalid guest state.
This time the check is done after emulation, but before writeback of the flags; we need to check the flags *before* execution of the instruction, we cannot check singlestep_rip because the CS base may have already been modified.
--- arch/x86/kvm/x86.c | 41 +++++++++++++++++++++++++++++++++++++- 1 files changed, 40 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +40 -1
@@ -4887,6 +4887,43 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,return dr6; } +static int kvm_vcpu_check_singlestep (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; + +/* +* Use the "raw" value to see if TF was passed to the processor. +* Note that the new value of the flags has not been saved yet. +* +* This is correct even for TF set by the guest, because "the +* processor will not generate this exception after the instruction +* that sets the TF flag". +*/ +unsigned long rflags = kvm_x86_ops->get_rflags (vcpu); + +if (unlikely (rflags &X86_EFLAGS_TF )) { +if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) { + kvm_run->debug.arch.dr6 =DR6_BS |DR6_FIXED_1 ; + kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip; + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + }else { + vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF ; +/* +* "Certain debug exceptions may clear bit 0-3. The +* remaining contents of the DR6 register are never +* cleared by the processor". +*/ + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |=DR6_BS ; +kvm_queue_exception (vcpu,DB_VECTOR ); + } + } + +return EMULATE_DONE ; +} +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run;
@@ -5031,10 +5068,12 @@ restart:if (writeback) {toggle_interruptibility (vcpu, ctxt->interruptibility); -kvm_set_rflags (vcpu, ctxt->eflags);kvm_make_request (KVM_REQ_EVENT , vcpu); vcpu->arch.emulate_regs_need_sync_to_vcpu = false;kvm_rip_write (vcpu, ctxt->eip); +if (r ==EMULATE_DONE ) + r =kvm_vcpu_check_singlestep (vcpu); +kvm_set_rflags (vcpu, ctxt->eflags); }else vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
Signature · 1 line
1.8.1.4
Paolo Bonzini
maintainerPATCH 1/2 KVM: x86: handle hardware breakpoints during emulation
In reply to Paolo BonziniThis lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses EMULATE_DO_MMIO to exit with KVM_EXIT_DEBUG.
--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +3 -2
@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */ EMULATE_FAIL ,/* can't emulate this instruction */ +EMULATE_PROCEED ,/* proceed with rest of emulation */ };#define EMULTYPE_NO_DECODE (1 << 0)
arch/x86/kvm/x86.c +65 -0
77 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Quoted 17 lines · Paolo Bonzini
This lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses
EMULATE_DO_MMIOto exit withKVM_EXIT_DEBUG.Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 792
Quoted 7 lines
arch/x86/include/asm/kvm_host.h +2
-2@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */
If it no longer means MMIO (or PIO) lest rename it to something more meaningful. EMULATE_EXIT? EMULATE_USER_EXIT?
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 794
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1
-0EMULATE_FAIL ,/* can't emulate this instruction */ + EMULATE_PROCEED, /* proceed with rest of emulation */
I think we can do without this. Have to function: check_bp(), handle_bp(). Do:
if (check_bp())
return handle_bp();
Quoted 89 lines
arch/x86/include/asm/kvm_host.h +0
-0};#define EMULTYPE_NO_DECODE (1 << 0)arch/x86/kvm/x86.c +65
-0@@ -4872,6 +4872,60 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,static int complete_emulated_mmio (struct kvm_vcpu *vcpu);static int complete_emulated_pio (struct kvm_vcpu *vcpu); +static int kvm_vcpu_check_hw_bp (unsigned long addr,u32 type,u32 dr7, +unsigned long *db) +{ +u32 dr6 =0 ; +int i; +u32 enable, rwlen; + + enable = dr7; + rwlen = dr7 >>16 ; +for (i =0 ; i <4 ; i++, enable >>=2 , rwlen >>=4 ) +if ((enable &3 ) && (rwlen &15 ) == type && db[i] == addr) + dr6 |= (1 << i); +return dr6; +} + +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; +unsigned long eip = vcpu->arch.emulate_ctxt.eip; +u32 dr6 =0 ; + +if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && + (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.guest_debug_dr7, + vcpu->arch.eff_db); + +if (dr6 !=0 ) { + kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; + kvm_run->debug.arch.pc =kvm_rip_read (vcpu) + +get_segment_base (vcpu,VCPU_SREG_CS ); + + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + } + } + +if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.dr7, + vcpu->arch.db); + +if (dr6 !=0 ) { + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |= dr6; +kvm_queue_exception (vcpu,DB_VECTOR ); +return EMULATE_DONE ; + } + } + +return EMULATE_PROCEED ; +} + int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, int emulation_type,@@ -4892,6 +4946,17 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); + +/* +* We will reenter on the same instruction since +* we do not set complete_userspace_io. This does not +* handle watchpoints yet, those would be handled in +* the emulate_ops. +*/ + r =kvm_vcpu_check_breakpoint (vcpu); +if (r !=EMULATE_PROCEED ) +return r; + ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false;Signature · 1 line
1.8.1.4
--
Gleb.
Paolo Bonzini
maintainerRe: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Il 04/06/2013 13:28, Gleb Natapov ha scritto:
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines
This lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses
EMULATE_DO_MMIOto exit withKVM_EXIT_DEBUG.Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;
Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 792
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +2
-2enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */ If it no longer means MMIO (or PIO) lest rename it to something more meaningful.
EMULATE_EXIT?EMULATE_USER_EXIT?
I'll go with EMULATE_USER_EXIT.
Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 794
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1
-0EMULATE_FAIL ,/* can't emulate this instruction */ + EMULATE_PROCEED, /* proceed with rest of emulation */I think we can do without this. Have to function:
check_bp(),handle_bp(). Do:if (
check_bp())
returnhandle_bp();
I tried this, but it doesn't work because you need to pass the computed dr6 from check_bp to handle_bp. It becomes really ugly.
If you do not want EMULATE_PROCEED, I can just use -1 instead in kvm_vcpu_check_breakpoint, and return if r < 0.
Paolo
Quoted 93 lines
Quoted 89 lines
arch/x86/include/asm/kvm_host.h +0
-0};#define EMULTYPE_NO_DECODE (1 << 0)arch/x86/kvm/x86.c +65
-0@@ -4872,6 +4872,60 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,static int complete_emulated_mmio (struct kvm_vcpu *vcpu);static int complete_emulated_pio (struct kvm_vcpu *vcpu); +static int kvm_vcpu_check_hw_bp (unsigned long addr,u32 type,u32 dr7, +unsigned long *db) +{ +u32 dr6 =0 ; +int i; +u32 enable, rwlen; + + enable = dr7; + rwlen = dr7 >>16 ; +for (i =0 ; i <4 ; i++, enable >>=2 , rwlen >>=4 ) +if ((enable &3 ) && (rwlen &15 ) == type && db[i] == addr) + dr6 |= (1 << i); +return dr6; +} + +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; +unsigned long eip = vcpu->arch.emulate_ctxt.eip; +u32 dr6 =0 ; + +if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && + (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.guest_debug_dr7, + vcpu->arch.eff_db); + +if (dr6 !=0 ) { + kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; + kvm_run->debug.arch.pc =kvm_rip_read (vcpu) + +get_segment_base (vcpu,VCPU_SREG_CS ); + + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + } + } + +if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.dr7, + vcpu->arch.db); + +if (dr6 !=0 ) { + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |= dr6; +kvm_queue_exception (vcpu,DB_VECTOR ); +return EMULATE_DONE ; + } + } + +return EMULATE_PROCEED ; +} + int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, int emulation_type,@@ -4892,6 +4946,17 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); + +/* +* We will reenter on the same instruction since +* we do not set complete_userspace_io. This does not +* handle watchpoints yet, those would be handled in +* the emulate_ops. +*/ + r =kvm_vcpu_check_breakpoint (vcpu); +if (r !=EMULATE_PROCEED ) +return r; + ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false;Signature · 1 line
1.8.1.4--
Gleb.
Re: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Quoted 36 lines · Paolo Bonzini
Il 04/06/2013 13:28, Gleb Natapov ha scritto:
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines
This lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses
EMULATE_DO_MMIOto exit withKVM_EXIT_DEBUG.Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 792
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +2
-2enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */ If it no longer means MMIO (or PIO) lest rename it to something more meaningful.
EMULATE_EXIT?EMULATE_USER_EXIT?I'll go with
EMULATE_USER_EXIT.Quoted 5 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 794
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1
-0EMULATE_FAIL ,/* can't emulate this instruction */ + EMULATE_PROCEED, /* proceed with rest of emulation */I think we can do without this. Have to function:
check_bp(),handle_bp(). Do:
Quoted 6 lines
Quoted 2 lines
if (
check_bp())
returnhandle_bp();I tried this, but it doesn't work because you need to pass the computed dr6 from
check_bptohandle_bp. It becomes really ugly.
Can't check_bp() return dr6?
if ((dr6 = check_bp())
return handle_bp(dr6);
Quoted 3 lines
If you do not want
EMULATE_PROCEED, I can just use -1 instead inkvm_vcpu_check_breakpoint, and return if r < 0.
But you need to know what to return EMULATE_DONE or EMULATE_USER_EXIT.
Quoted 95 lines
Paolo
Quoted 93 lines
Quoted 89 lines
arch/x86/include/asm/kvm_host.h +0
-0};#define EMULTYPE_NO_DECODE (1 << 0)arch/x86/kvm/x86.c +65
-0@@ -4872,6 +4872,60 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,static int complete_emulated_mmio (struct kvm_vcpu *vcpu);static int complete_emulated_pio (struct kvm_vcpu *vcpu); +static int kvm_vcpu_check_hw_bp (unsigned long addr,u32 type,u32 dr7, +unsigned long *db) +{ +u32 dr6 =0 ; +int i; +u32 enable, rwlen; + + enable = dr7; + rwlen = dr7 >>16 ; +for (i =0 ; i <4 ; i++, enable >>=2 , rwlen >>=4 ) +if ((enable &3 ) && (rwlen &15 ) == type && db[i] == addr) + dr6 |= (1 << i); +return dr6; +} + +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; +unsigned long eip = vcpu->arch.emulate_ctxt.eip; +u32 dr6 =0 ; + +if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && + (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.guest_debug_dr7, + vcpu->arch.eff_db); + +if (dr6 !=0 ) { + kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; + kvm_run->debug.arch.pc =kvm_rip_read (vcpu) + +get_segment_base (vcpu,VCPU_SREG_CS ); + + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + } + } + +if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.dr7, + vcpu->arch.db); + +if (dr6 !=0 ) { + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |= dr6; +kvm_queue_exception (vcpu,DB_VECTOR ); +return EMULATE_DONE ; + } + } + +return EMULATE_PROCEED ; +} + int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, int emulation_type,@@ -4892,6 +4946,17 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); + +/* +* We will reenter on the same instruction since +* we do not set complete_userspace_io. This does not +* handle watchpoints yet, those would be handled in +* the emulate_ops. +*/ + r =kvm_vcpu_check_breakpoint (vcpu); +if (r !=EMULATE_PROCEED ) +return r; + ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false;Signature · 1 line
1.8.1.4--
Gleb.
--
Gleb.
Paolo Bonzini
maintainerRe: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Il 04/06/2013 13:47, Gleb Natapov ha scritto:
Quoted 40 lines · Paolo Bonzini
Quoted 39 lines
Il 04/06/2013 13:28, Gleb Natapov ha scritto:
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines
This lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses
EMULATE_DO_MMIOto exit withKVM_EXIT_DEBUG.Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 792
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +2
-2enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */ If it no longer means MMIO (or PIO) lest rename it to something more meaningful.
EMULATE_EXIT?EMULATE_USER_EXIT?I'll go with
EMULATE_USER_EXIT.Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 794
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1
-0EMULATE_FAIL ,/* can't emulate this instruction */ + EMULATE_PROCEED, /* proceed with rest of emulation */I think we can do without this. Have to function:
check_bp(),handle_bp(). Do:if (
check_bp())
returnhandle_bp();
Quoted 7 lines
Quoted 3 lines
I tried this, but it doesn't work because you need to pass the computed dr6 from
check_bptohandle_bp. It becomes really ugly.Can't
check_bp()return dr6?if ((dr6 =
check_bp())return handle_bp(dr6);
It also needs to know if debugging the guest vs. in the guest. Thus there is duplicate code between check and handle.
Quoted 4 lines
Quoted 3 lines
If you do not want
EMULATE_PROCEED, I can just use -1 instead inkvm_vcpu_check_breakpoint, and return if r < 0.But you need to know what to return
EMULATE_DONEorEMULATE_USER_EXIT.
Sorry, _not_ return if r < 0.
Paolo
Quoted 99 lines
Quoted 95 lines
Paolo
Quoted 93 lines
Quoted 89 lines
arch/x86/include/asm/kvm_host.h +0
-0};#define EMULTYPE_NO_DECODE (1 << 0)arch/x86/kvm/x86.c +65
-0@@ -4872,6 +4872,60 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,static int complete_emulated_mmio (struct kvm_vcpu *vcpu);static int complete_emulated_pio (struct kvm_vcpu *vcpu); +static int kvm_vcpu_check_hw_bp (unsigned long addr,u32 type,u32 dr7, +unsigned long *db) +{ +u32 dr6 =0 ; +int i; +u32 enable, rwlen; + + enable = dr7; + rwlen = dr7 >>16 ; +for (i =0 ; i <4 ; i++, enable >>=2 , rwlen >>=4 ) +if ((enable &3 ) && (rwlen &15 ) == type && db[i] == addr) + dr6 |= (1 << i); +return dr6; +} + +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; +unsigned long eip = vcpu->arch.emulate_ctxt.eip; +u32 dr6 =0 ; + +if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && + (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.guest_debug_dr7, + vcpu->arch.eff_db); + +if (dr6 !=0 ) { + kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; + kvm_run->debug.arch.pc =kvm_rip_read (vcpu) + +get_segment_base (vcpu,VCPU_SREG_CS ); + + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + } + } + +if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.dr7, + vcpu->arch.db); + +if (dr6 !=0 ) { + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |= dr6; +kvm_queue_exception (vcpu,DB_VECTOR ); +return EMULATE_DONE ; + } + } + +return EMULATE_PROCEED ; +} + int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, int emulation_type,@@ -4892,6 +4946,17 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); + +/* +* We will reenter on the same instruction since +* we do not set complete_userspace_io. This does not +* handle watchpoints yet, those would be handled in +* the emulate_ops. +*/ + r =kvm_vcpu_check_breakpoint (vcpu); +if (r !=EMULATE_PROCEED ) +return r; + ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false;Signature · 1 line
1.8.1.4--
Gleb.--
Gleb.
Re: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Quoted 44 lines · Paolo Bonzini
Il 04/06/2013 13:47, Gleb Natapov ha scritto:
Quoted 43 lines · Paolo Bonzini
Quoted 42 lines
Il 04/06/2013 13:28, Gleb Natapov ha scritto:
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines
This lets debugging work better during emulation of invalid guest state.
The check is done before emulating the instruction, and (in the case of guest debugging) reuses
EMULATE_DO_MMIOto exit withKVM_EXIT_DEBUG.Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0@@ -788,9 +788,10 @@ extern u32 kvm_min_guest_tsc_khz;extern u32 kvm_max_guest_tsc_khz;Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 792
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +2
-2enum emulation_result { -EMULATE_DONE ,/* no further processing */ -EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */ +EMULATE_DONE ,/* no further processing */ +EMULATE_DO_MMIO ,/* kvm_run ready for userspace exit */ If it no longer means MMIO (or PIO) lest rename it to something more meaningful.
EMULATE_EXIT?EMULATE_USER_EXIT?I'll go with
EMULATE_USER_EXIT.Quoted 7 lines
arch/x86/include/asm/kvm_host.h extern u32 kvm_min_guest_tsc_khz; · line 794
Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1
-0EMULATE_FAIL ,/* can't emulate this instruction */ + EMULATE_PROCEED, /* proceed with rest of emulation */I think we can do without this. Have to function:
check_bp(),handle_bp(). Do:if (
check_bp())
returnhandle_bp();I tried this, but it doesn't work because you need to pass the computed dr6 from
check_bptohandle_bp. It becomes really ugly.
Quoted 8 lines
Quoted 4 lines
Can't
check_bp()return dr6?if ((dr6 =
check_bp())return handle_bp(dr6);It also needs to know if debugging the guest vs. in the guest. Thus there is duplicate code between check and handle.
Yeah. What about:
if ((dr6 = guest_debug()))
return handle_gues_debug();
else if ((dr6 = check_bp()))
return handle_bp(dr6);
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
If you do not want
EMULATE_PROCEED, I can just use -1 instead inkvm_vcpu_check_breakpoint, and return if r < 0.But you need to know what to return
EMULATE_DONEorEMULATE_USER_EXIT.Sorry, _not_ return if r < 0.
Function that returns enum or -1? This is worse IMO. Return EMULATE_DONE/EMULATE_USER_EXIT via a pointer will be better.
Quoted 101 lines
Paolo
Quoted 99 lines
Quoted 95 lines
Paolo
Quoted 93 lines
Quoted 89 lines
arch/x86/include/asm/kvm_host.h +0
-0};#define EMULTYPE_NO_DECODE (1 << 0)arch/x86/kvm/x86.c +65
-0@@ -4872,6 +4872,60 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,static int complete_emulated_mmio (struct kvm_vcpu *vcpu);static int complete_emulated_pio (struct kvm_vcpu *vcpu); +static int kvm_vcpu_check_hw_bp (unsigned long addr,u32 type,u32 dr7, +unsigned long *db) +{ +u32 dr6 =0 ; +int i; +u32 enable, rwlen; + + enable = dr7; + rwlen = dr7 >>16 ; +for (i =0 ; i <4 ; i++, enable >>=2 , rwlen >>=4 ) +if ((enable &3 ) && (rwlen &15 ) == type && db[i] == addr) + dr6 |= (1 << i); +return dr6; +} + +static int kvm_vcpu_check_breakpoint (struct kvm_vcpu *vcpu) +{ +struct kvm_run *kvm_run = vcpu->run; +unsigned long eip = vcpu->arch.emulate_ctxt.eip; +u32 dr6 =0 ; + +if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && + (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.guest_debug_dr7, + vcpu->arch.eff_db); + +if (dr6 !=0 ) { + kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; + kvm_run->debug.arch.pc =kvm_rip_read (vcpu) + +get_segment_base (vcpu,VCPU_SREG_CS ); + + kvm_run->debug.arch.exception =DB_VECTOR ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; +return EMULATE_DO_MMIO ; + } + } + +if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK )) { + dr6 =kvm_vcpu_check_hw_bp (eip,0 , + vcpu->arch.dr7, + vcpu->arch.db); + +if (dr6 !=0 ) { + vcpu->arch.dr6 &= ~15 ; + vcpu->arch.dr6 |= dr6; +kvm_queue_exception (vcpu,DB_VECTOR ); +return EMULATE_DONE ; + } + } + +return EMULATE_PROCEED ; +} + int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, int emulation_type,@@ -4892,6 +4946,17 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,if (!(emulation_type &EMULTYPE_NO_DECODE )) {init_emulate_ctxt (vcpu); + +/* +* We will reenter on the same instruction since +* we do not set complete_userspace_io. This does not +* handle watchpoints yet, those would be handled in +* the emulate_ops. +*/ + r =kvm_vcpu_check_breakpoint (vcpu); +if (r !=EMULATE_PROCEED ) +return r; + ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false;Signature · 1 line
1.8.1.4--
Gleb.--
Gleb.
--
Gleb.
Paolo Bonzini
maintainerRe: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation
Il 04/06/2013 14:53, Gleb Natapov ha scritto:
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Yeah. What about:
if ((dr6 =guest_debug()))
returnhandle_gues_debug();
else if ((dr6 =check_bp()))
returnhandle_bp(dr6);
I'll try either this...
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
If you do not want
EMULATE_PROCEED, I can just use -1 instead inkvm_vcpu_check_breakpoint, and return if r < 0.But you need to know what to return
EMULATE_DONEorEMULATE_USER_EXIT.Sorry, _not_ return if r < 0.
Function that returns enum or -1? This is worse IMO. Return EMULATE_DONE/EMULATE_USER_EXIT via a pointer will be better.
... or this, and see what looks nicer. But I like
if (check_bp(&r))
return r;
Thanks for the review.
Paolo