loader.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * QEMU Plugin Core Loader Code
  3. *
  4. * This is the code responsible for loading and unloading the plugins.
  5. * Aside from the basic housekeeping tasks we also need to ensure any
  6. * generated code is flushed when we remove a plugin so we cannot end
  7. * up calling and unloaded helper function.
  8. *
  9. * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
  10. * Copyright (C) 2019, Linaro
  11. *
  12. * License: GNU GPL, version 2 or later.
  13. * See the COPYING file in the top-level directory.
  14. *
  15. * SPDX-License-Identifier: GPL-2.0-or-later
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu/error-report.h"
  19. #include "qemu/config-file.h"
  20. #include "qapi/error.h"
  21. #include "qemu/lockable.h"
  22. #include "qemu/option.h"
  23. #include "qemu/rcu_queue.h"
  24. #include "qemu/qht.h"
  25. #include "qemu/bitmap.h"
  26. #include "qemu/xxhash.h"
  27. #include "qemu/plugin.h"
  28. #include "hw/core/cpu.h"
  29. #include "cpu.h"
  30. #include "exec/exec-all.h"
  31. #ifndef CONFIG_USER_ONLY
  32. #include "hw/boards.h"
  33. #endif
  34. #include "plugin.h"
  35. /*
  36. * For convenience we use a bitmap for plugin.mask, but really all we need is a
  37. * u32, which is what we store in TranslationBlock.
  38. */
  39. QEMU_BUILD_BUG_ON(QEMU_PLUGIN_EV_MAX > 32);
  40. struct qemu_plugin_desc {
  41. char *path;
  42. char **argv;
  43. QTAILQ_ENTRY(qemu_plugin_desc) entry;
  44. int argc;
  45. };
  46. struct qemu_plugin_parse_arg {
  47. QemuPluginList *head;
  48. struct qemu_plugin_desc *curr;
  49. };
  50. QemuOptsList qemu_plugin_opts = {
  51. .name = "plugin",
  52. .implied_opt_name = "file",
  53. .head = QTAILQ_HEAD_INITIALIZER(qemu_plugin_opts.head),
  54. .desc = {
  55. /* do our own parsing to support multiple plugins */
  56. { /* end of list */ }
  57. },
  58. };
  59. typedef int (*qemu_plugin_install_func_t)(qemu_plugin_id_t, const qemu_info_t *, int, char **);
  60. extern struct qemu_plugin_state plugin;
  61. void qemu_plugin_add_dyn_cb_arr(GArray *arr)
  62. {
  63. uint32_t hash = qemu_xxhash2((uint64_t)(uintptr_t)arr);
  64. bool inserted;
  65. inserted = qht_insert(&plugin.dyn_cb_arr_ht, arr, hash, NULL);
  66. g_assert(inserted);
  67. }
  68. static struct qemu_plugin_desc *plugin_find_desc(QemuPluginList *head,
  69. const char *path)
  70. {
  71. struct qemu_plugin_desc *desc;
  72. QTAILQ_FOREACH(desc, head, entry) {
  73. if (strcmp(desc->path, path) == 0) {
  74. return desc;
  75. }
  76. }
  77. return NULL;
  78. }
  79. static int plugin_add(void *opaque, const char *name, const char *value,
  80. Error **errp)
  81. {
  82. struct qemu_plugin_parse_arg *arg = opaque;
  83. struct qemu_plugin_desc *p;
  84. if (strcmp(name, "file") == 0) {
  85. if (strcmp(value, "") == 0) {
  86. error_setg(errp, "requires a non-empty argument");
  87. return 1;
  88. }
  89. p = plugin_find_desc(arg->head, value);
  90. if (p == NULL) {
  91. p = g_new0(struct qemu_plugin_desc, 1);
  92. p->path = g_strdup(value);
  93. QTAILQ_INSERT_TAIL(arg->head, p, entry);
  94. }
  95. arg->curr = p;
  96. } else if (strcmp(name, "arg") == 0) {
  97. if (arg->curr == NULL) {
  98. error_setg(errp, "missing earlier '-plugin file=' option");
  99. return 1;
  100. }
  101. p = arg->curr;
  102. p->argc++;
  103. p->argv = g_realloc_n(p->argv, p->argc, sizeof(char *));
  104. p->argv[p->argc - 1] = g_strdup(value);
  105. } else {
  106. error_setg(errp, "-plugin: unexpected parameter '%s'; ignored", name);
  107. }
  108. return 0;
  109. }
  110. void qemu_plugin_opt_parse(const char *optarg, QemuPluginList *head)
  111. {
  112. struct qemu_plugin_parse_arg arg;
  113. QemuOpts *opts;
  114. opts = qemu_opts_parse_noisily(qemu_find_opts("plugin"), optarg, true);
  115. if (opts == NULL) {
  116. exit(1);
  117. }
  118. arg.head = head;
  119. arg.curr = NULL;
  120. qemu_opt_foreach(opts, plugin_add, &arg, &error_fatal);
  121. qemu_opts_del(opts);
  122. }
  123. /*
  124. * From: https://en.wikipedia.org/wiki/Xorshift
  125. * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
  126. * guaranteed to be >= INT_MAX).
  127. */
  128. static uint64_t xorshift64star(uint64_t x)
  129. {
  130. x ^= x >> 12; /* a */
  131. x ^= x << 25; /* b */
  132. x ^= x >> 27; /* c */
  133. return x * UINT64_C(2685821657736338717);
  134. }
  135. static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info)
  136. {
  137. qemu_plugin_install_func_t install;
  138. struct qemu_plugin_ctx *ctx;
  139. gpointer sym;
  140. int rc;
  141. ctx = qemu_memalign(qemu_dcache_linesize, sizeof(*ctx));
  142. memset(ctx, 0, sizeof(*ctx));
  143. ctx->desc = desc;
  144. ctx->handle = g_module_open(desc->path, G_MODULE_BIND_LOCAL);
  145. if (ctx->handle == NULL) {
  146. error_report("%s: %s", __func__, g_module_error());
  147. goto err_dlopen;
  148. }
  149. if (!g_module_symbol(ctx->handle, "qemu_plugin_install", &sym)) {
  150. error_report("%s: %s", __func__, g_module_error());
  151. goto err_symbol;
  152. }
  153. install = (qemu_plugin_install_func_t) sym;
  154. /* symbol was found; it could be NULL though */
  155. if (install == NULL) {
  156. error_report("%s: %s: qemu_plugin_install is NULL",
  157. __func__, desc->path);
  158. goto err_symbol;
  159. }
  160. if (!g_module_symbol(ctx->handle, "qemu_plugin_version", &sym)) {
  161. error_report("TCG plugin %s does not declare API version %s",
  162. desc->path, g_module_error());
  163. goto err_symbol;
  164. } else {
  165. int version = *(int *)sym;
  166. if (version < QEMU_PLUGIN_MIN_VERSION) {
  167. error_report("TCG plugin %s requires API version %d, but "
  168. "this QEMU supports only a minimum version of %d",
  169. desc->path, version, QEMU_PLUGIN_MIN_VERSION);
  170. goto err_symbol;
  171. } else if (version > QEMU_PLUGIN_VERSION) {
  172. error_report("TCG plugin %s requires API version %d, but "
  173. "this QEMU supports only up to version %d",
  174. desc->path, version, QEMU_PLUGIN_VERSION);
  175. goto err_symbol;
  176. }
  177. }
  178. qemu_rec_mutex_lock(&plugin.lock);
  179. /* find an unused random id with &ctx as the seed */
  180. ctx->id = (uint64_t)(uintptr_t)ctx;
  181. for (;;) {
  182. void *existing;
  183. ctx->id = xorshift64star(ctx->id);
  184. existing = g_hash_table_lookup(plugin.id_ht, &ctx->id);
  185. if (likely(existing == NULL)) {
  186. bool success;
  187. success = g_hash_table_insert(plugin.id_ht, &ctx->id, &ctx->id);
  188. g_assert(success);
  189. break;
  190. }
  191. }
  192. QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry);
  193. ctx->installing = true;
  194. rc = install(ctx->id, info, desc->argc, desc->argv);
  195. ctx->installing = false;
  196. if (rc) {
  197. error_report("%s: qemu_plugin_install returned error code %d",
  198. __func__, rc);
  199. /*
  200. * we cannot rely on the plugin doing its own cleanup, so
  201. * call a full uninstall if the plugin did not yet call it.
  202. */
  203. if (!ctx->uninstalling) {
  204. plugin_reset_uninstall(ctx->id, NULL, false);
  205. }
  206. }
  207. qemu_rec_mutex_unlock(&plugin.lock);
  208. return rc;
  209. err_symbol:
  210. g_module_close(ctx->handle);
  211. err_dlopen:
  212. qemu_vfree(ctx);
  213. return 1;
  214. }
  215. /* call after having removed @desc from the list */
  216. static void plugin_desc_free(struct qemu_plugin_desc *desc)
  217. {
  218. int i;
  219. for (i = 0; i < desc->argc; i++) {
  220. g_free(desc->argv[i]);
  221. }
  222. g_free(desc->argv);
  223. g_free(desc->path);
  224. g_free(desc);
  225. }
  226. /**
  227. * qemu_plugin_load_list - load a list of plugins
  228. * @head: head of the list of descriptors of the plugins to be loaded
  229. *
  230. * Returns 0 if all plugins in the list are installed, !0 otherwise.
  231. *
  232. * Note: the descriptor of each successfully installed plugin is removed
  233. * from the list given by @head.
  234. */
  235. int qemu_plugin_load_list(QemuPluginList *head)
  236. {
  237. struct qemu_plugin_desc *desc, *next;
  238. g_autofree qemu_info_t *info = g_new0(qemu_info_t, 1);
  239. info->target_name = TARGET_NAME;
  240. info->version.min = QEMU_PLUGIN_MIN_VERSION;
  241. info->version.cur = QEMU_PLUGIN_VERSION;
  242. #ifndef CONFIG_USER_ONLY
  243. MachineState *ms = MACHINE(qdev_get_machine());
  244. info->system_emulation = true;
  245. info->system.smp_vcpus = ms->smp.cpus;
  246. info->system.max_vcpus = ms->smp.max_cpus;
  247. #else
  248. info->system_emulation = false;
  249. #endif
  250. QTAILQ_FOREACH_SAFE(desc, head, entry, next) {
  251. int err;
  252. err = plugin_load(desc, info);
  253. if (err) {
  254. return err;
  255. }
  256. QTAILQ_REMOVE(head, desc, entry);
  257. }
  258. return 0;
  259. }
  260. struct qemu_plugin_reset_data {
  261. struct qemu_plugin_ctx *ctx;
  262. qemu_plugin_simple_cb_t cb;
  263. bool reset;
  264. };
  265. static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data)
  266. {
  267. struct qemu_plugin_ctx *ctx = data->ctx;
  268. enum qemu_plugin_event ev;
  269. bool success;
  270. /*
  271. * After updating the subscription lists there is no need to wait for an RCU
  272. * grace period to elapse, because right now we either are in a "safe async"
  273. * work environment (i.e. all vCPUs are asleep), or no vCPUs have yet been
  274. * created.
  275. */
  276. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  277. plugin_unregister_cb__locked(ctx, ev);
  278. }
  279. if (data->reset) {
  280. g_assert(ctx->resetting);
  281. if (data->cb) {
  282. data->cb(ctx->id);
  283. }
  284. ctx->resetting = false;
  285. g_free(data);
  286. return;
  287. }
  288. g_assert(ctx->uninstalling);
  289. /* we cannot dlclose if we are going to return to plugin code */
  290. if (ctx->installing) {
  291. error_report("Calling qemu_plugin_uninstall from the install function "
  292. "is a bug. Instead, return !0 from the install function.");
  293. abort();
  294. }
  295. success = g_hash_table_remove(plugin.id_ht, &ctx->id);
  296. g_assert(success);
  297. QTAILQ_REMOVE(&plugin.ctxs, ctx, entry);
  298. if (data->cb) {
  299. data->cb(ctx->id);
  300. }
  301. if (!g_module_close(ctx->handle)) {
  302. warn_report("%s: %s", __func__, g_module_error());
  303. }
  304. plugin_desc_free(ctx->desc);
  305. qemu_vfree(ctx);
  306. g_free(data);
  307. }
  308. static void plugin_reset_destroy(struct qemu_plugin_reset_data *data)
  309. {
  310. qemu_rec_mutex_lock(&plugin.lock);
  311. plugin_reset_destroy__locked(data);
  312. qemu_rec_mutex_lock(&plugin.lock);
  313. }
  314. static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg)
  315. {
  316. struct qemu_plugin_reset_data *data = arg.host_ptr;
  317. g_assert(cpu_in_exclusive_context(cpu));
  318. tb_flush(cpu);
  319. plugin_reset_destroy(data);
  320. }
  321. void plugin_reset_uninstall(qemu_plugin_id_t id,
  322. qemu_plugin_simple_cb_t cb,
  323. bool reset)
  324. {
  325. struct qemu_plugin_reset_data *data;
  326. struct qemu_plugin_ctx *ctx;
  327. WITH_QEMU_LOCK_GUARD(&plugin.lock) {
  328. ctx = plugin_id_to_ctx_locked(id);
  329. if (ctx->uninstalling || (reset && ctx->resetting)) {
  330. return;
  331. }
  332. ctx->resetting = reset;
  333. ctx->uninstalling = !reset;
  334. }
  335. data = g_new(struct qemu_plugin_reset_data, 1);
  336. data->ctx = ctx;
  337. data->cb = cb;
  338. data->reset = reset;
  339. /*
  340. * Only flush the code cache if the vCPUs have been created. If so,
  341. * current_cpu must be non-NULL.
  342. */
  343. if (current_cpu) {
  344. async_safe_run_on_cpu(current_cpu, plugin_flush_destroy,
  345. RUN_ON_CPU_HOST_PTR(data));
  346. } else {
  347. /*
  348. * If current_cpu isn't set, then we don't have yet any vCPU threads
  349. * and we therefore can remove the callbacks synchronously.
  350. */
  351. plugin_reset_destroy(data);
  352. }
  353. }