kprobes.c 12 KB

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