2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
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.
18 #include "qemu-common.h"
19 #include "qemu/queue.h"
20 #include "qemu/module.h"
22 typedef struct ModuleEntry
25 QTAILQ_ENTRY(ModuleEntry) node;
26 module_init_type type;
29 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
31 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
33 static ModuleTypeList dso_init_list;
35 static void init_lists(void)
44 for (i = 0; i < MODULE_INIT_MAX; i++) {
45 QTAILQ_INIT(&init_type_list[i]);
48 QTAILQ_INIT(&dso_init_list);
54 static ModuleTypeList *find_type(module_init_type type)
60 l = &init_type_list[type];
65 void register_module_init(void (*fn)(void), module_init_type type)
70 e = g_malloc0(sizeof(*e));
76 QTAILQ_INSERT_TAIL(l, e, node);
79 void register_dso_module_init(void (*fn)(void), module_init_type type)
85 e = g_malloc0(sizeof(*e));
89 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
92 static void module_load(module_init_type type);
94 void module_call_init(module_init_type type)
102 QTAILQ_FOREACH(e, l, node) {
107 #ifdef CONFIG_MODULES
108 static int module_load_file(const char *fname)
112 const char *dsosuf = HOST_DSOSUF;
113 int len = strlen(fname);
114 int suf_len = strlen(dsosuf);
115 ModuleEntry *e, *next;
118 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
123 if (access(fname, F_OK)) {
128 assert(QTAILQ_EMPTY(&dso_init_list));
130 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
132 fprintf(stderr, "Failed to open module: %s\n",
137 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
138 fprintf(stderr, "Failed to initialize module: %s\n",
140 /* Print some info if this is a QEMU module (but from different build),
141 * this will make debugging user problems easier. */
142 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
144 "Note: only modules from the same build can be loaded.\n");
146 g_module_close(g_module);
149 QTAILQ_FOREACH(e, &dso_init_list, node) {
150 register_module_init(e->init, e->type);
155 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
156 QTAILQ_REMOVE(&dso_init_list, e, node);
164 void module_load(module_init_type type)
166 #ifdef CONFIG_MODULES
169 static const char *block_modules[] = {
177 if (!g_module_supported()) {
178 fprintf(stderr, "Module is not supported by system.\n");
183 case MODULE_INIT_BLOCK:
187 /* no other types have dynamic modules for now*/
191 exec_dir = qemu_get_exec_dir();
192 dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
193 dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
194 dirs[i++] = g_strdup_printf("%s", exec_dir ? : "");
195 assert(i == ARRAY_SIZE(dirs));
200 for (i = 0; i < ARRAY_SIZE(dirs); i++) {
201 fname = g_strdup_printf("%s/%s%s", dirs[i], *mp, HOST_DSOSUF);
202 ret = module_load_file(fname);
203 /* Try loading until loaded a module file */
210 if (ret == -ENOENT) {
211 fprintf(stderr, "Can't find module: %s\n", *mp);
217 for (i = 0; i < ARRAY_SIZE(dirs); i++) {