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