loader.c 12 KB

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