mmio-mod.c 11.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9
/*
 *
 * 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.
 */
J
Joe Perches 已提交
10

11
#define pr_fmt(fmt) "mmiotrace: " fmt
J
Joe Perches 已提交
12

P
Pekka Paalanen 已提交
13 14
#define DEBUG 1

15
#include <linux/moduleparam.h>
16
#include <linux/debugfs.h>
17
#include <linux/slab.h>
P
Pekka Paalanen 已提交
18
#include <linux/uaccess.h>
P
Pekka Paalanen 已提交
19
#include <linux/io.h>
20 21
#include <asm/pgtable.h>
#include <linux/mmiotrace.h>
22
#include <asm/e820/api.h> /* for ISA_START_ADDRESS */
A
Arun Sharma 已提交
23
#include <linux/atomic.h>
24
#include <linux/percpu.h>
25
#include <linux/cpu.h>
26 27 28 29 30 31 32 33 34 35

#include "pf_in.h"

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

P
Pekka Paalanen 已提交
36 37 38
struct remap_trace {
	struct list_head list;
	struct kmmio_probe probe;
39
	resource_size_t phys;
P
Pekka Paalanen 已提交
40 41 42
	unsigned long id;
};

43
/* Accessed per-cpu. */
44
static DEFINE_PER_CPU(struct trap_reason, pf_reason);
P
Pekka Paalanen 已提交
45
static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
46

P
Pekka Paalanen 已提交
47 48 49 50 51 52 53 54 55 56 57 58
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.
59
 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
P
Pekka Paalanen 已提交
60 61
 * - pre/post callbacks assume the effect of is_enabled() being true.
 */
62 63

/* module parameters */
P
Pekka Paalanen 已提交
64
static unsigned long	filter_offset;
65 66
static bool		nommiotrace;
static bool		trace_pc;
67 68 69 70 71 72 73 74

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 已提交
75 76 77 78 79

static bool is_enabled(void)
{
	return atomic_read(&mmiotrace_enabled);
}
80 81 82

static void print_pte(unsigned long address)
{
83
	unsigned int level;
84 85 86
	pte_t *pte = lookup_address(address, &level);

	if (!pte) {
J
Joe Perches 已提交
87 88
		pr_err("Error in %s: no pte for page 0x%08lx\n",
		       __func__, address);
89 90 91 92
		return;
	}

	if (level == PG_LEVEL_2M) {
J
Joe Perches 已提交
93 94
		pr_emerg("4MB pages are not currently supported: 0x%08lx\n",
			 address);
95 96
		BUG();
	}
J
Joe Perches 已提交
97 98
	pr_info("pte for 0x%lx: 0x%llx 0x%llx\n",
		address,
R
Randy Dunlap 已提交
99 100
		(unsigned long long)pte_val(*pte),
		(unsigned long long)pte_val(*pte) & _PAGE_PRESENT);
101 102 103 104 105 106 107 108
}

/*
 * 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)
{
109
	const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
J
Joe Perches 已提交
110 111
	pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n",
		 addr, my_reason->addr);
112
	print_pte(addr);
113 114
	pr_emerg("faulting IP is at %pS\n", (void *)regs->ip);
	pr_emerg("last faulting IP was at %pS\n", (void *)my_reason->ip);
115
#ifdef __i386__
116
	pr_emerg("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
J
Joe Perches 已提交
117
		 regs->ax, regs->bx, regs->cx, regs->dx);
118
	pr_emerg("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
J
Joe Perches 已提交
119
		 regs->si, regs->di, regs->bp, regs->sp);
120
#else
121
	pr_emerg("rax: %016lx   rcx: %016lx   rdx: %016lx\n",
J
Joe Perches 已提交
122
		 regs->ax, regs->cx, regs->dx);
123
	pr_emerg("rsi: %016lx   rdi: %016lx   rbp: %016lx   rsp: %016lx\n",
J
Joe Perches 已提交
124
		 regs->si, regs->di, regs->bp, regs->sp);
125
#endif
126
	put_cpu_var(pf_reason);
127 128 129 130 131 132
	BUG();
}

static void pre(struct kmmio_probe *p, struct pt_regs *regs,
						unsigned long addr)
{
133
	struct trap_reason *my_reason = &get_cpu_var(pf_reason);
P
Pekka Paalanen 已提交
134
	struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
135 136
	const unsigned long instptr = instruction_pointer(regs);
	const enum reason_type type = get_ins_type(instptr);
137
	struct remap_trace *trace = p->private;
138 139

	/* it doesn't make sense to have more than one active trace per cpu */
