blob: bb42cd04baec6c8db21054e168ac448dabbe5cdb [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>
Dave Martin41040cf2019-06-12 17:00:32 +010042#include <linux/swab.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000043
Dave Martinaf4a81b2018-03-01 17:44:07 +000044#include <asm/esr.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000045#include <asm/fpsimd.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010046#include <asm/cpufeature.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000047#include <asm/cputype.h>
Dave Martin2cf97d42018-04-12 17:04:39 +010048#include <asm/processor.h>
Dave Martin4328825d2017-08-03 17:23:22 +010049#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000050#include <asm/sigcontext.h>
51#include <asm/sysreg.h>
52#include <asm/traps.h>
Dave Martind06b76b2018-09-28 14:39:10 +010053#include <asm/virt.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000054
55#define FPEXC_IOF (1 << 0)
56#define FPEXC_DZF (1 << 1)
57#define FPEXC_OFF (1 << 2)
58#define FPEXC_UFF (1 << 3)
59#define FPEXC_IXF (1 << 4)
60#define FPEXC_IDF (1 << 7)
61
62/*
Dave Martinbc0ee472017-10-31 15:51:05 +000063 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
64 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020065 * In order to reduce the number of times the FPSIMD state is needlessly saved
66 * and restored, we need to keep track of two things:
67 * (a) for each task, we need to remember which CPU was the last one to have
68 * the task's FPSIMD state loaded into its FPSIMD registers;
69 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
70 * been loaded into its FPSIMD registers most recently, or whether it has
71 * been used to perform kernel mode NEON in the meantime.
72 *
Dave Martin20b85472018-03-28 10:50:48 +010073 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080074 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020075 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
76 * address of the userland FPSIMD state of the task that was loaded onto the CPU
77 * the most recently, or NULL if kernel mode NEON has been performed after that.
78 *
79 * With this in place, we no longer have to restore the next FPSIMD state right
80 * when switching between tasks. Instead, we can defer this check to userland
81 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010082 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020083 * can omit the FPSIMD restore.
84 *
85 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
86 * indicate whether or not the userland FPSIMD state of the current task is
87 * present in the registers. The flag is set unless the FPSIMD registers of this
88 * CPU currently contain the most recent userland FPSIMD state of the current
89 * task.
90 *
Dave Martincb84d112017-08-03 17:23:23 +010091 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
92 * save the task's FPSIMD context back to task_struct from softirq context.
93 * To prevent this from racing with the manipulation of the task's FPSIMD state
94 * from task context and thereby corrupting the state, it is necessary to
95 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
96 * flag with local_bh_disable() unless softirqs are already masked.
97 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020098 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010099 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200100 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
101 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
102 * cleared, otherwise it is set;
103 *
104 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
105 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +0100106 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200107 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
108 * TIF_FOREIGN_FPSTATE flag is cleared;
109 *
110 * - the task executes an ordinary syscall; upon return to userland, the
111 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
112 * restored;
113 *
114 * - the task executes a syscall which executes some NEON instructions; this is
115 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
116 * register contents to memory, clears the fpsimd_last_state per-cpu variable
117 * and sets the TIF_FOREIGN_FPSTATE flag;
118 *
119 * - the task gets preempted after kernel_neon_end() is called; as we have not
120 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
121 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
122 */
Dave Martincb968af2017-12-06 16:45:47 +0000123struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100124 struct user_fpsimd_state *st;
Dave Martin04950672018-09-28 14:39:11 +0100125 void *sve_state;
126 unsigned int sve_vl;
Dave Martincb968af2017-12-06 16:45:47 +0000127};
128
129static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200130
Dave Martin79ab0472017-10-31 15:51:06 +0000131/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000132static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000133
Dave Martin7582e222017-10-31 15:51:08 +0000134#ifdef CONFIG_ARM64_SVE
135
136/* Maximum supported vector length across all CPUs (initially poisoned) */
Dave Martin87c021a2018-06-01 11:10:13 +0100137int __ro_after_init sve_max_vl = SVE_VL_MIN;
Dave Martind06b76b2018-09-28 14:39:10 +0100138int __ro_after_init sve_max_virtualisable_vl = SVE_VL_MIN;
Dave Martin624835a2019-04-11 16:53:18 +0100139
140/*
141 * Set of available vector lengths,
142 * where length vq encoded as bit __vq_to_bit(vq):
143 */
Dave Martinead9e432018-09-28 14:39:21 +0100144__ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100145/* Set of vector lengths present on at least one cpu: */
146static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martin624835a2019-04-11 16:53:18 +0100147
Dave Martinfdfa9762017-10-31 15:51:12 +0000148static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000149
150#else /* ! CONFIG_ARM64_SVE */
151
152/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000153extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martind06b76b2018-09-28 14:39:10 +0100154extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000155extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000156
157#endif /* ! CONFIG_ARM64_SVE */
158
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200159/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000160 * Call __sve_free() directly only if you know task can't be scheduled
161 * or preempted.
162 */
163static void __sve_free(struct task_struct *task)
164{
165 kfree(task->thread.sve_state);
166 task->thread.sve_state = NULL;
167}
168
169static void sve_free(struct task_struct *task)
170{
171 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
172
173 __sve_free(task);
174}
175
Dave Martinbc0ee472017-10-31 15:51:05 +0000176/*
177 * TIF_SVE controls whether a task can use SVE without trapping while
178 * in userspace, and also the way a task's FPSIMD/SVE state is stored
179 * in thread_struct.
180 *
181 * The kernel uses this flag to track whether a user task is actively
182 * using SVE, and therefore whether full SVE register state needs to
183 * be tracked. If not, the cheaper FPSIMD context handling code can
184 * be used instead of the more costly SVE equivalents.
185 *
186 * * TIF_SVE set:
187 *
188 * The task can execute SVE instructions while in userspace without
189 * trapping to the kernel.
190 *
191 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
192 * corresponding Zn), P0-P15 and FFR are encoded in in
193 * task->thread.sve_state, formatted appropriately for vector
194 * length task->thread.sve_vl.
195 *
196 * task->thread.sve_state must point to a valid buffer at least
197 * sve_state_size(task) bytes in size.
198 *
199 * During any syscall, the kernel may optionally clear TIF_SVE and
200 * discard the vector state except for the FPSIMD subset.
201 *
202 * * TIF_SVE clear:
203 *
204 * An attempt by the user task to execute an SVE instruction causes
205 * do_sve_acc() to be called, which does some preparation and then
206 * sets TIF_SVE.
207 *
208 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100209 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000210 * logically zero but not stored anywhere; P0-P15 and FFR are not
211 * stored and have unspecified values from userspace's point of
212 * view. For hygiene purposes, the kernel zeroes them on next use,
213 * but userspace is discouraged from relying on this.
214 *
215 * task->thread.sve_state does not need to be non-NULL, valid or any
216 * particular size: it must not be dereferenced.
217 *
Dave Martin65896542018-03-28 10:50:49 +0100218 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
219 * irrespective of whether TIF_SVE is clear or set, since these are
220 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000221 */
222
223/*
224 * Update current's FPSIMD/SVE registers from thread_struct.
225 *
226 * This function should be called only when the FPSIMD/SVE state in
227 * thread_struct is known to be up to date, when preparing to enter
228 * userspace.
229 *
230 * Softirqs (and preemption) must be disabled.
231 */
232static void task_fpsimd_load(void)
233{
234 WARN_ON(!in_softirq() && !irqs_disabled());
235
236 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin2cf97d42018-04-12 17:04:39 +0100237 sve_load_state(sve_pffr(&current->thread),
Dave Martin65896542018-03-28 10:50:49 +0100238 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000239 sve_vq_from_vl(current->thread.sve_vl) - 1);
240 else
Dave Martin65896542018-03-28 10:50:49 +0100241 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000242}
243
244/*
Dave Martind1797612018-04-06 14:55:59 +0100245 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
246 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000247 *
248 * Softirqs (and preemption) must be disabled.
249 */
Dave Martine6b673b2018-04-06 14:55:59 +0100250void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000251{
Dave Martin04950672018-09-28 14:39:11 +0100252 struct fpsimd_last_state_struct const *last =
253 this_cpu_ptr(&fpsimd_last_state);
Dave Martine6b673b2018-04-06 14:55:59 +0100254 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100255
Dave Martinbc0ee472017-10-31 15:51:05 +0000256 WARN_ON(!in_softirq() && !irqs_disabled());
257
258 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
259 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
Dave Martin04950672018-09-28 14:39:11 +0100260 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
Dave Martinbc0ee472017-10-31 15:51:05 +0000261 /*
262 * Can't save the user regs, so current would
263 * re-enter user with corrupt state.
264 * There's no way to recover, so kill it:
265 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000266 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000267 return;
268 }
269
Dave Martin04950672018-09-28 14:39:11 +0100270 sve_save_state((char *)last->sve_state +
271 sve_ffr_offset(last->sve_vl),
272 &last->st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000273 } else
Dave Martin04950672018-09-28 14:39:11 +0100274 fpsimd_save_state(last->st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000275 }
276}
277
Dave Martin7582e222017-10-31 15:51:08 +0000278/*
Dave Martin7582e222017-10-31 15:51:08 +0000279 * All vector length selection from userspace comes through here.
280 * We're on a slow path, so some sanity-checks are included.
281 * If things go wrong there's a bug somewhere, but try to fall back to a
282 * safe choice.
283 */
284static unsigned int find_supported_vector_length(unsigned int vl)
285{
286 int bit;
287 int max_vl = sve_max_vl;
288
289 if (WARN_ON(!sve_vl_valid(vl)))
290 vl = SVE_VL_MIN;
291
292 if (WARN_ON(!sve_vl_valid(max_vl)))
293 max_vl = SVE_VL_MIN;
294
295 if (vl > max_vl)
296 vl = max_vl;
297
298 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
Dave Martinead9e432018-09-28 14:39:21 +0100299 __vq_to_bit(sve_vq_from_vl(vl)));
300 return sve_vl_from_vq(__bit_to_vq(bit));
Dave Martin7582e222017-10-31 15:51:08 +0000301}
302
Dave Martin4ffa09a2017-10-31 15:51:15 +0000303#ifdef CONFIG_SYSCTL
304
305static int sve_proc_do_default_vl(struct ctl_table *table, int write,
306 void __user *buffer, size_t *lenp,
307 loff_t *ppos)
308{
309 int ret;
310 int vl = sve_default_vl;
311 struct ctl_table tmp_table = {
312 .data = &vl,
313 .maxlen = sizeof(vl),
314 };
315
316 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
317 if (ret || !write)
318 return ret;
319
320 /* Writing -1 has the special meaning "set to max": */
Dave Martin87c021a2018-06-01 11:10:13 +0100321 if (vl == -1)
322 vl = sve_max_vl;
Dave Martin4ffa09a2017-10-31 15:51:15 +0000323
324 if (!sve_vl_valid(vl))
325 return -EINVAL;
326
Dave Martin87c021a2018-06-01 11:10:13 +0100327 sve_default_vl = find_supported_vector_length(vl);
Dave Martin4ffa09a2017-10-31 15:51:15 +0000328 return 0;
329}
330
331static struct ctl_table sve_default_vl_table[] = {
332 {
333 .procname = "sve_default_vector_length",
334 .mode = 0644,
335 .proc_handler = sve_proc_do_default_vl,
336 },
337 { }
338};
339
340static int __init sve_sysctl_init(void)
341{
342 if (system_supports_sve())
343 if (!register_sysctl("abi", sve_default_vl_table))
344 return -EINVAL;
345
346 return 0;
347}
348
349#else /* ! CONFIG_SYSCTL */
350static int __init sve_sysctl_init(void) { return 0; }
351#endif /* ! CONFIG_SYSCTL */
352
Dave Martinbc0ee472017-10-31 15:51:05 +0000353#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
354 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
355
Dave Martin41040cf2019-06-12 17:00:32 +0100356#ifdef CONFIG_CPU_BIG_ENDIAN
357static __uint128_t arm64_cpu_to_le128(__uint128_t x)
358{
359 u64 a = swab64(x);
360 u64 b = swab64(x >> 64);
361
362 return ((__uint128_t)a << 64) | b;
363}
364#else
365static __uint128_t arm64_cpu_to_le128(__uint128_t x)
366{
367 return x;
368}
369#endif
370
371#define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
372
Dave Martinbc0ee472017-10-31 15:51:05 +0000373/*
Dave Martin65896542018-03-28 10:50:49 +0100374 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000375 * task->thread.sve_state.
376 *
377 * Task can be a non-runnable task, or current. In the latter case,
378 * softirqs (and preemption) must be disabled.
379 * task->thread.sve_state must point to at least sve_state_size(task)
380 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100381 * task->thread.uw.fpsimd_state must be up to date before calling this
382 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000383 */
384static void fpsimd_to_sve(struct task_struct *task)
385{
386 unsigned int vq;
387 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100388 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000389 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100390 __uint128_t *p;
Dave Martinbc0ee472017-10-31 15:51:05 +0000391
392 if (!system_supports_sve())
393 return;
394
395 vq = sve_vq_from_vl(task->thread.sve_vl);
Dave Martin41040cf2019-06-12 17:00:32 +0100396 for (i = 0; i < 32; ++i) {
397 p = (__uint128_t *)ZREG(sst, vq, i);
398 *p = arm64_cpu_to_le128(fst->vregs[i]);
399 }
Dave Martinbc0ee472017-10-31 15:51:05 +0000400}
401
Dave Martin8cd969d2017-10-31 15:51:07 +0000402/*
403 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100404 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000405 *
406 * Task can be a non-runnable task, or current. In the latter case,
407 * softirqs (and preemption) must be disabled.
408 * task->thread.sve_state must point to at least sve_state_size(task)
409 * bytes of allocated kernel memory.
410 * task->thread.sve_state must be up to date before calling this function.
411 */
412static void sve_to_fpsimd(struct task_struct *task)
413{
414 unsigned int vq;
415 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100416 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000417 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100418 __uint128_t const *p;
Dave Martin8cd969d2017-10-31 15:51:07 +0000419
420 if (!system_supports_sve())
421 return;
422
423 vq = sve_vq_from_vl(task->thread.sve_vl);
Dave Martin41040cf2019-06-12 17:00:32 +0100424 for (i = 0; i < 32; ++i) {
425 p = (__uint128_t const *)ZREG(sst, vq, i);
426 fst->vregs[i] = arm64_le128_to_cpu(*p);
427 }
Dave Martin8cd969d2017-10-31 15:51:07 +0000428}
429
Dave Martinbc0ee472017-10-31 15:51:05 +0000430#ifdef CONFIG_ARM64_SVE
431
432/*
433 * Return how many bytes of memory are required to store the full SVE
434 * state for task, given task's currently configured vector length.
435 */
436size_t sve_state_size(struct task_struct const *task)
437{
438 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
439}
440
441/*
442 * Ensure that task->thread.sve_state is allocated and sufficiently large.
443 *
444 * This function should be used only in preparation for replacing
445 * task->thread.sve_state with new data. The memory is always zeroed
446 * here to prevent stale data from showing through: this is done in
447 * the interest of testability and predictability: except in the
448 * do_sve_acc() case, there is no ABI requirement to hide stale data
449 * written previously be task.
450 */
451void sve_alloc(struct task_struct *task)
452{
453 if (task->thread.sve_state) {
454 memset(task->thread.sve_state, 0, sve_state_size(current));
455 return;
456 }
457
458 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
459 task->thread.sve_state =
460 kzalloc(sve_state_size(task), GFP_KERNEL);
461
462 /*
463 * If future SVE revisions can have larger vectors though,
464 * this may cease to be true:
465 */
466 BUG_ON(!task->thread.sve_state);
467}
468
Dave Martin43d4da2c42017-10-31 15:51:13 +0000469
470/*
471 * Ensure that task->thread.sve_state is up to date with respect to
472 * the user task, irrespective of when SVE is in use or not.
473 *
474 * This should only be called by ptrace. task must be non-runnable.
475 * task->thread.sve_state must point to at least sve_state_size(task)
476 * bytes of allocated kernel memory.
477 */
478void fpsimd_sync_to_sve(struct task_struct *task)
479{
480 if (!test_tsk_thread_flag(task, TIF_SVE))
481 fpsimd_to_sve(task);
482}
483
484/*
Dave Martin65896542018-03-28 10:50:49 +0100485 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000486 * the user task, irrespective of whether SVE is in use or not.
487 *
488 * This should only be called by ptrace. task must be non-runnable.
489 * task->thread.sve_state must point to at least sve_state_size(task)
490 * bytes of allocated kernel memory.
491 */
492void sve_sync_to_fpsimd(struct task_struct *task)
493{
494 if (test_tsk_thread_flag(task, TIF_SVE))
495 sve_to_fpsimd(task);
496}
497
498/*
499 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100500 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000501 *
502 * This should only be called by ptrace to merge new FPSIMD register
503 * values into a task for which SVE is currently active.
504 * task must be non-runnable.
505 * task->thread.sve_state must point to at least sve_state_size(task)
506 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100507 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000508 * the new FPSIMD register values to be merged in.
509 */
510void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
511{
512 unsigned int vq;
513 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100514 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000515 unsigned int i;
Dave Martin41040cf2019-06-12 17:00:32 +0100516 __uint128_t *p;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000517
518 if (!test_tsk_thread_flag(task, TIF_SVE))
519 return;
520
521 vq = sve_vq_from_vl(task->thread.sve_vl);
522
523 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
524
Dave Martin41040cf2019-06-12 17:00:32 +0100525 for (i = 0; i < 32; ++i) {
526 p = (__uint128_t *)ZREG(sst, vq, i);
527 *p = arm64_cpu_to_le128(fst->vregs[i]);
528 }
Dave Martin43d4da2c42017-10-31 15:51:13 +0000529}
530
Dave Martin7582e222017-10-31 15:51:08 +0000531int sve_set_vector_length(struct task_struct *task,
532 unsigned long vl, unsigned long flags)
533{
534 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
535 PR_SVE_SET_VL_ONEXEC))
536 return -EINVAL;
537
538 if (!sve_vl_valid(vl))
539 return -EINVAL;
540
541 /*
542 * Clamp to the maximum vector length that VL-agnostic SVE code can
543 * work with. A flag may be assigned in the future to allow setting
544 * of larger vector lengths without confusing older software.
545 */
546 if (vl > SVE_VL_ARCH_MAX)
547 vl = SVE_VL_ARCH_MAX;
548
549 vl = find_supported_vector_length(vl);
550
551 if (flags & (PR_SVE_VL_INHERIT |
552 PR_SVE_SET_VL_ONEXEC))
553 task->thread.sve_vl_onexec = vl;
554 else
555 /* Reset VL to system default on next exec: */
556 task->thread.sve_vl_onexec = 0;
557
558 /* Only actually set the VL if not deferred: */
559 if (flags & PR_SVE_SET_VL_ONEXEC)
560 goto out;
561
562 if (vl == task->thread.sve_vl)
563 goto out;
564
565 /*
566 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
567 * write any live register state back to task_struct, and convert to a
568 * non-SVE thread.
569 */
570 if (task == current) {
571 local_bh_disable();
572
Dave Martind1797612018-04-06 14:55:59 +0100573 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000574 }
575
576 fpsimd_flush_task_state(task);
577 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
578 sve_to_fpsimd(task);
579
580 if (task == current)
581 local_bh_enable();
582
583 /*
584 * Force reallocation of task SVE state to the correct size
585 * on next use:
586 */
587 sve_free(task);
588
589 task->thread.sve_vl = vl;
590
591out:
Dave Martin09d12232018-04-11 17:59:06 +0100592 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
593 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000594
595 return 0;
596}
597
Dave Martinbc0ee472017-10-31 15:51:05 +0000598/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000599 * Encode the current vector length and flags for return.
600 * This is only required for prctl(): ptrace has separate fields
601 *
602 * flags are as for sve_set_vector_length().
603 */
604static int sve_prctl_status(unsigned long flags)
605{
606 int ret;
607
608 if (flags & PR_SVE_SET_VL_ONEXEC)
609 ret = current->thread.sve_vl_onexec;
610 else
611 ret = current->thread.sve_vl;
612
613 if (test_thread_flag(TIF_SVE_VL_INHERIT))
614 ret |= PR_SVE_VL_INHERIT;
615
616 return ret;
617}
618
619/* PR_SVE_SET_VL */
620int sve_set_current_vl(unsigned long arg)
621{
622 unsigned long vl, flags;
623 int ret;
624
625 vl = arg & PR_SVE_VL_LEN_MASK;
626 flags = arg & ~vl;
627
628 if (!system_supports_sve())
629 return -EINVAL;
630
631 ret = sve_set_vector_length(current, vl, flags);
632 if (ret)
633 return ret;
634
635 return sve_prctl_status(flags);
636}
637
638/* PR_SVE_GET_VL */
639int sve_get_current_vl(void)
640{
641 if (!system_supports_sve())
642 return -EINVAL;
643
644 return sve_prctl_status(0);
645}
646
Dave Martin2e0f2472017-10-31 15:51:10 +0000647static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
648{
649 unsigned int vq, vl;
650 unsigned long zcr;
651
652 bitmap_zero(map, SVE_VQ_MAX);
653
654 zcr = ZCR_ELx_LEN_MASK;
655 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
656
657 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
658 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
659 vl = sve_get_vl();
660 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
Dave Martinead9e432018-09-28 14:39:21 +0100661 set_bit(__vq_to_bit(vq), map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000662 }
663}
664
Dave Martin8b08e842018-12-06 16:32:35 +0000665/*
666 * Initialise the set of known supported VQs for the boot CPU.
667 * This is called during kernel boot, before secondary CPUs are brought up.
668 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000669void __init sve_init_vq_map(void)
670{
671 sve_probe_vqs(sve_vq_map);
Dave Martind06b76b2018-09-28 14:39:10 +0100672 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000673}
674
675/*
676 * If we haven't committed to the set of supported VQs yet, filter out
677 * those not supported by the current CPU.
Dave Martin8b08e842018-12-06 16:32:35 +0000678 * This function is called during the bring-up of early secondary CPUs only.
Dave Martin2e0f2472017-10-31 15:51:10 +0000679 */
680void sve_update_vq_map(void)
681{
Dave Martind06b76b2018-09-28 14:39:10 +0100682 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
683
684 sve_probe_vqs(tmp_map);
685 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
686 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
Dave Martin2e0f2472017-10-31 15:51:10 +0000687}
688
Dave Martin8b08e842018-12-06 16:32:35 +0000689/*
690 * Check whether the current CPU supports all VQs in the committed set.
691 * This function is called during the bring-up of late secondary CPUs only.
692 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000693int sve_verify_vq_map(void)
694{
Dave Martind06b76b2018-09-28 14:39:10 +0100695 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
696 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000697
Dave Martind06b76b2018-09-28 14:39:10 +0100698 sve_probe_vqs(tmp_map);
699
700 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
701 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
Dave Martin2e0f2472017-10-31 15:51:10 +0000702 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
703 smp_processor_id());
Dave Martind06b76b2018-09-28 14:39:10 +0100704 return -EINVAL;
Dave Martin2e0f2472017-10-31 15:51:10 +0000705 }
706
Dave Martind06b76b2018-09-28 14:39:10 +0100707 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
708 return 0;
709
710 /*
711 * For KVM, it is necessary to ensure that this CPU doesn't
712 * support any vector length that guests may have probed as
713 * unsupported.
714 */
715
716 /* Recover the set of supported VQs: */
717 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
718 /* Find VQs supported that are not globally supported: */
719 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
720
721 /* Find the lowest such VQ, if any: */
722 b = find_last_bit(tmp_map, SVE_VQ_MAX);
723 if (b >= SVE_VQ_MAX)
724 return 0; /* no mismatches */
725
726 /*
727 * Mismatches above sve_max_virtualisable_vl are fine, since
728 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
729 */
Dave Martinead9e432018-09-28 14:39:21 +0100730 if (sve_vl_from_vq(__bit_to_vq(b)) <= sve_max_virtualisable_vl) {
Dave Martind06b76b2018-09-28 14:39:10 +0100731 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
732 smp_processor_id());
733 return -EINVAL;
734 }
735
736 return 0;
Dave Martin2e0f2472017-10-31 15:51:10 +0000737}
738
Dave Martinfdfa9762017-10-31 15:51:12 +0000739static void __init sve_efi_setup(void)
740{
741 if (!IS_ENABLED(CONFIG_EFI))
742 return;
743
744 /*
745 * alloc_percpu() warns and prints a backtrace if this goes wrong.
746 * This is evidence of a crippled system and we are returning void,
747 * so no attempt is made to handle this situation here.
748 */
749 if (!sve_vl_valid(sve_max_vl))
750 goto fail;
751
752 efi_sve_state = __alloc_percpu(
753 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
754 if (!efi_sve_state)
755 goto fail;
756
757 return;
758
759fail:
760 panic("Cannot allocate percpu memory for EFI SVE save/restore");
761}
762
Dave Martin2e0f2472017-10-31 15:51:10 +0000763/*
764 * Enable SVE for EL1.
765 * Intended for use by the cpufeatures code during CPU boot.
766 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100767void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000768{
769 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
770 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000771}
772
Dave Martin31dc52b2018-04-12 16:47:20 +0100773/*
774 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
775 * vector length.
776 *
777 * Use only if SVE is present.
778 * This function clobbers the SVE vector length.
779 */
780u64 read_zcr_features(void)
781{
782 u64 zcr;
783 unsigned int vq_max;
784
785 /*
786 * Set the maximum possible VL, and write zeroes to all other
787 * bits to see if they stick.
788 */
789 sve_kernel_enable(NULL);
790 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
791
792 zcr = read_sysreg_s(SYS_ZCR_EL1);
793 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
794 vq_max = sve_vq_from_vl(sve_get_vl());
795 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
796
797 return zcr;
798}
799
Dave Martin2e0f2472017-10-31 15:51:10 +0000800void __init sve_setup(void)
801{
802 u64 zcr;
Dave Martind06b76b2018-09-28 14:39:10 +0100803 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
804 unsigned long b;
Dave Martin2e0f2472017-10-31 15:51:10 +0000805
806 if (!system_supports_sve())
807 return;
808
809 /*
810 * The SVE architecture mandates support for 128-bit vectors,
811 * so sve_vq_map must have at least SVE_VQ_MIN set.
812 * If something went wrong, at least try to patch it up:
813 */
Dave Martinead9e432018-09-28 14:39:21 +0100814 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
815 set_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map);
Dave Martin2e0f2472017-10-31 15:51:10 +0000816
817 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
818 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
819
820 /*
821 * Sanity-check that the max VL we determined through CPU features
822 * corresponds properly to sve_vq_map. If not, do our best:
823 */
824 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
825 sve_max_vl = find_supported_vector_length(sve_max_vl);
826
827 /*
828 * For the default VL, pick the maximum supported value <= 64.
829 * VL == 64 is guaranteed not to grow the signal frame.
830 */
831 sve_default_vl = find_supported_vector_length(64);
832
Dave Martind06b76b2018-09-28 14:39:10 +0100833 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
834 SVE_VQ_MAX);
835
836 b = find_last_bit(tmp_map, SVE_VQ_MAX);
837 if (b >= SVE_VQ_MAX)
838 /* No non-virtualisable VLs found */
839 sve_max_virtualisable_vl = SVE_VQ_MAX;
840 else if (WARN_ON(b == SVE_VQ_MAX - 1))
841 /* No virtualisable VLs? This is architecturally forbidden. */
842 sve_max_virtualisable_vl = SVE_VQ_MIN;
843 else /* b + 1 < SVE_VQ_MAX */
Dave Martinead9e432018-09-28 14:39:21 +0100844 sve_max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
Dave Martind06b76b2018-09-28 14:39:10 +0100845
846 if (sve_max_virtualisable_vl > sve_max_vl)
847 sve_max_virtualisable_vl = sve_max_vl;
848
Dave Martin2e0f2472017-10-31 15:51:10 +0000849 pr_info("SVE: maximum available vector length %u bytes per vector\n",
850 sve_max_vl);
851 pr_info("SVE: default vector length %u bytes per vector\n",
852 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000853
Dave Martind06b76b2018-09-28 14:39:10 +0100854 /* KVM decides whether to support mismatched systems. Just warn here: */
855 if (sve_max_virtualisable_vl < sve_max_vl)
856 pr_warn("SVE: unvirtualisable vector lengths present\n");
857
Dave Martinfdfa9762017-10-31 15:51:12 +0000858 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000859}
860
861/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000862 * Called from the put_task_struct() path, which cannot get here
863 * unless dead_task is really dead and not schedulable.
864 */
865void fpsimd_release_task(struct task_struct *dead_task)
866{
867 __sve_free(dead_task);
868}
869
870#endif /* CONFIG_ARM64_SVE */
871
872/*
873 * Trapped SVE access
874 *
875 * Storage is allocated for the full SVE state, the current FPSIMD
876 * register contents are migrated across, and TIF_SVE is set so that
877 * the SVE access trap will be disabled the next time this task
878 * reaches ret_to_user.
879 *
880 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
881 * would have disabled the SVE access trap for userspace during
882 * ret_to_user, making an SVE access trap impossible in that case.
883 */
884asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
885{
886 /* Even if we chose not to use SVE, the hardware could still trap: */
887 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000888 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000889 return;
890 }
891
892 sve_alloc(current);
893
894 local_bh_disable();
895
Dave Martind1797612018-04-06 14:55:59 +0100896 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000897
898 /* Force ret_to_user to reload the registers: */
899 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000900
Dave Martinefbc2022018-09-28 14:39:05 +0100901 fpsimd_to_sve(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000902 if (test_and_set_thread_flag(TIF_SVE))
903 WARN_ON(1); /* SVE access shouldn't have trapped */
904
905 local_bh_enable();
906}
907
Catalin Marinas53631b52012-03-05 11:49:32 +0000908/*
909 * Trapped FP/ASIMD access.
910 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000911asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000912{
913 /* TODO: implement lazy context saving/restoring */
914 WARN_ON(1);
915}
916
917/*
918 * Raise a SIGFPE for the current process.
919 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000920asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000921{
Dave Martinaf4a81b2018-03-01 17:44:07 +0000922 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000923
Dave Martinaf4a81b2018-03-01 17:44:07 +0000924 if (esr & ESR_ELx_FP_EXC_TFV) {
925 if (esr & FPEXC_IOF)
926 si_code = FPE_FLTINV;
927 else if (esr & FPEXC_DZF)
928 si_code = FPE_FLTDIV;
929 else if (esr & FPEXC_OFF)
930 si_code = FPE_FLTOVF;
931 else if (esr & FPEXC_UFF)
932 si_code = FPE_FLTUND;
933 else if (esr & FPEXC_IXF)
934 si_code = FPE_FLTRES;
935 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000936
Eric W. Biedermanc8526802018-04-16 13:47:06 -0500937 send_sig_fault(SIGFPE, si_code,
938 (void __user *)instruction_pointer(regs),
939 current);
Catalin Marinas53631b52012-03-05 11:49:32 +0000940}
941
942void fpsimd_thread_switch(struct task_struct *next)
943{
Dave Martindf3fb962018-05-21 19:08:15 +0100944 bool wrong_task, wrong_cpu;
945
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000946 if (!system_supports_fpsimd())
947 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200948
Dave Martindf3fb962018-05-21 19:08:15 +0100949 /* Save unsaved fpsimd state, if any: */
950 fpsimd_save();
951
Catalin Marinas53631b52012-03-05 11:49:32 +0000952 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100953 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
954 * state. For kernel threads, FPSIMD registers are never loaded
955 * and wrong_task and wrong_cpu will always be true.
Catalin Marinas53631b52012-03-05 11:49:32 +0000956 */
Dave Martindf3fb962018-05-21 19:08:15 +0100957 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100958 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100959 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100960
Dave Martindf3fb962018-05-21 19:08:15 +0100961 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
962 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000963}
964
965void fpsimd_flush_thread(void)
966{
Dave Martin7582e222017-10-31 15:51:08 +0000967 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000968
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000969 if (!system_supports_fpsimd())
970 return;
Dave Martincb84d112017-08-03 17:23:23 +0100971
972 local_bh_disable();
973
Dave Martinefbc2022018-09-28 14:39:05 +0100974 fpsimd_flush_task_state(current);
Dave Martin65896542018-03-28 10:50:49 +0100975 memset(&current->thread.uw.fpsimd_state, 0,
976 sizeof(current->thread.uw.fpsimd_state));
Dave Martinbc0ee472017-10-31 15:51:05 +0000977
978 if (system_supports_sve()) {
979 clear_thread_flag(TIF_SVE);
980 sve_free(current);
981
982 /*
983 * Reset the task vector length as required.
984 * This is where we ensure that all user tasks have a valid
985 * vector length configured: no kernel task can become a user
986 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000987 * By the time the first call to this function is made, all
988 * early hardware probing is complete, so sve_default_vl
989 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000990 * If a bug causes this to go wrong, we make some noise and
991 * try to fudge thread.sve_vl to a safe value here.
992 */
Dave Martin79ab0472017-10-31 15:51:06 +0000993 vl = current->thread.sve_vl_onexec ?
994 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000995
996 if (WARN_ON(!sve_vl_valid(vl)))
997 vl = SVE_VL_MIN;
998
Dave Martin7582e222017-10-31 15:51:08 +0000999 supported_vl = find_supported_vector_length(vl);
1000 if (WARN_ON(supported_vl != vl))
1001 vl = supported_vl;
1002
Dave Martinbc0ee472017-10-31 15:51:05 +00001003 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +00001004
1005 /*
1006 * If the task is not set to inherit, ensure that the vector
1007 * length will be reset by a subsequent exec:
1008 */
1009 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
1010 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +00001011 }
1012
Dave Martincb84d112017-08-03 17:23:23 +01001013 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +00001014}
1015
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001016/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001017 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1018 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001019 */
1020void fpsimd_preserve_current_state(void)
1021{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001022 if (!system_supports_fpsimd())
1023 return;
Dave Martincb84d112017-08-03 17:23:23 +01001024
1025 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +01001026 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001027 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001028}
1029
1030/*
Dave Martin8cd969d2017-10-31 15:51:07 +00001031 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +01001032 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +00001033 * the signal frame.
1034 */
1035void fpsimd_signal_preserve_current_state(void)
1036{
1037 fpsimd_preserve_current_state();
1038 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1039 sve_to_fpsimd(current);
1040}
1041
1042/*
Dave Martin8884b7b2017-12-06 16:45:46 +00001043 * Associate current's FPSIMD context with this cpu
1044 * Preemption must be disabled when calling this function.
1045 */
Dave Martine6b673b2018-04-06 14:55:59 +01001046void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +00001047{
Dave Martincb968af2017-12-06 16:45:47 +00001048 struct fpsimd_last_state_struct *last =
1049 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001050
Dave Martin65896542018-03-28 10:50:49 +01001051 last->st = &current->thread.uw.fpsimd_state;
Dave Martin04950672018-09-28 14:39:11 +01001052 last->sve_state = current->thread.sve_state;
1053 last->sve_vl = current->thread.sve_vl;
Dave Martin20b85472018-03-28 10:50:48 +01001054 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +01001055
1056 if (system_supports_sve()) {
1057 /* Toggle SVE trapping for userspace if needed */
1058 if (test_thread_flag(TIF_SVE))
1059 sve_user_enable();
1060 else
1061 sve_user_disable();
1062
1063 /* Serialised by exception return to user */
1064 }
Dave Martin8884b7b2017-12-06 16:45:46 +00001065}
1066
Dave Martin04950672018-09-28 14:39:11 +01001067void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1068 unsigned int sve_vl)
Dave Martine6b673b2018-04-06 14:55:59 +01001069{
1070 struct fpsimd_last_state_struct *last =
1071 this_cpu_ptr(&fpsimd_last_state);
1072
1073 WARN_ON(!in_softirq() && !irqs_disabled());
1074
1075 last->st = st;
Dave Martin04950672018-09-28 14:39:11 +01001076 last->sve_state = sve_state;
1077 last->sve_vl = sve_vl;
Dave Martin8884b7b2017-12-06 16:45:46 +00001078}
1079
1080/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001081 * Load the userland FPSIMD state of 'current' from memory, but only if the
1082 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1083 * state of 'current'
1084 */
1085void fpsimd_restore_current_state(void)
1086{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001087 if (!system_supports_fpsimd())
1088 return;
Dave Martincb84d112017-08-03 17:23:23 +01001089
1090 local_bh_disable();
1091
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001092 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001093 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001094 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001095 }
Dave Martincb84d112017-08-03 17:23:23 +01001096
1097 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001098}
1099
1100/*
1101 * Load an updated userland FPSIMD state for 'current' from memory and set the
1102 * flag that indicates that the FPSIMD register contents are the most recent
1103 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001104 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001105void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001106{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001107 if (!system_supports_fpsimd())
1108 return;
Dave Martincb84d112017-08-03 17:23:23 +01001109
1110 local_bh_disable();
1111
Dave Martin65896542018-03-28 10:50:49 +01001112 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001113 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001114 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001115
Dave Martin8cd969d2017-10-31 15:51:07 +00001116 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001117 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001118
Dave Martin0cff8e72018-05-09 14:27:41 +01001119 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001120
1121 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001122}
1123
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001124/*
1125 * Invalidate live CPU copies of task t's FPSIMD state
Dave Martinefbc2022018-09-28 14:39:05 +01001126 *
1127 * This function may be called with preemption enabled. The barrier()
1128 * ensures that the assignment to fpsimd_cpu is visible to any
1129 * preemption/softirq that could race with set_tsk_thread_flag(), so
1130 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1131 *
1132 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1133 * subsequent code.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001134 */
1135void fpsimd_flush_task_state(struct task_struct *t)
1136{
Dave Martin20b85472018-03-28 10:50:48 +01001137 t->thread.fpsimd_cpu = NR_CPUS;
Dave Martinefbc2022018-09-28 14:39:05 +01001138
1139 barrier();
1140 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1141
1142 barrier();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001143}
1144
Dave Martinefbc2022018-09-28 14:39:05 +01001145/*
1146 * Invalidate any task's FPSIMD state that is present on this cpu.
1147 * This function must be called with softirqs disabled.
1148 */
Dave Martine6b673b2018-04-06 14:55:59 +01001149void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001150{
Dave Martincb968af2017-12-06 16:45:47 +00001151 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001152 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001153}
1154
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001155#ifdef CONFIG_KERNEL_MODE_NEON
1156
Dave Martincb84d112017-08-03 17:23:23 +01001157DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001158EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001159
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001160/*
1161 * Kernel-side NEON support functions
1162 */
Dave Martincb84d112017-08-03 17:23:23 +01001163
1164/*
1165 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1166 * context
1167 *
1168 * Must not be called unless may_use_simd() returns true.
1169 * Task context in the FPSIMD registers is saved back to memory as necessary.
1170 *
1171 * A matching call to kernel_neon_end() must be made before returning from the
1172 * calling context.
1173 *
1174 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1175 * called.
1176 */
1177void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001178{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001179 if (WARN_ON(!system_supports_fpsimd()))
1180 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001181
Dave Martincb84d112017-08-03 17:23:23 +01001182 BUG_ON(!may_use_simd());
1183
1184 local_bh_disable();
1185
1186 __this_cpu_write(kernel_neon_busy, true);
1187
Dave Martindf3fb962018-05-21 19:08:15 +01001188 /* Save unsaved fpsimd state, if any: */
1189 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001190
1191 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001192 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001193
1194 preempt_disable();
1195
1196 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001197}
Dave Martincb84d112017-08-03 17:23:23 +01001198EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001199
Dave Martincb84d112017-08-03 17:23:23 +01001200/*
1201 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1202 *
1203 * Must be called from a context in which kernel_neon_begin() was previously
1204 * called, with no call to kernel_neon_end() in the meantime.
1205 *
1206 * The caller must not use the FPSIMD registers after this function is called,
1207 * unless kernel_neon_begin() is called again in the meantime.
1208 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001209void kernel_neon_end(void)
1210{
Dave Martincb84d112017-08-03 17:23:23 +01001211 bool busy;
1212
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001213 if (!system_supports_fpsimd())
1214 return;
Dave Martincb84d112017-08-03 17:23:23 +01001215
1216 busy = __this_cpu_xchg(kernel_neon_busy, false);
1217 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1218
1219 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001220}
1221EXPORT_SYMBOL(kernel_neon_end);
1222
Dave Martine580b8b2017-09-18 09:40:12 +01001223#ifdef CONFIG_EFI
1224
Dave Martin20b85472018-03-28 10:50:48 +01001225static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001226static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001227static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001228
1229/*
1230 * EFI runtime services support functions
1231 *
1232 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1233 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1234 * is always used rather than being an optional accelerator.
1235 *
1236 * These functions provide the necessary support for ensuring FPSIMD
1237 * save/restore in the contexts from which EFI is used.
1238 *
1239 * Do not use them for any other purpose -- if tempted to do so, you are
1240 * either doing something wrong or you need to propose some refactoring.
1241 */
1242
1243/*
1244 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1245 */
1246void __efi_fpsimd_begin(void)
1247{
1248 if (!system_supports_fpsimd())
1249 return;
1250
1251 WARN_ON(preemptible());
1252
Dave Martinfdfa9762017-10-31 15:51:12 +00001253 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001254 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001255 } else {
1256 /*
1257 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1258 * preserving:
1259 */
1260 if (system_supports_sve() && likely(efi_sve_state)) {
1261 char *sve_state = this_cpu_ptr(efi_sve_state);
1262
1263 __this_cpu_write(efi_sve_state_used, true);
1264
1265 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1266 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1267 } else {
1268 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1269 }
1270
Dave Martin4328825d2017-08-03 17:23:22 +01001271 __this_cpu_write(efi_fpsimd_state_used, true);
1272 }
1273}
1274
1275/*
1276 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1277 */
1278void __efi_fpsimd_end(void)
1279{
1280 if (!system_supports_fpsimd())
1281 return;
1282
Dave Martinfdfa9762017-10-31 15:51:12 +00001283 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001284 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001285 } else {
1286 if (system_supports_sve() &&
1287 likely(__this_cpu_read(efi_sve_state_used))) {
1288 char const *sve_state = this_cpu_ptr(efi_sve_state);
1289
1290 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1291 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1292 sve_vq_from_vl(sve_get_vl()) - 1);
1293
1294 __this_cpu_write(efi_sve_state_used, false);
1295 } else {
1296 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1297 }
1298 }
Dave Martin4328825d2017-08-03 17:23:22 +01001299}
1300
Dave Martine580b8b2017-09-18 09:40:12 +01001301#endif /* CONFIG_EFI */
1302
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001303#endif /* CONFIG_KERNEL_MODE_NEON */
1304
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001305#ifdef CONFIG_CPU_PM
1306static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1307 unsigned long cmd, void *v)
1308{
1309 switch (cmd) {
1310 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001311 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001312 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001313 break;
1314 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001315 break;
1316 case CPU_PM_ENTER_FAILED:
1317 default:
1318 return NOTIFY_DONE;
1319 }
1320 return NOTIFY_OK;
1321}
1322
1323static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1324 .notifier_call = fpsimd_cpu_pm_notifier,
1325};
1326
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001327static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001328{
1329 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1330}
1331
1332#else
1333static inline void fpsimd_pm_init(void) { }
1334#endif /* CONFIG_CPU_PM */
1335
Janet Liu32365e62015-06-11 12:02:45 +08001336#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001337static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001338{
Dave Martincb968af2017-12-06 16:45:47 +00001339 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001340 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001341}
1342
Janet Liu32365e62015-06-11 12:02:45 +08001343static inline void fpsimd_hotplug_init(void)
1344{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001345 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1346 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001347}
1348
1349#else
1350static inline void fpsimd_hotplug_init(void) { }
1351#endif
1352
Catalin Marinas53631b52012-03-05 11:49:32 +00001353/*
1354 * FP/SIMD support code initialisation.
1355 */
1356static int __init fpsimd_init(void)
1357{
Andrew Murrayaaba0982019-04-09 10:52:40 +01001358 if (cpu_have_named_feature(FP)) {
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001359 fpsimd_pm_init();
1360 fpsimd_hotplug_init();
1361 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001362 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001363 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001364
Andrew Murrayaaba0982019-04-09 10:52:40 +01001365 if (!cpu_have_named_feature(ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001366 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001367
Dave Martin4ffa09a2017-10-31 15:51:15 +00001368 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001369}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001370core_initcall(fpsimd_init);