[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / i386 / kernel / i387.c
blobc55e037f08f76dca232da6a0851c1c2d368887ef
1 /*
2 * linux/arch/i386/kernel/i387.c
4 * Copyright (C) 1994 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * General FPU state handling cleanups
8 * Gareth Hughes <gareth@valinux.com>, May 2000
9 */
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>
21 #ifdef CONFIG_MATH_EMULATION
22 #define HAVE_HWFP (boot_cpu_data.hard_math)
23 #else
24 #define HAVE_HWFP 1
25 #endif
27 static unsigned long mxcsr_feature_mask = 0xffffffff;
29 void mxcsr_feature_mask_init(void)
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;
39 mxcsr_feature_mask &= mask;
40 stts();
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.
49 void init_fpu(struct task_struct *tsk)
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;
63 /* only the device not available exception or ptrace can call init_fpu */
64 set_stopped_child_used_math(tsk);
68 * FPU lazy state save handling.
71 void kernel_fpu_begin(void)
73 struct thread_info *thread = current_thread_info();
75 preempt_disable();
76 if (thread->status & TS_USEDFPU) {
77 __save_init_fpu(thread->task);
78 return;
80 clts();
83 void restore_fpu( struct task_struct *tsk )
85 if ( cpu_has_fxsr ) {
86 asm volatile( "fxrstor %0"
87 : : "m" (tsk->thread.i387.fxsave) );
88 } else {
89 asm volatile( "frstor %0"
90 : : "m" (tsk->thread.i387.fsave) );
95 * FPU tag word conversions.
98 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
100 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
102 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
103 tmp = ~twd;
104 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
105 /* and move the valid bits to the lower byte. */
106 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
107 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
108 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
109 return tmp;
112 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
114 struct _fpxreg *st = NULL;
115 unsigned long tos = (fxsave->swd >> 11) & 7;
116 unsigned long twd = (unsigned long) fxsave->twd;
117 unsigned long tag;
118 unsigned long ret = 0xffff0000u;
119 int i;
121 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
123 for ( i = 0 ; i < 8 ; i++ ) {
124 if ( twd & 0x1 ) {
125 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
127 switch ( st->exponent & 0x7fff ) {
128 case 0x7fff:
129 tag = 2; /* Special */
130 break;
131 case 0x0000:
132 if ( !st->significand[0] &&
133 !st->significand[1] &&
134 !st->significand[2] &&
135 !st->significand[3] ) {
136 tag = 1; /* Zero */
137 } else {
138 tag = 2; /* Special */
140 break;
141 default:
142 if ( st->significand[3] & 0x8000 ) {
143 tag = 0; /* Valid */
144 } else {
145 tag = 2; /* Special */
147 break;
149 } else {
150 tag = 3; /* Empty */
152 ret |= (tag << (2 * i));
153 twd = twd >> 1;
155 return ret;
159 * FPU state interaction.
162 unsigned short get_fpu_cwd( struct task_struct *tsk )
164 if ( cpu_has_fxsr ) {
165 return tsk->thread.i387.fxsave.cwd;
166 } else {
167 return (unsigned short)tsk->thread.i387.fsave.cwd;
171 unsigned short get_fpu_swd( struct task_struct *tsk )
173 if ( cpu_has_fxsr ) {
174 return tsk->thread.i387.fxsave.swd;
175 } else {
176 return (unsigned short)tsk->thread.i387.fsave.swd;
180 #if 0
181 unsigned short get_fpu_twd( struct task_struct *tsk )
183 if ( cpu_has_fxsr ) {
184 return tsk->thread.i387.fxsave.twd;
185 } else {
186 return (unsigned short)tsk->thread.i387.fsave.twd;
189 #endif /* 0 */
191 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
193 if ( cpu_has_xmm ) {
194 return tsk->thread.i387.fxsave.mxcsr;
195 } else {
196 return 0x1f80;
200 #if 0
202 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
204 if ( cpu_has_fxsr ) {
205 tsk->thread.i387.fxsave.cwd = cwd;
206 } else {
207 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
211 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
213 if ( cpu_has_fxsr ) {
214 tsk->thread.i387.fxsave.swd = swd;
215 } else {
216 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
220 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
222 if ( cpu_has_fxsr ) {
223 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
224 } else {
225 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
229 #endif /* 0 */
232 * FXSR floating point environment conversions.
235 static int convert_fxsr_to_user( struct _fpstate __user *buf,
236 struct i387_fxsave_struct *fxsave )
238 unsigned long env[7];
239 struct _fpreg __user *to;
240 struct _fpxreg *from;
241 int i;
243 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
244 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
245 env[2] = twd_fxsr_to_i387(fxsave);
246 env[3] = fxsave->fip;
247 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
248 env[5] = fxsave->foo;
249 env[6] = fxsave->fos;
251 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
252 return 1;
254 to = &buf->_st[0];
255 from = (struct _fpxreg *) &fxsave->st_space[0];
256 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
257 unsigned long __user *t = (unsigned long __user *)to;
258 unsigned long *f = (unsigned long *)from;
260 if (__put_user(*f, t) ||
261 __put_user(*(f + 1), t + 1) ||
262 __put_user(from->exponent, &to->exponent))
263 return 1;
265 return 0;
268 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
269 struct _fpstate __user *buf )
271 unsigned long env[7];
272 struct _fpxreg *to;
273 struct _fpreg __user *from;
274 int i;
276 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
277 return 1;
279 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
280 fxsave->swd = (unsigned short)(env[1] & 0xffff);
281 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
282 fxsave->fip = env[3];
283 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
284 fxsave->fcs = (env[4] & 0xffff);
285 fxsave->foo = env[5];
286 fxsave->fos = env[6];
288 to = (struct _fpxreg *) &fxsave->st_space[0];
289 from = &buf->_st[0];
290 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
291 unsigned long *t = (unsigned long *)to;
292 unsigned long __user *f = (unsigned long __user *)from;
294 if (__get_user(*t, f) ||
295 __get_user(*(t + 1), f + 1) ||
296 __get_user(to->exponent, &from->exponent))
297 return 1;
299 return 0;
303 * Signal frame handlers.
306 static inline int save_i387_fsave( struct _fpstate __user *buf )
308 struct task_struct *tsk = current;
310 unlazy_fpu( tsk );
311 tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
312 if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
313 sizeof(struct i387_fsave_struct) ) )
314 return -1;
315 return 1;
318 static int save_i387_fxsave( struct _fpstate __user *buf )
320 struct task_struct *tsk = current;
321 int err = 0;
323 unlazy_fpu( tsk );
325 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
326 return -1;
328 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
329 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
330 if ( err )
331 return -1;
333 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
334 sizeof(struct i387_fxsave_struct) ) )
335 return -1;
336 return 1;
339 int save_i387( struct _fpstate __user *buf )
341 if ( !used_math() )
342 return 0;
344 /* This will cause a "finit" to be triggered by the next
345 * attempted FPU operation by the 'current' process.
347 clear_used_math();
349 if ( HAVE_HWFP ) {
350 if ( cpu_has_fxsr ) {
351 return save_i387_fxsave( buf );
352 } else {
353 return save_i387_fsave( buf );
355 } else {
356 return save_i387_soft( &current->thread.i387.soft, buf );
360 static inline int restore_i387_fsave( struct _fpstate __user *buf )
362 struct task_struct *tsk = current;
363 clear_fpu( tsk );
364 return __copy_from_user( &tsk->thread.i387.fsave, buf,
365 sizeof(struct i387_fsave_struct) );
368 static int restore_i387_fxsave( struct _fpstate __user *buf )
370 int err;
371 struct task_struct *tsk = current;
372 clear_fpu( tsk );
373 err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
374 sizeof(struct i387_fxsave_struct) );
375 /* mxcsr reserved bits must be masked to zero for security reasons */
376 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
377 return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
380 int restore_i387( struct _fpstate __user *buf )
382 int err;
384 if ( HAVE_HWFP ) {
385 if ( cpu_has_fxsr ) {
386 err = restore_i387_fxsave( buf );
387 } else {
388 err = restore_i387_fsave( buf );
390 } else {
391 err = restore_i387_soft( &current->thread.i387.soft, buf );
393 set_used_math();
394 return err;
398 * ptrace request handlers.
401 static inline int get_fpregs_fsave( struct user_i387_struct __user *buf,
402 struct task_struct *tsk )
404 return __copy_to_user( buf, &tsk->thread.i387.fsave,
405 sizeof(struct user_i387_struct) );
408 static inline int get_fpregs_fxsave( struct user_i387_struct __user *buf,
409 struct task_struct *tsk )
411 return convert_fxsr_to_user( (struct _fpstate __user *)buf,
412 &tsk->thread.i387.fxsave );
415 int get_fpregs( struct user_i387_struct __user *buf, struct task_struct *tsk )
417 if ( HAVE_HWFP ) {
418 if ( cpu_has_fxsr ) {
419 return get_fpregs_fxsave( buf, tsk );
420 } else {
421 return get_fpregs_fsave( buf, tsk );
423 } else {
424 return save_i387_soft( &tsk->thread.i387.soft,
425 (struct _fpstate __user *)buf );
429 static inline int set_fpregs_fsave( struct task_struct *tsk,
430 struct user_i387_struct __user *buf )
432 return __copy_from_user( &tsk->thread.i387.fsave, buf,
433 sizeof(struct user_i387_struct) );
436 static inline int set_fpregs_fxsave( struct task_struct *tsk,
437 struct user_i387_struct __user *buf )
439 return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
440 (struct _fpstate __user *)buf );
443 int set_fpregs( struct task_struct *tsk, struct user_i387_struct __user *buf )
445 if ( HAVE_HWFP ) {
446 if ( cpu_has_fxsr ) {
447 return set_fpregs_fxsave( tsk, buf );
448 } else {
449 return set_fpregs_fsave( tsk, buf );
451 } else {
452 return restore_i387_soft( &tsk->thread.i387.soft,
453 (struct _fpstate __user *)buf );
457 int get_fpxregs( struct user_fxsr_struct __user *buf, struct task_struct *tsk )
459 if ( cpu_has_fxsr ) {
460 if (__copy_to_user( buf, &tsk->thread.i387.fxsave,
461 sizeof(struct user_fxsr_struct) ))
462 return -EFAULT;
463 return 0;
464 } else {
465 return -EIO;
469 int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct __user *buf )
471 int ret = 0;
473 if ( cpu_has_fxsr ) {
474 if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
475 sizeof(struct user_fxsr_struct) ))
476 ret = -EFAULT;
477 /* mxcsr reserved bits must be masked to zero for security reasons */
478 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
479 } else {
480 ret = -EIO;
482 return ret;
486 * FPU state for core dumps.
489 static inline void copy_fpu_fsave( struct task_struct *tsk,
490 struct user_i387_struct *fpu )
492 memcpy( fpu, &tsk->thread.i387.fsave,
493 sizeof(struct user_i387_struct) );
496 static inline void copy_fpu_fxsave( struct task_struct *tsk,
497 struct user_i387_struct *fpu )
499 unsigned short *to;
500 unsigned short *from;
501 int i;
503 memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
505 to = (unsigned short *)&fpu->st_space[0];
506 from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
507 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
508 memcpy( to, from, 5 * sizeof(unsigned short) );
512 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
514 int fpvalid;
515 struct task_struct *tsk = current;
517 fpvalid = !!used_math();
518 if ( fpvalid ) {
519 unlazy_fpu( tsk );
520 if ( cpu_has_fxsr ) {
521 copy_fpu_fxsave( tsk, fpu );
522 } else {
523 copy_fpu_fsave( tsk, fpu );
527 return fpvalid;
530 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
532 int fpvalid = !!tsk_used_math(tsk);
534 if (fpvalid) {
535 if (tsk == current)
536 unlazy_fpu(tsk);
537 if (cpu_has_fxsr)
538 copy_fpu_fxsave(tsk, fpu);
539 else
540 copy_fpu_fsave(tsk, fpu);
542 return fpvalid;
545 int dump_task_extended_fpu(struct task_struct *tsk, struct user_fxsr_struct *fpu)
547 int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
549 if (fpvalid) {
550 if (tsk == current)
551 unlazy_fpu(tsk);
552 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
554 return fpvalid;