xref: /qemu/docs/devel/tcg-plugins.rst (revision 3f9f9a37ae2404c54458acadb516f0eda7cd2c15)
1027e3332SAlex Bennée..
2027e3332SAlex Bennée   Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
3027e3332SAlex Bennée   Copyright (c) 2019, Linaro Limited
4027e3332SAlex Bennée   Written by Emilio Cota and Alex Bennée
5027e3332SAlex Bennée
6a0a6754bSAlex Bennée.. _TCG Plugins:
7a0a6754bSAlex Bennée
8027e3332SAlex BennéeQEMU TCG Plugins
9027e3332SAlex Bennée================
10027e3332SAlex Bennée
110f37cf2fSChristoph Muellner
12e9adb4acSPaolo BonziniWriting plugins
13e9adb4acSPaolo Bonzini---------------
14e9adb4acSPaolo Bonzini
15e9adb4acSPaolo BonziniAPI versioning
16e9adb4acSPaolo Bonzini~~~~~~~~~~~~~~
17e9adb4acSPaolo Bonzini
18e9adb4acSPaolo BonziniThis is a new feature for QEMU and it does allow people to develop
19e9adb4acSPaolo Bonziniout-of-tree plugins that can be dynamically linked into a running QEMU
20e9adb4acSPaolo Bonziniprocess. However the project reserves the right to change or break the
21e9adb4acSPaolo BonziniAPI should it need to do so. The best way to avoid this is to submit
22e9adb4acSPaolo Bonziniyour plugin upstream so they can be updated if/when the API changes.
23e9adb4acSPaolo Bonzini
24e9adb4acSPaolo BonziniAll plugins need to declare a symbol which exports the plugin API
25e9adb4acSPaolo Bonziniversion they were built against. This can be done simply by::
26e9adb4acSPaolo Bonzini
27e9adb4acSPaolo Bonzini  QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
28e9adb4acSPaolo Bonzini
29e9adb4acSPaolo BonziniThe core code will refuse to load a plugin that doesn't export a
30e9adb4acSPaolo Bonzini``qemu_plugin_version`` symbol or if plugin version is outside of QEMU's
31e9adb4acSPaolo Bonzinisupported range of API versions.
32e9adb4acSPaolo Bonzini
33e9adb4acSPaolo BonziniAdditionally the ``qemu_info_t`` structure which is passed to the
34e9adb4acSPaolo Bonzini``qemu_plugin_install`` method of a plugin will detail the minimum and
35e9adb4acSPaolo Bonzinicurrent API versions supported by QEMU. The API version will be
36e9adb4acSPaolo Bonziniincremented if new APIs are added. The minimum API version will be
37e9adb4acSPaolo Bonziniincremented if existing APIs are changed or removed.
38e9adb4acSPaolo Bonzini
39e9adb4acSPaolo BonziniLifetime of the query handle
40e9adb4acSPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41e9adb4acSPaolo Bonzini
42e9adb4acSPaolo BonziniEach callback provides an opaque anonymous information handle which
43e9adb4acSPaolo Bonzinican usually be further queried to find out information about a
44e9adb4acSPaolo Bonzinitranslation, instruction or operation. The handles themselves are only
45e9adb4acSPaolo Bonzinivalid during the lifetime of the callback so it is important that any
46e9adb4acSPaolo Bonziniinformation that is needed is extracted during the callback and saved
47e9adb4acSPaolo Bonziniby the plugin.
48e9adb4acSPaolo Bonzini
49e9adb4acSPaolo BonziniPlugin life cycle
50e9adb4acSPaolo Bonzini~~~~~~~~~~~~~~~~~
51027e3332SAlex Bennée
52027e3332SAlex BennéeFirst the plugin is loaded and the public qemu_plugin_install function
53027e3332SAlex Bennéeis called. The plugin will then register callbacks for various plugin
54027e3332SAlex Bennéeevents. Generally plugins will register a handler for the *atexit*
55027e3332SAlex Bennéeif they want to dump a summary of collected information once the
56027e3332SAlex Bennéeprogram/system has finished running.
57027e3332SAlex Bennée
58027e3332SAlex BennéeWhen a registered event occurs the plugin callback is invoked. The
59027e3332SAlex Bennéecallbacks may provide additional information. In the case of a
60027e3332SAlex Bennéetranslation event the plugin has an option to enumerate the
61027e3332SAlex Bennéeinstructions in a block of instructions and optionally register
62027e3332SAlex Bennéecallbacks to some or all instructions when they are executed.
63027e3332SAlex Bennée
64*3f9f9a37SPierrick BouvierThere is also a facility to add inline instructions doing various operations,
65*3f9f9a37SPierrick Bouvierlike adding or storing an immediate value. It is also possible to execute a
66*3f9f9a37SPierrick Bouviercallback conditionally, with condition being evaluated inline. All those inline
67*3f9f9a37SPierrick Bouvieroperations are associated to a ``scoreboard``, which is a thread-local storage
68*3f9f9a37SPierrick Bouvierautomatically expanded when new cores/threads are created and that can be
69*3f9f9a37SPierrick Bouvieraccessed/modified in a thread-safe way without any lock needed. Combining inline
70*3f9f9a37SPierrick Bouvieroperations and conditional callbacks offer a more efficient way to instrument
71*3f9f9a37SPierrick Bouvierbinaries, compared to classic callbacks.
72027e3332SAlex Bennée
73027e3332SAlex BennéeFinally when QEMU exits all the registered *atexit* callbacks are
74027e3332SAlex Bennéeinvoked.
75027e3332SAlex Bennée
76e9adb4acSPaolo BonziniExposure of QEMU internals
77e9adb4acSPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~
78e9adb4acSPaolo Bonzini
79e9adb4acSPaolo BonziniThe plugin architecture actively avoids leaking implementation details
80e9adb4acSPaolo Bonziniabout how QEMU's translation works to the plugins. While there are
81e9adb4acSPaolo Bonziniconceptions such as translation time and translation blocks the
82e9adb4acSPaolo Bonzinidetails are opaque to plugins. The plugin is able to query select
83e9adb4acSPaolo Bonzinidetails of instructions and system configuration only through the
84e9adb4acSPaolo Bonziniexported *qemu_plugin* functions.
85e9adb4acSPaolo Bonzini
86f87b220fSAlex BennéeHowever the following assumptions can be made:
87f87b220fSAlex Bennée
88f87b220fSAlex BennéeTranslation Blocks
89f87b220fSAlex Bennée++++++++++++++++++
90f87b220fSAlex Bennée
91f87b220fSAlex BennéeAll code will go through a translation phase although not all
92f87b220fSAlex Bennéetranslations will be necessarily be executed. You need to instrument
93f87b220fSAlex Bennéeactual executions to track what is happening.
94f87b220fSAlex Bennée
95f87b220fSAlex BennéeIt is quite normal to see the same address translated multiple times.
96f87b220fSAlex BennéeIf you want to track the code in system emulation you should examine
97f87b220fSAlex Bennéethe underlying physical address (``qemu_plugin_insn_haddr``) to take
98f87b220fSAlex Bennéeinto account the effects of virtual memory although if the system does
99f87b220fSAlex Bennéepaging this will change too.
100f87b220fSAlex Bennée
101f87b220fSAlex BennéeNot all instructions in a block will always execute so if its
102f87b220fSAlex Bennéeimportant to track individual instruction execution you need to
103f87b220fSAlex Bennéeinstrument them directly. However asynchronous interrupts will not
104f87b220fSAlex Bennéechange control flow mid-block.
105f87b220fSAlex Bennée
106f87b220fSAlex BennéeInstructions
107f87b220fSAlex Bennée++++++++++++
108f87b220fSAlex Bennée
109f87b220fSAlex BennéeInstruction instrumentation runs before the instruction executes. You
110f87b220fSAlex Bennéecan be can be sure the instruction will be dispatched, but you can't
111f87b220fSAlex Bennéebe sure it will complete. Generally this will be because of a
112f87b220fSAlex Bennéesynchronous exception (e.g. SIGILL) triggered by the instruction
113f87b220fSAlex Bennéeattempting to execute. If you want to be sure you will need to
114f87b220fSAlex Bennéeinstrument the next instruction as well. See the ``execlog.c`` plugin
115f87b220fSAlex Bennéefor examples of how to track this and finalise details after execution.
116f87b220fSAlex Bennée
117f87b220fSAlex BennéeMemory Accesses
118f87b220fSAlex Bennée+++++++++++++++
119f87b220fSAlex Bennée
120f87b220fSAlex BennéeMemory callbacks are called after a successful load or store.
121f87b220fSAlex BennéeUnsuccessful operations (i.e. faults) will not be visible to memory
122f87b220fSAlex Bennéeinstrumentation although the execution side effects can be observed
123f87b220fSAlex Bennée(e.g. entering a exception handler).
124f87b220fSAlex Bennée
125f87b220fSAlex BennéeSystem Idle and Resume States
126f87b220fSAlex Bennée+++++++++++++++++++++++++++++
127f87b220fSAlex Bennée
128f87b220fSAlex BennéeThe ``qemu_plugin_register_vcpu_idle_cb`` and
129f87b220fSAlex Bennée``qemu_plugin_register_vcpu_resume_cb`` functions can be used to track
130f87b220fSAlex Bennéewhen CPUs go into and return from sleep states when waiting for
131f87b220fSAlex Bennéeexternal I/O. Be aware though that these may occur less frequently
132f87b220fSAlex Bennéethan in real HW due to the inefficiencies of emulation giving less
133f87b220fSAlex Bennéechance for the CPU to idle.
134f87b220fSAlex Bennée
135027e3332SAlex BennéeInternals
136e9adb4acSPaolo Bonzini---------
137027e3332SAlex Bennée
138027e3332SAlex BennéeLocking
139e9adb4acSPaolo Bonzini~~~~~~~
140027e3332SAlex Bennée
141027e3332SAlex BennéeWe have to ensure we cannot deadlock, particularly under MTTCG. For
142027e3332SAlex Bennéethis we acquire a lock when called from plugin code. We also keep the
143027e3332SAlex Bennéelist of callbacks under RCU so that we do not have to hold the lock
144027e3332SAlex Bennéewhen calling the callbacks. This is also for performance, since some
145027e3332SAlex Bennéecallbacks (e.g. memory access callbacks) might be called very
146027e3332SAlex Bennéefrequently.
147027e3332SAlex Bennée
148027e3332SAlex Bennée  * A consequence of this is that we keep our own list of CPUs, so that
149027e3332SAlex Bennée    we do not have to worry about locking order wrt cpu_list_lock.
150027e3332SAlex Bennée  * Use a recursive lock, since we can get registration calls from
151027e3332SAlex Bennée    callbacks.
152027e3332SAlex Bennée
153027e3332SAlex BennéeAs a result registering/unregistering callbacks is "slow", since it
154027e3332SAlex Bennéetakes a lock. But this is very infrequent; we want performance when
155027e3332SAlex Bennéecalling (or not calling) callbacks, not when registering them. Using
156027e3332SAlex BennéeRCU is great for this.
157027e3332SAlex Bennée
158027e3332SAlex BennéeWe support the uninstallation of a plugin at any time (e.g. from
159027e3332SAlex Bennéeplugin callbacks). This allows plugins to remove themselves if they no
160027e3332SAlex Bennéelonger want to instrument the code. This operation is asynchronous
161027e3332SAlex Bennéewhich means callbacks may still occur after the uninstall operation is
162027e3332SAlex Bennéerequested. The plugin isn't completely uninstalled until the safe work
163027e3332SAlex Bennéehas executed while all vCPUs are quiescent.
164c17a386bSAlex Bennée
165b0b3c0f5SAlex BennéePlugin API
166b0b3c0f5SAlex Bennée==========
1677f522743SAlex Bennée
1687f522743SAlex BennéeThe following API is generated from the inline documentation in
1697f522743SAlex Bennée``include/qemu/qemu-plugin.h``. Please ensure any updates to the API
1707f522743SAlex Bennéeinclude the full kernel-doc annotations.
1717f522743SAlex Bennée
1727f522743SAlex Bennée.. kernel-doc:: include/qemu/qemu-plugin.h
173