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