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