kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * arch/arm/kernel/kprobes.c
  3. *
  4. * Kprobes on ARM
  5. *
  6. * Abhishek Sagar <sagar.abhishek@gmail.com>
  7. * Copyright (C) 2006, 2007 Motorola Inc.
  8. *
  9. * Nicolas Pitre <nico@marvell.com>
  10. * Copyright (C) 2007 Marvell Ltd.
  11. *
  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.
  15. *
  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.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/stringify.h>
  27. #include <asm/traps.h>
  28. #include <asm/cacheflush.h>
  29. #define MIN_STACK_SIZE(addr) \
  30. min((unsigned long)MAX_STACK_SIZE, \
  31. (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
  32. #define flush_insns(addr, cnt) \
  33. flush_icache_range((unsigned long)(addr), \
  34. (unsigned long)(addr) + \
  35. sizeof(kprobe_opcode_t) * (cnt))
  36. /* Used as a marker in ARM_pc to note when we're in a jprobe. */
  37. #define JPROBE_MAGIC_ADDR 0xffffffff
  38. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  39. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  40. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  41. {
  42. kprobe_opcode_t insn;
  43. kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
  44. unsigned long addr = (unsigned long)p->addr;
  45. int is;
  46. if (addr & 0x3 || in_exception_text(addr))
  47. return -EINVAL;
  48. insn = *p->addr;
  49. p->opcode = insn;
  50. p->ainsn.insn = tmp_insn;
  51. switch (arm_kprobe_decode_insn(insn, &p->ainsn)) {
  52. case INSN_REJECTED: /* not supported */
  53. return -EINVAL;
  54. case INSN_GOOD: /* instruction uses slot */
  55. p->ainsn.insn = get_insn_slot();
  56. if (!p->ainsn.insn)
  57. return -ENOMEM;
  58. for (is = 0; is < MAX_INSN_SIZE; ++is)
  59. p->ainsn.insn[is] = tmp_insn[is];
  60. flush_insns(p->ainsn.insn, MAX_INSN_SIZE);
  61. break;
  62. case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
  63. p->ainsn.insn = NULL;
  64. break;
  65. }
  66. return 0;
  67. }
  68. void __kprobes arch_arm_kprobe(struct kprobe *p)
  69. {
  70. *p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
  71. flush_insns(p->addr, 1);
  72. }
  73. /*
  74. * The actual disarming is done here on each CPU and synchronized using
  75. * stop_machine. This synchronization is necessary on SMP to avoid removing
  76. * a probe between the moment the 'Undefined Instruction' exception is raised
  77. * and the moment the exception handler reads the faulting instruction from
  78. * memory.
  79. */
  80. int __kprobes __arch_disarm_kprobe(void *p)
  81. {
  82. struct kprobe *kp = p;
  83. *kp->addr = kp->opcode;
  84. flush_insns(kp->addr, 1);
  85. return 0;
  86. }
  87. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  88. {
  89. stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
  90. }
  91. void __kprobes arch_remove_kprobe(struct kprobe *p)
  92. {
  93. if (p->ainsn.insn) {
  94. free_insn_slot(p->ainsn.insn, 0);
  95. p->ainsn.insn = NULL;
  96. }
  97. }
  98. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  99. {
  100. kcb->prev_kprobe.kp = kprobe_running();
  101. kcb->prev_kprobe.status = kcb->kprobe_status;
  102. }
  103. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  104. {
  105. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  106. kcb->kprobe_status = kcb->prev_kprobe.status;
  107. }
  108. static void __kprobes set_current_kprobe(struct kprobe *p)
  109. {
  110. __get_cpu_var(current_kprobe) = p;
  111. }
  112. static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
  113. struct kprobe_ctlblk *kcb)
  114. {
  115. regs->ARM_pc += 4;
  116. p->ainsn.insn_handler(p, regs);
  117. }
  118. /*
  119. * Called with IRQs disabled. IRQs must remain disabled from that point
  120. * all the way until processing this kprobe is complete. The current
  121. * kprobes implementation cannot process more than one nested level of
  122. * kprobe, and that level is reserved for user kprobe handlers, so we can't
  123. * risk encountering a new kprobe in an interrupt handler.
  124. */
  125. void __kprobes kprobe_handler(struct pt_regs *regs)
  126. {
  127. struct kprobe *p, *cur;
  128. struct kprobe_ctlblk *kcb;
  129. kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->ARM_pc;
  130. kcb = get_kprobe_ctlblk();
  131. cur = kprobe_running();
  132. p = get_kprobe(addr);
  133. if (p) {
  134. if (cur) {
  135. /* Kprobe is pending, so we're recursing. */
  136. switch (kcb->kprobe_status) {
  137. case KPROBE_HIT_ACTIVE:
  138. case KPROBE_HIT_SSDONE:
  139. /* A pre- or post-handler probe got us here. */
  140. kprobes_inc_nmissed_count(p);
  141. save_previous_kprobe(kcb);
  142. set_current_kprobe(p);
  143. kcb->kprobe_status = KPROBE_REENTER;
  144. singlestep(p, regs, kcb);
  145. restore_previous_kprobe(kcb);
  146. break;
  147. default:
  148. /* impossible cases */
  149. BUG();
  150. }
  151. } else {
  152. set_current_kprobe(p);
  153. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  154. /*
  155. * If we have no pre-handler or it returned 0, we
  156. * continue with normal processing. If we have a
  157. * pre-handler and it returned non-zero, it prepped
  158. * for calling the break_handler below on re-entry,
  159. * so get out doing nothing more here.
  160. */
  161. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  162. kcb->kprobe_status = KPROBE_HIT_SS;
  163. singlestep(p, regs, kcb);
  164. if (p->post_handler) {
  165. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  166. p->post_handler(p, regs, 0);
  167. }
  168. reset_current_kprobe();
  169. }
  170. }
  171. } else if (cur) {
  172. /* We probably hit a jprobe. Call its break handler. */
  173. if (cur->break_handler && cur->break_handler(cur, regs)) {
  174. kcb->kprobe_status = KPROBE_HIT_SS;
  175. singlestep(cur, regs, kcb);
  176. if (cur->post_handler) {
  177. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  178. cur->post_handler(cur, regs, 0);
  179. }
  180. }
  181. reset_current_kprobe();
  182. } else {
  183. /*
  184. * The probe was removed and a race is in progress.
  185. * There is nothing we can do about it. Let's restart
  186. * the instruction. By the time we can restart, the
  187. * real instruction will be there.
  188. */
  189. }
  190. }
  191. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
  192. {
  193. unsigned long flags;
  194. local_irq_save(flags);
  195. kprobe_handler(regs);
  196. local_irq_restore(flags);
  197. return 0;
  198. }
  199. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
  200. {
  201. struct kprobe *cur = kprobe_running();
  202. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  203. switch (kcb->kprobe_status) {
  204. case KPROBE_HIT_SS:
  205. case KPROBE_REENTER:
  206. /*
  207. * We are here because the instruction being single
  208. * stepped caused a page fault. We reset the current
  209. * kprobe and the PC to point back to the probe address
  210. * and allow the page fault handler to continue as a
  211. * normal page fault.
  212. */
  213. regs->ARM_pc = (long)cur->addr;
  214. if (kcb->kprobe_status == KPROBE_REENTER) {
  215. restore_previous_kprobe(kcb);
  216. } else {
  217. reset_current_kprobe();
  218. }
  219. break;
  220. case KPROBE_HIT_ACTIVE:
  221. case KPROBE_HIT_SSDONE:
  222. /*
  223. * We increment the nmissed count for accounting,
  224. * we can also use npre/npostfault count for accounting
  225. * these specific fault cases.
  226. */
  227. kprobes_inc_nmissed_count(cur);
  228. /*
  229. * We come here because instructions in the pre/post
  230. * handler caused the page_fault, this could happen
  231. * if handler tries to access user space by
  232. * copy_from_user(), get_user() etc. Let the
  233. * user-specified handler try to fix it.
  234. */
  235. if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
  236. return 1;
  237. break;
  238. default:
  239. break;
  240. }
  241. return 0;
  242. }
  243. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  244. unsigned long val, void *data)
  245. {
  246. /*
  247. * notify_die() is currently never called on ARM,
  248. * so this callback is currently empty.
  249. */
  250. return NOTIFY_DONE;
  251. }
  252. /*
  253. * When a retprobed function returns, trampoline_handler() is called,
  254. * calling the kretprobe's handler. We construct a struct pt_regs to
  255. * give a view of registers r0-r11 to the user return-handler. This is
  256. * not a complete pt_regs structure, but that should be plenty sufficient
  257. * for kretprobe handlers which should normally be interested in r0 only
  258. * anyway.
  259. */
  260. void __naked __kprobes kretprobe_trampoline(void)
  261. {
  262. __asm__ __volatile__ (
  263. "stmdb sp!, {r0 - r11} \n\t"
  264. "mov r0, sp \n\t"
  265. "bl trampoline_handler \n\t"
  266. "mov lr, r0 \n\t"
  267. "ldmia sp!, {r0 - r11} \n\t"
  268. "mov pc, lr \n\t"
  269. : : : "memory");
  270. }
  271. /* Called from kretprobe_trampoline */
  272. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  273. {
  274. struct kretprobe_instance *ri = NULL;
  275. struct hlist_head *head, empty_rp;
  276. struct hlist_node *node, *tmp;
  277. unsigned long flags, orig_ret_address = 0;
  278. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  279. INIT_HLIST_HEAD(&empty_rp);
  280. kretprobe_hash_lock(current, &head, &flags);
  281. /*
  282. * It is possible to have multiple instances associated with a given
  283. * task either because multiple functions in the call path have
  284. * a return probe installed on them, and/or more than one return
  285. * probe was registered for a target function.
  286. *
  287. * We can handle this because:
  288. * - instances are always inserted at the head of the list
  289. * - when multiple return probes are registered for the same
  290. * function, the first instance's ret_addr will point to the
  291. * real return address, and all the rest will point to
  292. * kretprobe_trampoline
  293. */
  294. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  295. if (ri->task != current)
  296. /* another task is sharing our hash bucket */
  297. continue;
  298. if (ri->rp && ri->rp->handler) {
  299. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  300. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  301. ri->rp->handler(ri, regs);
  302. __get_cpu_var(current_kprobe) = NULL;
  303. }
  304. orig_ret_address = (unsigned long)ri->ret_addr;
  305. recycle_rp_inst(ri, &empty_rp);
  306. if (orig_ret_address != trampoline_address)
  307. /*
  308. * This is the real return address. Any other
  309. * instances associated with this task are for
  310. * other calls deeper on the call stack
  311. */
  312. break;
  313. }
  314. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  315. kretprobe_hash_unlock(current, &flags);
  316. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  317. hlist_del(&ri->hlist);
  318. kfree(ri);
  319. }
  320. return (void *)orig_ret_address;
  321. }
  322. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  323. struct pt_regs *regs)
  324. {
  325. ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
  326. /* Replace the return addr with trampoline addr. */
  327. regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
  328. }
  329. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  330. {
  331. struct jprobe *jp = container_of(p, struct jprobe, kp);
  332. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  333. long sp_addr = regs->ARM_sp;
  334. kcb->jprobe_saved_regs = *regs;
  335. memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
  336. regs->ARM_pc = (long)jp->entry;
  337. regs->ARM_cpsr |= PSR_I_BIT;
  338. preempt_disable();
  339. return 1;
  340. }
  341. void __kprobes jprobe_return(void)
  342. {
  343. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  344. __asm__ __volatile__ (
  345. /*
  346. * Setup an empty pt_regs. Fill SP and PC fields as
  347. * they're needed by longjmp_break_handler.
  348. *
  349. * We allocate some slack between the original SP and start of
  350. * our fabricated regs. To be precise we want to have worst case
  351. * covered which is STMFD with all 16 regs so we allocate 2 *
  352. * sizeof(struct_pt_regs)).
  353. *
  354. * This is to prevent any simulated instruction from writing
  355. * over the regs when they are accessing the stack.
  356. */
  357. "sub sp, %0, %1 \n\t"
  358. "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
  359. "str %0, [sp, %2] \n\t"
  360. "str r0, [sp, %3] \n\t"
  361. "mov r0, sp \n\t"
  362. "bl kprobe_handler \n\t"
  363. /*
  364. * Return to the context saved by setjmp_pre_handler
  365. * and restored by longjmp_break_handler.
  366. */
  367. "ldr r0, [sp, %4] \n\t"
  368. "msr cpsr_cxsf, r0 \n\t"
  369. "ldmia sp, {r0 - pc} \n\t"
  370. :
  371. : "r" (kcb->jprobe_saved_regs.ARM_sp),
  372. "I" (sizeof(struct pt_regs) * 2),
  373. "J" (offsetof(struct pt_regs, ARM_sp)),
  374. "J" (offsetof(struct pt_regs, ARM_pc)),
  375. "J" (offsetof(struct pt_regs, ARM_cpsr))
  376. : "memory", "cc");
  377. }
  378. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  379. {
  380. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  381. long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
  382. long orig_sp = regs->ARM_sp;
  383. struct jprobe *jp = container_of(p, struct jprobe, kp);
  384. if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
  385. if (orig_sp != stack_addr) {
  386. struct pt_regs *saved_regs =
  387. (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
  388. printk("current sp %lx does not match saved sp %lx\n",
  389. orig_sp, stack_addr);
  390. printk("Saved registers for jprobe %p\n", jp);
  391. show_regs(saved_regs);
  392. printk("Current registers\n");
  393. show_regs(regs);
  394. BUG();
  395. }
  396. *regs = kcb->jprobe_saved_regs;
  397. memcpy((void *)stack_addr, kcb->jprobes_stack,
  398. MIN_STACK_SIZE(stack_addr));
  399. preempt_enable_no_resched();
  400. return 1;
  401. }
  402. return 0;
  403. }
  404. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  405. {
  406. return 0;
  407. }
  408. static struct undef_hook kprobes_break_hook = {
  409. .instr_mask = 0xffffffff,
  410. .instr_val = KPROBE_BREAKPOINT_INSTRUCTION,
  411. .cpsr_mask = MODE_MASK,
  412. .cpsr_val = SVC_MODE,
  413. .fn = kprobe_trap_handler,
  414. };
  415. int __init arch_init_kprobes()
  416. {
  417. arm_kprobe_decode_init();
  418. register_undef_hook(&kprobes_break_hook);
  419. return 0;
  420. }