loader.c 11 KB

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