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/nospec.h>
29 #include <linux/smp.h>
30 #include <linux/ptrace.h>
31 #include <linux/user.h>
32 #include <linux/seccomp.h>
33 #include <linux/security.h>
34 #include <linux/init.h>
35 #include <linux/signal.h>
36 #include <linux/uaccess.h>
37 #include <linux/perf_event.h>
38 #include <linux/hw_breakpoint.h>
39 #include <linux/regset.h>
40 #include <linux/tracehook.h>
41 #include <linux/elf.h>
43 #include <asm/compat.h>
44 #include <asm/debug-monitors.h>
45 #include <asm/pgtable.h>
46 #include <asm/stacktrace.h>
47 #include <asm/syscall.h>
48 #include <asm/traps.h>
49 #include <asm/system_misc.h>
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/syscalls.h>
54 struct pt_regs_offset {
59 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
60 #define REG_OFFSET_END {.name = NULL, .offset = 0}
61 #define GPR_OFFSET_NAME(r) \
62 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
64 static const struct pt_regs_offset regoffset_table[] = {
96 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
99 REG_OFFSET_NAME(pstate),
104 * regs_query_register_offset() - query register offset from its name
105 * @name: the name of a register
107 * regs_query_register_offset() returns the offset of a register in struct
108 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
110 int regs_query_register_offset(const char *name)
112 const struct pt_regs_offset *roff;
114 for (roff = regoffset_table; roff->name != NULL; roff++)
115 if (!strcmp(roff->name, name))
121 * regs_within_kernel_stack() - check the address in the stack
122 * @regs: pt_regs which contains kernel stack pointer.
123 * @addr: address which is checked.
125 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
126 * If @addr is within the kernel stack, it returns true. If not, returns false.
128 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
130 return ((addr & ~(THREAD_SIZE - 1)) ==
131 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
136 * regs_get_kernel_stack_nth() - get Nth entry of the stack
137 * @regs: pt_regs which contains kernel stack pointer.
138 * @n: stack entry number.
140 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
141 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
144 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
146 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
149 if (regs_within_kernel_stack(regs, (unsigned long)addr))
156 * TODO: does not yet catch signals sent when the child dies.
157 * in exit.c or in signal.c.
161 * Called by kernel/ptrace.c when detaching..
163 void ptrace_disable(struct task_struct *child)
166 * This would be better off in core code, but PTRACE_DETACH has
167 * grown its fair share of arch-specific worts and changing it
168 * is likely to cause regressions on obscure architectures.
170 user_disable_single_step(child);
173 #ifdef CONFIG_HAVE_HW_BREAKPOINT
175 * Handle hitting a HW-breakpoint.
177 static void ptrace_hbptriggered(struct perf_event *bp,
178 struct perf_sample_data *data,
179 struct pt_regs *regs)
181 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
185 .si_code = TRAP_HWBKPT,
186 .si_addr = (void __user *)(bkpt->trigger),
192 if (!is_compat_task())
195 for (i = 0; i < ARM_MAX_BRP; ++i) {
196 if (current->thread.debug.hbp_break[i] == bp) {
197 info.si_errno = (i << 1) + 1;
202 for (i = 0; i < ARM_MAX_WRP; ++i) {
203 if (current->thread.debug.hbp_watch[i] == bp) {
204 info.si_errno = -((i << 1) + 1);
211 force_sig_info(SIGTRAP, &info, current);
215 * Unregister breakpoints from this task and reset the pointers in
218 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
221 struct thread_struct *t = &tsk->thread;
223 for (i = 0; i < ARM_MAX_BRP; i++) {
224 if (t->debug.hbp_break[i]) {
225 unregister_hw_breakpoint(t->debug.hbp_break[i]);
226 t->debug.hbp_break[i] = NULL;
230 for (i = 0; i < ARM_MAX_WRP; i++) {
231 if (t->debug.hbp_watch[i]) {
232 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
233 t->debug.hbp_watch[i] = NULL;
238 void ptrace_hw_copy_thread(struct task_struct *tsk)
240 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
243 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
244 struct task_struct *tsk,
247 struct perf_event *bp = ERR_PTR(-EINVAL);
250 case NT_ARM_HW_BREAK:
251 if (idx >= ARM_MAX_BRP)
253 idx = array_index_nospec(idx, ARM_MAX_BRP);
254 bp = tsk->thread.debug.hbp_break[idx];
256 case NT_ARM_HW_WATCH:
257 if (idx >= ARM_MAX_WRP)
259 idx = array_index_nospec(idx, ARM_MAX_WRP);
260 bp = tsk->thread.debug.hbp_watch[idx];
268 static int ptrace_hbp_set_event(unsigned int note_type,
269 struct task_struct *tsk,
271 struct perf_event *bp)
276 case NT_ARM_HW_BREAK:
277 if (idx >= ARM_MAX_BRP)
279 idx = array_index_nospec(idx, ARM_MAX_BRP);
280 tsk->thread.debug.hbp_break[idx] = bp;
283 case NT_ARM_HW_WATCH:
284 if (idx >= ARM_MAX_WRP)
286 idx = array_index_nospec(idx, ARM_MAX_WRP);
287 tsk->thread.debug.hbp_watch[idx] = bp;
296 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
297 struct task_struct *tsk,
300 struct perf_event *bp;
301 struct perf_event_attr attr;
305 case NT_ARM_HW_BREAK:
306 type = HW_BREAKPOINT_X;
308 case NT_ARM_HW_WATCH:
309 type = HW_BREAKPOINT_RW;
312 return ERR_PTR(-EINVAL);
315 ptrace_breakpoint_init(&attr);
318 * Initialise fields to sane defaults
319 * (i.e. values that will pass validation).
322 attr.bp_len = HW_BREAKPOINT_LEN_4;
326 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
330 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
337 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
338 struct arch_hw_breakpoint_ctrl ctrl,
339 struct perf_event_attr *attr)
341 int err, len, type, offset, disabled = !ctrl.enabled;
343 attr->disabled = disabled;
347 err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
352 case NT_ARM_HW_BREAK:
353 if ((type & HW_BREAKPOINT_X) != type)
356 case NT_ARM_HW_WATCH:
357 if ((type & HW_BREAKPOINT_RW) != type)
365 attr->bp_type = type;
366 attr->bp_addr += offset;
371 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
377 case NT_ARM_HW_BREAK:
378 num = hw_breakpoint_slots(TYPE_INST);
380 case NT_ARM_HW_WATCH:
381 num = hw_breakpoint_slots(TYPE_DATA);
387 reg |= debug_monitors_arch();
395 static int ptrace_hbp_get_ctrl(unsigned int note_type,
396 struct task_struct *tsk,
400 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
405 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
409 static int ptrace_hbp_get_addr(unsigned int note_type,
410 struct task_struct *tsk,
414 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
419 *addr = bp ? counter_arch_bp(bp)->address : 0;
423 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
424 struct task_struct *tsk,
427 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
430 bp = ptrace_hbp_create(note_type, tsk, idx);
435 static int ptrace_hbp_set_ctrl(unsigned int note_type,
436 struct task_struct *tsk,
441 struct perf_event *bp;
442 struct perf_event_attr attr;
443 struct arch_hw_breakpoint_ctrl ctrl;
445 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
452 decode_ctrl_reg(uctrl, &ctrl);
453 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
457 return modify_user_hw_breakpoint(bp, &attr);
460 static int ptrace_hbp_set_addr(unsigned int note_type,
461 struct task_struct *tsk,
466 struct perf_event *bp;
467 struct perf_event_attr attr;
469 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
477 err = modify_user_hw_breakpoint(bp, &attr);
481 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
482 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
483 #define PTRACE_HBP_PAD_SZ sizeof(u32)
485 static int hw_break_get(struct task_struct *target,
486 const struct user_regset *regset,
487 unsigned int pos, unsigned int count,
488 void *kbuf, void __user *ubuf)
490 unsigned int note_type = regset->core_note_type;
491 int ret, idx = 0, offset, limit;
496 ret = ptrace_hbp_get_resource_info(note_type, &info);
500 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
506 offset = offsetof(struct user_hwdebug_state, pad);
507 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
508 offset + PTRACE_HBP_PAD_SZ);
512 /* (address, ctrl) registers */
513 offset = offsetof(struct user_hwdebug_state, dbg_regs);
514 limit = regset->n * regset->size;
515 while (count && offset < limit) {
516 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
519 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
520 offset, offset + PTRACE_HBP_ADDR_SZ);
523 offset += PTRACE_HBP_ADDR_SZ;
525 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
528 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
529 offset, offset + PTRACE_HBP_CTRL_SZ);
532 offset += PTRACE_HBP_CTRL_SZ;
534 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
536 offset + PTRACE_HBP_PAD_SZ);
539 offset += PTRACE_HBP_PAD_SZ;
546 static int hw_break_set(struct task_struct *target,
547 const struct user_regset *regset,
548 unsigned int pos, unsigned int count,
549 const void *kbuf, const void __user *ubuf)
551 unsigned int note_type = regset->core_note_type;
552 int ret, idx = 0, offset, limit;
556 /* Resource info and pad */
557 offset = offsetof(struct user_hwdebug_state, dbg_regs);
558 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
562 /* (address, ctrl) registers */
563 limit = regset->n * regset->size;
564 while (count && offset < limit) {
565 if (count < PTRACE_HBP_ADDR_SZ)
567 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
568 offset, offset + PTRACE_HBP_ADDR_SZ);
571 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
574 offset += PTRACE_HBP_ADDR_SZ;
578 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
579 offset, offset + PTRACE_HBP_CTRL_SZ);
582 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
585 offset += PTRACE_HBP_CTRL_SZ;
587 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
589 offset + PTRACE_HBP_PAD_SZ);
592 offset += PTRACE_HBP_PAD_SZ;
598 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
600 static int gpr_get(struct task_struct *target,
601 const struct user_regset *regset,
602 unsigned int pos, unsigned int count,
603 void *kbuf, void __user *ubuf)
605 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
606 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
609 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
610 unsigned int pos, unsigned int count,
611 const void *kbuf, const void __user *ubuf)
614 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
616 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
620 if (!valid_user_regs(&newregs, target))
623 task_pt_regs(target)->user_regs = newregs;
627 static int fpr_active(struct task_struct *target, const struct user_regset *regset)
629 if (!system_supports_fpsimd())
635 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
637 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
638 unsigned int pos, unsigned int count,
639 void *kbuf, void __user *ubuf)
641 struct user_fpsimd_state *uregs;
642 uregs = &target->thread.fpsimd_state.user_fpsimd;
644 if (!system_supports_fpsimd())
647 if (target == current)
648 fpsimd_preserve_current_state();
650 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
653 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
654 unsigned int pos, unsigned int count,
655 const void *kbuf, const void __user *ubuf)
658 struct user_fpsimd_state newstate =
659 target->thread.fpsimd_state.user_fpsimd;
661 if (!system_supports_fpsimd())
664 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
668 target->thread.fpsimd_state.user_fpsimd = newstate;
669 fpsimd_flush_task_state(target);
673 static int tls_get(struct task_struct *target, const struct user_regset *regset,
674 unsigned int pos, unsigned int count,
675 void *kbuf, void __user *ubuf)
677 unsigned long *tls = &target->thread.tp_value;
679 if (target == current)
680 tls_preserve_current_state();
682 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
685 static int tls_set(struct task_struct *target, const struct user_regset *regset,
686 unsigned int pos, unsigned int count,
687 const void *kbuf, const void __user *ubuf)
690 unsigned long tls = target->thread.tp_value;
692 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
696 target->thread.tp_value = tls;
700 static int system_call_get(struct task_struct *target,
701 const struct user_regset *regset,
702 unsigned int pos, unsigned int count,
703 void *kbuf, void __user *ubuf)
705 int syscallno = task_pt_regs(target)->syscallno;
707 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
711 static int system_call_set(struct task_struct *target,
712 const struct user_regset *regset,
713 unsigned int pos, unsigned int count,
714 const void *kbuf, const void __user *ubuf)
716 int syscallno = task_pt_regs(target)->syscallno;
719 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
723 task_pt_regs(target)->syscallno = syscallno;
727 enum aarch64_regset {
731 #ifdef CONFIG_HAVE_HW_BREAKPOINT
738 static const struct user_regset aarch64_regsets[] = {
740 .core_note_type = NT_PRSTATUS,
741 .n = sizeof(struct user_pt_regs) / sizeof(u64),
743 .align = sizeof(u64),
748 .core_note_type = NT_PRFPREG,
749 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
751 * We pretend we have 32-bit registers because the fpsr and
752 * fpcr are 32-bits wide.
755 .align = sizeof(u32),
756 .active = fpr_active,
761 .core_note_type = NT_ARM_TLS,
763 .size = sizeof(void *),
764 .align = sizeof(void *),
768 #ifdef CONFIG_HAVE_HW_BREAKPOINT
769 [REGSET_HW_BREAK] = {
770 .core_note_type = NT_ARM_HW_BREAK,
771 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
773 .align = sizeof(u32),
777 [REGSET_HW_WATCH] = {
778 .core_note_type = NT_ARM_HW_WATCH,
779 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
781 .align = sizeof(u32),
786 [REGSET_SYSTEM_CALL] = {
787 .core_note_type = NT_ARM_SYSTEM_CALL,
790 .align = sizeof(int),
791 .get = system_call_get,
792 .set = system_call_set,
796 static const struct user_regset_view user_aarch64_view = {
797 .name = "aarch64", .e_machine = EM_AARCH64,
798 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
802 #include <linux/compat.h>
809 static int compat_gpr_get(struct task_struct *target,
810 const struct user_regset *regset,
811 unsigned int pos, unsigned int count,
812 void *kbuf, void __user *ubuf)
815 unsigned int i, start, num_regs;
817 /* Calculate the number of AArch32 registers contained in count */
818 num_regs = count / regset->size;
820 /* Convert pos into an register number */
821 start = pos / regset->size;
823 if (start + num_regs > regset->n)
826 for (i = 0; i < num_regs; ++i) {
827 unsigned int idx = start + i;
832 reg = task_pt_regs(target)->pc;
835 reg = task_pt_regs(target)->pstate;
836 reg = pstate_to_compat_psr(reg);
839 reg = task_pt_regs(target)->orig_x0;
842 reg = task_pt_regs(target)->regs[idx];
846 memcpy(kbuf, ®, sizeof(reg));
849 ret = copy_to_user(ubuf, ®, sizeof(reg));
862 static int compat_gpr_set(struct task_struct *target,
863 const struct user_regset *regset,
864 unsigned int pos, unsigned int count,
865 const void *kbuf, const void __user *ubuf)
867 struct pt_regs newregs;
869 unsigned int i, start, num_regs;
871 /* Calculate the number of AArch32 registers contained in count */
872 num_regs = count / regset->size;
874 /* Convert pos into an register number */
875 start = pos / regset->size;
877 if (start + num_regs > regset->n)
880 newregs = *task_pt_regs(target);
882 for (i = 0; i < num_regs; ++i) {
883 unsigned int idx = start + i;
887 memcpy(®, kbuf, sizeof(reg));
890 ret = copy_from_user(®, ubuf, sizeof(reg));
904 reg = compat_psr_to_pstate(reg);
905 newregs.pstate = reg;
908 newregs.orig_x0 = reg;
911 newregs.regs[idx] = reg;
916 if (valid_user_regs(&newregs.user_regs, target))
917 *task_pt_regs(target) = newregs;
924 static int compat_vfp_get(struct task_struct *target,
925 const struct user_regset *regset,
926 unsigned int pos, unsigned int count,
927 void *kbuf, void __user *ubuf)
929 struct user_fpsimd_state *uregs;
930 compat_ulong_t fpscr;
931 int ret, vregs_end_pos;
933 if (!system_supports_fpsimd())
936 uregs = &target->thread.fpsimd_state.user_fpsimd;
938 if (target == current)
939 fpsimd_preserve_current_state();
942 * The VFP registers are packed into the fpsimd_state, so they all sit
943 * nicely together for us. We just need to create the fpscr separately.
945 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
946 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
950 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
951 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
953 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
954 vregs_end_pos, VFP_STATE_SIZE);
960 static int compat_vfp_set(struct task_struct *target,
961 const struct user_regset *regset,
962 unsigned int pos, unsigned int count,
963 const void *kbuf, const void __user *ubuf)
965 struct user_fpsimd_state *uregs;
966 compat_ulong_t fpscr;
967 int ret, vregs_end_pos;
969 if (!system_supports_fpsimd())
972 uregs = &target->thread.fpsimd_state.user_fpsimd;
974 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
975 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
979 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
980 vregs_end_pos, VFP_STATE_SIZE);
982 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
983 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
987 fpsimd_flush_task_state(target);
991 static int compat_tls_get(struct task_struct *target,
992 const struct user_regset *regset, unsigned int pos,
993 unsigned int count, void *kbuf, void __user *ubuf)
995 compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
996 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
999 static int compat_tls_set(struct task_struct *target,
1000 const struct user_regset *regset, unsigned int pos,
1001 unsigned int count, const void *kbuf,
1002 const void __user *ubuf)
1005 compat_ulong_t tls = target->thread.tp_value;
1007 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1011 target->thread.tp_value = tls;
1015 static const struct user_regset aarch32_regsets[] = {
1016 [REGSET_COMPAT_GPR] = {
1017 .core_note_type = NT_PRSTATUS,
1018 .n = COMPAT_ELF_NGREG,
1019 .size = sizeof(compat_elf_greg_t),
1020 .align = sizeof(compat_elf_greg_t),
1021 .get = compat_gpr_get,
1022 .set = compat_gpr_set
1024 [REGSET_COMPAT_VFP] = {
1025 .core_note_type = NT_ARM_VFP,
1026 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1027 .size = sizeof(compat_ulong_t),
1028 .align = sizeof(compat_ulong_t),
1029 .active = fpr_active,
1030 .get = compat_vfp_get,
1031 .set = compat_vfp_set
1035 static const struct user_regset_view user_aarch32_view = {
1036 .name = "aarch32", .e_machine = EM_ARM,
1037 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1040 static const struct user_regset aarch32_ptrace_regsets[] = {
1042 .core_note_type = NT_PRSTATUS,
1043 .n = COMPAT_ELF_NGREG,
1044 .size = sizeof(compat_elf_greg_t),
1045 .align = sizeof(compat_elf_greg_t),
1046 .get = compat_gpr_get,
1047 .set = compat_gpr_set
1050 .core_note_type = NT_ARM_VFP,
1051 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1052 .size = sizeof(compat_ulong_t),
1053 .align = sizeof(compat_ulong_t),
1054 .get = compat_vfp_get,
1055 .set = compat_vfp_set
1058 .core_note_type = NT_ARM_TLS,
1060 .size = sizeof(compat_ulong_t),
1061 .align = sizeof(compat_ulong_t),
1062 .get = compat_tls_get,
1063 .set = compat_tls_set,
1065 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1066 [REGSET_HW_BREAK] = {
1067 .core_note_type = NT_ARM_HW_BREAK,
1068 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1069 .size = sizeof(u32),
1070 .align = sizeof(u32),
1071 .get = hw_break_get,
1072 .set = hw_break_set,
1074 [REGSET_HW_WATCH] = {
1075 .core_note_type = NT_ARM_HW_WATCH,
1076 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1077 .size = sizeof(u32),
1078 .align = sizeof(u32),
1079 .get = hw_break_get,
1080 .set = hw_break_set,
1083 [REGSET_SYSTEM_CALL] = {
1084 .core_note_type = NT_ARM_SYSTEM_CALL,
1086 .size = sizeof(int),
1087 .align = sizeof(int),
1088 .get = system_call_get,
1089 .set = system_call_set,
1093 static const struct user_regset_view user_aarch32_ptrace_view = {
1094 .name = "aarch32", .e_machine = EM_ARM,
1095 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1098 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1099 compat_ulong_t __user *ret)
1106 if (off == COMPAT_PT_TEXT_ADDR)
1107 tmp = tsk->mm->start_code;
1108 else if (off == COMPAT_PT_DATA_ADDR)
1109 tmp = tsk->mm->start_data;
1110 else if (off == COMPAT_PT_TEXT_END_ADDR)
1111 tmp = tsk->mm->end_code;
1112 else if (off < sizeof(compat_elf_gregset_t))
1113 return copy_regset_to_user(tsk, &user_aarch32_view,
1114 REGSET_COMPAT_GPR, off,
1115 sizeof(compat_ulong_t), ret);
1116 else if (off >= COMPAT_USER_SZ)
1121 return put_user(tmp, ret);
1124 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1128 mm_segment_t old_fs = get_fs();
1130 if (off & 3 || off >= COMPAT_USER_SZ)
1133 if (off >= sizeof(compat_elf_gregset_t))
1137 ret = copy_regset_from_user(tsk, &user_aarch32_view,
1138 REGSET_COMPAT_GPR, off,
1139 sizeof(compat_ulong_t),
1146 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1149 * Convert a virtual register number into an index for a thread_info
1150 * breakpoint array. Breakpoints are identified using positive numbers
1151 * whilst watchpoints are negative. The registers are laid out as pairs
1152 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1153 * Register 0 is reserved for describing resource information.
1155 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1157 return (abs(num) - 1) >> 1;
1160 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1162 u8 num_brps, num_wrps, debug_arch, wp_len;
1165 num_brps = hw_breakpoint_slots(TYPE_INST);
1166 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1168 debug_arch = debug_monitors_arch();
1182 static int compat_ptrace_hbp_get(unsigned int note_type,
1183 struct task_struct *tsk,
1190 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1193 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1196 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1203 static int compat_ptrace_hbp_set(unsigned int note_type,
1204 struct task_struct *tsk,
1211 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1215 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1218 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1224 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1225 compat_ulong_t __user *data)
1232 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1234 } else if (num == 0) {
1235 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1238 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1242 ret = put_user(kdata, data);
1247 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1248 compat_ulong_t __user *data)
1256 ret = get_user(kdata, data);
1261 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1263 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1267 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1269 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1270 compat_ulong_t caddr, compat_ulong_t cdata)
1272 unsigned long addr = caddr;
1273 unsigned long data = cdata;
1274 void __user *datap = compat_ptr(data);
1278 case PTRACE_PEEKUSR:
1279 ret = compat_ptrace_read_user(child, addr, datap);
1282 case PTRACE_POKEUSR:
1283 ret = compat_ptrace_write_user(child, addr, data);
1286 case COMPAT_PTRACE_GETREGS:
1287 ret = copy_regset_to_user(child,
1290 0, sizeof(compat_elf_gregset_t),
1294 case COMPAT_PTRACE_SETREGS:
1295 ret = copy_regset_from_user(child,
1298 0, sizeof(compat_elf_gregset_t),
1302 case COMPAT_PTRACE_GET_THREAD_AREA:
1303 ret = put_user((compat_ulong_t)child->thread.tp_value,
1304 (compat_ulong_t __user *)datap);
1307 case COMPAT_PTRACE_SET_SYSCALL:
1308 task_pt_regs(child)->syscallno = data;
1312 case COMPAT_PTRACE_GETVFPREGS:
1313 ret = copy_regset_to_user(child,
1320 case COMPAT_PTRACE_SETVFPREGS:
1321 ret = copy_regset_from_user(child,
1328 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1329 case COMPAT_PTRACE_GETHBPREGS:
1330 ret = compat_ptrace_gethbpregs(child, addr, datap);
1333 case COMPAT_PTRACE_SETHBPREGS:
1334 ret = compat_ptrace_sethbpregs(child, addr, datap);
1339 ret = compat_ptrace_request(child, request, addr,
1346 #endif /* CONFIG_COMPAT */
1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1350 #ifdef CONFIG_COMPAT
1352 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1353 * user_aarch32_view compatible with arm32. Native ptrace requests on
1354 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1355 * access to the TLS register.
1357 if (is_compat_task())
1358 return &user_aarch32_view;
1359 else if (is_compat_thread(task_thread_info(task)))
1360 return &user_aarch32_ptrace_view;
1362 return &user_aarch64_view;
1365 long arch_ptrace(struct task_struct *child, long request,
1366 unsigned long addr, unsigned long data)
1368 return ptrace_request(child, request, addr, data);
1371 enum ptrace_syscall_dir {
1372 PTRACE_SYSCALL_ENTER = 0,
1373 PTRACE_SYSCALL_EXIT,
1376 static void tracehook_report_syscall(struct pt_regs *regs,
1377 enum ptrace_syscall_dir dir)
1380 unsigned long saved_reg;
1383 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1384 * used to denote syscall entry/exit:
1386 regno = (is_compat_task() ? 12 : 7);
1387 saved_reg = regs->regs[regno];
1388 regs->regs[regno] = dir;
1390 if (dir == PTRACE_SYSCALL_EXIT)
1391 tracehook_report_syscall_exit(regs, 0);
1392 else if (tracehook_report_syscall_entry(regs))
1393 forget_syscall(regs);
1395 regs->regs[regno] = saved_reg;
1398 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1400 if (test_thread_flag(TIF_SYSCALL_TRACE))
1401 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1403 /* Do the secure computing after ptrace; failures should be fast. */
1404 if (secure_computing(NULL) == -1)
1407 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1408 trace_sys_enter(regs, regs->syscallno);
1410 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1411 regs->regs[2], regs->regs[3]);
1413 return regs->syscallno;
1416 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1418 audit_syscall_exit(regs);
1420 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1421 trace_sys_exit(regs, regs_return_value(regs));
1423 if (test_thread_flag(TIF_SYSCALL_TRACE))
1424 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1428 * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
1429 * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
1430 * not described in ARM DDI 0487D.a.
1431 * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
1432 * be allocated an EL0 meaning in future.
1433 * Userspace cannot use these until they have an architectural meaning.
1434 * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
1435 * We also reserve IL for the kernel; SS is handled dynamically.
1437 #define SPSR_EL1_AARCH64_RES0_BITS \
1438 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
1439 GENMASK_ULL(20, 13) | GENMASK_ULL(11, 10) | GENMASK_ULL(5, 5))
1440 #define SPSR_EL1_AARCH32_RES0_BITS \
1441 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
1443 static int valid_compat_regs(struct user_pt_regs *regs)
1445 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1447 if (!system_supports_mixed_endian_el0()) {
1448 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1449 regs->pstate |= COMPAT_PSR_E_BIT;
1451 regs->pstate &= ~COMPAT_PSR_E_BIT;
1454 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1455 (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1456 (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1457 (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1462 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1465 regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1466 COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1467 COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1468 COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1470 regs->pstate |= PSR_MODE32_BIT;
1475 static int valid_native_regs(struct user_pt_regs *regs)
1477 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1479 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1480 (regs->pstate & PSR_D_BIT) == 0 &&
1481 (regs->pstate & PSR_A_BIT) == 0 &&
1482 (regs->pstate & PSR_I_BIT) == 0 &&
1483 (regs->pstate & PSR_F_BIT) == 0) {
1487 /* Force PSR to a valid 64-bit EL0t */
1488 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1494 * Are the current registers suitable for user mode? (used to maintain
1495 * security in signal handlers)
1497 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1499 /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
1500 user_regs_reset_single_step(regs, task);
1502 if (is_compat_thread(task_thread_info(task)))
1503 return valid_compat_regs(regs);
1505 return valid_native_regs(regs);