xref: /qemu/docs/devel/tcg-plugins.rst (revision a622d64eea14ee0162621850093137a086c00ab5)
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
549675a9c6SAlex Bennéeexported *qemu_plugin* functions.
559675a9c6SAlex Bennée
569675a9c6SAlex BennéeQuery Handle Lifetime
579675a9c6SAlex Bennée---------------------
589675a9c6SAlex Bennée
599675a9c6SAlex BennéeEach callback provides an opaque anonymous information handle which
609675a9c6SAlex Bennéecan usually be further queried to find out information about a
619675a9c6SAlex Bennéetranslation, instruction or operation. The handles themselves are only
629675a9c6SAlex Bennéevalid during the lifetime of the callback so it is important that any
639675a9c6SAlex Bennéeinformation that is needed is extracted during the callback and saved
649675a9c6SAlex 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.
137c17a386bSAlex Bennée
138c17a386bSAlex BennéeExample Plugins
139c17a386bSAlex Bennée===============
140c17a386bSAlex Bennée
141c17a386bSAlex BennéeThere are a number of plugins included with QEMU and you are
142c17a386bSAlex Bennéeencouraged to contribute your own plugins plugins upstream. There is a
143c17a386bSAlex Bennée`contrib/plugins` directory where they can go.
144c17a386bSAlex Bennée
145c17a386bSAlex Bennée- tests/plugins
146c17a386bSAlex Bennée
147c17a386bSAlex BennéeThese are some basic plugins that are used to test and exercise the
148c17a386bSAlex BennéeAPI during the `make check-tcg` target.
149c17a386bSAlex Bennée
150c17a386bSAlex Bennée- contrib/plugins/hotblocks.c
151c17a386bSAlex Bennée
152c17a386bSAlex BennéeThe hotblocks plugin allows you to examine the where hot paths of
153c17a386bSAlex Bennéeexecution are in your program. Once the program has finished you will
154c17a386bSAlex Bennéeget a sorted list of blocks reporting the starting PC, translation
155c17a386bSAlex Bennéecount, number of instructions and execution count. This will work best
156c17a386bSAlex Bennéewith linux-user execution as system emulation tends to generate
157c17a386bSAlex Bennéere-translations as blocks from different programs get swapped in and
158c17a386bSAlex Bennéeout of system memory.
159c17a386bSAlex Bennée
160c17a386bSAlex BennéeIf your program is single-threaded you can use the `inline` option for
161c17a386bSAlex Bennéeslightly faster (but not thread safe) counters.
162c17a386bSAlex Bennée
163c17a386bSAlex BennéeExample::
164c17a386bSAlex Bennée
165c17a386bSAlex Bennée  ./aarch64-linux-user/qemu-aarch64 \
166c17a386bSAlex Bennée    -plugin contrib/plugins/libhotblocks.so -d plugin \
167c17a386bSAlex Bennée    ./tests/tcg/aarch64-linux-user/sha1
168c17a386bSAlex Bennée  SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6
169c17a386bSAlex Bennée  collected 903 entries in the hash table
170c17a386bSAlex Bennée  pc, tcount, icount, ecount
171c17a386bSAlex Bennée  0x0000000041ed10, 1, 5, 66087
172c17a386bSAlex Bennée  0x000000004002b0, 1, 4, 66087
173c17a386bSAlex Bennée  ...
174c17a386bSAlex Bennée
175c17a386bSAlex Bennée- contrib/plugins/hotpages.c
176c17a386bSAlex Bennée
177c17a386bSAlex BennéeSimilar to hotblocks but this time tracks memory accesses::
178c17a386bSAlex Bennée
179c17a386bSAlex Bennée  ./aarch64-linux-user/qemu-aarch64 \
180c17a386bSAlex Bennée    -plugin contrib/plugins/libhotpages.so -d plugin \
181c17a386bSAlex Bennée    ./tests/tcg/aarch64-linux-user/sha1
182c17a386bSAlex Bennée  SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6
183c17a386bSAlex Bennée  Addr, RCPUs, Reads, WCPUs, Writes
184c17a386bSAlex Bennée  0x000055007fe000, 0x0001, 31747952, 0x0001, 8835161
185c17a386bSAlex Bennée  0x000055007ff000, 0x0001, 29001054, 0x0001, 8780625
186c17a386bSAlex Bennée  0x00005500800000, 0x0001, 687465, 0x0001, 335857
187c17a386bSAlex Bennée  0x0000000048b000, 0x0001, 130594, 0x0001, 355
188c17a386bSAlex Bennée  0x0000000048a000, 0x0001, 1826, 0x0001, 11
189c17a386bSAlex Bennée
190c17a386bSAlex Bennée- contrib/plugins/howvec.c
191c17a386bSAlex Bennée
192c17a386bSAlex BennéeThis is an instruction classifier so can be used to count different
193c17a386bSAlex Bennéetypes of instructions. It has a number of options to refine which get
194c17a386bSAlex Bennéecounted. You can give an argument for a class of instructions to break
195c17a386bSAlex Bennéeit down fully, so for example to see all the system registers
196c17a386bSAlex Bennéeaccesses::
197c17a386bSAlex Bennée
198c17a386bSAlex Bennée  ./aarch64-softmmu/qemu-system-aarch64 $(QEMU_ARGS) \
199c17a386bSAlex Bennée    -append "root=/dev/sda2 systemd.unit=benchmark.service" \
200c17a386bSAlex Bennée    -smp 4 -plugin ./contrib/plugins/libhowvec.so,arg=sreg -d plugin
201c17a386bSAlex Bennée
202c17a386bSAlex Bennéewhich will lead to a sorted list after the class breakdown::
203c17a386bSAlex Bennée
204c17a386bSAlex Bennée  Instruction Classes:
205c17a386bSAlex Bennée  Class:   UDEF                   not counted
206c17a386bSAlex Bennée  Class:   SVE                    (68 hits)
207c17a386bSAlex Bennée  Class:   PCrel addr             (47789483 hits)
208c17a386bSAlex Bennée  Class:   Add/Sub (imm)          (192817388 hits)
209c17a386bSAlex Bennée  Class:   Logical (imm)          (93852565 hits)
210c17a386bSAlex Bennée  Class:   Move Wide (imm)        (76398116 hits)
211c17a386bSAlex Bennée  Class:   Bitfield               (44706084 hits)
212c17a386bSAlex Bennée  Class:   Extract                (5499257 hits)
213c17a386bSAlex Bennée  Class:   Cond Branch (imm)      (147202932 hits)
214c17a386bSAlex Bennée  Class:   Exception Gen          (193581 hits)
215c17a386bSAlex Bennée  Class:     NOP                  not counted
216c17a386bSAlex Bennée  Class:   Hints                  (6652291 hits)
217c17a386bSAlex Bennée  Class:   Barriers               (8001661 hits)
218c17a386bSAlex Bennée  Class:   PSTATE                 (1801695 hits)
219c17a386bSAlex Bennée  Class:   System Insn            (6385349 hits)
220c17a386bSAlex Bennée  Class:   System Reg             counted individually
221c17a386bSAlex Bennée  Class:   Branch (reg)           (69497127 hits)
222c17a386bSAlex Bennée  Class:   Branch (imm)           (84393665 hits)
223c17a386bSAlex Bennée  Class:   Cmp & Branch           (110929659 hits)
224c17a386bSAlex Bennée  Class:   Tst & Branch           (44681442 hits)
225c17a386bSAlex Bennée  Class:   AdvSimd ldstmult       (736 hits)
226c17a386bSAlex Bennée  Class:   ldst excl              (9098783 hits)
227c17a386bSAlex Bennée  Class:   Load Reg (lit)         (87189424 hits)
228c17a386bSAlex Bennée  Class:   ldst noalloc pair      (3264433 hits)
229c17a386bSAlex Bennée  Class:   ldst pair              (412526434 hits)
230c17a386bSAlex Bennée  Class:   ldst reg (imm)         (314734576 hits)
231c17a386bSAlex Bennée  Class: Loads & Stores           (2117774 hits)
232c17a386bSAlex Bennée  Class: Data Proc Reg            (223519077 hits)
233c17a386bSAlex Bennée  Class: Scalar FP                (31657954 hits)
234c17a386bSAlex Bennée  Individual Instructions:
235c17a386bSAlex Bennée  Instr: mrs x0, sp_el0           (2682661 hits)  (op=0xd5384100/  System Reg)
236c17a386bSAlex Bennée  Instr: mrs x1, tpidr_el2        (1789339 hits)  (op=0xd53cd041/  System Reg)
237c17a386bSAlex Bennée  Instr: mrs x2, tpidr_el2        (1513494 hits)  (op=0xd53cd042/  System Reg)
238c17a386bSAlex Bennée  Instr: mrs x0, tpidr_el2        (1490823 hits)  (op=0xd53cd040/  System Reg)
239c17a386bSAlex Bennée  Instr: mrs x1, sp_el0           (933793 hits)   (op=0xd5384101/  System Reg)
240c17a386bSAlex Bennée  Instr: mrs x2, sp_el0           (699516 hits)   (op=0xd5384102/  System Reg)
241c17a386bSAlex Bennée  Instr: mrs x4, tpidr_el2        (528437 hits)   (op=0xd53cd044/  System Reg)
242c17a386bSAlex Bennée  Instr: mrs x30, ttbr1_el1       (480776 hits)   (op=0xd538203e/  System Reg)
243c17a386bSAlex Bennée  Instr: msr ttbr1_el1, x30       (480713 hits)   (op=0xd518203e/  System Reg)
244c17a386bSAlex Bennée  Instr: msr vbar_el1, x30        (480671 hits)   (op=0xd518c01e/  System Reg)
245c17a386bSAlex Bennée  ...
246c17a386bSAlex Bennée
247c17a386bSAlex BennéeTo find the argument shorthand for the class you need to examine the
248c17a386bSAlex Bennéesource code of the plugin at the moment, specifically the `*opt`
249c17a386bSAlex Bennéeargument in the InsnClassExecCount tables.
250c17a386bSAlex Bennée
251c17a386bSAlex Bennée- contrib/plugins/lockstep.c
252c17a386bSAlex Bennée
253c17a386bSAlex BennéeThis is a debugging tool for developers who want to find out when and
254c17a386bSAlex Bennéewhere execution diverges after a subtle change to TCG code generation.
255c17a386bSAlex BennéeIt is not an exact science and results are likely to be mixed once
256c17a386bSAlex Bennéeasynchronous events are introduced. While the use of -icount can
257c17a386bSAlex Bennéeintroduce determinism to the execution flow it doesn't always follow
258c17a386bSAlex Bennéethe translation sequence will be exactly the same. Typically this is
259c17a386bSAlex Bennéecaused by a timer firing to service the GUI causing a block to end
260c17a386bSAlex Bennéeearly. However in some cases it has proved to be useful in pointing
261c17a386bSAlex Bennéepeople at roughly where execution diverges. The only argument you need
262c17a386bSAlex Bennéefor the plugin is a path for the socket the two instances will
263c17a386bSAlex Bennéecommunicate over::
264c17a386bSAlex Bennée
265c17a386bSAlex Bennée
266c17a386bSAlex Bennée  ./sparc-softmmu/qemu-system-sparc -monitor none -parallel none \
267c17a386bSAlex Bennée    -net none -M SS-20 -m 256 -kernel day11/zImage.elf \
268c17a386bSAlex Bennée    -plugin ./contrib/plugins/liblockstep.so,arg=lockstep-sparc.sock \
269c17a386bSAlex Bennée  -d plugin,nochain
270c17a386bSAlex Bennée
271c17a386bSAlex Bennéewhich will eventually report::
272c17a386bSAlex Bennée
273c17a386bSAlex Bennée  qemu-system-sparc: warning: nic lance.0 has no peer
274c17a386bSAlex Bennée  @ 0x000000ffd06678 vs 0x000000ffd001e0 (2/1 since last)
275c17a386bSAlex Bennée  @ 0x000000ffd07d9c vs 0x000000ffd06678 (3/1 since last)
276c17a386bSAlex Bennée  Δ insn_count @ 0x000000ffd07d9c (809900609) vs 0x000000ffd06678 (809900612)
277c17a386bSAlex Bennée    previously @ 0x000000ffd06678/10 (809900609 insns)
278c17a386bSAlex Bennée    previously @ 0x000000ffd001e0/4 (809900599 insns)
279c17a386bSAlex Bennée    previously @ 0x000000ffd080ac/2 (809900595 insns)
280c17a386bSAlex Bennée    previously @ 0x000000ffd08098/5 (809900593 insns)
281c17a386bSAlex Bennée    previously @ 0x000000ffd080c0/1 (809900588 insns)
282c17a386bSAlex Bennée
283*a622d64eSAlex Bennée- contrib/plugins/hwprofile
284*a622d64eSAlex Bennée
285*a622d64eSAlex BennéeThe hwprofile tool can only be used with system emulation and allows
286*a622d64eSAlex Bennéethe user to see what hardware is accessed how often. It has a number of options:
287*a622d64eSAlex Bennée
288*a622d64eSAlex Bennée * arg=read or arg=write
289*a622d64eSAlex Bennée
290*a622d64eSAlex Bennée By default the plugin tracks both reads and writes. You can use one
291*a622d64eSAlex Bennée of these options to limit the tracking to just one class of accesses.
292*a622d64eSAlex Bennée
293*a622d64eSAlex Bennée * arg=source
294*a622d64eSAlex Bennée
295*a622d64eSAlex Bennée Will include a detailed break down of what the guest PC that made the
296*a622d64eSAlex Bennée access was. Not compatible with arg=pattern. Example output::
297*a622d64eSAlex Bennée
298*a622d64eSAlex Bennée   cirrus-low-memory @ 0xfffffd00000a0000
299*a622d64eSAlex Bennée    pc:fffffc0000005cdc, 1, 256
300*a622d64eSAlex Bennée    pc:fffffc0000005ce8, 1, 256
301*a622d64eSAlex Bennée    pc:fffffc0000005cec, 1, 256
302*a622d64eSAlex Bennée
303*a622d64eSAlex Bennée * arg=pattern
304*a622d64eSAlex Bennée
305*a622d64eSAlex Bennée Instead break down the accesses based on the offset into the HW
306*a622d64eSAlex Bennée region. This can be useful for seeing the most used registers of a
307*a622d64eSAlex Bennée device. Example output::
308*a622d64eSAlex Bennée
309*a622d64eSAlex Bennée    pci0-conf @ 0xfffffd01fe000000
310*a622d64eSAlex Bennée      off:00000004, 1, 1
311*a622d64eSAlex Bennée      off:00000010, 1, 3
312*a622d64eSAlex Bennée      off:00000014, 1, 3
313*a622d64eSAlex Bennée      off:00000018, 1, 2
314*a622d64eSAlex Bennée      off:0000001c, 1, 2
315*a622d64eSAlex Bennée      off:00000020, 1, 2
316*a622d64eSAlex Bennée      ...
317