Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / arch / x86_64 / ia32 / ptrace32.c
1 /* 
2  * 32bit ptrace for x86-64.
3  *
4  * Copyright 2001,2002 Andi Kleen, SuSE Labs.
5  * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier 
6  * copyright.
7  * 
8  * This allows to access 64bit processes too; but there is no way to see the extended 
9  * register contents.
10  */ 
11
12 #include <linux/kernel.h>
13 #include <linux/stddef.h>
14 #include <linux/sched.h>
15 #include <linux/syscalls.h>
16 #include <linux/unistd.h>
17 #include <linux/mm.h>
18 #include <linux/ptrace.h>
19 #include <asm/ptrace.h>
20 #include <asm/compat.h>
21 #include <asm/uaccess.h>
22 #include <asm/user32.h>
23 #include <asm/user.h>
24 #include <asm/errno.h>
25 #include <asm/debugreg.h>
26 #include <asm/i387.h>
27 #include <asm/fpu32.h>
28 #include <asm/ia32.h>
29
30 /*
31  * Determines which flags the user has access to [1 = access, 0 = no access].
32  * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
33  * Also masks reserved bits (31-22, 15, 5, 3, 1).
34  */
35 #define FLAG_MASK 0x54dd5UL
36
37 #define R32(l,q) \
38         case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
39
40 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
41 {
42         int i;
43         __u64 *stack = (__u64 *)task_pt_regs(child);
44
45         switch (regno) {
46         case offsetof(struct user32, regs.fs):
47                 if (val && (val & 3) != 3) return -EIO; 
48                 child->thread.fsindex = val & 0xffff;
49                 break;
50         case offsetof(struct user32, regs.gs):
51                 if (val && (val & 3) != 3) return -EIO; 
52                 child->thread.gsindex = val & 0xffff;
53                 break;
54         case offsetof(struct user32, regs.ds):
55                 if (val && (val & 3) != 3) return -EIO; 
56                 child->thread.ds = val & 0xffff;
57                 break;
58         case offsetof(struct user32, regs.es):
59                 child->thread.es = val & 0xffff;
60                 break;
61         case offsetof(struct user32, regs.ss): 
62                 if ((val & 3) != 3) return -EIO;
63                 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
64                 break;
65         case offsetof(struct user32, regs.cs): 
66                 if ((val & 3) != 3) return -EIO;
67                 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
68                 break;
69
70         R32(ebx, rbx); 
71         R32(ecx, rcx);
72         R32(edx, rdx);
73         R32(edi, rdi);
74         R32(esi, rsi);
75         R32(ebp, rbp);
76         R32(eax, rax);
77         R32(orig_eax, orig_rax);
78         R32(eip, rip);
79         R32(esp, rsp);
80
81         case offsetof(struct user32, regs.eflags): {
82                 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
83                 val &= FLAG_MASK;
84                 *flags = val | (*flags & ~FLAG_MASK);
85                 break;
86         }
87
88         case offsetof(struct user32, u_debugreg[4]): 
89         case offsetof(struct user32, u_debugreg[5]):
90                 return -EIO;
91
92         case offsetof(struct user32, u_debugreg[0]):
93                 child->thread.debugreg0 = val;
94                 break;
95
96         case offsetof(struct user32, u_debugreg[1]):
97                 child->thread.debugreg1 = val;
98                 break;
99
100         case offsetof(struct user32, u_debugreg[2]):
101                 child->thread.debugreg2 = val;
102                 break;
103
104         case offsetof(struct user32, u_debugreg[3]):
105                 child->thread.debugreg3 = val;
106                 break;
107
108         case offsetof(struct user32, u_debugreg[6]):
109                 child->thread.debugreg6 = val;
110                 break; 
111
112         case offsetof(struct user32, u_debugreg[7]):
113                 val &= ~DR_CONTROL_RESERVED;
114                 /* See arch/i386/kernel/ptrace.c for an explanation of
115                  * this awkward check.*/
116                 for(i=0; i<4; i++)
117                         if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
118                                return -EIO;
119                 child->thread.debugreg7 = val; 
120                 break; 
121                     
122         default:
123                 if (regno > sizeof(struct user32) || (regno & 3))
124                         return -EIO;
125                
126                 /* Other dummy fields in the virtual user structure are ignored */ 
127                 break;          
128         }
129         return 0;
130 }
131
132 #undef R32
133
134 #define R32(l,q) \
135         case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
136
137 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
138 {
139         __u64 *stack = (__u64 *)task_pt_regs(child);
140
141         switch (regno) {
142         case offsetof(struct user32, regs.fs):
143                 *val = child->thread.fsindex;
144                 break;
145         case offsetof(struct user32, regs.gs):
146                 *val = child->thread.gsindex;
147                 break;
148         case offsetof(struct user32, regs.ds):
149                 *val = child->thread.ds;
150                 break;
151         case offsetof(struct user32, regs.es):
152                 *val = child->thread.es;
153                 break;
154
155         R32(cs, cs);
156         R32(ss, ss);
157         R32(ebx, rbx); 
158         R32(ecx, rcx);
159         R32(edx, rdx);
160         R32(edi, rdi);
161         R32(esi, rsi);
162         R32(ebp, rbp);
163         R32(eax, rax);
164         R32(orig_eax, orig_rax);
165         R32(eip, rip);
166         R32(eflags, eflags);
167         R32(esp, rsp);
168
169         case offsetof(struct user32, u_debugreg[0]): 
170                 *val = child->thread.debugreg0; 
171                 break; 
172         case offsetof(struct user32, u_debugreg[1]): 
173                 *val = child->thread.debugreg1; 
174                 break; 
175         case offsetof(struct user32, u_debugreg[2]): 
176                 *val = child->thread.debugreg2; 
177                 break; 
178         case offsetof(struct user32, u_debugreg[3]): 
179                 *val = child->thread.debugreg3; 
180                 break; 
181         case offsetof(struct user32, u_debugreg[6]): 
182                 *val = child->thread.debugreg6; 
183                 break; 
184         case offsetof(struct user32, u_debugreg[7]): 
185                 *val = child->thread.debugreg7; 
186                 break; 
187                     
188         default:
189                 if (regno > sizeof(struct user32) || (regno & 3))
190                         return -EIO;
191
192                 /* Other dummy fields in the virtual user structure are ignored */ 
193                 *val = 0;
194                 break;          
195         }
196         return 0;
197 }
198
199 #undef R32
200
201 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
202 {
203         int ret;
204         compat_siginfo_t *si32 = (compat_siginfo_t *)compat_ptr(data);
205         siginfo_t *si = compat_alloc_user_space(sizeof(siginfo_t));
206         if (request == PTRACE_SETSIGINFO) {
207                 ret = copy_siginfo_from_user32(si, si32);
208                 if (ret)
209                         return ret;
210         }
211         ret = sys_ptrace(request, pid, addr, (unsigned long)si);
212         if (ret)
213                 return ret;
214         if (request == PTRACE_GETSIGINFO)
215                 ret = copy_siginfo_to_user32(si32, si);
216         return ret;
217 }
218
219 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
220 {
221         struct task_struct *child;
222         struct pt_regs *childregs; 
223         void __user *datap = compat_ptr(data);
224         int ret;
225         __u32 val;
226
227         switch (request) { 
228         case PTRACE_TRACEME:
229         case PTRACE_ATTACH:
230         case PTRACE_KILL:
231         case PTRACE_CONT:
232         case PTRACE_SINGLESTEP:
233         case PTRACE_DETACH:
234         case PTRACE_SYSCALL:
235         case PTRACE_SETOPTIONS:
236                 return sys_ptrace(request, pid, addr, data); 
237
238         default:
239                 return -EINVAL;
240
241         case PTRACE_PEEKTEXT:
242         case PTRACE_PEEKDATA:
243         case PTRACE_POKEDATA:
244         case PTRACE_POKETEXT:
245         case PTRACE_POKEUSR:       
246         case PTRACE_PEEKUSR:
247         case PTRACE_GETREGS:
248         case PTRACE_SETREGS:
249         case PTRACE_SETFPREGS:
250         case PTRACE_GETFPREGS:
251         case PTRACE_SETFPXREGS:
252         case PTRACE_GETFPXREGS:
253         case PTRACE_GETEVENTMSG:
254                 break;
255
256         case PTRACE_SETSIGINFO:
257         case PTRACE_GETSIGINFO:
258                 return ptrace32_siginfo(request, pid, addr, data);
259         }
260
261         child = ptrace_get_task_struct(pid);
262         if (IS_ERR(child))
263                 return PTR_ERR(child);
264
265         ret = ptrace_check_attach(child, request == PTRACE_KILL);
266         if (ret < 0)
267                 goto out;
268
269         childregs = task_pt_regs(child);
270
271         switch (request) {
272         case PTRACE_PEEKDATA:
273         case PTRACE_PEEKTEXT:
274                 ret = 0;
275                 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
276                         ret = -EIO;
277                 else
278                         ret = put_user(val, (unsigned int __user *)datap); 
279                 break; 
280
281         case PTRACE_POKEDATA:
282         case PTRACE_POKETEXT:
283                 ret = 0;
284                 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
285                         ret = -EIO; 
286                 break;
287
288         case PTRACE_PEEKUSR:
289                 ret = getreg32(child, addr, &val);
290                 if (ret == 0)
291                         ret = put_user(val, (__u32 __user *)datap);
292                 break;
293
294         case PTRACE_POKEUSR:
295                 ret = putreg32(child, addr, data);
296                 break;
297
298         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
299                 int i;
300                 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
301                         ret = -EIO;
302                         break;
303                 }
304                 ret = 0;
305                 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
306                         getreg32(child, i, &val);
307                         ret |= __put_user(val,(u32 __user *)datap);
308                         datap += sizeof(u32);
309                 }
310                 break;
311         }
312
313         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
314                 unsigned long tmp;
315                 int i;
316                 if (!access_ok(VERIFY_READ, datap, 16*4)) {
317                         ret = -EIO;
318                         break;
319                 }
320                 ret = 0; 
321                 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
322                         ret |= __get_user(tmp, (u32 __user *)datap);
323                         putreg32(child, i, tmp);
324                         datap += sizeof(u32);
325                 }
326                 break;
327         }
328
329         case PTRACE_GETFPREGS:
330                 ret = -EIO; 
331                 if (!access_ok(VERIFY_READ, compat_ptr(data), 
332                                sizeof(struct user_i387_struct)))
333                         break;
334                 save_i387_ia32(child, datap, childregs, 1);
335                 ret = 0; 
336                         break;
337
338         case PTRACE_SETFPREGS:
339                 ret = -EIO;
340                 if (!access_ok(VERIFY_WRITE, datap, 
341                                sizeof(struct user_i387_struct)))
342                         break;
343                 ret = 0;
344                 /* don't check EFAULT to be bug-to-bug compatible to i386 */
345                 restore_i387_ia32(child, datap, 1);
346                 break;
347
348         case PTRACE_GETFPXREGS: { 
349                 struct user32_fxsr_struct __user *u = datap;
350                 init_fpu(child); 
351                 ret = -EIO;
352                 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
353                         break;
354                         ret = -EFAULT;
355                 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
356                         break;
357                 ret = __put_user(childregs->cs, &u->fcs);
358                 ret |= __put_user(child->thread.ds, &u->fos); 
359                 break; 
360         } 
361         case PTRACE_SETFPXREGS: { 
362                 struct user32_fxsr_struct __user *u = datap;
363                 unlazy_fpu(child);
364                 ret = -EIO;
365                 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
366                         break;
367                 /* no checking to be bug-to-bug compatible with i386 */
368                 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
369                 set_stopped_child_used_math(child);
370                 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
371                 ret = 0; 
372                 break;
373         }
374
375         case PTRACE_GETEVENTMSG:
376                 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
377                 break;
378
379         default:
380                 BUG();
381         }
382
383  out:
384         put_task_struct(child);
385         return ret;
386 }
387