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/bitops.h>
22 #include <linux/bottom_half.h>
23 #include <linux/bug.h>
24 #include <linux/cache.h>
25 #include <linux/compat.h>
26 #include <linux/cpu.h>
27 #include <linux/cpu_pm.h>
28 #include <linux/kernel.h>
29 #include <linux/linkage.h>
30 #include <linux/irqflags.h>
31 #include <linux/init.h>
32 #include <linux/percpu.h>
33 #include <linux/prctl.h>
34 #include <linux/preempt.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/stddef.h>
41 #include <linux/sysctl.h>
44 #include <asm/fpsimd.h>
45 #include <asm/cpufeature.h>
46 #include <asm/cputype.h>
47 #include <asm/processor.h>
49 #include <asm/sigcontext.h>
50 #include <asm/sysreg.h>
51 #include <asm/traps.h>
54 #define FPEXC_IOF (1 << 0)
55 #define FPEXC_DZF (1 << 1)
56 #define FPEXC_OFF (1 << 2)
57 #define FPEXC_UFF (1 << 3)
58 #define FPEXC_IXF (1 << 4)
59 #define FPEXC_IDF (1 << 7)
62 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
64 * In order to reduce the number of times the FPSIMD state is needlessly saved
65 * and restored, we need to keep track of two things:
66 * (a) for each task, we need to remember which CPU was the last one to have
67 * the task's FPSIMD state loaded into its FPSIMD registers;
68 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
69 * been loaded into its FPSIMD registers most recently, or whether it has
70 * been used to perform kernel mode NEON in the meantime.
72 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
73 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
74 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
75 * address of the userland FPSIMD state of the task that was loaded onto the CPU
76 * the most recently, or NULL if kernel mode NEON has been performed after that.
78 * With this in place, we no longer have to restore the next FPSIMD state right
79 * when switching between tasks. Instead, we can defer this check to userland
80 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
81 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
82 * can omit the FPSIMD restore.
84 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
85 * indicate whether or not the userland FPSIMD state of the current task is
86 * present in the registers. The flag is set unless the FPSIMD registers of this
87 * CPU currently contain the most recent userland FPSIMD state of the current
90 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
91 * save the task's FPSIMD context back to task_struct from softirq context.
92 * To prevent this from racing with the manipulation of the task's FPSIMD state
93 * from task context and thereby corrupting the state, it is necessary to
94 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
95 * flag with local_bh_disable() unless softirqs are already masked.
97 * For a certain task, the sequence may look something like this:
98 * - the task gets scheduled in; if both the task's fpsimd_cpu field
99 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
100 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
101 * cleared, otherwise it is set;
103 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
104 * userland FPSIMD state is copied from memory to the registers, the task's
105 * fpsimd_cpu field is set to the id of the current CPU, the current
106 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
107 * TIF_FOREIGN_FPSTATE flag is cleared;
109 * - the task executes an ordinary syscall; upon return to userland, the
110 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
113 * - the task executes a syscall which executes some NEON instructions; this is
114 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
115 * register contents to memory, clears the fpsimd_last_state per-cpu variable
116 * and sets the TIF_FOREIGN_FPSTATE flag;
118 * - the task gets preempted after kernel_neon_end() is called; as we have not
119 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
120 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
122 struct fpsimd_last_state_struct
{
123 struct user_fpsimd_state
*st
;
128 static DEFINE_PER_CPU(struct fpsimd_last_state_struct
, fpsimd_last_state
);
130 /* Default VL for tasks that don't set it explicitly: */
131 static int sve_default_vl
= -1;
133 #ifdef CONFIG_ARM64_SVE
135 /* Maximum supported vector length across all CPUs (initially poisoned) */
136 int __ro_after_init sve_max_vl
= SVE_VL_MIN
;
137 int __ro_after_init sve_max_virtualisable_vl
= SVE_VL_MIN
;
140 * Set of available vector lengths,
141 * where length vq encoded as bit __vq_to_bit(vq):
143 __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
144 /* Set of vector lengths present on at least one cpu: */
145 static __ro_after_init
DECLARE_BITMAP(sve_vq_partial_map
, SVE_VQ_MAX
);
147 static void __percpu
*efi_sve_state
;
149 #else /* ! CONFIG_ARM64_SVE */
151 /* Dummy declaration for code that will be optimised out: */
152 extern __ro_after_init
DECLARE_BITMAP(sve_vq_map
, SVE_VQ_MAX
);
153 extern __ro_after_init
DECLARE_BITMAP(sve_vq_partial_map
, SVE_VQ_MAX
);
154 extern void __percpu
*efi_sve_state
;
156 #endif /* ! CONFIG_ARM64_SVE */
159 * Call __sve_free() directly only if you know task can't be scheduled
162 static void __sve_free(struct task_struct
*task
)
164 kfree(task
->thread
.sve_state
);
165 task
->thread
.sve_state
= NULL
;
168 static void sve_free(struct task_struct
*task
)
170 WARN_ON(test_tsk_thread_flag(task
, TIF_SVE
));
176 * TIF_SVE controls whether a task can use SVE without trapping while
177 * in userspace, and also the way a task's FPSIMD/SVE state is stored
180 * The kernel uses this flag to track whether a user task is actively
181 * using SVE, and therefore whether full SVE register state needs to
182 * be tracked. If not, the cheaper FPSIMD context handling code can
183 * be used instead of the more costly SVE equivalents.
187 * The task can execute SVE instructions while in userspace without
188 * trapping to the kernel.
190 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
191 * corresponding Zn), P0-P15 and FFR are encoded in in
192 * task->thread.sve_state, formatted appropriately for vector
193 * length task->thread.sve_vl.
195 * task->thread.sve_state must point to a valid buffer at least
196 * sve_state_size(task) bytes in size.
198 * During any syscall, the kernel may optionally clear TIF_SVE and
199 * discard the vector state except for the FPSIMD subset.
203 * An attempt by the user task to execute an SVE instruction causes
204 * do_sve_acc() to be called, which does some preparation and then
207 * When stored, FPSIMD registers V0-V31 are encoded in
208 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
209 * logically zero but not stored anywhere; P0-P15 and FFR are not
210 * stored and have unspecified values from userspace's point of
211 * view. For hygiene purposes, the kernel zeroes them on next use,
212 * but userspace is discouraged from relying on this.
214 * task->thread.sve_state does not need to be non-NULL, valid or any
215 * particular size: it must not be dereferenced.
217 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
218 * irrespective of whether TIF_SVE is clear or set, since these are
219 * not vector length dependent.
223 * Update current's FPSIMD/SVE registers from thread_struct.
225 * This function should be called only when the FPSIMD/SVE state in
226 * thread_struct is known to be up to date, when preparing to enter
229 * Softirqs (and preemption) must be disabled.
231 static void task_fpsimd_load(void)
233 WARN_ON(!in_softirq() && !irqs_disabled());
235 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
236 sve_load_state(sve_pffr(¤t
->thread
),
237 ¤t
->thread
.uw
.fpsimd_state
.fpsr
,
238 sve_vq_from_vl(current
->thread
.sve_vl
) - 1);
240 fpsimd_load_state(¤t
->thread
.uw
.fpsimd_state
);
244 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
245 * date with respect to the CPU registers.
247 * Softirqs (and preemption) must be disabled.
249 void fpsimd_save(void)
251 struct fpsimd_last_state_struct
const *last
=
252 this_cpu_ptr(&fpsimd_last_state
);
253 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
255 WARN_ON(!in_softirq() && !irqs_disabled());
257 if (!test_thread_flag(TIF_FOREIGN_FPSTATE
)) {
258 if (system_supports_sve() && test_thread_flag(TIF_SVE
)) {
259 if (WARN_ON(sve_get_vl() != last
->sve_vl
)) {
261 * Can't save the user regs, so current would
262 * re-enter user with corrupt state.
263 * There's no way to recover, so kill it:
265 force_signal_inject(SIGKILL
, SI_KERNEL
, 0);
269 sve_save_state((char *)last
->sve_state
+
270 sve_ffr_offset(last
->sve_vl
),
273 fpsimd_save_state(last
->st
);
278 * All vector length selection from userspace comes through here.
279 * We're on a slow path, so some sanity-checks are included.
280 * If things go wrong there's a bug somewhere, but try to fall back to a
283 static unsigned int find_supported_vector_length(unsigned int vl
)
286 int max_vl
= sve_max_vl
;
288 if (WARN_ON(!sve_vl_valid(vl
)))
291 if (WARN_ON(!sve_vl_valid(max_vl
)))
297 bit
= find_next_bit(sve_vq_map
, SVE_VQ_MAX
,
298 __vq_to_bit(sve_vq_from_vl(vl
)));
299 return sve_vl_from_vq(__bit_to_vq(bit
));
304 static int sve_proc_do_default_vl(struct ctl_table
*table
, int write
,
305 void __user
*buffer
, size_t *lenp
,
309 int vl
= sve_default_vl
;
310 struct ctl_table tmp_table
= {
312 .maxlen
= sizeof(vl
),
315 ret
= proc_dointvec(&tmp_table
, write
, buffer
, lenp
, ppos
);
319 /* Writing -1 has the special meaning "set to max": */
323 if (!sve_vl_valid(vl
))
326 sve_default_vl
= find_supported_vector_length(vl
);
330 static struct ctl_table sve_default_vl_table
[] = {
332 .procname
= "sve_default_vector_length",
334 .proc_handler
= sve_proc_do_default_vl
,
339 static int __init
sve_sysctl_init(void)
341 if (system_supports_sve())
342 if (!register_sysctl("abi", sve_default_vl_table
))
348 #else /* ! CONFIG_SYSCTL */
349 static int __init
sve_sysctl_init(void) { return 0; }
350 #endif /* ! CONFIG_SYSCTL */
352 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
353 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
356 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
357 * task->thread.sve_state.
359 * Task can be a non-runnable task, or current. In the latter case,
360 * softirqs (and preemption) must be disabled.
361 * task->thread.sve_state must point to at least sve_state_size(task)
362 * bytes of allocated kernel memory.
363 * task->thread.uw.fpsimd_state must be up to date before calling this
366 static void fpsimd_to_sve(struct task_struct
*task
)
369 void *sst
= task
->thread
.sve_state
;
370 struct user_fpsimd_state
const *fst
= &task
->thread
.uw
.fpsimd_state
;
373 if (!system_supports_sve())
376 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
377 for (i
= 0; i
< 32; ++i
)
378 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
379 sizeof(fst
->vregs
[i
]));
383 * Transfer the SVE state in task->thread.sve_state to
384 * task->thread.uw.fpsimd_state.
386 * Task can be a non-runnable task, or current. In the latter case,
387 * softirqs (and preemption) must be disabled.
388 * task->thread.sve_state must point to at least sve_state_size(task)
389 * bytes of allocated kernel memory.
390 * task->thread.sve_state must be up to date before calling this function.
392 static void sve_to_fpsimd(struct task_struct
*task
)
395 void const *sst
= task
->thread
.sve_state
;
396 struct user_fpsimd_state
*fst
= &task
->thread
.uw
.fpsimd_state
;
399 if (!system_supports_sve())
402 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
403 for (i
= 0; i
< 32; ++i
)
404 memcpy(&fst
->vregs
[i
], ZREG(sst
, vq
, i
),
405 sizeof(fst
->vregs
[i
]));
408 #ifdef CONFIG_ARM64_SVE
411 * Return how many bytes of memory are required to store the full SVE
412 * state for task, given task's currently configured vector length.
414 size_t sve_state_size(struct task_struct
const *task
)
416 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task
->thread
.sve_vl
));
420 * Ensure that task->thread.sve_state is allocated and sufficiently large.
422 * This function should be used only in preparation for replacing
423 * task->thread.sve_state with new data. The memory is always zeroed
424 * here to prevent stale data from showing through: this is done in
425 * the interest of testability and predictability: except in the
426 * do_sve_acc() case, there is no ABI requirement to hide stale data
427 * written previously be task.
429 void sve_alloc(struct task_struct
*task
)
431 if (task
->thread
.sve_state
) {
432 memset(task
->thread
.sve_state
, 0, sve_state_size(current
));
436 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
437 task
->thread
.sve_state
=
438 kzalloc(sve_state_size(task
), GFP_KERNEL
);
441 * If future SVE revisions can have larger vectors though,
442 * this may cease to be true:
444 BUG_ON(!task
->thread
.sve_state
);
449 * Ensure that task->thread.sve_state is up to date with respect to
450 * the user task, irrespective of when SVE is in use or not.
452 * This should only be called by ptrace. task must be non-runnable.
453 * task->thread.sve_state must point to at least sve_state_size(task)
454 * bytes of allocated kernel memory.
456 void fpsimd_sync_to_sve(struct task_struct
*task
)
458 if (!test_tsk_thread_flag(task
, TIF_SVE
))
463 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
464 * the user task, irrespective of whether SVE is in use or not.
466 * This should only be called by ptrace. 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.
470 void sve_sync_to_fpsimd(struct task_struct
*task
)
472 if (test_tsk_thread_flag(task
, TIF_SVE
))
477 * Ensure that task->thread.sve_state is up to date with respect to
478 * the task->thread.uw.fpsimd_state.
480 * This should only be called by ptrace to merge new FPSIMD register
481 * values into a task for which SVE is currently active.
482 * task must be non-runnable.
483 * task->thread.sve_state must point to at least sve_state_size(task)
484 * bytes of allocated kernel memory.
485 * task->thread.uw.fpsimd_state must already have been initialised with
486 * the new FPSIMD register values to be merged in.
488 void sve_sync_from_fpsimd_zeropad(struct task_struct
*task
)
491 void *sst
= task
->thread
.sve_state
;
492 struct user_fpsimd_state
const *fst
= &task
->thread
.uw
.fpsimd_state
;
495 if (!test_tsk_thread_flag(task
, TIF_SVE
))
498 vq
= sve_vq_from_vl(task
->thread
.sve_vl
);
500 memset(sst
, 0, SVE_SIG_REGS_SIZE(vq
));
502 for (i
= 0; i
< 32; ++i
)
503 memcpy(ZREG(sst
, vq
, i
), &fst
->vregs
[i
],
504 sizeof(fst
->vregs
[i
]));
507 int sve_set_vector_length(struct task_struct
*task
,
508 unsigned long vl
, unsigned long flags
)
510 if (flags
& ~(unsigned long)(PR_SVE_VL_INHERIT
|
511 PR_SVE_SET_VL_ONEXEC
))
514 if (!sve_vl_valid(vl
))
518 * Clamp to the maximum vector length that VL-agnostic SVE code can
519 * work with. A flag may be assigned in the future to allow setting
520 * of larger vector lengths without confusing older software.
522 if (vl
> SVE_VL_ARCH_MAX
)
523 vl
= SVE_VL_ARCH_MAX
;
525 vl
= find_supported_vector_length(vl
);
527 if (flags
& (PR_SVE_VL_INHERIT
|
528 PR_SVE_SET_VL_ONEXEC
))
529 task
->thread
.sve_vl_onexec
= vl
;
531 /* Reset VL to system default on next exec: */
532 task
->thread
.sve_vl_onexec
= 0;
534 /* Only actually set the VL if not deferred: */
535 if (flags
& PR_SVE_SET_VL_ONEXEC
)
538 if (vl
== task
->thread
.sve_vl
)
542 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
543 * write any live register state back to task_struct, and convert to a
546 if (task
== current
) {
552 fpsimd_flush_task_state(task
);
553 if (test_and_clear_tsk_thread_flag(task
, TIF_SVE
))
560 * Force reallocation of task SVE state to the correct size
565 task
->thread
.sve_vl
= vl
;
568 update_tsk_thread_flag(task
, TIF_SVE_VL_INHERIT
,
569 flags
& PR_SVE_VL_INHERIT
);
575 * Encode the current vector length and flags for return.
576 * This is only required for prctl(): ptrace has separate fields
578 * flags are as for sve_set_vector_length().
580 static int sve_prctl_status(unsigned long flags
)
584 if (flags
& PR_SVE_SET_VL_ONEXEC
)
585 ret
= current
->thread
.sve_vl_onexec
;
587 ret
= current
->thread
.sve_vl
;
589 if (test_thread_flag(TIF_SVE_VL_INHERIT
))
590 ret
|= PR_SVE_VL_INHERIT
;
596 int sve_set_current_vl(unsigned long arg
)
598 unsigned long vl
, flags
;
601 vl
= arg
& PR_SVE_VL_LEN_MASK
;
604 if (!system_supports_sve())
607 ret
= sve_set_vector_length(current
, vl
, flags
);
611 return sve_prctl_status(flags
);
615 int sve_get_current_vl(void)
617 if (!system_supports_sve())
620 return sve_prctl_status(0);
623 static void sve_probe_vqs(DECLARE_BITMAP(map
, SVE_VQ_MAX
))
628 bitmap_zero(map
, SVE_VQ_MAX
);
630 zcr
= ZCR_ELx_LEN_MASK
;
631 zcr
= read_sysreg_s(SYS_ZCR_EL1
) & ~zcr
;
633 for (vq
= SVE_VQ_MAX
; vq
>= SVE_VQ_MIN
; --vq
) {
634 write_sysreg_s(zcr
| (vq
- 1), SYS_ZCR_EL1
); /* self-syncing */
636 vq
= sve_vq_from_vl(vl
); /* skip intervening lengths */
637 set_bit(__vq_to_bit(vq
), map
);
642 * Initialise the set of known supported VQs for the boot CPU.
643 * This is called during kernel boot, before secondary CPUs are brought up.
645 void __init
sve_init_vq_map(void)
647 sve_probe_vqs(sve_vq_map
);
648 bitmap_copy(sve_vq_partial_map
, sve_vq_map
, SVE_VQ_MAX
);
652 * If we haven't committed to the set of supported VQs yet, filter out
653 * those not supported by the current CPU.
654 * This function is called during the bring-up of early secondary CPUs only.
656 void sve_update_vq_map(void)
658 DECLARE_BITMAP(tmp_map
, SVE_VQ_MAX
);
660 sve_probe_vqs(tmp_map
);
661 bitmap_and(sve_vq_map
, sve_vq_map
, tmp_map
, SVE_VQ_MAX
);
662 bitmap_or(sve_vq_partial_map
, sve_vq_partial_map
, tmp_map
, SVE_VQ_MAX
);
666 * Check whether the current CPU supports all VQs in the committed set.
667 * This function is called during the bring-up of late secondary CPUs only.
669 int sve_verify_vq_map(void)
671 DECLARE_BITMAP(tmp_map
, SVE_VQ_MAX
);
674 sve_probe_vqs(tmp_map
);
676 bitmap_complement(tmp_map
, tmp_map
, SVE_VQ_MAX
);
677 if (bitmap_intersects(tmp_map
, sve_vq_map
, SVE_VQ_MAX
)) {
678 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
683 if (!IS_ENABLED(CONFIG_KVM
) || !is_hyp_mode_available())
687 * For KVM, it is necessary to ensure that this CPU doesn't
688 * support any vector length that guests may have probed as
692 /* Recover the set of supported VQs: */
693 bitmap_complement(tmp_map
, tmp_map
, SVE_VQ_MAX
);
694 /* Find VQs supported that are not globally supported: */
695 bitmap_andnot(tmp_map
, tmp_map
, sve_vq_map
, SVE_VQ_MAX
);
697 /* Find the lowest such VQ, if any: */
698 b
= find_last_bit(tmp_map
, SVE_VQ_MAX
);
700 return 0; /* no mismatches */
703 * Mismatches above sve_max_virtualisable_vl are fine, since
704 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
706 if (sve_vl_from_vq(__bit_to_vq(b
)) <= sve_max_virtualisable_vl
) {
707 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
715 static void __init
sve_efi_setup(void)
717 if (!IS_ENABLED(CONFIG_EFI
))
721 * alloc_percpu() warns and prints a backtrace if this goes wrong.
722 * This is evidence of a crippled system and we are returning void,
723 * so no attempt is made to handle this situation here.
725 if (!sve_vl_valid(sve_max_vl
))
728 efi_sve_state
= __alloc_percpu(
729 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl
)), SVE_VQ_BYTES
);
736 panic("Cannot allocate percpu memory for EFI SVE save/restore");
740 * Enable SVE for EL1.
741 * Intended for use by the cpufeatures code during CPU boot.
743 void sve_kernel_enable(const struct arm64_cpu_capabilities
*__always_unused p
)
745 write_sysreg(read_sysreg(CPACR_EL1
) | CPACR_EL1_ZEN_EL1EN
, CPACR_EL1
);
750 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
753 * Use only if SVE is present.
754 * This function clobbers the SVE vector length.
756 u64
read_zcr_features(void)
762 * Set the maximum possible VL, and write zeroes to all other
763 * bits to see if they stick.
765 sve_kernel_enable(NULL
);
766 write_sysreg_s(ZCR_ELx_LEN_MASK
, SYS_ZCR_EL1
);
768 zcr
= read_sysreg_s(SYS_ZCR_EL1
);
769 zcr
&= ~(u64
)ZCR_ELx_LEN_MASK
; /* find sticky 1s outside LEN field */
770 vq_max
= sve_vq_from_vl(sve_get_vl());
771 zcr
|= vq_max
- 1; /* set LEN field to maximum effective value */
776 void __init
sve_setup(void)
779 DECLARE_BITMAP(tmp_map
, SVE_VQ_MAX
);
782 if (!system_supports_sve())
786 * The SVE architecture mandates support for 128-bit vectors,
787 * so sve_vq_map must have at least SVE_VQ_MIN set.
788 * If something went wrong, at least try to patch it up:
790 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN
), sve_vq_map
)))
791 set_bit(__vq_to_bit(SVE_VQ_MIN
), sve_vq_map
);
793 zcr
= read_sanitised_ftr_reg(SYS_ZCR_EL1
);
794 sve_max_vl
= sve_vl_from_vq((zcr
& ZCR_ELx_LEN_MASK
) + 1);
797 * Sanity-check that the max VL we determined through CPU features
798 * corresponds properly to sve_vq_map. If not, do our best:
800 if (WARN_ON(sve_max_vl
!= find_supported_vector_length(sve_max_vl
)))
801 sve_max_vl
= find_supported_vector_length(sve_max_vl
);
804 * For the default VL, pick the maximum supported value <= 64.
805 * VL == 64 is guaranteed not to grow the signal frame.
807 sve_default_vl
= find_supported_vector_length(64);
809 bitmap_andnot(tmp_map
, sve_vq_partial_map
, sve_vq_map
,
812 b
= find_last_bit(tmp_map
, SVE_VQ_MAX
);
814 /* No non-virtualisable VLs found */
815 sve_max_virtualisable_vl
= SVE_VQ_MAX
;
816 else if (WARN_ON(b
== SVE_VQ_MAX
- 1))
817 /* No virtualisable VLs? This is architecturally forbidden. */
818 sve_max_virtualisable_vl
= SVE_VQ_MIN
;
819 else /* b + 1 < SVE_VQ_MAX */
820 sve_max_virtualisable_vl
= sve_vl_from_vq(__bit_to_vq(b
+ 1));
822 if (sve_max_virtualisable_vl
> sve_max_vl
)
823 sve_max_virtualisable_vl
= sve_max_vl
;
825 pr_info("SVE: maximum available vector length %u bytes per vector\n",
827 pr_info("SVE: default vector length %u bytes per vector\n",
830 /* KVM decides whether to support mismatched systems. Just warn here: */
831 if (sve_max_virtualisable_vl
< sve_max_vl
)
832 pr_warn("SVE: unvirtualisable vector lengths present\n");
838 * Called from the put_task_struct() path, which cannot get here
839 * unless dead_task is really dead and not schedulable.
841 void fpsimd_release_task(struct task_struct
*dead_task
)
843 __sve_free(dead_task
);
846 #endif /* CONFIG_ARM64_SVE */
851 * Storage is allocated for the full SVE state, the current FPSIMD
852 * register contents are migrated across, and TIF_SVE is set so that
853 * the SVE access trap will be disabled the next time this task
854 * reaches ret_to_user.
856 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
857 * would have disabled the SVE access trap for userspace during
858 * ret_to_user, making an SVE access trap impossible in that case.
860 asmlinkage
void do_sve_acc(unsigned int esr
, struct pt_regs
*regs
)
862 /* Even if we chose not to use SVE, the hardware could still trap: */
863 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
864 force_signal_inject(SIGILL
, ILL_ILLOPC
, regs
->pc
);
874 /* Force ret_to_user to reload the registers: */
875 fpsimd_flush_task_state(current
);
877 fpsimd_to_sve(current
);
878 if (test_and_set_thread_flag(TIF_SVE
))
879 WARN_ON(1); /* SVE access shouldn't have trapped */
885 * Trapped FP/ASIMD access.
887 asmlinkage
void do_fpsimd_acc(unsigned int esr
, struct pt_regs
*regs
)
889 /* TODO: implement lazy context saving/restoring */
894 * Raise a SIGFPE for the current process.
896 asmlinkage
void do_fpsimd_exc(unsigned int esr
, struct pt_regs
*regs
)
898 unsigned int si_code
= FPE_FLTUNK
;
900 if (esr
& ESR_ELx_FP_EXC_TFV
) {
902 si_code
= FPE_FLTINV
;
903 else if (esr
& FPEXC_DZF
)
904 si_code
= FPE_FLTDIV
;
905 else if (esr
& FPEXC_OFF
)
906 si_code
= FPE_FLTOVF
;
907 else if (esr
& FPEXC_UFF
)
908 si_code
= FPE_FLTUND
;
909 else if (esr
& FPEXC_IXF
)
910 si_code
= FPE_FLTRES
;
913 send_sig_fault(SIGFPE
, si_code
,
914 (void __user
*)instruction_pointer(regs
),
918 void fpsimd_thread_switch(struct task_struct
*next
)
920 bool wrong_task
, wrong_cpu
;
922 if (!system_supports_fpsimd())
925 /* Save unsaved fpsimd state, if any: */
929 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
930 * state. For kernel threads, FPSIMD registers are never loaded
931 * and wrong_task and wrong_cpu will always be true.
933 wrong_task
= __this_cpu_read(fpsimd_last_state
.st
) !=
934 &next
->thread
.uw
.fpsimd_state
;
935 wrong_cpu
= next
->thread
.fpsimd_cpu
!= smp_processor_id();
937 update_tsk_thread_flag(next
, TIF_FOREIGN_FPSTATE
,
938 wrong_task
|| wrong_cpu
);
941 void fpsimd_flush_thread(void)
943 int vl
, supported_vl
;
945 if (!system_supports_fpsimd())
950 fpsimd_flush_task_state(current
);
951 memset(¤t
->thread
.uw
.fpsimd_state
, 0,
952 sizeof(current
->thread
.uw
.fpsimd_state
));
954 if (system_supports_sve()) {
955 clear_thread_flag(TIF_SVE
);
959 * Reset the task vector length as required.
960 * This is where we ensure that all user tasks have a valid
961 * vector length configured: no kernel task can become a user
962 * task without an exec and hence a call to this function.
963 * By the time the first call to this function is made, all
964 * early hardware probing is complete, so sve_default_vl
966 * If a bug causes this to go wrong, we make some noise and
967 * try to fudge thread.sve_vl to a safe value here.
969 vl
= current
->thread
.sve_vl_onexec
?
970 current
->thread
.sve_vl_onexec
: sve_default_vl
;
972 if (WARN_ON(!sve_vl_valid(vl
)))
975 supported_vl
= find_supported_vector_length(vl
);
976 if (WARN_ON(supported_vl
!= vl
))
979 current
->thread
.sve_vl
= vl
;
982 * If the task is not set to inherit, ensure that the vector
983 * length will be reset by a subsequent exec:
985 if (!test_thread_flag(TIF_SVE_VL_INHERIT
))
986 current
->thread
.sve_vl_onexec
= 0;
993 * Save the userland FPSIMD state of 'current' to memory, but only if the state
994 * currently held in the registers does in fact belong to 'current'
996 void fpsimd_preserve_current_state(void)
998 if (!system_supports_fpsimd())
1007 * Like fpsimd_preserve_current_state(), but ensure that
1008 * current->thread.uw.fpsimd_state is updated so that it can be copied to
1011 void fpsimd_signal_preserve_current_state(void)
1013 fpsimd_preserve_current_state();
1014 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
1015 sve_to_fpsimd(current
);
1019 * Associate current's FPSIMD context with this cpu
1020 * Preemption must be disabled when calling this function.
1022 void fpsimd_bind_task_to_cpu(void)
1024 struct fpsimd_last_state_struct
*last
=
1025 this_cpu_ptr(&fpsimd_last_state
);
1027 last
->st
= ¤t
->thread
.uw
.fpsimd_state
;
1028 last
->sve_state
= current
->thread
.sve_state
;
1029 last
->sve_vl
= current
->thread
.sve_vl
;
1030 current
->thread
.fpsimd_cpu
= smp_processor_id();
1032 if (system_supports_sve()) {
1033 /* Toggle SVE trapping for userspace if needed */
1034 if (test_thread_flag(TIF_SVE
))
1039 /* Serialised by exception return to user */
1043 void fpsimd_bind_state_to_cpu(struct user_fpsimd_state
*st
, void *sve_state
,
1044 unsigned int sve_vl
)
1046 struct fpsimd_last_state_struct
*last
=
1047 this_cpu_ptr(&fpsimd_last_state
);
1049 WARN_ON(!in_softirq() && !irqs_disabled());
1052 last
->sve_state
= sve_state
;
1053 last
->sve_vl
= sve_vl
;
1057 * Load the userland FPSIMD state of 'current' from memory, but only if the
1058 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1059 * state of 'current'
1061 void fpsimd_restore_current_state(void)
1063 if (!system_supports_fpsimd())
1068 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE
)) {
1070 fpsimd_bind_task_to_cpu();
1077 * Load an updated userland FPSIMD state for 'current' from memory and set the
1078 * flag that indicates that the FPSIMD register contents are the most recent
1079 * FPSIMD state of 'current'
1081 void fpsimd_update_current_state(struct user_fpsimd_state
const *state
)
1083 if (!system_supports_fpsimd())
1088 current
->thread
.uw
.fpsimd_state
= *state
;
1089 if (system_supports_sve() && test_thread_flag(TIF_SVE
))
1090 fpsimd_to_sve(current
);
1093 fpsimd_bind_task_to_cpu();
1095 clear_thread_flag(TIF_FOREIGN_FPSTATE
);
1101 * Invalidate live CPU copies of task t's FPSIMD state
1103 * This function may be called with preemption enabled. The barrier()
1104 * ensures that the assignment to fpsimd_cpu is visible to any
1105 * preemption/softirq that could race with set_tsk_thread_flag(), so
1106 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1108 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1111 void fpsimd_flush_task_state(struct task_struct
*t
)
1113 t
->thread
.fpsimd_cpu
= NR_CPUS
;
1116 set_tsk_thread_flag(t
, TIF_FOREIGN_FPSTATE
);
1122 * Invalidate any task's FPSIMD state that is present on this cpu.
1123 * This function must be called with softirqs disabled.
1125 void fpsimd_flush_cpu_state(void)
1127 __this_cpu_write(fpsimd_last_state
.st
, NULL
);
1128 set_thread_flag(TIF_FOREIGN_FPSTATE
);
1131 #ifdef CONFIG_KERNEL_MODE_NEON
1133 DEFINE_PER_CPU(bool, kernel_neon_busy
);
1134 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy
);
1137 * Kernel-side NEON support functions
1141 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1144 * Must not be called unless may_use_simd() returns true.
1145 * Task context in the FPSIMD registers is saved back to memory as necessary.
1147 * A matching call to kernel_neon_end() must be made before returning from the
1150 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1153 void kernel_neon_begin(void)
1155 if (WARN_ON(!system_supports_fpsimd()))
1158 BUG_ON(!may_use_simd());
1162 __this_cpu_write(kernel_neon_busy
, true);
1164 /* Save unsaved fpsimd state, if any: */
1167 /* Invalidate any task state remaining in the fpsimd regs: */
1168 fpsimd_flush_cpu_state();
1174 EXPORT_SYMBOL(kernel_neon_begin
);
1177 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1179 * Must be called from a context in which kernel_neon_begin() was previously
1180 * called, with no call to kernel_neon_end() in the meantime.
1182 * The caller must not use the FPSIMD registers after this function is called,
1183 * unless kernel_neon_begin() is called again in the meantime.
1185 void kernel_neon_end(void)
1189 if (!system_supports_fpsimd())
1192 busy
= __this_cpu_xchg(kernel_neon_busy
, false);
1193 WARN_ON(!busy
); /* No matching kernel_neon_begin()? */
1197 EXPORT_SYMBOL(kernel_neon_end
);
1201 static DEFINE_PER_CPU(struct user_fpsimd_state
, efi_fpsimd_state
);
1202 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used
);
1203 static DEFINE_PER_CPU(bool, efi_sve_state_used
);
1206 * EFI runtime services support functions
1208 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1209 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1210 * is always used rather than being an optional accelerator.
1212 * These functions provide the necessary support for ensuring FPSIMD
1213 * save/restore in the contexts from which EFI is used.
1215 * Do not use them for any other purpose -- if tempted to do so, you are
1216 * either doing something wrong or you need to propose some refactoring.
1220 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1222 void __efi_fpsimd_begin(void)
1224 if (!system_supports_fpsimd())
1227 WARN_ON(preemptible());
1229 if (may_use_simd()) {
1230 kernel_neon_begin();
1233 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1236 if (system_supports_sve() && likely(efi_sve_state
)) {
1237 char *sve_state
= this_cpu_ptr(efi_sve_state
);
1239 __this_cpu_write(efi_sve_state_used
, true);
1241 sve_save_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1242 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
);
1244 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state
));
1247 __this_cpu_write(efi_fpsimd_state_used
, true);
1252 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1254 void __efi_fpsimd_end(void)
1256 if (!system_supports_fpsimd())
1259 if (!__this_cpu_xchg(efi_fpsimd_state_used
, false)) {
1262 if (system_supports_sve() &&
1263 likely(__this_cpu_read(efi_sve_state_used
))) {
1264 char const *sve_state
= this_cpu_ptr(efi_sve_state
);
1266 sve_load_state(sve_state
+ sve_ffr_offset(sve_max_vl
),
1267 &this_cpu_ptr(&efi_fpsimd_state
)->fpsr
,
1268 sve_vq_from_vl(sve_get_vl()) - 1);
1270 __this_cpu_write(efi_sve_state_used
, false);
1272 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state
));
1277 #endif /* CONFIG_EFI */
1279 #endif /* CONFIG_KERNEL_MODE_NEON */
1281 #ifdef CONFIG_CPU_PM
1282 static int fpsimd_cpu_pm_notifier(struct notifier_block
*self
,
1283 unsigned long cmd
, void *v
)
1288 fpsimd_flush_cpu_state();
1292 case CPU_PM_ENTER_FAILED
:
1299 static struct notifier_block fpsimd_cpu_pm_notifier_block
= {
1300 .notifier_call
= fpsimd_cpu_pm_notifier
,
1303 static void __init
fpsimd_pm_init(void)
1305 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block
);
1309 static inline void fpsimd_pm_init(void) { }
1310 #endif /* CONFIG_CPU_PM */
1312 #ifdef CONFIG_HOTPLUG_CPU
1313 static int fpsimd_cpu_dead(unsigned int cpu
)
1315 per_cpu(fpsimd_last_state
.st
, cpu
) = NULL
;
1319 static inline void fpsimd_hotplug_init(void)
1321 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD
, "arm64/fpsimd:dead",
1322 NULL
, fpsimd_cpu_dead
);
1326 static inline void fpsimd_hotplug_init(void) { }
1330 * FP/SIMD support code initialisation.
1332 static int __init
fpsimd_init(void)
1334 if (cpu_have_named_feature(FP
)) {
1336 fpsimd_hotplug_init();
1338 pr_notice("Floating-point is not implemented\n");
1341 if (!cpu_have_named_feature(ASIMD
))
1342 pr_notice("Advanced SIMD is not implemented\n");
1344 return sve_sysctl_init();
1346 core_initcall(fpsimd_init
);