Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.161
      1  1.161  riastrad /*	$NetBSD: kern_module.c,v 1.161 2023/01/31 13:21:37 riastradh 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.161  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.161 2023/01/31 13:21:37 riastradh 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.146  pgoyette #include <sys/lwp.h>
     51    1.1        ad #include <sys/kauth.h>
     52    1.1        ad #include <sys/kobj.h>
     53    1.1        ad #include <sys/kmem.h>
     54    1.1        ad #include <sys/module.h>
     55  1.140  pgoyette #include <sys/module_hook.h>
     56   1.26        ad #include <sys/kthread.h>
     57   1.34        ad #include <sys/sysctl.h>
     58   1.46   jnemeth #include <sys/lock.h>
     59  1.147  pgoyette #include <sys/evcnt.h>
     60    1.1        ad 
     61    1.1        ad #include <uvm/uvm_extern.h>
     62    1.1        ad 
     63   1.25        ad struct vm_map *module_map;
     64  1.106      matt const char *module_machine;
     65   1.54     pooka char	module_base[MODULE_BASE_SIZE];
     66    1.2        ad 
     67   1.59     pooka struct modlist        module_list = TAILQ_HEAD_INITIALIZER(module_list);
     68   1.59     pooka struct modlist        module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
     69   1.59     pooka static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     70   1.59     pooka 
     71  1.131       chs struct module_callbacks {
     72  1.131       chs 	TAILQ_ENTRY(module_callbacks) modcb_list;
     73  1.131       chs 	void (*modcb_load)(struct module *);
     74  1.131       chs 	void (*modcb_unload)(struct module *);
     75  1.131       chs };
     76  1.131       chs TAILQ_HEAD(modcblist, module_callbacks);
     77  1.131       chs static struct modcblist modcblist;
     78  1.131       chs 
     79  1.131       chs static module_t *module_netbsd;
     80  1.131       chs static const modinfo_t module_netbsd_modinfo = {
     81  1.131       chs 	.mi_version = __NetBSD_Version__,
     82  1.131       chs 	.mi_class = MODULE_CLASS_MISC,
     83  1.131       chs 	.mi_name = "netbsd"
     84  1.131       chs };
     85  1.131       chs 
     86   1.12        ad static module_t	*module_active;
     87  1.154       rin #ifdef MODULAR_DEFAULT_VERBOSE
     88  1.154       rin bool		module_verbose_on = true;
     89  1.154       rin #else
     90  1.154       rin bool		module_verbose_on = false;
     91  1.154       rin #endif
     92   1.99    nonaka #ifdef MODULAR_DEFAULT_AUTOLOAD
     93  1.114  pgoyette bool		module_autoload_on = true;
     94   1.98   jnemeth #else
     95  1.114  pgoyette bool		module_autoload_on = false;
     96   1.98   jnemeth #endif
     97  1.158  riastrad bool		module_autounload_unsafe = 0;
     98    1.1        ad u_int		module_count;
     99   1.59     pooka u_int		module_builtinlist;
    100  1.157  riastrad u_int		module_autotime = 10;
    101   1.26        ad u_int		module_gen = 1;
    102   1.26        ad static kcondvar_t module_thread_cv;
    103   1.26        ad static kmutex_t module_thread_lock;
    104   1.26        ad static int	module_thread_ticks;
    105   1.68  pgoyette int (*module_load_vfs_vec)(const char *, int, bool, module_t *,
    106  1.115   msaitoh 			   prop_dictionary_t *) = (void *)eopnotsupp;
    107    1.1        ad 
    108   1.51      elad static kauth_listener_t	module_listener;
    109   1.51      elad 
    110  1.131       chs static specificdata_domain_t module_specificdata_domain;
    111  1.131       chs 
    112    1.1        ad /* Ensure that the kernel's link set isn't empty. */
    113    1.1        ad static modinfo_t module_dummy;
    114    1.1        ad __link_set_add_rodata(modules, module_dummy);
    115    1.1        ad 
    116   1.70  pgoyette static module_t	*module_newmodule(modsrc_t);
    117  1.131       chs static void	module_free(module_t *);
    118   1.70  pgoyette static void	module_require_force(module_t *);
    119    1.9      jmmv static int	module_do_load(const char *, bool, int, prop_dictionary_t,
    120  1.100      matt 		    module_t **, modclass_t modclass, bool);
    121   1.70  pgoyette static int	module_do_unload(const char *, bool);
    122  1.116  christos static int	module_do_builtin(const module_t *, const char *, module_t **,
    123  1.116  christos     prop_dictionary_t);
    124   1.11        ad static int	module_fetch_info(module_t *);
    125   1.26        ad static void	module_thread(void *);
    126   1.54     pooka 
    127   1.60     pooka static module_t	*module_lookup(const char *);
    128   1.60     pooka static void	module_enqueue(module_t *);
    129   1.60     pooka 
    130   1.47   jnemeth static bool	module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
    131    1.1        ad 
    132   1.69     pooka static void	sysctl_module_setup(void);
    133   1.94  pgoyette static int	sysctl_module_autotime(SYSCTLFN_PROTO);
    134   1.94  pgoyette 
    135  1.131       chs static void	module_callback_load(struct module *);
    136  1.131       chs static void	module_callback_unload(struct module *);
    137  1.131       chs 
    138  1.100      matt #define MODULE_CLASS_MATCH(mi, modclass) \
    139  1.100      matt 	((modclass) == MODULE_CLASS_ANY || (modclass) == (mi)->mi_class)
    140   1.88  christos 
    141   1.88  christos static void
    142  1.100      matt module_incompat(const modinfo_t *mi, int modclass)
    143   1.88  christos {
    144  1.148  pgoyette 	module_error("incompatible module class %d for `%s' (wanted %d)",
    145  1.148  pgoyette 	    mi->mi_class, mi->mi_name, modclass);
    146   1.88  christos }
    147   1.69     pooka 
    148  1.131       chs struct module *
    149  1.131       chs module_kernel(void)
    150  1.131       chs {
    151  1.131       chs 
    152  1.131       chs 	return module_netbsd;
    153  1.131       chs }
    154  1.131       chs 
    155    1.1        ad /*
    156    1.1        ad  * module_error:
    157    1.1        ad  *
    158    1.1        ad  *	Utility function: log an error.
    159    1.1        ad  */
    160   1.54     pooka void
    161    1.1        ad module_error(const char *fmt, ...)
    162    1.1        ad {
    163    1.1        ad 	va_list ap;
    164    1.1        ad 
    165    1.1        ad 	va_start(ap, fmt);
    166    1.1        ad 	printf("WARNING: module error: ");
    167    1.1        ad 	vprintf(fmt, ap);
    168    1.1        ad 	printf("\n");
    169    1.1        ad 	va_end(ap);
    170    1.1        ad }
    171    1.1        ad 
    172    1.1        ad /*
    173   1.34        ad  * module_print:
    174   1.34        ad  *
    175   1.34        ad  *	Utility function: log verbose output.
    176   1.34        ad  */
    177   1.54     pooka void
    178   1.34        ad module_print(const char *fmt, ...)
    179   1.34        ad {
    180   1.34        ad 	va_list ap;
    181   1.34        ad 
    182   1.34        ad 	if (module_verbose_on) {
    183   1.34        ad 		va_start(ap, fmt);
    184   1.34        ad 		printf("DEBUG: module: ");
    185   1.34        ad 		vprintf(fmt, ap);
    186   1.34        ad 		printf("\n");
    187   1.34        ad 		va_end(ap);
    188   1.34        ad 	}
    189   1.34        ad }
    190   1.34        ad 
    191  1.131       chs /*
    192  1.131       chs  * module_name:
    193  1.131       chs  *
    194  1.131       chs  *	Utility function: return the module's name.
    195  1.131       chs  */
    196  1.131       chs const char *
    197  1.131       chs module_name(struct module *mod)
    198  1.131       chs {
    199  1.131       chs 
    200  1.131       chs 	return mod->mod_info->mi_name;
    201  1.131       chs }
    202  1.131       chs 
    203  1.131       chs /*
    204  1.131       chs  * module_source:
    205  1.131       chs  *
    206  1.131       chs  *	Utility function: return the module's source.
    207  1.131       chs  */
    208  1.131       chs modsrc_t
    209  1.131       chs module_source(struct module *mod)
    210  1.131       chs {
    211  1.131       chs 
    212  1.131       chs 	return mod->mod_source;
    213  1.131       chs }
    214  1.131       chs 
    215   1.55      elad static int
    216   1.55      elad module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    217   1.55      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    218   1.55      elad {
    219   1.55      elad 	int result;
    220   1.55      elad 
    221   1.55      elad 	result = KAUTH_RESULT_DEFER;
    222   1.55      elad 
    223   1.55      elad 	if (action != KAUTH_SYSTEM_MODULE)
    224   1.55      elad 		return result;
    225   1.55      elad 
    226   1.55      elad 	if ((uintptr_t)arg2 != 0)	/* autoload */
    227   1.55      elad 		result = KAUTH_RESULT_ALLOW;
    228   1.55      elad 
    229   1.55      elad 	return result;
    230   1.55      elad }
    231   1.55      elad 
    232   1.34        ad /*
    233   1.70  pgoyette  * Allocate a new module_t
    234   1.70  pgoyette  */
    235   1.70  pgoyette static module_t *
    236   1.70  pgoyette module_newmodule(modsrc_t source)
    237   1.70  pgoyette {
    238   1.70  pgoyette 	module_t *mod;
    239   1.70  pgoyette 
    240   1.70  pgoyette 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    241  1.125       chs 	mod->mod_source = source;
    242  1.131       chs 	specificdata_init(module_specificdata_domain, &mod->mod_sdref);
    243   1.70  pgoyette 	return mod;
    244   1.70  pgoyette }
    245   1.70  pgoyette 
    246   1.70  pgoyette /*
    247  1.131       chs  * Free a module_t
    248  1.131       chs  */
    249  1.131       chs static void
    250  1.131       chs module_free(module_t *mod)
    251  1.131       chs {
    252  1.131       chs 
    253  1.131       chs 	specificdata_fini(module_specificdata_domain, &mod->mod_sdref);
    254  1.133  pgoyette 	if (mod->mod_required)
    255  1.133  pgoyette 		kmem_free(mod->mod_required, mod->mod_arequired *
    256  1.133  pgoyette 		    sizeof(module_t *));
    257  1.131       chs 	kmem_free(mod, sizeof(*mod));
    258  1.131       chs }
    259  1.131       chs 
    260  1.131       chs /*
    261   1.70  pgoyette  * Require the -f (force) flag to load a module
    262   1.70  pgoyette  */
    263   1.70  pgoyette static void
    264   1.70  pgoyette module_require_force(struct module *mod)
    265   1.70  pgoyette {
    266  1.133  pgoyette 	SET(mod->mod_flags, MODFLG_MUST_FORCE);
    267   1.70  pgoyette }
    268   1.70  pgoyette 
    269   1.70  pgoyette /*
    270   1.59     pooka  * Add modules to the builtin list.  This can done at boottime or
    271   1.59     pooka  * at runtime if the module is linked into the kernel with an
    272   1.59     pooka  * external linker.  All or none of the input will be handled.
    273   1.59     pooka  * Optionally, the modules can be initialized.  If they are not
    274   1.59     pooka  * initialized, module_init_class() or module_load() can be used
    275   1.59     pooka  * later, but these are not guaranteed to give atomic results.
    276   1.59     pooka  */
    277   1.59     pooka int
    278   1.59     pooka module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
    279   1.59     pooka {
    280   1.59     pooka 	struct module **modp = NULL, *mod_iter;
    281   1.59     pooka 	int rv = 0, i, mipskip;
    282   1.59     pooka 
    283   1.59     pooka 	if (init) {
    284   1.59     pooka 		rv = kauth_authorize_system(kauth_cred_get(),
    285   1.59     pooka 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
    286   1.59     pooka 		    (void *)(uintptr_t)1, NULL);
    287   1.59     pooka 		if (rv) {
    288   1.59     pooka 			return rv;
    289   1.59     pooka 		}
    290   1.59     pooka 	}
    291   1.59     pooka 
    292   1.59     pooka 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    293   1.59     pooka 		if (mip[i] == &module_dummy) {
    294   1.59     pooka 			KASSERT(nmodinfo > 0);
    295   1.59     pooka 			nmodinfo--;
    296   1.59     pooka 		}
    297   1.59     pooka 	}
    298   1.59     pooka 	if (nmodinfo == 0)
    299   1.59     pooka 		return 0;
    300   1.59     pooka 
    301   1.59     pooka 	modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
    302   1.59     pooka 	for (i = 0, mipskip = 0; i < nmodinfo; i++) {
    303   1.59     pooka 		if (mip[i+mipskip] == &module_dummy) {
    304   1.59     pooka 			mipskip++;
    305   1.59     pooka 			continue;
    306   1.59     pooka 		}
    307   1.70  pgoyette 		modp[i] = module_newmodule(MODULE_SOURCE_KERNEL);
    308   1.59     pooka 		modp[i]->mod_info = mip[i+mipskip];
    309   1.59     pooka 	}
    310   1.72  pgoyette 	kernconfig_lock();
    311   1.59     pooka 
    312   1.59     pooka 	/* do this in three stages for error recovery and atomicity */
    313   1.59     pooka 
    314   1.59     pooka 	/* first check for presence */
    315   1.59     pooka 	for (i = 0; i < nmodinfo; i++) {
    316   1.59     pooka 		TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
    317   1.59     pooka 			if (strcmp(mod_iter->mod_info->mi_name,
    318   1.59     pooka 			    modp[i]->mod_info->mi_name) == 0)
    319   1.59     pooka 				break;
    320   1.59     pooka 		}
    321   1.59     pooka 		if (mod_iter) {
    322   1.59     pooka 			rv = EEXIST;
    323   1.59     pooka 			goto out;
    324   1.59     pooka 		}
    325   1.59     pooka 
    326   1.59     pooka 		if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
    327   1.59     pooka 			rv = EEXIST;
    328   1.59     pooka 			goto out;
    329   1.59     pooka 		}
    330   1.59     pooka 	}
    331   1.59     pooka 
    332   1.59     pooka 	/* then add to list */
    333   1.59     pooka 	for (i = 0; i < nmodinfo; i++) {
    334   1.59     pooka 		TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
    335   1.59     pooka 		module_builtinlist++;
    336   1.59     pooka 	}
    337   1.59     pooka 
    338   1.59     pooka 	/* finally, init (if required) */
    339   1.59     pooka 	if (init) {
    340   1.59     pooka 		for (i = 0; i < nmodinfo; i++) {
    341  1.116  christos 			rv = module_do_builtin(modp[i],
    342  1.116  christos 			    modp[i]->mod_info->mi_name, NULL, NULL);
    343   1.59     pooka 			/* throw in the towel, recovery hard & not worth it */
    344   1.59     pooka 			if (rv)
    345  1.117  christos 				panic("%s: builtin module \"%s\" init failed:"
    346  1.117  christos 				    " %d", __func__,
    347   1.59     pooka 				    modp[i]->mod_info->mi_name, rv);
    348   1.59     pooka 		}
    349   1.59     pooka 	}
    350   1.59     pooka 
    351   1.59     pooka  out:
    352   1.72  pgoyette 	kernconfig_unlock();
    353   1.59     pooka 	if (rv != 0) {
    354   1.59     pooka 		for (i = 0; i < nmodinfo; i++) {
    355   1.59     pooka 			if (modp[i])
    356  1.131       chs 				module_free(modp[i]);
    357   1.59     pooka 		}
    358   1.59     pooka 	}
    359   1.59     pooka 	kmem_free(modp, sizeof(*modp) * nmodinfo);
    360   1.59     pooka 	return rv;
    361   1.59     pooka }
    362   1.59     pooka 
    363   1.59     pooka /*
    364   1.59     pooka  * Optionally fini and remove builtin module from the kernel.
    365   1.59     pooka  * Note: the module will now be unreachable except via mi && builtin_add.
    366   1.59     pooka  */
    367   1.59     pooka int
    368   1.59     pooka module_builtin_remove(modinfo_t *mi, bool fini)
    369   1.59     pooka {
    370   1.59     pooka 	struct module *mod;
    371   1.59     pooka 	int rv = 0;
    372   1.59     pooka 
    373   1.59     pooka 	if (fini) {
    374   1.59     pooka 		rv = kauth_authorize_system(kauth_cred_get(),
    375   1.59     pooka 		    KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
    376   1.59     pooka 		    NULL, NULL);
    377   1.59     pooka 		if (rv)
    378   1.59     pooka 			return rv;
    379   1.59     pooka 
    380   1.72  pgoyette 		kernconfig_lock();
    381   1.70  pgoyette 		rv = module_do_unload(mi->mi_name, true);
    382   1.59     pooka 		if (rv) {
    383   1.59     pooka 			goto out;
    384   1.59     pooka 		}
    385   1.59     pooka 	} else {
    386   1.72  pgoyette 		kernconfig_lock();
    387   1.59     pooka 	}
    388   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    389   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
    390   1.59     pooka 			break;
    391   1.59     pooka 	}
    392   1.59     pooka 	if (mod) {
    393   1.59     pooka 		TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    394   1.59     pooka 		module_builtinlist--;
    395   1.59     pooka 	} else {
    396   1.59     pooka 		KASSERT(fini == false);
    397   1.59     pooka 		rv = ENOENT;
    398   1.59     pooka 	}
    399   1.59     pooka 
    400   1.59     pooka  out:
    401   1.72  pgoyette 	kernconfig_unlock();
    402   1.59     pooka 	return rv;
    403   1.59     pooka }
    404   1.59     pooka 
    405   1.59     pooka /*
    406    1.1        ad  * module_init:
    407    1.1        ad  *
    408    1.1        ad  *	Initialize the module subsystem.
    409    1.1        ad  */
    410    1.1        ad void
    411    1.1        ad module_init(void)
    412    1.1        ad {
    413   1.59     pooka 	__link_set_decl(modules, modinfo_t);
    414   1.59     pooka 	modinfo_t *const *mip;
    415   1.59     pooka 	int rv;
    416    1.1        ad 
    417   1.25        ad 	if (module_map == NULL) {
    418   1.25        ad 		module_map = kernel_map;
    419   1.25        ad 	}
    420   1.71  pgoyette 	cv_init(&module_thread_cv, "mod_unld");
    421   1.26        ad 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
    422  1.131       chs 	TAILQ_INIT(&modcblist);
    423   1.66  pgoyette 
    424   1.13        ad #ifdef MODULAR	/* XXX */
    425   1.11        ad 	module_init_md();
    426   1.12        ad #endif
    427   1.16        ad 
    428  1.149  christos #ifdef KERNEL_DIR
    429  1.149  christos 	const char *booted_kernel = get_booted_kernel();
    430  1.149  christos 	if (booted_kernel) {
    431  1.149  christos 		char *ptr = strrchr(booted_kernel, '/');
    432  1.149  christos 		snprintf(module_base, sizeof(module_base), "/%.*s/modules",
    433  1.149  christos 		    (int)(ptr - booted_kernel), booted_kernel);
    434  1.149  christos 	} else {
    435  1.149  christos 		strlcpy(module_base, "/netbsd/modules", sizeof(module_base));
    436  1.149  christos 		printf("Cannot find kernel name, loading modules from \"%s\"\n",
    437  1.149  christos 		    module_base);
    438  1.149  christos 	}
    439  1.149  christos #else
    440   1.78       mrg 	if (!module_machine)
    441   1.78       mrg 		module_machine = machine;
    442   1.16        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    443   1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
    444   1.78       mrg 	    module_machine, osrelease);
    445   1.16        ad #else						/* release */
    446   1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
    447   1.78       mrg 	    module_machine, __NetBSD_Version__ / 100000000,
    448   1.16        ad 	    __NetBSD_Version__ / 1000000 % 100);
    449   1.16        ad #endif
    450  1.149  christos #endif
    451   1.50      elad 
    452   1.55      elad 	module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    453   1.55      elad 	    module_listener_cb, NULL);
    454   1.59     pooka 
    455   1.59     pooka 	__link_set_foreach(mip, modules) {
    456   1.77   mbalmer 		if ((rv = module_builtin_add(mip, 1, false)) != 0)
    457   1.59     pooka 			module_error("builtin %s failed: %d\n",
    458   1.59     pooka 			    (*mip)->mi_name, rv);
    459   1.59     pooka 	}
    460   1.69     pooka 
    461   1.69     pooka 	sysctl_module_setup();
    462  1.131       chs 	module_specificdata_domain = specificdata_domain_create();
    463  1.131       chs 
    464  1.131       chs 	module_netbsd = module_newmodule(MODULE_SOURCE_KERNEL);
    465  1.131       chs 	module_netbsd->mod_refcnt = 1;
    466  1.131       chs 	module_netbsd->mod_info = &module_netbsd_modinfo;
    467   1.51      elad }
    468   1.51      elad 
    469   1.50      elad /*
    470   1.70  pgoyette  * module_start_unload_thread:
    471   1.50      elad  *
    472   1.50      elad  *	Start the auto unload kthread.
    473   1.50      elad  */
    474   1.50      elad void
    475   1.70  pgoyette module_start_unload_thread(void)
    476   1.50      elad {
    477   1.50      elad 	int error;
    478   1.26        ad 
    479   1.26        ad 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
    480   1.26        ad 	    NULL, NULL, "modunload");
    481   1.26        ad 	if (error != 0)
    482  1.117  christos 		panic("%s: %d", __func__, error);
    483    1.1        ad }
    484    1.1        ad 
    485   1.70  pgoyette /*
    486   1.70  pgoyette  * module_builtin_require_force
    487   1.70  pgoyette  *
    488   1.70  pgoyette  * Require MODCTL_MUST_FORCE to load any built-in modules that have
    489   1.70  pgoyette  * not yet been initialized
    490   1.70  pgoyette  */
    491   1.70  pgoyette void
    492   1.70  pgoyette module_builtin_require_force(void)
    493   1.70  pgoyette {
    494   1.70  pgoyette 	module_t *mod;
    495   1.70  pgoyette 
    496   1.72  pgoyette 	kernconfig_lock();
    497   1.70  pgoyette 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    498   1.70  pgoyette 		module_require_force(mod);
    499   1.70  pgoyette 	}
    500   1.72  pgoyette 	kernconfig_unlock();
    501   1.70  pgoyette }
    502   1.70  pgoyette 
    503   1.69     pooka static struct sysctllog *module_sysctllog;
    504   1.69     pooka 
    505   1.94  pgoyette static int
    506   1.94  pgoyette sysctl_module_autotime(SYSCTLFN_ARGS)
    507   1.94  pgoyette {
    508   1.94  pgoyette 	struct sysctlnode node;
    509   1.94  pgoyette 	int t, error;
    510   1.94  pgoyette 
    511   1.94  pgoyette 	t = *(int *)rnode->sysctl_data;
    512   1.94  pgoyette 
    513   1.94  pgoyette 	node = *rnode;
    514   1.94  pgoyette 	node.sysctl_data = &t;
    515   1.94  pgoyette 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    516   1.94  pgoyette 	if (error || newp == NULL)
    517   1.94  pgoyette 		return (error);
    518   1.94  pgoyette 
    519   1.94  pgoyette 	if (t < 0)
    520   1.94  pgoyette 		return (EINVAL);
    521   1.94  pgoyette 
    522   1.94  pgoyette 	*(int *)rnode->sysctl_data = t;
    523   1.94  pgoyette 	return (0);
    524   1.94  pgoyette }
    525   1.94  pgoyette 
    526   1.69     pooka static void
    527   1.69     pooka sysctl_module_setup(void)
    528   1.34        ad {
    529   1.34        ad 	const struct sysctlnode *node = NULL;
    530   1.34        ad 
    531   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, NULL, &node,
    532   1.34        ad 		CTLFLAG_PERMANENT,
    533   1.34        ad 		CTLTYPE_NODE, "module",
    534   1.34        ad 		SYSCTL_DESCR("Module options"),
    535   1.34        ad 		NULL, 0, NULL, 0,
    536   1.34        ad 		CTL_KERN, CTL_CREATE, CTL_EOL);
    537   1.34        ad 
    538   1.34        ad 	if (node == NULL)
    539   1.34        ad 		return;
    540   1.34        ad 
    541   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    542   1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    543   1.64    jruoho 		CTLTYPE_BOOL, "autoload",
    544   1.34        ad 		SYSCTL_DESCR("Enable automatic load of modules"),
    545   1.34        ad 		NULL, 0, &module_autoload_on, 0,
    546   1.34        ad 		CTL_CREATE, CTL_EOL);
    547   1.69     pooka 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    548   1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    549  1.158  riastrad 		CTLTYPE_BOOL, "autounload_unsafe",
    550  1.158  riastrad 		SYSCTL_DESCR("Enable automatic unload of unaudited modules"),
    551  1.158  riastrad 		NULL, 0, &module_autounload_unsafe, 0,
    552  1.158  riastrad 		CTL_CREATE, CTL_EOL);
    553  1.158  riastrad 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    554  1.158  riastrad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    555   1.64    jruoho 		CTLTYPE_BOOL, "verbose",
    556   1.34        ad 		SYSCTL_DESCR("Enable verbose output"),
    557   1.34        ad 		NULL, 0, &module_verbose_on, 0,
    558   1.34        ad 		CTL_CREATE, CTL_EOL);
    559   1.94  pgoyette 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    560   1.97   jnemeth 		CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    561   1.97   jnemeth 		CTLTYPE_STRING, "path",
    562   1.97   jnemeth 		SYSCTL_DESCR("Default module load path"),
    563   1.97   jnemeth 		NULL, 0, module_base, 0,
    564   1.97   jnemeth 		CTL_CREATE, CTL_EOL);
    565   1.97   jnemeth 	sysctl_createv(&module_sysctllog, 0, &node, NULL,
    566   1.94  pgoyette 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    567   1.94  pgoyette 		CTLTYPE_INT, "autotime",
    568   1.94  pgoyette 		SYSCTL_DESCR("Auto-unload delay"),
    569   1.94  pgoyette 		sysctl_module_autotime, 0, &module_autotime, 0,
    570   1.94  pgoyette 		CTL_CREATE, CTL_EOL);
    571   1.34        ad }
    572   1.34        ad 
    573    1.1        ad /*
    574    1.1        ad  * module_init_class:
    575    1.1        ad  *
    576    1.1        ad  *	Initialize all built-in and pre-loaded modules of the
    577    1.1        ad  *	specified class.
    578    1.1        ad  */
    579    1.1        ad void
    580  1.100      matt module_init_class(modclass_t modclass)
    581    1.1        ad {
    582   1.63     pooka 	TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
    583   1.59     pooka 	module_t *mod;
    584   1.59     pooka 	modinfo_t *mi;
    585    1.1        ad 
    586   1.72  pgoyette 	kernconfig_lock();
    587    1.1        ad 	/*
    588   1.59     pooka 	 * Builtins first.  These will not depend on pre-loaded modules
    589   1.59     pooka 	 * (because the kernel would not link).
    590    1.1        ad 	 */
    591   1.59     pooka 	do {
    592   1.59     pooka 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    593   1.59     pooka 			mi = mod->mod_info;
    594  1.100      matt 			if (!MODULE_CLASS_MATCH(mi, modclass))
    595   1.59     pooka 				continue;
    596   1.63     pooka 			/*
    597   1.63     pooka 			 * If initializing a builtin module fails, don't try
    598   1.63     pooka 			 * to load it again.  But keep it around and queue it
    599   1.70  pgoyette 			 * on the builtins list after we're done with module
    600   1.70  pgoyette 			 * init.  Don't set it to MODFLG_MUST_FORCE in case a
    601   1.70  pgoyette 			 * future attempt to initialize can be successful.
    602   1.70  pgoyette 			 * (If the module has previously been set to
    603   1.70  pgoyette 			 * MODFLG_MUST_FORCE, don't try to override that!)
    604   1.63     pooka 			 */
    605  1.133  pgoyette 			if (ISSET(mod->mod_flags, MODFLG_MUST_FORCE) ||
    606  1.116  christos 			    module_do_builtin(mod, mi->mi_name, NULL,
    607  1.116  christos 			    NULL) != 0) {
    608   1.63     pooka 				TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    609   1.63     pooka 				TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
    610   1.63     pooka 			}
    611   1.59     pooka 			break;
    612    1.1        ad 		}
    613   1.59     pooka 	} while (mod != NULL);
    614   1.59     pooka 
    615    1.1        ad 	/*
    616    1.1        ad 	 * Now preloaded modules.  These will be pulled off the
    617    1.1        ad 	 * list as we call module_do_load();
    618    1.1        ad 	 */
    619   1.11        ad 	do {
    620   1.59     pooka 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    621   1.11        ad 			mi = mod->mod_info;
    622  1.100      matt 			if (!MODULE_CLASS_MATCH(mi, modclass))
    623   1.11        ad 				continue;
    624   1.18        ad 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
    625  1.100      matt 			    modclass, false);
    626   1.11        ad 			break;
    627   1.11        ad 		}
    628   1.11        ad 	} while (mod != NULL);
    629   1.63     pooka 
    630   1.70  pgoyette 	/* return failed builtin modules to builtin list */
    631   1.63     pooka 	while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
    632   1.63     pooka 		TAILQ_REMOVE(&bi_fail, mod, mod_chain);
    633   1.63     pooka 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
    634   1.63     pooka 	}
    635   1.63     pooka 
    636   1.72  pgoyette 	kernconfig_unlock();
    637    1.1        ad }
    638    1.1        ad 
    639    1.1        ad /*
    640   1.21        ad  * module_compatible:
    641    1.1        ad  *
    642   1.21        ad  *	Return true if the two supplied kernel versions are said to
    643   1.21        ad  *	have the same binary interface for kernel code.  The entire
    644   1.21        ad  *	version is signficant for the development tree (-current),
    645   1.21        ad  *	major and minor versions are significant for official
    646   1.21        ad  *	releases of the system.
    647    1.1        ad  */
    648   1.22     pooka bool
    649   1.21        ad module_compatible(int v1, int v2)
    650    1.1        ad {
    651    1.1        ad 
    652   1.21        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    653   1.21        ad 	return v1 == v2;
    654   1.21        ad #else						/* release */
    655   1.21        ad 	return abs(v1 - v2) < 10000;
    656   1.21        ad #endif
    657    1.1        ad }
    658    1.1        ad 
    659    1.1        ad /*
    660    1.1        ad  * module_load:
    661    1.1        ad  *
    662    1.9      jmmv  *	Load a single module from the file system.
    663    1.1        ad  */
    664    1.1        ad int
    665   1.18        ad module_load(const char *filename, int flags, prop_dictionary_t props,
    666  1.100      matt 	    modclass_t modclass)
    667    1.1        ad {
    668  1.120      maya 	module_t *mod;
    669    1.1        ad 	int error;
    670    1.1        ad 
    671  1.120      maya 	/* Test if we already have the module loaded before
    672  1.120      maya 	 * authorizing so we have the opportunity to return EEXIST. */
    673  1.120      maya 	kernconfig_lock();
    674  1.120      maya 	mod = module_lookup(filename);
    675  1.120      maya 	if (mod != NULL) {
    676  1.120      maya 		module_print("%s module `%s' already loaded",
    677  1.120      maya 		    "requested", filename);
    678  1.120      maya 		error = EEXIST;
    679  1.120      maya 		goto out;
    680  1.120      maya 	}
    681  1.120      maya 
    682   1.18        ad 	/* Authorize. */
    683   1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    684   1.18        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
    685  1.120      maya 	if (error != 0)
    686  1.120      maya 		goto out;
    687   1.18        ad 
    688  1.100      matt 	error = module_do_load(filename, false, flags, props, NULL, modclass,
    689   1.23        ad 	    false);
    690  1.120      maya 
    691  1.120      maya out:
    692  1.119      maya 	kernconfig_unlock();
    693    1.1        ad 	return error;
    694    1.1        ad }
    695    1.1        ad 
    696    1.1        ad /*
    697   1.23        ad  * module_autoload:
    698   1.23        ad  *
    699   1.23        ad  *	Load a single module from the file system, system initiated.
    700   1.23        ad  */
    701   1.23        ad int
    702  1.100      matt module_autoload(const char *filename, modclass_t modclass)
    703   1.23        ad {
    704   1.23        ad 	int error;
    705  1.146  pgoyette 	struct proc *p = curlwp->l_proc;
    706   1.23        ad 
    707   1.72  pgoyette 	kernconfig_lock();
    708   1.23        ad 
    709   1.34        ad 	/* Nothing if the user has disabled it. */
    710   1.34        ad 	if (!module_autoload_on) {
    711   1.72  pgoyette 		kernconfig_unlock();
    712   1.34        ad 		return EPERM;
    713   1.34        ad 	}
    714   1.34        ad 
    715   1.56  dholland         /* Disallow path separators and magic symlinks. */
    716   1.29        ad         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    717   1.29        ad             strchr(filename, '.') != NULL) {
    718   1.72  pgoyette 		kernconfig_unlock();
    719   1.29        ad         	return EPERM;
    720   1.29        ad 	}
    721   1.29        ad 
    722   1.23        ad 	/* Authorize. */
    723   1.23        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    724   1.23        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    725   1.23        ad 
    726   1.72  pgoyette 	if (error == 0)
    727  1.100      matt 		error = module_do_load(filename, false, 0, NULL, NULL, modclass,
    728   1.72  pgoyette 		    true);
    729   1.72  pgoyette 
    730  1.152  pgoyette 	module_print("Autoload for `%s' requested by pid %d (%s), status %d",
    731  1.146  pgoyette 	    filename, p->p_pid, p->p_comm, error);
    732   1.72  pgoyette 	kernconfig_unlock();
    733   1.72  pgoyette 	return error;
    734   1.23        ad }
    735   1.23        ad 
    736   1.23        ad /*
    737    1.1        ad  * module_unload:
    738    1.1        ad  *
    739    1.1        ad  *	Find and unload a module by name.
    740    1.1        ad  */
    741    1.1        ad int
    742    1.1        ad module_unload(const char *name)
    743    1.1        ad {
    744    1.1        ad 	int error;
    745    1.1        ad 
    746   1.18        ad 	/* Authorize. */
    747   1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    748   1.18        ad 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    749   1.18        ad 	if (error != 0) {
    750   1.18        ad 		return error;
    751   1.18        ad 	}
    752   1.18        ad 
    753   1.72  pgoyette 	kernconfig_lock();
    754   1.70  pgoyette 	error = module_do_unload(name, true);
    755   1.72  pgoyette 	kernconfig_unlock();
    756    1.1        ad 
    757    1.1        ad 	return error;
    758    1.1        ad }
    759    1.1        ad 
    760    1.1        ad /*
    761    1.1        ad  * module_lookup:
    762    1.1        ad  *
    763    1.1        ad  *	Look up a module by name.
    764    1.1        ad  */
    765    1.1        ad module_t *
    766    1.1        ad module_lookup(const char *name)
    767    1.1        ad {
    768    1.1        ad 	module_t *mod;
    769    1.1        ad 
    770   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    771    1.1        ad 
    772    1.1        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    773  1.133  pgoyette 		if (strcmp(mod->mod_info->mi_name, name) == 0)
    774    1.1        ad 			break;
    775    1.1        ad 	}
    776    1.1        ad 
    777    1.1        ad 	return mod;
    778    1.1        ad }
    779    1.1        ad 
    780    1.1        ad /*
    781    1.1        ad  * module_hold:
    782    1.1        ad  *
    783    1.1        ad  *	Add a single reference to a module.  It's the caller's
    784    1.1        ad  *	responsibility to ensure that the reference is dropped
    785    1.1        ad  *	later.
    786    1.1        ad  */
    787  1.131       chs void
    788  1.131       chs module_hold(module_t *mod)
    789    1.1        ad {
    790    1.1        ad 
    791   1.72  pgoyette 	kernconfig_lock();
    792    1.1        ad 	mod->mod_refcnt++;
    793   1.72  pgoyette 	kernconfig_unlock();
    794    1.1        ad }
    795    1.1        ad 
    796    1.1        ad /*
    797    1.1        ad  * module_rele:
    798    1.1        ad  *
    799    1.1        ad  *	Release a reference acquired with module_hold().
    800    1.1        ad  */
    801    1.1        ad void
    802  1.131       chs module_rele(module_t *mod)
    803    1.1        ad {
    804    1.1        ad 
    805   1.72  pgoyette 	kernconfig_lock();
    806  1.131       chs 	KASSERT(mod->mod_refcnt > 0);
    807    1.1        ad 	mod->mod_refcnt--;
    808   1.72  pgoyette 	kernconfig_unlock();
    809    1.1        ad }
    810    1.1        ad 
    811    1.1        ad /*
    812   1.27        ad  * module_enqueue:
    813   1.27        ad  *
    814   1.27        ad  *	Put a module onto the global list and update counters.
    815   1.27        ad  */
    816   1.53     pooka void
    817   1.27        ad module_enqueue(module_t *mod)
    818   1.27        ad {
    819   1.27        ad 	int i;
    820   1.27        ad 
    821   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    822   1.53     pooka 
    823   1.27        ad 	/*
    824  1.113  pgoyette 	 * Put new entry at the head of the queue so autounload can unload
    825  1.113  pgoyette 	 * requisite modules with only one pass through the queue.
    826   1.27        ad 	 */
    827  1.113  pgoyette 	TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    828   1.27        ad 	if (mod->mod_nrequired) {
    829   1.27        ad 
    830   1.27        ad 		/* Add references to the requisite modules. */
    831   1.27        ad 		for (i = 0; i < mod->mod_nrequired; i++) {
    832  1.133  pgoyette 			KASSERT((*mod->mod_required)[i] != NULL);
    833  1.133  pgoyette 			(*mod->mod_required)[i]->mod_refcnt++;
    834   1.27        ad 		}
    835   1.27        ad 	}
    836   1.27        ad 	module_count++;
    837   1.27        ad 	module_gen++;
    838   1.27        ad }
    839   1.27        ad 
    840   1.27        ad /*
    841  1.133  pgoyette  * Our array of required module pointers starts with zero entries.  If we
    842  1.133  pgoyette  * need to add a new entry, and the list is already full, we reallocate a
    843  1.133  pgoyette  * larger array, adding MAXMODDEPS entries.
    844  1.133  pgoyette  */
    845  1.133  pgoyette static void
    846  1.133  pgoyette alloc_required(module_t *mod)
    847  1.133  pgoyette {
    848  1.133  pgoyette 	module_t *(*new)[], *(*old)[];
    849  1.133  pgoyette 	int areq;
    850  1.133  pgoyette 	int i;
    851  1.133  pgoyette 
    852  1.133  pgoyette 	if (mod->mod_nrequired >= mod->mod_arequired) {
    853  1.133  pgoyette 		areq = mod->mod_arequired + MAXMODDEPS;
    854  1.133  pgoyette 		old = mod->mod_required;
    855  1.133  pgoyette 		new = kmem_zalloc(areq * sizeof(module_t *), KM_SLEEP);
    856  1.133  pgoyette 		for (i = 0; i < mod->mod_arequired; i++)
    857  1.133  pgoyette 			(*new)[i] = (*old)[i];
    858  1.133  pgoyette 		mod->mod_required = new;
    859  1.133  pgoyette 		if (old)
    860  1.133  pgoyette 			kmem_free(old, mod->mod_arequired * sizeof(module_t *));
    861  1.133  pgoyette 		mod->mod_arequired = areq;
    862  1.133  pgoyette 	}
    863  1.133  pgoyette }
    864  1.133  pgoyette 
    865  1.133  pgoyette /*
    866    1.1        ad  * module_do_builtin:
    867    1.1        ad  *
    868   1.59     pooka  *	Initialize a module from the list of modules that are
    869   1.59     pooka  *	already linked into the kernel.
    870    1.1        ad  */
    871    1.1        ad static int
    872  1.116  christos module_do_builtin(const module_t *pmod, const char *name, module_t **modp,
    873  1.116  christos     prop_dictionary_t props)
    874    1.1        ad {
    875    1.1        ad 	const char *p, *s;
    876    1.1        ad 	char buf[MAXMODNAME];
    877   1.59     pooka 	modinfo_t *mi = NULL;
    878   1.72  pgoyette 	module_t *mod, *mod2, *mod_loaded, *prev_active;
    879    1.1        ad 	size_t len;
    880   1.27        ad 	int error;
    881    1.1        ad 
    882   1.72  pgoyette 	KASSERT(kernconfig_is_held());
    883    1.1        ad 
    884    1.1        ad 	/*
    885   1.59     pooka 	 * Search the list to see if we have a module by this name.
    886    1.1        ad 	 */
    887   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
    888   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    889   1.59     pooka 			mi = mod->mod_info;
    890   1.59     pooka 			break;
    891    1.1        ad 		}
    892    1.1        ad 	}
    893    1.1        ad 
    894    1.1        ad 	/*
    895   1.59     pooka 	 * Check to see if already loaded.  This might happen if we
    896   1.59     pooka 	 * were already loaded as a dependency.
    897    1.1        ad 	 */
    898   1.59     pooka 	if ((mod_loaded = module_lookup(name)) != NULL) {
    899   1.59     pooka 		KASSERT(mod == NULL);
    900   1.59     pooka 		if (modp)
    901   1.59     pooka 			*modp = mod_loaded;
    902   1.59     pooka 		return 0;
    903    1.1        ad 	}
    904   1.59     pooka 
    905   1.59     pooka 	/* Note! This is from TAILQ, not immediate above */
    906   1.65     pooka 	if (mi == NULL) {
    907   1.65     pooka 		/*
    908   1.65     pooka 		 * XXX: We'd like to panic here, but currently in some
    909   1.65     pooka 		 * cases (such as nfsserver + nfs), the dependee can be
    910  1.153    andvar 		 * successfully linked without the dependencies.
    911   1.65     pooka 		 */
    912  1.133  pgoyette 		module_error("built-in module %s can't find builtin "
    913  1.133  pgoyette 		    "dependency `%s'", pmod->mod_info->mi_name, name);
    914   1.65     pooka 		return ENOENT;
    915   1.65     pooka 	}
    916    1.1        ad 
    917    1.1        ad 	/*
    918    1.1        ad 	 * Initialize pre-requisites.
    919    1.1        ad 	 */
    920  1.133  pgoyette 	KASSERT(mod->mod_required == NULL);
    921  1.133  pgoyette 	KASSERT(mod->mod_arequired == 0);
    922  1.133  pgoyette 	KASSERT(mod->mod_nrequired == 0);
    923    1.1        ad 	if (mi->mi_required != NULL) {
    924    1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
    925    1.1        ad 			if (*s == ',')
    926    1.1        ad 				s++;
    927    1.1        ad 			p = s;
    928    1.1        ad 			while (*p != '\0' && *p != ',')
    929    1.1        ad 				p++;
    930  1.132  riastrad 			len = uimin(p - s + 1, sizeof(buf));
    931    1.1        ad 			strlcpy(buf, s, len);
    932    1.1        ad 			if (buf[0] == '\0')
    933    1.1        ad 				break;
    934  1.133  pgoyette 			alloc_required(mod);
    935  1.116  christos 			error = module_do_builtin(mod, buf, &mod2, NULL);
    936    1.1        ad 			if (error != 0) {
    937  1.133  pgoyette 				module_error("built-in module %s prerequisite "
    938  1.133  pgoyette 				    "%s failed, error %d", name, buf, error);
    939  1.133  pgoyette 				goto fail;
    940    1.1        ad 			}
    941  1.133  pgoyette 			(*mod->mod_required)[mod->mod_nrequired++] = mod2;
    942    1.1        ad 		}
    943    1.1        ad 	}
    944    1.1        ad 
    945    1.1        ad 	/*
    946    1.1        ad 	 * Try to initialize the module.
    947    1.1        ad 	 */
    948   1.72  pgoyette 	prev_active = module_active;
    949   1.12        ad 	module_active = mod;
    950   1.74     pooka 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
    951   1.72  pgoyette 	module_active = prev_active;
    952    1.1        ad 	if (error != 0) {
    953  1.133  pgoyette 		module_error("built-in module %s failed its MODULE_CMD_INIT, "
    954  1.133  pgoyette 		    "error %d", mi->mi_name, error);
    955  1.133  pgoyette 		goto fail;
    956    1.1        ad 	}
    957   1.59     pooka 
    958   1.59     pooka 	/* load always succeeds after this point */
    959   1.59     pooka 
    960   1.59     pooka 	TAILQ_REMOVE(&module_builtins, mod, mod_chain);
    961   1.59     pooka 	module_builtinlist--;
    962   1.59     pooka 	if (modp != NULL) {
    963   1.59     pooka 		*modp = mod;
    964   1.59     pooka 	}
    965   1.27        ad 	module_enqueue(mod);
    966    1.1        ad 	return 0;
    967  1.133  pgoyette 
    968  1.133  pgoyette  fail:
    969  1.133  pgoyette 	if (mod->mod_required)
    970  1.133  pgoyette 		kmem_free(mod->mod_required, mod->mod_arequired *
    971  1.133  pgoyette 		    sizeof(module_t *));
    972  1.133  pgoyette 	mod->mod_arequired = 0;
    973  1.133  pgoyette 	mod->mod_nrequired = 0;
    974  1.133  pgoyette 	mod->mod_required = NULL;
    975  1.133  pgoyette 	return error;
    976    1.1        ad }
    977    1.1        ad 
    978    1.1        ad /*
    979  1.137  pgoyette  * module_load_sysctl
    980  1.137  pgoyette  *
    981  1.138  pgoyette  * Check to see if a non-builtin module has any SYSCTL_SETUP() routine(s)
    982  1.137  pgoyette  * registered.  If so, call it (them).
    983  1.137  pgoyette  */
    984  1.137  pgoyette 
    985  1.137  pgoyette static void
    986  1.137  pgoyette module_load_sysctl(module_t *mod)
    987  1.137  pgoyette {
    988  1.137  pgoyette 	void (**ls_funcp)(struct sysctllog **);
    989  1.137  pgoyette 	void *ls_start;
    990  1.137  pgoyette 	size_t ls_size, count;
    991  1.137  pgoyette 	int error;
    992  1.137  pgoyette 
    993  1.138  pgoyette 	/*
    994  1.138  pgoyette 	 * Built-in modules don't have a mod_kobj so we cannot search
    995  1.138  pgoyette 	 * for their link_set_sysctl_funcs
    996  1.138  pgoyette 	 */
    997  1.138  pgoyette 	if (mod->mod_source == MODULE_SOURCE_KERNEL)
    998  1.138  pgoyette 		return;
    999  1.138  pgoyette 
   1000  1.137  pgoyette 	error = kobj_find_section(mod->mod_kobj, "link_set_sysctl_funcs",
   1001  1.137  pgoyette 	    &ls_start, &ls_size);
   1002  1.137  pgoyette 	if (error == 0) {
   1003  1.137  pgoyette 		count = ls_size / sizeof(ls_start);
   1004  1.137  pgoyette 		ls_funcp = ls_start;
   1005  1.137  pgoyette 		while (count--) {
   1006  1.137  pgoyette 			(**ls_funcp)(&mod->mod_sysctllog);
   1007  1.137  pgoyette 			ls_funcp++;
   1008  1.137  pgoyette 		}
   1009  1.137  pgoyette 	}
   1010  1.147  pgoyette }
   1011  1.147  pgoyette 
   1012  1.147  pgoyette /*
   1013  1.147  pgoyette  * module_load_evcnt
   1014  1.147  pgoyette  *
   1015  1.147  pgoyette  * Check to see if a non-builtin module has any static evcnt's defined;
   1016  1.147  pgoyette  * if so, attach them.
   1017  1.147  pgoyette  */
   1018  1.147  pgoyette 
   1019  1.147  pgoyette static void
   1020  1.147  pgoyette module_load_evcnt(module_t *mod)
   1021  1.147  pgoyette {
   1022  1.147  pgoyette 	struct evcnt * const *ls_evp;
   1023  1.147  pgoyette 	void *ls_start;
   1024  1.147  pgoyette 	size_t ls_size, count;
   1025  1.147  pgoyette 	int error;
   1026  1.147  pgoyette 
   1027  1.147  pgoyette 	/*
   1028  1.147  pgoyette 	 * Built-in modules' static evcnt stuff will be handled
   1029  1.147  pgoyette 	 * automatically as part of general kernel initialization
   1030  1.147  pgoyette 	 */
   1031  1.147  pgoyette 	if (mod->mod_source == MODULE_SOURCE_KERNEL)
   1032  1.147  pgoyette 		return;
   1033  1.147  pgoyette 
   1034  1.147  pgoyette 	error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
   1035  1.147  pgoyette 	    &ls_start, &ls_size);
   1036  1.147  pgoyette 	if (error == 0) {
   1037  1.147  pgoyette 		count = ls_size / sizeof(*ls_evp);
   1038  1.147  pgoyette 		ls_evp = ls_start;
   1039  1.147  pgoyette 		while (count--) {
   1040  1.147  pgoyette 			evcnt_attach_static(*ls_evp++);
   1041  1.147  pgoyette 		}
   1042  1.147  pgoyette 	}
   1043  1.147  pgoyette }
   1044  1.147  pgoyette 
   1045  1.147  pgoyette /*
   1046  1.147  pgoyette  * module_unload_evcnt
   1047  1.147  pgoyette  *
   1048  1.147  pgoyette  * Check to see if a non-builtin module has any static evcnt's defined;
   1049  1.147  pgoyette  * if so, detach them.
   1050  1.147  pgoyette  */
   1051  1.147  pgoyette 
   1052  1.147  pgoyette static void
   1053  1.147  pgoyette module_unload_evcnt(module_t *mod)
   1054  1.147  pgoyette {
   1055  1.147  pgoyette 	struct evcnt * const *ls_evp;
   1056  1.147  pgoyette 	void *ls_start;
   1057  1.147  pgoyette 	size_t ls_size, count;
   1058  1.147  pgoyette 	int error;
   1059  1.147  pgoyette 
   1060  1.147  pgoyette 	/*
   1061  1.147  pgoyette 	 * Built-in modules' static evcnt stuff will be handled
   1062  1.147  pgoyette 	 * automatically as part of general kernel initialization
   1063  1.147  pgoyette 	 */
   1064  1.147  pgoyette 	if (mod->mod_source == MODULE_SOURCE_KERNEL)
   1065  1.147  pgoyette 		return;
   1066  1.147  pgoyette 
   1067  1.147  pgoyette 	error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
   1068  1.147  pgoyette 	    &ls_start, &ls_size);
   1069  1.147  pgoyette 	if (error == 0) {
   1070  1.147  pgoyette 		count = ls_size / sizeof(*ls_evp);
   1071  1.147  pgoyette 		ls_evp = (void *)((char *)ls_start + ls_size);
   1072  1.147  pgoyette 		while (count--) {
   1073  1.147  pgoyette 			evcnt_detach(*--ls_evp);
   1074  1.147  pgoyette 		}
   1075  1.147  pgoyette 	}
   1076  1.137  pgoyette }
   1077  1.137  pgoyette 
   1078  1.137  pgoyette /*
   1079    1.1        ad  * module_do_load:
   1080    1.1        ad  *
   1081    1.1        ad  *	Helper routine: load a module from the file system, or one
   1082    1.1        ad  *	pushed by the boot loader.
   1083    1.1        ad  */
   1084    1.1        ad static int
   1085   1.27        ad module_do_load(const char *name, bool isdep, int flags,
   1086  1.100      matt 	       prop_dictionary_t props, module_t **modp, modclass_t modclass,
   1087   1.20        ad 	       bool autoload)
   1088    1.1        ad {
   1089  1.133  pgoyette 	/* The pending list for this level of recursion */
   1090   1.72  pgoyette 	TAILQ_HEAD(pending_t, module);
   1091   1.72  pgoyette 	struct pending_t *pending;
   1092   1.72  pgoyette 	struct pending_t new_pending = TAILQ_HEAD_INITIALIZER(new_pending);
   1093  1.133  pgoyette 
   1094  1.133  pgoyette 	/* The stack of pending lists */
   1095  1.133  pgoyette 	static SLIST_HEAD(pend_head, pend_entry) pend_stack =
   1096  1.133  pgoyette 		SLIST_HEAD_INITIALIZER(pend_stack);
   1097  1.133  pgoyette 	struct pend_entry {
   1098  1.133  pgoyette 		SLIST_ENTRY(pend_entry) pe_entry;
   1099  1.133  pgoyette 		struct pending_t *pe_pending;
   1100  1.133  pgoyette 	} my_pend_entry;
   1101  1.133  pgoyette 
   1102    1.1        ad 	modinfo_t *mi;
   1103   1.72  pgoyette 	module_t *mod, *mod2, *prev_active;
   1104   1.46   jnemeth 	prop_dictionary_t filedict;
   1105   1.54     pooka 	char buf[MAXMODNAME];
   1106    1.1        ad 	const char *s, *p;
   1107    1.1        ad 	int error;
   1108   1.54     pooka 	size_t len;
   1109    1.1        ad 
   1110   1.72  pgoyette 	KASSERT(kernconfig_is_held());
   1111    1.1        ad 
   1112   1.46   jnemeth 	filedict = NULL;
   1113    1.1        ad 	error = 0;
   1114    1.1        ad 
   1115    1.1        ad 	/*
   1116  1.133  pgoyette 	 * Set up the pending list for this entry.  If this is an
   1117  1.133  pgoyette 	 * internal entry (for a dependency), then use the same list
   1118  1.133  pgoyette 	 * as for the outer call;  otherwise, it's an external entry
   1119  1.133  pgoyette 	 * (possibly recursive, ie a module's xxx_modcmd(init, ...)
   1120  1.133  pgoyette 	 * routine called us), so use the locally allocated list.  In
   1121  1.133  pgoyette 	 * either case, add it to our stack.
   1122   1.72  pgoyette 	 */
   1123   1.72  pgoyette 	if (isdep) {
   1124  1.133  pgoyette 		KASSERT(SLIST_FIRST(&pend_stack) != NULL);
   1125  1.133  pgoyette 		pending = SLIST_FIRST(&pend_stack)->pe_pending;
   1126   1.72  pgoyette 	} else
   1127   1.72  pgoyette 		pending = &new_pending;
   1128  1.133  pgoyette 	my_pend_entry.pe_pending = pending;
   1129  1.133  pgoyette 	SLIST_INSERT_HEAD(&pend_stack, &my_pend_entry, pe_entry);
   1130   1.72  pgoyette 
   1131   1.72  pgoyette 	/*
   1132   1.59     pooka 	 * Search the list of disabled builtins first.
   1133   1.59     pooka 	 */
   1134   1.59     pooka 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
   1135   1.59     pooka 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
   1136   1.59     pooka 			break;
   1137   1.59     pooka 		}
   1138   1.59     pooka 	}
   1139   1.59     pooka 	if (mod) {
   1140  1.133  pgoyette 		if (ISSET(mod->mod_flags, MODFLG_MUST_FORCE) &&
   1141  1.133  pgoyette 		    !ISSET(flags, MODCTL_LOAD_FORCE)) {
   1142   1.62     pooka 			if (!autoload) {
   1143   1.62     pooka 				module_error("use -f to reinstate "
   1144   1.90  christos 				    "builtin module `%s'", name);
   1145   1.62     pooka 			}
   1146  1.133  pgoyette 			SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1147   1.59     pooka 			return EPERM;
   1148   1.59     pooka 		} else {
   1149  1.133  pgoyette 			SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1150  1.116  christos 			error = module_do_builtin(mod, name, modp, props);
   1151   1.59     pooka 			return error;
   1152   1.59     pooka 		}
   1153   1.59     pooka 	}
   1154   1.59     pooka 
   1155   1.59     pooka 	/*
   1156    1.1        ad 	 * Load the module and link.  Before going to the file system,
   1157   1.57     pooka 	 * scan the list of modules loaded by the boot loader.
   1158    1.1        ad 	 */
   1159    1.1        ad 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
   1160   1.27        ad 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
   1161    1.1        ad 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
   1162    1.1        ad 			break;
   1163    1.1        ad 		}
   1164    1.1        ad 	}
   1165   1.14    rumble 	if (mod != NULL) {
   1166   1.72  pgoyette 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
   1167   1.14    rumble 	} else {
   1168   1.27        ad 		/*
   1169  1.110  pgoyette 		 * Check to see if module is already present.
   1170   1.27        ad 		 */
   1171  1.110  pgoyette 		mod = module_lookup(name);
   1172  1.110  pgoyette 		if (mod != NULL) {
   1173  1.110  pgoyette 			if (modp != NULL) {
   1174  1.110  pgoyette 				*modp = mod;
   1175   1.27        ad 			}
   1176  1.110  pgoyette 			module_print("%s module `%s' already loaded",
   1177  1.110  pgoyette 			    isdep ? "dependent" : "requested", name);
   1178  1.133  pgoyette 			SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1179  1.110  pgoyette 			return EEXIST;
   1180  1.109      maxv 		}
   1181  1.110  pgoyette 
   1182   1.70  pgoyette 		mod = module_newmodule(MODULE_SOURCE_FILESYS);
   1183    1.1        ad 		if (mod == NULL) {
   1184   1.38  christos 			module_error("out of memory for `%s'", name);
   1185  1.133  pgoyette 			SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1186    1.1        ad 			return ENOMEM;
   1187    1.1        ad 		}
   1188   1.54     pooka 
   1189   1.67  pgoyette 		error = module_load_vfs_vec(name, flags, autoload, mod,
   1190   1.67  pgoyette 					    &filedict);
   1191    1.1        ad 		if (error != 0) {
   1192   1.91  christos #ifdef DEBUG
   1193   1.92  christos 			/*
   1194   1.92  christos 			 * The exec class of modules contains a list of
   1195   1.92  christos 			 * modules that is the union of all the modules
   1196   1.92  christos 			 * available for each architecture, so we don't
   1197   1.92  christos 			 * print an error if they are missing.
   1198   1.92  christos 			 */
   1199  1.104  christos 			if ((modclass != MODULE_CLASS_EXEC || error != ENOENT)
   1200  1.105  christos 			    && root_device != NULL)
   1201   1.92  christos 				module_error("vfs load failed for `%s', "
   1202   1.92  christos 				    "error %d", name, error);
   1203   1.91  christos #endif
   1204  1.133  pgoyette 			SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1205  1.131       chs 			module_free(mod);
   1206    1.8    rumble 			return error;
   1207    1.7    rumble 		}
   1208   1.72  pgoyette 		TAILQ_INSERT_TAIL(pending, mod, mod_chain);
   1209   1.54     pooka 
   1210   1.11        ad 		error = module_fetch_info(mod);
   1211   1.11        ad 		if (error != 0) {
   1212   1.90  christos 			module_error("cannot fetch info for `%s', error %d",
   1213   1.90  christos 			    name, error);
   1214   1.11        ad 			goto fail;
   1215   1.11        ad 		}
   1216    1.1        ad 	}
   1217    1.1        ad 
   1218    1.1        ad 	/*
   1219   1.11        ad 	 * Check compatibility.
   1220    1.1        ad 	 */
   1221    1.1        ad 	mi = mod->mod_info;
   1222  1.134  pgoyette 	if (strnlen(mi->mi_name, MAXMODNAME) >= MAXMODNAME) {
   1223    1.3    rumble 		error = EINVAL;
   1224   1.90  christos 		module_error("module name `%s' longer than %d", mi->mi_name,
   1225   1.90  christos 		    MAXMODNAME);
   1226    1.3    rumble 		goto fail;
   1227    1.3    rumble 	}
   1228  1.134  pgoyette 	if (mi->mi_class <= MODULE_CLASS_ANY ||
   1229  1.134  pgoyette 	    mi->mi_class >= MODULE_CLASS_MAX) {
   1230  1.134  pgoyette 		error = EINVAL;
   1231  1.134  pgoyette 		module_error("module `%s' has invalid class %d",
   1232  1.134  pgoyette 		    mi->mi_name, mi->mi_class);
   1233  1.134  pgoyette 		    goto fail;
   1234  1.134  pgoyette 	}
   1235   1.21        ad 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
   1236   1.87  christos 		module_error("module `%s' built for `%d', system `%d'",
   1237   1.87  christos 		    mi->mi_name, mi->mi_version, __NetBSD_Version__);
   1238  1.133  pgoyette 		if (ISSET(flags, MODCTL_LOAD_FORCE)) {
   1239   1.24        ad 			module_error("forced load, system may be unstable");
   1240   1.24        ad 		} else {
   1241   1.24        ad 			error = EPROGMISMATCH;
   1242   1.24        ad 			goto fail;
   1243   1.24        ad 		}
   1244   1.21        ad 	}
   1245    1.3    rumble 
   1246    1.1        ad 	/*
   1247   1.18        ad 	 * If a specific kind of module was requested, ensure that we have
   1248   1.18        ad 	 * a match.
   1249   1.18        ad 	 */
   1250  1.100      matt 	if (!MODULE_CLASS_MATCH(mi, modclass)) {
   1251  1.100      matt 		module_incompat(mi, modclass);
   1252   1.18        ad 		error = ENOENT;
   1253   1.18        ad 		goto fail;
   1254   1.18        ad 	}
   1255   1.18        ad 
   1256   1.18        ad 	/*
   1257   1.27        ad 	 * If loading a dependency, `name' is a plain module name.
   1258    1.1        ad 	 * The name must match.
   1259    1.1        ad 	 */
   1260   1.27        ad 	if (isdep && strcmp(mi->mi_name, name) != 0) {
   1261   1.32  christos 		module_error("dependency name mismatch (`%s' != `%s')",
   1262   1.32  christos 		    name, mi->mi_name);
   1263    1.1        ad 		error = ENOENT;
   1264    1.1        ad 		goto fail;
   1265    1.1        ad 	}
   1266    1.1        ad 
   1267    1.1        ad 	/*
   1268  1.126  pgoyette 	 * If we loaded a module from the filesystem, check the actual
   1269  1.126  pgoyette 	 * module name (from the modinfo_t) to ensure another module
   1270  1.126  pgoyette 	 * with the same name doesn't already exist.  (There's no
   1271  1.126  pgoyette 	 * guarantee the filename will match the module name, and the
   1272  1.126  pgoyette 	 * dup-symbols check may not be sufficient.)
   1273  1.126  pgoyette 	 */
   1274  1.126  pgoyette 	if (mod->mod_source == MODULE_SOURCE_FILESYS) {
   1275  1.126  pgoyette 		mod2 = module_lookup(mod->mod_info->mi_name);
   1276  1.130  pgoyette 		if ( mod2 && mod2 != mod) {
   1277  1.126  pgoyette 			module_error("module with name `%s' already loaded",
   1278  1.126  pgoyette 			    mod2->mod_info->mi_name);
   1279  1.126  pgoyette 			error = EEXIST;
   1280  1.133  pgoyette 			if (modp != NULL)
   1281  1.133  pgoyette 				*modp = mod2;
   1282  1.126  pgoyette 			goto fail;
   1283  1.126  pgoyette 		}
   1284  1.126  pgoyette 	}
   1285  1.126  pgoyette 
   1286  1.126  pgoyette 	/*
   1287    1.3    rumble 	 * Block circular dependencies.
   1288    1.3    rumble 	 */
   1289   1.72  pgoyette 	TAILQ_FOREACH(mod2, pending, mod_chain) {
   1290    1.1        ad 		if (mod == mod2) {
   1291    1.1        ad 			continue;
   1292    1.1        ad 		}
   1293    1.1        ad 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
   1294  1.109      maxv 			error = EDEADLK;
   1295   1.32  christos 			module_error("circular dependency detected for `%s'",
   1296   1.32  christos 			    mi->mi_name);
   1297  1.109      maxv 			goto fail;
   1298    1.1        ad 		}
   1299    1.1        ad 	}
   1300    1.1        ad 
   1301    1.1        ad 	/*
   1302    1.1        ad 	 * Now try to load any requisite modules.
   1303    1.1        ad 	 */
   1304    1.1        ad 	if (mi->mi_required != NULL) {
   1305  1.133  pgoyette 		mod->mod_arequired = 0;
   1306    1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
   1307    1.1        ad 			if (*s == ',')
   1308    1.1        ad 				s++;
   1309    1.1        ad 			p = s;
   1310    1.1        ad 			while (*p != '\0' && *p != ',')
   1311    1.1        ad 				p++;
   1312    1.3    rumble 			len = p - s + 1;
   1313    1.3    rumble 			if (len >= MAXMODNAME) {
   1314    1.3    rumble 				error = EINVAL;
   1315   1.90  christos 				module_error("required module name `%s' "
   1316   1.90  christos 				    "longer than %d", mi->mi_required,
   1317   1.90  christos 				    MAXMODNAME);
   1318    1.3    rumble 				goto fail;
   1319    1.3    rumble 			}
   1320    1.1        ad 			strlcpy(buf, s, len);
   1321    1.1        ad 			if (buf[0] == '\0')
   1322    1.1        ad 				break;
   1323  1.133  pgoyette 			alloc_required(mod);
   1324    1.6    rumble 			if (strcmp(buf, mi->mi_name) == 0) {
   1325    1.6    rumble 				error = EDEADLK;
   1326   1.32  christos 				module_error("self-dependency detected for "
   1327   1.32  christos 				   "`%s'", mi->mi_name);
   1328    1.6    rumble 				goto fail;
   1329    1.6    rumble 			}
   1330    1.9      jmmv 			error = module_do_load(buf, true, flags, NULL,
   1331   1.81  christos 			    &mod2, MODULE_CLASS_ANY, true);
   1332  1.110  pgoyette 			if (error != 0 && error != EEXIST) {
   1333   1.96      maxv 				module_error("recursive load failed for `%s' "
   1334   1.96      maxv 				    "(`%s' required), error %d", mi->mi_name,
   1335   1.96      maxv 				    buf, error);
   1336    1.1        ad 				goto fail;
   1337   1.88  christos 			}
   1338  1.133  pgoyette 			(*mod->mod_required)[mod->mod_nrequired++] = mod2;
   1339    1.1        ad 		}
   1340    1.1        ad 	}
   1341    1.1        ad 
   1342    1.1        ad 	/*
   1343   1.15        ad 	 * We loaded all needed modules successfully: perform global
   1344   1.15        ad 	 * relocations and initialize.
   1345    1.1        ad 	 */
   1346  1.136  pgoyette 	{
   1347  1.136  pgoyette 		char xname[MAXMODNAME];
   1348  1.136  pgoyette 
   1349  1.136  pgoyette 		/*
   1350  1.136  pgoyette 		 * In case of error the entire module is gone, so we
   1351  1.136  pgoyette 		 * need to save its name for possible error report.
   1352  1.136  pgoyette 		 */
   1353  1.136  pgoyette 
   1354  1.136  pgoyette 		strlcpy(xname, mi->mi_name, MAXMODNAME);
   1355  1.136  pgoyette 		error = kobj_affix(mod->mod_kobj, mi->mi_name);
   1356  1.136  pgoyette 		if (error != 0) {
   1357  1.136  pgoyette 			module_error("unable to affix module `%s', error %d",
   1358  1.136  pgoyette 			    xname, error);
   1359  1.136  pgoyette 			goto fail2;
   1360  1.136  pgoyette 		}
   1361   1.15        ad 	}
   1362   1.15        ad 
   1363   1.54     pooka 	if (filedict) {
   1364   1.54     pooka 		if (!module_merge_dicts(filedict, props)) {
   1365   1.90  christos 			module_error("module properties failed for %s", name);
   1366   1.54     pooka 			error = EINVAL;
   1367   1.46   jnemeth 			goto fail;
   1368   1.46   jnemeth 		}
   1369   1.46   jnemeth 	}
   1370  1.133  pgoyette 
   1371   1.72  pgoyette 	prev_active = module_active;
   1372   1.12        ad 	module_active = mod;
   1373  1.159  pgoyette 
   1374  1.159  pgoyette 	/*
   1375  1.159  pgoyette 	 * Note that we handle sysctl and evcnt setup _before_ we
   1376  1.159  pgoyette 	 * initialize the module itself.  This maintains a consistent
   1377  1.159  pgoyette 	 * order between built-in and run-time-loaded modules.  If
   1378  1.159  pgoyette 	 * initialization then fails, we'll need to undo these, too.
   1379  1.159  pgoyette 	 */
   1380  1.159  pgoyette 	module_load_sysctl(mod);	/* Set-up module's sysctl if any */
   1381  1.159  pgoyette 	module_load_evcnt(mod);		/* Attach any static evcnt needed */
   1382  1.159  pgoyette 
   1383  1.159  pgoyette 
   1384   1.54     pooka 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
   1385   1.72  pgoyette 	module_active = prev_active;
   1386   1.54     pooka 	if (filedict) {
   1387   1.46   jnemeth 		prop_object_release(filedict);
   1388   1.54     pooka 		filedict = NULL;
   1389   1.46   jnemeth 	}
   1390    1.1        ad 	if (error != 0) {
   1391  1.135  pgoyette 		module_error("modcmd(CMD_INIT) failed for `%s', error %d",
   1392   1.90  christos 		    mi->mi_name, error);
   1393  1.159  pgoyette 		goto fail3;
   1394    1.1        ad 	}
   1395   1.54     pooka 
   1396    1.1        ad 	/*
   1397  1.130  pgoyette 	 * If a recursive load already added a module with the same
   1398  1.130  pgoyette 	 * name, abort.
   1399  1.130  pgoyette 	 */
   1400  1.130  pgoyette 	mod2 = module_lookup(mi->mi_name);
   1401  1.130  pgoyette 	if (mod2 && mod2 != mod) {
   1402  1.130  pgoyette 		module_error("recursive load causes duplicate module `%s'",
   1403  1.130  pgoyette 		    mi->mi_name);
   1404  1.130  pgoyette 		error = EEXIST;
   1405  1.130  pgoyette 		goto fail1;
   1406  1.130  pgoyette 	}
   1407  1.130  pgoyette 
   1408  1.130  pgoyette 	/*
   1409    1.1        ad 	 * Good, the module loaded successfully.  Put it onto the
   1410    1.1        ad 	 * list and add references to its requisite modules.
   1411    1.1        ad 	 */
   1412   1.72  pgoyette 	TAILQ_REMOVE(pending, mod, mod_chain);
   1413   1.27        ad 	module_enqueue(mod);
   1414    1.1        ad 	if (modp != NULL) {
   1415    1.1        ad 		*modp = mod;
   1416    1.1        ad 	}
   1417   1.94  pgoyette 	if (autoload && module_autotime > 0) {
   1418   1.26        ad 		/*
   1419   1.26        ad 		 * Arrange to try unloading the module after
   1420   1.94  pgoyette 		 * a short delay unless auto-unload is disabled.
   1421   1.26        ad 		 */
   1422   1.26        ad 		mod->mod_autotime = time_second + module_autotime;
   1423  1.133  pgoyette 		SET(mod->mod_flags, MODFLG_AUTO_LOADED);
   1424   1.26        ad 		module_thread_kick();
   1425   1.26        ad 	}
   1426  1.133  pgoyette 	SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1427  1.107  pgoyette 	module_print("module `%s' loaded successfully", mi->mi_name);
   1428  1.131       chs 	module_callback_load(mod);
   1429    1.1        ad 	return 0;
   1430    1.7    rumble 
   1431  1.130  pgoyette  fail1:
   1432  1.130  pgoyette 	(*mi->mi_modcmd)(MODULE_CMD_FINI, NULL);
   1433  1.159  pgoyette  fail3:
   1434  1.159  pgoyette 	/*
   1435  1.159  pgoyette 	 * If there were any registered SYSCTL_SETUP funcs, make sure
   1436  1.159  pgoyette 	 * we release the sysctl entries
   1437  1.159  pgoyette 	 */
   1438  1.159  pgoyette 	if (mod->mod_sysctllog) {
   1439  1.159  pgoyette 		sysctl_teardown(&mod->mod_sysctllog);
   1440  1.159  pgoyette 	}
   1441  1.159  pgoyette 	/* Also detach any static evcnt's */
   1442  1.159  pgoyette 	module_unload_evcnt(mod);
   1443    1.1        ad  fail:
   1444   1.15        ad 	kobj_unload(mod->mod_kobj);
   1445   1.15        ad  fail2:
   1446   1.54     pooka 	if (filedict != NULL) {
   1447   1.54     pooka 		prop_object_release(filedict);
   1448   1.54     pooka 		filedict = NULL;
   1449   1.54     pooka 	}
   1450   1.72  pgoyette 	TAILQ_REMOVE(pending, mod, mod_chain);
   1451  1.133  pgoyette 	SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
   1452  1.131       chs 	module_free(mod);
   1453    1.1        ad 	return error;
   1454    1.1        ad }
   1455    1.1        ad 
   1456    1.1        ad /*
   1457    1.1        ad  * module_do_unload:
   1458    1.1        ad  *
   1459    1.1        ad  *	Helper routine: do the dirty work of unloading a module.
   1460    1.1        ad  */
   1461    1.1        ad static int
   1462   1.70  pgoyette module_do_unload(const char *name, bool load_requires_force)
   1463    1.1        ad {
   1464   1.72  pgoyette 	module_t *mod, *prev_active;
   1465    1.1        ad 	int error;
   1466    1.1        ad 	u_int i;
   1467    1.1        ad 
   1468   1.72  pgoyette 	KASSERT(kernconfig_is_held());
   1469   1.85   jnemeth 	KASSERT(name != NULL);
   1470    1.1        ad 
   1471  1.107  pgoyette 	module_print("unload requested for '%s' (%s)", name,
   1472  1.117  christos 	    load_requires_force ? "TRUE" : "FALSE");
   1473    1.1        ad 	mod = module_lookup(name);
   1474    1.1        ad 	if (mod == NULL) {
   1475   1.32  christos 		module_error("module `%s' not found", name);
   1476    1.1        ad 		return ENOENT;
   1477    1.1        ad 	}
   1478   1.59     pooka 	if (mod->mod_refcnt != 0) {
   1479  1.107  pgoyette 		module_print("module `%s' busy (%d refs)", name,
   1480  1.108  pgoyette 		    mod->mod_refcnt);
   1481    1.1        ad 		return EBUSY;
   1482    1.1        ad 	}
   1483   1.76     pooka 
   1484   1.76     pooka 	/*
   1485   1.76     pooka 	 * Builtin secmodels are there to stay.
   1486   1.76     pooka 	 */
   1487   1.76     pooka 	if (mod->mod_source == MODULE_SOURCE_KERNEL &&
   1488   1.76     pooka 	    mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) {
   1489  1.107  pgoyette 		module_print("cannot unload built-in secmodel module `%s'",
   1490  1.107  pgoyette 		    name);
   1491   1.76     pooka 		return EPERM;
   1492   1.76     pooka 	}
   1493   1.76     pooka 
   1494   1.72  pgoyette 	prev_active = module_active;
   1495   1.12        ad 	module_active = mod;
   1496  1.131       chs 	module_callback_unload(mod);
   1497  1.137  pgoyette 
   1498  1.159  pgoyette 	/* let the module clean up after itself */
   1499  1.159  pgoyette 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
   1500  1.159  pgoyette 
   1501  1.137  pgoyette 	/*
   1502  1.137  pgoyette 	 * If there were any registered SYSCTL_SETUP funcs, make sure
   1503  1.159  pgoyette 	 * we release the sysctl entries.  Same for static evcnt.
   1504  1.137  pgoyette 	 */
   1505  1.159  pgoyette 	if (error == 0) {
   1506  1.159  pgoyette 		if (mod->mod_sysctllog) {
   1507  1.159  pgoyette 			sysctl_teardown(&mod->mod_sysctllog);
   1508  1.159  pgoyette 		}
   1509  1.159  pgoyette 		module_unload_evcnt(mod);
   1510  1.137  pgoyette 	}
   1511   1.72  pgoyette 	module_active = prev_active;
   1512    1.1        ad 	if (error != 0) {
   1513  1.159  pgoyette 		module_print("could not unload module `%s' error=%d", name,
   1514   1.33        ad 		    error);
   1515    1.1        ad 		return error;
   1516    1.1        ad 	}
   1517    1.1        ad 	module_count--;
   1518    1.1        ad 	TAILQ_REMOVE(&module_list, mod, mod_chain);
   1519    1.1        ad 	for (i = 0; i < mod->mod_nrequired; i++) {
   1520  1.133  pgoyette 		(*mod->mod_required)[i]->mod_refcnt--;
   1521    1.1        ad 	}
   1522   1.85   jnemeth 	module_print("unloaded module `%s'", name);
   1523    1.1        ad 	if (mod->mod_kobj != NULL) {
   1524    1.1        ad 		kobj_unload(mod->mod_kobj);
   1525    1.1        ad 	}
   1526   1.59     pooka 	if (mod->mod_source == MODULE_SOURCE_KERNEL) {
   1527  1.133  pgoyette 		if (mod->mod_required != NULL) {
   1528  1.133  pgoyette 			/*
   1529  1.133  pgoyette 			 * release "required" resources - will be re-parsed
   1530  1.133  pgoyette 			 * if the module is re-enabled
   1531  1.133  pgoyette 			 */
   1532  1.133  pgoyette 			kmem_free(mod->mod_required,
   1533  1.133  pgoyette 			    mod->mod_arequired * sizeof(module_t *));
   1534  1.133  pgoyette 			mod->mod_nrequired = 0;
   1535  1.133  pgoyette 			mod->mod_arequired = 0;
   1536  1.133  pgoyette 			mod->mod_required = NULL;
   1537  1.133  pgoyette 		}
   1538   1.70  pgoyette 		if (load_requires_force)
   1539   1.70  pgoyette 			module_require_force(mod);
   1540   1.59     pooka 		TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
   1541   1.59     pooka 		module_builtinlist++;
   1542   1.59     pooka 	} else {
   1543  1.131       chs 		module_free(mod);
   1544   1.59     pooka 	}
   1545   1.26        ad 	module_gen++;
   1546    1.1        ad 
   1547    1.1        ad 	return 0;
   1548    1.1        ad }
   1549    1.1        ad 
   1550    1.1        ad /*
   1551    1.1        ad  * module_prime:
   1552    1.1        ad  *
   1553    1.1        ad  *	Push a module loaded by the bootloader onto our internal
   1554    1.1        ad  *	list.
   1555    1.1        ad  */
   1556    1.1        ad int
   1557   1.80  christos module_prime(const char *name, void *base, size_t size)
   1558    1.1        ad {
   1559  1.111  pgoyette 	__link_set_decl(modules, modinfo_t);
   1560  1.111  pgoyette 	modinfo_t *const *mip;
   1561    1.1        ad 	module_t *mod;
   1562    1.1        ad 	int error;
   1563    1.1        ad 
   1564  1.112  pgoyette 	/* Check for module name same as a built-in module */
   1565  1.111  pgoyette 
   1566  1.111  pgoyette 	__link_set_foreach(mip, modules) {
   1567  1.111  pgoyette 		if (*mip == &module_dummy)
   1568  1.111  pgoyette 			continue;
   1569  1.111  pgoyette 		if (strcmp((*mip)->mi_name, name) == 0) {
   1570  1.111  pgoyette 			module_error("module `%s' pushed by boot loader "
   1571  1.111  pgoyette 			    "already exists", name);
   1572  1.111  pgoyette 			return EEXIST;
   1573  1.111  pgoyette 		}
   1574  1.111  pgoyette 	}
   1575  1.112  pgoyette 
   1576  1.112  pgoyette 	/* Also eliminate duplicate boolist entries */
   1577  1.112  pgoyette 
   1578  1.112  pgoyette 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
   1579  1.112  pgoyette 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
   1580  1.112  pgoyette 			module_error("duplicate bootlist entry for module "
   1581  1.112  pgoyette 			    "`%s'", name);
   1582  1.112  pgoyette 			return EEXIST;
   1583  1.112  pgoyette 		}
   1584  1.112  pgoyette 	}
   1585  1.112  pgoyette 
   1586  1.112  pgoyette 	mod = module_newmodule(MODULE_SOURCE_BOOT);
   1587  1.112  pgoyette 	if (mod == NULL) {
   1588  1.112  pgoyette 		return ENOMEM;
   1589  1.112  pgoyette 	}
   1590  1.112  pgoyette 
   1591   1.80  christos 	error = kobj_load_mem(&mod->mod_kobj, name, base, size);
   1592    1.1        ad 	if (error != 0) {
   1593  1.131       chs 		module_free(mod);
   1594   1.90  christos 		module_error("unable to load `%s' pushed by boot loader, "
   1595   1.90  christos 		    "error %d", name, error);
   1596   1.11        ad 		return error;
   1597   1.11        ad 	}
   1598   1.11        ad 	error = module_fetch_info(mod);
   1599   1.11        ad 	if (error != 0) {
   1600   1.11        ad 		kobj_unload(mod->mod_kobj);
   1601  1.131       chs 		module_free(mod);
   1602  1.111  pgoyette 		module_error("unable to fetch_info for `%s' pushed by boot "
   1603  1.111  pgoyette 		    "loader, error %d", name, error);
   1604    1.1        ad 		return error;
   1605    1.1        ad 	}
   1606   1.11        ad 
   1607    1.1        ad 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
   1608    1.1        ad 
   1609    1.1        ad 	return 0;
   1610    1.1        ad }
   1611   1.11        ad 
   1612   1.11        ad /*
   1613   1.11        ad  * module_fetch_into:
   1614   1.11        ad  *
   1615   1.11        ad  *	Fetch modinfo record from a loaded module.
   1616   1.11        ad  */
   1617   1.11        ad static int
   1618   1.11        ad module_fetch_info(module_t *mod)
   1619   1.11        ad {
   1620   1.11        ad 	int error;
   1621   1.11        ad 	void *addr;
   1622   1.11        ad 	size_t size;
   1623   1.11        ad 
   1624   1.11        ad 	/*
   1625   1.11        ad 	 * Find module info record and check compatibility.
   1626   1.11        ad 	 */
   1627   1.11        ad 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
   1628   1.11        ad 	    &addr, &size);
   1629   1.11        ad 	if (error != 0) {
   1630   1.90  christos 		module_error("`link_set_modules' section not present, "
   1631   1.90  christos 		    "error %d", error);
   1632   1.11        ad 		return error;
   1633   1.11        ad 	}
   1634   1.11        ad 	if (size != sizeof(modinfo_t **)) {
   1635  1.161  riastrad 		if (size > sizeof(modinfo_t **) &&
   1636  1.161  riastrad 		    (size % sizeof(modinfo_t **)) == 0) {
   1637  1.161  riastrad 			module_error("`link_set_modules' section wrong size "
   1638  1.161  riastrad 			    "(%zu different MODULE declarations?)",
   1639  1.161  riastrad 			    size / sizeof(modinfo_t **));
   1640  1.161  riastrad 		} else {
   1641  1.161  riastrad 			module_error("`link_set_modules' section wrong size "
   1642  1.161  riastrad 			    "(got %zu, wanted %zu)",
   1643  1.161  riastrad 			    size, sizeof(modinfo_t **));
   1644  1.161  riastrad 		}
   1645   1.84      tron 		return ENOEXEC;
   1646   1.11        ad 	}
   1647   1.11        ad 	mod->mod_info = *(modinfo_t **)addr;
   1648   1.11        ad 
   1649   1.11        ad 	return 0;
   1650   1.11        ad }
   1651   1.12        ad 
   1652   1.12        ad /*
   1653   1.12        ad  * module_find_section:
   1654   1.12        ad  *
   1655   1.12        ad  *	Allows a module that is being initialized to look up a section
   1656   1.12        ad  *	within its ELF object.
   1657   1.12        ad  */
   1658   1.12        ad int
   1659   1.12        ad module_find_section(const char *name, void **addr, size_t *size)
   1660   1.12        ad {
   1661   1.12        ad 
   1662   1.72  pgoyette 	KASSERT(kernconfig_is_held());
   1663   1.12        ad 	KASSERT(module_active != NULL);
   1664   1.12        ad 
   1665   1.12        ad 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
   1666   1.12        ad }
   1667   1.26        ad 
   1668   1.26        ad /*
   1669   1.26        ad  * module_thread:
   1670   1.26        ad  *
   1671   1.26        ad  *	Automatically unload modules.  We try once to unload autoloaded
   1672   1.26        ad  *	modules after module_autotime seconds.  If the system is under
   1673   1.94  pgoyette  *	severe memory pressure, we'll try unloading all modules, else if
   1674   1.94  pgoyette  *	module_autotime is zero, we don't try to unload, even if the
   1675   1.94  pgoyette  *	module was previously scheduled for unload.
   1676   1.26        ad  */
   1677   1.26        ad static void
   1678   1.26        ad module_thread(void *cookie)
   1679   1.26        ad {
   1680   1.26        ad 	module_t *mod, *next;
   1681   1.28        ad 	modinfo_t *mi;
   1682   1.28        ad 	int error;
   1683   1.26        ad 
   1684   1.26        ad 	for (;;) {
   1685   1.72  pgoyette 		kernconfig_lock();
   1686   1.26        ad 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
   1687   1.26        ad 			next = TAILQ_NEXT(mod, mod_chain);
   1688   1.83  jmcneill 
   1689   1.83  jmcneill 			/* skip built-in modules */
   1690   1.61     pooka 			if (mod->mod_source == MODULE_SOURCE_KERNEL)
   1691   1.61     pooka 				continue;
   1692   1.83  jmcneill 			/* skip modules that weren't auto-loaded */
   1693  1.133  pgoyette 			if (!ISSET(mod->mod_flags, MODFLG_AUTO_LOADED))
   1694   1.83  jmcneill 				continue;
   1695   1.83  jmcneill 
   1696  1.151        ad 			if (uvm_availmem(false) < uvmexp.freemin) {
   1697   1.26        ad 				module_thread_ticks = hz;
   1698   1.94  pgoyette 			} else if (module_autotime == 0 ||
   1699   1.94  pgoyette 				   mod->mod_autotime == 0) {
   1700   1.26        ad 				continue;
   1701   1.26        ad 			} else if (time_second < mod->mod_autotime) {
   1702   1.26        ad 				module_thread_ticks = hz;
   1703   1.26        ad 			    	continue;
   1704   1.37        ad 			} else {
   1705   1.26        ad 				mod->mod_autotime = 0;
   1706   1.37        ad 			}
   1707   1.83  jmcneill 
   1708   1.28        ad 			/*
   1709  1.158  riastrad 			 * Ask the module if it can be safely unloaded.
   1710  1.158  riastrad 			 *
   1711  1.158  riastrad 			 * - Modules which have been audited to be OK
   1712  1.158  riastrad 			 *   with that will return 0.
   1713  1.158  riastrad 			 *
   1714  1.158  riastrad 			 * - Modules which have not been audited for
   1715  1.158  riastrad 			 *   safe autounload will return ENOTTY.
   1716  1.158  riastrad 			 *
   1717  1.158  riastrad 			 *   => With kern.module.autounload_unsafe=1,
   1718  1.158  riastrad 			 *      we treat ENOTTY as acceptance.
   1719  1.158  riastrad 			 *
   1720  1.158  riastrad 			 * - Some modules would ping-ping in and out
   1721  1.158  riastrad 			 *   because their use is transient but often.
   1722  1.158  riastrad 			 *   Example: exec_script.  Other modules may
   1723  1.158  riastrad 			 *   still be in use.  These modules can
   1724  1.158  riastrad 			 *   prevent autounload in all cases by
   1725  1.158  riastrad 			 *   returning EBUSY or some other error code.
   1726   1.28        ad 			 */
   1727   1.28        ad 			mi = mod->mod_info;
   1728   1.28        ad 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
   1729  1.158  riastrad 			if (error == 0 ||
   1730  1.158  riastrad 			    (error == ENOTTY && module_autounload_unsafe)) {
   1731   1.70  pgoyette 				(void)module_do_unload(mi->mi_name, false);
   1732  1.107  pgoyette 			} else
   1733  1.107  pgoyette 				module_print("module `%s' declined to be "
   1734  1.107  pgoyette 				    "auto-unloaded error=%d", mi->mi_name,
   1735  1.107  pgoyette 				    error);
   1736   1.26        ad 		}
   1737   1.72  pgoyette 		kernconfig_unlock();
   1738   1.26        ad 
   1739   1.26        ad 		mutex_enter(&module_thread_lock);
   1740   1.26        ad 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
   1741   1.26        ad 		    module_thread_ticks);
   1742   1.26        ad 		module_thread_ticks = 0;
   1743   1.26        ad 		mutex_exit(&module_thread_lock);
   1744   1.26        ad 	}
   1745   1.26        ad }
   1746   1.26        ad 
   1747   1.26        ad /*
   1748   1.26        ad  * module_thread:
   1749   1.26        ad  *
   1750   1.26        ad  *	Kick the module thread into action, perhaps because the
   1751   1.26        ad  *	system is low on memory.
   1752   1.26        ad  */
   1753   1.26        ad void
   1754   1.26        ad module_thread_kick(void)
   1755   1.26        ad {
   1756   1.26        ad 
   1757   1.26        ad 	mutex_enter(&module_thread_lock);
   1758   1.26        ad 	module_thread_ticks = hz;
   1759   1.26        ad 	cv_broadcast(&module_thread_cv);
   1760   1.26        ad 	mutex_exit(&module_thread_lock);
   1761   1.26        ad }
   1762   1.30        ad 
   1763   1.30        ad #ifdef DDB
   1764   1.30        ad /*
   1765   1.30        ad  * module_whatis:
   1766   1.30        ad  *
   1767   1.30        ad  *	Helper routine for DDB.
   1768   1.30        ad  */
   1769   1.30        ad void
   1770   1.30        ad module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1771   1.30        ad {
   1772   1.30        ad 	module_t *mod;
   1773   1.30        ad 	size_t msize;
   1774   1.30        ad 	vaddr_t maddr;
   1775   1.30        ad 
   1776   1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1777   1.43        ad 		if (mod->mod_kobj == NULL) {
   1778   1.43        ad 			continue;
   1779   1.43        ad 		}
   1780   1.49    dyoung 		if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1781   1.49    dyoung 			continue;
   1782   1.30        ad 		if (addr < maddr || addr >= maddr + msize) {
   1783   1.30        ad 			continue;
   1784   1.30        ad 		}
   1785   1.30        ad 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
   1786   1.30        ad 		    (void *)addr, (void *)maddr,
   1787   1.30        ad 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
   1788   1.30        ad 	}
   1789   1.30        ad }
   1790   1.30        ad 
   1791   1.30        ad /*
   1792   1.30        ad  * module_print_list:
   1793   1.30        ad  *
   1794   1.30        ad  *	Helper routine for DDB.
   1795   1.30        ad  */
   1796   1.30        ad void
   1797   1.30        ad module_print_list(void (*pr)(const char *, ...))
   1798   1.30        ad {
   1799   1.30        ad 	const char *src;
   1800   1.30        ad 	module_t *mod;
   1801   1.30        ad 	size_t msize;
   1802   1.30        ad 	vaddr_t maddr;
   1803   1.30        ad 
   1804   1.30        ad 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
   1805   1.30        ad 
   1806   1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1807   1.30        ad 		switch (mod->mod_source) {
   1808   1.30        ad 		case MODULE_SOURCE_KERNEL:
   1809   1.30        ad 			src = "builtin";
   1810   1.30        ad 			break;
   1811   1.30        ad 		case MODULE_SOURCE_FILESYS:
   1812   1.30        ad 			src = "filesys";
   1813   1.30        ad 			break;
   1814   1.30        ad 		case MODULE_SOURCE_BOOT:
   1815   1.30        ad 			src = "boot";
   1816   1.30        ad 			break;
   1817   1.30        ad 		default:
   1818   1.30        ad 			src = "unknown";
   1819   1.30        ad 			break;
   1820   1.30        ad 		}
   1821   1.49    dyoung 		if (mod->mod_kobj == NULL) {
   1822   1.43        ad 			maddr = 0;
   1823   1.43        ad 			msize = 0;
   1824   1.49    dyoung 		} else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1825   1.49    dyoung 			continue;
   1826   1.31        ad 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
   1827   1.30        ad 		    (long)maddr, (long)msize, src);
   1828   1.30        ad 	}
   1829   1.30        ad }
   1830   1.30        ad #endif	/* DDB */
   1831   1.46   jnemeth 
   1832   1.47   jnemeth static bool
   1833   1.47   jnemeth module_merge_dicts(prop_dictionary_t existing_dict,
   1834   1.47   jnemeth 		   const prop_dictionary_t new_dict)
   1835   1.47   jnemeth {
   1836   1.47   jnemeth 	prop_dictionary_keysym_t props_keysym;
   1837   1.47   jnemeth 	prop_object_iterator_t props_iter;
   1838   1.47   jnemeth 	prop_object_t props_obj;
   1839   1.47   jnemeth 	const char *props_key;
   1840   1.47   jnemeth 	bool error;
   1841   1.47   jnemeth 
   1842   1.52   jnemeth 	if (new_dict == NULL) {			/* nothing to merge */
   1843   1.52   jnemeth 		return true;
   1844   1.52   jnemeth 	}
   1845   1.52   jnemeth 
   1846   1.47   jnemeth 	error = false;
   1847   1.47   jnemeth 	props_iter = prop_dictionary_iterator(new_dict);
   1848   1.47   jnemeth 	if (props_iter == NULL) {
   1849   1.47   jnemeth 		return false;
   1850   1.47   jnemeth 	}
   1851   1.47   jnemeth 
   1852   1.47   jnemeth 	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
   1853   1.47   jnemeth 		props_keysym = (prop_dictionary_keysym_t)props_obj;
   1854  1.150   thorpej 		props_key = prop_dictionary_keysym_value(props_keysym);
   1855   1.47   jnemeth 		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
   1856   1.47   jnemeth 		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
   1857   1.47   jnemeth 		    props_key, props_obj)) {
   1858   1.47   jnemeth 			error = true;
   1859   1.47   jnemeth 			goto out;
   1860   1.47   jnemeth 		}
   1861   1.47   jnemeth 	}
   1862   1.47   jnemeth 	error = false;
   1863   1.47   jnemeth 
   1864   1.47   jnemeth out:
   1865   1.47   jnemeth 	prop_object_iterator_release(props_iter);
   1866   1.47   jnemeth 
   1867   1.47   jnemeth 	return !error;
   1868   1.47   jnemeth }
   1869  1.131       chs 
   1870  1.131       chs /*
   1871  1.131       chs  * module_specific_key_create:
   1872  1.131       chs  *
   1873  1.131       chs  *	Create a key for subsystem module-specific data.
   1874  1.131       chs  */
   1875  1.131       chs specificdata_key_t
   1876  1.131       chs module_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1877  1.131       chs {
   1878  1.131       chs 
   1879  1.131       chs 	return specificdata_key_create(module_specificdata_domain, keyp, dtor);
   1880  1.131       chs }
   1881  1.131       chs 
   1882  1.131       chs /*
   1883  1.131       chs  * module_specific_key_delete:
   1884  1.131       chs  *
   1885  1.131       chs  *	Delete a key for subsystem module-specific data.
   1886  1.131       chs  */
   1887  1.131       chs void
   1888  1.131       chs module_specific_key_delete(specificdata_key_t key)
   1889  1.131       chs {
   1890  1.131       chs 
   1891  1.131       chs 	return specificdata_key_delete(module_specificdata_domain, key);
   1892  1.131       chs }
   1893  1.131       chs 
   1894  1.131       chs /*
   1895  1.131       chs  * module_getspecific:
   1896  1.131       chs  *
   1897  1.131       chs  *	Return module-specific data corresponding to the specified key.
   1898  1.131       chs  */
   1899  1.131       chs void *
   1900  1.131       chs module_getspecific(module_t *mod, specificdata_key_t key)
   1901  1.131       chs {
   1902  1.131       chs 
   1903  1.131       chs 	return specificdata_getspecific(module_specificdata_domain,
   1904  1.131       chs 	    &mod->mod_sdref, key);
   1905  1.131       chs }
   1906  1.131       chs 
   1907  1.131       chs /*
   1908  1.131       chs  * module_setspecific:
   1909  1.131       chs  *
   1910  1.131       chs  *	Set module-specific data corresponding to the specified key.
   1911  1.131       chs  */
   1912  1.131       chs void
   1913  1.131       chs module_setspecific(module_t *mod, specificdata_key_t key, void *data)
   1914  1.131       chs {
   1915  1.131       chs 
   1916  1.131       chs 	specificdata_setspecific(module_specificdata_domain,
   1917  1.131       chs 	    &mod->mod_sdref, key, data);
   1918  1.131       chs }
   1919  1.131       chs 
   1920  1.131       chs /*
   1921  1.131       chs  * module_register_callbacks:
   1922  1.131       chs  *
   1923  1.131       chs  *	Register a new set of callbacks to be called on module load/unload.
   1924  1.131       chs  *	Call the load callback on each existing module.
   1925  1.131       chs  *	Return an opaque handle for unregistering these later.
   1926  1.131       chs  */
   1927  1.131       chs void *
   1928  1.131       chs module_register_callbacks(void (*load)(struct module *),
   1929  1.131       chs     void (*unload)(struct module *))
   1930  1.131       chs {
   1931  1.131       chs 	struct module_callbacks *modcb;
   1932  1.131       chs 	struct module *mod;
   1933  1.131       chs 
   1934  1.131       chs 	modcb = kmem_alloc(sizeof(*modcb), KM_SLEEP);
   1935  1.131       chs 	modcb->modcb_load = load;
   1936  1.131       chs 	modcb->modcb_unload = unload;
   1937  1.131       chs 
   1938  1.131       chs 	kernconfig_lock();
   1939  1.131       chs 	TAILQ_INSERT_TAIL(&modcblist, modcb, modcb_list);
   1940  1.155  riastrad 	TAILQ_FOREACH_REVERSE(mod, &module_list, modlist, mod_chain)
   1941  1.131       chs 		load(mod);
   1942  1.131       chs 	kernconfig_unlock();
   1943  1.131       chs 
   1944  1.131       chs 	return modcb;
   1945  1.131       chs }
   1946  1.131       chs 
   1947  1.131       chs /*
   1948  1.131       chs  * module_unregister_callbacks:
   1949  1.131       chs  *
   1950  1.131       chs  *	Unregister a previously-registered set of module load/unload callbacks.
   1951  1.131       chs  *	Call the unload callback on each existing module.
   1952  1.131       chs  */
   1953  1.131       chs void
   1954  1.131       chs module_unregister_callbacks(void *opaque)
   1955  1.131       chs {
   1956  1.131       chs 	struct module_callbacks *modcb;
   1957  1.131       chs 	struct module *mod;
   1958  1.131       chs 
   1959  1.131       chs 	modcb = opaque;
   1960  1.131       chs 	kernconfig_lock();
   1961  1.131       chs 	TAILQ_FOREACH(mod, &module_list, mod_chain)
   1962  1.131       chs 		modcb->modcb_unload(mod);
   1963  1.131       chs 	TAILQ_REMOVE(&modcblist, modcb, modcb_list);
   1964  1.131       chs 	kernconfig_unlock();
   1965  1.131       chs 	kmem_free(modcb, sizeof(*modcb));
   1966  1.131       chs }
   1967  1.131       chs 
   1968  1.131       chs /*
   1969  1.131       chs  * module_callback_load:
   1970  1.131       chs  *
   1971  1.131       chs  *	Helper routine: call all load callbacks on a module being loaded.
   1972  1.131       chs  */
   1973  1.131       chs static void
   1974  1.131       chs module_callback_load(struct module *mod)
   1975  1.131       chs {
   1976  1.131       chs 	struct module_callbacks *modcb;
   1977  1.131       chs 
   1978  1.131       chs 	TAILQ_FOREACH(modcb, &modcblist, modcb_list) {
   1979  1.131       chs 		modcb->modcb_load(mod);
   1980  1.131       chs 	}
   1981  1.131       chs }
   1982  1.131       chs 
   1983  1.131       chs /*
   1984  1.131       chs  * module_callback_unload:
   1985  1.131       chs  *
   1986  1.131       chs  *	Helper routine: call all unload callbacks on a module being unloaded.
   1987  1.131       chs  */
   1988  1.131       chs static void
   1989  1.131       chs module_callback_unload(struct module *mod)
   1990  1.131       chs {
   1991  1.131       chs 	struct module_callbacks *modcb;
   1992  1.131       chs 
   1993  1.131       chs 	TAILQ_FOREACH(modcb, &modcblist, modcb_list) {
   1994  1.131       chs 		modcb->modcb_unload(mod);
   1995  1.131       chs 	}
   1996  1.131       chs }
   1997