ae80745f668fc37d95baa7e275bdd35433f4e6fc
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 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
28 #define FP_EXP_TAG_VALID 0
29 #define FP_EXP_TAG_ZERO 1
30 #define FP_EXP_TAG_SPECIAL 2
31 #define FP_EXP_TAG_EMPTY 3
33 static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct
*fxsave
)
36 unsigned long tos
= (fxsave
->swd
>> 11) & 7;
37 unsigned long twd
= (unsigned long) fxsave
->twd
;
39 unsigned long ret
= 0xffff0000;
42 for (i
= 0; i
< 8; i
++, twd
>>= 1) {
44 st
= FPREG_ADDR(fxsave
, (i
- tos
) & 7);
46 switch (st
->exponent
& 0x7fff) {
48 tag
= FP_EXP_TAG_SPECIAL
;
51 if (!st
->significand
[0] &&
52 !st
->significand
[1] &&
53 !st
->significand
[2] &&
55 tag
= FP_EXP_TAG_ZERO
;
57 tag
= FP_EXP_TAG_SPECIAL
;
60 if (st
->significand
[3] & 0x8000)
61 tag
= FP_EXP_TAG_VALID
;
63 tag
= FP_EXP_TAG_SPECIAL
;
67 tag
= FP_EXP_TAG_EMPTY
;
69 ret
|= tag
<< (2 * i
);
74 #define G(num, val) err |= __get_user(val, num + (u32 __user *)buf)
76 static inline int convert_fxsr_from_user(struct i387_fxsave_struct
*fxsave
,
77 struct _fpstate_ia32 __user
*buf
)
80 struct _fpreg __user
*from
;
87 fxsave
->twd
= twd_i387_to_fxsr(fxsave
->twd
);
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
)))
106 #define P(num, val) err |= __put_user(val, num + (u32 __user *)buf)
108 static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user
*buf
,
109 struct i387_fxsave_struct
*fxsave
,
110 struct pt_regs
*regs
,
111 struct task_struct
*tsk
)
113 struct _fpreg __user
*to
;
114 struct _fpxreg
*from
;
118 if (tsk
== current
) {
120 * should be actually ds/cs at fpu exception time, but
121 * that information is not available in 64bit mode.
123 asm("movw %%ds,%0 " : "=r" (ds
));
124 asm("movw %%cs,%0 " : "=r" (cs
));
126 /* ptrace. task has stopped. */
131 P(0, (u32
)fxsave
->cwd
| 0xffff0000);
132 P(1, (u32
)fxsave
->swd
| 0xffff0000);
133 P(2, twd_fxsr_to_i387(fxsave
));
134 P(3, (u32
)fxsave
->rip
);
135 P(4, cs
| ((u32
)fxsave
->fop
) << 16);
137 P(6, 0xffff0000 | ds
);
143 from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
144 for (i
= 0; i
< 8; i
++, to
++, from
++) {
145 if (__copy_to_user(to
, from
, sizeof(*to
)))
151 int restore_i387_ia32(struct task_struct
*tsk
,
152 struct _fpstate_ia32 __user
*buf
, int fsave
)
156 if (__copy_from_user(&tsk
->thread
.i387
.fxsave
,
158 sizeof(struct i387_fxsave_struct
)))
160 tsk
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
161 set_stopped_child_used_math(tsk
);
163 return convert_fxsr_from_user(&tsk
->thread
.i387
.fxsave
, buf
);
166 int save_i387_ia32(struct task_struct
*tsk
, struct _fpstate_ia32 __user
*buf
,
167 struct pt_regs
*regs
, int fsave
)
172 if (convert_fxsr_to_user(buf
, &tsk
->thread
.i387
.fxsave
, regs
, tsk
))
176 err
|= __put_user(tsk
->thread
.i387
.fxsave
.swd
, &buf
->status
);
177 err
|= __put_user(X86_FXSR_MAGIC
, &buf
->magic
);
178 err
|= __copy_to_user(&buf
->_fxsr_env
[0], &tsk
->thread
.i387
.fxsave
,
179 sizeof(struct i387_fxsave_struct
));
This page took 0.066643 seconds and 4 git commands to generate.