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
8 #include <linux/module.h>
9 #include <linux/regset.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
13 #include <asm/sigcontext.h>
14 #include <asm/processor.h>
15 #include <asm/math_emu.h>
16 #include <asm/uaccess.h>
17 #include <asm/ptrace.h>
19 #include <asm/fpu-internal.h>
23 * Were we in an interrupt that interrupted kernel mode?
25 * For now, with eagerfpu we will return interrupted kernel FPU
26 * state as not-idle. TBD: Ideally we can change the return value
27 * to something like __thread_has_fpu(current). But we need to
28 * be careful of doing __thread_clear_has_fpu() before saving
29 * the FPU etc for supporting nested uses etc. For now, take
32 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
33 * pair does nothing at all: the thread must not have fpu (so
34 * that we don't try to save the FPU state), and TS must
35 * be set (so that the clts/stts pair does nothing that is
36 * visible in the interrupted kernel thread).
38 static inline bool interrupted_kernel_fpu_idle(void)
43 return !__thread_has_fpu(current
) &&
44 (read_cr0() & X86_CR0_TS
);
48 * Were we in user mode (or vm86 mode) when we were
51 * Doing kernel_fpu_begin/end() is ok if we are running
52 * in an interrupt context from user mode - we'll just
53 * save the FPU state as required.
55 static inline bool interrupted_user_mode(void)
57 struct pt_regs
*regs
= get_irq_regs();
58 return regs
&& user_mode_vm(regs
);
62 * Can we use the FPU in kernel mode with the
63 * whole "kernel_fpu_begin/end()" sequence?
65 * It's always ok in process context (ie "not interrupt")
66 * but it is sometimes ok even from an irq.
68 bool irq_fpu_usable(void)
70 return !in_interrupt() ||
71 interrupted_user_mode() ||
72 interrupted_kernel_fpu_idle();
74 EXPORT_SYMBOL(irq_fpu_usable
);
76 void __kernel_fpu_begin(void)
78 struct task_struct
*me
= current
;
80 if (__thread_has_fpu(me
)) {
82 __thread_clear_has_fpu(me
);
83 /* We do 'stts()' in __kernel_fpu_end() */
84 } else if (!use_eager_fpu()) {
85 this_cpu_write(fpu_owner_task
, NULL
);
89 EXPORT_SYMBOL(__kernel_fpu_begin
);
91 void __kernel_fpu_end(void)
98 EXPORT_SYMBOL(__kernel_fpu_end
);
100 void unlazy_fpu(struct task_struct
*tsk
)
103 if (__thread_has_fpu(tsk
)) {
104 __save_init_fpu(tsk
);
105 __thread_fpu_end(tsk
);
107 tsk
->fpu_counter
= 0;
110 EXPORT_SYMBOL(unlazy_fpu
);
112 unsigned int mxcsr_feature_mask __read_mostly
= 0xffffffffu
;
113 unsigned int xstate_size
;
114 EXPORT_SYMBOL_GPL(xstate_size
);
115 static struct i387_fxsave_struct fx_scratch __cpuinitdata
;
117 static void __cpuinit
mxcsr_feature_mask_init(void)
119 unsigned long mask
= 0;
122 memset(&fx_scratch
, 0, sizeof(struct i387_fxsave_struct
));
123 asm volatile("fxsave %0" : : "m" (fx_scratch
));
124 mask
= fx_scratch
.mxcsr_mask
;
128 mxcsr_feature_mask
&= mask
;
131 static void __cpuinit
init_thread_xstate(void)
134 * Note that xstate_size might be overwriten later during
140 * Disable xsave as we do not support it if i387
141 * emulation is enabled.
143 setup_clear_cpu_cap(X86_FEATURE_XSAVE
);
144 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT
);
145 xstate_size
= sizeof(struct i387_soft_struct
);
150 xstate_size
= sizeof(struct i387_fxsave_struct
);
152 xstate_size
= sizeof(struct i387_fsave_struct
);
156 * Called at bootup to set up the initial FPU state that is later cloned
157 * into all processes.
160 void __cpuinit
fpu_init(void)
163 unsigned long cr4_mask
= 0;
166 cr4_mask
|= X86_CR4_OSFXSR
;
168 cr4_mask
|= X86_CR4_OSXMMEXCPT
;
170 set_in_cr4(cr4_mask
);
173 cr0
&= ~(X86_CR0_TS
|X86_CR0_EM
); /* clear TS and EM */
179 * init_thread_xstate is only called once to avoid overriding
180 * xstate_size during boot time or during CPU hotplug.
182 if (xstate_size
== 0)
183 init_thread_xstate();
185 mxcsr_feature_mask_init();
190 void fpu_finit(struct fpu
*fpu
)
193 finit_soft_fpu(&fpu
->state
->soft
);
198 fx_finit(&fpu
->state
->fxsave
);
200 struct i387_fsave_struct
*fp
= &fpu
->state
->fsave
;
201 memset(fp
, 0, xstate_size
);
202 fp
->cwd
= 0xffff037fu
;
203 fp
->swd
= 0xffff0000u
;
204 fp
->twd
= 0xffffffffu
;
205 fp
->fos
= 0xffff0000u
;
208 EXPORT_SYMBOL_GPL(fpu_finit
);
211 * The _current_ task is using the FPU for the first time
212 * so initialize it and set the mxcsr to its default
213 * value at reset if we support XMM instructions and then
214 * remember the current task has used the FPU.
216 int init_fpu(struct task_struct
*tsk
)
220 if (tsk_used_math(tsk
)) {
221 if (HAVE_HWFP
&& tsk
== current
)
223 tsk
->thread
.fpu
.last_cpu
= ~0;
228 * Memory allocation at the first usage of the FPU and other state.
230 ret
= fpu_alloc(&tsk
->thread
.fpu
);
234 fpu_finit(&tsk
->thread
.fpu
);
236 set_stopped_child_used_math(tsk
);
239 EXPORT_SYMBOL_GPL(init_fpu
);
242 * The xstateregs_active() routine is the same as the fpregs_active() routine,
243 * as the "regset->n" for the xstate regset will be updated based on the feature
244 * capabilites supported by the xsave.
246 int fpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
248 return tsk_used_math(target
) ? regset
->n
: 0;
251 int xfpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
253 return (cpu_has_fxsr
&& tsk_used_math(target
)) ? regset
->n
: 0;
256 int xfpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
257 unsigned int pos
, unsigned int count
,
258 void *kbuf
, void __user
*ubuf
)
265 ret
= init_fpu(target
);
269 sanitize_i387_state(target
);
271 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
272 &target
->thread
.fpu
.state
->fxsave
, 0, -1);
275 int xfpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
276 unsigned int pos
, unsigned int count
,
277 const void *kbuf
, const void __user
*ubuf
)
284 ret
= init_fpu(target
);
288 sanitize_i387_state(target
);
290 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
291 &target
->thread
.fpu
.state
->fxsave
, 0, -1);
294 * mxcsr reserved bits must be masked to zero for security reasons.
296 target
->thread
.fpu
.state
->fxsave
.mxcsr
&= mxcsr_feature_mask
;
299 * update the header bits in the xsave header, indicating the
300 * presence of FP and SSE state.
303 target
->thread
.fpu
.state
->xsave
.xsave_hdr
.xstate_bv
|= XSTATE_FPSSE
;
308 int xstateregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
309 unsigned int pos
, unsigned int count
,
310 void *kbuf
, void __user
*ubuf
)
317 ret
= init_fpu(target
);
322 * Copy the 48bytes defined by the software first into the xstate
323 * memory layout in the thread struct, so that we can copy the entire
324 * xstateregs to the user using one user_regset_copyout().
326 memcpy(&target
->thread
.fpu
.state
->fxsave
.sw_reserved
,
327 xstate_fx_sw_bytes
, sizeof(xstate_fx_sw_bytes
));
330 * Copy the xstate memory layout.
332 ret
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
333 &target
->thread
.fpu
.state
->xsave
, 0, -1);
337 int xstateregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
338 unsigned int pos
, unsigned int count
,
339 const void *kbuf
, const void __user
*ubuf
)
342 struct xsave_hdr_struct
*xsave_hdr
;
347 ret
= init_fpu(target
);
351 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
352 &target
->thread
.fpu
.state
->xsave
, 0, -1);
355 * mxcsr reserved bits must be masked to zero for security reasons.
357 target
->thread
.fpu
.state
->fxsave
.mxcsr
&= mxcsr_feature_mask
;
359 xsave_hdr
= &target
->thread
.fpu
.state
->xsave
.xsave_hdr
;
361 xsave_hdr
->xstate_bv
&= pcntxt_mask
;
363 * These bits must be zero.
365 xsave_hdr
->reserved1
[0] = xsave_hdr
->reserved1
[1] = 0;
370 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
373 * FPU tag word conversions.
376 static inline unsigned short twd_i387_to_fxsr(unsigned short twd
)
378 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
380 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
382 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
383 /* and move the valid bits to the lower byte. */
384 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
385 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
386 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
391 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
392 #define FP_EXP_TAG_VALID 0
393 #define FP_EXP_TAG_ZERO 1
394 #define FP_EXP_TAG_SPECIAL 2
395 #define FP_EXP_TAG_EMPTY 3
397 static inline u32
twd_fxsr_to_i387(struct i387_fxsave_struct
*fxsave
)
400 u32 tos
= (fxsave
->swd
>> 11) & 7;
401 u32 twd
= (unsigned long) fxsave
->twd
;
403 u32 ret
= 0xffff0000u
;
406 for (i
= 0; i
< 8; i
++, twd
>>= 1) {
408 st
= FPREG_ADDR(fxsave
, (i
- tos
) & 7);
410 switch (st
->exponent
& 0x7fff) {
412 tag
= FP_EXP_TAG_SPECIAL
;
415 if (!st
->significand
[0] &&
416 !st
->significand
[1] &&
417 !st
->significand
[2] &&
419 tag
= FP_EXP_TAG_ZERO
;
421 tag
= FP_EXP_TAG_SPECIAL
;
424 if (st
->significand
[3] & 0x8000)
425 tag
= FP_EXP_TAG_VALID
;
427 tag
= FP_EXP_TAG_SPECIAL
;
431 tag
= FP_EXP_TAG_EMPTY
;
433 ret
|= tag
<< (2 * i
);
439 * FXSR floating point environment conversions.
443 convert_from_fxsr(struct user_i387_ia32_struct
*env
, struct task_struct
*tsk
)
445 struct i387_fxsave_struct
*fxsave
= &tsk
->thread
.fpu
.state
->fxsave
;
446 struct _fpreg
*to
= (struct _fpreg
*) &env
->st_space
[0];
447 struct _fpxreg
*from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
450 env
->cwd
= fxsave
->cwd
| 0xffff0000u
;
451 env
->swd
= fxsave
->swd
| 0xffff0000u
;
452 env
->twd
= twd_fxsr_to_i387(fxsave
);
455 env
->fip
= fxsave
->rip
;
456 env
->foo
= fxsave
->rdp
;
458 * should be actually ds/cs at fpu exception time, but
459 * that information is not available in 64bit mode.
461 env
->fcs
= task_pt_regs(tsk
)->cs
;
462 if (tsk
== current
) {
463 savesegment(ds
, env
->fos
);
465 env
->fos
= tsk
->thread
.ds
;
467 env
->fos
|= 0xffff0000;
469 env
->fip
= fxsave
->fip
;
470 env
->fcs
= (u16
) fxsave
->fcs
| ((u32
) fxsave
->fop
<< 16);
471 env
->foo
= fxsave
->foo
;
472 env
->fos
= fxsave
->fos
;
475 for (i
= 0; i
< 8; ++i
)
476 memcpy(&to
[i
], &from
[i
], sizeof(to
[0]));
479 void convert_to_fxsr(struct task_struct
*tsk
,
480 const struct user_i387_ia32_struct
*env
)
483 struct i387_fxsave_struct
*fxsave
= &tsk
->thread
.fpu
.state
->fxsave
;
484 struct _fpreg
*from
= (struct _fpreg
*) &env
->st_space
[0];
485 struct _fpxreg
*to
= (struct _fpxreg
*) &fxsave
->st_space
[0];
488 fxsave
->cwd
= env
->cwd
;
489 fxsave
->swd
= env
->swd
;
490 fxsave
->twd
= twd_i387_to_fxsr(env
->twd
);
491 fxsave
->fop
= (u16
) ((u32
) env
->fcs
>> 16);
493 fxsave
->rip
= env
->fip
;
494 fxsave
->rdp
= env
->foo
;
495 /* cs and ds ignored */
497 fxsave
->fip
= env
->fip
;
498 fxsave
->fcs
= (env
->fcs
& 0xffff);
499 fxsave
->foo
= env
->foo
;
500 fxsave
->fos
= env
->fos
;
503 for (i
= 0; i
< 8; ++i
)
504 memcpy(&to
[i
], &from
[i
], sizeof(from
[0]));
507 int fpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
508 unsigned int pos
, unsigned int count
,
509 void *kbuf
, void __user
*ubuf
)
511 struct user_i387_ia32_struct env
;
514 ret
= init_fpu(target
);
519 return fpregs_soft_get(target
, regset
, pos
, count
, kbuf
, ubuf
);
522 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
523 &target
->thread
.fpu
.state
->fsave
, 0,
527 sanitize_i387_state(target
);
529 if (kbuf
&& pos
== 0 && count
== sizeof(env
)) {
530 convert_from_fxsr(kbuf
, target
);
534 convert_from_fxsr(&env
, target
);
536 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
539 int fpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
540 unsigned int pos
, unsigned int count
,
541 const void *kbuf
, const void __user
*ubuf
)
543 struct user_i387_ia32_struct env
;
546 ret
= init_fpu(target
);
550 sanitize_i387_state(target
);
553 return fpregs_soft_set(target
, regset
, pos
, count
, kbuf
, ubuf
);
556 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
557 &target
->thread
.fpu
.state
->fsave
, 0, -1);
560 if (pos
> 0 || count
< sizeof(env
))
561 convert_from_fxsr(&env
, target
);
563 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
565 convert_to_fxsr(target
, &env
);
568 * update the header bit in the xsave header, indicating the
572 target
->thread
.fpu
.state
->xsave
.xsave_hdr
.xstate_bv
|= XSTATE_FP
;
577 * FPU state for core dumps.
578 * This is only used for a.out dumps now.
579 * It is declared generically using elf_fpregset_t (which is
580 * struct user_i387_struct) but is in fact only used for 32-bit
581 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
583 int dump_fpu(struct pt_regs
*regs
, struct user_i387_struct
*fpu
)
585 struct task_struct
*tsk
= current
;
588 fpvalid
= !!used_math();
590 fpvalid
= !fpregs_get(tsk
, NULL
,
591 0, sizeof(struct user_i387_ia32_struct
),
596 EXPORT_SYMBOL(dump_fpu
);
598 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */