blob: 2e7529295f5539fbeb2310c58939c370bc9edbe0 [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
H. Peter Anvin1965aae2008-10-22 22:26:29 -070010#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>
Suresh Siddhae4914012008-08-13 22:02:26 +100016#include <linux/hardirq.h>
H. Peter Anvin92c37fa2008-02-04 16:47:58 +010017#include <asm/asm.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010018#include <asm/processor.h>
19#include <asm/sigcontext.h>
20#include <asm/user.h>
21#include <asm/uaccess.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070022#include <asm/xsave.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010023
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070024extern unsigned int sig_xstate_size;
Roland McGrath1eeaed72008-01-30 13:31:51 +010025extern void fpu_init(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010026extern void mxcsr_feature_mask_init(void);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070027extern int init_fpu(struct task_struct *child);
Roland McGrath1eeaed72008-01-30 13:31:51 +010028extern asmlinkage void math_state_restore(void);
Jeremy Fitzhardingee6e9cac2009-04-24 00:40:59 -070029extern void __math_state_restore(void);
Suresh Siddha61c46282008-03-10 15:28:04 -070030extern void init_thread_xstate(void);
Jaswinder Singh36454932008-07-21 22:31:57 +053031extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Roland McGrath1eeaed72008-01-30 13:31:51 +010032
33extern user_regset_active_fn fpregs_active, xfpregs_active;
34extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get;
35extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set;
36
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070037extern struct _fpx_sw_bytes fx_sw_reserved;
Roland McGrath1eeaed72008-01-30 13:31:51 +010038#ifdef CONFIG_IA32_EMULATION
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070039extern unsigned int sig_xstate_ia32_size;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070040extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
Roland McGrath1eeaed72008-01-30 13:31:51 +010041struct _fpstate_ia32;
Suresh Siddhaab513702008-07-29 10:29:22 -070042struct _xstate_ia32;
43extern int save_i387_xstate_ia32(void __user *buf);
44extern int restore_i387_xstate_ia32(void __user *buf);
Roland McGrath1eeaed72008-01-30 13:31:51 +010045#endif
46
Suresh Siddhab359e8a2008-07-29 10:29:20 -070047#define X87_FSW_ES (1 << 7) /* Exception Summary */
48
Roland McGrath1eeaed72008-01-30 13:31:51 +010049#ifdef CONFIG_X86_64
50
51/* Ignore delayed exceptions from user space */
52static inline void tolerant_fwait(void)
53{
54 asm volatile("1: fwait\n"
55 "2:\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070056 _ASM_EXTABLE(1b, 2b));
Roland McGrath1eeaed72008-01-30 13:31:51 +010057}
58
Suresh Siddhab359e8a2008-07-29 10:29:20 -070059static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +010060{
61 int err;
62
63 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
64 "2:\n"
65 ".section .fixup,\"ax\"\n"
66 "3: movl $-1,%[err]\n"
67 " jmp 2b\n"
68 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070069 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +010070 : [err] "=r" (err)
Jiri Slaby4ecf4582009-04-08 13:32:00 +020071#if 0 /* See comment in fxsave() below. */
Roland McGrath1eeaed72008-01-30 13:31:51 +010072 : [fx] "r" (fx), "m" (*fx), "0" (0));
73#else
74 : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
75#endif
Roland McGrath1eeaed72008-01-30 13:31:51 +010076 return err;
77}
78
Roland McGrath1eeaed72008-01-30 13:31:51 +010079/* 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
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700104static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100105{
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)
Jiri Slaby4ecf4582009-04-08 13:32:00 +0200116#if 0 /* See comment in fxsave() below. */
Roland McGrath1eeaed72008-01-30 13:31:51 +0100117 : [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
Daniel Glöcknerab9e1852009-03-04 19:42:27 +0100168#ifdef CONFIG_MATH_EMULATION
169extern void finit_task(struct task_struct *tsk);
170#else
171static inline void finit_task(struct task_struct *tsk)
172{
173}
174#endif
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700175
Roland McGrath1eeaed72008-01-30 13:31:51 +0100176static inline void tolerant_fwait(void)
177{
178 asm volatile("fnclex ; fwait");
179}
180
Jiri Slaby34ba4762009-04-08 13:31:59 +0200181/* perform fxrstor iff the processor has extended states, otherwise frstor */
182static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100183{
184 /*
185 * The "nop" is needed to make the instructions the same
186 * length.
187 */
188 alternative_input(
189 "nop ; frstor %1",
190 "fxrstor %1",
191 X86_FEATURE_FXSR,
Jiri Slaby34ba4762009-04-08 13:31:59 +0200192 "m" (*fx));
193
Jiri Slabyfcb2ac52009-04-08 13:31:58 +0200194 return 0;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100195}
196
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 */
200#ifdef CONFIG_SMP
201#define safe_address (__per_cpu_offset[0])
202#else
203#define safe_address (kstat_cpu(0).cpustat.user)
204#endif
205
206/*
207 * These must be called with preempt disabled
208 */
209static inline void __save_init_fpu(struct task_struct *tsk)
210{
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700211 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;
214
215 xsave(tsk);
216
217 /*
218 * xsave header may indicate the init state of the FP.
219 */
220 if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
221 goto end;
222
223 if (unlikely(fx->swd & X87_FSW_ES))
224 asm volatile("fnclex");
225
226 /*
227 * we can do a simple return here or be paranoid :)
228 */
229 goto clear_state;
230 }
231
Roland McGrath1eeaed72008-01-30 13:31:51 +0100232 /* Use more nops than strictly needed in case the compiler
233 varies code */
234 alternative_input(
235 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
236 "fxsave %[fx]\n"
237 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
238 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700239 [fx] "m" (tsk->thread.xstate->fxsave),
240 [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700241clear_state:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100242 /* 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 */
245 alternative_input(
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));
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700251end:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100252 task_thread_info(tsk)->status &= ~TS_USEDFPU;
253}
254
Suresh Siddhaab513702008-07-29 10:29:22 -0700255#endif /* CONFIG_X86_64 */
256
Jiri Slaby34ba4762009-04-08 13:31:59 +0200257static inline int restore_fpu_checking(struct task_struct *tsk)
258{
259 if (task_thread_info(tsk)->status & TS_XSAVE)
260 return xrstor_checking(&tsk->thread.xstate->xsave);
261 else
262 return fxrstor_checking(&tsk->thread.xstate->fxsave);
263}
264
Roland McGrath1eeaed72008-01-30 13:31:51 +0100265/*
266 * Signal frame handlers...
267 */
Suresh Siddhaab513702008-07-29 10:29:22 -0700268extern int save_i387_xstate(void __user *buf);
269extern int restore_i387_xstate(void __user *buf);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100270
271static inline void __unlazy_fpu(struct task_struct *tsk)
272{
273 if (task_thread_info(tsk)->status & TS_USEDFPU) {
274 __save_init_fpu(tsk);
275 stts();
276 } else
277 tsk->fpu_counter = 0;
278}
279
280static inline void __clear_fpu(struct task_struct *tsk)
281{
282 if (task_thread_info(tsk)->status & TS_USEDFPU) {
283 tolerant_fwait();
284 task_thread_info(tsk)->status &= ~TS_USEDFPU;
285 stts();
286 }
287}
288
289static inline void kernel_fpu_begin(void)
290{
291 struct thread_info *me = current_thread_info();
292 preempt_disable();
293 if (me->status & TS_USEDFPU)
294 __save_init_fpu(me->task);
295 else
296 clts();
297}
298
299static inline void kernel_fpu_end(void)
300{
301 stts();
302 preempt_enable();
303}
304
Suresh Siddhae4914012008-08-13 22:02:26 +1000305/*
306 * Some instructions like VIA's padlock instructions generate a spurious
307 * DNA fault but don't modify SSE registers. And these instructions
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400308 * get used from interrupt context as well. To prevent these kernel instructions
309 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
Suresh Siddhae4914012008-08-13 22:02:26 +1000310 * should use them only in the context of irq_ts_save/restore()
311 */
312static inline int irq_ts_save(void)
313{
314 /*
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400315 * If in process context and not atomic, we can take a spurious DNA fault.
316 * Otherwise, doing clts() in process context requires disabling preemption
317 * or some heavy lifting like kernel_fpu_begin()
Suresh Siddhae4914012008-08-13 22:02:26 +1000318 */
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400319 if (!in_atomic())
Suresh Siddhae4914012008-08-13 22:02:26 +1000320 return 0;
321
322 if (read_cr0() & X86_CR0_TS) {
323 clts();
324 return 1;
325 }
326
327 return 0;
328}
329
330static inline void irq_ts_restore(int TS_state)
331{
332 if (TS_state)
333 stts();
334}
335
Roland McGrath1eeaed72008-01-30 13:31:51 +0100336#ifdef CONFIG_X86_64
337
338static inline void save_init_fpu(struct task_struct *tsk)
339{
340 __save_init_fpu(tsk);
341 stts();
342}
343
344#define unlazy_fpu __unlazy_fpu
345#define clear_fpu __clear_fpu
346
347#else /* CONFIG_X86_32 */
348
349/*
350 * These disable preemption on their own and are safe
351 */
352static inline void save_init_fpu(struct task_struct *tsk)
353{
354 preempt_disable();
355 __save_init_fpu(tsk);
356 stts();
357 preempt_enable();
358}
359
360static inline void unlazy_fpu(struct task_struct *tsk)
361{
362 preempt_disable();
363 __unlazy_fpu(tsk);
364 preempt_enable();
365}
366
367static inline void clear_fpu(struct task_struct *tsk)
368{
369 preempt_disable();
370 __clear_fpu(tsk);
371 preempt_enable();
372}
373
374#endif /* CONFIG_X86_64 */
375
376/*
Roland McGrath1eeaed72008-01-30 13:31:51 +0100377 * i387 state interaction
378 */
379static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
380{
381 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700382 return tsk->thread.xstate->fxsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100383 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200384 return (unsigned short)tsk->thread.xstate->fsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100385 }
386}
387
388static inline unsigned short get_fpu_swd(struct task_struct *tsk)
389{
390 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700391 return tsk->thread.xstate->fxsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100392 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200393 return (unsigned short)tsk->thread.xstate->fsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100394 }
395}
396
397static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
398{
399 if (cpu_has_xmm) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700400 return tsk->thread.xstate->fxsave.mxcsr;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100401 } else {
402 return MXCSR_DEFAULT;
403 }
404}
405
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700406#endif /* _ASM_X86_I387_H */