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.19 2004/01/29 03:31:13 ak Exp $
14 #include <linux/kernel.h>
15 #include <linux/stddef.h>
16 #include <linux/sched.h>
18 #include <asm/ptrace.h>
19 #include <asm/uaccess.h>
20 #include <asm/user32.h>
22 #include <asm/errno.h>
23 #include <asm/debugreg.h>
25 #include <asm/fpu32.h>
28 /* determines which flags the user has access to. */
29 /* 1 = access 0 = no access */
30 #define FLAG_MASK 0x44dd5UL
33 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
35 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
38 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
41 case offsetof(struct user32, regs.fs):
42 if (val && (val & 3) != 3) return -EIO;
43 child->thread.fs = val & 0xffff;
45 case offsetof(struct user32, regs.gs):
46 if (val && (val & 3) != 3) return -EIO;
47 child->thread.gs = val & 0xffff;
49 case offsetof(struct user32, regs.ds):
50 if (val && (val & 3) != 3) return -EIO;
51 child->thread.ds = val & 0xffff;
53 case offsetof(struct user32, regs.es):
54 child->thread.es = val & 0xffff;
56 case offsetof(struct user32, regs.ss):
57 if ((val & 3) != 3) return -EIO;
58 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
60 case offsetof(struct user32, regs.cs):
61 if ((val & 3) != 3) return -EIO;
62 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
72 R32(orig_eax, orig_rax);
76 case offsetof(struct user32, regs.eflags): {
77 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
79 *flags = val | (*flags & ~FLAG_MASK);
83 case offsetof(struct user32, u_debugreg[4]):
84 case offsetof(struct user32, u_debugreg[5]):
87 case offsetof(struct user32, u_debugreg[0]) ...
88 offsetof(struct user32, u_debugreg[3]):
89 case offsetof(struct user32, u_debugreg[6]):
90 child->thread.debugreg
91 [(regno-offsetof(struct user32, u_debugreg[0]))/4]
95 case offsetof(struct user32, u_debugreg[7]):
96 val &= ~DR_CONTROL_RESERVED;
97 /* You are not expected to understand this ... I don't neither. */
99 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
101 child->thread.debugreg[7] = val;
105 if (regno > sizeof(struct user32) || (regno & 3))
108 /* Other dummy fields in the virtual user structure are ignored */
117 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
119 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
121 __u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs));
124 case offsetof(struct user32, regs.fs):
125 *val = child->thread.fs;
127 case offsetof(struct user32, regs.gs):
128 *val = child->thread.gs;
130 case offsetof(struct user32, regs.ds):
131 *val = child->thread.ds;
133 case offsetof(struct user32, regs.es):
134 *val = child->thread.es;
146 R32(orig_eax, orig_rax);
151 case offsetof(struct user32, u_debugreg[0]) ... offsetof(struct user32, u_debugreg[7]):
152 *val = child->thread.debugreg[(regno-offsetof(struct user32, u_debugreg[0]))/4];
156 if (regno > sizeof(struct user32) || (regno & 3))
159 /* Other dummy fields in the virtual user structure are ignored */
168 static struct task_struct *find_target(int request, int pid, int *err)
170 struct task_struct *child;
177 read_lock(&tasklist_lock);
178 child = find_task_by_pid(pid);
180 get_task_struct(child);
181 read_unlock(&tasklist_lock);
186 *err = ptrace_check_attach(child, request == PTRACE_KILL);
192 free_task_struct(child);
198 extern asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, unsigned long data);
200 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
202 struct task_struct *child;
203 struct pt_regs *childregs;
213 case PTRACE_SINGLESTEP:
215 case PTRACE_SETOPTIONS:
216 ret = sys_ptrace(request, pid, addr, data);
219 case PTRACE_PEEKTEXT:
220 case PTRACE_PEEKDATA:
221 case PTRACE_POKEDATA:
222 case PTRACE_POKETEXT:
227 case PTRACE_SETFPREGS:
228 case PTRACE_GETFPREGS:
229 case PTRACE_SETFPXREGS:
230 case PTRACE_GETFPXREGS:
237 child = find_target(request, pid, &ret);
241 childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs));
244 case PTRACE_PEEKDATA:
245 case PTRACE_PEEKTEXT:
247 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
250 ret = put_user(val, (unsigned int *)(u64)data);
253 case PTRACE_POKEDATA:
254 case PTRACE_POKETEXT:
256 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
261 ret = getreg32(child, addr, &val);
263 ret = put_user(val, (__u32 *)(unsigned long) data);
267 ret = putreg32(child, addr, data);
270 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
272 if (!access_ok(VERIFY_WRITE, (unsigned *)(unsigned long)data, 16*4)) {
277 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
278 getreg32(child, i, &val);
279 ret |= __put_user(val,(u32 *) (unsigned long) data);
285 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
288 if (!access_ok(VERIFY_READ, (unsigned *)(unsigned long)data, 16*4)) {
293 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
294 ret |= __get_user(tmp, (u32 *) (unsigned long) data);
295 putreg32(child, i, tmp);
301 case PTRACE_GETFPREGS:
303 if (!access_ok(VERIFY_READ, (void *)(u64)data,
304 sizeof(struct user_i387_struct)))
306 save_i387_ia32(child, (void *)(u64)data, childregs, 1);
310 case PTRACE_SETFPREGS:
312 if (!access_ok(VERIFY_WRITE, (void *)(u64)data,
313 sizeof(struct user_i387_struct)))
316 /* don't check EFAULT to be bug-to-bug compatible to i386 */
317 restore_i387_ia32(child, (void *)(u64)data, 1);
320 case PTRACE_GETFPXREGS: {
321 struct user32_fxsr_struct *u = (void *)(u64)data;
324 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
327 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
329 ret = __put_user(childregs->cs, &u->fcs);
330 ret |= __put_user(child->thread.ds, &u->fos);
333 case PTRACE_SETFPXREGS: {
334 struct user32_fxsr_struct *u = (void *)(u64)data;
337 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
339 /* no checking to be bug-to-bug compatible with i386 */
340 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
341 child->used_math = 1;
342 child->thread.i387.fxsave.mxcsr &= 0xffbf;
352 free_task_struct(child);