1 /* $NetBSD: kern_module.c,v 1.133 2019/01/27 02:08:43 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.133 2019/01/27 02:08:43 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 (strlen(mi->mi_name) >= MAXMODNAME) { 1094 error = EINVAL; 1095 module_error("module name `%s' longer than %d", mi->mi_name, 1096 MAXMODNAME); 1097 goto fail; 1098 } 1099 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) { 1100 module_error("module `%s' built for `%d', system `%d'", 1101 mi->mi_name, mi->mi_version, __NetBSD_Version__); 1102 if (ISSET(flags, MODCTL_LOAD_FORCE)) { 1103 module_error("forced load, system may be unstable"); 1104 } else { 1105 error = EPROGMISMATCH; 1106 goto fail; 1107 } 1108 } 1109 1110 /* 1111 * If a specific kind of module was requested, ensure that we have 1112 * a match. 1113 */ 1114 if (!MODULE_CLASS_MATCH(mi, modclass)) { 1115 module_incompat(mi, modclass); 1116 error = ENOENT; 1117 goto fail; 1118 } 1119 1120 /* 1121 * If loading a dependency, `name' is a plain module name. 1122 * The name must match. 1123 */ 1124 if (isdep && strcmp(mi->mi_name, name) != 0) { 1125 module_error("dependency name mismatch (`%s' != `%s')", 1126 name, mi->mi_name); 1127 error = ENOENT; 1128 goto fail; 1129 } 1130 1131 /* 1132 * If we loaded a module from the filesystem, check the actual 1133 * module name (from the modinfo_t) to ensure another module 1134 * with the same name doesn't already exist. (There's no 1135 * guarantee the filename will match the module name, and the 1136 * dup-symbols check may not be sufficient.) 1137 */ 1138 if (mod->mod_source == MODULE_SOURCE_FILESYS) { 1139 mod2 = module_lookup(mod->mod_info->mi_name); 1140 if ( mod2 && mod2 != mod) { 1141 module_error("module with name `%s' already loaded", 1142 mod2->mod_info->mi_name); 1143 error = EEXIST; 1144 if (modp != NULL) 1145 *modp = mod2; 1146 goto fail; 1147 } 1148 } 1149 1150 /* 1151 * Block circular dependencies. 1152 */ 1153 TAILQ_FOREACH(mod2, pending, mod_chain) { 1154 if (mod == mod2) { 1155 continue; 1156 } 1157 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) { 1158 error = EDEADLK; 1159 module_error("circular dependency detected for `%s'", 1160 mi->mi_name); 1161 goto fail; 1162 } 1163 } 1164 1165 /* 1166 * Now try to load any requisite modules. 1167 */ 1168 if (mi->mi_required != NULL) { 1169 mod->mod_arequired = 0; 1170 for (s = mi->mi_required; *s != '\0'; s = p) { 1171 if (*s == ',') 1172 s++; 1173 p = s; 1174 while (*p != '\0' && *p != ',') 1175 p++; 1176 len = p - s + 1; 1177 if (len >= MAXMODNAME) { 1178 error = EINVAL; 1179 module_error("required module name `%s' " 1180 "longer than %d", mi->mi_required, 1181 MAXMODNAME); 1182 goto fail; 1183 } 1184 strlcpy(buf, s, len); 1185 if (buf[0] == '\0') 1186 break; 1187 alloc_required(mod); 1188 if (strcmp(buf, mi->mi_name) == 0) { 1189 error = EDEADLK; 1190 module_error("self-dependency detected for " 1191 "`%s'", mi->mi_name); 1192 goto fail; 1193 } 1194 error = module_do_load(buf, true, flags, NULL, 1195 &mod2, MODULE_CLASS_ANY, true); 1196 if (error != 0 && error != EEXIST) { 1197 module_error("recursive load failed for `%s' " 1198 "(`%s' required), error %d", mi->mi_name, 1199 buf, error); 1200 goto fail; 1201 } 1202 (*mod->mod_required)[mod->mod_nrequired++] = mod2; 1203 } 1204 } 1205 1206 /* 1207 * We loaded all needed modules successfully: perform global 1208 * relocations and initialize. 1209 */ 1210 error = kobj_affix(mod->mod_kobj, mi->mi_name); 1211 if (error != 0) { 1212 /* Cannot touch 'mi' as the module is now gone. */ 1213 module_error("unable to affix module `%s', error %d", name, 1214 error); 1215 goto fail2; 1216 } 1217 1218 if (filedict) { 1219 if (!module_merge_dicts(filedict, props)) { 1220 module_error("module properties failed for %s", name); 1221 error = EINVAL; 1222 goto fail; 1223 } 1224 } 1225 1226 prev_active = module_active; 1227 module_active = mod; 1228 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props); 1229 module_active = prev_active; 1230 if (filedict) { 1231 prop_object_release(filedict); 1232 filedict = NULL; 1233 } 1234 if (error != 0) { 1235 module_error("modcmd function failed for `%s', error %d", 1236 mi->mi_name, error); 1237 goto fail; 1238 } 1239 1240 /* 1241 * If a recursive load already added a module with the same 1242 * name, abort. 1243 */ 1244 mod2 = module_lookup(mi->mi_name); 1245 if (mod2 && mod2 != mod) { 1246 module_error("recursive load causes duplicate module `%s'", 1247 mi->mi_name); 1248 error = EEXIST; 1249 goto fail1; 1250 } 1251 1252 /* 1253 * Good, the module loaded successfully. Put it onto the 1254 * list and add references to its requisite modules. 1255 */ 1256 TAILQ_REMOVE(pending, mod, mod_chain); 1257 module_enqueue(mod); 1258 if (modp != NULL) { 1259 *modp = mod; 1260 } 1261 if (autoload && module_autotime > 0) { 1262 /* 1263 * Arrange to try unloading the module after 1264 * a short delay unless auto-unload is disabled. 1265 */ 1266 mod->mod_autotime = time_second + module_autotime; 1267 SET(mod->mod_flags, MODFLG_AUTO_LOADED); 1268 module_thread_kick(); 1269 } 1270 SLIST_REMOVE_HEAD(&pend_stack, pe_entry); 1271 module_print("module `%s' loaded successfully", mi->mi_name); 1272 module_callback_load(mod); 1273 return 0; 1274 1275 fail1: 1276 (*mi->mi_modcmd)(MODULE_CMD_FINI, NULL); 1277 fail: 1278 kobj_unload(mod->mod_kobj); 1279 fail2: 1280 if (filedict != NULL) { 1281 prop_object_release(filedict); 1282 filedict = NULL; 1283 } 1284 TAILQ_REMOVE(pending, mod, mod_chain); 1285 SLIST_REMOVE_HEAD(&pend_stack, pe_entry); 1286 module_free(mod); 1287 return error; 1288 } 1289 1290 /* 1291 * module_do_unload: 1292 * 1293 * Helper routine: do the dirty work of unloading a module. 1294 */ 1295 static int 1296 module_do_unload(const char *name, bool load_requires_force) 1297 { 1298 module_t *mod, *prev_active; 1299 int error; 1300 u_int i; 1301 1302 KASSERT(kernconfig_is_held()); 1303 KASSERT(name != NULL); 1304 1305 module_print("unload requested for '%s' (%s)", name, 1306 load_requires_force ? "TRUE" : "FALSE"); 1307 mod = module_lookup(name); 1308 if (mod == NULL) { 1309 module_error("module `%s' not found", name); 1310 return ENOENT; 1311 } 1312 if (mod->mod_refcnt != 0) { 1313 module_print("module `%s' busy (%d refs)", name, 1314 mod->mod_refcnt); 1315 return EBUSY; 1316 } 1317 1318 /* 1319 * Builtin secmodels are there to stay. 1320 */ 1321 if (mod->mod_source == MODULE_SOURCE_KERNEL && 1322 mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) { 1323 module_print("cannot unload built-in secmodel module `%s'", 1324 name); 1325 return EPERM; 1326 } 1327 1328 prev_active = module_active; 1329 module_active = mod; 1330 module_callback_unload(mod); 1331 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL); 1332 module_active = prev_active; 1333 if (error != 0) { 1334 module_print("cannot unload module `%s' error=%d", name, 1335 error); 1336 return error; 1337 } 1338 module_count--; 1339 TAILQ_REMOVE(&module_list, mod, mod_chain); 1340 for (i = 0; i < mod->mod_nrequired; i++) { 1341 (*mod->mod_required)[i]->mod_refcnt--; 1342 } 1343 module_print("unloaded module `%s'", name); 1344 if (mod->mod_kobj != NULL) { 1345 kobj_unload(mod->mod_kobj); 1346 } 1347 if (mod->mod_source == MODULE_SOURCE_KERNEL) { 1348 if (mod->mod_required != NULL) { 1349 /* 1350 * release "required" resources - will be re-parsed 1351 * if the module is re-enabled 1352 */ 1353 kmem_free(mod->mod_required, 1354 mod->mod_arequired * sizeof(module_t *)); 1355 mod->mod_nrequired = 0; 1356 mod->mod_arequired = 0; 1357 mod->mod_required = NULL; 1358 } 1359 if (load_requires_force) 1360 module_require_force(mod); 1361 TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain); 1362 module_builtinlist++; 1363 } else { 1364 module_free(mod); 1365 } 1366 module_gen++; 1367 1368 return 0; 1369 } 1370 1371 /* 1372 * module_prime: 1373 * 1374 * Push a module loaded by the bootloader onto our internal 1375 * list. 1376 */ 1377 int 1378 module_prime(const char *name, void *base, size_t size) 1379 { 1380 __link_set_decl(modules, modinfo_t); 1381 modinfo_t *const *mip; 1382 module_t *mod; 1383 int error; 1384 1385 /* Check for module name same as a built-in module */ 1386 1387 __link_set_foreach(mip, modules) { 1388 if (*mip == &module_dummy) 1389 continue; 1390 if (strcmp((*mip)->mi_name, name) == 0) { 1391 module_error("module `%s' pushed by boot loader " 1392 "already exists", name); 1393 return EEXIST; 1394 } 1395 } 1396 1397 /* Also eliminate duplicate boolist entries */ 1398 1399 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { 1400 if (strcmp(mod->mod_info->mi_name, name) == 0) { 1401 module_error("duplicate bootlist entry for module " 1402 "`%s'", name); 1403 return EEXIST; 1404 } 1405 } 1406 1407 mod = module_newmodule(MODULE_SOURCE_BOOT); 1408 if (mod == NULL) { 1409 return ENOMEM; 1410 } 1411 1412 error = kobj_load_mem(&mod->mod_kobj, name, base, size); 1413 if (error != 0) { 1414 module_free(mod); 1415 module_error("unable to load `%s' pushed by boot loader, " 1416 "error %d", name, error); 1417 return error; 1418 } 1419 error = module_fetch_info(mod); 1420 if (error != 0) { 1421 kobj_unload(mod->mod_kobj); 1422 module_free(mod); 1423 module_error("unable to fetch_info for `%s' pushed by boot " 1424 "loader, error %d", name, error); 1425 return error; 1426 } 1427 1428 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain); 1429 1430 return 0; 1431 } 1432 1433 /* 1434 * module_fetch_into: 1435 * 1436 * Fetch modinfo record from a loaded module. 1437 */ 1438 static int 1439 module_fetch_info(module_t *mod) 1440 { 1441 int error; 1442 void *addr; 1443 size_t size; 1444 1445 /* 1446 * Find module info record and check compatibility. 1447 */ 1448 error = kobj_find_section(mod->mod_kobj, "link_set_modules", 1449 &addr, &size); 1450 if (error != 0) { 1451 module_error("`link_set_modules' section not present, " 1452 "error %d", error); 1453 return error; 1454 } 1455 if (size != sizeof(modinfo_t **)) { 1456 module_error("`link_set_modules' section wrong size " 1457 "(got %zu, wanted %zu)", size, sizeof(modinfo_t **)); 1458 return ENOEXEC; 1459 } 1460 mod->mod_info = *(modinfo_t **)addr; 1461 1462 return 0; 1463 } 1464 1465 /* 1466 * module_find_section: 1467 * 1468 * Allows a module that is being initialized to look up a section 1469 * within its ELF object. 1470 */ 1471 int 1472 module_find_section(const char *name, void **addr, size_t *size) 1473 { 1474 1475 KASSERT(kernconfig_is_held()); 1476 KASSERT(module_active != NULL); 1477 1478 return kobj_find_section(module_active->mod_kobj, name, addr, size); 1479 } 1480 1481 /* 1482 * module_thread: 1483 * 1484 * Automatically unload modules. We try once to unload autoloaded 1485 * modules after module_autotime seconds. If the system is under 1486 * severe memory pressure, we'll try unloading all modules, else if 1487 * module_autotime is zero, we don't try to unload, even if the 1488 * module was previously scheduled for unload. 1489 */ 1490 static void 1491 module_thread(void *cookie) 1492 { 1493 module_t *mod, *next; 1494 modinfo_t *mi; 1495 int error; 1496 1497 for (;;) { 1498 kernconfig_lock(); 1499 for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) { 1500 next = TAILQ_NEXT(mod, mod_chain); 1501 1502 /* skip built-in modules */ 1503 if (mod->mod_source == MODULE_SOURCE_KERNEL) 1504 continue; 1505 /* skip modules that weren't auto-loaded */ 1506 if (!ISSET(mod->mod_flags, MODFLG_AUTO_LOADED)) 1507 continue; 1508 1509 if (uvmexp.free < uvmexp.freemin) { 1510 module_thread_ticks = hz; 1511 } else if (module_autotime == 0 || 1512 mod->mod_autotime == 0) { 1513 continue; 1514 } else if (time_second < mod->mod_autotime) { 1515 module_thread_ticks = hz; 1516 continue; 1517 } else { 1518 mod->mod_autotime = 0; 1519 } 1520 1521 /* 1522 * If this module wants to avoid autounload then 1523 * skip it. Some modules can ping-pong in and out 1524 * because their use is transient but often. 1525 * Example: exec_script. 1526 */ 1527 mi = mod->mod_info; 1528 error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL); 1529 if (error == 0 || error == ENOTTY) { 1530 (void)module_do_unload(mi->mi_name, false); 1531 } else 1532 module_print("module `%s' declined to be " 1533 "auto-unloaded error=%d", mi->mi_name, 1534 error); 1535 } 1536 kernconfig_unlock(); 1537 1538 mutex_enter(&module_thread_lock); 1539 (void)cv_timedwait(&module_thread_cv, &module_thread_lock, 1540 module_thread_ticks); 1541 module_thread_ticks = 0; 1542 mutex_exit(&module_thread_lock); 1543 } 1544 } 1545 1546 /* 1547 * module_thread: 1548 * 1549 * Kick the module thread into action, perhaps because the 1550 * system is low on memory. 1551 */ 1552 void 1553 module_thread_kick(void) 1554 { 1555 1556 mutex_enter(&module_thread_lock); 1557 module_thread_ticks = hz; 1558 cv_broadcast(&module_thread_cv); 1559 mutex_exit(&module_thread_lock); 1560 } 1561 1562 #ifdef DDB 1563 /* 1564 * module_whatis: 1565 * 1566 * Helper routine for DDB. 1567 */ 1568 void 1569 module_whatis(uintptr_t addr, void (*pr)(const char *, ...)) 1570 { 1571 module_t *mod; 1572 size_t msize; 1573 vaddr_t maddr; 1574 1575 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1576 if (mod->mod_kobj == NULL) { 1577 continue; 1578 } 1579 if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0) 1580 continue; 1581 if (addr < maddr || addr >= maddr + msize) { 1582 continue; 1583 } 1584 (*pr)("%p is %p+%zu, in kernel module `%s'\n", 1585 (void *)addr, (void *)maddr, 1586 (size_t)(addr - maddr), mod->mod_info->mi_name); 1587 } 1588 } 1589 1590 /* 1591 * module_print_list: 1592 * 1593 * Helper routine for DDB. 1594 */ 1595 void 1596 module_print_list(void (*pr)(const char *, ...)) 1597 { 1598 const char *src; 1599 module_t *mod; 1600 size_t msize; 1601 vaddr_t maddr; 1602 1603 (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE"); 1604 1605 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1606 switch (mod->mod_source) { 1607 case MODULE_SOURCE_KERNEL: 1608 src = "builtin"; 1609 break; 1610 case MODULE_SOURCE_FILESYS: 1611 src = "filesys"; 1612 break; 1613 case MODULE_SOURCE_BOOT: 1614 src = "boot"; 1615 break; 1616 default: 1617 src = "unknown"; 1618 break; 1619 } 1620 if (mod->mod_kobj == NULL) { 1621 maddr = 0; 1622 msize = 0; 1623 } else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0) 1624 continue; 1625 (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name, 1626 (long)maddr, (long)msize, src); 1627 } 1628 } 1629 #endif /* DDB */ 1630 1631 static bool 1632 module_merge_dicts(prop_dictionary_t existing_dict, 1633 const prop_dictionary_t new_dict) 1634 { 1635 prop_dictionary_keysym_t props_keysym; 1636 prop_object_iterator_t props_iter; 1637 prop_object_t props_obj; 1638 const char *props_key; 1639 bool error; 1640 1641 if (new_dict == NULL) { /* nothing to merge */ 1642 return true; 1643 } 1644 1645 error = false; 1646 props_iter = prop_dictionary_iterator(new_dict); 1647 if (props_iter == NULL) { 1648 return false; 1649 } 1650 1651 while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) { 1652 props_keysym = (prop_dictionary_keysym_t)props_obj; 1653 props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym); 1654 props_obj = prop_dictionary_get_keysym(new_dict, props_keysym); 1655 if ((props_obj == NULL) || !prop_dictionary_set(existing_dict, 1656 props_key, props_obj)) { 1657 error = true; 1658 goto out; 1659 } 1660 } 1661 error = false; 1662 1663 out: 1664 prop_object_iterator_release(props_iter); 1665 1666 return !error; 1667 } 1668 1669 /* 1670 * module_specific_key_create: 1671 * 1672 * Create a key for subsystem module-specific data. 1673 */ 1674 specificdata_key_t 1675 module_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 1676 { 1677 1678 return specificdata_key_create(module_specificdata_domain, keyp, dtor); 1679 } 1680 1681 /* 1682 * module_specific_key_delete: 1683 * 1684 * Delete a key for subsystem module-specific data. 1685 */ 1686 void 1687 module_specific_key_delete(specificdata_key_t key) 1688 { 1689 1690 return specificdata_key_delete(module_specificdata_domain, key); 1691 } 1692 1693 /* 1694 * module_getspecific: 1695 * 1696 * Return module-specific data corresponding to the specified key. 1697 */ 1698 void * 1699 module_getspecific(module_t *mod, specificdata_key_t key) 1700 { 1701 1702 return specificdata_getspecific(module_specificdata_domain, 1703 &mod->mod_sdref, key); 1704 } 1705 1706 /* 1707 * module_setspecific: 1708 * 1709 * Set module-specific data corresponding to the specified key. 1710 */ 1711 void 1712 module_setspecific(module_t *mod, specificdata_key_t key, void *data) 1713 { 1714 1715 specificdata_setspecific(module_specificdata_domain, 1716 &mod->mod_sdref, key, data); 1717 } 1718 1719 /* 1720 * module_register_callbacks: 1721 * 1722 * Register a new set of callbacks to be called on module load/unload. 1723 * Call the load callback on each existing module. 1724 * Return an opaque handle for unregistering these later. 1725 */ 1726 void * 1727 module_register_callbacks(void (*load)(struct module *), 1728 void (*unload)(struct module *)) 1729 { 1730 struct module_callbacks *modcb; 1731 struct module *mod; 1732 1733 modcb = kmem_alloc(sizeof(*modcb), KM_SLEEP); 1734 modcb->modcb_load = load; 1735 modcb->modcb_unload = unload; 1736 1737 kernconfig_lock(); 1738 TAILQ_INSERT_TAIL(&modcblist, modcb, modcb_list); 1739 TAILQ_FOREACH(mod, &module_list, mod_chain) 1740 load(mod); 1741 kernconfig_unlock(); 1742 1743 return modcb; 1744 } 1745 1746 /* 1747 * module_unregister_callbacks: 1748 * 1749 * Unregister a previously-registered set of module load/unload callbacks. 1750 * Call the unload callback on each existing module. 1751 */ 1752 void 1753 module_unregister_callbacks(void *opaque) 1754 { 1755 struct module_callbacks *modcb; 1756 struct module *mod; 1757 1758 modcb = opaque; 1759 kernconfig_lock(); 1760 TAILQ_FOREACH(mod, &module_list, mod_chain) 1761 modcb->modcb_unload(mod); 1762 TAILQ_REMOVE(&modcblist, modcb, modcb_list); 1763 kernconfig_unlock(); 1764 kmem_free(modcb, sizeof(*modcb)); 1765 } 1766 1767 /* 1768 * module_callback_load: 1769 * 1770 * Helper routine: call all load callbacks on a module being loaded. 1771 */ 1772 static void 1773 module_callback_load(struct module *mod) 1774 { 1775 struct module_callbacks *modcb; 1776 1777 TAILQ_FOREACH(modcb, &modcblist, modcb_list) { 1778 modcb->modcb_load(mod); 1779 } 1780 } 1781 1782 /* 1783 * module_callback_unload: 1784 * 1785 * Helper routine: call all unload callbacks on a module being unloaded. 1786 */ 1787 static void 1788 module_callback_unload(struct module *mod) 1789 { 1790 struct module_callbacks *modcb; 1791 1792 TAILQ_FOREACH(modcb, &modcblist, modcb_list) { 1793 modcb->modcb_unload(mod); 1794 } 1795 } 1796