2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
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"
27 typedef struct ModuleEntry
30 QTAILQ_ENTRY(ModuleEntry
) node
;
31 module_init_type type
;
34 typedef QTAILQ_HEAD(, ModuleEntry
) ModuleTypeList
;
36 static ModuleTypeList init_type_list
[MODULE_INIT_MAX
];
37 static bool modules_init_done
[MODULE_INIT_MAX
];
39 static ModuleTypeList dso_init_list
;
41 static void init_lists(void)
50 for (i
= 0; i
< MODULE_INIT_MAX
; i
++) {
51 QTAILQ_INIT(&init_type_list
[i
]);
54 QTAILQ_INIT(&dso_init_list
);
60 static ModuleTypeList
*find_type(module_init_type type
)
64 return &init_type_list
[type
];
67 void register_module_init(void (*fn
)(void), module_init_type type
)
72 e
= g_malloc0(sizeof(*e
));
78 QTAILQ_INSERT_TAIL(l
, e
, node
);
81 void register_dso_module_init(void (*fn
)(void), module_init_type type
)
87 e
= g_malloc0(sizeof(*e
));
91 QTAILQ_INSERT_TAIL(&dso_init_list
, e
, node
);
94 void module_call_init(module_init_type type
)
99 if (modules_init_done
[type
]) {
105 QTAILQ_FOREACH(e
, l
, node
) {
109 modules_init_done
[type
] = true;
112 #ifdef CONFIG_MODULES
113 static int module_load_file(const char *fname
, bool mayfail
, bool export_symbols
)
117 const char *dsosuf
= CONFIG_HOST_DSOSUF
;
118 int len
= strlen(fname
);
119 int suf_len
= strlen(dsosuf
);
120 ModuleEntry
*e
, *next
;
123 if (len
<= suf_len
|| strcmp(&fname
[len
- suf_len
], dsosuf
)) {
128 if (access(fname
, F_OK
)) {
133 assert(QTAILQ_EMPTY(&dso_init_list
));
136 if (!export_symbols
) {
137 flags
|= G_MODULE_BIND_LOCAL
;
139 g_module
= g_module_open(fname
, flags
);
142 fprintf(stderr
, "Failed to open module: %s\n",
148 if (!g_module_symbol(g_module
, DSO_STAMP_FUN_STR
, (gpointer
*)&sym
)) {
149 fprintf(stderr
, "Failed to initialize module: %s\n",
151 /* Print some info if this is a QEMU module (but from different build),
152 * this will make debugging user problems easier. */
153 if (g_module_symbol(g_module
, "qemu_module_dummy", (gpointer
*)&sym
)) {
155 "Note: only modules from the same build can be loaded.\n");
157 g_module_close(g_module
);
160 QTAILQ_FOREACH(e
, &dso_init_list
, node
) {
162 register_module_init(e
->init
, e
->type
);
167 QTAILQ_FOREACH_SAFE(e
, &dso_init_list
, node
, next
) {
168 QTAILQ_REMOVE(&dso_init_list
, e
, node
);
175 static const struct {
179 { "audio-spice", "ui-spice-core" },
180 { "chardev-spice", "ui-spice-core" },
181 { "hw-display-qxl", "ui-spice-core" },
182 { "ui-spice-app", "ui-spice-core" },
183 { "ui-spice-app", "chardev-spice" },
185 { "hw-display-virtio-gpu-gl", "hw-display-virtio-gpu" },
186 { "hw-display-virtio-gpu-pci-gl", "hw-display-virtio-gpu-pci" },
187 { "hw-display-virtio-vga-gl", "hw-display-virtio-vga" },
190 { "ui-egl-headless", "ui-opengl" },
191 { "ui-gtk", "ui-opengl" },
192 { "ui-sdl", "ui-opengl" },
193 { "ui-spice-core", "ui-opengl" },
198 bool module_load_one(const char *prefix
, const char *lib_name
, bool mayfail
)
200 bool success
= false;
202 #ifdef CONFIG_MODULES
204 #ifdef CONFIG_MODULE_UPGRADES
207 const char *search_dir
;
210 int i
= 0, n_dirs
= 0;
212 bool export_symbols
= false;
213 static GHashTable
*loaded_modules
;
215 if (!g_module_supported()) {
216 fprintf(stderr
, "Module is not supported by system.\n");
220 if (!loaded_modules
) {
221 loaded_modules
= g_hash_table_new(g_str_hash
, g_str_equal
);
224 module_name
= g_strdup_printf("%s%s", prefix
, lib_name
);
226 for (dep
= 0; dep
< ARRAY_SIZE(module_deps
); dep
++) {
227 if (strcmp(module_name
, module_deps
[dep
].name
) == 0) {
228 /* we depend on another module */
229 module_load_one("", module_deps
[dep
].dep
, false);
231 if (strcmp(module_name
, module_deps
[dep
].dep
) == 0) {
232 /* another module depends on us */
233 export_symbols
= true;
237 if (g_hash_table_contains(loaded_modules
, module_name
)) {
241 g_hash_table_add(loaded_modules
, module_name
);
243 search_dir
= getenv("QEMU_MODULE_DIR");
244 if (search_dir
!= NULL
) {
245 dirs
[n_dirs
++] = g_strdup_printf("%s", search_dir
);
247 dirs
[n_dirs
++] = get_relocated_path(CONFIG_QEMU_MODDIR
);
248 dirs
[n_dirs
++] = g_strdup(qemu_get_exec_dir());
250 #ifdef CONFIG_MODULE_UPGRADES
251 version_dir
= g_strcanon(g_strdup(QEMU_PKGVERSION
),
252 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS
"+-.~",
254 dirs
[n_dirs
++] = g_strdup_printf("/var/run/qemu/%s", version_dir
);
257 assert(n_dirs
<= ARRAY_SIZE(dirs
));
259 for (i
= 0; i
< n_dirs
; i
++) {
260 fname
= g_strdup_printf("%s/%s%s",
261 dirs
[i
], module_name
, CONFIG_HOST_DSOSUF
);
262 ret
= module_load_file(fname
, mayfail
, export_symbols
);
265 /* Try loading until loaded a module file */
273 g_hash_table_remove(loaded_modules
, module_name
);
277 for (i
= 0; i
< n_dirs
; i
++) {
286 * Building devices and other qom objects modular is mostly useful in
287 * case they have dependencies to external shared libraries, so we can
288 * cut down the core qemu library dependencies. Which is the case for
289 * only a very few devices & objects.
291 * So with the expectation that this will be rather the exception than
292 * the rule and the list will not gain that many entries, go with a
293 * simple manually maintained list for now.
295 * The list must be sorted by module (module_load_qom_all() needs this).
301 } const qom_modules
[] = {
302 { "ccid-card-passthru", "hw-", "usb-smartcard" },
303 { "ccid-card-emulated", "hw-", "usb-smartcard" },
304 { "usb-redir", "hw-", "usb-redirect" },
305 { "qxl-vga", "hw-", "display-qxl" },
306 { "qxl", "hw-", "display-qxl" },
307 { "virtio-gpu-device", "hw-", "display-virtio-gpu" },
308 { "virtio-gpu-gl-device", "hw-", "display-virtio-gpu-gl" },
309 { "vhost-user-gpu", "hw-", "display-virtio-gpu" },
310 { "virtio-gpu-pci-base", "hw-", "display-virtio-gpu-pci" },
311 { "virtio-gpu-pci", "hw-", "display-virtio-gpu-pci" },
312 { "virtio-gpu-gl-pci", "hw-", "display-virtio-gpu-pci-gl" },
313 { "vhost-user-gpu-pci", "hw-", "display-virtio-gpu-pci" },
314 { "virtio-gpu-ccw", "hw-", "s390x-virtio-gpu-ccw" },
315 { "virtio-vga-base", "hw-", "display-virtio-vga" },
316 { "virtio-vga", "hw-", "display-virtio-vga" },
317 { "virtio-vga-gl", "hw-", "display-virtio-vga-gl" },
318 { "vhost-user-vga", "hw-", "display-virtio-vga" },
319 { "chardev-braille", "chardev-", "baum" },
320 { "chardev-spicevmc", "chardev-", "spice" },
321 { "chardev-spiceport", "chardev-", "spice" },
324 static bool module_loaded_qom_all
;
326 void module_load_qom_one(const char *type
)
333 for (i
= 0; i
< ARRAY_SIZE(qom_modules
); i
++) {
334 if (strcmp(qom_modules
[i
].type
, type
) == 0) {
335 module_load_one(qom_modules
[i
].prefix
,
336 qom_modules
[i
].module
,
343 void module_load_qom_all(void)
347 if (module_loaded_qom_all
) {
350 for (i
= 0; i
< ARRAY_SIZE(qom_modules
); i
++) {
351 if (i
> 0 && (strcmp(qom_modules
[i
- 1].module
,
352 qom_modules
[i
].module
) == 0 &&
353 strcmp(qom_modules
[i
- 1].prefix
,
354 qom_modules
[i
].prefix
) == 0)) {
355 /* one module implementing multiple types -> load only once */
358 module_load_one(qom_modules
[i
].prefix
, qom_modules
[i
].module
, true);
360 module_loaded_qom_all
= true;