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/cpufeature.h>
43 #include <asm/debug-monitors.h>
44 #include <asm/pgtable.h>
45 #include <asm/syscall.h>
46 #include <asm/traps.h>
47 #include <asm/system_misc.h>
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/syscalls.h>
53 * TODO: does not yet catch signals sent when the child dies.
54 * in exit.c or in signal.c.
58 * Called by kernel/ptrace.c when detaching..
60 void ptrace_disable(struct task_struct *child)
63 * This would be better off in core code, but PTRACE_DETACH has
64 * grown its fair share of arch-specific worts and changing it
65 * is likely to cause regressions on obscure architectures.
67 user_disable_single_step(child);
70 #ifdef CONFIG_HAVE_HW_BREAKPOINT
72 * Handle hitting a HW-breakpoint.
74 static void ptrace_hbptriggered(struct perf_event *bp,
75 struct perf_sample_data *data,
78 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
82 .si_code = TRAP_HWBKPT,
83 .si_addr = (void __user *)(bkpt->trigger),
89 if (!is_compat_task())
92 for (i = 0; i < ARM_MAX_BRP; ++i) {
93 if (current->thread.debug.hbp_break[i] == bp) {
94 info.si_errno = (i << 1) + 1;
99 for (i = 0; i < ARM_MAX_WRP; ++i) {
100 if (current->thread.debug.hbp_watch[i] == bp) {
101 info.si_errno = -((i << 1) + 1);
108 force_sig_info(SIGTRAP, &info, current);
112 * Unregister breakpoints from this task and reset the pointers in
115 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
118 struct thread_struct *t = &tsk->thread;
120 for (i = 0; i < ARM_MAX_BRP; i++) {
121 if (t->debug.hbp_break[i]) {
122 unregister_hw_breakpoint(t->debug.hbp_break[i]);
123 t->debug.hbp_break[i] = NULL;
127 for (i = 0; i < ARM_MAX_WRP; i++) {
128 if (t->debug.hbp_watch[i]) {
129 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
130 t->debug.hbp_watch[i] = NULL;
135 void ptrace_hw_copy_thread(struct task_struct *tsk)
137 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
140 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
141 struct task_struct *tsk,
144 struct perf_event *bp = ERR_PTR(-EINVAL);
147 case NT_ARM_HW_BREAK:
148 if (idx < ARM_MAX_BRP)
149 bp = tsk->thread.debug.hbp_break[idx];
151 case NT_ARM_HW_WATCH:
152 if (idx < ARM_MAX_WRP)
153 bp = tsk->thread.debug.hbp_watch[idx];
160 static int ptrace_hbp_set_event(unsigned int note_type,
161 struct task_struct *tsk,
163 struct perf_event *bp)
168 case NT_ARM_HW_BREAK:
169 if (idx < ARM_MAX_BRP) {
170 tsk->thread.debug.hbp_break[idx] = bp;
174 case NT_ARM_HW_WATCH:
175 if (idx < ARM_MAX_WRP) {
176 tsk->thread.debug.hbp_watch[idx] = bp;
185 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
186 struct task_struct *tsk,
189 struct perf_event *bp;
190 struct perf_event_attr attr;
194 case NT_ARM_HW_BREAK:
195 type = HW_BREAKPOINT_X;
197 case NT_ARM_HW_WATCH:
198 type = HW_BREAKPOINT_RW;
201 return ERR_PTR(-EINVAL);
204 ptrace_breakpoint_init(&attr);
207 * Initialise fields to sane defaults
208 * (i.e. values that will pass validation).
211 attr.bp_len = HW_BREAKPOINT_LEN_4;
215 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
219 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
226 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
227 struct arch_hw_breakpoint_ctrl ctrl,
228 struct perf_event_attr *attr)
230 int err, len, type, disabled = !ctrl.enabled;
232 attr->disabled = disabled;
236 err = arch_bp_generic_fields(ctrl, &len, &type);
241 case NT_ARM_HW_BREAK:
242 if ((type & HW_BREAKPOINT_X) != type)
245 case NT_ARM_HW_WATCH:
246 if ((type & HW_BREAKPOINT_RW) != type)
254 attr->bp_type = type;
259 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
265 case NT_ARM_HW_BREAK:
266 num = hw_breakpoint_slots(TYPE_INST);
268 case NT_ARM_HW_WATCH:
269 num = hw_breakpoint_slots(TYPE_DATA);
275 reg |= debug_monitors_arch();
283 static int ptrace_hbp_get_ctrl(unsigned int note_type,
284 struct task_struct *tsk,
288 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
293 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
297 static int ptrace_hbp_get_addr(unsigned int note_type,
298 struct task_struct *tsk,
302 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
307 *addr = bp ? bp->attr.bp_addr : 0;
311 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
312 struct task_struct *tsk,
315 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
318 bp = ptrace_hbp_create(note_type, tsk, idx);
323 static int ptrace_hbp_set_ctrl(unsigned int note_type,
324 struct task_struct *tsk,
329 struct perf_event *bp;
330 struct perf_event_attr attr;
331 struct arch_hw_breakpoint_ctrl ctrl;
333 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
340 decode_ctrl_reg(uctrl, &ctrl);
341 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
345 return modify_user_hw_breakpoint(bp, &attr);
348 static int ptrace_hbp_set_addr(unsigned int note_type,
349 struct task_struct *tsk,
354 struct perf_event *bp;
355 struct perf_event_attr attr;
357 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
365 err = modify_user_hw_breakpoint(bp, &attr);
369 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
370 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
371 #define PTRACE_HBP_PAD_SZ sizeof(u32)
373 static int hw_break_get(struct task_struct *target,
374 const struct user_regset *regset,
375 unsigned int pos, unsigned int count,
376 void *kbuf, void __user *ubuf)
378 unsigned int note_type = regset->core_note_type;
379 int ret, idx = 0, offset, limit;
384 ret = ptrace_hbp_get_resource_info(note_type, &info);
388 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
394 offset = offsetof(struct user_hwdebug_state, pad);
395 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
396 offset + PTRACE_HBP_PAD_SZ);
400 /* (address, ctrl) registers */
401 offset = offsetof(struct user_hwdebug_state, dbg_regs);
402 limit = regset->n * regset->size;
403 while (count && offset < limit) {
404 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
407 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
408 offset, offset + PTRACE_HBP_ADDR_SZ);
411 offset += PTRACE_HBP_ADDR_SZ;
413 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
416 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
417 offset, offset + PTRACE_HBP_CTRL_SZ);
420 offset += PTRACE_HBP_CTRL_SZ;
422 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
424 offset + PTRACE_HBP_PAD_SZ);
427 offset += PTRACE_HBP_PAD_SZ;
434 static int hw_break_set(struct task_struct *target,
435 const struct user_regset *regset,
436 unsigned int pos, unsigned int count,
437 const void *kbuf, const void __user *ubuf)
439 unsigned int note_type = regset->core_note_type;
440 int ret, idx = 0, offset, limit;
444 /* Resource info and pad */
445 offset = offsetof(struct user_hwdebug_state, dbg_regs);
446 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
450 /* (address, ctrl) registers */
451 limit = regset->n * regset->size;
452 while (count && offset < limit) {
453 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
454 offset, offset + PTRACE_HBP_ADDR_SZ);
457 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
460 offset += PTRACE_HBP_ADDR_SZ;
462 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
463 offset, offset + PTRACE_HBP_CTRL_SZ);
466 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
469 offset += PTRACE_HBP_CTRL_SZ;
471 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
473 offset + PTRACE_HBP_PAD_SZ);
476 offset += PTRACE_HBP_PAD_SZ;
482 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
484 static int gpr_get(struct task_struct *target,
485 const struct user_regset *regset,
486 unsigned int pos, unsigned int count,
487 void *kbuf, void __user *ubuf)
489 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
490 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
493 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
494 unsigned int pos, unsigned int count,
495 const void *kbuf, const void __user *ubuf)
498 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
500 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
504 if (!valid_user_regs(&newregs, target))
507 task_pt_regs(target)->user_regs = newregs;
512 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
514 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
515 unsigned int pos, unsigned int count,
516 void *kbuf, void __user *ubuf)
518 struct user_fpsimd_state *uregs;
519 uregs = &target->thread.fpsimd_state.user_fpsimd;
520 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
523 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
524 unsigned int pos, unsigned int count,
525 const void *kbuf, const void __user *ubuf)
528 struct user_fpsimd_state newstate =
529 target->thread.fpsimd_state.user_fpsimd;
531 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
535 target->thread.fpsimd_state.user_fpsimd = newstate;
536 fpsimd_flush_task_state(target);
540 static int tls_get(struct task_struct *target, const struct user_regset *regset,
541 unsigned int pos, unsigned int count,
542 void *kbuf, void __user *ubuf)
544 unsigned long *tls = &target->thread.tp_value;
545 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
548 static int tls_set(struct task_struct *target, const struct user_regset *regset,
549 unsigned int pos, unsigned int count,
550 const void *kbuf, const void __user *ubuf)
553 unsigned long tls = target->thread.tp_value;
555 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
559 target->thread.tp_value = tls;
563 static int system_call_get(struct task_struct *target,
564 const struct user_regset *regset,
565 unsigned int pos, unsigned int count,
566 void *kbuf, void __user *ubuf)
568 int syscallno = task_pt_regs(target)->syscallno;
570 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
574 static int system_call_set(struct task_struct *target,
575 const struct user_regset *regset,
576 unsigned int pos, unsigned int count,
577 const void *kbuf, const void __user *ubuf)
581 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
585 task_pt_regs(target)->syscallno = syscallno;
589 enum aarch64_regset {
593 #ifdef CONFIG_HAVE_HW_BREAKPOINT
600 static const struct user_regset aarch64_regsets[] = {
602 .core_note_type = NT_PRSTATUS,
603 .n = sizeof(struct user_pt_regs) / sizeof(u64),
605 .align = sizeof(u64),
610 .core_note_type = NT_PRFPREG,
611 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
613 * We pretend we have 32-bit registers because the fpsr and
614 * fpcr are 32-bits wide.
617 .align = sizeof(u32),
622 .core_note_type = NT_ARM_TLS,
624 .size = sizeof(void *),
625 .align = sizeof(void *),
629 #ifdef CONFIG_HAVE_HW_BREAKPOINT
630 [REGSET_HW_BREAK] = {
631 .core_note_type = NT_ARM_HW_BREAK,
632 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
634 .align = sizeof(u32),
638 [REGSET_HW_WATCH] = {
639 .core_note_type = NT_ARM_HW_WATCH,
640 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
642 .align = sizeof(u32),
647 [REGSET_SYSTEM_CALL] = {
648 .core_note_type = NT_ARM_SYSTEM_CALL,
651 .align = sizeof(int),
652 .get = system_call_get,
653 .set = system_call_set,
657 static const struct user_regset_view user_aarch64_view = {
658 .name = "aarch64", .e_machine = EM_AARCH64,
659 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
663 #include <linux/compat.h>
670 static int compat_gpr_get(struct task_struct *target,
671 const struct user_regset *regset,
672 unsigned int pos, unsigned int count,
673 void *kbuf, void __user *ubuf)
676 unsigned int i, start, num_regs;
678 /* Calculate the number of AArch32 registers contained in count */
679 num_regs = count / regset->size;
681 /* Convert pos into an register number */
682 start = pos / regset->size;
684 if (start + num_regs > regset->n)
687 for (i = 0; i < num_regs; ++i) {
688 unsigned int idx = start + i;
693 reg = task_pt_regs(target)->pc;
696 reg = task_pt_regs(target)->pstate;
699 reg = task_pt_regs(target)->orig_x0;
702 reg = task_pt_regs(target)->regs[idx];
706 memcpy(kbuf, ®, sizeof(reg));
709 ret = copy_to_user(ubuf, ®, sizeof(reg));
722 static int compat_gpr_set(struct task_struct *target,
723 const struct user_regset *regset,
724 unsigned int pos, unsigned int count,
725 const void *kbuf, const void __user *ubuf)
727 struct pt_regs newregs;
729 unsigned int i, start, num_regs;
731 /* Calculate the number of AArch32 registers contained in count */
732 num_regs = count / regset->size;
734 /* Convert pos into an register number */
735 start = pos / regset->size;
737 if (start + num_regs > regset->n)
740 newregs = *task_pt_regs(target);
742 for (i = 0; i < num_regs; ++i) {
743 unsigned int idx = start + i;
747 memcpy(®, kbuf, sizeof(reg));
750 ret = copy_from_user(®, ubuf, sizeof(reg));
764 newregs.pstate = reg;
767 newregs.orig_x0 = reg;
770 newregs.regs[idx] = reg;
775 if (valid_user_regs(&newregs.user_regs, target))
776 *task_pt_regs(target) = newregs;
783 static int compat_vfp_get(struct task_struct *target,
784 const struct user_regset *regset,
785 unsigned int pos, unsigned int count,
786 void *kbuf, void __user *ubuf)
788 struct user_fpsimd_state *uregs;
789 compat_ulong_t fpscr;
792 uregs = &target->thread.fpsimd_state.user_fpsimd;
795 * The VFP registers are packed into the fpsimd_state, so they all sit
796 * nicely together for us. We just need to create the fpscr separately.
798 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
799 VFP_STATE_SIZE - sizeof(compat_ulong_t));
802 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
803 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
804 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
810 static int compat_vfp_set(struct task_struct *target,
811 const struct user_regset *regset,
812 unsigned int pos, unsigned int count,
813 const void *kbuf, const void __user *ubuf)
815 struct user_fpsimd_state *uregs;
816 compat_ulong_t fpscr;
819 if (pos + count > VFP_STATE_SIZE)
822 uregs = &target->thread.fpsimd_state.user_fpsimd;
824 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
825 VFP_STATE_SIZE - sizeof(compat_ulong_t));
828 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
829 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
830 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
833 fpsimd_flush_task_state(target);
837 static int compat_tls_get(struct task_struct *target,
838 const struct user_regset *regset, unsigned int pos,
839 unsigned int count, void *kbuf, void __user *ubuf)
841 compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
842 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
845 static int compat_tls_set(struct task_struct *target,
846 const struct user_regset *regset, unsigned int pos,
847 unsigned int count, const void *kbuf,
848 const void __user *ubuf)
853 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
857 target->thread.tp_value = tls;
861 static const struct user_regset aarch32_regsets[] = {
862 [REGSET_COMPAT_GPR] = {
863 .core_note_type = NT_PRSTATUS,
864 .n = COMPAT_ELF_NGREG,
865 .size = sizeof(compat_elf_greg_t),
866 .align = sizeof(compat_elf_greg_t),
867 .get = compat_gpr_get,
868 .set = compat_gpr_set
870 [REGSET_COMPAT_VFP] = {
871 .core_note_type = NT_ARM_VFP,
872 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
873 .size = sizeof(compat_ulong_t),
874 .align = sizeof(compat_ulong_t),
875 .get = compat_vfp_get,
876 .set = compat_vfp_set
880 static const struct user_regset_view user_aarch32_view = {
881 .name = "aarch32", .e_machine = EM_ARM,
882 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
885 static const struct user_regset aarch32_ptrace_regsets[] = {
887 .core_note_type = NT_PRSTATUS,
888 .n = COMPAT_ELF_NGREG,
889 .size = sizeof(compat_elf_greg_t),
890 .align = sizeof(compat_elf_greg_t),
891 .get = compat_gpr_get,
892 .set = compat_gpr_set
895 .core_note_type = NT_ARM_VFP,
896 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
897 .size = sizeof(compat_ulong_t),
898 .align = sizeof(compat_ulong_t),
899 .get = compat_vfp_get,
900 .set = compat_vfp_set
903 .core_note_type = NT_ARM_TLS,
905 .size = sizeof(compat_ulong_t),
906 .align = sizeof(compat_ulong_t),
907 .get = compat_tls_get,
908 .set = compat_tls_set,
910 #ifdef CONFIG_HAVE_HW_BREAKPOINT
911 [REGSET_HW_BREAK] = {
912 .core_note_type = NT_ARM_HW_BREAK,
913 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
915 .align = sizeof(u32),
919 [REGSET_HW_WATCH] = {
920 .core_note_type = NT_ARM_HW_WATCH,
921 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
923 .align = sizeof(u32),
928 [REGSET_SYSTEM_CALL] = {
929 .core_note_type = NT_ARM_SYSTEM_CALL,
932 .align = sizeof(int),
933 .get = system_call_get,
934 .set = system_call_set,
938 static const struct user_regset_view user_aarch32_ptrace_view = {
939 .name = "aarch32", .e_machine = EM_ARM,
940 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
943 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
944 compat_ulong_t __user *ret)
951 if (off == COMPAT_PT_TEXT_ADDR)
952 tmp = tsk->mm->start_code;
953 else if (off == COMPAT_PT_DATA_ADDR)
954 tmp = tsk->mm->start_data;
955 else if (off == COMPAT_PT_TEXT_END_ADDR)
956 tmp = tsk->mm->end_code;
957 else if (off < sizeof(compat_elf_gregset_t))
958 return copy_regset_to_user(tsk, &user_aarch32_view,
959 REGSET_COMPAT_GPR, off,
960 sizeof(compat_ulong_t), ret);
961 else if (off >= COMPAT_USER_SZ)
966 return put_user(tmp, ret);
969 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
973 mm_segment_t old_fs = get_fs();
975 if (off & 3 || off >= COMPAT_USER_SZ)
978 if (off >= sizeof(compat_elf_gregset_t))
982 ret = copy_regset_from_user(tsk, &user_aarch32_view,
983 REGSET_COMPAT_GPR, off,
984 sizeof(compat_ulong_t),
991 #ifdef CONFIG_HAVE_HW_BREAKPOINT
994 * Convert a virtual register number into an index for a thread_info
995 * breakpoint array. Breakpoints are identified using positive numbers
996 * whilst watchpoints are negative. The registers are laid out as pairs
997 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
998 * Register 0 is reserved for describing resource information.
1000 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1002 return (abs(num) - 1) >> 1;
1005 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1007 u8 num_brps, num_wrps, debug_arch, wp_len;
1010 num_brps = hw_breakpoint_slots(TYPE_INST);
1011 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1013 debug_arch = debug_monitors_arch();
1027 static int compat_ptrace_hbp_get(unsigned int note_type,
1028 struct task_struct *tsk,
1035 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1038 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1041 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1048 static int compat_ptrace_hbp_set(unsigned int note_type,
1049 struct task_struct *tsk,
1056 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1060 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1063 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1069 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1070 compat_ulong_t __user *data)
1074 mm_segment_t old_fs = get_fs();
1079 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1081 } else if (num == 0) {
1082 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1085 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1090 ret = put_user(kdata, data);
1095 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1096 compat_ulong_t __user *data)
1100 mm_segment_t old_fs = get_fs();
1105 ret = get_user(kdata, data);
1111 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1113 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1118 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1120 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1121 compat_ulong_t caddr, compat_ulong_t cdata)
1123 unsigned long addr = caddr;
1124 unsigned long data = cdata;
1125 void __user *datap = compat_ptr(data);
1129 case PTRACE_PEEKUSR:
1130 ret = compat_ptrace_read_user(child, addr, datap);
1133 case PTRACE_POKEUSR:
1134 ret = compat_ptrace_write_user(child, addr, data);
1137 case COMPAT_PTRACE_GETREGS:
1138 ret = copy_regset_to_user(child,
1141 0, sizeof(compat_elf_gregset_t),
1145 case COMPAT_PTRACE_SETREGS:
1146 ret = copy_regset_from_user(child,
1149 0, sizeof(compat_elf_gregset_t),
1153 case COMPAT_PTRACE_GET_THREAD_AREA:
1154 ret = put_user((compat_ulong_t)child->thread.tp_value,
1155 (compat_ulong_t __user *)datap);
1158 case COMPAT_PTRACE_SET_SYSCALL:
1159 task_pt_regs(child)->syscallno = data;
1163 case COMPAT_PTRACE_GETVFPREGS:
1164 ret = copy_regset_to_user(child,
1171 case COMPAT_PTRACE_SETVFPREGS:
1172 ret = copy_regset_from_user(child,
1179 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1180 case COMPAT_PTRACE_GETHBPREGS:
1181 ret = compat_ptrace_gethbpregs(child, addr, datap);
1184 case COMPAT_PTRACE_SETHBPREGS:
1185 ret = compat_ptrace_sethbpregs(child, addr, datap);
1190 ret = compat_ptrace_request(child, request, addr,
1197 #endif /* CONFIG_COMPAT */
1199 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1201 #ifdef CONFIG_COMPAT
1203 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1204 * user_aarch32_view compatible with arm32. Native ptrace requests on
1205 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1206 * access to the TLS register.
1208 if (is_compat_task())
1209 return &user_aarch32_view;
1210 else if (is_compat_thread(task_thread_info(task)))
1211 return &user_aarch32_ptrace_view;
1213 return &user_aarch64_view;
1216 long arch_ptrace(struct task_struct *child, long request,
1217 unsigned long addr, unsigned long data)
1219 return ptrace_request(child, request, addr, data);
1222 enum ptrace_syscall_dir {
1223 PTRACE_SYSCALL_ENTER = 0,
1224 PTRACE_SYSCALL_EXIT,
1227 static void tracehook_report_syscall(struct pt_regs *regs,
1228 enum ptrace_syscall_dir dir)
1231 unsigned long saved_reg;
1234 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1235 * used to denote syscall entry/exit:
1237 regno = (is_compat_task() ? 12 : 7);
1238 saved_reg = regs->regs[regno];
1239 regs->regs[regno] = dir;
1241 if (dir == PTRACE_SYSCALL_EXIT)
1242 tracehook_report_syscall_exit(regs, 0);
1243 else if (tracehook_report_syscall_entry(regs))
1244 regs->syscallno = ~0UL;
1246 regs->regs[regno] = saved_reg;
1249 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1251 /* Do the secure computing check first; failures should be fast. */
1252 if (secure_computing() == -1)
1255 if (test_thread_flag(TIF_SYSCALL_TRACE))
1256 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1258 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1259 trace_sys_enter(regs, regs->syscallno);
1261 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1262 regs->regs[2], regs->regs[3]);
1264 return regs->syscallno;
1267 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1269 audit_syscall_exit(regs);
1271 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1272 trace_sys_exit(regs, regs_return_value(regs));
1274 if (test_thread_flag(TIF_SYSCALL_TRACE))
1275 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1279 * Bits which are always architecturally RES0 per ARM DDI 0487A.h
1280 * Userspace cannot use these until they have an architectural meaning.
1281 * We also reserve IL for the kernel; SS is handled dynamically.
1283 #define SPSR_EL1_AARCH64_RES0_BITS \
1284 (GENMASK_ULL(63,32) | GENMASK_ULL(27, 22) | GENMASK_ULL(20, 10) | \
1286 #define SPSR_EL1_AARCH32_RES0_BITS \
1287 (GENMASK_ULL(63,32) | GENMASK_ULL(24, 22) | GENMASK_ULL(20,20))
1289 static int valid_compat_regs(struct user_pt_regs *regs)
1291 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1293 if (!system_supports_mixed_endian_el0()) {
1294 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1295 regs->pstate |= COMPAT_PSR_E_BIT;
1297 regs->pstate &= ~COMPAT_PSR_E_BIT;
1300 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1301 (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1302 (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1303 (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1308 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1311 regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1312 COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1313 COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1314 COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1316 regs->pstate |= PSR_MODE32_BIT;
1321 static int valid_native_regs(struct user_pt_regs *regs)
1323 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1325 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1326 (regs->pstate & PSR_D_BIT) == 0 &&
1327 (regs->pstate & PSR_A_BIT) == 0 &&
1328 (regs->pstate & PSR_I_BIT) == 0 &&
1329 (regs->pstate & PSR_F_BIT) == 0) {
1333 /* Force PSR to a valid 64-bit EL0t */
1334 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1340 * Are the current registers suitable for user mode? (used to maintain
1341 * security in signal handlers)
1343 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1345 if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1346 regs->pstate &= ~DBG_SPSR_SS;
1348 if (is_compat_thread(task_thread_info(task)))
1349 return valid_compat_regs(regs);
1351 return valid_native_regs(regs);