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