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