kprobes.c 12 KB

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