x86, xsave: dynamically allocate sigframes fpstate instead of static allocation
[linux-2.6/mini2440.git] / arch / x86 / kernel / i387.c
blob51fb288a2c97e6e8a06989231d3273f3979d605b
1 /*
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
7 */
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>
17 #include <asm/i387.h>
18 #include <asm/user.h>
20 #ifdef CONFIG_X86_64
21 # include <asm/sigcontext32.h>
22 # include <asm/user32.h>
23 #else
24 # define save_i387_ia32 save_i387
25 # define restore_i387_ia32 restore_i387
26 # define _fpstate_ia32 _fpstate
27 # define sig_xstate_ia32_size sig_xstate_size
28 # define user_i387_ia32_struct user_i387_struct
29 # define user32_fxsr_struct user_fxsr_struct
30 #endif
32 #ifdef CONFIG_MATH_EMULATION
33 # define HAVE_HWFP (boot_cpu_data.hard_math)
34 #else
35 # define HAVE_HWFP 1
36 #endif
38 static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
39 unsigned int xstate_size;
40 unsigned int sig_xstate_ia32_size = sizeof(struct _fpstate_ia32);
41 static struct i387_fxsave_struct fx_scratch __cpuinitdata;
43 void __cpuinit mxcsr_feature_mask_init(void)
45 unsigned long mask = 0;
47 clts();
48 if (cpu_has_fxsr) {
49 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
50 asm volatile("fxsave %0" : : "m" (fx_scratch));
51 mask = fx_scratch.mxcsr_mask;
52 if (mask == 0)
53 mask = 0x0000ffbf;
55 mxcsr_feature_mask &= mask;
56 stts();
59 void __init init_thread_xstate(void)
61 if (!HAVE_HWFP) {
62 xstate_size = sizeof(struct i387_soft_struct);
63 return;
66 if (cpu_has_xsave) {
67 xsave_cntxt_init();
68 return;
71 if (cpu_has_fxsr)
72 xstate_size = sizeof(struct i387_fxsave_struct);
73 #ifdef CONFIG_X86_32
74 else
75 xstate_size = sizeof(struct i387_fsave_struct);
76 #endif
79 #ifdef CONFIG_X86_64
81 * Called at bootup to set up the initial FPU state that is later cloned
82 * into all processes.
84 void __cpuinit fpu_init(void)
86 unsigned long oldcr0 = read_cr0();
88 set_in_cr4(X86_CR4_OSFXSR);
89 set_in_cr4(X86_CR4_OSXMMEXCPT);
91 write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
94 * Boot processor to setup the FP and extended state context info.
96 if (!smp_processor_id())
97 init_thread_xstate();
98 xsave_init();
100 mxcsr_feature_mask_init();
101 /* clean state in init */
102 if (cpu_has_xsave)
103 current_thread_info()->status = TS_XSAVE;
104 else
105 current_thread_info()->status = 0;
106 clear_used_math();
108 #endif /* CONFIG_X86_64 */
111 * The _current_ task is using the FPU for the first time
112 * so initialize it and set the mxcsr to its default
113 * value at reset if we support XMM instructions and then
114 * remeber the current task has used the FPU.
116 int init_fpu(struct task_struct *tsk)
118 if (tsk_used_math(tsk)) {
119 if (HAVE_HWFP && tsk == current)
120 unlazy_fpu(tsk);
121 return 0;
125 * Memory allocation at the first usage of the FPU and other state.
127 if (!tsk->thread.xstate) {
128 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
129 GFP_KERNEL);
130 if (!tsk->thread.xstate)
131 return -ENOMEM;
134 #ifdef CONFIG_X86_32
135 if (!HAVE_HWFP) {
136 memset(tsk->thread.xstate, 0, xstate_size);
137 finit();
138 set_stopped_child_used_math(tsk);
139 return 0;
141 #endif
143 if (cpu_has_fxsr) {
144 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
146 memset(fx, 0, xstate_size);
147 fx->cwd = 0x37f;
148 if (cpu_has_xmm)
149 fx->mxcsr = MXCSR_DEFAULT;
150 } else {
151 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
152 memset(fp, 0, xstate_size);
153 fp->cwd = 0xffff037fu;
154 fp->swd = 0xffff0000u;
155 fp->twd = 0xffffffffu;
156 fp->fos = 0xffff0000u;
159 * Only the device not available exception or ptrace can call init_fpu.
161 set_stopped_child_used_math(tsk);
162 return 0;
165 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
167 return tsk_used_math(target) ? regset->n : 0;
170 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
172 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
175 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
176 unsigned int pos, unsigned int count,
177 void *kbuf, void __user *ubuf)
179 int ret;
181 if (!cpu_has_fxsr)
182 return -ENODEV;
184 ret = init_fpu(target);
185 if (ret)
186 return ret;
188 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
189 &target->thread.xstate->fxsave, 0, -1);
192 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
193 unsigned int pos, unsigned int count,
194 const void *kbuf, const void __user *ubuf)
196 int ret;
198 if (!cpu_has_fxsr)
199 return -ENODEV;
201 ret = init_fpu(target);
202 if (ret)
203 return ret;
205 set_stopped_child_used_math(target);
207 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
208 &target->thread.xstate->fxsave, 0, -1);
211 * mxcsr reserved bits must be masked to zero for security reasons.
213 target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
215 return ret;
218 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
221 * FPU tag word conversions.
224 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
226 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
228 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
229 tmp = ~twd;
230 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
231 /* and move the valid bits to the lower byte. */
232 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
233 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
234 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
236 return tmp;
239 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
240 #define FP_EXP_TAG_VALID 0
241 #define FP_EXP_TAG_ZERO 1
242 #define FP_EXP_TAG_SPECIAL 2
243 #define FP_EXP_TAG_EMPTY 3
245 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
247 struct _fpxreg *st;
248 u32 tos = (fxsave->swd >> 11) & 7;
249 u32 twd = (unsigned long) fxsave->twd;
250 u32 tag;
251 u32 ret = 0xffff0000u;
252 int i;
254 for (i = 0; i < 8; i++, twd >>= 1) {
255 if (twd & 0x1) {
256 st = FPREG_ADDR(fxsave, (i - tos) & 7);
258 switch (st->exponent & 0x7fff) {
259 case 0x7fff:
260 tag = FP_EXP_TAG_SPECIAL;
261 break;
262 case 0x0000:
263 if (!st->significand[0] &&
264 !st->significand[1] &&
265 !st->significand[2] &&
266 !st->significand[3])
267 tag = FP_EXP_TAG_ZERO;
268 else
269 tag = FP_EXP_TAG_SPECIAL;
270 break;
271 default:
272 if (st->significand[3] & 0x8000)
273 tag = FP_EXP_TAG_VALID;
274 else
275 tag = FP_EXP_TAG_SPECIAL;
276 break;
278 } else {
279 tag = FP_EXP_TAG_EMPTY;
281 ret |= tag << (2 * i);
283 return ret;
287 * FXSR floating point environment conversions.
290 static void
291 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
293 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
294 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
295 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
296 int i;
298 env->cwd = fxsave->cwd | 0xffff0000u;
299 env->swd = fxsave->swd | 0xffff0000u;
300 env->twd = twd_fxsr_to_i387(fxsave);
302 #ifdef CONFIG_X86_64
303 env->fip = fxsave->rip;
304 env->foo = fxsave->rdp;
305 if (tsk == current) {
307 * should be actually ds/cs at fpu exception time, but
308 * that information is not available in 64bit mode.
310 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
311 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
312 } else {
313 struct pt_regs *regs = task_pt_regs(tsk);
315 env->fos = 0xffff0000 | tsk->thread.ds;
316 env->fcs = regs->cs;
318 #else
319 env->fip = fxsave->fip;
320 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
321 env->foo = fxsave->foo;
322 env->fos = fxsave->fos;
323 #endif
325 for (i = 0; i < 8; ++i)
326 memcpy(&to[i], &from[i], sizeof(to[0]));
329 static void convert_to_fxsr(struct task_struct *tsk,
330 const struct user_i387_ia32_struct *env)
333 struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
334 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
335 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
336 int i;
338 fxsave->cwd = env->cwd;
339 fxsave->swd = env->swd;
340 fxsave->twd = twd_i387_to_fxsr(env->twd);
341 fxsave->fop = (u16) ((u32) env->fcs >> 16);
342 #ifdef CONFIG_X86_64
343 fxsave->rip = env->fip;
344 fxsave->rdp = env->foo;
345 /* cs and ds ignored */
346 #else
347 fxsave->fip = env->fip;
348 fxsave->fcs = (env->fcs & 0xffff);
349 fxsave->foo = env->foo;
350 fxsave->fos = env->fos;
351 #endif
353 for (i = 0; i < 8; ++i)
354 memcpy(&to[i], &from[i], sizeof(from[0]));
357 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
358 unsigned int pos, unsigned int count,
359 void *kbuf, void __user *ubuf)
361 struct user_i387_ia32_struct env;
362 int ret;
364 ret = init_fpu(target);
365 if (ret)
366 return ret;
368 if (!HAVE_HWFP)
369 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
371 if (!cpu_has_fxsr) {
372 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
373 &target->thread.xstate->fsave, 0,
374 -1);
377 if (kbuf && pos == 0 && count == sizeof(env)) {
378 convert_from_fxsr(kbuf, target);
379 return 0;
382 convert_from_fxsr(&env, target);
384 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
387 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
388 unsigned int pos, unsigned int count,
389 const void *kbuf, const void __user *ubuf)
391 struct user_i387_ia32_struct env;
392 int ret;
394 ret = init_fpu(target);
395 if (ret)
396 return ret;
398 set_stopped_child_used_math(target);
400 if (!HAVE_HWFP)
401 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
403 if (!cpu_has_fxsr) {
404 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
405 &target->thread.xstate->fsave, 0, -1);
408 if (pos > 0 || count < sizeof(env))
409 convert_from_fxsr(&env, target);
411 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
412 if (!ret)
413 convert_to_fxsr(target, &env);
415 return ret;
419 * Signal frame handlers.
422 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
424 struct task_struct *tsk = current;
425 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
427 unlazy_fpu(tsk);
428 fp->status = fp->swd;
429 if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
430 return -1;
431 return 1;
434 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
436 struct task_struct *tsk = current;
437 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
438 struct user_i387_ia32_struct env;
439 int err = 0;
441 unlazy_fpu(tsk);
443 convert_from_fxsr(&env, tsk);
444 if (__copy_to_user(buf, &env, sizeof(env)))
445 return -1;
447 err |= __put_user(fx->swd, &buf->status);
448 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
449 if (err)
450 return -1;
452 if (__copy_to_user(&buf->_fxsr_env[0], fx,
453 sizeof(struct i387_fxsave_struct)))
454 return -1;
455 return 1;
458 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
460 if (!used_math())
461 return 0;
463 * This will cause a "finit" to be triggered by the next
464 * attempted FPU operation by the 'current' process.
466 clear_used_math();
468 if (!HAVE_HWFP) {
469 return fpregs_soft_get(current, NULL,
470 0, sizeof(struct user_i387_ia32_struct),
471 NULL, buf) ? -1 : 1;
474 if (cpu_has_fxsr)
475 return save_i387_fxsave(buf);
476 else
477 return save_i387_fsave(buf);
480 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
482 struct task_struct *tsk = current;
484 return __copy_from_user(&tsk->thread.xstate->fsave, buf,
485 sizeof(struct i387_fsave_struct));
488 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
490 struct task_struct *tsk = current;
491 struct user_i387_ia32_struct env;
492 int err;
494 err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
495 sizeof(struct i387_fxsave_struct));
496 /* mxcsr reserved bits must be masked to zero for security reasons */
497 tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
498 if (err || __copy_from_user(&env, buf, sizeof(env)))
499 return 1;
500 convert_to_fxsr(tsk, &env);
502 return 0;
505 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
507 int err;
508 struct task_struct *tsk = current;
510 if (HAVE_HWFP)
511 clear_fpu(tsk);
513 if (!used_math()) {
514 err = init_fpu(tsk);
515 if (err)
516 return err;
519 if (HAVE_HWFP) {
520 if (cpu_has_fxsr)
521 err = restore_i387_fxsave(buf);
522 else
523 err = restore_i387_fsave(buf);
524 } else {
525 err = fpregs_soft_set(current, NULL,
526 0, sizeof(struct user_i387_ia32_struct),
527 NULL, buf) != 0;
529 set_used_math();
531 return err;
535 * FPU state for core dumps.
536 * This is only used for a.out dumps now.
537 * It is declared generically using elf_fpregset_t (which is
538 * struct user_i387_struct) but is in fact only used for 32-bit
539 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
541 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
543 struct task_struct *tsk = current;
544 int fpvalid;
546 fpvalid = !!used_math();
547 if (fpvalid)
548 fpvalid = !fpregs_get(tsk, NULL,
549 0, sizeof(struct user_i387_ia32_struct),
550 fpu, NULL);
552 return fpvalid;
554 EXPORT_SYMBOL(dump_fpu);
556 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */