Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / sh / kernel / ptrace.c
1 /*
2  * linux/arch/sh/kernel/ptrace.c
3  *
4  * Original x86 implementation:
5  *      By Ross Biro 1/23/92
6  *      edited by Linus Torvalds
7  *
8  * SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/slab.h>
22 #include <linux/security.h>
23
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
29 #include <asm/mmu_context.h>
30
31 /*
32  * does not yet catch signals sent when the child dies.
33  * in exit.c or in signal.c.
34  */
35
36 /*
37  * This routine will get a word off of the process kernel stack.
38  */
39 static inline int get_stack_long(struct task_struct *task, int offset)
40 {
41         unsigned char *stack;
42
43         stack = (unsigned char *)
44                 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
45 #ifdef CONFIG_SH_DSP
46                 - sizeof(struct pt_dspregs)
47 #endif
48                 - sizeof(unsigned long);
49         stack += offset;
50         return (*((int *)stack));
51 }
52
53 /*
54  * This routine will put a word on the process kernel stack.
55  */
56 static inline int put_stack_long(struct task_struct *task, int offset,
57                                  unsigned long data)
58 {
59         unsigned char *stack;
60
61         stack = (unsigned char *)
62                 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs)
63 #ifdef CONFIG_SH_DSP
64                 - sizeof(struct pt_dspregs)
65 #endif
66                 - sizeof(unsigned long);
67         stack += offset;
68         *(unsigned long *) stack = data;
69         return 0;
70 }
71
72 /*
73  * Called by kernel/ptrace.c when detaching..
74  *
75  * Make sure single step bits etc are not set.
76  */
77 void ptrace_disable(struct task_struct *child)
78 {
79         /* nothing to do.. */
80 }
81
82 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
83 {
84         struct task_struct *child;
85         struct user * dummy = NULL;
86         int ret;
87
88         lock_kernel();
89         ret = -EPERM;
90         if (request == PTRACE_TRACEME) {
91                 /* are we already being traced? */
92                 if (current->ptrace & PT_PTRACED)
93                         goto out;
94                 ret = security_ptrace(current->parent, current);
95                 if (ret)
96                         goto out;
97                 /* set the ptrace bit in the process flags. */
98                 current->ptrace |= PT_PTRACED;
99                 ret = 0;
100                 goto out;
101         }
102         ret = -ESRCH;
103         read_lock(&tasklist_lock);
104         child = find_task_by_pid(pid);
105         if (child)
106                 get_task_struct(child);
107         read_unlock(&tasklist_lock);
108         if (!child)
109                 goto out;
110
111         ret = -EPERM;
112         if (pid == 1)           /* you may not mess with init */
113                 goto out_tsk;
114
115         if (request == PTRACE_ATTACH) {
116                 ret = ptrace_attach(child);
117                 goto out_tsk;
118         }
119
120         ret = ptrace_check_attach(child, request == PTRACE_KILL);
121         if (ret < 0)
122                 goto out_tsk;
123
124         switch (request) {
125         /* when I and D space are separate, these will need to be fixed. */
126         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
127         case PTRACE_PEEKDATA: {
128                 unsigned long tmp;
129                 int copied;
130
131                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
132                 ret = -EIO;
133                 if (copied != sizeof(tmp))
134                         break;
135                 ret = put_user(tmp,(unsigned long *) data);
136                 break;
137         }
138
139         /* read the word at location addr in the USER area. */
140         case PTRACE_PEEKUSR: {
141                 unsigned long tmp;
142
143                 ret = -EIO;
144                 if ((addr & 3) || addr < 0 || 
145                     addr > sizeof(struct user) - 3)
146                         break;
147
148                 if (addr < sizeof(struct pt_regs))
149                         tmp = get_stack_long(child, addr);
150                 else if (addr >= (long) &dummy->fpu &&
151                          addr < (long) &dummy->u_fpvalid) {
152                         if (!tsk_used_math(child)) {
153                                 if (addr == (long)&dummy->fpu.fpscr)
154                                         tmp = FPSCR_INIT;
155                                 else
156                                         tmp = 0;
157                         } else
158                                 tmp = ((long *)&child->thread.fpu)
159                                         [(addr - (long)&dummy->fpu) >> 2];
160                 } else if (addr == (long) &dummy->u_fpvalid)
161                         tmp = !!tsk_used_math(child);
162                 else
163                         tmp = 0;
164                 ret = put_user(tmp, (unsigned long *)data);
165                 break;
166         }
167
168         /* when I and D space are separate, this will have to be fixed. */
169         case PTRACE_POKETEXT: /* write the word at location addr. */
170         case PTRACE_POKEDATA:
171                 ret = 0;
172                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
173                         break;
174                 ret = -EIO;
175                 break;
176
177         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
178                 ret = -EIO;
179                 if ((addr & 3) || addr < 0 || 
180                     addr > sizeof(struct user) - 3)
181                         break;
182
183                 if (addr < sizeof(struct pt_regs))
184                         ret = put_stack_long(child, addr, data);
185                 else if (addr >= (long) &dummy->fpu &&
186                          addr < (long) &dummy->u_fpvalid) {
187                         set_stopped_child_used_math(child);
188                         ((long *)&child->thread.fpu)
189                                 [(addr - (long)&dummy->fpu) >> 2] = data;
190                         ret = 0;
191                 } else if (addr == (long) &dummy->u_fpvalid) {
192                         conditional_stopped_child_used_math(data, child);
193                         ret = 0;
194                 }
195                 break;
196
197         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
198         case PTRACE_CONT: { /* restart after signal. */
199                 ret = -EIO;
200                 if ((unsigned long) data > _NSIG)
201                         break;
202                 if (request == PTRACE_SYSCALL)
203                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
204                 else
205                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
206                 child->exit_code = data;
207                 wake_up_process(child);
208                 ret = 0;
209                 break;
210         }
211
212 /*
213  * make the child exit.  Best I can do is send it a sigkill. 
214  * perhaps it should be put in the status that it wants to 
215  * exit.
216  */
217         case PTRACE_KILL: {
218                 ret = 0;
219                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
220                         break;
221                 child->exit_code = SIGKILL;
222                 wake_up_process(child);
223                 break;
224         }
225
226         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
227                 long pc;
228                 struct pt_regs *dummy = NULL;
229
230                 ret = -EIO;
231                 if ((unsigned long) data > _NSIG)
232                         break;
233                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
234                 if ((child->ptrace & PT_DTRACE) == 0) {
235                         /* Spurious delayed TF traps may occur */
236                         child->ptrace |= PT_DTRACE;
237                 }
238
239                 pc = get_stack_long(child, (long)&dummy->pc);
240
241                 /* Next scheduling will set up UBC */
242                 if (child->thread.ubc_pc == 0)
243                         ubc_usercnt += 1;
244                 child->thread.ubc_pc = pc;
245
246                 child->exit_code = data;
247                 /* give it a chance to run. */
248                 wake_up_process(child);
249                 ret = 0;
250                 break;
251         }
252
253         case PTRACE_DETACH: /* detach a process that was attached. */
254                 ret = ptrace_detach(child, data);
255                 break;
256
257 #ifdef CONFIG_SH_DSP
258         case PTRACE_GETDSPREGS: {
259                 unsigned long dp;
260
261                 ret = -EIO;
262                 dp = ((unsigned long) child) + THREAD_SIZE -
263                          sizeof(struct pt_dspregs);
264                 if (*((int *) (dp - 4)) == SR_FD) {
265                         copy_to_user(addr, (void *) dp,
266                                 sizeof(struct pt_dspregs));
267                         ret = 0;
268                 }
269                 break;
270         }
271
272         case PTRACE_SETDSPREGS: {
273                 unsigned long dp;
274                 int i;
275
276                 ret = -EIO;
277                 dp = ((unsigned long) child) + THREAD_SIZE -
278                          sizeof(struct pt_dspregs);
279                 if (*((int *) (dp - 4)) == SR_FD) {
280                         copy_from_user((void *) dp, addr,
281                                 sizeof(struct pt_dspregs));
282                         ret = 0;
283                 }
284                 break;
285         }
286 #endif
287         default:
288                 ret = ptrace_request(child, request, addr, data);
289                 break;
290         }
291 out_tsk:
292         put_task_struct(child);
293 out:
294         unlock_kernel();
295         return ret;
296 }
297
298 asmlinkage void do_syscall_trace(void)
299 {
300         struct task_struct *tsk = current;
301
302         if (!test_thread_flag(TIF_SYSCALL_TRACE))
303                 return;
304         if (!(tsk->ptrace & PT_PTRACED))
305                 return;
306         /* the 0x80 provides a way for the tracing parent to distinguish
307            between a syscall stop and SIGTRAP delivery */
308         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
309                                  ? 0x80 : 0));
310
311         /*
312          * this isn't the same as continuing with a signal, but it will do
313          * for normal use.  strace only continues with a signal if the
314          * stopping signal is not SIGTRAP.  -brl
315          */
316         if (tsk->exit_code) {
317                 send_sig(tsk->exit_code, tsk, 1);
318                 tsk->exit_code = 0;
319         }
320 }