kmmio.c 15 KB

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