kprobes.c 15 KB

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