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 * $Id: kern_module.c,v 1.7 1998/07/11 07:45:40 bde Exp $
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/sysproto.h>
34 #include <sys/sysent.h>
35 #include <sys/module.h>
36 #include <sys/linker.h>
39 #define M_MODULE M_TEMP /* XXX */
41 typedef TAILQ_HEAD(, module) modulelist_t;
43 TAILQ_ENTRY(module) link; /* chain together all modules */
44 TAILQ_ENTRY(module) flink; /* all modules in a file */
45 struct linker_file* file; /* file which contains this module */
46 int refs; /* reference count */
47 int id; /* unique id number */
48 char *name; /* module name */
49 modeventhand_t handler; /* event handler */
50 void *arg; /* argument for handler */
53 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
55 static modulelist_t modules;
56 static int nextid = 1;
58 static void module_shutdown(int, void*);
61 module_init(void* arg)
64 at_shutdown(module_shutdown, 0, SHUTDOWN_POST_SYNC);
67 SYSINIT(module, SI_SUB_KMEM, SI_ORDER_ANY, module_init, 0);
70 module_shutdown(int arg1, void* arg2)
74 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
75 MOD_EVENT(mod, MOD_SHUTDOWN);
79 module_register_init(void *arg)
81 moduledata_t* data = (moduledata_t*) arg;
84 if (error = module_register(data->name, data->evhand, data->priv))
85 printf("module_register_init: module_register(%s, %lx, %p) returned %d",
86 data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
90 module_register(const char* name, modeventhand_t handler, void* arg)
96 namelen = strlen(name) + 1;
97 newmod = (module_t) malloc(sizeof(struct module) + namelen,
103 newmod->id = nextid++;
104 newmod->name = (char *) (newmod + 1);
105 strcpy(newmod->name, name);
106 newmod->handler = handler;
108 TAILQ_INSERT_TAIL(&modules, newmod, link);
110 if (linker_current_file) {
111 TAILQ_INSERT_TAIL(&linker_current_file->modules, newmod, flink);
112 newmod->file = linker_current_file;
116 if (error = MOD_EVENT(newmod, MOD_LOAD)) {
117 module_release(newmod);
125 module_reference(module_t mod)
127 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
133 module_release(module_t mod)
136 panic("module_release: bad reference count");
138 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
141 if (mod->refs == 0) {
142 TAILQ_REMOVE(&modules, mod, link);
144 TAILQ_REMOVE(&mod->file->modules, mod, flink);
151 module_lookupbyname(const char* name)
155 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
156 if (!strcmp(mod->name, name))
164 module_lookupbyid(int modid)
168 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
169 if (mod->id == modid)
177 module_unload(module_t mod)
179 return MOD_EVENT(mod, MOD_UNLOAD);
183 module_getid(module_t mod)
189 module_getfnext(module_t mod)
191 return TAILQ_NEXT(mod, flink);
198 modnext(struct proc* p, struct modnext_args* uap)
203 if (SCARG(uap, modid) == 0) {
204 mod = TAILQ_FIRST(&modules);
206 p->p_retval[0] = mod->id;
212 mod = module_lookupbyid(SCARG(uap, modid));
216 if (TAILQ_NEXT(mod, link))
217 p->p_retval[0] = TAILQ_NEXT(mod, link)->id;
224 modfnext(struct proc* p, struct modfnext_args* uap)
230 mod = module_lookupbyid(SCARG(uap, modid));
234 if (TAILQ_NEXT(mod, flink))
235 p->p_retval[0] = TAILQ_NEXT(mod, flink)->id;
242 modstat(struct proc* p, struct modstat_args* uap)
248 struct module_stat* stat;
250 mod = module_lookupbyid(SCARG(uap, modid));
254 stat = SCARG(uap, stat);
257 * Check the version of the user's structure.
259 if (error = copyin(&stat->version, &version, sizeof(version)))
261 if (version != sizeof(struct module_stat)) {
266 namelen = strlen(mod->name) + 1;
267 if (namelen > MAXMODNAME)
268 namelen = MAXMODNAME;
269 if (error = copyout(mod->name, &stat->name[0], namelen))
272 if (error = copyout(&mod->refs, &stat->refs, sizeof(int)))
274 if (error = copyout(&mod->id, &stat->id, sizeof(int)))
284 modfind(struct proc* p, struct modfind_args* uap)
287 char name[MAXMODNAME];
290 if (error = copyinstr(SCARG(uap, name), name, sizeof name, 0))
293 mod = module_lookupbyname(name);
297 p->p_retval[0] = mod->id;