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
.fs
= val
& 0xffff;
48 case offsetof(struct user32
, regs
.gs
):
49 if (val
&& (val
& 3) != 3) return -EIO
;
50 child
->thread
.gs
= 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 /* You are not expected to understand this ... I don't neither. */
114 if ((0x5454 >> ((val
>> (16 + 4*i
)) & 0xf)) & 1)
116 child
->thread
.debugreg7
= val
;
120 if (regno
> sizeof(struct user32
) || (regno
& 3))
123 /* Other dummy fields in the virtual user structure are ignored */
132 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
134 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
136 __u64
*stack
= (__u64
*)(child
->thread
.rsp0
- sizeof(struct pt_regs
));
139 case offsetof(struct user32
, regs
.fs
):
140 *val
= child
->thread
.fs
;
142 case offsetof(struct user32
, regs
.gs
):
143 *val
= child
->thread
.gs
;
145 case offsetof(struct user32
, regs
.ds
):
146 *val
= child
->thread
.ds
;
148 case offsetof(struct user32
, regs
.es
):
149 *val
= child
->thread
.es
;
161 R32(orig_eax
, orig_rax
);
166 case offsetof(struct user32
, u_debugreg
[0]):
167 *val
= child
->thread
.debugreg0
;
169 case offsetof(struct user32
, u_debugreg
[1]):
170 *val
= child
->thread
.debugreg1
;
172 case offsetof(struct user32
, u_debugreg
[2]):
173 *val
= child
->thread
.debugreg2
;
175 case offsetof(struct user32
, u_debugreg
[3]):
176 *val
= child
->thread
.debugreg3
;
178 case offsetof(struct user32
, u_debugreg
[6]):
179 *val
= child
->thread
.debugreg6
;
181 case offsetof(struct user32
, u_debugreg
[7]):
182 *val
= child
->thread
.debugreg7
;
186 if (regno
> sizeof(struct user32
) || (regno
& 3))
189 /* Other dummy fields in the virtual user structure are ignored */
198 static struct task_struct
*find_target(int request
, int pid
, int *err
)
200 struct task_struct
*child
;
207 read_lock(&tasklist_lock
);
208 child
= find_task_by_pid(pid
);
210 get_task_struct(child
);
211 read_unlock(&tasklist_lock
);
216 *err
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
223 put_task_struct(child
);
228 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
230 struct task_struct
*child
;
231 struct pt_regs
*childregs
;
232 void __user
*datap
= compat_ptr(data
);
238 return sys_ptrace(request
, pid
, addr
, data
);
240 case PTRACE_PEEKTEXT
:
241 case PTRACE_PEEKDATA
:
242 case PTRACE_POKEDATA
:
243 case PTRACE_POKETEXT
:
248 case PTRACE_SETFPREGS
:
249 case PTRACE_GETFPREGS
:
250 case PTRACE_SETFPXREGS
:
251 case PTRACE_GETFPXREGS
:
252 case PTRACE_GETEVENTMSG
:
256 child
= find_target(request
, pid
, &ret
);
260 childregs
= (struct pt_regs
*)(child
->thread
.rsp0
- sizeof(struct pt_regs
));
263 case PTRACE_PEEKDATA
:
264 case PTRACE_PEEKTEXT
:
266 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0)!=sizeof(u32
))
269 ret
= put_user(val
, (unsigned int __user
*)datap
);
272 case PTRACE_POKEDATA
:
273 case PTRACE_POKETEXT
:
275 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1)!=sizeof(u32
))
280 ret
= getreg32(child
, addr
, &val
);
282 ret
= put_user(val
, (__u32 __user
*)datap
);
286 ret
= putreg32(child
, addr
, data
);
289 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
291 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
296 for ( i
= 0; i
<= 16*4 ; i
+= sizeof(__u32
) ) {
297 getreg32(child
, i
, &val
);
298 ret
|= __put_user(val
,(u32 __user
*)datap
);
299 datap
+= sizeof(u32
);
304 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
307 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
312 for ( i
= 0; i
<= 16*4; i
+= sizeof(u32
) ) {
313 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
314 putreg32(child
, i
, tmp
);
315 datap
+= sizeof(u32
);
320 case PTRACE_GETFPREGS
:
322 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
323 sizeof(struct user_i387_struct
)))
325 save_i387_ia32(child
, datap
, childregs
, 1);
329 case PTRACE_SETFPREGS
:
331 if (!access_ok(VERIFY_WRITE
, datap
,
332 sizeof(struct user_i387_struct
)))
335 /* don't check EFAULT to be bug-to-bug compatible to i386 */
336 restore_i387_ia32(child
, datap
, 1);
339 case PTRACE_GETFPXREGS
: {
340 struct user32_fxsr_struct __user
*u
= datap
;
343 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
346 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
348 ret
= __put_user(childregs
->cs
, &u
->fcs
);
349 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
352 case PTRACE_SETFPXREGS
: {
353 struct user32_fxsr_struct __user
*u
= datap
;
356 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
358 /* no checking to be bug-to-bug compatible with i386 */
359 __copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
));
360 child
->used_math
= 1;
361 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
366 case PTRACE_GETEVENTMSG
:
367 ret
= put_user(child
->ptrace_message
,(unsigned int __user
*)(u64
)data
);
375 put_task_struct(child
);