blob: afc47f5c953135cd427b88fadd83dd6c6e2d5c6d [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
Joe Perches1bd591a2009-12-09 10:45:36 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020010#include <linux/list.h>
Ingo Molnar668a6c32008-05-19 13:35:24 +020011#include <linux/rculist.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020012#include <linux/spinlock.h>
13#include <linux/hash.h>
Paul Gortmaker4b599fe2016-07-13 20:18:55 -040014#include <linux/export.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020015#include <linux/kernel.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020016#include <linux/uaccess.h>
17#include <linux/ptrace.h>
18#include <linux/preempt.h>
Pekka Paalanenf5136382008-05-12 21:20:57 +020019#include <linux/percpu.h>
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020020#include <linux/kdebug.h>
Pekka Paalanend61fc442008-05-12 21:20:57 +020021#include <linux/mutex.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +020022#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020024#include <asm/cacheflush.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020025#include <asm/tlbflush.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +020026#include <linux/errno.h>
Pekka Paalanen13829532008-05-12 21:20:58 +020027#include <asm/debugreg.h>
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020028#include <linux/mmiotrace.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020029
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020030#define KMMIO_PAGE_HASH_BITS 4
31#define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
32
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020033struct kmmio_fault_page {
34 struct list_head list;
35 struct kmmio_fault_page *release_next;
Karol Herbstcfa52c02016-03-03 02:03:11 +010036 unsigned long addr; /* the requested address */
Stuart Bennett46e91d02009-04-28 20:17:50 +010037 pteval_t old_presence; /* page presence prior to arming */
Pekka Paalanen5359b582009-03-01 16:11:58 +020038 bool armed;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020039
40 /*
41 * Number of times this page has been registered as a part
42 * of a probe. If zero, page is disarmed and this may be freed.
Pekka Paalanen340430c2009-02-24 21:44:15 +020043 * Used only by writers (RCU) and post_kmmio_handler().
44 * Protected by kmmio_lock, when linked into kmmio_page_table.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020045 */
46 int count;
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +020047
48 bool scheduled_for_release;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020049};
50
51struct kmmio_delayed_release {
52 struct rcu_head rcu;
53 struct kmmio_fault_page *release_list;
54};
55
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020056struct kmmio_context {
57 struct kmmio_fault_page *fpage;
58 struct kmmio_probe *probe;
59 unsigned long saved_flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020060 unsigned long addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020061 int active;
62};
63
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020064static DEFINE_SPINLOCK(kmmio_lock);
65
Pekka Paalanen13829532008-05-12 21:20:58 +020066/* Protected by kmmio_lock */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020067unsigned int kmmio_count;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020068
69/* Read-protected by RCU, write-protected by kmmio_lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020070static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
71static LIST_HEAD(kmmio_probes);
72
Karol Herbstcfa52c02016-03-03 02:03:11 +010073static struct list_head *kmmio_page_list(unsigned long addr)
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020074{
Karol Herbstcfa52c02016-03-03 02:03:11 +010075 unsigned int l;
76 pte_t *pte = lookup_address(addr, &l);
77
78 if (!pte)
79 return NULL;
80 addr &= page_level_mask(l);
81
82 return &kmmio_page_table[hash_long(addr, KMMIO_PAGE_HASH_BITS)];
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020083}
84
Pekka Paalanenf5136382008-05-12 21:20:57 +020085/* Accessed per-cpu */
86static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020087
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020088/*
89 * this is basically a dynamic stabbing problem:
90 * Could use the existing prio tree code or
91 * Possible better implementations:
92 * The Interval Skip List: A Data Structure for Finding All Intervals That
93 * Overlap a Point (might be simple)
94 * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
95 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020096/* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020097static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
98{
99 struct kmmio_probe *p;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200100 list_for_each_entry_rcu(p, &kmmio_probes, list) {
Stuart Bennett33015c82009-04-28 20:17:48 +0100101 if (addr >= p->addr && addr < (p->addr + p->len))
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200102 return p;
103 }
104 return NULL;
105}
106
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200107/* You must be holding RCU read lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100108static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200109{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200110 struct list_head *head;
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100111 struct kmmio_fault_page *f;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100112 unsigned int l;
113 pte_t *pte = lookup_address(addr, &l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200114
Karol Herbstcfa52c02016-03-03 02:03:11 +0100115 if (!pte)
116 return NULL;
117 addr &= page_level_mask(l);
118 head = kmmio_page_list(addr);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100119 list_for_each_entry_rcu(f, head, list) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100120 if (f->addr == addr)
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100121 return f;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200122 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200123 return NULL;
124}
125
Stuart Bennett46e91d02009-04-28 20:17:50 +0100126static void clear_pmd_presence(pmd_t *pmd, bool clear, pmdval_t *old)
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200127{
128 pmdval_t v = pmd_val(*pmd);
Stuart Bennett46e91d02009-04-28 20:17:50 +0100129 if (clear) {
130 *old = v & _PAGE_PRESENT;
131 v &= ~_PAGE_PRESENT;
132 } else /* presume this has been called with clear==true previously */
133 v |= *old;
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200134 set_pmd(pmd, __pmd(v));
135}
136
Stuart Bennett46e91d02009-04-28 20:17:50 +0100137static void clear_pte_presence(pte_t *pte, bool clear, pteval_t *old)
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200138{
139 pteval_t v = pte_val(*pte);
Stuart Bennett46e91d02009-04-28 20:17:50 +0100140 if (clear) {
141 *old = v & _PAGE_PRESENT;
142 v &= ~_PAGE_PRESENT;
143 } else /* presume this has been called with clear==true previously */
144 v |= *old;
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200145 set_pte_atomic(pte, __pte(v));
146}
147
Stuart Bennett46e91d02009-04-28 20:17:50 +0100148static int clear_page_presence(struct kmmio_fault_page *f, bool clear)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200149{
Pekka Paalanen790e2a22008-05-12 21:21:14 +0200150 unsigned int level;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100151 pte_t *pte = lookup_address(f->addr, &level);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200152
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200153 if (!pte) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100154 pr_err("no pte for addr 0x%08lx\n", f->addr);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000155 return -1;
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200156 }
157
Pekka Paalanen13829532008-05-12 21:20:58 +0200158 switch (level) {
159 case PG_LEVEL_2M:
Stuart Bennett46e91d02009-04-28 20:17:50 +0100160 clear_pmd_presence((pmd_t *)pte, clear, &f->old_presence);
Pekka Paalanen13829532008-05-12 21:20:58 +0200161 break;
Pekka Paalanen13829532008-05-12 21:20:58 +0200162 case PG_LEVEL_4K:
Stuart Bennett46e91d02009-04-28 20:17:50 +0100163 clear_pte_presence(pte, clear, &f->old_presence);
Pekka Paalanen13829532008-05-12 21:20:58 +0200164 break;
Pekka Paalanen13829532008-05-12 21:20:58 +0200165 default:
Joe Perches1bd591a2009-12-09 10:45:36 -0800166 pr_err("unexpected page level 0x%x.\n", level);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000167 return -1;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200168 }
169
Karol Herbstcfa52c02016-03-03 02:03:11 +0100170 __flush_tlb_one(f->addr);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000171 return 0;
Pekka Paalanen13829532008-05-12 21:20:58 +0200172}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200173
Pekka Paalanen5359b582009-03-01 16:11:58 +0200174/*
175 * Mark the given page as not present. Access to it will trigger a fault.
176 *
177 * Struct kmmio_fault_page is protected by RCU and kmmio_lock, but the
178 * protection is ignored here. RCU read lock is assumed held, so the struct
179 * will not disappear unexpectedly. Furthermore, the caller must guarantee,
180 * that double arming the same virtual address (page) cannot occur.
181 *
182 * Double disarming on the other hand is allowed, and may occur when a fault
183 * and mmiotrace shutdown happen simultaneously.
184 */
185static int arm_kmmio_fault_page(struct kmmio_fault_page *f)
Pekka Paalanen13829532008-05-12 21:20:58 +0200186{
Pekka Paalanen5359b582009-03-01 16:11:58 +0200187 int ret;
Joe Perches1bd591a2009-12-09 10:45:36 -0800188 WARN_ONCE(f->armed, KERN_ERR pr_fmt("kmmio page already armed.\n"));
Pekka Paalanen5359b582009-03-01 16:11:58 +0200189 if (f->armed) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100190 pr_warning("double-arm: addr 0x%08lx, ref %d, old %d\n",
191 f->addr, f->count, !!f->old_presence);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200192 }
Stuart Bennett46e91d02009-04-28 20:17:50 +0100193 ret = clear_page_presence(f, true);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100194 WARN_ONCE(ret < 0, KERN_ERR pr_fmt("arming at 0x%08lx failed.\n"),
195 f->addr);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200196 f->armed = true;
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000197 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200198}
199
Pekka Paalanen5359b582009-03-01 16:11:58 +0200200/** Restore the given page to saved presence state. */
201static void disarm_kmmio_fault_page(struct kmmio_fault_page *f)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200202{
Stuart Bennett46e91d02009-04-28 20:17:50 +0100203 int ret = clear_page_presence(f, false);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200204 WARN_ONCE(ret < 0,
Karol Herbstcfa52c02016-03-03 02:03:11 +0100205 KERN_ERR "kmmio disarming at 0x%08lx failed.\n", f->addr);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200206 f->armed = false;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200207}
208
209/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200210 * This is being called from do_page_fault().
211 *
212 * We may be in an interrupt or a critical section. Also prefecthing may
213 * trigger a page fault. We may be in the middle of process switch.
214 * We cannot take any locks, because we could be executing especially
215 * within a kmmio critical section.
216 *
217 * Local interrupts are disabled, so preemption cannot happen.
218 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
219 */
220/*
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200221 * Interrupts are disabled on entry as trap3 is an interrupt gate
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200222 * and they remain disabled throughout this function.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200223 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200224int kmmio_handler(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200225{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200226 struct kmmio_context *ctx;
227 struct kmmio_fault_page *faultpage;
Pekka Paalanen13829532008-05-12 21:20:58 +0200228 int ret = 0; /* default to fault not handled */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100229 unsigned long page_base = addr;
230 unsigned int l;
231 pte_t *pte = lookup_address(addr, &l);
232 if (!pte)
233 return -EINVAL;
234 page_base &= page_level_mask(l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200235
236 /*
237 * Preemption is now disabled to prevent process switch during
238 * single stepping. We can only handle one active kmmio trace
239 * per cpu, so ensure that we finish it before something else
Pekka Paalanend61fc442008-05-12 21:20:57 +0200240 * gets to run. We also hold the RCU read lock over single
241 * stepping to avoid looking up the probe and kmmio_fault_page
242 * again.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200243 */
244 preempt_disable();
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200245 rcu_read_lock();
Pekka Paalanend61fc442008-05-12 21:20:57 +0200246
Karol Herbstcfa52c02016-03-03 02:03:11 +0100247 faultpage = get_kmmio_fault_page(page_base);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200248 if (!faultpage) {
249 /*
250 * Either this page fault is not caused by kmmio, or
251 * another CPU just pulled the kmmio probe from under
Pekka Paalanen13829532008-05-12 21:20:58 +0200252 * our feet. The latter case should not be possible.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200253 */
254 goto no_kmmio;
255 }
256
257 ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200258 if (ctx->active) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100259 if (page_base == ctx->addr) {
Pekka Paalanen13829532008-05-12 21:20:58 +0200260 /*
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000261 * A second fault on the same page means some other
262 * condition needs handling by do_page_fault(), the
263 * page really not being present is the most common.
Pekka Paalanen13829532008-05-12 21:20:58 +0200264 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800265 pr_debug("secondary hit for 0x%08lx CPU %d.\n",
266 addr, smp_processor_id());
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000267
268 if (!faultpage->old_presence)
Joe Perches1bd591a2009-12-09 10:45:36 -0800269 pr_info("unexpected secondary hit for address 0x%08lx on CPU %d.\n",
270 addr, smp_processor_id());
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000271 } else {
272 /*
273 * Prevent overwriting already in-flight context.
274 * This should not happen, let's hope disarming at
275 * least prevents a panic.
276 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800277 pr_emerg("recursive probe hit on CPU %d, for address 0x%08lx. Ignoring.\n",
278 smp_processor_id(), addr);
279 pr_emerg("previous hit was at 0x%08lx.\n", ctx->addr);
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000280 disarm_kmmio_fault_page(faultpage);
281 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200282 goto no_kmmio_ctx;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200283 }
284 ctx->active++;
285
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200286 ctx->fpage = faultpage;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100287 ctx->probe = get_kmmio_probe(page_base);
Ingo Molnar49023162008-05-12 21:20:58 +0200288 ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Karol Herbstcfa52c02016-03-03 02:03:11 +0100289 ctx->addr = page_base;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200290
291 if (ctx->probe && ctx->probe->pre_handler)
292 ctx->probe->pre_handler(ctx->probe, regs, addr);
293
Pekka Paalanend61fc442008-05-12 21:20:57 +0200294 /*
295 * Enable single-stepping and disable interrupts for the faulting
296 * context. Local interrupts must not get enabled during stepping.
297 */
Ingo Molnar49023162008-05-12 21:20:58 +0200298 regs->flags |= X86_EFLAGS_TF;
299 regs->flags &= ~X86_EFLAGS_IF;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200300
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200301 /* Now we set present bit in PTE and single step. */
Pekka Paalanen5359b582009-03-01 16:11:58 +0200302 disarm_kmmio_fault_page(ctx->fpage);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200303
Pekka Paalanend61fc442008-05-12 21:20:57 +0200304 /*
305 * If another cpu accesses the same page while we are stepping,
306 * the access will not be caught. It will simply succeed and the
307 * only downside is we lose the event. If this becomes a problem,
308 * the user should drop to single cpu before tracing.
309 */
310
Pekka Paalanenf5136382008-05-12 21:20:57 +0200311 put_cpu_var(kmmio_ctx);
Pekka Paalanen13829532008-05-12 21:20:58 +0200312 return 1; /* fault handled */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200313
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200314no_kmmio_ctx:
Pekka Paalanenf5136382008-05-12 21:20:57 +0200315 put_cpu_var(kmmio_ctx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200316no_kmmio:
317 rcu_read_unlock();
318 preempt_enable_no_resched();
Pekka Paalanen13829532008-05-12 21:20:58 +0200319 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200320}
321
322/*
323 * Interrupts are disabled on entry as trap1 is an interrupt gate
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200324 * and they remain disabled throughout this function.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200325 * This must always get called as the pair to kmmio_handler().
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200326 */
327static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
328{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200329 int ret = 0;
330 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200331
Pekka Paalanen13829532008-05-12 21:20:58 +0200332 if (!ctx->active) {
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100333 /*
334 * debug traps without an active context are due to either
335 * something external causing them (f.e. using a debugger while
336 * mmio tracing enabled), or erroneous behaviour
337 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800338 pr_warning("unexpected debug trap on CPU %d.\n",
339 smp_processor_id());
Pekka Paalanenf5136382008-05-12 21:20:57 +0200340 goto out;
Pekka Paalanen13829532008-05-12 21:20:58 +0200341 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200342
343 if (ctx->probe && ctx->probe->post_handler)
344 ctx->probe->post_handler(ctx->probe, condition, regs);
345
Pekka Paalanen340430c2009-02-24 21:44:15 +0200346 /* Prevent racing against release_kmmio_fault_page(). */
347 spin_lock(&kmmio_lock);
348 if (ctx->fpage->count)
349 arm_kmmio_fault_page(ctx->fpage);
350 spin_unlock(&kmmio_lock);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200351
Ingo Molnar49023162008-05-12 21:20:58 +0200352 regs->flags &= ~X86_EFLAGS_TF;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200353 regs->flags |= ctx->saved_flags;
354
355 /* These were acquired in kmmio_handler(). */
356 ctx->active--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200357 BUG_ON(ctx->active);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200358 rcu_read_unlock();
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200359 preempt_enable_no_resched();
360
361 /*
362 * if somebody else is singlestepping across a probe point, flags
363 * will have TF set, in which case, continue the remaining processing
364 * of do_debug, as if this is not a probe hit.
365 */
Ingo Molnar49023162008-05-12 21:20:58 +0200366 if (!(regs->flags & X86_EFLAGS_TF))
Pekka Paalanenf5136382008-05-12 21:20:57 +0200367 ret = 1;
Pekka Paalanenf5136382008-05-12 21:20:57 +0200368out:
369 put_cpu_var(kmmio_ctx);
370 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200371}
372
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200373/* You must be holding kmmio_lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100374static int add_kmmio_fault_page(unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200375{
376 struct kmmio_fault_page *f;
377
Karol Herbstcfa52c02016-03-03 02:03:11 +0100378 f = get_kmmio_fault_page(addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200379 if (f) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200380 if (!f->count)
Pekka Paalanen5359b582009-03-01 16:11:58 +0200381 arm_kmmio_fault_page(f);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200382 f->count++;
383 return 0;
384 }
385
Pekka Paalanen5359b582009-03-01 16:11:58 +0200386 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200387 if (!f)
388 return -1;
389
390 f->count = 1;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100391 f->addr = addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200392
Pekka Paalanen5359b582009-03-01 16:11:58 +0200393 if (arm_kmmio_fault_page(f)) {
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000394 kfree(f);
395 return -1;
396 }
397
Karol Herbstcfa52c02016-03-03 02:03:11 +0100398 list_add_rcu(&f->list, kmmio_page_list(f->addr));
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200399
400 return 0;
401}
402
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200403/* You must be holding kmmio_lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100404static void release_kmmio_fault_page(unsigned long addr,
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200405 struct kmmio_fault_page **release_list)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200406{
407 struct kmmio_fault_page *f;
408
Karol Herbstcfa52c02016-03-03 02:03:11 +0100409 f = get_kmmio_fault_page(addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200410 if (!f)
411 return;
412
413 f->count--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200414 BUG_ON(f->count < 0);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200415 if (!f->count) {
Pekka Paalanen5359b582009-03-01 16:11:58 +0200416 disarm_kmmio_fault_page(f);
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200417 if (!f->scheduled_for_release) {
418 f->release_next = *release_list;
419 *release_list = f;
420 f->scheduled_for_release = true;
421 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200422 }
423}
424
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200425/*
426 * With page-unaligned ioremaps, one or two armed pages may contain
427 * addresses from outside the intended mapping. Events for these addresses
428 * are currently silently dropped. The events may result only from programming
429 * mistakes by accessing addresses before the beginning or past the end of a
430 * mapping.
431 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200432int register_kmmio_probe(struct kmmio_probe *p)
433{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200434 unsigned long flags;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200435 int ret = 0;
436 unsigned long size = 0;
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200437 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100438 unsigned int l;
439 pte_t *pte;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200440
Pekka Paalanend61fc442008-05-12 21:20:57 +0200441 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200442 if (get_kmmio_probe(p->addr)) {
443 ret = -EEXIST;
444 goto out;
445 }
Karol Herbstcfa52c02016-03-03 02:03:11 +0100446
447 pte = lookup_address(p->addr, &l);
448 if (!pte) {
449 ret = -EINVAL;
450 goto out;
451 }
452
Pekka Paalanend61fc442008-05-12 21:20:57 +0200453 kmmio_count++;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200454 list_add_rcu(&p->list, &kmmio_probes);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200455 while (size < size_lim) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200456 if (add_kmmio_fault_page(p->addr + size))
Joe Perches1bd591a2009-12-09 10:45:36 -0800457 pr_err("Unable to set page fault.\n");
Karol Herbstcfa52c02016-03-03 02:03:11 +0100458 size += page_level_size(l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200459 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200460out:
Pekka Paalanend61fc442008-05-12 21:20:57 +0200461 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200462 /*
463 * XXX: What should I do here?
464 * Here was a call to global_flush_tlb(), but it does not exist
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200465 * anymore. It seems it's not needed after all.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200466 */
467 return ret;
468}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200469EXPORT_SYMBOL(register_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200470
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200471static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200472{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200473 struct kmmio_delayed_release *dr = container_of(
474 head,
475 struct kmmio_delayed_release,
476 rcu);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100477 struct kmmio_fault_page *f = dr->release_list;
478 while (f) {
479 struct kmmio_fault_page *next = f->release_next;
480 BUG_ON(f->count);
481 kfree(f);
482 f = next;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200483 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200484 kfree(dr);
485}
486
487static void remove_kmmio_fault_pages(struct rcu_head *head)
488{
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200489 struct kmmio_delayed_release *dr =
490 container_of(head, struct kmmio_delayed_release, rcu);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100491 struct kmmio_fault_page *f = dr->release_list;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200492 struct kmmio_fault_page **prevp = &dr->release_list;
493 unsigned long flags;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200494
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200495 spin_lock_irqsave(&kmmio_lock, flags);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100496 while (f) {
497 if (!f->count) {
498 list_del_rcu(&f->list);
499 prevp = &f->release_next;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200500 } else {
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100501 *prevp = f->release_next;
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200502 f->release_next = NULL;
503 f->scheduled_for_release = false;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200504 }
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200505 f = *prevp;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200506 }
507 spin_unlock_irqrestore(&kmmio_lock, flags);
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200508
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200509 /* This is the real RCU destroy call. */
510 call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200511}
512
513/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200514 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
Pekka Paalanend61fc442008-05-12 21:20:57 +0200515 * sure that the callbacks will not be called anymore. Only after that
516 * you may actually release your struct kmmio_probe.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200517 *
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200518 * Unregistering a kmmio fault page has three steps:
519 * 1. release_kmmio_fault_page()
520 * Disarm the page, wait a grace period to let all faults finish.
521 * 2. remove_kmmio_fault_pages()
522 * Remove the pages from kmmio_page_table.
523 * 3. rcu_free_kmmio_fault_pages()
Shaun Patterson80550392009-12-05 10:41:34 -0500524 * Actually free the kmmio_fault_page structs as with RCU.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200525 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200526void unregister_kmmio_probe(struct kmmio_probe *p)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200527{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200528 unsigned long flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200529 unsigned long size = 0;
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200530 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200531 struct kmmio_fault_page *release_list = NULL;
532 struct kmmio_delayed_release *drelease;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100533 unsigned int l;
534 pte_t *pte;
535
536 pte = lookup_address(p->addr, &l);
537 if (!pte)
538 return;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200539
Pekka Paalanend61fc442008-05-12 21:20:57 +0200540 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200541 while (size < size_lim) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200542 release_kmmio_fault_page(p->addr + size, &release_list);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100543 size += page_level_size(l);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200544 }
545 list_del_rcu(&p->list);
546 kmmio_count--;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200547 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200548
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200549 if (!release_list)
550 return;
551
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200552 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
553 if (!drelease) {
Joe Perches1bd591a2009-12-09 10:45:36 -0800554 pr_crit("leaking kmmio_fault_page objects.\n");
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200555 return;
556 }
557 drelease->release_list = release_list;
558
559 /*
560 * This is not really RCU here. We have just disarmed a set of
561 * pages so that they cannot trigger page faults anymore. However,
562 * we cannot remove the pages from kmmio_page_table,
563 * because a probe hit might be in flight on another CPU. The
564 * pages are collected into a list, and they will be removed from
565 * kmmio_page_table when it is certain that no probe hit related to
566 * these pages can be in flight. RCU grace period sounds like a
567 * good choice.
568 *
569 * If we removed the pages too early, kmmio page fault handler might
570 * not find the respective kmmio_fault_page and determine it's not
571 * a kmmio fault, when it actually is. This would lead to madness.
572 */
573 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200574}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200575EXPORT_SYMBOL(unregister_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200576
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100577static int
578kmmio_die_notifier(struct notifier_block *nb, unsigned long val, void *args)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200579{
580 struct die_args *arg = args;
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100581 unsigned long* dr6_p = (unsigned long *)ERR_PTR(arg->err);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200582
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100583 if (val == DIE_DEBUG && (*dr6_p & DR_STEP))
584 if (post_kmmio_handler(*dr6_p, arg->regs) == 1) {
K.Prasad62edab92009-06-01 23:47:06 +0530585 /*
586 * Reset the BS bit in dr6 (pointed by args->err) to
587 * denote completion of processing
588 */
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100589 *dr6_p &= ~DR_STEP;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200590 return NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +0530591 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200592
593 return NOTIFY_DONE;
594}
Pekka Paalanen13829532008-05-12 21:20:58 +0200595
596static struct notifier_block nb_die = {
597 .notifier_call = kmmio_die_notifier
598};
599
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100600int kmmio_init(void)
Pekka Paalanen13829532008-05-12 21:20:58 +0200601{
602 int i;
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100603
Pekka Paalanen13829532008-05-12 21:20:58 +0200604 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
605 INIT_LIST_HEAD(&kmmio_page_table[i]);
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100606
Pekka Paalanen13829532008-05-12 21:20:58 +0200607 return register_die_notifier(&nb_die);
608}
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100609
610void kmmio_cleanup(void)
611{
612 int i;
613
614 unregister_die_notifier(&nb_die);
615 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++) {
616 WARN_ONCE(!list_empty(&kmmio_page_table[i]),
617 KERN_ERR "kmmio_page_table not empty at cleanup, any further tracing will leak memory.\n");
618 }
619}