mmio-mod.c 14 KB

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