Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.15
      1 /*	$NetBSD: kern_module.c,v 1.15 2008/05/20 13:34:44 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Kernel module support.
     31  *
     32  * XXX Deps for loadable modules don't work, because we must load the
     33  * module in order to find out which modules it requires.  Linking may
     34  * fail because of missing symbols.
     35  */
     36 
     37 #include "opt_modular.h"
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.15 2008/05/20 13:34:44 ad Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/fcntl.h>
     45 #include <sys/proc.h>
     46 #include <sys/kauth.h>
     47 #include <sys/kobj.h>
     48 #include <sys/kmem.h>
     49 #include <sys/module.h>
     50 #include <sys/syscall.h>
     51 #include <sys/syscallargs.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <machine/stdarg.h>
     56 
     57 #ifndef LKM	/* XXX */
     58 struct vm_map *lkm_map;
     59 #endif
     60 
     61 struct modlist	module_list = TAILQ_HEAD_INITIALIZER(module_list);
     62 struct modlist	module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     63 static module_t	*module_active;
     64 u_int		module_count;
     65 kmutex_t	module_lock;
     66 
     67 /* Ensure that the kernel's link set isn't empty. */
     68 static modinfo_t module_dummy;
     69 __link_set_add_rodata(modules, module_dummy);
     70 
     71 static module_t	*module_lookup(const char *);
     72 static int	module_do_load(const char *, bool, int, prop_dictionary_t,
     73 		    module_t **);
     74 static int	module_do_unload(const char *);
     75 static void	module_error(const char *, ...);
     76 static int	module_do_builtin(const char *, module_t **);
     77 static int	module_fetch_info(module_t *);
     78 
     79 /*
     80  * module_error:
     81  *
     82  *	Utility function: log an error.
     83  */
     84 static void
     85 module_error(const char *fmt, ...)
     86 {
     87 	va_list ap;
     88 
     89 	va_start(ap, fmt);
     90 	printf("WARNING: module error: ");
     91 	vprintf(fmt, ap);
     92 	printf("\n");
     93 	va_end(ap);
     94 }
     95 
     96 /*
     97  * module_init:
     98  *
     99  *	Initialize the module subsystem.
    100  */
    101 void
    102 module_init(void)
    103 {
    104 	extern struct vm_map *lkm_map;
    105 
    106 	if (lkm_map == NULL)
    107 		lkm_map = kernel_map;
    108 	mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
    109 #ifdef MODULAR	/* XXX */
    110 	module_init_md();
    111 #endif
    112 }
    113 
    114 /*
    115  * module_init_class:
    116  *
    117  *	Initialize all built-in and pre-loaded modules of the
    118  *	specified class.
    119  */
    120 void
    121 module_init_class(modclass_t class)
    122 {
    123 	__link_set_decl(modules, modinfo_t);
    124 	modinfo_t *const *mip, *mi;
    125 	module_t *mod;
    126 
    127 	mutex_enter(&module_lock);
    128 	/*
    129 	 * Builtins first.  These can't depend on pre-loaded modules.
    130 	 */
    131 	__link_set_foreach(mip, modules) {
    132 		mi = *mip;
    133 		if (mi == &module_dummy) {
    134 			continue;
    135 		}
    136 		if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    137 			continue;
    138 		}
    139 		(void)module_do_builtin(mi->mi_name, NULL);
    140 	}
    141 	/*
    142 	 * Now preloaded modules.  These will be pulled off the
    143 	 * list as we call module_do_load();
    144 	 */
    145 	do {
    146 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    147 			mi = mod->mod_info;
    148 			if (class != MODULE_CLASS_ANY &&
    149 			    class != mi->mi_class)
    150 				continue;
    151 			module_do_load(mi->mi_name, false, 0, NULL, NULL);
    152 			break;
    153 		}
    154 	} while (mod != NULL);
    155 	mutex_exit(&module_lock);
    156 }
    157 
    158 /*
    159  * module_jettison:
    160  *
    161  *	Return memory used by pre-loaded modules to the freelist.
    162  */
    163 void
    164 module_jettison(void)
    165 {
    166 
    167 	/* XXX nothing yet */
    168 }
    169 
    170 /*
    171  * module_load:
    172  *
    173  *	Load a single module from the file system.
    174  */
    175 int
    176 module_load(const char *filename, int flags, prop_dictionary_t props)
    177 {
    178 	int error;
    179 
    180 	mutex_enter(&module_lock);
    181 	error = module_do_load(filename, false, flags, props, NULL);
    182 	mutex_exit(&module_lock);
    183 
    184 	return error;
    185 }
    186 
    187 /*
    188  * module_unload:
    189  *
    190  *	Find and unload a module by name.
    191  */
    192 int
    193 module_unload(const char *name)
    194 {
    195 	int error;
    196 
    197 	mutex_enter(&module_lock);
    198 	error = module_do_unload(name);
    199 	mutex_exit(&module_lock);
    200 
    201 	return error;
    202 }
    203 
    204 /*
    205  * module_lookup:
    206  *
    207  *	Look up a module by name.
    208  */
    209 module_t *
    210 module_lookup(const char *name)
    211 {
    212 	module_t *mod;
    213 
    214 	KASSERT(mutex_owned(&module_lock));
    215 
    216 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    217 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    218 			break;
    219 		}
    220 	}
    221 
    222 	return mod;
    223 }
    224 
    225 /*
    226  * module_hold:
    227  *
    228  *	Add a single reference to a module.  It's the caller's
    229  *	responsibility to ensure that the reference is dropped
    230  *	later.
    231  */
    232 int
    233 module_hold(const char *name)
    234 {
    235 	module_t *mod;
    236 
    237 	mutex_enter(&module_lock);
    238 	mod = module_lookup(name);
    239 	if (mod == NULL) {
    240 		mutex_exit(&module_lock);
    241 		return ENOENT;
    242 	}
    243 	mod->mod_refcnt++;
    244 	mutex_exit(&module_lock);
    245 
    246 	return 0;
    247 }
    248 
    249 /*
    250  * module_rele:
    251  *
    252  *	Release a reference acquired with module_hold().
    253  */
    254 void
    255 module_rele(const char *name)
    256 {
    257 	module_t *mod;
    258 
    259 	mutex_enter(&module_lock);
    260 	mod = module_lookup(name);
    261 	if (mod == NULL) {
    262 		mutex_exit(&module_lock);
    263 		panic("module_rele: gone");
    264 	}
    265 	mod->mod_refcnt--;
    266 	mutex_exit(&module_lock);
    267 }
    268 
    269 /*
    270  * module_do_builtin:
    271  *
    272  *	Initialize a single module from the list of modules that are
    273  *	built into the kernel (linked into the kernel image).
    274  */
    275 static int
    276 module_do_builtin(const char *name, module_t **modp)
    277 {
    278 	__link_set_decl(modules, modinfo_t);
    279 	modinfo_t *const *mip;
    280 	const char *p, *s;
    281 	char buf[MAXMODNAME];
    282 	modinfo_t *mi;
    283 	module_t *mod, *mod2;
    284 	size_t len;
    285 	int error, i;
    286 
    287 	KASSERT(mutex_owned(&module_lock));
    288 
    289 	/*
    290 	 * Check to see if already loaded.
    291 	 */
    292 	if ((mod = module_lookup(name)) != NULL) {
    293 		if (modp != NULL) {
    294 			*modp = mod;
    295 		}
    296 		return 0;
    297 	}
    298 
    299 	/*
    300 	 * Search the list to see if we have a module by this name.
    301 	 */
    302 	error = ENOENT;
    303 	__link_set_foreach(mip, modules) {
    304 		mi = *mip;
    305 		if (mi == &module_dummy) {
    306 			continue;
    307 		}
    308 		if (strcmp(mi->mi_name, name) == 0) {
    309 			error = 0;
    310 			break;
    311 		}
    312 	}
    313 	if (error != 0) {
    314 		return error;
    315 	}
    316 
    317 	/*
    318 	 * Initialize pre-requisites.
    319 	 */
    320 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    321 	if (mod == NULL) {
    322 		return ENOMEM;
    323 	}
    324 	if (modp != NULL) {
    325 		*modp = mod;
    326 	}
    327 	if (mi->mi_required != NULL) {
    328 		for (s = mi->mi_required; *s != '\0'; s = p) {
    329 			if (*s == ',')
    330 				s++;
    331 			p = s;
    332 			while (*p != '\0' && *p != ',')
    333 				p++;
    334 			len = min(p - s + 1, sizeof(buf));
    335 			strlcpy(buf, s, len);
    336 			if (buf[0] == '\0')
    337 				break;
    338 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    339 				module_error("too many required modules");
    340 				kmem_free(mod, sizeof(*mod));
    341 				return EINVAL;
    342 			}
    343 			error = module_do_builtin(buf, &mod2);
    344 			if (error != 0) {
    345 				kmem_free(mod, sizeof(*mod));
    346 				return error;
    347 			}
    348 			mod->mod_required[mod->mod_nrequired++] = mod2;
    349 		}
    350 	}
    351 
    352 	/*
    353 	 * Try to initialize the module.
    354 	 */
    355 	KASSERT(module_active == NULL);
    356 	module_active = mod;
    357 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
    358 	module_active = NULL;
    359 	if (error != 0) {
    360 		module_error("builtin module `%s' "
    361 		    "failed to init", mi->mi_name);
    362 		kmem_free(mod, sizeof(*mod));
    363 		return error;
    364 	}
    365 	mod->mod_info = mi;
    366 	mod->mod_source = MODULE_SOURCE_KERNEL;
    367 	module_count++;
    368 	TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
    369 
    370 	/*
    371 	 * If that worked, count dependencies.
    372 	 */
    373 	for (i = 0; i < mod->mod_nrequired; i++) {
    374 		mod->mod_required[i]->mod_refcnt++;
    375 	}
    376 
    377 	return 0;
    378 }
    379 
    380 /*
    381  * module_do_load:
    382  *
    383  *	Helper routine: load a module from the file system, or one
    384  *	pushed by the boot loader.
    385  */
    386 static int
    387 module_do_load(const char *filename, bool isdep, int flags,
    388     prop_dictionary_t props, module_t **modp)
    389 {
    390 	static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
    391 	static int depth;
    392 	const int maxdepth = 6;
    393 	modinfo_t *mi;
    394 	module_t *mod, *mod2;
    395 	char buf[MAXMODNAME];
    396 	const char *s, *p;
    397 	int error;
    398 	size_t len;
    399 	u_int i;
    400 
    401 	KASSERT(mutex_owned(&module_lock));
    402 
    403 	error = 0;
    404 
    405 	/*
    406 	 * Avoid recursing too far.
    407 	 */
    408 	if (++depth > maxdepth) {
    409 		module_error("too many required modules");
    410 		depth--;
    411 		return EMLINK;
    412 	}
    413 
    414 	/*
    415 	 * Load the module and link.  Before going to the file system,
    416 	 * scan the list of modules loaded by the boot loader.  Just
    417 	 * before init is started the list of modules loaded at boot
    418 	 * will be purged.  Before init is started we can assume that
    419 	 * `filename' is a module name and not a path name.
    420 	 */
    421 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    422 		if (strcmp(mod->mod_info->mi_name, filename) == 0) {
    423 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    424 			break;
    425 		}
    426 	}
    427 	if (mod != NULL) {
    428 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    429 	} else {
    430 		mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    431 		if (mod == NULL) {
    432 			depth--;
    433 			return ENOMEM;
    434 		}
    435 		error = kobj_load_file(&mod->mod_kobj, filename);
    436 		if (error != 0) {
    437 			kmem_free(mod, sizeof(*mod));
    438 			depth--;
    439 			module_error("unable to load kernel object");
    440 			return error;
    441 		}
    442 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    443 		mod->mod_source = MODULE_SOURCE_FILESYS;
    444 		error = module_fetch_info(mod);
    445 		if (error != 0) {
    446 			goto fail;
    447 		}
    448 	}
    449 
    450 	/*
    451 	 * Check compatibility.
    452 	 */
    453 	mi = mod->mod_info;
    454 	if (strlen(mi->mi_name) >= MAXMODNAME) {
    455 		error = EINVAL;
    456 		module_error("module name too long");
    457 		goto fail;
    458 	}
    459 
    460 	/*
    461 	 * If loading a dependency, `filename' is a plain module name.
    462 	 * The name must match.
    463 	 */
    464 	if (isdep && strcmp(mi->mi_name, filename) != 0) {
    465 		error = ENOENT;
    466 		goto fail;
    467 	}
    468 
    469 	/*
    470 	 * Check to see if the module is already loaded.  If so, we may
    471 	 * have been recursively called to handle a dependency, so be sure
    472 	 * to set modp.
    473 	 */
    474 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
    475 		if (modp != NULL)
    476 			*modp = mod2;
    477 		error = EEXIST;
    478 		goto fail;
    479 	}
    480 
    481 	/*
    482 	 * Block circular dependencies.
    483 	 */
    484 	TAILQ_FOREACH(mod2, &pending, mod_chain) {
    485 		if (mod == mod2) {
    486 			continue;
    487 		}
    488 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
    489 		    	error = EDEADLK;
    490 			module_error("circular dependency detected");
    491 		    	goto fail;
    492 		}
    493 	}
    494 
    495 	/*
    496 	 * Now try to load any requisite modules.
    497 	 */
    498 	if (mi->mi_required != NULL) {
    499 		for (s = mi->mi_required; *s != '\0'; s = p) {
    500 			if (*s == ',')
    501 				s++;
    502 			p = s;
    503 			while (*p != '\0' && *p != ',')
    504 				p++;
    505 			len = p - s + 1;
    506 			if (len >= MAXMODNAME) {
    507 				error = EINVAL;
    508 				module_error("required module name too long");
    509 				goto fail;
    510 			}
    511 			strlcpy(buf, s, len);
    512 			if (buf[0] == '\0')
    513 				break;
    514 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    515 				error = EINVAL;
    516 				module_error("too many required modules");
    517 				goto fail;
    518 			}
    519 			if (strcmp(buf, mi->mi_name) == 0) {
    520 				error = EDEADLK;
    521 				module_error("self-dependency detected");
    522 				goto fail;
    523 			}
    524 			error = module_do_load(buf, true, flags, NULL,
    525 			    &mod->mod_required[mod->mod_nrequired++]);
    526 			if (error != 0 && error != EEXIST)
    527 				goto fail;
    528 		}
    529 	}
    530 
    531 	/*
    532 	 * We loaded all needed modules successfully: perform global
    533 	 * relocations and initialize.
    534 	 */
    535 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
    536 	if (error != 0) {
    537 		module_error("unable to affix module");
    538 		goto fail2;
    539 	}
    540 
    541 	KASSERT(module_active == NULL);
    542 	module_active = mod;
    543 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    544 	module_active = NULL;
    545 	if (error != 0) {
    546 		module_error("modctl function returned error %d", error);
    547 		goto fail;
    548 	}
    549 
    550 	/*
    551 	 * Good, the module loaded successfully.  Put it onto the
    552 	 * list and add references to its requisite modules.
    553 	 */
    554 	module_count++;
    555 	TAILQ_REMOVE(&pending, mod, mod_chain);
    556 	TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
    557 	for (i = 0; i < mod->mod_nrequired; i++) {
    558 		KASSERT(mod->mod_required[i] != NULL);
    559 		mod->mod_required[i]->mod_refcnt++;
    560 	}
    561 	if (modp != NULL) {
    562 		*modp = mod;
    563 	}
    564 	depth--;
    565 	return 0;
    566 
    567  fail:
    568 	kobj_unload(mod->mod_kobj);
    569  fail2:
    570 	TAILQ_REMOVE(&pending, mod, mod_chain);
    571 	kmem_free(mod, sizeof(*mod));
    572 	depth--;
    573 	return error;
    574 }
    575 
    576 /*
    577  * module_do_unload:
    578  *
    579  *	Helper routine: do the dirty work of unloading a module.
    580  */
    581 static int
    582 module_do_unload(const char *name)
    583 {
    584 	module_t *mod;
    585 	int error;
    586 	u_int i;
    587 
    588 	KASSERT(mutex_owned(&module_lock));
    589 
    590 	mod = module_lookup(name);
    591 	if (mod == NULL) {
    592 		return ENOENT;
    593 	}
    594 	if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
    595 		return EBUSY;
    596 	}
    597 	KASSERT(module_active == NULL);
    598 	module_active = mod;
    599 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
    600 	module_active = NULL;
    601 	if (error != 0) {
    602 		return error;
    603 	}
    604 	module_count--;
    605 	TAILQ_REMOVE(&module_list, mod, mod_chain);
    606 	for (i = 0; i < mod->mod_nrequired; i++) {
    607 		mod->mod_required[i]->mod_refcnt--;
    608 	}
    609 	if (mod->mod_kobj != NULL) {
    610 		kobj_unload(mod->mod_kobj);
    611 	}
    612 	kmem_free(mod, sizeof(*mod));
    613 
    614 	return 0;
    615 }
    616 
    617 /*
    618  * module_prime:
    619  *
    620  *	Push a module loaded by the bootloader onto our internal
    621  *	list.
    622  */
    623 int
    624 module_prime(void *base, size_t size)
    625 {
    626 	module_t *mod;
    627 	int error;
    628 
    629 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    630 	if (mod == NULL) {
    631 		return ENOMEM;
    632 	}
    633 	mod->mod_source = MODULE_SOURCE_BOOT;
    634 
    635 	error = kobj_load_mem(&mod->mod_kobj, base, size);
    636 	if (error != 0) {
    637 		kmem_free(mod, sizeof(*mod));
    638 		module_error("unable to load object pushed by boot loader");
    639 		return error;
    640 	}
    641 	error = module_fetch_info(mod);
    642 	if (error != 0) {
    643 		kobj_unload(mod->mod_kobj);
    644 		kmem_free(mod, sizeof(*mod));
    645 		module_error("unable to load object pushed by boot loader");
    646 		return error;
    647 	}
    648 
    649 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
    650 
    651 	return 0;
    652 }
    653 
    654 /*
    655  * module_fetch_into:
    656  *
    657  *	Fetch modinfo record from a loaded module.
    658  */
    659 static int
    660 module_fetch_info(module_t *mod)
    661 {
    662 	int error;
    663 	void *addr;
    664 	size_t size;
    665 
    666 	/*
    667 	 * Find module info record and check compatibility.
    668 	 */
    669 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
    670 	    &addr, &size);
    671 	if (error != 0) {
    672 		module_error("`link_set_modules' section not present");
    673 		return error;
    674 	}
    675 	if (size != sizeof(modinfo_t **)) {
    676 		module_error("`link_set_modules' section wrong size");
    677 		return error;
    678 	}
    679 	mod->mod_info = *(modinfo_t **)addr;
    680 
    681 	return 0;
    682 }
    683 
    684 /*
    685  * module_find_section:
    686  *
    687  *	Allows a module that is being initialized to look up a section
    688  *	within its ELF object.
    689  */
    690 int
    691 module_find_section(const char *name, void **addr, size_t *size)
    692 {
    693 
    694 	KASSERT(mutex_owned(&module_lock));
    695 	KASSERT(module_active != NULL);
    696 
    697 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
    698 }
    699