140
	if (my_reason->active_traces)
141 142
		die_kmmio_nesting_error(regs, addr);
	else
143
		my_reason->active_traces++;
144

145 146 147
	my_reason->type = type;
	my_reason->addr = addr;
	my_reason->ip = instptr;
148

P
Pekka Paalanen 已提交
149 150
	my_trace->phys = addr - trace->probe.addr + trace->phys;
	my_trace->map_id = trace->id;
151 152 153 154 155 156

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

P
Pekka Paalanen 已提交
161 162 163 164 165
	/*
	 * 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?
	 */
166 167 168

	switch (type) {
	case REG_READ:
P
Pekka Paalanen 已提交
169 170
		my_trace->opcode = MMIO_READ;
		my_trace->width = get_ins_mem_width(instptr);
171 172
		break;
	case REG_WRITE:
P
Pekka Paalanen 已提交
173 174 175
		my_trace->opcode = MMIO_WRITE;
		my_trace->width = get_ins_mem_width(instptr);
		my_trace->value = get_ins_reg_val(instptr, regs);
176 177
		break;
	case IMM_WRITE:
P
Pekka Paalanen 已提交
178 179 180
		my_trace->opcode = MMIO_WRITE;
		my_trace->width = get_ins_mem_width(instptr);
		my_trace->value = get_ins_imm_val(instptr);
181 182 183 184
		break;
	default:
		{
			unsigned char *ip = (unsigned char *)instptr;
P
Pekka Paalanen 已提交
185 186 187
			my_trace->opcode = MMIO_UNKNOWN_OP;
			my_trace->width = 0;
			my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
188
								*(ip + 2);
189 190
		}
	}
191 192
	put_cpu_var(cpu_trace);
	put_cpu_var(pf_reason);
193 194 195 196 197
}

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

	/* this should always return the active_trace count to 0 */
202 203
	my_reason->active_traces--;
	if (my_reason->active_traces) {
J
Joe Perches 已提交
204
		pr_emerg("unexpected post handler");
205 206 207
		BUG();
	}

208
	switch (my_reason->type) {
209
	case REG_READ:
P
Pekka Paalanen 已提交
210
		my_trace->value = get_ins_reg_val(my_reason->ip, regs);
211 212 213 214
		break;
	default:
		break;
	}
P
Pekka Paalanen 已提交
215

P
Pekka Paalanen 已提交
216
	mmio_trace_rw(my_trace);
217 218
	put_cpu_var(cpu_trace);
	put_cpu_var(pf_reason);
219 220
}

