vserver 2.0 rc7
[linux-2.6.git] / arch / m68knommu / kernel / ptrace.c
1 /*
2  *  linux/arch/m68knommu/kernel/ptrace.c
3  *
4  *  Copyright (C) 1994 by Hamish Macdonald
5  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
6  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License.  See the file COPYING in the main directory of
10  * this archive for more details.
11  */
12
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/config.h>
22 #include <linux/signal.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/page.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
29
30 /*
31  * does not yet catch signals sent when the child dies.
32  * in exit.c or in signal.c.
33  */
34
35 /* determines which bits in the SR the user has access to. */
36 /* 1 = access 0 = no access */
37 #define SR_MASK 0x001f
38
39 /* sets the trace bits. */
40 #define TRACE_BITS 0x8000
41
42 /* Find the stack offset for a register, relative to thread.esp0. */
43 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
44 #define SW_REG(reg)     ((long)&((struct switch_stack *)0)->reg \
45                          - sizeof(struct switch_stack))
46 /* Mapping from PT_xxx to the stack offset at which the register is
47    saved.  Notice that usp has no stack-slot and needs to be treated
48    specially (see get_reg/put_reg below). */
49 static int regoff[] = {
50         PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
51         PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
52         PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
53         SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
54         PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
55 };
56
57 /*
58  * Get contents of register REGNO in task TASK.
59  */
60 static inline long get_reg(struct task_struct *task, int regno)
61 {
62         unsigned long *addr;
63
64         if (regno == PT_USP)
65                 addr = &task->thread.usp;
66         else if (regno < sizeof(regoff)/sizeof(regoff[0]))
67                 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
68         else
69                 return 0;
70         return *addr;
71 }
72
73 /*
74  * Write contents of register REGNO in task TASK.
75  */
76 static inline int put_reg(struct task_struct *task, int regno,
77                           unsigned long data)
78 {
79         unsigned long *addr;
80
81         if (regno == PT_USP)
82                 addr = &task->thread.usp;
83         else if (regno < sizeof(regoff)/sizeof(regoff[0]))
84                 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
85         else
86                 return -1;
87         *addr = data;
88         return 0;
89 }
90
91 /*
92  * Called by kernel/ptrace.c when detaching..
93  *
94  * Make sure the single step bit is not set.
95  */
96 void ptrace_disable(struct task_struct *child)
97 {
98         unsigned long tmp;
99         /* make sure the single step bit is not set. */
100         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
101         put_reg(child, PT_SR, tmp);
102 }
103
104 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
105 {
106         struct task_struct *child;
107         int ret;
108
109         lock_kernel();
110         ret = -EPERM;
111         if (request == PTRACE_TRACEME) {
112                 /* are we already being traced? */
113                 if (current->ptrace & PT_PTRACED)
114                         goto out;
115                 /* set the ptrace bit in the process flags. */
116                 current->ptrace |= PT_PTRACED;
117                 ret = 0;
118                 goto out;
119         }
120         ret = -ESRCH;
121         read_lock(&tasklist_lock);
122         child = find_task_by_pid(pid);
123         if (child)
124                 get_task_struct(child);
125         read_unlock(&tasklist_lock);
126         if (!child)
127                 goto out;
128         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
129                 goto out_tsk;
130
131         ret = -EPERM;
132         if (pid == 1)           /* you may not mess with init */
133                 goto out_tsk;
134
135         if (request == PTRACE_ATTACH) {
136                 ret = ptrace_attach(child);
137                 goto out_tsk;
138         }
139         ret = ptrace_check_attach(child, request == PTRACE_KILL);
140         if (ret < 0)
141                 goto out_tsk;
142
143         switch (request) {
144                 /* when I and D space are separate, these will need to be fixed. */
145                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
146                 case PTRACE_PEEKDATA: {
147                         unsigned long tmp;
148                         int copied;
149
150                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
151                         ret = -EIO;
152                         if (copied != sizeof(tmp))
153                                 break;
154                         ret = put_user(tmp,(unsigned long *) data);
155                         break;
156                 }
157
158                 /* read the word at location addr in the USER area. */
159                 case PTRACE_PEEKUSR: {
160                         unsigned long tmp;
161                         
162                         ret = -EIO;
163                         if ((addr & 3) || addr < 0 ||
164                             addr > sizeof(struct user) - 3)
165                                 break;
166                         
167                         tmp = 0;  /* Default return condition */
168                         addr = addr >> 2; /* temporary hack. */
169                         ret = -EIO;
170                         if (addr < 19) {
171                                 tmp = get_reg(child, addr);
172                                 if (addr == PT_SR)
173                                         tmp >>= 16;
174                         } else if (addr >= 21 && addr < 49) {
175                                 tmp = child->thread.fp[addr - 21];
176 #ifdef CONFIG_M68KFPU_EMU
177                                 /* Convert internal fpu reg representation
178                                  * into long double format
179                                  */
180                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
181                                         tmp = ((tmp & 0xffff0000) << 15) |
182                                               ((tmp & 0x0000ffff) << 16);
183 #endif
184                         } else if (addr == 49) {
185                                 tmp = child->mm->start_code;
186                         } else if (addr == 50) {
187                                 tmp = child->mm->start_data;
188                         } else if (addr == 51) {
189                                 tmp = child->mm->end_code;
190                         } else
191                                 break;
192                         ret = put_user(tmp,(unsigned long *) data);
193                         break;
194                 }
195
196                 /* when I and D space are separate, this will have to be fixed. */
197                 case PTRACE_POKETEXT: /* write the word at location addr. */
198                 case PTRACE_POKEDATA:
199                         ret = 0;
200                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
201                                 break;
202                         ret = -EIO;
203                         break;
204
205                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
206                         ret = -EIO;
207                         if ((addr & 3) || addr < 0 ||
208                             addr > sizeof(struct user) - 3)
209                                 break;
210
211                         addr = addr >> 2; /* temporary hack. */
212                             
213                         if (addr == PT_SR) {
214                                 data &= SR_MASK;
215                                 data <<= 16;
216                                 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
217                         }
218                         if (addr < 19) {
219                                 if (put_reg(child, addr, data))
220                                         break;
221                                 ret = 0;
222                                 break;
223                         }
224                         if (addr >= 21 && addr < 48)
225                         {
226 #ifdef CONFIG_M68KFPU_EMU
227                                 /* Convert long double format
228                                  * into internal fpu reg representation
229                                  */
230                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
231                                         data = (unsigned long)data << 15;
232                                         data = (data & 0xffff0000) |
233                                                ((data & 0x0000ffff) >> 1);
234                                 }
235 #endif
236                                 child->thread.fp[addr - 21] = data;
237                                 ret = 0;
238                         }
239                         break;
240
241                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
242                 case PTRACE_CONT: { /* restart after signal. */
243                         long tmp;
244
245                         ret = -EIO;
246                         if (!valid_signal(data))
247                                 break;
248                         if (request == PTRACE_SYSCALL)
249                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
250                         else
251                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
252                         child->exit_code = data;
253                         /* make sure the single step bit is not set. */
254                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
255                         put_reg(child, PT_SR, tmp);
256                         wake_up_process(child);
257                         ret = 0;
258                         break;
259                 }
260
261                 /*
262                  * make the child exit.  Best I can do is send it a sigkill. 
263                  * perhaps it should be put in the status that it wants to 
264                  * exit.
265                  */
266                 case PTRACE_KILL: {
267                         long tmp;
268
269                         ret = 0;
270                         if (child->exit_state == EXIT_ZOMBIE) /* already dead */
271                                 break;
272                         child->exit_code = SIGKILL;
273                         /* make sure the single step bit is not set. */
274                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
275                         put_reg(child, PT_SR, tmp);
276                         wake_up_process(child);
277                         break;
278                 }
279
280                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
281                         long tmp;
282
283                         ret = -EIO;
284                         if (!valid_signal(data))
285                                 break;
286                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
287                         tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
288                         put_reg(child, PT_SR, tmp);
289
290                         child->exit_code = data;
291                         /* give it a chance to run. */
292                         wake_up_process(child);
293                         ret = 0;
294                         break;
295                 }
296
297                 case PTRACE_DETACH:     /* detach a process that was attached. */
298                         ret = ptrace_detach(child, data);
299                         break;
300
301                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
302                         int i;
303                         unsigned long tmp;
304                         for (i = 0; i < 19; i++) {
305                             tmp = get_reg(child, i);
306                             if (i == PT_SR)
307                                 tmp >>= 16;
308                             if (put_user(tmp, (unsigned long *) data)) {
309                                 ret = -EFAULT;
310                                 break;
311                             }
312                             data += sizeof(long);
313                         }
314                         ret = 0;
315                         break;
316                 }
317
318                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
319                         int i;
320                         unsigned long tmp;
321                         for (i = 0; i < 19; i++) {
322                             if (get_user(tmp, (unsigned long *) data)) {
323                                 ret = -EFAULT;
324                                 break;
325                             }
326                             if (i == PT_SR) {
327                                 tmp &= SR_MASK;
328                                 tmp <<= 16;
329                                 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
330                             }
331                             put_reg(child, i, tmp);
332                             data += sizeof(long);
333                         }
334                         ret = 0;
335                         break;
336                 }
337
338 #ifdef PTRACE_GETFPREGS
339                 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
340                         ret = 0;
341                         if (copy_to_user((void *)data, &child->thread.fp,
342                                          sizeof(struct user_m68kfp_struct)))
343                                 ret = -EFAULT;
344                         break;
345                 }
346 #endif
347
348 #ifdef PTRACE_SETFPREGS
349                 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
350                         ret = 0;
351                         if (copy_from_user(&child->thread.fp, (void *)data,
352                                            sizeof(struct user_m68kfp_struct)))
353                                 ret = -EFAULT;
354                         break;
355                 }
356 #endif
357
358                 default:
359                         ret = -EIO;
360                         break;
361         }
362 out_tsk:
363         put_task_struct(child);
364 out:
365         unlock_kernel();
366         return ret;
367 }
368
369 asmlinkage void syscall_trace(void)
370 {
371         if (!test_thread_flag(TIF_SYSCALL_TRACE))
372                 return;
373         if (!(current->ptrace & PT_PTRACED))
374                 return;
375         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
376                                  ? 0x80 : 0));
377         /*
378          * this isn't the same as continuing with a signal, but it will do
379          * for normal use.  strace only continues with a signal if the
380          * stopping signal is not SIGTRAP.  -brl
381          */
382         if (current->exit_code) {
383                 send_sig(current->exit_code, current, 1);
384                 current->exit_code = 0;
385         }
386 }