kmmio.c 15 KB

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