2 * Based on arch/arm/kernel/ptrace.c
5 * edited by Linus Torvalds
6 * ARM modifications Copyright (C) 2000 Russell King
7 * Copyright (C) 2012 ARM Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/audit.h>
23 #include <linux/compat.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
27 #include <linux/smp.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
30 #include <linux/seccomp.h>
31 #include <linux/security.h>
32 #include <linux/init.h>
33 #include <linux/signal.h>
34 #include <linux/uaccess.h>
35 #include <linux/perf_event.h>
36 #include <linux/hw_breakpoint.h>
37 #include <linux/regset.h>
38 #include <linux/tracehook.h>
39 #include <linux/elf.h>
41 #include <asm/compat.h>
42 #include <asm/debug-monitors.h>
43 #include <asm/pgtable.h>
44 #include <asm/syscall.h>
45 #include <asm/traps.h>
46 #include <asm/system_misc.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/syscalls.h>
52 * TODO: does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
57 * Called by kernel/ptrace.c when detaching..
59 void ptrace_disable(struct task_struct *child)
63 #ifdef CONFIG_HAVE_HW_BREAKPOINT
65 * Handle hitting a HW-breakpoint.
67 static void ptrace_hbptriggered(struct perf_event *bp,
68 struct perf_sample_data *data,
71 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
75 .si_code = TRAP_HWBKPT,
76 .si_addr = (void __user *)(bkpt->trigger),
82 if (!is_compat_task())
85 for (i = 0; i < ARM_MAX_BRP; ++i) {
86 if (current->thread.debug.hbp_break[i] == bp) {
87 info.si_errno = (i << 1) + 1;
92 for (i = 0; i < ARM_MAX_WRP; ++i) {
93 if (current->thread.debug.hbp_watch[i] == bp) {
94 info.si_errno = -((i << 1) + 1);
101 force_sig_info(SIGTRAP, &info, current);
105 * Unregister breakpoints from this task and reset the pointers in
108 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
111 struct thread_struct *t = &tsk->thread;
113 for (i = 0; i < ARM_MAX_BRP; i++) {
114 if (t->debug.hbp_break[i]) {
115 unregister_hw_breakpoint(t->debug.hbp_break[i]);
116 t->debug.hbp_break[i] = NULL;
120 for (i = 0; i < ARM_MAX_WRP; i++) {
121 if (t->debug.hbp_watch[i]) {
122 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
123 t->debug.hbp_watch[i] = NULL;
128 void ptrace_hw_copy_thread(struct task_struct *tsk)
130 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
133 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
134 struct task_struct *tsk,
137 struct perf_event *bp = ERR_PTR(-EINVAL);
140 case NT_ARM_HW_BREAK:
141 if (idx < ARM_MAX_BRP)
142 bp = tsk->thread.debug.hbp_break[idx];
144 case NT_ARM_HW_WATCH:
145 if (idx < ARM_MAX_WRP)
146 bp = tsk->thread.debug.hbp_watch[idx];
153 static int ptrace_hbp_set_event(unsigned int note_type,
154 struct task_struct *tsk,
156 struct perf_event *bp)
161 case NT_ARM_HW_BREAK:
162 if (idx < ARM_MAX_BRP) {
163 tsk->thread.debug.hbp_break[idx] = bp;
167 case NT_ARM_HW_WATCH:
168 if (idx < ARM_MAX_WRP) {
169 tsk->thread.debug.hbp_watch[idx] = bp;
178 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
179 struct task_struct *tsk,
182 struct perf_event *bp;
183 struct perf_event_attr attr;
187 case NT_ARM_HW_BREAK:
188 type = HW_BREAKPOINT_X;
190 case NT_ARM_HW_WATCH:
191 type = HW_BREAKPOINT_RW;
194 return ERR_PTR(-EINVAL);
197 ptrace_breakpoint_init(&attr);
200 * Initialise fields to sane defaults
201 * (i.e. values that will pass validation).
204 attr.bp_len = HW_BREAKPOINT_LEN_4;
208 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
212 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
219 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
220 struct arch_hw_breakpoint_ctrl ctrl,
221 struct perf_event_attr *attr)
223 int err, len, type, disabled = !ctrl.enabled;
225 attr->disabled = disabled;
229 err = arch_bp_generic_fields(ctrl, &len, &type);
234 case NT_ARM_HW_BREAK:
235 if ((type & HW_BREAKPOINT_X) != type)
238 case NT_ARM_HW_WATCH:
239 if ((type & HW_BREAKPOINT_RW) != type)
247 attr->bp_type = type;
252 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
258 case NT_ARM_HW_BREAK:
259 num = hw_breakpoint_slots(TYPE_INST);
261 case NT_ARM_HW_WATCH:
262 num = hw_breakpoint_slots(TYPE_DATA);
268 reg |= debug_monitors_arch();
276 static int ptrace_hbp_get_ctrl(unsigned int note_type,
277 struct task_struct *tsk,
281 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
286 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
290 static int ptrace_hbp_get_addr(unsigned int note_type,
291 struct task_struct *tsk,
295 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
300 *addr = bp ? bp->attr.bp_addr : 0;
304 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
305 struct task_struct *tsk,
308 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
311 bp = ptrace_hbp_create(note_type, tsk, idx);
316 static int ptrace_hbp_set_ctrl(unsigned int note_type,
317 struct task_struct *tsk,
322 struct perf_event *bp;
323 struct perf_event_attr attr;
324 struct arch_hw_breakpoint_ctrl ctrl;
326 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
333 decode_ctrl_reg(uctrl, &ctrl);
334 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
338 return modify_user_hw_breakpoint(bp, &attr);
341 static int ptrace_hbp_set_addr(unsigned int note_type,
342 struct task_struct *tsk,
347 struct perf_event *bp;
348 struct perf_event_attr attr;
350 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
358 err = modify_user_hw_breakpoint(bp, &attr);
362 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
363 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
364 #define PTRACE_HBP_PAD_SZ sizeof(u32)
366 static int hw_break_get(struct task_struct *target,
367 const struct user_regset *regset,
368 unsigned int pos, unsigned int count,
369 void *kbuf, void __user *ubuf)
371 unsigned int note_type = regset->core_note_type;
372 int ret, idx = 0, offset, limit;
377 ret = ptrace_hbp_get_resource_info(note_type, &info);
381 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
387 offset = offsetof(struct user_hwdebug_state, pad);
388 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
389 offset + PTRACE_HBP_PAD_SZ);
393 /* (address, ctrl) registers */
394 offset = offsetof(struct user_hwdebug_state, dbg_regs);
395 limit = regset->n * regset->size;
396 while (count && offset < limit) {
397 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
400 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
401 offset, offset + PTRACE_HBP_ADDR_SZ);
404 offset += PTRACE_HBP_ADDR_SZ;
406 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
409 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
410 offset, offset + PTRACE_HBP_CTRL_SZ);
413 offset += PTRACE_HBP_CTRL_SZ;
415 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
417 offset + PTRACE_HBP_PAD_SZ);
420 offset += PTRACE_HBP_PAD_SZ;
427 static int hw_break_set(struct task_struct *target,
428 const struct user_regset *regset,
429 unsigned int pos, unsigned int count,
430 const void *kbuf, const void __user *ubuf)
432 unsigned int note_type = regset->core_note_type;
433 int ret, idx = 0, offset, limit;
437 /* Resource info and pad */
438 offset = offsetof(struct user_hwdebug_state, dbg_regs);
439 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
443 /* (address, ctrl) registers */
444 limit = regset->n * regset->size;
445 while (count && offset < limit) {
446 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
447 offset, offset + PTRACE_HBP_ADDR_SZ);
450 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
453 offset += PTRACE_HBP_ADDR_SZ;
455 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
456 offset, offset + PTRACE_HBP_CTRL_SZ);
459 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
462 offset += PTRACE_HBP_CTRL_SZ;
464 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
466 offset + PTRACE_HBP_PAD_SZ);
469 offset += PTRACE_HBP_PAD_SZ;
475 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
477 static int gpr_get(struct task_struct *target,
478 const struct user_regset *regset,
479 unsigned int pos, unsigned int count,
480 void *kbuf, void __user *ubuf)
482 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
483 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
486 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
487 unsigned int pos, unsigned int count,
488 const void *kbuf, const void __user *ubuf)
491 struct user_pt_regs newregs;
493 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
497 if (!valid_user_regs(&newregs))
500 task_pt_regs(target)->user_regs = newregs;
505 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
507 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 void *kbuf, void __user *ubuf)
511 struct user_fpsimd_state *uregs;
512 uregs = &target->thread.fpsimd_state.user_fpsimd;
513 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
516 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
517 unsigned int pos, unsigned int count,
518 const void *kbuf, const void __user *ubuf)
521 struct user_fpsimd_state newstate;
523 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
527 target->thread.fpsimd_state.user_fpsimd = newstate;
528 fpsimd_flush_task_state(target);
532 static int tls_get(struct task_struct *target, const struct user_regset *regset,
533 unsigned int pos, unsigned int count,
534 void *kbuf, void __user *ubuf)
536 unsigned long *tls = &target->thread.tp_value;
537 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
540 static int tls_set(struct task_struct *target, const struct user_regset *regset,
541 unsigned int pos, unsigned int count,
542 const void *kbuf, const void __user *ubuf)
547 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
551 target->thread.tp_value = tls;
555 static int system_call_get(struct task_struct *target,
556 const struct user_regset *regset,
557 unsigned int pos, unsigned int count,
558 void *kbuf, void __user *ubuf)
560 int syscallno = task_pt_regs(target)->syscallno;
562 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
566 static int system_call_set(struct task_struct *target,
567 const struct user_regset *regset,
568 unsigned int pos, unsigned int count,
569 const void *kbuf, const void __user *ubuf)
573 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
577 task_pt_regs(target)->syscallno = syscallno;
581 enum aarch64_regset {
585 #ifdef CONFIG_HAVE_HW_BREAKPOINT
592 static const struct user_regset aarch64_regsets[] = {
594 .core_note_type = NT_PRSTATUS,
595 .n = sizeof(struct user_pt_regs) / sizeof(u64),
597 .align = sizeof(u64),
602 .core_note_type = NT_PRFPREG,
603 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
605 * We pretend we have 32-bit registers because the fpsr and
606 * fpcr are 32-bits wide.
609 .align = sizeof(u32),
614 .core_note_type = NT_ARM_TLS,
616 .size = sizeof(void *),
617 .align = sizeof(void *),
621 #ifdef CONFIG_HAVE_HW_BREAKPOINT
622 [REGSET_HW_BREAK] = {
623 .core_note_type = NT_ARM_HW_BREAK,
624 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
626 .align = sizeof(u32),
630 [REGSET_HW_WATCH] = {
631 .core_note_type = NT_ARM_HW_WATCH,
632 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
634 .align = sizeof(u32),
639 [REGSET_SYSTEM_CALL] = {
640 .core_note_type = NT_ARM_SYSTEM_CALL,
643 .align = sizeof(int),
644 .get = system_call_get,
645 .set = system_call_set,
649 static const struct user_regset_view user_aarch64_view = {
650 .name = "aarch64", .e_machine = EM_AARCH64,
651 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
655 #include <linux/compat.h>
662 static int compat_gpr_get(struct task_struct *target,
663 const struct user_regset *regset,
664 unsigned int pos, unsigned int count,
665 void *kbuf, void __user *ubuf)
668 unsigned int i, start, num_regs;
670 /* Calculate the number of AArch32 registers contained in count */
671 num_regs = count / regset->size;
673 /* Convert pos into an register number */
674 start = pos / regset->size;
676 if (start + num_regs > regset->n)
679 for (i = 0; i < num_regs; ++i) {
680 unsigned int idx = start + i;
685 reg = task_pt_regs(target)->pc;
688 reg = task_pt_regs(target)->pstate;
691 reg = task_pt_regs(target)->orig_x0;
694 reg = task_pt_regs(target)->regs[idx];
698 memcpy(kbuf, ®, sizeof(reg));
701 ret = copy_to_user(ubuf, ®, sizeof(reg));
714 static int compat_gpr_set(struct task_struct *target,
715 const struct user_regset *regset,
716 unsigned int pos, unsigned int count,
717 const void *kbuf, const void __user *ubuf)
719 struct pt_regs newregs;
721 unsigned int i, start, num_regs;
723 /* Calculate the number of AArch32 registers contained in count */
724 num_regs = count / regset->size;
726 /* Convert pos into an register number */
727 start = pos / regset->size;
729 if (start + num_regs > regset->n)
732 newregs = *task_pt_regs(target);
734 for (i = 0; i < num_regs; ++i) {
735 unsigned int idx = start + i;
739 memcpy(®, kbuf, sizeof(reg));
742 ret = copy_from_user(®, ubuf, sizeof(reg));
756 newregs.pstate = reg;
759 newregs.orig_x0 = reg;
762 newregs.regs[idx] = reg;
767 if (valid_user_regs(&newregs.user_regs))
768 *task_pt_regs(target) = newregs;
775 static int compat_vfp_get(struct task_struct *target,
776 const struct user_regset *regset,
777 unsigned int pos, unsigned int count,
778 void *kbuf, void __user *ubuf)
780 struct user_fpsimd_state *uregs;
781 compat_ulong_t fpscr;
784 uregs = &target->thread.fpsimd_state.user_fpsimd;
787 * The VFP registers are packed into the fpsimd_state, so they all sit
788 * nicely together for us. We just need to create the fpscr separately.
790 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
791 VFP_STATE_SIZE - sizeof(compat_ulong_t));
794 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
795 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
796 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
802 static int compat_vfp_set(struct task_struct *target,
803 const struct user_regset *regset,
804 unsigned int pos, unsigned int count,
805 const void *kbuf, const void __user *ubuf)
807 struct user_fpsimd_state *uregs;
808 compat_ulong_t fpscr;
811 if (pos + count > VFP_STATE_SIZE)
814 uregs = &target->thread.fpsimd_state.user_fpsimd;
816 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
817 VFP_STATE_SIZE - sizeof(compat_ulong_t));
820 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
821 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
822 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
825 fpsimd_flush_task_state(target);
829 static int compat_tls_get(struct task_struct *target,
830 const struct user_regset *regset, unsigned int pos,
831 unsigned int count, void *kbuf, void __user *ubuf)
833 compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
834 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
837 static int compat_tls_set(struct task_struct *target,
838 const struct user_regset *regset, unsigned int pos,
839 unsigned int count, const void *kbuf,
840 const void __user *ubuf)
845 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
849 target->thread.tp_value = tls;
853 static const struct user_regset aarch32_regsets[] = {
854 [REGSET_COMPAT_GPR] = {
855 .core_note_type = NT_PRSTATUS,
856 .n = COMPAT_ELF_NGREG,
857 .size = sizeof(compat_elf_greg_t),
858 .align = sizeof(compat_elf_greg_t),
859 .get = compat_gpr_get,
860 .set = compat_gpr_set
862 [REGSET_COMPAT_VFP] = {
863 .core_note_type = NT_ARM_VFP,
864 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
865 .size = sizeof(compat_ulong_t),
866 .align = sizeof(compat_ulong_t),
867 .get = compat_vfp_get,
868 .set = compat_vfp_set
872 static const struct user_regset_view user_aarch32_view = {
873 .name = "aarch32", .e_machine = EM_ARM,
874 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
877 static const struct user_regset aarch32_ptrace_regsets[] = {
879 .core_note_type = NT_PRSTATUS,
880 .n = COMPAT_ELF_NGREG,
881 .size = sizeof(compat_elf_greg_t),
882 .align = sizeof(compat_elf_greg_t),
883 .get = compat_gpr_get,
884 .set = compat_gpr_set
887 .core_note_type = NT_ARM_VFP,
888 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
889 .size = sizeof(compat_ulong_t),
890 .align = sizeof(compat_ulong_t),
891 .get = compat_vfp_get,
892 .set = compat_vfp_set
895 .core_note_type = NT_ARM_TLS,
897 .size = sizeof(compat_ulong_t),
898 .align = sizeof(compat_ulong_t),
899 .get = compat_tls_get,
900 .set = compat_tls_set,
902 #ifdef CONFIG_HAVE_HW_BREAKPOINT
903 [REGSET_HW_BREAK] = {
904 .core_note_type = NT_ARM_HW_BREAK,
905 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
907 .align = sizeof(u32),
911 [REGSET_HW_WATCH] = {
912 .core_note_type = NT_ARM_HW_WATCH,
913 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
915 .align = sizeof(u32),
920 [REGSET_SYSTEM_CALL] = {
921 .core_note_type = NT_ARM_SYSTEM_CALL,
924 .align = sizeof(int),
925 .get = system_call_get,
926 .set = system_call_set,
930 static const struct user_regset_view user_aarch32_ptrace_view = {
931 .name = "aarch32", .e_machine = EM_ARM,
932 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
935 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
936 compat_ulong_t __user *ret)
943 if (off == COMPAT_PT_TEXT_ADDR)
944 tmp = tsk->mm->start_code;
945 else if (off == COMPAT_PT_DATA_ADDR)
946 tmp = tsk->mm->start_data;
947 else if (off == COMPAT_PT_TEXT_END_ADDR)
948 tmp = tsk->mm->end_code;
949 else if (off < sizeof(compat_elf_gregset_t))
950 return copy_regset_to_user(tsk, &user_aarch32_view,
951 REGSET_COMPAT_GPR, off,
952 sizeof(compat_ulong_t), ret);
953 else if (off >= COMPAT_USER_SZ)
958 return put_user(tmp, ret);
961 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
965 mm_segment_t old_fs = get_fs();
967 if (off & 3 || off >= COMPAT_USER_SZ)
970 if (off >= sizeof(compat_elf_gregset_t))
974 ret = copy_regset_from_user(tsk, &user_aarch32_view,
975 REGSET_COMPAT_GPR, off,
976 sizeof(compat_ulong_t),
983 #ifdef CONFIG_HAVE_HW_BREAKPOINT
986 * Convert a virtual register number into an index for a thread_info
987 * breakpoint array. Breakpoints are identified using positive numbers
988 * whilst watchpoints are negative. The registers are laid out as pairs
989 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
990 * Register 0 is reserved for describing resource information.
992 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
994 return (abs(num) - 1) >> 1;
997 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
999 u8 num_brps, num_wrps, debug_arch, wp_len;
1002 num_brps = hw_breakpoint_slots(TYPE_INST);
1003 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1005 debug_arch = debug_monitors_arch();
1019 static int compat_ptrace_hbp_get(unsigned int note_type,
1020 struct task_struct *tsk,
1027 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1030 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1033 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1040 static int compat_ptrace_hbp_set(unsigned int note_type,
1041 struct task_struct *tsk,
1048 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1052 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1055 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1061 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1062 compat_ulong_t __user *data)
1066 mm_segment_t old_fs = get_fs();
1071 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1073 } else if (num == 0) {
1074 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1077 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1082 ret = put_user(kdata, data);
1087 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1088 compat_ulong_t __user *data)
1092 mm_segment_t old_fs = get_fs();
1097 ret = get_user(kdata, data);
1103 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1105 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1110 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1112 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1113 compat_ulong_t caddr, compat_ulong_t cdata)
1115 unsigned long addr = caddr;
1116 unsigned long data = cdata;
1117 void __user *datap = compat_ptr(data);
1121 case PTRACE_PEEKUSR:
1122 ret = compat_ptrace_read_user(child, addr, datap);
1125 case PTRACE_POKEUSR:
1126 ret = compat_ptrace_write_user(child, addr, data);
1129 case COMPAT_PTRACE_GETREGS:
1130 ret = copy_regset_to_user(child,
1133 0, sizeof(compat_elf_gregset_t),
1137 case COMPAT_PTRACE_SETREGS:
1138 ret = copy_regset_from_user(child,
1141 0, sizeof(compat_elf_gregset_t),
1145 case COMPAT_PTRACE_GET_THREAD_AREA:
1146 ret = put_user((compat_ulong_t)child->thread.tp_value,
1147 (compat_ulong_t __user *)datap);
1150 case COMPAT_PTRACE_SET_SYSCALL:
1151 task_pt_regs(child)->syscallno = data;
1155 case COMPAT_PTRACE_GETVFPREGS:
1156 ret = copy_regset_to_user(child,
1163 case COMPAT_PTRACE_SETVFPREGS:
1164 ret = copy_regset_from_user(child,
1171 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1172 case COMPAT_PTRACE_GETHBPREGS:
1173 ret = compat_ptrace_gethbpregs(child, addr, datap);
1176 case COMPAT_PTRACE_SETHBPREGS:
1177 ret = compat_ptrace_sethbpregs(child, addr, datap);
1182 ret = compat_ptrace_request(child, request, addr,
1189 #endif /* CONFIG_COMPAT */
1191 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1193 #ifdef CONFIG_COMPAT
1195 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1196 * user_aarch32_view compatible with arm32. Native ptrace requests on
1197 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1198 * access to the TLS register.
1200 if (is_compat_task())
1201 return &user_aarch32_view;
1202 else if (is_compat_thread(task_thread_info(task)))
1203 return &user_aarch32_ptrace_view;
1205 return &user_aarch64_view;
1208 long arch_ptrace(struct task_struct *child, long request,
1209 unsigned long addr, unsigned long data)
1211 return ptrace_request(child, request, addr, data);
1214 enum ptrace_syscall_dir {
1215 PTRACE_SYSCALL_ENTER = 0,
1216 PTRACE_SYSCALL_EXIT,
1219 static void tracehook_report_syscall(struct pt_regs *regs,
1220 enum ptrace_syscall_dir dir)
1223 unsigned long saved_reg;
1226 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1227 * used to denote syscall entry/exit:
1229 regno = (is_compat_task() ? 12 : 7);
1230 saved_reg = regs->regs[regno];
1231 regs->regs[regno] = dir;
1233 if (dir == PTRACE_SYSCALL_EXIT)
1234 tracehook_report_syscall_exit(regs, 0);
1235 else if (tracehook_report_syscall_entry(regs))
1236 regs->syscallno = ~0UL;
1238 regs->regs[regno] = saved_reg;
1241 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1243 /* Do the secure computing check first; failures should be fast. */
1244 if (secure_computing() == -1)
1247 if (test_thread_flag(TIF_SYSCALL_TRACE))
1248 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1250 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1251 trace_sys_enter(regs, regs->syscallno);
1253 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1254 regs->regs[2], regs->regs[3]);
1256 return regs->syscallno;
1259 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1261 audit_syscall_exit(regs);
1263 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1264 trace_sys_exit(regs, regs_return_value(regs));
1266 if (test_thread_flag(TIF_SYSCALL_TRACE))
1267 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);