Merge phase #2 (PAT updates) of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[deliverable/linux.git] / arch / x86 / mm / mmio-mod.c
CommitLineData
8b7d89d0
PP
1/*
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.
6 *
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.
11 *
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.
15 *
16 * Copyright (C) IBM Corporation, 2005
17 * Jeff Muizelaar, 2006, 2007
18 * Pekka Paalanen, 2008 <pq@iki.fi>
19 *
20 * Derived from the read-mod example from relay-examples by Tom Zanussi.
21 */
d61fc448
PP
22#define DEBUG 1
23
8b7d89d0 24#include <linux/module.h>
8b7d89d0 25#include <linux/debugfs.h>
f984b51e 26#include <linux/uaccess.h>
970e6fa0 27#include <linux/io.h>
8b7d89d0
PP
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 */
fe1ffafa 33#include <asm/atomic.h>
f5136380 34#include <linux/percpu.h>
7423d111 35#include <linux/cpu.h>
8b7d89d0 36
8b7d89d0
PP
37#include "pf_in.h"
38
d61fc448 39#define NAME "mmiotrace: "
8b7d89d0 40
8b7d89d0
PP
41struct trap_reason {
42 unsigned long addr;
43 unsigned long ip;
44 enum reason_type type;
45 int active_traces;
46};
47
d61fc448
PP
48struct remap_trace {
49 struct list_head list;
50 struct kmmio_probe probe;
dee310d0 51 resource_size_t phys;
d61fc448
PP
52 unsigned long id;
53};
54
fe1ffafa 55/* Accessed per-cpu. */
f5136380 56static DEFINE_PER_CPU(struct trap_reason, pf_reason);
bd8ac686 57static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
8b7d89d0 58
f984b51e 59#if 0 /* XXX: no way gather this info anymore */
fe1ffafa 60/* Access to this is not per-cpu. */
f5136380 61static DEFINE_PER_CPU(atomic_t, dropped);
f984b51e 62#endif
fe1ffafa 63
d61fc448 64static struct dentry *marker_file;
8b7d89d0 65
d61fc448
PP
66static DEFINE_MUTEX(mmiotrace_mutex);
67static DEFINE_SPINLOCK(trace_lock);
68static atomic_t mmiotrace_enabled;
69static LIST_HEAD(trace_list); /* struct remap_trace */
d61fc448
PP
70
71/*
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
75 * and trace_lock.
76 * - Routines depending on is_enabled() must take trace_lock.
77 * - trace_list users must hold trace_lock.
bd8ac686 78 * - is_enabled() guarantees that mmio_trace_record is allowed.
d61fc448
PP
79 * - pre/post callbacks assume the effect of is_enabled() being true.
80 */
8b7d89d0
PP
81
82/* module parameters */
d61fc448
PP
83static unsigned long filter_offset;
84static int nommiotrace;
d61fc448 85static int trace_pc;
8b7d89d0 86
8b7d89d0
PP
87module_param(filter_offset, ulong, 0);
88module_param(nommiotrace, bool, 0);
8b7d89d0
PP
89module_param(trace_pc, bool, 0);
90
8b7d89d0
PP
91MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
92MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
8b7d89d0 93MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
d61fc448
PP
94
95static bool is_enabled(void)
96{
97 return atomic_read(&mmiotrace_enabled);
98}
8b7d89d0 99
bd8ac686 100#if 0 /* XXX: needs rewrite */
8b7d89d0 101/*
d61fc448 102 * Write callback for the debugfs entry:
8b7d89d0
PP
103 * Read a marker and write it to the mmio trace log
104 */
d61fc448
PP
105static ssize_t write_marker(struct file *file, const char __user *buffer,
106 size_t count, loff_t *ppos)
8b7d89d0
PP
107{
108 char *event = NULL;
109 struct mm_io_header *headp;
d61fc448 110 ssize_t len = (count > 65535) ? 65535 : count;
8b7d89d0
PP
111
112 event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
113 if (!event)
114 return -ENOMEM;
115
116 headp = (struct mm_io_header *)event;
117 headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
118 headp->data_len = len;
8b7d89d0
PP
119
120 if (copy_from_user(event + sizeof(*headp), buffer, len)) {
121 kfree(event);
122 return -EFAULT;
123 }
124
d61fc448 125 spin_lock_irq(&trace_lock);
f984b51e 126#if 0 /* XXX: convert this to use tracing */
d61fc448
PP
127 if (is_enabled())
128 relay_write(chan, event, sizeof(*headp) + len);
129 else
f984b51e 130#endif
d61fc448
PP
131 len = -EINVAL;
132 spin_unlock_irq(&trace_lock);
8b7d89d0
PP
133 kfree(event);
134 return len;
135}
bd8ac686 136#endif
8b7d89d0
PP
137
138static void print_pte(unsigned long address)
139{
790e2a29 140 unsigned int level;
75bb8835
PP
141 pte_t *pte = lookup_address(address, &level);
142
143 if (!pte) {
d61fc448 144 pr_err(NAME "Error in %s: no pte for page 0x%08lx\n",
0fd0e3da 145 __func__, address);
75bb8835
PP
146 return;
147 }
148
149 if (level == PG_LEVEL_2M) {
d61fc448
PP
150 pr_emerg(NAME "4MB pages are not currently supported: "
151 "0x%08lx\n", address);
8b7d89d0
PP
152 BUG();
153 }
0663bb6c
RD
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);
8b7d89d0
PP
157}
158
159/*
160 * For some reason the pre/post pairs have been called in an
161 * unmatched order. Report and die.
162 */
163static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
164{
f5136380 165 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
d61fc448
PP
166 pr_emerg(NAME "unexpected fault for address: 0x%08lx, "
167 "last fault for address: 0x%08lx\n",
f5136380 168 addr, my_reason->addr);
8b7d89d0 169 print_pte(addr);
d61fc448
PP
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);
8b7d89d0 172#ifdef __i386__
0fd0e3da 173 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
8b7d89d0 174 regs->ax, regs->bx, regs->cx, regs->dx);
0fd0e3da 175 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
8b7d89d0
PP
176 regs->si, regs->di, regs->bp, regs->sp);
177#else
0fd0e3da 178 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
8b7d89d0 179 regs->ax, regs->cx, regs->dx);
0fd0e3da 180 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
8b7d89d0
PP
181 regs->si, regs->di, regs->bp, regs->sp);
182#endif
f5136380 183 put_cpu_var(pf_reason);
8b7d89d0
PP
184 BUG();
185}
186
187static void pre(struct kmmio_probe *p, struct pt_regs *regs,
188 unsigned long addr)
189{
f5136380 190 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 191 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
192 const unsigned long instptr = instruction_pointer(regs);
193 const enum reason_type type = get_ins_type(instptr);
a50445d7 194 struct remap_trace *trace = p->private;
8b7d89d0
PP
195
196 /* it doesn't make sense to have more than one active trace per cpu */
f5136380 197 if (my_reason->active_traces)
8b7d89d0
PP
198 die_kmmio_nesting_error(regs, addr);
199 else
f5136380 200 my_reason->active_traces++;
8b7d89d0 201
f5136380
PP
202 my_reason->type = type;
203 my_reason->addr = addr;
204 my_reason->ip = instptr;
8b7d89d0 205
bd8ac686
PP
206 my_trace->phys = addr - trace->probe.addr + trace->phys;
207 my_trace->map_id = trace->id;
8b7d89d0
PP
208
209 /*
210 * Only record the program counter when requested.
211 * It may taint clean-room reverse engineering.
212 */
213 if (trace_pc)
bd8ac686 214 my_trace->pc = instptr;
8b7d89d0 215 else
bd8ac686 216 my_trace->pc = 0;
8b7d89d0 217
f984b51e
PP
218 /*
219 * XXX: the timestamp recorded will be *after* the tracing has been
220 * done, not at the time we hit the instruction. SMP implications
221 * on event ordering?
222 */
8b7d89d0
PP
223
224 switch (type) {
225 case REG_READ:
bd8ac686
PP
226 my_trace->opcode = MMIO_READ;
227 my_trace->width = get_ins_mem_width(instptr);
8b7d89d0
PP
228 break;
229 case REG_WRITE:
bd8ac686
PP
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);
8b7d89d0
PP
233 break;
234 case IMM_WRITE:
bd8ac686
PP
235 my_trace->opcode = MMIO_WRITE;
236 my_trace->width = get_ins_mem_width(instptr);
237 my_trace->value = get_ins_imm_val(instptr);
8b7d89d0
PP
238 break;
239 default:
240 {
241 unsigned char *ip = (unsigned char *)instptr;
bd8ac686
PP
242 my_trace->opcode = MMIO_UNKNOWN_OP;
243 my_trace->width = 0;
244 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 |
f5136380 245 *(ip + 2);
8b7d89d0
PP
246 }
247 }
f5136380
PP
248 put_cpu_var(cpu_trace);
249 put_cpu_var(pf_reason);
8b7d89d0
PP
250}
251
252static void post(struct kmmio_probe *p, unsigned long condition,
253 struct pt_regs *regs)
254{
f5136380 255 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
bd8ac686 256 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
8b7d89d0
PP
257
258 /* this should always return the active_trace count to 0 */
f5136380
PP
259 my_reason->active_traces--;
260 if (my_reason->active_traces) {
d61fc448 261 pr_emerg(NAME "unexpected post handler");
8b7d89d0
PP
262 BUG();
263 }
264
f5136380 265 switch (my_reason->type) {
8b7d89d0 266 case REG_READ:
bd8ac686 267 my_trace->value = get_ins_reg_val(my_reason->ip, regs);
8b7d89d0
PP
268 break;
269 default:
270 break;
271 }
f984b51e 272
bd8ac686 273 mmio_trace_rw(my_trace);
f5136380
PP
274 put_cpu_var(cpu_trace);
275 put_cpu_var(pf_reason);
8b7d89d0
PP
276}
277
dee310d0 278static void ioremap_trace_core(resource_size_t offset, unsigned long size,
8b7d89d0
PP
279 void __iomem *addr)
280{
d61fc448 281 static atomic_t next_id;
8b7d89d0 282 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
87e547fe 283 /* These are page-unaligned. */
bd8ac686
PP
284 struct mmiotrace_map map = {
285 .phys = offset,
286 .virt = (unsigned long)addr,
287 .len = size,
288 .opcode = MMIO_PROBE
8b7d89d0 289 };
8b7d89d0 290
d61fc448
PP
291 if (!trace) {
292 pr_err(NAME "kmalloc failed in ioremap\n");
293 return;
294 }
295
8b7d89d0
PP
296 *trace = (struct remap_trace) {
297 .probe = {
298 .addr = (unsigned long)addr,
299 .len = size,
300 .pre_handler = pre,
301 .post_handler = post,
a50445d7 302 .private = trace
d61fc448
PP
303 },
304 .phys = offset,
305 .id = atomic_inc_return(&next_id)
8b7d89d0 306 };
bd8ac686 307 map.map_id = trace->id;
8b7d89d0 308
d61fc448
PP
309 spin_lock_irq(&trace_lock);
310 if (!is_enabled())
311 goto not_enabled;
312
bd8ac686 313 mmio_trace_mapping(&map);
8b7d89d0 314 list_add_tail(&trace->list, &trace_list);
8b7d89d0
PP
315 if (!nommiotrace)
316 register_kmmio_probe(&trace->probe);
d61fc448
PP
317
318not_enabled:
319 spin_unlock_irq(&trace_lock);
8b7d89d0
PP
320}
321
dee310d0
PP
322void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
323 void __iomem *addr)
8b7d89d0 324{
d61fc448 325 if (!is_enabled()) /* recheck and proper locking in *_core() */
8b7d89d0
PP
326 return;
327
dee310d0
PP
328 pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n",
329 (unsigned long long)offset, size, addr);
d61fc448 330 if ((filter_offset) && (offset != filter_offset))
8b7d89d0 331 return;
d61fc448 332 ioremap_trace_core(offset, size, addr);
8b7d89d0 333}
8b7d89d0 334
d61fc448 335static void iounmap_trace_core(volatile void __iomem *addr)
8b7d89d0 336{
bd8ac686
PP
337 struct mmiotrace_map map = {
338 .phys = 0,
339 .virt = (unsigned long)addr,
340 .len = 0,
341 .opcode = MMIO_UNPROBE
8b7d89d0
PP
342 };
343 struct remap_trace *trace;
344 struct remap_trace *tmp;
d61fc448
PP
345 struct remap_trace *found_trace = NULL;
346
347 pr_debug(NAME "Unmapping %p.\n", addr);
8b7d89d0 348
d61fc448
PP
349 spin_lock_irq(&trace_lock);
350 if (!is_enabled())
351 goto not_enabled;
352
8b7d89d0
PP
353 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
354 if ((unsigned long)addr == trace->probe.addr) {
355 if (!nommiotrace)
356 unregister_kmmio_probe(&trace->probe);
357 list_del(&trace->list);
d61fc448 358 found_trace = trace;
8b7d89d0
PP
359 break;
360 }
361 }
bd8ac686
PP
362 map.map_id = (found_trace) ? found_trace->id : -1;
363 mmio_trace_mapping(&map);
d61fc448
PP
364
365not_enabled:
366 spin_unlock_irq(&trace_lock);
367 if (found_trace) {
368 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
369 kfree(found_trace);
370 }
371}
372
373void mmiotrace_iounmap(volatile void __iomem *addr)
374{
375 might_sleep();
376 if (is_enabled()) /* recheck and proper locking in *_core() */
377 iounmap_trace_core(addr);
8b7d89d0 378}
8b7d89d0
PP
379
380static void clear_trace_list(void)
381{
382 struct remap_trace *trace;
383 struct remap_trace *tmp;
384
d61fc448
PP
385 /*
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.
390 */
391 list_for_each_entry(trace, &trace_list, list) {
392 pr_notice(NAME "purging non-iounmapped "
8b7d89d0
PP
393 "trace @0x%08lx, size 0x%lx.\n",
394 trace->probe.addr, trace->probe.len);
395 if (!nommiotrace)
396 unregister_kmmio_probe(&trace->probe);
d61fc448
PP
397 }
398 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
399
400 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
8b7d89d0
PP
401 list_del(&trace->list);
402 kfree(trace);
d61fc448
PP
403 }
404}
405
7423d111
PP
406#ifdef CONFIG_HOTPLUG_CPU
407static cpumask_t downed_cpus;
408
409static void enter_uniprocessor(void)
410{
411 int cpu;
412 int err;
413
414 get_online_cpus();
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");
419 put_online_cpus();
420
421 for_each_cpu_mask(cpu, downed_cpus) {
422 err = cpu_down(cpu);
970e6fa0 423 if (!err)
7423d111 424 pr_info(NAME "CPU%d is down.\n", cpu);
970e6fa0 425 else
7423d111 426 pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err);
7423d111
PP
427 }
428 if (num_online_cpus() > 1)
429 pr_warning(NAME "multiple CPUs still online, "
430 "may miss events.\n");
431}
432
7701e8c5
MS
433/* __ref because leave_uniprocessor calls cpu_up which is __cpuinit,
434 but this whole function is ifdefed CONFIG_HOTPLUG_CPU */
435static void __ref leave_uniprocessor(void)
7423d111
PP
436{
437 int cpu;
438 int err;
439
440 if (cpus_weight(downed_cpus) == 0)
441 return;
442 pr_notice(NAME "Re-enabling CPUs...\n");
443 for_each_cpu_mask(cpu, downed_cpus) {
444 err = cpu_up(cpu);
445 if (!err)
446 pr_info(NAME "enabled CPU%d.\n", cpu);
447 else
448 pr_err(NAME "cannot re-enable CPU%d: %d\n", cpu, err);
449 }
450}
451
452#else /* !CONFIG_HOTPLUG_CPU */
453static void enter_uniprocessor(void)
454{
455 if (num_online_cpus() > 1)
456 pr_warning(NAME "multiple CPUs are online, may miss events. "
457 "Suggest booting with maxcpus=1 kernel argument.\n");
458}
459
460static void leave_uniprocessor(void)
461{
462}
463#endif
464
bd8ac686 465#if 0 /* XXX: out of order */
d61fc448
PP
466static struct file_operations fops_marker = {
467 .owner = THIS_MODULE,
468 .write = write_marker
469};
bd8ac686 470#endif
d61fc448 471
f984b51e 472void enable_mmiotrace(void)
d61fc448
PP
473{
474 mutex_lock(&mmiotrace_mutex);
475 if (is_enabled())
476 goto out;
477
f984b51e 478#if 0 /* XXX: tracing does not support text entries */
d61fc448
PP
479 marker_file = debugfs_create_file("marker", 0660, dir, NULL,
480 &fops_marker);
481 if (!marker_file)
482 pr_err(NAME "marker file creation failed.\n");
bd8ac686 483#endif
d61fc448
PP
484
485 if (nommiotrace)
486 pr_info(NAME "MMIO tracing disabled.\n");
7423d111 487 enter_uniprocessor();
d61fc448
PP
488 spin_lock_irq(&trace_lock);
489 atomic_inc(&mmiotrace_enabled);
490 spin_unlock_irq(&trace_lock);
491 pr_info(NAME "enabled.\n");
492out:
493 mutex_unlock(&mmiotrace_mutex);
494}
495
f984b51e 496void disable_mmiotrace(void)
d61fc448
PP
497{
498 mutex_lock(&mmiotrace_mutex);
499 if (!is_enabled())
500 goto out;
501
502 spin_lock_irq(&trace_lock);
503 atomic_dec(&mmiotrace_enabled);
504 BUG_ON(is_enabled());
505 spin_unlock_irq(&trace_lock);
506
507 clear_trace_list(); /* guarantees: no more kmmio callbacks */
7423d111 508 leave_uniprocessor();
d61fc448
PP
509 if (marker_file) {
510 debugfs_remove(marker_file);
511 marker_file = NULL;
512 }
d61fc448
PP
513
514 pr_info(NAME "disabled.\n");
515out:
516 mutex_unlock(&mmiotrace_mutex);
8b7d89d0 517}
This page took 0.086207 seconds and 5 git commands to generate.