2 * Copyright (C) 1994 Linus Torvalds
4 * Pentium III FXSR, SSE support
5 * General FPU state handling cleanups
6 * Gareth Hughes <gareth@valinux.com>, May 2000
9 #include <linux/sched.h>
10 #include <linux/module.h>
11 #include <asm/processor.h>
13 #include <asm/math_emu.h>
14 #include <asm/sigcontext.h>
16 #include <asm/ptrace.h>
17 #include <asm/uaccess.h>
19 #ifdef CONFIG_MATH_EMULATION
20 #define HAVE_HWFP (boot_cpu_data.hard_math)
25 static unsigned long mxcsr_feature_mask __read_mostly = 0xffffffff;
27 void mxcsr_feature_mask_init(void)
29 unsigned long mask = 0;
32 memset(¤t->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
33 asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
34 mask = current->thread.i387.fxsave.mxcsr_mask;
35 if (mask == 0) mask = 0x0000ffbf;
37 mxcsr_feature_mask &= mask;
42 * The _current_ task is using the FPU for the first time
43 * so initialize it and set the mxcsr to its default
44 * value at reset if we support XMM instructions and then
45 * remeber the current task has used the FPU.
47 void init_fpu(struct task_struct *tsk)
50 memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
51 tsk->thread.i387.fxsave.cwd = 0x37f;
53 tsk->thread.i387.fxsave.mxcsr = 0x1f80;
55 memset(&tsk->thread.i387.fsave, 0, sizeof(struct i387_fsave_struct));
56 tsk->thread.i387.fsave.cwd = 0xffff037fu;
57 tsk->thread.i387.fsave.swd = 0xffff0000u;
58 tsk->thread.i387.fsave.twd = 0xffffffffu;
59 tsk->thread.i387.fsave.fos = 0xffff0000u;
61 /* only the device not available exception or ptrace can call init_fpu */
62 set_stopped_child_used_math(tsk);
66 * FPU lazy state save handling.
69 void kernel_fpu_begin(void)
71 struct thread_info *thread = current_thread_info();
74 if (thread->status & TS_USEDFPU) {
75 __save_init_fpu(thread->task);
80 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
83 * FPU tag word conversions.
86 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
88 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
90 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
92 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
93 /* and move the valid bits to the lower byte. */
94 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
95 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
96 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
100 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
102 struct _fpxreg *st = NULL;
103 unsigned long tos = (fxsave->swd >> 11) & 7;
104 unsigned long twd = (unsigned long) fxsave->twd;
106 unsigned long ret = 0xffff0000u;
109 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
111 for ( i = 0 ; i < 8 ; i++ ) {
113 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
115 switch ( st->exponent & 0x7fff ) {
117 tag = 2; /* Special */
120 if ( !st->significand[0] &&
121 !st->significand[1] &&
122 !st->significand[2] &&
123 !st->significand[3] ) {
126 tag = 2; /* Special */
130 if ( st->significand[3] & 0x8000 ) {
133 tag = 2; /* Special */
140 ret |= (tag << (2 * i));
147 * FPU state interaction.
150 unsigned short get_fpu_cwd( struct task_struct *tsk )
152 if ( cpu_has_fxsr ) {
153 return tsk->thread.i387.fxsave.cwd;
155 return (unsigned short)tsk->thread.i387.fsave.cwd;
159 unsigned short get_fpu_swd( struct task_struct *tsk )
161 if ( cpu_has_fxsr ) {
162 return tsk->thread.i387.fxsave.swd;
164 return (unsigned short)tsk->thread.i387.fsave.swd;
169 unsigned short get_fpu_twd( struct task_struct *tsk )
171 if ( cpu_has_fxsr ) {
172 return tsk->thread.i387.fxsave.twd;
174 return (unsigned short)tsk->thread.i387.fsave.twd;
179 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
182 return tsk->thread.i387.fxsave.mxcsr;
190 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
192 if ( cpu_has_fxsr ) {
193 tsk->thread.i387.fxsave.cwd = cwd;
195 tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
199 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
201 if ( cpu_has_fxsr ) {
202 tsk->thread.i387.fxsave.swd = swd;
204 tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
208 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
210 if ( cpu_has_fxsr ) {
211 tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
213 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
220 * FXSR floating point environment conversions.
223 static int convert_fxsr_to_user( struct _fpstate __user *buf,
224 struct i387_fxsave_struct *fxsave )
226 unsigned long env[7];
227 struct _fpreg __user *to;
228 struct _fpxreg *from;
231 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
232 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
233 env[2] = twd_fxsr_to_i387(fxsave);
234 env[3] = fxsave->fip;
235 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
236 env[5] = fxsave->foo;
237 env[6] = fxsave->fos;
239 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
243 from = (struct _fpxreg *) &fxsave->st_space[0];
244 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
245 unsigned long __user *t = (unsigned long __user *)to;
246 unsigned long *f = (unsigned long *)from;
248 if (__put_user(*f, t) ||
249 __put_user(*(f + 1), t + 1) ||
250 __put_user(from->exponent, &to->exponent))
256 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
257 struct _fpstate __user *buf )
259 unsigned long env[7];
261 struct _fpreg __user *from;
264 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
267 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
268 fxsave->swd = (unsigned short)(env[1] & 0xffff);
269 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
270 fxsave->fip = env[3];
271 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
272 fxsave->fcs = (env[4] & 0xffff);
273 fxsave->foo = env[5];
274 fxsave->fos = env[6];
276 to = (struct _fpxreg *) &fxsave->st_space[0];
278 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
279 unsigned long *t = (unsigned long *)to;
280 unsigned long __user *f = (unsigned long __user *)from;
282 if (__get_user(*t, f) ||
283 __get_user(*(t + 1), f + 1) ||
284 __get_user(to->exponent, &from->exponent))
291 * Signal frame handlers.
294 static inline int save_i387_fsave( struct _fpstate __user *buf )
296 struct task_struct *tsk = current;
299 tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
300 if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
301 sizeof(struct i387_fsave_struct) ) )
306 static int save_i387_fxsave( struct _fpstate __user *buf )
308 struct task_struct *tsk = current;
313 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
316 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
317 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
321 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
322 sizeof(struct i387_fxsave_struct) ) )
327 int save_i387( struct _fpstate __user *buf )
332 /* This will cause a "finit" to be triggered by the next
333 * attempted FPU operation by the 'current' process.
338 if ( cpu_has_fxsr ) {
339 return save_i387_fxsave( buf );
341 return save_i387_fsave( buf );
344 return save_i387_soft( ¤t->thread.i387.soft, buf );
348 static inline int restore_i387_fsave( struct _fpstate __user *buf )
350 struct task_struct *tsk = current;
352 return __copy_from_user( &tsk->thread.i387.fsave, buf,
353 sizeof(struct i387_fsave_struct) );
356 static int restore_i387_fxsave( struct _fpstate __user *buf )
359 struct task_struct *tsk = current;
361 err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
362 sizeof(struct i387_fxsave_struct) );
363 /* mxcsr reserved bits must be masked to zero for security reasons */
364 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
365 return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
368 int restore_i387( struct _fpstate __user *buf )
373 if ( cpu_has_fxsr ) {
374 err = restore_i387_fxsave( buf );
376 err = restore_i387_fsave( buf );
379 err = restore_i387_soft( ¤t->thread.i387.soft, buf );
386 * ptrace request handlers.
389 static inline int get_fpregs_fsave( struct user_i387_struct __user *buf,
390 struct task_struct *tsk )
392 return __copy_to_user( buf, &tsk->thread.i387.fsave,
393 sizeof(struct user_i387_struct) );
396 static inline int get_fpregs_fxsave( struct user_i387_struct __user *buf,
397 struct task_struct *tsk )
399 return convert_fxsr_to_user( (struct _fpstate __user *)buf,
400 &tsk->thread.i387.fxsave );
403 int get_fpregs( struct user_i387_struct __user *buf, struct task_struct *tsk )
406 if ( cpu_has_fxsr ) {
407 return get_fpregs_fxsave( buf, tsk );
409 return get_fpregs_fsave( buf, tsk );
412 return save_i387_soft( &tsk->thread.i387.soft,
413 (struct _fpstate __user *)buf );
417 static inline int set_fpregs_fsave( struct task_struct *tsk,
418 struct user_i387_struct __user *buf )
420 return __copy_from_user( &tsk->thread.i387.fsave, buf,
421 sizeof(struct user_i387_struct) );
424 static inline int set_fpregs_fxsave( struct task_struct *tsk,
425 struct user_i387_struct __user *buf )
427 return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
428 (struct _fpstate __user *)buf );
431 int set_fpregs( struct task_struct *tsk, struct user_i387_struct __user *buf )
434 if ( cpu_has_fxsr ) {
435 return set_fpregs_fxsave( tsk, buf );
437 return set_fpregs_fsave( tsk, buf );
440 return restore_i387_soft( &tsk->thread.i387.soft,
441 (struct _fpstate __user *)buf );
445 int get_fpxregs( struct user_fxsr_struct __user *buf, struct task_struct *tsk )
447 if ( cpu_has_fxsr ) {
448 if (__copy_to_user( buf, &tsk->thread.i387.fxsave,
449 sizeof(struct user_fxsr_struct) ))
457 int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct __user *buf )
461 if ( cpu_has_fxsr ) {
462 if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
463 sizeof(struct user_fxsr_struct) ))
465 /* mxcsr reserved bits must be masked to zero for security reasons */
466 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
474 * FPU state for core dumps.
477 static inline void copy_fpu_fsave( struct task_struct *tsk,
478 struct user_i387_struct *fpu )
480 memcpy( fpu, &tsk->thread.i387.fsave,
481 sizeof(struct user_i387_struct) );
484 static inline void copy_fpu_fxsave( struct task_struct *tsk,
485 struct user_i387_struct *fpu )
488 unsigned short *from;
491 memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
493 to = (unsigned short *)&fpu->st_space[0];
494 from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
495 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
496 memcpy( to, from, 5 * sizeof(unsigned short) );
500 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
503 struct task_struct *tsk = current;
505 fpvalid = !!used_math();
508 if ( cpu_has_fxsr ) {
509 copy_fpu_fxsave( tsk, fpu );
511 copy_fpu_fsave( tsk, fpu );
517 EXPORT_SYMBOL(dump_fpu);
519 int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
521 int fpvalid = !!tsk_used_math(tsk);
527 copy_fpu_fxsave(tsk, fpu);
529 copy_fpu_fsave(tsk, fpu);
534 int dump_task_extended_fpu(struct task_struct *tsk, struct user_fxsr_struct *fpu)
536 int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;
541 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));