2 * Dynamic Binary Instrumentation Module based on KProbes
3 * modules/kprobe/arch/asm-arm/dbi_kprobes.c
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) Samsung Electronics, 2006-2010
21 * 2006-2007 Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM/MIPS
22 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
23 * Probes initial implementation; Support x86.
24 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
25 * 2010-2011 Alexander Shirshikov <a.shirshikov@samsung.com>: initial implementation for Thumb
26 * 2012 Stanislav Andreev <s.andreev@samsung.com>: added time debug profiling support; BUG() message fix
27 * 2012 Stanislav Andreev <s.andreev@samsung.com>: redesign of kprobe functionality -
28 * kprobe_handler() now called via undefined instruction hooks
29 * 2012 Stanislav Andreev <s.andreev@samsung.com>: hash tables search implemented for uprobes
32 #include <linux/module.h>
35 #include "dbi_kprobes.h"
36 #include "trampoline_arm.h"
37 #include "../dbi_kprobes.h"
38 #include "../../dbi_kprobes.h"
40 #include "../../dbi_kdebug.h"
41 #include "../../dbi_insn_slots.h"
42 #include "../../dbi_kprobes_deps.h"
45 #include <asm/cacheflush.h>
47 #ifdef TRAP_OVERHEAD_DEBUG
48 #include <linux/pid.h>
49 #include <linux/signal.h>
53 #include <linux/time.h>
56 #include <asm/traps.h>
57 #include <asm/ptrace.h>
58 #include <linux/list.h>
59 #include <linux/hash.h>
61 #define SUPRESS_BUG_MESSAGES
63 extern struct kprobe * per_cpu__current_kprobe;
64 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
66 static void (*__swap_register_undef_hook)(struct undef_hook *hook);
67 static void (*__swap_unregister_undef_hook)(struct undef_hook *hook);
70 unsigned long swap_sum_time = 0;
71 unsigned long swap_sum_hit = 0;
72 EXPORT_SYMBOL_GPL (swap_sum_time);
73 EXPORT_SYMBOL_GPL (swap_sum_hit);
76 static struct kprobe trampoline_p =
78 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
79 .pre_handler = trampoline_probe_handler
82 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs)
90 for (i = 0; i < 13; i++, reg_mask <<= 1)
92 if (!(insn & reg_mask))
98 for (i = 0; i < 13; i++)
100 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == i))
102 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == i))
104 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == i))
106 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == i))
113 DBPRINTF ("there are no free register %x in insn %lx!", uregs, insn);
116 DBPRINTF ("prep_pc_dep_insn_execbuf: using R%d, changing regs %x", i, uregs);
118 // set register to save
119 ARM_INSN_REG_SET_RD (insns[0], i);
120 // set register to load address to
121 ARM_INSN_REG_SET_RD (insns[1], i);
122 // set instruction to execute and patch it
125 ARM_INSN_REG_CLEAR_MR (insn, 15);
126 ARM_INSN_REG_SET_MR (insn, i);
130 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == 15))
131 ARM_INSN_REG_SET_RN (insn, i);
132 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == 15))
133 ARM_INSN_REG_SET_RD (insn, i);
134 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == 15))
135 ARM_INSN_REG_SET_RS (insn, i);
136 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == 15))
137 ARM_INSN_REG_SET_RM (insn, i);
139 insns[UPROBES_TRAMP_INSN_IDX] = insn;
140 // set register to restore
141 ARM_INSN_REG_SET_RD (insns[3], i);
144 EXPORT_SYMBOL_GPL(prep_pc_dep_insn_execbuf);
146 int arch_check_insn_arm(struct arch_specific_insn *ainsn)
150 // check instructions that can change PC by nature
152 // ARM_INSN_MATCH (UNDEF, ainsn->insn_arm[0]) ||
153 ARM_INSN_MATCH (AUNDEF, ainsn->insn_arm[0]) ||
154 ARM_INSN_MATCH (SWI, ainsn->insn_arm[0]) ||
155 ARM_INSN_MATCH (BREAK, ainsn->insn_arm[0]) ||
156 ARM_INSN_MATCH (BL, ainsn->insn_arm[0]) ||
157 ARM_INSN_MATCH (BLX1, ainsn->insn_arm[0]) ||
158 ARM_INSN_MATCH (BLX2, ainsn->insn_arm[0]) ||
159 ARM_INSN_MATCH (BX, ainsn->insn_arm[0]) ||
160 ARM_INSN_MATCH (BXJ, ainsn->insn_arm[0]))
162 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
165 #ifndef CONFIG_CPU_V7
166 // check instructions that can write result to PC
167 else if ((ARM_INSN_MATCH (DPIS, ainsn->insn_arm[0]) ||
168 ARM_INSN_MATCH (DPRS, ainsn->insn_arm[0]) ||
169 ARM_INSN_MATCH (DPI, ainsn->insn_arm[0]) ||
170 ARM_INSN_MATCH (LIO, ainsn->insn_arm[0]) ||
171 ARM_INSN_MATCH (LRO, ainsn->insn_arm[0])) &&
172 (ARM_INSN_REG_RD (ainsn->insn_arm[0]) == 15))
174 DBPRINTF ("Bad arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
177 #endif // CONFIG_CPU_V7
178 // check special instruction loads store multiple registers
179 else if ((ARM_INSN_MATCH (LM, ainsn->insn_arm[0]) || ARM_INSN_MATCH (SM, ainsn->insn_arm[0])) &&
180 // store pc or load to pc
181 (ARM_INSN_REG_MR (ainsn->insn_arm[0], 15) ||
182 // store/load with pc update
183 ((ARM_INSN_REG_RN (ainsn->insn_arm[0]) == 15) && (ainsn->insn_arm[0] & 0x200000))))
185 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
190 EXPORT_SYMBOL_GPL(arch_check_insn_arm);
192 int arch_prepare_kprobe (struct kprobe *p)
194 kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
195 int uregs, pc_dep, ret = 0;
196 kprobe_opcode_t insn[MAX_INSN_SIZE];
197 struct arch_specific_insn ainsn;
199 /* insn: must be on special executable page on i386. */
200 p->ainsn.insn = get_insn_slot(NULL, &kprobe_insn_pages, 0);
204 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
205 ainsn.insn_arm = ainsn.insn = insn;
206 ret = arch_check_insn_arm (&ainsn);
209 p->opcode = *p->addr;
213 if(ARM_INSN_MATCH (DPIS, insn[0]) || ARM_INSN_MATCH (LRO, insn[0]) ||
214 ARM_INSN_MATCH (SRO, insn[0]))
217 if( (ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
218 (ARM_INSN_MATCH (SRO, insn[0]) && (ARM_INSN_REG_RD (insn[0]) == 15)) )
220 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
225 else if(ARM_INSN_MATCH (DPI, insn[0]) || ARM_INSN_MATCH (LIO, insn[0]) ||
226 ARM_INSN_MATCH (SIO, insn[0]))
229 if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_MATCH (SIO, insn[0]) &&
230 (ARM_INSN_REG_RD (insn[0]) == 15)))
233 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
237 else if(ARM_INSN_MATCH (DPRS, insn[0]))
240 if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
241 (ARM_INSN_REG_RS (insn[0]) == 15))
244 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
248 else if(ARM_INSN_MATCH (SM, insn[0]))
251 if (ARM_INSN_REG_MR (insn[0], 15))
253 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
257 // check instructions that can write result to SP andu uses PC
258 if (pc_dep && (ARM_INSN_REG_RD (ainsn.insn[0]) == 13))
260 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
267 memcpy (insns, pc_dep_insn_execbuf, sizeof (insns));
268 if (prep_pc_dep_insn_execbuf (insns, insn[0], uregs) != 0)
270 DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
271 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
274 insns[6] = (kprobe_opcode_t) (p->addr + 2);
278 memcpy (insns, gen_insn_execbuf, sizeof (insns));
279 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
281 insns[7] = (kprobe_opcode_t) (p->addr + 1);
282 DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
283 DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
284 p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
285 insns[5], insns[6], insns[7], insns[8]);
286 memcpy (p->ainsn.insn, insns, sizeof(insns));
287 flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
295 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
296 printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
302 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
305 regs->ARM_pc = (unsigned long)p->ss_addr;
308 regs->ARM_pc = (unsigned long)p->ainsn.insn;
311 EXPORT_SYMBOL_GPL(prepare_singlestep);
313 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
315 kcb->prev_kprobe.kp = kprobe_running();
316 kcb->prev_kprobe.status = kcb->kprobe_status;
319 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
321 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
322 kcb->kprobe_status = kcb->prev_kprobe.status;
325 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
327 __get_cpu_var(current_kprobe) = p;
328 DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
331 #ifdef TRAP_OVERHEAD_DEBUG
332 static unsigned long trap_handler_counter_debug = 0;
333 #define SAMPLING_COUNTER 100000
336 static int kprobe_handler(struct pt_regs *regs)
338 struct kprobe *p, *cur;
339 struct kprobe_ctlblk *kcb;
341 kcb = get_kprobe_ctlblk();
342 cur = kprobe_running();
343 p = get_kprobe((void *)regs->ARM_pc);
347 /* Kprobe is pending, so we're recursing. */
348 switch (kcb->kprobe_status) {
349 case KPROBE_HIT_ACTIVE:
350 case KPROBE_HIT_SSDONE:
351 /* A pre- or post-handler probe got us here. */
352 kprobes_inc_nmissed_count(p);
353 save_previous_kprobe(kcb, NULL);
354 set_current_kprobe(p, 0, 0);
355 kcb->kprobe_status = KPROBE_REENTER;
356 prepare_singlestep(p, regs);
357 restore_previous_kprobe(kcb);
360 /* impossible cases */
364 set_current_kprobe(p, 0, 0);
365 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
367 if (!p->pre_handler || !p->pre_handler(p, regs)) {
368 kcb->kprobe_status = KPROBE_HIT_SS;
369 prepare_singlestep(p, regs);
370 reset_current_kprobe();
380 printk("no_kprobe\n");
384 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
389 #ifdef SUPRESS_BUG_MESSAGES
390 int swap_oops_in_progress;
391 /* oops_in_progress used to avoid BUG() messages
392 * that slow down kprobe_handler() execution */
393 swap_oops_in_progress = oops_in_progress;
394 oops_in_progress = 1;
397 local_irq_save(flags);
399 ret = kprobe_handler(regs);
400 preempt_enable_no_resched();
401 local_irq_restore(flags);
403 #ifdef SUPRESS_BUG_MESSAGES
404 oops_in_progress = swap_oops_in_progress;
410 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
412 struct jprobe *jp = container_of(p, struct jprobe, kp);
413 kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
414 entry_point_t entry = (entry_point_t)jp->entry;
415 pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
417 if (((unsigned long)p->addr == sched_addr) && sched_rp) {
418 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
419 patch_suspended_task(sched_rp, tinfo->task);
423 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
427 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
428 regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
436 void dbi_jprobe_return (void)
440 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
444 EXPORT_SYMBOL_GPL(longjmp_break_handler);
446 void arch_arm_kprobe (struct kprobe *p)
448 *p->addr = BREAKPOINT_INSTRUCTION;
449 flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
452 void arch_disarm_kprobe (struct kprobe *p)
454 *p->addr = p->opcode;
455 flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
459 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
461 struct kretprobe_instance *ri = NULL;
462 struct hlist_head *head;
463 struct hlist_node *node, *tmp;
464 unsigned long flags, orig_ret_address = 0;
465 unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
467 struct kretprobe *crp = NULL;
468 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
472 spin_lock_irqsave (&kretprobe_lock, flags);
475 * We are using different hash keys (current and mm) for finding kernel
476 * space and user space probes. Kernel space probes can change mm field in
477 * task_struct. User space probes can be shared between threads of one
478 * process so they have different current but same mm.
480 head = kretprobe_inst_table_head(current);
483 * It is possible to have multiple instances associated with a given
484 * task either because an multiple functions in the call path
485 * have a return probe installed on them, and/or more then one
486 * return probe was registered for a target function.
488 * We can handle this because:
489 * - instances are always inserted at the head of the list
490 * - when multiple return probes are registered for the same
491 * function, the first instance's ret_addr will point to the
492 * real return address, and all the rest will point to
493 * kretprobe_trampoline
495 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
497 if (ri->task != current)
498 /* another task is sharing our hash bucket */
500 if (ri->rp && ri->rp->handler){
501 ri->rp->handler (ri, regs, ri->rp->priv_arg);
504 orig_ret_address = (unsigned long) ri->ret_addr;
505 recycle_rp_inst (ri);
506 if (orig_ret_address != trampoline_address)
508 * This is the real return address. Any other
509 * instances associated with this task are for
510 * other calls deeper on the call stack
514 kretprobe_assert (ri, orig_ret_address, trampoline_address);
516 regs->uregs[14] = orig_ret_address;
517 DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
518 DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
520 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
522 regs->uregs[15] = orig_ret_address;
524 if (!thumb_mode( regs )) regs->uregs[15] += 4;
525 else regs->uregs[15] += 2;
528 DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
530 if(p){ // ARM, MIPS, X86 user space
531 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
533 regs->ARM_cpsr &= 0xFFFFFFDF;
535 if (user_mode( regs ) && (regs->uregs[14] & 0x01))
537 regs->ARM_cpsr |= 0x20;
541 if (kcb->kprobe_status == KPROBE_REENTER) {
542 restore_previous_kprobe(kcb);
544 reset_current_kprobe();
548 spin_unlock_irqrestore (&kretprobe_lock, flags);
551 * By returning a non-zero value, we are telling
552 * kprobe_handler() that we don't want the post_handler
553 * to run (and have re-enabled preemption)
558 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
560 void __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
562 struct kretprobe_instance *ri;
564 DBPRINTF ("start\n");
565 //TODO: test - remove retprobe after func entry but before its exit
566 if ((ri = get_free_rp_inst (rp)) != NULL)
570 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
571 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
573 /* Set flag of current mode */
574 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
576 /* Replace the return addr with trampoline addr */
577 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
579 // DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
583 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
588 void swap_register_undef_hook(struct undef_hook *hook)
590 __swap_register_undef_hook(hook);
592 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
594 void swap_unregister_undef_hook(struct undef_hook *hook)
596 __swap_unregister_undef_hook(hook);
598 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
600 // kernel probes hook
601 static struct undef_hook undef_ho_k = {
602 .instr_mask = 0xffffffff,
603 .instr_val = BREAKPOINT_INSTRUCTION,
604 .cpsr_mask = MODE_MASK,
605 .cpsr_val = SVC_MODE,
606 .fn = kprobe_trap_handler
609 int arch_init_kprobes(void)
613 // Register hooks (kprobe_handler)
614 __swap_register_undef_hook = swap_ksyms("register_undef_hook");
615 if (__swap_register_undef_hook == NULL) {
616 printk("no register_undef_hook symbol found!\n");
620 // Unregister hooks (kprobe_handler)
621 __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
622 if (__swap_unregister_undef_hook == NULL) {
623 printk("no unregister_undef_hook symbol found!\n");
627 swap_register_undef_hook(&undef_ho_k);
628 if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
629 //dbi_unregister_jprobe(&do_exit_p, 0);
635 void arch_exit_kprobes(void)
637 swap_unregister_undef_hook(&undef_ho_k);
640 /* export symbol for trampoline_arm.h */
641 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
642 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);