Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.29
      1 /*	$NetBSD: kern_module.c,v 1.29 2008/11/19 13:07:42 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.29 2008/11/19 13:07:42 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         /* Disallow path seperators and magic symlinks. */
    239         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    240             strchr(filename, '.') != NULL) {
    241         	return EPERM;
    242 	}
    243 
    244 	/* Authorize. */
    245 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    246 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    247 	if (error != 0) {
    248 		return error;
    249 	}
    250 
    251 	return module_do_load(filename, false, 0, NULL, NULL, class, true);
    252 }
    253 
    254 /*
    255  * module_unload:
    256  *
    257  *	Find and unload a module by name.
    258  */
    259 int
    260 module_unload(const char *name)
    261 {
    262 	int error;
    263 
    264 	/* Authorize. */
    265 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    266 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    267 	if (error != 0) {
    268 		return error;
    269 	}
    270 
    271 	mutex_enter(&module_lock);
    272 	error = module_do_unload(name);
    273 	mutex_exit(&module_lock);
    274 
    275 	return error;
    276 }
    277 
    278 /*
    279  * module_lookup:
    280  *
    281  *	Look up a module by name.
    282  */
    283 module_t *
    284 module_lookup(const char *name)
    285 {
    286 	module_t *mod;
    287 
    288 	KASSERT(mutex_owned(&module_lock));
    289 
    290 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    291 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    292 			break;
    293 		}
    294 	}
    295 
    296 	return mod;
    297 }
    298 
    299 /*
    300  * module_hold:
    301  *
    302  *	Add a single reference to a module.  It's the caller's
    303  *	responsibility to ensure that the reference is dropped
    304  *	later.
    305  */
    306 int
    307 module_hold(const char *name)
    308 {
    309 	module_t *mod;
    310 
    311 	mutex_enter(&module_lock);
    312 	mod = module_lookup(name);
    313 	if (mod == NULL) {
    314 		mutex_exit(&module_lock);
    315 		return ENOENT;
    316 	}
    317 	mod->mod_refcnt++;
    318 	mutex_exit(&module_lock);
    319 
    320 	return 0;
    321 }
    322 
    323 /*
    324  * module_rele:
    325  *
    326  *	Release a reference acquired with module_hold().
    327  */
    328 void
    329 module_rele(const char *name)
    330 {
    331 	module_t *mod;
    332 
    333 	mutex_enter(&module_lock);
    334 	mod = module_lookup(name);
    335 	if (mod == NULL) {
    336 		mutex_exit(&module_lock);
    337 		panic("module_rele: gone");
    338 	}
    339 	mod->mod_refcnt--;
    340 	mutex_exit(&module_lock);
    341 }
    342 
    343 /*
    344  * module_enqueue:
    345  *
    346  *	Put a module onto the global list and update counters.
    347  */
    348 static void
    349 module_enqueue(module_t *mod)
    350 {
    351 	int i;
    352 
    353 	/*
    354 	 * If there are requisite modules, put at the head of the queue.
    355 	 * This is so that autounload can unload requisite modules with
    356 	 * only one pass through the queue.
    357 	 */
    358 	if (mod->mod_nrequired) {
    359 		TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    360 
    361 		/* Add references to the requisite modules. */
    362 		for (i = 0; i < mod->mod_nrequired; i++) {
    363 			KASSERT(mod->mod_required[i] != NULL);
    364 			mod->mod_required[i]->mod_refcnt++;
    365 		}
    366 	} else {
    367 		TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
    368 	}
    369 	module_count++;
    370 	module_gen++;
    371 }
    372 
    373 /*
    374  * module_do_builtin:
    375  *
    376  *	Initialize a single module from the list of modules that are
    377  *	built into the kernel (linked into the kernel image).
    378  */
    379 static int
    380 module_do_builtin(const char *name, module_t **modp)
    381 {
    382 	__link_set_decl(modules, modinfo_t);
    383 	modinfo_t *const *mip;
    384 	const char *p, *s;
    385 	char buf[MAXMODNAME];
    386 	modinfo_t *mi;
    387 	module_t *mod, *mod2;
    388 	size_t len;
    389 	int error;
    390 
    391 	KASSERT(mutex_owned(&module_lock));
    392 
    393 	/*
    394 	 * Check to see if already loaded.
    395 	 */
    396 	if ((mod = module_lookup(name)) != NULL) {
    397 		if (modp != NULL) {
    398 			*modp = mod;
    399 		}
    400 		return 0;
    401 	}
    402 
    403 	/*
    404 	 * Search the list to see if we have a module by this name.
    405 	 */
    406 	error = ENOENT;
    407 	__link_set_foreach(mip, modules) {
    408 		mi = *mip;
    409 		if (mi == &module_dummy) {
    410 			continue;
    411 		}
    412 		if (strcmp(mi->mi_name, name) == 0) {
    413 			error = 0;
    414 			break;
    415 		}
    416 	}
    417 	if (error != 0) {
    418 		return error;
    419 	}
    420 
    421 	/*
    422 	 * Initialize pre-requisites.
    423 	 */
    424 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    425 	if (mod == NULL) {
    426 		return ENOMEM;
    427 	}
    428 	if (modp != NULL) {
    429 		*modp = mod;
    430 	}
    431 	if (mi->mi_required != NULL) {
    432 		for (s = mi->mi_required; *s != '\0'; s = p) {
    433 			if (*s == ',')
    434 				s++;
    435 			p = s;
    436 			while (*p != '\0' && *p != ',')
    437 				p++;
    438 			len = min(p - s + 1, sizeof(buf));
    439 			strlcpy(buf, s, len);
    440 			if (buf[0] == '\0')
    441 				break;
    442 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    443 				module_error("too many required modules");
    444 				kmem_free(mod, sizeof(*mod));
    445 				return EINVAL;
    446 			}
    447 			error = module_do_builtin(buf, &mod2);
    448 			if (error != 0) {
    449 				kmem_free(mod, sizeof(*mod));
    450 				return error;
    451 			}
    452 			mod->mod_required[mod->mod_nrequired++] = mod2;
    453 		}
    454 	}
    455 
    456 	/*
    457 	 * Try to initialize the module.
    458 	 */
    459 	KASSERT(module_active == NULL);
    460 	module_active = mod;
    461 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
    462 	module_active = NULL;
    463 	if (error != 0) {
    464 		module_error("builtin module `%s' "
    465 		    "failed to init", mi->mi_name);
    466 		kmem_free(mod, sizeof(*mod));
    467 		return error;
    468 	}
    469 	mod->mod_info = mi;
    470 	mod->mod_source = MODULE_SOURCE_KERNEL;
    471 	module_enqueue(mod);
    472 	return 0;
    473 }
    474 
    475 /*
    476  * module_do_load:
    477  *
    478  *	Helper routine: load a module from the file system, or one
    479  *	pushed by the boot loader.
    480  */
    481 static int
    482 module_do_load(const char *name, bool isdep, int flags,
    483 	       prop_dictionary_t props, module_t **modp, modclass_t class,
    484 	       bool autoload)
    485 {
    486 	static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
    487 	static int depth;
    488 	const int maxdepth = 6;
    489 	modinfo_t *mi;
    490 	module_t *mod, *mod2;
    491 	char buf[MAXMODNAME];
    492 	const char *s, *p;
    493 	int error;
    494 	size_t len;
    495 
    496 	KASSERT(mutex_owned(&module_lock));
    497 
    498 	error = 0;
    499 
    500 	/*
    501 	 * Avoid recursing too far.
    502 	 */
    503 	if (++depth > maxdepth) {
    504 		module_error("too many required modules");
    505 		depth--;
    506 		return EMLINK;
    507 	}
    508 
    509 	/*
    510 	 * Load the module and link.  Before going to the file system,
    511 	 * scan the list of modules loaded by the boot loader.  Just
    512 	 * before init is started the list of modules loaded at boot
    513 	 * will be purged.  Before init is started we can assume that
    514 	 * `name' is a module name and not a path name.
    515 	 */
    516 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    517 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    518 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    519 			break;
    520 		}
    521 	}
    522 	if (mod != NULL) {
    523 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    524 	} else {
    525 		/*
    526 		 * If a requisite module, check to see if it is
    527 		 * already present.
    528 		 */
    529 		if (isdep) {
    530 			TAILQ_FOREACH(mod, &module_list, mod_chain) {
    531 				if (strcmp(mod->mod_info->mi_name, name) == 0) {
    532 					break;
    533 				}
    534 			}
    535 			if (mod != NULL) {
    536 				if (modp != NULL) {
    537 					*modp = mod;
    538 				}
    539 				depth--;
    540 				return 0;
    541 			}
    542 		}
    543 		mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    544 		if (mod == NULL) {
    545 			depth--;
    546 			return ENOMEM;
    547 		}
    548 		error = kobj_load_file(&mod->mod_kobj, name, module_base,
    549 		    autoload);
    550 		if (error != 0) {
    551 			kmem_free(mod, sizeof(*mod));
    552 			depth--;
    553 			if (!autoload) {
    554 				module_error("unable to load kernel object");
    555 			}
    556 			return error;
    557 		}
    558 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    559 		mod->mod_source = MODULE_SOURCE_FILESYS;
    560 		error = module_fetch_info(mod);
    561 		if (error != 0) {
    562 			goto fail;
    563 		}
    564 	}
    565 
    566 	/*
    567 	 * Check compatibility.
    568 	 */
    569 	mi = mod->mod_info;
    570 	if (strlen(mi->mi_name) >= MAXMODNAME) {
    571 		error = EINVAL;
    572 		module_error("module name too long");
    573 		goto fail;
    574 	}
    575 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
    576 		module_error("module built for different version of system");
    577 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
    578 			module_error("forced load, system may be unstable");
    579 		} else {
    580 			error = EPROGMISMATCH;
    581 			goto fail;
    582 		}
    583 	}
    584 
    585 	/*
    586 	 * If a specific kind of module was requested, ensure that we have
    587 	 * a match.
    588 	 */
    589 	if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    590 		error = ENOENT;
    591 		goto fail;
    592 	}
    593 
    594 	/*
    595 	 * If loading a dependency, `name' is a plain module name.
    596 	 * The name must match.
    597 	 */
    598 	if (isdep && strcmp(mi->mi_name, name) != 0) {
    599 		error = ENOENT;
    600 		goto fail;
    601 	}
    602 
    603 	/*
    604 	 * Check to see if the module is already loaded.  If so, we may
    605 	 * have been recursively called to handle a dependency, so be sure
    606 	 * to set modp.
    607 	 */
    608 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
    609 		if (modp != NULL)
    610 			*modp = mod2;
    611 		error = EEXIST;
    612 		goto fail;
    613 	}
    614 
    615 	/*
    616 	 * Block circular dependencies.
    617 	 */
    618 	TAILQ_FOREACH(mod2, &pending, mod_chain) {
    619 		if (mod == mod2) {
    620 			continue;
    621 		}
    622 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
    623 		    	error = EDEADLK;
    624 			module_error("circular dependency detected");
    625 		    	goto fail;
    626 		}
    627 	}
    628 
    629 	/*
    630 	 * Now try to load any requisite modules.
    631 	 */
    632 	if (mi->mi_required != NULL) {
    633 		for (s = mi->mi_required; *s != '\0'; s = p) {
    634 			if (*s == ',')
    635 				s++;
    636 			p = s;
    637 			while (*p != '\0' && *p != ',')
    638 				p++;
    639 			len = p - s + 1;
    640 			if (len >= MAXMODNAME) {
    641 				error = EINVAL;
    642 				module_error("required module name too long");
    643 				goto fail;
    644 			}
    645 			strlcpy(buf, s, len);
    646 			if (buf[0] == '\0')
    647 				break;
    648 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    649 				error = EINVAL;
    650 				module_error("too many required modules");
    651 				goto fail;
    652 			}
    653 			if (strcmp(buf, mi->mi_name) == 0) {
    654 				error = EDEADLK;
    655 				module_error("self-dependency detected");
    656 				goto fail;
    657 			}
    658 			error = module_do_load(buf, true, flags, NULL,
    659 			    &mod->mod_required[mod->mod_nrequired++],
    660 			    MODULE_CLASS_ANY, true);
    661 			if (error != 0)
    662 				goto fail;
    663 		}
    664 	}
    665 
    666 	/*
    667 	 * We loaded all needed modules successfully: perform global
    668 	 * relocations and initialize.
    669 	 */
    670 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
    671 	if (error != 0) {
    672 		module_error("unable to affix module");
    673 		goto fail2;
    674 	}
    675 
    676 	KASSERT(module_active == NULL);
    677 	module_active = mod;
    678 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    679 	module_active = NULL;
    680 	if (error != 0) {
    681 		module_error("modctl function returned error %d", error);
    682 		goto fail;
    683 	}
    684 
    685 	/*
    686 	 * Good, the module loaded successfully.  Put it onto the
    687 	 * list and add references to its requisite modules.
    688 	 */
    689 	TAILQ_REMOVE(&pending, mod, mod_chain);
    690 	module_enqueue(mod);
    691 	if (modp != NULL) {
    692 		*modp = mod;
    693 	}
    694 	if (autoload) {
    695 		/*
    696 		 * Arrange to try unloading the module after
    697 		 * a short delay.
    698 		 */
    699 		mod->mod_autotime = time_second + module_autotime;
    700 		module_thread_kick();
    701 	}
    702 	depth--;
    703 	return 0;
    704 
    705  fail:
    706 	kobj_unload(mod->mod_kobj);
    707  fail2:
    708 	TAILQ_REMOVE(&pending, mod, mod_chain);
    709 	kmem_free(mod, sizeof(*mod));
    710 	depth--;
    711 	return error;
    712 }
    713 
    714 /*
    715  * module_do_unload:
    716  *
    717  *	Helper routine: do the dirty work of unloading a module.
    718  */
    719 static int
    720 module_do_unload(const char *name)
    721 {
    722 	module_t *mod;
    723 	int error;
    724 	u_int i;
    725 
    726 	KASSERT(mutex_owned(&module_lock));
    727 
    728 	mod = module_lookup(name);
    729 	if (mod == NULL) {
    730 		return ENOENT;
    731 	}
    732 	if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
    733 		return EBUSY;
    734 	}
    735 	KASSERT(module_active == NULL);
    736 	module_active = mod;
    737 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
    738 	module_active = NULL;
    739 	if (error != 0) {
    740 		return error;
    741 	}
    742 	module_count--;
    743 	TAILQ_REMOVE(&module_list, mod, mod_chain);
    744 	for (i = 0; i < mod->mod_nrequired; i++) {
    745 		mod->mod_required[i]->mod_refcnt--;
    746 	}
    747 	if (mod->mod_kobj != NULL) {
    748 		kobj_unload(mod->mod_kobj);
    749 	}
    750 	kmem_free(mod, sizeof(*mod));
    751 	module_gen++;
    752 
    753 	return 0;
    754 }
    755 
    756 /*
    757  * module_prime:
    758  *
    759  *	Push a module loaded by the bootloader onto our internal
    760  *	list.
    761  */
    762 int
    763 module_prime(void *base, size_t size)
    764 {
    765 	module_t *mod;
    766 	int error;
    767 
    768 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    769 	if (mod == NULL) {
    770 		return ENOMEM;
    771 	}
    772 	mod->mod_source = MODULE_SOURCE_BOOT;
    773 
    774 	error = kobj_load_mem(&mod->mod_kobj, base, size);
    775 	if (error != 0) {
    776 		kmem_free(mod, sizeof(*mod));
    777 		module_error("unable to load object pushed by boot loader");
    778 		return error;
    779 	}
    780 	error = module_fetch_info(mod);
    781 	if (error != 0) {
    782 		kobj_unload(mod->mod_kobj);
    783 		kmem_free(mod, sizeof(*mod));
    784 		module_error("unable to load object pushed by boot loader");
    785 		return error;
    786 	}
    787 
    788 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
    789 
    790 	return 0;
    791 }
    792 
    793 /*
    794  * module_fetch_into:
    795  *
    796  *	Fetch modinfo record from a loaded module.
    797  */
    798 static int
    799 module_fetch_info(module_t *mod)
    800 {
    801 	int error;
    802 	void *addr;
    803 	size_t size;
    804 
    805 	/*
    806 	 * Find module info record and check compatibility.
    807 	 */
    808 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
    809 	    &addr, &size);
    810 	if (error != 0) {
    811 		module_error("`link_set_modules' section not present");
    812 		return error;
    813 	}
    814 	if (size != sizeof(modinfo_t **)) {
    815 		module_error("`link_set_modules' section wrong size");
    816 		return error;
    817 	}
    818 	mod->mod_info = *(modinfo_t **)addr;
    819 
    820 	return 0;
    821 }
    822 
    823 /*
    824  * module_find_section:
    825  *
    826  *	Allows a module that is being initialized to look up a section
    827  *	within its ELF object.
    828  */
    829 int
    830 module_find_section(const char *name, void **addr, size_t *size)
    831 {
    832 
    833 	KASSERT(mutex_owned(&module_lock));
    834 	KASSERT(module_active != NULL);
    835 
    836 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
    837 }
    838 
    839 /*
    840  * module_thread:
    841  *
    842  *	Automatically unload modules.  We try once to unload autoloaded
    843  *	modules after module_autotime seconds.  If the system is under
    844  *	severe memory pressure, we'll try unloading all modules.
    845  */
    846 static void
    847 module_thread(void *cookie)
    848 {
    849 	module_t *mod, *next;
    850 	modinfo_t *mi;
    851 	int error;
    852 
    853 	for (;;) {
    854 		mutex_enter(&module_lock);
    855 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
    856 			next = TAILQ_NEXT(mod, mod_chain);
    857 			if (uvmexp.free < uvmexp.freemin) {
    858 				module_thread_ticks = hz;
    859 			} else if (mod->mod_autotime == 0) {
    860 				continue;
    861 			} else if (time_second < mod->mod_autotime) {
    862 				module_thread_ticks = hz;
    863 			    	continue;
    864 			} else {
    865 				mod->mod_autotime = 0;
    866 			}
    867 			/*
    868 			 * If this module wants to avoid autounload then
    869 			 * skip it.  Some modules can ping-pong in and out
    870 			 * because their use is transient but often.
    871 			 * Example: exec_script.
    872 			 */
    873 			mi = mod->mod_info;
    874 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
    875 			if (error == 0 || error == ENOTTY) {
    876 				(void)module_do_unload(mi->mi_name);
    877 			}
    878 		}
    879 		mutex_exit(&module_lock);
    880 
    881 		mutex_enter(&module_thread_lock);
    882 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
    883 		    module_thread_ticks);
    884 		module_thread_ticks = 0;
    885 		mutex_exit(&module_thread_lock);
    886 	}
    887 }
    888 
    889 /*
    890  * module_thread:
    891  *
    892  *	Kick the module thread into action, perhaps because the
    893  *	system is low on memory.
    894  */
    895 void
    896 module_thread_kick(void)
    897 {
    898 
    899 	mutex_enter(&module_thread_lock);
    900 	module_thread_ticks = hz;
    901 	cv_broadcast(&module_thread_cv);
    902 	mutex_exit(&module_thread_lock);
    903 }
    904