Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / i386 / kernel / i387.c
1 /*
2  *  linux/arch/i386/kernel/i387.c
3  *
4  *  Copyright (C) 1994 Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *  General FPU state handling cleanups
8  *      Gareth Hughes <gareth@valinux.com>, May 2000
9  */
10
11 #include <linux/sched.h>
12 #include <linux/module.h>
13 #include <asm/processor.h>
14 #include <asm/i387.h>
15 #include <asm/math_emu.h>
16 #include <asm/sigcontext.h>
17 #include <asm/user.h>
18 #include <asm/ptrace.h>
19 #include <asm/uaccess.h>
20
21 #ifdef CONFIG_MATH_EMULATION
22 #define HAVE_HWFP (boot_cpu_data.hard_math)
23 #else
24 #define HAVE_HWFP 1
25 #endif
26
27 static unsigned long mxcsr_feature_mask __read_mostly = 0xffffffff;
28
29 void mxcsr_feature_mask_init(void)
30 {
31         unsigned long mask = 0;
32         clts();
33         if (cpu_has_fxsr) {
34                 memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
35                 asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave)); 
36                 mask = current->thread.i387.fxsave.mxcsr_mask;
37                 if (mask == 0) mask = 0x0000ffbf;
38         } 
39         mxcsr_feature_mask &= mask;
40         stts();
41 }
42
43 /*
44  * The _current_ task is using the FPU for the first time
45  * so initialize it and set the mxcsr to its default
46  * value at reset if we support XMM instructions and then
47  * remeber the current task has used the FPU.
48  */
49 void init_fpu(struct task_struct *tsk)
50 {
51         if (cpu_has_fxsr) {
52                 memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
53                 tsk->thread.i387.fxsave.cwd = 0x37f;
54                 if (cpu_has_xmm)
55                         tsk->thread.i387.fxsave.mxcsr = 0x1f80;
56         } else {
57                 memset(&tsk->thread.i387.fsave, 0, sizeof(struct i387_fsave_struct));
58                 tsk->thread.i387.fsave.cwd = 0xffff037fu;
59                 tsk->thread.i387.fsave.swd = 0xffff0000u;
60                 tsk->thread.i387.fsave.twd = 0xffffffffu;
61                 tsk->thread.i387.fsave.fos = 0xffff0000u;
62         }
63         /* only the device not available exception or ptrace can call init_fpu */
64         set_stopped_child_used_math(tsk);
65 }
66
67 /*
68  * FPU lazy state save handling.
69  */
70
71 void kernel_fpu_begin(void)
72 {
73         struct thread_info *thread = current_thread_info();
74
75         preempt_disable();
76         if (thread->status & TS_USEDFPU) {
77                 __save_init_fpu(thread->task);
78                 return;
79         }
80         clts();
81 }
82 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
83
84 /*
85  * FPU tag word conversions.
86  */
87
88 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
89 {
90         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
91  
92         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
93         tmp = ~twd;
94         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
95         /* and move the valid bits to the lower byte. */
96         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
97         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
98         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
99         return tmp;
100 }
101
102 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
103 {
104         struct _fpxreg *st = NULL;
105         unsigned long tos = (fxsave->swd >> 11) & 7;
106         unsigned long twd = (unsigned long) fxsave->twd;
107         unsigned long tag;
108         unsigned long ret = 0xffff0000u;
109         int i;
110
111 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16);
112
113         for ( i = 0 ; i < 8 ; i++ ) {
114                 if ( twd & 0x1 ) {
115                         st = FPREG_ADDR( fxsave, (i - tos) & 7 );
116
117                         switch ( st->exponent & 0x7fff ) {
118                         case 0x7fff:
119                                 tag = 2;                /* Special */
120                                 break;
121                         case 0x0000:
122                                 if ( !st->significand[0] &&
123                                      !st->significand[1] &&
124                                      !st->significand[2] &&
125                                      !st->significand[3] ) {
126                                         tag = 1;        /* Zero */
127                                 } else {
128                                         tag = 2;        /* Special */
129                                 }
130                                 break;
131                         default:
132                                 if ( st->significand[3] & 0x8000 ) {
133                                         tag = 0;        /* Valid */
134                                 } else {
135                                         tag = 2;        /* Special */
136                                 }
137                                 break;
138                         }
139                 } else {
140                         tag = 3;                        /* Empty */
141                 }
142                 ret |= (tag << (2 * i));
143                 twd = twd >> 1;
144         }
145         return ret;
146 }
147
148 /*
149  * FPU state interaction.
150  */
151
152 unsigned short get_fpu_cwd( struct task_struct *tsk )
153 {
154         if ( cpu_has_fxsr ) {
155                 return tsk->thread.i387.fxsave.cwd;
156         } else {
157                 return (unsigned short)tsk->thread.i387.fsave.cwd;
158         }
159 }
160
161 unsigned short get_fpu_swd( struct task_struct *tsk )
162 {
163         if ( cpu_has_fxsr ) {
164                 return tsk->thread.i387.fxsave.swd;
165         } else {
166                 return (unsigned short)tsk->thread.i387.fsave.swd;
167         }
168 }
169
170 #if 0
171 unsigned short get_fpu_twd( struct task_struct *tsk )
172 {
173         if ( cpu_has_fxsr ) {
174                 return tsk->thread.i387.fxsave.twd;
175         } else {
176                 return (unsigned short)tsk->thread.i387.fsave.twd;
177         }
178 }
179 #endif  /*  0  */
180
181 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
182 {
183         if ( cpu_has_xmm ) {
184                 return tsk->thread.i387.fxsave.mxcsr;
185         } else {
186                 return 0x1f80;
187         }
188 }
189
190 #if 0
191
192 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
193 {
194         if ( cpu_has_fxsr ) {
195                 tsk->thread.i387.fxsave.cwd = cwd;
196         } else {
197                 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
198         }
199 }
200
201 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
202 {
203         if ( cpu_has_fxsr ) {
204                 tsk->thread.i387.fxsave.swd = swd;
205         } else {
206                 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
207         }
208 }
209
210 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
211 {
212         if ( cpu_has_fxsr ) {
213                 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
214         } else {
215                 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
216         }
217 }
218
219 #endif  /*  0  */
220
221 /*
222  * FXSR floating point environment conversions.
223  */
224
225 static inline void
226 convert_fxsr_env_to_i387(unsigned long env[7],
227                          struct i387_fxsave_struct *fxsave)
228 {
229         env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
230         env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
231         env[2] = twd_fxsr_to_i387(fxsave);
232         env[3] = fxsave->fip;
233         env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
234         env[5] = fxsave->foo;
235         env[6] = fxsave->fos;
236 }
237
238 static int convert_fxsr_to_user(struct _fpstate __user *buf,
239                                 struct i387_fxsave_struct *fxsave)
240 {
241         unsigned long env[7];
242         struct _fpreg __user *to;
243         struct _fpxreg *from;
244         int i;
245
246         convert_fxsr_env_to_i387(env, fxsave);
247         if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
248                 return 1;
249
250         to = &buf->_st[0];
251         from = (struct _fpxreg *) &fxsave->st_space[0];
252         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
253                 unsigned long __user *t = (unsigned long __user *)to;
254                 unsigned long *f = (unsigned long *)from;
255
256                 if (__put_user(*f, t) ||
257                                 __put_user(*(f + 1), t + 1) ||
258                                 __put_user(from->exponent, &to->exponent))
259                         return 1;
260         }
261         return 0;
262 }
263
264 static inline void
265 convert_fxsr_env_from_i387(struct i387_fxsave_struct *fxsave,
266                            const unsigned long env[7])
267 {
268         fxsave->cwd = (unsigned short)(env[0] & 0xffff);
269         fxsave->swd = (unsigned short)(env[1] & 0xffff);
270         fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
271         fxsave->fip = env[3];
272         fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
273         fxsave->fcs = (env[4] & 0xffff);
274         fxsave->foo = env[5];
275         fxsave->fos = env[6];
276 }
277
278 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
279                                           struct _fpstate __user *buf )
280 {
281         unsigned long env[7];
282         struct _fpxreg *to;
283         struct _fpreg __user *from;
284         int i;
285
286         if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
287                 return 1;
288
289         convert_fxsr_env_from_i387(fxsave, env);
290
291         to = (struct _fpxreg *) &fxsave->st_space[0];
292         from = &buf->_st[0];
293         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
294                 unsigned long *t = (unsigned long *)to;
295                 unsigned long __user *f = (unsigned long __user *)from;
296
297                 if (__get_user(*t, f) ||
298                                 __get_user(*(t + 1), f + 1) ||
299                                 __get_user(to->exponent, &from->exponent))
300                         return 1;
301         }
302         return 0;
303 }
304
305 /*
306  * Signal frame handlers.
307  */
308
309 static inline int save_i387_fsave( struct _fpstate __user *buf )
310 {
311         struct task_struct *tsk = current;
312
313         unlazy_fpu( tsk );
314         tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
315         if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
316                              sizeof(struct i387_fsave_struct) ) )
317                 return -1;
318         return 1;
319 }
320
321 static int save_i387_fxsave( struct _fpstate __user *buf )
322 {
323         struct task_struct *tsk = current;
324         int err = 0;
325
326         unlazy_fpu( tsk );
327
328         if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
329                 return -1;
330
331         err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
332         err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
333         if ( err )
334                 return -1;
335
336         if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
337                              sizeof(struct i387_fxsave_struct) ) )
338                 return -1;
339         return 1;
340 }
341
342 int save_i387( struct _fpstate __user *buf )
343 {
344         if ( !used_math() )
345                 return 0;
346
347         /* This will cause a "finit" to be triggered by the next
348          * attempted FPU operation by the 'current' process.
349          */
350         clear_used_math();
351
352         if ( HAVE_HWFP ) {
353                 if ( cpu_has_fxsr ) {
354                         return save_i387_fxsave( buf );
355                 } else {
356                         return save_i387_fsave( buf );
357                 }
358         } else {
359                 return save_i387_soft( &current->thread.i387.soft, buf );
360         }
361 }
362
363 static inline int restore_i387_fsave( struct _fpstate __user *buf )
364 {
365         struct task_struct *tsk = current;
366         clear_fpu( tsk );
367         return __copy_from_user( &tsk->thread.i387.fsave, buf,
368                                  sizeof(struct i387_fsave_struct) );
369 }
370
371 static int restore_i387_fxsave( struct _fpstate __user *buf )
372 {
373         int err;
374         struct task_struct *tsk = current;
375         clear_fpu( tsk );
376         err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
377                                 sizeof(struct i387_fxsave_struct) );
378         /* mxcsr reserved bits must be masked to zero for security reasons */
379         tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
380         return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
381 }
382
383 int restore_i387( struct _fpstate __user *buf )
384 {
385         int err;
386
387         if ( HAVE_HWFP ) {
388                 if ( cpu_has_fxsr ) {
389                         err = restore_i387_fxsave( buf );
390                 } else {
391                         err = restore_i387_fsave( buf );
392                 }
393         } else {
394                 err = restore_i387_soft( &current->thread.i387.soft, buf );
395         }
396         set_used_math();
397         return err;
398 }
399
400 /*
401  * ptrace request handlers.
402  */
403
404 static inline void get_fpregs_fsave(struct user_i387_struct *buf,
405                                     struct task_struct *tsk)
406 {
407         memcpy(buf, &tsk->thread.i387.fsave, sizeof(struct user_i387_struct));
408 }
409
410 static inline void get_fpregs_fxsave(struct user_i387_struct *buf,
411                                      struct task_struct *tsk)
412 {
413         struct _fpreg *to;
414         const struct _fpxreg *from;
415         unsigned int i;
416
417         convert_fxsr_env_to_i387((unsigned long *) buf,
418                                  &tsk->thread.i387.fxsave);
419
420         to = (struct _fpreg *) buf->st_space;
421         from = (const struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
422         for (i = 0; i < 8; i++, to++, from++)
423                 *to = *(const struct _fpreg *) from;
424 }
425
426 int get_fpregs(struct user_i387_struct *buf, struct task_struct *tsk)
427 {
428         if ( HAVE_HWFP ) {
429                 if (cpu_has_fxsr)
430                         get_fpregs_fxsave(buf, tsk);
431                 else
432                         get_fpregs_fsave(buf, tsk);
433                 return 0;
434         } else {
435                 return save_i387_soft( &tsk->thread.i387.soft,
436                                        (struct _fpstate __user *)buf );
437         }
438 }
439
440 static inline void set_fpregs_fsave(struct task_struct *tsk,
441                                     const struct user_i387_struct *buf)
442 {
443         memcpy(&tsk->thread.i387.fsave, buf, sizeof(struct user_i387_struct));
444 }
445
446 static inline void set_fpregs_fxsave(struct task_struct *tsk,
447                                      const struct user_i387_struct *buf)
448 {
449         struct _fpxreg *to;
450         const struct _fpreg *from;
451         unsigned int i;
452
453         convert_fxsr_env_from_i387(&tsk->thread.i387.fxsave,
454                                    (unsigned long *) buf);
455
456         to = (struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
457         from = (const struct _fpreg *) buf->st_space;
458         for (i = 0; i < 8; i++, to++, from++)
459                 *(struct _fpreg *) to = *from;
460 }
461
462 int set_fpregs(struct task_struct *tsk, const struct user_i387_struct *buf)
463 {
464         if ( HAVE_HWFP ) {
465                 if (cpu_has_fxsr)
466                         set_fpregs_fxsave(tsk, buf);
467                 else
468                         set_fpregs_fsave(tsk, buf);
469                 return 0;
470         } else {
471                 return restore_i387_soft( &tsk->thread.i387.soft,
472                                           (struct _fpstate __user *)buf );
473         }
474 }
475
476 void updated_fpxregs(struct task_struct *tsk)
477 {
478         /* mxcsr reserved bits must be masked to zero for security reasons */
479         tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
480 }
481
482 /*
483  * FPU state for core dumps.
484  */
485
486 static inline void copy_fpu_fsave( struct task_struct *tsk,
487                                    struct user_i387_struct *fpu )
488 {
489         memcpy( fpu, &tsk->thread.i387.fsave,
490                 sizeof(struct user_i387_struct) );
491 }
492
493 static inline void copy_fpu_fxsave( struct task_struct *tsk,
494                                    struct user_i387_struct *fpu )
495 {
496         unsigned short *to;
497         unsigned short *from;
498         int i;
499
500         memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
501
502         to = (unsigned short *)&fpu->st_space[0];
503         from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
504         for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
505                 memcpy( to, from, 5 * sizeof(unsigned short) );
506         }
507 }
508
509 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
510 {
511         int fpvalid;
512         struct task_struct *tsk = current;
513
514         fpvalid = !!used_math();
515         if ( fpvalid ) {
516                 unlazy_fpu( tsk );
517                 if ( cpu_has_fxsr ) {
518                         copy_fpu_fxsave( tsk, fpu );
519                 } else {
520                         copy_fpu_fsave( tsk, fpu );
521                 }
522         }
523
524         return fpvalid;
525 }
526 EXPORT_SYMBOL(dump_fpu);
527
528 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
529 {
530         int fpvalid = !!tsk_used_math(tsk);
531
532         if (fpvalid) {
533                 if (tsk == current)
534                         unlazy_fpu(tsk);
535                 if (cpu_has_fxsr)
536                         copy_fpu_fxsave(tsk, fpu);
537                 else
538                         copy_fpu_fsave(tsk, fpu);
539         }
540         return fpvalid;
541 }
542
543 int dump_task_extended_fpu(struct task_struct *tsk, struct user_fxsr_struct *fpu)
544 {
545         int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
546
547         if (fpvalid) {
548                 if (tsk == current)
549                        unlazy_fpu(tsk);
550                 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
551         }
552         return fpvalid;
553 }