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
11 #include <linux/config.h>
12 #include <linux/sched.h>
13 #include <asm/processor.h>
15 #include <asm/math_emu.h>
16 #include <asm/sigcontext.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)
27 unsigned long mxcsr_feature_mask = 0xffffffff;
29 void mxcsr_feature_mask_init(void)
31 unsigned long mask = 0;
34 memset(¤t->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;
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)
52 memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
53 tsk->thread.i387.fxsave.cwd = 0x37f;
55 tsk->thread.i387.fxsave.mxcsr = 0x1f80;
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;
67 * FPU lazy state save handling.
70 void kernel_fpu_begin(void)
72 struct thread_info *thread = current_thread_info();
75 if (thread->status & TS_USEDFPU) {
76 __save_init_fpu(thread->task);
82 void restore_fpu( struct task_struct *tsk )
85 asm volatile( "fxrstor %0"
86 : : "m" (tsk->thread.i387.fxsave) );
88 asm volatile( "frstor %0"
89 : : "m" (tsk->thread.i387.fsave) );
94 * FPU tag word conversions.
97 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
99 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
101 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
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 */
111 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
113 struct _fpxreg *st = NULL;
114 unsigned long twd = (unsigned long) fxsave->twd;
116 unsigned long ret = 0xffff0000u;
119 #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
121 for ( i = 0 ; i < 8 ; i++ ) {
123 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
125 switch ( st->exponent & 0x7fff ) {
127 tag = 2; /* Special */
130 if ( !st->significand[0] &&
131 !st->significand[1] &&
132 !st->significand[2] &&
133 !st->significand[3] ) {
136 tag = 2; /* Special */
140 if ( st->significand[3] & 0x8000 ) {
143 tag = 2; /* Special */
150 ret |= (tag << (2 * i));
157 * FPU state interaction.
160 unsigned short get_fpu_cwd( struct task_struct *tsk )
162 if ( cpu_has_fxsr ) {
163 return tsk->thread.i387.fxsave.cwd;
165 return (unsigned short)tsk->thread.i387.fsave.cwd;
169 unsigned short get_fpu_swd( struct task_struct *tsk )
171 if ( cpu_has_fxsr ) {
172 return tsk->thread.i387.fxsave.swd;
174 return (unsigned short)tsk->thread.i387.fsave.swd;
178 unsigned short get_fpu_twd( struct task_struct *tsk )
180 if ( cpu_has_fxsr ) {
181 return tsk->thread.i387.fxsave.twd;
183 return (unsigned short)tsk->thread.i387.fsave.twd;
187 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
190 return tsk->thread.i387.fxsave.mxcsr;
196 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
198 if ( cpu_has_fxsr ) {
199 tsk->thread.i387.fxsave.cwd = cwd;
201 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
205 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
207 if ( cpu_has_fxsr ) {
208 tsk->thread.i387.fxsave.swd = swd;
210 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
214 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
216 if ( cpu_has_fxsr ) {
217 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
219 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
224 * FXSR floating point environment conversions.
227 static int convert_fxsr_to_user( struct _fpstate __user *buf,
228 struct i387_fxsave_struct *fxsave )
230 unsigned long env[7];
231 struct _fpreg __user *to;
232 struct _fpxreg *from;
235 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
236 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
237 env[2] = twd_fxsr_to_i387(fxsave);
238 env[3] = fxsave->fip;
239 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
240 env[5] = fxsave->foo;
241 env[6] = fxsave->fos;
243 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
247 from = (struct _fpxreg *) &fxsave->st_space[0];
248 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
249 unsigned long *t = (unsigned long *)to;
250 unsigned long *f = (unsigned long *)from;
252 if (__put_user(*f, t) ||
253 __put_user(*(f + 1), t + 1) ||
254 __put_user(from->exponent, &to->exponent))
260 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
261 struct _fpstate __user *buf )
263 unsigned long env[7];
265 struct _fpreg __user *from;
268 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
271 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
272 fxsave->swd = (unsigned short)(env[1] & 0xffff);
273 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
274 fxsave->fip = env[3];
275 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
276 fxsave->fcs = (env[4] & 0xffff);
277 fxsave->foo = env[5];
278 fxsave->fos = env[6];
280 to = (struct _fpxreg *) &fxsave->st_space[0];
282 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
283 unsigned long *t = (unsigned long *)to;
284 unsigned long *f = (unsigned long *)from;
286 if (__get_user(*t, f) ||
287 __get_user(*(t + 1), f + 1) ||
288 __get_user(to->exponent, &from->exponent))
295 * Signal frame handlers.
298 static inline int save_i387_fsave( struct _fpstate __user *buf )
300 struct task_struct *tsk = current;
303 tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
304 if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
305 sizeof(struct i387_fsave_struct) ) )
310 static int save_i387_fxsave( struct _fpstate __user *buf )
312 struct task_struct *tsk = current;
317 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
320 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
321 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
325 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
326 sizeof(struct i387_fxsave_struct) ) )
331 int save_i387( struct _fpstate __user *buf )
333 if ( !current->used_math )
336 /* This will cause a "finit" to be triggered by the next
337 * attempted FPU operation by the 'current' process.
339 current->used_math = 0;
342 if ( cpu_has_fxsr ) {
343 return save_i387_fxsave( buf );
345 return save_i387_fsave( buf );
348 return save_i387_soft( ¤t->thread.i387.soft, buf );
352 static inline int restore_i387_fsave( struct _fpstate __user *buf )
354 struct task_struct *tsk = current;
356 return __copy_from_user( &tsk->thread.i387.fsave, buf,
357 sizeof(struct i387_fsave_struct) );
360 static int restore_i387_fxsave( struct _fpstate __user *buf )
363 struct task_struct *tsk = current;
365 err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
366 sizeof(struct i387_fxsave_struct) );
367 /* mxcsr reserved bits must be masked to zero for security reasons */
368 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
369 return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
372 int restore_i387( struct _fpstate __user *buf )
377 if ( cpu_has_fxsr ) {
378 err = restore_i387_fxsave( buf );
380 err = restore_i387_fsave( buf );
383 err = restore_i387_soft( ¤t->thread.i387.soft, buf );
385 current->used_math = 1;
390 * ptrace request handlers.
393 static inline int get_fpregs_fsave( struct user_i387_struct __user *buf,
394 struct task_struct *tsk )
396 return __copy_to_user( buf, &tsk->thread.i387.fsave,
397 sizeof(struct user_i387_struct) );
400 static inline int get_fpregs_fxsave( struct user_i387_struct __user *buf,
401 struct task_struct *tsk )
403 return convert_fxsr_to_user( (struct _fpstate __user *)buf,
404 &tsk->thread.i387.fxsave );
407 int get_fpregs( struct user_i387_struct __user *buf, struct task_struct *tsk )
410 if ( cpu_has_fxsr ) {
411 return get_fpregs_fxsave( buf, tsk );
413 return get_fpregs_fsave( buf, tsk );
416 return save_i387_soft( &tsk->thread.i387.soft,
417 (struct _fpstate __user *)buf );
421 static inline int set_fpregs_fsave( struct task_struct *tsk,
422 struct user_i387_struct __user *buf )
424 return __copy_from_user( &tsk->thread.i387.fsave, buf,
425 sizeof(struct user_i387_struct) );
428 static inline int set_fpregs_fxsave( struct task_struct *tsk,
429 struct user_i387_struct __user *buf )
431 return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
432 (struct _fpstate __user *)buf );
435 int set_fpregs( struct task_struct *tsk, struct user_i387_struct __user *buf )
438 if ( cpu_has_fxsr ) {
439 return set_fpregs_fxsave( tsk, buf );
441 return set_fpregs_fsave( tsk, buf );
444 return restore_i387_soft( &tsk->thread.i387.soft,
445 (struct _fpstate __user *)buf );
449 int get_fpxregs( struct user_fxsr_struct __user *buf, struct task_struct *tsk )
451 if ( cpu_has_fxsr ) {
452 if (__copy_to_user( buf, &tsk->thread.i387.fxsave,
453 sizeof(struct user_fxsr_struct) ))
461 int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct __user *buf )
465 if ( cpu_has_fxsr ) {
466 if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
467 sizeof(struct user_fxsr_struct) ))
469 /* mxcsr reserved bits must be masked to zero for security reasons */
470 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
478 * FPU state for core dumps.
481 static inline void copy_fpu_fsave( struct task_struct *tsk,
482 struct user_i387_struct *fpu )
484 memcpy( fpu, &tsk->thread.i387.fsave,
485 sizeof(struct user_i387_struct) );
488 static inline void copy_fpu_fxsave( struct task_struct *tsk,
489 struct user_i387_struct *fpu )
492 unsigned short *from;
495 memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
497 to = (unsigned short *)&fpu->st_space[0];
498 from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
499 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
500 memcpy( to, from, 5 * sizeof(unsigned short) );
504 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
507 struct task_struct *tsk = current;
509 fpvalid = tsk->used_math;
512 if ( cpu_has_fxsr ) {
513 copy_fpu_fxsave( tsk, fpu );
515 copy_fpu_fsave( tsk, fpu );
522 int dump_extended_fpu( struct pt_regs *regs, struct user_fxsr_struct *fpu )
525 struct task_struct *tsk = current;
527 fpvalid = tsk->used_math && cpu_has_fxsr;
530 memcpy( fpu, &tsk->thread.i387.fxsave,
531 sizeof(struct user_fxsr_struct) );
537 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
539 int fpvalid = tsk->used_math;
545 copy_fpu_fxsave(tsk, fpu);
547 copy_fpu_fsave(tsk, fpu);
552 int dump_task_extended_fpu(struct task_struct *tsk, struct user_fxsr_struct *fpu)
554 int fpvalid = tsk->used_math && cpu_has_fxsr;
559 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));