kmmio.c 16 KB

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