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;
130 if (request == PTRACE_TRACEME) {
131 /* are we already being traced? */
132 if (current->ptrace & PT_PTRACED)
134 /* set the ptrace bit in the process flags. */
135 current->ptrace |= PT_PTRACED;
140 read_lock(&tasklist_lock);
141 child = find_task_by_pid(pid);
143 get_task_struct(child);
144 read_unlock(&tasklist_lock);
149 if (pid == 1) /* you may not mess with init */
152 if (request == PTRACE_ATTACH) {
153 ret = ptrace_attach(child);
157 ret = ptrace_check_attach(child, request == PTRACE_KILL);
162 /* when I and D space are separate, these will need to be fixed. */
163 case PTRACE_PEEKTEXT: /* read word at location addr. */
164 case PTRACE_PEEKDATA: {
168 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
170 if (copied != sizeof(tmp))
172 ret = put_user(tmp,(unsigned long *) data);
176 /* read the word at location addr in the USER area. */
177 case PTRACE_PEEKUSR: {
181 if ((addr & 3) || addr < 0)
184 if (addr < sizeof(struct pt_regs))
185 tmp = get_stack_long(child, addr);
186 else if ((addr >= offsetof(struct user, fpu)) &&
187 (addr < offsetof(struct user, u_fpvalid))) {
188 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
189 } else if (addr == offsetof(struct user, u_fpvalid)) {
190 tmp = !!tsk_used_math(child);
194 ret = put_user(tmp, (unsigned long *)data);
198 /* when I and D space are separate, this will have to be fixed. */
199 case PTRACE_POKETEXT: /* write the word at location addr. */
200 case PTRACE_POKEDATA:
202 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
208 /* write the word at location addr in the USER area. We must
209 disallow any changes to certain SR bits or u_fpvalid, since
210 this could crash the kernel or result in a security
213 if ((addr & 3) || addr < 0)
216 if (addr < sizeof(struct pt_regs)) {
217 /* Ignore change of top 32 bits of SR */
218 if (addr == offsetof (struct pt_regs, sr)+4)
223 /* If lower 32 bits of SR, ignore non-user bits */
224 if (addr == offsetof (struct pt_regs, sr))
226 long cursr = get_stack_long(child, addr);
228 data |= (cursr & SR_MASK);
230 ret = put_stack_long(child, addr, data);
232 else if ((addr >= offsetof(struct user, fpu)) &&
233 (addr < offsetof(struct user, u_fpvalid))) {
234 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
238 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
239 case PTRACE_CONT: { /* restart after signal. */
241 if ((unsigned long) data > _NSIG)
243 if (request == PTRACE_SYSCALL)
244 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
246 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247 child->exit_code = data;
248 wake_up_process(child);
254 * make the child exit. Best I can do is send it a sigkill.
255 * perhaps it should be put in the status that it wants to
260 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
262 child->exit_code = SIGKILL;
263 wake_up_process(child);
267 case PTRACE_SINGLESTEP: { /* set the trap flag. */
268 struct pt_regs *regs;
271 if ((unsigned long) data > _NSIG)
273 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
274 if ((child->ptrace & PT_DTRACE) == 0) {
275 /* Spurious delayed TF traps may occur */
276 child->ptrace |= PT_DTRACE;
279 regs = child->thread.uregs;
281 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
283 child->exit_code = data;
284 /* give it a chance to run. */
285 wake_up_process(child);
290 case PTRACE_DETACH: /* detach a process that was attached. */
291 ret = ptrace_detach(child, data);
295 ret = ptrace_request(child, request, addr, data);
299 put_task_struct(child);
305 asmlinkage void syscall_trace(void)
307 struct task_struct *tsk = current;
309 if (!test_thread_flag(TIF_SYSCALL_TRACE))
311 if (!(tsk->ptrace & PT_PTRACED))
314 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
317 * this isn't the same as continuing with a signal, but it will do
318 * for normal use. strace only continues with a signal if the
319 * stopping signal is not SIGTRAP. -brl
321 if (tsk->exit_code) {
322 send_sig(tsk->exit_code, tsk, 1);
327 /* Called with interrupts disabled */
328 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
330 /* This is called after a single step exception (DEBUGSS).
331 There is no need to change the PC, as it is a post-execution
332 exception, as entry.S does not do anything to the PC for DEBUGSS.
333 We need to clear the Single Step setting in SR to avoid
334 continually stepping. */
336 regs->sr &= ~SR_SSTEP;
337 force_sig(SIGTRAP, current);
340 /* Called with interrupts disabled */
341 asmlinkage void do_software_break_point(unsigned long long vec,
342 struct pt_regs *regs)
344 /* We need to forward step the PC, to counteract the backstep done
347 force_sig(SIGTRAP, current);
352 * Called by kernel/ptrace.c when detaching..
354 * Make sure single step bits etc are not set.
356 void ptrace_disable(struct task_struct *child)
358 /* nothing to do.. */