1 /* 2 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org> 3 * Copyright (C) 2019, Linaro 4 * 5 * License: GNU GPL, version 2 or later. 6 * See the COPYING file in the top-level directory. 7 * 8 * SPDX-License-Identifier: GPL-2.0-or-later 9 */ 10 11 #ifndef QEMU_QEMU_PLUGIN_H 12 #define QEMU_QEMU_PLUGIN_H 13 14 #include <glib.h> 15 #include <inttypes.h> 16 #include <stdbool.h> 17 #include <stddef.h> 18 19 /* 20 * For best performance, build the plugin with -fvisibility=hidden so that 21 * QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with 22 * QEMU_PLUGIN_EXPORT. For more info, see 23 * https://gcc.gnu.org/wiki/Visibility 24 */ 25 #if defined _WIN32 || defined __CYGWIN__ 26 #ifdef CONFIG_PLUGIN 27 #define QEMU_PLUGIN_EXPORT __declspec(dllimport) 28 #define QEMU_PLUGIN_API __declspec(dllexport) 29 #else 30 #define QEMU_PLUGIN_EXPORT __declspec(dllexport) 31 #define QEMU_PLUGIN_API __declspec(dllimport) 32 #endif 33 #define QEMU_PLUGIN_LOCAL 34 #else 35 #define QEMU_PLUGIN_EXPORT __attribute__((visibility("default"))) 36 #define QEMU_PLUGIN_LOCAL __attribute__((visibility("hidden"))) 37 #define QEMU_PLUGIN_API 38 #endif 39 40 /** 41 * typedef qemu_plugin_id_t - Unique plugin ID 42 */ 43 typedef uint64_t qemu_plugin_id_t; 44 45 /* 46 * Versioning plugins: 47 * 48 * The plugin API will pass a minimum and current API version that 49 * QEMU currently supports. The minimum API will be incremented if an 50 * API needs to be deprecated. 51 * 52 * The plugins export the API they were built against by exposing the 53 * symbol qemu_plugin_version which can be checked. 54 * 55 * version 2: removed qemu_plugin_n_vcpus and qemu_plugin_n_max_vcpus 56 */ 57 58 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; 59 60 #define QEMU_PLUGIN_VERSION 2 61 62 /** 63 * struct qemu_info_t - system information for plugins 64 * 65 * This structure provides for some limited information about the 66 * system to allow the plugin to make decisions on how to proceed. For 67 * example it might only be suitable for running on some guest 68 * architectures or when under full system emulation. 69 */ 70 typedef struct qemu_info_t { 71 /** @target_name: string describing architecture */ 72 const char *target_name; 73 /** @version: minimum and current plugin API level */ 74 struct { 75 int min; 76 int cur; 77 } version; 78 /** @system_emulation: is this a full system emulation? */ 79 bool system_emulation; 80 union { 81 /** @system: information relevant to system emulation */ 82 struct { 83 /** @system.smp_vcpus: initial number of vCPUs */ 84 int smp_vcpus; 85 /** @system.max_vcpus: maximum possible number of vCPUs */ 86 int max_vcpus; 87 } system; 88 }; 89 } qemu_info_t; 90 91 /** 92 * qemu_plugin_install() - Install a plugin 93 * @id: this plugin's opaque ID 94 * @info: a block describing some details about the guest 95 * @argc: number of arguments 96 * @argv: array of arguments (@argc elements) 97 * 98 * All plugins must export this symbol which is called when the plugin 99 * is first loaded. Calling qemu_plugin_uninstall() from this function 100 * is a bug. 101 * 102 * Note: @info is only live during the call. Copy any information we 103 * want to keep. @argv remains valid throughout the lifetime of the 104 * loaded plugin. 105 * 106 * Return: 0 on successful loading, !0 for an error. 107 */ 108 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, 109 const qemu_info_t *info, 110 int argc, char **argv); 111 112 /** 113 * typedef qemu_plugin_simple_cb_t - simple callback 114 * @id: the unique qemu_plugin_id_t 115 * 116 * This callback passes no information aside from the unique @id. 117 */ 118 typedef void (*qemu_plugin_simple_cb_t)(qemu_plugin_id_t id); 119 120 /** 121 * typedef qemu_plugin_udata_cb_t - callback with user data 122 * @id: the unique qemu_plugin_id_t 123 * @userdata: a pointer to some user data supplied when the callback 124 * was registered. 125 */ 126 typedef void (*qemu_plugin_udata_cb_t)(qemu_plugin_id_t id, void *userdata); 127 128 /** 129 * typedef qemu_plugin_vcpu_simple_cb_t - vcpu callback 130 * @id: the unique qemu_plugin_id_t 131 * @vcpu_index: the current vcpu context 132 */ 133 typedef void (*qemu_plugin_vcpu_simple_cb_t)(qemu_plugin_id_t id, 134 unsigned int vcpu_index); 135 136 /** 137 * typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback 138 * @vcpu_index: the current vcpu context 139 * @userdata: a pointer to some user data supplied when the callback 140 * was registered. 141 */ 142 typedef void (*qemu_plugin_vcpu_udata_cb_t)(unsigned int vcpu_index, 143 void *userdata); 144 145 /** 146 * qemu_plugin_uninstall() - Uninstall a plugin 147 * @id: this plugin's opaque ID 148 * @cb: callback to be called once the plugin has been removed 149 * 150 * Do NOT assume that the plugin has been uninstalled once this function 151 * returns. Plugins are uninstalled asynchronously, and therefore the given 152 * plugin receives callbacks until @cb is called. 153 * 154 * Note: Calling this function from qemu_plugin_install() is a bug. 155 */ 156 QEMU_PLUGIN_API 157 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb); 158 159 /** 160 * qemu_plugin_reset() - Reset a plugin 161 * @id: this plugin's opaque ID 162 * @cb: callback to be called once the plugin has been reset 163 * 164 * Unregisters all callbacks for the plugin given by @id. 165 * 166 * Do NOT assume that the plugin has been reset once this function returns. 167 * Plugins are reset asynchronously, and therefore the given plugin receives 168 * callbacks until @cb is called. 169 */ 170 QEMU_PLUGIN_API 171 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb); 172 173 /** 174 * qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback 175 * @id: plugin ID 176 * @cb: callback function 177 * 178 * The @cb function is called every time a vCPU is initialized. 179 * 180 * See also: qemu_plugin_register_vcpu_exit_cb() 181 */ 182 QEMU_PLUGIN_API 183 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id, 184 qemu_plugin_vcpu_simple_cb_t cb); 185 186 /** 187 * qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback 188 * @id: plugin ID 189 * @cb: callback function 190 * 191 * The @cb function is called every time a vCPU exits. 192 * 193 * See also: qemu_plugin_register_vcpu_init_cb() 194 */ 195 QEMU_PLUGIN_API 196 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id, 197 qemu_plugin_vcpu_simple_cb_t cb); 198 199 /** 200 * qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback 201 * @id: plugin ID 202 * @cb: callback function 203 * 204 * The @cb function is called every time a vCPU idles. 205 */ 206 QEMU_PLUGIN_API 207 void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id, 208 qemu_plugin_vcpu_simple_cb_t cb); 209 210 /** 211 * qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback 212 * @id: plugin ID 213 * @cb: callback function 214 * 215 * The @cb function is called every time a vCPU resumes execution. 216 */ 217 QEMU_PLUGIN_API 218 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id, 219 qemu_plugin_vcpu_simple_cb_t cb); 220 221 /** struct qemu_plugin_tb - Opaque handle for a translation block */ 222 struct qemu_plugin_tb; 223 /** struct qemu_plugin_insn - Opaque handle for a translated instruction */ 224 struct qemu_plugin_insn; 225 /** struct qemu_plugin_scoreboard - Opaque handle for a scoreboard */ 226 struct qemu_plugin_scoreboard; 227 228 /** 229 * typedef qemu_plugin_u64 - uint64_t member of an entry in a scoreboard 230 * 231 * This field allows to access a specific uint64_t member in one given entry, 232 * located at a specified offset. Inline operations expect this as entry. 233 */ 234 typedef struct { 235 struct qemu_plugin_scoreboard *score; 236 size_t offset; 237 } qemu_plugin_u64; 238 239 /** 240 * enum qemu_plugin_cb_flags - type of callback 241 * 242 * @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs 243 * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs 244 * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs 245 * 246 * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change 247 * system register state. 248 */ 249 enum qemu_plugin_cb_flags { 250 QEMU_PLUGIN_CB_NO_REGS, 251 QEMU_PLUGIN_CB_R_REGS, 252 QEMU_PLUGIN_CB_RW_REGS, 253 }; 254 255 enum qemu_plugin_mem_rw { 256 QEMU_PLUGIN_MEM_R = 1, 257 QEMU_PLUGIN_MEM_W, 258 QEMU_PLUGIN_MEM_RW, 259 }; 260 261 /** 262 * typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback 263 * @id: unique plugin id 264 * @tb: opaque handle used for querying and instrumenting a block. 265 */ 266 typedef void (*qemu_plugin_vcpu_tb_trans_cb_t)(qemu_plugin_id_t id, 267 struct qemu_plugin_tb *tb); 268 269 /** 270 * qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb 271 * @id: plugin ID 272 * @cb: callback function 273 * 274 * The @cb function is called every time a translation occurs. The @cb 275 * function is passed an opaque qemu_plugin_type which it can query 276 * for additional information including the list of translated 277 * instructions. At this point the plugin can register further 278 * callbacks to be triggered when the block or individual instruction 279 * executes. 280 */ 281 QEMU_PLUGIN_API 282 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id, 283 qemu_plugin_vcpu_tb_trans_cb_t cb); 284 285 /** 286 * qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback 287 * @tb: the opaque qemu_plugin_tb handle for the translation 288 * @cb: callback function 289 * @flags: does the plugin read or write the CPU's registers? 290 * @userdata: any plugin data to pass to the @cb? 291 * 292 * The @cb function is called every time a translated unit executes. 293 */ 294 QEMU_PLUGIN_API 295 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb, 296 qemu_plugin_vcpu_udata_cb_t cb, 297 enum qemu_plugin_cb_flags flags, 298 void *userdata); 299 300 /** 301 * enum qemu_plugin_op - describes an inline op 302 * 303 * @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t 304 * 305 * Note: currently only a single inline op is supported. 306 */ 307 308 enum qemu_plugin_op { 309 QEMU_PLUGIN_INLINE_ADD_U64, 310 }; 311 312 /** 313 * qemu_plugin_register_vcpu_tb_exec_inline() - execution inline op 314 * @tb: the opaque qemu_plugin_tb handle for the translation 315 * @op: the type of qemu_plugin_op (e.g. ADD_U64) 316 * @ptr: the target memory location for the op 317 * @imm: the op data (e.g. 1) 318 * 319 * Insert an inline op to every time a translated unit executes. 320 * Useful if you just want to increment a single counter somewhere in 321 * memory. 322 * 323 * Note: ops are not atomic so in multi-threaded/multi-smp situations 324 * you will get inexact results. 325 */ 326 QEMU_PLUGIN_API 327 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb, 328 enum qemu_plugin_op op, 329 void *ptr, uint64_t imm); 330 331 /** 332 * qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb 333 * @insn: the opaque qemu_plugin_insn handle for an instruction 334 * @cb: callback function 335 * @flags: does the plugin read or write the CPU's registers? 336 * @userdata: any plugin data to pass to the @cb? 337 * 338 * The @cb function is called every time an instruction is executed 339 */ 340 QEMU_PLUGIN_API 341 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn, 342 qemu_plugin_vcpu_udata_cb_t cb, 343 enum qemu_plugin_cb_flags flags, 344 void *userdata); 345 346 /** 347 * qemu_plugin_register_vcpu_insn_exec_inline() - insn execution inline op 348 * @insn: the opaque qemu_plugin_insn handle for an instruction 349 * @op: the type of qemu_plugin_op (e.g. ADD_U64) 350 * @ptr: the target memory location for the op 351 * @imm: the op data (e.g. 1) 352 * 353 * Insert an inline op to every time an instruction executes. Useful 354 * if you just want to increment a single counter somewhere in memory. 355 */ 356 QEMU_PLUGIN_API 357 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn, 358 enum qemu_plugin_op op, 359 void *ptr, uint64_t imm); 360 361 /** 362 * qemu_plugin_tb_n_insns() - query helper for number of insns in TB 363 * @tb: opaque handle to TB passed to callback 364 * 365 * Returns: number of instructions in this block 366 */ 367 QEMU_PLUGIN_API 368 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb); 369 370 /** 371 * qemu_plugin_tb_vaddr() - query helper for vaddr of TB start 372 * @tb: opaque handle to TB passed to callback 373 * 374 * Returns: virtual address of block start 375 */ 376 QEMU_PLUGIN_API 377 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb); 378 379 /** 380 * qemu_plugin_tb_get_insn() - retrieve handle for instruction 381 * @tb: opaque handle to TB passed to callback 382 * @idx: instruction number, 0 indexed 383 * 384 * The returned handle can be used in follow up helper queries as well 385 * as when instrumenting an instruction. It is only valid for the 386 * lifetime of the callback. 387 * 388 * Returns: opaque handle to instruction 389 */ 390 QEMU_PLUGIN_API 391 struct qemu_plugin_insn * 392 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx); 393 394 /** 395 * qemu_plugin_insn_data() - return ptr to instruction data 396 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 397 * 398 * Note: data is only valid for duration of callback. See 399 * qemu_plugin_insn_size() to calculate size of stream. 400 * 401 * Returns: pointer to a stream of bytes containing the value of this 402 * instructions opcode. 403 */ 404 QEMU_PLUGIN_API 405 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn); 406 407 /** 408 * qemu_plugin_insn_size() - return size of instruction 409 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 410 * 411 * Returns: size of instruction in bytes 412 */ 413 QEMU_PLUGIN_API 414 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn); 415 416 /** 417 * qemu_plugin_insn_vaddr() - return vaddr of instruction 418 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 419 * 420 * Returns: virtual address of instruction 421 */ 422 QEMU_PLUGIN_API 423 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn); 424 425 /** 426 * qemu_plugin_insn_haddr() - return hardware addr of instruction 427 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 428 * 429 * Returns: hardware (physical) target address of instruction 430 */ 431 QEMU_PLUGIN_API 432 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn); 433 434 /** 435 * typedef qemu_plugin_meminfo_t - opaque memory transaction handle 436 * 437 * This can be further queried using the qemu_plugin_mem_* query 438 * functions. 439 */ 440 typedef uint32_t qemu_plugin_meminfo_t; 441 /** struct qemu_plugin_hwaddr - opaque hw address handle */ 442 struct qemu_plugin_hwaddr; 443 444 /** 445 * qemu_plugin_mem_size_shift() - get size of access 446 * @info: opaque memory transaction handle 447 * 448 * Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...) 449 */ 450 QEMU_PLUGIN_API 451 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info); 452 /** 453 * qemu_plugin_mem_is_sign_extended() - was the access sign extended 454 * @info: opaque memory transaction handle 455 * 456 * Returns: true if it was, otherwise false 457 */ 458 QEMU_PLUGIN_API 459 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info); 460 /** 461 * qemu_plugin_mem_is_big_endian() - was the access big endian 462 * @info: opaque memory transaction handle 463 * 464 * Returns: true if it was, otherwise false 465 */ 466 QEMU_PLUGIN_API 467 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info); 468 /** 469 * qemu_plugin_mem_is_store() - was the access a store 470 * @info: opaque memory transaction handle 471 * 472 * Returns: true if it was, otherwise false 473 */ 474 QEMU_PLUGIN_API 475 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info); 476 477 /** 478 * qemu_plugin_get_hwaddr() - return handle for memory operation 479 * @info: opaque memory info structure 480 * @vaddr: the virtual address of the memory operation 481 * 482 * For system emulation returns a qemu_plugin_hwaddr handle to query 483 * details about the actual physical address backing the virtual 484 * address. For linux-user guests it just returns NULL. 485 * 486 * This handle is *only* valid for the duration of the callback. Any 487 * information about the handle should be recovered before the 488 * callback returns. 489 */ 490 QEMU_PLUGIN_API 491 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info, 492 uint64_t vaddr); 493 494 /* 495 * The following additional queries can be run on the hwaddr structure to 496 * return information about it - namely whether it is for an IO access and the 497 * physical address associated with the access. 498 */ 499 500 /** 501 * qemu_plugin_hwaddr_is_io() - query whether memory operation is IO 502 * @haddr: address handle from qemu_plugin_get_hwaddr() 503 * 504 * Returns true if the handle's memory operation is to memory-mapped IO, or 505 * false if it is to RAM 506 */ 507 QEMU_PLUGIN_API 508 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr); 509 510 /** 511 * qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation 512 * @haddr: address handle from qemu_plugin_get_hwaddr() 513 * 514 * Returns the physical address associated with the memory operation 515 * 516 * Note that the returned physical address may not be unique if you are dealing 517 * with multiple address spaces. 518 */ 519 QEMU_PLUGIN_API 520 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr); 521 522 /* 523 * Returns a string representing the device. The string is valid for 524 * the lifetime of the plugin. 525 */ 526 QEMU_PLUGIN_API 527 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h); 528 529 /** 530 * typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type 531 * @vcpu_index: the executing vCPU 532 * @info: an opaque handle for further queries about the memory 533 * @vaddr: the virtual address of the transaction 534 * @userdata: any user data attached to the callback 535 */ 536 typedef void (*qemu_plugin_vcpu_mem_cb_t) (unsigned int vcpu_index, 537 qemu_plugin_meminfo_t info, 538 uint64_t vaddr, 539 void *userdata); 540 541 /** 542 * qemu_plugin_register_vcpu_mem_cb() - register memory access callback 543 * @insn: handle for instruction to instrument 544 * @cb: callback of type qemu_plugin_vcpu_mem_cb_t 545 * @flags: (currently unused) callback flags 546 * @rw: monitor reads, writes or both 547 * @userdata: opaque pointer for userdata 548 * 549 * This registers a full callback for every memory access generated by 550 * an instruction. If the instruction doesn't access memory no 551 * callback will be made. 552 * 553 * The callback reports the vCPU the access took place on, the virtual 554 * address of the access and a handle for further queries. The user 555 * can attach some userdata to the callback for additional purposes. 556 * 557 * Other execution threads will continue to execute during the 558 * callback so the plugin is responsible for ensuring it doesn't get 559 * confused by making appropriate use of locking if required. 560 */ 561 QEMU_PLUGIN_API 562 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn, 563 qemu_plugin_vcpu_mem_cb_t cb, 564 enum qemu_plugin_cb_flags flags, 565 enum qemu_plugin_mem_rw rw, 566 void *userdata); 567 568 /** 569 * qemu_plugin_register_vcpu_mem_inline() - register an inline op to any memory access 570 * @insn: handle for instruction to instrument 571 * @rw: apply to reads, writes or both 572 * @op: the op, of type qemu_plugin_op 573 * @ptr: pointer memory for the op 574 * @imm: immediate data for @op 575 * 576 * This registers a inline op every memory access generated by the 577 * instruction. This provides for a lightweight but not thread-safe 578 * way of counting the number of operations done. 579 */ 580 QEMU_PLUGIN_API 581 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn, 582 enum qemu_plugin_mem_rw rw, 583 enum qemu_plugin_op op, void *ptr, 584 uint64_t imm); 585 586 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 594 QEMU_PLUGIN_API 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 602 QEMU_PLUGIN_API 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 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 615 QEMU_PLUGIN_API 616 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn); 617 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 */ 625 QEMU_PLUGIN_API 626 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn); 627 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 */ 637 QEMU_PLUGIN_API 638 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id, 639 qemu_plugin_vcpu_simple_cb_t cb); 640 641 QEMU_PLUGIN_API 642 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id, 643 qemu_plugin_simple_cb_t cb); 644 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 */ 658 QEMU_PLUGIN_API 659 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id, 660 qemu_plugin_udata_cb_t cb, void *userdata); 661 662 /* returns how many vcpus were started at this point */ 663 int qemu_plugin_num_vcpus(void); 664 665 /** 666 * qemu_plugin_outs() - output string via QEMU's logging system 667 * @string: a string 668 */ 669 QEMU_PLUGIN_API 670 void qemu_plugin_outs(const char *string); 671 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 */ 683 QEMU_PLUGIN_API 684 bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret); 685 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 */ 694 QEMU_PLUGIN_API 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 */ 703 QEMU_PLUGIN_API 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 */ 712 QEMU_PLUGIN_API 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 */ 721 QEMU_PLUGIN_API 722 uint64_t qemu_plugin_entry_code(void); 723 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 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); 797 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 839 #endif /* QEMU_QEMU_PLUGIN_H */ 840