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_GR1
&& addr
<= PT_GR31
) ||
174 addr
== PT_IAOQ0
|| addr
== PT_IAOQ1
||
175 (addr
>= PT_FR0
&& addr
<= PT_FR31
+ 4) ||
177 *(unsigned long *) ((char *) task_regs(child
) + addr
) = data
;
182 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
183 return copy_regset_to_user(child
,
184 task_user_regset_view(current
),
186 0, sizeof(struct user_regs_struct
),
189 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
190 return copy_regset_from_user(child
,
191 task_user_regset_view(current
),
193 0, sizeof(struct user_regs_struct
),
196 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
197 return copy_regset_to_user(child
,
198 task_user_regset_view(current
),
200 0, sizeof(struct user_fp_struct
),
203 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
204 return copy_regset_from_user(child
,
205 task_user_regset_view(current
),
207 0, sizeof(struct user_fp_struct
),
211 ret
= ptrace_request(child
, request
, addr
, data
);
221 /* This function is needed to translate 32 bit pt_regs offsets in to
222 * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
223 * will request offset 12 if it wants gr3, but the lower 32 bits of
224 * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
225 * This code relies on a 32 bit pt_regs being comprised of 32 bit values
226 * except for the fp registers which (a) are 64 bits, and (b) follow
227 * the gr registers at the start of pt_regs. The 32 bit pt_regs should
228 * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
229 * being 64 bit in both cases.
232 static compat_ulong_t
translate_usr_offset(compat_ulong_t offset
)
235 return sizeof(struct pt_regs
);
236 else if (offset
<= 32*4) /* gr[0..31] */
237 return offset
* 2 + 4;
238 else if (offset
<= 32*4+32*8) /* gr[0..31] + fr[0..31] */
239 return offset
+ 32*4;
240 else if (offset
< sizeof(struct pt_regs
)/2 + 32*4)
241 return offset
* 2 + 4 - 32*8;
243 return sizeof(struct pt_regs
);
246 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
247 compat_ulong_t addr
, compat_ulong_t data
)
255 if (addr
& (sizeof(compat_uint_t
)-1))
257 addr
= translate_usr_offset(addr
);
258 if (addr
>= sizeof(struct pt_regs
))
261 tmp
= *(compat_uint_t
*) ((char *) task_regs(child
) + addr
);
262 ret
= put_user(tmp
, (compat_uint_t
*) (unsigned long) data
);
265 /* Write the word at location addr in the USER area. This will need
266 to change when the kernel no longer saves all regs on a syscall.
267 FIXME. There is a problem at the moment in that r3-r18 are only
268 saved if the process is ptraced on syscall entry, and even then
269 those values are overwritten by actual register values on syscall
272 /* Some register values written here may be ignored in
273 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
274 * r31/r31+4, and not with the values in pt_regs.
276 if (addr
== PT_PSW
) {
277 /* Since PT_PSW==0, it is valid for 32 bit processes
278 * under 64 bit kernels as well.
280 ret
= arch_ptrace(child
, request
, addr
, data
);
282 if (addr
& (sizeof(compat_uint_t
)-1))
284 addr
= translate_usr_offset(addr
);
285 if (addr
>= sizeof(struct pt_regs
))
287 if (addr
>= PT_FR0
&& addr
<= PT_FR31
+ 4) {
288 /* Special case, fp regs are 64 bits anyway */
289 *(__u64
*) ((char *) task_regs(child
) + addr
) = data
;
292 else if ((addr
>= PT_GR1
+4 && addr
<= PT_GR31
+4) ||
293 addr
== PT_IAOQ0
+4 || addr
== PT_IAOQ1
+4 ||
295 /* Zero the top 32 bits */
296 *(__u32
*) ((char *) task_regs(child
) + addr
- 4) = 0;
297 *(__u32
*) ((char *) task_regs(child
) + addr
) = data
;
304 ret
= compat_ptrace_request(child
, request
, addr
, data
);
312 long do_syscall_trace_enter(struct pt_regs
*regs
)
314 /* Do the secure computing check first. */
315 if (secure_computing() == -1)
318 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
319 tracehook_report_syscall_entry(regs
)) {
321 * Tracing decided this syscall should not happen or the
322 * debugger stored an invalid system call number. Skip
323 * the system call and the system call restart handling.
328 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
329 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
330 trace_sys_enter(regs
, regs
->gr
[20]);
334 if (!is_compat_task())
335 audit_syscall_entry(regs
->gr
[20], regs
->gr
[26], regs
->gr
[25],
336 regs
->gr
[24], regs
->gr
[23]);
339 audit_syscall_entry(regs
->gr
[20] & 0xffffffff,
340 regs
->gr
[26] & 0xffffffff,
341 regs
->gr
[25] & 0xffffffff,
342 regs
->gr
[24] & 0xffffffff,
343 regs
->gr
[23] & 0xffffffff);
347 * Sign extend the syscall number to 64bit since it may have been
348 * modified by a compat ptrace call
350 return (int) ((u32
) regs
->gr
[20]);
353 void do_syscall_trace_exit(struct pt_regs
*regs
)
355 int stepping
= test_thread_flag(TIF_SINGLESTEP
) ||
356 test_thread_flag(TIF_BLOCKSTEP
);
358 audit_syscall_exit(regs
);
360 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
361 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
362 trace_sys_exit(regs
, regs
->gr
[20]);
365 if (stepping
|| test_thread_flag(TIF_SYSCALL_TRACE
))
366 tracehook_report_syscall_exit(regs
, stepping
);
374 static int fpr_get(struct task_struct
*target
,
375 const struct user_regset
*regset
,
376 unsigned int pos
, unsigned int count
,
377 void *kbuf
, void __user
*ubuf
)
379 struct pt_regs
*regs
= task_regs(target
);
381 __u64 __user
*u
= ubuf
;
385 count
/= sizeof(reg
);
388 for (; count
> 0 && pos
< ELF_NFPREG
; --count
)
389 *k
++ = regs
->fr
[pos
++];
391 for (; count
> 0 && pos
< ELF_NFPREG
; --count
)
392 if (__put_user(regs
->fr
[pos
++], u
++))
398 count
*= sizeof(reg
);
399 return user_regset_copyout_zero(&pos
, &count
, &kbuf
, &ubuf
,
400 ELF_NFPREG
* sizeof(reg
), -1);
403 static int fpr_set(struct task_struct
*target
,
404 const struct user_regset
*regset
,
405 unsigned int pos
, unsigned int count
,
406 const void *kbuf
, const void __user
*ubuf
)
408 struct pt_regs
*regs
= task_regs(target
);
409 const __u64
*k
= kbuf
;
410 const __u64 __user
*u
= ubuf
;
414 count
/= sizeof(reg
);
417 for (; count
> 0 && pos
< ELF_NFPREG
; --count
)
418 regs
->fr
[pos
++] = *k
++;
420 for (; count
> 0 && pos
< ELF_NFPREG
; --count
) {
421 if (__get_user(reg
, u
++))
423 regs
->fr
[pos
++] = reg
;
429 count
*= sizeof(reg
);
430 return user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
431 ELF_NFPREG
* sizeof(reg
), -1);
434 #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long))
436 static unsigned long get_reg(struct pt_regs
*regs
, int num
)
439 case RI(gr
[0]) ... RI(gr
[31]): return regs
->gr
[num
- RI(gr
[0])];
440 case RI(sr
[0]) ... RI(sr
[7]): return regs
->sr
[num
- RI(sr
[0])];
441 case RI(iasq
[0]): return regs
->iasq
[0];
442 case RI(iasq
[1]): return regs
->iasq
[1];
443 case RI(iaoq
[0]): return regs
->iaoq
[0];
444 case RI(iaoq
[1]): return regs
->iaoq
[1];
445 case RI(sar
): return regs
->sar
;
446 case RI(iir
): return regs
->iir
;
447 case RI(isr
): return regs
->isr
;
448 case RI(ior
): return regs
->ior
;
449 case RI(ipsw
): return regs
->ipsw
;
450 case RI(cr27
): return regs
->cr27
;
451 case RI(cr0
): return mfctl(0);
452 case RI(cr24
): return mfctl(24);
453 case RI(cr25
): return mfctl(25);
454 case RI(cr26
): return mfctl(26);
455 case RI(cr28
): return mfctl(28);
456 case RI(cr29
): return mfctl(29);
457 case RI(cr30
): return mfctl(30);
458 case RI(cr31
): return mfctl(31);
459 case RI(cr8
): return mfctl(8);
460 case RI(cr9
): return mfctl(9);
461 case RI(cr12
): return mfctl(12);
462 case RI(cr13
): return mfctl(13);
463 case RI(cr10
): return mfctl(10);
464 case RI(cr15
): return mfctl(15);
469 static void set_reg(struct pt_regs
*regs
, int num
, unsigned long val
)
474 * Allow writing to Nullify, Divide-step-correction,
475 * and carry/borrow bits.
476 * BEWARE, if you set N, and then single step, it won't
477 * stop on the nullified instruction.
479 val
&= USER_PSW_BITS
;
480 regs
->gr
[0] &= ~USER_PSW_BITS
;
483 case RI(gr
[1]) ... RI(gr
[31]):
484 regs
->gr
[num
- RI(gr
[0])] = val
;
488 regs
->iaoq
[num
- RI(iaoq
[0])] = val
;
490 case RI(sar
): regs
->sar
= val
;
494 /* do not allow to change any of the following registers (yet) */
495 case RI(sr
[0]) ... RI(sr
[7]): return regs
->sr
[num
- RI(sr
[0])];
496 case RI(iasq
[0]): return regs
->iasq
[0];
497 case RI(iasq
[1]): return regs
->iasq
[1];
498 case RI(iir
): return regs
->iir
;
499 case RI(isr
): return regs
->isr
;
500 case RI(ior
): return regs
->ior
;
501 case RI(ipsw
): return regs
->ipsw
;
502 case RI(cr27
): return regs
->cr27
;
503 case cr0
, cr24
, cr25
, cr26
, cr27
, cr28
, cr29
, cr30
, cr31
;
504 case cr8
, cr9
, cr12
, cr13
, cr10
, cr15
;
509 static int gpr_get(struct task_struct
*target
,
510 const struct user_regset
*regset
,
511 unsigned int pos
, unsigned int count
,
512 void *kbuf
, void __user
*ubuf
)
514 struct pt_regs
*regs
= task_regs(target
);
515 unsigned long *k
= kbuf
;
516 unsigned long __user
*u
= ubuf
;
520 count
/= sizeof(reg
);
523 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
524 *k
++ = get_reg(regs
, pos
++);
526 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
527 if (__put_user(get_reg(regs
, pos
++), u
++))
532 count
*= sizeof(reg
);
533 return user_regset_copyout_zero(&pos
, &count
, &kbuf
, &ubuf
,
534 ELF_NGREG
* sizeof(reg
), -1);
537 static int gpr_set(struct task_struct
*target
,
538 const struct user_regset
*regset
,
539 unsigned int pos
, unsigned int count
,
540 const void *kbuf
, const void __user
*ubuf
)
542 struct pt_regs
*regs
= task_regs(target
);
543 const unsigned long *k
= kbuf
;
544 const unsigned long __user
*u
= ubuf
;
548 count
/= sizeof(reg
);
551 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
552 set_reg(regs
, pos
++, *k
++);
554 for (; count
> 0 && pos
< ELF_NGREG
; --count
) {
555 if (__get_user(reg
, u
++))
557 set_reg(regs
, pos
++, reg
);
563 count
*= sizeof(reg
);
564 return user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
565 ELF_NGREG
* sizeof(reg
), -1);
568 static const struct user_regset native_regsets
[] = {
570 .core_note_type
= NT_PRSTATUS
, .n
= ELF_NGREG
,
571 .size
= sizeof(long), .align
= sizeof(long),
572 .get
= gpr_get
, .set
= gpr_set
575 .core_note_type
= NT_PRFPREG
, .n
= ELF_NFPREG
,
576 .size
= sizeof(__u64
), .align
= sizeof(__u64
),
577 .get
= fpr_get
, .set
= fpr_set
581 static const struct user_regset_view user_parisc_native_view
= {
582 .name
= "parisc", .e_machine
= ELF_ARCH
, .ei_osabi
= ELFOSABI_LINUX
,
583 .regsets
= native_regsets
, .n
= ARRAY_SIZE(native_regsets
)
587 #include <linux/compat.h>
589 static int gpr32_get(struct task_struct
*target
,
590 const struct user_regset
*regset
,
591 unsigned int pos
, unsigned int count
,
592 void *kbuf
, void __user
*ubuf
)
594 struct pt_regs
*regs
= task_regs(target
);
595 compat_ulong_t
*k
= kbuf
;
596 compat_ulong_t __user
*u
= ubuf
;
600 count
/= sizeof(reg
);
603 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
604 *k
++ = get_reg(regs
, pos
++);
606 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
607 if (__put_user((compat_ulong_t
) get_reg(regs
, pos
++), u
++))
613 count
*= sizeof(reg
);
614 return user_regset_copyout_zero(&pos
, &count
, &kbuf
, &ubuf
,
615 ELF_NGREG
* sizeof(reg
), -1);
618 static int gpr32_set(struct task_struct
*target
,
619 const struct user_regset
*regset
,
620 unsigned int pos
, unsigned int count
,
621 const void *kbuf
, const void __user
*ubuf
)
623 struct pt_regs
*regs
= task_regs(target
);
624 const compat_ulong_t
*k
= kbuf
;
625 const compat_ulong_t __user
*u
= ubuf
;
629 count
/= sizeof(reg
);
632 for (; count
> 0 && pos
< ELF_NGREG
; --count
)
633 set_reg(regs
, pos
++, *k
++);
635 for (; count
> 0 && pos
< ELF_NGREG
; --count
) {
636 if (__get_user(reg
, u
++))
638 set_reg(regs
, pos
++, reg
);
644 count
*= sizeof(reg
);
645 return user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
646 ELF_NGREG
* sizeof(reg
), -1);
650 * These are the regset flavors matching the 32bit native set.
652 static const struct user_regset compat_regsets
[] = {
654 .core_note_type
= NT_PRSTATUS
, .n
= ELF_NGREG
,
655 .size
= sizeof(compat_long_t
), .align
= sizeof(compat_long_t
),
656 .get
= gpr32_get
, .set
= gpr32_set
659 .core_note_type
= NT_PRFPREG
, .n
= ELF_NFPREG
,
660 .size
= sizeof(__u64
), .align
= sizeof(__u64
),
661 .get
= fpr_get
, .set
= fpr_set
665 static const struct user_regset_view user_parisc_compat_view
= {
666 .name
= "parisc", .e_machine
= EM_PARISC
, .ei_osabi
= ELFOSABI_LINUX
,
667 .regsets
= compat_regsets
, .n
= ARRAY_SIZE(compat_regsets
)
669 #endif /* CONFIG_64BIT */
671 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
673 BUILD_BUG_ON(sizeof(struct user_regs_struct
)/sizeof(long) != ELF_NGREG
);
674 BUILD_BUG_ON(sizeof(struct user_fp_struct
)/sizeof(__u64
) != ELF_NFPREG
);
676 if (is_compat_task())
677 return &user_parisc_compat_view
;
679 return &user_parisc_native_view
;