mmiotrace.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. In-kernel memory-mapped I/O tracing
  2. Home page and links to optional user space tools:
  3. http://nouveau.freedesktop.org/wiki/MmioTrace
  4. MMIO tracing was originally developed by Intel around 2003 for their Fault
  5. Injection Test Harness. In Dec 2006 - Jan 2007, using the code from Intel,
  6. Jeff Muizelaar created a tool for tracing MMIO accesses with the Nouveau
  7. project in mind. Since then many people have contributed.
  8. Mmiotrace was built for reverse engineering any memory-mapped IO device with
  9. the Nouveau project as the first real user. Only x86 and x86_64 architectures
  10. are supported.
  11. Out-of-tree mmiotrace was originally modified for mainline inclusion and
  12. ftrace framework by Pekka Paalanen <pq@iki.fi>.
  13. Preparation
  14. -----------
  15. Mmiotrace feature is compiled in by the CONFIG_MMIOTRACE option. Tracing is
  16. disabled by default, so it is safe to have this set to yes. SMP systems are
  17. supported, but tracing is unreliable and may miss events if more than one CPU
  18. is on-line, therefore mmiotrace takes all but one CPU off-line during run-time
  19. activation [not implemented].
  20. Usage Quick Reference
  21. ---------------------
  22. $ mount -t debugfs debugfs /debug
  23. $ echo mmiotrace > /debug/tracing/current_tracer
  24. $ cat /debug/tracing/trace_pipe > mydump.txt &
  25. Start X or whatever.
  26. $ echo "X is up" > /debug/tracing/marker
  27. $ echo none > /debug/tracing/current_tracer
  28. Check kernel log.
  29. Usage
  30. -----
  31. Make sure debugfs is mounted to /debug. If not, (requires root privileges)
  32. $ mount -t debugfs debugfs /debug
  33. Check that the driver you are about to trace is not loaded.
  34. Activate mmiotrace (requires root privileges):
  35. $ echo mmiotrace > /debug/tracing/current_tracer
  36. Start storing the trace:
  37. $ cat /debug/tracing/trace_pipe > mydump.txt &
  38. The 'cat' process should stay running (sleeping) in the background.
  39. Load the driver you want to trace and use it. Mmiotrace will only catch MMIO
  40. accesses to areas that are ioremapped while mmiotrace is active.
  41. [Unimplemented feature:]
  42. During tracing you can place comments (markers) into the trace by
  43. $ echo "X is up" > /debug/tracing/marker
  44. This makes it easier to see which part of the (huge) trace corresponds to
  45. which action. It is recommended to place descriptive markers about what you
  46. do.
  47. Shut down mmiotrace (requires root privileges):
  48. $ echo none > /debug/tracing/current_tracer
  49. The 'cat' process exits. If it does not, kill it by 'fg' and pressing ctrl+c.
  50. [This feature is not implemented yet!]
  51. Check your kernel log. If there are messages about mmiotrace losing events,
  52. this is due to buffer overrun, and the trace is incomplete. You should enlarge
  53. the buffers and try again. [How?]
  54. If you are doing a trace for a driver project, e.g. Nouveau, you should also
  55. do the following before sending your results:
  56. $ lspci -vvv > lspci.txt
  57. $ dmesg > dmesg.txt
  58. $ tar zcf pciid-nick-mmiotrace.tar.gz mydump.txt lspci.txt dmesg.txt
  59. and then send the .tar.gz file. The trace compresses considerably. Replace
  60. "pciid" and "nick" with the PCI ID or model name of your piece of hardware
  61. under investigation and your nick name.
  62. How Mmiotrace Works
  63. -------------------
  64. Access to hardware IO-memory is gained by mapping addresses from PCI bus by
  65. calling one of the ioremap_*() functions. Mmiotrace is hooked into the
  66. __ioremap() function and gets called whenever a mapping is created. Mapping is
  67. an event that is recorded into the trace log. Note, that ISA range mappings
  68. are not caught, since the mapping always exists and is returned directly.
  69. MMIO accesses are recorded via page faults. Just before __ioremap() returns,
  70. the mapped pages are marked as not present. Any access to the pages causes a
  71. fault. The page fault handler calls mmiotrace to handle the fault. Mmiotrace
  72. marks the page present, sets TF flag to achieve single stepping and exits the
  73. fault handler. The instruction that faulted is executed and debug trap is
  74. entered. Here mmiotrace again marks the page as not present. The instruction
  75. is decoded to get the type of operation (read/write), data width and the value
  76. read or written. These are stored to the trace log.
  77. Setting the page present in the page fault handler has a race condition on SMP
  78. machines. During the single stepping other CPUs may run freely on that page
  79. and events can be missed without a notice. Re-enabling other CPUs during
  80. tracing is discouraged.
  81. Trace Log Format
  82. ----------------
  83. The raw log is text and easily filtered with e.g. grep and awk. One record is
  84. one line in the log. A record starts with a keyword, followed by keyword
  85. dependant arguments. Arguments are separated by a space, or continue until the
  86. end of line. The format for version 20070824 is as follows:
  87. Explanation Keyword Space separated arguments
  88. ---------------------------------------------------------------------------
  89. read event R width, timestamp, map id, physical, value, PC, PID
  90. write event W width, timestamp, map id, physical, value, PC, PID
  91. ioremap event MAP timestamp, map id, physical, virtual, length, PC, PID
  92. iounmap event UNMAP timestamp, map id, PC, PID
  93. marker MARK timestamp, text
  94. version VERSION the string "20070824"
  95. info for reader LSPCI one line from lspci -v
  96. PCI address map PCIDEV space separated /proc/bus/pci/devices data
  97. unk. opcode UNKNOWN timestamp, map id, physical, data, PC, PID
  98. Timestamp is in seconds with decimals. Physical is a PCI bus address, virtual
  99. is a kernel virtual address. Width is the data width in bytes and value is the
  100. data value. Map id is an arbitrary id number identifying the mapping that was
  101. used in an operation. PC is the program counter and PID is process id. PC is
  102. zero if it is not recorded. PID is always zero as tracing MMIO accesses
  103. originating in user space memory is not yet supported.
  104. For instance, the following awk filter will pass all 32-bit writes that target
  105. physical addresses in the range [0xfb73ce40, 0xfb800000[
  106. $ awk '/W 4 / { adr=strtonum($5); if (adr >= 0xfb73ce40 &&
  107. adr < 0xfb800000) print; }'
  108. Tools for Developers
  109. --------------------
  110. The user space tools include utilities for:
  111. - replacing numeric addresses and values with hardware register names
  112. - replaying MMIO logs, i.e., re-executing the recorded writes