blob: 6c01ee2062c4df7171dcf43691a39a6a88111da3 [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 Martin31dc52b2018-04-12 16:47:20 +010040#include <linux/stddef.h>
Dave Martin4ffa09a2017-10-31 15:51:15 +000041#include <linux/sysctl.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000042
Dave Martinaf4a81b2018-03-01 17:44:07 +000043#include <asm/esr.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000044#include <asm/fpsimd.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010045#include <asm/cpufeature.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000046#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010047#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000048#include <asm/sigcontext.h>
49#include <asm/sysreg.h>
50#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000051
52#define FPEXC_IOF (1 << 0)
53#define FPEXC_DZF (1 << 1)
54#define FPEXC_OFF (1 << 2)
55#define FPEXC_UFF (1 << 3)
56#define FPEXC_IXF (1 << 4)
57#define FPEXC_IDF (1 << 7)
58
59/*
Dave Martinbc0ee472017-10-31 15:51:05 +000060 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
61 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020062 * In order to reduce the number of times the FPSIMD state is needlessly saved
63 * and restored, we need to keep track of two things:
64 * (a) for each task, we need to remember which CPU was the last one to have
65 * the task's FPSIMD state loaded into its FPSIMD registers;
66 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
67 * been loaded into its FPSIMD registers most recently, or whether it has
68 * been used to perform kernel mode NEON in the meantime.
69 *
Dave Martin20b85472018-03-28 10:50:48 +010070 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080071 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020072 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
73 * address of the userland FPSIMD state of the task that was loaded onto the CPU
74 * the most recently, or NULL if kernel mode NEON has been performed after that.
75 *
76 * With this in place, we no longer have to restore the next FPSIMD state right
77 * when switching between tasks. Instead, we can defer this check to userland
78 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010079 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020080 * can omit the FPSIMD restore.
81 *
82 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
83 * indicate whether or not the userland FPSIMD state of the current task is
84 * present in the registers. The flag is set unless the FPSIMD registers of this
85 * CPU currently contain the most recent userland FPSIMD state of the current
86 * task.
87 *
Dave Martincb84d112017-08-03 17:23:23 +010088 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
89 * save the task's FPSIMD context back to task_struct from softirq context.
90 * To prevent this from racing with the manipulation of the task's FPSIMD state
91 * from task context and thereby corrupting the state, it is necessary to
92 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
93 * flag with local_bh_disable() unless softirqs are already masked.
94 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020095 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010096 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020097 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
98 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
99 * cleared, otherwise it is set;
100 *
101 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
102 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +0100103 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200104 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
105 * TIF_FOREIGN_FPSTATE flag is cleared;
106 *
107 * - the task executes an ordinary syscall; upon return to userland, the
108 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
109 * restored;
110 *
111 * - the task executes a syscall which executes some NEON instructions; this is
112 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
113 * register contents to memory, clears the fpsimd_last_state per-cpu variable
114 * and sets the TIF_FOREIGN_FPSTATE flag;
115 *
116 * - the task gets preempted after kernel_neon_end() is called; as we have not
117 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
118 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
119 */
Dave Martincb968af2017-12-06 16:45:47 +0000120struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100121 struct user_fpsimd_state *st;
Dave Martincb968af2017-12-06 16:45:47 +0000122 bool sve_in_use;
123};
124
125static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200126
Dave Martin79ab0472017-10-31 15:51:06 +0000127/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000128static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000129
Dave Martin7582e222017-10-31 15:51:08 +0000130#ifdef CONFIG_ARM64_SVE
131
132/* Maximum supported vector length across all CPUs (initially poisoned) */
133int __ro_after_init sve_max_vl = -1;
134/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000135static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000136static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000137
138#else /* ! CONFIG_ARM64_SVE */
139
140/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000141extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000142extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000143
144#endif /* ! CONFIG_ARM64_SVE */
145
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200146/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000147 * Call __sve_free() directly only if you know task can't be scheduled
148 * or preempted.
149 */
150static void __sve_free(struct task_struct *task)
151{
152 kfree(task->thread.sve_state);
153 task->thread.sve_state = NULL;
154}
155
156static void sve_free(struct task_struct *task)
157{
158 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
159
160 __sve_free(task);
161}
162
163
164/* Offset of FFR in the SVE register dump */
165static size_t sve_ffr_offset(int vl)
166{
167 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
168}
169
170static void *sve_pffr(struct task_struct *task)
171{
172 return (char *)task->thread.sve_state +
173 sve_ffr_offset(task->thread.sve_vl);
174}
175
176static void change_cpacr(u64 val, u64 mask)
177{
178 u64 cpacr = read_sysreg(CPACR_EL1);
179 u64 new = (cpacr & ~mask) | val;
180
181 if (new != cpacr)
182 write_sysreg(new, CPACR_EL1);
183}
184
185static void sve_user_disable(void)
186{
187 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
188}
189
190static void sve_user_enable(void)
191{
192 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
193}
194
195/*
196 * TIF_SVE controls whether a task can use SVE without trapping while
197 * in userspace, and also the way a task's FPSIMD/SVE state is stored
198 * in thread_struct.
199 *
200 * The kernel uses this flag to track whether a user task is actively
201 * using SVE, and therefore whether full SVE register state needs to
202 * be tracked. If not, the cheaper FPSIMD context handling code can
203 * be used instead of the more costly SVE equivalents.
204 *
205 * * TIF_SVE set:
206 *
207 * The task can execute SVE instructions while in userspace without
208 * trapping to the kernel.
209 *
210 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
211 * corresponding Zn), P0-P15 and FFR are encoded in in
212 * task->thread.sve_state, formatted appropriately for vector
213 * length task->thread.sve_vl.
214 *
215 * task->thread.sve_state must point to a valid buffer at least
216 * sve_state_size(task) bytes in size.
217 *
218 * During any syscall, the kernel may optionally clear TIF_SVE and
219 * discard the vector state except for the FPSIMD subset.
220 *
221 * * TIF_SVE clear:
222 *
223 * An attempt by the user task to execute an SVE instruction causes
224 * do_sve_acc() to be called, which does some preparation and then
225 * sets TIF_SVE.
226 *
227 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100228 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000229 * logically zero but not stored anywhere; P0-P15 and FFR are not
230 * stored and have unspecified values from userspace's point of
231 * view. For hygiene purposes, the kernel zeroes them on next use,
232 * but userspace is discouraged from relying on this.
233 *
234 * task->thread.sve_state does not need to be non-NULL, valid or any
235 * particular size: it must not be dereferenced.
236 *
Dave Martin65896542018-03-28 10:50:49 +0100237 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
238 * irrespective of whether TIF_SVE is clear or set, since these are
239 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000240 */
241
242/*
243 * Update current's FPSIMD/SVE registers from thread_struct.
244 *
245 * This function should be called only when the FPSIMD/SVE state in
246 * thread_struct is known to be up to date, when preparing to enter
247 * userspace.
248 *
249 * Softirqs (and preemption) must be disabled.
250 */
251static void task_fpsimd_load(void)
252{
253 WARN_ON(!in_softirq() && !irqs_disabled());
254
255 if (system_supports_sve() && test_thread_flag(TIF_SVE))
256 sve_load_state(sve_pffr(current),
Dave Martin65896542018-03-28 10:50:49 +0100257 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000258 sve_vq_from_vl(current->thread.sve_vl) - 1);
259 else
Dave Martin65896542018-03-28 10:50:49 +0100260 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000261}
262
263/*
Dave Martind1797612018-04-06 14:55:59 +0100264 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
265 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000266 *
267 * Softirqs (and preemption) must be disabled.
268 */
Dave Martine6b673b2018-04-06 14:55:59 +0100269void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000270{
Dave Martind1797612018-04-06 14:55:59 +0100271 struct user_fpsimd_state *st = __this_cpu_read(fpsimd_last_state.st);
Dave Martine6b673b2018-04-06 14:55:59 +0100272 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100273
Dave Martinbc0ee472017-10-31 15:51:05 +0000274 WARN_ON(!in_softirq() && !irqs_disabled());
275
276 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
277 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
278 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
279 /*
280 * Can't save the user regs, so current would
281 * re-enter user with corrupt state.
282 * There's no way to recover, so kill it:
283 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000284 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000285 return;
286 }
287
Dave Martind1797612018-04-06 14:55:59 +0100288 sve_save_state(sve_pffr(current), &st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000289 } else
Dave Martind1797612018-04-06 14:55:59 +0100290 fpsimd_save_state(st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000291 }
292}
293
Dave Martin7582e222017-10-31 15:51:08 +0000294/*
295 * Helpers to translate bit indices in sve_vq_map to VQ values (and
296 * vice versa). This allows find_next_bit() to be used to find the
297 * _maximum_ VQ not exceeding a certain value.
298 */
299
300static unsigned int vq_to_bit(unsigned int vq)
301{
302 return SVE_VQ_MAX - vq;
303}
304
305static unsigned int bit_to_vq(unsigned int bit)
306{
307 if (WARN_ON(bit >= SVE_VQ_MAX))
308 bit = SVE_VQ_MAX - 1;
309
310 return SVE_VQ_MAX - bit;
311}
312
313/*
314 * All vector length selection from userspace comes through here.
315 * We're on a slow path, so some sanity-checks are included.
316 * If things go wrong there's a bug somewhere, but try to fall back to a
317 * safe choice.
318 */
319static unsigned int find_supported_vector_length(unsigned int vl)
320{
321 int bit;
322 int max_vl = sve_max_vl;
323
324 if (WARN_ON(!sve_vl_valid(vl)))
325 vl = SVE_VL_MIN;
326
327 if (WARN_ON(!sve_vl_valid(max_vl)))
328 max_vl = SVE_VL_MIN;
329
330 if (vl > max_vl)
331 vl = max_vl;
332
333 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
334 vq_to_bit(sve_vq_from_vl(vl)));
335 return sve_vl_from_vq(bit_to_vq(bit));
336}
337
Dave Martin4ffa09a2017-10-31 15:51:15 +0000338#ifdef CONFIG_SYSCTL
339
340static int sve_proc_do_default_vl(struct ctl_table *table, int write,
341 void __user *buffer, size_t *lenp,
342 loff_t *ppos)
343{
344 int ret;
345 int vl = sve_default_vl;
346 struct ctl_table tmp_table = {
347 .data = &vl,
348 .maxlen = sizeof(vl),
349 };
350
351 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
352 if (ret || !write)
353 return ret;
354
355 /* Writing -1 has the special meaning "set to max": */
356 if (vl == -1) {
357 /* Fail safe if sve_max_vl wasn't initialised */
358 if (WARN_ON(!sve_vl_valid(sve_max_vl)))
359 vl = SVE_VL_MIN;
360 else
361 vl = sve_max_vl;
362
363 goto chosen;
364 }
365
366 if (!sve_vl_valid(vl))
367 return -EINVAL;
368
369 vl = find_supported_vector_length(vl);
370chosen:
371 sve_default_vl = vl;
372 return 0;
373}
374
375static struct ctl_table sve_default_vl_table[] = {
376 {
377 .procname = "sve_default_vector_length",
378 .mode = 0644,
379 .proc_handler = sve_proc_do_default_vl,
380 },
381 { }
382};
383
384static int __init sve_sysctl_init(void)
385{
386 if (system_supports_sve())
387 if (!register_sysctl("abi", sve_default_vl_table))
388 return -EINVAL;
389
390 return 0;
391}
392
393#else /* ! CONFIG_SYSCTL */
394static int __init sve_sysctl_init(void) { return 0; }
395#endif /* ! CONFIG_SYSCTL */
396
Dave Martinbc0ee472017-10-31 15:51:05 +0000397#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
398 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
399
400/*
Dave Martin65896542018-03-28 10:50:49 +0100401 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000402 * task->thread.sve_state.
403 *
404 * Task can be a non-runnable task, or current. In the latter case,
405 * softirqs (and preemption) must be disabled.
406 * task->thread.sve_state must point to at least sve_state_size(task)
407 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100408 * task->thread.uw.fpsimd_state must be up to date before calling this
409 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000410 */
411static void fpsimd_to_sve(struct task_struct *task)
412{
413 unsigned int vq;
414 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100415 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000416 unsigned int i;
417
418 if (!system_supports_sve())
419 return;
420
421 vq = sve_vq_from_vl(task->thread.sve_vl);
422 for (i = 0; i < 32; ++i)
423 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
424 sizeof(fst->vregs[i]));
425}
426
Dave Martin8cd969d2017-10-31 15:51:07 +0000427/*
428 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100429 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000430 *
431 * Task can be a non-runnable task, or current. In the latter case,
432 * softirqs (and preemption) must be disabled.
433 * task->thread.sve_state must point to at least sve_state_size(task)
434 * bytes of allocated kernel memory.
435 * task->thread.sve_state must be up to date before calling this function.
436 */
437static void sve_to_fpsimd(struct task_struct *task)
438{
439 unsigned int vq;
440 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100441 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000442 unsigned int i;
443
444 if (!system_supports_sve())
445 return;
446
447 vq = sve_vq_from_vl(task->thread.sve_vl);
448 for (i = 0; i < 32; ++i)
449 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
450 sizeof(fst->vregs[i]));
451}
452
Dave Martinbc0ee472017-10-31 15:51:05 +0000453#ifdef CONFIG_ARM64_SVE
454
455/*
456 * Return how many bytes of memory are required to store the full SVE
457 * state for task, given task's currently configured vector length.
458 */
459size_t sve_state_size(struct task_struct const *task)
460{
461 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
462}
463
464/*
465 * Ensure that task->thread.sve_state is allocated and sufficiently large.
466 *
467 * This function should be used only in preparation for replacing
468 * task->thread.sve_state with new data. The memory is always zeroed
469 * here to prevent stale data from showing through: this is done in
470 * the interest of testability and predictability: except in the
471 * do_sve_acc() case, there is no ABI requirement to hide stale data
472 * written previously be task.
473 */
474void sve_alloc(struct task_struct *task)
475{
476 if (task->thread.sve_state) {
477 memset(task->thread.sve_state, 0, sve_state_size(current));
478 return;
479 }
480
481 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
482 task->thread.sve_state =
483 kzalloc(sve_state_size(task), GFP_KERNEL);
484
485 /*
486 * If future SVE revisions can have larger vectors though,
487 * this may cease to be true:
488 */
489 BUG_ON(!task->thread.sve_state);
490}
491
Dave Martin43d4da2c42017-10-31 15:51:13 +0000492
493/*
494 * Ensure that task->thread.sve_state is up to date with respect to
495 * the user task, irrespective of when SVE is in use or not.
496 *
497 * This should only be called by ptrace. task must be non-runnable.
498 * task->thread.sve_state must point to at least sve_state_size(task)
499 * bytes of allocated kernel memory.
500 */
501void fpsimd_sync_to_sve(struct task_struct *task)
502{
503 if (!test_tsk_thread_flag(task, TIF_SVE))
504 fpsimd_to_sve(task);
505}
506
507/*
Dave Martin65896542018-03-28 10:50:49 +0100508 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000509 * the user task, irrespective of whether SVE is in use or not.
510 *
511 * This should only be called by ptrace. task must be non-runnable.
512 * task->thread.sve_state must point to at least sve_state_size(task)
513 * bytes of allocated kernel memory.
514 */
515void sve_sync_to_fpsimd(struct task_struct *task)
516{
517 if (test_tsk_thread_flag(task, TIF_SVE))
518 sve_to_fpsimd(task);
519}
520
521/*
522 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100523 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000524 *
525 * This should only be called by ptrace to merge new FPSIMD register
526 * values into a task for which SVE is currently active.
527 * task must be non-runnable.
528 * task->thread.sve_state must point to at least sve_state_size(task)
529 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100530 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000531 * the new FPSIMD register values to be merged in.
532 */
533void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
534{
535 unsigned int vq;
536 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100537 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000538 unsigned int i;
539
540 if (!test_tsk_thread_flag(task, TIF_SVE))
541 return;
542
543 vq = sve_vq_from_vl(task->thread.sve_vl);
544
545 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
546
547 for (i = 0; i < 32; ++i)
548 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
549 sizeof(fst->vregs[i]));
550}
551
Dave Martin7582e222017-10-31 15:51:08 +0000552int sve_set_vector_length(struct task_struct *task,
553 unsigned long vl, unsigned long flags)
554{
555 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
556 PR_SVE_SET_VL_ONEXEC))
557 return -EINVAL;
558
559 if (!sve_vl_valid(vl))
560 return -EINVAL;
561
562 /*
563 * Clamp to the maximum vector length that VL-agnostic SVE code can
564 * work with. A flag may be assigned in the future to allow setting
565 * of larger vector lengths without confusing older software.
566 */
567 if (vl > SVE_VL_ARCH_MAX)
568 vl = SVE_VL_ARCH_MAX;
569
570 vl = find_supported_vector_length(vl);
571
572 if (flags & (PR_SVE_VL_INHERIT |
573 PR_SVE_SET_VL_ONEXEC))
574 task->thread.sve_vl_onexec = vl;
575 else
576 /* Reset VL to system default on next exec: */
577 task->thread.sve_vl_onexec = 0;
578
579 /* Only actually set the VL if not deferred: */
580 if (flags & PR_SVE_SET_VL_ONEXEC)
581 goto out;
582
583 if (vl == task->thread.sve_vl)
584 goto out;
585
586 /*
587 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
588 * write any live register state back to task_struct, and convert to a
589 * non-SVE thread.
590 */
591 if (task == current) {
592 local_bh_disable();
593
Dave Martind1797612018-04-06 14:55:59 +0100594 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000595 set_thread_flag(TIF_FOREIGN_FPSTATE);
596 }
597
598 fpsimd_flush_task_state(task);
599 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
600 sve_to_fpsimd(task);
601
602 if (task == current)
603 local_bh_enable();
604
605 /*
606 * Force reallocation of task SVE state to the correct size
607 * on next use:
608 */
609 sve_free(task);
610
611 task->thread.sve_vl = vl;
612
613out:
Dave Martin09d12232018-04-11 17:59:06 +0100614 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
615 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000616
617 return 0;
618}
619
Dave Martinbc0ee472017-10-31 15:51:05 +0000620/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000621 * Encode the current vector length and flags for return.
622 * This is only required for prctl(): ptrace has separate fields
623 *
624 * flags are as for sve_set_vector_length().
625 */
626static int sve_prctl_status(unsigned long flags)
627{
628 int ret;
629
630 if (flags & PR_SVE_SET_VL_ONEXEC)
631 ret = current->thread.sve_vl_onexec;
632 else
633 ret = current->thread.sve_vl;
634
635 if (test_thread_flag(TIF_SVE_VL_INHERIT))
636 ret |= PR_SVE_VL_INHERIT;
637
638 return ret;
639}
640
641/* PR_SVE_SET_VL */
642int sve_set_current_vl(unsigned long arg)
643{
644 unsigned long vl, flags;
645 int ret;
646
647 vl = arg & PR_SVE_VL_LEN_MASK;
648 flags = arg & ~vl;
649
650 if (!system_supports_sve())
651 return -EINVAL;
652
653 ret = sve_set_vector_length(current, vl, flags);
654 if (ret)
655 return ret;
656
657 return sve_prctl_status(flags);
658}
659
660/* PR_SVE_GET_VL */
661int sve_get_current_vl(void)
662{
663 if (!system_supports_sve())
664 return -EINVAL;
665
666 return sve_prctl_status(0);
667}
668
669/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000670 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
671 * during secondary boot.
672 */
673static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
674
675static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
676{
677 unsigned int vq, vl;
678 unsigned long zcr;
679
680 bitmap_zero(map, SVE_VQ_MAX);
681
682 zcr = ZCR_ELx_LEN_MASK;
683 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
684
685 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
686 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
687 vl = sve_get_vl();
688 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
689 set_bit(vq_to_bit(vq), map);
690 }
691}
692
693void __init sve_init_vq_map(void)
694{
695 sve_probe_vqs(sve_vq_map);
696}
697
698/*
699 * If we haven't committed to the set of supported VQs yet, filter out
700 * those not supported by the current CPU.
701 */
702void sve_update_vq_map(void)
703{
704 sve_probe_vqs(sve_secondary_vq_map);
705 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
706}
707
708/* Check whether the current CPU supports all VQs in the committed set */
709int sve_verify_vq_map(void)
710{
711 int ret = 0;
712
713 sve_probe_vqs(sve_secondary_vq_map);
714 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
715 SVE_VQ_MAX);
716 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
717 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
718 smp_processor_id());
719 ret = -EINVAL;
720 }
721
722 return ret;
723}
724
Dave Martinfdfa9762017-10-31 15:51:12 +0000725static void __init sve_efi_setup(void)
726{
727 if (!IS_ENABLED(CONFIG_EFI))
728 return;
729
730 /*
731 * alloc_percpu() warns and prints a backtrace if this goes wrong.
732 * This is evidence of a crippled system and we are returning void,
733 * so no attempt is made to handle this situation here.
734 */
735 if (!sve_vl_valid(sve_max_vl))
736 goto fail;
737
738 efi_sve_state = __alloc_percpu(
739 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
740 if (!efi_sve_state)
741 goto fail;
742
743 return;
744
745fail:
746 panic("Cannot allocate percpu memory for EFI SVE save/restore");
747}
748
Dave Martin2e0f2472017-10-31 15:51:10 +0000749/*
750 * Enable SVE for EL1.
751 * Intended for use by the cpufeatures code during CPU boot.
752 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100753void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000754{
755 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
756 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000757}
758
Dave Martin31dc52b2018-04-12 16:47:20 +0100759/*
760 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
761 * vector length.
762 *
763 * Use only if SVE is present.
764 * This function clobbers the SVE vector length.
765 */
766u64 read_zcr_features(void)
767{
768 u64 zcr;
769 unsigned int vq_max;
770
771 /*
772 * Set the maximum possible VL, and write zeroes to all other
773 * bits to see if they stick.
774 */
775 sve_kernel_enable(NULL);
776 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
777
778 zcr = read_sysreg_s(SYS_ZCR_EL1);
779 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
780 vq_max = sve_vq_from_vl(sve_get_vl());
781 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
782
783 return zcr;
784}
785
Dave Martin2e0f2472017-10-31 15:51:10 +0000786void __init sve_setup(void)
787{
788 u64 zcr;
789
790 if (!system_supports_sve())
791 return;
792
793 /*
794 * The SVE architecture mandates support for 128-bit vectors,
795 * so sve_vq_map must have at least SVE_VQ_MIN set.
796 * If something went wrong, at least try to patch it up:
797 */
798 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
799 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
800
801 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
802 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
803
804 /*
805 * Sanity-check that the max VL we determined through CPU features
806 * corresponds properly to sve_vq_map. If not, do our best:
807 */
808 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
809 sve_max_vl = find_supported_vector_length(sve_max_vl);
810
811 /*
812 * For the default VL, pick the maximum supported value <= 64.
813 * VL == 64 is guaranteed not to grow the signal frame.
814 */
815 sve_default_vl = find_supported_vector_length(64);
816
817 pr_info("SVE: maximum available vector length %u bytes per vector\n",
818 sve_max_vl);
819 pr_info("SVE: default vector length %u bytes per vector\n",
820 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000821
822 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000823}
824
825/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000826 * Called from the put_task_struct() path, which cannot get here
827 * unless dead_task is really dead and not schedulable.
828 */
829void fpsimd_release_task(struct task_struct *dead_task)
830{
831 __sve_free(dead_task);
832}
833
834#endif /* CONFIG_ARM64_SVE */
835
836/*
837 * Trapped SVE access
838 *
839 * Storage is allocated for the full SVE state, the current FPSIMD
840 * register contents are migrated across, and TIF_SVE is set so that
841 * the SVE access trap will be disabled the next time this task
842 * reaches ret_to_user.
843 *
844 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
845 * would have disabled the SVE access trap for userspace during
846 * ret_to_user, making an SVE access trap impossible in that case.
847 */
848asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
849{
850 /* Even if we chose not to use SVE, the hardware could still trap: */
851 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000852 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000853 return;
854 }
855
856 sve_alloc(current);
857
858 local_bh_disable();
859
Dave Martind1797612018-04-06 14:55:59 +0100860 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000861 fpsimd_to_sve(current);
862
863 /* Force ret_to_user to reload the registers: */
864 fpsimd_flush_task_state(current);
865 set_thread_flag(TIF_FOREIGN_FPSTATE);
866
867 if (test_and_set_thread_flag(TIF_SVE))
868 WARN_ON(1); /* SVE access shouldn't have trapped */
869
870 local_bh_enable();
871}
872
Catalin Marinas53631b52012-03-05 11:49:32 +0000873/*
874 * Trapped FP/ASIMD access.
875 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000876asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000877{
878 /* TODO: implement lazy context saving/restoring */
879 WARN_ON(1);
880}
881
882/*
883 * Raise a SIGFPE for the current process.
884 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000885asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000886{
887 siginfo_t info;
Dave Martinaf4a81b2018-03-01 17:44:07 +0000888 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000889
Dave Martinaf4a81b2018-03-01 17:44:07 +0000890 if (esr & ESR_ELx_FP_EXC_TFV) {
891 if (esr & FPEXC_IOF)
892 si_code = FPE_FLTINV;
893 else if (esr & FPEXC_DZF)
894 si_code = FPE_FLTDIV;
895 else if (esr & FPEXC_OFF)
896 si_code = FPE_FLTOVF;
897 else if (esr & FPEXC_UFF)
898 si_code = FPE_FLTUND;
899 else if (esr & FPEXC_IXF)
900 si_code = FPE_FLTRES;
901 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000902
903 memset(&info, 0, sizeof(info));
904 info.si_signo = SIGFPE;
905 info.si_code = si_code;
906 info.si_addr = (void __user *)instruction_pointer(regs);
907
908 send_sig_info(SIGFPE, &info, current);
909}
910
911void fpsimd_thread_switch(struct task_struct *next)
912{
Dave Martindf3fb962018-05-21 19:08:15 +0100913 bool wrong_task, wrong_cpu;
914
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000915 if (!system_supports_fpsimd())
916 return;
Dave Martindf3fb962018-05-21 19:08:15 +0100917
918 /* Save unsaved fpsimd state, if any: */
919 fpsimd_save();
920
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200921 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100922 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
923 * state. For kernel threads, FPSIMD registers are never loaded
924 * and wrong_task and wrong_cpu will always be true.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200925 */
Dave Martindf3fb962018-05-21 19:08:15 +0100926 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100927 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100928 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100929
Dave Martindf3fb962018-05-21 19:08:15 +0100930 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
931 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000932}
933
934void fpsimd_flush_thread(void)
935{
Dave Martin7582e222017-10-31 15:51:08 +0000936 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000937
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000938 if (!system_supports_fpsimd())
939 return;
Dave Martincb84d112017-08-03 17:23:23 +0100940
941 local_bh_disable();
942
Dave Martin65896542018-03-28 10:50:49 +0100943 memset(&current->thread.uw.fpsimd_state, 0,
944 sizeof(current->thread.uw.fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100945 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000946
947 if (system_supports_sve()) {
948 clear_thread_flag(TIF_SVE);
949 sve_free(current);
950
951 /*
952 * Reset the task vector length as required.
953 * This is where we ensure that all user tasks have a valid
954 * vector length configured: no kernel task can become a user
955 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000956 * By the time the first call to this function is made, all
957 * early hardware probing is complete, so sve_default_vl
958 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000959 * If a bug causes this to go wrong, we make some noise and
960 * try to fudge thread.sve_vl to a safe value here.
961 */
Dave Martin79ab0472017-10-31 15:51:06 +0000962 vl = current->thread.sve_vl_onexec ?
963 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000964
965 if (WARN_ON(!sve_vl_valid(vl)))
966 vl = SVE_VL_MIN;
967
Dave Martin7582e222017-10-31 15:51:08 +0000968 supported_vl = find_supported_vector_length(vl);
969 if (WARN_ON(supported_vl != vl))
970 vl = supported_vl;
971
Dave Martinbc0ee472017-10-31 15:51:05 +0000972 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000973
974 /*
975 * If the task is not set to inherit, ensure that the vector
976 * length will be reset by a subsequent exec:
977 */
978 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
979 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000980 }
981
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200982 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100983
984 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000985}
986
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100987/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200988 * Save the userland FPSIMD state of 'current' to memory, but only if the state
989 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100990 */
991void fpsimd_preserve_current_state(void)
992{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000993 if (!system_supports_fpsimd())
994 return;
Dave Martincb84d112017-08-03 17:23:23 +0100995
996 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +0100997 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100998 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100999}
1000
1001/*
Dave Martin8cd969d2017-10-31 15:51:07 +00001002 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +01001003 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +00001004 * the signal frame.
1005 */
1006void fpsimd_signal_preserve_current_state(void)
1007{
1008 fpsimd_preserve_current_state();
1009 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1010 sve_to_fpsimd(current);
1011}
1012
1013/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001014 * Associate current's FPSIMD context with this cpu
1015 * Preemption must be disabled when calling this function.
1016 */
Dave Martine6b673b2018-04-06 14:55:59 +01001017void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +00001018{
Dave Martincb968af2017-12-06 16:45:47 +00001019 struct fpsimd_last_state_struct *last =
1020 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001021
Dave Martin65896542018-03-28 10:50:49 +01001022 last->st = &current->thread.uw.fpsimd_state;
Dave Martincb968af2017-12-06 16:45:47 +00001023 last->sve_in_use = test_thread_flag(TIF_SVE);
Dave Martin20b85472018-03-28 10:50:48 +01001024 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +01001025
1026 if (system_supports_sve()) {
1027 /* Toggle SVE trapping for userspace if needed */
1028 if (test_thread_flag(TIF_SVE))
1029 sve_user_enable();
1030 else
1031 sve_user_disable();
1032
1033 /* Serialised by exception return to user */
1034 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001035}
1036
Dave Martine6b673b2018-04-06 14:55:59 +01001037void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st)
1038{
1039 struct fpsimd_last_state_struct *last =
1040 this_cpu_ptr(&fpsimd_last_state);
1041
1042 WARN_ON(!in_softirq() && !irqs_disabled());
1043
1044 last->st = st;
1045 last->sve_in_use = false;
1046}
1047
Dave Martin8884b7b2017-12-06 16:45:46 +00001048/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001049 * Load the userland FPSIMD state of 'current' from memory, but only if the
1050 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1051 * state of 'current'
1052 */
1053void fpsimd_restore_current_state(void)
1054{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001055 if (!system_supports_fpsimd())
1056 return;
Dave Martincb84d112017-08-03 17:23:23 +01001057
1058 local_bh_disable();
1059
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001060 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001061 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001062 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001063 }
Dave Martincb84d112017-08-03 17:23:23 +01001064
1065 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001066}
1067
1068/*
1069 * Load an updated userland FPSIMD state for 'current' from memory and set the
1070 * flag that indicates that the FPSIMD register contents are the most recent
1071 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001072 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001073void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001074{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001075 if (!system_supports_fpsimd())
1076 return;
Dave Martincb84d112017-08-03 17:23:23 +01001077
1078 local_bh_disable();
1079
Dave Martin65896542018-03-28 10:50:49 +01001080 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001081 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001082 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001083
Dave Martin8cd969d2017-10-31 15:51:07 +00001084 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001085 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001086
Dave Martin0cff8e72018-05-09 14:27:41 +01001087 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001088
1089 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001090}
1091
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001092/*
1093 * Invalidate live CPU copies of task t's FPSIMD state
1094 */
1095void fpsimd_flush_task_state(struct task_struct *t)
1096{
Dave Martin20b85472018-03-28 10:50:48 +01001097 t->thread.fpsimd_cpu = NR_CPUS;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001098}
1099
Dave Martine6b673b2018-04-06 14:55:59 +01001100void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001101{
Dave Martincb968af2017-12-06 16:45:47 +00001102 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001103 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001104}
1105
1106/*
1107 * Invalidate any task SVE state currently held in this CPU's regs.
1108 *
1109 * This is used to prevent the kernel from trying to reuse SVE register data
1110 * that is detroyed by KVM guest enter/exit. This function should go away when
1111 * KVM SVE support is implemented. Don't use it for anything else.
1112 */
1113#ifdef CONFIG_ARM64_SVE
1114void sve_flush_cpu_state(void)
1115{
Dave Martincb968af2017-12-06 16:45:47 +00001116 struct fpsimd_last_state_struct const *last =
1117 this_cpu_ptr(&fpsimd_last_state);
Dave Martin17eed272017-10-31 15:51:16 +00001118
Dave Martincb968af2017-12-06 16:45:47 +00001119 if (last->st && last->sve_in_use)
Dave Martin17eed272017-10-31 15:51:16 +00001120 fpsimd_flush_cpu_state();
1121}
1122#endif /* CONFIG_ARM64_SVE */
1123
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001124#ifdef CONFIG_KERNEL_MODE_NEON
1125
Dave Martincb84d112017-08-03 17:23:23 +01001126DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001127EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001128
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001129/*
1130 * Kernel-side NEON support functions
1131 */
Dave Martincb84d112017-08-03 17:23:23 +01001132
1133/*
1134 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1135 * context
1136 *
1137 * Must not be called unless may_use_simd() returns true.
1138 * Task context in the FPSIMD registers is saved back to memory as necessary.
1139 *
1140 * A matching call to kernel_neon_end() must be made before returning from the
1141 * calling context.
1142 *
1143 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1144 * called.
1145 */
1146void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001147{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001148 if (WARN_ON(!system_supports_fpsimd()))
1149 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001150
Dave Martincb84d112017-08-03 17:23:23 +01001151 BUG_ON(!may_use_simd());
1152
1153 local_bh_disable();
1154
1155 __this_cpu_write(kernel_neon_busy, true);
1156
Dave Martindf3fb962018-05-21 19:08:15 +01001157 /* Save unsaved fpsimd state, if any: */
1158 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001159
1160 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001161 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001162
1163 preempt_disable();
1164
1165 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001166}
Dave Martincb84d112017-08-03 17:23:23 +01001167EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001168
Dave Martincb84d112017-08-03 17:23:23 +01001169/*
1170 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1171 *
1172 * Must be called from a context in which kernel_neon_begin() was previously
1173 * called, with no call to kernel_neon_end() in the meantime.
1174 *
1175 * The caller must not use the FPSIMD registers after this function is called,
1176 * unless kernel_neon_begin() is called again in the meantime.
1177 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001178void kernel_neon_end(void)
1179{
Dave Martincb84d112017-08-03 17:23:23 +01001180 bool busy;
1181
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001182 if (!system_supports_fpsimd())
1183 return;
Dave Martincb84d112017-08-03 17:23:23 +01001184
1185 busy = __this_cpu_xchg(kernel_neon_busy, false);
1186 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1187
1188 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001189}
1190EXPORT_SYMBOL(kernel_neon_end);
1191
Dave Martine580b8b2017-09-18 09:40:12 +01001192#ifdef CONFIG_EFI
1193
Dave Martin20b85472018-03-28 10:50:48 +01001194static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001195static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001196static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001197
1198/*
1199 * EFI runtime services support functions
1200 *
1201 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1202 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1203 * is always used rather than being an optional accelerator.
1204 *
1205 * These functions provide the necessary support for ensuring FPSIMD
1206 * save/restore in the contexts from which EFI is used.
1207 *
1208 * Do not use them for any other purpose -- if tempted to do so, you are
1209 * either doing something wrong or you need to propose some refactoring.
1210 */
1211
1212/*
1213 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1214 */
1215void __efi_fpsimd_begin(void)
1216{
1217 if (!system_supports_fpsimd())
1218 return;
1219
1220 WARN_ON(preemptible());
1221
Dave Martinfdfa9762017-10-31 15:51:12 +00001222 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001223 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001224 } else {
1225 /*
1226 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1227 * preserving:
1228 */
1229 if (system_supports_sve() && likely(efi_sve_state)) {
1230 char *sve_state = this_cpu_ptr(efi_sve_state);
1231
1232 __this_cpu_write(efi_sve_state_used, true);
1233
1234 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1235 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1236 } else {
1237 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1238 }
1239
Dave Martin4328825d2017-08-03 17:23:22 +01001240 __this_cpu_write(efi_fpsimd_state_used, true);
1241 }
1242}
1243
1244/*
1245 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1246 */
1247void __efi_fpsimd_end(void)
1248{
1249 if (!system_supports_fpsimd())
1250 return;
1251
Dave Martinfdfa9762017-10-31 15:51:12 +00001252 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001253 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001254 } else {
1255 if (system_supports_sve() &&
1256 likely(__this_cpu_read(efi_sve_state_used))) {
1257 char const *sve_state = this_cpu_ptr(efi_sve_state);
1258
1259 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1260 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1261 sve_vq_from_vl(sve_get_vl()) - 1);
1262
1263 __this_cpu_write(efi_sve_state_used, false);
1264 } else {
1265 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1266 }
1267 }
Dave Martin4328825d2017-08-03 17:23:22 +01001268}
1269
Dave Martine580b8b2017-09-18 09:40:12 +01001270#endif /* CONFIG_EFI */
1271
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001272#endif /* CONFIG_KERNEL_MODE_NEON */
1273
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001274#ifdef CONFIG_CPU_PM
1275static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1276 unsigned long cmd, void *v)
1277{
1278 switch (cmd) {
1279 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001280 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001281 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001282 break;
1283 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001284 break;
1285 case CPU_PM_ENTER_FAILED:
1286 default:
1287 return NOTIFY_DONE;
1288 }
1289 return NOTIFY_OK;
1290}
1291
1292static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1293 .notifier_call = fpsimd_cpu_pm_notifier,
1294};
1295
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001296static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001297{
1298 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1299}
1300
1301#else
1302static inline void fpsimd_pm_init(void) { }
1303#endif /* CONFIG_CPU_PM */
1304
Janet Liu32365e62015-06-11 12:02:45 +08001305#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001306static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001307{
Dave Martincb968af2017-12-06 16:45:47 +00001308 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001309 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001310}
1311
Janet Liu32365e62015-06-11 12:02:45 +08001312static inline void fpsimd_hotplug_init(void)
1313{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001314 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1315 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001316}
1317
1318#else
1319static inline void fpsimd_hotplug_init(void) { }
1320#endif
1321
Catalin Marinas53631b52012-03-05 11:49:32 +00001322/*
1323 * FP/SIMD support code initialisation.
1324 */
1325static int __init fpsimd_init(void)
1326{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001327 if (elf_hwcap & HWCAP_FP) {
1328 fpsimd_pm_init();
1329 fpsimd_hotplug_init();
1330 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001331 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001332 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001333
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001334 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001335 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001336
Dave Martin4ffa09a2017-10-31 15:51:15 +00001337 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001338}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001339core_initcall(fpsimd_init);