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