Merge branch 'sh/driver-core'
[pandora-kernel.git] / arch / arm / kernel / kprobes.c
1 /*
2  * arch/arm/kernel/kprobes.c
3  *
4  * Kprobes on ARM
5  *
6  * Abhishek Sagar <sagar.abhishek@gmail.com>
7  * Copyright (C) 2006, 2007 Motorola Inc.
8  *
9  * Nicolas Pitre <nico@marvell.com>
10  * Copyright (C) 2007 Marvell Ltd.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/kprobes.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/stop_machine.h>
27 #include <linux/stringify.h>
28 #include <asm/traps.h>
29 #include <asm/cacheflush.h>
30
31 #define MIN_STACK_SIZE(addr)                            \
32         min((unsigned long)MAX_STACK_SIZE,              \
33             (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
34
35 #define flush_insns(addr, cnt)                          \
36         flush_icache_range((unsigned long)(addr),       \
37                            (unsigned long)(addr) +      \
38                            sizeof(kprobe_opcode_t) * (cnt))
39
40 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
41 #define JPROBE_MAGIC_ADDR               0xffffffff
42
43 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
44 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
45
46
47 int __kprobes arch_prepare_kprobe(struct kprobe *p)
48 {
49         kprobe_opcode_t insn;
50         kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
51         unsigned long addr = (unsigned long)p->addr;
52         int is;
53
54         if (addr & 0x3 || in_exception_text(addr))
55                 return -EINVAL;
56
57         insn = *p->addr;
58         p->opcode = insn;
59         p->ainsn.insn = tmp_insn;
60
61         switch (arm_kprobe_decode_insn(insn, &p->ainsn)) {
62         case INSN_REJECTED:     /* not supported */
63                 return -EINVAL;
64
65         case INSN_GOOD:         /* instruction uses slot */
66                 p->ainsn.insn = get_insn_slot();
67                 if (!p->ainsn.insn)
68                         return -ENOMEM;
69                 for (is = 0; is < MAX_INSN_SIZE; ++is)
70                         p->ainsn.insn[is] = tmp_insn[is];
71                 flush_insns(p->ainsn.insn, MAX_INSN_SIZE);
72                 break;
73
74         case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
75                 p->ainsn.insn = NULL;
76                 break;
77         }
78
79         return 0;
80 }
81
82 void __kprobes arch_arm_kprobe(struct kprobe *p)
83 {
84         *p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
85         flush_insns(p->addr, 1);
86 }
87
88 /*
89  * The actual disarming is done here on each CPU and synchronized using
90  * stop_machine. This synchronization is necessary on SMP to avoid removing
91  * a probe between the moment the 'Undefined Instruction' exception is raised
92  * and the moment the exception handler reads the faulting instruction from
93  * memory.
94  */
95 int __kprobes __arch_disarm_kprobe(void *p)
96 {
97         struct kprobe *kp = p;
98         *kp->addr = kp->opcode;
99         flush_insns(kp->addr, 1);
100         return 0;
101 }
102
103 void __kprobes arch_disarm_kprobe(struct kprobe *p)
104 {
105         stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
106 }
107
108 void __kprobes arch_remove_kprobe(struct kprobe *p)
109 {
110         if (p->ainsn.insn) {
111                 free_insn_slot(p->ainsn.insn, 0);
112                 p->ainsn.insn = NULL;
113         }
114 }
115
116 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
117 {
118         kcb->prev_kprobe.kp = kprobe_running();
119         kcb->prev_kprobe.status = kcb->kprobe_status;
120 }
121
122 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
123 {
124         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
125         kcb->kprobe_status = kcb->prev_kprobe.status;
126 }
127
128 static void __kprobes set_current_kprobe(struct kprobe *p)
129 {
130         __get_cpu_var(current_kprobe) = p;
131 }
132
133 static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
134                                  struct kprobe_ctlblk *kcb)
135 {
136         regs->ARM_pc += 4;
137         p->ainsn.insn_handler(p, regs);
138 }
139
140 /*
141  * Called with IRQs disabled. IRQs must remain disabled from that point
142  * all the way until processing this kprobe is complete.  The current
143  * kprobes implementation cannot process more than one nested level of
144  * kprobe, and that level is reserved for user kprobe handlers, so we can't
145  * risk encountering a new kprobe in an interrupt handler.
146  */
147 void __kprobes kprobe_handler(struct pt_regs *regs)
148 {
149         struct kprobe *p, *cur;
150         struct kprobe_ctlblk *kcb;
151         kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->ARM_pc;
152
153         kcb = get_kprobe_ctlblk();
154         cur = kprobe_running();
155         p = get_kprobe(addr);
156
157         if (p) {
158                 if (cur) {
159                         /* Kprobe is pending, so we're recursing. */
160                         switch (kcb->kprobe_status) {
161                         case KPROBE_HIT_ACTIVE:
162                         case KPROBE_HIT_SSDONE:
163                                 /* A pre- or post-handler probe got us here. */
164                                 kprobes_inc_nmissed_count(p);
165                                 save_previous_kprobe(kcb);
166                                 set_current_kprobe(p);
167                                 kcb->kprobe_status = KPROBE_REENTER;
168                                 singlestep(p, regs, kcb);
169                                 restore_previous_kprobe(kcb);
170                                 break;
171                         default:
172                                 /* impossible cases */
173                                 BUG();
174                         }
175                 } else {
176                         set_current_kprobe(p);
177                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
178
179                         /*
180                          * If we have no pre-handler or it returned 0, we
181                          * continue with normal processing.  If we have a
182                          * pre-handler and it returned non-zero, it prepped
183                          * for calling the break_handler below on re-entry,
184                          * so get out doing nothing more here.
185                          */
186                         if (!p->pre_handler || !p->pre_handler(p, regs)) {
187                                 kcb->kprobe_status = KPROBE_HIT_SS;
188                                 singlestep(p, regs, kcb);
189                                 if (p->post_handler) {
190                                         kcb->kprobe_status = KPROBE_HIT_SSDONE;
191                                         p->post_handler(p, regs, 0);
192                                 }
193                                 reset_current_kprobe();
194                         }
195                 }
196         } else if (cur) {
197                 /* We probably hit a jprobe.  Call its break handler. */
198                 if (cur->break_handler && cur->break_handler(cur, regs)) {
199                         kcb->kprobe_status = KPROBE_HIT_SS;
200                         singlestep(cur, regs, kcb);
201                         if (cur->post_handler) {
202                                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
203                                 cur->post_handler(cur, regs, 0);
204                         }
205                 }
206                 reset_current_kprobe();
207         } else {
208                 /*
209                  * The probe was removed and a race is in progress.
210                  * There is nothing we can do about it.  Let's restart
211                  * the instruction.  By the time we can restart, the
212                  * real instruction will be there.
213                  */
214         }
215 }
216
217 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
218 {
219         unsigned long flags;
220         local_irq_save(flags);
221         kprobe_handler(regs);
222         local_irq_restore(flags);
223         return 0;
224 }
225
226 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
227 {
228         struct kprobe *cur = kprobe_running();
229         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
230
231         switch (kcb->kprobe_status) {
232         case KPROBE_HIT_SS:
233         case KPROBE_REENTER:
234                 /*
235                  * We are here because the instruction being single
236                  * stepped caused a page fault. We reset the current
237                  * kprobe and the PC to point back to the probe address
238                  * and allow the page fault handler to continue as a
239                  * normal page fault.
240                  */
241                 regs->ARM_pc = (long)cur->addr;
242                 if (kcb->kprobe_status == KPROBE_REENTER) {
243                         restore_previous_kprobe(kcb);
244                 } else {
245                         reset_current_kprobe();
246                 }
247                 break;
248
249         case KPROBE_HIT_ACTIVE:
250         case KPROBE_HIT_SSDONE:
251                 /*
252                  * We increment the nmissed count for accounting,
253                  * we can also use npre/npostfault count for accounting
254                  * these specific fault cases.
255                  */
256                 kprobes_inc_nmissed_count(cur);
257
258                 /*
259                  * We come here because instructions in the pre/post
260                  * handler caused the page_fault, this could happen
261                  * if handler tries to access user space by
262                  * copy_from_user(), get_user() etc. Let the
263                  * user-specified handler try to fix it.
264                  */
265                 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
266                         return 1;
267                 break;
268
269         default:
270                 break;
271         }
272
273         return 0;
274 }
275
276 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
277                                        unsigned long val, void *data)
278 {
279         /*
280          * notify_die() is currently never called on ARM,
281          * so this callback is currently empty.
282          */
283         return NOTIFY_DONE;
284 }
285
286 /*
287  * When a retprobed function returns, trampoline_handler() is called,
288  * calling the kretprobe's handler. We construct a struct pt_regs to
289  * give a view of registers r0-r11 to the user return-handler.  This is
290  * not a complete pt_regs structure, but that should be plenty sufficient
291  * for kretprobe handlers which should normally be interested in r0 only
292  * anyway.
293  */
294 void __naked __kprobes kretprobe_trampoline(void)
295 {
296         __asm__ __volatile__ (
297                 "stmdb  sp!, {r0 - r11}         \n\t"
298                 "mov    r0, sp                  \n\t"
299                 "bl     trampoline_handler      \n\t"
300                 "mov    lr, r0                  \n\t"
301                 "ldmia  sp!, {r0 - r11}         \n\t"
302                 "mov    pc, lr                  \n\t"
303                 : : : "memory");
304 }
305
306 /* Called from kretprobe_trampoline */
307 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
308 {
309         struct kretprobe_instance *ri = NULL;
310         struct hlist_head *head, empty_rp;
311         struct hlist_node *node, *tmp;
312         unsigned long flags, orig_ret_address = 0;
313         unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
314
315         INIT_HLIST_HEAD(&empty_rp);
316         kretprobe_hash_lock(current, &head, &flags);
317
318         /*
319          * It is possible to have multiple instances associated with a given
320          * task either because multiple functions in the call path have
321          * a return probe installed on them, and/or more than one return
322          * probe was registered for a target function.
323          *
324          * We can handle this because:
325          *     - instances are always inserted at the head of the list
326          *     - when multiple return probes are registered for the same
327          *       function, the first instance's ret_addr will point to the
328          *       real return address, and all the rest will point to
329          *       kretprobe_trampoline
330          */
331         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
332                 if (ri->task != current)
333                         /* another task is sharing our hash bucket */
334                         continue;
335
336                 if (ri->rp && ri->rp->handler) {
337                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
338                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
339                         ri->rp->handler(ri, regs);
340                         __get_cpu_var(current_kprobe) = NULL;
341                 }
342
343                 orig_ret_address = (unsigned long)ri->ret_addr;
344                 recycle_rp_inst(ri, &empty_rp);
345
346                 if (orig_ret_address != trampoline_address)
347                         /*
348                          * This is the real return address. Any other
349                          * instances associated with this task are for
350                          * other calls deeper on the call stack
351                          */
352                         break;
353         }
354
355         kretprobe_assert(ri, orig_ret_address, trampoline_address);
356         kretprobe_hash_unlock(current, &flags);
357
358         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
359                 hlist_del(&ri->hlist);
360                 kfree(ri);
361         }
362
363         return (void *)orig_ret_address;
364 }
365
366 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
367                                       struct pt_regs *regs)
368 {
369         ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
370
371         /* Replace the return addr with trampoline addr. */
372         regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
373 }
374
375 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
376 {
377         struct jprobe *jp = container_of(p, struct jprobe, kp);
378         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
379         long sp_addr = regs->ARM_sp;
380
381         kcb->jprobe_saved_regs = *regs;
382         memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
383         regs->ARM_pc = (long)jp->entry;
384         regs->ARM_cpsr |= PSR_I_BIT;
385         preempt_disable();
386         return 1;
387 }
388
389 void __kprobes jprobe_return(void)
390 {
391         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
392
393         __asm__ __volatile__ (
394                 /*
395                  * Setup an empty pt_regs. Fill SP and PC fields as
396                  * they're needed by longjmp_break_handler.
397                  *
398                  * We allocate some slack between the original SP and start of
399                  * our fabricated regs. To be precise we want to have worst case
400                  * covered which is STMFD with all 16 regs so we allocate 2 *
401                  * sizeof(struct_pt_regs)).
402                  *
403                  * This is to prevent any simulated instruction from writing
404                  * over the regs when they are accessing the stack.
405                  */
406                 "sub    sp, %0, %1              \n\t"
407                 "ldr    r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
408                 "str    %0, [sp, %2]            \n\t"
409                 "str    r0, [sp, %3]            \n\t"
410                 "mov    r0, sp                  \n\t"
411                 "bl     kprobe_handler          \n\t"
412
413                 /*
414                  * Return to the context saved by setjmp_pre_handler
415                  * and restored by longjmp_break_handler.
416                  */
417                 "ldr    r0, [sp, %4]            \n\t"
418                 "msr    cpsr_cxsf, r0           \n\t"
419                 "ldmia  sp, {r0 - pc}           \n\t"
420                 :
421                 : "r" (kcb->jprobe_saved_regs.ARM_sp),
422                   "I" (sizeof(struct pt_regs) * 2),
423                   "J" (offsetof(struct pt_regs, ARM_sp)),
424                   "J" (offsetof(struct pt_regs, ARM_pc)),
425                   "J" (offsetof(struct pt_regs, ARM_cpsr))
426                 : "memory", "cc");
427 }
428
429 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
430 {
431         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
432         long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
433         long orig_sp = regs->ARM_sp;
434         struct jprobe *jp = container_of(p, struct jprobe, kp);
435
436         if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
437                 if (orig_sp != stack_addr) {
438                         struct pt_regs *saved_regs =
439                                 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
440                         printk("current sp %lx does not match saved sp %lx\n",
441                                orig_sp, stack_addr);
442                         printk("Saved registers for jprobe %p\n", jp);
443                         show_regs(saved_regs);
444                         printk("Current registers\n");
445                         show_regs(regs);
446                         BUG();
447                 }
448                 *regs = kcb->jprobe_saved_regs;
449                 memcpy((void *)stack_addr, kcb->jprobes_stack,
450                        MIN_STACK_SIZE(stack_addr));
451                 preempt_enable_no_resched();
452                 return 1;
453         }
454         return 0;
455 }
456
457 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
458 {
459         return 0;
460 }
461
462 static struct undef_hook kprobes_break_hook = {
463         .instr_mask     = 0xffffffff,
464         .instr_val      = KPROBE_BREAKPOINT_INSTRUCTION,
465         .cpsr_mask      = MODE_MASK,
466         .cpsr_val       = SVC_MODE,
467         .fn             = kprobe_trap_handler,
468 };
469
470 int __init arch_init_kprobes()
471 {
472         arm_kprobe_decode_init();
473         register_undef_hook(&kprobes_break_hook);
474         return 0;
475 }