kern_module.c revision 1.21 1 /* $NetBSD: kern_module.c,v 1.21 2008/05/31 20:14:38 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
33 #include "opt_modular.h"
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.21 2008/05/31 20:14:38 ad Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/proc.h>
42 #include <sys/kauth.h>
43 #include <sys/kobj.h>
44 #include <sys/kmem.h>
45 #include <sys/module.h>
46 #include <sys/kauth.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #include <machine/stdarg.h>
51
52 #ifndef LKM /* XXX */
53 struct vm_map *lkm_map;
54 #endif
55
56 struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
57 struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
58 static module_t *module_active;
59 static char module_base[64];
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, int, prop_dictionary_t,
69 module_t **, modclass_t class, bool);
70 static int module_do_unload(const char *);
71 static void module_error(const char *, ...);
72 static int module_do_builtin(const char *, module_t **);
73 static int module_fetch_info(module_t *);
74
75 /*
76 * module_error:
77 *
78 * Utility function: log an error.
79 */
80 static void
81 module_error(const char *fmt, ...)
82 {
83 va_list ap;
84
85 va_start(ap, fmt);
86 printf("WARNING: module error: ");
87 vprintf(fmt, ap);
88 printf("\n");
89 va_end(ap);
90 }
91
92 /*
93 * module_init:
94 *
95 * Initialize the module subsystem.
96 */
97 void
98 module_init(void)
99 {
100 extern struct vm_map *lkm_map;
101
102 if (lkm_map == NULL)
103 lkm_map = kernel_map;
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 static 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, bool autoload)
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 autoload);
205 mutex_exit(&module_lock);
206
207 return error;
208 }
209
210 /*
211 * module_unload:
212 *
213 * Find and unload a module by name.
214 */
215 int
216 module_unload(const char *name)
217 {
218 int error;
219
220 /* Authorize. */
221 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
222 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
223 if (error != 0) {
224 return error;
225 }
226
227 mutex_enter(&module_lock);
228 error = module_do_unload(name);
229 mutex_exit(&module_lock);
230
231 return error;
232 }
233
234 /*
235 * module_lookup:
236 *
237 * Look up a module by name.
238 */
239 module_t *
240 module_lookup(const char *name)
241 {
242 module_t *mod;
243
244 KASSERT(mutex_owned(&module_lock));
245
246 TAILQ_FOREACH(mod, &module_list, mod_chain) {
247 if (strcmp(mod->mod_info->mi_name, name) == 0) {
248 break;
249 }
250 }
251
252 return mod;
253 }
254
255 /*
256 * module_hold:
257 *
258 * Add a single reference to a module. It's the caller's
259 * responsibility to ensure that the reference is dropped
260 * later.
261 */
262 int
263 module_hold(const char *name)
264 {
265 module_t *mod;
266
267 mutex_enter(&module_lock);
268 mod = module_lookup(name);
269 if (mod == NULL) {
270 mutex_exit(&module_lock);
271 return ENOENT;
272 }
273 mod->mod_refcnt++;
274 mutex_exit(&module_lock);
275
276 return 0;
277 }
278
279 /*
280 * module_rele:
281 *
282 * Release a reference acquired with module_hold().
283 */
284 void
285 module_rele(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 panic("module_rele: gone");
294 }
295 mod->mod_refcnt--;
296 mutex_exit(&module_lock);
297 }
298
299 /*
300 * module_do_builtin:
301 *
302 * Initialize a single module from the list of modules that are
303 * built into the kernel (linked into the kernel image).
304 */
305 static int
306 module_do_builtin(const char *name, module_t **modp)
307 {
308 __link_set_decl(modules, modinfo_t);
309 modinfo_t *const *mip;
310 const char *p, *s;
311 char buf[MAXMODNAME];
312 modinfo_t *mi;
313 module_t *mod, *mod2;
314 size_t len;
315 int error, i;
316
317 KASSERT(mutex_owned(&module_lock));
318
319 /*
320 * Check to see if already loaded.
321 */
322 if ((mod = module_lookup(name)) != NULL) {
323 if (modp != NULL) {
324 *modp = mod;
325 }
326 return 0;
327 }
328
329 /*
330 * Search the list to see if we have a module by this name.
331 */
332 error = ENOENT;
333 __link_set_foreach(mip, modules) {
334 mi = *mip;
335 if (mi == &module_dummy) {
336 continue;
337 }
338 if (strcmp(mi->mi_name, name) == 0) {
339 error = 0;
340 break;
341 }
342 }
343 if (error != 0) {
344 return error;
345 }
346
347 /*
348 * Initialize pre-requisites.
349 */
350 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
351 if (mod == NULL) {
352 return ENOMEM;
353 }
354 if (modp != NULL) {
355 *modp = mod;
356 }
357 if (mi->mi_required != NULL) {
358 for (s = mi->mi_required; *s != '\0'; s = p) {
359 if (*s == ',')
360 s++;
361 p = s;
362 while (*p != '\0' && *p != ',')
363 p++;
364 len = min(p - s + 1, sizeof(buf));
365 strlcpy(buf, s, len);
366 if (buf[0] == '\0')
367 break;
368 if (mod->mod_nrequired == MAXMODDEPS - 1) {
369 module_error("too many required modules");
370 kmem_free(mod, sizeof(*mod));
371 return EINVAL;
372 }
373 error = module_do_builtin(buf, &mod2);
374 if (error != 0) {
375 kmem_free(mod, sizeof(*mod));
376 return error;
377 }
378 mod->mod_required[mod->mod_nrequired++] = mod2;
379 }
380 }
381
382 /*
383 * Try to initialize the module.
384 */
385 KASSERT(module_active == NULL);
386 module_active = mod;
387 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
388 module_active = NULL;
389 if (error != 0) {
390 module_error("builtin module `%s' "
391 "failed to init", mi->mi_name);
392 kmem_free(mod, sizeof(*mod));
393 return error;
394 }
395 mod->mod_info = mi;
396 mod->mod_source = MODULE_SOURCE_KERNEL;
397 module_count++;
398 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
399
400 /*
401 * If that worked, count dependencies.
402 */
403 for (i = 0; i < mod->mod_nrequired; i++) {
404 mod->mod_required[i]->mod_refcnt++;
405 }
406
407 return 0;
408 }
409
410 /*
411 * module_do_load:
412 *
413 * Helper routine: load a module from the file system, or one
414 * pushed by the boot loader.
415 */
416 static int
417 module_do_load(const char *filename, bool isdep, int flags,
418 prop_dictionary_t props, module_t **modp, modclass_t class,
419 bool autoload)
420 {
421 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
422 static int depth;
423 const int maxdepth = 6;
424 modinfo_t *mi;
425 module_t *mod, *mod2;
426 char buf[MAXMODNAME];
427 const char *s, *p;
428 int error;
429 size_t len;
430 u_int i;
431
432 KASSERT(mutex_owned(&module_lock));
433
434 error = 0;
435
436 /*
437 * Avoid recursing too far.
438 */
439 if (++depth > maxdepth) {
440 module_error("too many required modules");
441 depth--;
442 return EMLINK;
443 }
444
445 /*
446 * Load the module and link. Before going to the file system,
447 * scan the list of modules loaded by the boot loader. Just
448 * before init is started the list of modules loaded at boot
449 * will be purged. Before init is started we can assume that
450 * `filename' is a module name and not a path name.
451 */
452 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
453 if (strcmp(mod->mod_info->mi_name, filename) == 0) {
454 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
455 break;
456 }
457 }
458 if (mod != NULL) {
459 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
460 } else {
461 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
462 if (mod == NULL) {
463 depth--;
464 return ENOMEM;
465 }
466 error = kobj_load_file(&mod->mod_kobj, filename, module_base,
467 autoload);
468 if (error != 0) {
469 kmem_free(mod, sizeof(*mod));
470 depth--;
471 module_error("unable to load kernel object");
472 return error;
473 }
474 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
475 mod->mod_source = MODULE_SOURCE_FILESYS;
476 error = module_fetch_info(mod);
477 if (error != 0) {
478 goto fail;
479 }
480 }
481
482 /*
483 * Check compatibility.
484 */
485 mi = mod->mod_info;
486 if (strlen(mi->mi_name) >= MAXMODNAME) {
487 error = EINVAL;
488 module_error("module name too long");
489 goto fail;
490 }
491 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
492 error = EPROGMISMATCH;
493 module_error("module built for different version of system");
494 goto fail;
495 }
496
497 /*
498 * If a specific kind of module was requested, ensure that we have
499 * a match.
500 */
501 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
502 error = ENOENT;
503 goto fail;
504 }
505
506 /*
507 * If loading a dependency, `filename' is a plain module name.
508 * The name must match.
509 */
510 if (isdep && strcmp(mi->mi_name, filename) != 0) {
511 error = ENOENT;
512 goto fail;
513 }
514
515 /*
516 * Check to see if the module is already loaded. If so, we may
517 * have been recursively called to handle a dependency, so be sure
518 * to set modp.
519 */
520 if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
521 if (modp != NULL)
522 *modp = mod2;
523 error = EEXIST;
524 goto fail;
525 }
526
527 /*
528 * Block circular dependencies.
529 */
530 TAILQ_FOREACH(mod2, &pending, mod_chain) {
531 if (mod == mod2) {
532 continue;
533 }
534 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
535 error = EDEADLK;
536 module_error("circular dependency detected");
537 goto fail;
538 }
539 }
540
541 /*
542 * Now try to load any requisite modules.
543 */
544 if (mi->mi_required != NULL) {
545 for (s = mi->mi_required; *s != '\0'; s = p) {
546 if (*s == ',')
547 s++;
548 p = s;
549 while (*p != '\0' && *p != ',')
550 p++;
551 len = p - s + 1;
552 if (len >= MAXMODNAME) {
553 error = EINVAL;
554 module_error("required module name too long");
555 goto fail;
556 }
557 strlcpy(buf, s, len);
558 if (buf[0] == '\0')
559 break;
560 if (mod->mod_nrequired == MAXMODDEPS - 1) {
561 error = EINVAL;
562 module_error("too many required modules");
563 goto fail;
564 }
565 if (strcmp(buf, mi->mi_name) == 0) {
566 error = EDEADLK;
567 module_error("self-dependency detected");
568 goto fail;
569 }
570 error = module_do_load(buf, true, flags, NULL,
571 &mod->mod_required[mod->mod_nrequired++],
572 MODULE_CLASS_ANY, true);
573 if (error != 0 && error != EEXIST)
574 goto fail;
575 }
576 }
577
578 /*
579 * We loaded all needed modules successfully: perform global
580 * relocations and initialize.
581 */
582 error = kobj_affix(mod->mod_kobj, mi->mi_name);
583 if (error != 0) {
584 module_error("unable to affix module");
585 goto fail2;
586 }
587
588 KASSERT(module_active == NULL);
589 module_active = mod;
590 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
591 module_active = NULL;
592 if (error != 0) {
593 module_error("modctl function returned error %d", error);
594 goto fail;
595 }
596
597 /*
598 * Good, the module loaded successfully. Put it onto the
599 * list and add references to its requisite modules.
600 */
601 module_count++;
602 TAILQ_REMOVE(&pending, mod, mod_chain);
603 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
604 for (i = 0; i < mod->mod_nrequired; i++) {
605 KASSERT(mod->mod_required[i] != NULL);
606 mod->mod_required[i]->mod_refcnt++;
607 }
608 if (modp != NULL) {
609 *modp = mod;
610 }
611 depth--;
612 return 0;
613
614 fail:
615 kobj_unload(mod->mod_kobj);
616 fail2:
617 TAILQ_REMOVE(&pending, mod, mod_chain);
618 kmem_free(mod, sizeof(*mod));
619 depth--;
620 return error;
621 }
622
623 /*
624 * module_do_unload:
625 *
626 * Helper routine: do the dirty work of unloading a module.
627 */
628 static int
629 module_do_unload(const char *name)
630 {
631 module_t *mod;
632 int error;
633 u_int i;
634
635 KASSERT(mutex_owned(&module_lock));
636
637 mod = module_lookup(name);
638 if (mod == NULL) {
639 return ENOENT;
640 }
641 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
642 return EBUSY;
643 }
644 KASSERT(module_active == NULL);
645 module_active = mod;
646 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
647 module_active = NULL;
648 if (error != 0) {
649 return error;
650 }
651 module_count--;
652 TAILQ_REMOVE(&module_list, mod, mod_chain);
653 for (i = 0; i < mod->mod_nrequired; i++) {
654 mod->mod_required[i]->mod_refcnt--;
655 }
656 if (mod->mod_kobj != NULL) {
657 kobj_unload(mod->mod_kobj);
658 }
659 kmem_free(mod, sizeof(*mod));
660
661 return 0;
662 }
663
664 /*
665 * module_prime:
666 *
667 * Push a module loaded by the bootloader onto our internal
668 * list.
669 */
670 int
671 module_prime(void *base, size_t size)
672 {
673 module_t *mod;
674 int error;
675
676 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
677 if (mod == NULL) {
678 return ENOMEM;
679 }
680 mod->mod_source = MODULE_SOURCE_BOOT;
681
682 error = kobj_load_mem(&mod->mod_kobj, base, size);
683 if (error != 0) {
684 kmem_free(mod, sizeof(*mod));
685 module_error("unable to load object pushed by boot loader");
686 return error;
687 }
688 error = module_fetch_info(mod);
689 if (error != 0) {
690 kobj_unload(mod->mod_kobj);
691 kmem_free(mod, sizeof(*mod));
692 module_error("unable to load object pushed by boot loader");
693 return error;
694 }
695
696 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
697
698 return 0;
699 }
700
701 /*
702 * module_fetch_into:
703 *
704 * Fetch modinfo record from a loaded module.
705 */
706 static int
707 module_fetch_info(module_t *mod)
708 {
709 int error;
710 void *addr;
711 size_t size;
712
713 /*
714 * Find module info record and check compatibility.
715 */
716 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
717 &addr, &size);
718 if (error != 0) {
719 module_error("`link_set_modules' section not present");
720 return error;
721 }
722 if (size != sizeof(modinfo_t **)) {
723 module_error("`link_set_modules' section wrong size");
724 return error;
725 }
726 mod->mod_info = *(modinfo_t **)addr;
727
728 return 0;
729 }
730
731 /*
732 * module_find_section:
733 *
734 * Allows a module that is being initialized to look up a section
735 * within its ELF object.
736 */
737 int
738 module_find_section(const char *name, void **addr, size_t *size)
739 {
740
741 KASSERT(mutex_owned(&module_lock));
742 KASSERT(module_active != NULL);
743
744 return kobj_find_section(module_active->mod_kobj, name, addr, size);
745 }
746