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