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