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/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/eventhandler.h>
33 #include <sys/malloc.h>
34 #include <sys/sysproto.h>
35 #include <sys/sysent.h>
38 #include <sys/mutex.h>
39 #include <sys/reboot.h>
41 #include <sys/module.h>
42 #include <sys/linker.h>
44 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
47 TAILQ_ENTRY(module) link; /* chain together all modules */
48 TAILQ_ENTRY(module) flink; /* all modules in a file */
49 struct linker_file *file; /* file which contains this module */
50 int refs; /* reference count */
51 int id; /* unique id number */
52 char *name; /* module name */
53 modeventhand_t handler; /* event handler */
54 void *arg; /* argument for handler */
55 modspecific_t data; /* module specific data */
58 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
60 static TAILQ_HEAD(modulelist, module) modules;
62 static int nextid = 1;
63 static void module_shutdown(void *, int);
66 modevent_nop(module_t mod, int what, void *arg)
80 module_init(void *arg)
83 sx_init(&modules_sx, "module subsystem sx lock");
85 EVENTHANDLER_REGISTER(shutdown_final, module_shutdown, NULL,
86 SHUTDOWN_PRI_DEFAULT);
89 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, NULL);
92 module_shutdown(void *arg1, int arg2)
100 TAILQ_FOREACH_REVERSE(mod, &modules, modulelist, link)
101 MOD_EVENT(mod, MOD_SHUTDOWN);
107 module_register_init(const void *arg)
109 const moduledata_t *data = (const moduledata_t *)arg;
115 mod = module_lookupbyname(data->name);
117 panic("module_register_init: module named %s not found\n",
120 error = MOD_EVENT(mod, MOD_LOAD);
122 MOD_EVENT(mod, MOD_UNLOAD);
126 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
127 " %d\n", data->name, (void *)data->evhand, data->priv,
133 * Once a module is successfully loaded, move
134 * it to the head of the module list for this
135 * linker file. This resorts the list so that
136 * when the kernel linker iterates over the
137 * modules to unload them, it will unload them
138 * in the reverse order they were loaded.
140 TAILQ_REMOVE(&mod->file->modules, mod, flink);
141 TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
149 module_register(const moduledata_t *data, linker_file_t container)
155 newmod = module_lookupbyname(data->name);
156 if (newmod != NULL) {
158 printf("%s: cannot register %s from %s; already loaded from %s\n",
159 __func__, data->name, container->filename, newmod->file->filename);
162 namelen = strlen(data->name) + 1;
163 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
165 newmod->id = nextid++;
166 newmod->name = (char *)(newmod + 1);
167 strcpy(newmod->name, data->name);
168 newmod->handler = data->evhand ? data->evhand : modevent_nop;
169 newmod->arg = data->priv;
170 bzero(&newmod->data, sizeof(newmod->data));
171 TAILQ_INSERT_TAIL(&modules, newmod, link);
174 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
175 newmod->file = container;
181 module_reference(module_t mod)
186 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
191 module_release(module_t mod)
197 panic("module_release: bad reference count");
199 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
202 if (mod->refs == 0) {
203 TAILQ_REMOVE(&modules, mod, link);
205 TAILQ_REMOVE(&mod->file->modules, mod, flink);
211 module_lookupbyname(const char *name)
218 TAILQ_FOREACH(mod, &modules, link) {
219 err = strcmp(mod->name, name);
227 module_lookupbyid(int modid)
233 TAILQ_FOREACH(mod, &modules, link)
234 if (mod->id == modid)
240 module_quiesce(module_t mod)
245 error = MOD_EVENT(mod, MOD_QUIESCE);
247 if (error == EOPNOTSUPP || error == EINVAL)
253 module_unload(module_t mod)
258 error = MOD_EVENT(mod, MOD_UNLOAD);
264 module_getid(module_t mod)
272 module_getfnext(module_t mod)
276 return (TAILQ_NEXT(mod, flink));
280 module_getname(module_t mod)
288 module_setspecific(module_t mod, modspecific_t *datap)
296 module_file(module_t mod)
306 sys_modnext(struct thread *td, struct modnext_args *uap)
311 td->td_retval[0] = -1;
314 if (uap->modid == 0) {
315 mod = TAILQ_FIRST(&modules);
317 td->td_retval[0] = mod->id;
322 mod = module_lookupbyid(uap->modid);
327 if (TAILQ_NEXT(mod, link))
328 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
330 td->td_retval[0] = 0;
337 sys_modfnext(struct thread *td, struct modfnext_args *uap)
342 td->td_retval[0] = -1;
345 mod = module_lookupbyid(uap->modid);
350 if (TAILQ_NEXT(mod, flink))
351 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
353 td->td_retval[0] = 0;
359 struct module_stat_v1 {
360 int version; /* set to sizeof(struct module_stat) */
361 char name[MAXMODNAMEV1V2];
366 struct module_stat_v2 {
367 int version; /* set to sizeof(struct module_stat) */
368 char name[MAXMODNAMEV1V2];
375 sys_modstat(struct thread *td, struct modstat_args *uap)
380 int id, namelen, refs, version;
381 struct module_stat *stat;
382 struct module_stat_v2 *stat_v2;
387 mod = module_lookupbyid(uap->modid);
400 * Check the version of the user's structure.
402 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
404 is_v1v2 = (version == sizeof(struct module_stat_v1) ||
405 version == sizeof(struct module_stat_v2));
406 if (!is_v1v2 && version != sizeof(struct module_stat))
408 namelen = strlen(mod->name) + 1;
409 if (is_v1v2 && namelen > MAXMODNAMEV1V2)
410 namelen = MAXMODNAMEV1V2;
411 else if (namelen > MAXMODNAMEV3)
412 namelen = MAXMODNAMEV3;
413 if ((error = copyout(name, &stat->name[0], namelen)) != 0)
416 /* Extending MAXMODNAME gives an offset change for v3. */
418 stat_v2 = (struct module_stat_v2 *)stat;
419 if ((error = copyout(&refs, &stat_v2->refs, sizeof(int))) != 0)
421 if ((error = copyout(&id, &stat_v2->id, sizeof(int))) != 0)
424 if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
426 if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
431 * >v1 stat includes module data.
433 if (version == sizeof(struct module_stat_v2)) {
434 if ((error = copyout(&data, &stat_v2->data,
437 } else if (version == sizeof(struct module_stat)) {
438 if ((error = copyout(&data, &stat->data,
442 td->td_retval[0] = 0;
447 sys_modfind(struct thread *td, struct modfind_args *uap)
450 char name[MAXMODNAMEV3];
453 if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
457 mod = module_lookupbyname(name);
461 td->td_retval[0] = module_getid(mod);
466 MODULE_VERSION(kernel, __FreeBSD_version);
468 #ifdef COMPAT_FREEBSD32
469 #include <sys/mount.h>
470 #include <sys/socket.h>
471 #include <compat/freebsd32/freebsd32_util.h>
472 #include <compat/freebsd32/freebsd32.h>
473 #include <compat/freebsd32/freebsd32_proto.h>
475 typedef union modspecific32 {
482 struct module_stat32_v2 {
484 char name[MAXMODNAMEV1V2];
487 modspecific32_t data;
490 struct module_stat32 {
492 char name[MAXMODNAME];
495 modspecific32_t data;
499 freebsd32_modstat(struct thread *td, struct freebsd32_modstat_args *uap)
502 modspecific32_t data32;
504 int id, namelen, refs, version;
505 struct module_stat32 *stat32;
506 struct module_stat32_v2 *stat32_v2;
511 mod = module_lookupbyid(uap->modid);
520 _Static_assert(sizeof(data32) == sizeof(data32.uintval),
521 "bad modspecific32_t size");
522 CP(mod->data, data32, uintval);
526 if ((error = copyin(&stat32->version, &version, sizeof(version))) != 0)
528 is_v1v2 = (version == sizeof(struct module_stat_v1) ||
529 version == sizeof(struct module_stat32_v2));
530 if (!is_v1v2 && version != sizeof(struct module_stat32))
532 namelen = strlen(mod->name) + 1;
533 if (is_v1v2 && namelen > MAXMODNAMEV1V2)
534 namelen = MAXMODNAMEV1V2;
535 else if (namelen > MAXMODNAMEV3)
536 namelen = MAXMODNAMEV3;
537 if ((error = copyout(name, &stat32->name[0], namelen)) != 0)
540 /* Extending MAXMODNAME gives an offset change for v3. */
542 stat32_v2 = (struct module_stat32_v2 *)stat32;
543 if ((error = copyout(&refs, &stat32_v2->refs, sizeof(int))) != 0)
545 if ((error = copyout(&id, &stat32_v2->id, sizeof(int))) != 0)
548 if ((error = copyout(&refs, &stat32->refs, sizeof(int))) != 0)
550 if ((error = copyout(&id, &stat32->id, sizeof(int))) != 0)
555 * >v1 stat includes module data.
557 if (version == sizeof(struct module_stat32_v2)) {
558 if ((error = copyout(&data32, &stat32_v2->data,
559 sizeof(data32))) != 0)
561 } else if (version == sizeof(struct module_stat32)) {
562 if ((error = copyout(&data32, &stat32->data,
563 sizeof(data32))) != 0)
566 td->td_retval[0] = 0;