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>
28 #include <asm/cacheflush.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/processor.h>
34 #include <asm/mmu_context.h>
37 * Get the address of the live pt_regs for the specified task.
38 * These are saved onto the top kernel stack when the process
41 * Note: if a user thread is execve'd from kernel space, the
42 * kernel stack will not be empty on entry to the kernel, so
43 * ptracing these tasks will fail.
45 static inline struct pt_regs
*
46 get_user_regs(struct task_struct
*task
)
48 return (struct pt_regs
*)
49 ((unsigned long)task
->thread_info
+ THREAD_SIZE
50 - sizeof(struct pt_regs
));
54 * This routine will get a word off of the process kernel stack.
56 static inline unsigned long int
57 get_stack_long(struct task_struct
*task
, int offset
)
61 stack
= (unsigned long *)get_user_regs(task
);
67 * This routine will put a word on the process kernel stack.
70 put_stack_long(struct task_struct
*task
, int offset
, unsigned long data
)
74 stack
= (unsigned long *)get_user_regs(task
);
80 static int reg_offset
[] = {
81 PT_R0
, PT_R1
, PT_R2
, PT_R3
, PT_R4
, PT_R5
, PT_R6
, PT_R7
,
82 PT_R8
, PT_R9
, PT_R10
, PT_R11
, PT_R12
, PT_FP
, PT_LR
, PT_SPU
,
86 * Read the word at offset "off" into the "struct user". We
87 * actually access the pt_regs stored on the kernel stack.
89 static int ptrace_read_user(struct task_struct
*tsk
, unsigned long off
,
90 unsigned long __user
*data
)
94 struct user
* dummy
= NULL
;
97 if ((off
& 3) || (off
< 0) || (off
> sizeof(struct user
) - 3))
103 __asm__
__volatile__ (
110 psw
= get_stack_long(tsk
, PT_PSW
);
111 tmp
= ((psw
>> 8) & 1);
115 unsigned long psw
, bbpsw
;
116 psw
= get_stack_long(tsk
, PT_PSW
);
117 bbpsw
= get_stack_long(tsk
, PT_BBPSW
);
118 tmp
= ((psw
>> 8) & 0xff) | ((bbpsw
& 0xff) << 8);
122 tmp
= get_stack_long(tsk
, PT_BPC
);
128 if (off
< (sizeof(struct pt_regs
) >> 2))
129 tmp
= get_stack_long(tsk
, off
);
131 else if (off
>= (long)(&dummy
->fpu
>> 2) &&
132 off
< (long)(&dummy
->u_fpvalid
>> 2)) {
133 if (!tsk_used_math(tsk
)) {
134 if (off
== (long)(&dummy
->fpu
.fpscr
>> 2))
139 tmp
= ((long *)(&tsk
->thread
.fpu
>> 2))
140 [off
- (long)&dummy
->fpu
];
141 } else if (off
== (long)(&dummy
->u_fpvalid
>> 2))
142 tmp
= !!tsk_used_math(tsk
);
143 #endif /* not NO_FPU */
148 return put_user(tmp
, data
);
151 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long off
,
156 struct user
* dummy
= NULL
;
159 if ((off
& 3) || off
< 0 ||
160 off
> sizeof(struct user
) - 3)
168 /* We don't allow to modify evb. */
173 /* We allow to modify only cbr in psw */
175 psw
= get_stack_long(tsk
, PT_PSW
);
176 psw
= (psw
& ~0x100) | ((data
& 1) << 8);
177 ret
= put_stack_long(tsk
, PT_PSW
, psw
);
185 if (off
< (sizeof(struct pt_regs
) >> 2))
186 ret
= put_stack_long(tsk
, off
, data
);
188 else if (off
>= (long)(&dummy
->fpu
>> 2) &&
189 off
< (long)(&dummy
->u_fpvalid
>> 2)) {
190 set_stopped_child_used_math(tsk
);
191 ((long *)&tsk
->thread
.fpu
)
192 [off
- (long)&dummy
->fpu
] = data
;
194 } else if (off
== (long)(&dummy
->u_fpvalid
>> 2)) {
195 conditional_stopped_child_used_math(data
, tsk
);
198 #endif /* not NO_FPU */
206 * Get all user integer registers.
208 static int ptrace_getregs(struct task_struct
*tsk
, void __user
*uregs
)
210 struct pt_regs
*regs
= get_user_regs(tsk
);
212 return copy_to_user(uregs
, regs
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
216 * Set all user integer registers.
218 static int ptrace_setregs(struct task_struct
*tsk
, void __user
*uregs
)
220 struct pt_regs newregs
;
224 if (copy_from_user(&newregs
, uregs
, sizeof(struct pt_regs
)) == 0) {
225 struct pt_regs
*regs
= get_user_regs(tsk
);
235 check_condition_bit(struct task_struct
*child
)
237 return (int)((get_stack_long(child
, PT_PSW
) >> 8) & 1);
241 check_condition_src(unsigned long op
, unsigned long regno1
,
242 unsigned long regno2
, struct task_struct
*child
)
244 unsigned long reg1
, reg2
;
246 reg2
= get_stack_long(child
, reg_offset
[regno2
]);
250 reg1
= get_stack_long(child
, reg_offset
[regno1
]);
253 reg1
= get_stack_long(child
, reg_offset
[regno1
]);
260 return (int)reg2
< 0;
262 return (int)reg2
>= 0;
264 return (int)reg2
<= 0;
266 return (int)reg2
> 0;
274 compute_next_pc_for_16bit_insn(unsigned long insn
, unsigned long pc
,
275 unsigned long *next_pc
,
276 struct task_struct
*child
)
278 unsigned long op
, op2
, op3
;
283 if (insn
& 0x00008000)
286 insn
&= 0x7fff; /* right slot */
288 insn
>>= 16; /* left slot */
290 op
= (insn
>> 12) & 0xf;
291 op2
= (insn
>> 8) & 0xf;
292 op3
= (insn
>> 4) & 0xf;
298 if (!check_condition_bit(child
)) {
299 disp
= (long)(insn
<< 24) >> 22;
300 *next_pc
= (pc
& ~0x3) + disp
;
306 if (check_condition_bit(child
)) {
307 disp
= (long)(insn
<< 24) >> 22;
308 *next_pc
= (pc
& ~0x3) + disp
;
314 disp
= (long)(insn
<< 24) >> 22;
315 *next_pc
= (pc
& ~0x3) + disp
;
319 } else if (op
== 0x1) {
322 if (op3
== 0xf) { /* TRAP */
326 /* kernel space is not allowed as next_pc */
328 unsigned long trapno
;
330 __asm__
__volatile__ (
335 *next_pc
= evb
+ (trapno
<< 2);
338 } else if (op3
== 0xd) { /* RTE */
339 *next_pc
= get_stack_long(child
, PT_BPC
);
344 if (op3
== 0xc && check_condition_bit(child
)) {
346 *next_pc
= get_stack_long(child
,
352 if (op3
== 0xc && !check_condition_bit(child
)) {
354 *next_pc
= get_stack_long(child
,
361 if (op3
== 0xc) { /* JMP */
363 *next_pc
= get_stack_long(child
,
377 compute_next_pc_for_32bit_insn(unsigned long insn
, unsigned long pc
,
378 unsigned long *next_pc
,
379 struct task_struct
*child
)
384 unsigned long regno1
, regno2
;
386 op
= (insn
>> 28) & 0xf;
387 if (op
== 0xf) { /* branch 24-bit relative */
388 op2
= (insn
>> 24) & 0xf;
392 if (!check_condition_bit(child
)) {
393 disp
= (long)(insn
<< 8) >> 6;
394 *next_pc
= (pc
& ~0x3) + disp
;
400 if (check_condition_bit(child
)) {
401 disp
= (long)(insn
<< 8) >> 6;
402 *next_pc
= (pc
& ~0x3) + disp
;
408 disp
= (long)(insn
<< 8) >> 6;
409 *next_pc
= (pc
& ~0x3) + disp
;
412 } else if (op
== 0xb) { /* branch 16-bit relative */
413 op2
= (insn
>> 20) & 0xf;
423 regno1
= ((insn
>> 24) & 0xf);
424 regno2
= ((insn
>> 16) & 0xf);
425 if (check_condition_src(op2
, regno1
, regno2
, child
)) {
426 disp
= (long)(insn
<< 16) >> 14;
427 *next_pc
= (pc
& ~0x3) + disp
;
437 compute_next_pc(unsigned long insn
, unsigned long pc
,
438 unsigned long *next_pc
, struct task_struct
*child
)
440 if (insn
& 0x80000000)
441 compute_next_pc_for_32bit_insn(insn
, pc
, next_pc
, child
);
443 compute_next_pc_for_16bit_insn(insn
, pc
, next_pc
, child
);
447 register_debug_trap(struct task_struct
*child
, unsigned long next_pc
,
448 unsigned long next_insn
, unsigned long *code
)
450 struct debug_trap
*p
= &child
->thread
.debug_trap
;
451 unsigned long addr
= next_pc
& ~3;
453 if (p
->nr_trap
== MAX_TRAPS
) {
454 printk("kernel BUG at %s %d: p->nr_trap = %d\n",
455 __FILE__
, __LINE__
, p
->nr_trap
);
458 p
->addr
[p
->nr_trap
] = addr
;
459 p
->insn
[p
->nr_trap
] = next_insn
;
462 *code
= (next_insn
& 0xffff0000) | 0x10f1;
465 if ((next_insn
& 0x80000000) || (next_insn
& 0x8000)) {
469 *code
= (next_insn
& 0xffff) | 0x10f10000;
477 unregister_debug_trap(struct task_struct
*child
, unsigned long addr
,
480 struct debug_trap
*p
= &child
->thread
.debug_trap
;
483 /* Search debug trap entry. */
484 for (i
= 0; i
< p
->nr_trap
; i
++) {
485 if (p
->addr
[i
] == addr
)
488 if (i
>= p
->nr_trap
) {
489 /* The trap may be requested from debugger.
490 * ptrace should do nothing in this case.
495 /* Recover orignal instruction code. */
498 /* Shift debug trap entries. */
499 while (i
< p
->nr_trap
- 1) {
500 p
->insn
[i
] = p
->insn
[i
+ 1];
501 p
->addr
[i
] = p
->addr
[i
+ 1];
509 unregister_all_debug_traps(struct task_struct
*child
)
511 struct debug_trap
*p
= &child
->thread
.debug_trap
;
514 for (i
= 0; i
< p
->nr_trap
; i
++)
515 access_process_vm(child
, p
->addr
[i
], &p
->insn
[i
], sizeof(p
->insn
[i
]), 1);
520 invalidate_cache(void)
522 #if defined(CONFIG_CHIP_M32700) || defined(CONFIG_CHIP_OPSP)
524 _flush_cache_copyback_all();
526 #else /* ! CONFIG_CHIP_M32700 */
528 /* Invalidate cache */
529 __asm__
__volatile__ (
532 "stb r1, @r0 ; cache off \n\t"
536 "stb r1, @r0 ; cache invalidate \n\t"
539 "ldb r1, @r0 ; invalidate check \n\t"
544 "stb r1, @r0 ; cache on \n\t"
545 : : : "r0", "r1", "memory"
547 /* FIXME: copying-back d-cache and invalidating i-cache are needed.
549 #endif /* CONFIG_CHIP_M32700 */
552 /* Embed a debug trap (TRAP1) code */
554 embed_debug_trap(struct task_struct
*child
, unsigned long next_pc
)
556 unsigned long next_insn
, code
;
557 unsigned long addr
= next_pc
& ~3;
559 if (access_process_vm(child
, addr
, &next_insn
, sizeof(next_insn
), 0)
560 != sizeof(next_insn
)) {
561 return -1; /* error */
564 /* Set a trap code. */
565 if (register_debug_trap(child
, next_pc
, next_insn
, &code
)) {
566 return -1; /* error */
568 if (access_process_vm(child
, addr
, &code
, sizeof(code
), 1)
570 return -1; /* error */
572 return 0; /* success */
576 withdraw_debug_trap(struct pt_regs
*regs
)
581 addr
= (regs
->bpc
- 2) & ~3;
583 if (unregister_debug_trap(current
, addr
, &code
)) {
584 access_process_vm(current
, addr
, &code
, sizeof(code
), 1);
590 init_debug_traps(struct task_struct
*child
)
592 struct debug_trap
*p
= &child
->thread
.debug_trap
;
595 for (i
= 0; i
< MAX_TRAPS
; i
++) {
603 * Called by kernel/ptrace.c when detaching..
605 * Make sure single step bits etc are not set.
607 void ptrace_disable(struct task_struct
*child
)
609 /* nothing to do.. */
613 do_ptrace(long request
, struct task_struct
*child
, long addr
, long data
)
620 * read word at location "addr" in the child process.
622 case PTRACE_PEEKTEXT
:
623 case PTRACE_PEEKDATA
:
624 ret
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
625 if (ret
== sizeof(tmp
))
626 ret
= put_user(tmp
,(unsigned long __user
*) data
);
632 * read the word at location addr in the USER area.
635 ret
= ptrace_read_user(child
, addr
,
636 (unsigned long __user
*)data
);
640 * write the word at location addr.
642 case PTRACE_POKETEXT
:
643 case PTRACE_POKEDATA
:
644 ret
= access_process_vm(child
, addr
, &data
, sizeof(data
), 1);
645 if (ret
== sizeof(data
)) {
647 if (request
== PTRACE_POKETEXT
) {
656 * write the word at location addr in the USER area.
659 ret
= ptrace_write_user(child
, addr
, data
);
663 * continue/restart and stop at next (return from) syscall
668 if ((unsigned long) data
> _NSIG
)
670 if (request
== PTRACE_SYSCALL
)
671 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
673 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
674 child
->exit_code
= data
;
675 wake_up_process(child
);
680 * make the child exit. Best I can do is send it a sigkill.
681 * perhaps it should be put in the status that it wants to
686 unregister_all_debug_traps(child
);
688 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
690 child
->exit_code
= SIGKILL
;
691 wake_up_process(child
);
696 * execute single instruction.
698 case PTRACE_SINGLESTEP
: {
699 unsigned long next_pc
;
700 unsigned long pc
, insn
;
703 if ((unsigned long) data
> _NSIG
)
705 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
706 if ((child
->ptrace
& PT_DTRACE
) == 0) {
707 /* Spurious delayed TF traps may occur */
708 child
->ptrace
|= PT_DTRACE
;
711 /* Compute next pc. */
712 pc
= get_stack_long(child
, PT_BPC
);
714 if (access_process_vm(child
, pc
&~3, &insn
, sizeof(insn
), 0)
718 compute_next_pc(insn
, pc
, &next_pc
, child
);
719 if (next_pc
& 0x80000000)
722 if (embed_debug_trap(child
, next_pc
))
726 child
->exit_code
= data
;
728 /* give it a chance to run. */
729 wake_up_process(child
);
735 * detach a process that was attached.
739 ret
= ptrace_detach(child
, data
);
743 ret
= ptrace_getregs(child
, (void __user
*)data
);
747 ret
= ptrace_setregs(child
, (void __user
*)data
);
751 ret
= ptrace_request(child
, request
, addr
, data
);
758 asmlinkage
int sys_ptrace(long request
, long pid
, long addr
, long data
)
760 struct task_struct
*child
;
765 if (request
== PTRACE_TRACEME
) {
766 /* are we already being traced? */
767 if (current
->ptrace
& PT_PTRACED
)
769 /* set the ptrace bit in the process flags. */
770 current
->ptrace
|= PT_PTRACED
;
775 read_lock(&tasklist_lock
);
776 child
= find_task_by_pid(pid
);
778 get_task_struct(child
);
779 read_unlock(&tasklist_lock
);
784 if (pid
== 1) /* you may not mess with init */
787 if (request
== PTRACE_ATTACH
) {
788 ret
= ptrace_attach(child
);
790 init_debug_traps(child
);
794 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
796 ret
= do_ptrace(request
, child
, addr
, data
);
799 put_task_struct(child
);
806 /* notification of system call entry/exit
807 * - triggered by current->work.syscall_trace
809 void do_syscall_trace(void)
811 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
813 if (!(current
->ptrace
& PT_PTRACED
))
815 /* the 0x80 provides a way for the tracing parent to distinguish
816 between a syscall stop and SIGTRAP delivery */
817 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
821 * this isn't the same as continuing with a signal, but it will do
822 * for normal use. strace only continues with a signal if the
823 * stopping signal is not SIGTRAP. -brl
825 if (current
->exit_code
) {
826 send_sig(current
->exit_code
, current
, 1);
827 current
->exit_code
= 0;
This page took 0.084085 seconds and 5 git commands to generate.