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>
33 * Determines which flags the user has access to [1 = access, 0 = no access].
34 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
35 * Also masks reserved bits (31-22, 15, 5, 3, 1).
37 #define FLAG_MASK 0x54dd5UL
40 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
42 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
45 __u64 *stack = (__u64 *)task_pt_regs(child);
48 case offsetof(struct user32, regs.fs):
49 if (val && (val & 3) != 3) return -EIO;
50 child->thread.fsindex = val & 0xffff;
52 case offsetof(struct user32, regs.gs):
53 if (val && (val & 3) != 3) return -EIO;
54 child->thread.gsindex = val & 0xffff;
56 case offsetof(struct user32, regs.ds):
57 if (val && (val & 3) != 3) return -EIO;
58 child->thread.ds = val & 0xffff;
60 case offsetof(struct user32, regs.es):
61 child->thread.es = val & 0xffff;
63 case offsetof(struct user32, regs.ss):
64 if ((val & 3) != 3) return -EIO;
65 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
67 case offsetof(struct user32, regs.cs):
68 if ((val & 3) != 3) return -EIO;
69 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
79 R32(orig_eax, orig_rax);
83 case offsetof(struct user32, regs.eflags): {
84 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
86 *flags = val | (*flags & ~FLAG_MASK);
90 case offsetof(struct user32, u_debugreg[4]):
91 case offsetof(struct user32, u_debugreg[5]):
94 case offsetof(struct user32, u_debugreg[0]):
95 child->thread.debugreg0 = val;
98 case offsetof(struct user32, u_debugreg[1]):
99 child->thread.debugreg1 = val;
102 case offsetof(struct user32, u_debugreg[2]):
103 child->thread.debugreg2 = val;
106 case offsetof(struct user32, u_debugreg[3]):
107 child->thread.debugreg3 = val;
110 case offsetof(struct user32, u_debugreg[6]):
111 child->thread.debugreg6 = val;
114 case offsetof(struct user32, u_debugreg[7]):
115 val &= ~DR_CONTROL_RESERVED;
116 /* See arch/i386/kernel/ptrace.c for an explanation of
117 * this awkward check.*/
119 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
121 child->thread.debugreg7 = val;
125 if (regno > sizeof(struct user32) || (regno & 3))
128 /* Other dummy fields in the virtual user structure are ignored */
137 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
139 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
141 __u64 *stack = (__u64 *)task_pt_regs(child);
144 case offsetof(struct user32, regs.fs):
145 *val = child->thread.fsindex;
147 case offsetof(struct user32, regs.gs):
148 *val = child->thread.gsindex;
150 case offsetof(struct user32, regs.ds):
151 *val = child->thread.ds;
153 case offsetof(struct user32, regs.es):
154 *val = child->thread.es;
166 R32(orig_eax, orig_rax);
171 case offsetof(struct user32, u_debugreg[0]):
172 *val = child->thread.debugreg0;
174 case offsetof(struct user32, u_debugreg[1]):
175 *val = child->thread.debugreg1;
177 case offsetof(struct user32, u_debugreg[2]):
178 *val = child->thread.debugreg2;
180 case offsetof(struct user32, u_debugreg[3]):
181 *val = child->thread.debugreg3;
183 case offsetof(struct user32, u_debugreg[6]):
184 *val = child->thread.debugreg6;
186 case offsetof(struct user32, u_debugreg[7]):
187 *val = child->thread.debugreg7;
191 if (regno > sizeof(struct user32) || (regno & 3))
194 /* Other dummy fields in the virtual user structure are ignored */
203 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
206 compat_siginfo_t *si32 = (compat_siginfo_t *)compat_ptr(data);
207 siginfo_t *si = compat_alloc_user_space(sizeof(siginfo_t));
208 if (request == PTRACE_SETSIGINFO) {
209 ret = copy_siginfo_from_user32(si, si32);
213 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
216 if (request == PTRACE_GETSIGINFO)
217 ret = copy_siginfo_to_user32(si32, si);
221 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
223 struct task_struct *child;
224 struct pt_regs *childregs;
225 void __user *datap = compat_ptr(data);
234 case PTRACE_SINGLESTEP:
237 case PTRACE_SETOPTIONS:
238 return sys_ptrace(request, pid, addr, data);
243 case PTRACE_PEEKTEXT:
244 case PTRACE_PEEKDATA:
245 case PTRACE_POKEDATA:
246 case PTRACE_POKETEXT:
251 case PTRACE_SETFPREGS:
252 case PTRACE_GETFPREGS:
253 case PTRACE_SETFPXREGS:
254 case PTRACE_GETFPXREGS:
255 case PTRACE_GETEVENTMSG:
258 case PTRACE_SETSIGINFO:
259 case PTRACE_GETSIGINFO:
260 return ptrace32_siginfo(request, pid, addr, data);
263 child = ptrace_get_task_struct(pid);
265 return PTR_ERR(child);
267 ret = ptrace_check_attach(child, request == PTRACE_KILL);
271 childregs = task_pt_regs(child);
274 case PTRACE_PEEKDATA:
275 case PTRACE_PEEKTEXT:
277 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
280 ret = put_user(val, (unsigned int __user *)datap);
283 case PTRACE_POKEDATA:
284 case PTRACE_POKETEXT:
286 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
291 ret = getreg32(child, addr, &val);
293 ret = put_user(val, (__u32 __user *)datap);
297 ret = putreg32(child, addr, data);
300 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
302 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
307 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
308 getreg32(child, i, &val);
309 ret |= __put_user(val,(u32 __user *)datap);
310 datap += sizeof(u32);
315 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
318 if (!access_ok(VERIFY_READ, datap, 16*4)) {
323 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
324 ret |= __get_user(tmp, (u32 __user *)datap);
325 putreg32(child, i, tmp);
326 datap += sizeof(u32);
331 case PTRACE_GETFPREGS:
333 if (!access_ok(VERIFY_READ, compat_ptr(data),
334 sizeof(struct user_i387_struct)))
336 save_i387_ia32(child, datap, childregs, 1);
340 case PTRACE_SETFPREGS:
342 if (!access_ok(VERIFY_WRITE, datap,
343 sizeof(struct user_i387_struct)))
346 /* don't check EFAULT to be bug-to-bug compatible to i386 */
347 restore_i387_ia32(child, datap, 1);
350 case PTRACE_GETFPXREGS: {
351 struct user32_fxsr_struct __user *u = datap;
354 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
357 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
359 ret = __put_user(childregs->cs, &u->fcs);
360 ret |= __put_user(child->thread.ds, &u->fos);
363 case PTRACE_SETFPXREGS: {
364 struct user32_fxsr_struct __user *u = datap;
367 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
369 /* no checking to be bug-to-bug compatible with i386 */
370 __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
371 set_stopped_child_used_math(child);
372 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
377 case PTRACE_GETEVENTMSG:
378 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
386 put_task_struct(child);