From patchwork Wed Apr 2 23:20:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 14036608 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1D5C0C36018 for ; Wed, 2 Apr 2025 23:52:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=bOtXHvr43L8fUw/Kk5YiMVxP+bSauiBwcbiOsJq5p8E=; b=KUSmJpE+TV3twyTtZ2ocqnE01n 8V/KfQW2Lyi0k7UVOja0HbWwJymDeKyJL6jTYmA3XlkY7ZqZy7URIsL6mCjjGz0Cn2zav2alYusSL CRA2pd+FHs+5VsN+XLVxrqWu7x+rwixSCzN4+5X1RCWXp7lnsF+hxwtm3HQIRCm6hNbmydnoBElOM Q9nf76zrC0/TyhxBZbrp60NwcDODe2kCUZ0h69tp3FHunulnkc+T6quM2FxlBU9b0SjhTLwLQ91Ge +2NlaCrfHpTjhIHxCmmWYMeacZRD6v6rdZwu7ZNAzBx9fjUUBHvIvPBC81C5WH15f9k/g4WetUizh 4v+WoEWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u07sg-00000007S0y-2OWI; Wed, 02 Apr 2025 23:52:26 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u07Ok-00000007ONX-1BEI for linux-arm-kernel@lists.infradead.org; Wed, 02 Apr 2025 23:21:32 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 757774448E; Wed, 2 Apr 2025 23:21:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31720C4CEE9; Wed, 2 Apr 2025 23:21:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743636089; bh=5LpLtUug8+WAmrpDqxb97Aqky7PzaY6BPA6LoGHVYvM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IrfWtS7V7xBo7dXjJXW9CxEXT7ymFBL89sGx+xR1yLSwRvkhQim+K2q+yDESKqwPC qaN/rgN9+DxZYMXLBj7ldYioRukpQorNAXO5g4ad3f4KJE7l4sLjAupD5egOX3n6O9 jVA4EDRnwRi158OHj1bAq9H39bpxeJyIx+IoiItkJS5XPSETemWyWkhYNOwZbnN8KR hBX3GCppItpiHDe6IKLX6E8Qv2Q+LOjyrwQxq+MJtreLYo5dfRwSdFMsRI2ibEfgA8 L8FnvHvwsqDDhXwzZcDETcTMlSv4pxpfuYObYZvlwAkzVr8T1TdFlwUMkjSLFivRl7 EPpkqupC85tHg== From: Mark Brown Date: Thu, 03 Apr 2025 00:20:24 +0100 Subject: [PATCH 5.15 v2 09/10] KVM: arm64: Calculate cptr_el2 traps on activating traps MIME-Version: 1.0 Message-Id: <20250403-stable-sve-5-15-v2-9-30a36a78a20a@kernel.org> References: <20250403-stable-sve-5-15-v2-0-30a36a78a20a@kernel.org> In-Reply-To: <20250403-stable-sve-5-15-v2-0-30a36a78a20a@kernel.org> To: Greg Kroah-Hartman , Marc Zyngier , James Morse , Suzuki K Poulose , Catalin Marinas , Will Deacon , Oleg Nesterov , Oliver Upton Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Mark Brown , Fuad Tabba , James Clark X-Mailer: b4 0.15-dev-c25d1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3292; i=broonie@kernel.org; h=from:subject:message-id; bh=dzuSkTHzORViq39mf97JEZt+XWlNRsFaxOQ4IWqXP1g=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBn7cZSEGnI/+KQv94AbW1N2k2iU5ke26uRK0ZyfN0W vH5p9Z2JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZ+3GUgAKCRAk1otyXVSH0AI3CA CDuKg/xk7+y9B/DfNZ4jUmI7nU3DfpaVl5OfpWEXhMa3U9Xnvk4EJBR1wPg65gT9fc3fEbK2drIuSL 1s4MqfylIEjvDrwI63TKdGy1QLNfeP6SvujtNjlty3yz3DWll9+q2UpS52Cn0teY7KHCwHrLcoR9Ti cz0GDop16NGzGLtq/eWSPbQWjKWxhW8VHzhF6wWaDN6h9Mf/qnsyG4wnTEK7E/EVW2flOIeaE1lfbG Cn8wQNktCJnN38ew7iA0606NIvmoRxupuRB/+9c4uYuzYr3zLUVsHsXGf4ZCk1gYw5VcZzgEVAT4Hu +3zD9R0M9kiG/PIFzOmmInMf21wsc9 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250402_162130_354396_D1006B37 X-CRM114-Status: GOOD ( 16.25 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Fuad Tabba [ Upstream commit 2fd5b4b0e7b440602455b79977bfa64dea101e6c ] Similar to VHE, calculate the value of cptr_el2 from scratch on activate traps. This removes the need to store cptr_el2 in every vcpu structure. Moreover, some traps, such as whether the guest owns the fp registers, need to be set on every vcpu run. Reported-by: James Clark Fixes: 5294afdbf45a ("KVM: arm64: Exclude FP ownership from kvm_vcpu_arch") Signed-off-by: Fuad Tabba Link: https://lore.kernel.org/r/20241216105057.579031-13-tabba@google.com Signed-off-by: Marc Zyngier Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_host.h | 1 - arch/arm64/kvm/arm.c | 1 - arch/arm64/kvm/hyp/nvhe/switch.c | 35 ++++++++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 3d4e2396a2d7..2e0952134e2e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -301,7 +301,6 @@ struct kvm_vcpu_arch { /* Values of trap registers for the guest. */ u64 hcr_el2; u64 mdcr_el2; - u64 cptr_el2; /* Values of trap registers for the host before guest entry. */ u64 mdcr_el2_host; diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 9ded5443de48..5ca8782edb96 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1138,7 +1138,6 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, } vcpu_reset_hcr(vcpu); - vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT; /* * Handle the "start in power-off" case. diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c index 4db5409f40c4..c0885197f2a5 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -34,21 +34,38 @@ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); DEFINE_PER_CPU(unsigned long, kvm_hyp_vector); -static void __activate_traps(struct kvm_vcpu *vcpu) +static bool guest_owns_fp_regs(struct kvm_vcpu *vcpu) { - u64 val; + return vcpu->arch.flags & KVM_ARM64_FP_ENABLED; +} - ___activate_traps(vcpu); - __activate_traps_common(vcpu); +static void __activate_cptr_traps(struct kvm_vcpu *vcpu) +{ + u64 val = CPTR_EL2_TAM; /* Same bit irrespective of E2H */ - val = vcpu->arch.cptr_el2; - val |= CPTR_EL2_TTA | CPTR_EL2_TAM; - if (!update_fp_enabled(vcpu)) { - val |= CPTR_EL2_TFP | CPTR_EL2_TZ; - __activate_traps_fpsimd32(vcpu); + /* !hVHE case upstream */ + if (1) { + val |= CPTR_EL2_TTA | CPTR_NVHE_EL2_RES1; + + if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs(vcpu)) + val |= CPTR_EL2_TZ; + + if (!guest_owns_fp_regs(vcpu)) + val |= CPTR_EL2_TFP; } + if (!guest_owns_fp_regs(vcpu)) + __activate_traps_fpsimd32(vcpu); + write_sysreg(val, cptr_el2); +} + +static void __activate_traps(struct kvm_vcpu *vcpu) +{ + ___activate_traps(vcpu); + __activate_traps_common(vcpu); + __activate_cptr_traps(vcpu); + write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2); if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {