4f3df6d6795c178adadcecea4aa4a0fda8b1251a
[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
128         ret = -EPERM;
129         if (pid == 1)           /* you may not mess with init */
130                 goto out_tsk;
131
132         if (request == PTRACE_ATTACH) {
133                 ret = ptrace_attach(child);
134                 goto out_tsk;
135         }
136         ret = -ESRCH;
137         if (!(child->ptrace & PT_PTRACED))
138                 goto out_tsk;
139         if (child->state != TASK_STOPPED) {
140                 if (request != PTRACE_KILL)
141                         goto out_tsk;
142         }
143         ret = ptrace_check_attach(child, request == PTRACE_KILL);
144         if (ret < 0)
145                 goto out_tsk;
146
147         switch (request) {
148                 /* when I and D space are separate, these will need to be fixed. */
149                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
150                 case PTRACE_PEEKDATA: {
151                         unsigned long tmp;
152                         int copied;
153
154                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
155                         ret = -EIO;
156                         if (copied != sizeof(tmp))
157                                 break;
158                         ret = put_user(tmp,(unsigned long *) data);
159                         break;
160                 }
161
162                 /* read the word at location addr in the USER area. */
163                 case PTRACE_PEEKUSR: {
164                         unsigned long tmp;
165                         
166                         ret = -EIO;
167                         if ((addr & 3) || addr < 0 ||
168                             addr > sizeof(struct user) - 3)
169                                 break;
170                         
171                         tmp = 0;  /* Default return condition */
172                         addr = addr >> 2; /* temporary hack. */
173                         ret = -EIO;
174                         if (addr < 19) {
175                                 tmp = get_reg(child, addr);
176                                 if (addr == PT_SR)
177                                         tmp >>= 16;
178                         } else if (addr >= 21 && addr < 49) {
179                                 tmp = child->thread.fp[addr - 21];
180 #ifdef CONFIG_M68KFPU_EMU
181                                 /* Convert internal fpu reg representation
182                                  * into long double format
183                                  */
184                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
185                                         tmp = ((tmp & 0xffff0000) << 15) |
186                                               ((tmp & 0x0000ffff) << 16);
187 #endif
188                         } else if (addr == 49) {
189                                 tmp = child->mm->start_code;
190                         } else if (addr == 50) {
191                                 tmp = child->mm->start_data;
192                         } else if (addr == 51) {
193                                 tmp = child->mm->end_code;
194                         } else
195                                 break;
196                         ret = put_user(tmp,(unsigned long *) data);
197                         break;
198                 }
199
200                 /* when I and D space are separate, this will have to be fixed. */
201                 case PTRACE_POKETEXT: /* write the word at location addr. */
202                 case PTRACE_POKEDATA:
203                         ret = 0;
204                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
205                                 break;
206                         ret = -EIO;
207                         break;
208
209                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
210                         ret = -EIO;
211                         if ((addr & 3) || addr < 0 ||
212                             addr > sizeof(struct user) - 3)
213                                 break;
214
215                         addr = addr >> 2; /* temporary hack. */
216                             
217                         if (addr == PT_SR) {
218                                 data &= SR_MASK;
219                                 data <<= 16;
220                                 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
221                         }
222                         if (addr < 19) {
223                                 if (put_reg(child, addr, data))
224                                         break;
225                                 ret = 0;
226                                 break;
227                         }
228                         if (addr >= 21 && addr < 48)
229                         {
230 #ifdef CONFIG_M68KFPU_EMU
231                                 /* Convert long double format
232                                  * into internal fpu reg representation
233                                  */
234                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
235                                         data = (unsigned long)data << 15;
236                                         data = (data & 0xffff0000) |
237                                                ((data & 0x0000ffff) >> 1);
238                                 }
239 #endif
240                                 child->thread.fp[addr - 21] = data;
241                                 ret = 0;
242                         }
243                         break;
244
245                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
246                 case PTRACE_CONT: { /* restart after signal. */
247                         long tmp;
248
249                         ret = -EIO;
250                         if ((unsigned long) data > _NSIG)
251                                 break;
252                         if (request == PTRACE_SYSCALL)
253                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
254                         else
255                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
256                         child->exit_code = data;
257                         /* make sure the single step bit is not set. */
258                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
259                         put_reg(child, PT_SR, tmp);
260                         wake_up_process(child);
261                         ret = 0;
262                         break;
263                 }
264
265                 /*
266                  * make the child exit.  Best I can do is send it a sigkill. 
267                  * perhaps it should be put in the status that it wants to 
268                  * exit.
269                  */
270                 case PTRACE_KILL: {
271                         long tmp;
272
273                         ret = 0;
274                         if (child->state == TASK_ZOMBIE) /* already dead */
275                                 break;
276                         child->exit_code = SIGKILL;
277                         /* make sure the single step bit is not set. */
278                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
279                         put_reg(child, PT_SR, tmp);
280                         wake_up_process(child);
281                         break;
282                 }
283
284                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
285                         long tmp;
286
287                         ret = -EIO;
288                         if ((unsigned long) data > _NSIG)
289                                 break;
290                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
291                         tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
292                         put_reg(child, PT_SR, tmp);
293
294                         child->exit_code = data;
295                         /* give it a chance to run. */
296                         wake_up_process(child);
297                         ret = 0;
298                         break;
299                 }
300
301                 case PTRACE_DETACH:     /* detach a process that was attached. */
302                         ret = ptrace_detach(child, data);
303                         break;
304
305                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
306                         int i;
307                         unsigned long tmp;
308                         for (i = 0; i < 19; i++) {
309                             tmp = get_reg(child, i);
310                             if (i == PT_SR)
311                                 tmp >>= 16;
312                             if (put_user(tmp, (unsigned long *) data)) {
313                                 ret = -EFAULT;
314                                 break;
315                             }
316                             data += sizeof(long);
317                         }
318                         ret = 0;
319                         break;
320                 }
321
322                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
323                         int i;
324                         unsigned long tmp;
325                         for (i = 0; i < 19; i++) {
326                             if (get_user(tmp, (unsigned long *) data)) {
327                                 ret = -EFAULT;
328                                 break;
329                             }
330                             if (i == PT_SR) {
331                                 tmp &= SR_MASK;
332                                 tmp <<= 16;
333                                 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
334                             }
335                             put_reg(child, i, tmp);
336                             data += sizeof(long);
337                         }
338                         ret = 0;
339                         break;
340                 }
341
342 #ifdef PTRACE_GETFPREGS
343                 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
344                         ret = 0;
345                         if (copy_to_user((void *)data, &child->thread.fp,
346                                          sizeof(struct user_m68kfp_struct)))
347                                 ret = -EFAULT;
348                         break;
349                 }
350 #endif
351
352 #ifdef PTRACE_SETFPREGS
353                 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
354                         ret = 0;
355                         if (copy_from_user(&child->thread.fp, (void *)data,
356                                            sizeof(struct user_m68kfp_struct)))
357                                 ret = -EFAULT;
358                         break;
359                 }
360 #endif
361
362                 default:
363                         ret = -EIO;
364                         break;
365         }
366 out_tsk:
367         put_task_struct(child);
368 out:
369         unlock_kernel();
370         return ret;
371 }
372
373 asmlinkage void syscall_trace(void)
374 {
375         if (!test_thread_flag(TIF_SYSCALL_TRACE))
376                 return;
377         if (!(current->ptrace & PT_PTRACED))
378                 return;
379         current->exit_code = SIGTRAP;
380         current->state = TASK_STOPPED;
381         notify_parent(current, SIGCHLD);
382         schedule();
383         /*
384          * this isn't the same as continuing with a signal, but it will do
385          * for normal use.  strace only continues with a signal if the
386          * stopping signal is not SIGTRAP.  -brl
387          */
388         if (current->exit_code) {
389                 send_sig(current->exit_code, current, 1);
390                 current->exit_code = 0;
391         }
392 }