kprobes.c 13 KB

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