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>
12 #include <asm/sigcontext.h>
13 #include <asm/processor.h>
14 #include <asm/math_emu.h>
15 #include <asm/uaccess.h>
16 #include <asm/ptrace.h>
21 # include <asm/sigcontext32.h>
22 # include <asm/user32.h>
24 # define save_i387_ia32 save_i387
25 # define restore_i387_ia32 restore_i387
26 # define _fpstate_ia32 _fpstate
27 # define user_i387_ia32_struct user_i387_struct
28 # define user32_fxsr_struct user_fxsr_struct
31 #ifdef CONFIG_MATH_EMULATION
32 # define HAVE_HWFP (boot_cpu_data.hard_math)
37 static unsigned int mxcsr_feature_mask __read_mostly
= 0xffffffffu
;
38 unsigned int xstate_size
;
39 static struct i387_fxsave_struct fx_scratch __cpuinitdata
;
41 void __cpuinit
mxcsr_feature_mask_init(void)
43 unsigned long mask
= 0;
47 memset(&fx_scratch
, 0, sizeof(struct i387_fxsave_struct
));
48 asm volatile("fxsave %0" : : "m" (fx_scratch
));
49 mask
= fx_scratch
.mxcsr_mask
;
53 mxcsr_feature_mask
&= mask
;
57 void __init
init_thread_xstate(void)
60 xstate_size
= sizeof(struct i387_fxsave_struct
);
63 xstate_size
= sizeof(struct i387_fsave_struct
);
69 * Called at bootup to set up the initial FPU state that is later cloned
72 void __cpuinit
fpu_init(void)
74 unsigned long oldcr0
= read_cr0();
76 set_in_cr4(X86_CR4_OSFXSR
);
77 set_in_cr4(X86_CR4_OSXMMEXCPT
);
79 write_cr0(oldcr0
& ~(X86_CR0_TS
|X86_CR0_EM
)); /* clear TS and EM */
81 mxcsr_feature_mask_init();
82 /* clean state in init */
83 current_thread_info()->status
= 0;
86 #endif /* CONFIG_X86_64 */
89 * The _current_ task is using the FPU for the first time
90 * so initialize it and set the mxcsr to its default
91 * value at reset if we support XMM instructions and then
92 * remeber the current task has used the FPU.
94 int init_fpu(struct task_struct
*tsk
)
96 if (tsk_used_math(tsk
)) {
103 * Memory allocation at the first usage of the FPU and other state.
105 if (!tsk
->thread
.xstate
) {
106 tsk
->thread
.xstate
= kmem_cache_alloc(task_xstate_cachep
,
108 if (!tsk
->thread
.xstate
)
113 struct i387_fxsave_struct
*fx
= &tsk
->thread
.xstate
->fxsave
;
115 memset(fx
, 0, xstate_size
);
118 fx
->mxcsr
= MXCSR_DEFAULT
;
120 struct i387_fsave_struct
*fp
= &tsk
->thread
.xstate
->fsave
;
121 memset(fp
, 0, xstate_size
);
122 fp
->cwd
= 0xffff037fu
;
123 fp
->swd
= 0xffff0000u
;
124 fp
->twd
= 0xffffffffu
;
125 fp
->fos
= 0xffff0000u
;
128 * Only the device not available exception or ptrace can call init_fpu.
130 set_stopped_child_used_math(tsk
);
134 int fpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
136 return tsk_used_math(target
) ? regset
->n
: 0;
139 int xfpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
141 return (cpu_has_fxsr
&& tsk_used_math(target
)) ? regset
->n
: 0;
144 int xfpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
145 unsigned int pos
, unsigned int count
,
146 void *kbuf
, void __user
*ubuf
)
153 ret
= init_fpu(target
);
157 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
158 &target
->thread
.xstate
->fxsave
, 0, -1);
161 int xfpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
162 unsigned int pos
, unsigned int count
,
163 const void *kbuf
, const void __user
*ubuf
)
170 ret
= init_fpu(target
);
174 set_stopped_child_used_math(target
);
176 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
177 &target
->thread
.xstate
->fxsave
, 0, -1);
180 * mxcsr reserved bits must be masked to zero for security reasons.
182 target
->thread
.xstate
->fxsave
.mxcsr
&= mxcsr_feature_mask
;
187 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
190 * FPU tag word conversions.
193 static inline unsigned short twd_i387_to_fxsr(unsigned short twd
)
195 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
197 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
199 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
200 /* and move the valid bits to the lower byte. */
201 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
202 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
203 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
208 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
209 #define FP_EXP_TAG_VALID 0
210 #define FP_EXP_TAG_ZERO 1
211 #define FP_EXP_TAG_SPECIAL 2
212 #define FP_EXP_TAG_EMPTY 3
214 static inline u32
twd_fxsr_to_i387(struct i387_fxsave_struct
*fxsave
)
217 u32 tos
= (fxsave
->swd
>> 11) & 7;
218 u32 twd
= (unsigned long) fxsave
->twd
;
220 u32 ret
= 0xffff0000u
;
223 for (i
= 0; i
< 8; i
++, twd
>>= 1) {
225 st
= FPREG_ADDR(fxsave
, (i
- tos
) & 7);
227 switch (st
->exponent
& 0x7fff) {
229 tag
= FP_EXP_TAG_SPECIAL
;
232 if (!st
->significand
[0] &&
233 !st
->significand
[1] &&
234 !st
->significand
[2] &&
236 tag
= FP_EXP_TAG_ZERO
;
238 tag
= FP_EXP_TAG_SPECIAL
;
241 if (st
->significand
[3] & 0x8000)
242 tag
= FP_EXP_TAG_VALID
;
244 tag
= FP_EXP_TAG_SPECIAL
;
248 tag
= FP_EXP_TAG_EMPTY
;
250 ret
|= tag
<< (2 * i
);
256 * FXSR floating point environment conversions.
260 convert_from_fxsr(struct user_i387_ia32_struct
*env
, struct task_struct
*tsk
)
262 struct i387_fxsave_struct
*fxsave
= &tsk
->thread
.xstate
->fxsave
;
263 struct _fpreg
*to
= (struct _fpreg
*) &env
->st_space
[0];
264 struct _fpxreg
*from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
267 env
->cwd
= fxsave
->cwd
| 0xffff0000u
;
268 env
->swd
= fxsave
->swd
| 0xffff0000u
;
269 env
->twd
= twd_fxsr_to_i387(fxsave
);
272 env
->fip
= fxsave
->rip
;
273 env
->foo
= fxsave
->rdp
;
274 if (tsk
== current
) {
276 * should be actually ds/cs at fpu exception time, but
277 * that information is not available in 64bit mode.
279 asm("mov %%ds, %[fos]" : [fos
] "=r" (env
->fos
));
280 asm("mov %%cs, %[fcs]" : [fcs
] "=r" (env
->fcs
));
282 struct pt_regs
*regs
= task_pt_regs(tsk
);
284 env
->fos
= 0xffff0000 | tsk
->thread
.ds
;
288 env
->fip
= fxsave
->fip
;
289 env
->fcs
= (u16
) fxsave
->fcs
| ((u32
) fxsave
->fop
<< 16);
290 env
->foo
= fxsave
->foo
;
291 env
->fos
= fxsave
->fos
;
294 for (i
= 0; i
< 8; ++i
)
295 memcpy(&to
[i
], &from
[i
], sizeof(to
[0]));
298 static void convert_to_fxsr(struct task_struct
*tsk
,
299 const struct user_i387_ia32_struct
*env
)
302 struct i387_fxsave_struct
*fxsave
= &tsk
->thread
.xstate
->fxsave
;
303 struct _fpreg
*from
= (struct _fpreg
*) &env
->st_space
[0];
304 struct _fpxreg
*to
= (struct _fpxreg
*) &fxsave
->st_space
[0];
307 fxsave
->cwd
= env
->cwd
;
308 fxsave
->swd
= env
->swd
;
309 fxsave
->twd
= twd_i387_to_fxsr(env
->twd
);
310 fxsave
->fop
= (u16
) ((u32
) env
->fcs
>> 16);
312 fxsave
->rip
= env
->fip
;
313 fxsave
->rdp
= env
->foo
;
314 /* cs and ds ignored */
316 fxsave
->fip
= env
->fip
;
317 fxsave
->fcs
= (env
->fcs
& 0xffff);
318 fxsave
->foo
= env
->foo
;
319 fxsave
->fos
= env
->fos
;
322 for (i
= 0; i
< 8; ++i
)
323 memcpy(&to
[i
], &from
[i
], sizeof(from
[0]));
326 int fpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
327 unsigned int pos
, unsigned int count
,
328 void *kbuf
, void __user
*ubuf
)
330 struct user_i387_ia32_struct env
;
334 return fpregs_soft_get(target
, regset
, pos
, count
, kbuf
, ubuf
);
336 ret
= init_fpu(target
);
341 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
342 &target
->thread
.xstate
->fsave
, 0,
346 if (kbuf
&& pos
== 0 && count
== sizeof(env
)) {
347 convert_from_fxsr(kbuf
, target
);
351 convert_from_fxsr(&env
, target
);
353 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
356 int fpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
357 unsigned int pos
, unsigned int count
,
358 const void *kbuf
, const void __user
*ubuf
)
360 struct user_i387_ia32_struct env
;
364 return fpregs_soft_set(target
, regset
, pos
, count
, kbuf
, ubuf
);
366 ret
= init_fpu(target
);
370 set_stopped_child_used_math(target
);
373 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
374 &target
->thread
.xstate
->fsave
, 0, -1);
377 if (pos
> 0 || count
< sizeof(env
))
378 convert_from_fxsr(&env
, target
);
380 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
382 convert_to_fxsr(target
, &env
);
388 * Signal frame handlers.
391 static inline int save_i387_fsave(struct _fpstate_ia32 __user
*buf
)
393 struct task_struct
*tsk
= current
;
394 struct i387_fsave_struct
*fp
= &tsk
->thread
.xstate
->fsave
;
397 fp
->status
= fp
->swd
;
398 if (__copy_to_user(buf
, fp
, sizeof(struct i387_fsave_struct
)))
403 static int save_i387_fxsave(struct _fpstate_ia32 __user
*buf
)
405 struct task_struct
*tsk
= current
;
406 struct i387_fxsave_struct
*fx
= &tsk
->thread
.xstate
->fxsave
;
407 struct user_i387_ia32_struct env
;
412 convert_from_fxsr(&env
, tsk
);
413 if (__copy_to_user(buf
, &env
, sizeof(env
)))
416 err
|= __put_user(fx
->swd
, &buf
->status
);
417 err
|= __put_user(X86_FXSR_MAGIC
, &buf
->magic
);
421 if (__copy_to_user(&buf
->_fxsr_env
[0], fx
,
422 sizeof(struct i387_fxsave_struct
)))
427 int save_i387_ia32(struct _fpstate_ia32 __user
*buf
)
432 * This will cause a "finit" to be triggered by the next
433 * attempted FPU operation by the 'current' process.
438 return fpregs_soft_get(current
, NULL
,
439 0, sizeof(struct user_i387_ia32_struct
),
444 return save_i387_fxsave(buf
);
446 return save_i387_fsave(buf
);
449 static inline int restore_i387_fsave(struct _fpstate_ia32 __user
*buf
)
451 struct task_struct
*tsk
= current
;
453 return __copy_from_user(&tsk
->thread
.xstate
->fsave
, buf
,
454 sizeof(struct i387_fsave_struct
));
457 static int restore_i387_fxsave(struct _fpstate_ia32 __user
*buf
)
459 struct task_struct
*tsk
= current
;
460 struct user_i387_ia32_struct env
;
463 err
= __copy_from_user(&tsk
->thread
.xstate
->fxsave
, &buf
->_fxsr_env
[0],
464 sizeof(struct i387_fxsave_struct
));
465 /* mxcsr reserved bits must be masked to zero for security reasons */
466 tsk
->thread
.xstate
->fxsave
.mxcsr
&= mxcsr_feature_mask
;
467 if (err
|| __copy_from_user(&env
, buf
, sizeof(env
)))
469 convert_to_fxsr(tsk
, &env
);
474 int restore_i387_ia32(struct _fpstate_ia32 __user
*buf
)
479 struct task_struct
*tsk
= current
;
490 err
= restore_i387_fxsave(buf
);
492 err
= restore_i387_fsave(buf
);
494 err
= fpregs_soft_set(current
, NULL
,
495 0, sizeof(struct user_i387_ia32_struct
),
504 * FPU state for core dumps.
505 * This is only used for a.out dumps now.
506 * It is declared generically using elf_fpregset_t (which is
507 * struct user_i387_struct) but is in fact only used for 32-bit
508 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
510 int dump_fpu(struct pt_regs
*regs
, struct user_i387_struct
*fpu
)
512 struct task_struct
*tsk
= current
;
515 fpvalid
= !!used_math();
517 fpvalid
= !fpregs_get(tsk
, NULL
,
518 0, sizeof(struct user_i387_ia32_struct
),
523 EXPORT_SYMBOL(dump_fpu
);
525 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */