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/signal.h>
26 #include <linux/sched/task_stack.h>
28 #include <linux/smp.h>
29 #include <linux/ptrace.h>
30 #include <linux/user.h>
31 #include <linux/seccomp.h>
32 #include <linux/security.h>
33 #include <linux/init.h>
34 #include <linux/signal.h>
35 #include <linux/uaccess.h>
36 #include <linux/perf_event.h>
37 #include <linux/hw_breakpoint.h>
38 #include <linux/regset.h>
39 #include <linux/tracehook.h>
40 #include <linux/elf.h>
42 #include <asm/compat.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>
52 struct pt_regs_offset {
57 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
58 #define REG_OFFSET_END {.name = NULL, .offset = 0}
59 #define GPR_OFFSET_NAME(r) \
60 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
62 static const struct pt_regs_offset regoffset_table[] = {
94 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
97 REG_OFFSET_NAME(pstate),
102 * regs_query_register_offset() - query register offset from its name
103 * @name: the name of a register
105 * regs_query_register_offset() returns the offset of a register in struct
106 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
108 int regs_query_register_offset(const char *name)
110 const struct pt_regs_offset *roff;
112 for (roff = regoffset_table; roff->name != NULL; roff++)
113 if (!strcmp(roff->name, name))
119 * regs_within_kernel_stack() - check the address in the stack
120 * @regs: pt_regs which contains kernel stack pointer.
121 * @addr: address which is checked.
123 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
124 * If @addr is within the kernel stack, it returns true. If not, returns false.
126 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
128 return ((addr & ~(THREAD_SIZE - 1)) ==
129 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
130 on_irq_stack(addr, raw_smp_processor_id());
134 * regs_get_kernel_stack_nth() - get Nth entry of the stack
135 * @regs: pt_regs which contains kernel stack pointer.
136 * @n: stack entry number.
138 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
139 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
142 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
144 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
147 if (regs_within_kernel_stack(regs, (unsigned long)addr))
154 * TODO: does not yet catch signals sent when the child dies.
155 * in exit.c or in signal.c.
159 * Called by kernel/ptrace.c when detaching..
161 void ptrace_disable(struct task_struct *child)
164 * This would be better off in core code, but PTRACE_DETACH has
165 * grown its fair share of arch-specific worts and changing it
166 * is likely to cause regressions on obscure architectures.
168 user_disable_single_step(child);
171 #ifdef CONFIG_HAVE_HW_BREAKPOINT
173 * Handle hitting a HW-breakpoint.
175 static void ptrace_hbptriggered(struct perf_event *bp,
176 struct perf_sample_data *data,
177 struct pt_regs *regs)
179 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
183 .si_code = TRAP_HWBKPT,
184 .si_addr = (void __user *)(bkpt->trigger),
190 if (!is_compat_task())
193 for (i = 0; i < ARM_MAX_BRP; ++i) {
194 if (current->thread.debug.hbp_break[i] == bp) {
195 info.si_errno = (i << 1) + 1;
200 for (i = 0; i < ARM_MAX_WRP; ++i) {
201 if (current->thread.debug.hbp_watch[i] == bp) {
202 info.si_errno = -((i << 1) + 1);
209 force_sig_info(SIGTRAP, &info, current);
213 * Unregister breakpoints from this task and reset the pointers in
216 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
219 struct thread_struct *t = &tsk->thread;
221 for (i = 0; i < ARM_MAX_BRP; i++) {
222 if (t->debug.hbp_break[i]) {
223 unregister_hw_breakpoint(t->debug.hbp_break[i]);
224 t->debug.hbp_break[i] = NULL;
228 for (i = 0; i < ARM_MAX_WRP; i++) {
229 if (t->debug.hbp_watch[i]) {
230 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
231 t->debug.hbp_watch[i] = NULL;
236 void ptrace_hw_copy_thread(struct task_struct *tsk)
238 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
241 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
242 struct task_struct *tsk,
245 struct perf_event *bp = ERR_PTR(-EINVAL);
248 case NT_ARM_HW_BREAK:
249 if (idx < ARM_MAX_BRP)
250 bp = tsk->thread.debug.hbp_break[idx];
252 case NT_ARM_HW_WATCH:
253 if (idx < ARM_MAX_WRP)
254 bp = tsk->thread.debug.hbp_watch[idx];
261 static int ptrace_hbp_set_event(unsigned int note_type,
262 struct task_struct *tsk,
264 struct perf_event *bp)
269 case NT_ARM_HW_BREAK:
270 if (idx < ARM_MAX_BRP) {
271 tsk->thread.debug.hbp_break[idx] = bp;
275 case NT_ARM_HW_WATCH:
276 if (idx < ARM_MAX_WRP) {
277 tsk->thread.debug.hbp_watch[idx] = bp;
286 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
287 struct task_struct *tsk,
290 struct perf_event *bp;
291 struct perf_event_attr attr;
295 case NT_ARM_HW_BREAK:
296 type = HW_BREAKPOINT_X;
298 case NT_ARM_HW_WATCH:
299 type = HW_BREAKPOINT_RW;
302 return ERR_PTR(-EINVAL);
305 ptrace_breakpoint_init(&attr);
308 * Initialise fields to sane defaults
309 * (i.e. values that will pass validation).
312 attr.bp_len = HW_BREAKPOINT_LEN_4;
316 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
320 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
327 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
328 struct arch_hw_breakpoint_ctrl ctrl,
329 struct perf_event_attr *attr)
331 int err, len, type, offset, disabled = !ctrl.enabled;
333 attr->disabled = disabled;
337 err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
342 case NT_ARM_HW_BREAK:
343 if ((type & HW_BREAKPOINT_X) != type)
346 case NT_ARM_HW_WATCH:
347 if ((type & HW_BREAKPOINT_RW) != type)
355 attr->bp_type = type;
356 attr->bp_addr += offset;
361 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
367 case NT_ARM_HW_BREAK:
368 num = hw_breakpoint_slots(TYPE_INST);
370 case NT_ARM_HW_WATCH:
371 num = hw_breakpoint_slots(TYPE_DATA);
377 reg |= debug_monitors_arch();
385 static int ptrace_hbp_get_ctrl(unsigned int note_type,
386 struct task_struct *tsk,
390 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
395 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
399 static int ptrace_hbp_get_addr(unsigned int note_type,
400 struct task_struct *tsk,
404 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
409 *addr = bp ? counter_arch_bp(bp)->address : 0;
413 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
414 struct task_struct *tsk,
417 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
420 bp = ptrace_hbp_create(note_type, tsk, idx);
425 static int ptrace_hbp_set_ctrl(unsigned int note_type,
426 struct task_struct *tsk,
431 struct perf_event *bp;
432 struct perf_event_attr attr;
433 struct arch_hw_breakpoint_ctrl ctrl;
435 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
442 decode_ctrl_reg(uctrl, &ctrl);
443 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
447 return modify_user_hw_breakpoint(bp, &attr);
450 static int ptrace_hbp_set_addr(unsigned int note_type,
451 struct task_struct *tsk,
456 struct perf_event *bp;
457 struct perf_event_attr attr;
459 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
467 err = modify_user_hw_breakpoint(bp, &attr);
471 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
472 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
473 #define PTRACE_HBP_PAD_SZ sizeof(u32)
475 static int hw_break_get(struct task_struct *target,
476 const struct user_regset *regset,
477 unsigned int pos, unsigned int count,
478 void *kbuf, void __user *ubuf)
480 unsigned int note_type = regset->core_note_type;
481 int ret, idx = 0, offset, limit;
486 ret = ptrace_hbp_get_resource_info(note_type, &info);
490 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
496 offset = offsetof(struct user_hwdebug_state, pad);
497 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
498 offset + PTRACE_HBP_PAD_SZ);
502 /* (address, ctrl) registers */
503 offset = offsetof(struct user_hwdebug_state, dbg_regs);
504 limit = regset->n * regset->size;
505 while (count && offset < limit) {
506 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
509 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
510 offset, offset + PTRACE_HBP_ADDR_SZ);
513 offset += PTRACE_HBP_ADDR_SZ;
515 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
518 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
519 offset, offset + PTRACE_HBP_CTRL_SZ);
522 offset += PTRACE_HBP_CTRL_SZ;
524 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
526 offset + PTRACE_HBP_PAD_SZ);
529 offset += PTRACE_HBP_PAD_SZ;
536 static int hw_break_set(struct task_struct *target,
537 const struct user_regset *regset,
538 unsigned int pos, unsigned int count,
539 const void *kbuf, const void __user *ubuf)
541 unsigned int note_type = regset->core_note_type;
542 int ret, idx = 0, offset, limit;
546 /* Resource info and pad */
547 offset = offsetof(struct user_hwdebug_state, dbg_regs);
548 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
552 /* (address, ctrl) registers */
553 limit = regset->n * regset->size;
554 while (count && offset < limit) {
555 if (count < PTRACE_HBP_ADDR_SZ)
557 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
558 offset, offset + PTRACE_HBP_ADDR_SZ);
561 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
564 offset += PTRACE_HBP_ADDR_SZ;
568 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
569 offset, offset + PTRACE_HBP_CTRL_SZ);
572 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
575 offset += PTRACE_HBP_CTRL_SZ;
577 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
579 offset + PTRACE_HBP_PAD_SZ);
582 offset += PTRACE_HBP_PAD_SZ;
588 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
590 static int gpr_get(struct task_struct *target,
591 const struct user_regset *regset,
592 unsigned int pos, unsigned int count,
593 void *kbuf, void __user *ubuf)
595 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
596 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
599 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
600 unsigned int pos, unsigned int count,
601 const void *kbuf, const void __user *ubuf)
604 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
606 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
610 if (!valid_user_regs(&newregs, target))
613 task_pt_regs(target)->user_regs = newregs;
618 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
620 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
621 unsigned int pos, unsigned int count,
622 void *kbuf, void __user *ubuf)
624 struct user_fpsimd_state *uregs;
625 uregs = &target->thread.fpsimd_state.user_fpsimd;
627 if (target == current)
628 fpsimd_preserve_current_state();
630 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
633 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
634 unsigned int pos, unsigned int count,
635 const void *kbuf, const void __user *ubuf)
638 struct user_fpsimd_state newstate =
639 target->thread.fpsimd_state.user_fpsimd;
641 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
645 target->thread.fpsimd_state.user_fpsimd = newstate;
646 fpsimd_flush_task_state(target);
650 static int tls_get(struct task_struct *target, const struct user_regset *regset,
651 unsigned int pos, unsigned int count,
652 void *kbuf, void __user *ubuf)
654 unsigned long *tls = &target->thread.tp_value;
656 if (target == current)
657 tls_preserve_current_state();
659 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
662 static int tls_set(struct task_struct *target, const struct user_regset *regset,
663 unsigned int pos, unsigned int count,
664 const void *kbuf, const void __user *ubuf)
667 unsigned long tls = target->thread.tp_value;
669 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
673 target->thread.tp_value = tls;
677 static int system_call_get(struct task_struct *target,
678 const struct user_regset *regset,
679 unsigned int pos, unsigned int count,
680 void *kbuf, void __user *ubuf)
682 int syscallno = task_pt_regs(target)->syscallno;
684 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
688 static int system_call_set(struct task_struct *target,
689 const struct user_regset *regset,
690 unsigned int pos, unsigned int count,
691 const void *kbuf, const void __user *ubuf)
693 int syscallno = task_pt_regs(target)->syscallno;
696 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
700 task_pt_regs(target)->syscallno = syscallno;
704 enum aarch64_regset {
708 #ifdef CONFIG_HAVE_HW_BREAKPOINT
715 static const struct user_regset aarch64_regsets[] = {
717 .core_note_type = NT_PRSTATUS,
718 .n = sizeof(struct user_pt_regs) / sizeof(u64),
720 .align = sizeof(u64),
725 .core_note_type = NT_PRFPREG,
726 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
728 * We pretend we have 32-bit registers because the fpsr and
729 * fpcr are 32-bits wide.
732 .align = sizeof(u32),
737 .core_note_type = NT_ARM_TLS,
739 .size = sizeof(void *),
740 .align = sizeof(void *),
744 #ifdef CONFIG_HAVE_HW_BREAKPOINT
745 [REGSET_HW_BREAK] = {
746 .core_note_type = NT_ARM_HW_BREAK,
747 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
749 .align = sizeof(u32),
753 [REGSET_HW_WATCH] = {
754 .core_note_type = NT_ARM_HW_WATCH,
755 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
757 .align = sizeof(u32),
762 [REGSET_SYSTEM_CALL] = {
763 .core_note_type = NT_ARM_SYSTEM_CALL,
766 .align = sizeof(int),
767 .get = system_call_get,
768 .set = system_call_set,
772 static const struct user_regset_view user_aarch64_view = {
773 .name = "aarch64", .e_machine = EM_AARCH64,
774 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
778 #include <linux/compat.h>
785 static int compat_gpr_get(struct task_struct *target,
786 const struct user_regset *regset,
787 unsigned int pos, unsigned int count,
788 void *kbuf, void __user *ubuf)
791 unsigned int i, start, num_regs;
793 /* Calculate the number of AArch32 registers contained in count */
794 num_regs = count / regset->size;
796 /* Convert pos into an register number */
797 start = pos / regset->size;
799 if (start + num_regs > regset->n)
802 for (i = 0; i < num_regs; ++i) {
803 unsigned int idx = start + i;
808 reg = task_pt_regs(target)->pc;
811 reg = task_pt_regs(target)->pstate;
814 reg = task_pt_regs(target)->orig_x0;
817 reg = task_pt_regs(target)->regs[idx];
821 memcpy(kbuf, ®, sizeof(reg));
824 ret = copy_to_user(ubuf, ®, sizeof(reg));
837 static int compat_gpr_set(struct task_struct *target,
838 const struct user_regset *regset,
839 unsigned int pos, unsigned int count,
840 const void *kbuf, const void __user *ubuf)
842 struct pt_regs newregs;
844 unsigned int i, start, num_regs;
846 /* Calculate the number of AArch32 registers contained in count */
847 num_regs = count / regset->size;
849 /* Convert pos into an register number */
850 start = pos / regset->size;
852 if (start + num_regs > regset->n)
855 newregs = *task_pt_regs(target);
857 for (i = 0; i < num_regs; ++i) {
858 unsigned int idx = start + i;
862 memcpy(®, kbuf, sizeof(reg));
865 ret = copy_from_user(®, ubuf, sizeof(reg));
879 newregs.pstate = reg;
882 newregs.orig_x0 = reg;
885 newregs.regs[idx] = reg;
890 if (valid_user_regs(&newregs.user_regs, target))
891 *task_pt_regs(target) = newregs;
898 static int compat_vfp_get(struct task_struct *target,
899 const struct user_regset *regset,
900 unsigned int pos, unsigned int count,
901 void *kbuf, void __user *ubuf)
903 struct user_fpsimd_state *uregs;
904 compat_ulong_t fpscr;
905 int ret, vregs_end_pos;
907 uregs = &target->thread.fpsimd_state.user_fpsimd;
909 if (target == current)
910 fpsimd_preserve_current_state();
913 * The VFP registers are packed into the fpsimd_state, so they all sit
914 * nicely together for us. We just need to create the fpscr separately.
916 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
917 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
921 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
922 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
924 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
925 vregs_end_pos, VFP_STATE_SIZE);
931 static int compat_vfp_set(struct task_struct *target,
932 const struct user_regset *regset,
933 unsigned int pos, unsigned int count,
934 const void *kbuf, const void __user *ubuf)
936 struct user_fpsimd_state *uregs;
937 compat_ulong_t fpscr;
938 int ret, vregs_end_pos;
940 uregs = &target->thread.fpsimd_state.user_fpsimd;
942 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
943 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
947 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
948 vregs_end_pos, VFP_STATE_SIZE);
950 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
951 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
955 fpsimd_flush_task_state(target);
959 static int compat_tls_get(struct task_struct *target,
960 const struct user_regset *regset, unsigned int pos,
961 unsigned int count, void *kbuf, void __user *ubuf)
963 compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
964 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
967 static int compat_tls_set(struct task_struct *target,
968 const struct user_regset *regset, unsigned int pos,
969 unsigned int count, const void *kbuf,
970 const void __user *ubuf)
973 compat_ulong_t tls = target->thread.tp_value;
975 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
979 target->thread.tp_value = tls;
983 static const struct user_regset aarch32_regsets[] = {
984 [REGSET_COMPAT_GPR] = {
985 .core_note_type = NT_PRSTATUS,
986 .n = COMPAT_ELF_NGREG,
987 .size = sizeof(compat_elf_greg_t),
988 .align = sizeof(compat_elf_greg_t),
989 .get = compat_gpr_get,
990 .set = compat_gpr_set
992 [REGSET_COMPAT_VFP] = {
993 .core_note_type = NT_ARM_VFP,
994 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
995 .size = sizeof(compat_ulong_t),
996 .align = sizeof(compat_ulong_t),
997 .get = compat_vfp_get,
998 .set = compat_vfp_set
1002 static const struct user_regset_view user_aarch32_view = {
1003 .name = "aarch32", .e_machine = EM_ARM,
1004 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1007 static const struct user_regset aarch32_ptrace_regsets[] = {
1009 .core_note_type = NT_PRSTATUS,
1010 .n = COMPAT_ELF_NGREG,
1011 .size = sizeof(compat_elf_greg_t),
1012 .align = sizeof(compat_elf_greg_t),
1013 .get = compat_gpr_get,
1014 .set = compat_gpr_set
1017 .core_note_type = NT_ARM_VFP,
1018 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1019 .size = sizeof(compat_ulong_t),
1020 .align = sizeof(compat_ulong_t),
1021 .get = compat_vfp_get,
1022 .set = compat_vfp_set
1025 .core_note_type = NT_ARM_TLS,
1027 .size = sizeof(compat_ulong_t),
1028 .align = sizeof(compat_ulong_t),
1029 .get = compat_tls_get,
1030 .set = compat_tls_set,
1032 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1033 [REGSET_HW_BREAK] = {
1034 .core_note_type = NT_ARM_HW_BREAK,
1035 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1036 .size = sizeof(u32),
1037 .align = sizeof(u32),
1038 .get = hw_break_get,
1039 .set = hw_break_set,
1041 [REGSET_HW_WATCH] = {
1042 .core_note_type = NT_ARM_HW_WATCH,
1043 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1044 .size = sizeof(u32),
1045 .align = sizeof(u32),
1046 .get = hw_break_get,
1047 .set = hw_break_set,
1050 [REGSET_SYSTEM_CALL] = {
1051 .core_note_type = NT_ARM_SYSTEM_CALL,
1053 .size = sizeof(int),
1054 .align = sizeof(int),
1055 .get = system_call_get,
1056 .set = system_call_set,
1060 static const struct user_regset_view user_aarch32_ptrace_view = {
1061 .name = "aarch32", .e_machine = EM_ARM,
1062 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1065 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1066 compat_ulong_t __user *ret)
1073 if (off == COMPAT_PT_TEXT_ADDR)
1074 tmp = tsk->mm->start_code;
1075 else if (off == COMPAT_PT_DATA_ADDR)
1076 tmp = tsk->mm->start_data;
1077 else if (off == COMPAT_PT_TEXT_END_ADDR)
1078 tmp = tsk->mm->end_code;
1079 else if (off < sizeof(compat_elf_gregset_t))
1080 return copy_regset_to_user(tsk, &user_aarch32_view,
1081 REGSET_COMPAT_GPR, off,
1082 sizeof(compat_ulong_t), ret);
1083 else if (off >= COMPAT_USER_SZ)
1088 return put_user(tmp, ret);
1091 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1095 mm_segment_t old_fs = get_fs();
1097 if (off & 3 || off >= COMPAT_USER_SZ)
1100 if (off >= sizeof(compat_elf_gregset_t))
1104 ret = copy_regset_from_user(tsk, &user_aarch32_view,
1105 REGSET_COMPAT_GPR, off,
1106 sizeof(compat_ulong_t),
1113 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1116 * Convert a virtual register number into an index for a thread_info
1117 * breakpoint array. Breakpoints are identified using positive numbers
1118 * whilst watchpoints are negative. The registers are laid out as pairs
1119 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1120 * Register 0 is reserved for describing resource information.
1122 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1124 return (abs(num) - 1) >> 1;
1127 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1129 u8 num_brps, num_wrps, debug_arch, wp_len;
1132 num_brps = hw_breakpoint_slots(TYPE_INST);
1133 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1135 debug_arch = debug_monitors_arch();
1149 static int compat_ptrace_hbp_get(unsigned int note_type,
1150 struct task_struct *tsk,
1157 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1160 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1163 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1170 static int compat_ptrace_hbp_set(unsigned int note_type,
1171 struct task_struct *tsk,
1178 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1182 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1185 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1191 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1192 compat_ulong_t __user *data)
1196 mm_segment_t old_fs = get_fs();
1201 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1203 } else if (num == 0) {
1204 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1207 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1212 ret = put_user(kdata, data);
1217 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1218 compat_ulong_t __user *data)
1222 mm_segment_t old_fs = get_fs();
1227 ret = get_user(kdata, data);
1233 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1235 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1240 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1242 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1243 compat_ulong_t caddr, compat_ulong_t cdata)
1245 unsigned long addr = caddr;
1246 unsigned long data = cdata;
1247 void __user *datap = compat_ptr(data);
1251 case PTRACE_PEEKUSR:
1252 ret = compat_ptrace_read_user(child, addr, datap);
1255 case PTRACE_POKEUSR:
1256 ret = compat_ptrace_write_user(child, addr, data);
1259 case COMPAT_PTRACE_GETREGS:
1260 ret = copy_regset_to_user(child,
1263 0, sizeof(compat_elf_gregset_t),
1267 case COMPAT_PTRACE_SETREGS:
1268 ret = copy_regset_from_user(child,
1271 0, sizeof(compat_elf_gregset_t),
1275 case COMPAT_PTRACE_GET_THREAD_AREA:
1276 ret = put_user((compat_ulong_t)child->thread.tp_value,
1277 (compat_ulong_t __user *)datap);
1280 case COMPAT_PTRACE_SET_SYSCALL:
1281 task_pt_regs(child)->syscallno = data;
1285 case COMPAT_PTRACE_GETVFPREGS:
1286 ret = copy_regset_to_user(child,
1293 case COMPAT_PTRACE_SETVFPREGS:
1294 ret = copy_regset_from_user(child,
1301 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1302 case COMPAT_PTRACE_GETHBPREGS:
1303 ret = compat_ptrace_gethbpregs(child, addr, datap);
1306 case COMPAT_PTRACE_SETHBPREGS:
1307 ret = compat_ptrace_sethbpregs(child, addr, datap);
1312 ret = compat_ptrace_request(child, request, addr,
1319 #endif /* CONFIG_COMPAT */
1321 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1323 #ifdef CONFIG_COMPAT
1325 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1326 * user_aarch32_view compatible with arm32. Native ptrace requests on
1327 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1328 * access to the TLS register.
1330 if (is_compat_task())
1331 return &user_aarch32_view;
1332 else if (is_compat_thread(task_thread_info(task)))
1333 return &user_aarch32_ptrace_view;
1335 return &user_aarch64_view;
1338 long arch_ptrace(struct task_struct *child, long request,
1339 unsigned long addr, unsigned long data)
1341 return ptrace_request(child, request, addr, data);
1344 enum ptrace_syscall_dir {
1345 PTRACE_SYSCALL_ENTER = 0,
1346 PTRACE_SYSCALL_EXIT,
1349 static void tracehook_report_syscall(struct pt_regs *regs,
1350 enum ptrace_syscall_dir dir)
1353 unsigned long saved_reg;
1356 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1357 * used to denote syscall entry/exit:
1359 regno = (is_compat_task() ? 12 : 7);
1360 saved_reg = regs->regs[regno];
1361 regs->regs[regno] = dir;
1363 if (dir == PTRACE_SYSCALL_EXIT)
1364 tracehook_report_syscall_exit(regs, 0);
1365 else if (tracehook_report_syscall_entry(regs))
1366 regs->syscallno = ~0UL;
1368 regs->regs[regno] = saved_reg;
1371 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1373 if (test_thread_flag(TIF_SYSCALL_TRACE))
1374 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1376 /* Do the secure computing after ptrace; failures should be fast. */
1377 if (secure_computing(NULL) == -1)
1380 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1381 trace_sys_enter(regs, regs->syscallno);
1383 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1384 regs->regs[2], regs->regs[3]);
1386 return regs->syscallno;
1389 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1391 audit_syscall_exit(regs);
1393 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1394 trace_sys_exit(regs, regs_return_value(regs));
1396 if (test_thread_flag(TIF_SYSCALL_TRACE))
1397 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1401 * Bits which are always architecturally RES0 per ARM DDI 0487A.h
1402 * Userspace cannot use these until they have an architectural meaning.
1403 * We also reserve IL for the kernel; SS is handled dynamically.
1405 #define SPSR_EL1_AARCH64_RES0_BITS \
1406 (GENMASK_ULL(63,32) | GENMASK_ULL(27, 22) | GENMASK_ULL(20, 10) | \
1408 #define SPSR_EL1_AARCH32_RES0_BITS \
1409 (GENMASK_ULL(63,32) | GENMASK_ULL(24, 22) | GENMASK_ULL(20,20))
1411 static int valid_compat_regs(struct user_pt_regs *regs)
1413 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1415 if (!system_supports_mixed_endian_el0()) {
1416 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1417 regs->pstate |= COMPAT_PSR_E_BIT;
1419 regs->pstate &= ~COMPAT_PSR_E_BIT;
1422 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1423 (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1424 (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1425 (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1430 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1433 regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1434 COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1435 COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1436 COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1438 regs->pstate |= PSR_MODE32_BIT;
1443 static int valid_native_regs(struct user_pt_regs *regs)
1445 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1447 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1448 (regs->pstate & PSR_D_BIT) == 0 &&
1449 (regs->pstate & PSR_A_BIT) == 0 &&
1450 (regs->pstate & PSR_I_BIT) == 0 &&
1451 (regs->pstate & PSR_F_BIT) == 0) {
1455 /* Force PSR to a valid 64-bit EL0t */
1456 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1462 * Are the current registers suitable for user mode? (used to maintain
1463 * security in signal handlers)
1465 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1467 if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1468 regs->pstate &= ~DBG_SPSR_SS;
1470 if (is_compat_thread(task_thread_info(task)))
1471 return valid_compat_regs(regs);
1473 return valid_native_regs(regs);