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