kern_module.c revision 1.34 1 /* $NetBSD: kern_module.c,v 1.34 2008/12/03 12:14:11 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.34 2008/12/03 12:14:11 ad Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_ddb.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
47 #include <sys/proc.h>
48 #include <sys/kauth.h>
49 #include <sys/kobj.h>
50 #include <sys/kmem.h>
51 #include <sys/module.h>
52 #include <sys/kauth.h>
53 #include <sys/kthread.h>
54 #include <sys/sysctl.h>
55
56 #include <uvm/uvm_extern.h>
57
58 #include <machine/stdarg.h>
59
60 struct vm_map *module_map;
61
62 struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
63 struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
64 static module_t *module_active;
65 static char module_base[64];
66 static int module_verbose_on;
67 static int module_autoload_on = 1;
68 u_int module_count;
69 kmutex_t module_lock;
70 u_int module_autotime = 10;
71 u_int module_gen = 1;
72 static kcondvar_t module_thread_cv;
73 static kmutex_t module_thread_lock;
74 static int module_thread_ticks;
75
76 /* Ensure that the kernel's link set isn't empty. */
77 static modinfo_t module_dummy;
78 __link_set_add_rodata(modules, module_dummy);
79
80 static module_t *module_lookup(const char *);
81 static int module_do_load(const char *, bool, int, prop_dictionary_t,
82 module_t **, modclass_t class, bool);
83 static int module_do_unload(const char *);
84 static void module_error(const char *, ...);
85 static void module_print(const char *, ...);
86 static int module_do_builtin(const char *, module_t **);
87 static int module_fetch_info(module_t *);
88 static void module_thread(void *);
89
90 /*
91 * module_error:
92 *
93 * Utility function: log an error.
94 */
95 static void
96 module_error(const char *fmt, ...)
97 {
98 va_list ap;
99
100 va_start(ap, fmt);
101 printf("WARNING: module error: ");
102 vprintf(fmt, ap);
103 printf("\n");
104 va_end(ap);
105 }
106
107 /*
108 * module_print:
109 *
110 * Utility function: log verbose output.
111 */
112 static void
113 module_print(const char *fmt, ...)
114 {
115 va_list ap;
116
117 if (module_verbose_on) {
118 va_start(ap, fmt);
119 printf("DEBUG: module: ");
120 vprintf(fmt, ap);
121 printf("\n");
122 va_end(ap);
123 }
124 }
125
126 /*
127 * module_init:
128 *
129 * Initialize the module subsystem.
130 */
131 void
132 module_init(void)
133 {
134 extern struct vm_map *module_map;
135 int error;
136
137 if (module_map == NULL) {
138 module_map = kernel_map;
139 }
140 mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
141 cv_init(&module_thread_cv, "modunload");
142 mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
143 #ifdef MODULAR /* XXX */
144 module_init_md();
145 #endif
146
147 #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
148 snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
149 machine, osrelease);
150 #else /* release */
151 snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
152 machine, __NetBSD_Version__ / 100000000,
153 __NetBSD_Version__ / 1000000 % 100);
154 #endif
155
156 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
157 NULL, NULL, "modunload");
158 if (error != 0)
159 panic("module_init: %d", error);
160 }
161
162 SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup")
163 {
164 const struct sysctlnode *node = NULL;
165
166 sysctl_createv(clog, 0, NULL, NULL,
167 CTLFLAG_PERMANENT,
168 CTLTYPE_NODE, "kern", NULL,
169 NULL, 0, NULL, 0,
170 CTL_KERN, CTL_EOL);
171 sysctl_createv(clog, 0, NULL, &node,
172 CTLFLAG_PERMANENT,
173 CTLTYPE_NODE, "module",
174 SYSCTL_DESCR("Module options"),
175 NULL, 0, NULL, 0,
176 CTL_KERN, CTL_CREATE, CTL_EOL);
177
178 if (node == NULL)
179 return;
180
181 sysctl_createv(clog, 0, &node, NULL,
182 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
183 CTLTYPE_INT, "autoload",
184 SYSCTL_DESCR("Enable automatic load of modules"),
185 NULL, 0, &module_autoload_on, 0,
186 CTL_CREATE, CTL_EOL);
187 sysctl_createv(clog, 0, &node, NULL,
188 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
189 CTLTYPE_INT, "verbose",
190 SYSCTL_DESCR("Enable verbose output"),
191 NULL, 0, &module_verbose_on, 0,
192 CTL_CREATE, CTL_EOL);
193 }
194
195 /*
196 * module_init_class:
197 *
198 * Initialize all built-in and pre-loaded modules of the
199 * specified class.
200 */
201 void
202 module_init_class(modclass_t class)
203 {
204 __link_set_decl(modules, modinfo_t);
205 modinfo_t *const *mip, *mi;
206 module_t *mod;
207
208 mutex_enter(&module_lock);
209 /*
210 * Builtins first. These can't depend on pre-loaded modules.
211 */
212 __link_set_foreach(mip, modules) {
213 mi = *mip;
214 if (mi == &module_dummy) {
215 continue;
216 }
217 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
218 continue;
219 }
220 (void)module_do_builtin(mi->mi_name, NULL);
221 }
222 /*
223 * Now preloaded modules. These will be pulled off the
224 * list as we call module_do_load();
225 */
226 do {
227 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
228 mi = mod->mod_info;
229 if (class != MODULE_CLASS_ANY &&
230 class != mi->mi_class)
231 continue;
232 module_do_load(mi->mi_name, false, 0, NULL, NULL,
233 class, true);
234 break;
235 }
236 } while (mod != NULL);
237 mutex_exit(&module_lock);
238 }
239
240 /*
241 * module_compatible:
242 *
243 * Return true if the two supplied kernel versions are said to
244 * have the same binary interface for kernel code. The entire
245 * version is signficant for the development tree (-current),
246 * major and minor versions are significant for official
247 * releases of the system.
248 */
249 bool
250 module_compatible(int v1, int v2)
251 {
252
253 #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
254 return v1 == v2;
255 #else /* release */
256 return abs(v1 - v2) < 10000;
257 #endif
258 }
259
260 /*
261 * module_load:
262 *
263 * Load a single module from the file system.
264 */
265 int
266 module_load(const char *filename, int flags, prop_dictionary_t props,
267 modclass_t class)
268 {
269 int error;
270
271 /* Authorize. */
272 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
273 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
274 if (error != 0) {
275 return error;
276 }
277
278 mutex_enter(&module_lock);
279 error = module_do_load(filename, false, flags, props, NULL, class,
280 false);
281 mutex_exit(&module_lock);
282
283 return error;
284 }
285
286 /*
287 * module_autoload:
288 *
289 * Load a single module from the file system, system initiated.
290 */
291 int
292 module_autoload(const char *filename, modclass_t class)
293 {
294 int error;
295
296 KASSERT(mutex_owned(&module_lock));
297
298 /* Nothing if the user has disabled it. */
299 if (!module_autoload_on) {
300 return EPERM;
301 }
302
303 /* Disallow path seperators and magic symlinks. */
304 if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
305 strchr(filename, '.') != NULL) {
306 return EPERM;
307 }
308
309 /* Authorize. */
310 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
311 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
312 if (error != 0) {
313 return error;
314 }
315
316 return module_do_load(filename, false, 0, NULL, NULL, class, true);
317 }
318
319 /*
320 * module_unload:
321 *
322 * Find and unload a module by name.
323 */
324 int
325 module_unload(const char *name)
326 {
327 int error;
328
329 /* Authorize. */
330 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
331 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
332 if (error != 0) {
333 return error;
334 }
335
336 mutex_enter(&module_lock);
337 error = module_do_unload(name);
338 mutex_exit(&module_lock);
339
340 return error;
341 }
342
343 /*
344 * module_lookup:
345 *
346 * Look up a module by name.
347 */
348 module_t *
349 module_lookup(const char *name)
350 {
351 module_t *mod;
352
353 KASSERT(mutex_owned(&module_lock));
354
355 TAILQ_FOREACH(mod, &module_list, mod_chain) {
356 if (strcmp(mod->mod_info->mi_name, name) == 0) {
357 break;
358 }
359 }
360
361 return mod;
362 }
363
364 /*
365 * module_hold:
366 *
367 * Add a single reference to a module. It's the caller's
368 * responsibility to ensure that the reference is dropped
369 * later.
370 */
371 int
372 module_hold(const char *name)
373 {
374 module_t *mod;
375
376 mutex_enter(&module_lock);
377 mod = module_lookup(name);
378 if (mod == NULL) {
379 mutex_exit(&module_lock);
380 return ENOENT;
381 }
382 mod->mod_refcnt++;
383 mutex_exit(&module_lock);
384
385 return 0;
386 }
387
388 /*
389 * module_rele:
390 *
391 * Release a reference acquired with module_hold().
392 */
393 void
394 module_rele(const char *name)
395 {
396 module_t *mod;
397
398 mutex_enter(&module_lock);
399 mod = module_lookup(name);
400 if (mod == NULL) {
401 mutex_exit(&module_lock);
402 panic("module_rele: gone");
403 }
404 mod->mod_refcnt--;
405 mutex_exit(&module_lock);
406 }
407
408 /*
409 * module_enqueue:
410 *
411 * Put a module onto the global list and update counters.
412 */
413 static void
414 module_enqueue(module_t *mod)
415 {
416 int i;
417
418 /*
419 * If there are requisite modules, put at the head of the queue.
420 * This is so that autounload can unload requisite modules with
421 * only one pass through the queue.
422 */
423 if (mod->mod_nrequired) {
424 TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
425
426 /* Add references to the requisite modules. */
427 for (i = 0; i < mod->mod_nrequired; i++) {
428 KASSERT(mod->mod_required[i] != NULL);
429 mod->mod_required[i]->mod_refcnt++;
430 }
431 } else {
432 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
433 }
434 module_count++;
435 module_gen++;
436 }
437
438 /*
439 * module_do_builtin:
440 *
441 * Initialize a single module from the list of modules that are
442 * built into the kernel (linked into the kernel image).
443 */
444 static int
445 module_do_builtin(const char *name, module_t **modp)
446 {
447 __link_set_decl(modules, modinfo_t);
448 modinfo_t *const *mip;
449 const char *p, *s;
450 char buf[MAXMODNAME];
451 modinfo_t *mi;
452 module_t *mod, *mod2;
453 size_t len;
454 int error;
455
456 KASSERT(mutex_owned(&module_lock));
457
458 /*
459 * Check to see if already loaded.
460 */
461 if ((mod = module_lookup(name)) != NULL) {
462 if (modp != NULL) {
463 *modp = mod;
464 }
465 return 0;
466 }
467
468 /*
469 * Search the list to see if we have a module by this name.
470 */
471 error = ENOENT;
472 __link_set_foreach(mip, modules) {
473 mi = *mip;
474 if (mi == &module_dummy) {
475 continue;
476 }
477 if (strcmp(mi->mi_name, name) == 0) {
478 error = 0;
479 break;
480 }
481 }
482 if (error != 0) {
483 return error;
484 }
485
486 /*
487 * Initialize pre-requisites.
488 */
489 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
490 if (mod == NULL) {
491 return ENOMEM;
492 }
493 if (modp != NULL) {
494 *modp = mod;
495 }
496 if (mi->mi_required != NULL) {
497 for (s = mi->mi_required; *s != '\0'; s = p) {
498 if (*s == ',')
499 s++;
500 p = s;
501 while (*p != '\0' && *p != ',')
502 p++;
503 len = min(p - s + 1, sizeof(buf));
504 strlcpy(buf, s, len);
505 if (buf[0] == '\0')
506 break;
507 if (mod->mod_nrequired == MAXMODDEPS - 1) {
508 module_error("too many required modules");
509 kmem_free(mod, sizeof(*mod));
510 return EINVAL;
511 }
512 error = module_do_builtin(buf, &mod2);
513 if (error != 0) {
514 kmem_free(mod, sizeof(*mod));
515 return error;
516 }
517 mod->mod_required[mod->mod_nrequired++] = mod2;
518 }
519 }
520
521 /*
522 * Try to initialize the module.
523 */
524 KASSERT(module_active == NULL);
525 module_active = mod;
526 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
527 module_active = NULL;
528 if (error != 0) {
529 module_error("builtin module `%s' "
530 "failed to init", mi->mi_name);
531 kmem_free(mod, sizeof(*mod));
532 return error;
533 }
534 mod->mod_info = mi;
535 mod->mod_source = MODULE_SOURCE_KERNEL;
536 module_enqueue(mod);
537 return 0;
538 }
539
540 /*
541 * module_do_load:
542 *
543 * Helper routine: load a module from the file system, or one
544 * pushed by the boot loader.
545 */
546 static int
547 module_do_load(const char *name, bool isdep, int flags,
548 prop_dictionary_t props, module_t **modp, modclass_t class,
549 bool autoload)
550 {
551 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
552 static int depth;
553 const int maxdepth = 6;
554 modinfo_t *mi;
555 module_t *mod, *mod2;
556 char buf[MAXMODNAME];
557 const char *s, *p;
558 int error;
559 size_t len;
560
561 KASSERT(mutex_owned(&module_lock));
562
563 error = 0;
564
565 /*
566 * Avoid recursing too far.
567 */
568 if (++depth > maxdepth) {
569 module_error("too many required modules");
570 depth--;
571 return EMLINK;
572 }
573
574 /*
575 * Load the module and link. Before going to the file system,
576 * scan the list of modules loaded by the boot loader. Just
577 * before init is started the list of modules loaded at boot
578 * will be purged. Before init is started we can assume that
579 * `name' is a module name and not a path name.
580 */
581 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
582 if (strcmp(mod->mod_info->mi_name, name) == 0) {
583 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
584 break;
585 }
586 }
587 if (mod != NULL) {
588 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
589 } else {
590 /*
591 * If a requisite module, check to see if it is
592 * already present.
593 */
594 if (isdep) {
595 TAILQ_FOREACH(mod, &module_list, mod_chain) {
596 if (strcmp(mod->mod_info->mi_name, name) == 0) {
597 break;
598 }
599 }
600 if (mod != NULL) {
601 if (modp != NULL) {
602 *modp = mod;
603 }
604 depth--;
605 return 0;
606 }
607 }
608 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
609 if (mod == NULL) {
610 depth--;
611 return ENOMEM;
612 }
613 error = kobj_load_file(&mod->mod_kobj, name, module_base,
614 autoload);
615 if (error != 0) {
616 kmem_free(mod, sizeof(*mod));
617 depth--;
618 if (autoload) {
619 module_print("Cannot load kernel object `%s'"
620 " error=%d", name, error);
621 } else {
622 module_error("Cannot load kernel object `%s'"
623 " error=%d", name, error);
624 }
625 return error;
626 }
627 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
628 mod->mod_source = MODULE_SOURCE_FILESYS;
629 error = module_fetch_info(mod);
630 if (error != 0) {
631 module_error("cannot fetch module info for `%s'",
632 name);
633 goto fail;
634 }
635 }
636
637 /*
638 * Check compatibility.
639 */
640 mi = mod->mod_info;
641 if (strlen(mi->mi_name) >= MAXMODNAME) {
642 error = EINVAL;
643 module_error("module name `%s' too long", mi->mi_name);
644 goto fail;
645 }
646 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
647 module_error("module built for `%d', system `%d'",
648 mi->mi_version, __NetBSD_Version__);
649 if ((flags & MODCTL_LOAD_FORCE) != 0) {
650 module_error("forced load, system may be unstable");
651 } else {
652 error = EPROGMISMATCH;
653 goto fail;
654 }
655 }
656
657 /*
658 * If a specific kind of module was requested, ensure that we have
659 * a match.
660 */
661 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
662 module_print("incompatible module class for `%s' (%d != %d)",
663 name, class, mi->mi_class);
664 error = ENOENT;
665 goto fail;
666 }
667
668 /*
669 * If loading a dependency, `name' is a plain module name.
670 * The name must match.
671 */
672 if (isdep && strcmp(mi->mi_name, name) != 0) {
673 module_error("dependency name mismatch (`%s' != `%s')",
674 name, mi->mi_name);
675 error = ENOENT;
676 goto fail;
677 }
678
679 /*
680 * Check to see if the module is already loaded. If so, we may
681 * have been recursively called to handle a dependency, so be sure
682 * to set modp.
683 */
684 if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
685 if (modp != NULL)
686 *modp = mod2;
687 module_print("module `%s' already loaded", mi->mi_name);
688 error = EEXIST;
689 goto fail;
690 }
691
692 /*
693 * Block circular dependencies.
694 */
695 TAILQ_FOREACH(mod2, &pending, mod_chain) {
696 if (mod == mod2) {
697 continue;
698 }
699 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
700 error = EDEADLK;
701 module_error("circular dependency detected for `%s'",
702 mi->mi_name);
703 goto fail;
704 }
705 }
706
707 /*
708 * Now try to load any requisite modules.
709 */
710 if (mi->mi_required != NULL) {
711 for (s = mi->mi_required; *s != '\0'; s = p) {
712 if (*s == ',')
713 s++;
714 p = s;
715 while (*p != '\0' && *p != ',')
716 p++;
717 len = p - s + 1;
718 if (len >= MAXMODNAME) {
719 error = EINVAL;
720 module_error("required module name `%s'"
721 " too long", mi->mi_required);
722 goto fail;
723 }
724 strlcpy(buf, s, len);
725 if (buf[0] == '\0')
726 break;
727 if (mod->mod_nrequired == MAXMODDEPS - 1) {
728 error = EINVAL;
729 module_error("too many required modules (%d)",
730 mod->mod_nrequired);
731 goto fail;
732 }
733 if (strcmp(buf, mi->mi_name) == 0) {
734 error = EDEADLK;
735 module_error("self-dependency detected for "
736 "`%s'", mi->mi_name);
737 goto fail;
738 }
739 error = module_do_load(buf, true, flags, NULL,
740 &mod->mod_required[mod->mod_nrequired++],
741 MODULE_CLASS_ANY, true);
742 if (error != 0)
743 goto fail;
744 }
745 }
746
747 /*
748 * We loaded all needed modules successfully: perform global
749 * relocations and initialize.
750 */
751 error = kobj_affix(mod->mod_kobj, mi->mi_name);
752 if (error != 0) {
753 module_error("unable to affix module `%s'", mi->mi_name);
754 goto fail2;
755 }
756
757 KASSERT(module_active == NULL);
758 module_active = mod;
759 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
760 module_active = NULL;
761 if (error != 0) {
762 module_error("modcmd function returned error %d for `%s'",
763 error, mi->mi_name);
764 goto fail;
765 }
766
767 /*
768 * Good, the module loaded successfully. Put it onto the
769 * list and add references to its requisite modules.
770 */
771 TAILQ_REMOVE(&pending, mod, mod_chain);
772 module_enqueue(mod);
773 if (modp != NULL) {
774 *modp = mod;
775 }
776 if (autoload) {
777 /*
778 * Arrange to try unloading the module after
779 * a short delay.
780 */
781 mod->mod_autotime = time_second + module_autotime;
782 module_thread_kick();
783 }
784 depth--;
785 return 0;
786
787 fail:
788 kobj_unload(mod->mod_kobj);
789 fail2:
790 TAILQ_REMOVE(&pending, mod, mod_chain);
791 kmem_free(mod, sizeof(*mod));
792 depth--;
793 return error;
794 }
795
796 /*
797 * module_do_unload:
798 *
799 * Helper routine: do the dirty work of unloading a module.
800 */
801 static int
802 module_do_unload(const char *name)
803 {
804 module_t *mod;
805 int error;
806 u_int i;
807
808 KASSERT(mutex_owned(&module_lock));
809
810 mod = module_lookup(name);
811 if (mod == NULL) {
812 module_error("module `%s' not found", name);
813 return ENOENT;
814 }
815 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
816 module_print("module `%s' busy", name);
817 return EBUSY;
818 }
819 KASSERT(module_active == NULL);
820 module_active = mod;
821 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
822 module_active = NULL;
823 if (error != 0) {
824 module_print("cannot unload module `%s' error=%d", name,
825 error);
826 return error;
827 }
828 module_count--;
829 TAILQ_REMOVE(&module_list, mod, mod_chain);
830 for (i = 0; i < mod->mod_nrequired; i++) {
831 mod->mod_required[i]->mod_refcnt--;
832 }
833 if (mod->mod_kobj != NULL) {
834 kobj_unload(mod->mod_kobj);
835 }
836 kmem_free(mod, sizeof(*mod));
837 module_gen++;
838
839 return 0;
840 }
841
842 /*
843 * module_prime:
844 *
845 * Push a module loaded by the bootloader onto our internal
846 * list.
847 */
848 int
849 module_prime(void *base, size_t size)
850 {
851 module_t *mod;
852 int error;
853
854 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
855 if (mod == NULL) {
856 return ENOMEM;
857 }
858 mod->mod_source = MODULE_SOURCE_BOOT;
859
860 error = kobj_load_mem(&mod->mod_kobj, base, size);
861 if (error != 0) {
862 kmem_free(mod, sizeof(*mod));
863 module_error("unable to load object pushed by boot loader");
864 return error;
865 }
866 error = module_fetch_info(mod);
867 if (error != 0) {
868 kobj_unload(mod->mod_kobj);
869 kmem_free(mod, sizeof(*mod));
870 module_error("unable to load object pushed by boot loader");
871 return error;
872 }
873
874 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
875
876 return 0;
877 }
878
879 /*
880 * module_fetch_into:
881 *
882 * Fetch modinfo record from a loaded module.
883 */
884 static int
885 module_fetch_info(module_t *mod)
886 {
887 int error;
888 void *addr;
889 size_t size;
890
891 /*
892 * Find module info record and check compatibility.
893 */
894 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
895 &addr, &size);
896 if (error != 0) {
897 module_error("`link_set_modules' section not present");
898 return error;
899 }
900 if (size != sizeof(modinfo_t **)) {
901 module_error("`link_set_modules' section wrong size");
902 return error;
903 }
904 mod->mod_info = *(modinfo_t **)addr;
905
906 return 0;
907 }
908
909 /*
910 * module_find_section:
911 *
912 * Allows a module that is being initialized to look up a section
913 * within its ELF object.
914 */
915 int
916 module_find_section(const char *name, void **addr, size_t *size)
917 {
918
919 KASSERT(mutex_owned(&module_lock));
920 KASSERT(module_active != NULL);
921
922 return kobj_find_section(module_active->mod_kobj, name, addr, size);
923 }
924
925 /*
926 * module_thread:
927 *
928 * Automatically unload modules. We try once to unload autoloaded
929 * modules after module_autotime seconds. If the system is under
930 * severe memory pressure, we'll try unloading all modules.
931 */
932 static void
933 module_thread(void *cookie)
934 {
935 module_t *mod, *next;
936 modinfo_t *mi;
937 int error;
938
939 for (;;) {
940 mutex_enter(&module_lock);
941 for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
942 next = TAILQ_NEXT(mod, mod_chain);
943 if (uvmexp.free < uvmexp.freemin) {
944 module_thread_ticks = hz;
945 } else if (mod->mod_autotime == 0) {
946 continue;
947 } else if (time_second < mod->mod_autotime) {
948 module_thread_ticks = hz;
949 continue;
950 } else {
951 mod->mod_autotime = 0;
952 }
953 /*
954 * If this module wants to avoid autounload then
955 * skip it. Some modules can ping-pong in and out
956 * because their use is transient but often.
957 * Example: exec_script.
958 */
959 mi = mod->mod_info;
960 error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
961 if (error == 0 || error == ENOTTY) {
962 (void)module_do_unload(mi->mi_name);
963 }
964 }
965 mutex_exit(&module_lock);
966
967 mutex_enter(&module_thread_lock);
968 (void)cv_timedwait(&module_thread_cv, &module_thread_lock,
969 module_thread_ticks);
970 module_thread_ticks = 0;
971 mutex_exit(&module_thread_lock);
972 }
973 }
974
975 /*
976 * module_thread:
977 *
978 * Kick the module thread into action, perhaps because the
979 * system is low on memory.
980 */
981 void
982 module_thread_kick(void)
983 {
984
985 mutex_enter(&module_thread_lock);
986 module_thread_ticks = hz;
987 cv_broadcast(&module_thread_cv);
988 mutex_exit(&module_thread_lock);
989 }
990
991 #ifdef DDB
992 /*
993 * module_whatis:
994 *
995 * Helper routine for DDB.
996 */
997 void
998 module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
999 {
1000 module_t *mod;
1001 size_t msize;
1002 vaddr_t maddr;
1003
1004 TAILQ_FOREACH(mod, &module_list, mod_chain) {
1005 kobj_stat(mod->mod_kobj, &maddr, &msize);
1006 if (addr < maddr || addr >= maddr + msize) {
1007 continue;
1008 }
1009 (*pr)("%p is %p+%zu, in kernel module `%s'\n",
1010 (void *)addr, (void *)maddr,
1011 (size_t)(addr - maddr), mod->mod_info->mi_name);
1012 }
1013 }
1014
1015 /*
1016 * module_print_list:
1017 *
1018 * Helper routine for DDB.
1019 */
1020 void
1021 module_print_list(void (*pr)(const char *, ...))
1022 {
1023 const char *src;
1024 module_t *mod;
1025 size_t msize;
1026 vaddr_t maddr;
1027
1028 (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
1029
1030 TAILQ_FOREACH(mod, &module_list, mod_chain) {
1031 switch (mod->mod_source) {
1032 case MODULE_SOURCE_KERNEL:
1033 src = "builtin";
1034 break;
1035 case MODULE_SOURCE_FILESYS:
1036 src = "filesys";
1037 break;
1038 case MODULE_SOURCE_BOOT:
1039 src = "boot";
1040 break;
1041 default:
1042 src = "unknown";
1043 break;
1044 }
1045 kobj_stat(mod->mod_kobj, &maddr, &msize);
1046 (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
1047 (long)maddr, (long)msize, src);
1048 }
1049 }
1050 #endif /* DDB */
1051