1 /*
2 * FP/SIMD context switching and fault handling
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <linux/bottom_half.h>
21 #include <linux/cpu.h>
22 #include <linux/cpu_pm.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/percpu.h>
26 #include <linux/preempt.h>
27 #include <linux/sched/signal.h>
28 #include <linux/signal.h>
29
30 #include <asm/fpsimd.h>
31 #include <asm/cpufeature.h>
32 #include <asm/cputype.h>
33 #include <asm/simd.h>
34
35 #define FPEXC_IOF (1 << 0)
36 #define FPEXC_DZF (1 << 1)
37 #define FPEXC_OFF (1 << 2)
38 #define FPEXC_UFF (1 << 3)
39 #define FPEXC_IXF (1 << 4)
40 #define FPEXC_IDF (1 << 7)
41
42 /*
43 * In order to reduce the number of times the FPSIMD state is needlessly saved
44 * and restored, we need to keep track of two things:
45 * (a) for each task, we need to remember which CPU was the last one to have
46 * the task's FPSIMD state loaded into its FPSIMD registers;
47 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
48 * been loaded into its FPSIMD registers most recently, or whether it has
49 * been used to perform kernel mode NEON in the meantime.
50 *
51 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
52 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
53 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
54 * address of the userland FPSIMD state of the task that was loaded onto the CPU
55 * the most recently, or NULL if kernel mode NEON has been performed after that.
56 *
57 * With this in place, we no longer have to restore the next FPSIMD state right
58 * when switching between tasks. Instead, we can defer this check to userland
59 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
60 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
61 * can omit the FPSIMD restore.
62 *
63 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
64 * indicate whether or not the userland FPSIMD state of the current task is
65 * present in the registers. The flag is set unless the FPSIMD registers of this
66 * CPU currently contain the most recent userland FPSIMD state of the current
67 * task.
68 *
69 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
70 * save the task's FPSIMD context back to task_struct from softirq context.
71 * To prevent this from racing with the manipulation of the task's FPSIMD state
72 * from task context and thereby corrupting the state, it is necessary to
73 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
74 * flag with local_bh_disable() unless softirqs are already masked.
75 *
76 * For a certain task, the sequence may look something like this:
77 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
78 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
79 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
80 * cleared, otherwise it is set;
81 *
82 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
83 * userland FPSIMD state is copied from memory to the registers, the task's
84 * fpsimd_state.cpu field is set to the id of the current CPU, the current
85 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
86 * TIF_FOREIGN_FPSTATE flag is cleared;
87 *
88 * - the task executes an ordinary syscall; upon return to userland, the
89 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
90 * restored;
91 *
92 * - the task executes a syscall which executes some NEON instructions; this is
93 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
94 * register contents to memory, clears the fpsimd_last_state per-cpu variable
95 * and sets the TIF_FOREIGN_FPSTATE flag;
96 *
97 * - the task gets preempted after kernel_neon_end() is called; as we have not
98 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
99 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
100 */
101 static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
102
103 /*
104 * Trapped FP/ASIMD access.
105 */
do_fpsimd_acc(unsigned int esr,struct pt_regs * regs)106 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
107 {
108 /* TODO: implement lazy context saving/restoring */
109 WARN_ON(1);
110 }
111
112 /*
113 * Raise a SIGFPE for the current process.
114 */
do_fpsimd_exc(unsigned int esr,struct pt_regs * regs)115 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
116 {
117 siginfo_t info;
118 unsigned int si_code = 0;
119
120 if (esr & FPEXC_IOF)
121 si_code = FPE_FLTINV;
122 else if (esr & FPEXC_DZF)
123 si_code = FPE_FLTDIV;
124 else if (esr & FPEXC_OFF)
125 si_code = FPE_FLTOVF;
126 else if (esr & FPEXC_UFF)
127 si_code = FPE_FLTUND;
128 else if (esr & FPEXC_IXF)
129 si_code = FPE_FLTRES;
130
131 memset(&info, 0, sizeof(info));
132 info.si_signo = SIGFPE;
133 info.si_code = si_code;
134 info.si_addr = (void __user *)instruction_pointer(regs);
135
136 send_sig_info(SIGFPE, &info, current);
137 }
138
fpsimd_thread_switch(struct task_struct * next)139 void fpsimd_thread_switch(struct task_struct *next)
140 {
141 if (!system_supports_fpsimd())
142 return;
143 /*
144 * Save the current FPSIMD state to memory, but only if whatever is in
145 * the registers is in fact the most recent userland FPSIMD state of
146 * 'current'.
147 */
148 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
149 fpsimd_save_state(¤t->thread.fpsimd_state);
150
151 if (next->mm) {
152 /*
153 * If we are switching to a task whose most recent userland
154 * FPSIMD state is already in the registers of *this* cpu,
155 * we can skip loading the state from memory. Otherwise, set
156 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
157 * upon the next return to userland.
158 */
159 struct fpsimd_state *st = &next->thread.fpsimd_state;
160
161 if (__this_cpu_read(fpsimd_last_state) == st
162 && st->cpu == smp_processor_id())
163 clear_ti_thread_flag(task_thread_info(next),
164 TIF_FOREIGN_FPSTATE);
165 else
166 set_ti_thread_flag(task_thread_info(next),
167 TIF_FOREIGN_FPSTATE);
168 }
169 }
170
fpsimd_flush_thread(void)171 void fpsimd_flush_thread(void)
172 {
173 if (!system_supports_fpsimd())
174 return;
175
176 local_bh_disable();
177
178 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
179 fpsimd_flush_task_state(current);
180 set_thread_flag(TIF_FOREIGN_FPSTATE);
181
182 local_bh_enable();
183 }
184
185 /*
186 * Save the userland FPSIMD state of 'current' to memory, but only if the state
187 * currently held in the registers does in fact belong to 'current'
188 */
fpsimd_preserve_current_state(void)189 void fpsimd_preserve_current_state(void)
190 {
191 if (!system_supports_fpsimd())
192 return;
193
194 local_bh_disable();
195
196 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
197 fpsimd_save_state(¤t->thread.fpsimd_state);
198
199 local_bh_enable();
200 }
201
202 /*
203 * Load the userland FPSIMD state of 'current' from memory, but only if the
204 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
205 * state of 'current'
206 */
fpsimd_restore_current_state(void)207 void fpsimd_restore_current_state(void)
208 {
209 /*
210 * For the tasks that were created before we detected the absence of
211 * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
212 * e.g, init. This could be then inherited by the children processes.
213 * If we later detect that the system doesn't support FP/SIMD,
214 * we must clear the flag for all the tasks to indicate that the
215 * FPSTATE is clean (as we can't have one) to avoid looping for ever in
216 * do_notify_resume().
217 */
218 if (!system_supports_fpsimd()) {
219 clear_thread_flag(TIF_FOREIGN_FPSTATE);
220 return;
221 }
222
223 local_bh_disable();
224
225 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
226 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
227
228 fpsimd_load_state(st);
229 __this_cpu_write(fpsimd_last_state, st);
230 st->cpu = smp_processor_id();
231 }
232
233 local_bh_enable();
234 }
235
236 /*
237 * Load an updated userland FPSIMD state for 'current' from memory and set the
238 * flag that indicates that the FPSIMD register contents are the most recent
239 * FPSIMD state of 'current'
240 */
fpsimd_update_current_state(struct fpsimd_state * state)241 void fpsimd_update_current_state(struct fpsimd_state *state)
242 {
243 if (WARN_ON(!system_supports_fpsimd()))
244 return;
245
246 local_bh_disable();
247
248 fpsimd_load_state(state);
249 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
250 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
251
252 __this_cpu_write(fpsimd_last_state, st);
253 st->cpu = smp_processor_id();
254 }
255
256 local_bh_enable();
257 }
258
259 /*
260 * Invalidate live CPU copies of task t's FPSIMD state
261 */
fpsimd_flush_task_state(struct task_struct * t)262 void fpsimd_flush_task_state(struct task_struct *t)
263 {
264 t->thread.fpsimd_state.cpu = NR_CPUS;
265 }
266
267 #ifdef CONFIG_KERNEL_MODE_NEON
268
269 DEFINE_PER_CPU(bool, kernel_neon_busy);
270 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
271
272 /*
273 * Kernel-side NEON support functions
274 */
275
276 /*
277 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
278 * context
279 *
280 * Must not be called unless may_use_simd() returns true.
281 * Task context in the FPSIMD registers is saved back to memory as necessary.
282 *
283 * A matching call to kernel_neon_end() must be made before returning from the
284 * calling context.
285 *
286 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
287 * called.
288 */
kernel_neon_begin(void)289 void kernel_neon_begin(void)
290 {
291 if (WARN_ON(!system_supports_fpsimd()))
292 return;
293
294 BUG_ON(!may_use_simd());
295
296 local_bh_disable();
297
298 __this_cpu_write(kernel_neon_busy, true);
299
300 /* Save unsaved task fpsimd state, if any: */
301 if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
302 fpsimd_save_state(¤t->thread.fpsimd_state);
303
304 /* Invalidate any task state remaining in the fpsimd regs: */
305 __this_cpu_write(fpsimd_last_state, NULL);
306
307 preempt_disable();
308
309 local_bh_enable();
310 }
311 EXPORT_SYMBOL(kernel_neon_begin);
312
313 /*
314 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
315 *
316 * Must be called from a context in which kernel_neon_begin() was previously
317 * called, with no call to kernel_neon_end() in the meantime.
318 *
319 * The caller must not use the FPSIMD registers after this function is called,
320 * unless kernel_neon_begin() is called again in the meantime.
321 */
kernel_neon_end(void)322 void kernel_neon_end(void)
323 {
324 bool busy;
325
326 if (!system_supports_fpsimd())
327 return;
328
329 busy = __this_cpu_xchg(kernel_neon_busy, false);
330 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
331
332 preempt_enable();
333 }
334 EXPORT_SYMBOL(kernel_neon_end);
335
336 #ifdef CONFIG_EFI
337
338 static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
339 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
340
341 /*
342 * EFI runtime services support functions
343 *
344 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
345 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
346 * is always used rather than being an optional accelerator.
347 *
348 * These functions provide the necessary support for ensuring FPSIMD
349 * save/restore in the contexts from which EFI is used.
350 *
351 * Do not use them for any other purpose -- if tempted to do so, you are
352 * either doing something wrong or you need to propose some refactoring.
353 */
354
355 /*
356 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
357 */
__efi_fpsimd_begin(void)358 void __efi_fpsimd_begin(void)
359 {
360 if (!system_supports_fpsimd())
361 return;
362
363 WARN_ON(preemptible());
364
365 if (may_use_simd())
366 kernel_neon_begin();
367 else {
368 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
369 __this_cpu_write(efi_fpsimd_state_used, true);
370 }
371 }
372
373 /*
374 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
375 */
__efi_fpsimd_end(void)376 void __efi_fpsimd_end(void)
377 {
378 if (!system_supports_fpsimd())
379 return;
380
381 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
382 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
383 else
384 kernel_neon_end();
385 }
386
387 #endif /* CONFIG_EFI */
388
389 #endif /* CONFIG_KERNEL_MODE_NEON */
390
391 #ifdef CONFIG_CPU_PM
fpsimd_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)392 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
393 unsigned long cmd, void *v)
394 {
395 switch (cmd) {
396 case CPU_PM_ENTER:
397 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
398 fpsimd_save_state(¤t->thread.fpsimd_state);
399 this_cpu_write(fpsimd_last_state, NULL);
400 break;
401 case CPU_PM_EXIT:
402 if (current->mm)
403 set_thread_flag(TIF_FOREIGN_FPSTATE);
404 break;
405 case CPU_PM_ENTER_FAILED:
406 default:
407 return NOTIFY_DONE;
408 }
409 return NOTIFY_OK;
410 }
411
412 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
413 .notifier_call = fpsimd_cpu_pm_notifier,
414 };
415
fpsimd_pm_init(void)416 static void __init fpsimd_pm_init(void)
417 {
418 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
419 }
420
421 #else
fpsimd_pm_init(void)422 static inline void fpsimd_pm_init(void) { }
423 #endif /* CONFIG_CPU_PM */
424
425 #ifdef CONFIG_HOTPLUG_CPU
fpsimd_cpu_dead(unsigned int cpu)426 static int fpsimd_cpu_dead(unsigned int cpu)
427 {
428 per_cpu(fpsimd_last_state, cpu) = NULL;
429 return 0;
430 }
431
fpsimd_hotplug_init(void)432 static inline void fpsimd_hotplug_init(void)
433 {
434 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
435 NULL, fpsimd_cpu_dead);
436 }
437
438 #else
fpsimd_hotplug_init(void)439 static inline void fpsimd_hotplug_init(void) { }
440 #endif
441
442 /*
443 * FP/SIMD support code initialisation.
444 */
fpsimd_init(void)445 static int __init fpsimd_init(void)
446 {
447 if (elf_hwcap & HWCAP_FP) {
448 fpsimd_pm_init();
449 fpsimd_hotplug_init();
450 } else {
451 pr_notice("Floating-point is not implemented\n");
452 }
453
454 if (!(elf_hwcap & HWCAP_ASIMD))
455 pr_notice("Advanced SIMD is not implemented\n");
456
457 return 0;
458 }
459 core_initcall(fpsimd_init);
460