kprobes.c 12 KB

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