Import changeset
[linux-flexiantxendom0-3.2.10.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         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 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
91 {
92         struct task_struct *child;
93         unsigned long flags;
94         int ret;
95
96         lock_kernel();
97         ret = -EPERM;
98         if (request == PTRACE_TRACEME) {
99                 /* are we already being traced? */
100                 if (current->ptrace & PT_PTRACED)
101                         goto out;
102                 /* set the ptrace bit in the process flags. */
103                 current->ptrace |= PT_PTRACED;
104                 ret = 0;
105                 goto out;
106         }
107         ret = -ESRCH;
108         read_lock(&tasklist_lock);
109         child = find_task_by_pid(pid);
110         read_unlock(&tasklist_lock);    /* FIXME!!! */
111         if (!child)
112                 goto out;
113         ret = -EPERM;
114         if (pid == 1)           /* you may not mess with init */
115                 goto out;
116         if (request == PTRACE_ATTACH) {
117                 if (child == current)
118                         goto out;
119                 if ((!child->dumpable ||
120                     (current->uid != child->euid) ||
121                     (current->uid != child->suid) ||
122                     (current->uid != child->uid) ||
123                     (current->gid != child->egid) ||
124                     (current->gid != child->sgid) ||
125                     (!cap_issubset(child->cap_permitted, current->cap_permitted)) ||
126                     (current->gid != child->gid)) && !capable(CAP_SYS_PTRACE))
127                         goto out;
128                 /* the same process cannot be attached many times */
129                 if (child->ptrace & PT_PTRACED)
130                         goto out;
131                 child->ptrace |= PT_PTRACED;
132
133                 write_lock_irqsave(&tasklist_lock, flags);
134                 if (child->p_pptr != current) {
135                         REMOVE_LINKS(child);
136                         child->p_pptr = current;
137                         SET_LINKS(child);
138                 }
139                 write_unlock_irqrestore(&tasklist_lock, flags);
140
141                 send_sig(SIGSTOP, child, 1);
142                 ret = 0;
143                 goto out;
144         }
145         ret = -ESRCH;
146         if (!(child->ptrace & PT_PTRACED))
147                 goto out;
148         if (child->state != TASK_STOPPED) {
149                 if (request != PTRACE_KILL)
150                         goto out;
151         }
152         if (child->p_pptr != current)
153                 goto out;
154
155         switch (request) {
156         /* when I and D space are separate, these will need to be fixed. */
157                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
158                 case PTRACE_PEEKDATA: {
159                         unsigned long tmp;
160                         int copied;
161
162                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
163                         ret = -EIO;
164                         if (copied != sizeof(tmp))
165                                 goto out;
166                         ret = put_user(tmp,(unsigned long *) data);
167                         goto out;
168                 }
169
170         /* read the word at location addr in the USER area. */
171                 case PTRACE_PEEKUSR: {
172                         unsigned long tmp;
173                         
174                         ret = -EIO;
175                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
176                                 goto out;
177                         
178                         tmp = 0;  /* Default return condition */
179                         addr = addr >> 2; /* temporary hack. */
180                         ret = -EIO;
181                         if (addr < 19) {
182                                 tmp = get_reg(child, addr);
183                                 if (addr == PT_SR)
184                                         tmp >>= 16;
185                         } else if (addr >= 21 && addr < 49) {
186                                 tmp = child->thread.fp[addr - 21];
187 #ifdef CONFIG_M68KFPU_EMU
188                                 /* Convert internal fpu reg representation
189                                  * into long double format
190                                  */
191                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
192                                         tmp = ((tmp & 0xffff0000) << 15) |
193                                               ((tmp & 0x0000ffff) << 16);
194 #endif
195                         } else
196                                 goto out;
197                         ret = put_user(tmp,(unsigned long *) data);
198                         goto out;
199                 }
200
201       /* when I and D space are separate, this will have to be fixed. */
202                 case PTRACE_POKETEXT: /* write the word at location addr. */
203                 case PTRACE_POKEDATA:
204                         ret = 0;
205                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
206                                 goto out;
207                         ret = -EIO;
208                         goto out;
209
210                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
211                         ret = -EIO;
212                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
213                                 goto out;
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                                         goto out;
225                                 ret = 0;
226                                 goto out;
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                         goto out;
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                                 goto out;
252                         if (request == PTRACE_SYSCALL)
253                                 child->ptrace |= PT_TRACESYS;
254                         else
255                                 child->ptrace &= ~PT_TRACESYS;
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                         goto out;
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                                 goto out;
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                         goto out;
282                 }
283
284                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
285                         long tmp;
286
287                         ret = -EIO;
288                         if ((unsigned long) data > _NSIG)
289                                 goto out;
290                         child->ptrace &= ~PT_TRACESYS;
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                         goto out;
299                 }
300
301                 case PTRACE_DETACH: { /* detach a process that was attached. */
302                         long tmp;
303
304                         ret = -EIO;
305                         if ((unsigned long) data > _NSIG)
306                                 goto out;
307                         child->ptrace &= ~(PT_PTRACED|PT_TRACESYS);
308                         child->exit_code = data;
309                         write_lock_irqsave(&tasklist_lock, flags);
310                         REMOVE_LINKS(child);
311                         child->p_pptr = child->p_opptr;
312                         SET_LINKS(child);
313                         write_unlock_irqrestore(&tasklist_lock, flags);
314                         /* make sure the single step bit is not set. */
315                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
316                         put_reg(child, PT_SR, tmp);
317                         wake_up_process(child);
318                         ret = 0;
319                         goto out;
320                 }
321
322                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
323                         int i;
324                         unsigned long tmp;
325                         for (i = 0; i < 19; i++) {
326                             tmp = get_reg(child, i);
327                             if (i == PT_SR)
328                                 tmp >>= 16;
329                             if (put_user(tmp, (unsigned long *) data)) {
330                                 ret = -EFAULT;
331                                 goto out;
332                             }
333                             data += sizeof(long);
334                         }
335                         ret = 0;
336                         goto out;
337                 }
338
339                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
340                         int i;
341                         unsigned long tmp;
342                         for (i = 0; i < 19; i++) {
343                             if (get_user(tmp, (unsigned long *) data)) {
344                                 ret = -EFAULT;
345                                 goto out;
346                             }
347                             if (i == PT_SR) {
348                                 tmp &= SR_MASK;
349                                 tmp <<= 16;
350                                 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
351                             }
352                             put_reg(child, i, tmp);
353                             data += sizeof(long);
354                         }
355                         ret = 0;
356                         goto out;
357                 }
358
359                 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
360                         ret = 0;
361                         if (copy_to_user((void *)data, &child->thread.fp,
362                                          sizeof(struct user_m68kfp_struct)))
363                                 ret = -EFAULT;
364                         goto out;
365                 }
366
367                 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
368                         ret = 0;
369                         if (copy_from_user(&child->thread.fp, (void *)data,
370                                            sizeof(struct user_m68kfp_struct)))
371                                 ret = -EFAULT;
372                         goto out;
373                 }
374
375                 default:
376                         ret = -EIO;
377                         goto out;
378         }
379 out:
380         unlock_kernel();
381         return ret;
382 }
383
384 asmlinkage void syscall_trace(void)
385 {
386         lock_kernel();
387         if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
388                         != (PT_PTRACED|PT_TRACESYS))
389                 goto out;
390         current->exit_code = SIGTRAP;
391         current->state = TASK_STOPPED;
392         notify_parent(current, SIGCHLD);
393         schedule();
394         /*
395          * this isn't the same as continuing with a signal, but it will do
396          * for normal use.  strace only continues with a signal if the
397          * stopping signal is not SIGTRAP.  -brl
398          */
399         if (current->exit_code) {
400                 send_sig(current->exit_code, current, 1);
401                 current->exit_code = 0;
402         }
403 out:
404         unlock_kernel();
405 }