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/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. err_dlopen:
  211. qemu_vfree(ctx);
  212. return 1;
  213. }
  214. /* call after having removed @desc from the list */
  215. static void plugin_desc_free(struct qemu_plugin_desc *desc)
  216. {
  217. int i;
  218. for (i = 0; i < desc->argc; i++) {
  219. g_free(desc->argv[i]);
  220. }
  221. g_free(desc->argv);
  222. g_free(desc->path);
  223. g_free(desc);
  224. }
  225. /**
  226. * qemu_plugin_load_list - load a list of plugins
  227. * @head: head of the list of descriptors of the plugins to be loaded
  228. *
  229. * Returns 0 if all plugins in the list are installed, !0 otherwise.
  230. *
  231. * Note: the descriptor of each successfully installed plugin is removed
  232. * from the list given by @head.
  233. */
  234. int qemu_plugin_load_list(QemuPluginList *head)
  235. {
  236. struct qemu_plugin_desc *desc, *next;
  237. g_autofree qemu_info_t *info = g_new0(qemu_info_t, 1);
  238. info->target_name = TARGET_NAME;
  239. info->version.min = QEMU_PLUGIN_MIN_VERSION;
  240. info->version.cur = QEMU_PLUGIN_VERSION;
  241. #ifndef CONFIG_USER_ONLY
  242. MachineState *ms = MACHINE(qdev_get_machine());
  243. info->system_emulation = true;
  244. info->system.smp_vcpus = ms->smp.cpus;
  245. info->system.max_vcpus = ms->smp.max_cpus;
  246. #else
  247. info->system_emulation = false;
  248. #endif
  249. QTAILQ_FOREACH_SAFE(desc, head, entry, next) {
  250. int err;
  251. err = plugin_load(desc, info);
  252. if (err) {
  253. return err;
  254. }
  255. QTAILQ_REMOVE(head, desc, entry);
  256. }
  257. return 0;
  258. }
  259. struct qemu_plugin_reset_data {
  260. struct qemu_plugin_ctx *ctx;
  261. qemu_plugin_simple_cb_t cb;
  262. bool reset;
  263. };
  264. static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data)
  265. {
  266. struct qemu_plugin_ctx *ctx = data->ctx;
  267. enum qemu_plugin_event ev;
  268. bool success;
  269. /*
  270. * After updating the subscription lists there is no need to wait for an RCU
  271. * grace period to elapse, because right now we either are in a "safe async"
  272. * work environment (i.e. all vCPUs are asleep), or no vCPUs have yet been
  273. * created.
  274. */
  275. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  276. plugin_unregister_cb__locked(ctx, ev);
  277. }
  278. if (data->reset) {
  279. g_assert(ctx->resetting);
  280. if (data->cb) {
  281. data->cb(ctx->id);
  282. }
  283. ctx->resetting = false;
  284. g_free(data);
  285. return;
  286. }
  287. g_assert(ctx->uninstalling);
  288. /* we cannot dlclose if we are going to return to plugin code */
  289. if (ctx->installing) {
  290. error_report("Calling qemu_plugin_uninstall from the install function "
  291. "is a bug. Instead, return !0 from the install function.");
  292. abort();
  293. }
  294. success = g_hash_table_remove(plugin.id_ht, &ctx->id);
  295. g_assert(success);
  296. QTAILQ_REMOVE(&plugin.ctxs, ctx, entry);
  297. if (data->cb) {
  298. data->cb(ctx->id);
  299. }
  300. if (!g_module_close(ctx->handle)) {
  301. warn_report("%s: %s", __func__, g_module_error());
  302. }
  303. plugin_desc_free(ctx->desc);
  304. qemu_vfree(ctx);
  305. g_free(data);
  306. }
  307. static void plugin_reset_destroy(struct qemu_plugin_reset_data *data)
  308. {
  309. qemu_rec_mutex_lock(&plugin.lock);
  310. plugin_reset_destroy__locked(data);
  311. qemu_rec_mutex_lock(&plugin.lock);
  312. }
  313. static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg)
  314. {
  315. struct qemu_plugin_reset_data *data = arg.host_ptr;
  316. g_assert(cpu_in_exclusive_context(cpu));
  317. tb_flush(cpu);
  318. plugin_reset_destroy(data);
  319. }
  320. void plugin_reset_uninstall(qemu_plugin_id_t id,
  321. qemu_plugin_simple_cb_t cb,
  322. bool reset)
  323. {
  324. struct qemu_plugin_reset_data *data;
  325. struct qemu_plugin_ctx *ctx;
  326. WITH_QEMU_LOCK_GUARD(&plugin.lock) {
  327. ctx = plugin_id_to_ctx_locked(id);
  328. if (ctx->uninstalling || (reset && ctx->resetting)) {
  329. return;
  330. }
  331. ctx->resetting = reset;
  332. ctx->uninstalling = !reset;
  333. }
  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. }