2 * linux/arch/arm/kernel/ptrace.c
5 * edited by Linus Torvalds
6 * ARM modifications Copyright (C) 2000 Russell King
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/init.h>
20 #include <linux/signal.h>
21 #include <linux/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/traps.h>
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
38 * Breakpoint SWI instruction: SWI &9F0001
40 #define BREAKINST_ARM 0xef9f0001
41 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
44 * New breakpoints - use an undefined instruction. The ARM architecture
45 * reference manual guarantees that the following instruction space
46 * will produce an undefined instruction exception on all CPUs:
48 * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
49 * Thumb: 1101 1110 xxxx xxxx
51 #define BREAKINST_ARM 0xe7f001f0
52 #define BREAKINST_THUMB 0xde01
55 struct pt_regs_offset {
60 #define REG_OFFSET_NAME(r) \
61 {.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64 static const struct pt_regs_offset regoffset_table[] = {
81 REG_OFFSET_NAME(cpsr),
82 REG_OFFSET_NAME(ORIG_r0),
87 * regs_query_register_offset() - query register offset from its name
88 * @name: the name of a register
90 * regs_query_register_offset() returns the offset of a register in struct
91 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
93 int regs_query_register_offset(const char *name)
95 const struct pt_regs_offset *roff;
96 for (roff = regoffset_table; roff->name != NULL; roff++)
97 if (!strcmp(roff->name, name))
103 * regs_query_register_name() - query register name from its offset
104 * @offset: the offset of a register in struct pt_regs.
106 * regs_query_register_name() returns the name of a register from its
107 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
109 const char *regs_query_register_name(unsigned int offset)
111 const struct pt_regs_offset *roff;
112 for (roff = regoffset_table; roff->name != NULL; roff++)
113 if (roff->offset == offset)
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 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)));
133 * regs_get_kernel_stack_nth() - get Nth entry of the stack
134 * @regs: pt_regs which contains kernel stack pointer.
135 * @n: stack entry number.
137 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
138 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
141 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
143 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
145 if (regs_within_kernel_stack(regs, (unsigned long)addr))
152 * this routine will get a word off of the processes privileged stack.
153 * the offset is how far from the base addr as stored in the THREAD.
154 * this routine assumes that all the privileged stacks are in our
157 static inline long get_user_reg(struct task_struct *task, int offset)
159 return task_pt_regs(task)->uregs[offset];
163 * this routine will put a word on the processes privileged stack.
164 * the offset is how far from the base addr as stored in the THREAD.
165 * this routine assumes that all the privileged stacks are in our
169 put_user_reg(struct task_struct *task, int offset, long data)
171 struct pt_regs newregs, *regs = task_pt_regs(task);
175 newregs.uregs[offset] = data;
177 if (valid_user_regs(&newregs)) {
178 regs->uregs[offset] = data;
186 read_u32(struct task_struct *task, unsigned long addr, u32 *res)
190 ret = access_process_vm(task, addr, res, sizeof(*res), 0);
192 return ret == sizeof(*res) ? 0 : -EIO;
196 read_instr(struct task_struct *task, unsigned long addr, u32 *res)
202 ret = access_process_vm(task, addr & ~1, &val, sizeof(val), 0);
203 ret = ret == sizeof(val) ? 0 : -EIO;
207 ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
208 ret = ret == sizeof(val) ? 0 : -EIO;
215 * Get value of register `rn' (in the instruction)
218 ptrace_getrn(struct task_struct *child, unsigned long insn)
220 unsigned int reg = (insn >> 16) & 15;
223 val = get_user_reg(child, reg);
231 * Get value of operand 2 (in an ALU instruction)
234 ptrace_getaluop2(struct task_struct *child, unsigned long insn)
240 if (insn & 1 << 25) {
242 shift = (insn >> 8) & 15;
245 val = get_user_reg (child, insn & 15);
248 shift = (int)get_user_reg (child, (insn >> 8) & 15);
250 shift = (insn >> 7) & 31;
252 type = (insn >> 5) & 3;
256 case 0: val <<= shift; break;
257 case 1: val >>= shift; break;
259 val = (((signed long)val) >> shift);
262 val = (val >> shift) | (val << (32 - shift));
269 * Get value of operand 2 (in a LDR instruction)
272 ptrace_getldrop2(struct task_struct *child, unsigned long insn)
278 val = get_user_reg(child, insn & 15);
279 shift = (insn >> 7) & 31;
280 type = (insn >> 5) & 3;
283 case 0: val <<= shift; break;
284 case 1: val >>= shift; break;
286 val = (((signed long)val) >> shift);
289 val = (val >> shift) | (val << (32 - shift));
295 #define OP_MASK 0x01e00000
296 #define OP_AND 0x00000000
297 #define OP_EOR 0x00200000
298 #define OP_SUB 0x00400000
299 #define OP_RSB 0x00600000
300 #define OP_ADD 0x00800000
301 #define OP_ADC 0x00a00000
302 #define OP_SBC 0x00c00000
303 #define OP_RSC 0x00e00000
304 #define OP_ORR 0x01800000
305 #define OP_MOV 0x01a00000
306 #define OP_BIC 0x01c00000
307 #define OP_MVN 0x01e00000
310 get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
314 switch (insn & 0x0e000000) {
320 long aluop1, aluop2, ccbit;
322 if ((insn & 0x0fffffd0) == 0x012fff10) {
326 alt = get_user_reg(child, insn & 15);
331 if ((insn & 0xf000) != 0xf000)
334 aluop1 = ptrace_getrn(child, insn);
335 aluop2 = ptrace_getaluop2(child, insn);
336 ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
338 switch (insn & OP_MASK) {
339 case OP_AND: alt = aluop1 & aluop2; break;
340 case OP_EOR: alt = aluop1 ^ aluop2; break;
341 case OP_SUB: alt = aluop1 - aluop2; break;
342 case OP_RSB: alt = aluop2 - aluop1; break;
343 case OP_ADD: alt = aluop1 + aluop2; break;
344 case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
345 case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
346 case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
347 case OP_ORR: alt = aluop1 | aluop2; break;
348 case OP_MOV: alt = aluop2; break;
349 case OP_BIC: alt = aluop1 & ~aluop2; break;
350 case OP_MVN: alt = ~aluop2; break;
360 if ((insn & 0x0010f000) == 0x0010f000) {
363 base = ptrace_getrn(child, insn);
364 if (insn & 1 << 24) {
367 if (insn & 0x02000000)
368 aluop2 = ptrace_getldrop2(child, insn);
370 aluop2 = insn & 0xfff;
377 read_u32(child, base, &alt);
385 if ((insn & 0x00108000) == 0x00108000) {
387 unsigned int nr_regs;
389 if (insn & (1 << 23)) {
390 nr_regs = hweight16(insn & 65535) << 2;
392 if (!(insn & (1 << 24)))
395 if (insn & (1 << 24))
401 base = ptrace_getrn(child, insn);
403 read_u32(child, base + nr_regs, &alt);
413 /* It's a branch/branch link: instead of trying to
414 * figure out whether the branch will be taken or not,
415 * we'll put a breakpoint at both locations. This is
416 * simpler, more reliable, and probably not a whole lot
417 * slower than the alternative approach of emulating the
420 displ = (insn & 0x00ffffff) << 8;
421 displ = (displ >> 6) + 8;
422 if (displ != 0 && displ != 4)
432 swap_insn(struct task_struct *task, unsigned long addr,
433 void *old_insn, void *new_insn, int size)
437 ret = access_process_vm(task, addr, old_insn, size, 0);
439 ret = access_process_vm(task, addr, new_insn, size, 1);
444 add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
446 int nr = dbg->nsaved;
449 u32 new_insn = BREAKINST_ARM;
452 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
455 dbg->bp[nr].address = addr;
459 printk(KERN_ERR "ptrace: too many breakpoints\n");
463 * Clear one breakpoint in the user program. We copy what the hardware
464 * does and use bit 0 of the address to indicate whether this is a Thumb
465 * breakpoint or an ARM breakpoint.
467 static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
469 unsigned long addr = bp->address;
470 union debug_insn old_insn;
474 ret = swap_insn(task, addr & ~1, &old_insn.thumb,
477 if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
478 printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
479 "0x%08lx (0x%04x)\n", task->comm,
480 task_pid_nr(task), addr, old_insn.thumb);
482 ret = swap_insn(task, addr & ~3, &old_insn.arm,
485 if (ret != 4 || old_insn.arm != BREAKINST_ARM)
486 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
487 "0x%08lx (0x%08x)\n", task->comm,
488 task_pid_nr(task), addr, old_insn.arm);
492 void ptrace_set_bpt(struct task_struct *child)
494 struct pt_regs *regs;
499 regs = task_pt_regs(child);
500 pc = instruction_pointer(regs);
502 if (thumb_mode(regs)) {
503 printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
507 res = read_instr(child, pc, &insn);
509 struct debug_info *dbg = &child->thread.debug;
514 alt = get_branch_address(child, pc, insn);
516 add_breakpoint(child, dbg, alt);
519 * Note that we ignore the result of setting the above
520 * breakpoint since it may fail. When it does, this is
521 * not so much an error, but a forewarning that we may
522 * be receiving a prefetch abort shortly.
524 * If we don't set this breakpoint here, then we can
525 * lose control of the thread during single stepping.
527 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
528 add_breakpoint(child, dbg, pc + 4);
533 * Ensure no single-step breakpoint is pending. Returns non-zero
534 * value if child was being single-stepped.
536 void ptrace_cancel_bpt(struct task_struct *child)
538 int i, nsaved = child->thread.debug.nsaved;
540 child->thread.debug.nsaved = 0;
543 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
547 for (i = 0; i < nsaved; i++)
548 clear_breakpoint(child, &child->thread.debug.bp[i]);
551 void user_disable_single_step(struct task_struct *task)
553 task->ptrace &= ~PT_SINGLESTEP;
554 ptrace_cancel_bpt(task);
557 void user_enable_single_step(struct task_struct *task)
559 task->ptrace |= PT_SINGLESTEP;
563 * Called by kernel/ptrace.c when detaching..
565 void ptrace_disable(struct task_struct *child)
567 user_disable_single_step(child);
571 * Handle hitting a breakpoint.
573 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
577 ptrace_cancel_bpt(tsk);
579 info.si_signo = SIGTRAP;
581 info.si_code = TRAP_BRKPT;
582 info.si_addr = (void __user *)instruction_pointer(regs);
584 force_sig_info(SIGTRAP, &info, tsk);
587 static int break_trap(struct pt_regs *regs, unsigned int instr)
589 ptrace_break(current, regs);
593 static struct undef_hook arm_break_hook = {
594 .instr_mask = 0x0fffffff,
595 .instr_val = 0x07f001f0,
596 .cpsr_mask = PSR_T_BIT,
601 static struct undef_hook thumb_break_hook = {
602 .instr_mask = 0xffff,
604 .cpsr_mask = PSR_T_BIT,
605 .cpsr_val = PSR_T_BIT,
609 static int thumb2_break_trap(struct pt_regs *regs, unsigned int instr)
614 /* Check the second half of the instruction. */
615 pc = (void __user *)(instruction_pointer(regs) + 2);
617 if (processor_mode(regs) == SVC_MODE) {
618 instr2 = *(u16 *) pc;
620 get_user(instr2, (u16 __user *)pc);
623 if (instr2 == 0xa000) {
624 ptrace_break(current, regs);
631 static struct undef_hook thumb2_break_hook = {
632 .instr_mask = 0xffff,
634 .cpsr_mask = PSR_T_BIT,
635 .cpsr_val = PSR_T_BIT,
636 .fn = thumb2_break_trap,
639 static int __init ptrace_break_init(void)
641 register_undef_hook(&arm_break_hook);
642 register_undef_hook(&thumb_break_hook);
643 register_undef_hook(&thumb2_break_hook);
647 core_initcall(ptrace_break_init);
650 * Read the word at offset "off" into the "struct user". We
651 * actually access the pt_regs stored on the kernel stack.
653 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
654 unsigned long __user *ret)
658 if (off & 3 || off >= sizeof(struct user))
662 if (off == PT_TEXT_ADDR)
663 tmp = tsk->mm->start_code;
664 else if (off == PT_DATA_ADDR)
665 tmp = tsk->mm->start_data;
666 else if (off == PT_TEXT_END_ADDR)
667 tmp = tsk->mm->end_code;
668 else if (off < sizeof(struct pt_regs))
669 tmp = get_user_reg(tsk, off >> 2);
671 return put_user(tmp, ret);
675 * Write the word at offset "off" into "struct user". We
676 * actually access the pt_regs stored on the kernel stack.
678 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
681 if (off & 3 || off >= sizeof(struct user))
684 if (off >= sizeof(struct pt_regs))
687 return put_user_reg(tsk, off >> 2, val);
691 * Get all user integer registers.
693 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
695 struct pt_regs *regs = task_pt_regs(tsk);
697 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
701 * Set all user integer registers.
703 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
705 struct pt_regs newregs;
709 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
710 struct pt_regs *regs = task_pt_regs(tsk);
713 if (valid_user_regs(&newregs)) {
723 * Get the child FPU state.
725 static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
727 return copy_to_user(ufp, &task_thread_info(tsk)->fpstate,
728 sizeof(struct user_fp)) ? -EFAULT : 0;
732 * Set the child FPU state.
734 static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
736 struct thread_info *thread = task_thread_info(tsk);
737 thread->used_cp[1] = thread->used_cp[2] = 1;
738 return copy_from_user(&thread->fpstate, ufp,
739 sizeof(struct user_fp)) ? -EFAULT : 0;
745 * Get the child iWMMXt state.
747 static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
749 struct thread_info *thread = task_thread_info(tsk);
751 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
753 iwmmxt_task_disable(thread); /* force it to ram */
754 return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
759 * Set the child iWMMXt state.
761 static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
763 struct thread_info *thread = task_thread_info(tsk);
765 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
767 iwmmxt_task_release(thread); /* force a reload */
768 return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
776 * Get the child Crunch state.
778 static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
780 struct thread_info *thread = task_thread_info(tsk);
782 crunch_task_disable(thread); /* force it to ram */
783 return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
788 * Set the child Crunch state.
790 static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
792 struct thread_info *thread = task_thread_info(tsk);
794 crunch_task_release(thread); /* force a reload */
795 return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
802 * Get the child VFP state.
804 static int ptrace_getvfpregs(struct task_struct *tsk, void __user *data)
806 struct thread_info *thread = task_thread_info(tsk);
807 union vfp_state *vfp = &thread->vfpstate;
808 struct user_vfp __user *ufp = data;
810 vfp_sync_hwstate(thread);
812 /* copy the floating point registers */
813 if (copy_to_user(&ufp->fpregs, &vfp->hard.fpregs,
814 sizeof(vfp->hard.fpregs)))
817 /* copy the status and control register */
818 if (put_user(vfp->hard.fpscr, &ufp->fpscr))
825 * Set the child VFP state.
827 static int ptrace_setvfpregs(struct task_struct *tsk, void __user *data)
829 struct thread_info *thread = task_thread_info(tsk);
830 union vfp_state *vfp = &thread->vfpstate;
831 struct user_vfp __user *ufp = data;
833 vfp_sync_hwstate(thread);
835 /* copy the floating point registers */
836 if (copy_from_user(&vfp->hard.fpregs, &ufp->fpregs,
837 sizeof(vfp->hard.fpregs)))
840 /* copy the status and control register */
841 if (get_user(vfp->hard.fpscr, &ufp->fpscr))
844 vfp_flush_hwstate(thread);
850 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
856 ret = ptrace_read_user(child, addr, (unsigned long __user *)data);
860 ret = ptrace_write_user(child, addr, data);
864 ret = ptrace_getregs(child, (void __user *)data);
868 ret = ptrace_setregs(child, (void __user *)data);
871 case PTRACE_GETFPREGS:
872 ret = ptrace_getfpregs(child, (void __user *)data);
875 case PTRACE_SETFPREGS:
876 ret = ptrace_setfpregs(child, (void __user *)data);
880 case PTRACE_GETWMMXREGS:
881 ret = ptrace_getwmmxregs(child, (void __user *)data);
884 case PTRACE_SETWMMXREGS:
885 ret = ptrace_setwmmxregs(child, (void __user *)data);
889 case PTRACE_GET_THREAD_AREA:
890 ret = put_user(task_thread_info(child)->tp_value,
891 (unsigned long __user *) data);
894 case PTRACE_SET_SYSCALL:
895 task_thread_info(child)->syscall = data;
900 case PTRACE_GETCRUNCHREGS:
901 ret = ptrace_getcrunchregs(child, (void __user *)data);
904 case PTRACE_SETCRUNCHREGS:
905 ret = ptrace_setcrunchregs(child, (void __user *)data);
910 case PTRACE_GETVFPREGS:
911 ret = ptrace_getvfpregs(child, (void __user *)data);
914 case PTRACE_SETVFPREGS:
915 ret = ptrace_setvfpregs(child, (void __user *)data);
920 ret = ptrace_request(child, request, addr, data);
927 asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
931 if (!test_thread_flag(TIF_SYSCALL_TRACE))
933 if (!(current->ptrace & PT_PTRACED))
937 * Save IP. IP is used to denote syscall entry/exit:
938 * IP = 0 -> entry, = 1 -> exit
943 current_thread_info()->syscall = scno;
945 /* the 0x80 provides a way for the tracing parent to distinguish
946 between a syscall stop and SIGTRAP delivery */
947 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
950 * this isn't the same as continuing with a signal, but it will do
951 * for normal use. strace only continues with a signal if the
952 * stopping signal is not SIGTRAP. -brl
954 if (current->exit_code) {
955 send_sig(current->exit_code, current, 1);
956 current->exit_code = 0;
960 return current_thread_info()->syscall;