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>
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>
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>
28 #define KMMIO_PAGE_HASH_BITS 4
29 #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
31 struct kmmio_fault_page {
32 struct list_head list;
33 struct kmmio_fault_page *release_next;
34 unsigned long page; /* location of the fault page */
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).
44 struct kmmio_delayed_release {
46 struct kmmio_fault_page *release_list;
49 struct kmmio_context {
50 struct kmmio_fault_page *fpage;
51 struct kmmio_probe *probe;
52 unsigned long saved_flags;
57 static DEFINE_SPINLOCK(kmmio_lock);
59 /* Protected by kmmio_lock */
60 unsigned int kmmio_count;
62 /* Read-protected by RCU, write-protected by kmmio_lock. */
63 static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
64 static LIST_HEAD(kmmio_probes);
66 static struct list_head *kmmio_page_list(unsigned long page)
68 return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
71 /* Accessed per-cpu */
72 static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
75 * this is basically a dynamic stabbing problem:
76 * Could use the existing prio tree code or
77 * Possible better implementations:
78 * The Interval Skip List: A Data Structure for Finding All Intervals That
79 * Overlap a Point (might be simple)
80 * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
82 /* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
83 static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
85 struct kmmio_probe *p;
86 list_for_each_entry_rcu(p, &kmmio_probes, list) {
87 if (addr >= p->addr && addr <= (p->addr + p->len))
93 /* You must be holding RCU read lock. */
94 static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
96 struct list_head *head;
97 struct kmmio_fault_page *p;
100 head = kmmio_page_list(page);
101 list_for_each_entry_rcu(p, head, list) {
108 static int set_page_present(unsigned long addr, bool present,
109 unsigned int *pglevel)
115 pte_t *pte = lookup_address(addr, &level);
118 pr_err("kmmio: no pte for page 0x%08lx\n", addr);
128 pmdval = pmd_val(*pmd) & ~_PAGE_PRESENT;
130 pmdval |= _PAGE_PRESENT;
131 set_pmd(pmd, __pmd(pmdval));
135 pteval = pte_val(*pte) & ~_PAGE_PRESENT;
137 pteval |= _PAGE_PRESENT;
138 set_pte_atomic(pte, __pte(pteval));
142 pr_err("kmmio: unexpected page level 0x%x.\n", level);
146 __flush_tlb_one(addr);
151 /** Mark the given page as not present. Access to it will trigger a fault. */
152 static int arm_kmmio_fault_page(unsigned long page, unsigned int *pglevel)
154 int ret = set_page_present(page & PAGE_MASK, false, pglevel);
155 WARN_ONCE(ret < 0, KERN_ERR "kmmio arming 0x%08lx failed.\n", page);
159 /** Mark the given page as present. */
160 static void disarm_kmmio_fault_page(unsigned long page, unsigned int *pglevel)
162 int ret = set_page_present(page & PAGE_MASK, true, pglevel);
163 WARN_ONCE(ret < 0, KERN_ERR "kmmio disarming 0x%08lx failed.\n", page);
167 * This is being called from do_page_fault().
169 * We may be in an interrupt or a critical section. Also prefecthing may
170 * trigger a page fault. We may be in the middle of process switch.
171 * We cannot take any locks, because we could be executing especially
172 * within a kmmio critical section.
174 * Local interrupts are disabled, so preemption cannot happen.
175 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
178 * Interrupts are disabled on entry as trap3 is an interrupt gate
179 * and they remain disabled thorough out this function.
181 int kmmio_handler(struct pt_regs *regs, unsigned long addr)
183 struct kmmio_context *ctx;
184 struct kmmio_fault_page *faultpage;
185 int ret = 0; /* default to fault not handled */
188 * Preemption is now disabled to prevent process switch during
189 * single stepping. We can only handle one active kmmio trace
190 * per cpu, so ensure that we finish it before something else
191 * gets to run. We also hold the RCU read lock over single
192 * stepping to avoid looking up the probe and kmmio_fault_page
198 faultpage = get_kmmio_fault_page(addr);
201 * Either this page fault is not caused by kmmio, or
202 * another CPU just pulled the kmmio probe from under
203 * our feet. The latter case should not be possible.
208 ctx = &get_cpu_var(kmmio_ctx);
210 disarm_kmmio_fault_page(faultpage->page, NULL);
211 if (addr == ctx->addr) {
213 * On SMP we sometimes get recursive probe hits on the
214 * same address. Context is already saved, fall out.
216 pr_debug("kmmio: duplicate probe hit on CPU %d, for "
217 "address 0x%08lx.\n",
218 smp_processor_id(), addr);
223 * Prevent overwriting already in-flight context.
224 * This should not happen, let's hope disarming at least
227 pr_emerg("kmmio: recursive probe hit on CPU %d, "
228 "for address 0x%08lx. Ignoring.\n",
229 smp_processor_id(), addr);
230 pr_emerg("kmmio: previous hit was at 0x%08lx.\n",
236 ctx->fpage = faultpage;
237 ctx->probe = get_kmmio_probe(addr);
238 ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
241 if (ctx->probe && ctx->probe->pre_handler)
242 ctx->probe->pre_handler(ctx->probe, regs, addr);
245 * Enable single-stepping and disable interrupts for the faulting
246 * context. Local interrupts must not get enabled during stepping.
248 regs->flags |= X86_EFLAGS_TF;
249 regs->flags &= ~X86_EFLAGS_IF;
251 /* Now we set present bit in PTE and single step. */
252 disarm_kmmio_fault_page(ctx->fpage->page, NULL);
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.
261 put_cpu_var(kmmio_ctx);
262 return 1; /* fault handled */
265 put_cpu_var(kmmio_ctx);
268 preempt_enable_no_resched();
273 * Interrupts are disabled on entry as trap1 is an interrupt gate
274 * and they remain disabled thorough out this function.
275 * This must always get called as the pair to kmmio_handler().
277 static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
280 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
283 pr_debug("kmmio: spurious debug trap on CPU %d.\n",
288 if (ctx->probe && ctx->probe->post_handler)
289 ctx->probe->post_handler(ctx->probe, condition, regs);
291 arm_kmmio_fault_page(ctx->fpage->page, NULL);
293 regs->flags &= ~X86_EFLAGS_TF;
294 regs->flags |= ctx->saved_flags;
296 /* These were acquired in kmmio_handler(). */
300 preempt_enable_no_resched();
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.
307 if (!(regs->flags & X86_EFLAGS_TF))
310 put_cpu_var(kmmio_ctx);
314 /* You must be holding kmmio_lock. */
315 static int add_kmmio_fault_page(unsigned long page)
317 struct kmmio_fault_page *f;
320 f = get_kmmio_fault_page(page);
323 arm_kmmio_fault_page(f->page, NULL);
328 f = kmalloc(sizeof(*f), GFP_ATOMIC);
335 if (arm_kmmio_fault_page(f->page, NULL)) {
340 list_add_rcu(&f->list, kmmio_page_list(f->page));
345 /* You must be holding kmmio_lock. */
346 static void release_kmmio_fault_page(unsigned long page,
347 struct kmmio_fault_page **release_list)
349 struct kmmio_fault_page *f;
352 f = get_kmmio_fault_page(page);
357 BUG_ON(f->count < 0);
359 disarm_kmmio_fault_page(f->page, NULL);
360 f->release_next = *release_list;
366 * With page-unaligned ioremaps, one or two armed pages may contain
367 * addresses from outside the intended mapping. Events for these addresses
368 * are currently silently dropped. The events may result only from programming
369 * mistakes by accessing addresses before the beginning or past the end of a
372 int register_kmmio_probe(struct kmmio_probe *p)
376 unsigned long size = 0;
377 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
379 spin_lock_irqsave(&kmmio_lock, flags);
380 if (get_kmmio_probe(p->addr)) {
385 list_add_rcu(&p->list, &kmmio_probes);
386 while (size < size_lim) {
387 if (add_kmmio_fault_page(p->addr + size))
388 pr_err("kmmio: Unable to set page fault.\n");
392 spin_unlock_irqrestore(&kmmio_lock, flags);
394 * XXX: What should I do here?
395 * Here was a call to global_flush_tlb(), but it does not exist
396 * anymore. It seems it's not needed after all.
400 EXPORT_SYMBOL(register_kmmio_probe);
402 static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
404 struct kmmio_delayed_release *dr = container_of(
406 struct kmmio_delayed_release,
408 struct kmmio_fault_page *p = dr->release_list;
410 struct kmmio_fault_page *next = p->release_next;
418 static void remove_kmmio_fault_pages(struct rcu_head *head)
420 struct kmmio_delayed_release *dr = container_of(
422 struct kmmio_delayed_release,
424 struct kmmio_fault_page *p = dr->release_list;
425 struct kmmio_fault_page **prevp = &dr->release_list;
427 spin_lock_irqsave(&kmmio_lock, flags);
430 list_del_rcu(&p->list);
432 *prevp = p->release_next;
433 prevp = &p->release_next;
436 spin_unlock_irqrestore(&kmmio_lock, flags);
437 /* This is the real RCU destroy call. */
438 call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
442 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
443 * sure that the callbacks will not be called anymore. Only after that
444 * you may actually release your struct kmmio_probe.
446 * Unregistering a kmmio fault page has three steps:
447 * 1. release_kmmio_fault_page()
448 * Disarm the page, wait a grace period to let all faults finish.
449 * 2. remove_kmmio_fault_pages()
450 * Remove the pages from kmmio_page_table.
451 * 3. rcu_free_kmmio_fault_pages()
452 * Actally free the kmmio_fault_page structs as with RCU.
454 void unregister_kmmio_probe(struct kmmio_probe *p)
457 unsigned long size = 0;
458 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
459 struct kmmio_fault_page *release_list = NULL;
460 struct kmmio_delayed_release *drelease;
462 spin_lock_irqsave(&kmmio_lock, flags);
463 while (size < size_lim) {
464 release_kmmio_fault_page(p->addr + size, &release_list);
467 list_del_rcu(&p->list);
469 spin_unlock_irqrestore(&kmmio_lock, flags);
471 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
473 pr_crit("kmmio: leaking kmmio_fault_page objects.\n");
476 drelease->release_list = release_list;
479 * This is not really RCU here. We have just disarmed a set of
480 * pages so that they cannot trigger page faults anymore. However,
481 * we cannot remove the pages from kmmio_page_table,
482 * because a probe hit might be in flight on another CPU. The
483 * pages are collected into a list, and they will be removed from
484 * kmmio_page_table when it is certain that no probe hit related to
485 * these pages can be in flight. RCU grace period sounds like a
488 * If we removed the pages too early, kmmio page fault handler might
489 * not find the respective kmmio_fault_page and determine it's not
490 * a kmmio fault, when it actually is. This would lead to madness.
492 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
494 EXPORT_SYMBOL(unregister_kmmio_probe);
496 static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
499 struct die_args *arg = args;
501 if (val == DIE_DEBUG && (arg->err & DR_STEP))
502 if (post_kmmio_handler(arg->err, arg->regs) == 1)
508 static struct notifier_block nb_die = {
509 .notifier_call = kmmio_die_notifier
512 static int __init init_kmmio(void)
515 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
516 INIT_LIST_HEAD(&kmmio_page_table[i]);
517 return register_die_notifier(&nb_die);
519 fs_initcall(init_kmmio); /* should be before device_initcall() */