blob: 0cfcf5c237c58b02af94abcc5a8b515850124846 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas53631b52012-03-05 11:49:32 +00002/*
3 * FP/SIMD context switching and fault handling
4 *
5 * Copyright (C) 2012 ARM Ltd.
6 * Author: Catalin Marinas <catalin.marinas@arm.com>
Catalin Marinas53631b52012-03-05 11:49:32 +00007 */
8
Dave Martin7582e222017-10-31 15:51:08 +00009#include <linux/bitmap.h>
Dave Martind06b76b2018-09-28 14:39:10 +010010#include <linux/bitops.h>
Dave Martincb84d112017-08-03 17:23:23 +010011#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000012#include <linux/bug.h>
Dave Martin7582e222017-10-31 15:51:08 +000013#include <linux/cache.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000014#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080015#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010016#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000017#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000018#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000019#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000020#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010021#include <linux/percpu.h>
Dave Martin2d2123b2017-10-31 15:51:14 +000022#include <linux/prctl.h>
Dave Martin4328825d2017-08-03 17:23:22 +010023#include <linux/preempt.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000024#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010025#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000026#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000027#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000028#include <linux/slab.h>
Dave Martin31dc52b2018-04-12 16:47:20 +010029#include <linux/stddef.h>
Dave Martin4ffa09a2017-10-31 15:51:15 +000030#include <linux/sysctl.h>
Dave Martin41040cf2019-06-12 17:00:32 +010031#include <linux/swab.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000032
Dave Martinaf4a81b2018-03-01 17:44:07 +000033#include <asm/esr.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000034#include <asm/fpsimd.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010035#include <asm/cpufeature.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000036#include <asm/cputype.h>
Dave Martin2cf97d42018-04-12 17:04:39 +010037#include <asm/processor.h>
Dave Martin4328825d2017-08-03 17:23:22 +010038#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000039#include <asm/sigcontext.h>
40#include <asm/sysreg.h>
41#include <asm/traps.h>
Dave Martind06b76b2018-09-28 14:39:10 +010042#include <asm/virt.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000043
44#define FPEXC_IOF (1 << 0)
45#define FPEXC_DZF (1 << 1)
46#define FPEXC_OFF (1 << 2)
47#define FPEXC_UFF (1 << 3)
48#define FPEXC_IXF (1 << 4)
49#define FPEXC_IDF (1 << 7)
50
51/*
Dave Martinbc0ee472017-10-31 15:51:05 +000052 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
53 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020054 * In order to reduce the number of times the FPSIMD state is needlessly saved
55 * and restored, we need to keep track of two things:
56 * (a) for each task, we need to remember which CPU was the last one to have
57 * the task's FPSIMD state loaded into its FPSIMD registers;
58 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
59 * been loaded into its FPSIMD registers most recently, or whether it has
60 * been used to perform kernel mode NEON in the meantime.
61 *
Dave Martin20b85472018-03-28 10:50:48 +010062 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080063 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020064 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
65 * address of the userland FPSIMD state of the task that was loaded onto the CPU
66 * the most recently, or NULL if kernel mode NEON has been performed after that.
67 *
68 * With this in place, we no longer have to restore the next FPSIMD state right
69 * when switching between tasks. Instead, we can defer this check to userland
70 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010071 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020072 * can omit the FPSIMD restore.
73 *
74 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
75 * indicate whether or not the userland FPSIMD state of the current task is
76 * present in the registers. The flag is set unless the FPSIMD registers of this
77 * CPU currently contain the most recent userland FPSIMD state of the current
78 * task.
79 *
Dave Martincb84d112017-08-03 17:23:23 +010080 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
81 * save the task's FPSIMD context back to task_struct from softirq context.
82 * To prevent this from racing with the manipulation of the task's FPSIMD state
83 * from task context and thereby corrupting the state, it is necessary to
84 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
85 * flag with local_bh_disable() unless softirqs are already masked.
86 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020087 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010088 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020089 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
90 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
91 * cleared, otherwise it is set;
92 *
93 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
94 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +010095 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020096 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
97 * TIF_FOREIGN_FPSTATE flag is cleared;
98 *
99 * - the task executes an ordinary syscall; upon return to userland, the
100 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
101 * restored;
102 *
103 * - the task executes a syscall which executes some NEON instructions; this is
104 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
105 * register contents to memory, clears the fpsimd_last_state per-cpu variable
106 * and sets the TIF_FOREIGN_FPSTATE flag;
107 *
108 * - the task gets preempted after kernel_neon_end() is called; as we have not
109 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
110 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
111 */
Dave Martincb968af2017-12-06 16:45:47 +0000112struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100113 struct user_fpsimd_state *st;
Dave Martin04950672018-09-28 14:39:11 +0100114 void *sve_state;
115 unsigned int sve_vl;
Dave Martincb968af2017-12-06 16:45:47 +0000116};
117
118static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200119
Dave Martin79ab0472017-10-31 15:51:06 +0000120/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000121static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000122
Dave Martin7582e222017-10-31 15:51:08 +0000123#ifdef CONFIG_ARM64_SVE
124
125/* Maximum supported vector length across all CPUs (initially poisoned) */
Dave Martin87c021a2018-06-01 11:10:13 +0100126int __ro_after_init sve_max_vl = SVE_VL_MIN;
Dave Martind06b76b2018-09-28 14:39:10 +0100127int __ro_after_init sve_max_virtualisable_vl = SVE_VL_MIN;
Dave Martin624835a2019-04-11 16:53:18 +0100128
129/*
130 * Set of available vector lengths,
131 * where length vq encoded as bit __vq_to_bit(vq):
132 */
Dave Martinead9e432018-09-28 14:39:21 +0100133__ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100134/* Set of vector lengths present on at least one cpu: */
135static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martin624835a2019-04-11 16:53:18 +0100136
Dave Martinfdfa9762017-10-31 15:51:12 +0000137static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000138
139#else /* ! CONFIG_ARM64_SVE */
140
141/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000142extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100143extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000144extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000145
146#endif /* ! CONFIG_ARM64_SVE */
147
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200148/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000149 * Call __sve_free() directly only if you know task can't be scheduled
150 * or preempted.
151 */
152static void __sve_free(struct task_struct *task)
153{
154 kfree(task->thread.sve_state);
155 task->thread.sve_state = NULL;
156}
157
158static void sve_free(struct task_struct *task)
159{
160 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
161
162 __sve_free(task);
163}
164
Dave Martinbc0ee472017-10-31 15:51:05 +0000165/*
166 * TIF_SVE controls whether a task can use SVE without trapping while
167 * in userspace, and also the way a task's FPSIMD/SVE state is stored
168 * in thread_struct.
169 *
170 * The kernel uses this flag to track whether a user task is actively
171 * using SVE, and therefore whether full SVE register state needs to
172 * be tracked. If not, the cheaper FPSIMD context handling code can
173 * be used instead of the more costly SVE equivalents.
174 *
175 * * TIF_SVE set:
176 *
177 * The task can execute SVE instructions while in userspace without
178 * trapping to the kernel.
179 *
180 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
181 * corresponding Zn), P0-P15 and FFR are encoded in in
182 * task->thread.sve_state, formatted appropriately for vector
183 * length task->thread.sve_vl.
184 *
185 * task->thread.sve_state must point to a valid buffer at least
186 * sve_state_size(task) bytes in size.
187 *
188 * During any syscall, the kernel may optionally clear TIF_SVE and
189 * discard the vector state except for the FPSIMD subset.
190 *
191 * * TIF_SVE clear:
192 *
193 * An attempt by the user task to execute an SVE instruction causes
194 * do_sve_acc() to be called, which does some preparation and then
195 * sets TIF_SVE.
196 *
197 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100198 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000199 * logically zero but not stored anywhere; P0-P15 and FFR are not
200 * stored and have unspecified values from userspace's point of
201 * view. For hygiene purposes, the kernel zeroes them on next use,
202 * but userspace is discouraged from relying on this.
203 *
204 * task->thread.sve_state does not need to be non-NULL, valid or any
205 * particular size: it must not be dereferenced.
206 *
Dave Martin65896542018-03-28 10:50:49 +0100207 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
208 * irrespective of whether TIF_SVE is clear or set, since these are
209 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000210 */
211
212/*
213 * Update current's FPSIMD/SVE registers from thread_struct.
214 *
215 * This function should be called only when the FPSIMD/SVE state in
216 * thread_struct is known to be up to date, when preparing to enter
217 * userspace.
218 *
219 * Softirqs (and preemption) must be disabled.
220 */
221static void task_fpsimd_load(void)
222{
223 WARN_ON(!in_softirq() && !irqs_disabled());
224
225 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin2cf97d42018-04-12 17:04:39 +0100226 sve_load_state(sve_pffr(&current->thread),
Dave Martin65896542018-03-28 10:50:49 +0100227 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000228 sve_vq_from_vl(current->thread.sve_vl) - 1);
229 else
Dave Martin65896542018-03-28 10:50:49 +0100230 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000231}
232
233/*
Dave Martind1797612018-04-06 14:55:59 +0100234 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
235 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000236 *
237 * Softirqs (and preemption) must be disabled.
238 */
Dave Martine6b673b2018-04-06 14:55:59 +0100239void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000240{
Dave Martin04950672018-09-28 14:39:11 +0100241 struct fpsimd_last_state_struct const *last =
242 this_cpu_ptr(&fpsimd_last_state);
Dave Martine6b673b2018-04-06 14:55:59 +0100243 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100244
Dave Martinbc0ee472017-10-31 15:51:05 +0000245 WARN_ON(!in_softirq() && !irqs_disabled());
246
247 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
248 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
Dave Martin04950672018-09-28 14:39:11 +0100249 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
Dave Martinbc0ee472017-10-31 15:51:05 +0000250 /*
251 * Can't save the user regs, so current would
252 * re-enter user with corrupt state.
253 * There's no way to recover, so kill it:
254 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000255 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000256 return;
257 }
258
Dave Martin04950672018-09-28 14:39:11 +0100259 sve_save_state((char *)last->sve_state +
260 sve_ffr_offset(last->sve_vl),
261 &last->st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000262 } else
Dave Martin04950672018-09-28 14:39:11 +0100263 fpsimd_save_state(last->st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000264 }
265}
266
Dave Martin7582e222017-10-31 15:51:08 +0000267/*
Dave Martin7582e222017-10-31 15:51:08 +0000268 * All vector length selection from userspace comes through here.
269 * We're on a slow path, so some sanity-checks are included.
270 * If things go wrong there's a bug somewhere, but try to fall back to a
271 * safe choice.
272 */
273static unsigned int find_supported_vector_length(unsigned int vl)
274{
275 int bit;
276 int max_vl = sve_max_vl;
277
278 if (WARN_ON(!sve_vl_valid(vl)))
279 vl = SVE_VL_MIN;
280
281 if (WARN_ON(!sve_vl_valid(max_vl)))
282 max_vl = SVE_VL_MIN;
283
284 if (vl > max_vl)
285 vl = max_vl;
286
287 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
Dave Martinead9e432018-09-28 14:39:21 +0100288 __vq_to_bit(sve_vq_from_vl(vl)));
289 return sve_vl_from_vq(__bit_to_vq(bit));
Dave Martin7582e222017-10-31 15:51:08 +0000290}
291
Dave Martin4ffa09a2017-10-31 15:51:15 +0000292#ifdef CONFIG_SYSCTL
293
294static int sve_proc_do_default_vl(struct ctl_table *table, int write,
295 void __user *buffer, size_t *lenp,
296 loff_t *ppos)
297{
298 int ret;
299 int vl = sve_default_vl;
300 struct ctl_table tmp_table = {
301 .data = &vl,
302 .maxlen = sizeof(vl),
303 };
304
305 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
306 if (ret || !write)
307 return ret;
308
309 /* Writing -1 has the special meaning "set to max": */
Dave Martin87c021a2018-06-01 11:10:13 +0100310 if (vl == -1)
311 vl = sve_max_vl;
Dave Martin4ffa09a2017-10-31 15:51:15 +0000312
313 if (!sve_vl_valid(vl))
314 return -EINVAL;
315
Dave Martin87c021a2018-06-01 11:10:13 +0100316 sve_default_vl = find_supported_vector_length(vl);
Dave Martin4ffa09a2017-10-31 15:51:15 +0000317 return 0;
318}
319
320static struct ctl_table sve_default_vl_table[] = {
321 {
322 .procname = "sve_default_vector_length",
323 .mode = 0644,
324 .proc_handler = sve_proc_do_default_vl,
325 },
326 { }
327};
328
329static int __init sve_sysctl_init(void)
330{
331 if (system_supports_sve())
332 if (!register_sysctl("abi", sve_default_vl_table))
333 return -EINVAL;
334
335 return 0;
336}
337
338#else /* ! CONFIG_SYSCTL */
339static int __init sve_sysctl_init(void) { return 0; }
340#endif /* ! CONFIG_SYSCTL */
341
Dave Martinbc0ee472017-10-31 15:51:05 +0000342#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
343 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
344
Dave Martin41040cf2019-06-12 17:00:32 +0100345#ifdef CONFIG_CPU_BIG_ENDIAN
346static __uint128_t arm64_cpu_to_le128(__uint128_t x)
347{
348 u64 a = swab64(x);
349 u64 b = swab64(x >> 64);
350
351 return ((__uint128_t)a << 64) | b;
352}
353#else
354static __uint128_t arm64_cpu_to_le128(__uint128_t x)
355{
356 return x;
357}
358#endif
359
360#define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
361
Dave Martinbc0ee472017-10-31 15:51:05 +0000362/*
Dave Martin65896542018-03-28 10:50:49 +0100363 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000364 * task->thread.sve_state.
365 *
366 * Task can be a non-runnable task, or current. In the latter case,
367 * softirqs (and preemption) must be disabled.
368 * task->thread.sve_state must point to at least sve_state_size(task)
369 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100370 * task->thread.uw.fpsimd_state must be up to date before calling this
371 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000372 */
373static void fpsimd_to_sve(struct task_struct *task)
374{
375 unsigned int vq;
376 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100377 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000378 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100379 __uint128_t *p;
Dave Martinbc0ee472017-10-31 15:51:05 +0000380
381 if (!system_supports_sve())
382 return;
383
384 vq = sve_vq_from_vl(task->thread.sve_vl);
Dave Martin41040cf2019-06-12 17:00:32 +0100385 for (i = 0; i < 32; ++i) {
386 p = (__uint128_t *)ZREG(sst, vq, i);
387 *p = arm64_cpu_to_le128(fst->vregs[i]);
388 }
Dave Martinbc0ee472017-10-31 15:51:05 +0000389}
390
Dave Martin8cd969d2017-10-31 15:51:07 +0000391/*
392 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100393 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000394 *
395 * Task can be a non-runnable task, or current. In the latter case,
396 * softirqs (and preemption) must be disabled.
397 * task->thread.sve_state must point to at least sve_state_size(task)
398 * bytes of allocated kernel memory.
399 * task->thread.sve_state must be up to date before calling this function.
400 */
401static void sve_to_fpsimd(struct task_struct *task)
402{
403 unsigned int vq;
404 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100405 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000406 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100407 __uint128_t const *p;
Dave Martin8cd969d2017-10-31 15:51:07 +0000408
409 if (!system_supports_sve())
410 return;
411
412 vq = sve_vq_from_vl(task->thread.sve_vl);
Dave Martin41040cf2019-06-12 17:00:32 +0100413 for (i = 0; i < 32; ++i) {
414 p = (__uint128_t const *)ZREG(sst, vq, i);
415 fst->vregs[i] = arm64_le128_to_cpu(*p);
416 }
Dave Martin8cd969d2017-10-31 15:51:07 +0000417}
418
Dave Martinbc0ee472017-10-31 15:51:05 +0000419#ifdef CONFIG_ARM64_SVE
420
421/*
422 * Return how many bytes of memory are required to store the full SVE
423 * state for task, given task's currently configured vector length.
424 */
425size_t sve_state_size(struct task_struct const *task)
426{
427 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
428}
429
430/*
431 * Ensure that task->thread.sve_state is allocated and sufficiently large.
432 *
433 * This function should be used only in preparation for replacing
434 * task->thread.sve_state with new data. The memory is always zeroed
435 * here to prevent stale data from showing through: this is done in
436 * the interest of testability and predictability: except in the
437 * do_sve_acc() case, there is no ABI requirement to hide stale data
438 * written previously be task.
439 */
440void sve_alloc(struct task_struct *task)
441{
442 if (task->thread.sve_state) {
443 memset(task->thread.sve_state, 0, sve_state_size(current));
444 return;
445 }
446
447 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
448 task->thread.sve_state =
449 kzalloc(sve_state_size(task), GFP_KERNEL);
450
451 /*
452 * If future SVE revisions can have larger vectors though,
453 * this may cease to be true:
454 */
455 BUG_ON(!task->thread.sve_state);
456}
457
Dave Martin43d4da2c42017-10-31 15:51:13 +0000458
459/*
460 * Ensure that task->thread.sve_state is up to date with respect to
461 * the user task, irrespective of when SVE is in use or not.
462 *
463 * This should only be called by ptrace. task must be non-runnable.
464 * task->thread.sve_state must point to at least sve_state_size(task)
465 * bytes of allocated kernel memory.
466 */
467void fpsimd_sync_to_sve(struct task_struct *task)
468{
469 if (!test_tsk_thread_flag(task, TIF_SVE))
470 fpsimd_to_sve(task);
471}
472
473/*
Dave Martin65896542018-03-28 10:50:49 +0100474 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000475 * the user task, irrespective of whether SVE is in use or not.
476 *
477 * This should only be called by ptrace. task must be non-runnable.
478 * task->thread.sve_state must point to at least sve_state_size(task)
479 * bytes of allocated kernel memory.
480 */
481void sve_sync_to_fpsimd(struct task_struct *task)
482{
483 if (test_tsk_thread_flag(task, TIF_SVE))
484 sve_to_fpsimd(task);
485}
486
487/*
488 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100489 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000490 *
491 * This should only be called by ptrace to merge new FPSIMD register
492 * values into a task for which SVE is currently active.
493 * task must be non-runnable.
494 * task->thread.sve_state must point to at least sve_state_size(task)
495 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100496 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000497 * the new FPSIMD register values to be merged in.
498 */
499void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
500{
501 unsigned int vq;
502 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100503 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000504 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100505 __uint128_t *p;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000506
507 if (!test_tsk_thread_flag(task, TIF_SVE))
508 return;
509
510 vq = sve_vq_from_vl(task->thread.sve_vl);
511
512 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
513
Dave Martin41040cf2019-06-12 17:00:32 +0100514 for (i = 0; i < 32; ++i) {
515 p = (__uint128_t *)ZREG(sst, vq, i);
516 *p = arm64_cpu_to_le128(fst->vregs[i]);
517 }
Dave Martin43d4da2c42017-10-31 15:51:13 +0000518}
519
Dave Martin7582e222017-10-31 15:51:08 +0000520int sve_set_vector_length(struct task_struct *task,
521 unsigned long vl, unsigned long flags)
522{
523 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
524 PR_SVE_SET_VL_ONEXEC))
525 return -EINVAL;
526
527 if (!sve_vl_valid(vl))
528 return -EINVAL;
529
530 /*
531 * Clamp to the maximum vector length that VL-agnostic SVE code can
532 * work with. A flag may be assigned in the future to allow setting
533 * of larger vector lengths without confusing older software.
534 */
535 if (vl > SVE_VL_ARCH_MAX)
536 vl = SVE_VL_ARCH_MAX;
537
538 vl = find_supported_vector_length(vl);
539
540 if (flags & (PR_SVE_VL_INHERIT |
541 PR_SVE_SET_VL_ONEXEC))
542 task->thread.sve_vl_onexec = vl;
543 else
544 /* Reset VL to system default on next exec: */
545 task->thread.sve_vl_onexec = 0;
546
547 /* Only actually set the VL if not deferred: */
548 if (flags & PR_SVE_SET_VL_ONEXEC)
549 goto out;
550
551 if (vl == task->thread.sve_vl)
552 goto out;
553
554 /*
555 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
556 * write any live register state back to task_struct, and convert to a
557 * non-SVE thread.
558 */
559 if (task == current) {
560 local_bh_disable();
561
Dave Martind1797612018-04-06 14:55:59 +0100562 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000563 }
564
565 fpsimd_flush_task_state(task);
566 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
567 sve_to_fpsimd(task);
568
569 if (task == current)
570 local_bh_enable();
571
572 /*
573 * Force reallocation of task SVE state to the correct size
574 * on next use:
575 */
576 sve_free(task);
577
578 task->thread.sve_vl = vl;
579
580out:
Dave Martin09d12232018-04-11 17:59:06 +0100581 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
582 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000583
584 return 0;
585}
586
Dave Martinbc0ee472017-10-31 15:51:05 +0000587/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000588 * Encode the current vector length and flags for return.
589 * This is only required for prctl(): ptrace has separate fields
590 *
591 * flags are as for sve_set_vector_length().
592 */
593static int sve_prctl_status(unsigned long flags)
594{
595 int ret;
596
597 if (flags & PR_SVE_SET_VL_ONEXEC)
598 ret = current->thread.sve_vl_onexec;
599 else
600 ret = current->thread.sve_vl;
601
602 if (test_thread_flag(TIF_SVE_VL_INHERIT))
603 ret |= PR_SVE_VL_INHERIT;
604
605 return ret;
606}
607
608/* PR_SVE_SET_VL */
609int sve_set_current_vl(unsigned long arg)
610{
611 unsigned long vl, flags;
612 int ret;
613
614 vl = arg & PR_SVE_VL_LEN_MASK;
615 flags = arg & ~vl;
616
617 if (!system_supports_sve())
618 return -EINVAL;
619
620 ret = sve_set_vector_length(current, vl, flags);
621 if (ret)
622 return ret;
623
624 return sve_prctl_status(flags);
625}
626
627/* PR_SVE_GET_VL */
628int sve_get_current_vl(void)
629{
630 if (!system_supports_sve())
631 return -EINVAL;
632
633 return sve_prctl_status(0);
634}
635
Dave Martin2e0f2472017-10-31 15:51:10 +0000636static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
637{
638 unsigned int vq, vl;
639 unsigned long zcr;
640
641 bitmap_zero(map, SVE_VQ_MAX);
642
643 zcr = ZCR_ELx_LEN_MASK;
644 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
645
646 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
647 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
648 vl = sve_get_vl();
649 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
Dave Martinead9e432018-09-28 14:39:21 +0100650 set_bit(__vq_to_bit(vq), map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000651 }
652}
653
Dave Martin8b08e842018-12-06 16:32:35 +0000654/*
655 * Initialise the set of known supported VQs for the boot CPU.
656 * This is called during kernel boot, before secondary CPUs are brought up.
657 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000658void __init sve_init_vq_map(void)
659{
660 sve_probe_vqs(sve_vq_map);
Dave Martind06b76b2018-09-28 14:39:10 +0100661 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000662}
663
664/*
665 * If we haven't committed to the set of supported VQs yet, filter out
666 * those not supported by the current CPU.
Dave Martin8b08e842018-12-06 16:32:35 +0000667 * This function is called during the bring-up of early secondary CPUs only.
Dave Martin2e0f2472017-10-31 15:51:10 +0000668 */
669void sve_update_vq_map(void)
670{
Dave Martind06b76b2018-09-28 14:39:10 +0100671 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
672
673 sve_probe_vqs(tmp_map);
674 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
675 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000676}
677
Dave Martin8b08e842018-12-06 16:32:35 +0000678/*
679 * Check whether the current CPU supports all VQs in the committed set.
680 * This function is called during the bring-up of late secondary CPUs only.
681 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000682int sve_verify_vq_map(void)
683{
Dave Martind06b76b2018-09-28 14:39:10 +0100684 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
685 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000686
Dave Martind06b76b2018-09-28 14:39:10 +0100687 sve_probe_vqs(tmp_map);
688
689 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
690 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
Dave Martin2e0f2472017-10-31 15:51:10 +0000691 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
692 smp_processor_id());
Dave Martind06b76b2018-09-28 14:39:10 +0100693 return -EINVAL;
Dave Martin2e0f2472017-10-31 15:51:10 +0000694 }
695
Dave Martind06b76b2018-09-28 14:39:10 +0100696 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
697 return 0;
698
699 /*
700 * For KVM, it is necessary to ensure that this CPU doesn't
701 * support any vector length that guests may have probed as
702 * unsupported.
703 */
704
705 /* Recover the set of supported VQs: */
706 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
707 /* Find VQs supported that are not globally supported: */
708 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
709
710 /* Find the lowest such VQ, if any: */
711 b = find_last_bit(tmp_map, SVE_VQ_MAX);
712 if (b >= SVE_VQ_MAX)
713 return 0; /* no mismatches */
714
715 /*
716 * Mismatches above sve_max_virtualisable_vl are fine, since
717 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
718 */
Dave Martinead9e432018-09-28 14:39:21 +0100719 if (sve_vl_from_vq(__bit_to_vq(b)) <= sve_max_virtualisable_vl) {
Dave Martind06b76b2018-09-28 14:39:10 +0100720 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
721 smp_processor_id());
722 return -EINVAL;
723 }
724
725 return 0;
Dave Martin2e0f2472017-10-31 15:51:10 +0000726}
727
Dave Martinfdfa9762017-10-31 15:51:12 +0000728static void __init sve_efi_setup(void)
729{
730 if (!IS_ENABLED(CONFIG_EFI))
731 return;
732
733 /*
734 * alloc_percpu() warns and prints a backtrace if this goes wrong.
735 * This is evidence of a crippled system and we are returning void,
736 * so no attempt is made to handle this situation here.
737 */
738 if (!sve_vl_valid(sve_max_vl))
739 goto fail;
740
741 efi_sve_state = __alloc_percpu(
742 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
743 if (!efi_sve_state)
744 goto fail;
745
746 return;
747
748fail:
749 panic("Cannot allocate percpu memory for EFI SVE save/restore");
750}
751
Dave Martin2e0f2472017-10-31 15:51:10 +0000752/*
753 * Enable SVE for EL1.
754 * Intended for use by the cpufeatures code during CPU boot.
755 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100756void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000757{
758 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
759 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000760}
761
Dave Martin31dc52b2018-04-12 16:47:20 +0100762/*
763 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
764 * vector length.
765 *
766 * Use only if SVE is present.
767 * This function clobbers the SVE vector length.
768 */
769u64 read_zcr_features(void)
770{
771 u64 zcr;
772 unsigned int vq_max;
773
774 /*
775 * Set the maximum possible VL, and write zeroes to all other
776 * bits to see if they stick.
777 */
778 sve_kernel_enable(NULL);
779 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
780
781 zcr = read_sysreg_s(SYS_ZCR_EL1);
782 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
783 vq_max = sve_vq_from_vl(sve_get_vl());
784 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
785
786 return zcr;
787}
788
Dave Martin2e0f2472017-10-31 15:51:10 +0000789void __init sve_setup(void)
790{
791 u64 zcr;
Dave Martind06b76b2018-09-28 14:39:10 +0100792 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
793 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000794
795 if (!system_supports_sve())
796 return;
797
798 /*
799 * The SVE architecture mandates support for 128-bit vectors,
800 * so sve_vq_map must have at least SVE_VQ_MIN set.
801 * If something went wrong, at least try to patch it up:
802 */
Dave Martinead9e432018-09-28 14:39:21 +0100803 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
804 set_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000805
806 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
807 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
808
809 /*
810 * Sanity-check that the max VL we determined through CPU features
811 * corresponds properly to sve_vq_map. If not, do our best:
812 */
813 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
814 sve_max_vl = find_supported_vector_length(sve_max_vl);
815
816 /*
817 * For the default VL, pick the maximum supported value <= 64.
818 * VL == 64 is guaranteed not to grow the signal frame.
819 */
820 sve_default_vl = find_supported_vector_length(64);
821
Dave Martind06b76b2018-09-28 14:39:10 +0100822 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
823 SVE_VQ_MAX);
824
825 b = find_last_bit(tmp_map, SVE_VQ_MAX);
826 if (b >= SVE_VQ_MAX)
827 /* No non-virtualisable VLs found */
828 sve_max_virtualisable_vl = SVE_VQ_MAX;
829 else if (WARN_ON(b == SVE_VQ_MAX - 1))
830 /* No virtualisable VLs? This is architecturally forbidden. */
831 sve_max_virtualisable_vl = SVE_VQ_MIN;
832 else /* b + 1 < SVE_VQ_MAX */
Dave Martinead9e432018-09-28 14:39:21 +0100833 sve_max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
Dave Martind06b76b2018-09-28 14:39:10 +0100834
835 if (sve_max_virtualisable_vl > sve_max_vl)
836 sve_max_virtualisable_vl = sve_max_vl;
837
Dave Martin2e0f2472017-10-31 15:51:10 +0000838 pr_info("SVE: maximum available vector length %u bytes per vector\n",
839 sve_max_vl);
840 pr_info("SVE: default vector length %u bytes per vector\n",
841 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000842
Dave Martind06b76b2018-09-28 14:39:10 +0100843 /* KVM decides whether to support mismatched systems. Just warn here: */
844 if (sve_max_virtualisable_vl < sve_max_vl)
845 pr_warn("SVE: unvirtualisable vector lengths present\n");
846
Dave Martinfdfa9762017-10-31 15:51:12 +0000847 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000848}
849
850/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000851 * Called from the put_task_struct() path, which cannot get here
852 * unless dead_task is really dead and not schedulable.
853 */
854void fpsimd_release_task(struct task_struct *dead_task)
855{
856 __sve_free(dead_task);
857}
858
859#endif /* CONFIG_ARM64_SVE */
860
861/*
862 * Trapped SVE access
863 *
864 * Storage is allocated for the full SVE state, the current FPSIMD
865 * register contents are migrated across, and TIF_SVE is set so that
866 * the SVE access trap will be disabled the next time this task
867 * reaches ret_to_user.
868 *
869 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
870 * would have disabled the SVE access trap for userspace during
871 * ret_to_user, making an SVE access trap impossible in that case.
872 */
873asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
874{
875 /* Even if we chose not to use SVE, the hardware could still trap: */
876 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000877 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000878 return;
879 }
880
881 sve_alloc(current);
882
883 local_bh_disable();
884
Dave Martind1797612018-04-06 14:55:59 +0100885 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000886
887 /* Force ret_to_user to reload the registers: */
888 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000889
Dave Martinefbc2022018-09-28 14:39:05 +0100890 fpsimd_to_sve(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000891 if (test_and_set_thread_flag(TIF_SVE))
892 WARN_ON(1); /* SVE access shouldn't have trapped */
893
894 local_bh_enable();
895}
896
Catalin Marinas53631b52012-03-05 11:49:32 +0000897/*
898 * Trapped FP/ASIMD access.
899 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000900asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000901{
902 /* TODO: implement lazy context saving/restoring */
903 WARN_ON(1);
904}
905
906/*
907 * Raise a SIGFPE for the current process.
908 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000909asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000910{
Dave Martinaf4a81b2018-03-01 17:44:07 +0000911 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000912
Dave Martinaf4a81b2018-03-01 17:44:07 +0000913 if (esr & ESR_ELx_FP_EXC_TFV) {
914 if (esr & FPEXC_IOF)
915 si_code = FPE_FLTINV;
916 else if (esr & FPEXC_DZF)
917 si_code = FPE_FLTDIV;
918 else if (esr & FPEXC_OFF)
919 si_code = FPE_FLTOVF;
920 else if (esr & FPEXC_UFF)
921 si_code = FPE_FLTUND;
922 else if (esr & FPEXC_IXF)
923 si_code = FPE_FLTRES;
924 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000925
Eric W. Biedermanc8526802018-04-16 13:47:06 -0500926 send_sig_fault(SIGFPE, si_code,
927 (void __user *)instruction_pointer(regs),
928 current);
Catalin Marinas53631b52012-03-05 11:49:32 +0000929}
930
931void fpsimd_thread_switch(struct task_struct *next)
932{
Dave Martindf3fb962018-05-21 19:08:15 +0100933 bool wrong_task, wrong_cpu;
934
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000935 if (!system_supports_fpsimd())
936 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200937
Dave Martindf3fb962018-05-21 19:08:15 +0100938 /* Save unsaved fpsimd state, if any: */
939 fpsimd_save();
940
Catalin Marinas53631b52012-03-05 11:49:32 +0000941 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100942 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
943 * state. For kernel threads, FPSIMD registers are never loaded
944 * and wrong_task and wrong_cpu will always be true.
Catalin Marinas53631b52012-03-05 11:49:32 +0000945 */
Dave Martindf3fb962018-05-21 19:08:15 +0100946 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100947 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100948 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100949
Dave Martindf3fb962018-05-21 19:08:15 +0100950 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
951 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000952}
953
954void fpsimd_flush_thread(void)
955{
Dave Martin7582e222017-10-31 15:51:08 +0000956 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000957
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000958 if (!system_supports_fpsimd())
959 return;
Dave Martincb84d112017-08-03 17:23:23 +0100960
961 local_bh_disable();
962
Dave Martinefbc2022018-09-28 14:39:05 +0100963 fpsimd_flush_task_state(current);
Dave Martin65896542018-03-28 10:50:49 +0100964 memset(&current->thread.uw.fpsimd_state, 0,
965 sizeof(current->thread.uw.fpsimd_state));
Dave Martinbc0ee472017-10-31 15:51:05 +0000966
967 if (system_supports_sve()) {
968 clear_thread_flag(TIF_SVE);
969 sve_free(current);
970
971 /*
972 * Reset the task vector length as required.
973 * This is where we ensure that all user tasks have a valid
974 * vector length configured: no kernel task can become a user
975 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000976 * By the time the first call to this function is made, all
977 * early hardware probing is complete, so sve_default_vl
978 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000979 * If a bug causes this to go wrong, we make some noise and
980 * try to fudge thread.sve_vl to a safe value here.
981 */
Dave Martin79ab0472017-10-31 15:51:06 +0000982 vl = current->thread.sve_vl_onexec ?
983 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000984
985 if (WARN_ON(!sve_vl_valid(vl)))
986 vl = SVE_VL_MIN;
987
Dave Martin7582e222017-10-31 15:51:08 +0000988 supported_vl = find_supported_vector_length(vl);
989 if (WARN_ON(supported_vl != vl))
990 vl = supported_vl;
991
Dave Martinbc0ee472017-10-31 15:51:05 +0000992 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000993
994 /*
995 * If the task is not set to inherit, ensure that the vector
996 * length will be reset by a subsequent exec:
997 */
998 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
999 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +00001000 }
1001
Dave Martincb84d112017-08-03 17:23:23 +01001002 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +00001003}
1004
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001005/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001006 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1007 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001008 */
1009void fpsimd_preserve_current_state(void)
1010{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001011 if (!system_supports_fpsimd())
1012 return;
Dave Martincb84d112017-08-03 17:23:23 +01001013
1014 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +01001015 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001016 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001017}
1018
1019/*
Dave Martin8cd969d2017-10-31 15:51:07 +00001020 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +01001021 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +00001022 * the signal frame.
1023 */
1024void fpsimd_signal_preserve_current_state(void)
1025{
1026 fpsimd_preserve_current_state();
1027 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1028 sve_to_fpsimd(current);
1029}
1030
1031/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001032 * Associate current's FPSIMD context with this cpu
1033 * Preemption must be disabled when calling this function.
1034 */
Dave Martine6b673b2018-04-06 14:55:59 +01001035void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +00001036{
Dave Martincb968af2017-12-06 16:45:47 +00001037 struct fpsimd_last_state_struct *last =
1038 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001039
Dave Martin65896542018-03-28 10:50:49 +01001040 last->st = &current->thread.uw.fpsimd_state;
Dave Martin04950672018-09-28 14:39:11 +01001041 last->sve_state = current->thread.sve_state;
1042 last->sve_vl = current->thread.sve_vl;
Dave Martin20b85472018-03-28 10:50:48 +01001043 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +01001044
1045 if (system_supports_sve()) {
1046 /* Toggle SVE trapping for userspace if needed */
1047 if (test_thread_flag(TIF_SVE))
1048 sve_user_enable();
1049 else
1050 sve_user_disable();
1051
1052 /* Serialised by exception return to user */
1053 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001054}
1055
Dave Martin04950672018-09-28 14:39:11 +01001056void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1057 unsigned int sve_vl)
Dave Martine6b673b2018-04-06 14:55:59 +01001058{
1059 struct fpsimd_last_state_struct *last =
1060 this_cpu_ptr(&fpsimd_last_state);
1061
1062 WARN_ON(!in_softirq() && !irqs_disabled());
1063
1064 last->st = st;
Dave Martin04950672018-09-28 14:39:11 +01001065 last->sve_state = sve_state;
1066 last->sve_vl = sve_vl;
Dave Martin8884b7b2017-12-06 16:45:46 +00001067}
1068
1069/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001070 * Load the userland FPSIMD state of 'current' from memory, but only if the
1071 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1072 * state of 'current'
1073 */
1074void fpsimd_restore_current_state(void)
1075{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001076 if (!system_supports_fpsimd())
1077 return;
Dave Martincb84d112017-08-03 17:23:23 +01001078
1079 local_bh_disable();
1080
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001081 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001082 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001083 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001084 }
Dave Martincb84d112017-08-03 17:23:23 +01001085
1086 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001087}
1088
1089/*
1090 * Load an updated userland FPSIMD state for 'current' from memory and set the
1091 * flag that indicates that the FPSIMD register contents are the most recent
1092 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001093 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001094void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001095{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001096 if (!system_supports_fpsimd())
1097 return;
Dave Martincb84d112017-08-03 17:23:23 +01001098
1099 local_bh_disable();
1100
Dave Martin65896542018-03-28 10:50:49 +01001101 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001102 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001103 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001104
Dave Martin8cd969d2017-10-31 15:51:07 +00001105 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001106 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001107
Dave Martin0cff8e72018-05-09 14:27:41 +01001108 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001109
1110 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001111}
1112
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001113/*
1114 * Invalidate live CPU copies of task t's FPSIMD state
Dave Martinefbc2022018-09-28 14:39:05 +01001115 *
1116 * This function may be called with preemption enabled. The barrier()
1117 * ensures that the assignment to fpsimd_cpu is visible to any
1118 * preemption/softirq that could race with set_tsk_thread_flag(), so
1119 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1120 *
1121 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1122 * subsequent code.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001123 */
1124void fpsimd_flush_task_state(struct task_struct *t)
1125{
Dave Martin20b85472018-03-28 10:50:48 +01001126 t->thread.fpsimd_cpu = NR_CPUS;
Dave Martinefbc2022018-09-28 14:39:05 +01001127
1128 barrier();
1129 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1130
1131 barrier();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001132}
1133
Dave Martinefbc2022018-09-28 14:39:05 +01001134/*
1135 * Invalidate any task's FPSIMD state that is present on this cpu.
1136 * This function must be called with softirqs disabled.
1137 */
Dave Martine6b673b2018-04-06 14:55:59 +01001138void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001139{
Dave Martincb968af2017-12-06 16:45:47 +00001140 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001141 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001142}
1143
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001144#ifdef CONFIG_KERNEL_MODE_NEON
1145
Dave Martincb84d112017-08-03 17:23:23 +01001146DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001147EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001148
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001149/*
1150 * Kernel-side NEON support functions
1151 */
Dave Martincb84d112017-08-03 17:23:23 +01001152
1153/*
1154 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1155 * context
1156 *
1157 * Must not be called unless may_use_simd() returns true.
1158 * Task context in the FPSIMD registers is saved back to memory as necessary.
1159 *
1160 * A matching call to kernel_neon_end() must be made before returning from the
1161 * calling context.
1162 *
1163 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1164 * called.
1165 */
1166void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001167{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001168 if (WARN_ON(!system_supports_fpsimd()))
1169 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001170
Dave Martincb84d112017-08-03 17:23:23 +01001171 BUG_ON(!may_use_simd());
1172
1173 local_bh_disable();
1174
1175 __this_cpu_write(kernel_neon_busy, true);
1176
Dave Martindf3fb962018-05-21 19:08:15 +01001177 /* Save unsaved fpsimd state, if any: */
1178 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001179
1180 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001181 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001182
1183 preempt_disable();
1184
1185 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001186}
Dave Martincb84d112017-08-03 17:23:23 +01001187EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001188
Dave Martincb84d112017-08-03 17:23:23 +01001189/*
1190 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1191 *
1192 * Must be called from a context in which kernel_neon_begin() was previously
1193 * called, with no call to kernel_neon_end() in the meantime.
1194 *
1195 * The caller must not use the FPSIMD registers after this function is called,
1196 * unless kernel_neon_begin() is called again in the meantime.
1197 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001198void kernel_neon_end(void)
1199{
Dave Martincb84d112017-08-03 17:23:23 +01001200 bool busy;
1201
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001202 if (!system_supports_fpsimd())
1203 return;
Dave Martincb84d112017-08-03 17:23:23 +01001204
1205 busy = __this_cpu_xchg(kernel_neon_busy, false);
1206 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1207
1208 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001209}
1210EXPORT_SYMBOL(kernel_neon_end);
1211
Dave Martine580b8b2017-09-18 09:40:12 +01001212#ifdef CONFIG_EFI
1213
Dave Martin20b85472018-03-28 10:50:48 +01001214static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001215static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001216static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001217
1218/*
1219 * EFI runtime services support functions
1220 *
1221 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1222 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1223 * is always used rather than being an optional accelerator.
1224 *
1225 * These functions provide the necessary support for ensuring FPSIMD
1226 * save/restore in the contexts from which EFI is used.
1227 *
1228 * Do not use them for any other purpose -- if tempted to do so, you are
1229 * either doing something wrong or you need to propose some refactoring.
1230 */
1231
1232/*
1233 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1234 */
1235void __efi_fpsimd_begin(void)
1236{
1237 if (!system_supports_fpsimd())
1238 return;
1239
1240 WARN_ON(preemptible());
1241
Dave Martinfdfa9762017-10-31 15:51:12 +00001242 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001243 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001244 } else {
1245 /*
1246 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1247 * preserving:
1248 */
1249 if (system_supports_sve() && likely(efi_sve_state)) {
1250 char *sve_state = this_cpu_ptr(efi_sve_state);
1251
1252 __this_cpu_write(efi_sve_state_used, true);
1253
1254 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1255 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1256 } else {
1257 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1258 }
1259
Dave Martin4328825d2017-08-03 17:23:22 +01001260 __this_cpu_write(efi_fpsimd_state_used, true);
1261 }
1262}
1263
1264/*
1265 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1266 */
1267void __efi_fpsimd_end(void)
1268{
1269 if (!system_supports_fpsimd())
1270 return;
1271
Dave Martinfdfa9762017-10-31 15:51:12 +00001272 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001273 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001274 } else {
1275 if (system_supports_sve() &&
1276 likely(__this_cpu_read(efi_sve_state_used))) {
1277 char const *sve_state = this_cpu_ptr(efi_sve_state);
1278
1279 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1280 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1281 sve_vq_from_vl(sve_get_vl()) - 1);
1282
1283 __this_cpu_write(efi_sve_state_used, false);
1284 } else {
1285 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1286 }
1287 }
Dave Martin4328825d2017-08-03 17:23:22 +01001288}
1289
Dave Martine580b8b2017-09-18 09:40:12 +01001290#endif /* CONFIG_EFI */
1291
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001292#endif /* CONFIG_KERNEL_MODE_NEON */
1293
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001294#ifdef CONFIG_CPU_PM
1295static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1296 unsigned long cmd, void *v)
1297{
1298 switch (cmd) {
1299 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001300 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001301 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001302 break;
1303 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001304 break;
1305 case CPU_PM_ENTER_FAILED:
1306 default:
1307 return NOTIFY_DONE;
1308 }
1309 return NOTIFY_OK;
1310}
1311
1312static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1313 .notifier_call = fpsimd_cpu_pm_notifier,
1314};
1315
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001316static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001317{
1318 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1319}
1320
1321#else
1322static inline void fpsimd_pm_init(void) { }
1323#endif /* CONFIG_CPU_PM */
1324
Janet Liu32365e62015-06-11 12:02:45 +08001325#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001326static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001327{
Dave Martincb968af2017-12-06 16:45:47 +00001328 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001329 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001330}
1331
Janet Liu32365e62015-06-11 12:02:45 +08001332static inline void fpsimd_hotplug_init(void)
1333{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001334 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1335 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001336}
1337
1338#else
1339static inline void fpsimd_hotplug_init(void) { }
1340#endif
1341
Catalin Marinas53631b52012-03-05 11:49:32 +00001342/*
1343 * FP/SIMD support code initialisation.
1344 */
1345static int __init fpsimd_init(void)
1346{
Andrew Murrayaaba0982019-04-09 10:52:40 +01001347 if (cpu_have_named_feature(FP)) {
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001348 fpsimd_pm_init();
1349 fpsimd_hotplug_init();
1350 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001351 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001352 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001353
Andrew Murrayaaba0982019-04-09 10:52:40 +01001354 if (!cpu_have_named_feature(ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001355 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001356
Dave Martin4ffa09a2017-10-31 15:51:15 +00001357 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001358}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001359core_initcall(fpsimd_init);