blob: 9d853732f9f4776077733dc2df73af380694b288 [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 if (system_supports_sve()) {
262 /* Toggle SVE trapping for userspace if needed */
263 if (test_thread_flag(TIF_SVE))
264 sve_user_enable();
265 else
266 sve_user_disable();
267
268 /* Serialised by exception return to user */
269 }
270}
271
272/*
273 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
274 * with respect to the CPU registers.
275 *
276 * Softirqs (and preemption) must be disabled.
277 */
278static void task_fpsimd_save(void)
279{
280 WARN_ON(!in_softirq() && !irqs_disabled());
281
282 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
283 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
284 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
285 /*
286 * Can't save the user regs, so current would
287 * re-enter user with corrupt state.
288 * There's no way to recover, so kill it:
289 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000290 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000291 return;
292 }
293
294 sve_save_state(sve_pffr(current),
Dave Martin65896542018-03-28 10:50:49 +0100295 &current->thread.uw.fpsimd_state.fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000296 } else
Dave Martin65896542018-03-28 10:50:49 +0100297 fpsimd_save_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000298 }
299}
300
Dave Martin7582e222017-10-31 15:51:08 +0000301/*
302 * Helpers to translate bit indices in sve_vq_map to VQ values (and
303 * vice versa). This allows find_next_bit() to be used to find the
304 * _maximum_ VQ not exceeding a certain value.
305 */
306
307static unsigned int vq_to_bit(unsigned int vq)
308{
309 return SVE_VQ_MAX - vq;
310}
311
312static unsigned int bit_to_vq(unsigned int bit)
313{
314 if (WARN_ON(bit >= SVE_VQ_MAX))
315 bit = SVE_VQ_MAX - 1;
316
317 return SVE_VQ_MAX - bit;
318}
319
320/*
321 * All vector length selection from userspace comes through here.
322 * We're on a slow path, so some sanity-checks are included.
323 * If things go wrong there's a bug somewhere, but try to fall back to a
324 * safe choice.
325 */
326static unsigned int find_supported_vector_length(unsigned int vl)
327{
328 int bit;
329 int max_vl = sve_max_vl;
330
331 if (WARN_ON(!sve_vl_valid(vl)))
332 vl = SVE_VL_MIN;
333
334 if (WARN_ON(!sve_vl_valid(max_vl)))
335 max_vl = SVE_VL_MIN;
336
337 if (vl > max_vl)
338 vl = max_vl;
339
340 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
341 vq_to_bit(sve_vq_from_vl(vl)));
342 return sve_vl_from_vq(bit_to_vq(bit));
343}
344
Dave Martin4ffa09a2017-10-31 15:51:15 +0000345#ifdef CONFIG_SYSCTL
346
347static int sve_proc_do_default_vl(struct ctl_table *table, int write,
348 void __user *buffer, size_t *lenp,
349 loff_t *ppos)
350{
351 int ret;
352 int vl = sve_default_vl;
353 struct ctl_table tmp_table = {
354 .data = &vl,
355 .maxlen = sizeof(vl),
356 };
357
358 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
359 if (ret || !write)
360 return ret;
361
362 /* Writing -1 has the special meaning "set to max": */
363 if (vl == -1) {
364 /* Fail safe if sve_max_vl wasn't initialised */
365 if (WARN_ON(!sve_vl_valid(sve_max_vl)))
366 vl = SVE_VL_MIN;
367 else
368 vl = sve_max_vl;
369
370 goto chosen;
371 }
372
373 if (!sve_vl_valid(vl))
374 return -EINVAL;
375
376 vl = find_supported_vector_length(vl);
377chosen:
378 sve_default_vl = vl;
379 return 0;
380}
381
382static struct ctl_table sve_default_vl_table[] = {
383 {
384 .procname = "sve_default_vector_length",
385 .mode = 0644,
386 .proc_handler = sve_proc_do_default_vl,
387 },
388 { }
389};
390
391static int __init sve_sysctl_init(void)
392{
393 if (system_supports_sve())
394 if (!register_sysctl("abi", sve_default_vl_table))
395 return -EINVAL;
396
397 return 0;
398}
399
400#else /* ! CONFIG_SYSCTL */
401static int __init sve_sysctl_init(void) { return 0; }
402#endif /* ! CONFIG_SYSCTL */
403
Dave Martinbc0ee472017-10-31 15:51:05 +0000404#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
405 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
406
407/*
Dave Martin65896542018-03-28 10:50:49 +0100408 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000409 * task->thread.sve_state.
410 *
411 * Task can be a non-runnable task, or current. In the latter case,
412 * softirqs (and preemption) must be disabled.
413 * task->thread.sve_state must point to at least sve_state_size(task)
414 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100415 * task->thread.uw.fpsimd_state must be up to date before calling this
416 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000417 */
418static void fpsimd_to_sve(struct task_struct *task)
419{
420 unsigned int vq;
421 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100422 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000423 unsigned int i;
424
425 if (!system_supports_sve())
426 return;
427
428 vq = sve_vq_from_vl(task->thread.sve_vl);
429 for (i = 0; i < 32; ++i)
430 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
431 sizeof(fst->vregs[i]));
432}
433
Dave Martin8cd969d2017-10-31 15:51:07 +0000434/*
435 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100436 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000437 *
438 * Task can be a non-runnable task, or current. In the latter case,
439 * softirqs (and preemption) must be disabled.
440 * task->thread.sve_state must point to at least sve_state_size(task)
441 * bytes of allocated kernel memory.
442 * task->thread.sve_state must be up to date before calling this function.
443 */
444static void sve_to_fpsimd(struct task_struct *task)
445{
446 unsigned int vq;
447 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100448 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000449 unsigned int i;
450
451 if (!system_supports_sve())
452 return;
453
454 vq = sve_vq_from_vl(task->thread.sve_vl);
455 for (i = 0; i < 32; ++i)
456 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
457 sizeof(fst->vregs[i]));
458}
459
Dave Martinbc0ee472017-10-31 15:51:05 +0000460#ifdef CONFIG_ARM64_SVE
461
462/*
463 * Return how many bytes of memory are required to store the full SVE
464 * state for task, given task's currently configured vector length.
465 */
466size_t sve_state_size(struct task_struct const *task)
467{
468 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
469}
470
471/*
472 * Ensure that task->thread.sve_state is allocated and sufficiently large.
473 *
474 * This function should be used only in preparation for replacing
475 * task->thread.sve_state with new data. The memory is always zeroed
476 * here to prevent stale data from showing through: this is done in
477 * the interest of testability and predictability: except in the
478 * do_sve_acc() case, there is no ABI requirement to hide stale data
479 * written previously be task.
480 */
481void sve_alloc(struct task_struct *task)
482{
483 if (task->thread.sve_state) {
484 memset(task->thread.sve_state, 0, sve_state_size(current));
485 return;
486 }
487
488 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
489 task->thread.sve_state =
490 kzalloc(sve_state_size(task), GFP_KERNEL);
491
492 /*
493 * If future SVE revisions can have larger vectors though,
494 * this may cease to be true:
495 */
496 BUG_ON(!task->thread.sve_state);
497}
498
Dave Martin43d4da2c42017-10-31 15:51:13 +0000499
500/*
501 * Ensure that task->thread.sve_state is up to date with respect to
502 * the user task, irrespective of when SVE is in use or not.
503 *
504 * This should only be called by ptrace. task must be non-runnable.
505 * task->thread.sve_state must point to at least sve_state_size(task)
506 * bytes of allocated kernel memory.
507 */
508void fpsimd_sync_to_sve(struct task_struct *task)
509{
510 if (!test_tsk_thread_flag(task, TIF_SVE))
511 fpsimd_to_sve(task);
512}
513
514/*
Dave Martin65896542018-03-28 10:50:49 +0100515 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000516 * the user task, irrespective of whether SVE is in use or not.
517 *
518 * This should only be called by ptrace. task must be non-runnable.
519 * task->thread.sve_state must point to at least sve_state_size(task)
520 * bytes of allocated kernel memory.
521 */
522void sve_sync_to_fpsimd(struct task_struct *task)
523{
524 if (test_tsk_thread_flag(task, TIF_SVE))
525 sve_to_fpsimd(task);
526}
527
528/*
529 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100530 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000531 *
532 * This should only be called by ptrace to merge new FPSIMD register
533 * values into a task for which SVE is currently active.
534 * 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.
Dave Martin65896542018-03-28 10:50:49 +0100537 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000538 * the new FPSIMD register values to be merged in.
539 */
540void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
541{
542 unsigned int vq;
543 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100544 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000545 unsigned int i;
546
547 if (!test_tsk_thread_flag(task, TIF_SVE))
548 return;
549
550 vq = sve_vq_from_vl(task->thread.sve_vl);
551
552 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
553
554 for (i = 0; i < 32; ++i)
555 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
556 sizeof(fst->vregs[i]));
557}
558
Dave Martin7582e222017-10-31 15:51:08 +0000559int sve_set_vector_length(struct task_struct *task,
560 unsigned long vl, unsigned long flags)
561{
562 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
563 PR_SVE_SET_VL_ONEXEC))
564 return -EINVAL;
565
566 if (!sve_vl_valid(vl))
567 return -EINVAL;
568
569 /*
570 * Clamp to the maximum vector length that VL-agnostic SVE code can
571 * work with. A flag may be assigned in the future to allow setting
572 * of larger vector lengths without confusing older software.
573 */
574 if (vl > SVE_VL_ARCH_MAX)
575 vl = SVE_VL_ARCH_MAX;
576
577 vl = find_supported_vector_length(vl);
578
579 if (flags & (PR_SVE_VL_INHERIT |
580 PR_SVE_SET_VL_ONEXEC))
581 task->thread.sve_vl_onexec = vl;
582 else
583 /* Reset VL to system default on next exec: */
584 task->thread.sve_vl_onexec = 0;
585
586 /* Only actually set the VL if not deferred: */
587 if (flags & PR_SVE_SET_VL_ONEXEC)
588 goto out;
589
590 if (vl == task->thread.sve_vl)
591 goto out;
592
593 /*
594 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
595 * write any live register state back to task_struct, and convert to a
596 * non-SVE thread.
597 */
598 if (task == current) {
599 local_bh_disable();
600
601 task_fpsimd_save();
602 set_thread_flag(TIF_FOREIGN_FPSTATE);
603 }
604
605 fpsimd_flush_task_state(task);
606 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
607 sve_to_fpsimd(task);
608
609 if (task == current)
610 local_bh_enable();
611
612 /*
613 * Force reallocation of task SVE state to the correct size
614 * on next use:
615 */
616 sve_free(task);
617
618 task->thread.sve_vl = vl;
619
620out:
Dave Martin09d12232018-04-11 17:59:06 +0100621 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
622 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000623
624 return 0;
625}
626
Dave Martinbc0ee472017-10-31 15:51:05 +0000627/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000628 * Encode the current vector length and flags for return.
629 * This is only required for prctl(): ptrace has separate fields
630 *
631 * flags are as for sve_set_vector_length().
632 */
633static int sve_prctl_status(unsigned long flags)
634{
635 int ret;
636
637 if (flags & PR_SVE_SET_VL_ONEXEC)
638 ret = current->thread.sve_vl_onexec;
639 else
640 ret = current->thread.sve_vl;
641
642 if (test_thread_flag(TIF_SVE_VL_INHERIT))
643 ret |= PR_SVE_VL_INHERIT;
644
645 return ret;
646}
647
648/* PR_SVE_SET_VL */
649int sve_set_current_vl(unsigned long arg)
650{
651 unsigned long vl, flags;
652 int ret;
653
654 vl = arg & PR_SVE_VL_LEN_MASK;
655 flags = arg & ~vl;
656
657 if (!system_supports_sve())
658 return -EINVAL;
659
660 ret = sve_set_vector_length(current, vl, flags);
661 if (ret)
662 return ret;
663
664 return sve_prctl_status(flags);
665}
666
667/* PR_SVE_GET_VL */
668int sve_get_current_vl(void)
669{
670 if (!system_supports_sve())
671 return -EINVAL;
672
673 return sve_prctl_status(0);
674}
675
676/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000677 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
678 * during secondary boot.
679 */
680static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
681
682static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
683{
684 unsigned int vq, vl;
685 unsigned long zcr;
686
687 bitmap_zero(map, SVE_VQ_MAX);
688
689 zcr = ZCR_ELx_LEN_MASK;
690 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
691
692 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
693 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
694 vl = sve_get_vl();
695 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
696 set_bit(vq_to_bit(vq), map);
697 }
698}
699
700void __init sve_init_vq_map(void)
701{
702 sve_probe_vqs(sve_vq_map);
703}
704
705/*
706 * If we haven't committed to the set of supported VQs yet, filter out
707 * those not supported by the current CPU.
708 */
709void sve_update_vq_map(void)
710{
711 sve_probe_vqs(sve_secondary_vq_map);
712 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
713}
714
715/* Check whether the current CPU supports all VQs in the committed set */
716int sve_verify_vq_map(void)
717{
718 int ret = 0;
719
720 sve_probe_vqs(sve_secondary_vq_map);
721 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
722 SVE_VQ_MAX);
723 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
724 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
725 smp_processor_id());
726 ret = -EINVAL;
727 }
728
729 return ret;
730}
731
Dave Martinfdfa9762017-10-31 15:51:12 +0000732static void __init sve_efi_setup(void)
733{
734 if (!IS_ENABLED(CONFIG_EFI))
735 return;
736
737 /*
738 * alloc_percpu() warns and prints a backtrace if this goes wrong.
739 * This is evidence of a crippled system and we are returning void,
740 * so no attempt is made to handle this situation here.
741 */
742 if (!sve_vl_valid(sve_max_vl))
743 goto fail;
744
745 efi_sve_state = __alloc_percpu(
746 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
747 if (!efi_sve_state)
748 goto fail;
749
750 return;
751
752fail:
753 panic("Cannot allocate percpu memory for EFI SVE save/restore");
754}
755
Dave Martin2e0f2472017-10-31 15:51:10 +0000756/*
757 * Enable SVE for EL1.
758 * Intended for use by the cpufeatures code during CPU boot.
759 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100760void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000761{
762 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
763 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000764}
765
766void __init sve_setup(void)
767{
768 u64 zcr;
769
770 if (!system_supports_sve())
771 return;
772
773 /*
774 * The SVE architecture mandates support for 128-bit vectors,
775 * so sve_vq_map must have at least SVE_VQ_MIN set.
776 * If something went wrong, at least try to patch it up:
777 */
778 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
779 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
780
781 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
782 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
783
784 /*
785 * Sanity-check that the max VL we determined through CPU features
786 * corresponds properly to sve_vq_map. If not, do our best:
787 */
788 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
789 sve_max_vl = find_supported_vector_length(sve_max_vl);
790
791 /*
792 * For the default VL, pick the maximum supported value <= 64.
793 * VL == 64 is guaranteed not to grow the signal frame.
794 */
795 sve_default_vl = find_supported_vector_length(64);
796
797 pr_info("SVE: maximum available vector length %u bytes per vector\n",
798 sve_max_vl);
799 pr_info("SVE: default vector length %u bytes per vector\n",
800 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000801
802 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000803}
804
805/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000806 * Called from the put_task_struct() path, which cannot get here
807 * unless dead_task is really dead and not schedulable.
808 */
809void fpsimd_release_task(struct task_struct *dead_task)
810{
811 __sve_free(dead_task);
812}
813
814#endif /* CONFIG_ARM64_SVE */
815
816/*
817 * Trapped SVE access
818 *
819 * Storage is allocated for the full SVE state, the current FPSIMD
820 * register contents are migrated across, and TIF_SVE is set so that
821 * the SVE access trap will be disabled the next time this task
822 * reaches ret_to_user.
823 *
824 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
825 * would have disabled the SVE access trap for userspace during
826 * ret_to_user, making an SVE access trap impossible in that case.
827 */
828asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
829{
830 /* Even if we chose not to use SVE, the hardware could still trap: */
831 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000832 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000833 return;
834 }
835
836 sve_alloc(current);
837
838 local_bh_disable();
839
840 task_fpsimd_save();
841 fpsimd_to_sve(current);
842
843 /* Force ret_to_user to reload the registers: */
844 fpsimd_flush_task_state(current);
845 set_thread_flag(TIF_FOREIGN_FPSTATE);
846
847 if (test_and_set_thread_flag(TIF_SVE))
848 WARN_ON(1); /* SVE access shouldn't have trapped */
849
850 local_bh_enable();
851}
852
Catalin Marinas53631b52012-03-05 11:49:32 +0000853/*
854 * Trapped FP/ASIMD access.
855 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000856asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000857{
858 /* TODO: implement lazy context saving/restoring */
859 WARN_ON(1);
860}
861
862/*
863 * Raise a SIGFPE for the current process.
864 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000865asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000866{
867 siginfo_t info;
Dave Martinaf4a81b2018-03-01 17:44:07 +0000868 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000869
Dave Martinaf4a81b2018-03-01 17:44:07 +0000870 if (esr & ESR_ELx_FP_EXC_TFV) {
871 if (esr & FPEXC_IOF)
872 si_code = FPE_FLTINV;
873 else if (esr & FPEXC_DZF)
874 si_code = FPE_FLTDIV;
875 else if (esr & FPEXC_OFF)
876 si_code = FPE_FLTOVF;
877 else if (esr & FPEXC_UFF)
878 si_code = FPE_FLTUND;
879 else if (esr & FPEXC_IXF)
880 si_code = FPE_FLTRES;
881 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000882
883 memset(&info, 0, sizeof(info));
884 info.si_signo = SIGFPE;
885 info.si_code = si_code;
886 info.si_addr = (void __user *)instruction_pointer(regs);
887
888 send_sig_info(SIGFPE, &info, current);
889}
890
891void fpsimd_thread_switch(struct task_struct *next)
892{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000893 if (!system_supports_fpsimd())
894 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200895 /*
896 * Save the current FPSIMD state to memory, but only if whatever is in
897 * the registers is in fact the most recent userland FPSIMD state of
898 * 'current'.
899 */
Dave Martinbc0ee472017-10-31 15:51:05 +0000900 if (current->mm)
901 task_fpsimd_save();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200902
903 if (next->mm) {
904 /*
905 * If we are switching to a task whose most recent userland
906 * FPSIMD state is already in the registers of *this* cpu,
907 * we can skip loading the state from memory. Otherwise, set
908 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
909 * upon the next return to userland.
910 */
Dave Martin09d12232018-04-11 17:59:06 +0100911 bool wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
912 &next->thread.uw.fpsimd_state;
913 bool wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
914
915 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
916 wrong_task || wrong_cpu);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200917 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000918}
919
920void fpsimd_flush_thread(void)
921{
Dave Martin7582e222017-10-31 15:51:08 +0000922 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000923
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000924 if (!system_supports_fpsimd())
925 return;
Dave Martincb84d112017-08-03 17:23:23 +0100926
927 local_bh_disable();
928
Dave Martin65896542018-03-28 10:50:49 +0100929 memset(&current->thread.uw.fpsimd_state, 0,
930 sizeof(current->thread.uw.fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100931 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000932
933 if (system_supports_sve()) {
934 clear_thread_flag(TIF_SVE);
935 sve_free(current);
936
937 /*
938 * Reset the task vector length as required.
939 * This is where we ensure that all user tasks have a valid
940 * vector length configured: no kernel task can become a user
941 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000942 * By the time the first call to this function is made, all
943 * early hardware probing is complete, so sve_default_vl
944 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000945 * If a bug causes this to go wrong, we make some noise and
946 * try to fudge thread.sve_vl to a safe value here.
947 */
Dave Martin79ab0472017-10-31 15:51:06 +0000948 vl = current->thread.sve_vl_onexec ?
949 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000950
951 if (WARN_ON(!sve_vl_valid(vl)))
952 vl = SVE_VL_MIN;
953
Dave Martin7582e222017-10-31 15:51:08 +0000954 supported_vl = find_supported_vector_length(vl);
955 if (WARN_ON(supported_vl != vl))
956 vl = supported_vl;
957
Dave Martinbc0ee472017-10-31 15:51:05 +0000958 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000959
960 /*
961 * If the task is not set to inherit, ensure that the vector
962 * length will be reset by a subsequent exec:
963 */
964 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
965 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000966 }
967
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200968 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100969
970 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000971}
972
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100973/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200974 * Save the userland FPSIMD state of 'current' to memory, but only if the state
975 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100976 */
977void fpsimd_preserve_current_state(void)
978{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000979 if (!system_supports_fpsimd())
980 return;
Dave Martincb84d112017-08-03 17:23:23 +0100981
982 local_bh_disable();
Dave Martin8cd969d2017-10-31 15:51:07 +0000983 task_fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100984 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100985}
986
987/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000988 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +0100989 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +0000990 * the signal frame.
991 */
992void fpsimd_signal_preserve_current_state(void)
993{
994 fpsimd_preserve_current_state();
995 if (system_supports_sve() && test_thread_flag(TIF_SVE))
996 sve_to_fpsimd(current);
997}
998
999/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001000 * Associate current's FPSIMD context with this cpu
1001 * Preemption must be disabled when calling this function.
1002 */
1003static void fpsimd_bind_to_cpu(void)
1004{
Dave Martincb968af2017-12-06 16:45:47 +00001005 struct fpsimd_last_state_struct *last =
1006 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001007
Dave Martin65896542018-03-28 10:50:49 +01001008 last->st = &current->thread.uw.fpsimd_state;
Dave Martincb968af2017-12-06 16:45:47 +00001009 last->sve_in_use = test_thread_flag(TIF_SVE);
Dave Martin20b85472018-03-28 10:50:48 +01001010 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin8884b7b2017-12-06 16:45:46 +00001011}
1012
1013/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001014 * Load the userland FPSIMD state of 'current' from memory, but only if the
1015 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1016 * state of 'current'
1017 */
1018void fpsimd_restore_current_state(void)
1019{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001020 if (!system_supports_fpsimd())
1021 return;
Dave Martincb84d112017-08-03 17:23:23 +01001022
1023 local_bh_disable();
1024
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001025 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001026 task_fpsimd_load();
Dave Martin8884b7b2017-12-06 16:45:46 +00001027 fpsimd_bind_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001028 }
Dave Martincb84d112017-08-03 17:23:23 +01001029
1030 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001031}
1032
1033/*
1034 * Load an updated userland FPSIMD state for 'current' from memory and set the
1035 * flag that indicates that the FPSIMD register contents are the most recent
1036 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001037 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001038void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001039{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001040 if (!system_supports_fpsimd())
1041 return;
Dave Martincb84d112017-08-03 17:23:23 +01001042
1043 local_bh_disable();
1044
Dave Martin65896542018-03-28 10:50:49 +01001045 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001046 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001047 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001048
Dave Martin8cd969d2017-10-31 15:51:07 +00001049 task_fpsimd_load();
1050
Dave Martin8884b7b2017-12-06 16:45:46 +00001051 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE))
1052 fpsimd_bind_to_cpu();
Dave Martincb84d112017-08-03 17:23:23 +01001053
1054 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001055}
1056
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001057/*
1058 * Invalidate live CPU copies of task t's FPSIMD state
1059 */
1060void fpsimd_flush_task_state(struct task_struct *t)
1061{
Dave Martin20b85472018-03-28 10:50:48 +01001062 t->thread.fpsimd_cpu = NR_CPUS;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001063}
1064
Dave Martin17eed272017-10-31 15:51:16 +00001065static inline void fpsimd_flush_cpu_state(void)
1066{
Dave Martincb968af2017-12-06 16:45:47 +00001067 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001068 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001069}
1070
1071/*
1072 * Invalidate any task SVE state currently held in this CPU's regs.
1073 *
1074 * This is used to prevent the kernel from trying to reuse SVE register data
1075 * that is detroyed by KVM guest enter/exit. This function should go away when
1076 * KVM SVE support is implemented. Don't use it for anything else.
1077 */
1078#ifdef CONFIG_ARM64_SVE
1079void sve_flush_cpu_state(void)
1080{
Dave Martincb968af2017-12-06 16:45:47 +00001081 struct fpsimd_last_state_struct const *last =
1082 this_cpu_ptr(&fpsimd_last_state);
Dave Martin17eed272017-10-31 15:51:16 +00001083
Dave Martincb968af2017-12-06 16:45:47 +00001084 if (last->st && last->sve_in_use)
Dave Martin17eed272017-10-31 15:51:16 +00001085 fpsimd_flush_cpu_state();
1086}
1087#endif /* CONFIG_ARM64_SVE */
1088
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001089#ifdef CONFIG_KERNEL_MODE_NEON
1090
Dave Martincb84d112017-08-03 17:23:23 +01001091DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001092EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001093
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001094/*
1095 * Kernel-side NEON support functions
1096 */
Dave Martincb84d112017-08-03 17:23:23 +01001097
1098/*
1099 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1100 * context
1101 *
1102 * Must not be called unless may_use_simd() returns true.
1103 * Task context in the FPSIMD registers is saved back to memory as necessary.
1104 *
1105 * A matching call to kernel_neon_end() must be made before returning from the
1106 * calling context.
1107 *
1108 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1109 * called.
1110 */
1111void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001112{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001113 if (WARN_ON(!system_supports_fpsimd()))
1114 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001115
Dave Martincb84d112017-08-03 17:23:23 +01001116 BUG_ON(!may_use_simd());
1117
1118 local_bh_disable();
1119
1120 __this_cpu_write(kernel_neon_busy, true);
1121
1122 /* Save unsaved task fpsimd state, if any: */
Dave Martind8ad71f2018-05-21 18:25:43 +01001123 if (current->mm)
Dave Martin1bd3f932017-10-31 15:51:11 +00001124 task_fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001125
1126 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001127 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001128
1129 preempt_disable();
1130
1131 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001132}
Dave Martincb84d112017-08-03 17:23:23 +01001133EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001134
Dave Martincb84d112017-08-03 17:23:23 +01001135/*
1136 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1137 *
1138 * Must be called from a context in which kernel_neon_begin() was previously
1139 * called, with no call to kernel_neon_end() in the meantime.
1140 *
1141 * The caller must not use the FPSIMD registers after this function is called,
1142 * unless kernel_neon_begin() is called again in the meantime.
1143 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001144void kernel_neon_end(void)
1145{
Dave Martincb84d112017-08-03 17:23:23 +01001146 bool busy;
1147
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001148 if (!system_supports_fpsimd())
1149 return;
Dave Martincb84d112017-08-03 17:23:23 +01001150
1151 busy = __this_cpu_xchg(kernel_neon_busy, false);
1152 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1153
1154 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001155}
1156EXPORT_SYMBOL(kernel_neon_end);
1157
Dave Martine580b8b2017-09-18 09:40:12 +01001158#ifdef CONFIG_EFI
1159
Dave Martin20b85472018-03-28 10:50:48 +01001160static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001161static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001162static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001163
1164/*
1165 * EFI runtime services support functions
1166 *
1167 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1168 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1169 * is always used rather than being an optional accelerator.
1170 *
1171 * These functions provide the necessary support for ensuring FPSIMD
1172 * save/restore in the contexts from which EFI is used.
1173 *
1174 * Do not use them for any other purpose -- if tempted to do so, you are
1175 * either doing something wrong or you need to propose some refactoring.
1176 */
1177
1178/*
1179 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1180 */
1181void __efi_fpsimd_begin(void)
1182{
1183 if (!system_supports_fpsimd())
1184 return;
1185
1186 WARN_ON(preemptible());
1187
Dave Martinfdfa9762017-10-31 15:51:12 +00001188 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001189 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001190 } else {
1191 /*
1192 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1193 * preserving:
1194 */
1195 if (system_supports_sve() && likely(efi_sve_state)) {
1196 char *sve_state = this_cpu_ptr(efi_sve_state);
1197
1198 __this_cpu_write(efi_sve_state_used, true);
1199
1200 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1201 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1202 } else {
1203 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1204 }
1205
Dave Martin4328825d2017-08-03 17:23:22 +01001206 __this_cpu_write(efi_fpsimd_state_used, true);
1207 }
1208}
1209
1210/*
1211 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1212 */
1213void __efi_fpsimd_end(void)
1214{
1215 if (!system_supports_fpsimd())
1216 return;
1217
Dave Martinfdfa9762017-10-31 15:51:12 +00001218 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001219 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001220 } else {
1221 if (system_supports_sve() &&
1222 likely(__this_cpu_read(efi_sve_state_used))) {
1223 char const *sve_state = this_cpu_ptr(efi_sve_state);
1224
1225 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1226 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1227 sve_vq_from_vl(sve_get_vl()) - 1);
1228
1229 __this_cpu_write(efi_sve_state_used, false);
1230 } else {
1231 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1232 }
1233 }
Dave Martin4328825d2017-08-03 17:23:22 +01001234}
1235
Dave Martine580b8b2017-09-18 09:40:12 +01001236#endif /* CONFIG_EFI */
1237
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001238#endif /* CONFIG_KERNEL_MODE_NEON */
1239
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001240#ifdef CONFIG_CPU_PM
1241static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1242 unsigned long cmd, void *v)
1243{
1244 switch (cmd) {
1245 case CPU_PM_ENTER:
Dave Martinbc0ee472017-10-31 15:51:05 +00001246 if (current->mm)
1247 task_fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001248 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001249 break;
1250 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001251 break;
1252 case CPU_PM_ENTER_FAILED:
1253 default:
1254 return NOTIFY_DONE;
1255 }
1256 return NOTIFY_OK;
1257}
1258
1259static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1260 .notifier_call = fpsimd_cpu_pm_notifier,
1261};
1262
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001263static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001264{
1265 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1266}
1267
1268#else
1269static inline void fpsimd_pm_init(void) { }
1270#endif /* CONFIG_CPU_PM */
1271
Janet Liu32365e62015-06-11 12:02:45 +08001272#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001273static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001274{
Dave Martincb968af2017-12-06 16:45:47 +00001275 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001276 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001277}
1278
Janet Liu32365e62015-06-11 12:02:45 +08001279static inline void fpsimd_hotplug_init(void)
1280{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001281 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1282 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001283}
1284
1285#else
1286static inline void fpsimd_hotplug_init(void) { }
1287#endif
1288
Catalin Marinas53631b52012-03-05 11:49:32 +00001289/*
1290 * FP/SIMD support code initialisation.
1291 */
1292static int __init fpsimd_init(void)
1293{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001294 if (elf_hwcap & HWCAP_FP) {
1295 fpsimd_pm_init();
1296 fpsimd_hotplug_init();
1297 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001298 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001299 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001300
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001301 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001302 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001303
Dave Martin4ffa09a2017-10-31 15:51:15 +00001304 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001305}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001306core_initcall(fpsimd_init);