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