Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.39
      1 /*	$NetBSD: kern_module.c,v 1.39 2009/01/20 20:12:41 drochner 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.39 2009/01/20 20:12:41 drochner Exp $");
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_ddb.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/proc.h>
     48 #include <sys/kauth.h>
     49 #include <sys/kobj.h>
     50 #include <sys/kmem.h>
     51 #include <sys/module.h>
     52 #include <sys/kauth.h>
     53 #include <sys/kthread.h>
     54 #include <sys/sysctl.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <machine/stdarg.h>
     59 
     60 struct vm_map *module_map;
     61 
     62 struct modlist	module_list = TAILQ_HEAD_INITIALIZER(module_list);
     63 struct modlist	module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     64 static module_t	*module_active;
     65 static char	module_base[64];
     66 static int	module_verbose_on;
     67 static int	module_autoload_on = 1;
     68 u_int		module_count;
     69 kmutex_t	module_lock;
     70 u_int		module_autotime = 10;
     71 u_int		module_gen = 1;
     72 static kcondvar_t module_thread_cv;
     73 static kmutex_t module_thread_lock;
     74 static int	module_thread_ticks;
     75 
     76 /* Ensure that the kernel's link set isn't empty. */
     77 static modinfo_t module_dummy;
     78 __link_set_add_rodata(modules, module_dummy);
     79 
     80 static module_t	*module_lookup(const char *);
     81 static int	module_do_load(const char *, bool, int, prop_dictionary_t,
     82 		    module_t **, modclass_t class, bool);
     83 static int	module_do_unload(const char *);
     84 static void	module_error(const char *, ...)
     85 			__attribute__((__format__(__printf__,1,2)));
     86 static void	module_print(const char *, ...)
     87 			__attribute__((__format__(__printf__,1,2)));
     88 static int	module_do_builtin(const char *, module_t **);
     89 static int	module_fetch_info(module_t *);
     90 static void	module_thread(void *);
     91 
     92 /*
     93  * module_error:
     94  *
     95  *	Utility function: log an error.
     96  */
     97 static void
     98 module_error(const char *fmt, ...)
     99 {
    100 	va_list ap;
    101 
    102 	va_start(ap, fmt);
    103 	printf("WARNING: module error: ");
    104 	vprintf(fmt, ap);
    105 	printf("\n");
    106 	va_end(ap);
    107 }
    108 
    109 /*
    110  * module_print:
    111  *
    112  *	Utility function: log verbose output.
    113  */
    114 static void
    115 module_print(const char *fmt, ...)
    116 {
    117 	va_list ap;
    118 
    119 	if (module_verbose_on) {
    120 		va_start(ap, fmt);
    121 		printf("DEBUG: module: ");
    122 		vprintf(fmt, ap);
    123 		printf("\n");
    124 		va_end(ap);
    125 	}
    126 }
    127 
    128 /*
    129  * module_init:
    130  *
    131  *	Initialize the module subsystem.
    132  */
    133 void
    134 module_init(void)
    135 {
    136 	extern struct vm_map *module_map;
    137 	int error;
    138 
    139 	if (module_map == NULL) {
    140 		module_map = kernel_map;
    141 	}
    142 	mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
    143 	cv_init(&module_thread_cv, "modunload");
    144 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
    145 #ifdef MODULAR	/* XXX */
    146 	module_init_md();
    147 #endif
    148 
    149 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    150 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
    151 	    machine, osrelease);
    152 #else						/* release */
    153 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
    154 	    machine, __NetBSD_Version__ / 100000000,
    155 	    __NetBSD_Version__ / 1000000 % 100);
    156 #endif
    157 
    158 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
    159 	    NULL, NULL, "modunload");
    160 	if (error != 0)
    161 		panic("module_init: %d", error);
    162 }
    163 
    164 SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup")
    165 {
    166 	const struct sysctlnode *node = NULL;
    167 
    168 	sysctl_createv(clog, 0, NULL, NULL,
    169 		CTLFLAG_PERMANENT,
    170 		CTLTYPE_NODE, "kern", NULL,
    171 		NULL, 0, NULL, 0,
    172 		CTL_KERN, CTL_EOL);
    173 	sysctl_createv(clog, 0, NULL, &node,
    174 		CTLFLAG_PERMANENT,
    175 		CTLTYPE_NODE, "module",
    176 		SYSCTL_DESCR("Module options"),
    177 		NULL, 0, NULL, 0,
    178 		CTL_KERN, CTL_CREATE, CTL_EOL);
    179 
    180 	if (node == NULL)
    181 		return;
    182 
    183 	sysctl_createv(clog, 0, &node, NULL,
    184 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    185 		CTLTYPE_INT, "autoload",
    186 		SYSCTL_DESCR("Enable automatic load of modules"),
    187 		NULL, 0, &module_autoload_on, 0,
    188 		CTL_CREATE, CTL_EOL);
    189 	sysctl_createv(clog, 0, &node, NULL,
    190 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    191 		CTLTYPE_INT, "verbose",
    192 		SYSCTL_DESCR("Enable verbose output"),
    193 		NULL, 0, &module_verbose_on, 0,
    194 		CTL_CREATE, CTL_EOL);
    195 }
    196 
    197 /*
    198  * module_init_class:
    199  *
    200  *	Initialize all built-in and pre-loaded modules of the
    201  *	specified class.
    202  */
    203 void
    204 module_init_class(modclass_t class)
    205 {
    206 	__link_set_decl(modules, modinfo_t);
    207 	modinfo_t *const *mip, *mi;
    208 	module_t *mod;
    209 
    210 	mutex_enter(&module_lock);
    211 	/*
    212 	 * Builtins first.  These can't depend on pre-loaded modules.
    213 	 */
    214 	__link_set_foreach(mip, modules) {
    215 		mi = *mip;
    216 		if (mi == &module_dummy) {
    217 			continue;
    218 		}
    219 		if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    220 			continue;
    221 		}
    222 		(void)module_do_builtin(mi->mi_name, NULL);
    223 	}
    224 	/*
    225 	 * Now preloaded modules.  These will be pulled off the
    226 	 * list as we call module_do_load();
    227 	 */
    228 	do {
    229 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    230 			mi = mod->mod_info;
    231 			if (class != MODULE_CLASS_ANY &&
    232 			    class != mi->mi_class)
    233 				continue;
    234 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
    235 			    class, false);
    236 			break;
    237 		}
    238 	} while (mod != NULL);
    239 	mutex_exit(&module_lock);
    240 }
    241 
    242 /*
    243  * module_compatible:
    244  *
    245  *	Return true if the two supplied kernel versions are said to
    246  *	have the same binary interface for kernel code.  The entire
    247  *	version is signficant for the development tree (-current),
    248  *	major and minor versions are significant for official
    249  *	releases of the system.
    250  */
    251 bool
    252 module_compatible(int v1, int v2)
    253 {
    254 
    255 #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    256 	return v1 == v2;
    257 #else						/* release */
    258 	return abs(v1 - v2) < 10000;
    259 #endif
    260 }
    261 
    262 /*
    263  * module_load:
    264  *
    265  *	Load a single module from the file system.
    266  */
    267 int
    268 module_load(const char *filename, int flags, prop_dictionary_t props,
    269 	    modclass_t class)
    270 {
    271 	int error;
    272 
    273 	/* Authorize. */
    274 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    275 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
    276 	if (error != 0) {
    277 		return error;
    278 	}
    279 
    280 	mutex_enter(&module_lock);
    281 	error = module_do_load(filename, false, flags, props, NULL, class,
    282 	    false);
    283 	mutex_exit(&module_lock);
    284 
    285 	return error;
    286 }
    287 
    288 /*
    289  * module_autoload:
    290  *
    291  *	Load a single module from the file system, system initiated.
    292  */
    293 int
    294 module_autoload(const char *filename, modclass_t class)
    295 {
    296 	int error;
    297 
    298 	KASSERT(mutex_owned(&module_lock));
    299 
    300 	/* Nothing if the user has disabled it. */
    301 	if (!module_autoload_on) {
    302 		return EPERM;
    303 	}
    304 
    305         /* Disallow path seperators and magic symlinks. */
    306         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    307             strchr(filename, '.') != NULL) {
    308         	return EPERM;
    309 	}
    310 
    311 	/* Authorize. */
    312 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    313 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    314 	if (error != 0) {
    315 		return error;
    316 	}
    317 
    318 	return module_do_load(filename, false, 0, NULL, NULL, class, true);
    319 }
    320 
    321 /*
    322  * module_unload:
    323  *
    324  *	Find and unload a module by name.
    325  */
    326 int
    327 module_unload(const char *name)
    328 {
    329 	int error;
    330 
    331 	/* Authorize. */
    332 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    333 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    334 	if (error != 0) {
    335 		return error;
    336 	}
    337 
    338 	mutex_enter(&module_lock);
    339 	error = module_do_unload(name);
    340 	mutex_exit(&module_lock);
    341 
    342 	return error;
    343 }
    344 
    345 /*
    346  * module_lookup:
    347  *
    348  *	Look up a module by name.
    349  */
    350 module_t *
    351 module_lookup(const char *name)
    352 {
    353 	module_t *mod;
    354 
    355 	KASSERT(mutex_owned(&module_lock));
    356 
    357 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    358 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    359 			break;
    360 		}
    361 	}
    362 
    363 	return mod;
    364 }
    365 
    366 /*
    367  * module_hold:
    368  *
    369  *	Add a single reference to a module.  It's the caller's
    370  *	responsibility to ensure that the reference is dropped
    371  *	later.
    372  */
    373 int
    374 module_hold(const char *name)
    375 {
    376 	module_t *mod;
    377 
    378 	mutex_enter(&module_lock);
    379 	mod = module_lookup(name);
    380 	if (mod == NULL) {
    381 		mutex_exit(&module_lock);
    382 		return ENOENT;
    383 	}
    384 	mod->mod_refcnt++;
    385 	mutex_exit(&module_lock);
    386 
    387 	return 0;
    388 }
    389 
    390 /*
    391  * module_rele:
    392  *
    393  *	Release a reference acquired with module_hold().
    394  */
    395 void
    396 module_rele(const char *name)
    397 {
    398 	module_t *mod;
    399 
    400 	mutex_enter(&module_lock);
    401 	mod = module_lookup(name);
    402 	if (mod == NULL) {
    403 		mutex_exit(&module_lock);
    404 		panic("module_rele: gone");
    405 	}
    406 	mod->mod_refcnt--;
    407 	mutex_exit(&module_lock);
    408 }
    409 
    410 /*
    411  * module_enqueue:
    412  *
    413  *	Put a module onto the global list and update counters.
    414  */
    415 static void
    416 module_enqueue(module_t *mod)
    417 {
    418 	int i;
    419 
    420 	/*
    421 	 * If there are requisite modules, put at the head of the queue.
    422 	 * This is so that autounload can unload requisite modules with
    423 	 * only one pass through the queue.
    424 	 */
    425 	if (mod->mod_nrequired) {
    426 		TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    427 
    428 		/* Add references to the requisite modules. */
    429 		for (i = 0; i < mod->mod_nrequired; i++) {
    430 			KASSERT(mod->mod_required[i] != NULL);
    431 			mod->mod_required[i]->mod_refcnt++;
    432 		}
    433 	} else {
    434 		TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
    435 	}
    436 	module_count++;
    437 	module_gen++;
    438 }
    439 
    440 /*
    441  * module_do_builtin:
    442  *
    443  *	Initialize a single module from the list of modules that are
    444  *	built into the kernel (linked into the kernel image).
    445  */
    446 static int
    447 module_do_builtin(const char *name, module_t **modp)
    448 {
    449 	__link_set_decl(modules, modinfo_t);
    450 	modinfo_t *const *mip;
    451 	const char *p, *s;
    452 	char buf[MAXMODNAME];
    453 	modinfo_t *mi;
    454 	module_t *mod, *mod2;
    455 	size_t len;
    456 	int error;
    457 
    458 	KASSERT(mutex_owned(&module_lock));
    459 
    460 	/*
    461 	 * Check to see if already loaded.
    462 	 */
    463 	if ((mod = module_lookup(name)) != NULL) {
    464 		if (modp != NULL) {
    465 			*modp = mod;
    466 		}
    467 		return 0;
    468 	}
    469 
    470 	/*
    471 	 * Search the list to see if we have a module by this name.
    472 	 */
    473 	error = ENOENT;
    474 	__link_set_foreach(mip, modules) {
    475 		mi = *mip;
    476 		if (mi == &module_dummy) {
    477 			continue;
    478 		}
    479 		if (strcmp(mi->mi_name, name) == 0) {
    480 			error = 0;
    481 			break;
    482 		}
    483 	}
    484 	if (error != 0) {
    485 		module_error("can't find `%s'", name);
    486 		return error;
    487 	}
    488 
    489 	/*
    490 	 * Initialize pre-requisites.
    491 	 */
    492 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    493 	if (mod == NULL) {
    494 		module_error("out of memory for `%s'", name);
    495 		return ENOMEM;
    496 	}
    497 	if (modp != NULL) {
    498 		*modp = mod;
    499 	}
    500 	if (mi->mi_required != NULL) {
    501 		for (s = mi->mi_required; *s != '\0'; s = p) {
    502 			if (*s == ',')
    503 				s++;
    504 			p = s;
    505 			while (*p != '\0' && *p != ',')
    506 				p++;
    507 			len = min(p - s + 1, sizeof(buf));
    508 			strlcpy(buf, s, len);
    509 			if (buf[0] == '\0')
    510 				break;
    511 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    512 				module_error("too many required modules");
    513 				kmem_free(mod, sizeof(*mod));
    514 				return EINVAL;
    515 			}
    516 			error = module_do_builtin(buf, &mod2);
    517 			if (error != 0) {
    518 				kmem_free(mod, sizeof(*mod));
    519 				return error;
    520 			}
    521 			mod->mod_required[mod->mod_nrequired++] = mod2;
    522 		}
    523 	}
    524 
    525 	/*
    526 	 * Try to initialize the module.
    527 	 */
    528 	KASSERT(module_active == NULL);
    529 	module_active = mod;
    530 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
    531 	module_active = NULL;
    532 	if (error != 0) {
    533 		module_error("builtin module `%s' "
    534 		    "failed to init", mi->mi_name);
    535 		kmem_free(mod, sizeof(*mod));
    536 		return error;
    537 	}
    538 	mod->mod_info = mi;
    539 	mod->mod_source = MODULE_SOURCE_KERNEL;
    540 	module_enqueue(mod);
    541 	return 0;
    542 }
    543 
    544 /*
    545  * module_do_load:
    546  *
    547  *	Helper routine: load a module from the file system, or one
    548  *	pushed by the boot loader.
    549  */
    550 static int
    551 module_do_load(const char *name, bool isdep, int flags,
    552 	       prop_dictionary_t props, module_t **modp, modclass_t class,
    553 	       bool autoload)
    554 {
    555 	static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
    556 	static int depth;
    557 	const int maxdepth = 6;
    558 	modinfo_t *mi;
    559 	module_t *mod, *mod2;
    560 	char buf[MAXMODNAME];
    561 	const char *s, *p;
    562 	int error;
    563 	size_t len;
    564 
    565 	KASSERT(mutex_owned(&module_lock));
    566 
    567 	error = 0;
    568 
    569 	/*
    570 	 * Avoid recursing too far.
    571 	 */
    572 	if (++depth > maxdepth) {
    573 		module_error("too many required modules");
    574 		depth--;
    575 		return EMLINK;
    576 	}
    577 
    578 	/*
    579 	 * Load the module and link.  Before going to the file system,
    580 	 * scan the list of modules loaded by the boot loader.  Just
    581 	 * before init is started the list of modules loaded at boot
    582 	 * will be purged.  Before init is started we can assume that
    583 	 * `name' is a module name and not a path name.
    584 	 */
    585 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    586 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    587 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    588 			break;
    589 		}
    590 	}
    591 	if (mod != NULL) {
    592 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    593 	} else {
    594 		/*
    595 		 * If a requisite module, check to see if it is
    596 		 * already present.
    597 		 */
    598 		if (isdep) {
    599 			TAILQ_FOREACH(mod, &module_list, mod_chain) {
    600 				if (strcmp(mod->mod_info->mi_name, name) == 0) {
    601 					break;
    602 				}
    603 			}
    604 			if (mod != NULL) {
    605 				if (modp != NULL) {
    606 					*modp = mod;
    607 				}
    608 				depth--;
    609 				return 0;
    610 			}
    611 		}
    612 		mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    613 		if (mod == NULL) {
    614 			module_error("out of memory for `%s'", name);
    615 			depth--;
    616 			return ENOMEM;
    617 		}
    618 		error = kobj_load_file(&mod->mod_kobj, name, module_base,
    619 		    autoload);
    620 		if (error != 0) {
    621 			kmem_free(mod, sizeof(*mod));
    622 			depth--;
    623 			if (autoload) {
    624 				module_print("Cannot load kernel object `%s'"
    625 				    " error=%d", name, error);
    626 			} else {
    627 				module_error("Cannot load kernel object `%s'"
    628 				    " error=%d", name, error);
    629 			}
    630 			return error;
    631 		}
    632 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    633 		mod->mod_source = MODULE_SOURCE_FILESYS;
    634 		error = module_fetch_info(mod);
    635 		if (error != 0) {
    636 			module_error("cannot fetch module info for `%s'",
    637 			    name);
    638 			goto fail;
    639 		}
    640 	}
    641 
    642 	/*
    643 	 * Check compatibility.
    644 	 */
    645 	mi = mod->mod_info;
    646 	if (strlen(mi->mi_name) >= MAXMODNAME) {
    647 		error = EINVAL;
    648 		module_error("module name `%s' too long", mi->mi_name);
    649 		goto fail;
    650 	}
    651 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
    652 		module_error("module built for `%d', system `%d'",
    653 		    mi->mi_version, __NetBSD_Version__);
    654 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
    655 			module_error("forced load, system may be unstable");
    656 		} else {
    657 			error = EPROGMISMATCH;
    658 			goto fail;
    659 		}
    660 	}
    661 
    662 	/*
    663 	 * If a specific kind of module was requested, ensure that we have
    664 	 * a match.
    665 	 */
    666 	if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    667 		module_print("incompatible module class for `%s' (%d != %d)",
    668 		    name, class, mi->mi_class);
    669 		error = ENOENT;
    670 		goto fail;
    671 	}
    672 
    673 	/*
    674 	 * If loading a dependency, `name' is a plain module name.
    675 	 * The name must match.
    676 	 */
    677 	if (isdep && strcmp(mi->mi_name, name) != 0) {
    678 		module_error("dependency name mismatch (`%s' != `%s')",
    679 		    name, mi->mi_name);
    680 		error = ENOENT;
    681 		goto fail;
    682 	}
    683 
    684 	/*
    685 	 * Check to see if the module is already loaded.  If so, we may
    686 	 * have been recursively called to handle a dependency, so be sure
    687 	 * to set modp.
    688 	 */
    689 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
    690 		if (modp != NULL)
    691 			*modp = mod2;
    692 		module_print("module `%s' already loaded", mi->mi_name);
    693 		error = EEXIST;
    694 		goto fail;
    695 	}
    696 
    697 	/*
    698 	 * Block circular dependencies.
    699 	 */
    700 	TAILQ_FOREACH(mod2, &pending, mod_chain) {
    701 		if (mod == mod2) {
    702 			continue;
    703 		}
    704 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
    705 		    	error = EDEADLK;
    706 			module_error("circular dependency detected for `%s'",
    707 			    mi->mi_name);
    708 		    	goto fail;
    709 		}
    710 	}
    711 
    712 	/*
    713 	 * Now try to load any requisite modules.
    714 	 */
    715 	if (mi->mi_required != NULL) {
    716 		for (s = mi->mi_required; *s != '\0'; s = p) {
    717 			if (*s == ',')
    718 				s++;
    719 			p = s;
    720 			while (*p != '\0' && *p != ',')
    721 				p++;
    722 			len = p - s + 1;
    723 			if (len >= MAXMODNAME) {
    724 				error = EINVAL;
    725 				module_error("required module name `%s'"
    726 				    " too long", mi->mi_required);
    727 				goto fail;
    728 			}
    729 			strlcpy(buf, s, len);
    730 			if (buf[0] == '\0')
    731 				break;
    732 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    733 				error = EINVAL;
    734 				module_error("too many required modules (%d)",
    735 				    mod->mod_nrequired);
    736 				goto fail;
    737 			}
    738 			if (strcmp(buf, mi->mi_name) == 0) {
    739 				error = EDEADLK;
    740 				module_error("self-dependency detected for "
    741 				   "`%s'", mi->mi_name);
    742 				goto fail;
    743 			}
    744 			error = module_do_load(buf, true, flags, NULL,
    745 			    &mod->mod_required[mod->mod_nrequired++],
    746 			    MODULE_CLASS_ANY, true);
    747 			if (error != 0)
    748 				goto fail;
    749 		}
    750 	}
    751 
    752 	/*
    753 	 * We loaded all needed modules successfully: perform global
    754 	 * relocations and initialize.
    755 	 */
    756 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
    757 	if (error != 0) {
    758 		/* Cannot touch 'mi' as the module is now gone. */
    759 		module_error("unable to affix module `%s'", name);
    760 		goto fail2;
    761 	}
    762 
    763 	KASSERT(module_active == NULL);
    764 	module_active = mod;
    765 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    766 	module_active = NULL;
    767 	if (error != 0) {
    768 		module_error("modcmd function returned error %d for `%s'",
    769 		    error, mi->mi_name);
    770 		goto fail;
    771 	}
    772 
    773 	/*
    774 	 * Good, the module loaded successfully.  Put it onto the
    775 	 * list and add references to its requisite modules.
    776 	 */
    777 	TAILQ_REMOVE(&pending, mod, mod_chain);
    778 	module_enqueue(mod);
    779 	if (modp != NULL) {
    780 		*modp = mod;
    781 	}
    782 	if (autoload) {
    783 		/*
    784 		 * Arrange to try unloading the module after
    785 		 * a short delay.
    786 		 */
    787 		mod->mod_autotime = time_second + module_autotime;
    788 		module_thread_kick();
    789 	}
    790 	depth--;
    791 	return 0;
    792 
    793  fail:
    794 	kobj_unload(mod->mod_kobj);
    795  fail2:
    796 	TAILQ_REMOVE(&pending, mod, mod_chain);
    797 	kmem_free(mod, sizeof(*mod));
    798 	depth--;
    799 	return error;
    800 }
    801 
    802 /*
    803  * module_do_unload:
    804  *
    805  *	Helper routine: do the dirty work of unloading a module.
    806  */
    807 static int
    808 module_do_unload(const char *name)
    809 {
    810 	module_t *mod;
    811 	int error;
    812 	u_int i;
    813 
    814 	KASSERT(mutex_owned(&module_lock));
    815 
    816 	mod = module_lookup(name);
    817 	if (mod == NULL) {
    818 		module_error("module `%s' not found", name);
    819 		return ENOENT;
    820 	}
    821 	if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
    822 		module_print("module `%s' busy", name);
    823 		return EBUSY;
    824 	}
    825 	KASSERT(module_active == NULL);
    826 	module_active = mod;
    827 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
    828 	module_active = NULL;
    829 	if (error != 0) {
    830 		module_print("cannot unload module `%s' error=%d", name,
    831 		    error);
    832 		return error;
    833 	}
    834 	module_count--;
    835 	TAILQ_REMOVE(&module_list, mod, mod_chain);
    836 	for (i = 0; i < mod->mod_nrequired; i++) {
    837 		mod->mod_required[i]->mod_refcnt--;
    838 	}
    839 	if (mod->mod_kobj != NULL) {
    840 		kobj_unload(mod->mod_kobj);
    841 	}
    842 	kmem_free(mod, sizeof(*mod));
    843 	module_gen++;
    844 
    845 	return 0;
    846 }
    847 
    848 /*
    849  * module_prime:
    850  *
    851  *	Push a module loaded by the bootloader onto our internal
    852  *	list.
    853  */
    854 int
    855 module_prime(void *base, size_t size)
    856 {
    857 	module_t *mod;
    858 	int error;
    859 
    860 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    861 	if (mod == NULL) {
    862 		return ENOMEM;
    863 	}
    864 	mod->mod_source = MODULE_SOURCE_BOOT;
    865 
    866 	error = kobj_load_mem(&mod->mod_kobj, base, size);
    867 	if (error != 0) {
    868 		kmem_free(mod, sizeof(*mod));
    869 		module_error("unable to load object pushed by boot loader");
    870 		return error;
    871 	}
    872 	error = module_fetch_info(mod);
    873 	if (error != 0) {
    874 		kobj_unload(mod->mod_kobj);
    875 		kmem_free(mod, sizeof(*mod));
    876 		module_error("unable to load object pushed by boot loader");
    877 		return error;
    878 	}
    879 
    880 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
    881 
    882 	return 0;
    883 }
    884 
    885 /*
    886  * module_fetch_into:
    887  *
    888  *	Fetch modinfo record from a loaded module.
    889  */
    890 static int
    891 module_fetch_info(module_t *mod)
    892 {
    893 	int error;
    894 	void *addr;
    895 	size_t size;
    896 
    897 	/*
    898 	 * Find module info record and check compatibility.
    899 	 */
    900 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
    901 	    &addr, &size);
    902 	if (error != 0) {
    903 		module_error("`link_set_modules' section not present");
    904 		return error;
    905 	}
    906 	if (size != sizeof(modinfo_t **)) {
    907 		module_error("`link_set_modules' section wrong size");
    908 		return error;
    909 	}
    910 	mod->mod_info = *(modinfo_t **)addr;
    911 
    912 	return 0;
    913 }
    914 
    915 /*
    916  * module_find_section:
    917  *
    918  *	Allows a module that is being initialized to look up a section
    919  *	within its ELF object.
    920  */
    921 int
    922 module_find_section(const char *name, void **addr, size_t *size)
    923 {
    924 
    925 	KASSERT(mutex_owned(&module_lock));
    926 	KASSERT(module_active != NULL);
    927 
    928 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
    929 }
    930 
    931 /*
    932  * module_thread:
    933  *
    934  *	Automatically unload modules.  We try once to unload autoloaded
    935  *	modules after module_autotime seconds.  If the system is under
    936  *	severe memory pressure, we'll try unloading all modules.
    937  */
    938 static void
    939 module_thread(void *cookie)
    940 {
    941 	module_t *mod, *next;
    942 	modinfo_t *mi;
    943 	int error;
    944 
    945 	for (;;) {
    946 		mutex_enter(&module_lock);
    947 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
    948 			next = TAILQ_NEXT(mod, mod_chain);
    949 			if (uvmexp.free < uvmexp.freemin) {
    950 				module_thread_ticks = hz;
    951 			} else if (mod->mod_autotime == 0) {
    952 				continue;
    953 			} else if (time_second < mod->mod_autotime) {
    954 				module_thread_ticks = hz;
    955 			    	continue;
    956 			} else {
    957 				mod->mod_autotime = 0;
    958 			}
    959 			/*
    960 			 * If this module wants to avoid autounload then
    961 			 * skip it.  Some modules can ping-pong in and out
    962 			 * because their use is transient but often.
    963 			 * Example: exec_script.
    964 			 */
    965 			mi = mod->mod_info;
    966 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
    967 			if (error == 0 || error == ENOTTY) {
    968 				(void)module_do_unload(mi->mi_name);
    969 			}
    970 		}
    971 		mutex_exit(&module_lock);
    972 
    973 		mutex_enter(&module_thread_lock);
    974 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
    975 		    module_thread_ticks);
    976 		module_thread_ticks = 0;
    977 		mutex_exit(&module_thread_lock);
    978 	}
    979 }
    980 
    981 /*
    982  * module_thread:
    983  *
    984  *	Kick the module thread into action, perhaps because the
    985  *	system is low on memory.
    986  */
    987 void
    988 module_thread_kick(void)
    989 {
    990 
    991 	mutex_enter(&module_thread_lock);
    992 	module_thread_ticks = hz;
    993 	cv_broadcast(&module_thread_cv);
    994 	mutex_exit(&module_thread_lock);
    995 }
    996 
    997 #ifdef DDB
    998 /*
    999  * module_whatis:
   1000  *
   1001  *	Helper routine for DDB.
   1002  */
   1003 void
   1004 module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1005 {
   1006 	module_t *mod;
   1007 	size_t msize;
   1008 	vaddr_t maddr;
   1009 
   1010 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1011 		kobj_stat(mod->mod_kobj, &maddr, &msize);
   1012 		if (addr < maddr || addr >= maddr + msize) {
   1013 			continue;
   1014 		}
   1015 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
   1016 		    (void *)addr, (void *)maddr,
   1017 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
   1018 	}
   1019 }
   1020 
   1021 /*
   1022  * module_print_list:
   1023  *
   1024  *	Helper routine for DDB.
   1025  */
   1026 void
   1027 module_print_list(void (*pr)(const char *, ...))
   1028 {
   1029 	const char *src;
   1030 	module_t *mod;
   1031 	size_t msize;
   1032 	vaddr_t maddr;
   1033 
   1034 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
   1035 
   1036 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1037 		switch (mod->mod_source) {
   1038 		case MODULE_SOURCE_KERNEL:
   1039 			src = "builtin";
   1040 			break;
   1041 		case MODULE_SOURCE_FILESYS:
   1042 			src = "filesys";
   1043 			break;
   1044 		case MODULE_SOURCE_BOOT:
   1045 			src = "boot";
   1046 			break;
   1047 		default:
   1048 			src = "unknown";
   1049 			break;
   1050 		}
   1051 		kobj_stat(mod->mod_kobj, &maddr, &msize);
   1052 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
   1053 		    (long)maddr, (long)msize, src);
   1054 	}
   1055 }
   1056 #endif	/* DDB */
   1057