xref: /openbmc/qemu/util/module.c (revision 55567891)
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 static int module_load_file(const char *fname)
114 {
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;
122 
123     if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
124         /* wrong suffix */
125         ret = -EINVAL;
126         goto out;
127     }
128     if (access(fname, F_OK)) {
129         ret = -ENOENT;
130         goto out;
131     }
132 
133     assert(QTAILQ_EMPTY(&dso_init_list));
134 
135     g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
136     if (!g_module) {
137         fprintf(stderr, "Failed to open module: %s\n",
138                 g_module_error());
139         ret = -EINVAL;
140         goto out;
141     }
142     if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
143         fprintf(stderr, "Failed to initialize module: %s\n",
144                 fname);
145         /* Print some info if this is a QEMU module (but from different build),
146          * this will make debugging user problems easier. */
147         if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
148             fprintf(stderr,
149                     "Note: only modules from the same build can be loaded.\n");
150         }
151         g_module_close(g_module);
152         ret = -EINVAL;
153     } else {
154         QTAILQ_FOREACH(e, &dso_init_list, node) {
155             e->init();
156             register_module_init(e->init, e->type);
157         }
158         ret = 0;
159     }
160 
161     QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
162         QTAILQ_REMOVE(&dso_init_list, e, node);
163         g_free(e);
164     }
165 out:
166     return ret;
167 }
168 #endif
169 
170 bool module_load_one(const char *prefix, const char *lib_name)
171 {
172     bool success = false;
173 
174 #ifdef CONFIG_MODULES
175     char *fname = NULL;
176 #ifdef CONFIG_MODULE_UPGRADES
177     char *version_dir;
178 #endif
179     const char *search_dir;
180     char *dirs[5];
181     char *module_name;
182     int i = 0, n_dirs = 0;
183     int ret;
184     static GHashTable *loaded_modules;
185 
186     if (!g_module_supported()) {
187         fprintf(stderr, "Module is not supported by system.\n");
188         return false;
189     }
190 
191     if (!loaded_modules) {
192         loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
193     }
194 
195     module_name = g_strdup_printf("%s%s", prefix, lib_name);
196 
197     if (!g_hash_table_add(loaded_modules, module_name)) {
198         g_free(module_name);
199         return true;
200     }
201 
202     search_dir = getenv("QEMU_MODULE_DIR");
203     if (search_dir != NULL) {
204         dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
205     }
206     dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
207     dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
208 
209 #ifdef CONFIG_MODULE_UPGRADES
210     version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
211                              G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
212                              '_');
213     dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
214 #endif
215 
216     assert(n_dirs <= ARRAY_SIZE(dirs));
217 
218     for (i = 0; i < n_dirs; i++) {
219         fname = g_strdup_printf("%s/%s%s",
220                 dirs[i], module_name, CONFIG_HOST_DSOSUF);
221         ret = module_load_file(fname);
222         g_free(fname);
223         fname = NULL;
224         /* Try loading until loaded a module file */
225         if (!ret) {
226             success = true;
227             break;
228         }
229     }
230 
231     if (!success) {
232         g_hash_table_remove(loaded_modules, module_name);
233         g_free(module_name);
234     }
235 
236     for (i = 0; i < n_dirs; i++) {
237         g_free(dirs[i]);
238     }
239 
240 #endif
241     return success;
242 }
243 
244 /*
245  * Building devices and other qom objects modular is mostly useful in
246  * case they have dependencies to external shared libraries, so we can
247  * cut down the core qemu library dependencies.  Which is the case for
248  * only a very few devices & objects.
249  *
250  * So with the expectation that this will be rather the exception than
251  * to rule and the list will not gain that many entries go with a
252  * simple manually maintained list for now.
253  */
254 static struct {
255     const char *type;
256     const char *prefix;
257     const char *module;
258 } const qom_modules[] = {
259     { "ccid-card-passthru",    "hw-", "usb-smartcard"         },
260     { "ccid-card-emulated",    "hw-", "usb-smartcard"         },
261     { "usb-redir",             "hw-", "usb-redirect"          },
262     { "qxl-vga",               "hw-", "display-qxl"           },
263     { "qxl",                   "hw-", "display-qxl"           },
264     { "virtio-gpu-device",     "hw-", "display-virtio-gpu"    },
265     { "vhost-user-gpu",        "hw-", "display-virtio-gpu"    },
266     { "chardev-braille",       "chardev-", "baum"             },
267 };
268 
269 static bool module_loaded_qom_all;
270 
271 void module_load_qom_one(const char *type)
272 {
273     int i;
274 
275     if (!type) {
276         return;
277     }
278     if (module_loaded_qom_all) {
279         return;
280     }
281     for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
282         if (strcmp(qom_modules[i].type, type) == 0) {
283             module_load_one(qom_modules[i].prefix,
284                             qom_modules[i].module);
285             return;
286         }
287     }
288 }
289 
290 void module_load_qom_all(void)
291 {
292     int i;
293 
294     if (module_loaded_qom_all) {
295         return;
296     }
297     for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
298         if (i > 0 && (strcmp(qom_modules[i - 1].module,
299                              qom_modules[i].module) == 0 &&
300                       strcmp(qom_modules[i - 1].prefix,
301                              qom_modules[i].prefix) == 0)) {
302             /* one module implementing multiple types -> load only once */
303             continue;
304         }
305         module_load_one(qom_modules[i].prefix, qom_modules[i].module);
306     }
307     module_loaded_qom_all = true;
308 }
309