[PATCH] Remove i386_ksyms.c, almost.
[linux-2.6/suspend2-2.6.18.git] / arch / i386 / kernel / i387.c
blobb817168d9c6210e4cfb55140b3543449c46dcfab
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 <linux/module.h>
14 #include <asm/processor.h>
15 #include <asm/i387.h>
16 #include <asm/math_emu.h>
17 #include <asm/sigcontext.h>
18 #include <asm/user.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)
24 #else
25 #define HAVE_HWFP 1
26 #endif
28 static unsigned long mxcsr_feature_mask = 0xffffffff;
30 void mxcsr_feature_mask_init(void)
32 unsigned long mask = 0;
33 clts();
34 if (cpu_has_fxsr) {
35 memset(&current->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;
41 stts();
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)
52 if (cpu_has_fxsr) {
53 memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
54 tsk->thread.i387.fxsave.cwd = 0x37f;
55 if (cpu_has_xmm)
56 tsk->thread.i387.fxsave.mxcsr = 0x1f80;
57 } else {
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();
76 preempt_disable();
77 if (thread->status & TS_USEDFPU) {
78 __save_init_fpu(thread->task);
79 return;
81 clts();
83 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
85 void restore_fpu( struct task_struct *tsk )
87 if ( cpu_has_fxsr ) {
88 asm volatile( "fxrstor %0"
89 : : "m" (tsk->thread.i387.fxsave) );
90 } else {
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) */
105 tmp = ~twd;
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 */
111 return tmp;
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;
119 unsigned long tag;
120 unsigned long ret = 0xffff0000u;
121 int i;
123 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
125 for ( i = 0 ; i < 8 ; i++ ) {
126 if ( twd & 0x1 ) {
127 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
129 switch ( st->exponent & 0x7fff ) {
130 case 0x7fff:
131 tag = 2; /* Special */
132 break;
133 case 0x0000:
134 if ( !st->significand[0] &&
135 !st->significand[1] &&
136 !st->significand[2] &&
137 !st->significand[3] ) {
138 tag = 1; /* Zero */
139 } else {
140 tag = 2; /* Special */
142 break;
143 default:
144 if ( st->significand[3] & 0x8000 ) {
145 tag = 0; /* Valid */
146 } else {
147 tag = 2; /* Special */
149 break;
151 } else {
152 tag = 3; /* Empty */
154 ret |= (tag << (2 * i));
155 twd = twd >> 1;
157 return ret;
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;
168 } else {
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;
177 } else {
178 return (unsigned short)tsk->thread.i387.fsave.swd;
182 #if 0
183 unsigned short get_fpu_twd( struct task_struct *tsk )
185 if ( cpu_has_fxsr ) {
186 return tsk->thread.i387.fxsave.twd;
187 } else {
188 return (unsigned short)tsk->thread.i387.fsave.twd;
191 #endif /* 0 */
193 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
195 if ( cpu_has_xmm ) {
196 return tsk->thread.i387.fxsave.mxcsr;
197 } else {
198 return 0x1f80;
202 #if 0
204 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
206 if ( cpu_has_fxsr ) {
207 tsk->thread.i387.fxsave.cwd = cwd;
208 } else {
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;
217 } else {
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);
226 } else {
227 tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
231 #endif /* 0 */
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;
243 int i;
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) ) )
254 return 1;
256 to = &buf->_st[0];
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))
265 return 1;
267 return 0;
270 static int convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
271 struct _fpstate __user *buf )
273 unsigned long env[7];
274 struct _fpxreg *to;
275 struct _fpreg __user *from;
276 int i;
278 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
279 return 1;
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];
291 from = &buf->_st[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))
299 return 1;
301 return 0;
305 * Signal frame handlers.
308 static inline int save_i387_fsave( struct _fpstate __user *buf )
310 struct task_struct *tsk = current;
312 unlazy_fpu( tsk );
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) ) )
316 return -1;
317 return 1;
320 static int save_i387_fxsave( struct _fpstate __user *buf )
322 struct task_struct *tsk = current;
323 int err = 0;
325 unlazy_fpu( tsk );
327 if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
328 return -1;
330 err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
331 err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
332 if ( err )
333 return -1;
335 if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
336 sizeof(struct i387_fxsave_struct) ) )
337 return -1;
338 return 1;
341 int save_i387( struct _fpstate __user *buf )
343 if ( !used_math() )
344 return 0;
346 /* This will cause a "finit" to be triggered by the next
347 * attempted FPU operation by the 'current' process.
349 clear_used_math();
351 if ( HAVE_HWFP ) {
352 if ( cpu_has_fxsr ) {
353 return save_i387_fxsave( buf );
354 } else {
355 return save_i387_fsave( buf );
357 } else {
358 return save_i387_soft( &current->thread.i387.soft, buf );
362 static inline int restore_i387_fsave( struct _fpstate __user *buf )
364 struct task_struct *tsk = current;
365 clear_fpu( tsk );
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 )
372 int err;
373 struct task_struct *tsk = current;
374 clear_fpu( tsk );
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 )
384 int err;
386 if ( HAVE_HWFP ) {
387 if ( cpu_has_fxsr ) {
388 err = restore_i387_fxsave( buf );
389 } else {
390 err = restore_i387_fsave( buf );
392 } else {
393 err = restore_i387_soft( &current->thread.i387.soft, buf );
395 set_used_math();
396 return err;
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 )
419 if ( HAVE_HWFP ) {
420 if ( cpu_has_fxsr ) {
421 return get_fpregs_fxsave( buf, tsk );
422 } else {
423 return get_fpregs_fsave( buf, tsk );
425 } else {
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 )
447 if ( HAVE_HWFP ) {
448 if ( cpu_has_fxsr ) {
449 return set_fpregs_fxsave( tsk, buf );
450 } else {
451 return set_fpregs_fsave( tsk, buf );
453 } else {
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) ))
464 return -EFAULT;
465 return 0;
466 } else {
467 return -EIO;
471 int set_fpxregs( struct task_struct *tsk, struct user_fxsr_struct __user *buf )
473 int ret = 0;
475 if ( cpu_has_fxsr ) {
476 if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
477 sizeof(struct user_fxsr_struct) ))
478 ret = -EFAULT;
479 /* mxcsr reserved bits must be masked to zero for security reasons */
480 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
481 } else {
482 ret = -EIO;
484 return ret;
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 )
501 unsigned short *to;
502 unsigned short *from;
503 int i;
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 )
516 int fpvalid;
517 struct task_struct *tsk = current;
519 fpvalid = !!used_math();
520 if ( fpvalid ) {
521 unlazy_fpu( tsk );
522 if ( cpu_has_fxsr ) {
523 copy_fpu_fxsave( tsk, fpu );
524 } else {
525 copy_fpu_fsave( tsk, fpu );
529 return fpvalid;
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);
537 if (fpvalid) {
538 if (tsk == current)
539 unlazy_fpu(tsk);
540 if (cpu_has_fxsr)
541 copy_fpu_fxsave(tsk, fpu);
542 else
543 copy_fpu_fsave(tsk, fpu);
545 return fpvalid;
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;
552 if (fpvalid) {
553 if (tsk == current)
554 unlazy_fpu(tsk);
555 memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
557 return fpvalid;