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 <linux/hardirq.h>
18 #include <asm/processor.h>
19 #include <asm/sigcontext.h>
21 #include <asm/uaccess.h>
22 #include <asm/xsave.h>
24 extern unsigned int sig_xstate_size
;
25 extern void fpu_init(void);
26 extern void mxcsr_feature_mask_init(void);
27 extern int init_fpu(struct task_struct
*child
);
28 extern asmlinkage
void math_state_restore(void);
29 extern void __math_state_restore(void);
30 extern void init_thread_xstate(void);
31 extern int dump_fpu(struct pt_regs
*, struct user_i387_struct
*);
33 extern user_regset_active_fn fpregs_active
, xfpregs_active
;
34 extern user_regset_get_fn fpregs_get
, xfpregs_get
, fpregs_soft_get
;
35 extern user_regset_set_fn fpregs_set
, xfpregs_set
, fpregs_soft_set
;
37 extern struct _fpx_sw_bytes fx_sw_reserved
;
38 #ifdef CONFIG_IA32_EMULATION
39 extern unsigned int sig_xstate_ia32_size
;
40 extern struct _fpx_sw_bytes fx_sw_reserved_ia32
;
43 extern int save_i387_xstate_ia32(void __user
*buf
);
44 extern int restore_i387_xstate_ia32(void __user
*buf
);
47 #define X87_FSW_ES (1 << 7) /* Exception Summary */
51 /* Ignore delayed exceptions from user space */
52 static inline void tolerant_fwait(void)
54 asm volatile("1: fwait\n"
56 _ASM_EXTABLE(1b
, 2b
));
59 static inline int fxrstor_checking(struct i387_fxsave_struct
*fx
)
63 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
65 ".section .fixup,\"ax\"\n"
66 "3: movl $-1,%[err]\n"
71 #if 0 /* See comment in fxsave() below. */
72 : [fx
] "r" (fx
), "m" (*fx
), "0" (0));
74 : [fx
] "cdaSDb" (fx
), "m" (*fx
), "0" (0));
79 /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
80 is pending. Clear the x87 state here by setting it to fixed
81 values. The kernel data segment can be sometimes 0 and sometimes
82 new user value. Both should be ok.
83 Use the PDA as safe address because it should be already in L1. */
84 static inline void clear_fpu_state(struct task_struct
*tsk
)
86 struct xsave_struct
*xstate
= &tsk
->thread
.xstate
->xsave
;
87 struct i387_fxsave_struct
*fx
= &tsk
->thread
.xstate
->fxsave
;
90 * xsave header may indicate the init state of the FP.
92 if ((task_thread_info(tsk
)->status
& TS_XSAVE
) &&
93 !(xstate
->xsave_hdr
.xstate_bv
& XSTATE_FP
))
96 if (unlikely(fx
->swd
& X87_FSW_ES
))
97 asm volatile("fnclex");
98 alternative_input(ASM_NOP8 ASM_NOP2
,
99 " emms\n" /* clear stack tags */
100 " fildl %%gs:0", /* load to clear state */
101 X86_FEATURE_FXSAVE_LEAK
);
104 static inline int fxsave_user(struct i387_fxsave_struct __user
*fx
)
108 asm volatile("1: rex64/fxsave (%[fx])\n\t"
110 ".section .fixup,\"ax\"\n"
111 "3: movl $-1,%[err]\n"
115 : [err
] "=r" (err
), "=m" (*fx
)
116 #if 0 /* See comment in fxsave() below. */
117 : [fx
] "r" (fx
), "0" (0));
119 : [fx
] "cdaSDb" (fx
), "0" (0));
122 __clear_user(fx
, sizeof(struct i387_fxsave_struct
)))
124 /* No need to clear here because the caller clears USED_MATH */
128 static inline void fxsave(struct task_struct
*tsk
)
130 /* Using "rex64; fxsave %0" is broken because, if the memory operand
131 uses any extended registers for addressing, a second REX prefix
132 will be generated (to the assembler, rex64 followed by semicolon
133 is a separate instruction), and hence the 64-bitness is lost. */
135 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
136 starting with gas 2.16. */
137 __asm__
__volatile__("fxsaveq %0"
138 : "=m" (tsk
->thread
.xstate
->fxsave
));
140 /* Using, as a workaround, the properly prefixed form below isn't
141 accepted by any binutils version so far released, complaining that
142 the same type of prefix is used twice if an extended register is
143 needed for addressing (fix submitted to mainline 2005-11-21). */
144 __asm__
__volatile__("rex64/fxsave %0"
145 : "=m" (tsk
->thread
.xstate
->fxsave
));
147 /* This, however, we can work around by forcing the compiler to select
148 an addressing mode that doesn't require extended registers. */
149 __asm__
__volatile__("rex64/fxsave (%1)"
150 : "=m" (tsk
->thread
.xstate
->fxsave
)
151 : "cdaSDb" (&tsk
->thread
.xstate
->fxsave
));
155 static inline void __save_init_fpu(struct task_struct
*tsk
)
157 if (task_thread_info(tsk
)->status
& TS_XSAVE
)
162 clear_fpu_state(tsk
);
163 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
166 #else /* CONFIG_X86_32 */
168 #ifdef CONFIG_MATH_EMULATION
169 extern void finit_task(struct task_struct
*tsk
);
171 static inline void finit_task(struct task_struct
*tsk
)
176 static inline void tolerant_fwait(void)
178 asm volatile("fnclex ; fwait");
181 /* perform fxrstor iff the processor has extended states, otherwise frstor */
182 static inline int fxrstor_checking(struct i387_fxsave_struct
*fx
)
185 * The "nop" is needed to make the instructions the same
197 /* We need a safe address that is cheap to find and that is already
198 in L1 during context switch. The best choices are unfortunately
199 different for UP and SMP */
201 #define safe_address (__per_cpu_offset[0])
203 #define safe_address (kstat_cpu(0).cpustat.user)
207 * These must be called with preempt disabled
209 static inline void __save_init_fpu(struct task_struct
*tsk
)
211 if (task_thread_info(tsk
)->status
& TS_XSAVE
) {
212 struct xsave_struct
*xstate
= &tsk
->thread
.xstate
->xsave
;
213 struct i387_fxsave_struct
*fx
= &tsk
->thread
.xstate
->fxsave
;
218 * xsave header may indicate the init state of the FP.
220 if (!(xstate
->xsave_hdr
.xstate_bv
& XSTATE_FP
))
223 if (unlikely(fx
->swd
& X87_FSW_ES
))
224 asm volatile("fnclex");
227 * we can do a simple return here or be paranoid :)
232 /* Use more nops than strictly needed in case the compiler
235 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4
,
237 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
239 [fx
] "m" (tsk
->thread
.xstate
->fxsave
),
240 [fsw
] "m" (tsk
->thread
.xstate
->fxsave
.swd
) : "memory");
242 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
243 is pending. Clear the x87 state here by setting it to fixed
244 values. safe_address is a random variable that should be in L1 */
246 GENERIC_NOP8 GENERIC_NOP2
,
247 "emms\n\t" /* clear stack tags */
248 "fildl %[addr]", /* set F?P to defined value */
249 X86_FEATURE_FXSAVE_LEAK
,
250 [addr
] "m" (safe_address
));
252 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
255 #endif /* CONFIG_X86_64 */
257 static inline int restore_fpu_checking(struct task_struct
*tsk
)
259 if (task_thread_info(tsk
)->status
& TS_XSAVE
)
260 return xrstor_checking(&tsk
->thread
.xstate
->xsave
);
262 return fxrstor_checking(&tsk
->thread
.xstate
->fxsave
);
266 * Signal frame handlers...
268 extern int save_i387_xstate(void __user
*buf
);
269 extern int restore_i387_xstate(void __user
*buf
);
271 static inline void __unlazy_fpu(struct task_struct
*tsk
)
273 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
274 __save_init_fpu(tsk
);
277 tsk
->fpu_counter
= 0;
280 static inline void __clear_fpu(struct task_struct
*tsk
)
282 if (task_thread_info(tsk
)->status
& TS_USEDFPU
) {
284 task_thread_info(tsk
)->status
&= ~TS_USEDFPU
;
289 static inline void kernel_fpu_begin(void)
291 struct thread_info
*me
= current_thread_info();
293 if (me
->status
& TS_USEDFPU
)
294 __save_init_fpu(me
->task
);
299 static inline void kernel_fpu_end(void)
305 static inline bool irq_fpu_usable(void)
307 struct pt_regs
*regs
;
309 return !in_interrupt() || !(regs
= get_irq_regs()) || \
310 user_mode(regs
) || (read_cr0() & X86_CR0_TS
);
314 * Some instructions like VIA's padlock instructions generate a spurious
315 * DNA fault but don't modify SSE registers. And these instructions
316 * get used from interrupt context as well. To prevent these kernel instructions
317 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
318 * should use them only in the context of irq_ts_save/restore()
320 static inline int irq_ts_save(void)
323 * If in process context and not atomic, we can take a spurious DNA fault.
324 * Otherwise, doing clts() in process context requires disabling preemption
325 * or some heavy lifting like kernel_fpu_begin()
330 if (read_cr0() & X86_CR0_TS
) {
338 static inline void irq_ts_restore(int TS_state
)
346 static inline void save_init_fpu(struct task_struct
*tsk
)
348 __save_init_fpu(tsk
);
352 #define unlazy_fpu __unlazy_fpu
353 #define clear_fpu __clear_fpu
355 #else /* CONFIG_X86_32 */
358 * These disable preemption on their own and are safe
360 static inline void save_init_fpu(struct task_struct
*tsk
)
363 __save_init_fpu(tsk
);
368 static inline void unlazy_fpu(struct task_struct
*tsk
)
375 static inline void clear_fpu(struct task_struct
*tsk
)
382 #endif /* CONFIG_X86_64 */
385 * i387 state interaction
387 static inline unsigned short get_fpu_cwd(struct task_struct
*tsk
)
390 return tsk
->thread
.xstate
->fxsave
.cwd
;
392 return (unsigned short)tsk
->thread
.xstate
->fsave
.cwd
;
396 static inline unsigned short get_fpu_swd(struct task_struct
*tsk
)
399 return tsk
->thread
.xstate
->fxsave
.swd
;
401 return (unsigned short)tsk
->thread
.xstate
->fsave
.swd
;
405 static inline unsigned short get_fpu_mxcsr(struct task_struct
*tsk
)
408 return tsk
->thread
.xstate
->fxsave
.mxcsr
;
410 return MXCSR_DEFAULT
;
414 #endif /* _ASM_X86_I387_H */