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