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 & 0x0fffffd0) == 0x012fff10) {
249 alt = get_user_reg(child, insn & 15);
254 if ((insn & 0xf000) != 0xf000)
257 aluop1 = ptrace_getrn(child, insn);
258 aluop2 = ptrace_getaluop2(child, insn);
259 ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
261 switch (insn & OP_MASK) {
262 case OP_AND: alt = aluop1 & aluop2; break;
263 case OP_EOR: alt = aluop1 ^ aluop2; break;
264 case OP_SUB: alt = aluop1 - aluop2; break;
265 case OP_RSB: alt = aluop2 - aluop1; break;
266 case OP_ADD: alt = aluop1 + aluop2; break;
267 case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
268 case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
269 case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
270 case OP_ORR: alt = aluop1 | aluop2; break;
271 case OP_MOV: alt = aluop2; break;
272 case OP_BIC: alt = aluop1 & ~aluop2; break;
273 case OP_MVN: alt = ~aluop2; break;
283 if ((insn & 0x0010f000) == 0x0010f000) {
286 base = ptrace_getrn(child, insn);
287 if (insn & 1 << 24) {
290 if (insn & 0x02000000)
291 aluop2 = ptrace_getldrop2(child, insn);
293 aluop2 = insn & 0xfff;
300 if (read_u32(child, base, &alt) == 0)
301 alt = pc_pointer(alt);
309 if ((insn & 0x00108000) == 0x00108000) {
311 unsigned int nr_regs;
313 if (insn & (1 << 23)) {
314 nr_regs = hweight16(insn & 65535) << 2;
316 if (!(insn & (1 << 24)))
319 if (insn & (1 << 24))
325 base = ptrace_getrn(child, insn);
327 if (read_u32(child, base + nr_regs, &alt) == 0)
328 alt = pc_pointer(alt);
338 /* It's a branch/branch link: instead of trying to
339 * figure out whether the branch will be taken or not,
340 * we'll put a breakpoint at both locations. This is
341 * simpler, more reliable, and probably not a whole lot
342 * slower than the alternative approach of emulating the
345 displ = (insn & 0x00ffffff) << 8;
346 displ = (displ >> 6) + 8;
347 if (displ != 0 && displ != 4)
357 swap_insn(struct task_struct *task, unsigned long addr,
358 void *old_insn, void *new_insn, int size)
362 ret = access_process_vm(task, addr, old_insn, size, 0);
364 ret = access_process_vm(task, addr, new_insn, size, 1);
369 add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
371 int nr = dbg->nsaved;
374 u32 new_insn = BREAKINST_ARM;
377 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
380 dbg->bp[nr].address = addr;
384 printk(KERN_ERR "ptrace: too many breakpoints\n");
388 * Clear one breakpoint in the user program. We copy what the hardware
389 * does and use bit 0 of the address to indicate whether this is a Thumb
390 * breakpoint or an ARM breakpoint.
392 static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
394 unsigned long addr = bp->address;
395 union debug_insn old_insn;
399 ret = swap_insn(task, addr & ~1, &old_insn.thumb,
402 if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
403 printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
404 "0x%08lx (0x%04x)\n", task->comm, task->pid,
405 addr, old_insn.thumb);
407 ret = swap_insn(task, addr & ~3, &old_insn.arm,
410 if (ret != 4 || old_insn.arm != BREAKINST_ARM)
411 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
412 "0x%08lx (0x%08x)\n", task->comm, task->pid,
417 void ptrace_set_bpt(struct task_struct *child)
419 struct pt_regs *regs;
424 regs = get_user_regs(child);
425 pc = instruction_pointer(regs);
427 if (thumb_mode(regs)) {
428 printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
432 res = read_instr(child, pc, &insn);
434 struct debug_info *dbg = &child->thread.debug;
439 alt = get_branch_address(child, pc, insn);
441 add_breakpoint(child, dbg, alt);
444 * Note that we ignore the result of setting the above
445 * breakpoint since it may fail. When it does, this is
446 * not so much an error, but a forewarning that we may
447 * be receiving a prefetch abort shortly.
449 * If we don't set this breakpoint here, then we can
450 * lose control of the thread during single stepping.
452 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
453 add_breakpoint(child, dbg, pc + 4);
458 * Ensure no single-step breakpoint is pending. Returns non-zero
459 * value if child was being single-stepped.
461 void ptrace_cancel_bpt(struct task_struct *child)
463 int i, nsaved = child->thread.debug.nsaved;
465 child->thread.debug.nsaved = 0;
468 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
472 for (i = 0; i < nsaved; i++)
473 clear_breakpoint(child, &child->thread.debug.bp[i]);
477 * Called by kernel/ptrace.c when detaching..
479 * Make sure the single step bit is not set.
481 void ptrace_disable(struct task_struct *child)
483 child->ptrace &= ~PT_SINGLESTEP;
484 ptrace_cancel_bpt(child);
488 * Handle hitting a breakpoint.
490 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
494 ptrace_cancel_bpt(tsk);
496 info.si_signo = SIGTRAP;
498 info.si_code = TRAP_BRKPT;
499 info.si_addr = (void __user *)instruction_pointer(regs);
501 force_sig_info(SIGTRAP, &info, tsk);
504 static int break_trap(struct pt_regs *regs, unsigned int instr)
506 ptrace_break(current, regs);
510 static struct undef_hook arm_break_hook = {
511 .instr_mask = 0x0fffffff,
512 .instr_val = 0x07f001f0,
513 .cpsr_mask = PSR_T_BIT,
518 static struct undef_hook thumb_break_hook = {
519 .instr_mask = 0xffff,
521 .cpsr_mask = PSR_T_BIT,
522 .cpsr_val = PSR_T_BIT,
526 static int __init ptrace_break_init(void)
528 register_undef_hook(&arm_break_hook);
529 register_undef_hook(&thumb_break_hook);
533 core_initcall(ptrace_break_init);
536 * Read the word at offset "off" into the "struct user". We
537 * actually access the pt_regs stored on the kernel stack.
539 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
540 unsigned long __user *ret)
544 if (off & 3 || off >= sizeof(struct user))
548 if (off < sizeof(struct pt_regs))
549 tmp = get_user_reg(tsk, off >> 2);
551 return put_user(tmp, ret);
555 * Write the word at offset "off" into "struct user". We
556 * actually access the pt_regs stored on the kernel stack.
558 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
561 if (off & 3 || off >= sizeof(struct user))
564 if (off >= sizeof(struct pt_regs))
567 return put_user_reg(tsk, off >> 2, val);
571 * Get all user integer registers.
573 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
575 struct pt_regs *regs = get_user_regs(tsk);
577 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
581 * Set all user integer registers.
583 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
585 struct pt_regs newregs;
589 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
590 struct pt_regs *regs = get_user_regs(tsk);
593 if (valid_user_regs(&newregs)) {
603 * Get the child FPU state.
605 static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
607 return copy_to_user(ufp, &tsk->thread_info->fpstate,
608 sizeof(struct user_fp)) ? -EFAULT : 0;
612 * Set the child FPU state.
614 static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
616 struct thread_info *thread = tsk->thread_info;
617 thread->used_cp[1] = thread->used_cp[2] = 1;
618 return copy_from_user(&thread->fpstate, ufp,
619 sizeof(struct user_fp)) ? -EFAULT : 0;
625 * Get the child iWMMXt state.
627 static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
629 struct thread_info *thread = tsk->thread_info;
630 void *ptr = &thread->fpstate;
632 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
634 iwmmxt_task_disable(thread); /* force it to ram */
635 /* The iWMMXt state is stored doubleword-aligned. */
636 if (((long) ptr) & 4)
638 return copy_to_user(ufp, ptr, 0x98) ? -EFAULT : 0;
642 * Set the child iWMMXt state.
644 static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
646 struct thread_info *thread = tsk->thread_info;
647 void *ptr = &thread->fpstate;
649 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
651 iwmmxt_task_release(thread); /* force a reload */
652 /* The iWMMXt state is stored doubleword-aligned. */
653 if (((long) ptr) & 4)
655 return copy_from_user(ptr, ufp, 0x98) ? -EFAULT : 0;
660 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
667 * read word at location "addr" in the child process.
669 case PTRACE_PEEKTEXT:
670 case PTRACE_PEEKDATA:
671 ret = access_process_vm(child, addr, &tmp,
672 sizeof(unsigned long), 0);
673 if (ret == sizeof(unsigned long))
674 ret = put_user(tmp, (unsigned long __user *) data);
680 ret = ptrace_read_user(child, addr, (unsigned long __user *)data);
684 * write the word at location addr.
686 case PTRACE_POKETEXT:
687 case PTRACE_POKEDATA:
688 ret = access_process_vm(child, addr, &data,
689 sizeof(unsigned long), 1);
690 if (ret == sizeof(unsigned long))
697 ret = ptrace_write_user(child, addr, data);
701 * continue/restart and stop at next (return from) syscall
706 if (!valid_signal(data))
708 if (request == PTRACE_SYSCALL)
709 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
711 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
712 child->exit_code = data;
713 /* make sure single-step breakpoint is gone. */
714 child->ptrace &= ~PT_SINGLESTEP;
715 ptrace_cancel_bpt(child);
716 wake_up_process(child);
721 * make the child exit. Best I can do is send it a sigkill.
722 * perhaps it should be put in the status that it wants to
726 /* make sure single-step breakpoint is gone. */
727 child->ptrace &= ~PT_SINGLESTEP;
728 ptrace_cancel_bpt(child);
729 if (child->exit_state != EXIT_ZOMBIE) {
730 child->exit_code = SIGKILL;
731 wake_up_process(child);
737 * execute single instruction.
739 case PTRACE_SINGLESTEP:
741 if (!valid_signal(data))
743 child->ptrace |= PT_SINGLESTEP;
744 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
745 child->exit_code = data;
746 /* give it a chance to run. */
747 wake_up_process(child);
752 ret = ptrace_detach(child, data);
756 ret = ptrace_getregs(child, (void __user *)data);
760 ret = ptrace_setregs(child, (void __user *)data);
763 case PTRACE_GETFPREGS:
764 ret = ptrace_getfpregs(child, (void __user *)data);
767 case PTRACE_SETFPREGS:
768 ret = ptrace_setfpregs(child, (void __user *)data);
772 case PTRACE_GETWMMXREGS:
773 ret = ptrace_getwmmxregs(child, (void __user *)data);
776 case PTRACE_SETWMMXREGS:
777 ret = ptrace_setwmmxregs(child, (void __user *)data);
781 case PTRACE_GET_THREAD_AREA:
782 ret = put_user(child->thread_info->tp_value,
783 (unsigned long __user *) data);
787 ret = ptrace_request(child, request, addr, data);
794 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
798 if (!test_thread_flag(TIF_SYSCALL_TRACE))
800 if (!(current->ptrace & PT_PTRACED))
804 * Save IP. IP is used to denote syscall entry/exit:
805 * IP = 0 -> entry, = 1 -> exit
810 /* the 0x80 provides a way for the tracing parent to distinguish
811 between a syscall stop and SIGTRAP delivery */
812 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
815 * this isn't the same as continuing with a signal, but it will do
816 * for normal use. strace only continues with a signal if the
817 * stopping signal is not SIGTRAP. -brl
819 if (current->exit_code) {
820 send_sig(current->exit_code, current, 1);
821 current->exit_code = 0;