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 | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 20 | #include <linux/bottom_half.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 21 | #include <linux/bug.h> |
| 22 | #include <linux/compat.h> |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 23 | #include <linux/cpu.h> |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 24 | #include <linux/cpu_pm.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 26 | #include <linux/linkage.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 27 | #include <linux/irqflags.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 28 | #include <linux/init.h> |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 29 | #include <linux/percpu.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 30 | #include <linux/preempt.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 31 | #include <linux/ptrace.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 33 | #include <linux/sched/task_stack.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 34 | #include <linux/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 35 | #include <linux/slab.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 36 | |
| 37 | #include <asm/fpsimd.h> |
| 38 | #include <asm/cputype.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 39 | #include <asm/simd.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 40 | #include <asm/sigcontext.h> |
| 41 | #include <asm/sysreg.h> |
| 42 | #include <asm/traps.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 43 | |
| 44 | #define FPEXC_IOF (1 << 0) |
| 45 | #define FPEXC_DZF (1 << 1) |
| 46 | #define FPEXC_OFF (1 << 2) |
| 47 | #define FPEXC_UFF (1 << 3) |
| 48 | #define FPEXC_IXF (1 << 4) |
| 49 | #define FPEXC_IDF (1 << 7) |
| 50 | |
| 51 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 52 | * (Note: in this discussion, statements about FPSIMD apply equally to SVE.) |
| 53 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 54 | * In order to reduce the number of times the FPSIMD state is needlessly saved |
| 55 | * and restored, we need to keep track of two things: |
| 56 | * (a) for each task, we need to remember which CPU was the last one to have |
| 57 | * the task's FPSIMD state loaded into its FPSIMD registers; |
| 58 | * (b) for each CPU, we need to remember which task's userland FPSIMD state has |
| 59 | * been loaded into its FPSIMD registers most recently, or whether it has |
| 60 | * been used to perform kernel mode NEON in the meantime. |
| 61 | * |
| 62 | * 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] | 63 | * 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] | 64 | * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the |
| 65 | * address of the userland FPSIMD state of the task that was loaded onto the CPU |
| 66 | * the most recently, or NULL if kernel mode NEON has been performed after that. |
| 67 | * |
| 68 | * With this in place, we no longer have to restore the next FPSIMD state right |
| 69 | * when switching between tasks. Instead, we can defer this check to userland |
| 70 | * resume, at which time we verify whether the CPU's fpsimd_last_state and the |
| 71 | * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we |
| 72 | * can omit the FPSIMD restore. |
| 73 | * |
| 74 | * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to |
| 75 | * indicate whether or not the userland FPSIMD state of the current task is |
| 76 | * present in the registers. The flag is set unless the FPSIMD registers of this |
| 77 | * CPU currently contain the most recent userland FPSIMD state of the current |
| 78 | * task. |
| 79 | * |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 80 | * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may |
| 81 | * save the task's FPSIMD context back to task_struct from softirq context. |
| 82 | * To prevent this from racing with the manipulation of the task's FPSIMD state |
| 83 | * from task context and thereby corrupting the state, it is necessary to |
| 84 | * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE |
| 85 | * flag with local_bh_disable() unless softirqs are already masked. |
| 86 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 87 | * For a certain task, the sequence may look something like this: |
| 88 | * - the task gets scheduled in; if both the task's fpsimd_state.cpu field |
| 89 | * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu |
| 90 | * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is |
| 91 | * cleared, otherwise it is set; |
| 92 | * |
| 93 | * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's |
| 94 | * userland FPSIMD state is copied from memory to the registers, the task's |
| 95 | * fpsimd_state.cpu field is set to the id of the current CPU, the current |
| 96 | * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the |
| 97 | * TIF_FOREIGN_FPSTATE flag is cleared; |
| 98 | * |
| 99 | * - the task executes an ordinary syscall; upon return to userland, the |
| 100 | * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is |
| 101 | * restored; |
| 102 | * |
| 103 | * - the task executes a syscall which executes some NEON instructions; this is |
| 104 | * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD |
| 105 | * register contents to memory, clears the fpsimd_last_state per-cpu variable |
| 106 | * and sets the TIF_FOREIGN_FPSTATE flag; |
| 107 | * |
| 108 | * - the task gets preempted after kernel_neon_end() is called; as we have not |
| 109 | * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so |
| 110 | * whatever is in the FPSIMD registers is not saved to memory, but discarded. |
| 111 | */ |
| 112 | static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state); |
| 113 | |
| 114 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 115 | * Call __sve_free() directly only if you know task can't be scheduled |
| 116 | * or preempted. |
| 117 | */ |
| 118 | static void __sve_free(struct task_struct *task) |
| 119 | { |
| 120 | kfree(task->thread.sve_state); |
| 121 | task->thread.sve_state = NULL; |
| 122 | } |
| 123 | |
| 124 | static void sve_free(struct task_struct *task) |
| 125 | { |
| 126 | WARN_ON(test_tsk_thread_flag(task, TIF_SVE)); |
| 127 | |
| 128 | __sve_free(task); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* Offset of FFR in the SVE register dump */ |
| 133 | static size_t sve_ffr_offset(int vl) |
| 134 | { |
| 135 | return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET; |
| 136 | } |
| 137 | |
| 138 | static void *sve_pffr(struct task_struct *task) |
| 139 | { |
| 140 | return (char *)task->thread.sve_state + |
| 141 | sve_ffr_offset(task->thread.sve_vl); |
| 142 | } |
| 143 | |
| 144 | static void change_cpacr(u64 val, u64 mask) |
| 145 | { |
| 146 | u64 cpacr = read_sysreg(CPACR_EL1); |
| 147 | u64 new = (cpacr & ~mask) | val; |
| 148 | |
| 149 | if (new != cpacr) |
| 150 | write_sysreg(new, CPACR_EL1); |
| 151 | } |
| 152 | |
| 153 | static void sve_user_disable(void) |
| 154 | { |
| 155 | change_cpacr(0, CPACR_EL1_ZEN_EL0EN); |
| 156 | } |
| 157 | |
| 158 | static void sve_user_enable(void) |
| 159 | { |
| 160 | change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * TIF_SVE controls whether a task can use SVE without trapping while |
| 165 | * in userspace, and also the way a task's FPSIMD/SVE state is stored |
| 166 | * in thread_struct. |
| 167 | * |
| 168 | * The kernel uses this flag to track whether a user task is actively |
| 169 | * using SVE, and therefore whether full SVE register state needs to |
| 170 | * be tracked. If not, the cheaper FPSIMD context handling code can |
| 171 | * be used instead of the more costly SVE equivalents. |
| 172 | * |
| 173 | * * TIF_SVE set: |
| 174 | * |
| 175 | * The task can execute SVE instructions while in userspace without |
| 176 | * trapping to the kernel. |
| 177 | * |
| 178 | * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the |
| 179 | * corresponding Zn), P0-P15 and FFR are encoded in in |
| 180 | * task->thread.sve_state, formatted appropriately for vector |
| 181 | * length task->thread.sve_vl. |
| 182 | * |
| 183 | * task->thread.sve_state must point to a valid buffer at least |
| 184 | * sve_state_size(task) bytes in size. |
| 185 | * |
| 186 | * During any syscall, the kernel may optionally clear TIF_SVE and |
| 187 | * discard the vector state except for the FPSIMD subset. |
| 188 | * |
| 189 | * * TIF_SVE clear: |
| 190 | * |
| 191 | * An attempt by the user task to execute an SVE instruction causes |
| 192 | * do_sve_acc() to be called, which does some preparation and then |
| 193 | * sets TIF_SVE. |
| 194 | * |
| 195 | * When stored, FPSIMD registers V0-V31 are encoded in |
| 196 | * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are |
| 197 | * logically zero but not stored anywhere; P0-P15 and FFR are not |
| 198 | * stored and have unspecified values from userspace's point of |
| 199 | * view. For hygiene purposes, the kernel zeroes them on next use, |
| 200 | * but userspace is discouraged from relying on this. |
| 201 | * |
| 202 | * task->thread.sve_state does not need to be non-NULL, valid or any |
| 203 | * particular size: it must not be dereferenced. |
| 204 | * |
| 205 | * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of |
| 206 | * whether TIF_SVE is clear or set, since these are not vector length |
| 207 | * dependent. |
| 208 | */ |
| 209 | |
| 210 | /* |
| 211 | * Update current's FPSIMD/SVE registers from thread_struct. |
| 212 | * |
| 213 | * This function should be called only when the FPSIMD/SVE state in |
| 214 | * thread_struct is known to be up to date, when preparing to enter |
| 215 | * userspace. |
| 216 | * |
| 217 | * Softirqs (and preemption) must be disabled. |
| 218 | */ |
| 219 | static void task_fpsimd_load(void) |
| 220 | { |
| 221 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 222 | |
| 223 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) |
| 224 | sve_load_state(sve_pffr(current), |
| 225 | ¤t->thread.fpsimd_state.fpsr, |
| 226 | sve_vq_from_vl(current->thread.sve_vl) - 1); |
| 227 | else |
| 228 | fpsimd_load_state(¤t->thread.fpsimd_state); |
| 229 | |
| 230 | if (system_supports_sve()) { |
| 231 | /* Toggle SVE trapping for userspace if needed */ |
| 232 | if (test_thread_flag(TIF_SVE)) |
| 233 | sve_user_enable(); |
| 234 | else |
| 235 | sve_user_disable(); |
| 236 | |
| 237 | /* Serialised by exception return to user */ |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Ensure current's FPSIMD/SVE storage in thread_struct is up to date |
| 243 | * with respect to the CPU registers. |
| 244 | * |
| 245 | * Softirqs (and preemption) must be disabled. |
| 246 | */ |
| 247 | static void task_fpsimd_save(void) |
| 248 | { |
| 249 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 250 | |
| 251 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 252 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) { |
| 253 | if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) { |
| 254 | /* |
| 255 | * Can't save the user regs, so current would |
| 256 | * re-enter user with corrupt state. |
| 257 | * There's no way to recover, so kill it: |
| 258 | */ |
| 259 | force_signal_inject( |
| 260 | SIGKILL, 0, current_pt_regs(), 0); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | sve_save_state(sve_pffr(current), |
| 265 | ¤t->thread.fpsimd_state.fpsr); |
| 266 | } else |
| 267 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \ |
| 272 | (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET)) |
| 273 | |
| 274 | /* |
| 275 | * Transfer the FPSIMD state in task->thread.fpsimd_state to |
| 276 | * task->thread.sve_state. |
| 277 | * |
| 278 | * Task can be a non-runnable task, or current. In the latter case, |
| 279 | * softirqs (and preemption) must be disabled. |
| 280 | * task->thread.sve_state must point to at least sve_state_size(task) |
| 281 | * bytes of allocated kernel memory. |
| 282 | * task->thread.fpsimd_state must be up to date before calling this function. |
| 283 | */ |
| 284 | static void fpsimd_to_sve(struct task_struct *task) |
| 285 | { |
| 286 | unsigned int vq; |
| 287 | void *sst = task->thread.sve_state; |
| 288 | struct fpsimd_state const *fst = &task->thread.fpsimd_state; |
| 289 | unsigned int i; |
| 290 | |
| 291 | if (!system_supports_sve()) |
| 292 | return; |
| 293 | |
| 294 | vq = sve_vq_from_vl(task->thread.sve_vl); |
| 295 | for (i = 0; i < 32; ++i) |
| 296 | memcpy(ZREG(sst, vq, i), &fst->vregs[i], |
| 297 | sizeof(fst->vregs[i])); |
| 298 | } |
| 299 | |
| 300 | #ifdef CONFIG_ARM64_SVE |
| 301 | |
| 302 | /* |
| 303 | * Return how many bytes of memory are required to store the full SVE |
| 304 | * state for task, given task's currently configured vector length. |
| 305 | */ |
| 306 | size_t sve_state_size(struct task_struct const *task) |
| 307 | { |
| 308 | return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl)); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Ensure that task->thread.sve_state is allocated and sufficiently large. |
| 313 | * |
| 314 | * This function should be used only in preparation for replacing |
| 315 | * task->thread.sve_state with new data. The memory is always zeroed |
| 316 | * here to prevent stale data from showing through: this is done in |
| 317 | * the interest of testability and predictability: except in the |
| 318 | * do_sve_acc() case, there is no ABI requirement to hide stale data |
| 319 | * written previously be task. |
| 320 | */ |
| 321 | void sve_alloc(struct task_struct *task) |
| 322 | { |
| 323 | if (task->thread.sve_state) { |
| 324 | memset(task->thread.sve_state, 0, sve_state_size(current)); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | /* This is a small allocation (maximum ~8KB) and Should Not Fail. */ |
| 329 | task->thread.sve_state = |
| 330 | kzalloc(sve_state_size(task), GFP_KERNEL); |
| 331 | |
| 332 | /* |
| 333 | * If future SVE revisions can have larger vectors though, |
| 334 | * this may cease to be true: |
| 335 | */ |
| 336 | BUG_ON(!task->thread.sve_state); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Called from the put_task_struct() path, which cannot get here |
| 341 | * unless dead_task is really dead and not schedulable. |
| 342 | */ |
| 343 | void fpsimd_release_task(struct task_struct *dead_task) |
| 344 | { |
| 345 | __sve_free(dead_task); |
| 346 | } |
| 347 | |
| 348 | #endif /* CONFIG_ARM64_SVE */ |
| 349 | |
| 350 | /* |
| 351 | * Trapped SVE access |
| 352 | * |
| 353 | * Storage is allocated for the full SVE state, the current FPSIMD |
| 354 | * register contents are migrated across, and TIF_SVE is set so that |
| 355 | * the SVE access trap will be disabled the next time this task |
| 356 | * reaches ret_to_user. |
| 357 | * |
| 358 | * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load() |
| 359 | * would have disabled the SVE access trap for userspace during |
| 360 | * ret_to_user, making an SVE access trap impossible in that case. |
| 361 | */ |
| 362 | asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs) |
| 363 | { |
| 364 | /* Even if we chose not to use SVE, the hardware could still trap: */ |
| 365 | if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) { |
| 366 | force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | sve_alloc(current); |
| 371 | |
| 372 | local_bh_disable(); |
| 373 | |
| 374 | task_fpsimd_save(); |
| 375 | fpsimd_to_sve(current); |
| 376 | |
| 377 | /* Force ret_to_user to reload the registers: */ |
| 378 | fpsimd_flush_task_state(current); |
| 379 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| 380 | |
| 381 | if (test_and_set_thread_flag(TIF_SVE)) |
| 382 | WARN_ON(1); /* SVE access shouldn't have trapped */ |
| 383 | |
| 384 | local_bh_enable(); |
| 385 | } |
| 386 | |
| 387 | /* |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 388 | * Trapped FP/ASIMD access. |
| 389 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 390 | asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 391 | { |
| 392 | /* TODO: implement lazy context saving/restoring */ |
| 393 | WARN_ON(1); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Raise a SIGFPE for the current process. |
| 398 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 399 | asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 400 | { |
| 401 | siginfo_t info; |
| 402 | unsigned int si_code = 0; |
| 403 | |
| 404 | if (esr & FPEXC_IOF) |
| 405 | si_code = FPE_FLTINV; |
| 406 | else if (esr & FPEXC_DZF) |
| 407 | si_code = FPE_FLTDIV; |
| 408 | else if (esr & FPEXC_OFF) |
| 409 | si_code = FPE_FLTOVF; |
| 410 | else if (esr & FPEXC_UFF) |
| 411 | si_code = FPE_FLTUND; |
| 412 | else if (esr & FPEXC_IXF) |
| 413 | si_code = FPE_FLTRES; |
| 414 | |
| 415 | memset(&info, 0, sizeof(info)); |
| 416 | info.si_signo = SIGFPE; |
| 417 | info.si_code = si_code; |
| 418 | info.si_addr = (void __user *)instruction_pointer(regs); |
| 419 | |
| 420 | send_sig_info(SIGFPE, &info, current); |
| 421 | } |
| 422 | |
| 423 | void fpsimd_thread_switch(struct task_struct *next) |
| 424 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 425 | if (!system_supports_fpsimd()) |
| 426 | return; |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 427 | /* |
| 428 | * Save the current FPSIMD state to memory, but only if whatever is in |
| 429 | * the registers is in fact the most recent userland FPSIMD state of |
| 430 | * 'current'. |
| 431 | */ |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 432 | if (current->mm) |
| 433 | task_fpsimd_save(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 434 | |
| 435 | if (next->mm) { |
| 436 | /* |
| 437 | * If we are switching to a task whose most recent userland |
| 438 | * FPSIMD state is already in the registers of *this* cpu, |
| 439 | * we can skip loading the state from memory. Otherwise, set |
| 440 | * the TIF_FOREIGN_FPSTATE flag so the state will be loaded |
| 441 | * upon the next return to userland. |
| 442 | */ |
| 443 | struct fpsimd_state *st = &next->thread.fpsimd_state; |
| 444 | |
| 445 | if (__this_cpu_read(fpsimd_last_state) == st |
| 446 | && st->cpu == smp_processor_id()) |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 447 | clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 448 | else |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 449 | set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 450 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void fpsimd_flush_thread(void) |
| 454 | { |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 455 | int vl; |
| 456 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 457 | if (!system_supports_fpsimd()) |
| 458 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 459 | |
| 460 | local_bh_disable(); |
| 461 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 462 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); |
Ard Biesheuvel | 674c242c | 2015-08-27 07:12:33 +0100 | [diff] [blame] | 463 | fpsimd_flush_task_state(current); |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 464 | |
| 465 | if (system_supports_sve()) { |
| 466 | clear_thread_flag(TIF_SVE); |
| 467 | sve_free(current); |
| 468 | |
| 469 | /* |
| 470 | * Reset the task vector length as required. |
| 471 | * This is where we ensure that all user tasks have a valid |
| 472 | * vector length configured: no kernel task can become a user |
| 473 | * task without an exec and hence a call to this function. |
| 474 | * If a bug causes this to go wrong, we make some noise and |
| 475 | * try to fudge thread.sve_vl to a safe value here. |
| 476 | */ |
| 477 | vl = current->thread.sve_vl; |
| 478 | |
| 479 | if (vl == 0) |
| 480 | vl = SVE_VL_MIN; |
| 481 | |
| 482 | if (WARN_ON(!sve_vl_valid(vl))) |
| 483 | vl = SVE_VL_MIN; |
| 484 | |
| 485 | current->thread.sve_vl = vl; |
| 486 | } |
| 487 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 488 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 489 | |
| 490 | local_bh_enable(); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 493 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 494 | * Save the userland FPSIMD state of 'current' to memory, but only if the state |
| 495 | * currently held in the registers does in fact belong to 'current' |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 496 | * |
| 497 | * Currently, SVE tasks can't exist, so just WARN in that case. |
| 498 | * Subsequent patches will add full SVE support here. |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 499 | */ |
| 500 | void fpsimd_preserve_current_state(void) |
| 501 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 502 | if (!system_supports_fpsimd()) |
| 503 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 504 | |
| 505 | local_bh_disable(); |
| 506 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 507 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 508 | fpsimd_save_state(¤t->thread.fpsimd_state); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 509 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 510 | WARN_ON_ONCE(test_and_clear_thread_flag(TIF_SVE)); |
| 511 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 512 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 516 | * Load the userland FPSIMD state of 'current' from memory, but only if the |
| 517 | * FPSIMD state already held in the registers is /not/ the most recent FPSIMD |
| 518 | * state of 'current' |
| 519 | */ |
| 520 | void fpsimd_restore_current_state(void) |
| 521 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 522 | if (!system_supports_fpsimd()) |
| 523 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 524 | |
| 525 | local_bh_disable(); |
| 526 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 527 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 528 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 529 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 530 | task_fpsimd_load(); |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 531 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 532 | st->cpu = smp_processor_id(); |
| 533 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 534 | |
| 535 | local_bh_enable(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Load an updated userland FPSIMD state for 'current' from memory and set the |
| 540 | * flag that indicates that the FPSIMD register contents are the most recent |
| 541 | * FPSIMD state of 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 542 | */ |
| 543 | void fpsimd_update_current_state(struct fpsimd_state *state) |
| 544 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 545 | if (!system_supports_fpsimd()) |
| 546 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 547 | |
| 548 | local_bh_disable(); |
| 549 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 550 | fpsimd_load_state(state); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 551 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 552 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 553 | |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 554 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 555 | st->cpu = smp_processor_id(); |
| 556 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 557 | |
| 558 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 561 | /* |
| 562 | * Invalidate live CPU copies of task t's FPSIMD state |
| 563 | */ |
| 564 | void fpsimd_flush_task_state(struct task_struct *t) |
| 565 | { |
| 566 | t->thread.fpsimd_state.cpu = NR_CPUS; |
| 567 | } |
| 568 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 569 | #ifdef CONFIG_KERNEL_MODE_NEON |
| 570 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 571 | DEFINE_PER_CPU(bool, kernel_neon_busy); |
Catalin Marinas | 11cefd5 | 2017-08-07 12:36:35 +0100 | [diff] [blame] | 572 | EXPORT_PER_CPU_SYMBOL(kernel_neon_busy); |
Ard Biesheuvel | 190f1ca | 2014-02-24 15:26:29 +0100 | [diff] [blame] | 573 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 574 | /* |
| 575 | * Kernel-side NEON support functions |
| 576 | */ |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling |
| 580 | * context |
| 581 | * |
| 582 | * Must not be called unless may_use_simd() returns true. |
| 583 | * Task context in the FPSIMD registers is saved back to memory as necessary. |
| 584 | * |
| 585 | * A matching call to kernel_neon_end() must be made before returning from the |
| 586 | * calling context. |
| 587 | * |
| 588 | * The caller may freely use the FPSIMD registers until kernel_neon_end() is |
| 589 | * called. |
| 590 | */ |
| 591 | void kernel_neon_begin(void) |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 592 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 593 | if (WARN_ON(!system_supports_fpsimd())) |
| 594 | return; |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 595 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 596 | BUG_ON(!may_use_simd()); |
| 597 | |
| 598 | local_bh_disable(); |
| 599 | |
| 600 | __this_cpu_write(kernel_neon_busy, true); |
| 601 | |
| 602 | /* Save unsaved task fpsimd state, if any: */ |
| 603 | if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 604 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 605 | |
| 606 | /* Invalidate any task state remaining in the fpsimd regs: */ |
| 607 | __this_cpu_write(fpsimd_last_state, NULL); |
| 608 | |
| 609 | preempt_disable(); |
| 610 | |
| 611 | local_bh_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 612 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 613 | EXPORT_SYMBOL(kernel_neon_begin); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 614 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 615 | /* |
| 616 | * kernel_neon_end(): give the CPU FPSIMD registers back to the current task |
| 617 | * |
| 618 | * Must be called from a context in which kernel_neon_begin() was previously |
| 619 | * called, with no call to kernel_neon_end() in the meantime. |
| 620 | * |
| 621 | * The caller must not use the FPSIMD registers after this function is called, |
| 622 | * unless kernel_neon_begin() is called again in the meantime. |
| 623 | */ |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 624 | void kernel_neon_end(void) |
| 625 | { |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 626 | bool busy; |
| 627 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 628 | if (!system_supports_fpsimd()) |
| 629 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 630 | |
| 631 | busy = __this_cpu_xchg(kernel_neon_busy, false); |
| 632 | WARN_ON(!busy); /* No matching kernel_neon_begin()? */ |
| 633 | |
| 634 | preempt_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 635 | } |
| 636 | EXPORT_SYMBOL(kernel_neon_end); |
| 637 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 638 | #ifdef CONFIG_EFI |
| 639 | |
Dave Martin | 3b66023 | 2017-08-18 14:53:47 +0100 | [diff] [blame] | 640 | static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state); |
| 641 | static DEFINE_PER_CPU(bool, efi_fpsimd_state_used); |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * EFI runtime services support functions |
| 645 | * |
| 646 | * The ABI for EFI runtime services allows EFI to use FPSIMD during the call. |
| 647 | * This means that for EFI (and only for EFI), we have to assume that FPSIMD |
| 648 | * is always used rather than being an optional accelerator. |
| 649 | * |
| 650 | * These functions provide the necessary support for ensuring FPSIMD |
| 651 | * save/restore in the contexts from which EFI is used. |
| 652 | * |
| 653 | * Do not use them for any other purpose -- if tempted to do so, you are |
| 654 | * either doing something wrong or you need to propose some refactoring. |
| 655 | */ |
| 656 | |
| 657 | /* |
| 658 | * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call |
| 659 | */ |
| 660 | void __efi_fpsimd_begin(void) |
| 661 | { |
| 662 | if (!system_supports_fpsimd()) |
| 663 | return; |
| 664 | |
| 665 | WARN_ON(preemptible()); |
| 666 | |
| 667 | if (may_use_simd()) |
| 668 | kernel_neon_begin(); |
| 669 | else { |
| 670 | fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 671 | __this_cpu_write(efi_fpsimd_state_used, true); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call |
| 677 | */ |
| 678 | void __efi_fpsimd_end(void) |
| 679 | { |
| 680 | if (!system_supports_fpsimd()) |
| 681 | return; |
| 682 | |
| 683 | if (__this_cpu_xchg(efi_fpsimd_state_used, false)) |
| 684 | fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 685 | else |
| 686 | kernel_neon_end(); |
| 687 | } |
| 688 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 689 | #endif /* CONFIG_EFI */ |
| 690 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 691 | #endif /* CONFIG_KERNEL_MODE_NEON */ |
| 692 | |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 693 | #ifdef CONFIG_CPU_PM |
| 694 | static int fpsimd_cpu_pm_notifier(struct notifier_block *self, |
| 695 | unsigned long cmd, void *v) |
| 696 | { |
| 697 | switch (cmd) { |
| 698 | case CPU_PM_ENTER: |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame^] | 699 | if (current->mm) |
| 700 | task_fpsimd_save(); |
Leo Yan | 7c68a9c | 2014-09-01 11:09:51 +0800 | [diff] [blame] | 701 | this_cpu_write(fpsimd_last_state, NULL); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 702 | break; |
| 703 | case CPU_PM_EXIT: |
| 704 | if (current->mm) |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 705 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 706 | break; |
| 707 | case CPU_PM_ENTER_FAILED: |
| 708 | default: |
| 709 | return NOTIFY_DONE; |
| 710 | } |
| 711 | return NOTIFY_OK; |
| 712 | } |
| 713 | |
| 714 | static struct notifier_block fpsimd_cpu_pm_notifier_block = { |
| 715 | .notifier_call = fpsimd_cpu_pm_notifier, |
| 716 | }; |
| 717 | |
Jisheng Zhang | a7c61a3 | 2015-11-20 17:59:10 +0800 | [diff] [blame] | 718 | static void __init fpsimd_pm_init(void) |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 719 | { |
| 720 | cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block); |
| 721 | } |
| 722 | |
| 723 | #else |
| 724 | static inline void fpsimd_pm_init(void) { } |
| 725 | #endif /* CONFIG_CPU_PM */ |
| 726 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 727 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 728 | static int fpsimd_cpu_dead(unsigned int cpu) |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 729 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 730 | per_cpu(fpsimd_last_state, cpu) = NULL; |
| 731 | return 0; |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 732 | } |
| 733 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 734 | static inline void fpsimd_hotplug_init(void) |
| 735 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 736 | cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead", |
| 737 | NULL, fpsimd_cpu_dead); |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | #else |
| 741 | static inline void fpsimd_hotplug_init(void) { } |
| 742 | #endif |
| 743 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 744 | /* |
| 745 | * FP/SIMD support code initialisation. |
| 746 | */ |
| 747 | static int __init fpsimd_init(void) |
| 748 | { |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 749 | if (elf_hwcap & HWCAP_FP) { |
| 750 | fpsimd_pm_init(); |
| 751 | fpsimd_hotplug_init(); |
| 752 | } else { |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 753 | pr_notice("Floating-point is not implemented\n"); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 754 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 755 | |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 756 | if (!(elf_hwcap & HWCAP_ASIMD)) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 757 | pr_notice("Advanced SIMD is not implemented\n"); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 758 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | late_initcall(fpsimd_init); |