kprobes.c 14 KB

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