blob: 794dd990da82e692b51d1fedd06cadffa826f65f [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 Martin2d2123b2017-10-31 15:51:14 +000032#include <linux/prctl.h>
Dave Martin4328825d2017-08-03 17:23:22 +010033#include <linux/preempt.h>
Dave Martin7582e222017-10-31 15:51:08 +000034#include <linux/prctl.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000035#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010036#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000037#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000038#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000039#include <linux/slab.h>
Dave Martin4ffa09a2017-10-31 15:51:15 +000040#include <linux/sysctl.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000041
Dave Martinaf4a81b2018-03-01 17:44:07 +000042#include <asm/esr.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000043#include <asm/fpsimd.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010044#include <asm/cpufeature.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000045#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010046#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000047#include <asm/sigcontext.h>
48#include <asm/sysreg.h>
49#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000050
51#define FPEXC_IOF (1 << 0)
52#define FPEXC_DZF (1 << 1)
53#define FPEXC_OFF (1 << 2)
54#define FPEXC_UFF (1 << 3)
55#define FPEXC_IXF (1 << 4)
56#define FPEXC_IDF (1 << 7)
57
58/*
Dave Martinbc0ee472017-10-31 15:51:05 +000059 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
60 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020061 * In order to reduce the number of times the FPSIMD state is needlessly saved
62 * and restored, we need to keep track of two things:
63 * (a) for each task, we need to remember which CPU was the last one to have
64 * the task's FPSIMD state loaded into its FPSIMD registers;
65 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
66 * been loaded into its FPSIMD registers most recently, or whether it has
67 * been used to perform kernel mode NEON in the meantime.
68 *
Dave Martin20b85472018-03-28 10:50:48 +010069 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080070 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020071 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
72 * address of the userland FPSIMD state of the task that was loaded onto the CPU
73 * the most recently, or NULL if kernel mode NEON has been performed after that.
74 *
75 * With this in place, we no longer have to restore the next FPSIMD state right
76 * when switching between tasks. Instead, we can defer this check to userland
77 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010078 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020079 * can omit the FPSIMD restore.
80 *
81 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
82 * indicate whether or not the userland FPSIMD state of the current task is
83 * present in the registers. The flag is set unless the FPSIMD registers of this
84 * CPU currently contain the most recent userland FPSIMD state of the current
85 * task.
86 *
Dave Martincb84d112017-08-03 17:23:23 +010087 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
88 * save the task's FPSIMD context back to task_struct from softirq context.
89 * To prevent this from racing with the manipulation of the task's FPSIMD state
90 * from task context and thereby corrupting the state, it is necessary to
91 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
92 * flag with local_bh_disable() unless softirqs are already masked.
93 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020094 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010095 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020096 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
97 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
98 * cleared, otherwise it is set;
99 *
100 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
101 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +0100102 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200103 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
104 * TIF_FOREIGN_FPSTATE flag is cleared;
105 *
106 * - the task executes an ordinary syscall; upon return to userland, the
107 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
108 * restored;
109 *
110 * - the task executes a syscall which executes some NEON instructions; this is
111 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
112 * register contents to memory, clears the fpsimd_last_state per-cpu variable
113 * and sets the TIF_FOREIGN_FPSTATE flag;
114 *
115 * - the task gets preempted after kernel_neon_end() is called; as we have not
116 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
117 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
118 */
Dave Martincb968af2017-12-06 16:45:47 +0000119struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100120 struct user_fpsimd_state *st;
Dave Martincb968af2017-12-06 16:45:47 +0000121 bool sve_in_use;
122};
123
124static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200125
Dave Martin79ab0472017-10-31 15:51:06 +0000126/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000127static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000128
Dave Martin7582e222017-10-31 15:51:08 +0000129#ifdef CONFIG_ARM64_SVE
130
131/* Maximum supported vector length across all CPUs (initially poisoned) */
132int __ro_after_init sve_max_vl = -1;
133/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000134static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000135static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000136
137#else /* ! CONFIG_ARM64_SVE */
138
139/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000140extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000141extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000142
143#endif /* ! CONFIG_ARM64_SVE */
144
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200145/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000146 * Call __sve_free() directly only if you know task can't be scheduled
147 * or preempted.
148 */
149static void __sve_free(struct task_struct *task)
150{
151 kfree(task->thread.sve_state);
152 task->thread.sve_state = NULL;
153}
154
155static void sve_free(struct task_struct *task)
156{
157 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
158
159 __sve_free(task);
160}
161
162
163/* Offset of FFR in the SVE register dump */
164static size_t sve_ffr_offset(int vl)
165{
166 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
167}
168
169static void *sve_pffr(struct task_struct *task)
170{
171 return (char *)task->thread.sve_state +
172 sve_ffr_offset(task->thread.sve_vl);
173}
174
175static void change_cpacr(u64 val, u64 mask)
176{
177 u64 cpacr = read_sysreg(CPACR_EL1);
178 u64 new = (cpacr & ~mask) | val;
179
180 if (new != cpacr)
181 write_sysreg(new, CPACR_EL1);
182}
183
184static void sve_user_disable(void)
185{
186 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
187}
188
189static void sve_user_enable(void)
190{
191 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
192}
193
194/*
195 * TIF_SVE controls whether a task can use SVE without trapping while
196 * in userspace, and also the way a task's FPSIMD/SVE state is stored
197 * in thread_struct.
198 *
199 * The kernel uses this flag to track whether a user task is actively
200 * using SVE, and therefore whether full SVE register state needs to
201 * be tracked. If not, the cheaper FPSIMD context handling code can
202 * be used instead of the more costly SVE equivalents.
203 *
204 * * TIF_SVE set:
205 *
206 * The task can execute SVE instructions while in userspace without
207 * trapping to the kernel.
208 *
209 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
210 * corresponding Zn), P0-P15 and FFR are encoded in in
211 * task->thread.sve_state, formatted appropriately for vector
212 * length task->thread.sve_vl.
213 *
214 * task->thread.sve_state must point to a valid buffer at least
215 * sve_state_size(task) bytes in size.
216 *
217 * During any syscall, the kernel may optionally clear TIF_SVE and
218 * discard the vector state except for the FPSIMD subset.
219 *
220 * * TIF_SVE clear:
221 *
222 * An attempt by the user task to execute an SVE instruction causes
223 * do_sve_acc() to be called, which does some preparation and then
224 * sets TIF_SVE.
225 *
226 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100227 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000228 * logically zero but not stored anywhere; P0-P15 and FFR are not
229 * stored and have unspecified values from userspace's point of
230 * view. For hygiene purposes, the kernel zeroes them on next use,
231 * but userspace is discouraged from relying on this.
232 *
233 * task->thread.sve_state does not need to be non-NULL, valid or any
234 * particular size: it must not be dereferenced.
235 *
Dave Martin65896542018-03-28 10:50:49 +0100236 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
237 * irrespective of whether TIF_SVE is clear or set, since these are
238 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000239 */
240
241/*
242 * Update current's FPSIMD/SVE registers from thread_struct.
243 *
244 * This function should be called only when the FPSIMD/SVE state in
245 * thread_struct is known to be up to date, when preparing to enter
246 * userspace.
247 *
248 * Softirqs (and preemption) must be disabled.
249 */
250static void task_fpsimd_load(void)
251{
252 WARN_ON(!in_softirq() && !irqs_disabled());
253
254 if (system_supports_sve() && test_thread_flag(TIF_SVE))
255 sve_load_state(sve_pffr(current),
Dave Martin65896542018-03-28 10:50:49 +0100256 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000257 sve_vq_from_vl(current->thread.sve_vl) - 1);
258 else
Dave Martin65896542018-03-28 10:50:49 +0100259 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000260}
261
262/*
Dave Martind1797612018-04-06 14:55:59 +0100263 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
264 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000265 *
266 * Softirqs (and preemption) must be disabled.
267 */
Dave Martine6b673b2018-04-06 14:55:59 +0100268void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000269{
Dave Martind1797612018-04-06 14:55:59 +0100270 struct user_fpsimd_state *st = __this_cpu_read(fpsimd_last_state.st);
Dave Martine6b673b2018-04-06 14:55:59 +0100271 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100272
Dave Martinbc0ee472017-10-31 15:51:05 +0000273 WARN_ON(!in_softirq() && !irqs_disabled());
274
275 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
276 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
277 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
278 /*
279 * Can't save the user regs, so current would
280 * re-enter user with corrupt state.
281 * There's no way to recover, so kill it:
282 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000283 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000284 return;
285 }
286
Dave Martind1797612018-04-06 14:55:59 +0100287 sve_save_state(sve_pffr(current), &st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000288 } else
Dave Martind1797612018-04-06 14:55:59 +0100289 fpsimd_save_state(st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000290 }
291}
292
Dave Martin7582e222017-10-31 15:51:08 +0000293/*
294 * Helpers to translate bit indices in sve_vq_map to VQ values (and
295 * vice versa). This allows find_next_bit() to be used to find the
296 * _maximum_ VQ not exceeding a certain value.
297 */
298
299static unsigned int vq_to_bit(unsigned int vq)
300{
301 return SVE_VQ_MAX - vq;
302}
303
304static unsigned int bit_to_vq(unsigned int bit)
305{
306 if (WARN_ON(bit >= SVE_VQ_MAX))
307 bit = SVE_VQ_MAX - 1;
308
309 return SVE_VQ_MAX - bit;
310}
311
312/*
313 * All vector length selection from userspace comes through here.
314 * We're on a slow path, so some sanity-checks are included.
315 * If things go wrong there's a bug somewhere, but try to fall back to a
316 * safe choice.
317 */
318static unsigned int find_supported_vector_length(unsigned int vl)
319{
320 int bit;
321 int max_vl = sve_max_vl;
322
323 if (WARN_ON(!sve_vl_valid(vl)))
324 vl = SVE_VL_MIN;
325
326 if (WARN_ON(!sve_vl_valid(max_vl)))
327 max_vl = SVE_VL_MIN;
328
329 if (vl > max_vl)
330 vl = max_vl;
331
332 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
333 vq_to_bit(sve_vq_from_vl(vl)));
334 return sve_vl_from_vq(bit_to_vq(bit));
335}
336
Dave Martin4ffa09a2017-10-31 15:51:15 +0000337#ifdef CONFIG_SYSCTL
338
339static int sve_proc_do_default_vl(struct ctl_table *table, int write,
340 void __user *buffer, size_t *lenp,
341 loff_t *ppos)
342{
343 int ret;
344 int vl = sve_default_vl;
345 struct ctl_table tmp_table = {
346 .data = &vl,
347 .maxlen = sizeof(vl),
348 };
349
350 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
351 if (ret || !write)
352 return ret;
353
354 /* Writing -1 has the special meaning "set to max": */
355 if (vl == -1) {
356 /* Fail safe if sve_max_vl wasn't initialised */
357 if (WARN_ON(!sve_vl_valid(sve_max_vl)))
358 vl = SVE_VL_MIN;
359 else
360 vl = sve_max_vl;
361
362 goto chosen;
363 }
364
365 if (!sve_vl_valid(vl))
366 return -EINVAL;
367
368 vl = find_supported_vector_length(vl);
369chosen:
370 sve_default_vl = vl;
371 return 0;
372}
373
374static struct ctl_table sve_default_vl_table[] = {
375 {
376 .procname = "sve_default_vector_length",
377 .mode = 0644,
378 .proc_handler = sve_proc_do_default_vl,
379 },
380 { }
381};
382
383static int __init sve_sysctl_init(void)
384{
385 if (system_supports_sve())
386 if (!register_sysctl("abi", sve_default_vl_table))
387 return -EINVAL;
388
389 return 0;
390}
391
392#else /* ! CONFIG_SYSCTL */
393static int __init sve_sysctl_init(void) { return 0; }
394#endif /* ! CONFIG_SYSCTL */
395
Dave Martinbc0ee472017-10-31 15:51:05 +0000396#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
397 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
398
399/*
Dave Martin65896542018-03-28 10:50:49 +0100400 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000401 * task->thread.sve_state.
402 *
403 * Task can be a non-runnable task, or current. In the latter case,
404 * softirqs (and preemption) must be disabled.
405 * task->thread.sve_state must point to at least sve_state_size(task)
406 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100407 * task->thread.uw.fpsimd_state must be up to date before calling this
408 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000409 */
410static void fpsimd_to_sve(struct task_struct *task)
411{
412 unsigned int vq;
413 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100414 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000415 unsigned int i;
416
417 if (!system_supports_sve())
418 return;
419
420 vq = sve_vq_from_vl(task->thread.sve_vl);
421 for (i = 0; i < 32; ++i)
422 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
423 sizeof(fst->vregs[i]));
424}
425
Dave Martin8cd969d2017-10-31 15:51:07 +0000426/*
427 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100428 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000429 *
430 * Task can be a non-runnable task, or current. In the latter case,
431 * softirqs (and preemption) must be disabled.
432 * task->thread.sve_state must point to at least sve_state_size(task)
433 * bytes of allocated kernel memory.
434 * task->thread.sve_state must be up to date before calling this function.
435 */
436static void sve_to_fpsimd(struct task_struct *task)
437{
438 unsigned int vq;
439 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100440 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000441 unsigned int i;
442
443 if (!system_supports_sve())
444 return;
445
446 vq = sve_vq_from_vl(task->thread.sve_vl);
447 for (i = 0; i < 32; ++i)
448 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
449 sizeof(fst->vregs[i]));
450}
451
Dave Martinbc0ee472017-10-31 15:51:05 +0000452#ifdef CONFIG_ARM64_SVE
453
454/*
455 * Return how many bytes of memory are required to store the full SVE
456 * state for task, given task's currently configured vector length.
457 */
458size_t sve_state_size(struct task_struct const *task)
459{
460 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
461}
462
463/*
464 * Ensure that task->thread.sve_state is allocated and sufficiently large.
465 *
466 * This function should be used only in preparation for replacing
467 * task->thread.sve_state with new data. The memory is always zeroed
468 * here to prevent stale data from showing through: this is done in
469 * the interest of testability and predictability: except in the
470 * do_sve_acc() case, there is no ABI requirement to hide stale data
471 * written previously be task.
472 */
473void sve_alloc(struct task_struct *task)
474{
475 if (task->thread.sve_state) {
476 memset(task->thread.sve_state, 0, sve_state_size(current));
477 return;
478 }
479
480 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
481 task->thread.sve_state =
482 kzalloc(sve_state_size(task), GFP_KERNEL);
483
484 /*
485 * If future SVE revisions can have larger vectors though,
486 * this may cease to be true:
487 */
488 BUG_ON(!task->thread.sve_state);
489}
490
Dave Martin43d4da2c42017-10-31 15:51:13 +0000491
492/*
493 * Ensure that task->thread.sve_state is up to date with respect to
494 * the user task, irrespective of when SVE is in use or not.
495 *
496 * This should only be called by ptrace. task must be non-runnable.
497 * task->thread.sve_state must point to at least sve_state_size(task)
498 * bytes of allocated kernel memory.
499 */
500void fpsimd_sync_to_sve(struct task_struct *task)
501{
502 if (!test_tsk_thread_flag(task, TIF_SVE))
503 fpsimd_to_sve(task);
504}
505
506/*
Dave Martin65896542018-03-28 10:50:49 +0100507 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000508 * the user task, irrespective of whether SVE is in use or not.
509 *
510 * This should only be called by ptrace. task must be non-runnable.
511 * task->thread.sve_state must point to at least sve_state_size(task)
512 * bytes of allocated kernel memory.
513 */
514void sve_sync_to_fpsimd(struct task_struct *task)
515{
516 if (test_tsk_thread_flag(task, TIF_SVE))
517 sve_to_fpsimd(task);
518}
519
520/*
521 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100522 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000523 *
524 * This should only be called by ptrace to merge new FPSIMD register
525 * values into a task for which SVE is currently active.
526 * task must be non-runnable.
527 * task->thread.sve_state must point to at least sve_state_size(task)
528 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100529 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000530 * the new FPSIMD register values to be merged in.
531 */
532void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
533{
534 unsigned int vq;
535 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100536 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000537 unsigned int i;
538
539 if (!test_tsk_thread_flag(task, TIF_SVE))
540 return;
541
542 vq = sve_vq_from_vl(task->thread.sve_vl);
543
544 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
545
546 for (i = 0; i < 32; ++i)
547 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
548 sizeof(fst->vregs[i]));
549}
550
Dave Martin7582e222017-10-31 15:51:08 +0000551int sve_set_vector_length(struct task_struct *task,
552 unsigned long vl, unsigned long flags)
553{
554 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
555 PR_SVE_SET_VL_ONEXEC))
556 return -EINVAL;
557
558 if (!sve_vl_valid(vl))
559 return -EINVAL;
560
561 /*
562 * Clamp to the maximum vector length that VL-agnostic SVE code can
563 * work with. A flag may be assigned in the future to allow setting
564 * of larger vector lengths without confusing older software.
565 */
566 if (vl > SVE_VL_ARCH_MAX)
567 vl = SVE_VL_ARCH_MAX;
568
569 vl = find_supported_vector_length(vl);
570
571 if (flags & (PR_SVE_VL_INHERIT |
572 PR_SVE_SET_VL_ONEXEC))
573 task->thread.sve_vl_onexec = vl;
574 else
575 /* Reset VL to system default on next exec: */
576 task->thread.sve_vl_onexec = 0;
577
578 /* Only actually set the VL if not deferred: */
579 if (flags & PR_SVE_SET_VL_ONEXEC)
580 goto out;
581
582 if (vl == task->thread.sve_vl)
583 goto out;
584
585 /*
586 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
587 * write any live register state back to task_struct, and convert to a
588 * non-SVE thread.
589 */
590 if (task == current) {
591 local_bh_disable();
592
Dave Martind1797612018-04-06 14:55:59 +0100593 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000594 set_thread_flag(TIF_FOREIGN_FPSTATE);
595 }
596
597 fpsimd_flush_task_state(task);
598 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
599 sve_to_fpsimd(task);
600
601 if (task == current)
602 local_bh_enable();
603
604 /*
605 * Force reallocation of task SVE state to the correct size
606 * on next use:
607 */
608 sve_free(task);
609
610 task->thread.sve_vl = vl;
611
612out:
Dave Martin09d12232018-04-11 17:59:06 +0100613 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
614 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000615
616 return 0;
617}
618
Dave Martinbc0ee472017-10-31 15:51:05 +0000619/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000620 * Encode the current vector length and flags for return.
621 * This is only required for prctl(): ptrace has separate fields
622 *
623 * flags are as for sve_set_vector_length().
624 */
625static int sve_prctl_status(unsigned long flags)
626{
627 int ret;
628
629 if (flags & PR_SVE_SET_VL_ONEXEC)
630 ret = current->thread.sve_vl_onexec;
631 else
632 ret = current->thread.sve_vl;
633
634 if (test_thread_flag(TIF_SVE_VL_INHERIT))
635 ret |= PR_SVE_VL_INHERIT;
636
637 return ret;
638}
639
640/* PR_SVE_SET_VL */
641int sve_set_current_vl(unsigned long arg)
642{
643 unsigned long vl, flags;
644 int ret;
645
646 vl = arg & PR_SVE_VL_LEN_MASK;
647 flags = arg & ~vl;
648
649 if (!system_supports_sve())
650 return -EINVAL;
651
652 ret = sve_set_vector_length(current, vl, flags);
653 if (ret)
654 return ret;
655
656 return sve_prctl_status(flags);
657}
658
659/* PR_SVE_GET_VL */
660int sve_get_current_vl(void)
661{
662 if (!system_supports_sve())
663 return -EINVAL;
664
665 return sve_prctl_status(0);
666}
667
668/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000669 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
670 * during secondary boot.
671 */
672static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
673
674static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
675{
676 unsigned int vq, vl;
677 unsigned long zcr;
678
679 bitmap_zero(map, SVE_VQ_MAX);
680
681 zcr = ZCR_ELx_LEN_MASK;
682 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
683
684 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
685 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
686 vl = sve_get_vl();
687 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
688 set_bit(vq_to_bit(vq), map);
689 }
690}
691
692void __init sve_init_vq_map(void)
693{
694 sve_probe_vqs(sve_vq_map);
695}
696
697/*
698 * If we haven't committed to the set of supported VQs yet, filter out
699 * those not supported by the current CPU.
700 */
701void sve_update_vq_map(void)
702{
703 sve_probe_vqs(sve_secondary_vq_map);
704 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
705}
706
707/* Check whether the current CPU supports all VQs in the committed set */
708int sve_verify_vq_map(void)
709{
710 int ret = 0;
711
712 sve_probe_vqs(sve_secondary_vq_map);
713 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
714 SVE_VQ_MAX);
715 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
716 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
717 smp_processor_id());
718 ret = -EINVAL;
719 }
720
721 return ret;
722}
723
Dave Martinfdfa9762017-10-31 15:51:12 +0000724static void __init sve_efi_setup(void)
725{
726 if (!IS_ENABLED(CONFIG_EFI))
727 return;
728
729 /*
730 * alloc_percpu() warns and prints a backtrace if this goes wrong.
731 * This is evidence of a crippled system and we are returning void,
732 * so no attempt is made to handle this situation here.
733 */
734 if (!sve_vl_valid(sve_max_vl))
735 goto fail;
736
737 efi_sve_state = __alloc_percpu(
738 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
739 if (!efi_sve_state)
740 goto fail;
741
742 return;
743
744fail:
745 panic("Cannot allocate percpu memory for EFI SVE save/restore");
746}
747
Dave Martin2e0f2472017-10-31 15:51:10 +0000748/*
749 * Enable SVE for EL1.
750 * Intended for use by the cpufeatures code during CPU boot.
751 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100752void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000753{
754 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
755 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000756}
757
758void __init sve_setup(void)
759{
760 u64 zcr;
761
762 if (!system_supports_sve())
763 return;
764
765 /*
766 * The SVE architecture mandates support for 128-bit vectors,
767 * so sve_vq_map must have at least SVE_VQ_MIN set.
768 * If something went wrong, at least try to patch it up:
769 */
770 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
771 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
772
773 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
774 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
775
776 /*
777 * Sanity-check that the max VL we determined through CPU features
778 * corresponds properly to sve_vq_map. If not, do our best:
779 */
780 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
781 sve_max_vl = find_supported_vector_length(sve_max_vl);
782
783 /*
784 * For the default VL, pick the maximum supported value <= 64.
785 * VL == 64 is guaranteed not to grow the signal frame.
786 */
787 sve_default_vl = find_supported_vector_length(64);
788
789 pr_info("SVE: maximum available vector length %u bytes per vector\n",
790 sve_max_vl);
791 pr_info("SVE: default vector length %u bytes per vector\n",
792 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000793
794 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000795}
796
797/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000798 * Called from the put_task_struct() path, which cannot get here
799 * unless dead_task is really dead and not schedulable.
800 */
801void fpsimd_release_task(struct task_struct *dead_task)
802{
803 __sve_free(dead_task);
804}
805
806#endif /* CONFIG_ARM64_SVE */
807
808/*
809 * Trapped SVE access
810 *
811 * Storage is allocated for the full SVE state, the current FPSIMD
812 * register contents are migrated across, and TIF_SVE is set so that
813 * the SVE access trap will be disabled the next time this task
814 * reaches ret_to_user.
815 *
816 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
817 * would have disabled the SVE access trap for userspace during
818 * ret_to_user, making an SVE access trap impossible in that case.
819 */
820asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
821{
822 /* Even if we chose not to use SVE, the hardware could still trap: */
823 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000824 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000825 return;
826 }
827
828 sve_alloc(current);
829
830 local_bh_disable();
831
Dave Martind1797612018-04-06 14:55:59 +0100832 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000833 fpsimd_to_sve(current);
834
835 /* Force ret_to_user to reload the registers: */
836 fpsimd_flush_task_state(current);
837 set_thread_flag(TIF_FOREIGN_FPSTATE);
838
839 if (test_and_set_thread_flag(TIF_SVE))
840 WARN_ON(1); /* SVE access shouldn't have trapped */
841
842 local_bh_enable();
843}
844
Catalin Marinas53631b52012-03-05 11:49:32 +0000845/*
846 * Trapped FP/ASIMD access.
847 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000848asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000849{
850 /* TODO: implement lazy context saving/restoring */
851 WARN_ON(1);
852}
853
854/*
855 * Raise a SIGFPE for the current process.
856 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000857asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000858{
859 siginfo_t info;
Dave Martinaf4a81b2018-03-01 17:44:07 +0000860 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000861
Dave Martinaf4a81b2018-03-01 17:44:07 +0000862 if (esr & ESR_ELx_FP_EXC_TFV) {
863 if (esr & FPEXC_IOF)
864 si_code = FPE_FLTINV;
865 else if (esr & FPEXC_DZF)
866 si_code = FPE_FLTDIV;
867 else if (esr & FPEXC_OFF)
868 si_code = FPE_FLTOVF;
869 else if (esr & FPEXC_UFF)
870 si_code = FPE_FLTUND;
871 else if (esr & FPEXC_IXF)
872 si_code = FPE_FLTRES;
873 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000874
875 memset(&info, 0, sizeof(info));
876 info.si_signo = SIGFPE;
877 info.si_code = si_code;
878 info.si_addr = (void __user *)instruction_pointer(regs);
879
880 send_sig_info(SIGFPE, &info, current);
881}
882
883void fpsimd_thread_switch(struct task_struct *next)
884{
Dave Martindf3fb962018-05-21 19:08:15 +0100885 bool wrong_task, wrong_cpu;
886
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000887 if (!system_supports_fpsimd())
888 return;
Dave Martindf3fb962018-05-21 19:08:15 +0100889
890 /* Save unsaved fpsimd state, if any: */
891 fpsimd_save();
892
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200893 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100894 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
895 * state. For kernel threads, FPSIMD registers are never loaded
896 * and wrong_task and wrong_cpu will always be true.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200897 */
Dave Martindf3fb962018-05-21 19:08:15 +0100898 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100899 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100900 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100901
Dave Martindf3fb962018-05-21 19:08:15 +0100902 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
903 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000904}
905
906void fpsimd_flush_thread(void)
907{
Dave Martin7582e222017-10-31 15:51:08 +0000908 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000909
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000910 if (!system_supports_fpsimd())
911 return;
Dave Martincb84d112017-08-03 17:23:23 +0100912
913 local_bh_disable();
914
Dave Martin65896542018-03-28 10:50:49 +0100915 memset(&current->thread.uw.fpsimd_state, 0,
916 sizeof(current->thread.uw.fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100917 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000918
919 if (system_supports_sve()) {
920 clear_thread_flag(TIF_SVE);
921 sve_free(current);
922
923 /*
924 * Reset the task vector length as required.
925 * This is where we ensure that all user tasks have a valid
926 * vector length configured: no kernel task can become a user
927 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000928 * By the time the first call to this function is made, all
929 * early hardware probing is complete, so sve_default_vl
930 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000931 * If a bug causes this to go wrong, we make some noise and
932 * try to fudge thread.sve_vl to a safe value here.
933 */
Dave Martin79ab0472017-10-31 15:51:06 +0000934 vl = current->thread.sve_vl_onexec ?
935 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000936
937 if (WARN_ON(!sve_vl_valid(vl)))
938 vl = SVE_VL_MIN;
939
Dave Martin7582e222017-10-31 15:51:08 +0000940 supported_vl = find_supported_vector_length(vl);
941 if (WARN_ON(supported_vl != vl))
942 vl = supported_vl;
943
Dave Martinbc0ee472017-10-31 15:51:05 +0000944 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000945
946 /*
947 * If the task is not set to inherit, ensure that the vector
948 * length will be reset by a subsequent exec:
949 */
950 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
951 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000952 }
953
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200954 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100955
956 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000957}
958
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100959/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200960 * Save the userland FPSIMD state of 'current' to memory, but only if the state
961 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100962 */
963void fpsimd_preserve_current_state(void)
964{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000965 if (!system_supports_fpsimd())
966 return;
Dave Martincb84d112017-08-03 17:23:23 +0100967
968 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +0100969 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100970 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100971}
972
973/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000974 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +0100975 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +0000976 * the signal frame.
977 */
978void fpsimd_signal_preserve_current_state(void)
979{
980 fpsimd_preserve_current_state();
981 if (system_supports_sve() && test_thread_flag(TIF_SVE))
982 sve_to_fpsimd(current);
983}
984
985/*
Dave Martin8884b7b2017-12-06 16:45:46 +0000986 * Associate current's FPSIMD context with this cpu
987 * Preemption must be disabled when calling this function.
988 */
Dave Martine6b673b2018-04-06 14:55:59 +0100989void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +0000990{
Dave Martincb968af2017-12-06 16:45:47 +0000991 struct fpsimd_last_state_struct *last =
992 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +0000993
Dave Martin65896542018-03-28 10:50:49 +0100994 last->st = &current->thread.uw.fpsimd_state;
Dave Martincb968af2017-12-06 16:45:47 +0000995 last->sve_in_use = test_thread_flag(TIF_SVE);
Dave Martin20b85472018-03-28 10:50:48 +0100996 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +0100997
998 if (system_supports_sve()) {
999 /* Toggle SVE trapping for userspace if needed */
1000 if (test_thread_flag(TIF_SVE))
1001 sve_user_enable();
1002 else
1003 sve_user_disable();
1004
1005 /* Serialised by exception return to user */
1006 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001007}
1008
Dave Martine6b673b2018-04-06 14:55:59 +01001009void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st)
1010{
1011 struct fpsimd_last_state_struct *last =
1012 this_cpu_ptr(&fpsimd_last_state);
1013
1014 WARN_ON(!in_softirq() && !irqs_disabled());
1015
1016 last->st = st;
1017 last->sve_in_use = false;
1018}
1019
Dave Martin8884b7b2017-12-06 16:45:46 +00001020/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001021 * Load the userland FPSIMD state of 'current' from memory, but only if the
1022 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1023 * state of 'current'
1024 */
1025void fpsimd_restore_current_state(void)
1026{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001027 if (!system_supports_fpsimd())
1028 return;
Dave Martincb84d112017-08-03 17:23:23 +01001029
1030 local_bh_disable();
1031
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001032 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001033 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001034 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001035 }
Dave Martincb84d112017-08-03 17:23:23 +01001036
1037 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001038}
1039
1040/*
1041 * Load an updated userland FPSIMD state for 'current' from memory and set the
1042 * flag that indicates that the FPSIMD register contents are the most recent
1043 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001044 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001045void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001046{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001047 if (!system_supports_fpsimd())
1048 return;
Dave Martincb84d112017-08-03 17:23:23 +01001049
1050 local_bh_disable();
1051
Dave Martin65896542018-03-28 10:50:49 +01001052 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001053 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001054 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001055
Dave Martin8cd969d2017-10-31 15:51:07 +00001056 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001057 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001058
Dave Martin0cff8e72018-05-09 14:27:41 +01001059 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001060
1061 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001062}
1063
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001064/*
1065 * Invalidate live CPU copies of task t's FPSIMD state
1066 */
1067void fpsimd_flush_task_state(struct task_struct *t)
1068{
Dave Martin20b85472018-03-28 10:50:48 +01001069 t->thread.fpsimd_cpu = NR_CPUS;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001070}
1071
Dave Martine6b673b2018-04-06 14:55:59 +01001072void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001073{
Dave Martincb968af2017-12-06 16:45:47 +00001074 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001075 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001076}
1077
1078/*
1079 * Invalidate any task SVE state currently held in this CPU's regs.
1080 *
1081 * This is used to prevent the kernel from trying to reuse SVE register data
1082 * that is detroyed by KVM guest enter/exit. This function should go away when
1083 * KVM SVE support is implemented. Don't use it for anything else.
1084 */
1085#ifdef CONFIG_ARM64_SVE
1086void sve_flush_cpu_state(void)
1087{
Dave Martincb968af2017-12-06 16:45:47 +00001088 struct fpsimd_last_state_struct const *last =
1089 this_cpu_ptr(&fpsimd_last_state);
Dave Martin17eed272017-10-31 15:51:16 +00001090
Dave Martincb968af2017-12-06 16:45:47 +00001091 if (last->st && last->sve_in_use)
Dave Martin17eed272017-10-31 15:51:16 +00001092 fpsimd_flush_cpu_state();
1093}
1094#endif /* CONFIG_ARM64_SVE */
1095
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001096#ifdef CONFIG_KERNEL_MODE_NEON
1097
Dave Martincb84d112017-08-03 17:23:23 +01001098DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001099EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001100
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001101/*
1102 * Kernel-side NEON support functions
1103 */
Dave Martincb84d112017-08-03 17:23:23 +01001104
1105/*
1106 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1107 * context
1108 *
1109 * Must not be called unless may_use_simd() returns true.
1110 * Task context in the FPSIMD registers is saved back to memory as necessary.
1111 *
1112 * A matching call to kernel_neon_end() must be made before returning from the
1113 * calling context.
1114 *
1115 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1116 * called.
1117 */
1118void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001119{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001120 if (WARN_ON(!system_supports_fpsimd()))
1121 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001122
Dave Martincb84d112017-08-03 17:23:23 +01001123 BUG_ON(!may_use_simd());
1124
1125 local_bh_disable();
1126
1127 __this_cpu_write(kernel_neon_busy, true);
1128
Dave Martindf3fb962018-05-21 19:08:15 +01001129 /* Save unsaved fpsimd state, if any: */
1130 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001131
1132 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001133 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001134
1135 preempt_disable();
1136
1137 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001138}
Dave Martincb84d112017-08-03 17:23:23 +01001139EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001140
Dave Martincb84d112017-08-03 17:23:23 +01001141/*
1142 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1143 *
1144 * Must be called from a context in which kernel_neon_begin() was previously
1145 * called, with no call to kernel_neon_end() in the meantime.
1146 *
1147 * The caller must not use the FPSIMD registers after this function is called,
1148 * unless kernel_neon_begin() is called again in the meantime.
1149 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001150void kernel_neon_end(void)
1151{
Dave Martincb84d112017-08-03 17:23:23 +01001152 bool busy;
1153
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001154 if (!system_supports_fpsimd())
1155 return;
Dave Martincb84d112017-08-03 17:23:23 +01001156
1157 busy = __this_cpu_xchg(kernel_neon_busy, false);
1158 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1159
1160 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001161}
1162EXPORT_SYMBOL(kernel_neon_end);
1163
Dave Martine580b8b2017-09-18 09:40:12 +01001164#ifdef CONFIG_EFI
1165
Dave Martin20b85472018-03-28 10:50:48 +01001166static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001167static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001168static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001169
1170/*
1171 * EFI runtime services support functions
1172 *
1173 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1174 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1175 * is always used rather than being an optional accelerator.
1176 *
1177 * These functions provide the necessary support for ensuring FPSIMD
1178 * save/restore in the contexts from which EFI is used.
1179 *
1180 * Do not use them for any other purpose -- if tempted to do so, you are
1181 * either doing something wrong or you need to propose some refactoring.
1182 */
1183
1184/*
1185 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1186 */
1187void __efi_fpsimd_begin(void)
1188{
1189 if (!system_supports_fpsimd())
1190 return;
1191
1192 WARN_ON(preemptible());
1193
Dave Martinfdfa9762017-10-31 15:51:12 +00001194 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001195 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001196 } else {
1197 /*
1198 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1199 * preserving:
1200 */
1201 if (system_supports_sve() && likely(efi_sve_state)) {
1202 char *sve_state = this_cpu_ptr(efi_sve_state);
1203
1204 __this_cpu_write(efi_sve_state_used, true);
1205
1206 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1207 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1208 } else {
1209 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1210 }
1211
Dave Martin4328825d2017-08-03 17:23:22 +01001212 __this_cpu_write(efi_fpsimd_state_used, true);
1213 }
1214}
1215
1216/*
1217 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1218 */
1219void __efi_fpsimd_end(void)
1220{
1221 if (!system_supports_fpsimd())
1222 return;
1223
Dave Martinfdfa9762017-10-31 15:51:12 +00001224 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001225 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001226 } else {
1227 if (system_supports_sve() &&
1228 likely(__this_cpu_read(efi_sve_state_used))) {
1229 char const *sve_state = this_cpu_ptr(efi_sve_state);
1230
1231 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1232 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1233 sve_vq_from_vl(sve_get_vl()) - 1);
1234
1235 __this_cpu_write(efi_sve_state_used, false);
1236 } else {
1237 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1238 }
1239 }
Dave Martin4328825d2017-08-03 17:23:22 +01001240}
1241
Dave Martine580b8b2017-09-18 09:40:12 +01001242#endif /* CONFIG_EFI */
1243
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001244#endif /* CONFIG_KERNEL_MODE_NEON */
1245
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001246#ifdef CONFIG_CPU_PM
1247static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1248 unsigned long cmd, void *v)
1249{
1250 switch (cmd) {
1251 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001252 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001253 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001254 break;
1255 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001256 break;
1257 case CPU_PM_ENTER_FAILED:
1258 default:
1259 return NOTIFY_DONE;
1260 }
1261 return NOTIFY_OK;
1262}
1263
1264static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1265 .notifier_call = fpsimd_cpu_pm_notifier,
1266};
1267
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001268static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001269{
1270 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1271}
1272
1273#else
1274static inline void fpsimd_pm_init(void) { }
1275#endif /* CONFIG_CPU_PM */
1276
Janet Liu32365e62015-06-11 12:02:45 +08001277#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001278static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001279{
Dave Martincb968af2017-12-06 16:45:47 +00001280 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001281 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001282}
1283
Janet Liu32365e62015-06-11 12:02:45 +08001284static inline void fpsimd_hotplug_init(void)
1285{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001286 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1287 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001288}
1289
1290#else
1291static inline void fpsimd_hotplug_init(void) { }
1292#endif
1293
Catalin Marinas53631b52012-03-05 11:49:32 +00001294/*
1295 * FP/SIMD support code initialisation.
1296 */
1297static int __init fpsimd_init(void)
1298{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001299 if (elf_hwcap & HWCAP_FP) {
1300 fpsimd_pm_init();
1301 fpsimd_hotplug_init();
1302 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001303 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001304 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001305
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001306 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001307 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001308
Dave Martin4ffa09a2017-10-31 15:51:15 +00001309 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001310}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001311core_initcall(fpsimd_init);