From patchwork Thu May 30 16:00:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 2637411 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id A996640276 for ; Thu, 30 May 2013 16:01:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934303Ab3E3QAv (ORCPT ); Thu, 30 May 2013 12:00:51 -0400 Received: from mail-ea0-f169.google.com ([209.85.215.169]:61301 "EHLO mail-ea0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934231Ab3E3QAn (ORCPT ); Thu, 30 May 2013 12:00:43 -0400 Received: by mail-ea0-f169.google.com with SMTP id m14so542221eaj.0 for ; Thu, 30 May 2013 09:00:42 -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=gjjRVQEQUE9YTrKLy2tVidHDmf9ppIFHH5lMs08rvUE=; b=sxa6Z1WPcNwR303j+fsrbamTRTm8+ar27HIM7qRQ9mfY/oje5wRLqN3CvyrC+FDlHN 5RRk64VShf+96beeZxrnrPZMy89C/DlgheXXO+XSodLhi8bepzT/KVPjwHf2vJNt115v kwEWt5hLlrCvhl403zFU78zZuDuhmgXLANMNlJHz+32kInpdcj63usr+D+SM3bVoH8oy fyB/ch9txbgSoLLMeK6mXJX64aB6wiEp6iicNA9MN+DOfIPkVW2pkK985XxWPWTIK0eM zgdIqdIVwXON+hjc682fBieQuVb2n/OxfliUzn9IFR9hztl+riKF/XAsN3hvZMDBpmzh Hnig== X-Received: by 10.15.81.197 with SMTP id x45mr10469380eey.9.1369929641991; Thu, 30 May 2013 09:00:41 -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.39 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 30 May 2013 09:00:41 -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 2/2] KVM: x86: handle singlestep during emulation Date: Thu, 30 May 2013 18:00:31 +0200 Message-Id: <1369929631-2101-3-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. 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. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 41 +++++++++++++++++++++++++++++++++++++- 1 files changed, 40 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 33b51bc..0728096 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -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;