kprobes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Kernel Probes (KProbes)
  3. * arch/ppc64/kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation ( includes contributions from
  23. * Rusty Russell).
  24. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  25. * interface to access function arguments.
  26. * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
  27. * for PPC64
  28. */
  29. #include <linux/config.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/preempt.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/kdebug.h>
  35. #include <asm/sstep.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. }
  59. return ret;
  60. }
  61. void __kprobes arch_arm_kprobe(struct kprobe *p)
  62. {
  63. *p->addr = BREAKPOINT_INSTRUCTION;
  64. flush_icache_range((unsigned long) p->addr,
  65. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  66. }
  67. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  68. {
  69. *p->addr = p->opcode;
  70. flush_icache_range((unsigned long) p->addr,
  71. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  72. }
  73. void __kprobes arch_remove_kprobe(struct kprobe *p)
  74. {
  75. free_insn_slot(p->ainsn.insn);
  76. }
  77. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  78. {
  79. kprobe_opcode_t insn = *p->ainsn.insn;
  80. regs->msr |= MSR_SE;
  81. /* single step inline if it is a trap variant */
  82. if (is_trap(insn))
  83. regs->nip = (unsigned long)p->addr;
  84. else
  85. regs->nip = (unsigned long)p->ainsn.insn;
  86. }
  87. static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  88. {
  89. kcb->prev_kprobe.kp = kprobe_running();
  90. kcb->prev_kprobe.status = kcb->kprobe_status;
  91. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  92. }
  93. static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  94. {
  95. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  96. kcb->kprobe_status = kcb->prev_kprobe.status;
  97. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  98. }
  99. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  100. struct kprobe_ctlblk *kcb)
  101. {
  102. __get_cpu_var(current_kprobe) = p;
  103. kcb->kprobe_saved_msr = regs->msr;
  104. }
  105. /* Called with kretprobe_lock held */
  106. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  107. struct pt_regs *regs)
  108. {
  109. struct kretprobe_instance *ri;
  110. if ((ri = get_free_rp_inst(rp)) != NULL) {
  111. ri->rp = rp;
  112. ri->task = current;
  113. ri->ret_addr = (kprobe_opcode_t *)regs->link;
  114. /* Replace the return addr with trampoline addr */
  115. regs->link = (unsigned long)kretprobe_trampoline;
  116. add_rp_inst(ri);
  117. } else {
  118. rp->nmissed++;
  119. }
  120. }
  121. static inline int 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. p = __get_cpu_var(current_kprobe);
  159. if (p->break_handler && p->break_handler(p, regs)) {
  160. goto ss_probe;
  161. }
  162. }
  163. goto no_kprobe;
  164. }
  165. p = get_kprobe(addr);
  166. if (!p) {
  167. if (*addr != BREAKPOINT_INSTRUCTION) {
  168. /*
  169. * PowerPC has multiple variants of the "trap"
  170. * instruction. If the current instruction is a
  171. * trap variant, it could belong to someone else
  172. */
  173. kprobe_opcode_t cur_insn = *addr;
  174. if (is_trap(cur_insn))
  175. goto no_kprobe;
  176. /*
  177. * The breakpoint instruction was removed right
  178. * after we hit it. Another cpu has removed
  179. * either a probepoint or a debugger breakpoint
  180. * at this address. In either case, no further
  181. * handling of this interrupt is appropriate.
  182. */
  183. ret = 1;
  184. }
  185. /* Not one of ours: let kernel handle it */
  186. goto no_kprobe;
  187. }
  188. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  189. set_current_kprobe(p, regs, kcb);
  190. if (p->pre_handler && p->pre_handler(p, regs))
  191. /* handler has already set things up, so skip ss setup */
  192. return 1;
  193. ss_probe:
  194. prepare_singlestep(p, regs);
  195. kcb->kprobe_status = KPROBE_HIT_SS;
  196. return 1;
  197. no_kprobe:
  198. preempt_enable_no_resched();
  199. return ret;
  200. }
  201. /*
  202. * Function return probe trampoline:
  203. * - init_kprobes() establishes a probepoint here
  204. * - When the probed function returns, this probe
  205. * causes the handlers to fire
  206. */
  207. void kretprobe_trampoline_holder(void)
  208. {
  209. asm volatile(".global kretprobe_trampoline\n"
  210. "kretprobe_trampoline:\n"
  211. "nop\n");
  212. }
  213. /*
  214. * Called when the probe at kretprobe trampoline is hit
  215. */
  216. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  217. {
  218. struct kretprobe_instance *ri = NULL;
  219. struct hlist_head *head;
  220. struct hlist_node *node, *tmp;
  221. unsigned long flags, orig_ret_address = 0;
  222. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  223. spin_lock_irqsave(&kretprobe_lock, flags);
  224. head = kretprobe_inst_table_head(current);
  225. /*
  226. * It is possible to have multiple instances associated with a given
  227. * task either because an multiple functions in the call path
  228. * have a return probe installed on them, and/or more then one return
  229. * return probe was registered for a target function.
  230. *
  231. * We can handle this because:
  232. * - instances are always inserted at the head of the list
  233. * - when multiple return probes are registered for the same
  234. * function, the first instance's ret_addr will point to the
  235. * real return address, and all the rest will point to
  236. * kretprobe_trampoline
  237. */
  238. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  239. if (ri->task != current)
  240. /* another task is sharing our hash bucket */
  241. continue;
  242. if (ri->rp && ri->rp->handler)
  243. ri->rp->handler(ri, regs);
  244. orig_ret_address = (unsigned long)ri->ret_addr;
  245. recycle_rp_inst(ri);
  246. if (orig_ret_address != trampoline_address)
  247. /*
  248. * This is the real return address. Any other
  249. * instances associated with this task are for
  250. * other calls deeper on the call stack
  251. */
  252. break;
  253. }
  254. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  255. regs->nip = orig_ret_address;
  256. reset_current_kprobe();
  257. spin_unlock_irqrestore(&kretprobe_lock, flags);
  258. preempt_enable_no_resched();
  259. /*
  260. * By returning a non-zero value, we are telling
  261. * kprobe_handler() that we don't want the post_handler
  262. * to run (and have re-enabled preemption)
  263. */
  264. return 1;
  265. }
  266. /*
  267. * Called after single-stepping. p->addr is the address of the
  268. * instruction whose first byte has been replaced by the "breakpoint"
  269. * instruction. To avoid the SMP problems that can occur when we
  270. * temporarily put back the original opcode to single-step, we
  271. * single-stepped a copy of the instruction. The address of this
  272. * copy is p->ainsn.insn.
  273. */
  274. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  275. {
  276. int ret;
  277. unsigned int insn = *p->ainsn.insn;
  278. regs->nip = (unsigned long)p->addr;
  279. ret = emulate_step(regs, insn);
  280. if (ret == 0)
  281. regs->nip = (unsigned long)p->addr + 4;
  282. }
  283. static inline int post_kprobe_handler(struct pt_regs *regs)
  284. {
  285. struct kprobe *cur = kprobe_running();
  286. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  287. if (!cur)
  288. return 0;
  289. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  290. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  291. cur->post_handler(cur, regs, 0);
  292. }
  293. resume_execution(cur, regs);
  294. regs->msr |= kcb->kprobe_saved_msr;
  295. /*Restore back the original saved kprobes variables and continue. */
  296. if (kcb->kprobe_status == KPROBE_REENTER) {
  297. restore_previous_kprobe(kcb);
  298. goto out;
  299. }
  300. reset_current_kprobe();
  301. out:
  302. preempt_enable_no_resched();
  303. /*
  304. * if somebody else is singlestepping across a probe point, msr
  305. * will have SE set, in which case, continue the remaining processing
  306. * of do_debug, as if this is not a probe hit.
  307. */
  308. if (regs->msr & MSR_SE)
  309. return 0;
  310. return 1;
  311. }
  312. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  313. {
  314. struct kprobe *cur = kprobe_running();
  315. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  316. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  317. return 1;
  318. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  319. resume_execution(cur, regs);
  320. regs->msr &= ~MSR_SE;
  321. regs->msr |= kcb->kprobe_saved_msr;
  322. reset_current_kprobe();
  323. preempt_enable_no_resched();
  324. }
  325. return 0;
  326. }
  327. /*
  328. * Wrapper routine to for handling exceptions.
  329. */
  330. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  331. unsigned long val, void *data)
  332. {
  333. struct die_args *args = (struct die_args *)data;
  334. int ret = NOTIFY_DONE;
  335. switch (val) {
  336. case DIE_BPT:
  337. if (kprobe_handler(args->regs))
  338. ret = NOTIFY_STOP;
  339. break;
  340. case DIE_SSTEP:
  341. if (post_kprobe_handler(args->regs))
  342. ret = NOTIFY_STOP;
  343. break;
  344. case DIE_PAGE_FAULT:
  345. /* kprobe_running() needs smp_processor_id() */
  346. preempt_disable();
  347. if (kprobe_running() &&
  348. kprobe_fault_handler(args->regs, args->trapnr))
  349. ret = NOTIFY_STOP;
  350. preempt_enable();
  351. break;
  352. default:
  353. break;
  354. }
  355. return ret;
  356. }
  357. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  358. {
  359. struct jprobe *jp = container_of(p, struct jprobe, kp);
  360. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  361. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  362. /* setup return addr to the jprobe handler routine */
  363. regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
  364. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  365. return 1;
  366. }
  367. void __kprobes jprobe_return(void)
  368. {
  369. asm volatile("trap" ::: "memory");
  370. }
  371. void __kprobes jprobe_return_end(void)
  372. {
  373. };
  374. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  375. {
  376. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  377. /*
  378. * FIXME - we should ideally be validating that we got here 'cos
  379. * of the "trap" in jprobe_return() above, before restoring the
  380. * saved regs...
  381. */
  382. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  383. preempt_enable_no_resched();
  384. return 1;
  385. }
  386. static struct kprobe trampoline_p = {
  387. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  388. .pre_handler = trampoline_probe_handler
  389. };
  390. int __init arch_init_kprobes(void)
  391. {
  392. return register_kprobe(&trampoline_p);
  393. }