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