18ea6abf0SAlex Bennée.. 28ea6abf0SAlex Bennée Copyright (C) 2017, Emilio G. Cota <cota@braap.org> 38ea6abf0SAlex Bennée Copyright (c) 2019, Linaro Limited 48ea6abf0SAlex Bennée Written by Emilio Cota and Alex Bennée 58ea6abf0SAlex Bennée 6a0a6754bSAlex Bennée.. _TCG Plugins: 7a0a6754bSAlex Bennée 88ea6abf0SAlex BennéeQEMU TCG Plugins 98ea6abf0SAlex Bennée================ 108ea6abf0SAlex 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~~~~~~~~~~~~~~~~~ 518ea6abf0SAlex Bennée 528ea6abf0SAlex BennéeFirst the plugin is loaded and the public qemu_plugin_install function 538ea6abf0SAlex Bennéeis called. The plugin will then register callbacks for various plugin 548ea6abf0SAlex Bennéeevents. Generally plugins will register a handler for the *atexit* 558ea6abf0SAlex Bennéeif they want to dump a summary of collected information once the 568ea6abf0SAlex Bennéeprogram/system has finished running. 578ea6abf0SAlex Bennée 588ea6abf0SAlex BennéeWhen a registered event occurs the plugin callback is invoked. The 598ea6abf0SAlex Bennéecallbacks may provide additional information. In the case of a 608ea6abf0SAlex Bennéetranslation event the plugin has an option to enumerate the 618ea6abf0SAlex Bennéeinstructions in a block of instructions and optionally register 628ea6abf0SAlex Bennéecallbacks to some or all instructions when they are executed. 638ea6abf0SAlex 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. 728ea6abf0SAlex Bennée 738ea6abf0SAlex BennéeFinally when QEMU exits all the registered *atexit* callbacks are 748ea6abf0SAlex Bennéeinvoked. 758ea6abf0SAlex 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 1358ea6abf0SAlex BennéeInternals 136e9adb4acSPaolo Bonzini--------- 1378ea6abf0SAlex Bennée 1388ea6abf0SAlex BennéeLocking 139e9adb4acSPaolo Bonzini~~~~~~~ 1408ea6abf0SAlex Bennée 1418ea6abf0SAlex BennéeWe have to ensure we cannot deadlock, particularly under MTTCG. For 1428ea6abf0SAlex Bennéethis we acquire a lock when called from plugin code. We also keep the 1438ea6abf0SAlex Bennéelist of callbacks under RCU so that we do not have to hold the lock 1448ea6abf0SAlex Bennéewhen calling the callbacks. This is also for performance, since some 1458ea6abf0SAlex Bennéecallbacks (e.g. memory access callbacks) might be called very 1468ea6abf0SAlex Bennéefrequently. 1478ea6abf0SAlex Bennée 1488ea6abf0SAlex Bennée * A consequence of this is that we keep our own list of CPUs, so that 1498ea6abf0SAlex Bennée we do not have to worry about locking order wrt cpu_list_lock. 1508ea6abf0SAlex Bennée * Use a recursive lock, since we can get registration calls from 1518ea6abf0SAlex Bennée callbacks. 1528ea6abf0SAlex Bennée 1538ea6abf0SAlex BennéeAs a result registering/unregistering callbacks is "slow", since it 1548ea6abf0SAlex Bennéetakes a lock. But this is very infrequent; we want performance when 1558ea6abf0SAlex Bennéecalling (or not calling) callbacks, not when registering them. Using 1568ea6abf0SAlex BennéeRCU is great for this. 1578ea6abf0SAlex Bennée 1588ea6abf0SAlex BennéeWe support the uninstallation of a plugin at any time (e.g. from 1598ea6abf0SAlex Bennéeplugin callbacks). This allows plugins to remove themselves if they no 1608ea6abf0SAlex Bennéelonger want to instrument the code. This operation is asynchronous 1618ea6abf0SAlex Bennéewhich means callbacks may still occur after the uninstall operation is 1628ea6abf0SAlex Bennéerequested. The plugin isn't completely uninstalled until the safe work 1638ea6abf0SAlex 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