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.
23 #define pr_fmt(fmt) "mmiotrace: " fmt
27 #include <linux/module.h>
28 #include <linux/debugfs.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
32 #include <linux/version.h>
33 #include <linux/kallsyms.h>
34 #include <asm/pgtable.h>
35 #include <linux/mmiotrace.h>
36 #include <asm/e820.h> /* for ISA_START_ADDRESS */
37 #include <asm/atomic.h>
38 #include <linux/percpu.h>
39 #include <linux/cpu.h>
46 enum reason_type type
;
51 struct list_head list
;
52 struct kmmio_probe probe
;
57 /* Accessed per-cpu. */
58 static DEFINE_PER_CPU(struct trap_reason
, pf_reason
);
59 static DEFINE_PER_CPU(struct mmiotrace_rw
, cpu_trace
);
61 static DEFINE_MUTEX(mmiotrace_mutex
);
62 static DEFINE_SPINLOCK(trace_lock
);
63 static atomic_t mmiotrace_enabled
;
64 static LIST_HEAD(trace_list
); /* struct remap_trace */
67 * Locking in this file:
68 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
69 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
71 * - Routines depending on is_enabled() must take trace_lock.
72 * - trace_list users must hold trace_lock.
73 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
74 * - pre/post callbacks assume the effect of is_enabled() being true.
77 /* module parameters */
78 static unsigned long filter_offset
;
79 static int nommiotrace
;
82 module_param(filter_offset
, ulong
, 0);
83 module_param(nommiotrace
, bool, 0);
84 module_param(trace_pc
, bool, 0);
86 MODULE_PARM_DESC(filter_offset
, "Start address of traced mappings.");
87 MODULE_PARM_DESC(nommiotrace
, "Disable actual MMIO tracing.");
88 MODULE_PARM_DESC(trace_pc
, "Record address of faulting instructions.");
90 static bool is_enabled(void)
92 return atomic_read(&mmiotrace_enabled
);
95 static void print_pte(unsigned long address
)
98 pte_t
*pte
= lookup_address(address
, &level
);
101 pr_err("Error in %s: no pte for page 0x%08lx\n",
106 if (level
== PG_LEVEL_2M
) {
107 pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
111 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
113 (unsigned long long)pte_val(*pte
),
114 (unsigned long long)pte_val(*pte
) & _PAGE_PRESENT
);
118 * For some reason the pre/post pairs have been called in an
119 * unmatched order. Report and die.
121 static void die_kmmio_nesting_error(struct pt_regs
*regs
, unsigned long addr
)
123 const struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
124 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
125 addr
, my_reason
->addr
);
127 print_symbol(KERN_EMERG
"faulting IP is at %s\n", regs
->ip
);
128 print_symbol(KERN_EMERG
"last faulting IP was at %s\n", my_reason
->ip
);
130 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
131 regs
->ax
, regs
->bx
, regs
->cx
, regs
->dx
);
132 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
133 regs
->si
, regs
->di
, regs
->bp
, regs
->sp
);
135 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
136 regs
->ax
, regs
->cx
, regs
->dx
);
137 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
138 regs
->si
, regs
->di
, regs
->bp
, regs
->sp
);
140 put_cpu_var(pf_reason
);
144 static void pre(struct kmmio_probe
*p
, struct pt_regs
*regs
,
147 struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
148 struct mmiotrace_rw
*my_trace
= &get_cpu_var(cpu_trace
);
149 const unsigned long instptr
= instruction_pointer(regs
);
150 const enum reason_type type
= get_ins_type(instptr
);
151 struct remap_trace
*trace
= p
->private;
153 /* it doesn't make sense to have more than one active trace per cpu */
154 if (my_reason
->active_traces
)
155 die_kmmio_nesting_error(regs
, addr
);
157 my_reason
->active_traces
++;
159 my_reason
->type
= type
;
160 my_reason
->addr
= addr
;
161 my_reason
->ip
= instptr
;
163 my_trace
->phys
= addr
- trace
->probe
.addr
+ trace
->phys
;
164 my_trace
->map_id
= trace
->id
;
167 * Only record the program counter when requested.
168 * It may taint clean-room reverse engineering.
171 my_trace
->pc
= instptr
;
176 * XXX: the timestamp recorded will be *after* the tracing has been
177 * done, not at the time we hit the instruction. SMP implications
183 my_trace
->opcode
= MMIO_READ
;
184 my_trace
->width
= get_ins_mem_width(instptr
);
187 my_trace
->opcode
= MMIO_WRITE
;
188 my_trace
->width
= get_ins_mem_width(instptr
);
189 my_trace
->value
= get_ins_reg_val(instptr
, regs
);
192 my_trace
->opcode
= MMIO_WRITE
;
193 my_trace
->width
= get_ins_mem_width(instptr
);
194 my_trace
->value
= get_ins_imm_val(instptr
);
198 unsigned char *ip
= (unsigned char *)instptr
;
199 my_trace
->opcode
= MMIO_UNKNOWN_OP
;
201 my_trace
->value
= (*ip
) << 16 | *(ip
+ 1) << 8 |
205 put_cpu_var(cpu_trace
);
206 put_cpu_var(pf_reason
);
209 static void post(struct kmmio_probe
*p
, unsigned long condition
,
210 struct pt_regs
*regs
)
212 struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
213 struct mmiotrace_rw
*my_trace
= &get_cpu_var(cpu_trace
);
215 /* this should always return the active_trace count to 0 */
216 my_reason
->active_traces
--;
217 if (my_reason
->active_traces
) {
218 pr_emerg("unexpected post handler");
222 switch (my_reason
->type
) {
224 my_trace
->value
= get_ins_reg_val(my_reason
->ip
, regs
);
230 mmio_trace_rw(my_trace
);
231 put_cpu_var(cpu_trace
);
232 put_cpu_var(pf_reason
);
235 static void ioremap_trace_core(resource_size_t offset
, unsigned long size
,
238 static atomic_t next_id
;
239 struct remap_trace
*trace
= kmalloc(sizeof(*trace
), GFP_KERNEL
);
240 /* These are page-unaligned. */
241 struct mmiotrace_map map
= {
243 .virt
= (unsigned long)addr
,
249 pr_err("kmalloc failed in ioremap\n");
253 *trace
= (struct remap_trace
) {
255 .addr
= (unsigned long)addr
,
258 .post_handler
= post
,
262 .id
= atomic_inc_return(&next_id
)
264 map
.map_id
= trace
->id
;
266 spin_lock_irq(&trace_lock
);
272 mmio_trace_mapping(&map
);
273 list_add_tail(&trace
->list
, &trace_list
);
275 register_kmmio_probe(&trace
->probe
);
278 spin_unlock_irq(&trace_lock
);
281 void mmiotrace_ioremap(resource_size_t offset
, unsigned long size
,
284 if (!is_enabled()) /* recheck and proper locking in *_core() */
287 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
288 (unsigned long long)offset
, size
, addr
);
289 if ((filter_offset
) && (offset
!= filter_offset
))
291 ioremap_trace_core(offset
, size
, addr
);
294 static void iounmap_trace_core(volatile void __iomem
*addr
)
296 struct mmiotrace_map map
= {
298 .virt
= (unsigned long)addr
,
300 .opcode
= MMIO_UNPROBE
302 struct remap_trace
*trace
;
303 struct remap_trace
*tmp
;
304 struct remap_trace
*found_trace
= NULL
;
306 pr_debug("Unmapping %p.\n", addr
);
308 spin_lock_irq(&trace_lock
);
312 list_for_each_entry_safe(trace
, tmp
, &trace_list
, list
) {
313 if ((unsigned long)addr
== trace
->probe
.addr
) {
315 unregister_kmmio_probe(&trace
->probe
);
316 list_del(&trace
->list
);
321 map
.map_id
= (found_trace
) ? found_trace
->id
: -1;
322 mmio_trace_mapping(&map
);
325 spin_unlock_irq(&trace_lock
);
327 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
332 void mmiotrace_iounmap(volatile void __iomem
*addr
)
335 if (is_enabled()) /* recheck and proper locking in *_core() */
336 iounmap_trace_core(addr
);
339 int mmiotrace_printk(const char *fmt
, ...)
346 spin_lock_irqsave(&trace_lock
, flags
);
348 ret
= mmio_trace_printk(fmt
, args
);
349 spin_unlock_irqrestore(&trace_lock
, flags
);
354 EXPORT_SYMBOL(mmiotrace_printk
);
356 static void clear_trace_list(void)
358 struct remap_trace
*trace
;
359 struct remap_trace
*tmp
;
362 * No locking required, because the caller ensures we are in a
363 * critical section via mutex, and is_enabled() is false,
364 * i.e. nothing can traverse or modify this list.
365 * Caller also ensures is_enabled() cannot change.
367 list_for_each_entry(trace
, &trace_list
, list
) {
368 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
369 trace
->probe
.addr
, trace
->probe
.len
);
371 unregister_kmmio_probe(&trace
->probe
);
373 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
375 list_for_each_entry_safe(trace
, tmp
, &trace_list
, list
) {
376 list_del(&trace
->list
);
381 #ifdef CONFIG_HOTPLUG_CPU
382 static cpumask_var_t downed_cpus
;
384 static void enter_uniprocessor(void)
389 if (downed_cpus
== NULL
&&
390 !alloc_cpumask_var(&downed_cpus
, GFP_KERNEL
)) {
391 pr_notice("Failed to allocate mask\n");
396 cpumask_copy(downed_cpus
, cpu_online_mask
);
397 cpumask_clear_cpu(cpumask_first(cpu_online_mask
), downed_cpus
);
398 if (num_online_cpus() > 1)
399 pr_notice("Disabling non-boot CPUs...\n");
402 for_each_cpu(cpu
, downed_cpus
) {
405 pr_info("CPU%d is down.\n", cpu
);
407 pr_err("Error taking CPU%d down: %d\n", cpu
, err
);
410 if (num_online_cpus() > 1)
411 pr_warning("multiple CPUs still online, may miss events.\n");
414 /* __ref because leave_uniprocessor calls cpu_up which is __cpuinit,
415 but this whole function is ifdefed CONFIG_HOTPLUG_CPU */
416 static void __ref
leave_uniprocessor(void)
421 if (downed_cpus
== NULL
|| cpumask_weight(downed_cpus
) == 0)
423 pr_notice("Re-enabling CPUs...\n");
424 for_each_cpu(cpu
, downed_cpus
) {
427 pr_info("enabled CPU%d.\n", cpu
);
429 pr_err("cannot re-enable CPU%d: %d\n", cpu
, err
);
433 #else /* !CONFIG_HOTPLUG_CPU */
434 static void enter_uniprocessor(void)
436 if (num_online_cpus() > 1)
437 pr_warning("multiple CPUs are online, may miss events. "
438 "Suggest booting with maxcpus=1 kernel argument.\n");
441 static void leave_uniprocessor(void)
446 void enable_mmiotrace(void)
448 mutex_lock(&mmiotrace_mutex
);
453 pr_info("MMIO tracing disabled.\n");
455 enter_uniprocessor();
456 spin_lock_irq(&trace_lock
);
457 atomic_inc(&mmiotrace_enabled
);
458 spin_unlock_irq(&trace_lock
);
459 pr_info("enabled.\n");
461 mutex_unlock(&mmiotrace_mutex
);
464 void disable_mmiotrace(void)
466 mutex_lock(&mmiotrace_mutex
);
470 spin_lock_irq(&trace_lock
);
471 atomic_dec(&mmiotrace_enabled
);
472 BUG_ON(is_enabled());
473 spin_unlock_irq(&trace_lock
);
475 clear_trace_list(); /* guarantees: no more kmmio callbacks */
476 leave_uniprocessor();
478 pr_info("disabled.\n");
480 mutex_unlock(&mmiotrace_mutex
);