2
0

tcg-plugins.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ================
  6. QEMU TCG Plugins
  7. ================
  8. QEMU TCG plugins provide a way for users to run experiments taking
  9. advantage of the total system control emulation can have over a guest.
  10. It provides a mechanism for plugins to subscribe to events during
  11. translation and execution and optionally callback into the plugin
  12. during these events. TCG plugins are unable to change the system state
  13. only monitor it passively. However they can do this down to an
  14. individual instruction granularity including potentially subscribing
  15. to all load and store operations.
  16. API Stability
  17. =============
  18. This is a new feature for QEMU and it does allow people to develop
  19. out-of-tree plugins that can be dynamically linked into a running QEMU
  20. process. However the project reserves the right to change or break the
  21. API should it need to do so. The best way to avoid this is to submit
  22. your plugin upstream so they can be updated if/when the API changes.
  23. API versioning
  24. --------------
  25. All plugins need to declare a symbol which exports the plugin API
  26. version they were built against. This can be done simply by::
  27. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  28. The core code will refuse to load a plugin that doesn't export a
  29. `qemu_plugin_version` symbol or if plugin version is outside of QEMU's
  30. supported range of API versions.
  31. Additionally the `qemu_info_t` structure which is passed to the
  32. `qemu_plugin_install` method of a plugin will detail the minimum and
  33. current API versions supported by QEMU. The API version will be
  34. incremented if new APIs are added. The minimum API version will be
  35. incremented if existing APIs are changed or removed.
  36. Exposure of QEMU internals
  37. --------------------------
  38. The plugin architecture actively avoids leaking implementation details
  39. about how QEMU's translation works to the plugins. While there are
  40. conceptions such as translation time and translation blocks the
  41. details are opaque to plugins. The plugin is able to query select
  42. details of instructions and system configuration only through the
  43. exported *qemu_plugin* functions.
  44. Query Handle Lifetime
  45. ---------------------
  46. Each callback provides an opaque anonymous information handle which
  47. can usually be further queried to find out information about a
  48. translation, instruction or operation. The handles themselves are only
  49. valid during the lifetime of the callback so it is important that any
  50. information that is needed is extracted during the callback and saved
  51. by the plugin.
  52. Usage
  53. =====
  54. The QEMU binary needs to be compiled for plugin support::
  55. configure --enable-plugins
  56. Once built a program can be run with multiple plugins loaded each with
  57. their own arguments::
  58. $QEMU $OTHER_QEMU_ARGS \
  59. -plugin tests/plugin/libhowvec.so,arg=inline,arg=hint \
  60. -plugin tests/plugin/libhotblocks.so
  61. Arguments are plugin specific and can be used to modify their
  62. behaviour. In this case the howvec plugin is being asked to use inline
  63. ops to count and break down the hint instructions by type.
  64. Plugin Life cycle
  65. =================
  66. First the plugin is loaded and the public qemu_plugin_install function
  67. is called. The plugin will then register callbacks for various plugin
  68. events. Generally plugins will register a handler for the *atexit*
  69. if they want to dump a summary of collected information once the
  70. program/system has finished running.
  71. When a registered event occurs the plugin callback is invoked. The
  72. callbacks may provide additional information. In the case of a
  73. translation event the plugin has an option to enumerate the
  74. instructions in a block of instructions and optionally register
  75. callbacks to some or all instructions when they are executed.
  76. There is also a facility to add an inline event where code to
  77. increment a counter can be directly inlined with the translation.
  78. Currently only a simple increment is supported. This is not atomic so
  79. can miss counts. If you want absolute precision you should use a
  80. callback which can then ensure atomicity itself.
  81. Finally when QEMU exits all the registered *atexit* callbacks are
  82. invoked.
  83. Internals
  84. =========
  85. Locking
  86. -------
  87. We have to ensure we cannot deadlock, particularly under MTTCG. For
  88. this we acquire a lock when called from plugin code. We also keep the
  89. list of callbacks under RCU so that we do not have to hold the lock
  90. when calling the callbacks. This is also for performance, since some
  91. callbacks (e.g. memory access callbacks) might be called very
  92. frequently.
  93. * A consequence of this is that we keep our own list of CPUs, so that
  94. we do not have to worry about locking order wrt cpu_list_lock.
  95. * Use a recursive lock, since we can get registration calls from
  96. callbacks.
  97. As a result registering/unregistering callbacks is "slow", since it
  98. takes a lock. But this is very infrequent; we want performance when
  99. calling (or not calling) callbacks, not when registering them. Using
  100. RCU is great for this.
  101. We support the uninstallation of a plugin at any time (e.g. from
  102. plugin callbacks). This allows plugins to remove themselves if they no
  103. longer want to instrument the code. This operation is asynchronous
  104. which means callbacks may still occur after the uninstall operation is
  105. requested. The plugin isn't completely uninstalled until the safe work
  106. has executed while all vCPUs are quiescent.