From patchwork Thu Nov 9 17:00: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: 10051329 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 264E860381 for ; Thu, 9 Nov 2017 17:06:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D74728989 for ; Thu, 9 Nov 2017 17:06:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0255A2B013; Thu, 9 Nov 2017 17:06:50 +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=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A683A28989 for ; Thu, 9 Nov 2017 17:06:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=TOteJ/R+HrrgE8G/TKogwsC0rdwYw18XL3oGlACMgEk=; b=MM/se0mysrR2xr wpetKFEXmeW2BFWlHyzkF2QEJ8HNwnhGV/hcyVbct8pZhQYdJWYC1Rrod0kQklKjAXZWrDF1Jyclg JkUfWgwzvwyEwNpY9C0cUyDziCcEw+O5B+SbwA3EaIROg9IIHXbwZ1A/UD+huSlrSleZH1KfOUzc4 lUlWihMlguqlZ6TT+JIXStpxMEFXKUbw1IgFC8JSqRoIEqo8uYlPonE3i8VJeVHzHUikOZJQH0uZ5 hHWiU/SnlnJs6fvO6IeMWevFXMAukoJnBpeKOvZqNBAFtPhkJnT9W7G0C8FFpREOSPR9tKAOj9kvv 4JLd90j1BmZNxceg346Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eCqHy-0006HA-00; Thu, 09 Nov 2017 17:06:50 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eCqC8-0001oO-4J for linux-arm-kernel@lists.infradead.org; Thu, 09 Nov 2017 17:00:52 +0000 Received: by mail-wm0-x243.google.com with SMTP id r68so2968220wmr.0 for ; Thu, 09 Nov 2017 09:00: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=V6HCRLgqa9ErgrXDTIirUcDf6PpujyqfQP8dm4aQeDI=; b=havQvlqcBCpW8VQKFVjgkzBILNpTmrCupPEsGdcx59tqwrqRSmq5YX6E4uguOTwn3k lu5XPx1T+L59Ouyo8+sp/WoEIXb+4MIij6nHKiimTPEFhtsSjdOY4C2SShKM+c1z9ody 9eQWJW9Gxzj9i3hLb+CwGc5VYHwz68GIYZ5Wg= 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=V6HCRLgqa9ErgrXDTIirUcDf6PpujyqfQP8dm4aQeDI=; b=Z4/v065oFqdR925UKCWck89TKTR+HLQMYBglBwz9vmh9JmEuLXjrYq75sYKMGNEwRL jWarqbzL1YyprqU05k3Swc6SlfiwLeygB87DXXs9Lh07WfcvJFzcDhFLaniXM/iQbprs v0ZdYEfWygHQa8scf+om64t+ruDrEp1dSzXQkoZe3HyqngL0fxY6B5JW9xrvUx4/36JH SQFZOdADPZ/9wNB38rgbIpcy0JtDfiRL9W7t4xZyy9dCyjom1nNpZnWstEtrRLD28afB jBaQkaPdKc2b3rltTcurv5qBljJpD071Sy9MIJFbBarMjQYUO6Czx8pca85PJCNpKIv1 qrwA== X-Gm-Message-State: AJaThX4ZqMwYgG0pS78PU3yBx2/mB02LFvuvI0WWCrCiiJbe1RA32S8Z xeOqtqkuL86H869GTNPsALHyt+u06i0= X-Google-Smtp-Source: AGs4zMaTy7iltqo0B8kZj9HDoIrjtDk8ZNg3ZuUjp96eUS2Yx/cK7KzCRYOFo7yTmEUcvl+EzIFPJA== X-Received: by 10.28.69.8 with SMTP id s8mr343101wma.65.1510246826444; Thu, 09 Nov 2017 09:00:26 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id 25sm7341808wrv.8.2017.11.09.09.00.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Nov 2017 09:00:24 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 567A03E0349; Thu, 9 Nov 2017 17:00: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 Subject: [PATCH v2 2/3] kvm: arm64: handle single-stepping trapped instructions Date: Thu, 9 Nov 2017 17:00:20 +0000 Message-Id: <20171109170021.2984-3-alex.bennee@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171109170021.2984-1-alex.bennee@linaro.org> References: <20171109170021.2984-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171109_090048_488277_B129F763 X-CRM114-Status: GOOD ( 17.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Catalin Marinas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , open list , Will Deacon Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.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