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 <linux/module.h>
14 #include <asm/processor.h>
16 #include <asm/math_emu.h>
17 #include <asm/sigcontext.h>
19 #include <asm/ptrace.h>
20 #include <asm/uaccess.h>
22 #ifdef CONFIG_MATH_EMULATION
23 #define HAVE_HWFP (boot_cpu_data.hard_math)
28 static unsigned long mxcsr_feature_mask = 0xffffffff;
30 void mxcsr_feature_mask_init(void)
32 unsigned long mask = 0;
35 memset(¤t->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;
40 mxcsr_feature_mask &= mask;
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.
50 void init_fpu(struct task_struct *tsk)
53 memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
54 tsk->thread.i387.fxsave.cwd = 0x37f;
56 tsk->thread.i387.fxsave.mxcsr = 0x1f80;
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;
64 /* only the device not available exception or ptrace can call init_fpu */
65 set_stopped_child_used_math(tsk);
69 * FPU lazy state save handling.
72 void kernel_fpu_begin(void)
74 struct thread_info *thread = current_thread_info();
77 if (thread->status & TS_USEDFPU) {
78 __save_init_fpu(thread->task);
83 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
85 void restore_fpu( struct task_struct *tsk )
88 asm volatile( "fxrstor %0"
89 : : "m" (tsk->thread.i387.fxsave) );
91 asm volatile( "frstor %0"
92 : : "m" (tsk->thread.i387.fsave) );
97 * FPU tag word conversions.
100 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
102 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
104 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
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 */
114 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
116 struct _fpxreg *st = NULL;
117 unsigned long tos = (fxsave->swd >> 11) & 7;
118 unsigned long twd = (unsigned long) fxsave->twd;
120 unsigned long ret = 0xffff0000u;
123 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
125 for ( i = 0 ; i < 8 ; i++ ) {
127 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
129 switch ( st->exponent & 0x7fff ) {
131 tag = 2; /* Special */
134 if ( !st->significand[0] &&
135 !st->significand[1] &&
136 !st->significand[2] &&
137 !st->significand[3] ) {
140 tag = 2; /* Special */
144 if ( st->significand[3] & 0x8000 ) {
147 tag = 2; /* Special */
154 ret |= (tag << (2 * i));
161 * FPU state interaction.
164 unsigned short get_fpu_cwd( struct task_struct *tsk )
166 if ( cpu_has_fxsr ) {
167 return tsk->thread.i387.fxsave.cwd;
169 return (unsigned short)tsk->thread.i387.fsave.cwd;
173 unsigned short get_fpu_swd( struct task_struct *tsk )
175 if ( cpu_has_fxsr ) {
176 return tsk->thread.i387.fxsave.swd;
178 return (unsigned short)tsk->thread.i387.fsave.swd;
183 unsigned short get_fpu_twd( struct task_struct *tsk )
185 if ( cpu_has_fxsr ) {
186 return tsk->thread.i387.fxsave.twd;
188 return (unsigned short)tsk->thread.i387.fsave.twd;
193 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
196 return tsk->thread.i387.fxsave.mxcsr;
204 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
206 if ( cpu_has_fxsr ) {
207 tsk->thread.i387.fxsave.cwd = cwd;
209 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
213 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
215 if ( cpu_has_fxsr ) {
216 tsk->thread.i387.fxsave.swd = swd;
218 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
222 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
224 if ( cpu_has_fxsr ) {
225 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
227 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
234 * FXSR floating point environment conversions.
237 static int convert_fxsr_to_user( struct _fpstate __user *buf,
238 struct i387_fxsave_struct *fxsave )
240 unsigned long env[7];
241 struct _fpreg __user *to;
242 struct _fpxreg *from;
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;
253 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
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;
262 if (__put_user(*f, t) ||
263 __put_user(*(f + 1), t + 1) ||
264 __put_user(from->exponent, &to->exponent))
270 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
271 struct _fpstate __user *buf )
273 unsigned long env[7];
275 struct _fpreg __user *from;
278 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
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];
290 to = (struct _fpxreg *) &fxsave->st_space[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;
296 if (__get_user(*t, f) ||
297 __get_user(*(t + 1), f + 1) ||
298 __get_user(to->exponent, &from->exponent))
305 * Signal frame handlers.
308 static inline int save_i387_fsave( struct _fpstate __user *buf )
310 struct task_struct *tsk = current;
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) ) )
320 static int save_i387_fxsave( struct _fpstate __user *buf )
322 struct task_struct *tsk = current;
327 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
330 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
331 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
335 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
336 sizeof(struct i387_fxsave_struct) ) )
341 int save_i387( struct _fpstate __user *buf )
346 /* This will cause a "finit" to be triggered by the next
347 * attempted FPU operation by the 'current' process.
352 if ( cpu_has_fxsr ) {
353 return save_i387_fxsave( buf );
355 return save_i387_fsave( buf );
358 return save_i387_soft( ¤t->thread.i387.soft, buf );
362 static inline int restore_i387_fsave( struct _fpstate __user *buf )
364 struct task_struct *tsk = current;
366 return __copy_from_user( &tsk->thread.i387.fsave, buf,
367 sizeof(struct i387_fsave_struct) );
370 static int restore_i387_fxsave( struct _fpstate __user *buf )
373 struct task_struct *tsk = current;
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 );
382 int restore_i387( struct _fpstate __user *buf )
387 if ( cpu_has_fxsr ) {
388 err = restore_i387_fxsave( buf );
390 err = restore_i387_fsave( buf );
393 err = restore_i387_soft( ¤t->thread.i387.soft, buf );
400 * ptrace request handlers.
403 static inline int get_fpregs_fsave( struct user_i387_struct __user *buf,
404 struct task_struct *tsk )
406 return __copy_to_user( buf, &tsk->thread.i387.fsave,
407 sizeof(struct user_i387_struct) );
410 static inline int get_fpregs_fxsave( struct user_i387_struct __user *buf,
411 struct task_struct *tsk )
413 return convert_fxsr_to_user( (struct _fpstate __user *)buf,
414 &tsk->thread.i387.fxsave );
417 int get_fpregs( struct user_i387_struct __user *buf, struct task_struct *tsk )
420 if ( cpu_has_fxsr ) {
421 return get_fpregs_fxsave( buf, tsk );
423 return get_fpregs_fsave( buf, tsk );
426 return save_i387_soft( &tsk->thread.i387.soft,
427 (struct _fpstate __user *)buf );
431 static inline int set_fpregs_fsave( struct task_struct *tsk,
432 struct user_i387_struct __user *buf )
434 return __copy_from_user( &tsk->thread.i387.fsave, buf,
435 sizeof(struct user_i387_struct) );
438 static inline int set_fpregs_fxsave( struct task_struct *tsk,
439 struct user_i387_struct __user *buf )
441 return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
442 (struct _fpstate __user *)buf );
445 int set_fpregs( struct task_struct *tsk, struct user_i387_struct __user *buf )
448 if ( cpu_has_fxsr ) {
449 return set_fpregs_fxsave( tsk, buf );
451 return set_fpregs_fsave( tsk, buf );
454 return restore_i387_soft( &tsk->thread.i387.soft,
455 (struct _fpstate __user *)buf );
459 int get_fpxregs( struct user_fxsr_struct __user *buf, struct task_struct *tsk )
461 if ( cpu_has_fxsr ) {
462 if (__copy_to_user( buf, &tsk->thread.i387.fxsave,
463 sizeof(struct user_fxsr_struct) ))
471 int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct __user *buf )
475 if ( cpu_has_fxsr ) {
476 if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
477 sizeof(struct user_fxsr_struct) ))
479 /* mxcsr reserved bits must be masked to zero for security reasons */
480 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
488 * FPU state for core dumps.
491 static inline void copy_fpu_fsave( struct task_struct *tsk,
492 struct user_i387_struct *fpu )
494 memcpy( fpu, &tsk->thread.i387.fsave,
495 sizeof(struct user_i387_struct) );
498 static inline void copy_fpu_fxsave( struct task_struct *tsk,
499 struct user_i387_struct *fpu )
502 unsigned short *from;
505 memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
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) );
514 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
517 struct task_struct *tsk = current;
519 fpvalid = !!used_math();
522 if ( cpu_has_fxsr ) {
523 copy_fpu_fxsave( tsk, fpu );
525 copy_fpu_fsave( tsk, fpu );
531 EXPORT_SYMBOL(dump_fpu);
533 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
535 int fpvalid = !!tsk_used_math(tsk);
541 copy_fpu_fxsave(tsk, fpu);
543 copy_fpu_fsave(tsk, fpu);
548 int dump_task_extended_fpu(struct task_struct *tsk, struct user_fxsr_struct *fpu)
550 int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
555 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));