2 * Copyright (C) 1994 Linus Torvalds
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
10 #ifndef _ASM_X86_I387_H
11 #define _ASM_X86_I387_H
13 #include <linux/sched.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/regset.h>
16 #include <asm/processor.h>
17 #include <asm/sigcontext.h>
19 #include <asm/uaccess.h>
21 extern void fpu_init(void);
22 extern unsigned int mxcsr_feature_mask
;
23 extern void mxcsr_feature_mask_init(void);
24 extern void init_fpu(struct task_struct
*child
);
25 extern asmlinkage
void math_state_restore(void);
27 extern user_regset_active_fn fpregs_active
, xfpregs_active
;
28 extern user_regset_get_fn fpregs_get
, xfpregs_get
, fpregs_soft_get
;
29 extern user_regset_set_fn fpregs_set
, xfpregs_set
, fpregs_soft_set
;
31 #ifdef CONFIG_IA32_EMULATION
33 extern int save_i387_ia32(struct _fpstate_ia32 __user
*buf
);
34 extern int restore_i387_ia32(struct _fpstate_ia32 __user
*buf
);
39 /* Ignore delayed exceptions from user space */
40 static inline void tolerant_fwait(void)
42 asm volatile("1: fwait\n"
44 " .section __ex_table,\"a\"\n"
50 static inline int restore_fpu_checking(struct i387_fxsave_struct
*fx
)
54 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
56 ".section .fixup,\"ax\"\n"
57 "3: movl $-1,%[err]\n"
60 ".section __ex_table,\"a\"\n"
65 #if 0 /* See comment in __save_init_fpu() below. */
66 : [fx
] "r" (fx
), "m" (*fx
), "0" (0));
68 : [fx
] "cdaSDb" (fx
), "m" (*fx
), "0" (0));
75 #define X87_FSW_ES (1 << 7) /* Exception Summary */
77 /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
78 is pending. Clear the x87 state here by setting it to fixed
79 values. The kernel data segment can be sometimes 0 and sometimes
80 new user value. Both should be ok.
81 Use the PDA as safe address because it should be already in L1. */
82 static inline void clear_fpu_state(struct i387_fxsave_struct
*fx
)
84 if (unlikely(fx
->swd
& X87_FSW_ES
))
85 asm volatile("fnclex");
86 alternative_input(ASM_NOP8 ASM_NOP2
,
87 " emms\n" /* clear stack tags */
88 " fildl %%gs:0", /* load to clear state */
89 X86_FEATURE_FXSAVE_LEAK
);
92 static inline int save_i387_checking(struct i387_fxsave_struct __user
*fx
)
96 asm volatile("1: rex64/fxsave (%[fx])\n\t"
98 ".section .fixup,\"ax\"\n"
99 "3: movl $-1,%[err]\n"
102 ".section __ex_table,\"a\"\n"
106 : [err
] "=r" (err
), "=m" (*fx
)
107 #if 0 /* See comment in __fxsave_clear() below. */
108 : [fx
] "r" (fx
), "0" (0));
110 : [fx
] "cdaSDb" (fx
), "0" (0));
112 if (unlikely(err
) && __clear_user(fx
, sizeof(struct i387_fxsave_struct
)))
114 /* No need to clear here because the caller clears USED_MATH */
118 static inline void __save_init_fpu(struct task_struct
*tsk
)
120 /* Using "rex64; fxsave %0" is broken because, if the memory operand
121 uses any extended registers for addressing, a second REX prefix
122 will be generated (to the assembler, rex64 followed by semicolon
123 is a separate instruction), and hence the 64-bitness is lost. */
125 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
126 starting with gas 2.16. */
127 __asm__
__volatile__("fxsaveq %0"
128 : "=m" (tsk
->thread
.i387
.fxsave
));
130 /* Using, as a workaround, the properly prefixed form below isn't
131 accepted by any binutils version so far released, complaining that
132 the same type of prefix is used twice if an extended register is
133 needed for addressing (fix submitted to mainline 2005-11-21). */
134 __asm__
__volatile__("rex64/fxsave %0"
135 : "=m" (tsk
->thread
.i387
.fxsave
));
137 /* This, however, we can work around by forcing the compiler to select
138 an addressing mode that doesn't require extended registers. */
139 __asm__
__volatile__("rex64/fxsave %P2(%1)"
140 : "=m" (tsk
->thread
.i387
.fxsave
)
142 "i" (offsetof(__typeof__(*tsk
),
143 thread
.i387
.fxsave
)));
145 clear_fpu_state(&tsk
->thread
.i387
.fxsave
);
146 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
150 * Signal frame handlers.
153 static inline int save_i387(struct _fpstate __user
*buf
)
155 struct task_struct
*tsk
= current
;
158 BUILD_BUG_ON(sizeof(struct user_i387_struct
) !=
159 sizeof(tsk
->thread
.i387
.fxsave
));
161 if ((unsigned long)buf
% 16)
162 printk("save_i387: bad fpstate %p\n", buf
);
166 clear_used_math(); /* trigger finit */
167 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
168 err
= save_i387_checking((struct i387_fxsave_struct __user
*)buf
);
170 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
173 if (__copy_to_user(buf
, &tsk
->thread
.i387
.fxsave
,
174 sizeof(struct i387_fxsave_struct
)))
181 * This restores directly out of user space. Exceptions are handled.
183 static inline int restore_i387(struct _fpstate __user
*buf
)
186 if (!(task_thread_info(current
)->status
& TS_USEDFPU
)) {
188 task_thread_info(current
)->status
|= TS_USEDFPU
;
190 return restore_fpu_checking((__force
struct i387_fxsave_struct
*)buf
);
193 #else /* CONFIG_X86_32 */
195 static inline void tolerant_fwait(void)
197 asm volatile("fnclex ; fwait");
200 static inline void restore_fpu(struct task_struct
*tsk
)
203 * The "nop" is needed to make the instructions the same
210 "m" ((tsk
)->thread
.i387
.fxsave
));
213 /* We need a safe address that is cheap to find and that is already
214 in L1 during context switch. The best choices are unfortunately
215 different for UP and SMP */
217 #define safe_address (__per_cpu_offset[0])
219 #define safe_address (kstat_cpu(0).cpustat.user)
223 * These must be called with preempt disabled
225 static inline void __save_init_fpu(struct task_struct
*tsk
)
227 /* Use more nops than strictly needed in case the compiler
230 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4
,
232 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
234 [fx
] "m" (tsk
->thread
.i387
.fxsave
),
235 [fsw
] "m" (tsk
->thread
.i387
.fxsave
.swd
) : "memory");
236 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
237 is pending. Clear the x87 state here by setting it to fixed
238 values. safe_address is a random variable that should be in L1 */
240 GENERIC_NOP8 GENERIC_NOP2
,
241 "emms\n\t" /* clear stack tags */
242 "fildl %[addr]", /* set F?P to defined value */
243 X86_FEATURE_FXSAVE_LEAK
,
244 [addr
] "m" (safe_address
));
245 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
249 * Signal frame handlers...
251 extern int save_i387(struct _fpstate __user
*buf
);
252 extern int restore_i387(struct _fpstate __user
*buf
);
254 #endif /* CONFIG_X86_64 */
256 static inline void __unlazy_fpu(struct task_struct
*tsk
)
258 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
259 __save_init_fpu(tsk
);
262 tsk
->fpu_counter
= 0;
265 static inline void __clear_fpu(struct task_struct
*tsk
)
267 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
269 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
274 static inline void kernel_fpu_begin(void)
276 struct thread_info
*me
= current_thread_info();
278 if (me
->status
& TS_USEDFPU
)
279 __save_init_fpu(me
->task
);
284 static inline void kernel_fpu_end(void)
292 static inline void save_init_fpu(struct task_struct
*tsk
)
294 __save_init_fpu(tsk
);
298 #define unlazy_fpu __unlazy_fpu
299 #define clear_fpu __clear_fpu
301 #else /* CONFIG_X86_32 */
304 * These disable preemption on their own and are safe
306 static inline void save_init_fpu(struct task_struct
*tsk
)
309 __save_init_fpu(tsk
);
314 static inline void unlazy_fpu(struct task_struct
*tsk
)
321 static inline void clear_fpu(struct task_struct
*tsk
)
328 #endif /* CONFIG_X86_64 */
331 * i387 state interaction
333 static inline unsigned short get_fpu_cwd(struct task_struct
*tsk
)
336 return tsk
->thread
.i387
.fxsave
.cwd
;
338 return (unsigned short)tsk
->thread
.i387
.fsave
.cwd
;
342 static inline unsigned short get_fpu_swd(struct task_struct
*tsk
)
345 return tsk
->thread
.i387
.fxsave
.swd
;
347 return (unsigned short)tsk
->thread
.i387
.fsave
.swd
;
351 static inline unsigned short get_fpu_mxcsr(struct task_struct
*tsk
)
354 return tsk
->thread
.i387
.fxsave
.mxcsr
;
356 return MXCSR_DEFAULT
;
360 #endif /* _ASM_X86_I387_H */