Merge branch 'drm-forlinus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.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 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
105 {
106         int ret;
107
108         switch (request) {
109                 /* when I and D space are separate, these will need to be fixed. */
110                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
111                 case PTRACE_PEEKDATA: {
112                         unsigned long tmp;
113                         int copied;
114
115                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
116                         ret = -EIO;
117                         if (copied != sizeof(tmp))
118                                 break;
119                         ret = put_user(tmp,(unsigned long *) data);
120                         break;
121                 }
122
123                 /* read the word at location addr in the USER area. */
124                 case PTRACE_PEEKUSR: {
125                         unsigned long tmp;
126                         
127                         ret = -EIO;
128                         if ((addr & 3) || addr < 0 ||
129                             addr > sizeof(struct user) - 3)
130                                 break;
131                         
132                         tmp = 0;  /* Default return condition */
133                         addr = addr >> 2; /* temporary hack. */
134                         ret = -EIO;
135                         if (addr < 19) {
136                                 tmp = get_reg(child, addr);
137                                 if (addr == PT_SR)
138                                         tmp >>= 16;
139                         } else if (addr >= 21 && addr < 49) {
140                                 tmp = child->thread.fp[addr - 21];
141 #ifdef CONFIG_M68KFPU_EMU
142                                 /* Convert internal fpu reg representation
143                                  * into long double format
144                                  */
145                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
146                                         tmp = ((tmp & 0xffff0000) << 15) |
147                                               ((tmp & 0x0000ffff) << 16);
148 #endif
149                         } else if (addr == 49) {
150                                 tmp = child->mm->start_code;
151                         } else if (addr == 50) {
152                                 tmp = child->mm->start_data;
153                         } else if (addr == 51) {
154                                 tmp = child->mm->end_code;
155                         } else
156                                 break;
157                         ret = put_user(tmp,(unsigned long *) data);
158                         break;
159                 }
160
161                 /* when I and D space are separate, this will have to be fixed. */
162                 case PTRACE_POKETEXT: /* write the word at location addr. */
163                 case PTRACE_POKEDATA:
164                         ret = 0;
165                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
166                                 break;
167                         ret = -EIO;
168                         break;
169
170                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
171                         ret = -EIO;
172                         if ((addr & 3) || addr < 0 ||
173                             addr > sizeof(struct user) - 3)
174                                 break;
175
176                         addr = addr >> 2; /* temporary hack. */
177                             
178                         if (addr == PT_SR) {
179                                 data &= SR_MASK;
180                                 data <<= 16;
181                                 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
182                         }
183                         if (addr < 19) {
184                                 if (put_reg(child, addr, data))
185                                         break;
186                                 ret = 0;
187                                 break;
188                         }
189                         if (addr >= 21 && addr < 48)
190                         {
191 #ifdef CONFIG_M68KFPU_EMU
192                                 /* Convert long double format
193                                  * into internal fpu reg representation
194                                  */
195                                 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
196                                         data = (unsigned long)data << 15;
197                                         data = (data & 0xffff0000) |
198                                                ((data & 0x0000ffff) >> 1);
199                                 }
200 #endif
201                                 child->thread.fp[addr - 21] = data;
202                                 ret = 0;
203                         }
204                         break;
205
206                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
207                 case PTRACE_CONT: { /* restart after signal. */
208                         long tmp;
209
210                         ret = -EIO;
211                         if (!valid_signal(data))
212                                 break;
213                         if (request == PTRACE_SYSCALL)
214                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
215                         else
216                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
217                         child->exit_code = data;
218                         /* make sure the single step bit is not set. */
219                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
220                         put_reg(child, PT_SR, tmp);
221                         wake_up_process(child);
222                         ret = 0;
223                         break;
224                 }
225
226                 /*
227                  * make the child exit.  Best I can do is send it a sigkill. 
228                  * perhaps it should be put in the status that it wants to 
229                  * exit.
230                  */
231                 case PTRACE_KILL: {
232                         long tmp;
233
234                         ret = 0;
235                         if (child->exit_state == EXIT_ZOMBIE) /* already dead */
236                                 break;
237                         child->exit_code = SIGKILL;
238                         /* make sure the single step bit is not set. */
239                         tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
240                         put_reg(child, PT_SR, tmp);
241                         wake_up_process(child);
242                         break;
243                 }
244
245                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
246                         long tmp;
247
248                         ret = -EIO;
249                         if (!valid_signal(data))
250                                 break;
251                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
252                         tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
253                         put_reg(child, PT_SR, tmp);
254
255                         child->exit_code = data;
256                         /* give it a chance to run. */
257                         wake_up_process(child);
258                         ret = 0;
259                         break;
260                 }
261
262                 case PTRACE_DETACH:     /* detach a process that was attached. */
263                         ret = ptrace_detach(child, data);
264                         break;
265
266                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
267                         int i;
268                         unsigned long tmp;
269                         for (i = 0; i < 19; i++) {
270                             tmp = get_reg(child, i);
271                             if (i == PT_SR)
272                                 tmp >>= 16;
273                             if (put_user(tmp, (unsigned long *) data)) {
274                                 ret = -EFAULT;
275                                 break;
276                             }
277                             data += sizeof(long);
278                         }
279                         ret = 0;
280                         break;
281                 }
282
283                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
284                         int i;
285                         unsigned long tmp;
286                         for (i = 0; i < 19; i++) {
287                             if (get_user(tmp, (unsigned long *) data)) {
288                                 ret = -EFAULT;
289                                 break;
290                             }
291                             if (i == PT_SR) {
292                                 tmp &= SR_MASK;
293                                 tmp <<= 16;
294                                 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
295                             }
296                             put_reg(child, i, tmp);
297                             data += sizeof(long);
298                         }
299                         ret = 0;
300                         break;
301                 }
302
303 #ifdef PTRACE_GETFPREGS
304                 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
305                         ret = 0;
306                         if (copy_to_user((void *)data, &child->thread.fp,
307                                          sizeof(struct user_m68kfp_struct)))
308                                 ret = -EFAULT;
309                         break;
310                 }
311 #endif
312
313 #ifdef PTRACE_SETFPREGS
314                 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
315                         ret = 0;
316                         if (copy_from_user(&child->thread.fp, (void *)data,
317                                            sizeof(struct user_m68kfp_struct)))
318                                 ret = -EFAULT;
319                         break;
320                 }
321 #endif
322
323                 default:
324                         ret = -EIO;
325                         break;
326         }
327         return ret;
328 }
329
330 asmlinkage void syscall_trace(void)
331 {
332         if (!test_thread_flag(TIF_SYSCALL_TRACE))
333                 return;
334         if (!(current->ptrace & PT_PTRACED))
335                 return;
336         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
337                                  ? 0x80 : 0));
338         /*
339          * this isn't the same as continuing with a signal, but it will do
340          * for normal use.  strace only continues with a signal if the
341          * stopping signal is not SIGTRAP.  -brl
342          */
343         if (current->exit_code) {
344                 send_sig(current->exit_code, current, 1);
345                 current->exit_code = 0;
346         }
347 }