1 /* $NetBSD: kern_module.c,v 1.37 2008/12/05 12:55:09 ad 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.37 2008/12/05 12:55:09 ad Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_ddb.h" 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/fcntl.h> 47 #include <sys/proc.h> 48 #include <sys/kauth.h> 49 #include <sys/kobj.h> 50 #include <sys/kmem.h> 51 #include <sys/module.h> 52 #include <sys/kauth.h> 53 #include <sys/kthread.h> 54 #include <sys/sysctl.h> 55 56 #include <uvm/uvm_extern.h> 57 58 #include <machine/stdarg.h> 59 60 struct vm_map *module_map; 61 62 struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list); 63 struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist); 64 static module_t *module_active; 65 static char module_base[64]; 66 static int module_verbose_on; 67 static int module_autoload_on = 1; 68 u_int module_count; 69 kmutex_t module_lock; 70 u_int module_autotime = 10; 71 u_int module_gen = 1; 72 static kcondvar_t module_thread_cv; 73 static kmutex_t module_thread_lock; 74 static int module_thread_ticks; 75 76 /* Ensure that the kernel's link set isn't empty. */ 77 static modinfo_t module_dummy; 78 __link_set_add_rodata(modules, module_dummy); 79 80 static module_t *module_lookup(const char *); 81 static int module_do_load(const char *, bool, int, prop_dictionary_t, 82 module_t **, modclass_t class, bool); 83 static int module_do_unload(const char *); 84 static void module_error(const char *, ...); 85 static void module_print(const char *, ...); 86 static int module_do_builtin(const char *, module_t **); 87 static int module_fetch_info(module_t *); 88 static void module_thread(void *); 89 90 /* 91 * module_error: 92 * 93 * Utility function: log an error. 94 */ 95 static void 96 module_error(const char *fmt, ...) 97 { 98 va_list ap; 99 100 va_start(ap, fmt); 101 printf("WARNING: module error: "); 102 vprintf(fmt, ap); 103 printf("\n"); 104 va_end(ap); 105 } 106 107 /* 108 * module_print: 109 * 110 * Utility function: log verbose output. 111 */ 112 static void 113 module_print(const char *fmt, ...) 114 { 115 va_list ap; 116 117 if (module_verbose_on) { 118 va_start(ap, fmt); 119 printf("DEBUG: module: "); 120 vprintf(fmt, ap); 121 printf("\n"); 122 va_end(ap); 123 } 124 } 125 126 /* 127 * module_init: 128 * 129 * Initialize the module subsystem. 130 */ 131 void 132 module_init(void) 133 { 134 extern struct vm_map *module_map; 135 int error; 136 137 if (module_map == NULL) { 138 module_map = kernel_map; 139 } 140 mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE); 141 cv_init(&module_thread_cv, "modunload"); 142 mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE); 143 #ifdef MODULAR /* XXX */ 144 module_init_md(); 145 #endif 146 147 #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ 148 snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules", 149 machine, osrelease); 150 #else /* release */ 151 snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules", 152 machine, __NetBSD_Version__ / 100000000, 153 __NetBSD_Version__ / 1000000 % 100); 154 #endif 155 156 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread, 157 NULL, NULL, "modunload"); 158 if (error != 0) 159 panic("module_init: %d", error); 160 } 161 162 SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup") 163 { 164 const struct sysctlnode *node = NULL; 165 166 sysctl_createv(clog, 0, NULL, NULL, 167 CTLFLAG_PERMANENT, 168 CTLTYPE_NODE, "kern", NULL, 169 NULL, 0, NULL, 0, 170 CTL_KERN, CTL_EOL); 171 sysctl_createv(clog, 0, NULL, &node, 172 CTLFLAG_PERMANENT, 173 CTLTYPE_NODE, "module", 174 SYSCTL_DESCR("Module options"), 175 NULL, 0, NULL, 0, 176 CTL_KERN, CTL_CREATE, CTL_EOL); 177 178 if (node == NULL) 179 return; 180 181 sysctl_createv(clog, 0, &node, NULL, 182 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 183 CTLTYPE_INT, "autoload", 184 SYSCTL_DESCR("Enable automatic load of modules"), 185 NULL, 0, &module_autoload_on, 0, 186 CTL_CREATE, CTL_EOL); 187 sysctl_createv(clog, 0, &node, NULL, 188 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 189 CTLTYPE_INT, "verbose", 190 SYSCTL_DESCR("Enable verbose output"), 191 NULL, 0, &module_verbose_on, 0, 192 CTL_CREATE, CTL_EOL); 193 } 194 195 /* 196 * module_init_class: 197 * 198 * Initialize all built-in and pre-loaded modules of the 199 * specified class. 200 */ 201 void 202 module_init_class(modclass_t class) 203 { 204 __link_set_decl(modules, modinfo_t); 205 modinfo_t *const *mip, *mi; 206 module_t *mod; 207 208 mutex_enter(&module_lock); 209 /* 210 * Builtins first. These can't depend on pre-loaded modules. 211 */ 212 __link_set_foreach(mip, modules) { 213 mi = *mip; 214 if (mi == &module_dummy) { 215 continue; 216 } 217 if (class != MODULE_CLASS_ANY && class != mi->mi_class) { 218 continue; 219 } 220 (void)module_do_builtin(mi->mi_name, NULL); 221 } 222 /* 223 * Now preloaded modules. These will be pulled off the 224 * list as we call module_do_load(); 225 */ 226 do { 227 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { 228 mi = mod->mod_info; 229 if (class != MODULE_CLASS_ANY && 230 class != mi->mi_class) 231 continue; 232 module_do_load(mi->mi_name, false, 0, NULL, NULL, 233 class, true); 234 break; 235 } 236 } while (mod != NULL); 237 mutex_exit(&module_lock); 238 } 239 240 /* 241 * module_compatible: 242 * 243 * Return true if the two supplied kernel versions are said to 244 * have the same binary interface for kernel code. The entire 245 * version is signficant for the development tree (-current), 246 * major and minor versions are significant for official 247 * releases of the system. 248 */ 249 bool 250 module_compatible(int v1, int v2) 251 { 252 253 #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ 254 return v1 == v2; 255 #else /* release */ 256 return abs(v1 - v2) < 10000; 257 #endif 258 } 259 260 /* 261 * module_load: 262 * 263 * Load a single module from the file system. 264 */ 265 int 266 module_load(const char *filename, int flags, prop_dictionary_t props, 267 modclass_t class) 268 { 269 int error; 270 271 /* Authorize. */ 272 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, 273 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL); 274 if (error != 0) { 275 return error; 276 } 277 278 mutex_enter(&module_lock); 279 error = module_do_load(filename, false, flags, props, NULL, class, 280 false); 281 mutex_exit(&module_lock); 282 283 return error; 284 } 285 286 /* 287 * module_autoload: 288 * 289 * Load a single module from the file system, system initiated. 290 */ 291 int 292 module_autoload(const char *filename, modclass_t class) 293 { 294 int error; 295 296 KASSERT(mutex_owned(&module_lock)); 297 298 /* Nothing if the user has disabled it. */ 299 if (!module_autoload_on) { 300 return EPERM; 301 } 302 303 /* Disallow path seperators and magic symlinks. */ 304 if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL || 305 strchr(filename, '.') != NULL) { 306 return EPERM; 307 } 308 309 /* Authorize. */ 310 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, 311 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL); 312 if (error != 0) { 313 return error; 314 } 315 316 return module_do_load(filename, false, 0, NULL, NULL, class, true); 317 } 318 319 /* 320 * module_unload: 321 * 322 * Find and unload a module by name. 323 */ 324 int 325 module_unload(const char *name) 326 { 327 int error; 328 329 /* Authorize. */ 330 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, 331 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL); 332 if (error != 0) { 333 return error; 334 } 335 336 mutex_enter(&module_lock); 337 error = module_do_unload(name); 338 mutex_exit(&module_lock); 339 340 return error; 341 } 342 343 /* 344 * module_lookup: 345 * 346 * Look up a module by name. 347 */ 348 module_t * 349 module_lookup(const char *name) 350 { 351 module_t *mod; 352 353 KASSERT(mutex_owned(&module_lock)); 354 355 TAILQ_FOREACH(mod, &module_list, mod_chain) { 356 if (strcmp(mod->mod_info->mi_name, name) == 0) { 357 break; 358 } 359 } 360 361 return mod; 362 } 363 364 /* 365 * module_hold: 366 * 367 * Add a single reference to a module. It's the caller's 368 * responsibility to ensure that the reference is dropped 369 * later. 370 */ 371 int 372 module_hold(const char *name) 373 { 374 module_t *mod; 375 376 mutex_enter(&module_lock); 377 mod = module_lookup(name); 378 if (mod == NULL) { 379 mutex_exit(&module_lock); 380 return ENOENT; 381 } 382 mod->mod_refcnt++; 383 mutex_exit(&module_lock); 384 385 return 0; 386 } 387 388 /* 389 * module_rele: 390 * 391 * Release a reference acquired with module_hold(). 392 */ 393 void 394 module_rele(const char *name) 395 { 396 module_t *mod; 397 398 mutex_enter(&module_lock); 399 mod = module_lookup(name); 400 if (mod == NULL) { 401 mutex_exit(&module_lock); 402 panic("module_rele: gone"); 403 } 404 mod->mod_refcnt--; 405 mutex_exit(&module_lock); 406 } 407 408 /* 409 * module_enqueue: 410 * 411 * Put a module onto the global list and update counters. 412 */ 413 static void 414 module_enqueue(module_t *mod) 415 { 416 int i; 417 418 /* 419 * If there are requisite modules, put at the head of the queue. 420 * This is so that autounload can unload requisite modules with 421 * only one pass through the queue. 422 */ 423 if (mod->mod_nrequired) { 424 TAILQ_INSERT_HEAD(&module_list, mod, mod_chain); 425 426 /* Add references to the requisite modules. */ 427 for (i = 0; i < mod->mod_nrequired; i++) { 428 KASSERT(mod->mod_required[i] != NULL); 429 mod->mod_required[i]->mod_refcnt++; 430 } 431 } else { 432 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain); 433 } 434 module_count++; 435 module_gen++; 436 } 437 438 /* 439 * module_do_builtin: 440 * 441 * Initialize a single module from the list of modules that are 442 * built into the kernel (linked into the kernel image). 443 */ 444 static int 445 module_do_builtin(const char *name, module_t **modp) 446 { 447 __link_set_decl(modules, modinfo_t); 448 modinfo_t *const *mip; 449 const char *p, *s; 450 char buf[MAXMODNAME]; 451 modinfo_t *mi; 452 module_t *mod, *mod2; 453 size_t len; 454 int error; 455 456 KASSERT(mutex_owned(&module_lock)); 457 458 /* 459 * Check to see if already loaded. 460 */ 461 if ((mod = module_lookup(name)) != NULL) { 462 if (modp != NULL) { 463 *modp = mod; 464 } 465 return 0; 466 } 467 468 /* 469 * Search the list to see if we have a module by this name. 470 */ 471 error = ENOENT; 472 __link_set_foreach(mip, modules) { 473 mi = *mip; 474 if (mi == &module_dummy) { 475 continue; 476 } 477 if (strcmp(mi->mi_name, name) == 0) { 478 error = 0; 479 break; 480 } 481 } 482 if (error != 0) { 483 return error; 484 } 485 486 /* 487 * Initialize pre-requisites. 488 */ 489 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 490 if (mod == NULL) { 491 return ENOMEM; 492 } 493 if (modp != NULL) { 494 *modp = mod; 495 } 496 if (mi->mi_required != NULL) { 497 for (s = mi->mi_required; *s != '\0'; s = p) { 498 if (*s == ',') 499 s++; 500 p = s; 501 while (*p != '\0' && *p != ',') 502 p++; 503 len = min(p - s + 1, sizeof(buf)); 504 strlcpy(buf, s, len); 505 if (buf[0] == '\0') 506 break; 507 if (mod->mod_nrequired == MAXMODDEPS - 1) { 508 module_error("too many required modules"); 509 kmem_free(mod, sizeof(*mod)); 510 return EINVAL; 511 } 512 error = module_do_builtin(buf, &mod2); 513 if (error != 0) { 514 kmem_free(mod, sizeof(*mod)); 515 return error; 516 } 517 mod->mod_required[mod->mod_nrequired++] = mod2; 518 } 519 } 520 521 /* 522 * Try to initialize the module. 523 */ 524 KASSERT(module_active == NULL); 525 module_active = mod; 526 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL); 527 module_active = NULL; 528 if (error != 0) { 529 module_error("builtin module `%s' " 530 "failed to init", mi->mi_name); 531 kmem_free(mod, sizeof(*mod)); 532 return error; 533 } 534 mod->mod_info = mi; 535 mod->mod_source = MODULE_SOURCE_KERNEL; 536 module_enqueue(mod); 537 return 0; 538 } 539 540 /* 541 * module_do_load: 542 * 543 * Helper routine: load a module from the file system, or one 544 * pushed by the boot loader. 545 */ 546 static int 547 module_do_load(const char *name, bool isdep, int flags, 548 prop_dictionary_t props, module_t **modp, modclass_t class, 549 bool autoload) 550 { 551 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending); 552 static int depth; 553 const int maxdepth = 6; 554 modinfo_t *mi; 555 module_t *mod, *mod2; 556 char buf[MAXMODNAME]; 557 const char *s, *p; 558 int error; 559 size_t len; 560 561 KASSERT(mutex_owned(&module_lock)); 562 563 error = 0; 564 565 /* 566 * Avoid recursing too far. 567 */ 568 if (++depth > maxdepth) { 569 module_error("too many required modules"); 570 depth--; 571 return EMLINK; 572 } 573 574 /* 575 * Load the module and link. Before going to the file system, 576 * scan the list of modules loaded by the boot loader. Just 577 * before init is started the list of modules loaded at boot 578 * will be purged. Before init is started we can assume that 579 * `name' is a module name and not a path name. 580 */ 581 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { 582 if (strcmp(mod->mod_info->mi_name, name) == 0) { 583 TAILQ_REMOVE(&module_bootlist, mod, mod_chain); 584 break; 585 } 586 } 587 if (mod != NULL) { 588 TAILQ_INSERT_TAIL(&pending, mod, mod_chain); 589 } else { 590 /* 591 * If a requisite module, check to see if it is 592 * already present. 593 */ 594 if (isdep) { 595 TAILQ_FOREACH(mod, &module_list, mod_chain) { 596 if (strcmp(mod->mod_info->mi_name, name) == 0) { 597 break; 598 } 599 } 600 if (mod != NULL) { 601 if (modp != NULL) { 602 *modp = mod; 603 } 604 depth--; 605 return 0; 606 } 607 } 608 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 609 if (mod == NULL) { 610 depth--; 611 return ENOMEM; 612 } 613 error = kobj_load_file(&mod->mod_kobj, name, module_base, 614 autoload); 615 if (error != 0) { 616 kmem_free(mod, sizeof(*mod)); 617 depth--; 618 if (autoload) { 619 module_print("Cannot load kernel object `%s'" 620 " error=%d", name, error); 621 } else { 622 module_error("Cannot load kernel object `%s'" 623 " error=%d", name, error); 624 } 625 return error; 626 } 627 TAILQ_INSERT_TAIL(&pending, mod, mod_chain); 628 mod->mod_source = MODULE_SOURCE_FILESYS; 629 error = module_fetch_info(mod); 630 if (error != 0) { 631 module_error("cannot fetch module info for `%s'", 632 name); 633 goto fail; 634 } 635 } 636 637 /* 638 * Check compatibility. 639 */ 640 mi = mod->mod_info; 641 if (strlen(mi->mi_name) >= MAXMODNAME) { 642 error = EINVAL; 643 module_error("module name `%s' too long", mi->mi_name); 644 goto fail; 645 } 646 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) { 647 module_error("module built for `%d', system `%d'", 648 mi->mi_version, __NetBSD_Version__); 649 if ((flags & MODCTL_LOAD_FORCE) != 0) { 650 module_error("forced load, system may be unstable"); 651 } else { 652 error = EPROGMISMATCH; 653 goto fail; 654 } 655 } 656 657 /* 658 * If a specific kind of module was requested, ensure that we have 659 * a match. 660 */ 661 if (class != MODULE_CLASS_ANY && class != mi->mi_class) { 662 module_print("incompatible module class for `%s' (%d != %d)", 663 name, class, mi->mi_class); 664 error = ENOENT; 665 goto fail; 666 } 667 668 /* 669 * If loading a dependency, `name' is a plain module name. 670 * The name must match. 671 */ 672 if (isdep && strcmp(mi->mi_name, name) != 0) { 673 module_error("dependency name mismatch (`%s' != `%s')", 674 name, mi->mi_name); 675 error = ENOENT; 676 goto fail; 677 } 678 679 /* 680 * Check to see if the module is already loaded. If so, we may 681 * have been recursively called to handle a dependency, so be sure 682 * to set modp. 683 */ 684 if ((mod2 = module_lookup(mi->mi_name)) != NULL) { 685 if (modp != NULL) 686 *modp = mod2; 687 module_print("module `%s' already loaded", mi->mi_name); 688 error = EEXIST; 689 goto fail; 690 } 691 692 /* 693 * Block circular dependencies. 694 */ 695 TAILQ_FOREACH(mod2, &pending, mod_chain) { 696 if (mod == mod2) { 697 continue; 698 } 699 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) { 700 error = EDEADLK; 701 module_error("circular dependency detected for `%s'", 702 mi->mi_name); 703 goto fail; 704 } 705 } 706 707 /* 708 * Now try to load any requisite modules. 709 */ 710 if (mi->mi_required != NULL) { 711 for (s = mi->mi_required; *s != '\0'; s = p) { 712 if (*s == ',') 713 s++; 714 p = s; 715 while (*p != '\0' && *p != ',') 716 p++; 717 len = p - s + 1; 718 if (len >= MAXMODNAME) { 719 error = EINVAL; 720 module_error("required module name `%s'" 721 " too long", mi->mi_required); 722 goto fail; 723 } 724 strlcpy(buf, s, len); 725 if (buf[0] == '\0') 726 break; 727 if (mod->mod_nrequired == MAXMODDEPS - 1) { 728 error = EINVAL; 729 module_error("too many required modules (%d)", 730 mod->mod_nrequired); 731 goto fail; 732 } 733 if (strcmp(buf, mi->mi_name) == 0) { 734 error = EDEADLK; 735 module_error("self-dependency detected for " 736 "`%s'", mi->mi_name); 737 goto fail; 738 } 739 error = module_do_load(buf, true, flags, NULL, 740 &mod->mod_required[mod->mod_nrequired++], 741 MODULE_CLASS_ANY, true); 742 if (error != 0) 743 goto fail; 744 } 745 } 746 747 /* 748 * We loaded all needed modules successfully: perform global 749 * relocations and initialize. 750 */ 751 error = kobj_affix(mod->mod_kobj, mi->mi_name); 752 if (error != 0) { 753 /* Cannot touch 'mi' as the module is now gone. */ 754 module_error("unable to affix module `%s'", name); 755 goto fail2; 756 } 757 758 KASSERT(module_active == NULL); 759 module_active = mod; 760 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props); 761 module_active = NULL; 762 if (error != 0) { 763 module_error("modcmd function returned error %d for `%s'", 764 error, mi->mi_name); 765 goto fail; 766 } 767 768 /* 769 * Good, the module loaded successfully. Put it onto the 770 * list and add references to its requisite modules. 771 */ 772 TAILQ_REMOVE(&pending, mod, mod_chain); 773 module_enqueue(mod); 774 if (modp != NULL) { 775 *modp = mod; 776 } 777 if (autoload) { 778 /* 779 * Arrange to try unloading the module after 780 * a short delay. 781 */ 782 mod->mod_autotime = time_second + module_autotime; 783 module_thread_kick(); 784 } 785 depth--; 786 return 0; 787 788 fail: 789 kobj_unload(mod->mod_kobj); 790 fail2: 791 TAILQ_REMOVE(&pending, mod, mod_chain); 792 kmem_free(mod, sizeof(*mod)); 793 depth--; 794 return error; 795 } 796 797 /* 798 * module_do_unload: 799 * 800 * Helper routine: do the dirty work of unloading a module. 801 */ 802 static int 803 module_do_unload(const char *name) 804 { 805 module_t *mod; 806 int error; 807 u_int i; 808 809 KASSERT(mutex_owned(&module_lock)); 810 811 mod = module_lookup(name); 812 if (mod == NULL) { 813 module_error("module `%s' not found", name); 814 return ENOENT; 815 } 816 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) { 817 module_print("module `%s' busy", name); 818 return EBUSY; 819 } 820 KASSERT(module_active == NULL); 821 module_active = mod; 822 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL); 823 module_active = NULL; 824 if (error != 0) { 825 module_print("cannot unload module `%s' error=%d", name, 826 error); 827 return error; 828 } 829 module_count--; 830 TAILQ_REMOVE(&module_list, mod, mod_chain); 831 for (i = 0; i < mod->mod_nrequired; i++) { 832 mod->mod_required[i]->mod_refcnt--; 833 } 834 if (mod->mod_kobj != NULL) { 835 kobj_unload(mod->mod_kobj); 836 } 837 kmem_free(mod, sizeof(*mod)); 838 module_gen++; 839 840 return 0; 841 } 842 843 /* 844 * module_prime: 845 * 846 * Push a module loaded by the bootloader onto our internal 847 * list. 848 */ 849 int 850 module_prime(void *base, size_t size) 851 { 852 module_t *mod; 853 int error; 854 855 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 856 if (mod == NULL) { 857 return ENOMEM; 858 } 859 mod->mod_source = MODULE_SOURCE_BOOT; 860 861 error = kobj_load_mem(&mod->mod_kobj, base, size); 862 if (error != 0) { 863 kmem_free(mod, sizeof(*mod)); 864 module_error("unable to load object pushed by boot loader"); 865 return error; 866 } 867 error = module_fetch_info(mod); 868 if (error != 0) { 869 kobj_unload(mod->mod_kobj); 870 kmem_free(mod, sizeof(*mod)); 871 module_error("unable to load object pushed by boot loader"); 872 return error; 873 } 874 875 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain); 876 877 return 0; 878 } 879 880 /* 881 * module_fetch_into: 882 * 883 * Fetch modinfo record from a loaded module. 884 */ 885 static int 886 module_fetch_info(module_t *mod) 887 { 888 int error; 889 void *addr; 890 size_t size; 891 892 /* 893 * Find module info record and check compatibility. 894 */ 895 error = kobj_find_section(mod->mod_kobj, "link_set_modules", 896 &addr, &size); 897 if (error != 0) { 898 module_error("`link_set_modules' section not present"); 899 return error; 900 } 901 if (size != sizeof(modinfo_t **)) { 902 module_error("`link_set_modules' section wrong size"); 903 return error; 904 } 905 mod->mod_info = *(modinfo_t **)addr; 906 907 return 0; 908 } 909 910 /* 911 * module_find_section: 912 * 913 * Allows a module that is being initialized to look up a section 914 * within its ELF object. 915 */ 916 int 917 module_find_section(const char *name, void **addr, size_t *size) 918 { 919 920 KASSERT(mutex_owned(&module_lock)); 921 KASSERT(module_active != NULL); 922 923 return kobj_find_section(module_active->mod_kobj, name, addr, size); 924 } 925 926 /* 927 * module_thread: 928 * 929 * Automatically unload modules. We try once to unload autoloaded 930 * modules after module_autotime seconds. If the system is under 931 * severe memory pressure, we'll try unloading all modules. 932 */ 933 static void 934 module_thread(void *cookie) 935 { 936 module_t *mod, *next; 937 modinfo_t *mi; 938 int error; 939 940 for (;;) { 941 mutex_enter(&module_lock); 942 for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) { 943 next = TAILQ_NEXT(mod, mod_chain); 944 if (uvmexp.free < uvmexp.freemin) { 945 module_thread_ticks = hz; 946 } else if (mod->mod_autotime == 0) { 947 continue; 948 } else if (time_second < mod->mod_autotime) { 949 module_thread_ticks = hz; 950 continue; 951 } else { 952 mod->mod_autotime = 0; 953 } 954 /* 955 * If this module wants to avoid autounload then 956 * skip it. Some modules can ping-pong in and out 957 * because their use is transient but often. 958 * Example: exec_script. 959 */ 960 mi = mod->mod_info; 961 error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL); 962 if (error == 0 || error == ENOTTY) { 963 (void)module_do_unload(mi->mi_name); 964 } 965 } 966 mutex_exit(&module_lock); 967 968 mutex_enter(&module_thread_lock); 969 (void)cv_timedwait(&module_thread_cv, &module_thread_lock, 970 module_thread_ticks); 971 module_thread_ticks = 0; 972 mutex_exit(&module_thread_lock); 973 } 974 } 975 976 /* 977 * module_thread: 978 * 979 * Kick the module thread into action, perhaps because the 980 * system is low on memory. 981 */ 982 void 983 module_thread_kick(void) 984 { 985 986 mutex_enter(&module_thread_lock); 987 module_thread_ticks = hz; 988 cv_broadcast(&module_thread_cv); 989 mutex_exit(&module_thread_lock); 990 } 991 992 #ifdef DDB 993 /* 994 * module_whatis: 995 * 996 * Helper routine for DDB. 997 */ 998 void 999 module_whatis(uintptr_t addr, void (*pr)(const char *, ...)) 1000 { 1001 module_t *mod; 1002 size_t msize; 1003 vaddr_t maddr; 1004 1005 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1006 kobj_stat(mod->mod_kobj, &maddr, &msize); 1007 if (addr < maddr || addr >= maddr + msize) { 1008 continue; 1009 } 1010 (*pr)("%p is %p+%zu, in kernel module `%s'\n", 1011 (void *)addr, (void *)maddr, 1012 (size_t)(addr - maddr), mod->mod_info->mi_name); 1013 } 1014 } 1015 1016 /* 1017 * module_print_list: 1018 * 1019 * Helper routine for DDB. 1020 */ 1021 void 1022 module_print_list(void (*pr)(const char *, ...)) 1023 { 1024 const char *src; 1025 module_t *mod; 1026 size_t msize; 1027 vaddr_t maddr; 1028 1029 (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE"); 1030 1031 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1032 switch (mod->mod_source) { 1033 case MODULE_SOURCE_KERNEL: 1034 src = "builtin"; 1035 break; 1036 case MODULE_SOURCE_FILESYS: 1037 src = "filesys"; 1038 break; 1039 case MODULE_SOURCE_BOOT: 1040 src = "boot"; 1041 break; 1042 default: 1043 src = "unknown"; 1044 break; 1045 } 1046 kobj_stat(mod->mod_kobj, &maddr, &msize); 1047 (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name, 1048 (long)maddr, (long)msize, src); 1049 } 1050 } 1051 #endif /* DDB */ 1052