Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * include/asm-x86_64/i387.h |
| 3 | * |
| 4 | * Copyright (C) 1994 Linus Torvalds |
| 5 | * |
| 6 | * Pentium III FXSR, SSE support |
| 7 | * General FPU state handling cleanups |
| 8 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 9 | * x86-64 work by Andi Kleen 2002 |
| 10 | */ |
| 11 | |
| 12 | #ifndef __ASM_X86_64_I387_H |
| 13 | #define __ASM_X86_64_I387_H |
| 14 | |
| 15 | #include <linux/sched.h> |
| 16 | #include <asm/processor.h> |
| 17 | #include <asm/sigcontext.h> |
| 18 | #include <asm/user.h> |
| 19 | #include <asm/thread_info.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
| 22 | extern void fpu_init(void); |
| 23 | extern unsigned int mxcsr_feature_mask; |
| 24 | extern void mxcsr_feature_mask_init(void); |
| 25 | extern void init_fpu(struct task_struct *child); |
| 26 | extern int save_i387(struct _fpstate __user *buf); |
| 27 | |
| 28 | /* |
| 29 | * FPU lazy state save handling... |
| 30 | */ |
| 31 | |
| 32 | #define unlazy_fpu(tsk) do { \ |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame^] | 33 | if (task_thread_info(tsk)->status & TS_USEDFPU) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | save_init_fpu(tsk); \ |
| 35 | } while (0) |
| 36 | |
| 37 | /* Ignore delayed exceptions from user space */ |
| 38 | static inline void tolerant_fwait(void) |
| 39 | { |
| 40 | asm volatile("1: fwait\n" |
| 41 | "2:\n" |
| 42 | " .section __ex_table,\"a\"\n" |
| 43 | " .align 8\n" |
| 44 | " .quad 1b,2b\n" |
| 45 | " .previous\n"); |
| 46 | } |
| 47 | |
| 48 | #define clear_fpu(tsk) do { \ |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame^] | 49 | if (task_thread_info(tsk)->status & TS_USEDFPU) { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | tolerant_fwait(); \ |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame^] | 51 | task_thread_info(tsk)->status &= ~TS_USEDFPU; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | stts(); \ |
| 53 | } \ |
| 54 | } while (0) |
| 55 | |
| 56 | /* |
| 57 | * ptrace request handers... |
| 58 | */ |
| 59 | extern int get_fpregs(struct user_i387_struct __user *buf, |
| 60 | struct task_struct *tsk); |
| 61 | extern int set_fpregs(struct task_struct *tsk, |
| 62 | struct user_i387_struct __user *buf); |
| 63 | |
| 64 | /* |
| 65 | * i387 state interaction |
| 66 | */ |
| 67 | #define get_fpu_mxcsr(t) ((t)->thread.i387.fxsave.mxcsr) |
| 68 | #define get_fpu_cwd(t) ((t)->thread.i387.fxsave.cwd) |
| 69 | #define get_fpu_fxsr_twd(t) ((t)->thread.i387.fxsave.twd) |
| 70 | #define get_fpu_swd(t) ((t)->thread.i387.fxsave.swd) |
| 71 | #define set_fpu_cwd(t,val) ((t)->thread.i387.fxsave.cwd = (val)) |
| 72 | #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val)) |
| 73 | #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val)) |
| 74 | |
| 75 | static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) |
| 76 | { |
| 77 | int err; |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 78 | |
| 79 | asm volatile("1: rex64/fxrstor (%[fx])\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | "2:\n" |
| 81 | ".section .fixup,\"ax\"\n" |
| 82 | "3: movl $-1,%[err]\n" |
| 83 | " jmp 2b\n" |
| 84 | ".previous\n" |
| 85 | ".section __ex_table,\"a\"\n" |
| 86 | " .align 8\n" |
| 87 | " .quad 1b,3b\n" |
| 88 | ".previous" |
| 89 | : [err] "=r" (err) |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 90 | #if 0 /* See comment in __fxsave_clear() below. */ |
| 91 | : [fx] "r" (fx), "m" (*fx), "0" (0)); |
| 92 | #else |
| 93 | : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0)); |
| 94 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (unlikely(err)) |
| 96 | init_fpu(current); |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 | static inline int save_i387_checking(struct i387_fxsave_struct __user *fx) |
| 101 | { |
| 102 | int err; |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 103 | |
| 104 | asm volatile("1: rex64/fxsave (%[fx])\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | "2:\n" |
| 106 | ".section .fixup,\"ax\"\n" |
| 107 | "3: movl $-1,%[err]\n" |
| 108 | " jmp 2b\n" |
| 109 | ".previous\n" |
| 110 | ".section __ex_table,\"a\"\n" |
| 111 | " .align 8\n" |
| 112 | " .quad 1b,3b\n" |
| 113 | ".previous" |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 114 | : [err] "=r" (err), "=m" (*fx) |
| 115 | #if 0 /* See comment in __fxsave_clear() below. */ |
| 116 | : [fx] "r" (fx), "0" (0)); |
| 117 | #else |
| 118 | : [fx] "cdaSDb" (fx), "0" (0)); |
| 119 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | if (unlikely(err)) |
| 121 | __clear_user(fx, sizeof(struct i387_fxsave_struct)); |
| 122 | return err; |
| 123 | } |
| 124 | |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 125 | static inline void __fxsave_clear(struct task_struct *tsk) |
| 126 | { |
| 127 | /* Using "rex64; fxsave %0" is broken because, if the memory operand |
| 128 | uses any extended registers for addressing, a second REX prefix |
| 129 | will be generated (to the assembler, rex64 followed by semicolon |
| 130 | is a separate instruction), and hence the 64-bitness is lost. */ |
| 131 | #if 0 |
| 132 | /* Using "fxsaveq %0" would be the ideal choice, but is only supported |
| 133 | starting with gas 2.16. */ |
| 134 | __asm__ __volatile__("fxsaveq %0" |
| 135 | : "=m" (tsk->thread.i387.fxsave)); |
| 136 | #elif 0 |
| 137 | /* Using, as a workaround, the properly prefixed form below isn't |
| 138 | accepted by any binutils version so far released, complaining that |
| 139 | the same type of prefix is used twice if an extended register is |
| 140 | needed for addressing (fix submitted to mainline 2005-11-21). */ |
| 141 | __asm__ __volatile__("rex64/fxsave %0" |
| 142 | : "=m" (tsk->thread.i387.fxsave)); |
| 143 | #else |
| 144 | /* This, however, we can work around by forcing the compiler to select |
| 145 | an addressing mode that doesn't require extended registers. */ |
| 146 | __asm__ __volatile__("rex64/fxsave %P2(%1)" |
| 147 | : "=m" (tsk->thread.i387.fxsave) |
| 148 | : "cdaSDb" (tsk), |
| 149 | "i" (offsetof(__typeof__(*tsk), |
| 150 | thread.i387.fxsave))); |
| 151 | #endif |
| 152 | __asm__ __volatile__("fnclex"); |
| 153 | } |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | static inline void kernel_fpu_begin(void) |
| 156 | { |
| 157 | struct thread_info *me = current_thread_info(); |
| 158 | preempt_disable(); |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 159 | if (me->status & TS_USEDFPU) { |
| 160 | __fxsave_clear(me->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | me->status &= ~TS_USEDFPU; |
| 162 | return; |
| 163 | } |
| 164 | clts(); |
| 165 | } |
| 166 | |
| 167 | static inline void kernel_fpu_end(void) |
| 168 | { |
| 169 | stts(); |
| 170 | preempt_enable(); |
| 171 | } |
| 172 | |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame^] | 173 | static inline void save_init_fpu(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Jan Beulich | 7180d4f | 2006-01-11 22:43:36 +0100 | [diff] [blame] | 175 | __fxsave_clear(tsk); |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame^] | 176 | task_thread_info(tsk)->status &= ~TS_USEDFPU; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | stts(); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * This restores directly out of user space. Exceptions are handled. |
| 182 | */ |
| 183 | static inline int restore_i387(struct _fpstate __user *buf) |
| 184 | { |
| 185 | return restore_fpu_checking((__force struct i387_fxsave_struct *)buf); |
| 186 | } |
| 187 | |
| 188 | #endif /* __ASM_X86_64_I387_H */ |