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/bootmem.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>
22 # include <asm/sigcontext32.h>
23 # include <asm/user32.h>
25 # define save_i387_ia32 save_i387
26 # define restore_i387_ia32 restore_i387
27 # define _fpstate_ia32 _fpstate
28 # define user_i387_ia32_struct user_i387_struct
29 # define user32_fxsr_struct user_fxsr_struct
32 #ifdef CONFIG_MATH_EMULATION
33 # define HAVE_HWFP (boot_cpu_data.hard_math)
38 static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
39 unsigned int xstate_size;
40 static struct i387_fxsave_struct fx_scratch __cpuinitdata;
42 void __cpuinit mxcsr_feature_mask_init(void)
44 unsigned long mask = 0;
48 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
49 asm volatile("fxsave %0" : : "m" (fx_scratch));
50 mask = fx_scratch.mxcsr_mask;
54 mxcsr_feature_mask &= mask;
58 void __init init_thread_xstate(void)
61 xstate_size = sizeof(struct i387_fxsave_struct);
64 xstate_size = sizeof(struct i387_fsave_struct);
66 init_task.thread.xstate = alloc_bootmem(xstate_size);
71 * Called at bootup to set up the initial FPU state that is later cloned
74 void __cpuinit fpu_init(void)
76 unsigned long oldcr0 = read_cr0();
78 set_in_cr4(X86_CR4_OSFXSR);
79 set_in_cr4(X86_CR4_OSXMMEXCPT);
81 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
83 mxcsr_feature_mask_init();
84 /* clean state in init */
85 current_thread_info()->status = 0;
88 #endif /* CONFIG_X86_64 */
91 * The _current_ task is using the FPU for the first time
92 * so initialize it and set the mxcsr to its default
93 * value at reset if we support XMM instructions and then
94 * remeber the current task has used the FPU.
96 void init_fpu(struct task_struct *tsk)
98 if (tsk_used_math(tsk)) {
105 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
107 memset(fx, 0, xstate_size);
110 fx->mxcsr = MXCSR_DEFAULT;
112 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
113 memset(fp, 0, xstate_size);
114 fp->cwd = 0xffff037fu;
115 fp->swd = 0xffff0000u;
116 fp->twd = 0xffffffffu;
117 fp->fos = 0xffff0000u;
120 * Only the device not available exception or ptrace can call init_fpu.
122 set_stopped_child_used_math(tsk);
125 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
127 return tsk_used_math(target) ? regset->n : 0;
130 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
132 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
135 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
136 unsigned int pos, unsigned int count,
137 void *kbuf, void __user *ubuf)
144 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
145 &target->thread.xstate->fxsave, 0, -1);
148 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
149 unsigned int pos, unsigned int count,
150 const void *kbuf, const void __user *ubuf)
158 set_stopped_child_used_math(target);
160 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
161 &target->thread.xstate->fxsave, 0, -1);
164 * mxcsr reserved bits must be masked to zero for security reasons.
166 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
171 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
174 * FPU tag word conversions.
177 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
179 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
181 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
183 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
184 /* and move the valid bits to the lower byte. */
185 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
186 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
187 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
192 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
193 #define FP_EXP_TAG_VALID 0
194 #define FP_EXP_TAG_ZERO 1
195 #define FP_EXP_TAG_SPECIAL 2
196 #define FP_EXP_TAG_EMPTY 3
198 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
201 u32 tos = (fxsave->swd >> 11) & 7;
202 u32 twd = (unsigned long) fxsave->twd;
204 u32 ret = 0xffff0000u;
207 for (i = 0; i < 8; i++, twd >>= 1) {
209 st = FPREG_ADDR(fxsave, (i - tos) & 7);
211 switch (st->exponent & 0x7fff) {
213 tag = FP_EXP_TAG_SPECIAL;
216 if (!st->significand[0] &&
217 !st->significand[1] &&
218 !st->significand[2] &&
220 tag = FP_EXP_TAG_ZERO;
222 tag = FP_EXP_TAG_SPECIAL;
225 if (st->significand[3] & 0x8000)
226 tag = FP_EXP_TAG_VALID;
228 tag = FP_EXP_TAG_SPECIAL;
232 tag = FP_EXP_TAG_EMPTY;
234 ret |= tag << (2 * i);
240 * FXSR floating point environment conversions.
244 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
246 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
247 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
248 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
251 env->cwd = fxsave->cwd | 0xffff0000u;
252 env->swd = fxsave->swd | 0xffff0000u;
253 env->twd = twd_fxsr_to_i387(fxsave);
256 env->fip = fxsave->rip;
257 env->foo = fxsave->rdp;
258 if (tsk == current) {
260 * should be actually ds/cs at fpu exception time, but
261 * that information is not available in 64bit mode.
263 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
264 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
266 struct pt_regs *regs = task_pt_regs(tsk);
268 env->fos = 0xffff0000 | tsk->thread.ds;
272 env->fip = fxsave->fip;
273 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
274 env->foo = fxsave->foo;
275 env->fos = fxsave->fos;
278 for (i = 0; i < 8; ++i)
279 memcpy(&to[i], &from[i], sizeof(to[0]));
282 static void convert_to_fxsr(struct task_struct *tsk,
283 const struct user_i387_ia32_struct *env)
286 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
287 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
288 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
291 fxsave->cwd = env->cwd;
292 fxsave->swd = env->swd;
293 fxsave->twd = twd_i387_to_fxsr(env->twd);
294 fxsave->fop = (u16) ((u32) env->fcs >> 16);
296 fxsave->rip = env->fip;
297 fxsave->rdp = env->foo;
298 /* cs and ds ignored */
300 fxsave->fip = env->fip;
301 fxsave->fcs = (env->fcs & 0xffff);
302 fxsave->foo = env->foo;
303 fxsave->fos = env->fos;
306 for (i = 0; i < 8; ++i)
307 memcpy(&to[i], &from[i], sizeof(from[0]));
310 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
311 unsigned int pos, unsigned int count,
312 void *kbuf, void __user *ubuf)
314 struct user_i387_ia32_struct env;
317 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
322 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
323 &target->thread.xstate->fsave, 0,
327 if (kbuf && pos == 0 && count == sizeof(env)) {
328 convert_from_fxsr(kbuf, target);
332 convert_from_fxsr(&env, target);
334 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
337 int fpregs_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)
341 struct user_i387_ia32_struct env;
345 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
348 set_stopped_child_used_math(target);
351 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
352 &target->thread.xstate->fsave, 0, -1);
355 if (pos > 0 || count < sizeof(env))
356 convert_from_fxsr(&env, target);
358 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
360 convert_to_fxsr(target, &env);
366 * Signal frame handlers.
369 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
371 struct task_struct *tsk = current;
372 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
375 fp->status = fp->swd;
376 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
381 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
383 struct task_struct *tsk = current;
384 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
385 struct user_i387_ia32_struct env;
390 convert_from_fxsr(&env, tsk);
391 if (__copy_to_user(buf, &env, sizeof(env)))
394 err |= __put_user(fx->swd, &buf->status);
395 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
399 if (__copy_to_user(&buf->_fxsr_env[0], fx,
400 sizeof(struct i387_fxsave_struct)))
405 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
410 * This will cause a "finit" to be triggered by the next
411 * attempted FPU operation by the 'current' process.
416 return fpregs_soft_get(current, NULL,
417 0, sizeof(struct user_i387_ia32_struct),
422 return save_i387_fxsave(buf);
424 return save_i387_fsave(buf);
427 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
429 struct task_struct *tsk = current;
432 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
433 sizeof(struct i387_fsave_struct));
436 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
438 struct task_struct *tsk = current;
439 struct user_i387_ia32_struct env;
443 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
444 sizeof(struct i387_fxsave_struct));
445 /* mxcsr reserved bits must be masked to zero for security reasons */
446 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
447 if (err || __copy_from_user(&env, buf, sizeof(env)))
449 convert_to_fxsr(tsk, &env);
454 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
460 err = restore_i387_fxsave(buf);
462 err = restore_i387_fsave(buf);
464 err = fpregs_soft_set(current, NULL,
465 0, sizeof(struct user_i387_ia32_struct),
474 * FPU state for core dumps.
475 * This is only used for a.out dumps now.
476 * It is declared generically using elf_fpregset_t (which is
477 * struct user_i387_struct) but is in fact only used for 32-bit
478 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
480 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
482 struct task_struct *tsk = current;
485 fpvalid = !!used_math();
487 fpvalid = !fpregs_get(tsk, NULL,
488 0, sizeof(struct user_i387_ia32_struct),
493 EXPORT_SYMBOL(dump_fpu);
495 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */