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