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/prctl.h>
33 #include <linux/preempt.h>
34 #include <linux/prctl.h>
35 #include <linux/ptrace.h>
36 #include <linux/sched/signal.h>
37 #include <linux/sched/task_stack.h>
38 #include <linux/signal.h>
39 #include <linux/slab.h>
40 #include <linux/sysctl.h>
43 #include <asm/fpsimd.h>
44 #include <asm/cpufeature.h>
45 #include <asm/cputype.h>
47 #include <asm/sigcontext.h>
48 #include <asm/sysreg.h>
49 #include <asm/traps.h>
51 #define FPEXC_IOF (1 << 0)
52 #define FPEXC_DZF (1 << 1)
53 #define FPEXC_OFF (1 << 2)
54 #define FPEXC_UFF (1 << 3)
55 #define FPEXC_IXF (1 << 4)
56 #define FPEXC_IDF (1 << 7)
59 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
61 * In order to reduce the number of times the FPSIMD state is needlessly saved
62 * and restored, we need to keep track of two things:
63 * (a) for each task, we need to remember which CPU was the last one to have
64 * the task's FPSIMD state loaded into its FPSIMD registers;
65 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
66 * been loaded into its FPSIMD registers most recently, or whether it has
67 * been used to perform kernel mode NEON in the meantime.
69 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
70 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
71 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
72 * address of the userland FPSIMD state of the task that was loaded onto the CPU
73 * the most recently, or NULL if kernel mode NEON has been performed after that.
75 * With this in place, we no longer have to restore the next FPSIMD state right
76 * when switching between tasks. Instead, we can defer this check to userland
77 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
78 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
79 * can omit the FPSIMD restore.
81 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
82 * indicate whether or not the userland FPSIMD state of the current task is
83 * present in the registers. The flag is set unless the FPSIMD registers of this
84 * CPU currently contain the most recent userland FPSIMD state of the current
87 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
88 * save the task's FPSIMD context back to task_struct from softirq context.
89 * To prevent this from racing with the manipulation of the task's FPSIMD state
90 * from task context and thereby corrupting the state, it is necessary to
91 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
92 * flag with local_bh_disable() unless softirqs are already masked.
94 * For a certain task, the sequence may look something like this:
95 * - the task gets scheduled in; if both the task's fpsimd_cpu field
96 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
97 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
98 * cleared, otherwise it is set;
100 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
101 * userland FPSIMD state is copied from memory to the registers, the task's
102 * fpsimd_cpu field is set to the id of the current CPU, the current
103 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
104 * TIF_FOREIGN_FPSTATE flag is cleared;
106 * - the task executes an ordinary syscall; upon return to userland, the
107 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
110 * - the task executes a syscall which executes some NEON instructions; this is
111 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
112 * register contents to memory, clears the fpsimd_last_state per-cpu variable
113 * and sets the TIF_FOREIGN_FPSTATE flag;
115 * - the task gets preempted after kernel_neon_end() is called; as we have not
116 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
117 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
119 struct fpsimd_last_state_struct
{
120 struct user_fpsimd_state
*st
;
124 static DEFINE_PER_CPU(struct fpsimd_last_state_struct
, fpsimd_last_state
);
126 /* Default VL for tasks that don't set it explicitly: */
127 static int sve_default_vl
= -1;
129 #ifdef CONFIG_ARM64_SVE
131 /* Maximum supported vector length across all CPUs (initially poisoned) */
132 int __ro_after_init sve_max_vl
= -1;
133 /* Set of available vector lengths, as vq_to_bit(vq): */
134 static __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
135 static void __percpu
*efi_sve_state
;
137 #else /* ! CONFIG_ARM64_SVE */
139 /* Dummy declaration for code that will be optimised out: */
140 extern __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
141 extern void __percpu
*efi_sve_state
;
143 #endif /* ! CONFIG_ARM64_SVE */
146 * Call __sve_free() directly only if you know task can't be scheduled
149 static void __sve_free(struct task_struct
*task
)
151 kfree(task
->thread
.sve_state
);
152 task
->thread
.sve_state
= NULL
;
155 static void sve_free(struct task_struct
*task
)
157 WARN_ON(test_tsk_thread_flag(task
, TIF_SVE
));
163 /* Offset of FFR in the SVE register dump */
164 static size_t sve_ffr_offset(int vl
)
166 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl
)) - SVE_SIG_REGS_OFFSET
;
169 static void *sve_pffr(struct task_struct
*task
)
171 return (char *)task
->thread
.sve_state
+
172 sve_ffr_offset(task
->thread
.sve_vl
);
175 static void change_cpacr(u64 val
, u64 mask
)
177 u64 cpacr
= read_sysreg(CPACR_EL1
);
178 u64
new = (cpacr
& ~mask
) | val
;
181 write_sysreg(new, CPACR_EL1
);
184 static void sve_user_disable(void)
186 change_cpacr(0, CPACR_EL1_ZEN_EL0EN
);
189 static void sve_user_enable(void)
191 change_cpacr(CPACR_EL1_ZEN_EL0EN
, CPACR_EL1_ZEN_EL0EN
);
195 * TIF_SVE controls whether a task can use SVE without trapping while
196 * in userspace, and also the way a task's FPSIMD/SVE state is stored
199 * The kernel uses this flag to track whether a user task is actively
200 * using SVE, and therefore whether full SVE register state needs to
201 * be tracked. If not, the cheaper FPSIMD context handling code can
202 * be used instead of the more costly SVE equivalents.
206 * The task can execute SVE instructions while in userspace without
207 * trapping to the kernel.
209 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
210 * corresponding Zn), P0-P15 and FFR are encoded in in
211 * task->thread.sve_state, formatted appropriately for vector
212 * length task->thread.sve_vl.
214 * task->thread.sve_state must point to a valid buffer at least
215 * sve_state_size(task) bytes in size.
217 * During any syscall, the kernel may optionally clear TIF_SVE and
218 * discard the vector state except for the FPSIMD subset.
222 * An attempt by the user task to execute an SVE instruction causes
223 * do_sve_acc() to be called, which does some preparation and then
226 * When stored, FPSIMD registers V0-V31 are encoded in
227 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
228 * logically zero but not stored anywhere; P0-P15 and FFR are not
229 * stored and have unspecified values from userspace's point of
230 * view. For hygiene purposes, the kernel zeroes them on next use,
231 * but userspace is discouraged from relying on this.
233 * task->thread.sve_state does not need to be non-NULL, valid or any
234 * particular size: it must not be dereferenced.
236 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
237 * irrespective of whether TIF_SVE is clear or set, since these are
238 * not vector length dependent.
242 * Update current's FPSIMD/SVE registers from thread_struct.
244 * This function should be called only when the FPSIMD/SVE state in
245 * thread_struct is known to be up to date, when preparing to enter
248 * Softirqs (and preemption) must be disabled.
250 static void task_fpsimd_load(void)
252 WARN_ON(!in_softirq() && !irqs_disabled());
254 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
255 sve_load_state(sve_pffr(current
),
256 ¤t
->thread
.uw
.fpsimd_state
.fpsr
,
257 sve_vq_from_vl(current
->thread
.sve_vl
) - 1);
259 fpsimd_load_state(¤t
->thread
.uw
.fpsimd_state
);
261 if (system_supports_sve()) {
262 /* Toggle SVE trapping for userspace if needed */
263 if (test_thread_flag(TIF_SVE
))
268 /* Serialised by exception return to user */
273 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
274 * date with respect to the CPU registers.
276 * Softirqs (and preemption) must be disabled.
278 static void fpsimd_save(void)
280 struct user_fpsimd_state
*st
= __this_cpu_read(fpsimd_last_state
.st
);
281 /* set by fpsimd_bind_to_cpu() */
283 WARN_ON(!in_softirq() && !irqs_disabled());
285 if (!test_thread_flag(TIF_FOREIGN_FPSTATE
)) {
286 if (system_supports_sve() && test_thread_flag(TIF_SVE
)) {
287 if (WARN_ON(sve_get_vl() != current
->thread
.sve_vl
)) {
289 * Can't save the user regs, so current would
290 * re-enter user with corrupt state.
291 * There's no way to recover, so kill it:
293 force_signal_inject(SIGKILL
, SI_KERNEL
, 0);
297 sve_save_state(sve_pffr(current
), &st
->fpsr
);
299 fpsimd_save_state(st
);
304 * Helpers to translate bit indices in sve_vq_map to VQ values (and
305 * vice versa). This allows find_next_bit() to be used to find the
306 * _maximum_ VQ not exceeding a certain value.
309 static unsigned int vq_to_bit(unsigned int vq
)
311 return SVE_VQ_MAX
- vq
;
314 static unsigned int bit_to_vq(unsigned int bit
)
316 if (WARN_ON(bit
>= SVE_VQ_MAX
))
317 bit
= SVE_VQ_MAX
- 1;
319 return SVE_VQ_MAX
- bit
;
323 * All vector length selection from userspace comes through here.
324 * We're on a slow path, so some sanity-checks are included.
325 * If things go wrong there's a bug somewhere, but try to fall back to a
328 static unsigned int find_supported_vector_length(unsigned int vl
)
331 int max_vl
= sve_max_vl
;
333 if (WARN_ON(!sve_vl_valid(vl
)))
336 if (WARN_ON(!sve_vl_valid(max_vl
)))
342 bit
= find_next_bit(sve_vq_map
, SVE_VQ_MAX
,
343 vq_to_bit(sve_vq_from_vl(vl
)));
344 return sve_vl_from_vq(bit_to_vq(bit
));
349 static int sve_proc_do_default_vl(struct ctl_table
*table
, int write
,
350 void __user
*buffer
, size_t *lenp
,
354 int vl
= sve_default_vl
;
355 struct ctl_table tmp_table
= {
357 .maxlen
= sizeof(vl
),
360 ret
= proc_dointvec(&tmp_table
, write
, buffer
, lenp
, ppos
);
364 /* Writing -1 has the special meaning "set to max": */
366 /* Fail safe if sve_max_vl wasn't initialised */
367 if (WARN_ON(!sve_vl_valid(sve_max_vl
)))
375 if (!sve_vl_valid(vl
))
378 vl
= find_supported_vector_length(vl
);
384 static struct ctl_table sve_default_vl_table
[] = {
386 .procname
= "sve_default_vector_length",
388 .proc_handler
= sve_proc_do_default_vl
,
393 static int __init
sve_sysctl_init(void)
395 if (system_supports_sve())
396 if (!register_sysctl("abi", sve_default_vl_table
))
402 #else /* ! CONFIG_SYSCTL */
403 static int __init
sve_sysctl_init(void) { return 0; }
404 #endif /* ! CONFIG_SYSCTL */
406 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
407 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
410 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
411 * task->thread.sve_state.
413 * Task can be a non-runnable task, or current. In the latter case,
414 * softirqs (and preemption) must be disabled.
415 * task->thread.sve_state must point to at least sve_state_size(task)
416 * bytes of allocated kernel memory.
417 * task->thread.uw.fpsimd_state must be up to date before calling this
420 static void fpsimd_to_sve(struct task_struct
*task
)
423 void *sst
= task
->thread
.sve_state
;
424 struct user_fpsimd_state
const *fst
= &task
->thread
.uw
.fpsimd_state
;
427 if (!system_supports_sve())
430 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
431 for (i
= 0; i
< 32; ++i
)
432 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
433 sizeof(fst
->vregs
[i
]));
437 * Transfer the SVE state in task->thread.sve_state to
438 * task->thread.uw.fpsimd_state.
440 * Task can be a non-runnable task, or current. In the latter case,
441 * softirqs (and preemption) must be disabled.
442 * task->thread.sve_state must point to at least sve_state_size(task)
443 * bytes of allocated kernel memory.
444 * task->thread.sve_state must be up to date before calling this function.
446 static void sve_to_fpsimd(struct task_struct
*task
)
449 void const *sst
= task
->thread
.sve_state
;
450 struct user_fpsimd_state
*fst
= &task
->thread
.uw
.fpsimd_state
;
453 if (!system_supports_sve())
456 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
457 for (i
= 0; i
< 32; ++i
)
458 memcpy(&fst
->vregs
[i
], ZREG(sst
, vq
, i
),
459 sizeof(fst
->vregs
[i
]));
462 #ifdef CONFIG_ARM64_SVE
465 * Return how many bytes of memory are required to store the full SVE
466 * state for task, given task's currently configured vector length.
468 size_t sve_state_size(struct task_struct
const *task
)
470 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task
->thread
.sve_vl
));
474 * Ensure that task->thread.sve_state is allocated and sufficiently large.
476 * This function should be used only in preparation for replacing
477 * task->thread.sve_state with new data. The memory is always zeroed
478 * here to prevent stale data from showing through: this is done in
479 * the interest of testability and predictability: except in the
480 * do_sve_acc() case, there is no ABI requirement to hide stale data
481 * written previously be task.
483 void sve_alloc(struct task_struct
*task
)
485 if (task
->thread
.sve_state
) {
486 memset(task
->thread
.sve_state
, 0, sve_state_size(current
));
490 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
491 task
->thread
.sve_state
=
492 kzalloc(sve_state_size(task
), GFP_KERNEL
);
495 * If future SVE revisions can have larger vectors though,
496 * this may cease to be true:
498 BUG_ON(!task
->thread
.sve_state
);
503 * Ensure that task->thread.sve_state is up to date with respect to
504 * the user task, irrespective of when SVE is in use or not.
506 * This should only be called by ptrace. task must be non-runnable.
507 * task->thread.sve_state must point to at least sve_state_size(task)
508 * bytes of allocated kernel memory.
510 void fpsimd_sync_to_sve(struct task_struct
*task
)
512 if (!test_tsk_thread_flag(task
, TIF_SVE
))
517 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
518 * the user task, irrespective of whether SVE is in use or not.
520 * This should only be called by ptrace. task must be non-runnable.
521 * task->thread.sve_state must point to at least sve_state_size(task)
522 * bytes of allocated kernel memory.
524 void sve_sync_to_fpsimd(struct task_struct
*task
)
526 if (test_tsk_thread_flag(task
, TIF_SVE
))
531 * Ensure that task->thread.sve_state is up to date with respect to
532 * the task->thread.uw.fpsimd_state.
534 * This should only be called by ptrace to merge new FPSIMD register
535 * values into a task for which SVE is currently active.
536 * task must be non-runnable.
537 * task->thread.sve_state must point to at least sve_state_size(task)
538 * bytes of allocated kernel memory.
539 * task->thread.uw.fpsimd_state must already have been initialised with
540 * the new FPSIMD register values to be merged in.
542 void sve_sync_from_fpsimd_zeropad(struct task_struct
*task
)
545 void *sst
= task
->thread
.sve_state
;
546 struct user_fpsimd_state
const *fst
= &task
->thread
.uw
.fpsimd_state
;
549 if (!test_tsk_thread_flag(task
, TIF_SVE
))
552 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
554 memset(sst
, 0, SVE_SIG_REGS_SIZE(vq
));
556 for (i
= 0; i
< 32; ++i
)
557 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
558 sizeof(fst
->vregs
[i
]));
561 int sve_set_vector_length(struct task_struct
*task
,
562 unsigned long vl
, unsigned long flags
)
564 if (flags
& ~(unsigned long)(PR_SVE_VL_INHERIT
|
565 PR_SVE_SET_VL_ONEXEC
))
568 if (!sve_vl_valid(vl
))
572 * Clamp to the maximum vector length that VL-agnostic SVE code can
573 * work with. A flag may be assigned in the future to allow setting
574 * of larger vector lengths without confusing older software.
576 if (vl
> SVE_VL_ARCH_MAX
)
577 vl
= SVE_VL_ARCH_MAX
;
579 vl
= find_supported_vector_length(vl
);
581 if (flags
& (PR_SVE_VL_INHERIT
|
582 PR_SVE_SET_VL_ONEXEC
))
583 task
->thread
.sve_vl_onexec
= vl
;
585 /* Reset VL to system default on next exec: */
586 task
->thread
.sve_vl_onexec
= 0;
588 /* Only actually set the VL if not deferred: */
589 if (flags
& PR_SVE_SET_VL_ONEXEC
)
592 if (vl
== task
->thread
.sve_vl
)
596 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
597 * write any live register state back to task_struct, and convert to a
600 if (task
== current
) {
604 set_thread_flag(TIF_FOREIGN_FPSTATE
);
607 fpsimd_flush_task_state(task
);
608 if (test_and_clear_tsk_thread_flag(task
, TIF_SVE
))
615 * Force reallocation of task SVE state to the correct size
620 task
->thread
.sve_vl
= vl
;
623 update_tsk_thread_flag(task
, TIF_SVE_VL_INHERIT
,
624 flags
& PR_SVE_VL_INHERIT
);
630 * Encode the current vector length and flags for return.
631 * This is only required for prctl(): ptrace has separate fields
633 * flags are as for sve_set_vector_length().
635 static int sve_prctl_status(unsigned long flags
)
639 if (flags
& PR_SVE_SET_VL_ONEXEC
)
640 ret
= current
->thread
.sve_vl_onexec
;
642 ret
= current
->thread
.sve_vl
;
644 if (test_thread_flag(TIF_SVE_VL_INHERIT
))
645 ret
|= PR_SVE_VL_INHERIT
;
651 int sve_set_current_vl(unsigned long arg
)
653 unsigned long vl
, flags
;
656 vl
= arg
& PR_SVE_VL_LEN_MASK
;
659 if (!system_supports_sve())
662 ret
= sve_set_vector_length(current
, vl
, flags
);
666 return sve_prctl_status(flags
);
670 int sve_get_current_vl(void)
672 if (!system_supports_sve())
675 return sve_prctl_status(0);
679 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
680 * during secondary boot.
682 static DECLARE_BITMAP(sve_secondary_vq_map
, SVE_VQ_MAX
);
684 static void sve_probe_vqs(DECLARE_BITMAP(map
, SVE_VQ_MAX
))
689 bitmap_zero(map
, SVE_VQ_MAX
);
691 zcr
= ZCR_ELx_LEN_MASK
;
692 zcr
= read_sysreg_s(SYS_ZCR_EL1
) & ~zcr
;
694 for (vq
= SVE_VQ_MAX
; vq
>= SVE_VQ_MIN
; --vq
) {
695 write_sysreg_s(zcr
| (vq
- 1), SYS_ZCR_EL1
); /* self-syncing */
697 vq
= sve_vq_from_vl(vl
); /* skip intervening lengths */
698 set_bit(vq_to_bit(vq
), map
);
702 void __init
sve_init_vq_map(void)
704 sve_probe_vqs(sve_vq_map
);
708 * If we haven't committed to the set of supported VQs yet, filter out
709 * those not supported by the current CPU.
711 void sve_update_vq_map(void)
713 sve_probe_vqs(sve_secondary_vq_map
);
714 bitmap_and(sve_vq_map
, sve_vq_map
, sve_secondary_vq_map
, SVE_VQ_MAX
);
717 /* Check whether the current CPU supports all VQs in the committed set */
718 int sve_verify_vq_map(void)
722 sve_probe_vqs(sve_secondary_vq_map
);
723 bitmap_andnot(sve_secondary_vq_map
, sve_vq_map
, sve_secondary_vq_map
,
725 if (!bitmap_empty(sve_secondary_vq_map
, SVE_VQ_MAX
)) {
726 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
734 static void __init
sve_efi_setup(void)
736 if (!IS_ENABLED(CONFIG_EFI
))
740 * alloc_percpu() warns and prints a backtrace if this goes wrong.
741 * This is evidence of a crippled system and we are returning void,
742 * so no attempt is made to handle this situation here.
744 if (!sve_vl_valid(sve_max_vl
))
747 efi_sve_state
= __alloc_percpu(
748 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl
)), SVE_VQ_BYTES
);
755 panic("Cannot allocate percpu memory for EFI SVE save/restore");
759 * Enable SVE for EL1.
760 * Intended for use by the cpufeatures code during CPU boot.
762 void sve_kernel_enable(const struct arm64_cpu_capabilities
*__always_unused p
)
764 write_sysreg(read_sysreg(CPACR_EL1
) | CPACR_EL1_ZEN_EL1EN
, CPACR_EL1
);
768 void __init
sve_setup(void)
772 if (!system_supports_sve())
776 * The SVE architecture mandates support for 128-bit vectors,
777 * so sve_vq_map must have at least SVE_VQ_MIN set.
778 * If something went wrong, at least try to patch it up:
780 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN
), sve_vq_map
)))
781 set_bit(vq_to_bit(SVE_VQ_MIN
), sve_vq_map
);
783 zcr
= read_sanitised_ftr_reg(SYS_ZCR_EL1
);
784 sve_max_vl
= sve_vl_from_vq((zcr
& ZCR_ELx_LEN_MASK
) + 1);
787 * Sanity-check that the max VL we determined through CPU features
788 * corresponds properly to sve_vq_map. If not, do our best:
790 if (WARN_ON(sve_max_vl
!= find_supported_vector_length(sve_max_vl
)))
791 sve_max_vl
= find_supported_vector_length(sve_max_vl
);
794 * For the default VL, pick the maximum supported value <= 64.
795 * VL == 64 is guaranteed not to grow the signal frame.
797 sve_default_vl
= find_supported_vector_length(64);
799 pr_info("SVE: maximum available vector length %u bytes per vector\n",
801 pr_info("SVE: default vector length %u bytes per vector\n",
808 * Called from the put_task_struct() path, which cannot get here
809 * unless dead_task is really dead and not schedulable.
811 void fpsimd_release_task(struct task_struct
*dead_task
)
813 __sve_free(dead_task
);
816 #endif /* CONFIG_ARM64_SVE */
821 * Storage is allocated for the full SVE state, the current FPSIMD
822 * register contents are migrated across, and TIF_SVE is set so that
823 * the SVE access trap will be disabled the next time this task
824 * reaches ret_to_user.
826 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
827 * would have disabled the SVE access trap for userspace during
828 * ret_to_user, making an SVE access trap impossible in that case.
830 asmlinkage
void do_sve_acc(unsigned int esr
, struct pt_regs
*regs
)
832 /* Even if we chose not to use SVE, the hardware could still trap: */
833 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
834 force_signal_inject(SIGILL
, ILL_ILLOPC
, regs
->pc
);
843 fpsimd_to_sve(current
);
845 /* Force ret_to_user to reload the registers: */
846 fpsimd_flush_task_state(current
);
847 set_thread_flag(TIF_FOREIGN_FPSTATE
);
849 if (test_and_set_thread_flag(TIF_SVE
))
850 WARN_ON(1); /* SVE access shouldn't have trapped */
856 * Trapped FP/ASIMD access.
858 asmlinkage
void do_fpsimd_acc(unsigned int esr
, struct pt_regs
*regs
)
860 /* TODO: implement lazy context saving/restoring */
865 * Raise a SIGFPE for the current process.
867 asmlinkage
void do_fpsimd_exc(unsigned int esr
, struct pt_regs
*regs
)
870 unsigned int si_code
= FPE_FLTUNK
;
872 if (esr
& ESR_ELx_FP_EXC_TFV
) {
874 si_code
= FPE_FLTINV
;
875 else if (esr
& FPEXC_DZF
)
876 si_code
= FPE_FLTDIV
;
877 else if (esr
& FPEXC_OFF
)
878 si_code
= FPE_FLTOVF
;
879 else if (esr
& FPEXC_UFF
)
880 si_code
= FPE_FLTUND
;
881 else if (esr
& FPEXC_IXF
)
882 si_code
= FPE_FLTRES
;
885 memset(&info
, 0, sizeof(info
));
886 info
.si_signo
= SIGFPE
;
887 info
.si_code
= si_code
;
888 info
.si_addr
= (void __user
*)instruction_pointer(regs
);
890 send_sig_info(SIGFPE
, &info
, current
);
893 void fpsimd_thread_switch(struct task_struct
*next
)
895 if (!system_supports_fpsimd())
898 * Save the current FPSIMD state to memory, but only if whatever is in
899 * the registers is in fact the most recent userland FPSIMD state of
907 * If we are switching to a task whose most recent userland
908 * FPSIMD state is already in the registers of *this* cpu,
909 * we can skip loading the state from memory. Otherwise, set
910 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
911 * upon the next return to userland.
913 bool wrong_task
= __this_cpu_read(fpsimd_last_state
.st
) !=
914 &next
->thread
.uw
.fpsimd_state
;
915 bool wrong_cpu
= next
->thread
.fpsimd_cpu
!= smp_processor_id();
917 update_tsk_thread_flag(next
, TIF_FOREIGN_FPSTATE
,
918 wrong_task
|| wrong_cpu
);
922 void fpsimd_flush_thread(void)
924 int vl
, supported_vl
;
926 if (!system_supports_fpsimd())
931 memset(¤t
->thread
.uw
.fpsimd_state
, 0,
932 sizeof(current
->thread
.uw
.fpsimd_state
));
933 fpsimd_flush_task_state(current
);
935 if (system_supports_sve()) {
936 clear_thread_flag(TIF_SVE
);
940 * Reset the task vector length as required.
941 * This is where we ensure that all user tasks have a valid
942 * vector length configured: no kernel task can become a user
943 * task without an exec and hence a call to this function.
944 * By the time the first call to this function is made, all
945 * early hardware probing is complete, so sve_default_vl
947 * If a bug causes this to go wrong, we make some noise and
948 * try to fudge thread.sve_vl to a safe value here.
950 vl
= current
->thread
.sve_vl_onexec
?
951 current
->thread
.sve_vl_onexec
: sve_default_vl
;
953 if (WARN_ON(!sve_vl_valid(vl
)))
956 supported_vl
= find_supported_vector_length(vl
);
957 if (WARN_ON(supported_vl
!= vl
))
960 current
->thread
.sve_vl
= vl
;
963 * If the task is not set to inherit, ensure that the vector
964 * length will be reset by a subsequent exec:
966 if (!test_thread_flag(TIF_SVE_VL_INHERIT
))
967 current
->thread
.sve_vl_onexec
= 0;
970 set_thread_flag(TIF_FOREIGN_FPSTATE
);
976 * Save the userland FPSIMD state of 'current' to memory, but only if the state
977 * currently held in the registers does in fact belong to 'current'
979 void fpsimd_preserve_current_state(void)
981 if (!system_supports_fpsimd())
990 * Like fpsimd_preserve_current_state(), but ensure that
991 * current->thread.uw.fpsimd_state is updated so that it can be copied to
994 void fpsimd_signal_preserve_current_state(void)
996 fpsimd_preserve_current_state();
997 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
998 sve_to_fpsimd(current
);
1002 * Associate current's FPSIMD context with this cpu
1003 * Preemption must be disabled when calling this function.
1005 static void fpsimd_bind_to_cpu(void)
1007 struct fpsimd_last_state_struct
*last
=
1008 this_cpu_ptr(&fpsimd_last_state
);
1010 last
->st
= ¤t
->thread
.uw
.fpsimd_state
;
1011 last
->sve_in_use
= test_thread_flag(TIF_SVE
);
1012 current
->thread
.fpsimd_cpu
= smp_processor_id();
1016 * Load the userland FPSIMD state of 'current' from memory, but only if the
1017 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1018 * state of 'current'
1020 void fpsimd_restore_current_state(void)
1022 if (!system_supports_fpsimd())
1027 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE
)) {
1029 fpsimd_bind_to_cpu();
1036 * Load an updated userland FPSIMD state for 'current' from memory and set the
1037 * flag that indicates that the FPSIMD register contents are the most recent
1038 * FPSIMD state of 'current'
1040 void fpsimd_update_current_state(struct user_fpsimd_state
const *state
)
1042 if (!system_supports_fpsimd())
1047 current
->thread
.uw
.fpsimd_state
= *state
;
1048 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
1049 fpsimd_to_sve(current
);
1053 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE
))
1054 fpsimd_bind_to_cpu();
1060 * Invalidate live CPU copies of task t's FPSIMD state
1062 void fpsimd_flush_task_state(struct task_struct
*t
)
1064 t
->thread
.fpsimd_cpu
= NR_CPUS
;
1067 static inline void fpsimd_flush_cpu_state(void)
1069 __this_cpu_write(fpsimd_last_state
.st
, NULL
);
1070 set_thread_flag(TIF_FOREIGN_FPSTATE
);
1074 * Invalidate any task SVE state currently held in this CPU's regs.
1076 * This is used to prevent the kernel from trying to reuse SVE register data
1077 * that is detroyed by KVM guest enter/exit. This function should go away when
1078 * KVM SVE support is implemented. Don't use it for anything else.
1080 #ifdef CONFIG_ARM64_SVE
1081 void sve_flush_cpu_state(void)
1083 struct fpsimd_last_state_struct
const *last
=
1084 this_cpu_ptr(&fpsimd_last_state
);
1086 if (last
->st
&& last
->sve_in_use
)
1087 fpsimd_flush_cpu_state();
1089 #endif /* CONFIG_ARM64_SVE */
1091 #ifdef CONFIG_KERNEL_MODE_NEON
1093 DEFINE_PER_CPU(bool, kernel_neon_busy
);
1094 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy
);
1097 * Kernel-side NEON support functions
1101 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1104 * Must not be called unless may_use_simd() returns true.
1105 * Task context in the FPSIMD registers is saved back to memory as necessary.
1107 * A matching call to kernel_neon_end() must be made before returning from the
1110 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1113 void kernel_neon_begin(void)
1115 if (WARN_ON(!system_supports_fpsimd()))
1118 BUG_ON(!may_use_simd());
1122 __this_cpu_write(kernel_neon_busy
, true);
1124 /* Save unsaved task fpsimd state, if any: */
1128 /* Invalidate any task state remaining in the fpsimd regs: */
1129 fpsimd_flush_cpu_state();
1135 EXPORT_SYMBOL(kernel_neon_begin
);
1138 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1140 * Must be called from a context in which kernel_neon_begin() was previously
1141 * called, with no call to kernel_neon_end() in the meantime.
1143 * The caller must not use the FPSIMD registers after this function is called,
1144 * unless kernel_neon_begin() is called again in the meantime.
1146 void kernel_neon_end(void)
1150 if (!system_supports_fpsimd())
1153 busy
= __this_cpu_xchg(kernel_neon_busy
, false);
1154 WARN_ON(!busy
); /* No matching kernel_neon_begin()? */
1158 EXPORT_SYMBOL(kernel_neon_end
);
1162 static DEFINE_PER_CPU(struct user_fpsimd_state
, efi_fpsimd_state
);
1163 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used
);
1164 static DEFINE_PER_CPU(bool, efi_sve_state_used
);
1167 * EFI runtime services support functions
1169 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1170 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1171 * is always used rather than being an optional accelerator.
1173 * These functions provide the necessary support for ensuring FPSIMD
1174 * save/restore in the contexts from which EFI is used.
1176 * Do not use them for any other purpose -- if tempted to do so, you are
1177 * either doing something wrong or you need to propose some refactoring.
1181 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1183 void __efi_fpsimd_begin(void)
1185 if (!system_supports_fpsimd())
1188 WARN_ON(preemptible());
1190 if (may_use_simd()) {
1191 kernel_neon_begin();
1194 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1197 if (system_supports_sve() && likely(efi_sve_state
)) {
1198 char *sve_state
= this_cpu_ptr(efi_sve_state
);
1200 __this_cpu_write(efi_sve_state_used
, true);
1202 sve_save_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1203 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
);
1205 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state
));
1208 __this_cpu_write(efi_fpsimd_state_used
, true);
1213 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1215 void __efi_fpsimd_end(void)
1217 if (!system_supports_fpsimd())
1220 if (!__this_cpu_xchg(efi_fpsimd_state_used
, false)) {
1223 if (system_supports_sve() &&
1224 likely(__this_cpu_read(efi_sve_state_used
))) {
1225 char const *sve_state
= this_cpu_ptr(efi_sve_state
);
1227 sve_load_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1228 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
,
1229 sve_vq_from_vl(sve_get_vl()) - 1);
1231 __this_cpu_write(efi_sve_state_used
, false);
1233 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state
));
1238 #endif /* CONFIG_EFI */
1240 #endif /* CONFIG_KERNEL_MODE_NEON */
1242 #ifdef CONFIG_CPU_PM
1243 static int fpsimd_cpu_pm_notifier(struct notifier_block
*self
,
1244 unsigned long cmd
, void *v
)
1250 fpsimd_flush_cpu_state();
1254 case CPU_PM_ENTER_FAILED
:
1261 static struct notifier_block fpsimd_cpu_pm_notifier_block
= {
1262 .notifier_call
= fpsimd_cpu_pm_notifier
,
1265 static void __init
fpsimd_pm_init(void)
1267 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block
);
1271 static inline void fpsimd_pm_init(void) { }
1272 #endif /* CONFIG_CPU_PM */
1274 #ifdef CONFIG_HOTPLUG_CPU
1275 static int fpsimd_cpu_dead(unsigned int cpu
)
1277 per_cpu(fpsimd_last_state
.st
, cpu
) = NULL
;
1281 static inline void fpsimd_hotplug_init(void)
1283 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD
, "arm64/fpsimd:dead",
1284 NULL
, fpsimd_cpu_dead
);
1288 static inline void fpsimd_hotplug_init(void) { }
1292 * FP/SIMD support code initialisation.
1294 static int __init
fpsimd_init(void)
1296 if (elf_hwcap
& HWCAP_FP
) {
1298 fpsimd_hotplug_init();
1300 pr_notice("Floating-point is not implemented\n");
1303 if (!(elf_hwcap
& HWCAP_ASIMD
))
1304 pr_notice("Advanced SIMD is not implemented\n");
1306 return sve_sysctl_init();
1308 core_initcall(fpsimd_init
);