initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / sh64 / kernel / ptrace.c
blobc48bc4a27b44b459d3c95ea55b00bc98f2f967ec
1 /*
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
4 * for more details.
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>
24 #include <linux/mm.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>
31 #include <asm/io.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)
52 unsigned char *stack;
54 stack = (unsigned char *)(task->thread.uregs);
55 stack += offset;
56 return (*((int *)stack));
59 static inline unsigned long
60 get_fpu_long(struct task_struct *task, unsigned long addr)
62 unsigned long tmp;
63 struct pt_regs *regs;
64 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
66 if (!task->used_math) {
67 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
68 tmp = FPSCR_INIT;
69 } else {
70 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
72 return tmp;
75 if (last_task_used_math == task) {
76 grab_fpu();
77 fpsave(&task->thread.fpu.hard);
78 release_fpu();
79 last_task_used_math = 0;
80 regs->sr |= SR_FD;
83 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
84 return tmp;
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,
91 unsigned long data)
93 unsigned char *stack;
95 stack = (unsigned char *)(task->thread.uregs);
96 stack += offset;
97 *(unsigned long *) stack = data;
98 return 0;
101 static inline int
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 (!task->used_math) {
109 fpinit(&task->thread.fpu.hard);
110 task->used_math = 1;
111 } else if (last_task_used_math == task) {
112 grab_fpu();
113 fpsave(&task->thread.fpu.hard);
114 release_fpu();
115 last_task_used_math = 0;
116 regs->sr |= SR_FD;
119 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
120 return 0;
123 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
125 struct task_struct *child;
126 int ret;
128 lock_kernel();
129 ret = -EPERM;
130 if (request == PTRACE_TRACEME) {
131 /* are we already being traced? */
132 if (current->ptrace & PT_PTRACED)
133 goto out;
134 /* set the ptrace bit in the process flags. */
135 current->ptrace |= PT_PTRACED;
136 ret = 0;
137 goto out;
139 ret = -ESRCH;
140 read_lock(&tasklist_lock);
141 child = find_task_by_pid(pid);
142 if (child)
143 get_task_struct(child);
144 read_unlock(&tasklist_lock);
145 if (!child)
146 goto out;
148 ret = -EPERM;
149 if (pid == 1) /* you may not mess with init */
150 goto out_tsk;
152 if (request == PTRACE_ATTACH) {
153 ret = ptrace_attach(child);
154 goto out_tsk;
157 ret = ptrace_check_attach(child, request == PTRACE_KILL);
158 if (ret < 0)
159 goto out_tsk;
161 switch (request) {
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: {
165 unsigned long tmp;
166 int copied;
168 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
169 ret = -EIO;
170 if (copied != sizeof(tmp))
171 break;
172 ret = put_user(tmp,(unsigned long *) data);
173 break;
176 /* read the word at location addr in the USER area. */
177 case PTRACE_PEEKUSR: {
178 unsigned long tmp;
180 ret = -EIO;
181 if ((addr & 3) || addr < 0)
182 break;
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 = child->used_math;
191 } else {
192 break;
194 ret = put_user(tmp, (unsigned long *)data);
195 break;
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:
201 ret = 0;
202 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
203 break;
204 ret = -EIO;
205 break;
207 case PTRACE_POKEUSR:
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
211 loophole. */
212 ret = -EIO;
213 if ((addr & 3) || addr < 0)
214 break;
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)
220 ret = 0;
221 break;
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);
227 data &= ~(SR_MASK);
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);
236 break;
238 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
239 case PTRACE_CONT: { /* restart after signal. */
240 ret = -EIO;
241 if ((unsigned long) data > _NSIG)
242 break;
243 if (request == PTRACE_SYSCALL)
244 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
245 else
246 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247 child->exit_code = data;
248 wake_up_process(child);
249 ret = 0;
250 break;
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
256 * exit.
258 case PTRACE_KILL: {
259 ret = 0;
260 if (child->state == TASK_ZOMBIE) /* already dead */
261 break;
262 child->exit_code = SIGKILL;
263 wake_up_process(child);
264 break;
267 case PTRACE_SINGLESTEP: { /* set the trap flag. */
268 struct pt_regs *regs;
270 ret = -EIO;
271 if ((unsigned long) data > _NSIG)
272 break;
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);
286 ret = 0;
287 break;
290 case PTRACE_DETACH: /* detach a process that was attached. */
291 ret = ptrace_detach(child, data);
292 break;
294 default:
295 ret = ptrace_request(child, request, addr, data);
296 break;
298 out_tsk:
299 put_task_struct(child);
300 out:
301 unlock_kernel();
302 return ret;
305 asmlinkage void syscall_trace(void)
307 struct task_struct *tsk = current;
309 if (!test_thread_flag(TIF_SYSCALL_TRACE))
310 return;
311 if (!(tsk->ptrace & PT_PTRACED))
312 return;
314 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
315 ? 0x80 : 0));
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);
323 tsk->exit_code = 0;
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. */
335 local_irq_enable();
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
345 in signal.c. */
346 local_irq_enable();
347 force_sig(SIGTRAP, current);
348 regs->pc += 4;
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.. */