From patchwork Fri Oct 6 11: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: 9989027 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 44BE160247 for ; Fri, 6 Oct 2017 11:40:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 38C6528B29 for ; Fri, 6 Oct 2017 11:40:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2D84428D6E; Fri, 6 Oct 2017 11:40:08 +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 9C93328B29 for ; Fri, 6 Oct 2017 11:40:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752008AbdJFLj1 (ORCPT ); Fri, 6 Oct 2017 07:39:27 -0400 Received: from mail-wr0-f175.google.com ([209.85.128.175]:48305 "EHLO mail-wr0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751807AbdJFLjZ (ORCPT ); Fri, 6 Oct 2017 07:39:25 -0400 Received: by mail-wr0-f175.google.com with SMTP id u5so12678605wrc.5 for ; Fri, 06 Oct 2017 04:39:24 -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=UwKuQC/yk7g/K15Qz0LbymMDwJ+ks4gf80Kx2AJkLyY=; b=hWX6ybeAx+1+lTdHpEDsW9uC4IF4YdzA7Ek45g4r+kJqJwgpc5FELEf2AHMN4bnzY/ IBOegnAwojwP9UDP/jV+2M00w5ow1IIqtmuKRorFDTp9QkFIUl2pV3PvZ8wOrnGNybfn /Psv3iebyz18sX5SXSyYjRbcjEX3aGXM5kE4s= 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=UwKuQC/yk7g/K15Qz0LbymMDwJ+ks4gf80Kx2AJkLyY=; b=WFsCscWsa3R5/eV5o7GC9HDcc/ZiDGNhyHHuzhjlijcO1FrFNds677MPikDjdC8Qjf W2J53qYKGudT9eC8cB0RyNmJmisbG7QUlWZl7opkFdV+mEgH2Hbs6zK2kBy3ym/PSGt3 h5uD0xNTEpreZiP+FC+J7GTIKvzI09kV9KGagvnlOcYKrekpb80rL1AODEyOMQlAKOwk 6QN+w1iZ4K23IoO0xIh6AJDxj+1m/LmBts4arcoWp+qWoHYOvAAedosKJuU+TEJXYMO9 YsJhtRMZenkiJZL5/7YApvpUUjIdFZE0ALC45TnW1ea+p1QWo2S0XtRg/aaaZaZ+ap78 xm8w== X-Gm-Message-State: AMCzsaV6MlG6GHVETpMFmc8xZRiVHlptU7Yk9uDaG4gCVZhLVG07hrpk 5HwJS957N3itJT4gMOjOHnwUMg== X-Google-Smtp-Source: AOwi7QBMKIAxc7e36w2r7R+yvbPS9T4ZY8zvC7yOeiJsK6JER/ZnFNd9zdqXkfNB9/ZW2oHxhkoDjw== X-Received: by 10.223.131.4 with SMTP id 4mr1898341wrd.106.1507289963681; Fri, 06 Oct 2017 04:39:23 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id k141sm1503463wmg.15.2017.10.06.04.39.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Oct 2017 04:39:21 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 86E8B3E04DE; 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?= , Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v1 1/2] KVM: arm64: handle single-stepping trapped instructions Date: Fri, 6 Oct 2017 12:39:20 +0100 Message-Id: <20171006113921.24880-2-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 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. We fake debug.arch.hsr to contain ESR_ELx_EC_SOFTSTP_LOW so QEMU knows it was a single-step event (and without altering the userspace ABI). Signed-off-by: Alex Bennée Reviewed-by: Julien Thierry --- arch/arm64/kvm/handle_exit.c | 48 +++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 7debb74843a0..c918d291cb58 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -178,6 +178,39 @@ static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu) return arm_exit_handlers[hsr_ec]; } +/* + * When handling traps we need to ensure exit the guest if we + * completely emulated the instruction while single-stepping. Stuff to + * be emulated in userspace needs to complete that 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); + } + + 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; + } + + return handled; +} + /* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace. @@ -185,8 +218,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 +245,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