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