Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3533362pxb; Mon, 16 Nov 2020 18:08:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUDZY8t3tCHBKLrISTmDExhD+7Ga2Dlu16i/CPg7r4IJbsc3X0ISNx4aIKphLFQuh1dA8I X-Received: by 2002:a50:da46:: with SMTP id a6mr14334282edk.272.1605578909766; Mon, 16 Nov 2020 18:08:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605578909; cv=none; d=google.com; s=arc-20160816; b=P5vEKdNug9sjfbutcrPQnTAOkOMjCAHkLyl86NTlVoDOMwlihkWZcvGaWUUgqeZ0CT z5O9btvNfYA3snG/GU9AiC16V6c/3YWeiujouE6abu9cO6RMPnCC6QWe2vopbzofkB87 t3xZJW8bpg8Oyo7K3p7mClYJAWlz/MsLzpNQw+tfQwD0mVZ7eGDkc9KbhidP4Js/03VD 3rc8mNUAmIXwTXyfusA6CDqbefuIS70Unemx7lf7wleQH8IQyTVuemS0Vbd5vIqqJ0O9 7ulSqy0qvbzjCuG7O3Fwo7RMQcVFqzUDdgRIxzxwSgXTQCwRINRJtsCmqSvvV+Uga9ai 1mYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qG/HoneOc994OJu++CpnomVnwB4P/ZTLebHJA4Q7lT4=; b=S/aYmtpAdHqcwL0WN9BeuXG3ia7oIkqfivamuznb0MGPYJGfVKJWHsdlkDeVNbOdoI 1M3ueUzbosEn3d5kG83FsVBqPlkFEobzPABw2mMWtDvRVoBFBazpRhL3cwnrrukk/dbR 3dPUBkRMOhRZj4dZJ5CSY1+Ulq8jkCalKtp8LU2NchR9j+UT5lU7Klk1yz3QPl/a54TL VACYPB+ziawkYWW+Oo3RO2eoRXGIzu8VwVReiULK916apmJMKd87gZf9Cq1T23Xd4NSm ovJ7xlTRiru2XyVlkCmklSuDO2ZHacQRN+QhC9z1/Jj67C79OQ/K8D6SxquXK66N3LoW a0Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fuyl7aAN; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a19si13651562edb.410.2020.11.16.18.08.06; Mon, 16 Nov 2020 18:08:29 -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=@google.com header.s=20161025 header.b=fuyl7aAN; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387708AbgKPUnx (ORCPT + 99 others); Mon, 16 Nov 2020 15:43:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727879AbgKPUnv (ORCPT ); Mon, 16 Nov 2020 15:43:51 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A397C0613CF for ; Mon, 16 Nov 2020 12:43:50 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id 23so658091wmg.1 for ; Mon, 16 Nov 2020 12:43:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qG/HoneOc994OJu++CpnomVnwB4P/ZTLebHJA4Q7lT4=; b=fuyl7aANEA8fwOLCn2RRwgCPg5sH4c3V57+KawyAxxoLb7efdJQZXGdT9w5DVinpSN p2cAZJJWwFiJGbY03YsNvgLfePDdDVDyDPwOMDf/VM7RYux4B6QAjpaMrNMdIhM2JrsN F8/TRQQH4URgM1anQ1ZAltLVCT4z9ckQp5p08BYXpuYeiA5tS7vX6pKlf9hEYaW2vPc8 LWhKPK+b9hxepoECbGLi31tWLqhNOzvRoPIjHvF/XPajEqHh29e1qKoQCVrXM+XHJ+ZV ayVTPHi56g7rqfkgAjMCABWTBv39onhqdO/kne03yfewB09hmJUpxDdqQNE33LiVwuUS t7CQ== 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=qG/HoneOc994OJu++CpnomVnwB4P/ZTLebHJA4Q7lT4=; b=ASQ0yBvi3AUbh2fL4isia1TMT1r42enid6oX8dBKIuBlV5wZ5SO66w8Blr3Hf+FQ6k Q/uKbm02UwxGoKmb7afqX9Z0bZDlgIypBj7W10drzefU/UHhIwol4UrE1Mq0gvcIa4IH hBwNt9jGGduMrFIDWKX0+tjK7D192d9N9o8BZJYeGE8hDiDWeU+q4LnHHj7mLolF9MU9 z1ckZa2GZaC8gKnhuCb/VfrWTdv1qBtW4eHzxZLT3dExnT7nZCfZqrqCAQZxKAcwBACu MNp73DgKjgXTj8++7K0Gli+/cHJWdEUCmOreKm7z3edw7dlnlInblF2TN2/b7xvIoAhL hGNA== X-Gm-Message-State: AOAM533xi+CGu0/xWhZepKtoczTaMtHbkK2RxOSJVIfCmP/vbW76Lgo/ /kT8Wqkr2egaXx8uRcSO2EQNOw== X-Received: by 2002:a1c:2586:: with SMTP id l128mr656447wml.149.1605559428664; Mon, 16 Nov 2020 12:43:48 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:bc40:bd71:373a:1b33]) by smtp.gmail.com with ESMTPSA id f17sm562523wmh.10.2020.11.16.12.43.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Nov 2020 12:43:47 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Marc Zyngier , 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, David Brazdil Subject: [PATCH v2 12/24] kvm: arm64: Bootstrap PSCI SMC handler in nVHE EL2 Date: Mon, 16 Nov 2020 20:43:06 +0000 Message-Id: <20201116204318.63987-13-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201116204318.63987-1-dbrazdil@google.com> References: <20201116204318.63987-1-dbrazdil@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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]; + int i; + + CHOOSE_NVHE_SYM(kvm_host_psci_version) = psci_ops.get_version + ? psci_ops.get_version() : PSCI_VERSION(0, 0); + for (i = 0; i < PSCI_FN_MAX; ++i) + CHOOSE_NVHE_SYM(kvm_host_psci_function_id)[i] = psci_get_function_id(i); +} + 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; + 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