blob: 6448921a2f5974d57426d122d4d3b779a7cfad49 [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 Martin624835a2019-04-11 16:53:18 +0100138
139/*
140 * Set of available vector lengths,
141 * where length vq encoded as bit __vq_to_bit(vq):
142 */
Dave Martinead9e432018-09-28 14:39:21 +0100143__ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100144/* Set of vector lengths present on at least one cpu: */
145static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martin624835a2019-04-11 16:53:18 +0100146
Dave Martinfdfa9762017-10-31 15:51:12 +0000147static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000148
149#else /* ! CONFIG_ARM64_SVE */
150
151/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000152extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100153extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000154extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000155
156#endif /* ! CONFIG_ARM64_SVE */
157
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200158/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000159 * Call __sve_free() directly only if you know task can't be scheduled
160 * or preempted.
161 */
162static void __sve_free(struct task_struct *task)
163{
164 kfree(task->thread.sve_state);
165 task->thread.sve_state = NULL;
166}
167
168static void sve_free(struct task_struct *task)
169{
170 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
171
172 __sve_free(task);
173}
174
Dave Martinbc0ee472017-10-31 15:51:05 +0000175/*
176 * TIF_SVE controls whether a task can use SVE without trapping while
177 * in userspace, and also the way a task's FPSIMD/SVE state is stored
178 * in thread_struct.
179 *
180 * The kernel uses this flag to track whether a user task is actively
181 * using SVE, and therefore whether full SVE register state needs to
182 * be tracked. If not, the cheaper FPSIMD context handling code can
183 * be used instead of the more costly SVE equivalents.
184 *
185 * * TIF_SVE set:
186 *
187 * The task can execute SVE instructions while in userspace without
188 * trapping to the kernel.
189 *
190 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
191 * corresponding Zn), P0-P15 and FFR are encoded in in
192 * task->thread.sve_state, formatted appropriately for vector
193 * length task->thread.sve_vl.
194 *
195 * task->thread.sve_state must point to a valid buffer at least
196 * sve_state_size(task) bytes in size.
197 *
198 * During any syscall, the kernel may optionally clear TIF_SVE and
199 * discard the vector state except for the FPSIMD subset.
200 *
201 * * TIF_SVE clear:
202 *
203 * An attempt by the user task to execute an SVE instruction causes
204 * do_sve_acc() to be called, which does some preparation and then
205 * sets TIF_SVE.
206 *
207 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100208 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000209 * logically zero but not stored anywhere; P0-P15 and FFR are not
210 * stored and have unspecified values from userspace's point of
211 * view. For hygiene purposes, the kernel zeroes them on next use,
212 * but userspace is discouraged from relying on this.
213 *
214 * task->thread.sve_state does not need to be non-NULL, valid or any
215 * particular size: it must not be dereferenced.
216 *
Dave Martin65896542018-03-28 10:50:49 +0100217 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
218 * irrespective of whether TIF_SVE is clear or set, since these are
219 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000220 */
221
222/*
223 * Update current's FPSIMD/SVE registers from thread_struct.
224 *
225 * This function should be called only when the FPSIMD/SVE state in
226 * thread_struct is known to be up to date, when preparing to enter
227 * userspace.
228 *
229 * Softirqs (and preemption) must be disabled.
230 */
231static void task_fpsimd_load(void)
232{
233 WARN_ON(!in_softirq() && !irqs_disabled());
234
235 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin2cf97d42018-04-12 17:04:39 +0100236 sve_load_state(sve_pffr(&current->thread),
Dave Martin65896542018-03-28 10:50:49 +0100237 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000238 sve_vq_from_vl(current->thread.sve_vl) - 1);
239 else
Dave Martin65896542018-03-28 10:50:49 +0100240 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000241}
242
243/*
Dave Martind1797612018-04-06 14:55:59 +0100244 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
245 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000246 *
247 * Softirqs (and preemption) must be disabled.
248 */
Julien Grall54b8c7c2019-05-21 18:21:38 +0100249static void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000250{
Dave Martin04950672018-09-28 14:39:11 +0100251 struct fpsimd_last_state_struct const *last =
252 this_cpu_ptr(&fpsimd_last_state);
Dave Martine6b673b2018-04-06 14:55:59 +0100253 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100254
Dave Martinbc0ee472017-10-31 15:51:05 +0000255 WARN_ON(!in_softirq() && !irqs_disabled());
256
257 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
258 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
Dave Martin04950672018-09-28 14:39:11 +0100259 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
Dave Martinbc0ee472017-10-31 15:51:05 +0000260 /*
261 * Can't save the user regs, so current would
262 * re-enter user with corrupt state.
263 * There's no way to recover, so kill it:
264 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000265 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000266 return;
267 }
268
Dave Martin04950672018-09-28 14:39:11 +0100269 sve_save_state((char *)last->sve_state +
270 sve_ffr_offset(last->sve_vl),
271 &last->st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000272 } else
Dave Martin04950672018-09-28 14:39:11 +0100273 fpsimd_save_state(last->st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000274 }
275}
276
Dave Martin7582e222017-10-31 15:51:08 +0000277/*
Dave Martin7582e222017-10-31 15:51:08 +0000278 * All vector length selection from userspace comes through here.
279 * We're on a slow path, so some sanity-checks are included.
280 * If things go wrong there's a bug somewhere, but try to fall back to a
281 * safe choice.
282 */
283static unsigned int find_supported_vector_length(unsigned int vl)
284{
285 int bit;
286 int max_vl = sve_max_vl;
287
288 if (WARN_ON(!sve_vl_valid(vl)))
289 vl = SVE_VL_MIN;
290
291 if (WARN_ON(!sve_vl_valid(max_vl)))
292 max_vl = SVE_VL_MIN;
293
294 if (vl > max_vl)
295 vl = max_vl;
296
297 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
Dave Martinead9e432018-09-28 14:39:21 +0100298 __vq_to_bit(sve_vq_from_vl(vl)));
299 return sve_vl_from_vq(__bit_to_vq(bit));
Dave Martin7582e222017-10-31 15:51:08 +0000300}
301
Dave Martin4ffa09a2017-10-31 15:51:15 +0000302#ifdef CONFIG_SYSCTL
303
304static int sve_proc_do_default_vl(struct ctl_table *table, int write,
305 void __user *buffer, size_t *lenp,
306 loff_t *ppos)
307{
308 int ret;
309 int vl = sve_default_vl;
310 struct ctl_table tmp_table = {
311 .data = &vl,
312 .maxlen = sizeof(vl),
313 };
314
315 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
316 if (ret || !write)
317 return ret;
318
319 /* Writing -1 has the special meaning "set to max": */
Dave Martin87c021a2018-06-01 11:10:13 +0100320 if (vl == -1)
321 vl = sve_max_vl;
Dave Martin4ffa09a2017-10-31 15:51:15 +0000322
323 if (!sve_vl_valid(vl))
324 return -EINVAL;
325
Dave Martin87c021a2018-06-01 11:10:13 +0100326 sve_default_vl = find_supported_vector_length(vl);
Dave Martin4ffa09a2017-10-31 15:51:15 +0000327 return 0;
328}
329
330static struct ctl_table sve_default_vl_table[] = {
331 {
332 .procname = "sve_default_vector_length",
333 .mode = 0644,
334 .proc_handler = sve_proc_do_default_vl,
335 },
336 { }
337};
338
339static int __init sve_sysctl_init(void)
340{
341 if (system_supports_sve())
342 if (!register_sysctl("abi", sve_default_vl_table))
343 return -EINVAL;
344
345 return 0;
346}
347
348#else /* ! CONFIG_SYSCTL */
349static int __init sve_sysctl_init(void) { return 0; }
350#endif /* ! CONFIG_SYSCTL */
351
Dave Martinbc0ee472017-10-31 15:51:05 +0000352#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
353 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
354
355/*
Dave Martin65896542018-03-28 10:50:49 +0100356 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000357 * task->thread.sve_state.
358 *
359 * Task can be a non-runnable task, or current. In the latter case,
360 * softirqs (and preemption) must be disabled.
361 * task->thread.sve_state must point to at least sve_state_size(task)
362 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100363 * task->thread.uw.fpsimd_state must be up to date before calling this
364 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000365 */
366static void fpsimd_to_sve(struct task_struct *task)
367{
368 unsigned int vq;
369 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100370 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000371 unsigned int i;
372
373 if (!system_supports_sve())
374 return;
375
376 vq = sve_vq_from_vl(task->thread.sve_vl);
377 for (i = 0; i < 32; ++i)
378 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
379 sizeof(fst->vregs[i]));
380}
381
Dave Martin8cd969d2017-10-31 15:51:07 +0000382/*
383 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100384 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000385 *
386 * Task can be a non-runnable task, or current. In the latter case,
387 * softirqs (and preemption) must be disabled.
388 * task->thread.sve_state must point to at least sve_state_size(task)
389 * bytes of allocated kernel memory.
390 * task->thread.sve_state must be up to date before calling this function.
391 */
392static void sve_to_fpsimd(struct task_struct *task)
393{
394 unsigned int vq;
395 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100396 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000397 unsigned int i;
398
399 if (!system_supports_sve())
400 return;
401
402 vq = sve_vq_from_vl(task->thread.sve_vl);
403 for (i = 0; i < 32; ++i)
404 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
405 sizeof(fst->vregs[i]));
406}
407
Dave Martinbc0ee472017-10-31 15:51:05 +0000408#ifdef CONFIG_ARM64_SVE
409
410/*
411 * Return how many bytes of memory are required to store the full SVE
412 * state for task, given task's currently configured vector length.
413 */
414size_t sve_state_size(struct task_struct const *task)
415{
416 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
417}
418
419/*
420 * Ensure that task->thread.sve_state is allocated and sufficiently large.
421 *
422 * This function should be used only in preparation for replacing
423 * task->thread.sve_state with new data. The memory is always zeroed
424 * here to prevent stale data from showing through: this is done in
425 * the interest of testability and predictability: except in the
426 * do_sve_acc() case, there is no ABI requirement to hide stale data
427 * written previously be task.
428 */
429void sve_alloc(struct task_struct *task)
430{
431 if (task->thread.sve_state) {
432 memset(task->thread.sve_state, 0, sve_state_size(current));
433 return;
434 }
435
436 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
437 task->thread.sve_state =
438 kzalloc(sve_state_size(task), GFP_KERNEL);
439
440 /*
441 * If future SVE revisions can have larger vectors though,
442 * this may cease to be true:
443 */
444 BUG_ON(!task->thread.sve_state);
445}
446
Dave Martin43d4da2c42017-10-31 15:51:13 +0000447
448/*
449 * Ensure that task->thread.sve_state is up to date with respect to
450 * the user task, irrespective of when SVE is in use or not.
451 *
452 * This should only be called by ptrace. task must be non-runnable.
453 * task->thread.sve_state must point to at least sve_state_size(task)
454 * bytes of allocated kernel memory.
455 */
456void fpsimd_sync_to_sve(struct task_struct *task)
457{
458 if (!test_tsk_thread_flag(task, TIF_SVE))
459 fpsimd_to_sve(task);
460}
461
462/*
Dave Martin65896542018-03-28 10:50:49 +0100463 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000464 * the user task, irrespective of whether 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 sve_sync_to_fpsimd(struct task_struct *task)
471{
472 if (test_tsk_thread_flag(task, TIF_SVE))
473 sve_to_fpsimd(task);
474}
475
476/*
477 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100478 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000479 *
480 * This should only be called by ptrace to merge new FPSIMD register
481 * values into a task for which SVE is currently active.
482 * task must be non-runnable.
483 * task->thread.sve_state must point to at least sve_state_size(task)
484 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100485 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000486 * the new FPSIMD register values to be merged in.
487 */
488void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
489{
490 unsigned int vq;
491 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100492 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000493 unsigned int i;
494
495 if (!test_tsk_thread_flag(task, TIF_SVE))
496 return;
497
498 vq = sve_vq_from_vl(task->thread.sve_vl);
499
500 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
501
502 for (i = 0; i < 32; ++i)
503 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
504 sizeof(fst->vregs[i]));
505}
506
Dave Martin7582e222017-10-31 15:51:08 +0000507int sve_set_vector_length(struct task_struct *task,
508 unsigned long vl, unsigned long flags)
509{
510 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
511 PR_SVE_SET_VL_ONEXEC))
512 return -EINVAL;
513
514 if (!sve_vl_valid(vl))
515 return -EINVAL;
516
517 /*
518 * Clamp to the maximum vector length that VL-agnostic SVE code can
519 * work with. A flag may be assigned in the future to allow setting
520 * of larger vector lengths without confusing older software.
521 */
522 if (vl > SVE_VL_ARCH_MAX)
523 vl = SVE_VL_ARCH_MAX;
524
525 vl = find_supported_vector_length(vl);
526
527 if (flags & (PR_SVE_VL_INHERIT |
528 PR_SVE_SET_VL_ONEXEC))
529 task->thread.sve_vl_onexec = vl;
530 else
531 /* Reset VL to system default on next exec: */
532 task->thread.sve_vl_onexec = 0;
533
534 /* Only actually set the VL if not deferred: */
535 if (flags & PR_SVE_SET_VL_ONEXEC)
536 goto out;
537
538 if (vl == task->thread.sve_vl)
539 goto out;
540
541 /*
542 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
543 * write any live register state back to task_struct, and convert to a
544 * non-SVE thread.
545 */
546 if (task == current) {
547 local_bh_disable();
548
Dave Martind1797612018-04-06 14:55:59 +0100549 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000550 }
551
552 fpsimd_flush_task_state(task);
553 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
554 sve_to_fpsimd(task);
555
556 if (task == current)
557 local_bh_enable();
558
559 /*
560 * Force reallocation of task SVE state to the correct size
561 * on next use:
562 */
563 sve_free(task);
564
565 task->thread.sve_vl = vl;
566
567out:
Dave Martin09d12232018-04-11 17:59:06 +0100568 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
569 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000570
571 return 0;
572}
573
Dave Martinbc0ee472017-10-31 15:51:05 +0000574/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000575 * Encode the current vector length and flags for return.
576 * This is only required for prctl(): ptrace has separate fields
577 *
578 * flags are as for sve_set_vector_length().
579 */
580static int sve_prctl_status(unsigned long flags)
581{
582 int ret;
583
584 if (flags & PR_SVE_SET_VL_ONEXEC)
585 ret = current->thread.sve_vl_onexec;
586 else
587 ret = current->thread.sve_vl;
588
589 if (test_thread_flag(TIF_SVE_VL_INHERIT))
590 ret |= PR_SVE_VL_INHERIT;
591
592 return ret;
593}
594
595/* PR_SVE_SET_VL */
596int sve_set_current_vl(unsigned long arg)
597{
598 unsigned long vl, flags;
599 int ret;
600
601 vl = arg & PR_SVE_VL_LEN_MASK;
602 flags = arg & ~vl;
603
604 if (!system_supports_sve())
605 return -EINVAL;
606
607 ret = sve_set_vector_length(current, vl, flags);
608 if (ret)
609 return ret;
610
611 return sve_prctl_status(flags);
612}
613
614/* PR_SVE_GET_VL */
615int sve_get_current_vl(void)
616{
617 if (!system_supports_sve())
618 return -EINVAL;
619
620 return sve_prctl_status(0);
621}
622
Dave Martin2e0f2472017-10-31 15:51:10 +0000623static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
624{
625 unsigned int vq, vl;
626 unsigned long zcr;
627
628 bitmap_zero(map, SVE_VQ_MAX);
629
630 zcr = ZCR_ELx_LEN_MASK;
631 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
632
633 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
634 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
635 vl = sve_get_vl();
636 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
Dave Martinead9e432018-09-28 14:39:21 +0100637 set_bit(__vq_to_bit(vq), map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000638 }
639}
640
Dave Martin8b08e842018-12-06 16:32:35 +0000641/*
642 * Initialise the set of known supported VQs for the boot CPU.
643 * This is called during kernel boot, before secondary CPUs are brought up.
644 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000645void __init sve_init_vq_map(void)
646{
647 sve_probe_vqs(sve_vq_map);
Dave Martind06b76b2018-09-28 14:39:10 +0100648 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000649}
650
651/*
652 * If we haven't committed to the set of supported VQs yet, filter out
653 * those not supported by the current CPU.
Dave Martin8b08e842018-12-06 16:32:35 +0000654 * This function is called during the bring-up of early secondary CPUs only.
Dave Martin2e0f2472017-10-31 15:51:10 +0000655 */
656void sve_update_vq_map(void)
657{
Dave Martind06b76b2018-09-28 14:39:10 +0100658 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
659
660 sve_probe_vqs(tmp_map);
661 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
662 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000663}
664
Dave Martin8b08e842018-12-06 16:32:35 +0000665/*
666 * Check whether the current CPU supports all VQs in the committed set.
667 * This function is called during the bring-up of late secondary CPUs only.
668 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000669int sve_verify_vq_map(void)
670{
Dave Martind06b76b2018-09-28 14:39:10 +0100671 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
672 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000673
Dave Martind06b76b2018-09-28 14:39:10 +0100674 sve_probe_vqs(tmp_map);
675
676 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
677 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
Dave Martin2e0f2472017-10-31 15:51:10 +0000678 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
679 smp_processor_id());
Dave Martind06b76b2018-09-28 14:39:10 +0100680 return -EINVAL;
Dave Martin2e0f2472017-10-31 15:51:10 +0000681 }
682
Dave Martind06b76b2018-09-28 14:39:10 +0100683 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
684 return 0;
685
686 /*
687 * For KVM, it is necessary to ensure that this CPU doesn't
688 * support any vector length that guests may have probed as
689 * unsupported.
690 */
691
692 /* Recover the set of supported VQs: */
693 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
694 /* Find VQs supported that are not globally supported: */
695 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
696
697 /* Find the lowest such VQ, if any: */
698 b = find_last_bit(tmp_map, SVE_VQ_MAX);
699 if (b >= SVE_VQ_MAX)
700 return 0; /* no mismatches */
701
702 /*
703 * Mismatches above sve_max_virtualisable_vl are fine, since
704 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
705 */
Dave Martinead9e432018-09-28 14:39:21 +0100706 if (sve_vl_from_vq(__bit_to_vq(b)) <= sve_max_virtualisable_vl) {
Dave Martind06b76b2018-09-28 14:39:10 +0100707 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
708 smp_processor_id());
709 return -EINVAL;
710 }
711
712 return 0;
Dave Martin2e0f2472017-10-31 15:51:10 +0000713}
714
Dave Martinfdfa9762017-10-31 15:51:12 +0000715static void __init sve_efi_setup(void)
716{
717 if (!IS_ENABLED(CONFIG_EFI))
718 return;
719
720 /*
721 * alloc_percpu() warns and prints a backtrace if this goes wrong.
722 * This is evidence of a crippled system and we are returning void,
723 * so no attempt is made to handle this situation here.
724 */
725 if (!sve_vl_valid(sve_max_vl))
726 goto fail;
727
728 efi_sve_state = __alloc_percpu(
729 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
730 if (!efi_sve_state)
731 goto fail;
732
733 return;
734
735fail:
736 panic("Cannot allocate percpu memory for EFI SVE save/restore");
737}
738
Dave Martin2e0f2472017-10-31 15:51:10 +0000739/*
740 * Enable SVE for EL1.
741 * Intended for use by the cpufeatures code during CPU boot.
742 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100743void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000744{
745 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
746 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000747}
748
Dave Martin31dc52b2018-04-12 16:47:20 +0100749/*
750 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
751 * vector length.
752 *
753 * Use only if SVE is present.
754 * This function clobbers the SVE vector length.
755 */
756u64 read_zcr_features(void)
757{
758 u64 zcr;
759 unsigned int vq_max;
760
761 /*
762 * Set the maximum possible VL, and write zeroes to all other
763 * bits to see if they stick.
764 */
765 sve_kernel_enable(NULL);
766 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
767
768 zcr = read_sysreg_s(SYS_ZCR_EL1);
769 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
770 vq_max = sve_vq_from_vl(sve_get_vl());
771 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
772
773 return zcr;
774}
775
Dave Martin2e0f2472017-10-31 15:51:10 +0000776void __init sve_setup(void)
777{
778 u64 zcr;
Dave Martind06b76b2018-09-28 14:39:10 +0100779 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
780 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000781
782 if (!system_supports_sve())
783 return;
784
785 /*
786 * The SVE architecture mandates support for 128-bit vectors,
787 * so sve_vq_map must have at least SVE_VQ_MIN set.
788 * If something went wrong, at least try to patch it up:
789 */
Dave Martinead9e432018-09-28 14:39:21 +0100790 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
791 set_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000792
793 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
794 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
795
796 /*
797 * Sanity-check that the max VL we determined through CPU features
798 * corresponds properly to sve_vq_map. If not, do our best:
799 */
800 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
801 sve_max_vl = find_supported_vector_length(sve_max_vl);
802
803 /*
804 * For the default VL, pick the maximum supported value <= 64.
805 * VL == 64 is guaranteed not to grow the signal frame.
806 */
807 sve_default_vl = find_supported_vector_length(64);
808
Dave Martind06b76b2018-09-28 14:39:10 +0100809 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
810 SVE_VQ_MAX);
811
812 b = find_last_bit(tmp_map, SVE_VQ_MAX);
813 if (b >= SVE_VQ_MAX)
814 /* No non-virtualisable VLs found */
815 sve_max_virtualisable_vl = SVE_VQ_MAX;
816 else if (WARN_ON(b == SVE_VQ_MAX - 1))
817 /* No virtualisable VLs? This is architecturally forbidden. */
818 sve_max_virtualisable_vl = SVE_VQ_MIN;
819 else /* b + 1 < SVE_VQ_MAX */
Dave Martinead9e432018-09-28 14:39:21 +0100820 sve_max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
Dave Martind06b76b2018-09-28 14:39:10 +0100821
822 if (sve_max_virtualisable_vl > sve_max_vl)
823 sve_max_virtualisable_vl = sve_max_vl;
824
Dave Martin2e0f2472017-10-31 15:51:10 +0000825 pr_info("SVE: maximum available vector length %u bytes per vector\n",
826 sve_max_vl);
827 pr_info("SVE: default vector length %u bytes per vector\n",
828 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000829
Dave Martind06b76b2018-09-28 14:39:10 +0100830 /* KVM decides whether to support mismatched systems. Just warn here: */
831 if (sve_max_virtualisable_vl < sve_max_vl)
832 pr_warn("SVE: unvirtualisable vector lengths present\n");
833
Dave Martinfdfa9762017-10-31 15:51:12 +0000834 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000835}
836
837/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000838 * Called from the put_task_struct() path, which cannot get here
839 * unless dead_task is really dead and not schedulable.
840 */
841void fpsimd_release_task(struct task_struct *dead_task)
842{
843 __sve_free(dead_task);
844}
845
846#endif /* CONFIG_ARM64_SVE */
847
848/*
849 * Trapped SVE access
850 *
851 * Storage is allocated for the full SVE state, the current FPSIMD
852 * register contents are migrated across, and TIF_SVE is set so that
853 * the SVE access trap will be disabled the next time this task
854 * reaches ret_to_user.
855 *
856 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
857 * would have disabled the SVE access trap for userspace during
858 * ret_to_user, making an SVE access trap impossible in that case.
859 */
860asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
861{
862 /* Even if we chose not to use SVE, the hardware could still trap: */
863 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000864 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000865 return;
866 }
867
868 sve_alloc(current);
869
870 local_bh_disable();
871
Dave Martind1797612018-04-06 14:55:59 +0100872 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000873
874 /* Force ret_to_user to reload the registers: */
875 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000876
Dave Martinefbc2022018-09-28 14:39:05 +0100877 fpsimd_to_sve(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000878 if (test_and_set_thread_flag(TIF_SVE))
879 WARN_ON(1); /* SVE access shouldn't have trapped */
880
881 local_bh_enable();
882}
883
Catalin Marinas53631b52012-03-05 11:49:32 +0000884/*
885 * Trapped FP/ASIMD access.
886 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000887asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000888{
889 /* TODO: implement lazy context saving/restoring */
890 WARN_ON(1);
891}
892
893/*
894 * Raise a SIGFPE for the current process.
895 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000896asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000897{
Dave Martinaf4a81b2018-03-01 17:44:07 +0000898 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000899
Dave Martinaf4a81b2018-03-01 17:44:07 +0000900 if (esr & ESR_ELx_FP_EXC_TFV) {
901 if (esr & FPEXC_IOF)
902 si_code = FPE_FLTINV;
903 else if (esr & FPEXC_DZF)
904 si_code = FPE_FLTDIV;
905 else if (esr & FPEXC_OFF)
906 si_code = FPE_FLTOVF;
907 else if (esr & FPEXC_UFF)
908 si_code = FPE_FLTUND;
909 else if (esr & FPEXC_IXF)
910 si_code = FPE_FLTRES;
911 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000912
Eric W. Biedermanc8526802018-04-16 13:47:06 -0500913 send_sig_fault(SIGFPE, si_code,
914 (void __user *)instruction_pointer(regs),
915 current);
Catalin Marinas53631b52012-03-05 11:49:32 +0000916}
917
918void fpsimd_thread_switch(struct task_struct *next)
919{
Dave Martindf3fb962018-05-21 19:08:15 +0100920 bool wrong_task, wrong_cpu;
921
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000922 if (!system_supports_fpsimd())
923 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200924
Dave Martindf3fb962018-05-21 19:08:15 +0100925 /* Save unsaved fpsimd state, if any: */
926 fpsimd_save();
927
Catalin Marinas53631b52012-03-05 11:49:32 +0000928 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100929 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
930 * state. For kernel threads, FPSIMD registers are never loaded
931 * and wrong_task and wrong_cpu will always be true.
Catalin Marinas53631b52012-03-05 11:49:32 +0000932 */
Dave Martindf3fb962018-05-21 19:08:15 +0100933 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100934 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100935 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100936
Dave Martindf3fb962018-05-21 19:08:15 +0100937 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
938 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000939}
940
941void fpsimd_flush_thread(void)
942{
Dave Martin7582e222017-10-31 15:51:08 +0000943 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000944
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000945 if (!system_supports_fpsimd())
946 return;
Dave Martincb84d112017-08-03 17:23:23 +0100947
948 local_bh_disable();
949
Dave Martinefbc2022018-09-28 14:39:05 +0100950 fpsimd_flush_task_state(current);
Dave Martin65896542018-03-28 10:50:49 +0100951 memset(&current->thread.uw.fpsimd_state, 0,
952 sizeof(current->thread.uw.fpsimd_state));
Dave Martinbc0ee472017-10-31 15:51:05 +0000953
954 if (system_supports_sve()) {
955 clear_thread_flag(TIF_SVE);
956 sve_free(current);
957
958 /*
959 * Reset the task vector length as required.
960 * This is where we ensure that all user tasks have a valid
961 * vector length configured: no kernel task can become a user
962 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000963 * By the time the first call to this function is made, all
964 * early hardware probing is complete, so sve_default_vl
965 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000966 * If a bug causes this to go wrong, we make some noise and
967 * try to fudge thread.sve_vl to a safe value here.
968 */
Dave Martin79ab0472017-10-31 15:51:06 +0000969 vl = current->thread.sve_vl_onexec ?
970 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000971
972 if (WARN_ON(!sve_vl_valid(vl)))
973 vl = SVE_VL_MIN;
974
Dave Martin7582e222017-10-31 15:51:08 +0000975 supported_vl = find_supported_vector_length(vl);
976 if (WARN_ON(supported_vl != vl))
977 vl = supported_vl;
978
Dave Martinbc0ee472017-10-31 15:51:05 +0000979 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000980
981 /*
982 * If the task is not set to inherit, ensure that the vector
983 * length will be reset by a subsequent exec:
984 */
985 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
986 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000987 }
988
Dave Martincb84d112017-08-03 17:23:23 +0100989 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000990}
991
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100992/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200993 * Save the userland FPSIMD state of 'current' to memory, but only if the state
994 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100995 */
996void fpsimd_preserve_current_state(void)
997{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000998 if (!system_supports_fpsimd())
999 return;
Dave Martincb84d112017-08-03 17:23:23 +01001000
1001 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +01001002 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001003 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001004}
1005
1006/*
Dave Martin8cd969d2017-10-31 15:51:07 +00001007 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +01001008 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +00001009 * the signal frame.
1010 */
1011void fpsimd_signal_preserve_current_state(void)
1012{
1013 fpsimd_preserve_current_state();
1014 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1015 sve_to_fpsimd(current);
1016}
1017
1018/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001019 * Associate current's FPSIMD context with this cpu
1020 * Preemption must be disabled when calling this function.
1021 */
Dave Martine6b673b2018-04-06 14:55:59 +01001022void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +00001023{
Dave Martincb968af2017-12-06 16:45:47 +00001024 struct fpsimd_last_state_struct *last =
1025 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001026
Dave Martin65896542018-03-28 10:50:49 +01001027 last->st = &current->thread.uw.fpsimd_state;
Dave Martin04950672018-09-28 14:39:11 +01001028 last->sve_state = current->thread.sve_state;
1029 last->sve_vl = current->thread.sve_vl;
Dave Martin20b85472018-03-28 10:50:48 +01001030 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +01001031
1032 if (system_supports_sve()) {
1033 /* Toggle SVE trapping for userspace if needed */
1034 if (test_thread_flag(TIF_SVE))
1035 sve_user_enable();
1036 else
1037 sve_user_disable();
1038
1039 /* Serialised by exception return to user */
1040 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001041}
1042
Dave Martin04950672018-09-28 14:39:11 +01001043void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1044 unsigned int sve_vl)
Dave Martine6b673b2018-04-06 14:55:59 +01001045{
1046 struct fpsimd_last_state_struct *last =
1047 this_cpu_ptr(&fpsimd_last_state);
1048
1049 WARN_ON(!in_softirq() && !irqs_disabled());
1050
1051 last->st = st;
Dave Martin04950672018-09-28 14:39:11 +01001052 last->sve_state = sve_state;
1053 last->sve_vl = sve_vl;
Dave Martin8884b7b2017-12-06 16:45:46 +00001054}
1055
1056/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001057 * Load the userland FPSIMD state of 'current' from memory, but only if the
1058 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1059 * state of 'current'
1060 */
1061void fpsimd_restore_current_state(void)
1062{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001063 if (!system_supports_fpsimd())
1064 return;
Dave Martincb84d112017-08-03 17:23:23 +01001065
1066 local_bh_disable();
1067
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001068 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001069 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001070 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001071 }
Dave Martincb84d112017-08-03 17:23:23 +01001072
1073 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001074}
1075
1076/*
1077 * Load an updated userland FPSIMD state for 'current' from memory and set the
1078 * flag that indicates that the FPSIMD register contents are the most recent
1079 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001080 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001081void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001082{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001083 if (!system_supports_fpsimd())
1084 return;
Dave Martincb84d112017-08-03 17:23:23 +01001085
1086 local_bh_disable();
1087
Dave Martin65896542018-03-28 10:50:49 +01001088 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001089 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001090 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001091
Dave Martin8cd969d2017-10-31 15:51:07 +00001092 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001093 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001094
Dave Martin0cff8e72018-05-09 14:27:41 +01001095 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001096
1097 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001098}
1099
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001100/*
1101 * Invalidate live CPU copies of task t's FPSIMD state
Dave Martinefbc2022018-09-28 14:39:05 +01001102 *
1103 * This function may be called with preemption enabled. The barrier()
1104 * ensures that the assignment to fpsimd_cpu is visible to any
1105 * preemption/softirq that could race with set_tsk_thread_flag(), so
1106 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1107 *
1108 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1109 * subsequent code.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001110 */
1111void fpsimd_flush_task_state(struct task_struct *t)
1112{
Dave Martin20b85472018-03-28 10:50:48 +01001113 t->thread.fpsimd_cpu = NR_CPUS;
Dave Martinefbc2022018-09-28 14:39:05 +01001114
1115 barrier();
1116 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1117
1118 barrier();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001119}
1120
Dave Martinefbc2022018-09-28 14:39:05 +01001121/*
1122 * Invalidate any task's FPSIMD state that is present on this cpu.
1123 * This function must be called with softirqs disabled.
1124 */
Julien Grall54b8c7c2019-05-21 18:21:38 +01001125static void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001126{
Dave Martincb968af2017-12-06 16:45:47 +00001127 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001128 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001129}
1130
Julien Grall54b8c7c2019-05-21 18:21:38 +01001131/*
1132 * Save the FPSIMD state to memory and invalidate cpu view.
1133 * This function must be called with softirqs (and preemption) disabled.
1134 */
1135void fpsimd_save_and_flush_cpu_state(void)
1136{
1137 fpsimd_save();
1138 fpsimd_flush_cpu_state();
1139}
1140
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001141#ifdef CONFIG_KERNEL_MODE_NEON
1142
Dave Martincb84d112017-08-03 17:23:23 +01001143DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001144EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001145
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001146/*
1147 * Kernel-side NEON support functions
1148 */
Dave Martincb84d112017-08-03 17:23:23 +01001149
1150/*
1151 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1152 * context
1153 *
1154 * Must not be called unless may_use_simd() returns true.
1155 * Task context in the FPSIMD registers is saved back to memory as necessary.
1156 *
1157 * A matching call to kernel_neon_end() must be made before returning from the
1158 * calling context.
1159 *
1160 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1161 * called.
1162 */
1163void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001164{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001165 if (WARN_ON(!system_supports_fpsimd()))
1166 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001167
Dave Martincb84d112017-08-03 17:23:23 +01001168 BUG_ON(!may_use_simd());
1169
1170 local_bh_disable();
1171
1172 __this_cpu_write(kernel_neon_busy, true);
1173
Dave Martindf3fb962018-05-21 19:08:15 +01001174 /* Save unsaved fpsimd state, if any: */
1175 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001176
1177 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001178 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001179
1180 preempt_disable();
1181
1182 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001183}
Dave Martincb84d112017-08-03 17:23:23 +01001184EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001185
Dave Martincb84d112017-08-03 17:23:23 +01001186/*
1187 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1188 *
1189 * Must be called from a context in which kernel_neon_begin() was previously
1190 * called, with no call to kernel_neon_end() in the meantime.
1191 *
1192 * The caller must not use the FPSIMD registers after this function is called,
1193 * unless kernel_neon_begin() is called again in the meantime.
1194 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001195void kernel_neon_end(void)
1196{
Dave Martincb84d112017-08-03 17:23:23 +01001197 bool busy;
1198
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001199 if (!system_supports_fpsimd())
1200 return;
Dave Martincb84d112017-08-03 17:23:23 +01001201
1202 busy = __this_cpu_xchg(kernel_neon_busy, false);
1203 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1204
1205 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001206}
1207EXPORT_SYMBOL(kernel_neon_end);
1208
Dave Martine580b8b2017-09-18 09:40:12 +01001209#ifdef CONFIG_EFI
1210
Dave Martin20b85472018-03-28 10:50:48 +01001211static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001212static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001213static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001214
1215/*
1216 * EFI runtime services support functions
1217 *
1218 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1219 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1220 * is always used rather than being an optional accelerator.
1221 *
1222 * These functions provide the necessary support for ensuring FPSIMD
1223 * save/restore in the contexts from which EFI is used.
1224 *
1225 * Do not use them for any other purpose -- if tempted to do so, you are
1226 * either doing something wrong or you need to propose some refactoring.
1227 */
1228
1229/*
1230 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1231 */
1232void __efi_fpsimd_begin(void)
1233{
1234 if (!system_supports_fpsimd())
1235 return;
1236
1237 WARN_ON(preemptible());
1238
Dave Martinfdfa9762017-10-31 15:51:12 +00001239 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001240 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001241 } else {
1242 /*
1243 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1244 * preserving:
1245 */
1246 if (system_supports_sve() && likely(efi_sve_state)) {
1247 char *sve_state = this_cpu_ptr(efi_sve_state);
1248
1249 __this_cpu_write(efi_sve_state_used, true);
1250
1251 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1252 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1253 } else {
1254 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1255 }
1256
Dave Martin4328825d2017-08-03 17:23:22 +01001257 __this_cpu_write(efi_fpsimd_state_used, true);
1258 }
1259}
1260
1261/*
1262 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1263 */
1264void __efi_fpsimd_end(void)
1265{
1266 if (!system_supports_fpsimd())
1267 return;
1268
Dave Martinfdfa9762017-10-31 15:51:12 +00001269 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001270 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001271 } else {
1272 if (system_supports_sve() &&
1273 likely(__this_cpu_read(efi_sve_state_used))) {
1274 char const *sve_state = this_cpu_ptr(efi_sve_state);
1275
1276 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1277 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1278 sve_vq_from_vl(sve_get_vl()) - 1);
1279
1280 __this_cpu_write(efi_sve_state_used, false);
1281 } else {
1282 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1283 }
1284 }
Dave Martin4328825d2017-08-03 17:23:22 +01001285}
1286
Dave Martine580b8b2017-09-18 09:40:12 +01001287#endif /* CONFIG_EFI */
1288
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001289#endif /* CONFIG_KERNEL_MODE_NEON */
1290
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001291#ifdef CONFIG_CPU_PM
1292static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1293 unsigned long cmd, void *v)
1294{
1295 switch (cmd) {
1296 case CPU_PM_ENTER:
Julien Grall54b8c7c2019-05-21 18:21:38 +01001297 fpsimd_save_and_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001298 break;
1299 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001300 break;
1301 case CPU_PM_ENTER_FAILED:
1302 default:
1303 return NOTIFY_DONE;
1304 }
1305 return NOTIFY_OK;
1306}
1307
1308static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1309 .notifier_call = fpsimd_cpu_pm_notifier,
1310};
1311
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001312static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001313{
1314 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1315}
1316
1317#else
1318static inline void fpsimd_pm_init(void) { }
1319#endif /* CONFIG_CPU_PM */
1320
Janet Liu32365e62015-06-11 12:02:45 +08001321#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001322static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001323{
Dave Martincb968af2017-12-06 16:45:47 +00001324 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001325 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001326}
1327
Janet Liu32365e62015-06-11 12:02:45 +08001328static inline void fpsimd_hotplug_init(void)
1329{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001330 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1331 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001332}
1333
1334#else
1335static inline void fpsimd_hotplug_init(void) { }
1336#endif
1337
Catalin Marinas53631b52012-03-05 11:49:32 +00001338/*
1339 * FP/SIMD support code initialisation.
1340 */
1341static int __init fpsimd_init(void)
1342{
Andrew Murrayaaba0982019-04-09 10:52:40 +01001343 if (cpu_have_named_feature(FP)) {
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001344 fpsimd_pm_init();
1345 fpsimd_hotplug_init();
1346 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001347 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001348 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001349
Andrew Murrayaaba0982019-04-09 10:52:40 +01001350 if (!cpu_have_named_feature(ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001351 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001352
Dave Martin4ffa09a2017-10-31 15:51:15 +00001353 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001354}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001355core_initcall(fpsimd_init);