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
9 * see the extended register contents.
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): \
40 stack[offsetof(struct pt_regs, q) / 8] = val; break
42 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)
50 child
->thread
.fsindex
= val
& 0xffff;
52 case offsetof(struct user32
, regs
.gs
):
53 if (val
&& (val
& 3) != 3)
55 child
->thread
.gsindex
= val
& 0xffff;
57 case offsetof(struct user32
, regs
.ds
):
58 if (val
&& (val
& 3) != 3)
60 child
->thread
.ds
= val
& 0xffff;
62 case offsetof(struct user32
, regs
.es
):
63 child
->thread
.es
= val
& 0xffff;
65 case offsetof(struct user32
, regs
.ss
):
68 stack
[offsetof(struct pt_regs
, ss
)/8] = val
& 0xffff;
70 case offsetof(struct user32
, regs
.cs
):
73 stack
[offsetof(struct pt_regs
, cs
)/8] = val
& 0xffff;
83 R32(orig_eax
, orig_ax
);
87 case offsetof(struct user32
, regs
.eflags
): {
88 __u64
*flags
= &stack
[offsetof(struct pt_regs
, flags
)/8];
92 * If the user value contains TF, mark that
93 * it was not "us" (the debugger) that set it.
94 * If not, make sure it stays set if we had.
96 if (val
& X86_EFLAGS_TF
)
97 clear_tsk_thread_flag(child
, TIF_FORCED_TF
);
98 else if (test_tsk_thread_flag(child
, TIF_FORCED_TF
))
100 *flags
= val
| (*flags
& ~FLAG_MASK
);
104 case offsetof(struct user32
, u_debugreg
[0]) ...
105 offsetof(struct user32
, u_debugreg
[7]):
106 regno
-= offsetof(struct user32
, u_debugreg
[0]);
107 return ptrace_set_debugreg(child
, regno
/ 4, val
);
110 if (regno
> sizeof(struct user32
) || (regno
& 3))
114 * Other dummy fields in the virtual user structure
125 case offsetof(struct user32, regs.l): \
126 *val = stack[offsetof(struct pt_regs, q)/8]; break
128 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
130 __u64
*stack
= (__u64
*)task_pt_regs(child
);
133 case offsetof(struct user32
, regs
.fs
):
134 *val
= child
->thread
.fsindex
;
136 case offsetof(struct user32
, regs
.gs
):
137 *val
= child
->thread
.gsindex
;
139 case offsetof(struct user32
, regs
.ds
):
140 *val
= child
->thread
.ds
;
142 case offsetof(struct user32
, regs
.es
):
143 *val
= child
->thread
.es
;
155 R32(orig_eax
, orig_ax
);
159 case offsetof(struct user32
, regs
.eflags
):
161 * If the debugger set TF, hide it from the readout.
163 *val
= stack
[offsetof(struct pt_regs
, flags
)/8];
164 if (test_tsk_thread_flag(child
, TIF_FORCED_TF
))
165 *val
&= ~X86_EFLAGS_TF
;
168 case offsetof(struct user32
, u_debugreg
[0]) ...
169 offsetof(struct user32
, u_debugreg
[7]):
170 regno
-= offsetof(struct user32
, u_debugreg
[0]);
171 *val
= ptrace_get_debugreg(child
, regno
/ 4);
175 if (regno
> sizeof(struct user32
) || (regno
& 3))
179 * Other dummy fields in the virtual user structure
190 static long ptrace32_siginfo(unsigned request
, u32 pid
, u32 addr
, u32 data
)
192 siginfo_t __user
*si
= compat_alloc_user_space(sizeof(siginfo_t
));
193 compat_siginfo_t __user
*si32
= compat_ptr(data
);
197 if (request
== PTRACE_SETSIGINFO
) {
198 memset(&ssi
, 0, sizeof(siginfo_t
));
199 ret
= copy_siginfo_from_user32(&ssi
, si32
);
202 if (copy_to_user(si
, &ssi
, sizeof(siginfo_t
)))
205 ret
= sys_ptrace(request
, pid
, addr
, (unsigned long)si
);
208 if (request
== PTRACE_GETSIGINFO
) {
209 if (copy_from_user(&ssi
, si
, sizeof(siginfo_t
)))
211 ret
= copy_siginfo_to_user32(si32
, &ssi
);
216 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
218 struct task_struct
*child
;
219 struct pt_regs
*childregs
;
220 void __user
*datap
= compat_ptr(data
);
229 case PTRACE_SINGLESTEP
:
230 case PTRACE_SINGLEBLOCK
:
233 case PTRACE_OLDSETOPTIONS
:
234 case PTRACE_SETOPTIONS
:
235 case PTRACE_SET_THREAD_AREA
:
236 case PTRACE_GET_THREAD_AREA
:
237 return sys_ptrace(request
, pid
, addr
, data
);
242 case PTRACE_PEEKTEXT
:
243 case PTRACE_PEEKDATA
:
244 case PTRACE_POKEDATA
:
245 case PTRACE_POKETEXT
:
250 case PTRACE_SETFPREGS
:
251 case PTRACE_GETFPREGS
:
252 case PTRACE_SETFPXREGS
:
253 case PTRACE_GETFPXREGS
:
254 case PTRACE_GETEVENTMSG
:
257 case PTRACE_SETSIGINFO
:
258 case PTRACE_GETSIGINFO
:
259 return ptrace32_siginfo(request
, pid
, addr
, data
);
262 child
= ptrace_get_task_struct(pid
);
264 return PTR_ERR(child
);
266 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
270 childregs
= task_pt_regs(child
);
273 case PTRACE_PEEKDATA
:
274 case PTRACE_PEEKTEXT
:
276 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0) !=
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) !=
292 ret
= getreg32(child
, addr
, &val
);
294 ret
= put_user(val
, (__u32 __user
*)datap
);
298 ret
= putreg32(child
, addr
, data
);
301 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
304 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
309 for (i
= 0; i
<= 16*4; i
+= sizeof(__u32
)) {
310 getreg32(child
, i
, &val
);
311 ret
|= __put_user(val
, (u32 __user
*)datap
);
312 datap
+= sizeof(u32
);
317 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
321 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
326 for (i
= 0; i
<= 16*4; i
+= sizeof(u32
)) {
327 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
328 putreg32(child
, i
, tmp
);
329 datap
+= sizeof(u32
);
334 case PTRACE_GETFPREGS
:
336 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
337 sizeof(struct user_i387_struct
)))
339 save_i387_ia32(child
, datap
, childregs
, 1);
343 case PTRACE_SETFPREGS
:
345 if (!access_ok(VERIFY_WRITE
, datap
,
346 sizeof(struct user_i387_struct
)))
349 /* don't check EFAULT to be bug-to-bug compatible to i386 */
350 restore_i387_ia32(child
, datap
, 1);
353 case PTRACE_GETFPXREGS
: {
354 struct user32_fxsr_struct __user
*u
= datap
;
358 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
361 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
363 ret
= __put_user(childregs
->cs
, &u
->fcs
);
364 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
367 case PTRACE_SETFPXREGS
: {
368 struct user32_fxsr_struct __user
*u
= datap
;
372 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
375 * no checking to be bug-to-bug compatible with i386.
376 * but silence warning
378 if (__copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
)))
380 set_stopped_child_used_math(child
);
381 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
386 case PTRACE_GETEVENTMSG
:
387 ret
= put_user(child
->ptrace_message
,
388 (unsigned int __user
*)compat_ptr(data
));
396 put_task_struct(child
);