Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.125
      1 /*	$NetBSD: kern_module.c,v 1.125 2017/06/01 02:45:13 chs 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.125 2017/06/01 02:45:13 chs Exp $");
     38 
     39 #define _MODULE_INTERNAL
     40 
     41 #ifdef _KERNEL_OPT
     42 #include "opt_ddb.h"
     43 #include "opt_modular.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/kauth.h>
     51 #include <sys/kobj.h>
     52 #include <sys/kmem.h>
     53 #include <sys/module.h>
     54 #include <sys/kthread.h>
     55 #include <sys/sysctl.h>
     56 #include <sys/lock.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 struct vm_map *module_map;
     61 const char *module_machine;
     62 char	module_base[MODULE_BASE_SIZE];
     63 
     64 struct modlist        module_list = TAILQ_HEAD_INITIALIZER(module_list);
     65 struct modlist        module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
     66 static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     67 
     68 static module_t	*module_active;
     69 bool		module_verbose_on;
     70 #ifdef MODULAR_DEFAULT_AUTOLOAD
     71 bool		module_autoload_on = true;
     72 #else
     73 bool		module_autoload_on = false;
     74 #endif
     75 u_int		module_count;
     76 u_int		module_builtinlist;
     77 u_int		module_autotime = 10;
     78 u_int		module_gen = 1;
     79 static kcondvar_t module_thread_cv;
     80 static kmutex_t module_thread_lock;
     81 static int	module_thread_ticks;
     82 int (*module_load_vfs_vec)(const char *, int, bool, module_t *,
     83 			   prop_dictionary_t *) = (void *)eopnotsupp;
     84 
     85 static kauth_listener_t	module_listener;
     86 
     87 /* Ensure that the kernel's link set isn't empty. */
     88 static modinfo_t module_dummy;
     89 __link_set_add_rodata(modules, module_dummy);
     90 
     91 static module_t	*module_newmodule(modsrc_t);
     92 static void	module_require_force(module_t *);
     93 static int	module_do_load(const char *, bool, int, prop_dictionary_t,
     94 		    module_t **, modclass_t modclass, bool);
     95 static int	module_do_unload(const char *, bool);
     96 static int	module_do_builtin(const module_t *, const char *, module_t **,
     97     prop_dictionary_t);
     98 static int	module_fetch_info(module_t *);
     99 static void	module_thread(void *);
    100 
    101 static module_t	*module_lookup(const char *);
    102 static void	module_enqueue(module_t *);
    103 
    104 static bool	module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
    105 
    106 static void	sysctl_module_setup(void);
    107 static int	sysctl_module_autotime(SYSCTLFN_PROTO);
    108 
    109 #define MODULE_CLASS_MATCH(mi, modclass) \
    110 	((modclass) == MODULE_CLASS_ANY || (modclass) == (mi)->mi_class)
    111 
    112 static void
    113 module_incompat(const modinfo_t *mi, int modclass)
    114 {
    115 	module_error("incompatible module class for `%s' (%d != %d)",
    116 	    mi->mi_name, modclass, mi->mi_class);
    117 }
    118 
    119 /*
    120  * module_error:
    121  *
    122  *	Utility function: log an error.
    123  */
    124 void
    125 module_error(const char *fmt, ...)
    126 {
    127 	va_list ap;
    128 
    129 	va_start(ap, fmt);
    130 	printf("WARNING: module error: ");
    131 	vprintf(fmt, ap);
    132 	printf("\n");
    133 	va_end(ap);
    134 }
    135 
    136 /*
    137  * module_print:
    138  *
    139  *	Utility function: log verbose output.
    140  */
    141 void
    142 module_print(const char *fmt, ...)
    143 {
    144 	va_list ap;
    145 
    146 	if (module_verbose_on) {
    147 		va_start(ap, fmt);
    148 		printf("DEBUG: module: ");
    149 		vprintf(fmt, ap);
    150 		printf("\n");
    151 		va_end(ap);
    152 	}
    153 }
    154 
    155 static int
    156 module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    157     void *arg0, void *arg1, void *arg2, void *arg3)
    158 {
    159 	int result;
    160 
    161 	result = KAUTH_RESULT_DEFER;
    162 
    163 	if (action != KAUTH_SYSTEM_MODULE)
    164 		return result;
    165 
    166 	if ((uintptr_t)arg2 != 0)	/* autoload */
    167 		result = KAUTH_RESULT_ALLOW;
    168 
    169 	return result;
    170 }
    171 
    172 /*
    173  * Allocate a new module_t
    174  */
    175 static module_t *
    176 module_newmodule(modsrc_t source)
    177 {
    178 	module_t *mod;
    179 
    180 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    181 	mod->mod_source = source;
    182 	mod->mod_info = NULL;
    183 	mod->mod_flags = 0;
    184 	return mod;
    185 }
    186 
    187 /*
    188  * Require the -f (force) flag to load a module
    189  */
    190 static void
    191 module_require_force(struct module *mod)
    192 {
    193 	mod->mod_flags |= MODFLG_MUST_FORCE;
    194 }
    195 
    196 /*
    197  * Add modules to the builtin list.  This can done at boottime or
    198  * at runtime if the module is linked into the kernel with an
    199  * external linker.  All or none of the input will be handled.
    200  * Optionally, the modules can be initialized.  If they are not
    201  * initialized, module_init_class() or module_load() can be used
    202  * later, but these are not guaranteed to give atomic results.
    203  */
    204 int
    205 module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
    206 {
    207 	struct module **modp = NULL, *mod_iter;
    208 	int rv = 0, i, mipskip;
    209 
    210 	if (init) {
    211 		rv = kauth_authorize_system(kauth_cred_get(),
    212 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
    213 		    (void *)(uintptr_t)1, NULL);
    214 		if (rv) {
    215 			return rv;
    216 		}
    217 	}
    218 
    219 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    220 		if (mip[i] == &module_dummy) {
    221 			KASSERT(nmodinfo > 0);
    222 			nmodinfo--;
    223 		}
    224 	}
    225 	if (nmodinfo == 0)
    226 		return 0;
    227 
    228 	modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
    229 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    230 		if (mip[i+mipskip] == &module_dummy) {
    231 			mipskip++;
    232 			continue;
    233 		}
    234 		modp[i] = module_newmodule(MODULE_SOURCE_KERNEL);
    235 		modp[i]->mod_info = mip[i+mipskip];
    236 	}
    237 	kernconfig_lock();
    238 
    239 	/* do this in three stages for error recovery and atomicity */
    240 
    241 	/* first check for presence */
    242 	for (i = 0; i < nmodinfo; i++) {
    243 		TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
    244 			if (strcmp(mod_iter->mod_info->mi_name,
    245 			    modp[i]->mod_info->mi_name) == 0)
    246 				break;
    247 		}
    248 		if (mod_iter) {
    249 			rv = EEXIST;
    250 			goto out;
    251 		}
    252 
    253 		if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
    254 			rv = EEXIST;
    255 			goto out;
    256 		}
    257 	}
    258 
    259 	/* then add to list */
    260 	for (i = 0; i < nmodinfo; i++) {
    261 		TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
    262 		module_builtinlist++;
    263 	}
    264 
    265 	/* finally, init (if required) */
    266 	if (init) {
    267 		for (i = 0; i < nmodinfo; i++) {
    268 			rv = module_do_builtin(modp[i],
    269 			    modp[i]->mod_info->mi_name, NULL, NULL);
    270 			/* throw in the towel, recovery hard & not worth it */
    271 			if (rv)
    272 				panic("%s: builtin module \"%s\" init failed:"
    273 				    " %d", __func__,
    274 				    modp[i]->mod_info->mi_name, rv);
    275 		}
    276 	}
    277 
    278  out:
    279 	kernconfig_unlock();
    280 	if (rv != 0) {
    281 		for (i = 0; i < nmodinfo; i++) {
    282 			if (modp[i])
    283 				kmem_free(modp[i], sizeof(*modp[i]));
    284 		}
    285 	}
    286 	kmem_free(modp, sizeof(*modp) * nmodinfo);
    287 	return rv;
    288 }
    289 
    290 /*
    291  * Optionally fini and remove builtin module from the kernel.
    292  * Note: the module will now be unreachable except via mi && builtin_add.
    293  */
    294 int
    295 module_builtin_remove(modinfo_t *mi, bool fini)
    296 {
    297 	struct module *mod;
    298 	int rv = 0;
    299 
    300 	if (fini) {
    301 		rv = kauth_authorize_system(kauth_cred_get(),
    302 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
    303 		    NULL, NULL);
    304 		if (rv)
    305 			return rv;
    306 
    307 		kernconfig_lock();
    308 		rv = module_do_unload(mi->mi_name, true);
    309 		if (rv) {
    310 			goto out;
    311 		}
    312 	} else {
    313 		kernconfig_lock();
    314 	}
    315 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    316 		if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
    317 			break;
    318 	}
    319 	if (mod) {
    320 		TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    321 		module_builtinlist--;
    322 	} else {
    323 		KASSERT(fini == false);
    324 		rv = ENOENT;
    325 	}
    326 
    327  out:
    328 	kernconfig_unlock();
    329 	return rv;
    330 }
    331 
    332 /*
    333  * module_init:
    334  *
    335  *	Initialize the module subsystem.
    336  */
    337 void
    338 module_init(void)
    339 {
    340 	__link_set_decl(modules, modinfo_t);
    341 	extern struct vm_map *module_map;
    342 	modinfo_t *const *mip;
    343 	int rv;
    344 
    345 	if (module_map == NULL) {
    346 		module_map = kernel_map;
    347 	}
    348 	cv_init(&module_thread_cv, "mod_unld");
    349 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
    350 
    351 #ifdef MODULAR	/* XXX */
    352 	module_init_md();
    353 #endif
    354 
    355 	if (!module_machine)
    356 		module_machine = machine;
    357 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    358 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
    359 	    module_machine, osrelease);
    360 #else						/* release */
    361 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
    362 	    module_machine, __NetBSD_Version__ / 100000000,
    363 	    __NetBSD_Version__ / 1000000 % 100);
    364 #endif
    365 
    366 	module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    367 	    module_listener_cb, NULL);
    368 
    369 	__link_set_foreach(mip, modules) {
    370 		if ((rv = module_builtin_add(mip, 1, false)) != 0)
    371 			module_error("builtin %s failed: %d\n",
    372 			    (*mip)->mi_name, rv);
    373 	}
    374 
    375 	sysctl_module_setup();
    376 }
    377 
    378 /*
    379  * module_start_unload_thread:
    380  *
    381  *	Start the auto unload kthread.
    382  */
    383 void
    384 module_start_unload_thread(void)
    385 {
    386 	int error;
    387 
    388 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
    389 	    NULL, NULL, "modunload");
    390 	if (error != 0)
    391 		panic("%s: %d", __func__, error);
    392 }
    393 
    394 /*
    395  * module_builtin_require_force
    396  *
    397  * Require MODCTL_MUST_FORCE to load any built-in modules that have
    398  * not yet been initialized
    399  */
    400 void
    401 module_builtin_require_force(void)
    402 {
    403 	module_t *mod;
    404 
    405 	kernconfig_lock();
    406 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    407 		module_require_force(mod);
    408 	}
    409 	kernconfig_unlock();
    410 }
    411 
    412 static struct sysctllog *module_sysctllog;
    413 
    414 static int
    415 sysctl_module_autotime(SYSCTLFN_ARGS)
    416 {
    417 	struct sysctlnode node;
    418 	int t, error;
    419 
    420 	t = *(int *)rnode->sysctl_data;
    421 
    422 	node = *rnode;
    423 	node.sysctl_data = &t;
    424 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    425 	if (error || newp == NULL)
    426 		return (error);
    427 
    428 	if (t < 0)
    429 		return (EINVAL);
    430 
    431 	*(int *)rnode->sysctl_data = t;
    432 	return (0);
    433 }
    434 
    435 static void
    436 sysctl_module_setup(void)
    437 {
    438 	const struct sysctlnode *node = NULL;
    439 
    440 	sysctl_createv(&module_sysctllog, 0, NULL, &node,
    441 		CTLFLAG_PERMANENT,
    442 		CTLTYPE_NODE, "module",
    443 		SYSCTL_DESCR("Module options"),
    444 		NULL, 0, NULL, 0,
    445 		CTL_KERN, CTL_CREATE, CTL_EOL);
    446 
    447 	if (node == NULL)
    448 		return;
    449 
    450 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    451 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    452 		CTLTYPE_BOOL, "autoload",
    453 		SYSCTL_DESCR("Enable automatic load of modules"),
    454 		NULL, 0, &module_autoload_on, 0,
    455 		CTL_CREATE, CTL_EOL);
    456 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    457 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    458 		CTLTYPE_BOOL, "verbose",
    459 		SYSCTL_DESCR("Enable verbose output"),
    460 		NULL, 0, &module_verbose_on, 0,
    461 		CTL_CREATE, CTL_EOL);
    462 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    463 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    464 		CTLTYPE_STRING, "path",
    465 		SYSCTL_DESCR("Default module load path"),
    466 		NULL, 0, module_base, 0,
    467 		CTL_CREATE, CTL_EOL);
    468 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    469 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    470 		CTLTYPE_INT, "autotime",
    471 		SYSCTL_DESCR("Auto-unload delay"),
    472 		sysctl_module_autotime, 0, &module_autotime, 0,
    473 		CTL_CREATE, CTL_EOL);
    474 }
    475 
    476 /*
    477  * module_init_class:
    478  *
    479  *	Initialize all built-in and pre-loaded modules of the
    480  *	specified class.
    481  */
    482 void
    483 module_init_class(modclass_t modclass)
    484 {
    485 	TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
    486 	module_t *mod;
    487 	modinfo_t *mi;
    488 
    489 	kernconfig_lock();
    490 	/*
    491 	 * Builtins first.  These will not depend on pre-loaded modules
    492 	 * (because the kernel would not link).
    493 	 */
    494 	do {
    495 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    496 			mi = mod->mod_info;
    497 			if (!MODULE_CLASS_MATCH(mi, modclass))
    498 				continue;
    499 			/*
    500 			 * If initializing a builtin module fails, don't try
    501 			 * to load it again.  But keep it around and queue it
    502 			 * on the builtins list after we're done with module
    503 			 * init.  Don't set it to MODFLG_MUST_FORCE in case a
    504 			 * future attempt to initialize can be successful.
    505 			 * (If the module has previously been set to
    506 			 * MODFLG_MUST_FORCE, don't try to override that!)
    507 			 */
    508 			if ((mod->mod_flags & MODFLG_MUST_FORCE) ||
    509 			    module_do_builtin(mod, mi->mi_name, NULL,
    510 			    NULL) != 0) {
    511 				TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    512 				TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
    513 			}
    514 			break;
    515 		}
    516 	} while (mod != NULL);
    517 
    518 	/*
    519 	 * Now preloaded modules.  These will be pulled off the
    520 	 * list as we call module_do_load();
    521 	 */
    522 	do {
    523 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    524 			mi = mod->mod_info;
    525 			if (!MODULE_CLASS_MATCH(mi, modclass))
    526 				continue;
    527 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
    528 			    modclass, false);
    529 			break;
    530 		}
    531 	} while (mod != NULL);
    532 
    533 	/* return failed builtin modules to builtin list */
    534 	while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
    535 		TAILQ_REMOVE(&bi_fail, mod, mod_chain);
    536 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
    537 	}
    538 
    539 	kernconfig_unlock();
    540 }
    541 
    542 /*
    543  * module_compatible:
    544  *
    545  *	Return true if the two supplied kernel versions are said to
    546  *	have the same binary interface for kernel code.  The entire
    547  *	version is signficant for the development tree (-current),
    548  *	major and minor versions are significant for official
    549  *	releases of the system.
    550  */
    551 bool
    552 module_compatible(int v1, int v2)
    553 {
    554 
    555 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    556 	return v1 == v2;
    557 #else						/* release */
    558 	return abs(v1 - v2) < 10000;
    559 #endif
    560 }
    561 
    562 /*
    563  * module_load:
    564  *
    565  *	Load a single module from the file system.
    566  */
    567 int
    568 module_load(const char *filename, int flags, prop_dictionary_t props,
    569 	    modclass_t modclass)
    570 {
    571 	module_t *mod;
    572 	int error;
    573 
    574 	/* Test if we already have the module loaded before
    575 	 * authorizing so we have the opportunity to return EEXIST. */
    576 	kernconfig_lock();
    577 	mod = module_lookup(filename);
    578 	if (mod != NULL) {
    579 		module_print("%s module `%s' already loaded",
    580 		    "requested", filename);
    581 		error = EEXIST;
    582 		goto out;
    583 	}
    584 
    585 	/* Authorize. */
    586 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    587 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
    588 	if (error != 0)
    589 		goto out;
    590 
    591 	error = module_do_load(filename, false, flags, props, NULL, modclass,
    592 	    false);
    593 
    594 out:
    595 	kernconfig_unlock();
    596 	return error;
    597 }
    598 
    599 /*
    600  * module_autoload:
    601  *
    602  *	Load a single module from the file system, system initiated.
    603  */
    604 int
    605 module_autoload(const char *filename, modclass_t modclass)
    606 {
    607 	int error;
    608 
    609 	kernconfig_lock();
    610 
    611 	/* Nothing if the user has disabled it. */
    612 	if (!module_autoload_on) {
    613 		kernconfig_unlock();
    614 		return EPERM;
    615 	}
    616 
    617         /* Disallow path separators and magic symlinks. */
    618         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    619             strchr(filename, '.') != NULL) {
    620 		kernconfig_unlock();
    621         	return EPERM;
    622 	}
    623 
    624 	/* Authorize. */
    625 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    626 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    627 
    628 	if (error == 0)
    629 		error = module_do_load(filename, false, 0, NULL, NULL, modclass,
    630 		    true);
    631 
    632 	kernconfig_unlock();
    633 	return error;
    634 }
    635 
    636 /*
    637  * module_unload:
    638  *
    639  *	Find and unload a module by name.
    640  */
    641 int
    642 module_unload(const char *name)
    643 {
    644 	int error;
    645 
    646 	/* Authorize. */
    647 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    648 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    649 	if (error != 0) {
    650 		return error;
    651 	}
    652 
    653 	kernconfig_lock();
    654 	error = module_do_unload(name, true);
    655 	kernconfig_unlock();
    656 
    657 	return error;
    658 }
    659 
    660 /*
    661  * module_lookup:
    662  *
    663  *	Look up a module by name.
    664  */
    665 module_t *
    666 module_lookup(const char *name)
    667 {
    668 	module_t *mod;
    669 
    670 	KASSERT(kernconfig_is_held());
    671 
    672 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    673 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    674 			break;
    675 		}
    676 	}
    677 
    678 	return mod;
    679 }
    680 
    681 /*
    682  * module_hold:
    683  *
    684  *	Add a single reference to a module.  It's the caller's
    685  *	responsibility to ensure that the reference is dropped
    686  *	later.
    687  */
    688 int
    689 module_hold(const char *name)
    690 {
    691 	module_t *mod;
    692 
    693 	kernconfig_lock();
    694 	mod = module_lookup(name);
    695 	if (mod == NULL) {
    696 		kernconfig_unlock();
    697 		return ENOENT;
    698 	}
    699 	mod->mod_refcnt++;
    700 	kernconfig_unlock();
    701 
    702 	return 0;
    703 }
    704 
    705 /*
    706  * module_rele:
    707  *
    708  *	Release a reference acquired with module_hold().
    709  */
    710 void
    711 module_rele(const char *name)
    712 {
    713 	module_t *mod;
    714 
    715 	kernconfig_lock();
    716 	mod = module_lookup(name);
    717 	if (mod == NULL) {
    718 		kernconfig_unlock();
    719 		panic("%s: gone", __func__);
    720 	}
    721 	mod->mod_refcnt--;
    722 	kernconfig_unlock();
    723 }
    724 
    725 /*
    726  * module_enqueue:
    727  *
    728  *	Put a module onto the global list and update counters.
    729  */
    730 void
    731 module_enqueue(module_t *mod)
    732 {
    733 	int i;
    734 
    735 	KASSERT(kernconfig_is_held());
    736 
    737 	/*
    738 	 * Put new entry at the head of the queue so autounload can unload
    739 	 * requisite modules with only one pass through the queue.
    740 	 */
    741 	TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    742 	if (mod->mod_nrequired) {
    743 
    744 		/* Add references to the requisite modules. */
    745 		for (i = 0; i < mod->mod_nrequired; i++) {
    746 			KASSERT(mod->mod_required[i] != NULL);
    747 			mod->mod_required[i]->mod_refcnt++;
    748 		}
    749 	}
    750 	module_count++;
    751 	module_gen++;
    752 }
    753 
    754 /*
    755  * module_do_builtin:
    756  *
    757  *	Initialize a module from the list of modules that are
    758  *	already linked into the kernel.
    759  */
    760 static int
    761 module_do_builtin(const module_t *pmod, const char *name, module_t **modp,
    762     prop_dictionary_t props)
    763 {
    764 	const char *p, *s;
    765 	char buf[MAXMODNAME];
    766 	modinfo_t *mi = NULL;
    767 	module_t *mod, *mod2, *mod_loaded, *prev_active;
    768 	size_t len;
    769 	int error;
    770 
    771 	KASSERT(kernconfig_is_held());
    772 
    773 	/*
    774 	 * Search the list to see if we have a module by this name.
    775 	 */
    776 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    777 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    778 			mi = mod->mod_info;
    779 			break;
    780 		}
    781 	}
    782 
    783 	/*
    784 	 * Check to see if already loaded.  This might happen if we
    785 	 * were already loaded as a dependency.
    786 	 */
    787 	if ((mod_loaded = module_lookup(name)) != NULL) {
    788 		KASSERT(mod == NULL);
    789 		if (modp)
    790 			*modp = mod_loaded;
    791 		return 0;
    792 	}
    793 
    794 	/* Note! This is from TAILQ, not immediate above */
    795 	if (mi == NULL) {
    796 		/*
    797 		 * XXX: We'd like to panic here, but currently in some
    798 		 * cases (such as nfsserver + nfs), the dependee can be
    799 		 * succesfully linked without the dependencies.
    800 		 */
    801 		module_error("%s: can't find builtin dependency `%s'",
    802 		    pmod->mod_info->mi_name, name);
    803 		return ENOENT;
    804 	}
    805 
    806 	/*
    807 	 * Initialize pre-requisites.
    808 	 */
    809 	if (mi->mi_required != NULL) {
    810 		for (s = mi->mi_required; *s != '\0'; s = p) {
    811 			if (*s == ',')
    812 				s++;
    813 			p = s;
    814 			while (*p != '\0' && *p != ',')
    815 				p++;
    816 			len = min(p - s + 1, sizeof(buf));
    817 			strlcpy(buf, s, len);
    818 			if (buf[0] == '\0')
    819 				break;
    820 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    821 				module_error("%s: too many required modules "
    822 				    "%d >= %d", pmod->mod_info->mi_name,
    823 				    mod->mod_nrequired, MAXMODDEPS - 1);
    824 				return EINVAL;
    825 			}
    826 			error = module_do_builtin(mod, buf, &mod2, NULL);
    827 			if (error != 0) {
    828 				return error;
    829 			}
    830 			mod->mod_required[mod->mod_nrequired++] = mod2;
    831 		}
    832 	}
    833 
    834 	/*
    835 	 * Try to initialize the module.
    836 	 */
    837 	prev_active = module_active;
    838 	module_active = mod;
    839 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    840 	module_active = prev_active;
    841 	if (error != 0) {
    842 		module_error("builtin module `%s' "
    843 		    "failed to init, error %d", mi->mi_name, error);
    844 		return error;
    845 	}
    846 
    847 	/* load always succeeds after this point */
    848 
    849 	TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    850 	module_builtinlist--;
    851 	if (modp != NULL) {
    852 		*modp = mod;
    853 	}
    854 	module_enqueue(mod);
    855 	return 0;
    856 }
    857 
    858 /*
    859  * module_do_load:
    860  *
    861  *	Helper routine: load a module from the file system, or one
    862  *	pushed by the boot loader.
    863  */
    864 static int
    865 module_do_load(const char *name, bool isdep, int flags,
    866 	       prop_dictionary_t props, module_t **modp, modclass_t modclass,
    867 	       bool autoload)
    868 {
    869 #define MODULE_MAX_DEPTH 6
    870 
    871 	TAILQ_HEAD(pending_t, module);
    872 	static int depth = 0;
    873 	static struct pending_t *pending_lists[MODULE_MAX_DEPTH];
    874 	struct pending_t *pending;
    875 	struct pending_t new_pending = TAILQ_HEAD_INITIALIZER(new_pending);
    876 	modinfo_t *mi;
    877 	module_t *mod, *mod2, *prev_active;
    878 	prop_dictionary_t filedict;
    879 	char buf[MAXMODNAME];
    880 	const char *s, *p;
    881 	int error;
    882 	size_t len;
    883 
    884 	KASSERT(kernconfig_is_held());
    885 
    886 	filedict = NULL;
    887 	error = 0;
    888 
    889 	/*
    890 	 * Avoid recursing too far.
    891 	 */
    892 	if (++depth > MODULE_MAX_DEPTH) {
    893 		module_error("recursion too deep for `%s' %d > %d", name,
    894 		    depth, MODULE_MAX_DEPTH);
    895 		depth--;
    896 		return EMLINK;
    897 	}
    898 
    899 	/*
    900 	 * Set up the pending list for this depth.  If this is a
    901 	 * recursive entry, then use same list as for outer call,
    902 	 * else use the locally allocated list.  In either case,
    903 	 * remember which one we're using.
    904 	 */
    905 	if (isdep) {
    906 		KASSERT(depth > 1);
    907 		pending = pending_lists[depth - 2];
    908 	} else
    909 		pending = &new_pending;
    910 	pending_lists[depth - 1] = pending;
    911 
    912 	/*
    913 	 * Search the list of disabled builtins first.
    914 	 */
    915 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    916 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    917 			break;
    918 		}
    919 	}
    920 	if (mod) {
    921 		if ((mod->mod_flags & MODFLG_MUST_FORCE) &&
    922 		    (flags & MODCTL_LOAD_FORCE) == 0) {
    923 			if (!autoload) {
    924 				module_error("use -f to reinstate "
    925 				    "builtin module `%s'", name);
    926 			}
    927 			depth--;
    928 			return EPERM;
    929 		} else {
    930 			error = module_do_builtin(mod, name, modp, props);
    931 			depth--;
    932 			return error;
    933 		}
    934 	}
    935 
    936 	/*
    937 	 * Load the module and link.  Before going to the file system,
    938 	 * scan the list of modules loaded by the boot loader.
    939 	 */
    940 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    941 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    942 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    943 			break;
    944 		}
    945 	}
    946 	if (mod != NULL) {
    947 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
    948 	} else {
    949 		/*
    950 		 * Check to see if module is already present.
    951 		 */
    952 		mod = module_lookup(name);
    953 		if (mod != NULL) {
    954 			if (modp != NULL) {
    955 				*modp = mod;
    956 			}
    957 			module_print("%s module `%s' already loaded",
    958 			    isdep ? "dependent" : "requested", name);
    959 			depth--;
    960 			return EEXIST;
    961 		}
    962 
    963 		mod = module_newmodule(MODULE_SOURCE_FILESYS);
    964 		if (mod == NULL) {
    965 			module_error("out of memory for `%s'", name);
    966 			depth--;
    967 			return ENOMEM;
    968 		}
    969 
    970 		error = module_load_vfs_vec(name, flags, autoload, mod,
    971 					    &filedict);
    972 		if (error != 0) {
    973 #ifdef DEBUG
    974 			/*
    975 			 * The exec class of modules contains a list of
    976 			 * modules that is the union of all the modules
    977 			 * available for each architecture, so we don't
    978 			 * print an error if they are missing.
    979 			 */
    980 			if ((modclass != MODULE_CLASS_EXEC || error != ENOENT)
    981 			    && root_device != NULL)
    982 				module_error("vfs load failed for `%s', "
    983 				    "error %d", name, error);
    984 #endif
    985 			kmem_free(mod, sizeof(*mod));
    986 			depth--;
    987 			return error;
    988 		}
    989 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
    990 
    991 		error = module_fetch_info(mod);
    992 		if (error != 0) {
    993 			module_error("cannot fetch info for `%s', error %d",
    994 			    name, error);
    995 			goto fail;
    996 		}
    997 	}
    998 
    999 	/*
   1000 	 * Check compatibility.
   1001 	 */
   1002 	mi = mod->mod_info;
   1003 	if (strlen(mi->mi_name) >= MAXMODNAME) {
   1004 		error = EINVAL;
   1005 		module_error("module name `%s' longer than %d", mi->mi_name,
   1006 		    MAXMODNAME);
   1007 		goto fail;
   1008 	}
   1009 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
   1010 		module_error("module `%s' built for `%d', system `%d'",
   1011 		    mi->mi_name, mi->mi_version, __NetBSD_Version__);
   1012 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
   1013 			module_error("forced load, system may be unstable");
   1014 		} else {
   1015 			error = EPROGMISMATCH;
   1016 			goto fail;
   1017 		}
   1018 	}
   1019 
   1020 	/*
   1021 	 * If a specific kind of module was requested, ensure that we have
   1022 	 * a match.
   1023 	 */
   1024 	if (!MODULE_CLASS_MATCH(mi, modclass)) {
   1025 		module_incompat(mi, modclass);
   1026 		error = ENOENT;
   1027 		goto fail;
   1028 	}
   1029 
   1030 	/*
   1031 	 * If loading a dependency, `name' is a plain module name.
   1032 	 * The name must match.
   1033 	 */
   1034 	if (isdep && strcmp(mi->mi_name, name) != 0) {
   1035 		module_error("dependency name mismatch (`%s' != `%s')",
   1036 		    name, mi->mi_name);
   1037 		error = ENOENT;
   1038 		goto fail;
   1039 	}
   1040 
   1041 	/*
   1042 	 * Block circular dependencies.
   1043 	 */
   1044 	TAILQ_FOREACH(mod2, pending, mod_chain) {
   1045 		if (mod == mod2) {
   1046 			continue;
   1047 		}
   1048 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
   1049 			error = EDEADLK;
   1050 			module_error("circular dependency detected for `%s'",
   1051 			    mi->mi_name);
   1052 			goto fail;
   1053 		}
   1054 	}
   1055 
   1056 	/*
   1057 	 * Now try to load any requisite modules.
   1058 	 */
   1059 	if (mi->mi_required != NULL) {
   1060 		for (s = mi->mi_required; *s != '\0'; s = p) {
   1061 			if (*s == ',')
   1062 				s++;
   1063 			p = s;
   1064 			while (*p != '\0' && *p != ',')
   1065 				p++;
   1066 			len = p - s + 1;
   1067 			if (len >= MAXMODNAME) {
   1068 				error = EINVAL;
   1069 				module_error("required module name `%s' "
   1070 				    "longer than %d", mi->mi_required,
   1071 				    MAXMODNAME);
   1072 				goto fail;
   1073 			}
   1074 			strlcpy(buf, s, len);
   1075 			if (buf[0] == '\0')
   1076 				break;
   1077 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
   1078 				error = EINVAL;
   1079 				module_error("too many required modules "
   1080 				    "%d >= %d", mod->mod_nrequired,
   1081 				    MAXMODDEPS - 1);
   1082 				goto fail;
   1083 			}
   1084 			if (strcmp(buf, mi->mi_name) == 0) {
   1085 				error = EDEADLK;
   1086 				module_error("self-dependency detected for "
   1087 				   "`%s'", mi->mi_name);
   1088 				goto fail;
   1089 			}
   1090 			error = module_do_load(buf, true, flags, NULL,
   1091 			    &mod2, MODULE_CLASS_ANY, true);
   1092 			if (error != 0 && error != EEXIST) {
   1093 				module_error("recursive load failed for `%s' "
   1094 				    "(`%s' required), error %d", mi->mi_name,
   1095 				    buf, error);
   1096 				goto fail;
   1097 			}
   1098 			mod->mod_required[mod->mod_nrequired++] = mod2;
   1099 		}
   1100 	}
   1101 
   1102 	/*
   1103 	 * We loaded all needed modules successfully: perform global
   1104 	 * relocations and initialize.
   1105 	 */
   1106 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
   1107 	if (error != 0) {
   1108 		/* Cannot touch 'mi' as the module is now gone. */
   1109 		module_error("unable to affix module `%s', error %d", name,
   1110 		    error);
   1111 		goto fail2;
   1112 	}
   1113 
   1114 	if (filedict) {
   1115 		if (!module_merge_dicts(filedict, props)) {
   1116 			module_error("module properties failed for %s", name);
   1117 			error = EINVAL;
   1118 			goto fail;
   1119 		}
   1120 	}
   1121 	prev_active = module_active;
   1122 	module_active = mod;
   1123 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
   1124 	module_active = prev_active;
   1125 	if (filedict) {
   1126 		prop_object_release(filedict);
   1127 		filedict = NULL;
   1128 	}
   1129 	if (error != 0) {
   1130 		module_error("modcmd function failed for `%s', error %d",
   1131 		    mi->mi_name, error);
   1132 		goto fail;
   1133 	}
   1134 
   1135 	/*
   1136 	 * Good, the module loaded successfully.  Put it onto the
   1137 	 * list and add references to its requisite modules.
   1138 	 */
   1139 	TAILQ_REMOVE(pending, mod, mod_chain);
   1140 	module_enqueue(mod);
   1141 	if (modp != NULL) {
   1142 		*modp = mod;
   1143 	}
   1144 	if (autoload && module_autotime > 0) {
   1145 		/*
   1146 		 * Arrange to try unloading the module after
   1147 		 * a short delay unless auto-unload is disabled.
   1148 		 */
   1149 		mod->mod_autotime = time_second + module_autotime;
   1150 		mod->mod_flags |= MODFLG_AUTO_LOADED;
   1151 		module_thread_kick();
   1152 	}
   1153 	depth--;
   1154 	module_print("module `%s' loaded successfully", mi->mi_name);
   1155 	return 0;
   1156 
   1157  fail:
   1158 	kobj_unload(mod->mod_kobj);
   1159  fail2:
   1160 	if (filedict != NULL) {
   1161 		prop_object_release(filedict);
   1162 		filedict = NULL;
   1163 	}
   1164 	TAILQ_REMOVE(pending, mod, mod_chain);
   1165 	kmem_free(mod, sizeof(*mod));
   1166 	depth--;
   1167 	return error;
   1168 }
   1169 
   1170 /*
   1171  * module_do_unload:
   1172  *
   1173  *	Helper routine: do the dirty work of unloading a module.
   1174  */
   1175 static int
   1176 module_do_unload(const char *name, bool load_requires_force)
   1177 {
   1178 	module_t *mod, *prev_active;
   1179 	int error;
   1180 	u_int i;
   1181 
   1182 	KASSERT(kernconfig_is_held());
   1183 	KASSERT(name != NULL);
   1184 
   1185 	module_print("unload requested for '%s' (%s)", name,
   1186 	    load_requires_force ? "TRUE" : "FALSE");
   1187 	mod = module_lookup(name);
   1188 	if (mod == NULL) {
   1189 		module_error("module `%s' not found", name);
   1190 		return ENOENT;
   1191 	}
   1192 	if (mod->mod_refcnt != 0) {
   1193 		module_print("module `%s' busy (%d refs)", name,
   1194 		    mod->mod_refcnt);
   1195 		return EBUSY;
   1196 	}
   1197 
   1198 	/*
   1199 	 * Builtin secmodels are there to stay.
   1200 	 */
   1201 	if (mod->mod_source == MODULE_SOURCE_KERNEL &&
   1202 	    mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) {
   1203 		module_print("cannot unload built-in secmodel module `%s'",
   1204 		    name);
   1205 		return EPERM;
   1206 	}
   1207 
   1208 	prev_active = module_active;
   1209 	module_active = mod;
   1210 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
   1211 	module_active = prev_active;
   1212 	if (error != 0) {
   1213 		module_print("cannot unload module `%s' error=%d", name,
   1214 		    error);
   1215 		return error;
   1216 	}
   1217 	module_count--;
   1218 	TAILQ_REMOVE(&module_list, mod, mod_chain);
   1219 	for (i = 0; i < mod->mod_nrequired; i++) {
   1220 		mod->mod_required[i]->mod_refcnt--;
   1221 	}
   1222 	module_print("unloaded module `%s'", name);
   1223 	if (mod->mod_kobj != NULL) {
   1224 		kobj_unload(mod->mod_kobj);
   1225 	}
   1226 	if (mod->mod_source == MODULE_SOURCE_KERNEL) {
   1227 		mod->mod_nrequired = 0; /* will be re-parsed */
   1228 		if (load_requires_force)
   1229 			module_require_force(mod);
   1230 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
   1231 		module_builtinlist++;
   1232 	} else {
   1233 		kmem_free(mod, sizeof(*mod));
   1234 	}
   1235 	module_gen++;
   1236 
   1237 	return 0;
   1238 }
   1239 
   1240 /*
   1241  * module_prime:
   1242  *
   1243  *	Push a module loaded by the bootloader onto our internal
   1244  *	list.
   1245  */
   1246 int
   1247 module_prime(const char *name, void *base, size_t size)
   1248 {
   1249 	__link_set_decl(modules, modinfo_t);
   1250 	modinfo_t *const *mip;
   1251 	module_t *mod;
   1252 	int error;
   1253 
   1254 	/* Check for module name same as a built-in module */
   1255 
   1256 	__link_set_foreach(mip, modules) {
   1257 		if (*mip == &module_dummy)
   1258 			continue;
   1259 		if (strcmp((*mip)->mi_name, name) == 0) {
   1260 			module_error("module `%s' pushed by boot loader "
   1261 			    "already exists", name);
   1262 			return EEXIST;
   1263 		}
   1264 	}
   1265 
   1266 	/* Also eliminate duplicate boolist entries */
   1267 
   1268 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
   1269 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
   1270 			module_error("duplicate bootlist entry for module "
   1271 			    "`%s'", name);
   1272 			return EEXIST;
   1273 		}
   1274 	}
   1275 
   1276 	mod = module_newmodule(MODULE_SOURCE_BOOT);
   1277 	if (mod == NULL) {
   1278 		return ENOMEM;
   1279 	}
   1280 
   1281 	error = kobj_load_mem(&mod->mod_kobj, name, base, size);
   1282 	if (error != 0) {
   1283 		kmem_free(mod, sizeof(*mod));
   1284 		module_error("unable to load `%s' pushed by boot loader, "
   1285 		    "error %d", name, error);
   1286 		return error;
   1287 	}
   1288 	error = module_fetch_info(mod);
   1289 	if (error != 0) {
   1290 		kobj_unload(mod->mod_kobj);
   1291 		kmem_free(mod, sizeof(*mod));
   1292 		module_error("unable to fetch_info for `%s' pushed by boot "
   1293 		    "loader, error %d", name, error);
   1294 		return error;
   1295 	}
   1296 
   1297 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
   1298 
   1299 	return 0;
   1300 }
   1301 
   1302 /*
   1303  * module_fetch_into:
   1304  *
   1305  *	Fetch modinfo record from a loaded module.
   1306  */
   1307 static int
   1308 module_fetch_info(module_t *mod)
   1309 {
   1310 	int error;
   1311 	void *addr;
   1312 	size_t size;
   1313 
   1314 	/*
   1315 	 * Find module info record and check compatibility.
   1316 	 */
   1317 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
   1318 	    &addr, &size);
   1319 	if (error != 0) {
   1320 		module_error("`link_set_modules' section not present, "
   1321 		    "error %d", error);
   1322 		return error;
   1323 	}
   1324 	if (size != sizeof(modinfo_t **)) {
   1325 		module_error("`link_set_modules' section wrong size %zu != %zu",
   1326 		    size, sizeof(modinfo_t **));
   1327 		return ENOEXEC;
   1328 	}
   1329 	mod->mod_info = *(modinfo_t **)addr;
   1330 
   1331 	return 0;
   1332 }
   1333 
   1334 /*
   1335  * module_find_section:
   1336  *
   1337  *	Allows a module that is being initialized to look up a section
   1338  *	within its ELF object.
   1339  */
   1340 int
   1341 module_find_section(const char *name, void **addr, size_t *size)
   1342 {
   1343 
   1344 	KASSERT(kernconfig_is_held());
   1345 	KASSERT(module_active != NULL);
   1346 
   1347 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
   1348 }
   1349 
   1350 /*
   1351  * module_thread:
   1352  *
   1353  *	Automatically unload modules.  We try once to unload autoloaded
   1354  *	modules after module_autotime seconds.  If the system is under
   1355  *	severe memory pressure, we'll try unloading all modules, else if
   1356  *	module_autotime is zero, we don't try to unload, even if the
   1357  *	module was previously scheduled for unload.
   1358  */
   1359 static void
   1360 module_thread(void *cookie)
   1361 {
   1362 	module_t *mod, *next;
   1363 	modinfo_t *mi;
   1364 	int error;
   1365 
   1366 	for (;;) {
   1367 		kernconfig_lock();
   1368 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
   1369 			next = TAILQ_NEXT(mod, mod_chain);
   1370 
   1371 			/* skip built-in modules */
   1372 			if (mod->mod_source == MODULE_SOURCE_KERNEL)
   1373 				continue;
   1374 			/* skip modules that weren't auto-loaded */
   1375 			if ((mod->mod_flags & MODFLG_AUTO_LOADED) == 0)
   1376 				continue;
   1377 
   1378 			if (uvmexp.free < uvmexp.freemin) {
   1379 				module_thread_ticks = hz;
   1380 			} else if (module_autotime == 0 ||
   1381 				   mod->mod_autotime == 0) {
   1382 				continue;
   1383 			} else if (time_second < mod->mod_autotime) {
   1384 				module_thread_ticks = hz;
   1385 			    	continue;
   1386 			} else {
   1387 				mod->mod_autotime = 0;
   1388 			}
   1389 
   1390 			/*
   1391 			 * If this module wants to avoid autounload then
   1392 			 * skip it.  Some modules can ping-pong in and out
   1393 			 * because their use is transient but often.
   1394 			 * Example: exec_script.
   1395 			 */
   1396 			mi = mod->mod_info;
   1397 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
   1398 			if (error == 0 || error == ENOTTY) {
   1399 				(void)module_do_unload(mi->mi_name, false);
   1400 			} else
   1401 				module_print("module `%s' declined to be "
   1402 				    "auto-unloaded error=%d", mi->mi_name,
   1403 				    error);
   1404 		}
   1405 		kernconfig_unlock();
   1406 
   1407 		mutex_enter(&module_thread_lock);
   1408 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
   1409 		    module_thread_ticks);
   1410 		module_thread_ticks = 0;
   1411 		mutex_exit(&module_thread_lock);
   1412 	}
   1413 }
   1414 
   1415 /*
   1416  * module_thread:
   1417  *
   1418  *	Kick the module thread into action, perhaps because the
   1419  *	system is low on memory.
   1420  */
   1421 void
   1422 module_thread_kick(void)
   1423 {
   1424 
   1425 	mutex_enter(&module_thread_lock);
   1426 	module_thread_ticks = hz;
   1427 	cv_broadcast(&module_thread_cv);
   1428 	mutex_exit(&module_thread_lock);
   1429 }
   1430 
   1431 #ifdef DDB
   1432 /*
   1433  * module_whatis:
   1434  *
   1435  *	Helper routine for DDB.
   1436  */
   1437 void
   1438 module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1439 {
   1440 	module_t *mod;
   1441 	size_t msize;
   1442 	vaddr_t maddr;
   1443 
   1444 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1445 		if (mod->mod_kobj == NULL) {
   1446 			continue;
   1447 		}
   1448 		if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1449 			continue;
   1450 		if (addr < maddr || addr >= maddr + msize) {
   1451 			continue;
   1452 		}
   1453 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
   1454 		    (void *)addr, (void *)maddr,
   1455 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
   1456 	}
   1457 }
   1458 
   1459 /*
   1460  * module_print_list:
   1461  *
   1462  *	Helper routine for DDB.
   1463  */
   1464 void
   1465 module_print_list(void (*pr)(const char *, ...))
   1466 {
   1467 	const char *src;
   1468 	module_t *mod;
   1469 	size_t msize;
   1470 	vaddr_t maddr;
   1471 
   1472 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
   1473 
   1474 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1475 		switch (mod->mod_source) {
   1476 		case MODULE_SOURCE_KERNEL:
   1477 			src = "builtin";
   1478 			break;
   1479 		case MODULE_SOURCE_FILESYS:
   1480 			src = "filesys";
   1481 			break;
   1482 		case MODULE_SOURCE_BOOT:
   1483 			src = "boot";
   1484 			break;
   1485 		default:
   1486 			src = "unknown";
   1487 			break;
   1488 		}
   1489 		if (mod->mod_kobj == NULL) {
   1490 			maddr = 0;
   1491 			msize = 0;
   1492 		} else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1493 			continue;
   1494 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
   1495 		    (long)maddr, (long)msize, src);
   1496 	}
   1497 }
   1498 #endif	/* DDB */
   1499 
   1500 static bool
   1501 module_merge_dicts(prop_dictionary_t existing_dict,
   1502 		   const prop_dictionary_t new_dict)
   1503 {
   1504 	prop_dictionary_keysym_t props_keysym;
   1505 	prop_object_iterator_t props_iter;
   1506 	prop_object_t props_obj;
   1507 	const char *props_key;
   1508 	bool error;
   1509 
   1510 	if (new_dict == NULL) {			/* nothing to merge */
   1511 		return true;
   1512 	}
   1513 
   1514 	error = false;
   1515 	props_iter = prop_dictionary_iterator(new_dict);
   1516 	if (props_iter == NULL) {
   1517 		return false;
   1518 	}
   1519 
   1520 	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
   1521 		props_keysym = (prop_dictionary_keysym_t)props_obj;
   1522 		props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
   1523 		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
   1524 		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
   1525 		    props_key, props_obj)) {
   1526 			error = true;
   1527 			goto out;
   1528 		}
   1529 	}
   1530 	error = false;
   1531 
   1532 out:
   1533 	prop_object_iterator_release(props_iter);
   1534 
   1535 	return !error;
   1536 }
   1537