xref: /dragonfly/sys/kern/kern_module.c (revision cae2835b)
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/sysproto.h>
35 #include <sys/sysent.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/proc.h>
39 
40 #include <sys/mplock2.h>
41 
42 MALLOC_DEFINE(M_MODULE, "module", "module data structures");
43 
44 typedef TAILQ_HEAD(, module) modulelist_t;
45 struct module {
46     TAILQ_ENTRY(module)	link;		/* chain together all modules */
47     TAILQ_ENTRY(module)	flink;		/* all modules in a file */
48     struct linker_file*	file;		/* file which contains this module */
49     int			refs;		/* reference count */
50     int			id;		/* unique id number */
51     char		*name;		/* module name */
52     modeventhand_t	handler;	/* event handler */
53     void		*arg;		/* argument for handler */
54     modspecific_t	data;		/* module specific data */
55 };
56 
57 #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
58 
59 static modulelist_t modules = TAILQ_HEAD_INITIALIZER(modules);
60 static int nextid = 1;
61 
62 static void module_shutdown(void*, int);
63 
64 static int
65 modevent_nop(module_t mod, int what, void* arg)
66 {
67 	return 0;
68 }
69 
70 
71 static void
72 module_init(void* arg)
73 {
74     TAILQ_INIT(&modules);
75     EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
76 			  SHUTDOWN_PRI_DEFAULT);
77 }
78 
79 SYSINIT(module, SI_BOOT2_KLD, SI_ORDER_FIRST, module_init, 0);
80 
81 static void
82 module_shutdown(void* arg1, int arg2)
83 {
84     module_t mod;
85 
86     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
87 	MOD_EVENT(mod, MOD_SHUTDOWN);
88 }
89 
90 void
91 module_register_init(const void *arg)
92 {
93     const moduledata_t* data = (const moduledata_t*) arg;
94     int error;
95     module_t mod;
96 
97     mod = module_lookupbyname(data->name);
98     if (mod == NULL) {
99 #if 0
100 	panic("module_register_init: module named %s not found", data->name);
101 #else
102 	/* temporary kludge until kernel `file' attachment registers modules */
103 	error = module_register(data, linker_kernel_file);
104 	if (error)
105 	    panic("module_register_init: register of module failed! %d", error);
106 	mod = module_lookupbyname(data->name);
107 	if (mod == NULL)
108 	    panic("module_register_init: module STILL not found!");
109 #endif
110     }
111     error = MOD_EVENT(mod, MOD_LOAD);
112     if (error) {
113 	module_unload(mod);	/* ignore error */
114 	module_release(mod);
115 	kprintf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
116 	       data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
117     }
118 }
119 
120 int
121 module_register(const moduledata_t *data, linker_file_t container)
122 {
123     size_t namelen;
124     module_t newmod;
125 
126     newmod = module_lookupbyname(data->name);
127     if (newmod != NULL) {
128 	kprintf("module_register: module %s already exists!\n", data->name);
129 	return EEXIST;
130     }
131     namelen = strlen(data->name) + 1;
132     newmod = (module_t) kmalloc(sizeof(struct module) + namelen,
133 			       M_MODULE, M_WAITOK);
134 
135     newmod->refs = 1;
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);
143 
144     if (container == NULL)
145 	container = linker_current_file;
146     if (container)
147 	TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
148     newmod->file = container;
149 
150     return 0;
151 }
152 
153 void
154 module_reference(module_t mod)
155 {
156     MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
157 
158     mod->refs++;
159 }
160 
161 /*
162  * module_release()
163  *
164  *	Release ref on the module and return the new reference count.  If 0
165  *	is returned, the module has been removed from its list and freed.
166  */
167 int
168 module_release(module_t mod)
169 {
170     int rc;
171 
172     if (mod->refs <= 0)
173 	panic("module_release: bad reference count");
174 
175     MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
176 
177     rc = --mod->refs;
178     if (rc == 0) {
179 	TAILQ_REMOVE(&modules, mod, link);
180 	if (mod->file) {
181 	    TAILQ_REMOVE(&mod->file->modules, mod, flink);
182 	}
183 	kfree(mod, M_MODULE);
184     }
185     return(rc);
186 }
187 
188 module_t
189 module_lookupbyname(const char* name)
190 {
191     module_t mod;
192 
193     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
194 	if (!strcmp(mod->name, name))
195 	    return mod;
196     }
197 
198     return NULL;
199 }
200 
201 module_t
202 module_lookupbyid(int modid)
203 {
204     module_t mod;
205 
206     for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
207 	if (mod->id == modid)
208 	    return mod;
209     }
210 
211     return NULL;
212 }
213 
214 int
215 module_unload(module_t mod)
216 {
217     int error;
218 
219     error = MOD_EVENT(mod, MOD_UNLOAD);
220     /*sync_devs();*/
221     return (error);
222 }
223 
224 int
225 module_getid(module_t mod)
226 {
227     return mod->id;
228 }
229 
230 module_t
231 module_getfnext(module_t mod)
232 {
233     return TAILQ_NEXT(mod, flink);
234 }
235 
236 void
237 module_setspecific(module_t mod, modspecific_t *datap)
238 {
239     mod->data = *datap;
240 }
241 
242 /*
243  * Syscalls.
244  *
245  * MPALMOSTSAFE
246  */
247 int
248 sys_modnext(struct modnext_args *uap)
249 {
250     module_t mod;
251     int error;
252 
253     error = 0;
254     get_mplock();
255     uap->sysmsg_result = -1;
256     if (uap->modid == 0) {
257 	mod = TAILQ_FIRST(&modules);
258 	if (mod)
259 	    uap->sysmsg_result = mod->id;
260 	else
261 	    error = ENOENT;
262 	goto done;
263     }
264 
265     mod = module_lookupbyid(uap->modid);
266     if (!mod) {
267 	error = ENOENT;
268 	goto done;
269     }
270 
271     if (TAILQ_NEXT(mod, link))
272 	uap->sysmsg_result = TAILQ_NEXT(mod, link)->id;
273     else
274 	uap->sysmsg_result = 0;
275 done:
276     rel_mplock();
277     return error;
278 }
279 
280 /*
281  * MPALMOSTSAFE
282  */
283 int
284 sys_modfnext(struct modfnext_args *uap)
285 {
286     module_t mod;
287     int error;
288 
289     get_mplock();
290     uap->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 	uap->sysmsg_result = TAILQ_NEXT(mod, flink)->id;
300     else
301 	uap->sysmsg_result = 0;
302     error = 0;
303 done:
304     rel_mplock();
305     return error;
306 }
307 
308 struct module_stat_v1 {
309     int		version;	/* set to sizeof(struct module_stat) */
310     char	name[MAXMODNAME];
311     int		refs;
312     int		id;
313 };
314 
315 /*
316  * MPALMOSTSAFE
317  */
318 int
319 sys_modstat(struct modstat_args *uap)
320 {
321     module_t mod;
322     int error;
323     int namelen;
324     int version;
325     struct module_stat* stat;
326 
327     get_mplock();
328     mod = module_lookupbyid(uap->modid);
329     if (!mod) {
330 	error = ENOENT;
331 	goto out;
332     }
333 
334     stat = uap->stat;
335 
336     /*
337      * Check the version of the user's structure.
338      */
339     if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
340 	goto out;
341     if (version != sizeof(struct module_stat_v1)
342 	&& version != sizeof(struct module_stat)) {
343 	error = EINVAL;
344 	goto out;
345     }
346 
347     namelen = strlen(mod->name) + 1;
348     if (namelen > MAXMODNAME)
349 	namelen = MAXMODNAME;
350     if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
351 	goto out;
352 
353     if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
354 	goto out;
355     if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
356 	goto out;
357 
358     /*
359      * >v1 stat includes module data.
360      */
361     if (version == sizeof(struct module_stat)) {
362 	if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
363 	    goto out;
364     }
365 
366     uap->sysmsg_result = 0;
367 
368 out:
369     rel_mplock();
370     return error;
371 }
372 
373 /*
374  * MPALMOSTSAFE
375  */
376 int
377 sys_modfind(struct modfind_args *uap)
378 {
379     int error;
380     char name[MAXMODNAME];
381     module_t mod;
382 
383     get_mplock();
384     if ((error = copyinstr(uap->name, name, sizeof name, 0)) != 0)
385 	goto out;
386 
387     mod = module_lookupbyname(name);
388     if (!mod)
389 	error = ENOENT;
390     else
391 	uap->sysmsg_result = mod->id;
392 
393 out:
394     rel_mplock();
395     return error;
396 }
397