loader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "qemu/help_option.h"
  21. #include "qapi/error.h"
  22. #include "qemu/lockable.h"
  23. #include "qemu/option.h"
  24. #include "qemu/rcu_queue.h"
  25. #include "qemu/qht.h"
  26. #include "qemu/bitmap.h"
  27. #include "qemu/cacheinfo.h"
  28. #include "qemu/xxhash.h"
  29. #include "qemu/plugin.h"
  30. #include "qemu/memalign.h"
  31. #include "hw/core/cpu.h"
  32. #include "exec/tb-flush.h"
  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. bool is_on;
  84. char *fullarg;
  85. if (is_help_option(value)) {
  86. printf("Plugin options\n");
  87. printf(" file=<path/to/plugin.so>\n");
  88. printf(" plugin specific arguments\n");
  89. exit(0);
  90. } else if (strcmp(name, "file") == 0) {
  91. if (strcmp(value, "") == 0) {
  92. error_setg(errp, "requires a non-empty argument");
  93. return 1;
  94. }
  95. p = plugin_find_desc(arg->head, value);
  96. if (p == NULL) {
  97. p = g_new0(struct qemu_plugin_desc, 1);
  98. p->path = g_strdup(value);
  99. QTAILQ_INSERT_TAIL(arg->head, p, entry);
  100. }
  101. arg->curr = p;
  102. } else {
  103. if (arg->curr == NULL) {
  104. error_setg(errp, "missing earlier '-plugin file=' option");
  105. return 1;
  106. }
  107. if (g_strcmp0(name, "arg") == 0 &&
  108. !qapi_bool_parse(name, value, &is_on, NULL)) {
  109. if (strchr(value, '=') == NULL) {
  110. /* Will treat arg="argname" as "argname=on" */
  111. fullarg = g_strdup_printf("%s=%s", value, "on");
  112. } else {
  113. fullarg = g_strdup(value);
  114. }
  115. warn_report("using 'arg=%s' is deprecated", value);
  116. error_printf("Please use '%s' directly\n", fullarg);
  117. } else {
  118. fullarg = g_strdup_printf("%s=%s", name, value);
  119. }
  120. p = arg->curr;
  121. p->argc++;
  122. p->argv = g_realloc_n(p->argv, p->argc, sizeof(char *));
  123. p->argv[p->argc - 1] = fullarg;
  124. }
  125. return 0;
  126. }
  127. void qemu_plugin_opt_parse(const char *optstr, QemuPluginList *head)
  128. {
  129. struct qemu_plugin_parse_arg arg;
  130. QemuOpts *opts;
  131. opts = qemu_opts_parse_noisily(qemu_find_opts("plugin"), optstr, true);
  132. if (opts == NULL) {
  133. exit(1);
  134. }
  135. arg.head = head;
  136. arg.curr = NULL;
  137. qemu_opt_foreach(opts, plugin_add, &arg, &error_fatal);
  138. qemu_opts_del(opts);
  139. }
  140. /*
  141. * From: https://en.wikipedia.org/wiki/Xorshift
  142. * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
  143. * guaranteed to be >= INT_MAX).
  144. */
  145. static uint64_t xorshift64star(uint64_t x)
  146. {
  147. x ^= x >> 12; /* a */
  148. x ^= x << 25; /* b */
  149. x ^= x >> 27; /* c */
  150. return x * UINT64_C(2685821657736338717);
  151. }
  152. /*
  153. * Disable CFI checks.
  154. * The install and version functions have been loaded from an external library
  155. * so we do not have type information
  156. */
  157. QEMU_DISABLE_CFI
  158. static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, Error **errp)
  159. {
  160. qemu_plugin_install_func_t install;
  161. struct qemu_plugin_ctx *ctx;
  162. gpointer sym;
  163. int rc;
  164. ctx = qemu_memalign(qemu_dcache_linesize, sizeof(*ctx));
  165. memset(ctx, 0, sizeof(*ctx));
  166. ctx->desc = desc;
  167. ctx->handle = g_module_open(desc->path, G_MODULE_BIND_LOCAL);
  168. if (ctx->handle == NULL) {
  169. error_setg(errp, "Could not load plugin %s: %s", desc->path, g_module_error());
  170. goto err_dlopen;
  171. }
  172. if (!g_module_symbol(ctx->handle, "qemu_plugin_install", &sym)) {
  173. error_setg(errp, "Could not load plugin %s: %s", desc->path, g_module_error());
  174. goto err_symbol;
  175. }
  176. install = (qemu_plugin_install_func_t) sym;
  177. /* symbol was found; it could be NULL though */
  178. if (install == NULL) {
  179. error_setg(errp, "Could not load plugin %s: qemu_plugin_install is NULL",
  180. desc->path);
  181. goto err_symbol;
  182. }
  183. if (!g_module_symbol(ctx->handle, "qemu_plugin_version", &sym)) {
  184. error_setg(errp, "Could not load plugin %s: plugin does not declare API version %s",
  185. desc->path, g_module_error());
  186. goto err_symbol;
  187. } else {
  188. int version = *(int *)sym;
  189. if (version < QEMU_PLUGIN_MIN_VERSION) {
  190. error_setg(errp, "Could not load plugin %s: plugin requires API version %d, but "
  191. "this QEMU supports only a minimum version of %d",
  192. desc->path, version, QEMU_PLUGIN_MIN_VERSION);
  193. goto err_symbol;
  194. } else if (version > QEMU_PLUGIN_VERSION) {
  195. error_setg(errp, "Could not load plugin %s: plugin requires API version %d, but "
  196. "this QEMU supports only up to version %d",
  197. desc->path, version, QEMU_PLUGIN_VERSION);
  198. goto err_symbol;
  199. }
  200. }
  201. qemu_rec_mutex_lock(&plugin.lock);
  202. /* find an unused random id with &ctx as the seed */
  203. ctx->id = (uint64_t)(uintptr_t)ctx;
  204. for (;;) {
  205. void *existing;
  206. ctx->id = xorshift64star(ctx->id);
  207. existing = g_hash_table_lookup(plugin.id_ht, &ctx->id);
  208. if (likely(existing == NULL)) {
  209. bool success;
  210. success = g_hash_table_insert(plugin.id_ht, &ctx->id, &ctx->id);
  211. g_assert(success);
  212. break;
  213. }
  214. }
  215. QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry);
  216. ctx->installing = true;
  217. rc = install(ctx->id, info, desc->argc, desc->argv);
  218. ctx->installing = false;
  219. if (rc) {
  220. error_setg(errp, "Could not load plugin %s: qemu_plugin_install returned error code %d",
  221. desc->path, rc);
  222. /*
  223. * we cannot rely on the plugin doing its own cleanup, so
  224. * call a full uninstall if the plugin did not yet call it.
  225. */
  226. if (!ctx->uninstalling) {
  227. plugin_reset_uninstall(ctx->id, NULL, false);
  228. }
  229. }
  230. qemu_rec_mutex_unlock(&plugin.lock);
  231. return rc;
  232. err_symbol:
  233. g_module_close(ctx->handle);
  234. err_dlopen:
  235. qemu_vfree(ctx);
  236. return 1;
  237. }
  238. /* call after having removed @desc from the list */
  239. static void plugin_desc_free(struct qemu_plugin_desc *desc)
  240. {
  241. int i;
  242. for (i = 0; i < desc->argc; i++) {
  243. g_free(desc->argv[i]);
  244. }
  245. g_free(desc->argv);
  246. g_free(desc->path);
  247. g_free(desc);
  248. }
  249. /**
  250. * qemu_plugin_load_list - load a list of plugins
  251. * @head: head of the list of descriptors of the plugins to be loaded
  252. *
  253. * Returns 0 if all plugins in the list are installed, !0 otherwise.
  254. *
  255. * Note: the descriptor of each successfully installed plugin is removed
  256. * from the list given by @head.
  257. */
  258. int qemu_plugin_load_list(QemuPluginList *head, Error **errp)
  259. {
  260. struct qemu_plugin_desc *desc, *next;
  261. g_autofree qemu_info_t *info = g_new0(qemu_info_t, 1);
  262. info->target_name = target_name();
  263. info->version.min = QEMU_PLUGIN_MIN_VERSION;
  264. info->version.cur = QEMU_PLUGIN_VERSION;
  265. qemu_plugin_fillin_mode_info(info);
  266. QTAILQ_FOREACH_SAFE(desc, head, entry, next) {
  267. int err;
  268. err = plugin_load(desc, info, errp);
  269. if (err) {
  270. return err;
  271. }
  272. QTAILQ_REMOVE(head, desc, entry);
  273. }
  274. return 0;
  275. }
  276. struct qemu_plugin_reset_data {
  277. struct qemu_plugin_ctx *ctx;
  278. qemu_plugin_simple_cb_t cb;
  279. bool reset;
  280. };
  281. static void plugin_reset_destroy__locked(struct qemu_plugin_reset_data *data)
  282. {
  283. struct qemu_plugin_ctx *ctx = data->ctx;
  284. enum qemu_plugin_event ev;
  285. bool success;
  286. /*
  287. * After updating the subscription lists there is no need to wait for an RCU
  288. * grace period to elapse, because right now we either are in a "safe async"
  289. * work environment (i.e. all vCPUs are asleep), or no vCPUs have yet been
  290. * created.
  291. */
  292. for (ev = 0; ev < QEMU_PLUGIN_EV_MAX; ev++) {
  293. plugin_unregister_cb__locked(ctx, ev);
  294. }
  295. if (data->reset) {
  296. g_assert(ctx->resetting);
  297. if (data->cb) {
  298. data->cb(ctx->id);
  299. }
  300. ctx->resetting = false;
  301. g_free(data);
  302. return;
  303. }
  304. g_assert(ctx->uninstalling);
  305. /* we cannot dlclose if we are going to return to plugin code */
  306. if (ctx->installing) {
  307. error_report("Calling qemu_plugin_uninstall from the install function "
  308. "is a bug. Instead, return !0 from the install function.");
  309. abort();
  310. }
  311. success = g_hash_table_remove(plugin.id_ht, &ctx->id);
  312. g_assert(success);
  313. QTAILQ_REMOVE(&plugin.ctxs, ctx, entry);
  314. if (data->cb) {
  315. data->cb(ctx->id);
  316. }
  317. if (!g_module_close(ctx->handle)) {
  318. warn_report("%s: %s", __func__, g_module_error());
  319. }
  320. plugin_desc_free(ctx->desc);
  321. qemu_vfree(ctx);
  322. g_free(data);
  323. }
  324. static void plugin_reset_destroy(struct qemu_plugin_reset_data *data)
  325. {
  326. qemu_rec_mutex_lock(&plugin.lock);
  327. plugin_reset_destroy__locked(data);
  328. qemu_rec_mutex_unlock(&plugin.lock);
  329. }
  330. static void plugin_flush_destroy(CPUState *cpu, run_on_cpu_data arg)
  331. {
  332. struct qemu_plugin_reset_data *data = arg.host_ptr;
  333. g_assert(cpu_in_exclusive_context(cpu));
  334. tb_flush(cpu);
  335. plugin_reset_destroy(data);
  336. }
  337. void plugin_reset_uninstall(qemu_plugin_id_t id,
  338. qemu_plugin_simple_cb_t cb,
  339. bool reset)
  340. {
  341. struct qemu_plugin_reset_data *data;
  342. struct qemu_plugin_ctx *ctx = NULL;
  343. WITH_QEMU_LOCK_GUARD(&plugin.lock) {
  344. ctx = plugin_id_to_ctx_locked(id);
  345. if (ctx->uninstalling || (reset && ctx->resetting)) {
  346. return;
  347. }
  348. ctx->resetting = reset;
  349. ctx->uninstalling = !reset;
  350. }
  351. data = g_new(struct qemu_plugin_reset_data, 1);
  352. data->ctx = ctx;
  353. data->cb = cb;
  354. data->reset = reset;
  355. /*
  356. * Only flush the code cache if the vCPUs have been created. If so,
  357. * current_cpu must be non-NULL.
  358. */
  359. if (current_cpu) {
  360. async_safe_run_on_cpu(current_cpu, plugin_flush_destroy,
  361. RUN_ON_CPU_HOST_PTR(data));
  362. } else {
  363. /*
  364. * If current_cpu isn't set, then we don't have yet any vCPU threads
  365. * and we therefore can remove the callbacks synchronously.
  366. */
  367. plugin_reset_destroy(data);
  368. }
  369. }