e22a9a9dce8a81a95e99c502be9d92c2a3af3e77
[pandora-kernel.git] / arch / x86 / kernel / i387.c
1 /*
2  *  Copyright (C) 1994 Linus Torvalds
3  *
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>
11
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>
19
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 user_i387_ia32_struct  user_i387_struct
28 # define user32_fxsr_struct     user_fxsr_struct
29 #endif
30
31 #ifdef CONFIG_MATH_EMULATION
32 # define HAVE_HWFP              (boot_cpu_data.hard_math)
33 #else
34 # define HAVE_HWFP              1
35 #endif
36
37 static unsigned int             mxcsr_feature_mask __read_mostly = 0xffffffffu;
38 unsigned int xstate_size;
39 static struct i387_fxsave_struct fx_scratch __cpuinitdata;
40
41 void __cpuinit mxcsr_feature_mask_init(void)
42 {
43         unsigned long mask = 0;
44
45         clts();
46         if (cpu_has_fxsr) {
47                 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
48                 asm volatile("fxsave %0" : : "m" (fx_scratch));
49                 mask = fx_scratch.mxcsr_mask;
50                 if (mask == 0)
51                         mask = 0x0000ffbf;
52         }
53         mxcsr_feature_mask &= mask;
54         stts();
55 }
56
57 void __init init_thread_xstate(void)
58 {
59         if (!HAVE_HWFP) {
60                 xstate_size = sizeof(struct i387_soft_struct);
61                 return;
62         }
63
64         if (cpu_has_xsave) {
65                 xsave_cntxt_init();
66                 return;
67         }
68
69         if (cpu_has_fxsr)
70                 xstate_size = sizeof(struct i387_fxsave_struct);
71 #ifdef CONFIG_X86_32
72         else
73                 xstate_size = sizeof(struct i387_fsave_struct);
74 #endif
75 }
76
77 #ifdef CONFIG_X86_64
78 /*
79  * Called at bootup to set up the initial FPU state that is later cloned
80  * into all processes.
81  */
82 void __cpuinit fpu_init(void)
83 {
84         unsigned long oldcr0 = read_cr0();
85
86         set_in_cr4(X86_CR4_OSFXSR);
87         set_in_cr4(X86_CR4_OSXMMEXCPT);
88
89         write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
90
91         /*
92          * Boot processor to setup the FP and extended state context info.
93          */
94         if (!smp_processor_id())
95                 init_thread_xstate();
96         xsave_init();
97
98         mxcsr_feature_mask_init();
99         /* clean state in init */
100         current_thread_info()->status = 0;
101         clear_used_math();
102 }
103 #endif  /* CONFIG_X86_64 */
104
105 /*
106  * The _current_ task is using the FPU for the first time
107  * so initialize it and set the mxcsr to its default
108  * value at reset if we support XMM instructions and then
109  * remeber the current task has used the FPU.
110  */
111 int init_fpu(struct task_struct *tsk)
112 {
113         if (tsk_used_math(tsk)) {
114                 if (HAVE_HWFP && tsk == current)
115                         unlazy_fpu(tsk);
116                 return 0;
117         }
118
119         /*
120          * Memory allocation at the first usage of the FPU and other state.
121          */
122         if (!tsk->thread.xstate) {
123                 tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
124                                                       GFP_KERNEL);
125                 if (!tsk->thread.xstate)
126                         return -ENOMEM;
127         }
128
129 #ifdef CONFIG_X86_32
130         if (!HAVE_HWFP) {
131                 memset(tsk->thread.xstate, 0, xstate_size);
132                 finit();
133                 set_stopped_child_used_math(tsk);
134                 return 0;
135         }
136 #endif
137
138         if (cpu_has_fxsr) {
139                 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
140
141                 memset(fx, 0, xstate_size);
142                 fx->cwd = 0x37f;
143                 if (cpu_has_xmm)
144                         fx->mxcsr = MXCSR_DEFAULT;
145         } else {
146                 struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
147                 memset(fp, 0, xstate_size);
148                 fp->cwd = 0xffff037fu;
149                 fp->swd = 0xffff0000u;
150                 fp->twd = 0xffffffffu;
151                 fp->fos = 0xffff0000u;
152         }
153         /*
154          * Only the device not available exception or ptrace can call init_fpu.
155          */
156         set_stopped_child_used_math(tsk);
157         return 0;
158 }
159
160 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
161 {
162         return tsk_used_math(target) ? regset->n : 0;
163 }
164
165 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
166 {
167         return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
168 }
169
170 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
171                 unsigned int pos, unsigned int count,
172                 void *kbuf, void __user *ubuf)
173 {
174         int ret;
175
176         if (!cpu_has_fxsr)
177                 return -ENODEV;
178
179         ret = init_fpu(target);
180         if (ret)
181                 return ret;
182
183         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
184                                    &target->thread.xstate->fxsave, 0, -1);
185 }
186
187 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
188                 unsigned int pos, unsigned int count,
189                 const void *kbuf, const void __user *ubuf)
190 {
191         int ret;
192
193         if (!cpu_has_fxsr)
194                 return -ENODEV;
195
196         ret = init_fpu(target);
197         if (ret)
198                 return ret;
199
200         set_stopped_child_used_math(target);
201
202         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
203                                  &target->thread.xstate->fxsave, 0, -1);
204
205         /*
206          * mxcsr reserved bits must be masked to zero for security reasons.
207          */
208         target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
209
210         return ret;
211 }
212
213 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
214
215 /*
216  * FPU tag word conversions.
217  */
218
219 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
220 {
221         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
222
223         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
224         tmp = ~twd;
225         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
226         /* and move the valid bits to the lower byte. */
227         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
228         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
229         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
230
231         return tmp;
232 }
233
234 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16);
235 #define FP_EXP_TAG_VALID        0
236 #define FP_EXP_TAG_ZERO         1
237 #define FP_EXP_TAG_SPECIAL      2
238 #define FP_EXP_TAG_EMPTY        3
239
240 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
241 {
242         struct _fpxreg *st;
243         u32 tos = (fxsave->swd >> 11) & 7;
244         u32 twd = (unsigned long) fxsave->twd;
245         u32 tag;
246         u32 ret = 0xffff0000u;
247         int i;
248
249         for (i = 0; i < 8; i++, twd >>= 1) {
250                 if (twd & 0x1) {
251                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
252
253                         switch (st->exponent & 0x7fff) {
254                         case 0x7fff:
255                                 tag = FP_EXP_TAG_SPECIAL;
256                                 break;
257                         case 0x0000:
258                                 if (!st->significand[0] &&
259                                     !st->significand[1] &&
260                                     !st->significand[2] &&
261                                     !st->significand[3])
262                                         tag = FP_EXP_TAG_ZERO;
263                                 else
264                                         tag = FP_EXP_TAG_SPECIAL;
265                                 break;
266                         default:
267                                 if (st->significand[3] & 0x8000)
268                                         tag = FP_EXP_TAG_VALID;
269                                 else
270                                         tag = FP_EXP_TAG_SPECIAL;
271                                 break;
272                         }
273                 } else {
274                         tag = FP_EXP_TAG_EMPTY;
275                 }
276                 ret |= tag << (2 * i);
277         }
278         return ret;
279 }
280
281 /*
282  * FXSR floating point environment conversions.
283  */
284
285 static void
286 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
287 {
288         struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
289         struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
290         struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
291         int i;
292
293         env->cwd = fxsave->cwd | 0xffff0000u;
294         env->swd = fxsave->swd | 0xffff0000u;
295         env->twd = twd_fxsr_to_i387(fxsave);
296
297 #ifdef CONFIG_X86_64
298         env->fip = fxsave->rip;
299         env->foo = fxsave->rdp;
300         if (tsk == current) {
301                 /*
302                  * should be actually ds/cs at fpu exception time, but
303                  * that information is not available in 64bit mode.
304                  */
305                 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
306                 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
307         } else {
308                 struct pt_regs *regs = task_pt_regs(tsk);
309
310                 env->fos = 0xffff0000 | tsk->thread.ds;
311                 env->fcs = regs->cs;
312         }
313 #else
314         env->fip = fxsave->fip;
315         env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
316         env->foo = fxsave->foo;
317         env->fos = fxsave->fos;
318 #endif
319
320         for (i = 0; i < 8; ++i)
321                 memcpy(&to[i], &from[i], sizeof(to[0]));
322 }
323
324 static void convert_to_fxsr(struct task_struct *tsk,
325                             const struct user_i387_ia32_struct *env)
326
327 {
328         struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave;
329         struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
330         struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
331         int i;
332
333         fxsave->cwd = env->cwd;
334         fxsave->swd = env->swd;
335         fxsave->twd = twd_i387_to_fxsr(env->twd);
336         fxsave->fop = (u16) ((u32) env->fcs >> 16);
337 #ifdef CONFIG_X86_64
338         fxsave->rip = env->fip;
339         fxsave->rdp = env->foo;
340         /* cs and ds ignored */
341 #else
342         fxsave->fip = env->fip;
343         fxsave->fcs = (env->fcs & 0xffff);
344         fxsave->foo = env->foo;
345         fxsave->fos = env->fos;
346 #endif
347
348         for (i = 0; i < 8; ++i)
349                 memcpy(&to[i], &from[i], sizeof(from[0]));
350 }
351
352 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
353                unsigned int pos, unsigned int count,
354                void *kbuf, void __user *ubuf)
355 {
356         struct user_i387_ia32_struct env;
357         int ret;
358
359         ret = init_fpu(target);
360         if (ret)
361                 return ret;
362
363         if (!HAVE_HWFP)
364                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
365
366         if (!cpu_has_fxsr) {
367                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
368                                            &target->thread.xstate->fsave, 0,
369                                            -1);
370         }
371
372         if (kbuf && pos == 0 && count == sizeof(env)) {
373                 convert_from_fxsr(kbuf, target);
374                 return 0;
375         }
376
377         convert_from_fxsr(&env, target);
378
379         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
380 }
381
382 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
383                unsigned int pos, unsigned int count,
384                const void *kbuf, const void __user *ubuf)
385 {
386         struct user_i387_ia32_struct env;
387         int ret;
388
389         ret = init_fpu(target);
390         if (ret)
391                 return ret;
392
393         set_stopped_child_used_math(target);
394
395         if (!HAVE_HWFP)
396                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
397
398         if (!cpu_has_fxsr) {
399                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
400                                           &target->thread.xstate->fsave, 0, -1);
401         }
402
403         if (pos > 0 || count < sizeof(env))
404                 convert_from_fxsr(&env, target);
405
406         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
407         if (!ret)
408                 convert_to_fxsr(target, &env);
409
410         return ret;
411 }
412
413 /*
414  * Signal frame handlers.
415  */
416
417 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
418 {
419         struct task_struct *tsk = current;
420         struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
421
422         unlazy_fpu(tsk);
423         fp->status = fp->swd;
424         if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
425                 return -1;
426         return 1;
427 }
428
429 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
430 {
431         struct task_struct *tsk = current;
432         struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
433         struct user_i387_ia32_struct env;
434         int err = 0;
435
436         unlazy_fpu(tsk);
437
438         convert_from_fxsr(&env, tsk);
439         if (__copy_to_user(buf, &env, sizeof(env)))
440                 return -1;
441
442         err |= __put_user(fx->swd, &buf->status);
443         err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
444         if (err)
445                 return -1;
446
447         if (__copy_to_user(&buf->_fxsr_env[0], fx,
448                            sizeof(struct i387_fxsave_struct)))
449                 return -1;
450         return 1;
451 }
452
453 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
454 {
455         if (!used_math())
456                 return 0;
457         /*
458          * This will cause a "finit" to be triggered by the next
459          * attempted FPU operation by the 'current' process.
460          */
461         clear_used_math();
462
463         if (!HAVE_HWFP) {
464                 return fpregs_soft_get(current, NULL,
465                                        0, sizeof(struct user_i387_ia32_struct),
466                                        NULL, buf) ? -1 : 1;
467         }
468
469         if (cpu_has_fxsr)
470                 return save_i387_fxsave(buf);
471         else
472                 return save_i387_fsave(buf);
473 }
474
475 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
476 {
477         struct task_struct *tsk = current;
478
479         return __copy_from_user(&tsk->thread.xstate->fsave, buf,
480                                 sizeof(struct i387_fsave_struct));
481 }
482
483 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
484 {
485         struct task_struct *tsk = current;
486         struct user_i387_ia32_struct env;
487         int err;
488
489         err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0],
490                                sizeof(struct i387_fxsave_struct));
491         /* mxcsr reserved bits must be masked to zero for security reasons */
492         tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
493         if (err || __copy_from_user(&env, buf, sizeof(env)))
494                 return 1;
495         convert_to_fxsr(tsk, &env);
496
497         return 0;
498 }
499
500 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
501 {
502         int err;
503         struct task_struct *tsk = current;
504
505         if (HAVE_HWFP)
506                 clear_fpu(tsk);
507
508         if (!used_math()) {
509                 err = init_fpu(tsk);
510                 if (err)
511                         return err;
512         }
513
514         if (HAVE_HWFP) {
515                 if (cpu_has_fxsr)
516                         err = restore_i387_fxsave(buf);
517                 else
518                         err = restore_i387_fsave(buf);
519         } else {
520                 err = fpregs_soft_set(current, NULL,
521                                       0, sizeof(struct user_i387_ia32_struct),
522                                       NULL, buf) != 0;
523         }
524         set_used_math();
525
526         return err;
527 }
528
529 /*
530  * FPU state for core dumps.
531  * This is only used for a.out dumps now.
532  * It is declared generically using elf_fpregset_t (which is
533  * struct user_i387_struct) but is in fact only used for 32-bit
534  * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
535  */
536 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
537 {
538         struct task_struct *tsk = current;
539         int fpvalid;
540
541         fpvalid = !!used_math();
542         if (fpvalid)
543                 fpvalid = !fpregs_get(tsk, NULL,
544                                       0, sizeof(struct user_i387_ia32_struct),
545                                       fpu, NULL);
546
547         return fpvalid;
548 }
549 EXPORT_SYMBOL(dump_fpu);
550
551 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */