From patchwork Thu May 30 16:00:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 2637391 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 02313DFF69 for ; Thu, 30 May 2013 16:01:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934367Ab3E3QA4 (ORCPT ); Thu, 30 May 2013 12:00:56 -0400 Received: from mail-ea0-f174.google.com ([209.85.215.174]:39621 "EHLO mail-ea0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934275Ab3E3QAn (ORCPT ); Thu, 30 May 2013 12:00:43 -0400 Received: by mail-ea0-f174.google.com with SMTP id z7so516914eaf.19 for ; Thu, 30 May 2013 09:00:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=iEfqW5BWVW8LUGV5qI5yUfMULPr2mj/YLWgV0BBK088=; b=I0By0WdlwmNHsQgG+Mqo0a4SpYLgBZzq4hKCOy6sJa0rx/8SiHmMBYyuUhjhZKjmGj ibxgd+f3snGEfGZvsaGqW6jU7r5BfxqBKNulB8h0qPWAqI8E4eXbnOEaqrFoYiz00dAP 3gioMVHAcXhE3SDQCicjd9neisUNU8/8AGUuFbwbvXnONgwwS6Wtl5mIXqipAZ/ot8uT OYzEupBVye69CZoBwVAt5QNEHXVLl865uBhfGBOqJHKM6TMRHJ2Q/CsCJP5cYnqEBlHm 2bxlgo4Fj3Iwrm6P+MwXsq47Y9Z/Pqhgrz+v7YTY1PeVWTstepUYaf2D1DxYi7U9d5lL ZZgw== X-Received: by 10.14.172.70 with SMTP id s46mr10322692eel.133.1369929639737; Thu, 30 May 2013 09:00:39 -0700 (PDT) Received: from playground.lan (net-37-116-217-184.cust.dsl.vodafone.it. [37.116.217.184]) by mx.google.com with ESMTPSA id f1sm33054035eem.17.2013.05.30.09.00.37 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 30 May 2013 09:00:38 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, gnatapov@redhat.com, jan.kiszka@siemens.com Subject: [PATCH 1/2] KVM: x86: handle hardware breakpoints during emulation Date: Thu, 30 May 2013 18:00:30 +0200 Message-Id: <1369929631-2101-2-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1369929631-2101-1-git-send-email-pbonzini@redhat.com> References: <1369929631-2101-1-git-send-email-pbonzini@redhat.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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_MMIO to exit with KVM_EXIT_DEBUG. Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/x86.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index e2e09f3..aefd8c2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -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) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1d928af..33b51bc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -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;