mmio-mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 <linux/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. 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. #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. unsigned 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->private;
  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(resource_size_t 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. /* These are page-unaligned. */
  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. .private = 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 mmiotrace_ioremap(resource_size_t offset, unsigned long size,
  282. void __iomem *addr)
  283. {
  284. if (!is_enabled()) /* recheck and proper locking in *_core() */
  285. return;
  286. pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n",
  287. (unsigned long long)offset, size, addr);
  288. if ((filter_offset) && (offset != filter_offset))
  289. return;
  290. ioremap_trace_core(offset, size, addr);
  291. }
  292. static void iounmap_trace_core(volatile void __iomem *addr)
  293. {
  294. struct mmiotrace_map map = {
  295. .phys = 0,
  296. .virt = (unsigned long)addr,
  297. .len = 0,
  298. .opcode = MMIO_UNPROBE
  299. };
  300. struct remap_trace *trace;
  301. struct remap_trace *tmp;
  302. struct remap_trace *found_trace = NULL;
  303. pr_debug(NAME "Unmapping %p.\n", addr);
  304. spin_lock_irq(&trace_lock);
  305. if (!is_enabled())
  306. goto not_enabled;
  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. not_enabled:
  319. spin_unlock_irq(&trace_lock);
  320. if (found_trace) {
  321. synchronize_rcu(); /* unregister_kmmio_probe() requirement */
  322. kfree(found_trace);
  323. }
  324. }
  325. void mmiotrace_iounmap(volatile void __iomem *addr)
  326. {
  327. might_sleep();
  328. if (is_enabled()) /* recheck and proper locking in *_core() */
  329. iounmap_trace_core(addr);
  330. }
  331. static void clear_trace_list(void)
  332. {
  333. struct remap_trace *trace;
  334. struct remap_trace *tmp;
  335. /*
  336. * No locking required, because the caller ensures we are in a
  337. * critical section via mutex, and is_enabled() is false,
  338. * i.e. nothing can traverse or modify this list.
  339. * Caller also ensures is_enabled() cannot change.
  340. */
  341. list_for_each_entry(trace, &trace_list, list) {
  342. pr_notice(NAME "purging non-iounmapped "
  343. "trace @0x%08lx, size 0x%lx.\n",
  344. trace->probe.addr, trace->probe.len);
  345. if (!nommiotrace)
  346. unregister_kmmio_probe(&trace->probe);
  347. }
  348. synchronize_rcu(); /* unregister_kmmio_probe() requirement */
  349. list_for_each_entry_safe(trace, tmp, &trace_list, list) {
  350. list_del(&trace->list);
  351. kfree(trace);
  352. }
  353. }
  354. #ifdef CONFIG_HOTPLUG_CPU
  355. static cpumask_t downed_cpus;
  356. static void enter_uniprocessor(void)
  357. {
  358. int cpu;
  359. int err;
  360. get_online_cpus();
  361. downed_cpus = cpu_online_map;
  362. cpu_clear(first_cpu(cpu_online_map), downed_cpus);
  363. if (num_online_cpus() > 1)
  364. pr_notice(NAME "Disabling non-boot CPUs...\n");
  365. put_online_cpus();
  366. for_each_cpu_mask(cpu, downed_cpus) {
  367. err = cpu_down(cpu);
  368. if (!err)
  369. pr_info(NAME "CPU%d is down.\n", cpu);
  370. else
  371. pr_err(NAME "Error taking CPU%d down: %d\n", cpu, err);
  372. }
  373. if (num_online_cpus() > 1)
  374. pr_warning(NAME "multiple CPUs still online, "
  375. "may miss events.\n");
  376. }
  377. /* __ref because leave_uniprocessor calls cpu_up which is __cpuinit,
  378. but this whole function is ifdefed CONFIG_HOTPLUG_CPU */
  379. static void __ref leave_uniprocessor(void)
  380. {
  381. int cpu;
  382. int err;
  383. if (cpus_weight(downed_cpus) == 0)
  384. return;
  385. pr_notice(NAME "Re-enabling CPUs...\n");
  386. for_each_cpu_mask(cpu, downed_cpus) {
  387. err = cpu_up(cpu);
  388. if (!err)
  389. pr_info(NAME "enabled CPU%d.\n", cpu);
  390. else
  391. pr_err(NAME "cannot re-enable CPU%d: %d\n", cpu, err);
  392. }
  393. }
  394. #else /* !CONFIG_HOTPLUG_CPU */
  395. static void enter_uniprocessor(void)
  396. {
  397. if (num_online_cpus() > 1)
  398. pr_warning(NAME "multiple CPUs are online, may miss events. "
  399. "Suggest booting with maxcpus=1 kernel argument.\n");
  400. }
  401. static void leave_uniprocessor(void)
  402. {
  403. }
  404. #endif
  405. #if 0 /* XXX: out of order */
  406. static struct file_operations fops_marker = {
  407. .owner = THIS_MODULE,
  408. .write = write_marker
  409. };
  410. #endif
  411. void enable_mmiotrace(void)
  412. {
  413. mutex_lock(&mmiotrace_mutex);
  414. if (is_enabled())
  415. goto out;
  416. #if 0 /* XXX: tracing does not support text entries */
  417. marker_file = debugfs_create_file("marker", 0660, dir, NULL,
  418. &fops_marker);
  419. if (!marker_file)
  420. pr_err(NAME "marker file creation failed.\n");
  421. #endif
  422. if (nommiotrace)
  423. pr_info(NAME "MMIO tracing disabled.\n");
  424. enter_uniprocessor();
  425. spin_lock_irq(&trace_lock);
  426. atomic_inc(&mmiotrace_enabled);
  427. spin_unlock_irq(&trace_lock);
  428. pr_info(NAME "enabled.\n");
  429. out:
  430. mutex_unlock(&mmiotrace_mutex);
  431. }
  432. void disable_mmiotrace(void)
  433. {
  434. mutex_lock(&mmiotrace_mutex);
  435. if (!is_enabled())
  436. goto out;
  437. spin_lock_irq(&trace_lock);
  438. atomic_dec(&mmiotrace_enabled);
  439. BUG_ON(is_enabled());
  440. spin_unlock_irq(&trace_lock);
  441. clear_trace_list(); /* guarantees: no more kmmio callbacks */
  442. leave_uniprocessor();
  443. if (marker_file) {
  444. debugfs_remove(marker_file);
  445. marker_file = NULL;
  446. }
  447. pr_info(NAME "disabled.\n");
  448. out:
  449. mutex_unlock(&mmiotrace_mutex);
  450. }