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, cnt) \
38 flush_icache_range((unsigned long)(addr), \
39 (unsigned long)(addr) + \
40 sizeof(kprobe_opcode_t) * (cnt))
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;
56 if (addr & 0x3 || in_exception_text(addr))
61 p->ainsn.insn = tmp_insn;
63 switch (arm_kprobe_decode_insn(insn, &p->ainsn)) {
64 case INSN_REJECTED: /* not supported */
67 case INSN_GOOD: /* instruction uses slot */
68 p->ainsn.insn = get_insn_slot();
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);
76 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
84 void __kprobes arch_arm_kprobe(struct kprobe *p)
86 *p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
87 flush_insns(p->addr, 1);
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
97 int __kprobes __arch_disarm_kprobe(void *p)
99 struct kprobe *kp = p;
100 *kp->addr = kp->opcode;
101 flush_insns(kp->addr, 1);
105 void __kprobes arch_disarm_kprobe(struct kprobe *p)
107 stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
110 void __kprobes arch_remove_kprobe(struct kprobe *p)
113 free_insn_slot(p->ainsn.insn, 0);
114 p->ainsn.insn = NULL;
118 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
120 kcb->prev_kprobe.kp = kprobe_running();
121 kcb->prev_kprobe.status = kcb->kprobe_status;
124 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
126 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
127 kcb->kprobe_status = kcb->prev_kprobe.status;
130 static void __kprobes set_current_kprobe(struct kprobe *p)
132 __get_cpu_var(current_kprobe) = p;
135 static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
136 struct kprobe_ctlblk *kcb)
139 if (p->ainsn.insn_check_cc(regs->ARM_cpsr))
140 p->ainsn.insn_handler(p, regs);
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.
150 void __kprobes kprobe_handler(struct pt_regs *regs)
152 struct kprobe *p, *cur;
153 struct kprobe_ctlblk *kcb;
154 kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->ARM_pc;
156 kcb = get_kprobe_ctlblk();
157 cur = kprobe_running();
158 p = get_kprobe(addr);
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);
175 /* impossible cases */
179 set_current_kprobe(p);
180 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
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.
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);
196 reset_current_kprobe();
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);
209 reset_current_kprobe();
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.
220 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
223 local_irq_save(flags);
224 kprobe_handler(regs);
225 local_irq_restore(flags);
229 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
231 struct kprobe *cur = kprobe_running();
232 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
234 switch (kcb->kprobe_status) {
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
244 regs->ARM_pc = (long)cur->addr;
245 if (kcb->kprobe_status == KPROBE_REENTER) {
246 restore_previous_kprobe(kcb);
248 reset_current_kprobe();
252 case KPROBE_HIT_ACTIVE:
253 case KPROBE_HIT_SSDONE:
255 * We increment the nmissed count for accounting,
256 * we can also use npre/npostfault count for accounting
257 * these specific fault cases.
259 kprobes_inc_nmissed_count(cur);
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.
268 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
279 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
280 unsigned long val, void *data)
283 * notify_die() is currently never called on ARM,
284 * so this callback is currently empty.
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
297 void __naked __kprobes kretprobe_trampoline(void)
299 __asm__ __volatile__ (
300 "stmdb sp!, {r0 - r11} \n\t"
302 "bl trampoline_handler \n\t"
304 "ldmia sp!, {r0 - r11} \n\t"
309 /* Called from kretprobe_trampoline */
310 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
312 struct kretprobe_instance *ri = NULL;
313 struct hlist_head *head, empty_rp;
314 struct hlist_node *node, *tmp;
315 unsigned long flags, orig_ret_address = 0;
316 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
318 INIT_HLIST_HEAD(&empty_rp);
319 kretprobe_hash_lock(current, &head, &flags);
322 * It is possible to have multiple instances associated with a given
323 * task either because multiple functions in the call path have
324 * a return probe installed on them, and/or more than one return
325 * probe was registered for a target function.
327 * We can handle this because:
328 * - instances are always inserted at the head of the list
329 * - when multiple return probes are registered for the same
330 * function, the first instance's ret_addr will point to the
331 * real return address, and all the rest will point to
332 * kretprobe_trampoline
334 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
335 if (ri->task != current)
336 /* another task is sharing our hash bucket */
339 if (ri->rp && ri->rp->handler) {
340 __get_cpu_var(current_kprobe) = &ri->rp->kp;
341 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
342 ri->rp->handler(ri, regs);
343 __get_cpu_var(current_kprobe) = NULL;
346 orig_ret_address = (unsigned long)ri->ret_addr;
347 recycle_rp_inst(ri, &empty_rp);
349 if (orig_ret_address != trampoline_address)
351 * This is the real return address. Any other
352 * instances associated with this task are for
353 * other calls deeper on the call stack
358 kretprobe_assert(ri, orig_ret_address, trampoline_address);
359 kretprobe_hash_unlock(current, &flags);
361 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
362 hlist_del(&ri->hlist);
366 return (void *)orig_ret_address;
369 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
370 struct pt_regs *regs)
372 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
374 /* Replace the return addr with trampoline addr. */
375 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
378 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
380 struct jprobe *jp = container_of(p, struct jprobe, kp);
381 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
382 long sp_addr = regs->ARM_sp;
384 kcb->jprobe_saved_regs = *regs;
385 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
386 regs->ARM_pc = (long)jp->entry;
387 regs->ARM_cpsr |= PSR_I_BIT;
392 void __kprobes jprobe_return(void)
394 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
396 __asm__ __volatile__ (
398 * Setup an empty pt_regs. Fill SP and PC fields as
399 * they're needed by longjmp_break_handler.
401 * We allocate some slack between the original SP and start of
402 * our fabricated regs. To be precise we want to have worst case
403 * covered which is STMFD with all 16 regs so we allocate 2 *
404 * sizeof(struct_pt_regs)).
406 * This is to prevent any simulated instruction from writing
407 * over the regs when they are accessing the stack.
409 "sub sp, %0, %1 \n\t"
410 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
411 "str %0, [sp, %2] \n\t"
412 "str r0, [sp, %3] \n\t"
414 "bl kprobe_handler \n\t"
417 * Return to the context saved by setjmp_pre_handler
418 * and restored by longjmp_break_handler.
420 "ldr r0, [sp, %4] \n\t"
421 "msr cpsr_cxsf, r0 \n\t"
422 "ldmia sp, {r0 - pc} \n\t"
424 : "r" (kcb->jprobe_saved_regs.ARM_sp),
425 "I" (sizeof(struct pt_regs) * 2),
426 "J" (offsetof(struct pt_regs, ARM_sp)),
427 "J" (offsetof(struct pt_regs, ARM_pc)),
428 "J" (offsetof(struct pt_regs, ARM_cpsr))
432 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
434 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
435 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
436 long orig_sp = regs->ARM_sp;
437 struct jprobe *jp = container_of(p, struct jprobe, kp);
439 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
440 if (orig_sp != stack_addr) {
441 struct pt_regs *saved_regs =
442 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
443 printk("current sp %lx does not match saved sp %lx\n",
444 orig_sp, stack_addr);
445 printk("Saved registers for jprobe %p\n", jp);
446 show_regs(saved_regs);
447 printk("Current registers\n");
451 *regs = kcb->jprobe_saved_regs;
452 memcpy((void *)stack_addr, kcb->jprobes_stack,
453 MIN_STACK_SIZE(stack_addr));
454 preempt_enable_no_resched();
460 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
465 static struct undef_hook kprobes_break_hook = {
466 .instr_mask = 0xffffffff,
467 .instr_val = KPROBE_BREAKPOINT_INSTRUCTION,
468 .cpsr_mask = MODE_MASK,
469 .cpsr_val = SVC_MODE,
470 .fn = kprobe_trap_handler,
473 int __init arch_init_kprobes()
475 arm_kprobe_decode_init();
476 register_undef_hook(&kprobes_break_hook);