mmio-mod.c 13 KB

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