1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/kernel/ptrace.c
6 * edited by Linus Torvalds
7 * ARM modifications Copyright (C) 2000 Russell King
9 #include <linux/kernel.h>
10 #include <linux/sched/signal.h>
11 #include <linux/sched/task_stack.h>
13 #include <linux/elf.h>
14 #include <linux/smp.h>
15 #include <linux/ptrace.h>
16 #include <linux/user.h>
17 #include <linux/security.h>
18 #include <linux/init.h>
19 #include <linux/signal.h>
20 #include <linux/uaccess.h>
21 #include <linux/perf_event.h>
22 #include <linux/hw_breakpoint.h>
23 #include <linux/regset.h>
24 #include <linux/audit.h>
25 #include <linux/tracehook.h>
26 #include <linux/unistd.h>
28 #include <asm/traps.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/syscalls.h>
36 * does not yet catch signals sent when the child dies.
37 * in exit.c or in signal.c.
42 * Breakpoint SWI instruction: SWI &9F0001
44 #define BREAKINST_ARM 0xef9f0001
45 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
48 * New breakpoints - use an undefined instruction. The ARM architecture
49 * reference manual guarantees that the following instruction space
50 * will produce an undefined instruction exception on all CPUs:
52 * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
53 * Thumb: 1101 1110 xxxx xxxx
55 #define BREAKINST_ARM 0xe7f001f0
56 #define BREAKINST_THUMB 0xde01
59 struct pt_regs_offset {
64 #define REG_OFFSET_NAME(r) \
65 {.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
66 #define REG_OFFSET_END {.name = NULL, .offset = 0}
68 static const struct pt_regs_offset regoffset_table[] = {
85 REG_OFFSET_NAME(cpsr),
86 REG_OFFSET_NAME(ORIG_r0),
91 * regs_query_register_offset() - query register offset from its name
92 * @name: the name of a register
94 * regs_query_register_offset() returns the offset of a register in struct
95 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
97 int regs_query_register_offset(const char *name)
99 const struct pt_regs_offset *roff;
100 for (roff = regoffset_table; roff->name != NULL; roff++)
101 if (!strcmp(roff->name, name))
107 * regs_query_register_name() - query register name from its offset
108 * @offset: the offset of a register in struct pt_regs.
110 * regs_query_register_name() returns the name of a register from its
111 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
113 const char *regs_query_register_name(unsigned int offset)
115 const struct pt_regs_offset *roff;
116 for (roff = regoffset_table; roff->name != NULL; roff++)
117 if (roff->offset == offset)
123 * regs_within_kernel_stack() - check the address in the stack
124 * @regs: pt_regs which contains kernel stack pointer.
125 * @addr: address which is checked.
127 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
128 * If @addr is within the kernel stack, it returns true. If not, returns false.
130 bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
132 return ((addr & ~(THREAD_SIZE - 1)) ==
133 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
137 * regs_get_kernel_stack_nth() - get Nth entry of the stack
138 * @regs: pt_regs which contains kernel stack pointer.
139 * @n: stack entry number.
141 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
142 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
145 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
147 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
149 if (regs_within_kernel_stack(regs, (unsigned long)addr))
156 * this routine will get a word off of the processes privileged stack.
157 * the offset is how far from the base addr as stored in the THREAD.
158 * this routine assumes that all the privileged stacks are in our
161 static inline long get_user_reg(struct task_struct *task, int offset)
163 return task_pt_regs(task)->uregs[offset];
167 * this routine will put a word on the processes privileged stack.
168 * the offset is how far from the base addr as stored in the THREAD.
169 * this routine assumes that all the privileged stacks are in our
173 put_user_reg(struct task_struct *task, int offset, long data)
175 struct pt_regs newregs, *regs = task_pt_regs(task);
179 newregs.uregs[offset] = data;
181 if (valid_user_regs(&newregs)) {
182 regs->uregs[offset] = data;
190 * Called by kernel/ptrace.c when detaching..
192 void ptrace_disable(struct task_struct *child)
198 * Handle hitting a breakpoint.
200 void ptrace_break(struct pt_regs *regs)
202 force_sig_fault(SIGTRAP, TRAP_BRKPT,
203 (void __user *)instruction_pointer(regs));
206 static int break_trap(struct pt_regs *regs, unsigned int instr)
212 static struct undef_hook arm_break_hook = {
213 .instr_mask = 0x0fffffff,
214 .instr_val = 0x07f001f0,
215 .cpsr_mask = PSR_T_BIT,
220 static struct undef_hook thumb_break_hook = {
221 .instr_mask = 0xffffffff,
222 .instr_val = 0x0000de01,
223 .cpsr_mask = PSR_T_BIT,
224 .cpsr_val = PSR_T_BIT,
228 static struct undef_hook thumb2_break_hook = {
229 .instr_mask = 0xffffffff,
230 .instr_val = 0xf7f0a000,
231 .cpsr_mask = PSR_T_BIT,
232 .cpsr_val = PSR_T_BIT,
236 static int __init ptrace_break_init(void)
238 register_undef_hook(&arm_break_hook);
239 register_undef_hook(&thumb_break_hook);
240 register_undef_hook(&thumb2_break_hook);
244 core_initcall(ptrace_break_init);
247 * Read the word at offset "off" into the "struct user". We
248 * actually access the pt_regs stored on the kernel stack.
250 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
251 unsigned long __user *ret)
259 if (off == PT_TEXT_ADDR)
260 tmp = tsk->mm->start_code;
261 else if (off == PT_DATA_ADDR)
262 tmp = tsk->mm->start_data;
263 else if (off == PT_TEXT_END_ADDR)
264 tmp = tsk->mm->end_code;
265 else if (off < sizeof(struct pt_regs))
266 tmp = get_user_reg(tsk, off >> 2);
267 else if (off >= sizeof(struct user))
270 return put_user(tmp, ret);
274 * Write the word at offset "off" into "struct user". We
275 * actually access the pt_regs stored on the kernel stack.
277 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
280 if (off & 3 || off >= sizeof(struct user))
283 if (off >= sizeof(struct pt_regs))
286 return put_user_reg(tsk, off >> 2, val);
292 * Get the child iWMMXt state.
294 static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
296 struct thread_info *thread = task_thread_info(tsk);
298 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
300 iwmmxt_task_disable(thread); /* force it to ram */
301 return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
306 * Set the child iWMMXt state.
308 static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
310 struct thread_info *thread = task_thread_info(tsk);
312 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
314 iwmmxt_task_release(thread); /* force a reload */
315 return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
323 * Get the child Crunch state.
325 static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
327 struct thread_info *thread = task_thread_info(tsk);
329 crunch_task_disable(thread); /* force it to ram */
330 return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
335 * Set the child Crunch state.
337 static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
339 struct thread_info *thread = task_thread_info(tsk);
341 crunch_task_release(thread); /* force a reload */
342 return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
347 #ifdef CONFIG_HAVE_HW_BREAKPOINT
349 * Convert a virtual register number into an index for a thread_info
350 * breakpoint array. Breakpoints are identified using positive numbers
351 * whilst watchpoints are negative. The registers are laid out as pairs
352 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
353 * Register 0 is reserved for describing resource information.
355 static int ptrace_hbp_num_to_idx(long num)
358 num = (ARM_MAX_BRP << 1) - num;
359 return (num - 1) >> 1;
363 * Returns the virtual register number for the address of the
364 * breakpoint at index idx.
366 static long ptrace_hbp_idx_to_num(int idx)
368 long mid = ARM_MAX_BRP << 1;
369 long num = (idx << 1) + 1;
370 return num > mid ? mid - num : num;
374 * Handle hitting a HW-breakpoint.
376 static void ptrace_hbptriggered(struct perf_event *bp,
377 struct perf_sample_data *data,
378 struct pt_regs *regs)
380 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
384 for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
385 if (current->thread.debug.hbp[i] == bp)
388 num = (i == ARM_MAX_HBP_SLOTS) ? 0 : ptrace_hbp_idx_to_num(i);
390 force_sig_ptrace_errno_trap((int)num, (void __user *)(bkpt->trigger));
394 * Set ptrace breakpoint pointers to zero for this task.
395 * This is required in order to prevent child processes from unregistering
396 * breakpoints held by their parent.
398 void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
400 memset(tsk->thread.debug.hbp, 0, sizeof(tsk->thread.debug.hbp));
404 * Unregister breakpoints from this task and reset the pointers in
407 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
410 struct thread_struct *t = &tsk->thread;
412 for (i = 0; i < ARM_MAX_HBP_SLOTS; i++) {
413 if (t->debug.hbp[i]) {
414 unregister_hw_breakpoint(t->debug.hbp[i]);
415 t->debug.hbp[i] = NULL;
420 static u32 ptrace_get_hbp_resource_info(void)
422 u8 num_brps, num_wrps, debug_arch, wp_len;
425 num_brps = hw_breakpoint_slots(TYPE_INST);
426 num_wrps = hw_breakpoint_slots(TYPE_DATA);
427 debug_arch = arch_get_debug_arch();
428 wp_len = arch_get_max_wp_len();
441 static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
443 struct perf_event_attr attr;
445 ptrace_breakpoint_init(&attr);
447 /* Initialise fields to sane defaults. */
449 attr.bp_len = HW_BREAKPOINT_LEN_4;
453 return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
457 static int ptrace_gethbpregs(struct task_struct *tsk, long num,
458 unsigned long __user *data)
462 struct perf_event *bp;
463 struct arch_hw_breakpoint_ctrl arch_ctrl;
466 reg = ptrace_get_hbp_resource_info();
468 idx = ptrace_hbp_num_to_idx(num);
469 if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
474 bp = tsk->thread.debug.hbp[idx];
480 arch_ctrl = counter_arch_bp(bp)->ctrl;
483 * Fix up the len because we may have adjusted it
484 * to compensate for an unaligned address.
486 while (!(arch_ctrl.len & 0x1))
490 reg = bp->attr.bp_addr;
492 reg = encode_ctrl_reg(arch_ctrl);
496 if (put_user(reg, data))
503 static int ptrace_sethbpregs(struct task_struct *tsk, long num,
504 unsigned long __user *data)
506 int idx, gen_len, gen_type, implied_type, ret = 0;
508 struct perf_event *bp;
509 struct arch_hw_breakpoint_ctrl ctrl;
510 struct perf_event_attr attr;
515 implied_type = HW_BREAKPOINT_RW;
517 implied_type = HW_BREAKPOINT_X;
519 idx = ptrace_hbp_num_to_idx(num);
520 if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
525 if (get_user(user_val, data)) {
530 bp = tsk->thread.debug.hbp[idx];
532 bp = ptrace_hbp_create(tsk, implied_type);
537 tsk->thread.debug.hbp[idx] = bp;
544 attr.bp_addr = user_val;
547 decode_ctrl_reg(user_val, &ctrl);
548 ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
552 if ((gen_type & implied_type) != gen_type) {
557 attr.bp_len = gen_len;
558 attr.bp_type = gen_type;
559 attr.disabled = !ctrl.enabled;
562 ret = modify_user_hw_breakpoint(bp, &attr);
568 /* regset get/set implementations */
570 static int gpr_get(struct task_struct *target,
571 const struct user_regset *regset,
574 return membuf_write(&to, task_pt_regs(target), sizeof(struct pt_regs));
577 static int gpr_set(struct task_struct *target,
578 const struct user_regset *regset,
579 unsigned int pos, unsigned int count,
580 const void *kbuf, const void __user *ubuf)
583 struct pt_regs newregs = *task_pt_regs(target);
585 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
591 if (!valid_user_regs(&newregs))
594 *task_pt_regs(target) = newregs;
598 static int fpa_get(struct task_struct *target,
599 const struct user_regset *regset,
602 return membuf_write(&to, &task_thread_info(target)->fpstate,
603 sizeof(struct user_fp));
606 static int fpa_set(struct task_struct *target,
607 const struct user_regset *regset,
608 unsigned int pos, unsigned int count,
609 const void *kbuf, const void __user *ubuf)
611 struct thread_info *thread = task_thread_info(target);
613 thread->used_cp[1] = thread->used_cp[2] = 1;
615 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
617 0, sizeof(struct user_fp));
622 * VFP register get/set implementations.
624 * With respect to the kernel, struct user_fp is divided into three chunks:
625 * 16 or 32 real VFP registers (d0-d15 or d0-31)
626 * These are transferred to/from the real registers in the task's
627 * vfp_hard_struct. The number of registers depends on the kernel
630 * 16 or 0 fake VFP registers (d16-d31 or empty)
631 * i.e., the user_vfp structure has space for 32 registers even if
632 * the kernel doesn't have them all.
634 * vfp_get() reads this chunk as zero where applicable
635 * vfp_set() ignores this chunk
637 * 1 word for the FPSCR
639 static int vfp_get(struct task_struct *target,
640 const struct user_regset *regset,
643 struct thread_info *thread = task_thread_info(target);
644 struct vfp_hard_struct const *vfp = &thread->vfpstate.hard;
645 const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
647 vfp_sync_hwstate(thread);
649 membuf_write(&to, vfp->fpregs, sizeof(vfp->fpregs));
650 membuf_zero(&to, user_fpscr_offset - sizeof(vfp->fpregs));
651 return membuf_store(&to, vfp->fpscr);
655 * For vfp_set() a read-modify-write is done on the VFP registers,
656 * in order to avoid writing back a half-modified set of registers on
659 static int vfp_set(struct task_struct *target,
660 const struct user_regset *regset,
661 unsigned int pos, unsigned int count,
662 const void *kbuf, const void __user *ubuf)
665 struct thread_info *thread = task_thread_info(target);
666 struct vfp_hard_struct new_vfp;
667 const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
668 const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
670 vfp_sync_hwstate(thread);
671 new_vfp = thread->vfpstate.hard;
673 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
676 user_fpregs_offset + sizeof(new_vfp.fpregs));
680 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
681 user_fpregs_offset + sizeof(new_vfp.fpregs),
686 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
689 user_fpscr_offset + sizeof(new_vfp.fpscr));
693 thread->vfpstate.hard = new_vfp;
694 vfp_flush_hwstate(thread);
698 #endif /* CONFIG_VFP */
708 static const struct user_regset arm_regsets[] = {
710 .core_note_type = NT_PRSTATUS,
713 .align = sizeof(u32),
714 .regset_get = gpr_get,
719 * For the FPA regs in fpstate, the real fields are a mixture
720 * of sizes, so pretend that the registers are word-sized:
722 .core_note_type = NT_PRFPREG,
723 .n = sizeof(struct user_fp) / sizeof(u32),
725 .align = sizeof(u32),
726 .regset_get = fpa_get,
732 * Pretend that the VFP regs are word-sized, since the FPSCR is
733 * a single word dangling at the end of struct user_vfp:
735 .core_note_type = NT_ARM_VFP,
736 .n = ARM_VFPREGS_SIZE / sizeof(u32),
738 .align = sizeof(u32),
739 .regset_get = vfp_get,
742 #endif /* CONFIG_VFP */
745 static const struct user_regset_view user_arm_view = {
746 .name = "arm", .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
747 .regsets = arm_regsets, .n = ARRAY_SIZE(arm_regsets)
750 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
752 return &user_arm_view;
755 long arch_ptrace(struct task_struct *child, long request,
756 unsigned long addr, unsigned long data)
759 unsigned long __user *datap = (unsigned long __user *) data;
763 ret = ptrace_read_user(child, addr, datap);
767 ret = ptrace_write_user(child, addr, data);
771 ret = copy_regset_to_user(child,
772 &user_arm_view, REGSET_GPR,
773 0, sizeof(struct pt_regs),
778 ret = copy_regset_from_user(child,
779 &user_arm_view, REGSET_GPR,
780 0, sizeof(struct pt_regs),
784 case PTRACE_GETFPREGS:
785 ret = copy_regset_to_user(child,
786 &user_arm_view, REGSET_FPR,
787 0, sizeof(union fp_state),
791 case PTRACE_SETFPREGS:
792 ret = copy_regset_from_user(child,
793 &user_arm_view, REGSET_FPR,
794 0, sizeof(union fp_state),
799 case PTRACE_GETWMMXREGS:
800 ret = ptrace_getwmmxregs(child, datap);
803 case PTRACE_SETWMMXREGS:
804 ret = ptrace_setwmmxregs(child, datap);
808 case PTRACE_GET_THREAD_AREA:
809 ret = put_user(task_thread_info(child)->tp_value[0],
813 case PTRACE_SET_SYSCALL:
814 task_thread_info(child)->syscall = data;
819 case PTRACE_GETCRUNCHREGS:
820 ret = ptrace_getcrunchregs(child, datap);
823 case PTRACE_SETCRUNCHREGS:
824 ret = ptrace_setcrunchregs(child, datap);
829 case PTRACE_GETVFPREGS:
830 ret = copy_regset_to_user(child,
831 &user_arm_view, REGSET_VFP,
836 case PTRACE_SETVFPREGS:
837 ret = copy_regset_from_user(child,
838 &user_arm_view, REGSET_VFP,
844 #ifdef CONFIG_HAVE_HW_BREAKPOINT
845 case PTRACE_GETHBPREGS:
846 ret = ptrace_gethbpregs(child, addr,
847 (unsigned long __user *)data);
849 case PTRACE_SETHBPREGS:
850 ret = ptrace_sethbpregs(child, addr,
851 (unsigned long __user *)data);
856 ret = ptrace_request(child, request, addr, data);
863 enum ptrace_syscall_dir {
864 PTRACE_SYSCALL_ENTER = 0,
868 static void tracehook_report_syscall(struct pt_regs *regs,
869 enum ptrace_syscall_dir dir)
874 * IP is used to denote syscall entry/exit:
875 * IP = 0 -> entry, =1 -> exit
880 if (dir == PTRACE_SYSCALL_EXIT)
881 tracehook_report_syscall_exit(regs, 0);
882 else if (tracehook_report_syscall_entry(regs))
883 current_thread_info()->syscall = -1;
888 asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
890 current_thread_info()->syscall = scno;
892 if (test_thread_flag(TIF_SYSCALL_TRACE))
893 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
895 /* Do seccomp after ptrace; syscall may have changed. */
896 #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
897 if (secure_computing() == -1)
900 /* XXX: remove this once OABI gets fixed */
901 secure_computing_strict(current_thread_info()->syscall);
904 /* Tracer or seccomp may have changed syscall. */
905 scno = current_thread_info()->syscall;
907 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
908 trace_sys_enter(regs, scno);
910 audit_syscall_entry(scno, regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
916 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
919 * Audit the syscall before anything else, as a debugger may
920 * come in and change the current registers.
922 audit_syscall_exit(regs);
925 * Note that we haven't updated the ->syscall field for the
926 * current thread. This isn't a problem because it will have
927 * been set on syscall entry and there hasn't been an opportunity
928 * for a PTRACE_SET_SYSCALL since then.
930 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
931 trace_sys_exit(regs, regs_return_value(regs));
933 if (test_thread_flag(TIF_SYSCALL_TRACE))
934 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);