Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1524458pxb; Wed, 4 Nov 2020 11:14:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJyuYHcU6/vOaCUdyBh+PmSHEvME7od45PH7eXlhrReZ4H4ocMWTpN66Bn/Fk92Gmcrhx8yU X-Received: by 2002:a17:906:a981:: with SMTP id jr1mr25203244ejb.99.1604517290339; Wed, 04 Nov 2020 11:14:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604517290; cv=none; d=google.com; s=arc-20160816; b=T+jYVanMJL2qTCvtNRHMKCi/M2eZILejKxjsB6tfAMKAHgWeOXgyvw5NQtWIm1I/In gBgYNcSWEffTAOBvLa4lmXlkS25WX+VJ4C2WqFutGXe6oPBzhccYnyceThmPKus624Jd Gd5pIz0e8T828fkTgkzBM6ziCX5wBdAKlA5AHwxYLQ6zT7BBfFvUx4duDruY98b4DtaL 13t/Y8ncP+ieaNP1qmLic4X1bNEpTtHV36seBCPPY4vvxHU9RWKoPNzzLw4QGuG5X2XY dpTwFiTk/AXH1fhq2UfMeZydcyvTihK38bU+10igG+xAgqUNuysX2hpuCtns2a+dMQ7W LZYQ== 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=k1mZbgF5Ouf6celHonuP3A/s1Eh7mvuRv0TPOHTEAQo=; b=VulGxuZfz65/Ck122tR32Nu548FKT6C0IfIezzgZ7p1PIWANEP9QbZkq/V3k927bdD Il4zuaMyl+YLgrDublbSUIvq48H8fXnOTjn40z2CDOS66KGOX6YdDs2wmiqD3qHtZ2LG LH2WwV5BrXiUKDzavbXdf7NeiHN6cYvgV6Jt77NqAZx58+A7QEhGgLMCNp9RfhhvsNFR GRLYLMdXzs+ZBfgvUQ+nBENWofENunzYXcFKH9R01fkoRozJr/C3DVGU9wVdd1CWePz2 0tNjo4FfIwHcUNdfEjkyk+74EtxtznItbrTeKTxrN3I4E8F8svpcAQUyeG1yOwZ55Ybb E0MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gEbk0tKi; 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 a6si2134852ejj.398.2020.11.04.11.14.25; Wed, 04 Nov 2020 11:14:50 -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=gEbk0tKi; 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 S1732564AbgKDShW (ORCPT + 99 others); Wed, 4 Nov 2020 13:37:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732530AbgKDShS (ORCPT ); Wed, 4 Nov 2020 13:37:18 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23579C0613D3 for ; Wed, 4 Nov 2020 10:37:18 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id k18so3266243wmj.5 for ; Wed, 04 Nov 2020 10:37:18 -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=k1mZbgF5Ouf6celHonuP3A/s1Eh7mvuRv0TPOHTEAQo=; b=gEbk0tKiTb2BPXDRISpv4WCdZLKXkbNLCjo9s0sAl7NmT5+hkNrkumyGOXB7xc/JYH YXXMWPL4izPCNAKWaVBjsFPxiTj85GP1FLZnDTuyUdZ7/PODY8ozmEI3bzsOGhIqrlPj PR5DFKv8a9eoIKBPgbZOXc7z6HwbThHepHAzSJ6MXbm/AvlBnQLNI1xKBJUgOs7R+/GI JpqrxNWZZk9x5mTbJY2i9dQ4QZtZ/ZxKVbnD2PT5EtwNYOfFK8lGQdFnx7W1IwaRFg7W n0BgSJehlQZsUSwbm1Db3EwQnJf2RK63eIxGuJPtxBov4BTrqTjJKFIyEAa0/J8LrmfX Z3qA== 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=k1mZbgF5Ouf6celHonuP3A/s1Eh7mvuRv0TPOHTEAQo=; b=JNvkw2Ma26jYnHIuFXXWTs3GETnAD8xFGAuS7kJwCmqOZhYRAcAKoRI7CVuhP8k5H4 8FrjWy1sxQypGe2IK07M4XqaOgdQuwv+ASNhUsdtyVakWlJVaIwq3BwX4Kcn/AVmr7ZH LShTWQktvfJOThnrgCkHnuZDt0cBPHrXcERqhJ0X/Dmx+1+uU/TtG3WEXu7Zv60pWMzW D7q1knL2iBFJFkctLrlbKn03OwR6Fo5RQHrMrDidyBYB8AWc0ycLq3XYLRfOMT9xtb+9 RF86xsB3Uapn7iTbwiZzolHSZLkeJ9INsTHmkUGDdgFEJRzRlEjMD4fLk3Fp3355mMBU kXnw== X-Gm-Message-State: AOAM533bdrYphk/Wn727hyPf/dP+iLW5KvexH10c2KOqvnBEd5I4Wswk W0Va1rWSLB6OFDjXDundHDpFmQ== X-Received: by 2002:a7b:cf1a:: with SMTP id l26mr6109935wmg.18.1604515036611; Wed, 04 Nov 2020 10:37:16 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:c8d2:30f2:53c6:bc2]) by smtp.gmail.com with ESMTPSA id e7sm4292303wrm.6.2020.11.04.10.37.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Nov 2020 10:37:15 -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 , kernel-team@android.com, David Brazdil Subject: [RFC PATCH 18/26] kvm: arm64: Intercept PSCI_CPU_OFF host SMC calls Date: Wed, 4 Nov 2020 18:36:22 +0000 Message-Id: <20201104183630.27513-19-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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a handler of the CPU_OFF PSCI host SMC trapped in KVM nVHE hyp code. When invoked, it changes the recorded state of the core to OFF before forwarding the call to EL3. If the call fails, it changes the state back to ON and returns the error to the host. Signed-off-by: David Brazdil --- arch/arm64/kvm/hyp/nvhe/psci.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c index c3d0a6246c66..00dc0cab860c 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci.c +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -13,6 +13,8 @@ #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]; @@ -20,6 +22,7 @@ s64 hyp_physvirt_offset; #define __hyp_pa(x) ((phys_addr_t)(x) + hyp_physvirt_offset) +static DEFINE_PER_CPU(hyp_spinlock_t, psci_cpu_lock); DEFINE_PER_CPU(enum kvm_nvhe_psci_state, psci_cpu_state); static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) @@ -76,9 +79,32 @@ static __noreturn unsigned long psci_forward_noreturn(struct kvm_cpu_context *ho hyp_panic(); /* unreachable */ } +static int psci_cpu_off(u64 func_id, struct kvm_cpu_context *host_ctxt) +{ + hyp_spinlock_t *cpu_lock = this_cpu_ptr(&psci_cpu_lock); + enum kvm_nvhe_psci_state *cpu_power = this_cpu_ptr(&psci_cpu_state); + u32 power_state = (u32)host_ctxt->regs.regs[1]; + int ret; + + /* Change the recorded state to OFF before forwarding the call. */ + hyp_spin_lock(cpu_lock); + *cpu_power = KVM_NVHE_PSCI_CPU_OFF; + hyp_spin_unlock(cpu_lock); + + ret = psci_call(func_id, power_state, 0, 0); + + /* Call was unsuccessful. Restore the recorded state and return to host. */ + hyp_spin_lock(cpu_lock); + *cpu_power = KVM_NVHE_PSCI_CPU_ON; + hyp_spin_unlock(cpu_lock); + return ret; +} + static unsigned long psci_0_1_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) { - if (func_id == kvm_host_psci_function_id[PSCI_FN_MIGRATE]) + if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_OFF]) + return psci_cpu_off(func_id, host_ctxt); + else if (func_id == kvm_host_psci_function_id[PSCI_FN_MIGRATE]) return psci_forward(host_ctxt); else return PSCI_RET_NOT_SUPPORTED; @@ -97,6 +123,8 @@ static unsigned long psci_0_2_handler(u64 func_id, struct kvm_cpu_context *host_ case PSCI_0_2_FN_SYSTEM_RESET: psci_forward_noreturn(host_ctxt); unreachable(); + case PSCI_0_2_FN_CPU_OFF: + return psci_cpu_off(func_id, host_ctxt); default: return PSCI_RET_NOT_SUPPORTED; } -- 2.29.1.341.ge80a0c044ae-goog