1 /* $NetBSD: kern_module.c,v 1.38 2008/12/28 03:21:02 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software developed for The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Kernel module support. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.38 2008/12/28 03:21:02 christos 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, false); 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 module_error("can't find `%s'", name); 484 return error; 485 } 486 487 /* 488 * Initialize pre-requisites. 489 */ 490 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 491 if (mod == NULL) { 492 module_error("out of memory for `%s'", name); 493 return ENOMEM; 494 } 495 if (modp != NULL) { 496 *modp = mod; 497 } 498 if (mi->mi_required != NULL) { 499 for (s = mi->mi_required; *s != '\0'; s = p) { 500 if (*s == ',') 501 s++; 502 p = s; 503 while (*p != '\0' && *p != ',') 504 p++; 505 len = min(p - s + 1, sizeof(buf)); 506 strlcpy(buf, s, len); 507 if (buf[0] == '\0') 508 break; 509 if (mod->mod_nrequired == MAXMODDEPS - 1) { 510 module_error("too many required modules"); 511 kmem_free(mod, sizeof(*mod)); 512 return EINVAL; 513 } 514 error = module_do_builtin(buf, &mod2); 515 if (error != 0) { 516 kmem_free(mod, sizeof(*mod)); 517 return error; 518 } 519 mod->mod_required[mod->mod_nrequired++] = mod2; 520 } 521 } 522 523 /* 524 * Try to initialize the module. 525 */ 526 KASSERT(module_active == NULL); 527 module_active = mod; 528 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL); 529 module_active = NULL; 530 if (error != 0) { 531 module_error("builtin module `%s' " 532 "failed to init", mi->mi_name); 533 kmem_free(mod, sizeof(*mod)); 534 return error; 535 } 536 mod->mod_info = mi; 537 mod->mod_source = MODULE_SOURCE_KERNEL; 538 module_enqueue(mod); 539 return 0; 540 } 541 542 /* 543 * module_do_load: 544 * 545 * Helper routine: load a module from the file system, or one 546 * pushed by the boot loader. 547 */ 548 static int 549 module_do_load(const char *name, bool isdep, int flags, 550 prop_dictionary_t props, module_t **modp, modclass_t class, 551 bool autoload) 552 { 553 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending); 554 static int depth; 555 const int maxdepth = 6; 556 modinfo_t *mi; 557 module_t *mod, *mod2; 558 char buf[MAXMODNAME]; 559 const char *s, *p; 560 int error; 561 size_t len; 562 563 KASSERT(mutex_owned(&module_lock)); 564 565 error = 0; 566 567 /* 568 * Avoid recursing too far. 569 */ 570 if (++depth > maxdepth) { 571 module_error("too many required modules"); 572 depth--; 573 return EMLINK; 574 } 575 576 /* 577 * Load the module and link. Before going to the file system, 578 * scan the list of modules loaded by the boot loader. Just 579 * before init is started the list of modules loaded at boot 580 * will be purged. Before init is started we can assume that 581 * `name' is a module name and not a path name. 582 */ 583 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { 584 if (strcmp(mod->mod_info->mi_name, name) == 0) { 585 TAILQ_REMOVE(&module_bootlist, mod, mod_chain); 586 break; 587 } 588 } 589 if (mod != NULL) { 590 TAILQ_INSERT_TAIL(&pending, mod, mod_chain); 591 } else { 592 /* 593 * If a requisite module, check to see if it is 594 * already present. 595 */ 596 if (isdep) { 597 TAILQ_FOREACH(mod, &module_list, mod_chain) { 598 if (strcmp(mod->mod_info->mi_name, name) == 0) { 599 break; 600 } 601 } 602 if (mod != NULL) { 603 if (modp != NULL) { 604 *modp = mod; 605 } 606 depth--; 607 return 0; 608 } 609 } 610 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 611 if (mod == NULL) { 612 module_error("out of memory for `%s'", name); 613 depth--; 614 return ENOMEM; 615 } 616 error = kobj_load_file(&mod->mod_kobj, name, module_base, 617 autoload); 618 if (error != 0) { 619 kmem_free(mod, sizeof(*mod)); 620 depth--; 621 if (autoload) { 622 module_print("Cannot load kernel object `%s'" 623 " error=%d", name, error); 624 } else { 625 module_error("Cannot load kernel object `%s'" 626 " error=%d", name, error); 627 } 628 return error; 629 } 630 TAILQ_INSERT_TAIL(&pending, mod, mod_chain); 631 mod->mod_source = MODULE_SOURCE_FILESYS; 632 error = module_fetch_info(mod); 633 if (error != 0) { 634 module_error("cannot fetch module info for `%s'", 635 name); 636 goto fail; 637 } 638 } 639 640 /* 641 * Check compatibility. 642 */ 643 mi = mod->mod_info; 644 if (strlen(mi->mi_name) >= MAXMODNAME) { 645 error = EINVAL; 646 module_error("module name `%s' too long", mi->mi_name); 647 goto fail; 648 } 649 if (!module_compatible(mi->mi_version, __NetBSD_Version__)) { 650 module_error("module built for `%d', system `%d'", 651 mi->mi_version, __NetBSD_Version__); 652 if ((flags & MODCTL_LOAD_FORCE) != 0) { 653 module_error("forced load, system may be unstable"); 654 } else { 655 error = EPROGMISMATCH; 656 goto fail; 657 } 658 } 659 660 /* 661 * If a specific kind of module was requested, ensure that we have 662 * a match. 663 */ 664 if (class != MODULE_CLASS_ANY && class != mi->mi_class) { 665 module_print("incompatible module class for `%s' (%d != %d)", 666 name, class, mi->mi_class); 667 error = ENOENT; 668 goto fail; 669 } 670 671 /* 672 * If loading a dependency, `name' is a plain module name. 673 * The name must match. 674 */ 675 if (isdep && strcmp(mi->mi_name, name) != 0) { 676 module_error("dependency name mismatch (`%s' != `%s')", 677 name, mi->mi_name); 678 error = ENOENT; 679 goto fail; 680 } 681 682 /* 683 * Check to see if the module is already loaded. If so, we may 684 * have been recursively called to handle a dependency, so be sure 685 * to set modp. 686 */ 687 if ((mod2 = module_lookup(mi->mi_name)) != NULL) { 688 if (modp != NULL) 689 *modp = mod2; 690 module_print("module `%s' already loaded", mi->mi_name); 691 error = EEXIST; 692 goto fail; 693 } 694 695 /* 696 * Block circular dependencies. 697 */ 698 TAILQ_FOREACH(mod2, &pending, mod_chain) { 699 if (mod == mod2) { 700 continue; 701 } 702 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) { 703 error = EDEADLK; 704 module_error("circular dependency detected for `%s'", 705 mi->mi_name); 706 goto fail; 707 } 708 } 709 710 /* 711 * Now try to load any requisite modules. 712 */ 713 if (mi->mi_required != NULL) { 714 for (s = mi->mi_required; *s != '\0'; s = p) { 715 if (*s == ',') 716 s++; 717 p = s; 718 while (*p != '\0' && *p != ',') 719 p++; 720 len = p - s + 1; 721 if (len >= MAXMODNAME) { 722 error = EINVAL; 723 module_error("required module name `%s'" 724 " too long", mi->mi_required); 725 goto fail; 726 } 727 strlcpy(buf, s, len); 728 if (buf[0] == '\0') 729 break; 730 if (mod->mod_nrequired == MAXMODDEPS - 1) { 731 error = EINVAL; 732 module_error("too many required modules (%d)", 733 mod->mod_nrequired); 734 goto fail; 735 } 736 if (strcmp(buf, mi->mi_name) == 0) { 737 error = EDEADLK; 738 module_error("self-dependency detected for " 739 "`%s'", mi->mi_name); 740 goto fail; 741 } 742 error = module_do_load(buf, true, flags, NULL, 743 &mod->mod_required[mod->mod_nrequired++], 744 MODULE_CLASS_ANY, true); 745 if (error != 0) 746 goto fail; 747 } 748 } 749 750 /* 751 * We loaded all needed modules successfully: perform global 752 * relocations and initialize. 753 */ 754 error = kobj_affix(mod->mod_kobj, mi->mi_name); 755 if (error != 0) { 756 /* Cannot touch 'mi' as the module is now gone. */ 757 module_error("unable to affix module `%s'", name); 758 goto fail2; 759 } 760 761 KASSERT(module_active == NULL); 762 module_active = mod; 763 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props); 764 module_active = NULL; 765 if (error != 0) { 766 module_error("modcmd function returned error %d for `%s'", 767 error, mi->mi_name); 768 goto fail; 769 } 770 771 /* 772 * Good, the module loaded successfully. Put it onto the 773 * list and add references to its requisite modules. 774 */ 775 TAILQ_REMOVE(&pending, mod, mod_chain); 776 module_enqueue(mod); 777 if (modp != NULL) { 778 *modp = mod; 779 } 780 if (autoload) { 781 /* 782 * Arrange to try unloading the module after 783 * a short delay. 784 */ 785 mod->mod_autotime = time_second + module_autotime; 786 module_thread_kick(); 787 } 788 depth--; 789 return 0; 790 791 fail: 792 kobj_unload(mod->mod_kobj); 793 fail2: 794 TAILQ_REMOVE(&pending, mod, mod_chain); 795 kmem_free(mod, sizeof(*mod)); 796 depth--; 797 return error; 798 } 799 800 /* 801 * module_do_unload: 802 * 803 * Helper routine: do the dirty work of unloading a module. 804 */ 805 static int 806 module_do_unload(const char *name) 807 { 808 module_t *mod; 809 int error; 810 u_int i; 811 812 KASSERT(mutex_owned(&module_lock)); 813 814 mod = module_lookup(name); 815 if (mod == NULL) { 816 module_error("module `%s' not found", name); 817 return ENOENT; 818 } 819 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) { 820 module_print("module `%s' busy", name); 821 return EBUSY; 822 } 823 KASSERT(module_active == NULL); 824 module_active = mod; 825 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL); 826 module_active = NULL; 827 if (error != 0) { 828 module_print("cannot unload module `%s' error=%d", name, 829 error); 830 return error; 831 } 832 module_count--; 833 TAILQ_REMOVE(&module_list, mod, mod_chain); 834 for (i = 0; i < mod->mod_nrequired; i++) { 835 mod->mod_required[i]->mod_refcnt--; 836 } 837 if (mod->mod_kobj != NULL) { 838 kobj_unload(mod->mod_kobj); 839 } 840 kmem_free(mod, sizeof(*mod)); 841 module_gen++; 842 843 return 0; 844 } 845 846 /* 847 * module_prime: 848 * 849 * Push a module loaded by the bootloader onto our internal 850 * list. 851 */ 852 int 853 module_prime(void *base, size_t size) 854 { 855 module_t *mod; 856 int error; 857 858 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); 859 if (mod == NULL) { 860 return ENOMEM; 861 } 862 mod->mod_source = MODULE_SOURCE_BOOT; 863 864 error = kobj_load_mem(&mod->mod_kobj, base, size); 865 if (error != 0) { 866 kmem_free(mod, sizeof(*mod)); 867 module_error("unable to load object pushed by boot loader"); 868 return error; 869 } 870 error = module_fetch_info(mod); 871 if (error != 0) { 872 kobj_unload(mod->mod_kobj); 873 kmem_free(mod, sizeof(*mod)); 874 module_error("unable to load object pushed by boot loader"); 875 return error; 876 } 877 878 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain); 879 880 return 0; 881 } 882 883 /* 884 * module_fetch_into: 885 * 886 * Fetch modinfo record from a loaded module. 887 */ 888 static int 889 module_fetch_info(module_t *mod) 890 { 891 int error; 892 void *addr; 893 size_t size; 894 895 /* 896 * Find module info record and check compatibility. 897 */ 898 error = kobj_find_section(mod->mod_kobj, "link_set_modules", 899 &addr, &size); 900 if (error != 0) { 901 module_error("`link_set_modules' section not present"); 902 return error; 903 } 904 if (size != sizeof(modinfo_t **)) { 905 module_error("`link_set_modules' section wrong size"); 906 return error; 907 } 908 mod->mod_info = *(modinfo_t **)addr; 909 910 return 0; 911 } 912 913 /* 914 * module_find_section: 915 * 916 * Allows a module that is being initialized to look up a section 917 * within its ELF object. 918 */ 919 int 920 module_find_section(const char *name, void **addr, size_t *size) 921 { 922 923 KASSERT(mutex_owned(&module_lock)); 924 KASSERT(module_active != NULL); 925 926 return kobj_find_section(module_active->mod_kobj, name, addr, size); 927 } 928 929 /* 930 * module_thread: 931 * 932 * Automatically unload modules. We try once to unload autoloaded 933 * modules after module_autotime seconds. If the system is under 934 * severe memory pressure, we'll try unloading all modules. 935 */ 936 static void 937 module_thread(void *cookie) 938 { 939 module_t *mod, *next; 940 modinfo_t *mi; 941 int error; 942 943 for (;;) { 944 mutex_enter(&module_lock); 945 for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) { 946 next = TAILQ_NEXT(mod, mod_chain); 947 if (uvmexp.free < uvmexp.freemin) { 948 module_thread_ticks = hz; 949 } else if (mod->mod_autotime == 0) { 950 continue; 951 } else if (time_second < mod->mod_autotime) { 952 module_thread_ticks = hz; 953 continue; 954 } else { 955 mod->mod_autotime = 0; 956 } 957 /* 958 * If this module wants to avoid autounload then 959 * skip it. Some modules can ping-pong in and out 960 * because their use is transient but often. 961 * Example: exec_script. 962 */ 963 mi = mod->mod_info; 964 error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL); 965 if (error == 0 || error == ENOTTY) { 966 (void)module_do_unload(mi->mi_name); 967 } 968 } 969 mutex_exit(&module_lock); 970 971 mutex_enter(&module_thread_lock); 972 (void)cv_timedwait(&module_thread_cv, &module_thread_lock, 973 module_thread_ticks); 974 module_thread_ticks = 0; 975 mutex_exit(&module_thread_lock); 976 } 977 } 978 979 /* 980 * module_thread: 981 * 982 * Kick the module thread into action, perhaps because the 983 * system is low on memory. 984 */ 985 void 986 module_thread_kick(void) 987 { 988 989 mutex_enter(&module_thread_lock); 990 module_thread_ticks = hz; 991 cv_broadcast(&module_thread_cv); 992 mutex_exit(&module_thread_lock); 993 } 994 995 #ifdef DDB 996 /* 997 * module_whatis: 998 * 999 * Helper routine for DDB. 1000 */ 1001 void 1002 module_whatis(uintptr_t addr, void (*pr)(const char *, ...)) 1003 { 1004 module_t *mod; 1005 size_t msize; 1006 vaddr_t maddr; 1007 1008 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1009 kobj_stat(mod->mod_kobj, &maddr, &msize); 1010 if (addr < maddr || addr >= maddr + msize) { 1011 continue; 1012 } 1013 (*pr)("%p is %p+%zu, in kernel module `%s'\n", 1014 (void *)addr, (void *)maddr, 1015 (size_t)(addr - maddr), mod->mod_info->mi_name); 1016 } 1017 } 1018 1019 /* 1020 * module_print_list: 1021 * 1022 * Helper routine for DDB. 1023 */ 1024 void 1025 module_print_list(void (*pr)(const char *, ...)) 1026 { 1027 const char *src; 1028 module_t *mod; 1029 size_t msize; 1030 vaddr_t maddr; 1031 1032 (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE"); 1033 1034 TAILQ_FOREACH(mod, &module_list, mod_chain) { 1035 switch (mod->mod_source) { 1036 case MODULE_SOURCE_KERNEL: 1037 src = "builtin"; 1038 break; 1039 case MODULE_SOURCE_FILESYS: 1040 src = "filesys"; 1041 break; 1042 case MODULE_SOURCE_BOOT: 1043 src = "boot"; 1044 break; 1045 default: 1046 src = "unknown"; 1047 break; 1048 } 1049 kobj_stat(mod->mod_kobj, &maddr, &msize); 1050 (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name, 1051 (long)maddr, (long)msize, src); 1052 } 1053 } 1054 #endif /* DDB */ 1055