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/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>
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>
27 #define KMMIO_PAGE_HASH_BITS 4
28 #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 page; /* location of the fault page */
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).
43 struct kmmio_delayed_release {
45 struct kmmio_fault_page *release_list;
48 struct kmmio_context {
49 struct kmmio_fault_page *fpage;
50 struct kmmio_probe *probe;
51 unsigned long saved_flags;
56 static DEFINE_SPINLOCK(kmmio_lock);
58 /* Protected by kmmio_lock */
59 unsigned int kmmio_count;
61 /* Read-protected by RCU, write-protected by kmmio_lock. */
62 static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
63 static LIST_HEAD(kmmio_probes);
65 static struct list_head *kmmio_page_list(unsigned long page)
67 return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
70 /* Accessed per-cpu */
71 static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
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
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)
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))
92 /* You must be holding RCU read lock. */
93 static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
95 struct list_head *head;
96 struct kmmio_fault_page *p;
99 head = kmmio_page_list(page);
100 list_for_each_entry_rcu(p, head, list) {
107 static void set_page_present(unsigned long addr, bool present, int *pglevel)
113 pte_t *pte = lookup_address(addr, &level);
116 pr_err("kmmio: no pte for page 0x%08lx\n", addr);
126 pmdval = pmd_val(*pmd) & ~_PAGE_PRESENT;
128 pmdval |= _PAGE_PRESENT;
129 set_pmd(pmd, __pmd(pmdval));
133 pteval = pte_val(*pte) & ~_PAGE_PRESENT;
135 pteval |= _PAGE_PRESENT;
136 set_pte_atomic(pte, __pte(pteval));
140 pr_err("kmmio: unexpected page level 0x%x.\n", level);
144 __flush_tlb_one(addr);
147 /** Mark the given page as not present. Access to it will trigger a fault. */
148 static void arm_kmmio_fault_page(unsigned long page, int *page_level)
150 set_page_present(page & PAGE_MASK, false, page_level);
153 /** Mark the given page as present. */
154 static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
156 set_page_present(page & PAGE_MASK, true, page_level);
160 * This is being called from do_page_fault().
162 * We may be in an interrupt or a critical section. Also prefecthing may
163 * trigger a page fault. We may be in the middle of process switch.
164 * We cannot take any locks, because we could be executing especially
165 * within a kmmio critical section.
167 * Local interrupts are disabled, so preemption cannot happen.
168 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
171 * Interrupts are disabled on entry as trap3 is an interrupt gate
172 * and they remain disabled thorough out this function.
174 int kmmio_handler(struct pt_regs *regs, unsigned long addr)
176 struct kmmio_context *ctx;
177 struct kmmio_fault_page *faultpage;
178 int ret = 0; /* default to fault not handled */
181 * Preemption is now disabled to prevent process switch during
182 * single stepping. We can only handle one active kmmio trace
183 * per cpu, so ensure that we finish it before something else
184 * gets to run. We also hold the RCU read lock over single
185 * stepping to avoid looking up the probe and kmmio_fault_page
191 faultpage = get_kmmio_fault_page(addr);
194 * Either this page fault is not caused by kmmio, or
195 * another CPU just pulled the kmmio probe from under
196 * our feet. The latter case should not be possible.
201 ctx = &get_cpu_var(kmmio_ctx);
203 disarm_kmmio_fault_page(faultpage->page, NULL);
204 if (addr == ctx->addr) {
206 * On SMP we sometimes get recursive probe hits on the
207 * same address. Context is already saved, fall out.
209 pr_debug("kmmio: duplicate probe hit on CPU %d, for "
210 "address 0x%08lx.\n",
211 smp_processor_id(), addr);
216 * Prevent overwriting already in-flight context.
217 * This should not happen, let's hope disarming at least
220 pr_emerg("kmmio: recursive probe hit on CPU %d, "
221 "for address 0x%08lx. Ignoring.\n",
222 smp_processor_id(), addr);
223 pr_emerg("kmmio: previous hit was at 0x%08lx.\n",
229 ctx->fpage = faultpage;
230 ctx->probe = get_kmmio_probe(addr);
231 ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
234 if (ctx->probe && ctx->probe->pre_handler)
235 ctx->probe->pre_handler(ctx->probe, regs, addr);
238 * Enable single-stepping and disable interrupts for the faulting
239 * context. Local interrupts must not get enabled during stepping.
241 regs->flags |= X86_EFLAGS_TF;
242 regs->flags &= ~X86_EFLAGS_IF;
244 /* Now we set present bit in PTE and single step. */
245 disarm_kmmio_fault_page(ctx->fpage->page, NULL);
248 * If another cpu accesses the same page while we are stepping,
249 * the access will not be caught. It will simply succeed and the
250 * only downside is we lose the event. If this becomes a problem,
251 * the user should drop to single cpu before tracing.
254 put_cpu_var(kmmio_ctx);
255 return 1; /* fault handled */
258 put_cpu_var(kmmio_ctx);
261 preempt_enable_no_resched();
266 * Interrupts are disabled on entry as trap1 is an interrupt gate
267 * and they remain disabled thorough out this function.
268 * This must always get called as the pair to kmmio_handler().
270 static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
273 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
276 pr_debug("kmmio: spurious debug trap on CPU %d.\n",
281 if (ctx->probe && ctx->probe->post_handler)
282 ctx->probe->post_handler(ctx->probe, condition, regs);
284 arm_kmmio_fault_page(ctx->fpage->page, NULL);
286 regs->flags &= ~X86_EFLAGS_TF;
287 regs->flags |= ctx->saved_flags;
289 /* These were acquired in kmmio_handler(). */
293 preempt_enable_no_resched();
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.
300 if (!(regs->flags & X86_EFLAGS_TF))
303 put_cpu_var(kmmio_ctx);
307 /* You must be holding kmmio_lock. */
308 static int add_kmmio_fault_page(unsigned long page)
310 struct kmmio_fault_page *f;
313 f = get_kmmio_fault_page(page);
316 arm_kmmio_fault_page(f->page, NULL);
321 f = kmalloc(sizeof(*f), GFP_ATOMIC);
327 list_add_rcu(&f->list, kmmio_page_list(f->page));
329 arm_kmmio_fault_page(f->page, NULL);
334 /* You must be holding kmmio_lock. */
335 static void release_kmmio_fault_page(unsigned long page,
336 struct kmmio_fault_page **release_list)
338 struct kmmio_fault_page *f;
341 f = get_kmmio_fault_page(page);
346 BUG_ON(f->count < 0);
348 disarm_kmmio_fault_page(f->page, NULL);
349 f->release_next = *release_list;
355 * With page-unaligned ioremaps, one or two armed pages may contain
356 * addresses from outside the intended mapping. Events for these addresses
357 * are currently silently dropped. The events may result only from programming
358 * mistakes by accessing addresses before the beginning or past the end of a
361 int register_kmmio_probe(struct kmmio_probe *p)
365 unsigned long size = 0;
366 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
368 spin_lock_irqsave(&kmmio_lock, flags);
369 if (get_kmmio_probe(p->addr)) {
374 list_add_rcu(&p->list, &kmmio_probes);
375 while (size < size_lim) {
376 if (add_kmmio_fault_page(p->addr + size))
377 pr_err("kmmio: Unable to set page fault.\n");
381 spin_unlock_irqrestore(&kmmio_lock, flags);
383 * XXX: What should I do here?
384 * Here was a call to global_flush_tlb(), but it does not exist
385 * anymore. It seems it's not needed after all.
389 EXPORT_SYMBOL(register_kmmio_probe);
391 static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
393 struct kmmio_delayed_release *dr = container_of(
395 struct kmmio_delayed_release,
397 struct kmmio_fault_page *p = dr->release_list;
399 struct kmmio_fault_page *next = p->release_next;
407 static void remove_kmmio_fault_pages(struct rcu_head *head)
409 struct kmmio_delayed_release *dr = container_of(
411 struct kmmio_delayed_release,
413 struct kmmio_fault_page *p = dr->release_list;
414 struct kmmio_fault_page **prevp = &dr->release_list;
416 spin_lock_irqsave(&kmmio_lock, flags);
419 list_del_rcu(&p->list);
421 *prevp = p->release_next;
422 prevp = &p->release_next;
425 spin_unlock_irqrestore(&kmmio_lock, flags);
426 /* This is the real RCU destroy call. */
427 call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
431 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
432 * sure that the callbacks will not be called anymore. Only after that
433 * you may actually release your struct kmmio_probe.
435 * Unregistering a kmmio fault page has three steps:
436 * 1. release_kmmio_fault_page()
437 * Disarm the page, wait a grace period to let all faults finish.
438 * 2. remove_kmmio_fault_pages()
439 * Remove the pages from kmmio_page_table.
440 * 3. rcu_free_kmmio_fault_pages()
441 * Actally free the kmmio_fault_page structs as with RCU.
443 void unregister_kmmio_probe(struct kmmio_probe *p)
446 unsigned long size = 0;
447 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
448 struct kmmio_fault_page *release_list = NULL;
449 struct kmmio_delayed_release *drelease;
451 spin_lock_irqsave(&kmmio_lock, flags);
452 while (size < size_lim) {
453 release_kmmio_fault_page(p->addr + size, &release_list);
456 list_del_rcu(&p->list);
458 spin_unlock_irqrestore(&kmmio_lock, flags);
460 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
462 pr_crit("kmmio: leaking kmmio_fault_page objects.\n");
465 drelease->release_list = release_list;
468 * This is not really RCU here. We have just disarmed a set of
469 * pages so that they cannot trigger page faults anymore. However,
470 * we cannot remove the pages from kmmio_page_table,
471 * because a probe hit might be in flight on another CPU. The
472 * pages are collected into a list, and they will be removed from
473 * kmmio_page_table when it is certain that no probe hit related to
474 * these pages can be in flight. RCU grace period sounds like a
477 * If we removed the pages too early, kmmio page fault handler might
478 * not find the respective kmmio_fault_page and determine it's not
479 * a kmmio fault, when it actually is. This would lead to madness.
481 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
483 EXPORT_SYMBOL(unregister_kmmio_probe);
485 static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
488 struct die_args *arg = args;
490 if (val == DIE_DEBUG && (arg->err & DR_STEP))
491 if (post_kmmio_handler(arg->err, arg->regs) == 1)
497 static struct notifier_block nb_die = {
498 .notifier_call = kmmio_die_notifier
501 static int __init init_kmmio(void)
504 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
505 INIT_LIST_HEAD(&kmmio_page_table[i]);
506 return register_die_notifier(&nb_die);
508 fs_initcall(init_kmmio); /* should be before device_initcall() */