initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / x86_64 / ia32 / ptrace32.c
blob4445da812ecf61d284da857564207a0784ca6d0f
1 /*
2 * 32bit ptrace for x86-64.
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.
11 * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $
12 */
14 #include <linux/kernel.h>
15 #include <linux/stddef.h>
16 #include <linux/sched.h>
17 #include <linux/syscalls.h>
18 #include <linux/unistd.h>
19 #include <linux/mm.h>
20 #include <linux/ptrace.h>
21 #include <asm/ptrace.h>
22 #include <asm/compat.h>
23 #include <asm/uaccess.h>
24 #include <asm/user32.h>
25 #include <asm/user.h>
26 #include <asm/errno.h>
27 #include <asm/debugreg.h>
28 #include <asm/i387.h>
29 #include <asm/fpu32.h>
31 /* determines which flags the user has access to. */
32 /* 1 = access 0 = no access */
33 #define FLAG_MASK 0x44dd5UL
35 #define R32(l,q) \
36 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
38 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
40 int i;
41 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
43 switch (regno) {
44 case offsetof(struct user32, regs.fs):
45 if (val && (val & 3) != 3) return -EIO;
46 child->thread.fs = val & 0xffff;
47 break;
48 case offsetof(struct user32, regs.gs):
49 if (val && (val & 3) != 3) return -EIO;
50 child->thread.gs = val & 0xffff;
51 break;
52 case offsetof(struct user32, regs.ds):
53 if (val && (val & 3) != 3) return -EIO;
54 child->thread.ds = val & 0xffff;
55 break;
56 case offsetof(struct user32, regs.es):
57 child->thread.es = val & 0xffff;
58 break;
59 case offsetof(struct user32, regs.ss):
60 if ((val & 3) != 3) return -EIO;
61 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
62 break;
63 case offsetof(struct user32, regs.cs):
64 if ((val & 3) != 3) return -EIO;
65 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
66 break;
68 R32(ebx, rbx);
69 R32(ecx, rcx);
70 R32(edx, rdx);
71 R32(edi, rdi);
72 R32(esi, rsi);
73 R32(ebp, rbp);
74 R32(eax, rax);
75 R32(orig_eax, orig_rax);
76 R32(eip, rip);
77 R32(esp, rsp);
79 case offsetof(struct user32, regs.eflags): {
80 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
81 val &= FLAG_MASK;
82 *flags = val | (*flags & ~FLAG_MASK);
83 break;
86 case offsetof(struct user32, u_debugreg[4]):
87 case offsetof(struct user32, u_debugreg[5]):
88 return -EIO;
90 case offsetof(struct user32, u_debugreg[0]):
91 child->thread.debugreg0 = val;
92 break;
94 case offsetof(struct user32, u_debugreg[1]):
95 child->thread.debugreg1 = val;
96 break;
98 case offsetof(struct user32, u_debugreg[2]):
99 child->thread.debugreg2 = val;
100 break;
102 case offsetof(struct user32, u_debugreg[3]):
103 child->thread.debugreg3 = val;
104 break;
106 case offsetof(struct user32, u_debugreg[6]):
107 child->thread.debugreg6 = val;
108 break;
110 case offsetof(struct user32, u_debugreg[7]):
111 val &= ~DR_CONTROL_RESERVED;
112 /* You are not expected to understand this ... I don't neither. */
113 for(i=0; i<4; i++)
114 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
115 return -EIO;
116 child->thread.debugreg7 = val;
117 break;
119 default:
120 if (regno > sizeof(struct user32) || (regno & 3))
121 return -EIO;
123 /* Other dummy fields in the virtual user structure are ignored */
124 break;
126 return 0;
129 #undef R32
131 #define R32(l,q) \
132 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
134 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
136 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
138 switch (regno) {
139 case offsetof(struct user32, regs.fs):
140 *val = child->thread.fs;
141 break;
142 case offsetof(struct user32, regs.gs):
143 *val = child->thread.gs;
144 break;
145 case offsetof(struct user32, regs.ds):
146 *val = child->thread.ds;
147 break;
148 case offsetof(struct user32, regs.es):
149 *val = child->thread.es;
150 break;
152 R32(cs, cs);
153 R32(ss, ss);
154 R32(ebx, rbx);
155 R32(ecx, rcx);
156 R32(edx, rdx);
157 R32(edi, rdi);
158 R32(esi, rsi);
159 R32(ebp, rbp);
160 R32(eax, rax);
161 R32(orig_eax, orig_rax);
162 R32(eip, rip);
163 R32(eflags, eflags);
164 R32(esp, rsp);
166 case offsetof(struct user32, u_debugreg[0]):
167 *val = child->thread.debugreg0;
168 break;
169 case offsetof(struct user32, u_debugreg[1]):
170 *val = child->thread.debugreg1;
171 break;
172 case offsetof(struct user32, u_debugreg[2]):
173 *val = child->thread.debugreg2;
174 break;
175 case offsetof(struct user32, u_debugreg[3]):
176 *val = child->thread.debugreg3;
177 break;
178 case offsetof(struct user32, u_debugreg[6]):
179 *val = child->thread.debugreg6;
180 break;
181 case offsetof(struct user32, u_debugreg[7]):
182 *val = child->thread.debugreg7;
183 break;
185 default:
186 if (regno > sizeof(struct user32) || (regno & 3))
187 return -EIO;
189 /* Other dummy fields in the virtual user structure are ignored */
190 *val = 0;
191 break;
193 return 0;
196 #undef R32
198 static struct task_struct *find_target(int request, int pid, int *err)
200 struct task_struct *child;
202 *err = -EPERM;
203 if (pid == 1)
204 return NULL;
206 *err = -ESRCH;
207 read_lock(&tasklist_lock);
208 child = find_task_by_pid(pid);
209 if (child)
210 get_task_struct(child);
211 read_unlock(&tasklist_lock);
212 if (child) {
213 *err = -EPERM;
214 if (child->pid == 1)
215 goto out;
216 *err = ptrace_check_attach(child, request == PTRACE_KILL);
217 if (*err < 0)
218 goto out;
219 return child;
221 out:
222 if (child)
223 put_task_struct(child);
224 return NULL;
228 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
230 struct task_struct *child;
231 struct pt_regs *childregs;
232 void __user *datap = compat_ptr(data);
233 int ret;
234 __u32 val;
236 switch (request) {
237 default:
238 return sys_ptrace(request, pid, addr, data);
240 case PTRACE_PEEKTEXT:
241 case PTRACE_PEEKDATA:
242 case PTRACE_POKEDATA:
243 case PTRACE_POKETEXT:
244 case PTRACE_POKEUSR:
245 case PTRACE_PEEKUSR:
246 case PTRACE_GETREGS:
247 case PTRACE_SETREGS:
248 case PTRACE_SETFPREGS:
249 case PTRACE_GETFPREGS:
250 case PTRACE_SETFPXREGS:
251 case PTRACE_GETFPXREGS:
252 case PTRACE_GETEVENTMSG:
253 break;
256 child = find_target(request, pid, &ret);
257 if (!child)
258 return ret;
260 childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs));
262 switch (request) {
263 case PTRACE_PEEKDATA:
264 case PTRACE_PEEKTEXT:
265 ret = 0;
266 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
267 ret = -EIO;
268 else
269 ret = put_user(val, (unsigned int __user *)datap);
270 break;
272 case PTRACE_POKEDATA:
273 case PTRACE_POKETEXT:
274 ret = 0;
275 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
276 ret = -EIO;
277 break;
279 case PTRACE_PEEKUSR:
280 ret = getreg32(child, addr, &val);
281 if (ret == 0)
282 ret = put_user(val, (__u32 __user *)datap);
283 break;
285 case PTRACE_POKEUSR:
286 ret = putreg32(child, addr, data);
287 break;
289 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
290 int i;
291 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
292 ret = -EIO;
293 break;
295 ret = 0;
296 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
297 getreg32(child, i, &val);
298 ret |= __put_user(val,(u32 __user *)datap);
299 datap += sizeof(u32);
301 break;
304 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
305 unsigned long tmp;
306 int i;
307 if (!access_ok(VERIFY_READ, datap, 16*4)) {
308 ret = -EIO;
309 break;
311 ret = 0;
312 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
313 ret |= __get_user(tmp, (u32 __user *)datap);
314 putreg32(child, i, tmp);
315 datap += sizeof(u32);
317 break;
320 case PTRACE_GETFPREGS:
321 ret = -EIO;
322 if (!access_ok(VERIFY_READ, compat_ptr(data),
323 sizeof(struct user_i387_struct)))
324 break;
325 save_i387_ia32(child, datap, childregs, 1);
326 ret = 0;
327 break;
329 case PTRACE_SETFPREGS:
330 ret = -EIO;
331 if (!access_ok(VERIFY_WRITE, datap,
332 sizeof(struct user_i387_struct)))
333 break;
334 ret = 0;
335 /* don't check EFAULT to be bug-to-bug compatible to i386 */
336 restore_i387_ia32(child, datap, 1);
337 break;
339 case PTRACE_GETFPXREGS: {
340 struct user32_fxsr_struct __user *u = datap;
341 init_fpu(child);
342 ret = -EIO;
343 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
344 break;
345 ret = -EFAULT;
346 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
347 break;
348 ret = __put_user(childregs->cs, &u->fcs);
349 ret |= __put_user(child->thread.ds, &u->fos);
350 break;
352 case PTRACE_SETFPXREGS: {
353 struct user32_fxsr_struct __user *u = datap;
354 unlazy_fpu(child);
355 ret = -EIO;
356 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
357 break;
358 /* no checking to be bug-to-bug compatible with i386 */
359 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
360 child->used_math = 1;
361 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
362 ret = 0;
363 break;
366 case PTRACE_GETEVENTMSG:
367 ret = put_user(child->ptrace_message,(unsigned int __user *)(u64)data);
368 break;
370 default:
371 ret = -EINVAL;
372 break;
375 put_task_struct(child);
376 return ret;