2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * arch/sh64/kernel/ptrace.c
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003 Paul Mundt
11 * Started from SH3/4 version:
12 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
14 * Original x86 implementation:
15 * By Ross Biro 1/23/92
16 * edited by Linus Torvalds
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/rwsem.h>
23 #include <linux/sched.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35 #include <asm/processor.h>
36 #include <asm/mmu_context.h>
38 /* This mask defines the bits of the SR which the user is not allowed to
39 change, which are everything except S, Q, M, PR, SZ, FR. */
40 #define SR_MASK (0xffff8cfd)
43 * does not yet catch signals sent when the child dies.
44 * in exit.c or in signal.c.
48 * This routine will get a word from the user area in the process kernel stack.
50 static inline int get_stack_long(struct task_struct
*task
, int offset
)
54 stack
= (unsigned char *)(task
->thread
.uregs
);
56 return (*((int *)stack
));
59 static inline unsigned long
60 get_fpu_long(struct task_struct
*task
, unsigned long addr
)
64 regs
= (struct pt_regs
*)((unsigned char *)task
+ THREAD_SIZE
) - 1;
66 if (!tsk_used_math(task
)) {
67 if (addr
== offsetof(struct user_fpu_struct
, fpscr
)) {
70 tmp
= 0xffffffffUL
; /* matches initial value in fpu.c */
75 if (last_task_used_math
== task
) {
77 fpsave(&task
->thread
.fpu
.hard
);
79 last_task_used_math
= 0;
83 tmp
= ((long *)&task
->thread
.fpu
)[addr
/ sizeof(unsigned long)];
88 * This routine will put a word into the user area in the process kernel stack.
90 static inline int put_stack_long(struct task_struct
*task
, int offset
,
95 stack
= (unsigned char *)(task
->thread
.uregs
);
97 *(unsigned long *) stack
= data
;
102 put_fpu_long(struct task_struct
*task
, unsigned long addr
, unsigned long data
)
104 struct pt_regs
*regs
;
106 regs
= (struct pt_regs
*)((unsigned char *)task
+ THREAD_SIZE
) - 1;
108 if (!tsk_used_math(task
)) {
109 fpinit(&task
->thread
.fpu
.hard
);
110 set_stopped_child_used_math(task
);
111 } else if (last_task_used_math
== task
) {
113 fpsave(&task
->thread
.fpu
.hard
);
115 last_task_used_math
= 0;
119 ((long *)&task
->thread
.fpu
)[addr
/ sizeof(unsigned long)] = data
;
123 asmlinkage
int sys_ptrace(long request
, long pid
, long addr
, long data
)
125 struct task_struct
*child
;
126 extern void poke_real_address_q(unsigned long long addr
, unsigned long long data
);
127 #define WPC_DBRMODE 0x0d104008
128 static int first_call
= 1;
134 /* Set WPC.DBRMODE to 0. This makes all debug events get
135 * delivered through RESVEC, i.e. into the handlers in entry.S.
136 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
137 * would normally be left set to 1, which makes debug events get
138 * delivered through DBRVEC, i.e. into the remote gdb's
139 * handlers. This prevents ptrace getting them, and confuses
140 * the remote gdb.) */
141 printk("DBRMODE set to 0 to permit native debugging\n");
142 poke_real_address_q(WPC_DBRMODE
, 0);
147 if (request
== PTRACE_TRACEME
) {
148 /* are we already being traced? */
149 if (current
->ptrace
& PT_PTRACED
)
151 /* set the ptrace bit in the process flags. */
152 current
->ptrace
|= PT_PTRACED
;
157 read_lock(&tasklist_lock
);
158 child
= find_task_by_pid(pid
);
160 get_task_struct(child
);
161 read_unlock(&tasklist_lock
);
166 if (pid
== 1) /* you may not mess with init */
169 if (request
== PTRACE_ATTACH
) {
170 ret
= ptrace_attach(child
);
174 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
179 /* when I and D space are separate, these will need to be fixed. */
180 case PTRACE_PEEKTEXT
: /* read word at location addr. */
181 case PTRACE_PEEKDATA
: {
185 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
187 if (copied
!= sizeof(tmp
))
189 ret
= put_user(tmp
,(unsigned long *) data
);
193 /* read the word at location addr in the USER area. */
194 case PTRACE_PEEKUSR
: {
198 if ((addr
& 3) || addr
< 0)
201 if (addr
< sizeof(struct pt_regs
))
202 tmp
= get_stack_long(child
, addr
);
203 else if ((addr
>= offsetof(struct user
, fpu
)) &&
204 (addr
< offsetof(struct user
, u_fpvalid
))) {
205 tmp
= get_fpu_long(child
, addr
- offsetof(struct user
, fpu
));
206 } else if (addr
== offsetof(struct user
, u_fpvalid
)) {
207 tmp
= !!tsk_used_math(child
);
211 ret
= put_user(tmp
, (unsigned long *)data
);
215 /* when I and D space are separate, this will have to be fixed. */
216 case PTRACE_POKETEXT
: /* write the word at location addr. */
217 case PTRACE_POKEDATA
:
219 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
225 /* write the word at location addr in the USER area. We must
226 disallow any changes to certain SR bits or u_fpvalid, since
227 this could crash the kernel or result in a security
230 if ((addr
& 3) || addr
< 0)
233 if (addr
< sizeof(struct pt_regs
)) {
234 /* Ignore change of top 32 bits of SR */
235 if (addr
== offsetof (struct pt_regs
, sr
)+4)
240 /* If lower 32 bits of SR, ignore non-user bits */
241 if (addr
== offsetof (struct pt_regs
, sr
))
243 long cursr
= get_stack_long(child
, addr
);
245 data
|= (cursr
& SR_MASK
);
247 ret
= put_stack_long(child
, addr
, data
);
249 else if ((addr
>= offsetof(struct user
, fpu
)) &&
250 (addr
< offsetof(struct user
, u_fpvalid
))) {
251 ret
= put_fpu_long(child
, addr
- offsetof(struct user
, fpu
), data
);
255 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
256 case PTRACE_CONT
: { /* restart after signal. */
258 if ((unsigned long) data
> _NSIG
)
260 if (request
== PTRACE_SYSCALL
)
261 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
263 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
264 child
->exit_code
= data
;
265 wake_up_process(child
);
271 * make the child exit. Best I can do is send it a sigkill.
272 * perhaps it should be put in the status that it wants to
277 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
279 child
->exit_code
= SIGKILL
;
280 wake_up_process(child
);
284 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
285 struct pt_regs
*regs
;
288 if ((unsigned long) data
> _NSIG
)
290 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
291 if ((child
->ptrace
& PT_DTRACE
) == 0) {
292 /* Spurious delayed TF traps may occur */
293 child
->ptrace
|= PT_DTRACE
;
296 regs
= child
->thread
.uregs
;
298 regs
->sr
|= SR_SSTEP
; /* auto-resetting upon exception */
300 child
->exit_code
= data
;
301 /* give it a chance to run. */
302 wake_up_process(child
);
307 case PTRACE_DETACH
: /* detach a process that was attached. */
308 ret
= ptrace_detach(child
, data
);
312 ret
= ptrace_request(child
, request
, addr
, data
);
316 put_task_struct(child
);
322 asmlinkage
void syscall_trace(void)
324 struct task_struct
*tsk
= current
;
326 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
328 if (!(tsk
->ptrace
& PT_PTRACED
))
331 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
334 * this isn't the same as continuing with a signal, but it will do
335 * for normal use. strace only continues with a signal if the
336 * stopping signal is not SIGTRAP. -brl
338 if (tsk
->exit_code
) {
339 send_sig(tsk
->exit_code
, tsk
, 1);
344 /* Called with interrupts disabled */
345 asmlinkage
void do_single_step(unsigned long long vec
, struct pt_regs
*regs
)
347 /* This is called after a single step exception (DEBUGSS).
348 There is no need to change the PC, as it is a post-execution
349 exception, as entry.S does not do anything to the PC for DEBUGSS.
350 We need to clear the Single Step setting in SR to avoid
351 continually stepping. */
353 regs
->sr
&= ~SR_SSTEP
;
354 force_sig(SIGTRAP
, current
);
357 /* Called with interrupts disabled */
358 asmlinkage
void do_software_break_point(unsigned long long vec
,
359 struct pt_regs
*regs
)
361 /* We need to forward step the PC, to counteract the backstep done
364 force_sig(SIGTRAP
, current
);
369 * Called by kernel/ptrace.c when detaching..
371 * Make sure single step bits etc are not set.
373 void ptrace_disable(struct task_struct
*child
)
375 /* nothing to do.. */