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>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/traps.h>
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
39 * Breakpoint SWI instruction: SWI &9F0001
41 #define BREAKINST_ARM 0xef9f0001
42 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
45 * New breakpoints - use an undefined instruction. The ARM architecture
46 * reference manual guarantees that the following instruction space
47 * will produce an undefined instruction exception on all CPUs:
49 * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
50 * Thumb: 1101 1110 xxxx xxxx
52 #define BREAKINST_ARM 0xe7f001f0
53 #define BREAKINST_THUMB 0xde01
57 * Get the address of the live pt_regs for the specified task.
58 * These are saved onto the top kernel stack when the process
61 * Note: if a user thread is execve'd from kernel space, the
62 * kernel stack will not be empty on entry to the kernel, so
63 * ptracing these tasks will fail.
65 static inline struct pt_regs *
66 get_user_regs(struct task_struct *task)
68 return (struct pt_regs *)
69 ((unsigned long)task->thread_info + THREAD_SIZE -
70 8 - sizeof(struct pt_regs));
74 * this routine will get a word off of the processes privileged stack.
75 * the offset is how far from the base addr as stored in the THREAD.
76 * this routine assumes that all the privileged stacks are in our
79 static inline long get_user_reg(struct task_struct *task, int offset)
81 return get_user_regs(task)->uregs[offset];
85 * this routine will put a word on the processes privileged stack.
86 * the offset is how far from the base addr as stored in the THREAD.
87 * this routine assumes that all the privileged stacks are in our
91 put_user_reg(struct task_struct *task, int offset, long data)
93 struct pt_regs newregs, *regs = get_user_regs(task);
97 newregs.uregs[offset] = data;
99 if (valid_user_regs(&newregs)) {
100 regs->uregs[offset] = data;
108 read_u32(struct task_struct *task, unsigned long addr, u32 *res)
112 ret = access_process_vm(task, addr, res, sizeof(*res), 0);
114 return ret == sizeof(*res) ? 0 : -EIO;
118 read_instr(struct task_struct *task, unsigned long addr, u32 *res)
124 ret = access_process_vm(task, addr & ~1, &val, sizeof(val), 0);
125 ret = ret == sizeof(val) ? 0 : -EIO;
129 ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
130 ret = ret == sizeof(val) ? 0 : -EIO;
137 * Get value of register `rn' (in the instruction)
140 ptrace_getrn(struct task_struct *child, unsigned long insn)
142 unsigned int reg = (insn >> 16) & 15;
145 val = get_user_reg(child, reg);
147 val = pc_pointer(val + 8);
153 * Get value of operand 2 (in an ALU instruction)
156 ptrace_getaluop2(struct task_struct *child, unsigned long insn)
162 if (insn & 1 << 25) {
164 shift = (insn >> 8) & 15;
167 val = get_user_reg (child, insn & 15);
170 shift = (int)get_user_reg (child, (insn >> 8) & 15);
172 shift = (insn >> 7) & 31;
174 type = (insn >> 5) & 3;
178 case 0: val <<= shift; break;
179 case 1: val >>= shift; break;
181 val = (((signed long)val) >> shift);
184 val = (val >> shift) | (val << (32 - shift));
191 * Get value of operand 2 (in a LDR instruction)
194 ptrace_getldrop2(struct task_struct *child, unsigned long insn)
200 val = get_user_reg(child, insn & 15);
201 shift = (insn >> 7) & 31;
202 type = (insn >> 5) & 3;
205 case 0: val <<= shift; break;
206 case 1: val >>= shift; break;
208 val = (((signed long)val) >> shift);
211 val = (val >> shift) | (val << (32 - shift));
217 #define OP_MASK 0x01e00000
218 #define OP_AND 0x00000000
219 #define OP_EOR 0x00200000
220 #define OP_SUB 0x00400000
221 #define OP_RSB 0x00600000
222 #define OP_ADD 0x00800000
223 #define OP_ADC 0x00a00000
224 #define OP_SBC 0x00c00000
225 #define OP_RSC 0x00e00000
226 #define OP_ORR 0x01800000
227 #define OP_MOV 0x01a00000
228 #define OP_BIC 0x01c00000
229 #define OP_MVN 0x01e00000
232 get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
236 switch (insn & 0x0e000000) {
242 long aluop1, aluop2, ccbit;
244 if ((insn & 0xf000) != 0xf000)
247 aluop1 = ptrace_getrn(child, insn);
248 aluop2 = ptrace_getaluop2(child, insn);
249 ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
251 switch (insn & OP_MASK) {
252 case OP_AND: alt = aluop1 & aluop2; break;
253 case OP_EOR: alt = aluop1 ^ aluop2; break;
254 case OP_SUB: alt = aluop1 - aluop2; break;
255 case OP_RSB: alt = aluop2 - aluop1; break;
256 case OP_ADD: alt = aluop1 + aluop2; break;
257 case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
258 case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
259 case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
260 case OP_ORR: alt = aluop1 | aluop2; break;
261 case OP_MOV: alt = aluop2; break;
262 case OP_BIC: alt = aluop1 & ~aluop2; break;
263 case OP_MVN: alt = ~aluop2; break;
273 if ((insn & 0x0010f000) == 0x0010f000) {
276 base = ptrace_getrn(child, insn);
277 if (insn & 1 << 24) {
280 if (insn & 0x02000000)
281 aluop2 = ptrace_getldrop2(child, insn);
283 aluop2 = insn & 0xfff;
290 if (read_u32(child, base, &alt) == 0)
291 alt = pc_pointer(alt);
299 if ((insn & 0x00108000) == 0x00108000) {
301 unsigned int nr_regs;
303 if (insn & (1 << 23)) {
304 nr_regs = hweight16(insn & 65535) << 2;
306 if (!(insn & (1 << 24)))
309 if (insn & (1 << 24))
315 base = ptrace_getrn(child, insn);
317 if (read_u32(child, base + nr_regs, &alt) == 0)
318 alt = pc_pointer(alt);
328 /* It's a branch/branch link: instead of trying to
329 * figure out whether the branch will be taken or not,
330 * we'll put a breakpoint at both locations. This is
331 * simpler, more reliable, and probably not a whole lot
332 * slower than the alternative approach of emulating the
335 displ = (insn & 0x00ffffff) << 8;
336 displ = (displ >> 6) + 8;
337 if (displ != 0 && displ != 4)
347 swap_insn(struct task_struct *task, unsigned long addr,
348 void *old_insn, void *new_insn, int size)
352 ret = access_process_vm(task, addr, old_insn, size, 0);
354 ret = access_process_vm(task, addr, new_insn, size, 1);
359 add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
361 int nr = dbg->nsaved;
364 u32 new_insn = BREAKINST_ARM;
367 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
370 dbg->bp[nr].address = addr;
374 printk(KERN_ERR "ptrace: too many breakpoints\n");
378 * Clear one breakpoint in the user program. We copy what the hardware
379 * does and use bit 0 of the address to indicate whether this is a Thumb
380 * breakpoint or an ARM breakpoint.
382 static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
384 unsigned long addr = bp->address;
385 union debug_insn old_insn;
389 ret = swap_insn(task, addr & ~1, &old_insn.thumb,
392 if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
393 printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
394 "0x%08lx (0x%04x)\n", task->comm, task->pid,
395 addr, old_insn.thumb);
397 ret = swap_insn(task, addr & ~3, &old_insn.arm,
400 if (ret != 4 || old_insn.arm != BREAKINST_ARM)
401 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
402 "0x%08lx (0x%08x)\n", task->comm, task->pid,
407 void ptrace_set_bpt(struct task_struct *child)
409 struct pt_regs *regs;
414 regs = get_user_regs(child);
415 pc = instruction_pointer(regs);
417 if (thumb_mode(regs)) {
418 printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
422 res = read_instr(child, pc, &insn);
424 struct debug_info *dbg = &child->thread.debug;
429 alt = get_branch_address(child, pc, insn);
431 add_breakpoint(child, dbg, alt);
434 * Note that we ignore the result of setting the above
435 * breakpoint since it may fail. When it does, this is
436 * not so much an error, but a forewarning that we may
437 * be receiving a prefetch abort shortly.
439 * If we don't set this breakpoint here, then we can
440 * lose control of the thread during single stepping.
442 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
443 add_breakpoint(child, dbg, pc + 4);
448 * Ensure no single-step breakpoint is pending. Returns non-zero
449 * value if child was being single-stepped.
451 void ptrace_cancel_bpt(struct task_struct *child)
453 int i, nsaved = child->thread.debug.nsaved;
455 child->thread.debug.nsaved = 0;
458 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
462 for (i = 0; i < nsaved; i++)
463 clear_breakpoint(child, &child->thread.debug.bp[i]);
467 * Called by kernel/ptrace.c when detaching..
469 * Make sure the single step bit is not set.
471 void ptrace_disable(struct task_struct *child)
473 child->ptrace &= ~PT_SINGLESTEP;
474 ptrace_cancel_bpt(child);
478 * Handle hitting a breakpoint.
480 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
484 ptrace_cancel_bpt(tsk);
486 info.si_signo = SIGTRAP;
488 info.si_code = TRAP_BRKPT;
489 info.si_addr = (void __user *)instruction_pointer(regs);
491 force_sig_info(SIGTRAP, &info, tsk);
494 static int break_trap(struct pt_regs *regs, unsigned int instr)
496 ptrace_break(current, regs);
500 static struct undef_hook arm_break_hook = {
501 .instr_mask = 0x0fffffff,
502 .instr_val = 0x07f001f0,
503 .cpsr_mask = PSR_T_BIT,
508 static struct undef_hook thumb_break_hook = {
509 .instr_mask = 0xffff,
511 .cpsr_mask = PSR_T_BIT,
512 .cpsr_val = PSR_T_BIT,
516 static int __init ptrace_break_init(void)
518 register_undef_hook(&arm_break_hook);
519 register_undef_hook(&thumb_break_hook);
523 core_initcall(ptrace_break_init);
526 * Read the word at offset "off" into the "struct user". We
527 * actually access the pt_regs stored on the kernel stack.
529 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
530 unsigned long __user *ret)
534 if (off & 3 || off >= sizeof(struct user))
538 if (off < sizeof(struct pt_regs))
539 tmp = get_user_reg(tsk, off >> 2);
541 return put_user(tmp, ret);
545 * Write the word at offset "off" into "struct user". We
546 * actually access the pt_regs stored on the kernel stack.
548 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
551 if (off & 3 || off >= sizeof(struct user))
554 if (off >= sizeof(struct pt_regs))
557 return put_user_reg(tsk, off >> 2, val);
561 * Get all user integer registers.
563 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
565 struct pt_regs *regs = get_user_regs(tsk);
567 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
571 * Set all user integer registers.
573 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
575 struct pt_regs newregs;
579 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
580 struct pt_regs *regs = get_user_regs(tsk);
583 if (valid_user_regs(&newregs)) {
593 * Get the child FPU state.
595 static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
597 return copy_to_user(ufp, &tsk->thread_info->fpstate,
598 sizeof(struct user_fp)) ? -EFAULT : 0;
602 * Set the child FPU state.
604 static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
606 struct thread_info *thread = tsk->thread_info;
607 thread->used_cp[1] = thread->used_cp[2] = 1;
608 return copy_from_user(&thread->fpstate, ufp,
609 sizeof(struct user_fp)) ? -EFAULT : 0;
615 * Get the child iWMMXt state.
617 static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
619 struct thread_info *thread = tsk->thread_info;
620 void *ptr = &thread->fpstate;
622 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
624 iwmmxt_task_disable(thread); /* force it to ram */
625 /* The iWMMXt state is stored doubleword-aligned. */
626 if (((long) ptr) & 4)
628 return copy_to_user(ufp, ptr, 0x98) ? -EFAULT : 0;
632 * Set the child iWMMXt state.
634 static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
636 struct thread_info *thread = tsk->thread_info;
637 void *ptr = &thread->fpstate;
639 if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
641 iwmmxt_task_release(thread); /* force a reload */
642 /* The iWMMXt state is stored doubleword-aligned. */
643 if (((long) ptr) & 4)
645 return copy_from_user(ptr, ufp, 0x98) ? -EFAULT : 0;
650 static int do_ptrace(int request, struct task_struct *child, long addr, long data)
657 * read word at location "addr" in the child process.
659 case PTRACE_PEEKTEXT:
660 case PTRACE_PEEKDATA:
661 ret = access_process_vm(child, addr, &tmp,
662 sizeof(unsigned long), 0);
663 if (ret == sizeof(unsigned long))
664 ret = put_user(tmp, (unsigned long __user *) data);
670 ret = ptrace_read_user(child, addr, (unsigned long __user *)data);
674 * write the word at location addr.
676 case PTRACE_POKETEXT:
677 case PTRACE_POKEDATA:
678 ret = access_process_vm(child, addr, &data,
679 sizeof(unsigned long), 1);
680 if (ret == sizeof(unsigned long))
687 ret = ptrace_write_user(child, addr, data);
691 * continue/restart and stop at next (return from) syscall
696 if ((unsigned long) data > _NSIG)
698 if (request == PTRACE_SYSCALL)
699 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
701 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
702 child->exit_code = data;
703 /* make sure single-step breakpoint is gone. */
704 child->ptrace &= ~PT_SINGLESTEP;
705 ptrace_cancel_bpt(child);
706 wake_up_process(child);
711 * make the child exit. Best I can do is send it a sigkill.
712 * perhaps it should be put in the status that it wants to
716 /* make sure single-step breakpoint is gone. */
717 child->ptrace &= ~PT_SINGLESTEP;
718 ptrace_cancel_bpt(child);
719 if (child->exit_state != EXIT_ZOMBIE) {
720 child->exit_code = SIGKILL;
721 wake_up_process(child);
727 * execute single instruction.
729 case PTRACE_SINGLESTEP:
731 if ((unsigned long) data > _NSIG)
733 child->ptrace |= PT_SINGLESTEP;
734 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
735 child->exit_code = data;
736 /* give it a chance to run. */
737 wake_up_process(child);
742 ret = ptrace_detach(child, data);
746 ret = ptrace_getregs(child, (void __user *)data);
750 ret = ptrace_setregs(child, (void __user *)data);
753 case PTRACE_GETFPREGS:
754 ret = ptrace_getfpregs(child, (void __user *)data);
757 case PTRACE_SETFPREGS:
758 ret = ptrace_setfpregs(child, (void __user *)data);
762 case PTRACE_GETWMMXREGS:
763 ret = ptrace_getwmmxregs(child, (void __user *)data);
766 case PTRACE_SETWMMXREGS:
767 ret = ptrace_setwmmxregs(child, (void __user *)data);
771 case PTRACE_GET_THREAD_AREA:
772 ret = put_user(child->thread_info->tp_value,
773 (unsigned long __user *) data);
777 ret = ptrace_request(child, request, addr, data);
784 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
786 struct task_struct *child;
791 if (request == PTRACE_TRACEME) {
792 /* are we already being traced? */
793 if (current->ptrace & PT_PTRACED)
795 ret = security_ptrace(current->parent, current);
798 /* set the ptrace bit in the process flags. */
799 current->ptrace |= PT_PTRACED;
804 read_lock(&tasklist_lock);
805 child = find_task_by_pid(pid);
807 get_task_struct(child);
808 read_unlock(&tasklist_lock);
813 if (pid == 1) /* you may not mess with init */
816 if (request == PTRACE_ATTACH) {
817 ret = ptrace_attach(child);
820 ret = ptrace_check_attach(child, request == PTRACE_KILL);
822 ret = do_ptrace(request, child, addr, data);
825 put_task_struct(child);
831 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
835 if (!test_thread_flag(TIF_SYSCALL_TRACE))
837 if (!(current->ptrace & PT_PTRACED))
841 * Save IP. IP is used to denote syscall entry/exit:
842 * IP = 0 -> entry, = 1 -> exit
847 /* the 0x80 provides a way for the tracing parent to distinguish
848 between a syscall stop and SIGTRAP delivery */
849 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
852 * this isn't the same as continuing with a signal, but it will do
853 * for normal use. strace only continues with a signal if the
854 * stopping signal is not SIGTRAP. -brl
856 if (current->exit_code) {
857 send_sig(current->exit_code, current, 1);
858 current->exit_code = 0;