kprobes.c 12 KB

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