mmio-mod.c 14 KB

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