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