blob: a6d256f4ac81a22b7188fbca756fb9c174652af9 [file] [log] [blame]
Roland McGrath1eeaed72008-01-30 13:31:51 +01001/*
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
Vegard Nossum77ef50a2008-06-18 17:08:48 +020010#ifndef ASM_X86__I387_H
11#define ASM_X86__I387_H
Roland McGrath1eeaed72008-01-30 13:31:51 +010012
13#include <linux/sched.h>
14#include <linux/kernel_stat.h>
15#include <linux/regset.h>
H. Peter Anvin92c37fa2008-02-04 16:47:58 +010016#include <asm/asm.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010017#include <asm/processor.h>
18#include <asm/sigcontext.h>
19#include <asm/user.h>
20#include <asm/uaccess.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070021#include <asm/xsave.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010022
23extern void fpu_init(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010024extern void mxcsr_feature_mask_init(void);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070025extern int init_fpu(struct task_struct *child);
Roland McGrath1eeaed72008-01-30 13:31:51 +010026extern asmlinkage void math_state_restore(void);
Suresh Siddha61c46282008-03-10 15:28:04 -070027extern void init_thread_xstate(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010028
29extern user_regset_active_fn fpregs_active, xfpregs_active;
30extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
31extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
32
33#ifdef CONFIG_IA32_EMULATION
34struct _fpstate_ia32;
35extern int save_i387_ia32(struct _fpstate_ia32 __user *buf);
36extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf);
37#endif
38
Suresh Siddhab359e8a2008-07-29 10:29:20 -070039#define X87_FSW_ES (1 << 7) /* Exception Summary */
40
Roland McGrath1eeaed72008-01-30 13:31:51 +010041#ifdef CONFIG_X86_64
42
43/* Ignore delayed exceptions from user space */
44static inline void tolerant_fwait(void)
45{
46 asm volatile("1: fwait\n"
47 "2:\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070048 _ASM_EXTABLE(1b, 2b));
Roland McGrath1eeaed72008-01-30 13:31:51 +010049}
50
Suresh Siddhab359e8a2008-07-29 10:29:20 -070051static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +010052{
53 int err;
54
55 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
56 "2:\n"
57 ".section .fixup,\"ax\"\n"
58 "3: movl $-1,%[err]\n"
59 " jmp 2b\n"
60 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070061 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +010062 : [err] "=r" (err)
63#if 0 /* See comment in __save_init_fpu() below. */
64 : [fx] "r" (fx), "m" (*fx), "0" (0));
65#else
66 : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
67#endif
Roland McGrath1eeaed72008-01-30 13:31:51 +010068 return err;
69}
70
Suresh Siddhab359e8a2008-07-29 10:29:20 -070071static inline int restore_fpu_checking(struct task_struct *tsk)
72{
73 if (task_thread_info(tsk)->status & TS_XSAVE)
74 return xrstor_checking(&tsk->thread.xstate->xsave);
75 else
76 return fxrstor_checking(&tsk->thread.xstate->fxsave);
77}
Roland McGrath1eeaed72008-01-30 13:31:51 +010078
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. */
Suresh Siddhab359e8a2008-07-29 10:29:20 -070084static inline void clear_fpu_state(struct task_struct *tsk)
Roland McGrath1eeaed72008-01-30 13:31:51 +010085{
Suresh Siddhab359e8a2008-07-29 10:29:20 -070086 struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
87 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
88
89 /*
90 * xsave header may indicate the init state of the FP.
91 */
92 if ((task_thread_info(tsk)->status & TS_XSAVE) &&
93 !(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
94 return;
95
Roland McGrath1eeaed72008-01-30 13:31:51 +010096 if (unlikely(fx->swd & X87_FSW_ES))
Joe Perchesaffe6632008-03-23 01:02:18 -070097 asm volatile("fnclex");
Roland McGrath1eeaed72008-01-30 13:31:51 +010098 alternative_input(ASM_NOP8 ASM_NOP2,
Joe Perchesaffe6632008-03-23 01:02:18 -070099 " emms\n" /* clear stack tags */
100 " fildl %%gs:0", /* load to clear state */
101 X86_FEATURE_FXSAVE_LEAK);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100102}
103
104static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
105{
106 int err;
107
108 asm volatile("1: rex64/fxsave (%[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 Perchesaffe6632008-03-23 01:02:18 -0700114 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100115 : [err] "=r" (err), "=m" (*fx)
116#if 0 /* See comment in __fxsave_clear() below. */
117 : [fx] "r" (fx), "0" (0));
118#else
119 : [fx] "cdaSDb" (fx), "0" (0));
120#endif
Joe Perchesaffe6632008-03-23 01:02:18 -0700121 if (unlikely(err) &&
122 __clear_user(fx, sizeof(struct i387_fxsave_struct)))
Roland McGrath1eeaed72008-01-30 13:31:51 +0100123 err = -EFAULT;
124 /* No need to clear here because the caller clears USED_MATH */
125 return err;
126}
127
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700128static inline void fxsave(struct task_struct *tsk)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100129{
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. */
134#if 0
135 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
136 starting with gas 2.16. */
137 __asm__ __volatile__("fxsaveq %0"
Suresh Siddha61c46282008-03-10 15:28:04 -0700138 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100139#elif 0
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"
Suresh Siddha61c46282008-03-10 15:28:04 -0700145 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100146#else
147 /* This, however, we can work around by forcing the compiler to select
148 an addressing mode that doesn't require extended registers. */
Suresh Siddha61c46282008-03-10 15:28:04 -0700149 __asm__ __volatile__("rex64/fxsave (%1)"
150 : "=m" (tsk->thread.xstate->fxsave)
151 : "cdaSDb" (&tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100152#endif
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700153}
154
155static inline void __save_init_fpu(struct task_struct *tsk)
156{
157 if (task_thread_info(tsk)->status & TS_XSAVE)
158 xsave(tsk);
159 else
160 fxsave(tsk);
161
162 clear_fpu_state(tsk);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100163 task_thread_info(tsk)->status &= ~TS_USEDFPU;
164}
165
Roland McGrath1eeaed72008-01-30 13:31:51 +0100166#else /* CONFIG_X86_32 */
167
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700168extern void finit(void);
169
Roland McGrath1eeaed72008-01-30 13:31:51 +0100170static inline void tolerant_fwait(void)
171{
172 asm volatile("fnclex ; fwait");
173}
174
175static inline void restore_fpu(struct task_struct *tsk)
176{
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700177 if (task_thread_info(tsk)->status & TS_XSAVE) {
178 xrstor_checking(&tsk->thread.xstate->xsave);
179 return;
180 }
Roland McGrath1eeaed72008-01-30 13:31:51 +0100181 /*
182 * The "nop" is needed to make the instructions the same
183 * length.
184 */
185 alternative_input(
186 "nop ; frstor %1",
187 "fxrstor %1",
188 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700189 "m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100190}
191
192/* We need a safe address that is cheap to find and that is already
193 in L1 during context switch. The best choices are unfortunately
194 different for UP and SMP */
195#ifdef CONFIG_SMP
196#define safe_address (__per_cpu_offset[0])
197#else
198#define safe_address (kstat_cpu(0).cpustat.user)
199#endif
200
201/*
202 * These must be called with preempt disabled
203 */
204static inline void __save_init_fpu(struct task_struct *tsk)
205{
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700206 if (task_thread_info(tsk)->status & TS_XSAVE) {
207 struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
208 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
209
210 xsave(tsk);
211
212 /*
213 * xsave header may indicate the init state of the FP.
214 */
215 if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
216 goto end;
217
218 if (unlikely(fx->swd & X87_FSW_ES))
219 asm volatile("fnclex");
220
221 /*
222 * we can do a simple return here or be paranoid :)
223 */
224 goto clear_state;
225 }
226
Roland McGrath1eeaed72008-01-30 13:31:51 +0100227 /* Use more nops than strictly needed in case the compiler
228 varies code */
229 alternative_input(
230 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
231 "fxsave %[fx]\n"
232 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
233 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700234 [fx] "m" (tsk->thread.xstate->fxsave),
235 [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700236clear_state:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100237 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
238 is pending. Clear the x87 state here by setting it to fixed
239 values. safe_address is a random variable that should be in L1 */
240 alternative_input(
241 GENERIC_NOP8 GENERIC_NOP2,
242 "emms\n\t" /* clear stack tags */
243 "fildl %[addr]", /* set F?P to defined value */
244 X86_FEATURE_FXSAVE_LEAK,
245 [addr] "m" (safe_address));
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700246end:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100247 task_thread_info(tsk)->status &= ~TS_USEDFPU;
248}
249
250/*
251 * Signal frame handlers...
252 */
253extern int save_i387(struct _fpstate __user *buf);
254extern int restore_i387(struct _fpstate __user *buf);
255
256#endif /* CONFIG_X86_64 */
257
258static inline void __unlazy_fpu(struct task_struct *tsk)
259{
260 if (task_thread_info(tsk)->status & TS_USEDFPU) {
261 __save_init_fpu(tsk);
262 stts();
263 } else
264 tsk->fpu_counter = 0;
265}
266
267static inline void __clear_fpu(struct task_struct *tsk)
268{
269 if (task_thread_info(tsk)->status & TS_USEDFPU) {
270 tolerant_fwait();
271 task_thread_info(tsk)->status &= ~TS_USEDFPU;
272 stts();
273 }
274}
275
276static inline void kernel_fpu_begin(void)
277{
278 struct thread_info *me = current_thread_info();
279 preempt_disable();
280 if (me->status & TS_USEDFPU)
281 __save_init_fpu(me->task);
282 else
283 clts();
284}
285
286static inline void kernel_fpu_end(void)
287{
288 stts();
289 preempt_enable();
290}
291
292#ifdef CONFIG_X86_64
293
294static inline void save_init_fpu(struct task_struct *tsk)
295{
296 __save_init_fpu(tsk);
297 stts();
298}
299
300#define unlazy_fpu __unlazy_fpu
301#define clear_fpu __clear_fpu
302
303#else /* CONFIG_X86_32 */
304
305/*
306 * These disable preemption on their own and are safe
307 */
308static inline void save_init_fpu(struct task_struct *tsk)
309{
310 preempt_disable();
311 __save_init_fpu(tsk);
312 stts();
313 preempt_enable();
314}
315
316static inline void unlazy_fpu(struct task_struct *tsk)
317{
318 preempt_disable();
319 __unlazy_fpu(tsk);
320 preempt_enable();
321}
322
323static inline void clear_fpu(struct task_struct *tsk)
324{
325 preempt_disable();
326 __clear_fpu(tsk);
327 preempt_enable();
328}
329
330#endif /* CONFIG_X86_64 */
331
332/*
Roland McGrath1eeaed72008-01-30 13:31:51 +0100333 * i387 state interaction
334 */
335static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
336{
337 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700338 return tsk->thread.xstate->fxsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100339 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200340 return (unsigned short)tsk->thread.xstate->fsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100341 }
342}
343
344static inline unsigned short get_fpu_swd(struct task_struct *tsk)
345{
346 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700347 return tsk->thread.xstate->fxsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100348 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200349 return (unsigned short)tsk->thread.xstate->fsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100350 }
351}
352
353static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
354{
355 if (cpu_has_xmm) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700356 return tsk->thread.xstate->fxsave.mxcsr;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100357 } else {
358 return MXCSR_DEFAULT;
359 }
360}
361
Vegard Nossum77ef50a2008-06-18 17:08:48 +0200362#endif /* ASM_X86__I387_H */