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 $
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>
36 #include <sys/module.h>
37 #include <sys/linker.h>
40 #define M_MODULE M_TEMP /* XXX */
42 typedef TAILQ_HEAD(, module) modulelist_t;
44 TAILQ_ENTRY(module) link; /* chain together all modules */
45 TAILQ_ENTRY(module) flink; /* all modules in a file */
46 struct linker_file* file; /* file which contains this module */
47 int refs; /* reference count */
48 int id; /* unique id number */
49 char *name; /* module name */
50 modeventhand_t handler; /* event handler */
51 void *arg; /* argument for handler */
52 modspecific_t data; /* module specific data */
55 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
57 static modulelist_t modules;
58 static int nextid = 1;
60 static void module_shutdown(void*, int);
63 modevent_nop(module_t mod, int what, void* arg)
70 module_init(void* arg)
73 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
74 SHUTDOWN_PRI_DEFAULT);
77 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
80 module_shutdown(void* arg1, int arg2)
84 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
85 MOD_EVENT(mod, MOD_SHUTDOWN);
89 module_register_init(const void *arg)
91 const moduledata_t* data = (const moduledata_t*) arg;
95 mod = module_lookupbyname(data->name);
98 panic("module_register_init: module named %s not found\n", data->name);
100 /* temporary kludge until kernel `file' attachment registers modules */
101 error = module_register(data, linker_kernel_file);
103 panic("module_register_init: register of module failed! %d", error);
104 mod = module_lookupbyname(data->name);
106 panic("module_register_init: module STILL not found!");
109 error = MOD_EVENT(mod, MOD_LOAD);
111 MOD_EVENT(mod, MOD_UNLOAD);
113 printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
114 data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
119 module_register(const moduledata_t *data, linker_file_t container)
124 newmod = module_lookupbyname(data->name);
125 if (newmod != NULL) {
126 printf("module_register: module %s already exists!\n", data->name);
129 namelen = strlen(data->name) + 1;
130 newmod = (module_t) malloc(sizeof(struct module) + namelen,
136 newmod->id = nextid++;
137 newmod->name = (char *) (newmod + 1);
138 strcpy(newmod->name, data->name);
139 newmod->handler = data->evhand ? data->evhand : modevent_nop;
140 newmod->arg = data->priv;
141 bzero(&newmod->data, sizeof(newmod->data));
142 TAILQ_INSERT_TAIL(&modules, newmod, link);
144 if (container == NULL)
145 container = linker_current_file;
147 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
148 newmod->file = container;
154 module_reference(module_t mod)
156 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
162 module_release(module_t mod)
165 panic("module_release: bad reference count");
167 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
170 if (mod->refs == 0) {
171 TAILQ_REMOVE(&modules, mod, link);
173 TAILQ_REMOVE(&mod->file->modules, mod, flink);
180 module_lookupbyname(const char* name)
184 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
185 if (!strcmp(mod->name, name))
193 module_lookupbyid(int modid)
197 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
198 if (mod->id == modid)
206 module_unload(module_t mod)
208 return MOD_EVENT(mod, MOD_UNLOAD);
212 module_getid(module_t mod)
218 module_getfnext(module_t mod)
220 return TAILQ_NEXT(mod, flink);
224 module_setspecific(module_t mod, modspecific_t *datap)
233 modnext(struct proc* p, struct modnext_args* uap)
238 if (SCARG(uap, modid) == 0) {
239 mod = TAILQ_FIRST(&modules);
241 p->p_retval[0] = mod->id;
247 mod = module_lookupbyid(SCARG(uap, modid));
251 if (TAILQ_NEXT(mod, link))
252 p->p_retval[0] = TAILQ_NEXT(mod, link)->id;
259 modfnext(struct proc* p, struct modfnext_args* uap)
265 mod = module_lookupbyid(SCARG(uap, modid));
269 if (TAILQ_NEXT(mod, flink))
270 p->p_retval[0] = TAILQ_NEXT(mod, flink)->id;
276 struct module_stat_v1 {
277 int version; /* set to sizeof(struct module_stat) */
278 char name[MAXMODNAME];
284 modstat(struct proc* p, struct modstat_args* uap)
290 struct module_stat* stat;
292 mod = module_lookupbyid(SCARG(uap, modid));
296 stat = SCARG(uap, stat);
299 * Check the version of the user's structure.
301 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
303 if (version != sizeof(struct module_stat_v1)
304 && version != sizeof(struct module_stat)) {
309 namelen = strlen(mod->name) + 1;
310 if (namelen > MAXMODNAME)
311 namelen = MAXMODNAME;
312 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
315 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
317 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
321 * >v1 stat includes module data.
323 if (version == sizeof(struct module_stat)) {
324 if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
335 modfind(struct proc* p, struct modfind_args* uap)
338 char name[MAXMODNAME];
341 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
344 mod = module_lookupbyname(name);
348 p->p_retval[0] = mod->id;