xref: /freebsd/sys/kern/kern_module.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Copyright (c) 1997 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "opt_compat.h"
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
39 #include <sys/proc.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/reboot.h>
43 #include <sys/sx.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
46 
47 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
48 
49 typedef TAILQ_HEAD(, module) modulelist_t;
50 struct module {
51 	TAILQ_ENTRY(module)	link;	/* chain together all modules */
52 	TAILQ_ENTRY(module)	flink;	/* all modules in a file */
53 	struct linker_file	*file;	/* file which contains this module */
54 	int			refs;	/* reference count */
55 	int 			id;	/* unique id number */
56 	char 			*name;	/* module name */
57 	modeventhand_t 		handler;	/* event handler */
58 	void 			*arg;	/* argument for handler */
59 	modspecific_t 		data;	/* module specific data */
60 };
61 
62 #define MOD_EVENT(mod, type)	(mod)->handler((mod), (type), (mod)->arg)
63 
64 static modulelist_t modules;
65 struct sx modules_sx;
66 static int nextid = 1;
67 static void module_shutdown(void *, int);
68 
69 static int
70 modevent_nop(module_t mod, int what, void *arg)
71 {
72 
73 	switch(what) {
74 	case MOD_LOAD:
75 		return (0);
76 	case MOD_UNLOAD:
77 		return (EBUSY);
78 	default:
79 		return (EOPNOTSUPP);
80 	}
81 }
82 
83 static void
84 module_init(void *arg)
85 {
86 
87 	sx_init(&modules_sx, "module subsystem sx lock");
88 	TAILQ_INIT(&modules);
89 	EVENTHANDLER_REGISTER(shutdown_final, module_shutdown, NULL,
90 	    SHUTDOWN_PRI_DEFAULT);
91 }
92 
93 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0)
94 
95 static void
96 module_shutdown(void *arg1, int arg2)
97 {
98 	module_t mod;
99 
100 	if (arg2 & RB_NOSYNC)
101 		return;
102 	mtx_lock(&Giant);
103 	MOD_SLOCK;
104 	TAILQ_FOREACH(mod, &modules, link)
105 		MOD_EVENT(mod, MOD_SHUTDOWN);
106 	MOD_SUNLOCK;
107 	mtx_unlock(&Giant);
108 }
109 
110 void
111 module_register_init(const void *arg)
112 {
113 	const moduledata_t *data = (const moduledata_t *)arg;
114 	int error;
115 	module_t mod;
116 
117 	mtx_lock(&Giant);
118 	MOD_SLOCK;
119 	mod = module_lookupbyname(data->name);
120 	if (mod == NULL)
121 		panic("module_register_init: module named %s not found\n",
122 		    data->name);
123 	MOD_SUNLOCK;
124 	error = MOD_EVENT(mod, MOD_LOAD);
125 	if (error) {
126 		MOD_EVENT(mod, MOD_UNLOAD);
127 		MOD_XLOCK;
128 		module_release(mod);
129 		MOD_XUNLOCK;
130 		printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
131 		    " %d\n", data->name, (void *)data->evhand, data->priv,
132 		    error);
133 	}
134 	mtx_unlock(&Giant);
135 }
136 
137 int
138 module_register(const moduledata_t *data, linker_file_t container)
139 {
140 	size_t namelen;
141 	module_t newmod;
142 
143 	MOD_XLOCK;
144 	newmod = module_lookupbyname(data->name);
145 	if (newmod != NULL) {
146 		MOD_XUNLOCK;
147 		printf("module_register: module %s already exists!\n",
148 		    data->name);
149 		return (EEXIST);
150 	}
151 	namelen = strlen(data->name) + 1;
152 	newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
153 	if (newmod == NULL) {
154 		MOD_XUNLOCK;
155 		return (ENOMEM);
156 	}
157 	newmod->refs = 1;
158 	newmod->id = nextid++;
159 	newmod->name = (char *)(newmod + 1);
160 	strcpy(newmod->name, data->name);
161 	newmod->handler = data->evhand ? data->evhand : modevent_nop;
162 	newmod->arg = data->priv;
163 	bzero(&newmod->data, sizeof(newmod->data));
164 	TAILQ_INSERT_TAIL(&modules, newmod, link);
165 
166 	if (container)
167 		TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
168 	newmod->file = container;
169 	MOD_XUNLOCK;
170 	return (0);
171 }
172 
173 void
174 module_reference(module_t mod)
175 {
176 
177 	MOD_XLOCK_ASSERT;
178 
179 	MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
180 	mod->refs++;
181 }
182 
183 void
184 module_release(module_t mod)
185 {
186 
187 	MOD_XLOCK_ASSERT;
188 
189 	if (mod->refs <= 0)
190 		panic("module_release: bad reference count");
191 
192 	MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
193 
194 	mod->refs--;
195 	if (mod->refs == 0) {
196 		TAILQ_REMOVE(&modules, mod, link);
197 		if (mod->file)
198 			TAILQ_REMOVE(&mod->file->modules, mod, flink);
199 		MOD_XUNLOCK;
200 		free(mod, M_MODULE);
201 		MOD_XLOCK;
202 	}
203 }
204 
205 module_t
206 module_lookupbyname(const char *name)
207 {
208 	module_t mod;
209 	int err;
210 
211 	MOD_LOCK_ASSERT;
212 
213 	TAILQ_FOREACH(mod, &modules, link) {
214 		err = strcmp(mod->name, name);
215 		if (err == 0)
216 			return (mod);
217 	}
218 	return (NULL);
219 }
220 
221 module_t
222 module_lookupbyid(int modid)
223 {
224         module_t mod;
225 
226         MOD_LOCK_ASSERT;
227 
228         TAILQ_FOREACH(mod, &modules, link)
229                 if (mod->id == modid)
230                         return(mod);
231         return (NULL);
232 }
233 
234 int
235 module_unload(module_t mod, int flags)
236 {
237 	int error;
238 
239 	mtx_lock(&Giant);
240 	error = MOD_EVENT(mod, MOD_QUIESCE);
241 	if (error == EOPNOTSUPP || error == EINVAL)
242 		error = 0;
243 	if (error == 0 || flags == LINKER_UNLOAD_FORCE)
244 		error = MOD_EVENT(mod, MOD_UNLOAD);
245 	mtx_unlock(&Giant);
246 	return (error);
247 }
248 
249 int
250 module_getid(module_t mod)
251 {
252 
253 	MOD_LOCK_ASSERT;
254 	return (mod->id);
255 }
256 
257 module_t
258 module_getfnext(module_t mod)
259 {
260 
261 	MOD_LOCK_ASSERT;
262 	return (TAILQ_NEXT(mod, flink));
263 }
264 
265 void
266 module_setspecific(module_t mod, modspecific_t *datap)
267 {
268 
269 	MOD_XLOCK_ASSERT;
270 	mod->data = *datap;
271 }
272 
273 linker_file_t
274 module_file(module_t mod)
275 {
276 
277 	return (mod->file);
278 }
279 
280 /*
281  * Syscalls.
282  */
283 /*
284  * MPSAFE
285  */
286 int
287 modnext(struct thread *td, struct modnext_args *uap)
288 {
289 	module_t mod;
290 	int error = 0;
291 
292 	td->td_retval[0] = -1;
293 
294 	MOD_SLOCK;
295 	if (uap->modid == 0) {
296 		mod = TAILQ_FIRST(&modules);
297 		if (mod)
298 			td->td_retval[0] = mod->id;
299 		else
300 			error = ENOENT;
301 		goto done2;
302 	}
303 	mod = module_lookupbyid(uap->modid);
304 	if (mod == NULL) {
305 		error = ENOENT;
306 		goto done2;
307 	}
308 	if (TAILQ_NEXT(mod, link))
309 		td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
310 	else
311 		td->td_retval[0] = 0;
312 done2:
313 	MOD_SUNLOCK;
314 	return (error);
315 }
316 
317 /*
318  * MPSAFE
319  */
320 int
321 modfnext(struct thread *td, struct modfnext_args *uap)
322 {
323 	module_t mod;
324 	int error;
325 
326 	td->td_retval[0] = -1;
327 
328 	MOD_SLOCK;
329 	mod = module_lookupbyid(uap->modid);
330 	if (mod == NULL) {
331 		error = ENOENT;
332 	} else {
333 		error = 0;
334 		if (TAILQ_NEXT(mod, flink))
335 			td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
336 		else
337 			td->td_retval[0] = 0;
338 	}
339 	MOD_SUNLOCK;
340 	return (error);
341 }
342 
343 struct module_stat_v1 {
344 	int	version;		/* set to sizeof(struct module_stat) */
345 	char	name[MAXMODNAME];
346 	int	refs;
347 	int	id;
348 };
349 
350 /*
351  * MPSAFE
352  */
353 int
354 modstat(struct thread *td, struct modstat_args *uap)
355 {
356 	module_t mod;
357 	modspecific_t data;
358 	int error = 0;
359 	int id, namelen, refs, version;
360 	struct module_stat *stat;
361 	char *name;
362 
363 	MOD_SLOCK;
364 	mod = module_lookupbyid(uap->modid);
365 	if (mod == NULL) {
366 		MOD_SUNLOCK;
367 		return (ENOENT);
368 	}
369 	id = mod->id;
370 	refs = mod->refs;
371 	name = mod->name;
372 	data = mod->data;
373 	MOD_SUNLOCK;
374 	stat = uap->stat;
375 
376 	/*
377 	 * Check the version of the user's structure.
378 	 */
379 	if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
380 		return (error);
381 	if (version != sizeof(struct module_stat_v1)
382 	    && version != sizeof(struct module_stat))
383 		return (EINVAL);
384 	namelen = strlen(mod->name) + 1;
385 	if (namelen > MAXMODNAME)
386 		namelen = MAXMODNAME;
387 	if ((error = copyout(name, &stat->name[0], namelen)) != 0)
388 		return (error);
389 
390 	if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
391 		return (error);
392 	if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
393 		return (error);
394 
395 	/*
396 	 * >v1 stat includes module data.
397 	 */
398 	if (version == sizeof(struct module_stat))
399 		if ((error = copyout(&data, &stat->data,
400 		    sizeof(data))) != 0)
401 			return (error);
402 	td->td_retval[0] = 0;
403 	return (error);
404 }
405 
406 /*
407  * MPSAFE
408  */
409 int
410 modfind(struct thread *td, struct modfind_args *uap)
411 {
412 	int error = 0;
413 	char name[MAXMODNAME];
414 	module_t mod;
415 
416 	if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
417 		return (error);
418 
419 	MOD_SLOCK;
420 	mod = module_lookupbyname(name);
421 	if (mod == NULL)
422 		error = ENOENT;
423 	else
424 		td->td_retval[0] = module_getid(mod);
425 	MOD_SUNLOCK;
426 	return (error);
427 }
428 
429 #ifdef COMPAT_IA32
430 #include <sys/mount.h>
431 #include <compat/freebsd32/freebsd32_util.h>
432 #include <compat/freebsd32/freebsd32.h>
433 #include <compat/freebsd32/freebsd32_proto.h>
434 
435 typedef union modspecific32 {
436 	int		intval;
437 	u_int32_t	uintval;
438 	int		longval;
439 	u_int32_t	ulongval;
440 } modspecific32_t;
441 
442 struct module_stat32 {
443 	int		version;
444 	char		name[MAXMODNAME];
445 	int		refs;
446 	int		id;
447 	modspecific32_t	data;
448 };
449 
450 /*
451  * MPSAFE
452  */
453 int
454 freebsd32_modstat(struct thread *td, struct freebsd32_modstat_args *uap)
455 {
456 	module_t mod;
457 	modspecific32_t data32;
458 	int error = 0;
459 	int id, namelen, refs, version;
460 	struct module_stat32 *stat32;
461 	char *name;
462 
463 	MOD_SLOCK;
464 	mod = module_lookupbyid(uap->modid);
465 	if (mod == NULL) {
466 		MOD_SUNLOCK;
467 		return (ENOENT);
468 	}
469 
470 	id = mod->id;
471 	refs = mod->refs;
472 	name = mod->name;
473 	CP(mod->data, data32, intval);
474 	CP(mod->data, data32, uintval);
475 	CP(mod->data, data32, longval);
476 	CP(mod->data, data32, ulongval);
477 	MOD_SUNLOCK;
478 	stat32 = uap->stat;
479 
480 	if ((error = copyin(&stat32->version, &version, sizeof(version))) != 0)
481 		return (error);
482 	if (version != sizeof(struct module_stat_v1)
483 	    && version != sizeof(struct module_stat32))
484 		return (EINVAL);
485 	namelen = strlen(mod->name) + 1;
486 	if (namelen > MAXMODNAME)
487 		namelen = MAXMODNAME;
488 	if ((error = copyout(name, &stat32->name[0], namelen)) != 0)
489 		return (error);
490 
491 	if ((error = copyout(&refs, &stat32->refs, sizeof(int))) != 0)
492 		return (error);
493 	if ((error = copyout(&id, &stat32->id, sizeof(int))) != 0)
494 		return (error);
495 
496 	/*
497 	 * >v1 stat includes module data.
498 	 */
499 	if (version == sizeof(struct module_stat32))
500 		if ((error = copyout(&data32, &stat32->data,
501 		    sizeof(data32))) != 0)
502 			return (error);
503 	td->td_retval[0] = 0;
504 	return (error);
505 }
506 #endif
507