2009-05-14 22:29:53 +04:00
|
|
|
/*
|
|
|
|
* QEMU Module Infrastructure
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-13 20:44:23 +04:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-05-14 22:29:53 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-02-27 02:31:33 +04:00
|
|
|
#include "qemu-common.h"
|
2014-02-25 20:36:55 +04:00
|
|
|
#ifdef CONFIG_MODULES
|
2014-02-10 10:48:57 +04:00
|
|
|
#include <gmodule.h>
|
2014-02-25 20:36:55 +04:00
|
|
|
#endif
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/module.h"
|
2009-05-14 22:29:53 +04:00
|
|
|
|
|
|
|
typedef struct ModuleEntry
|
|
|
|
{
|
|
|
|
void (*init)(void);
|
2009-09-12 11:36:22 +04:00
|
|
|
QTAILQ_ENTRY(ModuleEntry) node;
|
2014-02-10 10:48:57 +04:00
|
|
|
module_init_type type;
|
2009-05-14 22:29:53 +04:00
|
|
|
} ModuleEntry;
|
|
|
|
|
2009-09-12 11:36:22 +04:00
|
|
|
typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
static ModuleTypeList init_type_list[MODULE_INIT_MAX];
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2014-02-10 10:48:57 +04:00
|
|
|
static ModuleTypeList dso_init_list;
|
|
|
|
|
|
|
|
static void init_lists(void)
|
2009-05-14 22:29:53 +04:00
|
|
|
{
|
2009-05-15 02:57:31 +04:00
|
|
|
static int inited;
|
|
|
|
int i;
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
if (inited) {
|
|
|
|
return;
|
2009-05-14 22:29:53 +04:00
|
|
|
}
|
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
for (i = 0; i < MODULE_INIT_MAX; i++) {
|
2009-09-12 11:36:22 +04:00
|
|
|
QTAILQ_INIT(&init_type_list[i]);
|
2009-05-15 02:57:31 +04:00
|
|
|
}
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2014-02-10 10:48:57 +04:00
|
|
|
QTAILQ_INIT(&dso_init_list);
|
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
inited = 1;
|
|
|
|
}
|
2009-05-14 22:29:53 +04:00
|
|
|
|
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
static ModuleTypeList *find_type(module_init_type type)
|
|
|
|
{
|
2014-02-10 10:48:57 +04:00
|
|
|
init_lists();
|
2009-05-15 02:57:31 +04:00
|
|
|
|
2016-06-14 00:57:58 +03:00
|
|
|
return &init_type_list[type];
|
2009-05-14 22:29:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_module_init(void (*fn)(void), module_init_type type)
|
|
|
|
{
|
|
|
|
ModuleEntry *e;
|
|
|
|
ModuleTypeList *l;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
e = g_malloc0(sizeof(*e));
|
2009-05-14 22:29:53 +04:00
|
|
|
e->init = fn;
|
2014-02-10 10:48:57 +04:00
|
|
|
e->type = type;
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2009-05-15 02:57:31 +04:00
|
|
|
l = find_type(type);
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2009-09-12 11:36:22 +04:00
|
|
|
QTAILQ_INSERT_TAIL(l, e, node);
|
2009-05-14 22:29:53 +04:00
|
|
|
}
|
|
|
|
|
2014-02-10 10:48:57 +04:00
|
|
|
void register_dso_module_init(void (*fn)(void), module_init_type type)
|
|
|
|
{
|
|
|
|
ModuleEntry *e;
|
|
|
|
|
|
|
|
init_lists();
|
|
|
|
|
|
|
|
e = g_malloc0(sizeof(*e));
|
|
|
|
e->init = fn;
|
|
|
|
e->type = type;
|
|
|
|
|
|
|
|
QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void module_load(module_init_type type);
|
|
|
|
|
2009-05-14 22:29:53 +04:00
|
|
|
void module_call_init(module_init_type type)
|
|
|
|
{
|
|
|
|
ModuleTypeList *l;
|
|
|
|
ModuleEntry *e;
|
|
|
|
|
2014-02-10 10:48:57 +04:00
|
|
|
module_load(type);
|
2009-05-15 02:57:31 +04:00
|
|
|
l = find_type(type);
|
2009-05-14 22:29:53 +04:00
|
|
|
|
2009-09-12 11:36:22 +04:00
|
|
|
QTAILQ_FOREACH(e, l, node) {
|
2009-05-14 22:29:53 +04:00
|
|
|
e->init();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 10:48:57 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_MODULES
|
|
|
|
static int module_load_file(const char *fname)
|
|
|
|
{
|
|
|
|
GModule *g_module;
|
|
|
|
void (*sym)(void);
|
|
|
|
const char *dsosuf = HOST_DSOSUF;
|
|
|
|
int len = strlen(fname);
|
|
|
|
int suf_len = strlen(dsosuf);
|
|
|
|
ModuleEntry *e, *next;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
|
|
|
|
/* wrong suffix */
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (access(fname, F_OK)) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(QTAILQ_EMPTY(&dso_init_list));
|
|
|
|
|
|
|
|
g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
|
|
|
|
if (!g_module) {
|
|
|
|
fprintf(stderr, "Failed to open module: %s\n",
|
|
|
|
g_module_error());
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
|
|
|
|
fprintf(stderr, "Failed to initialize module: %s\n",
|
|
|
|
fname);
|
|
|
|
/* Print some info if this is a QEMU module (but from different build),
|
|
|
|
* this will make debugging user problems easier. */
|
|
|
|
if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Note: only modules from the same build can be loaded.\n");
|
|
|
|
}
|
|
|
|
g_module_close(g_module);
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
|
|
|
QTAILQ_FOREACH(e, &dso_init_list, node) {
|
|
|
|
register_module_init(e->init, e->type);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
|
|
|
|
QTAILQ_REMOVE(&dso_init_list, e, node);
|
|
|
|
g_free(e);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-16 22:02:52 +04:00
|
|
|
static void module_load(module_init_type type)
|
2014-02-10 10:48:57 +04:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_MODULES
|
|
|
|
char *fname = NULL;
|
|
|
|
const char **mp;
|
|
|
|
static const char *block_modules[] = {
|
|
|
|
CONFIG_BLOCK_MODULES
|
|
|
|
};
|
|
|
|
char *exec_dir;
|
|
|
|
char *dirs[3];
|
|
|
|
int i = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!g_module_supported()) {
|
|
|
|
fprintf(stderr, "Module is not supported by system.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MODULE_INIT_BLOCK:
|
|
|
|
mp = block_modules;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* no other types have dynamic modules for now*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exec_dir = qemu_get_exec_dir();
|
|
|
|
dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
|
|
|
|
dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
|
|
|
|
dirs[i++] = g_strdup_printf("%s", exec_dir ? : "");
|
|
|
|
assert(i == ARRAY_SIZE(dirs));
|
|
|
|
g_free(exec_dir);
|
|
|
|
exec_dir = NULL;
|
|
|
|
|
|
|
|
for ( ; *mp; mp++) {
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dirs); i++) {
|
|
|
|
fname = g_strdup_printf("%s/%s%s", dirs[i], *mp, HOST_DSOSUF);
|
|
|
|
ret = module_load_file(fname);
|
2014-07-15 18:04:25 +04:00
|
|
|
g_free(fname);
|
|
|
|
fname = NULL;
|
2014-02-10 10:48:57 +04:00
|
|
|
/* Try loading until loaded a module file */
|
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dirs); i++) {
|
|
|
|
g_free(dirs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|