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
8 * This allows to access 64bit processes too; but there is no way to see the extended
11 * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $
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>
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>
26 #include <asm/errno.h>
27 #include <asm/debugreg.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
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)
41 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
44 case offsetof(struct user32, regs.fs):
45 if (val && (val & 3) != 3) return -EIO;
46 child->thread.fsindex = val & 0xffff;
48 case offsetof(struct user32, regs.gs):
49 if (val && (val & 3) != 3) return -EIO;
50 child->thread.gsindex = val & 0xffff;
52 case offsetof(struct user32, regs.ds):
53 if (val && (val & 3) != 3) return -EIO;
54 child->thread.ds = val & 0xffff;
56 case offsetof(struct user32, regs.es):
57 child->thread.es = val & 0xffff;
59 case offsetof(struct user32, regs.ss):
60 if ((val & 3) != 3) return -EIO;
61 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
63 case offsetof(struct user32, regs.cs):
64 if ((val & 3) != 3) return -EIO;
65 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
75 R32(orig_eax, orig_rax);
79 case offsetof(struct user32, regs.eflags): {
80 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
82 *flags = val | (*flags & ~FLAG_MASK);
86 case offsetof(struct user32, u_debugreg[4]):
87 case offsetof(struct user32, u_debugreg[5]):
90 case offsetof(struct user32, u_debugreg[0]):
91 child->thread.debugreg0 = val;
94 case offsetof(struct user32, u_debugreg[1]):
95 child->thread.debugreg1 = val;
98 case offsetof(struct user32, u_debugreg[2]):
99 child->thread.debugreg2 = val;
102 case offsetof(struct user32, u_debugreg[3]):
103 child->thread.debugreg3 = val;
106 case offsetof(struct user32, u_debugreg[6]):
107 child->thread.debugreg6 = val;
110 case offsetof(struct user32, u_debugreg[7]):
111 val &= ~DR_CONTROL_RESERVED;
112 /* See arch/i386/kernel/ptrace.c for an explanation of
113 * this awkward check.*/
115 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
117 child->thread.debugreg7 = val;
121 if (regno > sizeof(struct user32) || (regno & 3))
124 /* Other dummy fields in the virtual user structure are ignored */
133 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
135 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
137 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
140 case offsetof(struct user32, regs.fs):
141 *val = child->thread.fsindex;
143 case offsetof(struct user32, regs.gs):
144 *val = child->thread.gsindex;
146 case offsetof(struct user32, regs.ds):
147 *val = child->thread.ds;
149 case offsetof(struct user32, regs.es):
150 *val = child->thread.es;
162 R32(orig_eax, orig_rax);
167 case offsetof(struct user32, u_debugreg[0]):
168 *val = child->thread.debugreg0;
170 case offsetof(struct user32, u_debugreg[1]):
171 *val = child->thread.debugreg1;
173 case offsetof(struct user32, u_debugreg[2]):
174 *val = child->thread.debugreg2;
176 case offsetof(struct user32, u_debugreg[3]):
177 *val = child->thread.debugreg3;
179 case offsetof(struct user32, u_debugreg[6]):
180 *val = child->thread.debugreg6;
182 case offsetof(struct user32, u_debugreg[7]):
183 *val = child->thread.debugreg7;
187 if (regno > sizeof(struct user32) || (regno & 3))
190 /* Other dummy fields in the virtual user structure are ignored */
199 static struct task_struct *find_target(int request, int pid, int *err)
201 struct task_struct *child;
208 read_lock(&tasklist_lock);
209 child = find_task_by_pid(pid);
211 get_task_struct(child);
212 read_unlock(&tasklist_lock);
217 *err = ptrace_check_attach(child, request == PTRACE_KILL);
224 put_task_struct(child);
229 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
231 struct task_struct *child;
232 struct pt_regs *childregs;
233 void __user *datap = compat_ptr(data);
239 return sys_ptrace(request, pid, addr, data);
241 case PTRACE_PEEKTEXT:
242 case PTRACE_PEEKDATA:
243 case PTRACE_POKEDATA:
244 case PTRACE_POKETEXT:
249 case PTRACE_SETFPREGS:
250 case PTRACE_GETFPREGS:
251 case PTRACE_SETFPXREGS:
252 case PTRACE_GETFPXREGS:
253 case PTRACE_GETEVENTMSG:
257 child = find_target(request, pid, &ret);
261 childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs));
264 case PTRACE_PEEKDATA:
265 case PTRACE_PEEKTEXT:
267 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
270 ret = put_user(val, (unsigned int __user *)datap);
273 case PTRACE_POKEDATA:
274 case PTRACE_POKETEXT:
276 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
281 ret = getreg32(child, addr, &val);
283 ret = put_user(val, (__u32 __user *)datap);
287 ret = putreg32(child, addr, data);
290 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
292 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
297 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
298 getreg32(child, i, &val);
299 ret |= __put_user(val,(u32 __user *)datap);
300 datap += sizeof(u32);
305 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
308 if (!access_ok(VERIFY_READ, datap, 16*4)) {
313 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
314 ret |= __get_user(tmp, (u32 __user *)datap);
315 putreg32(child, i, tmp);
316 datap += sizeof(u32);
321 case PTRACE_GETFPREGS:
323 if (!access_ok(VERIFY_READ, compat_ptr(data),
324 sizeof(struct user_i387_struct)))
326 save_i387_ia32(child, datap, childregs, 1);
330 case PTRACE_SETFPREGS:
332 if (!access_ok(VERIFY_WRITE, datap,
333 sizeof(struct user_i387_struct)))
336 /* don't check EFAULT to be bug-to-bug compatible to i386 */
337 restore_i387_ia32(child, datap, 1);
340 case PTRACE_GETFPXREGS: {
341 struct user32_fxsr_struct __user *u = datap;
344 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
347 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
349 ret = __put_user(childregs->cs, &u->fcs);
350 ret |= __put_user(child->thread.ds, &u->fos);
353 case PTRACE_SETFPXREGS: {
354 struct user32_fxsr_struct __user *u = datap;
357 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
359 /* no checking to be bug-to-bug compatible with i386 */
360 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
361 set_stopped_child_used_math(child);
362 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
367 case PTRACE_GETEVENTMSG:
368 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
376 put_task_struct(child);