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