kprobes.c 14 KB

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