]>
Commit | Line | Data |
---|---|---|
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 |
43 | typedef 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 | ||
62 | extern 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 | */ | |
74 | typedef 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 |
112 | QEMU_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 | */ |
122 | typedef 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 |
130 | typedef 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 |
137 | typedef 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 |
146 | typedef 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 | 160 | QEMU_PLUGIN_API |
975c4553 EC |
161 | void 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 | 174 | QEMU_PLUGIN_API |
975c4553 EC |
175 | void 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 | 186 | QEMU_PLUGIN_API |
975c4553 EC |
187 | void 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 | 199 | QEMU_PLUGIN_API |
975c4553 EC |
200 | void 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 | 210 | QEMU_PLUGIN_API |
975c4553 EC |
211 | void 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 | 221 | QEMU_PLUGIN_API |
975c4553 EC |
222 | void 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 | 226 | struct qemu_plugin_tb; |
83b9c2bf | 227 | /** struct qemu_plugin_insn - Opaque handle for a translated instruction */ |
975c4553 | 228 | struct qemu_plugin_insn; |
a3c2cf0b PB |
229 | /** struct qemu_plugin_scoreboard - Opaque handle for a scoreboard */ |
230 | struct 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 | */ | |
238 | typedef 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 | 253 | enum 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 | ||
259 | enum qemu_plugin_mem_rw { | |
260 | QEMU_PLUGIN_MEM_R = 1, | |
261 | QEMU_PLUGIN_MEM_W, | |
262 | QEMU_PLUGIN_MEM_RW, | |
263 | }; | |
264 | ||
83b9c2bf AB |
265 | /** |
266 | * typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback | |
267 | * @id: unique plugin id | |
268 | * @tb: opaque handle used for querying and instrumenting a block. | |
269 | */ | |
270 | typedef void (*qemu_plugin_vcpu_tb_trans_cb_t)(qemu_plugin_id_t id, | |
271 | struct qemu_plugin_tb *tb); | |
272 | ||
975c4553 EC |
273 | /** |
274 | * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb | |
275 | * @id: plugin ID | |
276 | * @cb: callback function | |
277 | * | |
278 | * The @cb function is called every time a translation occurs. The @cb | |
279 | * function is passed an opaque qemu_plugin_type which it can query | |
280 | * for additional information including the list of translated | |
281 | * instructions. At this point the plugin can register further | |
282 | * callbacks to be triggered when the block or individual instruction | |
283 | * executes. | |
284 | */ | |
fb691b8c | 285 | QEMU_PLUGIN_API |
975c4553 EC |
286 | void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id, |
287 | qemu_plugin_vcpu_tb_trans_cb_t cb); | |
288 | ||
289 | /** | |
38c4101d | 290 | * qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback |
975c4553 EC |
291 | * @tb: the opaque qemu_plugin_tb handle for the translation |
292 | * @cb: callback function | |
293 | * @flags: does the plugin read or write the CPU's registers? | |
294 | * @userdata: any plugin data to pass to the @cb? | |
295 | * | |
296 | * The @cb function is called every time a translated unit executes. | |
297 | */ | |
fb691b8c | 298 | QEMU_PLUGIN_API |
975c4553 EC |
299 | void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb, |
300 | qemu_plugin_vcpu_udata_cb_t cb, | |
301 | enum qemu_plugin_cb_flags flags, | |
302 | void *userdata); | |
303 | ||
8bc9a4d4 AB |
304 | /** |
305 | * enum qemu_plugin_op - describes an inline op | |
306 | * | |
307 | * @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t | |
308 | * | |
309 | * Note: currently only a single inline op is supported. | |
310 | */ | |
311 | ||
975c4553 EC |
312 | enum qemu_plugin_op { |
313 | QEMU_PLUGIN_INLINE_ADD_U64, | |
314 | }; | |
315 | ||
0bcebaba PB |
316 | /** |
317 | * qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu() - execution inline op | |
318 | * @tb: the opaque qemu_plugin_tb handle for the translation | |
319 | * @op: the type of qemu_plugin_op (e.g. ADD_U64) | |
320 | * @entry: entry to run op | |
321 | * @imm: the op data (e.g. 1) | |
322 | * | |
323 | * Insert an inline op on a given scoreboard entry. | |
324 | */ | |
325 | QEMU_PLUGIN_API | |
326 | void qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( | |
327 | struct qemu_plugin_tb *tb, | |
328 | enum qemu_plugin_op op, | |
329 | qemu_plugin_u64 entry, | |
330 | uint64_t imm); | |
331 | ||
975c4553 EC |
332 | /** |
333 | * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb | |
334 | * @insn: the opaque qemu_plugin_insn handle for an instruction | |
335 | * @cb: callback function | |
336 | * @flags: does the plugin read or write the CPU's registers? | |
337 | * @userdata: any plugin data to pass to the @cb? | |
338 | * | |
339 | * The @cb function is called every time an instruction is executed | |
340 | */ | |
fb691b8c | 341 | QEMU_PLUGIN_API |
975c4553 EC |
342 | void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn, |
343 | qemu_plugin_vcpu_udata_cb_t cb, | |
344 | enum qemu_plugin_cb_flags flags, | |
345 | void *userdata); | |
346 | ||
0bcebaba PB |
347 | /** |
348 | * qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu() - insn exec inline op | |
349 | * @insn: the opaque qemu_plugin_insn handle for an instruction | |
350 | * @op: the type of qemu_plugin_op (e.g. ADD_U64) | |
351 | * @entry: entry to run op | |
352 | * @imm: the op data (e.g. 1) | |
353 | * | |
354 | * Insert an inline op to every time an instruction executes. | |
355 | */ | |
356 | QEMU_PLUGIN_API | |
357 | void qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( | |
358 | struct qemu_plugin_insn *insn, | |
359 | enum qemu_plugin_op op, | |
360 | qemu_plugin_u64 entry, | |
361 | uint64_t imm); | |
362 | ||
8affbacb AB |
363 | /** |
364 | * qemu_plugin_tb_n_insns() - query helper for number of insns in TB | |
365 | * @tb: opaque handle to TB passed to callback | |
366 | * | |
367 | * Returns: number of instructions in this block | |
975c4553 | 368 | */ |
fb691b8c | 369 | QEMU_PLUGIN_API |
975c4553 EC |
370 | size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb); |
371 | ||
8affbacb AB |
372 | /** |
373 | * qemu_plugin_tb_vaddr() - query helper for vaddr of TB start | |
374 | * @tb: opaque handle to TB passed to callback | |
375 | * | |
376 | * Returns: virtual address of block start | |
377 | */ | |
fb691b8c | 378 | QEMU_PLUGIN_API |
975c4553 EC |
379 | uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb); |
380 | ||
8affbacb AB |
381 | /** |
382 | * qemu_plugin_tb_get_insn() - retrieve handle for instruction | |
383 | * @tb: opaque handle to TB passed to callback | |
384 | * @idx: instruction number, 0 indexed | |
385 | * | |
386 | * The returned handle can be used in follow up helper queries as well | |
387 | * as when instrumenting an instruction. It is only valid for the | |
388 | * lifetime of the callback. | |
389 | * | |
390 | * Returns: opaque handle to instruction | |
391 | */ | |
fb691b8c | 392 | QEMU_PLUGIN_API |
975c4553 EC |
393 | struct qemu_plugin_insn * |
394 | qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx); | |
395 | ||
8affbacb | 396 | /** |
4abc8923 | 397 | * qemu_plugin_insn_data() - copy instruction data |
8affbacb | 398 | * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() |
4abc8923 RH |
399 | * @dest: destination into which data is copied |
400 | * @len: length of dest | |
8affbacb | 401 | * |
4abc8923 | 402 | * Returns the number of bytes copied, minimum of @len and insn size. |
8affbacb | 403 | */ |
fb691b8c | 404 | QEMU_PLUGIN_API |
4abc8923 RH |
405 | size_t qemu_plugin_insn_data(const struct qemu_plugin_insn *insn, |
406 | void *dest, size_t len); | |
975c4553 | 407 | |
8affbacb AB |
408 | /** |
409 | * qemu_plugin_insn_size() - return size of instruction | |
410 | * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() | |
411 | * | |
412 | * Returns: size of instruction in bytes | |
413 | */ | |
fb691b8c | 414 | QEMU_PLUGIN_API |
975c4553 EC |
415 | size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn); |
416 | ||
8affbacb AB |
417 | /** |
418 | * qemu_plugin_insn_vaddr() - return vaddr of instruction | |
419 | * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() | |
420 | * | |
421 | * Returns: virtual address of instruction | |
422 | */ | |
fb691b8c | 423 | QEMU_PLUGIN_API |
975c4553 | 424 | uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn); |
8affbacb AB |
425 | |
426 | /** | |
427 | * qemu_plugin_insn_haddr() - return hardware addr of instruction | |
428 | * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() | |
429 | * | |
430 | * Returns: hardware (physical) target address of instruction | |
431 | */ | |
fb691b8c | 432 | QEMU_PLUGIN_API |
975c4553 EC |
433 | void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn); |
434 | ||
fc292a7e AB |
435 | /** |
436 | * typedef qemu_plugin_meminfo_t - opaque memory transaction handle | |
975c4553 | 437 | * |
fc292a7e AB |
438 | * This can be further queried using the qemu_plugin_mem_* query |
439 | * functions. | |
975c4553 EC |
440 | */ |
441 | typedef uint32_t qemu_plugin_meminfo_t; | |
fc292a7e | 442 | /** struct qemu_plugin_hwaddr - opaque hw address handle */ |
975c4553 EC |
443 | struct qemu_plugin_hwaddr; |
444 | ||
fc292a7e AB |
445 | /** |
446 | * qemu_plugin_mem_size_shift() - get size of access | |
447 | * @info: opaque memory transaction handle | |
448 | * | |
449 | * Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...) | |
450 | */ | |
fb691b8c | 451 | QEMU_PLUGIN_API |
975c4553 | 452 | unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info); |
fc292a7e AB |
453 | /** |
454 | * qemu_plugin_mem_is_sign_extended() - was the access sign extended | |
455 | * @info: opaque memory transaction handle | |
456 | * | |
457 | * Returns: true if it was, otherwise false | |
458 | */ | |
fb691b8c | 459 | QEMU_PLUGIN_API |
975c4553 | 460 | bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info); |
fc292a7e AB |
461 | /** |
462 | * qemu_plugin_mem_is_big_endian() - was the access big endian | |
463 | * @info: opaque memory transaction handle | |
464 | * | |
465 | * Returns: true if it was, otherwise false | |
466 | */ | |
fb691b8c | 467 | QEMU_PLUGIN_API |
975c4553 | 468 | bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info); |
fc292a7e AB |
469 | /** |
470 | * qemu_plugin_mem_is_store() - was the access a store | |
471 | * @info: opaque memory transaction handle | |
472 | * | |
473 | * Returns: true if it was, otherwise false | |
474 | */ | |
fb691b8c | 475 | QEMU_PLUGIN_API |
975c4553 EC |
476 | bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info); |
477 | ||
787148bf AL |
478 | /** |
479 | * qemu_plugin_get_hwaddr() - return handle for memory operation | |
fc292a7e | 480 | * @info: opaque memory info structure |
975c4553 EC |
481 | * @vaddr: the virtual address of the memory operation |
482 | * | |
483 | * For system emulation returns a qemu_plugin_hwaddr handle to query | |
484 | * details about the actual physical address backing the virtual | |
485 | * address. For linux-user guests it just returns NULL. | |
486 | * | |
487 | * This handle is *only* valid for the duration of the callback. Any | |
488 | * information about the handle should be recovered before the | |
489 | * callback returns. | |
490 | */ | |
fb691b8c | 491 | QEMU_PLUGIN_API |
975c4553 EC |
492 | struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info, |
493 | uint64_t vaddr); | |
494 | ||
235537fa | 495 | /* |
787148bf AL |
496 | * The following additional queries can be run on the hwaddr structure to |
497 | * return information about it - namely whether it is for an IO access and the | |
498 | * physical address associated with the access. | |
499 | */ | |
500 | ||
501 | /** | |
502 | * qemu_plugin_hwaddr_is_io() - query whether memory operation is IO | |
503 | * @haddr: address handle from qemu_plugin_get_hwaddr() | |
504 | * | |
505 | * Returns true if the handle's memory operation is to memory-mapped IO, or | |
506 | * false if it is to RAM | |
235537fa | 507 | */ |
fb691b8c | 508 | QEMU_PLUGIN_API |
308e7549 | 509 | bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr); |
787148bf AL |
510 | |
511 | /** | |
512 | * qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation | |
513 | * @haddr: address handle from qemu_plugin_get_hwaddr() | |
514 | * | |
515 | * Returns the physical address associated with the memory operation | |
516 | * | |
517 | * Note that the returned physical address may not be unique if you are dealing | |
518 | * with multiple address spaces. | |
519 | */ | |
fb691b8c | 520 | QEMU_PLUGIN_API |
787148bf | 521 | uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr); |
235537fa | 522 | |
b853a79f AB |
523 | /* |
524 | * Returns a string representing the device. The string is valid for | |
525 | * the lifetime of the plugin. | |
526 | */ | |
fb691b8c | 527 | QEMU_PLUGIN_API |
b853a79f AB |
528 | const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h); |
529 | ||
32ba75ad AB |
530 | /** |
531 | * typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type | |
532 | * @vcpu_index: the executing vCPU | |
533 | * @info: an opaque handle for further queries about the memory | |
534 | * @vaddr: the virtual address of the transaction | |
535 | * @userdata: any user data attached to the callback | |
536 | */ | |
537 | typedef void (*qemu_plugin_vcpu_mem_cb_t) (unsigned int vcpu_index, | |
538 | qemu_plugin_meminfo_t info, | |
539 | uint64_t vaddr, | |
540 | void *userdata); | |
975c4553 | 541 | |
32ba75ad AB |
542 | /** |
543 | * qemu_plugin_register_vcpu_mem_cb() - register memory access callback | |
544 | * @insn: handle for instruction to instrument | |
545 | * @cb: callback of type qemu_plugin_vcpu_mem_cb_t | |
546 | * @flags: (currently unused) callback flags | |
547 | * @rw: monitor reads, writes or both | |
548 | * @userdata: opaque pointer for userdata | |
549 | * | |
550 | * This registers a full callback for every memory access generated by | |
551 | * an instruction. If the instruction doesn't access memory no | |
552 | * callback will be made. | |
553 | * | |
554 | * The callback reports the vCPU the access took place on, the virtual | |
555 | * address of the access and a handle for further queries. The user | |
556 | * can attach some userdata to the callback for additional purposes. | |
557 | * | |
558 | * Other execution threads will continue to execute during the | |
559 | * callback so the plugin is responsible for ensuring it doesn't get | |
560 | * confused by making appropriate use of locking if required. | |
561 | */ | |
fb691b8c | 562 | QEMU_PLUGIN_API |
975c4553 EC |
563 | void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn, |
564 | qemu_plugin_vcpu_mem_cb_t cb, | |
565 | enum qemu_plugin_cb_flags flags, | |
566 | enum qemu_plugin_mem_rw rw, | |
567 | void *userdata); | |
568 | ||
0bcebaba PB |
569 | /** |
570 | * qemu_plugin_register_vcpu_mem_inline_per_vcpu() - inline op for mem access | |
571 | * @insn: handle for instruction to instrument | |
572 | * @rw: apply to reads, writes or both | |
573 | * @op: the op, of type qemu_plugin_op | |
574 | * @entry: entry to run op | |
575 | * @imm: immediate data for @op | |
576 | * | |
577 | * This registers a inline op every memory access generated by the | |
578 | * instruction. | |
579 | */ | |
580 | QEMU_PLUGIN_API | |
581 | void qemu_plugin_register_vcpu_mem_inline_per_vcpu( | |
582 | struct qemu_plugin_insn *insn, | |
583 | enum qemu_plugin_mem_rw rw, | |
584 | enum qemu_plugin_op op, | |
585 | qemu_plugin_u64 entry, | |
586 | uint64_t imm); | |
975c4553 EC |
587 | |
588 | typedef void | |
589 | (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index, | |
590 | int64_t num, uint64_t a1, uint64_t a2, | |
591 | uint64_t a3, uint64_t a4, uint64_t a5, | |
592 | uint64_t a6, uint64_t a7, uint64_t a8); | |
593 | ||
fb691b8c | 594 | QEMU_PLUGIN_API |
975c4553 EC |
595 | void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id, |
596 | qemu_plugin_vcpu_syscall_cb_t cb); | |
597 | ||
598 | typedef void | |
599 | (*qemu_plugin_vcpu_syscall_ret_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_idx, | |
600 | int64_t num, int64_t ret); | |
601 | ||
fb691b8c | 602 | QEMU_PLUGIN_API |
975c4553 EC |
603 | void |
604 | qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id, | |
605 | qemu_plugin_vcpu_syscall_ret_cb_t cb); | |
606 | ||
607 | ||
cbafa236 AB |
608 | /** |
609 | * qemu_plugin_insn_disas() - return disassembly string for instruction | |
610 | * @insn: instruction reference | |
611 | * | |
612 | * Returns an allocated string containing the disassembly | |
613 | */ | |
614 | ||
fb691b8c | 615 | QEMU_PLUGIN_API |
cbafa236 AB |
616 | char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn); |
617 | ||
7c4ab60f AB |
618 | /** |
619 | * qemu_plugin_insn_symbol() - best effort symbol lookup | |
620 | * @insn: instruction reference | |
621 | * | |
622 | * Return a static string referring to the symbol. This is dependent | |
623 | * on the binary QEMU is running having provided a symbol table. | |
624 | */ | |
fb691b8c | 625 | QEMU_PLUGIN_API |
7c4ab60f AB |
626 | const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn); |
627 | ||
975c4553 EC |
628 | /** |
629 | * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU | |
630 | * @id: plugin ID | |
631 | * @cb: callback function | |
632 | * | |
633 | * The @cb function is called once for each existing vCPU. | |
634 | * | |
635 | * See also: qemu_plugin_register_vcpu_init_cb() | |
636 | */ | |
fb691b8c | 637 | QEMU_PLUGIN_API |
975c4553 EC |
638 | void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id, |
639 | qemu_plugin_vcpu_simple_cb_t cb); | |
640 | ||
fb691b8c | 641 | QEMU_PLUGIN_API |
975c4553 EC |
642 | void qemu_plugin_register_flush_cb(qemu_plugin_id_t id, |
643 | qemu_plugin_simple_cb_t cb); | |
644 | ||
f7e68c9c AB |
645 | /** |
646 | * qemu_plugin_register_atexit_cb() - register exit callback | |
647 | * @id: plugin ID | |
648 | * @cb: callback | |
649 | * @userdata: user data for callback | |
650 | * | |
651 | * The @cb function is called once execution has finished. Plugins | |
652 | * should be able to free all their resources at this point much like | |
653 | * after a reset/uninstall callback is called. | |
654 | * | |
655 | * In user-mode it is possible a few un-instrumented instructions from | |
656 | * child threads may run before the host kernel reaps the threads. | |
657 | */ | |
fb691b8c | 658 | QEMU_PLUGIN_API |
975c4553 EC |
659 | void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id, |
660 | qemu_plugin_udata_cb_t cb, void *userdata); | |
661 | ||
4a448b14 PB |
662 | /* returns how many vcpus were started at this point */ |
663 | int qemu_plugin_num_vcpus(void); | |
664 | ||
ca76a669 AB |
665 | /** |
666 | * qemu_plugin_outs() - output string via QEMU's logging system | |
667 | * @string: a string | |
668 | */ | |
fb691b8c | 669 | QEMU_PLUGIN_API |
ca76a669 AB |
670 | void qemu_plugin_outs(const char *string); |
671 | ||
6a9e8a08 MM |
672 | /** |
673 | * qemu_plugin_bool_parse() - parses a boolean argument in the form of | |
674 | * "<argname>=[on|yes|true|off|no|false]" | |
675 | * | |
676 | * @name: argument name, the part before the equals sign | |
677 | * @val: argument value, what's after the equals sign | |
678 | * @ret: output return value | |
679 | * | |
680 | * returns true if the combination @name=@val parses correctly to a boolean | |
681 | * argument, and false otherwise | |
682 | */ | |
fb691b8c | 683 | QEMU_PLUGIN_API |
6a9e8a08 MM |
684 | bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret); |
685 | ||
91d40327 IA |
686 | /** |
687 | * qemu_plugin_path_to_binary() - path to binary file being executed | |
688 | * | |
689 | * Return a string representing the path to the binary. For user-mode | |
690 | * this is the main executable. For system emulation we currently | |
691 | * return NULL. The user should g_free() the string once no longer | |
692 | * needed. | |
693 | */ | |
fb691b8c | 694 | QEMU_PLUGIN_API |
91d40327 IA |
695 | const char *qemu_plugin_path_to_binary(void); |
696 | ||
697 | /** | |
698 | * qemu_plugin_start_code() - returns start of text segment | |
699 | * | |
700 | * Returns the nominal start address of the main text segment in | |
701 | * user-mode. Currently returns 0 for system emulation. | |
702 | */ | |
fb691b8c | 703 | QEMU_PLUGIN_API |
91d40327 IA |
704 | uint64_t qemu_plugin_start_code(void); |
705 | ||
706 | /** | |
707 | * qemu_plugin_end_code() - returns end of text segment | |
708 | * | |
709 | * Returns the nominal end address of the main text segment in | |
710 | * user-mode. Currently returns 0 for system emulation. | |
711 | */ | |
fb691b8c | 712 | QEMU_PLUGIN_API |
91d40327 IA |
713 | uint64_t qemu_plugin_end_code(void); |
714 | ||
715 | /** | |
716 | * qemu_plugin_entry_code() - returns start address for module | |
717 | * | |
718 | * Returns the nominal entry address of the main text segment in | |
719 | * user-mode. Currently returns 0 for system emulation. | |
720 | */ | |
fb691b8c | 721 | QEMU_PLUGIN_API |
91d40327 IA |
722 | uint64_t qemu_plugin_entry_code(void); |
723 | ||
8df5e27c AB |
724 | /** struct qemu_plugin_register - Opaque handle for register access */ |
725 | struct qemu_plugin_register; | |
726 | ||
727 | /** | |
728 | * typedef qemu_plugin_reg_descriptor - register descriptions | |
729 | * | |
730 | * @handle: opaque handle for retrieving value with qemu_plugin_read_register | |
731 | * @name: register name | |
732 | * @feature: optional feature descriptor, can be NULL | |
733 | */ | |
734 | typedef struct { | |
735 | struct qemu_plugin_register *handle; | |
736 | const char *name; | |
737 | const char *feature; | |
738 | } qemu_plugin_reg_descriptor; | |
739 | ||
740 | /** | |
741 | * qemu_plugin_get_registers() - return register list for current vCPU | |
742 | * | |
743 | * Returns a potentially empty GArray of qemu_plugin_reg_descriptor. | |
744 | * Caller frees the array (but not the const strings). | |
745 | * | |
746 | * Should be used from a qemu_plugin_register_vcpu_init_cb() callback | |
747 | * after the vCPU is initialised, i.e. in the vCPU context. | |
748 | */ | |
749 | QEMU_PLUGIN_API | |
750 | GArray *qemu_plugin_get_registers(void); | |
751 | ||
752 | /** | |
753 | * qemu_plugin_read_register() - read register for current vCPU | |
754 | * | |
755 | * @handle: a @qemu_plugin_reg_handle handle | |
756 | * @buf: A GByteArray for the data owned by the plugin | |
757 | * | |
758 | * This function is only available in a context that register read access is | |
759 | * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. | |
760 | * | |
761 | * Returns the size of the read register. The content of @buf is in target byte | |
762 | * order. On failure returns -1. | |
763 | */ | |
764 | QEMU_PLUGIN_API | |
765 | int qemu_plugin_read_register(struct qemu_plugin_register *handle, | |
766 | GByteArray *buf); | |
767 | ||
a3c2cf0b PB |
768 | /** |
769 | * qemu_plugin_scoreboard_new() - alloc a new scoreboard | |
770 | * | |
771 | * @element_size: size (in bytes) for one entry | |
772 | * | |
773 | * Returns a pointer to a new scoreboard. It must be freed using | |
774 | * qemu_plugin_scoreboard_free. | |
775 | */ | |
776 | QEMU_PLUGIN_API | |
777 | struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size); | |
778 | ||
779 | /** | |
780 | * qemu_plugin_scoreboard_free() - free a scoreboard | |
781 | * @score: scoreboard to free | |
782 | */ | |
783 | QEMU_PLUGIN_API | |
784 | void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score); | |
785 | ||
786 | /** | |
787 | * qemu_plugin_scoreboard_find() - get pointer to an entry of a scoreboard | |
788 | * @score: scoreboard to query | |
789 | * @vcpu_index: entry index | |
790 | * | |
791 | * Returns address of entry of a scoreboard matching a given vcpu_index. This | |
792 | * address can be modified later if scoreboard is resized. | |
793 | */ | |
794 | QEMU_PLUGIN_API | |
795 | void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score, | |
796 | unsigned int vcpu_index); | |
8df5e27c | 797 | |
8042e2ea PB |
798 | /* Macros to define a qemu_plugin_u64 */ |
799 | #define qemu_plugin_scoreboard_u64(score) \ | |
800 | (qemu_plugin_u64) {score, 0} | |
801 | #define qemu_plugin_scoreboard_u64_in_struct(score, type, member) \ | |
802 | (qemu_plugin_u64) {score, offsetof(type, member)} | |
803 | ||
804 | /** | |
805 | * qemu_plugin_u64_add() - add a value to a qemu_plugin_u64 for a given vcpu | |
806 | * @entry: entry to query | |
807 | * @vcpu_index: entry index | |
808 | * @added: value to add | |
809 | */ | |
810 | QEMU_PLUGIN_API | |
811 | void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index, | |
812 | uint64_t added); | |
813 | ||
814 | /** | |
815 | * qemu_plugin_u64_get() - get value of a qemu_plugin_u64 for a given vcpu | |
816 | * @entry: entry to query | |
817 | * @vcpu_index: entry index | |
818 | */ | |
819 | QEMU_PLUGIN_API | |
820 | uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry, unsigned int vcpu_index); | |
821 | ||
822 | /** | |
823 | * qemu_plugin_u64_set() - set value of a qemu_plugin_u64 for a given vcpu | |
824 | * @entry: entry to query | |
825 | * @vcpu_index: entry index | |
826 | * @val: new value | |
827 | */ | |
828 | QEMU_PLUGIN_API | |
829 | void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index, | |
830 | uint64_t val); | |
831 | ||
832 | /** | |
833 | * qemu_plugin_u64_sum() - return sum of all vcpu entries in a scoreboard | |
834 | * @entry: entry to sum | |
835 | */ | |
836 | QEMU_PLUGIN_API | |
837 | uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry); | |
838 | ||
52581c71 | 839 | #endif /* QEMU_QEMU_PLUGIN_H */ |