kmmio.c 16 KB

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