0e47d3d674277c137aabd1c4abab135e0dc13455
[firefly-linux-kernel-4.4.55.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 #ifdef CONFIG_THUMB2_KERNEL
306                 "bx     lr                      \n\t"
307 #else
308                 "mov    pc, lr                  \n\t"
309 #endif
310                 : : : "memory");
311 }
312
313 /* Called from kretprobe_trampoline */
314 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
315 {
316         struct kretprobe_instance *ri = NULL;
317         struct hlist_head *head, empty_rp;
318         struct hlist_node *node, *tmp;
319         unsigned long flags, orig_ret_address = 0;
320         unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
321
322         INIT_HLIST_HEAD(&empty_rp);
323         kretprobe_hash_lock(current, &head, &flags);
324
325         /*
326          * It is possible to have multiple instances associated with a given
327          * task either because multiple functions in the call path have
328          * a return probe installed on them, and/or more than one return
329          * probe was registered for a target function.
330          *
331          * We can handle this because:
332          *     - instances are always inserted at the head of the list
333          *     - when multiple return probes are registered for the same
334          *       function, the first instance's ret_addr will point to the
335          *       real return address, and all the rest will point to
336          *       kretprobe_trampoline
337          */
338         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
339                 if (ri->task != current)
340                         /* another task is sharing our hash bucket */
341                         continue;
342
343                 if (ri->rp && ri->rp->handler) {
344                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
345                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
346                         ri->rp->handler(ri, regs);
347                         __get_cpu_var(current_kprobe) = NULL;
348                 }
349
350                 orig_ret_address = (unsigned long)ri->ret_addr;
351                 recycle_rp_inst(ri, &empty_rp);
352
353                 if (orig_ret_address != trampoline_address)
354                         /*
355                          * This is the real return address. Any other
356                          * instances associated with this task are for
357                          * other calls deeper on the call stack
358                          */
359                         break;
360         }
361
362         kretprobe_assert(ri, orig_ret_address, trampoline_address);
363         kretprobe_hash_unlock(current, &flags);
364
365         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
366                 hlist_del(&ri->hlist);
367                 kfree(ri);
368         }
369
370         return (void *)orig_ret_address;
371 }
372
373 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
374                                       struct pt_regs *regs)
375 {
376         ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
377
378         /* Replace the return addr with trampoline addr. */
379         regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
380 }
381
382 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
383 {
384         struct jprobe *jp = container_of(p, struct jprobe, kp);
385         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
386         long sp_addr = regs->ARM_sp;
387         long cpsr;
388
389         kcb->jprobe_saved_regs = *regs;
390         memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
391         regs->ARM_pc = (long)jp->entry;
392
393         cpsr = regs->ARM_cpsr | PSR_I_BIT;
394 #ifdef CONFIG_THUMB2_KERNEL
395         /* Set correct Thumb state in cpsr */
396         if (regs->ARM_pc & 1)
397                 cpsr |= PSR_T_BIT;
398         else
399                 cpsr &= ~PSR_T_BIT;
400 #endif
401         regs->ARM_cpsr = cpsr;
402
403         preempt_disable();
404         return 1;
405 }
406
407 void __kprobes jprobe_return(void)
408 {
409         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
410
411         __asm__ __volatile__ (
412                 /*
413                  * Setup an empty pt_regs. Fill SP and PC fields as
414                  * they're needed by longjmp_break_handler.
415                  *
416                  * We allocate some slack between the original SP and start of
417                  * our fabricated regs. To be precise we want to have worst case
418                  * covered which is STMFD with all 16 regs so we allocate 2 *
419                  * sizeof(struct_pt_regs)).
420                  *
421                  * This is to prevent any simulated instruction from writing
422                  * over the regs when they are accessing the stack.
423                  */
424 #ifdef CONFIG_THUMB2_KERNEL
425                 "sub    r0, %0, %1              \n\t"
426                 "mov    sp, r0                  \n\t"
427 #else
428                 "sub    sp, %0, %1              \n\t"
429 #endif
430                 "ldr    r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
431                 "str    %0, [sp, %2]            \n\t"
432                 "str    r0, [sp, %3]            \n\t"
433                 "mov    r0, sp                  \n\t"
434                 "bl     kprobe_handler          \n\t"
435
436                 /*
437                  * Return to the context saved by setjmp_pre_handler
438                  * and restored by longjmp_break_handler.
439                  */
440 #ifdef CONFIG_THUMB2_KERNEL
441                 "ldr    lr, [sp, %2]            \n\t" /* lr = saved sp */
442                 "ldrd   r0, r1, [sp, %5]        \n\t" /* r0,r1 = saved lr,pc */
443                 "ldr    r2, [sp, %4]            \n\t" /* r2 = saved psr */
444                 "stmdb  lr!, {r0, r1, r2}       \n\t" /* push saved lr and */
445                                                       /* rfe context */
446                 "ldmia  sp, {r0 - r12}          \n\t"
447                 "mov    sp, lr                  \n\t"
448                 "ldr    lr, [sp], #4            \n\t"
449                 "rfeia  sp!                     \n\t"
450 #else
451                 "ldr    r0, [sp, %4]            \n\t"
452                 "msr    cpsr_cxsf, r0           \n\t"
453                 "ldmia  sp, {r0 - pc}           \n\t"
454 #endif
455                 :
456                 : "r" (kcb->jprobe_saved_regs.ARM_sp),
457                   "I" (sizeof(struct pt_regs) * 2),
458                   "J" (offsetof(struct pt_regs, ARM_sp)),
459                   "J" (offsetof(struct pt_regs, ARM_pc)),
460                   "J" (offsetof(struct pt_regs, ARM_cpsr)),
461                   "J" (offsetof(struct pt_regs, ARM_lr))
462                 : "memory", "cc");
463 }
464
465 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
466 {
467         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
468         long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
469         long orig_sp = regs->ARM_sp;
470         struct jprobe *jp = container_of(p, struct jprobe, kp);
471
472         if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
473                 if (orig_sp != stack_addr) {
474                         struct pt_regs *saved_regs =
475                                 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
476                         printk("current sp %lx does not match saved sp %lx\n",
477                                orig_sp, stack_addr);
478                         printk("Saved registers for jprobe %p\n", jp);
479                         show_regs(saved_regs);
480                         printk("Current registers\n");
481                         show_regs(regs);
482                         BUG();
483                 }
484                 *regs = kcb->jprobe_saved_regs;
485                 memcpy((void *)stack_addr, kcb->jprobes_stack,
486                        MIN_STACK_SIZE(stack_addr));
487                 preempt_enable_no_resched();
488                 return 1;
489         }
490         return 0;
491 }
492
493 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
494 {
495         return 0;
496 }
497
498 static struct undef_hook kprobes_break_hook = {
499         .instr_mask     = 0xffffffff,
500         .instr_val      = KPROBE_BREAKPOINT_INSTRUCTION,
501         .cpsr_mask      = MODE_MASK,
502         .cpsr_val       = SVC_MODE,
503         .fn             = kprobe_trap_handler,
504 };
505
506 int __init arch_init_kprobes()
507 {
508         arm_kprobe_decode_init();
509         register_undef_hook(&kprobes_break_hook);
510         return 0;
511 }