2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/eventhandler.h>
34 #include <sys/malloc.h>
35 #include <sys/sysproto.h>
36 #include <sys/sysent.h>
39 #include <sys/mutex.h>
40 #include <sys/reboot.h>
42 #include <sys/module.h>
43 #include <sys/linker.h>
45 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
48 TAILQ_ENTRY(module) link; /* chain together all modules */
49 TAILQ_ENTRY(module) flink; /* all modules in a file */
50 struct linker_file *file; /* file which contains this module */
51 int refs; /* reference count */
52 int id; /* unique id number */
53 char *name; /* module name */
54 modeventhand_t handler; /* event handler */
55 void *arg; /* argument for handler */
56 modspecific_t data; /* module specific data */
59 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
61 static TAILQ_HEAD(modulelist, module) modules;
63 static int nextid = 1;
64 static void module_shutdown(void *, int);
67 modevent_nop(module_t mod, int what, void *arg)
81 module_init(void *arg)
84 sx_init(&modules_sx, "module subsystem sx lock");
86 EVENTHANDLER_REGISTER(shutdown_final, module_shutdown, NULL,
87 SHUTDOWN_PRI_DEFAULT);
90 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, NULL);
93 module_shutdown(void *arg1, int arg2)
101 TAILQ_FOREACH_REVERSE(mod, &modules, modulelist, link)
102 MOD_EVENT(mod, MOD_SHUTDOWN);
108 module_register_init(const void *arg)
110 const moduledata_t *data = (const moduledata_t *)arg;
116 mod = module_lookupbyname(data->name);
118 panic("module_register_init: module named %s not found\n",
121 error = MOD_EVENT(mod, MOD_LOAD);
123 MOD_EVENT(mod, MOD_UNLOAD);
127 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
128 " %d\n", data->name, (void *)data->evhand, data->priv,
134 * Once a module is successfully loaded, move
135 * it to the head of the module list for this
136 * linker file. This resorts the list so that
137 * when the kernel linker iterates over the
138 * modules to unload them, it will unload them
139 * in the reverse order they were loaded.
141 TAILQ_REMOVE(&mod->file->modules, mod, flink);
142 TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
150 module_register(const moduledata_t *data, linker_file_t container)
156 newmod = module_lookupbyname(data->name);
157 if (newmod != NULL) {
159 printf("%s: cannot register %s from %s; already loaded from %s\n",
160 __func__, data->name, container->filename, newmod->file->filename);
163 namelen = strlen(data->name) + 1;
164 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
166 newmod->id = nextid++;
167 newmod->name = (char *)(newmod + 1);
168 strcpy(newmod->name, data->name);
169 newmod->handler = data->evhand ? data->evhand : modevent_nop;
170 newmod->arg = data->priv;
171 bzero(&newmod->data, sizeof(newmod->data));
172 TAILQ_INSERT_TAIL(&modules, newmod, link);
175 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
176 newmod->file = container;
182 module_reference(module_t mod)
187 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
192 module_release(module_t mod)
198 panic("module_release: bad reference count");
200 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
203 if (mod->refs == 0) {
204 TAILQ_REMOVE(&modules, mod, link);
206 TAILQ_REMOVE(&mod->file->modules, mod, flink);
212 module_lookupbyname(const char *name)
219 TAILQ_FOREACH(mod, &modules, link) {
220 err = strcmp(mod->name, name);
228 module_lookupbyid(int modid)
234 TAILQ_FOREACH(mod, &modules, link)
235 if (mod->id == modid)
241 module_quiesce(module_t mod)
246 error = MOD_EVENT(mod, MOD_QUIESCE);
248 if (error == EOPNOTSUPP || error == EINVAL)
254 module_unload(module_t mod)
259 error = MOD_EVENT(mod, MOD_UNLOAD);
265 module_getid(module_t mod)
273 module_getfnext(module_t mod)
277 return (TAILQ_NEXT(mod, flink));
281 module_getname(module_t mod)
289 module_setspecific(module_t mod, modspecific_t *datap)
297 module_file(module_t mod)
307 sys_modnext(struct thread *td, struct modnext_args *uap)
312 td->td_retval[0] = -1;
315 if (uap->modid == 0) {
316 mod = TAILQ_FIRST(&modules);
318 td->td_retval[0] = mod->id;
323 mod = module_lookupbyid(uap->modid);
328 if (TAILQ_NEXT(mod, link))
329 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
331 td->td_retval[0] = 0;
338 sys_modfnext(struct thread *td, struct modfnext_args *uap)
343 td->td_retval[0] = -1;
346 mod = module_lookupbyid(uap->modid);
351 if (TAILQ_NEXT(mod, flink))
352 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
354 td->td_retval[0] = 0;
360 struct module_stat_v1 {
361 int version; /* set to sizeof(struct module_stat) */
362 char name[MAXMODNAMEV1V2];
367 struct module_stat_v2 {
368 int version; /* set to sizeof(struct module_stat) */
369 char name[MAXMODNAMEV1V2];
376 sys_modstat(struct thread *td, struct modstat_args *uap)
381 int id, namelen, refs, version;
382 struct module_stat *stat;
383 struct module_stat_v2 *stat_v2;
388 mod = module_lookupbyid(uap->modid);
401 * Check the version of the user's structure.
403 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
405 is_v1v2 = (version == sizeof(struct module_stat_v1) ||
406 version == sizeof(struct module_stat_v2));
407 if (!is_v1v2 && version != sizeof(struct module_stat))
409 namelen = strlen(mod->name) + 1;
410 if (is_v1v2 && namelen > MAXMODNAMEV1V2)
411 namelen = MAXMODNAMEV1V2;
412 else if (namelen > MAXMODNAMEV3)
413 namelen = MAXMODNAMEV3;
414 if ((error = copyout(name, &stat->name[0], namelen)) != 0)
417 /* Extending MAXMODNAME gives an offset change for v3. */
419 stat_v2 = (struct module_stat_v2 *)stat;
420 if ((error = copyout(&refs, &stat_v2->refs, sizeof(int))) != 0)
422 if ((error = copyout(&id, &stat_v2->id, sizeof(int))) != 0)
425 if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
427 if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
432 * >v1 stat includes module data.
434 if (version == sizeof(struct module_stat_v2)) {
435 if ((error = copyout(&data, &stat_v2->data,
438 } else if (version == sizeof(struct module_stat)) {
439 if ((error = copyout(&data, &stat->data,
443 td->td_retval[0] = 0;
448 sys_modfind(struct thread *td, struct modfind_args *uap)
451 char name[MAXMODNAMEV3];
454 if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
458 mod = module_lookupbyname(name);
462 td->td_retval[0] = module_getid(mod);
467 MODULE_VERSION(kernel, __FreeBSD_version);
469 #ifdef COMPAT_FREEBSD32
470 #include <sys/mount.h>
471 #include <sys/socket.h>
472 #include <compat/freebsd32/freebsd32_util.h>
473 #include <compat/freebsd32/freebsd32.h>
474 #include <compat/freebsd32/freebsd32_proto.h>
476 typedef union modspecific32 {
483 struct module_stat32_v2 {
485 char name[MAXMODNAMEV1V2];
488 modspecific32_t data;
491 struct module_stat32 {
493 char name[MAXMODNAME];
496 modspecific32_t data;
500 freebsd32_modstat(struct thread *td, struct freebsd32_modstat_args *uap)
503 modspecific32_t data32;
505 int id, namelen, refs, version;
506 struct module_stat32 *stat32;
507 struct module_stat32_v2 *stat32_v2;
512 mod = module_lookupbyid(uap->modid);
521 _Static_assert(sizeof(data32) == sizeof(data32.uintval),
522 "bad modspecific32_t size");
523 CP(mod->data, data32, uintval);
527 if ((error = copyin(&stat32->version, &version, sizeof(version))) != 0)
529 is_v1v2 = (version == sizeof(struct module_stat_v1) ||
530 version == sizeof(struct module_stat32_v2));
531 if (!is_v1v2 && version != sizeof(struct module_stat32))
533 namelen = strlen(mod->name) + 1;
534 if (is_v1v2 && namelen > MAXMODNAMEV1V2)
535 namelen = MAXMODNAMEV1V2;
536 else if (namelen > MAXMODNAMEV3)
537 namelen = MAXMODNAMEV3;
538 if ((error = copyout(name, &stat32->name[0], namelen)) != 0)
541 /* Extending MAXMODNAME gives an offset change for v3. */
543 stat32_v2 = (struct module_stat32_v2 *)stat32;
544 if ((error = copyout(&refs, &stat32_v2->refs, sizeof(int))) != 0)
546 if ((error = copyout(&id, &stat32_v2->id, sizeof(int))) != 0)
549 if ((error = copyout(&refs, &stat32->refs, sizeof(int))) != 0)
551 if ((error = copyout(&id, &stat32->id, sizeof(int))) != 0)
556 * >v1 stat includes module data.
558 if (version == sizeof(struct module_stat32_v2)) {
559 if ((error = copyout(&data32, &stat32_v2->data,
560 sizeof(data32))) != 0)
562 } else if (version == sizeof(struct module_stat32)) {
563 if ((error = copyout(&data32, &stat32->data,
564 sizeof(data32))) != 0)
567 td->td_retval[0] = 0;