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