2 * linux/arch/m32r/kernel/ptrace.c
4 * Copyright (C) 2002 Hirokazu Takata, Takeo Takahashi
5 * Copyright (C) 2004 Hirokazu Takata, Kei Sakamoto
7 * Original x86 implementation:
9 * edited by Linus Torvalds
11 * Some code taken from sh version:
12 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
13 * Some code taken from arm version:
14 * Copyright (C) 2000 Russell King
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/string.h>
27 #include <linux/signal.h>
29 #include <asm/cacheflush.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/system.h>
34 #include <asm/processor.h>
35 #include <asm/mmu_context.h>
38 * Get the address of the live pt_regs for the specified task.
39 * These are saved onto the top kernel stack when the process
42 * Note: if a user thread is execve'd from kernel space, the
43 * kernel stack will not be empty on entry to the kernel, so
44 * ptracing these tasks will fail.
46 static inline struct pt_regs *
47 get_user_regs(struct task_struct *task)
49 return (struct pt_regs *)
50 ((unsigned long)task->thread_info + THREAD_SIZE
51 - sizeof(struct pt_regs));
55 * This routine will get a word off of the process kernel stack.
57 static inline unsigned long int
58 get_stack_long(struct task_struct *task, int offset)
62 stack = (unsigned long *)get_user_regs(task);
68 * This routine will put a word on the process kernel stack.
71 put_stack_long(struct task_struct *task, int offset, unsigned long data)
75 stack = (unsigned long *)get_user_regs(task);
81 static int reg_offset[] = {
82 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
83 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_FP, PT_LR, PT_SPU,
87 * Read the word at offset "off" into the "struct user". We
88 * actually access the pt_regs stored on the kernel stack.
90 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
91 unsigned long __user *data)
95 struct user * dummy = NULL;
98 if ((off & 3) || (off < 0) || (off > sizeof(struct user) - 3))
104 __asm__ __volatile__ (
111 psw = get_stack_long(tsk, PT_PSW);
112 tmp = ((psw >> 8) & 1);
116 unsigned long psw, bbpsw;
117 psw = get_stack_long(tsk, PT_PSW);
118 bbpsw = get_stack_long(tsk, PT_BBPSW);
119 tmp = ((psw >> 8) & 0xff) | ((bbpsw & 0xff) << 8);
123 tmp = get_stack_long(tsk, PT_BPC);
129 if (off < (sizeof(struct pt_regs) >> 2))
130 tmp = get_stack_long(tsk, off);
132 else if (off >= (long)(&dummy->fpu >> 2) &&
133 off < (long)(&dummy->u_fpvalid >> 2)) {
134 if (!tsk_used_math(tsk)) {
135 if (off == (long)(&dummy->fpu.fpscr >> 2))
140 tmp = ((long *)(&tsk->thread.fpu >> 2))
141 [off - (long)&dummy->fpu];
142 } else if (off == (long)(&dummy->u_fpvalid >> 2))
143 tmp = !!tsk_used_math(tsk);
144 #endif /* not NO_FPU */
149 return put_user(tmp, data);
152 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
157 struct user * dummy = NULL;
160 if ((off & 3) || off < 0 ||
161 off > sizeof(struct user) - 3)
169 /* We don't allow to modify evb. */
174 /* We allow to modify only cbr in psw */
176 psw = get_stack_long(tsk, PT_PSW);
177 psw = (psw & ~0x100) | ((data & 1) << 8);
178 ret = put_stack_long(tsk, PT_PSW, psw);
186 if (off < (sizeof(struct pt_regs) >> 2))
187 ret = put_stack_long(tsk, off, data);
189 else if (off >= (long)(&dummy->fpu >> 2) &&
190 off < (long)(&dummy->u_fpvalid >> 2)) {
191 set_stopped_child_used_math(tsk);
192 ((long *)&tsk->thread.fpu)
193 [off - (long)&dummy->fpu] = data;
195 } else if (off == (long)(&dummy->u_fpvalid >> 2)) {
196 conditional_stopped_child_used_math(data, tsk);
199 #endif /* not NO_FPU */
207 * Get all user integer registers.
209 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
211 struct pt_regs *regs = get_user_regs(tsk);
213 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
217 * Set all user integer registers.
219 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
221 struct pt_regs newregs;
225 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
226 struct pt_regs *regs = get_user_regs(tsk);
236 check_condition_bit(struct task_struct *child)
238 return (int)((get_stack_long(child, PT_PSW) >> 8) & 1);
242 check_condition_src(unsigned long op, unsigned long regno1,
243 unsigned long regno2, struct task_struct *child)
245 unsigned long reg1, reg2;
247 reg2 = get_stack_long(child, reg_offset[regno2]);
251 reg1 = get_stack_long(child, reg_offset[regno1]);
254 reg1 = get_stack_long(child, reg_offset[regno1]);
261 return (int)reg2 < 0;
263 return (int)reg2 >= 0;
265 return (int)reg2 <= 0;
267 return (int)reg2 > 0;
275 compute_next_pc_for_16bit_insn(unsigned long insn, unsigned long pc,
276 unsigned long *next_pc,
277 struct task_struct *child)
279 unsigned long op, op2, op3;
284 if (insn & 0x00008000)
287 insn &= 0x7fff; /* right slot */
289 insn >>= 16; /* left slot */
291 op = (insn >> 12) & 0xf;
292 op2 = (insn >> 8) & 0xf;
293 op3 = (insn >> 4) & 0xf;
299 if (!check_condition_bit(child)) {
300 disp = (long)(insn << 24) >> 22;
301 *next_pc = (pc & ~0x3) + disp;
307 if (check_condition_bit(child)) {
308 disp = (long)(insn << 24) >> 22;
309 *next_pc = (pc & ~0x3) + disp;
315 disp = (long)(insn << 24) >> 22;
316 *next_pc = (pc & ~0x3) + disp;
320 } else if (op == 0x1) {
323 if (op3 == 0xf) { /* TRAP */
327 /* kernel space is not allowed as next_pc */
329 unsigned long trapno;
331 __asm__ __volatile__ (
336 *next_pc = evb + (trapno << 2);
339 } else if (op3 == 0xd) { /* RTE */
340 *next_pc = get_stack_long(child, PT_BPC);
345 if (op3 == 0xc && check_condition_bit(child)) {
347 *next_pc = get_stack_long(child,
353 if (op3 == 0xc && !check_condition_bit(child)) {
355 *next_pc = get_stack_long(child,
362 if (op3 == 0xc) { /* JMP */
364 *next_pc = get_stack_long(child,
378 compute_next_pc_for_32bit_insn(unsigned long insn, unsigned long pc,
379 unsigned long *next_pc,
380 struct task_struct *child)
385 unsigned long regno1, regno2;
387 op = (insn >> 28) & 0xf;
388 if (op == 0xf) { /* branch 24-bit relative */
389 op2 = (insn >> 24) & 0xf;
393 if (!check_condition_bit(child)) {
394 disp = (long)(insn << 8) >> 6;
395 *next_pc = (pc & ~0x3) + disp;
401 if (check_condition_bit(child)) {
402 disp = (long)(insn << 8) >> 6;
403 *next_pc = (pc & ~0x3) + disp;
409 disp = (long)(insn << 8) >> 6;
410 *next_pc = (pc & ~0x3) + disp;
413 } else if (op == 0xb) { /* branch 16-bit relative */
414 op2 = (insn >> 20) & 0xf;
424 regno1 = ((insn >> 24) & 0xf);
425 regno2 = ((insn >> 16) & 0xf);
426 if (check_condition_src(op2, regno1, regno2, child)) {
427 disp = (long)(insn << 16) >> 14;
428 *next_pc = (pc & ~0x3) + disp;
438 compute_next_pc(unsigned long insn, unsigned long pc,
439 unsigned long *next_pc, struct task_struct *child)
441 if (insn & 0x80000000)
442 compute_next_pc_for_32bit_insn(insn, pc, next_pc, child);
444 compute_next_pc_for_16bit_insn(insn, pc, next_pc, child);
448 register_debug_trap(struct task_struct *child, unsigned long next_pc,
449 unsigned long next_insn, unsigned long *code)
451 struct debug_trap *p = &child->thread.debug_trap;
452 unsigned long addr = next_pc & ~3;
454 if (p->nr_trap == MAX_TRAPS) {
455 printk("kernel BUG at %s %d: p->nr_trap = %d\n",
456 __FILE__, __LINE__, p->nr_trap);
459 p->addr[p->nr_trap] = addr;
460 p->insn[p->nr_trap] = next_insn;
463 *code = (next_insn & 0xffff0000) | 0x10f1;
466 if ((next_insn & 0x80000000) || (next_insn & 0x8000)) {
470 *code = (next_insn & 0xffff) | 0x10f10000;
478 unregister_debug_trap(struct task_struct *child, unsigned long addr,
481 struct debug_trap *p = &child->thread.debug_trap;
484 /* Search debug trap entry. */
485 for (i = 0; i < p->nr_trap; i++) {
486 if (p->addr[i] == addr)
489 if (i >= p->nr_trap) {
490 /* The trap may be requested from debugger.
491 * ptrace should do nothing in this case.
496 /* Recover orignal instruction code. */
499 /* Shift debug trap entries. */
500 while (i < p->nr_trap - 1) {
501 p->insn[i] = p->insn[i + 1];
502 p->addr[i] = p->addr[i + 1];
510 unregister_all_debug_traps(struct task_struct *child)
512 struct debug_trap *p = &child->thread.debug_trap;
515 for (i = 0; i < p->nr_trap; i++)
516 access_process_vm(child, p->addr[i], &p->insn[i], sizeof(p->insn[i]), 1);
521 invalidate_cache(void)
523 #if defined(CONFIG_CHIP_M32700) || defined(CONFIG_CHIP_OPSP)
525 _flush_cache_copyback_all();
527 #else /* ! CONFIG_CHIP_M32700 */
529 /* Invalidate cache */
530 __asm__ __volatile__ (
533 "stb r1, @r0 ; cache off \n\t"
537 "stb r1, @r0 ; cache invalidate \n\t"
540 "ldb r1, @r0 ; invalidate check \n\t"
545 "stb r1, @r0 ; cache on \n\t"
546 : : : "r0", "r1", "memory"
548 /* FIXME: copying-back d-cache and invalidating i-cache are needed.
550 #endif /* CONFIG_CHIP_M32700 */
553 /* Embed a debug trap (TRAP1) code */
555 embed_debug_trap(struct task_struct *child, unsigned long next_pc)
557 unsigned long next_insn, code;
558 unsigned long addr = next_pc & ~3;
560 if (access_process_vm(child, addr, &next_insn, sizeof(next_insn), 0)
561 != sizeof(next_insn)) {
562 return -1; /* error */
565 /* Set a trap code. */
566 if (register_debug_trap(child, next_pc, next_insn, &code)) {
567 return -1; /* error */
569 if (access_process_vm(child, addr, &code, sizeof(code), 1)
571 return -1; /* error */
573 return 0; /* success */
577 withdraw_debug_trap(struct pt_regs *regs)
582 addr = (regs->bpc - 2) & ~3;
584 if (unregister_debug_trap(current, addr, &code)) {
585 access_process_vm(current, addr, &code, sizeof(code), 1);
591 init_debug_traps(struct task_struct *child)
593 struct debug_trap *p = &child->thread.debug_trap;
596 for (i = 0; i < MAX_TRAPS; i++) {
604 * Called by kernel/ptrace.c when detaching..
606 * Make sure single step bits etc are not set.
608 void ptrace_disable(struct task_struct *child)
610 /* nothing to do.. */
614 do_ptrace(long request, struct task_struct *child, long addr, long data)
621 * read word at location "addr" in the child process.
623 case PTRACE_PEEKTEXT:
624 case PTRACE_PEEKDATA:
625 ret = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
626 if (ret == sizeof(tmp))
627 ret = put_user(tmp,(unsigned long __user *) data);
633 * read the word at location addr in the USER area.
636 ret = ptrace_read_user(child, addr,
637 (unsigned long __user *)data);
641 * write the word at location addr.
643 case PTRACE_POKETEXT:
644 case PTRACE_POKEDATA:
645 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
646 if (ret == sizeof(data)) {
648 if (request == PTRACE_POKETEXT) {
657 * write the word at location addr in the USER area.
660 ret = ptrace_write_user(child, addr, data);
664 * continue/restart and stop at next (return from) syscall
669 if (!valid_signal(data))
671 if (request == PTRACE_SYSCALL)
672 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
674 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
675 child->exit_code = data;
676 wake_up_process(child);
681 * make the child exit. Best I can do is send it a sigkill.
682 * perhaps it should be put in the status that it wants to
687 unregister_all_debug_traps(child);
689 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
691 child->exit_code = SIGKILL;
692 wake_up_process(child);
697 * execute single instruction.
699 case PTRACE_SINGLESTEP: {
700 unsigned long next_pc;
701 unsigned long pc, insn;
704 if (!valid_signal(data))
706 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
707 if ((child->ptrace & PT_DTRACE) == 0) {
708 /* Spurious delayed TF traps may occur */
709 child->ptrace |= PT_DTRACE;
712 /* Compute next pc. */
713 pc = get_stack_long(child, PT_BPC);
715 if (access_process_vm(child, pc&~3, &insn, sizeof(insn), 0)
719 compute_next_pc(insn, pc, &next_pc, child);
720 if (next_pc & 0x80000000)
723 if (embed_debug_trap(child, next_pc))
727 child->exit_code = data;
729 /* give it a chance to run. */
730 wake_up_process(child);
736 * detach a process that was attached.
740 ret = ptrace_detach(child, data);
744 ret = ptrace_getregs(child, (void __user *)data);
748 ret = ptrace_setregs(child, (void __user *)data);
752 ret = ptrace_request(child, request, addr, data);
759 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
761 struct task_struct *child;
765 if (request == PTRACE_TRACEME) {
766 ret = ptrace_traceme();
770 child = ptrace_get_task_struct(pid);
772 ret = PTR_ERR(child);
776 if (request == PTRACE_ATTACH) {
777 ret = ptrace_attach(child);
779 init_debug_traps(child);
783 ret = ptrace_check_attach(child, request == PTRACE_KILL);
785 ret = do_ptrace(request, child, addr, data);
788 put_task_struct(child);
795 /* notification of system call entry/exit
796 * - triggered by current->work.syscall_trace
798 void do_syscall_trace(void)
800 if (!test_thread_flag(TIF_SYSCALL_TRACE))
802 if (!(current->ptrace & PT_PTRACED))
804 /* the 0x80 provides a way for the tracing parent to distinguish
805 between a syscall stop and SIGTRAP delivery */
806 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
810 * this isn't the same as continuing with a signal, but it will do
811 * for normal use. strace only continues with a signal if the
812 * stopping signal is not SIGTRAP. -brl
814 if (current->exit_code) {
815 send_sig(current->exit_code, current, 1);
816 current->exit_code = 0;