From patchwork Wed Oct 16 17:02:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 3054781 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D53779F2B6 for ; Wed, 16 Oct 2013 17:05:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E11B7204AF for ; Wed, 16 Oct 2013 17:05:34 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1EC2320357 for ; Wed, 16 Oct 2013 17:05:33 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VWUWB-0003zQ-Gu; Wed, 16 Oct 2013 17:04:20 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VWUVp-0007IP-B4; Wed, 16 Oct 2013 17:03:57 +0000 Received: from mail-pb0-f48.google.com ([209.85.160.48]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VWUVh-0007Fn-5Z for linux-arm-kernel@lists.infradead.org; Wed, 16 Oct 2013 17:03:54 +0000 Received: by mail-pb0-f48.google.com with SMTP id ma3so1075390pbc.21 for ; Wed, 16 Oct 2013 10:03:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BNM19iZuNtRGrWFGJliCtfstt2hfg+AsZEoV5QbvLes=; b=i3qOmGmIgl+/GszXSAFRFgngasfQaK8LtoZXRyVHqxfF9iGiiXEvd1Wk10U6575UxB vt/AXsVITBudo+2pwtKsbE0S6nsTAijQXsjldn0rHFNlQHnT75bg0qvDsXL7vstUteKd tBE+aKkszyAiP9eHggV6kOfDInqPCkFqwR+yS+sZqMEatQx9apIrHfR846J/K9XTYIql cXrHQeOmd9jjm1qtyppwgqn0y5z+bK28iUZ2rWPaLlErYhK4EQxtlOjl/hQ2KayzZpLw vpZi5W6jl5K2zfdPISGAGnbd/lVDUSTbwIdLwLPamWYYzFKQTSpQCQQypG/qFTb5ZXlE zH0Q== X-Gm-Message-State: ALoCoQkCatFFWkHOtKECRGTWV7Lx5ukIBTK0v3Vwvbc84frWBXg2r4Aq6NTrY+gmEzBmdGeMekaj X-Received: by 10.68.164.165 with SMTP id yr5mr3968918pbb.146.1381943007374; Wed, 16 Oct 2013 10:03:27 -0700 (PDT) Received: from pnqlab006.amcc.com ([182.73.239.130]) by mx.google.com with ESMTPSA id hz10sm92298689pbc.36.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Oct 2013 10:03:26 -0700 (PDT) From: Anup Patel To: kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 2/5] ARM/ARM64: KVM: Forward PSCI SYSTEM_OFF and SYSTEM_RESET to user space Date: Wed, 16 Oct 2013 22:32:31 +0530 Message-Id: <1381942954-22388-3-git-send-email-anup.patel@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1381942954-22388-1-git-send-email-anup.patel@linaro.org> References: <1381942954-22388-1-git-send-email-anup.patel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131016_130349_591195_7742F3CE X-CRM114-Status: GOOD ( 17.49 ) X-Spam-Score: 0.4 (/) Cc: Mark Rutland , linaro-kernel@lists.linaro.org, Russell King , Anup Patel , patches@linaro.org, Marc Zyngier , Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, Christoffer Dall , Pranavkumar Sawargaonkar X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,KHOP_BIG_TO_CC, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The PSCI SYSTEM_OFF and SYSTEM_RESET functions are VM or Guest level functions hence cannot be emulated by the in-kernel PSCI emulation code. To tackle this, we forward PSCI SYSTEM_OFF and SYSTEM_RESET function calls from Guest to user space (i.e. QEMU or KVMTOOL) via KVM run structure with KVM_EXIT_PSCI exit reason. Signed-off-by: Anup Patel Signed-off-by: Pranavkumar Sawargaonkar --- arch/arm/include/asm/kvm_psci.h | 25 +++++++++++++++- arch/arm/include/uapi/asm/kvm.h | 2 ++ arch/arm/kvm/arm.c | 12 ++++++-- arch/arm/kvm/handle_exit.c | 12 ++++++-- arch/arm/kvm/psci.c | 57 ++++++++++++++++++++++++++++++++++--- arch/arm64/include/asm/kvm_psci.h | 25 +++++++++++++++- arch/arm64/include/uapi/asm/kvm.h | 2 ++ arch/arm64/kvm/handle_exit.c | 24 ++++++++++++---- 8 files changed, 141 insertions(+), 18 deletions(-) diff --git a/arch/arm/include/asm/kvm_psci.h b/arch/arm/include/asm/kvm_psci.h index 9a83d98..783566f 100644 --- a/arch/arm/include/asm/kvm_psci.h +++ b/arch/arm/include/asm/kvm_psci.h @@ -18,6 +18,29 @@ #ifndef __ARM_KVM_PSCI_H__ #define __ARM_KVM_PSCI_H__ -bool kvm_psci_call(struct kvm_vcpu *vcpu); +#include +#include +#include + +/* + * The in-kernel PSCI emulation code wants to use a copy of run->psci, + * which is an anonymous type. Use our own type instead. + */ +struct kvm_exit_psci { + u32 fn; + u64 args[7]; +}; + +static inline void kvm_prepare_psci(struct kvm_run *run, + struct kvm_exit_psci *psci) +{ + run->psci.fn = psci->fn; + memcpy(&run->psci.args, &psci->args, sizeof(run->psci.args)); + memset(&run->psci.ret, 0, sizeof(run->psci.ret)); + run->exit_reason = KVM_EXIT_PSCI; +} + +int kvm_handle_psci_return(struct kvm_vcpu *vcpu, struct kvm_run *run); +int kvm_psci_call(struct kvm_vcpu *vcpu, struct kvm_run *run); #endif /* __ARM_KVM_PSCI_H__ */ diff --git a/arch/arm/include/uapi/asm/kvm.h b/arch/arm/include/uapi/asm/kvm.h index c1ee007..205cf0e 100644 --- a/arch/arm/include/uapi/asm/kvm.h +++ b/arch/arm/include/uapi/asm/kvm.h @@ -171,6 +171,8 @@ struct kvm_arch_memory_slot { #define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) #define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) #define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) +#define KVM_PSCI_FN_SYSTEM_OFF KVM_PSCI_FN(4) +#define KVM_PSCI_FN_SYSTEM_RESET KVM_PSCI_FN(5) #define KVM_PSCI_RET_SUCCESS 0 #define KVM_PSCI_RET_NI ((unsigned long)-1) diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index cc5adb9..5ffd9a3 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -459,7 +459,7 @@ static void update_vttbr(struct kvm *kvm) spin_unlock(&kvm_vmid_lock); } -static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) +static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu, struct kvm_run *run) { if (likely(vcpu->arch.has_run_once)) return 0; @@ -483,7 +483,7 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) */ if (test_and_clear_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) { *vcpu_reg(vcpu, 0) = KVM_PSCI_FN_CPU_OFF; - kvm_psci_call(vcpu); + kvm_psci_call(vcpu, run); } return 0; @@ -520,7 +520,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) if (unlikely(!kvm_vcpu_initialized(vcpu))) return -ENOEXEC; - ret = kvm_vcpu_first_run_init(vcpu); + ret = kvm_vcpu_first_run_init(vcpu, vcpu->run); if (ret) return ret; @@ -530,6 +530,12 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) return ret; } + if (run->exit_reason == KVM_EXIT_PSCI) { + ret = kvm_handle_psci_return(vcpu, vcpu->run); + if (ret) + return ret; + } + if (vcpu->sigset_active) sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); diff --git a/arch/arm/kvm/handle_exit.c b/arch/arm/kvm/handle_exit.c index df4c82d..1a12e6c 100644 --- a/arch/arm/kvm/handle_exit.c +++ b/arch/arm/kvm/handle_exit.c @@ -40,14 +40,20 @@ static int handle_svc_hyp(struct kvm_vcpu *vcpu, struct kvm_run *run) static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) { + int ret; + trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), kvm_vcpu_hvc_get_imm(vcpu)); - if (kvm_psci_call(vcpu)) + ret = kvm_psci_call(vcpu, run); + if (!ret) + return 1; + else if (ret == -EINVAL) { + kvm_inject_undefined(vcpu); return 1; + } - kvm_inject_undefined(vcpu); - return 1; + return 0; } static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) diff --git a/arch/arm/kvm/psci.c b/arch/arm/kvm/psci.c index 86a693a..72c23a7 100644 --- a/arch/arm/kvm/psci.c +++ b/arch/arm/kvm/psci.c @@ -71,6 +71,45 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) return KVM_PSCI_RET_SUCCESS; } +static void kvm_psci_system_off(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + struct kvm_exit_psci psci; + + psci.fn = KVM_PSCI_FN_SYSTEM_OFF; + memset(&psci.args, 0, sizeof(psci.args)); + kvm_prepare_psci(run, &psci); +} + +static void kvm_psci_system_reset(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + struct kvm_exit_psci psci; + + psci.fn = KVM_PSCI_FN_SYSTEM_RESET; + memset(&psci.args, 0, sizeof(psci.args)); + kvm_prepare_psci(run, &psci); +} + +/** + * kvm_handle_psci_return -- Handle PSCI after user space emulation + * @vcpu: The VCPU pointer + * @run: The VCPU run struct containing the psci data + * + * This should only be called after returning from userspace for + * PSCI emulation. + */ +int kvm_handle_psci_return(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + /* + * Currently, the PSCI functions passed to user space for emulation + * are SYSTEM_OFF and SYSTEM_RESET. These PSCI functions are not + * expected to return back after emulating in user space hence by + * default we return -EINVAL to avoid user space from doing RUN ioctl + * after handling KVM_EXIT_PSCI. + */ + + return -EINVAL; +} + /** * kvm_psci_call - handle PSCI call if r0 value is in range * @vcpu: Pointer to the VCPU struct @@ -81,8 +120,9 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) * function number specified in r0 is withing the PSCI range, and false * otherwise. */ -bool kvm_psci_call(struct kvm_vcpu *vcpu) +int kvm_psci_call(struct kvm_vcpu *vcpu, struct kvm_run *run) { + int ret = 0; unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0); unsigned long val; @@ -98,11 +138,20 @@ bool kvm_psci_call(struct kvm_vcpu *vcpu) case KVM_PSCI_FN_MIGRATE: val = KVM_PSCI_RET_NI; break; - + case KVM_PSCI_FN_SYSTEM_OFF: + kvm_psci_system_off(vcpu, run); + val = KVM_PSCI_RET_SUCCESS; + ret = -EINTR; + break; + case KVM_PSCI_FN_SYSTEM_RESET: + kvm_psci_system_reset(vcpu, run); + val = KVM_PSCI_RET_SUCCESS; + ret = -EINTR; + break; default: - return false; + return -EINVAL; } *vcpu_reg(vcpu, 0) = val; - return true; + return ret; } diff --git a/arch/arm64/include/asm/kvm_psci.h b/arch/arm64/include/asm/kvm_psci.h index e301a48..db90649 100644 --- a/arch/arm64/include/asm/kvm_psci.h +++ b/arch/arm64/include/asm/kvm_psci.h @@ -18,6 +18,29 @@ #ifndef __ARM64_KVM_PSCI_H__ #define __ARM64_KVM_PSCI_H__ -bool kvm_psci_call(struct kvm_vcpu *vcpu); +#include +#include +#include + +/* + * The in-kernel PSCI emulation code wants to use a copy of run->psci, + * which is an anonymous type. Use our own type instead. + */ +struct kvm_exit_psci { + u32 fn; + u64 args[7]; +}; + +static inline void kvm_prepare_psci(struct kvm_run *run, + struct kvm_exit_psci *psci) +{ + run->psci.fn = psci->fn; + memcpy(&run->psci.args, &psci->args, sizeof(run->psci.args)); + memset(&run->psci.ret, 0, sizeof(run->psci.ret)); + run->exit_reason = KVM_EXIT_PSCI; +} + +int kvm_handle_psci_return(struct kvm_vcpu *vcpu, struct kvm_run *run); +int kvm_psci_call(struct kvm_vcpu *vcpu, struct kvm_run *run); #endif /* __ARM64_KVM_PSCI_H__ */ diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index d9f026b..f678902 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -158,6 +158,8 @@ struct kvm_arch_memory_slot { #define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) #define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) #define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) +#define KVM_PSCI_FN_SYSTEM_OFF KVM_PSCI_FN(4) +#define KVM_PSCI_FN_SYSTEM_RESET KVM_PSCI_FN(5) #define KVM_PSCI_RET_SUCCESS 0 #define KVM_PSCI_RET_NI ((unsigned long)-1) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 9beaca0..28e20bb 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -30,20 +30,32 @@ typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *); static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) { - if (kvm_psci_call(vcpu)) + int ret; + + ret = kvm_psci_call(vcpu, run); + if (!ret) + return 1; + else if (ret == -EINVAL) { + kvm_inject_undefined(vcpu); return 1; + } - kvm_inject_undefined(vcpu); - return 1; + return 0; } static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) { - if (kvm_psci_call(vcpu)) + int ret; + + ret = kvm_psci_call(vcpu, run); + if (!ret) + return 1; + else if (ret == -EINVAL) { + kvm_inject_undefined(vcpu); return 1; + } - kvm_inject_undefined(vcpu); - return 1; + return 0; } /**