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