Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1994 Linus Torvalds |
| 3 | * |
| 4 | * Pentium III FXSR, SSE support |
| 5 | * General FPU state handling cleanups |
| 6 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 7 | * x86-64 work by Andi Kleen 2002 |
| 8 | */ |
| 9 | |
| 10 | #ifndef _FPU_INTERNAL_H |
| 11 | #define _FPU_INTERNAL_H |
| 12 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 13 | #include <linux/regset.h> |
Suresh Siddha | 050902c | 2012-07-24 16:05:27 -0700 | [diff] [blame] | 14 | #include <linux/compat.h> |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Ingo Molnar | f89e32e | 2015-04-22 10:58:10 +0200 | [diff] [blame] | 16 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 17 | #include <asm/user.h> |
Ingo Molnar | f89e32e | 2015-04-22 10:58:10 +0200 | [diff] [blame] | 18 | #include <asm/i387.h> |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 19 | #include <asm/xsave.h> |
| 20 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 21 | #ifdef CONFIG_X86_64 |
| 22 | # include <asm/sigcontext32.h> |
| 23 | # include <asm/user32.h> |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 24 | struct ksignal; |
| 25 | int ia32_setup_rt_frame(int sig, struct ksignal *ksig, |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 26 | compat_sigset_t *set, struct pt_regs *regs); |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 27 | int ia32_setup_frame(int sig, struct ksignal *ksig, |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 28 | compat_sigset_t *set, struct pt_regs *regs); |
| 29 | #else |
| 30 | # define user_i387_ia32_struct user_i387_struct |
| 31 | # define user32_fxsr_struct user_fxsr_struct |
| 32 | # define ia32_setup_frame __setup_frame |
| 33 | # define ia32_setup_rt_frame __setup_rt_frame |
| 34 | #endif |
| 35 | |
| 36 | extern unsigned int mxcsr_feature_mask; |
Ingo Molnar | 3a9c4b0 | 2015-04-03 13:16:51 +0200 | [diff] [blame] | 37 | extern void fpu__cpu_init(void); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 38 | extern void eager_fpu_init(void); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 39 | |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 40 | DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 41 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 42 | extern void convert_from_fxsr(struct user_i387_ia32_struct *env, |
| 43 | struct task_struct *tsk); |
| 44 | extern void convert_to_fxsr(struct task_struct *tsk, |
| 45 | const struct user_i387_ia32_struct *env); |
| 46 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 47 | extern user_regset_active_fn fpregs_active, xfpregs_active; |
| 48 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, |
| 49 | xstateregs_get; |
| 50 | extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, |
| 51 | xstateregs_set; |
| 52 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 53 | /* |
| 54 | * xstateregs_active == fpregs_active. Please refer to the comment |
| 55 | * at the definition of fpregs_active. |
| 56 | */ |
| 57 | #define xstateregs_active fpregs_active |
| 58 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 59 | #ifdef CONFIG_MATH_EMULATION |
| 60 | extern void finit_soft_fpu(struct i387_soft_struct *soft); |
| 61 | #else |
| 62 | static inline void finit_soft_fpu(struct i387_soft_struct *soft) {} |
| 63 | #endif |
| 64 | |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 65 | /* |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 66 | * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx, |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 67 | * on this CPU. |
| 68 | * |
| 69 | * This will disable any lazy FPU state restore of the current FPU state, |
| 70 | * but if the current thread owns the FPU, it will still be saved by. |
| 71 | */ |
| 72 | static inline void __cpu_disable_lazy_restore(unsigned int cpu) |
| 73 | { |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 74 | per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL; |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Rik van Riel | 33e03de | 2015-02-06 15:02:02 -0500 | [diff] [blame] | 77 | /* |
| 78 | * Used to indicate that the FPU state in memory is newer than the FPU |
| 79 | * state in registers, and the FPU state should be reloaded next time the |
| 80 | * task is run. Only safe on the current task, or non-running tasks. |
| 81 | */ |
| 82 | static inline void task_disable_lazy_fpu_restore(struct task_struct *tsk) |
| 83 | { |
| 84 | tsk->thread.fpu.last_cpu = ~0; |
| 85 | } |
| 86 | |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 87 | static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu) |
| 88 | { |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 89 | return &new->thread.fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 90 | cpu == new->thread.fpu.last_cpu; |
| 91 | } |
| 92 | |
Suresh Siddha | 050902c | 2012-07-24 16:05:27 -0700 | [diff] [blame] | 93 | static inline int is_ia32_compat_frame(void) |
| 94 | { |
| 95 | return config_enabled(CONFIG_IA32_EMULATION) && |
| 96 | test_thread_flag(TIF_IA32); |
| 97 | } |
| 98 | |
| 99 | static inline int is_ia32_frame(void) |
| 100 | { |
| 101 | return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame(); |
| 102 | } |
| 103 | |
| 104 | static inline int is_x32_frame(void) |
| 105 | { |
| 106 | return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32); |
| 107 | } |
| 108 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 109 | #define X87_FSW_ES (1 << 7) /* Exception Summary */ |
| 110 | |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 111 | static __always_inline __pure bool use_eager_fpu(void) |
| 112 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 113 | return static_cpu_has_safe(X86_FEATURE_EAGER_FPU); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 116 | static __always_inline __pure bool use_xsaveopt(void) |
| 117 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 118 | return static_cpu_has_safe(X86_FEATURE_XSAVEOPT); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static __always_inline __pure bool use_xsave(void) |
| 122 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 123 | return static_cpu_has_safe(X86_FEATURE_XSAVE); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static __always_inline __pure bool use_fxsr(void) |
| 127 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 128 | return static_cpu_has_safe(X86_FEATURE_FXSR); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 131 | static inline void fx_finit(struct i387_fxsave_struct *fx) |
| 132 | { |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 133 | fx->cwd = 0x37f; |
Suresh Siddha | a8615af | 2012-09-10 10:40:08 -0700 | [diff] [blame] | 134 | fx->mxcsr = MXCSR_DEFAULT; |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 137 | extern void __sanitize_i387_state(struct task_struct *); |
| 138 | |
| 139 | static inline void sanitize_i387_state(struct task_struct *tsk) |
| 140 | { |
| 141 | if (!use_xsaveopt()) |
| 142 | return; |
| 143 | __sanitize_i387_state(tsk); |
| 144 | } |
| 145 | |
H. Peter Anvin | 49b8c695 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 146 | #define user_insn(insn, output, input...) \ |
| 147 | ({ \ |
| 148 | int err; \ |
| 149 | asm volatile(ASM_STAC "\n" \ |
| 150 | "1:" #insn "\n\t" \ |
| 151 | "2: " ASM_CLAC "\n" \ |
| 152 | ".section .fixup,\"ax\"\n" \ |
| 153 | "3: movl $-1,%[err]\n" \ |
| 154 | " jmp 2b\n" \ |
| 155 | ".previous\n" \ |
| 156 | _ASM_EXTABLE(1b, 3b) \ |
| 157 | : [err] "=r" (err), output \ |
| 158 | : "0"(0), input); \ |
| 159 | err; \ |
| 160 | }) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 161 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 162 | #define check_insn(insn, output, input...) \ |
| 163 | ({ \ |
| 164 | int err; \ |
| 165 | asm volatile("1:" #insn "\n\t" \ |
| 166 | "2:\n" \ |
| 167 | ".section .fixup,\"ax\"\n" \ |
| 168 | "3: movl $-1,%[err]\n" \ |
| 169 | " jmp 2b\n" \ |
| 170 | ".previous\n" \ |
| 171 | _ASM_EXTABLE(1b, 3b) \ |
| 172 | : [err] "=r" (err), output \ |
| 173 | : "0"(0), input); \ |
| 174 | err; \ |
| 175 | }) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 176 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 177 | static inline int fsave_user(struct i387_fsave_struct __user *fx) |
| 178 | { |
H. Peter Anvin | 49b8c695 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 179 | return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static inline int fxsave_user(struct i387_fxsave_struct __user *fx) |
| 183 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 184 | if (config_enabled(CONFIG_X86_32)) |
H. Peter Anvin | 49b8c695 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 185 | return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 186 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
H. Peter Anvin | 49b8c695 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 187 | return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 188 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 189 | /* See comment in fpu_fxsave() below. */ |
H. Peter Anvin | 49b8c695 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 190 | return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 193 | static inline int fxrstor_checking(struct i387_fxsave_struct *fx) |
| 194 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 195 | if (config_enabled(CONFIG_X86_32)) |
| 196 | return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 197 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
| 198 | return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 199 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 200 | /* See comment in fpu_fxsave() below. */ |
| 201 | return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), |
| 202 | "m" (*fx)); |
| 203 | } |
| 204 | |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 205 | static inline int fxrstor_user(struct i387_fxsave_struct __user *fx) |
| 206 | { |
| 207 | if (config_enabled(CONFIG_X86_32)) |
| 208 | return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 209 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
| 210 | return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 211 | |
| 212 | /* See comment in fpu_fxsave() below. */ |
| 213 | return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), |
| 214 | "m" (*fx)); |
| 215 | } |
| 216 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 217 | static inline int frstor_checking(struct i387_fsave_struct *fx) |
| 218 | { |
| 219 | return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 220 | } |
| 221 | |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 222 | static inline int frstor_user(struct i387_fsave_struct __user *fx) |
| 223 | { |
| 224 | return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 225 | } |
| 226 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 227 | static inline void fpu_fxsave(struct fpu *fpu) |
| 228 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 229 | if (config_enabled(CONFIG_X86_32)) |
| 230 | asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave)); |
| 231 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
Borislav Petkov | 6ca7a8a | 2014-12-21 15:02:23 +0100 | [diff] [blame] | 232 | asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 233 | else { |
| 234 | /* Using "rex64; fxsave %0" is broken because, if the memory |
| 235 | * operand uses any extended registers for addressing, a second |
| 236 | * REX prefix will be generated (to the assembler, rex64 |
| 237 | * followed by semicolon is a separate instruction), and hence |
| 238 | * the 64-bitness is lost. |
| 239 | * |
| 240 | * Using "fxsaveq %0" would be the ideal choice, but is only |
| 241 | * supported starting with gas 2.16. |
| 242 | * |
| 243 | * Using, as a workaround, the properly prefixed form below |
| 244 | * isn't accepted by any binutils version so far released, |
| 245 | * complaining that the same type of prefix is used twice if |
| 246 | * an extended register is needed for addressing (fix submitted |
| 247 | * to mainline 2005-11-21). |
| 248 | * |
| 249 | * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave)); |
| 250 | * |
| 251 | * This, however, we can work around by forcing the compiler to |
| 252 | * select an addressing mode that doesn't require extended |
| 253 | * registers. |
| 254 | */ |
| 255 | asm volatile( "rex64/fxsave (%[fx])" |
| 256 | : "=m" (fpu->state->fxsave) |
| 257 | : [fx] "R" (&fpu->state->fxsave)); |
| 258 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 261 | /* |
| 262 | * These must be called with preempt disabled. Returns |
| 263 | * 'true' if the FPU state is still intact. |
| 264 | */ |
| 265 | static inline int fpu_save_init(struct fpu *fpu) |
| 266 | { |
| 267 | if (use_xsave()) { |
Ingo Molnar | 0afc4a9 | 2015-04-22 15:14:44 +0200 | [diff] [blame] | 268 | xsave_state(&fpu->state->xsave); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * xsave header may indicate the init state of the FP. |
| 272 | */ |
| 273 | if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP)) |
| 274 | return 1; |
| 275 | } else if (use_fxsr()) { |
| 276 | fpu_fxsave(fpu); |
| 277 | } else { |
| 278 | asm volatile("fnsave %[fx]; fwait" |
| 279 | : [fx] "=m" (fpu->state->fsave)); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * If exceptions are pending, we need to clear them so |
| 285 | * that we don't randomly get exceptions later. |
| 286 | * |
| 287 | * FIXME! Is this perhaps only true for the old-style |
| 288 | * irq13 case? Maybe we could leave the x87 state |
| 289 | * intact otherwise? |
| 290 | */ |
| 291 | if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) { |
| 292 | asm volatile("fnclex"); |
| 293 | return 0; |
| 294 | } |
| 295 | return 1; |
| 296 | } |
| 297 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 298 | static inline int fpu_restore_checking(struct fpu *fpu) |
| 299 | { |
| 300 | if (use_xsave()) |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 301 | return fpu_xrstor_checking(&fpu->state->xsave); |
| 302 | else if (use_fxsr()) |
| 303 | return fxrstor_checking(&fpu->state->fxsave); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 304 | else |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 305 | return frstor_checking(&fpu->state->fsave); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static inline int restore_fpu_checking(struct task_struct *tsk) |
| 309 | { |
Borislav Petkov | 6ca7a8a | 2014-12-21 15:02:23 +0100 | [diff] [blame] | 310 | /* |
| 311 | * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is |
| 312 | * pending. Clear the x87 state here by setting it to fixed values. |
| 313 | * "m" is a random variable that should be in L1. |
| 314 | */ |
Borislav Petkov | 9b13a93 | 2014-06-18 00:06:23 +0200 | [diff] [blame] | 315 | if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) { |
Linus Torvalds | 26bef13 | 2014-01-11 19:15:52 -0800 | [diff] [blame] | 316 | asm volatile( |
| 317 | "fnclex\n\t" |
| 318 | "emms\n\t" |
| 319 | "fildl %P[addr]" /* set F?P to defined value */ |
| 320 | : : [addr] "m" (tsk->thread.fpu.has_fpu)); |
| 321 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 322 | |
| 323 | return fpu_restore_checking(&tsk->thread.fpu); |
| 324 | } |
| 325 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 326 | /* Must be paired with an 'stts' after! */ |
Ingo Molnar | 36fe617 | 2015-04-23 12:08:58 +0200 | [diff] [blame] | 327 | static inline void __thread_clear_has_fpu(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 328 | { |
Ingo Molnar | 36fe617 | 2015-04-23 12:08:58 +0200 | [diff] [blame] | 329 | fpu->has_fpu = 0; |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 330 | this_cpu_write(fpu_fpregs_owner_ctx, NULL); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Must be paired with a 'clts' before! */ |
Ingo Molnar | c0311f6 | 2015-04-23 12:24:59 +0200 | [diff] [blame] | 334 | static inline void __thread_set_has_fpu(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 335 | { |
Ingo Molnar | c0311f6 | 2015-04-23 12:24:59 +0200 | [diff] [blame] | 336 | fpu->has_fpu = 1; |
| 337 | this_cpu_write(fpu_fpregs_owner_ctx, fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Encapsulate the CR0.TS handling together with the |
| 342 | * software flag. |
| 343 | * |
| 344 | * These generally need preemption protection to work, |
| 345 | * do try to avoid using these on their own. |
| 346 | */ |
Ingo Molnar | 35191e3 | 2015-04-23 12:26:55 +0200 | [diff] [blame] | 347 | static inline void __thread_fpu_end(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 348 | { |
Ingo Molnar | 35191e3 | 2015-04-23 12:26:55 +0200 | [diff] [blame] | 349 | __thread_clear_has_fpu(fpu); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 350 | if (!use_eager_fpu()) |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 351 | stts(); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 354 | static inline void __thread_fpu_begin(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 355 | { |
Oleg Nesterov | 31d9633 | 2014-09-02 19:57:20 +0200 | [diff] [blame] | 356 | if (!use_eager_fpu()) |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 357 | clts(); |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 358 | __thread_set_has_fpu(fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 361 | static inline void drop_fpu(struct task_struct *tsk) |
| 362 | { |
Ingo Molnar | 276983f | 2015-04-23 11:55:18 +0200 | [diff] [blame] | 363 | struct fpu *fpu = &tsk->thread.fpu; |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Forget coprocessor state.. |
| 366 | */ |
| 367 | preempt_disable(); |
Ingo Molnar | c0c2803 | 2015-04-22 09:52:56 +0200 | [diff] [blame] | 368 | tsk->thread.fpu.counter = 0; |
Borislav Petkov | d2d0ac9 | 2015-03-14 11:52:12 +0100 | [diff] [blame] | 369 | |
Ingo Molnar | 276983f | 2015-04-23 11:55:18 +0200 | [diff] [blame] | 370 | if (fpu->has_fpu) { |
Borislav Petkov | d2d0ac9 | 2015-03-14 11:52:12 +0100 | [diff] [blame] | 371 | /* Ignore delayed exceptions from user space */ |
| 372 | asm volatile("1: fwait\n" |
| 373 | "2:\n" |
| 374 | _ASM_EXTABLE(1b, 2b)); |
Ingo Molnar | 35191e3 | 2015-04-23 12:26:55 +0200 | [diff] [blame] | 375 | __thread_fpu_end(fpu); |
Borislav Petkov | d2d0ac9 | 2015-03-14 11:52:12 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame^] | 378 | fpu->fpstate_active = 0; |
Ingo Molnar | 4c13841 | 2015-04-23 12:46:20 +0200 | [diff] [blame] | 379 | |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 380 | preempt_enable(); |
| 381 | } |
| 382 | |
Oleg Nesterov | 8f4d818 | 2015-03-11 18:34:29 +0100 | [diff] [blame] | 383 | static inline void restore_init_xstate(void) |
| 384 | { |
| 385 | if (use_xsave()) |
| 386 | xrstor_state(init_xstate_buf, -1); |
| 387 | else |
| 388 | fxrstor_checking(&init_xstate_buf->i387); |
| 389 | } |
| 390 | |
Borislav Petkov | b85e67d | 2015-03-16 10:21:55 +0100 | [diff] [blame] | 391 | /* |
| 392 | * Reset the FPU state in the eager case and drop it in the lazy case (later use |
| 393 | * will reinit it). |
| 394 | */ |
| 395 | static inline void fpu_reset_state(struct task_struct *tsk) |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 396 | { |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 397 | if (!use_eager_fpu()) |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 398 | drop_fpu(tsk); |
Oleg Nesterov | 8f4d818 | 2015-03-11 18:34:29 +0100 | [diff] [blame] | 399 | else |
| 400 | restore_init_xstate(); |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 403 | /* |
| 404 | * FPU state switching for scheduling. |
| 405 | * |
| 406 | * This is a two-stage process: |
| 407 | * |
| 408 | * - switch_fpu_prepare() saves the old state and |
| 409 | * sets the new state of the CR0.TS bit. This is |
| 410 | * done within the context of the old process. |
| 411 | * |
| 412 | * - switch_fpu_finish() restores the new state as |
| 413 | * necessary. |
| 414 | */ |
| 415 | typedef struct { int preload; } fpu_switch_t; |
| 416 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 417 | static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu) |
| 418 | { |
Ingo Molnar | 276983f | 2015-04-23 11:55:18 +0200 | [diff] [blame] | 419 | struct fpu *old_fpu = &old->thread.fpu; |
Ingo Molnar | c0311f6 | 2015-04-23 12:24:59 +0200 | [diff] [blame] | 420 | struct fpu *new_fpu = &new->thread.fpu; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 421 | fpu_switch_t fpu; |
| 422 | |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 423 | /* |
| 424 | * If the task has used the math, pre-load the FPU on xsave processors |
| 425 | * or if the past 5 consecutive context-switches used math. |
| 426 | */ |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame^] | 427 | fpu.preload = new_fpu->fpstate_active && |
Ingo Molnar | c0c2803 | 2015-04-22 09:52:56 +0200 | [diff] [blame] | 428 | (use_eager_fpu() || new->thread.fpu.counter > 5); |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 429 | |
Ingo Molnar | 276983f | 2015-04-23 11:55:18 +0200 | [diff] [blame] | 430 | if (old_fpu->has_fpu) { |
Ingo Molnar | 6522d78 | 2015-04-22 19:54:09 +0200 | [diff] [blame] | 431 | if (!fpu_save_init(&old->thread.fpu)) |
Rik van Riel | 6a5fe89 | 2015-02-06 15:02:04 -0500 | [diff] [blame] | 432 | task_disable_lazy_fpu_restore(old); |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 433 | else |
| 434 | old->thread.fpu.last_cpu = cpu; |
| 435 | |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 436 | /* But leave fpu_fpregs_owner_ctx! */ |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 437 | old->thread.fpu.has_fpu = 0; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 438 | |
| 439 | /* Don't change CR0.TS if we just switch! */ |
| 440 | if (fpu.preload) { |
Ingo Molnar | c0c2803 | 2015-04-22 09:52:56 +0200 | [diff] [blame] | 441 | new->thread.fpu.counter++; |
Ingo Molnar | c0311f6 | 2015-04-23 12:24:59 +0200 | [diff] [blame] | 442 | __thread_set_has_fpu(new_fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 443 | prefetch(new->thread.fpu.state); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 444 | } else if (!use_eager_fpu()) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 445 | stts(); |
| 446 | } else { |
Ingo Molnar | c0c2803 | 2015-04-22 09:52:56 +0200 | [diff] [blame] | 447 | old->thread.fpu.counter = 0; |
Rik van Riel | 6a5fe89 | 2015-02-06 15:02:04 -0500 | [diff] [blame] | 448 | task_disable_lazy_fpu_restore(old); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 449 | if (fpu.preload) { |
Ingo Molnar | c0c2803 | 2015-04-22 09:52:56 +0200 | [diff] [blame] | 450 | new->thread.fpu.counter++; |
Rik van Riel | 728e53f | 2015-02-06 15:02:05 -0500 | [diff] [blame] | 451 | if (fpu_lazy_restore(new, cpu)) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 452 | fpu.preload = 0; |
| 453 | else |
| 454 | prefetch(new->thread.fpu.state); |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 455 | __thread_fpu_begin(new_fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | return fpu; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * By the time this gets called, we've already cleared CR0.TS and |
| 463 | * given the process the FPU if we are going to preload the FPU |
| 464 | * state - all we need to do is to conditionally restore the register |
| 465 | * state itself. |
| 466 | */ |
| 467 | static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) |
| 468 | { |
| 469 | if (fpu.preload) { |
| 470 | if (unlikely(restore_fpu_checking(new))) |
Borislav Petkov | b85e67d | 2015-03-16 10:21:55 +0100 | [diff] [blame] | 471 | fpu_reset_state(new); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Signal frame handlers... |
| 477 | */ |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 478 | extern int save_xstate_sig(void __user *buf, void __user *fx, int size); |
| 479 | extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 480 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 481 | static inline int xstate_sigframe_size(void) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 482 | { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 483 | return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size; |
| 484 | } |
| 485 | |
| 486 | static inline int restore_xstate_sig(void __user *buf, int ia32_frame) |
| 487 | { |
| 488 | void __user *buf_fx = buf; |
| 489 | int size = xstate_sigframe_size(); |
| 490 | |
| 491 | if (ia32_frame && use_fxsr()) { |
| 492 | buf_fx = buf + sizeof(struct i387_fsave_struct); |
| 493 | size += sizeof(struct i387_fsave_struct); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 494 | } |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 495 | |
| 496 | return __restore_xstate_sig(buf, buf_fx, size); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
Oleg Nesterov | fb14b4e | 2015-03-11 18:34:09 +0100 | [diff] [blame] | 500 | * Needs to be preemption-safe. |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 501 | * |
Suresh Siddha | 377ffbc | 2012-08-24 14:12:58 -0700 | [diff] [blame] | 502 | * NOTE! user_fpu_begin() must be used only immediately before restoring |
Oleg Nesterov | fb14b4e | 2015-03-11 18:34:09 +0100 | [diff] [blame] | 503 | * the save state. It does not do any saving/restoring on its own. In |
| 504 | * lazy FPU mode, it is just an optimization to avoid a #NM exception, |
| 505 | * the task can lose the FPU right after preempt_enable(). |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 506 | */ |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 507 | static inline void user_fpu_begin(void) |
| 508 | { |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 509 | struct fpu *fpu = ¤t->thread.fpu; |
| 510 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 511 | preempt_disable(); |
| 512 | if (!user_has_fpu()) |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 513 | __thread_fpu_begin(fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 514 | preempt_enable(); |
| 515 | } |
| 516 | |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 517 | static inline void __save_fpu(struct task_struct *tsk) |
| 518 | { |
Fenghua Yu | f41d830 | 2014-05-29 11:12:41 -0700 | [diff] [blame] | 519 | if (use_xsave()) { |
| 520 | if (unlikely(system_state == SYSTEM_BOOTING)) |
Ingo Molnar | 3e261c1 | 2015-04-22 15:08:34 +0200 | [diff] [blame] | 521 | xsave_state_booting(&tsk->thread.fpu.state->xsave); |
Fenghua Yu | f41d830 | 2014-05-29 11:12:41 -0700 | [diff] [blame] | 522 | else |
Ingo Molnar | 3e261c1 | 2015-04-22 15:08:34 +0200 | [diff] [blame] | 523 | xsave_state(&tsk->thread.fpu.state->xsave); |
Fenghua Yu | f41d830 | 2014-05-29 11:12:41 -0700 | [diff] [blame] | 524 | } else |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 525 | fpu_fxsave(&tsk->thread.fpu); |
| 526 | } |
| 527 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 528 | /* |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 529 | * i387 state interaction |
| 530 | */ |
| 531 | static inline unsigned short get_fpu_cwd(struct task_struct *tsk) |
| 532 | { |
| 533 | if (cpu_has_fxsr) { |
| 534 | return tsk->thread.fpu.state->fxsave.cwd; |
| 535 | } else { |
| 536 | return (unsigned short)tsk->thread.fpu.state->fsave.cwd; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | static inline unsigned short get_fpu_swd(struct task_struct *tsk) |
| 541 | { |
| 542 | if (cpu_has_fxsr) { |
| 543 | return tsk->thread.fpu.state->fxsave.swd; |
| 544 | } else { |
| 545 | return (unsigned short)tsk->thread.fpu.state->fsave.swd; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk) |
| 550 | { |
| 551 | if (cpu_has_xmm) { |
| 552 | return tsk->thread.fpu.state->fxsave.mxcsr; |
| 553 | } else { |
| 554 | return MXCSR_DEFAULT; |
| 555 | } |
| 556 | } |
| 557 | |
Ingo Molnar | 8ffb53a | 2015-04-22 15:41:56 +0200 | [diff] [blame] | 558 | extern void fpstate_cache_init(void); |
| 559 | |
Ingo Molnar | ed97b08 | 2015-04-03 12:41:14 +0200 | [diff] [blame] | 560 | extern int fpstate_alloc(struct fpu *fpu); |
Ingo Molnar | 5a12bf6 | 2015-04-22 15:58:37 +0200 | [diff] [blame] | 561 | extern void fpstate_free(struct fpu *fpu); |
Ingo Molnar | a752b53 | 2015-04-22 15:47:05 +0200 | [diff] [blame] | 562 | extern int fpu__copy(struct task_struct *dst, struct task_struct *src); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 563 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 564 | static inline unsigned long |
| 565 | alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, |
| 566 | unsigned long *size) |
| 567 | { |
| 568 | unsigned long frame_size = xstate_sigframe_size(); |
| 569 | |
| 570 | *buf_fx = sp = round_down(sp - frame_size, 64); |
| 571 | if (ia32_frame && use_fxsr()) { |
| 572 | frame_size += sizeof(struct i387_fsave_struct); |
| 573 | sp -= sizeof(struct i387_fsave_struct); |
| 574 | } |
| 575 | |
| 576 | *size = frame_size; |
| 577 | return sp; |
| 578 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 579 | |
| 580 | #endif |