kprobes.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /* kprobe_status settings */
  38. #define KPROBE_HIT_ACTIVE 0x00000001
  39. #define KPROBE_HIT_SS 0x00000002
  40. static struct kprobe *current_kprobe;
  41. static unsigned long kprobe_status, kprobe_saved_msr;
  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. return ret;
  55. }
  56. void arch_copy_kprobe(struct kprobe *p)
  57. {
  58. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  59. p->opcode = *p->addr;
  60. }
  61. void 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 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 arch_remove_kprobe(struct kprobe *p)
  74. {
  75. }
  76. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  77. {
  78. regs->msr |= MSR_SE;
  79. /*single step inline if it a breakpoint instruction*/
  80. if (p->opcode == BREAKPOINT_INSTRUCTION)
  81. regs->nip = (unsigned long)p->addr;
  82. else
  83. regs->nip = (unsigned long)&p->ainsn.insn;
  84. }
  85. static inline int kprobe_handler(struct pt_regs *regs)
  86. {
  87. struct kprobe *p;
  88. int ret = 0;
  89. unsigned int *addr = (unsigned int *)regs->nip;
  90. /* Check we're not actually recursing */
  91. if (kprobe_running()) {
  92. /* We *are* holding lock here, so this is safe.
  93. Disarm the probe we just hit, and ignore it. */
  94. p = get_kprobe(addr);
  95. if (p) {
  96. if (kprobe_status == KPROBE_HIT_SS) {
  97. regs->msr &= ~MSR_SE;
  98. regs->msr |= kprobe_saved_msr;
  99. unlock_kprobes();
  100. goto no_kprobe;
  101. }
  102. arch_disarm_kprobe(p);
  103. regs->nip = (unsigned long)p->addr;
  104. ret = 1;
  105. } else {
  106. p = current_kprobe;
  107. if (p->break_handler && p->break_handler(p, regs)) {
  108. goto ss_probe;
  109. }
  110. }
  111. /* If it's not ours, can't be delete race, (we hold lock). */
  112. goto no_kprobe;
  113. }
  114. lock_kprobes();
  115. p = get_kprobe(addr);
  116. if (!p) {
  117. unlock_kprobes();
  118. if (*addr != BREAKPOINT_INSTRUCTION) {
  119. /*
  120. * PowerPC has multiple variants of the "trap"
  121. * instruction. If the current instruction is a
  122. * trap variant, it could belong to someone else
  123. */
  124. kprobe_opcode_t cur_insn = *addr;
  125. if (IS_TW(cur_insn) || IS_TD(cur_insn) ||
  126. IS_TWI(cur_insn) || IS_TDI(cur_insn))
  127. goto no_kprobe;
  128. /*
  129. * The breakpoint instruction was removed right
  130. * after we hit it. Another cpu has removed
  131. * either a probepoint or a debugger breakpoint
  132. * at this address. In either case, no further
  133. * handling of this interrupt is appropriate.
  134. */
  135. ret = 1;
  136. }
  137. /* Not one of ours: let kernel handle it */
  138. goto no_kprobe;
  139. }
  140. kprobe_status = KPROBE_HIT_ACTIVE;
  141. current_kprobe = p;
  142. kprobe_saved_msr = regs->msr;
  143. if (p->pre_handler && p->pre_handler(p, regs))
  144. /* handler has already set things up, so skip ss setup */
  145. return 1;
  146. ss_probe:
  147. prepare_singlestep(p, regs);
  148. kprobe_status = KPROBE_HIT_SS;
  149. /*
  150. * This preempt_disable() matches the preempt_enable_no_resched()
  151. * in post_kprobe_handler().
  152. */
  153. preempt_disable();
  154. return 1;
  155. no_kprobe:
  156. return ret;
  157. }
  158. /*
  159. * Called after single-stepping. p->addr is the address of the
  160. * instruction whose first byte has been replaced by the "breakpoint"
  161. * instruction. To avoid the SMP problems that can occur when we
  162. * temporarily put back the original opcode to single-step, we
  163. * single-stepped a copy of the instruction. The address of this
  164. * copy is p->ainsn.insn.
  165. */
  166. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  167. {
  168. int ret;
  169. regs->nip = (unsigned long)p->addr;
  170. ret = emulate_step(regs, p->ainsn.insn[0]);
  171. if (ret == 0)
  172. regs->nip = (unsigned long)p->addr + 4;
  173. }
  174. static inline int post_kprobe_handler(struct pt_regs *regs)
  175. {
  176. if (!kprobe_running())
  177. return 0;
  178. if (current_kprobe->post_handler)
  179. current_kprobe->post_handler(current_kprobe, regs, 0);
  180. resume_execution(current_kprobe, regs);
  181. regs->msr |= kprobe_saved_msr;
  182. unlock_kprobes();
  183. preempt_enable_no_resched();
  184. /*
  185. * if somebody else is singlestepping across a probe point, msr
  186. * will have SE set, in which case, continue the remaining processing
  187. * of do_debug, as if this is not a probe hit.
  188. */
  189. if (regs->msr & MSR_SE)
  190. return 0;
  191. return 1;
  192. }
  193. /* Interrupts disabled, kprobe_lock held. */
  194. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  195. {
  196. if (current_kprobe->fault_handler
  197. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  198. return 1;
  199. if (kprobe_status & KPROBE_HIT_SS) {
  200. resume_execution(current_kprobe, regs);
  201. regs->msr &= ~MSR_SE;
  202. regs->msr |= kprobe_saved_msr;
  203. unlock_kprobes();
  204. preempt_enable_no_resched();
  205. }
  206. return 0;
  207. }
  208. /*
  209. * Wrapper routine to for handling exceptions.
  210. */
  211. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  212. void *data)
  213. {
  214. struct die_args *args = (struct die_args *)data;
  215. int ret = NOTIFY_DONE;
  216. /*
  217. * Interrupts are not disabled here. We need to disable
  218. * preemption, because kprobe_running() uses smp_processor_id().
  219. */
  220. preempt_disable();
  221. switch (val) {
  222. case DIE_BPT:
  223. if (kprobe_handler(args->regs))
  224. ret = NOTIFY_STOP;
  225. break;
  226. case DIE_SSTEP:
  227. if (post_kprobe_handler(args->regs))
  228. ret = NOTIFY_STOP;
  229. break;
  230. case DIE_GPF:
  231. case DIE_PAGE_FAULT:
  232. if (kprobe_running() &&
  233. kprobe_fault_handler(args->regs, args->trapnr))
  234. ret = NOTIFY_STOP;
  235. break;
  236. default:
  237. break;
  238. }
  239. preempt_enable();
  240. return ret;
  241. }
  242. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  243. {
  244. struct jprobe *jp = container_of(p, struct jprobe, kp);
  245. memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
  246. /* setup return addr to the jprobe handler routine */
  247. regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
  248. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  249. return 1;
  250. }
  251. void jprobe_return(void)
  252. {
  253. asm volatile("trap" ::: "memory");
  254. }
  255. void jprobe_return_end(void)
  256. {
  257. };
  258. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  259. {
  260. /*
  261. * FIXME - we should ideally be validating that we got here 'cos
  262. * of the "trap" in jprobe_return() above, before restoring the
  263. * saved regs...
  264. */
  265. memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
  266. return 1;
  267. }