vserver 2.0 rc7
[linux-2.6.git] / arch / cris / arch-v10 / kernel / ptrace.c
1 /*
2  * Copyright (C) 2000-2003, Axis Communications AB.
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/mm.h>
8 #include <linux/smp.h>
9 #include <linux/smp_lock.h>
10 #include <linux/errno.h>
11 #include <linux/ptrace.h>
12 #include <linux/user.h>
13 #include <linux/signal.h>
14
15 #include <asm/uaccess.h>
16 #include <asm/page.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/processor.h>
20
21 /* 
22  * Determines which bits in DCCR the user has access to.
23  * 1 = access, 0 = no access.
24  */
25 #define DCCR_MASK 0x0000001f     /* XNZVC */
26
27 /*
28  * Get contents of register REGNO in task TASK.
29  */
30 inline long get_reg(struct task_struct *task, unsigned int regno)
31 {
32         /* USP is a special case, it's not in the pt_regs struct but
33          * in the tasks thread struct
34          */
35
36         if (regno == PT_USP)
37                 return task->thread.usp;
38         else if (regno < PT_MAX)
39                 return ((unsigned long *)user_regs(task->thread_info))[regno];
40         else
41                 return 0;
42 }
43
44 /*
45  * Write contents of register REGNO in task TASK.
46  */
47 inline int put_reg(struct task_struct *task, unsigned int regno,
48                           unsigned long data)
49 {
50         if (regno == PT_USP)
51                 task->thread.usp = data;
52         else if (regno < PT_MAX)
53                 ((unsigned long *)user_regs(task->thread_info))[regno] = data;
54         else
55                 return -1;
56         return 0;
57 }
58
59 /*
60  * Called by kernel/ptrace.c when detaching.
61  *
62  * Make sure the single step bit is not set.
63  */
64 void 
65 ptrace_disable(struct task_struct *child)
66 {
67        /* Todo - pending singlesteps? */
68 }
69
70 /* 
71  * Note that this implementation of ptrace behaves differently from vanilla
72  * ptrace.  Contrary to what the man page says, in the PTRACE_PEEKTEXT,
73  * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
74  * ignored.  Instead, the data variable is expected to point at a location
75  * (in user space) where the result of the ptrace call is written (instead of
76  * being returned).
77  */
78 asmlinkage int 
79 sys_ptrace(long request, long pid, long addr, long data)
80 {
81         struct task_struct *child;
82         int ret;
83         unsigned long __user *datap = (unsigned long __user *)data;
84
85         lock_kernel();
86         ret = -EPERM;
87         
88         if (request == PTRACE_TRACEME) {
89                 if (current->ptrace & PT_PTRACED)
90                         goto out;
91
92                 current->ptrace |= PT_PTRACED;
93                 ret = 0;
94                 goto out;
95         }
96         
97         ret = -ESRCH;
98         read_lock(&tasklist_lock);
99         child = find_task_by_pid(pid);
100         
101         if (child)
102                 get_task_struct(child);
103         
104         read_unlock(&tasklist_lock);
105         
106         if (!child)
107                 goto out;
108         
109         ret = -EPERM;
110         
111         if (pid == 1)           /* Leave the init process alone! */
112                 goto out_tsk;
113         
114         if (request == PTRACE_ATTACH) {
115                 ret = ptrace_attach(child);
116                 goto out_tsk;
117         }
118         
119         ret = ptrace_check_attach(child, request == PTRACE_KILL);
120         if (ret < 0)
121                 goto out_tsk;
122
123         switch (request) {
124                 /* Read word at location address. */ 
125                 case PTRACE_PEEKTEXT:
126                 case PTRACE_PEEKDATA: {
127                         unsigned long tmp;
128                         int copied;
129
130                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
131                         ret = -EIO;
132                         
133                         if (copied != sizeof(tmp))
134                                 break;
135                         
136                         ret = put_user(tmp,datap);
137                         break;
138                 }
139
140                 /* Read the word at location address in the USER area. */
141                 case PTRACE_PEEKUSR: {
142                         unsigned long tmp;
143
144                         ret = -EIO;
145                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
146                                 break;
147
148                         tmp = get_reg(child, addr >> 2);
149                         ret = put_user(tmp, datap);
150                         break;
151                 }
152                 
153                 /* Write the word at location address. */
154                 case PTRACE_POKETEXT:
155                 case PTRACE_POKEDATA:
156                         ret = 0;
157                         
158                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
159                                 break;
160                         
161                         ret = -EIO;
162                         break;
163  
164                 /* Write the word at location address in the USER area. */
165                 case PTRACE_POKEUSR:
166                         ret = -EIO;
167                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
168                                 break;
169
170                         addr >>= 2;
171
172                         if (addr == PT_DCCR) {
173                                 /* don't allow the tracing process to change stuff like
174                                  * interrupt enable, kernel/user bit, dma enables etc.
175                                  */
176                                 data &= DCCR_MASK;
177                                 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
178                         }
179                         if (put_reg(child, addr, data))
180                                 break;
181                         ret = 0;
182                         break;
183
184                 case PTRACE_SYSCALL:
185                 case PTRACE_CONT:
186                         ret = -EIO;
187                         
188                         if (!valid_signal(data))
189                                 break;
190                         
191                         if (request == PTRACE_SYSCALL) {
192                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
193                         }
194                         else {
195                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
196                         }
197                         
198                         child->exit_code = data;
199                         
200                         /* TODO: make sure any pending breakpoint is killed */
201                         wake_up_process(child);
202                         ret = 0;
203                         
204                         break;
205                 
206                 /* Make the child exit by sending it a sigkill. */
207                 case PTRACE_KILL:
208                         ret = 0;
209                         
210                         if (child->state == TASK_ZOMBIE)
211                                 break;
212                         
213                         child->exit_code = SIGKILL;
214                         
215                         /* TODO: make sure any pending breakpoint is killed */
216                         wake_up_process(child);
217                         break;
218
219                 /* Set the trap flag. */
220                 case PTRACE_SINGLESTEP:
221                         ret = -EIO;
222                         
223                         if (!valid_signal(data))
224                                 break;
225                         
226                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
227
228                         /* TODO: set some clever breakpoint mechanism... */
229
230                         child->exit_code = data;
231                         wake_up_process(child);
232                         ret = 0;
233                         break;
234
235                 case PTRACE_DETACH:
236                         ret = ptrace_detach(child, data);
237                         break;
238
239                 /* Get all GP registers from the child. */
240                 case PTRACE_GETREGS: {
241                         int i;
242                         unsigned long tmp;
243                         
244                         for (i = 0; i <= PT_MAX; i++) {
245                                 tmp = get_reg(child, i);
246                                 
247                                 if (put_user(tmp, datap)) {
248                                         ret = -EFAULT;
249                                         goto out_tsk;
250                                 }
251                                 
252                                 data += sizeof(long);
253                         }
254
255                         ret = 0;
256                         break;
257                 }
258
259                 /* Set all GP registers in the child. */
260                 case PTRACE_SETREGS: {
261                         int i;
262                         unsigned long tmp;
263                         
264                         for (i = 0; i <= PT_MAX; i++) {
265                                 if (get_user(tmp, datap)) {
266                                         ret = -EFAULT;
267                                         goto out_tsk;
268                                 }
269                                 
270                                 if (i == PT_DCCR) {
271                                         tmp &= DCCR_MASK;
272                                         tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
273                                 }
274                                 
275                                 put_reg(child, i, tmp);
276                                 data += sizeof(long);
277                         }
278                         
279                         ret = 0;
280                         break;
281                 }
282
283                 default:
284                         ret = ptrace_request(child, request, addr, data);
285                         break;
286         }
287 out_tsk:
288         put_task_struct(child);
289 out:
290         unlock_kernel();
291         return ret;
292 }
293
294 void do_syscall_trace(void)
295 {
296         if (!test_thread_flag(TIF_SYSCALL_TRACE))
297                 return;
298         
299         if (!(current->ptrace & PT_PTRACED))
300                 return;
301         
302         /* the 0x80 provides a way for the tracing parent to distinguish
303            between a syscall stop and SIGTRAP delivery */
304         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
305                                  ? 0x80 : 0));
306         
307         /*
308          * This isn't the same as continuing with a signal, but it will do for
309          * normal use.
310          */
311         if (current->exit_code) {
312                 send_sig(current->exit_code, current, 1);
313                 current->exit_code = 0;
314         }
315 }