kmmio.c 16 KB

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