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