2 * arch/arm/kernel/kprobes.c
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/stringify.h>
28 #include <asm/traps.h>
29 #include <asm/opcodes.h>
30 #include <asm/cacheflush.h>
35 #define MIN_STACK_SIZE(addr) \
36 min((unsigned long)MAX_STACK_SIZE, \
37 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
39 #define flush_insns(addr, size) \
40 flush_icache_range((unsigned long)(addr), \
41 (unsigned long)(addr) + \
44 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
45 #define JPROBE_MAGIC_ADDR 0xffffffff
47 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
48 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
51 int __kprobes arch_prepare_kprobe(struct kprobe *p)
54 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
55 unsigned long addr = (unsigned long)p->addr;
57 kprobe_decode_insn_t *decode_insn;
60 if (in_exception_text(addr))
63 #ifdef CONFIG_THUMB2_KERNEL
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;
72 decode_insn = thumb16_kprobe_decode_insn;
73 #else /* !CONFIG_THUMB2_KERNEL */
77 insn = __mem_to_opcode_arm(*p->addr);
78 decode_insn = arm_kprobe_decode_insn;
82 p->ainsn.insn = tmp_insn;
84 switch ((*decode_insn)(insn, &p->ainsn)) {
85 case INSN_REJECTED: /* not supported */
88 case INSN_GOOD: /* instruction uses slot */
89 p->ainsn.insn = get_insn_slot();
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);
100 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
101 p->ainsn.insn = NULL;
108 void __kprobes arch_arm_kprobe(struct kprobe *p)
113 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
114 /* Remove any Thumb flag */
115 addr = (void *)((uintptr_t)p->addr & ~1);
117 if (is_wide_instruction(p->opcode))
118 brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
120 brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
122 kprobe_opcode_t insn = p->opcode;
125 brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
127 if (insn >= 0xe0000000)
128 brkp |= 0xe0000000; /* Unconditional instruction */
130 brkp |= insn & 0xf0000000; /* Copy condition from insn */
133 patch_text(addr, brkp);
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
144 int __kprobes __arch_disarm_kprobe(void *p)
146 struct kprobe *kp = p;
147 void *addr = (void *)((uintptr_t)kp->addr & ~1);
149 __patch_text(addr, kp->opcode);
154 void __kprobes arch_disarm_kprobe(struct kprobe *p)
156 stop_machine(__arch_disarm_kprobe, p, cpu_online_mask);
159 void __kprobes arch_remove_kprobe(struct kprobe *p)
162 free_insn_slot(p->ainsn.insn, 0);
163 p->ainsn.insn = NULL;
167 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
169 kcb->prev_kprobe.kp = kprobe_running();
170 kcb->prev_kprobe.status = kcb->kprobe_status;
173 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
175 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
176 kcb->kprobe_status = kcb->prev_kprobe.status;
179 static void __kprobes set_current_kprobe(struct kprobe *p)
181 __get_cpu_var(current_kprobe) = p;
184 static void __kprobes
185 singlestep_skip(struct kprobe *p, struct pt_regs *regs)
187 #ifdef CONFIG_THUMB2_KERNEL
188 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
189 if (is_wide_instruction(p->opcode))
198 static inline void __kprobes
199 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
201 p->ainsn.insn_singlestep(p, regs);
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.
211 void __kprobes kprobe_handler(struct pt_regs *regs)
213 struct kprobe *p, *cur;
214 struct kprobe_ctlblk *kcb;
216 kcb = get_kprobe_ctlblk();
217 cur = kprobe_running();
219 #ifdef CONFIG_THUMB2_KERNEL
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.
225 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
227 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
229 #else /* ! CONFIG_THUMB2_KERNEL */
230 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
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);
248 /* impossible cases */
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;
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.
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);
270 reset_current_kprobe();
274 * Probe hit but conditional execution check failed,
275 * so just skip the instruction and continue as if
276 * nothing had happened.
278 singlestep_skip(p, regs);
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);
290 reset_current_kprobe();
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.
301 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
304 local_irq_save(flags);
305 kprobe_handler(regs);
306 local_irq_restore(flags);
310 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
312 struct kprobe *cur = kprobe_running();
313 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
315 switch (kcb->kprobe_status) {
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
325 regs->ARM_pc = (long)cur->addr;
326 if (kcb->kprobe_status == KPROBE_REENTER) {
327 restore_previous_kprobe(kcb);
329 reset_current_kprobe();
333 case KPROBE_HIT_ACTIVE:
334 case KPROBE_HIT_SSDONE:
336 * We increment the nmissed count for accounting,
337 * we can also use npre/npostfault count for accounting
338 * these specific fault cases.
340 kprobes_inc_nmissed_count(cur);
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.
349 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
360 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
361 unsigned long val, void *data)
364 * notify_die() is currently never called on ARM,
365 * so this callback is currently empty.
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
378 void __naked __kprobes kretprobe_trampoline(void)
380 __asm__ __volatile__ (
381 "stmdb sp!, {r0 - r11} \n\t"
383 "bl trampoline_handler \n\t"
385 "ldmia sp!, {r0 - r11} \n\t"
386 #ifdef CONFIG_THUMB2_KERNEL
394 /* Called from kretprobe_trampoline */
395 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
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;
403 INIT_HLIST_HEAD(&empty_rp);
404 kretprobe_hash_lock(current, &head, &flags);
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.
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
419 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
420 if (ri->task != current)
421 /* another task is sharing our hash bucket */
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;
431 orig_ret_address = (unsigned long)ri->ret_addr;
432 recycle_rp_inst(ri, &empty_rp);
434 if (orig_ret_address != trampoline_address)
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
443 kretprobe_assert(ri, orig_ret_address, trampoline_address);
444 kretprobe_hash_unlock(current, &flags);
446 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
447 hlist_del(&ri->hlist);
451 return (void *)orig_ret_address;
454 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
455 struct pt_regs *regs)
457 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
459 /* Replace the return addr with trampoline addr. */
460 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
463 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
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;
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;
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)
482 regs->ARM_cpsr = cpsr;
488 void __kprobes jprobe_return(void)
490 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
492 __asm__ __volatile__ (
494 * Setup an empty pt_regs. Fill SP and PC fields as
495 * they're needed by longjmp_break_handler.
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)).
502 * This is to prevent any simulated instruction from writing
503 * over the regs when they are accessing the stack.
505 #ifdef CONFIG_THUMB2_KERNEL
506 "sub r0, %0, %1 \n\t"
509 "sub sp, %0, %1 \n\t"
511 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
512 "str %0, [sp, %2] \n\t"
513 "str r0, [sp, %3] \n\t"
515 "bl kprobe_handler \n\t"
518 * Return to the context saved by setjmp_pre_handler
519 * and restored by longjmp_break_handler.
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 */
527 "ldmia sp, {r0 - r12} \n\t"
529 "ldr lr, [sp], #4 \n\t"
532 "ldr r0, [sp, %4] \n\t"
533 "msr cpsr_cxsf, r0 \n\t"
534 "ldmia sp, {r0 - pc} \n\t"
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))
546 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
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);
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");
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();
574 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
579 #ifdef CONFIG_THUMB2_KERNEL
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,
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,
597 #else /* !CONFIG_THUMB2_KERNEL */
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,
607 #endif /* !CONFIG_THUMB2_KERNEL */
609 int __init arch_init_kprobes()
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);
616 register_undef_hook(&kprobes_arm_break_hook);