plugin.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Plugin Shared Internal Functions
  3. *
  4. * Copyright (C) 2019, Linaro
  5. *
  6. * License: GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. */
  11. #ifndef PLUGIN_H
  12. #define PLUGIN_H
  13. #include <gmodule.h>
  14. #include "qemu/queue.h"
  15. #include "qemu/qht.h"
  16. #define QEMU_PLUGIN_MIN_VERSION 2
  17. /* global state */
  18. struct qemu_plugin_state {
  19. QTAILQ_HEAD(, qemu_plugin_ctx) ctxs;
  20. QLIST_HEAD(, qemu_plugin_cb) cb_lists[QEMU_PLUGIN_EV_MAX];
  21. /*
  22. * Use the HT as a hash map by inserting k == v, which saves memory as
  23. * documented by GLib. The parent struct is obtained with container_of().
  24. */
  25. GHashTable *id_ht;
  26. /*
  27. * Use the HT as a hash map. Note that we could use a list here,
  28. * but with the HT we avoid adding a field to CPUState.
  29. */
  30. GHashTable *cpu_ht;
  31. QLIST_HEAD(, qemu_plugin_scoreboard) scoreboards;
  32. size_t scoreboard_alloc_size;
  33. DECLARE_BITMAP(mask, QEMU_PLUGIN_EV_MAX);
  34. /*
  35. * @lock protects the struct as well as ctx->uninstalling.
  36. * The lock must be acquired by all API ops.
  37. * The lock is recursive, which greatly simplifies things, e.g.
  38. * callback registration from qemu_plugin_vcpu_for_each().
  39. */
  40. QemuRecMutex lock;
  41. /*
  42. * HT of callbacks invoked from helpers. All entries are freed when
  43. * the code cache is flushed.
  44. */
  45. struct qht dyn_cb_arr_ht;
  46. /* How many vcpus were started */
  47. int num_vcpus;
  48. };
  49. struct qemu_plugin_ctx {
  50. GModule *handle;
  51. qemu_plugin_id_t id;
  52. struct qemu_plugin_cb *callbacks[QEMU_PLUGIN_EV_MAX];
  53. QTAILQ_ENTRY(qemu_plugin_ctx) entry;
  54. /*
  55. * keep a reference to @desc until uninstall, so that plugins do not have
  56. * to strdup plugin args.
  57. */
  58. struct qemu_plugin_desc *desc;
  59. bool installing;
  60. bool uninstalling;
  61. bool resetting;
  62. };
  63. struct qemu_plugin_ctx *plugin_id_to_ctx_locked(qemu_plugin_id_t id);
  64. void plugin_register_inline_op_on_entry(GArray **arr,
  65. enum qemu_plugin_mem_rw rw,
  66. enum qemu_plugin_op op,
  67. qemu_plugin_u64 entry,
  68. uint64_t imm);
  69. void plugin_reset_uninstall(qemu_plugin_id_t id,
  70. qemu_plugin_simple_cb_t cb,
  71. bool reset);
  72. void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  73. void *func);
  74. void plugin_unregister_cb__locked(struct qemu_plugin_ctx *ctx,
  75. enum qemu_plugin_event ev);
  76. void
  77. plugin_register_cb_udata(qemu_plugin_id_t id, enum qemu_plugin_event ev,
  78. void *func, void *udata);
  79. void
  80. plugin_register_dyn_cb__udata(GArray **arr,
  81. qemu_plugin_vcpu_udata_cb_t cb,
  82. enum qemu_plugin_cb_flags flags, void *udata);
  83. void
  84. plugin_register_dyn_cond_cb__udata(GArray **arr,
  85. qemu_plugin_vcpu_udata_cb_t cb,
  86. enum qemu_plugin_cb_flags flags,
  87. enum qemu_plugin_cond cond,
  88. qemu_plugin_u64 entry,
  89. uint64_t imm,
  90. void *udata);
  91. void plugin_register_vcpu_mem_cb(GArray **arr,
  92. void *cb,
  93. enum qemu_plugin_cb_flags flags,
  94. enum qemu_plugin_mem_rw rw,
  95. void *udata);
  96. void exec_inline_op(enum plugin_dyn_cb_type type,
  97. struct qemu_plugin_inline_cb *cb,
  98. int cpu_index);
  99. int plugin_num_vcpus(void);
  100. struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size);
  101. void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score);
  102. /**
  103. * qemu_plugin_fillin_mode_info() - populate mode specific info
  104. * info: pointer to qemu_info_t structure
  105. */
  106. void qemu_plugin_fillin_mode_info(qemu_info_t *info);
  107. #endif /* PLUGIN_H */