kprobes.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Kernel Probes (KProbes)
  3. * 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 suggestions from
  23. * Rusty Russell).
  24. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  25. * hlists and exceptions notifier as suggested by Andi Kleen.
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  29. * exceptions notifier to be first on the priority list.
  30. */
  31. #include <linux/kprobes.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/hash.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/errno.h>
  38. #include <asm/kdebug.h>
  39. #define KPROBE_HASH_BITS 6
  40. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  41. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  42. unsigned int kprobe_cpu = NR_CPUS;
  43. static DEFINE_SPINLOCK(kprobe_lock);
  44. static struct kprobe *curr_kprobe;
  45. /* Locks kprobe: irqs must be disabled */
  46. void lock_kprobes(void)
  47. {
  48. spin_lock(&kprobe_lock);
  49. kprobe_cpu = smp_processor_id();
  50. }
  51. void unlock_kprobes(void)
  52. {
  53. kprobe_cpu = NR_CPUS;
  54. spin_unlock(&kprobe_lock);
  55. }
  56. /* You have to be holding the kprobe_lock */
  57. struct kprobe *get_kprobe(void *addr)
  58. {
  59. struct hlist_head *head;
  60. struct hlist_node *node;
  61. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  62. hlist_for_each(node, head) {
  63. struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
  64. if (p->addr == addr)
  65. return p;
  66. }
  67. return NULL;
  68. }
  69. /*
  70. * Aggregate handlers for multiple kprobes support - these handlers
  71. * take care of invoking the individual kprobe handlers on p->list
  72. */
  73. int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  74. {
  75. struct kprobe *kp;
  76. list_for_each_entry(kp, &p->list, list) {
  77. if (kp->pre_handler) {
  78. curr_kprobe = kp;
  79. kp->pre_handler(kp, regs);
  80. curr_kprobe = NULL;
  81. }
  82. }
  83. return 0;
  84. }
  85. void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  86. unsigned long flags)
  87. {
  88. struct kprobe *kp;
  89. list_for_each_entry(kp, &p->list, list) {
  90. if (kp->post_handler) {
  91. curr_kprobe = kp;
  92. kp->post_handler(kp, regs, flags);
  93. curr_kprobe = NULL;
  94. }
  95. }
  96. return;
  97. }
  98. int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
  99. {
  100. /*
  101. * if we faulted "during" the execution of a user specified
  102. * probe handler, invoke just that probe's fault handler
  103. */
  104. if (curr_kprobe && curr_kprobe->fault_handler) {
  105. if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. /*
  111. * Fill in the required fields of the "manager kprobe". Replace the
  112. * earlier kprobe in the hlist with the manager kprobe
  113. */
  114. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  115. {
  116. ap->addr = p->addr;
  117. ap->opcode = p->opcode;
  118. memcpy(&ap->ainsn, &p->ainsn, sizeof(struct arch_specific_insn));
  119. ap->pre_handler = aggr_pre_handler;
  120. ap->post_handler = aggr_post_handler;
  121. ap->fault_handler = aggr_fault_handler;
  122. INIT_LIST_HEAD(&ap->list);
  123. list_add(&p->list, &ap->list);
  124. INIT_HLIST_NODE(&ap->hlist);
  125. hlist_del(&p->hlist);
  126. hlist_add_head(&ap->hlist,
  127. &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
  128. }
  129. /*
  130. * This is the second or subsequent kprobe at the address - handle
  131. * the intricacies
  132. * TODO: Move kcalloc outside the spinlock
  133. */
  134. static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
  135. {
  136. int ret = 0;
  137. struct kprobe *ap;
  138. if (old_p->break_handler || p->break_handler) {
  139. ret = -EEXIST; /* kprobe and jprobe can't (yet) coexist */
  140. } else if (old_p->pre_handler == aggr_pre_handler) {
  141. list_add(&p->list, &old_p->list);
  142. } else {
  143. ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
  144. if (!ap)
  145. return -ENOMEM;
  146. add_aggr_kprobe(ap, old_p);
  147. list_add(&p->list, &ap->list);
  148. }
  149. return ret;
  150. }
  151. /* kprobe removal house-keeping routines */
  152. static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
  153. {
  154. *p->addr = p->opcode;
  155. hlist_del(&p->hlist);
  156. flush_icache_range((unsigned long) p->addr,
  157. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  158. spin_unlock_irqrestore(&kprobe_lock, flags);
  159. arch_remove_kprobe(p);
  160. }
  161. static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
  162. struct kprobe *p, unsigned long flags)
  163. {
  164. list_del(&p->list);
  165. if (list_empty(&old_p->list)) {
  166. cleanup_kprobe(old_p, flags);
  167. kfree(old_p);
  168. } else
  169. spin_unlock_irqrestore(&kprobe_lock, flags);
  170. }
  171. int register_kprobe(struct kprobe *p)
  172. {
  173. int ret = 0;
  174. unsigned long flags = 0;
  175. struct kprobe *old_p;
  176. if ((ret = arch_prepare_kprobe(p)) != 0) {
  177. goto rm_kprobe;
  178. }
  179. spin_lock_irqsave(&kprobe_lock, flags);
  180. old_p = get_kprobe(p->addr);
  181. if (old_p) {
  182. ret = register_aggr_kprobe(old_p, p);
  183. goto out;
  184. }
  185. arch_copy_kprobe(p);
  186. INIT_HLIST_NODE(&p->hlist);
  187. hlist_add_head(&p->hlist,
  188. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  189. p->opcode = *p->addr;
  190. *p->addr = BREAKPOINT_INSTRUCTION;
  191. flush_icache_range((unsigned long) p->addr,
  192. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  193. out:
  194. spin_unlock_irqrestore(&kprobe_lock, flags);
  195. rm_kprobe:
  196. if (ret == -EEXIST)
  197. arch_remove_kprobe(p);
  198. return ret;
  199. }
  200. void unregister_kprobe(struct kprobe *p)
  201. {
  202. unsigned long flags;
  203. struct kprobe *old_p;
  204. spin_lock_irqsave(&kprobe_lock, flags);
  205. old_p = get_kprobe(p->addr);
  206. if (old_p) {
  207. if (old_p->pre_handler == aggr_pre_handler)
  208. cleanup_aggr_kprobe(old_p, p, flags);
  209. else
  210. cleanup_kprobe(p, flags);
  211. } else
  212. spin_unlock_irqrestore(&kprobe_lock, flags);
  213. }
  214. static struct notifier_block kprobe_exceptions_nb = {
  215. .notifier_call = kprobe_exceptions_notify,
  216. .priority = 0x7fffffff /* we need to notified first */
  217. };
  218. int register_jprobe(struct jprobe *jp)
  219. {
  220. /* Todo: Verify probepoint is a function entry point */
  221. jp->kp.pre_handler = setjmp_pre_handler;
  222. jp->kp.break_handler = longjmp_break_handler;
  223. return register_kprobe(&jp->kp);
  224. }
  225. void unregister_jprobe(struct jprobe *jp)
  226. {
  227. unregister_kprobe(&jp->kp);
  228. }
  229. static int __init init_kprobes(void)
  230. {
  231. int i, err = 0;
  232. /* FIXME allocate the probe table, currently defined statically */
  233. /* initialize all list heads */
  234. for (i = 0; i < KPROBE_TABLE_SIZE; i++)
  235. INIT_HLIST_HEAD(&kprobe_table[i]);
  236. err = register_die_notifier(&kprobe_exceptions_nb);
  237. return err;
  238. }
  239. __initcall(init_kprobes);
  240. EXPORT_SYMBOL_GPL(register_kprobe);
  241. EXPORT_SYMBOL_GPL(unregister_kprobe);
  242. EXPORT_SYMBOL_GPL(register_jprobe);
  243. EXPORT_SYMBOL_GPL(unregister_jprobe);
  244. EXPORT_SYMBOL_GPL(jprobe_return);