kprobes.c 13 KB

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