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: 21075 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f199.google.com (mail-qc0-f199.google.com [209.85.216.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B86B125B8B for ; Wed, 16 Oct 2013 17:03:29 +0000 (UTC) Received: by mail-qc0-f199.google.com with SMTP id n4sf2024377qcx.10 for ; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:x-gm-message-state:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=BNM19iZuNtRGrWFGJliCtfstt2hfg+AsZEoV5QbvLes=; b=AdQROvfZrPjnJgGsuuJoLzUuVwnCXvxNQyjfehPhaF3xlWFfn7CfXYRNpo1dXEsRXX rxguEGNiQP1Fl//jDyxpe+aLWYcVdxRbsgOuNUw9Dceo5ZuLfGPDwSe+hs7yaMx4U4cs a/sPCBwXYNGn/QC3Rcwbbxj+CIyFMlhqf0cuRZ2GSKBZoDJRqBoX6LTwuIqzNHDiDe3c pLw98CKiwOVjht22+9NoIQp+U9l2R5GAM/t1X1/9NSYOot9S/Xjj77jFzAGyTWd3dUDk cHqojoStTss7+fGz/Xeo99CvukHLiG+IUV3b2EJLyb7S6V2Re/FAkCTAXQ+GuST9WcBj 5vqg== X-Received: by 10.58.189.163 with SMTP id gj3mr1337868vec.7.1381943009328; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.130.233 with SMTP id oh9ls606944qeb.62.gmail; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) X-Received: by 10.52.228.137 with SMTP id si9mr1067139vdc.29.1381943009185; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) Received: from mail-vb0-f45.google.com (mail-vb0-f45.google.com [209.85.212.45]) by mx.google.com with ESMTPS id dg3si5505939veb.30.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Oct 2013 10:03:29 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.212.45 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.45; Received: by mail-vb0-f45.google.com with SMTP id e15so520162vbg.32 for ; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) X-Gm-Message-State: ALoCoQkp/ZghZPAVJwlzzpZXRR9AAO4PnZ3iNaf0/q8n1oL97AEFbE1RxL0UjB7qh2JR9PlXCX6G X-Received: by 10.58.156.106 with SMTP id wd10mr3112720veb.7.1381943009061; Wed, 16 Oct 2013 10:03:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp54571vcz; Wed, 16 Oct 2013 10:03:28 -0700 (PDT) X-Received: by 10.66.139.38 with SMTP id qv6mr4627530pab.59.1381943008024; Wed, 16 Oct 2013 10:03:28 -0700 (PDT) Received: from mail-pa0-f44.google.com (mail-pa0-f44.google.com [209.85.220.44]) by mx.google.com with ESMTPS id ar2si56997305pbc.352.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Oct 2013 10:03:28 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.44 is neither permitted nor denied by best guess record for domain of anup.patel@linaro.org) client-ip=209.85.220.44; Received: by mail-pa0-f44.google.com with SMTP id lf10so1355323pab.31 for ; Wed, 16 Oct 2013 10:03:27 -0700 (PDT) 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 Cc: linux-arm-kernel@lists.infradead.org, patches@linaro.org, linaro-kernel@lists.linaro.org, Pranavkumar Sawargaonkar , Christoffer Dall , Marc Zyngier , Catalin Marinas , Will Deacon , Mark Rutland , Russell King , Anup Patel 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-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: anup.patel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.45 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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; } /**