Blackfin arch: noMMU CPLB lookup tables can be in L1 SRAM
[linux-2.6] / 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 TEXT_OFFSET 0
50 /*
51  * does not yet catch signals sent when the child dies.
52  * in exit.c or in signal.c.
53  */
54
55 /* determines which bits in the SYSCFG reg the user has access to. */
56 /* 1 = access 0 = no access */
57 #define SYSCFG_MASK 0x0007      /* SYSCFG reg */
58 /* sets the trace bits. */
59 #define TRACE_BITS 0x0001
60
61 /* Find the stack offset for a register, relative to thread.esp0. */
62 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
63
64 /*
65  * Get the address of the live pt_regs for the specified task.
66  * These are saved onto the top kernel stack when the process
67  * is not running.
68  *
69  * Note: if a user thread is execve'd from kernel space, the
70  * kernel stack will not be empty on entry to the kernel, so
71  * ptracing these tasks will fail.
72  */
73 static inline struct pt_regs *get_user_regs(struct task_struct *task)
74 {
75         return (struct pt_regs *)
76             ((unsigned long)task_stack_page(task) +
77              (THREAD_SIZE - sizeof(struct pt_regs)));
78 }
79
80 /*
81  * Get all user integer registers.
82  */
83 static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
84 {
85         struct pt_regs regs;
86         memcpy(&regs, get_user_regs(tsk), sizeof(regs));
87         regs.usp = tsk->thread.usp;
88         return copy_to_user(uregs, &regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
89 }
90
91 /* Mapping from PT_xxx to the stack offset at which the register is
92  * saved.  Notice that usp has no stack-slot and needs to be treated
93  * specially (see get_reg/put_reg below).
94  */
95
96 /*
97  * Get contents of register REGNO in task TASK.
98  */
99 static inline long get_reg(struct task_struct *task, int regno)
100 {
101         unsigned char *reg_ptr;
102
103         struct pt_regs *regs =
104             (struct pt_regs *)((unsigned long)task_stack_page(task) +
105                                (THREAD_SIZE - sizeof(struct pt_regs)));
106         reg_ptr = (char *)regs;
107
108         switch (regno) {
109         case PT_USP:
110                 return task->thread.usp;
111         default:
112                 if (regno <= 216)
113                         return *(long *)(reg_ptr + regno);
114         }
115         /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
116
117         printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
118         return 0;
119 }
120
121 /*
122  * Write contents of register REGNO in task TASK.
123  */
124 static inline int
125 put_reg(struct task_struct *task, int regno, unsigned long data)
126 {
127         char *reg_ptr;
128
129         struct pt_regs *regs =
130             (struct pt_regs *)((unsigned long)task_stack_page(task) +
131                                (THREAD_SIZE - sizeof(struct pt_regs)));
132         reg_ptr = (char *)regs;
133
134         switch (regno) {
135         case PT_PC:
136                 /*********************************************************************/
137                 /* At this point the kernel is most likely in exception.             */
138                 /* The RETX register will be used to populate the pc of the process. */
139                 /*********************************************************************/
140                 regs->retx = data;
141                 regs->pc = data;
142                 break;
143         case PT_RETX:
144                 break;          /* regs->retx = data; break; */
145         case PT_USP:
146                 regs->usp = data;
147                 task->thread.usp = data;
148                 break;
149         default:
150                 if (regno <= 216)
151                         *(long *)(reg_ptr + regno) = data;
152         }
153         return 0;
154 }
155
156 /*
157  * check that an address falls within the bounds of the target process's memory mappings
158  */
159 static inline int is_user_addr_valid(struct task_struct *child,
160                                      unsigned long start, unsigned long len)
161 {
162         struct vm_list_struct *vml;
163         struct sram_list_struct *sraml;
164
165         /* overflow */
166         if (start + len < start)
167                 return -EIO;
168
169         for (vml = child->mm->context.vmlist; vml; vml = vml->next)
170                 if (start >= vml->vma->vm_start && start + len < vml->vma->vm_end)
171                         return 0;
172
173         for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
174                 if (start >= (unsigned long)sraml->addr
175                     && start + len < (unsigned long)sraml->addr + sraml->length)
176                         return 0;
177
178         if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
179                 return 0;
180
181         return -EIO;
182 }
183
184 void ptrace_enable(struct task_struct *child)
185 {
186         unsigned long tmp;
187         tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
188         put_reg(child, PT_SYSCFG, tmp);
189 }
190
191 /*
192  * Called by kernel/ptrace.c when detaching..
193  *
194  * Make sure the single step bit is not set.
195  */
196 void ptrace_disable(struct task_struct *child)
197 {
198         unsigned long tmp;
199         /* make sure the single step bit is not set. */
200         tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
201         put_reg(child, PT_SYSCFG, tmp);
202 }
203
204 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
205 {
206         int ret;
207         unsigned long __user *datap = (unsigned long __user *)data;
208
209         switch (request) {
210                 /* when I and D space are separate, these will need to be fixed. */
211         case PTRACE_PEEKDATA:
212                 pr_debug("ptrace: PEEKDATA\n");
213                 /* fall through */
214         case PTRACE_PEEKTEXT:   /* read word at location addr. */
215                 {
216                         unsigned long tmp = 0;
217                         int copied;
218
219                         ret = -EIO;
220                         pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %ld\n", addr, sizeof(data));
221                         if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0)
222                                 break;
223                         pr_debug("ptrace: user address is valid\n");
224
225                         if (L1_CODE_LENGTH != 0 && addr >= get_l1_code_start()
226                             && addr + sizeof(tmp) <= get_l1_code_start() + L1_CODE_LENGTH) {
227                                 safe_dma_memcpy (&tmp, (const void *)(addr), sizeof(tmp));
228                                 copied = sizeof(tmp);
229
230                         } else if (L1_DATA_A_LENGTH != 0 && addr >= L1_DATA_A_START
231                             && addr + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
232                                 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
233                                 copied = sizeof(tmp);
234
235                         } else if (L1_DATA_B_LENGTH != 0 && addr >= L1_DATA_B_START
236                             && addr + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
237                                 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
238                                 copied = sizeof(tmp);
239
240                         } else if (addr >= FIXED_CODE_START
241                             && addr + sizeof(tmp) <= FIXED_CODE_END) {
242                                 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
243                                 copied = sizeof(tmp);
244
245                         } else
246                                 copied = access_process_vm(child, addr, &tmp,
247                                                            sizeof(tmp), 0);
248
249                         pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
250                         if (copied != sizeof(tmp))
251                                 break;
252                         ret = put_user(tmp, datap);
253                         break;
254                 }
255
256                 /* read the word at location addr in the USER area. */
257         case PTRACE_PEEKUSR:
258                 {
259                         unsigned long tmp;
260                         ret = -EIO;
261                         tmp = 0;
262                         if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
263                                 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
264                                                     "0 - %x sizeof(pt_regs) is %lx\n",
265                                      (int)addr, sizeof(struct pt_regs));
266                                 break;
267                         }
268                         if (addr == sizeof(struct pt_regs)) {
269                                 /* PT_TEXT_ADDR */
270                                 tmp = child->mm->start_code + TEXT_OFFSET;
271                         } else if (addr == (sizeof(struct pt_regs) + 4)) {
272                                 /* PT_TEXT_END_ADDR */
273                                 tmp = child->mm->end_code;
274                         } else if (addr == (sizeof(struct pt_regs) + 8)) {
275                                 /* PT_DATA_ADDR */
276                                 tmp = child->mm->start_data;
277 #ifdef CONFIG_BINFMT_ELF_FDPIC
278                         } else if (addr == (sizeof(struct pt_regs) + 12)) {
279                                 tmp = child->mm->context.exec_fdpic_loadmap;
280                         } else if (addr == (sizeof(struct pt_regs) + 16)) {
281                                 tmp = child->mm->context.interp_fdpic_loadmap;
282 #endif
283                         } else {
284                                 tmp = get_reg(child, addr);
285                         }
286                         ret = put_user(tmp, datap);
287                         break;
288                 }
289
290                 /* when I and D space are separate, this will have to be fixed. */
291         case PTRACE_POKEDATA:
292                 pr_debug("ptrace: PTRACE_PEEKDATA\n");
293                 /* fall through */
294         case PTRACE_POKETEXT:   /* write the word at location addr. */
295                 {
296                         int copied;
297
298                         ret = -EIO;
299                         pr_debug("ptrace: POKETEXT at addr 0x%08lx + %ld bytes %lx\n",
300                                  addr, sizeof(data), data);
301                         if (is_user_addr_valid(child, addr, sizeof(data)) < 0)
302                                 break;
303                         pr_debug("ptrace: user address is valid\n");
304
305                         if (L1_CODE_LENGTH != 0 && addr >= get_l1_code_start()
306                             && addr + sizeof(data) <= get_l1_code_start() + L1_CODE_LENGTH) {
307                                 safe_dma_memcpy ((void *)(addr), &data, sizeof(data));
308                                 copied = sizeof(data);
309
310                         } else if (L1_DATA_A_LENGTH != 0 && addr >= L1_DATA_A_START
311                             && addr + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
312                                 memcpy((void *)(addr), &data, sizeof(data));
313                                 copied = sizeof(data);
314
315                         } else if (L1_DATA_B_LENGTH != 0 && addr >= L1_DATA_B_START
316                             && addr + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
317                                 memcpy((void *)(addr), &data, sizeof(data));
318                                 copied = sizeof(data);
319
320                         } else if (addr >= FIXED_CODE_START
321                             && addr + sizeof(data) <= FIXED_CODE_END) {
322                                 memcpy((void *)(addr), &data, sizeof(data));
323                                 copied = sizeof(data);
324
325                         } else
326                                 copied = access_process_vm(child, addr, &data,
327                                                            sizeof(data), 1);
328
329                         pr_debug("ptrace: copied size %d\n", copied);
330                         if (copied != sizeof(data))
331                                 break;
332                         ret = 0;
333                         break;
334                 }
335
336         case PTRACE_POKEUSR:    /* write the word at location addr in the USER area */
337                 ret = -EIO;
338                 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
339                         printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
340                         break;
341                 }
342
343                 if (addr >= (sizeof(struct pt_regs))) {
344                         ret = 0;
345                         break;
346                 }
347                 if (addr == PT_SYSCFG) {
348                         data &= SYSCFG_MASK;
349                         data |= get_reg(child, PT_SYSCFG);
350                 }
351                 ret = put_reg(child, addr, data);
352                 break;
353
354         case PTRACE_SYSCALL:    /* continue and stop at next (return from) syscall */
355         case PTRACE_CONT:       /* restart after signal. */
356                 pr_debug("ptrace: syscall/cont\n");
357
358                 ret = -EIO;
359                 if (!valid_signal(data))
360                         break;
361                 if (request == PTRACE_SYSCALL)
362                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
363                 else
364                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
365                 child->exit_code = data;
366                 ptrace_disable(child);
367                 pr_debug("ptrace: before wake_up_process\n");
368                 wake_up_process(child);
369                 ret = 0;
370                 break;
371
372         /*
373          * make the child exit.  Best I can do is send it a sigkill.
374          * perhaps it should be put in the status that it wants to
375          * exit.
376          */
377         case PTRACE_KILL:
378                 ret = 0;
379                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
380                         break;
381                 child->exit_code = SIGKILL;
382                 ptrace_disable(child);
383                 wake_up_process(child);
384                 break;
385
386         case PTRACE_SINGLESTEP: /* set the trap flag. */
387                 pr_debug("ptrace: single step\n");
388                 ret = -EIO;
389                 if (!valid_signal(data))
390                         break;
391                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
392                 ptrace_enable(child);
393                 child->exit_code = data;
394                 wake_up_process(child);
395                 ret = 0;
396                 break;
397
398         case PTRACE_GETREGS:
399                 /* Get all gp regs from the child. */
400                 ret = ptrace_getregs(child, datap);
401                 break;
402
403         case PTRACE_SETREGS:
404                 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
405                 /* Set all gp regs in the child. */
406                 ret = 0;
407                 break;
408
409         default:
410                 ret = ptrace_request(child, request, addr, data);
411                 break;
412         }
413
414         return ret;
415 }
416
417 asmlinkage void syscall_trace(void)
418 {
419         if (!test_thread_flag(TIF_SYSCALL_TRACE))
420                 return;
421
422         if (!(current->ptrace & PT_PTRACED))
423                 return;
424
425         /* the 0x80 provides a way for the tracing parent to distinguish
426          * between a syscall stop and SIGTRAP delivery
427          */
428         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
429                                  ? 0x80 : 0));
430
431         /*
432          * this isn't the same as continuing with a signal, but it will do
433          * for normal use.  strace only continues with a signal if the
434          * stopping signal is not SIGTRAP.  -brl
435          */
436         if (current->exit_code) {
437                 send_sig(current->exit_code, current, 1);
438                 current->exit_code = 0;
439         }
440 }