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