Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 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 | */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 22 | #define DEBUG 1 |
| 23 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 24 | #include <linux/module.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 27 | #include <linux/io.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 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 */ |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 33 | #include <asm/atomic.h> |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 34 | #include <linux/percpu.h> |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 35 | #include <linux/cpu.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 36 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 37 | #include "pf_in.h" |
| 38 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 39 | #define NAME "mmiotrace: " |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 40 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 41 | struct trap_reason { |
| 42 | unsigned long addr; |
| 43 | unsigned long ip; |
| 44 | enum reason_type type; |
| 45 | int active_traces; |
| 46 | }; |
| 47 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 48 | struct remap_trace { |
| 49 | struct list_head list; |
| 50 | struct kmmio_probe probe; |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 51 | resource_size_t phys; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 52 | unsigned long id; |
| 53 | }; |
| 54 | |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 55 | /* Accessed per-cpu. */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 56 | static DEFINE_PER_CPU(struct trap_reason, pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 57 | static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 58 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 59 | #if 0 /* XXX: no way gather this info anymore */ |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 60 | /* Access to this is not per-cpu. */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 61 | static DEFINE_PER_CPU(atomic_t, dropped); |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 62 | #endif |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 63 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 64 | static struct dentry *marker_file; |
| 65 | |
| 66 | static DEFINE_MUTEX(mmiotrace_mutex); |
| 67 | static DEFINE_SPINLOCK(trace_lock); |
| 68 | static atomic_t mmiotrace_enabled; |
| 69 | static LIST_HEAD(trace_list); /* struct remap_trace */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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. |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 78 | * - is_enabled() guarantees that mmio_trace_record is allowed. |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 79 | * - pre/post callbacks assume the effect of is_enabled() being true. |
| 80 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 81 | |
| 82 | /* module parameters */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 83 | static unsigned long filter_offset; |
| 84 | static int nommiotrace; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 85 | static int trace_pc; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 86 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 87 | module_param(filter_offset, ulong, 0); |
| 88 | module_param(nommiotrace, bool, 0); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 89 | module_param(trace_pc, bool, 0); |
| 90 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 91 | MODULE_PARM_DESC(filter_offset, "Start address of traced mappings."); |
| 92 | MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing."); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 93 | MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions."); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 94 | |
| 95 | static bool is_enabled(void) |
| 96 | { |
| 97 | return atomic_read(&mmiotrace_enabled); |
| 98 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 99 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 100 | #if 0 /* XXX: needs rewrite */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 101 | /* |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 102 | * Write callback for the debugfs entry: |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 103 | * Read a marker and write it to the mmio trace log |
| 104 | */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 105 | static ssize_t write_marker(struct file *file, const char __user *buffer, |
| 106 | size_t count, loff_t *ppos) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 107 | { |
| 108 | char *event = NULL; |
| 109 | struct mm_io_header *headp; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 110 | ssize_t len = (count > 65535) ? 65535 : count; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 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; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 119 | |
| 120 | if (copy_from_user(event + sizeof(*headp), buffer, len)) { |
| 121 | kfree(event); |
| 122 | return -EFAULT; |
| 123 | } |
| 124 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 125 | spin_lock_irq(&trace_lock); |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 126 | #if 0 /* XXX: convert this to use tracing */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 127 | if (is_enabled()) |
| 128 | relay_write(chan, event, sizeof(*headp) + len); |
| 129 | else |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 130 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 131 | len = -EINVAL; |
| 132 | spin_unlock_irq(&trace_lock); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 133 | kfree(event); |
| 134 | return len; |
| 135 | } |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 136 | #endif |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 137 | |
| 138 | static void print_pte(unsigned long address) |
| 139 | { |
Pekka Paalanen | 790e2a2 | 2008-05-12 21:21:14 +0200 | [diff] [blame] | 140 | unsigned int level; |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 141 | pte_t *pte = lookup_address(address, &level); |
| 142 | |
| 143 | if (!pte) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 144 | pr_err(NAME "Error in %s: no pte for page 0x%08lx\n", |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 145 | __func__, address); |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (level == PG_LEVEL_2M) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 150 | pr_emerg(NAME "4MB pages are not currently supported: " |
| 151 | "0x%08lx\n", address); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 152 | BUG(); |
| 153 | } |
Randy Dunlap | 0663bb6 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 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); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * For some reason the pre/post pairs have been called in an |
| 161 | * unmatched order. Report and die. |
| 162 | */ |
| 163 | static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr) |
| 164 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 165 | const struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 166 | pr_emerg(NAME "unexpected fault for address: 0x%08lx, " |
| 167 | "last fault for address: 0x%08lx\n", |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 168 | addr, my_reason->addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 169 | print_pte(addr); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 172 | #ifdef __i386__ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 173 | pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 174 | regs->ax, regs->bx, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 175 | pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 176 | regs->si, regs->di, regs->bp, regs->sp); |
| 177 | #else |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 178 | pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 179 | regs->ax, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 180 | pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 181 | regs->si, regs->di, regs->bp, regs->sp); |
| 182 | #endif |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 183 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 184 | BUG(); |
| 185 | } |
| 186 | |
| 187 | static void pre(struct kmmio_probe *p, struct pt_regs *regs, |
| 188 | unsigned long addr) |
| 189 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 190 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 191 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 192 | const unsigned long instptr = instruction_pointer(regs); |
| 193 | const enum reason_type type = get_ins_type(instptr); |
Pekka Paalanen | a50445d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 194 | struct remap_trace *trace = p->private; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 195 | |
| 196 | /* it doesn't make sense to have more than one active trace per cpu */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 197 | if (my_reason->active_traces) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 198 | die_kmmio_nesting_error(regs, addr); |
| 199 | else |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 200 | my_reason->active_traces++; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 201 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 202 | my_reason->type = type; |
| 203 | my_reason->addr = addr; |
| 204 | my_reason->ip = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 205 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 206 | my_trace->phys = addr - trace->probe.addr + trace->phys; |
| 207 | my_trace->map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Only record the program counter when requested. |
| 211 | * It may taint clean-room reverse engineering. |
| 212 | */ |
| 213 | if (trace_pc) |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 214 | my_trace->pc = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 215 | else |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 216 | my_trace->pc = 0; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 217 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 223 | |
| 224 | switch (type) { |
| 225 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 226 | my_trace->opcode = MMIO_READ; |
| 227 | my_trace->width = get_ins_mem_width(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 228 | break; |
| 229 | case REG_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 233 | break; |
| 234 | case IMM_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 235 | my_trace->opcode = MMIO_WRITE; |
| 236 | my_trace->width = get_ins_mem_width(instptr); |
| 237 | my_trace->value = get_ins_imm_val(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | { |
| 241 | unsigned char *ip = (unsigned char *)instptr; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 242 | my_trace->opcode = MMIO_UNKNOWN_OP; |
| 243 | my_trace->width = 0; |
| 244 | my_trace->value = (*ip) << 16 | *(ip + 1) << 8 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 245 | *(ip + 2); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 248 | put_cpu_var(cpu_trace); |
| 249 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void post(struct kmmio_probe *p, unsigned long condition, |
| 253 | struct pt_regs *regs) |
| 254 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 255 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 256 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 257 | |
| 258 | /* this should always return the active_trace count to 0 */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 259 | my_reason->active_traces--; |
| 260 | if (my_reason->active_traces) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 261 | pr_emerg(NAME "unexpected post handler"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 262 | BUG(); |
| 263 | } |
| 264 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 265 | switch (my_reason->type) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 266 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 267 | my_trace->value = get_ins_reg_val(my_reason->ip, regs); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 268 | break; |
| 269 | default: |
| 270 | break; |
| 271 | } |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 272 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 273 | mmio_trace_rw(my_trace); |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 274 | put_cpu_var(cpu_trace); |
| 275 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 278 | static void ioremap_trace_core(resource_size_t offset, unsigned long size, |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 279 | void __iomem *addr) |
| 280 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 281 | static atomic_t next_id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 282 | struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL); |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 283 | /* These are page-unaligned. */ |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 284 | struct mmiotrace_map map = { |
| 285 | .phys = offset, |
| 286 | .virt = (unsigned long)addr, |
| 287 | .len = size, |
| 288 | .opcode = MMIO_PROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 289 | }; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 290 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 291 | if (!trace) { |
| 292 | pr_err(NAME "kmalloc failed in ioremap\n"); |
| 293 | return; |
| 294 | } |
| 295 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 296 | *trace = (struct remap_trace) { |
| 297 | .probe = { |
| 298 | .addr = (unsigned long)addr, |
| 299 | .len = size, |
| 300 | .pre_handler = pre, |
| 301 | .post_handler = post, |
Pekka Paalanen | a50445d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 302 | .private = trace |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 303 | }, |
| 304 | .phys = offset, |
| 305 | .id = atomic_inc_return(&next_id) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 306 | }; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 307 | map.map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 308 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 309 | spin_lock_irq(&trace_lock); |
| 310 | if (!is_enabled()) |
| 311 | goto not_enabled; |
| 312 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 313 | mmio_trace_mapping(&map); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 314 | list_add_tail(&trace->list, &trace_list); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 315 | if (!nommiotrace) |
| 316 | register_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 317 | |
| 318 | not_enabled: |
| 319 | spin_unlock_irq(&trace_lock); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 322 | void mmiotrace_ioremap(resource_size_t offset, unsigned long size, |
| 323 | void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 324 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 325 | if (!is_enabled()) /* recheck and proper locking in *_core() */ |
| 326 | return; |
| 327 | |
Pekka Paalanen | dee310d | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 328 | pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n", |
| 329 | (unsigned long long)offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 330 | if ((filter_offset) && (offset != filter_offset)) |
| 331 | return; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 332 | ioremap_trace_core(offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 335 | static void iounmap_trace_core(volatile void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 336 | { |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 337 | struct mmiotrace_map map = { |
| 338 | .phys = 0, |
| 339 | .virt = (unsigned long)addr, |
| 340 | .len = 0, |
| 341 | .opcode = MMIO_UNPROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 342 | }; |
| 343 | struct remap_trace *trace; |
| 344 | struct remap_trace *tmp; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 345 | struct remap_trace *found_trace = NULL; |
| 346 | |
| 347 | pr_debug(NAME "Unmapping %p.\n", addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 348 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 349 | spin_lock_irq(&trace_lock); |
| 350 | if (!is_enabled()) |
| 351 | goto not_enabled; |
| 352 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 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); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 358 | found_trace = trace; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | } |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 362 | map.map_id = (found_trace) ? found_trace->id : -1; |
| 363 | mmio_trace_mapping(&map); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 364 | |
| 365 | not_enabled: |
| 366 | spin_unlock_irq(&trace_lock); |
| 367 | if (found_trace) { |
| 368 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 369 | kfree(found_trace); |
| 370 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 371 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 372 | |
| 373 | void 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); |
| 378 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 379 | |
| 380 | static void clear_trace_list(void) |
| 381 | { |
| 382 | struct remap_trace *trace; |
| 383 | struct remap_trace *tmp; |
| 384 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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 " |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 393 | "trace @0x%08lx, size 0x%lx.\n", |
| 394 | trace->probe.addr, trace->probe.len); |
| 395 | if (!nommiotrace) |
| 396 | unregister_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 397 | } |
| 398 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 399 | |
| 400 | list_for_each_entry_safe(trace, tmp, &trace_list, list) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 401 | list_del(&trace->list); |
| 402 | kfree(trace); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 406 | #ifdef CONFIG_HOTPLUG_CPU |
| 407 | static cpumask_t downed_cpus; |
| 408 | |
| 409 | static 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); |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 423 | if (!err) |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 424 | pr_info(NAME "CPU%d is down.\n", cpu); |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 425 | else |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 426 | pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 427 | } |
| 428 | if (num_online_cpus() > 1) |
| 429 | pr_warning(NAME "multiple CPUs still online, " |
| 430 | "may miss events.\n"); |
| 431 | } |
| 432 | |
Marcin Slusarz | 7701e8c | 2008-08-20 21:07:47 +0200 | [diff] [blame^] | 433 | /* __ref because leave_uniprocessor calls cpu_up which is __cpuinit, |
| 434 | but this whole function is ifdefed CONFIG_HOTPLUG_CPU */ |
| 435 | static void __ref leave_uniprocessor(void) |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 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 */ |
| 453 | static 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 | |
| 460 | static void leave_uniprocessor(void) |
| 461 | { |
| 462 | } |
| 463 | #endif |
| 464 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 465 | #if 0 /* XXX: out of order */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 466 | static struct file_operations fops_marker = { |
| 467 | .owner = THIS_MODULE, |
| 468 | .write = write_marker |
| 469 | }; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 470 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 471 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 472 | void enable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 473 | { |
| 474 | mutex_lock(&mmiotrace_mutex); |
| 475 | if (is_enabled()) |
| 476 | goto out; |
| 477 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 478 | #if 0 /* XXX: tracing does not support text entries */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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"); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 483 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 484 | |
| 485 | if (nommiotrace) |
| 486 | pr_info(NAME "MMIO tracing disabled.\n"); |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 487 | enter_uniprocessor(); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 488 | spin_lock_irq(&trace_lock); |
| 489 | atomic_inc(&mmiotrace_enabled); |
| 490 | spin_unlock_irq(&trace_lock); |
| 491 | pr_info(NAME "enabled.\n"); |
| 492 | out: |
| 493 | mutex_unlock(&mmiotrace_mutex); |
| 494 | } |
| 495 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 496 | void disable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 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 */ |
Pekka Paalanen | 7423d11 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 508 | leave_uniprocessor(); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 509 | if (marker_file) { |
| 510 | debugfs_remove(marker_file); |
| 511 | marker_file = NULL; |
| 512 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 513 | |
| 514 | pr_info(NAME "disabled.\n"); |
| 515 | out: |
| 516 | mutex_unlock(&mmiotrace_mutex); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 517 | } |