kprobes.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. static inline int kprobe_handler(struct pt_regs *regs)
  110. {
  111. struct kprobe *p;
  112. int ret = 0;
  113. unsigned int *addr = (unsigned int *)regs->nip;
  114. /* Check we're not actually recursing */
  115. if (kprobe_running()) {
  116. /* We *are* holding lock here, so this is safe.
  117. Disarm the probe we just hit, and ignore it. */
  118. p = get_kprobe(addr);
  119. if (p) {
  120. if (kprobe_status == KPROBE_HIT_SS) {
  121. regs->msr &= ~MSR_SE;
  122. regs->msr |= kprobe_saved_msr;
  123. unlock_kprobes();
  124. goto no_kprobe;
  125. }
  126. /* We have reentered the kprobe_handler(), since
  127. * another probe was hit while within the handler.
  128. * We here save the original kprobes variables and
  129. * just single step on the instruction of the new probe
  130. * without calling any user handlers.
  131. */
  132. save_previous_kprobe();
  133. current_kprobe = p;
  134. kprobe_saved_msr = regs->msr;
  135. p->nmissed++;
  136. prepare_singlestep(p, regs);
  137. kprobe_status = KPROBE_REENTER;
  138. return 1;
  139. } else {
  140. p = current_kprobe;
  141. if (p->break_handler && p->break_handler(p, regs)) {
  142. goto ss_probe;
  143. }
  144. }
  145. /* If it's not ours, can't be delete race, (we hold lock). */
  146. goto no_kprobe;
  147. }
  148. lock_kprobes();
  149. p = get_kprobe(addr);
  150. if (!p) {
  151. unlock_kprobes();
  152. if (*addr != BREAKPOINT_INSTRUCTION) {
  153. /*
  154. * PowerPC has multiple variants of the "trap"
  155. * instruction. If the current instruction is a
  156. * trap variant, it could belong to someone else
  157. */
  158. kprobe_opcode_t cur_insn = *addr;
  159. if (IS_TW(cur_insn) || IS_TD(cur_insn) ||
  160. IS_TWI(cur_insn) || IS_TDI(cur_insn))
  161. goto no_kprobe;
  162. /*
  163. * The breakpoint instruction was removed right
  164. * after we hit it. Another cpu has removed
  165. * either a probepoint or a debugger breakpoint
  166. * at this address. In either case, no further
  167. * handling of this interrupt is appropriate.
  168. */
  169. ret = 1;
  170. }
  171. /* Not one of ours: let kernel handle it */
  172. goto no_kprobe;
  173. }
  174. kprobe_status = KPROBE_HIT_ACTIVE;
  175. current_kprobe = p;
  176. kprobe_saved_msr = regs->msr;
  177. if (p->pre_handler && p->pre_handler(p, regs))
  178. /* handler has already set things up, so skip ss setup */
  179. return 1;
  180. ss_probe:
  181. prepare_singlestep(p, regs);
  182. kprobe_status = KPROBE_HIT_SS;
  183. /*
  184. * This preempt_disable() matches the preempt_enable_no_resched()
  185. * in post_kprobe_handler().
  186. */
  187. preempt_disable();
  188. return 1;
  189. no_kprobe:
  190. return ret;
  191. }
  192. /*
  193. * Called after single-stepping. p->addr is the address of the
  194. * instruction whose first byte has been replaced by the "breakpoint"
  195. * instruction. To avoid the SMP problems that can occur when we
  196. * temporarily put back the original opcode to single-step, we
  197. * single-stepped a copy of the instruction. The address of this
  198. * copy is p->ainsn.insn.
  199. */
  200. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  201. {
  202. int ret;
  203. unsigned int insn = *p->ainsn.insn;
  204. regs->nip = (unsigned long)p->addr;
  205. ret = emulate_step(regs, insn);
  206. if (ret == 0)
  207. regs->nip = (unsigned long)p->addr + 4;
  208. }
  209. static inline int post_kprobe_handler(struct pt_regs *regs)
  210. {
  211. if (!kprobe_running())
  212. return 0;
  213. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  214. kprobe_status = KPROBE_HIT_SSDONE;
  215. current_kprobe->post_handler(current_kprobe, regs, 0);
  216. }
  217. resume_execution(current_kprobe, regs);
  218. regs->msr |= kprobe_saved_msr;
  219. /*Restore back the original saved kprobes variables and continue. */
  220. if (kprobe_status == KPROBE_REENTER) {
  221. restore_previous_kprobe();
  222. goto out;
  223. }
  224. unlock_kprobes();
  225. out:
  226. preempt_enable_no_resched();
  227. /*
  228. * if somebody else is singlestepping across a probe point, msr
  229. * will have SE set, in which case, continue the remaining processing
  230. * of do_debug, as if this is not a probe hit.
  231. */
  232. if (regs->msr & MSR_SE)
  233. return 0;
  234. return 1;
  235. }
  236. /* Interrupts disabled, kprobe_lock held. */
  237. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  238. {
  239. if (current_kprobe->fault_handler
  240. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  241. return 1;
  242. if (kprobe_status & KPROBE_HIT_SS) {
  243. resume_execution(current_kprobe, regs);
  244. regs->msr &= ~MSR_SE;
  245. regs->msr |= kprobe_saved_msr;
  246. unlock_kprobes();
  247. preempt_enable_no_resched();
  248. }
  249. return 0;
  250. }
  251. /*
  252. * Wrapper routine to for handling exceptions.
  253. */
  254. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  255. void *data)
  256. {
  257. struct die_args *args = (struct die_args *)data;
  258. int ret = NOTIFY_DONE;
  259. /*
  260. * Interrupts are not disabled here. We need to disable
  261. * preemption, because kprobe_running() uses smp_processor_id().
  262. */
  263. preempt_disable();
  264. switch (val) {
  265. case DIE_BPT:
  266. if (kprobe_handler(args->regs))
  267. ret = NOTIFY_STOP;
  268. break;
  269. case DIE_SSTEP:
  270. if (post_kprobe_handler(args->regs))
  271. ret = NOTIFY_STOP;
  272. break;
  273. case DIE_GPF:
  274. case DIE_PAGE_FAULT:
  275. if (kprobe_running() &&
  276. kprobe_fault_handler(args->regs, args->trapnr))
  277. ret = NOTIFY_STOP;
  278. break;
  279. default:
  280. break;
  281. }
  282. preempt_enable();
  283. return ret;
  284. }
  285. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  286. {
  287. struct jprobe *jp = container_of(p, struct jprobe, kp);
  288. memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
  289. /* setup return addr to the jprobe handler routine */
  290. regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
  291. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  292. return 1;
  293. }
  294. void jprobe_return(void)
  295. {
  296. asm volatile("trap" ::: "memory");
  297. }
  298. void jprobe_return_end(void)
  299. {
  300. };
  301. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  302. {
  303. /*
  304. * FIXME - we should ideally be validating that we got here 'cos
  305. * of the "trap" in jprobe_return() above, before restoring the
  306. * saved regs...
  307. */
  308. memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
  309. return 1;
  310. }