blob: c21c2ed046120c8e12d439e71848200f11c41bd5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +02002/* Support for MMIO probes.
3 * Benfit many code from kprobes
4 * (C) 2002 Louis Zhuang <louis.zhuang@intel.com>.
5 * 2007 Alexander Eichner
6 * 2008 Pekka Paalanen <pq@iki.fi>
7 */
8
Joe Perches1bd591a2009-12-09 10:45:36 -08009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020011#include <linux/list.h>
Ingo Molnar668a6c32008-05-19 13:35:24 +020012#include <linux/rculist.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020013#include <linux/spinlock.h>
14#include <linux/hash.h>
Paul Gortmaker4b599fe2016-07-13 20:18:55 -040015#include <linux/export.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020016#include <linux/kernel.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020017#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 Paalanen970e6fa2008-05-12 21:21:03 +020023#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020025#include <asm/cacheflush.h>
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020026#include <asm/tlbflush.h>
Pekka Paalanen970e6fa2008-05-12 21:21:03 +020027#include <linux/errno.h>
Pekka Paalanen13829532008-05-12 21:20:58 +020028#include <asm/debugreg.h>
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;
Karol Herbstcfa52c02016-03-03 02:03:11 +010037 unsigned long addr; /* the requested address */
Stuart Bennett46e91d02009-04-28 20:17:50 +010038 pteval_t old_presence; /* page presence prior to arming */
Pekka Paalanen5359b582009-03-01 16:11:58 +020039 bool armed;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020040
41 /*
42 * Number of times this page has been registered as a part
43 * of a probe. If zero, page is disarmed and this may be freed.
Pekka Paalanen340430c2009-02-24 21:44:15 +020044 * Used only by writers (RCU) and post_kmmio_handler().
45 * Protected by kmmio_lock, when linked into kmmio_page_table.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020046 */
47 int count;
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +020048
49 bool scheduled_for_release;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020050};
51
52struct kmmio_delayed_release {
53 struct rcu_head rcu;
54 struct kmmio_fault_page *release_list;
55};
56
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020057struct kmmio_context {
58 struct kmmio_fault_page *fpage;
59 struct kmmio_probe *probe;
60 unsigned long saved_flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020061 unsigned long addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020062 int active;
63};
64
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020065static DEFINE_SPINLOCK(kmmio_lock);
66
Pekka Paalanen13829532008-05-12 21:20:58 +020067/* Protected by kmmio_lock */
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
Karol Herbstcfa52c02016-03-03 02:03:11 +010074static struct list_head *kmmio_page_list(unsigned long addr)
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020075{
Karol Herbstcfa52c02016-03-03 02:03:11 +010076 unsigned int l;
77 pte_t *pte = lookup_address(addr, &l);
78
79 if (!pte)
80 return NULL;
81 addr &= page_level_mask(l);
82
83 return &kmmio_page_table[hash_long(addr, KMMIO_PAGE_HASH_BITS)];
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020084}
85
Pekka Paalanenf5136382008-05-12 21:20:57 +020086/* Accessed per-cpu */
87static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020088
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020089/*
90 * this is basically a dynamic stabbing problem:
91 * Could use the existing prio tree code or
92 * Possible better implementations:
93 * The Interval Skip List: A Data Structure for Finding All Intervals That
94 * Overlap a Point (might be simple)
95 * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
96 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020097/* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +020098static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
99{
100 struct kmmio_probe *p;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200101 list_for_each_entry_rcu(p, &kmmio_probes, list) {
Stuart Bennett33015c82009-04-28 20:17:48 +0100102 if (addr >= p->addr && addr < (p->addr + p->len))
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200103 return p;
104 }
105 return NULL;
106}
107
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200108/* You must be holding RCU read lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100109static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200110{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200111 struct list_head *head;
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100112 struct kmmio_fault_page *f;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100113 unsigned int l;
114 pte_t *pte = lookup_address(addr, &l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200115
Karol Herbstcfa52c02016-03-03 02:03:11 +0100116 if (!pte)
117 return NULL;
118 addr &= page_level_mask(l);
119 head = kmmio_page_list(addr);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100120 list_for_each_entry_rcu(f, head, list) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100121 if (f->addr == addr)
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100122 return f;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200123 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200124 return NULL;
125}
126
Stuart Bennett46e91d02009-04-28 20:17:50 +0100127static void clear_pmd_presence(pmd_t *pmd, bool clear, pmdval_t *old)
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200128{
129 pmdval_t v = pmd_val(*pmd);
Stuart Bennett46e91d02009-04-28 20:17:50 +0100130 if (clear) {
131 *old = v & _PAGE_PRESENT;
132 v &= ~_PAGE_PRESENT;
133 } else /* presume this has been called with clear==true previously */
134 v |= *old;
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200135 set_pmd(pmd, __pmd(v));
136}
137
Stuart Bennett46e91d02009-04-28 20:17:50 +0100138static void clear_pte_presence(pte_t *pte, bool clear, pteval_t *old)
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200139{
140 pteval_t v = pte_val(*pte);
Stuart Bennett46e91d02009-04-28 20:17:50 +0100141 if (clear) {
142 *old = v & _PAGE_PRESENT;
143 v &= ~_PAGE_PRESENT;
144 } else /* presume this has been called with clear==true previously */
145 v |= *old;
Pekka Paalanen0b700a62009-03-01 16:12:48 +0200146 set_pte_atomic(pte, __pte(v));
147}
148
Stuart Bennett46e91d02009-04-28 20:17:50 +0100149static int clear_page_presence(struct kmmio_fault_page *f, bool clear)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200150{
Pekka Paalanen790e2a22008-05-12 21:21:14 +0200151 unsigned int level;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100152 pte_t *pte = lookup_address(f->addr, &level);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200153
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200154 if (!pte) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100155 pr_err("no pte for addr 0x%08lx\n", f->addr);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000156 return -1;
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200157 }
158
Pekka Paalanen13829532008-05-12 21:20:58 +0200159 switch (level) {
160 case PG_LEVEL_2M:
Stuart Bennett46e91d02009-04-28 20:17:50 +0100161 clear_pmd_presence((pmd_t *)pte, clear, &f->old_presence);
Pekka Paalanen13829532008-05-12 21:20:58 +0200162 break;
Pekka Paalanen13829532008-05-12 21:20:58 +0200163 case PG_LEVEL_4K:
Stuart Bennett46e91d02009-04-28 20:17:50 +0100164 clear_pte_presence(pte, clear, &f->old_presence);
Pekka Paalanen13829532008-05-12 21:20:58 +0200165 break;
Pekka Paalanen13829532008-05-12 21:20:58 +0200166 default:
Joe Perches1bd591a2009-12-09 10:45:36 -0800167 pr_err("unexpected page level 0x%x.\n", level);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000168 return -1;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200169 }
170
Karol Herbstcfa52c02016-03-03 02:03:11 +0100171 __flush_tlb_one(f->addr);
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000172 return 0;
Pekka Paalanen13829532008-05-12 21:20:58 +0200173}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200174
Pekka Paalanen5359b582009-03-01 16:11:58 +0200175/*
176 * Mark the given page as not present. Access to it will trigger a fault.
177 *
178 * Struct kmmio_fault_page is protected by RCU and kmmio_lock, but the
179 * protection is ignored here. RCU read lock is assumed held, so the struct
180 * will not disappear unexpectedly. Furthermore, the caller must guarantee,
181 * that double arming the same virtual address (page) cannot occur.
182 *
183 * Double disarming on the other hand is allowed, and may occur when a fault
184 * and mmiotrace shutdown happen simultaneously.
185 */
186static int arm_kmmio_fault_page(struct kmmio_fault_page *f)
Pekka Paalanen13829532008-05-12 21:20:58 +0200187{
Pekka Paalanen5359b582009-03-01 16:11:58 +0200188 int ret;
Joe Perches1bd591a2009-12-09 10:45:36 -0800189 WARN_ONCE(f->armed, KERN_ERR pr_fmt("kmmio page already armed.\n"));
Pekka Paalanen5359b582009-03-01 16:11:58 +0200190 if (f->armed) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100191 pr_warning("double-arm: addr 0x%08lx, ref %d, old %d\n",
192 f->addr, f->count, !!f->old_presence);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200193 }
Stuart Bennett46e91d02009-04-28 20:17:50 +0100194 ret = clear_page_presence(f, true);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100195 WARN_ONCE(ret < 0, KERN_ERR pr_fmt("arming at 0x%08lx failed.\n"),
196 f->addr);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200197 f->armed = true;
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000198 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200199}
200
Pekka Paalanen5359b582009-03-01 16:11:58 +0200201/** Restore the given page to saved presence state. */
202static void disarm_kmmio_fault_page(struct kmmio_fault_page *f)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200203{
Stuart Bennett46e91d02009-04-28 20:17:50 +0100204 int ret = clear_page_presence(f, false);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200205 WARN_ONCE(ret < 0,
Karol Herbstcfa52c02016-03-03 02:03:11 +0100206 KERN_ERR "kmmio disarming at 0x%08lx failed.\n", f->addr);
Pekka Paalanen5359b582009-03-01 16:11:58 +0200207 f->armed = false;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200208}
209
210/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200211 * This is being called from do_page_fault().
212 *
213 * We may be in an interrupt or a critical section. Also prefecthing may
214 * trigger a page fault. We may be in the middle of process switch.
215 * We cannot take any locks, because we could be executing especially
216 * within a kmmio critical section.
217 *
218 * Local interrupts are disabled, so preemption cannot happen.
219 * Do not enable interrupts, do not sleep, and watch out for other CPUs.
220 */
221/*
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200222 * Interrupts are disabled on entry as trap3 is an interrupt gate
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200223 * and they remain disabled throughout this function.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200224 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200225int kmmio_handler(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200226{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200227 struct kmmio_context *ctx;
228 struct kmmio_fault_page *faultpage;
Pekka Paalanen13829532008-05-12 21:20:58 +0200229 int ret = 0; /* default to fault not handled */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100230 unsigned long page_base = addr;
231 unsigned int l;
232 pte_t *pte = lookup_address(addr, &l);
233 if (!pte)
234 return -EINVAL;
235 page_base &= page_level_mask(l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200236
237 /*
238 * Preemption is now disabled to prevent process switch during
239 * single stepping. We can only handle one active kmmio trace
240 * per cpu, so ensure that we finish it before something else
Pekka Paalanend61fc442008-05-12 21:20:57 +0200241 * gets to run. We also hold the RCU read lock over single
242 * stepping to avoid looking up the probe and kmmio_fault_page
243 * again.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200244 */
245 preempt_disable();
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200246 rcu_read_lock();
Pekka Paalanend61fc442008-05-12 21:20:57 +0200247
Karol Herbstcfa52c02016-03-03 02:03:11 +0100248 faultpage = get_kmmio_fault_page(page_base);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200249 if (!faultpage) {
250 /*
251 * Either this page fault is not caused by kmmio, or
252 * another CPU just pulled the kmmio probe from under
Pekka Paalanen13829532008-05-12 21:20:58 +0200253 * our feet. The latter case should not be possible.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200254 */
255 goto no_kmmio;
256 }
257
258 ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200259 if (ctx->active) {
Karol Herbstcfa52c02016-03-03 02:03:11 +0100260 if (page_base == ctx->addr) {
Pekka Paalanen13829532008-05-12 21:20:58 +0200261 /*
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000262 * A second fault on the same page means some other
263 * condition needs handling by do_page_fault(), the
264 * page really not being present is the most common.
Pekka Paalanen13829532008-05-12 21:20:58 +0200265 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800266 pr_debug("secondary hit for 0x%08lx CPU %d.\n",
267 addr, smp_processor_id());
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000268
269 if (!faultpage->old_presence)
Joe Perches1bd591a2009-12-09 10:45:36 -0800270 pr_info("unexpected secondary hit for address 0x%08lx on CPU %d.\n",
271 addr, smp_processor_id());
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000272 } else {
273 /*
274 * Prevent overwriting already in-flight context.
275 * This should not happen, let's hope disarming at
276 * least prevents a panic.
277 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800278 pr_emerg("recursive probe hit on CPU %d, for address 0x%08lx. Ignoring.\n",
279 smp_processor_id(), addr);
280 pr_emerg("previous hit was at 0x%08lx.\n", ctx->addr);
Stuart Bennett3e39aa12009-02-05 11:02:02 +0000281 disarm_kmmio_fault_page(faultpage);
282 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200283 goto no_kmmio_ctx;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200284 }
285 ctx->active++;
286
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200287 ctx->fpage = faultpage;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100288 ctx->probe = get_kmmio_probe(page_base);
Ingo Molnar49023162008-05-12 21:20:58 +0200289 ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Karol Herbstcfa52c02016-03-03 02:03:11 +0100290 ctx->addr = page_base;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200291
292 if (ctx->probe && ctx->probe->pre_handler)
293 ctx->probe->pre_handler(ctx->probe, regs, addr);
294
Pekka Paalanend61fc442008-05-12 21:20:57 +0200295 /*
296 * Enable single-stepping and disable interrupts for the faulting
297 * context. Local interrupts must not get enabled during stepping.
298 */
Ingo Molnar49023162008-05-12 21:20:58 +0200299 regs->flags |= X86_EFLAGS_TF;
300 regs->flags &= ~X86_EFLAGS_IF;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200301
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200302 /* Now we set present bit in PTE and single step. */
Pekka Paalanen5359b582009-03-01 16:11:58 +0200303 disarm_kmmio_fault_page(ctx->fpage);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200304
Pekka Paalanend61fc442008-05-12 21:20:57 +0200305 /*
306 * If another cpu accesses the same page while we are stepping,
307 * the access will not be caught. It will simply succeed and the
308 * only downside is we lose the event. If this becomes a problem,
309 * the user should drop to single cpu before tracing.
310 */
311
Pekka Paalanenf5136382008-05-12 21:20:57 +0200312 put_cpu_var(kmmio_ctx);
Pekka Paalanen13829532008-05-12 21:20:58 +0200313 return 1; /* fault handled */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200314
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200315no_kmmio_ctx:
Pekka Paalanenf5136382008-05-12 21:20:57 +0200316 put_cpu_var(kmmio_ctx);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200317no_kmmio:
318 rcu_read_unlock();
319 preempt_enable_no_resched();
Pekka Paalanen13829532008-05-12 21:20:58 +0200320 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200321}
322
323/*
324 * Interrupts are disabled on entry as trap1 is an interrupt gate
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200325 * and they remain disabled throughout this function.
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200326 * This must always get called as the pair to kmmio_handler().
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200327 */
328static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
329{
Pekka Paalanenf5136382008-05-12 21:20:57 +0200330 int ret = 0;
331 struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200332
Pekka Paalanen13829532008-05-12 21:20:58 +0200333 if (!ctx->active) {
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100334 /*
335 * debug traps without an active context are due to either
336 * something external causing them (f.e. using a debugger while
337 * mmio tracing enabled), or erroneous behaviour
338 */
Joe Perches1bd591a2009-12-09 10:45:36 -0800339 pr_warning("unexpected debug trap on CPU %d.\n",
340 smp_processor_id());
Pekka Paalanenf5136382008-05-12 21:20:57 +0200341 goto out;
Pekka Paalanen13829532008-05-12 21:20:58 +0200342 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200343
344 if (ctx->probe && ctx->probe->post_handler)
345 ctx->probe->post_handler(ctx->probe, condition, regs);
346
Pekka Paalanen340430c2009-02-24 21:44:15 +0200347 /* Prevent racing against release_kmmio_fault_page(). */
348 spin_lock(&kmmio_lock);
349 if (ctx->fpage->count)
350 arm_kmmio_fault_page(ctx->fpage);
351 spin_unlock(&kmmio_lock);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200352
Ingo Molnar49023162008-05-12 21:20:58 +0200353 regs->flags &= ~X86_EFLAGS_TF;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200354 regs->flags |= ctx->saved_flags;
355
356 /* These were acquired in kmmio_handler(). */
357 ctx->active--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200358 BUG_ON(ctx->active);
Pekka Paalanend61fc442008-05-12 21:20:57 +0200359 rcu_read_unlock();
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200360 preempt_enable_no_resched();
361
362 /*
363 * if somebody else is singlestepping across a probe point, flags
364 * will have TF set, in which case, continue the remaining processing
365 * of do_debug, as if this is not a probe hit.
366 */
Ingo Molnar49023162008-05-12 21:20:58 +0200367 if (!(regs->flags & X86_EFLAGS_TF))
Pekka Paalanenf5136382008-05-12 21:20:57 +0200368 ret = 1;
Pekka Paalanenf5136382008-05-12 21:20:57 +0200369out:
370 put_cpu_var(kmmio_ctx);
371 return ret;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200372}
373
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200374/* You must be holding kmmio_lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100375static int add_kmmio_fault_page(unsigned long addr)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200376{
377 struct kmmio_fault_page *f;
378
Karol Herbstcfa52c02016-03-03 02:03:11 +0100379 f = get_kmmio_fault_page(addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200380 if (f) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200381 if (!f->count)
Pekka Paalanen5359b582009-03-01 16:11:58 +0200382 arm_kmmio_fault_page(f);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200383 f->count++;
384 return 0;
385 }
386
Pekka Paalanen5359b582009-03-01 16:11:58 +0200387 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200388 if (!f)
389 return -1;
390
391 f->count = 1;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100392 f->addr = addr;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200393
Pekka Paalanen5359b582009-03-01 16:11:58 +0200394 if (arm_kmmio_fault_page(f)) {
Stuart Bennette9d54ca2009-01-30 17:38:59 +0000395 kfree(f);
396 return -1;
397 }
398
Karol Herbstcfa52c02016-03-03 02:03:11 +0100399 list_add_rcu(&f->list, kmmio_page_list(f->addr));
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200400
401 return 0;
402}
403
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200404/* You must be holding kmmio_lock. */
Karol Herbstcfa52c02016-03-03 02:03:11 +0100405static void release_kmmio_fault_page(unsigned long addr,
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200406 struct kmmio_fault_page **release_list)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200407{
408 struct kmmio_fault_page *f;
409
Karol Herbstcfa52c02016-03-03 02:03:11 +0100410 f = get_kmmio_fault_page(addr);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200411 if (!f)
412 return;
413
414 f->count--;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200415 BUG_ON(f->count < 0);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200416 if (!f->count) {
Pekka Paalanen5359b582009-03-01 16:11:58 +0200417 disarm_kmmio_fault_page(f);
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200418 if (!f->scheduled_for_release) {
419 f->release_next = *release_list;
420 *release_list = f;
421 f->scheduled_for_release = true;
422 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200423 }
424}
425
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200426/*
427 * With page-unaligned ioremaps, one or two armed pages may contain
428 * addresses from outside the intended mapping. Events for these addresses
429 * are currently silently dropped. The events may result only from programming
430 * mistakes by accessing addresses before the beginning or past the end of a
431 * mapping.
432 */
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200433int register_kmmio_probe(struct kmmio_probe *p)
434{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200435 unsigned long flags;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200436 int ret = 0;
437 unsigned long size = 0;
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200438 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100439 unsigned int l;
440 pte_t *pte;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200441
Pekka Paalanend61fc442008-05-12 21:20:57 +0200442 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200443 if (get_kmmio_probe(p->addr)) {
444 ret = -EEXIST;
445 goto out;
446 }
Karol Herbstcfa52c02016-03-03 02:03:11 +0100447
448 pte = lookup_address(p->addr, &l);
449 if (!pte) {
450 ret = -EINVAL;
451 goto out;
452 }
453
Pekka Paalanend61fc442008-05-12 21:20:57 +0200454 kmmio_count++;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200455 list_add_rcu(&p->list, &kmmio_probes);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200456 while (size < size_lim) {
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200457 if (add_kmmio_fault_page(p->addr + size))
Joe Perches1bd591a2009-12-09 10:45:36 -0800458 pr_err("Unable to set page fault.\n");
Karol Herbstcfa52c02016-03-03 02:03:11 +0100459 size += page_level_size(l);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200460 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200461out:
Pekka Paalanend61fc442008-05-12 21:20:57 +0200462 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200463 /*
464 * XXX: What should I do here?
465 * Here was a call to global_flush_tlb(), but it does not exist
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200466 * anymore. It seems it's not needed after all.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200467 */
468 return ret;
469}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200470EXPORT_SYMBOL(register_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200471
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200472static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200473{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200474 struct kmmio_delayed_release *dr = container_of(
475 head,
476 struct kmmio_delayed_release,
477 rcu);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100478 struct kmmio_fault_page *f = dr->release_list;
479 while (f) {
480 struct kmmio_fault_page *next = f->release_next;
481 BUG_ON(f->count);
482 kfree(f);
483 f = next;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200484 }
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200485 kfree(dr);
486}
487
488static void remove_kmmio_fault_pages(struct rcu_head *head)
489{
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200490 struct kmmio_delayed_release *dr =
491 container_of(head, struct kmmio_delayed_release, rcu);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100492 struct kmmio_fault_page *f = dr->release_list;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200493 struct kmmio_fault_page **prevp = &dr->release_list;
494 unsigned long flags;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200495
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200496 spin_lock_irqsave(&kmmio_lock, flags);
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100497 while (f) {
498 if (!f->count) {
499 list_del_rcu(&f->list);
500 prevp = &f->release_next;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200501 } else {
Stuart Bennett0492e1b2009-04-28 20:17:49 +0100502 *prevp = f->release_next;
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200503 f->release_next = NULL;
504 f->scheduled_for_release = false;
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200505 }
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200506 f = *prevp;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200507 }
508 spin_unlock_irqrestore(&kmmio_lock, flags);
Stuart Bennettd0fc63f2009-03-08 20:21:35 +0200509
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200510 /* This is the real RCU destroy call. */
511 call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200512}
513
514/*
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200515 * Remove a kmmio probe. You have to synchronize_rcu() before you can be
Pekka Paalanend61fc442008-05-12 21:20:57 +0200516 * sure that the callbacks will not be called anymore. Only after that
517 * you may actually release your struct kmmio_probe.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200518 *
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200519 * Unregistering a kmmio fault page has three steps:
520 * 1. release_kmmio_fault_page()
521 * Disarm the page, wait a grace period to let all faults finish.
522 * 2. remove_kmmio_fault_pages()
523 * Remove the pages from kmmio_page_table.
524 * 3. rcu_free_kmmio_fault_pages()
Shaun Patterson80550392009-12-05 10:41:34 -0500525 * Actually free the kmmio_fault_page structs as with RCU.
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200526 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200527void unregister_kmmio_probe(struct kmmio_probe *p)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200528{
Pekka Paalanend61fc442008-05-12 21:20:57 +0200529 unsigned long flags;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200530 unsigned long size = 0;
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200531 const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200532 struct kmmio_fault_page *release_list = NULL;
533 struct kmmio_delayed_release *drelease;
Karol Herbstcfa52c02016-03-03 02:03:11 +0100534 unsigned int l;
535 pte_t *pte;
536
537 pte = lookup_address(p->addr, &l);
538 if (!pte)
539 return;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200540
Pekka Paalanend61fc442008-05-12 21:20:57 +0200541 spin_lock_irqsave(&kmmio_lock, flags);
Pekka Paalanen87e547f2008-05-12 21:21:03 +0200542 while (size < size_lim) {
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200543 release_kmmio_fault_page(p->addr + size, &release_list);
Karol Herbstcfa52c02016-03-03 02:03:11 +0100544 size += page_level_size(l);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200545 }
546 list_del_rcu(&p->list);
547 kmmio_count--;
Pekka Paalanend61fc442008-05-12 21:20:57 +0200548 spin_unlock_irqrestore(&kmmio_lock, flags);
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200549
Marcin Slusarz8b8f79b2010-06-13 23:56:54 +0200550 if (!release_list)
551 return;
552
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200553 drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
554 if (!drelease) {
Joe Perches1bd591a2009-12-09 10:45:36 -0800555 pr_crit("leaking kmmio_fault_page objects.\n");
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200556 return;
557 }
558 drelease->release_list = release_list;
559
560 /*
561 * This is not really RCU here. We have just disarmed a set of
562 * pages so that they cannot trigger page faults anymore. However,
563 * we cannot remove the pages from kmmio_page_table,
564 * because a probe hit might be in flight on another CPU. The
565 * pages are collected into a list, and they will be removed from
566 * kmmio_page_table when it is certain that no probe hit related to
567 * these pages can be in flight. RCU grace period sounds like a
568 * good choice.
569 *
570 * If we removed the pages too early, kmmio page fault handler might
571 * not find the respective kmmio_fault_page and determine it's not
572 * a kmmio fault, when it actually is. This would lead to madness.
573 */
574 call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200575}
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200576EXPORT_SYMBOL(unregister_kmmio_probe);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200577
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100578static int
579kmmio_die_notifier(struct notifier_block *nb, unsigned long val, void *args)
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200580{
581 struct die_args *arg = args;
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100582 unsigned long* dr6_p = (unsigned long *)ERR_PTR(arg->err);
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200583
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100584 if (val == DIE_DEBUG && (*dr6_p & DR_STEP))
585 if (post_kmmio_handler(*dr6_p, arg->regs) == 1) {
K.Prasad62edab92009-06-01 23:47:06 +0530586 /*
587 * Reset the BS bit in dr6 (pointed by args->err) to
588 * denote completion of processing
589 */
Luca Barbieri0bb7a952010-01-16 10:39:30 +0100590 *dr6_p &= ~DR_STEP;
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200591 return NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +0530592 }
Pekka Paalanen8b7d89d2008-05-12 21:20:56 +0200593
594 return NOTIFY_DONE;
595}
Pekka Paalanen13829532008-05-12 21:20:58 +0200596
597static struct notifier_block nb_die = {
598 .notifier_call = kmmio_die_notifier
599};
600
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100601int kmmio_init(void)
Pekka Paalanen13829532008-05-12 21:20:58 +0200602{
603 int i;
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100604
Pekka Paalanen13829532008-05-12 21:20:58 +0200605 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
606 INIT_LIST_HEAD(&kmmio_page_table[i]);
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100607
Pekka Paalanen13829532008-05-12 21:20:58 +0200608 return register_die_notifier(&nb_die);
609}
Stuart Bennett0f9a6232009-04-28 20:17:51 +0100610
611void kmmio_cleanup(void)
612{
613 int i;
614
615 unregister_die_notifier(&nb_die);
616 for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++) {
617 WARN_ONCE(!list_empty(&kmmio_page_table[i]),
618 KERN_ERR "kmmio_page_table not empty at cleanup, any further tracing will leak memory.\n");
619 }
620}