]>
Commit | Line | Data |
---|---|---|
0bfe3ca5 AL |
1 | /* |
2 | * QEMU Module Infrastructure | |
3 | * | |
4 | * Copyright IBM, Corp. 2009 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | * | |
6b620ca3 PB |
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. | |
0bfe3ca5 AL |
14 | */ |
15 | ||
aafd7584 | 16 | #include "qemu/osdep.h" |
aa0d1f44 | 17 | #ifdef CONFIG_MODULES |
e26110cf | 18 | #include <gmodule.h> |
aa0d1f44 | 19 | #endif |
1de7afc9 PB |
20 | #include "qemu/queue.h" |
21 | #include "qemu/module.h" | |
1b934064 | 22 | #include "qemu/cutils.h" |
bd83c861 CE |
23 | #ifdef CONFIG_MODULE_UPGRADES |
24 | #include "qemu-version.h" | |
25 | #endif | |
0bfe3ca5 AL |
26 | |
27 | typedef struct ModuleEntry | |
28 | { | |
0bfe3ca5 | 29 | void (*init)(void); |
72cf2d4f | 30 | QTAILQ_ENTRY(ModuleEntry) node; |
e26110cf | 31 | module_init_type type; |
0bfe3ca5 AL |
32 | } ModuleEntry; |
33 | ||
72cf2d4f | 34 | typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList; |
0bfe3ca5 | 35 | |
f7897430 | 36 | static ModuleTypeList init_type_list[MODULE_INIT_MAX]; |
46a07579 | 37 | static bool modules_init_done[MODULE_INIT_MAX]; |
0bfe3ca5 | 38 | |
e26110cf FZ |
39 | static ModuleTypeList dso_init_list; |
40 | ||
41 | static void init_lists(void) | |
0bfe3ca5 | 42 | { |
f7897430 AL |
43 | static int inited; |
44 | int i; | |
0bfe3ca5 | 45 | |
f7897430 AL |
46 | if (inited) { |
47 | return; | |
0bfe3ca5 AL |
48 | } |
49 | ||
f7897430 | 50 | for (i = 0; i < MODULE_INIT_MAX; i++) { |
72cf2d4f | 51 | QTAILQ_INIT(&init_type_list[i]); |
f7897430 | 52 | } |
0bfe3ca5 | 53 | |
e26110cf FZ |
54 | QTAILQ_INIT(&dso_init_list); |
55 | ||
f7897430 AL |
56 | inited = 1; |
57 | } | |
0bfe3ca5 | 58 | |
0bfe3ca5 | 59 | |
f7897430 AL |
60 | static ModuleTypeList *find_type(module_init_type type) |
61 | { | |
e26110cf | 62 | init_lists(); |
f7897430 | 63 | |
9be38598 | 64 | return &init_type_list[type]; |
0bfe3ca5 AL |
65 | } |
66 | ||
67 | void register_module_init(void (*fn)(void), module_init_type type) | |
68 | { | |
69 | ModuleEntry *e; | |
70 | ModuleTypeList *l; | |
71 | ||
7267c094 | 72 | e = g_malloc0(sizeof(*e)); |
0bfe3ca5 | 73 | e->init = fn; |
e26110cf | 74 | e->type = type; |
0bfe3ca5 | 75 | |
f7897430 | 76 | l = find_type(type); |
0bfe3ca5 | 77 | |
72cf2d4f | 78 | QTAILQ_INSERT_TAIL(l, e, node); |
0bfe3ca5 AL |
79 | } |
80 | ||
e26110cf FZ |
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 | ||
0bfe3ca5 AL |
94 | void module_call_init(module_init_type type) |
95 | { | |
96 | ModuleTypeList *l; | |
97 | ModuleEntry *e; | |
98 | ||
46a07579 AB |
99 | if (modules_init_done[type]) { |
100 | return; | |
101 | } | |
102 | ||
f7897430 | 103 | l = find_type(type); |
0bfe3ca5 | 104 | |
72cf2d4f | 105 | QTAILQ_FOREACH(e, l, node) { |
0bfe3ca5 AL |
106 | e->init(); |
107 | } | |
46a07579 AB |
108 | |
109 | modules_init_done[type] = true; | |
0bfe3ca5 | 110 | } |
e26110cf FZ |
111 | |
112 | #ifdef CONFIG_MODULES | |
6f13fa7a | 113 | static int module_load_file(const char *fname, bool mayfail, bool export_symbols) |
e26110cf FZ |
114 | { |
115 | GModule *g_module; | |
116 | void (*sym)(void); | |
859aef02 | 117 | const char *dsosuf = CONFIG_HOST_DSOSUF; |
e26110cf FZ |
118 | int len = strlen(fname); |
119 | int suf_len = strlen(dsosuf); | |
120 | ModuleEntry *e, *next; | |
6f13fa7a | 121 | int ret, flags; |
e26110cf FZ |
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 | ||
546323bd | 135 | flags = 0; |
6f13fa7a GH |
136 | if (!export_symbols) { |
137 | flags |= G_MODULE_BIND_LOCAL; | |
138 | } | |
139 | g_module = g_module_open(fname, flags); | |
e26110cf | 140 | if (!g_module) { |
50109320 GH |
141 | if (!mayfail) { |
142 | fprintf(stderr, "Failed to open module: %s\n", | |
143 | g_module_error()); | |
144 | } | |
e26110cf FZ |
145 | ret = -EINVAL; |
146 | goto out; | |
147 | } | |
148 | if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) { | |
149 | fprintf(stderr, "Failed to initialize module: %s\n", | |
150 | fname); | |
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)) { | |
154 | fprintf(stderr, | |
155 | "Note: only modules from the same build can be loaded.\n"); | |
156 | } | |
157 | g_module_close(g_module); | |
158 | ret = -EINVAL; | |
159 | } else { | |
160 | QTAILQ_FOREACH(e, &dso_init_list, node) { | |
88d88798 | 161 | e->init(); |
e26110cf FZ |
162 | register_module_init(e->init, e->type); |
163 | } | |
164 | ret = 0; | |
165 | } | |
166 | ||
167 | QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) { | |
168 | QTAILQ_REMOVE(&dso_init_list, e, node); | |
169 | g_free(e); | |
170 | } | |
171 | out: | |
172 | return ret; | |
173 | } | |
6f13fa7a GH |
174 | |
175 | static const struct { | |
176 | const char *name; | |
177 | const char *dep; | |
178 | } module_deps[] = { | |
4e651514 GH |
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" }, | |
c8263659 | 184 | |
e349693a | 185 | { "hw-display-virtio-gpu-gl", "hw-display-virtio-gpu" }, |
17cdac0b | 186 | { "hw-display-virtio-gpu-pci-gl", "hw-display-virtio-gpu-pci" }, |
b36eb886 | 187 | { "hw-display-virtio-vga-gl", "hw-display-virtio-vga" }, |
e349693a | 188 | |
c8263659 GH |
189 | #ifdef CONFIG_OPENGL |
190 | { "ui-egl-headless", "ui-opengl" }, | |
191 | { "ui-gtk", "ui-opengl" }, | |
192 | { "ui-sdl", "ui-opengl" }, | |
193 | { "ui-spice-core", "ui-opengl" }, | |
194 | #endif | |
6f13fa7a | 195 | }; |
e26110cf FZ |
196 | #endif |
197 | ||
50109320 | 198 | bool module_load_one(const char *prefix, const char *lib_name, bool mayfail) |
e26110cf | 199 | { |
81d8ccb1 MAL |
200 | bool success = false; |
201 | ||
e26110cf FZ |
202 | #ifdef CONFIG_MODULES |
203 | char *fname = NULL; | |
bd83c861 CE |
204 | #ifdef CONFIG_MODULE_UPGRADES |
205 | char *version_dir; | |
206 | #endif | |
900610e6 | 207 | const char *search_dir; |
267514b3 | 208 | char *dirs[5]; |
dffa41b4 | 209 | char *module_name; |
900610e6 | 210 | int i = 0, n_dirs = 0; |
6f13fa7a GH |
211 | int ret, dep; |
212 | bool export_symbols = false; | |
dffa41b4 | 213 | static GHashTable *loaded_modules; |
e26110cf FZ |
214 | |
215 | if (!g_module_supported()) { | |
216 | fprintf(stderr, "Module is not supported by system.\n"); | |
81d8ccb1 | 217 | return false; |
e26110cf FZ |
218 | } |
219 | ||
dffa41b4 FZ |
220 | if (!loaded_modules) { |
221 | loaded_modules = g_hash_table_new(g_str_hash, g_str_equal); | |
222 | } | |
223 | ||
224 | module_name = g_strdup_printf("%s%s", prefix, lib_name); | |
225 | ||
6f13fa7a GH |
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); | |
230 | } | |
231 | if (strcmp(module_name, module_deps[dep].dep) == 0) { | |
232 | /* another module depends on us */ | |
233 | export_symbols = true; | |
234 | } | |
235 | } | |
236 | ||
64e16fbb | 237 | if (g_hash_table_contains(loaded_modules, module_name)) { |
dffa41b4 | 238 | g_free(module_name); |
81d8ccb1 | 239 | return true; |
dffa41b4 | 240 | } |
64e16fbb | 241 | g_hash_table_add(loaded_modules, module_name); |
dffa41b4 | 242 | |
900610e6 | 243 | search_dir = getenv("QEMU_MODULE_DIR"); |
244 | if (search_dir != NULL) { | |
245 | dirs[n_dirs++] = g_strdup_printf("%s", search_dir); | |
246 | } | |
1b934064 | 247 | dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR); |
a4c13869 | 248 | dirs[n_dirs++] = g_strdup(qemu_get_exec_dir()); |
bd83c861 CE |
249 | |
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 "+-.~", | |
253 | '_'); | |
254 | dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir); | |
255 | #endif | |
256 | ||
900610e6 | 257 | assert(n_dirs <= ARRAY_SIZE(dirs)); |
258 | ||
900610e6 | 259 | for (i = 0; i < n_dirs; i++) { |
dffa41b4 | 260 | fname = g_strdup_printf("%s/%s%s", |
859aef02 | 261 | dirs[i], module_name, CONFIG_HOST_DSOSUF); |
6f13fa7a | 262 | ret = module_load_file(fname, mayfail, export_symbols); |
88d88798 MM |
263 | g_free(fname); |
264 | fname = NULL; | |
265 | /* Try loading until loaded a module file */ | |
266 | if (!ret) { | |
81d8ccb1 | 267 | success = true; |
88d88798 | 268 | break; |
e26110cf | 269 | } |
e26110cf FZ |
270 | } |
271 | ||
81d8ccb1 MAL |
272 | if (!success) { |
273 | g_hash_table_remove(loaded_modules, module_name); | |
638be478 | 274 | g_free(module_name); |
81d8ccb1 MAL |
275 | } |
276 | ||
900610e6 | 277 | for (i = 0; i < n_dirs; i++) { |
e26110cf FZ |
278 | g_free(dirs[i]); |
279 | } | |
280 | ||
281 | #endif | |
81d8ccb1 | 282 | return success; |
e26110cf | 283 | } |
28457744 GH |
284 | |
285 | /* | |
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. | |
290 | * | |
291 | * So with the expectation that this will be rather the exception than | |
f88908cf | 292 | * the rule and the list will not gain that many entries, go with a |
28457744 | 293 | * simple manually maintained list for now. |
f88908cf GH |
294 | * |
295 | * The list must be sorted by module (module_load_qom_all() needs this). | |
28457744 GH |
296 | */ |
297 | static struct { | |
298 | const char *type; | |
299 | const char *prefix; | |
300 | const char *module; | |
301 | } const qom_modules[] = { | |
8887312b GH |
302 | { "ccid-card-passthru", "hw-", "usb-smartcard" }, |
303 | { "ccid-card-emulated", "hw-", "usb-smartcard" }, | |
aa9c8573 | 304 | { "usb-redir", "hw-", "usb-redirect" }, |
d39e93d4 GH |
305 | { "qxl-vga", "hw-", "display-qxl" }, |
306 | { "qxl", "hw-", "display-qxl" }, | |
7b0de5b7 | 307 | { "virtio-gpu-device", "hw-", "display-virtio-gpu" }, |
e349693a | 308 | { "virtio-gpu-gl-device", "hw-", "display-virtio-gpu-gl" }, |
7b0de5b7 | 309 | { "vhost-user-gpu", "hw-", "display-virtio-gpu" }, |
74acdf0a GH |
310 | { "virtio-gpu-pci-base", "hw-", "display-virtio-gpu-pci" }, |
311 | { "virtio-gpu-pci", "hw-", "display-virtio-gpu-pci" }, | |
17cdac0b | 312 | { "virtio-gpu-gl-pci", "hw-", "display-virtio-gpu-pci-gl" }, |
74acdf0a | 313 | { "vhost-user-gpu-pci", "hw-", "display-virtio-gpu-pci" }, |
adcf33a5 | 314 | { "virtio-gpu-ccw", "hw-", "s390x-virtio-gpu-ccw" }, |
1e1f9c20 GH |
315 | { "virtio-vga-base", "hw-", "display-virtio-vga" }, |
316 | { "virtio-vga", "hw-", "display-virtio-vga" }, | |
b36eb886 | 317 | { "virtio-vga-gl", "hw-", "display-virtio-vga-gl" }, |
1e1f9c20 | 318 | { "vhost-user-vga", "hw-", "display-virtio-vga" }, |
ef138c77 | 319 | { "chardev-braille", "chardev-", "baum" }, |
23ebeaae GH |
320 | { "chardev-spicevmc", "chardev-", "spice" }, |
321 | { "chardev-spiceport", "chardev-", "spice" }, | |
28457744 GH |
322 | }; |
323 | ||
324 | static bool module_loaded_qom_all; | |
325 | ||
326 | void module_load_qom_one(const char *type) | |
327 | { | |
328 | int i; | |
329 | ||
d87350b0 GH |
330 | if (!type) { |
331 | return; | |
332 | } | |
28457744 GH |
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, | |
50109320 GH |
336 | qom_modules[i].module, |
337 | false); | |
28457744 GH |
338 | return; |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | void module_load_qom_all(void) | |
344 | { | |
345 | int i; | |
346 | ||
347 | if (module_loaded_qom_all) { | |
348 | return; | |
349 | } | |
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 */ | |
356 | continue; | |
357 | } | |
50109320 | 358 | module_load_one(qom_modules[i].prefix, qom_modules[i].module, true); |
28457744 GH |
359 | } |
360 | module_loaded_qom_all = true; | |
361 | } |