1 /* MN10300 Process tracing
3 * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Modified by David Howells (dhowells@redhat.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
22 #include <asm/system.h>
23 #include <asm/processor.h>
24 #include <asm/cacheflush.h>
26 #include <asm/asm-offsets.h>
29 * translate ptrace register IDs into struct pt_regs offsets
31 static const u8 ptrace_regid_to_frame
[] = {
32 [PT_A3
<< 2] = REG_A3
,
33 [PT_A2
<< 2] = REG_A2
,
34 [PT_D3
<< 2] = REG_D3
,
35 [PT_D2
<< 2] = REG_D2
,
36 [PT_MCVF
<< 2] = REG_MCVF
,
37 [PT_MCRL
<< 2] = REG_MCRL
,
38 [PT_MCRH
<< 2] = REG_MCRH
,
39 [PT_MDRQ
<< 2] = REG_MDRQ
,
40 [PT_E1
<< 2] = REG_E1
,
41 [PT_E0
<< 2] = REG_E0
,
42 [PT_E7
<< 2] = REG_E7
,
43 [PT_E6
<< 2] = REG_E6
,
44 [PT_E5
<< 2] = REG_E5
,
45 [PT_E4
<< 2] = REG_E4
,
46 [PT_E3
<< 2] = REG_E3
,
47 [PT_E2
<< 2] = REG_E2
,
48 [PT_SP
<< 2] = REG_SP
,
49 [PT_LAR
<< 2] = REG_LAR
,
50 [PT_LIR
<< 2] = REG_LIR
,
51 [PT_MDR
<< 2] = REG_MDR
,
52 [PT_A1
<< 2] = REG_A1
,
53 [PT_A0
<< 2] = REG_A0
,
54 [PT_D1
<< 2] = REG_D1
,
55 [PT_D0
<< 2] = REG_D0
,
56 [PT_ORIG_D0
<< 2] = REG_ORIG_D0
,
57 [PT_EPSW
<< 2] = REG_EPSW
,
58 [PT_PC
<< 2] = REG_PC
,
61 static inline int get_stack_long(struct task_struct
*task
, int offset
)
63 return *(unsigned long *)
64 ((unsigned long) task
->thread
.uregs
+ offset
);
68 * this routine will put a word on the processes privileged stack.
69 * the offset is how far from the base addr as stored in the TSS.
70 * this routine assumes that all the privileged stacks are in our
74 int put_stack_long(struct task_struct
*task
, int offset
, unsigned long data
)
78 stack
= (unsigned long) task
->thread
.uregs
+ offset
;
79 *(unsigned long *) stack
= data
;
83 static inline unsigned long get_fpregs(struct fpu_state_struct
*buf
,
84 struct task_struct
*tsk
)
86 return __copy_to_user(buf
, &tsk
->thread
.fpu_state
,
87 sizeof(struct fpu_state_struct
));
90 static inline unsigned long set_fpregs(struct task_struct
*tsk
,
91 struct fpu_state_struct
*buf
)
93 return __copy_from_user(&tsk
->thread
.fpu_state
, buf
,
94 sizeof(struct fpu_state_struct
));
97 static inline void fpsave_init(struct task_struct
*task
)
99 memset(&task
->thread
.fpu_state
, 0, sizeof(struct fpu_state_struct
));
103 * make sure the single step bit is not set
105 void ptrace_disable(struct task_struct
*child
)
107 #ifndef CONFIG_MN10300_USING_JTAG
108 struct user
*dummy
= NULL
;
111 tmp
= get_stack_long(child
, (unsigned long) &dummy
->regs
.epsw
);
113 put_stack_long(child
, (unsigned long) &dummy
->regs
.epsw
, tmp
);
118 * set the single step bit
120 void ptrace_enable(struct task_struct
*child
)
122 #ifndef CONFIG_MN10300_USING_JTAG
123 struct user
*dummy
= NULL
;
126 tmp
= get_stack_long(child
, (unsigned long) &dummy
->regs
.epsw
);
128 put_stack_long(child
, (unsigned long) &dummy
->regs
.epsw
, tmp
);
133 * handle the arch-specific side of process tracing
135 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
137 struct fpu_state_struct fpu_state
;
141 /* read the word at location addr. */
142 case PTRACE_PEEKTEXT
: {
146 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
148 if (copied
!= sizeof(tmp
))
150 ret
= put_user(tmp
, (unsigned long *) data
);
154 /* read the word at location addr. */
155 case PTRACE_PEEKDATA
: {
159 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
161 if (copied
!= sizeof(tmp
))
163 ret
= put_user(tmp
, (unsigned long *) data
);
167 /* read the word at location addr in the USER area. */
168 case PTRACE_PEEKUSR
: {
172 if ((addr
& 3) || addr
< 0 ||
173 addr
> sizeof(struct user
) - 3)
176 tmp
= 0; /* Default return condition */
177 if (addr
< NR_PTREGS
<< 2)
178 tmp
= get_stack_long(child
,
179 ptrace_regid_to_frame
[addr
]);
180 ret
= put_user(tmp
, (unsigned long *) data
);
184 /* write the word at location addr. */
185 case PTRACE_POKETEXT
:
186 case PTRACE_POKEDATA
:
187 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) ==
194 /* write the word at location addr in the USER area */
197 if ((addr
& 3) || addr
< 0 ||
198 addr
> sizeof(struct user
) - 3)
202 if (addr
< NR_PTREGS
<< 2)
203 ret
= put_stack_long(child
, ptrace_regid_to_frame
[addr
],
207 /* continue and stop at next (return from) syscall */
209 /* restart after signal. */
212 if ((unsigned long) data
> _NSIG
)
214 if (request
== PTRACE_SYSCALL
)
215 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
217 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
218 child
->exit_code
= data
;
219 ptrace_disable(child
);
220 wake_up_process(child
);
225 * make the child exit
226 * - the best I can do is send it a sigkill
227 * - perhaps it should be put in the status that it wants to
232 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
234 child
->exit_code
= SIGKILL
;
235 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
236 ptrace_disable(child
);
237 wake_up_process(child
);
240 case PTRACE_SINGLESTEP
: /* set the trap flag. */
241 #ifndef CONFIG_MN10300_USING_JTAG
243 if ((unsigned long) data
> _NSIG
)
245 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
246 ptrace_enable(child
);
247 child
->exit_code
= data
;
248 wake_up_process(child
);
255 case PTRACE_DETACH
: /* detach a process that was attached. */
256 ret
= ptrace_detach(child
, data
);
259 /* Get all gp regs from the child. */
260 case PTRACE_GETREGS
: {
263 if (!access_ok(VERIFY_WRITE
, (unsigned *) data
, NR_PTREGS
<< 2)) {
268 for (i
= 0; i
< NR_PTREGS
<< 2; i
+= 4) {
269 tmp
= get_stack_long(child
, ptrace_regid_to_frame
[i
]);
270 __put_user(tmp
, (unsigned long *) data
);
277 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
280 if (!access_ok(VERIFY_READ
, (unsigned long *)data
,
281 sizeof(struct pt_regs
))) {
286 for (i
= 0; i
< NR_PTREGS
<< 2; i
+= 4) {
287 __get_user(tmp
, (unsigned long *) data
);
288 put_stack_long(child
, ptrace_regid_to_frame
[i
], tmp
);
295 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
296 if (is_using_fpu(child
)) {
298 fpu_state
= child
->thread
.fpu_state
;
300 memset(&fpu_state
, 0, sizeof(fpu_state
));
304 if (copy_to_user((void *) data
, &fpu_state
,
305 sizeof(fpu_state
)) == 0)
310 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
312 if (copy_from_user(&fpu_state
, (const void *) data
,
313 sizeof(fpu_state
)) == 0) {
314 fpu_kill_state(child
);
315 child
->thread
.fpu_state
= fpu_state
;
316 set_using_fpu(child
);
322 case PTRACE_SETOPTIONS
: {
323 if (data
& PTRACE_O_TRACESYSGOOD
)
324 child
->ptrace
|= PT_TRACESYSGOOD
;
326 child
->ptrace
&= ~PT_TRACESYSGOOD
;
340 * notification of system call entry/exit
341 * - triggered by current->work.syscall_trace
343 asmlinkage
void do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
346 /* just in case... */
347 printk(KERN_DEBUG
"[%d] syscall_%lu(%lx,%lx,%lx,%lx) = %lx\n",
358 if (!test_thread_flag(TIF_SYSCALL_TRACE
) &&
359 !test_thread_flag(TIF_SINGLESTEP
))
361 if (!(current
->ptrace
& PT_PTRACED
))
364 /* the 0x80 provides a way for the tracing parent to distinguish
365 between a syscall stop and SIGTRAP delivery */
366 ptrace_notify(SIGTRAP
|
367 ((current
->ptrace
& PT_TRACESYSGOOD
) &&
368 !test_thread_flag(TIF_SINGLESTEP
) ? 0x80 : 0));
371 * this isn't the same as continuing with a signal, but it will do
372 * for normal use. strace only continues with a signal if the
373 * stopping signal is not SIGTRAP. -brl
375 if (current
->exit_code
) {
376 send_sig(current
->exit_code
, current
, 1);
377 current
->exit_code
= 0;