kmmio.c 16 KB

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