2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * FXSAVE<->i387 conversion support. Based on code by Gareth Hughes.
4 * This is used for ptrace, signals and coredumps in 32bit emulation.
7 #include <linux/sched.h>
8 #include <asm/sigcontext32.h>
9 #include <asm/processor.h>
10 #include <asm/uaccess.h>
13 static inline unsigned short twd_i387_to_fxsr(unsigned short twd
)
15 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
17 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
19 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
20 /* and move the valid bits to the lower byte. */
21 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
22 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
23 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
27 static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct
*fxsave
)
29 struct _fpxreg
*st
= NULL
;
30 unsigned long tos
= (fxsave
->swd
>> 11) & 7;
31 unsigned long twd
= (unsigned long) fxsave
->twd
;
33 unsigned long ret
= 0xffff0000;
36 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
38 for (i
= 0 ; i
< 8 ; i
++) {
40 st
= FPREG_ADDR( fxsave
, (i
- tos
) & 7 );
42 switch (st
->exponent
& 0x7fff) {
44 tag
= 2; /* Special */
47 if ( !st
->significand
[0] &&
48 !st
->significand
[1] &&
49 !st
->significand
[2] &&
50 !st
->significand
[3] ) {
53 tag
= 2; /* Special */
57 if (st
->significand
[3] & 0x8000) {
60 tag
= 2; /* Special */
67 ret
|= (tag
<< (2 * i
));
74 static inline int convert_fxsr_from_user(struct i387_fxsave_struct
*fxsave
,
75 struct _fpstate_ia32 __user
*buf
)
78 struct _fpreg __user
*from
;
83 #define G(num,val) err |= __get_user(val, num + (u32 __user *)buf)
87 fxsave
->twd
= twd_i387_to_fxsr(fxsave
->twd
);
90 fxsave
->fop
= v
>>16; /* cs ignored */
97 to
= (struct _fpxreg
*)&fxsave
->st_space
[0];
99 for (i
= 0 ; i
< 8 ; i
++, to
++, from
++) {
100 if (__copy_from_user(to
, from
, sizeof(*from
)))
107 static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user
*buf
,
108 struct i387_fxsave_struct
*fxsave
,
109 struct pt_regs
*regs
,
110 struct task_struct
*tsk
)
112 struct _fpreg __user
*to
;
113 struct _fpxreg
*from
;
118 if (tsk
== current
) {
119 /* should be actually ds/cs at fpu exception time,
120 but that information is not available in 64bit mode. */
121 asm("movw %%ds,%0 " : "=r" (ds
));
122 asm("movw %%cs,%0 " : "=r" (cs
));
123 } else { /* ptrace. task has stopped. */
128 #define P(num,val) err |= __put_user(val, num + (u32 __user *)buf)
129 P(0, (u32
)fxsave
->cwd
| 0xffff0000);
130 P(1, (u32
)fxsave
->swd
| 0xffff0000);
131 P(2, twd_fxsr_to_i387(fxsave
));
132 P(3, (u32
)fxsave
->rip
);
133 P(4, cs
| ((u32
)fxsave
->fop
) << 16);
135 P(6, 0xffff0000 | ds
);
142 from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
143 for ( i
= 0 ; i
< 8 ; i
++, to
++, from
++ ) {
144 if (__copy_to_user(to
, from
, sizeof(*to
)))
150 int restore_i387_ia32(struct task_struct
*tsk
, struct _fpstate_ia32 __user
*buf
, int fsave
)
154 if (__copy_from_user(&tsk
->thread
.i387
.fxsave
,
156 sizeof(struct i387_fxsave_struct
)))
158 tsk
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
159 set_stopped_child_used_math(tsk
);
161 return convert_fxsr_from_user(&tsk
->thread
.i387
.fxsave
, buf
);
164 int save_i387_ia32(struct task_struct
*tsk
,
165 struct _fpstate_ia32 __user
*buf
,
166 struct pt_regs
*regs
,
172 if (convert_fxsr_to_user(buf
, &tsk
->thread
.i387
.fxsave
, regs
, tsk
))
176 err
|= __put_user(tsk
->thread
.i387
.fxsave
.swd
, &buf
->status
);
179 err
|= __put_user(X86_FXSR_MAGIC
, &buf
->magic
);
180 err
|= __copy_to_user(&buf
->_fxsr_env
[0], &tsk
->thread
.i387
.fxsave
,
181 sizeof(struct i387_fxsave_struct
));