xref: /freebsd/sys/sys/module.h (revision 1323ec57)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _SYS_MODULE_H_
32 #define _SYS_MODULE_H_
33 
34 /*
35  * Module metadata types
36  */
37 #define	MDT_DEPEND	1		/* argument is a module name */
38 #define	MDT_MODULE	2		/* module declaration */
39 #define	MDT_VERSION	3		/* module version(s) */
40 #define	MDT_PNP_INFO	4		/* Plug and play hints record */
41 
42 #define	MDT_STRUCT_VERSION	1	/* version of metadata structure */
43 #define	MDT_SETNAME	"modmetadata_set"
44 
45 typedef enum modeventtype {
46 	MOD_LOAD,
47 	MOD_UNLOAD,
48 	MOD_SHUTDOWN,
49 	MOD_QUIESCE
50 } modeventtype_t;
51 
52 typedef struct module *module_t;
53 typedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
54 
55 /*
56  * Struct for registering modules statically via SYSINIT.
57  */
58 typedef struct moduledata {
59 	const char	*name;		/* module name */
60 	modeventhand_t  evhand;		/* event handler */
61 	void		*priv;		/* extra data */
62 } moduledata_t;
63 
64 /*
65  * A module can use this to report module specific data to the user via
66  * kldstat(2).
67  */
68 typedef union modspecific {
69 	int	intval;
70 	u_int	uintval;
71 	long	longval;
72 	u_long	ulongval;
73 } modspecific_t;
74 
75 /*
76  * Module dependency declaration
77  */
78 struct mod_depend {
79 	int	md_ver_minimum;
80 	int	md_ver_preferred;
81 	int	md_ver_maximum;
82 };
83 
84 /*
85  * Module version declaration
86  */
87 struct mod_version {
88 	int	mv_version;
89 };
90 
91 struct mod_metadata {
92 	int		md_version;	/* structure version MDTV_* */
93 	int		md_type;	/* type of entry MDT_* */
94 	const void	*md_data;	/* specific data */
95 	const char	*md_cval;	/* common string label */
96 };
97 
98 struct mod_pnp_match_info
99 {
100 	const char *descr;	/* Description of the table */
101 	const char *bus;	/* Name of the bus for this table */
102 	const void *table;	/* Pointer to pnp table */
103 	int entry_len;		/* Length of each entry in the table (may be */
104 				/*   longer than descr describes). */
105 	int num_entry;		/* Number of entries in the table */
106 };
107 #ifdef	_KERNEL
108 
109 #include <sys/linker_set.h>
110 
111 #define	MODULE_METADATA_CONCAT(uniquifier)	_mod_metadata##uniquifier
112 #define	MODULE_METADATA(uniquifier, type, data, cval)			\
113 	static struct mod_metadata MODULE_METADATA_CONCAT(uniquifier) = {	\
114 		MDT_STRUCT_VERSION,					\
115 		type,							\
116 		data,							\
117 		cval							\
118 	};								\
119 	DATA_SET(modmetadata_set, MODULE_METADATA_CONCAT(uniquifier))
120 
121 #define	MODULE_DEPEND(module, mdepend, vmin, vpref, vmax)		\
122 	static struct mod_depend _##module##_depend_on_##mdepend	\
123 	    __section(".data") = {					\
124 		vmin,							\
125 		vpref,							\
126 		vmax							\
127 	};								\
128 	MODULE_METADATA(_md_##module##_on_##mdepend, MDT_DEPEND,	\
129 	    &_##module##_depend_on_##mdepend, #mdepend)
130 
131 /*
132  * Every kernel has a 'kernel' module with the version set to
133  * __FreeBSD_version.  We embed a MODULE_DEPEND() inside every module
134  * that depends on the 'kernel' module.  It uses the current value of
135  * __FreeBSD_version as the minimum and preferred versions.  For the
136  * maximum version it rounds the version up to the end of its branch
137  * (i.e. M99999 for M.x).  This allows a module built on M.x to work
138  * on M.y systems where y >= x, but fail on M.z systems where z < x.
139  */
140 #define	MODULE_KERNEL_MAXVER	(roundup(__FreeBSD_version, 100000) - 1)
141 
142 #define	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, maxver)	\
143 	MODULE_DEPEND(name, kernel, __FreeBSD_version,			\
144 	    __FreeBSD_version, maxver);					\
145 	MODULE_METADATA(_md_##name, MDT_MODULE, &data, __XSTRING(name));\
146 	SYSINIT(name##module, sub, order, module_register_init, &data);	\
147 	struct __hack
148 
149 #ifdef KLD_TIED
150 #define	DECLARE_MODULE(name, data, sub, order)				\
151 	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, __FreeBSD_version)
152 #else
153 #define	DECLARE_MODULE(name, data, sub, order)							\
154 	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, MODULE_KERNEL_MAXVER)
155 #endif
156 
157 /*
158  * The module declared with DECLARE_MODULE_TIED can only be loaded
159  * into the kernel with exactly the same __FreeBSD_version.
160  *
161  * Use it for modules that use kernel interfaces that are not stable
162  * even on STABLE/X branches.
163  */
164 #define	DECLARE_MODULE_TIED(name, data, sub, order)			\
165 	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, __FreeBSD_version)
166 
167 #define	MODULE_VERSION_CONCAT(module, version)	_##module##_version
168 #define	MODULE_VERSION(module, version)					\
169 	static struct mod_version MODULE_VERSION_CONCAT(module, version)\
170 	    __section(".data") = {					\
171 		version							\
172 	};								\
173 	MODULE_METADATA(MODULE_VERSION_CONCAT(module, version), MDT_VERSION,\
174 	    &MODULE_VERSION_CONCAT(module, version), __XSTRING(module))
175 
176 /**
177  * Generic macros to create pnp info hints that modules may export
178  * to allow external tools to parse their internal device tables
179  * to make an informed guess about what driver(s) to load.
180  */
181 #define	MODULE_PNP_INFO(d, b, unique, t, n)				\
182 	static const struct mod_pnp_match_info _module_pnp_##b##_##unique = {	\
183 		.descr = d,						\
184 		.bus = #b,						\
185 		.table = t,						\
186 		.entry_len = sizeof((t)[0]),				\
187 		.num_entry = n						\
188 	};								\
189 	MODULE_METADATA(_md_##b##_pnpinfo_##unique, MDT_PNP_INFO,	\
190 	    &_module_pnp_##b##_##unique, #b);
191 /**
192  * descr is a string that describes each entry in the table. The general
193  * form is the grammar (TYPE:pnp_name[/pnp_name];)*
194  * where TYPE is one of the following:
195  *	U8	uint8_t element
196  *	V8	like U8 and 0xff means match any
197  *	G16	uint16_t element, any value >= matches
198  *	L16	uint16_t element, any value <= matches
199  *	M16	uint16_t element, mask of which of the following fields to use.
200  *	U16	uint16_t element
201  *	V16	like U16 and 0xffff means match any
202  *	U32	uint32_t element
203  *	V32	like U32 and 0xffffffff means match any
204  *	W32	Two 16-bit values with first pnp_name in LSW and second in MSW.
205  *	Z	pointer to a string to match exactly
206  *	D	pointer to a string to human readable description for device
207  *	P	A pointer that should be ignored
208  *	E	EISA PNP Identifier (in binary, but bus publishes string)
209  *	T	Key for whole table. pnp_name=value. must be last, if present.
210  *
211  * The pnp_name "#" is reserved for other fields that should be ignored.
212  * Otherwise pnp_name must match the name from the parent device's pnpinfo
213  * output. The second pnp_name is used for the W32 type.
214  */
215 
216 extern struct sx modules_sx;
217 
218 #define	MOD_XLOCK	sx_xlock(&modules_sx)
219 #define	MOD_SLOCK	sx_slock(&modules_sx)
220 #define	MOD_XUNLOCK	sx_xunlock(&modules_sx)
221 #define	MOD_SUNLOCK	sx_sunlock(&modules_sx)
222 #define	MOD_LOCK_ASSERT	sx_assert(&modules_sx, SX_LOCKED)
223 #define	MOD_XLOCK_ASSERT	sx_assert(&modules_sx, SX_XLOCKED)
224 
225 struct linker_file;
226 
227 void	module_register_init(const void *);
228 int	module_register(const struct moduledata *, struct linker_file *);
229 module_t	module_lookupbyname(const char *);
230 module_t	module_lookupbyid(int);
231 int	module_quiesce(module_t);
232 void	module_reference(module_t);
233 void	module_release(module_t);
234 int	module_unload(module_t);
235 int	module_getid(module_t);
236 module_t	module_getfnext(module_t);
237 const char *	module_getname(module_t);
238 void	module_setspecific(module_t, modspecific_t *);
239 struct linker_file *module_file(module_t);
240 
241 #ifdef	MOD_DEBUG
242 extern int mod_debug;
243 #define	MOD_DEBUG_REFS	1
244 
245 #define	MOD_DPF(cat, args) do {						\
246 	if (mod_debug & MOD_DEBUG_##cat)				\
247 		printf args;						\
248 } while (0)
249 
250 #else	/* !MOD_DEBUG */
251 
252 #define	MOD_DPF(cat, args)
253 #endif
254 #endif	/* _KERNEL */
255 
256 #define	MAXMODNAMEV1V2	32
257 #define	MAXMODNAMEV3	MAXPATHLEN
258 #define	MAXMODNAME	MAXMODNAMEV3
259 
260 struct module_stat {
261 	int		version;	/* set to sizeof(struct module_stat) */
262 	char		name[MAXMODNAME];
263 	int		refs;
264 	int		id;
265 	modspecific_t	data;
266 };
267 
268 #ifndef _KERNEL
269 
270 #include <sys/cdefs.h>
271 
272 __BEGIN_DECLS
273 int	modnext(int _modid);
274 int	modfnext(int _modid);
275 int	modstat(int _modid, struct module_stat *_stat);
276 int	modfind(const char *_name);
277 __END_DECLS
278 
279 #endif
280 
281 #endif	/* !_SYS_MODULE_H_ */
282