kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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/module.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 <asm/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. static 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. /*
  131. * We don't want to be preempted for the entire
  132. * duration of kprobe processing
  133. */
  134. preempt_disable();
  135. kcb = get_kprobe_ctlblk();
  136. /* Check we're not actually recursing */
  137. if (kprobe_running()) {
  138. p = get_kprobe(addr);
  139. if (p) {
  140. kprobe_opcode_t insn = *p->ainsn.insn;
  141. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  142. is_trap(insn)) {
  143. /* Turn off 'trace' bits */
  144. regs->msr &= ~MSR_SINGLESTEP;
  145. regs->msr |= kcb->kprobe_saved_msr;
  146. goto no_kprobe;
  147. }
  148. /* We have reentered the kprobe_handler(), since
  149. * another probe was hit while within the handler.
  150. * We here save the original kprobes variables and
  151. * just single step on the instruction of the new probe
  152. * without calling any user handlers.
  153. */
  154. save_previous_kprobe(kcb);
  155. set_current_kprobe(p, regs, kcb);
  156. kcb->kprobe_saved_msr = regs->msr;
  157. kprobes_inc_nmissed_count(p);
  158. prepare_singlestep(p, regs);
  159. kcb->kprobe_status = KPROBE_REENTER;
  160. return 1;
  161. } else {
  162. if (*addr != BREAKPOINT_INSTRUCTION) {
  163. /* If trap variant, then it belongs not to us */
  164. kprobe_opcode_t cur_insn = *addr;
  165. if (is_trap(cur_insn))
  166. goto no_kprobe;
  167. /* The breakpoint instruction was removed by
  168. * another cpu right after we hit, no further
  169. * handling of this interrupt is appropriate
  170. */
  171. ret = 1;
  172. goto no_kprobe;
  173. }
  174. p = __this_cpu_read(current_kprobe);
  175. if (p->break_handler && p->break_handler(p, regs)) {
  176. goto ss_probe;
  177. }
  178. }
  179. goto no_kprobe;
  180. }
  181. p = get_kprobe(addr);
  182. if (!p) {
  183. if (*addr != BREAKPOINT_INSTRUCTION) {
  184. /*
  185. * PowerPC has multiple variants of the "trap"
  186. * instruction. If the current instruction is a
  187. * trap variant, it could belong to someone else
  188. */
  189. kprobe_opcode_t cur_insn = *addr;
  190. if (is_trap(cur_insn))
  191. goto no_kprobe;
  192. /*
  193. * The breakpoint instruction was removed right
  194. * after we hit it. Another cpu has removed
  195. * either a probepoint or a debugger breakpoint
  196. * at this address. In either case, no further
  197. * handling of this interrupt is appropriate.
  198. */
  199. ret = 1;
  200. }
  201. /* Not one of ours: let kernel handle it */
  202. goto no_kprobe;
  203. }
  204. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  205. set_current_kprobe(p, regs, kcb);
  206. if (p->pre_handler && p->pre_handler(p, regs))
  207. /* handler has already set things up, so skip ss setup */
  208. return 1;
  209. ss_probe:
  210. if (p->ainsn.boostable >= 0) {
  211. unsigned int insn = *p->ainsn.insn;
  212. /* regs->nip is also adjusted if emulate_step returns 1 */
  213. ret = emulate_step(regs, insn);
  214. if (ret > 0) {
  215. /*
  216. * Once this instruction has been boosted
  217. * successfully, set the boostable flag
  218. */
  219. if (unlikely(p->ainsn.boostable == 0))
  220. p->ainsn.boostable = 1;
  221. if (p->post_handler)
  222. p->post_handler(p, regs, 0);
  223. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  224. reset_current_kprobe();
  225. preempt_enable_no_resched();
  226. return 1;
  227. } else if (ret < 0) {
  228. /*
  229. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  230. * So, we should never get here... but, its still
  231. * good to catch them, just in case...
  232. */
  233. printk("Can't step on instruction %x\n", insn);
  234. BUG();
  235. } else if (ret == 0)
  236. /* This instruction can't be boosted */
  237. p->ainsn.boostable = -1;
  238. }
  239. prepare_singlestep(p, regs);
  240. kcb->kprobe_status = KPROBE_HIT_SS;
  241. return 1;
  242. no_kprobe:
  243. preempt_enable_no_resched();
  244. return ret;
  245. }
  246. /*
  247. * Function return probe trampoline:
  248. * - init_kprobes() establishes a probepoint here
  249. * - When the probed function returns, this probe
  250. * causes the handlers to fire
  251. */
  252. static void __used kretprobe_trampoline_holder(void)
  253. {
  254. asm volatile(".global kretprobe_trampoline\n"
  255. "kretprobe_trampoline:\n"
  256. "nop\n");
  257. }
  258. /*
  259. * Called when the probe at kretprobe trampoline is hit
  260. */
  261. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  262. struct pt_regs *regs)
  263. {
  264. struct kretprobe_instance *ri = NULL;
  265. struct hlist_head *head, empty_rp;
  266. struct hlist_node *tmp;
  267. unsigned long flags, orig_ret_address = 0;
  268. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  269. INIT_HLIST_HEAD(&empty_rp);
  270. kretprobe_hash_lock(current, &head, &flags);
  271. /*
  272. * It is possible to have multiple instances associated with a given
  273. * task either because an multiple functions in the call path
  274. * have a return probe installed on them, and/or more than one return
  275. * return probe was registered for a target function.
  276. *
  277. * We can handle this because:
  278. * - instances are always inserted at the head of the list
  279. * - when multiple return probes are registered for the same
  280. * function, the first instance's ret_addr will point to the
  281. * real return address, and all the rest will point to
  282. * kretprobe_trampoline
  283. */
  284. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  285. if (ri->task != current)
  286. /* another task is sharing our hash bucket */
  287. continue;
  288. if (ri->rp && ri->rp->handler)
  289. ri->rp->handler(ri, regs);
  290. orig_ret_address = (unsigned long)ri->ret_addr;
  291. recycle_rp_inst(ri, &empty_rp);
  292. if (orig_ret_address != trampoline_address)
  293. /*
  294. * This is the real return address. Any other
  295. * instances associated with this task are for
  296. * other calls deeper on the call stack
  297. */
  298. break;
  299. }
  300. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  301. regs->nip = orig_ret_address;
  302. reset_current_kprobe();
  303. kretprobe_hash_unlock(current, &flags);
  304. preempt_enable_no_resched();
  305. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  306. hlist_del(&ri->hlist);
  307. kfree(ri);
  308. }
  309. /*
  310. * By returning a non-zero value, we are telling
  311. * kprobe_handler() that we don't want the post_handler
  312. * to run (and have re-enabled preemption)
  313. */
  314. return 1;
  315. }
  316. /*
  317. * Called after single-stepping. p->addr is the address of the
  318. * instruction whose first byte has been replaced by the "breakpoint"
  319. * instruction. To avoid the SMP problems that can occur when we
  320. * temporarily put back the original opcode to single-step, we
  321. * single-stepped a copy of the instruction. The address of this
  322. * copy is p->ainsn.insn.
  323. */
  324. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  325. {
  326. struct kprobe *cur = kprobe_running();
  327. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  328. if (!cur)
  329. return 0;
  330. /* make sure we got here for instruction we have a kprobe on */
  331. if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
  332. return 0;
  333. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  334. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  335. cur->post_handler(cur, regs, 0);
  336. }
  337. /* Adjust nip to after the single-stepped instruction */
  338. regs->nip = (unsigned long)cur->addr + 4;
  339. regs->msr |= kcb->kprobe_saved_msr;
  340. /*Restore back the original saved kprobes variables and continue. */
  341. if (kcb->kprobe_status == KPROBE_REENTER) {
  342. restore_previous_kprobe(kcb);
  343. goto out;
  344. }
  345. reset_current_kprobe();
  346. out:
  347. preempt_enable_no_resched();
  348. /*
  349. * if somebody else is singlestepping across a probe point, msr
  350. * will have DE/SE set, in which case, continue the remaining processing
  351. * of do_debug, as if this is not a probe hit.
  352. */
  353. if (regs->msr & MSR_SINGLESTEP)
  354. return 0;
  355. return 1;
  356. }
  357. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  358. {
  359. struct kprobe *cur = kprobe_running();
  360. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  361. const struct exception_table_entry *entry;
  362. switch(kcb->kprobe_status) {
  363. case KPROBE_HIT_SS:
  364. case KPROBE_REENTER:
  365. /*
  366. * We are here because the instruction being single
  367. * stepped caused a page fault. We reset the current
  368. * kprobe and the nip points back to the probe address
  369. * and allow the page fault handler to continue as a
  370. * normal page fault.
  371. */
  372. regs->nip = (unsigned long)cur->addr;
  373. regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
  374. regs->msr |= kcb->kprobe_saved_msr;
  375. if (kcb->kprobe_status == KPROBE_REENTER)
  376. restore_previous_kprobe(kcb);
  377. else
  378. reset_current_kprobe();
  379. preempt_enable_no_resched();
  380. break;
  381. case KPROBE_HIT_ACTIVE:
  382. case KPROBE_HIT_SSDONE:
  383. /*
  384. * We increment the nmissed count for accounting,
  385. * we can also use npre/npostfault count for accounting
  386. * these specific fault cases.
  387. */
  388. kprobes_inc_nmissed_count(cur);
  389. /*
  390. * We come here because instructions in the pre/post
  391. * handler caused the page_fault, this could happen
  392. * if handler tries to access user space by
  393. * copy_from_user(), get_user() etc. Let the
  394. * user-specified handler try to fix it first.
  395. */
  396. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  397. return 1;
  398. /*
  399. * In case the user-specified fault handler returned
  400. * zero, try to fix up.
  401. */
  402. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  403. regs->nip = entry->fixup;
  404. return 1;
  405. }
  406. /*
  407. * fixup_exception() could not handle it,
  408. * Let do_page_fault() fix it.
  409. */
  410. break;
  411. default:
  412. break;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * Wrapper routine to for handling exceptions.
  418. */
  419. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  420. unsigned long val, void *data)
  421. {
  422. struct die_args *args = (struct die_args *)data;
  423. int ret = NOTIFY_DONE;
  424. if (args->regs && user_mode(args->regs))
  425. return ret;
  426. switch (val) {
  427. case DIE_BPT:
  428. if (kprobe_handler(args->regs))
  429. ret = NOTIFY_STOP;
  430. break;
  431. case DIE_SSTEP:
  432. if (post_kprobe_handler(args->regs))
  433. ret = NOTIFY_STOP;
  434. break;
  435. default:
  436. break;
  437. }
  438. return ret;
  439. }
  440. unsigned long arch_deref_entry_point(void *entry)
  441. {
  442. return ppc_global_function_entry(entry);
  443. }
  444. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  445. {
  446. struct jprobe *jp = container_of(p, struct jprobe, kp);
  447. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  448. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  449. /* setup return addr to the jprobe handler routine */
  450. regs->nip = arch_deref_entry_point(jp->entry);
  451. #ifdef CONFIG_PPC64
  452. #if defined(_CALL_ELF) && _CALL_ELF == 2
  453. regs->gpr[12] = (unsigned long)jp->entry;
  454. #else
  455. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  456. #endif
  457. #endif
  458. /*
  459. * jprobes use jprobe_return() which skips the normal return
  460. * path of the function, and this messes up the accounting of the
  461. * function graph tracer.
  462. *
  463. * Pause function graph tracing while performing the jprobe function.
  464. */
  465. pause_graph_tracing();
  466. return 1;
  467. }
  468. void __used __kprobes jprobe_return(void)
  469. {
  470. asm volatile("trap" ::: "memory");
  471. }
  472. static void __used __kprobes jprobe_return_end(void)
  473. {
  474. };
  475. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  476. {
  477. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  478. /*
  479. * FIXME - we should ideally be validating that we got here 'cos
  480. * of the "trap" in jprobe_return() above, before restoring the
  481. * saved regs...
  482. */
  483. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  484. /* It's OK to start function graph tracing again */
  485. unpause_graph_tracing();
  486. preempt_enable_no_resched();
  487. return 1;
  488. }
  489. static struct kprobe trampoline_p = {
  490. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  491. .pre_handler = trampoline_probe_handler
  492. };
  493. int __init arch_init_kprobes(void)
  494. {
  495. return register_kprobe(&trampoline_p);
  496. }
  497. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  498. {
  499. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  500. return 1;
  501. return 0;
  502. }