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/bitmap.h>
21 #include <linux/bottom_half.h>
22 #include <linux/bug.h>
23 #include <linux/cache.h>
24 #include <linux/compat.h>
25 #include <linux/cpu.h>
26 #include <linux/cpu_pm.h>
27 #include <linux/kernel.h>
28 #include <linux/linkage.h>
29 #include <linux/irqflags.h>
30 #include <linux/init.h>
31 #include <linux/percpu.h>
32 #include <linux/preempt.h>
33 #include <linux/prctl.h>
34 #include <linux/ptrace.h>
35 #include <linux/sched/signal.h>
36 #include <linux/sched/task_stack.h>
37 #include <linux/signal.h>
38 #include <linux/slab.h>
40 #include <asm/fpsimd.h>
41 #include <asm/cputype.h>
43 #include <asm/sigcontext.h>
44 #include <asm/sysreg.h>
45 #include <asm/traps.h>
47 #define FPEXC_IOF (1 << 0)
48 #define FPEXC_DZF (1 << 1)
49 #define FPEXC_OFF (1 << 2)
50 #define FPEXC_UFF (1 << 3)
51 #define FPEXC_IXF (1 << 4)
52 #define FPEXC_IDF (1 << 7)
55 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
57 * In order to reduce the number of times the FPSIMD state is needlessly saved
58 * and restored, we need to keep track of two things:
59 * (a) for each task, we need to remember which CPU was the last one to have
60 * the task's FPSIMD state loaded into its FPSIMD registers;
61 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
62 * been loaded into its FPSIMD registers most recently, or whether it has
63 * been used to perform kernel mode NEON in the meantime.
65 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
66 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
67 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
68 * address of the userland FPSIMD state of the task that was loaded onto the CPU
69 * the most recently, or NULL if kernel mode NEON has been performed after that.
71 * With this in place, we no longer have to restore the next FPSIMD state right
72 * when switching between tasks. Instead, we can defer this check to userland
73 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
74 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
75 * can omit the FPSIMD restore.
77 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
78 * indicate whether or not the userland FPSIMD state of the current task is
79 * present in the registers. The flag is set unless the FPSIMD registers of this
80 * CPU currently contain the most recent userland FPSIMD state of the current
83 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
84 * save the task's FPSIMD context back to task_struct from softirq context.
85 * To prevent this from racing with the manipulation of the task's FPSIMD state
86 * from task context and thereby corrupting the state, it is necessary to
87 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
88 * flag with local_bh_disable() unless softirqs are already masked.
90 * For a certain task, the sequence may look something like this:
91 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
92 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
93 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
94 * cleared, otherwise it is set;
96 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
97 * userland FPSIMD state is copied from memory to the registers, the task's
98 * fpsimd_state.cpu field is set to the id of the current CPU, the current
99 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
100 * TIF_FOREIGN_FPSTATE flag is cleared;
102 * - the task executes an ordinary syscall; upon return to userland, the
103 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
106 * - the task executes a syscall which executes some NEON instructions; this is
107 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
108 * register contents to memory, clears the fpsimd_last_state per-cpu variable
109 * and sets the TIF_FOREIGN_FPSTATE flag;
111 * - the task gets preempted after kernel_neon_end() is called; as we have not
112 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
113 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
115 static DEFINE_PER_CPU(struct fpsimd_state
*, fpsimd_last_state
);
117 /* Default VL for tasks that don't set it explicitly: */
118 static int sve_default_vl
= -1;
120 #ifdef CONFIG_ARM64_SVE
122 /* Maximum supported vector length across all CPUs (initially poisoned) */
123 int __ro_after_init sve_max_vl
= -1;
124 /* Set of available vector lengths, as vq_to_bit(vq): */
125 static __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
126 static void __percpu
*efi_sve_state
;
128 #else /* ! CONFIG_ARM64_SVE */
130 /* Dummy declaration for code that will be optimised out: */
131 extern __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
132 extern void __percpu
*efi_sve_state
;
134 #endif /* ! CONFIG_ARM64_SVE */
137 * Call __sve_free() directly only if you know task can't be scheduled
140 static void __sve_free(struct task_struct
*task
)
142 kfree(task
->thread
.sve_state
);
143 task
->thread
.sve_state
= NULL
;
146 static void sve_free(struct task_struct
*task
)
148 WARN_ON(test_tsk_thread_flag(task
, TIF_SVE
));
154 /* Offset of FFR in the SVE register dump */
155 static size_t sve_ffr_offset(int vl
)
157 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl
)) - SVE_SIG_REGS_OFFSET
;
160 static void *sve_pffr(struct task_struct
*task
)
162 return (char *)task
->thread
.sve_state
+
163 sve_ffr_offset(task
->thread
.sve_vl
);
166 static void change_cpacr(u64 val
, u64 mask
)
168 u64 cpacr
= read_sysreg(CPACR_EL1
);
169 u64
new = (cpacr
& ~mask
) | val
;
172 write_sysreg(new, CPACR_EL1
);
175 static void sve_user_disable(void)
177 change_cpacr(0, CPACR_EL1_ZEN_EL0EN
);
180 static void sve_user_enable(void)
182 change_cpacr(CPACR_EL1_ZEN_EL0EN
, CPACR_EL1_ZEN_EL0EN
);
186 * TIF_SVE controls whether a task can use SVE without trapping while
187 * in userspace, and also the way a task's FPSIMD/SVE state is stored
190 * The kernel uses this flag to track whether a user task is actively
191 * using SVE, and therefore whether full SVE register state needs to
192 * be tracked. If not, the cheaper FPSIMD context handling code can
193 * be used instead of the more costly SVE equivalents.
197 * The task can execute SVE instructions while in userspace without
198 * trapping to the kernel.
200 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
201 * corresponding Zn), P0-P15 and FFR are encoded in in
202 * task->thread.sve_state, formatted appropriately for vector
203 * length task->thread.sve_vl.
205 * task->thread.sve_state must point to a valid buffer at least
206 * sve_state_size(task) bytes in size.
208 * During any syscall, the kernel may optionally clear TIF_SVE and
209 * discard the vector state except for the FPSIMD subset.
213 * An attempt by the user task to execute an SVE instruction causes
214 * do_sve_acc() to be called, which does some preparation and then
217 * When stored, FPSIMD registers V0-V31 are encoded in
218 * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
219 * logically zero but not stored anywhere; P0-P15 and FFR are not
220 * stored and have unspecified values from userspace's point of
221 * view. For hygiene purposes, the kernel zeroes them on next use,
222 * but userspace is discouraged from relying on this.
224 * task->thread.sve_state does not need to be non-NULL, valid or any
225 * particular size: it must not be dereferenced.
227 * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
228 * whether TIF_SVE is clear or set, since these are not vector length
233 * Update current's FPSIMD/SVE registers from thread_struct.
235 * This function should be called only when the FPSIMD/SVE state in
236 * thread_struct is known to be up to date, when preparing to enter
239 * Softirqs (and preemption) must be disabled.
241 static void task_fpsimd_load(void)
243 WARN_ON(!in_softirq() && !irqs_disabled());
245 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
246 sve_load_state(sve_pffr(current
),
247 ¤t
->thread
.fpsimd_state
.fpsr
,
248 sve_vq_from_vl(current
->thread
.sve_vl
) - 1);
250 fpsimd_load_state(¤t
->thread
.fpsimd_state
);
252 if (system_supports_sve()) {
253 /* Toggle SVE trapping for userspace if needed */
254 if (test_thread_flag(TIF_SVE
))
259 /* Serialised by exception return to user */
264 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
265 * with respect to the CPU registers.
267 * Softirqs (and preemption) must be disabled.
269 static void task_fpsimd_save(void)
271 WARN_ON(!in_softirq() && !irqs_disabled());
273 if (!test_thread_flag(TIF_FOREIGN_FPSTATE
)) {
274 if (system_supports_sve() && test_thread_flag(TIF_SVE
)) {
275 if (WARN_ON(sve_get_vl() != current
->thread
.sve_vl
)) {
277 * Can't save the user regs, so current would
278 * re-enter user with corrupt state.
279 * There's no way to recover, so kill it:
282 SIGKILL
, 0, current_pt_regs(), 0);
286 sve_save_state(sve_pffr(current
),
287 ¤t
->thread
.fpsimd_state
.fpsr
);
289 fpsimd_save_state(¤t
->thread
.fpsimd_state
);
294 * Helpers to translate bit indices in sve_vq_map to VQ values (and
295 * vice versa). This allows find_next_bit() to be used to find the
296 * _maximum_ VQ not exceeding a certain value.
299 static unsigned int vq_to_bit(unsigned int vq
)
301 return SVE_VQ_MAX
- vq
;
304 static unsigned int bit_to_vq(unsigned int bit
)
306 if (WARN_ON(bit
>= SVE_VQ_MAX
))
307 bit
= SVE_VQ_MAX
- 1;
309 return SVE_VQ_MAX
- bit
;
313 * All vector length selection from userspace comes through here.
314 * We're on a slow path, so some sanity-checks are included.
315 * If things go wrong there's a bug somewhere, but try to fall back to a
318 static unsigned int find_supported_vector_length(unsigned int vl
)
321 int max_vl
= sve_max_vl
;
323 if (WARN_ON(!sve_vl_valid(vl
)))
326 if (WARN_ON(!sve_vl_valid(max_vl
)))
332 bit
= find_next_bit(sve_vq_map
, SVE_VQ_MAX
,
333 vq_to_bit(sve_vq_from_vl(vl
)));
334 return sve_vl_from_vq(bit_to_vq(bit
));
337 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
338 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
341 * Transfer the FPSIMD state in task->thread.fpsimd_state to
342 * task->thread.sve_state.
344 * Task can be a non-runnable task, or current. In the latter case,
345 * softirqs (and preemption) must be disabled.
346 * task->thread.sve_state must point to at least sve_state_size(task)
347 * bytes of allocated kernel memory.
348 * task->thread.fpsimd_state must be up to date before calling this function.
350 static void fpsimd_to_sve(struct task_struct
*task
)
353 void *sst
= task
->thread
.sve_state
;
354 struct fpsimd_state
const *fst
= &task
->thread
.fpsimd_state
;
357 if (!system_supports_sve())
360 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
361 for (i
= 0; i
< 32; ++i
)
362 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
363 sizeof(fst
->vregs
[i
]));
367 * Transfer the SVE state in task->thread.sve_state to
368 * task->thread.fpsimd_state.
370 * Task can be a non-runnable task, or current. In the latter case,
371 * softirqs (and preemption) must be disabled.
372 * task->thread.sve_state must point to at least sve_state_size(task)
373 * bytes of allocated kernel memory.
374 * task->thread.sve_state must be up to date before calling this function.
376 static void sve_to_fpsimd(struct task_struct
*task
)
379 void const *sst
= task
->thread
.sve_state
;
380 struct fpsimd_state
*fst
= &task
->thread
.fpsimd_state
;
383 if (!system_supports_sve())
386 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
387 for (i
= 0; i
< 32; ++i
)
388 memcpy(&fst
->vregs
[i
], ZREG(sst
, vq
, i
),
389 sizeof(fst
->vregs
[i
]));
392 #ifdef CONFIG_ARM64_SVE
395 * Return how many bytes of memory are required to store the full SVE
396 * state for task, given task's currently configured vector length.
398 size_t sve_state_size(struct task_struct
const *task
)
400 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task
->thread
.sve_vl
));
404 * Ensure that task->thread.sve_state is allocated and sufficiently large.
406 * This function should be used only in preparation for replacing
407 * task->thread.sve_state with new data. The memory is always zeroed
408 * here to prevent stale data from showing through: this is done in
409 * the interest of testability and predictability: except in the
410 * do_sve_acc() case, there is no ABI requirement to hide stale data
411 * written previously be task.
413 void sve_alloc(struct task_struct
*task
)
415 if (task
->thread
.sve_state
) {
416 memset(task
->thread
.sve_state
, 0, sve_state_size(current
));
420 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
421 task
->thread
.sve_state
=
422 kzalloc(sve_state_size(task
), GFP_KERNEL
);
425 * If future SVE revisions can have larger vectors though,
426 * this may cease to be true:
428 BUG_ON(!task
->thread
.sve_state
);
433 * Ensure that task->thread.sve_state is up to date with respect to
434 * the user task, irrespective of when SVE is in use or not.
436 * This should only be called by ptrace. task must be non-runnable.
437 * task->thread.sve_state must point to at least sve_state_size(task)
438 * bytes of allocated kernel memory.
440 void fpsimd_sync_to_sve(struct task_struct
*task
)
442 if (!test_tsk_thread_flag(task
, TIF_SVE
))
447 * Ensure that task->thread.fpsimd_state is up to date with respect to
448 * the user task, irrespective of whether SVE is in use or not.
450 * This should only be called by ptrace. task must be non-runnable.
451 * task->thread.sve_state must point to at least sve_state_size(task)
452 * bytes of allocated kernel memory.
454 void sve_sync_to_fpsimd(struct task_struct
*task
)
456 if (test_tsk_thread_flag(task
, TIF_SVE
))
461 * Ensure that task->thread.sve_state is up to date with respect to
462 * the task->thread.fpsimd_state.
464 * This should only be called by ptrace to merge new FPSIMD register
465 * values into a task for which SVE is currently active.
466 * task must be non-runnable.
467 * task->thread.sve_state must point to at least sve_state_size(task)
468 * bytes of allocated kernel memory.
469 * task->thread.fpsimd_state must already have been initialised with
470 * the new FPSIMD register values to be merged in.
472 void sve_sync_from_fpsimd_zeropad(struct task_struct
*task
)
475 void *sst
= task
->thread
.sve_state
;
476 struct fpsimd_state
const *fst
= &task
->thread
.fpsimd_state
;
479 if (!test_tsk_thread_flag(task
, TIF_SVE
))
482 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
484 memset(sst
, 0, SVE_SIG_REGS_SIZE(vq
));
486 for (i
= 0; i
< 32; ++i
)
487 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
488 sizeof(fst
->vregs
[i
]));
491 int sve_set_vector_length(struct task_struct
*task
,
492 unsigned long vl
, unsigned long flags
)
494 if (flags
& ~(unsigned long)(PR_SVE_VL_INHERIT
|
495 PR_SVE_SET_VL_ONEXEC
))
498 if (!sve_vl_valid(vl
))
502 * Clamp to the maximum vector length that VL-agnostic SVE code can
503 * work with. A flag may be assigned in the future to allow setting
504 * of larger vector lengths without confusing older software.
506 if (vl
> SVE_VL_ARCH_MAX
)
507 vl
= SVE_VL_ARCH_MAX
;
509 vl
= find_supported_vector_length(vl
);
511 if (flags
& (PR_SVE_VL_INHERIT
|
512 PR_SVE_SET_VL_ONEXEC
))
513 task
->thread
.sve_vl_onexec
= vl
;
515 /* Reset VL to system default on next exec: */
516 task
->thread
.sve_vl_onexec
= 0;
518 /* Only actually set the VL if not deferred: */
519 if (flags
& PR_SVE_SET_VL_ONEXEC
)
522 if (vl
== task
->thread
.sve_vl
)
526 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
527 * write any live register state back to task_struct, and convert to a
530 if (task
== current
) {
534 set_thread_flag(TIF_FOREIGN_FPSTATE
);
537 fpsimd_flush_task_state(task
);
538 if (test_and_clear_tsk_thread_flag(task
, TIF_SVE
))
545 * Force reallocation of task SVE state to the correct size
550 task
->thread
.sve_vl
= vl
;
553 if (flags
& PR_SVE_VL_INHERIT
)
554 set_tsk_thread_flag(task
, TIF_SVE_VL_INHERIT
);
556 clear_tsk_thread_flag(task
, TIF_SVE_VL_INHERIT
);
562 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
563 * during secondary boot.
565 static DECLARE_BITMAP(sve_secondary_vq_map
, SVE_VQ_MAX
);
567 static void sve_probe_vqs(DECLARE_BITMAP(map
, SVE_VQ_MAX
))
572 bitmap_zero(map
, SVE_VQ_MAX
);
574 zcr
= ZCR_ELx_LEN_MASK
;
575 zcr
= read_sysreg_s(SYS_ZCR_EL1
) & ~zcr
;
577 for (vq
= SVE_VQ_MAX
; vq
>= SVE_VQ_MIN
; --vq
) {
578 write_sysreg_s(zcr
| (vq
- 1), SYS_ZCR_EL1
); /* self-syncing */
580 vq
= sve_vq_from_vl(vl
); /* skip intervening lengths */
581 set_bit(vq_to_bit(vq
), map
);
585 void __init
sve_init_vq_map(void)
587 sve_probe_vqs(sve_vq_map
);
591 * If we haven't committed to the set of supported VQs yet, filter out
592 * those not supported by the current CPU.
594 void sve_update_vq_map(void)
596 sve_probe_vqs(sve_secondary_vq_map
);
597 bitmap_and(sve_vq_map
, sve_vq_map
, sve_secondary_vq_map
, SVE_VQ_MAX
);
600 /* Check whether the current CPU supports all VQs in the committed set */
601 int sve_verify_vq_map(void)
605 sve_probe_vqs(sve_secondary_vq_map
);
606 bitmap_andnot(sve_secondary_vq_map
, sve_vq_map
, sve_secondary_vq_map
,
608 if (!bitmap_empty(sve_secondary_vq_map
, SVE_VQ_MAX
)) {
609 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
617 static void __init
sve_efi_setup(void)
619 if (!IS_ENABLED(CONFIG_EFI
))
623 * alloc_percpu() warns and prints a backtrace if this goes wrong.
624 * This is evidence of a crippled system and we are returning void,
625 * so no attempt is made to handle this situation here.
627 if (!sve_vl_valid(sve_max_vl
))
630 efi_sve_state
= __alloc_percpu(
631 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl
)), SVE_VQ_BYTES
);
638 panic("Cannot allocate percpu memory for EFI SVE save/restore");
642 * Enable SVE for EL1.
643 * Intended for use by the cpufeatures code during CPU boot.
645 int sve_kernel_enable(void *__always_unused p
)
647 write_sysreg(read_sysreg(CPACR_EL1
) | CPACR_EL1_ZEN_EL1EN
, CPACR_EL1
);
653 void __init
sve_setup(void)
657 if (!system_supports_sve())
661 * The SVE architecture mandates support for 128-bit vectors,
662 * so sve_vq_map must have at least SVE_VQ_MIN set.
663 * If something went wrong, at least try to patch it up:
665 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN
), sve_vq_map
)))
666 set_bit(vq_to_bit(SVE_VQ_MIN
), sve_vq_map
);
668 zcr
= read_sanitised_ftr_reg(SYS_ZCR_EL1
);
669 sve_max_vl
= sve_vl_from_vq((zcr
& ZCR_ELx_LEN_MASK
) + 1);
672 * Sanity-check that the max VL we determined through CPU features
673 * corresponds properly to sve_vq_map. If not, do our best:
675 if (WARN_ON(sve_max_vl
!= find_supported_vector_length(sve_max_vl
)))
676 sve_max_vl
= find_supported_vector_length(sve_max_vl
);
679 * For the default VL, pick the maximum supported value <= 64.
680 * VL == 64 is guaranteed not to grow the signal frame.
682 sve_default_vl
= find_supported_vector_length(64);
684 pr_info("SVE: maximum available vector length %u bytes per vector\n",
686 pr_info("SVE: default vector length %u bytes per vector\n",
693 * Called from the put_task_struct() path, which cannot get here
694 * unless dead_task is really dead and not schedulable.
696 void fpsimd_release_task(struct task_struct
*dead_task
)
698 __sve_free(dead_task
);
701 #endif /* CONFIG_ARM64_SVE */
706 * Storage is allocated for the full SVE state, the current FPSIMD
707 * register contents are migrated across, and TIF_SVE is set so that
708 * the SVE access trap will be disabled the next time this task
709 * reaches ret_to_user.
711 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
712 * would have disabled the SVE access trap for userspace during
713 * ret_to_user, making an SVE access trap impossible in that case.
715 asmlinkage
void do_sve_acc(unsigned int esr
, struct pt_regs
*regs
)
717 /* Even if we chose not to use SVE, the hardware could still trap: */
718 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
719 force_signal_inject(SIGILL
, ILL_ILLOPC
, regs
, 0);
728 fpsimd_to_sve(current
);
730 /* Force ret_to_user to reload the registers: */
731 fpsimd_flush_task_state(current
);
732 set_thread_flag(TIF_FOREIGN_FPSTATE
);
734 if (test_and_set_thread_flag(TIF_SVE
))
735 WARN_ON(1); /* SVE access shouldn't have trapped */
741 * Trapped FP/ASIMD access.
743 asmlinkage
void do_fpsimd_acc(unsigned int esr
, struct pt_regs
*regs
)
745 /* TODO: implement lazy context saving/restoring */
750 * Raise a SIGFPE for the current process.
752 asmlinkage
void do_fpsimd_exc(unsigned int esr
, struct pt_regs
*regs
)
755 unsigned int si_code
= 0;
758 si_code
= FPE_FLTINV
;
759 else if (esr
& FPEXC_DZF
)
760 si_code
= FPE_FLTDIV
;
761 else if (esr
& FPEXC_OFF
)
762 si_code
= FPE_FLTOVF
;
763 else if (esr
& FPEXC_UFF
)
764 si_code
= FPE_FLTUND
;
765 else if (esr
& FPEXC_IXF
)
766 si_code
= FPE_FLTRES
;
768 memset(&info
, 0, sizeof(info
));
769 info
.si_signo
= SIGFPE
;
770 info
.si_code
= si_code
;
771 info
.si_addr
= (void __user
*)instruction_pointer(regs
);
773 send_sig_info(SIGFPE
, &info
, current
);
776 void fpsimd_thread_switch(struct task_struct
*next
)
778 if (!system_supports_fpsimd())
781 * Save the current FPSIMD state to memory, but only if whatever is in
782 * the registers is in fact the most recent userland FPSIMD state of
790 * If we are switching to a task whose most recent userland
791 * FPSIMD state is already in the registers of *this* cpu,
792 * we can skip loading the state from memory. Otherwise, set
793 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
794 * upon the next return to userland.
796 struct fpsimd_state
*st
= &next
->thread
.fpsimd_state
;
798 if (__this_cpu_read(fpsimd_last_state
) == st
799 && st
->cpu
== smp_processor_id())
800 clear_tsk_thread_flag(next
, TIF_FOREIGN_FPSTATE
);
802 set_tsk_thread_flag(next
, TIF_FOREIGN_FPSTATE
);
806 void fpsimd_flush_thread(void)
808 int vl
, supported_vl
;
810 if (!system_supports_fpsimd())
815 memset(¤t
->thread
.fpsimd_state
, 0, sizeof(struct fpsimd_state
));
816 fpsimd_flush_task_state(current
);
818 if (system_supports_sve()) {
819 clear_thread_flag(TIF_SVE
);
823 * Reset the task vector length as required.
824 * This is where we ensure that all user tasks have a valid
825 * vector length configured: no kernel task can become a user
826 * task without an exec and hence a call to this function.
827 * By the time the first call to this function is made, all
828 * early hardware probing is complete, so sve_default_vl
830 * If a bug causes this to go wrong, we make some noise and
831 * try to fudge thread.sve_vl to a safe value here.
833 vl
= current
->thread
.sve_vl_onexec
?
834 current
->thread
.sve_vl_onexec
: sve_default_vl
;
836 if (WARN_ON(!sve_vl_valid(vl
)))
839 supported_vl
= find_supported_vector_length(vl
);
840 if (WARN_ON(supported_vl
!= vl
))
843 current
->thread
.sve_vl
= vl
;
846 * If the task is not set to inherit, ensure that the vector
847 * length will be reset by a subsequent exec:
849 if (!test_thread_flag(TIF_SVE_VL_INHERIT
))
850 current
->thread
.sve_vl_onexec
= 0;
853 set_thread_flag(TIF_FOREIGN_FPSTATE
);
859 * Save the userland FPSIMD state of 'current' to memory, but only if the state
860 * currently held in the registers does in fact belong to 'current'
862 void fpsimd_preserve_current_state(void)
864 if (!system_supports_fpsimd())
873 * Like fpsimd_preserve_current_state(), but ensure that
874 * current->thread.fpsimd_state is updated so that it can be copied to
877 void fpsimd_signal_preserve_current_state(void)
879 fpsimd_preserve_current_state();
880 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
881 sve_to_fpsimd(current
);
885 * Load the userland FPSIMD state of 'current' from memory, but only if the
886 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
889 void fpsimd_restore_current_state(void)
891 if (!system_supports_fpsimd())
896 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE
)) {
897 struct fpsimd_state
*st
= ¤t
->thread
.fpsimd_state
;
900 __this_cpu_write(fpsimd_last_state
, st
);
901 st
->cpu
= smp_processor_id();
908 * Load an updated userland FPSIMD state for 'current' from memory and set the
909 * flag that indicates that the FPSIMD register contents are the most recent
910 * FPSIMD state of 'current'
912 void fpsimd_update_current_state(struct fpsimd_state
*state
)
914 if (!system_supports_fpsimd())
919 if (system_supports_sve() && test_thread_flag(TIF_SVE
)) {
920 current
->thread
.fpsimd_state
= *state
;
921 fpsimd_to_sve(current
);
925 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE
)) {
926 struct fpsimd_state
*st
= ¤t
->thread
.fpsimd_state
;
928 __this_cpu_write(fpsimd_last_state
, st
);
929 st
->cpu
= smp_processor_id();
936 * Invalidate live CPU copies of task t's FPSIMD state
938 void fpsimd_flush_task_state(struct task_struct
*t
)
940 t
->thread
.fpsimd_state
.cpu
= NR_CPUS
;
943 #ifdef CONFIG_KERNEL_MODE_NEON
945 DEFINE_PER_CPU(bool, kernel_neon_busy
);
946 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy
);
949 * Kernel-side NEON support functions
953 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
956 * Must not be called unless may_use_simd() returns true.
957 * Task context in the FPSIMD registers is saved back to memory as necessary.
959 * A matching call to kernel_neon_end() must be made before returning from the
962 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
965 void kernel_neon_begin(void)
967 if (WARN_ON(!system_supports_fpsimd()))
970 BUG_ON(!may_use_simd());
974 __this_cpu_write(kernel_neon_busy
, true);
976 /* Save unsaved task fpsimd state, if any: */
979 set_thread_flag(TIF_FOREIGN_FPSTATE
);
982 /* Invalidate any task state remaining in the fpsimd regs: */
983 __this_cpu_write(fpsimd_last_state
, NULL
);
989 EXPORT_SYMBOL(kernel_neon_begin
);
992 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
994 * Must be called from a context in which kernel_neon_begin() was previously
995 * called, with no call to kernel_neon_end() in the meantime.
997 * The caller must not use the FPSIMD registers after this function is called,
998 * unless kernel_neon_begin() is called again in the meantime.
1000 void kernel_neon_end(void)
1004 if (!system_supports_fpsimd())
1007 busy
= __this_cpu_xchg(kernel_neon_busy
, false);
1008 WARN_ON(!busy
); /* No matching kernel_neon_begin()? */
1012 EXPORT_SYMBOL(kernel_neon_end
);
1016 static DEFINE_PER_CPU(struct fpsimd_state
, efi_fpsimd_state
);
1017 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used
);
1018 static DEFINE_PER_CPU(bool, efi_sve_state_used
);
1021 * EFI runtime services support functions
1023 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1024 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1025 * is always used rather than being an optional accelerator.
1027 * These functions provide the necessary support for ensuring FPSIMD
1028 * save/restore in the contexts from which EFI is used.
1030 * Do not use them for any other purpose -- if tempted to do so, you are
1031 * either doing something wrong or you need to propose some refactoring.
1035 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1037 void __efi_fpsimd_begin(void)
1039 if (!system_supports_fpsimd())
1042 WARN_ON(preemptible());
1044 if (may_use_simd()) {
1045 kernel_neon_begin();
1048 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1051 if (system_supports_sve() && likely(efi_sve_state
)) {
1052 char *sve_state
= this_cpu_ptr(efi_sve_state
);
1054 __this_cpu_write(efi_sve_state_used
, true);
1056 sve_save_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1057 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
);
1059 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state
));
1062 __this_cpu_write(efi_fpsimd_state_used
, true);
1067 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1069 void __efi_fpsimd_end(void)
1071 if (!system_supports_fpsimd())
1074 if (!__this_cpu_xchg(efi_fpsimd_state_used
, false)) {
1077 if (system_supports_sve() &&
1078 likely(__this_cpu_read(efi_sve_state_used
))) {
1079 char const *sve_state
= this_cpu_ptr(efi_sve_state
);
1081 sve_load_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1082 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
,
1083 sve_vq_from_vl(sve_get_vl()) - 1);
1085 __this_cpu_write(efi_sve_state_used
, false);
1087 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state
));
1092 #endif /* CONFIG_EFI */
1094 #endif /* CONFIG_KERNEL_MODE_NEON */
1096 #ifdef CONFIG_CPU_PM
1097 static int fpsimd_cpu_pm_notifier(struct notifier_block
*self
,
1098 unsigned long cmd
, void *v
)
1104 this_cpu_write(fpsimd_last_state
, NULL
);
1108 set_thread_flag(TIF_FOREIGN_FPSTATE
);
1110 case CPU_PM_ENTER_FAILED
:
1117 static struct notifier_block fpsimd_cpu_pm_notifier_block
= {
1118 .notifier_call
= fpsimd_cpu_pm_notifier
,
1121 static void __init
fpsimd_pm_init(void)
1123 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block
);
1127 static inline void fpsimd_pm_init(void) { }
1128 #endif /* CONFIG_CPU_PM */
1130 #ifdef CONFIG_HOTPLUG_CPU
1131 static int fpsimd_cpu_dead(unsigned int cpu
)
1133 per_cpu(fpsimd_last_state
, cpu
) = NULL
;
1137 static inline void fpsimd_hotplug_init(void)
1139 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD
, "arm64/fpsimd:dead",
1140 NULL
, fpsimd_cpu_dead
);
1144 static inline void fpsimd_hotplug_init(void) { }
1148 * FP/SIMD support code initialisation.
1150 static int __init
fpsimd_init(void)
1152 if (elf_hwcap
& HWCAP_FP
) {
1154 fpsimd_hotplug_init();
1156 pr_notice("Floating-point is not implemented\n");
1159 if (!(elf_hwcap
& HWCAP_ASIMD
))
1160 pr_notice("Advanced SIMD is not implemented\n");
1164 late_initcall(fpsimd_init
);