mmio-mod.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2005
 *               Jeff Muizelaar, 2006, 2007
 *               Pekka Paalanen, 2008 <pq@iki.fi>
 *
 * Derived from the read-mod example from relay-examples by Tom Zanussi.
 */
P
Pekka Paalanen 已提交
22 23
#define DEBUG 1

24 25
#include <linux/module.h>
#include <linux/debugfs.h>
P
Pekka Paalanen 已提交
26
#include <linux/uaccess.h>
27 28 29 30 31 32
#include <asm/io.h>
#include <linux/version.h>
#include <linux/kallsyms.h>
#include <asm/pgtable.h>
#include <linux/mmiotrace.h>
#include <asm/e820.h> /* for ISA_START_ADDRESS */
33
#include <asm/atomic.h>
34
#include <linux/percpu.h>
35 36 37

#include "pf_in.h"

P
Pekka Paalanen 已提交
38
#define NAME "mmiotrace: "
39 40 41 42 43 44 45 46

struct trap_reason {
	unsigned long addr;
	unsigned long ip;
	enum reason_type type;
	int active_traces;
};

P
Pekka Paalanen 已提交
47 48 49 50 51 52 53
struct remap_trace {
	struct list_head list;
	struct kmmio_probe probe;
	unsigned long phys;
	unsigned long id;
};

54
/* Accessed per-cpu. */
55
static DEFINE_PER_CPU(struct trap_reason, pf_reason);
P
Pekka Paalanen 已提交
56
static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
57

P
Pekka Paalanen 已提交
58
#if 0 /* XXX: no way gather this info anymore */
59
/* Access to this is not per-cpu. */
60
static DEFINE_PER_CPU(atomic_t, dropped);
P
Pekka Paalanen 已提交
61
#endif
62

P
Pekka Paalanen 已提交
63
static struct dentry *marker_file;
64

P
Pekka Paalanen 已提交
65 66 67 68 69 70 71 72 73 74 75 76
static DEFINE_MUTEX(mmiotrace_mutex);
static DEFINE_SPINLOCK(trace_lock);
static atomic_t mmiotrace_enabled;
static LIST_HEAD(trace_list);		/* struct remap_trace */

/*
 * Locking in this file:
 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
 *   and trace_lock.
 * - Routines depending on is_enabled() must take trace_lock.
 * - trace_list users must hold trace_lock.
P
Pekka Paalanen 已提交
77
 * - is_enabled() guarantees that mmio_trace_record is allowed.
P
Pekka Paalanen 已提交
78 79
 * - pre/post callbacks assume the effect of is_enabled() being true.
 */
80 81

/* module parameters */
P
Pekka Paalanen 已提交
82 83 84
static unsigned long	filter_offset;
static int		nommiotrace;
static int		trace_pc;
85 86 87 88 89 90 91 92

module_param(filter_offset, ulong, 0);
module_param(nommiotrace, bool, 0);
module_param(trace_pc, bool, 0);

MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
P
Pekka Paalanen 已提交
93 94 95 96 97

static bool is_enabled(void)
{
	return atomic_read(&mmiotrace_enabled);
}
98

P
Pekka Paalanen 已提交
99
#if 0 /* XXX: needs rewrite */
100
/*
P
Pekka Paalanen 已提交
101
 * Write callback for the debugfs entry:
102 103
 * Read a marker and write it to the mmio trace log
 */
P
Pekka Paalanen 已提交
104 105
static ssize_t write_marker(struct file *file, const char __user *buffer,
						size_t count, loff_t *ppos)
106 107 108
{
	char *event = NULL;
	struct mm_io_header *headp;
P
Pekka Paalanen 已提交
109
	ssize_t len = (count > 65535) ? 65535 : count;
110 111 112 113 114 115 116 117 118 119 120 121 122 123

	event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
	if (!event)
		return -ENOMEM;

	headp = (struct mm_io_header *)event;
	headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
	headp->data_len = len;

	if (copy_from_user(event + sizeof(*headp), buffer, len)) {
		kfree(event);
		return -EFAULT;
	}

P
Pekka Paalanen 已提交
124
	spin_lock_irq(&trace_lock);
P
Pekka Paalanen 已提交
125
#if 0 /* XXX: convert this to use tracing */
P
Pekka Paalanen 已提交
126 127 128
	if (is_enabled())
		relay_write(chan, event, sizeof(*headp) + len);
	else
P
Pekka Paalanen 已提交
129
#endif
P
Pekka Paalanen 已提交
130 131
		len = -EINVAL;
	spin_unlock_irq(&trace_lock);
132 133 134
	kfree(event);
	return len;
}
P
Pekka Paalanen 已提交
135
#endif
136 137 138

static void print_pte(unsigned long address)
{
139 140 141 142
	int level;
	pte_t *pte = lookup_address(address, &level);

	if (!pte) {
P
Pekka Paalanen 已提交
143
		pr_err(NAME "Error in %s: no pte for page 0x%08lx\n",
144
							__func__, address);
145 146 147 148
		return;
	}

	if (level == PG_LEVEL_2M) {
P
Pekka Paalanen 已提交
149 150
		pr_emerg(NAME "4MB pages are not currently supported: "
							"0x%08lx\n", address);
151 152
		BUG();
	}
R
Randy Dunlap 已提交
153 154 155
	pr_info(NAME "pte for 0x%lx: 0x%llx 0x%llx\n", address,
		(unsigned long long)pte_val(*pte),
		(unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
156 157 158 159 160 161 162 163
}

/*
 * For some reason the pre/post pairs have been called in an
 * unmatched order. Report and die.
 */
static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
{
164
	const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
P
Pekka Paalanen 已提交
165 166
	pr_emerg(NAME "unexpected fault for address: 0x%08lx, "
					"last fault for address: 0x%08lx\n",
167
					addr, my_reason->addr);
168
	print_pte(addr);
P
Pekka Paalanen 已提交
169 170
	print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip);
	print_symbol(KERN_EMERG "last faulting IP was at %s\n", my_reason->ip);
171
#ifdef __i386__
172
	pr_emerg("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
173
			regs->ax, regs->bx, regs->cx, regs->dx);
174
	pr_emerg("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
175 176
			regs->si, regs->di, regs->bp, regs->sp);
#else
177
	pr_emerg("rax: %016lx   rcx: %016lx   rdx: %016lx\n",
178
					regs->ax, regs->cx, regs->dx);
179
	pr_emerg("rsi: %016lx   rdi: %016lx   rbp: %016lx   rsp: %016lx\n",
180 181
				regs->si, regs->di, regs->bp, regs->sp);
#endif
182
	put_cpu_var(pf_reason);
183 184 185 186 187 188
	BUG();
}

static void pre(struct kmmio_probe *p, struct pt_regs *regs,
						unsigned long addr)
{
189
	struct trap_reason *my_reason = &get_cpu_var(pf_reason);
P
Pekka Paalanen 已提交
190
	struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
191 192
	const unsigned long instptr = instruction_pointer(regs);
	const enum reason_type type = get_ins_type(instptr);
P
Pekka Paalanen 已提交
193
	struct remap_trace *trace = p->user_data;
194 195

	/* it doesn't make sense to have more than one active trace per cpu */
196
	if (my_reason->active_traces)
197 198
		die_kmmio_nesting_error(regs, addr);
	else
199
		my_reason->active_traces++;
200

201 202 203
	my_reason->type = type;
	my_reason->addr = addr;
	my_reason->ip = instptr;
204

P
Pekka Paalanen 已提交
205 206
	my_trace->phys = addr - trace->probe.addr + trace->phys;
	my_trace->map_id = trace->id;
207 208 209 210 211 212

	/*
	 * Only record the program counter when requested.
	 * It may taint clean-room reverse engineering.
	 */
	if (trace_pc)
P
Pekka Paalanen 已提交
213
		my_trace->pc = instptr;
214
	else
P
Pekka Paalanen 已提交
215
		my_trace->pc = 0;
216

P
Pekka Paalanen 已提交
217 218 219 220 221
	/*
	 * XXX: the timestamp recorded will be *after* the tracing has been
	 * done, not at the time we hit the instruction. SMP implications
	 * on event ordering?
	 */
222 223 224

	switch (type) {
	case REG_READ:
P
Pekka Paalanen 已提交
225 226
		my_trace->opcode = MMIO_READ;
		my_trace->width = get_ins_mem_width(instptr);
227 228
		break;
	case REG_WRITE:
P
Pekka Paalanen 已提交
229 230 231
		my_trace->opcode = MMIO_WRITE;
		my_trace->width = get_ins_mem_width(instptr);
		my_trace->value = get_ins_reg_val(instptr, regs);
232 233
		break;
	case IMM_WRITE:
P
Pekka Paalanen 已提交
234 235 236
		my_trace->opcode = MMIO_WRITE;
		my_trace->width = get_ins_mem_width(instptr);
		my_trace->value = get_ins_imm_val(instptr);
237 238 239 240
		break;
	default:
		{
			unsigned char *ip = (unsigned char *)instptr;
P
Pekka Paalanen 已提交
241 242 243
			my_trace->opcode = MMIO_UNKNOWN_OP;
			my_trace->width = 0;
			my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
244
								*(ip + 2);
245 246
		}
	}
247 248
	put_cpu_var(cpu_trace);
	put_cpu_var(pf_reason);
249 250 251 252 253
}

static void post(struct kmmio_probe *p, unsigned long condition,
							struct pt_regs *regs)
{
254
	struct trap_reason *my_reason = &get_cpu_var(pf_reason);
P
Pekka Paalanen 已提交
255
	struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
256 257

	/* this should always return the active_trace count to 0 */
258 259
	my_reason->active_traces--;
	if (my_reason->active_traces) {
P
Pekka Paalanen 已提交
260
		pr_emerg(NAME "unexpected post handler");
261 262 263
		BUG();
	}

264
	switch (my_reason->type) {
265
	case REG_READ:
P
Pekka Paalanen 已提交
266
		my_trace->value = get_ins_reg_val(my_reason->ip, regs);
267 268 269 270
		break;
	default:
		break;
	}
P
Pekka Paalanen 已提交
271

P
Pekka Paalanen 已提交
272
	mmio_trace_rw(my_trace);
273 274
	put_cpu_var(cpu_trace);
	put_cpu_var(pf_reason);
275 276
}

P
Pekka Paalanen 已提交
277
static void ioremap_trace_core(unsigned long offset, unsigned long size,
278 279
							void __iomem *addr)
{
P
Pekka Paalanen 已提交
280
	static atomic_t next_id;
281
	struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
P
Pekka Paalanen 已提交
282 283 284 285 286
	struct mmiotrace_map map = {
		.phys = offset,
		.virt = (unsigned long)addr,
		.len = size,
		.opcode = MMIO_PROBE
287 288
	};

P
Pekka Paalanen 已提交
289 290 291 292 293
	if (!trace) {
		pr_err(NAME "kmalloc failed in ioremap\n");
		return;
	}

294 295 296 297 298 299
	*trace = (struct remap_trace) {
		.probe = {
			.addr = (unsigned long)addr,
			.len = size,
			.pre_handler = pre,
			.post_handler = post,
P
Pekka Paalanen 已提交
300 301 302 303
			.user_data = trace
		},
		.phys = offset,
		.id = atomic_inc_return(&next_id)
304
	};
P
Pekka Paalanen 已提交
305
	map.map_id = trace->id;
306

P
Pekka Paalanen 已提交
307 308 309 310
	spin_lock_irq(&trace_lock);
	if (!is_enabled())
		goto not_enabled;

P
Pekka Paalanen 已提交
311
	mmio_trace_mapping(&map);
312 313 314
	list_add_tail(&trace->list, &trace_list);
	if (!nommiotrace)
		register_kmmio_probe(&trace->probe);
P
Pekka Paalanen 已提交
315 316 317

not_enabled:
	spin_unlock_irq(&trace_lock);
318 319
}

P
Pekka Paalanen 已提交
320 321
void
mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
322
{
P
Pekka Paalanen 已提交
323
	if (!is_enabled()) /* recheck and proper locking in *_core() */
324 325
		return;

P
Pekka Paalanen 已提交
326 327
	pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr);
	if ((filter_offset) && (offset != filter_offset))
328
		return;
P
Pekka Paalanen 已提交
329
	ioremap_trace_core(offset, size, addr);
330 331
}

