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 | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 27 | #include <asm/io.h> |
| 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 | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 35 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 36 | #include "pf_in.h" |
| 37 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 38 | #define NAME "mmiotrace: " |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 39 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 40 | struct trap_reason { |
| 41 | unsigned long addr; |
| 42 | unsigned long ip; |
| 43 | enum reason_type type; |
| 44 | int active_traces; |
| 45 | }; |
| 46 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 47 | struct remap_trace { |
| 48 | struct list_head list; |
| 49 | struct kmmio_probe probe; |
| 50 | unsigned long phys; |
| 51 | unsigned long id; |
| 52 | }; |
| 53 | |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 54 | /* Accessed per-cpu. */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 55 | static DEFINE_PER_CPU(struct trap_reason, pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 56 | static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 57 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 58 | #if 0 /* XXX: no way gather this info anymore */ |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 59 | /* Access to this is not per-cpu. */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 60 | static DEFINE_PER_CPU(atomic_t, dropped); |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 61 | #endif |
Pekka Paalanen | fe1ffaf | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 62 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 63 | static struct dentry *marker_file; |
| 64 | |
| 65 | static DEFINE_MUTEX(mmiotrace_mutex); |
| 66 | static DEFINE_SPINLOCK(trace_lock); |
| 67 | static atomic_t mmiotrace_enabled; |
| 68 | static LIST_HEAD(trace_list); /* struct remap_trace */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Locking in this file: |
| 72 | * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections. |
| 73 | * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex |
| 74 | * and trace_lock. |
| 75 | * - Routines depending on is_enabled() must take trace_lock. |
| 76 | * - trace_list users must hold trace_lock. |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 77 | * - is_enabled() guarantees that mmio_trace_record is allowed. |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 78 | * - pre/post callbacks assume the effect of is_enabled() being true. |
| 79 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 80 | |
| 81 | /* module parameters */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 82 | static unsigned long filter_offset; |
| 83 | static int nommiotrace; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 84 | static int trace_pc; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 85 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 86 | module_param(filter_offset, ulong, 0); |
| 87 | module_param(nommiotrace, bool, 0); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 88 | module_param(trace_pc, bool, 0); |
| 89 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 90 | MODULE_PARM_DESC(filter_offset, "Start address of traced mappings."); |
| 91 | MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing."); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 92 | MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions."); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 93 | |
| 94 | static bool is_enabled(void) |
| 95 | { |
| 96 | return atomic_read(&mmiotrace_enabled); |
| 97 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 98 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 99 | #if 0 /* XXX: needs rewrite */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 100 | /* |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 101 | * Write callback for the debugfs entry: |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 102 | * Read a marker and write it to the mmio trace log |
| 103 | */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 104 | static ssize_t write_marker(struct file *file, const char __user *buffer, |
| 105 | size_t count, loff_t *ppos) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 106 | { |
| 107 | char *event = NULL; |
| 108 | struct mm_io_header *headp; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 109 | ssize_t len = (count > 65535) ? 65535 : count; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 110 | |
| 111 | event = kzalloc(sizeof(*headp) + len, GFP_KERNEL); |
| 112 | if (!event) |
| 113 | return -ENOMEM; |
| 114 | |
| 115 | headp = (struct mm_io_header *)event; |
| 116 | headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT); |
| 117 | headp->data_len = len; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 118 | |
| 119 | if (copy_from_user(event + sizeof(*headp), buffer, len)) { |
| 120 | kfree(event); |
| 121 | return -EFAULT; |
| 122 | } |
| 123 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 124 | spin_lock_irq(&trace_lock); |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 125 | #if 0 /* XXX: convert this to use tracing */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 126 | if (is_enabled()) |
| 127 | relay_write(chan, event, sizeof(*headp) + len); |
| 128 | else |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 129 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 130 | len = -EINVAL; |
| 131 | spin_unlock_irq(&trace_lock); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 132 | kfree(event); |
| 133 | return len; |
| 134 | } |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 135 | #endif |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 136 | |
| 137 | static void print_pte(unsigned long address) |
| 138 | { |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 139 | int level; |
| 140 | pte_t *pte = lookup_address(address, &level); |
| 141 | |
| 142 | if (!pte) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 143 | 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] | 144 | __func__, address); |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (level == PG_LEVEL_2M) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 149 | pr_emerg(NAME "4MB pages are not currently supported: " |
| 150 | "0x%08lx\n", address); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 151 | BUG(); |
| 152 | } |
Randy Dunlap | 0663bb6 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 153 | pr_info(NAME "pte for 0x%lx: 0x%llx 0x%llx\n", address, |
| 154 | (unsigned long long)pte_val(*pte), |
| 155 | (unsigned long long)pte_val(*pte) & _PAGE_PRESENT); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * For some reason the pre/post pairs have been called in an |
| 160 | * unmatched order. Report and die. |
| 161 | */ |
| 162 | static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr) |
| 163 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 164 | const struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 165 | pr_emerg(NAME "unexpected fault for address: 0x%08lx, " |
| 166 | "last fault for address: 0x%08lx\n", |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 167 | addr, my_reason->addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 168 | print_pte(addr); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 169 | print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip); |
| 170 | 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] | 171 | #ifdef __i386__ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 172 | pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 173 | regs->ax, regs->bx, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 174 | pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 175 | regs->si, regs->di, regs->bp, regs->sp); |
| 176 | #else |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 177 | pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 178 | regs->ax, regs->cx, regs->dx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 179 | pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n", |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 180 | regs->si, regs->di, regs->bp, regs->sp); |
| 181 | #endif |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 182 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 183 | BUG(); |
| 184 | } |
| 185 | |
| 186 | static void pre(struct kmmio_probe *p, struct pt_regs *regs, |
| 187 | unsigned long addr) |
| 188 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 189 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 190 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 191 | const unsigned long instptr = instruction_pointer(regs); |
| 192 | const enum reason_type type = get_ins_type(instptr); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 193 | struct remap_trace *trace = p->user_data; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 194 | |
| 195 | /* 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] | 196 | if (my_reason->active_traces) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 197 | die_kmmio_nesting_error(regs, addr); |
| 198 | else |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 199 | my_reason->active_traces++; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 200 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 201 | my_reason->type = type; |
| 202 | my_reason->addr = addr; |
| 203 | my_reason->ip = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 204 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 205 | my_trace->phys = addr - trace->probe.addr + trace->phys; |
| 206 | my_trace->map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Only record the program counter when requested. |
| 210 | * It may taint clean-room reverse engineering. |
| 211 | */ |
| 212 | if (trace_pc) |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 213 | my_trace->pc = instptr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 214 | else |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 215 | my_trace->pc = 0; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 216 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 217 | /* |
| 218 | * XXX: the timestamp recorded will be *after* the tracing has been |
| 219 | * done, not at the time we hit the instruction. SMP implications |
| 220 | * on event ordering? |
| 221 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 222 | |
| 223 | switch (type) { |
| 224 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 225 | my_trace->opcode = MMIO_READ; |
| 226 | my_trace->width = get_ins_mem_width(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 227 | break; |
| 228 | case REG_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 229 | my_trace->opcode = MMIO_WRITE; |
| 230 | my_trace->width = get_ins_mem_width(instptr); |
| 231 | my_trace->value = get_ins_reg_val(instptr, regs); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 232 | break; |
| 233 | case IMM_WRITE: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 234 | my_trace->opcode = MMIO_WRITE; |
| 235 | my_trace->width = get_ins_mem_width(instptr); |
| 236 | my_trace->value = get_ins_imm_val(instptr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 237 | break; |
| 238 | default: |
| 239 | { |
| 240 | unsigned char *ip = (unsigned char *)instptr; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 241 | my_trace->opcode = MMIO_UNKNOWN_OP; |
| 242 | my_trace->width = 0; |
| 243 | my_trace->value = (*ip) << 16 | *(ip + 1) << 8 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 244 | *(ip + 2); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 245 | } |
| 246 | } |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 247 | put_cpu_var(cpu_trace); |
| 248 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void post(struct kmmio_probe *p, unsigned long condition, |
| 252 | struct pt_regs *regs) |
| 253 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 254 | struct trap_reason *my_reason = &get_cpu_var(pf_reason); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 255 | struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 256 | |
| 257 | /* this should always return the active_trace count to 0 */ |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 258 | my_reason->active_traces--; |
| 259 | if (my_reason->active_traces) { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 260 | pr_emerg(NAME "unexpected post handler"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 261 | BUG(); |
| 262 | } |
| 263 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 264 | switch (my_reason->type) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 265 | case REG_READ: |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 266 | my_trace->value = get_ins_reg_val(my_reason->ip, regs); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 267 | break; |
| 268 | default: |
| 269 | break; |
| 270 | } |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 271 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 272 | mmio_trace_rw(my_trace); |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 273 | put_cpu_var(cpu_trace); |
| 274 | put_cpu_var(pf_reason); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 277 | static void ioremap_trace_core(unsigned long offset, unsigned long size, |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 278 | void __iomem *addr) |
| 279 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 280 | static atomic_t next_id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 281 | struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 282 | struct mmiotrace_map map = { |
| 283 | .phys = offset, |
| 284 | .virt = (unsigned long)addr, |
| 285 | .len = size, |
| 286 | .opcode = MMIO_PROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 287 | }; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 288 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 289 | if (!trace) { |
| 290 | pr_err(NAME "kmalloc failed in ioremap\n"); |
| 291 | return; |
| 292 | } |
| 293 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 294 | *trace = (struct remap_trace) { |
| 295 | .probe = { |
| 296 | .addr = (unsigned long)addr, |
| 297 | .len = size, |
| 298 | .pre_handler = pre, |
| 299 | .post_handler = post, |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 300 | .user_data = trace |
| 301 | }, |
| 302 | .phys = offset, |
| 303 | .id = atomic_inc_return(&next_id) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 304 | }; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 305 | map.map_id = trace->id; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 306 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 307 | spin_lock_irq(&trace_lock); |
| 308 | if (!is_enabled()) |
| 309 | goto not_enabled; |
| 310 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 311 | mmio_trace_mapping(&map); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 312 | list_add_tail(&trace->list, &trace_list); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 313 | if (!nommiotrace) |
| 314 | register_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 315 | |
| 316 | not_enabled: |
| 317 | spin_unlock_irq(&trace_lock); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 320 | void |
| 321 | mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 322 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 323 | if (!is_enabled()) /* recheck and proper locking in *_core() */ |
| 324 | return; |
| 325 | |
| 326 | pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 327 | if ((filter_offset) && (offset != filter_offset)) |
| 328 | return; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 329 | ioremap_trace_core(offset, size, addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 332 | static void iounmap_trace_core(volatile void __iomem *addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 333 | { |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 334 | struct mmiotrace_map map = { |
| 335 | .phys = 0, |
| 336 | .virt = (unsigned long)addr, |
| 337 | .len = 0, |
| 338 | .opcode = MMIO_UNPROBE |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 339 | }; |
| 340 | struct remap_trace *trace; |
| 341 | struct remap_trace *tmp; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 342 | struct remap_trace *found_trace = NULL; |
| 343 | |
| 344 | pr_debug(NAME "Unmapping %p.\n", addr); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 345 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 346 | spin_lock_irq(&trace_lock); |
| 347 | if (!is_enabled()) |
| 348 | goto not_enabled; |
| 349 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 350 | list_for_each_entry_safe(trace, tmp, &trace_list, list) { |
| 351 | if ((unsigned long)addr == trace->probe.addr) { |
| 352 | if (!nommiotrace) |
| 353 | unregister_kmmio_probe(&trace->probe); |
| 354 | list_del(&trace->list); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 355 | found_trace = trace; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | } |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 359 | map.map_id = (found_trace) ? found_trace->id : -1; |
| 360 | mmio_trace_mapping(&map); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 361 | |
| 362 | not_enabled: |
| 363 | spin_unlock_irq(&trace_lock); |
| 364 | if (found_trace) { |
| 365 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 366 | kfree(found_trace); |
| 367 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 368 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 369 | |
| 370 | void mmiotrace_iounmap(volatile void __iomem *addr) |
| 371 | { |
| 372 | might_sleep(); |
| 373 | if (is_enabled()) /* recheck and proper locking in *_core() */ |
| 374 | iounmap_trace_core(addr); |
| 375 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 376 | |
| 377 | static void clear_trace_list(void) |
| 378 | { |
| 379 | struct remap_trace *trace; |
| 380 | struct remap_trace *tmp; |
| 381 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 382 | /* |
| 383 | * No locking required, because the caller ensures we are in a |
| 384 | * critical section via mutex, and is_enabled() is false, |
| 385 | * i.e. nothing can traverse or modify this list. |
| 386 | * Caller also ensures is_enabled() cannot change. |
| 387 | */ |
| 388 | list_for_each_entry(trace, &trace_list, list) { |
| 389 | pr_notice(NAME "purging non-iounmapped " |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 390 | "trace @0x%08lx, size 0x%lx.\n", |
| 391 | trace->probe.addr, trace->probe.len); |
| 392 | if (!nommiotrace) |
| 393 | unregister_kmmio_probe(&trace->probe); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 394 | } |
| 395 | synchronize_rcu(); /* unregister_kmmio_probe() requirement */ |
| 396 | |
| 397 | list_for_each_entry_safe(trace, tmp, &trace_list, list) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 398 | list_del(&trace->list); |
| 399 | kfree(trace); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 403 | #if 0 /* XXX: out of order */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 404 | static struct file_operations fops_marker = { |
| 405 | .owner = THIS_MODULE, |
| 406 | .write = write_marker |
| 407 | }; |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 408 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 409 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 410 | void enable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 411 | { |
| 412 | mutex_lock(&mmiotrace_mutex); |
| 413 | if (is_enabled()) |
| 414 | goto out; |
| 415 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 416 | #if 0 /* XXX: tracing does not support text entries */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 417 | marker_file = debugfs_create_file("marker", 0660, dir, NULL, |
| 418 | &fops_marker); |
| 419 | if (!marker_file) |
| 420 | pr_err(NAME "marker file creation failed.\n"); |
Pekka Paalanen | bd8ac68 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 421 | #endif |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 422 | |
| 423 | if (nommiotrace) |
| 424 | pr_info(NAME "MMIO tracing disabled.\n"); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 425 | spin_lock_irq(&trace_lock); |
| 426 | atomic_inc(&mmiotrace_enabled); |
| 427 | spin_unlock_irq(&trace_lock); |
| 428 | pr_info(NAME "enabled.\n"); |
| 429 | out: |
| 430 | mutex_unlock(&mmiotrace_mutex); |
| 431 | } |
| 432 | |
Pekka Paalanen | f984b51 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 433 | void disable_mmiotrace(void) |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 434 | { |
| 435 | mutex_lock(&mmiotrace_mutex); |
| 436 | if (!is_enabled()) |
| 437 | goto out; |
| 438 | |
| 439 | spin_lock_irq(&trace_lock); |
| 440 | atomic_dec(&mmiotrace_enabled); |
| 441 | BUG_ON(is_enabled()); |
| 442 | spin_unlock_irq(&trace_lock); |
| 443 | |
| 444 | clear_trace_list(); /* guarantees: no more kmmio callbacks */ |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 445 | if (marker_file) { |
| 446 | debugfs_remove(marker_file); |
| 447 | marker_file = NULL; |
| 448 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 449 | |
| 450 | pr_info(NAME "disabled.\n"); |
| 451 | out: |
| 452 | mutex_unlock(&mmiotrace_mutex); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 453 | } |