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