Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 20 | #include <linux/bitmap.h> |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 21 | #include <linux/bottom_half.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 22 | #include <linux/bug.h> |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 23 | #include <linux/cache.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 24 | #include <linux/compat.h> |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 26 | #include <linux/cpu_pm.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 27 | #include <linux/kernel.h> |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 28 | #include <linux/linkage.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 29 | #include <linux/irqflags.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 30 | #include <linux/init.h> |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 31 | #include <linux/percpu.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 32 | #include <linux/preempt.h> |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 33 | #include <linux/prctl.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 34 | #include <linux/ptrace.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 35 | #include <linux/sched/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 36 | #include <linux/sched/task_stack.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 37 | #include <linux/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 38 | #include <linux/slab.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 39 | |
| 40 | #include <asm/fpsimd.h> |
| 41 | #include <asm/cputype.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 42 | #include <asm/simd.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 43 | #include <asm/sigcontext.h> |
| 44 | #include <asm/sysreg.h> |
| 45 | #include <asm/traps.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 46 | |
| 47 | #define FPEXC_IOF (1 << 0) |
| 48 | #define FPEXC_DZF (1 << 1) |
| 49 | #define FPEXC_OFF (1 << 2) |
| 50 | #define FPEXC_UFF (1 << 3) |
| 51 | #define FPEXC_IXF (1 << 4) |
| 52 | #define FPEXC_IDF (1 << 7) |
| 53 | |
| 54 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 55 | * (Note: in this discussion, statements about FPSIMD apply equally to SVE.) |
| 56 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 57 | * In order to reduce the number of times the FPSIMD state is needlessly saved |
| 58 | * and restored, we need to keep track of two things: |
| 59 | * (a) for each task, we need to remember which CPU was the last one to have |
| 60 | * the task's FPSIMD state loaded into its FPSIMD registers; |
| 61 | * (b) for each CPU, we need to remember which task's userland FPSIMD state has |
| 62 | * been loaded into its FPSIMD registers most recently, or whether it has |
| 63 | * been used to perform kernel mode NEON in the meantime. |
| 64 | * |
| 65 | * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to |
Adam Buchbinder | ef769e3 | 2016-02-24 09:52:41 -0800 | [diff] [blame] | 66 | * the id of the current CPU every time the state is loaded onto a CPU. For (b), |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 67 | * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the |
| 68 | * address of the userland FPSIMD state of the task that was loaded onto the CPU |
| 69 | * the most recently, or NULL if kernel mode NEON has been performed after that. |
| 70 | * |
| 71 | * With this in place, we no longer have to restore the next FPSIMD state right |
| 72 | * when switching between tasks. Instead, we can defer this check to userland |
| 73 | * resume, at which time we verify whether the CPU's fpsimd_last_state and the |
| 74 | * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we |
| 75 | * can omit the FPSIMD restore. |
| 76 | * |
| 77 | * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to |
| 78 | * indicate whether or not the userland FPSIMD state of the current task is |
| 79 | * present in the registers. The flag is set unless the FPSIMD registers of this |
| 80 | * CPU currently contain the most recent userland FPSIMD state of the current |
| 81 | * task. |
| 82 | * |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 83 | * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may |
| 84 | * save the task's FPSIMD context back to task_struct from softirq context. |
| 85 | * To prevent this from racing with the manipulation of the task's FPSIMD state |
| 86 | * from task context and thereby corrupting the state, it is necessary to |
| 87 | * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE |
| 88 | * flag with local_bh_disable() unless softirqs are already masked. |
| 89 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 90 | * For a certain task, the sequence may look something like this: |
| 91 | * - the task gets scheduled in; if both the task's fpsimd_state.cpu field |
| 92 | * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu |
| 93 | * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is |
| 94 | * cleared, otherwise it is set; |
| 95 | * |
| 96 | * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's |
| 97 | * userland FPSIMD state is copied from memory to the registers, the task's |
| 98 | * fpsimd_state.cpu field is set to the id of the current CPU, the current |
| 99 | * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the |
| 100 | * TIF_FOREIGN_FPSTATE flag is cleared; |
| 101 | * |
| 102 | * - the task executes an ordinary syscall; upon return to userland, the |
| 103 | * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is |
| 104 | * restored; |
| 105 | * |
| 106 | * - the task executes a syscall which executes some NEON instructions; this is |
| 107 | * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD |
| 108 | * register contents to memory, clears the fpsimd_last_state per-cpu variable |
| 109 | * and sets the TIF_FOREIGN_FPSTATE flag; |
| 110 | * |
| 111 | * - the task gets preempted after kernel_neon_end() is called; as we have not |
| 112 | * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so |
| 113 | * whatever is in the FPSIMD registers is not saved to memory, but discarded. |
| 114 | */ |
| 115 | static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state); |
| 116 | |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 117 | /* Default VL for tasks that don't set it explicitly: */ |
Dave Martin | 2e0f247 | 2017-10-31 15:51:10 +0000 | [diff] [blame^] | 118 | static int sve_default_vl = -1; |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 119 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_ARM64_SVE |
| 121 | |
| 122 | /* Maximum supported vector length across all CPUs (initially poisoned) */ |
| 123 | int __ro_after_init sve_max_vl = -1; |
| 124 | /* Set of available vector lengths, as vq_to_bit(vq): */ |
Dave Martin | 2e0f247 | 2017-10-31 15:51:10 +0000 | [diff] [blame^] | 125 | static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX); |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 126 | |
| 127 | #else /* ! CONFIG_ARM64_SVE */ |
| 128 | |
| 129 | /* Dummy declaration for code that will be optimised out: */ |
Dave Martin | 2e0f247 | 2017-10-31 15:51:10 +0000 | [diff] [blame^] | 130 | extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX); |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 131 | |
| 132 | #endif /* ! CONFIG_ARM64_SVE */ |
| 133 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 134 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 135 | * Call __sve_free() directly only if you know task can't be scheduled |
| 136 | * or preempted. |
| 137 | */ |
| 138 | static void __sve_free(struct task_struct *task) |
| 139 | { |
| 140 | kfree(task->thread.sve_state); |
| 141 | task->thread.sve_state = NULL; |
| 142 | } |
| 143 | |
| 144 | static void sve_free(struct task_struct *task) |
| 145 | { |
| 146 | WARN_ON(test_tsk_thread_flag(task, TIF_SVE)); |
| 147 | |
| 148 | __sve_free(task); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* Offset of FFR in the SVE register dump */ |
| 153 | static size_t sve_ffr_offset(int vl) |
| 154 | { |
| 155 | return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET; |
| 156 | } |
| 157 | |
| 158 | static void *sve_pffr(struct task_struct *task) |
| 159 | { |
| 160 | return (char *)task->thread.sve_state + |
| 161 | sve_ffr_offset(task->thread.sve_vl); |
| 162 | } |
| 163 | |
| 164 | static void change_cpacr(u64 val, u64 mask) |
| 165 | { |
| 166 | u64 cpacr = read_sysreg(CPACR_EL1); |
| 167 | u64 new = (cpacr & ~mask) | val; |
| 168 | |
| 169 | if (new != cpacr) |
| 170 | write_sysreg(new, CPACR_EL1); |
| 171 | } |
| 172 | |
| 173 | static void sve_user_disable(void) |
| 174 | { |
| 175 | change_cpacr(0, CPACR_EL1_ZEN_EL0EN); |
| 176 | } |
| 177 | |
| 178 | static void sve_user_enable(void) |
| 179 | { |
| 180 | change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * TIF_SVE controls whether a task can use SVE without trapping while |
| 185 | * in userspace, and also the way a task's FPSIMD/SVE state is stored |
| 186 | * in thread_struct. |
| 187 | * |
| 188 | * The kernel uses this flag to track whether a user task is actively |
| 189 | * using SVE, and therefore whether full SVE register state needs to |
| 190 | * be tracked. If not, the cheaper FPSIMD context handling code can |
| 191 | * be used instead of the more costly SVE equivalents. |
| 192 | * |
| 193 | * * TIF_SVE set: |
| 194 | * |
| 195 | * The task can execute SVE instructions while in userspace without |
| 196 | * trapping to the kernel. |
| 197 | * |
| 198 | * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the |
| 199 | * corresponding Zn), P0-P15 and FFR are encoded in in |
| 200 | * task->thread.sve_state, formatted appropriately for vector |
| 201 | * length task->thread.sve_vl. |
| 202 | * |
| 203 | * task->thread.sve_state must point to a valid buffer at least |
| 204 | * sve_state_size(task) bytes in size. |
| 205 | * |
| 206 | * During any syscall, the kernel may optionally clear TIF_SVE and |
| 207 | * discard the vector state except for the FPSIMD subset. |
| 208 | * |
| 209 | * * TIF_SVE clear: |
| 210 | * |
| 211 | * An attempt by the user task to execute an SVE instruction causes |
| 212 | * do_sve_acc() to be called, which does some preparation and then |
| 213 | * sets TIF_SVE. |
| 214 | * |
| 215 | * When stored, FPSIMD registers V0-V31 are encoded in |
| 216 | * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are |
| 217 | * logically zero but not stored anywhere; P0-P15 and FFR are not |
| 218 | * stored and have unspecified values from userspace's point of |
| 219 | * view. For hygiene purposes, the kernel zeroes them on next use, |
| 220 | * but userspace is discouraged from relying on this. |
| 221 | * |
| 222 | * task->thread.sve_state does not need to be non-NULL, valid or any |
| 223 | * particular size: it must not be dereferenced. |
| 224 | * |
| 225 | * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of |
| 226 | * whether TIF_SVE is clear or set, since these are not vector length |
| 227 | * dependent. |
| 228 | */ |
| 229 | |
| 230 | /* |
| 231 | * Update current's FPSIMD/SVE registers from thread_struct. |
| 232 | * |
| 233 | * This function should be called only when the FPSIMD/SVE state in |
| 234 | * thread_struct is known to be up to date, when preparing to enter |
| 235 | * userspace. |
| 236 | * |
| 237 | * Softirqs (and preemption) must be disabled. |
| 238 | */ |
| 239 | static void task_fpsimd_load(void) |
| 240 | { |
| 241 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 242 | |
| 243 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) |
| 244 | sve_load_state(sve_pffr(current), |
| 245 | ¤t->thread.fpsimd_state.fpsr, |
| 246 | sve_vq_from_vl(current->thread.sve_vl) - 1); |
| 247 | else |
| 248 | fpsimd_load_state(¤t->thread.fpsimd_state); |
| 249 | |
| 250 | if (system_supports_sve()) { |
| 251 | /* Toggle SVE trapping for userspace if needed */ |
| 252 | if (test_thread_flag(TIF_SVE)) |
| 253 | sve_user_enable(); |
| 254 | else |
| 255 | sve_user_disable(); |
| 256 | |
| 257 | /* Serialised by exception return to user */ |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Ensure current's FPSIMD/SVE storage in thread_struct is up to date |
| 263 | * with respect to the CPU registers. |
| 264 | * |
| 265 | * Softirqs (and preemption) must be disabled. |
| 266 | */ |
| 267 | static void task_fpsimd_save(void) |
| 268 | { |
| 269 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 270 | |
| 271 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 272 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) { |
| 273 | if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) { |
| 274 | /* |
| 275 | * Can't save the user regs, so current would |
| 276 | * re-enter user with corrupt state. |
| 277 | * There's no way to recover, so kill it: |
| 278 | */ |
| 279 | force_signal_inject( |
| 280 | SIGKILL, 0, current_pt_regs(), 0); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | sve_save_state(sve_pffr(current), |
| 285 | ¤t->thread.fpsimd_state.fpsr); |
| 286 | } else |
| 287 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 288 | } |
| 289 | } |
| 290 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 291 | /* |
| 292 | * Helpers to translate bit indices in sve_vq_map to VQ values (and |
| 293 | * vice versa). This allows find_next_bit() to be used to find the |
| 294 | * _maximum_ VQ not exceeding a certain value. |
| 295 | */ |
| 296 | |
| 297 | static unsigned int vq_to_bit(unsigned int vq) |
| 298 | { |
| 299 | return SVE_VQ_MAX - vq; |
| 300 | } |
| 301 | |
| 302 | static unsigned int bit_to_vq(unsigned int bit) |
| 303 | { |
| 304 | if (WARN_ON(bit >= SVE_VQ_MAX)) |
| 305 | bit = SVE_VQ_MAX - 1; |
| 306 | |
| 307 | return SVE_VQ_MAX - bit; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * All vector length selection from userspace comes through here. |
| 312 | * We're on a slow path, so some sanity-checks are included. |
| 313 | * If things go wrong there's a bug somewhere, but try to fall back to a |
| 314 | * safe choice. |
| 315 | */ |
| 316 | static unsigned int find_supported_vector_length(unsigned int vl) |
| 317 | { |
| 318 | int bit; |
| 319 | int max_vl = sve_max_vl; |
| 320 | |
| 321 | if (WARN_ON(!sve_vl_valid(vl))) |
| 322 | vl = SVE_VL_MIN; |
| 323 | |
| 324 | if (WARN_ON(!sve_vl_valid(max_vl))) |
| 325 | max_vl = SVE_VL_MIN; |
| 326 | |
| 327 | if (vl > max_vl) |
| 328 | vl = max_vl; |
| 329 | |
| 330 | bit = find_next_bit(sve_vq_map, SVE_VQ_MAX, |
| 331 | vq_to_bit(sve_vq_from_vl(vl))); |
| 332 | return sve_vl_from_vq(bit_to_vq(bit)); |
| 333 | } |
| 334 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 335 | #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \ |
| 336 | (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET)) |
| 337 | |
| 338 | /* |
| 339 | * Transfer the FPSIMD state in task->thread.fpsimd_state to |
| 340 | * task->thread.sve_state. |
| 341 | * |
| 342 | * Task can be a non-runnable task, or current. In the latter case, |
| 343 | * softirqs (and preemption) must be disabled. |
| 344 | * task->thread.sve_state must point to at least sve_state_size(task) |
| 345 | * bytes of allocated kernel memory. |
| 346 | * task->thread.fpsimd_state must be up to date before calling this function. |
| 347 | */ |
| 348 | static void fpsimd_to_sve(struct task_struct *task) |
| 349 | { |
| 350 | unsigned int vq; |
| 351 | void *sst = task->thread.sve_state; |
| 352 | struct fpsimd_state const *fst = &task->thread.fpsimd_state; |
| 353 | unsigned int i; |
| 354 | |
| 355 | if (!system_supports_sve()) |
| 356 | return; |
| 357 | |
| 358 | vq = sve_vq_from_vl(task->thread.sve_vl); |
| 359 | for (i = 0; i < 32; ++i) |
| 360 | memcpy(ZREG(sst, vq, i), &fst->vregs[i], |
| 361 | sizeof(fst->vregs[i])); |
| 362 | } |
| 363 | |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 364 | /* |
| 365 | * Transfer the SVE state in task->thread.sve_state to |
| 366 | * task->thread.fpsimd_state. |
| 367 | * |
| 368 | * Task can be a non-runnable task, or current. In the latter case, |
| 369 | * softirqs (and preemption) must be disabled. |
| 370 | * task->thread.sve_state must point to at least sve_state_size(task) |
| 371 | * bytes of allocated kernel memory. |
| 372 | * task->thread.sve_state must be up to date before calling this function. |
| 373 | */ |
| 374 | static void sve_to_fpsimd(struct task_struct *task) |
| 375 | { |
| 376 | unsigned int vq; |
| 377 | void const *sst = task->thread.sve_state; |
| 378 | struct fpsimd_state *fst = &task->thread.fpsimd_state; |
| 379 | unsigned int i; |
| 380 | |
| 381 | if (!system_supports_sve()) |
| 382 | return; |
| 383 | |
| 384 | vq = sve_vq_from_vl(task->thread.sve_vl); |
| 385 | for (i = 0; i < 32; ++i) |
| 386 | memcpy(&fst->vregs[i], ZREG(sst, vq, i), |
| 387 | sizeof(fst->vregs[i])); |
| 388 | } |
| 389 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 390 | #ifdef CONFIG_ARM64_SVE |
| 391 | |
| 392 | /* |
| 393 | * Return how many bytes of memory are required to store the full SVE |
| 394 | * state for task, given task's currently configured vector length. |
| 395 | */ |
| 396 | size_t sve_state_size(struct task_struct const *task) |
| 397 | { |
| 398 | return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl)); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Ensure that task->thread.sve_state is allocated and sufficiently large. |
| 403 | * |
| 404 | * This function should be used only in preparation for replacing |
| 405 | * task->thread.sve_state with new data. The memory is always zeroed |
| 406 | * here to prevent stale data from showing through: this is done in |
| 407 | * the interest of testability and predictability: except in the |
| 408 | * do_sve_acc() case, there is no ABI requirement to hide stale data |
| 409 | * written previously be task. |
| 410 | */ |
| 411 | void sve_alloc(struct task_struct *task) |
| 412 | { |
| 413 | if (task->thread.sve_state) { |
| 414 | memset(task->thread.sve_state, 0, sve_state_size(current)); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | /* This is a small allocation (maximum ~8KB) and Should Not Fail. */ |
| 419 | task->thread.sve_state = |
| 420 | kzalloc(sve_state_size(task), GFP_KERNEL); |
| 421 | |
| 422 | /* |
| 423 | * If future SVE revisions can have larger vectors though, |
| 424 | * this may cease to be true: |
| 425 | */ |
| 426 | BUG_ON(!task->thread.sve_state); |
| 427 | } |
| 428 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 429 | int sve_set_vector_length(struct task_struct *task, |
| 430 | unsigned long vl, unsigned long flags) |
| 431 | { |
| 432 | if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT | |
| 433 | PR_SVE_SET_VL_ONEXEC)) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | if (!sve_vl_valid(vl)) |
| 437 | return -EINVAL; |
| 438 | |
| 439 | /* |
| 440 | * Clamp to the maximum vector length that VL-agnostic SVE code can |
| 441 | * work with. A flag may be assigned in the future to allow setting |
| 442 | * of larger vector lengths without confusing older software. |
| 443 | */ |
| 444 | if (vl > SVE_VL_ARCH_MAX) |
| 445 | vl = SVE_VL_ARCH_MAX; |
| 446 | |
| 447 | vl = find_supported_vector_length(vl); |
| 448 | |
| 449 | if (flags & (PR_SVE_VL_INHERIT | |
| 450 | PR_SVE_SET_VL_ONEXEC)) |
| 451 | task->thread.sve_vl_onexec = vl; |
| 452 | else |
| 453 | /* Reset VL to system default on next exec: */ |
| 454 | task->thread.sve_vl_onexec = 0; |
| 455 | |
| 456 | /* Only actually set the VL if not deferred: */ |
| 457 | if (flags & PR_SVE_SET_VL_ONEXEC) |
| 458 | goto out; |
| 459 | |
| 460 | if (vl == task->thread.sve_vl) |
| 461 | goto out; |
| 462 | |
| 463 | /* |
| 464 | * To ensure the FPSIMD bits of the SVE vector registers are preserved, |
| 465 | * write any live register state back to task_struct, and convert to a |
| 466 | * non-SVE thread. |
| 467 | */ |
| 468 | if (task == current) { |
| 469 | local_bh_disable(); |
| 470 | |
| 471 | task_fpsimd_save(); |
| 472 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| 473 | } |
| 474 | |
| 475 | fpsimd_flush_task_state(task); |
| 476 | if (test_and_clear_tsk_thread_flag(task, TIF_SVE)) |
| 477 | sve_to_fpsimd(task); |
| 478 | |
| 479 | if (task == current) |
| 480 | local_bh_enable(); |
| 481 | |
| 482 | /* |
| 483 | * Force reallocation of task SVE state to the correct size |
| 484 | * on next use: |
| 485 | */ |
| 486 | sve_free(task); |
| 487 | |
| 488 | task->thread.sve_vl = vl; |
| 489 | |
| 490 | out: |
| 491 | if (flags & PR_SVE_VL_INHERIT) |
| 492 | set_tsk_thread_flag(task, TIF_SVE_VL_INHERIT); |
| 493 | else |
| 494 | clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT); |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 499 | /* |
Dave Martin | 2e0f247 | 2017-10-31 15:51:10 +0000 | [diff] [blame^] | 500 | * Bitmap for temporary storage of the per-CPU set of supported vector lengths |
| 501 | * during secondary boot. |
| 502 | */ |
| 503 | static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX); |
| 504 | |
| 505 | static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX)) |
| 506 | { |
| 507 | unsigned int vq, vl; |
| 508 | unsigned long zcr; |
| 509 | |
| 510 | bitmap_zero(map, SVE_VQ_MAX); |
| 511 | |
| 512 | zcr = ZCR_ELx_LEN_MASK; |
| 513 | zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr; |
| 514 | |
| 515 | for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) { |
| 516 | write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */ |
| 517 | vl = sve_get_vl(); |
| 518 | vq = sve_vq_from_vl(vl); /* skip intervening lengths */ |
| 519 | set_bit(vq_to_bit(vq), map); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void __init sve_init_vq_map(void) |
| 524 | { |
| 525 | sve_probe_vqs(sve_vq_map); |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * If we haven't committed to the set of supported VQs yet, filter out |
| 530 | * those not supported by the current CPU. |
| 531 | */ |
| 532 | void sve_update_vq_map(void) |
| 533 | { |
| 534 | sve_probe_vqs(sve_secondary_vq_map); |
| 535 | bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX); |
| 536 | } |
| 537 | |
| 538 | /* Check whether the current CPU supports all VQs in the committed set */ |
| 539 | int sve_verify_vq_map(void) |
| 540 | { |
| 541 | int ret = 0; |
| 542 | |
| 543 | sve_probe_vqs(sve_secondary_vq_map); |
| 544 | bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map, |
| 545 | SVE_VQ_MAX); |
| 546 | if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) { |
| 547 | pr_warn("SVE: cpu%d: Required vector length(s) missing\n", |
| 548 | smp_processor_id()); |
| 549 | ret = -EINVAL; |
| 550 | } |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Enable SVE for EL1. |
| 557 | * Intended for use by the cpufeatures code during CPU boot. |
| 558 | */ |
| 559 | int sve_kernel_enable(void *__always_unused p) |
| 560 | { |
| 561 | write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1); |
| 562 | isb(); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | void __init sve_setup(void) |
| 568 | { |
| 569 | u64 zcr; |
| 570 | |
| 571 | if (!system_supports_sve()) |
| 572 | return; |
| 573 | |
| 574 | /* |
| 575 | * The SVE architecture mandates support for 128-bit vectors, |
| 576 | * so sve_vq_map must have at least SVE_VQ_MIN set. |
| 577 | * If something went wrong, at least try to patch it up: |
| 578 | */ |
| 579 | if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map))) |
| 580 | set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map); |
| 581 | |
| 582 | zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1); |
| 583 | sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1); |
| 584 | |
| 585 | /* |
| 586 | * Sanity-check that the max VL we determined through CPU features |
| 587 | * corresponds properly to sve_vq_map. If not, do our best: |
| 588 | */ |
| 589 | if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl))) |
| 590 | sve_max_vl = find_supported_vector_length(sve_max_vl); |
| 591 | |
| 592 | /* |
| 593 | * For the default VL, pick the maximum supported value <= 64. |
| 594 | * VL == 64 is guaranteed not to grow the signal frame. |
| 595 | */ |
| 596 | sve_default_vl = find_supported_vector_length(64); |
| 597 | |
| 598 | pr_info("SVE: maximum available vector length %u bytes per vector\n", |
| 599 | sve_max_vl); |
| 600 | pr_info("SVE: default vector length %u bytes per vector\n", |
| 601 | sve_default_vl); |
| 602 | } |
| 603 | |
| 604 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 605 | * Called from the put_task_struct() path, which cannot get here |
| 606 | * unless dead_task is really dead and not schedulable. |
| 607 | */ |
| 608 | void fpsimd_release_task(struct task_struct *dead_task) |
| 609 | { |
| 610 | __sve_free(dead_task); |
| 611 | } |
| 612 | |
| 613 | #endif /* CONFIG_ARM64_SVE */ |
| 614 | |
| 615 | /* |
| 616 | * Trapped SVE access |
| 617 | * |
| 618 | * Storage is allocated for the full SVE state, the current FPSIMD |
| 619 | * register contents are migrated across, and TIF_SVE is set so that |
| 620 | * the SVE access trap will be disabled the next time this task |
| 621 | * reaches ret_to_user. |
| 622 | * |
| 623 | * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load() |
| 624 | * would have disabled the SVE access trap for userspace during |
| 625 | * ret_to_user, making an SVE access trap impossible in that case. |
| 626 | */ |
| 627 | asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs) |
| 628 | { |
| 629 | /* Even if we chose not to use SVE, the hardware could still trap: */ |
| 630 | if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) { |
| 631 | force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | sve_alloc(current); |
| 636 | |
| 637 | local_bh_disable(); |
| 638 | |
| 639 | task_fpsimd_save(); |
| 640 | fpsimd_to_sve(current); |
| 641 | |
| 642 | /* Force ret_to_user to reload the registers: */ |
| 643 | fpsimd_flush_task_state(current); |
| 644 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| 645 | |
| 646 | if (test_and_set_thread_flag(TIF_SVE)) |
| 647 | WARN_ON(1); /* SVE access shouldn't have trapped */ |
| 648 | |
| 649 | local_bh_enable(); |
| 650 | } |
| 651 | |
| 652 | /* |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 653 | * Trapped FP/ASIMD access. |
| 654 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 655 | asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 656 | { |
| 657 | /* TODO: implement lazy context saving/restoring */ |
| 658 | WARN_ON(1); |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Raise a SIGFPE for the current process. |
| 663 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 664 | asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 665 | { |
| 666 | siginfo_t info; |
| 667 | unsigned int si_code = 0; |
| 668 | |
| 669 | if (esr & FPEXC_IOF) |
| 670 | si_code = FPE_FLTINV; |
| 671 | else if (esr & FPEXC_DZF) |
| 672 | si_code = FPE_FLTDIV; |
| 673 | else if (esr & FPEXC_OFF) |
| 674 | si_code = FPE_FLTOVF; |
| 675 | else if (esr & FPEXC_UFF) |
| 676 | si_code = FPE_FLTUND; |
| 677 | else if (esr & FPEXC_IXF) |
| 678 | si_code = FPE_FLTRES; |
| 679 | |
| 680 | memset(&info, 0, sizeof(info)); |
| 681 | info.si_signo = SIGFPE; |
| 682 | info.si_code = si_code; |
| 683 | info.si_addr = (void __user *)instruction_pointer(regs); |
| 684 | |
| 685 | send_sig_info(SIGFPE, &info, current); |
| 686 | } |
| 687 | |
| 688 | void fpsimd_thread_switch(struct task_struct *next) |
| 689 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 690 | if (!system_supports_fpsimd()) |
| 691 | return; |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 692 | /* |
| 693 | * Save the current FPSIMD state to memory, but only if whatever is in |
| 694 | * the registers is in fact the most recent userland FPSIMD state of |
| 695 | * 'current'. |
| 696 | */ |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 697 | if (current->mm) |
| 698 | task_fpsimd_save(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 699 | |
| 700 | if (next->mm) { |
| 701 | /* |
| 702 | * If we are switching to a task whose most recent userland |
| 703 | * FPSIMD state is already in the registers of *this* cpu, |
| 704 | * we can skip loading the state from memory. Otherwise, set |
| 705 | * the TIF_FOREIGN_FPSTATE flag so the state will be loaded |
| 706 | * upon the next return to userland. |
| 707 | */ |
| 708 | struct fpsimd_state *st = &next->thread.fpsimd_state; |
| 709 | |
| 710 | if (__this_cpu_read(fpsimd_last_state) == st |
| 711 | && st->cpu == smp_processor_id()) |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 712 | clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 713 | else |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 714 | set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 715 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void fpsimd_flush_thread(void) |
| 719 | { |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 720 | int vl, supported_vl; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 721 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 722 | if (!system_supports_fpsimd()) |
| 723 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 724 | |
| 725 | local_bh_disable(); |
| 726 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 727 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); |
Ard Biesheuvel | 674c242c | 2015-08-27 07:12:33 +0100 | [diff] [blame] | 728 | fpsimd_flush_task_state(current); |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 729 | |
| 730 | if (system_supports_sve()) { |
| 731 | clear_thread_flag(TIF_SVE); |
| 732 | sve_free(current); |
| 733 | |
| 734 | /* |
| 735 | * Reset the task vector length as required. |
| 736 | * This is where we ensure that all user tasks have a valid |
| 737 | * vector length configured: no kernel task can become a user |
| 738 | * task without an exec and hence a call to this function. |
Dave Martin | 2e0f247 | 2017-10-31 15:51:10 +0000 | [diff] [blame^] | 739 | * By the time the first call to this function is made, all |
| 740 | * early hardware probing is complete, so sve_default_vl |
| 741 | * should be valid. |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 742 | * If a bug causes this to go wrong, we make some noise and |
| 743 | * try to fudge thread.sve_vl to a safe value here. |
| 744 | */ |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 745 | vl = current->thread.sve_vl_onexec ? |
| 746 | current->thread.sve_vl_onexec : sve_default_vl; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 747 | |
| 748 | if (WARN_ON(!sve_vl_valid(vl))) |
| 749 | vl = SVE_VL_MIN; |
| 750 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame] | 751 | supported_vl = find_supported_vector_length(vl); |
| 752 | if (WARN_ON(supported_vl != vl)) |
| 753 | vl = supported_vl; |
| 754 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 755 | current->thread.sve_vl = vl; |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * If the task is not set to inherit, ensure that the vector |
| 759 | * length will be reset by a subsequent exec: |
| 760 | */ |
| 761 | if (!test_thread_flag(TIF_SVE_VL_INHERIT)) |
| 762 | current->thread.sve_vl_onexec = 0; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 765 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 766 | |
| 767 | local_bh_enable(); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 770 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 771 | * Save the userland FPSIMD state of 'current' to memory, but only if the state |
| 772 | * currently held in the registers does in fact belong to 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 773 | */ |
| 774 | void fpsimd_preserve_current_state(void) |
| 775 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 776 | if (!system_supports_fpsimd()) |
| 777 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 778 | |
| 779 | local_bh_disable(); |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 780 | task_fpsimd_save(); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 781 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 785 | * Like fpsimd_preserve_current_state(), but ensure that |
| 786 | * current->thread.fpsimd_state is updated so that it can be copied to |
| 787 | * the signal frame. |
| 788 | */ |
| 789 | void fpsimd_signal_preserve_current_state(void) |
| 790 | { |
| 791 | fpsimd_preserve_current_state(); |
| 792 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) |
| 793 | sve_to_fpsimd(current); |
| 794 | } |
| 795 | |
| 796 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 797 | * Load the userland FPSIMD state of 'current' from memory, but only if the |
| 798 | * FPSIMD state already held in the registers is /not/ the most recent FPSIMD |
| 799 | * state of 'current' |
| 800 | */ |
| 801 | void fpsimd_restore_current_state(void) |
| 802 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 803 | if (!system_supports_fpsimd()) |
| 804 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 805 | |
| 806 | local_bh_disable(); |
| 807 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 808 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 809 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 810 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 811 | task_fpsimd_load(); |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 812 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 813 | st->cpu = smp_processor_id(); |
| 814 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 815 | |
| 816 | local_bh_enable(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Load an updated userland FPSIMD state for 'current' from memory and set the |
| 821 | * flag that indicates that the FPSIMD register contents are the most recent |
| 822 | * FPSIMD state of 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 823 | */ |
| 824 | void fpsimd_update_current_state(struct fpsimd_state *state) |
| 825 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 826 | if (!system_supports_fpsimd()) |
| 827 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 828 | |
| 829 | local_bh_disable(); |
| 830 | |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 831 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) { |
| 832 | current->thread.fpsimd_state = *state; |
| 833 | fpsimd_to_sve(current); |
| 834 | } |
| 835 | task_fpsimd_load(); |
| 836 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 837 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 838 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 839 | |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 840 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 841 | st->cpu = smp_processor_id(); |
| 842 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 843 | |
| 844 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 847 | /* |
| 848 | * Invalidate live CPU copies of task t's FPSIMD state |
| 849 | */ |
| 850 | void fpsimd_flush_task_state(struct task_struct *t) |
| 851 | { |
| 852 | t->thread.fpsimd_state.cpu = NR_CPUS; |
| 853 | } |
| 854 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 855 | #ifdef CONFIG_KERNEL_MODE_NEON |
| 856 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 857 | DEFINE_PER_CPU(bool, kernel_neon_busy); |
Catalin Marinas | 11cefd5 | 2017-08-07 12:36:35 +0100 | [diff] [blame] | 858 | EXPORT_PER_CPU_SYMBOL(kernel_neon_busy); |
Ard Biesheuvel | 190f1ca | 2014-02-24 15:26:29 +0100 | [diff] [blame] | 859 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 860 | /* |
| 861 | * Kernel-side NEON support functions |
| 862 | */ |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling |
| 866 | * context |
| 867 | * |
| 868 | * Must not be called unless may_use_simd() returns true. |
| 869 | * Task context in the FPSIMD registers is saved back to memory as necessary. |
| 870 | * |
| 871 | * A matching call to kernel_neon_end() must be made before returning from the |
| 872 | * calling context. |
| 873 | * |
| 874 | * The caller may freely use the FPSIMD registers until kernel_neon_end() is |
| 875 | * called. |
| 876 | */ |
| 877 | void kernel_neon_begin(void) |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 878 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 879 | if (WARN_ON(!system_supports_fpsimd())) |
| 880 | return; |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 881 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 882 | BUG_ON(!may_use_simd()); |
| 883 | |
| 884 | local_bh_disable(); |
| 885 | |
| 886 | __this_cpu_write(kernel_neon_busy, true); |
| 887 | |
| 888 | /* Save unsaved task fpsimd state, if any: */ |
| 889 | if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 890 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 891 | |
| 892 | /* Invalidate any task state remaining in the fpsimd regs: */ |
| 893 | __this_cpu_write(fpsimd_last_state, NULL); |
| 894 | |
| 895 | preempt_disable(); |
| 896 | |
| 897 | local_bh_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 898 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 899 | EXPORT_SYMBOL(kernel_neon_begin); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 900 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 901 | /* |
| 902 | * kernel_neon_end(): give the CPU FPSIMD registers back to the current task |
| 903 | * |
| 904 | * Must be called from a context in which kernel_neon_begin() was previously |
| 905 | * called, with no call to kernel_neon_end() in the meantime. |
| 906 | * |
| 907 | * The caller must not use the FPSIMD registers after this function is called, |
| 908 | * unless kernel_neon_begin() is called again in the meantime. |
| 909 | */ |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 910 | void kernel_neon_end(void) |
| 911 | { |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 912 | bool busy; |
| 913 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 914 | if (!system_supports_fpsimd()) |
| 915 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 916 | |
| 917 | busy = __this_cpu_xchg(kernel_neon_busy, false); |
| 918 | WARN_ON(!busy); /* No matching kernel_neon_begin()? */ |
| 919 | |
| 920 | preempt_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 921 | } |
| 922 | EXPORT_SYMBOL(kernel_neon_end); |
| 923 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 924 | #ifdef CONFIG_EFI |
| 925 | |
Dave Martin | 3b66023 | 2017-08-18 14:53:47 +0100 | [diff] [blame] | 926 | static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state); |
| 927 | static DEFINE_PER_CPU(bool, efi_fpsimd_state_used); |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * EFI runtime services support functions |
| 931 | * |
| 932 | * The ABI for EFI runtime services allows EFI to use FPSIMD during the call. |
| 933 | * This means that for EFI (and only for EFI), we have to assume that FPSIMD |
| 934 | * is always used rather than being an optional accelerator. |
| 935 | * |
| 936 | * These functions provide the necessary support for ensuring FPSIMD |
| 937 | * save/restore in the contexts from which EFI is used. |
| 938 | * |
| 939 | * Do not use them for any other purpose -- if tempted to do so, you are |
| 940 | * either doing something wrong or you need to propose some refactoring. |
| 941 | */ |
| 942 | |
| 943 | /* |
| 944 | * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call |
| 945 | */ |
| 946 | void __efi_fpsimd_begin(void) |
| 947 | { |
| 948 | if (!system_supports_fpsimd()) |
| 949 | return; |
| 950 | |
| 951 | WARN_ON(preemptible()); |
| 952 | |
| 953 | if (may_use_simd()) |
| 954 | kernel_neon_begin(); |
| 955 | else { |
| 956 | fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 957 | __this_cpu_write(efi_fpsimd_state_used, true); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call |
| 963 | */ |
| 964 | void __efi_fpsimd_end(void) |
| 965 | { |
| 966 | if (!system_supports_fpsimd()) |
| 967 | return; |
| 968 | |
| 969 | if (__this_cpu_xchg(efi_fpsimd_state_used, false)) |
| 970 | fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 971 | else |
| 972 | kernel_neon_end(); |
| 973 | } |
| 974 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 975 | #endif /* CONFIG_EFI */ |
| 976 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 977 | #endif /* CONFIG_KERNEL_MODE_NEON */ |
| 978 | |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 979 | #ifdef CONFIG_CPU_PM |
| 980 | static int fpsimd_cpu_pm_notifier(struct notifier_block *self, |
| 981 | unsigned long cmd, void *v) |
| 982 | { |
| 983 | switch (cmd) { |
| 984 | case CPU_PM_ENTER: |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 985 | if (current->mm) |
| 986 | task_fpsimd_save(); |
Leo Yan | 7c68a9c | 2014-09-01 11:09:51 +0800 | [diff] [blame] | 987 | this_cpu_write(fpsimd_last_state, NULL); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 988 | break; |
| 989 | case CPU_PM_EXIT: |
| 990 | if (current->mm) |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 991 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 992 | break; |
| 993 | case CPU_PM_ENTER_FAILED: |
| 994 | default: |
| 995 | return NOTIFY_DONE; |
| 996 | } |
| 997 | return NOTIFY_OK; |
| 998 | } |
| 999 | |
| 1000 | static struct notifier_block fpsimd_cpu_pm_notifier_block = { |
| 1001 | .notifier_call = fpsimd_cpu_pm_notifier, |
| 1002 | }; |
| 1003 | |
Jisheng Zhang | a7c61a3 | 2015-11-20 17:59:10 +0800 | [diff] [blame] | 1004 | static void __init fpsimd_pm_init(void) |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 1005 | { |
| 1006 | cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block); |
| 1007 | } |
| 1008 | |
| 1009 | #else |
| 1010 | static inline void fpsimd_pm_init(void) { } |
| 1011 | #endif /* CONFIG_CPU_PM */ |
| 1012 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 1013 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 1014 | static int fpsimd_cpu_dead(unsigned int cpu) |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 1015 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 1016 | per_cpu(fpsimd_last_state, cpu) = NULL; |
| 1017 | return 0; |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 1020 | static inline void fpsimd_hotplug_init(void) |
| 1021 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 1022 | cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead", |
| 1023 | NULL, fpsimd_cpu_dead); |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | #else |
| 1027 | static inline void fpsimd_hotplug_init(void) { } |
| 1028 | #endif |
| 1029 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1030 | /* |
| 1031 | * FP/SIMD support code initialisation. |
| 1032 | */ |
| 1033 | static int __init fpsimd_init(void) |
| 1034 | { |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 1035 | if (elf_hwcap & HWCAP_FP) { |
| 1036 | fpsimd_pm_init(); |
| 1037 | fpsimd_hotplug_init(); |
| 1038 | } else { |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1039 | pr_notice("Floating-point is not implemented\n"); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1040 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1041 | |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 1042 | if (!(elf_hwcap & HWCAP_ASIMD)) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1043 | pr_notice("Advanced SIMD is not implemented\n"); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 1044 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
| 1047 | late_initcall(fpsimd_init); |