kprobes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/config.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/preempt.h>
  32. #include <linux/module.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/kdebug.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. 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)) {
  47. printk("Cannot register a kprobe on rfid or mtmsrd\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, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  58. p->opcode = *p->addr;
  59. }
  60. return ret;
  61. }
  62. void __kprobes arch_arm_kprobe(struct kprobe *p)
  63. {
  64. *p->addr = BREAKPOINT_INSTRUCTION;
  65. flush_icache_range((unsigned long) p->addr,
  66. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  67. }
  68. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  69. {
  70. *p->addr = p->opcode;
  71. flush_icache_range((unsigned long) p->addr,
  72. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  73. }
  74. void __kprobes arch_remove_kprobe(struct kprobe *p)
  75. {
  76. mutex_lock(&kprobe_mutex);
  77. free_insn_slot(p->ainsn.insn);
  78. mutex_unlock(&kprobe_mutex);
  79. }
  80. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  81. {
  82. regs->msr |= MSR_SE;
  83. /*
  84. * On powerpc we should single step on the original
  85. * instruction even if the probed insn is a trap
  86. * variant as values in regs could play a part in
  87. * if the trap is taken or not
  88. */
  89. regs->nip = (unsigned long)p->ainsn.insn;
  90. }
  91. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  92. {
  93. kcb->prev_kprobe.kp = kprobe_running();
  94. kcb->prev_kprobe.status = kcb->kprobe_status;
  95. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  96. }
  97. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  98. {
  99. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  100. kcb->kprobe_status = kcb->prev_kprobe.status;
  101. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  102. }
  103. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  104. struct kprobe_ctlblk *kcb)
  105. {
  106. __get_cpu_var(current_kprobe) = p;
  107. kcb->kprobe_saved_msr = regs->msr;
  108. }
  109. /* Called with kretprobe_lock held */
  110. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  111. struct pt_regs *regs)
  112. {
  113. struct kretprobe_instance *ri;
  114. if ((ri = get_free_rp_inst(rp)) != NULL) {
  115. ri->rp = rp;
  116. ri->task = current;
  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. add_rp_inst(ri);
  121. } else {
  122. rp->nmissed++;
  123. }
  124. }
  125. static int __kprobes kprobe_handler(struct pt_regs *regs)
  126. {
  127. struct kprobe *p;
  128. int ret = 0;
  129. unsigned int *addr = (unsigned int *)regs->nip;
  130. struct kprobe_ctlblk *kcb;
  131. /*
  132. * We don't want to be preempted for the entire
  133. * duration of kprobe processing
  134. */
  135. preempt_disable();
  136. kcb = get_kprobe_ctlblk();
  137. /* Check we're not actually recursing */
  138. if (kprobe_running()) {
  139. p = get_kprobe(addr);
  140. if (p) {
  141. kprobe_opcode_t insn = *p->ainsn.insn;
  142. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  143. is_trap(insn)) {
  144. regs->msr &= ~MSR_SE;
  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 = __get_cpu_var(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. prepare_singlestep(p, regs);
  211. kcb->kprobe_status = KPROBE_HIT_SS;
  212. return 1;
  213. no_kprobe:
  214. preempt_enable_no_resched();
  215. return ret;
  216. }
  217. /*
  218. * Function return probe trampoline:
  219. * - init_kprobes() establishes a probepoint here
  220. * - When the probed function returns, this probe
  221. * causes the handlers to fire
  222. */
  223. void kretprobe_trampoline_holder(void)
  224. {
  225. asm volatile(".global kretprobe_trampoline\n"
  226. "kretprobe_trampoline:\n"
  227. "nop\n");
  228. }
  229. /*
  230. * Called when the probe at kretprobe trampoline is hit
  231. */
  232. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  233. {
  234. struct kretprobe_instance *ri = NULL;
  235. struct hlist_head *head;
  236. struct hlist_node *node, *tmp;
  237. unsigned long flags, orig_ret_address = 0;
  238. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  239. spin_lock_irqsave(&kretprobe_lock, flags);
  240. head = kretprobe_inst_table_head(current);
  241. /*
  242. * It is possible to have multiple instances associated with a given
  243. * task either because an multiple functions in the call path
  244. * have a return probe installed on them, and/or more then one return
  245. * return probe was registered for a target function.
  246. *
  247. * We can handle this because:
  248. * - instances are always inserted at the head of the list
  249. * - when multiple return probes are registered for the same
  250. * function, the first instance's ret_addr will point to the
  251. * real return address, and all the rest will point to
  252. * kretprobe_trampoline
  253. */
  254. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  255. if (ri->task != current)
  256. /* another task is sharing our hash bucket */
  257. continue;
  258. if (ri->rp && ri->rp->handler)
  259. ri->rp->handler(ri, regs);
  260. orig_ret_address = (unsigned long)ri->ret_addr;
  261. recycle_rp_inst(ri);
  262. if (orig_ret_address != trampoline_address)
  263. /*
  264. * This is the real return address. Any other
  265. * instances associated with this task are for
  266. * other calls deeper on the call stack
  267. */
  268. break;
  269. }
  270. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  271. regs->nip = orig_ret_address;
  272. reset_current_kprobe();
  273. spin_unlock_irqrestore(&kretprobe_lock, flags);
  274. preempt_enable_no_resched();
  275. /*
  276. * By returning a non-zero value, we are telling
  277. * kprobe_handler() that we don't want the post_handler
  278. * to run (and have re-enabled preemption)
  279. */
  280. return 1;
  281. }
  282. /*
  283. * Called after single-stepping. p->addr is the address of the
  284. * instruction whose first byte has been replaced by the "breakpoint"
  285. * instruction. To avoid the SMP problems that can occur when we
  286. * temporarily put back the original opcode to single-step, we
  287. * single-stepped a copy of the instruction. The address of this
  288. * copy is p->ainsn.insn.
  289. */
  290. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  291. {
  292. int ret;
  293. unsigned int insn = *p->ainsn.insn;
  294. regs->nip = (unsigned long)p->addr;
  295. ret = emulate_step(regs, insn);
  296. if (ret == 0)
  297. regs->nip = (unsigned long)p->addr + 4;
  298. }
  299. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  300. {
  301. struct kprobe *cur = kprobe_running();
  302. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  303. if (!cur)
  304. return 0;
  305. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  306. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  307. cur->post_handler(cur, regs, 0);
  308. }
  309. resume_execution(cur, regs);
  310. regs->msr |= kcb->kprobe_saved_msr;
  311. /*Restore back the original saved kprobes variables and continue. */
  312. if (kcb->kprobe_status == KPROBE_REENTER) {
  313. restore_previous_kprobe(kcb);
  314. goto out;
  315. }
  316. reset_current_kprobe();
  317. out:
  318. preempt_enable_no_resched();
  319. /*
  320. * if somebody else is singlestepping across a probe point, msr
  321. * will have SE set, in which case, continue the remaining processing
  322. * of do_debug, as if this is not a probe hit.
  323. */
  324. if (regs->msr & MSR_SE)
  325. return 0;
  326. return 1;
  327. }
  328. static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  329. {
  330. struct kprobe *cur = kprobe_running();
  331. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  332. const struct exception_table_entry *entry;
  333. switch(kcb->kprobe_status) {
  334. case KPROBE_HIT_SS:
  335. case KPROBE_REENTER:
  336. /*
  337. * We are here because the instruction being single
  338. * stepped caused a page fault. We reset the current
  339. * kprobe and the nip points back to the probe address
  340. * and allow the page fault handler to continue as a
  341. * normal page fault.
  342. */
  343. regs->nip = (unsigned long)cur->addr;
  344. regs->msr &= ~MSR_SE;
  345. regs->msr |= kcb->kprobe_saved_msr;
  346. if (kcb->kprobe_status == KPROBE_REENTER)
  347. restore_previous_kprobe(kcb);
  348. else
  349. reset_current_kprobe();
  350. preempt_enable_no_resched();
  351. break;
  352. case KPROBE_HIT_ACTIVE:
  353. case KPROBE_HIT_SSDONE:
  354. /*
  355. * We increment the nmissed count for accounting,
  356. * we can also use npre/npostfault count for accouting
  357. * these specific fault cases.
  358. */
  359. kprobes_inc_nmissed_count(cur);
  360. /*
  361. * We come here because instructions in the pre/post
  362. * handler caused the page_fault, this could happen
  363. * if handler tries to access user space by
  364. * copy_from_user(), get_user() etc. Let the
  365. * user-specified handler try to fix it first.
  366. */
  367. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  368. return 1;
  369. /*
  370. * In case the user-specified fault handler returned
  371. * zero, try to fix up.
  372. */
  373. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  374. regs->nip = entry->fixup;
  375. return 1;
  376. }
  377. /*
  378. * fixup_exception() could not handle it,
  379. * Let do_page_fault() fix it.
  380. */
  381. break;
  382. default:
  383. break;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Wrapper routine to for handling exceptions.
  389. */
  390. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  391. unsigned long val, void *data)
  392. {
  393. struct die_args *args = (struct die_args *)data;
  394. int ret = NOTIFY_DONE;
  395. if (args->regs && user_mode(args->regs))
  396. return ret;
  397. switch (val) {
  398. case DIE_BPT:
  399. if (kprobe_handler(args->regs))
  400. ret = NOTIFY_STOP;
  401. break;
  402. case DIE_SSTEP:
  403. if (post_kprobe_handler(args->regs))
  404. ret = NOTIFY_STOP;
  405. break;
  406. case DIE_PAGE_FAULT:
  407. /* kprobe_running() needs smp_processor_id() */
  408. preempt_disable();
  409. if (kprobe_running() &&
  410. kprobe_fault_handler(args->regs, args->trapnr))
  411. ret = NOTIFY_STOP;
  412. preempt_enable();
  413. break;
  414. default:
  415. break;
  416. }
  417. return ret;
  418. }
  419. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  420. {
  421. struct jprobe *jp = container_of(p, struct jprobe, kp);
  422. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  423. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  424. /* setup return addr to the jprobe handler routine */
  425. regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
  426. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  427. return 1;
  428. }
  429. void __kprobes jprobe_return(void)
  430. {
  431. asm volatile("trap" ::: "memory");
  432. }
  433. void __kprobes jprobe_return_end(void)
  434. {
  435. };
  436. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  437. {
  438. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  439. /*
  440. * FIXME - we should ideally be validating that we got here 'cos
  441. * of the "trap" in jprobe_return() above, before restoring the
  442. * saved regs...
  443. */
  444. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  445. preempt_enable_no_resched();
  446. return 1;
  447. }
  448. static struct kprobe trampoline_p = {
  449. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  450. .pre_handler = trampoline_probe_handler
  451. };
  452. int __init arch_init_kprobes(void)
  453. {
  454. return register_kprobe(&trampoline_p);
  455. }