d487566760dde405425b1d9f7cb8f06c0d014a92
[dragonfly.git] / sys / kern / kern_module.c
1 /*-
2  * Copyright (c) 1997 Doug Rabson
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
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.3 2003/06/23 17:55:41 dillon Exp $
28  */
29
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>
40
41 #define M_MODULE        M_TEMP          /* XXX */
42
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 */
54 };
55
56 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
57
58 static modulelist_t modules;
59 static int nextid = 1;
60
61 static void module_shutdown(void*, int);
62
63 static int
64 modevent_nop(module_t mod, int what, void* arg)
65 {
66         return 0;
67 }
68
69
70 static void
71 module_init(void* arg)
72 {
73     TAILQ_INIT(&modules);
74     EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
75                           SHUTDOWN_PRI_DEFAULT);
76 }
77
78 SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
79
80 static void
81 module_shutdown(void* arg1, int arg2)
82 {
83     module_t mod;
84
85     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
86         MOD_EVENT(mod, MOD_SHUTDOWN);
87 }
88
89 void
90 module_register_init(const void *arg)
91 {
92     const moduledata_t* data = (const moduledata_t*) arg;
93     int error;
94     module_t mod;
95
96     mod = module_lookupbyname(data->name);
97     if (mod == NULL) {
98 #if 0
99         panic("module_register_init: module named %s not found\n", 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
109     }
110     error = MOD_EVENT(mod, MOD_LOAD);
111     if (error) {
112         MOD_EVENT(mod, MOD_UNLOAD);
113         module_release(mod);
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);
116     }
117 }
118
119 int
120 module_register(const moduledata_t *data, linker_file_t container)
121 {
122     size_t namelen;
123     module_t newmod;
124
125     newmod = module_lookupbyname(data->name);
126     if (newmod != NULL) {
127         printf("module_register: module %s already exists!\n", data->name);
128         return EEXIST;
129     }
130     namelen = strlen(data->name) + 1;
131     newmod = (module_t) malloc(sizeof(struct module) + namelen,
132                                M_MODULE, M_WAITOK);
133     if (newmod == 0)
134         return ENOMEM;
135
136     newmod->refs = 1;
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);
144
145     if (container == NULL)
146         container = linker_current_file;
147     if (container)
148         TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
149     newmod->file = container;
150
151     return 0;
152 }
153
154 void
155 module_reference(module_t mod)
156 {
157     MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
158
159     mod->refs++;
160 }
161
162 void
163 module_release(module_t mod)
164 {
165     if (mod->refs <= 0)
166         panic("module_release: bad reference count");
167
168     MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
169
170     mod->refs--;
171     if (mod->refs == 0) {
172         TAILQ_REMOVE(&modules, mod, link);
173         if (mod->file) {
174             TAILQ_REMOVE(&mod->file->modules, mod, flink);
175         }
176         free(mod, M_MODULE);
177     }
178 }
179
180 module_t
181 module_lookupbyname(const char* name)
182 {
183     module_t mod;
184
185     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
186         if (!strcmp(mod->name, name))
187             return mod;
188     }
189
190     return 0;
191 }
192
193 module_t
194 module_lookupbyid(int modid)
195 {
196     module_t mod;
197
198     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
199         if (mod->id == modid)
200             return mod;
201     }
202
203     return 0;
204 }
205
206 int
207 module_unload(module_t mod)
208 {
209     return MOD_EVENT(mod, MOD_UNLOAD);
210 }
211
212 int
213 module_getid(module_t mod)
214 {
215     return mod->id;
216 }
217
218 module_t
219 module_getfnext(module_t mod)
220 {
221     return TAILQ_NEXT(mod, flink);
222 }
223
224 void
225 module_setspecific(module_t mod, modspecific_t *datap)
226 {
227     mod->data = *datap;
228 }
229
230 /*
231  * Syscalls.
232  */
233 int
234 modnext(struct modnext_args *uap)
235 {
236     struct proc *p = curproc;
237     module_t mod;
238
239     p->p_retval[0] = -1;
240     if (SCARG(uap, modid) == 0) {
241         mod = TAILQ_FIRST(&modules);
242         if (mod) {
243             p->p_retval[0] = mod->id;
244             return 0;
245         } else
246             return ENOENT;
247     }
248
249     mod = module_lookupbyid(SCARG(uap, modid));
250     if (!mod)
251         return ENOENT;
252
253     if (TAILQ_NEXT(mod, link))
254         p->p_retval[0] = TAILQ_NEXT(mod, link)->id;
255     else
256         p->p_retval[0] = 0;
257     return 0;
258 }
259
260 int
261 modfnext(struct modfnext_args *uap)
262 {
263     struct proc *p = curproc;
264     module_t mod;
265
266     p->p_retval[0] = -1;
267
268     mod = module_lookupbyid(SCARG(uap, modid));
269     if (!mod)
270         return ENOENT;
271
272     if (TAILQ_NEXT(mod, flink))
273         p->p_retval[0] = TAILQ_NEXT(mod, flink)->id;
274     else
275         p->p_retval[0] = 0;
276     return 0;
277 }
278
279 struct module_stat_v1 {
280     int         version;        /* set to sizeof(struct module_stat) */
281     char        name[MAXMODNAME];
282     int         refs;
283     int         id;
284 };
285
286 int
287 modstat(struct modstat_args *uap)
288 {
289     struct proc *p = curproc;
290     module_t mod;
291     int error = 0;
292     int namelen;
293     int version;
294     struct module_stat* stat;
295
296     mod = module_lookupbyid(SCARG(uap, modid));
297     if (!mod)
298         return ENOENT;
299
300     stat = SCARG(uap, stat);
301
302     /*
303      * Check the version of the user's structure.
304      */
305     if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
306         goto out;
307     if (version != sizeof(struct module_stat_v1)
308         && version != sizeof(struct module_stat)) {
309         error = EINVAL;
310         goto out;
311     }
312
313     namelen = strlen(mod->name) + 1;
314     if (namelen > MAXMODNAME)
315         namelen = MAXMODNAME;
316     if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
317         goto out;
318
319     if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
320         goto out;
321     if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
322         goto out;
323
324     /*
325      * >v1 stat includes module data.
326      */
327     if (version == sizeof(struct module_stat)) {
328         if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
329             goto out;
330     }
331
332     p->p_retval[0] = 0;
333
334 out:
335     return error;
336 }
337
338 int
339 modfind(struct modfind_args *uap)
340 {
341     struct proc *p = curproc;
342     int error = 0;
343     char name[MAXMODNAME];
344     module_t mod;
345
346     if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
347         goto out;
348
349     mod = module_lookupbyname(name);
350     if (!mod)
351         error = ENOENT;
352     else
353         p->p_retval[0] = mod->id;
354
355 out:
356     return error;
357 }