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