mmio-mod.c 12 KB

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