blob: 667be3472114285b285ab33c970ebccbc5e33b77 [file] [log] [blame]
Catalin Marinas53631b52012-03-05 11:49:32 +00001/*
2 * FP/SIMD context switching and fault handling
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
6 *
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.
10 *
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.
15 *
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/>.
18 */
19
Dave Martin7582e222017-10-31 15:51:08 +000020#include <linux/bitmap.h>
Dave Martincb84d112017-08-03 17:23:23 +010021#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000022#include <linux/bug.h>
Dave Martin7582e222017-10-31 15:51:08 +000023#include <linux/cache.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000024#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080025#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010026#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000027#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000028#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000029#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000030#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010031#include <linux/percpu.h>
Dave Martin4328825d2017-08-03 17:23:22 +010032#include <linux/preempt.h>
Dave Martin7582e222017-10-31 15:51:08 +000033#include <linux/prctl.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000034#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010035#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000036#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000037#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000038#include <linux/slab.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000039
40#include <asm/fpsimd.h>
41#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010042#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000043#include <asm/sigcontext.h>
44#include <asm/sysreg.h>
45#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000046
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/*
Dave Martinbc0ee472017-10-31 15:51:05 +000055 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
56 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020057 * 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 'cpu' field to struct fpsimd_state, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080066 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020067 * 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_state.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.
82 *
Dave Martincb84d112017-08-03 17:23:23 +010083 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
84 * save the task's FPSIMD context back to task_struct from softirq context.
85 * To prevent this from racing with the manipulation of the task's FPSIMD state
86 * from task context and thereby corrupting the state, it is necessary to
87 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
88 * flag with local_bh_disable() unless softirqs are already masked.
89 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020090 * For a certain task, the sequence may look something like this:
91 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
92 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
93 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
94 * cleared, otherwise it is set;
95 *
96 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
97 * userland FPSIMD state is copied from memory to the registers, the task's
98 * fpsimd_state.cpu field is set to the id of the current CPU, the current
99 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
100 * TIF_FOREIGN_FPSTATE flag is cleared;
101 *
102 * - the task executes an ordinary syscall; upon return to userland, the
103 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
104 * restored;
105 *
106 * - the task executes a syscall which executes some NEON instructions; this is
107 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
108 * register contents to memory, clears the fpsimd_last_state per-cpu variable
109 * and sets the TIF_FOREIGN_FPSTATE flag;
110 *
111 * - the task gets preempted after kernel_neon_end() is called; as we have not
112 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
113 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
114 */
115static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
116
Dave Martin79ab0472017-10-31 15:51:06 +0000117/* Default VL for tasks that don't set it explicitly: */
118static int sve_default_vl = SVE_VL_MIN;
119
Dave Martin7582e222017-10-31 15:51:08 +0000120#ifdef CONFIG_ARM64_SVE
121
122/* Maximum supported vector length across all CPUs (initially poisoned) */
123int __ro_after_init sve_max_vl = -1;
124/* Set of available vector lengths, as vq_to_bit(vq): */
125static DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
126
127#else /* ! CONFIG_ARM64_SVE */
128
129/* Dummy declaration for code that will be optimised out: */
130extern DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
131
132#endif /* ! CONFIG_ARM64_SVE */
133
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200134/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000135 * Call __sve_free() directly only if you know task can't be scheduled
136 * or preempted.
137 */
138static void __sve_free(struct task_struct *task)
139{
140 kfree(task->thread.sve_state);
141 task->thread.sve_state = NULL;
142}
143
144static void sve_free(struct task_struct *task)
145{
146 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
147
148 __sve_free(task);
149}
150
151
152/* Offset of FFR in the SVE register dump */
153static size_t sve_ffr_offset(int vl)
154{
155 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
156}
157
158static void *sve_pffr(struct task_struct *task)
159{
160 return (char *)task->thread.sve_state +
161 sve_ffr_offset(task->thread.sve_vl);
162}
163
164static void change_cpacr(u64 val, u64 mask)
165{
166 u64 cpacr = read_sysreg(CPACR_EL1);
167 u64 new = (cpacr & ~mask) | val;
168
169 if (new != cpacr)
170 write_sysreg(new, CPACR_EL1);
171}
172
173static void sve_user_disable(void)
174{
175 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
176}
177
178static void sve_user_enable(void)
179{
180 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
181}
182
183/*
184 * TIF_SVE controls whether a task can use SVE without trapping while
185 * in userspace, and also the way a task's FPSIMD/SVE state is stored
186 * in thread_struct.
187 *
188 * The kernel uses this flag to track whether a user task is actively
189 * using SVE, and therefore whether full SVE register state needs to
190 * be tracked. If not, the cheaper FPSIMD context handling code can
191 * be used instead of the more costly SVE equivalents.
192 *
193 * * TIF_SVE set:
194 *
195 * The task can execute SVE instructions while in userspace without
196 * trapping to the kernel.
197 *
198 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
199 * corresponding Zn), P0-P15 and FFR are encoded in in
200 * task->thread.sve_state, formatted appropriately for vector
201 * length task->thread.sve_vl.
202 *
203 * task->thread.sve_state must point to a valid buffer at least
204 * sve_state_size(task) bytes in size.
205 *
206 * During any syscall, the kernel may optionally clear TIF_SVE and
207 * discard the vector state except for the FPSIMD subset.
208 *
209 * * TIF_SVE clear:
210 *
211 * An attempt by the user task to execute an SVE instruction causes
212 * do_sve_acc() to be called, which does some preparation and then
213 * sets TIF_SVE.
214 *
215 * When stored, FPSIMD registers V0-V31 are encoded in
216 * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
217 * logically zero but not stored anywhere; P0-P15 and FFR are not
218 * stored and have unspecified values from userspace's point of
219 * view. For hygiene purposes, the kernel zeroes them on next use,
220 * but userspace is discouraged from relying on this.
221 *
222 * task->thread.sve_state does not need to be non-NULL, valid or any
223 * particular size: it must not be dereferenced.
224 *
225 * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
226 * whether TIF_SVE is clear or set, since these are not vector length
227 * dependent.
228 */
229
230/*
231 * Update current's FPSIMD/SVE registers from thread_struct.
232 *
233 * This function should be called only when the FPSIMD/SVE state in
234 * thread_struct is known to be up to date, when preparing to enter
235 * userspace.
236 *
237 * Softirqs (and preemption) must be disabled.
238 */
239static void task_fpsimd_load(void)
240{
241 WARN_ON(!in_softirq() && !irqs_disabled());
242
243 if (system_supports_sve() && test_thread_flag(TIF_SVE))
244 sve_load_state(sve_pffr(current),
245 &current->thread.fpsimd_state.fpsr,
246 sve_vq_from_vl(current->thread.sve_vl) - 1);
247 else
248 fpsimd_load_state(&current->thread.fpsimd_state);
249
250 if (system_supports_sve()) {
251 /* Toggle SVE trapping for userspace if needed */
252 if (test_thread_flag(TIF_SVE))
253 sve_user_enable();
254 else
255 sve_user_disable();
256
257 /* Serialised by exception return to user */
258 }
259}
260
261/*
262 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
263 * with respect to the CPU registers.
264 *
265 * Softirqs (and preemption) must be disabled.
266 */
267static void task_fpsimd_save(void)
268{
269 WARN_ON(!in_softirq() && !irqs_disabled());
270
271 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
272 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
273 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
274 /*
275 * Can't save the user regs, so current would
276 * re-enter user with corrupt state.
277 * There's no way to recover, so kill it:
278 */
279 force_signal_inject(
280 SIGKILL, 0, current_pt_regs(), 0);
281 return;
282 }
283
284 sve_save_state(sve_pffr(current),
285 &current->thread.fpsimd_state.fpsr);
286 } else
287 fpsimd_save_state(&current->thread.fpsimd_state);
288 }
289}
290
Dave Martin7582e222017-10-31 15:51:08 +0000291/*
292 * Helpers to translate bit indices in sve_vq_map to VQ values (and
293 * vice versa). This allows find_next_bit() to be used to find the
294 * _maximum_ VQ not exceeding a certain value.
295 */
296
297static unsigned int vq_to_bit(unsigned int vq)
298{
299 return SVE_VQ_MAX - vq;
300}
301
302static unsigned int bit_to_vq(unsigned int bit)
303{
304 if (WARN_ON(bit >= SVE_VQ_MAX))
305 bit = SVE_VQ_MAX - 1;
306
307 return SVE_VQ_MAX - bit;
308}
309
310/*
311 * All vector length selection from userspace comes through here.
312 * We're on a slow path, so some sanity-checks are included.
313 * If things go wrong there's a bug somewhere, but try to fall back to a
314 * safe choice.
315 */
316static unsigned int find_supported_vector_length(unsigned int vl)
317{
318 int bit;
319 int max_vl = sve_max_vl;
320
321 if (WARN_ON(!sve_vl_valid(vl)))
322 vl = SVE_VL_MIN;
323
324 if (WARN_ON(!sve_vl_valid(max_vl)))
325 max_vl = SVE_VL_MIN;
326
327 if (vl > max_vl)
328 vl = max_vl;
329
330 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
331 vq_to_bit(sve_vq_from_vl(vl)));
332 return sve_vl_from_vq(bit_to_vq(bit));
333}
334
Dave Martinbc0ee472017-10-31 15:51:05 +0000335#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
336 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
337
338/*
339 * Transfer the FPSIMD state in task->thread.fpsimd_state to
340 * task->thread.sve_state.
341 *
342 * Task can be a non-runnable task, or current. In the latter case,
343 * softirqs (and preemption) must be disabled.
344 * task->thread.sve_state must point to at least sve_state_size(task)
345 * bytes of allocated kernel memory.
346 * task->thread.fpsimd_state must be up to date before calling this function.
347 */
348static void fpsimd_to_sve(struct task_struct *task)
349{
350 unsigned int vq;
351 void *sst = task->thread.sve_state;
352 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
353 unsigned int i;
354
355 if (!system_supports_sve())
356 return;
357
358 vq = sve_vq_from_vl(task->thread.sve_vl);
359 for (i = 0; i < 32; ++i)
360 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
361 sizeof(fst->vregs[i]));
362}
363
Dave Martin8cd969d2017-10-31 15:51:07 +0000364/*
365 * Transfer the SVE state in task->thread.sve_state to
366 * task->thread.fpsimd_state.
367 *
368 * Task can be a non-runnable task, or current. In the latter case,
369 * softirqs (and preemption) must be disabled.
370 * task->thread.sve_state must point to at least sve_state_size(task)
371 * bytes of allocated kernel memory.
372 * task->thread.sve_state must be up to date before calling this function.
373 */
374static void sve_to_fpsimd(struct task_struct *task)
375{
376 unsigned int vq;
377 void const *sst = task->thread.sve_state;
378 struct fpsimd_state *fst = &task->thread.fpsimd_state;
379 unsigned int i;
380
381 if (!system_supports_sve())
382 return;
383
384 vq = sve_vq_from_vl(task->thread.sve_vl);
385 for (i = 0; i < 32; ++i)
386 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
387 sizeof(fst->vregs[i]));
388}
389
Dave Martinbc0ee472017-10-31 15:51:05 +0000390#ifdef CONFIG_ARM64_SVE
391
392/*
393 * Return how many bytes of memory are required to store the full SVE
394 * state for task, given task's currently configured vector length.
395 */
396size_t sve_state_size(struct task_struct const *task)
397{
398 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
399}
400
401/*
402 * Ensure that task->thread.sve_state is allocated and sufficiently large.
403 *
404 * This function should be used only in preparation for replacing
405 * task->thread.sve_state with new data. The memory is always zeroed
406 * here to prevent stale data from showing through: this is done in
407 * the interest of testability and predictability: except in the
408 * do_sve_acc() case, there is no ABI requirement to hide stale data
409 * written previously be task.
410 */
411void sve_alloc(struct task_struct *task)
412{
413 if (task->thread.sve_state) {
414 memset(task->thread.sve_state, 0, sve_state_size(current));
415 return;
416 }
417
418 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
419 task->thread.sve_state =
420 kzalloc(sve_state_size(task), GFP_KERNEL);
421
422 /*
423 * If future SVE revisions can have larger vectors though,
424 * this may cease to be true:
425 */
426 BUG_ON(!task->thread.sve_state);
427}
428
Dave Martin7582e222017-10-31 15:51:08 +0000429int sve_set_vector_length(struct task_struct *task,
430 unsigned long vl, unsigned long flags)
431{
432 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
433 PR_SVE_SET_VL_ONEXEC))
434 return -EINVAL;
435
436 if (!sve_vl_valid(vl))
437 return -EINVAL;
438
439 /*
440 * Clamp to the maximum vector length that VL-agnostic SVE code can
441 * work with. A flag may be assigned in the future to allow setting
442 * of larger vector lengths without confusing older software.
443 */
444 if (vl > SVE_VL_ARCH_MAX)
445 vl = SVE_VL_ARCH_MAX;
446
447 vl = find_supported_vector_length(vl);
448
449 if (flags & (PR_SVE_VL_INHERIT |
450 PR_SVE_SET_VL_ONEXEC))
451 task->thread.sve_vl_onexec = vl;
452 else
453 /* Reset VL to system default on next exec: */
454 task->thread.sve_vl_onexec = 0;
455
456 /* Only actually set the VL if not deferred: */
457 if (flags & PR_SVE_SET_VL_ONEXEC)
458 goto out;
459
460 if (vl == task->thread.sve_vl)
461 goto out;
462
463 /*
464 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
465 * write any live register state back to task_struct, and convert to a
466 * non-SVE thread.
467 */
468 if (task == current) {
469 local_bh_disable();
470
471 task_fpsimd_save();
472 set_thread_flag(TIF_FOREIGN_FPSTATE);
473 }
474
475 fpsimd_flush_task_state(task);
476 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
477 sve_to_fpsimd(task);
478
479 if (task == current)
480 local_bh_enable();
481
482 /*
483 * Force reallocation of task SVE state to the correct size
484 * on next use:
485 */
486 sve_free(task);
487
488 task->thread.sve_vl = vl;
489
490out:
491 if (flags & PR_SVE_VL_INHERIT)
492 set_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
493 else
494 clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
495
496 return 0;
497}
498
Dave Martinbc0ee472017-10-31 15:51:05 +0000499/*
500 * Called from the put_task_struct() path, which cannot get here
501 * unless dead_task is really dead and not schedulable.
502 */
503void fpsimd_release_task(struct task_struct *dead_task)
504{
505 __sve_free(dead_task);
506}
507
508#endif /* CONFIG_ARM64_SVE */
509
510/*
511 * Trapped SVE access
512 *
513 * Storage is allocated for the full SVE state, the current FPSIMD
514 * register contents are migrated across, and TIF_SVE is set so that
515 * the SVE access trap will be disabled the next time this task
516 * reaches ret_to_user.
517 *
518 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
519 * would have disabled the SVE access trap for userspace during
520 * ret_to_user, making an SVE access trap impossible in that case.
521 */
522asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
523{
524 /* Even if we chose not to use SVE, the hardware could still trap: */
525 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
526 force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
527 return;
528 }
529
530 sve_alloc(current);
531
532 local_bh_disable();
533
534 task_fpsimd_save();
535 fpsimd_to_sve(current);
536
537 /* Force ret_to_user to reload the registers: */
538 fpsimd_flush_task_state(current);
539 set_thread_flag(TIF_FOREIGN_FPSTATE);
540
541 if (test_and_set_thread_flag(TIF_SVE))
542 WARN_ON(1); /* SVE access shouldn't have trapped */
543
544 local_bh_enable();
545}
546
547/*
Catalin Marinas53631b52012-03-05 11:49:32 +0000548 * Trapped FP/ASIMD access.
549 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000550asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000551{
552 /* TODO: implement lazy context saving/restoring */
553 WARN_ON(1);
554}
555
556/*
557 * Raise a SIGFPE for the current process.
558 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000559asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000560{
561 siginfo_t info;
562 unsigned int si_code = 0;
563
564 if (esr & FPEXC_IOF)
565 si_code = FPE_FLTINV;
566 else if (esr & FPEXC_DZF)
567 si_code = FPE_FLTDIV;
568 else if (esr & FPEXC_OFF)
569 si_code = FPE_FLTOVF;
570 else if (esr & FPEXC_UFF)
571 si_code = FPE_FLTUND;
572 else if (esr & FPEXC_IXF)
573 si_code = FPE_FLTRES;
574
575 memset(&info, 0, sizeof(info));
576 info.si_signo = SIGFPE;
577 info.si_code = si_code;
578 info.si_addr = (void __user *)instruction_pointer(regs);
579
580 send_sig_info(SIGFPE, &info, current);
581}
582
583void fpsimd_thread_switch(struct task_struct *next)
584{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000585 if (!system_supports_fpsimd())
586 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200587 /*
588 * Save the current FPSIMD state to memory, but only if whatever is in
589 * the registers is in fact the most recent userland FPSIMD state of
590 * 'current'.
591 */
Dave Martinbc0ee472017-10-31 15:51:05 +0000592 if (current->mm)
593 task_fpsimd_save();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200594
595 if (next->mm) {
596 /*
597 * If we are switching to a task whose most recent userland
598 * FPSIMD state is already in the registers of *this* cpu,
599 * we can skip loading the state from memory. Otherwise, set
600 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
601 * upon the next return to userland.
602 */
603 struct fpsimd_state *st = &next->thread.fpsimd_state;
604
605 if (__this_cpu_read(fpsimd_last_state) == st
606 && st->cpu == smp_processor_id())
Dave Martin9cf5b542017-10-31 15:50:59 +0000607 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200608 else
Dave Martin9cf5b542017-10-31 15:50:59 +0000609 set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200610 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000611}
612
613void fpsimd_flush_thread(void)
614{
Dave Martin7582e222017-10-31 15:51:08 +0000615 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000616
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000617 if (!system_supports_fpsimd())
618 return;
Dave Martincb84d112017-08-03 17:23:23 +0100619
620 local_bh_disable();
621
Catalin Marinas53631b52012-03-05 11:49:32 +0000622 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100623 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000624
625 if (system_supports_sve()) {
626 clear_thread_flag(TIF_SVE);
627 sve_free(current);
628
629 /*
630 * Reset the task vector length as required.
631 * This is where we ensure that all user tasks have a valid
632 * vector length configured: no kernel task can become a user
633 * task without an exec and hence a call to this function.
634 * If a bug causes this to go wrong, we make some noise and
635 * try to fudge thread.sve_vl to a safe value here.
636 */
Dave Martin79ab0472017-10-31 15:51:06 +0000637 vl = current->thread.sve_vl_onexec ?
638 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000639
640 if (WARN_ON(!sve_vl_valid(vl)))
641 vl = SVE_VL_MIN;
642
Dave Martin7582e222017-10-31 15:51:08 +0000643 supported_vl = find_supported_vector_length(vl);
644 if (WARN_ON(supported_vl != vl))
645 vl = supported_vl;
646
Dave Martinbc0ee472017-10-31 15:51:05 +0000647 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000648
649 /*
650 * If the task is not set to inherit, ensure that the vector
651 * length will be reset by a subsequent exec:
652 */
653 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
654 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000655 }
656
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200657 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100658
659 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000660}
661
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100662/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200663 * Save the userland FPSIMD state of 'current' to memory, but only if the state
664 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100665 */
666void fpsimd_preserve_current_state(void)
667{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000668 if (!system_supports_fpsimd())
669 return;
Dave Martincb84d112017-08-03 17:23:23 +0100670
671 local_bh_disable();
Dave Martin8cd969d2017-10-31 15:51:07 +0000672 task_fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100673 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100674}
675
676/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000677 * Like fpsimd_preserve_current_state(), but ensure that
678 * current->thread.fpsimd_state is updated so that it can be copied to
679 * the signal frame.
680 */
681void fpsimd_signal_preserve_current_state(void)
682{
683 fpsimd_preserve_current_state();
684 if (system_supports_sve() && test_thread_flag(TIF_SVE))
685 sve_to_fpsimd(current);
686}
687
688/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200689 * Load the userland FPSIMD state of 'current' from memory, but only if the
690 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
691 * state of 'current'
692 */
693void fpsimd_restore_current_state(void)
694{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000695 if (!system_supports_fpsimd())
696 return;
Dave Martincb84d112017-08-03 17:23:23 +0100697
698 local_bh_disable();
699
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200700 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
701 struct fpsimd_state *st = &current->thread.fpsimd_state;
702
Dave Martinbc0ee472017-10-31 15:51:05 +0000703 task_fpsimd_load();
Dave Martin50464182017-08-03 17:23:21 +0100704 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200705 st->cpu = smp_processor_id();
706 }
Dave Martincb84d112017-08-03 17:23:23 +0100707
708 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200709}
710
711/*
712 * Load an updated userland FPSIMD state for 'current' from memory and set the
713 * flag that indicates that the FPSIMD register contents are the most recent
714 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100715 */
716void fpsimd_update_current_state(struct fpsimd_state *state)
717{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000718 if (!system_supports_fpsimd())
719 return;
Dave Martincb84d112017-08-03 17:23:23 +0100720
721 local_bh_disable();
722
Dave Martin8cd969d2017-10-31 15:51:07 +0000723 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
724 current->thread.fpsimd_state = *state;
725 fpsimd_to_sve(current);
726 }
727 task_fpsimd_load();
728
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200729 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
730 struct fpsimd_state *st = &current->thread.fpsimd_state;
731
Dave Martin50464182017-08-03 17:23:21 +0100732 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200733 st->cpu = smp_processor_id();
734 }
Dave Martincb84d112017-08-03 17:23:23 +0100735
736 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100737}
738
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200739/*
740 * Invalidate live CPU copies of task t's FPSIMD state
741 */
742void fpsimd_flush_task_state(struct task_struct *t)
743{
744 t->thread.fpsimd_state.cpu = NR_CPUS;
745}
746
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100747#ifdef CONFIG_KERNEL_MODE_NEON
748
Dave Martincb84d112017-08-03 17:23:23 +0100749DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +0100750EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100751
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100752/*
753 * Kernel-side NEON support functions
754 */
Dave Martincb84d112017-08-03 17:23:23 +0100755
756/*
757 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
758 * context
759 *
760 * Must not be called unless may_use_simd() returns true.
761 * Task context in the FPSIMD registers is saved back to memory as necessary.
762 *
763 * A matching call to kernel_neon_end() must be made before returning from the
764 * calling context.
765 *
766 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
767 * called.
768 */
769void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100770{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000771 if (WARN_ON(!system_supports_fpsimd()))
772 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100773
Dave Martincb84d112017-08-03 17:23:23 +0100774 BUG_ON(!may_use_simd());
775
776 local_bh_disable();
777
778 __this_cpu_write(kernel_neon_busy, true);
779
780 /* Save unsaved task fpsimd state, if any: */
781 if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
782 fpsimd_save_state(&current->thread.fpsimd_state);
783
784 /* Invalidate any task state remaining in the fpsimd regs: */
785 __this_cpu_write(fpsimd_last_state, NULL);
786
787 preempt_disable();
788
789 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100790}
Dave Martincb84d112017-08-03 17:23:23 +0100791EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100792
Dave Martincb84d112017-08-03 17:23:23 +0100793/*
794 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
795 *
796 * Must be called from a context in which kernel_neon_begin() was previously
797 * called, with no call to kernel_neon_end() in the meantime.
798 *
799 * The caller must not use the FPSIMD registers after this function is called,
800 * unless kernel_neon_begin() is called again in the meantime.
801 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100802void kernel_neon_end(void)
803{
Dave Martincb84d112017-08-03 17:23:23 +0100804 bool busy;
805
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000806 if (!system_supports_fpsimd())
807 return;
Dave Martincb84d112017-08-03 17:23:23 +0100808
809 busy = __this_cpu_xchg(kernel_neon_busy, false);
810 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
811
812 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100813}
814EXPORT_SYMBOL(kernel_neon_end);
815
Dave Martine580b8b2017-09-18 09:40:12 +0100816#ifdef CONFIG_EFI
817
Dave Martin3b660232017-08-18 14:53:47 +0100818static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
819static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +0100820
821/*
822 * EFI runtime services support functions
823 *
824 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
825 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
826 * is always used rather than being an optional accelerator.
827 *
828 * These functions provide the necessary support for ensuring FPSIMD
829 * save/restore in the contexts from which EFI is used.
830 *
831 * Do not use them for any other purpose -- if tempted to do so, you are
832 * either doing something wrong or you need to propose some refactoring.
833 */
834
835/*
836 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
837 */
838void __efi_fpsimd_begin(void)
839{
840 if (!system_supports_fpsimd())
841 return;
842
843 WARN_ON(preemptible());
844
845 if (may_use_simd())
846 kernel_neon_begin();
847 else {
848 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
849 __this_cpu_write(efi_fpsimd_state_used, true);
850 }
851}
852
853/*
854 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
855 */
856void __efi_fpsimd_end(void)
857{
858 if (!system_supports_fpsimd())
859 return;
860
861 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
862 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
863 else
864 kernel_neon_end();
865}
866
Dave Martine580b8b2017-09-18 09:40:12 +0100867#endif /* CONFIG_EFI */
868
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100869#endif /* CONFIG_KERNEL_MODE_NEON */
870
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100871#ifdef CONFIG_CPU_PM
872static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
873 unsigned long cmd, void *v)
874{
875 switch (cmd) {
876 case CPU_PM_ENTER:
Dave Martinbc0ee472017-10-31 15:51:05 +0000877 if (current->mm)
878 task_fpsimd_save();
Leo Yan7c68a9c2014-09-01 11:09:51 +0800879 this_cpu_write(fpsimd_last_state, NULL);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100880 break;
881 case CPU_PM_EXIT:
882 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200883 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100884 break;
885 case CPU_PM_ENTER_FAILED:
886 default:
887 return NOTIFY_DONE;
888 }
889 return NOTIFY_OK;
890}
891
892static struct notifier_block fpsimd_cpu_pm_notifier_block = {
893 .notifier_call = fpsimd_cpu_pm_notifier,
894};
895
Jisheng Zhanga7c61a32015-11-20 17:59:10 +0800896static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100897{
898 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
899}
900
901#else
902static inline void fpsimd_pm_init(void) { }
903#endif /* CONFIG_CPU_PM */
904
Janet Liu32365e62015-06-11 12:02:45 +0800905#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200906static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +0800907{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200908 per_cpu(fpsimd_last_state, cpu) = NULL;
909 return 0;
Janet Liu32365e62015-06-11 12:02:45 +0800910}
911
Janet Liu32365e62015-06-11 12:02:45 +0800912static inline void fpsimd_hotplug_init(void)
913{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200914 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
915 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +0800916}
917
918#else
919static inline void fpsimd_hotplug_init(void) { }
920#endif
921
Catalin Marinas53631b52012-03-05 11:49:32 +0000922/*
923 * FP/SIMD support code initialisation.
924 */
925static int __init fpsimd_init(void)
926{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100927 if (elf_hwcap & HWCAP_FP) {
928 fpsimd_pm_init();
929 fpsimd_hotplug_init();
930 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +0000931 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +0000932 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000933
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100934 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +0000935 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100936
Catalin Marinas53631b52012-03-05 11:49:32 +0000937 return 0;
938}
939late_initcall(fpsimd_init);