2 * linux/arch/sh/kernel/ptrace.c
4 * Original x86 implementation:
6 * edited by Linus Torvalds
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
9 * Audit support: Yuichi Nakamura <ynakam@hitachisoft.jp>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/slab.h>
19 #include <linux/security.h>
20 #include <linux/signal.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
27 #include <asm/mmu_context.h>
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
35 * This routine will get a word off of the process kernel stack.
37 static inline int get_stack_long(struct task_struct
*task
, int offset
)
41 stack
= (unsigned char *)task_pt_regs(task
);
43 return (*((int *)stack
));
47 * This routine will put a word on the process kernel stack.
49 static inline int put_stack_long(struct task_struct
*task
, int offset
,
54 stack
= (unsigned char *)task_pt_regs(task
);
56 *(unsigned long *) stack
= data
;
60 static void ptrace_disable_singlestep(struct task_struct
*child
)
62 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
65 * Ensure the UBC is not programmed at the next context switch.
67 * Normally this is not needed but there are sequences such as
68 * singlestep, signal delivery, and continue that leave the
69 * ubc_pc non-zero leading to spurious SIGTRAPs.
71 if (child
->thread
.ubc_pc
!= 0) {
73 child
->thread
.ubc_pc
= 0;
78 * Called by kernel/ptrace.c when detaching..
80 * Make sure single step bits etc are not set.
82 void ptrace_disable(struct task_struct
*child
)
84 ptrace_disable_singlestep(child
);
87 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
89 struct user
* dummy
= NULL
;
93 /* when I and D space are separate, these will need to be fixed. */
94 case PTRACE_PEEKTEXT
: /* read word at location addr. */
96 ret
= generic_ptrace_peekdata(child
, addr
, data
);
99 /* read the word at location addr in the USER area. */
100 case PTRACE_PEEKUSR
: {
104 if ((addr
& 3) || addr
< 0 ||
105 addr
> sizeof(struct user
) - 3)
108 if (addr
< sizeof(struct pt_regs
))
109 tmp
= get_stack_long(child
, addr
);
110 else if (addr
>= (long) &dummy
->fpu
&&
111 addr
< (long) &dummy
->u_fpvalid
) {
112 if (!tsk_used_math(child
)) {
113 if (addr
== (long)&dummy
->fpu
.fpscr
)
118 tmp
= ((long *)&child
->thread
.fpu
)
119 [(addr
- (long)&dummy
->fpu
) >> 2];
120 } else if (addr
== (long) &dummy
->u_fpvalid
)
121 tmp
= !!tsk_used_math(child
);
124 ret
= put_user(tmp
, (unsigned long __user
*)data
);
128 /* when I and D space are separate, this will have to be fixed. */
129 case PTRACE_POKETEXT
: /* write the word at location addr. */
130 case PTRACE_POKEDATA
:
131 ret
= generic_ptrace_pokedata(child
, addr
, data
);
134 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
136 if ((addr
& 3) || addr
< 0 ||
137 addr
> sizeof(struct user
) - 3)
140 if (addr
< sizeof(struct pt_regs
))
141 ret
= put_stack_long(child
, addr
, data
);
142 else if (addr
>= (long) &dummy
->fpu
&&
143 addr
< (long) &dummy
->u_fpvalid
) {
144 set_stopped_child_used_math(child
);
145 ((long *)&child
->thread
.fpu
)
146 [(addr
- (long)&dummy
->fpu
) >> 2] = data
;
148 } else if (addr
== (long) &dummy
->u_fpvalid
) {
149 conditional_stopped_child_used_math(data
, child
);
154 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
155 case PTRACE_CONT
: { /* restart after signal. */
157 if (!valid_signal(data
))
159 if (request
== PTRACE_SYSCALL
)
160 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
162 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
164 ptrace_disable_singlestep(child
);
166 child
->exit_code
= data
;
167 wake_up_process(child
);
173 * make the child exit. Best I can do is send it a sigkill.
174 * perhaps it should be put in the status that it wants to
179 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
181 ptrace_disable_singlestep(child
);
182 child
->exit_code
= SIGKILL
;
183 wake_up_process(child
);
187 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
189 struct pt_regs
*regs
= NULL
;
192 if (!valid_signal(data
))
194 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
195 if ((child
->ptrace
& PT_DTRACE
) == 0) {
196 /* Spurious delayed TF traps may occur */
197 child
->ptrace
|= PT_DTRACE
;
200 pc
= get_stack_long(child
, (long)®s
->pc
);
202 /* Next scheduling will set up UBC */
203 if (child
->thread
.ubc_pc
== 0)
205 child
->thread
.ubc_pc
= pc
;
207 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
208 child
->exit_code
= data
;
209 /* give it a chance to run. */
210 wake_up_process(child
);
216 case PTRACE_GETDSPREGS
: {
220 dp
= ((unsigned long) child
) + THREAD_SIZE
-
221 sizeof(struct pt_dspregs
);
222 if (*((int *) (dp
- 4)) == SR_FD
) {
223 copy_to_user((void *)addr
, (void *) dp
,
224 sizeof(struct pt_dspregs
));
230 case PTRACE_SETDSPREGS
: {
234 dp
= ((unsigned long) child
) + THREAD_SIZE
-
235 sizeof(struct pt_dspregs
);
236 if (*((int *) (dp
- 4)) == SR_FD
) {
237 copy_from_user((void *) dp
, (void *)addr
,
238 sizeof(struct pt_dspregs
));
245 ret
= ptrace_request(child
, request
, addr
, data
);
252 asmlinkage
void do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
254 struct task_struct
*tsk
= current
;
256 if (unlikely(current
->audit_context
) && entryexit
)
257 audit_syscall_exit(AUDITSC_RESULT(regs
->regs
[0]),
260 if (!test_thread_flag(TIF_SYSCALL_TRACE
) &&
261 !test_thread_flag(TIF_SINGLESTEP
))
263 if (!(tsk
->ptrace
& PT_PTRACED
))
266 /* the 0x80 provides a way for the tracing parent to distinguish
267 between a syscall stop and SIGTRAP delivery */
268 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) &&
269 !test_thread_flag(TIF_SINGLESTEP
) ? 0x80 : 0));
272 * this isn't the same as continuing with a signal, but it will do
273 * for normal use. strace only continues with a signal if the
274 * stopping signal is not SIGTRAP. -brl
276 if (tsk
->exit_code
) {
277 send_sig(tsk
->exit_code
, tsk
, 1);
282 if (unlikely(current
->audit_context
) && !entryexit
)
283 audit_syscall_entry(AUDIT_ARCH_SH
, regs
->regs
[3],
284 regs
->regs
[4], regs
->regs
[5],
285 regs
->regs
[6], regs
->regs
[7]);