blob: 8a93afa786007289db7003bc90e0ca0f59702270 [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 Martind06b76b2018-09-28 14:39:10 +010021#include <linux/bitops.h>
Dave Martincb84d112017-08-03 17:23:23 +010022#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000023#include <linux/bug.h>
Dave Martin7582e222017-10-31 15:51:08 +000024#include <linux/cache.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000025#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080026#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010027#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000028#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000029#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000030#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000031#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010032#include <linux/percpu.h>
Dave Martin2d2123b2017-10-31 15:51:14 +000033#include <linux/prctl.h>
Dave Martin4328825d2017-08-03 17:23:22 +010034#include <linux/preempt.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 Martin2cf97d42018-04-12 17:04:39 +010047#include <asm/processor.h>
Dave Martin4328825d2017-08-03 17:23:22 +010048#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000049#include <asm/sigcontext.h>
50#include <asm/sysreg.h>
51#include <asm/traps.h>
Dave Martind06b76b2018-09-28 14:39:10 +010052#include <asm/virt.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000053
54#define FPEXC_IOF (1 << 0)
55#define FPEXC_DZF (1 << 1)
56#define FPEXC_OFF (1 << 2)
57#define FPEXC_UFF (1 << 3)
58#define FPEXC_IXF (1 << 4)
59#define FPEXC_IDF (1 << 7)
60
61/*
Dave Martinbc0ee472017-10-31 15:51:05 +000062 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
63 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020064 * In order to reduce the number of times the FPSIMD state is needlessly saved
65 * and restored, we need to keep track of two things:
66 * (a) for each task, we need to remember which CPU was the last one to have
67 * the task's FPSIMD state loaded into its FPSIMD registers;
68 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
69 * been loaded into its FPSIMD registers most recently, or whether it has
70 * been used to perform kernel mode NEON in the meantime.
71 *
Dave Martin20b85472018-03-28 10:50:48 +010072 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080073 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020074 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
75 * address of the userland FPSIMD state of the task that was loaded onto the CPU
76 * the most recently, or NULL if kernel mode NEON has been performed after that.
77 *
78 * With this in place, we no longer have to restore the next FPSIMD state right
79 * when switching between tasks. Instead, we can defer this check to userland
80 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010081 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020082 * can omit the FPSIMD restore.
83 *
84 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
85 * indicate whether or not the userland FPSIMD state of the current task is
86 * present in the registers. The flag is set unless the FPSIMD registers of this
87 * CPU currently contain the most recent userland FPSIMD state of the current
88 * task.
89 *
Dave Martincb84d112017-08-03 17:23:23 +010090 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
91 * save the task's FPSIMD context back to task_struct from softirq context.
92 * To prevent this from racing with the manipulation of the task's FPSIMD state
93 * from task context and thereby corrupting the state, it is necessary to
94 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
95 * flag with local_bh_disable() unless softirqs are already masked.
96 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020097 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010098 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020099 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
100 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
101 * cleared, otherwise it is set;
102 *
103 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
104 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +0100105 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200106 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
107 * TIF_FOREIGN_FPSTATE flag is cleared;
108 *
109 * - the task executes an ordinary syscall; upon return to userland, the
110 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
111 * restored;
112 *
113 * - the task executes a syscall which executes some NEON instructions; this is
114 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
115 * register contents to memory, clears the fpsimd_last_state per-cpu variable
116 * and sets the TIF_FOREIGN_FPSTATE flag;
117 *
118 * - the task gets preempted after kernel_neon_end() is called; as we have not
119 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
120 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
121 */
Dave Martincb968af2017-12-06 16:45:47 +0000122struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100123 struct user_fpsimd_state *st;
Dave Martin04950672018-09-28 14:39:11 +0100124 void *sve_state;
125 unsigned int sve_vl;
Dave Martincb968af2017-12-06 16:45:47 +0000126};
127
128static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200129
Dave Martin79ab0472017-10-31 15:51:06 +0000130/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000131static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000132
Dave Martin7582e222017-10-31 15:51:08 +0000133#ifdef CONFIG_ARM64_SVE
134
135/* Maximum supported vector length across all CPUs (initially poisoned) */
Dave Martin87c021a2018-06-01 11:10:13 +0100136int __ro_after_init sve_max_vl = SVE_VL_MIN;
Dave Martind06b76b2018-09-28 14:39:10 +0100137int __ro_after_init sve_max_virtualisable_vl = SVE_VL_MIN;
Dave Martin7582e222017-10-31 15:51:08 +0000138/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000139static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100140/* Set of vector lengths present on at least one cpu: */
141static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000142static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000143
144#else /* ! CONFIG_ARM64_SVE */
145
146/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000147extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100148extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000149extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000150
151#endif /* ! CONFIG_ARM64_SVE */
152
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200153/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000154 * Call __sve_free() directly only if you know task can't be scheduled
155 * or preempted.
156 */
157static void __sve_free(struct task_struct *task)
158{
159 kfree(task->thread.sve_state);
160 task->thread.sve_state = NULL;
161}
162
163static void sve_free(struct task_struct *task)
164{
165 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
166
167 __sve_free(task);
168}
169
Dave Martinbc0ee472017-10-31 15:51:05 +0000170/*
171 * TIF_SVE controls whether a task can use SVE without trapping while
172 * in userspace, and also the way a task's FPSIMD/SVE state is stored
173 * in thread_struct.
174 *
175 * The kernel uses this flag to track whether a user task is actively
176 * using SVE, and therefore whether full SVE register state needs to
177 * be tracked. If not, the cheaper FPSIMD context handling code can
178 * be used instead of the more costly SVE equivalents.
179 *
180 * * TIF_SVE set:
181 *
182 * The task can execute SVE instructions while in userspace without
183 * trapping to the kernel.
184 *
185 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
186 * corresponding Zn), P0-P15 and FFR are encoded in in
187 * task->thread.sve_state, formatted appropriately for vector
188 * length task->thread.sve_vl.
189 *
190 * task->thread.sve_state must point to a valid buffer at least
191 * sve_state_size(task) bytes in size.
192 *
193 * During any syscall, the kernel may optionally clear TIF_SVE and
194 * discard the vector state except for the FPSIMD subset.
195 *
196 * * TIF_SVE clear:
197 *
198 * An attempt by the user task to execute an SVE instruction causes
199 * do_sve_acc() to be called, which does some preparation and then
200 * sets TIF_SVE.
201 *
202 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100203 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000204 * logically zero but not stored anywhere; P0-P15 and FFR are not
205 * stored and have unspecified values from userspace's point of
206 * view. For hygiene purposes, the kernel zeroes them on next use,
207 * but userspace is discouraged from relying on this.
208 *
209 * task->thread.sve_state does not need to be non-NULL, valid or any
210 * particular size: it must not be dereferenced.
211 *
Dave Martin65896542018-03-28 10:50:49 +0100212 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
213 * irrespective of whether TIF_SVE is clear or set, since these are
214 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000215 */
216
217/*
218 * Update current's FPSIMD/SVE registers from thread_struct.
219 *
220 * This function should be called only when the FPSIMD/SVE state in
221 * thread_struct is known to be up to date, when preparing to enter
222 * userspace.
223 *
224 * Softirqs (and preemption) must be disabled.
225 */
226static void task_fpsimd_load(void)
227{
228 WARN_ON(!in_softirq() && !irqs_disabled());
229
230 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin2cf97d42018-04-12 17:04:39 +0100231 sve_load_state(sve_pffr(&current->thread),
Dave Martin65896542018-03-28 10:50:49 +0100232 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000233 sve_vq_from_vl(current->thread.sve_vl) - 1);
234 else
Dave Martin65896542018-03-28 10:50:49 +0100235 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000236}
237
238/*
Dave Martind1797612018-04-06 14:55:59 +0100239 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
240 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000241 *
242 * Softirqs (and preemption) must be disabled.
243 */
Dave Martine6b673b2018-04-06 14:55:59 +0100244void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000245{
Dave Martin04950672018-09-28 14:39:11 +0100246 struct fpsimd_last_state_struct const *last =
247 this_cpu_ptr(&fpsimd_last_state);
Dave Martine6b673b2018-04-06 14:55:59 +0100248 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100249
Dave Martinbc0ee472017-10-31 15:51:05 +0000250 WARN_ON(!in_softirq() && !irqs_disabled());
251
252 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
253 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
Dave Martin04950672018-09-28 14:39:11 +0100254 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
Dave Martinbc0ee472017-10-31 15:51:05 +0000255 /*
256 * Can't save the user regs, so current would
257 * re-enter user with corrupt state.
258 * There's no way to recover, so kill it:
259 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000260 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000261 return;
262 }
263
Dave Martin04950672018-09-28 14:39:11 +0100264 sve_save_state((char *)last->sve_state +
265 sve_ffr_offset(last->sve_vl),
266 &last->st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000267 } else
Dave Martin04950672018-09-28 14:39:11 +0100268 fpsimd_save_state(last->st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000269 }
270}
271
Dave Martin7582e222017-10-31 15:51:08 +0000272/*
273 * Helpers to translate bit indices in sve_vq_map to VQ values (and
274 * vice versa). This allows find_next_bit() to be used to find the
275 * _maximum_ VQ not exceeding a certain value.
276 */
277
278static unsigned int vq_to_bit(unsigned int vq)
279{
280 return SVE_VQ_MAX - vq;
281}
282
283static unsigned int bit_to_vq(unsigned int bit)
284{
285 if (WARN_ON(bit >= SVE_VQ_MAX))
286 bit = SVE_VQ_MAX - 1;
287
288 return SVE_VQ_MAX - bit;
289}
290
291/*
292 * All vector length selection from userspace comes through here.
293 * We're on a slow path, so some sanity-checks are included.
294 * If things go wrong there's a bug somewhere, but try to fall back to a
295 * safe choice.
296 */
297static unsigned int find_supported_vector_length(unsigned int vl)
298{
299 int bit;
300 int max_vl = sve_max_vl;
301
302 if (WARN_ON(!sve_vl_valid(vl)))
303 vl = SVE_VL_MIN;
304
305 if (WARN_ON(!sve_vl_valid(max_vl)))
306 max_vl = SVE_VL_MIN;
307
308 if (vl > max_vl)
309 vl = max_vl;
310
311 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
312 vq_to_bit(sve_vq_from_vl(vl)));
313 return sve_vl_from_vq(bit_to_vq(bit));
314}
315
Dave Martin4ffa09a2017-10-31 15:51:15 +0000316#ifdef CONFIG_SYSCTL
317
318static int sve_proc_do_default_vl(struct ctl_table *table, int write,
319 void __user *buffer, size_t *lenp,
320 loff_t *ppos)
321{
322 int ret;
323 int vl = sve_default_vl;
324 struct ctl_table tmp_table = {
325 .data = &vl,
326 .maxlen = sizeof(vl),
327 };
328
329 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
330 if (ret || !write)
331 return ret;
332
333 /* Writing -1 has the special meaning "set to max": */
Dave Martin87c021a2018-06-01 11:10:13 +0100334 if (vl == -1)
335 vl = sve_max_vl;
Dave Martin4ffa09a2017-10-31 15:51:15 +0000336
337 if (!sve_vl_valid(vl))
338 return -EINVAL;
339
Dave Martin87c021a2018-06-01 11:10:13 +0100340 sve_default_vl = find_supported_vector_length(vl);
Dave Martin4ffa09a2017-10-31 15:51:15 +0000341 return 0;
342}
343
344static struct ctl_table sve_default_vl_table[] = {
345 {
346 .procname = "sve_default_vector_length",
347 .mode = 0644,
348 .proc_handler = sve_proc_do_default_vl,
349 },
350 { }
351};
352
353static int __init sve_sysctl_init(void)
354{
355 if (system_supports_sve())
356 if (!register_sysctl("abi", sve_default_vl_table))
357 return -EINVAL;
358
359 return 0;
360}
361
362#else /* ! CONFIG_SYSCTL */
363static int __init sve_sysctl_init(void) { return 0; }
364#endif /* ! CONFIG_SYSCTL */
365
Dave Martinbc0ee472017-10-31 15:51:05 +0000366#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
367 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
368
369/*
Dave Martin65896542018-03-28 10:50:49 +0100370 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000371 * task->thread.sve_state.
372 *
373 * Task can be a non-runnable task, or current. In the latter case,
374 * softirqs (and preemption) must be disabled.
375 * task->thread.sve_state must point to at least sve_state_size(task)
376 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100377 * task->thread.uw.fpsimd_state must be up to date before calling this
378 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000379 */
380static void fpsimd_to_sve(struct task_struct *task)
381{
382 unsigned int vq;
383 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100384 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000385 unsigned int i;
386
387 if (!system_supports_sve())
388 return;
389
390 vq = sve_vq_from_vl(task->thread.sve_vl);
391 for (i = 0; i < 32; ++i)
392 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
393 sizeof(fst->vregs[i]));
394}
395
Dave Martin8cd969d2017-10-31 15:51:07 +0000396/*
397 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100398 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000399 *
400 * Task can be a non-runnable task, or current. In the latter case,
401 * softirqs (and preemption) must be disabled.
402 * task->thread.sve_state must point to at least sve_state_size(task)
403 * bytes of allocated kernel memory.
404 * task->thread.sve_state must be up to date before calling this function.
405 */
406static void sve_to_fpsimd(struct task_struct *task)
407{
408 unsigned int vq;
409 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100410 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000411 unsigned int i;
412
413 if (!system_supports_sve())
414 return;
415
416 vq = sve_vq_from_vl(task->thread.sve_vl);
417 for (i = 0; i < 32; ++i)
418 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
419 sizeof(fst->vregs[i]));
420}
421
Dave Martinbc0ee472017-10-31 15:51:05 +0000422#ifdef CONFIG_ARM64_SVE
423
424/*
425 * Return how many bytes of memory are required to store the full SVE
426 * state for task, given task's currently configured vector length.
427 */
428size_t sve_state_size(struct task_struct const *task)
429{
430 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
431}
432
433/*
434 * Ensure that task->thread.sve_state is allocated and sufficiently large.
435 *
436 * This function should be used only in preparation for replacing
437 * task->thread.sve_state with new data. The memory is always zeroed
438 * here to prevent stale data from showing through: this is done in
439 * the interest of testability and predictability: except in the
440 * do_sve_acc() case, there is no ABI requirement to hide stale data
441 * written previously be task.
442 */
443void sve_alloc(struct task_struct *task)
444{
445 if (task->thread.sve_state) {
446 memset(task->thread.sve_state, 0, sve_state_size(current));
447 return;
448 }
449
450 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
451 task->thread.sve_state =
452 kzalloc(sve_state_size(task), GFP_KERNEL);
453
454 /*
455 * If future SVE revisions can have larger vectors though,
456 * this may cease to be true:
457 */
458 BUG_ON(!task->thread.sve_state);
459}
460
Dave Martin43d4da2c42017-10-31 15:51:13 +0000461
462/*
463 * Ensure that task->thread.sve_state is up to date with respect to
464 * the user task, irrespective of when SVE is in use or not.
465 *
466 * This should only be called by ptrace. task must be non-runnable.
467 * task->thread.sve_state must point to at least sve_state_size(task)
468 * bytes of allocated kernel memory.
469 */
470void fpsimd_sync_to_sve(struct task_struct *task)
471{
472 if (!test_tsk_thread_flag(task, TIF_SVE))
473 fpsimd_to_sve(task);
474}
475
476/*
Dave Martin65896542018-03-28 10:50:49 +0100477 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000478 * the user task, irrespective of whether SVE is in use or not.
479 *
480 * This should only be called by ptrace. task must be non-runnable.
481 * task->thread.sve_state must point to at least sve_state_size(task)
482 * bytes of allocated kernel memory.
483 */
484void sve_sync_to_fpsimd(struct task_struct *task)
485{
486 if (test_tsk_thread_flag(task, TIF_SVE))
487 sve_to_fpsimd(task);
488}
489
490/*
491 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100492 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000493 *
494 * This should only be called by ptrace to merge new FPSIMD register
495 * values into a task for which SVE is currently active.
496 * task must be non-runnable.
497 * task->thread.sve_state must point to at least sve_state_size(task)
498 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100499 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000500 * the new FPSIMD register values to be merged in.
501 */
502void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
503{
504 unsigned int vq;
505 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100506 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000507 unsigned int i;
508
509 if (!test_tsk_thread_flag(task, TIF_SVE))
510 return;
511
512 vq = sve_vq_from_vl(task->thread.sve_vl);
513
514 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
515
516 for (i = 0; i < 32; ++i)
517 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
518 sizeof(fst->vregs[i]));
519}
520
Dave Martin7582e222017-10-31 15:51:08 +0000521int sve_set_vector_length(struct task_struct *task,
522 unsigned long vl, unsigned long flags)
523{
524 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
525 PR_SVE_SET_VL_ONEXEC))
526 return -EINVAL;
527
528 if (!sve_vl_valid(vl))
529 return -EINVAL;
530
531 /*
532 * Clamp to the maximum vector length that VL-agnostic SVE code can
533 * work with. A flag may be assigned in the future to allow setting
534 * of larger vector lengths without confusing older software.
535 */
536 if (vl > SVE_VL_ARCH_MAX)
537 vl = SVE_VL_ARCH_MAX;
538
539 vl = find_supported_vector_length(vl);
540
541 if (flags & (PR_SVE_VL_INHERIT |
542 PR_SVE_SET_VL_ONEXEC))
543 task->thread.sve_vl_onexec = vl;
544 else
545 /* Reset VL to system default on next exec: */
546 task->thread.sve_vl_onexec = 0;
547
548 /* Only actually set the VL if not deferred: */
549 if (flags & PR_SVE_SET_VL_ONEXEC)
550 goto out;
551
552 if (vl == task->thread.sve_vl)
553 goto out;
554
555 /*
556 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
557 * write any live register state back to task_struct, and convert to a
558 * non-SVE thread.
559 */
560 if (task == current) {
561 local_bh_disable();
562
Dave Martind1797612018-04-06 14:55:59 +0100563 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000564 }
565
566 fpsimd_flush_task_state(task);
567 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
568 sve_to_fpsimd(task);
569
570 if (task == current)
571 local_bh_enable();
572
573 /*
574 * Force reallocation of task SVE state to the correct size
575 * on next use:
576 */
577 sve_free(task);
578
579 task->thread.sve_vl = vl;
580
581out:
Dave Martin09d12232018-04-11 17:59:06 +0100582 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
583 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000584
585 return 0;
586}
587
Dave Martinbc0ee472017-10-31 15:51:05 +0000588/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000589 * Encode the current vector length and flags for return.
590 * This is only required for prctl(): ptrace has separate fields
591 *
592 * flags are as for sve_set_vector_length().
593 */
594static int sve_prctl_status(unsigned long flags)
595{
596 int ret;
597
598 if (flags & PR_SVE_SET_VL_ONEXEC)
599 ret = current->thread.sve_vl_onexec;
600 else
601 ret = current->thread.sve_vl;
602
603 if (test_thread_flag(TIF_SVE_VL_INHERIT))
604 ret |= PR_SVE_VL_INHERIT;
605
606 return ret;
607}
608
609/* PR_SVE_SET_VL */
610int sve_set_current_vl(unsigned long arg)
611{
612 unsigned long vl, flags;
613 int ret;
614
615 vl = arg & PR_SVE_VL_LEN_MASK;
616 flags = arg & ~vl;
617
618 if (!system_supports_sve())
619 return -EINVAL;
620
621 ret = sve_set_vector_length(current, vl, flags);
622 if (ret)
623 return ret;
624
625 return sve_prctl_status(flags);
626}
627
628/* PR_SVE_GET_VL */
629int sve_get_current_vl(void)
630{
631 if (!system_supports_sve())
632 return -EINVAL;
633
634 return sve_prctl_status(0);
635}
636
Dave Martin2e0f2472017-10-31 15:51:10 +0000637static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
638{
639 unsigned int vq, vl;
640 unsigned long zcr;
641
642 bitmap_zero(map, SVE_VQ_MAX);
643
644 zcr = ZCR_ELx_LEN_MASK;
645 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
646
647 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
648 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
649 vl = sve_get_vl();
650 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
651 set_bit(vq_to_bit(vq), map);
652 }
653}
654
Dave Martin8b08e842018-12-06 16:32:35 +0000655/*
656 * Initialise the set of known supported VQs for the boot CPU.
657 * This is called during kernel boot, before secondary CPUs are brought up.
658 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000659void __init sve_init_vq_map(void)
660{
661 sve_probe_vqs(sve_vq_map);
Dave Martind06b76b2018-09-28 14:39:10 +0100662 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000663}
664
665/*
666 * If we haven't committed to the set of supported VQs yet, filter out
667 * those not supported by the current CPU.
Dave Martin8b08e842018-12-06 16:32:35 +0000668 * This function is called during the bring-up of early secondary CPUs only.
Dave Martin2e0f2472017-10-31 15:51:10 +0000669 */
670void sve_update_vq_map(void)
671{
Dave Martind06b76b2018-09-28 14:39:10 +0100672 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
673
674 sve_probe_vqs(tmp_map);
675 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
676 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000677}
678
Dave Martin8b08e842018-12-06 16:32:35 +0000679/*
680 * Check whether the current CPU supports all VQs in the committed set.
681 * This function is called during the bring-up of late secondary CPUs only.
682 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000683int sve_verify_vq_map(void)
684{
Dave Martind06b76b2018-09-28 14:39:10 +0100685 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
686 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000687
Dave Martind06b76b2018-09-28 14:39:10 +0100688 sve_probe_vqs(tmp_map);
689
690 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
691 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
Dave Martin2e0f2472017-10-31 15:51:10 +0000692 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
693 smp_processor_id());
Dave Martind06b76b2018-09-28 14:39:10 +0100694 return -EINVAL;
Dave Martin2e0f2472017-10-31 15:51:10 +0000695 }
696
Dave Martind06b76b2018-09-28 14:39:10 +0100697 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
698 return 0;
699
700 /*
701 * For KVM, it is necessary to ensure that this CPU doesn't
702 * support any vector length that guests may have probed as
703 * unsupported.
704 */
705
706 /* Recover the set of supported VQs: */
707 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
708 /* Find VQs supported that are not globally supported: */
709 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
710
711 /* Find the lowest such VQ, if any: */
712 b = find_last_bit(tmp_map, SVE_VQ_MAX);
713 if (b >= SVE_VQ_MAX)
714 return 0; /* no mismatches */
715
716 /*
717 * Mismatches above sve_max_virtualisable_vl are fine, since
718 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
719 */
720 if (sve_vl_from_vq(bit_to_vq(b)) <= sve_max_virtualisable_vl) {
721 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
722 smp_processor_id());
723 return -EINVAL;
724 }
725
726 return 0;
Dave Martin2e0f2472017-10-31 15:51:10 +0000727}
728
Dave Martinfdfa9762017-10-31 15:51:12 +0000729static void __init sve_efi_setup(void)
730{
731 if (!IS_ENABLED(CONFIG_EFI))
732 return;
733
734 /*
735 * alloc_percpu() warns and prints a backtrace if this goes wrong.
736 * This is evidence of a crippled system and we are returning void,
737 * so no attempt is made to handle this situation here.
738 */
739 if (!sve_vl_valid(sve_max_vl))
740 goto fail;
741
742 efi_sve_state = __alloc_percpu(
743 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
744 if (!efi_sve_state)
745 goto fail;
746
747 return;
748
749fail:
750 panic("Cannot allocate percpu memory for EFI SVE save/restore");
751}
752
Dave Martin2e0f2472017-10-31 15:51:10 +0000753/*
754 * Enable SVE for EL1.
755 * Intended for use by the cpufeatures code during CPU boot.
756 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100757void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000758{
759 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
760 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000761}
762
Dave Martin31dc52b2018-04-12 16:47:20 +0100763/*
764 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
765 * vector length.
766 *
767 * Use only if SVE is present.
768 * This function clobbers the SVE vector length.
769 */
770u64 read_zcr_features(void)
771{
772 u64 zcr;
773 unsigned int vq_max;
774
775 /*
776 * Set the maximum possible VL, and write zeroes to all other
777 * bits to see if they stick.
778 */
779 sve_kernel_enable(NULL);
780 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
781
782 zcr = read_sysreg_s(SYS_ZCR_EL1);
783 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
784 vq_max = sve_vq_from_vl(sve_get_vl());
785 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
786
787 return zcr;
788}
789
Dave Martin2e0f2472017-10-31 15:51:10 +0000790void __init sve_setup(void)
791{
792 u64 zcr;
Dave Martind06b76b2018-09-28 14:39:10 +0100793 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
794 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000795
796 if (!system_supports_sve())
797 return;
798
799 /*
800 * The SVE architecture mandates support for 128-bit vectors,
801 * so sve_vq_map must have at least SVE_VQ_MIN set.
802 * If something went wrong, at least try to patch it up:
803 */
804 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
805 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
806
807 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
808 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
809
810 /*
811 * Sanity-check that the max VL we determined through CPU features
812 * corresponds properly to sve_vq_map. If not, do our best:
813 */
814 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
815 sve_max_vl = find_supported_vector_length(sve_max_vl);
816
817 /*
818 * For the default VL, pick the maximum supported value <= 64.
819 * VL == 64 is guaranteed not to grow the signal frame.
820 */
821 sve_default_vl = find_supported_vector_length(64);
822
Dave Martind06b76b2018-09-28 14:39:10 +0100823 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
824 SVE_VQ_MAX);
825
826 b = find_last_bit(tmp_map, SVE_VQ_MAX);
827 if (b >= SVE_VQ_MAX)
828 /* No non-virtualisable VLs found */
829 sve_max_virtualisable_vl = SVE_VQ_MAX;
830 else if (WARN_ON(b == SVE_VQ_MAX - 1))
831 /* No virtualisable VLs? This is architecturally forbidden. */
832 sve_max_virtualisable_vl = SVE_VQ_MIN;
833 else /* b + 1 < SVE_VQ_MAX */
834 sve_max_virtualisable_vl = sve_vl_from_vq(bit_to_vq(b + 1));
835
836 if (sve_max_virtualisable_vl > sve_max_vl)
837 sve_max_virtualisable_vl = sve_max_vl;
838
Dave Martin2e0f2472017-10-31 15:51:10 +0000839 pr_info("SVE: maximum available vector length %u bytes per vector\n",
840 sve_max_vl);
841 pr_info("SVE: default vector length %u bytes per vector\n",
842 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000843
Dave Martind06b76b2018-09-28 14:39:10 +0100844 /* KVM decides whether to support mismatched systems. Just warn here: */
845 if (sve_max_virtualisable_vl < sve_max_vl)
846 pr_warn("SVE: unvirtualisable vector lengths present\n");
847
Dave Martinfdfa9762017-10-31 15:51:12 +0000848 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000849}
850
851/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000852 * Called from the put_task_struct() path, which cannot get here
853 * unless dead_task is really dead and not schedulable.
854 */
855void fpsimd_release_task(struct task_struct *dead_task)
856{
857 __sve_free(dead_task);
858}
859
860#endif /* CONFIG_ARM64_SVE */
861
862/*
863 * Trapped SVE access
864 *
865 * Storage is allocated for the full SVE state, the current FPSIMD
866 * register contents are migrated across, and TIF_SVE is set so that
867 * the SVE access trap will be disabled the next time this task
868 * reaches ret_to_user.
869 *
870 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
871 * would have disabled the SVE access trap for userspace during
872 * ret_to_user, making an SVE access trap impossible in that case.
873 */
874asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
875{
876 /* Even if we chose not to use SVE, the hardware could still trap: */
877 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000878 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000879 return;
880 }
881
882 sve_alloc(current);
883
884 local_bh_disable();
885
Dave Martind1797612018-04-06 14:55:59 +0100886 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000887
888 /* Force ret_to_user to reload the registers: */
889 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000890
Dave Martinefbc2022018-09-28 14:39:05 +0100891 fpsimd_to_sve(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000892 if (test_and_set_thread_flag(TIF_SVE))
893 WARN_ON(1); /* SVE access shouldn't have trapped */
894
895 local_bh_enable();
896}
897
Catalin Marinas53631b52012-03-05 11:49:32 +0000898/*
899 * Trapped FP/ASIMD access.
900 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000901asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000902{
903 /* TODO: implement lazy context saving/restoring */
904 WARN_ON(1);
905}
906
907/*
908 * Raise a SIGFPE for the current process.
909 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000910asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000911{
Dave Martinaf4a81b2018-03-01 17:44:07 +0000912 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000913
Dave Martinaf4a81b2018-03-01 17:44:07 +0000914 if (esr & ESR_ELx_FP_EXC_TFV) {
915 if (esr & FPEXC_IOF)
916 si_code = FPE_FLTINV;
917 else if (esr & FPEXC_DZF)
918 si_code = FPE_FLTDIV;
919 else if (esr & FPEXC_OFF)
920 si_code = FPE_FLTOVF;
921 else if (esr & FPEXC_UFF)
922 si_code = FPE_FLTUND;
923 else if (esr & FPEXC_IXF)
924 si_code = FPE_FLTRES;
925 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000926
Eric W. Biedermanc8526802018-04-16 13:47:06 -0500927 send_sig_fault(SIGFPE, si_code,
928 (void __user *)instruction_pointer(regs),
929 current);
Catalin Marinas53631b52012-03-05 11:49:32 +0000930}
931
932void fpsimd_thread_switch(struct task_struct *next)
933{
Dave Martindf3fb962018-05-21 19:08:15 +0100934 bool wrong_task, wrong_cpu;
935
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000936 if (!system_supports_fpsimd())
937 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200938
Dave Martindf3fb962018-05-21 19:08:15 +0100939 /* Save unsaved fpsimd state, if any: */
940 fpsimd_save();
941
Catalin Marinas53631b52012-03-05 11:49:32 +0000942 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100943 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
944 * state. For kernel threads, FPSIMD registers are never loaded
945 * and wrong_task and wrong_cpu will always be true.
Catalin Marinas53631b52012-03-05 11:49:32 +0000946 */
Dave Martindf3fb962018-05-21 19:08:15 +0100947 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100948 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100949 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100950
Dave Martindf3fb962018-05-21 19:08:15 +0100951 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
952 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000953}
954
955void fpsimd_flush_thread(void)
956{
Dave Martin7582e222017-10-31 15:51:08 +0000957 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000958
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000959 if (!system_supports_fpsimd())
960 return;
Dave Martincb84d112017-08-03 17:23:23 +0100961
962 local_bh_disable();
963
Dave Martinefbc2022018-09-28 14:39:05 +0100964 fpsimd_flush_task_state(current);
Dave Martin65896542018-03-28 10:50:49 +0100965 memset(&current->thread.uw.fpsimd_state, 0,
966 sizeof(current->thread.uw.fpsimd_state));
Dave Martinbc0ee472017-10-31 15:51:05 +0000967
968 if (system_supports_sve()) {
969 clear_thread_flag(TIF_SVE);
970 sve_free(current);
971
972 /*
973 * Reset the task vector length as required.
974 * This is where we ensure that all user tasks have a valid
975 * vector length configured: no kernel task can become a user
976 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000977 * By the time the first call to this function is made, all
978 * early hardware probing is complete, so sve_default_vl
979 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000980 * If a bug causes this to go wrong, we make some noise and
981 * try to fudge thread.sve_vl to a safe value here.
982 */
Dave Martin79ab0472017-10-31 15:51:06 +0000983 vl = current->thread.sve_vl_onexec ?
984 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000985
986 if (WARN_ON(!sve_vl_valid(vl)))
987 vl = SVE_VL_MIN;
988
Dave Martin7582e222017-10-31 15:51:08 +0000989 supported_vl = find_supported_vector_length(vl);
990 if (WARN_ON(supported_vl != vl))
991 vl = supported_vl;
992
Dave Martinbc0ee472017-10-31 15:51:05 +0000993 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000994
995 /*
996 * If the task is not set to inherit, ensure that the vector
997 * length will be reset by a subsequent exec:
998 */
999 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
1000 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +00001001 }
1002
Dave Martincb84d112017-08-03 17:23:23 +01001003 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +00001004}
1005
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001006/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001007 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1008 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001009 */
1010void fpsimd_preserve_current_state(void)
1011{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001012 if (!system_supports_fpsimd())
1013 return;
Dave Martincb84d112017-08-03 17:23:23 +01001014
1015 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +01001016 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001017 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001018}
1019
1020/*
Dave Martin8cd969d2017-10-31 15:51:07 +00001021 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +01001022 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +00001023 * the signal frame.
1024 */
1025void fpsimd_signal_preserve_current_state(void)
1026{
1027 fpsimd_preserve_current_state();
1028 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1029 sve_to_fpsimd(current);
1030}
1031
1032/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001033 * Associate current's FPSIMD context with this cpu
1034 * Preemption must be disabled when calling this function.
1035 */
Dave Martine6b673b2018-04-06 14:55:59 +01001036void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +00001037{
Dave Martincb968af2017-12-06 16:45:47 +00001038 struct fpsimd_last_state_struct *last =
1039 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001040
Dave Martin65896542018-03-28 10:50:49 +01001041 last->st = &current->thread.uw.fpsimd_state;
Dave Martin04950672018-09-28 14:39:11 +01001042 last->sve_state = current->thread.sve_state;
1043 last->sve_vl = current->thread.sve_vl;
Dave Martin20b85472018-03-28 10:50:48 +01001044 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +01001045
1046 if (system_supports_sve()) {
1047 /* Toggle SVE trapping for userspace if needed */
1048 if (test_thread_flag(TIF_SVE))
1049 sve_user_enable();
1050 else
1051 sve_user_disable();
1052
1053 /* Serialised by exception return to user */
1054 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001055}
1056
Dave Martin04950672018-09-28 14:39:11 +01001057void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1058 unsigned int sve_vl)
Dave Martine6b673b2018-04-06 14:55:59 +01001059{
1060 struct fpsimd_last_state_struct *last =
1061 this_cpu_ptr(&fpsimd_last_state);
1062
1063 WARN_ON(!in_softirq() && !irqs_disabled());
1064
1065 last->st = st;
Dave Martin04950672018-09-28 14:39:11 +01001066 last->sve_state = sve_state;
1067 last->sve_vl = sve_vl;
Dave Martin8884b7b2017-12-06 16:45:46 +00001068}
1069
1070/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001071 * Load the userland FPSIMD state of 'current' from memory, but only if the
1072 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1073 * state of 'current'
1074 */
1075void fpsimd_restore_current_state(void)
1076{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001077 if (!system_supports_fpsimd())
1078 return;
Dave Martincb84d112017-08-03 17:23:23 +01001079
1080 local_bh_disable();
1081
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001082 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001083 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001084 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001085 }
Dave Martincb84d112017-08-03 17:23:23 +01001086
1087 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001088}
1089
1090/*
1091 * Load an updated userland FPSIMD state for 'current' from memory and set the
1092 * flag that indicates that the FPSIMD register contents are the most recent
1093 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001094 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001095void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001096{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001097 if (!system_supports_fpsimd())
1098 return;
Dave Martincb84d112017-08-03 17:23:23 +01001099
1100 local_bh_disable();
1101
Dave Martin65896542018-03-28 10:50:49 +01001102 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001103 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001104 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001105
Dave Martin8cd969d2017-10-31 15:51:07 +00001106 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001107 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001108
Dave Martin0cff8e72018-05-09 14:27:41 +01001109 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001110
1111 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001112}
1113
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001114/*
1115 * Invalidate live CPU copies of task t's FPSIMD state
Dave Martinefbc2022018-09-28 14:39:05 +01001116 *
1117 * This function may be called with preemption enabled. The barrier()
1118 * ensures that the assignment to fpsimd_cpu is visible to any
1119 * preemption/softirq that could race with set_tsk_thread_flag(), so
1120 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1121 *
1122 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1123 * subsequent code.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001124 */
1125void fpsimd_flush_task_state(struct task_struct *t)
1126{
Dave Martin20b85472018-03-28 10:50:48 +01001127 t->thread.fpsimd_cpu = NR_CPUS;
Dave Martinefbc2022018-09-28 14:39:05 +01001128
1129 barrier();
1130 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1131
1132 barrier();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001133}
1134
Dave Martinefbc2022018-09-28 14:39:05 +01001135/*
1136 * Invalidate any task's FPSIMD state that is present on this cpu.
1137 * This function must be called with softirqs disabled.
1138 */
Dave Martine6b673b2018-04-06 14:55:59 +01001139void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001140{
Dave Martincb968af2017-12-06 16:45:47 +00001141 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001142 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001143}
1144
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001145#ifdef CONFIG_KERNEL_MODE_NEON
1146
Dave Martincb84d112017-08-03 17:23:23 +01001147DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001148EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001149
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001150/*
1151 * Kernel-side NEON support functions
1152 */
Dave Martincb84d112017-08-03 17:23:23 +01001153
1154/*
1155 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1156 * context
1157 *
1158 * Must not be called unless may_use_simd() returns true.
1159 * Task context in the FPSIMD registers is saved back to memory as necessary.
1160 *
1161 * A matching call to kernel_neon_end() must be made before returning from the
1162 * calling context.
1163 *
1164 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1165 * called.
1166 */
1167void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001168{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001169 if (WARN_ON(!system_supports_fpsimd()))
1170 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001171
Dave Martincb84d112017-08-03 17:23:23 +01001172 BUG_ON(!may_use_simd());
1173
1174 local_bh_disable();
1175
1176 __this_cpu_write(kernel_neon_busy, true);
1177
Dave Martindf3fb962018-05-21 19:08:15 +01001178 /* Save unsaved fpsimd state, if any: */
1179 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001180
1181 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001182 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001183
1184 preempt_disable();
1185
1186 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001187}
Dave Martincb84d112017-08-03 17:23:23 +01001188EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001189
Dave Martincb84d112017-08-03 17:23:23 +01001190/*
1191 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1192 *
1193 * Must be called from a context in which kernel_neon_begin() was previously
1194 * called, with no call to kernel_neon_end() in the meantime.
1195 *
1196 * The caller must not use the FPSIMD registers after this function is called,
1197 * unless kernel_neon_begin() is called again in the meantime.
1198 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001199void kernel_neon_end(void)
1200{
Dave Martincb84d112017-08-03 17:23:23 +01001201 bool busy;
1202
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001203 if (!system_supports_fpsimd())
1204 return;
Dave Martincb84d112017-08-03 17:23:23 +01001205
1206 busy = __this_cpu_xchg(kernel_neon_busy, false);
1207 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1208
1209 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001210}
1211EXPORT_SYMBOL(kernel_neon_end);
1212
Dave Martine580b8b2017-09-18 09:40:12 +01001213#ifdef CONFIG_EFI
1214
Dave Martin20b85472018-03-28 10:50:48 +01001215static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001216static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001217static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001218
1219/*
1220 * EFI runtime services support functions
1221 *
1222 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1223 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1224 * is always used rather than being an optional accelerator.
1225 *
1226 * These functions provide the necessary support for ensuring FPSIMD
1227 * save/restore in the contexts from which EFI is used.
1228 *
1229 * Do not use them for any other purpose -- if tempted to do so, you are
1230 * either doing something wrong or you need to propose some refactoring.
1231 */
1232
1233/*
1234 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1235 */
1236void __efi_fpsimd_begin(void)
1237{
1238 if (!system_supports_fpsimd())
1239 return;
1240
1241 WARN_ON(preemptible());
1242
Dave Martinfdfa9762017-10-31 15:51:12 +00001243 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001244 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001245 } else {
1246 /*
1247 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1248 * preserving:
1249 */
1250 if (system_supports_sve() && likely(efi_sve_state)) {
1251 char *sve_state = this_cpu_ptr(efi_sve_state);
1252
1253 __this_cpu_write(efi_sve_state_used, true);
1254
1255 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1256 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1257 } else {
1258 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1259 }
1260
Dave Martin4328825d2017-08-03 17:23:22 +01001261 __this_cpu_write(efi_fpsimd_state_used, true);
1262 }
1263}
1264
1265/*
1266 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1267 */
1268void __efi_fpsimd_end(void)
1269{
1270 if (!system_supports_fpsimd())
1271 return;
1272
Dave Martinfdfa9762017-10-31 15:51:12 +00001273 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001274 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001275 } else {
1276 if (system_supports_sve() &&
1277 likely(__this_cpu_read(efi_sve_state_used))) {
1278 char const *sve_state = this_cpu_ptr(efi_sve_state);
1279
1280 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1281 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1282 sve_vq_from_vl(sve_get_vl()) - 1);
1283
1284 __this_cpu_write(efi_sve_state_used, false);
1285 } else {
1286 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1287 }
1288 }
Dave Martin4328825d2017-08-03 17:23:22 +01001289}
1290
Dave Martine580b8b2017-09-18 09:40:12 +01001291#endif /* CONFIG_EFI */
1292
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001293#endif /* CONFIG_KERNEL_MODE_NEON */
1294
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001295#ifdef CONFIG_CPU_PM
1296static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1297 unsigned long cmd, void *v)
1298{
1299 switch (cmd) {
1300 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001301 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001302 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001303 break;
1304 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001305 break;
1306 case CPU_PM_ENTER_FAILED:
1307 default:
1308 return NOTIFY_DONE;
1309 }
1310 return NOTIFY_OK;
1311}
1312
1313static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1314 .notifier_call = fpsimd_cpu_pm_notifier,
1315};
1316
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001317static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001318{
1319 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1320}
1321
1322#else
1323static inline void fpsimd_pm_init(void) { }
1324#endif /* CONFIG_CPU_PM */
1325
Janet Liu32365e62015-06-11 12:02:45 +08001326#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001327static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001328{
Dave Martincb968af2017-12-06 16:45:47 +00001329 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001330 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001331}
1332
Janet Liu32365e62015-06-11 12:02:45 +08001333static inline void fpsimd_hotplug_init(void)
1334{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001335 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1336 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001337}
1338
1339#else
1340static inline void fpsimd_hotplug_init(void) { }
1341#endif
1342
Catalin Marinas53631b52012-03-05 11:49:32 +00001343/*
1344 * FP/SIMD support code initialisation.
1345 */
1346static int __init fpsimd_init(void)
1347{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001348 if (elf_hwcap & HWCAP_FP) {
1349 fpsimd_pm_init();
1350 fpsimd_hotplug_init();
1351 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001352 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001353 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001354
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001355 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001356 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001357
Dave Martin4ffa09a2017-10-31 15:51:15 +00001358 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001359}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001360core_initcall(fpsimd_init);