mmio-mod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #include <linux/module.h>
  23. #include <linux/relay.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/proc_fs.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. /* This app's relay channel files will appear in /debug/mmio-trace */
  36. #define APP_DIR "mmio-trace"
  37. /* the marker injection file in /proc */
  38. #define MARKER_FILE "mmio-marker"
  39. #define MODULE_NAME "mmiotrace"
  40. struct trap_reason {
  41. unsigned long addr;
  42. unsigned long ip;
  43. enum reason_type type;
  44. int active_traces;
  45. };
  46. /* Accessed per-cpu. */
  47. static DEFINE_PER_CPU(struct trap_reason, pf_reason);
  48. static DEFINE_PER_CPU(struct mm_io_header_rw, cpu_trace);
  49. /* Access to this is not per-cpu. */
  50. static DEFINE_PER_CPU(atomic_t, dropped);
  51. static struct file_operations mmio_fops = {
  52. .owner = THIS_MODULE,
  53. };
  54. static const size_t subbuf_size = 256*1024;
  55. static struct rchan *chan;
  56. static struct dentry *dir;
  57. static struct proc_dir_entry *proc_marker_file;
  58. /* module parameters */
  59. static unsigned int n_subbufs = 32*4;
  60. static unsigned long filter_offset;
  61. static int nommiotrace;
  62. static int ISA_trace;
  63. static int trace_pc;
  64. module_param(n_subbufs, uint, 0);
  65. module_param(filter_offset, ulong, 0);
  66. module_param(nommiotrace, bool, 0);
  67. module_param(ISA_trace, bool, 0);
  68. module_param(trace_pc, bool, 0);
  69. MODULE_PARM_DESC(n_subbufs, "Number of 256kB buffers, default 128.");
  70. MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
  71. MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
  72. MODULE_PARM_DESC(ISA_trace, "Do not exclude the low ISA range.");
  73. MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
  74. static void record_timestamp(struct mm_io_header *header)
  75. {
  76. struct timespec now;
  77. getnstimeofday(&now);
  78. header->sec = now.tv_sec;
  79. header->nsec = now.tv_nsec;
  80. }
  81. /*
  82. * Write callback for the /proc entry:
  83. * Read a marker and write it to the mmio trace log
  84. */
  85. static int write_marker(struct file *file, const char __user *buffer,
  86. unsigned long count, void *data)
  87. {
  88. char *event = NULL;
  89. struct mm_io_header *headp;
  90. int len = (count > 65535) ? 65535 : count;
  91. event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
  92. if (!event)
  93. return -ENOMEM;
  94. headp = (struct mm_io_header *)event;
  95. headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
  96. headp->data_len = len;
  97. record_timestamp(headp);
  98. if (copy_from_user(event + sizeof(*headp), buffer, len)) {
  99. kfree(event);
  100. return -EFAULT;
  101. }
  102. relay_write(chan, event, sizeof(*headp) + len);
  103. kfree(event);
  104. return len;
  105. }
  106. static void print_pte(unsigned long address)
  107. {
  108. int level;
  109. pte_t *pte = lookup_address(address, &level);
  110. if (!pte) {
  111. pr_err(MODULE_NAME ": Error in %s: no pte for page 0x%08lx\n",
  112. __func__, address);
  113. return;
  114. }
  115. if (level == PG_LEVEL_2M) {
  116. pr_emerg(MODULE_NAME ": 4MB pages are not currently "
  117. "supported: %lx\n", address);
  118. BUG();
  119. }
  120. pr_info(MODULE_NAME ": pte for 0x%lx: 0x%lx 0x%lx\n",
  121. address, pte_val(*pte),
  122. pte_val(*pte) & _PAGE_PRESENT);
  123. }
  124. /*
  125. * For some reason the pre/post pairs have been called in an
  126. * unmatched order. Report and die.
  127. */
  128. static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
  129. {
  130. const struct trap_reason *my_reason = &get_cpu_var(pf_reason);
  131. pr_emerg(MODULE_NAME ": unexpected fault for address: %lx, "
  132. "last fault for address: %lx\n",
  133. addr, my_reason->addr);
  134. print_pte(addr);
  135. #ifdef __i386__
  136. print_symbol(KERN_EMERG "faulting EIP is at %s\n", regs->ip);
  137. print_symbol(KERN_EMERG "last faulting EIP was at %s\n",
  138. my_reason->ip);
  139. pr_emerg("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
  140. regs->ax, regs->bx, regs->cx, regs->dx);
  141. pr_emerg("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
  142. regs->si, regs->di, regs->bp, regs->sp);
  143. #else
  144. print_symbol(KERN_EMERG "faulting RIP is at %s\n", regs->ip);
  145. print_symbol(KERN_EMERG "last faulting RIP was at %s\n",
  146. my_reason->ip);
  147. pr_emerg("rax: %016lx rcx: %016lx rdx: %016lx\n",
  148. regs->ax, regs->cx, regs->dx);
  149. pr_emerg("rsi: %016lx rdi: %016lx rbp: %016lx rsp: %016lx\n",
  150. regs->si, regs->di, regs->bp, regs->sp);
  151. #endif
  152. put_cpu_var(pf_reason);
  153. BUG();
  154. }
  155. static void pre(struct kmmio_probe *p, struct pt_regs *regs,
  156. unsigned long addr)
  157. {
  158. struct trap_reason *my_reason = &get_cpu_var(pf_reason);
  159. struct mm_io_header_rw *my_trace = &get_cpu_var(cpu_trace);
  160. const unsigned long instptr = instruction_pointer(regs);
  161. const enum reason_type type = get_ins_type(instptr);
  162. /* it doesn't make sense to have more than one active trace per cpu */
  163. if (my_reason->active_traces)
  164. die_kmmio_nesting_error(regs, addr);
  165. else
  166. my_reason->active_traces++;
  167. my_reason->type = type;
  168. my_reason->addr = addr;
  169. my_reason->ip = instptr;
  170. my_trace->header.type = MMIO_MAGIC;
  171. my_trace->header.pid = 0;
  172. my_trace->header.data_len = sizeof(struct mm_io_rw);
  173. my_trace->rw.address = addr;
  174. /*
  175. * Only record the program counter when requested.
  176. * It may taint clean-room reverse engineering.
  177. */
  178. if (trace_pc)
  179. my_trace->rw.pc = instptr;
  180. else
  181. my_trace->rw.pc = 0;
  182. record_timestamp(&my_trace->header);
  183. switch (type) {
  184. case REG_READ:
  185. my_trace->header.type |=
  186. (MMIO_READ << MMIO_OPCODE_SHIFT) |
  187. (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
  188. break;
  189. case REG_WRITE:
  190. my_trace->header.type |=
  191. (MMIO_WRITE << MMIO_OPCODE_SHIFT) |
  192. (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
  193. my_trace->rw.value = get_ins_reg_val(instptr, regs);
  194. break;
  195. case IMM_WRITE:
  196. my_trace->header.type |=
  197. (MMIO_WRITE << MMIO_OPCODE_SHIFT) |
  198. (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
  199. my_trace->rw.value = get_ins_imm_val(instptr);
  200. break;
  201. default:
  202. {
  203. unsigned char *ip = (unsigned char *)instptr;
  204. my_trace->header.type |=
  205. (MMIO_UNKNOWN_OP << MMIO_OPCODE_SHIFT);
  206. my_trace->rw.value = (*ip) << 16 | *(ip + 1) << 8 |
  207. *(ip + 2);
  208. }
  209. }
  210. put_cpu_var(cpu_trace);
  211. put_cpu_var(pf_reason);
  212. }
  213. static void post(struct kmmio_probe *p, unsigned long condition,
  214. struct pt_regs *regs)
  215. {
  216. struct trap_reason *my_reason = &get_cpu_var(pf_reason);
  217. struct mm_io_header_rw *my_trace = &get_cpu_var(cpu_trace);
  218. /*
  219. * XXX: This might not get called, if the probe is removed while
  220. * trace hit is on flight.
  221. */
  222. /* this should always return the active_trace count to 0 */
  223. my_reason->active_traces--;
  224. if (my_reason->active_traces) {
  225. pr_emerg(MODULE_NAME ": unexpected post handler");
  226. BUG();
  227. }
  228. switch (my_reason->type) {
  229. case REG_READ:
  230. my_trace->rw.value = get_ins_reg_val(my_reason->ip, regs);
  231. break;
  232. default:
  233. break;
  234. }
  235. relay_write(chan, my_trace, sizeof(*my_trace));
  236. put_cpu_var(cpu_trace);
  237. put_cpu_var(pf_reason);
  238. }
  239. /*
  240. * subbuf_start() relay callback.
  241. *
  242. * Defined so that we know when events are dropped due to the buffer-full
  243. * condition.
  244. */
  245. static int subbuf_start_handler(struct rchan_buf *buf, void *subbuf,
  246. void *prev_subbuf, size_t prev_padding)
  247. {
  248. unsigned int cpu = buf->cpu;
  249. atomic_t *drop = &per_cpu(dropped, cpu);
  250. int count;
  251. if (relay_buf_full(buf)) {
  252. if (atomic_inc_return(drop) == 1)
  253. pr_err(MODULE_NAME ": cpu %d buffer full!\n", cpu);
  254. return 0;
  255. }
  256. count = atomic_read(drop);
  257. if (count) {
  258. pr_err(MODULE_NAME ": cpu %d buffer no longer full, "
  259. "missed %d events.\n",
  260. cpu, count);
  261. atomic_sub(count, drop);
  262. }
  263. return 1;
  264. }
  265. /* file_create() callback. Creates relay file in debugfs. */
  266. static struct dentry *create_buf_file_handler(const char *filename,
  267. struct dentry *parent,
  268. int mode,
  269. struct rchan_buf *buf,
  270. int *is_global)
  271. {
  272. struct dentry *buf_file;
  273. mmio_fops.read = relay_file_operations.read;
  274. mmio_fops.open = relay_file_operations.open;
  275. mmio_fops.poll = relay_file_operations.poll;
  276. mmio_fops.mmap = relay_file_operations.mmap;
  277. mmio_fops.release = relay_file_operations.release;
  278. mmio_fops.splice_read = relay_file_operations.splice_read;
  279. buf_file = debugfs_create_file(filename, mode, parent, buf,
  280. &mmio_fops);
  281. return buf_file;
  282. }
  283. /* file_remove() default callback. Removes relay file in debugfs. */
  284. static int remove_buf_file_handler(struct dentry *dentry)
  285. {
  286. debugfs_remove(dentry);
  287. return 0;
  288. }
  289. static struct rchan_callbacks relay_callbacks = {
  290. .subbuf_start = subbuf_start_handler,
  291. .create_buf_file = create_buf_file_handler,
  292. .remove_buf_file = remove_buf_file_handler,
  293. };
  294. /*
  295. * create_channel - creates channel /debug/APP_DIR/cpuXXX
  296. * Returns channel on success, NULL otherwise
  297. */
  298. static struct rchan *create_channel(unsigned size, unsigned n)
  299. {
  300. return relay_open("cpu", dir, size, n, &relay_callbacks, NULL);
  301. }
  302. /* destroy_channel - destroys channel /debug/APP_DIR/cpuXXX */
  303. static void destroy_channel(void)
  304. {
  305. if (chan) {
  306. relay_close(chan);
  307. chan = NULL;
  308. }
  309. }
  310. struct remap_trace {
  311. struct list_head list;
  312. struct kmmio_probe probe;
  313. };
  314. static LIST_HEAD(trace_list);
  315. static DEFINE_SPINLOCK(trace_list_lock);
  316. static void do_ioremap_trace_core(unsigned long offset, unsigned long size,
  317. void __iomem *addr)
  318. {
  319. struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
  320. struct mm_io_header_map event = {
  321. .header = {
  322. .type = MMIO_MAGIC |
  323. (MMIO_PROBE << MMIO_OPCODE_SHIFT),
  324. .sec = 0,
  325. .nsec = 0,
  326. .pid = 0,
  327. .data_len = sizeof(struct mm_io_map)
  328. },
  329. .map = {
  330. .phys = offset,
  331. .addr = (unsigned long)addr,
  332. .len = size,
  333. .pc = 0
  334. }
  335. };
  336. record_timestamp(&event.header);
  337. *trace = (struct remap_trace) {
  338. .probe = {
  339. .addr = (unsigned long)addr,
  340. .len = size,
  341. .pre_handler = pre,
  342. .post_handler = post,
  343. }
  344. };
  345. relay_write(chan, &event, sizeof(event));
  346. spin_lock(&trace_list_lock);
  347. list_add_tail(&trace->list, &trace_list);
  348. spin_unlock(&trace_list_lock);
  349. if (!nommiotrace)
  350. register_kmmio_probe(&trace->probe);
  351. }
  352. static void ioremap_trace_core(unsigned long offset, unsigned long size,
  353. void __iomem *addr)
  354. {
  355. if ((filter_offset) && (offset != filter_offset))
  356. return;
  357. /* Don't trace the low PCI/ISA area, it's always mapped.. */
  358. if (!ISA_trace && (offset < ISA_END_ADDRESS) &&
  359. (offset + size > ISA_START_ADDRESS)) {
  360. pr_notice(MODULE_NAME ": Ignoring map of low PCI/ISA area "
  361. "(0x%lx-0x%lx)\n",
  362. offset, offset + size);
  363. return;
  364. }
  365. do_ioremap_trace_core(offset, size, addr);
  366. }
  367. void __iomem *ioremap_cache_trace(unsigned long offset, unsigned long size)
  368. {
  369. void __iomem *p = ioremap_cache(offset, size);
  370. pr_debug(MODULE_NAME ": ioremap_cache(0x%lx, 0x%lx) = %p\n",
  371. offset, size, p);
  372. ioremap_trace_core(offset, size, p);
  373. return p;
  374. }
  375. EXPORT_SYMBOL(ioremap_cache_trace);
  376. void __iomem *ioremap_nocache_trace(unsigned long offset, unsigned long size)
  377. {
  378. void __iomem *p = ioremap_nocache(offset, size);
  379. pr_debug(MODULE_NAME ": ioremap_nocache(0x%lx, 0x%lx) = %p\n",
  380. offset, size, p);
  381. ioremap_trace_core(offset, size, p);
  382. return p;
  383. }
  384. EXPORT_SYMBOL(ioremap_nocache_trace);
  385. void iounmap_trace(volatile void __iomem *addr)
  386. {
  387. struct mm_io_header_map event = {
  388. .header = {
  389. .type = MMIO_MAGIC |
  390. (MMIO_UNPROBE << MMIO_OPCODE_SHIFT),
  391. .sec = 0,
  392. .nsec = 0,
  393. .pid = 0,
  394. .data_len = sizeof(struct mm_io_map)
  395. },
  396. .map = {
  397. .phys = 0,
  398. .addr = (unsigned long)addr,
  399. .len = 0,
  400. .pc = 0
  401. }
  402. };
  403. struct remap_trace *trace;
  404. struct remap_trace *tmp;
  405. pr_debug(MODULE_NAME ": Unmapping %p.\n", addr);
  406. record_timestamp(&event.header);
  407. spin_lock(&trace_list_lock);
  408. list_for_each_entry_safe(trace, tmp, &trace_list, list) {
  409. if ((unsigned long)addr == trace->probe.addr) {
  410. if (!nommiotrace)
  411. unregister_kmmio_probe(&trace->probe);
  412. list_del(&trace->list);
  413. kfree(trace);
  414. break;
  415. }
  416. }
  417. spin_unlock(&trace_list_lock);
  418. relay_write(chan, &event, sizeof(event));
  419. iounmap(addr);
  420. }
  421. EXPORT_SYMBOL(iounmap_trace);
  422. static void clear_trace_list(void)
  423. {
  424. struct remap_trace *trace;
  425. struct remap_trace *tmp;
  426. spin_lock(&trace_list_lock);
  427. list_for_each_entry_safe(trace, tmp, &trace_list, list) {
  428. pr_warning(MODULE_NAME ": purging non-iounmapped "
  429. "trace @0x%08lx, size 0x%lx.\n",
  430. trace->probe.addr, trace->probe.len);
  431. if (!nommiotrace)
  432. unregister_kmmio_probe(&trace->probe);
  433. list_del(&trace->list);
  434. kfree(trace);
  435. break;
  436. }
  437. spin_unlock(&trace_list_lock);
  438. }
  439. static int __init init(void)
  440. {
  441. if (n_subbufs < 2)
  442. return -EINVAL;
  443. dir = debugfs_create_dir(APP_DIR, NULL);
  444. if (!dir) {
  445. pr_err(MODULE_NAME ": Couldn't create relay app directory.\n");
  446. return -ENOMEM;
  447. }
  448. chan = create_channel(subbuf_size, n_subbufs);
  449. if (!chan) {
  450. debugfs_remove(dir);
  451. pr_err(MODULE_NAME ": relay app channel creation failed\n");
  452. return -ENOMEM;
  453. }
  454. reference_kmmio();
  455. proc_marker_file = create_proc_entry(MARKER_FILE, 0, NULL);
  456. if (proc_marker_file)
  457. proc_marker_file->write_proc = write_marker;
  458. pr_debug(MODULE_NAME ": loaded.\n");
  459. if (nommiotrace)
  460. pr_info(MODULE_NAME ": MMIO tracing disabled.\n");
  461. if (ISA_trace)
  462. pr_warning(MODULE_NAME ": Warning! low ISA range will be "
  463. "traced.\n");
  464. return 0;
  465. }
  466. static void __exit cleanup(void)
  467. {
  468. pr_debug(MODULE_NAME ": unload...\n");
  469. clear_trace_list();
  470. unreference_kmmio();
  471. remove_proc_entry(MARKER_FILE, NULL);
  472. destroy_channel();
  473. if (dir)
  474. debugfs_remove(dir);
  475. }
  476. module_init(init);
  477. module_exit(cleanup);
  478. MODULE_LICENSE("GPL");