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