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>
46 #include <asm/traps.h>
47 #include <asm/ptrace.h>
48 #include <linux/list.h>
49 #include <linux/hash.h>
51 #define SUPRESS_BUG_MESSAGES
53 extern struct kprobe * per_cpu__current_kprobe;
54 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
56 static void (*__swap_register_undef_hook)(struct undef_hook *hook);
57 static void (*__swap_unregister_undef_hook)(struct undef_hook *hook);
59 static struct kprobe trampoline_p =
61 .addr = (kprobe_opcode_t *)&kretprobe_trampoline,
62 .pre_handler = trampoline_probe_handler
65 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs)
72 for (i = 0; i < 13; i++, reg_mask <<= 1) {
73 if (!(insn & reg_mask))
77 for (i = 0; i < 13; i++) {
78 if ((uregs & 0x1) && (ARM_INSN_REG_RN(insn) == i))
80 if ((uregs & 0x2) && (ARM_INSN_REG_RD(insn) == i))
82 if ((uregs & 0x4) && (ARM_INSN_REG_RS(insn) == i))
84 if ((uregs & 0x8) && (ARM_INSN_REG_RM(insn) == i))
91 DBPRINTF ("there are no free register %x in insn %lx!", uregs, insn);
94 DBPRINTF ("prep_pc_dep_insn_execbuf: using R%d, changing regs %x", i, uregs);
96 // set register to save
97 ARM_INSN_REG_SET_RD(insns[0], i);
98 // set register to load address to
99 ARM_INSN_REG_SET_RD(insns[1], i);
100 // set instruction to execute and patch it
102 ARM_INSN_REG_CLEAR_MR(insn, 15);
103 ARM_INSN_REG_SET_MR(insn, i);
105 if ((uregs & 0x1) && (ARM_INSN_REG_RN(insn) == 15))
106 ARM_INSN_REG_SET_RN(insn, i);
107 if ((uregs & 0x2) && (ARM_INSN_REG_RD(insn) == 15))
108 ARM_INSN_REG_SET_RD(insn, i);
109 if ((uregs & 0x4) && (ARM_INSN_REG_RS(insn) == 15))
110 ARM_INSN_REG_SET_RS(insn, i);
111 if ((uregs & 0x8) && (ARM_INSN_REG_RM(insn) == 15))
112 ARM_INSN_REG_SET_RM(insn, i);
115 insns[UPROBES_TRAMP_INSN_IDX] = insn;
116 // set register to restore
117 ARM_INSN_REG_SET_RD(insns[3], i);
121 EXPORT_SYMBOL_GPL(prep_pc_dep_insn_execbuf);
123 int arch_check_insn_arm(struct arch_specific_insn *ainsn)
127 // check instructions that can change PC by nature
129 // ARM_INSN_MATCH(UNDEF, ainsn->insn_arm[0]) ||
130 ARM_INSN_MATCH(AUNDEF, ainsn->insn_arm[0]) ||
131 ARM_INSN_MATCH(SWI, ainsn->insn_arm[0]) ||
132 ARM_INSN_MATCH(BREAK, ainsn->insn_arm[0]) ||
133 ARM_INSN_MATCH(BL, ainsn->insn_arm[0]) ||
134 ARM_INSN_MATCH(BLX1, ainsn->insn_arm[0]) ||
135 ARM_INSN_MATCH(BLX2, ainsn->insn_arm[0]) ||
136 ARM_INSN_MATCH(BX, ainsn->insn_arm[0]) ||
137 ARM_INSN_MATCH(BXJ, ainsn->insn_arm[0])) {
138 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
140 #ifndef CONFIG_CPU_V7
141 // check instructions that can write result to PC
142 } else if ((ARM_INSN_MATCH(DPIS, ainsn->insn_arm[0]) ||
143 ARM_INSN_MATCH(DPRS, ainsn->insn_arm[0]) ||
144 ARM_INSN_MATCH(DPI, ainsn->insn_arm[0]) ||
145 ARM_INSN_MATCH(LIO, ainsn->insn_arm[0]) ||
146 ARM_INSN_MATCH(LRO, ainsn->insn_arm[0])) &&
147 (ARM_INSN_REG_RD(ainsn->insn_arm[0]) == 15)) {
148 DBPRINTF ("Bad arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
150 #endif // CONFIG_CPU_V7
151 // check special instruction loads store multiple registers
152 } else if ((ARM_INSN_MATCH(LM, ainsn->insn_arm[0]) || ARM_INSN_MATCH(SM, ainsn->insn_arm[0])) &&
153 // store pc or load to pc
154 (ARM_INSN_REG_MR(ainsn->insn_arm[0], 15) ||
155 // store/load with pc update
156 ((ARM_INSN_REG_RN(ainsn->insn_arm[0]) == 15) && (ainsn->insn_arm[0] & 0x200000)))) {
157 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
163 EXPORT_SYMBOL_GPL(arch_check_insn_arm);
165 int arch_prepare_kprobe(struct kprobe *p, struct hlist_head *page_list)
167 kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
168 int uregs, pc_dep, ret = 0;
169 kprobe_opcode_t insn[MAX_INSN_SIZE];
170 struct arch_specific_insn ainsn;
172 /* insn: must be on special executable page on i386. */
173 p->ainsn.insn = get_insn_slot(NULL, page_list, 0);
177 memcpy(insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
178 ainsn.insn_arm = ainsn.insn = insn;
179 ret = arch_check_insn_arm(&ainsn);
181 p->opcode = *p->addr;
185 if (ARM_INSN_MATCH(DPIS, insn[0]) || ARM_INSN_MATCH(LRO, insn[0]) ||
186 ARM_INSN_MATCH(SRO, insn[0])) {
188 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_REG_RM(insn[0]) == 15) ||
189 (ARM_INSN_MATCH(SRO, insn[0]) && (ARM_INSN_REG_RD(insn[0]) == 15))) {
190 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
195 } else if( ARM_INSN_MATCH(DPI, insn[0]) || ARM_INSN_MATCH(LIO, insn[0]) ||
196 ARM_INSN_MATCH(SIO, insn[0])) {
198 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_MATCH(SIO, insn[0]) &&
199 (ARM_INSN_REG_RD (insn[0]) == 15))) {
201 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
204 } else if (ARM_INSN_MATCH(DPRS, insn[0])) {
206 if ((ARM_INSN_REG_RN(insn[0]) == 15) || (ARM_INSN_REG_RM(insn[0]) == 15) ||
207 (ARM_INSN_REG_RS (insn[0]) == 15)) {
209 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
212 } else if(ARM_INSN_MATCH(SM, insn[0])) {
214 if (ARM_INSN_REG_MR(insn[0], 15)) {
215 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
220 // check instructions that can write result to SP andu uses PC
221 if (pc_dep && (ARM_INSN_REG_RD(ainsn.insn[0]) == 13)) {
222 free_insn_slot(page_list, NULL, p->ainsn.insn);
225 if (uregs && pc_dep) {
226 memcpy(insns, pc_dep_insn_execbuf, sizeof(insns));
227 if (prep_pc_dep_insn_execbuf(insns, insn[0], uregs) != 0) {
228 DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
229 free_insn_slot(page_list, NULL, p->ainsn.insn);
232 insns[6] = (kprobe_opcode_t)(p->addr + 2);
234 memcpy(insns, gen_insn_execbuf, sizeof(insns));
235 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
237 insns[7] = (kprobe_opcode_t)(p->addr + 1);
238 DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
239 DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
240 p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
241 insns[5], insns[6], insns[7], insns[8]);
242 memcpy(p->ainsn.insn, insns, sizeof(insns));
243 flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
249 free_insn_slot(page_list, NULL, p->ainsn.insn);
250 printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
256 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
259 regs->ARM_pc = (unsigned long)p->ss_addr;
262 regs->ARM_pc = (unsigned long)p->ainsn.insn;
265 EXPORT_SYMBOL_GPL(prepare_singlestep);
267 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
269 kcb->prev_kprobe.kp = kprobe_running();
270 kcb->prev_kprobe.status = kcb->kprobe_status;
273 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
275 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
276 kcb->kprobe_status = kcb->prev_kprobe.status;
279 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
281 __get_cpu_var(current_kprobe) = p;
282 DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
285 static int kprobe_handler(struct pt_regs *regs)
287 struct kprobe *p, *cur;
288 struct kprobe_ctlblk *kcb;
290 kcb = get_kprobe_ctlblk();
291 cur = kprobe_running();
292 p = get_kprobe((void *)regs->ARM_pc);
296 /* Kprobe is pending, so we're recursing. */
297 switch (kcb->kprobe_status) {
298 case KPROBE_HIT_ACTIVE:
299 case KPROBE_HIT_SSDONE:
300 /* A pre- or post-handler probe got us here. */
301 kprobes_inc_nmissed_count(p);
302 save_previous_kprobe(kcb, NULL);
303 set_current_kprobe(p, 0, 0);
304 kcb->kprobe_status = KPROBE_REENTER;
305 prepare_singlestep(p, regs);
306 restore_previous_kprobe(kcb);
309 /* impossible cases */
313 set_current_kprobe(p, 0, 0);
314 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
316 if (!p->pre_handler || !p->pre_handler(p, regs)) {
317 kcb->kprobe_status = KPROBE_HIT_SS;
318 prepare_singlestep(p, regs);
319 reset_current_kprobe();
329 printk("no_kprobe\n");
333 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
338 #ifdef SUPRESS_BUG_MESSAGES
339 int swap_oops_in_progress;
340 /* oops_in_progress used to avoid BUG() messages
341 * that slow down kprobe_handler() execution */
342 swap_oops_in_progress = oops_in_progress;
343 oops_in_progress = 1;
346 local_irq_save(flags);
348 ret = kprobe_handler(regs);
349 preempt_enable_no_resched();
350 local_irq_restore(flags);
352 #ifdef SUPRESS_BUG_MESSAGES
353 oops_in_progress = swap_oops_in_progress;
359 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
361 struct jprobe *jp = container_of(p, struct jprobe, kp);
362 kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
363 entry_point_t entry = (entry_point_t)jp->entry;
364 pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
366 if (((unsigned long)p->addr == sched_addr) && sched_rp) {
367 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
368 patch_suspended_task(sched_rp, tinfo->task);
372 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
376 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
377 regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
385 void dbi_jprobe_return (void)
389 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
393 EXPORT_SYMBOL_GPL(longjmp_break_handler);
395 void arch_arm_kprobe(struct kprobe *p)
397 *p->addr = BREAKPOINT_INSTRUCTION;
398 flush_icache_range((unsigned long)p->addr, (unsigned long)p->addr + sizeof(kprobe_opcode_t));
401 void arch_disarm_kprobe(struct kprobe *p)
403 *p->addr = p->opcode;
404 flush_icache_range((unsigned long)p->addr, (unsigned long)p->addr + sizeof(kprobe_opcode_t));
407 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
409 struct kretprobe_instance *ri = NULL;
410 struct hlist_head *head;
411 struct hlist_node *node, *tmp;
412 unsigned long flags, orig_ret_address = 0;
413 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
415 struct kretprobe *crp = NULL;
416 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
418 spin_lock_irqsave(&kretprobe_lock, flags);
421 * We are using different hash keys (current and mm) for finding kernel
422 * space and user space probes. Kernel space probes can change mm field in
423 * task_struct. User space probes can be shared between threads of one
424 * process so they have different current but same mm.
426 head = kretprobe_inst_table_head(current);
429 * It is possible to have multiple instances associated with a given
430 * task either because an multiple functions in the call path
431 * have a return probe installed on them, and/or more then one
432 * return probe was registered for a target function.
434 * We can handle this because:
435 * - instances are always inserted at the head of the list
436 * - when multiple return probes are registered for the same
437 * function, the first instance's ret_addr will point to the
438 * real return address, and all the rest will point to
439 * kretprobe_trampoline
441 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
442 if (ri->task != current)
443 /* another task is sharing our hash bucket */
445 if (ri->rp && ri->rp->handler) {
446 ri->rp->handler(ri, regs, ri->rp->priv_arg);
449 orig_ret_address = (unsigned long)ri->ret_addr;
451 if (orig_ret_address != trampoline_address)
453 * This is the real return address. Any other
454 * instances associated with this task are for
455 * other calls deeper on the call stack
459 kretprobe_assert(ri, orig_ret_address, trampoline_address);
461 regs->ARM_lr = orig_ret_address;
462 regs->ARM_pc = orig_ret_address;
464 if (kcb->kprobe_status == KPROBE_REENTER) {
465 restore_previous_kprobe(kcb);
467 reset_current_kprobe();
470 spin_unlock_irqrestore(&kretprobe_lock, flags);
473 * By returning a non-zero value, we are telling
474 * kprobe_handler() that we don't want the post_handler
475 * to run (and have re-enabled preemption)
480 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
482 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
484 struct kretprobe_instance *ri;
486 DBPRINTF ("start\n");
487 //TODO: test - remove retprobe after func entry but before its exit
488 if ((ri = get_free_rp_inst(rp)) != NULL) {
491 ri->ret_addr = (kprobe_opcode_t *)regs->uregs[14];
492 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
494 /* Set flag of current mode */
495 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
497 /* Replace the return addr with trampoline addr */
498 regs->uregs[14] = (unsigned long)&kretprobe_trampoline;
500 // DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
503 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
508 void swap_register_undef_hook(struct undef_hook *hook)
510 __swap_register_undef_hook(hook);
512 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
514 void swap_unregister_undef_hook(struct undef_hook *hook)
516 __swap_unregister_undef_hook(hook);
518 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
520 // kernel probes hook
521 static struct undef_hook undef_ho_k = {
522 .instr_mask = 0xffffffff,
523 .instr_val = BREAKPOINT_INSTRUCTION,
524 .cpsr_mask = MODE_MASK,
525 .cpsr_val = SVC_MODE,
526 .fn = kprobe_trap_handler
529 int arch_init_kprobes(void)
533 // Register hooks (kprobe_handler)
534 __swap_register_undef_hook = swap_ksyms("register_undef_hook");
535 if (__swap_register_undef_hook == NULL) {
536 printk("no register_undef_hook symbol found!\n");
540 // Unregister hooks (kprobe_handler)
541 __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
542 if (__swap_unregister_undef_hook == NULL) {
543 printk("no unregister_undef_hook symbol found!\n");
547 swap_register_undef_hook(&undef_ho_k);
548 if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
549 //dbi_unregister_jprobe(&do_exit_p, 0);
556 void arch_exit_kprobes(void)
558 swap_unregister_undef_hook(&undef_ho_k);
561 /* export symbol for trampoline_arm.h */
562 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
563 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);