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"
17 #include "qemu-common.h"
21 #include "qemu/queue.h"
22 #include "qemu/module.h"
24 typedef struct ModuleEntry
27 QTAILQ_ENTRY(ModuleEntry
) node
;
28 module_init_type type
;
31 typedef QTAILQ_HEAD(, ModuleEntry
) ModuleTypeList
;
33 static ModuleTypeList init_type_list
[MODULE_INIT_MAX
];
35 static ModuleTypeList dso_init_list
;
37 static void init_lists(void)
46 for (i
= 0; i
< MODULE_INIT_MAX
; i
++) {
47 QTAILQ_INIT(&init_type_list
[i
]);
50 QTAILQ_INIT(&dso_init_list
);
56 static ModuleTypeList
*find_type(module_init_type type
)
60 return &init_type_list
[type
];
63 void register_module_init(void (*fn
)(void), module_init_type type
)
68 e
= g_malloc0(sizeof(*e
));
74 QTAILQ_INSERT_TAIL(l
, e
, node
);
77 void register_dso_module_init(void (*fn
)(void), module_init_type type
)
83 e
= g_malloc0(sizeof(*e
));
87 QTAILQ_INSERT_TAIL(&dso_init_list
, e
, node
);
90 static void module_load(module_init_type type
);
92 void module_call_init(module_init_type type
)
100 QTAILQ_FOREACH(e
, l
, node
) {
105 #ifdef CONFIG_MODULES
106 static int module_load_file(const char *fname
)
110 const char *dsosuf
= HOST_DSOSUF
;
111 int len
= strlen(fname
);
112 int suf_len
= strlen(dsosuf
);
113 ModuleEntry
*e
, *next
;
116 if (len
<= suf_len
|| strcmp(&fname
[len
- suf_len
], dsosuf
)) {
121 if (access(fname
, F_OK
)) {
126 assert(QTAILQ_EMPTY(&dso_init_list
));
128 g_module
= g_module_open(fname
, G_MODULE_BIND_LAZY
| G_MODULE_BIND_LOCAL
);
130 fprintf(stderr
, "Failed to open module: %s\n",
135 if (!g_module_symbol(g_module
, DSO_STAMP_FUN_STR
, (gpointer
*)&sym
)) {
136 fprintf(stderr
, "Failed to initialize module: %s\n",
138 /* Print some info if this is a QEMU module (but from different build),
139 * this will make debugging user problems easier. */
140 if (g_module_symbol(g_module
, "qemu_module_dummy", (gpointer
*)&sym
)) {
142 "Note: only modules from the same build can be loaded.\n");
144 g_module_close(g_module
);
147 QTAILQ_FOREACH(e
, &dso_init_list
, node
) {
148 register_module_init(e
->init
, e
->type
);
153 QTAILQ_FOREACH_SAFE(e
, &dso_init_list
, node
, next
) {
154 QTAILQ_REMOVE(&dso_init_list
, e
, node
);
162 static void module_load(module_init_type type
)
164 #ifdef CONFIG_MODULES
167 static const char *block_modules
[] = {
175 if (!g_module_supported()) {
176 fprintf(stderr
, "Module is not supported by system.\n");
181 case MODULE_INIT_BLOCK
:
185 /* no other types have dynamic modules for now*/
189 exec_dir
= qemu_get_exec_dir();
190 dirs
[i
++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR
);
191 dirs
[i
++] = g_strdup_printf("%s/..", exec_dir
? : "");
192 dirs
[i
++] = g_strdup_printf("%s", exec_dir
? : "");
193 assert(i
== ARRAY_SIZE(dirs
));
198 for (i
= 0; i
< ARRAY_SIZE(dirs
); i
++) {
199 fname
= g_strdup_printf("%s/%s%s", dirs
[i
], *mp
, HOST_DSOSUF
);
200 ret
= module_load_file(fname
);
203 /* Try loading until loaded a module file */
210 for (i
= 0; i
< ARRAY_SIZE(dirs
); i
++) {