From patchwork Fri Oct 6 11:39:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 9989025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7C79760247 for ; Fri, 6 Oct 2017 11:40:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DF7028B29 for ; Fri, 6 Oct 2017 11:40:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F35A28D6E; Fri, 6 Oct 2017 11:40:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E691728B29 for ; Fri, 6 Oct 2017 11:40:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752045AbdJFLj2 (ORCPT ); Fri, 6 Oct 2017 07:39:28 -0400 Received: from mail-wr0-f169.google.com ([209.85.128.169]:56495 "EHLO mail-wr0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750967AbdJFLj0 (ORCPT ); Fri, 6 Oct 2017 07:39:26 -0400 Received: by mail-wr0-f169.google.com with SMTP id r79so7486113wrb.13 for ; Fri, 06 Oct 2017 04:39:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HYF7hHobPSSuQ8IJMTfxPpGY1uEg67AuQE03qJbobuE=; b=kSijtQCRzN2WAcXmBVwdlIHUgR563lok3GQqScibHfByxEBI4q89gVDreIyx35XV6E oRLesUjpCnM6Dk+VAo9C7auA/q4kac0eKXX/54puZsmB1HPyACo2Jk45CUKp10uFz1/Z maaf+UV0YfpDUn8DjIdOP/LOASQ+BldzLlb8E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HYF7hHobPSSuQ8IJMTfxPpGY1uEg67AuQE03qJbobuE=; b=V0Wnfwd8a1lTFvsQvM+1z/UbOWh/a0mLHNm9EGBQDUqv5CGgz89iPFtYLL0XmHwq51 sCpQc9WM53NkohPCwBBzbdbLwP/X3N8TS83CrjP3VTA6HKKPR6dudOKwSHbRfi3Q0ONp 04kY+22+h3QM/KpGTyMmvfDeCXrW5KpiKVpMpS8G8BE3Dl+xS70vNkV+AbyBPjoj23Ct xEd0PCS1Ie+zEVL6c6BHPwc/uHp6X9j/Kpp2tl7BkDAFti3F3kebUfiGhAzI/5dpBXlt TWuFvhbelI7AIBTwNE0J3+GQ8YVbwDkzUZrL3E8fYo2RY8eh/JfMWJWNM1LVtLwvFeJr eNjQ== X-Gm-Message-State: AMCzsaXFOVF+5740OGqPEiansRMva9Zen8oUsLy0MkAqVGDscS38b7iG MSum9bRsTjHpU03GRnN5neO1IA== X-Google-Smtp-Source: AOwi7QCqWCPXmcMu+Yd0GjXFiU++3I8aEz/V2sRHKYDQc8Py7hxeWXG9oKeGT6V8qbcQS28AdsKRvg== X-Received: by 10.223.142.45 with SMTP id n42mr1656732wrb.278.1507289964704; Fri, 06 Oct 2017 04:39:24 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id w62sm1119448wmb.32.2017.10.06.04.39.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Oct 2017 04:39:23 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id CBDA53E07E1; Fri, 6 Oct 2017 12:39:21 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: julien.thierry@arm.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, christoffer.dall@linaro.org, marc.zyngier@arm.com Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Russell King , Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v1 2/2] kvm: arm64: handle single-step of userspace mmio instructions Date: Fri, 6 Oct 2017 12:39:21 +0100 Message-Id: <20171006113921.24880-3-alex.bennee@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171006113921.24880-1-alex.bennee@linaro.org> References: <20171006113921.24880-1-alex.bennee@linaro.org> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The system state of KVM when using userspace emulation is not complete until we return into KVM_RUN. To handle mmio related updates we wait until they have been committed and then schedule our KVM_EXIT_DEBUG. I've introduced a new function kvm_arm_maybe_return_debug() to wrap up the differences between arm/arm64 which is currently null for arm. Signed-off-by: Alex Bennée Reviewed-by: Julien Thierry --- arch/arm/include/asm/kvm_host.h | 2 ++ arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/debug.c | 21 +++++++++++++++++++++ arch/arm64/kvm/handle_exit.c | 9 +++------ virt/kvm/arm/arm.c | 2 +- virt/kvm/arm/mmio.c | 3 ++- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index 4a879f6ff13b..aec943f6d123 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -285,6 +285,8 @@ static inline void kvm_arm_init_debug(void) {} static inline void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) {} static inline void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) {} static inline void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) {} +static inline int kvm_arm_maybe_return_debug(struct kvm_vcpu *vcpu, + struct kvm_run *run) {} int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e923b58606e2..fa67d21662f6 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -369,6 +369,7 @@ void kvm_arm_init_debug(void); void kvm_arm_setup_debug(struct kvm_vcpu *vcpu); void kvm_arm_clear_debug(struct kvm_vcpu *vcpu); void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu); +int kvm_arm_maybe_return_debug(struct kvm_vcpu *vcpu, struct kvm_run *run); int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index dbadfaf850a7..a10a18c55c87 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -221,3 +221,24 @@ void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) } } } + + +/* + * When KVM has successfully emulated the instruction we might want to + * return we a KVM_EXIT_DEBUG. We can only do this once the emulation + * is complete though so for userspace emulations we have to wait + * until we have re-entered KVM. + * + * Return > 0 to return to guest, 0 (and set exit_reason) on proper + * exit to userspace. + */ + +int kvm_arm_maybe_return_debug(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { + run->exit_reason = KVM_EXIT_DEBUG; + run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT; + return 0; + } + return 1; +} diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index c918d291cb58..7b04f59217bf 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -202,13 +202,10 @@ static int handle_trap_exceptions(struct kvm_vcpu *vcpu, struct kvm_run *run) handled = exit_handler(vcpu, run); } - if (handled && (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)) { - handled = 0; - run->exit_reason = KVM_EXIT_DEBUG; - run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT; - } + if (handled) + return kvm_arm_maybe_return_debug(vcpu, run); - return handled; + return 0; } /* diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index b9f68e4add71..3d28fe2daa26 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -623,7 +623,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) if (run->exit_reason == KVM_EXIT_MMIO) { ret = kvm_handle_mmio_return(vcpu, vcpu->run); - if (ret) + if (ret < 1) return ret; } diff --git a/virt/kvm/arm/mmio.c b/virt/kvm/arm/mmio.c index b6e715fd3c90..e43e3bd6222f 100644 --- a/virt/kvm/arm/mmio.c +++ b/virt/kvm/arm/mmio.c @@ -117,7 +117,8 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run) vcpu_set_reg(vcpu, vcpu->arch.mmio_decode.rt, data); } - return 0; + /* If debugging in effect we may need to return now */ + return kvm_arm_maybe_return_debug(vcpu, run); } static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)