Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.117
      1  1.117  christos /*	$NetBSD: kern_module.c,v 1.117 2016/08/13 12:05:49 christos 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.25        ad  * This code is derived from software developed for The NetBSD Foundation
      8   1.25        ad  * by Andrew Doran.
      9   1.25        ad  *
     10    1.1        ad  * Redistribution and use in source and binary forms, with or without
     11    1.1        ad  * modification, are permitted provided that the following conditions
     12    1.1        ad  * are met:
     13    1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14    1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15    1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17    1.1        ad  *    documentation and/or other materials provided with the distribution.
     18    1.1        ad  *
     19    1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30    1.1        ad  */
     31    1.1        ad 
     32    1.1        ad /*
     33    1.2        ad  * Kernel module support.
     34    1.1        ad  */
     35    1.1        ad 
     36    1.1        ad #include <sys/cdefs.h>
     37  1.117  christos __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.117 2016/08/13 12:05:49 christos Exp $");
     38   1.54     pooka 
     39   1.54     pooka #define _MODULE_INTERNAL
     40   1.30        ad 
     41   1.30        ad #ifdef _KERNEL_OPT
     42   1.30        ad #include "opt_ddb.h"
     43   1.42       apb #include "opt_modular.h"
     44   1.30        ad #endif
     45    1.1        ad 
     46    1.1        ad #include <sys/param.h>
     47    1.1        ad #include <sys/systm.h>
     48   1.26        ad #include <sys/kernel.h>
     49    1.1        ad #include <sys/proc.h>
     50    1.1        ad #include <sys/kauth.h>
     51    1.1        ad #include <sys/kobj.h>
     52    1.1        ad #include <sys/kmem.h>
     53    1.1        ad #include <sys/module.h>
     54   1.26        ad #include <sys/kthread.h>
     55   1.34        ad #include <sys/sysctl.h>
     56   1.46   jnemeth #include <sys/lock.h>
     57    1.1        ad 
     58    1.1        ad #include <uvm/uvm_extern.h>
     59    1.1        ad 
     60   1.25        ad struct vm_map *module_map;
     61  1.106      matt const char *module_machine;
     62   1.54     pooka char	module_base[MODULE_BASE_SIZE];
     63    1.2        ad 
     64   1.59     pooka struct modlist        module_list = TAILQ_HEAD_INITIALIZER(module_list);
     65   1.59     pooka struct modlist        module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
     66   1.59     pooka static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     67   1.59     pooka 
     68   1.12        ad static module_t	*module_active;
     69  1.114  pgoyette bool		module_verbose_on;
     70   1.99    nonaka #ifdef MODULAR_DEFAULT_AUTOLOAD
     71  1.114  pgoyette bool		module_autoload_on = true;
     72   1.98   jnemeth #else
     73  1.114  pgoyette bool		module_autoload_on = false;
     74   1.98   jnemeth #endif
     75    1.1        ad u_int		module_count;
     76   1.59     pooka u_int		module_builtinlist;
     77   1.26        ad u_int		module_autotime = 10;
     78   1.26        ad u_int		module_gen = 1;
     79   1.26        ad static kcondvar_t module_thread_cv;
     80   1.26        ad static kmutex_t module_thread_lock;
     81   1.26        ad static int	module_thread_ticks;
     82   1.68  pgoyette int (*module_load_vfs_vec)(const char *, int, bool, module_t *,
     83  1.115   msaitoh 			   prop_dictionary_t *) = (void *)eopnotsupp;
     84    1.1        ad 
     85   1.51      elad static kauth_listener_t	module_listener;
     86   1.51      elad 
     87    1.1        ad /* Ensure that the kernel's link set isn't empty. */
     88    1.1        ad static modinfo_t module_dummy;
     89    1.1        ad __link_set_add_rodata(modules, module_dummy);
     90    1.1        ad 
     91   1.70  pgoyette static module_t	*module_newmodule(modsrc_t);
     92   1.70  pgoyette static void	module_require_force(module_t *);
     93    1.9      jmmv static int	module_do_load(const char *, bool, int, prop_dictionary_t,
     94  1.100      matt 		    module_t **, modclass_t modclass, bool);
     95   1.70  pgoyette static int	module_do_unload(const char *, bool);
     96  1.116  christos static int	module_do_builtin(const module_t *, const char *, module_t **,
     97  1.116  christos     prop_dictionary_t);
     98   1.11        ad static int	module_fetch_info(module_t *);
     99   1.26        ad static void	module_thread(void *);
    100   1.54     pooka 
    101   1.60     pooka static module_t	*module_lookup(const char *);
    102   1.60     pooka static void	module_enqueue(module_t *);
    103   1.60     pooka 
    104   1.47   jnemeth static bool	module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
    105    1.1        ad 
    106   1.69     pooka static void	sysctl_module_setup(void);
    107   1.94  pgoyette static int	sysctl_module_autotime(SYSCTLFN_PROTO);
    108   1.94  pgoyette 
    109  1.100      matt #define MODULE_CLASS_MATCH(mi, modclass) \
    110  1.100      matt 	((modclass) == MODULE_CLASS_ANY || (modclass) == (mi)->mi_class)
    111   1.88  christos 
    112   1.88  christos static void
    113  1.100      matt module_incompat(const modinfo_t *mi, int modclass)
    114   1.88  christos {
    115   1.88  christos 	module_error("incompatible module class for `%s' (%d != %d)",
    116  1.100      matt 	    mi->mi_name, modclass, mi->mi_class);
    117   1.88  christos }
    118   1.69     pooka 
    119    1.1        ad /*
    120    1.1        ad  * module_error:
    121    1.1        ad  *
    122    1.1        ad  *	Utility function: log an error.
    123    1.1        ad  */
    124   1.54     pooka void
    125    1.1        ad module_error(const char *fmt, ...)
    126    1.1        ad {
    127    1.1        ad 	va_list ap;
    128    1.1        ad 
    129    1.1        ad 	va_start(ap, fmt);
    130    1.1        ad 	printf("WARNING: module error: ");
    131    1.1        ad 	vprintf(fmt, ap);
    132    1.1        ad 	printf("\n");
    133    1.1        ad 	va_end(ap);
    134    1.1        ad }
    135    1.1        ad 
    136    1.1        ad /*
    137   1.34        ad  * module_print:
    138   1.34        ad  *
    139   1.34        ad  *	Utility function: log verbose output.
    140   1.34        ad  */
    141   1.54     pooka void
    142   1.34        ad module_print(const char *fmt, ...)
    143   1.34        ad {
    144   1.34        ad 	va_list ap;
    145   1.34        ad 
    146   1.34        ad 	if (module_verbose_on) {
    147   1.34        ad 		va_start(ap, fmt);
    148   1.34        ad 		printf("DEBUG: module: ");
    149   1.34        ad 		vprintf(fmt, ap);
    150   1.34        ad 		printf("\n");
    151   1.34        ad 		va_end(ap);
    152   1.34        ad 	}
    153   1.34        ad }
    154   1.34        ad 
    155   1.55      elad static int
    156   1.55      elad module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    157   1.55      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    158   1.55      elad {
    159   1.55      elad 	int result;
    160   1.55      elad 
    161   1.55      elad 	result = KAUTH_RESULT_DEFER;
    162   1.55      elad 
    163   1.55      elad 	if (action != KAUTH_SYSTEM_MODULE)
    164   1.55      elad 		return result;
    165   1.55      elad 
    166   1.55      elad 	if ((uintptr_t)arg2 != 0)	/* autoload */
    167   1.55      elad 		result = KAUTH_RESULT_ALLOW;
    168   1.55      elad 
    169   1.55      elad 	return result;
    170   1.55      elad }
    171   1.55      elad 
    172   1.34        ad /*
    173   1.70  pgoyette  * Allocate a new module_t
    174   1.70  pgoyette  */
    175   1.70  pgoyette static module_t *
    176   1.70  pgoyette module_newmodule(modsrc_t source)
    177   1.70  pgoyette {
    178   1.70  pgoyette 	module_t *mod;
    179   1.70  pgoyette 
    180   1.70  pgoyette 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    181   1.70  pgoyette 	if (mod != NULL) {
    182   1.70  pgoyette 		mod->mod_source = source;
    183   1.70  pgoyette 		mod->mod_info = NULL;
    184   1.70  pgoyette 		mod->mod_flags = 0;
    185   1.70  pgoyette 	}
    186   1.70  pgoyette 	return mod;
    187   1.70  pgoyette }
    188   1.70  pgoyette 
    189   1.70  pgoyette /*
    190   1.70  pgoyette  * Require the -f (force) flag to load a module
    191   1.70  pgoyette  */
    192   1.70  pgoyette static void
    193   1.70  pgoyette module_require_force(struct module *mod)
    194   1.70  pgoyette {
    195   1.70  pgoyette 	mod->mod_flags |= MODFLG_MUST_FORCE;
    196   1.70  pgoyette }
    197   1.70  pgoyette 
    198   1.70  pgoyette /*
    199   1.59     pooka  * Add modules to the builtin list.  This can done at boottime or
    200   1.59     pooka  * at runtime if the module is linked into the kernel with an
    201   1.59     pooka  * external linker.  All or none of the input will be handled.
    202   1.59     pooka  * Optionally, the modules can be initialized.  If they are not
    203   1.59     pooka  * initialized, module_init_class() or module_load() can be used
    204   1.59     pooka  * later, but these are not guaranteed to give atomic results.
    205   1.59     pooka  */
    206   1.59     pooka int
    207   1.59     pooka module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
    208   1.59     pooka {
    209   1.59     pooka 	struct module **modp = NULL, *mod_iter;
    210   1.59     pooka 	int rv = 0, i, mipskip;
    211   1.59     pooka 
    212   1.59     pooka 	if (init) {
    213   1.59     pooka 		rv = kauth_authorize_system(kauth_cred_get(),
    214   1.59     pooka 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
    215   1.59     pooka 		    (void *)(uintptr_t)1, NULL);
    216   1.59     pooka 		if (rv) {
    217   1.59     pooka 			return rv;
    218   1.59     pooka 		}
    219   1.59     pooka 	}
    220   1.59     pooka 
    221   1.59     pooka 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    222   1.59     pooka 		if (mip[i] == &module_dummy) {
    223   1.59     pooka 			KASSERT(nmodinfo > 0);
    224   1.59     pooka 			nmodinfo--;
    225   1.59     pooka 		}
    226   1.59     pooka 	}
    227   1.59     pooka 	if (nmodinfo == 0)
    228   1.59     pooka 		return 0;
    229   1.59     pooka 
    230   1.59     pooka 	modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
    231   1.59     pooka 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    232   1.59     pooka 		if (mip[i+mipskip] == &module_dummy) {
    233   1.59     pooka 			mipskip++;
    234   1.59     pooka 			continue;
    235   1.59     pooka 		}
    236   1.70  pgoyette 		modp[i] = module_newmodule(MODULE_SOURCE_KERNEL);
    237   1.59     pooka 		modp[i]->mod_info = mip[i+mipskip];
    238   1.59     pooka 	}
    239   1.72  pgoyette 	kernconfig_lock();
    240   1.59     pooka 
    241   1.59     pooka 	/* do this in three stages for error recovery and atomicity */
    242   1.59     pooka 
    243   1.59     pooka 	/* first check for presence */
    244   1.59     pooka 	for (i = 0; i < nmodinfo; i++) {
    245   1.59     pooka 		TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
    246   1.59     pooka 			if (strcmp(mod_iter->mod_info->mi_name,
    247   1.59     pooka 			    modp[i]->mod_info->mi_name) == 0)
    248   1.59     pooka 				break;
    249   1.59     pooka 		}
    250   1.59     pooka 		if (mod_iter) {
    251   1.59     pooka 			rv = EEXIST;
    252   1.59     pooka 			goto out;
    253   1.59     pooka 		}
    254   1.59     pooka 
    255   1.59     pooka 		if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
    256   1.59     pooka 			rv = EEXIST;
    257   1.59     pooka 			goto out;
    258   1.59     pooka 		}
    259   1.59     pooka 	}
    260   1.59     pooka 
    261   1.59     pooka 	/* then add to list */
    262   1.59     pooka 	for (i = 0; i < nmodinfo; i++) {
    263   1.59     pooka 		TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
    264   1.59     pooka 		module_builtinlist++;
    265   1.59     pooka 	}
    266   1.59     pooka 
    267   1.59     pooka 	/* finally, init (if required) */
    268   1.59     pooka 	if (init) {
    269   1.59     pooka 		for (i = 0; i < nmodinfo; i++) {
    270  1.116  christos 			rv = module_do_builtin(modp[i],
    271  1.116  christos 			    modp[i]->mod_info->mi_name, NULL, NULL);
    272   1.59     pooka 			/* throw in the towel, recovery hard & not worth it */
    273   1.59     pooka 			if (rv)
    274  1.117  christos 				panic("%s: builtin module \"%s\" init failed:"
    275  1.117  christos 				    " %d", __func__,
    276   1.59     pooka 				    modp[i]->mod_info->mi_name, rv);
    277   1.59     pooka 		}
    278   1.59     pooka 	}
    279   1.59     pooka 
    280   1.59     pooka  out:
    281   1.72  pgoyette 	kernconfig_unlock();
    282   1.59     pooka 	if (rv != 0) {
    283   1.59     pooka 		for (i = 0; i < nmodinfo; i++) {
    284   1.59     pooka 			if (modp[i])
    285   1.59     pooka 				kmem_free(modp[i], sizeof(*modp[i]));
    286   1.59     pooka 		}
    287   1.59     pooka 	}
    288   1.59     pooka 	kmem_free(modp, sizeof(*modp) * nmodinfo);
    289   1.59     pooka 	return rv;
    290   1.59     pooka }
    291   1.59     pooka 
    292   1.59     pooka /*
    293   1.59     pooka  * Optionally fini and remove builtin module from the kernel.
    294   1.59     pooka  * Note: the module will now be unreachable except via mi && builtin_add.
    295   1.59     pooka  */
    296   1.59     pooka int
    297   1.59     pooka module_builtin_remove(modinfo_t *mi, bool fini)
    298   1.59     pooka {
    299   1.59     pooka 	struct module *mod;
    300   1.59     pooka 	int rv = 0;
    301   1.59     pooka 
    302   1.59     pooka 	if (fini) {
    303   1.59     pooka 		rv = kauth_authorize_system(kauth_cred_get(),
    304   1.59     pooka 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
    305   1.59     pooka 		    NULL, NULL);
    306   1.59     pooka 		if (rv)
    307   1.59     pooka 			return rv;
    308   1.59     pooka 
    309   1.72  pgoyette 		kernconfig_lock();
    310   1.70  pgoyette 		rv = module_do_unload(mi->mi_name, true);
    311   1.59     pooka 		if (rv) {
    312   1.59     pooka 			goto out;
    313   1.59     pooka 		}
    314   1.59     pooka 	} else {
    315   1.72  pgoyette 		kernconfig_lock();
    316   1.59     pooka 	}
    317   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    318   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
    319   1.59     pooka 			break;
    320   1.59     pooka 	}
    321   1.59     pooka 	if (mod) {
    322   1.59     pooka 		TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    323   1.59     pooka 		module_builtinlist--;
    324   1.59     pooka 	} else {
    325   1.59     pooka 		KASSERT(fini == false);
    326   1.59     pooka 		rv = ENOENT;
    327   1.59     pooka 	}
    328   1.59     pooka 
    329   1.59     pooka  out:
    330   1.72  pgoyette 	kernconfig_unlock();
    331   1.59     pooka 	return rv;
    332   1.59     pooka }
    333   1.59     pooka 
    334   1.59     pooka /*
    335    1.1        ad  * module_init:
    336    1.1        ad  *
    337    1.1        ad  *	Initialize the module subsystem.
    338    1.1        ad  */
    339    1.1        ad void
    340    1.1        ad module_init(void)
    341    1.1        ad {
    342   1.59     pooka 	__link_set_decl(modules, modinfo_t);
    343   1.25        ad 	extern struct vm_map *module_map;
    344   1.59     pooka 	modinfo_t *const *mip;
    345   1.59     pooka 	int rv;
    346    1.1        ad 
    347   1.25        ad 	if (module_map == NULL) {
    348   1.25        ad 		module_map = kernel_map;
    349   1.25        ad 	}
    350   1.71  pgoyette 	cv_init(&module_thread_cv, "mod_unld");
    351   1.26        ad 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
    352   1.66  pgoyette 
    353   1.13        ad #ifdef MODULAR	/* XXX */
    354   1.11        ad 	module_init_md();
    355   1.12        ad #endif
    356   1.16        ad 
    357   1.78       mrg 	if (!module_machine)
    358   1.78       mrg 		module_machine = machine;
    359   1.16        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    360   1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
    361   1.78       mrg 	    module_machine, osrelease);
    362   1.16        ad #else						/* release */
    363   1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
    364   1.78       mrg 	    module_machine, __NetBSD_Version__ / 100000000,
    365   1.16        ad 	    __NetBSD_Version__ / 1000000 % 100);
    366   1.16        ad #endif
    367   1.50      elad 
    368   1.55      elad 	module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    369   1.55      elad 	    module_listener_cb, NULL);
    370   1.59     pooka 
    371   1.59     pooka 	__link_set_foreach(mip, modules) {
    372   1.77   mbalmer 		if ((rv = module_builtin_add(mip, 1, false)) != 0)
    373   1.59     pooka 			module_error("builtin %s failed: %d\n",
    374   1.59     pooka 			    (*mip)->mi_name, rv);
    375   1.59     pooka 	}
    376   1.69     pooka 
    377   1.69     pooka 	sysctl_module_setup();
    378   1.51      elad }
    379   1.51      elad 
    380   1.50      elad /*
    381   1.70  pgoyette  * module_start_unload_thread:
    382   1.50      elad  *
    383   1.50      elad  *	Start the auto unload kthread.
    384   1.50      elad  */
    385   1.50      elad void
    386   1.70  pgoyette module_start_unload_thread(void)
    387   1.50      elad {
    388   1.50      elad 	int error;
    389   1.26        ad 
    390   1.26        ad 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
    391   1.26        ad 	    NULL, NULL, "modunload");
    392   1.26        ad 	if (error != 0)
    393  1.117  christos 		panic("%s: %d", __func__, error);
    394    1.1        ad }
    395    1.1        ad 
    396   1.70  pgoyette /*
    397   1.70  pgoyette  * module_builtin_require_force
    398   1.70  pgoyette  *
    399   1.70  pgoyette  * Require MODCTL_MUST_FORCE to load any built-in modules that have
    400   1.70  pgoyette  * not yet been initialized
    401   1.70  pgoyette  */
    402   1.70  pgoyette void
    403   1.70  pgoyette module_builtin_require_force(void)
    404   1.70  pgoyette {
    405   1.70  pgoyette 	module_t *mod;
    406   1.70  pgoyette 
    407   1.72  pgoyette 	kernconfig_lock();
    408   1.70  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    409   1.70  pgoyette 		module_require_force(mod);
    410   1.70  pgoyette 	}
    411   1.72  pgoyette 	kernconfig_unlock();
    412   1.70  pgoyette }
    413   1.70  pgoyette 
    414   1.69     pooka static struct sysctllog *module_sysctllog;
    415   1.69     pooka 
    416   1.94  pgoyette static int
    417   1.94  pgoyette sysctl_module_autotime(SYSCTLFN_ARGS)
    418   1.94  pgoyette {
    419   1.94  pgoyette 	struct sysctlnode node;
    420   1.94  pgoyette 	int t, error;
    421   1.94  pgoyette 
    422   1.94  pgoyette 	t = *(int *)rnode->sysctl_data;
    423   1.94  pgoyette 
    424   1.94  pgoyette 	node = *rnode;
    425   1.94  pgoyette 	node.sysctl_data = &t;
    426   1.94  pgoyette 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    427   1.94  pgoyette 	if (error || newp == NULL)
    428   1.94  pgoyette 		return (error);
    429   1.94  pgoyette 
    430   1.94  pgoyette 	if (t < 0)
    431   1.94  pgoyette 		return (EINVAL);
    432   1.94  pgoyette 
    433   1.94  pgoyette 	*(int *)rnode->sysctl_data = t;
    434   1.94  pgoyette 	return (0);
    435   1.94  pgoyette }
    436   1.94  pgoyette 
    437   1.69     pooka static void
    438   1.69     pooka sysctl_module_setup(void)
    439   1.34        ad {
    440   1.34        ad 	const struct sysctlnode *node = NULL;
    441   1.34        ad 
    442   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, NULL, &node,
    443   1.34        ad 		CTLFLAG_PERMANENT,
    444   1.34        ad 		CTLTYPE_NODE, "module",
    445   1.34        ad 		SYSCTL_DESCR("Module options"),
    446   1.34        ad 		NULL, 0, NULL, 0,
    447   1.34        ad 		CTL_KERN, CTL_CREATE, CTL_EOL);
    448   1.34        ad 
    449   1.34        ad 	if (node == NULL)
    450   1.34        ad 		return;
    451   1.34        ad 
    452   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    453   1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    454   1.64    jruoho 		CTLTYPE_BOOL, "autoload",
    455   1.34        ad 		SYSCTL_DESCR("Enable automatic load of modules"),
    456   1.34        ad 		NULL, 0, &module_autoload_on, 0,
    457   1.34        ad 		CTL_CREATE, CTL_EOL);
    458   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    459   1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    460   1.64    jruoho 		CTLTYPE_BOOL, "verbose",
    461   1.34        ad 		SYSCTL_DESCR("Enable verbose output"),
    462   1.34        ad 		NULL, 0, &module_verbose_on, 0,
    463   1.34        ad 		CTL_CREATE, CTL_EOL);
    464   1.94  pgoyette 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    465   1.97   jnemeth 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    466   1.97   jnemeth 		CTLTYPE_STRING, "path",
    467   1.97   jnemeth 		SYSCTL_DESCR("Default module load path"),
    468   1.97   jnemeth 		NULL, 0, module_base, 0,
    469   1.97   jnemeth 		CTL_CREATE, CTL_EOL);
    470   1.97   jnemeth 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    471   1.94  pgoyette 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    472   1.94  pgoyette 		CTLTYPE_INT, "autotime",
    473   1.94  pgoyette 		SYSCTL_DESCR("Auto-unload delay"),
    474   1.94  pgoyette 		sysctl_module_autotime, 0, &module_autotime, 0,
    475   1.94  pgoyette 		CTL_CREATE, CTL_EOL);
    476   1.34        ad }
    477   1.34        ad 
    478    1.1        ad /*
    479    1.1        ad  * module_init_class:
    480    1.1        ad  *
    481    1.1        ad  *	Initialize all built-in and pre-loaded modules of the
    482    1.1        ad  *	specified class.
    483    1.1        ad  */
    484    1.1        ad void
    485  1.100      matt module_init_class(modclass_t modclass)
    486    1.1        ad {
    487   1.63     pooka 	TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
    488   1.59     pooka 	module_t *mod;
    489   1.59     pooka 	modinfo_t *mi;
    490    1.1        ad 
    491   1.72  pgoyette 	kernconfig_lock();
    492    1.1        ad 	/*
    493   1.59     pooka 	 * Builtins first.  These will not depend on pre-loaded modules
    494   1.59     pooka 	 * (because the kernel would not link).
    495    1.1        ad 	 */
    496   1.59     pooka 	do {
    497   1.59     pooka 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    498   1.59     pooka 			mi = mod->mod_info;
    499  1.100      matt 			if (!MODULE_CLASS_MATCH(mi, modclass))
    500   1.59     pooka 				continue;
    501   1.63     pooka 			/*
    502   1.63     pooka 			 * If initializing a builtin module fails, don't try
    503   1.63     pooka 			 * to load it again.  But keep it around and queue it
    504   1.70  pgoyette 			 * on the builtins list after we're done with module
    505   1.70  pgoyette 			 * init.  Don't set it to MODFLG_MUST_FORCE in case a
    506   1.70  pgoyette 			 * future attempt to initialize can be successful.
    507   1.70  pgoyette 			 * (If the module has previously been set to
    508   1.70  pgoyette 			 * MODFLG_MUST_FORCE, don't try to override that!)
    509   1.63     pooka 			 */
    510  1.100      matt 			if ((mod->mod_flags & MODFLG_MUST_FORCE) ||
    511  1.116  christos 			    module_do_builtin(mod, mi->mi_name, NULL,
    512  1.116  christos 			    NULL) != 0) {
    513   1.63     pooka 				TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    514   1.63     pooka 				TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
    515   1.63     pooka 			}
    516   1.59     pooka 			break;
    517    1.1        ad 		}
    518   1.59     pooka 	} while (mod != NULL);
    519   1.59     pooka 
    520    1.1        ad 	/*
    521    1.1        ad 	 * Now preloaded modules.  These will be pulled off the
    522    1.1        ad 	 * list as we call module_do_load();
    523    1.1        ad 	 */
    524   1.11        ad 	do {
    525   1.59     pooka 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    526   1.11        ad 			mi = mod->mod_info;
    527  1.100      matt 			if (!MODULE_CLASS_MATCH(mi, modclass))
    528   1.11        ad 				continue;
    529   1.18        ad 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
    530  1.100      matt 			    modclass, false);
    531   1.11        ad 			break;
    532   1.11        ad 		}
    533   1.11        ad 	} while (mod != NULL);
    534   1.63     pooka 
    535   1.70  pgoyette 	/* return failed builtin modules to builtin list */
    536   1.63     pooka 	while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
    537   1.63     pooka 		TAILQ_REMOVE(&bi_fail, mod, mod_chain);
    538   1.63     pooka 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
    539   1.63     pooka 	}
    540   1.63     pooka 
    541   1.72  pgoyette 	kernconfig_unlock();
    542    1.1        ad }
    543    1.1        ad 
    544    1.1        ad /*
    545   1.21        ad  * module_compatible:
    546    1.1        ad  *
    547   1.21        ad  *	Return true if the two supplied kernel versions are said to
    548   1.21        ad  *	have the same binary interface for kernel code.  The entire
    549   1.21        ad  *	version is signficant for the development tree (-current),
    550   1.21        ad  *	major and minor versions are significant for official
    551   1.21        ad  *	releases of the system.
    552    1.1        ad  */
    553   1.22     pooka bool
    554   1.21        ad module_compatible(int v1, int v2)
    555    1.1        ad {
    556    1.1        ad 
    557   1.21        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    558   1.21        ad 	return v1 == v2;
    559   1.21        ad #else						/* release */
    560   1.21        ad 	return abs(v1 - v2) < 10000;
    561   1.21        ad #endif
    562    1.1        ad }
    563    1.1        ad 
    564    1.1        ad /*
    565    1.1        ad  * module_load:
    566    1.1        ad  *
    567    1.9      jmmv  *	Load a single module from the file system.
    568    1.1        ad  */
    569    1.1        ad int
    570   1.18        ad module_load(const char *filename, int flags, prop_dictionary_t props,
    571  1.100      matt 	    modclass_t modclass)
    572    1.1        ad {
    573    1.1        ad 	int error;
    574    1.1        ad 
    575   1.18        ad 	/* Authorize. */
    576   1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    577   1.18        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
    578   1.18        ad 	if (error != 0) {
    579   1.18        ad 		return error;
    580   1.18        ad 	}
    581   1.18        ad 
    582   1.72  pgoyette 	kernconfig_lock();
    583  1.100      matt 	error = module_do_load(filename, false, flags, props, NULL, modclass,
    584   1.23        ad 	    false);
    585   1.72  pgoyette 	kernconfig_unlock();
    586    1.1        ad 
    587    1.1        ad 	return error;
    588    1.1        ad }
    589    1.1        ad 
    590    1.1        ad /*
    591   1.23        ad  * module_autoload:
    592   1.23        ad  *
    593   1.23        ad  *	Load a single module from the file system, system initiated.
    594   1.23        ad  */
    595   1.23        ad int
    596  1.100      matt module_autoload(const char *filename, modclass_t modclass)
    597   1.23        ad {
    598   1.23        ad 	int error;
    599   1.23        ad 
    600   1.72  pgoyette 	kernconfig_lock();
    601   1.23        ad 
    602   1.34        ad 	/* Nothing if the user has disabled it. */
    603   1.34        ad 	if (!module_autoload_on) {
    604   1.72  pgoyette 		kernconfig_unlock();
    605   1.34        ad 		return EPERM;
    606   1.34        ad 	}
    607   1.34        ad 
    608   1.56  dholland         /* Disallow path separators and magic symlinks. */
    609   1.29        ad         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    610   1.29        ad             strchr(filename, '.') != NULL) {
    611   1.72  pgoyette 		kernconfig_unlock();
    612   1.29        ad         	return EPERM;
    613   1.29        ad 	}
    614   1.29        ad 
    615   1.23        ad 	/* Authorize. */
    616   1.23        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    617   1.23        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    618   1.23        ad 
    619   1.72  pgoyette 	if (error == 0)
    620  1.100      matt 		error = module_do_load(filename, false, 0, NULL, NULL, modclass,
    621   1.72  pgoyette 		    true);
    622   1.72  pgoyette 
    623   1.72  pgoyette 	kernconfig_unlock();
    624   1.72  pgoyette 	return error;
    625   1.23        ad }
    626   1.23        ad 
    627   1.23        ad /*
    628    1.1        ad  * module_unload:
    629    1.1        ad  *
    630    1.1        ad  *	Find and unload a module by name.
    631    1.1        ad  */
    632    1.1        ad int
    633    1.1        ad module_unload(const char *name)
    634    1.1        ad {
    635    1.1        ad 	int error;
    636    1.1        ad 
    637   1.18        ad 	/* Authorize. */
    638   1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    639   1.18        ad 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    640   1.18        ad 	if (error != 0) {
    641   1.18        ad 		return error;
    642   1.18        ad 	}
    643   1.18        ad 
    644   1.72  pgoyette 	kernconfig_lock();
    645   1.70  pgoyette 	error = module_do_unload(name, true);
    646   1.72  pgoyette 	kernconfig_unlock();
    647    1.1        ad 
    648    1.1        ad 	return error;
    649    1.1        ad }
    650    1.1        ad 
    651    1.1        ad /*
    652    1.1        ad  * module_lookup:
    653    1.1        ad  *
    654    1.1        ad  *	Look up a module by name.
    655    1.1        ad  */
    656    1.1        ad module_t *
    657    1.1        ad module_lookup(const char *name)
    658    1.1        ad {
    659    1.1        ad 	module_t *mod;
    660    1.1        ad 
    661   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    662    1.1        ad 
    663    1.1        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    664    1.1        ad 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    665    1.1        ad 			break;
    666    1.1        ad 		}
    667    1.1        ad 	}
    668    1.1        ad 
    669    1.1        ad 	return mod;
    670    1.1        ad }
    671    1.1        ad 
    672    1.1        ad /*
    673    1.1        ad  * module_hold:
    674    1.1        ad  *
    675    1.1        ad  *	Add a single reference to a module.  It's the caller's
    676    1.1        ad  *	responsibility to ensure that the reference is dropped
    677    1.1        ad  *	later.
    678    1.1        ad  */
    679    1.1        ad int
    680    1.1        ad module_hold(const char *name)
    681    1.1        ad {
    682    1.1        ad 	module_t *mod;
    683    1.1        ad 
    684   1.72  pgoyette 	kernconfig_lock();
    685    1.1        ad 	mod = module_lookup(name);
    686    1.1        ad 	if (mod == NULL) {
    687   1.72  pgoyette 		kernconfig_unlock();
    688    1.1        ad 		return ENOENT;
    689    1.1        ad 	}
    690    1.1        ad 	mod->mod_refcnt++;
    691   1.72  pgoyette 	kernconfig_unlock();
    692    1.1        ad 
    693    1.1        ad 	return 0;
    694    1.1        ad }
    695    1.1        ad 
    696    1.1        ad /*
    697    1.1        ad  * module_rele:
    698    1.1        ad  *
    699    1.1        ad  *	Release a reference acquired with module_hold().
    700    1.1        ad  */
    701    1.1        ad void
    702    1.1        ad module_rele(const char *name)
    703    1.1        ad {
    704    1.1        ad 	module_t *mod;
    705    1.1        ad 
    706   1.72  pgoyette 	kernconfig_lock();
    707    1.1        ad 	mod = module_lookup(name);
    708    1.1        ad 	if (mod == NULL) {
    709   1.72  pgoyette 		kernconfig_unlock();
    710  1.117  christos 		panic("%s: gone", __func__);
    711    1.1        ad 	}
    712    1.1        ad 	mod->mod_refcnt--;
    713   1.72  pgoyette 	kernconfig_unlock();
    714    1.1        ad }
    715    1.1        ad 
    716    1.1        ad /*
    717   1.27        ad  * module_enqueue:
    718   1.27        ad  *
    719   1.27        ad  *	Put a module onto the global list and update counters.
    720   1.27        ad  */
    721   1.53     pooka void
    722   1.27        ad module_enqueue(module_t *mod)
    723   1.27        ad {
    724   1.27        ad 	int i;
    725   1.27        ad 
    726   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    727   1.53     pooka 
    728   1.27        ad 	/*
    729  1.113  pgoyette 	 * Put new entry at the head of the queue so autounload can unload
    730  1.113  pgoyette 	 * requisite modules with only one pass through the queue.
    731   1.27        ad 	 */
    732  1.113  pgoyette 	TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    733   1.27        ad 	if (mod->mod_nrequired) {
    734   1.27        ad 
    735   1.27        ad 		/* Add references to the requisite modules. */
    736   1.27        ad 		for (i = 0; i < mod->mod_nrequired; i++) {
    737   1.27        ad 			KASSERT(mod->mod_required[i] != NULL);
    738   1.27        ad 			mod->mod_required[i]->mod_refcnt++;
    739   1.27        ad 		}
    740   1.27        ad 	}
    741   1.27        ad 	module_count++;
    742   1.27        ad 	module_gen++;
    743   1.27        ad }
    744   1.27        ad 
    745   1.27        ad /*
    746    1.1        ad  * module_do_builtin:
    747    1.1        ad  *
    748   1.59     pooka  *	Initialize a module from the list of modules that are
    749   1.59     pooka  *	already linked into the kernel.
    750    1.1        ad  */
    751    1.1        ad static int
    752  1.116  christos module_do_builtin(const module_t *pmod, const char *name, module_t **modp,
    753  1.116  christos     prop_dictionary_t props)
    754    1.1        ad {
    755    1.1        ad 	const char *p, *s;
    756    1.1        ad 	char buf[MAXMODNAME];
    757   1.59     pooka 	modinfo_t *mi = NULL;
    758   1.72  pgoyette 	module_t *mod, *mod2, *mod_loaded, *prev_active;
    759    1.1        ad 	size_t len;
    760   1.27        ad 	int error;
    761    1.1        ad 
    762   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    763    1.1        ad 
    764    1.1        ad 	/*
    765   1.59     pooka 	 * Search the list to see if we have a module by this name.
    766    1.1        ad 	 */
    767   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    768   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    769   1.59     pooka 			mi = mod->mod_info;
    770   1.59     pooka 			break;
    771    1.1        ad 		}
    772    1.1        ad 	}
    773    1.1        ad 
    774    1.1        ad 	/*
    775   1.59     pooka 	 * Check to see if already loaded.  This might happen if we
    776   1.59     pooka 	 * were already loaded as a dependency.
    777    1.1        ad 	 */
    778   1.59     pooka 	if ((mod_loaded = module_lookup(name)) != NULL) {
    779   1.59     pooka 		KASSERT(mod == NULL);
    780   1.59     pooka 		if (modp)
    781   1.59     pooka 			*modp = mod_loaded;
    782   1.59     pooka 		return 0;
    783    1.1        ad 	}
    784   1.59     pooka 
    785   1.59     pooka 	/* Note! This is from TAILQ, not immediate above */
    786   1.65     pooka 	if (mi == NULL) {
    787   1.65     pooka 		/*
    788   1.65     pooka 		 * XXX: We'd like to panic here, but currently in some
    789   1.65     pooka 		 * cases (such as nfsserver + nfs), the dependee can be
    790   1.65     pooka 		 * succesfully linked without the dependencies.
    791   1.65     pooka 		 */
    792  1.116  christos 		module_error("%s: can't find builtin dependency `%s'",
    793  1.116  christos 		    pmod->mod_info->mi_name, name);
    794   1.65     pooka 		return ENOENT;
    795   1.65     pooka 	}
    796    1.1        ad 
    797    1.1        ad 	/*
    798    1.1        ad 	 * Initialize pre-requisites.
    799    1.1        ad 	 */
    800    1.1        ad 	if (mi->mi_required != NULL) {
    801    1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
    802    1.1        ad 			if (*s == ',')
    803    1.1        ad 				s++;
    804    1.1        ad 			p = s;
    805    1.1        ad 			while (*p != '\0' && *p != ',')
    806    1.1        ad 				p++;
    807    1.1        ad 			len = min(p - s + 1, sizeof(buf));
    808    1.1        ad 			strlcpy(buf, s, len);
    809    1.1        ad 			if (buf[0] == '\0')
    810    1.1        ad 				break;
    811    1.1        ad 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    812  1.116  christos 				module_error("%s: too many required modules "
    813  1.116  christos 				    "%d >= %d", pmod->mod_info->mi_name,
    814  1.116  christos 				    mod->mod_nrequired, MAXMODDEPS - 1);
    815    1.1        ad 				return EINVAL;
    816    1.1        ad 			}
    817  1.116  christos 			error = module_do_builtin(mod, buf, &mod2, NULL);
    818    1.1        ad 			if (error != 0) {
    819    1.1        ad 				return error;
    820    1.1        ad 			}
    821    1.1        ad 			mod->mod_required[mod->mod_nrequired++] = mod2;
    822    1.1        ad 		}
    823    1.1        ad 	}
    824    1.1        ad 
    825    1.1        ad 	/*
    826    1.1        ad 	 * Try to initialize the module.
    827    1.1        ad 	 */
    828   1.72  pgoyette 	prev_active = module_active;
    829   1.12        ad 	module_active = mod;
    830   1.74     pooka 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    831   1.72  pgoyette 	module_active = prev_active;
    832    1.1        ad 	if (error != 0) {
    833    1.1        ad 		module_error("builtin module `%s' "
    834   1.90  christos 		    "failed to init, error %d", mi->mi_name, error);
    835    1.1        ad 		return error;
    836    1.1        ad 	}
    837   1.59     pooka 
    838   1.59     pooka 	/* load always succeeds after this point */
    839   1.59     pooka 
    840   1.59     pooka 	TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    841   1.59     pooka 	module_builtinlist--;
    842   1.59     pooka 	if (modp != NULL) {
    843   1.59     pooka 		*modp = mod;
    844   1.59     pooka 	}
    845   1.27        ad 	module_enqueue(mod);
    846    1.1        ad 	return 0;
    847    1.1        ad }
    848    1.1        ad 
    849    1.1        ad /*
    850    1.1        ad  * module_do_load:
    851    1.1        ad  *
    852    1.1        ad  *	Helper routine: load a module from the file system, or one
    853    1.1        ad  *	pushed by the boot loader.
    854    1.1        ad  */
    855    1.1        ad static int
    856   1.27        ad module_do_load(const char *name, bool isdep, int flags,
    857  1.100      matt 	       prop_dictionary_t props, module_t **modp, modclass_t modclass,
    858   1.20        ad 	       bool autoload)
    859    1.1        ad {
    860   1.72  pgoyette #define MODULE_MAX_DEPTH 6
    861   1.72  pgoyette 
    862   1.72  pgoyette 	TAILQ_HEAD(pending_t, module);
    863   1.72  pgoyette 	static int depth = 0;
    864   1.72  pgoyette 	static struct pending_t *pending_lists[MODULE_MAX_DEPTH];
    865   1.72  pgoyette 	struct pending_t *pending;
    866   1.72  pgoyette 	struct pending_t new_pending = TAILQ_HEAD_INITIALIZER(new_pending);
    867    1.1        ad 	modinfo_t *mi;
    868   1.72  pgoyette 	module_t *mod, *mod2, *prev_active;
    869   1.46   jnemeth 	prop_dictionary_t filedict;
    870   1.54     pooka 	char buf[MAXMODNAME];
    871    1.1        ad 	const char *s, *p;
    872    1.1        ad 	int error;
    873   1.54     pooka 	size_t len;
    874    1.1        ad 
    875   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    876    1.1        ad 
    877   1.46   jnemeth 	filedict = NULL;
    878    1.1        ad 	error = 0;
    879    1.1        ad 
    880    1.1        ad 	/*
    881    1.1        ad 	 * Avoid recursing too far.
    882    1.1        ad 	 */
    883   1.72  pgoyette 	if (++depth > MODULE_MAX_DEPTH) {
    884   1.90  christos 		module_error("recursion too deep for `%s' %d > %d", name,
    885   1.90  christos 		    depth, MODULE_MAX_DEPTH);
    886    1.1        ad 		depth--;
    887    1.1        ad 		return EMLINK;
    888    1.1        ad 	}
    889    1.1        ad 
    890    1.1        ad 	/*
    891   1.72  pgoyette 	 * Set up the pending list for this depth.  If this is a
    892   1.72  pgoyette 	 * recursive entry, then use same list as for outer call,
    893   1.72  pgoyette 	 * else use the locally allocated list.  In either case,
    894   1.72  pgoyette 	 * remember which one we're using.
    895   1.72  pgoyette 	 */
    896   1.72  pgoyette 	if (isdep) {
    897   1.72  pgoyette 		KASSERT(depth > 1);
    898   1.72  pgoyette 		pending = pending_lists[depth - 2];
    899   1.72  pgoyette 	} else
    900   1.72  pgoyette 		pending = &new_pending;
    901   1.72  pgoyette 	pending_lists[depth - 1] = pending;
    902   1.72  pgoyette 
    903   1.72  pgoyette 	/*
    904   1.59     pooka 	 * Search the list of disabled builtins first.
    905   1.59     pooka 	 */
    906   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    907   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    908   1.59     pooka 			break;
    909   1.59     pooka 		}
    910   1.59     pooka 	}
    911   1.59     pooka 	if (mod) {
    912   1.70  pgoyette 		if ((mod->mod_flags & MODFLG_MUST_FORCE) &&
    913   1.70  pgoyette 		    (flags & MODCTL_LOAD_FORCE) == 0) {
    914   1.62     pooka 			if (!autoload) {
    915   1.62     pooka 				module_error("use -f to reinstate "
    916   1.90  christos 				    "builtin module `%s'", name);
    917   1.62     pooka 			}
    918   1.59     pooka 			depth--;
    919   1.59     pooka 			return EPERM;
    920   1.59     pooka 		} else {
    921  1.116  christos 			error = module_do_builtin(mod, name, modp, props);
    922   1.59     pooka 			depth--;
    923   1.59     pooka 			return error;
    924   1.59     pooka 		}
    925   1.59     pooka 	}
    926   1.59     pooka 
    927   1.59     pooka 	/*
    928    1.1        ad 	 * Load the module and link.  Before going to the file system,
    929   1.57     pooka 	 * scan the list of modules loaded by the boot loader.
    930    1.1        ad 	 */
    931    1.1        ad 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    932   1.27        ad 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    933    1.1        ad 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    934    1.1        ad 			break;
    935    1.1        ad 		}
    936    1.1        ad 	}
    937   1.14    rumble 	if (mod != NULL) {
    938   1.72  pgoyette 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
    939   1.14    rumble 	} else {
    940   1.27        ad 		/*
    941  1.110  pgoyette 		 * Check to see if module is already present.
    942   1.27        ad 		 */
    943  1.110  pgoyette 		mod = module_lookup(name);
    944  1.110  pgoyette 		if (mod != NULL) {
    945  1.110  pgoyette 			if (modp != NULL) {
    946  1.110  pgoyette 				*modp = mod;
    947   1.27        ad 			}
    948  1.110  pgoyette 			module_print("%s module `%s' already loaded",
    949  1.110  pgoyette 			    isdep ? "dependent" : "requested", name);
    950  1.110  pgoyette 			depth--;
    951  1.110  pgoyette 			return EEXIST;
    952  1.109      maxv 		}
    953  1.110  pgoyette 
    954   1.70  pgoyette 		mod = module_newmodule(MODULE_SOURCE_FILESYS);
    955    1.1        ad 		if (mod == NULL) {
    956   1.38  christos 			module_error("out of memory for `%s'", name);
    957    1.1        ad 			depth--;
    958    1.1        ad 			return ENOMEM;
    959    1.1        ad 		}
    960   1.54     pooka 
    961   1.67  pgoyette 		error = module_load_vfs_vec(name, flags, autoload, mod,
    962   1.67  pgoyette 					    &filedict);
    963    1.1        ad 		if (error != 0) {
    964   1.91  christos #ifdef DEBUG
    965   1.92  christos 			/*
    966   1.92  christos 			 * The exec class of modules contains a list of
    967   1.92  christos 			 * modules that is the union of all the modules
    968   1.92  christos 			 * available for each architecture, so we don't
    969   1.92  christos 			 * print an error if they are missing.
    970   1.92  christos 			 */
    971  1.104  christos 			if ((modclass != MODULE_CLASS_EXEC || error != ENOENT)
    972  1.105  christos 			    && root_device != NULL)
    973   1.92  christos 				module_error("vfs load failed for `%s', "
    974   1.92  christos 				    "error %d", name, error);
    975   1.91  christos #endif
    976    1.1        ad 			kmem_free(mod, sizeof(*mod));
    977    1.1        ad 			depth--;
    978    1.8    rumble 			return error;
    979    1.7    rumble 		}
    980   1.72  pgoyette 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
    981   1.54     pooka 
    982   1.11        ad 		error = module_fetch_info(mod);
    983   1.11        ad 		if (error != 0) {
    984   1.90  christos 			module_error("cannot fetch info for `%s', error %d",
    985   1.90  christos 			    name, error);
    986   1.11        ad 			goto fail;
    987   1.11        ad 		}
    988    1.1        ad 	}
    989    1.1        ad 
    990    1.1        ad 	/*
    991   1.11        ad 	 * Check compatibility.
    992    1.1        ad 	 */
    993    1.1        ad 	mi = mod->mod_info;
    994    1.3    rumble 	if (strlen(mi->mi_name) >= MAXMODNAME) {
    995    1.3    rumble 		error = EINVAL;
    996   1.90  christos 		module_error("module name `%s' longer than %d", mi->mi_name,
    997   1.90  christos 		    MAXMODNAME);
    998    1.3    rumble 		goto fail;
    999    1.3    rumble 	}
   1000   1.21        ad 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
   1001   1.87  christos 		module_error("module `%s' built for `%d', system `%d'",
   1002   1.87  christos 		    mi->mi_name, mi->mi_version, __NetBSD_Version__);
   1003   1.24        ad 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
   1004   1.24        ad 			module_error("forced load, system may be unstable");
   1005   1.24        ad 		} else {
   1006   1.24        ad 			error = EPROGMISMATCH;
   1007   1.24        ad 			goto fail;
   1008   1.24        ad 		}
   1009   1.21        ad 	}
   1010    1.3    rumble 
   1011    1.1        ad 	/*
   1012   1.18        ad 	 * If a specific kind of module was requested, ensure that we have
   1013   1.18        ad 	 * a match.
   1014   1.18        ad 	 */
   1015  1.100      matt 	if (!MODULE_CLASS_MATCH(mi, modclass)) {
   1016  1.100      matt 		module_incompat(mi, modclass);
   1017   1.18        ad 		error = ENOENT;
   1018   1.18        ad 		goto fail;
   1019   1.18        ad 	}
   1020   1.18        ad 
   1021   1.18        ad 	/*
   1022   1.27        ad 	 * If loading a dependency, `name' is a plain module name.
   1023    1.1        ad 	 * The name must match.
   1024    1.1        ad 	 */
   1025   1.27        ad 	if (isdep && strcmp(mi->mi_name, name) != 0) {
   1026   1.32  christos 		module_error("dependency name mismatch (`%s' != `%s')",
   1027   1.32  christos 		    name, mi->mi_name);
   1028    1.1        ad 		error = ENOENT;
   1029    1.1        ad 		goto fail;
   1030    1.1        ad 	}
   1031    1.1        ad 
   1032    1.1        ad 	/*
   1033    1.3    rumble 	 * Block circular dependencies.
   1034    1.3    rumble 	 */
   1035   1.72  pgoyette 	TAILQ_FOREACH(mod2, pending, mod_chain) {
   1036    1.1        ad 		if (mod == mod2) {
   1037    1.1        ad 			continue;
   1038    1.1        ad 		}
   1039    1.1        ad 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
   1040  1.109      maxv 			error = EDEADLK;
   1041   1.32  christos 			module_error("circular dependency detected for `%s'",
   1042   1.32  christos 			    mi->mi_name);
   1043  1.109      maxv 			goto fail;
   1044    1.1        ad 		}
   1045    1.1        ad 	}
   1046    1.1        ad 
   1047    1.1        ad 	/*
   1048    1.1        ad 	 * Now try to load any requisite modules.
   1049    1.1        ad 	 */
   1050    1.1        ad 	if (mi->mi_required != NULL) {
   1051    1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
   1052    1.1        ad 			if (*s == ',')
   1053    1.1        ad 				s++;
   1054    1.1        ad 			p = s;
   1055    1.1        ad 			while (*p != '\0' && *p != ',')
   1056    1.1        ad 				p++;
   1057    1.3    rumble 			len = p - s + 1;
   1058    1.3    rumble 			if (len >= MAXMODNAME) {
   1059    1.3    rumble 				error = EINVAL;
   1060   1.90  christos 				module_error("required module name `%s' "
   1061   1.90  christos 				    "longer than %d", mi->mi_required,
   1062   1.90  christos 				    MAXMODNAME);
   1063    1.3    rumble 				goto fail;
   1064    1.3    rumble 			}
   1065    1.1        ad 			strlcpy(buf, s, len);
   1066    1.1        ad 			if (buf[0] == '\0')
   1067    1.1        ad 				break;
   1068    1.1        ad 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
   1069    1.1        ad 				error = EINVAL;
   1070   1.90  christos 				module_error("too many required modules "
   1071   1.90  christos 				    "%d >= %d", mod->mod_nrequired,
   1072   1.90  christos 				    MAXMODDEPS - 1);
   1073    1.1        ad 				goto fail;
   1074    1.1        ad 			}
   1075    1.6    rumble 			if (strcmp(buf, mi->mi_name) == 0) {
   1076    1.6    rumble 				error = EDEADLK;
   1077   1.32  christos 				module_error("self-dependency detected for "
   1078   1.32  christos 				   "`%s'", mi->mi_name);
   1079    1.6    rumble 				goto fail;
   1080    1.6    rumble 			}
   1081    1.9      jmmv 			error = module_do_load(buf, true, flags, NULL,
   1082   1.81  christos 			    &mod2, MODULE_CLASS_ANY, true);
   1083  1.110  pgoyette 			if (error != 0 && error != EEXIST) {
   1084   1.96      maxv 				module_error("recursive load failed for `%s' "
   1085   1.96      maxv 				    "(`%s' required), error %d", mi->mi_name,
   1086   1.96      maxv 				    buf, error);
   1087    1.1        ad 				goto fail;
   1088   1.88  christos 			}
   1089   1.81  christos 			mod->mod_required[mod->mod_nrequired++] = mod2;
   1090    1.1        ad 		}
   1091    1.1        ad 	}
   1092    1.1        ad 
   1093    1.1        ad 	/*
   1094   1.15        ad 	 * We loaded all needed modules successfully: perform global
   1095   1.15        ad 	 * relocations and initialize.
   1096    1.1        ad 	 */
   1097   1.15        ad 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
   1098   1.15        ad 	if (error != 0) {
   1099   1.36        ad 		/* Cannot touch 'mi' as the module is now gone. */
   1100   1.90  christos 		module_error("unable to affix module `%s', error %d", name,
   1101   1.90  christos 		    error);
   1102   1.15        ad 		goto fail2;
   1103   1.15        ad 	}
   1104   1.15        ad 
   1105   1.54     pooka 	if (filedict) {
   1106   1.54     pooka 		if (!module_merge_dicts(filedict, props)) {
   1107   1.90  christos 			module_error("module properties failed for %s", name);
   1108   1.54     pooka 			error = EINVAL;
   1109   1.46   jnemeth 			goto fail;
   1110   1.46   jnemeth 		}
   1111   1.46   jnemeth 	}
   1112   1.72  pgoyette 	prev_active = module_active;
   1113   1.12        ad 	module_active = mod;
   1114   1.54     pooka 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
   1115   1.72  pgoyette 	module_active = prev_active;
   1116   1.54     pooka 	if (filedict) {
   1117   1.46   jnemeth 		prop_object_release(filedict);
   1118   1.54     pooka 		filedict = NULL;
   1119   1.46   jnemeth 	}
   1120    1.1        ad 	if (error != 0) {
   1121   1.90  christos 		module_error("modcmd function failed for `%s', error %d",
   1122   1.90  christos 		    mi->mi_name, error);
   1123    1.1        ad 		goto fail;
   1124    1.1        ad 	}
   1125   1.54     pooka 
   1126    1.1        ad 	/*
   1127    1.1        ad 	 * Good, the module loaded successfully.  Put it onto the
   1128    1.1        ad 	 * list and add references to its requisite modules.
   1129    1.1        ad 	 */
   1130   1.72  pgoyette 	TAILQ_REMOVE(pending, mod, mod_chain);
   1131   1.27        ad 	module_enqueue(mod);
   1132    1.1        ad 	if (modp != NULL) {
   1133    1.1        ad 		*modp = mod;
   1134    1.1        ad 	}
   1135   1.94  pgoyette 	if (autoload && module_autotime > 0) {
   1136   1.26        ad 		/*
   1137   1.26        ad 		 * Arrange to try unloading the module after
   1138   1.94  pgoyette 		 * a short delay unless auto-unload is disabled.
   1139   1.26        ad 		 */
   1140   1.26        ad 		mod->mod_autotime = time_second + module_autotime;
   1141   1.83  jmcneill 		mod->mod_flags |= MODFLG_AUTO_LOADED;
   1142   1.26        ad 		module_thread_kick();
   1143   1.26        ad 	}
   1144    1.1        ad 	depth--;
   1145  1.107  pgoyette 	module_print("module `%s' loaded successfully", mi->mi_name);
   1146    1.1        ad 	return 0;
   1147    1.7    rumble 
   1148    1.1        ad  fail:
   1149   1.15        ad 	kobj_unload(mod->mod_kobj);
   1150   1.15        ad  fail2:
   1151   1.54     pooka 	if (filedict != NULL) {
   1152   1.54     pooka 		prop_object_release(filedict);
   1153   1.54     pooka 		filedict = NULL;
   1154   1.54     pooka 	}
   1155   1.72  pgoyette 	TAILQ_REMOVE(pending, mod, mod_chain);
   1156    1.1        ad 	kmem_free(mod, sizeof(*mod));
   1157    1.1        ad 	depth--;
   1158    1.1        ad 	return error;
   1159    1.1        ad }
   1160    1.1        ad 
   1161    1.1        ad /*
   1162    1.1        ad  * module_do_unload:
   1163    1.1        ad  *
   1164    1.1        ad  *	Helper routine: do the dirty work of unloading a module.
   1165    1.1        ad  */
   1166    1.1        ad static int
   1167   1.70  pgoyette module_do_unload(const char *name, bool load_requires_force)
   1168    1.1        ad {
   1169   1.72  pgoyette 	module_t *mod, *prev_active;
   1170    1.1        ad 	int error;
   1171    1.1        ad 	u_int i;
   1172    1.1        ad 
   1173   1.72  pgoyette 	KASSERT(kernconfig_is_held());
   1174   1.85   jnemeth 	KASSERT(name != NULL);
   1175    1.1        ad 
   1176  1.107  pgoyette 	module_print("unload requested for '%s' (%s)", name,
   1177  1.117  christos 	    load_requires_force ? "TRUE" : "FALSE");
   1178    1.1        ad 	mod = module_lookup(name);
   1179    1.1        ad 	if (mod == NULL) {
   1180   1.32  christos 		module_error("module `%s' not found", name);
   1181    1.1        ad 		return ENOENT;
   1182    1.1        ad 	}
   1183   1.59     pooka 	if (mod->mod_refcnt != 0) {
   1184  1.107  pgoyette 		module_print("module `%s' busy (%d refs)", name,
   1185  1.108  pgoyette 		    mod->mod_refcnt);
   1186    1.1        ad 		return EBUSY;
   1187    1.1        ad 	}
   1188   1.76     pooka 
   1189   1.76     pooka 	/*
   1190   1.76     pooka 	 * Builtin secmodels are there to stay.
   1191   1.76     pooka 	 */
   1192   1.76     pooka 	if (mod->mod_source == MODULE_SOURCE_KERNEL &&
   1193   1.76     pooka 	    mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) {
   1194  1.107  pgoyette 		module_print("cannot unload built-in secmodel module `%s'",
   1195  1.107  pgoyette 		    name);
   1196   1.76     pooka 		return EPERM;
   1197   1.76     pooka 	}
   1198   1.76     pooka 
   1199   1.72  pgoyette 	prev_active = module_active;
   1200   1.12        ad 	module_active = mod;
   1201    1.1        ad 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
   1202   1.72  pgoyette 	module_active = prev_active;
   1203    1.1        ad 	if (error != 0) {
   1204   1.34        ad 		module_print("cannot unload module `%s' error=%d", name,
   1205   1.33        ad 		    error);
   1206    1.1        ad 		return error;
   1207    1.1        ad 	}
   1208    1.1        ad 	module_count--;
   1209    1.1        ad 	TAILQ_REMOVE(&module_list, mod, mod_chain);
   1210    1.1        ad 	for (i = 0; i < mod->mod_nrequired; i++) {
   1211    1.1        ad 		mod->mod_required[i]->mod_refcnt--;
   1212    1.1        ad 	}
   1213   1.85   jnemeth 	module_print("unloaded module `%s'", name);
   1214    1.1        ad 	if (mod->mod_kobj != NULL) {
   1215    1.1        ad 		kobj_unload(mod->mod_kobj);
   1216    1.1        ad 	}
   1217   1.59     pooka 	if (mod->mod_source == MODULE_SOURCE_KERNEL) {
   1218   1.59     pooka 		mod->mod_nrequired = 0; /* will be re-parsed */
   1219   1.70  pgoyette 		if (load_requires_force)
   1220   1.70  pgoyette 			module_require_force(mod);
   1221   1.59     pooka 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
   1222   1.59     pooka 		module_builtinlist++;
   1223   1.59     pooka 	} else {
   1224   1.59     pooka 		kmem_free(mod, sizeof(*mod));
   1225   1.59     pooka 	}
   1226   1.26        ad 	module_gen++;
   1227    1.1        ad 
   1228    1.1        ad 	return 0;
   1229    1.1        ad }
   1230    1.1        ad 
   1231    1.1        ad /*
   1232    1.1        ad  * module_prime:
   1233    1.1        ad  *
   1234    1.1        ad  *	Push a module loaded by the bootloader onto our internal
   1235    1.1        ad  *	list.
   1236    1.1        ad  */
   1237    1.1        ad int
   1238   1.80  christos module_prime(const char *name, void *base, size_t size)
   1239    1.1        ad {
   1240  1.111  pgoyette 	__link_set_decl(modules, modinfo_t);
   1241  1.111  pgoyette 	modinfo_t *const *mip;
   1242    1.1        ad 	module_t *mod;
   1243    1.1        ad 	int error;
   1244    1.1        ad 
   1245  1.112  pgoyette 	/* Check for module name same as a built-in module */
   1246  1.111  pgoyette 
   1247  1.111  pgoyette 	__link_set_foreach(mip, modules) {
   1248  1.111  pgoyette 		if (*mip == &module_dummy)
   1249  1.111  pgoyette 			continue;
   1250  1.111  pgoyette 		if (strcmp((*mip)->mi_name, name) == 0) {
   1251  1.111  pgoyette 			module_error("module `%s' pushed by boot loader "
   1252  1.111  pgoyette 			    "already exists", name);
   1253  1.111  pgoyette 			return EEXIST;
   1254  1.111  pgoyette 		}
   1255  1.111  pgoyette 	}
   1256  1.112  pgoyette 
   1257  1.112  pgoyette 	/* Also eliminate duplicate boolist entries */
   1258  1.112  pgoyette 
   1259  1.112  pgoyette 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
   1260  1.112  pgoyette 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
   1261  1.112  pgoyette 			module_error("duplicate bootlist entry for module "
   1262  1.112  pgoyette 			    "`%s'", name);
   1263  1.112  pgoyette 			return EEXIST;
   1264  1.112  pgoyette 		}
   1265  1.112  pgoyette 	}
   1266  1.112  pgoyette 
   1267  1.112  pgoyette 	mod = module_newmodule(MODULE_SOURCE_BOOT);
   1268  1.112  pgoyette 	if (mod == NULL) {
   1269  1.112  pgoyette 		return ENOMEM;
   1270  1.112  pgoyette 	}
   1271  1.112  pgoyette 
   1272   1.80  christos 	error = kobj_load_mem(&mod->mod_kobj, name, base, size);
   1273    1.1        ad 	if (error != 0) {
   1274   1.11        ad 		kmem_free(mod, sizeof(*mod));
   1275   1.90  christos 		module_error("unable to load `%s' pushed by boot loader, "
   1276   1.90  christos 		    "error %d", name, error);
   1277   1.11        ad 		return error;
   1278   1.11        ad 	}
   1279   1.11        ad 	error = module_fetch_info(mod);
   1280   1.11        ad 	if (error != 0) {
   1281   1.11        ad 		kobj_unload(mod->mod_kobj);
   1282    1.1        ad 		kmem_free(mod, sizeof(*mod));
   1283  1.111  pgoyette 		module_error("unable to fetch_info for `%s' pushed by boot "
   1284  1.111  pgoyette 		    "loader, error %d", name, error);
   1285    1.1        ad 		return error;
   1286    1.1        ad 	}
   1287   1.11        ad 
   1288    1.1        ad 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
   1289    1.1        ad 
   1290    1.1        ad 	return 0;
   1291    1.1        ad }
   1292   1.11        ad 
   1293   1.11        ad /*
   1294   1.11        ad  * module_fetch_into:
   1295   1.11        ad  *
   1296   1.11        ad  *	Fetch modinfo record from a loaded module.
   1297   1.11        ad  */
   1298   1.11        ad static int
   1299   1.11        ad module_fetch_info(module_t *mod)
   1300   1.11        ad {
   1301   1.11        ad 	int error;
   1302   1.11        ad 	void *addr;
   1303   1.11        ad 	size_t size;
   1304   1.11        ad 
   1305   1.11        ad 	/*
   1306   1.11        ad 	 * Find module info record and check compatibility.
   1307   1.11        ad 	 */
   1308   1.11        ad 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
   1309   1.11        ad 	    &addr, &size);
   1310   1.11        ad 	if (error != 0) {
   1311   1.90  christos 		module_error("`link_set_modules' section not present, "
   1312   1.90  christos 		    "error %d", error);
   1313   1.11        ad 		return error;
   1314   1.11        ad 	}
   1315   1.11        ad 	if (size != sizeof(modinfo_t **)) {
   1316   1.90  christos 		module_error("`link_set_modules' section wrong size %zu != %zu",
   1317   1.90  christos 		    size, sizeof(modinfo_t **));
   1318   1.84      tron 		return ENOEXEC;
   1319   1.11        ad 	}
   1320   1.11        ad 	mod->mod_info = *(modinfo_t **)addr;
   1321   1.11        ad 
   1322   1.11        ad 	return 0;
   1323   1.11        ad }
   1324   1.12        ad 
   1325   1.12        ad /*
   1326   1.12        ad  * module_find_section:
   1327   1.12        ad  *
   1328   1.12        ad  *	Allows a module that is being initialized to look up a section
   1329   1.12        ad  *	within its ELF object.
   1330   1.12        ad  */
   1331   1.12        ad int
   1332   1.12        ad module_find_section(const char *name, void **addr, size_t *size)
   1333   1.12        ad {
   1334   1.12        ad 
   1335   1.72  pgoyette 	KASSERT(kernconfig_is_held());
   1336   1.12        ad 	KASSERT(module_active != NULL);
   1337   1.12        ad 
   1338   1.12        ad 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
   1339   1.12        ad }
   1340   1.26        ad 
   1341   1.26        ad /*
   1342   1.26        ad  * module_thread:
   1343   1.26        ad  *
   1344   1.26        ad  *	Automatically unload modules.  We try once to unload autoloaded
   1345   1.26        ad  *	modules after module_autotime seconds.  If the system is under
   1346   1.94  pgoyette  *	severe memory pressure, we'll try unloading all modules, else if
   1347   1.94  pgoyette  *	module_autotime is zero, we don't try to unload, even if the
   1348   1.94  pgoyette  *	module was previously scheduled for unload.
   1349   1.26        ad  */
   1350   1.26        ad static void
   1351   1.26        ad module_thread(void *cookie)
   1352   1.26        ad {
   1353   1.26        ad 	module_t *mod, *next;
   1354   1.28        ad 	modinfo_t *mi;
   1355   1.28        ad 	int error;
   1356   1.26        ad 
   1357   1.26        ad 	for (;;) {
   1358   1.72  pgoyette 		kernconfig_lock();
   1359   1.26        ad 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
   1360   1.26        ad 			next = TAILQ_NEXT(mod, mod_chain);
   1361   1.83  jmcneill 
   1362   1.83  jmcneill 			/* skip built-in modules */
   1363   1.61     pooka 			if (mod->mod_source == MODULE_SOURCE_KERNEL)
   1364   1.61     pooka 				continue;
   1365   1.83  jmcneill 			/* skip modules that weren't auto-loaded */
   1366   1.83  jmcneill 			if ((mod->mod_flags & MODFLG_AUTO_LOADED) == 0)
   1367   1.83  jmcneill 				continue;
   1368   1.83  jmcneill 
   1369   1.37        ad 			if (uvmexp.free < uvmexp.freemin) {
   1370   1.26        ad 				module_thread_ticks = hz;
   1371   1.94  pgoyette 			} else if (module_autotime == 0 ||
   1372   1.94  pgoyette 				   mod->mod_autotime == 0) {
   1373   1.26        ad 				continue;
   1374   1.26        ad 			} else if (time_second < mod->mod_autotime) {
   1375   1.26        ad 				module_thread_ticks = hz;
   1376   1.26        ad 			    	continue;
   1377   1.37        ad 			} else {
   1378   1.26        ad 				mod->mod_autotime = 0;
   1379   1.37        ad 			}
   1380   1.83  jmcneill 
   1381   1.28        ad 			/*
   1382   1.28        ad 			 * If this module wants to avoid autounload then
   1383   1.28        ad 			 * skip it.  Some modules can ping-pong in and out
   1384   1.28        ad 			 * because their use is transient but often.
   1385   1.28        ad 			 * Example: exec_script.
   1386   1.28        ad 			 */
   1387   1.28        ad 			mi = mod->mod_info;
   1388   1.28        ad 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
   1389   1.28        ad 			if (error == 0 || error == ENOTTY) {
   1390   1.70  pgoyette 				(void)module_do_unload(mi->mi_name, false);
   1391  1.107  pgoyette 			} else
   1392  1.107  pgoyette 				module_print("module `%s' declined to be "
   1393  1.107  pgoyette 				    "auto-unloaded error=%d", mi->mi_name,
   1394  1.107  pgoyette 				    error);
   1395   1.26        ad 		}
   1396   1.72  pgoyette 		kernconfig_unlock();
   1397   1.26        ad 
   1398   1.26        ad 		mutex_enter(&module_thread_lock);
   1399   1.26        ad 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
   1400   1.26        ad 		    module_thread_ticks);
   1401   1.26        ad 		module_thread_ticks = 0;
   1402   1.26        ad 		mutex_exit(&module_thread_lock);
   1403   1.26        ad 	}
   1404   1.26        ad }
   1405   1.26        ad 
   1406   1.26        ad /*
   1407   1.26        ad  * module_thread:
   1408   1.26        ad  *
   1409   1.26        ad  *	Kick the module thread into action, perhaps because the
   1410   1.26        ad  *	system is low on memory.
   1411   1.26        ad  */
   1412   1.26        ad void
   1413   1.26        ad module_thread_kick(void)
   1414   1.26        ad {
   1415   1.26        ad 
   1416   1.26        ad 	mutex_enter(&module_thread_lock);
   1417   1.26        ad 	module_thread_ticks = hz;
   1418   1.26        ad 	cv_broadcast(&module_thread_cv);
   1419   1.26        ad 	mutex_exit(&module_thread_lock);
   1420   1.26        ad }
   1421   1.30        ad 
   1422   1.30        ad #ifdef DDB
   1423   1.30        ad /*
   1424   1.30        ad  * module_whatis:
   1425   1.30        ad  *
   1426   1.30        ad  *	Helper routine for DDB.
   1427   1.30        ad  */
   1428   1.30        ad void
   1429   1.30        ad module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1430   1.30        ad {
   1431   1.30        ad 	module_t *mod;
   1432   1.30        ad 	size_t msize;
   1433   1.30        ad 	vaddr_t maddr;
   1434   1.30        ad 
   1435   1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1436   1.43        ad 		if (mod->mod_kobj == NULL) {
   1437   1.43        ad 			continue;
   1438   1.43        ad 		}
   1439   1.49    dyoung 		if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1440   1.49    dyoung 			continue;
   1441   1.30        ad 		if (addr < maddr || addr >= maddr + msize) {
   1442   1.30        ad 			continue;
   1443   1.30        ad 		}
   1444   1.30        ad 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
   1445   1.30        ad 		    (void *)addr, (void *)maddr,
   1446   1.30        ad 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
   1447   1.30        ad 	}
   1448   1.30        ad }
   1449   1.30        ad 
   1450   1.30        ad /*
   1451   1.30        ad  * module_print_list:
   1452   1.30        ad  *
   1453   1.30        ad  *	Helper routine for DDB.
   1454   1.30        ad  */
   1455   1.30        ad void
   1456   1.30        ad module_print_list(void (*pr)(const char *, ...))
   1457   1.30        ad {
   1458   1.30        ad 	const char *src;
   1459   1.30        ad 	module_t *mod;
   1460   1.30        ad 	size_t msize;
   1461   1.30        ad 	vaddr_t maddr;
   1462   1.30        ad 
   1463   1.30        ad 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
   1464   1.30        ad 
   1465   1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1466   1.30        ad 		switch (mod->mod_source) {
   1467   1.30        ad 		case MODULE_SOURCE_KERNEL:
   1468   1.30        ad 			src = "builtin";
   1469   1.30        ad 			break;
   1470   1.30        ad 		case MODULE_SOURCE_FILESYS:
   1471   1.30        ad 			src = "filesys";
   1472   1.30        ad 			break;
   1473   1.30        ad 		case MODULE_SOURCE_BOOT:
   1474   1.30        ad 			src = "boot";
   1475   1.30        ad 			break;
   1476   1.30        ad 		default:
   1477   1.30        ad 			src = "unknown";
   1478   1.30        ad 			break;
   1479   1.30        ad 		}
   1480   1.49    dyoung 		if (mod->mod_kobj == NULL) {
   1481   1.43        ad 			maddr = 0;
   1482   1.43        ad 			msize = 0;
   1483   1.49    dyoung 		} else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1484   1.49    dyoung 			continue;
   1485   1.31        ad 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
   1486   1.30        ad 		    (long)maddr, (long)msize, src);
   1487   1.30        ad 	}
   1488   1.30        ad }
   1489   1.30        ad #endif	/* DDB */
   1490   1.46   jnemeth 
   1491   1.47   jnemeth static bool
   1492   1.47   jnemeth module_merge_dicts(prop_dictionary_t existing_dict,
   1493   1.47   jnemeth 		   const prop_dictionary_t new_dict)
   1494   1.47   jnemeth {
   1495   1.47   jnemeth 	prop_dictionary_keysym_t props_keysym;
   1496   1.47   jnemeth 	prop_object_iterator_t props_iter;
   1497   1.47   jnemeth 	prop_object_t props_obj;
   1498   1.47   jnemeth 	const char *props_key;
   1499   1.47   jnemeth 	bool error;
   1500   1.47   jnemeth 
   1501   1.52   jnemeth 	if (new_dict == NULL) {			/* nothing to merge */
   1502   1.52   jnemeth 		return true;
   1503   1.52   jnemeth 	}
   1504   1.52   jnemeth 
   1505   1.47   jnemeth 	error = false;
   1506   1.47   jnemeth 	props_iter = prop_dictionary_iterator(new_dict);
   1507   1.47   jnemeth 	if (props_iter == NULL) {
   1508   1.47   jnemeth 		return false;
   1509   1.47   jnemeth 	}
   1510   1.47   jnemeth 
   1511   1.47   jnemeth 	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
   1512   1.47   jnemeth 		props_keysym = (prop_dictionary_keysym_t)props_obj;
   1513   1.47   jnemeth 		props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
   1514   1.47   jnemeth 		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
   1515   1.47   jnemeth 		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
   1516   1.47   jnemeth 		    props_key, props_obj)) {
   1517   1.47   jnemeth 			error = true;
   1518   1.47   jnemeth 			goto out;
   1519   1.47   jnemeth 		}
   1520   1.47   jnemeth 	}
   1521   1.47   jnemeth 	error = false;
   1522   1.47   jnemeth 
   1523   1.47   jnemeth out:
   1524   1.47   jnemeth 	prop_object_iterator_release(props_iter);
   1525   1.47   jnemeth 
   1526   1.47   jnemeth 	return !error;
   1527   1.47   jnemeth }
   1528