kprobes.c 15 KB

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