From nobody Fri Dec 19 20:06:36 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B7AE154C08; Fri, 14 Feb 2025 02:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739498537; cv=none; b=DL4ezlOlDwWH6Ua7dLMuT8tFXSsjHireDTBBiO4yErnoBpJzKYQTiYzHs3lWzzs+HcJiV22Ni/3lIYZ/L2tkY76AY8J3IsME2yY06j+EXthGAuG58aG3H/B3kBazrk5WDr6izbBXSmRv0TrbpMgMX9/wvxVYEAO9QaHjZEPLCGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739498537; c=relaxed/simple; bh=qJoUCI/piVewnKDz2NbQYZx9SqUOrxIeyc4/FfZsD2w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZXGpHpID19BmPFJcPhn43QorwH7Ln9QulEmGUDbNSt9suuOhP2LH01FH4AxcU7aA52lZcMC7DA0LqUxeirH/Y1CZx5uX6PDMAEMR/nNtjmAVLqnoqdn86DEu91nrCZYFPwBX34OVsKdPfcFjVwE854aibQttOk0Y9yY/0j14/As= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZB8CfTpR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZB8CfTpR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AE01C4CEE2; Fri, 14 Feb 2025 02:02:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739498535; bh=qJoUCI/piVewnKDz2NbQYZx9SqUOrxIeyc4/FfZsD2w=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZB8CfTpRWiYxxCrhL39z/xQ38EE+jDN008Sog4jTU2G8ZycnrofPb15OcsVErFcs0 3LzD5vnTGOkKoUyVS464XBeD7/AxYabp2CmvMeH026B5+XoGTTqZni63DbgU9XB2FN yRhg44vMHtlj0jTW7j02+aW0yXIWSUKWZycqR7pZ399fMq3HsQRb14rtZO2KAGAH6K Fj9D288HvzQTrPDCUO0UwGOxoxrRtD1WFHy5r6osmAblcFjLysQm84rE7bndDnLxNT OFH5jgQYXpGqA9obD7XTnlNHkKO0j59lEWtP9wO22qx4gVlrQd4fDhW5iWEkZhMyX0 YHpyzpC1Jeolg== From: Mark Brown Date: Fri, 14 Feb 2025 01:58:03 +0000 Subject: [PATCH v4 20/27] KVM: arm64: Support userspace access to streaming mode Z and P registers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250214-kvm-arm64-sme-v4-20-d64a681adcc2@kernel.org> References: <20250214-kvm-arm64-sme-v4-0-d64a681adcc2@kernel.org> In-Reply-To: <20250214-kvm-arm64-sme-v4-0-d64a681adcc2@kernel.org> To: Marc Zyngier , Oliver Upton , Joey Gouly , Catalin Marinas , Suzuki K Poulose , Will Deacon , Paolo Bonzini , Jonathan Corbet , Shuah Khan Cc: Dave Martin , Fuad Tabba , Mark Rutland , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Mark Brown X-Mailer: b4 0.15-dev-1b0d6 X-Developer-Signature: v=1; a=openpgp-sha256; l=4700; i=broonie@kernel.org; h=from:subject:message-id; bh=qJoUCI/piVewnKDz2NbQYZx9SqUOrxIeyc4/FfZsD2w=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnrqPQVluPUgR4/8xMUi5D+iwEu3mCzO/0306nk2LS pfO/5nSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZ66j0AAKCRAk1otyXVSH0KM5B/ 936FqN9s6ZPvcmAa1Q0hAWjtBvrBLxlnLr0wQlC5Wl9zTDmnHcXMiEYm+x8MupV6PRjhbYSgL/l/E2 d7OOIPMMduHLij74JxAEUVWWUn4EIWtngkbRt82mTDQhaS6YwSuwtaIUK3njOU07EmRyEee8ZuSXCK 5jfTdB43azT5V7JikQnT/+4IFQZ2MJknnEYvlyhLyj2tiE2n4Dct9JdU05taKbYoUAzUQUdgA1ZUAz 9jcYbB+Jko/ITjBc9jyQtZNBc7+76aWhMyu4sf5merfLcbSLaKkZigYX0horu3gvEXYb8vgq83eHqF xZ9PYqpRutOA4/bMTrECU1qi3/Dh1P X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB SME introduces a mode called streaming mode where the Z, P and optionally FFR registers can be accessed using the SVE instructions but with the SME vector length. Reflect this in the ABI for accessing the guest registers by making the vector length for the vcpu reflect the vector length that would be seen by the guest were it running, using the SME vector length when the guest is configured for streaming mode. Since SME may be present without SVE we also update the existing checks for access to the Z, P and V registers to check for either SVE or streaming mode. When not in streaming mode the guest floating point state may be accessed via the V registers. Any VMM that supports SME must be aware of the need to configure streaming mode prior to writing the floating point registers that this creates. Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/guest.c | 38 ++++++++++++++++++++++++++++++++++-= --- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index acd243a31ccb77cce3d95829e22456a71e87725c..67ac3e37fde31901513c2e69269= 3d3ba1967b0bd 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1600,4 +1600,7 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 = val); #define vcpu_has_fa64(vcpu) kvm_has_fa64((vcpu)->kvm) #endif =20 +#define vcpu_in_streaming_mode(vcpu) \ + (__vcpu_sys_reg(vcpu, SVCR) & SVCR_SM_MASK) + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 62df7830d11e6e7d01d175566e4b6a771ac2ec6d..87a0a027e1e7e2487eff703dee2= 958137c9640ef 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -73,6 +73,11 @@ static u64 core_reg_offset_from_id(u64 id) return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE); } =20 +static bool vcpu_has_sve_regs(const struct kvm_vcpu *vcpu) +{ + return vcpu_has_sve(vcpu) || vcpu_in_streaming_mode(vcpu); +} + static int core_reg_size_from_offset(const struct kvm_vcpu *vcpu, u64 off) { int size; @@ -110,9 +115,10 @@ static int core_reg_size_from_offset(const struct kvm_= vcpu *vcpu, u64 off) /* * The KVM_REG_ARM64_SVE regs must be used instead of * KVM_REG_ARM_CORE for accessing the FPSIMD V-registers on - * SVE-enabled vcpus: + * SVE-enabled vcpus or when a SME enabled vcpu is in + * streaming mode: */ - if (vcpu_has_sve(vcpu) && core_reg_offset_is_vreg(off)) + if (vcpu_has_sve_regs(vcpu) && core_reg_offset_is_vreg(off)) return -EINVAL; =20 return size; @@ -426,6 +432,24 @@ struct vec_state_reg_region { unsigned int upad; /* extra trailing padding in user memory */ }; =20 +/* + * We represent the Z and P registers to userspace using either the + * SVE or SME vector length, depending on which features the guest has + * and if the guest is in streaming mode. + */ +static unsigned int vcpu_sve_cur_vq(struct kvm_vcpu *vcpu) +{ + unsigned int vq =3D 0; + + if (vcpu_has_sve(vcpu)) + vq =3D vcpu_sve_max_vq(vcpu); + + if (vcpu_in_streaming_mode(vcpu)) + vq =3D vcpu_sme_max_vq(vcpu); + + return vq; +} + /* * Validate SVE register ID and get sanitised bounds for user/kernel SVE * register copy @@ -466,7 +490,7 @@ static int sve_reg_to_region(struct vec_state_reg_regio= n *region, if (!vcpu_has_sve(vcpu) || (reg->id & SVE_REG_SLICE_MASK) > 0) return -ENOENT; =20 - vq =3D vcpu_sve_max_vq(vcpu); + vq =3D vcpu_sve_cur_vq(vcpu); =20 reqoffset =3D SVE_SIG_ZREG_OFFSET(vq, reg_num) - SVE_SIG_REGS_OFFSET; @@ -476,7 +500,7 @@ static int sve_reg_to_region(struct vec_state_reg_regio= n *region, if (!vcpu_has_sve(vcpu) || (reg->id & SVE_REG_SLICE_MASK) > 0) return -ENOENT; =20 - vq =3D vcpu_sve_max_vq(vcpu); + vq =3D vcpu_sve_cur_vq(vcpu); =20 reqoffset =3D SVE_SIG_PREG_OFFSET(vq, reg_num) - SVE_SIG_REGS_OFFSET; @@ -515,6 +539,9 @@ static int get_sve_reg(struct kvm_vcpu *vcpu, const str= uct kvm_one_reg *reg) if (!kvm_arm_vcpu_vec_finalized(vcpu)) return -EPERM; =20 + if (!vcpu_has_sve_regs(vcpu)) + return -EBUSY; + if (copy_to_user(uptr, vcpu->arch.sve_state + region.koffset, region.klen) || clear_user(uptr + region.klen, region.upad)) @@ -541,6 +568,9 @@ static int set_sve_reg(struct kvm_vcpu *vcpu, const str= uct kvm_one_reg *reg) if (!kvm_arm_vcpu_vec_finalized(vcpu)) return -EPERM; =20 + if (!vcpu_has_sve_regs(vcpu)) + return -EBUSY; + if (copy_from_user(vcpu->arch.sve_state + region.koffset, uptr, region.klen)) return -EFAULT; --=20 2.39.5