kern_module.c revision 1.1 1 /* $NetBSD: kern_module.c,v 1.1 2008/01/16 12:34:52 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Kernel modules.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.1 2008/01/16 12:34:52 ad Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
46 #include <sys/proc.h>
47 #include <sys/kauth.h>
48 #include <sys/kobj.h>
49 #include <sys/kmem.h>
50 #include <sys/module.h>
51 #include <sys/syscall.h>
52 #include <sys/syscallargs.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <machine/stdarg.h>
57
58 struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
59 struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
60 u_int module_count;
61 kmutex_t module_lock;
62
63 /* Ensure that the kernel's link set isn't empty. */
64 static modinfo_t module_dummy;
65 __link_set_add_rodata(modules, module_dummy);
66
67 static module_t *module_lookup(const char *);
68 static int module_do_load(const char *, bool, bool, module_t **);
69 static int module_do_unload(const char *);
70 static void module_error(const char *, ...);
71 static int module_do_builtin(const char *, module_t **);
72
73 /*
74 * module_error:
75 *
76 * Utility function: log an error.
77 */
78 static void
79 module_error(const char *fmt, ...)
80 {
81 va_list ap;
82
83 va_start(ap, fmt);
84 printf("WARNING: module error: ");
85 vprintf(fmt, ap);
86 printf("\n");
87 va_end(ap);
88 }
89
90 /*
91 * module_init:
92 *
93 * Initialize the module subsystem.
94 */
95 void
96 module_init(void)
97 {
98 extern struct vm_map *lkm_map;
99
100 if (lkm_map == NULL)
101 lkm_map = kernel_map;
102 mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
103 }
104
105 /*
106 * module_init_class:
107 *
108 * Initialize all built-in and pre-loaded modules of the
109 * specified class.
110 */
111 void
112 module_init_class(modclass_t class)
113 {
114 __link_set_decl(modules, modinfo_t);
115 modinfo_t *const *mip, *mi;
116 module_t *mod;
117
118 mutex_enter(&module_lock);
119 /*
120 * Builtins first. These can't depend on pre-loaded modules.
121 */
122 __link_set_foreach(mip, modules) {
123 mi = *mip;
124 if (mi == &module_dummy) {
125 continue;
126 }
127 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
128 continue;
129 }
130 (void)module_do_builtin(mi->mi_name, NULL);
131 }
132 /*
133 * Now preloaded modules. These will be pulled off the
134 * list as we call module_do_load();
135 */
136 while ((mod = TAILQ_FIRST(&module_bootlist)) != NULL) {
137 module_do_load(mod->mod_info->mi_name, false, false, NULL);
138 }
139 mutex_exit(&module_lock);
140 }
141
142 /*
143 * module_jettison:
144 *
145 * Return memory used by pre-loaded modules to the freelist.
146 */
147 void
148 module_jettison(void)
149 {
150
151 /* XXX nothing yet */
152 }
153
154 /*
155 * module_load:
156 *
157 * Load a single module from the file system. If force is set,
158 * bypass the version check.
159 */
160 int
161 module_load(const char *filename, bool force)
162 {
163 int error;
164
165 mutex_enter(&module_lock);
166 error = module_do_load(filename, false, force, NULL);
167 mutex_exit(&module_lock);
168
169 return error;
170 }
171
172 /*
173 * module_unload:
174 *
175 * Find and unload a module by name.
176 */
177 int
178 module_unload(const char *name)
179 {
180 int error;
181
182 mutex_enter(&module_lock);
183 error = module_do_unload(name);
184 mutex_exit(&module_lock);
185
186 return error;
187 }
188
189 /*
190 * module_lookup:
191 *
192 * Look up a module by name.
193 */
194 module_t *
195 module_lookup(const char *name)
196 {
197 module_t *mod;
198
199 KASSERT(mutex_owned(&module_lock));
200
201 TAILQ_FOREACH(mod, &module_list, mod_chain) {
202 if (strcmp(mod->mod_info->mi_name, name) == 0) {
203 break;
204 }
205 }
206
207 return mod;
208 }
209
210 /*
211 * module_hold:
212 *
213 * Add a single reference to a module. It's the caller's
214 * responsibility to ensure that the reference is dropped
215 * later.
216 */
217 int
218 module_hold(const char *name)
219 {
220 module_t *mod;
221
222 mutex_enter(&module_lock);
223 mod = module_lookup(name);
224 if (mod == NULL) {
225 mutex_exit(&module_lock);
226 return ENOENT;
227 }
228 mod->mod_refcnt++;
229 mutex_exit(&module_lock);
230
231 return 0;
232 }
233
234 /*
235 * module_rele:
236 *
237 * Release a reference acquired with module_hold().
238 */
239 void
240 module_rele(const char *name)
241 {
242 module_t *mod;
243
244 mutex_enter(&module_lock);
245 mod = module_lookup(name);
246 if (mod == NULL) {
247 mutex_exit(&module_lock);
248 panic("module_rele: gone");
249 }
250 mod->mod_refcnt--;
251 mutex_exit(&module_lock);
252 }
253
254 /*
255 * module_do_builtin:
256 *
257 * Initialize a single module from the list of modules that are
258 * built into the kernel (linked into the kernel image).
259 */
260 static int
261 module_do_builtin(const char *name, module_t **modp)
262 {
263 __link_set_decl(modules, modinfo_t);
264 modinfo_t *const *mip;
265 const char *p, *s;
266 char buf[MAXMODNAME];
267 modinfo_t *mi;
268 module_t *mod, *mod2;
269 size_t len;
270 int error, i;
271
272 KASSERT(mutex_owned(&module_lock));
273
274 /*
275 * Check to see if already loaded.
276 */
277 if ((mod = module_lookup(name)) != NULL) {
278 if (modp != NULL) {
279 *modp = mod;
280 }
281 return 0;
282 }
283
284 /*
285 * Search the list to see if we have a module by this name.
286 */
287 error = ENOENT;
288 __link_set_foreach(mip, modules) {
289 mi = *mip;
290 if (mi == &module_dummy) {
291 continue;
292 }
293 if (strcmp(mi->mi_name, name) == 0) {
294 error = 0;
295 break;
296 }
297 }
298 if (error != 0) {
299 return error;
300 }
301
302 /*
303 * Initialize pre-requisites.
304 */
305 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
306 if (mod == NULL) {
307 return ENOMEM;
308 }
309 if (modp != NULL) {
310 *modp = mod;
311 }
312 if (mi->mi_required != NULL) {
313 for (s = mi->mi_required; *s != '\0'; s = p) {
314 if (*s == ',')
315 s++;
316 p = s;
317 while (*p != '\0' && *p != ',')
318 p++;
319 len = min(p - s + 1, sizeof(buf));
320 strlcpy(buf, s, len);
321 if (buf[0] == '\0')
322 break;
323 if (mod->mod_nrequired == MAXMODDEPS - 1) {
324 module_error("too many required modules");
325 kmem_free(mod, sizeof(*mod));
326 return EINVAL;
327 }
328 error = module_do_builtin(buf, &mod2);
329 if (error != 0) {
330 kmem_free(mod, sizeof(*mod));
331 return error;
332 }
333 mod->mod_required[mod->mod_nrequired++] = mod2;
334 }
335 }
336
337 /*
338 * Try to initialize the module.
339 */
340 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
341 if (error != 0) {
342 module_error("builtin module `%s' "
343 "failed to init", mi->mi_name);
344 kmem_free(mod, sizeof(*mod));
345 return error;
346 }
347 mod->mod_info = mi;
348 mod->mod_source = MODULE_SOURCE_KERNEL;
349 module_count++;
350 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
351
352 /*
353 * If that worked, count dependencies.
354 */
355 for (i = 0; i < mod->mod_nrequired; i++) {
356 mod->mod_required[i]->mod_refcnt++;
357 }
358
359 return 0;
360 }
361
362 /*
363 * module_do_load:
364 *
365 * Helper routine: load a module from the file system, or one
366 * pushed by the boot loader.
367 */
368 static int
369 module_do_load(const char *filename, bool isdep, bool force, module_t **modp)
370 {
371 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
372 static int depth;
373 const int maxdepth = 6;
374 modinfo_t *mi;
375 module_t *mod, *mod2;
376 char buf[MAXMODNAME];
377 const char *s, *p;
378 void *addr;
379 size_t size;
380 int error;
381 size_t len;
382 u_int i;
383
384 KASSERT(mutex_owned(&module_lock));
385
386 error = 0;
387
388 /*
389 * Avoid recursing too far.
390 */
391 if (++depth > maxdepth) {
392 module_error("too many required modules");
393 depth--;
394 return EMLINK;
395 }
396
397 /*
398 * Load the module and link. Before going to the file system,
399 * scan the list of modules loaded by the boot loader. Just
400 * before init is started the list of modules loaded at boot
401 * will be purged. Before init is started we can assume that
402 * `filename' is a module name and not a path name.
403 */
404 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
405 if (strcmp(mod->mod_info->mi_name, filename) == 0) {
406 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
407 break;
408 }
409 }
410 if (mod == NULL) {
411 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
412 if (mod == NULL) {
413 depth--;
414 return ENOMEM;
415 }
416 error = kobj_open_file(&mod->mod_kobj, filename);
417 if (error != 0) {
418 kmem_free(mod, sizeof(*mod));
419 depth--;
420 module_error("unable to open object file");
421 return error;
422 }
423 error = kobj_load(mod->mod_kobj);
424 mod->mod_source = MODULE_SOURCE_FILESYS;
425 }
426 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
427 if (error != 0) {
428 goto fail;
429 }
430
431 /*
432 * Find module info record and check compatibility.
433 */
434 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
435 &addr, &size);
436 if (error != 0) {
437 module_error("`link_set_modules' section not present");
438 goto fail;
439 }
440 if (size != sizeof(modinfo_t **)) {
441 module_error("`link_set_modules' section wrong size");
442 goto fail;
443 }
444 mod->mod_info = *(modinfo_t **)addr;
445 mi = mod->mod_info;
446
447 /*
448 * If loading a dependency, `filename' is a plain module name.
449 * The name must match.
450 */
451 if (isdep && strcmp(mi->mi_name, filename) != 0) {
452 error = ENOENT;
453 goto fail;
454 }
455
456 /*
457 * Check to see if the module is already loaded, and block
458 * circular dependencies.
459 */
460 if (module_lookup(mi->mi_name) != NULL) {
461 error = EEXIST;
462 goto fail;
463 }
464 TAILQ_FOREACH(mod2, &pending, mod_chain) {
465 if (mod == mod2) {
466 continue;
467 }
468 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
469 error = EDEADLK;
470 module_error("circular dependency detected");
471 goto fail;
472 }
473 }
474
475 /*
476 * Pass proper name to kobj. This will register the module
477 * with the ksyms framework.
478 */
479 error = kobj_set_name(mod->mod_kobj, mi->mi_name);
480 if (error != 0) {
481 module_error("unable to set name");
482 goto fail;
483 }
484
485 /*
486 * Now try to load any requisite modules.
487 */
488 if (mi->mi_required != NULL) {
489 for (s = mi->mi_required; *s != '\0'; s = p) {
490 if (*s == ',')
491 s++;
492 p = s;
493 while (*p != '\0' && *p != ',')
494 p++;
495 len = min(p - s + 1, MAXMODNAME);
496 strlcpy(buf, s, len);
497 if (buf[0] == '\0')
498 break;
499 if (mod->mod_nrequired == MAXMODDEPS - 1) {
500 error = EINVAL;
501 module_error("too many required modules");
502 goto fail;
503 }
504 error = module_do_load(buf, true, force,
505 &mod->mod_required[mod->mod_nrequired++]);
506 if (error != 0 && error != EEXIST)
507 goto fail;
508 }
509 }
510
511 /*
512 * We loaded all needed modules successfully: initialize.
513 */
514 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
515 if (error != 0) {
516 module_error("modctl function returned error %d", error);
517 goto fail;
518 }
519
520 /*
521 * Good, the module loaded successfully. Put it onto the
522 * list and add references to its requisite modules.
523 */
524 module_count++;
525 kobj_close(mod->mod_kobj);
526 TAILQ_REMOVE(&pending, mod, mod_chain);
527 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
528 for (i = 0; i < mod->mod_nrequired; i++) {
529 mod->mod_required[i]->mod_refcnt++;
530 }
531 if (modp != NULL) {
532 *modp = mod;
533 }
534 depth--;
535 return 0;
536 fail:
537 kobj_close(mod->mod_kobj);
538 TAILQ_REMOVE(&pending, mod, mod_chain);
539 kobj_unload(mod->mod_kobj);
540 kmem_free(mod, sizeof(*mod));
541 depth--;
542 return error;
543 }
544
545 /*
546 * module_do_unload:
547 *
548 * Helper routine: do the dirty work of unloading a module.
549 */
550 static int
551 module_do_unload(const char *name)
552 {
553 module_t *mod;
554 int error;
555 u_int i;
556
557 KASSERT(mutex_owned(&module_lock));
558
559 mod = module_lookup(name);
560 if (mod == NULL) {
561 return ENOENT;
562 }
563 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
564 return EBUSY;
565 }
566 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
567 if (error != 0) {
568 return error;
569 }
570 module_count--;
571 TAILQ_REMOVE(&module_list, mod, mod_chain);
572 for (i = 0; i < mod->mod_nrequired; i++) {
573 mod->mod_required[i]->mod_refcnt--;
574 }
575 if (mod->mod_kobj != NULL) {
576 kobj_unload(mod->mod_kobj);
577 }
578 kmem_free(mod, sizeof(*mod));
579
580 return 0;
581 }
582
583 /*
584 * module_prime:
585 *
586 * Push a module loaded by the bootloader onto our internal
587 * list.
588 */
589 int
590 module_prime(void *base, size_t size)
591 {
592 module_t *mod;
593 int error;
594
595 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
596 if (mod == NULL) {
597 return ENOMEM;
598 }
599 mod->mod_source = MODULE_SOURCE_BOOT;
600
601 error = kobj_open_mem(&mod->mod_kobj, base, size);
602 if (error == 0) {
603 kmem_free(mod, sizeof(*mod));
604 module_error("unable to open object pushed by boot loader");
605 return error;
606 }
607 error = kobj_load(mod->mod_kobj);
608 if (error != 0) {
609 kmem_free(mod, sizeof(*mod));
610 module_error("unable to push object pushed by boot loader");
611 return error;
612 }
613 mod->mod_source = MODULE_SOURCE_FILESYS;
614 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
615
616 return 0;
617 }
618