2 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
3 * Copyright (C) 2019, Linaro
5 * License: GNU GPL, version 2 or later.
6 * See the COPYING file in the top-level directory.
8 * SPDX-License-Identifier: GPL-2.0-or-later
10 #ifndef QEMU_PLUGIN_API_H
11 #define QEMU_PLUGIN_API_H
18 * For best performance, build the plugin with -fvisibility=hidden so that
19 * QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with
20 * QEMU_PLUGIN_EXPORT. For more info, see
21 * https://gcc.gnu.org/wiki/Visibility
23 #if defined _WIN32 || defined __CYGWIN__
25 #define QEMU_PLUGIN_EXPORT __declspec(dllexport)
27 #define QEMU_PLUGIN_EXPORT __declspec(dllimport)
29 #define QEMU_PLUGIN_LOCAL
31 #define QEMU_PLUGIN_EXPORT __attribute__((visibility("default")))
32 #define QEMU_PLUGIN_LOCAL __attribute__((visibility("hidden")))
36 * typedef qemu_plugin_id_t - Unique plugin ID
38 typedef uint64_t qemu_plugin_id_t
;
43 * The plugin API will pass a minimum and current API version that
44 * QEMU currently supports. The minimum API will be incremented if an
45 * API needs to be deprecated.
47 * The plugins export the API they were built against by exposing the
48 * symbol qemu_plugin_version which can be checked.
51 extern QEMU_PLUGIN_EXPORT
int qemu_plugin_version
;
53 #define QEMU_PLUGIN_VERSION 1
56 * struct qemu_info_t - system information for plugins
58 * This structure provides for some limited information about the
59 * system to allow the plugin to make decisions on how to proceed. For
60 * example it might only be suitable for running on some guest
61 * architectures or when under full system emulation.
63 typedef struct qemu_info_t
{
64 /** @target_name: string describing architecture */
65 const char *target_name
;
66 /** @version: minimum and current plugin API level */
71 /** @system_emulation: is this a full system emulation? */
72 bool system_emulation
;
74 /** @system: information relevant to system emulation */
76 /** @system.smp_vcpus: initial number of vCPUs */
78 /** @system.max_vcpus: maximum possible number of vCPUs */
85 * qemu_plugin_install() - Install a plugin
86 * @id: this plugin's opaque ID
87 * @info: a block describing some details about the guest
88 * @argc: number of arguments
89 * @argv: array of arguments (@argc elements)
91 * All plugins must export this symbol which is called when the plugin
92 * is first loaded. Calling qemu_plugin_uninstall() from this function
95 * Note: @info is only live during the call. Copy any information we
96 * want to keep. @argv remains valid throughout the lifetime of the
99 * Return: 0 on successful loading, !0 for an error.
101 QEMU_PLUGIN_EXPORT
int qemu_plugin_install(qemu_plugin_id_t id
,
102 const qemu_info_t
*info
,
103 int argc
, char **argv
);
106 * typedef qemu_plugin_simple_cb_t - simple callback
107 * @id: the unique qemu_plugin_id_t
109 * This callback passes no information aside from the unique @id.
111 typedef void (*qemu_plugin_simple_cb_t
)(qemu_plugin_id_t id
);
114 * typedef qemu_plugin_udata_cb_t - callback with user data
115 * @id: the unique qemu_plugin_id_t
116 * @userdata: a pointer to some user data supplied when the callback
119 typedef void (*qemu_plugin_udata_cb_t
)(qemu_plugin_id_t id
, void *userdata
);
122 * typedef qemu_plugin_vcpu_simple_cb_t - vcpu callback
123 * @id: the unique qemu_plugin_id_t
124 * @vcpu_index: the current vcpu context
126 typedef void (*qemu_plugin_vcpu_simple_cb_t
)(qemu_plugin_id_t id
,
127 unsigned int vcpu_index
);
130 * typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback
131 * @vcpu_index: the current vcpu context
132 * @userdata: a pointer to some user data supplied when the callback
135 typedef void (*qemu_plugin_vcpu_udata_cb_t
)(unsigned int vcpu_index
,
139 * qemu_plugin_uninstall() - Uninstall a plugin
140 * @id: this plugin's opaque ID
141 * @cb: callback to be called once the plugin has been removed
143 * Do NOT assume that the plugin has been uninstalled once this function
144 * returns. Plugins are uninstalled asynchronously, and therefore the given
145 * plugin receives callbacks until @cb is called.
147 * Note: Calling this function from qemu_plugin_install() is a bug.
149 void qemu_plugin_uninstall(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
);
152 * qemu_plugin_reset() - Reset a plugin
153 * @id: this plugin's opaque ID
154 * @cb: callback to be called once the plugin has been reset
156 * Unregisters all callbacks for the plugin given by @id.
158 * Do NOT assume that the plugin has been reset once this function returns.
159 * Plugins are reset asynchronously, and therefore the given plugin receives
160 * callbacks until @cb is called.
162 void qemu_plugin_reset(qemu_plugin_id_t id
, qemu_plugin_simple_cb_t cb
);
165 * qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback
167 * @cb: callback function
169 * The @cb function is called every time a vCPU is initialized.
171 * See also: qemu_plugin_register_vcpu_exit_cb()
173 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id
,
174 qemu_plugin_vcpu_simple_cb_t cb
);
177 * qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback
179 * @cb: callback function
181 * The @cb function is called every time a vCPU exits.
183 * See also: qemu_plugin_register_vcpu_init_cb()
185 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id
,
186 qemu_plugin_vcpu_simple_cb_t cb
);
189 * qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback
191 * @cb: callback function
193 * The @cb function is called every time a vCPU idles.
195 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id
,
196 qemu_plugin_vcpu_simple_cb_t cb
);
199 * qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback
201 * @cb: callback function
203 * The @cb function is called every time a vCPU resumes execution.
205 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id
,
206 qemu_plugin_vcpu_simple_cb_t cb
);
208 /** struct qemu_plugin_tb - Opaque handle for a translation block */
209 struct qemu_plugin_tb
;
210 /** struct qemu_plugin_insn - Opaque handle for a translated instruction */
211 struct qemu_plugin_insn
;
214 * enum qemu_plugin_cb_flags - type of callback
216 * @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs
217 * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs
218 * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs
220 * Note: currently unused, plugins cannot read or change system
223 enum qemu_plugin_cb_flags
{
224 QEMU_PLUGIN_CB_NO_REGS
,
225 QEMU_PLUGIN_CB_R_REGS
,
226 QEMU_PLUGIN_CB_RW_REGS
,
229 enum qemu_plugin_mem_rw
{
230 QEMU_PLUGIN_MEM_R
= 1,
236 * typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback
237 * @id: unique plugin id
238 * @tb: opaque handle used for querying and instrumenting a block.
240 typedef void (*qemu_plugin_vcpu_tb_trans_cb_t
)(qemu_plugin_id_t id
,
241 struct qemu_plugin_tb
*tb
);
244 * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb
246 * @cb: callback function
248 * The @cb function is called every time a translation occurs. The @cb
249 * function is passed an opaque qemu_plugin_type which it can query
250 * for additional information including the list of translated
251 * instructions. At this point the plugin can register further
252 * callbacks to be triggered when the block or individual instruction
255 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id
,
256 qemu_plugin_vcpu_tb_trans_cb_t cb
);
259 * qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback
260 * @tb: the opaque qemu_plugin_tb handle for the translation
261 * @cb: callback function
262 * @flags: does the plugin read or write the CPU's registers?
263 * @userdata: any plugin data to pass to the @cb?
265 * The @cb function is called every time a translated unit executes.
267 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb
*tb
,
268 qemu_plugin_vcpu_udata_cb_t cb
,
269 enum qemu_plugin_cb_flags flags
,
273 * enum qemu_plugin_op - describes an inline op
275 * @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t
277 * Note: currently only a single inline op is supported.
280 enum qemu_plugin_op
{
281 QEMU_PLUGIN_INLINE_ADD_U64
,
285 * qemu_plugin_register_vcpu_tb_exec_inline() - execution inline op
286 * @tb: the opaque qemu_plugin_tb handle for the translation
287 * @op: the type of qemu_plugin_op (e.g. ADD_U64)
288 * @ptr: the target memory location for the op
289 * @imm: the op data (e.g. 1)
291 * Insert an inline op to every time a translated unit executes.
292 * Useful if you just want to increment a single counter somewhere in
295 * Note: ops are not atomic so in multi-threaded/multi-smp situations
296 * you will get inexact results.
298 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb
*tb
,
299 enum qemu_plugin_op op
,
300 void *ptr
, uint64_t imm
);
303 * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb
304 * @insn: the opaque qemu_plugin_insn handle for an instruction
305 * @cb: callback function
306 * @flags: does the plugin read or write the CPU's registers?
307 * @userdata: any plugin data to pass to the @cb?
309 * The @cb function is called every time an instruction is executed
311 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn
*insn
,
312 qemu_plugin_vcpu_udata_cb_t cb
,
313 enum qemu_plugin_cb_flags flags
,
317 * qemu_plugin_register_vcpu_insn_exec_inline() - insn execution inline op
318 * @insn: the opaque qemu_plugin_insn handle for an instruction
319 * @op: the type of qemu_plugin_op (e.g. ADD_U64)
320 * @ptr: the target memory location for the op
321 * @imm: the op data (e.g. 1)
323 * Insert an inline op to every time an instruction executes. Useful
324 * if you just want to increment a single counter somewhere in memory.
326 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn
*insn
,
327 enum qemu_plugin_op op
,
328 void *ptr
, uint64_t imm
);
331 * qemu_plugin_tb_n_insns() - query helper for number of insns in TB
332 * @tb: opaque handle to TB passed to callback
334 * Returns: number of instructions in this block
336 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb
*tb
);
339 * qemu_plugin_tb_vaddr() - query helper for vaddr of TB start
340 * @tb: opaque handle to TB passed to callback
342 * Returns: virtual address of block start
344 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb
*tb
);
347 * qemu_plugin_tb_get_insn() - retrieve handle for instruction
348 * @tb: opaque handle to TB passed to callback
349 * @idx: instruction number, 0 indexed
351 * The returned handle can be used in follow up helper queries as well
352 * as when instrumenting an instruction. It is only valid for the
353 * lifetime of the callback.
355 * Returns: opaque handle to instruction
357 struct qemu_plugin_insn
*
358 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb
*tb
, size_t idx
);
361 * qemu_plugin_insn_data() - return ptr to instruction data
362 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
364 * Note: data is only valid for duration of callback. See
365 * qemu_plugin_insn_size() to calculate size of stream.
367 * Returns: pointer to a stream of bytes containing the value of this
368 * instructions opcode.
370 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn
*insn
);
373 * qemu_plugin_insn_size() - return size of instruction
374 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
376 * Returns: size of instruction in bytes
378 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn
*insn
);
381 * qemu_plugin_insn_vaddr() - return vaddr of instruction
382 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
384 * Returns: virtual address of instruction
386 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn
*insn
);
389 * qemu_plugin_insn_haddr() - return hardware addr of instruction
390 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn()
392 * Returns: hardware (physical) target address of instruction
394 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn
*insn
);
397 * typedef qemu_plugin_meminfo_t - opaque memory transaction handle
399 * This can be further queried using the qemu_plugin_mem_* query
402 typedef uint32_t qemu_plugin_meminfo_t
;
403 /** struct qemu_plugin_hwaddr - opaque hw address handle */
404 struct qemu_plugin_hwaddr
;
407 * qemu_plugin_mem_size_shift() - get size of access
408 * @info: opaque memory transaction handle
410 * Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...)
412 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info
);
414 * qemu_plugin_mem_is_sign_extended() - was the access sign extended
415 * @info: opaque memory transaction handle
417 * Returns: true if it was, otherwise false
419 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info
);
421 * qemu_plugin_mem_is_big_endian() - was the access big endian
422 * @info: opaque memory transaction handle
424 * Returns: true if it was, otherwise false
426 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info
);
428 * qemu_plugin_mem_is_store() - was the access a store
429 * @info: opaque memory transaction handle
431 * Returns: true if it was, otherwise false
433 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info
);
436 * qemu_plugin_get_hwaddr() - return handle for memory operation
437 * @info: opaque memory info structure
438 * @vaddr: the virtual address of the memory operation
440 * For system emulation returns a qemu_plugin_hwaddr handle to query
441 * details about the actual physical address backing the virtual
442 * address. For linux-user guests it just returns NULL.
444 * This handle is *only* valid for the duration of the callback. Any
445 * information about the handle should be recovered before the
448 struct qemu_plugin_hwaddr
*qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info
,
452 * The following additional queries can be run on the hwaddr structure to
453 * return information about it - namely whether it is for an IO access and the
454 * physical address associated with the access.
458 * qemu_plugin_hwaddr_is_io() - query whether memory operation is IO
459 * @haddr: address handle from qemu_plugin_get_hwaddr()
461 * Returns true if the handle's memory operation is to memory-mapped IO, or
462 * false if it is to RAM
464 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr
*haddr
);
467 * qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation
468 * @haddr: address handle from qemu_plugin_get_hwaddr()
470 * Returns the physical address associated with the memory operation
472 * Note that the returned physical address may not be unique if you are dealing
473 * with multiple address spaces.
475 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr
*haddr
);
478 * Returns a string representing the device. The string is valid for
479 * the lifetime of the plugin.
481 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr
*h
);
484 (*qemu_plugin_vcpu_mem_cb_t
)(unsigned int vcpu_index
,
485 qemu_plugin_meminfo_t info
, uint64_t vaddr
,
488 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn
*insn
,
489 qemu_plugin_vcpu_mem_cb_t cb
,
490 enum qemu_plugin_cb_flags flags
,
491 enum qemu_plugin_mem_rw rw
,
494 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn
*insn
,
495 enum qemu_plugin_mem_rw rw
,
496 enum qemu_plugin_op op
, void *ptr
,
502 (*qemu_plugin_vcpu_syscall_cb_t
)(qemu_plugin_id_t id
, unsigned int vcpu_index
,
503 int64_t num
, uint64_t a1
, uint64_t a2
,
504 uint64_t a3
, uint64_t a4
, uint64_t a5
,
505 uint64_t a6
, uint64_t a7
, uint64_t a8
);
507 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id
,
508 qemu_plugin_vcpu_syscall_cb_t cb
);
511 (*qemu_plugin_vcpu_syscall_ret_cb_t
)(qemu_plugin_id_t id
, unsigned int vcpu_idx
,
512 int64_t num
, int64_t ret
);
515 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id
,
516 qemu_plugin_vcpu_syscall_ret_cb_t cb
);
520 * qemu_plugin_insn_disas() - return disassembly string for instruction
521 * @insn: instruction reference
523 * Returns an allocated string containing the disassembly
526 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn
*insn
);
529 * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU
531 * @cb: callback function
533 * The @cb function is called once for each existing vCPU.
535 * See also: qemu_plugin_register_vcpu_init_cb()
537 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id
,
538 qemu_plugin_vcpu_simple_cb_t cb
);
540 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id
,
541 qemu_plugin_simple_cb_t cb
);
543 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id
,
544 qemu_plugin_udata_cb_t cb
, void *userdata
);
546 /* returns -1 in user-mode */
547 int qemu_plugin_n_vcpus(void);
549 /* returns -1 in user-mode */
550 int qemu_plugin_n_max_vcpus(void);
553 * qemu_plugin_outs() - output string via QEMU's logging system
556 void qemu_plugin_outs(const char *string
);
558 #endif /* QEMU_PLUGIN_API_H */