From: Nadav Amit Subject: Re: [PATCH 2/2] KVM: x86: Guest watchpoints during emulation. Date: Thu, 24 Sep 2015 13:03:04 +0300 Message-ID: <7482EC4E-8C78-4042-9F74-66B798A9E715@gmail.com> References: <1443085690-9824-1-git-send-email-namit@cs.technion.ac.il> <1443085690-9824-3-git-send-email-namit@cs.technion.ac.il> Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Thomas Gleixner , X86 ML , kvm list , Andrey Isakov , ramibnet@gmail.com To: Paolo Bonzini Return-path: Received: from mail-wi0-f177.google.com ([209.85.212.177]:37700 "EHLO mail-wi0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752022AbbIXKDJ convert rfc822-to-8bit (ORCPT ); Thu, 24 Sep 2015 06:03:09 -0400 Received: by wicfx3 with SMTP id fx3so105278970wic.0 for ; Thu, 24 Sep 2015 03:03:07 -0700 (PDT) In-Reply-To: <1443085690-9824-3-git-send-email-namit@cs.technion.ac.il> Sender: kvm-owner@vger.kernel.org List-ID: Looking at the patch again, there two points you may want to change, se= e inline. If you want, I=E2=80=99ll send a v2. Nadav Nadav Amit wrote: > From: Nadav Amit >=20 > This adds support for guest data and I/O breakpoints during instructi= on > emulation. >=20 > Watchpoints are examined during data and io interceptions: segmented_= read, > segmented_write, em_in, em_out, segmented_read_std and kvm_fast_pio_o= ut. >=20 > When such a breakpoint is triggered, trap is reported by DB_VECTOR > exception. >=20 > Signed-off-by: Andrey Isakov > Signed-off-by: Rami Burstein > Signed-off-by: Nadav Amit > --- > arch/x86/include/asm/kvm_emulate.h | 3 ++ > arch/x86/kvm/emulate.c | 32 +++++++++++++++++ > arch/x86/kvm/x86.c | 74 +++++++++++++++++++++++++++++= --------- > 3 files changed, 92 insertions(+), 17 deletions(-) >=20 > diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/as= m/kvm_emulate.h > index e16466e..f6d5d6c 100644 > --- a/arch/x86/include/asm/kvm_emulate.h > +++ b/arch/x86/include/asm/kvm_emulate.h > @@ -211,6 +211,9 @@ struct x86_emulate_ops { > void (*get_cpuid)(struct x86_emulate_ctxt *ctxt, > u32 *eax, u32 *ebx, u32 *ecx, u32 *edx); > void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked); > + bool (*check_watchpoint)(struct x86_emulate_ctxt *ctxt, > + unsigned long addr, unsigned int length, > + int type); > }; >=20 > typedef u32 __attribute__((vector_size(16))) sse128_t; > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index b372a75..4e91b7b 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -24,6 +24,7 @@ > #include "kvm_cache_regs.h" > #include > #include > +#include > #include > #include >=20 > @@ -564,6 +565,17 @@ static int emulate_db(struct x86_emulate_ctxt *c= txt) > return emulate_exception(ctxt, DB_VECTOR, 0, false); > } >=20 > +static void emulate_db_trap(struct x86_emulate_ctxt *ctxt) > +{ > + /* > + * If a fault is later encountered, the exception information will = be > + * overridden. Otherwise the trap would be handled after the emulat= ion > + * is completed. > + */ > + (void)emulate_exception(ctxt, DB_VECTOR, 0, false); > + ctxt->have_exception =3D true; > +} > + > static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) > { > return emulate_exception(ctxt, GP_VECTOR, err, true); > @@ -776,6 +788,10 @@ static int segmented_read_std(struct x86_emulate= _ctxt *ctxt, > rc =3D linearize(ctxt, addr, size, false, &linear); > if (rc !=3D X86EMUL_CONTINUE) > return rc; > + > + if (ctxt->ops->check_watchpoint(ctxt, linear, size, DR_RW_READ)) > + emulate_db_trap(ctxt); > + > return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exceptio= n); > } >=20 > @@ -1369,6 +1385,10 @@ static int segmented_read(struct x86_emulate_c= txt *ctxt, > rc =3D linearize(ctxt, addr, size, false, &linear); > if (rc !=3D X86EMUL_CONTINUE) > return rc; > + > + if (ctxt->ops->check_watchpoint(ctxt, linear, size, DR_RW_READ)) > + emulate_db_trap(ctxt); > + > return read_emulated(ctxt, linear, data, size); > } >=20 > @@ -1383,6 +1403,10 @@ static int segmented_write(struct x86_emulate_= ctxt *ctxt, > rc =3D linearize(ctxt, addr, size, true, &linear); > if (rc !=3D X86EMUL_CONTINUE) > return rc; > + > + if (ctxt->ops->check_watchpoint(ctxt, linear, size, DR_RW_WRITE)) > + emulate_db_trap(ctxt); > + > return ctxt->ops->write_emulated(ctxt, linear, data, size, > &ctxt->exception); > } > @@ -3729,11 +3753,19 @@ static int em_in(struct x86_emulate_ctxt *ctx= t) > &ctxt->dst.val)) > return X86EMUL_IO_NEEDED; >=20 > + if (ctxt->ops->check_watchpoint(ctxt, ctxt->src.val, ctxt->dst.byte= s, > + DR_RW_PORT)) > + emulate_db_trap(ctxt); > + > return X86EMUL_CONTINUE; > } >=20 > static int em_out(struct x86_emulate_ctxt *ctxt) > { > + if (ctxt->ops->check_watchpoint(ctxt, ctxt->dst.val, ctxt->src.byte= s, > + DR_RW_PORT)) > + emulate_db_trap(ctxt); > + > ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, > &ctxt->src.val, 1); > /* Disable writeback. */ > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 3e4d032..ba75f76 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -4831,6 +4831,55 @@ static void emulator_set_nmi_mask(struct x86_e= mulate_ctxt *ctxt, bool masked) > kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked); > } >=20 > +static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 le= n, > + u32 dr7, unsigned long *db) > +{ > + u32 dr6 =3D 0; > + int i; > + u32 enable, dr7_rw, dr7_len; > + unsigned long align_db; > + > + enable =3D dr7; > + dr7_rw =3D dr7 >> 16; > + dr7_len =3D dr7_rw >> 2; > + for (i =3D 0; i < 4; i++, enable >>=3D 2, dr7_rw >>=3D 4, dr7_len >= >=3D 4) { > + if (!(enable & 3)) > + continue; > + > + /* Check type matches, but on writes, check read bp */ > + if (((dr7_rw & 3) !=3D type && > + !((dr7_rw & 3) =3D=3D DR_RW_READ && type =3D=3D DR_RW_WRITE))) > + continue; > + align_db =3D db[i] & ~((unsigned long)dr7_len & 3); > + if (addr < (align_db + (dr7_len & 3) + 1) && > + align_db < (addr + len)) > + dr6 |=3D (1 << i); > + } > + return dr6; > +} > + > +static bool emulator_check_watchpoint(struct x86_emulate_ctxt *ctxt, > + unsigned long addr, unsigned int length, > + int type) > +{ > + u32 dr6; > + struct kvm_vcpu *vcpu =3D emul_to_vcpu(ctxt); > + > + if (likely(!(vcpu->arch.dr7 & DR7_BP_EN_MASK)) || > + (type =3D=3D DR_RW_PORT && !(emulator_get_cr(ctxt, 4) & X86_CR4= _DE))) You may want to call kvm_get_cr4 instead of emulator_get_cr . > + return false; > + > + dr6 =3D kvm_vcpu_check_hw_bp(addr, type, length, vcpu->arch.dr7, > + vcpu->arch.db); > + > + if (dr6 !=3D 0) { > + __kvm_set_dr(vcpu, DR_STATUS, > + (vcpu->arch.dr6 & ~DR_TRAP_BITS) | dr6); I think there is no need to mask DR_TRAP_BIT. It may cause a problem if= we have multiple breakpoints on the same instruction. > + return true; > + } > + return false; > +} > + > static const struct x86_emulate_ops emulate_ops =3D { > .read_gpr =3D emulator_read_gpr, > .write_gpr =3D emulator_write_gpr, > @@ -4869,6 +4918,7 @@ static const struct x86_emulate_ops emulate_ops= =3D { > .intercept =3D emulator_intercept, > .get_cpuid =3D emulator_get_cpuid, > .set_nmi_mask =3D emulator_set_nmi_mask, > + .check_watchpoint =3D emulator_check_watchpoint, > }; >=20 > static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) > @@ -5118,21 +5168,6 @@ static void kvm_set_hflags(struct kvm_vcpu *vc= pu, unsigned emul_flags) > kvm_smm_changed(vcpu); > } >=20 > -static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr= 7, > - unsigned long *db) > -{ > - u32 dr6 =3D 0; > - int i; > - u32 enable, rwlen; > - > - enable =3D dr7; > - rwlen =3D dr7 >> 16; > - for (i =3D 0; i < 4; i++, enable >>=3D 2, rwlen >>=3D 4) > - if ((enable & 3) && (rwlen & 15) =3D=3D type && db[i] =3D=3D addr) > - dr6 |=3D (1 << i); > - return dr6; > -} > - > static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned= long rflags, int *r) > { > struct kvm_run *kvm_run =3D vcpu->run; > @@ -5173,7 +5208,7 @@ static bool kvm_vcpu_check_breakpoint(struct kv= m_vcpu *vcpu, int *r) > (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { > struct kvm_run *kvm_run =3D vcpu->run; > unsigned long eip =3D kvm_get_linear_rip(vcpu); > - u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, 0, > + u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, DR_RW_EXECUTE, 1, > vcpu->arch.guest_debug_dr7, > vcpu->arch.eff_db); >=20 > @@ -5190,7 +5225,7 @@ static bool kvm_vcpu_check_breakpoint(struct kv= m_vcpu *vcpu, int *r) > if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && > !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { > unsigned long eip =3D kvm_get_linear_rip(vcpu); > - u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, 0, > + u32 dr6 =3D kvm_vcpu_check_hw_bp(eip, DR_RW_EXECUTE, 1, > vcpu->arch.dr7, > vcpu->arch.db); >=20 > @@ -5346,6 +5381,11 @@ int kvm_fast_pio_out(struct kvm_vcpu *vcpu, in= t size, unsigned short port) > unsigned long val =3D kvm_register_read(vcpu, VCPU_REGS_RAX); > int ret =3D emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt, > size, port, &val, 1); > + > + if (emulator_check_watchpoint(&vcpu->arch.emulate_ctxt, port, size, > + DR_RW_PORT)) > + kvm_queue_exception(vcpu, DB_VECTOR); > + > /* do not return to emulator after return from userspace */ > vcpu->arch.pio.count =3D 0; > return ret; > --=20 > 2.1.4