From patchwork Thu Nov 16 15:39:20 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: 10061419 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 BF0EC601AE for ; Thu, 16 Nov 2017 15:40:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF9E72AA97 for ; Thu, 16 Nov 2017 15:40:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2C4F2AA9A; Thu, 16 Nov 2017 15:40:07 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI 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 515D72AA97 for ; Thu, 16 Nov 2017 15:40:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965251AbdKPPji (ORCPT ); Thu, 16 Nov 2017 10:39:38 -0500 Received: from mail-wm0-f67.google.com ([74.125.82.67]:40903 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935677AbdKPPj2 (ORCPT ); Thu, 16 Nov 2017 10:39:28 -0500 Received: by mail-wm0-f67.google.com with SMTP id b189so1010562wmd.5 for ; Thu, 16 Nov 2017 07:39:27 -0800 (PST) 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=slpupatpw0H5CEBDfNoAa38imSLd9H2hQ5fwhN9ClgM=; b=EGjta8svYFrnc10lGrCuaJMwxX1lw3PhDah2isnz7TWa4fyx7k+PVozKa8Ftg91cHF CYpC/yVDSCTBB5LhkMcc3RVlUQkqykKVL2hlzbfi4mTqgI5xIPONFQlvY9XqY7LtNksq qggqY6CG36c+qLLKbkIoyovSdOhHOUfSg7baY= 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=slpupatpw0H5CEBDfNoAa38imSLd9H2hQ5fwhN9ClgM=; b=rb1PT79RF5eUeUZAEqP0kpsNqFaDQVe0HT7bjbfKrZsSSaNlnTnt39DGvgtnUQhW1d 3wweoW2UpmWCZxzubudhSJpbwO6I6OsfOwZMRXDuB5OjhPSS6FoIZtlwDOu7lraALc3E mdxu0M3yH7jVF4eKhRfip+FKYxTPLbhgXZ8LLjKrUxODp8b4TzuT78WiLqskW4N6bETP 1pt+rctTw+m8RZD4X8IlhwjepbSkW1+V9FTViDtNFyWb2fm/78yb9C3dzbI5S6u0FF97 D42p6RK+S43tNFvoKK07VNwqjC2yyASskLquqemeh6EDISQgTFjISz69KV6Jdo4Nttc5 NE5g== X-Gm-Message-State: AJaThX64uB79GutdLSjDZ1RV3QvVCipOD2dfpWITjeXRaa3pPT7Veofh DDlf/PNC5GWlSaQM78DgirGSlA== X-Google-Smtp-Source: AGs4zMZB1Nvifo9Zue5o2VBa8Sp7GHANP5LqarbIXoqsjT2i8mQPoEDyfPJzoE9W6IM23RD9lizy5g== X-Received: by 10.28.70.131 with SMTP id t125mr1778583wma.92.1510846766528; Thu, 16 Nov 2017 07:39:26 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id l16sm4355131wma.19.2017.11.16.07.39.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Nov 2017 07:39:23 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 2F3A93E0412; Thu, 16 Nov 2017 15:39:22 +0000 (GMT) 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?= , Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v3 2/3] kvm: arm64: handle single-stepping trapped instructions Date: Thu, 16 Nov 2017 15:39:20 +0000 Message-Id: <20171116153921.21991-3-alex.bennee@linaro.org> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171116153921.21991-1-alex.bennee@linaro.org> References: <20171116153921.21991-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 If we are using guest debug to single-step the guest we need to ensure we exit after emulating the instruction. This only affects instructions completely emulated by the kernel. For userspace emulated instructions we need to exit and return to complete the emulation. The kvm_arm_handle_step_debug() helper sets up the necessary exit state if needed. Signed-off-by: Alex Bennée Reviewed-by: Julien Thierry --- v2 - use helper from patch 1 - if (handled > 0) instead of if (handled) so errors propagate --- arch/arm64/kvm/handle_exit.c | 47 +++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 7debb74843a0..af1c804742f6 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -178,6 +178,38 @@ static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) return arm_exit_handlers[hsr_ec]; } +/* + * We may be single-stepping an emulated instruction. If the emulation + * has been completed in-kernel we can return to userspace with a + * KVM_EXIT_DEBUG, otherwise the userspace needs to complete its + * emulation first. + */ + +static int handle_trap_exceptions(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + int handled; + + /* + * See ARM ARM B1.14.1: "Hyp traps on instructions + * that fail their condition code check" + */ + if (!kvm_condition_valid(vcpu)) { + kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); + handled = 1; + } else { + exit_handle_fn exit_handler; + + exit_handler = kvm_get_exit_handler(vcpu); + handled = exit_handler(vcpu, run); + } + + /* helper sets exit_reason if we need to return to userspace */ + if (handled > 0 && kvm_arm_handle_step_debug(vcpu, run)) + handled = 0; + + return handled; +} + /* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace. @@ -185,8 +217,6 @@ static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, int exception_index) { - exit_handle_fn exit_handler; - if (ARM_SERROR_PENDING(exception_index)) { u8 hsr_ec = ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu)); @@ -214,18 +244,7 @@ int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, kvm_inject_vabt(vcpu); return 1; case ARM_EXCEPTION_TRAP: - /* - * See ARM ARM B1.14.1: "Hyp traps on instructions - * that fail their condition code check" - */ - if (!kvm_condition_valid(vcpu)) { - kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu)); - return 1; - } - - exit_handler = kvm_get_exit_handler(vcpu); - - return exit_handler(vcpu, run); + return handle_trap_exceptions(vcpu, run); case ARM_EXCEPTION_HYP_GONE: /* * EL2 has been reset to the hyp-stub. This happens when a guest