xref: /openbmc/linux/arch/arm64/kvm/fpsimd.c (revision 3f868e142c0bb052a1c15fd3ceca1391604e2e69)
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 
17e6b673b7SDave Martin /*
18e6b673b7SDave Martin  * Called on entry to KVM_RUN unless this vcpu previously ran at least
19e6b673b7SDave Martin  * once and the most recent prior KVM_RUN for this vcpu was called from
20e6b673b7SDave Martin  * the same task as current (highly likely).
21e6b673b7SDave Martin  *
22e6b673b7SDave Martin  * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
23e6b673b7SDave Martin  * such that on entering hyp the relevant parts of current are already
24e6b673b7SDave Martin  * mapped.
25e6b673b7SDave Martin  */
26e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
27e6b673b7SDave Martin {
28e6b673b7SDave Martin 	int ret;
29e6b673b7SDave Martin 
30e6b673b7SDave Martin 	struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
31e6b673b7SDave Martin 
32bee14bcaSMarc Zyngier 	/* Make sure the host task fpsimd state is visible to hyp: */
33*3f868e14SQuentin Perret 	ret = kvm_share_hyp(fpsimd, fpsimd + 1);
342d761dbfSMarc Zyngier 	if (!ret)
35e6b673b7SDave Martin 		vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
362d761dbfSMarc Zyngier 
37e6b673b7SDave Martin 	return ret;
38e6b673b7SDave Martin }
39e6b673b7SDave Martin 
40e6b673b7SDave Martin /*
41e6b673b7SDave Martin  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
42e6b673b7SDave Martin  * The actual loading is done by the FPSIMD access trap taken to hyp.
43e6b673b7SDave Martin  *
44e6b673b7SDave Martin  * Here, we just set the correct metadata to indicate that the FPSIMD
45e6b673b7SDave Martin  * state in the cpu regs (if any) belongs to current on the host.
46e6b673b7SDave Martin  */
47e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
48e6b673b7SDave Martin {
49e6b673b7SDave Martin 	BUG_ON(!current->mm);
508383741aSMarc Zyngier 	BUG_ON(test_thread_flag(TIF_SVE));
51e6b673b7SDave Martin 
528383741aSMarc Zyngier 	vcpu->arch.flags &= ~KVM_ARM64_FP_ENABLED;
53e6b673b7SDave Martin 	vcpu->arch.flags |= KVM_ARM64_FP_HOST;
54b3eb56b6SDave Martin 
55b3eb56b6SDave Martin 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
56b3eb56b6SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
57e6b673b7SDave Martin }
58e6b673b7SDave Martin 
59af9a0e21SMarc Zyngier void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
60af9a0e21SMarc Zyngier {
61af9a0e21SMarc Zyngier 	if (test_thread_flag(TIF_FOREIGN_FPSTATE))
62af9a0e21SMarc Zyngier 		vcpu->arch.flags |= KVM_ARM64_FP_FOREIGN_FPSTATE;
63af9a0e21SMarc Zyngier 	else
64af9a0e21SMarc Zyngier 		vcpu->arch.flags &= ~KVM_ARM64_FP_FOREIGN_FPSTATE;
65af9a0e21SMarc Zyngier }
66af9a0e21SMarc Zyngier 
67e6b673b7SDave Martin /*
68e6b673b7SDave Martin  * If the guest FPSIMD state was loaded, update the host's context
69e6b673b7SDave Martin  * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
70e6b673b7SDave Martin  * so that they will be written back if the kernel clobbers them due to
71e6b673b7SDave Martin  * kernel-mode NEON before re-entry into the guest.
72e6b673b7SDave Martin  */
73e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
74e6b673b7SDave Martin {
75e6b673b7SDave Martin 	WARN_ON_ONCE(!irqs_disabled());
76e6b673b7SDave Martin 
77e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
78e47c2055SMarc Zyngier 		fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
79b43b5dd9SDave Martin 					 vcpu->arch.sve_state,
80b43b5dd9SDave Martin 					 vcpu->arch.sve_max_vl);
8104950674SDave Martin 
82e6b673b7SDave Martin 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
83b43b5dd9SDave Martin 		update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
84e6b673b7SDave Martin 	}
85e6b673b7SDave Martin }
86e6b673b7SDave Martin 
87e6b673b7SDave Martin /*
88e6b673b7SDave Martin  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
89e6b673b7SDave Martin  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
90e6b673b7SDave Martin  * disappears and another task or vcpu appears that recycles the same
91e6b673b7SDave Martin  * struct fpsimd_state.
92e6b673b7SDave Martin  */
93e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
94e6b673b7SDave Martin {
95b045e4d0SDave Martin 	unsigned long flags;
96b045e4d0SDave Martin 
97b045e4d0SDave Martin 	local_irq_save(flags);
98e6b673b7SDave Martin 
99e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
1008383741aSMarc Zyngier 		if (vcpu_has_sve(vcpu)) {
10183857371SMarc Zyngier 			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
102b145a843SMarc Zyngier 
1038c8010d6SMarc Zyngier 			/* Restore the VL that was saved when bound to the CPU */
1048c8010d6SMarc Zyngier 			if (!has_vhe())
1058c8010d6SMarc Zyngier 				sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
1068c8010d6SMarc Zyngier 						       SYS_ZCR_EL1);
1078c8010d6SMarc Zyngier 		}
1088c8010d6SMarc Zyngier 
109b145a843SMarc Zyngier 		fpsimd_save_and_flush_cpu_state();
1108383741aSMarc Zyngier 	} else if (has_vhe() && system_supports_sve()) {
111b3eb56b6SDave Martin 		/*
112b3eb56b6SDave Martin 		 * The FPSIMD/SVE state in the CPU has not been touched, and we
113b3eb56b6SDave Martin 		 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
114b3eb56b6SDave Martin 		 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
115b3eb56b6SDave Martin 		 * for EL0.  To avoid spurious traps, restore the trap state
116b3eb56b6SDave Martin 		 * seen by kvm_arch_vcpu_load_fp():
117b3eb56b6SDave Martin 		 */
118b3eb56b6SDave Martin 		if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
119b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
120b3eb56b6SDave Martin 		else
121b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
122e6b673b7SDave Martin 	}
123e6b673b7SDave Martin 
1248383741aSMarc Zyngier 	update_thread_flag(TIF_SVE, 0);
1252955bcc8SDave Martin 
126b045e4d0SDave Martin 	local_irq_restore(flags);
127e6b673b7SDave Martin }
128