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