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/cacheflush.h>
33 #define MIN_STACK_SIZE(addr) \
34 min((unsigned long)MAX_STACK_SIZE, \
35 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
37 #define flush_insns(addr, size) \
38 flush_icache_range((unsigned long)(addr), \
39 (unsigned long)(addr) + \
42 /* Used as a marker in ARM_pc to note when we're in a jprobe. */
43 #define JPROBE_MAGIC_ADDR 0xffffffff
45 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
46 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
49 int __kprobes arch_prepare_kprobe(struct kprobe *p)
52 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
53 unsigned long addr = (unsigned long)p->addr;
54 kprobe_decode_insn_t *decode_insn;
57 if (in_exception_text(addr))
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)) {
65 insn |= ((u16 *)addr)[1];
66 decode_insn = thumb32_kprobe_decode_insn;
68 decode_insn = thumb16_kprobe_decode_insn;
69 #else /* !CONFIG_THUMB2_KERNEL */
73 decode_insn = arm_kprobe_decode_insn;
77 p->ainsn.insn = tmp_insn;
79 switch ((*decode_insn)(insn, &p->ainsn)) {
80 case INSN_REJECTED: /* not supported */
83 case INSN_GOOD: /* instruction uses slot */
84 p->ainsn.insn = get_insn_slot();
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);
93 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
101 #ifdef CONFIG_THUMB2_KERNEL
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.
108 static int __kprobes set_t32_breakpoint(void *addr)
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));
116 void __kprobes arch_arm_kprobe(struct kprobe *p)
118 uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
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);
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);
133 flush_insns(addr, sizeof(u32));
137 #else /* !CONFIG_THUMB2_KERNEL */
139 void __kprobes arch_arm_kprobe(struct kprobe *p)
141 kprobe_opcode_t insn = p->opcode;
142 kprobe_opcode_t brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
143 if (insn >= 0xe0000000)
144 brkp |= 0xe0000000; /* Unconditional instruction */
146 brkp |= insn & 0xf0000000; /* Copy condition from insn */
148 flush_insns(p->addr, sizeof(p->addr[0]));
151 #endif /* !CONFIG_THUMB2_KERNEL */
154 * The actual disarming is done here on each CPU and synchronized using
155 * stop_machine. This synchronization is necessary on SMP to avoid removing
156 * a probe between the moment the 'Undefined Instruction' exception is raised
157 * and the moment the exception handler reads the faulting instruction from
158 * memory. It is also needed to atomically set the two half-words of a 32-bit
161 int __kprobes __arch_disarm_kprobe(void *p)
163 struct kprobe *kp = p;
164 #ifdef CONFIG_THUMB2_KERNEL
165 u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
166 kprobe_opcode_t insn = kp->opcode;
169 if (is_wide_instruction(insn)) {
170 ((u16 *)addr)[0] = insn>>16;
171 ((u16 *)addr)[1] = insn;
174 ((u16 *)addr)[0] = insn;
177 flush_insns(addr, len);
179 #else /* !CONFIG_THUMB2_KERNEL */
180 *kp->addr = kp->opcode;
181 flush_insns(kp->addr, sizeof(kp->addr[0]));
186 void __kprobes arch_disarm_kprobe(struct kprobe *p)
188 stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
191 void __kprobes arch_remove_kprobe(struct kprobe *p)
194 free_insn_slot(p->ainsn.insn, 0);
195 p->ainsn.insn = NULL;
199 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
201 kcb->prev_kprobe.kp = kprobe_running();
202 kcb->prev_kprobe.status = kcb->kprobe_status;
205 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
207 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
208 kcb->kprobe_status = kcb->prev_kprobe.status;
211 static void __kprobes set_current_kprobe(struct kprobe *p)
213 __get_cpu_var(current_kprobe) = p;
216 static void __kprobes
217 singlestep_skip(struct kprobe *p, struct pt_regs *regs)
219 #ifdef CONFIG_THUMB2_KERNEL
220 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
221 if (is_wide_instruction(p->opcode))
230 static inline void __kprobes
231 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
233 p->ainsn.insn_singlestep(p, regs);
237 * Called with IRQs disabled. IRQs must remain disabled from that point
238 * all the way until processing this kprobe is complete. The current
239 * kprobes implementation cannot process more than one nested level of
240 * kprobe, and that level is reserved for user kprobe handlers, so we can't
241 * risk encountering a new kprobe in an interrupt handler.
243 void __kprobes kprobe_handler(struct pt_regs *regs)
245 struct kprobe *p, *cur;
246 struct kprobe_ctlblk *kcb;
248 kcb = get_kprobe_ctlblk();
249 cur = kprobe_running();
251 #ifdef CONFIG_THUMB2_KERNEL
253 * First look for a probe which was registered using an address with
254 * bit 0 set, this is the usual situation for pointers to Thumb code.
255 * If not found, fallback to looking for one with bit 0 clear.
257 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
259 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
261 #else /* ! CONFIG_THUMB2_KERNEL */
262 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
267 /* Kprobe is pending, so we're recursing. */
268 switch (kcb->kprobe_status) {
269 case KPROBE_HIT_ACTIVE:
270 case KPROBE_HIT_SSDONE:
271 /* A pre- or post-handler probe got us here. */
272 kprobes_inc_nmissed_count(p);
273 save_previous_kprobe(kcb);
274 set_current_kprobe(p);
275 kcb->kprobe_status = KPROBE_REENTER;
276 singlestep(p, regs, kcb);
277 restore_previous_kprobe(kcb);
280 /* impossible cases */
283 } else if (p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
284 /* Probe hit and conditional execution check ok. */
285 set_current_kprobe(p);
286 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
289 * If we have no pre-handler or it returned 0, we
290 * continue with normal processing. If we have a
291 * pre-handler and it returned non-zero, it prepped
292 * for calling the break_handler below on re-entry,
293 * so get out doing nothing more here.
295 if (!p->pre_handler || !p->pre_handler(p, regs)) {
296 kcb->kprobe_status = KPROBE_HIT_SS;
297 singlestep(p, regs, kcb);
298 if (p->post_handler) {
299 kcb->kprobe_status = KPROBE_HIT_SSDONE;
300 p->post_handler(p, regs, 0);
302 reset_current_kprobe();
306 * Probe hit but conditional execution check failed,
307 * so just skip the instruction and continue as if
308 * nothing had happened.
310 singlestep_skip(p, regs);
313 /* We probably hit a jprobe. Call its break handler. */
314 if (cur->break_handler && cur->break_handler(cur, regs)) {
315 kcb->kprobe_status = KPROBE_HIT_SS;
316 singlestep(cur, regs, kcb);
317 if (cur->post_handler) {
318 kcb->kprobe_status = KPROBE_HIT_SSDONE;
319 cur->post_handler(cur, regs, 0);
322 reset_current_kprobe();
325 * The probe was removed and a race is in progress.
326 * There is nothing we can do about it. Let's restart
327 * the instruction. By the time we can restart, the
328 * real instruction will be there.
333 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
336 local_irq_save(flags);
337 kprobe_handler(regs);
338 local_irq_restore(flags);
342 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
344 struct kprobe *cur = kprobe_running();
345 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
347 switch (kcb->kprobe_status) {
351 * We are here because the instruction being single
352 * stepped caused a page fault. We reset the current
353 * kprobe and the PC to point back to the probe address
354 * and allow the page fault handler to continue as a
357 regs->ARM_pc = (long)cur->addr;
358 if (kcb->kprobe_status == KPROBE_REENTER) {
359 restore_previous_kprobe(kcb);
361 reset_current_kprobe();
365 case KPROBE_HIT_ACTIVE:
366 case KPROBE_HIT_SSDONE:
368 * We increment the nmissed count for accounting,
369 * we can also use npre/npostfault count for accounting
370 * these specific fault cases.
372 kprobes_inc_nmissed_count(cur);
375 * We come here because instructions in the pre/post
376 * handler caused the page_fault, this could happen
377 * if handler tries to access user space by
378 * copy_from_user(), get_user() etc. Let the
379 * user-specified handler try to fix it.
381 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
392 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
393 unsigned long val, void *data)
396 * notify_die() is currently never called on ARM,
397 * so this callback is currently empty.
403 * When a retprobed function returns, trampoline_handler() is called,
404 * calling the kretprobe's handler. We construct a struct pt_regs to
405 * give a view of registers r0-r11 to the user return-handler. This is
406 * not a complete pt_regs structure, but that should be plenty sufficient
407 * for kretprobe handlers which should normally be interested in r0 only
410 void __naked __kprobes kretprobe_trampoline(void)
412 __asm__ __volatile__ (
413 "stmdb sp!, {r0 - r11} \n\t"
415 "bl trampoline_handler \n\t"
417 "ldmia sp!, {r0 - r11} \n\t"
418 #ifdef CONFIG_THUMB2_KERNEL
426 /* Called from kretprobe_trampoline */
427 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
429 struct kretprobe_instance *ri = NULL;
430 struct hlist_head *head, empty_rp;
431 struct hlist_node *node, *tmp;
432 unsigned long flags, orig_ret_address = 0;
433 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
435 INIT_HLIST_HEAD(&empty_rp);
436 kretprobe_hash_lock(current, &head, &flags);
439 * It is possible to have multiple instances associated with a given
440 * task either because multiple functions in the call path have
441 * a return probe installed on them, and/or more than one return
442 * probe was registered for a target function.
444 * We can handle this because:
445 * - instances are always inserted at the head of the list
446 * - when multiple return probes are registered for the same
447 * function, the first instance's ret_addr will point to the
448 * real return address, and all the rest will point to
449 * kretprobe_trampoline
451 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
452 if (ri->task != current)
453 /* another task is sharing our hash bucket */
456 if (ri->rp && ri->rp->handler) {
457 __get_cpu_var(current_kprobe) = &ri->rp->kp;
458 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
459 ri->rp->handler(ri, regs);
460 __get_cpu_var(current_kprobe) = NULL;
463 orig_ret_address = (unsigned long)ri->ret_addr;
464 recycle_rp_inst(ri, &empty_rp);
466 if (orig_ret_address != trampoline_address)
468 * This is the real return address. Any other
469 * instances associated with this task are for
470 * other calls deeper on the call stack
475 kretprobe_assert(ri, orig_ret_address, trampoline_address);
476 kretprobe_hash_unlock(current, &flags);
478 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
479 hlist_del(&ri->hlist);
483 return (void *)orig_ret_address;
486 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
487 struct pt_regs *regs)
489 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
491 /* Replace the return addr with trampoline addr. */
492 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
495 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
497 struct jprobe *jp = container_of(p, struct jprobe, kp);
498 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
499 long sp_addr = regs->ARM_sp;
502 kcb->jprobe_saved_regs = *regs;
503 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
504 regs->ARM_pc = (long)jp->entry;
506 cpsr = regs->ARM_cpsr | PSR_I_BIT;
507 #ifdef CONFIG_THUMB2_KERNEL
508 /* Set correct Thumb state in cpsr */
509 if (regs->ARM_pc & 1)
514 regs->ARM_cpsr = cpsr;
520 void __kprobes jprobe_return(void)
522 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
524 __asm__ __volatile__ (
526 * Setup an empty pt_regs. Fill SP and PC fields as
527 * they're needed by longjmp_break_handler.
529 * We allocate some slack between the original SP and start of
530 * our fabricated regs. To be precise we want to have worst case
531 * covered which is STMFD with all 16 regs so we allocate 2 *
532 * sizeof(struct_pt_regs)).
534 * This is to prevent any simulated instruction from writing
535 * over the regs when they are accessing the stack.
537 #ifdef CONFIG_THUMB2_KERNEL
538 "sub r0, %0, %1 \n\t"
541 "sub sp, %0, %1 \n\t"
543 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
544 "str %0, [sp, %2] \n\t"
545 "str r0, [sp, %3] \n\t"
547 "bl kprobe_handler \n\t"
550 * Return to the context saved by setjmp_pre_handler
551 * and restored by longjmp_break_handler.
553 #ifdef CONFIG_THUMB2_KERNEL
554 "ldr lr, [sp, %2] \n\t" /* lr = saved sp */
555 "ldrd r0, r1, [sp, %5] \n\t" /* r0,r1 = saved lr,pc */
556 "ldr r2, [sp, %4] \n\t" /* r2 = saved psr */
557 "stmdb lr!, {r0, r1, r2} \n\t" /* push saved lr and */
559 "ldmia sp, {r0 - r12} \n\t"
561 "ldr lr, [sp], #4 \n\t"
564 "ldr r0, [sp, %4] \n\t"
565 "msr cpsr_cxsf, r0 \n\t"
566 "ldmia sp, {r0 - pc} \n\t"
569 : "r" (kcb->jprobe_saved_regs.ARM_sp),
570 "I" (sizeof(struct pt_regs) * 2),
571 "J" (offsetof(struct pt_regs, ARM_sp)),
572 "J" (offsetof(struct pt_regs, ARM_pc)),
573 "J" (offsetof(struct pt_regs, ARM_cpsr)),
574 "J" (offsetof(struct pt_regs, ARM_lr))
578 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
580 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
581 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
582 long orig_sp = regs->ARM_sp;
583 struct jprobe *jp = container_of(p, struct jprobe, kp);
585 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
586 if (orig_sp != stack_addr) {
587 struct pt_regs *saved_regs =
588 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
589 printk("current sp %lx does not match saved sp %lx\n",
590 orig_sp, stack_addr);
591 printk("Saved registers for jprobe %p\n", jp);
592 show_regs(saved_regs);
593 printk("Current registers\n");
597 *regs = kcb->jprobe_saved_regs;
598 memcpy((void *)stack_addr, kcb->jprobes_stack,
599 MIN_STACK_SIZE(stack_addr));
600 preempt_enable_no_resched();
606 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
611 #ifdef CONFIG_THUMB2_KERNEL
613 static struct undef_hook kprobes_thumb16_break_hook = {
614 .instr_mask = 0xffff,
615 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
616 .cpsr_mask = MODE_MASK,
617 .cpsr_val = SVC_MODE,
618 .fn = kprobe_trap_handler,
621 static struct undef_hook kprobes_thumb32_break_hook = {
622 .instr_mask = 0xffffffff,
623 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
624 .cpsr_mask = MODE_MASK,
625 .cpsr_val = SVC_MODE,
626 .fn = kprobe_trap_handler,
629 #else /* !CONFIG_THUMB2_KERNEL */
631 static struct undef_hook kprobes_arm_break_hook = {
632 .instr_mask = 0x0fffffff,
633 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
634 .cpsr_mask = MODE_MASK,
635 .cpsr_val = SVC_MODE,
636 .fn = kprobe_trap_handler,
639 #endif /* !CONFIG_THUMB2_KERNEL */
641 int __init arch_init_kprobes()
643 arm_kprobe_decode_init();
644 #ifdef CONFIG_THUMB2_KERNEL
645 register_undef_hook(&kprobes_thumb16_break_hook);
646 register_undef_hook(&kprobes_thumb32_break_hook);
648 register_undef_hook(&kprobes_arm_break_hook);