aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/mmiotrace/mmio-mod.c
diff options
context:
space:
mode:
authorPekka Paalanen <pq@iki.fi>2008-05-12 15:20:57 -0400
committerThomas Gleixner <tglx@linutronix.de>2008-05-24 05:22:24 -0400
commitd61fc44853f46fb002228b18aa5f30db21fcd4ac (patch)
tree14fa9416aeceb7c5d24876c1111f6f2458a1dc7d /arch/x86/kernel/mmiotrace/mmio-mod.c
parent0fd0e3da4557c479b820b9a4a7afa25b4637ddf2 (diff)
x86: mmiotrace, preview 2
Kconfig.debug, Makefile and testmmiotrace.c style fixes. Use real mutex instead of mutex. Fix failure path in register probe func. kmmio: RCU read-locked over single stepping. Generate mapping id's. Make mmio-mod.c built-in and rewrite its locking. Add debugfs file to enable/disable mmiotracing. kmmio: use irqsave spinlocks. Lots of cleanups in mmio-mod.c Marker file moved from /proc into debugfs. Call mmiotrace entrypoints directly from ioremap.c. Signed-off-by: Pekka Paalanen <pq@iki.fi> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'arch/x86/kernel/mmiotrace/mmio-mod.c')
-rw-r--r--arch/x86/kernel/mmiotrace/mmio-mod.c397
1 files changed, 261 insertions, 136 deletions
diff --git a/arch/x86/kernel/mmiotrace/mmio-mod.c b/arch/x86/kernel/mmiotrace/mmio-mod.c
index e1a508588f03..738644061e4e 100644
--- a/arch/x86/kernel/mmiotrace/mmio-mod.c
+++ b/arch/x86/kernel/mmiotrace/mmio-mod.c
@@ -19,6 +19,8 @@
19 * 19 *
20 * Derived from the read-mod example from relay-examples by Tom Zanussi. 20 * Derived from the read-mod example from relay-examples by Tom Zanussi.
21 */ 21 */
22#define DEBUG 1
23
22#include <linux/module.h> 24#include <linux/module.h>
23#include <linux/relay.h> 25#include <linux/relay.h>
24#include <linux/debugfs.h> 26#include <linux/debugfs.h>
@@ -34,12 +36,12 @@
34 36
35#include "pf_in.h" 37#include "pf_in.h"
36 38
37/* This app's relay channel files will appear in /debug/mmio-trace */ 39#define NAME "mmiotrace: "
38#define APP_DIR "mmio-trace"
39/* the marker injection file in /proc */
40#define MARKER_FILE "mmio-marker"
41 40
42#define MODULE_NAME "mmiotrace" 41/* This app's relay channel files will appear in /debug/mmio-trace */
42static const char APP_DIR[] = "mmio-trace";
43/* the marker injection file in /debug/APP_DIR */
44static const char MARKER_FILE[] = "mmio-marker";
43 45
44struct trap_reason { 46struct trap_reason {
45 unsigned long addr; 47 unsigned long addr;
@@ -48,6 +50,15 @@ struct trap_reason {
48 int active_traces; 50 int active_traces;
49}; 51};
50 52
53struct remap_trace {
54 struct list_head list;
55 struct kmmio_probe probe;
56 unsigned long phys;
57 unsigned long id;
58};
59
60static const size_t subbuf_size = 256*1024;
61
51/* Accessed per-cpu. */ 62/* Accessed per-cpu. */
52static DEFINE_PER_CPU(struct trap_reason, pf_reason); 63static DEFINE_PER_CPU(struct trap_reason, pf_reason);
53static DEFINE_PER_CPU(struct mm_io_header_rw, cpu_trace); 64static DEFINE_PER_CPU(struct mm_io_header_rw, cpu_trace);
@@ -55,33 +66,53 @@ static DEFINE_PER_CPU(struct mm_io_header_rw, cpu_trace);
55/* Access to this is not per-cpu. */ 66/* Access to this is not per-cpu. */
56static DEFINE_PER_CPU(atomic_t, dropped); 67static DEFINE_PER_CPU(atomic_t, dropped);
57 68
58static struct file_operations mmio_fops = { 69static struct dentry *dir;
59 .owner = THIS_MODULE, 70static struct dentry *enabled_file;
60}; 71static struct dentry *marker_file;
61 72
62static const size_t subbuf_size = 256*1024; 73static DEFINE_MUTEX(mmiotrace_mutex);
74static DEFINE_SPINLOCK(trace_lock);
75static atomic_t mmiotrace_enabled;
76static LIST_HEAD(trace_list); /* struct remap_trace */
63static struct rchan *chan; 77static struct rchan *chan;
64static struct dentry *dir; 78
65static struct proc_dir_entry *proc_marker_file; 79/*
80 * Locking in this file:
81 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections.
82 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex
83 * and trace_lock.
84 * - Routines depending on is_enabled() must take trace_lock.
85 * - trace_list users must hold trace_lock.
86 * - is_enabled() guarantees that chan is valid.
87 * - pre/post callbacks assume the effect of is_enabled() being true.
88 */
66 89
67/* module parameters */ 90/* module parameters */
68static unsigned int n_subbufs = 32*4; 91static unsigned int n_subbufs = 32*4;
69static unsigned long filter_offset; 92static unsigned long filter_offset;
70static int nommiotrace; 93static int nommiotrace;
71static int ISA_trace; 94static int ISA_trace;
72static int trace_pc; 95static int trace_pc;
96static int enable_now;
73 97
74module_param(n_subbufs, uint, 0); 98module_param(n_subbufs, uint, 0);
75module_param(filter_offset, ulong, 0); 99module_param(filter_offset, ulong, 0);
76module_param(nommiotrace, bool, 0); 100module_param(nommiotrace, bool, 0);
77module_param(ISA_trace, bool, 0); 101module_param(ISA_trace, bool, 0);
78module_param(trace_pc, bool, 0); 102module_param(trace_pc, bool, 0);
103module_param(enable_now, bool, 0);
79 104
80MODULE_PARM_DESC(n_subbufs, "Number of 256kB buffers, default 128."); 105MODULE_PARM_DESC(n_subbufs, "Number of 256kB buffers, default 128.");
81MODULE_PARM_DESC(filter_offset, "Start address of traced mappings."); 106MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
82MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing."); 107MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
83MODULE_PARM_DESC(ISA_trace, "Do not exclude the low ISA range."); 108MODULE_PARM_DESC(ISA_trace, "Do not exclude the low ISA range.");
84MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions."); 109MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
110MODULE_PARM_DESC(enable_now, "Start mmiotracing immediately on module load.");
111
112static bool is_enabled(void)
113{
114 return atomic_read(&mmiotrace_enabled);
115}
85 116
86static void record_timestamp(struct mm_io_header *header) 117static void record_timestamp(struct mm_io_header *header)
87{ 118{
@@ -93,15 +124,15 @@ static void record_timestamp(struct mm_io_header *header)
93} 124}
94 125
95/* 126/*
96 * Write callback for the /proc entry: 127 * Write callback for the debugfs entry:
97 * Read a marker and write it to the mmio trace log 128 * Read a marker and write it to the mmio trace log
98 */ 129 */
99static int write_marker(struct file *file, const char __user *buffer, 130static ssize_t write_marker(struct file *file, const char __user *buffer,
100 unsigned long count, void *data) 131 size_t count, loff_t *ppos)
101{ 132{
102 char *event = NULL; 133 char *event = NULL;
103 struct mm_io_header *headp; 134 struct mm_io_header *headp;
104 int len = (count > 65535) ? 65535 : count; 135 ssize_t len = (count > 65535) ? 65535 : count;
105 136
106 event = kzalloc(sizeof(*headp) + len, GFP_KERNEL); 137 event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
107 if (!event) 138 if (!event)
@@ -117,7 +148,12 @@ static int write_marker(struct file *file, const char __user *buffer,
117 return -EFAULT; 148 return -EFAULT;
118 } 149 }
119 150
120 relay_write(chan, event, sizeof(*headp) + len); 151 spin_lock_irq(&trace_lock);
152 if (is_enabled())
153 relay_write(chan, event, sizeof(*headp) + len);
154 else
155 len = -EINVAL;
156 spin_unlock_irq(&trace_lock);
121 kfree(event); 157 kfree(event);
122 return len; 158 return len;
123} 159}
@@ -128,19 +164,18 @@ static void print_pte(unsigned long address)
128 pte_t *pte = lookup_address(address, &level); 164 pte_t *pte = lookup_address(address, &level);
129 165
130 if (!pte) { 166 if (!pte) {
131 pr_err(MODULE_NAME ": Error in %s: no pte for page 0x%08lx\n", 167 pr_err(NAME "Error in %s: no pte for page 0x%08lx\n",
132 __func__, address); 168 __func__, address);
133 return; 169 return;
134 } 170 }
135 171
136 if (level == PG_LEVEL_2M) { 172 if (level == PG_LEVEL_2M) {
137 pr_emerg(MODULE_NAME ": 4MB pages are not currently " 173 pr_emerg(NAME "4MB pages are not currently supported: "
138 "supported: %lx\n", address); 174 "0x%08lx\n", address);
139 BUG(); 175 BUG();
140 } 176 }
141 pr_info(MODULE_NAME ": pte for 0x%lx: 0x%lx 0x%lx\n", 177 pr_info(NAME "pte for 0x%lx: 0x%lx 0x%lx\n", address, pte_val(*pte),
142 address, pte_val(*pte), 178 pte_val(*pte) & _PAGE_PRESENT);
143 pte_val(*pte) & _PAGE_PRESENT);
144} 179}
145 180
146/* 181/*
@@ -150,22 +185,18 @@ static void print_pte(unsigned long address)
150static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr) 185static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
151{ 186{
152 const struct trap_reason *my_reason = &get_cpu_var(pf_reason); 187 const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
153 pr_emerg(MODULE_NAME ": unexpected fault for address: %lx, " 188 pr_emerg(NAME "unexpected fault for address: 0x%08lx, "
154 "last fault for address: %lx\n", 189 "last fault for address: 0x%08lx\n",
155 addr, my_reason->addr); 190 addr, my_reason->addr);
156 print_pte(addr); 191 print_pte(addr);
192 print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip);
193 print_symbol(KERN_EMERG "last faulting IP was at %s\n", my_reason->ip);
157#ifdef __i386__ 194#ifdef __i386__
158 print_symbol(KERN_EMERG "faulting EIP is at %s\n", regs->ip);
159 print_symbol(KERN_EMERG "last faulting EIP was at %s\n",
160 my_reason->ip);
161 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", 195 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
162 regs->ax, regs->bx, regs->cx, regs->dx); 196 regs->ax, regs->bx, regs->cx, regs->dx);
163 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", 197 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
164 regs->si, regs->di, regs->bp, regs->sp); 198 regs->si, regs->di, regs->bp, regs->sp);
165#else 199#else
166 print_symbol(KERN_EMERG "faulting RIP is at %s\n", regs->ip);
167 print_symbol(KERN_EMERG "last faulting RIP was at %s\n",
168 my_reason->ip);
169 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n", 200 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
170 regs->ax, regs->cx, regs->dx); 201 regs->ax, regs->cx, regs->dx);
171 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n", 202 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
@@ -197,6 +228,10 @@ static void pre(struct kmmio_probe *p, struct pt_regs *regs,
197 my_trace->header.pid = 0; 228 my_trace->header.pid = 0;
198 my_trace->header.data_len = sizeof(struct mm_io_rw); 229 my_trace->header.data_len = sizeof(struct mm_io_rw);
199 my_trace->rw.address = addr; 230 my_trace->rw.address = addr;
231 /*
232 * struct remap_trace *trace = p->user_data;
233 * phys = addr - trace->probe.addr + trace->phys;
234 */
200 235
201 /* 236 /*
202 * Only record the program counter when requested. 237 * Only record the program counter when requested.
@@ -246,15 +281,10 @@ static void post(struct kmmio_probe *p, unsigned long condition,
246 struct trap_reason *my_reason = &get_cpu_var(pf_reason); 281 struct trap_reason *my_reason = &get_cpu_var(pf_reason);
247 struct mm_io_header_rw *my_trace = &get_cpu_var(cpu_trace); 282 struct mm_io_header_rw *my_trace = &get_cpu_var(cpu_trace);
248 283
249 /*
250 * XXX: This might not get called, if the probe is removed while
251 * trace hit is on flight.
252 */
253
254 /* this should always return the active_trace count to 0 */ 284 /* this should always return the active_trace count to 0 */
255 my_reason->active_traces--; 285 my_reason->active_traces--;
256 if (my_reason->active_traces) { 286 if (my_reason->active_traces) {
257 pr_emerg(MODULE_NAME ": unexpected post handler"); 287 pr_emerg(NAME "unexpected post handler");
258 BUG(); 288 BUG();
259 } 289 }
260 290
@@ -284,20 +314,23 @@ static int subbuf_start_handler(struct rchan_buf *buf, void *subbuf,
284 int count; 314 int count;
285 if (relay_buf_full(buf)) { 315 if (relay_buf_full(buf)) {
286 if (atomic_inc_return(drop) == 1) 316 if (atomic_inc_return(drop) == 1)
287 pr_err(MODULE_NAME ": cpu %d buffer full!\n", cpu); 317 pr_err(NAME "cpu %d buffer full!\n", cpu);
288 return 0; 318 return 0;
289 } 319 }
290 count = atomic_read(drop); 320 count = atomic_read(drop);
291 if (count) { 321 if (count) {
292 pr_err(MODULE_NAME ": cpu %d buffer no longer full, " 322 pr_err(NAME "cpu %d buffer no longer full, missed %d events.\n",
293 "missed %d events.\n", 323 cpu, count);
294 cpu, count);
295 atomic_sub(count, drop); 324 atomic_sub(count, drop);
296 } 325 }
297 326
298 return 1; 327 return 1;
299} 328}
300 329
330static struct file_operations mmio_fops = {
331 .owner = THIS_MODULE,
332};
333
301/* file_create() callback. Creates relay file in debugfs. */ 334/* file_create() callback. Creates relay file in debugfs. */
302static struct dentry *create_buf_file_handler(const char *filename, 335static struct dentry *create_buf_file_handler(const char *filename,
303 struct dentry *parent, 336 struct dentry *parent,
@@ -333,34 +366,10 @@ static struct rchan_callbacks relay_callbacks = {
333 .remove_buf_file = remove_buf_file_handler, 366 .remove_buf_file = remove_buf_file_handler,
334}; 367};
335 368
336/* 369static void ioremap_trace_core(unsigned long offset, unsigned long size,
337 * create_channel - creates channel /debug/APP_DIR/cpuXXX
338 * Returns channel on success, NULL otherwise
339 */
340static struct rchan *create_channel(unsigned size, unsigned n)
341{
342 return relay_open("cpu", dir, size, n, &relay_callbacks, NULL);
343}
344
345/* destroy_channel - destroys channel /debug/APP_DIR/cpuXXX */
346static void destroy_channel(void)
347{
348 if (chan) {
349 relay_close(chan);
350 chan = NULL;
351 }
352}
353
354struct remap_trace {
355 struct list_head list;
356 struct kmmio_probe probe;
357};
358static LIST_HEAD(trace_list);
359static DEFINE_SPINLOCK(trace_list_lock);
360
361static void do_ioremap_trace_core(unsigned long offset, unsigned long size,
362 void __iomem *addr) 370 void __iomem *addr)
363{ 371{
372 static atomic_t next_id;
364 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL); 373 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
365 struct mm_io_header_map event = { 374 struct mm_io_header_map event = {
366 .header = { 375 .header = {
@@ -380,61 +389,49 @@ static void do_ioremap_trace_core(unsigned long offset, unsigned long size,
380 }; 389 };
381 record_timestamp(&event.header); 390 record_timestamp(&event.header);
382 391
392 if (!trace) {
393 pr_err(NAME "kmalloc failed in ioremap\n");
394 return;
395 }
396
383 *trace = (struct remap_trace) { 397 *trace = (struct remap_trace) {
384 .probe = { 398 .probe = {
385 .addr = (unsigned long)addr, 399 .addr = (unsigned long)addr,
386 .len = size, 400 .len = size,
387 .pre_handler = pre, 401 .pre_handler = pre,
388 .post_handler = post, 402 .post_handler = post,
389 } 403 .user_data = trace
404 },
405 .phys = offset,
406 .id = atomic_inc_return(&next_id)
390 }; 407 };
391 408
409 spin_lock_irq(&trace_lock);
410 if (!is_enabled())
411 goto not_enabled;
412
392 relay_write(chan, &event, sizeof(event)); 413 relay_write(chan, &event, sizeof(event));
393 spin_lock(&trace_list_lock);
394 list_add_tail(&trace->list, &trace_list); 414 list_add_tail(&trace->list, &trace_list);
395 spin_unlock(&trace_list_lock);
396 if (!nommiotrace) 415 if (!nommiotrace)
397 register_kmmio_probe(&trace->probe); 416 register_kmmio_probe(&trace->probe);
417
418not_enabled:
419 spin_unlock_irq(&trace_lock);
398} 420}
399 421
400static void ioremap_trace_core(unsigned long offset, unsigned long size, 422void
401 void __iomem *addr) 423mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
402{ 424{
403 if ((filter_offset) && (offset != filter_offset)) 425 if (!is_enabled()) /* recheck and proper locking in *_core() */
404 return; 426 return;
405 427
406 /* Don't trace the low PCI/ISA area, it's always mapped.. */ 428 pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr);
407 if (!ISA_trace && (offset < ISA_END_ADDRESS) && 429 if ((filter_offset) && (offset != filter_offset))
408 (offset + size > ISA_START_ADDRESS)) {
409 pr_notice(MODULE_NAME ": Ignoring map of low PCI/ISA area "
410 "(0x%lx-0x%lx)\n",
411 offset, offset + size);
412 return; 430 return;
413 } 431 ioremap_trace_core(offset, size, addr);
414 do_ioremap_trace_core(offset, size, addr);
415}
416
417void __iomem *ioremap_cache_trace(unsigned long offset, unsigned long size)
418{
419 void __iomem *p = ioremap_cache(offset, size);
420 pr_debug(MODULE_NAME ": ioremap_cache(0x%lx, 0x%lx) = %p\n",
421 offset, size, p);
422 ioremap_trace_core(offset, size, p);
423 return p;
424} 432}
425EXPORT_SYMBOL(ioremap_cache_trace);
426 433
427void __iomem *ioremap_nocache_trace(unsigned long offset, unsigned long size) 434static void iounmap_trace_core(volatile void __iomem *addr)
428{
429 void __iomem *p = ioremap_nocache(offset, size);
430 pr_debug(MODULE_NAME ": ioremap_nocache(0x%lx, 0x%lx) = %p\n",
431 offset, size, p);
432 ioremap_trace_core(offset, size, p);
433 return p;
434}
435EXPORT_SYMBOL(ioremap_nocache_trace);
436
437void iounmap_trace(volatile void __iomem *addr)
438{ 435{
439 struct mm_io_header_map event = { 436 struct mm_io_header_map event = {
440 .header = { 437 .header = {
@@ -454,84 +451,212 @@ void iounmap_trace(volatile void __iomem *addr)
454 }; 451 };
455 struct remap_trace *trace; 452 struct remap_trace *trace;
456 struct remap_trace *tmp; 453 struct remap_trace *tmp;
457 pr_debug(MODULE_NAME ": Unmapping %p.\n", addr); 454 struct remap_trace *found_trace = NULL;
455
456 pr_debug(NAME "Unmapping %p.\n", addr);
458 record_timestamp(&event.header); 457 record_timestamp(&event.header);
459 458
460 spin_lock(&trace_list_lock); 459 spin_lock_irq(&trace_lock);
460 if (!is_enabled())
461 goto not_enabled;
462
461 list_for_each_entry_safe(trace, tmp, &trace_list, list) { 463 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
462 if ((unsigned long)addr == trace->probe.addr) { 464 if ((unsigned long)addr == trace->probe.addr) {
463 if (!nommiotrace) 465 if (!nommiotrace)
464 unregister_kmmio_probe(&trace->probe); 466 unregister_kmmio_probe(&trace->probe);
465 list_del(&trace->list); 467 list_del(&trace->list);
466 kfree(trace); 468 found_trace = trace;
467 break; 469 break;
468 } 470 }
469 } 471 }
470 spin_unlock(&trace_list_lock);
471 relay_write(chan, &event, sizeof(event)); 472 relay_write(chan, &event, sizeof(event));
472 iounmap(addr); 473
474not_enabled:
475 spin_unlock_irq(&trace_lock);
476 if (found_trace) {
477 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
478 kfree(found_trace);
479 }
480}
481
482void mmiotrace_iounmap(volatile void __iomem *addr)
483{
484 might_sleep();
485 if (is_enabled()) /* recheck and proper locking in *_core() */
486 iounmap_trace_core(addr);
473} 487}
474EXPORT_SYMBOL(iounmap_trace);
475 488
476static void clear_trace_list(void) 489static void clear_trace_list(void)
477{ 490{
478 struct remap_trace *trace; 491 struct remap_trace *trace;
479 struct remap_trace *tmp; 492 struct remap_trace *tmp;
480 493
481 spin_lock(&trace_list_lock); 494 /*
482 list_for_each_entry_safe(trace, tmp, &trace_list, list) { 495 * No locking required, because the caller ensures we are in a
483 pr_warning(MODULE_NAME ": purging non-iounmapped " 496 * critical section via mutex, and is_enabled() is false,
497 * i.e. nothing can traverse or modify this list.
498 * Caller also ensures is_enabled() cannot change.
499 */
500 list_for_each_entry(trace, &trace_list, list) {
501 pr_notice(NAME "purging non-iounmapped "
484 "trace @0x%08lx, size 0x%lx.\n", 502 "trace @0x%08lx, size 0x%lx.\n",
485 trace->probe.addr, trace->probe.len); 503 trace->probe.addr, trace->probe.len);
486 if (!nommiotrace) 504 if (!nommiotrace)
487 unregister_kmmio_probe(&trace->probe); 505 unregister_kmmio_probe(&trace->probe);
506 }
507 synchronize_rcu(); /* unregister_kmmio_probe() requirement */
508
509 list_for_each_entry_safe(trace, tmp, &trace_list, list) {
488 list_del(&trace->list); 510 list_del(&trace->list);
489 kfree(trace); 511 kfree(trace);
512 }
513}
514
515static ssize_t read_enabled_file_bool(struct file *file,
516 char __user *user_buf, size_t count, loff_t *ppos)
517{
518 char buf[3];
519
520 if (is_enabled())
521 buf[0] = '1';
522 else
523 buf[0] = '0';
524 buf[1] = '\n';
525 buf[2] = '\0';
526 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
527}
528
529static void enable_mmiotrace(void);
530static void disable_mmiotrace(void);
531
532static ssize_t write_enabled_file_bool(struct file *file,
533 const char __user *user_buf, size_t count, loff_t *ppos)
534{
535 char buf[32];
536 int buf_size = min(count, (sizeof(buf)-1));
537
538 if (copy_from_user(buf, user_buf, buf_size))
539 return -EFAULT;
540
541 switch (buf[0]) {
542 case 'y':
543 case 'Y':
544 case '1':
545 enable_mmiotrace();
546 break;
547 case 'n':
548 case 'N':
549 case '0':
550 disable_mmiotrace();
490 break; 551 break;
491 } 552 }
492 spin_unlock(&trace_list_lock); 553
554 return count;
555}
556
557/* this ripped from kernel/kprobes.c */
558static struct file_operations fops_enabled = {
559 .owner = THIS_MODULE,
560 .read = read_enabled_file_bool,
561 .write = write_enabled_file_bool
562};
563
564static struct file_operations fops_marker = {
565 .owner = THIS_MODULE,
566 .write = write_marker
567};
568
569static void enable_mmiotrace(void)
570{
571 mutex_lock(&mmiotrace_mutex);
572 if (is_enabled())
573 goto out;
574
575 chan = relay_open("cpu", dir, subbuf_size, n_subbufs,
576 &relay_callbacks, NULL);
577 if (!chan) {
578 pr_err(NAME "relay app channel creation failed.\n");
579 goto out;
580 }
581
582 reference_kmmio();
583
584 marker_file = debugfs_create_file("marker", 0660, dir, NULL,
585 &fops_marker);
586 if (!marker_file)
587 pr_err(NAME "marker file creation failed.\n");
588
589 if (nommiotrace)
590 pr_info(NAME "MMIO tracing disabled.\n");
591 if (ISA_trace)
592 pr_warning(NAME "Warning! low ISA range will be traced.\n");
593 spin_lock_irq(&trace_lock);
594 atomic_inc(&mmiotrace_enabled);
595 spin_unlock_irq(&trace_lock);
596 pr_info(NAME "enabled.\n");
597out:
598 mutex_unlock(&mmiotrace_mutex);
599}
600
601static void disable_mmiotrace(void)
602{
603 mutex_lock(&mmiotrace_mutex);
604 if (!is_enabled())
605 goto out;
606
607 spin_lock_irq(&trace_lock);
608 atomic_dec(&mmiotrace_enabled);
609 BUG_ON(is_enabled());
610 spin_unlock_irq(&trace_lock);
611
612 clear_trace_list(); /* guarantees: no more kmmio callbacks */
613 unreference_kmmio();
614 if (marker_file) {
615 debugfs_remove(marker_file);
616 marker_file = NULL;
617 }
618 if (chan) {
619 relay_close(chan);
620 chan = NULL;
621 }
622
623 pr_info(NAME "disabled.\n");
624out:
625 mutex_unlock(&mmiotrace_mutex);
493} 626}
494 627
495static int __init init(void) 628static int __init init(void)
496{ 629{
630 pr_debug(NAME "load...\n");
497 if (n_subbufs < 2) 631 if (n_subbufs < 2)
498 return -EINVAL; 632 return -EINVAL;
499 633
500 dir = debugfs_create_dir(APP_DIR, NULL); 634 dir = debugfs_create_dir(APP_DIR, NULL);
501 if (!dir) { 635 if (!dir) {
502 pr_err(MODULE_NAME ": Couldn't create relay app directory.\n"); 636 pr_err(NAME "Couldn't create relay app directory.\n");
503 return -ENOMEM; 637 return -ENOMEM;
504 } 638 }
505 639
506 chan = create_channel(subbuf_size, n_subbufs); 640 enabled_file = debugfs_create_file("enabled", 0600, dir, NULL,
507 if (!chan) { 641 &fops_enabled);
642 if (!enabled_file) {
643 pr_err(NAME "Couldn't create enabled file.\n");
508 debugfs_remove(dir); 644 debugfs_remove(dir);
509 pr_err(MODULE_NAME ": relay app channel creation failed\n");
510 return -ENOMEM; 645 return -ENOMEM;
511 } 646 }
512 647
513 reference_kmmio(); 648 if (enable_now)
514 649 enable_mmiotrace();
515 proc_marker_file = create_proc_entry(MARKER_FILE, 0, NULL);
516 if (proc_marker_file)
517 proc_marker_file->write_proc = write_marker;
518 650
519 pr_debug(MODULE_NAME ": loaded.\n");
520 if (nommiotrace)
521 pr_info(MODULE_NAME ": MMIO tracing disabled.\n");
522 if (ISA_trace)
523 pr_warning(MODULE_NAME ": Warning! low ISA range will be "
524 "traced.\n");
525 return 0; 651 return 0;
526} 652}
527 653
528static void __exit cleanup(void) 654static void __exit cleanup(void)
529{ 655{
530 pr_debug(MODULE_NAME ": unload...\n"); 656 pr_debug(NAME "unload...\n");
531 clear_trace_list(); 657 if (enabled_file)
532 unreference_kmmio(); 658 debugfs_remove(enabled_file);
533 remove_proc_entry(MARKER_FILE, NULL); 659 disable_mmiotrace();
534 destroy_channel();
535 if (dir) 660 if (dir)
536 debugfs_remove(dir); 661 debugfs_remove(dir);
537} 662}