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