Linux-2.6.12-rc2
[cascardo/linux.git] / arch / um / sys-i386 / ptrace.c
1 /* 
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <linux/config.h>
7 #include <linux/compiler.h>
8 #include "linux/sched.h"
9 #include "asm/elf.h"
10 #include "asm/ptrace.h"
11 #include "asm/uaccess.h"
12 #include "asm/unistd.h"
13 #include "sysdep/ptrace.h"
14 #include "sysdep/sigcontext.h"
15 #include "sysdep/sc.h"
16
17 void arch_switch(void)
18 {
19         update_debugregs(current->thread.arch.debugregs_seq);
20 }
21
22 int is_syscall(unsigned long addr)
23 {
24         unsigned short instr;
25         int n;
26
27         n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
28         if(n){
29                 printk("is_syscall : failed to read instruction from 0x%lx\n",
30                        addr);
31                 return(0);
32         }
33         /* int 0x80 or sysenter */
34         return((instr == 0x80cd) || (instr == 0x340f));
35 }
36
37 /* determines which flags the user has access to. */
38 /* 1 = access 0 = no access */
39 #define FLAG_MASK 0x00044dd5
40
41 int putreg(struct task_struct *child, int regno, unsigned long value)
42 {
43         regno >>= 2;
44         switch (regno) {
45         case FS:
46                 if (value && (value & 3) != 3)
47                         return -EIO;
48                 PT_REGS_FS(&child->thread.regs) = value;
49                 return 0;
50         case GS:
51                 if (value && (value & 3) != 3)
52                         return -EIO;
53                 PT_REGS_GS(&child->thread.regs) = value;
54                 return 0;
55         case DS:
56         case ES:
57                 if (value && (value & 3) != 3)
58                         return -EIO;
59                 value &= 0xffff;
60                 break;
61         case SS:
62         case CS:
63                 if ((value & 3) != 3)
64                         return -EIO;
65                 value &= 0xffff;
66                 break;
67         case EFL:
68                 value &= FLAG_MASK;
69                 value |= PT_REGS_EFLAGS(&child->thread.regs);
70                 break;
71         }
72         PT_REGS_SET(&child->thread.regs, regno, value);
73         return 0;
74 }
75
76 unsigned long getreg(struct task_struct *child, int regno)
77 {
78         unsigned long retval = ~0UL;
79
80         regno >>= 2;
81         switch (regno) {
82         case FS:
83         case GS:
84         case DS:
85         case ES:
86         case SS:
87         case CS:
88                 retval = 0xffff;
89                 /* fall through */
90         default:
91                 retval &= PT_REG(&child->thread.regs, regno);
92         }
93         return retval;
94 }
95
96 struct i387_fxsave_struct {
97         unsigned short  cwd;
98         unsigned short  swd;
99         unsigned short  twd;
100         unsigned short  fop;
101         long    fip;
102         long    fcs;
103         long    foo;
104         long    fos;
105         long    mxcsr;
106         long    reserved;
107         long    st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
108         long    xmm_space[32];  /* 8*16 bytes for each XMM-reg = 128 bytes */
109         long    padding[56];
110 };
111
112 /*
113  * FPU tag word conversions.
114  */
115
116 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
117 {
118         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
119  
120         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
121         tmp = ~twd;
122         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
123         /* and move the valid bits to the lower byte. */
124         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
125         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
126         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
127         return tmp;
128 }
129
130 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
131 {
132         struct _fpxreg *st = NULL;
133         unsigned long twd = (unsigned long) fxsave->twd;
134         unsigned long tag;
135         unsigned long ret = 0xffff0000;
136         int i;
137
138 #define FPREG_ADDR(f, n)        ((char *)&(f)->st_space + (n) * 16);
139
140         for ( i = 0 ; i < 8 ; i++ ) {
141                 if ( twd & 0x1 ) {
142                         st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
143
144                         switch ( st->exponent & 0x7fff ) {
145                         case 0x7fff:
146                                 tag = 2;                /* Special */
147                                 break;
148                         case 0x0000:
149                                 if ( !st->significand[0] &&
150                                      !st->significand[1] &&
151                                      !st->significand[2] &&
152                                      !st->significand[3] ) {
153                                         tag = 1;        /* Zero */
154                                 } else {
155                                         tag = 2;        /* Special */
156                                 }
157                                 break;
158                         default:
159                                 if ( st->significand[3] & 0x8000 ) {
160                                         tag = 0;        /* Valid */
161                                 } else {
162                                         tag = 2;        /* Special */
163                                 }
164                                 break;
165                         }
166                 } else {
167                         tag = 3;                        /* Empty */
168                 }
169                 ret |= (tag << (2 * i));
170                 twd = twd >> 1;
171         }
172         return ret;
173 }
174
175 /*
176  * FXSR floating point environment conversions.
177  */
178
179 #ifdef CONFIG_MODE_TT
180 static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
181                                           struct pt_regs *regs)
182 {
183         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
184         unsigned long env[7];
185         struct _fpreg __user *to;
186         struct _fpxreg *from;
187         int i;
188
189         env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
190         env[1] = (unsigned long)fxsave->swd | 0xffff0000;
191         env[2] = twd_fxsr_to_i387(fxsave);
192         env[3] = fxsave->fip;
193         env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
194         env[5] = fxsave->foo;
195         env[6] = fxsave->fos;
196
197         if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
198                 return 1;
199
200         to = &buf->_st[0];
201         from = (struct _fpxreg *) &fxsave->st_space[0];
202         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
203                 if ( __copy_to_user( to, from, sizeof(*to) ) )
204                         return 1;
205         }
206         return 0;
207 }
208 #endif
209
210 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
211                                        struct pt_regs *regs)
212 {
213         return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
214 }
215
216 #ifdef CONFIG_MODE_TT
217 static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
218                                             struct _fpstate __user *buf)
219 {
220         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
221         unsigned long env[7];
222         struct _fpxreg *to;
223         struct _fpreg __user *from;
224         int i;
225
226         if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
227                 return 1;
228
229         fxsave->cwd = (unsigned short)(env[0] & 0xffff);
230         fxsave->swd = (unsigned short)(env[1] & 0xffff);
231         fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
232         fxsave->fip = env[3];
233         fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
234         fxsave->fcs = (env[4] & 0xffff);
235         fxsave->foo = env[5];
236         fxsave->fos = env[6];
237
238         to = (struct _fpxreg *) &fxsave->st_space[0];
239         from = &buf->_st[0];
240         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
241                 if ( __copy_from_user( to, from, sizeof(*from) ) )
242                         return 1;
243         }
244         return 0;
245 }
246 #endif
247
248 static inline int convert_fxsr_from_user(struct pt_regs *regs, 
249                                          struct _fpstate __user *buf)
250 {
251         return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
252 }
253
254 int get_fpregs(unsigned long buf, struct task_struct *child)
255 {
256         int err;
257
258         err = convert_fxsr_to_user((struct _fpstate __user *) buf,
259                                    &child->thread.regs);
260         if(err) return(-EFAULT);
261         else return(0);
262 }
263
264 int set_fpregs(unsigned long buf, struct task_struct *child)
265 {
266         int err;
267
268         err = convert_fxsr_from_user(&child->thread.regs, 
269                                      (struct _fpstate __user *) buf);
270         if(err) return(-EFAULT);
271         else return(0);
272 }
273
274 #ifdef CONFIG_MODE_TT
275 int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
276 {
277         struct pt_regs *regs = &tsk->thread.regs;
278         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
279         int err;
280
281         err = __copy_to_user((void __user *) buf, fxsave,
282                              sizeof(struct user_fxsr_struct));
283         if(err) return -EFAULT;
284         else return 0;
285 }
286 #endif
287
288 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
289 {
290         return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
291 }
292
293 #ifdef CONFIG_MODE_TT
294 int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
295 {
296         struct pt_regs *regs = &tsk->thread.regs;
297         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
298         int err;
299
300         err = __copy_from_user(fxsave, (void __user *) buf,
301                                sizeof(struct user_fxsr_struct) );
302         if(err) return -EFAULT;
303         else return 0;
304 }
305 #endif
306
307 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
308 {
309         return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
310 }
311
312 #ifdef notdef
313 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
314 {
315         fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
316                     (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
317         fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
318         fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
319         fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
320         fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
321         fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
322         fpu->fos = 0;
323         memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
324                sizeof(fpu->st_space));
325         return(1);
326 }
327 #endif
328
329 #ifdef CONFIG_MODE_TT
330 static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
331                                       struct user_i387_struct *buf)
332 {
333         struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
334         unsigned short *to;
335         unsigned short *from;
336         int i;
337
338         memcpy( buf, fpu, 7 * sizeof(long) );
339
340         to = (unsigned short *) &buf->st_space[0];
341         from = (unsigned short *) &fpu->st_space[0];
342         for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
343                 memcpy( to, from, 5 * sizeof(unsigned short) );
344         }
345 }
346 #endif
347
348 static inline void copy_fpu_fxsave(struct pt_regs *regs,
349                                    struct user_i387_struct *buf)
350 {
351         (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
352 }
353
354 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
355 {
356         copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
357         return(1);
358 }
359
360 /*
361  * Overrides for Emacs so that we follow Linus's tabbing style.
362  * Emacs will notice this stuff at the end of the file and automatically
363  * adjust the settings for this buffer only.  This must remain at the end
364  * of the file.
365  * ---------------------------------------------------------------------------
366  * Local variables:
367  * c-file-style: "linux"
368  * End:
369  */