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>
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
;
124 if (regno
> sizeof(struct user32
) || (regno
& 3))
127 /* Other dummy fields in the virtual user structure are ignored */
136 case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
138 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
140 __u64
*stack
= (__u64
*)task_pt_regs(child
);
143 case offsetof(struct user32
, regs
.fs
):
144 *val
= child
->thread
.fsindex
;
146 case offsetof(struct user32
, regs
.gs
):
147 *val
= child
->thread
.gsindex
;
149 case offsetof(struct user32
, regs
.ds
):
150 *val
= child
->thread
.ds
;
152 case offsetof(struct user32
, regs
.es
):
153 *val
= child
->thread
.es
;
165 R32(orig_eax
, orig_rax
);
170 case offsetof(struct user32
, u_debugreg
[0]):
171 *val
= child
->thread
.debugreg0
;
173 case offsetof(struct user32
, u_debugreg
[1]):
174 *val
= child
->thread
.debugreg1
;
176 case offsetof(struct user32
, u_debugreg
[2]):
177 *val
= child
->thread
.debugreg2
;
179 case offsetof(struct user32
, u_debugreg
[3]):
180 *val
= child
->thread
.debugreg3
;
182 case offsetof(struct user32
, u_debugreg
[6]):
183 *val
= child
->thread
.debugreg6
;
185 case offsetof(struct user32
, u_debugreg
[7]):
186 *val
= child
->thread
.debugreg7
;
190 if (regno
> sizeof(struct user32
) || (regno
& 3))
193 /* Other dummy fields in the virtual user structure are ignored */
202 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
204 struct task_struct
*child
;
205 struct pt_regs
*childregs
;
206 void __user
*datap
= compat_ptr(data
);
212 return sys_ptrace(request
, pid
, addr
, data
);
214 case PTRACE_PEEKTEXT
:
215 case PTRACE_PEEKDATA
:
216 case PTRACE_POKEDATA
:
217 case PTRACE_POKETEXT
:
222 case PTRACE_SETFPREGS
:
223 case PTRACE_GETFPREGS
:
224 case PTRACE_SETFPXREGS
:
225 case PTRACE_GETFPXREGS
:
226 case PTRACE_GETEVENTMSG
:
230 if (request
== PTRACE_TRACEME
)
231 return ptrace_traceme();
233 child
= ptrace_get_task_struct(pid
);
235 return PTR_ERR(child
);
237 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
241 childregs
= task_pt_regs(child
);
244 case PTRACE_PEEKDATA
:
245 case PTRACE_PEEKTEXT
:
247 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0)!=sizeof(u32
))
250 ret
= put_user(val
, (unsigned int __user
*)datap
);
253 case PTRACE_POKEDATA
:
254 case PTRACE_POKETEXT
:
256 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1)!=sizeof(u32
))
261 ret
= getreg32(child
, addr
, &val
);
263 ret
= put_user(val
, (__u32 __user
*)datap
);
267 ret
= putreg32(child
, addr
, data
);
270 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
272 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
277 for ( i
= 0; i
<= 16*4 ; i
+= sizeof(__u32
) ) {
278 getreg32(child
, i
, &val
);
279 ret
|= __put_user(val
,(u32 __user
*)datap
);
280 datap
+= sizeof(u32
);
285 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
288 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
293 for ( i
= 0; i
<= 16*4; i
+= sizeof(u32
) ) {
294 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
295 putreg32(child
, i
, tmp
);
296 datap
+= sizeof(u32
);
301 case PTRACE_GETFPREGS
:
303 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
304 sizeof(struct user_i387_struct
)))
306 save_i387_ia32(child
, datap
, childregs
, 1);
310 case PTRACE_SETFPREGS
:
312 if (!access_ok(VERIFY_WRITE
, datap
,
313 sizeof(struct user_i387_struct
)))
316 /* don't check EFAULT to be bug-to-bug compatible to i386 */
317 restore_i387_ia32(child
, datap
, 1);
320 case PTRACE_GETFPXREGS
: {
321 struct user32_fxsr_struct __user
*u
= datap
;
324 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
327 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
329 ret
= __put_user(childregs
->cs
, &u
->fcs
);
330 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
333 case PTRACE_SETFPXREGS
: {
334 struct user32_fxsr_struct __user
*u
= datap
;
337 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
339 /* no checking to be bug-to-bug compatible with i386 */
340 __copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
));
341 set_stopped_child_used_math(child
);
342 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
347 case PTRACE_GETEVENTMSG
:
348 ret
= put_user(child
->ptrace_message
,(unsigned int __user
*)compat_ptr(data
));
357 put_task_struct(child
);