From patchwork Wed Nov 4 18:36:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11881893 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3797C2D0A3 for ; Wed, 4 Nov 2020 18:40:10 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4EA132067C for ; Wed, 4 Nov 2020 18:40:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="iZJm9a8I"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="bnzTyIjl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4EA132067C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.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=7+esqcG9wikNUvSouj0GduOu890fsCrDkBKkF3wZJcg=; b=iZJm9a8ILnyP8WGy/F4VNbMgV J5GDVTWNKIi2oVH3nGcU5Z7a9WqhIsACsivI8ol37pQueXKpI9TOwa3e4TqI6zuoSRtD4LIZbTCVR eVAf1CFGgiRZFAtGtcwJNfa1nrmI0E+9LuJ++huz8Z0R1KR/wOr1htWXwpx7aGIYS/XThULBPdQpj xDm/eH458ewTZQ5gcrZ+EL88FjHwZmYRoEnDm4FX800hPs64szxU0NogxiP8FrLlIPVFPdbCj9vPf wTIxQAhqve/2+eWJJIJvukqPlqJmu0P1jpDaK2jHIQ9uIUiUSYdv/40P/G/GaF/ZEAMvYmN/TAXFC ojz5Sv96g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNfi-0002KA-1A; Wed, 04 Nov 2020 18:38:14 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNed-0001mX-2h for linux-arm-kernel@lists.infradead.org; Wed, 04 Nov 2020 18:37:08 +0000 Received: by mail-wm1-x344.google.com with SMTP id c18so3358610wme.2 for ; Wed, 04 Nov 2020 10:37:07 -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=B3d+QzOWawlq1O6qWW0gNBz2FTnwOmMHRq4HC+NVGn4=; b=bnzTyIjlyJiHyVe/d6fBeFTtZRk669wctKitVdZxL5eKRvHxekGaYaGcRkjMCnl+DJ NjrSTTKOO9SW5Up/qmMYqUnzUmpQTjhj/sMMMz8AhJTotYOXxVNy93wWdmv+JUht97Ao HNiPunbAncmDK5pB2s19mi4k1gFA/znMfI+oF77UIpwdETFox1QsQrbACiN6Wf3osi+v tzt2Pv3lRAYXItcD5VHXEh88CFzwhr65Oex5GMhkAIEzdLdD8yqxL6kdqk6I1wGuNgt5 VXhuHizaJ1OH4LXIlE74pCuUSg4sl7s20qWpBYUEqQTHorjOPMWW2ioRSnfrkL4Sj5WB na9Q== 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=B3d+QzOWawlq1O6qWW0gNBz2FTnwOmMHRq4HC+NVGn4=; b=dGWmMLCEIh4sW1u5hxgjfBO2rlwSfgmbxIMBCtwmCqeUtkj/vSmelnkke3vLtlP7aX KokMHAA7hVt8XBljdRlbzswD754QAiXevxsBOmCL9pfKcjEZwnqdgSyKI0z6oiAxQ6Bk /RhfS88kRcD6F43IgJyLGWwQgdTQE3OySir+WadBEoLuNdrWWoelTP7sVoH1B9215cKg EVgxRC/qp2QtxDJxXydjdGOND70IiihYXUNeupYjsLDvDbp2NRoZoNXipCi9TBM0J+gs RKexMAOhvREO9FUIWf5lJi3ND7rrlM9jFpnvw3ccpQA86qnC9VOoRj0UyDGQg09DTaqy JUGg== X-Gm-Message-State: AOAM532HCzjZndhJZm1KE3wCU77GbuHWnNGIw6e4TJr0pEqDmvHTKiKt x2aeEHQlPMlTkbOakM5XZ7UvXw== X-Google-Smtp-Source: ABdhPJxVS5jWFGrlmaTy+L20ANnS0eCOXwmlYOy6SIM8Ix3vFBWONSxQVU6NleEARPZn0hYfOxSTpw== X-Received: by 2002:a05:600c:22d7:: with SMTP id 23mr6155656wmg.67.1604515025991; Wed, 04 Nov 2020 10:37:05 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:c8d2:30f2:53c6:bc2]) by smtp.gmail.com with ESMTPSA id v6sm3660726wrs.39.2020.11.04.10.37.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Nov 2020 10:37:05 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 13/26] kvm: arm64: Bootstrap PSCI SMC handler in nVHE EL2 Date: Wed, 4 Nov 2020 18:36:17 +0000 Message-Id: <20201104183630.27513-14-dbrazdil@google.com> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201104183630.27513-1-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201104_133707_173688_7536A041 X-CRM114-Status: GOOD ( 27.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Lorenzo Pieralisi , kernel-team@android.com, Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 | 12 ++++ arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++ arch/arm64/kvm/hyp/nvhe/psci.c | 102 +++++++++++++++++++++++++++++ include/uapi/linux/psci.h | 1 + 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/hyp/nvhe/psci.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 ff200fc8d653..cedec793da64 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 @@ -1498,6 +1499,16 @@ static void init_cpu_logical_map(void) CHOOSE_NVHE_SYM(__cpu_logical_map)[cpu] = cpu_logical_map(cpu); } +static void init_psci(void) +{ + extern u32 kvm_nvhe_sym(kvm_host_psci_version); + extern u32 kvm_nvhe_sym(kvm_host_psci_function_id)[PSCI_FN_MAX]; + + kvm_nvhe_sym(kvm_host_psci_version) = psci_driver_version; + memcpy(kvm_nvhe_sym(kvm_host_psci_function_id), + psci_function_id, sizeof(psci_function_id)); +} + static int init_common_resources(void) { return kvm_set_ipa_limit(); @@ -1676,6 +1687,7 @@ static int init_hyp_mode(void) } init_cpu_logical_map(); + init_psci(); return 0; diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index c45f440cce51..647b63337a51 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 percpu.o + hyp-main.o percpu.o psci.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 fffc2dc09a1f..aa54db514550 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -134,6 +134,10 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt) */ skip_host_instruction(); + /* Try to handle host's PSCI SMCs. */ + if (kvm_host_psci_handler(host_ctxt)) + return; + /* Forward SMC not handled in EL2 to EL3. */ forward_host_smc(host_ctxt); } diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c new file mode 100644 index 000000000000..82d3b2c89658 --- /dev/null +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -0,0 +1,102 @@ +// 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 kvm_host_psci_version = PSCI_VERSION(0, 0); +u32 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_fn_call(u64 func_id) +{ + u64 base = func_id & ~PSCI_0_2_FN_ID_MASK; + + return base == PSCI_0_2_FN_BASE || base == PSCI_0_2_FN64_BASE; +} + +static bool is_psci_call(u64 func_id) +{ + if (kvm_host_psci_version == PSCI_VERSION(0, 0)) + return false; + else if (kvm_host_psci_version == PSCI_VERSION(0, 1)) + return is_psci_0_1_call(func_id); + else + return is_psci_0_2_fn_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) +{ + int ret; + + ret = psci_0_2_handler(func_id, host_ctxt); + if (ret != PSCI_RET_NOT_SUPPORTED) + return ret; + + switch (func_id) { + default: + return PSCI_RET_NOT_SUPPORTED; + } +} + +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; + + if (kvm_host_psci_version == PSCI_VERSION(0, 1)) + ret = psci_0_1_handler(func_id, host_ctxt); + else if (kvm_host_psci_version == PSCI_VERSION(0, 2)) + ret = psci_0_2_handler(func_id, host_ctxt); + else if (PSCI_VERSION_MAJOR(kvm_host_psci_version) >= 1) + ret = psci_1_0_handler(func_id, host_ctxt); + else + ret = PSCI_RET_NOT_SUPPORTED; + + 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; +} diff --git a/include/uapi/linux/psci.h b/include/uapi/linux/psci.h index 2fcad1dd0b0e..0d52b8dbe8c2 100644 --- a/include/uapi/linux/psci.h +++ b/include/uapi/linux/psci.h @@ -29,6 +29,7 @@ #define PSCI_0_2_FN64_BASE \ (PSCI_0_2_FN_BASE + PSCI_0_2_64BIT) #define PSCI_0_2_FN64(n) (PSCI_0_2_FN64_BASE + (n)) +#define PSCI_0_2_FN_ID_MASK 0xffff #define PSCI_0_2_FN_PSCI_VERSION PSCI_0_2_FN(0) #define PSCI_0_2_FN_CPU_SUSPEND PSCI_0_2_FN(1)