kprobes.c 12 KB

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