Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / x86_64 / 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 #include <asm/user32.h>
13
14 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
15 {
16         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
17  
18         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
19         tmp = ~twd;
20         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
21         /* and move the valid bits to the lower byte. */
22         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
23         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
24         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
25         return tmp;
26 }
27
28 static inline unsigned long
29 twd_fxsr_to_i387(const struct i387_fxsave_struct *fxsave)
30 {
31         struct _fpxreg *st = NULL;
32         unsigned long tos = (fxsave->swd >> 11) & 7;
33         unsigned long twd = (unsigned long) fxsave->twd;
34         unsigned long tag;
35         unsigned long ret = 0xffff0000;
36         int i;
37
38 #define FPREG_ADDR(f, n)        ((void *)&(f)->st_space + (n) * 16);
39
40         for (i = 0 ; i < 8 ; i++) {
41                 if (twd & 0x1) {
42                         st = FPREG_ADDR( fxsave, (i - tos) & 7 );
43
44                         switch (st->exponent & 0x7fff) {
45                         case 0x7fff:
46                                 tag = 2;                /* Special */
47                                 break;
48                         case 0x0000:
49                                 if ( !st->significand[0] &&
50                                      !st->significand[1] &&
51                                      !st->significand[2] &&
52                                      !st->significand[3] ) {
53                                         tag = 1;        /* Zero */
54                                 } else {
55                                         tag = 2;        /* Special */
56                                 }
57                                 break;
58                         default:
59                                 if (st->significand[3] & 0x8000) {
60                                         tag = 0;        /* Valid */
61                                 } else {
62                                         tag = 2;        /* Special */
63                                 }
64                                 break;
65                         }
66                 } else {
67                         tag = 3;                        /* Empty */
68                 }
69                 ret |= (tag << (2 * i));
70                 twd = twd >> 1;
71         }
72         return ret;
73 }
74
75
76 static inline void
77 convert_fxsr_env_from_i387(struct i387_fxsave_struct *fxsave, const u32 env[7])
78 {
79         u32 v;
80 #define G(num,val) val = env[num]
81         G(0, fxsave->cwd);
82         G(1, fxsave->swd);
83         G(2, fxsave->twd);
84         fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
85         G(3, fxsave->rip);
86         G(4, v);
87         fxsave->fop = v>>16;    /* cs ignored */
88         G(5, fxsave->rdp);
89         /* 6: ds ignored */
90 #undef G
91 }
92
93 static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
94                                          struct _fpstate_ia32 __user *buf)
95 {
96         u32 env[7];
97         struct _fpxreg *to;
98         struct _fpreg __user *from;
99         int i;
100
101         if (__copy_from_user(env, buf, sizeof(env)))
102                 return -1; 
103
104         convert_fxsr_env_from_i387(fxsave, env);
105
106         to = (struct _fpxreg *)&fxsave->st_space[0];
107         from = &buf->_st[0];
108         for (i = 0 ; i < 8 ; i++, to++, from++) {
109                 if (__copy_from_user(to, from, sizeof(*from)))
110                         return -1;
111         }
112         return 0;
113 }
114
115
116 static inline void
117 convert_fxsr_env_to_i387(struct task_struct *tsk, struct pt_regs *regs,
118                          u32 env[7], const struct i387_fxsave_struct *fxsave)
119 {
120         u16 cs,ds; 
121
122         if (tsk == current) {
123                 /* should be actually ds/cs at fpu exception time,
124                    but that information is not available in 64bit mode. */
125                 asm("movw %%ds,%0 " : "=r" (ds)); 
126                 asm("movw %%cs,%0 " : "=r" (cs));               
127         } else { /* ptrace. task has stopped. */
128                 ds = tsk->thread.ds;
129                 cs = regs->cs;
130         } 
131
132 #define P(num,val) env[num] = val
133         P(0, (u32)fxsave->cwd | 0xffff0000);
134         P(1, (u32)fxsave->swd | 0xffff0000);
135         P(2, twd_fxsr_to_i387(fxsave));
136         P(3, (u32)fxsave->rip);
137         P(4,  cs | ((u32)fxsave->fop) << 16); 
138         P(5, fxsave->rdp);
139         P(6, 0xffff0000 | ds);
140 #undef P
141 }
142
143
144 static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user *buf,
145                                        struct i387_fxsave_struct *fxsave,
146                                        struct pt_regs *regs,
147                                        struct task_struct *tsk)
148 {
149         struct _fpreg __user *to;
150         struct _fpxreg *from;
151         int i;
152         u32 env[7];
153
154         convert_fxsr_env_to_i387(tsk, regs, env, fxsave);
155         if (__copy_to_user(buf, env, sizeof(env)))
156                 return -1; 
157
158         to = &buf->_st[0];
159         from = (struct _fpxreg *) &fxsave->st_space[0];
160         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
161                 if (__copy_to_user(to, from, sizeof(*to)))
162                         return -1;
163         }
164         return 0;
165 }
166
167 int restore_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf, int fsave) 
168
169         clear_fpu(tsk);
170         if (!fsave) { 
171                 if (__copy_from_user(&tsk->thread.i387.fxsave, 
172                                      &buf->_fxsr_env[0],
173                                      sizeof(struct i387_fxsave_struct)))
174                         return -1;
175                 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
176                 set_stopped_child_used_math(tsk);
177         } 
178         return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
179 }  
180
181 int save_i387_ia32(struct task_struct *tsk, 
182                    struct _fpstate_ia32 __user *buf, 
183                    struct pt_regs *regs,
184                    int fsave)
185 {
186         int err = 0;
187
188         init_fpu(tsk);
189         if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
190                 return -1;
191         if (fsave)
192                 return 0;
193         err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
194         if (fsave) 
195                 return err ? -1 : 1;    
196         err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
197         err |= __copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
198                               sizeof(struct i387_fxsave_struct));
199         return err ? -1 : 1;
200 }
201
202 int get_fpregs32(struct user_i387_ia32_struct *buf, struct task_struct *tsk)
203 {
204         struct pt_regs *regs = ((struct pt_regs *)tsk->thread.rsp0) - 1;
205         struct _fpreg *to;
206         const struct _fpxreg *from;
207         unsigned int i;
208
209         convert_fxsr_env_to_i387(tsk, regs,
210                                  (u32 *) buf, &tsk->thread.i387.fxsave);
211
212         to = (struct _fpreg *) buf->st_space;
213         from = (const struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
214         for (i = 0; i < 8; i++, to++, from++)
215                 *to = *(const struct _fpreg *) from;
216
217         return 0;
218 }
219
220 int
221 set_fpregs32(struct task_struct *tsk, const struct user_i387_ia32_struct *buf)
222 {
223         struct _fpxreg *to;
224         const struct _fpreg *from;
225         unsigned int i;
226
227         convert_fxsr_env_from_i387(&tsk->thread.i387.fxsave, (u32 *) buf);
228
229         to = (struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
230         from = (const struct _fpreg *) buf->st_space;
231         for (i = 0; i < 8; i++, to++, from++)
232                 *(struct _fpreg *) to = *from;
233
234         return 0;
235 }