hw/mips/cps: Expose input clock and connect it to CPU cores
[qemu/ar7.git] / util / module.c
blobf0ed05fbd06b87b8bd85c4e59a8556f5781b7577
1 /*
2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
6 * Authors:
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 #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
27 typedef struct ModuleEntry
29 void (*init)(void);
30 QTAILQ_ENTRY(ModuleEntry) node;
31 module_init_type type;
32 } ModuleEntry;
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)
43 static int inited;
44 int i;
46 if (inited) {
47 return;
50 for (i = 0; i < MODULE_INIT_MAX; i++) {
51 QTAILQ_INIT(&init_type_list[i]);
54 QTAILQ_INIT(&dso_init_list);
56 inited = 1;
60 static ModuleTypeList *find_type(module_init_type type)
62 init_lists();
64 return &init_type_list[type];
67 void register_module_init(void (*fn)(void), module_init_type type)
69 ModuleEntry *e;
70 ModuleTypeList *l;
72 e = g_malloc0(sizeof(*e));
73 e->init = fn;
74 e->type = type;
76 l = find_type(type);
78 QTAILQ_INSERT_TAIL(l, e, node);
81 void register_dso_module_init(void (*fn)(void), module_init_type type)
83 ModuleEntry *e;
85 init_lists();
87 e = g_malloc0(sizeof(*e));
88 e->init = fn;
89 e->type = type;
91 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
94 void module_call_init(module_init_type type)
96 ModuleTypeList *l;
97 ModuleEntry *e;
99 if (modules_init_done[type]) {
100 return;
103 l = find_type(type);
105 QTAILQ_FOREACH(e, l, node) {
106 e->init();
109 modules_init_done[type] = true;
112 #ifdef CONFIG_MODULES
113 static int module_load_file(const char *fname, bool mayfail)
115 GModule *g_module;
116 void (*sym)(void);
117 const char *dsosuf = CONFIG_HOST_DSOSUF;
118 int len = strlen(fname);
119 int suf_len = strlen(dsosuf);
120 ModuleEntry *e, *next;
121 int ret;
123 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
124 /* wrong suffix */
125 ret = -EINVAL;
126 goto out;
128 if (access(fname, F_OK)) {
129 ret = -ENOENT;
130 goto out;
133 assert(QTAILQ_EMPTY(&dso_init_list));
135 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
136 if (!g_module) {
137 if (!mayfail) {
138 fprintf(stderr, "Failed to open module: %s\n",
139 g_module_error());
141 ret = -EINVAL;
142 goto out;
144 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
145 fprintf(stderr, "Failed to initialize module: %s\n",
146 fname);
147 /* Print some info if this is a QEMU module (but from different build),
148 * this will make debugging user problems easier. */
149 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
150 fprintf(stderr,
151 "Note: only modules from the same build can be loaded.\n");
153 g_module_close(g_module);
154 ret = -EINVAL;
155 } else {
156 QTAILQ_FOREACH(e, &dso_init_list, node) {
157 e->init();
158 register_module_init(e->init, e->type);
160 ret = 0;
163 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
164 QTAILQ_REMOVE(&dso_init_list, e, node);
165 g_free(e);
167 out:
168 return ret;
170 #endif
172 bool module_load_one(const char *prefix, const char *lib_name, bool mayfail)
174 bool success = false;
176 #ifdef CONFIG_MODULES
177 char *fname = NULL;
178 #ifdef CONFIG_MODULE_UPGRADES
179 char *version_dir;
180 #endif
181 const char *search_dir;
182 char *dirs[5];
183 char *module_name;
184 int i = 0, n_dirs = 0;
185 int ret;
186 static GHashTable *loaded_modules;
188 if (!g_module_supported()) {
189 fprintf(stderr, "Module is not supported by system.\n");
190 return false;
193 if (!loaded_modules) {
194 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
197 module_name = g_strdup_printf("%s%s", prefix, lib_name);
199 if (!g_hash_table_add(loaded_modules, module_name)) {
200 g_free(module_name);
201 return true;
204 search_dir = getenv("QEMU_MODULE_DIR");
205 if (search_dir != NULL) {
206 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
208 dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
209 dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
211 #ifdef CONFIG_MODULE_UPGRADES
212 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
213 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
214 '_');
215 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
216 #endif
218 assert(n_dirs <= ARRAY_SIZE(dirs));
220 for (i = 0; i < n_dirs; i++) {
221 fname = g_strdup_printf("%s/%s%s",
222 dirs[i], module_name, CONFIG_HOST_DSOSUF);
223 ret = module_load_file(fname, mayfail);
224 g_free(fname);
225 fname = NULL;
226 /* Try loading until loaded a module file */
227 if (!ret) {
228 success = true;
229 break;
233 if (!success) {
234 g_hash_table_remove(loaded_modules, module_name);
235 g_free(module_name);
238 for (i = 0; i < n_dirs; i++) {
239 g_free(dirs[i]);
242 #endif
243 return success;
247 * Building devices and other qom objects modular is mostly useful in
248 * case they have dependencies to external shared libraries, so we can
249 * cut down the core qemu library dependencies. Which is the case for
250 * only a very few devices & objects.
252 * So with the expectation that this will be rather the exception than
253 * the rule and the list will not gain that many entries, go with a
254 * simple manually maintained list for now.
256 * The list must be sorted by module (module_load_qom_all() needs this).
258 static struct {
259 const char *type;
260 const char *prefix;
261 const char *module;
262 } const qom_modules[] = {
263 { "ccid-card-passthru", "hw-", "usb-smartcard" },
264 { "ccid-card-emulated", "hw-", "usb-smartcard" },
265 { "usb-redir", "hw-", "usb-redirect" },
266 { "qxl-vga", "hw-", "display-qxl" },
267 { "qxl", "hw-", "display-qxl" },
268 { "virtio-gpu-device", "hw-", "display-virtio-gpu" },
269 { "vhost-user-gpu", "hw-", "display-virtio-gpu" },
270 { "chardev-braille", "chardev-", "baum" },
271 { "chardev-spicevmc", "chardev-", "spice" },
272 { "chardev-spiceport", "chardev-", "spice" },
275 static bool module_loaded_qom_all;
277 void module_load_qom_one(const char *type)
279 int i;
281 if (!type) {
282 return;
284 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
285 if (strcmp(qom_modules[i].type, type) == 0) {
286 module_load_one(qom_modules[i].prefix,
287 qom_modules[i].module,
288 false);
289 return;
294 void module_load_qom_all(void)
296 int i;
298 if (module_loaded_qom_all) {
299 return;
301 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
302 if (i > 0 && (strcmp(qom_modules[i - 1].module,
303 qom_modules[i].module) == 0 &&
304 strcmp(qom_modules[i - 1].prefix,
305 qom_modules[i].prefix) == 0)) {
306 /* one module implementing multiple types -> load only once */
307 continue;
309 module_load_one(qom_modules[i].prefix, qom_modules[i].module, true);
311 module_loaded_qom_all = true;