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
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>
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>
24 #include <asm/errno.h>
25 #include <asm/debugreg.h>
27 #include <asm/fpu32.h>
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).
35 #define FLAG_MASK 0x54dd5UL
38 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
40 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
43 __u64 *stack = (__u64 *)task_pt_regs(child);
46 case offsetof(struct user32, regs.fs):
47 if (val && (val & 3) != 3) return -EIO;
48 child->thread.fsindex = val & 0xffff;
50 case offsetof(struct user32, regs.gs):
51 if (val && (val & 3) != 3) return -EIO;
52 child->thread.gsindex = val & 0xffff;
54 case offsetof(struct user32, regs.ds):
55 if (val && (val & 3) != 3) return -EIO;
56 child->thread.ds = val & 0xffff;
58 case offsetof(struct user32, regs.es):
59 child->thread.es = val & 0xffff;
61 case offsetof(struct user32, regs.ss):
62 if ((val & 3) != 3) return -EIO;
63 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
65 case offsetof(struct user32, regs.cs):
66 if ((val & 3) != 3) return -EIO;
67 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
77 R32(orig_eax, orig_rax);
81 case offsetof(struct user32, regs.eflags): {
82 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
84 *flags = val | (*flags & ~FLAG_MASK);
88 case offsetof(struct user32, u_debugreg[4]):
89 case offsetof(struct user32, u_debugreg[5]):
92 case offsetof(struct user32, u_debugreg[0]):
93 child->thread.debugreg0 = val;
96 case offsetof(struct user32, u_debugreg[1]):
97 child->thread.debugreg1 = val;
100 case offsetof(struct user32, u_debugreg[2]):
101 child->thread.debugreg2 = val;
104 case offsetof(struct user32, u_debugreg[3]):
105 child->thread.debugreg3 = val;
108 case offsetof(struct user32, u_debugreg[6]):
109 child->thread.debugreg6 = val;
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.*/
117 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
119 child->thread.debugreg7 = val;
121 set_tsk_thread_flag(child, TIF_DEBUG);
123 clear_tsk_thread_flag(child, TIF_DEBUG);
127 if (regno > sizeof(struct user32) || (regno & 3))
130 /* Other dummy fields in the virtual user structure are ignored */
139 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
141 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
143 __u64 *stack = (__u64 *)task_pt_regs(child);
146 case offsetof(struct user32, regs.fs):
147 *val = child->thread.fsindex;
149 case offsetof(struct user32, regs.gs):
150 *val = child->thread.gsindex;
152 case offsetof(struct user32, regs.ds):
153 *val = child->thread.ds;
155 case offsetof(struct user32, regs.es):
156 *val = child->thread.es;
168 R32(orig_eax, orig_rax);
173 case offsetof(struct user32, u_debugreg[0]):
174 *val = child->thread.debugreg0;
176 case offsetof(struct user32, u_debugreg[1]):
177 *val = child->thread.debugreg1;
179 case offsetof(struct user32, u_debugreg[2]):
180 *val = child->thread.debugreg2;
182 case offsetof(struct user32, u_debugreg[3]):
183 *val = child->thread.debugreg3;
185 case offsetof(struct user32, u_debugreg[6]):
186 *val = child->thread.debugreg6;
188 case offsetof(struct user32, u_debugreg[7]):
189 *val = child->thread.debugreg7;
193 if (regno > sizeof(struct user32) || (regno & 3))
196 /* Other dummy fields in the virtual user structure are ignored */
205 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
208 compat_siginfo_t *si32 = (compat_siginfo_t *)compat_ptr(data);
210 siginfo_t *si = compat_alloc_user_space(sizeof(siginfo_t));
211 if (request == PTRACE_SETSIGINFO) {
212 memset(&ssi, 0, sizeof(siginfo_t));
213 ret = copy_siginfo_from_user32(&ssi, si32);
216 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
219 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
222 if (request == PTRACE_GETSIGINFO) {
223 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
225 ret = copy_siginfo_to_user32(si32, &ssi);
230 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
232 struct task_struct *child;
233 struct pt_regs *childregs;
234 void __user *datap = compat_ptr(data);
243 case PTRACE_SINGLESTEP:
246 case PTRACE_SETOPTIONS:
247 return sys_ptrace(request, pid, addr, data);
252 case PTRACE_PEEKTEXT:
253 case PTRACE_PEEKDATA:
254 case PTRACE_POKEDATA:
255 case PTRACE_POKETEXT:
260 case PTRACE_SETFPREGS:
261 case PTRACE_GETFPREGS:
262 case PTRACE_SETFPXREGS:
263 case PTRACE_GETFPXREGS:
264 case PTRACE_GETEVENTMSG:
267 case PTRACE_SETSIGINFO:
268 case PTRACE_GETSIGINFO:
269 return ptrace32_siginfo(request, pid, addr, data);
272 child = ptrace_get_task_struct(pid);
274 return PTR_ERR(child);
276 ret = ptrace_check_attach(child, request == PTRACE_KILL);
280 childregs = task_pt_regs(child);
283 case PTRACE_PEEKDATA:
284 case PTRACE_PEEKTEXT:
286 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
289 ret = put_user(val, (unsigned int __user *)datap);
292 case PTRACE_POKEDATA:
293 case PTRACE_POKETEXT:
295 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
300 ret = getreg32(child, addr, &val);
302 ret = put_user(val, (__u32 __user *)datap);
306 ret = putreg32(child, addr, data);
309 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
311 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
316 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
317 getreg32(child, i, &val);
318 ret |= __put_user(val,(u32 __user *)datap);
319 datap += sizeof(u32);
324 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
327 if (!access_ok(VERIFY_READ, datap, 16*4)) {
332 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
333 ret |= __get_user(tmp, (u32 __user *)datap);
334 putreg32(child, i, tmp);
335 datap += sizeof(u32);
340 case PTRACE_GETFPREGS:
342 if (!access_ok(VERIFY_READ, compat_ptr(data),
343 sizeof(struct user_i387_struct)))
345 save_i387_ia32(child, datap, childregs, 1);
349 case PTRACE_SETFPREGS:
351 if (!access_ok(VERIFY_WRITE, datap,
352 sizeof(struct user_i387_struct)))
355 /* don't check EFAULT to be bug-to-bug compatible to i386 */
356 restore_i387_ia32(child, datap, 1);
359 case PTRACE_GETFPXREGS: {
360 struct user32_fxsr_struct __user *u = datap;
363 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
366 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
368 ret = __put_user(childregs->cs, &u->fcs);
369 ret |= __put_user(child->thread.ds, &u->fos);
372 case PTRACE_SETFPXREGS: {
373 struct user32_fxsr_struct __user *u = datap;
376 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
378 /* no checking to be bug-to-bug compatible with i386. */
379 /* but silence warning */
380 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
382 set_stopped_child_used_math(child);
383 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
388 case PTRACE_GETEVENTMSG:
389 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
397 put_task_struct(child);