mmio-mod.c 12 KB

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