blob: 1259dd368625871bb25339ed54c6bf0e9133459c [file] [log] [blame]
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +01001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010014 */
15
Peter Maydellaafd7582016-01-29 17:49:55 +000016#include "qemu/osdep.h"
Fam Zhengd844a7b2014-02-27 06:31:33 +080017#include "qemu-common.h"
Paolo Bonziniaa0d1f42014-02-25 17:36:55 +010018#ifdef CONFIG_MODULES
Fam Zhenge26110c2014-02-10 14:48:57 +080019#include <gmodule.h>
Paolo Bonziniaa0d1f42014-02-25 17:36:55 +010020#endif
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/queue.h"
22#include "qemu/module.h"
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010023
24typedef struct ModuleEntry
25{
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010026 void (*init)(void);
Blue Swirl72cf2d42009-09-12 07:36:22 +000027 QTAILQ_ENTRY(ModuleEntry) node;
Fam Zhenge26110c2014-02-10 14:48:57 +080028 module_init_type type;
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010029} ModuleEntry;
30
Blue Swirl72cf2d42009-09-12 07:36:22 +000031typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
Anthony Liguorif7897432009-05-14 17:57:31 -050032
33static ModuleTypeList init_type_list[MODULE_INIT_MAX];
34
Fam Zhenge26110c2014-02-10 14:48:57 +080035static ModuleTypeList dso_init_list;
36
37static void init_lists(void)
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010038{
Anthony Liguorif7897432009-05-14 17:57:31 -050039 static int inited;
40 int i;
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010041
Anthony Liguorif7897432009-05-14 17:57:31 -050042 if (inited) {
43 return;
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010044 }
45
Anthony Liguorif7897432009-05-14 17:57:31 -050046 for (i = 0; i < MODULE_INIT_MAX; i++) {
Blue Swirl72cf2d42009-09-12 07:36:22 +000047 QTAILQ_INIT(&init_type_list[i]);
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010048 }
49
Fam Zhenge26110c2014-02-10 14:48:57 +080050 QTAILQ_INIT(&dso_init_list);
51
Anthony Liguorif7897432009-05-14 17:57:31 -050052 inited = 1;
53}
54
55
56static ModuleTypeList *find_type(module_init_type type)
57{
Fam Zhenge26110c2014-02-10 14:48:57 +080058 init_lists();
Anthony Liguorif7897432009-05-14 17:57:31 -050059
Eduardo Habkost9be38592016-06-13 18:57:58 -030060 return &init_type_list[type];
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010061}
62
63void register_module_init(void (*fn)(void), module_init_type type)
64{
65 ModuleEntry *e;
66 ModuleTypeList *l;
67
Anthony Liguori7267c092011-08-20 22:09:37 -050068 e = g_malloc0(sizeof(*e));
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010069 e->init = fn;
Fam Zhenge26110c2014-02-10 14:48:57 +080070 e->type = type;
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010071
Anthony Liguorif7897432009-05-14 17:57:31 -050072 l = find_type(type);
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010073
Blue Swirl72cf2d42009-09-12 07:36:22 +000074 QTAILQ_INSERT_TAIL(l, e, node);
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010075}
76
Fam Zhenge26110c2014-02-10 14:48:57 +080077void register_dso_module_init(void (*fn)(void), module_init_type type)
78{
79 ModuleEntry *e;
80
81 init_lists();
82
83 e = g_malloc0(sizeof(*e));
84 e->init = fn;
85 e->type = type;
86
87 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
88}
89
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010090void module_call_init(module_init_type type)
91{
92 ModuleTypeList *l;
93 ModuleEntry *e;
94
Anthony Liguorif7897432009-05-14 17:57:31 -050095 l = find_type(type);
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010096
Blue Swirl72cf2d42009-09-12 07:36:22 +000097 QTAILQ_FOREACH(e, l, node) {
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010098 e->init();
99 }
100}
Fam Zhenge26110c2014-02-10 14:48:57 +0800101
102#ifdef CONFIG_MODULES
103static int module_load_file(const char *fname)
104{
105 GModule *g_module;
106 void (*sym)(void);
107 const char *dsosuf = HOST_DSOSUF;
108 int len = strlen(fname);
109 int suf_len = strlen(dsosuf);
110 ModuleEntry *e, *next;
111 int ret;
112
113 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
114 /* wrong suffix */
115 ret = -EINVAL;
116 goto out;
117 }
118 if (access(fname, F_OK)) {
119 ret = -ENOENT;
120 goto out;
121 }
122
123 assert(QTAILQ_EMPTY(&dso_init_list));
124
125 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
126 if (!g_module) {
127 fprintf(stderr, "Failed to open module: %s\n",
128 g_module_error());
129 ret = -EINVAL;
130 goto out;
131 }
132 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
133 fprintf(stderr, "Failed to initialize module: %s\n",
134 fname);
135 /* Print some info if this is a QEMU module (but from different build),
136 * this will make debugging user problems easier. */
137 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
138 fprintf(stderr,
139 "Note: only modules from the same build can be loaded.\n");
140 }
141 g_module_close(g_module);
142 ret = -EINVAL;
143 } else {
144 QTAILQ_FOREACH(e, &dso_init_list, node) {
Marc Mari88d88792016-08-12 09:27:03 -0400145 e->init();
Fam Zhenge26110c2014-02-10 14:48:57 +0800146 register_module_init(e->init, e->type);
147 }
148 ret = 0;
149 }
150
151 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
152 QTAILQ_REMOVE(&dso_init_list, e, node);
153 g_free(e);
154 }
155out:
156 return ret;
157}
158#endif
159
Marc Mari88d88792016-08-12 09:27:03 -0400160void module_load_one(const char *prefix, const char *lib_name)
Fam Zhenge26110c2014-02-10 14:48:57 +0800161{
162#ifdef CONFIG_MODULES
163 char *fname = NULL;
Fam Zhenge26110c2014-02-10 14:48:57 +0800164 char *exec_dir;
ryang900610e2018-07-04 14:10:10 -0400165 const char *search_dir;
166 char *dirs[4];
Fam Zhengdffa41b2016-09-05 10:50:44 +0800167 char *module_name;
ryang900610e2018-07-04 14:10:10 -0400168 int i = 0, n_dirs = 0;
Fam Zhenge26110c2014-02-10 14:48:57 +0800169 int ret;
Fam Zhengdffa41b2016-09-05 10:50:44 +0800170 static GHashTable *loaded_modules;
Fam Zhenge26110c2014-02-10 14:48:57 +0800171
172 if (!g_module_supported()) {
173 fprintf(stderr, "Module is not supported by system.\n");
174 return;
175 }
176
Fam Zhengdffa41b2016-09-05 10:50:44 +0800177 if (!loaded_modules) {
178 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
179 }
180
181 module_name = g_strdup_printf("%s%s", prefix, lib_name);
182
183 if (g_hash_table_lookup(loaded_modules, module_name)) {
184 g_free(module_name);
185 return;
186 }
187 g_hash_table_insert(loaded_modules, module_name, module_name);
188
Fam Zhenge26110c2014-02-10 14:48:57 +0800189 exec_dir = qemu_get_exec_dir();
ryang900610e2018-07-04 14:10:10 -0400190 search_dir = getenv("QEMU_MODULE_DIR");
191 if (search_dir != NULL) {
192 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
193 }
194 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
195 dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : "");
196 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
197 assert(n_dirs <= ARRAY_SIZE(dirs));
198
Fam Zhenge26110c2014-02-10 14:48:57 +0800199 g_free(exec_dir);
200 exec_dir = NULL;
201
ryang900610e2018-07-04 14:10:10 -0400202 for (i = 0; i < n_dirs; i++) {
Fam Zhengdffa41b2016-09-05 10:50:44 +0800203 fname = g_strdup_printf("%s/%s%s",
204 dirs[i], module_name, HOST_DSOSUF);
Marc Mari88d88792016-08-12 09:27:03 -0400205 ret = module_load_file(fname);
206 g_free(fname);
207 fname = NULL;
208 /* Try loading until loaded a module file */
209 if (!ret) {
210 break;
Fam Zhenge26110c2014-02-10 14:48:57 +0800211 }
Fam Zhenge26110c2014-02-10 14:48:57 +0800212 }
213
ryang900610e2018-07-04 14:10:10 -0400214 for (i = 0; i < n_dirs; i++) {
Fam Zhenge26110c2014-02-10 14:48:57 +0800215 g_free(dirs[i]);
216 }
217
218#endif
219}