Merge branch 'upstream'
[pandora-kernel.git] / arch / arm26 / kernel / ptrace.c
1 /*
2  *  linux/arch/arm26/kernel/ptrace.c
3  *
4  *  By Ross Biro 1/23/92
5  * edited by Linus Torvalds
6  * ARM modifications Copyright (C) 2000 Russell King
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/config.h>
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/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/security.h>
21 #include <linux/signal.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 //#include <asm/processor.h>
27
28 #include "ptrace.h"
29
30 #define REG_PC  15
31 #define REG_PSR 15
32 /*
33  * does not yet catch signals sent when the child dies.
34  * in exit.c or in signal.c.
35  */
36
37 /*
38  * Breakpoint SWI instruction: SWI &9F0001
39  */
40 #define BREAKINST_ARM   0xef9f0001
41
42 /*
43  * this routine will get a word off of the processes privileged stack.
44  * the offset is how far from the base addr as stored in the THREAD.
45  * this routine assumes that all the privileged stacks are in our
46  * data space.
47  */
48 static inline long get_user_reg(struct task_struct *task, int offset)
49 {
50         return task_pt_regs(task)->uregs[offset];
51 }
52
53 /*
54  * this routine will put a word on the processes privileged stack.
55  * the offset is how far from the base addr as stored in the THREAD.
56  * this routine assumes that all the privileged stacks are in our
57  * data space.
58  */
59 static inline int
60 put_user_reg(struct task_struct *task, int offset, long data)
61 {
62         struct pt_regs newregs, *regs = task_pt_regs(task);
63         int ret = -EINVAL;
64
65         newregs = *regs;
66         newregs.uregs[offset] = data;
67
68         if (valid_user_regs(&newregs)) {
69                 regs->uregs[offset] = data;
70                 ret = 0;
71         }
72
73         return ret;
74 }
75
76 static inline int
77 read_u32(struct task_struct *task, unsigned long addr, u32 *res)
78 {
79         int ret;
80
81         ret = access_process_vm(task, addr, res, sizeof(*res), 0);
82
83         return ret == sizeof(*res) ? 0 : -EIO;
84 }
85
86 static inline int
87 read_instr(struct task_struct *task, unsigned long addr, u32 *res)
88 {
89         int ret;
90         u32 val;
91         ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
92         ret = ret == sizeof(val) ? 0 : -EIO;
93         *res = val;
94         return ret;
95 }
96
97 /*
98  * Get value of register `rn' (in the instruction)
99  */
100 static unsigned long
101 ptrace_getrn(struct task_struct *child, unsigned long insn)
102 {
103         unsigned int reg = (insn >> 16) & 15;
104         unsigned long val;
105
106         val = get_user_reg(child, reg);
107         if (reg == 15)
108                 val = pc_pointer(val + 8); //FIXME - correct for arm26?
109
110         return val;
111 }
112
113 /*
114  * Get value of operand 2 (in an ALU instruction)
115  */
116 static unsigned long
117 ptrace_getaluop2(struct task_struct *child, unsigned long insn)
118 {
119         unsigned long val;
120         int shift;
121         int type;
122
123         if (insn & 1 << 25) {
124                 val = insn & 255;
125                 shift = (insn >> 8) & 15;
126                 type = 3;
127         } else {
128                 val = get_user_reg (child, insn & 15);
129
130                 if (insn & (1 << 4))
131                         shift = (int)get_user_reg (child, (insn >> 8) & 15);
132                 else
133                         shift = (insn >> 7) & 31;
134
135                 type = (insn >> 5) & 3;
136         }
137
138         switch (type) {
139         case 0: val <<= shift;  break;
140         case 1: val >>= shift;  break;
141         case 2:
142                 val = (((signed long)val) >> shift);
143                 break;
144         case 3:
145                 val = (val >> shift) | (val << (32 - shift));
146                 break;
147         }
148         return val;
149 }
150
151 /*
152  * Get value of operand 2 (in a LDR instruction)
153  */
154 static unsigned long
155 ptrace_getldrop2(struct task_struct *child, unsigned long insn)
156 {
157         unsigned long val;
158         int shift;
159         int type;
160
161         val = get_user_reg(child, insn & 15);
162         shift = (insn >> 7) & 31;
163         type = (insn >> 5) & 3;
164
165         switch (type) {
166         case 0: val <<= shift;  break;
167         case 1: val >>= shift;  break;
168         case 2:
169                 val = (((signed long)val) >> shift);
170                 break;
171         case 3:
172                 val = (val >> shift) | (val << (32 - shift));
173                 break;
174         }
175         return val;
176 }
177
178 #define OP_MASK 0x01e00000
179 #define OP_AND  0x00000000
180 #define OP_EOR  0x00200000
181 #define OP_SUB  0x00400000
182 #define OP_RSB  0x00600000
183 #define OP_ADD  0x00800000
184 #define OP_ADC  0x00a00000
185 #define OP_SBC  0x00c00000
186 #define OP_RSC  0x00e00000
187 #define OP_ORR  0x01800000
188 #define OP_MOV  0x01a00000
189 #define OP_BIC  0x01c00000
190 #define OP_MVN  0x01e00000
191
192 static unsigned long
193 get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
194 {
195         u32 alt = 0;
196
197         switch (insn & 0x0e000000) {
198         case 0x00000000:
199         case 0x02000000: {
200                 /*
201                  * data processing
202                  */
203                 long aluop1, aluop2, ccbit;
204
205                 if ((insn & 0xf000) != 0xf000)
206                         break;
207
208                 aluop1 = ptrace_getrn(child, insn);
209                 aluop2 = ptrace_getaluop2(child, insn);
210                 ccbit  = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
211
212                 switch (insn & OP_MASK) {
213                 case OP_AND: alt = aluop1 & aluop2;             break;
214                 case OP_EOR: alt = aluop1 ^ aluop2;             break;
215                 case OP_SUB: alt = aluop1 - aluop2;             break;
216                 case OP_RSB: alt = aluop2 - aluop1;             break;
217                 case OP_ADD: alt = aluop1 + aluop2;             break;
218                 case OP_ADC: alt = aluop1 + aluop2 + ccbit;     break;
219                 case OP_SBC: alt = aluop1 - aluop2 + ccbit;     break;
220                 case OP_RSC: alt = aluop2 - aluop1 + ccbit;     break;
221                 case OP_ORR: alt = aluop1 | aluop2;             break;
222                 case OP_MOV: alt = aluop2;                      break;
223                 case OP_BIC: alt = aluop1 & ~aluop2;            break;
224                 case OP_MVN: alt = ~aluop2;                     break;
225                 }
226                 break;
227         }
228
229         case 0x04000000:
230         case 0x06000000:
231                 /*
232                  * ldr
233                  */
234                 if ((insn & 0x0010f000) == 0x0010f000) {
235                         unsigned long base;
236
237                         base = ptrace_getrn(child, insn);
238                         if (insn & 1 << 24) {
239                                 long aluop2;
240
241                                 if (insn & 0x02000000)
242                                         aluop2 = ptrace_getldrop2(child, insn);
243                                 else
244                                         aluop2 = insn & 0xfff;
245
246                                 if (insn & 1 << 23)
247                                         base += aluop2;
248                                 else
249                                         base -= aluop2;
250                         }
251                         if (read_u32(child, base, &alt) == 0)
252                                 alt = pc_pointer(alt);
253                 }
254                 break;
255
256         case 0x08000000:
257                 /*
258                  * ldm
259                  */
260                 if ((insn & 0x00108000) == 0x00108000) {
261                         unsigned long base;
262                         unsigned int nr_regs;
263
264                         if (insn & (1 << 23)) {
265                                 nr_regs = hweight16(insn & 65535) << 2;
266
267                                 if (!(insn & (1 << 24)))
268                                         nr_regs -= 4;
269                         } else {
270                                 if (insn & (1 << 24))
271                                         nr_regs = -4;
272                                 else
273                                         nr_regs = 0;
274                         }
275
276                         base = ptrace_getrn(child, insn);
277
278                         if (read_u32(child, base + nr_regs, &alt) == 0)
279                                 alt = pc_pointer(alt);
280                         break;
281                 }
282                 break;
283
284         case 0x0a000000: {
285                 /*
286                  * bl or b
287                  */
288                 signed long displ;
289                 /* It's a branch/branch link: instead of trying to
290                  * figure out whether the branch will be taken or not,
291                  * we'll put a breakpoint at both locations.  This is
292                  * simpler, more reliable, and probably not a whole lot
293                  * slower than the alternative approach of emulating the
294                  * branch.
295                  */
296                 displ = (insn & 0x00ffffff) << 8;
297                 displ = (displ >> 6) + 8;
298                 if (displ != 0 && displ != 4)
299                         alt = pc + displ;
300             }
301             break;
302         }
303
304         return alt;
305 }
306
307 static int
308 swap_insn(struct task_struct *task, unsigned long addr,
309           void *old_insn, void *new_insn, int size)
310 {
311         int ret;
312
313         ret = access_process_vm(task, addr, old_insn, size, 0);
314         if (ret == size)
315                 ret = access_process_vm(task, addr, new_insn, size, 1);
316         return ret;
317 }
318
319 static void
320 add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
321 {
322         int nr = dbg->nsaved;
323
324         if (nr < 2) {
325                 u32 new_insn = BREAKINST_ARM;
326                 int res;
327
328                 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
329
330                 if (res == 4) {
331                         dbg->bp[nr].address = addr;
332                         dbg->nsaved += 1;
333                 }
334         } else
335                 printk(KERN_ERR "ptrace: too many breakpoints\n");
336 }
337
338 /*
339  * Clear one breakpoint in the user program.  We copy what the hardware
340  * does and use bit 0 of the address to indicate whether this is a Thumb
341  * breakpoint or an ARM breakpoint.
342  */
343 static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
344 {
345         unsigned long addr = bp->address;
346         u32 old_insn;
347         int ret;
348
349         ret = swap_insn(task, addr & ~3, &old_insn,
350                         &bp->insn, 4);
351
352         if (ret != 4 || old_insn != BREAKINST_ARM)
353                 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
354                         "0x%08lx (0x%08x)\n", task->comm, task->pid,
355                         addr, old_insn);
356 }
357
358 void ptrace_set_bpt(struct task_struct *child)
359 {
360         struct pt_regs *regs;
361         unsigned long pc;
362         u32 insn;
363         int res;
364
365         regs = task_pt_regs(child);
366         pc = instruction_pointer(regs);
367
368         res = read_instr(child, pc, &insn);
369         if (!res) {
370                 struct debug_info *dbg = &child->thread.debug;
371                 unsigned long alt;
372
373                 dbg->nsaved = 0;
374
375                 alt = get_branch_address(child, pc, insn);
376                 if (alt)
377                         add_breakpoint(child, dbg, alt);
378
379                 /*
380                  * Note that we ignore the result of setting the above
381                  * breakpoint since it may fail.  When it does, this is
382                  * not so much an error, but a forewarning that we may
383                  * be receiving a prefetch abort shortly.
384                  *
385                  * If we don't set this breakpoint here, then we can
386                  * lose control of the thread during single stepping.
387                  */
388                 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
389                         add_breakpoint(child, dbg, pc + 4);
390         }
391 }
392
393 /*
394  * Ensure no single-step breakpoint is pending.  Returns non-zero
395  * value if child was being single-stepped.
396  */
397 void ptrace_cancel_bpt(struct task_struct *child)
398 {
399         int i, nsaved = child->thread.debug.nsaved;
400
401         child->thread.debug.nsaved = 0;
402
403         if (nsaved > 2) {
404                 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
405                 nsaved = 2;
406         }
407
408         for (i = 0; i < nsaved; i++)
409                 clear_breakpoint(child, &child->thread.debug.bp[i]);
410 }
411
412 /*
413  * Called by kernel/ptrace.c when detaching..
414  *
415  * Make sure the single step bit is not set.
416  */
417 void ptrace_disable(struct task_struct *child)
418 {
419         child->ptrace &= ~PT_SINGLESTEP;
420         ptrace_cancel_bpt(child);
421 }
422
423 /*
424  * Handle hitting a breakpoint.
425  */
426 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
427 {
428         siginfo_t info;
429
430         /*
431          * The PC is always left pointing at the next instruction.  Fix this.
432          */
433         regs->ARM_pc -= 4;
434
435         if (tsk->thread.debug.nsaved == 0)
436                 printk(KERN_ERR "ptrace: bogus breakpoint trap\n");
437
438         ptrace_cancel_bpt(tsk);
439
440         info.si_signo = SIGTRAP;
441         info.si_errno = 0;
442         info.si_code  = TRAP_BRKPT;
443         info.si_addr  = (void *)instruction_pointer(regs) - 4;
444
445         force_sig_info(SIGTRAP, &info, tsk);
446 }
447
448 /*
449  * Read the word at offset "off" into the "struct user".  We
450  * actually access the pt_regs stored on the kernel stack.
451  */
452 static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
453                             unsigned long *ret)
454 {
455         unsigned long tmp;
456
457         if (off & 3 || off >= sizeof(struct user))
458                 return -EIO;
459
460         tmp = 0;
461         if (off < sizeof(struct pt_regs))
462                 tmp = get_user_reg(tsk, off >> 2);
463
464         return put_user(tmp, ret);
465 }
466
467 /*
468  * Write the word at offset "off" into "struct user".  We
469  * actually access the pt_regs stored on the kernel stack.
470  */
471 static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
472                              unsigned long val)
473 {
474         if (off & 3 || off >= sizeof(struct user))
475                 return -EIO;
476
477         if (off >= sizeof(struct pt_regs))
478                 return 0;
479
480         return put_user_reg(tsk, off >> 2, val);
481 }
482
483 /*
484  * Get all user integer registers.
485  */
486 static int ptrace_getregs(struct task_struct *tsk, void *uregs)
487 {
488         struct pt_regs *regs = task_pt_regs(tsk);
489
490         return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
491 }
492
493 /*
494  * Set all user integer registers.
495  */
496 static int ptrace_setregs(struct task_struct *tsk, void *uregs)
497 {
498         struct pt_regs newregs;
499         int ret;
500
501         ret = -EFAULT;
502         if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
503                 struct pt_regs *regs = task_pt_regs(tsk);
504
505                 ret = -EINVAL;
506                 if (valid_user_regs(&newregs)) {
507                         *regs = newregs;
508                         ret = 0;
509                 }
510         }
511
512         return ret;
513 }
514
515 /*
516  * Get the child FPU state.
517  */
518 static int ptrace_getfpregs(struct task_struct *tsk, void *ufp)
519 {
520         return copy_to_user(ufp, &task_thread_info(tsk)->fpstate,
521                             sizeof(struct user_fp)) ? -EFAULT : 0;
522 }
523
524 /*
525  * Set the child FPU state.
526  */
527 static int ptrace_setfpregs(struct task_struct *tsk, void *ufp)
528 {
529         set_stopped_child_used_math(tsk);
530         return copy_from_user(&task_thread_info(tsk)->fpstate, ufp,
531                               sizeof(struct user_fp)) ? -EFAULT : 0;
532 }
533
534 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
535 {
536         unsigned long tmp;
537         int ret;
538
539         switch (request) {
540                 /*
541                  * read word at location "addr" in the child process.
542                  */
543                 case PTRACE_PEEKTEXT:
544                 case PTRACE_PEEKDATA:
545                         ret = access_process_vm(child, addr, &tmp,
546                                                 sizeof(unsigned long), 0);
547                         if (ret == sizeof(unsigned long))
548                                 ret = put_user(tmp, (unsigned long *) data);
549                         else
550                                 ret = -EIO;
551                         break;
552
553                 case PTRACE_PEEKUSR:
554                         ret = ptrace_read_user(child, addr, (unsigned long *)data);
555                         break;
556
557                 /*
558                  * write the word at location addr.
559                  */
560                 case PTRACE_POKETEXT:
561                 case PTRACE_POKEDATA:
562                         ret = access_process_vm(child, addr, &data,
563                                                 sizeof(unsigned long), 1);
564                         if (ret == sizeof(unsigned long))
565                                 ret = 0;
566                         else
567                                 ret = -EIO;
568                         break;
569
570                 case PTRACE_POKEUSR:
571                         ret = ptrace_write_user(child, addr, data);
572                         break;
573
574                 /*
575                  * continue/restart and stop at next (return from) syscall
576                  */
577                 case PTRACE_SYSCALL:
578                 case PTRACE_CONT:
579                         ret = -EIO;
580                         if (!valid_signal(data))
581                                 break;
582                         if (request == PTRACE_SYSCALL)
583                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
584                         else
585                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
586                         child->exit_code = data;
587                         /* make sure single-step breakpoint is gone. */
588                         child->ptrace &= ~PT_SINGLESTEP;
589                         ptrace_cancel_bpt(child);
590                         wake_up_process(child);
591                         ret = 0;
592                         break;
593
594                 /*
595                  * make the child exit.  Best I can do is send it a sigkill.
596                  * perhaps it should be put in the status that it wants to
597                  * exit.
598                  */
599                 case PTRACE_KILL:
600                         /* make sure single-step breakpoint is gone. */
601                         child->ptrace &= ~PT_SINGLESTEP;
602                         ptrace_cancel_bpt(child);
603                         if (child->exit_state != EXIT_ZOMBIE) {
604                                 child->exit_code = SIGKILL;
605                                 wake_up_process(child);
606                         }
607                         ret = 0;
608                         break;
609
610                 /*
611                  * execute single instruction.
612                  */
613                 case PTRACE_SINGLESTEP:
614                         ret = -EIO;
615                         if (!valid_signal(data))
616                                 break;
617                         child->ptrace |= PT_SINGLESTEP;
618                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
619                         child->exit_code = data;
620                         /* give it a chance to run. */
621                         wake_up_process(child);
622                         ret = 0;
623                         break;
624
625                 case PTRACE_DETACH:
626                         ret = ptrace_detach(child, data);
627                         break;
628
629                 case PTRACE_GETREGS:
630                         ret = ptrace_getregs(child, (void *)data);
631                         break;
632
633                 case PTRACE_SETREGS:
634                         ret = ptrace_setregs(child, (void *)data);
635                         break;
636
637                 case PTRACE_GETFPREGS:
638                         ret = ptrace_getfpregs(child, (void *)data);
639                         break;
640                 
641                 case PTRACE_SETFPREGS:
642                         ret = ptrace_setfpregs(child, (void *)data);
643                         break;
644
645                 default:
646                         ret = ptrace_request(child, request, addr, data);
647                         break;
648         }
649
650         return ret;
651 }
652
653 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
654 {
655         unsigned long ip;
656
657         if (!test_thread_flag(TIF_SYSCALL_TRACE))
658                 return;
659         if (!(current->ptrace & PT_PTRACED))
660                 return;
661
662         /*
663          * Save IP.  IP is used to denote syscall entry/exit:
664          *  IP = 0 -> entry, = 1 -> exit
665          */
666         ip = regs->ARM_ip;
667         regs->ARM_ip = why;
668
669         /* the 0x80 provides a way for the tracing parent to distinguish
670            between a syscall stop and SIGTRAP delivery */
671         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
672                                  ? 0x80 : 0));
673         /*
674          * this isn't the same as continuing with a signal, but it will do
675          * for normal use.  strace only continues with a signal if the
676          * stopping signal is not SIGTRAP.  -brl
677          */
678         if (current->exit_code) {
679                 send_sig(current->exit_code, current, 1);
680                 current->exit_code = 0;
681         }
682         regs->ARM_ip = ip;
683 }