xref: /qemu/include/qemu/module.h (revision c551fb0b)
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  */
13 
14 #ifndef QEMU_MODULE_H
15 #define QEMU_MODULE_H
16 
17 
18 #define DSO_STAMP_FUN         glue(qemu_stamp, CONFIG_STAMP)
19 #define DSO_STAMP_FUN_STR     stringify(DSO_STAMP_FUN)
20 
21 #ifdef BUILD_DSO
22 void DSO_STAMP_FUN(void);
23 /* This is a dummy symbol to identify a loaded DSO as a QEMU module, so we can
24  * distinguish "version mismatch" from "not a QEMU module", when the stamp
25  * check fails during module loading */
26 void qemu_module_dummy(void);
27 
28 #define module_init(function, type)                                         \
29 static void __attribute__((constructor)) do_qemu_init_ ## function(void)    \
30 {                                                                           \
31     register_dso_module_init(function, type);                               \
32 }
33 #else
34 /* This should not be used directly.  Use block_init etc. instead.  */
35 #define module_init(function, type)                                         \
36 static void __attribute__((constructor)) do_qemu_init_ ## function(void)    \
37 {                                                                           \
38     register_module_init(function, type);                                   \
39 }
40 #endif
41 
42 typedef enum {
43     MODULE_INIT_MIGRATION,
44     MODULE_INIT_BLOCK,
45     MODULE_INIT_OPTS,
46     MODULE_INIT_QOM,
47     MODULE_INIT_TRACE,
48     MODULE_INIT_XEN_BACKEND,
49     MODULE_INIT_LIBQOS,
50     MODULE_INIT_FUZZ_TARGET,
51     MODULE_INIT_MAX
52 } module_init_type;
53 
54 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
55 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
56 #define type_init(function) module_init(function, MODULE_INIT_QOM)
57 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
58 #define xen_backend_init(function) module_init(function, \
59                                                MODULE_INIT_XEN_BACKEND)
60 #define libqos_init(function) module_init(function, MODULE_INIT_LIBQOS)
61 #define fuzz_target_init(function) module_init(function, \
62                                                MODULE_INIT_FUZZ_TARGET)
63 #define migration_init(function) module_init(function, MODULE_INIT_MIGRATION)
64 #define block_module_load(lib, errp) module_load("block-", lib, errp)
65 #define ui_module_load(lib, errp) module_load("ui-", lib, errp)
66 #define audio_module_load(lib, errp) module_load("audio-", lib, errp)
67 
68 void register_module_init(void (*fn)(void), module_init_type type);
69 void register_dso_module_init(void (*fn)(void), module_init_type type);
70 
71 void module_call_init(module_init_type type);
72 
73 /*
74  * module_load: attempt to load a module from a set of directories
75  *
76  * directories searched are:
77  * - getenv("QEMU_MODULE_DIR")
78  * - get_relocated_path(CONFIG_QEMU_MODDIR);
79  * - /var/run/qemu/${version_dir}
80  *
81  * prefix:         a subsystem prefix, or the empty string ("audio-", ..., "")
82  * name:           name of the module
83  * errp:           error to set in case the module is found, but load failed.
84  *
85  * Return value:   -1 on error (errp set if not NULL).
86  *                 0 if module or one of its dependencies are not installed,
87  *                 1 if the module is found and loaded,
88  *                 2 if the module is already loaded, or module is built-in.
89  */
90 int module_load(const char *prefix, const char *name, Error **errp);
91 
92 /*
93  * module_load_qom: attempt to load a module to provide a QOM type
94  *
95  * type:           the type to be provided
96  * errp:           error to set.
97  *
98  * Return value:   as per module_load.
99  */
100 int module_load_qom(const char *type, Error **errp);
101 void module_load_qom_all(void);
102 void module_allow_arch(const char *arch);
103 
104 /**
105  * DOC: module info annotation macros
106  *
107  * ``scripts/modinfo-collect.py`` will collect module info,
108  * using the preprocessor and -DQEMU_MODINFO.
109  *
110  * ``scripts/modinfo-generate.py`` will create a module meta-data database
111  * from the collected information so qemu knows about module
112  * dependencies and QOM objects implemented by modules.
113  *
114  * See ``*.modinfo`` and ``modinfo.c`` in the build directory to check the
115  * script results.
116  */
117 #ifdef QEMU_MODINFO
118 # define modinfo(kind, value) \
119     MODINFO_START kind value MODINFO_END
120 #else
121 # define modinfo(kind, value)
122 #endif
123 
124 /**
125  * module_obj
126  *
127  * @name: QOM type.
128  *
129  * This module implements QOM type @name.
130  */
131 #define module_obj(name) modinfo(obj, name)
132 
133 /**
134  * module_dep
135  *
136  * @name: module name
137  *
138  * This module depends on module @name.
139  */
140 #define module_dep(name) modinfo(dep, name)
141 
142 /**
143  * module_arch
144  *
145  * @name: target architecture
146  *
147  * This module is for target architecture @arch.
148  *
149  * Note that target-dependent modules are tagged automatically, so
150  * this is only needed in case target-independent modules should be
151  * restricted.  Use case example: the ccw bus is implemented by s390x
152  * only.
153  */
154 #define module_arch(name) modinfo(arch, name)
155 
156 /**
157  * module_opts
158  *
159  * @name: QemuOpts name
160  *
161  * This module registers QemuOpts @name.
162  */
163 #define module_opts(name) modinfo(opts, name)
164 
165 /**
166  * module_kconfig
167  *
168  * @name: Kconfig requirement necessary to load the module
169  *
170  * This module requires a core module that should be implemented and
171  * enabled in Kconfig.
172  */
173 #define module_kconfig(name) modinfo(kconfig, name)
174 
175 /*
176  * module info database
177  *
178  * scripts/modinfo-generate.c will build this using the data collected
179  * by scripts/modinfo-collect.py
180  */
181 typedef struct QemuModinfo QemuModinfo;
182 struct QemuModinfo {
183     const char *name;
184     const char *arch;
185     const char **objs;
186     const char **deps;
187     const char **opts;
188 };
189 extern const QemuModinfo qemu_modinfo[];
190 void module_init_info(const QemuModinfo *info);
191 
192 #endif
193