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/kernel.h>
18 #include <linux/sched.h>
20 #include <linux/err.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 * This routine will get a word off of the process kernel stack.
40 static inline unsigned long int
41 get_stack_long(struct task_struct *task, int offset)
45 stack = (unsigned long *)task_pt_regs(task);
51 * This routine will put a word on the process kernel stack.
54 put_stack_long(struct task_struct *task, int offset, unsigned long data)
58 stack = (unsigned long *)task_pt_regs(task);
64 static int reg_offset[] = {
65 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
66 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_FP, PT_LR, PT_SPU,
70 * Read the word at offset "off" into the "struct user". We
71 * actually access the pt_regs stored on the kernel stack.
73 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
74 unsigned long __user *data)
78 struct user * dummy = NULL;
81 if ((off & 3) || (off < 0) || (off > sizeof(struct user) - 3))
87 __asm__ __volatile__ (
94 psw = get_stack_long(tsk, PT_PSW);
95 tmp = ((psw >> 8) & 1);
99 unsigned long psw, bbpsw;
100 psw = get_stack_long(tsk, PT_PSW);
101 bbpsw = get_stack_long(tsk, PT_BBPSW);
102 tmp = ((psw >> 8) & 0xff) | ((bbpsw & 0xff) << 8);
106 tmp = get_stack_long(tsk, PT_BPC);
112 if (off < (sizeof(struct pt_regs) >> 2))
113 tmp = get_stack_long(tsk, off);
115 else if (off >= (long)(&dummy->fpu >> 2) &&
116 off < (long)(&dummy->u_fpvalid >> 2)) {
117 if (!tsk_used_math(tsk)) {
118 if (off == (long)(&dummy->fpu.fpscr >> 2))
123 tmp = ((long *)(&tsk->thread.fpu >> 2))
124 [off - (long)&dummy->fpu];
125 } else if (off == (long)(&dummy->u_fpvalid >> 2))
126 tmp = !!tsk_used_math(tsk);
127 #endif /* not NO_FPU */
132 return put_user(tmp, data);
135 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
140 struct user * dummy = NULL;
143 if ((off & 3) || off < 0 ||
144 off > sizeof(struct user) - 3)
152 /* We don't allow to modify evb. */
157 /* We allow to modify only cbr in psw */
159 psw = get_stack_long(tsk, PT_PSW);
160 psw = (psw & ~0x100) | ((data & 1) << 8);
161 ret = put_stack_long(tsk, PT_PSW, psw);
169 if (off < (sizeof(struct pt_regs) >> 2))
170 ret = put_stack_long(tsk, off, data);
172 else if (off >= (long)(&dummy->fpu >> 2) &&
173 off < (long)(&dummy->u_fpvalid >> 2)) {
174 set_stopped_child_used_math(tsk);
175 ((long *)&tsk->thread.fpu)
176 [off - (long)&dummy->fpu] = data;
178 } else if (off == (long)(&dummy->u_fpvalid >> 2)) {
179 conditional_stopped_child_used_math(data, tsk);
182 #endif /* not NO_FPU */
190 * Get all user integer registers.
192 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
194 struct pt_regs *regs = task_pt_regs(tsk);
196 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
200 * Set all user integer registers.
202 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
204 struct pt_regs newregs;
208 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
209 struct pt_regs *regs = task_pt_regs(tsk);
219 check_condition_bit(struct task_struct *child)
221 return (int)((get_stack_long(child, PT_PSW) >> 8) & 1);
225 check_condition_src(unsigned long op, unsigned long regno1,
226 unsigned long regno2, struct task_struct *child)
228 unsigned long reg1, reg2;
230 reg2 = get_stack_long(child, reg_offset[regno2]);
234 reg1 = get_stack_long(child, reg_offset[regno1]);
237 reg1 = get_stack_long(child, reg_offset[regno1]);
244 return (int)reg2 < 0;
246 return (int)reg2 >= 0;
248 return (int)reg2 <= 0;
250 return (int)reg2 > 0;
258 compute_next_pc_for_16bit_insn(unsigned long insn, unsigned long pc,
259 unsigned long *next_pc,
260 struct task_struct *child)
262 unsigned long op, op2, op3;
267 if (insn & 0x00008000)
270 insn &= 0x7fff; /* right slot */
272 insn >>= 16; /* left slot */
274 op = (insn >> 12) & 0xf;
275 op2 = (insn >> 8) & 0xf;
276 op3 = (insn >> 4) & 0xf;
282 if (!check_condition_bit(child)) {
283 disp = (long)(insn << 24) >> 22;
284 *next_pc = (pc & ~0x3) + disp;
290 if (check_condition_bit(child)) {
291 disp = (long)(insn << 24) >> 22;
292 *next_pc = (pc & ~0x3) + disp;
298 disp = (long)(insn << 24) >> 22;
299 *next_pc = (pc & ~0x3) + disp;
303 } else if (op == 0x1) {
306 if (op3 == 0xf) { /* TRAP */
310 /* kernel space is not allowed as next_pc */
312 unsigned long trapno;
314 __asm__ __volatile__ (
319 *next_pc = evb + (trapno << 2);
322 } else if (op3 == 0xd) { /* RTE */
323 *next_pc = get_stack_long(child, PT_BPC);
328 if (op3 == 0xc && check_condition_bit(child)) {
330 *next_pc = get_stack_long(child,
336 if (op3 == 0xc && !check_condition_bit(child)) {
338 *next_pc = get_stack_long(child,
345 if (op3 == 0xc) { /* JMP */
347 *next_pc = get_stack_long(child,
361 compute_next_pc_for_32bit_insn(unsigned long insn, unsigned long pc,
362 unsigned long *next_pc,
363 struct task_struct *child)
368 unsigned long regno1, regno2;
370 op = (insn >> 28) & 0xf;
371 if (op == 0xf) { /* branch 24-bit relative */
372 op2 = (insn >> 24) & 0xf;
376 if (!check_condition_bit(child)) {
377 disp = (long)(insn << 8) >> 6;
378 *next_pc = (pc & ~0x3) + disp;
384 if (check_condition_bit(child)) {
385 disp = (long)(insn << 8) >> 6;
386 *next_pc = (pc & ~0x3) + disp;
392 disp = (long)(insn << 8) >> 6;
393 *next_pc = (pc & ~0x3) + disp;
396 } else if (op == 0xb) { /* branch 16-bit relative */
397 op2 = (insn >> 20) & 0xf;
407 regno1 = ((insn >> 24) & 0xf);
408 regno2 = ((insn >> 16) & 0xf);
409 if (check_condition_src(op2, regno1, regno2, child)) {
410 disp = (long)(insn << 16) >> 14;
411 *next_pc = (pc & ~0x3) + disp;
421 compute_next_pc(unsigned long insn, unsigned long pc,
422 unsigned long *next_pc, struct task_struct *child)
424 if (insn & 0x80000000)
425 compute_next_pc_for_32bit_insn(insn, pc, next_pc, child);
427 compute_next_pc_for_16bit_insn(insn, pc, next_pc, child);
431 register_debug_trap(struct task_struct *child, unsigned long next_pc,
432 unsigned long next_insn, unsigned long *code)
434 struct debug_trap *p = &child->thread.debug_trap;
435 unsigned long addr = next_pc & ~3;
437 if (p->nr_trap == MAX_TRAPS) {
438 printk("kernel BUG at %s %d: p->nr_trap = %d\n",
439 __FILE__, __LINE__, p->nr_trap);
442 p->addr[p->nr_trap] = addr;
443 p->insn[p->nr_trap] = next_insn;
446 *code = (next_insn & 0xffff0000) | 0x10f1;
449 if ((next_insn & 0x80000000) || (next_insn & 0x8000)) {
453 *code = (next_insn & 0xffff) | 0x10f10000;
461 unregister_debug_trap(struct task_struct *child, unsigned long addr,
464 struct debug_trap *p = &child->thread.debug_trap;
467 /* Search debug trap entry. */
468 for (i = 0; i < p->nr_trap; i++) {
469 if (p->addr[i] == addr)
472 if (i >= p->nr_trap) {
473 /* The trap may be requested from debugger.
474 * ptrace should do nothing in this case.
479 /* Recover original instruction code. */
482 /* Shift debug trap entries. */
483 while (i < p->nr_trap - 1) {
484 p->insn[i] = p->insn[i + 1];
485 p->addr[i] = p->addr[i + 1];
493 unregister_all_debug_traps(struct task_struct *child)
495 struct debug_trap *p = &child->thread.debug_trap;
498 for (i = 0; i < p->nr_trap; i++)
499 access_process_vm(child, p->addr[i], &p->insn[i], sizeof(p->insn[i]), 1);
504 invalidate_cache(void)
506 #if defined(CONFIG_CHIP_M32700) || defined(CONFIG_CHIP_OPSP)
508 _flush_cache_copyback_all();
510 #else /* ! CONFIG_CHIP_M32700 */
512 /* Invalidate cache */
513 __asm__ __volatile__ (
516 "stb r1, @r0 ; cache off \n\t"
520 "stb r1, @r0 ; cache invalidate \n\t"
523 "ldb r1, @r0 ; invalidate check \n\t"
528 "stb r1, @r0 ; cache on \n\t"
529 : : : "r0", "r1", "memory"
531 /* FIXME: copying-back d-cache and invalidating i-cache are needed.
533 #endif /* CONFIG_CHIP_M32700 */
536 /* Embed a debug trap (TRAP1) code */
538 embed_debug_trap(struct task_struct *child, unsigned long next_pc)
540 unsigned long next_insn, code;
541 unsigned long addr = next_pc & ~3;
543 if (access_process_vm(child, addr, &next_insn, sizeof(next_insn), 0)
544 != sizeof(next_insn)) {
545 return -1; /* error */
548 /* Set a trap code. */
549 if (register_debug_trap(child, next_pc, next_insn, &code)) {
550 return -1; /* error */
552 if (access_process_vm(child, addr, &code, sizeof(code), 1)
554 return -1; /* error */
556 return 0; /* success */
560 withdraw_debug_trap(struct pt_regs *regs)
565 addr = (regs->bpc - 2) & ~3;
567 if (unregister_debug_trap(current, addr, &code)) {
568 access_process_vm(current, addr, &code, sizeof(code), 1);
574 init_debug_traps(struct task_struct *child)
576 struct debug_trap *p = &child->thread.debug_trap;
579 for (i = 0; i < MAX_TRAPS; i++) {
587 * Called by kernel/ptrace.c when detaching..
589 * Make sure single step bits etc are not set.
591 void ptrace_disable(struct task_struct *child)
593 /* nothing to do.. */
597 arch_ptrace(struct task_struct *child, long request, long addr, long data)
603 * read word at location "addr" in the child process.
605 case PTRACE_PEEKTEXT:
606 case PTRACE_PEEKDATA:
607 ret = generic_ptrace_peekdata(child, addr, data);
611 * read the word at location addr in the USER area.
614 ret = ptrace_read_user(child, addr,
615 (unsigned long __user *)data);
619 * write the word at location addr.
621 case PTRACE_POKETEXT:
622 case PTRACE_POKEDATA:
623 ret = generic_ptrace_pokedata(child, addr, data);
624 if (ret == 0 && request == PTRACE_POKETEXT)
629 * write the word at location addr in the USER area.
632 ret = ptrace_write_user(child, addr, data);
636 * continue/restart and stop at next (return from) syscall
641 if (!valid_signal(data))
643 if (request == PTRACE_SYSCALL)
644 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
646 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
647 child->exit_code = data;
648 wake_up_process(child);
653 * make the child exit. Best I can do is send it a sigkill.
654 * perhaps it should be put in the status that it wants to
659 unregister_all_debug_traps(child);
661 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
663 child->exit_code = SIGKILL;
664 wake_up_process(child);
669 * execute single instruction.
671 case PTRACE_SINGLESTEP: {
672 unsigned long next_pc;
673 unsigned long pc, insn;
676 if (!valid_signal(data))
678 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
679 if ((child->ptrace & PT_DTRACE) == 0) {
680 /* Spurious delayed TF traps may occur */
681 child->ptrace |= PT_DTRACE;
684 /* Compute next pc. */
685 pc = get_stack_long(child, PT_BPC);
687 if (access_process_vm(child, pc&~3, &insn, sizeof(insn), 0)
691 compute_next_pc(insn, pc, &next_pc, child);
692 if (next_pc & 0x80000000)
695 if (embed_debug_trap(child, next_pc))
699 child->exit_code = data;
701 /* give it a chance to run. */
702 wake_up_process(child);
708 ret = ptrace_getregs(child, (void __user *)data);
712 ret = ptrace_setregs(child, (void __user *)data);
716 ret = ptrace_request(child, request, addr, data);
723 /* notification of system call entry/exit
724 * - triggered by current->work.syscall_trace
726 void do_syscall_trace(void)
728 if (!test_thread_flag(TIF_SYSCALL_TRACE))
730 if (!(current->ptrace & PT_PTRACED))
732 /* the 0x80 provides a way for the tracing parent to distinguish
733 between a syscall stop and SIGTRAP delivery */
734 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
738 * this isn't the same as continuing with a signal, but it will do
739 * for normal use. strace only continues with a signal if the
740 * stopping signal is not SIGTRAP. -brl
742 if (current->exit_code) {
743 send_sig(current->exit_code, current, 1);
744 current->exit_code = 0;