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 6027e3332SAlex Bennée================ 7027e3332SAlex BennéeQEMU TCG Plugins 8027e3332SAlex Bennée================ 9027e3332SAlex Bennée 10027e3332SAlex BennéeQEMU TCG plugins provide a way for users to run experiments taking 11027e3332SAlex Bennéeadvantage of the total system control emulation can have over a guest. 12027e3332SAlex BennéeIt provides a mechanism for plugins to subscribe to events during 13027e3332SAlex Bennéetranslation and execution and optionally callback into the plugin 14027e3332SAlex Bennéeduring these events. TCG plugins are unable to change the system state 15027e3332SAlex Bennéeonly monitor it passively. However they can do this down to an 16027e3332SAlex Bennéeindividual instruction granularity including potentially subscribing 17027e3332SAlex Bennéeto all load and store operations. 18027e3332SAlex Bennée 19027e3332SAlex BennéeAPI Stability 20027e3332SAlex Bennée============= 21027e3332SAlex Bennée 22027e3332SAlex BennéeThis is a new feature for QEMU and it does allow people to develop 23027e3332SAlex Bennéeout-of-tree plugins that can be dynamically linked into a running QEMU 24027e3332SAlex Bennéeprocess. However the project reserves the right to change or break the 25027e3332SAlex BennéeAPI should it need to do so. The best way to avoid this is to submit 26027e3332SAlex Bennéeyour plugin upstream so they can be updated if/when the API changes. 27027e3332SAlex Bennée 285c6ecbdcSAlex BennéeAPI versioning 295c6ecbdcSAlex Bennée-------------- 305c6ecbdcSAlex Bennée 315c6ecbdcSAlex BennéeAll plugins need to declare a symbol which exports the plugin API 325c6ecbdcSAlex Bennéeversion they were built against. This can be done simply by:: 335c6ecbdcSAlex Bennée 345c6ecbdcSAlex Bennée QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; 355c6ecbdcSAlex Bennée 365c6ecbdcSAlex BennéeThe core code will refuse to load a plugin that doesn't export a 375c6ecbdcSAlex Bennée`qemu_plugin_version` symbol or if plugin version is outside of QEMU's 385c6ecbdcSAlex Bennéesupported range of API versions. 395c6ecbdcSAlex Bennée 405c6ecbdcSAlex BennéeAdditionally the `qemu_info_t` structure which is passed to the 415c6ecbdcSAlex Bennée`qemu_plugin_install` method of a plugin will detail the minimum and 425c6ecbdcSAlex Bennéecurrent API versions supported by QEMU. The API version will be 435c6ecbdcSAlex Bennéeincremented if new APIs are added. The minimum API version will be 445c6ecbdcSAlex Bennéeincremented if existing APIs are changed or removed. 45027e3332SAlex Bennée 46027e3332SAlex BennéeExposure of QEMU internals 47027e3332SAlex Bennée-------------------------- 48027e3332SAlex Bennée 49027e3332SAlex BennéeThe plugin architecture actively avoids leaking implementation details 50027e3332SAlex Bennéeabout how QEMU's translation works to the plugins. While there are 51027e3332SAlex Bennéeconceptions such as translation time and translation blocks the 52027e3332SAlex Bennéedetails are opaque to plugins. The plugin is able to query select 53027e3332SAlex Bennéedetails of instructions and system configuration only through the 54*9675a9c6SAlex Bennéeexported *qemu_plugin* functions. 55*9675a9c6SAlex Bennée 56*9675a9c6SAlex BennéeQuery Handle Lifetime 57*9675a9c6SAlex Bennée--------------------- 58*9675a9c6SAlex Bennée 59*9675a9c6SAlex BennéeEach callback provides an opaque anonymous information handle which 60*9675a9c6SAlex Bennéecan usually be further queried to find out information about a 61*9675a9c6SAlex Bennéetranslation, instruction or operation. The handles themselves are only 62*9675a9c6SAlex Bennéevalid during the lifetime of the callback so it is important that any 63*9675a9c6SAlex Bennéeinformation that is needed is extracted during the callback and saved 64*9675a9c6SAlex Bennéeby the plugin. 65027e3332SAlex Bennée 66027e3332SAlex BennéeUsage 67027e3332SAlex Bennée===== 68027e3332SAlex Bennée 695c6ecbdcSAlex BennéeThe QEMU binary needs to be compiled for plugin support:: 70027e3332SAlex Bennée 71027e3332SAlex Bennée configure --enable-plugins 72027e3332SAlex Bennée 73027e3332SAlex BennéeOnce built a program can be run with multiple plugins loaded each with 745c6ecbdcSAlex Bennéetheir own arguments:: 75027e3332SAlex Bennée 76027e3332SAlex Bennée $QEMU $OTHER_QEMU_ARGS \ 77027e3332SAlex Bennée -plugin tests/plugin/libhowvec.so,arg=inline,arg=hint \ 78027e3332SAlex Bennée -plugin tests/plugin/libhotblocks.so 79027e3332SAlex Bennée 80027e3332SAlex BennéeArguments are plugin specific and can be used to modify their 81027e3332SAlex Bennéebehaviour. In this case the howvec plugin is being asked to use inline 82027e3332SAlex Bennéeops to count and break down the hint instructions by type. 83027e3332SAlex Bennée 84027e3332SAlex BennéePlugin Life cycle 85027e3332SAlex Bennée================= 86027e3332SAlex Bennée 87027e3332SAlex BennéeFirst the plugin is loaded and the public qemu_plugin_install function 88027e3332SAlex Bennéeis called. The plugin will then register callbacks for various plugin 89027e3332SAlex Bennéeevents. Generally plugins will register a handler for the *atexit* 90027e3332SAlex Bennéeif they want to dump a summary of collected information once the 91027e3332SAlex Bennéeprogram/system has finished running. 92027e3332SAlex Bennée 93027e3332SAlex BennéeWhen a registered event occurs the plugin callback is invoked. The 94027e3332SAlex Bennéecallbacks may provide additional information. In the case of a 95027e3332SAlex Bennéetranslation event the plugin has an option to enumerate the 96027e3332SAlex Bennéeinstructions in a block of instructions and optionally register 97027e3332SAlex Bennéecallbacks to some or all instructions when they are executed. 98027e3332SAlex Bennée 99027e3332SAlex BennéeThere is also a facility to add an inline event where code to 100027e3332SAlex Bennéeincrement a counter can be directly inlined with the translation. 101027e3332SAlex BennéeCurrently only a simple increment is supported. This is not atomic so 102027e3332SAlex Bennéecan miss counts. If you want absolute precision you should use a 103027e3332SAlex Bennéecallback which can then ensure atomicity itself. 104027e3332SAlex Bennée 105027e3332SAlex BennéeFinally when QEMU exits all the registered *atexit* callbacks are 106027e3332SAlex Bennéeinvoked. 107027e3332SAlex Bennée 108027e3332SAlex BennéeInternals 109027e3332SAlex Bennée========= 110027e3332SAlex Bennée 111027e3332SAlex BennéeLocking 112027e3332SAlex Bennée------- 113027e3332SAlex Bennée 114027e3332SAlex BennéeWe have to ensure we cannot deadlock, particularly under MTTCG. For 115027e3332SAlex Bennéethis we acquire a lock when called from plugin code. We also keep the 116027e3332SAlex Bennéelist of callbacks under RCU so that we do not have to hold the lock 117027e3332SAlex Bennéewhen calling the callbacks. This is also for performance, since some 118027e3332SAlex Bennéecallbacks (e.g. memory access callbacks) might be called very 119027e3332SAlex Bennéefrequently. 120027e3332SAlex Bennée 121027e3332SAlex Bennée * A consequence of this is that we keep our own list of CPUs, so that 122027e3332SAlex Bennée we do not have to worry about locking order wrt cpu_list_lock. 123027e3332SAlex Bennée * Use a recursive lock, since we can get registration calls from 124027e3332SAlex Bennée callbacks. 125027e3332SAlex Bennée 126027e3332SAlex BennéeAs a result registering/unregistering callbacks is "slow", since it 127027e3332SAlex Bennéetakes a lock. But this is very infrequent; we want performance when 128027e3332SAlex Bennéecalling (or not calling) callbacks, not when registering them. Using 129027e3332SAlex BennéeRCU is great for this. 130027e3332SAlex Bennée 131027e3332SAlex BennéeWe support the uninstallation of a plugin at any time (e.g. from 132027e3332SAlex Bennéeplugin callbacks). This allows plugins to remove themselves if they no 133027e3332SAlex Bennéelonger want to instrument the code. This operation is asynchronous 134027e3332SAlex Bennéewhich means callbacks may still occur after the uninstall operation is 135027e3332SAlex Bennéerequested. The plugin isn't completely uninstalled until the safe work 136027e3332SAlex Bennéehas executed while all vCPUs are quiescent. 137