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 */ 22 23#define pr_fmt(fmt) "mmiotrace: " fmt 24 25#define DEBUG 1 26 27#include <linux/module.h> 28#include <linux/debugfs.h> 29#include <linux/slab.h> 30#include <linux/uaccess.h> 31#include <linux/io.h> 32#include <linux/version.h> 33#include <linux/kallsyms.h> 34#include <asm/pgtable.h> 35#include <linux/mmiotrace.h> 36#include <asm/e820.h> /* for ISA_START_ADDRESS */ 37#include <asm/atomic.h> 38#include <linux/percpu.h> 39#include <linux/cpu.h> 40 41#include "pf_in.h" 42 43struct trap_reason { 44 unsigned long addr; 45 unsigned long ip; 46 enum reason_type type; 47 int active_traces; 48}; 49 50struct remap_trace { 51 struct list_head list; 52 struct kmmio_probe probe; 53 resource_size_t phys; 54 unsigned long id; 55}; 56 57/* Accessed per-cpu. */ 58static DEFINE_PER_CPU(struct trap_reason, pf_reason); 59static DEFINE_PER_CPU(struct mmiotrace_rw, cpu_trace); 60 61static DEFINE_MUTEX(mmiotrace_mutex); 62static DEFINE_SPINLOCK(trace_lock); 63static atomic_t mmiotrace_enabled; 64static LIST_HEAD(trace_list); /* struct remap_trace */ 65 66/* 67 * Locking in this file: 68 * - mmiotrace_mutex enforces enable/disable_mmiotrace() critical sections. 69 * - mmiotrace_enabled may be modified only when holding mmiotrace_mutex 70 * and trace_lock. 71 * - Routines depending on is_enabled() must take trace_lock. 72 * - trace_list users must hold trace_lock. 73 * - is_enabled() guarantees that mmio_trace_{rw,mapping} are allowed. 74 * - pre/post callbacks assume the effect of is_enabled() being true. 75 */ 76 77/* module parameters */ 78static unsigned long filter_offset; 79static int nommiotrace; 80static int trace_pc; 81 82module_param(filter_offset, ulong, 0); 83module_param(nommiotrace, bool, 0); 84module_param(trace_pc, bool, 0); 85 86MODULE_PARM_DESC(filter_offset, "Start address of traced mappings."); 87MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing."); 88MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions."); 89 90static bool is_enabled(void) 91{ 92 return atomic_read(&mmiotrace_enabled); 93} 94 95static void print_pte(unsigned long address) 96{ 97 unsigned int level; 98 pte_t *pte = lookup_address(address, &level); 99 100 if (!pte) { 101 pr_err("Error in %s: no pte for page 0x%08lx\n", 102 __func__, address); 103 return; 104 } 105 106 if (level == PG_LEVEL_2M) { 107 pr_emerg("4MB pages are not currently supported: 0x%08lx\n", 108 address); 109 BUG(); 110 } 111 pr_info("pte for 0x%lx: 0x%llx 0x%llx\n", 112 address, 113 (unsigned long long)pte_val(*pte), 114 (unsigned long long)pte_val(*pte) & _PAGE_PRESENT); 115} 116 117/* 118 * For some reason the pre/post pairs have been called in an 119 * unmatched order. Report and die. 120 */ 121static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr) 122{ 123 const struct trap_reason *my_reason = &get_cpu_var(pf_reason); 124 pr_emerg("unexpected fault for address: 0x%08lx, last fault for address: 0x%08lx\n", 125 addr, my_reason->addr); 126 print_pte(addr); 127 print_symbol(KERN_EMERG "faulting IP is at %s\n", regs->ip); 128 print_symbol(KERN_EMERG "last faulting IP was at %s\n", my_reason->ip); 129#ifdef __i386__ 130 pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", 131 regs->ax, regs->bx, regs->cx, regs->dx); 132 pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", 133 regs->si, regs->di, regs->bp, regs->sp); 134#else 135 pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n", 136 regs->ax, regs->cx, regs->dx); 137 pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n", 138 regs->si, regs->di, regs->bp, regs->sp); 139#endif 140 put_cpu_var(pf_reason); 141 BUG(); 142} 143 144static void pre(struct kmmio_probe *p, struct pt_regs *regs, 145 unsigned long addr) 146{ 147 struct trap_reason *my_reason = &get_cpu_var(pf_reason); 148 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); 149 const unsigned long instptr = instruction_pointer(regs); 150 const enum reason_type type = get_ins_type(instptr); 151 struct remap_trace *trace = p->private; 152 153 /* it doesn't make sense to have more than one active trace per cpu */ 154 if (my_reason->active_traces) 155 die_kmmio_nesting_error(regs, addr); 156 else 157 my_reason->active_traces++; 158 159 my_reason->type = type; 160 my_reason->addr = addr; 161 my_reason->ip = instptr; 162 163 my_trace->phys = addr - trace->probe.addr + trace->phys; 164 my_trace->map_id = trace->id; 165 166 /* 167 * Only record the program counter when requested. 168 * It may taint clean-room reverse engineering. 169 */ 170 if (trace_pc) 171 my_trace->pc = instptr; 172 else 173 my_trace->pc = 0; 174 175 176 switch (type) { 177 case REG_READ: 178 my_trace->opcode = MMIO_READ; 179 my_trace->width = get_ins_mem_width(instptr); 180 break; 181 case REG_WRITE: 182 my_trace->opcode = MMIO_WRITE; 183 my_trace->width = get_ins_mem_width(instptr); 184 my_trace->value = get_ins_reg_val(instptr, regs); 185 break; 186 case IMM_WRITE: 187 my_trace->opcode = MMIO_WRITE; 188 my_trace->width = get_ins_mem_width(instptr); 189 my_trace->value = get_ins_imm_val(instptr); 190 break; 191 default: 192 { 193 unsigned char *ip = (unsigned char *)instptr; 194 my_trace->opcode = MMIO_UNKNOWN_OP; 195 my_trace->width = 0; 196 my_trace->value = (*ip) << 16 | *(ip + 1) << 8 | 197 *(ip + 2); 198 } 199 } 200 put_cpu_var(cpu_trace); 201 put_cpu_var(pf_reason); 202} 203 204static void post(struct kmmio_probe *p, unsigned long condition, 205 struct pt_regs *regs) 206{ 207 struct trap_reason *my_reason = &get_cpu_var(pf_reason); 208 struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace); 209 210 /* this should always return the active_trace count to 0 */ 211 my_reason->active_traces--; 212 if (my_reason->active_traces) { 213 pr_emerg("unexpected post handler"); 214 BUG(); 215 } 216 217 switch (my_reason->type) { 218 case REG_READ: 219 my_trace->value = get_ins_reg_val(my_reason->ip, regs); 220 break; 221 default: 222 break; 223 } 224 225 mmio_trace_rw(my_trace); 226 put_cpu_var(cpu_trace); 227 put_cpu_var(pf_reason); 228} 229 230static void ioremap_trace_core(resource_size_t offset, unsigned long size, 231 void __iomem *addr) 232{ 233 static atomic_t next_id; 234 struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL); 235 /* These are page-unaligned. */ 236 struct mmiotrace_map map = { 237 .phys = offset, 238 .virt = (unsigned long)addr, 239 .len = size, 240 .opcode = MMIO_PROBE 241 }; 242 243 if (!trace) { 244 pr_err("kmalloc failed in ioremap\n"); 245 return; 246 } 247 248 *trace = (struct remap_trace) { 249 .probe = { 250 .addr = (unsigned long)addr, 251 .len = size, 252 .pre_handler = pre, 253 .post_handler = post, 254 .private = trace 255 }, 256 .phys = offset, 257 .id = atomic_inc_return(&next_id) 258 }; 259 map.map_id = trace->id; 260 261 spin_lock_irq(&trace_lock); 262 if (!is_enabled()) { 263 kfree(trace); 264 goto not_enabled; 265 } 266 267 mmio_trace_mapping(&map); 268 list_add_tail(&trace->list, &trace_list); 269 if (!nommiotrace) 270 register_kmmio_probe(&trace->probe); 271 272not_enabled: 273 spin_unlock_irq(&trace_lock); 274} 275 276void mmiotrace_ioremap(resource_size_t offset, unsigned long size, 277 void __iomem *addr) 278{ 279 if (!is_enabled()) /* recheck and proper locking in *_core() */ 280 return; 281 282 pr_debug("ioremap_*(0x%llx, 0x%lx) = %p\n", 283 (unsigned long long)offset, size, addr); 284 if ((filter_offset) && (offset != filter_offset)) 285 return; 286 ioremap_trace_core(offset, size, addr); 287} 288 289static void iounmap_trace_core(volatile void __iomem *addr) 290{ 291 struct mmiotrace_map map = { 292 .phys = 0, 293 .virt = (unsigned long)addr, 294 .len = 0, 295 .opcode = MMIO_UNPROBE 296 }; 297 struct remap_trace *trace; 298 struct remap_trace *tmp; 299 struct remap_trace *found_trace = NULL; 300 301 pr_debug("Unmapping %p.\n", addr); 302 303 spin_lock_irq(&trace_lock); 304 if (!is_enabled()) 305 goto not_enabled; 306 307 list_for_each_entry_safe(trace, tmp, &trace_list, list) { 308 if ((unsigned long)addr == trace->probe.addr) { 309 if (!nommiotrace) 310 unregister_kmmio_probe(&trace->probe); 311 list_del(&trace->list); 312 found_trace = trace; 313 break; 314 } 315 } 316 map.map_id = (found_trace) ? found_trace->id : -1; 317 mmio_trace_mapping(&map); 318 319not_enabled: 320 spin_unlock_irq(&trace_lock); 321 if (found_trace) { 322 synchronize_rcu(); /* unregister_kmmio_probe() requirement */ 323 kfree(found_trace); 324 } 325} 326 327void mmiotrace_iounmap(volatile void __iomem *addr) 328{ 329 might_sleep(); 330 if (is_enabled()) /* recheck and proper locking in *_core() */ 331 iounmap_trace_core(addr); 332} 333 334int mmiotrace_printk(const char *fmt, ...) 335{ 336 int ret = 0; 337 va_list args; 338 unsigned long flags; 339 va_start(args, fmt); 340 341 spin_lock_irqsave(&trace_lock, flags); 342 if (is_enabled()) 343 ret = mmio_trace_printk(fmt, args); 344 spin_unlock_irqrestore(&trace_lock, flags); 345 346 va_end(args); 347 return ret; 348} 349EXPORT_SYMBOL(mmiotrace_printk); 350 351static void clear_trace_list(void) 352{ 353 struct remap_trace *trace; 354 struct remap_trace *tmp; 355 356 /* 357 * No locking required, because the caller ensures we are in a 358 * critical section via mutex, and is_enabled() is false, 359 * i.e. nothing can traverse or modify this list. 360 * Caller also ensures is_enabled() cannot change. 361 */ 362 list_for_each_entry(trace, &trace_list, list) { 363 pr_notice("purging non-iounmapped trace @0x%08lx, size 0x%lx.\n", 364 trace->probe.addr, trace->probe.len); 365 if (!nommiotrace) 366 unregister_kmmio_probe(&trace->probe); 367 } 368 synchronize_rcu(); /* unregister_kmmio_probe() requirement */ 369 370 list_for_each_entry_safe(trace, tmp, &trace_list, list) { 371 list_del(&trace->list); 372 kfree(trace); 373 } 374} 375 376#ifdef CONFIG_HOTPLUG_CPU 377static cpumask_var_t downed_cpus; 378 379static void enter_uniprocessor(void) 380{ 381 int cpu; 382 int err; 383 384 if (downed_cpus == NULL && 385 !alloc_cpumask_var(&downed_cpus, GFP_KERNEL)) { 386 pr_notice("Failed to allocate mask\n"); 387 goto out; 388 } 389 390 get_online_cpus(); 391 cpumask_copy(downed_cpus, cpu_online_mask); 392 cpumask_clear_cpu(cpumask_first(cpu_online_mask), downed_cpus); 393 if (num_online_cpus() > 1) 394 pr_notice("Disabling non-boot CPUs...\n"); 395 put_online_cpus(); 396 397 for_each_cpu(cpu, downed_cpus) { 398 err = cpu_down(cpu); 399 if (!err) 400 pr_info("CPU%d is down.\n", cpu); 401 else 402 pr_err("Error taking CPU%d down: %d\n", cpu, err); 403 } 404out: 405 if (num_online_cpus() > 1) 406 pr_warning("multiple CPUs still online, may miss events.\n"); 407} 408 409/* __ref because leave_uniprocessor calls cpu_up which is __cpuinit, 410 but this whole function is ifdefed CONFIG_HOTPLUG_CPU */ 411static void __ref leave_uniprocessor(void) 412{ 413 int cpu; 414 int err; 415 416 if (downed_cpus == NULL || cpumask_weight(downed_cpus) == 0) 417 return; 418 pr_notice("Re-enabling CPUs...\n"); 419 for_each_cpu(cpu, downed_cpus) { 420 err = cpu_up(cpu); 421 if (!err) 422 pr_info("enabled CPU%d.\n", cpu); 423 else 424 pr_err("cannot re-enable CPU%d: %d\n", cpu, err); 425 } 426} 427 428#else /* !CONFIG_HOTPLUG_CPU */ 429static void enter_uniprocessor(void) 430{ 431 if (num_online_cpus() > 1) 432 pr_warning("multiple CPUs are online, may miss events. " 433 "Suggest booting with maxcpus=1 kernel argument.\n"); 434} 435 436static void leave_uniprocessor(void) 437{ 438} 439#endif 440 441void enable_mmiotrace(void) 442{ 443 mutex_lock(&mmiotrace_mutex); 444 if (is_enabled()) 445 goto out; 446 447 if (nommiotrace) 448 pr_info("MMIO tracing disabled.\n"); 449 kmmio_init(); 450 enter_uniprocessor(); 451 spin_lock_irq(&trace_lock); 452 atomic_inc(&mmiotrace_enabled); 453 spin_unlock_irq(&trace_lock); 454 pr_info("enabled.\n"); 455out: 456 mutex_unlock(&mmiotrace_mutex); 457} 458 459void disable_mmiotrace(void) 460{ 461 mutex_lock(&mmiotrace_mutex); 462 if (!is_enabled()) 463 goto out; 464 465 spin_lock_irq(&trace_lock); 466 atomic_dec(&mmiotrace_enabled); 467 BUG_ON(is_enabled()); 468 spin_unlock_irq(&trace_lock); 469 470 clear_trace_list(); /* guarantees: no more kmmio callbacks */ 471 leave_uniprocessor(); 472 kmmio_cleanup(); 473 pr_info("disabled.\n"); 474out: 475 mutex_unlock(&mmiotrace_mutex); 476} 477