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