ARM: kprobes: Add Thumb breakpoint support
[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, size)                         \
38         flush_icache_range((unsigned long)(addr),       \
39                            (unsigned long)(addr) +      \
40                            (size))
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         kprobe_decode_insn_t *decode_insn;
55         int is;
56
57         if (in_exception_text(addr))
58                 return -EINVAL;
59
60 #ifdef CONFIG_THUMB2_KERNEL
61         addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
62         insn = ((u16 *)addr)[0];
63         if (is_wide_instruction(insn)) {
64                 insn <<= 16;
65                 insn |= ((u16 *)addr)[1];
66                 decode_insn = thumb32_kprobe_decode_insn;
67         } else
68                 decode_insn = thumb16_kprobe_decode_insn;
69 #else /* !CONFIG_THUMB2_KERNEL */
70         if (addr & 0x3)
71                 return -EINVAL;
72         insn = *p->addr;
73         decode_insn = arm_kprobe_decode_insn;
74 #endif
75
76         p->opcode = insn;
77         p->ainsn.insn = tmp_insn;
78
79         switch ((*decode_insn)(insn, &p->ainsn)) {
80         case INSN_REJECTED:     /* not supported */
81                 return -EINVAL;
82
83         case INSN_GOOD:         /* instruction uses slot */
84                 p->ainsn.insn = get_insn_slot();
85                 if (!p->ainsn.insn)
86                         return -ENOMEM;
87                 for (is = 0; is < MAX_INSN_SIZE; ++is)
88                         p->ainsn.insn[is] = tmp_insn[is];
89                 flush_insns(p->ainsn.insn,
90                                 sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
91                 break;
92
93         case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
94                 p->ainsn.insn = NULL;
95                 break;
96         }
97
98         return 0;
99 }
100
101 #ifdef CONFIG_THUMB2_KERNEL
102
103 /*
104  * For a 32-bit Thumb breakpoint spanning two memory words we need to take
105  * special precautions to insert the breakpoint atomically, especially on SMP
106  * systems. This is achieved by calling this arming function using stop_machine.
107  */
108 static int __kprobes set_t32_breakpoint(void *addr)
109 {
110         ((u16 *)addr)[0] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION >> 16;
111         ((u16 *)addr)[1] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION & 0xffff;
112         flush_insns(addr, 2*sizeof(u16));
113         return 0;
114 }
115
116 void __kprobes arch_arm_kprobe(struct kprobe *p)
117 {
118         uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
119
120         if (!is_wide_instruction(p->opcode)) {
121                 *(u16 *)addr = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
122                 flush_insns(addr, sizeof(u16));
123         } else if (addr & 2) {
124                 /* A 32-bit instruction spanning two words needs special care */
125                 stop_machine(set_t32_breakpoint, (void *)addr, &cpu_online_map);
126         } else {
127                 /* Word aligned 32-bit instruction can be written atomically */
128                 u32 bkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
129 #ifndef __ARMEB__ /* Swap halfwords for little-endian */
130                 bkp = (bkp >> 16) | (bkp << 16);
131 #endif
132                 *(u32 *)addr = bkp;
133                 flush_insns(addr, sizeof(u32));
134         }
135 }
136
137 #else /* !CONFIG_THUMB2_KERNEL */
138
139 void __kprobes arch_arm_kprobe(struct kprobe *p)
140 {
141         *p->addr = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
142         flush_insns(p->addr, sizeof(p->addr[0]));
143 }
144
145 #endif /* !CONFIG_THUMB2_KERNEL */
146
147 /*
148  * The actual disarming is done here on each CPU and synchronized using
149  * stop_machine. This synchronization is necessary on SMP to avoid removing
150  * a probe between the moment the 'Undefined Instruction' exception is raised
151  * and the moment the exception handler reads the faulting instruction from
152  * memory. It is also needed to atomically set the two half-words of a 32-bit
153  * Thumb breakpoint.
154  */
155 int __kprobes __arch_disarm_kprobe(void *p)
156 {
157         struct kprobe *kp = p;
158 #ifdef CONFIG_THUMB2_KERNEL
159         u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
160         kprobe_opcode_t insn = kp->opcode;
161         unsigned int len;
162
163         if (is_wide_instruction(insn)) {
164                 ((u16 *)addr)[0] = insn>>16;
165                 ((u16 *)addr)[1] = insn;
166                 len = 2*sizeof(u16);
167         } else {
168                 ((u16 *)addr)[0] = insn;
169                 len = sizeof(u16);
170         }
171         flush_insns(addr, len);
172
173 #else /* !CONFIG_THUMB2_KERNEL */
174         *kp->addr = kp->opcode;
175         flush_insns(kp->addr, sizeof(kp->addr[0]));
176 #endif
177         return 0;
178 }
179
180 void __kprobes arch_disarm_kprobe(struct kprobe *p)
181 {
182         stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
183 }
184
185 void __kprobes arch_remove_kprobe(struct kprobe *p)
186 {
187         if (p->ainsn.insn) {
188                 free_insn_slot(p->ainsn.insn, 0);
189                 p->ainsn.insn = NULL;
190         }
191 }
192
193 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
194 {
195         kcb->prev_kprobe.kp = kprobe_running();
196         kcb->prev_kprobe.status = kcb->kprobe_status;
197 }
198
199 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
200 {
201         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
202         kcb->kprobe_status = kcb->prev_kprobe.status;
203 }
204
205 static void __kprobes set_current_kprobe(struct kprobe *p)
206 {
207         __get_cpu_var(current_kprobe) = p;
208 }
209
210 static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
211                                  struct kprobe_ctlblk *kcb)
212 {
213         regs->ARM_pc += 4;
214         if (p->ainsn.insn_check_cc(regs->ARM_cpsr))
215                 p->ainsn.insn_handler(p, regs);
216 }
217
218 /*
219  * Called with IRQs disabled. IRQs must remain disabled from that point
220  * all the way until processing this kprobe is complete.  The current
221  * kprobes implementation cannot process more than one nested level of
222  * kprobe, and that level is reserved for user kprobe handlers, so we can't
223  * risk encountering a new kprobe in an interrupt handler.
224  */
225 void __kprobes kprobe_handler(struct pt_regs *regs)
226 {
227         struct kprobe *p, *cur;
228         struct kprobe_ctlblk *kcb;
229
230         kcb = get_kprobe_ctlblk();
231         cur = kprobe_running();
232
233 #ifdef CONFIG_THUMB2_KERNEL
234         /*
235          * First look for a probe which was registered using an address with
236          * bit 0 set, this is the usual situation for pointers to Thumb code.
237          * If not found, fallback to looking for one with bit 0 clear.
238          */
239         p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
240         if (!p)
241                 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
242
243 #else /* ! CONFIG_THUMB2_KERNEL */
244         p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
245 #endif
246
247         if (p) {
248                 if (cur) {
249                         /* Kprobe is pending, so we're recursing. */
250                         switch (kcb->kprobe_status) {
251                         case KPROBE_HIT_ACTIVE:
252                         case KPROBE_HIT_SSDONE:
253                                 /* A pre- or post-handler probe got us here. */
254                                 kprobes_inc_nmissed_count(p);
255                                 save_previous_kprobe(kcb);
256                                 set_current_kprobe(p);
257                                 kcb->kprobe_status = KPROBE_REENTER;
258                                 singlestep(p, regs, kcb);
259                                 restore_previous_kprobe(kcb);
260                                 break;
261                         default:
262                                 /* impossible cases */
263                                 BUG();
264                         }
265                 } else {
266                         set_current_kprobe(p);
267                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
268
269                         /*
270                          * If we have no pre-handler or it returned 0, we
271                          * continue with normal processing.  If we have a
272                          * pre-handler and it returned non-zero, it prepped
273                          * for calling the break_handler below on re-entry,
274                          * so get out doing nothing more here.
275                          */
276                         if (!p->pre_handler || !p->pre_handler(p, regs)) {
277                                 kcb->kprobe_status = KPROBE_HIT_SS;
278                                 singlestep(p, regs, kcb);
279                                 if (p->post_handler) {
280                                         kcb->kprobe_status = KPROBE_HIT_SSDONE;
281                                         p->post_handler(p, regs, 0);
282                                 }
283                                 reset_current_kprobe();
284                         }
285                 }
286         } else if (cur) {
287                 /* We probably hit a jprobe.  Call its break handler. */
288                 if (cur->break_handler && cur->break_handler(cur, regs)) {
289                         kcb->kprobe_status = KPROBE_HIT_SS;
290                         singlestep(cur, regs, kcb);
291                         if (cur->post_handler) {
292                                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
293                                 cur->post_handler(cur, regs, 0);
294                         }
295                 }
296                 reset_current_kprobe();
297         } else {
298                 /*
299                  * The probe was removed and a race is in progress.
300                  * There is nothing we can do about it.  Let's restart
301                  * the instruction.  By the time we can restart, the
302                  * real instruction will be there.
303                  */
304         }
305 }
306
307 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
308 {
309         unsigned long flags;
310         local_irq_save(flags);
311         kprobe_handler(regs);
312         local_irq_restore(flags);
313         return 0;
314 }
315
316 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
317 {
318         struct kprobe *cur = kprobe_running();
319         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
320
321         switch (kcb->kprobe_status) {
322         case KPROBE_HIT_SS:
323         case KPROBE_REENTER:
324                 /*
325                  * We are here because the instruction being single
326                  * stepped caused a page fault. We reset the current
327                  * kprobe and the PC to point back to the probe address
328                  * and allow the page fault handler to continue as a
329                  * normal page fault.
330                  */
331                 regs->ARM_pc = (long)cur->addr;
332                 if (kcb->kprobe_status == KPROBE_REENTER) {
333                         restore_previous_kprobe(kcb);
334                 } else {
335                         reset_current_kprobe();
336                 }
337                 break;
338
339         case KPROBE_HIT_ACTIVE:
340         case KPROBE_HIT_SSDONE:
341                 /*
342                  * We increment the nmissed count for accounting,
343                  * we can also use npre/npostfault count for accounting
344                  * these specific fault cases.
345                  */
346                 kprobes_inc_nmissed_count(cur);
347
348                 /*
349                  * We come here because instructions in the pre/post
350                  * handler caused the page_fault, this could happen
351                  * if handler tries to access user space by
352                  * copy_from_user(), get_user() etc. Let the
353                  * user-specified handler try to fix it.
354                  */
355                 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
356                         return 1;
357                 break;
358
359         default:
360                 break;
361         }
362
363         return 0;
364 }
365
366 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
367                                        unsigned long val, void *data)
368 {
369         /*
370          * notify_die() is currently never called on ARM,
371          * so this callback is currently empty.
372          */
373         return NOTIFY_DONE;
374 }
375
376 /*
377  * When a retprobed function returns, trampoline_handler() is called,
378  * calling the kretprobe's handler. We construct a struct pt_regs to
379  * give a view of registers r0-r11 to the user return-handler.  This is
380  * not a complete pt_regs structure, but that should be plenty sufficient
381  * for kretprobe handlers which should normally be interested in r0 only
382  * anyway.
383  */
384 void __naked __kprobes kretprobe_trampoline(void)
385 {
386         __asm__ __volatile__ (
387                 "stmdb  sp!, {r0 - r11}         \n\t"
388                 "mov    r0, sp                  \n\t"
389                 "bl     trampoline_handler      \n\t"
390                 "mov    lr, r0                  \n\t"
391                 "ldmia  sp!, {r0 - r11}         \n\t"
392 #ifdef CONFIG_THUMB2_KERNEL
393                 "bx     lr                      \n\t"
394 #else
395                 "mov    pc, lr                  \n\t"
396 #endif
397                 : : : "memory");
398 }
399
400 /* Called from kretprobe_trampoline */
401 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
402 {
403         struct kretprobe_instance *ri = NULL;
404         struct hlist_head *head, empty_rp;
405         struct hlist_node *node, *tmp;
406         unsigned long flags, orig_ret_address = 0;
407         unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
408
409         INIT_HLIST_HEAD(&empty_rp);
410         kretprobe_hash_lock(current, &head, &flags);
411
412         /*
413          * It is possible to have multiple instances associated with a given
414          * task either because multiple functions in the call path have
415          * a return probe installed on them, and/or more than one return
416          * probe was registered for a target function.
417          *
418          * We can handle this because:
419          *     - instances are always inserted at the head of the list
420          *     - when multiple return probes are registered for the same
421          *       function, the first instance's ret_addr will point to the
422          *       real return address, and all the rest will point to
423          *       kretprobe_trampoline
424          */
425         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
426                 if (ri->task != current)
427                         /* another task is sharing our hash bucket */
428                         continue;
429
430                 if (ri->rp && ri->rp->handler) {
431                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
432                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
433                         ri->rp->handler(ri, regs);
434                         __get_cpu_var(current_kprobe) = NULL;
435                 }
436
437                 orig_ret_address = (unsigned long)ri->ret_addr;
438                 recycle_rp_inst(ri, &empty_rp);
439
440                 if (orig_ret_address != trampoline_address)
441                         /*
442                          * This is the real return address. Any other
443                          * instances associated with this task are for
444                          * other calls deeper on the call stack
445                          */
446                         break;
447         }
448
449         kretprobe_assert(ri, orig_ret_address, trampoline_address);
450         kretprobe_hash_unlock(current, &flags);
451
452         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
453                 hlist_del(&ri->hlist);
454                 kfree(ri);
455         }
456
457         return (void *)orig_ret_address;
458 }
459
460 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
461                                       struct pt_regs *regs)
462 {
463         ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
464
465         /* Replace the return addr with trampoline addr. */
466         regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
467 }
468
469 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
470 {
471         struct jprobe *jp = container_of(p, struct jprobe, kp);
472         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
473         long sp_addr = regs->ARM_sp;
474         long cpsr;
475
476         kcb->jprobe_saved_regs = *regs;
477         memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
478         regs->ARM_pc = (long)jp->entry;
479
480         cpsr = regs->ARM_cpsr | PSR_I_BIT;
481 #ifdef CONFIG_THUMB2_KERNEL
482         /* Set correct Thumb state in cpsr */
483         if (regs->ARM_pc & 1)
484                 cpsr |= PSR_T_BIT;
485         else
486                 cpsr &= ~PSR_T_BIT;
487 #endif
488         regs->ARM_cpsr = cpsr;
489
490         preempt_disable();
491         return 1;
492 }
493
494 void __kprobes jprobe_return(void)
495 {
496         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
497
498         __asm__ __volatile__ (
499                 /*
500                  * Setup an empty pt_regs. Fill SP and PC fields as
501                  * they're needed by longjmp_break_handler.
502                  *
503                  * We allocate some slack between the original SP and start of
504                  * our fabricated regs. To be precise we want to have worst case
505                  * covered which is STMFD with all 16 regs so we allocate 2 *
506                  * sizeof(struct_pt_regs)).
507                  *
508                  * This is to prevent any simulated instruction from writing
509                  * over the regs when they are accessing the stack.
510                  */
511 #ifdef CONFIG_THUMB2_KERNEL
512                 "sub    r0, %0, %1              \n\t"
513                 "mov    sp, r0                  \n\t"
514 #else
515                 "sub    sp, %0, %1              \n\t"
516 #endif
517                 "ldr    r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
518                 "str    %0, [sp, %2]            \n\t"
519                 "str    r0, [sp, %3]            \n\t"
520                 "mov    r0, sp                  \n\t"
521                 "bl     kprobe_handler          \n\t"
522
523                 /*
524                  * Return to the context saved by setjmp_pre_handler
525                  * and restored by longjmp_break_handler.
526                  */
527 #ifdef CONFIG_THUMB2_KERNEL
528                 "ldr    lr, [sp, %2]            \n\t" /* lr = saved sp */
529                 "ldrd   r0, r1, [sp, %5]        \n\t" /* r0,r1 = saved lr,pc */
530                 "ldr    r2, [sp, %4]            \n\t" /* r2 = saved psr */
531                 "stmdb  lr!, {r0, r1, r2}       \n\t" /* push saved lr and */
532                                                       /* rfe context */
533                 "ldmia  sp, {r0 - r12}          \n\t"
534                 "mov    sp, lr                  \n\t"
535                 "ldr    lr, [sp], #4            \n\t"
536                 "rfeia  sp!                     \n\t"
537 #else
538                 "ldr    r0, [sp, %4]            \n\t"
539                 "msr    cpsr_cxsf, r0           \n\t"
540                 "ldmia  sp, {r0 - pc}           \n\t"
541 #endif
542                 :
543                 : "r" (kcb->jprobe_saved_regs.ARM_sp),
544                   "I" (sizeof(struct pt_regs) * 2),
545                   "J" (offsetof(struct pt_regs, ARM_sp)),
546                   "J" (offsetof(struct pt_regs, ARM_pc)),
547                   "J" (offsetof(struct pt_regs, ARM_cpsr)),
548                   "J" (offsetof(struct pt_regs, ARM_lr))
549                 : "memory", "cc");
550 }
551
552 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
553 {
554         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
555         long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
556         long orig_sp = regs->ARM_sp;
557         struct jprobe *jp = container_of(p, struct jprobe, kp);
558
559         if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
560                 if (orig_sp != stack_addr) {
561                         struct pt_regs *saved_regs =
562                                 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
563                         printk("current sp %lx does not match saved sp %lx\n",
564                                orig_sp, stack_addr);
565                         printk("Saved registers for jprobe %p\n", jp);
566                         show_regs(saved_regs);
567                         printk("Current registers\n");
568                         show_regs(regs);
569                         BUG();
570                 }
571                 *regs = kcb->jprobe_saved_regs;
572                 memcpy((void *)stack_addr, kcb->jprobes_stack,
573                        MIN_STACK_SIZE(stack_addr));
574                 preempt_enable_no_resched();
575                 return 1;
576         }
577         return 0;
578 }
579
580 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
581 {
582         return 0;
583 }
584
585 #ifdef CONFIG_THUMB2_KERNEL
586
587 static struct undef_hook kprobes_thumb16_break_hook = {
588         .instr_mask     = 0xffff,
589         .instr_val      = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
590         .cpsr_mask      = MODE_MASK,
591         .cpsr_val       = SVC_MODE,
592         .fn             = kprobe_trap_handler,
593 };
594
595 static struct undef_hook kprobes_thumb32_break_hook = {
596         .instr_mask     = 0xffffffff,
597         .instr_val      = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
598         .cpsr_mask      = MODE_MASK,
599         .cpsr_val       = SVC_MODE,
600         .fn             = kprobe_trap_handler,
601 };
602
603 #else  /* !CONFIG_THUMB2_KERNEL */
604
605 static struct undef_hook kprobes_arm_break_hook = {
606         .instr_mask     = 0xffffffff,
607         .instr_val      = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
608         .cpsr_mask      = MODE_MASK,
609         .cpsr_val       = SVC_MODE,
610         .fn             = kprobe_trap_handler,
611 };
612
613 #endif /* !CONFIG_THUMB2_KERNEL */
614
615 int __init arch_init_kprobes()
616 {
617         arm_kprobe_decode_init();
618 #ifdef CONFIG_THUMB2_KERNEL
619         register_undef_hook(&kprobes_thumb16_break_hook);
620         register_undef_hook(&kprobes_thumb32_break_hook);
621 #else
622         register_undef_hook(&kprobes_arm_break_hook);
623 #endif
624         return 0;
625 }