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/version.h>
9 #include <linux/list.h>
10 #include <linux/spinlock.h>
11 #include <linux/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
17 #include <linux/uaccess.h>
18 #include <linux/ptrace.h>
19 #include <linux/preempt.h>
20 #include <linux/percpu.h>
21 #include <linux/kdebug.h>
22 #include <linux/mutex.h>
24 #include <asm/cacheflush.h>
25 #include <asm/errno.h>
26 #include <asm/tlbflush.h>
27 #include <asm/pgtable.h>
29 #include <linux/mmiotrace.h>
31 #define KMMIO_PAGE_HASH_BITS 4
32 #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
34 struct kmmio_fault_page {
35 struct list_head list;
36 struct kmmio_fault_page *release_next;
37 unsigned long page; /* location of the fault page */
40 * Number of times this page has been registered as a part
41 * of a probe. If zero, page is disarmed and this may be freed.
42 * Used only by writers (RCU).
47 struct kmmio_delayed_release {
49 struct kmmio_fault_page *release_list;
52 struct kmmio_context {
53 struct kmmio_fault_page *fpage;
54 struct kmmio_probe *probe;
55 unsigned long saved_flags;
60 static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
63 static DEFINE_MUTEX(kmmio_init_mutex);
64 static DEFINE_SPINLOCK(kmmio_lock);
66 /* These are protected by kmmio_lock */
67 static int kmmio_initialized;
68 unsigned int kmmio_count;
70 /* Read-protected by RCU, write-protected by kmmio_lock. */
71 static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
72 static LIST_HEAD(kmmio_probes);
74 static struct list_head *kmmio_page_list(unsigned long page)
76 return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
79 /* Accessed per-cpu */
80 static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
82 /* protected by kmmio_init_mutex */
83 static struct notifier_block nb_die = {
84 .notifier_call = kmmio_die_notifier
88 * Makes sure kmmio is initialized and usable.
89 * This must be called before any other kmmio function defined here.
92 void reference_kmmio(void)
94 mutex_lock(&kmmio_init_mutex);
95 spin_lock_irq(&kmmio_lock);
96 if (!kmmio_initialized) {
98 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
99 INIT_LIST_HEAD(&kmmio_page_table[i]);
100 if (register_die_notifier(&nb_die))
104 spin_unlock_irq(&kmmio_lock);
105 mutex_unlock(&kmmio_init_mutex);
107 EXPORT_SYMBOL_GPL(reference_kmmio);
110 * Clean up kmmio after use. This must be called for every call to
111 * reference_kmmio(). All probes registered after the corresponding
112 * reference_kmmio() must have been unregistered when calling this.
115 void unreference_kmmio(void)
119 mutex_lock(&kmmio_init_mutex);
120 spin_lock_irq(&kmmio_lock);
122 if (kmmio_initialized == 1) {
123 BUG_ON(is_kmmio_active());
127 BUG_ON(kmmio_initialized < 0);
128 spin_unlock_irq(&kmmio_lock);
131 unregister_die_notifier(&nb_die); /* calls sync_rcu() */
132 mutex_unlock(&kmmio_init_mutex);
134 EXPORT_SYMBOL(unreference_kmmio);
137 * this is basically a dynamic stabbing problem:
138 * Could use the existing prio tree code or
139 * Possible better implementations:
140 * The Interval Skip List: A Data Structure for Finding All Intervals That
141 * Overlap a Point (might be simple)
142 * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
144 /* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
145 static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
147 struct kmmio_probe *p;
148 list_for_each_entry_rcu(p, &kmmio_probes, list) {
149 if (addr >= p->addr && addr <= (p->addr + p->len))
155 /* You must be holding RCU read lock. */
156 static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
158 struct list_head *head;
159 struct kmmio_fault_page *p;
162 head = kmmio_page_list(page);
163 list_for_each_entry_rcu(p, head, list) {
170 /** Mark the given page as not present. Access to it will trigger a fault. */
171 static void arm_kmmio_fault_page(unsigned long page, int *page_level)
173 unsigned long address = page & PAGE_MASK;
175 pte_t *pte = lookup_address(address, &level);
178 pr_err("kmmio: Error in %s: no pte for page 0x%08lx\n",
183 if (level == PG_LEVEL_2M) {
184 pmd_t *pmd = (pmd_t *)pte;
185 set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_PRESENT));
188 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
194 __flush_tlb_one(page);
197 /** Mark the given page as present. */
198 static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
200 unsigned long address = page & PAGE_MASK;
202 pte_t *pte = lookup_address(address, &level);
205 pr_err("kmmio: Error in %s: no pte for page 0x%08lx\n",
210 if (level == PG_LEVEL_2M) {
211 pmd_t *pmd = (pmd_t *)pte;
212 set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_PRESENT));
215 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
221 __flush_tlb_one(page);
225 * This is being called from do_page_fault().
227 * We may be in an interrupt or a critical section. Also prefecthing may
228 * trigger a page fault. We may be in the middle of process switch.
229 * We cannot take any locks, because we could be executing especially
230 * within a kmmio critical section.
232 * Local interrupts are disabled, so preemption cannot happen.
233 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
236 * Interrupts are disabled on entry as trap3 is an interrupt gate
237 * and they remain disabled thorough out this function.
239 int kmmio_handler(struct pt_regs *regs, unsigned long addr)
241 struct kmmio_context *ctx;
242 struct kmmio_fault_page *faultpage;
245 * Preemption is now disabled to prevent process switch during
246 * single stepping. We can only handle one active kmmio trace
247 * per cpu, so ensure that we finish it before something else
248 * gets to run. We also hold the RCU read lock over single
249 * stepping to avoid looking up the probe and kmmio_fault_page
255 faultpage = get_kmmio_fault_page(addr);
258 * Either this page fault is not caused by kmmio, or
259 * another CPU just pulled the kmmio probe from under
260 * our feet. In the latter case all hell breaks loose.
265 ctx = &get_cpu_var(kmmio_ctx);
268 * Prevent overwriting already in-flight context.
269 * If this page fault really was due to kmmio trap,
270 * all hell breaks loose.
272 pr_emerg("kmmio: recursive probe hit on CPU %d, "
273 "for address 0x%08lx. Ignoring.\n",
274 smp_processor_id(), addr);
279 ctx->fpage = faultpage;
280 ctx->probe = get_kmmio_probe(addr);
281 ctx->saved_flags = (regs->flags & (TF_MASK|IF_MASK));
284 if (ctx->probe && ctx->probe->pre_handler)
285 ctx->probe->pre_handler(ctx->probe, regs, addr);
288 * Enable single-stepping and disable interrupts for the faulting
289 * context. Local interrupts must not get enabled during stepping.
291 regs->flags |= TF_MASK;
292 regs->flags &= ~IF_MASK;
294 /* Now we set present bit in PTE and single step. */
295 disarm_kmmio_fault_page(ctx->fpage->page, NULL);
298 * If another cpu accesses the same page while we are stepping,
299 * the access will not be caught. It will simply succeed and the
300 * only downside is we lose the event. If this becomes a problem,
301 * the user should drop to single cpu before tracing.
304 put_cpu_var(kmmio_ctx);
308 put_cpu_var(kmmio_ctx);
311 preempt_enable_no_resched();
312 return 0; /* page fault not handled by kmmio */
316 * Interrupts are disabled on entry as trap1 is an interrupt gate
317 * and they remain disabled thorough out this function.
318 * This must always get called as the pair to kmmio_handler().
320 static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
323 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
328 if (ctx->probe && ctx->probe->post_handler)
329 ctx->probe->post_handler(ctx->probe, condition, regs);
331 arm_kmmio_fault_page(ctx->fpage->page, NULL);
333 regs->flags &= ~TF_MASK;
334 regs->flags |= ctx->saved_flags;
336 /* These were acquired in kmmio_handler(). */
340 preempt_enable_no_resched();
343 * if somebody else is singlestepping across a probe point, flags
344 * will have TF set, in which case, continue the remaining processing
345 * of do_debug, as if this is not a probe hit.
347 if (!(regs->flags & TF_MASK))
350 put_cpu_var(kmmio_ctx);
354 /* You must be holding kmmio_lock. */
355 static int add_kmmio_fault_page(unsigned long page)
357 struct kmmio_fault_page *f;
360 f = get_kmmio_fault_page(page);
363 arm_kmmio_fault_page(f->page, NULL);
368 f = kmalloc(sizeof(*f), GFP_ATOMIC);
374 list_add_rcu(&f->list, kmmio_page_list(f->page));
376 arm_kmmio_fault_page(f->page, NULL);
381 /* You must be holding kmmio_lock. */
382 static void release_kmmio_fault_page(unsigned long page,
383 struct kmmio_fault_page **release_list)
385 struct kmmio_fault_page *f;
388 f = get_kmmio_fault_page(page);
393 BUG_ON(f->count < 0);
395 disarm_kmmio_fault_page(f->page, NULL);
396 f->release_next = *release_list;
401 int register_kmmio_probe(struct kmmio_probe *p)
405 unsigned long size = 0;
407 spin_lock_irqsave(&kmmio_lock, flags);
408 if (get_kmmio_probe(p->addr)) {
413 list_add_rcu(&p->list, &kmmio_probes);
414 while (size < p->len) {
415 if (add_kmmio_fault_page(p->addr + size))
416 pr_err("kmmio: Unable to set page fault.\n");
420 spin_unlock_irqrestore(&kmmio_lock, flags);
422 * XXX: What should I do here?
423 * Here was a call to global_flush_tlb(), but it does not exist
424 * anymore. It seems it's not needed after all.
428 EXPORT_SYMBOL(register_kmmio_probe);
430 static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
432 struct kmmio_delayed_release *dr = container_of(
434 struct kmmio_delayed_release,
436 struct kmmio_fault_page *p = dr->release_list;
438 struct kmmio_fault_page *next = p->release_next;
446 static void remove_kmmio_fault_pages(struct rcu_head *head)
448 struct kmmio_delayed_release *dr = container_of(
450 struct kmmio_delayed_release,
452 struct kmmio_fault_page *p = dr->release_list;
453 struct kmmio_fault_page **prevp = &dr->release_list;
455 spin_lock_irqsave(&kmmio_lock, flags);
458 list_del_rcu(&p->list);
460 *prevp = p->release_next;
461 prevp = &p->release_next;
464 spin_unlock_irqrestore(&kmmio_lock, flags);
465 /* This is the real RCU destroy call. */
466 call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
470 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
471 * sure that the callbacks will not be called anymore. Only after that
472 * you may actually release your struct kmmio_probe.
474 * Unregistering a kmmio fault page has three steps:
475 * 1. release_kmmio_fault_page()
476 * Disarm the page, wait a grace period to let all faults finish.
477 * 2. remove_kmmio_fault_pages()
478 * Remove the pages from kmmio_page_table.
479 * 3. rcu_free_kmmio_fault_pages()
480 * Actally free the kmmio_fault_page structs as with RCU.
482 void unregister_kmmio_probe(struct kmmio_probe *p)
485 unsigned long size = 0;
486 struct kmmio_fault_page *release_list = NULL;
487 struct kmmio_delayed_release *drelease;
489 spin_lock_irqsave(&kmmio_lock, flags);
490 while (size < p->len) {
491 release_kmmio_fault_page(p->addr + size, &release_list);
494 list_del_rcu(&p->list);
496 spin_unlock_irqrestore(&kmmio_lock, flags);
498 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
500 pr_crit("kmmio: leaking kmmio_fault_page objects.\n");
503 drelease->release_list = release_list;
506 * This is not really RCU here. We have just disarmed a set of
507 * pages so that they cannot trigger page faults anymore. However,
508 * we cannot remove the pages from kmmio_page_table,
509 * because a probe hit might be in flight on another CPU. The
510 * pages are collected into a list, and they will be removed from
511 * kmmio_page_table when it is certain that no probe hit related to
512 * these pages can be in flight. RCU grace period sounds like a
515 * If we removed the pages too early, kmmio page fault handler might
516 * not find the respective kmmio_fault_page and determine it's not
517 * a kmmio fault, when it actually is. This would lead to madness.
519 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
521 EXPORT_SYMBOL(unregister_kmmio_probe);
523 static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
526 struct die_args *arg = args;
528 if (val == DIE_DEBUG)
529 if (post_kmmio_handler(arg->err, arg->regs) == 1)