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.
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.
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.
16 * Copyright (C) IBM Corporation, 2005
17 * Jeff Muizelaar, 2006, 2007
18 * Pekka Paalanen, 2008 <pq@iki.fi>
20 * Derived from the read-mod example from relay-examples by Tom Zanussi.
24 #include <linux/module.h>
25 #include <linux/debugfs.h>
26 #include <linux/uaccess.h>
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 */
33 #include <asm/atomic.h>
34 #include <linux/percpu.h>
35 #include <linux/cpu.h>
39 #define NAME "mmiotrace: "
44 enum reason_type type;
49 struct list_head list;
50 struct kmmio_probe probe;
55 /* Accessed per-cpu. */
56 static DEFINE_PER_CPU(struct trap_reason, pf_reason);
57 static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
59 #if 0 /* XXX: no way gather this info anymore */
60 /* Access to this is not per-cpu. */
61 static DEFINE_PER_CPU(atomic_t, dropped);
64 static struct dentry *marker_file;
66 static DEFINE_MUTEX(mmiotrace_mutex);
67 static DEFINE_SPINLOCK(trace_lock);
68 static atomic_t mmiotrace_enabled;
69 static LIST_HEAD(trace_list); /* struct remap_trace */
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
76 * - Routines depending on is_enabled() must take trace_lock.
77 * - trace_list users must hold trace_lock.
78 * - is_enabled() guarantees that mmio_trace_record is allowed.
79 * - pre/post callbacks assume the effect of is_enabled() being true.
82 /* module parameters */
83 static unsigned long filter_offset;
84 static int nommiotrace;
87 module_param(filter_offset, ulong, 0);
88 module_param(nommiotrace, bool, 0);
89 module_param(trace_pc, bool, 0);
91 MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
92 MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
93 MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
95 static bool is_enabled(void)
97 return atomic_read(&mmiotrace_enabled);
100 #if 0 /* XXX: needs rewrite */
102 * Write callback for the debugfs entry:
103 * Read a marker and write it to the mmio trace log
105 static ssize_t write_marker(struct file *file, const char __user *buffer,
106 size_t count, loff_t *ppos)
109 struct mm_io_header *headp;
110 ssize_t len = (count > 65535) ? 65535 : count;
112 event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
116 headp = (struct mm_io_header *)event;
117 headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
118 headp->data_len = len;
120 if (copy_from_user(event + sizeof(*headp), buffer, len)) {
125 spin_lock_irq(&trace_lock);
126 #if 0 /* XXX: convert this to use tracing */
128 relay_write(chan, event, sizeof(*headp) + len);
132 spin_unlock_irq(&trace_lock);
138 static void print_pte(unsigned long address)
141 pte_t *pte = lookup_address(address, &level);
144 pr_err(NAME "Error in %s: no pte for page 0x%08lx\n",
149 if (level == PG_LEVEL_2M) {
150 pr_emerg(NAME "4MB pages are not currently supported: "
151 "0x%08lx\n", address);
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);
160 * For some reason the pre/post pairs have been called in an
161 * unmatched order. Report and die.
163 static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
165 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
166 pr_emerg(NAME "unexpected fault for address: 0x%08lx, "
167 "last fault for address: 0x%08lx\n",
168 addr, my_reason->addr);
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);
173 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
174 regs->ax, regs->bx, regs->cx, regs->dx);
175 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
176 regs->si, regs->di, regs->bp, regs->sp);
178 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
179 regs->ax, regs->cx, regs->dx);
180 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
181 regs->si, regs->di, regs->bp, regs->sp);
183 put_cpu_var(pf_reason);
187 static void pre(struct kmmio_probe *p, struct pt_regs *regs,
190 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
191 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
192 const unsigned long instptr = instruction_pointer(regs);
193 const enum reason_type type = get_ins_type(instptr);
194 struct remap_trace *trace = p->private;
196 /* it doesn't make sense to have more than one active trace per cpu */
197 if (my_reason->active_traces)
198 die_kmmio_nesting_error(regs, addr);
200 my_reason->active_traces++;
202 my_reason->type = type;
203 my_reason->addr = addr;
204 my_reason->ip = instptr;
206 my_trace->phys = addr - trace->probe.addr + trace->phys;
207 my_trace->map_id = trace->id;
210 * Only record the program counter when requested.
211 * It may taint clean-room reverse engineering.
214 my_trace->pc = instptr;
219 * XXX: the timestamp recorded will be *after* the tracing has been
220 * done, not at the time we hit the instruction. SMP implications
226 my_trace->opcode = MMIO_READ;
227 my_trace->width = get_ins_mem_width(instptr);
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);
235 my_trace->opcode = MMIO_WRITE;
236 my_trace->width = get_ins_mem_width(instptr);
237 my_trace->value = get_ins_imm_val(instptr);
241 unsigned char *ip = (unsigned char *)instptr;
242 my_trace->opcode = MMIO_UNKNOWN_OP;
244 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
248 put_cpu_var(cpu_trace);
249 put_cpu_var(pf_reason);
252 static void post(struct kmmio_probe *p, unsigned long condition,
253 struct pt_regs *regs)
255 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
256 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
258 /* this should always return the active_trace count to 0 */
259 my_reason->active_traces--;
260 if (my_reason->active_traces) {
261 pr_emerg(NAME "unexpected post handler");
265 switch (my_reason->type) {
267 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
273 mmio_trace_rw(my_trace);
274 put_cpu_var(cpu_trace);
275 put_cpu_var(pf_reason);
278 static void ioremap_trace_core(resource_size_t offset, unsigned long size,
281 static atomic_t next_id;
282 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
283 /* These are page-unaligned. */
284 struct mmiotrace_map map = {
286 .virt = (unsigned long)addr,
292 pr_err(NAME "kmalloc failed in ioremap\n");
296 *trace = (struct remap_trace) {
298 .addr = (unsigned long)addr,
301 .post_handler = post,
305 .id = atomic_inc_return(&next_id)
307 map.map_id = trace->id;
309 spin_lock_irq(&trace_lock);
313 mmio_trace_mapping(&map);
314 list_add_tail(&trace->list, &trace_list);
316 register_kmmio_probe(&trace->probe);
319 spin_unlock_irq(&trace_lock);
322 void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
325 if (!is_enabled()) /* recheck and proper locking in *_core() */
328 pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n",
329 (unsigned long long)offset, size, addr);
330 if ((filter_offset) && (offset != filter_offset))
332 ioremap_trace_core(offset, size, addr);
335 static void iounmap_trace_core(volatile void __iomem *addr)
337 struct mmiotrace_map map = {
339 .virt = (unsigned long)addr,
341 .opcode = MMIO_UNPROBE
343 struct remap_trace *trace;
344 struct remap_trace *tmp;
345 struct remap_trace *found_trace = NULL;
347 pr_debug(NAME "Unmapping %p.\n", addr);
349 spin_lock_irq(&trace_lock);
353 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
354 if ((unsigned long)addr == trace->probe.addr) {
356 unregister_kmmio_probe(&trace->probe);
357 list_del(&trace->list);
362 map.map_id = (found_trace) ? found_trace->id : -1;
363 mmio_trace_mapping(&map);
366 spin_unlock_irq(&trace_lock);
368 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
373 void mmiotrace_iounmap(volatile void __iomem *addr)
376 if (is_enabled()) /* recheck and proper locking in *_core() */
377 iounmap_trace_core(addr);
380 static void clear_trace_list(void)
382 struct remap_trace *trace;
383 struct remap_trace *tmp;
386 * No locking required, because the caller ensures we are in a
387 * critical section via mutex, and is_enabled() is false,
388 * i.e. nothing can traverse or modify this list.
389 * Caller also ensures is_enabled() cannot change.
391 list_for_each_entry(trace, &trace_list, list) {
392 pr_notice(NAME "purging non-iounmapped "
393 "trace @0x%08lx, size 0x%lx.\n",
394 trace->probe.addr, trace->probe.len);
396 unregister_kmmio_probe(&trace->probe);
398 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
400 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
401 list_del(&trace->list);
406 #ifdef CONFIG_HOTPLUG_CPU
407 static cpumask_t downed_cpus;
409 static void enter_uniprocessor(void)
415 downed_cpus = cpu_online_map;
416 cpu_clear(first_cpu(cpu_online_map), downed_cpus);
417 if (num_online_cpus() > 1)
418 pr_notice(NAME "Disabling non-boot CPUs...\n");
421 for_each_cpu_mask(cpu, downed_cpus) {
424 pr_info(NAME "CPU%d is down.\n", cpu);
426 pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err);
428 if (num_online_cpus() > 1)
429 pr_warning(NAME "multiple CPUs still online, "
430 "may miss events.\n");
433 static void leave_uniprocessor(void)
438 if (cpus_weight(downed_cpus) == 0)
440 pr_notice(NAME "Re-enabling CPUs...\n");
441 for_each_cpu_mask(cpu, downed_cpus) {
444 pr_info(NAME "enabled CPU%d.\n", cpu);
446 pr_err(NAME "cannot re-enable CPU%d: %d\n", cpu, err);
450 #else /* !CONFIG_HOTPLUG_CPU */
451 static void enter_uniprocessor(void)
453 if (num_online_cpus() > 1)
454 pr_warning(NAME "multiple CPUs are online, may miss events. "
455 "Suggest booting with maxcpus=1 kernel argument.\n");
458 static void leave_uniprocessor(void)
463 #if 0 /* XXX: out of order */
464 static struct file_operations fops_marker = {
465 .owner = THIS_MODULE,
466 .write = write_marker
470 void enable_mmiotrace(void)
472 mutex_lock(&mmiotrace_mutex);
476 #if 0 /* XXX: tracing does not support text entries */
477 marker_file = debugfs_create_file("marker", 0660, dir, NULL,
480 pr_err(NAME "marker file creation failed.\n");
484 pr_info(NAME "MMIO tracing disabled.\n");
485 enter_uniprocessor();
486 spin_lock_irq(&trace_lock);
487 atomic_inc(&mmiotrace_enabled);
488 spin_unlock_irq(&trace_lock);
489 pr_info(NAME "enabled.\n");
491 mutex_unlock(&mmiotrace_mutex);
494 void disable_mmiotrace(void)
496 mutex_lock(&mmiotrace_mutex);
500 spin_lock_irq(&trace_lock);
501 atomic_dec(&mmiotrace_enabled);
502 BUG_ON(is_enabled());
503 spin_unlock_irq(&trace_lock);
505 clear_trace_list(); /* guarantees: no more kmmio callbacks */
506 leave_uniprocessor();
508 debugfs_remove(marker_file);
512 pr_info(NAME "disabled.\n");
514 mutex_unlock(&mmiotrace_mutex);