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 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
233 struct task_struct
*child
;
234 struct pt_regs
*childregs
;
235 void __user
*datap
= compat_ptr(data
);
244 case PTRACE_SINGLESTEP
:
247 case PTRACE_OLDSETOPTIONS
:
248 case PTRACE_SETOPTIONS
:
249 case PTRACE_SET_THREAD_AREA
:
250 case PTRACE_GET_THREAD_AREA
:
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
);
276 child
= ptrace_get_task_struct(pid
);
278 return PTR_ERR(child
);
280 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
284 childregs
= task_pt_regs(child
);
287 case PTRACE_PEEKDATA
:
288 case PTRACE_PEEKTEXT
:
290 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0)!=sizeof(u32
))
293 ret
= put_user(val
, (unsigned int __user
*)datap
);
296 case PTRACE_POKEDATA
:
297 case PTRACE_POKETEXT
:
299 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1)!=sizeof(u32
))
304 ret
= getreg32(child
, addr
, &val
);
306 ret
= put_user(val
, (__u32 __user
*)datap
);
310 ret
= putreg32(child
, addr
, data
);
313 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
315 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
320 for ( i
= 0; i
<= 16*4 ; i
+= sizeof(__u32
) ) {
321 getreg32(child
, i
, &val
);
322 ret
|= __put_user(val
,(u32 __user
*)datap
);
323 datap
+= sizeof(u32
);
328 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
331 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
336 for ( i
= 0; i
<= 16*4; i
+= sizeof(u32
) ) {
337 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
338 putreg32(child
, i
, tmp
);
339 datap
+= sizeof(u32
);
344 case PTRACE_GETFPREGS
:
346 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
347 sizeof(struct user_i387_struct
)))
349 save_i387_ia32(child
, datap
, childregs
, 1);
353 case PTRACE_SETFPREGS
:
355 if (!access_ok(VERIFY_WRITE
, datap
,
356 sizeof(struct user_i387_struct
)))
359 /* don't check EFAULT to be bug-to-bug compatible to i386 */
360 restore_i387_ia32(child
, datap
, 1);
363 case PTRACE_GETFPXREGS
: {
364 struct user32_fxsr_struct __user
*u
= datap
;
367 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
370 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
372 ret
= __put_user(childregs
->cs
, &u
->fcs
);
373 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
376 case PTRACE_SETFPXREGS
: {
377 struct user32_fxsr_struct __user
*u
= datap
;
380 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
382 /* no checking to be bug-to-bug compatible with i386. */
383 /* but silence warning */
384 if (__copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
)))
386 set_stopped_child_used_math(child
);
387 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
392 case PTRACE_GETEVENTMSG
:
393 ret
= put_user(child
->ptrace_message
,(unsigned int __user
*)compat_ptr(data
));
401 put_task_struct(child
);