1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * FP/SIMD context switching and fault handling
4 *
5 * Copyright (C) 2012 ARM Ltd.
6 * Author: Catalin Marinas <catalin.marinas@arm.com>
7 */
8
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/bottom_half.h>
12 #include <linux/bug.h>
13 #include <linux/cache.h>
14 #include <linux/compat.h>
15 #include <linux/compiler.h>
16 #include <linux/cpu.h>
17 #include <linux/cpu_pm.h>
18 #include <linux/kernel.h>
19 #include <linux/linkage.h>
20 #include <linux/irqflags.h>
21 #include <linux/init.h>
22 #include <linux/percpu.h>
23 #include <linux/prctl.h>
24 #include <linux/preempt.h>
25 #include <linux/ptrace.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sched/task_stack.h>
28 #include <linux/signal.h>
29 #include <linux/slab.h>
30 #include <linux/stddef.h>
31 #include <linux/sysctl.h>
32 #include <linux/swab.h>
33
34 #include <asm/esr.h>
35 #include <asm/exception.h>
36 #include <asm/fpsimd.h>
37 #include <asm/cpufeature.h>
38 #include <asm/cputype.h>
39 #include <asm/neon.h>
40 #include <asm/processor.h>
41 #include <asm/simd.h>
42 #include <asm/sigcontext.h>
43 #include <asm/sysreg.h>
44 #include <asm/traps.h>
45 #include <asm/virt.h>
46
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)
53
54 /*
55 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
56 *
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.
64 *
65 * For (a), we add a fpsimd_cpu field to thread_struct, 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.
70 *
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_cpu are still mutually in sync. If this is the case, we
75 * can omit the FPSIMD restore.
76 *
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
81 * task. If the task is behaving as a VMM, then this is will be managed by
82 * KVM which will clear it to indicate that the vcpu FPSIMD state is currently
83 * loaded on the CPU, allowing the state to be saved if a FPSIMD-aware
84 * softirq kicks in. Upon vcpu_put(), KVM will save the vcpu FP state and
85 * flag the register state as invalid.
86 *
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 {, __}get_cpu_fpsimd_context(). This will still allow softirqs to
93 * run but prevent them to use FPSIMD.
94 *
95 * For a certain task, the sequence may look something like this:
96 * - the task gets scheduled in; if both the task's fpsimd_cpu field
97 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
98 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
99 * cleared, otherwise it is set;
100 *
101 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
102 * userland FPSIMD state is copied from memory to the registers, the task's
103 * fpsimd_cpu field is set to the id of the current CPU, the current
104 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
105 * TIF_FOREIGN_FPSTATE flag is cleared;
106 *
107 * - the task executes an ordinary syscall; upon return to userland, the
108 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
109 * restored;
110 *
111 * - the task executes a syscall which executes some NEON instructions; this is
112 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
113 * register contents to memory, clears the fpsimd_last_state per-cpu variable
114 * and sets the TIF_FOREIGN_FPSTATE flag;
115 *
116 * - the task gets preempted after kernel_neon_end() is called; as we have not
117 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
118 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
119 */
120 struct fpsimd_last_state_struct {
121 struct user_fpsimd_state *st;
122 void *sve_state;
123 unsigned int sve_vl;
124 };
125
126 static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
127
128 /* Default VL for tasks that don't set it explicitly: */
129 static int __sve_default_vl = -1;
130
get_sve_default_vl(void)131 static int get_sve_default_vl(void)
132 {
133 return READ_ONCE(__sve_default_vl);
134 }
135
136 #ifdef CONFIG_ARM64_SVE
137
set_sve_default_vl(int val)138 static void set_sve_default_vl(int val)
139 {
140 WRITE_ONCE(__sve_default_vl, val);
141 }
142
143 /* Maximum supported vector length across all CPUs (initially poisoned) */
144 int __ro_after_init sve_max_vl = SVE_VL_MIN;
145 int __ro_after_init sve_max_virtualisable_vl = SVE_VL_MIN;
146
147 /*
148 * Set of available vector lengths,
149 * where length vq encoded as bit __vq_to_bit(vq):
150 */
151 __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
152 /* Set of vector lengths present on at least one cpu: */
153 static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
154
155 static void __percpu *efi_sve_state;
156
157 #else /* ! CONFIG_ARM64_SVE */
158
159 /* Dummy declaration for code that will be optimised out: */
160 extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
161 extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
162 extern void __percpu *efi_sve_state;
163
164 #endif /* ! CONFIG_ARM64_SVE */
165
166 DEFINE_PER_CPU(bool, fpsimd_context_busy);
167 EXPORT_PER_CPU_SYMBOL(fpsimd_context_busy);
168
169 static void fpsimd_bind_task_to_cpu(void);
170
__get_cpu_fpsimd_context(void)171 static void __get_cpu_fpsimd_context(void)
172 {
173 bool busy = __this_cpu_xchg(fpsimd_context_busy, true);
174
175 WARN_ON(busy);
176 }
177
178 /*
179 * Claim ownership of the CPU FPSIMD context for use by the calling context.
180 *
181 * The caller may freely manipulate the FPSIMD context metadata until
182 * put_cpu_fpsimd_context() is called.
183 *
184 * The double-underscore version must only be called if you know the task
185 * can't be preempted.
186 */
get_cpu_fpsimd_context(void)187 static void get_cpu_fpsimd_context(void)
188 {
189 local_bh_disable();
190 __get_cpu_fpsimd_context();
191 }
192
__put_cpu_fpsimd_context(void)193 static void __put_cpu_fpsimd_context(void)
194 {
195 bool busy = __this_cpu_xchg(fpsimd_context_busy, false);
196
197 WARN_ON(!busy); /* No matching get_cpu_fpsimd_context()? */
198 }
199
200 /*
201 * Release the CPU FPSIMD context.
202 *
203 * Must be called from a context in which get_cpu_fpsimd_context() was
204 * previously called, with no call to put_cpu_fpsimd_context() in the
205 * meantime.
206 */
put_cpu_fpsimd_context(void)207 static void put_cpu_fpsimd_context(void)
208 {
209 __put_cpu_fpsimd_context();
210 local_bh_enable();
211 }
212
have_cpu_fpsimd_context(void)213 static bool have_cpu_fpsimd_context(void)
214 {
215 return !preemptible() && __this_cpu_read(fpsimd_context_busy);
216 }
217
218 /*
219 * Call __sve_free() directly only if you know task can't be scheduled
220 * or preempted.
221 */
__sve_free(struct task_struct * task)222 static void __sve_free(struct task_struct *task)
223 {
224 kfree(task->thread.sve_state);
225 task->thread.sve_state = NULL;
226 }
227
sve_free(struct task_struct * task)228 static void sve_free(struct task_struct *task)
229 {
230 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
231
232 __sve_free(task);
233 }
234
235 /*
236 * TIF_SVE controls whether a task can use SVE without trapping while
237 * in userspace, and also the way a task's FPSIMD/SVE state is stored
238 * in thread_struct.
239 *
240 * The kernel uses this flag to track whether a user task is actively
241 * using SVE, and therefore whether full SVE register state needs to
242 * be tracked. If not, the cheaper FPSIMD context handling code can
243 * be used instead of the more costly SVE equivalents.
244 *
245 * * TIF_SVE set:
246 *
247 * The task can execute SVE instructions while in userspace without
248 * trapping to the kernel.
249 *
250 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
251 * corresponding Zn), P0-P15 and FFR are encoded in in
252 * task->thread.sve_state, formatted appropriately for vector
253 * length task->thread.sve_vl.
254 *
255 * task->thread.sve_state must point to a valid buffer at least
256 * sve_state_size(task) bytes in size.
257 *
258 * During any syscall, the kernel may optionally clear TIF_SVE and
259 * discard the vector state except for the FPSIMD subset.
260 *
261 * * TIF_SVE clear:
262 *
263 * An attempt by the user task to execute an SVE instruction causes
264 * do_sve_acc() to be called, which does some preparation and then
265 * sets TIF_SVE.
266 *
267 * When stored, FPSIMD registers V0-V31 are encoded in
268 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
269 * logically zero but not stored anywhere; P0-P15 and FFR are not
270 * stored and have unspecified values from userspace's point of
271 * view. For hygiene purposes, the kernel zeroes them on next use,
272 * but userspace is discouraged from relying on this.
273 *
274 * task->thread.sve_state does not need to be non-NULL, valid or any
275 * particular size: it must not be dereferenced.
276 *
277 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
278 * irrespective of whether TIF_SVE is clear or set, since these are
279 * not vector length dependent.
280 */
281
282 /*
283 * Update current's FPSIMD/SVE registers from thread_struct.
284 *
285 * This function should be called only when the FPSIMD/SVE state in
286 * thread_struct is known to be up to date, when preparing to enter
287 * userspace.
288 */
task_fpsimd_load(void)289 static void task_fpsimd_load(void)
290 {
291 WARN_ON(!system_supports_fpsimd());
292 WARN_ON(!have_cpu_fpsimd_context());
293
294 if (IS_ENABLED(CONFIG_ARM64_SVE) && test_thread_flag(TIF_SVE))
295 sve_load_state(sve_pffr(¤t->thread),
296 ¤t->thread.uw.fpsimd_state.fpsr,
297 sve_vq_from_vl(current->thread.sve_vl) - 1);
298 else
299 fpsimd_load_state(¤t->thread.uw.fpsimd_state);
300 }
301
302 /*
303 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
304 * date with respect to the CPU registers.
305 */
fpsimd_save(void)306 static void fpsimd_save(void)
307 {
308 struct fpsimd_last_state_struct const *last =
309 this_cpu_ptr(&fpsimd_last_state);
310 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
311
312 WARN_ON(!system_supports_fpsimd());
313 WARN_ON(!have_cpu_fpsimd_context());
314
315 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
316 if (IS_ENABLED(CONFIG_ARM64_SVE) &&
317 test_thread_flag(TIF_SVE)) {
318 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
319 /*
320 * Can't save the user regs, so current would
321 * re-enter user with corrupt state.
322 * There's no way to recover, so kill it:
323 */
324 force_signal_inject(SIGKILL, SI_KERNEL, 0, 0);
325 return;
326 }
327
328 sve_save_state((char *)last->sve_state +
329 sve_ffr_offset(last->sve_vl),
330 &last->st->fpsr);
331 } else
332 fpsimd_save_state(last->st);
333 }
334 }
335
336 /*
337 * All vector length selection from userspace comes through here.
338 * We're on a slow path, so some sanity-checks are included.
339 * If things go wrong there's a bug somewhere, but try to fall back to a
340 * safe choice.
341 */
find_supported_vector_length(unsigned int vl)342 static unsigned int find_supported_vector_length(unsigned int vl)
343 {
344 int bit;
345 int max_vl = sve_max_vl;
346
347 if (WARN_ON(!sve_vl_valid(vl)))
348 vl = SVE_VL_MIN;
349
350 if (WARN_ON(!sve_vl_valid(max_vl)))
351 max_vl = SVE_VL_MIN;
352
353 if (vl > max_vl)
354 vl = max_vl;
355
356 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
357 __vq_to_bit(sve_vq_from_vl(vl)));
358 return sve_vl_from_vq(__bit_to_vq(bit));
359 }
360
361 #if defined(CONFIG_ARM64_SVE) && defined(CONFIG_SYSCTL)
362
sve_proc_do_default_vl(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)363 static int sve_proc_do_default_vl(struct ctl_table *table, int write,
364 void *buffer, size_t *lenp, loff_t *ppos)
365 {
366 int ret;
367 int vl = get_sve_default_vl();
368 struct ctl_table tmp_table = {
369 .data = &vl,
370 .maxlen = sizeof(vl),
371 };
372
373 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
374 if (ret || !write)
375 return ret;
376
377 /* Writing -1 has the special meaning "set to max": */
378 if (vl == -1)
379 vl = sve_max_vl;
380
381 if (!sve_vl_valid(vl))
382 return -EINVAL;
383
384 set_sve_default_vl(find_supported_vector_length(vl));
385 return 0;
386 }
387
388 static struct ctl_table sve_default_vl_table[] = {
389 {
390 .procname = "sve_default_vector_length",
391 .mode = 0644,
392 .proc_handler = sve_proc_do_default_vl,
393 },
394 { }
395 };
396
sve_sysctl_init(void)397 static int __init sve_sysctl_init(void)
398 {
399 if (system_supports_sve())
400 if (!register_sysctl("abi", sve_default_vl_table))
401 return -EINVAL;
402
403 return 0;
404 }
405
406 #else /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
sve_sysctl_init(void)407 static int __init sve_sysctl_init(void) { return 0; }
408 #endif /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
409
410 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
411 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
412
413 #ifdef CONFIG_CPU_BIG_ENDIAN
arm64_cpu_to_le128(__uint128_t x)414 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
415 {
416 u64 a = swab64(x);
417 u64 b = swab64(x >> 64);
418
419 return ((__uint128_t)a << 64) | b;
420 }
421 #else
arm64_cpu_to_le128(__uint128_t x)422 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
423 {
424 return x;
425 }
426 #endif
427
428 #define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
429
__fpsimd_to_sve(void * sst,struct user_fpsimd_state const * fst,unsigned int vq)430 static void __fpsimd_to_sve(void *sst, struct user_fpsimd_state const *fst,
431 unsigned int vq)
432 {
433 unsigned int i;
434 __uint128_t *p;
435
436 for (i = 0; i < SVE_NUM_ZREGS; ++i) {
437 p = (__uint128_t *)ZREG(sst, vq, i);
438 *p = arm64_cpu_to_le128(fst->vregs[i]);
439 }
440 }
441
442 /*
443 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
444 * task->thread.sve_state.
445 *
446 * Task can be a non-runnable task, or current. In the latter case,
447 * the caller must have ownership of the cpu FPSIMD context before calling
448 * this function.
449 * task->thread.sve_state must point to at least sve_state_size(task)
450 * bytes of allocated kernel memory.
451 * task->thread.uw.fpsimd_state must be up to date before calling this
452 * function.
453 */
fpsimd_to_sve(struct task_struct * task)454 static void fpsimd_to_sve(struct task_struct *task)
455 {
456 unsigned int vq;
457 void *sst = task->thread.sve_state;
458 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
459
460 if (!system_supports_sve())
461 return;
462
463 vq = sve_vq_from_vl(task->thread.sve_vl);
464 __fpsimd_to_sve(sst, fst, vq);
465 }
466
467 /*
468 * Transfer the SVE state in task->thread.sve_state to
469 * task->thread.uw.fpsimd_state.
470 *
471 * Task can be a non-runnable task, or current. In the latter case,
472 * the caller must have ownership of the cpu FPSIMD context before calling
473 * this function.
474 * task->thread.sve_state must point to at least sve_state_size(task)
475 * bytes of allocated kernel memory.
476 * task->thread.sve_state must be up to date before calling this function.
477 */
sve_to_fpsimd(struct task_struct * task)478 static void sve_to_fpsimd(struct task_struct *task)
479 {
480 unsigned int vq;
481 void const *sst = task->thread.sve_state;
482 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
483 unsigned int i;
484 __uint128_t const *p;
485
486 if (!system_supports_sve())
487 return;
488
489 vq = sve_vq_from_vl(task->thread.sve_vl);
490 for (i = 0; i < SVE_NUM_ZREGS; ++i) {
491 p = (__uint128_t const *)ZREG(sst, vq, i);
492 fst->vregs[i] = arm64_le128_to_cpu(*p);
493 }
494 }
495
496 #ifdef CONFIG_ARM64_SVE
497
498 /*
499 * Return how many bytes of memory are required to store the full SVE
500 * state for task, given task's currently configured vector length.
501 */
sve_state_size(struct task_struct const * task)502 size_t sve_state_size(struct task_struct const *task)
503 {
504 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
505 }
506
507 /*
508 * Ensure that task->thread.sve_state is allocated and sufficiently large.
509 *
510 * This function should be used only in preparation for replacing
511 * task->thread.sve_state with new data. The memory is always zeroed
512 * here to prevent stale data from showing through: this is done in
513 * the interest of testability and predictability: except in the
514 * do_sve_acc() case, there is no ABI requirement to hide stale data
515 * written previously be task.
516 */
sve_alloc(struct task_struct * task)517 void sve_alloc(struct task_struct *task)
518 {
519 if (task->thread.sve_state) {
520 memset(task->thread.sve_state, 0, sve_state_size(task));
521 return;
522 }
523
524 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
525 task->thread.sve_state =
526 kzalloc(sve_state_size(task), GFP_KERNEL);
527 }
528
529
530 /*
531 * Ensure that task->thread.sve_state is up to date with respect to
532 * the user task, irrespective of when SVE is in use or not.
533 *
534 * This should only be called by ptrace. task must be non-runnable.
535 * task->thread.sve_state must point to at least sve_state_size(task)
536 * bytes of allocated kernel memory.
537 */
fpsimd_sync_to_sve(struct task_struct * task)538 void fpsimd_sync_to_sve(struct task_struct *task)
539 {
540 if (!test_tsk_thread_flag(task, TIF_SVE))
541 fpsimd_to_sve(task);
542 }
543
544 /*
545 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
546 * the user task, irrespective of whether SVE is in use or not.
547 *
548 * This should only be called by ptrace. task must be non-runnable.
549 * task->thread.sve_state must point to at least sve_state_size(task)
550 * bytes of allocated kernel memory.
551 */
sve_sync_to_fpsimd(struct task_struct * task)552 void sve_sync_to_fpsimd(struct task_struct *task)
553 {
554 if (test_tsk_thread_flag(task, TIF_SVE))
555 sve_to_fpsimd(task);
556 }
557
558 /*
559 * Ensure that task->thread.sve_state is up to date with respect to
560 * the task->thread.uw.fpsimd_state.
561 *
562 * This should only be called by ptrace to merge new FPSIMD register
563 * values into a task for which SVE is currently active.
564 * task must be non-runnable.
565 * task->thread.sve_state must point to at least sve_state_size(task)
566 * bytes of allocated kernel memory.
567 * task->thread.uw.fpsimd_state must already have been initialised with
568 * the new FPSIMD register values to be merged in.
569 */
sve_sync_from_fpsimd_zeropad(struct task_struct * task)570 void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
571 {
572 unsigned int vq;
573 void *sst = task->thread.sve_state;
574 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
575
576 if (!test_tsk_thread_flag(task, TIF_SVE))
577 return;
578
579 vq = sve_vq_from_vl(task->thread.sve_vl);
580
581 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
582 __fpsimd_to_sve(sst, fst, vq);
583 }
584
sve_set_vector_length(struct task_struct * task,unsigned long vl,unsigned long flags)585 int sve_set_vector_length(struct task_struct *task,
586 unsigned long vl, unsigned long flags)
587 {
588 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
589 PR_SVE_SET_VL_ONEXEC))
590 return -EINVAL;
591
592 if (!sve_vl_valid(vl))
593 return -EINVAL;
594
595 /*
596 * Clamp to the maximum vector length that VL-agnostic SVE code can
597 * work with. A flag may be assigned in the future to allow setting
598 * of larger vector lengths without confusing older software.
599 */
600 if (vl > SVE_VL_ARCH_MAX)
601 vl = SVE_VL_ARCH_MAX;
602
603 vl = find_supported_vector_length(vl);
604
605 if (flags & (PR_SVE_VL_INHERIT |
606 PR_SVE_SET_VL_ONEXEC))
607 task->thread.sve_vl_onexec = vl;
608 else
609 /* Reset VL to system default on next exec: */
610 task->thread.sve_vl_onexec = 0;
611
612 /* Only actually set the VL if not deferred: */
613 if (flags & PR_SVE_SET_VL_ONEXEC)
614 goto out;
615
616 if (vl == task->thread.sve_vl)
617 goto out;
618
619 /*
620 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
621 * write any live register state back to task_struct, and convert to a
622 * non-SVE thread.
623 */
624 if (task == current) {
625 get_cpu_fpsimd_context();
626
627 fpsimd_save();
628 }
629
630 fpsimd_flush_task_state(task);
631 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
632 sve_to_fpsimd(task);
633
634 if (task == current)
635 put_cpu_fpsimd_context();
636
637 /*
638 * Force reallocation of task SVE state to the correct size
639 * on next use:
640 */
641 sve_free(task);
642
643 task->thread.sve_vl = vl;
644
645 out:
646 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
647 flags & PR_SVE_VL_INHERIT);
648
649 return 0;
650 }
651
652 /*
653 * Encode the current vector length and flags for return.
654 * This is only required for prctl(): ptrace has separate fields
655 *
656 * flags are as for sve_set_vector_length().
657 */
sve_prctl_status(unsigned long flags)658 static int sve_prctl_status(unsigned long flags)
659 {
660 int ret;
661
662 if (flags & PR_SVE_SET_VL_ONEXEC)
663 ret = current->thread.sve_vl_onexec;
664 else
665 ret = current->thread.sve_vl;
666
667 if (test_thread_flag(TIF_SVE_VL_INHERIT))
668 ret |= PR_SVE_VL_INHERIT;
669
670 return ret;
671 }
672
673 /* PR_SVE_SET_VL */
sve_set_current_vl(unsigned long arg)674 int sve_set_current_vl(unsigned long arg)
675 {
676 unsigned long vl, flags;
677 int ret;
678
679 vl = arg & PR_SVE_VL_LEN_MASK;
680 flags = arg & ~vl;
681
682 if (!system_supports_sve() || is_compat_task())
683 return -EINVAL;
684
685 ret = sve_set_vector_length(current, vl, flags);
686 if (ret)
687 return ret;
688
689 return sve_prctl_status(flags);
690 }
691
692 /* PR_SVE_GET_VL */
sve_get_current_vl(void)693 int sve_get_current_vl(void)
694 {
695 if (!system_supports_sve() || is_compat_task())
696 return -EINVAL;
697
698 return sve_prctl_status(0);
699 }
700
sve_probe_vqs(DECLARE_BITMAP (map,SVE_VQ_MAX))701 static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
702 {
703 unsigned int vq, vl;
704 unsigned long zcr;
705
706 bitmap_zero(map, SVE_VQ_MAX);
707
708 zcr = ZCR_ELx_LEN_MASK;
709 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
710
711 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
712 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
713 vl = sve_get_vl();
714 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
715 set_bit(__vq_to_bit(vq), map);
716 }
717 }
718
719 /*
720 * Initialise the set of known supported VQs for the boot CPU.
721 * This is called during kernel boot, before secondary CPUs are brought up.
722 */
sve_init_vq_map(void)723 void __init sve_init_vq_map(void)
724 {
725 sve_probe_vqs(sve_vq_map);
726 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
727 }
728
729 /*
730 * If we haven't committed to the set of supported VQs yet, filter out
731 * those not supported by the current CPU.
732 * This function is called during the bring-up of early secondary CPUs only.
733 */
sve_update_vq_map(void)734 void sve_update_vq_map(void)
735 {
736 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
737
738 sve_probe_vqs(tmp_map);
739 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
740 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
741 }
742
743 /*
744 * Check whether the current CPU supports all VQs in the committed set.
745 * This function is called during the bring-up of late secondary CPUs only.
746 */
sve_verify_vq_map(void)747 int sve_verify_vq_map(void)
748 {
749 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
750 unsigned long b;
751
752 sve_probe_vqs(tmp_map);
753
754 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
755 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
756 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
757 smp_processor_id());
758 return -EINVAL;
759 }
760
761 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
762 return 0;
763
764 /*
765 * For KVM, it is necessary to ensure that this CPU doesn't
766 * support any vector length that guests may have probed as
767 * unsupported.
768 */
769
770 /* Recover the set of supported VQs: */
771 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
772 /* Find VQs supported that are not globally supported: */
773 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
774
775 /* Find the lowest such VQ, if any: */
776 b = find_last_bit(tmp_map, SVE_VQ_MAX);
777 if (b >= SVE_VQ_MAX)
778 return 0; /* no mismatches */
779
780 /*
781 * Mismatches above sve_max_virtualisable_vl are fine, since
782 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
783 */
784 if (sve_vl_from_vq(__bit_to_vq(b)) <= sve_max_virtualisable_vl) {
785 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
786 smp_processor_id());
787 return -EINVAL;
788 }
789
790 return 0;
791 }
792
sve_efi_setup(void)793 static void __init sve_efi_setup(void)
794 {
795 if (!IS_ENABLED(CONFIG_EFI))
796 return;
797
798 /*
799 * alloc_percpu() warns and prints a backtrace if this goes wrong.
800 * This is evidence of a crippled system and we are returning void,
801 * so no attempt is made to handle this situation here.
802 */
803 if (!sve_vl_valid(sve_max_vl))
804 goto fail;
805
806 efi_sve_state = __alloc_percpu(
807 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
808 if (!efi_sve_state)
809 goto fail;
810
811 return;
812
813 fail:
814 panic("Cannot allocate percpu memory for EFI SVE save/restore");
815 }
816
817 /*
818 * Enable SVE for EL1.
819 * Intended for use by the cpufeatures code during CPU boot.
820 */
sve_kernel_enable(const struct arm64_cpu_capabilities * __always_unused p)821 void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
822 {
823 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
824 isb();
825 }
826
827 /*
828 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
829 * vector length.
830 *
831 * Use only if SVE is present.
832 * This function clobbers the SVE vector length.
833 */
read_zcr_features(void)834 u64 read_zcr_features(void)
835 {
836 u64 zcr;
837 unsigned int vq_max;
838
839 /*
840 * Set the maximum possible VL, and write zeroes to all other
841 * bits to see if they stick.
842 */
843 sve_kernel_enable(NULL);
844 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
845
846 zcr = read_sysreg_s(SYS_ZCR_EL1);
847 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
848 vq_max = sve_vq_from_vl(sve_get_vl());
849 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
850
851 return zcr;
852 }
853
sve_setup(void)854 void __init sve_setup(void)
855 {
856 u64 zcr;
857 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
858 unsigned long b;
859
860 if (!system_supports_sve())
861 return;
862
863 /*
864 * The SVE architecture mandates support for 128-bit vectors,
865 * so sve_vq_map must have at least SVE_VQ_MIN set.
866 * If something went wrong, at least try to patch it up:
867 */
868 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
869 set_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map);
870
871 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
872 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
873
874 /*
875 * Sanity-check that the max VL we determined through CPU features
876 * corresponds properly to sve_vq_map. If not, do our best:
877 */
878 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
879 sve_max_vl = find_supported_vector_length(sve_max_vl);
880
881 /*
882 * For the default VL, pick the maximum supported value <= 64.
883 * VL == 64 is guaranteed not to grow the signal frame.
884 */
885 set_sve_default_vl(find_supported_vector_length(64));
886
887 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
888 SVE_VQ_MAX);
889
890 b = find_last_bit(tmp_map, SVE_VQ_MAX);
891 if (b >= SVE_VQ_MAX)
892 /* No non-virtualisable VLs found */
893 sve_max_virtualisable_vl = SVE_VQ_MAX;
894 else if (WARN_ON(b == SVE_VQ_MAX - 1))
895 /* No virtualisable VLs? This is architecturally forbidden. */
896 sve_max_virtualisable_vl = SVE_VQ_MIN;
897 else /* b + 1 < SVE_VQ_MAX */
898 sve_max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
899
900 if (sve_max_virtualisable_vl > sve_max_vl)
901 sve_max_virtualisable_vl = sve_max_vl;
902
903 pr_info("SVE: maximum available vector length %u bytes per vector\n",
904 sve_max_vl);
905 pr_info("SVE: default vector length %u bytes per vector\n",
906 get_sve_default_vl());
907
908 /* KVM decides whether to support mismatched systems. Just warn here: */
909 if (sve_max_virtualisable_vl < sve_max_vl)
910 pr_warn("SVE: unvirtualisable vector lengths present\n");
911
912 sve_efi_setup();
913 }
914
915 /*
916 * Called from the put_task_struct() path, which cannot get here
917 * unless dead_task is really dead and not schedulable.
918 */
fpsimd_release_task(struct task_struct * dead_task)919 void fpsimd_release_task(struct task_struct *dead_task)
920 {
921 __sve_free(dead_task);
922 }
923
924 #endif /* CONFIG_ARM64_SVE */
925
926 /*
927 * Trapped SVE access
928 *
929 * Storage is allocated for the full SVE state, the current FPSIMD
930 * register contents are migrated across, and the access trap is
931 * disabled.
932 *
933 * TIF_SVE should be clear on entry: otherwise, fpsimd_restore_current_state()
934 * would have disabled the SVE access trap for userspace during
935 * ret_to_user, making an SVE access trap impossible in that case.
936 */
do_sve_acc(unsigned int esr,struct pt_regs * regs)937 void do_sve_acc(unsigned int esr, struct pt_regs *regs)
938 {
939 /* Even if we chose not to use SVE, the hardware could still trap: */
940 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
941 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
942 return;
943 }
944
945 sve_alloc(current);
946 if (!current->thread.sve_state) {
947 force_sig(SIGKILL);
948 return;
949 }
950
951 get_cpu_fpsimd_context();
952
953 if (test_and_set_thread_flag(TIF_SVE))
954 WARN_ON(1); /* SVE access shouldn't have trapped */
955
956 /*
957 * Convert the FPSIMD state to SVE, zeroing all the state that
958 * is not shared with FPSIMD. If (as is likely) the current
959 * state is live in the registers then do this there and
960 * update our metadata for the current task including
961 * disabling the trap, otherwise update our in-memory copy.
962 */
963 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
964 unsigned long vq_minus_one =
965 sve_vq_from_vl(current->thread.sve_vl) - 1;
966 sve_set_vq(vq_minus_one);
967 sve_flush_live(vq_minus_one);
968 fpsimd_bind_task_to_cpu();
969 } else {
970 fpsimd_to_sve(current);
971 }
972
973 put_cpu_fpsimd_context();
974 }
975
976 /*
977 * Trapped FP/ASIMD access.
978 */
do_fpsimd_acc(unsigned int esr,struct pt_regs * regs)979 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
980 {
981 /* TODO: implement lazy context saving/restoring */
982 WARN_ON(1);
983 }
984
985 /*
986 * Raise a SIGFPE for the current process.
987 */
do_fpsimd_exc(unsigned int esr,struct pt_regs * regs)988 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
989 {
990 unsigned int si_code = FPE_FLTUNK;
991
992 if (esr & ESR_ELx_FP_EXC_TFV) {
993 if (esr & FPEXC_IOF)
994 si_code = FPE_FLTINV;
995 else if (esr & FPEXC_DZF)
996 si_code = FPE_FLTDIV;
997 else if (esr & FPEXC_OFF)
998 si_code = FPE_FLTOVF;
999 else if (esr & FPEXC_UFF)
1000 si_code = FPE_FLTUND;
1001 else if (esr & FPEXC_IXF)
1002 si_code = FPE_FLTRES;
1003 }
1004
1005 send_sig_fault(SIGFPE, si_code,
1006 (void __user *)instruction_pointer(regs),
1007 current);
1008 }
1009
fpsimd_thread_switch(struct task_struct * next)1010 void fpsimd_thread_switch(struct task_struct *next)
1011 {
1012 bool wrong_task, wrong_cpu;
1013
1014 if (!system_supports_fpsimd())
1015 return;
1016
1017 __get_cpu_fpsimd_context();
1018
1019 /* Save unsaved fpsimd state, if any: */
1020 fpsimd_save();
1021
1022 /*
1023 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
1024 * state. For kernel threads, FPSIMD registers are never loaded
1025 * and wrong_task and wrong_cpu will always be true.
1026 */
1027 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
1028 &next->thread.uw.fpsimd_state;
1029 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
1030
1031 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
1032 wrong_task || wrong_cpu);
1033
1034 __put_cpu_fpsimd_context();
1035 }
1036
fpsimd_flush_thread(void)1037 void fpsimd_flush_thread(void)
1038 {
1039 int vl, supported_vl;
1040
1041 if (!system_supports_fpsimd())
1042 return;
1043
1044 get_cpu_fpsimd_context();
1045
1046 fpsimd_flush_task_state(current);
1047 memset(¤t->thread.uw.fpsimd_state, 0,
1048 sizeof(current->thread.uw.fpsimd_state));
1049
1050 if (system_supports_sve()) {
1051 clear_thread_flag(TIF_SVE);
1052 sve_free(current);
1053
1054 /*
1055 * Reset the task vector length as required.
1056 * This is where we ensure that all user tasks have a valid
1057 * vector length configured: no kernel task can become a user
1058 * task without an exec and hence a call to this function.
1059 * By the time the first call to this function is made, all
1060 * early hardware probing is complete, so __sve_default_vl
1061 * should be valid.
1062 * If a bug causes this to go wrong, we make some noise and
1063 * try to fudge thread.sve_vl to a safe value here.
1064 */
1065 vl = current->thread.sve_vl_onexec ?
1066 current->thread.sve_vl_onexec : get_sve_default_vl();
1067
1068 if (WARN_ON(!sve_vl_valid(vl)))
1069 vl = SVE_VL_MIN;
1070
1071 supported_vl = find_supported_vector_length(vl);
1072 if (WARN_ON(supported_vl != vl))
1073 vl = supported_vl;
1074
1075 current->thread.sve_vl = vl;
1076
1077 /*
1078 * If the task is not set to inherit, ensure that the vector
1079 * length will be reset by a subsequent exec:
1080 */
1081 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
1082 current->thread.sve_vl_onexec = 0;
1083 }
1084
1085 put_cpu_fpsimd_context();
1086 }
1087
1088 /*
1089 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1090 * currently held in the registers does in fact belong to 'current'
1091 */
fpsimd_preserve_current_state(void)1092 void fpsimd_preserve_current_state(void)
1093 {
1094 if (!system_supports_fpsimd())
1095 return;
1096
1097 get_cpu_fpsimd_context();
1098 fpsimd_save();
1099 put_cpu_fpsimd_context();
1100 }
1101
1102 /*
1103 * Like fpsimd_preserve_current_state(), but ensure that
1104 * current->thread.uw.fpsimd_state is updated so that it can be copied to
1105 * the signal frame.
1106 */
fpsimd_signal_preserve_current_state(void)1107 void fpsimd_signal_preserve_current_state(void)
1108 {
1109 fpsimd_preserve_current_state();
1110 if (test_thread_flag(TIF_SVE))
1111 sve_to_fpsimd(current);
1112 }
1113
1114 /*
1115 * Associate current's FPSIMD context with this cpu
1116 * The caller must have ownership of the cpu FPSIMD context before calling
1117 * this function.
1118 */
fpsimd_bind_task_to_cpu(void)1119 static void fpsimd_bind_task_to_cpu(void)
1120 {
1121 struct fpsimd_last_state_struct *last =
1122 this_cpu_ptr(&fpsimd_last_state);
1123
1124 WARN_ON(!system_supports_fpsimd());
1125 last->st = ¤t->thread.uw.fpsimd_state;
1126 last->sve_state = current->thread.sve_state;
1127 last->sve_vl = current->thread.sve_vl;
1128 current->thread.fpsimd_cpu = smp_processor_id();
1129
1130 if (system_supports_sve()) {
1131 /* Toggle SVE trapping for userspace if needed */
1132 if (test_thread_flag(TIF_SVE))
1133 sve_user_enable();
1134 else
1135 sve_user_disable();
1136
1137 /* Serialised by exception return to user */
1138 }
1139 }
1140
fpsimd_bind_state_to_cpu(struct user_fpsimd_state * st,void * sve_state,unsigned int sve_vl)1141 void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1142 unsigned int sve_vl)
1143 {
1144 struct fpsimd_last_state_struct *last =
1145 this_cpu_ptr(&fpsimd_last_state);
1146
1147 WARN_ON(!system_supports_fpsimd());
1148 WARN_ON(!in_softirq() && !irqs_disabled());
1149
1150 last->st = st;
1151 last->sve_state = sve_state;
1152 last->sve_vl = sve_vl;
1153 }
1154
1155 /*
1156 * Load the userland FPSIMD state of 'current' from memory, but only if the
1157 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1158 * state of 'current'
1159 */
fpsimd_restore_current_state(void)1160 void fpsimd_restore_current_state(void)
1161 {
1162 /*
1163 * For the tasks that were created before we detected the absence of
1164 * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
1165 * e.g, init. This could be then inherited by the children processes.
1166 * If we later detect that the system doesn't support FP/SIMD,
1167 * we must clear the flag for all the tasks to indicate that the
1168 * FPSTATE is clean (as we can't have one) to avoid looping for ever in
1169 * do_notify_resume().
1170 */
1171 if (!system_supports_fpsimd()) {
1172 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1173 return;
1174 }
1175
1176 get_cpu_fpsimd_context();
1177
1178 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
1179 task_fpsimd_load();
1180 fpsimd_bind_task_to_cpu();
1181 }
1182
1183 put_cpu_fpsimd_context();
1184 }
1185
1186 /*
1187 * Load an updated userland FPSIMD state for 'current' from memory and set the
1188 * flag that indicates that the FPSIMD register contents are the most recent
1189 * FPSIMD state of 'current'
1190 */
fpsimd_update_current_state(struct user_fpsimd_state const * state)1191 void fpsimd_update_current_state(struct user_fpsimd_state const *state)
1192 {
1193 if (WARN_ON(!system_supports_fpsimd()))
1194 return;
1195
1196 get_cpu_fpsimd_context();
1197
1198 current->thread.uw.fpsimd_state = *state;
1199 if (test_thread_flag(TIF_SVE))
1200 fpsimd_to_sve(current);
1201
1202 task_fpsimd_load();
1203 fpsimd_bind_task_to_cpu();
1204
1205 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1206
1207 put_cpu_fpsimd_context();
1208 }
1209
1210 /*
1211 * Invalidate live CPU copies of task t's FPSIMD state
1212 *
1213 * This function may be called with preemption enabled. The barrier()
1214 * ensures that the assignment to fpsimd_cpu is visible to any
1215 * preemption/softirq that could race with set_tsk_thread_flag(), so
1216 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1217 *
1218 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1219 * subsequent code.
1220 */
fpsimd_flush_task_state(struct task_struct * t)1221 void fpsimd_flush_task_state(struct task_struct *t)
1222 {
1223 t->thread.fpsimd_cpu = NR_CPUS;
1224 /*
1225 * If we don't support fpsimd, bail out after we have
1226 * reset the fpsimd_cpu for this task and clear the
1227 * FPSTATE.
1228 */
1229 if (!system_supports_fpsimd())
1230 return;
1231 barrier();
1232 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1233
1234 barrier();
1235 }
1236
1237 /*
1238 * Invalidate any task's FPSIMD state that is present on this cpu.
1239 * The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1240 * before calling this function.
1241 */
fpsimd_flush_cpu_state(void)1242 static void fpsimd_flush_cpu_state(void)
1243 {
1244 WARN_ON(!system_supports_fpsimd());
1245 __this_cpu_write(fpsimd_last_state.st, NULL);
1246 set_thread_flag(TIF_FOREIGN_FPSTATE);
1247 }
1248
1249 /*
1250 * Save the FPSIMD state to memory and invalidate cpu view.
1251 * This function must be called with preemption disabled.
1252 */
fpsimd_save_and_flush_cpu_state(void)1253 void fpsimd_save_and_flush_cpu_state(void)
1254 {
1255 if (!system_supports_fpsimd())
1256 return;
1257 WARN_ON(preemptible());
1258 __get_cpu_fpsimd_context();
1259 fpsimd_save();
1260 fpsimd_flush_cpu_state();
1261 __put_cpu_fpsimd_context();
1262 }
1263
1264 #ifdef CONFIG_KERNEL_MODE_NEON
1265
1266 /*
1267 * Kernel-side NEON support functions
1268 */
1269
1270 /*
1271 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1272 * context
1273 *
1274 * Must not be called unless may_use_simd() returns true.
1275 * Task context in the FPSIMD registers is saved back to memory as necessary.
1276 *
1277 * A matching call to kernel_neon_end() must be made before returning from the
1278 * calling context.
1279 *
1280 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1281 * called.
1282 */
kernel_neon_begin(void)1283 void kernel_neon_begin(void)
1284 {
1285 if (WARN_ON(!system_supports_fpsimd()))
1286 return;
1287
1288 BUG_ON(!may_use_simd());
1289
1290 get_cpu_fpsimd_context();
1291
1292 /* Save unsaved fpsimd state, if any: */
1293 fpsimd_save();
1294
1295 /* Invalidate any task state remaining in the fpsimd regs: */
1296 fpsimd_flush_cpu_state();
1297 }
1298 EXPORT_SYMBOL(kernel_neon_begin);
1299
1300 /*
1301 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1302 *
1303 * Must be called from a context in which kernel_neon_begin() was previously
1304 * called, with no call to kernel_neon_end() in the meantime.
1305 *
1306 * The caller must not use the FPSIMD registers after this function is called,
1307 * unless kernel_neon_begin() is called again in the meantime.
1308 */
kernel_neon_end(void)1309 void kernel_neon_end(void)
1310 {
1311 if (!system_supports_fpsimd())
1312 return;
1313
1314 put_cpu_fpsimd_context();
1315 }
1316 EXPORT_SYMBOL(kernel_neon_end);
1317
1318 #ifdef CONFIG_EFI
1319
1320 static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
1321 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
1322 static DEFINE_PER_CPU(bool, efi_sve_state_used);
1323
1324 /*
1325 * EFI runtime services support functions
1326 *
1327 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1328 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1329 * is always used rather than being an optional accelerator.
1330 *
1331 * These functions provide the necessary support for ensuring FPSIMD
1332 * save/restore in the contexts from which EFI is used.
1333 *
1334 * Do not use them for any other purpose -- if tempted to do so, you are
1335 * either doing something wrong or you need to propose some refactoring.
1336 */
1337
1338 /*
1339 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1340 */
__efi_fpsimd_begin(void)1341 void __efi_fpsimd_begin(void)
1342 {
1343 if (!system_supports_fpsimd())
1344 return;
1345
1346 WARN_ON(preemptible());
1347
1348 if (may_use_simd()) {
1349 kernel_neon_begin();
1350 } else {
1351 /*
1352 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1353 * preserving:
1354 */
1355 if (system_supports_sve() && likely(efi_sve_state)) {
1356 char *sve_state = this_cpu_ptr(efi_sve_state);
1357
1358 __this_cpu_write(efi_sve_state_used, true);
1359
1360 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1361 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1362 } else {
1363 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1364 }
1365
1366 __this_cpu_write(efi_fpsimd_state_used, true);
1367 }
1368 }
1369
1370 /*
1371 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1372 */
__efi_fpsimd_end(void)1373 void __efi_fpsimd_end(void)
1374 {
1375 if (!system_supports_fpsimd())
1376 return;
1377
1378 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
1379 kernel_neon_end();
1380 } else {
1381 if (system_supports_sve() &&
1382 likely(__this_cpu_read(efi_sve_state_used))) {
1383 char const *sve_state = this_cpu_ptr(efi_sve_state);
1384
1385 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1386 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1387 sve_vq_from_vl(sve_get_vl()) - 1);
1388
1389 __this_cpu_write(efi_sve_state_used, false);
1390 } else {
1391 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1392 }
1393 }
1394 }
1395
1396 #endif /* CONFIG_EFI */
1397
1398 #endif /* CONFIG_KERNEL_MODE_NEON */
1399
1400 #ifdef CONFIG_CPU_PM
fpsimd_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)1401 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1402 unsigned long cmd, void *v)
1403 {
1404 switch (cmd) {
1405 case CPU_PM_ENTER:
1406 fpsimd_save_and_flush_cpu_state();
1407 break;
1408 case CPU_PM_EXIT:
1409 break;
1410 case CPU_PM_ENTER_FAILED:
1411 default:
1412 return NOTIFY_DONE;
1413 }
1414 return NOTIFY_OK;
1415 }
1416
1417 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1418 .notifier_call = fpsimd_cpu_pm_notifier,
1419 };
1420
fpsimd_pm_init(void)1421 static void __init fpsimd_pm_init(void)
1422 {
1423 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1424 }
1425
1426 #else
fpsimd_pm_init(void)1427 static inline void fpsimd_pm_init(void) { }
1428 #endif /* CONFIG_CPU_PM */
1429
1430 #ifdef CONFIG_HOTPLUG_CPU
fpsimd_cpu_dead(unsigned int cpu)1431 static int fpsimd_cpu_dead(unsigned int cpu)
1432 {
1433 per_cpu(fpsimd_last_state.st, cpu) = NULL;
1434 return 0;
1435 }
1436
fpsimd_hotplug_init(void)1437 static inline void fpsimd_hotplug_init(void)
1438 {
1439 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1440 NULL, fpsimd_cpu_dead);
1441 }
1442
1443 #else
fpsimd_hotplug_init(void)1444 static inline void fpsimd_hotplug_init(void) { }
1445 #endif
1446
1447 /*
1448 * FP/SIMD support code initialisation.
1449 */
fpsimd_init(void)1450 static int __init fpsimd_init(void)
1451 {
1452 if (cpu_have_named_feature(FP)) {
1453 fpsimd_pm_init();
1454 fpsimd_hotplug_init();
1455 } else {
1456 pr_notice("Floating-point is not implemented\n");
1457 }
1458
1459 if (!cpu_have_named_feature(ASIMD))
1460 pr_notice("Advanced SIMD is not implemented\n");
1461
1462 return sve_sysctl_init();
1463 }
1464 core_initcall(fpsimd_init);
1465