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
26 * $FreeBSD: src/sys/kern/kern_module.c,v 1.21 1999/11/08 06:53:30 peter Exp $
27 * $DragonFly: src/sys/kern/kern_module.c,v 1.8 2004/05/10 10:51:31 hmp Exp $
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>
37 #include <sys/module.h>
38 #include <sys/linker.h>
41 MALLOC_DEFINE(M_MODULE, "module", "module data structures");
43 typedef TAILQ_HEAD(, module) modulelist_t;
45 TAILQ_ENTRY(module) link; /* chain together all modules */
46 TAILQ_ENTRY(module) flink; /* all modules in a file */
47 struct linker_file* file; /* file which contains this module */
48 int refs; /* reference count */
49 int id; /* unique id number */
50 char *name; /* module name */
51 modeventhand_t handler; /* event handler */
52 void *arg; /* argument for handler */
53 modspecific_t data; /* module specific data */
56 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
58 static modulelist_t modules;
59 static int nextid = 1;
61 static void module_shutdown(void*, int);
64 modevent_nop(module_t mod, int what, void* arg)
71 module_init(void* arg)
74 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
75 SHUTDOWN_PRI_DEFAULT);
78 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
81 module_shutdown(void* arg1, int arg2)
85 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
86 MOD_EVENT(mod, MOD_SHUTDOWN);
90 module_register_init(const void *arg)
92 const moduledata_t* data = (const moduledata_t*) arg;
96 mod = module_lookupbyname(data->name);
99 panic("module_register_init: module named %s not found", data->name);
101 /* temporary kludge until kernel `file' attachment registers modules */
102 error = module_register(data, linker_kernel_file);
104 panic("module_register_init: register of module failed! %d", error);
105 mod = module_lookupbyname(data->name);
107 panic("module_register_init: module STILL not found!");
110 error = MOD_EVENT(mod, MOD_LOAD);
112 MOD_EVENT(mod, MOD_UNLOAD);
114 printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
115 data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
120 module_register(const moduledata_t *data, linker_file_t container)
125 newmod = module_lookupbyname(data->name);
126 if (newmod != NULL) {
127 printf("module_register: module %s already exists!\n", data->name);
130 namelen = strlen(data->name) + 1;
131 newmod = (module_t) malloc(sizeof(struct module) + namelen,
137 newmod->id = nextid++;
138 newmod->name = (char *) (newmod + 1);
139 strcpy(newmod->name, data->name);
140 newmod->handler = data->evhand ? data->evhand : modevent_nop;
141 newmod->arg = data->priv;
142 bzero(&newmod->data, sizeof(newmod->data));
143 TAILQ_INSERT_TAIL(&modules, newmod, link);
145 if (container == NULL)
146 container = linker_current_file;
148 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
149 newmod->file = container;
155 module_reference(module_t mod)
157 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
165 * Release ref on the module and return the new reference count. If 0
166 * is returned, the module has been removed from its list and freed.
169 module_release(module_t mod)
174 panic("module_release: bad reference count");
176 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
180 TAILQ_REMOVE(&modules, mod, link);
182 TAILQ_REMOVE(&mod->file->modules, mod, flink);
190 module_lookupbyname(const char* name)
194 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
195 if (!strcmp(mod->name, name))
203 module_lookupbyid(int modid)
207 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
208 if (mod->id == modid)
216 module_unload(module_t mod)
218 return MOD_EVENT(mod, MOD_UNLOAD);
222 module_getid(module_t mod)
228 module_getfnext(module_t mod)
230 return TAILQ_NEXT(mod, flink);
234 module_setspecific(module_t mod, modspecific_t *datap)
243 modnext(struct modnext_args *uap)
247 uap->sysmsg_result = -1;
248 if (SCARG(uap, modid) == 0) {
249 mod = TAILQ_FIRST(&modules);
251 uap->sysmsg_result = mod->id;
257 mod = module_lookupbyid(SCARG(uap, modid));
261 if (TAILQ_NEXT(mod, link))
262 uap->sysmsg_result = TAILQ_NEXT(mod, link)->id;
264 uap->sysmsg_result = 0;
269 modfnext(struct modfnext_args *uap)
273 uap->sysmsg_result = -1;
275 mod = module_lookupbyid(SCARG(uap, modid));
279 if (TAILQ_NEXT(mod, flink))
280 uap->sysmsg_result = TAILQ_NEXT(mod, flink)->id;
282 uap->sysmsg_result = 0;
286 struct module_stat_v1 {
287 int version; /* set to sizeof(struct module_stat) */
288 char name[MAXMODNAME];
294 modstat(struct modstat_args *uap)
300 struct module_stat* stat;
302 mod = module_lookupbyid(SCARG(uap, modid));
306 stat = SCARG(uap, stat);
309 * Check the version of the user's structure.
311 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
313 if (version != sizeof(struct module_stat_v1)
314 && version != sizeof(struct module_stat)) {
319 namelen = strlen(mod->name) + 1;
320 if (namelen > MAXMODNAME)
321 namelen = MAXMODNAME;
322 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
325 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
327 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
331 * >v1 stat includes module data.
333 if (version == sizeof(struct module_stat)) {
334 if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
338 uap->sysmsg_result = 0;
345 modfind(struct modfind_args *uap)
348 char name[MAXMODNAME];
351 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
354 mod = module_lookupbyname(name);
358 uap->sysmsg_result = mod->id;