]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/i387.c
x86, xsave: dynamically allocate sigframes fpstate instead of static allocation
[karo-tx-linux.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 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
31
32 #ifdef CONFIG_MATH_EMULATION
33 # define HAVE_HWFP              (boot_cpu_data.hard_math)
34 #else
35 # define HAVE_HWFP              1
36 #endif
37
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;
42
43 void __cpuinit mxcsr_feature_mask_init(void)
44 {
45         unsigned long mask = 0;
46
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;
54         }
55         mxcsr_feature_mask &= mask;
56         stts();
57 }
58
59 void __init init_thread_xstate(void)
60 {
61         if (!HAVE_HWFP) {
62                 xstate_size = sizeof(struct i387_soft_struct);
63                 return;
64         }
65
66         if (cpu_has_xsave) {
67                 xsave_cntxt_init();
68                 return;
69         }
70
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
77 }
78
79 #ifdef CONFIG_X86_64
80 /*
81  * Called at bootup to set up the initial FPU state that is later cloned
82  * into all processes.
83  */
84 void __cpuinit fpu_init(void)
85 {
86         unsigned long oldcr0 = read_cr0();
87
88         set_in_cr4(X86_CR4_OSFXSR);
89         set_in_cr4(X86_CR4_OSXMMEXCPT);
90
91         write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
92
93         /*
94          * Boot processor to setup the FP and extended state context info.
95          */
96         if (!smp_processor_id())
97                 init_thread_xstate();
98         xsave_init();
99
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();
107 }
108 #endif  /* CONFIG_X86_64 */
109
110 /*
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.
115  */
116 int init_fpu(struct task_struct *tsk)
117 {
118         if (tsk_used_math(tsk)) {
119                 if (HAVE_HWFP && tsk == current)
120                         unlazy_fpu(tsk);
121                 return 0;
122         }
123
124         /*
125          * Memory allocation at the first usage of the FPU and other state.
126          */
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;
132         }
133
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;
140         }
141 #endif
142
143         if (cpu_has_fxsr) {
144                 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
145
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;
157         }
158         /*
159          * Only the device not available exception or ptrace can call init_fpu.
160          */
161         set_stopped_child_used_math(tsk);
162         return 0;
163 }
164
165 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
166 {
167         return tsk_used_math(target) ? regset->n : 0;
168 }
169
170 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
171 {
172         return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
173 }
174
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)
178 {
179         int ret;
180
181         if (!cpu_has_fxsr)
182                 return -ENODEV;
183
184         ret = init_fpu(target);
185         if (ret)
186                 return ret;
187
188         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
189                                    &target->thread.xstate->fxsave, 0, -1);
190 }
191
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)
195 {
196         int ret;
197
198         if (!cpu_has_fxsr)
199                 return -ENODEV;
200
201         ret = init_fpu(target);
202         if (ret)
203                 return ret;
204
205         set_stopped_child_used_math(target);
206
207         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
208                                  &target->thread.xstate->fxsave, 0, -1);
209
210         /*
211          * mxcsr reserved bits must be masked to zero for security reasons.
212          */
213         target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
214
215         return ret;
216 }
217
218 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
219
220 /*
221  * FPU tag word conversions.
222  */
223
224 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
225 {
226         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
227
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 */
235
236         return tmp;
237 }
238
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
244
245 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
246 {
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;
253
254         for (i = 0; i < 8; i++, twd >>= 1) {
255                 if (twd & 0x1) {
256                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
257
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;
277                         }
278                 } else {
279                         tag = FP_EXP_TAG_EMPTY;
280                 }
281                 ret |= tag << (2 * i);
282         }
283         return ret;
284 }
285
286 /*
287  * FXSR floating point environment conversions.
288  */
289
290 static void
291 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
292 {
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;
297
298         env->cwd = fxsave->cwd | 0xffff0000u;
299         env->swd = fxsave->swd | 0xffff0000u;
300         env->twd = twd_fxsr_to_i387(fxsave);
301
302 #ifdef CONFIG_X86_64
303         env->fip = fxsave->rip;
304         env->foo = fxsave->rdp;
305         if (tsk == current) {
306                 /*
307                  * should be actually ds/cs at fpu exception time, but
308                  * that information is not available in 64bit mode.
309                  */
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);
314
315                 env->fos = 0xffff0000 | tsk->thread.ds;
316                 env->fcs = regs->cs;
317         }
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
324
325         for (i = 0; i < 8; ++i)
326                 memcpy(&to[i], &from[i], sizeof(to[0]));
327 }
328
329 static void convert_to_fxsr(struct task_struct *tsk,
330                             const struct user_i387_ia32_struct *env)
331
332 {
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;
337
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
352
353         for (i = 0; i < 8; ++i)
354                 memcpy(&to[i], &from[i], sizeof(from[0]));
355 }
356
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)
360 {
361         struct user_i387_ia32_struct env;
362         int ret;
363
364         ret = init_fpu(target);
365         if (ret)
366                 return ret;
367
368         if (!HAVE_HWFP)
369                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
370
371         if (!cpu_has_fxsr) {
372                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
373                                            &target->thread.xstate->fsave, 0,
374                                            -1);
375         }
376
377         if (kbuf && pos == 0 && count == sizeof(env)) {
378                 convert_from_fxsr(kbuf, target);
379                 return 0;
380         }
381
382         convert_from_fxsr(&env, target);
383
384         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
385 }
386
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)
390 {
391         struct user_i387_ia32_struct env;
392         int ret;
393
394         ret = init_fpu(target);
395         if (ret)
396                 return ret;
397
398         set_stopped_child_used_math(target);
399
400         if (!HAVE_HWFP)
401                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
402
403         if (!cpu_has_fxsr) {
404                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
405                                           &target->thread.xstate->fsave, 0, -1);
406         }
407
408         if (pos > 0 || count < sizeof(env))
409                 convert_from_fxsr(&env, target);
410
411         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
412         if (!ret)
413                 convert_to_fxsr(target, &env);
414
415         return ret;
416 }
417
418 /*
419  * Signal frame handlers.
420  */
421
422 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
423 {
424         struct task_struct *tsk = current;
425         struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
426
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;
432 }
433
434 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
435 {
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;
440
441         unlazy_fpu(tsk);
442
443         convert_from_fxsr(&env, tsk);
444         if (__copy_to_user(buf, &env, sizeof(env)))
445                 return -1;
446
447         err |= __put_user(fx->swd, &buf->status);
448         err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
449         if (err)
450                 return -1;
451
452         if (__copy_to_user(&buf->_fxsr_env[0], fx,
453                            sizeof(struct i387_fxsave_struct)))
454                 return -1;
455         return 1;
456 }
457
458 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
459 {
460         if (!used_math())
461                 return 0;
462         /*
463          * This will cause a "finit" to be triggered by the next
464          * attempted FPU operation by the 'current' process.
465          */
466         clear_used_math();
467
468         if (!HAVE_HWFP) {
469                 return fpregs_soft_get(current, NULL,
470                                        0, sizeof(struct user_i387_ia32_struct),
471                                        NULL, buf) ? -1 : 1;
472         }
473
474         if (cpu_has_fxsr)
475                 return save_i387_fxsave(buf);
476         else
477                 return save_i387_fsave(buf);
478 }
479
480 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
481 {
482         struct task_struct *tsk = current;
483
484         return __copy_from_user(&tsk->thread.xstate->fsave, buf,
485                                 sizeof(struct i387_fsave_struct));
486 }
487
488 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
489 {
490         struct task_struct *tsk = current;
491         struct user_i387_ia32_struct env;
492         int err;
493
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);
501
502         return 0;
503 }
504
505 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
506 {
507         int err;
508         struct task_struct *tsk = current;
509
510         if (HAVE_HWFP)
511                 clear_fpu(tsk);
512
513         if (!used_math()) {
514                 err = init_fpu(tsk);
515                 if (err)
516                         return err;
517         }
518
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;
528         }
529         set_used_math();
530
531         return err;
532 }
533
534 /*
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.
540  */
541 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
542 {
543         struct task_struct *tsk = current;
544         int fpvalid;
545
546         fpvalid = !!used_math();
547         if (fpvalid)
548                 fpvalid = !fpregs_get(tsk, NULL,
549                                       0, sizeof(struct user_i387_ia32_struct),
550                                       fpu, NULL);
551
552         return fpvalid;
553 }
554 EXPORT_SYMBOL(dump_fpu);
555
556 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */