blob: a301a6825c3a88db39c838a99c08fb6a928d7bf2 [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
Herbert Xu3b0d6592009-11-03 09:11:15 -050013#ifndef __ASSEMBLY__
14
Roland McGrath1eeaed72008-01-30 13:31:51 +010015#include <linux/sched.h>
16#include <linux/kernel_stat.h>
17#include <linux/regset.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100018#include <linux/hardirq.h>
H. Peter Anvin92c37fa2008-02-04 16:47:58 +010019#include <asm/asm.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010020#include <asm/processor.h>
21#include <asm/sigcontext.h>
22#include <asm/user.h>
23#include <asm/uaccess.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070024#include <asm/xsave.h>
Roland McGrath1eeaed72008-01-30 13:31:51 +010025
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070026extern unsigned int sig_xstate_size;
Roland McGrath1eeaed72008-01-30 13:31:51 +010027extern void fpu_init(void);
Roland McGrath1eeaed72008-01-30 13:31:51 +010028extern void mxcsr_feature_mask_init(void);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070029extern int init_fpu(struct task_struct *child);
Roland McGrath1eeaed72008-01-30 13:31:51 +010030extern asmlinkage void math_state_restore(void);
Jeremy Fitzhardingee6e9cac2009-04-24 00:40:59 -070031extern void __math_state_restore(void);
Suresh Siddha61c46282008-03-10 15:28:04 -070032extern void init_thread_xstate(void);
Jaswinder Singh36454932008-07-21 22:31:57 +053033extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Roland McGrath1eeaed72008-01-30 13:31:51 +010034
35extern user_regset_active_fn fpregs_active, xfpregs_active;
Suresh Siddha5b3efd52010-02-11 11:50:59 -080036extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
37 xstateregs_get;
38extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
39 xstateregs_set;
40
41/*
42 * xstateregs_active == fpregs_active. Please refer to the comment
43 * at the definition of fpregs_active.
44 */
45#define xstateregs_active fpregs_active
Roland McGrath1eeaed72008-01-30 13:31:51 +010046
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070047extern struct _fpx_sw_bytes fx_sw_reserved;
Roland McGrath1eeaed72008-01-30 13:31:51 +010048#ifdef CONFIG_IA32_EMULATION
Suresh Siddha3c1c7f12008-07-29 10:29:21 -070049extern unsigned int sig_xstate_ia32_size;
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070050extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
Roland McGrath1eeaed72008-01-30 13:31:51 +010051struct _fpstate_ia32;
Suresh Siddhaab513702008-07-29 10:29:22 -070052struct _xstate_ia32;
53extern int save_i387_xstate_ia32(void __user *buf);
54extern int restore_i387_xstate_ia32(void __user *buf);
Roland McGrath1eeaed72008-01-30 13:31:51 +010055#endif
56
Suresh Siddhab359e8a2008-07-29 10:29:20 -070057#define X87_FSW_ES (1 << 7) /* Exception Summary */
58
Avi Kivityc9ad4882010-05-06 11:45:45 +030059static inline bool use_xsave(void)
60{
61 u8 has_xsave;
62
63 alternative_io("mov $0, %0",
64 "mov $1, %0",
65 X86_FEATURE_XSAVE,
66 "=g"(has_xsave));
67
68 return has_xsave;
69}
70
Roland McGrath1eeaed72008-01-30 13:31:51 +010071#ifdef CONFIG_X86_64
72
73/* Ignore delayed exceptions from user space */
74static inline void tolerant_fwait(void)
75{
76 asm volatile("1: fwait\n"
77 "2:\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070078 _ASM_EXTABLE(1b, 2b));
Roland McGrath1eeaed72008-01-30 13:31:51 +010079}
80
Suresh Siddhab359e8a2008-07-29 10:29:20 -070081static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +010082{
83 int err;
84
85 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
86 "2:\n"
87 ".section .fixup,\"ax\"\n"
88 "3: movl $-1,%[err]\n"
89 " jmp 2b\n"
90 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -070091 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +010092 : [err] "=r" (err)
Jiri Slaby4ecf4582009-04-08 13:32:00 +020093#if 0 /* See comment in fxsave() below. */
Roland McGrath1eeaed72008-01-30 13:31:51 +010094 : [fx] "r" (fx), "m" (*fx), "0" (0));
95#else
96 : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
97#endif
Roland McGrath1eeaed72008-01-30 13:31:51 +010098 return err;
99}
100
Roland McGrath1eeaed72008-01-30 13:31:51 +0100101/* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
102 is pending. Clear the x87 state here by setting it to fixed
103 values. The kernel data segment can be sometimes 0 and sometimes
104 new user value. Both should be ok.
105 Use the PDA as safe address because it should be already in L1. */
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700106static inline void clear_fpu_state(struct task_struct *tsk)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100107{
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700108 struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
109 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
110
111 /*
112 * xsave header may indicate the init state of the FP.
113 */
Avi Kivityc9ad4882010-05-06 11:45:45 +0300114 if (use_xsave() &&
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700115 !(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
116 return;
117
Roland McGrath1eeaed72008-01-30 13:31:51 +0100118 if (unlikely(fx->swd & X87_FSW_ES))
Joe Perchesaffe6632008-03-23 01:02:18 -0700119 asm volatile("fnclex");
Roland McGrath1eeaed72008-01-30 13:31:51 +0100120 alternative_input(ASM_NOP8 ASM_NOP2,
Joe Perchesaffe6632008-03-23 01:02:18 -0700121 " emms\n" /* clear stack tags */
122 " fildl %%gs:0", /* load to clear state */
123 X86_FEATURE_FXSAVE_LEAK);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100124}
125
Suresh Siddhac37b5ef2008-07-29 10:29:25 -0700126static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100127{
128 int err;
129
130 asm volatile("1: rex64/fxsave (%[fx])\n\t"
131 "2:\n"
132 ".section .fixup,\"ax\"\n"
133 "3: movl $-1,%[err]\n"
134 " jmp 2b\n"
135 ".previous\n"
Joe Perchesaffe6632008-03-23 01:02:18 -0700136 _ASM_EXTABLE(1b, 3b)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100137 : [err] "=r" (err), "=m" (*fx)
Jiri Slaby4ecf4582009-04-08 13:32:00 +0200138#if 0 /* See comment in fxsave() below. */
Roland McGrath1eeaed72008-01-30 13:31:51 +0100139 : [fx] "r" (fx), "0" (0));
140#else
141 : [fx] "cdaSDb" (fx), "0" (0));
142#endif
Joe Perchesaffe6632008-03-23 01:02:18 -0700143 if (unlikely(err) &&
144 __clear_user(fx, sizeof(struct i387_fxsave_struct)))
Roland McGrath1eeaed72008-01-30 13:31:51 +0100145 err = -EFAULT;
146 /* No need to clear here because the caller clears USED_MATH */
147 return err;
148}
149
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700150static inline void fxsave(struct task_struct *tsk)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100151{
152 /* Using "rex64; fxsave %0" is broken because, if the memory operand
153 uses any extended registers for addressing, a second REX prefix
154 will be generated (to the assembler, rex64 followed by semicolon
155 is a separate instruction), and hence the 64-bitness is lost. */
156#if 0
157 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
158 starting with gas 2.16. */
159 __asm__ __volatile__("fxsaveq %0"
Suresh Siddha61c46282008-03-10 15:28:04 -0700160 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100161#elif 0
162 /* Using, as a workaround, the properly prefixed form below isn't
163 accepted by any binutils version so far released, complaining that
164 the same type of prefix is used twice if an extended register is
165 needed for addressing (fix submitted to mainline 2005-11-21). */
166 __asm__ __volatile__("rex64/fxsave %0"
Suresh Siddha61c46282008-03-10 15:28:04 -0700167 : "=m" (tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100168#else
169 /* This, however, we can work around by forcing the compiler to select
170 an addressing mode that doesn't require extended registers. */
Suresh Siddha61c46282008-03-10 15:28:04 -0700171 __asm__ __volatile__("rex64/fxsave (%1)"
172 : "=m" (tsk->thread.xstate->fxsave)
173 : "cdaSDb" (&tsk->thread.xstate->fxsave));
Roland McGrath1eeaed72008-01-30 13:31:51 +0100174#endif
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700175}
176
177static inline void __save_init_fpu(struct task_struct *tsk)
178{
Avi Kivityc9ad4882010-05-06 11:45:45 +0300179 if (use_xsave())
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700180 xsave(tsk);
181 else
182 fxsave(tsk);
183
184 clear_fpu_state(tsk);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100185 task_thread_info(tsk)->status &= ~TS_USEDFPU;
186}
187
Roland McGrath1eeaed72008-01-30 13:31:51 +0100188#else /* CONFIG_X86_32 */
189
Daniel Glöcknerab9e1852009-03-04 19:42:27 +0100190#ifdef CONFIG_MATH_EMULATION
191extern void finit_task(struct task_struct *tsk);
192#else
193static inline void finit_task(struct task_struct *tsk)
194{
195}
196#endif
Suresh Siddhae8a496a2008-05-23 16:26:37 -0700197
Roland McGrath1eeaed72008-01-30 13:31:51 +0100198static inline void tolerant_fwait(void)
199{
200 asm volatile("fnclex ; fwait");
201}
202
Jiri Slaby34ba4762009-04-08 13:31:59 +0200203/* perform fxrstor iff the processor has extended states, otherwise frstor */
204static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
Roland McGrath1eeaed72008-01-30 13:31:51 +0100205{
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,
Jiri Slaby34ba4762009-04-08 13:31:59 +0200214 "m" (*fx));
215
Jiri Slabyfcb2ac52009-04-08 13:31:58 +0200216 return 0;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100217}
218
219/* We need a safe address that is cheap to find and that is already
220 in L1 during context switch. The best choices are unfortunately
221 different for UP and SMP */
222#ifdef CONFIG_SMP
223#define safe_address (__per_cpu_offset[0])
224#else
225#define safe_address (kstat_cpu(0).cpustat.user)
226#endif
227
228/*
229 * These must be called with preempt disabled
230 */
231static inline void __save_init_fpu(struct task_struct *tsk)
232{
Avi Kivityc9ad4882010-05-06 11:45:45 +0300233 if (use_xsave()) {
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700234 struct xsave_struct *xstate = &tsk->thread.xstate->xsave;
235 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
236
237 xsave(tsk);
238
239 /*
240 * xsave header may indicate the init state of the FP.
241 */
242 if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP))
243 goto end;
244
245 if (unlikely(fx->swd & X87_FSW_ES))
246 asm volatile("fnclex");
247
248 /*
249 * we can do a simple return here or be paranoid :)
250 */
251 goto clear_state;
252 }
253
Roland McGrath1eeaed72008-01-30 13:31:51 +0100254 /* Use more nops than strictly needed in case the compiler
255 varies code */
256 alternative_input(
257 "fnsave %[fx] ;fwait;" GENERIC_NOP8 GENERIC_NOP4,
258 "fxsave %[fx]\n"
259 "bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
260 X86_FEATURE_FXSR,
Suresh Siddha61c46282008-03-10 15:28:04 -0700261 [fx] "m" (tsk->thread.xstate->fxsave),
262 [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory");
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700263clear_state:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100264 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
265 is pending. Clear the x87 state here by setting it to fixed
266 values. safe_address is a random variable that should be in L1 */
267 alternative_input(
268 GENERIC_NOP8 GENERIC_NOP2,
269 "emms\n\t" /* clear stack tags */
270 "fildl %[addr]", /* set F?P to defined value */
271 X86_FEATURE_FXSAVE_LEAK,
272 [addr] "m" (safe_address));
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700273end:
Roland McGrath1eeaed72008-01-30 13:31:51 +0100274 task_thread_info(tsk)->status &= ~TS_USEDFPU;
275}
276
Suresh Siddhaab513702008-07-29 10:29:22 -0700277#endif /* CONFIG_X86_64 */
278
Jiri Slaby34ba4762009-04-08 13:31:59 +0200279static inline int restore_fpu_checking(struct task_struct *tsk)
280{
Avi Kivityc9ad4882010-05-06 11:45:45 +0300281 if (use_xsave())
Jiri Slaby34ba4762009-04-08 13:31:59 +0200282 return xrstor_checking(&tsk->thread.xstate->xsave);
283 else
284 return fxrstor_checking(&tsk->thread.xstate->fxsave);
285}
286
Roland McGrath1eeaed72008-01-30 13:31:51 +0100287/*
288 * Signal frame handlers...
289 */
Suresh Siddhaab513702008-07-29 10:29:22 -0700290extern int save_i387_xstate(void __user *buf);
291extern int restore_i387_xstate(void __user *buf);
Roland McGrath1eeaed72008-01-30 13:31:51 +0100292
293static inline void __unlazy_fpu(struct task_struct *tsk)
294{
295 if (task_thread_info(tsk)->status & TS_USEDFPU) {
296 __save_init_fpu(tsk);
297 stts();
298 } else
299 tsk->fpu_counter = 0;
300}
301
302static inline void __clear_fpu(struct task_struct *tsk)
303{
304 if (task_thread_info(tsk)->status & TS_USEDFPU) {
305 tolerant_fwait();
306 task_thread_info(tsk)->status &= ~TS_USEDFPU;
307 stts();
308 }
309}
310
311static inline void kernel_fpu_begin(void)
312{
313 struct thread_info *me = current_thread_info();
314 preempt_disable();
315 if (me->status & TS_USEDFPU)
316 __save_init_fpu(me->task);
317 else
318 clts();
319}
320
321static inline void kernel_fpu_end(void)
322{
323 stts();
324 preempt_enable();
325}
326
Huang Yingae4b6882009-08-31 13:11:54 +0800327static inline bool irq_fpu_usable(void)
328{
329 struct pt_regs *regs;
330
331 return !in_interrupt() || !(regs = get_irq_regs()) || \
332 user_mode(regs) || (read_cr0() & X86_CR0_TS);
333}
334
Suresh Siddhae4914012008-08-13 22:02:26 +1000335/*
336 * Some instructions like VIA's padlock instructions generate a spurious
337 * DNA fault but don't modify SSE registers. And these instructions
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400338 * get used from interrupt context as well. To prevent these kernel instructions
339 * in interrupt context interacting wrongly with other user/kernel fpu usage, we
Suresh Siddhae4914012008-08-13 22:02:26 +1000340 * should use them only in the context of irq_ts_save/restore()
341 */
342static inline int irq_ts_save(void)
343{
344 /*
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400345 * If in process context and not atomic, we can take a spurious DNA fault.
346 * Otherwise, doing clts() in process context requires disabling preemption
347 * or some heavy lifting like kernel_fpu_begin()
Suresh Siddhae4914012008-08-13 22:02:26 +1000348 */
Chuck Ebbert0b8c3d52009-06-09 10:40:50 -0400349 if (!in_atomic())
Suresh Siddhae4914012008-08-13 22:02:26 +1000350 return 0;
351
352 if (read_cr0() & X86_CR0_TS) {
353 clts();
354 return 1;
355 }
356
357 return 0;
358}
359
360static inline void irq_ts_restore(int TS_state)
361{
362 if (TS_state)
363 stts();
364}
365
Roland McGrath1eeaed72008-01-30 13:31:51 +0100366#ifdef CONFIG_X86_64
367
368static inline void save_init_fpu(struct task_struct *tsk)
369{
370 __save_init_fpu(tsk);
371 stts();
372}
373
374#define unlazy_fpu __unlazy_fpu
375#define clear_fpu __clear_fpu
376
377#else /* CONFIG_X86_32 */
378
379/*
380 * These disable preemption on their own and are safe
381 */
382static inline void save_init_fpu(struct task_struct *tsk)
383{
384 preempt_disable();
385 __save_init_fpu(tsk);
386 stts();
387 preempt_enable();
388}
389
390static inline void unlazy_fpu(struct task_struct *tsk)
391{
392 preempt_disable();
393 __unlazy_fpu(tsk);
394 preempt_enable();
395}
396
397static inline void clear_fpu(struct task_struct *tsk)
398{
399 preempt_disable();
400 __clear_fpu(tsk);
401 preempt_enable();
402}
403
404#endif /* CONFIG_X86_64 */
405
406/*
Roland McGrath1eeaed72008-01-30 13:31:51 +0100407 * i387 state interaction
408 */
409static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
410{
411 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700412 return tsk->thread.xstate->fxsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100413 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200414 return (unsigned short)tsk->thread.xstate->fsave.cwd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100415 }
416}
417
418static inline unsigned short get_fpu_swd(struct task_struct *tsk)
419{
420 if (cpu_has_fxsr) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700421 return tsk->thread.xstate->fxsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100422 } else {
Suresh Siddha1679f272008-04-16 10:27:53 +0200423 return (unsigned short)tsk->thread.xstate->fsave.swd;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100424 }
425}
426
427static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
428{
429 if (cpu_has_xmm) {
Suresh Siddha61c46282008-03-10 15:28:04 -0700430 return tsk->thread.xstate->fxsave.mxcsr;
Roland McGrath1eeaed72008-01-30 13:31:51 +0100431 } else {
432 return MXCSR_DEFAULT;
433 }
434}
435
Herbert Xu3b0d6592009-11-03 09:11:15 -0500436#endif /* __ASSEMBLY__ */
437
438#define PSHUFB_XMM5_XMM0 .byte 0x66, 0x0f, 0x38, 0x00, 0xc5
439#define PSHUFB_XMM5_XMM6 .byte 0x66, 0x0f, 0x38, 0x00, 0xf5
440
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700441#endif /* _ASM_X86_I387_H */