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