blob: 487898923830fb4380cacd0cd70877305a8db9e4 [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
10#ifndef _ASM_X86_I387_H
11#define _ASM_X86_I387_H
12
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>
21
22extern void fpu_init(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010023extern void mxcsr_feature_mask_init(void);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070024extern int init_fpu(struct task_struct *child);
Roland McGrath1eeaed72008-01-30 13:31:51 +010025extern asmlinkage void math_state_restore(void);
Suresh Siddha61c46282008-03-10 15:28:04 -070026extern void init_thread_xstate(void);
Jaswinder Singh36454932008-07-21 22:31:57 +053027extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
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
39#ifdef CONFIG_X86_64
40
41/* Ignore delayed exceptions from user space */
42static inline void tolerant_fwait(void)
43{
44 asm volatile("1: fwait\n"
45 "2:\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070046 _ASM_EXTABLE(1b, 2b));
Roland McGrath1eeaed72008-01-30 13:31:51 +010047}
48
49static inline int restore_fpu_checking(struct i387_fxsave_struct *fx)
50{
51 int err;
52
53 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
54 "2:\n"
55 ".section .fixup,\"ax\"\n"
56 "3: movl $-1,%[err]\n"
57 " jmp 2b\n"
58 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070059 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +010060 : [err] "=r" (err)
61#if 0 /* See comment in __save_init_fpu() below. */
62 : [fx] "r" (fx), "m" (*fx), "0" (0));
63#else
64 : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
65#endif
66 if (unlikely(err))
67 init_fpu(current);
68 return err;
69}
70
71#define X87_FSW_ES (1 << 7) /* Exception Summary */
72
73/* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
74 is pending. Clear the x87 state here by setting it to fixed
75 values. The kernel data segment can be sometimes 0 and sometimes
76 new user value. Both should be ok.
77 Use the PDA as safe address because it should be already in L1. */
78static inline void clear_fpu_state(struct i387_fxsave_struct *fx)
79{
80 if (unlikely(fx->swd & X87_FSW_ES))
Joe Perchesaffe6632008-03-23 01:02:18 -070081 asm volatile("fnclex");
Roland McGrath1eeaed72008-01-30 13:31:51 +010082 alternative_input(ASM_NOP8 ASM_NOP2,
Joe Perchesaffe6632008-03-23 01:02:18 -070083 " emms\n" /* clear stack tags */
84 " fildl %%gs:0", /* load to clear state */
85 X86_FEATURE_FXSAVE_LEAK);
Roland McGrath1eeaed72008-01-30 13:31:51 +010086}
87
88static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
89{
90 int err;
91
92 asm volatile("1: rex64/fxsave (%[fx])\n\t"
93 "2:\n"
94 ".section .fixup,\"ax\"\n"
95 "3: movl $-1,%[err]\n"
96 " jmp 2b\n"
97 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070098 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +010099 : [err] "=r" (err), "=m" (*fx)
100#if 0 /* See comment in __fxsave_clear() below. */
101 : [fx] "r" (fx), "0" (0));
102#else
103 : [fx] "cdaSDb" (fx), "0" (0));
104#endif
Joe Perchesaffe6632008-03-23 01:02:18 -0700105 if (unlikely(err) &&
106 __clear_user(fx, sizeof(struct i387_fxsave_struct)))
Roland McGrath1eeaed72008-01-30 13:31:51 +0100107 err = -EFAULT;
108 /* No need to clear here because the caller clears USED_MATH */
109 return err;
110}
111
112static inline void __save_init_fpu(struct task_struct *tsk)
113{
114 /* Using "rex64; fxsave %0" is broken because, if the memory operand
115 uses any extended registers for addressing, a second REX prefix
116 will be generated (to the assembler, rex64 followed by semicolon
117 is a separate instruction), and hence the 64-bitness is lost. */
118#if 0
119 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
120 starting with gas 2.16. */
121 __asm__ __volatile__("fxsaveq %0"
Suresh Siddha61c46282008-03-10 15:28:04 -0700122 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100123#elif 0
124 /* Using, as a workaround, the properly prefixed form below isn't
125 accepted by any binutils version so far released, complaining that
126 the same type of prefix is used twice if an extended register is
127 needed for addressing (fix submitted to mainline 2005-11-21). */
128 __asm__ __volatile__("rex64/fxsave %0"
Suresh Siddha61c46282008-03-10 15:28:04 -0700129 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100130#else
131 /* This, however, we can work around by forcing the compiler to select
132 an addressing mode that doesn't require extended registers. */
Suresh Siddha61c46282008-03-10 15:28:04 -0700133 __asm__ __volatile__("rex64/fxsave (%1)"
134 : "=m" (tsk->thread.xstate->fxsave)
135 : "cdaSDb" (&tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100136#endif
Suresh Siddha61c46282008-03-10 15:28:04 -0700137 clear_fpu_state(&tsk->thread.xstate->fxsave);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100138 task_thread_info(tsk)->status &= ~TS_USEDFPU;
139}
140
141/*
142 * Signal frame handlers.
143 */
144
145static inline int save_i387(struct _fpstate __user *buf)
146{
147 struct task_struct *tsk = current;
148 int err = 0;
149
150 BUILD_BUG_ON(sizeof(struct user_i387_struct) !=
Suresh Siddha61c46282008-03-10 15:28:04 -0700151 sizeof(tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100152
153 if ((unsigned long)buf % 16)
154 printk("save_i387: bad fpstate %p\n", buf);
155
156 if (!used_math())
157 return 0;
158 clear_used_math(); /* trigger finit */
159 if (task_thread_info(tsk)->status & TS_USEDFPU) {
Joe Perchesaffe6632008-03-23 01:02:18 -0700160 err = save_i387_checking((struct i387_fxsave_struct __user *)
161 buf);
162 if (err)
163 return err;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100164 task_thread_info(tsk)->status &= ~TS_USEDFPU;
165 stts();
166 } else {
Suresh Siddha61c46282008-03-10 15:28:04 -0700167 if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
Roland McGrath1eeaed72008-01-30 13:31:51 +0100168 sizeof(struct i387_fxsave_struct)))
169 return -1;
170 }
171 return 1;
172}
173
174/*
175 * This restores directly out of user space. Exceptions are handled.
176 */
177static inline int restore_i387(struct _fpstate __user *buf)
178{
Suresh Siddhafd3c3ed2008-05-07 12:09:52 -0700179 struct task_struct *tsk = current;
180 int err;
181
182 if (!used_math()) {
183 err = init_fpu(tsk);
184 if (err)
185 return err;
186 }
187
Roland McGrath1eeaed72008-01-30 13:31:51 +0100188 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
189 clts();
190 task_thread_info(current)->status |= TS_USEDFPU;
191 }
192 return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
193}
194
195#else /* CONFIG_X86_32 */
196
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700197extern void finit(void);
198
Roland McGrath1eeaed72008-01-30 13:31:51 +0100199static inline void tolerant_fwait(void)
200{
201 asm volatile("fnclex ; fwait");
202}
203
204static inline void restore_fpu(struct task_struct *tsk)
205{
206 /*
207 * The "nop" is needed to make the instructions the same
208 * length.
209 */
210 alternative_input(
211 "nop ; frstor %1",
212 "fxrstor %1",
213 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700214 "m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100215}
216
217/* We need a safe address that is cheap to find and that is already
218 in L1 during context switch. The best choices are unfortunately
219 different for UP and SMP */
220#ifdef CONFIG_SMP
221#define safe_address (__per_cpu_offset[0])
222#else
223#define safe_address (kstat_cpu(0).cpustat.user)
224#endif
225
226/*
227 * These must be called with preempt disabled
228 */
229static inline void __save_init_fpu(struct task_struct *tsk)
230{
231 /* Use more nops than strictly needed in case the compiler
232 varies code */
233 alternative_input(
234 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
235 "fxsave %[fx]\n"
236 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
237 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700238 [fx] "m" (tsk->thread.xstate->fxsave),
239 [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
Roland McGrath1eeaed72008-01-30 13:31:51 +0100240 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
241 is pending. Clear the x87 state here by setting it to fixed
242 values. safe_address is a random variable that should be in L1 */
243 alternative_input(
244 GENERIC_NOP8 GENERIC_NOP2,
245 "emms\n\t" /* clear stack tags */
246 "fildl %[addr]", /* set F?P to defined value */
247 X86_FEATURE_FXSAVE_LEAK,
248 [addr] "m" (safe_address));
249 task_thread_info(tsk)->status &= ~TS_USEDFPU;
250}
251
252/*
253 * Signal frame handlers...
254 */
255extern int save_i387(struct _fpstate __user *buf);
256extern int restore_i387(struct _fpstate __user *buf);
257
258#endif /* CONFIG_X86_64 */
259
260static inline void __unlazy_fpu(struct task_struct *tsk)
261{
262 if (task_thread_info(tsk)->status & TS_USEDFPU) {
263 __save_init_fpu(tsk);
264 stts();
265 } else
266 tsk->fpu_counter = 0;
267}
268
269static inline void __clear_fpu(struct task_struct *tsk)
270{
271 if (task_thread_info(tsk)->status & TS_USEDFPU) {
272 tolerant_fwait();
273 task_thread_info(tsk)->status &= ~TS_USEDFPU;
274 stts();
275 }
276}
277
278static inline void kernel_fpu_begin(void)
279{
280 struct thread_info *me = current_thread_info();
281 preempt_disable();
282 if (me->status & TS_USEDFPU)
283 __save_init_fpu(me->task);
284 else
285 clts();
286}
287
288static inline void kernel_fpu_end(void)
289{
290 stts();
291 preempt_enable();
292}
293
294#ifdef CONFIG_X86_64
295
296static inline void save_init_fpu(struct task_struct *tsk)
297{
298 __save_init_fpu(tsk);
299 stts();
300}
301
302#define unlazy_fpu __unlazy_fpu
303#define clear_fpu __clear_fpu
304
305#else /* CONFIG_X86_32 */
306
307/*
308 * These disable preemption on their own and are safe
309 */
310static inline void save_init_fpu(struct task_struct *tsk)
311{
312 preempt_disable();
313 __save_init_fpu(tsk);
314 stts();
315 preempt_enable();
316}
317
318static inline void unlazy_fpu(struct task_struct *tsk)
319{
320 preempt_disable();
321 __unlazy_fpu(tsk);
322 preempt_enable();
323}
324
325static inline void clear_fpu(struct task_struct *tsk)
326{
327 preempt_disable();
328 __clear_fpu(tsk);
329 preempt_enable();
330}
331
332#endif /* CONFIG_X86_64 */
333
334/*
Roland McGrath1eeaed72008-01-30 13:31:51 +0100335 * i387 state interaction
336 */
337static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
338{
339 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700340 return tsk->thread.xstate->fxsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100341 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200342 return (unsigned short)tsk->thread.xstate->fsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100343 }
344}
345
346static inline unsigned short get_fpu_swd(struct task_struct *tsk)
347{
348 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700349 return tsk->thread.xstate->fxsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100350 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200351 return (unsigned short)tsk->thread.xstate->fsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100352 }
353}
354
355static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
356{
357 if (cpu_has_xmm) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700358 return tsk->thread.xstate->fxsave.mxcsr;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100359 } else {
360 return MXCSR_DEFAULT;
361 }
362}
363
364#endif /* _ASM_X86_I387_H */