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 "dbi_kprobes_arm.h"
37 #include "dbi_kprobes_thumb.h"
38 #include "../dbi_kprobes.h"
39 #include "../../dbi_kprobes.h"
41 #include "../../dbi_kdebug.h"
42 #include "../../dbi_insn_slots.h"
43 #include "../../dbi_kprobes_deps.h"
46 #include <asm/cacheflush.h>
48 #ifdef TRAP_OVERHEAD_DEBUG
49 #include <linux/pid.h>
50 #include <linux/signal.h>
54 #include <linux/time.h>
57 #include <asm/traps.h>
58 #include <asm/ptrace.h>
59 #include <linux/list.h>
60 #include <linux/hash.h>
62 #define SUPRESS_BUG_MESSAGES
64 extern struct kprobe * per_cpu__current_kprobe;
65 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
67 static void (*__swap_register_undef_hook)(struct undef_hook *hook);
68 static void (*__swap_unregister_undef_hook)(struct undef_hook *hook);
71 unsigned long swap_sum_time = 0;
72 unsigned long swap_sum_hit = 0;
73 EXPORT_SYMBOL_GPL (swap_sum_time);
74 EXPORT_SYMBOL_GPL (swap_sum_hit);
77 static unsigned int arr_traps_template[] = {
78 0xe1a0c00d, // mov ip, sp
79 0xe92dd800, // stmdb sp!, {fp, ip, lr, pc}
80 0xe24cb004, // sub fp, ip, #4 ; 0x4
82 0xe3500000, // cmp r0, #0 ; 0x0
83 0xe89da800, // ldmia sp, {fp, sp, pc}
89 static struct kprobe trampoline_p =
91 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
92 .pre_handler = trampoline_probe_handler
95 // is instruction Thumb2 and NOT a branch, etc...
96 int isThumb2(kprobe_opcode_t insn)
98 if(( (insn & 0xf800) == 0xe800 ||
99 (insn & 0xf800) == 0xf000 ||
100 (insn & 0xf800) == 0xf800)) return 1;
103 EXPORT_SYMBOL_GPL(isThumb2);
105 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs)
113 for (i = 0; i < 13; i++, reg_mask <<= 1)
115 if (!(insn & reg_mask))
121 for (i = 0; i < 13; i++)
123 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == i))
125 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == i))
127 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == i))
129 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == i))
136 DBPRINTF ("there are no free register %x in insn %lx!", uregs, insn);
139 DBPRINTF ("prep_pc_dep_insn_execbuf: using R%d, changing regs %x", i, uregs);
141 // set register to save
142 ARM_INSN_REG_SET_RD (insns[0], i);
143 // set register to load address to
144 ARM_INSN_REG_SET_RD (insns[1], i);
145 // set instruction to execute and patch it
148 ARM_INSN_REG_CLEAR_MR (insn, 15);
149 ARM_INSN_REG_SET_MR (insn, i);
153 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == 15))
154 ARM_INSN_REG_SET_RN (insn, i);
155 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == 15))
156 ARM_INSN_REG_SET_RD (insn, i);
157 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == 15))
158 ARM_INSN_REG_SET_RS (insn, i);
159 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == 15))
160 ARM_INSN_REG_SET_RM (insn, i);
162 insns[UPROBES_TRAMP_INSN_IDX] = insn;
163 // set register to restore
164 ARM_INSN_REG_SET_RD (insns[3], i);
167 EXPORT_SYMBOL_GPL(prep_pc_dep_insn_execbuf);
169 int arch_check_insn_arm(struct arch_specific_insn *ainsn)
173 // check instructions that can change PC by nature
175 // ARM_INSN_MATCH (UNDEF, ainsn->insn_arm[0]) ||
176 ARM_INSN_MATCH (AUNDEF, ainsn->insn_arm[0]) ||
177 ARM_INSN_MATCH (SWI, ainsn->insn_arm[0]) ||
178 ARM_INSN_MATCH (BREAK, ainsn->insn_arm[0]) ||
179 ARM_INSN_MATCH (BL, ainsn->insn_arm[0]) ||
180 ARM_INSN_MATCH (BLX1, ainsn->insn_arm[0]) ||
181 ARM_INSN_MATCH (BLX2, ainsn->insn_arm[0]) ||
182 ARM_INSN_MATCH (BX, ainsn->insn_arm[0]) ||
183 ARM_INSN_MATCH (BXJ, ainsn->insn_arm[0]))
185 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
188 #ifndef CONFIG_CPU_V7
189 // check instructions that can write result to PC
190 else if ((ARM_INSN_MATCH (DPIS, ainsn->insn_arm[0]) ||
191 ARM_INSN_MATCH (DPRS, ainsn->insn_arm[0]) ||
192 ARM_INSN_MATCH (DPI, ainsn->insn_arm[0]) ||
193 ARM_INSN_MATCH (LIO, ainsn->insn_arm[0]) ||
194 ARM_INSN_MATCH (LRO, ainsn->insn_arm[0])) &&
195 (ARM_INSN_REG_RD (ainsn->insn_arm[0]) == 15))
197 DBPRINTF ("Bad arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
200 #endif // CONFIG_CPU_V7
201 // check special instruction loads store multiple registers
202 else if ((ARM_INSN_MATCH (LM, ainsn->insn_arm[0]) || ARM_INSN_MATCH (SM, ainsn->insn_arm[0])) &&
203 // store pc or load to pc
204 (ARM_INSN_REG_MR (ainsn->insn_arm[0], 15) ||
205 // store/load with pc update
206 ((ARM_INSN_REG_RN (ainsn->insn_arm[0]) == 15) && (ainsn->insn_arm[0] & 0x200000))))
208 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
213 EXPORT_SYMBOL_GPL(arch_check_insn_arm);
215 int arch_prepare_kretprobe (struct kretprobe *p)
217 DBPRINTF("Warrning: arch_prepare_kretprobe is not implemented\n");
221 int arch_prepare_kprobe (struct kprobe *p)
223 kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
224 int uregs, pc_dep, ret = 0;
225 kprobe_opcode_t insn[MAX_INSN_SIZE];
226 struct arch_specific_insn ainsn;
228 /* insn: must be on special executable page on i386. */
229 p->ainsn.insn = get_insn_slot(NULL, &kprobe_insn_pages, 0);
233 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
234 ainsn.insn_arm = ainsn.insn = insn;
235 ret = arch_check_insn_arm (&ainsn);
238 p->opcode = *p->addr;
242 if(ARM_INSN_MATCH (DPIS, insn[0]) || ARM_INSN_MATCH (LRO, insn[0]) ||
243 ARM_INSN_MATCH (SRO, insn[0]))
246 if( (ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
247 (ARM_INSN_MATCH (SRO, insn[0]) && (ARM_INSN_REG_RD (insn[0]) == 15)) )
249 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
254 else if(ARM_INSN_MATCH (DPI, insn[0]) || ARM_INSN_MATCH (LIO, insn[0]) ||
255 ARM_INSN_MATCH (SIO, insn[0]))
258 if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_MATCH (SIO, insn[0]) &&
259 (ARM_INSN_REG_RD (insn[0]) == 15)))
262 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
266 else if(ARM_INSN_MATCH (DPRS, insn[0]))
269 if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
270 (ARM_INSN_REG_RS (insn[0]) == 15))
273 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
277 else if(ARM_INSN_MATCH (SM, insn[0]))
280 if (ARM_INSN_REG_MR (insn[0], 15))
282 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
286 // check instructions that can write result to SP andu uses PC
287 if (pc_dep && (ARM_INSN_REG_RD (ainsn.insn[0]) == 13))
289 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
296 memcpy (insns, pc_dep_insn_execbuf, sizeof (insns));
297 if (prep_pc_dep_insn_execbuf (insns, insn[0], uregs) != 0)
299 DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
300 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
303 insns[6] = (kprobe_opcode_t) (p->addr + 2);
307 memcpy (insns, gen_insn_execbuf, sizeof (insns));
308 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
310 insns[7] = (kprobe_opcode_t) (p->addr + 1);
311 DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
312 DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
313 p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
314 insns[5], insns[6], insns[7], insns[8]);
315 memcpy (p->ainsn.insn, insns, sizeof(insns));
316 flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
324 free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
325 printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
331 static unsigned int arch_construct_brunch (unsigned int base, unsigned int addr, int link)
333 kprobe_opcode_t insn;
334 unsigned int bpi = (unsigned int) base - (unsigned int) addr - 8;
337 DBPRINTF ("base=%x addr=%x base-addr-8=%x\n", base, addr, bpi);
338 if (abs (insn & 0xffffff) > 0xffffff)
340 DBPRINTF ("ERROR: kprobe address out of range\n");
343 insn = insn & 0xffffff;
344 insn = insn | ((link != 0) ? 0xeb000000 : 0xea000000);
345 DBPRINTF ("insn=%lX\n", insn);
346 return (unsigned int) insn;
349 int arch_prepare_uretprobe (struct kretprobe *p, struct task_struct *task)
351 DBPRINTF("Warrning: arch_prepare_uretprobe is not implemented\n");
354 EXPORT_SYMBOL_GPL(arch_prepare_uretprobe);
356 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
359 regs->ARM_pc = (unsigned long)p->ss_addr;
362 regs->ARM_pc = (unsigned long)p->ainsn.insn;
366 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
369 panic("arm_save_previous_kprobe: p_run == NULL\n");
372 if (kcb->prev_kprobe.kp != NULL) {
373 DBPRINTF ("no space to save new probe[]: task = %d/%s", current->pid, current->comm);
376 kcb->prev_kprobe.kp = p_run;
377 kcb->prev_kprobe.status = kcb->kprobe_status;
380 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
382 set_current_kprobe(kcb->prev_kprobe.kp, NULL, NULL);
383 kcb->kprobe_status = kcb->prev_kprobe.status;
384 kcb->prev_kprobe.kp = NULL;
385 kcb->prev_kprobe.status = 0;
388 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
390 __get_cpu_var(current_kprobe) = p;
391 DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
394 static int check_validity_insn(struct kprobe *p, struct pt_regs *regs, struct task_struct *task)
398 if (unlikely(thumb_mode(regs))) {
399 if (p->safe_thumb != -1) {
400 p->ainsn.insn = p->ainsn.insn_thumb;
401 list_for_each_entry_rcu(kp, &p->list, list) {
402 kp->ainsn.insn = p->ainsn.insn_thumb;
405 printk("Error in %s at %d: we are in thumb mode (!) and check instruction was fail \
406 (%0lX instruction at %p address)!\n", __FILE__, __LINE__, p->opcode, p->addr);
407 // Test case when we do our actions on already running application
408 arch_disarm_uprobe(p, task);
412 if (p->safe_arm != -1) {
413 p->ainsn.insn = p->ainsn.insn_arm;
414 list_for_each_entry_rcu(kp, &p->list, list) {
415 kp->ainsn.insn = p->ainsn.insn_arm;
418 printk("Error in %s at %d: we are in arm mode (!) and check instruction was fail \
419 (%0lX instruction at %p address)!\n", __FILE__, __LINE__, p->opcode, p->addr);
420 // Test case when we do our actions on already running application
421 arch_disarm_uprobe(p, task);
429 #ifdef TRAP_OVERHEAD_DEBUG
430 static unsigned long trap_handler_counter_debug = 0;
431 #define SAMPLING_COUNTER 100000
434 static int kprobe_handler(struct pt_regs *regs)
437 char *msg_out = NULL;
438 unsigned long user_m = user_mode(regs);
439 pid_t tgid = (user_m) ? current->tgid : 0;
440 kprobe_opcode_t *addr = (kprobe_opcode_t *) (regs->ARM_pc);
442 struct kprobe *p = NULL, *p_run = NULL;
443 int ret = 0, retprobe = 0, reenter = 0;
444 kprobe_opcode_t *ssaddr = NULL;
445 struct kprobe_ctlblk *kcb;
447 #ifdef SUPRESS_BUG_MESSAGES
448 int swap_oops_in_progress;
449 // oops_in_progress used to avoid BUG() messages that slow down kprobe_handler() execution
450 swap_oops_in_progress = oops_in_progress;
451 oops_in_progress = 1;
453 #ifdef TRAP_OVERHEAD_DEBUG
454 trap_handler_counter_debug++;
455 if ( trap_handler_counter_debug < SAMPLING_COUNTER ) {
459 // XXX NOTE - user must care about catching signal via signal handler to avoid hanging!
460 printk("Trap %ld reached - send SIGUSR1\n", trap_handler_counter_debug);
461 kill_pid(get_task_pid(current, PIDTYPE_PID), SIGUSR1, 1);
462 trap_handler_counter_debug = 0;
467 #ifdef OVERHEAD_DEBUG
468 struct timeval swap_tv1;
469 struct timeval swap_tv2;
470 #define USEC_IN_SEC_NUM 1000000
471 do_gettimeofday(&swap_tv1);
475 // printk("### kprobe_handler: task[tgid=%u (%s)] addr=%p\n", tgid, current->comm, addr);
476 p = get_kprobe(addr, tgid);
478 if (user_m && p && (check_validity_insn(p, regs, current) != 0)) {
482 /* We're in an interrupt, but this is clear and BUG()-safe. */
483 kcb = get_kprobe_ctlblk ();
485 /* Check we're not actually recursing */
486 // TODO: event is not saving in trace
487 p_run = kprobe_running();
493 if (!tgid && (addr == (kprobe_opcode_t *)kretprobe_trampoline)) {
494 save_previous_kprobe(kcb, p_run);
495 kcb->kprobe_status = KPROBE_REENTER;
498 /* We have reentered the kprobe_handler(), since
499 * another probe was hit while within the handler.
500 * We here save the original kprobes variables and
501 * just single step on the instruction of the new probe
502 * without calling any user handlers.
504 kprobes_inc_nmissed_count (p);
505 prepare_singlestep (p, regs);
511 if(tgid) { //we can reenter probe upon uretprobe exception
512 DBPRINTF ("check for UNDEF_INSTRUCTION %p\n", addr);
513 // UNDEF_INSTRUCTION from user space
515 p = get_kprobe_by_insn_slot(addr, tgid, regs);
517 save_previous_kprobe(kcb, p_run);
518 kcb->kprobe_status = KPROBE_REENTER;
521 DBPRINTF ("uretprobe %p\n", addr);
526 DBPRINTF ("kprobe_running !!! p = 0x%p p->break_handler = 0x%p", p, p->break_handler);
527 /*if (p->break_handler && p->break_handler(p, regs)) {
528 DBPRINTF("kprobe_running !!! goto ss");
531 DBPRINTF ("unknown uprobe at %p cur at %p/%p\n", addr, p->addr, p->ainsn.insn);
533 ssaddr = p->ainsn.insn + UPROBES_TRAMP_SS_BREAK_IDX;
535 ssaddr = p->ainsn.insn + KPROBES_TRAMP_SS_BREAK_IDX;
536 if (addr == ssaddr) {
537 regs->ARM_pc = (unsigned long) (p->addr + 1);
538 DBPRINTF ("finish step at %p cur at %p/%p, redirect to %lx\n", addr, p->addr, p->ainsn.insn, regs->ARM_pc);
539 if (kcb->kprobe_status == KPROBE_REENTER) {
540 restore_previous_kprobe(kcb);
542 reset_current_kprobe();
545 DBPRINTF ("kprobe_running !!! goto no");
547 /* If it's not ours, can't be delete race, (we hold lock). */
548 DBPRINTF ("no_kprobe");
556 DBPRINTF ("search UNDEF_INSTRUCTION %p\n", addr);
557 // UNDEF_INSTRUCTION from user space
559 p = get_kprobe_by_insn_slot(addr, tgid, regs);
561 /* Not one of ours: let kernel handle it */
562 DBPRINTF ("no_kprobe");
566 DBPRINTF ("uretprobe %p\n", addr);
568 /* Not one of ours: let kernel handle it */
569 DBPRINTF ("no_kprobe");
573 // restore opcode for thumb app
574 if (user_mode( regs ) && thumb_mode( regs )) {
575 if (!isThumb2(p->opcode)) {
576 unsigned long tmp = p->opcode >> 16;
577 write_proc_vm_atomic(current, (unsigned long)((unsigned short*)p->addr + 1), &tmp, 2);
579 // "2*sizeof(kprobe_opcode_t)" - strange. Should be "sizeof(kprobe_opcode_t)", need to test
580 flush_icache_range((unsigned int) p->addr, ((unsigned int)p->addr) + (2 * sizeof(kprobe_opcode_t)));
583 set_current_kprobe(p, NULL, NULL);
585 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
586 if (retprobe) { //(einsn == UNDEF_INSTRUCTION)
587 ret = trampoline_probe_handler (p, regs);
588 } else if (p->pre_handler) {
589 ret = p->pre_handler (p, regs);
590 if(p->pre_handler != trampoline_probe_handler) {
591 reset_current_kprobe();
596 /* handler has already set things up, so skip ss setup */
602 msg_out = "no_kprobe\n";
603 err_out = 1; // return with death
607 msg_out = "no_kprobe live\n";
608 err_out = 0; // ok - life is life
612 preempt_enable_no_resched();
613 #ifdef OVERHEAD_DEBUG
614 do_gettimeofday(&swap_tv2);
616 swap_sum_time += ((swap_tv2.tv_sec - swap_tv1.tv_sec) * USEC_IN_SEC_NUM +
617 (swap_tv2.tv_usec - swap_tv1.tv_usec));
619 #ifdef SUPRESS_BUG_MESSAGES
620 oops_in_progress = swap_oops_in_progress;
630 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
634 local_irq_save(flags);
635 ret = kprobe_handler(regs);
636 local_irq_restore(flags);
639 EXPORT_SYMBOL_GPL(kprobe_trap_handler);
641 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs)
643 struct jprobe *jp = container_of (p, struct jprobe, kp);
644 kprobe_pre_entry_handler_t pre_entry;
648 // p = kprobe_running(regs);
651 DBPRINTF ("pjp = 0x%p jp->entry = 0x%p", jp, jp->entry);
652 entry = (entry_point_t) jp->entry;
653 pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
655 // DIE("entry NULL", regs)
656 DBPRINTF ("entry = 0x%p jp->entry = 0x%p", entry, jp->entry);
658 //call handler for all kernel probes and user space ones which belong to current tgid
659 if (!p->tgid || (p->tgid == current->tgid))
661 if(!p->tgid && ((unsigned int)p->addr == sched_addr) && sched_rp) {
662 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
663 patch_suspended_task(sched_rp, tinfo->task);
666 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
668 entry (regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
672 dbi_arch_uprobe_return ();
674 dbi_jprobe_return ();
678 dbi_arch_uprobe_return ();
680 prepare_singlestep (p, regs);
684 EXPORT_SYMBOL_GPL(setjmp_pre_handler);
686 void dbi_jprobe_return (void)
690 void dbi_arch_uprobe_return (void)
693 EXPORT_SYMBOL_GPL(dbi_arch_uprobe_return);
695 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
698 //kprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
699 kprobe_opcode_t insns[2];
703 insns[0] = BREAKPOINT_INSTRUCTION;
704 insns[1] = p->opcode;
705 //p->opcode = *p->addr;
706 if (read_proc_vm_atomic (current, (unsigned long) (p->addr), &(p->opcode), sizeof (p->opcode)) < sizeof (p->opcode))
708 printk ("ERROR[%lu]: failed to read vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
711 //*p->addr = BREAKPOINT_INSTRUCTION;
712 //*(p->addr+1) = p->opcode;
713 if (write_proc_vm_atomic (current, (unsigned long) (p->addr), insns, sizeof (insns)) < sizeof (insns))
715 printk ("ERROR[%lu]: failed to write vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
721 DBPRINTF ("p->opcode = 0x%lx *p->addr = 0x%lx p->addr = 0x%p\n", p->opcode, *p->addr, p->addr);
722 *(p->addr + 1) = p->opcode;
723 p->opcode = *p->addr;
724 *p->addr = BREAKPOINT_INSTRUCTION;
726 flush_icache_range ((unsigned int) p->addr, (unsigned int) (((unsigned int) p->addr) + (sizeof (kprobe_opcode_t) * 2)));
729 reset_current_kprobe();
735 EXPORT_SYMBOL_GPL(longjmp_break_handler);
737 void arch_arm_kprobe (struct kprobe *p)
739 *p->addr = BREAKPOINT_INSTRUCTION;
740 flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
743 void arch_disarm_kprobe (struct kprobe *p)
745 *p->addr = p->opcode;
746 flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
750 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
752 struct kretprobe_instance *ri = NULL;
753 struct hlist_head *head;
754 struct hlist_node *node, *tmp;
755 unsigned long flags, orig_ret_address = 0;
756 unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
758 struct kretprobe *crp = NULL;
759 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
764 // in case of user space retprobe trampoline is at the Nth instruction of US tramp
765 if (!thumb_mode( regs ))
766 trampoline_address = (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
768 trampoline_address = (unsigned long)(p->ainsn.insn) + 0x1b;
771 spin_lock_irqsave (&kretprobe_lock, flags);
774 * We are using different hash keys (current and mm) for finding kernel
775 * space and user space probes. Kernel space probes can change mm field in
776 * task_struct. User space probes can be shared between threads of one
777 * process so they have different current but same mm.
780 head = kretprobe_inst_table_head(current->mm);
782 head = kretprobe_inst_table_head(current);
786 * It is possible to have multiple instances associated with a given
787 * task either because an multiple functions in the call path
788 * have a return probe installed on them, and/or more then one
789 * return probe was registered for a target function.
791 * We can handle this because:
792 * - instances are always inserted at the head of the list
793 * - when multiple return probes are registered for the same
794 * function, the first instance's ret_addr will point to the
795 * real return address, and all the rest will point to
796 * kretprobe_trampoline
798 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
800 if (ri->task != current)
801 /* another task is sharing our hash bucket */
803 if (ri->rp && ri->rp->handler){
804 ri->rp->handler (ri, regs, ri->rp->priv_arg);
807 orig_ret_address = (unsigned long) ri->ret_addr;
808 recycle_rp_inst (ri);
809 if (orig_ret_address != trampoline_address)
811 * This is the real return address. Any other
812 * instances associated with this task are for
813 * other calls deeper on the call stack
817 kretprobe_assert (ri, orig_ret_address, trampoline_address);
818 //BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
819 //E.G. Check this code in case of __switch_to function instrumentation -- currently this code generates dump in this case
820 //if (trampoline_address != (unsigned long) &kretprobe_trampoline){
821 //if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
822 //if (ri->rp) BUG_ON (ri->rp->kp.tgid == 0);
823 //else if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
825 if ((ri->rp && ri->rp->kp.tgid) || (ri->rp2 && ri->rp2->kp.tgid))
826 BUG_ON (trampoline_address == (unsigned long) &kretprobe_trampoline);
828 regs->uregs[14] = orig_ret_address;
829 DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
830 DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
832 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
834 regs->uregs[15] = orig_ret_address;
836 if (!thumb_mode( regs )) regs->uregs[15] += 4;
837 else regs->uregs[15] += 2;
840 DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
842 if(p){ // ARM, MIPS, X86 user space
843 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
845 regs->ARM_cpsr &= 0xFFFFFFDF;
847 if (user_mode( regs ) && (regs->uregs[14] & 0x01))
849 regs->ARM_cpsr |= 0x20;
853 //TODO: test - enter function, delete us retprobe, exit function
854 // for user space retprobes only - deferred deletion
856 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
858 // if we are not at the end of the list and current retprobe should be disarmed
861 struct hlist_node *current_node = node;
863 /*sprintf(die_msg, "deferred disarm p->addr = %p [%lx %lx %lx]\n",
864 crp->kp.addr, *kaddrs[0], *kaddrs[1], *kaddrs[2]);
865 DIE(die_msg, regs); */
866 // look for other instances for the same retprobe
867 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
870 * Trying to find another retprobe instance associated with
873 if (ri->rp2 == crp && node != current_node)
879 // if there are no more instances for this retprobe
881 struct kprobe *is_p = &crp->kp;
882 DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
884 If there is no any retprobe instances of this retprobe
885 we can free the resources related to the probe.
887 if (!(hlist_unhashed(&is_p->is_hlist_arm))) {
888 hlist_del_rcu(&is_p->is_hlist_arm);
890 if (!(hlist_unhashed(&is_p->is_hlist_thumb))) {
891 hlist_del_rcu(&is_p->is_hlist_thumb);
894 dbi_unregister_kprobe(&crp->kp, current);
897 hlist_del(current_node);
901 if (kcb->kprobe_status == KPROBE_REENTER) {
902 restore_previous_kprobe(kcb);
904 reset_current_kprobe();
908 spin_unlock_irqrestore (&kretprobe_lock, flags);
911 * By returning a non-zero value, we are telling
912 * kprobe_handler() that we don't want the post_handler
913 * to run (and have re-enabled preemption)
919 void __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
921 struct kretprobe_instance *ri;
923 DBPRINTF ("start\n");
924 //TODO: test - remove retprobe after func entry but before its exit
925 if ((ri = get_free_rp_inst (rp)) != NULL)
930 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
931 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
934 if (!thumb_mode( regs ))
935 regs->uregs[14] = (unsigned long) (rp->kp.ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
937 regs->uregs[14] = (unsigned long) (rp->kp.ainsn.insn) + 0x1b;
939 else /* Replace the return addr with trampoline addr */
940 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
942 // DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
946 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
952 int asm_init_module_dependencies(void)
954 //No module dependencies
958 void swap_register_undef_hook(struct undef_hook *hook)
960 __swap_register_undef_hook(hook);
962 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
964 void swap_unregister_undef_hook(struct undef_hook *hook)
966 __swap_unregister_undef_hook(hook);
968 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
970 // kernel probes hook
971 static struct undef_hook undef_ho_k = {
972 .instr_mask = 0xffffffff,
973 .instr_val = BREAKPOINT_INSTRUCTION,
974 .cpsr_mask = MODE_MASK,
975 .cpsr_val = SVC_MODE,
976 .fn = kprobe_trap_handler
979 int __init arch_init_kprobes (void)
981 unsigned int do_bp_handler = 0;
984 if (arch_init_module_dependencies())
986 DBPRINTF ("Unable to init module dependencies\n");
990 do_bp_handler = swap_ksyms("do_undefinstr");
991 if (do_bp_handler == 0) {
992 DBPRINTF("no do_undefinstr symbol found!");
995 arr_traps_template[NOTIFIER_CALL_CHAIN_INDEX] = arch_construct_brunch ((unsigned int)kprobe_handler, do_bp_handler + NOTIFIER_CALL_CHAIN_INDEX * 4, 1);
996 // Register hooks (kprobe_handler)
997 __swap_register_undef_hook = swap_ksyms("register_undef_hook");
998 if (__swap_register_undef_hook == NULL) {
999 printk("no register_undef_hook symbol found!\n");
1003 // Unregister hooks (kprobe_handler)
1004 __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
1005 if (__swap_unregister_undef_hook == NULL) {
1006 printk("no unregister_undef_hook symbol found!\n");
1010 swap_register_undef_hook(&undef_ho_k);
1011 if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
1012 //dbi_unregister_jprobe(&do_exit_p, 0);
1018 void __exit dbi_arch_exit_kprobes (void)
1020 swap_unregister_undef_hook(&undef_ho_k);
1023 //EXPORT_SYMBOL_GPL (dbi_arch_uprobe_return);
1024 //EXPORT_SYMBOL_GPL (dbi_arch_exit_kprobes);