2 * Kernel support for the ptrace() and syscall tracing interfaces.
4 * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
5 * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
6 * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
7 * Copyright (C) 2008-2016 Helge Deller <deller@gmx.de>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/elf.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/personality.h>
20 #include <linux/regset.h>
21 #include <linux/security.h>
22 #include <linux/seccomp.h>
23 #include <linux/compat.h>
24 #include <linux/signal.h>
25 #include <linux/audit.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/processor.h>
30 #include <asm/asm-offsets.h>
32 /* PSW bits we allow the debugger to modify */
33 #define USER_PSW_BITS (PSW_N | PSW_B | PSW_V | PSW_CB)
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/syscalls.h>
39 * These are our native regset flavors.
47 * Called by kernel/ptrace.c when detaching..
49 * Make sure single step bits etc are not set.
51 void ptrace_disable(struct task_struct *task)
53 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
54 clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
56 /* make sure the trap bits are not set */
64 * The following functions are called by ptrace_resume() when
65 * enabling or disabling single/block tracing.
67 void user_disable_single_step(struct task_struct *task)
72 void user_enable_single_step(struct task_struct *task)
74 clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
75 set_tsk_thread_flag(task, TIF_SINGLESTEP);
77 if (pa_psw(task)->n) {
80 /* Nullified, just crank over the queue. */
81 task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1];
82 task_regs(task)->iasq[0] = task_regs(task)->iasq[1];
83 task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4;
90 /* Don't wake up the task, but let the
91 parent know something happened. */
92 si.si_code = TRAP_TRACE;
93 si.si_addr = (void __user *) (task_regs(task)->iaoq[0] & ~3);
94 si.si_signo = SIGTRAP;
96 force_sig_info(SIGTRAP, &si, task);
97 /* notify_parent(task, SIGCHLD); */
101 /* Enable recovery counter traps. The recovery counter
102 * itself will be set to zero on a task switch. If the
103 * task is suspended on a syscall then the syscall return
104 * path will overwrite the recovery counter with a suitable
105 * value such that it traps once back in user space. We
106 * disable interrupts in the tasks PSW here also, to avoid
107 * interrupts while the recovery counter is decrementing.
115 void user_enable_block_step(struct task_struct *task)
117 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
118 set_tsk_thread_flag(task, TIF_BLOCKSTEP);
120 /* Enable taken branch trap. */
127 long arch_ptrace(struct task_struct *child, long request,
128 unsigned long addr, unsigned long data)
130 unsigned long __user *datap = (unsigned long __user *)data;
136 /* Read the word at location addr in the USER area. For ptraced
137 processes, the kernel saves all regs on a syscall. */
139 if ((addr & (sizeof(unsigned long)-1)) ||
140 addr >= sizeof(struct pt_regs))
142 tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
143 ret = put_user(tmp, datap);
146 /* Write the word at location addr in the USER area. This will need
147 to change when the kernel no longer saves all regs on a syscall.
148 FIXME. There is a problem at the moment in that r3-r18 are only
149 saved if the process is ptraced on syscall entry, and even then
150 those values are overwritten by actual register values on syscall
153 /* Some register values written here may be ignored in
154 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
155 * r31/r31+4, and not with the values in pt_regs.
157 if (addr == PT_PSW) {
158 /* Allow writing to Nullify, Divide-step-correction,
159 * and carry/borrow bits.
160 * BEWARE, if you set N, and then single step, it won't
161 * stop on the nullified instruction.
163 data &= USER_PSW_BITS;
164 task_regs(child)->gr[0] &= ~USER_PSW_BITS;
165 task_regs(child)->gr[0] |= data;
170 if ((addr & (sizeof(unsigned long)-1)) ||
171 addr >= sizeof(struct pt_regs))
173 if (addr == PT_IAOQ0 || addr == PT_IAOQ1) {
174 data |= 3; /* ensure userspace privilege */
176 if ((addr >= PT_GR1 && addr <= PT_GR31) ||
177 addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
178 (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
180 *(unsigned long *) ((char *) task_regs(child) + addr) = data;
185 case PTRACE_GETREGS: /* Get all gp regs from the child. */
186 return copy_regset_to_user(child,
187 task_user_regset_view(current),
189 0, sizeof(struct user_regs_struct),
192 case PTRACE_SETREGS: /* Set all gp regs in the child. */
193 return copy_regset_from_user(child,
194 task_user_regset_view(current),
196 0, sizeof(struct user_regs_struct),
199 case PTRACE_GETFPREGS: /* Get the child FPU state. */
200 return copy_regset_to_user(child,
201 task_user_regset_view(current),
203 0, sizeof(struct user_fp_struct),
206 case PTRACE_SETFPREGS: /* Set the child FPU state. */
207 return copy_regset_from_user(child,
208 task_user_regset_view(current),
210 0, sizeof(struct user_fp_struct),
214 ret = ptrace_request(child, request, addr, data);
224 /* This function is needed to translate 32 bit pt_regs offsets in to
225 * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
226 * will request offset 12 if it wants gr3, but the lower 32 bits of
227 * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
228 * This code relies on a 32 bit pt_regs being comprised of 32 bit values
229 * except for the fp registers which (a) are 64 bits, and (b) follow
230 * the gr registers at the start of pt_regs. The 32 bit pt_regs should
231 * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
232 * being 64 bit in both cases.
235 static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
239 if (offset < 32*4) /* gr[0..31] */
240 pos = offset * 2 + 4;
241 else if (offset < 32*4+32*8) /* fr[0] ... fr[31] */
242 pos = (offset - 32*4) + PT_FR0;
243 else if (offset < sizeof(struct pt_regs)/2 + 32*4) /* sr[0] ... ipsw */
244 pos = (offset - 32*4 - 32*8) * 2 + PT_SR0 + 4;
246 pos = sizeof(struct pt_regs);
251 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
252 compat_ulong_t addr, compat_ulong_t data)
260 if (addr & (sizeof(compat_uint_t)-1))
262 addr = translate_usr_offset(addr);
263 if (addr >= sizeof(struct pt_regs))
266 tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
267 ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
270 /* Write the word at location addr in the USER area. This will need
271 to change when the kernel no longer saves all regs on a syscall.
272 FIXME. There is a problem at the moment in that r3-r18 are only
273 saved if the process is ptraced on syscall entry, and even then
274 those values are overwritten by actual register values on syscall
277 /* Some register values written here may be ignored in
278 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
279 * r31/r31+4, and not with the values in pt_regs.
281 if (addr == PT_PSW) {
282 /* Since PT_PSW==0, it is valid for 32 bit processes
283 * under 64 bit kernels as well.
285 ret = arch_ptrace(child, request, addr, data);
287 if (addr & (sizeof(compat_uint_t)-1))
289 addr = translate_usr_offset(addr);
290 if (addr >= sizeof(struct pt_regs))
292 if (addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4) {
293 data |= 3; /* ensure userspace privilege */
295 if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
296 /* Special case, fp regs are 64 bits anyway */
297 *(__u32 *) ((char *) task_regs(child) + addr) = data;
300 else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
301 addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
303 /* Zero the top 32 bits */
304 *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
305 *(__u32 *) ((char *) task_regs(child) + addr) = data;
312 ret = compat_ptrace_request(child, request, addr, data);
320 long do_syscall_trace_enter(struct pt_regs *regs)
322 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
323 int rc = tracehook_report_syscall_entry(regs);
326 * As tracesys_next does not set %r28 to -ENOSYS
327 * when %r20 is set to -1, initialize it here.
329 regs->gr[28] = -ENOSYS;
333 * A nonzero return code from
334 * tracehook_report_syscall_entry() tells us
335 * to prevent the syscall execution. Skip
336 * the syscall call and the syscall restart handling.
338 * Note that the tracer may also just change
339 * regs->gr[20] to an invalid syscall number,
340 * that is handled by tracesys_next.
347 /* Do the secure computing check after ptrace. */
348 if (secure_computing(NULL) == -1)
351 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
352 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
353 trace_sys_enter(regs, regs->gr[20]);
357 if (!is_compat_task())
358 audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25],
359 regs->gr[24], regs->gr[23]);
362 audit_syscall_entry(regs->gr[20] & 0xffffffff,
363 regs->gr[26] & 0xffffffff,
364 regs->gr[25] & 0xffffffff,
365 regs->gr[24] & 0xffffffff,
366 regs->gr[23] & 0xffffffff);
369 * Sign extend the syscall number to 64bit since it may have been
370 * modified by a compat ptrace call
372 return (int) ((u32) regs->gr[20]);
375 void do_syscall_trace_exit(struct pt_regs *regs)
377 int stepping = test_thread_flag(TIF_SINGLESTEP) ||
378 test_thread_flag(TIF_BLOCKSTEP);
380 audit_syscall_exit(regs);
382 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
383 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
384 trace_sys_exit(regs, regs->gr[20]);
387 if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
388 tracehook_report_syscall_exit(regs, stepping);
396 static int fpr_get(struct task_struct *target,
397 const struct user_regset *regset,
398 unsigned int pos, unsigned int count,
399 void *kbuf, void __user *ubuf)
401 struct pt_regs *regs = task_regs(target);
403 __u64 __user *u = ubuf;
407 count /= sizeof(reg);
410 for (; count > 0 && pos < ELF_NFPREG; --count)
411 *k++ = regs->fr[pos++];
413 for (; count > 0 && pos < ELF_NFPREG; --count)
414 if (__put_user(regs->fr[pos++], u++))
420 count *= sizeof(reg);
421 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
422 ELF_NFPREG * sizeof(reg), -1);
425 static int fpr_set(struct task_struct *target,
426 const struct user_regset *regset,
427 unsigned int pos, unsigned int count,
428 const void *kbuf, const void __user *ubuf)
430 struct pt_regs *regs = task_regs(target);
431 const __u64 *k = kbuf;
432 const __u64 __user *u = ubuf;
436 count /= sizeof(reg);
439 for (; count > 0 && pos < ELF_NFPREG; --count)
440 regs->fr[pos++] = *k++;
442 for (; count > 0 && pos < ELF_NFPREG; --count) {
443 if (__get_user(reg, u++))
445 regs->fr[pos++] = reg;
451 count *= sizeof(reg);
452 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
453 ELF_NFPREG * sizeof(reg), -1);
456 #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long))
458 static unsigned long get_reg(struct pt_regs *regs, int num)
461 case RI(gr[0]) ... RI(gr[31]): return regs->gr[num - RI(gr[0])];
462 case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
463 case RI(iasq[0]): return regs->iasq[0];
464 case RI(iasq[1]): return regs->iasq[1];
465 case RI(iaoq[0]): return regs->iaoq[0];
466 case RI(iaoq[1]): return regs->iaoq[1];
467 case RI(sar): return regs->sar;
468 case RI(iir): return regs->iir;
469 case RI(isr): return regs->isr;
470 case RI(ior): return regs->ior;
471 case RI(ipsw): return regs->ipsw;
472 case RI(cr27): return regs->cr27;
473 case RI(cr0): return mfctl(0);
474 case RI(cr24): return mfctl(24);
475 case RI(cr25): return mfctl(25);
476 case RI(cr26): return mfctl(26);
477 case RI(cr28): return mfctl(28);
478 case RI(cr29): return mfctl(29);
479 case RI(cr30): return mfctl(30);
480 case RI(cr31): return mfctl(31);
481 case RI(cr8): return mfctl(8);
482 case RI(cr9): return mfctl(9);
483 case RI(cr12): return mfctl(12);
484 case RI(cr13): return mfctl(13);
485 case RI(cr10): return mfctl(10);
486 case RI(cr15): return mfctl(15);
491 static void set_reg(struct pt_regs *regs, int num, unsigned long val)
496 * Allow writing to Nullify, Divide-step-correction,
497 * and carry/borrow bits.
498 * BEWARE, if you set N, and then single step, it won't
499 * stop on the nullified instruction.
501 val &= USER_PSW_BITS;
502 regs->gr[0] &= ~USER_PSW_BITS;
505 case RI(gr[1]) ... RI(gr[31]):
506 regs->gr[num - RI(gr[0])] = val;
510 /* set 2 lowest bits to ensure userspace privilege: */
511 regs->iaoq[num - RI(iaoq[0])] = val | 3;
513 case RI(sar): regs->sar = val;
517 /* do not allow to change any of the following registers (yet) */
518 case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])];
519 case RI(iasq[0]): return regs->iasq[0];
520 case RI(iasq[1]): return regs->iasq[1];
521 case RI(iir): return regs->iir;
522 case RI(isr): return regs->isr;
523 case RI(ior): return regs->ior;
524 case RI(ipsw): return regs->ipsw;
525 case RI(cr27): return regs->cr27;
526 case cr0, cr24, cr25, cr26, cr27, cr28, cr29, cr30, cr31;
527 case cr8, cr9, cr12, cr13, cr10, cr15;
532 static int gpr_get(struct task_struct *target,
533 const struct user_regset *regset,
534 unsigned int pos, unsigned int count,
535 void *kbuf, void __user *ubuf)
537 struct pt_regs *regs = task_regs(target);
538 unsigned long *k = kbuf;
539 unsigned long __user *u = ubuf;
543 count /= sizeof(reg);
546 for (; count > 0 && pos < ELF_NGREG; --count)
547 *k++ = get_reg(regs, pos++);
549 for (; count > 0 && pos < ELF_NGREG; --count)
550 if (__put_user(get_reg(regs, pos++), u++))
555 count *= sizeof(reg);
556 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
557 ELF_NGREG * sizeof(reg), -1);
560 static int gpr_set(struct task_struct *target,
561 const struct user_regset *regset,
562 unsigned int pos, unsigned int count,
563 const void *kbuf, const void __user *ubuf)
565 struct pt_regs *regs = task_regs(target);
566 const unsigned long *k = kbuf;
567 const unsigned long __user *u = ubuf;
571 count /= sizeof(reg);
574 for (; count > 0 && pos < ELF_NGREG; --count)
575 set_reg(regs, pos++, *k++);
577 for (; count > 0 && pos < ELF_NGREG; --count) {
578 if (__get_user(reg, u++))
580 set_reg(regs, pos++, reg);
586 count *= sizeof(reg);
587 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
588 ELF_NGREG * sizeof(reg), -1);
591 static const struct user_regset native_regsets[] = {
593 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
594 .size = sizeof(long), .align = sizeof(long),
595 .get = gpr_get, .set = gpr_set
598 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
599 .size = sizeof(__u64), .align = sizeof(__u64),
600 .get = fpr_get, .set = fpr_set
604 static const struct user_regset_view user_parisc_native_view = {
605 .name = "parisc", .e_machine = ELF_ARCH, .ei_osabi = ELFOSABI_LINUX,
606 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
610 #include <linux/compat.h>
612 static int gpr32_get(struct task_struct *target,
613 const struct user_regset *regset,
614 unsigned int pos, unsigned int count,
615 void *kbuf, void __user *ubuf)
617 struct pt_regs *regs = task_regs(target);
618 compat_ulong_t *k = kbuf;
619 compat_ulong_t __user *u = ubuf;
623 count /= sizeof(reg);
626 for (; count > 0 && pos < ELF_NGREG; --count)
627 *k++ = get_reg(regs, pos++);
629 for (; count > 0 && pos < ELF_NGREG; --count)
630 if (__put_user((compat_ulong_t) get_reg(regs, pos++), u++))
636 count *= sizeof(reg);
637 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
638 ELF_NGREG * sizeof(reg), -1);
641 static int gpr32_set(struct task_struct *target,
642 const struct user_regset *regset,
643 unsigned int pos, unsigned int count,
644 const void *kbuf, const void __user *ubuf)
646 struct pt_regs *regs = task_regs(target);
647 const compat_ulong_t *k = kbuf;
648 const compat_ulong_t __user *u = ubuf;
652 count /= sizeof(reg);
655 for (; count > 0 && pos < ELF_NGREG; --count)
656 set_reg(regs, pos++, *k++);
658 for (; count > 0 && pos < ELF_NGREG; --count) {
659 if (__get_user(reg, u++))
661 set_reg(regs, pos++, reg);
667 count *= sizeof(reg);
668 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
669 ELF_NGREG * sizeof(reg), -1);
673 * These are the regset flavors matching the 32bit native set.
675 static const struct user_regset compat_regsets[] = {
677 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
678 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
679 .get = gpr32_get, .set = gpr32_set
682 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
683 .size = sizeof(__u64), .align = sizeof(__u64),
684 .get = fpr_get, .set = fpr_set
688 static const struct user_regset_view user_parisc_compat_view = {
689 .name = "parisc", .e_machine = EM_PARISC, .ei_osabi = ELFOSABI_LINUX,
690 .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
692 #endif /* CONFIG_64BIT */
694 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
696 BUILD_BUG_ON(sizeof(struct user_regs_struct)/sizeof(long) != ELF_NGREG);
697 BUILD_BUG_ON(sizeof(struct user_fp_struct)/sizeof(__u64) != ELF_NFPREG);
699 if (is_compat_task())
700 return &user_parisc_compat_view;
702 return &user_parisc_native_view;