1 /* 2 * QEMU Module Infrastructure 3 * 4 * Copyright IBM, Corp. 2009 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Contributions after 2012-01-13 are licensed under the terms of the 13 * GNU GPL, version 2 or (at your option) any later version. 14 */ 15 16 #include "qemu/osdep.h" 17 #ifdef CONFIG_MODULES 18 #include <gmodule.h> 19 #endif 20 #include "qemu/queue.h" 21 #include "qemu/module.h" 22 #include "qemu/cutils.h" 23 #ifdef CONFIG_MODULE_UPGRADES 24 #include "qemu-version.h" 25 #endif 26 27 typedef struct ModuleEntry 28 { 29 void (*init)(void); 30 QTAILQ_ENTRY(ModuleEntry) node; 31 module_init_type type; 32 } ModuleEntry; 33 34 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList; 35 36 static ModuleTypeList init_type_list[MODULE_INIT_MAX]; 37 static bool modules_init_done[MODULE_INIT_MAX]; 38 39 static ModuleTypeList dso_init_list; 40 41 static void init_lists(void) 42 { 43 static int inited; 44 int i; 45 46 if (inited) { 47 return; 48 } 49 50 for (i = 0; i < MODULE_INIT_MAX; i++) { 51 QTAILQ_INIT(&init_type_list[i]); 52 } 53 54 QTAILQ_INIT(&dso_init_list); 55 56 inited = 1; 57 } 58 59 60 static ModuleTypeList *find_type(module_init_type type) 61 { 62 init_lists(); 63 64 return &init_type_list[type]; 65 } 66 67 void register_module_init(void (*fn)(void), module_init_type type) 68 { 69 ModuleEntry *e; 70 ModuleTypeList *l; 71 72 e = g_malloc0(sizeof(*e)); 73 e->init = fn; 74 e->type = type; 75 76 l = find_type(type); 77 78 QTAILQ_INSERT_TAIL(l, e, node); 79 } 80 81 void register_dso_module_init(void (*fn)(void), module_init_type type) 82 { 83 ModuleEntry *e; 84 85 init_lists(); 86 87 e = g_malloc0(sizeof(*e)); 88 e->init = fn; 89 e->type = type; 90 91 QTAILQ_INSERT_TAIL(&dso_init_list, e, node); 92 } 93 94 void module_call_init(module_init_type type) 95 { 96 ModuleTypeList *l; 97 ModuleEntry *e; 98 99 if (modules_init_done[type]) { 100 return; 101 } 102 103 l = find_type(type); 104 105 QTAILQ_FOREACH(e, l, node) { 106 e->init(); 107 } 108 109 modules_init_done[type] = true; 110 } 111 112 #ifdef CONFIG_MODULES 113 114 static const QemuModinfo module_info_stub[] = { { 115 /* end of list */ 116 } }; 117 static const QemuModinfo *module_info = module_info_stub; 118 119 void module_init_info(const QemuModinfo *info) 120 { 121 module_info = info; 122 } 123 124 static int module_load_file(const char *fname, bool mayfail, bool export_symbols) 125 { 126 GModule *g_module; 127 void (*sym)(void); 128 const char *dsosuf = CONFIG_HOST_DSOSUF; 129 int len = strlen(fname); 130 int suf_len = strlen(dsosuf); 131 ModuleEntry *e, *next; 132 int ret, flags; 133 134 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) { 135 /* wrong suffix */ 136 ret = -EINVAL; 137 goto out; 138 } 139 if (access(fname, F_OK)) { 140 ret = -ENOENT; 141 goto out; 142 } 143 144 assert(QTAILQ_EMPTY(&dso_init_list)); 145 146 flags = 0; 147 if (!export_symbols) { 148 flags |= G_MODULE_BIND_LOCAL; 149 } 150 g_module = g_module_open(fname, flags); 151 if (!g_module) { 152 if (!mayfail) { 153 fprintf(stderr, "Failed to open module: %s\n", 154 g_module_error()); 155 } 156 ret = -EINVAL; 157 goto out; 158 } 159 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) { 160 fprintf(stderr, "Failed to initialize module: %s\n", 161 fname); 162 /* Print some info if this is a QEMU module (but from different build), 163 * this will make debugging user problems easier. */ 164 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) { 165 fprintf(stderr, 166 "Note: only modules from the same build can be loaded.\n"); 167 } 168 g_module_close(g_module); 169 ret = -EINVAL; 170 } else { 171 QTAILQ_FOREACH(e, &dso_init_list, node) { 172 e->init(); 173 register_module_init(e->init, e->type); 174 } 175 ret = 0; 176 } 177 178 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) { 179 QTAILQ_REMOVE(&dso_init_list, e, node); 180 g_free(e); 181 } 182 out: 183 return ret; 184 } 185 186 static const struct { 187 const char *name; 188 const char *dep; 189 } module_deps[] = { 190 { "audio-spice", "ui-spice-core" }, 191 { "chardev-spice", "ui-spice-core" }, 192 { "hw-display-qxl", "ui-spice-core" }, 193 { "ui-spice-app", "ui-spice-core" }, 194 { "ui-spice-app", "chardev-spice" }, 195 196 { "hw-display-virtio-gpu-gl", "hw-display-virtio-gpu" }, 197 { "hw-display-virtio-gpu-pci-gl", "hw-display-virtio-gpu-pci" }, 198 { "hw-display-virtio-vga-gl", "hw-display-virtio-vga" }, 199 200 #ifdef CONFIG_OPENGL 201 { "ui-egl-headless", "ui-opengl" }, 202 { "ui-gtk", "ui-opengl" }, 203 { "ui-sdl", "ui-opengl" }, 204 { "ui-spice-core", "ui-opengl" }, 205 #endif 206 }; 207 #endif 208 209 bool module_load_one(const char *prefix, const char *lib_name, bool mayfail) 210 { 211 bool success = false; 212 213 #ifdef CONFIG_MODULES 214 char *fname = NULL; 215 #ifdef CONFIG_MODULE_UPGRADES 216 char *version_dir; 217 #endif 218 const char *search_dir; 219 char *dirs[5]; 220 char *module_name; 221 int i = 0, n_dirs = 0; 222 int ret, dep; 223 bool export_symbols = false; 224 static GHashTable *loaded_modules; 225 226 if (!g_module_supported()) { 227 fprintf(stderr, "Module is not supported by system.\n"); 228 return false; 229 } 230 231 if (!loaded_modules) { 232 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal); 233 } 234 235 module_name = g_strdup_printf("%s%s", prefix, lib_name); 236 237 for (dep = 0; dep < ARRAY_SIZE(module_deps); dep++) { 238 if (strcmp(module_name, module_deps[dep].name) == 0) { 239 /* we depend on another module */ 240 module_load_one("", module_deps[dep].dep, false); 241 } 242 if (strcmp(module_name, module_deps[dep].dep) == 0) { 243 /* another module depends on us */ 244 export_symbols = true; 245 } 246 } 247 248 if (g_hash_table_contains(loaded_modules, module_name)) { 249 g_free(module_name); 250 return true; 251 } 252 g_hash_table_add(loaded_modules, module_name); 253 254 search_dir = getenv("QEMU_MODULE_DIR"); 255 if (search_dir != NULL) { 256 dirs[n_dirs++] = g_strdup_printf("%s", search_dir); 257 } 258 dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR); 259 dirs[n_dirs++] = g_strdup(qemu_get_exec_dir()); 260 261 #ifdef CONFIG_MODULE_UPGRADES 262 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION), 263 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~", 264 '_'); 265 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir); 266 #endif 267 268 assert(n_dirs <= ARRAY_SIZE(dirs)); 269 270 for (i = 0; i < n_dirs; i++) { 271 fname = g_strdup_printf("%s/%s%s", 272 dirs[i], module_name, CONFIG_HOST_DSOSUF); 273 ret = module_load_file(fname, mayfail, export_symbols); 274 g_free(fname); 275 fname = NULL; 276 /* Try loading until loaded a module file */ 277 if (!ret) { 278 success = true; 279 break; 280 } 281 } 282 283 if (!success) { 284 g_hash_table_remove(loaded_modules, module_name); 285 g_free(module_name); 286 } 287 288 for (i = 0; i < n_dirs; i++) { 289 g_free(dirs[i]); 290 } 291 292 #endif 293 return success; 294 } 295 296 /* 297 * Building devices and other qom objects modular is mostly useful in 298 * case they have dependencies to external shared libraries, so we can 299 * cut down the core qemu library dependencies. Which is the case for 300 * only a very few devices & objects. 301 * 302 * So with the expectation that this will be rather the exception than 303 * the rule and the list will not gain that many entries, go with a 304 * simple manually maintained list for now. 305 * 306 * The list must be sorted by module (module_load_qom_all() needs this). 307 */ 308 static struct { 309 const char *type; 310 const char *prefix; 311 const char *module; 312 } const qom_modules[] = { 313 { "ccid-card-passthru", "hw-", "usb-smartcard" }, 314 { "ccid-card-emulated", "hw-", "usb-smartcard" }, 315 { "usb-redir", "hw-", "usb-redirect" }, 316 { "qxl-vga", "hw-", "display-qxl" }, 317 { "qxl", "hw-", "display-qxl" }, 318 { "virtio-gpu-device", "hw-", "display-virtio-gpu" }, 319 { "virtio-gpu-gl-device", "hw-", "display-virtio-gpu-gl" }, 320 { "vhost-user-gpu", "hw-", "display-virtio-gpu" }, 321 { "virtio-gpu-pci-base", "hw-", "display-virtio-gpu-pci" }, 322 { "virtio-gpu-pci", "hw-", "display-virtio-gpu-pci" }, 323 { "virtio-gpu-gl-pci", "hw-", "display-virtio-gpu-pci-gl" }, 324 { "vhost-user-gpu-pci", "hw-", "display-virtio-gpu-pci" }, 325 { "virtio-gpu-ccw", "hw-", "s390x-virtio-gpu-ccw" }, 326 { "virtio-vga-base", "hw-", "display-virtio-vga" }, 327 { "virtio-vga", "hw-", "display-virtio-vga" }, 328 { "virtio-vga-gl", "hw-", "display-virtio-vga-gl" }, 329 { "vhost-user-vga", "hw-", "display-virtio-vga" }, 330 { "chardev-braille", "chardev-", "baum" }, 331 { "chardev-spicevmc", "chardev-", "spice" }, 332 { "chardev-spiceport", "chardev-", "spice" }, 333 }; 334 335 static bool module_loaded_qom_all; 336 337 void module_load_qom_one(const char *type) 338 { 339 int i; 340 341 if (!type) { 342 return; 343 } 344 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) { 345 if (strcmp(qom_modules[i].type, type) == 0) { 346 module_load_one(qom_modules[i].prefix, 347 qom_modules[i].module, 348 false); 349 return; 350 } 351 } 352 } 353 354 void module_load_qom_all(void) 355 { 356 int i; 357 358 if (module_loaded_qom_all) { 359 return; 360 } 361 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) { 362 if (i > 0 && (strcmp(qom_modules[i - 1].module, 363 qom_modules[i].module) == 0 && 364 strcmp(qom_modules[i - 1].prefix, 365 qom_modules[i].prefix) == 0)) { 366 /* one module implementing multiple types -> load only once */ 367 continue; 368 } 369 module_load_one(qom_modules[i].prefix, qom_modules[i].module, true); 370 } 371 module_loaded_qom_all = true; 372 } 373