kmmio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Support for MMIO probes.
  2. * Benfit many code from kprobes
  3. * (C) 2002 Louis Zhuang <louis.zhuang@intel.com>.
  4. * 2007 Alexander Eichner
  5. * 2008 Pekka Paalanen <pq@iki.fi>
  6. */
  7. #include <linux/version.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/hash.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/preempt.h>
  18. #include <asm/io.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/errno.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/pgtable.h>
  23. #include "kmmio.h"
  24. #define KMMIO_HASH_BITS 6
  25. #define KMMIO_TABLE_SIZE (1 << KMMIO_HASH_BITS)
  26. #define KMMIO_PAGE_HASH_BITS 4
  27. #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
  28. struct kmmio_context {
  29. struct kmmio_fault_page *fpage;
  30. struct kmmio_probe *probe;
  31. unsigned long saved_flags;
  32. int active;
  33. };
  34. static int kmmio_page_fault(struct pt_regs *regs, unsigned long error_code,
  35. unsigned long address);
  36. static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
  37. void *args);
  38. static DEFINE_SPINLOCK(kmmio_lock);
  39. /* These are protected by kmmio_lock */
  40. unsigned int kmmio_count;
  41. static unsigned int handler_registered;
  42. static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
  43. static LIST_HEAD(kmmio_probes);
  44. static struct kmmio_context kmmio_ctx[NR_CPUS];
  45. static struct notifier_block nb_die = {
  46. .notifier_call = kmmio_die_notifier
  47. };
  48. int init_kmmio(void)
  49. {
  50. int i;
  51. for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
  52. INIT_LIST_HEAD(&kmmio_page_table[i]);
  53. register_die_notifier(&nb_die);
  54. return 0;
  55. }
  56. void cleanup_kmmio(void)
  57. {
  58. /*
  59. * Assume the following have been already cleaned by calling
  60. * unregister_kmmio_probe() appropriately:
  61. * kmmio_page_table, kmmio_probes
  62. */
  63. if (handler_registered) {
  64. if (mmiotrace_unregister_pf(&kmmio_page_fault))
  65. BUG();
  66. synchronize_rcu();
  67. }
  68. unregister_die_notifier(&nb_die);
  69. }
  70. /*
  71. * this is basically a dynamic stabbing problem:
  72. * Could use the existing prio tree code or
  73. * Possible better implementations:
  74. * The Interval Skip List: A Data Structure for Finding All Intervals That
  75. * Overlap a Point (might be simple)
  76. * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
  77. */
  78. /* Get the kmmio at this addr (if any). You must be holding kmmio_lock. */
  79. static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
  80. {
  81. struct kmmio_probe *p;
  82. list_for_each_entry(p, &kmmio_probes, list) {
  83. if (addr >= p->addr && addr <= (p->addr + p->len))
  84. return p;
  85. }
  86. return NULL;
  87. }
  88. static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
  89. {
  90. struct list_head *head, *tmp;
  91. page &= PAGE_MASK;
  92. head = &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
  93. list_for_each(tmp, head) {
  94. struct kmmio_fault_page *p
  95. = list_entry(tmp, struct kmmio_fault_page, list);
  96. if (p->page == page)
  97. return p;
  98. }
  99. return NULL;
  100. }
  101. static void arm_kmmio_fault_page(unsigned long page, int *page_level)
  102. {
  103. unsigned long address = page & PAGE_MASK;
  104. int level;
  105. pte_t *pte = lookup_address(address, &level);
  106. if (!pte) {
  107. printk(KERN_ERR "Error in %s: no pte for page 0x%08lx\n",
  108. __FUNCTION__, page);
  109. return;
  110. }
  111. if (level == PG_LEVEL_2M) {
  112. pmd_t *pmd = (pmd_t *)pte;
  113. set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_PRESENT));
  114. } else {
  115. /* PG_LEVEL_4K */
  116. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
  117. }
  118. if (page_level)
  119. *page_level = level;
  120. __flush_tlb_one(page);
  121. }
  122. static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
  123. {
  124. unsigned long address = page & PAGE_MASK;
  125. int level;
  126. pte_t *pte = lookup_address(address, &level);
  127. if (!pte) {
  128. printk(KERN_ERR "Error in %s: no pte for page 0x%08lx\n",
  129. __FUNCTION__, page);
  130. return;
  131. }
  132. if (level == PG_LEVEL_2M) {
  133. pmd_t *pmd = (pmd_t *)pte;
  134. set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_PRESENT));
  135. } else {
  136. /* PG_LEVEL_4K */
  137. set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
  138. }
  139. if (page_level)
  140. *page_level = level;
  141. __flush_tlb_one(page);
  142. }
  143. /*
  144. * Interrupts are disabled on entry as trap3 is an interrupt gate
  145. * and they remain disabled thorough out this function.
  146. */
  147. static int kmmio_handler(struct pt_regs *regs, unsigned long addr)
  148. {
  149. struct kmmio_context *ctx;
  150. int cpu;
  151. /*
  152. * Preemption is now disabled to prevent process switch during
  153. * single stepping. We can only handle one active kmmio trace
  154. * per cpu, so ensure that we finish it before something else
  155. * gets to run.
  156. *
  157. * XXX what if an interrupt occurs between returning from
  158. * do_page_fault() and entering the single-step exception handler?
  159. * And that interrupt triggers a kmmio trap?
  160. */
  161. preempt_disable();
  162. cpu = smp_processor_id();
  163. ctx = &kmmio_ctx[cpu];
  164. /* interrupts disabled and CPU-local data => atomicity guaranteed. */
  165. if (ctx->active) {
  166. /*
  167. * This avoids a deadlock with kmmio_lock.
  168. * If this page fault really was due to kmmio trap,
  169. * all hell breaks loose.
  170. */
  171. printk(KERN_EMERG "mmiotrace: recursive probe hit on CPU %d, "
  172. "for address %lu. Ignoring.\n",
  173. cpu, addr);
  174. goto no_kmmio;
  175. }
  176. ctx->active++;
  177. /*
  178. * Acquire the kmmio lock to prevent changes affecting
  179. * get_kmmio_fault_page() and get_kmmio_probe(), since we save their
  180. * returned pointers.
  181. * The lock is released in post_kmmio_handler().
  182. * XXX: could/should get_kmmio_*() be using RCU instead of spinlock?
  183. */
  184. spin_lock(&kmmio_lock);
  185. ctx->fpage = get_kmmio_fault_page(addr);
  186. if (!ctx->fpage) {
  187. /* this page fault is not caused by kmmio */
  188. goto no_kmmio_locked;
  189. }
  190. ctx->probe = get_kmmio_probe(addr);
  191. ctx->saved_flags = (regs->flags & (TF_MASK|IF_MASK));
  192. if (ctx->probe && ctx->probe->pre_handler)
  193. ctx->probe->pre_handler(ctx->probe, regs, addr);
  194. regs->flags |= TF_MASK;
  195. regs->flags &= ~IF_MASK;
  196. /* We hold lock, now we set present bit in PTE and single step. */
  197. disarm_kmmio_fault_page(ctx->fpage->page, NULL);
  198. return 1;
  199. no_kmmio_locked:
  200. spin_unlock(&kmmio_lock);
  201. ctx->active--;
  202. no_kmmio:
  203. preempt_enable_no_resched();
  204. /* page fault not handled by kmmio */
  205. return 0;
  206. }
  207. /*
  208. * Interrupts are disabled on entry as trap1 is an interrupt gate
  209. * and they remain disabled thorough out this function.
  210. * And we hold kmmio lock.
  211. */
  212. static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
  213. {
  214. int cpu = smp_processor_id();
  215. struct kmmio_context *ctx = &kmmio_ctx[cpu];
  216. if (!ctx->active)
  217. return 0;
  218. if (ctx->probe && ctx->probe->post_handler)
  219. ctx->probe->post_handler(ctx->probe, condition, regs);
  220. arm_kmmio_fault_page(ctx->fpage->page, NULL);
  221. regs->flags &= ~TF_MASK;
  222. regs->flags |= ctx->saved_flags;
  223. /* These were acquired in kmmio_handler(). */
  224. ctx->active--;
  225. spin_unlock(&kmmio_lock);
  226. preempt_enable_no_resched();
  227. /*
  228. * if somebody else is singlestepping across a probe point, flags
  229. * will have TF set, in which case, continue the remaining processing
  230. * of do_debug, as if this is not a probe hit.
  231. */
  232. if (regs->flags & TF_MASK)
  233. return 0;
  234. return 1;
  235. }
  236. static int add_kmmio_fault_page(unsigned long page)
  237. {
  238. struct kmmio_fault_page *f;
  239. page &= PAGE_MASK;
  240. f = get_kmmio_fault_page(page);
  241. if (f) {
  242. f->count++;
  243. return 0;
  244. }
  245. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  246. if (!f)
  247. return -1;
  248. f->count = 1;
  249. f->page = page;
  250. list_add(&f->list,
  251. &kmmio_page_table[hash_long(f->page, KMMIO_PAGE_HASH_BITS)]);
  252. arm_kmmio_fault_page(f->page, NULL);
  253. return 0;
  254. }
  255. static void release_kmmio_fault_page(unsigned long page)
  256. {
  257. struct kmmio_fault_page *f;
  258. page &= PAGE_MASK;
  259. f = get_kmmio_fault_page(page);
  260. if (!f)
  261. return;
  262. f->count--;
  263. if (!f->count) {
  264. disarm_kmmio_fault_page(f->page, NULL);
  265. list_del(&f->list);
  266. }
  267. }
  268. int register_kmmio_probe(struct kmmio_probe *p)
  269. {
  270. int ret = 0;
  271. unsigned long size = 0;
  272. spin_lock_irq(&kmmio_lock);
  273. kmmio_count++;
  274. if (get_kmmio_probe(p->addr)) {
  275. ret = -EEXIST;
  276. goto out;
  277. }
  278. list_add(&p->list, &kmmio_probes);
  279. /*printk("adding fault pages...\n");*/
  280. while (size < p->len) {
  281. if (add_kmmio_fault_page(p->addr + size))
  282. printk(KERN_ERR "mmio: Unable to set page fault.\n");
  283. size += PAGE_SIZE;
  284. }
  285. if (!handler_registered) {
  286. if (mmiotrace_register_pf(&kmmio_page_fault))
  287. printk(KERN_ERR "mmiotrace: Cannot register page "
  288. "fault handler.\n");
  289. else
  290. handler_registered++;
  291. }
  292. out:
  293. spin_unlock_irq(&kmmio_lock);
  294. /*
  295. * XXX: What should I do here?
  296. * Here was a call to global_flush_tlb(), but it does not exist
  297. * anymore.
  298. */
  299. return ret;
  300. }
  301. void unregister_kmmio_probe(struct kmmio_probe *p)
  302. {
  303. unsigned long size = 0;
  304. spin_lock_irq(&kmmio_lock);
  305. while (size < p->len) {
  306. release_kmmio_fault_page(p->addr + size);
  307. size += PAGE_SIZE;
  308. }
  309. list_del(&p->list);
  310. kmmio_count--;
  311. spin_unlock_irq(&kmmio_lock);
  312. }
  313. /*
  314. * According to 2.6.20, mainly x86_64 arch:
  315. * This is being called from do_page_fault(), via the page fault notifier
  316. * chain. The chain is called for both user space faults and kernel space
  317. * faults (address >= TASK_SIZE64), except not on faults serviced by
  318. * vmalloc_fault().
  319. *
  320. * We may be in an interrupt or a critical section. Also prefecthing may
  321. * trigger a page fault. We may be in the middle of process switch.
  322. * The page fault hook functionality has put us inside RCU read lock.
  323. *
  324. * Local interrupts are disabled, so preemption cannot happen.
  325. * Do not enable interrupts, do not sleep, and watch out for other CPUs.
  326. */
  327. static int kmmio_page_fault(struct pt_regs *regs, unsigned long error_code,
  328. unsigned long address)
  329. {
  330. if (is_kmmio_active())
  331. if (kmmio_handler(regs, address) == 1)
  332. return -1;
  333. return 0;
  334. }
  335. static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
  336. void *args)
  337. {
  338. struct die_args *arg = args;
  339. if (val == DIE_DEBUG)
  340. if (post_kmmio_handler(arg->err, arg->regs) == 1)
  341. return NOTIFY_STOP;
  342. return NOTIFY_DONE;
  343. }