Home | History | Annotate | Line # | Download | only in kern
kern_module.c revision 1.50
      1  1.50      elad /*	$NetBSD: kern_module.c,v 1.50 2009/10/02 18:50:14 elad 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.50      elad __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.50 2009/10/02 18:50:14 elad Exp $");
     38  1.30        ad 
     39  1.30        ad #ifdef _KERNEL_OPT
     40  1.30        ad #include "opt_ddb.h"
     41  1.42       apb #include "opt_modular.h"
     42  1.30        ad #endif
     43   1.1        ad 
     44   1.1        ad #include <sys/param.h>
     45   1.1        ad #include <sys/systm.h>
     46  1.26        ad #include <sys/kernel.h>
     47   1.1        ad #include <sys/fcntl.h>
     48   1.1        ad #include <sys/proc.h>
     49   1.1        ad #include <sys/kauth.h>
     50   1.1        ad #include <sys/kobj.h>
     51   1.1        ad #include <sys/kmem.h>
     52   1.1        ad #include <sys/module.h>
     53  1.18        ad #include <sys/kauth.h>
     54  1.26        ad #include <sys/kthread.h>
     55  1.34        ad #include <sys/sysctl.h>
     56  1.44   jnemeth #include <sys/namei.h>
     57  1.46   jnemeth #include <sys/lock.h>
     58  1.46   jnemeth #include <sys/vnode.h>
     59  1.46   jnemeth #include <sys/stat.h>
     60   1.1        ad 
     61   1.1        ad #include <uvm/uvm_extern.h>
     62   1.1        ad 
     63   1.1        ad #include <machine/stdarg.h>
     64   1.1        ad 
     65  1.25        ad struct vm_map *module_map;
     66   1.2        ad 
     67   1.1        ad struct modlist	module_list = TAILQ_HEAD_INITIALIZER(module_list);
     68   1.1        ad struct modlist	module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
     69  1.12        ad static module_t	*module_active;
     70  1.16        ad static char	module_base[64];
     71  1.34        ad static int	module_verbose_on;
     72  1.34        ad static int	module_autoload_on = 1;
     73   1.1        ad u_int		module_count;
     74   1.1        ad kmutex_t	module_lock;
     75  1.26        ad u_int		module_autotime = 10;
     76  1.26        ad u_int		module_gen = 1;
     77  1.26        ad static kcondvar_t module_thread_cv;
     78  1.26        ad static kmutex_t module_thread_lock;
     79  1.26        ad static int	module_thread_ticks;
     80   1.1        ad 
     81   1.1        ad /* Ensure that the kernel's link set isn't empty. */
     82   1.1        ad static modinfo_t module_dummy;
     83   1.1        ad __link_set_add_rodata(modules, module_dummy);
     84   1.1        ad 
     85   1.1        ad static module_t	*module_lookup(const char *);
     86   1.9      jmmv static int	module_do_load(const char *, bool, int, prop_dictionary_t,
     87  1.20        ad 		    module_t **, modclass_t class, bool);
     88   1.1        ad static int	module_do_unload(const char *);
     89  1.39  drochner static void	module_error(const char *, ...)
     90  1.39  drochner 			__attribute__((__format__(__printf__,1,2)));
     91  1.39  drochner static void	module_print(const char *, ...)
     92  1.39  drochner 			__attribute__((__format__(__printf__,1,2)));
     93   1.1        ad static int	module_do_builtin(const char *, module_t **);
     94  1.11        ad static int	module_fetch_info(module_t *);
     95  1.26        ad static void	module_thread(void *);
     96  1.46   jnemeth static int	module_load_plist_file(const char *, const bool, void **,
     97  1.46   jnemeth 		    size_t *);
     98  1.47   jnemeth static bool	module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
     99   1.1        ad 
    100   1.1        ad /*
    101   1.1        ad  * module_error:
    102   1.1        ad  *
    103   1.1        ad  *	Utility function: log an error.
    104   1.1        ad  */
    105   1.1        ad static void
    106   1.1        ad module_error(const char *fmt, ...)
    107   1.1        ad {
    108   1.1        ad 	va_list ap;
    109   1.1        ad 
    110   1.1        ad 	va_start(ap, fmt);
    111   1.1        ad 	printf("WARNING: module error: ");
    112   1.1        ad 	vprintf(fmt, ap);
    113   1.1        ad 	printf("\n");
    114   1.1        ad 	va_end(ap);
    115   1.1        ad }
    116   1.1        ad 
    117   1.1        ad /*
    118  1.34        ad  * module_print:
    119  1.34        ad  *
    120  1.34        ad  *	Utility function: log verbose output.
    121  1.34        ad  */
    122  1.34        ad static void
    123  1.34        ad module_print(const char *fmt, ...)
    124  1.34        ad {
    125  1.34        ad 	va_list ap;
    126  1.34        ad 
    127  1.34        ad 	if (module_verbose_on) {
    128  1.34        ad 		va_start(ap, fmt);
    129  1.34        ad 		printf("DEBUG: module: ");
    130  1.34        ad 		vprintf(fmt, ap);
    131  1.34        ad 		printf("\n");
    132  1.34        ad 		va_end(ap);
    133  1.34        ad 	}
    134  1.34        ad }
    135  1.34        ad 
    136  1.34        ad /*
    137   1.1        ad  * module_init:
    138   1.1        ad  *
    139   1.1        ad  *	Initialize the module subsystem.
    140   1.1        ad  */
    141   1.1        ad void
    142   1.1        ad module_init(void)
    143   1.1        ad {
    144  1.25        ad 	extern struct vm_map *module_map;
    145   1.1        ad 
    146  1.25        ad 	if (module_map == NULL) {
    147  1.25        ad 		module_map = kernel_map;
    148  1.25        ad 	}
    149   1.1        ad 	mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
    150  1.26        ad 	cv_init(&module_thread_cv, "modunload");
    151  1.26        ad 	mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
    152  1.13        ad #ifdef MODULAR	/* XXX */
    153  1.11        ad 	module_init_md();
    154  1.12        ad #endif
    155  1.16        ad 
    156  1.16        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    157  1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
    158  1.17        ad 	    machine, osrelease);
    159  1.16        ad #else						/* release */
    160  1.41     rmind 	snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
    161  1.17        ad 	    machine, __NetBSD_Version__ / 100000000,
    162  1.16        ad 	    __NetBSD_Version__ / 1000000 % 100);
    163  1.16        ad #endif
    164  1.50      elad }
    165  1.50      elad 
    166  1.50      elad /*
    167  1.50      elad  * module_init2:
    168  1.50      elad  *
    169  1.50      elad  *	Start the auto unload kthread.
    170  1.50      elad  */
    171  1.50      elad void
    172  1.50      elad module_init2(void)
    173  1.50      elad {
    174  1.50      elad 	int error;
    175  1.26        ad 
    176  1.26        ad 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
    177  1.26        ad 	    NULL, NULL, "modunload");
    178  1.26        ad 	if (error != 0)
    179  1.26        ad 		panic("module_init: %d", error);
    180   1.1        ad }
    181   1.1        ad 
    182  1.34        ad SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup")
    183  1.34        ad {
    184  1.34        ad 	const struct sysctlnode *node = NULL;
    185  1.34        ad 
    186  1.34        ad 	sysctl_createv(clog, 0, NULL, NULL,
    187  1.34        ad 		CTLFLAG_PERMANENT,
    188  1.34        ad 		CTLTYPE_NODE, "kern", NULL,
    189  1.34        ad 		NULL, 0, NULL, 0,
    190  1.34        ad 		CTL_KERN, CTL_EOL);
    191  1.34        ad 	sysctl_createv(clog, 0, NULL, &node,
    192  1.34        ad 		CTLFLAG_PERMANENT,
    193  1.34        ad 		CTLTYPE_NODE, "module",
    194  1.34        ad 		SYSCTL_DESCR("Module options"),
    195  1.34        ad 		NULL, 0, NULL, 0,
    196  1.34        ad 		CTL_KERN, CTL_CREATE, CTL_EOL);
    197  1.34        ad 
    198  1.34        ad 	if (node == NULL)
    199  1.34        ad 		return;
    200  1.34        ad 
    201  1.34        ad 	sysctl_createv(clog, 0, &node, NULL,
    202  1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    203  1.34        ad 		CTLTYPE_INT, "autoload",
    204  1.34        ad 		SYSCTL_DESCR("Enable automatic load of modules"),
    205  1.34        ad 		NULL, 0, &module_autoload_on, 0,
    206  1.34        ad 		CTL_CREATE, CTL_EOL);
    207  1.34        ad 	sysctl_createv(clog, 0, &node, NULL,
    208  1.34        ad 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    209  1.34        ad 		CTLTYPE_INT, "verbose",
    210  1.34        ad 		SYSCTL_DESCR("Enable verbose output"),
    211  1.34        ad 		NULL, 0, &module_verbose_on, 0,
    212  1.34        ad 		CTL_CREATE, CTL_EOL);
    213  1.34        ad }
    214  1.34        ad 
    215   1.1        ad /*
    216   1.1        ad  * module_init_class:
    217   1.1        ad  *
    218   1.1        ad  *	Initialize all built-in and pre-loaded modules of the
    219   1.1        ad  *	specified class.
    220   1.1        ad  */
    221   1.1        ad void
    222   1.1        ad module_init_class(modclass_t class)
    223   1.1        ad {
    224   1.1        ad 	__link_set_decl(modules, modinfo_t);
    225   1.1        ad 	modinfo_t *const *mip, *mi;
    226   1.1        ad 	module_t *mod;
    227   1.1        ad 
    228   1.1        ad 	mutex_enter(&module_lock);
    229   1.1        ad 	/*
    230   1.1        ad 	 * Builtins first.  These can't depend on pre-loaded modules.
    231   1.1        ad 	 */
    232   1.1        ad 	__link_set_foreach(mip, modules) {
    233   1.1        ad 		mi = *mip;
    234   1.1        ad 		if (mi == &module_dummy) {
    235   1.1        ad 			continue;
    236   1.1        ad 		}
    237   1.1        ad 		if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    238   1.1        ad 			continue;
    239   1.1        ad 		}
    240   1.1        ad 		(void)module_do_builtin(mi->mi_name, NULL);
    241   1.1        ad 	}
    242   1.1        ad 	/*
    243   1.1        ad 	 * Now preloaded modules.  These will be pulled off the
    244   1.1        ad 	 * list as we call module_do_load();
    245   1.1        ad 	 */
    246  1.11        ad 	do {
    247  1.11        ad 		TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    248  1.11        ad 			mi = mod->mod_info;
    249  1.11        ad 			if (class != MODULE_CLASS_ANY &&
    250  1.11        ad 			    class != mi->mi_class)
    251  1.11        ad 				continue;
    252  1.18        ad 			module_do_load(mi->mi_name, false, 0, NULL, NULL,
    253  1.38  christos 			    class, false);
    254  1.11        ad 			break;
    255  1.11        ad 		}
    256  1.11        ad 	} while (mod != NULL);
    257   1.1        ad 	mutex_exit(&module_lock);
    258   1.1        ad }
    259   1.1        ad 
    260   1.1        ad /*
    261  1.21        ad  * module_compatible:
    262   1.1        ad  *
    263  1.21        ad  *	Return true if the two supplied kernel versions are said to
    264  1.21        ad  *	have the same binary interface for kernel code.  The entire
    265  1.21        ad  *	version is signficant for the development tree (-current),
    266  1.21        ad  *	major and minor versions are significant for official
    267  1.21        ad  *	releases of the system.
    268   1.1        ad  */
    269  1.22     pooka bool
    270  1.21        ad module_compatible(int v1, int v2)
    271   1.1        ad {
    272   1.1        ad 
    273  1.21        ad #if __NetBSD_Version__ / 1000000 % 100 == 99	/* -current */
    274  1.21        ad 	return v1 == v2;
    275  1.21        ad #else						/* release */
    276  1.21        ad 	return abs(v1 - v2) < 10000;
    277  1.21        ad #endif
    278   1.1        ad }
    279   1.1        ad 
    280   1.1        ad /*
    281   1.1        ad  * module_load:
    282   1.1        ad  *
    283   1.9      jmmv  *	Load a single module from the file system.
    284   1.1        ad  */
    285   1.1        ad int
    286  1.18        ad module_load(const char *filename, int flags, prop_dictionary_t props,
    287  1.23        ad 	    modclass_t class)
    288   1.1        ad {
    289   1.1        ad 	int error;
    290   1.1        ad 
    291  1.18        ad 	/* Authorize. */
    292  1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    293  1.18        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
    294  1.18        ad 	if (error != 0) {
    295  1.18        ad 		return error;
    296  1.18        ad 	}
    297  1.18        ad 
    298   1.1        ad 	mutex_enter(&module_lock);
    299  1.20        ad 	error = module_do_load(filename, false, flags, props, NULL, class,
    300  1.23        ad 	    false);
    301   1.1        ad 	mutex_exit(&module_lock);
    302   1.1        ad 
    303   1.1        ad 	return error;
    304   1.1        ad }
    305   1.1        ad 
    306   1.1        ad /*
    307  1.23        ad  * module_autoload:
    308  1.23        ad  *
    309  1.23        ad  *	Load a single module from the file system, system initiated.
    310  1.23        ad  */
    311  1.23        ad int
    312  1.23        ad module_autoload(const char *filename, modclass_t class)
    313  1.23        ad {
    314  1.23        ad 	int error;
    315  1.23        ad 
    316  1.23        ad 	KASSERT(mutex_owned(&module_lock));
    317  1.23        ad 
    318  1.34        ad 	/* Nothing if the user has disabled it. */
    319  1.34        ad 	if (!module_autoload_on) {
    320  1.34        ad 		return EPERM;
    321  1.34        ad 	}
    322  1.34        ad 
    323  1.29        ad         /* Disallow path seperators and magic symlinks. */
    324  1.29        ad         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
    325  1.29        ad             strchr(filename, '.') != NULL) {
    326  1.29        ad         	return EPERM;
    327  1.29        ad 	}
    328  1.29        ad 
    329  1.23        ad 	/* Authorize. */
    330  1.23        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    331  1.23        ad 	    0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
    332  1.23        ad 	if (error != 0) {
    333  1.23        ad 		return error;
    334  1.23        ad 	}
    335  1.23        ad 
    336  1.23        ad 	return module_do_load(filename, false, 0, NULL, NULL, class, true);
    337  1.23        ad }
    338  1.23        ad 
    339  1.23        ad /*
    340   1.1        ad  * module_unload:
    341   1.1        ad  *
    342   1.1        ad  *	Find and unload a module by name.
    343   1.1        ad  */
    344   1.1        ad int
    345   1.1        ad module_unload(const char *name)
    346   1.1        ad {
    347   1.1        ad 	int error;
    348   1.1        ad 
    349  1.18        ad 	/* Authorize. */
    350  1.18        ad 	error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
    351  1.18        ad 	    0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
    352  1.18        ad 	if (error != 0) {
    353  1.18        ad 		return error;
    354  1.18        ad 	}
    355  1.18        ad 
    356   1.1        ad 	mutex_enter(&module_lock);
    357   1.1        ad 	error = module_do_unload(name);
    358   1.1        ad 	mutex_exit(&module_lock);
    359   1.1        ad 
    360   1.1        ad 	return error;
    361   1.1        ad }
    362   1.1        ad 
    363   1.1        ad /*
    364   1.1        ad  * module_lookup:
    365   1.1        ad  *
    366   1.1        ad  *	Look up a module by name.
    367   1.1        ad  */
    368   1.1        ad module_t *
    369   1.1        ad module_lookup(const char *name)
    370   1.1        ad {
    371   1.1        ad 	module_t *mod;
    372   1.1        ad 
    373   1.1        ad 	KASSERT(mutex_owned(&module_lock));
    374   1.1        ad 
    375   1.1        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
    376   1.1        ad 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    377   1.1        ad 			break;
    378   1.1        ad 		}
    379   1.1        ad 	}
    380   1.1        ad 
    381   1.1        ad 	return mod;
    382   1.1        ad }
    383   1.1        ad 
    384   1.1        ad /*
    385   1.1        ad  * module_hold:
    386   1.1        ad  *
    387   1.1        ad  *	Add a single reference to a module.  It's the caller's
    388   1.1        ad  *	responsibility to ensure that the reference is dropped
    389   1.1        ad  *	later.
    390   1.1        ad  */
    391   1.1        ad int
    392   1.1        ad module_hold(const char *name)
    393   1.1        ad {
    394   1.1        ad 	module_t *mod;
    395   1.1        ad 
    396   1.1        ad 	mutex_enter(&module_lock);
    397   1.1        ad 	mod = module_lookup(name);
    398   1.1        ad 	if (mod == NULL) {
    399   1.1        ad 		mutex_exit(&module_lock);
    400   1.1        ad 		return ENOENT;
    401   1.1        ad 	}
    402   1.1        ad 	mod->mod_refcnt++;
    403   1.1        ad 	mutex_exit(&module_lock);
    404   1.1        ad 
    405   1.1        ad 	return 0;
    406   1.1        ad }
    407   1.1        ad 
    408   1.1        ad /*
    409   1.1        ad  * module_rele:
    410   1.1        ad  *
    411   1.1        ad  *	Release a reference acquired with module_hold().
    412   1.1        ad  */
    413   1.1        ad void
    414   1.1        ad module_rele(const char *name)
    415   1.1        ad {
    416   1.1        ad 	module_t *mod;
    417   1.1        ad 
    418   1.1        ad 	mutex_enter(&module_lock);
    419   1.1        ad 	mod = module_lookup(name);
    420   1.1        ad 	if (mod == NULL) {
    421   1.1        ad 		mutex_exit(&module_lock);
    422   1.1        ad 		panic("module_rele: gone");
    423   1.1        ad 	}
    424   1.1        ad 	mod->mod_refcnt--;
    425   1.1        ad 	mutex_exit(&module_lock);
    426   1.1        ad }
    427   1.1        ad 
    428   1.1        ad /*
    429  1.27        ad  * module_enqueue:
    430  1.27        ad  *
    431  1.27        ad  *	Put a module onto the global list and update counters.
    432  1.27        ad  */
    433  1.27        ad static void
    434  1.27        ad module_enqueue(module_t *mod)
    435  1.27        ad {
    436  1.27        ad 	int i;
    437  1.27        ad 
    438  1.27        ad 	/*
    439  1.27        ad 	 * If there are requisite modules, put at the head of the queue.
    440  1.27        ad 	 * This is so that autounload can unload requisite modules with
    441  1.27        ad 	 * only one pass through the queue.
    442  1.27        ad 	 */
    443  1.27        ad 	if (mod->mod_nrequired) {
    444  1.27        ad 		TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
    445  1.27        ad 
    446  1.27        ad 		/* Add references to the requisite modules. */
    447  1.27        ad 		for (i = 0; i < mod->mod_nrequired; i++) {
    448  1.27        ad 			KASSERT(mod->mod_required[i] != NULL);
    449  1.27        ad 			mod->mod_required[i]->mod_refcnt++;
    450  1.27        ad 		}
    451  1.27        ad 	} else {
    452  1.27        ad 		TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
    453  1.27        ad 	}
    454  1.27        ad 	module_count++;
    455  1.27        ad 	module_gen++;
    456  1.27        ad }
    457  1.27        ad 
    458  1.27        ad /*
    459   1.1        ad  * module_do_builtin:
    460   1.1        ad  *
    461   1.1        ad  *	Initialize a single module from the list of modules that are
    462   1.1        ad  *	built into the kernel (linked into the kernel image).
    463   1.1        ad  */
    464   1.1        ad static int
    465   1.1        ad module_do_builtin(const char *name, module_t **modp)
    466   1.1        ad {
    467   1.1        ad 	__link_set_decl(modules, modinfo_t);
    468   1.1        ad 	modinfo_t *const *mip;
    469   1.1        ad 	const char *p, *s;
    470   1.1        ad 	char buf[MAXMODNAME];
    471   1.1        ad 	modinfo_t *mi;
    472   1.1        ad 	module_t *mod, *mod2;
    473   1.1        ad 	size_t len;
    474  1.27        ad 	int error;
    475   1.1        ad 
    476   1.1        ad 	KASSERT(mutex_owned(&module_lock));
    477   1.1        ad 
    478   1.1        ad 	/*
    479   1.1        ad 	 * Check to see if already loaded.
    480   1.1        ad 	 */
    481   1.1        ad 	if ((mod = module_lookup(name)) != NULL) {
    482   1.1        ad 		if (modp != NULL) {
    483   1.1        ad 			*modp = mod;
    484   1.1        ad 		}
    485   1.1        ad 		return 0;
    486   1.1        ad 	}
    487   1.1        ad 
    488   1.1        ad 	/*
    489   1.1        ad 	 * Search the list to see if we have a module by this name.
    490   1.1        ad 	 */
    491   1.1        ad 	error = ENOENT;
    492   1.1        ad 	__link_set_foreach(mip, modules) {
    493   1.1        ad 		mi = *mip;
    494   1.1        ad 		if (mi == &module_dummy) {
    495   1.1        ad 			continue;
    496   1.1        ad 		}
    497   1.1        ad 		if (strcmp(mi->mi_name, name) == 0) {
    498   1.1        ad 			error = 0;
    499   1.1        ad 			break;
    500   1.1        ad 		}
    501   1.1        ad 	}
    502   1.1        ad 	if (error != 0) {
    503  1.38  christos 		module_error("can't find `%s'", name);
    504   1.1        ad 		return error;
    505   1.1        ad 	}
    506   1.1        ad 
    507   1.1        ad 	/*
    508   1.1        ad 	 * Initialize pre-requisites.
    509   1.1        ad 	 */
    510   1.1        ad 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    511   1.1        ad 	if (mod == NULL) {
    512  1.38  christos 		module_error("out of memory for `%s'", name);
    513   1.1        ad 		return ENOMEM;
    514   1.1        ad 	}
    515   1.1        ad 	if (modp != NULL) {
    516   1.1        ad 		*modp = mod;
    517   1.1        ad 	}
    518   1.1        ad 	if (mi->mi_required != NULL) {
    519   1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
    520   1.1        ad 			if (*s == ',')
    521   1.1        ad 				s++;
    522   1.1        ad 			p = s;
    523   1.1        ad 			while (*p != '\0' && *p != ',')
    524   1.1        ad 				p++;
    525   1.1        ad 			len = min(p - s + 1, sizeof(buf));
    526   1.1        ad 			strlcpy(buf, s, len);
    527   1.1        ad 			if (buf[0] == '\0')
    528   1.1        ad 				break;
    529   1.1        ad 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    530   1.1        ad 				module_error("too many required modules");
    531   1.1        ad 				kmem_free(mod, sizeof(*mod));
    532   1.1        ad 				return EINVAL;
    533   1.1        ad 			}
    534   1.1        ad 			error = module_do_builtin(buf, &mod2);
    535   1.1        ad 			if (error != 0) {
    536   1.1        ad 				kmem_free(mod, sizeof(*mod));
    537   1.1        ad 				return error;
    538   1.1        ad 			}
    539   1.1        ad 			mod->mod_required[mod->mod_nrequired++] = mod2;
    540   1.1        ad 		}
    541   1.1        ad 	}
    542   1.1        ad 
    543   1.1        ad 	/*
    544   1.1        ad 	 * Try to initialize the module.
    545   1.1        ad 	 */
    546  1.12        ad 	KASSERT(module_active == NULL);
    547  1.12        ad 	module_active = mod;
    548   1.1        ad 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
    549  1.12        ad 	module_active = NULL;
    550   1.1        ad 	if (error != 0) {
    551   1.1        ad 		module_error("builtin module `%s' "
    552   1.1        ad 		    "failed to init", mi->mi_name);
    553   1.1        ad 		kmem_free(mod, sizeof(*mod));
    554   1.1        ad 		return error;
    555   1.1        ad 	}
    556  1.50      elad 	if (mi->mi_class == MODULE_CLASS_SECMODEL)
    557  1.50      elad 		secmodel_register();
    558   1.1        ad 	mod->mod_info = mi;
    559   1.1        ad 	mod->mod_source = MODULE_SOURCE_KERNEL;
    560  1.27        ad 	module_enqueue(mod);
    561   1.1        ad 	return 0;
    562   1.1        ad }
    563   1.1        ad 
    564   1.1        ad /*
    565   1.1        ad  * module_do_load:
    566   1.1        ad  *
    567   1.1        ad  *	Helper routine: load a module from the file system, or one
    568   1.1        ad  *	pushed by the boot loader.
    569   1.1        ad  */
    570   1.1        ad static int
    571  1.27        ad module_do_load(const char *name, bool isdep, int flags,
    572  1.20        ad 	       prop_dictionary_t props, module_t **modp, modclass_t class,
    573  1.20        ad 	       bool autoload)
    574   1.1        ad {
    575   1.1        ad 	static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
    576   1.1        ad 	static int depth;
    577   1.1        ad 	const int maxdepth = 6;
    578   1.1        ad 	modinfo_t *mi;
    579   1.1        ad 	module_t *mod, *mod2;
    580  1.46   jnemeth 	prop_dictionary_t filedict;
    581  1.46   jnemeth 	void *plist;
    582  1.44   jnemeth 	char buf[MAXMODNAME], *path;
    583   1.1        ad 	const char *s, *p;
    584   1.1        ad 	int error;
    585  1.46   jnemeth 	size_t len, plistlen;
    586  1.45   jnemeth 	bool nochroot;
    587   1.1        ad 
    588   1.1        ad 	KASSERT(mutex_owned(&module_lock));
    589   1.1        ad 
    590  1.46   jnemeth 	filedict = NULL;
    591  1.46   jnemeth 	path = NULL;
    592   1.1        ad 	error = 0;
    593  1.46   jnemeth 	nochroot = false;
    594   1.1        ad 
    595   1.1        ad 	/*
    596   1.1        ad 	 * Avoid recursing too far.
    597   1.1        ad 	 */
    598   1.1        ad 	if (++depth > maxdepth) {
    599   1.1        ad 		module_error("too many required modules");
    600   1.1        ad 		depth--;
    601   1.1        ad 		return EMLINK;
    602   1.1        ad 	}
    603   1.1        ad 
    604   1.1        ad 	/*
    605   1.1        ad 	 * Load the module and link.  Before going to the file system,
    606   1.1        ad 	 * scan the list of modules loaded by the boot loader.  Just
    607   1.1        ad 	 * before init is started the list of modules loaded at boot
    608   1.1        ad 	 * will be purged.  Before init is started we can assume that
    609  1.27        ad 	 * `name' is a module name and not a path name.
    610   1.1        ad 	 */
    611   1.1        ad 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
    612  1.27        ad 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
    613   1.1        ad 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
    614   1.1        ad 			break;
    615   1.1        ad 		}
    616   1.1        ad 	}
    617  1.14    rumble 	if (mod != NULL) {
    618  1.14    rumble 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    619  1.14    rumble 	} else {
    620  1.27        ad 		/*
    621  1.27        ad 		 * If a requisite module, check to see if it is
    622  1.27        ad 		 * already present.
    623  1.27        ad 		 */
    624  1.27        ad 		if (isdep) {
    625  1.27        ad 			TAILQ_FOREACH(mod, &module_list, mod_chain) {
    626  1.27        ad 				if (strcmp(mod->mod_info->mi_name, name) == 0) {
    627  1.27        ad 					break;
    628  1.27        ad 				}
    629  1.27        ad 			}
    630  1.27        ad 			if (mod != NULL) {
    631  1.27        ad 				if (modp != NULL) {
    632  1.27        ad 					*modp = mod;
    633  1.27        ad 				}
    634  1.27        ad 				depth--;
    635  1.27        ad 				return 0;
    636  1.27        ad 			}
    637  1.27        ad 		}
    638   1.1        ad 		mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    639   1.1        ad 		if (mod == NULL) {
    640  1.38  christos 			module_error("out of memory for `%s'", name);
    641   1.1        ad 			depth--;
    642   1.1        ad 			return ENOMEM;
    643   1.1        ad 		}
    644  1.44   jnemeth 		path = PNBUF_GET();
    645  1.44   jnemeth 		if (!autoload) {
    646  1.45   jnemeth 			nochroot = false;
    647  1.44   jnemeth 			snprintf(path, MAXPATHLEN, "%s", name);
    648  1.45   jnemeth 			error = kobj_load_file(&mod->mod_kobj, path, nochroot);
    649  1.44   jnemeth 		}
    650  1.44   jnemeth 		if (autoload || (error == ENOENT)) {
    651  1.45   jnemeth 			nochroot = true;
    652  1.44   jnemeth 			snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod",
    653  1.44   jnemeth 			    module_base, name, name);
    654  1.45   jnemeth 			error = kobj_load_file(&mod->mod_kobj, path, nochroot);
    655  1.44   jnemeth 		}
    656   1.1        ad 		if (error != 0) {
    657   1.1        ad 			kmem_free(mod, sizeof(*mod));
    658   1.1        ad 			depth--;
    659  1.44   jnemeth 			PNBUF_PUT(path);
    660  1.34        ad 			if (autoload) {
    661  1.34        ad 				module_print("Cannot load kernel object `%s'"
    662  1.34        ad 				    " error=%d", name, error);
    663  1.34        ad 			} else {
    664  1.32  christos 				module_error("Cannot load kernel object `%s'"
    665  1.33        ad 				    " error=%d", name, error);
    666  1.34        ad 			}
    667   1.8    rumble 			return error;
    668   1.7    rumble 		}
    669  1.14    rumble 		TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
    670   1.1        ad 		mod->mod_source = MODULE_SOURCE_FILESYS;
    671  1.11        ad 		error = module_fetch_info(mod);
    672  1.11        ad 		if (error != 0) {
    673  1.33        ad 			module_error("cannot fetch module info for `%s'",
    674  1.33        ad 			    name);
    675  1.11        ad 			goto fail;
    676  1.11        ad 		}
    677   1.1        ad 	}
    678   1.1        ad 
    679   1.1        ad 	/*
    680  1.11        ad 	 * Check compatibility.
    681   1.1        ad 	 */
    682   1.1        ad 	mi = mod->mod_info;
    683   1.3    rumble 	if (strlen(mi->mi_name) >= MAXMODNAME) {
    684   1.3    rumble 		error = EINVAL;
    685  1.32  christos 		module_error("module name `%s' too long", mi->mi_name);
    686   1.3    rumble 		goto fail;
    687   1.3    rumble 	}
    688  1.21        ad 	if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
    689  1.33        ad 		module_error("module built for `%d', system `%d'",
    690  1.32  christos 		    mi->mi_version, __NetBSD_Version__);
    691  1.24        ad 		if ((flags & MODCTL_LOAD_FORCE) != 0) {
    692  1.24        ad 			module_error("forced load, system may be unstable");
    693  1.24        ad 		} else {
    694  1.24        ad 			error = EPROGMISMATCH;
    695  1.24        ad 			goto fail;
    696  1.24        ad 		}
    697  1.21        ad 	}
    698   1.3    rumble 
    699   1.1        ad 	/*
    700  1.18        ad 	 * If a specific kind of module was requested, ensure that we have
    701  1.18        ad 	 * a match.
    702  1.18        ad 	 */
    703  1.18        ad 	if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
    704  1.34        ad 		module_print("incompatible module class for `%s' (%d != %d)",
    705  1.32  christos 		    name, class, mi->mi_class);
    706  1.18        ad 		error = ENOENT;
    707  1.18        ad 		goto fail;
    708  1.18        ad 	}
    709  1.18        ad 
    710  1.18        ad 	/*
    711  1.27        ad 	 * If loading a dependency, `name' is a plain module name.
    712   1.1        ad 	 * The name must match.
    713   1.1        ad 	 */
    714  1.27        ad 	if (isdep && strcmp(mi->mi_name, name) != 0) {
    715  1.32  christos 		module_error("dependency name mismatch (`%s' != `%s')",
    716  1.32  christos 		    name, mi->mi_name);
    717   1.1        ad 		error = ENOENT;
    718   1.1        ad 		goto fail;
    719   1.1        ad 	}
    720   1.1        ad 
    721   1.1        ad 	/*
    722   1.3    rumble 	 * Check to see if the module is already loaded.  If so, we may
    723   1.3    rumble 	 * have been recursively called to handle a dependency, so be sure
    724   1.3    rumble 	 * to set modp.
    725   1.1        ad 	 */
    726   1.3    rumble 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
    727   1.5    rumble 		if (modp != NULL)
    728   1.5    rumble 			*modp = mod2;
    729  1.34        ad 		module_print("module `%s' already loaded", mi->mi_name);
    730   1.1        ad 		error = EEXIST;
    731   1.1        ad 		goto fail;
    732   1.1        ad 	}
    733   1.3    rumble 
    734   1.3    rumble 	/*
    735   1.3    rumble 	 * Block circular dependencies.
    736   1.3    rumble 	 */
    737   1.1        ad 	TAILQ_FOREACH(mod2, &pending, mod_chain) {
    738   1.1        ad 		if (mod == mod2) {
    739   1.1        ad 			continue;
    740   1.1        ad 		}
    741   1.1        ad 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
    742   1.1        ad 		    	error = EDEADLK;
    743  1.32  christos 			module_error("circular dependency detected for `%s'",
    744  1.32  christos 			    mi->mi_name);
    745   1.1        ad 		    	goto fail;
    746   1.1        ad 		}
    747   1.1        ad 	}
    748   1.1        ad 
    749   1.1        ad 	/*
    750   1.1        ad 	 * Now try to load any requisite modules.
    751   1.1        ad 	 */
    752   1.1        ad 	if (mi->mi_required != NULL) {
    753   1.1        ad 		for (s = mi->mi_required; *s != '\0'; s = p) {
    754   1.1        ad 			if (*s == ',')
    755   1.1        ad 				s++;
    756   1.1        ad 			p = s;
    757   1.1        ad 			while (*p != '\0' && *p != ',')
    758   1.1        ad 				p++;
    759   1.3    rumble 			len = p - s + 1;
    760   1.3    rumble 			if (len >= MAXMODNAME) {
    761   1.3    rumble 				error = EINVAL;
    762  1.32  christos 				module_error("required module name `%s'"
    763  1.32  christos 				    " too long", mi->mi_required);
    764   1.3    rumble 				goto fail;
    765   1.3    rumble 			}
    766   1.1        ad 			strlcpy(buf, s, len);
    767   1.1        ad 			if (buf[0] == '\0')
    768   1.1        ad 				break;
    769   1.1        ad 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
    770   1.1        ad 				error = EINVAL;
    771  1.32  christos 				module_error("too many required modules (%d)",
    772  1.32  christos 				    mod->mod_nrequired);
    773   1.1        ad 				goto fail;
    774   1.1        ad 			}
    775   1.6    rumble 			if (strcmp(buf, mi->mi_name) == 0) {
    776   1.6    rumble 				error = EDEADLK;
    777  1.32  christos 				module_error("self-dependency detected for "
    778  1.32  christos 				   "`%s'", mi->mi_name);
    779   1.6    rumble 				goto fail;
    780   1.6    rumble 			}
    781   1.9      jmmv 			error = module_do_load(buf, true, flags, NULL,
    782  1.18        ad 			    &mod->mod_required[mod->mod_nrequired++],
    783  1.20        ad 			    MODULE_CLASS_ANY, true);
    784  1.27        ad 			if (error != 0)
    785   1.1        ad 				goto fail;
    786   1.1        ad 		}
    787   1.1        ad 	}
    788   1.1        ad 
    789   1.1        ad 	/*
    790  1.15        ad 	 * We loaded all needed modules successfully: perform global
    791  1.15        ad 	 * relocations and initialize.
    792   1.1        ad 	 */
    793  1.15        ad 	error = kobj_affix(mod->mod_kobj, mi->mi_name);
    794  1.15        ad 	if (error != 0) {
    795  1.36        ad 		/* Cannot touch 'mi' as the module is now gone. */
    796  1.36        ad 		module_error("unable to affix module `%s'", name);
    797  1.15        ad 		goto fail2;
    798  1.15        ad 	}
    799  1.15        ad 
    800  1.46   jnemeth 	/*
    801  1.46   jnemeth 	 * Load and process <module>.prop if it exists.
    802  1.46   jnemeth 	 */
    803  1.48   jnemeth 	if (((flags & MODCTL_NO_PROP) == 0) &&
    804  1.48   jnemeth 	    (mod->mod_source == MODULE_SOURCE_FILESYS)) {
    805  1.46   jnemeth 		error = module_load_plist_file(path, nochroot, &plist,
    806  1.46   jnemeth 		    &plistlen);
    807  1.46   jnemeth 		if (error != 0) {
    808  1.46   jnemeth 			module_print("plist load returned error %d for `%s'",
    809  1.46   jnemeth 			    error, path);
    810  1.46   jnemeth 		} else {
    811  1.46   jnemeth 			filedict = prop_dictionary_internalize(plist);
    812  1.46   jnemeth 			if (filedict == NULL) {
    813  1.46   jnemeth 				error = EINVAL;
    814  1.47   jnemeth 			} else if (!module_merge_dicts(filedict, props)) {
    815  1.47   jnemeth 				error = EINVAL;
    816  1.47   jnemeth 				prop_object_release(filedict);
    817  1.47   jnemeth 				filedict = NULL;
    818  1.46   jnemeth 			}
    819  1.46   jnemeth 		}
    820  1.46   jnemeth 		if (plist != NULL) {
    821  1.46   jnemeth 			kmem_free(plist, PAGE_SIZE);
    822  1.46   jnemeth 		}
    823  1.46   jnemeth 		if ((error != 0) && (error != ENOENT)) {
    824  1.46   jnemeth 			goto fail;
    825  1.46   jnemeth 		}
    826  1.46   jnemeth 	}
    827  1.46   jnemeth 
    828  1.12        ad 	KASSERT(module_active == NULL);
    829  1.12        ad 	module_active = mod;
    830  1.46   jnemeth 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, (filedict != NULL) ?
    831  1.47   jnemeth 	    filedict : props);	/* props will have been merged with filedict */
    832  1.12        ad 	module_active = NULL;
    833  1.46   jnemeth 	if (filedict != NULL) {
    834  1.46   jnemeth 		prop_object_release(filedict);
    835  1.46   jnemeth 	}
    836   1.1        ad 	if (error != 0) {
    837  1.32  christos 		module_error("modcmd function returned error %d for `%s'",
    838  1.32  christos 		    error, mi->mi_name);
    839   1.1        ad 		goto fail;
    840   1.1        ad 	}
    841  1.50      elad 	if (mi->mi_class == MODULE_CLASS_SECMODEL)
    842  1.50      elad 		secmodel_register();
    843   1.1        ad 
    844   1.1        ad 	/*
    845   1.1        ad 	 * Good, the module loaded successfully.  Put it onto the
    846   1.1        ad 	 * list and add references to its requisite modules.
    847   1.1        ad 	 */
    848   1.1        ad 	TAILQ_REMOVE(&pending, mod, mod_chain);
    849  1.27        ad 	module_enqueue(mod);
    850   1.1        ad 	if (modp != NULL) {
    851   1.1        ad 		*modp = mod;
    852   1.1        ad 	}
    853  1.26        ad 	if (autoload) {
    854  1.26        ad 		/*
    855  1.26        ad 		 * Arrange to try unloading the module after
    856  1.26        ad 		 * a short delay.
    857  1.26        ad 		 */
    858  1.26        ad 		mod->mod_autotime = time_second + module_autotime;
    859  1.26        ad 		module_thread_kick();
    860  1.26        ad 	}
    861   1.1        ad 	depth--;
    862  1.44   jnemeth 	if (path != NULL)
    863  1.44   jnemeth 		PNBUF_PUT(path);
    864   1.1        ad 	return 0;
    865   1.7    rumble 
    866   1.1        ad  fail:
    867  1.15        ad 	kobj_unload(mod->mod_kobj);
    868  1.15        ad  fail2:
    869   1.1        ad 	TAILQ_REMOVE(&pending, mod, mod_chain);
    870   1.1        ad 	kmem_free(mod, sizeof(*mod));
    871   1.1        ad 	depth--;
    872  1.44   jnemeth 	if (path != NULL)
    873  1.44   jnemeth 		PNBUF_PUT(path);
    874   1.1        ad 	return error;
    875   1.1        ad }
    876   1.1        ad 
    877   1.1        ad /*
    878   1.1        ad  * module_do_unload:
    879   1.1        ad  *
    880   1.1        ad  *	Helper routine: do the dirty work of unloading a module.
    881   1.1        ad  */
    882   1.1        ad static int
    883   1.1        ad module_do_unload(const char *name)
    884   1.1        ad {
    885   1.1        ad 	module_t *mod;
    886   1.1        ad 	int error;
    887   1.1        ad 	u_int i;
    888   1.1        ad 
    889   1.1        ad 	KASSERT(mutex_owned(&module_lock));
    890   1.1        ad 
    891   1.1        ad 	mod = module_lookup(name);
    892   1.1        ad 	if (mod == NULL) {
    893  1.32  christos 		module_error("module `%s' not found", name);
    894   1.1        ad 		return ENOENT;
    895   1.1        ad 	}
    896   1.1        ad 	if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
    897  1.34        ad 		module_print("module `%s' busy", name);
    898   1.1        ad 		return EBUSY;
    899   1.1        ad 	}
    900  1.12        ad 	KASSERT(module_active == NULL);
    901  1.12        ad 	module_active = mod;
    902   1.1        ad 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
    903  1.12        ad 	module_active = NULL;
    904   1.1        ad 	if (error != 0) {
    905  1.34        ad 		module_print("cannot unload module `%s' error=%d", name,
    906  1.33        ad 		    error);
    907   1.1        ad 		return error;
    908   1.1        ad 	}
    909  1.50      elad 	if (mod->mod_info->mi_class == MODULE_CLASS_SECMODEL)
    910  1.50      elad 		secmodel_deregister();
    911   1.1        ad 	module_count--;
    912   1.1        ad 	TAILQ_REMOVE(&module_list, mod, mod_chain);
    913   1.1        ad 	for (i = 0; i < mod->mod_nrequired; i++) {
    914   1.1        ad 		mod->mod_required[i]->mod_refcnt--;
    915   1.1        ad 	}
    916   1.1        ad 	if (mod->mod_kobj != NULL) {
    917   1.1        ad 		kobj_unload(mod->mod_kobj);
    918   1.1        ad 	}
    919   1.1        ad 	kmem_free(mod, sizeof(*mod));
    920  1.26        ad 	module_gen++;
    921   1.1        ad 
    922   1.1        ad 	return 0;
    923   1.1        ad }
    924   1.1        ad 
    925   1.1        ad /*
    926   1.1        ad  * module_prime:
    927   1.1        ad  *
    928   1.1        ad  *	Push a module loaded by the bootloader onto our internal
    929   1.1        ad  *	list.
    930   1.1        ad  */
    931   1.1        ad int
    932   1.1        ad module_prime(void *base, size_t size)
    933   1.1        ad {
    934   1.1        ad 	module_t *mod;
    935   1.1        ad 	int error;
    936   1.1        ad 
    937   1.1        ad 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
    938   1.1        ad 	if (mod == NULL) {
    939   1.1        ad 		return ENOMEM;
    940   1.1        ad 	}
    941   1.1        ad 	mod->mod_source = MODULE_SOURCE_BOOT;
    942   1.1        ad 
    943  1.15        ad 	error = kobj_load_mem(&mod->mod_kobj, base, size);
    944   1.1        ad 	if (error != 0) {
    945  1.11        ad 		kmem_free(mod, sizeof(*mod));
    946  1.11        ad 		module_error("unable to load object pushed by boot loader");
    947  1.11        ad 		return error;
    948  1.11        ad 	}
    949  1.11        ad 	error = module_fetch_info(mod);
    950  1.11        ad 	if (error != 0) {
    951  1.11        ad 		kobj_unload(mod->mod_kobj);
    952   1.1        ad 		kmem_free(mod, sizeof(*mod));
    953  1.11        ad 		module_error("unable to load object pushed by boot loader");
    954   1.1        ad 		return error;
    955   1.1        ad 	}
    956  1.11        ad 
    957   1.1        ad 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
    958   1.1        ad 
    959   1.1        ad 	return 0;
    960   1.1        ad }
    961  1.11        ad 
    962  1.11        ad /*
    963  1.11        ad  * module_fetch_into:
    964  1.11        ad  *
    965  1.11        ad  *	Fetch modinfo record from a loaded module.
    966  1.11        ad  */
    967  1.11        ad static int
    968  1.11        ad module_fetch_info(module_t *mod)
    969  1.11        ad {
    970  1.11        ad 	int error;
    971  1.11        ad 	void *addr;
    972  1.11        ad 	size_t size;
    973  1.11        ad 
    974  1.11        ad 	/*
    975  1.11        ad 	 * Find module info record and check compatibility.
    976  1.11        ad 	 */
    977  1.11        ad 	error = kobj_find_section(mod->mod_kobj, "link_set_modules",
    978  1.11        ad 	    &addr, &size);
    979  1.11        ad 	if (error != 0) {
    980  1.11        ad 		module_error("`link_set_modules' section not present");
    981  1.11        ad 		return error;
    982  1.11        ad 	}
    983  1.11        ad 	if (size != sizeof(modinfo_t **)) {
    984  1.11        ad 		module_error("`link_set_modules' section wrong size");
    985  1.11        ad 		return error;
    986  1.11        ad 	}
    987  1.11        ad 	mod->mod_info = *(modinfo_t **)addr;
    988  1.11        ad 
    989  1.11        ad 	return 0;
    990  1.11        ad }
    991  1.12        ad 
    992  1.12        ad /*
    993  1.12        ad  * module_find_section:
    994  1.12        ad  *
    995  1.12        ad  *	Allows a module that is being initialized to look up a section
    996  1.12        ad  *	within its ELF object.
    997  1.12        ad  */
    998  1.12        ad int
    999  1.12        ad module_find_section(const char *name, void **addr, size_t *size)
   1000  1.12        ad {
   1001  1.12        ad 
   1002  1.12        ad 	KASSERT(mutex_owned(&module_lock));
   1003  1.12        ad 	KASSERT(module_active != NULL);
   1004  1.12        ad 
   1005  1.12        ad 	return kobj_find_section(module_active->mod_kobj, name, addr, size);
   1006  1.12        ad }
   1007  1.26        ad 
   1008  1.26        ad /*
   1009  1.26        ad  * module_thread:
   1010  1.26        ad  *
   1011  1.26        ad  *	Automatically unload modules.  We try once to unload autoloaded
   1012  1.26        ad  *	modules after module_autotime seconds.  If the system is under
   1013  1.26        ad  *	severe memory pressure, we'll try unloading all modules.
   1014  1.26        ad  */
   1015  1.26        ad static void
   1016  1.26        ad module_thread(void *cookie)
   1017  1.26        ad {
   1018  1.26        ad 	module_t *mod, *next;
   1019  1.28        ad 	modinfo_t *mi;
   1020  1.28        ad 	int error;
   1021  1.26        ad 
   1022  1.26        ad 	for (;;) {
   1023  1.26        ad 		mutex_enter(&module_lock);
   1024  1.26        ad 		for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
   1025  1.26        ad 			next = TAILQ_NEXT(mod, mod_chain);
   1026  1.37        ad 			if (uvmexp.free < uvmexp.freemin) {
   1027  1.26        ad 				module_thread_ticks = hz;
   1028  1.26        ad 			} else if (mod->mod_autotime == 0) {
   1029  1.26        ad 				continue;
   1030  1.26        ad 			} else if (time_second < mod->mod_autotime) {
   1031  1.26        ad 				module_thread_ticks = hz;
   1032  1.26        ad 			    	continue;
   1033  1.37        ad 			} else {
   1034  1.26        ad 				mod->mod_autotime = 0;
   1035  1.37        ad 			}
   1036  1.28        ad 			/*
   1037  1.28        ad 			 * If this module wants to avoid autounload then
   1038  1.28        ad 			 * skip it.  Some modules can ping-pong in and out
   1039  1.28        ad 			 * because their use is transient but often.
   1040  1.28        ad 			 * Example: exec_script.
   1041  1.28        ad 			 */
   1042  1.28        ad 			mi = mod->mod_info;
   1043  1.28        ad 			error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
   1044  1.28        ad 			if (error == 0 || error == ENOTTY) {
   1045  1.37        ad 				(void)module_do_unload(mi->mi_name);
   1046  1.28        ad 			}
   1047  1.26        ad 		}
   1048  1.26        ad 		mutex_exit(&module_lock);
   1049  1.26        ad 
   1050  1.26        ad 		mutex_enter(&module_thread_lock);
   1051  1.26        ad 		(void)cv_timedwait(&module_thread_cv, &module_thread_lock,
   1052  1.26        ad 		    module_thread_ticks);
   1053  1.26        ad 		module_thread_ticks = 0;
   1054  1.26        ad 		mutex_exit(&module_thread_lock);
   1055  1.26        ad 	}
   1056  1.26        ad }
   1057  1.26        ad 
   1058  1.26        ad /*
   1059  1.26        ad  * module_thread:
   1060  1.26        ad  *
   1061  1.26        ad  *	Kick the module thread into action, perhaps because the
   1062  1.26        ad  *	system is low on memory.
   1063  1.26        ad  */
   1064  1.26        ad void
   1065  1.26        ad module_thread_kick(void)
   1066  1.26        ad {
   1067  1.26        ad 
   1068  1.26        ad 	mutex_enter(&module_thread_lock);
   1069  1.26        ad 	module_thread_ticks = hz;
   1070  1.26        ad 	cv_broadcast(&module_thread_cv);
   1071  1.26        ad 	mutex_exit(&module_thread_lock);
   1072  1.26        ad }
   1073  1.30        ad 
   1074  1.30        ad #ifdef DDB
   1075  1.30        ad /*
   1076  1.30        ad  * module_whatis:
   1077  1.30        ad  *
   1078  1.30        ad  *	Helper routine for DDB.
   1079  1.30        ad  */
   1080  1.30        ad void
   1081  1.30        ad module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1082  1.30        ad {
   1083  1.30        ad 	module_t *mod;
   1084  1.30        ad 	size_t msize;
   1085  1.30        ad 	vaddr_t maddr;
   1086  1.30        ad 
   1087  1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1088  1.43        ad 		if (mod->mod_kobj == NULL) {
   1089  1.43        ad 			continue;
   1090  1.43        ad 		}
   1091  1.49    dyoung 		if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1092  1.49    dyoung 			continue;
   1093  1.30        ad 		if (addr < maddr || addr >= maddr + msize) {
   1094  1.30        ad 			continue;
   1095  1.30        ad 		}
   1096  1.30        ad 		(*pr)("%p is %p+%zu, in kernel module `%s'\n",
   1097  1.30        ad 		    (void *)addr, (void *)maddr,
   1098  1.30        ad 		    (size_t)(addr - maddr), mod->mod_info->mi_name);
   1099  1.30        ad 	}
   1100  1.30        ad }
   1101  1.30        ad 
   1102  1.30        ad /*
   1103  1.30        ad  * module_print_list:
   1104  1.30        ad  *
   1105  1.30        ad  *	Helper routine for DDB.
   1106  1.30        ad  */
   1107  1.30        ad void
   1108  1.30        ad module_print_list(void (*pr)(const char *, ...))
   1109  1.30        ad {
   1110  1.30        ad 	const char *src;
   1111  1.30        ad 	module_t *mod;
   1112  1.30        ad 	size_t msize;
   1113  1.30        ad 	vaddr_t maddr;
   1114  1.30        ad 
   1115  1.30        ad 	(*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
   1116  1.30        ad 
   1117  1.30        ad 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
   1118  1.30        ad 		switch (mod->mod_source) {
   1119  1.30        ad 		case MODULE_SOURCE_KERNEL:
   1120  1.30        ad 			src = "builtin";
   1121  1.30        ad 			break;
   1122  1.30        ad 		case MODULE_SOURCE_FILESYS:
   1123  1.30        ad 			src = "filesys";
   1124  1.30        ad 			break;
   1125  1.30        ad 		case MODULE_SOURCE_BOOT:
   1126  1.30        ad 			src = "boot";
   1127  1.30        ad 			break;
   1128  1.30        ad 		default:
   1129  1.30        ad 			src = "unknown";
   1130  1.30        ad 			break;
   1131  1.30        ad 		}
   1132  1.49    dyoung 		if (mod->mod_kobj == NULL) {
   1133  1.43        ad 			maddr = 0;
   1134  1.43        ad 			msize = 0;
   1135  1.49    dyoung 		} else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
   1136  1.49    dyoung 			continue;
   1137  1.31        ad 		(*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
   1138  1.30        ad 		    (long)maddr, (long)msize, src);
   1139  1.30        ad 	}
   1140  1.30        ad }
   1141  1.30        ad #endif	/* DDB */
   1142  1.46   jnemeth 
   1143  1.46   jnemeth /*
   1144  1.46   jnemeth  * module_load_plist_file:
   1145  1.46   jnemeth  *
   1146  1.46   jnemeth  *	Load a plist located in the file system into memory.
   1147  1.46   jnemeth  */
   1148  1.46   jnemeth static int
   1149  1.46   jnemeth module_load_plist_file(const char *modpath, const bool nochroot,
   1150  1.46   jnemeth 		       void **basep, size_t *length)
   1151  1.46   jnemeth {
   1152  1.46   jnemeth 	struct nameidata nd;
   1153  1.46   jnemeth 	struct stat sb;
   1154  1.46   jnemeth 	void *base;
   1155  1.46   jnemeth 	char *proppath;
   1156  1.46   jnemeth 	size_t resid;
   1157  1.46   jnemeth 	int error, pathlen;
   1158  1.46   jnemeth 
   1159  1.46   jnemeth 	base = NULL;
   1160  1.46   jnemeth 	*length = 0;
   1161  1.46   jnemeth 
   1162  1.46   jnemeth 	proppath = PNBUF_GET();
   1163  1.46   jnemeth 	strcpy(proppath, modpath);
   1164  1.46   jnemeth 	pathlen = strlen(proppath);
   1165  1.46   jnemeth 	if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
   1166  1.46   jnemeth 		strcpy(&proppath[pathlen - 5], ".prop");
   1167  1.46   jnemeth 	} else if (pathlen < MAXPATHLEN - 5) {
   1168  1.46   jnemeth 			strcat(proppath, ".prop");
   1169  1.46   jnemeth 	} else {
   1170  1.46   jnemeth 		error = ENOENT;
   1171  1.46   jnemeth 		goto out1;
   1172  1.46   jnemeth 	}
   1173  1.46   jnemeth 
   1174  1.46   jnemeth 	NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0),
   1175  1.46   jnemeth 	    UIO_SYSSPACE, proppath);
   1176  1.46   jnemeth 
   1177  1.46   jnemeth 	error = namei(&nd);
   1178  1.46   jnemeth 	if (error != 0) {
   1179  1.46   jnemeth 		goto out1;
   1180  1.46   jnemeth 	}
   1181  1.46   jnemeth 
   1182  1.46   jnemeth 	error = vn_stat(nd.ni_vp, &sb);
   1183  1.46   jnemeth 	if (sb.st_size >= (PAGE_SIZE - 1)) {	/* leave space for term \0 */
   1184  1.46   jnemeth 		error = EINVAL;
   1185  1.46   jnemeth 	}
   1186  1.46   jnemeth 	if (error != 0) {
   1187  1.46   jnemeth 		goto out1;
   1188  1.46   jnemeth 	}
   1189  1.46   jnemeth 
   1190  1.46   jnemeth 	error = vn_open(&nd, FREAD, 0);
   1191  1.46   jnemeth  	if (error != 0) {
   1192  1.46   jnemeth 	 	goto out1;
   1193  1.46   jnemeth 	}
   1194  1.46   jnemeth 
   1195  1.46   jnemeth 	base = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   1196  1.46   jnemeth 	if (base == NULL) {
   1197  1.46   jnemeth 		error = ENOMEM;
   1198  1.46   jnemeth 		goto out;
   1199  1.46   jnemeth 	}
   1200  1.46   jnemeth 
   1201  1.46   jnemeth 	error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
   1202  1.46   jnemeth 	    UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
   1203  1.46   jnemeth 	*((uint8_t *)base + sb.st_size) = '\0';
   1204  1.46   jnemeth 	if (error == 0 && resid != 0) {
   1205  1.46   jnemeth 		error = EINVAL;
   1206  1.46   jnemeth 	}
   1207  1.46   jnemeth 	if (error != 0) {
   1208  1.46   jnemeth 		kmem_free(base, PAGE_SIZE);
   1209  1.46   jnemeth 		base = NULL;
   1210  1.46   jnemeth 	}
   1211  1.46   jnemeth 	*length = sb.st_size;
   1212  1.46   jnemeth 
   1213  1.46   jnemeth out:
   1214  1.46   jnemeth 	VOP_UNLOCK(nd.ni_vp, 0);
   1215  1.46   jnemeth 	vn_close(nd.ni_vp, FREAD, kauth_cred_get());
   1216  1.46   jnemeth 
   1217  1.46   jnemeth out1:
   1218  1.46   jnemeth 	PNBUF_PUT(proppath);
   1219  1.46   jnemeth 	*basep = base;
   1220  1.46   jnemeth 	return error;
   1221  1.46   jnemeth }
   1222  1.47   jnemeth 
   1223  1.47   jnemeth static bool
   1224  1.47   jnemeth module_merge_dicts(prop_dictionary_t existing_dict,
   1225  1.47   jnemeth 		   const prop_dictionary_t new_dict)
   1226  1.47   jnemeth {
   1227  1.47   jnemeth 	prop_dictionary_keysym_t props_keysym;
   1228  1.47   jnemeth 	prop_object_iterator_t props_iter;
   1229  1.47   jnemeth 	prop_object_t props_obj;
   1230  1.47   jnemeth 	const char *props_key;
   1231  1.47   jnemeth 	bool error;
   1232  1.47   jnemeth 
   1233  1.47   jnemeth 	error = false;
   1234  1.47   jnemeth 	props_iter = prop_dictionary_iterator(new_dict);
   1235  1.47   jnemeth 	if (props_iter == NULL) {
   1236  1.47   jnemeth 		return false;
   1237  1.47   jnemeth 	}
   1238  1.47   jnemeth 
   1239  1.47   jnemeth 	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
   1240  1.47   jnemeth 		props_keysym = (prop_dictionary_keysym_t)props_obj;
   1241  1.47   jnemeth 		props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
   1242  1.47   jnemeth 		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
   1243  1.47   jnemeth 		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
   1244  1.47   jnemeth 		    props_key, props_obj)) {
   1245  1.47   jnemeth 			error = true;
   1246  1.47   jnemeth 			goto out;
   1247  1.47   jnemeth 		}
   1248  1.47   jnemeth 	}
   1249  1.47   jnemeth 	error = false;
   1250  1.47   jnemeth 
   1251  1.47   jnemeth out:
   1252  1.47   jnemeth 	prop_object_iterator_release(props_iter);
   1253  1.47   jnemeth 
   1254  1.47   jnemeth 	return !error;
   1255  1.47   jnemeth }
   1256