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: 115055 Delivered-To: patch@linaro.org Received: by 10.80.163.170 with SMTP id s39csp1262986edb; Fri, 6 Oct 2017 04:39:31 -0700 (PDT) X-Received: by 10.98.214.17 with SMTP id r17mr2001556pfg.246.1507289970892; Fri, 06 Oct 2017 04:39:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507289970; cv=none; d=google.com; s=arc-20160816; b=HySkLV4t+/U7foAuwXlC7o326asB03mVaXkeK1c7nzDdQ9BroQKVJnl8nQwOpzN9O6 1a5u+s+HDGva4E1mHKr5UBrCd3TMj7Sc+6Rjupg2O/8srd3oC5wQbyHXfi9hJshbr9fa 79Ho3vAwMSpA6y91ykZV33Bn8Ao5vt5lritzwfBP71TJu+6ESMFRihq8vsUVyN6fTX7z 8h0w+D8IwbSO2YnoHuSn2GR/26HYw6XQZgrjCBcjZkgyIU3B1FDNXpEzjOX+RMIuvcNV 5rqqEmnJj5Qg4zVU3YK/dMaoRSni3PE91MvSJ4B/u3MCqvdSP0aTb/p7TJSZN80wCnKu x9gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=UwKuQC/yk7g/K15Qz0LbymMDwJ+ks4gf80Kx2AJkLyY=; b=LeyE3Sh+IF6rAqIzqP8no7uvGFag3AEajNaEfZPH3d4a7eYYq9SQngFKCI1fQImMnL CmBkdRnjP+zCTCFQKkbpd5K62NYqWys44zJodxk9WH4Vi6jle53dX4cfQdztFeyB3EXy qSrCpLm9GQUwjGaGL0Qgb0T9fTWtUXoPyxvDos558LPpGx3LMWgQRgAjZWRiuoJeP8sF xbRT1sctyG6a0fCi4PIrYJf2iUziKVx+JYN95cCyx2mqQ34Ih70pUg+R/L+tbCmiLKSi KYHSL1vlP6NTQG7fCtdGR7JQE8ZljhtAmK9cajKlRg9/Wl2fokfT7s6H5m86SHhuMPZQ qv3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hWX6ybeA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si1050591pld.697.2017.10.06.04.39.30; Fri, 06 Oct 2017 04:39:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hWX6ybeA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752083AbdJFLj2 (ORCPT + 26 others); Fri, 6 Oct 2017 07:39:28 -0400 Received: from mail-wr0-f182.google.com ([209.85.128.182]:55627 "EHLO mail-wr0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751774AbdJFLjZ (ORCPT ); Fri, 6 Oct 2017 07:39:25 -0400 Received: by mail-wr0-f182.google.com with SMTP id 22so1659962wrb.12 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=sPsDo5Oz/ujbAjeiR6ZPsR75Oj71ltP4YPXPC6JrIpqodllKS3msIj+er9gETbJ30p 9FvC2p9aGxiLidfN0JcMNsr4PrpC2pziP2Fp1VQ5k9ZevpiJw9OZyEJ41cu4sI28AnDX 7SfY2QFou4Lg2ofiJviAFviACrWaIr6F2m7TgGbp/JI98GKxNUvYIo2CiCvX6wtG7+K4 nAljJYfeqx4JaJ2Kda0PQcAwhAPlHTWtng8lRXMdM+YaV8Xi9KDUmEVrbPvnE9KPrVYC 4U11+R3fIHYBpT2Pq+xOV+FBqFoIYBN9kQmkH1Bh/FKxmLfveeXdFqwFIMZHr7DXcL7P SMAA== X-Gm-Message-State: AMCzsaVRVqPTXSn7rW0851P3eMlR60nG4PqBxIAcPnShl6q08SmXbrTf rw+qvwnqTuK3giGLUCu+myYJ6w== 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_Benn=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_Benn=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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- arch/arm64/kvm/handle_exit.c | 48 +++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-) -- 2.14.1 Reviewed-by: Julien Thierry 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