P
Pekka Paalanen 已提交
332
static void iounmap_trace_core(volatile void __iomem *addr)
333
{
P
Pekka Paalanen 已提交
334 335 336 337 338
	struct mmiotrace_map map = {
		.phys = 0,
		.virt = (unsigned long)addr,
		.len = 0,
		.opcode = MMIO_UNPROBE
339 340 341
	};
	struct remap_trace *trace;
	struct remap_trace *tmp;
P
Pekka Paalanen 已提交
342 343 344
	struct remap_trace *found_trace = NULL;

	pr_debug(NAME "Unmapping %p.\n", addr);
345

P
Pekka Paalanen 已提交
346 347 348 349
	spin_lock_irq(&trace_lock);
	if (!is_enabled())
		goto not_enabled;

350 351 352 353 354
	list_for_each_entry_safe(trace, tmp, &trace_list, list) {
		if ((unsigned long)addr == trace->probe.addr) {
			if (!nommiotrace)
				unregister_kmmio_probe(&trace->probe);
			list_del(&trace->list);
P
Pekka Paalanen 已提交
355
			found_trace = trace;
356 357 358
			break;
		}
	}
P
Pekka Paalanen 已提交
359 360
	map.map_id = (found_trace) ? found_trace->id : -1;
	mmio_trace_mapping(&map);
P
Pekka Paalanen 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374

not_enabled:
	spin_unlock_irq(&trace_lock);
	if (found_trace) {
		synchronize_rcu(); /* unregister_kmmio_probe() requirement */
		kfree(found_trace);
	}
}

