xref: /openbmc/linux/arch/arm64/kvm/fpsimd.c (revision b3eb56b629d1095dde56fa37f4d7bcd5f783c8b2)
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>
12e6b673b7SDave Martin #include <asm/kvm_asm.h>
13e6b673b7SDave Martin #include <asm/kvm_host.h>
14e6b673b7SDave Martin #include <asm/kvm_mmu.h>
15*b3eb56b6SDave 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 thread_info *ti = &current->thread_info;
31e6b673b7SDave Martin 	struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
32e6b673b7SDave Martin 
33e6b673b7SDave Martin 	/*
34e6b673b7SDave Martin 	 * Make sure the host task thread flags and fpsimd state are
35e6b673b7SDave Martin 	 * visible to hyp:
36e6b673b7SDave Martin 	 */
37e6b673b7SDave Martin 	ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP);
38e6b673b7SDave Martin 	if (ret)
39e6b673b7SDave Martin 		goto error;
40e6b673b7SDave Martin 
41e6b673b7SDave Martin 	ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP);
42e6b673b7SDave Martin 	if (ret)
43e6b673b7SDave Martin 		goto error;
44e6b673b7SDave Martin 
45e6b673b7SDave Martin 	vcpu->arch.host_thread_info = kern_hyp_va(ti);
46e6b673b7SDave Martin 	vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
47e6b673b7SDave Martin error:
48e6b673b7SDave Martin 	return ret;
49e6b673b7SDave Martin }
50e6b673b7SDave Martin 
51e6b673b7SDave Martin /*
52e6b673b7SDave Martin  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
53e6b673b7SDave Martin  * The actual loading is done by the FPSIMD access trap taken to hyp.
54e6b673b7SDave Martin  *
55e6b673b7SDave Martin  * Here, we just set the correct metadata to indicate that the FPSIMD
56e6b673b7SDave Martin  * state in the cpu regs (if any) belongs to current on the host.
57e6b673b7SDave Martin  *
58e6b673b7SDave Martin  * TIF_SVE is backed up here, since it may get clobbered with guest state.
59e6b673b7SDave Martin  * This flag is restored by kvm_arch_vcpu_put_fp(vcpu).
60e6b673b7SDave Martin  */
61e6b673b7SDave Martin void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
62e6b673b7SDave Martin {
63e6b673b7SDave Martin 	BUG_ON(!current->mm);
64e6b673b7SDave Martin 
65*b3eb56b6SDave Martin 	vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
66*b3eb56b6SDave Martin 			      KVM_ARM64_HOST_SVE_IN_USE |
67*b3eb56b6SDave Martin 			      KVM_ARM64_HOST_SVE_ENABLED);
68e6b673b7SDave Martin 	vcpu->arch.flags |= KVM_ARM64_FP_HOST;
69*b3eb56b6SDave Martin 
70e6b673b7SDave Martin 	if (test_thread_flag(TIF_SVE))
71e6b673b7SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
72*b3eb56b6SDave Martin 
73*b3eb56b6SDave Martin 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
74*b3eb56b6SDave Martin 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
75e6b673b7SDave Martin }
76e6b673b7SDave Martin 
77e6b673b7SDave Martin /*
78e6b673b7SDave Martin  * If the guest FPSIMD state was loaded, update the host's context
79e6b673b7SDave Martin  * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
80e6b673b7SDave Martin  * so that they will be written back if the kernel clobbers them due to
81e6b673b7SDave Martin  * kernel-mode NEON before re-entry into the guest.
82e6b673b7SDave Martin  */
83e6b673b7SDave Martin void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
84e6b673b7SDave Martin {
85e6b673b7SDave Martin 	WARN_ON_ONCE(!irqs_disabled());
86e6b673b7SDave Martin 
87e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
88e6b673b7SDave Martin 		fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.gp_regs.fp_regs);
89e6b673b7SDave Martin 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
90e6b673b7SDave Martin 		clear_thread_flag(TIF_SVE);
91e6b673b7SDave Martin 	}
92e6b673b7SDave Martin }
93e6b673b7SDave Martin 
94e6b673b7SDave Martin /*
95e6b673b7SDave Martin  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
96e6b673b7SDave Martin  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
97e6b673b7SDave Martin  * disappears and another task or vcpu appears that recycles the same
98e6b673b7SDave Martin  * struct fpsimd_state.
99e6b673b7SDave Martin  */
100e6b673b7SDave Martin void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
101e6b673b7SDave Martin {
102b045e4d0SDave Martin 	unsigned long flags;
103b045e4d0SDave Martin 
104b045e4d0SDave Martin 	local_irq_save(flags);
105e6b673b7SDave Martin 
106e6b673b7SDave Martin 	update_thread_flag(TIF_SVE,
107e6b673b7SDave Martin 			   vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
108e6b673b7SDave Martin 
109e6b673b7SDave Martin 	if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
110e6b673b7SDave Martin 		/* Clean guest FP state to memory and invalidate cpu view */
111e6b673b7SDave Martin 		fpsimd_save();
112e6b673b7SDave Martin 		fpsimd_flush_cpu_state();
113*b3eb56b6SDave Martin 	} else if (system_supports_sve()) {
114*b3eb56b6SDave Martin 		/*
115*b3eb56b6SDave Martin 		 * The FPSIMD/SVE state in the CPU has not been touched, and we
116*b3eb56b6SDave Martin 		 * have SVE (and VHE): CPACR_EL1 (alias CPTR_EL2) has been
117*b3eb56b6SDave Martin 		 * reset to CPACR_EL1_DEFAULT by the Hyp code, disabling SVE
118*b3eb56b6SDave Martin 		 * for EL0.  To avoid spurious traps, restore the trap state
119*b3eb56b6SDave Martin 		 * seen by kvm_arch_vcpu_load_fp():
120*b3eb56b6SDave Martin 		 */
121*b3eb56b6SDave Martin 		if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
122*b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
123*b3eb56b6SDave Martin 		else
124*b3eb56b6SDave Martin 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
125e6b673b7SDave Martin 	}
126e6b673b7SDave Martin 
127b045e4d0SDave Martin 	local_irq_restore(flags);
128e6b673b7SDave Martin }
129