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): \
42 static int putreg32(struct task_struct *child, unsigned regno, u32 val)
44 struct pt_regs *regs = task_pt_regs(child);
47 case offsetof(struct user32, regs.fs):
48 if (val && (val & 3) != 3)
50 child->thread.fsindex = val & 0xffff;
52 loadsegment(fs, child->thread.fsindex);
54 case offsetof(struct user32, regs.gs):
55 if (val && (val & 3) != 3)
57 child->thread.gsindex = val & 0xffff;
59 load_gs_index(child->thread.gsindex);
61 case offsetof(struct user32, regs.ds):
62 if (val && (val & 3) != 3)
64 child->thread.ds = val & 0xffff;
66 loadsegment(ds, child->thread.ds);
68 case offsetof(struct user32, regs.es):
69 child->thread.es = val & 0xffff;
71 loadsegment(es, child->thread.ds);
73 case offsetof(struct user32, regs.ss):
76 regs->ss = val & 0xffff;
78 case offsetof(struct user32, regs.cs):
81 regs->cs = val & 0xffff;
91 R32(orig_eax, orig_ax);
95 case offsetof(struct user32, regs.eflags):
98 * If the user value contains TF, mark that
99 * it was not "us" (the debugger) that set it.
100 * If not, make sure it stays set if we had.
102 if (val & X86_EFLAGS_TF)
103 clear_tsk_thread_flag(child, TIF_FORCED_TF);
104 else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
105 val |= X86_EFLAGS_TF;
106 regs->flags = val | (regs->flags & ~FLAG_MASK);
109 case offsetof(struct user32, u_debugreg[0]) ...
110 offsetof(struct user32, u_debugreg[7]):
111 regno -= offsetof(struct user32, u_debugreg[0]);
112 return ptrace_set_debugreg(child, regno / 4, val);
115 if (regno > sizeof(struct user32) || (regno & 3))
119 * Other dummy fields in the virtual user structure
130 case offsetof(struct user32, regs.l): \
131 *val = regs->q; break
133 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
135 struct pt_regs *regs = task_pt_regs(child);
138 case offsetof(struct user32, regs.fs):
139 *val = child->thread.fsindex;
140 if (child == current)
141 asm("movl %%fs,%0" : "=r" (*val));
143 case offsetof(struct user32, regs.gs):
144 *val = child->thread.gsindex;
145 if (child == current)
146 asm("movl %%gs,%0" : "=r" (*val));
148 case offsetof(struct user32, regs.ds):
149 *val = child->thread.ds;
150 if (child == current)
151 asm("movl %%ds,%0" : "=r" (*val));
153 case offsetof(struct user32, regs.es):
154 *val = child->thread.es;
155 if (child == current)
156 asm("movl %%es,%0" : "=r" (*val));
168 R32(orig_eax, orig_ax);
172 case offsetof(struct user32, regs.eflags):
174 * If the debugger set TF, hide it from the readout.
177 if (test_tsk_thread_flag(child, TIF_FORCED_TF))
178 *val &= ~X86_EFLAGS_TF;
181 case offsetof(struct user32, u_debugreg[0]) ...
182 offsetof(struct user32, u_debugreg[7]):
183 regno -= offsetof(struct user32, u_debugreg[0]);
184 *val = ptrace_get_debugreg(child, regno / 4);
188 if (regno > sizeof(struct user32) || (regno & 3))
192 * Other dummy fields in the virtual user structure
203 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
205 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
206 compat_siginfo_t __user *si32 = compat_ptr(data);
210 if (request == PTRACE_SETSIGINFO) {
211 memset(&ssi, 0, sizeof(siginfo_t));
212 ret = copy_siginfo_from_user32(&ssi, si32);
215 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
218 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
221 if (request == PTRACE_GETSIGINFO) {
222 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
224 ret = copy_siginfo_to_user32(si32, &ssi);
229 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
231 struct task_struct *child;
232 struct pt_regs *childregs;
233 void __user *datap = compat_ptr(data);
242 case PTRACE_SINGLESTEP:
243 case PTRACE_SINGLEBLOCK:
246 case PTRACE_OLDSETOPTIONS:
247 case PTRACE_SETOPTIONS:
248 case PTRACE_SET_THREAD_AREA:
249 case PTRACE_GET_THREAD_AREA:
250 return sys_ptrace(request, pid, addr, data);
255 case PTRACE_PEEKTEXT:
256 case PTRACE_PEEKDATA:
257 case PTRACE_POKEDATA:
258 case PTRACE_POKETEXT:
263 case PTRACE_SETFPREGS:
264 case PTRACE_GETFPREGS:
265 case PTRACE_SETFPXREGS:
266 case PTRACE_GETFPXREGS:
267 case PTRACE_GETEVENTMSG:
270 case PTRACE_SETSIGINFO:
271 case PTRACE_GETSIGINFO:
272 return ptrace32_siginfo(request, pid, addr, data);
275 child = ptrace_get_task_struct(pid);
277 return PTR_ERR(child);
279 ret = ptrace_check_attach(child, request == PTRACE_KILL);
283 childregs = task_pt_regs(child);
286 case PTRACE_PEEKDATA:
287 case PTRACE_PEEKTEXT:
289 if (access_process_vm(child, addr, &val, sizeof(u32), 0) !=
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) !=
305 ret = getreg32(child, addr, &val);
307 ret = put_user(val, (__u32 __user *)datap);
311 ret = putreg32(child, addr, data);
314 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
317 if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
322 for (i = 0; i <= 16*4; i += sizeof(__u32)) {
323 getreg32(child, i, &val);
324 ret |= __put_user(val, (u32 __user *)datap);
325 datap += sizeof(u32);
330 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
334 if (!access_ok(VERIFY_READ, datap, 16*4)) {
339 for (i = 0; i <= 16*4; i += sizeof(u32)) {
340 ret |= __get_user(tmp, (u32 __user *)datap);
341 putreg32(child, i, tmp);
342 datap += sizeof(u32);
347 case PTRACE_GETFPREGS:
349 if (!access_ok(VERIFY_READ, compat_ptr(data),
350 sizeof(struct user_i387_struct)))
352 save_i387_ia32(child, datap, childregs, 1);
356 case PTRACE_SETFPREGS:
358 if (!access_ok(VERIFY_WRITE, datap,
359 sizeof(struct user_i387_struct)))
362 /* don't check EFAULT to be bug-to-bug compatible to i386 */
363 restore_i387_ia32(child, datap, 1);
366 case PTRACE_GETFPXREGS: {
367 struct user32_fxsr_struct __user *u = datap;
371 if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
374 if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
376 ret = __put_user(childregs->cs, &u->fcs);
377 ret |= __put_user(child->thread.ds, &u->fos);
380 case PTRACE_SETFPXREGS: {
381 struct user32_fxsr_struct __user *u = datap;
385 if (!access_ok(VERIFY_READ, u, sizeof(*u)))
388 * no checking to be bug-to-bug compatible with i386.
389 * but silence warning
391 if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
393 set_stopped_child_used_math(child);
394 child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
399 case PTRACE_GETEVENTMSG:
400 ret = put_user(child->ptrace_message,
401 (unsigned int __user *)compat_ptr(data));
409 put_task_struct(child);