x86 mmiotrace: fix page-unaligned ioremaps
[deliverable/linux.git] / arch / x86 / mm / mmio-mod.c
CommitLineData
8b7d89d0
PP
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) IBM Corporation, 2005
17 * Jeff Muizelaar, 2006, 2007
18 * Pekka Paalanen, 2008 <pq@iki.fi>
19 *
20 * Derived from the read-mod example from relay-examples by Tom Zanussi.
21 */
d61fc448
PP
22#define DEBUG 1
23
8b7d89d0 24#include <linux/module.h>
8b7d89d0 25#include <linux/debugfs.h>
f984b51e 26#include <linux/uaccess.h>
970e6fa0 27#include <linux/io.h>
8b7d89d0
PP
28#include <linux/version.h>
29#include <linux/kallsyms.h>
30#include <asm/pgtable.h>
31#include <linux/mmiotrace.h>
32#include <asm/e820.h> /* for ISA_START_ADDRESS */
fe1ffafa 33#include <asm/atomic.h>
f5136380 34#include <linux/percpu.h>
7423d111 35#include <linux/cpu.h>
8b7d89d0 36
8b7d89d0
PP
37#include "pf_in.h"
38
d61fc448 39#define NAME "mmiotrace: "
8b7d89d0 40
8b7d89d0
PP
41struct trap_reason {
42 unsigned long addr;
43 unsigned long ip;
44 enum reason_type type;
45 int active_traces;
46};
47
d61fc448
PP
48struct remap_trace {
49 struct list_head list;
50 struct kmmio_probe probe;
51 unsigned long phys;
52 unsigned long id;
53};
54
fe1ffafa 55/* Accessed per-cpu. */
f5136380 56static DEFINE_PER_CPU(struct trap_reason, pf_reason);
bd8ac686 57static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
8b7d89d0 58
f984b51e 59#if 0 /* XXX: no way gather this info anymore */
fe1ffafa 60/* Access to this is not per-cpu. */
f5136380 61static DEFINE_PER_CPU(atomic_t, dropped);
f984b51e 62#endif
fe1ffafa 63
d61fc448 64static struct dentry *marker_file;
8b7d89d0 65
d61fc448
PP
66static DEFINE_MUTEX(mmiotrace_mutex);
67static DEFINE_SPINLOCK(trace_lock);
68static atomic_t mmiotrace_enabled;
69static LIST_HEAD(trace_list); /* struct remap_trace */
d61fc448
PP
70
71/*
72 * Locking in this file:
73 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
74 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
75 * and trace_lock.
76 * - Routines depending on is_enabled() must take trace_lock.
77 * - trace_list users must hold trace_lock.
bd8ac686 78 * - is_enabled() guarantees that mmio_trace_record is allowed.
d61fc448
PP
79 * - pre/post callbacks assume the effect of is_enabled() being true.
80 */
8b7d89d0
PP
81
82/* module parameters */
d61fc448
PP
83static unsigned long filter_offset;
84static int nommiotrace;
d61fc448 85static int trace_pc;
8b7d89d0 86
8b7d89d0
PP
87module_param(filter_offset, ulong, 0);
88module_param(nommiotrace, bool, 0);
8b7d89d0
PP
89module_param(trace_pc, bool, 0);
90
8b7d89d0
PP
91MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
92MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
8b7d89d0 93MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
d61fc448
PP
94
95static bool is_enabled(void)
96{
97 return atomic_read(&mmiotrace_enabled);
98}
8b7d89d0 99
bd8ac686 100#if 0 /* XXX: needs rewrite */
8b7d89d0 101/*
d61fc448 102 * Write callback for the debugfs entry:
8b7d89d0
PP
103 * Read a marker and write it to the mmio trace log
104 */
d61fc448
PP
105static ssize_t write_marker(struct file *file, const char __user *buffer,
106 size_t count, loff_t *ppos)
8b7d89d0
PP
107{
108 char *event = NULL;
109 struct mm_io_header *headp;
d61fc448 110 ssize_t len = (count > 65535) ? 65535 : count;
8b7d89d0
PP
111
112 event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
113 if (!event)
114 return -ENOMEM;
115
116 headp = (struct mm_io_header *)event;
117 headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
118 headp->data_len = len;
8b7d89d0
PP
119
120 if (copy_from_user(event + sizeof(*headp), buffer, len)) {
121 kfree(event);
122 return -EFAULT;
123 }
124
d61fc448 125 spin_lock_irq(&trace_lock);
f984b51e 126#if 0 /* XXX: convert this to use tracing */
d61fc448
PP
127 if (is_enabled())
128 relay_write(chan, event, sizeof(*headp) + len);
129 else
f984b51e 130#endif
d61fc448
PP
131 len = -EINVAL;
132 spin_unlock_irq(&trace_lock);
8b7d89d0
PP
133 kfree(event);
134 return len;
135}
bd8ac686 136#endif
8b7d89d0
PP
137
138static void print_pte(unsigned long address)
139{
75bb8835
PP
140 int level;
141 pte_t *pte = lookup_address(address, &level);
142
143 if (!pte) {
d61fc448 144 pr_err(NAME "Error in %s: no pte for page 0x%08lx\n",
0fd0e3da 145 __func__, address);
75bb8835
PP
146 return;
147 }
148
149 if (level == PG_LEVEL_2M) {
d61fc448
PP
150 pr_emerg(NAME "4MB pages are not currently supported: "
151 "0x%08lx\n", address);
8b7d89d0
PP
152 BUG();
153 }
0663bb6c
RD
154 pr_info(NAME "pte for 0x%lx: 0x%llx 0x%llx\n", address,
155 (unsigned long long)pte_val(*pte),
156 (unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
8b7d89d0
PP
157}
158
159/*
160 * For some reason the pre/post pairs have been called in an
161 * unmatched order. Report and die.
162 */
163static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
164{
f5136380 165 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
d61fc448
PP
166 pr_emerg(NAME "unexpected fault for address: 0x%08lx, "
167 "last fault for address: 0x%08lx\n",
f5136380 168 addr, my_reason->addr);
8b7d89d0 169 print_pte(addr);
d61fc448
PP
170 print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip);
171 print_symbol(KERN_EMERG "last faulting IP was at %s\n", my_reason->ip);
8b7d89d0 172#ifdef __i386__
0fd0e3da 173 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
8b7d89d0 174 regs->ax, regs->bx, regs->cx, regs->dx);
0fd0e3da 175 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
8b7d89d0
PP
176 regs->si, regs->di, regs->bp, regs->sp);
177#else
0fd0e3da 178 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
8b7d89d0 179 regs->ax, regs->cx, regs->dx);
0fd0e3da 180 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
8b7d89d0
PP
181 regs->si, regs->di, regs->bp, regs->sp);
182#endif
f5136380 183 put_cpu_var(pf_reason);
8b7d89d0
PP
184 BUG();
185}
186
187static void pre(struct kmmio_probe *p, struct pt_regs *regs,
188 unsigned long addr)
189{
f5136380 190 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 191 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
192 const unsigned long instptr = instruction_pointer(regs);
193 const enum reason_type type = get_ins_type(instptr);
bd8ac686 194 struct remap_trace *trace = p->user_data;
8b7d89d0
PP
195
196 /* it doesn't make sense to have more than one active trace per cpu */
f5136380 197 if (my_reason->active_traces)
8b7d89d0
PP
198 die_kmmio_nesting_error(regs, addr);
199 else
f5136380 200 my_reason->active_traces++;
8b7d89d0 201
f5136380
PP
202 my_reason->type = type;
203 my_reason->addr = addr;
204 my_reason->ip = instptr;
8b7d89d0 205
bd8ac686
PP
206 my_trace->phys = addr - trace->probe.addr + trace->phys;
207 my_trace->map_id = trace->id;
8b7d89d0
PP
208
209 /*
210 * Only record the program counter when requested.
211 * It may taint clean-room reverse engineering.
212 */
213 if (trace_pc)
bd8ac686 214 my_trace->pc = instptr;
8b7d89d0 215 else
bd8ac686 216 my_trace->pc = 0;
8b7d89d0 217
f984b51e
PP
218 /*
219 * XXX: the timestamp recorded will be *after* the tracing has been
220 * done, not at the time we hit the instruction. SMP implications
221 * on event ordering?
222 */
8b7d89d0
PP
223
224 switch (type) {
225 case REG_READ:
bd8ac686
PP
226 my_trace->opcode = MMIO_READ;
227 my_trace->width = get_ins_mem_width(instptr);
8b7d89d0
PP
228 break;
229 case REG_WRITE:
bd8ac686
PP
230 my_trace->opcode = MMIO_WRITE;
231 my_trace->width = get_ins_mem_width(instptr);
232 my_trace->value = get_ins_reg_val(instptr, regs);
8b7d89d0
PP
233 break;
234 case IMM_WRITE:
bd8ac686
PP
235 my_trace->opcode = MMIO_WRITE;
236 my_trace->width = get_ins_mem_width(instptr);
237 my_trace->value = get_ins_imm_val(instptr);
8b7d89d0
PP
238 break;
239 default:
240 {
241 unsigned char *ip = (unsigned char *)instptr;
bd8ac686
PP
242 my_trace->opcode = MMIO_UNKNOWN_OP;
243 my_trace->width = 0;
244 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
f5136380 245 *(ip + 2);
8b7d89d0
PP
246 }
247 }
f5136380
PP
248 put_cpu_var(cpu_trace);
249 put_cpu_var(pf_reason);
8b7d89d0
PP
250}
251
252static void post(struct kmmio_probe *p, unsigned long condition,
253 struct pt_regs *regs)
254{
f5136380 255 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 256 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
257
258 /* this should always return the active_trace count to 0 */
f5136380
PP
259 my_reason->active_traces--;
260 if (my_reason->active_traces) {
d61fc448 261 pr_emerg(NAME "unexpected post handler");
8b7d89d0
PP
262 BUG();
263 }
264
f5136380 265 switch (my_reason->type) {
8b7d89d0 266 case REG_READ:
bd8ac686 267 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
8b7d89d0
PP
268 break;
269 default:
270 break;
271 }
f984b51e 272
bd8ac686 273 mmio_trace_rw(my_trace);
f5136380
PP
274 put_cpu_var(cpu_trace);
275 put_cpu_var(pf_reason);
8b7d89d0
PP
276}
277
d61fc448 278static void ioremap_trace_core(unsigned long offset, unsigned long size,
8b7d89d0
PP
279 void __iomem *addr)
280{
d61fc448 281 static atomic_t next_id;
8b7d89d0 282 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
87e547fe 283 /* These are page-unaligned. */
bd8ac686
PP
284 struct mmiotrace_map map = {
285 .phys = offset,
286 .virt = (unsigned long)addr,
287 .len = size,
288 .opcode = MMIO_PROBE
8b7d89d0 289 };
8b7d89d0 290
d61fc448
PP
291 if (!trace) {
292 pr_err(NAME "kmalloc failed in ioremap\n");
293 return;
294 }
295
8b7d89d0
PP
296 *trace = (struct remap_trace) {
297 .probe = {
298 .addr = (unsigned long)addr,
299 .len = size,
300 .pre_handler = pre,
301 .post_handler = post,
d61fc448
PP
302 .user_data = trace
303 },
304 .phys = offset,
305 .id = atomic_inc_return(&next_id)
8b7d89d0 306 };
bd8ac686 307 map.map_id = trace->id;
8b7d89d0 308
d61fc448
PP
309 spin_lock_irq(&trace_lock);
310 if (!is_enabled())
311 goto not_enabled;
312
bd8ac686 313 mmio_trace_mapping(&map);
8b7d89d0 314 list_add_tail(&trace->list, &trace_list);
8b7d89d0
PP
315 if (!nommiotrace)
316 register_kmmio_probe(&trace->probe);
d61fc448
PP
317
318not_enabled:
319 spin_unlock_irq(&trace_lock);
8b7d89d0
PP
320}
321
d61fc448
PP
322void
323mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
8b7d89d0 324{
d61fc448 325 if (!is_enabled()) /* recheck and proper locking in *_core() */
8b7d89d0
PP
326 return;
327
d61fc448
PP
328 pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr);
329 if ((filter_offset) && (offset != filter_offset))
8b7d89d0 330 return;
d61fc448 331 ioremap_trace_core(offset, size, addr);
8b7d89d0 332}
8b7d89d0 333
d61fc448 334static void iounmap_trace_core(volatile void __iomem *addr)
8b7d89d0 335{
bd8ac686
PP
336 struct mmiotrace_map map = {
337 .phys = 0,
338 .virt = (unsigned long)addr,
339 .len = 0,
340 .opcode = MMIO_UNPROBE
8b7d89d0
PP
341 };
342 struct remap_trace *trace;
343 struct remap_trace *tmp;
d61fc448
PP
344 struct remap_trace *found_trace = NULL;
345
346 pr_debug(NAME "Unmapping %p.\n", addr);
8b7d89d0 347
d61fc448
PP
348 spin_lock_irq(&trace_lock);
349 if (!is_enabled())
350 goto not_enabled;
351
8b7d89d0
PP
352 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
353 if ((unsigned long)addr == trace->probe.addr) {
354 if (!nommiotrace)
355 unregister_kmmio_probe(&trace->probe);
356 list_del(&trace->list);
d61fc448 357 found_trace = trace;
8b7d89d0
PP
358 break;
359 }
360 }
bd8ac686
PP
361 map.map_id = (found_trace) ? found_trace->id : -1;
362 mmio_trace_mapping(&map);
d61fc448
PP
363
364not_enabled:
365 spin_unlock_irq(&trace_lock);
366 if (found_trace) {
367 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
368 kfree(found_trace);
369 }
370}
371
372void mmiotrace_iounmap(volatile void __iomem *addr)
373{
374 might_sleep();
375 if (is_enabled()) /* recheck and proper locking in *_core() */
376 iounmap_trace_core(addr);
8b7d89d0 377}
8b7d89d0
PP
378
379static void clear_trace_list(void)
380{
381 struct remap_trace *trace;
382 struct remap_trace *tmp;
383
d61fc448
PP
384 /*
385 * No locking required, because the caller ensures we are in a
386 * critical section via mutex, and is_enabled() is false,
387 * i.e. nothing can traverse or modify this list.
388 * Caller also ensures is_enabled() cannot change.
389 */
390 list_for_each_entry(trace, &trace_list, list) {
391 pr_notice(NAME "purging non-iounmapped "
8b7d89d0
PP
392 "trace @0x%08lx, size 0x%lx.\n",
393 trace->probe.addr, trace->probe.len);
394 if (!nommiotrace)
395 unregister_kmmio_probe(&trace->probe);
d61fc448
PP
396 }
397 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
398
399 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
8b7d89d0
PP
400 list_del(&trace->list);
401 kfree(trace);
d61fc448
PP
402 }
403}
404
7423d111
PP
405#ifdef CONFIG_HOTPLUG_CPU
406static cpumask_t downed_cpus;
407
408static void enter_uniprocessor(void)
409{
410 int cpu;
411 int err;
412
413 get_online_cpus();
414 downed_cpus = cpu_online_map;
415 cpu_clear(first_cpu(cpu_online_map), downed_cpus);
416 if (num_online_cpus() > 1)
417 pr_notice(NAME "Disabling non-boot CPUs...\n");
418 put_online_cpus();
419
420 for_each_cpu_mask(cpu, downed_cpus) {
421 err = cpu_down(cpu);
970e6fa0 422 if (!err)
7423d111 423 pr_info(NAME "CPU%d is down.\n", cpu);
970e6fa0 424 else
7423d111 425 pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err);
7423d111
PP
426 }
427 if (num_online_cpus() > 1)
428 pr_warning(NAME "multiple CPUs still online, "
429 "may miss events.\n");
430}
431
432static void leave_uniprocessor(void)
433{
434 int cpu;
435 int err;
436
437 if (cpus_weight(downed_cpus) == 0)
438 return;
439 pr_notice(NAME "Re-enabling CPUs...\n");
440 for_each_cpu_mask(cpu, downed_cpus) {
441 err = cpu_up(cpu);
442 if (!err)
443 pr_info(NAME "enabled CPU%d.\n", cpu);
444 else
445 pr_err(NAME "cannot re-enable CPU%d: %d\n", cpu, err);
446 }
447}
448
449#else /* !CONFIG_HOTPLUG_CPU */
450static void enter_uniprocessor(void)
451{
452 if (num_online_cpus() > 1)
453 pr_warning(NAME "multiple CPUs are online, may miss events. "
454 "Suggest booting with maxcpus=1 kernel argument.\n");
455}
456
457static void leave_uniprocessor(void)
458{
459}
460#endif
461
bd8ac686 462#if 0 /* XXX: out of order */
d61fc448
PP
463static struct file_operations fops_marker = {
464 .owner = THIS_MODULE,
465 .write = write_marker
466};
bd8ac686 467#endif
d61fc448 468
f984b51e 469void enable_mmiotrace(void)
d61fc448
PP
470{
471 mutex_lock(&mmiotrace_mutex);
472 if (is_enabled())
473 goto out;
474
f984b51e 475#if 0 /* XXX: tracing does not support text entries */
d61fc448
PP
476 marker_file = debugfs_create_file("marker", 0660, dir, NULL,
477 &fops_marker);
478 if (!marker_file)
479 pr_err(NAME "marker file creation failed.\n");
bd8ac686 480#endif
d61fc448
PP
481
482 if (nommiotrace)
483 pr_info(NAME "MMIO tracing disabled.\n");
7423d111 484 enter_uniprocessor();
d61fc448
PP
485 spin_lock_irq(&trace_lock);
486 atomic_inc(&mmiotrace_enabled);
487 spin_unlock_irq(&trace_lock);
488 pr_info(NAME "enabled.\n");
489out:
490 mutex_unlock(&mmiotrace_mutex);
491}
492
f984b51e 493void disable_mmiotrace(void)
d61fc448
PP
494{
495 mutex_lock(&mmiotrace_mutex);
496 if (!is_enabled())
497 goto out;
498
499 spin_lock_irq(&trace_lock);
500 atomic_dec(&mmiotrace_enabled);
501 BUG_ON(is_enabled());
502 spin_unlock_irq(&trace_lock);
503
504 clear_trace_list(); /* guarantees: no more kmmio callbacks */
7423d111 505 leave_uniprocessor();
d61fc448
PP
506 if (marker_file) {
507 debugfs_remove(marker_file);
508 marker_file = NULL;
509 }
d61fc448
PP
510
511 pr_info(NAME "disabled.\n");
512out:
513 mutex_unlock(&mmiotrace_mutex);
8b7d89d0 514}
This page took 0.054599 seconds and 5 git commands to generate.