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 "
  83. "supported\n");
  84. return -EINVAL;
  85. }
  86. /* insn: must be on special executable page on tile. */
  87. p->ainsn.insn = get_insn_slot();
  88. if (!p->ainsn.insn)
  89. return -ENOMEM;
  90. /*
  91. * In the kprobe->ainsn.insn[] array we store the original
  92. * instruction at index zero and a break trap instruction at
  93. * index one.
  94. */
  95. memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
  96. p->ainsn.insn[1] = breakpoint2_insn;
  97. p->opcode = *p->addr;
  98. return 0;
  99. }
  100. void __kprobes arch_arm_kprobe(struct kprobe *p)
  101. {
  102. unsigned long addr_wr;
  103. /* Operate on writable kernel text mapping. */
  104. addr_wr = (unsigned long)p->addr - MEM_SV_START + PAGE_OFFSET;
  105. if (probe_kernel_write((void *)addr_wr, &breakpoint_insn,
  106. sizeof(breakpoint_insn)))
  107. pr_err("%s: failed to enable kprobe\n", __func__);
  108. smp_wmb();
  109. flush_insn_slot(p);
  110. }
  111. void __kprobes arch_disarm_kprobe(struct kprobe *kp)
  112. {
  113. unsigned long addr_wr;
  114. /* Operate on writable kernel text mapping. */
  115. addr_wr = (unsigned long)kp->addr - MEM_SV_START + PAGE_OFFSET;
  116. if (probe_kernel_write((void *)addr_wr, &kp->opcode,
  117. sizeof(kp->opcode)))
  118. pr_err("%s: failed to enable kprobe\n", __func__);
  119. smp_wmb();
  120. flush_insn_slot(kp);
  121. }
  122. void __kprobes arch_remove_kprobe(struct kprobe *p)
  123. {
  124. if (p->ainsn.insn) {
  125. free_insn_slot(p->ainsn.insn, 0);
  126. p->ainsn.insn = NULL;
  127. }
  128. }
  129. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  130. {
  131. kcb->prev_kprobe.kp = kprobe_running();
  132. kcb->prev_kprobe.status = kcb->kprobe_status;
  133. kcb->prev_kprobe.saved_pc = kcb->kprobe_saved_pc;
  134. }
  135. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  136. {
  137. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  138. kcb->kprobe_status = kcb->prev_kprobe.status;
  139. kcb->kprobe_saved_pc = kcb->prev_kprobe.saved_pc;
  140. }
  141. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  142. struct kprobe_ctlblk *kcb)
  143. {
  144. __this_cpu_write(current_kprobe, p);
  145. kcb->kprobe_saved_pc = regs->pc;
  146. }
  147. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  148. {
  149. /* Single step inline if the instruction is a break. */
  150. if (p->opcode == breakpoint_insn ||
  151. p->opcode == breakpoint2_insn)
  152. regs->pc = (unsigned long)p->addr;
  153. else
  154. regs->pc = (unsigned long)&p->ainsn.insn[0];
  155. }
  156. static int __kprobes kprobe_handler(struct pt_regs *regs)
  157. {
  158. struct kprobe *p;
  159. int ret = 0;
  160. kprobe_opcode_t *addr;
  161. struct kprobe_ctlblk *kcb;
  162. addr = (kprobe_opcode_t *)regs->pc;
  163. /*
  164. * We don't want to be preempted for the entire
  165. * duration of kprobe processing.
  166. */
  167. preempt_disable();
  168. kcb = get_kprobe_ctlblk();
  169. /* Check we're not actually recursing. */
  170. if (kprobe_running()) {
  171. p = get_kprobe(addr);
  172. if (p) {
  173. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  174. p->ainsn.insn[0] == breakpoint_insn) {
  175. goto no_kprobe;
  176. }
  177. /*
  178. * We have reentered the kprobe_handler(), since
  179. * another probe was hit while within the handler.
  180. * We here save the original kprobes variables and
  181. * just single step on the instruction of the new probe
  182. * without calling any user handlers.
  183. */
  184. save_previous_kprobe(kcb);
  185. set_current_kprobe(p, regs, kcb);
  186. kprobes_inc_nmissed_count(p);
  187. prepare_singlestep(p, regs);
  188. kcb->kprobe_status = KPROBE_REENTER;
  189. return 1;
  190. } else {
  191. if (*addr != breakpoint_insn) {
  192. /*
  193. * The breakpoint instruction was removed by
  194. * another cpu right after we hit, no further
  195. * handling of this interrupt is appropriate.
  196. */
  197. ret = 1;
  198. goto no_kprobe;
  199. }
  200. p = __this_cpu_read(current_kprobe);
  201. if (p->break_handler && p->break_handler(p, regs))
  202. goto ss_probe;
  203. }
  204. goto no_kprobe;
  205. }
  206. p = get_kprobe(addr);
  207. if (!p) {
  208. if (*addr != breakpoint_insn) {
  209. /*
  210. * The breakpoint instruction was removed right
  211. * after we hit it. Another cpu has removed
  212. * either a probepoint or a debugger breakpoint
  213. * at this address. In either case, no further
  214. * handling of this interrupt is appropriate.
  215. */
  216. ret = 1;
  217. }
  218. /* Not one of ours: let kernel handle it. */
  219. goto no_kprobe;
  220. }
  221. set_current_kprobe(p, regs, kcb);
  222. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  223. if (p->pre_handler && p->pre_handler(p, regs)) {
  224. /* Handler has already set things up, so skip ss setup. */
  225. return 1;
  226. }
  227. ss_probe:
  228. prepare_singlestep(p, regs);
  229. kcb->kprobe_status = KPROBE_HIT_SS;
  230. return 1;
  231. no_kprobe:
  232. preempt_enable_no_resched();
  233. return ret;
  234. }
  235. /*
  236. * Called after single-stepping. p->addr is the address of the
  237. * instruction that has been replaced by the breakpoint. To avoid the
  238. * SMP problems that can occur when we temporarily put back the
  239. * original opcode to single-step, we single-stepped a copy of the
  240. * instruction. The address of this copy is p->ainsn.insn.
  241. *
  242. * This function prepares to return from the post-single-step
  243. * breakpoint trap.
  244. */
  245. static void __kprobes resume_execution(struct kprobe *p,
  246. struct pt_regs *regs,
  247. struct kprobe_ctlblk *kcb)
  248. {
  249. unsigned long orig_pc = kcb->kprobe_saved_pc;
  250. regs->pc = orig_pc + 8;
  251. }
  252. static inline int post_kprobe_handler(struct pt_regs *regs)
  253. {
  254. struct kprobe *cur = kprobe_running();
  255. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  256. if (!cur)
  257. return 0;
  258. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  259. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  260. cur->post_handler(cur, regs, 0);
  261. }
  262. resume_execution(cur, regs, kcb);
  263. /* Restore back the original saved kprobes variables and continue. */
  264. if (kcb->kprobe_status == KPROBE_REENTER) {
  265. restore_previous_kprobe(kcb);
  266. goto out;
  267. }
  268. reset_current_kprobe();
  269. out:
  270. preempt_enable_no_resched();
  271. return 1;
  272. }
  273. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  274. {
  275. struct kprobe *cur = kprobe_running();
  276. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  277. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  278. return 1;
  279. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  280. /*
  281. * We are here because the instruction being single
  282. * stepped caused a page fault. We reset the current
  283. * kprobe and the ip points back to the probe address
  284. * and allow the page fault handler to continue as a
  285. * normal page fault.
  286. */
  287. resume_execution(cur, regs, kcb);
  288. reset_current_kprobe();
  289. preempt_enable_no_resched();
  290. }
  291. return 0;
  292. }
  293. /*
  294. * Wrapper routine for handling exceptions.
  295. */
  296. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  297. unsigned long val, void *data)
  298. {
  299. struct die_args *args = (struct die_args *)data;
  300. int ret = NOTIFY_DONE;
  301. switch (val) {
  302. case DIE_BREAK:
  303. if (kprobe_handler(args->regs))
  304. ret = NOTIFY_STOP;
  305. break;
  306. case DIE_SSTEPBP:
  307. if (post_kprobe_handler(args->regs))
  308. ret = NOTIFY_STOP;
  309. break;
  310. case DIE_PAGE_FAULT:
  311. /* kprobe_running() needs smp_processor_id(). */
  312. preempt_disable();
  313. if (kprobe_running()
  314. && kprobe_fault_handler(args->regs, args->trapnr))
  315. ret = NOTIFY_STOP;
  316. preempt_enable();
  317. break;
  318. default:
  319. break;
  320. }
  321. return ret;
  322. }
  323. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  324. {
  325. struct jprobe *jp = container_of(p, struct jprobe, kp);
  326. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  327. kcb->jprobe_saved_regs = *regs;
  328. kcb->jprobe_saved_sp = regs->sp;
  329. memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
  330. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  331. regs->pc = (unsigned long)(jp->entry);
  332. return 1;
  333. }
  334. /* Defined in the inline asm below. */
  335. void jprobe_return_end(void);
  336. void __kprobes jprobe_return(void)
  337. {
  338. asm volatile(
  339. "bpt\n\t"
  340. ".globl jprobe_return_end\n"
  341. "jprobe_return_end:\n");
  342. }
  343. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  344. {
  345. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  346. if (regs->pc >= (unsigned long)jprobe_return &&
  347. regs->pc <= (unsigned long)jprobe_return_end) {
  348. *regs = kcb->jprobe_saved_regs;
  349. memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
  350. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  351. preempt_enable_no_resched();
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Function return probe trampoline:
  358. * - init_kprobes() establishes a probepoint here
  359. * - When the probed function returns, this probe causes the
  360. * handlers to fire
  361. */
  362. static void __used kretprobe_trampoline_holder(void)
  363. {
  364. asm volatile(
  365. "nop\n\t"
  366. ".global kretprobe_trampoline\n"
  367. "kretprobe_trampoline:\n\t"
  368. "nop\n\t"
  369. : : : "memory");
  370. }
  371. void kretprobe_trampoline(void);
  372. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  373. struct pt_regs *regs)
  374. {
  375. ri->ret_addr = (kprobe_opcode_t *) regs->lr;
  376. /* Replace the return addr with trampoline addr */
  377. regs->lr = (unsigned long)kretprobe_trampoline;
  378. }
  379. /*
  380. * Called when the probe at kretprobe trampoline is hit.
  381. */
  382. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  383. struct pt_regs *regs)
  384. {
  385. struct kretprobe_instance *ri = NULL;
  386. struct hlist_head *head, empty_rp;
  387. struct hlist_node *tmp;
  388. unsigned long flags, orig_ret_address = 0;
  389. unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
  390. INIT_HLIST_HEAD(&empty_rp);
  391. kretprobe_hash_lock(current, &head, &flags);
  392. /*
  393. * It is possible to have multiple instances associated with a given
  394. * task either because multiple functions in the call path have
  395. * a return probe installed on them, and/or more than one return
  396. * return probe was registered for a target function.
  397. *
  398. * We can handle this because:
  399. * - instances are always inserted at the head of the list
  400. * - when multiple return probes are registered for the same
  401. * function, the first instance's ret_addr will point to the
  402. * real return address, and all the rest will point to
  403. * kretprobe_trampoline
  404. */
  405. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  406. if (ri->task != current)
  407. /* another task is sharing our hash bucket */
  408. continue;
  409. if (ri->rp && ri->rp->handler)
  410. ri->rp->handler(ri, regs);
  411. orig_ret_address = (unsigned long)ri->ret_addr;
  412. recycle_rp_inst(ri, &empty_rp);
  413. if (orig_ret_address != trampoline_address) {
  414. /*
  415. * This is the real return address. Any other
  416. * instances associated with this task are for
  417. * other calls deeper on the call stack
  418. */
  419. break;
  420. }
  421. }
  422. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  423. instruction_pointer(regs) = orig_ret_address;
  424. reset_current_kprobe();
  425. kretprobe_hash_unlock(current, &flags);
  426. preempt_enable_no_resched();
  427. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  428. hlist_del(&ri->hlist);
  429. kfree(ri);
  430. }
  431. /*
  432. * By returning a non-zero value, we are telling
  433. * kprobe_handler() that we don't want the post_handler
  434. * to run (and have re-enabled preemption)
  435. */
  436. return 1;
  437. }
  438. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  439. {
  440. if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
  441. return 1;
  442. return 0;
  443. }
  444. static struct kprobe trampoline_p = {
  445. .addr = (kprobe_opcode_t *)kretprobe_trampoline,
  446. .pre_handler = trampoline_probe_handler
  447. };
  448. int __init arch_init_kprobes(void)
  449. {
  450. register_kprobe(&trampoline_p);
  451. return 0;
  452. }