void mmiotrace_iounmap(volatile void __iomem *addr)
{
	might_sleep();
	if (is_enabled()) /* recheck and proper locking in *_core() */
		iounmap_trace_core(addr);
375 376 377 378 379 380 381
}

static void clear_trace_list(void)
{
	struct remap_trace *trace;
	struct remap_trace *tmp;

P
Pekka Paalanen 已提交
382 383 384 385 386 387 388 389
	/*
	 * No locking required, because the caller ensures we are in a
	 * critical section via mutex, and is_enabled() is false,
	 * i.e. nothing can traverse or modify this list.
	 * Caller also ensures is_enabled() cannot change.
	 */
	list_for_each_entry(trace, &trace_list, list) {
		pr_notice(NAME "purging non-iounmapped "
390 391 392 393
					"trace @0x%08lx, size 0x%lx.\n",
					trace->probe.addr, trace->probe.len);
		if (!nommiotrace)
			unregister_kmmio_probe(&trace->probe);
P
Pekka Paalanen 已提交
394 395 396 397
	}
	synchronize_rcu(); /* unregister_kmmio_probe() requirement */

	list_for_each_entry_safe(trace, tmp, &trace_list, list) {
398 399
		list_del(&trace->list);
		kfree(trace);
P
Pekka Paalanen 已提交
400 401 402
	}
}

P
Pekka Paalanen 已提交
403
#if 0 /* XXX: out of order */
P
Pekka Paalanen 已提交
404 405 406 407
static struct file_operations fops_marker = {
	.owner =	THIS_MODULE,
	.write =	write_marker
};
P
Pekka Paalanen 已提交
408
#endif
P
Pekka Paalanen 已提交
409

P
Pekka Paalanen 已提交
410
void enable_mmiotrace(void)
P
Pekka Paalanen 已提交
411 412 413 414 415
{
	mutex_lock(&mmiotrace_mutex);
	if (is_enabled())
		goto out;

P
Pekka Paalanen 已提交
416
#if 0 /* XXX: tracing does not support text entries */
P
Pekka Paalanen 已提交
417 418 419 420
	marker_file = debugfs_create_file("marker", 0660, dir, NULL,
								&fops_marker);
	if (!marker_file)
		pr_err(NAME "marker file creation failed.\n");
P
Pekka Paalanen 已提交
421
#endif
P
Pekka Paalanen 已提交
422 423 424 425 426 427 428 429 430 431 432

	if (nommiotrace)
		pr_info(NAME "MMIO tracing disabled.\n");
	spin_lock_irq(&trace_lock);
	atomic_inc(&mmiotrace_enabled);
	spin_unlock_irq(&trace_lock);
	pr_info(NAME "enabled.\n");
out:
	mutex_unlock(&mmiotrace_mutex);
}

P
Pekka Paalanen 已提交
433
void disable_mmiotrace(void)
P
Pekka Paalanen 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
	mutex_lock(&mmiotrace_mutex);
	if (!is_enabled())
		goto out;

	spin_lock_irq(&trace_lock);
	atomic_dec(&mmiotrace_enabled);
	BUG_ON(is_enabled());
	spin_unlock_irq(&trace_lock);

	clear_trace_list(); /* guarantees: no more kmmio callbacks */
	if (marker_file) {
		debugfs_remove(marker_file);
		marker_file = NULL;
	}

	pr_info(NAME "disabled.\n");
out:
	mutex_unlock(&mmiotrace_mutex);
453
}