xref: /openbmc/linux/arch/arm64/kvm/fpsimd.c (revision 83857371d4cbeff8551fa770e045be9c6b04715c)
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/thread_info.h>
11e6b673b7SDave Martin #include <linux/kvm_host.h>
1204950674SDave Martin #include <asm/fpsimd.h>
13e6b673b7SDave Martin #include <asm/kvm_asm.h>
14*83857371SMarc Zyngier #include <asm/kvm_hyp.h>
15e6b673b7SDave Martin #include <asm/kvm_mmu.h>
16b3eb56b6SDave Martin #include <asm/sysreg.h>
17e6b673b7SDave Martin 
18e6b673b7SDave Martin /*
19e6b673b7SDave Martin  * Called on entry to KVM_RUN unless this vcpu previously ran at least
20e6b673b7SDave Martin  * once and the most recent prior KVM_RUN for this vcpu was called from
21e6b673b7SDave Martin  * the same task as current (highly likely).
22e6b673b7SDave Martin  *
23e6b673b7SDave Martin  * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
24e6b673b7SDave Martin  * such that on entering hyp the relevant parts of current are already
25e6b673b7SDave Martin  * mapped.
26e6b673b7SDave Martin  */
27e6b673b7SDave Martin int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
28e6b673b7SDave Martin {
29e6b673b7SDave Martin 	int ret;
30e6b673b7SDave Martin 
31e6b673b7SDave Martin 	struct thread_info *ti = &current->thread_info;
32e6b673b7SDave Martin 	struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
33e6b673b7SDave Martin 
34e6b673b7SDave Martin 	/*
35e6b673b7SDave Martin 	 * Make sure the host task thread flags and fpsimd state are
36e6b673b7SDave Martin 	 * visible to hyp:
37e6b673b7SDave Martin 	 */
38e6b673b7SDave Martin 	ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP);
39e6b673b7SDave Martin 	if (ret)
40e6b673b7SDave Martin 		goto error;
41e6b673b7SDave Martin 
42e6b673b7SDave Martin 	ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP);
43e6b673b7SDave Martin 	if (ret)
44e6b673b7SDave Martin 		goto error;
45e6b673b7SDave Martin 
46e6b673b7SDave Martin 	vcpu->arch.host_thread_info = kern_hyp_va(ti);
47e6b673b7SDave Martin 	vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
48e6b673b7SDave Martin error:
49e6b673b7SDave Martin 	return ret;
50e6b673b7SDave Martin }
51e6b673b7SDave Martin 
52e6b673b7SDave Martin /*
53e6b673b7SDave Martin  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
54e6b673b7SDave Martin  * The actual loading is done by the FPSIMD access trap taken to hyp.
55e6b673b7SDave Martin  *
56e6b673b7SDave Martin  * Here, we just set the correct metadata to indicate that the FPSIMD
57e6b673b7SDave Martin  * state in the cpu regs (if any) belongs to current on the host.
58e6b673b7SDave Martin  *
59e6b673b7SDave Martin  * TIF_SVE is backed up here, since it may get clobbered with guest state.
60e6b673b7SDave Martin  * This flag is restored by kvm_arch_vcpu_put_fp(vcpu).
61e6b673b7SDave Martin  */
62e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
63e6b673b7SDave Martin {
64e6b673b7SDave Martin 	BUG_ON(!current->mm);
65e6b673b7SDave Martin 
66b3eb56b6SDave Martin 	vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
67b3eb56b6SDave Martin 			      KVM_ARM64_HOST_SVE_IN_USE |
68b3eb56b6SDave Martin 			      KVM_ARM64_HOST_SVE_ENABLED);
69e6b673b7SDave Martin 	vcpu->arch.flags |= KVM_ARM64_FP_HOST;
70b3eb56b6SDave Martin 
71e6b673b7SDave Martin 	if (test_thread_flag(TIF_SVE))
72e6b673b7SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
73b3eb56b6SDave Martin 
74b3eb56b6SDave Martin 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
75b3eb56b6SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
76e6b673b7SDave Martin }
77e6b673b7SDave Martin 
78e6b673b7SDave Martin /*
79e6b673b7SDave Martin  * If the guest FPSIMD state was loaded, update the host's context
80e6b673b7SDave Martin  * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
81e6b673b7SDave Martin  * so that they will be written back if the kernel clobbers them due to
82e6b673b7SDave Martin  * kernel-mode NEON before re-entry into the guest.
83e6b673b7SDave Martin  */
84e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
85e6b673b7SDave Martin {
86e6b673b7SDave Martin 	WARN_ON_ONCE(!irqs_disabled());
87e6b673b7SDave Martin 
88e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
89e47c2055SMarc Zyngier 		fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
90b43b5dd9SDave Martin 					 vcpu->arch.sve_state,
91b43b5dd9SDave Martin 					 vcpu->arch.sve_max_vl);
9204950674SDave Martin 
93e6b673b7SDave Martin 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
94b43b5dd9SDave Martin 		update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
95e6b673b7SDave Martin 	}
96e6b673b7SDave Martin }
97e6b673b7SDave Martin 
98e6b673b7SDave Martin /*
99e6b673b7SDave Martin  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
100e6b673b7SDave Martin  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
101e6b673b7SDave Martin  * disappears and another task or vcpu appears that recycles the same
102e6b673b7SDave Martin  * struct fpsimd_state.
103e6b673b7SDave Martin  */
104e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
105e6b673b7SDave Martin {
106b045e4d0SDave Martin 	unsigned long flags;
10773433762SDave Martin 	bool host_has_sve = system_supports_sve();
10873433762SDave Martin 	bool guest_has_sve = vcpu_has_sve(vcpu);
109b045e4d0SDave Martin 
110b045e4d0SDave Martin 	local_irq_save(flags);
111e6b673b7SDave Martin 
112e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
11354b8c7cbSJulien Grall 		fpsimd_save_and_flush_cpu_state();
11473433762SDave Martin 
11573433762SDave Martin 		if (guest_has_sve)
116*83857371SMarc Zyngier 			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
11773433762SDave Martin 	} else if (host_has_sve) {
118b3eb56b6SDave Martin 		/*
119b3eb56b6SDave Martin 		 * The FPSIMD/SVE state in the CPU has not been touched, and we
120b3eb56b6SDave Martin 		 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
121b3eb56b6SDave Martin 		 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
122b3eb56b6SDave Martin 		 * for EL0.  To avoid spurious traps, restore the trap state
123b3eb56b6SDave Martin 		 * seen by kvm_arch_vcpu_load_fp():
124b3eb56b6SDave Martin 		 */
125b3eb56b6SDave Martin 		if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
126b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
127b3eb56b6SDave Martin 		else
128b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
129e6b673b7SDave Martin 	}
130e6b673b7SDave Martin 
1312955bcc8SDave Martin 	update_thread_flag(TIF_SVE,
1322955bcc8SDave Martin 			   vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
1332955bcc8SDave Martin 
134b045e4d0SDave Martin 	local_irq_restore(flags);
135e6b673b7SDave Martin }
136