tcg-plugins.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ..
  2. Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  3. Copyright (c) 2019, Linaro Limited
  4. Written by Emilio Cota and Alex Bennée
  5. .. _TCG Plugins:
  6. QEMU TCG Plugins
  7. ================
  8. Writing plugins
  9. ---------------
  10. API versioning
  11. ~~~~~~~~~~~~~~
  12. This is a new feature for QEMU and it does allow people to develop
  13. out-of-tree plugins that can be dynamically linked into a running QEMU
  14. process. However the project reserves the right to change or break the
  15. API should it need to do so. The best way to avoid this is to submit
  16. your plugin upstream so they can be updated if/when the API changes.
  17. All plugins need to declare a symbol which exports the plugin API
  18. version they were built against. This can be done simply by::
  19. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  20. The core code will refuse to load a plugin that doesn't export a
  21. ``qemu_plugin_version`` symbol or if plugin version is outside of QEMU's
  22. supported range of API versions.
  23. Additionally the ``qemu_info_t`` structure which is passed to the
  24. ``qemu_plugin_install`` method of a plugin will detail the minimum and
  25. current API versions supported by QEMU. The API version will be
  26. incremented if new APIs are added. The minimum API version will be
  27. incremented if existing APIs are changed or removed.
  28. Lifetime of the query handle
  29. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. Each callback provides an opaque anonymous information handle which
  31. can usually be further queried to find out information about a
  32. translation, instruction or operation. The handles themselves are only
  33. valid during the lifetime of the callback so it is important that any
  34. information that is needed is extracted during the callback and saved
  35. by the plugin.
  36. Plugin life cycle
  37. ~~~~~~~~~~~~~~~~~
  38. First the plugin is loaded and the public qemu_plugin_install function
  39. is called. The plugin will then register callbacks for various plugin
  40. events. Generally plugins will register a handler for the *atexit*
  41. if they want to dump a summary of collected information once the
  42. program/system has finished running.
  43. When a registered event occurs the plugin callback is invoked. The
  44. callbacks may provide additional information. In the case of a
  45. translation event the plugin has an option to enumerate the
  46. instructions in a block of instructions and optionally register
  47. callbacks to some or all instructions when they are executed.
  48. There is also a facility to add inline instructions doing various operations,
  49. like adding or storing an immediate value. It is also possible to execute a
  50. callback conditionally, with condition being evaluated inline. All those inline
  51. operations are associated to a ``scoreboard``, which is a thread-local storage
  52. automatically expanded when new cores/threads are created and that can be
  53. accessed/modified in a thread-safe way without any lock needed. Combining inline
  54. operations and conditional callbacks offer a more efficient way to instrument
  55. binaries, compared to classic callbacks.
  56. Finally when QEMU exits all the registered *atexit* callbacks are
  57. invoked.
  58. Exposure of QEMU internals
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. The plugin architecture actively avoids leaking implementation details
  61. about how QEMU's translation works to the plugins. While there are
  62. conceptions such as translation time and translation blocks the
  63. details are opaque to plugins. The plugin is able to query select
  64. details of instructions and system configuration only through the
  65. exported *qemu_plugin* functions.
  66. However the following assumptions can be made:
  67. Translation Blocks
  68. ++++++++++++++++++
  69. All code will go through a translation phase although not all
  70. translations will be necessarily be executed. You need to instrument
  71. actual executions to track what is happening.
  72. It is quite normal to see the same address translated multiple times.
  73. If you want to track the code in system emulation you should examine
  74. the underlying physical address (``qemu_plugin_insn_haddr``) to take
  75. into account the effects of virtual memory although if the system does
  76. paging this will change too.
  77. Not all instructions in a block will always execute so if its
  78. important to track individual instruction execution you need to
  79. instrument them directly. However asynchronous interrupts will not
  80. change control flow mid-block.
  81. Instructions
  82. ++++++++++++
  83. Instruction instrumentation runs before the instruction executes. You
  84. can be can be sure the instruction will be dispatched, but you can't
  85. be sure it will complete. Generally this will be because of a
  86. synchronous exception (e.g. SIGILL) triggered by the instruction
  87. attempting to execute. If you want to be sure you will need to
  88. instrument the next instruction as well. See the ``execlog.c`` plugin
  89. for examples of how to track this and finalise details after execution.
  90. Memory Accesses
  91. +++++++++++++++
  92. Memory callbacks are called after a successful load or store.
  93. Unsuccessful operations (i.e. faults) will not be visible to memory
  94. instrumentation although the execution side effects can be observed
  95. (e.g. entering a exception handler).
  96. System Idle and Resume States
  97. +++++++++++++++++++++++++++++
  98. The ``qemu_plugin_register_vcpu_idle_cb`` and
  99. ``qemu_plugin_register_vcpu_resume_cb`` functions can be used to track
  100. when CPUs go into and return from sleep states when waiting for
  101. external I/O. Be aware though that these may occur less frequently
  102. than in real HW due to the inefficiencies of emulation giving less
  103. chance for the CPU to idle.
  104. Internals
  105. ---------
  106. Locking
  107. ~~~~~~~
  108. We have to ensure we cannot deadlock, particularly under MTTCG. For
  109. this we acquire a lock when called from plugin code. We also keep the
  110. list of callbacks under RCU so that we do not have to hold the lock
  111. when calling the callbacks. This is also for performance, since some
  112. callbacks (e.g. memory access callbacks) might be called very
  113. frequently.
  114. * A consequence of this is that we keep our own list of CPUs, so that
  115. we do not have to worry about locking order wrt cpu_list_lock.
  116. * Use a recursive lock, since we can get registration calls from
  117. callbacks.
  118. As a result registering/unregistering callbacks is "slow", since it
  119. takes a lock. But this is very infrequent; we want performance when
  120. calling (or not calling) callbacks, not when registering them. Using
  121. RCU is great for this.
  122. We support the uninstallation of a plugin at any time (e.g. from
  123. plugin callbacks). This allows plugins to remove themselves if they no
  124. longer want to instrument the code. This operation is asynchronous
  125. which means callbacks may still occur after the uninstall operation is
  126. requested. The plugin isn't completely uninstalled until the safe work
  127. has executed while all vCPUs are quiescent.
  128. Plugin API
  129. ==========
  130. The following API is generated from the inline documentation in
  131. ``include/qemu/qemu-plugin.h``. Please ensure any updates to the API
  132. include the full kernel-doc annotations.
  133. .. kernel-doc:: include/qemu/qemu-plugin.h