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