xref: /openbmc/linux/arch/arm64/kvm/fpsimd.c (revision e9ada6c208c15c907afe5afb1aa82e23e81eb8ba)
1e6b673b7SDave Martin // SPDX-License-Identifier: GPL-2.0
2e6b673b7SDave Martin /*
3e6b673b7SDave Martin  * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
4e6b673b7SDave Martin  *
5e6b673b7SDave Martin  * Copyright 2018 Arm Limited
6e6b673b7SDave Martin  * Author: Dave Martin <Dave.Martin@arm.com>
7e6b673b7SDave Martin  */
8b045e4d0SDave Martin #include <linux/irqflags.h>
9e6b673b7SDave Martin #include <linux/sched.h>
10e6b673b7SDave Martin #include <linux/kvm_host.h>
1104950674SDave Martin #include <asm/fpsimd.h>
12e6b673b7SDave Martin #include <asm/kvm_asm.h>
1383857371SMarc Zyngier #include <asm/kvm_hyp.h>
14e6b673b7SDave Martin #include <asm/kvm_mmu.h>
15b3eb56b6SDave Martin #include <asm/sysreg.h>
16e6b673b7SDave Martin 
1752b28657SQuentin Perret void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
1852b28657SQuentin Perret {
1952b28657SQuentin Perret 	struct task_struct *p = vcpu->arch.parent_task;
2052b28657SQuentin Perret 	struct user_fpsimd_state *fpsimd;
2152b28657SQuentin Perret 
2252b28657SQuentin Perret 	if (!is_protected_kvm_enabled() || !p)
2352b28657SQuentin Perret 		return;
2452b28657SQuentin Perret 
2552b28657SQuentin Perret 	fpsimd = &p->thread.uw.fpsimd_state;
2652b28657SQuentin Perret 	kvm_unshare_hyp(fpsimd, fpsimd + 1);
2752b28657SQuentin Perret 	put_task_struct(p);
2852b28657SQuentin Perret }
2952b28657SQuentin Perret 
30e6b673b7SDave Martin /*
31e6b673b7SDave Martin  * Called on entry to KVM_RUN unless this vcpu previously ran at least
32e6b673b7SDave Martin  * once and the most recent prior KVM_RUN for this vcpu was called from
33e6b673b7SDave Martin  * the same task as current (highly likely).
34e6b673b7SDave Martin  *
35e6b673b7SDave Martin  * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
36e6b673b7SDave Martin  * such that on entering hyp the relevant parts of current are already
37e6b673b7SDave Martin  * mapped.
38e6b673b7SDave Martin  */
39e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
40e6b673b7SDave Martin {
41e6b673b7SDave Martin 	int ret;
42e6b673b7SDave Martin 
43e6b673b7SDave Martin 	struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
44e6b673b7SDave Martin 
4552b28657SQuentin Perret 	kvm_vcpu_unshare_task_fp(vcpu);
4652b28657SQuentin Perret 
47bee14bcaSMarc Zyngier 	/* Make sure the host task fpsimd state is visible to hyp: */
483f868e14SQuentin Perret 	ret = kvm_share_hyp(fpsimd, fpsimd + 1);
4952b28657SQuentin Perret 	if (ret)
5052b28657SQuentin Perret 		return ret;
5152b28657SQuentin Perret 
52e6b673b7SDave Martin 	vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
532d761dbfSMarc Zyngier 
5452b28657SQuentin Perret 	/*
5552b28657SQuentin Perret 	 * We need to keep current's task_struct pinned until its data has been
5652b28657SQuentin Perret 	 * unshared with the hypervisor to make sure it is not re-used by the
5752b28657SQuentin Perret 	 * kernel and donated to someone else while already shared -- see
5852b28657SQuentin Perret 	 * kvm_vcpu_unshare_task_fp() for the matching put_task_struct().
5952b28657SQuentin Perret 	 */
6052b28657SQuentin Perret 	if (is_protected_kvm_enabled()) {
6152b28657SQuentin Perret 		get_task_struct(current);
6252b28657SQuentin Perret 		vcpu->arch.parent_task = current;
6352b28657SQuentin Perret 	}
6452b28657SQuentin Perret 
6552b28657SQuentin Perret 	return 0;
66e6b673b7SDave Martin }
67e6b673b7SDave Martin 
68e6b673b7SDave Martin /*
69e6b673b7SDave Martin  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
70e6b673b7SDave Martin  * The actual loading is done by the FPSIMD access trap taken to hyp.
71e6b673b7SDave Martin  *
72e6b673b7SDave Martin  * Here, we just set the correct metadata to indicate that the FPSIMD
73e6b673b7SDave Martin  * state in the cpu regs (if any) belongs to current on the host.
74e6b673b7SDave Martin  */
75e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
76e6b673b7SDave Martin {
77e6b673b7SDave Martin 	BUG_ON(!current->mm);
788383741aSMarc Zyngier 	BUG_ON(test_thread_flag(TIF_SVE));
79e6b673b7SDave Martin 
808383741aSMarc Zyngier 	vcpu->arch.flags &= ~KVM_ARM64_FP_ENABLED;
81e6b673b7SDave Martin 	vcpu->arch.flags |= KVM_ARM64_FP_HOST;
82b3eb56b6SDave Martin 
83d52d165dSMarc Zyngier 	vcpu->arch.flags &= ~KVM_ARM64_HOST_SVE_ENABLED;
84b3eb56b6SDave Martin 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
85b3eb56b6SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
86861262abSMark Brown 
87861262abSMark Brown 	/*
88861262abSMark Brown 	 * We don't currently support SME guests but if we leave
89861262abSMark Brown 	 * things in streaming mode then when the guest starts running
90861262abSMark Brown 	 * FPSIMD or SVE code it may generate SME traps so as a
91861262abSMark Brown 	 * special case if we are in streaming mode we force the host
92861262abSMark Brown 	 * state to be saved now and exit streaming mode so that we
93861262abSMark Brown 	 * don't have to handle any SME traps for valid guest
94861262abSMark Brown 	 * operations. Do this for ZA as well for now for simplicity.
95861262abSMark Brown 	 */
96861262abSMark Brown 	if (system_supports_sme()) {
97039f49c4SMarc Zyngier 		vcpu->arch.flags &= ~KVM_ARM64_HOST_SME_ENABLED;
98861262abSMark Brown 		if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
99861262abSMark Brown 			vcpu->arch.flags |= KVM_ARM64_HOST_SME_ENABLED;
100861262abSMark Brown 
101ec0067a6SMark Brown 		if (read_sysreg_s(SYS_SVCR) &
102ec0067a6SMark Brown 		    (SVCR_SM_MASK | SVCR_ZA_MASK)) {
103861262abSMark Brown 			vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
104861262abSMark Brown 			fpsimd_save_and_flush_cpu_state();
105861262abSMark Brown 		}
106861262abSMark Brown 	}
107e6b673b7SDave Martin }
108e6b673b7SDave Martin 
10923afc825SMark Brown /*
110*e9ada6c2SMarc Zyngier  * Called just before entering the guest once we are no longer preemptable
111*e9ada6c2SMarc Zyngier  * and interrupts are disabled. If we have managed to run anything using
112*e9ada6c2SMarc Zyngier  * FP while we were preemptible (such as off the back of an interrupt),
113*e9ada6c2SMarc Zyngier  * then neither the host nor the guest own the FP hardware (and it was the
114*e9ada6c2SMarc Zyngier  * responsibility of the code that used FP to save the existing state).
115*e9ada6c2SMarc Zyngier  *
116*e9ada6c2SMarc Zyngier  * Note that not supporting FP is basically the same thing as far as the
117*e9ada6c2SMarc Zyngier  * hypervisor is concerned (nothing to save).
11823afc825SMark Brown  */
119af9a0e21SMarc Zyngier void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
120af9a0e21SMarc Zyngier {
121*e9ada6c2SMarc Zyngier 	if (!system_supports_fpsimd() || test_thread_flag(TIF_FOREIGN_FPSTATE))
122*e9ada6c2SMarc Zyngier 		vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED | KVM_ARM64_FP_HOST);
123af9a0e21SMarc Zyngier }
124af9a0e21SMarc Zyngier 
125e6b673b7SDave Martin /*
12623afc825SMark Brown  * Called just after exiting the guest. If the guest FPSIMD state
12723afc825SMark Brown  * was loaded, update the host's context tracking data mark the CPU
12823afc825SMark Brown  * FPSIMD regs as dirty and belonging to vcpu so that they will be
12923afc825SMark Brown  * written back if the kernel clobbers them due to kernel-mode NEON
13023afc825SMark Brown  * before re-entry into the guest.
131e6b673b7SDave Martin  */
132e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
133e6b673b7SDave Martin {
134e6b673b7SDave Martin 	WARN_ON_ONCE(!irqs_disabled());
135e6b673b7SDave Martin 
136e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
137b40c559bSMark Brown 		/*
138b40c559bSMark Brown 		 * Currently we do not support SME guests so SVCR is
139b40c559bSMark Brown 		 * always 0 and we just need a variable to point to.
140b40c559bSMark Brown 		 */
141e47c2055SMarc Zyngier 		fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
142b43b5dd9SDave Martin 					 vcpu->arch.sve_state,
143b40c559bSMark Brown 					 vcpu->arch.sve_max_vl,
1440033cd93SMark Brown 					 NULL, 0, &vcpu->arch.svcr);
14504950674SDave Martin 
146e6b673b7SDave Martin 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
147b43b5dd9SDave Martin 		update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
148e6b673b7SDave Martin 	}
149e6b673b7SDave Martin }
150e6b673b7SDave Martin 
151e6b673b7SDave Martin /*
152e6b673b7SDave Martin  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
153e6b673b7SDave Martin  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
154e6b673b7SDave Martin  * disappears and another task or vcpu appears that recycles the same
155e6b673b7SDave Martin  * struct fpsimd_state.
156e6b673b7SDave Martin  */
157e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
158e6b673b7SDave Martin {
159b045e4d0SDave Martin 	unsigned long flags;
160b045e4d0SDave Martin 
161b045e4d0SDave Martin 	local_irq_save(flags);
162e6b673b7SDave Martin 
163861262abSMark Brown 	/*
164861262abSMark Brown 	 * If we have VHE then the Hyp code will reset CPACR_EL1 to
165861262abSMark Brown 	 * CPACR_EL1_DEFAULT and we need to reenable SME.
166861262abSMark Brown 	 */
167861262abSMark Brown 	if (has_vhe() && system_supports_sme()) {
168861262abSMark Brown 		/* Also restore EL0 state seen on entry */
169861262abSMark Brown 		if (vcpu->arch.flags & KVM_ARM64_HOST_SME_ENABLED)
170861262abSMark Brown 			sysreg_clear_set(CPACR_EL1, 0,
171861262abSMark Brown 					 CPACR_EL1_SMEN_EL0EN |
172861262abSMark Brown 					 CPACR_EL1_SMEN_EL1EN);
173861262abSMark Brown 		else
174861262abSMark Brown 			sysreg_clear_set(CPACR_EL1,
175861262abSMark Brown 					 CPACR_EL1_SMEN_EL0EN,
176861262abSMark Brown 					 CPACR_EL1_SMEN_EL1EN);
177861262abSMark Brown 	}
178861262abSMark Brown 
179e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
1808383741aSMarc Zyngier 		if (vcpu_has_sve(vcpu)) {
18183857371SMarc Zyngier 			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
182b145a843SMarc Zyngier 
1838c8010d6SMarc Zyngier 			/* Restore the VL that was saved when bound to the CPU */
1848c8010d6SMarc Zyngier 			if (!has_vhe())
1858c8010d6SMarc Zyngier 				sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
1868c8010d6SMarc Zyngier 						       SYS_ZCR_EL1);
1878c8010d6SMarc Zyngier 		}
1888c8010d6SMarc Zyngier 
189b145a843SMarc Zyngier 		fpsimd_save_and_flush_cpu_state();
1908383741aSMarc Zyngier 	} else if (has_vhe() && system_supports_sve()) {
191b3eb56b6SDave Martin 		/*
192b3eb56b6SDave Martin 		 * The FPSIMD/SVE state in the CPU has not been touched, and we
193b3eb56b6SDave Martin 		 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
194b3eb56b6SDave Martin 		 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
195b3eb56b6SDave Martin 		 * for EL0.  To avoid spurious traps, restore the trap state
196b3eb56b6SDave Martin 		 * seen by kvm_arch_vcpu_load_fp():
197b3eb56b6SDave Martin 		 */
198b3eb56b6SDave Martin 		if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
199b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
200b3eb56b6SDave Martin 		else
201b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
202e6b673b7SDave Martin 	}
203e6b673b7SDave Martin 
2048383741aSMarc Zyngier 	update_thread_flag(TIF_SVE, 0);
2052955bcc8SDave Martin 
206b045e4d0SDave Martin 	local_irq_restore(flags);
207e6b673b7SDave Martin }
208