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
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>
40 #include <sys/module.h>
41 #include <sys/linker.h>
43 static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
45 typedef TAILQ_HEAD(, module) modulelist_t;
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 modulelist_t modules;
62 static int nextid = 1;
63 static void module_shutdown(void *, int);
66 modevent_nop(module_t mod, int what, void *arg)
72 module_init(void *arg)
75 sx_init(&modules_sx, "module subsystem sx lock");
77 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
78 SHUTDOWN_PRI_DEFAULT);
81 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0)
84 module_shutdown(void *arg1, int arg2)
89 TAILQ_FOREACH(mod, &modules, link)
90 MOD_EVENT(mod, MOD_SHUTDOWN);
95 module_register_init(const void *arg)
97 const moduledata_t *data = (const moduledata_t *)arg;
102 mod = module_lookupbyname(data->name);
104 panic("module_register_init: module named %s not found\n",
107 error = MOD_EVENT(mod, MOD_LOAD);
109 MOD_EVENT(mod, MOD_UNLOAD);
113 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
114 " %d\n", data->name, (void *)data->evhand, data->priv,
120 module_register(const moduledata_t *data, linker_file_t container)
126 newmod = module_lookupbyname(data->name);
127 if (newmod != NULL) {
129 printf("module_register: module %s already exists!\n",
134 namelen = strlen(data->name) + 1;
135 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
140 newmod->id = nextid++;
141 newmod->name = (char *)(newmod + 1);
142 strcpy(newmod->name, data->name);
143 newmod->handler = data->evhand ? data->evhand : modevent_nop;
144 newmod->arg = data->priv;
145 bzero(&newmod->data, sizeof(newmod->data));
146 TAILQ_INSERT_TAIL(&modules, newmod, link);
149 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
150 newmod->file = container;
156 module_reference(module_t mod)
161 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
166 module_release(module_t mod)
172 panic("module_release: bad reference count");
174 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
177 if (mod->refs == 0) {
178 TAILQ_REMOVE(&modules, mod, link);
180 TAILQ_REMOVE(&mod->file->modules, mod, flink);
188 module_lookupbyname(const char *name)
195 TAILQ_FOREACH(mod, &modules, link) {
196 err = strcmp(mod->name, name);
204 module_lookupbyid(int modid)
210 TAILQ_FOREACH(mod, &modules, link)
211 if (mod->id == modid)
217 module_unload(module_t mod)
220 return (MOD_EVENT(mod, MOD_UNLOAD));
224 module_getid(module_t mod)
232 module_getfnext(module_t mod)
236 return (TAILQ_NEXT(mod, flink));
240 module_setspecific(module_t mod, modspecific_t *datap)
254 modnext(struct thread *td, struct modnext_args *uap)
261 td->td_retval[0] = -1;
263 if (SCARG(uap, modid) == 0) {
264 mod = TAILQ_FIRST(&modules);
266 td->td_retval[0] = mod->id;
271 mod = module_lookupbyid(SCARG(uap, modid));
276 if (TAILQ_NEXT(mod, link))
277 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
279 td->td_retval[0] = 0;
290 modfnext(struct thread *td, struct modfnext_args *uap)
295 td->td_retval[0] = -1;
300 mod = module_lookupbyid(SCARG(uap, modid));
305 if (TAILQ_NEXT(mod, flink))
306 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
308 td->td_retval[0] = 0;
315 struct module_stat_v1 {
316 int version; /* set to sizeof(struct module_stat) */
317 char name[MAXMODNAME];
326 modstat(struct thread *td, struct modstat_args *uap)
331 int id, namelen, refs, version;
332 struct module_stat *stat;
338 mod = module_lookupbyid(SCARG(uap, modid));
349 stat = SCARG(uap, stat);
352 * Check the version of the user's structure.
354 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
356 if (version != sizeof(struct module_stat_v1)
357 && version != sizeof(struct module_stat)) {
361 namelen = strlen(mod->name) + 1;
362 if (namelen > MAXMODNAME)
363 namelen = MAXMODNAME;
364 if ((error = copyout(name, &stat->name[0], namelen)) != 0)
367 if ((error = copyout(&refs, &stat->refs, sizeof(int))) != 0)
369 if ((error = copyout(&id, &stat->id, sizeof(int))) != 0)
373 * >v1 stat includes module data.
375 if (version == sizeof(struct module_stat)) {
376 if ((error = copyout(&data, &stat->data,
380 td->td_retval[0] = 0;
390 modfind(struct thread *td, struct modfind_args *uap)
393 char name[MAXMODNAME];
396 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
401 mod = module_lookupbyname(name);
405 td->td_retval[0] = module_getid(mod);