221
static void ioremap_trace_core(resource_size_t offset, unsigned long size,
222 223
							void __iomem *addr)
{
P
Pekka Paalanen 已提交
224
	static atomic_t next_id;
225
	struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
226
	/* These are page-unaligned. */
P
Pekka Paalanen 已提交
227 228 229 230 231
	struct mmiotrace_map map = {
		.phys = offset,
		.virt = (unsigned long)addr,
		.len = size,
		.opcode = MMIO_PROBE
232 233
	};

P
Pekka Paalanen 已提交
234
	if (!trace) {
J
Joe Perches 已提交
235
		pr_err("kmalloc failed in ioremap\n");
P
Pekka Paalanen 已提交
236 237 238
		return;
	}

239 240 241 242 243 244
	*trace = (struct remap_trace) {
		.probe = {
			.addr = (unsigned long)addr,
			.len = size,
			.pre_handler = pre,
			.post_handler = post,
245
			.private = trace
P
Pekka Paalanen 已提交
246 247 248
		},
		.phys = offset,
		.id = atomic_inc_return(&next_id)
249
	};
P
Pekka Paalanen 已提交
250
	map.map_id = trace->id;
251

P
Pekka Paalanen 已提交
252
	spin_lock_irq(&trace_lock);
253 254
	if (!is_enabled()) {
		kfree(trace);
P
Pekka Paalanen 已提交
255
		goto not_enabled;
256
	}
P
Pekka Paalanen 已提交
257

P
Pekka Paalanen 已提交
258
	mmio_trace_mapping(&map);
259 260 261
	list_add_tail(&trace->list, &trace_list);
	if (!nommiotrace)
		register_kmmio_probe(&trace->probe);
P
Pekka Paalanen 已提交
262 263 264

not_enabled:
	spin_unlock_irq(&trace_lock);
265 266
}

267 268
void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
						void __iomem *addr)
269
{
P
Pekka Paalanen 已提交
270
	if (!is_enabled()) /* recheck and proper locking in *_core() */
271 272
		return;

J
Joe Perches 已提交
273 274
	pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n",
		 (unsigned long long)offset, size, addr);
P
Pekka Paalanen 已提交
275
	if ((filter_offset) && (offset != filter_offset))
276
		return;
P
Pekka Paalanen 已提交
277
	ioremap_trace_core(offset, size, addr);
278 279
}

P
Pekka Paalanen 已提交
280
static void iounmap_trace_core(volatile void __iomem *addr)
281
{
P
Pekka Paalanen 已提交
282 283 284 285 286
	struct mmiotrace_map map = {
		.phys = 0,
		.virt = (unsigned long)addr,
		.len = 0,
		.opcode = MMIO_UNPROBE
287 288 289
	};
	struct remap_trace *trace;
	struct remap_trace *tmp;
P
Pekka Paalanen 已提交
290 291
	struct remap_trace *found_trace = NULL;

J
Joe Perches 已提交
292
	pr_debug("Unmapping %p.\n", addr);
293

P
Pekka Paalanen 已提交
294 295 296 297
	spin_lock_irq(&trace_lock);
	if (!is_enabled())
		goto not_enabled;

298 299 300 301 302
	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 已提交
303
			found_trace = trace;
304 305 306
			break;
		}
	}
P
Pekka Paalanen 已提交
307 308
	map.map_id = (found_trace) ? found_trace->id : -1;
	mmio_trace_mapping(&map);
P
Pekka Paalanen 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322

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);
323 324
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
int mmiotrace_printk(const char *fmt, ...)
{
	int ret = 0;
	va_list args;
	unsigned long flags;
	va_start(args, fmt);

	spin_lock_irqsave(&trace_lock, flags);
	if (is_enabled())
		ret = mmio_trace_printk(fmt, args);
	spin_unlock_irqrestore(&trace_lock, flags);

	va_end(args);
	return ret;
}
EXPORT_SYMBOL(mmiotrace_printk);

342 343 344 345 346
static void clear_trace_list(void)
{
	struct remap_trace *trace;
	struct remap_trace *tmp;

P
Pekka Paalanen 已提交
347 348 349 350 351 352 353
	/*
	 * 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) {
J
Joe Perches 已提交
354 355
		pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n",
			  trace->probe.addr, trace->probe.len);
356 357
		if (!nommiotrace)
			unregister_kmmio_probe(&trace->probe);
P
Pekka Paalanen 已提交
358 359 360 361
	}
	synchronize_rcu(); /* unregister_kmmio_probe() requirement */

	list_for_each_entry_safe(trace, tmp, &trace_list, list) {
362 363
		list_del(&trace->list);
		kfree(trace);
P
Pekka Paalanen 已提交
364 365 366
	}
}

