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