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: 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. 60 */ 61 62 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; 63 64 #define QEMU_PLUGIN_VERSION 2 65 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 */ 76 const char *target_name; 77 /** @version: minimum and current plugin API level */ 78 struct { 79 int min; 80 int cur; 81 } version; 82 /** @system_emulation: is this a full system emulation? */ 83 bool system_emulation; 84 union { 85 /** @system: information relevant to system emulation */ 86 struct { 87 /** @system.smp_vcpus: initial number of vCPUs */ 88 int smp_vcpus; 89 /** @system.max_vcpus: maximum possible number of vCPUs */ 90 int max_vcpus; 91 } system; 92 }; 93 } qemu_info_t; 94 95 /** 96 * qemu_plugin_install() - Install a plugin 97 * @id: this plugin's opaque ID 98 * @info: a block describing some details about the guest 99 * @argc: number of arguments 100 * @argv: array of arguments (@argc elements) 101 * 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. 105 * 106 * Note: @info is only live during the call. Copy any information we 107 * want to keep. @argv remains valid throughout the lifetime of the 108 * loaded plugin. 109 * 110 * Return: 0 on successful loading, !0 for an error. 111 */ 112 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, 113 const qemu_info_t *info, 114 int argc, char **argv); 115 116 /** 117 * typedef qemu_plugin_simple_cb_t - simple callback 118 * @id: the unique qemu_plugin_id_t 119 * 120 * This callback passes no information aside from the unique @id. 121 */ 122 typedef void (*qemu_plugin_simple_cb_t)(qemu_plugin_id_t id); 123 124 /** 125 * typedef qemu_plugin_udata_cb_t - callback with user data 126 * @id: the unique qemu_plugin_id_t 127 * @userdata: a pointer to some user data supplied when the callback 128 * was registered. 129 */ 130 typedef void (*qemu_plugin_udata_cb_t)(qemu_plugin_id_t id, void *userdata); 131 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 */ 137 typedef void (*qemu_plugin_vcpu_simple_cb_t)(qemu_plugin_id_t id, 138 unsigned int vcpu_index); 139 140 /** 141 * typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback 142 * @vcpu_index: the current vcpu context 143 * @userdata: a pointer to some user data supplied when the callback 144 * was registered. 145 */ 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 */ 160 QEMU_PLUGIN_API 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 */ 174 QEMU_PLUGIN_API 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 */ 186 QEMU_PLUGIN_API 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 */ 199 QEMU_PLUGIN_API 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 */ 210 QEMU_PLUGIN_API 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 */ 221 QEMU_PLUGIN_API 222 void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id, 223 qemu_plugin_vcpu_simple_cb_t cb); 224 225 /** struct qemu_plugin_tb - Opaque handle for a translation block */ 226 struct qemu_plugin_tb; 227 /** struct qemu_plugin_insn - Opaque handle for a translated instruction */ 228 struct qemu_plugin_insn; 229 /** struct qemu_plugin_scoreboard - Opaque handle for a scoreboard */ 230 struct qemu_plugin_scoreboard; 231 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 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 * 250 * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change 251 * system register state. 252 */ 253 enum qemu_plugin_cb_flags { 254 QEMU_PLUGIN_CB_NO_REGS, 255 QEMU_PLUGIN_CB_R_REGS, 256 QEMU_PLUGIN_CB_RW_REGS, 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 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 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 */ 285 QEMU_PLUGIN_API 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 /** 290 * qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback 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 */ 298 QEMU_PLUGIN_API 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 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 312 enum qemu_plugin_op { 313 QEMU_PLUGIN_INLINE_ADD_U64, 314 }; 315 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 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 */ 341 QEMU_PLUGIN_API 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 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 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 368 */ 369 QEMU_PLUGIN_API 370 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb); 371 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 */ 378 QEMU_PLUGIN_API 379 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb); 380 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 */ 392 QEMU_PLUGIN_API 393 struct qemu_plugin_insn * 394 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx); 395 396 /** 397 * qemu_plugin_insn_data() - return ptr to instruction data 398 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 399 * 400 * Note: data is only valid for duration of callback. See 401 * qemu_plugin_insn_size() to calculate size of stream. 402 * 403 * Returns: pointer to a stream of bytes containing the value of this 404 * instructions opcode. 405 */ 406 QEMU_PLUGIN_API 407 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn); 408 409 /** 410 * qemu_plugin_insn_size() - return size of instruction 411 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 412 * 413 * Returns: size of instruction in bytes 414 */ 415 QEMU_PLUGIN_API 416 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn); 417 418 /** 419 * qemu_plugin_insn_vaddr() - return vaddr of instruction 420 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 421 * 422 * Returns: virtual address of instruction 423 */ 424 QEMU_PLUGIN_API 425 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn); 426 427 /** 428 * qemu_plugin_insn_haddr() - return hardware addr of instruction 429 * @insn: opaque instruction handle from qemu_plugin_tb_get_insn() 430 * 431 * Returns: hardware (physical) target address of instruction 432 */ 433 QEMU_PLUGIN_API 434 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn); 435 436 /** 437 * typedef qemu_plugin_meminfo_t - opaque memory transaction handle 438 * 439 * This can be further queried using the qemu_plugin_mem_* query 440 * functions. 441 */ 442 typedef uint32_t qemu_plugin_meminfo_t; 443 /** struct qemu_plugin_hwaddr - opaque hw address handle */ 444 struct qemu_plugin_hwaddr; 445 446 /** 447 * qemu_plugin_mem_size_shift() - get size of access 448 * @info: opaque memory transaction handle 449 * 450 * Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...) 451 */ 452 QEMU_PLUGIN_API 453 unsigned int qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info); 454 /** 455 * qemu_plugin_mem_is_sign_extended() - was the access sign extended 456 * @info: opaque memory transaction handle 457 * 458 * Returns: true if it was, otherwise false 459 */ 460 QEMU_PLUGIN_API 461 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info); 462 /** 463 * qemu_plugin_mem_is_big_endian() - was the access big endian 464 * @info: opaque memory transaction handle 465 * 466 * Returns: true if it was, otherwise false 467 */ 468 QEMU_PLUGIN_API 469 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info); 470 /** 471 * qemu_plugin_mem_is_store() - was the access a store 472 * @info: opaque memory transaction handle 473 * 474 * Returns: true if it was, otherwise false 475 */ 476 QEMU_PLUGIN_API 477 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info); 478 479 /** 480 * qemu_plugin_get_hwaddr() - return handle for memory operation 481 * @info: opaque memory info structure 482 * @vaddr: the virtual address of the memory operation 483 * 484 * For system emulation returns a qemu_plugin_hwaddr handle to query 485 * details about the actual physical address backing the virtual 486 * address. For linux-user guests it just returns NULL. 487 * 488 * This handle is *only* valid for the duration of the callback. Any 489 * information about the handle should be recovered before the 490 * callback returns. 491 */ 492 QEMU_PLUGIN_API 493 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info, 494 uint64_t vaddr); 495 496 /* 497 * The following additional queries can be run on the hwaddr structure to 498 * return information about it - namely whether it is for an IO access and the 499 * physical address associated with the access. 500 */ 501 502 /** 503 * qemu_plugin_hwaddr_is_io() - query whether memory operation is IO 504 * @haddr: address handle from qemu_plugin_get_hwaddr() 505 * 506 * Returns true if the handle's memory operation is to memory-mapped IO, or 507 * false if it is to RAM 508 */ 509 QEMU_PLUGIN_API 510 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr); 511 512 /** 513 * qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation 514 * @haddr: address handle from qemu_plugin_get_hwaddr() 515 * 516 * Returns the physical address associated with the memory operation 517 * 518 * Note that the returned physical address may not be unique if you are dealing 519 * with multiple address spaces. 520 */ 521 QEMU_PLUGIN_API 522 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr); 523 524 /* 525 * Returns a string representing the device. The string is valid for 526 * the lifetime of the plugin. 527 */ 528 QEMU_PLUGIN_API 529 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h); 530 531 /** 532 * typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type 533 * @vcpu_index: the executing vCPU 534 * @info: an opaque handle for further queries about the memory 535 * @vaddr: the virtual address of the transaction 536 * @userdata: any user data attached to the callback 537 */ 538 typedef void (*qemu_plugin_vcpu_mem_cb_t) (unsigned int vcpu_index, 539 qemu_plugin_meminfo_t info, 540 uint64_t vaddr, 541 void *userdata); 542 543 /** 544 * qemu_plugin_register_vcpu_mem_cb() - register memory access callback 545 * @insn: handle for instruction to instrument 546 * @cb: callback of type qemu_plugin_vcpu_mem_cb_t 547 * @flags: (currently unused) callback flags 548 * @rw: monitor reads, writes or both 549 * @userdata: opaque pointer for userdata 550 * 551 * This registers a full callback for every memory access generated by 552 * an instruction. If the instruction doesn't access memory no 553 * callback will be made. 554 * 555 * The callback reports the vCPU the access took place on, the virtual 556 * address of the access and a handle for further queries. The user 557 * can attach some userdata to the callback for additional purposes. 558 * 559 * Other execution threads will continue to execute during the 560 * callback so the plugin is responsible for ensuring it doesn't get 561 * confused by making appropriate use of locking if required. 562 */ 563 QEMU_PLUGIN_API 564 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn, 565 qemu_plugin_vcpu_mem_cb_t cb, 566 enum qemu_plugin_cb_flags flags, 567 enum qemu_plugin_mem_rw rw, 568 void *userdata); 569 570 /** 571 * qemu_plugin_register_vcpu_mem_inline_per_vcpu() - inline op for mem access 572 * @insn: handle for instruction to instrument 573 * @rw: apply to reads, writes or both 574 * @op: the op, of type qemu_plugin_op 575 * @entry: entry to run op 576 * @imm: immediate data for @op 577 * 578 * This registers a inline op every memory access generated by the 579 * instruction. 580 */ 581 QEMU_PLUGIN_API 582 void qemu_plugin_register_vcpu_mem_inline_per_vcpu( 583 struct qemu_plugin_insn *insn, 584 enum qemu_plugin_mem_rw rw, 585 enum qemu_plugin_op op, 586 qemu_plugin_u64 entry, 587 uint64_t imm); 588 589 typedef void 590 (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index, 591 int64_t num, uint64_t a1, uint64_t a2, 592 uint64_t a3, uint64_t a4, uint64_t a5, 593 uint64_t a6, uint64_t a7, uint64_t a8); 594 595 QEMU_PLUGIN_API 596 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id, 597 qemu_plugin_vcpu_syscall_cb_t cb); 598 599 typedef void 600 (*qemu_plugin_vcpu_syscall_ret_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_idx, 601 int64_t num, int64_t ret); 602 603 QEMU_PLUGIN_API 604 void 605 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id, 606 qemu_plugin_vcpu_syscall_ret_cb_t cb); 607 608 609 /** 610 * qemu_plugin_insn_disas() - return disassembly string for instruction 611 * @insn: instruction reference 612 * 613 * Returns an allocated string containing the disassembly 614 */ 615 616 QEMU_PLUGIN_API 617 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn); 618 619 /** 620 * qemu_plugin_insn_symbol() - best effort symbol lookup 621 * @insn: instruction reference 622 * 623 * Return a static string referring to the symbol. This is dependent 624 * on the binary QEMU is running having provided a symbol table. 625 */ 626 QEMU_PLUGIN_API 627 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn); 628 629 /** 630 * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU 631 * @id: plugin ID 632 * @cb: callback function 633 * 634 * The @cb function is called once for each existing vCPU. 635 * 636 * See also: qemu_plugin_register_vcpu_init_cb() 637 */ 638 QEMU_PLUGIN_API 639 void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id, 640 qemu_plugin_vcpu_simple_cb_t cb); 641 642 QEMU_PLUGIN_API 643 void qemu_plugin_register_flush_cb(qemu_plugin_id_t id, 644 qemu_plugin_simple_cb_t cb); 645 646 /** 647 * qemu_plugin_register_atexit_cb() - register exit callback 648 * @id: plugin ID 649 * @cb: callback 650 * @userdata: user data for callback 651 * 652 * The @cb function is called once execution has finished. Plugins 653 * should be able to free all their resources at this point much like 654 * after a reset/uninstall callback is called. 655 * 656 * In user-mode it is possible a few un-instrumented instructions from 657 * child threads may run before the host kernel reaps the threads. 658 */ 659 QEMU_PLUGIN_API 660 void qemu_plugin_register_atexit_cb(qemu_plugin_id_t id, 661 qemu_plugin_udata_cb_t cb, void *userdata); 662 663 /* returns how many vcpus were started at this point */ 664 int qemu_plugin_num_vcpus(void); 665 666 /** 667 * qemu_plugin_outs() - output string via QEMU's logging system 668 * @string: a string 669 */ 670 QEMU_PLUGIN_API 671 void qemu_plugin_outs(const char *string); 672 673 /** 674 * qemu_plugin_bool_parse() - parses a boolean argument in the form of 675 * "<argname>=[on|yes|true|off|no|false]" 676 * 677 * @name: argument name, the part before the equals sign 678 * @val: argument value, what's after the equals sign 679 * @ret: output return value 680 * 681 * returns true if the combination @name=@val parses correctly to a boolean 682 * argument, and false otherwise 683 */ 684 QEMU_PLUGIN_API 685 bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret); 686 687 /** 688 * qemu_plugin_path_to_binary() - path to binary file being executed 689 * 690 * Return a string representing the path to the binary. For user-mode 691 * this is the main executable. For system emulation we currently 692 * return NULL. The user should g_free() the string once no longer 693 * needed. 694 */ 695 QEMU_PLUGIN_API 696 const char *qemu_plugin_path_to_binary(void); 697 698 /** 699 * qemu_plugin_start_code() - returns start of text segment 700 * 701 * Returns the nominal start address of the main text segment in 702 * user-mode. Currently returns 0 for system emulation. 703 */ 704 QEMU_PLUGIN_API 705 uint64_t qemu_plugin_start_code(void); 706 707 /** 708 * qemu_plugin_end_code() - returns end of text segment 709 * 710 * Returns the nominal end address of the main text segment in 711 * user-mode. Currently returns 0 for system emulation. 712 */ 713 QEMU_PLUGIN_API 714 uint64_t qemu_plugin_end_code(void); 715 716 /** 717 * qemu_plugin_entry_code() - returns start address for module 718 * 719 * Returns the nominal entry address of the main text segment in 720 * user-mode. Currently returns 0 for system emulation. 721 */ 722 QEMU_PLUGIN_API 723 uint64_t qemu_plugin_entry_code(void); 724 725 /** struct qemu_plugin_register - Opaque handle for register access */ 726 struct qemu_plugin_register; 727 728 /** 729 * typedef qemu_plugin_reg_descriptor - register descriptions 730 * 731 * @handle: opaque handle for retrieving value with qemu_plugin_read_register 732 * @name: register name 733 * @feature: optional feature descriptor, can be NULL 734 */ 735 typedef struct { 736 struct qemu_plugin_register *handle; 737 const char *name; 738 const char *feature; 739 } qemu_plugin_reg_descriptor; 740 741 /** 742 * qemu_plugin_get_registers() - return register list for current vCPU 743 * 744 * Returns a potentially empty GArray of qemu_plugin_reg_descriptor. 745 * Caller frees the array (but not the const strings). 746 * 747 * Should be used from a qemu_plugin_register_vcpu_init_cb() callback 748 * after the vCPU is initialised, i.e. in the vCPU context. 749 */ 750 QEMU_PLUGIN_API 751 GArray *qemu_plugin_get_registers(void); 752 753 /** 754 * qemu_plugin_read_register() - read register for current vCPU 755 * 756 * @handle: a @qemu_plugin_reg_handle handle 757 * @buf: A GByteArray for the data owned by the plugin 758 * 759 * This function is only available in a context that register read access is 760 * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. 761 * 762 * Returns the size of the read register. The content of @buf is in target byte 763 * order. On failure returns -1. 764 */ 765 QEMU_PLUGIN_API 766 int qemu_plugin_read_register(struct qemu_plugin_register *handle, 767 GByteArray *buf); 768 769 /** 770 * qemu_plugin_scoreboard_new() - alloc a new scoreboard 771 * 772 * @element_size: size (in bytes) for one entry 773 * 774 * Returns a pointer to a new scoreboard. It must be freed using 775 * qemu_plugin_scoreboard_free. 776 */ 777 QEMU_PLUGIN_API 778 struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_size); 779 780 /** 781 * qemu_plugin_scoreboard_free() - free a scoreboard 782 * @score: scoreboard to free 783 */ 784 QEMU_PLUGIN_API 785 void qemu_plugin_scoreboard_free(struct qemu_plugin_scoreboard *score); 786 787 /** 788 * qemu_plugin_scoreboard_find() - get pointer to an entry of a scoreboard 789 * @score: scoreboard to query 790 * @vcpu_index: entry index 791 * 792 * Returns address of entry of a scoreboard matching a given vcpu_index. This 793 * address can be modified later if scoreboard is resized. 794 */ 795 QEMU_PLUGIN_API 796 void *qemu_plugin_scoreboard_find(struct qemu_plugin_scoreboard *score, 797 unsigned int vcpu_index); 798 799 /* Macros to define a qemu_plugin_u64 */ 800 #define qemu_plugin_scoreboard_u64(score) \ 801 (qemu_plugin_u64) {score, 0} 802 #define qemu_plugin_scoreboard_u64_in_struct(score, type, member) \ 803 (qemu_plugin_u64) {score, offsetof(type, member)} 804 805 /** 806 * qemu_plugin_u64_add() - add a value to a qemu_plugin_u64 for a given vcpu 807 * @entry: entry to query 808 * @vcpu_index: entry index 809 * @added: value to add 810 */ 811 QEMU_PLUGIN_API 812 void qemu_plugin_u64_add(qemu_plugin_u64 entry, unsigned int vcpu_index, 813 uint64_t added); 814 815 /** 816 * qemu_plugin_u64_get() - get value of a qemu_plugin_u64 for a given vcpu 817 * @entry: entry to query 818 * @vcpu_index: entry index 819 */ 820 QEMU_PLUGIN_API 821 uint64_t qemu_plugin_u64_get(qemu_plugin_u64 entry, unsigned int vcpu_index); 822 823 /** 824 * qemu_plugin_u64_set() - set value of a qemu_plugin_u64 for a given vcpu 825 * @entry: entry to query 826 * @vcpu_index: entry index 827 * @val: new value 828 */ 829 QEMU_PLUGIN_API 830 void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index, 831 uint64_t val); 832 833 /** 834 * qemu_plugin_u64_sum() - return sum of all vcpu entries in a scoreboard 835 * @entry: entry to sum 836 */ 837 QEMU_PLUGIN_API 838 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry); 839 840 #endif /* QEMU_QEMU_PLUGIN_H */ 841