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/moduleparam.h>
28 #include <linux/debugfs.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <linux/mmiotrace.h>
34 #include <asm/e820/api.h> /* for ISA_START_ADDRESS */
35 #include <linux/atomic.h>
36 #include <linux/percpu.h>
37 #include <linux/cpu.h>
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 static DEFINE_MUTEX(mmiotrace_mutex
);
60 static DEFINE_SPINLOCK(trace_lock
);
61 static atomic_t mmiotrace_enabled
;
62 static LIST_HEAD(trace_list
); /* struct remap_trace */
65 * Locking in this file:
66 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
67 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
69 * - Routines depending on is_enabled() must take trace_lock.
70 * - trace_list users must hold trace_lock.
71 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
72 * - pre/post callbacks assume the effect of is_enabled() being true.
75 /* module parameters */
76 static unsigned long filter_offset
;
77 static bool nommiotrace
;
80 module_param(filter_offset
, ulong
, 0);
81 module_param(nommiotrace
, bool, 0);
82 module_param(trace_pc
, bool, 0);
84 MODULE_PARM_DESC(filter_offset
, "Start address of traced mappings.");
85 MODULE_PARM_DESC(nommiotrace
, "Disable actual MMIO tracing.");
86 MODULE_PARM_DESC(trace_pc
, "Record address of faulting instructions.");
88 static bool is_enabled(void)
90 return atomic_read(&mmiotrace_enabled
);
93 static void print_pte(unsigned long address
)
96 pte_t
*pte
= lookup_address(address
, &level
);
99 pr_err("Error in %s: no pte for page 0x%08lx\n",
104 if (level
== PG_LEVEL_2M
) {
105 pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
109 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
111 (unsigned long long)pte_val(*pte
),
112 (unsigned long long)pte_val(*pte
) & _PAGE_PRESENT
);
116 * For some reason the pre/post pairs have been called in an
117 * unmatched order. Report and die.
119 static void die_kmmio_nesting_error(struct pt_regs
*regs
, unsigned long addr
)
121 const struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
122 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
123 addr
, my_reason
->addr
);
125 pr_emerg("faulting IP is at %pS\n", (void *)regs
->ip
);
126 pr_emerg("last faulting IP was at %pS\n", (void *)my_reason
->ip
);
128 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
129 regs
->ax
, regs
->bx
, regs
->cx
, regs
->dx
);
130 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
131 regs
->si
, regs
->di
, regs
->bp
, regs
->sp
);
133 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
134 regs
->ax
, regs
->cx
, regs
->dx
);
135 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
136 regs
->si
, regs
->di
, regs
->bp
, regs
->sp
);
138 put_cpu_var(pf_reason
);
142 static void pre(struct kmmio_probe
*p
, struct pt_regs
*regs
,
145 struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
146 struct mmiotrace_rw
*my_trace
= &get_cpu_var(cpu_trace
);
147 const unsigned long instptr
= instruction_pointer(regs
);
148 const enum reason_type type
= get_ins_type(instptr
);
149 struct remap_trace
*trace
= p
->private;
151 /* it doesn't make sense to have more than one active trace per cpu */
152 if (my_reason
->active_traces
)
153 die_kmmio_nesting_error(regs
, addr
);
155 my_reason
->active_traces
++;
157 my_reason
->type
= type
;
158 my_reason
->addr
= addr
;
159 my_reason
->ip
= instptr
;
161 my_trace
->phys
= addr
- trace
->probe
.addr
+ trace
->phys
;
162 my_trace
->map_id
= trace
->id
;
165 * Only record the program counter when requested.
166 * It may taint clean-room reverse engineering.
169 my_trace
->pc
= instptr
;
174 * XXX: the timestamp recorded will be *after* the tracing has been
175 * done, not at the time we hit the instruction. SMP implications
181 my_trace
->opcode
= MMIO_READ
;
182 my_trace
->width
= get_ins_mem_width(instptr
);
185 my_trace
->opcode
= MMIO_WRITE
;
186 my_trace
->width
= get_ins_mem_width(instptr
);
187 my_trace
->value
= get_ins_reg_val(instptr
, regs
);
190 my_trace
->opcode
= MMIO_WRITE
;
191 my_trace
->width
= get_ins_mem_width(instptr
);
192 my_trace
->value
= get_ins_imm_val(instptr
);
196 unsigned char *ip
= (unsigned char *)instptr
;
197 my_trace
->opcode
= MMIO_UNKNOWN_OP
;
199 my_trace
->value
= (*ip
) << 16 | *(ip
+ 1) << 8 |
203 put_cpu_var(cpu_trace
);
204 put_cpu_var(pf_reason
);
207 static void post(struct kmmio_probe
*p
, unsigned long condition
,
208 struct pt_regs
*regs
)
210 struct trap_reason
*my_reason
= &get_cpu_var(pf_reason
);
211 struct mmiotrace_rw
*my_trace
= &get_cpu_var(cpu_trace
);
213 /* this should always return the active_trace count to 0 */
214 my_reason
->active_traces
--;
215 if (my_reason
->active_traces
) {
216 pr_emerg("unexpected post handler");
220 switch (my_reason
->type
) {
222 my_trace
->value
= get_ins_reg_val(my_reason
->ip
, regs
);
228 mmio_trace_rw(my_trace
);
229 put_cpu_var(cpu_trace
);
230 put_cpu_var(pf_reason
);
233 static void ioremap_trace_core(resource_size_t offset
, unsigned long size
,
236 static atomic_t next_id
;
237 struct remap_trace
*trace
= kmalloc(sizeof(*trace
), GFP_KERNEL
);
238 /* These are page-unaligned. */
239 struct mmiotrace_map map
= {
241 .virt
= (unsigned long)addr
,
247 pr_err("kmalloc failed in ioremap\n");
251 *trace
= (struct remap_trace
) {
253 .addr
= (unsigned long)addr
,
256 .post_handler
= post
,
260 .id
= atomic_inc_return(&next_id
)
262 map
.map_id
= trace
->id
;
264 spin_lock_irq(&trace_lock
);
270 mmio_trace_mapping(&map
);
271 list_add_tail(&trace
->list
, &trace_list
);
273 register_kmmio_probe(&trace
->probe
);
276 spin_unlock_irq(&trace_lock
);
279 void mmiotrace_ioremap(resource_size_t offset
, unsigned long size
,
282 if (!is_enabled()) /* recheck and proper locking in *_core() */
285 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
286 (unsigned long long)offset
, size
, addr
);
287 if ((filter_offset
) && (offset
!= filter_offset
))
289 ioremap_trace_core(offset
, size
, addr
);
292 static void iounmap_trace_core(volatile void __iomem
*addr
)
294 struct mmiotrace_map map
= {
296 .virt
= (unsigned long)addr
,
298 .opcode
= MMIO_UNPROBE
300 struct remap_trace
*trace
;
301 struct remap_trace
*tmp
;
302 struct remap_trace
*found_trace
= NULL
;
304 pr_debug("Unmapping %p.\n", addr
);
306 spin_lock_irq(&trace_lock
);
310 list_for_each_entry_safe(trace
, tmp
, &trace_list
, list
) {
311 if ((unsigned long)addr
== trace
->probe
.addr
) {
313 unregister_kmmio_probe(&trace
->probe
);
314 list_del(&trace
->list
);
319 map
.map_id
= (found_trace
) ? found_trace
->id
: -1;
320 mmio_trace_mapping(&map
);
323 spin_unlock_irq(&trace_lock
);
325 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
330 void mmiotrace_iounmap(volatile void __iomem
*addr
)
333 if (is_enabled()) /* recheck and proper locking in *_core() */
334 iounmap_trace_core(addr
);
337 int mmiotrace_printk(const char *fmt
, ...)
344 spin_lock_irqsave(&trace_lock
, flags
);
346 ret
= mmio_trace_printk(fmt
, args
);
347 spin_unlock_irqrestore(&trace_lock
, flags
);
352 EXPORT_SYMBOL(mmiotrace_printk
);
354 static void clear_trace_list(void)
356 struct remap_trace
*trace
;
357 struct remap_trace
*tmp
;
360 * No locking required, because the caller ensures we are in a
361 * critical section via mutex, and is_enabled() is false,
362 * i.e. nothing can traverse or modify this list.
363 * Caller also ensures is_enabled() cannot change.
365 list_for_each_entry(trace
, &trace_list
, list
) {
366 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
367 trace
->probe
.addr
, trace
->probe
.len
);
369 unregister_kmmio_probe(&trace
->probe
);
371 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
373 list_for_each_entry_safe(trace
, tmp
, &trace_list
, list
) {
374 list_del(&trace
->list
);
379 #ifdef CONFIG_HOTPLUG_CPU
380 static cpumask_var_t downed_cpus
;
382 static void enter_uniprocessor(void)
387 if (downed_cpus
== NULL
&&
388 !alloc_cpumask_var(&downed_cpus
, GFP_KERNEL
)) {
389 pr_notice("Failed to allocate mask\n");
394 cpumask_copy(downed_cpus
, cpu_online_mask
);
395 cpumask_clear_cpu(cpumask_first(cpu_online_mask
), downed_cpus
);
396 if (num_online_cpus() > 1)
397 pr_notice("Disabling non-boot CPUs...\n");
400 for_each_cpu(cpu
, downed_cpus
) {
403 pr_info("CPU%d is down.\n", cpu
);
405 pr_err("Error taking CPU%d down: %d\n", cpu
, err
);
408 if (num_online_cpus() > 1)
409 pr_warning("multiple CPUs still online, may miss events.\n");
412 static void leave_uniprocessor(void)
417 if (downed_cpus
== NULL
|| cpumask_weight(downed_cpus
) == 0)
419 pr_notice("Re-enabling CPUs...\n");
420 for_each_cpu(cpu
, downed_cpus
) {
423 pr_info("enabled CPU%d.\n", cpu
);
425 pr_err("cannot re-enable CPU%d: %d\n", cpu
, err
);
429 #else /* !CONFIG_HOTPLUG_CPU */
430 static void enter_uniprocessor(void)
432 if (num_online_cpus() > 1)
433 pr_warning("multiple CPUs are online, may miss events. "
434 "Suggest booting with maxcpus=1 kernel argument.\n");
437 static void leave_uniprocessor(void)
442 void enable_mmiotrace(void)
444 mutex_lock(&mmiotrace_mutex
);
449 pr_info("MMIO tracing disabled.\n");
451 enter_uniprocessor();
452 spin_lock_irq(&trace_lock
);
453 atomic_inc(&mmiotrace_enabled
);
454 spin_unlock_irq(&trace_lock
);
455 pr_info("enabled.\n");
457 mutex_unlock(&mmiotrace_mutex
);
460 void disable_mmiotrace(void)
462 mutex_lock(&mmiotrace_mutex
);
466 spin_lock_irq(&trace_lock
);
467 atomic_dec(&mmiotrace_enabled
);
468 BUG_ON(is_enabled());
469 spin_unlock_irq(&trace_lock
);
471 clear_trace_list(); /* guarantees: no more kmmio callbacks */
472 leave_uniprocessor();
474 pr_info("disabled.\n");
476 mutex_unlock(&mmiotrace_mutex
);