blob: efb4679330876482aafd253655a7c7f7b1faa6f0 [file] [log] [blame]
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02001/* 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
8#include <linux/version.h>
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +02009#include <linux/list.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020010#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>
16#include <linux/mm.h>
17#include <linux/uaccess.h>
18#include <linux/ptrace.h>
19#include <linux/preempt.h>
Pekka Paalanenf5136382008-05-12 21:20:57 +020020#include <linux/percpu.h>
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020021#include <linux/kdebug.h>
Pekka Paalanend61fc442008-05-12 21:20:57 +020022#include <linux/mutex.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020023#include <asm/io.h>
24#include <asm/cacheflush.h>
25#include <asm/errno.h>
26#include <asm/tlbflush.h>
Pekka Paalanen75bb8832008-05-12 21:20:56 +020027#include <asm/pgtable.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020028
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020029#include <linux/mmiotrace.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020030
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020031#define KMMIO_PAGE_HASH_BITS 4
32#define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
33
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020034struct kmmio_fault_page {
35 struct list_head list;
36 struct kmmio_fault_page *release_next;
37 unsigned long page; /* location of the fault page */
38
39 /*
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).
43 */
44 int count;
45};
46
47struct kmmio_delayed_release {
48 struct rcu_head rcu;
49 struct kmmio_fault_page *release_list;
50};
51
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020052struct kmmio_context {
53 struct kmmio_fault_page *fpage;
54 struct kmmio_probe *probe;
55 unsigned long saved_flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020056 unsigned long addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020057 int active;
58};
59
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020060static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
61 void *args);
62
Pekka Paalanend61fc442008-05-12 21:20:57 +020063static DEFINE_MUTEX(kmmio_init_mutex);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020064static DEFINE_SPINLOCK(kmmio_lock);
65
66/* These are protected by kmmio_lock */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020067static int kmmio_initialized;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020068unsigned int kmmio_count;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020069
70/* Read-protected by RCU, write-protected by kmmio_lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020071static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
72static LIST_HEAD(kmmio_probes);
73
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020074static struct list_head *kmmio_page_list(unsigned long page)
75{
76 return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)];
77}
78
Pekka Paalanenf5136382008-05-12 21:20:57 +020079/* Accessed per-cpu */
80static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020081
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020082/* protected by kmmio_init_mutex */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020083static struct notifier_block nb_die = {
84 .notifier_call = kmmio_die_notifier
85};
86
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020087/**
88 * Makes sure kmmio is initialized and usable.
89 * This must be called before any other kmmio function defined here.
90 * May sleep.
91 */
92void reference_kmmio(void)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020093{
Pekka Paalanend61fc442008-05-12 21:20:57 +020094 mutex_lock(&kmmio_init_mutex);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020095 spin_lock_irq(&kmmio_lock);
96 if (!kmmio_initialized) {
97 int i;
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))
Pekka Paalanen10c43d22008-05-12 21:20:57 +0200101 BUG();
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200102 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200103 kmmio_initialized++;
104 spin_unlock_irq(&kmmio_lock);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200105 mutex_unlock(&kmmio_init_mutex);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200106}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200107EXPORT_SYMBOL_GPL(reference_kmmio);
108
109/**
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.
113 * May sleep.
114 */
115void unreference_kmmio(void)
116{
117 bool unreg = false;
118
Pekka Paalanend61fc442008-05-12 21:20:57 +0200119 mutex_lock(&kmmio_init_mutex);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200120 spin_lock_irq(&kmmio_lock);
121
122 if (kmmio_initialized == 1) {
123 BUG_ON(is_kmmio_active());
124 unreg = true;
125 }
126 kmmio_initialized--;
127 BUG_ON(kmmio_initialized < 0);
128 spin_unlock_irq(&kmmio_lock);
129
130 if (unreg)
131 unregister_die_notifier(&nb_die); /* calls sync_rcu() */
Pekka Paalanend61fc442008-05-12 21:20:57 +0200132 mutex_unlock(&kmmio_init_mutex);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200133}
134EXPORT_SYMBOL(unreference_kmmio);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200135
136/*
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
143 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200144/* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200145static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
146{
147 struct kmmio_probe *p;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200148 list_for_each_entry_rcu(p, &kmmio_probes, list) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200149 if (addr >= p->addr && addr <= (p->addr + p->len))
150 return p;
151 }
152 return NULL;
153}
154
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200155/* You must be holding RCU read lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200156static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
157{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200158 struct list_head *head;
159 struct kmmio_fault_page *p;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200160
161 page &= PAGE_MASK;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200162 head = kmmio_page_list(page);
163 list_for_each_entry_rcu(p, head, list) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200164 if (p->page == page)
165 return p;
166 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200167 return NULL;
168}
169
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200170/** Mark the given page as not present. Access to it will trigger a fault. */
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200171static void arm_kmmio_fault_page(unsigned long page, int *page_level)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200172{
173 unsigned long address = page & PAGE_MASK;
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200174 int level;
175 pte_t *pte = lookup_address(address, &level);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200176
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200177 if (!pte) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200178 pr_err("kmmio: Error in %s: no pte for page 0x%08lx\n",
179 __func__, page);
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200180 return;
181 }
182
183 if (level == PG_LEVEL_2M) {
184 pmd_t *pmd = (pmd_t *)pte;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200185 set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_PRESENT));
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200186 } else {
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200187 /* PG_LEVEL_4K */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200188 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
189 }
190
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200191 if (page_level)
192 *page_level = level;
193
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200194 __flush_tlb_one(page);
195}
196
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200197/** Mark the given page as present. */
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200198static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200199{
200 unsigned long address = page & PAGE_MASK;
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200201 int level;
202 pte_t *pte = lookup_address(address, &level);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200203
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200204 if (!pte) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200205 pr_err("kmmio: Error in %s: no pte for page 0x%08lx\n",
206 __func__, page);
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200207 return;
208 }
209
210 if (level == PG_LEVEL_2M) {
211 pmd_t *pmd = (pmd_t *)pte;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200212 set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_PRESENT));
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200213 } else {
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200214 /* PG_LEVEL_4K */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200215 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
216 }
217
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200218 if (page_level)
219 *page_level = level;
220
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200221 __flush_tlb_one(page);
222}
223
224/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200225 * This is being called from do_page_fault().
226 *
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.
231 *
232 * Local interrupts are disabled, so preemption cannot happen.
233 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
234 */
235/*
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200236 * Interrupts are disabled on entry as trap3 is an interrupt gate
237 * and they remain disabled thorough out this function.
238 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200239int kmmio_handler(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200240{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200241 struct kmmio_context *ctx;
242 struct kmmio_fault_page *faultpage;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200243
244 /*
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
Pekka Paalanend61fc442008-05-12 21:20:57 +0200248 * gets to run. We also hold the RCU read lock over single
249 * stepping to avoid looking up the probe and kmmio_fault_page
250 * again.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200251 */
252 preempt_disable();
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200253 rcu_read_lock();
Pekka Paalanend61fc442008-05-12 21:20:57 +0200254
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200255 faultpage = get_kmmio_fault_page(addr);
256 if (!faultpage) {
257 /*
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.
261 */
262 goto no_kmmio;
263 }
264
265 ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200266 if (ctx->active) {
267 /*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200268 * Prevent overwriting already in-flight context.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200269 * If this page fault really was due to kmmio trap,
270 * all hell breaks loose.
271 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200272 pr_emerg("kmmio: recursive probe hit on CPU %d, "
273 "for address 0x%08lx. Ignoring.\n",
Pekka Paalanenf5136382008-05-12 21:20:57 +0200274 smp_processor_id(), addr);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200275 goto no_kmmio_ctx;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200276 }
277 ctx->active++;
278
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200279 ctx->fpage = faultpage;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200280 ctx->probe = get_kmmio_probe(addr);
281 ctx->saved_flags = (regs->flags & (TF_MASK|IF_MASK));
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200282 ctx->addr = addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200283
284 if (ctx->probe && ctx->probe->pre_handler)
285 ctx->probe->pre_handler(ctx->probe, regs, addr);
286
Pekka Paalanend61fc442008-05-12 21:20:57 +0200287 /*
288 * Enable single-stepping and disable interrupts for the faulting
289 * context. Local interrupts must not get enabled during stepping.
290 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200291 regs->flags |= TF_MASK;
292 regs->flags &= ~IF_MASK;
293
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200294 /* Now we set present bit in PTE and single step. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200295 disarm_kmmio_fault_page(ctx->fpage->page, NULL);
296
Pekka Paalanend61fc442008-05-12 21:20:57 +0200297 /*
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.
302 */
303
Pekka Paalanenf5136382008-05-12 21:20:57 +0200304 put_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200305 return 1;
306
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200307no_kmmio_ctx:
Pekka Paalanenf5136382008-05-12 21:20:57 +0200308 put_cpu_var(kmmio_ctx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200309no_kmmio:
310 rcu_read_unlock();
311 preempt_enable_no_resched();
312 return 0; /* page fault not handled by kmmio */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200313}
314
315/*
316 * Interrupts are disabled on entry as trap1 is an interrupt gate
317 * and they remain disabled thorough out this function.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200318 * This must always get called as the pair to kmmio_handler().
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200319 */
320static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
321{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200322 int ret = 0;
323 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200324
325 if (!ctx->active)
Pekka Paalanenf5136382008-05-12 21:20:57 +0200326 goto out;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200327
328 if (ctx->probe && ctx->probe->post_handler)
329 ctx->probe->post_handler(ctx->probe, condition, regs);
330
Pekka Paalanend61fc442008-05-12 21:20:57 +0200331 arm_kmmio_fault_page(ctx->fpage->page, NULL);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200332
333 regs->flags &= ~TF_MASK;
334 regs->flags |= ctx->saved_flags;
335
336 /* These were acquired in kmmio_handler(). */
337 ctx->active--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200338 BUG_ON(ctx->active);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200339 rcu_read_unlock();
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200340 preempt_enable_no_resched();
341
342 /*
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.
346 */
Pekka Paalanenf5136382008-05-12 21:20:57 +0200347 if (!(regs->flags & TF_MASK))
348 ret = 1;
Pekka Paalanenf5136382008-05-12 21:20:57 +0200349out:
350 put_cpu_var(kmmio_ctx);
351 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200352}
353
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200354/* You must be holding kmmio_lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200355static int add_kmmio_fault_page(unsigned long page)
356{
357 struct kmmio_fault_page *f;
358
359 page &= PAGE_MASK;
360 f = get_kmmio_fault_page(page);
361 if (f) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200362 if (!f->count)
363 arm_kmmio_fault_page(f->page, NULL);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200364 f->count++;
365 return 0;
366 }
367
368 f = kmalloc(sizeof(*f), GFP_ATOMIC);
369 if (!f)
370 return -1;
371
372 f->count = 1;
373 f->page = page;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200374 list_add_rcu(&f->list, kmmio_page_list(f->page));
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200375
376 arm_kmmio_fault_page(f->page, NULL);
377
378 return 0;
379}
380
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200381/* You must be holding kmmio_lock. */
382static void release_kmmio_fault_page(unsigned long page,
383 struct kmmio_fault_page **release_list)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200384{
385 struct kmmio_fault_page *f;
386
387 page &= PAGE_MASK;
388 f = get_kmmio_fault_page(page);
389 if (!f)
390 return;
391
392 f->count--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200393 BUG_ON(f->count < 0);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200394 if (!f->count) {
395 disarm_kmmio_fault_page(f->page, NULL);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200396 f->release_next = *release_list;
397 *release_list = f;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200398 }
399}
400
401int register_kmmio_probe(struct kmmio_probe *p)
402{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200403 unsigned long flags;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200404 int ret = 0;
405 unsigned long size = 0;
406
Pekka Paalanend61fc442008-05-12 21:20:57 +0200407 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200408 if (get_kmmio_probe(p->addr)) {
409 ret = -EEXIST;
410 goto out;
411 }
Pekka Paalanend61fc442008-05-12 21:20:57 +0200412 kmmio_count++;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200413 list_add_rcu(&p->list, &kmmio_probes);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200414 while (size < p->len) {
415 if (add_kmmio_fault_page(p->addr + size))
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200416 pr_err("kmmio: Unable to set page fault.\n");
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200417 size += PAGE_SIZE;
418 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200419out:
Pekka Paalanend61fc442008-05-12 21:20:57 +0200420 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200421 /*
422 * XXX: What should I do here?
423 * Here was a call to global_flush_tlb(), but it does not exist
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200424 * anymore. It seems it's not needed after all.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200425 */
426 return ret;
427}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200428EXPORT_SYMBOL(register_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200429
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200430static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200431{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200432 struct kmmio_delayed_release *dr = container_of(
433 head,
434 struct kmmio_delayed_release,
435 rcu);
436 struct kmmio_fault_page *p = dr->release_list;
437 while (p) {
438 struct kmmio_fault_page *next = p->release_next;
439 BUG_ON(p->count);
440 kfree(p);
441 p = next;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200442 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200443 kfree(dr);
444}
445
446static void remove_kmmio_fault_pages(struct rcu_head *head)
447{
448 struct kmmio_delayed_release *dr = container_of(
449 head,
450 struct kmmio_delayed_release,
451 rcu);
452 struct kmmio_fault_page *p = dr->release_list;
453 struct kmmio_fault_page **prevp = &dr->release_list;
454 unsigned long flags;
455 spin_lock_irqsave(&kmmio_lock, flags);
456 while (p) {
457 if (!p->count)
458 list_del_rcu(&p->list);
459 else
460 *prevp = p->release_next;
461 prevp = &p->release_next;
462 p = p->release_next;
463 }
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);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200467}
468
469/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200470 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
Pekka Paalanend61fc442008-05-12 21:20:57 +0200471 * sure that the callbacks will not be called anymore. Only after that
472 * you may actually release your struct kmmio_probe.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200473 *
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200474 * 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.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200481 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200482void unregister_kmmio_probe(struct kmmio_probe *p)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200483{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200484 unsigned long flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200485 unsigned long size = 0;
486 struct kmmio_fault_page *release_list = NULL;
487 struct kmmio_delayed_release *drelease;
488
Pekka Paalanend61fc442008-05-12 21:20:57 +0200489 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200490 while (size < p->len) {
491 release_kmmio_fault_page(p->addr + size, &release_list);
492 size += PAGE_SIZE;
493 }
494 list_del_rcu(&p->list);
495 kmmio_count--;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200496 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200497
498 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
499 if (!drelease) {
500 pr_crit("kmmio: leaking kmmio_fault_page objects.\n");
501 return;
502 }
503 drelease->release_list = release_list;
504
505 /*
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
513 * good choice.
514 *
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.
518 */
519 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200520}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200521EXPORT_SYMBOL(unregister_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200522
523static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
524 void *args)
525{
526 struct die_args *arg = args;
527
528 if (val == DIE_DEBUG)
529 if (post_kmmio_handler(arg->err, arg->regs) == 1)
530 return NOTIFY_STOP;
531
532 return NOTIFY_DONE;
533}