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