Merge branch 'for-linus' of git://neil.brown.name/md
[sfrench/cifs-2.6.git] / arch / blackfin / kernel / ptrace.c
1 /*
2  * File:         arch/blackfin/kernel/ptrace.c
3  * Based on:     Taken from linux/kernel/ptrace.c
4  * Author:       linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
5  *
6  * Created:      1/23/92
7  * Description:
8  *
9  * Modified:
10  *               Copyright 2004-2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/mm.h>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/errno.h>
36 #include <linux/ptrace.h>
37 #include <linux/user.h>
38 #include <linux/signal.h>
39 #include <linux/uaccess.h>
40
41 #include <asm/page.h>
42 #include <asm/pgtable.h>
43 #include <asm/system.h>
44 #include <asm/processor.h>
45 #include <asm/asm-offsets.h>
46 #include <asm/dma.h>
47 #include <asm/fixed_code.h>
48
49 #define MAX_SHARED_LIBS 3
50 #define TEXT_OFFSET 0
51 /*
52  * does not yet catch signals sent when the child dies.
53  * in exit.c or in signal.c.
54  */
55
56 /* determines which bits in the SYSCFG reg the user has access to. */
57 /* 1 = access 0 = no access */
58 #define SYSCFG_MASK 0x0007      /* SYSCFG reg */
59 /* sets the trace bits. */
60 #define TRACE_BITS 0x0001
61
62 /* Find the stack offset for a register, relative to thread.esp0. */
63 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
64
65 /*
66  * Get the address of the live pt_regs for the specified task.
67  * These are saved onto the top kernel stack when the process
68  * is not running.
69  *
70  * Note: if a user thread is execve'd from kernel space, the
71  * kernel stack will not be empty on entry to the kernel, so
72  * ptracing these tasks will fail.
73  */
74 static inline struct pt_regs *get_user_regs(struct task_struct *task)
75 {
76         return (struct pt_regs *)
77             ((unsigned long)task_stack_page(task) +
78              (THREAD_SIZE - sizeof(struct pt_regs)));
79 }
80
81 /*
82  * Get all user integer registers.
83  */
84 static inline int ptrace_getregs(struct task_struct *tsk, void __user * uregs)
85 {
86         struct pt_regs *regs = get_user_regs(tsk);
87         return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
88 }
89
90 /* Mapping from PT_xxx to the stack offset at which the register is
91  * saved.  Notice that usp has no stack-slot and needs to be treated
92  * specially (see get_reg/put_reg below).
93  */
94
95 /*
96  * Get contents of register REGNO in task TASK.
97  */
98 static inline long get_reg(struct task_struct *task, int regno)
99 {
100         unsigned char *reg_ptr;
101
102         struct pt_regs *regs =
103             (struct pt_regs *)((unsigned long)task_stack_page(task) +
104                                (THREAD_SIZE - sizeof(struct pt_regs)));
105         reg_ptr = (char *)regs;
106
107         switch (regno) {
108         case PT_USP:
109                 return task->thread.usp;
110         default:
111                 if (regno <= 216)
112                         return *(long *)(reg_ptr + regno);
113         }
114         /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
115
116         printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
117         return 0;
118 }
119
120 /*
121  * Write contents of register REGNO in task TASK.
122  */
123 static inline int
124 put_reg(struct task_struct *task, int regno, unsigned long data)
125 {
126         char *reg_ptr;
127
128         struct pt_regs *regs =
129             (struct pt_regs *)((unsigned long)task_stack_page(task) +
130                                (THREAD_SIZE - sizeof(struct pt_regs)));
131         reg_ptr = (char *)regs;
132
133         switch (regno) {
134         case PT_PC:
135                 /*********************************************************************/
136                 /* At this point the kernel is most likely in exception.             */
137                 /* The RETX register will be used to populate the pc of the process. */
138                 /*********************************************************************/
139                 regs->retx = data;
140                 regs->pc = data;
141                 break;
142         case PT_RETX:
143                 break;          /* regs->retx = data; break; */
144         case PT_USP:
145                 regs->usp = data;
146                 task->thread.usp = data;
147                 break;
148         default:
149                 if (regno <= 216)
150                         *(long *)(reg_ptr + regno) = data;
151         }
152         return 0;
153 }
154
155 /*
156  * check that an address falls within the bounds of the target process's memory mappings
157  */
158 static inline int is_user_addr_valid(struct task_struct *child,
159                                      unsigned long start, unsigned long len)
160 {
161         struct vm_list_struct *vml;
162         struct sram_list_struct *sraml;
163
164         for (vml = child->mm->context.vmlist; vml; vml = vml->next)
165                 if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end)
166                         return 0;
167
168         for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
169                 if (start >= (unsigned long)sraml->addr
170                     && start + len <= (unsigned long)sraml->addr + sraml->length)
171                         return 0;
172
173         if (start >= FIXED_CODE_START && start + len <= FIXED_CODE_END)
174                 return 0;
175
176         return -EIO;
177 }
178
179 /*
180  * Called by kernel/ptrace.c when detaching..
181  *
182  * Make sure the single step bit is not set.
183  */
184 void ptrace_disable(struct task_struct *child)
185 {
186         unsigned long tmp;
187         /* make sure the single step bit is not set. */
188         tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
189         put_reg(child, PT_SYSCFG, tmp);
190 }
191
192 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
193 {
194         int ret;
195         int add = 0;
196         unsigned long __user *datap = (unsigned long __user *)data;
197
198         switch (request) {
199                 /* when I and D space are separate, these will need to be fixed. */
200         case PTRACE_PEEKDATA:
201                 pr_debug("ptrace: PEEKDATA\n");
202                 add = MAX_SHARED_LIBS * 4;      /* space between text and data */
203                 /* fall through */
204         case PTRACE_PEEKTEXT:   /* read word at location addr. */
205                 {
206                         unsigned long tmp = 0;
207                         int copied;
208
209                         ret = -EIO;
210                         pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + add %d %ld\n", addr, add,
211                                  sizeof(data));
212                         if (is_user_addr_valid(child, addr + add, sizeof(tmp)) < 0)
213                                 break;
214                         pr_debug("ptrace: user address is valid\n");
215
216 #if L1_CODE_LENGTH != 0
217                         if (addr + add >= L1_CODE_START
218                             && addr + add + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) {
219                                 safe_dma_memcpy (&tmp, (const void *)(addr + add), sizeof(tmp));
220                                 copied = sizeof(tmp);
221                         } else
222 #endif
223 #if L1_DATA_A_LENGTH != 0
224                         if (addr + add >= L1_DATA_A_START
225                             && addr + add + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
226                                 memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
227                                 copied = sizeof(tmp);
228                         } else
229 #endif
230 #if L1_DATA_B_LENGTH != 0
231                         if (addr + add >= L1_DATA_B_START
232                             && addr + add + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
233                                 memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
234                                 copied = sizeof(tmp);
235                         } else
236 #endif
237                         if (addr + add >= FIXED_CODE_START
238                             && addr + add + sizeof(tmp) <= FIXED_CODE_END) {
239                                 memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
240                                 copied = sizeof(tmp);
241                         } else
242                                 copied = access_process_vm(child, addr + add, &tmp,
243                                                            sizeof(tmp), 0);
244                         pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
245                         if (copied != sizeof(tmp))
246                                 break;
247                         ret = put_user(tmp, datap);
248                         break;
249                 }
250
251                 /* read the word at location addr in the USER area. */
252         case PTRACE_PEEKUSR:
253                 {
254                         unsigned long tmp;
255                         ret = -EIO;
256                         tmp = 0;
257                         if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
258                                 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
259                                                     "0 - %x sizeof(pt_regs) is %lx\n",
260                                      (int)addr, sizeof(struct pt_regs));
261                                 break;
262                         }
263                         if (addr == sizeof(struct pt_regs)) {
264                                 /* PT_TEXT_ADDR */
265                                 tmp = child->mm->start_code + TEXT_OFFSET;
266                         } else if (addr == (sizeof(struct pt_regs) + 4)) {
267                                 /* PT_TEXT_END_ADDR */
268                                 tmp = child->mm->end_code;
269                         } else if (addr == (sizeof(struct pt_regs) + 8)) {
270                                 /* PT_DATA_ADDR */
271                                 tmp = child->mm->start_data;
272 #ifdef CONFIG_BINFMT_ELF_FDPIC
273                         } else if (addr == (sizeof(struct pt_regs) + 12)) {
274                                 tmp = child->mm->context.exec_fdpic_loadmap;
275                         } else if (addr == (sizeof(struct pt_regs) + 16)) {
276                                 tmp = child->mm->context.interp_fdpic_loadmap;
277 #endif
278                         } else {
279                                 tmp = get_reg(child, addr);
280                         }
281                         ret = put_user(tmp, datap);
282                         break;
283                 }
284
285                 /* when I and D space are separate, this will have to be fixed. */
286         case PTRACE_POKEDATA:
287                 printk(KERN_NOTICE "ptrace: PTRACE_PEEKDATA\n");
288                 /* fall through */
289         case PTRACE_POKETEXT:   /* write the word at location addr. */
290                 {
291                         int copied;
292
293                         ret = -EIO;
294                         pr_debug("ptrace: POKETEXT at addr 0x%08lx + add %d %ld bytes %lx\n",
295                                  addr, add, sizeof(data), data);
296                         if (is_user_addr_valid(child, addr + add, sizeof(data)) < 0)
297                                 break;
298                         pr_debug("ptrace: user address is valid\n");
299
300 #if L1_CODE_LENGTH != 0
301                         if (addr + add >= L1_CODE_START
302                             && addr + add + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) {
303                                 safe_dma_memcpy ((void *)(addr + add), &data, sizeof(data));
304                                 copied = sizeof(data);
305                         } else
306 #endif
307 #if L1_DATA_A_LENGTH != 0
308                         if (addr + add >= L1_DATA_A_START
309                             && addr + add + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
310                                 memcpy((void *)(addr + add), &data, sizeof(data));
311                                 copied = sizeof(data);
312                         } else
313 #endif
314 #if L1_DATA_B_LENGTH != 0
315                         if (addr + add >= L1_DATA_B_START
316                             && addr + add + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
317                                 memcpy((void *)(addr + add), &data, sizeof(data));
318                                 copied = sizeof(data);
319                         } else
320 #endif
321                         if (addr + add >= FIXED_CODE_START
322                             && addr + add + sizeof(data) <= FIXED_CODE_END) {
323                                 memcpy((void *)(addr + add), &data, sizeof(data));
324                                 copied = sizeof(data);
325                         } else
326                                 copied = access_process_vm(child, addr + add, &data,
327                                                            sizeof(data), 1);
328                         pr_debug("ptrace: copied size %d\n", copied);
329                         if (copied != sizeof(data))
330                                 break;
331                         ret = 0;
332                         break;
333                 }
334
335         case PTRACE_POKEUSR:    /* write the word at location addr in the USER area */
336                 ret = -EIO;
337                 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
338                         printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
339                         break;
340                 }
341
342                 if (addr >= (sizeof(struct pt_regs))) {
343                         ret = 0;
344                         break;
345                 }
346                 if (addr == PT_SYSCFG) {
347                         data &= SYSCFG_MASK;
348                         data |= get_reg(child, PT_SYSCFG);
349                 }
350                 ret = put_reg(child, addr, data);
351                 break;
352
353         case PTRACE_SYSCALL:    /* continue and stop at next (return from) syscall */
354         case PTRACE_CONT:
355                 {               /* restart after signal. */
356                         long tmp;
357
358                         pr_debug("ptrace_cont\n");
359
360                         ret = -EIO;
361                         if (!valid_signal(data))
362                                 break;
363                         if (request == PTRACE_SYSCALL)
364                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
365                         else
366                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
367
368                         child->exit_code = data;
369                         /* make sure the single step bit is not set. */
370                         tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
371                         put_reg(child, PT_SYSCFG, tmp);
372                         pr_debug("before wake_up_process\n");
373                         wake_up_process(child);
374                         ret = 0;
375                         break;
376                 }
377
378         /*
379          * make the child exit.  Best I can do is send it a sigkill.
380          * perhaps it should be put in the status that it wants to
381          * exit.
382          */
383         case PTRACE_KILL:
384                 {
385                         long tmp;
386                         ret = 0;
387                         if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
388                                 break;
389                         child->exit_code = SIGKILL;
390                         /* make sure the single step bit is not set. */
391                         tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
392                         put_reg(child, PT_SYSCFG, tmp);
393                         wake_up_process(child);
394                         break;
395                 }
396
397         case PTRACE_SINGLESTEP:
398                 {               /* set the trap flag. */
399                         long tmp;
400
401                         pr_debug("single step\n");
402                         ret = -EIO;
403                         if (!valid_signal(data))
404                                 break;
405                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
406
407                         tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
408                         put_reg(child, PT_SYSCFG, tmp);
409
410                         child->exit_code = data;
411                         /* give it a chance to run. */
412                         wake_up_process(child);
413                         ret = 0;
414                         break;
415                 }
416
417         case PTRACE_GETREGS:
418                 {
419
420                         /* Get all gp regs from the child. */
421                         ret = ptrace_getregs(child, datap);
422                         break;
423                 }
424
425         case PTRACE_SETREGS:
426                 {
427                         printk(KERN_NOTICE
428                                "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
429                         /* Set all gp regs in the child. */
430                         ret = 0;
431                         break;
432                 }
433         default:
434                 ret = ptrace_request(child, request, addr, data);
435                 break;
436         }
437
438         return ret;
439 }
440
441 asmlinkage void syscall_trace(void)
442 {
443
444         if (!test_thread_flag(TIF_SYSCALL_TRACE))
445                 return;
446
447         if (!(current->ptrace & PT_PTRACED))
448                 return;
449
450         /* the 0x80 provides a way for the tracing parent to distinguish
451          * between a syscall stop and SIGTRAP delivery
452          */
453         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
454                                  ? 0x80 : 0));
455
456         /*
457          * this isn't the same as continuing with a signal, but it will do
458          * for normal use.  strace only continues with a signal if the
459          * stopping signal is not SIGTRAP.  -brl
460          */
461         if (current->exit_code) {
462                 send_sig(current->exit_code, current, 1);
463                 current->exit_code = 0;
464         }
465 }