]> Git Repo - qemu.git/blame - util/module.c
target/m68k: use gdb_get_reg helpers
[qemu.git] / util / module.c
CommitLineData
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"
0bfe3ca5
AL
22
23typedef struct ModuleEntry
24{
0bfe3ca5 25 void (*init)(void);
72cf2d4f 26 QTAILQ_ENTRY(ModuleEntry) node;
e26110cf 27 module_init_type type;
0bfe3ca5
AL
28} ModuleEntry;
29
72cf2d4f 30typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
0bfe3ca5 31
f7897430 32static ModuleTypeList init_type_list[MODULE_INIT_MAX];
46a07579 33static bool modules_init_done[MODULE_INIT_MAX];
0bfe3ca5 34
e26110cf
FZ
35static ModuleTypeList dso_init_list;
36
37static void init_lists(void)
0bfe3ca5 38{
f7897430
AL
39 static int inited;
40 int i;
0bfe3ca5 41
f7897430
AL
42 if (inited) {
43 return;
0bfe3ca5
AL
44 }
45
f7897430 46 for (i = 0; i < MODULE_INIT_MAX; i++) {
72cf2d4f 47 QTAILQ_INIT(&init_type_list[i]);
f7897430 48 }
0bfe3ca5 49
e26110cf
FZ
50 QTAILQ_INIT(&dso_init_list);
51
f7897430
AL
52 inited = 1;
53}
0bfe3ca5 54
0bfe3ca5 55
f7897430
AL
56static ModuleTypeList *find_type(module_init_type type)
57{
e26110cf 58 init_lists();
f7897430 59
9be38598 60 return &init_type_list[type];
0bfe3ca5
AL
61}
62
63void register_module_init(void (*fn)(void), module_init_type type)
64{
65 ModuleEntry *e;
66 ModuleTypeList *l;
67
7267c094 68 e = g_malloc0(sizeof(*e));
0bfe3ca5 69 e->init = fn;
e26110cf 70 e->type = type;
0bfe3ca5 71
f7897430 72 l = find_type(type);
0bfe3ca5 73
72cf2d4f 74 QTAILQ_INSERT_TAIL(l, e, node);
0bfe3ca5
AL
75}
76
e26110cf
FZ
77void 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
0bfe3ca5
AL
90void module_call_init(module_init_type type)
91{
92 ModuleTypeList *l;
93 ModuleEntry *e;
94
46a07579
AB
95 if (modules_init_done[type]) {
96 return;
97 }
98
f7897430 99 l = find_type(type);
0bfe3ca5 100
72cf2d4f 101 QTAILQ_FOREACH(e, l, node) {
0bfe3ca5
AL
102 e->init();
103 }
46a07579
AB
104
105 modules_init_done[type] = true;
0bfe3ca5 106}
e26110cf
FZ
107
108#ifdef CONFIG_MODULES
109static int module_load_file(const char *fname)
110{
111 GModule *g_module;
112 void (*sym)(void);
113 const char *dsosuf = HOST_DSOSUF;
114 int len = strlen(fname);
115 int suf_len = strlen(dsosuf);
116 ModuleEntry *e, *next;
117 int ret;
118
119 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
120 /* wrong suffix */
121 ret = -EINVAL;
122 goto out;
123 }
124 if (access(fname, F_OK)) {
125 ret = -ENOENT;
126 goto out;
127 }
128
129 assert(QTAILQ_EMPTY(&dso_init_list));
130
131 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
132 if (!g_module) {
133 fprintf(stderr, "Failed to open module: %s\n",
134 g_module_error());
135 ret = -EINVAL;
136 goto out;
137 }
138 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
139 fprintf(stderr, "Failed to initialize module: %s\n",
140 fname);
141 /* Print some info if this is a QEMU module (but from different build),
142 * this will make debugging user problems easier. */
143 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
144 fprintf(stderr,
145 "Note: only modules from the same build can be loaded.\n");
146 }
147 g_module_close(g_module);
148 ret = -EINVAL;
149 } else {
150 QTAILQ_FOREACH(e, &dso_init_list, node) {
88d88798 151 e->init();
e26110cf
FZ
152 register_module_init(e->init, e->type);
153 }
154 ret = 0;
155 }
156
157 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
158 QTAILQ_REMOVE(&dso_init_list, e, node);
159 g_free(e);
160 }
161out:
162 return ret;
163}
164#endif
165
81d8ccb1 166bool module_load_one(const char *prefix, const char *lib_name)
e26110cf 167{
81d8ccb1
MAL
168 bool success = false;
169
e26110cf
FZ
170#ifdef CONFIG_MODULES
171 char *fname = NULL;
e26110cf 172 char *exec_dir;
900610e6 173 const char *search_dir;
174 char *dirs[4];
dffa41b4 175 char *module_name;
900610e6 176 int i = 0, n_dirs = 0;
e26110cf 177 int ret;
dffa41b4 178 static GHashTable *loaded_modules;
e26110cf
FZ
179
180 if (!g_module_supported()) {
181 fprintf(stderr, "Module is not supported by system.\n");
81d8ccb1 182 return false;
e26110cf
FZ
183 }
184
dffa41b4
FZ
185 if (!loaded_modules) {
186 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
187 }
188
189 module_name = g_strdup_printf("%s%s", prefix, lib_name);
190
90629122 191 if (!g_hash_table_add(loaded_modules, module_name)) {
dffa41b4 192 g_free(module_name);
81d8ccb1 193 return true;
dffa41b4 194 }
dffa41b4 195
e26110cf 196 exec_dir = qemu_get_exec_dir();
900610e6 197 search_dir = getenv("QEMU_MODULE_DIR");
198 if (search_dir != NULL) {
199 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
200 }
201 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
202 dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : "");
203 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
204 assert(n_dirs <= ARRAY_SIZE(dirs));
205
e26110cf
FZ
206 g_free(exec_dir);
207 exec_dir = NULL;
208
900610e6 209 for (i = 0; i < n_dirs; i++) {
dffa41b4
FZ
210 fname = g_strdup_printf("%s/%s%s",
211 dirs[i], module_name, HOST_DSOSUF);
88d88798
MM
212 ret = module_load_file(fname);
213 g_free(fname);
214 fname = NULL;
215 /* Try loading until loaded a module file */
216 if (!ret) {
81d8ccb1 217 success = true;
88d88798 218 break;
e26110cf 219 }
e26110cf
FZ
220 }
221
81d8ccb1
MAL
222 if (!success) {
223 g_hash_table_remove(loaded_modules, module_name);
638be478 224 g_free(module_name);
81d8ccb1
MAL
225 }
226
900610e6 227 for (i = 0; i < n_dirs; i++) {
e26110cf
FZ
228 g_free(dirs[i]);
229 }
230
231#endif
81d8ccb1 232 return success;
e26110cf 233}
This page took 0.762963 seconds and 4 git commands to generate.