2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 "opt_compat.h"
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/eventhandler.h>
38 #include <sys/malloc.h>
39 #include <sys/sysproto.h>
40 #include <sys/sysent.h>
43 #include <sys/mutex.h>
44 #include <sys/reboot.h>
46 #include <sys/module.h>
47 #include <sys/linker.h>
49 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
52 TAILQ_ENTRY(module) link; /* chain together all modules */
53 TAILQ_ENTRY(module) flink; /* all modules in a file */
54 struct linker_file *file; /* file which contains this module */
55 int refs; /* reference count */
56 int id; /* unique id number */
57 char *name; /* module name */
58 modeventhand_t handler; /* event handler */
59 void *arg; /* argument for handler */
60 modspecific_t data; /* module specific data */
63 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
65 static TAILQ_HEAD(modulelist, module) modules;
67 static int nextid = 1;
68 static void module_shutdown(void *, int);
71 modevent_nop(module_t mod, int what, void *arg)
85 module_init(void *arg)
88 sx_init(&modules_sx, "module subsystem sx lock");
90 EVENTHANDLER_REGISTER(shutdown_final, module_shutdown, NULL,
91 SHUTDOWN_PRI_DEFAULT);
94 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
97 module_shutdown(void *arg1, int arg2)
101 if (arg2 & RB_NOSYNC)
105 TAILQ_FOREACH_REVERSE(mod, &modules, modulelist, link)
106 MOD_EVENT(mod, MOD_SHUTDOWN);
112 module_register_init(const void *arg)
114 const moduledata_t *data = (const moduledata_t *)arg;
120 mod = module_lookupbyname(data->name);
122 panic("module_register_init: module named %s not found\n",
125 error = MOD_EVENT(mod, MOD_LOAD);
127 MOD_EVENT(mod, MOD_UNLOAD);
131 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
132 " %d\n", data->name, (void *)data->evhand, data->priv,
138 * Once a module is successfully loaded, move
139 * it to the head of the module list for this
140 * linker file. This resorts the list so that
141 * when the kernel linker iterates over the
142 * modules to unload them, it will unload them
143 * in the reverse order they were loaded.
145 TAILQ_REMOVE(&mod->file->modules, mod, flink);
146 TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
154 module_register(const moduledata_t *data, linker_file_t container)
160 newmod = module_lookupbyname(data->name);
161 if (newmod != NULL) {
163 printf("%s: cannot register %s from %s; already loaded from %s\n",
164 __func__, data->name, container->filename, newmod->file->filename);
167 namelen = strlen(data->name) + 1;
168 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
170 newmod->id = nextid++;
171 newmod->name = (char *)(newmod + 1);
172 strcpy(newmod->name, data->name);
173 newmod->handler = data->evhand ? data->evhand : modevent_nop;
174 newmod->arg = data->priv;
175 bzero(&newmod->data, sizeof(newmod->data));
176 TAILQ_INSERT_TAIL(&modules, newmod, link);
179 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
180 newmod->file = container;
186 module_reference(module_t mod)
191 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
196 module_release(module_t mod)
202 panic("module_release: bad reference count");
204 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
207 if (mod->refs == 0) {
208 TAILQ_REMOVE(&modules, mod, link);
210 TAILQ_REMOVE(&mod->file->modules, mod, flink);
216 module_lookupbyname(const char *name)
223 TAILQ_FOREACH(mod, &modules, link) {
224 err = strcmp(mod->name, name);
232 module_lookupbyid(int modid)
238 TAILQ_FOREACH(mod, &modules, link)
239 if (mod->id == modid)
245 module_quiesce(module_t mod)
250 error = MOD_EVENT(mod, MOD_QUIESCE);
252 if (error == EOPNOTSUPP || error == EINVAL)
258 module_unload(module_t mod)
263 error = MOD_EVENT(mod, MOD_UNLOAD);
269 module_getid(module_t mod)
277 module_getfnext(module_t mod)
281 return (TAILQ_NEXT(mod, flink));
285 module_getname(module_t mod)
293 module_setspecific(module_t mod, modspecific_t *datap)
301 module_file(module_t mod)
311 sys_modnext(struct thread *td, struct modnext_args *uap)
316 td->td_retval[0] = -1;
319 if (uap->modid == 0) {
320 mod = TAILQ_FIRST(&modules);
322 td->td_retval[0] = mod->id;
327 mod = module_lookupbyid(uap->modid);
332 if (TAILQ_NEXT(mod, link))
333 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
335 td->td_retval[0] = 0;
342 sys_modfnext(struct thread *td, struct modfnext_args *uap)
347 td->td_retval[0] = -1;
350 mod = module_lookupbyid(uap->modid);
355 if (TAILQ_NEXT(mod, flink))
356 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
358 td->td_retval[0] = 0;
364 struct module_stat_v1 {
365 int version; /* set to sizeof(struct module_stat) */
366 char name[MAXMODNAME];
372 sys_modstat(struct thread *td, struct modstat_args *uap)
377 int id, namelen, refs, version;
378 struct module_stat *stat;
382 mod = module_lookupbyid(uap->modid);
395 * Check the version of the user's structure.
397 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
399 if (version != sizeof(struct module_stat_v1)
400 && version != sizeof(struct module_stat))
402 namelen = strlen(mod->name) + 1;
403 if (namelen > MAXMODNAME)
404 namelen = MAXMODNAME;
405 if ((error = copyout(name, &stat->name[0], namelen)) != 0)
408 if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
410 if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
414 * >v1 stat includes module data.
416 if (version == sizeof(struct module_stat))
417 if ((error = copyout(&data, &stat->data,
420 td->td_retval[0] = 0;
425 sys_modfind(struct thread *td, struct modfind_args *uap)
428 char name[MAXMODNAME];
431 if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
435 mod = module_lookupbyname(name);
439 td->td_retval[0] = module_getid(mod);
444 MODULE_VERSION(kernel, __FreeBSD_version);
446 #ifdef COMPAT_FREEBSD32
447 #include <sys/mount.h>
448 #include <sys/socket.h>
449 #include <compat/freebsd32/freebsd32_util.h>
450 #include <compat/freebsd32/freebsd32.h>
451 #include <compat/freebsd32/freebsd32_proto.h>
453 typedef union modspecific32 {
460 struct module_stat32 {
462 char name[MAXMODNAME];
465 modspecific32_t data;
469 freebsd32_modstat(struct thread *td, struct freebsd32_modstat_args *uap)
472 modspecific32_t data32;
474 int id, namelen, refs, version;
475 struct module_stat32 *stat32;
479 mod = module_lookupbyid(uap->modid);
488 CP(mod->data, data32, intval);
489 CP(mod->data, data32, uintval);
490 CP(mod->data, data32, longval);
491 CP(mod->data, data32, ulongval);
495 if ((error = copyin(&stat32->version, &version, sizeof(version))) != 0)
497 if (version != sizeof(struct module_stat_v1)
498 && version != sizeof(struct module_stat32))
500 namelen = strlen(mod->name) + 1;
501 if (namelen > MAXMODNAME)
502 namelen = MAXMODNAME;
503 if ((error = copyout(name, &stat32->name[0], namelen)) != 0)
506 if ((error = copyout(&refs, &stat32->refs, sizeof(int))) != 0)
508 if ((error = copyout(&id, &stat32->id, sizeof(int))) != 0)
512 * >v1 stat includes module data.
514 if (version == sizeof(struct module_stat32))
515 if ((error = copyout(&data32, &stat32->data,
516 sizeof(data32))) != 0)
518 td->td_retval[0] = 0;