tracing: x86, mmiotrace: only register for die notifier when tracer active
[linux-2.6.git] / arch / x86 / mm / mmio-mod.c
index 8256546..132772a 100644 (file)
@@ -24,7 +24,7 @@
 #include <linux/module.h>
 #include <linux/debugfs.h>
 #include <linux/uaccess.h>
-#include <asm/io.h>
+#include <linux/io.h>
 #include <linux/version.h>
 #include <linux/kallsyms.h>
 #include <asm/pgtable.h>
@@ -32,6 +32,7 @@
 #include <asm/e820.h> /* for ISA_START_ADDRESS */
 #include <asm/atomic.h>
 #include <linux/percpu.h>
+#include <linux/cpu.h>
 
 #include "pf_in.h"
 
@@ -47,7 +48,7 @@ struct trap_reason {
 struct remap_trace {
        struct list_head list;
        struct kmmio_probe probe;
-       unsigned long phys;
+       resource_size_t phys;
        unsigned long id;
 };
 
@@ -55,13 +56,6 @@ struct remap_trace {
 static DEFINE_PER_CPU(struct trap_reason, pf_reason);
 static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace);
 
-#if 0 /* XXX: no way gather this info anymore */
-/* Access to this is not per-cpu. */
-static DEFINE_PER_CPU(atomic_t, dropped);
-#endif
-
-static struct dentry *marker_file;
-
 static DEFINE_MUTEX(mmiotrace_mutex);
 static DEFINE_SPINLOCK(trace_lock);
 static atomic_t mmiotrace_enabled;
@@ -74,24 +68,21 @@ static LIST_HEAD(trace_list);               /* struct remap_trace */
  *   and trace_lock.
  * - Routines depending on is_enabled() must take trace_lock.
  * - trace_list users must hold trace_lock.
- * - is_enabled() guarantees that mmio_trace_record is allowed.
+ * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed.
  * - pre/post callbacks assume the effect of is_enabled() being true.
  */
 
 /* module parameters */
 static unsigned long   filter_offset;
 static int             nommiotrace;
-static int             ISA_trace;
 static int             trace_pc;
 
 module_param(filter_offset, ulong, 0);
 module_param(nommiotrace, bool, 0);
-module_param(ISA_trace, 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(ISA_trace, "Do not exclude the low ISA range.");
 MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
 
 static bool is_enabled(void)
@@ -99,47 +90,9 @@ static bool is_enabled(void)
        return atomic_read(&mmiotrace_enabled);
 }
 
-#if 0 /* XXX: needs rewrite */
-/*
- * Write callback for the debugfs entry:
- * Read a marker and write it to the mmio trace log
- */
-static ssize_t write_marker(struct file *file, const char __user *buffer,
-                                               size_t count, loff_t *ppos)
-{
-       char *event = NULL;
-       struct mm_io_header *headp;
-       ssize_t len = (count > 65535) ? 65535 : count;
-
-       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;
-       }
-
-       spin_lock_irq(&trace_lock);
-#if 0 /* XXX: convert this to use tracing */
-       if (is_enabled())
-               relay_write(chan, event, sizeof(*headp) + len);
-       else
-#endif
-               len = -EINVAL;
-       spin_unlock_irq(&trace_lock);
-       kfree(event);
-       return len;
-}
-#endif
-
 static void print_pte(unsigned long address)
 {
-       int level;
+       unsigned int level;
        pte_t *pte = lookup_address(address, &level);
 
        if (!pte) {
@@ -153,8 +106,9 @@ static void print_pte(unsigned long address)
                                                        "0x%08lx\n", address);
                BUG();
        }
-       pr_info(NAME "pte for 0x%lx: 0x%lx 0x%lx\n", address, pte_val(*pte),
-                                               pte_val(*pte) & _PAGE_PRESENT);
+       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);
 }
 
 /*
@@ -192,7 +146,7 @@ static void pre(struct kmmio_probe *p, struct pt_regs *regs,
        struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
        const unsigned long instptr = instruction_pointer(regs);
        const enum reason_type type = get_ins_type(instptr);
-       struct remap_trace *trace = p->user_data;
+       struct remap_trace *trace = p->private;
 
        /* it doesn't make sense to have more than one active trace per cpu */
        if (my_reason->active_traces)
@@ -276,11 +230,12 @@ static void post(struct kmmio_probe *p, unsigned long condition,
        put_cpu_var(pf_reason);
 }
 
