2 * FP/SIMD context switching and fault handling
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
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.
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.
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/>.
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>
30 #include <asm/fpsimd.h>
31 #include <asm/cpufeature.h>
32 #include <asm/cputype.h>
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)
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.
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.
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.
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
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.
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;
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;
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
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;
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.
101 static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
104 * Trapped FP/ASIMD access.
106 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
108 /* TODO: implement lazy context saving/restoring */
113 * Raise a SIGFPE for the current process.
115 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
118 unsigned int si_code = 0;
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;
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);
136 send_sig_info(SIGFPE, &info, current);
139 void fpsimd_thread_switch(struct task_struct *next)
141 if (!system_supports_fpsimd())
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
148 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
149 fpsimd_save_state(¤t->thread.fpsimd_state);
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.
159 struct fpsimd_state *st = &next->thread.fpsimd_state;
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);
166 set_ti_thread_flag(task_thread_info(next),
167 TIF_FOREIGN_FPSTATE);
171 void fpsimd_flush_thread(void)
173 if (!system_supports_fpsimd())
178 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
179 fpsimd_flush_task_state(current);
180 set_thread_flag(TIF_FOREIGN_FPSTATE);
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'
189 void fpsimd_preserve_current_state(void)
191 if (!system_supports_fpsimd())
196 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
197 fpsimd_save_state(¤t->thread.fpsimd_state);
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
207 void fpsimd_restore_current_state(void)
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().
218 if (!system_supports_fpsimd()) {
219 clear_thread_flag(TIF_FOREIGN_FPSTATE);
225 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
226 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
228 fpsimd_load_state(st);
229 __this_cpu_write(fpsimd_last_state, st);
230 st->cpu = smp_processor_id();
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'
241 void fpsimd_update_current_state(struct fpsimd_state *state)
243 if (WARN_ON(!system_supports_fpsimd()))
248 fpsimd_load_state(state);
249 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
250 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
252 __this_cpu_write(fpsimd_last_state, st);
253 st->cpu = smp_processor_id();
260 * Invalidate live CPU copies of task t's FPSIMD state
262 void fpsimd_flush_task_state(struct task_struct *t)
264 t->thread.fpsimd_state.cpu = NR_CPUS;
267 #ifdef CONFIG_KERNEL_MODE_NEON
269 DEFINE_PER_CPU(bool, kernel_neon_busy);
270 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
273 * Kernel-side NEON support functions
277 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
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.
283 * A matching call to kernel_neon_end() must be made before returning from the
286 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
289 void kernel_neon_begin(void)
291 if (WARN_ON(!system_supports_fpsimd()))
294 BUG_ON(!may_use_simd());
298 __this_cpu_write(kernel_neon_busy, true);
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);
304 /* Invalidate any task state remaining in the fpsimd regs: */
305 __this_cpu_write(fpsimd_last_state, NULL);
311 EXPORT_SYMBOL(kernel_neon_begin);
314 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
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.
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.
322 void kernel_neon_end(void)
326 if (!system_supports_fpsimd())
329 busy = __this_cpu_xchg(kernel_neon_busy, false);
330 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
334 EXPORT_SYMBOL(kernel_neon_end);
338 static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
339 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
342 * EFI runtime services support functions
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.
348 * These functions provide the necessary support for ensuring FPSIMD
349 * save/restore in the contexts from which EFI is used.
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.
356 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
358 void __efi_fpsimd_begin(void)
360 if (!system_supports_fpsimd())
363 WARN_ON(preemptible());
368 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
369 __this_cpu_write(efi_fpsimd_state_used, true);
374 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
376 void __efi_fpsimd_end(void)
378 if (!system_supports_fpsimd())
381 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
382 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
387 #endif /* CONFIG_EFI */
389 #endif /* CONFIG_KERNEL_MODE_NEON */
392 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
393 unsigned long cmd, void *v)
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);
403 set_thread_flag(TIF_FOREIGN_FPSTATE);
405 case CPU_PM_ENTER_FAILED:
412 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
413 .notifier_call = fpsimd_cpu_pm_notifier,
416 static void __init fpsimd_pm_init(void)
418 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
422 static inline void fpsimd_pm_init(void) { }
423 #endif /* CONFIG_CPU_PM */
425 #ifdef CONFIG_HOTPLUG_CPU
426 static int fpsimd_cpu_dead(unsigned int cpu)
428 per_cpu(fpsimd_last_state, cpu) = NULL;
432 static inline void fpsimd_hotplug_init(void)
434 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
435 NULL, fpsimd_cpu_dead);
439 static inline void fpsimd_hotplug_init(void) { }
443 * FP/SIMD support code initialisation.
445 static int __init fpsimd_init(void)
447 if (elf_hwcap & HWCAP_FP) {
449 fpsimd_hotplug_init();
451 pr_notice("Floating-point is not implemented\n");
454 if (!(elf_hwcap & HWCAP_ASIMD))
455 pr_notice("Advanced SIMD is not implemented\n");
459 core_initcall(fpsimd_init);