xref: /qemu/include/qemu/qemu-plugin.h (revision 0bcebaba)
1 /*
2  * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
3  * Copyright (C) 2019, Linaro
4  *
5  * License: GNU GPL, version 2 or later.
6  *   See the COPYING file in the top-level directory.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #ifndef QEMU_QEMU_PLUGIN_H
12 #define QEMU_QEMU_PLUGIN_H
13 
14 #include <glib.h>
15 #include <inttypes.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18 
19 /*
20  * For best performance, build the plugin with -fvisibility=hidden so that
21  * QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with
22  * QEMU_PLUGIN_EXPORT. For more info, see
23  *   https://gcc.gnu.org/wiki/Visibility
24  */
25 #if defined _WIN32 || defined __CYGWIN__
26   #ifdef CONFIG_PLUGIN
27     #define QEMU_PLUGIN_EXPORT __declspec(dllimport)
28     #define QEMU_PLUGIN_API __declspec(dllexport)
29   #else
30     #define QEMU_PLUGIN_EXPORT __declspec(dllexport)
31     #define QEMU_PLUGIN_API __declspec(dllimport)
32   #endif
33   #define QEMU_PLUGIN_LOCAL
34 #else
35   #define QEMU_PLUGIN_EXPORT __attribute__((visibility("default")))
36   #define QEMU_PLUGIN_LOCAL  __attribute__((visibility("hidden")))
37   #define QEMU_PLUGIN_API
38 #endif
39 
40 /**
41  * typedef qemu_plugin_id_t - Unique plugin ID
42  */
43 typedef uint64_t qemu_plugin_id_t;
44 
45 /*
46  * Versioning plugins:
47  *
48  * The plugin API will pass a minimum and current API version that
49  * QEMU currently supports. The minimum API will be incremented if an
50  * API needs to be deprecated.
51  *
52  * The plugins export the API they were built against by exposing the
53  * symbol qemu_plugin_version which can be checked.
54  *
55  * version 2: removed qemu_plugin_n_vcpus and qemu_plugin_n_max_vcpus
56  */
57 
58 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
59 
60 #define QEMU_PLUGIN_VERSION 2
61 
62 /**
63  * struct qemu_info_t - system information for plugins
64  *
65  * This structure provides for some limited information about the
66  * system to allow the plugin to make decisions on how to proceed. For
67  * example it might only be suitable for running on some guest
68  * architectures or when under full system emulation.
69  */
70 typedef struct qemu_info_t {
71     /** @target_name: string describing architecture */
72     const char *target_name;
73     /** @version: minimum and current plugin API level */
74     struct {
75         int min;
76         int cur;
77     } version;
78     /** @system_emulation: is this a full system emulation? */
79     bool system_emulation;
80     union {
81         /** @system: information relevant to system emulation */
82         struct {
83             /** @system.smp_vcpus: initial number of vCPUs */
84             int smp_vcpus;
85             /** @system.max_vcpus: maximum possible number of vCPUs */
86             int max_vcpus;
87         } system;
88     };
89 } qemu_info_t;
90 
91 /**
92  * qemu_plugin_install() - Install a plugin
93  * @id: this plugin's opaque ID
94  * @info: a block describing some details about the guest
95  * @argc: number of arguments
96  * @argv: array of arguments (@argc elements)
97  *
98  * All plugins must export this symbol which is called when the plugin
99  * is first loaded. Calling qemu_plugin_uninstall() from this function
100  * is a bug.
101  *
102  * Note: @info is only live during the call. Copy any information we
103  * want to keep. @argv remains valid throughout the lifetime of the
104  * loaded plugin.
105  *
106  * Return: 0 on successful loading, !0 for an error.
107  */
108 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
109                                            const qemu_info_t *info,
110                                            int argc, char **argv);
111 
112 /**
113  * typedef qemu_plugin_simple_cb_t - simple callback
114  * @id: the unique qemu_plugin_id_t
115  *
116  * This callback passes no information aside from the unique @id.
117  */
118 typedef void (*qemu_plugin_simple_cb_t)(qemu_plugin_id_t id);
119 
120 /**
121  * typedef qemu_plugin_udata_cb_t - callback with user data
122  * @id: the unique qemu_plugin_id_t
123  * @userdata: a pointer to some user data supplied when the callback
124  * was registered.
125  */
126 typedef void (*qemu_plugin_udata_cb_t)(qemu_plugin_id_t id, void *userdata);
127 
128 /**
129  * typedef qemu_plugin_vcpu_simple_cb_t - vcpu callback
130  * @id: the unique qemu_plugin_id_t
131  * @vcpu_index: the current vcpu context
132  */
133 typedef void (*qemu_plugin_vcpu_simple_cb_t)(qemu_plugin_id_t id,
134                                              unsigned int vcpu_index);
135 
136 /**
137  * typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback
138  * @vcpu_index: the current vcpu context
139  * @userdata: a pointer to some user data supplied when the callback
140  * was registered.
141  */
142 typedef void (*qemu_plugin_vcpu_udata_cb_t)(unsigned int vcpu_index,
143                                             void *userdata);
144 
145 /**
146  * qemu_plugin_uninstall() - Uninstall a plugin
147  * @id: this plugin's opaque ID
148  * @cb: callback to be called once the plugin has been removed
149  *
150  * Do NOT assume that the plugin has been uninstalled once this function
151  * returns. Plugins are uninstalled asynchronously, and therefore the given
152  * plugin receives callbacks until @cb is called.
153  *
154  * Note: Calling this function from qemu_plugin_install() is a bug.
155  */
156 QEMU_PLUGIN_API
157 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb);
158 
159 /**
160  * qemu_plugin_reset() - Reset a plugin
161  * @id: this plugin's opaque ID
162  * @cb: callback to be called once the plugin has been reset
163  *
164  * Unregisters all callbacks for the plugin given by @id.
165  *
166  * Do NOT assume that the plugin has been reset once this function returns.
167  * Plugins are reset asynchronously, and therefore the given plugin receives
168  * callbacks until @cb is called.
169  */
170 QEMU_PLUGIN_API
171 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb);
172 
173 /**
174  * qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback
175  * @id: plugin ID
176  * @cb: callback function
177  *
178  * The @cb function is called every time a vCPU is initialized.
179  *
180  * See also: qemu_plugin_register_vcpu_exit_cb()
181  */
182 QEMU_PLUGIN_API
183 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
184                                        qemu_plugin_vcpu_simple_cb_t cb);
185 
186 /**
187  * qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback
188  * @id: plugin ID
189  * @cb: callback function
190  *
191  * The @cb function is called every time a vCPU exits.
192  *
193  * See also: qemu_plugin_register_vcpu_init_cb()
194  */
195 QEMU_PLUGIN_API
196 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
197                                        qemu_plugin_vcpu_simple_cb_t cb);
198 
199 /**
200  * qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback
201  * @id: plugin ID
202  * @cb: callback function
203  *
204  * The @cb function is called every time a vCPU idles.
205  */
206 QEMU_PLUGIN_API
207 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
208                                        qemu_plugin_vcpu_simple_cb_t cb);
209 
210 /**
211  * qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback
212  * @id: plugin ID
213  * @cb: callback function
214  *
215  * The @cb function is called every time a vCPU resumes execution.
216  */
217 QEMU_PLUGIN_API
218 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
219                                          qemu_plugin_vcpu_simple_cb_t cb);
220 
221 /** struct qemu_plugin_tb - Opaque handle for a translation block */
222 struct qemu_plugin_tb;
223 /** struct qemu_plugin_insn - Opaque handle for a translated instruction */
224 struct qemu_plugin_insn;
225 /** struct qemu_plugin_scoreboard - Opaque handle for a scoreboard */
226 struct qemu_plugin_scoreboard;
227 
228 /**
229  * typedef qemu_plugin_u64 - uint64_t member of an entry in a scoreboard
230  *
231  * This field allows to access a specific uint64_t member in one given entry,
232  * located at a specified offset. Inline operations expect this as entry.
233  */
234 typedef struct {
235     struct qemu_plugin_scoreboard *score;
236     size_t offset;
237 } qemu_plugin_u64;
238 
239 /**
240  * enum qemu_plugin_cb_flags - type of callback
241  *
242  * @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs
243  * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs
244  * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs
245  *
246  * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change
247  * system register state.
248  */
249 enum qemu_plugin_cb_flags {
250     QEMU_PLUGIN_CB_NO_REGS,
251     QEMU_PLUGIN_CB_R_REGS,
252     QEMU_PLUGIN_CB_RW_REGS,
253 };
254 
255 enum qemu_plugin_mem_rw {
256     QEMU_PLUGIN_MEM_R = 1,
257     QEMU_PLUGIN_MEM_W,
258     QEMU_PLUGIN_MEM_RW,
259 };
260 
261 /**
262  * typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback
263  * @id: unique plugin id
264  * @tb: opaque handle used for querying and instrumenting a block.
265  */
266 typedef void (*qemu_plugin_vcpu_tb_trans_cb_t)(qemu_plugin_id_t id,
267                                                struct qemu_plugin_tb *tb);
268 
269 /**
270  * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb
271  * @id: plugin ID
272  * @cb: callback function
273  *
274  * The @cb function is called every time a translation occurs. The @cb
275  * function is passed an opaque qemu_plugin_type which it can query
276  * for additional information including the list of translated
277  * instructions. At this point the plugin can register further
278  * callbacks to be triggered when the block or individual instruction
279  * executes.
280  */
281 QEMU_PLUGIN_API
282 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
283                                            qemu_plugin_vcpu_tb_trans_cb_t cb);
284 
285 /**
286  * qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback
287  * @tb: the opaque qemu_plugin_tb handle for the translation
288  * @cb: callback function
289  * @flags: does the plugin read or write the CPU's registers?
290  * @userdata: any plugin data to pass to the @cb?
291  *
292  * The @cb function is called every time a translated unit executes.
293  */
294 QEMU_PLUGIN_API
295 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
296                                           qemu_plugin_vcpu_udata_cb_t cb,
297                                           enum qemu_plugin_cb_flags flags,
298                                           void *userdata);
299 
300 /**
301  * enum qemu_plugin_op - describes an inline op
302  *
303  * @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t
304  *
305  * Note: currently only a single inline op is supported.
306  */
307 
308 enum qemu_plugin_op {
309     QEMU_PLUGIN_INLINE_ADD_U64,
310 };
311 
312 /**
313  * qemu_plugin_register_vcpu_tb_exec_inline() - execution inline op
314  * @tb: the opaque qemu_plugin_tb handle for the translation
315  * @op: the type of qemu_plugin_op (e.g. ADD_U64)
316  * @ptr: the target memory location for the op
317  * @imm: the op data (e.g. 1)
318  *
319  * Insert an inline op to every time a translated unit executes.
320  * Useful if you just want to increment a single counter somewhere in
321  * memory.
322  *
323  * Note: ops are not atomic so in multi-threaded/multi-smp situations
324  * you will get inexact results.
325  */
326 QEMU_PLUGIN_API
327 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
328                                               enum qemu_plugin_op op,
329                                               void *ptr, uint64_t imm);
330 
331 /**
332  * qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu() - execution inline op
333  * @tb: the opaque qemu_plugin_tb handle for the translation
334  * @op: the type of qemu_plugin_op (e.g. ADD_U64)
335  * @entry: entry to run op
336  * @imm: the op data (e.g. 1)
337  *
338  * Insert an inline op on a given scoreboard entry.
339  */
340 QEMU_PLUGIN_API
341 void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
342     struct qemu_plugin_tb *tb,
343     enum qemu_plugin_op op,
344     qemu_plugin_u64 entry,
345     uint64_t imm);
346 
347 /**
348  * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb
349  * @insn: the opaque qemu_plugin_insn handle for an instruction
350  * @cb: callback function
351  * @flags: does the plugin read or write the CPU's registers?
352  * @userdata: any plugin data to pass to the @cb?
353  *
354  * The @cb function is called every time an instruction is executed
355  */
356 QEMU_PLUGIN_API
357 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
358                                             qemu_plugin_vcpu_udata_cb_t cb,
359                                             enum qemu_plugin_cb_flags flags,
360                                             void *userdata);
361 
362 /**
363  * qemu_plugin_register_vcpu_insn_exec_inline() - insn execution inline op
364  * @insn: the opaque qemu_plugin_insn handle for an instruction
365  * @op: the type of qemu_plugin_op (e.g. ADD_U64)
366  * @ptr: the target memory location for the op
367  * @imm: the op data (e.g. 1)
368  *
369  * Insert an inline op to every time an instruction executes. Useful
370  * if you just want to increment a single counter somewhere in memory.
371  */
372 QEMU_PLUGIN_API
373 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
374                                                 enum qemu_plugin_op op,
375                                                 void *ptr, uint64_t imm);
376 
377 /**
378  * qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu() - insn exec inline op
379  * @insn: the opaque qemu_plugin_insn handle for an instruction
380  * @op: the type of qemu_plugin_op (e.g. ADD_U64)
381  * @entry: entry to run op
382  * @imm: the op data (e.g. 1)
383  *
384  * Insert an inline op to every time an instruction executes.
385  */
386 QEMU_PLUGIN_API
387 void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
388     struct qemu_plugin_insn *insn,
389     enum qemu_plugin_op op,
390     qemu_plugin_u64 entry,
391     uint64_t imm);
392 
393 /**
394  * qemu_plugin_tb_n_insns() - query helper for number of insns in TB
395  * @tb: opaque handle to TB passed to callback
396  *
397  * Returns: number of instructions in this block
398  */
399 QEMU_PLUGIN_API
400 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb);
401 
402 /**
403  * qemu_plugin_tb_vaddr() - query helper for vaddr of TB start
404  * @tb: opaque handle to TB passed to callback
405  *
406  * Returns: virtual address of block start
407  */
408 QEMU_PLUGIN_API
409 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb);
410 
411 /**
412  * qemu_plugin_tb_get_insn() - retrieve handle for instruction
413  * @tb: opaque handle to TB passed to callback
414  * @idx: instruction number, 0 indexed
415  *
416  * The returned handle can be used in follow up helper queries as well
417  * as when instrumenting an instruction. It is only valid for the
418  * lifetime of the callback.
419  *
420  * Returns: opaque handle to instruction
421  */
422 QEMU_PLUGIN_API
423 struct qemu_plugin_insn *
424 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx);
425 
426 /**
427  * qemu_plugin_insn_data() - return ptr to instruction data
428  * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
429  *
430  * Note: data is only valid for duration of callback. See
431  * qemu_plugin_insn_size() to calculate size of stream.
432  *
433  * Returns: pointer to a stream of bytes containing the value of this
434  * instructions opcode.
435  */
436 QEMU_PLUGIN_API
437 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn);
438 
439 /**
440  * qemu_plugin_insn_size() - return size of instruction
441  * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
442  *
443  * Returns: size of instruction in bytes
444  */
445 QEMU_PLUGIN_API
446 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn);
447 
448 /**
449  * qemu_plugin_insn_vaddr() - return vaddr of instruction
450  * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
451  *
452  * Returns: virtual address of instruction
453  */
454 QEMU_PLUGIN_API
455 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn);
456 
457 /**
458  * qemu_plugin_insn_haddr() - return hardware addr of instruction
459  * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
460  *
461  * Returns: hardware (physical) target address of instruction
462  */
463 QEMU_PLUGIN_API
464 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn);
465 
466 /**
467  * typedef qemu_plugin_meminfo_t - opaque memory transaction handle
468  *
469  * This can be further queried using the qemu_plugin_mem_* query
470  * functions.
471  */
472 typedef uint32_t qemu_plugin_meminfo_t;
473 /** struct qemu_plugin_hwaddr - opaque hw address handle */
474 struct qemu_plugin_hwaddr;
475 
476 /**
477  * qemu_plugin_mem_size_shift() - get size of access
478  * @info: opaque memory transaction handle
479  *
480  * Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...)
481  */
482 QEMU_PLUGIN_API
483 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info);
484 /**
485  * qemu_plugin_mem_is_sign_extended() - was the access sign extended
486  * @info: opaque memory transaction handle
487  *
488  * Returns: true if it was, otherwise false
489  */
490 QEMU_PLUGIN_API
491 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info);
492 /**
493  * qemu_plugin_mem_is_big_endian() - was the access big endian
494  * @info: opaque memory transaction handle
495  *
496  * Returns: true if it was, otherwise false
497  */
498 QEMU_PLUGIN_API
499 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info);
500 /**
501  * qemu_plugin_mem_is_store() - was the access a store
502  * @info: opaque memory transaction handle
503  *
504  * Returns: true if it was, otherwise false
505  */
506 QEMU_PLUGIN_API
507 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info);
508 
509 /**
510  * qemu_plugin_get_hwaddr() - return handle for memory operation
511  * @info: opaque memory info structure
512  * @vaddr: the virtual address of the memory operation
513  *
514  * For system emulation returns a qemu_plugin_hwaddr handle to query
515  * details about the actual physical address backing the virtual
516  * address. For linux-user guests it just returns NULL.
517  *
518  * This handle is *only* valid for the duration of the callback. Any
519  * information about the handle should be recovered before the
520  * callback returns.
521  */
522 QEMU_PLUGIN_API
523 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
524                                                   uint64_t vaddr);
525 
526 /*
527  * The following additional queries can be run on the hwaddr structure to
528  * return information about it - namely whether it is for an IO access and the
529  * physical address associated with the access.
530  */
531 
532 /**
533  * qemu_plugin_hwaddr_is_io() - query whether memory operation is IO
534  * @haddr: address handle from qemu_plugin_get_hwaddr()
535  *
536  * Returns true if the handle's memory operation is to memory-mapped IO, or
537  * false if it is to RAM
538  */
539 QEMU_PLUGIN_API
540 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr);
541 
542 /**
543  * qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation
544  * @haddr: address handle from qemu_plugin_get_hwaddr()
545  *
546  * Returns the physical address associated with the memory operation
547  *
548  * Note that the returned physical address may not be unique if you are dealing
549  * with multiple address spaces.
550  */
551 QEMU_PLUGIN_API
552 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr);
553 
554 /*
555  * Returns a string representing the device. The string is valid for
556  * the lifetime of the plugin.
557  */
558 QEMU_PLUGIN_API
559 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h);
560 
561 /**
562  * typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type
563  * @vcpu_index: the executing vCPU
564  * @info: an opaque handle for further queries about the memory
565  * @vaddr: the virtual address of the transaction
566  * @userdata: any user data attached to the callback
567  */
568 typedef void (*qemu_plugin_vcpu_mem_cb_t) (unsigned int vcpu_index,
569                                            qemu_plugin_meminfo_t info,
570                                            uint64_t vaddr,
571                                            void *userdata);
572 
573 /**
574  * qemu_plugin_register_vcpu_mem_cb() - register memory access callback
575  * @insn: handle for instruction to instrument
576  * @cb: callback of type qemu_plugin_vcpu_mem_cb_t
577  * @flags: (currently unused) callback flags
578  * @rw: monitor reads, writes or both
579  * @userdata: opaque pointer for userdata
580  *
581  * This registers a full callback for every memory access generated by
582  * an instruction. If the instruction doesn't access memory no
583  * callback will be made.
584  *
585  * The callback reports the vCPU the access took place on, the virtual
586  * address of the access and a handle for further queries. The user
587  * can attach some userdata to the callback for additional purposes.
588  *
589  * Other execution threads will continue to execute during the
590  * callback so the plugin is responsible for ensuring it doesn't get
591  * confused by making appropriate use of locking if required.
592  */
593 QEMU_PLUGIN_API
594 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
595                                       qemu_plugin_vcpu_mem_cb_t cb,
596                                       enum qemu_plugin_cb_flags flags,
597                                       enum qemu_plugin_mem_rw rw,
598                                       void *userdata);
599 
600 /**
601  * qemu_plugin_register_vcpu_mem_inline() - register an inline op to any memory access
602  * @insn: handle for instruction to instrument
603  * @rw: apply to reads, writes or both
604  * @op: the op, of type qemu_plugin_op
605  * @ptr: pointer memory for the op
606  * @imm: immediate data for @op
607  *
608  * This registers a inline op every memory access generated by the
609  * instruction. This provides for a lightweight but not thread-safe
610  * way of counting the number of operations done.
611  */
612 QEMU_PLUGIN_API
613 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
614                                           enum qemu_plugin_mem_rw rw,
615                                           enum qemu_plugin_op op, void *ptr,
616                                           uint64_t imm);
617 
618 /**
619  * qemu_plugin_register_vcpu_mem_inline_per_vcpu() - inline op for mem access
620  * @insn: handle for instruction to instrument
621  * @rw: apply to reads, writes or both
622  * @op: the op, of type qemu_plugin_op
623  * @entry: entry to run op
624  * @imm: immediate data for @op
625  *
626  * This registers a inline op every memory access generated by the
627  * instruction.
628  */
629 QEMU_PLUGIN_API
630 void qemu_plugin_register_vcpu_mem_inline_per_vcpu(
631     struct qemu_plugin_insn *insn,
632     enum qemu_plugin_mem_rw rw,
633     enum qemu_plugin_op op,
634     qemu_plugin_u64 entry,
635     uint64_t imm);
636 
637 typedef void
638 (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index,
639                                  int64_t num, uint64_t a1, uint64_t a2,
640                                  uint64_t a3, uint64_t a4, uint64_t a5,
641                                  uint64_t a6, uint64_t a7, uint64_t a8);
642 
643 QEMU_PLUGIN_API
644 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
645                                           qemu_plugin_vcpu_syscall_cb_t cb);
646 
647 typedef void
648 (*qemu_plugin_vcpu_syscall_ret_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_idx,
649                                      int64_t num, int64_t ret);
650 
651 QEMU_PLUGIN_API
652 void
653 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
654                                          qemu_plugin_vcpu_syscall_ret_cb_t cb);
655 
656 
657 /**
658  * qemu_plugin_insn_disas() - return disassembly string for instruction
659  * @insn: instruction reference
660  *
661  * Returns an allocated string containing the disassembly
662  */
663 
664 QEMU_PLUGIN_API
665 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn);
666 
667 /**
668  * qemu_plugin_insn_symbol() - best effort symbol lookup
669  * @insn: instruction reference
670  *
671  * Return a static string referring to the symbol. This is dependent
672  * on the binary QEMU is running having provided a symbol table.
673  */
674 QEMU_PLUGIN_API
675 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn);
676 
677 /**
678  * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU
679  * @id: plugin ID
680  * @cb: callback function
681  *
682  * The @cb function is called once for each existing vCPU.
683  *
684  * See also: qemu_plugin_register_vcpu_init_cb()
685  */
686 QEMU_PLUGIN_API
687 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
688                                qemu_plugin_vcpu_simple_cb_t cb);
689 
690 QEMU_PLUGIN_API
691 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
692                                    qemu_plugin_simple_cb_t cb);
693 
694 /**
695  * qemu_plugin_register_atexit_cb() - register exit callback
696  * @id: plugin ID
697  * @cb: callback
698  * @userdata: user data for callback
699  *
700  * The @cb function is called once execution has finished. Plugins
701  * should be able to free all their resources at this point much like
702  * after a reset/uninstall callback is called.
703  *
704  * In user-mode it is possible a few un-instrumented instructions from
705  * child threads may run before the host kernel reaps the threads.
706  */
707 QEMU_PLUGIN_API
708 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
709                                     qemu_plugin_udata_cb_t cb, void *userdata);
710 
711 /* returns how many vcpus were started at this point */
712 int qemu_plugin_num_vcpus(void);
713 
714 /**
715  * qemu_plugin_outs() - output string via QEMU's logging system
716  * @string: a string
717  */
718 QEMU_PLUGIN_API
719 void qemu_plugin_outs(const char *string);
720 
721 /**
722  * qemu_plugin_bool_parse() - parses a boolean argument in the form of
723  * "<argname>=[on|yes|true|off|no|false]"
724  *
725  * @name: argument name, the part before the equals sign
726  * @val: argument value, what's after the equals sign
727  * @ret: output return value
728  *
729  * returns true if the combination @name=@val parses correctly to a boolean
730  * argument, and false otherwise
731  */
732 QEMU_PLUGIN_API
733 bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret);
734 
735 /**
736  * qemu_plugin_path_to_binary() - path to binary file being executed
737  *
738  * Return a string representing the path to the binary. For user-mode
739  * this is the main executable. For system emulation we currently
740  * return NULL. The user should g_free() the string once no longer
741  * needed.
742  */
743 QEMU_PLUGIN_API
744 const char *qemu_plugin_path_to_binary(void);
745 
746 /**
747  * qemu_plugin_start_code() - returns start of text segment
748  *
749  * Returns the nominal start address of the main text segment in
750  * user-mode. Currently returns 0 for system emulation.
751  */
752 QEMU_PLUGIN_API
753 uint64_t qemu_plugin_start_code(void);
754 
755 /**
756  * qemu_plugin_end_code() - returns end of text segment
757  *
758  * Returns the nominal end address of the main text segment in
759  * user-mode. Currently returns 0 for system emulation.
760  */
761 QEMU_PLUGIN_API
762 uint64_t qemu_plugin_end_code(void);
763 
764 /**
765  * qemu_plugin_entry_code() - returns start address for module
766  *
767  * Returns the nominal entry address of the main text segment in
768  * user-mode. Currently returns 0 for system emulation.
769  */
770 QEMU_PLUGIN_API
771 uint64_t qemu_plugin_entry_code(void);
772 
773 /** struct qemu_plugin_register - Opaque handle for register access */
774 struct qemu_plugin_register;
775 
776 /**
777  * typedef qemu_plugin_reg_descriptor - register descriptions
778  *
779  * @handle: opaque handle for retrieving value with qemu_plugin_read_register
780  * @name: register name
781  * @feature: optional feature descriptor, can be NULL
782  */
783 typedef struct {
784     struct qemu_plugin_register *handle;
785     const char *name;
786     const char *feature;
787 } qemu_plugin_reg_descriptor;
788 
789 /**
790  * qemu_plugin_get_registers() - return register list for current vCPU
791  *
792  * Returns a potentially empty GArray of qemu_plugin_reg_descriptor.
793  * Caller frees the array (but not the const strings).
794  *
795  * Should be used from a qemu_plugin_register_vcpu_init_cb() callback
796  * after the vCPU is initialised, i.e. in the vCPU context.
797  */
798 QEMU_PLUGIN_API
799 GArray *qemu_plugin_get_registers(void);
800 
801 /**
802  * qemu_plugin_read_register() - read register for current vCPU
803  *
804  * @handle: a @qemu_plugin_reg_handle handle
805  * @buf: A GByteArray for the data owned by the plugin
806  *
807  * This function is only available in a context that register read access is
808  * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag.
809  *
810  * Returns the size of the read register. The content of @buf is in target byte
811  * order. On failure returns -1.
812  */
813 QEMU_PLUGIN_API
814 int qemu_plugin_read_register(struct qemu_plugin_register *handle,
815                               GByteArray *buf);
816 
817 /**
818  * qemu_plugin_scoreboard_new() - alloc a new scoreboard
819  *
820  * @element_size: size (in bytes) for one entry
821  *
822  * Returns a pointer to a new scoreboard. It must be freed using
823  * qemu_plugin_scoreboard_free.
824  */
825 QEMU_PLUGIN_API
826 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size);
827 
828 /**
829  * qemu_plugin_scoreboard_free() - free a scoreboard
830  * @score: scoreboard to free
831  */
832 QEMU_PLUGIN_API
833 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score);
834 
835 /**
836  * qemu_plugin_scoreboard_find() - get pointer to an entry of a scoreboard
837  * @score: scoreboard to query
838  * @vcpu_index: entry index
839  *
840  * Returns address of entry of a scoreboard matching a given vcpu_index. This
841  * address can be modified later if scoreboard is resized.
842  */
843 QEMU_PLUGIN_API
844 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score,
845                                   unsigned int vcpu_index);
846 
847 /* Macros to define a qemu_plugin_u64 */
848 #define qemu_plugin_scoreboard_u64(score) \
849     (qemu_plugin_u64) {score, 0}
850 #define qemu_plugin_scoreboard_u64_in_struct(score, type, member) \
851     (qemu_plugin_u64) {score, offsetof(type, member)}
852 
853 /**
854  * qemu_plugin_u64_add() - add a value to a qemu_plugin_u64 for a given vcpu
855  * @entry: entry to query
856  * @vcpu_index: entry index
857  * @added: value to add
858  */
859 QEMU_PLUGIN_API
860 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index,
861                          uint64_t added);
862 
863 /**
864  * qemu_plugin_u64_get() - get value of a qemu_plugin_u64 for a given vcpu
865  * @entry: entry to query
866  * @vcpu_index: entry index
867  */
868 QEMU_PLUGIN_API
869 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry, unsigned int vcpu_index);
870 
871 /**
872  * qemu_plugin_u64_set() - set value of a qemu_plugin_u64 for a given vcpu
873  * @entry: entry to query
874  * @vcpu_index: entry index
875  * @val: new value
876  */
877 QEMU_PLUGIN_API
878 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
879                          uint64_t val);
880 
881 /**
882  * qemu_plugin_u64_sum() - return sum of all vcpu entries in a scoreboard
883  * @entry: entry to sum
884  */
885 QEMU_PLUGIN_API
886 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry);
887 
888 #endif /* QEMU_QEMU_PLUGIN_H */
889