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