367
#ifdef CONFIG_HOTPLUG_CPU
368
static cpumask_var_t downed_cpus;
369 370 371 372 373 374

static void enter_uniprocessor(void)
{
	int cpu;
	int err;

375
	if (!cpumask_available(downed_cpus) &&
376
	    !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) {
J
Joe Perches 已提交
377
		pr_notice("Failed to allocate mask\n");
378 379 380
		goto out;
	}

381
	get_online_cpus();
382 383
	cpumask_copy(downed_cpus, cpu_online_mask);
	cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus);
384
	if (num_online_cpus() > 1)
J
Joe Perches 已提交
385
		pr_notice("Disabling non-boot CPUs...\n");
386 387
	put_online_cpus();

388
	for_each_cpu(cpu, downed_cpus) {
389
		err = remove_cpu(cpu);
P
Pekka Paalanen 已提交
390
		if (!err)
J
Joe Perches 已提交
391
			pr_info("CPU%d is down.\n", cpu);
P
Pekka Paalanen 已提交
392
		else
J
Joe Perches 已提交
393
			pr_err("Error taking CPU%d down: %d\n", cpu, err);
394
	}
395
out:
396
	if (num_online_cpus() > 1)
397
		pr_warn("multiple CPUs still online, may miss events.\n");
398 399
}

400
static void leave_uniprocessor(void)
401 402 403 404
{
	int cpu;
	int err;

405
	if (!cpumask_available(downed_cpus) || cpumask_weight(downed_cpus) == 0)
406
		return;
J
Joe Perches 已提交
407
	pr_notice("Re-enabling CPUs...\n");
408
	for_each_cpu(cpu, downed_cpus) {
409
		err = add_cpu(cpu);
410
		if (!err)
J
Joe Perches 已提交
411
			pr_info("enabled CPU%d.\n", cpu);
412
		else
J
Joe Perches 已提交
413
			pr_err("cannot re-enable CPU%d: %d\n", cpu, err);
414 415 416 417 418 419 420
	}
}

#else /* !CONFIG_HOTPLUG_CPU */
static void enter_uniprocessor(void)
{
	if (num_online_cpus() > 1)
421 422
		pr_warn("multiple CPUs are online, may miss events. "
			"Suggest booting with maxcpus=1 kernel argument.\n");
423 424 425 426 427 428 429
}

static void leave_uniprocessor(void)
{
}
#endif

P
Pekka Paalanen 已提交
430
void enable_mmiotrace(void)
P
Pekka Paalanen 已提交
431 432 433 434 435 436
{
	mutex_lock(&mmiotrace_mutex);
	if (is_enabled())
		goto out;

	if (nommiotrace)
J
Joe Perches 已提交
437
		pr_info("MMIO tracing disabled.\n");
438
	kmmio_init();
439
	enter_uniprocessor();
P
Pekka Paalanen 已提交
440 441 442
	spin_lock_irq(&trace_lock);
	atomic_inc(&mmiotrace_enabled);
	spin_unlock_irq(&trace_lock);
J
Joe Perches 已提交
443
	pr_info("enabled.\n");
P
Pekka Paalanen 已提交
444 445 446 447
out:
	mutex_unlock(&mmiotrace_mutex);
}

P
Pekka Paalanen 已提交
448
void disable_mmiotrace(void)
P
Pekka Paalanen 已提交
449 450 451 452 453 454 455 456 457 458 459
{
	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 */
460
	leave_uniprocessor();
461
	kmmio_cleanup();
J
Joe Perches 已提交
462
	pr_info("disabled.\n");
P
Pekka Paalanen 已提交
463 464
out:
	mutex_unlock(&mmiotrace_mutex);
465
}