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_soft_struct);
70 xstate_size = sizeof(struct i387_fxsave_struct);
73 xstate_size = sizeof(struct i387_fsave_struct);
79 * Called at bootup to set up the initial FPU state that is later cloned
82 void __cpuinit fpu_init(void)
84 unsigned long oldcr0 = read_cr0();
86 set_in_cr4(X86_CR4_OSFXSR);
87 set_in_cr4(X86_CR4_OSXMMEXCPT);
89 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
92 * Boot processor to setup the FP and extended state context info.
94 if (!smp_processor_id())
98 mxcsr_feature_mask_init();
99 /* clean state in init */
101 current_thread_info()->status = TS_XSAVE;
103 current_thread_info()->status = 0;
106 #endif /* CONFIG_X86_64 */
109 * The _current_ task is using the FPU for the first time
110 * so initialize it and set the mxcsr to its default
111 * value at reset if we support XMM instructions and then
112 * remeber the current task has used the FPU.
114 int init_fpu(struct task_struct *tsk)
116 if (tsk_used_math(tsk)) {
117 if (HAVE_HWFP && tsk == current)
123 * Memory allocation at the first usage of the FPU and other state.
125 if (!tsk->thread.xstate) {
126 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
128 if (!tsk->thread.xstate)
134 memset(tsk->thread.xstate, 0, xstate_size);
136 set_stopped_child_used_math(tsk);
142 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
144 memset(fx, 0, xstate_size);
147 fx->mxcsr = MXCSR_DEFAULT;
149 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
150 memset(fp, 0, xstate_size);
151 fp->cwd = 0xffff037fu;
152 fp->swd = 0xffff0000u;
153 fp->twd = 0xffffffffu;
154 fp->fos = 0xffff0000u;
157 * Only the device not available exception or ptrace can call init_fpu.
159 set_stopped_child_used_math(tsk);
163 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
165 return tsk_used_math(target) ? regset->n : 0;
168 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
170 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
173 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
174 unsigned int pos, unsigned int count,
175 void *kbuf, void __user *ubuf)
182 ret = init_fpu(target);
186 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
187 &target->thread.xstate->fxsave, 0, -1);
190 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
191 unsigned int pos, unsigned int count,
192 const void *kbuf, const void __user *ubuf)
199 ret = init_fpu(target);
203 set_stopped_child_used_math(target);
205 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
206 &target->thread.xstate->fxsave, 0, -1);
209 * mxcsr reserved bits must be masked to zero for security reasons.
211 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
216 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
219 * FPU tag word conversions.
222 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
224 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
226 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
228 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
229 /* and move the valid bits to the lower byte. */
230 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
231 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
232 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
237 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
238 #define FP_EXP_TAG_VALID 0
239 #define FP_EXP_TAG_ZERO 1
240 #define FP_EXP_TAG_SPECIAL 2
241 #define FP_EXP_TAG_EMPTY 3
243 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
246 u32 tos = (fxsave->swd >> 11) & 7;
247 u32 twd = (unsigned long) fxsave->twd;
249 u32 ret = 0xffff0000u;
252 for (i = 0; i < 8; i++, twd >>= 1) {
254 st = FPREG_ADDR(fxsave, (i - tos) & 7);
256 switch (st->exponent & 0x7fff) {
258 tag = FP_EXP_TAG_SPECIAL;
261 if (!st->significand[0] &&
262 !st->significand[1] &&
263 !st->significand[2] &&
265 tag = FP_EXP_TAG_ZERO;
267 tag = FP_EXP_TAG_SPECIAL;
270 if (st->significand[3] & 0x8000)
271 tag = FP_EXP_TAG_VALID;
273 tag = FP_EXP_TAG_SPECIAL;
277 tag = FP_EXP_TAG_EMPTY;
279 ret |= tag << (2 * i);
285 * FXSR floating point environment conversions.
289 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
291 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
292 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
293 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
296 env->cwd = fxsave->cwd | 0xffff0000u;
297 env->swd = fxsave->swd | 0xffff0000u;
298 env->twd = twd_fxsr_to_i387(fxsave);
301 env->fip = fxsave->rip;
302 env->foo = fxsave->rdp;
303 if (tsk == current) {
305 * should be actually ds/cs at fpu exception time, but
306 * that information is not available in 64bit mode.
308 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
309 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
311 struct pt_regs *regs = task_pt_regs(tsk);
313 env->fos = 0xffff0000 | tsk->thread.ds;
317 env->fip = fxsave->fip;
318 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
319 env->foo = fxsave->foo;
320 env->fos = fxsave->fos;
323 for (i = 0; i < 8; ++i)
324 memcpy(&to[i], &from[i], sizeof(to[0]));
327 static void convert_to_fxsr(struct task_struct *tsk,
328 const struct user_i387_ia32_struct *env)
331 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
332 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
333 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
336 fxsave->cwd = env->cwd;
337 fxsave->swd = env->swd;
338 fxsave->twd = twd_i387_to_fxsr(env->twd);
339 fxsave->fop = (u16) ((u32) env->fcs >> 16);
341 fxsave->rip = env->fip;
342 fxsave->rdp = env->foo;
343 /* cs and ds ignored */
345 fxsave->fip = env->fip;
346 fxsave->fcs = (env->fcs & 0xffff);
347 fxsave->foo = env->foo;
348 fxsave->fos = env->fos;
351 for (i = 0; i < 8; ++i)
352 memcpy(&to[i], &from[i], sizeof(from[0]));
355 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
356 unsigned int pos, unsigned int count,
357 void *kbuf, void __user *ubuf)
359 struct user_i387_ia32_struct env;
362 ret = init_fpu(target);
367 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
370 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
371 &target->thread.xstate->fsave, 0,
375 if (kbuf && pos == 0 && count == sizeof(env)) {
376 convert_from_fxsr(kbuf, target);
380 convert_from_fxsr(&env, target);
382 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
385 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
386 unsigned int pos, unsigned int count,
387 const void *kbuf, const void __user *ubuf)
389 struct user_i387_ia32_struct env;
392 ret = init_fpu(target);
396 set_stopped_child_used_math(target);
399 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
402 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
403 &target->thread.xstate->fsave, 0, -1);
406 if (pos > 0 || count < sizeof(env))
407 convert_from_fxsr(&env, target);
409 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
411 convert_to_fxsr(target, &env);
417 * Signal frame handlers.
420 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
422 struct task_struct *tsk = current;
423 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
426 fp->status = fp->swd;
427 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
432 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
434 struct task_struct *tsk = current;
435 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
436 struct user_i387_ia32_struct env;
441 convert_from_fxsr(&env, tsk);
442 if (__copy_to_user(buf, &env, sizeof(env)))
445 err |= __put_user(fx->swd, &buf->status);
446 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
450 if (__copy_to_user(&buf->_fxsr_env[0], fx,
451 sizeof(struct i387_fxsave_struct)))
456 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
461 * This will cause a "finit" to be triggered by the next
462 * attempted FPU operation by the 'current' process.
467 return fpregs_soft_get(current, NULL,
468 0, sizeof(struct user_i387_ia32_struct),
473 return save_i387_fxsave(buf);
475 return save_i387_fsave(buf);
478 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
480 struct task_struct *tsk = current;
482 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
483 sizeof(struct i387_fsave_struct));
486 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
488 struct task_struct *tsk = current;
489 struct user_i387_ia32_struct env;
492 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
493 sizeof(struct i387_fxsave_struct));
494 /* mxcsr reserved bits must be masked to zero for security reasons */
495 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
496 if (err || __copy_from_user(&env, buf, sizeof(env)))
498 convert_to_fxsr(tsk, &env);
503 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
506 struct task_struct *tsk = current;
519 err = restore_i387_fxsave(buf);
521 err = restore_i387_fsave(buf);
523 err = fpregs_soft_set(current, NULL,
524 0, sizeof(struct user_i387_ia32_struct),
533 * FPU state for core dumps.
534 * This is only used for a.out dumps now.
535 * It is declared generically using elf_fpregset_t (which is
536 * struct user_i387_struct) but is in fact only used for 32-bit
537 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
539 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
541 struct task_struct *tsk = current;
544 fpvalid = !!used_math();
546 fpvalid = !fpregs_get(tsk, NULL,
547 0, sizeof(struct user_i387_ia32_struct),
552 EXPORT_SYMBOL(dump_fpu);
554 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */