kmmio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/list.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/hash.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/preempt.h>
  16. #include <linux/percpu.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/mutex.h>
  19. #include <asm/io.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/errno.h>
  23. #include <asm/debugreg.h>
  24. #include <linux/mmiotrace.h>
  25. #define KMMIO_PAGE_HASH_BITS 4
  26. #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
  27. struct kmmio_fault_page {
  28. struct list_head list;
  29. struct kmmio_fault_page *release_next;
  30. unsigned long page; /* location of the fault page */
  31. /*
  32. * Number of times this page has been registered as a part
  33. * of a probe. If zero, page is disarmed and this may be freed.
  34. * Used only by writers (RCU).
  35. */
  36. int count;
  37. };
  38. struct kmmio_delayed_release {
  39. struct rcu_head rcu;
  40. struct kmmio_fault_page *release_list;
  41. };
  42. struct kmmio_context {
  43. struct kmmio_fault_page *fpage;
  44. struct kmmio_probe *probe;
  45. unsigned long saved_flags;
  46. unsigned long addr;
  47. int active;
  48. };
  49. static DEFINE_SPINLOCK(kmmio_lock);
  50. /* Protected by kmmio_lock */
  51. unsigned int kmmio_count;
  52. /* Read-protected by RCU, write-protected by kmmio_lock. */
  53. static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
  54. static LIST_HEAD(kmmio_probes);
  55. static struct list_head *kmmio_page_list(unsigned long page)
  56. {
  57. return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
  58. }
  59. /* Accessed per-cpu */
  60. static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
  61. /*
  62. * this is basically a dynamic stabbing problem:
  63. * Could use the existing prio tree code or
  64. * Possible better implementations:
  65. * The Interval Skip List: A Data Structure for Finding All Intervals That
  66. * Overlap a Point (might be simple)
  67. * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
  68. */
  69. /* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
  70. static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
  71. {
  72. struct kmmio_probe *p;
  73. list_for_each_entry_rcu(p, &kmmio_probes, list) {
  74. if (addr >= p->addr && addr <= (p->addr + p->len))
  75. return p;
  76. }
  77. return NULL;
  78. }
  79. /* You must be holding RCU read lock. */
  80. static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
  81. {
  82. struct list_head *head;
  83. struct kmmio_fault_page *p;
  84. page &= PAGE_MASK;
  85. head = kmmio_page_list(page);
  86. list_for_each_entry_rcu(p, head, list) {
  87. if (p->page == page)
  88. return p;
  89. }
  90. return NULL;
  91. }
  92. static void set_page_present(unsigned long addr, bool present, int *pglevel)
  93. {
  94. pteval_t pteval;
  95. pmdval_t pmdval;
  96. int level;
  97. pmd_t *pmd;
  98. pte_t *pte = lookup_address(addr, &level);
  99. if (!pte) {
  100. pr_err("kmmio: no pte for page 0x%08lx\n", addr);
  101. return;
  102. }
  103. if (pglevel)
  104. *pglevel = level;
  105. switch (level) {
  106. case PG_LEVEL_2M:
  107. pmd = (pmd_t *)pte;
  108. pmdval = pmd_val(*pmd) & ~_PAGE_PRESENT;
  109. if (present)
  110. pmdval |= _PAGE_PRESENT;
  111. set_pmd(pmd, __pmd(pmdval));
  112. break;
  113. case PG_LEVEL_4K:
  114. pteval = pte_val(*pte) & ~_PAGE_PRESENT;
  115. if (present)
  116. pteval |= _PAGE_PRESENT;
  117. set_pte_atomic(pte, __pte(pteval));
  118. break;
  119. default:
  120. pr_err("kmmio: unexpected page level 0x%x.\n", level);
  121. return;
  122. }
  123. __flush_tlb_one(addr);
  124. }
  125. /** Mark the given page as not present. Access to it will trigger a fault. */
  126. static void arm_kmmio_fault_page(unsigned long page, int *page_level)
  127. {
  128. set_page_present(page & PAGE_MASK, false, page_level);
  129. }
  130. /** Mark the given page as present. */
  131. static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
  132. {
  133. set_page_present(page & PAGE_MASK, true, page_level);
  134. }
  135. /*
  136. * This is being called from do_page_fault().
  137. *
  138. * We may be in an interrupt or a critical section. Also prefecthing may
  139. * trigger a page fault. We may be in the middle of process switch.
  140. * We cannot take any locks, because we could be executing especially
  141. * within a kmmio critical section.
  142. *
  143. * Local interrupts are disabled, so preemption cannot happen.
  144. * Do not enable interrupts, do not sleep, and watch out for other CPUs.
  145. */
  146. /*
  147. * Interrupts are disabled on entry as trap3 is an interrupt gate
  148. * and they remain disabled thorough out this function.
  149. */
  150. int kmmio_handler(struct pt_regs *regs, unsigned long addr)
  151. {
  152. struct kmmio_context *ctx;
  153. struct kmmio_fault_page *faultpage;
  154. int ret = 0; /* default to fault not handled */
  155. /*
  156. * Preemption is now disabled to prevent process switch during
  157. * single stepping. We can only handle one active kmmio trace
  158. * per cpu, so ensure that we finish it before something else
  159. * gets to run. We also hold the RCU read lock over single
  160. * stepping to avoid looking up the probe and kmmio_fault_page
  161. * again.
  162. */
  163. preempt_disable();
  164. rcu_read_lock();
  165. faultpage = get_kmmio_fault_page(addr);
  166. if (!faultpage) {
  167. /*
  168. * Either this page fault is not caused by kmmio, or
  169. * another CPU just pulled the kmmio probe from under
  170. * our feet. The latter case should not be possible.
  171. */
  172. goto no_kmmio;
  173. }
  174. ctx = &get_cpu_var(kmmio_ctx);
  175. if (ctx->active) {
  176. disarm_kmmio_fault_page(faultpage->page, NULL);
  177. if (addr == ctx->addr) {
  178. /*
  179. * On SMP we sometimes get recursive probe hits on the
  180. * same address. Context is already saved, fall out.
  181. */
  182. pr_debug("kmmio: duplicate probe hit on CPU %d, for "
  183. "address 0x%08lx.\n",
  184. smp_processor_id(), addr);
  185. ret = 1;
  186. goto no_kmmio_ctx;
  187. }
  188. /*
  189. * Prevent overwriting already in-flight context.
  190. * This should not happen, let's hope disarming at least
  191. * prevents a panic.
  192. */
  193. pr_emerg("kmmio: recursive probe hit on CPU %d, "
  194. "for address 0x%08lx. Ignoring.\n",
  195. smp_processor_id(), addr);
  196. pr_emerg("kmmio: previous hit was at 0x%08lx.\n",
  197. ctx->addr);
  198. goto no_kmmio_ctx;
  199. }
  200. ctx->active++;
  201. ctx->fpage = faultpage;
  202. ctx->probe = get_kmmio_probe(addr);
  203. ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  204. ctx->addr = addr;
  205. if (ctx->probe && ctx->probe->pre_handler)
  206. ctx->probe->pre_handler(ctx->probe, regs, addr);
  207. /*
  208. * Enable single-stepping and disable interrupts for the faulting
  209. * context. Local interrupts must not get enabled during stepping.
  210. */
  211. regs->flags |= X86_EFLAGS_TF;
  212. regs->flags &= ~X86_EFLAGS_IF;
  213. /* Now we set present bit in PTE and single step. */
  214. disarm_kmmio_fault_page(ctx->fpage->page, NULL);
  215. /*
  216. * If another cpu accesses the same page while we are stepping,
  217. * the access will not be caught. It will simply succeed and the
  218. * only downside is we lose the event. If this becomes a problem,
  219. * the user should drop to single cpu before tracing.
  220. */
  221. put_cpu_var(kmmio_ctx);
  222. return 1; /* fault handled */
  223. no_kmmio_ctx:
  224. put_cpu_var(kmmio_ctx);
  225. no_kmmio:
  226. rcu_read_unlock();
  227. preempt_enable_no_resched();
  228. return ret;
  229. }
  230. /*
  231. * Interrupts are disabled on entry as trap1 is an interrupt gate
  232. * and they remain disabled thorough out this function.
  233. * This must always get called as the pair to kmmio_handler().
  234. */
  235. static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
  236. {
  237. int ret = 0;
  238. struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
  239. if (!ctx->active) {
  240. pr_debug("kmmio: spurious debug trap on CPU %d.\n",
  241. smp_processor_id());
  242. goto out;
  243. }
  244. if (ctx->probe && ctx->probe->post_handler)
  245. ctx->probe->post_handler(ctx->probe, condition, regs);
  246. arm_kmmio_fault_page(ctx->fpage->page, NULL);
  247. regs->flags &= ~X86_EFLAGS_TF;
  248. regs->flags |= ctx->saved_flags;
  249. /* These were acquired in kmmio_handler(). */
  250. ctx->active--;
  251. BUG_ON(ctx->active);
  252. rcu_read_unlock();
  253. preempt_enable_no_resched();
  254. /*
  255. * if somebody else is singlestepping across a probe point, flags
  256. * will have TF set, in which case, continue the remaining processing
  257. * of do_debug, as if this is not a probe hit.
  258. */
  259. if (!(regs->flags & X86_EFLAGS_TF))
  260. ret = 1;
  261. out:
  262. put_cpu_var(kmmio_ctx);
  263. return ret;
  264. }
  265. /* You must be holding kmmio_lock. */
  266. static int add_kmmio_fault_page(unsigned long page)
  267. {
  268. struct kmmio_fault_page *f;
  269. page &= PAGE_MASK;
  270. f = get_kmmio_fault_page(page);
  271. if (f) {
  272. if (!f->count)
  273. arm_kmmio_fault_page(f->page, NULL);
  274. f->count++;
  275. return 0;
  276. }
  277. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  278. if (!f)
  279. return -1;
  280. f->count = 1;
  281. f->page = page;
  282. list_add_rcu(&f->list, kmmio_page_list(f->page));
  283. arm_kmmio_fault_page(f->page, NULL);
  284. return 0;
  285. }
  286. /* You must be holding kmmio_lock. */
  287. static void release_kmmio_fault_page(unsigned long page,
  288. struct kmmio_fault_page **release_list)
  289. {
  290. struct kmmio_fault_page *f;
  291. page &= PAGE_MASK;
  292. f = get_kmmio_fault_page(page);
  293. if (!f)
  294. return;
  295. f->count--;
  296. BUG_ON(f->count < 0);
  297. if (!f->count) {
  298. disarm_kmmio_fault_page(f->page, NULL);
  299. f->release_next = *release_list;
  300. *release_list = f;
  301. }
  302. }
  303. int register_kmmio_probe(struct kmmio_probe *p)
  304. {
  305. unsigned long flags;
  306. int ret = 0;
  307. unsigned long size = 0;
  308. spin_lock_irqsave(&kmmio_lock, flags);
  309. if (get_kmmio_probe(p->addr)) {
  310. ret = -EEXIST;
  311. goto out;
  312. }
  313. kmmio_count++;
  314. list_add_rcu(&p->list, &kmmio_probes);
  315. while (size < p->len) {
  316. if (add_kmmio_fault_page(p->addr + size))
  317. pr_err("kmmio: Unable to set page fault.\n");
  318. size += PAGE_SIZE;
  319. }
  320. out:
  321. spin_unlock_irqrestore(&kmmio_lock, flags);
  322. /*
  323. * XXX: What should I do here?
  324. * Here was a call to global_flush_tlb(), but it does not exist
  325. * anymore. It seems it's not needed after all.
  326. */
  327. return ret;
  328. }
  329. EXPORT_SYMBOL(register_kmmio_probe);
  330. static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
  331. {
  332. struct kmmio_delayed_release *dr = container_of(
  333. head,
  334. struct kmmio_delayed_release,
  335. rcu);
  336. struct kmmio_fault_page *p = dr->release_list;
  337. while (p) {
  338. struct kmmio_fault_page *next = p->release_next;
  339. BUG_ON(p->count);
  340. kfree(p);
  341. p = next;
  342. }
  343. kfree(dr);
  344. }
  345. static void remove_kmmio_fault_pages(struct rcu_head *head)
  346. {
  347. struct kmmio_delayed_release *dr = container_of(
  348. head,
  349. struct kmmio_delayed_release,
  350. rcu);
  351. struct kmmio_fault_page *p = dr->release_list;
  352. struct kmmio_fault_page **prevp = &dr->release_list;
  353. unsigned long flags;
  354. spin_lock_irqsave(&kmmio_lock, flags);
  355. while (p) {
  356. if (!p->count)
  357. list_del_rcu(&p->list);
  358. else
  359. *prevp = p->release_next;
  360. prevp = &p->release_next;
  361. p = p->release_next;
  362. }
  363. spin_unlock_irqrestore(&kmmio_lock, flags);
  364. /* This is the real RCU destroy call. */
  365. call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
  366. }
  367. /*
  368. * Remove a kmmio probe. You have to synchronize_rcu() before you can be
  369. * sure that the callbacks will not be called anymore. Only after that
  370. * you may actually release your struct kmmio_probe.
  371. *
  372. * Unregistering a kmmio fault page has three steps:
  373. * 1. release_kmmio_fault_page()
  374. * Disarm the page, wait a grace period to let all faults finish.
  375. * 2. remove_kmmio_fault_pages()
  376. * Remove the pages from kmmio_page_table.
  377. * 3. rcu_free_kmmio_fault_pages()
  378. * Actally free the kmmio_fault_page structs as with RCU.
  379. */
  380. void unregister_kmmio_probe(struct kmmio_probe *p)
  381. {
  382. unsigned long flags;
  383. unsigned long size = 0;
  384. struct kmmio_fault_page *release_list = NULL;
  385. struct kmmio_delayed_release *drelease;
  386. spin_lock_irqsave(&kmmio_lock, flags);
  387. while (size < p->len) {
  388. release_kmmio_fault_page(p->addr + size, &release_list);
  389. size += PAGE_SIZE;
  390. }
  391. list_del_rcu(&p->list);
  392. kmmio_count--;
  393. spin_unlock_irqrestore(&kmmio_lock, flags);
  394. drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
  395. if (!drelease) {
  396. pr_crit("kmmio: leaking kmmio_fault_page objects.\n");
  397. return;
  398. }
  399. drelease->release_list = release_list;
  400. /*
  401. * This is not really RCU here. We have just disarmed a set of
  402. * pages so that they cannot trigger page faults anymore. However,
  403. * we cannot remove the pages from kmmio_page_table,
  404. * because a probe hit might be in flight on another CPU. The
  405. * pages are collected into a list, and they will be removed from
  406. * kmmio_page_table when it is certain that no probe hit related to
  407. * these pages can be in flight. RCU grace period sounds like a
  408. * good choice.
  409. *
  410. * If we removed the pages too early, kmmio page fault handler might
  411. * not find the respective kmmio_fault_page and determine it's not
  412. * a kmmio fault, when it actually is. This would lead to madness.
  413. */
  414. call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
  415. }
  416. EXPORT_SYMBOL(unregister_kmmio_probe);
  417. static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
  418. void *args)
  419. {
  420. struct die_args *arg = args;
  421. if (val == DIE_DEBUG && (arg->err & DR_STEP))
  422. if (post_kmmio_handler(arg->err, arg->regs) == 1)
  423. return NOTIFY_STOP;
  424. return NOTIFY_DONE;
  425. }
  426. static struct notifier_block nb_die = {
  427. .notifier_call = kmmio_die_notifier
  428. };
  429. static int __init init_kmmio(void)
  430. {
  431. int i;
  432. for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
  433. INIT_LIST_HEAD(&kmmio_page_table[i]);
  434. return register_die_notifier(&nb_die);
  435. }
  436. fs_initcall(init_kmmio); /* should be before device_initcall() */