]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/i387.c
baf632b221d43366b1f026b28add587e249e5840
[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 #include <linux/bootmem.h>
12
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>
18 #include <asm/i387.h>
19 #include <asm/user.h>
20
21 #ifdef CONFIG_X86_64
22 # include <asm/sigcontext32.h>
23 # include <asm/user32.h>
24 #else
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
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 static struct i387_fxsave_struct fx_scratch __cpuinitdata;
41
42 void __cpuinit mxcsr_feature_mask_init(void)
43 {
44         unsigned long mask = 0;
45
46         clts();
47         if (cpu_has_fxsr) {
48                 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
49                 asm volatile("fxsave %0" : : "m" (fx_scratch));
50                 mask = fx_scratch.mxcsr_mask;
51                 if (mask == 0)
52                         mask = 0x0000ffbf;
53         }
54         mxcsr_feature_mask &= mask;
55         stts();
56 }
57
58 void __init init_thread_xstate(void)
59 {
60         if (cpu_has_fxsr)
61                 xstate_size = sizeof(struct i387_fxsave_struct);
62 #ifdef CONFIG_X86_32
63         else
64                 xstate_size = sizeof(struct i387_fsave_struct);
65 #endif
66         init_task.thread.xstate = alloc_bootmem(xstate_size);
67 }
68
69 #ifdef CONFIG_X86_64
70 /*
71  * Called at bootup to set up the initial FPU state that is later cloned
72  * into all processes.
73  */
74 void __cpuinit fpu_init(void)
75 {
76         unsigned long oldcr0 = read_cr0();
77
78         set_in_cr4(X86_CR4_OSFXSR);
79         set_in_cr4(X86_CR4_OSXMMEXCPT);
80
81         write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */
82
83         mxcsr_feature_mask_init();
84         /* clean state in init */
85         current_thread_info()->status = 0;
86         clear_used_math();
87 }
88 #endif  /* CONFIG_X86_64 */
89
90 /*
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.
95  */
96 void init_fpu(struct task_struct *tsk)
97 {
98         if (tsk_used_math(tsk)) {
99                 if (tsk == current)
100                         unlazy_fpu(tsk);
101                 return;
102         }
103
104         if (cpu_has_fxsr) {
105                 struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
106
107                 memset(fx, 0, xstate_size);
108                 fx->cwd = 0x37f;
109                 if (cpu_has_xmm)
110                         fx->mxcsr = MXCSR_DEFAULT;
111         } else {
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;
118         }
119         /*
120          * Only the device not available exception or ptrace can call init_fpu.
121          */
122         set_stopped_child_used_math(tsk);
123 }
124
125 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
126 {
127         return tsk_used_math(target) ? regset->n : 0;
128 }
129
130 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
131 {
132         return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
133 }
134
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)
138 {
139         if (!cpu_has_fxsr)
140                 return -ENODEV;
141
142         init_fpu(target);
143
144         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
145                                    &target->thread.xstate->fxsave, 0, -1);
146 }
147
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)
151 {
152         int ret;
153
154         if (!cpu_has_fxsr)
155                 return -ENODEV;
156
157         init_fpu(target);
158         set_stopped_child_used_math(target);
159
160         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
161                                  &target->thread.xstate->fxsave, 0, -1);
162
163         /*
164          * mxcsr reserved bits must be masked to zero for security reasons.
165          */
166         target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask;
167
168         return ret;
169 }
170
171 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
172
173 /*
174  * FPU tag word conversions.
175  */
176
177 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
178 {
179         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
180
181         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
182         tmp = ~twd;
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 */
188
189         return tmp;
190 }
191
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
197
198 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
199 {
200         struct _fpxreg *st;
201         u32 tos = (fxsave->swd >> 11) & 7;
202         u32 twd = (unsigned long) fxsave->twd;
203         u32 tag;
204         u32 ret = 0xffff0000u;
205         int i;
206
207         for (i = 0; i < 8; i++, twd >>= 1) {
208                 if (twd & 0x1) {
209                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
210
211                         switch (st->exponent & 0x7fff) {
212                         case 0x7fff:
213                                 tag = FP_EXP_TAG_SPECIAL;
214                                 break;
215                         case 0x0000:
216                                 if (!st->significand[0] &&
217                                     !st->significand[1] &&
218                                     !st->significand[2] &&
219                                     !st->significand[3])
220                                         tag = FP_EXP_TAG_ZERO;
221                                 else
222                                         tag = FP_EXP_TAG_SPECIAL;
223                                 break;
224                         default:
225                                 if (st->significand[3] & 0x8000)
226                                         tag = FP_EXP_TAG_VALID;
227                                 else
228                                         tag = FP_EXP_TAG_SPECIAL;
229                                 break;
230                         }
231                 } else {
232                         tag = FP_EXP_TAG_EMPTY;
233                 }
234                 ret |= tag << (2 * i);
235         }
236         return ret;
237 }
238
239 /*
240  * FXSR floating point environment conversions.
241  */
242
243 static void
244 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
245 {
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];
249         int i;
250
251         env->cwd = fxsave->cwd | 0xffff0000u;
252         env->swd = fxsave->swd | 0xffff0000u;
253         env->twd = twd_fxsr_to_i387(fxsave);
254
255 #ifdef CONFIG_X86_64
256         env->fip = fxsave->rip;
257         env->foo = fxsave->rdp;
258         if (tsk == current) {
259                 /*
260                  * should be actually ds/cs at fpu exception time, but
261                  * that information is not available in 64bit mode.
262                  */
263                 asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos));
264                 asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs));
265         } else {
266                 struct pt_regs *regs = task_pt_regs(tsk);
267
268                 env->fos = 0xffff0000 | tsk->thread.ds;
269                 env->fcs = regs->cs;
270         }
271 #else
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;
276 #endif
277
278         for (i = 0; i < 8; ++i)
279                 memcpy(&to[i], &from[i], sizeof(to[0]));
280 }
281
282 static void convert_to_fxsr(struct task_struct *tsk,
283                             const struct user_i387_ia32_struct *env)
284
285 {
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];
289         int i;
290
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);
295 #ifdef CONFIG_X86_64
296         fxsave->rip = env->fip;
297         fxsave->rdp = env->foo;
298         /* cs and ds ignored */
299 #else
300         fxsave->fip = env->fip;
301         fxsave->fcs = (env->fcs & 0xffff);
302         fxsave->foo = env->foo;
303         fxsave->fos = env->fos;
304 #endif
305
306         for (i = 0; i < 8; ++i)
307                 memcpy(&to[i], &from[i], sizeof(from[0]));
308 }
309
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)
313 {
314         struct user_i387_ia32_struct env;
315
316         if (!HAVE_HWFP)
317                 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
318
319         init_fpu(target);
320
321         if (!cpu_has_fxsr) {
322                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
323                                            &target->thread.xstate->fsave, 0,
324                                            -1);
325         }
326
327         if (kbuf && pos == 0 && count == sizeof(env)) {
328                 convert_from_fxsr(kbuf, target);
329                 return 0;
330         }
331
332         convert_from_fxsr(&env, target);
333
334         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
335 }
336
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)
340 {
341         struct user_i387_ia32_struct env;
342         int ret;
343
344         if (!HAVE_HWFP)
345                 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
346
347         init_fpu(target);
348         set_stopped_child_used_math(target);
349
350         if (!cpu_has_fxsr) {
351                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
352                                           &target->thread.xstate->fsave, 0, -1);
353         }
354
355         if (pos > 0 || count < sizeof(env))
356                 convert_from_fxsr(&env, target);
357
358         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
359         if (!ret)
360                 convert_to_fxsr(target, &env);
361
362         return ret;
363 }
364
365 /*
366  * Signal frame handlers.
367  */
368
369 static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
370 {
371         struct task_struct *tsk = current;
372         struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave;
373
374         unlazy_fpu(tsk);
375         fp->status = fp->swd;
376         if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct)))
377                 return -1;
378         return 1;
379 }
380
381 static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
382 {
383         struct task_struct *tsk = current;
384         struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave;
385         struct user_i387_ia32_struct env;
386         int err = 0;
387
388         unlazy_fpu(tsk);
389
390         convert_from_fxsr(&env, tsk);
391         if (__copy_to_user(buf, &env, sizeof(env)))
392                 return -1;
393
394         err |= __put_user(fx->swd, &buf->status);
395         err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
396         if (err)
397                 return -1;
398
399         if (__copy_to_user(&buf->_fxsr_env[0], fx,
400                            sizeof(struct i387_fxsave_struct)))
401                 return -1;
402         return 1;
403 }
404
405 int save_i387_ia32(struct _fpstate_ia32 __user *buf)
406 {
407         if (!used_math())
408                 return 0;
409         /*
410          * This will cause a "finit" to be triggered by the next
411          * attempted FPU operation by the 'current' process.
412          */
413         clear_used_math();
414
415         if (!HAVE_HWFP) {
416                 return fpregs_soft_get(current, NULL,
417                                        0, sizeof(struct user_i387_ia32_struct),
418                                        NULL, buf) ? -1 : 1;
419         }
420
421         if (cpu_has_fxsr)
422                 return save_i387_fxsave(buf);
423         else
424                 return save_i387_fsave(buf);
425 }
426
427 static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
428 {
429         struct task_struct *tsk = current;
430
431         clear_fpu(tsk);
432         return __copy_from_user(&tsk->thread.xstate->fsave, buf,
433                                 sizeof(struct i387_fsave_struct));
434 }
435
436 static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
437 {
438         struct task_struct *tsk = current;
439         struct user_i387_ia32_struct env;
440         int err;
441
442         clear_fpu(tsk);
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)))
448                 return 1;
449         convert_to_fxsr(tsk, &env);
450
451         return 0;
452 }
453
454 int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
455 {
456         int err;
457
458         if (HAVE_HWFP) {
459                 if (cpu_has_fxsr)
460                         err = restore_i387_fxsave(buf);
461                 else
462                         err = restore_i387_fsave(buf);
463         } else {
464                 err = fpregs_soft_set(current, NULL,
465                                       0, sizeof(struct user_i387_ia32_struct),
466                                       NULL, buf) != 0;
467         }
468         set_used_math();
469
470         return err;
471 }
472
473 /*
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.
479  */
480 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
481 {
482         struct task_struct *tsk = current;
483         int fpvalid;
484
485         fpvalid = !!used_math();
486         if (fpvalid)
487                 fpvalid = !fpregs_get(tsk, NULL,
488                                       0, sizeof(struct user_i387_ia32_struct),
489                                       fpu, NULL);
490
491         return fpvalid;
492 }
493 EXPORT_SYMBOL(dump_fpu);
494
495 #endif  /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */