vserver 2.0 rc7
[linux-2.6.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 int poke_user(struct task_struct *child, long addr, long data)
77 {
78         if ((addr & 3) || addr < 0)
79                 return -EIO;
80
81         if (addr < MAX_REG_OFFSET)
82                 return putreg(child, addr, data);
83
84         else if((addr >= offsetof(struct user, u_debugreg[0])) &&
85                 (addr <= offsetof(struct user, u_debugreg[7]))){
86                 addr -= offsetof(struct user, u_debugreg[0]);
87                 addr = addr >> 2;
88                 if((addr == 4) || (addr == 5)) return -EIO;
89                 child->thread.arch.debugregs[addr] = data;
90                 return 0;
91         }
92         return -EIO;
93 }
94
95 unsigned long getreg(struct task_struct *child, int regno)
96 {
97         unsigned long retval = ~0UL;
98
99         regno >>= 2;
100         switch (regno) {
101         case FS:
102         case GS:
103         case DS:
104         case ES:
105         case SS:
106         case CS:
107                 retval = 0xffff;
108                 /* fall through */
109         default:
110                 retval &= PT_REG(&child->thread.regs, regno);
111         }
112         return retval;
113 }
114
115 int peek_user(struct task_struct *child, long addr, long data)
116 {
117 /* read the word at location addr in the USER area. */
118         unsigned long tmp;
119
120         if ((addr & 3) || addr < 0)
121                 return -EIO;
122
123         tmp = 0;  /* Default return condition */
124         if(addr < MAX_REG_OFFSET){
125                 tmp = getreg(child, addr);
126         }
127         else if((addr >= offsetof(struct user, u_debugreg[0])) &&
128                 (addr <= offsetof(struct user, u_debugreg[7]))){
129                 addr -= offsetof(struct user, u_debugreg[0]);
130                 addr = addr >> 2;
131                 tmp = child->thread.arch.debugregs[addr];
132         }
133         return put_user(tmp, (unsigned long *) data);
134 }
135
136 struct i387_fxsave_struct {
137         unsigned short  cwd;
138         unsigned short  swd;
139         unsigned short  twd;
140         unsigned short  fop;
141         long    fip;
142         long    fcs;
143         long    foo;
144         long    fos;
145         long    mxcsr;
146         long    reserved;
147         long    st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
148         long    xmm_space[32];  /* 8*16 bytes for each XMM-reg = 128 bytes */
149         long    padding[56];
150 };
151
152 /*
153  * FPU tag word conversions.
154  */
155
156 static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
157 {
158         unsigned int tmp; /* to avoid 16 bit prefixes in the code */
159  
160         /* Transform each pair of bits into 01 (valid) or 00 (empty) */
161         tmp = ~twd;
162         tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
163         /* and move the valid bits to the lower byte. */
164         tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
165         tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
166         tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
167         return tmp;
168 }
169
170 static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
171 {
172         struct _fpxreg *st = NULL;
173         unsigned long twd = (unsigned long) fxsave->twd;
174         unsigned long tag;
175         unsigned long ret = 0xffff0000;
176         int i;
177
178 #define FPREG_ADDR(f, n)        ((char *)&(f)->st_space + (n) * 16);
179
180         for ( i = 0 ; i < 8 ; i++ ) {
181                 if ( twd & 0x1 ) {
182                         st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
183
184                         switch ( st->exponent & 0x7fff ) {
185                         case 0x7fff:
186                                 tag = 2;                /* Special */
187                                 break;
188                         case 0x0000:
189                                 if ( !st->significand[0] &&
190                                      !st->significand[1] &&
191                                      !st->significand[2] &&
192                                      !st->significand[3] ) {
193                                         tag = 1;        /* Zero */
194                                 } else {
195                                         tag = 2;        /* Special */
196                                 }
197                                 break;
198                         default:
199                                 if ( st->significand[3] & 0x8000 ) {
200                                         tag = 0;        /* Valid */
201                                 } else {
202                                         tag = 2;        /* Special */
203                                 }
204                                 break;
205                         }
206                 } else {
207                         tag = 3;                        /* Empty */
208                 }
209                 ret |= (tag << (2 * i));
210                 twd = twd >> 1;
211         }
212         return ret;
213 }
214
215 /*
216  * FXSR floating point environment conversions.
217  */
218
219 #ifdef CONFIG_MODE_TT
220 static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
221                                           struct pt_regs *regs)
222 {
223         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
224         unsigned long env[7];
225         struct _fpreg __user *to;
226         struct _fpxreg *from;
227         int i;
228
229         env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
230         env[1] = (unsigned long)fxsave->swd | 0xffff0000;
231         env[2] = twd_fxsr_to_i387(fxsave);
232         env[3] = fxsave->fip;
233         env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
234         env[5] = fxsave->foo;
235         env[6] = fxsave->fos;
236
237         if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
238                 return 1;
239
240         to = &buf->_st[0];
241         from = (struct _fpxreg *) &fxsave->st_space[0];
242         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
243                 if ( __copy_to_user( to, from, sizeof(*to) ) )
244                         return 1;
245         }
246         return 0;
247 }
248 #endif
249
250 static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
251                                        struct pt_regs *regs)
252 {
253         return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
254 }
255
256 #ifdef CONFIG_MODE_TT
257 static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
258                                             struct _fpstate __user *buf)
259 {
260         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
261         unsigned long env[7];
262         struct _fpxreg *to;
263         struct _fpreg __user *from;
264         int i;
265
266         if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
267                 return 1;
268
269         fxsave->cwd = (unsigned short)(env[0] & 0xffff);
270         fxsave->swd = (unsigned short)(env[1] & 0xffff);
271         fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
272         fxsave->fip = env[3];
273         fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
274         fxsave->fcs = (env[4] & 0xffff);
275         fxsave->foo = env[5];
276         fxsave->fos = env[6];
277
278         to = (struct _fpxreg *) &fxsave->st_space[0];
279         from = &buf->_st[0];
280         for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
281                 if ( __copy_from_user( to, from, sizeof(*from) ) )
282                         return 1;
283         }
284         return 0;
285 }
286 #endif
287
288 static inline int convert_fxsr_from_user(struct pt_regs *regs, 
289                                          struct _fpstate __user *buf)
290 {
291         return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
292 }
293
294 int get_fpregs(unsigned long buf, struct task_struct *child)
295 {
296         int err;
297
298         err = convert_fxsr_to_user((struct _fpstate __user *) buf,
299                                    &child->thread.regs);
300         if(err) return(-EFAULT);
301         else return(0);
302 }
303
304 int set_fpregs(unsigned long buf, struct task_struct *child)
305 {
306         int err;
307
308         err = convert_fxsr_from_user(&child->thread.regs, 
309                                      (struct _fpstate __user *) buf);
310         if(err) return(-EFAULT);
311         else return(0);
312 }
313
314 #ifdef CONFIG_MODE_TT
315 int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
316 {
317         struct pt_regs *regs = &tsk->thread.regs;
318         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
319         int err;
320
321         err = __copy_to_user((void __user *) buf, fxsave,
322                              sizeof(struct user_fxsr_struct));
323         if(err) return -EFAULT;
324         else return 0;
325 }
326 #endif
327
328 int get_fpxregs(unsigned long buf, struct task_struct *tsk)
329 {
330         return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
331 }
332
333 #ifdef CONFIG_MODE_TT
334 int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
335 {
336         struct pt_regs *regs = &tsk->thread.regs;
337         struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
338         int err;
339
340         err = __copy_from_user(fxsave, (void __user *) buf,
341                                sizeof(struct user_fxsr_struct) );
342         if(err) return -EFAULT;
343         else return 0;
344 }
345 #endif
346
347 int set_fpxregs(unsigned long buf, struct task_struct *tsk)
348 {
349         return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
350 }
351
352 #ifdef notdef
353 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
354 {
355         fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
356                     (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
357         fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
358         fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
359         fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
360         fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
361         fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
362         fpu->fos = 0;
363         memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
364                sizeof(fpu->st_space));
365         return(1);
366 }
367 #endif
368
369 #ifdef CONFIG_MODE_TT
370 static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
371                                       struct user_i387_struct *buf)
372 {
373         struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
374         unsigned short *to;
375         unsigned short *from;
376         int i;
377
378         memcpy( buf, fpu, 7 * sizeof(long) );
379
380         to = (unsigned short *) &buf->st_space[0];
381         from = (unsigned short *) &fpu->st_space[0];
382         for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
383                 memcpy( to, from, 5 * sizeof(unsigned short) );
384         }
385 }
386 #endif
387
388 static inline void copy_fpu_fxsave(struct pt_regs *regs,
389                                    struct user_i387_struct *buf)
390 {
391         (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
392 }
393
394 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
395 {
396         copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
397         return(1);
398 }
399
400 /*
401  * Overrides for Emacs so that we follow Linus's tabbing style.
402  * Emacs will notice this stuff at the end of the file and automatically
403  * adjust the settings for this buffer only.  This must remain at the end
404  * of the file.
405  * ---------------------------------------------------------------------------
406  * Local variables:
407  * c-file-style: "linux"
408  * End:
409  */