-static void ioremap_trace_core(unsigned long offset, unsigned long size,
+static void ioremap_trace_core(resource_size_t offset, unsigned long size,
                                                        void __iomem *addr)
 {
        static atomic_t next_id;
        struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
+       /* These are page-unaligned. */
        struct mmiotrace_map map = {
                .phys = offset,
                .virt = (unsigned long)addr,
@@ -299,7 +254,7 @@ static void ioremap_trace_core(unsigned long offset, unsigned long size,
                        .len = size,
                        .pre_handler = pre,
                        .post_handler = post,
-                       .user_data = trace
+                       .private = trace
                },
                .phys = offset,
                .id = atomic_inc_return(&next_id)
@@ -307,8 +262,10 @@ static void ioremap_trace_core(unsigned long offset, unsigned long size,
        map.map_id = trace->id;
 
        spin_lock_irq(&trace_lock);
-       if (!is_enabled())
+       if (!is_enabled()) {
+               kfree(trace);
                goto not_enabled;
+       }
 
        mmio_trace_mapping(&map);
        list_add_tail(&trace->list, &trace_list);
@@ -319,13 +276,14 @@ not_enabled:
        spin_unlock_irq(&trace_lock);
 }
 
-void
-mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
+void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
+                                               void __iomem *addr)
 {
        if (!is_enabled()) /* recheck and proper locking in *_core() */
                return;
 
-       pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr);
+       pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n",
+                               (unsigned long long)offset, size, addr);
        if ((filter_offset) && (offset != filter_offset))
                return;
        ioremap_trace_core(offset, size, addr);
@@ -376,6 +334,23 @@ void mmiotrace_iounmap(volatile void __iomem *addr)
                iounmap_trace_core(addr);
 }
 
+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);
+
 static void clear_trace_list(void)
 {
        struct remap_trace *trace;
@@ -402,11 +377,70 @@ static void clear_trace_list(void)
        }
 }
 
-#if 0 /* XXX: out of order */
-static struct file_operations fops_marker = {
-       .owner =        THIS_MODULE,
-       .write =        write_marker
-};
+#ifdef CONFIG_HOTPLUG_CPU
+static cpumask_var_t downed_cpus;
+
+static void enter_uniprocessor(void)
+{
+       int cpu;
+       int err;
+
+       if (downed_cpus == NULL &&
+           !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) {
+               pr_notice(NAME "Failed to allocate mask\n");
+               goto out;
+       }
+
+       get_online_cpus();
+       cpumask_copy(downed_cpus, cpu_online_mask);
+       cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus);
+       if (num_online_cpus() > 1)
+               pr_notice(NAME "Disabling non-boot CPUs...\n");
+       put_online_cpus();
+
+       for_each_cpu(cpu, downed_cpus) {
+               err = cpu_down(cpu);
+               if (!err)
+                       pr_info(NAME "CPU%d is down.\n", cpu);
+               else
+                       pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err);
+       }
+out:
+       if (num_online_cpus() > 1)
+               pr_warning(NAME "multiple CPUs still online, "
+                                               "may miss events.\n");
+}
+
+/* __ref because leave_uniprocessor calls cpu_up which is __cpuinit,
+   but this whole function is ifdefed CONFIG_HOTPLUG_CPU */
+static void __ref leave_uniprocessor(void)
+{
+       int cpu;
+       int err;
+
+       if (downed_cpus == NULL || cpumask_weight(downed_cpus) == 0)
+               return;
+       pr_notice(NAME "Re-enabling CPUs...\n");
+       for_each_cpu(cpu, downed_cpus) {
+               err = cpu_up(cpu);
+               if (!err)
+                       pr_info(NAME "enabled CPU%d.\n", cpu);
+               else
+                       pr_err(NAME "cannot re-enable CPU%d: %d\n", cpu, err);
+       }
+}
+
+#else /* !CONFIG_HOTPLUG_CPU */
+static void enter_uniprocessor(void)
+{
+       if (num_online_cpus() > 1)
+               pr_warning(NAME "multiple CPUs are online, may miss events. "
+                       "Suggest booting with maxcpus=1 kernel argument.\n");
+}
+
+static void leave_uniprocessor(void)
+{
+}
 #endif
 
 void enable_mmiotrace(void)
@@ -415,17 +449,10 @@ void enable_mmiotrace(void)
        if (is_enabled())
                goto out;
 
-#if 0 /* XXX: tracing does not support text entries */
-       marker_file = debugfs_create_file("marker", 0660, dir, NULL,
-                                                               &fops_marker);
-       if (!marker_file)
-               pr_err(NAME "marker file creation failed.\n");
-#endif
-
        if (nommiotrace)
                pr_info(NAME "MMIO tracing disabled.\n");
-       if (ISA_trace)
-               pr_warning(NAME "Warning! low ISA range will be traced.\n");
+       kmmio_init();
+       enter_uniprocessor();
        spin_lock_irq(&trace_lock);
        atomic_inc(&mmiotrace_enabled);
        spin_unlock_irq(&trace_lock);
@@ -446,11 +473,8 @@ void disable_mmiotrace(void)
        spin_unlock_irq(&trace_lock);
 
        clear_trace_list(); /* guarantees: no more kmmio callbacks */
-       if (marker_file) {
-               debugfs_remove(marker_file);
-               marker_file = NULL;
-       }
-
+       leave_uniprocessor();
+       kmmio_cleanup();
        pr_info(NAME "disabled.\n");
 out:
        mutex_unlock(&mmiotrace_mutex);