kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * arch/tile/kernel/kprobes.c
  3. * Kprobes on TILE-Gx
  4. *
  5. * Some portions copied from the MIPS version.
  6. *
  7. * Copyright (C) IBM Corporation, 2002, 2004
  8. * Copyright 2006 Sony Corp.
  9. * Copyright 2010 Cavium Networks
  10. *
  11. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation, version 2.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  20. * NON INFRINGEMENT. See the GNU General Public License for
  21. * more details.
  22. */
  23. #include <linux/kprobes.h>
  24. #include <linux/kdebug.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/cacheflush.h>
  29. #include <arch/opcode.h>
  30. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  31. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  32. tile_bundle_bits breakpoint_insn = TILEGX_BPT_BUNDLE;
  33. tile_bundle_bits breakpoint2_insn = TILEGX_BPT_BUNDLE | DIE_SSTEPBP;
  34. /*
  35. * Check whether instruction is branch or jump, or if executing it
  36. * has different results depending on where it is executed (e.g. lnk).
  37. */
  38. static int __kprobes insn_has_control(kprobe_opcode_t insn)
  39. {
  40. if (get_Mode(insn) != 0) { /* Y-format bundle */
  41. if (get_Opcode_Y1(insn) != RRR_1_OPCODE_Y1 ||
  42. get_RRROpcodeExtension_Y1(insn) != UNARY_RRR_1_OPCODE_Y1)
  43. return 0;
  44. switch (get_UnaryOpcodeExtension_Y1(insn)) {
  45. case JALRP_UNARY_OPCODE_Y1:
  46. case JALR_UNARY_OPCODE_Y1:
  47. case JRP_UNARY_OPCODE_Y1:
  48. case JR_UNARY_OPCODE_Y1:
  49. case LNK_UNARY_OPCODE_Y1:
  50. return 1;
  51. default:
  52. return 0;
  53. }
  54. }
  55. switch (get_Opcode_X1(insn)) {
  56. case BRANCH_OPCODE_X1: /* branch instructions */
  57. case JUMP_OPCODE_X1: /* jump instructions: j and jal */
  58. return 1;
  59. case RRR_0_OPCODE_X1: /* other jump instructions */
  60. if (get_RRROpcodeExtension_X1(insn) != UNARY_RRR_0_OPCODE_X1)
  61. return 0;
  62. switch (get_UnaryOpcodeExtension_X1(insn)) {
  63. case JALRP_UNARY_OPCODE_X1:
  64. case JALR_UNARY_OPCODE_X1:
  65. case JRP_UNARY_OPCODE_X1:
  66. case JR_UNARY_OPCODE_X1:
  67. case LNK_UNARY_OPCODE_X1:
  68. return 1;
  69. default:
  70. return 0;
  71. }
  72. default:
  73. return 0;
  74. }
  75. }
  76. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  77. {
  78. unsigned long addr = (unsigned long)p->addr;
  79. if (addr & (sizeof(kprobe_opcode_t) - 1))
  80. return -EINVAL;
  81. if (insn_has_control(*p->addr)) {
  82. pr_notice("Kprobes for control instructions are not supported\n");
  83. return -EINVAL;
  84. }
  85. /* insn: must be on special executable page on tile. */
  86. p->ainsn.insn = get_insn_slot();
  87. if (!p->ainsn.insn)
  88. return -ENOMEM;
  89. /*
  90. * In the kprobe->ainsn.insn[] array we store the original
  91. * instruction at index zero and a break trap instruction at
  92. * index one.
  93. */
  94. memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
  95. p->ainsn.insn[1] = breakpoint2_insn;
  96. p->opcode = *p->addr;
  97. return 0;
  98. }
  99. void __kprobes arch_arm_kprobe(struct kprobe *p)
  100. {
  101. unsigned long addr_wr;
  102. /* Operate on writable kernel text mapping. */
  103. addr_wr = ktext_writable_addr(p->addr);
  104. if (probe_kernel_write((void *)addr_wr, &breakpoint_insn,
  105. sizeof(breakpoint_insn)))
  106. pr_err("%s: failed to enable kprobe\n", __func__);
  107. smp_wmb();
  108. flush_insn_slot(p);
  109. }
  110. void __kprobes arch_disarm_kprobe(struct kprobe *kp)
  111. {
  112. unsigned long addr_wr;
  113. /* Operate on writable kernel text mapping. */
  114. addr_wr = ktext_writable_addr(kp->addr);
  115. if (probe_kernel_write((void *)addr_wr, &kp->opcode,
  116. sizeof(kp->opcode)))
  117. pr_err("%s: failed to enable kprobe\n", __func__);
  118. smp_wmb();
  119. flush_insn_slot(kp);
  120. }
  121. void __kprobes arch_remove_kprobe(struct kprobe *p)
  122. {
  123. if (p->ainsn.insn) {
  124. free_insn_slot(p->ainsn.insn, 0);
  125. p->ainsn.insn = NULL;
  126. }
  127. }
  128. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  129. {
  130. kcb->prev_kprobe.kp = kprobe_running();
  131. kcb->prev_kprobe.status = kcb->kprobe_status;
  132. kcb->prev_kprobe.saved_pc = kcb->kprobe_saved_pc;
  133. }
  134. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  135. {
  136. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  137. kcb->kprobe_status = kcb->prev_kprobe.status;
  138. kcb->kprobe_saved_pc = kcb->prev_kprobe.saved_pc;
  139. }
  140. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  141. struct kprobe_ctlblk *kcb)
  142. {
  143. __this_cpu_write(current_kprobe, p);
  144. kcb->kprobe_saved_pc = regs->pc;
  145. }
  146. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  147. {
  148. /* Single step inline if the instruction is a break. */
  149. if (p->opcode == breakpoint_insn ||
  150. p->opcode == breakpoint2_insn)
  151. regs->pc = (unsigned long)p->addr;
  152. else
  153. regs->pc = (unsigned long)&p->ainsn.insn[0];
  154. }
  155. static int __kprobes kprobe_handler(struct pt_regs *regs)
  156. {
  157. struct kprobe *p;
  158. int ret = 0;
  159. kprobe_opcode_t *addr;
  160. struct kprobe_ctlblk *kcb;
  161. addr = (kprobe_opcode_t *)regs->pc;
  162. /*
  163. * We don't want to be preempted for the entire
  164. * duration of kprobe processing.
  165. */
  166. preempt_disable();
  167. kcb = get_kprobe_ctlblk();
  168. /* Check we're not actually recursing. */
  169. if (kprobe_running()) {
  170. p = get_kprobe(addr);
  171. if (p) {
  172. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  173. p->ainsn.insn[0] == breakpoint_insn) {
  174. goto no_kprobe;
  175. }
  176. /*
  177. * We have reentered the kprobe_handler(), since
  178. * another probe was hit while within the handler.
  179. * We here save the original kprobes variables and
  180. * just single step on the instruction of the new probe
  181. * without calling any user handlers.
  182. */
  183. save_previous_kprobe(kcb);
  184. set_current_kprobe(p, regs, kcb);
  185. kprobes_inc_nmissed_count(p);
  186. prepare_singlestep(p, regs);
  187. kcb->kprobe_status = KPROBE_REENTER;
  188. return 1;
  189. } else {
  190. if (*addr != breakpoint_insn) {
  191. /*
  192. * The breakpoint instruction was removed by
  193. * another cpu right after we hit, no further
  194. * handling of this interrupt is appropriate.
  195. */
  196. ret = 1;
  197. goto no_kprobe;
  198. }
  199. p = __this_cpu_read(current_kprobe);
  200. if (p->break_handler && p->break_handler(p, regs))
  201. goto ss_probe;
  202. }
  203. goto no_kprobe;
  204. }
  205. p = get_kprobe(addr);
  206. if (!p) {
  207. if (*addr != breakpoint_insn) {
  208. /*
  209. * The breakpoint instruction was removed right
  210. * after we hit it. Another cpu has removed
  211. * either a probepoint or a debugger breakpoint
  212. * at this address. In either case, no further
  213. * handling of this interrupt is appropriate.
  214. */
  215. ret = 1;
  216. }
  217. /* Not one of ours: let kernel handle it. */
  218. goto no_kprobe;
  219. }
  220. set_current_kprobe(p, regs, kcb);
  221. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  222. if (p->pre_handler && p->pre_handler(p, regs)) {
  223. /* Handler has already set things up, so skip ss setup. */
  224. return 1;
  225. }
  226. ss_probe:
  227. prepare_singlestep(p, regs);
  228. kcb->kprobe_status = KPROBE_HIT_SS;
  229. return 1;
  230. no_kprobe:
  231. preempt_enable_no_resched();
  232. return ret;
  233. }
  234. /*
  235. * Called after single-stepping. p->addr is the address of the
  236. * instruction that has been replaced by the breakpoint. To avoid the
  237. * SMP problems that can occur when we temporarily put back the
  238. * original opcode to single-step, we single-stepped a copy of the
  239. * instruction. The address of this copy is p->ainsn.insn.
  240. *
  241. * This function prepares to return from the post-single-step
  242. * breakpoint trap.
  243. */
  244. static void __kprobes resume_execution(struct kprobe *p,
  245. struct pt_regs *regs,
  246. struct kprobe_ctlblk *kcb)
  247. {
  248. unsigned long orig_pc = kcb->kprobe_saved_pc;
  249. regs->pc = orig_pc + 8;
  250. }
  251. static inline int post_kprobe_handler(struct pt_regs *regs)
  252. {
  253. struct kprobe *cur = kprobe_running();
  254. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  255. if (!cur)
  256. return 0;
  257. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  258. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  259. cur->post_handler(cur, regs, 0);
  260. }
  261. resume_execution(cur, regs, kcb);
  262. /* Restore back the original saved kprobes variables and continue. */
  263. if (kcb->kprobe_status == KPROBE_REENTER) {
  264. restore_previous_kprobe(kcb);
  265. goto out;
  266. }
  267. reset_current_kprobe();
  268. out:
  269. preempt_enable_no_resched();
  270. return 1;
  271. }
  272. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  273. {
  274. struct kprobe *cur = kprobe_running();
  275. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  276. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  277. return 1;
  278. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  279. /*
  280. * We are here because the instruction being single
  281. * stepped caused a page fault. We reset the current
  282. * kprobe and the ip points back to the probe address
  283. * and allow the page fault handler to continue as a
  284. * normal page fault.
  285. */
  286. resume_execution(cur, regs, kcb);
  287. reset_current_kprobe();
  288. preempt_enable_no_resched();
  289. }
  290. return 0;
  291. }
  292. /*
  293. * Wrapper routine for handling exceptions.
  294. */
  295. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  296. unsigned long val, void *data)
  297. {
  298. struct die_args *args = (struct die_args *)data;
  299. int ret = NOTIFY_DONE;
  300. switch (val) {
  301. case DIE_BREAK:
  302. if (kprobe_handler(args->regs))
  303. ret = NOTIFY_STOP;
  304. break;
  305. case DIE_SSTEPBP:
  306. if (post_kprobe_handler(args->regs))
  307. ret = NOTIFY_STOP;
  308. break;
  309. case DIE_PAGE_FAULT:
  310. /* kprobe_running() needs smp_processor_id(). */
  311. preempt_disable();
  312. if (kprobe_running()
  313. && kprobe_fault_handler(args->regs, args->trapnr))
  314. ret = NOTIFY_STOP;
  315. preempt_enable();
  316. break;
  317. default:
  318. break;
  319. }
  320. return ret;
  321. }
  322. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  323. {
  324. struct jprobe *jp = container_of(p, struct jprobe, kp);
  325. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  326. kcb->jprobe_saved_regs = *regs;
  327. kcb->jprobe_saved_sp = regs->sp;
  328. memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
  329. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  330. regs->pc = (unsigned long)(jp->entry);
  331. return 1;
  332. }
  333. /* Defined in the inline asm below. */
  334. void jprobe_return_end(void);
  335. void __kprobes jprobe_return(void)
  336. {
  337. asm volatile(
  338. "bpt\n\t"
  339. ".globl jprobe_return_end\n"
  340. "jprobe_return_end:\n");
  341. }
  342. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  343. {
  344. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  345. if (regs->pc >= (unsigned long)jprobe_return &&
  346. regs->pc <= (unsigned long)jprobe_return_end) {
  347. *regs = kcb->jprobe_saved_regs;
  348. memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
  349. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  350. preempt_enable_no_resched();
  351. return 1;
  352. }
  353. return 0;
  354. }
  355. /*
  356. * Function return probe trampoline:
  357. * - init_kprobes() establishes a probepoint here
  358. * - When the probed function returns, this probe causes the
  359. * handlers to fire
  360. */
  361. static void __used kretprobe_trampoline_holder(void)
  362. {
  363. asm volatile(
  364. "nop\n\t"
  365. ".global kretprobe_trampoline\n"
  366. "kretprobe_trampoline:\n\t"
  367. "nop\n\t"
  368. : : : "memory");
  369. }
  370. void kretprobe_trampoline(void);
  371. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  372. struct pt_regs *regs)
  373. {
  374. ri->ret_addr = (kprobe_opcode_t *) regs->lr;
  375. /* Replace the return addr with trampoline addr */
  376. regs->lr = (unsigned long)kretprobe_trampoline;
  377. }
  378. /*
  379. * Called when the probe at kretprobe trampoline is hit.
  380. */
  381. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  382. struct pt_regs *regs)
  383. {
  384. struct kretprobe_instance *ri = NULL;
  385. struct hlist_head *head, empty_rp;
  386. struct hlist_node *tmp;
  387. unsigned long flags, orig_ret_address = 0;
  388. unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
  389. INIT_HLIST_HEAD(&empty_rp);
  390. kretprobe_hash_lock(current, &head, &flags);
  391. /*
  392. * It is possible to have multiple instances associated with a given
  393. * task either because multiple functions in the call path have
  394. * a return probe installed on them, and/or more than one return
  395. * return probe was registered for a target function.
  396. *
  397. * We can handle this because:
  398. * - instances are always inserted at the head of the list
  399. * - when multiple return probes are registered for the same
  400. * function, the first instance's ret_addr will point to the
  401. * real return address, and all the rest will point to
  402. * kretprobe_trampoline
  403. */
  404. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  405. if (ri->task != current)
  406. /* another task is sharing our hash bucket */
  407. continue;
  408. if (ri->rp && ri->rp->handler)
  409. ri->rp->handler(ri, regs);
  410. orig_ret_address = (unsigned long)ri->ret_addr;
  411. recycle_rp_inst(ri, &empty_rp);
  412. if (orig_ret_address != trampoline_address) {
  413. /*
  414. * This is the real return address. Any other
  415. * instances associated with this task are for
  416. * other calls deeper on the call stack
  417. */
  418. break;
  419. }
  420. }
  421. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  422. instruction_pointer(regs) = orig_ret_address;
  423. reset_current_kprobe();
  424. kretprobe_hash_unlock(current, &flags);
  425. preempt_enable_no_resched();
  426. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  427. hlist_del(&ri->hlist);
  428. kfree(ri);
  429. }
  430. /*
  431. * By returning a non-zero value, we are telling
  432. * kprobe_handler() that we don't want the post_handler
  433. * to run (and have re-enabled preemption)
  434. */
  435. return 1;
  436. }
  437. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  438. {
  439. if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
  440. return 1;
  441. return 0;
  442. }
  443. static struct kprobe trampoline_p = {
  444. .addr = (kprobe_opcode_t *)kretprobe_trampoline,
  445. .pre_handler = trampoline_probe_handler
  446. };
  447. int __init arch_init_kprobes(void)
  448. {
  449. register_kprobe(&trampoline_p);
  450. return 0;
  451. }