kmmio.c 16 KB

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