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