Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1035943pxu; Mon, 23 Nov 2020 10:00:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJwWvSDoVuZPtlCLvOrTzeKj980Cs0Q+1sBh4jgPVslYw/DIvf13EvGT9BAtoV2GcBJ74o5u X-Received: by 2002:a17:906:4482:: with SMTP id y2mr707470ejo.342.1606154413377; Mon, 23 Nov 2020 10:00:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606154413; cv=none; d=google.com; s=arc-20160816; b=JybCsIjmkwYyCuhMMxul7oYzhbbiwR3/wLwWJaupZOZ+Ot70bPBT44Mkop5K0HVNsK oR8mN/N926NZKTN4N3JekLJgejYddoOQ93IOBdtCDLzcvklH9EObzjPHrkBC64VKYXWj VzkbcoGANqC+NVxqEqda2kuUyi9HX7d6HzHNk4xIKLFMTbQSet4+a7s+r2AdPdubxoNQ FBAyU3B+8CvTwt834UMmCsBqmFC8YIS0C1O4GSuQkXC7fPMgAmUk7PWS8TRORvfW2Cly Id9T6/uJ4FXuueDpQTzqxwGkKCH4XO5oIV738GIMUAo7ca/rEqxdCvMvig8uT307ecmE 4L0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=EEKKpmpshBchKfSWI6pWgMWCM4SJSgxCMxP4PgEgKaU=; b=BTSMiiRaxrDupxEeiPfHOVHMC9hlUGr/JDlqa7Ukb8WzRty9UZbfV77vtLYvImWaGA a0VV2wyOhGR3509e7EubOwYP+pkbwseMIBlIdqe7i1+wErU6JAGCjC/uAS8s5OpXZ2d0 0QglA4seWVK73Uueo0yrOpc0bdhm9tAnhxEetuJOdOZjS44RI4D310SLJpHwY3P35Xwf jNLFuP/Th87rUOazH5Sh2V1icAnViFrqd8mGMqK5k4io9alo654L6jyBucuHpZ+LKkH/ yqDtv54YLoPGRJPEX9060TuvqIPotCChqESZoLy1lS3P1IN8FSD2+7iT3JAJ4g73oru+ 6KPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MyGoDeE9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 41si7129598edq.580.2020.11.23.09.59.49; Mon, 23 Nov 2020 10:00:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MyGoDeE9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729478AbgKWRz0 (ORCPT + 99 others); Mon, 23 Nov 2020 12:55:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:52336 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726015AbgKWRz0 (ORCPT ); Mon, 23 Nov 2020 12:55:26 -0500 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1CCE820758; Mon, 23 Nov 2020 17:55:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606154125; bh=qpL2f9sT7xzn5bt7AxFkYrpasWopOpy+BMt6mJNfUdE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=MyGoDeE9Xb5w0Rb1rp40aIH1W3SfLalyE0hoPHRvdaBD9q4AvRwLZdkKf5dKD79/V RrHWMtlp/SRjl9XIKcv3hJgdpA3BGUyL0wMfnyxUtasH03ex+8/qjFsIIluYN/7K+r BPBUP8q+VZGOPlmw0/ZG25s7hrbPWZlT0A2P5H4I= Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=wait-a-minute.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1khG3e-00D1UR-UK; Mon, 23 Nov 2020 17:55:23 +0000 Date: Mon, 23 Nov 2020 17:55:21 +0000 Message-ID: <87blfo556e.wl-maz@kernel.org> From: Marc Zyngier To: David Brazdil Cc: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, James Morse , Julien Thierry , Suzuki K Poulose , Catalin Marinas , Will Deacon , Dennis Zhou , Tejun Heo , Christoph Lameter , Mark Rutland , Lorenzo Pieralisi , Quentin Perret , Andrew Scull , Andrew Walbran , kernel-team@android.com Subject: Re: [PATCH v2 12/24] kvm: arm64: Bootstrap PSCI SMC handler in nVHE EL2 In-Reply-To: <20201116204318.63987-13-dbrazdil@google.com> References: <20201116204318.63987-1-dbrazdil@google.com> <20201116204318.63987-13-dbrazdil@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, mark.rutland@arm.com, lorenzo.pieralisi@arm.com, qperret@google.com, ascull@google.com, qwandor@google.com, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 16 Nov 2020 20:43:06 +0000, David Brazdil wrote: > > Add a handler of PSCI SMCs in nVHE hyp code. The handler is initialized > with the version used by the host's PSCI driver and the function IDs it > was configured with. If the SMC function ID matches one of the > configured PSCI calls (for v0.1) or falls into the PSCI function ID > range (for v0.2+), the SMC is handled by the PSCI handler. For now, all > SMCs return PSCI_RET_NOT_SUPPORTED. > > Signed-off-by: David Brazdil > --- > arch/arm64/include/asm/kvm_hyp.h | 4 ++ > arch/arm64/kvm/arm.c | 14 ++++ > arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 +- > arch/arm64/kvm/hyp/nvhe/psci-relay.c | 104 +++++++++++++++++++++++++++ > 5 files changed, 128 insertions(+), 2 deletions(-) > create mode 100644 arch/arm64/kvm/hyp/nvhe/psci-relay.c > > diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h > index a3289071f3d8..95a2bbbcc7e1 100644 > --- a/arch/arm64/include/asm/kvm_hyp.h > +++ b/arch/arm64/include/asm/kvm_hyp.h > @@ -96,6 +96,10 @@ void deactivate_traps_vhe_put(void); > > u64 __guest_enter(struct kvm_vcpu *vcpu); > > +#ifdef __KVM_NVHE_HYPERVISOR__ > +bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt); > +#endif > + > void __noreturn hyp_panic(void); > #ifdef __KVM_NVHE_HYPERVISOR__ > void __noreturn __hyp_do_panic(bool restore_host, u64 spsr, u64 elr, u64 par); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index cdd7981ea560..7d2270eeecfb 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > #include > > #define CREATE_TRACE_POINTS > @@ -1514,6 +1515,18 @@ static void init_cpu_logical_map(void) > CHOOSE_NVHE_SYM(__cpu_logical_map)[cpu] = cpu_logical_map(cpu); > } > > +static void init_psci_relay(void) > +{ > + extern u32 kvm_nvhe_sym(kvm_host_psci_version); > + extern u32 kvm_nvhe_sym(kvm_host_psci_function_id)[PSCI_FN_MAX]; nit: I'd rather have these outside of the function body. > + int i; > + > + CHOOSE_NVHE_SYM(kvm_host_psci_version) = psci_ops.get_version > + ? psci_ops.get_version() : PSCI_VERSION(0, 0); nit: please write this with an if/else construct, it will read a lot better. > + for (i = 0; i < PSCI_FN_MAX; ++i) > + CHOOSE_NVHE_SYM(kvm_host_psci_function_id)[i] = psci_get_function_id(i); Either pick kvm_nvhe_sym(), or CHOOSE_NVHE_SYM(). Having both used together is just an annoyance (and in this case there is nothing to choose, really). > +} > + > static int init_common_resources(void) > { > return kvm_set_ipa_limit(); > @@ -1693,6 +1706,7 @@ static int init_hyp_mode(void) > } > > init_cpu_logical_map(); > + init_psci_relay(); > > return 0; > > diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile > index 2d842e009a40..bf62c8e42ab2 100644 > --- a/arch/arm64/kvm/hyp/nvhe/Makefile > +++ b/arch/arm64/kvm/hyp/nvhe/Makefile > @@ -7,7 +7,7 @@ asflags-y := -D__KVM_NVHE_HYPERVISOR__ > ccflags-y := -D__KVM_NVHE_HYPERVISOR__ > > obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ > - hyp-main.o hyp-smp.o > + hyp-main.o hyp-smp.o psci-relay.o > obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ > ../fpsimd.o ../hyp-entry.o > > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c > index 71a17af05953..df4acb40dd39 100644 > --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c > @@ -120,7 +120,11 @@ static void skip_host_instruction(void) > > static void handle_host_smc(struct kvm_cpu_context *host_ctxt) > { > - default_host_smc_handler(host_ctxt); > + bool handled; > + > + handled = kvm_host_psci_handler(host_ctxt); > + if (!handled) > + default_host_smc_handler(host_ctxt); > > /* > * Unlike HVC, the return address of an SMC is the instruction's PC. > diff --git a/arch/arm64/kvm/hyp/nvhe/psci-relay.c b/arch/arm64/kvm/hyp/nvhe/psci-relay.c > new file mode 100644 > index 000000000000..d75d3f896bfd > --- /dev/null > +++ b/arch/arm64/kvm/hyp/nvhe/psci-relay.c > @@ -0,0 +1,104 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2020 - Google LLC > + * Author: David Brazdil > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +/* Config options set by the host. */ > +u32 __ro_after_init kvm_host_psci_version = PSCI_VERSION(0, 0); > +u32 __ro_after_init kvm_host_psci_function_id[PSCI_FN_MAX]; > + > +static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) > +{ > + return host_ctxt->regs.regs[0]; > +} > + > +static bool is_psci_0_1_call(u64 func_id) > +{ > + unsigned int i; > + > + for (i = 0; i < ARRAY_SIZE(kvm_host_psci_function_id); ++i) { > + if (func_id == kvm_host_psci_function_id[i]) > + return true; > + } > + return false; > +} > + > +static bool is_psci_0_2_call(u64 func_id) > +{ > + /* SMCCC reserves IDs 0x00-1F with the given 32/64-bit base for PSCI. */ > + return (PSCI_0_2_FN(0) <= func_id && func_id <= PSCI_0_2_FN(31)) || > + (PSCI_0_2_FN64(0) <= func_id && func_id <= PSCI_0_2_FN64(31)); > +} > + > +static bool is_psci_call(u64 func_id) > +{ > + switch (kvm_host_psci_version) { > + case PSCI_VERSION(0, 0): > + return false; > + case PSCI_VERSION(0, 1): > + return is_psci_0_1_call(func_id); > + default: > + return is_psci_0_2_call(func_id); > + } > +} > + > +static unsigned long psci_0_1_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) > +{ > + return PSCI_RET_NOT_SUPPORTED; > +} > + > +static unsigned long psci_0_2_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) > +{ > + switch (func_id) { > + default: > + return PSCI_RET_NOT_SUPPORTED; > + } > +} > + > +static unsigned long psci_1_0_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) > +{ > + switch (func_id) { > + default: > + return psci_0_2_handler(func_id, host_ctxt); > + } > +} > + > +bool kvm_host_psci_handler(struct kvm_cpu_context *host_ctxt) > +{ > + u64 func_id = get_psci_func_id(host_ctxt); > + unsigned long ret; > + > + if (!is_psci_call(func_id)) > + return false; > + > + switch (kvm_host_psci_version) { > + case PSCI_VERSION(0, 0): > + ret = PSCI_RET_NOT_SUPPORTED; But isn't that way too late? No PSCI means that we cannot control the way CPUs boot at all. I think we should completely fail the whole Protected KVM business if we don't have PSCI. > + break; > + case PSCI_VERSION(0, 1): > + ret = psci_0_1_handler(func_id, host_ctxt); > + break; > + case PSCI_VERSION(0, 2): > + ret = psci_0_2_handler(func_id, host_ctxt); > + break; > + default: > + ret = psci_1_0_handler(func_id, host_ctxt); > + break; > + } > + > + host_ctxt->regs.regs[0] = ret; > + host_ctxt->regs.regs[1] = 0; > + host_ctxt->regs.regs[2] = 0; > + host_ctxt->regs.regs[3] = 0; > + return true; > +} > -- > 2.29.2.299.gdc1121823c-goog > > Thanks, M. -- Without deviation from the norm, progress is not possible.