1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/arm64/kvm/fpsimd.c: Guest/host FPSIMD context coordination helpers
4  *
5  * Copyright 2018 Arm Limited
6  * Author: Dave Martin <[email protected]>
7  */
8 #include <linux/irqflags.h>
9 #include <linux/sched.h>
10 #include <linux/kvm_host.h>
11 #include <asm/fpsimd.h>
12 #include <asm/kvm_asm.h>
13 #include <asm/kvm_hyp.h>
14 #include <asm/kvm_mmu.h>
15 #include <asm/sysreg.h>
16 
17 /*
18  * Called on entry to KVM_RUN unless this vcpu previously ran at least
19  * once and the most recent prior KVM_RUN for this vcpu was called from
20  * the same task as current (highly likely).
21  *
22  * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
23  * such that on entering hyp the relevant parts of current are already
24  * mapped.
25  */
kvm_arch_vcpu_run_map_fp(struct kvm_vcpu * vcpu)26 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
27 {
28 	struct user_fpsimd_state *fpsimd = &current->thread.uw.fpsimd_state;
29 	int ret;
30 
31 	/* pKVM has its own tracking of the host fpsimd state. */
32 	if (is_protected_kvm_enabled())
33 		return 0;
34 
35 	/* Make sure the host task fpsimd state is visible to hyp: */
36 	ret = kvm_share_hyp(fpsimd, fpsimd + 1);
37 	if (ret)
38 		return ret;
39 
40 	return 0;
41 }
42 
43 /*
44  * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
45  * The actual loading is done by the FPSIMD access trap taken to hyp.
46  *
47  * Here, we just set the correct metadata to indicate that the FPSIMD
48  * state in the cpu regs (if any) belongs to current on the host.
49  */
kvm_arch_vcpu_load_fp(struct kvm_vcpu * vcpu)50 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
51 {
52 	BUG_ON(!current->mm);
53 
54 	if (!system_supports_fpsimd())
55 		return;
56 
57 	/*
58 	 * Ensure that any host FPSIMD/SVE/SME state is saved and unbound such
59 	 * that the host kernel is responsible for restoring this state upon
60 	 * return to userspace, and the hyp code doesn't need to save anything.
61 	 *
62 	 * When the host may use SME, fpsimd_save_and_flush_cpu_state() ensures
63 	 * that PSTATE.{SM,ZA} == {0,0}.
64 	 */
65 	fpsimd_save_and_flush_cpu_state();
66 	*host_data_ptr(fp_owner) = FP_STATE_FREE;
67 
68 	WARN_ON_ONCE(system_supports_sme() && read_sysreg_s(SYS_SVCR));
69 }
70 
71 /*
72  * Called just before entering the guest once we are no longer preemptible
73  * and interrupts are disabled. If we have managed to run anything using
74  * FP while we were preemptible (such as off the back of an interrupt),
75  * then neither the host nor the guest own the FP hardware (and it was the
76  * responsibility of the code that used FP to save the existing state).
77  */
kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu * vcpu)78 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
79 {
80 	if (test_thread_flag(TIF_FOREIGN_FPSTATE))
81 		*host_data_ptr(fp_owner) = FP_STATE_FREE;
82 }
83 
84 /*
85  * Called just after exiting the guest. If the guest FPSIMD state
86  * was loaded, update the host's context tracking data mark the CPU
87  * FPSIMD regs as dirty and belonging to vcpu so that they will be
88  * written back if the kernel clobbers them due to kernel-mode NEON
89  * before re-entry into the guest.
90  */
kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu * vcpu)91 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
92 {
93 	struct cpu_fp_state fp_state;
94 
95 	WARN_ON_ONCE(!irqs_disabled());
96 
97 	if (guest_owns_fp_regs()) {
98 		/*
99 		 * Currently we do not support SME guests so SVCR is
100 		 * always 0 and we just need a variable to point to.
101 		 */
102 		fp_state.st = &vcpu->arch.ctxt.fp_regs;
103 		fp_state.sve_state = vcpu->arch.sve_state;
104 		fp_state.sve_vl = vcpu->arch.sve_max_vl;
105 		fp_state.sme_state = NULL;
106 		fp_state.svcr = &__vcpu_sys_reg(vcpu, SVCR);
107 		fp_state.fpmr = &__vcpu_sys_reg(vcpu, FPMR);
108 		fp_state.fp_type = &vcpu->arch.fp_type;
109 
110 		if (vcpu_has_sve(vcpu))
111 			fp_state.to_save = FP_STATE_SVE;
112 		else
113 			fp_state.to_save = FP_STATE_FPSIMD;
114 
115 		fpsimd_bind_state_to_cpu(&fp_state);
116 
117 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
118 	}
119 }
120 
121 /*
122  * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
123  * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
124  * disappears and another task or vcpu appears that recycles the same
125  * struct fpsimd_state.
126  */
kvm_arch_vcpu_put_fp(struct kvm_vcpu * vcpu)127 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
128 {
129 	unsigned long flags;
130 
131 	local_irq_save(flags);
132 
133 	if (guest_owns_fp_regs()) {
134 		/*
135 		 * Flush (save and invalidate) the fpsimd/sve state so that if
136 		 * the host tries to use fpsimd/sve, it's not using stale data
137 		 * from the guest.
138 		 *
139 		 * Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the
140 		 * context unconditionally, in both nVHE and VHE. This allows
141 		 * the kernel to restore the fpsimd/sve state, including ZCR_EL1
142 		 * when needed.
143 		 */
144 		fpsimd_save_and_flush_cpu_state();
145 	}
146 
147 	local_irq_restore(flags);
148 }
149