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