mmio-mod.c 12 KB

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