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 <Dave.Martin@arm.com>
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
kvm_vcpu_unshare_task_fp(struct kvm_vcpu * vcpu)17 void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
18 {
19 struct task_struct *p = vcpu->arch.parent_task;
20 struct user_fpsimd_state *fpsimd;
21
22 if (!is_protected_kvm_enabled() || !p)
23 return;
24
25 fpsimd = &p->thread.uw.fpsimd_state;
26 kvm_unshare_hyp(fpsimd, fpsimd + 1);
27 put_task_struct(p);
28 }
29
30 /*
31 * Called on entry to KVM_RUN unless this vcpu previously ran at least
32 * once and the most recent prior KVM_RUN for this vcpu was called from
33 * the same task as current (highly likely).
34 *
35 * This is guaranteed to execute before kvm_arch_vcpu_load_fp(vcpu),
36 * such that on entering hyp the relevant parts of current are already
37 * mapped.
38 */
kvm_arch_vcpu_run_map_fp(struct kvm_vcpu * vcpu)39 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
40 {
41 int ret;
42
43 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
44
45 kvm_vcpu_unshare_task_fp(vcpu);
46
47 /* Make sure the host task fpsimd state is visible to hyp: */
48 ret = kvm_share_hyp(fpsimd, fpsimd + 1);
49 if (ret)
50 return ret;
51
52 /*
53 * We need to keep current's task_struct pinned until its data has been
54 * unshared with the hypervisor to make sure it is not re-used by the
55 * kernel and donated to someone else while already shared -- see
56 * kvm_vcpu_unshare_task_fp() for the matching put_task_struct().
57 */
58 if (is_protected_kvm_enabled()) {
59 get_task_struct(current);
60 vcpu->arch.parent_task = current;
61 }
62
63 return 0;
64 }
65
66 /*
67 * Prepare vcpu for saving the host's FPSIMD state and loading the guest's.
68 * The actual loading is done by the FPSIMD access trap taken to hyp.
69 *
70 * Here, we just set the correct metadata to indicate that the FPSIMD
71 * state in the cpu regs (if any) belongs to current on the host.
72 */
kvm_arch_vcpu_load_fp(struct kvm_vcpu * vcpu)73 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
74 {
75 BUG_ON(!current->mm);
76
77 if (!system_supports_fpsimd())
78 return;
79
80 /*
81 * Ensure that any host FPSIMD/SVE/SME state is saved and unbound such
82 * that the host kernel is responsible for restoring this state upon
83 * return to userspace, and the hyp code doesn't need to save anything.
84 *
85 * When the host may use SME, fpsimd_save_and_flush_cpu_state() ensures
86 * that PSTATE.{SM,ZA} == {0,0}.
87 */
88 fpsimd_save_and_flush_cpu_state();
89 vcpu->arch.fp_state = FP_STATE_FREE;
90 }
91
92 /*
93 * Called just before entering the guest once we are no longer preemptable
94 * and interrupts are disabled. If we have managed to run anything using
95 * FP while we were preemptible (such as off the back of an interrupt),
96 * then neither the host nor the guest own the FP hardware (and it was the
97 * responsibility of the code that used FP to save the existing state).
98 */
kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu * vcpu)99 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
100 {
101 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
102 vcpu->arch.fp_state = FP_STATE_FREE;
103 }
104
105 /*
106 * Called just after exiting the guest. If the guest FPSIMD state
107 * was loaded, update the host's context tracking data mark the CPU
108 * FPSIMD regs as dirty and belonging to vcpu so that they will be
109 * written back if the kernel clobbers them due to kernel-mode NEON
110 * before re-entry into the guest.
111 */
kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu * vcpu)112 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
113 {
114 struct cpu_fp_state fp_state;
115
116 WARN_ON_ONCE(!irqs_disabled());
117
118 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
119
120 /*
121 * Currently we do not support SME guests so SVCR is
122 * always 0 and we just need a variable to point to.
123 */
124 fp_state.st = &vcpu->arch.ctxt.fp_regs;
125 fp_state.sve_state = vcpu->arch.sve_state;
126 fp_state.sve_vl = vcpu->arch.sve_max_vl;
127 fp_state.sme_state = NULL;
128 fp_state.svcr = &vcpu->arch.svcr;
129 fp_state.fp_type = &vcpu->arch.fp_type;
130
131 if (vcpu_has_sve(vcpu))
132 fp_state.to_save = FP_STATE_SVE;
133 else
134 fp_state.to_save = FP_STATE_FPSIMD;
135
136 fpsimd_bind_state_to_cpu(&fp_state);
137
138 clear_thread_flag(TIF_FOREIGN_FPSTATE);
139 }
140 }
141
142 /*
143 * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the
144 * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu
145 * disappears and another task or vcpu appears that recycles the same
146 * struct fpsimd_state.
147 */
kvm_arch_vcpu_put_fp(struct kvm_vcpu * vcpu)148 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
149 {
150 unsigned long flags;
151
152 local_irq_save(flags);
153
154 if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
155 /*
156 * Flush (save and invalidate) the fpsimd/sve state so that if
157 * the host tries to use fpsimd/sve, it's not using stale data
158 * from the guest.
159 *
160 * Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the
161 * context unconditionally, in both nVHE and VHE. This allows
162 * the kernel to restore the fpsimd/sve state, including ZCR_EL1
163 * when needed.
164 */
165 fpsimd_save_and_flush_cpu_state();
166 }
167
168 local_irq_restore(flags);
169 }
170