Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [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 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 10 | #ifndef _ASM_X86_I387_H |
| 11 | #define _ASM_X86_I387_H |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 12 | |
Herbert Xu | 3b0d659 | 2009-11-03 09:11:15 -0500 | [diff] [blame] | 13 | #ifndef __ASSEMBLY__ |
| 14 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 15 | #include <linux/sched.h> |
| 16 | #include <linux/kernel_stat.h> |
| 17 | #include <linux/regset.h> |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 18 | #include <linux/hardirq.h> |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 19 | #include <linux/slab.h> |
H. Peter Anvin | 92c37fa | 2008-02-04 16:47:58 +0100 | [diff] [blame] | 20 | #include <asm/asm.h> |
H. Peter Anvin | c9775b4 | 2010-05-11 17:49:54 -0700 | [diff] [blame] | 21 | #include <asm/cpufeature.h> |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 22 | #include <asm/processor.h> |
| 23 | #include <asm/sigcontext.h> |
| 24 | #include <asm/user.h> |
| 25 | #include <asm/uaccess.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 26 | #include <asm/xsave.h> |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 27 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 28 | extern unsigned int sig_xstate_size; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 29 | extern void fpu_init(void); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 30 | extern void mxcsr_feature_mask_init(void); |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 31 | extern int init_fpu(struct task_struct *child); |
Linus Torvalds | be98c2c | 2012-02-13 13:47:25 -0800 | [diff] [blame] | 32 | extern void math_state_restore(void); |
Jaswinder Singh | 3645493 | 2008-07-21 22:31:57 +0530 | [diff] [blame] | 33 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 34 | |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 35 | DECLARE_PER_CPU(struct task_struct *, fpu_owner_task); |
| 36 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 37 | extern user_regset_active_fn fpregs_active, xfpregs_active; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 38 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, |
| 39 | xstateregs_get; |
| 40 | extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, |
| 41 | xstateregs_set; |
| 42 | |
| 43 | /* |
| 44 | * xstateregs_active == fpregs_active. Please refer to the comment |
| 45 | * at the definition of fpregs_active. |
| 46 | */ |
| 47 | #define xstateregs_active fpregs_active |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 48 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 49 | extern struct _fpx_sw_bytes fx_sw_reserved; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 50 | #ifdef CONFIG_IA32_EMULATION |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 51 | extern unsigned int sig_xstate_ia32_size; |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 52 | extern struct _fpx_sw_bytes fx_sw_reserved_ia32; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 53 | struct _fpstate_ia32; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 54 | struct _xstate_ia32; |
| 55 | extern int save_i387_xstate_ia32(void __user *buf); |
| 56 | extern int restore_i387_xstate_ia32(void __user *buf); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 57 | #endif |
| 58 | |
Brian Gerst | 8eb91a5 | 2010-09-03 21:17:16 -0400 | [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 | |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 65 | #define X87_FSW_ES (1 << 7) /* Exception Summary */ |
| 66 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 67 | static __always_inline __pure bool use_xsaveopt(void) |
| 68 | { |
Suresh Siddha | 6bad06b | 2010-07-19 16:05:52 -0700 | [diff] [blame] | 69 | return static_cpu_has(X86_FEATURE_XSAVEOPT); |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
H. Peter Anvin | c9775b4 | 2010-05-11 17:49:54 -0700 | [diff] [blame] | 72 | static __always_inline __pure bool use_xsave(void) |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 73 | { |
H. Peter Anvin | c9775b4 | 2010-05-11 17:49:54 -0700 | [diff] [blame] | 74 | return static_cpu_has(X86_FEATURE_XSAVE); |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 75 | } |
| 76 | |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 77 | static __always_inline __pure bool use_fxsr(void) |
| 78 | { |
| 79 | return static_cpu_has(X86_FEATURE_FXSR); |
| 80 | } |
| 81 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 82 | extern void __sanitize_i387_state(struct task_struct *); |
| 83 | |
| 84 | static inline void sanitize_i387_state(struct task_struct *tsk) |
| 85 | { |
| 86 | if (!use_xsaveopt()) |
| 87 | return; |
| 88 | __sanitize_i387_state(tsk); |
| 89 | } |
| 90 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 91 | #ifdef CONFIG_X86_64 |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 92 | static inline int fxrstor_checking(struct i387_fxsave_struct *fx) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 93 | { |
| 94 | int err; |
| 95 | |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 96 | /* See comment in fxsave() below. */ |
H. Peter Anvin | fd35fbc | 2010-10-22 15:33:38 -0700 | [diff] [blame] | 97 | #ifdef CONFIG_AS_FXSAVEQ |
| 98 | asm volatile("1: fxrstorq %[fx]\n\t" |
| 99 | "2:\n" |
| 100 | ".section .fixup,\"ax\"\n" |
| 101 | "3: movl $-1,%[err]\n" |
| 102 | " jmp 2b\n" |
| 103 | ".previous\n" |
| 104 | _ASM_EXTABLE(1b, 3b) |
| 105 | : [err] "=r" (err) |
| 106 | : [fx] "m" (*fx), "0" (0)); |
| 107 | #else |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 108 | asm volatile("1: rex64/fxrstor (%[fx])\n\t" |
| 109 | "2:\n" |
| 110 | ".section .fixup,\"ax\"\n" |
| 111 | "3: movl $-1,%[err]\n" |
| 112 | " jmp 2b\n" |
| 113 | ".previous\n" |
Joe Perches | affe663 | 2008-03-23 01:02:18 -0700 | [diff] [blame] | 114 | _ASM_EXTABLE(1b, 3b) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 115 | : [err] "=r" (err) |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 116 | : [fx] "R" (fx), "m" (*fx), "0" (0)); |
H. Peter Anvin | fd35fbc | 2010-10-22 15:33:38 -0700 | [diff] [blame] | 117 | #endif |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 118 | return err; |
| 119 | } |
| 120 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 121 | static inline int fxsave_user(struct i387_fxsave_struct __user *fx) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 122 | { |
| 123 | int err; |
| 124 | |
Suresh Siddha | 8e221b6 | 2010-06-22 16:23:37 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Clear the bytes not touched by the fxsave and reserved |
| 127 | * for the SW usage. |
| 128 | */ |
| 129 | err = __clear_user(&fx->sw_reserved, |
| 130 | sizeof(struct _fpx_sw_bytes)); |
| 131 | if (unlikely(err)) |
| 132 | return -EFAULT; |
| 133 | |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 134 | /* See comment in fxsave() below. */ |
H. Peter Anvin | fd35fbc | 2010-10-22 15:33:38 -0700 | [diff] [blame] | 135 | #ifdef CONFIG_AS_FXSAVEQ |
| 136 | asm volatile("1: fxsaveq %[fx]\n\t" |
| 137 | "2:\n" |
| 138 | ".section .fixup,\"ax\"\n" |
| 139 | "3: movl $-1,%[err]\n" |
| 140 | " jmp 2b\n" |
| 141 | ".previous\n" |
| 142 | _ASM_EXTABLE(1b, 3b) |
| 143 | : [err] "=r" (err), [fx] "=m" (*fx) |
| 144 | : "0" (0)); |
| 145 | #else |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 146 | asm volatile("1: rex64/fxsave (%[fx])\n\t" |
| 147 | "2:\n" |
| 148 | ".section .fixup,\"ax\"\n" |
| 149 | "3: movl $-1,%[err]\n" |
| 150 | " jmp 2b\n" |
| 151 | ".previous\n" |
Joe Perches | affe663 | 2008-03-23 01:02:18 -0700 | [diff] [blame] | 152 | _ASM_EXTABLE(1b, 3b) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 153 | : [err] "=r" (err), "=m" (*fx) |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 154 | : [fx] "R" (fx), "0" (0)); |
H. Peter Anvin | fd35fbc | 2010-10-22 15:33:38 -0700 | [diff] [blame] | 155 | #endif |
Joe Perches | affe663 | 2008-03-23 01:02:18 -0700 | [diff] [blame] | 156 | if (unlikely(err) && |
| 157 | __clear_user(fx, sizeof(struct i387_fxsave_struct))) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 158 | err = -EFAULT; |
| 159 | /* No need to clear here because the caller clears USED_MATH */ |
| 160 | return err; |
| 161 | } |
| 162 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 163 | static inline void fpu_fxsave(struct fpu *fpu) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 164 | { |
| 165 | /* Using "rex64; fxsave %0" is broken because, if the memory operand |
| 166 | uses any extended registers for addressing, a second REX prefix |
| 167 | will be generated (to the assembler, rex64 followed by semicolon |
| 168 | is a separate instruction), and hence the 64-bitness is lost. */ |
Linus Torvalds | b6f7e38 | 2010-10-21 13:34:32 -0700 | [diff] [blame] | 169 | |
H. Peter Anvin | d7acb92 | 2010-10-13 16:00:29 -0700 | [diff] [blame] | 170 | #ifdef CONFIG_AS_FXSAVEQ |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 171 | /* Using "fxsaveq %0" would be the ideal choice, but is only supported |
| 172 | starting with gas 2.16. */ |
| 173 | __asm__ __volatile__("fxsaveq %0" |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 174 | : "=m" (fpu->state->fxsave)); |
Linus Torvalds | b6f7e38 | 2010-10-21 13:34:32 -0700 | [diff] [blame] | 175 | #else |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 176 | /* Using, as a workaround, the properly prefixed form below isn't |
| 177 | accepted by any binutils version so far released, complaining that |
| 178 | the same type of prefix is used twice if an extended register is |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 179 | needed for addressing (fix submitted to mainline 2005-11-21). |
| 180 | asm volatile("rex64/fxsave %0" |
| 181 | : "=m" (fpu->state->fxsave)); |
| 182 | This, however, we can work around by forcing the compiler to select |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 183 | an addressing mode that doesn't require extended registers. */ |
Brian Gerst | 8202413 | 2010-09-03 21:17:14 -0400 | [diff] [blame] | 184 | asm volatile("rex64/fxsave (%[fx])" |
| 185 | : "=m" (fpu->state->fxsave) |
| 186 | : [fx] "R" (&fpu->state->fxsave)); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 187 | #endif |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 190 | #else /* CONFIG_X86_32 */ |
| 191 | |
Jiri Slaby | 34ba476 | 2009-04-08 13:31:59 +0200 | [diff] [blame] | 192 | /* perform fxrstor iff the processor has extended states, otherwise frstor */ |
| 193 | static inline int fxrstor_checking(struct i387_fxsave_struct *fx) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 194 | { |
| 195 | /* |
| 196 | * The "nop" is needed to make the instructions the same |
| 197 | * length. |
| 198 | */ |
| 199 | alternative_input( |
| 200 | "nop ; frstor %1", |
| 201 | "fxrstor %1", |
| 202 | X86_FEATURE_FXSR, |
Jiri Slaby | 34ba476 | 2009-04-08 13:31:59 +0200 | [diff] [blame] | 203 | "m" (*fx)); |
| 204 | |
Jiri Slaby | fcb2ac5 | 2009-04-08 13:31:58 +0200 | [diff] [blame] | 205 | return 0; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 208 | static inline void fpu_fxsave(struct fpu *fpu) |
| 209 | { |
| 210 | asm volatile("fxsave %[fx]" |
| 211 | : [fx] "=m" (fpu->state->fxsave)); |
| 212 | } |
| 213 | |
Brian Gerst | b2b57fe | 2010-09-03 21:17:19 -0400 | [diff] [blame] | 214 | #endif /* CONFIG_X86_64 */ |
| 215 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 216 | /* |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 217 | * These must be called with preempt disabled. Returns |
| 218 | * 'true' if the FPU state is still intact. |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 219 | */ |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 220 | static inline int fpu_save_init(struct fpu *fpu) |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 221 | { |
Avi Kivity | c9ad488 | 2010-05-06 11:45:45 +0300 | [diff] [blame] | 222 | if (use_xsave()) { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 223 | fpu_xsave(fpu); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * xsave header may indicate the init state of the FP. |
| 227 | */ |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 228 | if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP)) |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 229 | return 1; |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 230 | } else if (use_fxsr()) { |
| 231 | fpu_fxsave(fpu); |
| 232 | } else { |
Hans Rosenfeld | f994d99 | 2011-04-06 18:06:43 +0200 | [diff] [blame] | 233 | asm volatile("fnsave %[fx]; fwait" |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 234 | : [fx] "=m" (fpu->state->fsave)); |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 235 | return 0; |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 238 | /* |
| 239 | * If exceptions are pending, we need to clear them so |
| 240 | * that we don't randomly get exceptions later. |
| 241 | * |
| 242 | * FIXME! Is this perhaps only true for the old-style |
| 243 | * irq13 case? Maybe we could leave the x87 state |
| 244 | * intact otherwise? |
| 245 | */ |
| 246 | if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) { |
Brian Gerst | 58a992b | 2010-09-03 21:17:18 -0400 | [diff] [blame] | 247 | asm volatile("fnclex"); |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | return 1; |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 251 | } |
| 252 | |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 253 | static inline int __save_init_fpu(struct task_struct *tsk) |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 254 | { |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 255 | return fpu_save_init(&tsk->thread.fpu); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 258 | static inline int fpu_fxrstor_checking(struct fpu *fpu) |
| 259 | { |
| 260 | return fxrstor_checking(&fpu->state->fxsave); |
| 261 | } |
| 262 | |
| 263 | static inline int fpu_restore_checking(struct fpu *fpu) |
| 264 | { |
| 265 | if (use_xsave()) |
| 266 | return fpu_xrstor_checking(fpu); |
| 267 | else |
| 268 | return fpu_fxrstor_checking(fpu); |
| 269 | } |
| 270 | |
Jiri Slaby | 34ba476 | 2009-04-08 13:31:59 +0200 | [diff] [blame] | 271 | static inline int restore_fpu_checking(struct task_struct *tsk) |
| 272 | { |
Linus Torvalds | 80ab6f1 | 2012-02-19 11:48:44 -0800 | [diff] [blame] | 273 | /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception |
| 274 | is pending. Clear the x87 state here by setting it to fixed |
| 275 | values. "m" is a random variable that should be in L1 */ |
| 276 | alternative_input( |
| 277 | ASM_NOP8 ASM_NOP2, |
| 278 | "emms\n\t" /* clear stack tags */ |
| 279 | "fildl %P[addr]", /* set F?P to defined value */ |
| 280 | X86_FEATURE_FXSAVE_LEAK, |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 281 | [addr] "m" (tsk->thread.fpu.has_fpu)); |
Linus Torvalds | 80ab6f1 | 2012-02-19 11:48:44 -0800 | [diff] [blame] | 282 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 283 | return fpu_restore_checking(&tsk->thread.fpu); |
Jiri Slaby | 34ba476 | 2009-04-08 13:31:59 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 286 | /* |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 287 | * Software FPU state helpers. Careful: these need to |
| 288 | * be preemption protection *and* they need to be |
| 289 | * properly paired with the CR0.TS changes! |
| 290 | */ |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 291 | static inline int __thread_has_fpu(struct task_struct *tsk) |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 292 | { |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 293 | return tsk->thread.fpu.has_fpu; |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* Must be paired with an 'stts' after! */ |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 297 | static inline void __thread_clear_has_fpu(struct task_struct *tsk) |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 298 | { |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 299 | tsk->thread.fpu.has_fpu = 0; |
| 300 | percpu_write(fpu_owner_task, NULL); |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Must be paired with a 'clts' before! */ |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 304 | static inline void __thread_set_has_fpu(struct task_struct *tsk) |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 305 | { |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 306 | tsk->thread.fpu.has_fpu = 1; |
| 307 | percpu_write(fpu_owner_task, tsk); |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Encapsulate the CR0.TS handling together with the |
| 312 | * software flag. |
| 313 | * |
| 314 | * These generally need preemption protection to work, |
| 315 | * do try to avoid using these on their own. |
| 316 | */ |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 317 | static inline void __thread_fpu_end(struct task_struct *tsk) |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 318 | { |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 319 | __thread_clear_has_fpu(tsk); |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 320 | stts(); |
| 321 | } |
| 322 | |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 323 | static inline void __thread_fpu_begin(struct task_struct *tsk) |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 324 | { |
| 325 | clts(); |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 326 | __thread_set_has_fpu(tsk); |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 330 | * FPU state switching for scheduling. |
| 331 | * |
| 332 | * This is a two-stage process: |
| 333 | * |
| 334 | * - switch_fpu_prepare() saves the old state and |
| 335 | * sets the new state of the CR0.TS bit. This is |
| 336 | * done within the context of the old process. |
| 337 | * |
| 338 | * - switch_fpu_finish() restores the new state as |
| 339 | * necessary. |
| 340 | */ |
| 341 | typedef struct { int preload; } fpu_switch_t; |
| 342 | |
| 343 | /* |
| 344 | * FIXME! We could do a totally lazy restore, but we need to |
| 345 | * add a per-cpu "this was the task that last touched the FPU |
| 346 | * on this CPU" variable, and the task needs to have a "I last |
| 347 | * touched the FPU on this CPU" and check them. |
| 348 | * |
| 349 | * We don't do that yet, so "fpu_lazy_restore()" always returns |
| 350 | * false, but some day.. |
| 351 | */ |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 352 | static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu) |
| 353 | { |
| 354 | return new == percpu_read_stable(fpu_owner_task) && |
| 355 | cpu == new->thread.fpu.last_cpu; |
| 356 | } |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 357 | |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 358 | static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu) |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 359 | { |
| 360 | fpu_switch_t fpu; |
| 361 | |
| 362 | fpu.preload = tsk_used_math(new) && new->fpu_counter > 5; |
| 363 | if (__thread_has_fpu(old)) { |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 364 | if (!__save_init_fpu(old)) |
| 365 | cpu = ~0; |
| 366 | old->thread.fpu.last_cpu = cpu; |
| 367 | old->thread.fpu.has_fpu = 0; /* But leave fpu_owner_task! */ |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 368 | |
| 369 | /* Don't change CR0.TS if we just switch! */ |
| 370 | if (fpu.preload) { |
Linus Torvalds | cea20ca | 2012-02-20 10:24:09 -0800 | [diff] [blame] | 371 | new->fpu_counter++; |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 372 | __thread_set_has_fpu(new); |
| 373 | prefetch(new->thread.fpu.state); |
| 374 | } else |
| 375 | stts(); |
| 376 | } else { |
| 377 | old->fpu_counter = 0; |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 378 | old->thread.fpu.last_cpu = ~0; |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 379 | if (fpu.preload) { |
Linus Torvalds | cea20ca | 2012-02-20 10:24:09 -0800 | [diff] [blame] | 380 | new->fpu_counter++; |
Linus Torvalds | 7e16838 | 2012-02-19 13:27:00 -0800 | [diff] [blame] | 381 | if (fpu_lazy_restore(new, cpu)) |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 382 | fpu.preload = 0; |
| 383 | else |
| 384 | prefetch(new->thread.fpu.state); |
| 385 | __thread_fpu_begin(new); |
| 386 | } |
| 387 | } |
| 388 | return fpu; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * By the time this gets called, we've already cleared CR0.TS and |
| 393 | * given the process the FPU if we are going to preload the FPU |
| 394 | * state - all we need to do is to conditionally restore the register |
| 395 | * state itself. |
| 396 | */ |
| 397 | static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu) |
| 398 | { |
Linus Torvalds | 80ab6f1 | 2012-02-19 11:48:44 -0800 | [diff] [blame] | 399 | if (fpu.preload) { |
| 400 | if (unlikely(restore_fpu_checking(new))) |
| 401 | __thread_fpu_end(new); |
| 402 | } |
Linus Torvalds | 34ddc81 | 2012-02-18 12:56:35 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 406 | * Signal frame handlers... |
| 407 | */ |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 408 | extern int save_i387_xstate(void __user *buf); |
| 409 | extern int restore_i387_xstate(void __user *buf); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 410 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 411 | static inline void __clear_fpu(struct task_struct *tsk) |
| 412 | { |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 413 | if (__thread_has_fpu(tsk)) { |
Brian Gerst | 51115d4 | 2010-09-03 21:17:10 -0400 | [diff] [blame] | 414 | /* Ignore delayed exceptions from user space */ |
| 415 | asm volatile("1: fwait\n" |
| 416 | "2:\n" |
| 417 | _ASM_EXTABLE(1b, 2b)); |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 418 | __thread_fpu_end(tsk); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame^] | 422 | extern bool irq_fpu_usable(void); |
| 423 | extern void kernel_fpu_begin(void); |
| 424 | extern void kernel_fpu_end(void); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 425 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 426 | /* |
| 427 | * Some instructions like VIA's padlock instructions generate a spurious |
| 428 | * DNA fault but don't modify SSE registers. And these instructions |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 429 | * get used from interrupt context as well. To prevent these kernel instructions |
| 430 | * in interrupt context interacting wrongly with other user/kernel fpu usage, we |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 431 | * should use them only in the context of irq_ts_save/restore() |
| 432 | */ |
| 433 | static inline int irq_ts_save(void) |
| 434 | { |
| 435 | /* |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 436 | * If in process context and not atomic, we can take a spurious DNA fault. |
| 437 | * Otherwise, doing clts() in process context requires disabling preemption |
| 438 | * or some heavy lifting like kernel_fpu_begin() |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 439 | */ |
Chuck Ebbert | 0b8c3d5 | 2009-06-09 10:40:50 -0400 | [diff] [blame] | 440 | if (!in_atomic()) |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 441 | return 0; |
| 442 | |
| 443 | if (read_cr0() & X86_CR0_TS) { |
| 444 | clts(); |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static inline void irq_ts_restore(int TS_state) |
| 452 | { |
| 453 | if (TS_state) |
| 454 | stts(); |
| 455 | } |
| 456 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 457 | /* |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 458 | * The question "does this thread have fpu access?" |
| 459 | * is slightly racy, since preemption could come in |
| 460 | * and revoke it immediately after the test. |
| 461 | * |
| 462 | * However, even in that very unlikely scenario, |
| 463 | * we can just assume we have FPU access - typically |
| 464 | * to save the FP state - we'll just take a #NM |
| 465 | * fault and get the FPU access back. |
| 466 | * |
| 467 | * The actual user_fpu_begin/end() functions |
| 468 | * need to be preemption-safe, though. |
| 469 | * |
| 470 | * NOTE! user_fpu_end() must be used only after you |
| 471 | * have saved the FP state, and user_fpu_begin() must |
| 472 | * be used only immediately before restoring it. |
| 473 | * These functions do not do any save/restore on |
| 474 | * their own. |
| 475 | */ |
| 476 | static inline int user_has_fpu(void) |
| 477 | { |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 478 | return __thread_has_fpu(current); |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static inline void user_fpu_end(void) |
| 482 | { |
| 483 | preempt_disable(); |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 484 | __thread_fpu_end(current); |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 485 | preempt_enable(); |
| 486 | } |
| 487 | |
| 488 | static inline void user_fpu_begin(void) |
| 489 | { |
| 490 | preempt_disable(); |
Linus Torvalds | 6d59d7a | 2012-02-16 13:33:12 -0800 | [diff] [blame] | 491 | if (!user_has_fpu()) |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 492 | __thread_fpu_begin(current); |
Linus Torvalds | 15d8791 | 2012-02-16 09:15:04 -0800 | [diff] [blame] | 493 | preempt_enable(); |
| 494 | } |
| 495 | |
| 496 | /* |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 497 | * These disable preemption on their own and are safe |
| 498 | */ |
| 499 | static inline void save_init_fpu(struct task_struct *tsk) |
| 500 | { |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 501 | WARN_ON_ONCE(!__thread_has_fpu(tsk)); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 502 | preempt_disable(); |
| 503 | __save_init_fpu(tsk); |
Linus Torvalds | f94edac | 2012-02-17 21:48:54 -0800 | [diff] [blame] | 504 | __thread_fpu_end(tsk); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 505 | preempt_enable(); |
| 506 | } |
| 507 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame^] | 508 | extern void unlazy_fpu(struct task_struct *tsk); |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 509 | |
| 510 | static inline void clear_fpu(struct task_struct *tsk) |
| 511 | { |
| 512 | preempt_disable(); |
| 513 | __clear_fpu(tsk); |
| 514 | preempt_enable(); |
| 515 | } |
| 516 | |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 517 | /* |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 518 | * i387 state interaction |
| 519 | */ |
| 520 | static inline unsigned short get_fpu_cwd(struct task_struct *tsk) |
| 521 | { |
| 522 | if (cpu_has_fxsr) { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 523 | return tsk->thread.fpu.state->fxsave.cwd; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 524 | } else { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 525 | return (unsigned short)tsk->thread.fpu.state->fsave.cwd; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | static inline unsigned short get_fpu_swd(struct task_struct *tsk) |
| 530 | { |
| 531 | if (cpu_has_fxsr) { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 532 | return tsk->thread.fpu.state->fxsave.swd; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 533 | } else { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 534 | return (unsigned short)tsk->thread.fpu.state->fsave.swd; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk) |
| 539 | { |
| 540 | if (cpu_has_xmm) { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 541 | return tsk->thread.fpu.state->fxsave.mxcsr; |
Roland McGrath | 1eeaed7 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 542 | } else { |
| 543 | return MXCSR_DEFAULT; |
| 544 | } |
| 545 | } |
| 546 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 547 | static bool fpu_allocated(struct fpu *fpu) |
| 548 | { |
| 549 | return fpu->state != NULL; |
| 550 | } |
| 551 | |
| 552 | static inline int fpu_alloc(struct fpu *fpu) |
| 553 | { |
| 554 | if (fpu_allocated(fpu)) |
| 555 | return 0; |
| 556 | fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL); |
| 557 | if (!fpu->state) |
| 558 | return -ENOMEM; |
| 559 | WARN_ON((unsigned long)fpu->state & 15); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static inline void fpu_free(struct fpu *fpu) |
| 564 | { |
| 565 | if (fpu->state) { |
| 566 | kmem_cache_free(task_xstate_cachep, fpu->state); |
| 567 | fpu->state = NULL; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static inline void fpu_copy(struct fpu *dst, struct fpu *src) |
| 572 | { |
| 573 | memcpy(dst->state, src->state, xstate_size); |
| 574 | } |
| 575 | |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 576 | extern void fpu_finit(struct fpu *fpu); |
| 577 | |
Herbert Xu | 3b0d659 | 2009-11-03 09:11:15 -0500 | [diff] [blame] | 578 | #endif /* __ASSEMBLY__ */ |
| 579 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 580 | #endif /* _ASM_X86_I387_H */ |