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/err.h>
19 #include <linux/ptrace.h>
20 #include <asm/ptrace.h>
21 #include <asm/compat.h>
22 #include <asm/uaccess.h>
23 #include <asm/user32.h>
25 #include <asm/errno.h>
26 #include <asm/debugreg.h>
28 #include <asm/fpu32.h>
32 * Determines which flags the user has access to [1 = access, 0 = no access].
33 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
34 * Also masks reserved bits (31-22, 15, 5, 3, 1).
36 #define FLAG_MASK 0x54dd5UL
39 case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break
41 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
44 __u64 *stack = (__u64 *)task_pt_regs(child);
47 case offsetof(struct user32, regs.fs):
48 if (val && (val & 3) != 3) return -EIO;
49 child->thread.fsindex = val & 0xffff;
51 case offsetof(struct user32, regs.gs):
52 if (val && (val & 3) != 3) return -EIO;
53 child->thread.gsindex = val & 0xffff;
55 case offsetof(struct user32, regs.ds):
56 if (val && (val & 3) != 3) return -EIO;
57 child->thread.ds = val & 0xffff;
59 case offsetof(struct user32, regs.es):
60 child->thread.es = val & 0xffff;
62 case offsetof(struct user32, regs.ss):
63 if ((val & 3) != 3) return -EIO;
64 stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
66 case offsetof(struct user32, regs.cs):
67 if ((val & 3) != 3) return -EIO;
68 stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
78 R32(orig_eax, orig_rax);
82 case offsetof(struct user32, regs.eflags): {
83 __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
85 *flags = val | (*flags & ~FLAG_MASK);
89 case offsetof(struct user32, u_debugreg[4]):
90 case offsetof(struct user32, u_debugreg[5]):
93 case offsetof(struct user32, u_debugreg[0]):
94 child->thread.debugreg0 = val;
97 case offsetof(struct user32, u_debugreg[1]):
98 child->thread.debugreg1 = val;
101 case offsetof(struct user32, u_debugreg[2]):
102 child->thread.debugreg2 = val;
105 case offsetof(struct user32, u_debugreg[3]):
106 child->thread.debugreg3 = val;
109 case offsetof(struct user32, u_debugreg[6]):
110 child->thread.debugreg6 = val;
113 case offsetof(struct user32, u_debugreg[7]):
114 val &= ~DR_CONTROL_RESERVED;
115 /* See arch/i386/kernel/ptrace.c for an explanation of
116 * this awkward check.*/
118 if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
120 child->thread.debugreg7 = val;
122 set_tsk_thread_flag(child, TIF_DEBUG);
124 clear_tsk_thread_flag(child, TIF_DEBUG);
128 if (regno > sizeof(struct user32) || (regno & 3))
131 /* Other dummy fields in the virtual user structure are ignored */
140 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
142 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
144 __u64 *stack = (__u64 *)task_pt_regs(child);
147 case offsetof(struct user32, regs.fs):
148 *val = child->thread.fsindex;
150 case offsetof(struct user32, regs.gs):
151 *val = child->thread.gsindex;
153 case offsetof(struct user32, regs.ds):
154 *val = child->thread.ds;
156 case offsetof(struct user32, regs.es):
157 *val = child->thread.es;
169 R32(orig_eax, orig_rax);
174 case offsetof(struct user32, u_debugreg[0]):
175 *val = child->thread.debugreg0;
177 case offsetof(struct user32, u_debugreg[1]):
178 *val = child->thread.debugreg1;
180 case offsetof(struct user32, u_debugreg[2]):
181 *val = child->thread.debugreg2;
183 case offsetof(struct user32, u_debugreg[3]):
184 *val = child->thread.debugreg3;
186 case offsetof(struct user32, u_debugreg[6]):
187 *val = child->thread.debugreg6;
189 case offsetof(struct user32, u_debugreg[7]):
190 *val = child->thread.debugreg7;
194 if (regno > sizeof(struct user32) || (regno & 3))
197 /* Other dummy fields in the virtual user structure are ignored */
206 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
209 compat_siginfo_t __user *si32 = compat_ptr(data);
211 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
212 if (request == PTRACE_SETSIGINFO) {
213 memset(&ssi, 0, sizeof(siginfo_t));
214 ret = copy_siginfo_from_user32(&ssi, si32);
217 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
220 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
223 if (request == PTRACE_GETSIGINFO) {
224 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
226 ret = copy_siginfo_to_user32(si32, &ssi);
231 #define COMPAT_GDT_ENTRY_TLS_MIN 6
233 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
235 struct task_struct *child;
236 struct pt_regs *childregs;
237 void __user *datap = compat_ptr(data);
246 case PTRACE_SINGLESTEP:
249 case PTRACE_OLDSETOPTIONS:
250 case PTRACE_SETOPTIONS:
251 return sys_ptrace(request, pid, addr, data);
256 case PTRACE_PEEKTEXT:
257 case PTRACE_PEEKDATA:
258 case PTRACE_POKEDATA:
259 case PTRACE_POKETEXT:
264 case PTRACE_SETFPREGS:
265 case PTRACE_GETFPREGS:
266 case PTRACE_SETFPXREGS:
267 case PTRACE_GETFPXREGS:
268 case PTRACE_GETEVENTMSG:
271 case PTRACE_SETSIGINFO:
272 case PTRACE_GETSIGINFO:
273 return ptrace32_siginfo(request, pid, addr, data);
275 case PTRACE_SET_THREAD_AREA:
276 case PTRACE_GET_THREAD_AREA:
277 return sys_ptrace(request, pid,
278 addr + GDT_ENTRY_TLS_MIN - COMPAT_GDT_ENTRY_TLS_MIN,
282 child = ptrace_get_task_struct(pid);
284 return PTR_ERR(child);
286 ret = ptrace_check_attach(child, request == PTRACE_KILL);
290 childregs = task_pt_regs(child);
293 case PTRACE_PEEKDATA:
294 case PTRACE_PEEKTEXT:
296 if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
299 ret = put_user(val, (unsigned int __user *)datap);
302 case PTRACE_POKEDATA:
303 case PTRACE_POKETEXT:
305 if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
310 ret = getreg32(child, addr, &val);
312 ret = put_user(val, (__u32 __user *)datap);
316 ret = putreg32(child, addr, data);
319 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
321 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
326 for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
327 getreg32(child, i, &val);
328 ret |= __put_user(val,(u32 __user *)datap);
329 datap += sizeof(u32);
334 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
337 if (!access_ok(VERIFY_READ, datap, 16*4)) {
342 for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
343 ret |= __get_user(tmp, (u32 __user *)datap);
344 putreg32(child, i, tmp);
345 datap += sizeof(u32);
350 case PTRACE_GETFPREGS:
352 if (!access_ok(VERIFY_READ, compat_ptr(data),
353 sizeof(struct user_i387_struct)))
355 save_i387_ia32(child, datap, childregs, 1);
359 case PTRACE_SETFPREGS:
361 if (!access_ok(VERIFY_WRITE, datap,
362 sizeof(struct user_i387_struct)))
365 /* don't check EFAULT to be bug-to-bug compatible to i386 */
366 restore_i387_ia32(child, datap, 1);
369 case PTRACE_GETFPXREGS: {
370 struct user32_fxsr_struct __user *u = datap;
373 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
376 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
378 ret = __put_user(childregs->cs, &u->fcs);
379 ret |= __put_user(child->thread.ds, &u->fos);
382 case PTRACE_SETFPXREGS: {
383 struct user32_fxsr_struct __user *u = datap;
386 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
388 /* no checking to be bug-to-bug compatible with i386. */
389 /* but silence warning */
390 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
392 set_stopped_child_used_math(child);
393 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
398 case PTRACE_GETEVENTMSG:
399 ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
407 put_task_struct(child);