kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Kernel Probes (KProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
  26. * for PPC64
  27. */
  28. #include <linux/kprobes.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/preempt.h>
  31. #include <linux/extable.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/slab.h>
  34. #include <asm/code-patching.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/sstep.h>
  37. #include <linux/uaccess.h>
  38. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  39. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  40. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  41. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  42. {
  43. int ret = 0;
  44. kprobe_opcode_t insn = *p->addr;
  45. if ((unsigned long)p->addr & 0x03) {
  46. printk("Attempt to register kprobe at an unaligned address\n");
  47. ret = -EINVAL;
  48. } else if (IS_MTMSRD(insn) || IS_RFID(insn) || IS_RFI(insn)) {
  49. printk("Cannot register a kprobe on rfi/rfid or mtmsr[d]\n");
  50. ret = -EINVAL;
  51. }
  52. /* insn must be on a special executable page on ppc64. This is
  53. * not explicitly required on ppc32 (right now), but it doesn't hurt */
  54. if (!ret) {
  55. p->ainsn.insn = get_insn_slot();
  56. if (!p->ainsn.insn)
  57. ret = -ENOMEM;
  58. }
  59. if (!ret) {
  60. memcpy(p->ainsn.insn, p->addr,
  61. MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  62. p->opcode = *p->addr;
  63. flush_icache_range((unsigned long)p->ainsn.insn,
  64. (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
  65. }
  66. p->ainsn.boostable = 0;
  67. return ret;
  68. }
  69. void __kprobes arch_arm_kprobe(struct kprobe *p)
  70. {
  71. *p->addr = BREAKPOINT_INSTRUCTION;
  72. flush_icache_range((unsigned long) p->addr,
  73. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  74. }
  75. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  76. {
  77. *p->addr = p->opcode;
  78. flush_icache_range((unsigned long) p->addr,
  79. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  80. }
  81. void __kprobes arch_remove_kprobe(struct kprobe *p)
  82. {
  83. if (p->ainsn.insn) {
  84. free_insn_slot(p->ainsn.insn, 0);
  85. p->ainsn.insn = NULL;
  86. }
  87. }
  88. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  89. {
  90. enable_single_step(regs);
  91. /*
  92. * On powerpc we should single step on the original
  93. * instruction even if the probed insn is a trap
  94. * variant as values in regs could play a part in
  95. * if the trap is taken or not
  96. */
  97. regs->nip = (unsigned long)p->ainsn.insn;
  98. }
  99. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  100. {
  101. kcb->prev_kprobe.kp = kprobe_running();
  102. kcb->prev_kprobe.status = kcb->kprobe_status;
  103. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  104. }
  105. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  106. {
  107. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  108. kcb->kprobe_status = kcb->prev_kprobe.status;
  109. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  110. }
  111. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  112. struct kprobe_ctlblk *kcb)
  113. {
  114. __this_cpu_write(current_kprobe, p);
  115. kcb->kprobe_saved_msr = regs->msr;
  116. }
  117. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  118. struct pt_regs *regs)
  119. {
  120. ri->ret_addr = (kprobe_opcode_t *)regs->link;
  121. /* Replace the return addr with trampoline addr */
  122. regs->link = (unsigned long)kretprobe_trampoline;
  123. }
  124. int __kprobes kprobe_handler(struct pt_regs *regs)
  125. {
  126. struct kprobe *p;
  127. int ret = 0;
  128. unsigned int *addr = (unsigned int *)regs->nip;
  129. struct kprobe_ctlblk *kcb;
  130. if (user_mode(regs))
  131. return 0;
  132. /*
  133. * We don't want to be preempted for the entire
  134. * duration of kprobe processing
  135. */
  136. preempt_disable();
  137. kcb = get_kprobe_ctlblk();
  138. /* Check we're not actually recursing */
  139. if (kprobe_running()) {
  140. p = get_kprobe(addr);
  141. if (p) {
  142. kprobe_opcode_t insn = *p->ainsn.insn;
  143. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  144. is_trap(insn)) {
  145. /* Turn off 'trace' bits */
  146. regs->msr &= ~MSR_SINGLESTEP;
  147. regs->msr |= kcb->kprobe_saved_msr;
  148. goto no_kprobe;
  149. }
  150. /* We have reentered the kprobe_handler(), since
  151. * another probe was hit while within the handler.
  152. * We here save the original kprobes variables and
  153. * just single step on the instruction of the new probe
  154. * without calling any user handlers.
  155. */
  156. save_previous_kprobe(kcb);
  157. set_current_kprobe(p, regs, kcb);
  158. kcb->kprobe_saved_msr = regs->msr;
  159. kprobes_inc_nmissed_count(p);
  160. prepare_singlestep(p, regs);
  161. kcb->kprobe_status = KPROBE_REENTER;
  162. return 1;
  163. } else {
  164. if (*addr != BREAKPOINT_INSTRUCTION) {
  165. /* If trap variant, then it belongs not to us */
  166. kprobe_opcode_t cur_insn = *addr;
  167. if (is_trap(cur_insn))
  168. goto no_kprobe;
  169. /* The breakpoint instruction was removed by
  170. * another cpu right after we hit, no further
  171. * handling of this interrupt is appropriate
  172. */
  173. ret = 1;
  174. goto no_kprobe;
  175. }
  176. p = __this_cpu_read(current_kprobe);
  177. if (p->break_handler && p->break_handler(p, regs)) {
  178. goto ss_probe;
  179. }
  180. }
  181. goto no_kprobe;
  182. }
  183. p = get_kprobe(addr);
  184. if (!p) {
  185. if (*addr != BREAKPOINT_INSTRUCTION) {
  186. /*
  187. * PowerPC has multiple variants of the "trap"
  188. * instruction. If the current instruction is a
  189. * trap variant, it could belong to someone else
  190. */
  191. kprobe_opcode_t cur_insn = *addr;
  192. if (is_trap(cur_insn))
  193. goto no_kprobe;
  194. /*
  195. * The breakpoint instruction was removed right
  196. * after we hit it. Another cpu has removed
  197. * either a probepoint or a debugger breakpoint
  198. * at this address. In either case, no further
  199. * handling of this interrupt is appropriate.
  200. */
  201. ret = 1;
  202. }
  203. /* Not one of ours: let kernel handle it */
  204. goto no_kprobe;
  205. }
  206. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  207. set_current_kprobe(p, regs, kcb);
  208. if (p->pre_handler && p->pre_handler(p, regs))
  209. /* handler has already set things up, so skip ss setup */
  210. return 1;
  211. ss_probe:
  212. if (p->ainsn.boostable >= 0) {
  213. unsigned int insn = *p->ainsn.insn;
  214. /* regs->nip is also adjusted if emulate_step returns 1 */
  215. ret = emulate_step(regs, insn);
  216. if (ret > 0) {
  217. /*
  218. * Once this instruction has been boosted
  219. * successfully, set the boostable flag
  220. */
  221. if (unlikely(p->ainsn.boostable == 0))
  222. p->ainsn.boostable = 1;
  223. if (p->post_handler)
  224. p->post_handler(p, regs, 0);
  225. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  226. reset_current_kprobe();
  227. preempt_enable_no_resched();
  228. return 1;
  229. } else if (ret < 0) {
  230. /*
  231. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  232. * So, we should never get here... but, its still
  233. * good to catch them, just in case...
  234. */
  235. printk("Can't step on instruction %x\n", insn);
  236. BUG();
  237. } else if (ret == 0)
  238. /* This instruction can't be boosted */
  239. p->ainsn.boostable = -1;
  240. }
  241. prepare_singlestep(p, regs);
  242. kcb->kprobe_status = KPROBE_HIT_SS;
  243. return 1;
  244. no_kprobe:
  245. preempt_enable_no_resched();
  246. return ret;
  247. }
  248. /*
  249. * Function return probe trampoline:
  250. * - init_kprobes() establishes a probepoint here
  251. * - When the probed function returns, this probe
  252. * causes the handlers to fire
  253. */
  254. asm(".global kretprobe_trampoline\n"
  255. ".type kretprobe_trampoline, @function\n"
  256. "kretprobe_trampoline:\n"
  257. "nop\n"
  258. ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
  259. /*
  260. * Called when the probe at kretprobe trampoline is hit
  261. */
  262. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  263. struct pt_regs *regs)
  264. {
  265. struct kretprobe_instance *ri = NULL;
  266. struct hlist_head *head, empty_rp;
  267. struct hlist_node *tmp;
  268. unsigned long flags, orig_ret_address = 0;
  269. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  270. INIT_HLIST_HEAD(&empty_rp);
  271. kretprobe_hash_lock(current, &head, &flags);
  272. /*
  273. * It is possible to have multiple instances associated with a given
  274. * task either because an multiple functions in the call path
  275. * have a return probe installed on them, and/or more than one return
  276. * return probe was registered for a target function.
  277. *
  278. * We can handle this because:
  279. * - instances are always inserted at the head of the list
  280. * - when multiple return probes are registered for the same
  281. * function, the first instance's ret_addr will point to the
  282. * real return address, and all the rest will point to
  283. * kretprobe_trampoline
  284. */
  285. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  286. if (ri->task != current)
  287. /* another task is sharing our hash bucket */
  288. continue;
  289. if (ri->rp && ri->rp->handler)
  290. ri->rp->handler(ri, regs);
  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. regs->nip = orig_ret_address;
  303. reset_current_kprobe();
  304. kretprobe_hash_unlock(current, &flags);
  305. preempt_enable_no_resched();
  306. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  307. hlist_del(&ri->hlist);
  308. kfree(ri);
  309. }
  310. /*
  311. * By returning a non-zero value, we are telling
  312. * kprobe_handler() that we don't want the post_handler
  313. * to run (and have re-enabled preemption)
  314. */
  315. return 1;
  316. }
  317. /*
  318. * Called after single-stepping. p->addr is the address of the
  319. * instruction whose first byte has been replaced by the "breakpoint"
  320. * instruction. To avoid the SMP problems that can occur when we
  321. * temporarily put back the original opcode to single-step, we
  322. * single-stepped a copy of the instruction. The address of this
  323. * copy is p->ainsn.insn.
  324. */
  325. int __kprobes kprobe_post_handler(struct pt_regs *regs)
  326. {
  327. struct kprobe *cur = kprobe_running();
  328. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  329. if (!cur || user_mode(regs))
  330. return 0;
  331. /* make sure we got here for instruction we have a kprobe on */
  332. if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
  333. return 0;
  334. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  335. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  336. cur->post_handler(cur, regs, 0);
  337. }
  338. /* Adjust nip to after the single-stepped instruction */
  339. regs->nip = (unsigned long)cur->addr + 4;
  340. regs->msr |= kcb->kprobe_saved_msr;
  341. /*Restore back the original saved kprobes variables and continue. */
  342. if (kcb->kprobe_status == KPROBE_REENTER) {
  343. restore_previous_kprobe(kcb);
  344. goto out;
  345. }
  346. reset_current_kprobe();
  347. out:
  348. preempt_enable_no_resched();
  349. /*
  350. * if somebody else is singlestepping across a probe point, msr
  351. * will have DE/SE set, in which case, continue the remaining processing
  352. * of do_debug, as if this is not a probe hit.
  353. */
  354. if (regs->msr & MSR_SINGLESTEP)
  355. return 0;
  356. return 1;
  357. }
  358. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  359. {
  360. struct kprobe *cur = kprobe_running();
  361. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  362. const struct exception_table_entry *entry;
  363. switch(kcb->kprobe_status) {
  364. case KPROBE_HIT_SS:
  365. case KPROBE_REENTER:
  366. /*
  367. * We are here because the instruction being single
  368. * stepped caused a page fault. We reset the current
  369. * kprobe and the nip points back to the probe address
  370. * and allow the page fault handler to continue as a
  371. * normal page fault.
  372. */
  373. regs->nip = (unsigned long)cur->addr;
  374. regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
  375. regs->msr |= kcb->kprobe_saved_msr;
  376. if (kcb->kprobe_status == KPROBE_REENTER)
  377. restore_previous_kprobe(kcb);
  378. else
  379. reset_current_kprobe();
  380. preempt_enable_no_resched();
  381. break;
  382. case KPROBE_HIT_ACTIVE:
  383. case KPROBE_HIT_SSDONE:
  384. /*
  385. * We increment the nmissed count for accounting,
  386. * we can also use npre/npostfault count for accounting
  387. * these specific fault cases.
  388. */
  389. kprobes_inc_nmissed_count(cur);
  390. /*
  391. * We come here because instructions in the pre/post
  392. * handler caused the page_fault, this could happen
  393. * if handler tries to access user space by
  394. * copy_from_user(), get_user() etc. Let the
  395. * user-specified handler try to fix it first.
  396. */
  397. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  398. return 1;
  399. /*
  400. * In case the user-specified fault handler returned
  401. * zero, try to fix up.
  402. */
  403. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  404. regs->nip = extable_fixup(entry);
  405. return 1;
  406. }
  407. /*
  408. * fixup_exception() could not handle it,
  409. * Let do_page_fault() fix it.
  410. */
  411. break;
  412. default:
  413. break;
  414. }
  415. return 0;
  416. }
  417. /*
  418. * Wrapper routine to for handling exceptions.
  419. */
  420. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  421. unsigned long val, void *data)
  422. {
  423. return NOTIFY_DONE;
  424. }
  425. unsigned long arch_deref_entry_point(void *entry)
  426. {
  427. return ppc_global_function_entry(entry);
  428. }
  429. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  430. {
  431. struct jprobe *jp = container_of(p, struct jprobe, kp);
  432. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  433. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  434. /* setup return addr to the jprobe handler routine */
  435. regs->nip = arch_deref_entry_point(jp->entry);
  436. #ifdef PPC64_ELF_ABI_v2
  437. regs->gpr[12] = (unsigned long)jp->entry;
  438. #elif defined(PPC64_ELF_ABI_v1)
  439. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  440. #endif
  441. return 1;
  442. }
  443. void __used __kprobes jprobe_return(void)
  444. {
  445. asm volatile("trap" ::: "memory");
  446. }
  447. static void __used __kprobes jprobe_return_end(void)
  448. {
  449. };
  450. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  451. {
  452. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  453. /*
  454. * FIXME - we should ideally be validating that we got here 'cos
  455. * of the "trap" in jprobe_return() above, before restoring the
  456. * saved regs...
  457. */
  458. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  459. preempt_enable_no_resched();
  460. return 1;
  461. }
  462. static struct kprobe trampoline_p = {
  463. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  464. .pre_handler = trampoline_probe_handler
  465. };
  466. int __init arch_init_kprobes(void)
  467. {
  468. return register_kprobe(&trampoline_p);
  469. }
  470. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  471. {
  472. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  473. return 1;
  474. return 0;
  475. }