loader.c 12 KB

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