2 * Copyright (c) 1997 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include "opt_compat.h"
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
41 #include <sys/mutex.h>
42 #include <sys/reboot.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
47 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
50 TAILQ_ENTRY(module) link; /* chain together all modules */
51 TAILQ_ENTRY(module) flink; /* all modules in a file */
52 struct linker_file *file; /* file which contains this module */
53 int refs; /* reference count */
54 int id; /* unique id number */
55 char *name; /* module name */
56 modeventhand_t handler; /* event handler */
57 void *arg; /* argument for handler */
58 modspecific_t data; /* module specific data */
61 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
63 static TAILQ_HEAD(modulelist, module) modules;
65 static int nextid = 1;
66 static void module_shutdown(void *, int);
69 modevent_nop(module_t mod, int what, void *arg)
83 module_init(void *arg)
86 sx_init(&modules_sx, "module subsystem sx lock");
88 EVENTHANDLER_REGISTER(shutdown_final, module_shutdown, NULL,
89 SHUTDOWN_PRI_DEFAULT);
92 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
95 module_shutdown(void *arg1, int arg2)
103 TAILQ_FOREACH_REVERSE(mod, &modules, modulelist, link)
104 MOD_EVENT(mod, MOD_SHUTDOWN);
110 module_register_init(const void *arg)
112 const moduledata_t *data = (const moduledata_t *)arg;
118 mod = module_lookupbyname(data->name);
120 panic("module_register_init: module named %s not found\n",
123 error = MOD_EVENT(mod, MOD_LOAD);
125 MOD_EVENT(mod, MOD_UNLOAD);
129 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
130 " %d\n", data->name, (void *)data->evhand, data->priv,
136 * Once a module is succesfully loaded, move
137 * it to the head of the module list for this
138 * linker file. This resorts the list so that
139 * when the kernel linker iterates over the
140 * modules to unload them, it will unload them
141 * in the reverse order they were loaded.
143 TAILQ_REMOVE(&mod->file->modules, mod, flink);
144 TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
152 module_register(const moduledata_t *data, linker_file_t container)
158 newmod = module_lookupbyname(data->name);
159 if (newmod != NULL) {
161 printf("module_register: module %s already exists!\n",
165 namelen = strlen(data->name) + 1;
166 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
167 if (newmod == NULL) {
172 newmod->id = nextid++;
173 newmod->name = (char *)(newmod + 1);
174 strcpy(newmod->name, data->name);
175 newmod->handler = data->evhand ? data->evhand : modevent_nop;
176 newmod->arg = data->priv;
177 bzero(&newmod->data, sizeof(newmod->data));
178 TAILQ_INSERT_TAIL(&modules, newmod, link);
181 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
182 newmod->file = container;
188 module_reference(module_t mod)
193 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
198 module_release(module_t mod)
204 panic("module_release: bad reference count");
206 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
209 if (mod->refs == 0) {
210 TAILQ_REMOVE(&modules, mod, link);
212 TAILQ_REMOVE(&mod->file->modules, mod, flink);
218 module_lookupbyname(const char *name)
225 TAILQ_FOREACH(mod, &modules, link) {
226 err = strcmp(mod->name, name);
234 module_lookupbyid(int modid)
240 TAILQ_FOREACH(mod, &modules, link)
241 if (mod->id == modid)
247 module_quiesce(module_t mod)
252 error = MOD_EVENT(mod, MOD_QUIESCE);
254 if (error == EOPNOTSUPP || error == EINVAL)
260 module_unload(module_t mod)
265 error = MOD_EVENT(mod, MOD_UNLOAD);
271 module_getid(module_t mod)
279 module_getfnext(module_t mod)
283 return (TAILQ_NEXT(mod, flink));
287 module_getname(module_t mod)
295 module_setspecific(module_t mod, modspecific_t *datap)
303 module_file(module_t mod)
313 modnext(struct thread *td, struct modnext_args *uap)
318 td->td_retval[0] = -1;
321 if (uap->modid == 0) {
322 mod = TAILQ_FIRST(&modules);
324 td->td_retval[0] = mod->id;
329 mod = module_lookupbyid(uap->modid);
334 if (TAILQ_NEXT(mod, link))
335 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
337 td->td_retval[0] = 0;
344 modfnext(struct thread *td, struct modfnext_args *uap)
349 td->td_retval[0] = -1;
352 mod = module_lookupbyid(uap->modid);
357 if (TAILQ_NEXT(mod, flink))
358 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
360 td->td_retval[0] = 0;
366 struct module_stat_v1 {
367 int version; /* set to sizeof(struct module_stat) */
368 char name[MAXMODNAME];
374 modstat(struct thread *td, struct modstat_args *uap)
379 int id, namelen, refs, version;
380 struct module_stat *stat;
384 mod = module_lookupbyid(uap->modid);
397 * Check the version of the user's structure.
399 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
401 if (version != sizeof(struct module_stat_v1)
402 && version != sizeof(struct module_stat))
404 namelen = strlen(mod->name) + 1;
405 if (namelen > MAXMODNAME)
406 namelen = MAXMODNAME;
407 if ((error = copyout(name, &stat->name[0], namelen)) != 0)
410 if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
412 if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
416 * >v1 stat includes module data.
418 if (version == sizeof(struct module_stat))
419 if ((error = copyout(&data, &stat->data,
422 td->td_retval[0] = 0;
427 modfind(struct thread *td, struct modfind_args *uap)
430 char name[MAXMODNAME];
433 if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
437 mod = module_lookupbyname(name);
441 td->td_retval[0] = module_getid(mod);
446 MODULE_VERSION(kernel, __FreeBSD_version);
448 #ifdef COMPAT_FREEBSD32
449 #include <sys/mount.h>
450 #include <sys/socket.h>
451 #include <compat/freebsd32/freebsd32_util.h>
452 #include <compat/freebsd32/freebsd32.h>
453 #include <compat/freebsd32/freebsd32_proto.h>
455 typedef union modspecific32 {
462 struct module_stat32 {
464 char name[MAXMODNAME];
467 modspecific32_t data;
471 freebsd32_modstat(struct thread *td, struct freebsd32_modstat_args *uap)
474 modspecific32_t data32;
476 int id, namelen, refs, version;
477 struct module_stat32 *stat32;
481 mod = module_lookupbyid(uap->modid);
490 CP(mod->data, data32, intval);
491 CP(mod->data, data32, uintval);
492 CP(mod->data, data32, longval);
493 CP(mod->data, data32, ulongval);
497 if ((error = copyin(&stat32->version, &version, sizeof(version))) != 0)
499 if (version != sizeof(struct module_stat_v1)
500 && version != sizeof(struct module_stat32))
502 namelen = strlen(mod->name) + 1;
503 if (namelen > MAXMODNAME)
504 namelen = MAXMODNAME;
505 if ((error = copyout(name, &stat32->name[0], namelen)) != 0)
508 if ((error = copyout(&refs, &stat32->refs, sizeof(int))) != 0)
510 if ((error = copyout(&id, &stat32->id, sizeof(int))) != 0)
514 * >v1 stat includes module data.
516 if (version == sizeof(struct module_stat32))
517 if ((error = copyout(&data32, &stat32->data,
518 sizeof(data32))) != 0)
520 td->td_retval[0] = 0;