tests: Fixes test-io-channel-file by mask only owner file state mask bits
[qemu/ar7.git] / include / qemu / qemu-plugin.h
blobbab8b0d4b3af9572b90c45300a916ba16993304a
1 /*
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
9 */
10 #ifndef QEMU_PLUGIN_API_H
11 #define QEMU_PLUGIN_API_H
13 #include <inttypes.h>
14 #include <stdbool.h>
15 #include <stddef.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__
24 #ifdef BUILDING_DLL
25 #define QEMU_PLUGIN_EXPORT __declspec(dllexport)
26 #else
27 #define QEMU_PLUGIN_EXPORT __declspec(dllimport)
28 #endif
29 #define QEMU_PLUGIN_LOCAL
30 #else
31 #if __GNUC__ >= 4
32 #define QEMU_PLUGIN_EXPORT __attribute__((visibility("default")))
33 #define QEMU_PLUGIN_LOCAL __attribute__((visibility("hidden")))
34 #else
35 #define QEMU_PLUGIN_EXPORT
36 #define QEMU_PLUGIN_LOCAL
37 #endif
38 #endif
40 typedef uint64_t qemu_plugin_id_t;
43 * Versioning plugins:
45 * The plugin API will pass a minimum and current API version that
46 * QEMU currently supports. The minimum API will be incremented if an
47 * API needs to be deprecated.
49 * The plugins export the API they were built against by exposing the
50 * symbol qemu_plugin_version which can be checked.
53 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
55 #define QEMU_PLUGIN_VERSION 0
57 typedef struct {
58 /* string describing architecture */
59 const char *target_name;
60 struct {
61 int min;
62 int cur;
63 } version;
64 /* is this a full system emulation? */
65 bool system_emulation;
66 union {
68 * smp_vcpus may change if vCPUs can be hot-plugged, max_vcpus
69 * is the system-wide limit.
71 struct {
72 int smp_vcpus;
73 int max_vcpus;
74 } system;
76 } qemu_info_t;
78 /**
79 * qemu_plugin_install() - Install a plugin
80 * @id: this plugin's opaque ID
81 * @info: a block describing some details about the guest
82 * @argc: number of arguments
83 * @argv: array of arguments (@argc elements)
85 * All plugins must export this symbol.
87 * Note: Calling qemu_plugin_uninstall() from this function is a bug. To raise
88 * an error during install, return !0.
90 * Note: @info is only live during the call. Copy any information we
91 * want to keep.
93 * Note: @argv remains valid throughout the lifetime of the loaded plugin.
95 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
96 const qemu_info_t *info,
97 int argc, char **argv);
100 * Prototypes for the various callback styles we will be registering
101 * in the following functions.
103 typedef void (*qemu_plugin_simple_cb_t)(qemu_plugin_id_t id);
105 typedef void (*qemu_plugin_udata_cb_t)(qemu_plugin_id_t id, void *userdata);
107 typedef void (*qemu_plugin_vcpu_simple_cb_t)(qemu_plugin_id_t id,
108 unsigned int vcpu_index);
110 typedef void (*qemu_plugin_vcpu_udata_cb_t)(unsigned int vcpu_index,
111 void *userdata);
114 * qemu_plugin_uninstall() - Uninstall a plugin
115 * @id: this plugin's opaque ID
116 * @cb: callback to be called once the plugin has been removed
118 * Do NOT assume that the plugin has been uninstalled once this function
119 * returns. Plugins are uninstalled asynchronously, and therefore the given
120 * plugin receives callbacks until @cb is called.
122 * Note: Calling this function from qemu_plugin_install() is a bug.
124 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb);
127 * qemu_plugin_reset() - Reset a plugin
128 * @id: this plugin's opaque ID
129 * @cb: callback to be called once the plugin has been reset
131 * Unregisters all callbacks for the plugin given by @id.
133 * Do NOT assume that the plugin has been reset once this function returns.
134 * Plugins are reset asynchronously, and therefore the given plugin receives
135 * callbacks until @cb is called.
137 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb);
140 * qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback
141 * @id: plugin ID
142 * @cb: callback function
144 * The @cb function is called every time a vCPU is initialized.
146 * See also: qemu_plugin_register_vcpu_exit_cb()
148 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
149 qemu_plugin_vcpu_simple_cb_t cb);
152 * qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback
153 * @id: plugin ID
154 * @cb: callback function
156 * The @cb function is called every time a vCPU exits.
158 * See also: qemu_plugin_register_vcpu_init_cb()
160 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
161 qemu_plugin_vcpu_simple_cb_t cb);
164 * qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback
165 * @id: plugin ID
166 * @cb: callback function
168 * The @cb function is called every time a vCPU idles.
170 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
171 qemu_plugin_vcpu_simple_cb_t cb);
174 * qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback
175 * @id: plugin ID
176 * @cb: callback function
178 * The @cb function is called every time a vCPU resumes execution.
180 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
181 qemu_plugin_vcpu_simple_cb_t cb);
184 * Opaque types that the plugin is given during the translation and
185 * instrumentation phase.
187 struct qemu_plugin_tb;
188 struct qemu_plugin_insn;
190 enum qemu_plugin_cb_flags {
191 QEMU_PLUGIN_CB_NO_REGS, /* callback does not access the CPU's regs */
192 QEMU_PLUGIN_CB_R_REGS, /* callback reads the CPU's regs */
193 QEMU_PLUGIN_CB_RW_REGS, /* callback reads and writes the CPU's regs */
196 enum qemu_plugin_mem_rw {
197 QEMU_PLUGIN_MEM_R = 1,
198 QEMU_PLUGIN_MEM_W,
199 QEMU_PLUGIN_MEM_RW,
203 * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb
204 * @id: plugin ID
205 * @cb: callback function
207 * The @cb function is called every time a translation occurs. The @cb
208 * function is passed an opaque qemu_plugin_type which it can query
209 * for additional information including the list of translated
210 * instructions. At this point the plugin can register further
211 * callbacks to be triggered when the block or individual instruction
212 * executes.
214 typedef void (*qemu_plugin_vcpu_tb_trans_cb_t)(qemu_plugin_id_t id,
215 struct qemu_plugin_tb *tb);
217 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
218 qemu_plugin_vcpu_tb_trans_cb_t cb);
221 * qemu_plugin_register_vcpu_tb_trans_exec_cb() - register execution callback
222 * @tb: the opaque qemu_plugin_tb handle for the translation
223 * @cb: callback function
224 * @flags: does the plugin read or write the CPU's registers?
225 * @userdata: any plugin data to pass to the @cb?
227 * The @cb function is called every time a translated unit executes.
229 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
230 qemu_plugin_vcpu_udata_cb_t cb,
231 enum qemu_plugin_cb_flags flags,
232 void *userdata);
234 enum qemu_plugin_op {
235 QEMU_PLUGIN_INLINE_ADD_U64,
239 * qemu_plugin_register_vcpu_tb_trans_exec_inline() - execution inline op
240 * @tb: the opaque qemu_plugin_tb handle for the translation
241 * @op: the type of qemu_plugin_op (e.g. ADD_U64)
242 * @ptr: the target memory location for the op
243 * @imm: the op data (e.g. 1)
245 * Insert an inline op to every time a translated unit executes.
246 * Useful if you just want to increment a single counter somewhere in
247 * memory.
249 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
250 enum qemu_plugin_op op,
251 void *ptr, uint64_t imm);
254 * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb
255 * @insn: the opaque qemu_plugin_insn handle for an instruction
256 * @cb: callback function
257 * @flags: does the plugin read or write the CPU's registers?
258 * @userdata: any plugin data to pass to the @cb?
260 * The @cb function is called every time an instruction is executed
262 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
263 qemu_plugin_vcpu_udata_cb_t cb,
264 enum qemu_plugin_cb_flags flags,
265 void *userdata);
268 * qemu_plugin_register_vcpu_insn_exec_inline() - insn execution inline op
269 * @insn: the opaque qemu_plugin_insn handle for an instruction
270 * @cb: callback function
271 * @op: the type of qemu_plugin_op (e.g. ADD_U64)
272 * @ptr: the target memory location for the op
273 * @imm: the op data (e.g. 1)
275 * Insert an inline op to every time an instruction executes. Useful
276 * if you just want to increment a single counter somewhere in memory.
278 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
279 enum qemu_plugin_op op,
280 void *ptr, uint64_t imm);
283 * Helpers to query information about the instructions in a block
285 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb);
287 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb);
289 struct qemu_plugin_insn *
290 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx);
292 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn);
294 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn);
296 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn);
297 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn);
300 * Memory Instrumentation
302 * The anonymous qemu_plugin_meminfo_t and qemu_plugin_hwaddr types
303 * can be used in queries to QEMU to get more information about a
304 * given memory access.
306 typedef uint32_t qemu_plugin_meminfo_t;
307 struct qemu_plugin_hwaddr;
309 /* meminfo queries */
310 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info);
311 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info);
312 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info);
313 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info);
316 * qemu_plugin_get_hwaddr():
317 * @vaddr: the virtual address of the memory operation
319 * For system emulation returns a qemu_plugin_hwaddr handle to query
320 * details about the actual physical address backing the virtual
321 * address. For linux-user guests it just returns NULL.
323 * This handle is *only* valid for the duration of the callback. Any
324 * information about the handle should be recovered before the
325 * callback returns.
327 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
328 uint64_t vaddr);
331 * The following additional queries can be run on the hwaddr structure
332 * to return information about it. For non-IO accesses the device
333 * offset will be into the appropriate block of RAM.
335 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr);
336 uint64_t qemu_plugin_hwaddr_device_offset(const struct qemu_plugin_hwaddr *haddr);
338 typedef void
339 (*qemu_plugin_vcpu_mem_cb_t)(unsigned int vcpu_index,
340 qemu_plugin_meminfo_t info, uint64_t vaddr,
341 void *userdata);
343 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
344 qemu_plugin_vcpu_mem_cb_t cb,
345 enum qemu_plugin_cb_flags flags,
346 enum qemu_plugin_mem_rw rw,
347 void *userdata);
349 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
350 enum qemu_plugin_mem_rw rw,
351 enum qemu_plugin_op op, void *ptr,
352 uint64_t imm);
356 typedef void
357 (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index,
358 int64_t num, uint64_t a1, uint64_t a2,
359 uint64_t a3, uint64_t a4, uint64_t a5,
360 uint64_t a6, uint64_t a7, uint64_t a8);
362 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
363 qemu_plugin_vcpu_syscall_cb_t cb);
365 typedef void
366 (*qemu_plugin_vcpu_syscall_ret_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_idx,
367 int64_t num, int64_t ret);
369 void
370 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
371 qemu_plugin_vcpu_syscall_ret_cb_t cb);
375 * qemu_plugin_insn_disas() - return disassembly string for instruction
376 * @insn: instruction reference
378 * Returns an allocated string containing the disassembly
381 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn);
384 * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU
385 * @id: plugin ID
386 * @cb: callback function
388 * The @cb function is called once for each existing vCPU.
390 * See also: qemu_plugin_register_vcpu_init_cb()
392 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
393 qemu_plugin_vcpu_simple_cb_t cb);
395 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id,
396 qemu_plugin_simple_cb_t cb);
398 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id,
399 qemu_plugin_udata_cb_t cb, void *userdata);
401 /* returns -1 in user-mode */
402 int qemu_plugin_n_vcpus(void);
404 /* returns -1 in user-mode */
405 int qemu_plugin_n_max_vcpus(void);
408 * qemu_plugin_outs() - output string via QEMU's logging system
409 * @string: a string
411 void qemu_plugin_outs(const char *string);
413 #endif /* QEMU_PLUGIN_API_H */