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