[BACK]Return to kern_module.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/kern_module.c, Revision 1.82

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

CVSweb <webmaster@jp.NetBSD.org>