x86: x86 i387 user_regset
[sfrench/cifs-2.6.git] / arch / x86 / ia32 / fpu32.c
1 /*
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.
5  */
6
7 #include <linux/sched.h>
8 #include <asm/sigcontext32.h>
9 #include <asm/processor.h>
10 #include <asm/uaccess.h>
11 #include <asm/i387.h>
12
13 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
14 {
15         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
16
17         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
18         tmp = ~twd;
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 */
24         return tmp;
25 }
26
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
32
33 static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
34 {
35         struct _fpxreg *st;
36         unsigned long tos = (fxsave->swd >> 11) & 7;
37         unsigned long twd = (unsigned long) fxsave->twd;
38         unsigned long tag;
39         unsigned long ret = 0xffff0000;
40         int i;
41
42         for (i = 0; i < 8; i++, twd >>= 1) {
43                 if (twd & 0x1) {
44                         st = FPREG_ADDR(fxsave, (i - tos) & 7);
45
46                         switch (st->exponent & 0x7fff) {
47                         case 0x7fff:
48                                 tag = FP_EXP_TAG_SPECIAL;
49                                 break;
50                         case 0x0000:
51                                 if (!st->significand[0] &&
52                                     !st->significand[1] &&
53                                     !st->significand[2] &&
54                                     !st->significand[3])
55                                         tag = FP_EXP_TAG_ZERO;
56                                 else
57                                         tag = FP_EXP_TAG_SPECIAL;
58                                 break;
59                         default:
60                                 if (st->significand[3] & 0x8000)
61                                         tag = FP_EXP_TAG_VALID;
62                                 else
63                                         tag = FP_EXP_TAG_SPECIAL;
64                                 break;
65                         }
66                 } else {
67                         tag = FP_EXP_TAG_EMPTY;
68                 }
69                 ret |= tag << (2 * i);
70         }
71         return ret;
72 }
73
74 #define G(num, val) err |= __get_user(val, num + (u32 __user *)buf)
75
76 static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
77                                          struct _fpstate_ia32 __user *buf)
78 {
79         struct _fpxreg *to;
80         struct _fpreg __user *from;
81         int i, err = 0;
82         u32 v;
83
84         G(0, fxsave->cwd);
85         G(1, fxsave->swd);
86         G(2, fxsave->twd);
87         fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
88         G(3, fxsave->rip);
89         G(4, v);
90         /* cs ignored */
91         fxsave->fop = v>>16;
92         G(5, fxsave->rdp);
93         /* 6: ds ignored */
94         if (err)
95                 return -1;
96
97         to = (struct _fpxreg *)&fxsave->st_space[0];
98         from = &buf->_st[0];
99         for (i = 0; i < 8; i++, to++, from++) {
100                 if (__copy_from_user(to, from, sizeof(*from)))
101                         return -1;
102         }
103         return 0;
104 }
105
106 #define P(num, val) err |= __put_user(val, num + (u32 __user *)buf)
107
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)
112 {
113         struct _fpreg __user *to;
114         struct _fpxreg *from;
115         int i, err = 0;
116         u16 cs, ds;
117
118         if (tsk == current) {
119                 /*
120                  * should be actually ds/cs at fpu exception time, but
121                  * that information is not available in 64bit mode.
122                  */
123                 asm("movw %%ds,%0 " : "=r" (ds));
124                 asm("movw %%cs,%0 " : "=r" (cs));
125         } else {
126                  /* ptrace. task has stopped. */
127                 ds = tsk->thread.ds;
128                 cs = regs->cs;
129         }
130
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);
136         P(5, fxsave->rdp);
137         P(6, 0xffff0000 | ds);
138
139         if (err)
140                 return -1;
141
142         to = &buf->_st[0];
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)))
146                         return -1;
147         }
148         return 0;
149 }
150
151 int restore_i387_ia32(struct task_struct *tsk,
152                       struct _fpstate_ia32 __user *buf, int fsave)
153 {
154         clear_fpu(tsk);
155         if (!fsave) {
156                 if (__copy_from_user(&tsk->thread.i387.fxsave,
157                                      &buf->_fxsr_env[0],
158                                      sizeof(struct i387_fxsave_struct)))
159                         return -1;
160                 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
161                 set_stopped_child_used_math(tsk);
162         }
163         return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
164 }
165
166 int save_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf,
167                    struct pt_regs *regs, int fsave)
168 {
169         int err = 0;
170
171         init_fpu(tsk);
172         if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
173                 return -1;
174         if (fsave)
175                 return 0;
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));
180         return err ? -1 : 1;
181 }