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 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
201 struct task_struct *child;
202 struct pt_regs *childregs;
203 void __user *datap = compat_ptr(data);
209 return sys_ptrace(request, pid, addr, data);
211 case PTRACE_PEEKTEXT:
212 case PTRACE_PEEKDATA:
213 case PTRACE_POKEDATA:
214 case PTRACE_POKETEXT:
219 case PTRACE_SETFPREGS:
220 case PTRACE_GETFPREGS:
221 case PTRACE_SETFPXREGS:
222 case PTRACE_GETFPXREGS:
223 case PTRACE_GETEVENTMSG:
227 if (request == PTRACE_TRACEME)
228 return ptrace_traceme();
230 child = ptrace_get_task_struct(pid);
232 return PTR_ERR(child);
234 ret = ptrace_check_attach(child, request == PTRACE_KILL);
238 childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs));
241 case PTRACE_PEEKDATA:
242 case PTRACE_PEEKTEXT:
244 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
247 ret = put_user(val, (unsigned int __user *)datap);
250 case PTRACE_POKEDATA:
251 case PTRACE_POKETEXT:
253 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
258 ret = getreg32(child, addr, &val);
260 ret = put_user(val, (__u32 __user *)datap);
264 ret = putreg32(child, addr, data);
267 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
269 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
274 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
275 getreg32(child, i, &val);
276 ret |= __put_user(val,(u32 __user *)datap);
277 datap += sizeof(u32);
282 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
285 if (!access_ok(VERIFY_READ, datap, 16*4)) {
290 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
291 ret |= __get_user(tmp, (u32 __user *)datap);
292 putreg32(child, i, tmp);
293 datap += sizeof(u32);
298 case PTRACE_GETFPREGS:
300 if (!access_ok(VERIFY_READ, compat_ptr(data),
301 sizeof(struct user_i387_struct)))
303 save_i387_ia32(child, datap, childregs, 1);
307 case PTRACE_SETFPREGS:
309 if (!access_ok(VERIFY_WRITE, datap,
310 sizeof(struct user_i387_struct)))
313 /* don't check EFAULT to be bug-to-bug compatible to i386 */
314 restore_i387_ia32(child, datap, 1);
317 case PTRACE_GETFPXREGS: {
318 struct user32_fxsr_struct __user *u = datap;
321 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
324 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
326 ret = __put_user(childregs->cs, &u->fcs);
327 ret |= __put_user(child->thread.ds, &u->fos);
330 case PTRACE_SETFPXREGS: {
331 struct user32_fxsr_struct __user *u = datap;
334 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
336 /* no checking to be bug-to-bug compatible with i386 */
337 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
338 set_stopped_child_used_math(child);
339 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
344 case PTRACE_GETEVENTMSG:
345 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
354 put_task_struct(child);