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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/security.h>
21 #include <linux/init.h>
22 #include <linux/signal.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/traps.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
40 * Breakpoint SWI instruction: SWI &9F0001
42 #define BREAKINST_ARM 0xef9f0001
43 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
46 * New breakpoints - use an undefined instruction. The ARM architecture
47 * reference manual guarantees that the following instruction space
48 * will produce an undefined instruction exception on all CPUs:
50 * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
51 * Thumb: 1101 1110 xxxx xxxx
53 #define BREAKINST_ARM 0xe7f001f0
54 #define BREAKINST_THUMB 0xde01
58 * Get the address of the live pt_regs for the specified task.
59 * These are saved onto the top kernel stack when the process
62 * Note: if a user thread is execve'd from kernel space, the
63 * kernel stack will not be empty on entry to the kernel, so
64 * ptracing these tasks will fail.
66 static inline struct pt_regs *
67 get_user_regs(struct task_struct *task)
69 return (struct pt_regs *)
70 ((unsigned long)task->thread_info + THREAD_SIZE -
71 8 - sizeof(struct pt_regs));
75 * this routine will get a word off of the processes privileged stack.
76 * the offset is how far from the base addr as stored in the THREAD.
77 * this routine assumes that all the privileged stacks are in our
80 static inline long get_user_reg(struct task_struct *task, int offset)
82 return get_user_regs(task)->uregs[offset];
86 * this routine will put a word on the processes privileged stack.
87 * the offset is how far from the base addr as stored in the THREAD.
88 * this routine assumes that all the privileged stacks are in our
92 put_user_reg(struct task_struct *task, int offset, long data)
94 struct pt_regs newregs, *regs = get_user_regs(task);
98 newregs.uregs[offset] = data;
100 if (valid_user_regs(&newregs)) {
101 regs->uregs[offset] = data;
109 read_u32(struct task_struct *task, unsigned long addr, u32 *res)
113 ret = access_process_vm(task, addr, res, sizeof(*res), 0);
115 return ret == sizeof(*res) ? 0 : -EIO;
119 read_instr(struct task_struct *task, unsigned long addr, u32 *res)
125 ret = access_process_vm(task, addr & ~1, &val, sizeof(val), 0);
126 ret = ret == sizeof(val) ? 0 : -EIO;
130 ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
131 ret = ret == sizeof(val) ? 0 : -EIO;
138 * Get value of register `rn' (in the instruction)
141 ptrace_getrn(struct task_struct *child, unsigned long insn)
143 unsigned int reg = (insn >> 16) & 15;
146 val = get_user_reg(child, reg);
148 val = pc_pointer(val + 8);
154 * Get value of operand 2 (in an ALU instruction)
157 ptrace_getaluop2(struct task_struct *child, unsigned long insn)
163 if (insn & 1 << 25) {
165 shift = (insn >> 8) & 15;
168 val = get_user_reg (child, insn & 15);
171 shift = (int)get_user_reg (child, (insn >> 8) & 15);
173 shift = (insn >> 7) & 31;
175 type = (insn >> 5) & 3;
179 case 0: val <<= shift; break;
180 case 1: val >>= shift; break;
182 val = (((signed long)val) >> shift);
185 val = (val >> shift) | (val << (32 - shift));
192 * Get value of operand 2 (in a LDR instruction)
195 ptrace_getldrop2(struct task_struct *child, unsigned long insn)
201 val = get_user_reg(child, insn & 15);
202 shift = (insn >> 7) & 31;
203 type = (insn >> 5) & 3;
206 case 0: val <<= shift; break;
207 case 1: val >>= shift; break;
209 val = (((signed long)val) >> shift);
212 val = (val >> shift) | (val << (32 - shift));
218 #define OP_MASK 0x01e00000
219 #define OP_AND 0x00000000
220 #define OP_EOR 0x00200000
221 #define OP_SUB 0x00400000
222 #define OP_RSB 0x00600000
223 #define OP_ADD 0x00800000
224 #define OP_ADC 0x00a00000
225 #define OP_SBC 0x00c00000
226 #define OP_RSC 0x00e00000
227 #define OP_ORR 0x01800000
228 #define OP_MOV 0x01a00000
229 #define OP_BIC 0x01c00000
230 #define OP_MVN 0x01e00000
233 get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
237 switch (insn & 0x0e000000) {
243 long aluop1, aluop2, ccbit;
245 if ((insn & 0xf000) != 0xf000)
248 aluop1 = ptrace_getrn(child, insn);
249 aluop2 = ptrace_getaluop2(child, insn);
250 ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
252 switch (insn & OP_MASK) {
253 case OP_AND: alt = aluop1 & aluop2; break;
254 case OP_EOR: alt = aluop1 ^ aluop2; break;
255 case OP_SUB: alt = aluop1 - aluop2; break;
256 case OP_RSB: alt = aluop2 - aluop1; break;
257 case OP_ADD: alt = aluop1 + aluop2; break;
258 case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
259 case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
260 case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
261 case OP_ORR: alt = aluop1 | aluop2; break;
262 case OP_MOV: alt = aluop2; break;
263 case OP_BIC: alt = aluop1 & ~aluop2; break;
264 case OP_MVN: alt = ~aluop2; break;
274 if ((insn & 0x0010f000) == 0x0010f000) {
277 base = ptrace_getrn(child, insn);
278 if (insn & 1 << 24) {
281 if (insn & 0x02000000)
282 aluop2 = ptrace_getldrop2(child, insn);
284 aluop2 = insn & 0xfff;
291 if (read_u32(child, base, &alt) == 0)
292 alt = pc_pointer(alt);
300 if ((insn & 0x00108000) == 0x00108000) {
302 unsigned int nr_regs;
304 if (insn & (1 << 23)) {
305 nr_regs = hweight16(insn & 65535) << 2;
307 if (!(insn & (1 << 24)))
310 if (insn & (1 << 24))
316 base = ptrace_getrn(child, insn);
318 if (read_u32(child, base + nr_regs, &alt) == 0)
319 alt = pc_pointer(alt);
329 /* It's a branch/branch link: instead of trying to
330 * figure out whether the branch will be taken or not,
331 * we'll put a breakpoint at both locations. This is
332 * simpler, more reliable, and probably not a whole lot
333 * slower than the alternative approach of emulating the
336 displ = (insn & 0x00ffffff) << 8;
337 displ = (displ >> 6) + 8;
338 if (displ != 0 && displ != 4)
348 swap_insn(struct task_struct *task, unsigned long addr,
349 void *old_insn, void *new_insn, int size)
353 ret = access_process_vm(task, addr, old_insn, size, 0);
355 ret = access_process_vm(task, addr, new_insn, size, 1);
360 add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
362 int nr = dbg->nsaved;
365 u32 new_insn = BREAKINST_ARM;
368 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
371 dbg->bp[nr].address = addr;
375 printk(KERN_ERR "ptrace: too many breakpoints\n");
379 * Clear one breakpoint in the user program. We copy what the hardware
380 * does and use bit 0 of the address to indicate whether this is a Thumb
381 * breakpoint or an ARM breakpoint.
383 static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
385 unsigned long addr = bp->address;
386 union debug_insn old_insn;
390 ret = swap_insn(task, addr & ~1, &old_insn.thumb,
393 if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
394 printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
395 "0x%08lx (0x%04x)\n", task->comm, task->pid,
396 addr, old_insn.thumb);
398 ret = swap_insn(task, addr & ~3, &old_insn.arm,
401 if (ret != 4 || old_insn.arm != BREAKINST_ARM)
402 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
403 "0x%08lx (0x%08x)\n", task->comm, task->pid,
408 void ptrace_set_bpt(struct task_struct *child)
410 struct pt_regs *regs;
415 regs = get_user_regs(child);
416 pc = instruction_pointer(regs);
418 if (thumb_mode(regs)) {
419 printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
423 res = read_instr(child, pc, &insn);
425 struct debug_info *dbg = &child->thread.debug;
430 alt = get_branch_address(child, pc, insn);
432 add_breakpoint(child, dbg, alt);
435 * Note that we ignore the result of setting the above
436 * breakpoint since it may fail. When it does, this is
437 * not so much an error, but a forewarning that we may
438 * be receiving a prefetch abort shortly.
440 * If we don't set this breakpoint here, then we can
441 * lose control of the thread during single stepping.
443 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
444 add_breakpoint(child, dbg, pc + 4);
449 * Ensure no single-step breakpoint is pending. Returns non-zero
450 * value if child was being single-stepped.
452 void ptrace_cancel_bpt(struct task_struct *child)
454 int i, nsaved = child->thread.debug.nsaved;
456 child->thread.debug.nsaved = 0;
459 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
463 for (i = 0; i < nsaved; i++)
464 clear_breakpoint(child, &child->thread.debug.bp[i]);
468 * Called by kernel/ptrace.c when detaching..
470 * Make sure the single step bit is not set.
472 void ptrace_disable(struct task_struct *child)
474 child->ptrace &= ~PT_SINGLESTEP;
475 ptrace_cancel_bpt(child);
479 * Handle hitting a breakpoint.
481 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
485 ptrace_cancel_bpt(tsk);
487 info.si_signo = SIGTRAP;
489 info.si_code = TRAP_BRKPT;
490 info.si_addr = (void __user *)instruction_pointer(regs);
492 force_sig_info(SIGTRAP, &info, tsk);
495 static int break_trap(struct pt_regs *regs, unsigned int instr)
497 ptrace_break(current, regs);
501 static struct undef_hook arm_break_hook = {
502 .instr_mask = 0x0fffffff,
503 .instr_val = 0x07f001f0,
504 .cpsr_mask = PSR_T_BIT,
509 static struct undef_hook thumb_break_hook = {
510 .instr_mask = 0xffff,
512 .cpsr_mask = PSR_T_BIT,
513 .cpsr_val = PSR_T_BIT,
517 static int __init ptrace_break_init(void)
519 register_undef_hook(&arm_break_hook);
520 register_undef_hook(&thumb_break_hook);
524 core_initcall(ptrace_break_init);
527 * Read the word at offset "off" into the "struct user". We
528 * actually access the pt_regs stored on the kernel stack.
530 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
531 unsigned long __user *ret)
535 if (off & 3 || off >= sizeof(struct user))
539 if (off < sizeof(struct pt_regs))
540 tmp = get_user_reg(tsk, off >> 2);
542 return put_user(tmp, ret);
546 * Write the word at offset "off" into "struct user". We
547 * actually access the pt_regs stored on the kernel stack.
549 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
552 if (off & 3 || off >= sizeof(struct user))
555 if (off >= sizeof(struct pt_regs))
558 return put_user_reg(tsk, off >> 2, val);
562 * Get all user integer registers.
564 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
566 struct pt_regs *regs = get_user_regs(tsk);
568 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
572 * Set all user integer registers.
574 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
576 struct pt_regs newregs;
580 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
581 struct pt_regs *regs = get_user_regs(tsk);
584 if (valid_user_regs(&newregs)) {
594 * Get the child FPU state.
596 static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
598 return copy_to_user(ufp, &tsk->thread_info->fpstate,
599 sizeof(struct user_fp)) ? -EFAULT : 0;
603 * Set the child FPU state.
605 static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
607 struct thread_info *thread = tsk->thread_info;
608 thread->used_cp[1] = thread->used_cp[2] = 1;
609 return copy_from_user(&thread->fpstate, ufp,
610 sizeof(struct user_fp)) ? -EFAULT : 0;
616 * Get the child iWMMXt state.
618 static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
620 struct thread_info *thread = tsk->thread_info;
621 void *ptr = &thread->fpstate;
623 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
625 iwmmxt_task_disable(thread); /* force it to ram */
626 /* The iWMMXt state is stored doubleword-aligned. */
627 if (((long) ptr) & 4)
629 return copy_to_user(ufp, ptr, 0x98) ? -EFAULT : 0;
633 * Set the child iWMMXt state.
635 static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
637 struct thread_info *thread = tsk->thread_info;
638 void *ptr = &thread->fpstate;
640 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
642 iwmmxt_task_release(thread); /* force a reload */
643 /* The iWMMXt state is stored doubleword-aligned. */
644 if (((long) ptr) & 4)
646 return copy_from_user(ptr, ufp, 0x98) ? -EFAULT : 0;
651 static int do_ptrace(int request, struct task_struct *child, long addr, long data)
658 * read word at location "addr" in the child process.
660 case PTRACE_PEEKTEXT:
661 case PTRACE_PEEKDATA:
662 ret = access_process_vm(child, addr, &tmp,
663 sizeof(unsigned long), 0);
664 if (ret == sizeof(unsigned long))
665 ret = put_user(tmp, (unsigned long __user *) data);
671 ret = ptrace_read_user(child, addr, (unsigned long __user *)data);
675 * write the word at location addr.
677 case PTRACE_POKETEXT:
678 case PTRACE_POKEDATA:
679 ret = access_process_vm(child, addr, &data,
680 sizeof(unsigned long), 1);
681 if (ret == sizeof(unsigned long))
688 ret = ptrace_write_user(child, addr, data);
692 * continue/restart and stop at next (return from) syscall
697 if (!valid_signal(data))
699 if (request == PTRACE_SYSCALL)
700 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
702 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
703 child->exit_code = data;
704 /* make sure single-step breakpoint is gone. */
705 child->ptrace &= ~PT_SINGLESTEP;
706 ptrace_cancel_bpt(child);
707 wake_up_process(child);
712 * make the child exit. Best I can do is send it a sigkill.
713 * perhaps it should be put in the status that it wants to
717 /* make sure single-step breakpoint is gone. */
718 child->ptrace &= ~PT_SINGLESTEP;
719 ptrace_cancel_bpt(child);
720 if (child->exit_state != EXIT_ZOMBIE) {
721 child->exit_code = SIGKILL;
722 wake_up_process(child);
728 * execute single instruction.
730 case PTRACE_SINGLESTEP:
732 if (!valid_signal(data))
734 child->ptrace |= PT_SINGLESTEP;
735 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
736 child->exit_code = data;
737 /* give it a chance to run. */
738 wake_up_process(child);
743 ret = ptrace_detach(child, data);
747 ret = ptrace_getregs(child, (void __user *)data);
751 ret = ptrace_setregs(child, (void __user *)data);
754 case PTRACE_GETFPREGS:
755 ret = ptrace_getfpregs(child, (void __user *)data);
758 case PTRACE_SETFPREGS:
759 ret = ptrace_setfpregs(child, (void __user *)data);
763 case PTRACE_GETWMMXREGS:
764 ret = ptrace_getwmmxregs(child, (void __user *)data);
767 case PTRACE_SETWMMXREGS:
768 ret = ptrace_setwmmxregs(child, (void __user *)data);
772 case PTRACE_GET_THREAD_AREA:
773 ret = put_user(child->thread_info->tp_value,
774 (unsigned long __user *) data);
778 ret = ptrace_request(child, request, addr, data);
785 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
787 struct task_struct *child;
792 if (request == PTRACE_TRACEME) {
793 /* are we already being traced? */
794 if (current->ptrace & PT_PTRACED)
796 ret = security_ptrace(current->parent, current);
799 /* set the ptrace bit in the process flags. */
800 current->ptrace |= PT_PTRACED;
805 read_lock(&tasklist_lock);
806 child = find_task_by_pid(pid);
808 get_task_struct(child);
809 read_unlock(&tasklist_lock);
814 if (pid == 1) /* you may not mess with init */
817 if (request == PTRACE_ATTACH) {
818 ret = ptrace_attach(child);
821 ret = ptrace_check_attach(child, request == PTRACE_KILL);
823 ret = do_ptrace(request, child, addr, data);
826 put_task_struct(child);
832 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
836 if (!test_thread_flag(TIF_SYSCALL_TRACE))
838 if (!(current->ptrace & PT_PTRACED))
842 * Save IP. IP is used to denote syscall entry/exit:
843 * IP = 0 -> entry, = 1 -> exit
848 /* the 0x80 provides a way for the tracing parent to distinguish
849 between a syscall stop and SIGTRAP delivery */
850 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
853 * this isn't the same as continuing with a signal, but it will do
854 * for normal use. strace only continues with a signal if the
855 * stopping signal is not SIGTRAP. -brl
857 if (current->exit_code) {
858 send_sig(current->exit_code, current, 1);
859 current->exit_code = 0;