Linux 4.15.10
[linux/fpc-iii.git] / arch / arm / probes / kprobes / core.c
blob52d1cd14fda45d961ece2d0c4d417d986a372de9
1 /*
2 * arch/arm/kernel/kprobes.c
4 * Kprobes on ARM
6 * Abhishek Sagar <sagar.abhishek@gmail.com>
7 * Copyright (C) 2006, 2007 Motorola Inc.
9 * Nicolas Pitre <nico@marvell.com>
10 * Copyright (C) 2007 Marvell Ltd.
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.
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.
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/sched/debug.h>
28 #include <linux/stringify.h>
29 #include <asm/traps.h>
30 #include <asm/opcodes.h>
31 #include <asm/cacheflush.h>
32 #include <linux/percpu.h>
33 #include <linux/bug.h>
34 #include <asm/patch.h>
36 #include "../decode-arm.h"
37 #include "../decode-thumb.h"
38 #include "core.h"
40 #define MIN_STACK_SIZE(addr) \
41 min((unsigned long)MAX_STACK_SIZE, \
42 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
44 #define flush_insns(addr, size) \
45 flush_icache_range((unsigned long)(addr), \
46 (unsigned long)(addr) + \
47 (size))
49 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
50 #define JPROBE_MAGIC_ADDR 0xffffffff
52 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
53 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
56 int __kprobes arch_prepare_kprobe(struct kprobe *p)
58 kprobe_opcode_t insn;
59 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
60 unsigned long addr = (unsigned long)p->addr;
61 bool thumb;
62 kprobe_decode_insn_t *decode_insn;
63 const union decode_action *actions;
64 int is;
65 const struct decode_checker **checkers;
67 if (in_exception_text(addr))
68 return -EINVAL;
70 #ifdef CONFIG_THUMB2_KERNEL
71 thumb = true;
72 addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
73 insn = __mem_to_opcode_thumb16(((u16 *)addr)[0]);
74 if (is_wide_instruction(insn)) {
75 u16 inst2 = __mem_to_opcode_thumb16(((u16 *)addr)[1]);
76 insn = __opcode_thumb32_compose(insn, inst2);
77 decode_insn = thumb32_probes_decode_insn;
78 actions = kprobes_t32_actions;
79 checkers = kprobes_t32_checkers;
80 } else {
81 decode_insn = thumb16_probes_decode_insn;
82 actions = kprobes_t16_actions;
83 checkers = kprobes_t16_checkers;
85 #else /* !CONFIG_THUMB2_KERNEL */
86 thumb = false;
87 if (addr & 0x3)
88 return -EINVAL;
89 insn = __mem_to_opcode_arm(*p->addr);
90 decode_insn = arm_probes_decode_insn;
91 actions = kprobes_arm_actions;
92 checkers = kprobes_arm_checkers;
93 #endif
95 p->opcode = insn;
96 p->ainsn.insn = tmp_insn;
98 switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
99 case INSN_REJECTED: /* not supported */
100 return -EINVAL;
102 case INSN_GOOD: /* instruction uses slot */
103 p->ainsn.insn = get_insn_slot();
104 if (!p->ainsn.insn)
105 return -ENOMEM;
106 for (is = 0; is < MAX_INSN_SIZE; ++is)
107 p->ainsn.insn[is] = tmp_insn[is];
108 flush_insns(p->ainsn.insn,
109 sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
110 p->ainsn.insn_fn = (probes_insn_fn_t *)
111 ((uintptr_t)p->ainsn.insn | thumb);
112 break;
114 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
115 p->ainsn.insn = NULL;
116 break;
120 * Never instrument insn like 'str r0, [sp, +/-r1]'. Also, insn likes
121 * 'str r0, [sp, #-68]' should also be prohibited.
122 * See __und_svc.
124 if ((p->ainsn.stack_space < 0) ||
125 (p->ainsn.stack_space > MAX_STACK_SIZE))
126 return -EINVAL;
128 return 0;
131 void __kprobes arch_arm_kprobe(struct kprobe *p)
133 unsigned int brkp;
134 void *addr;
136 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
137 /* Remove any Thumb flag */
138 addr = (void *)((uintptr_t)p->addr & ~1);
140 if (is_wide_instruction(p->opcode))
141 brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
142 else
143 brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
144 } else {
145 kprobe_opcode_t insn = p->opcode;
147 addr = p->addr;
148 brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
150 if (insn >= 0xe0000000)
151 brkp |= 0xe0000000; /* Unconditional instruction */
152 else
153 brkp |= insn & 0xf0000000; /* Copy condition from insn */
156 patch_text(addr, brkp);
160 * The actual disarming is done here on each CPU and synchronized using
161 * stop_machine. This synchronization is necessary on SMP to avoid removing
162 * a probe between the moment the 'Undefined Instruction' exception is raised
163 * and the moment the exception handler reads the faulting instruction from
164 * memory. It is also needed to atomically set the two half-words of a 32-bit
165 * Thumb breakpoint.
167 struct patch {
168 void *addr;
169 unsigned int insn;
172 static int __kprobes_remove_breakpoint(void *data)
174 struct patch *p = data;
175 __patch_text(p->addr, p->insn);
176 return 0;
179 void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
181 struct patch p = {
182 .addr = addr,
183 .insn = insn,
185 stop_machine_cpuslocked(__kprobes_remove_breakpoint, &p,
186 cpu_online_mask);
189 void __kprobes arch_disarm_kprobe(struct kprobe *p)
191 kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
192 p->opcode);
195 void __kprobes arch_remove_kprobe(struct kprobe *p)
197 if (p->ainsn.insn) {
198 free_insn_slot(p->ainsn.insn, 0);
199 p->ainsn.insn = NULL;
203 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
205 kcb->prev_kprobe.kp = kprobe_running();
206 kcb->prev_kprobe.status = kcb->kprobe_status;
209 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
211 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
212 kcb->kprobe_status = kcb->prev_kprobe.status;
215 static void __kprobes set_current_kprobe(struct kprobe *p)
217 __this_cpu_write(current_kprobe, p);
220 static void __kprobes
221 singlestep_skip(struct kprobe *p, struct pt_regs *regs)
223 #ifdef CONFIG_THUMB2_KERNEL
224 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
225 if (is_wide_instruction(p->opcode))
226 regs->ARM_pc += 4;
227 else
228 regs->ARM_pc += 2;
229 #else
230 regs->ARM_pc += 4;
231 #endif
234 static inline void __kprobes
235 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
237 p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
241 * Called with IRQs disabled. IRQs must remain disabled from that point
242 * all the way until processing this kprobe is complete. The current
243 * kprobes implementation cannot process more than one nested level of
244 * kprobe, and that level is reserved for user kprobe handlers, so we can't
245 * risk encountering a new kprobe in an interrupt handler.
247 void __kprobes kprobe_handler(struct pt_regs *regs)
249 struct kprobe *p, *cur;
250 struct kprobe_ctlblk *kcb;
252 kcb = get_kprobe_ctlblk();
253 cur = kprobe_running();
255 #ifdef CONFIG_THUMB2_KERNEL
257 * First look for a probe which was registered using an address with
258 * bit 0 set, this is the usual situation for pointers to Thumb code.
259 * If not found, fallback to looking for one with bit 0 clear.
261 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
262 if (!p)
263 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
265 #else /* ! CONFIG_THUMB2_KERNEL */
266 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
267 #endif
269 if (p) {
270 if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
272 * Probe hit but conditional execution check failed,
273 * so just skip the instruction and continue as if
274 * nothing had happened.
275 * In this case, we can skip recursing check too.
277 singlestep_skip(p, regs);
278 } else if (cur) {
279 /* Kprobe is pending, so we're recursing. */
280 switch (kcb->kprobe_status) {
281 case KPROBE_HIT_ACTIVE:
282 case KPROBE_HIT_SSDONE:
283 case KPROBE_HIT_SS:
284 /* A pre- or post-handler probe got us here. */
285 kprobes_inc_nmissed_count(p);
286 save_previous_kprobe(kcb);
287 set_current_kprobe(p);
288 kcb->kprobe_status = KPROBE_REENTER;
289 singlestep(p, regs, kcb);
290 restore_previous_kprobe(kcb);
291 break;
292 case KPROBE_REENTER:
293 /* A nested probe was hit in FIQ, it is a BUG */
294 pr_warn("Unrecoverable kprobe detected at %p.\n",
295 p->addr);
296 /* fall through */
297 default:
298 /* impossible cases */
299 BUG();
301 } else {
302 /* Probe hit and conditional execution check ok. */
303 set_current_kprobe(p);
304 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
307 * If we have no pre-handler or it returned 0, we
308 * continue with normal processing. If we have a
309 * pre-handler and it returned non-zero, it prepped
310 * for calling the break_handler below on re-entry,
311 * so get out doing nothing more here.
313 if (!p->pre_handler || !p->pre_handler(p, regs)) {
314 kcb->kprobe_status = KPROBE_HIT_SS;
315 singlestep(p, regs, kcb);
316 if (p->post_handler) {
317 kcb->kprobe_status = KPROBE_HIT_SSDONE;
318 p->post_handler(p, regs, 0);
320 reset_current_kprobe();
323 } else if (cur) {
324 /* We probably hit a jprobe. Call its break handler. */
325 if (cur->break_handler && cur->break_handler(cur, regs)) {
326 kcb->kprobe_status = KPROBE_HIT_SS;
327 singlestep(cur, regs, kcb);
328 if (cur->post_handler) {
329 kcb->kprobe_status = KPROBE_HIT_SSDONE;
330 cur->post_handler(cur, regs, 0);
333 reset_current_kprobe();
334 } else {
336 * The probe was removed and a race is in progress.
337 * There is nothing we can do about it. Let's restart
338 * the instruction. By the time we can restart, the
339 * real instruction will be there.
344 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
346 unsigned long flags;
347 local_irq_save(flags);
348 kprobe_handler(regs);
349 local_irq_restore(flags);
350 return 0;
353 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
355 struct kprobe *cur = kprobe_running();
356 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
358 switch (kcb->kprobe_status) {
359 case KPROBE_HIT_SS:
360 case KPROBE_REENTER:
362 * We are here because the instruction being single
363 * stepped caused a page fault. We reset the current
364 * kprobe and the PC to point back to the probe address
365 * and allow the page fault handler to continue as a
366 * normal page fault.
368 regs->ARM_pc = (long)cur->addr;
369 if (kcb->kprobe_status == KPROBE_REENTER) {
370 restore_previous_kprobe(kcb);
371 } else {
372 reset_current_kprobe();
374 break;
376 case KPROBE_HIT_ACTIVE:
377 case KPROBE_HIT_SSDONE:
379 * We increment the nmissed count for accounting,
380 * we can also use npre/npostfault count for accounting
381 * these specific fault cases.
383 kprobes_inc_nmissed_count(cur);
386 * We come here because instructions in the pre/post
387 * handler caused the page_fault, this could happen
388 * if handler tries to access user space by
389 * copy_from_user(), get_user() etc. Let the
390 * user-specified handler try to fix it.
392 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
393 return 1;
394 break;
396 default:
397 break;
400 return 0;
403 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
404 unsigned long val, void *data)
407 * notify_die() is currently never called on ARM,
408 * so this callback is currently empty.
410 return NOTIFY_DONE;
414 * When a retprobed function returns, trampoline_handler() is called,
415 * calling the kretprobe's handler. We construct a struct pt_regs to
416 * give a view of registers r0-r11 to the user return-handler. This is
417 * not a complete pt_regs structure, but that should be plenty sufficient
418 * for kretprobe handlers which should normally be interested in r0 only
419 * anyway.
421 void __naked __kprobes kretprobe_trampoline(void)
423 __asm__ __volatile__ (
424 "stmdb sp!, {r0 - r11} \n\t"
425 "mov r0, sp \n\t"
426 "bl trampoline_handler \n\t"
427 "mov lr, r0 \n\t"
428 "ldmia sp!, {r0 - r11} \n\t"
429 #ifdef CONFIG_THUMB2_KERNEL
430 "bx lr \n\t"
431 #else
432 "mov pc, lr \n\t"
433 #endif
434 : : : "memory");
437 /* Called from kretprobe_trampoline */
438 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
440 struct kretprobe_instance *ri = NULL;
441 struct hlist_head *head, empty_rp;
442 struct hlist_node *tmp;
443 unsigned long flags, orig_ret_address = 0;
444 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
445 kprobe_opcode_t *correct_ret_addr = NULL;
447 INIT_HLIST_HEAD(&empty_rp);
448 kretprobe_hash_lock(current, &head, &flags);
451 * It is possible to have multiple instances associated with a given
452 * task either because multiple functions in the call path have
453 * a return probe installed on them, and/or more than one return
454 * probe was registered for a target function.
456 * We can handle this because:
457 * - instances are always inserted at the head of the list
458 * - when multiple return probes are registered for the same
459 * function, the first instance's ret_addr will point to the
460 * real return address, and all the rest will point to
461 * kretprobe_trampoline
463 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
464 if (ri->task != current)
465 /* another task is sharing our hash bucket */
466 continue;
468 orig_ret_address = (unsigned long)ri->ret_addr;
470 if (orig_ret_address != trampoline_address)
472 * This is the real return address. Any other
473 * instances associated with this task are for
474 * other calls deeper on the call stack
476 break;
479 kretprobe_assert(ri, orig_ret_address, trampoline_address);
481 correct_ret_addr = ri->ret_addr;
482 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
483 if (ri->task != current)
484 /* another task is sharing our hash bucket */
485 continue;
487 orig_ret_address = (unsigned long)ri->ret_addr;
488 if (ri->rp && ri->rp->handler) {
489 __this_cpu_write(current_kprobe, &ri->rp->kp);
490 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
491 ri->ret_addr = correct_ret_addr;
492 ri->rp->handler(ri, regs);
493 __this_cpu_write(current_kprobe, NULL);
496 recycle_rp_inst(ri, &empty_rp);
498 if (orig_ret_address != trampoline_address)
500 * This is the real return address. Any other
501 * instances associated with this task are for
502 * other calls deeper on the call stack
504 break;
507 kretprobe_hash_unlock(current, &flags);
509 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
510 hlist_del(&ri->hlist);
511 kfree(ri);
514 return (void *)orig_ret_address;
517 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
518 struct pt_regs *regs)
520 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
522 /* Replace the return addr with trampoline addr. */
523 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
526 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
528 struct jprobe *jp = container_of(p, struct jprobe, kp);
529 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
530 long sp_addr = regs->ARM_sp;
531 long cpsr;
533 kcb->jprobe_saved_regs = *regs;
534 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
535 regs->ARM_pc = (long)jp->entry;
537 cpsr = regs->ARM_cpsr | PSR_I_BIT;
538 #ifdef CONFIG_THUMB2_KERNEL
539 /* Set correct Thumb state in cpsr */
540 if (regs->ARM_pc & 1)
541 cpsr |= PSR_T_BIT;
542 else
543 cpsr &= ~PSR_T_BIT;
544 #endif
545 regs->ARM_cpsr = cpsr;
547 preempt_disable();
548 return 1;
551 void __kprobes jprobe_return(void)
553 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
555 __asm__ __volatile__ (
557 * Setup an empty pt_regs. Fill SP and PC fields as
558 * they're needed by longjmp_break_handler.
560 * We allocate some slack between the original SP and start of
561 * our fabricated regs. To be precise we want to have worst case
562 * covered which is STMFD with all 16 regs so we allocate 2 *
563 * sizeof(struct_pt_regs)).
565 * This is to prevent any simulated instruction from writing
566 * over the regs when they are accessing the stack.
568 #ifdef CONFIG_THUMB2_KERNEL
569 "sub r0, %0, %1 \n\t"
570 "mov sp, r0 \n\t"
571 #else
572 "sub sp, %0, %1 \n\t"
573 #endif
574 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
575 "str %0, [sp, %2] \n\t"
576 "str r0, [sp, %3] \n\t"
577 "mov r0, sp \n\t"
578 "bl kprobe_handler \n\t"
581 * Return to the context saved by setjmp_pre_handler
582 * and restored by longjmp_break_handler.
584 #ifdef CONFIG_THUMB2_KERNEL
585 "ldr lr, [sp, %2] \n\t" /* lr = saved sp */
586 "ldrd r0, r1, [sp, %5] \n\t" /* r0,r1 = saved lr,pc */
587 "ldr r2, [sp, %4] \n\t" /* r2 = saved psr */
588 "stmdb lr!, {r0, r1, r2} \n\t" /* push saved lr and */
589 /* rfe context */
590 "ldmia sp, {r0 - r12} \n\t"
591 "mov sp, lr \n\t"
592 "ldr lr, [sp], #4 \n\t"
593 "rfeia sp! \n\t"
594 #else
595 "ldr r0, [sp, %4] \n\t"
596 "msr cpsr_cxsf, r0 \n\t"
597 "ldmia sp, {r0 - pc} \n\t"
598 #endif
600 : "r" (kcb->jprobe_saved_regs.ARM_sp),
601 "I" (sizeof(struct pt_regs) * 2),
602 "J" (offsetof(struct pt_regs, ARM_sp)),
603 "J" (offsetof(struct pt_regs, ARM_pc)),
604 "J" (offsetof(struct pt_regs, ARM_cpsr)),
605 "J" (offsetof(struct pt_regs, ARM_lr))
606 : "memory", "cc");
609 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
611 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
612 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
613 long orig_sp = regs->ARM_sp;
614 struct jprobe *jp = container_of(p, struct jprobe, kp);
616 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
617 if (orig_sp != stack_addr) {
618 struct pt_regs *saved_regs =
619 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
620 printk("current sp %lx does not match saved sp %lx\n",
621 orig_sp, stack_addr);
622 printk("Saved registers for jprobe %p\n", jp);
623 show_regs(saved_regs);
624 printk("Current registers\n");
625 show_regs(regs);
626 BUG();
628 *regs = kcb->jprobe_saved_regs;
629 memcpy((void *)stack_addr, kcb->jprobes_stack,
630 MIN_STACK_SIZE(stack_addr));
631 preempt_enable_no_resched();
632 return 1;
634 return 0;
637 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
639 return 0;
642 #ifdef CONFIG_THUMB2_KERNEL
644 static struct undef_hook kprobes_thumb16_break_hook = {
645 .instr_mask = 0xffff,
646 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
647 .cpsr_mask = MODE_MASK,
648 .cpsr_val = SVC_MODE,
649 .fn = kprobe_trap_handler,
652 static struct undef_hook kprobes_thumb32_break_hook = {
653 .instr_mask = 0xffffffff,
654 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
655 .cpsr_mask = MODE_MASK,
656 .cpsr_val = SVC_MODE,
657 .fn = kprobe_trap_handler,
660 #else /* !CONFIG_THUMB2_KERNEL */
662 static struct undef_hook kprobes_arm_break_hook = {
663 .instr_mask = 0x0fffffff,
664 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
665 .cpsr_mask = MODE_MASK,
666 .cpsr_val = SVC_MODE,
667 .fn = kprobe_trap_handler,
670 #endif /* !CONFIG_THUMB2_KERNEL */
672 int __init arch_init_kprobes()
674 arm_probes_decode_init();
675 #ifdef CONFIG_THUMB2_KERNEL
676 register_undef_hook(&kprobes_thumb16_break_hook);
677 register_undef_hook(&kprobes_thumb32_break_hook);
678 #else
679 register_undef_hook(&kprobes_arm_break_hook);
680 #endif
681 return 0;