xref: /dragonfly/sys/kern/kern_module.c (revision 0085a56d)
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  */
28 
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/sysmsg.h>
35 #include <sys/sysent.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/proc.h>
39 
40 MALLOC_DEFINE(M_MODULE, "module", "module data structures");
41 
42 typedef TAILQ_HEAD(, module) modulelist_t;
43 struct module {
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 */
53 };
54 
55 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
56 
57 static modulelist_t modules = TAILQ_HEAD_INITIALIZER(modules);
58 static struct lwkt_token mod_token = LWKT_TOKEN_INITIALIZER(mod_token);
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_BOOT2_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", 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 	module_unload(mod);	/* ignore error */
113 	module_release(mod);
114 	kprintf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
115 	       data->name, (u_long)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 	kprintf("module_register: module %s already exists!\n", data->name);
128 	return EEXIST;
129     }
130     namelen = strlen(data->name) + 1;
131     newmod = (module_t) kmalloc(sizeof(struct module) + namelen,
132 			       M_MODULE, M_WAITOK);
133 
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);
142 
143     if (container == NULL)
144 	container = linker_current_file;
145     if (container)
146 	TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
147     newmod->file = container;
148 
149     return 0;
150 }
151 
152 void
153 module_reference(module_t mod)
154 {
155     MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
156 
157     mod->refs++;
158 }
159 
160 /*
161  * module_release()
162  *
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.
165  */
166 int
167 module_release(module_t mod)
168 {
169     int rc;
170 
171     if (mod->refs <= 0)
172 	panic("module_release: bad reference count");
173 
174     MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
175 
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);
181 	}
182 	kfree(mod, M_MODULE);
183     }
184     return(rc);
185 }
186 
187 module_t
188 module_lookupbyname(const char* name)
189 {
190     module_t mod;
191 
192     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
193 	if (!strcmp(mod->name, name))
194 	    return mod;
195     }
196 
197     return NULL;
198 }
199 
200 module_t
201 module_lookupbyid(int modid)
202 {
203     module_t mod;
204 
205     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
206 	if (mod->id == modid)
207 	    return mod;
208     }
209 
210     return NULL;
211 }
212 
213 int
214 module_unload(module_t mod)
215 {
216     int error;
217 
218     error = MOD_EVENT(mod, MOD_UNLOAD);
219     /*sync_devs();*/
220     return (error);
221 }
222 
223 int
224 module_getid(module_t mod)
225 {
226     return mod->id;
227 }
228 
229 module_t
230 module_getfnext(module_t mod)
231 {
232     return TAILQ_NEXT(mod, flink);
233 }
234 
235 void
236 module_setspecific(module_t mod, modspecific_t *datap)
237 {
238     mod->data = *datap;
239 }
240 
241 /*
242  * Syscalls.
243  *
244  * MPALMOSTSAFE
245  */
246 int
247 sys_modnext(struct sysmsg *sysmsg, const struct modnext_args *uap)
248 {
249     module_t mod;
250     int error;
251 
252     error = 0;
253     lwkt_gettoken(&mod_token);
254     sysmsg->sysmsg_result = -1;
255     if (uap->modid == 0) {
256 	mod = TAILQ_FIRST(&modules);
257 	if (mod)
258 	    sysmsg->sysmsg_result = mod->id;
259 	else
260 	    error = ENOENT;
261 	goto done;
262     }
263 
264     mod = module_lookupbyid(uap->modid);
265     if (!mod) {
266 	error = ENOENT;
267 	goto done;
268     }
269 
270     if (TAILQ_NEXT(mod, link))
271 	sysmsg->sysmsg_result = TAILQ_NEXT(mod, link)->id;
272     else
273 	sysmsg->sysmsg_result = 0;
274 done:
275     lwkt_reltoken(&mod_token);
276 
277     return error;
278 }
279 
280 /*
281  * MPALMOSTSAFE
282  */
283 int
284 sys_modfnext(struct sysmsg *sysmsg, const struct modfnext_args *uap)
285 {
286     module_t mod;
287     int error;
288 
289     lwkt_gettoken(&mod_token);
290     sysmsg->sysmsg_result = -1;
291 
292     mod = module_lookupbyid(uap->modid);
293     if (!mod) {
294 	error = ENOENT;
295 	goto done;
296     }
297 
298     if (TAILQ_NEXT(mod, flink))
299 	sysmsg->sysmsg_result = TAILQ_NEXT(mod, flink)->id;
300     else
301 	sysmsg->sysmsg_result = 0;
302     error = 0;
303 done:
304     lwkt_reltoken(&mod_token);
305 
306     return error;
307 }
308 
309 struct module_stat_v1 {
310     int		version;	/* set to sizeof(struct module_stat) */
311     char	name[MAXMODNAME];
312     int		refs;
313     int		id;
314 };
315 
316 /*
317  * MPALMOSTSAFE
318  */
319 int
320 sys_modstat(struct sysmsg *sysmsg, const struct modstat_args *uap)
321 {
322     module_t mod;
323     int error;
324     int namelen;
325     int version;
326     struct module_stat* stat;
327 
328     lwkt_gettoken(&mod_token);
329     mod = module_lookupbyid(uap->modid);
330     if (!mod) {
331 	error = ENOENT;
332 	goto out;
333     }
334 
335     stat = uap->stat;
336 
337     /*
338      * Check the version of the user's structure.
339      */
340     if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
341 	goto out;
342     if (version != sizeof(struct module_stat_v1)
343 	&& version != sizeof(struct module_stat)) {
344 	error = EINVAL;
345 	goto out;
346     }
347 
348     namelen = strlen(mod->name) + 1;
349     if (namelen > MAXMODNAME)
350 	namelen = MAXMODNAME;
351     if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
352 	goto out;
353 
354     if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
355 	goto out;
356     if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
357 	goto out;
358 
359     /*
360      * >v1 stat includes module data.
361      */
362     if (version == sizeof(struct module_stat)) {
363 	if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
364 	    goto out;
365     }
366 
367     sysmsg->sysmsg_result = 0;
368 
369 out:
370     lwkt_reltoken(&mod_token);
371 
372     return error;
373 }
374 
375 /*
376  * MPALMOSTSAFE
377  */
378 int
379 sys_modfind(struct sysmsg *sysmsg, const struct modfind_args *uap)
380 {
381     int error;
382     char name[MAXMODNAME];
383     module_t mod;
384 
385     lwkt_gettoken(&mod_token);
386     if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
387 	goto out;
388 
389     mod = module_lookupbyname(name);
390     if (!mod)
391 	error = ENOENT;
392     else
393 	sysmsg->sysmsg_result = mod->id;
394 
395 out:
396     lwkt_reltoken(&mod_token);
397 
398     return error;
399 }
400