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