Nuke following outdated drivers
[dragonfly.git] / sys / kern / kern_module.c
blobdd5c73990f5b092c23682a125e8a74e06367f093
1 /*-
2 * Copyright (c) 1997 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
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.15 2008/01/06 16:55:51 swildner 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>
39 #include <sys/proc.h>
41 MALLOC_DEFINE(M_MODULE, "module", "module data structures");
43 typedef TAILQ_HEAD(, module) modulelist_t;
44 struct module {
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);
63 static int
64 modevent_nop(module_t mod, int what, void* arg)
66 return 0;
70 static void
71 module_init(void* arg)
73 TAILQ_INIT(&modules);
74 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
75 SHUTDOWN_PRI_DEFAULT);
78 SYSINIT(module, SI_BOOT2_KLD, SI_ORDER_FIRST, module_init, 0);
80 static void
81 module_shutdown(void* arg1, int arg2)
83 module_t mod;
85 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
86 MOD_EVENT(mod, MOD_SHUTDOWN);
89 void
90 module_register_init(const void *arg)
92 const moduledata_t* data = (const moduledata_t*) arg;
93 int error;
94 module_t mod;
96 mod = module_lookupbyname(data->name);
97 if (mod == NULL) {
98 #if 0
99 panic("module_register_init: module named %s not found", data->name);
100 #else
101 /* temporary kludge until kernel `file' attachment registers modules */
102 error = module_register(data, linker_kernel_file);
103 if (error)
104 panic("module_register_init: register of module failed! %d", error);
105 mod = module_lookupbyname(data->name);
106 if (mod == NULL)
107 panic("module_register_init: module STILL not found!");
108 #endif
110 error = MOD_EVENT(mod, MOD_LOAD);
111 if (error) {
112 MOD_EVENT(mod, MOD_UNLOAD);
113 module_release(mod);
114 kprintf("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)
122 size_t namelen;
123 module_t newmod;
125 newmod = module_lookupbyname(data->name);
126 if (newmod != NULL) {
127 kprintf("module_register: module %s already exists!\n", data->name);
128 return EEXIST;
130 namelen = strlen(data->name) + 1;
131 newmod = (module_t) kmalloc(sizeof(struct module) + namelen,
132 M_MODULE, M_WAITOK);
134 newmod->refs = 1;
135 newmod->id = nextid++;
136 newmod->name = (char *) (newmod + 1);
137 strcpy(newmod->name, data->name);
138 newmod->handler = data->evhand ? data->evhand : modevent_nop;
139 newmod->arg = data->priv;
140 bzero(&newmod->data, sizeof(newmod->data));
141 TAILQ_INSERT_TAIL(&modules, newmod, link);
143 if (container == NULL)
144 container = linker_current_file;
145 if (container)
146 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
147 newmod->file = container;
149 return 0;
152 void
153 module_reference(module_t mod)
155 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
157 mod->refs++;
161 * module_release()
163 * Release ref on the module and return the new reference count. If 0
164 * is returned, the module has been removed from its list and freed.
167 module_release(module_t mod)
169 int rc;
171 if (mod->refs <= 0)
172 panic("module_release: bad reference count");
174 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
176 rc = --mod->refs;
177 if (rc == 0) {
178 TAILQ_REMOVE(&modules, mod, link);
179 if (mod->file) {
180 TAILQ_REMOVE(&mod->file->modules, mod, flink);
182 kfree(mod, M_MODULE);
184 return(rc);
187 module_t
188 module_lookupbyname(const char* name)
190 module_t mod;
192 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
193 if (!strcmp(mod->name, name))
194 return mod;
197 return 0;
200 module_t
201 module_lookupbyid(int modid)
203 module_t mod;
205 for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
206 if (mod->id == modid)
207 return mod;
210 return 0;
214 module_unload(module_t mod)
216 return MOD_EVENT(mod, MOD_UNLOAD);
220 module_getid(module_t mod)
222 return mod->id;
225 module_t
226 module_getfnext(module_t mod)
228 return TAILQ_NEXT(mod, flink);
231 void
232 module_setspecific(module_t mod, modspecific_t *datap)
234 mod->data = *datap;
238 * Syscalls.
241 sys_modnext(struct modnext_args *uap)
243 module_t mod;
245 uap->sysmsg_result = -1;
246 if (uap->modid == 0) {
247 mod = TAILQ_FIRST(&modules);
248 if (mod) {
249 uap->sysmsg_result = mod->id;
250 return 0;
251 } else
252 return ENOENT;
255 mod = module_lookupbyid(uap->modid);
256 if (!mod)
257 return ENOENT;
259 if (TAILQ_NEXT(mod, link))
260 uap->sysmsg_result = TAILQ_NEXT(mod, link)->id;
261 else
262 uap->sysmsg_result = 0;
263 return 0;
267 sys_modfnext(struct modfnext_args *uap)
269 module_t mod;
271 uap->sysmsg_result = -1;
273 mod = module_lookupbyid(uap->modid);
274 if (!mod)
275 return ENOENT;
277 if (TAILQ_NEXT(mod, flink))
278 uap->sysmsg_result = TAILQ_NEXT(mod, flink)->id;
279 else
280 uap->sysmsg_result = 0;
281 return 0;
284 struct module_stat_v1 {
285 int version; /* set to sizeof(struct module_stat) */
286 char name[MAXMODNAME];
287 int refs;
288 int id;
292 sys_modstat(struct modstat_args *uap)
294 module_t mod;
295 int error = 0;
296 int namelen;
297 int version;
298 struct module_stat* stat;
300 mod = module_lookupbyid(uap->modid);
301 if (!mod)
302 return ENOENT;
304 stat = uap->stat;
307 * Check the version of the user's structure.
309 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
310 goto out;
311 if (version != sizeof(struct module_stat_v1)
312 && version != sizeof(struct module_stat)) {
313 error = EINVAL;
314 goto out;
317 namelen = strlen(mod->name) + 1;
318 if (namelen > MAXMODNAME)
319 namelen = MAXMODNAME;
320 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
321 goto out;
323 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
324 goto out;
325 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
326 goto out;
329 * >v1 stat includes module data.
331 if (version == sizeof(struct module_stat)) {
332 if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
333 goto out;
336 uap->sysmsg_result = 0;
338 out:
339 return error;
343 sys_modfind(struct modfind_args *uap)
345 int error = 0;
346 char name[MAXMODNAME];
347 module_t mod;
349 if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
350 goto out;
352 mod = module_lookupbyname(name);
353 if (!mod)
354 error = ENOENT;
355 else
356 uap->sysmsg_result = mod->id;
358 out:
359 return error;