1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997-2000 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ddb.h" 33 #include "opt_kld.h" 34 #include "opt_hwpmc_hooks.h" 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/sysproto.h> 41 #include <sys/sysent.h> 42 #include <sys/priv.h> 43 #include <sys/proc.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/sx.h> 47 #include <sys/module.h> 48 #include <sys/mount.h> 49 #include <sys/linker.h> 50 #include <sys/eventhandler.h> 51 #include <sys/fcntl.h> 52 #include <sys/jail.h> 53 #include <sys/libkern.h> 54 #include <sys/namei.h> 55 #include <sys/vnode.h> 56 #include <sys/syscallsubr.h> 57 #include <sys/sysctl.h> 58 59 #ifdef DDB 60 #include <ddb/ddb.h> 61 #endif 62 63 #include <net/vnet.h> 64 65 #include <security/mac/mac_framework.h> 66 67 #include "linker_if.h" 68 69 #ifdef HWPMC_HOOKS 70 #include <sys/pmckern.h> 71 #endif 72 73 #ifdef KLD_DEBUG 74 int kld_debug = 0; 75 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN, 76 &kld_debug, 0, "Set various levels of KLD debug"); 77 #endif 78 79 /* These variables are used by kernel debuggers to enumerate loaded files. */ 80 const int kld_off_address = offsetof(struct linker_file, address); 81 const int kld_off_filename = offsetof(struct linker_file, filename); 82 const int kld_off_pathname = offsetof(struct linker_file, pathname); 83 const int kld_off_next = offsetof(struct linker_file, link.tqe_next); 84 85 /* 86 * static char *linker_search_path(const char *name, struct mod_depend 87 * *verinfo); 88 */ 89 static const char *linker_basename(const char *path); 90 91 /* 92 * Find a currently loaded file given its filename. 93 */ 94 static linker_file_t linker_find_file_by_name(const char* _filename); 95 96 /* 97 * Find a currently loaded file given its file id. 98 */ 99 static linker_file_t linker_find_file_by_id(int _fileid); 100 101 /* Metadata from the static kernel */ 102 SET_DECLARE(modmetadata_set, struct mod_metadata); 103 104 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker"); 105 106 linker_file_t linker_kernel_file; 107 108 static struct sx kld_sx; /* kernel linker lock */ 109 static u_int kld_busy; 110 static struct thread *kld_busy_owner; 111 112 /* 113 * Load counter used by clients to determine if a linker file has been 114 * re-loaded. This counter is incremented for each file load. 115 */ 116 static int loadcnt; 117 118 static linker_class_list_t classes; 119 static linker_file_list_t linker_files; 120 static int next_file_id = 1; 121 static int linker_no_more_classes = 0; 122 123 #define LINKER_GET_NEXT_FILE_ID(a) do { \ 124 linker_file_t lftmp; \ 125 \ 126 if (!cold) \ 127 sx_assert(&kld_sx, SA_XLOCKED); \ 128 retry: \ 129 TAILQ_FOREACH(lftmp, &linker_files, link) { \ 130 if (next_file_id == lftmp->id) { \ 131 next_file_id++; \ 132 goto retry; \ 133 } \ 134 } \ 135 (a) = next_file_id; \ 136 } while (0) 137 138 /* XXX wrong name; we're looking at version provision tags here, not modules */ 139 typedef TAILQ_HEAD(, modlist) modlisthead_t; 140 struct modlist { 141 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 142 linker_file_t container; 143 const char *name; 144 int version; 145 }; 146 typedef struct modlist *modlist_t; 147 static modlisthead_t found_modules; 148 149 static int linker_file_add_dependency(linker_file_t file, 150 linker_file_t dep); 151 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file, 152 const char* name, int deps); 153 static int linker_load_module(const char *kldname, 154 const char *modname, struct linker_file *parent, 155 const struct mod_depend *verinfo, struct linker_file **lfpp); 156 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo); 157 158 static void 159 linker_init(void *arg) 160 { 161 162 sx_init(&kld_sx, "kernel linker"); 163 TAILQ_INIT(&classes); 164 TAILQ_INIT(&linker_files); 165 } 166 167 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL); 168 169 static void 170 linker_stop_class_add(void *arg) 171 { 172 173 linker_no_more_classes = 1; 174 } 175 176 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL); 177 178 int 179 linker_add_class(linker_class_t lc) 180 { 181 182 /* 183 * We disallow any class registration past SI_ORDER_ANY 184 * of SI_SUB_KLD. We bump the reference count to keep the 185 * ops from being freed. 186 */ 187 if (linker_no_more_classes == 1) 188 return (EPERM); 189 kobj_class_compile((kobj_class_t) lc); 190 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */ 191 TAILQ_INSERT_TAIL(&classes, lc, link); 192 return (0); 193 } 194 195 static void 196 linker_file_sysinit(linker_file_t lf) 197 { 198 struct sysinit **start, **stop, **sipp, **xipp, *save; 199 200 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 201 lf->filename)); 202 203 sx_assert(&kld_sx, SA_XLOCKED); 204 205 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 206 return; 207 /* 208 * Perform a bubble sort of the system initialization objects by 209 * their subsystem (primary key) and order (secondary key). 210 * 211 * Since some things care about execution order, this is the operation 212 * which ensures continued function. 213 */ 214 for (sipp = start; sipp < stop; sipp++) { 215 for (xipp = sipp + 1; xipp < stop; xipp++) { 216 if ((*sipp)->subsystem < (*xipp)->subsystem || 217 ((*sipp)->subsystem == (*xipp)->subsystem && 218 (*sipp)->order <= (*xipp)->order)) 219 continue; /* skip */ 220 save = *sipp; 221 *sipp = *xipp; 222 *xipp = save; 223 } 224 } 225 226 /* 227 * Traverse the (now) ordered list of system initialization tasks. 228 * Perform each task, and continue on to the next task. 229 */ 230 sx_xunlock(&kld_sx); 231 mtx_lock(&Giant); 232 for (sipp = start; sipp < stop; sipp++) { 233 if ((*sipp)->subsystem == SI_SUB_DUMMY) 234 continue; /* skip dummy task(s) */ 235 236 /* Call function */ 237 (*((*sipp)->func)) ((*sipp)->udata); 238 } 239 mtx_unlock(&Giant); 240 sx_xlock(&kld_sx); 241 } 242 243 static void 244 linker_file_sysuninit(linker_file_t lf) 245 { 246 struct sysinit **start, **stop, **sipp, **xipp, *save; 247 248 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 249 lf->filename)); 250 251 sx_assert(&kld_sx, SA_XLOCKED); 252 253 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, 254 NULL) != 0) 255 return; 256 257 /* 258 * Perform a reverse bubble sort of the system initialization objects 259 * by their subsystem (primary key) and order (secondary key). 260 * 261 * Since some things care about execution order, this is the operation 262 * which ensures continued function. 263 */ 264 for (sipp = start; sipp < stop; sipp++) { 265 for (xipp = sipp + 1; xipp < stop; xipp++) { 266 if ((*sipp)->subsystem > (*xipp)->subsystem || 267 ((*sipp)->subsystem == (*xipp)->subsystem && 268 (*sipp)->order >= (*xipp)->order)) 269 continue; /* skip */ 270 save = *sipp; 271 *sipp = *xipp; 272 *xipp = save; 273 } 274 } 275 276 /* 277 * Traverse the (now) ordered list of system initialization tasks. 278 * Perform each task, and continue on to the next task. 279 */ 280 sx_xunlock(&kld_sx); 281 mtx_lock(&Giant); 282 for (sipp = start; sipp < stop; sipp++) { 283 if ((*sipp)->subsystem == SI_SUB_DUMMY) 284 continue; /* skip dummy task(s) */ 285 286 /* Call function */ 287 (*((*sipp)->func)) ((*sipp)->udata); 288 } 289 mtx_unlock(&Giant); 290 sx_xlock(&kld_sx); 291 } 292 293 static void 294 linker_file_register_sysctls(linker_file_t lf, bool enable) 295 { 296 struct sysctl_oid **start, **stop, **oidp; 297 298 KLD_DPF(FILE, 299 ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 300 lf->filename)); 301 302 sx_assert(&kld_sx, SA_XLOCKED); 303 304 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 305 return; 306 307 sx_xunlock(&kld_sx); 308 sysctl_wlock(); 309 for (oidp = start; oidp < stop; oidp++) { 310 if (enable) 311 sysctl_register_oid(*oidp); 312 else 313 sysctl_register_disabled_oid(*oidp); 314 } 315 sysctl_wunlock(); 316 sx_xlock(&kld_sx); 317 } 318 319 static void 320 linker_file_enable_sysctls(linker_file_t lf) 321 { 322 struct sysctl_oid **start, **stop, **oidp; 323 324 KLD_DPF(FILE, 325 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n", 326 lf->filename)); 327 328 sx_assert(&kld_sx, SA_XLOCKED); 329 330 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 331 return; 332 333 sx_xunlock(&kld_sx); 334 sysctl_wlock(); 335 for (oidp = start; oidp < stop; oidp++) 336 sysctl_enable_oid(*oidp); 337 sysctl_wunlock(); 338 sx_xlock(&kld_sx); 339 } 340 341 static void 342 linker_file_unregister_sysctls(linker_file_t lf) 343 { 344 struct sysctl_oid **start, **stop, **oidp; 345 346 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs" 347 " for %s\n", lf->filename)); 348 349 sx_assert(&kld_sx, SA_XLOCKED); 350 351 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 352 return; 353 354 sx_xunlock(&kld_sx); 355 sysctl_wlock(); 356 for (oidp = start; oidp < stop; oidp++) 357 sysctl_unregister_oid(*oidp); 358 sysctl_wunlock(); 359 sx_xlock(&kld_sx); 360 } 361 362 static int 363 linker_file_register_modules(linker_file_t lf) 364 { 365 struct mod_metadata **start, **stop, **mdp; 366 const moduledata_t *moddata; 367 int first_error, error; 368 369 KLD_DPF(FILE, ("linker_file_register_modules: registering modules" 370 " in %s\n", lf->filename)); 371 372 sx_assert(&kld_sx, SA_XLOCKED); 373 374 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) { 375 /* 376 * This fallback should be unnecessary, but if we get booted 377 * from boot2 instead of loader and we are missing our 378 * metadata then we have to try the best we can. 379 */ 380 if (lf == linker_kernel_file) { 381 start = SET_BEGIN(modmetadata_set); 382 stop = SET_LIMIT(modmetadata_set); 383 } else 384 return (0); 385 } 386 first_error = 0; 387 for (mdp = start; mdp < stop; mdp++) { 388 if ((*mdp)->md_type != MDT_MODULE) 389 continue; 390 moddata = (*mdp)->md_data; 391 KLD_DPF(FILE, ("Registering module %s in %s\n", 392 moddata->name, lf->filename)); 393 error = module_register(moddata, lf); 394 if (error) { 395 printf("Module %s failed to register: %d\n", 396 moddata->name, error); 397 if (first_error == 0) 398 first_error = error; 399 } 400 } 401 return (first_error); 402 } 403 404 static void 405 linker_init_kernel_modules(void) 406 { 407 408 sx_xlock(&kld_sx); 409 linker_file_register_modules(linker_kernel_file); 410 sx_xunlock(&kld_sx); 411 } 412 413 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 414 NULL); 415 416 static int 417 linker_load_file(const char *filename, linker_file_t *result) 418 { 419 linker_class_t lc; 420 linker_file_t lf; 421 int foundfile, error, modules; 422 423 /* Refuse to load modules if securelevel raised */ 424 if (prison0.pr_securelevel > 0) 425 return (EPERM); 426 427 sx_assert(&kld_sx, SA_XLOCKED); 428 lf = linker_find_file_by_name(filename); 429 if (lf) { 430 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded," 431 " incrementing refs\n", filename)); 432 *result = lf; 433 lf->refs++; 434 return (0); 435 } 436 foundfile = 0; 437 error = 0; 438 439 /* 440 * We do not need to protect (lock) classes here because there is 441 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY) 442 * and there is no class deregistration mechanism at this time. 443 */ 444 TAILQ_FOREACH(lc, &classes, link) { 445 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n", 446 filename)); 447 error = LINKER_LOAD_FILE(lc, filename, &lf); 448 /* 449 * If we got something other than ENOENT, then it exists but 450 * we cannot load it for some other reason. 451 */ 452 if (error != ENOENT) 453 foundfile = 1; 454 if (lf) { 455 error = linker_file_register_modules(lf); 456 if (error == EEXIST) { 457 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 458 return (error); 459 } 460 modules = !TAILQ_EMPTY(&lf->modules); 461 linker_file_register_sysctls(lf, false); 462 linker_file_sysinit(lf); 463 lf->flags |= LINKER_FILE_LINKED; 464 465 /* 466 * If all of the modules in this file failed 467 * to load, unload the file and return an 468 * error of ENOEXEC. 469 */ 470 if (modules && TAILQ_EMPTY(&lf->modules)) { 471 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 472 return (ENOEXEC); 473 } 474 linker_file_enable_sysctls(lf); 475 EVENTHANDLER_INVOKE(kld_load, lf); 476 *result = lf; 477 return (0); 478 } 479 } 480 /* 481 * Less than ideal, but tells the user whether it failed to load or 482 * the module was not found. 483 */ 484 if (foundfile) { 485 /* 486 * If the file type has not been recognized by the last try 487 * printout a message before to fail. 488 */ 489 if (error == ENOSYS) 490 printf("%s: %s - unsupported file type\n", 491 __func__, filename); 492 493 /* 494 * Format not recognized or otherwise unloadable. 495 * When loading a module that is statically built into 496 * the kernel EEXIST percolates back up as the return 497 * value. Preserve this so that apps like sysinstall 498 * can recognize this special case and not post bogus 499 * dialog boxes. 500 */ 501 if (error != EEXIST) 502 error = ENOEXEC; 503 } else 504 error = ENOENT; /* Nothing found */ 505 return (error); 506 } 507 508 int 509 linker_reference_module(const char *modname, struct mod_depend *verinfo, 510 linker_file_t *result) 511 { 512 modlist_t mod; 513 int error; 514 515 sx_xlock(&kld_sx); 516 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) { 517 *result = mod->container; 518 (*result)->refs++; 519 sx_xunlock(&kld_sx); 520 return (0); 521 } 522 523 error = linker_load_module(NULL, modname, NULL, verinfo, result); 524 sx_xunlock(&kld_sx); 525 return (error); 526 } 527 528 int 529 linker_release_module(const char *modname, struct mod_depend *verinfo, 530 linker_file_t lf) 531 { 532 modlist_t mod; 533 int error; 534 535 sx_xlock(&kld_sx); 536 if (lf == NULL) { 537 KASSERT(modname != NULL, 538 ("linker_release_module: no file or name")); 539 mod = modlist_lookup2(modname, verinfo); 540 if (mod == NULL) { 541 sx_xunlock(&kld_sx); 542 return (ESRCH); 543 } 544 lf = mod->container; 545 } else 546 KASSERT(modname == NULL && verinfo == NULL, 547 ("linker_release_module: both file and name")); 548 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL); 549 sx_xunlock(&kld_sx); 550 return (error); 551 } 552 553 static linker_file_t 554 linker_find_file_by_name(const char *filename) 555 { 556 linker_file_t lf; 557 char *koname; 558 559 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 560 sprintf(koname, "%s.ko", filename); 561 562 sx_assert(&kld_sx, SA_XLOCKED); 563 TAILQ_FOREACH(lf, &linker_files, link) { 564 if (strcmp(lf->filename, koname) == 0) 565 break; 566 if (strcmp(lf->filename, filename) == 0) 567 break; 568 } 569 free(koname, M_LINKER); 570 return (lf); 571 } 572 573 static linker_file_t 574 linker_find_file_by_id(int fileid) 575 { 576 linker_file_t lf; 577 578 sx_assert(&kld_sx, SA_XLOCKED); 579 TAILQ_FOREACH(lf, &linker_files, link) 580 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED) 581 break; 582 return (lf); 583 } 584 585 int 586 linker_file_foreach(linker_predicate_t *predicate, void *context) 587 { 588 linker_file_t lf; 589 int retval = 0; 590 591 sx_xlock(&kld_sx); 592 TAILQ_FOREACH(lf, &linker_files, link) { 593 retval = predicate(lf, context); 594 if (retval != 0) 595 break; 596 } 597 sx_xunlock(&kld_sx); 598 return (retval); 599 } 600 601 linker_file_t 602 linker_make_file(const char *pathname, linker_class_t lc) 603 { 604 linker_file_t lf; 605 const char *filename; 606 607 if (!cold) 608 sx_assert(&kld_sx, SA_XLOCKED); 609 filename = linker_basename(pathname); 610 611 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname)); 612 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK); 613 if (lf == NULL) 614 return (NULL); 615 lf->ctors_addr = 0; 616 lf->ctors_size = 0; 617 lf->dtors_addr = 0; 618 lf->dtors_size = 0; 619 lf->refs = 1; 620 lf->userrefs = 0; 621 lf->flags = 0; 622 lf->filename = strdup(filename, M_LINKER); 623 lf->pathname = strdup(pathname, M_LINKER); 624 LINKER_GET_NEXT_FILE_ID(lf->id); 625 lf->ndeps = 0; 626 lf->deps = NULL; 627 lf->loadcnt = ++loadcnt; 628 #ifdef __arm__ 629 lf->exidx_addr = 0; 630 lf->exidx_size = 0; 631 #endif 632 STAILQ_INIT(&lf->common); 633 TAILQ_INIT(&lf->modules); 634 TAILQ_INSERT_TAIL(&linker_files, lf, link); 635 return (lf); 636 } 637 638 int 639 linker_file_unload(linker_file_t file, int flags) 640 { 641 module_t mod, next; 642 modlist_t ml, nextml; 643 struct common_symbol *cp; 644 int error, i; 645 646 /* Refuse to unload modules if securelevel raised. */ 647 if (prison0.pr_securelevel > 0) 648 return (EPERM); 649 650 sx_assert(&kld_sx, SA_XLOCKED); 651 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 652 653 /* Easy case of just dropping a reference. */ 654 if (file->refs > 1) { 655 file->refs--; 656 return (0); 657 } 658 659 /* Give eventhandlers a chance to prevent the unload. */ 660 error = 0; 661 EVENTHANDLER_INVOKE(kld_unload_try, file, &error); 662 if (error != 0) 663 return (EBUSY); 664 665 KLD_DPF(FILE, ("linker_file_unload: file is unloading," 666 " informing modules\n")); 667 668 /* 669 * Quiesce all the modules to give them a chance to veto the unload. 670 */ 671 MOD_SLOCK; 672 for (mod = TAILQ_FIRST(&file->modules); mod; 673 mod = module_getfnext(mod)) { 674 error = module_quiesce(mod); 675 if (error != 0 && flags != LINKER_UNLOAD_FORCE) { 676 KLD_DPF(FILE, ("linker_file_unload: module %s" 677 " vetoed unload\n", module_getname(mod))); 678 /* 679 * XXX: Do we need to tell all the quiesced modules 680 * that they can resume work now via a new module 681 * event? 682 */ 683 MOD_SUNLOCK; 684 return (error); 685 } 686 } 687 MOD_SUNLOCK; 688 689 /* 690 * Inform any modules associated with this file that they are 691 * being unloaded. 692 */ 693 MOD_XLOCK; 694 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 695 next = module_getfnext(mod); 696 MOD_XUNLOCK; 697 698 /* 699 * Give the module a chance to veto the unload. 700 */ 701 if ((error = module_unload(mod)) != 0) { 702 #ifdef KLD_DEBUG 703 MOD_SLOCK; 704 KLD_DPF(FILE, ("linker_file_unload: module %s" 705 " failed unload\n", module_getname(mod))); 706 MOD_SUNLOCK; 707 #endif 708 return (error); 709 } 710 MOD_XLOCK; 711 module_release(mod); 712 } 713 MOD_XUNLOCK; 714 715 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) { 716 if (ml->container == file) { 717 TAILQ_REMOVE(&found_modules, ml, link); 718 free(ml, M_LINKER); 719 } 720 } 721 722 /* 723 * Don't try to run SYSUNINITs if we are unloaded due to a 724 * link error. 725 */ 726 if (file->flags & LINKER_FILE_LINKED) { 727 file->flags &= ~LINKER_FILE_LINKED; 728 linker_file_unregister_sysctls(file); 729 linker_file_sysuninit(file); 730 } 731 TAILQ_REMOVE(&linker_files, file, link); 732 733 if (file->deps) { 734 for (i = 0; i < file->ndeps; i++) 735 linker_file_unload(file->deps[i], flags); 736 free(file->deps, M_LINKER); 737 file->deps = NULL; 738 } 739 while ((cp = STAILQ_FIRST(&file->common)) != NULL) { 740 STAILQ_REMOVE_HEAD(&file->common, link); 741 free(cp, M_LINKER); 742 } 743 744 LINKER_UNLOAD(file); 745 746 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address, 747 file->size); 748 749 if (file->filename) { 750 free(file->filename, M_LINKER); 751 file->filename = NULL; 752 } 753 if (file->pathname) { 754 free(file->pathname, M_LINKER); 755 file->pathname = NULL; 756 } 757 kobj_delete((kobj_t) file, M_LINKER); 758 return (0); 759 } 760 761 int 762 linker_ctf_get(linker_file_t file, linker_ctf_t *lc) 763 { 764 return (LINKER_CTF_GET(file, lc)); 765 } 766 767 static int 768 linker_file_add_dependency(linker_file_t file, linker_file_t dep) 769 { 770 linker_file_t *newdeps; 771 772 sx_assert(&kld_sx, SA_XLOCKED); 773 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps), 774 M_LINKER, M_WAITOK | M_ZERO); 775 file->deps[file->ndeps] = dep; 776 file->ndeps++; 777 KLD_DPF(FILE, ("linker_file_add_dependency:" 778 " adding %s as dependency for %s\n", 779 dep->filename, file->filename)); 780 return (0); 781 } 782 783 /* 784 * Locate a linker set and its contents. This is a helper function to avoid 785 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **. 786 * This function is used in this file so we can avoid having lots of (void **) 787 * casts. 788 */ 789 int 790 linker_file_lookup_set(linker_file_t file, const char *name, 791 void *firstp, void *lastp, int *countp) 792 { 793 794 sx_assert(&kld_sx, SA_LOCKED); 795 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp)); 796 } 797 798 /* 799 * List all functions in a file. 800 */ 801 int 802 linker_file_function_listall(linker_file_t lf, 803 linker_function_nameval_callback_t callback_func, void *arg) 804 { 805 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg)); 806 } 807 808 caddr_t 809 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps) 810 { 811 caddr_t sym; 812 int locked; 813 814 locked = sx_xlocked(&kld_sx); 815 if (!locked) 816 sx_xlock(&kld_sx); 817 sym = linker_file_lookup_symbol_internal(file, name, deps); 818 if (!locked) 819 sx_xunlock(&kld_sx); 820 return (sym); 821 } 822 823 static caddr_t 824 linker_file_lookup_symbol_internal(linker_file_t file, const char *name, 825 int deps) 826 { 827 c_linker_sym_t sym; 828 linker_symval_t symval; 829 caddr_t address; 830 size_t common_size = 0; 831 int i; 832 833 sx_assert(&kld_sx, SA_XLOCKED); 834 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 835 file, name, deps)); 836 837 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 838 LINKER_SYMBOL_VALUES(file, sym, &symval); 839 if (symval.value == 0) 840 /* 841 * For commons, first look them up in the 842 * dependencies and only allocate space if not found 843 * there. 844 */ 845 common_size = symval.size; 846 else { 847 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol" 848 ".value=%p\n", symval.value)); 849 return (symval.value); 850 } 851 } 852 if (deps) { 853 for (i = 0; i < file->ndeps; i++) { 854 address = linker_file_lookup_symbol_internal( 855 file->deps[i], name, 0); 856 if (address) { 857 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 858 " deps value=%p\n", address)); 859 return (address); 860 } 861 } 862 } 863 if (common_size > 0) { 864 /* 865 * This is a common symbol which was not found in the 866 * dependencies. We maintain a simple common symbol table in 867 * the file object. 868 */ 869 struct common_symbol *cp; 870 871 STAILQ_FOREACH(cp, &file->common, link) { 872 if (strcmp(cp->name, name) == 0) { 873 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 874 " old common value=%p\n", cp->address)); 875 return (cp->address); 876 } 877 } 878 /* 879 * Round the symbol size up to align. 880 */ 881 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 882 cp = malloc(sizeof(struct common_symbol) 883 + common_size + strlen(name) + 1, M_LINKER, 884 M_WAITOK | M_ZERO); 885 cp->address = (caddr_t)(cp + 1); 886 cp->name = cp->address + common_size; 887 strcpy(cp->name, name); 888 bzero(cp->address, common_size); 889 STAILQ_INSERT_TAIL(&file->common, cp, link); 890 891 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common" 892 " value=%p\n", cp->address)); 893 return (cp->address); 894 } 895 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 896 return (0); 897 } 898 899 /* 900 * Both DDB and stack(9) rely on the kernel linker to provide forward and 901 * backward lookup of symbols. However, DDB and sometimes stack(9) need to 902 * do this in a lockfree manner. We provide a set of internal helper 903 * routines to perform these operations without locks, and then wrappers that 904 * optionally lock. 905 * 906 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB. 907 */ 908 #ifdef DDB 909 static int 910 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym) 911 { 912 linker_file_t lf; 913 914 TAILQ_FOREACH(lf, &linker_files, link) { 915 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 916 return (0); 917 } 918 return (ENOENT); 919 } 920 #endif 921 922 static int 923 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 924 { 925 linker_file_t lf; 926 c_linker_sym_t best, es; 927 u_long diff, bestdiff, off; 928 929 best = 0; 930 off = (uintptr_t)value; 931 bestdiff = off; 932 TAILQ_FOREACH(lf, &linker_files, link) { 933 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 934 continue; 935 if (es != 0 && diff < bestdiff) { 936 best = es; 937 bestdiff = diff; 938 } 939 if (bestdiff == 0) 940 break; 941 } 942 if (best) { 943 *sym = best; 944 *diffp = bestdiff; 945 return (0); 946 } else { 947 *sym = 0; 948 *diffp = off; 949 return (ENOENT); 950 } 951 } 952 953 static int 954 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 955 { 956 linker_file_t lf; 957 958 TAILQ_FOREACH(lf, &linker_files, link) { 959 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 960 return (0); 961 } 962 return (ENOENT); 963 } 964 965 static int 966 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen, 967 long *offset) 968 { 969 linker_symval_t symval; 970 c_linker_sym_t sym; 971 int error; 972 973 *offset = 0; 974 error = linker_debug_search_symbol(value, &sym, offset); 975 if (error) 976 return (error); 977 error = linker_debug_symbol_values(sym, &symval); 978 if (error) 979 return (error); 980 strlcpy(buf, symval.name, buflen); 981 return (0); 982 } 983 984 /* 985 * DDB Helpers. DDB has to look across multiple files with their own symbol 986 * tables and string tables. 987 * 988 * Note that we do not obey list locking protocols here. We really don't need 989 * DDB to hang because somebody's got the lock held. We'll take the chance 990 * that the files list is inconsistent instead. 991 */ 992 #ifdef DDB 993 int 994 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 995 { 996 997 return (linker_debug_lookup(symstr, sym)); 998 } 999 #endif 1000 1001 int 1002 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 1003 { 1004 1005 return (linker_debug_search_symbol(value, sym, diffp)); 1006 } 1007 1008 int 1009 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 1010 { 1011 1012 return (linker_debug_symbol_values(sym, symval)); 1013 } 1014 1015 int 1016 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, 1017 long *offset) 1018 { 1019 1020 return (linker_debug_search_symbol_name(value, buf, buflen, offset)); 1021 } 1022 1023 /* 1024 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do 1025 * obey locking protocols, and offer a significantly less complex interface. 1026 */ 1027 int 1028 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen, 1029 long *offset, int flags) 1030 { 1031 int error; 1032 1033 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 && 1034 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK), 1035 ("%s: bad flags: 0x%x", __func__, flags)); 1036 1037 if (flags & M_NOWAIT) { 1038 if (!sx_try_slock(&kld_sx)) 1039 return (EWOULDBLOCK); 1040 } else 1041 sx_slock(&kld_sx); 1042 1043 error = linker_debug_search_symbol_name(value, buf, buflen, offset); 1044 sx_sunlock(&kld_sx); 1045 return (error); 1046 } 1047 1048 int 1049 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 1050 long *offset) 1051 { 1052 1053 return (linker_search_symbol_name_flags(value, buf, buflen, offset, 1054 M_WAITOK)); 1055 } 1056 1057 int 1058 linker_kldload_busy(int flags) 1059 { 1060 int error; 1061 1062 MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED | 1063 LINKER_UB_PCATCH)) == 0); 1064 if ((flags & LINKER_UB_LOCKED) != 0) 1065 sx_assert(&kld_sx, SA_XLOCKED); 1066 1067 if ((flags & LINKER_UB_LOCKED) == 0) 1068 sx_xlock(&kld_sx); 1069 while (kld_busy > 0) { 1070 if (kld_busy_owner == curthread) 1071 break; 1072 error = sx_sleep(&kld_busy, &kld_sx, 1073 (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0, 1074 "kldbusy", 0); 1075 if (error != 0) { 1076 if ((flags & LINKER_UB_UNLOCK) != 0) 1077 sx_xunlock(&kld_sx); 1078 return (error); 1079 } 1080 } 1081 kld_busy++; 1082 kld_busy_owner = curthread; 1083 if ((flags & LINKER_UB_UNLOCK) != 0) 1084 sx_xunlock(&kld_sx); 1085 return (0); 1086 } 1087 1088 void 1089 linker_kldload_unbusy(int flags) 1090 { 1091 MPASS((flags & ~LINKER_UB_LOCKED) == 0); 1092 if ((flags & LINKER_UB_LOCKED) != 0) 1093 sx_assert(&kld_sx, SA_XLOCKED); 1094 1095 if ((flags & LINKER_UB_LOCKED) == 0) 1096 sx_xlock(&kld_sx); 1097 MPASS(kld_busy > 0); 1098 if (kld_busy_owner != curthread) 1099 panic("linker_kldload_unbusy done by not owning thread %p", 1100 kld_busy_owner); 1101 kld_busy--; 1102 if (kld_busy == 0) { 1103 kld_busy_owner = NULL; 1104 wakeup(&kld_busy); 1105 } 1106 sx_xunlock(&kld_sx); 1107 } 1108 1109 /* 1110 * Syscalls. 1111 */ 1112 int 1113 kern_kldload(struct thread *td, const char *file, int *fileid) 1114 { 1115 const char *kldname, *modname; 1116 linker_file_t lf; 1117 int error; 1118 1119 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1120 return (error); 1121 1122 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 1123 return (error); 1124 1125 /* 1126 * If file does not contain a qualified name or any dot in it 1127 * (kldname.ko, or kldname.ver.ko) treat it as an interface 1128 * name. 1129 */ 1130 if (strchr(file, '/') || strchr(file, '.')) { 1131 kldname = file; 1132 modname = NULL; 1133 } else { 1134 kldname = NULL; 1135 modname = file; 1136 } 1137 1138 error = linker_kldload_busy(LINKER_UB_PCATCH); 1139 if (error != 0) { 1140 sx_xunlock(&kld_sx); 1141 return (error); 1142 } 1143 1144 /* 1145 * It is possible that kldloaded module will attach a new ifnet, 1146 * so vnet context must be set when this ocurs. 1147 */ 1148 CURVNET_SET(TD_TO_VNET(td)); 1149 1150 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 1151 CURVNET_RESTORE(); 1152 1153 if (error == 0) { 1154 lf->userrefs++; 1155 if (fileid != NULL) 1156 *fileid = lf->id; 1157 } 1158 linker_kldload_unbusy(LINKER_UB_LOCKED); 1159 return (error); 1160 } 1161 1162 int 1163 sys_kldload(struct thread *td, struct kldload_args *uap) 1164 { 1165 char *pathname = NULL; 1166 int error, fileid; 1167 1168 td->td_retval[0] = -1; 1169 1170 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1171 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1172 if (error == 0) { 1173 error = kern_kldload(td, pathname, &fileid); 1174 if (error == 0) 1175 td->td_retval[0] = fileid; 1176 } 1177 free(pathname, M_TEMP); 1178 return (error); 1179 } 1180 1181 int 1182 kern_kldunload(struct thread *td, int fileid, int flags) 1183 { 1184 linker_file_t lf; 1185 int error = 0; 1186 1187 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1188 return (error); 1189 1190 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1191 return (error); 1192 1193 error = linker_kldload_busy(LINKER_UB_PCATCH); 1194 if (error != 0) { 1195 sx_xunlock(&kld_sx); 1196 return (error); 1197 } 1198 1199 CURVNET_SET(TD_TO_VNET(td)); 1200 lf = linker_find_file_by_id(fileid); 1201 if (lf) { 1202 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1203 1204 if (lf->userrefs == 0) { 1205 /* 1206 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1207 */ 1208 printf("kldunload: attempt to unload file that was" 1209 " loaded by the kernel\n"); 1210 error = EBUSY; 1211 } else { 1212 lf->userrefs--; 1213 error = linker_file_unload(lf, flags); 1214 if (error) 1215 lf->userrefs++; 1216 } 1217 } else 1218 error = ENOENT; 1219 CURVNET_RESTORE(); 1220 linker_kldload_unbusy(LINKER_UB_LOCKED); 1221 return (error); 1222 } 1223 1224 int 1225 sys_kldunload(struct thread *td, struct kldunload_args *uap) 1226 { 1227 1228 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1229 } 1230 1231 int 1232 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1233 { 1234 1235 if (uap->flags != LINKER_UNLOAD_NORMAL && 1236 uap->flags != LINKER_UNLOAD_FORCE) 1237 return (EINVAL); 1238 return (kern_kldunload(td, uap->fileid, uap->flags)); 1239 } 1240 1241 int 1242 sys_kldfind(struct thread *td, struct kldfind_args *uap) 1243 { 1244 char *pathname; 1245 const char *filename; 1246 linker_file_t lf; 1247 int error; 1248 1249 #ifdef MAC 1250 error = mac_kld_check_stat(td->td_ucred); 1251 if (error) 1252 return (error); 1253 #endif 1254 1255 td->td_retval[0] = -1; 1256 1257 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1258 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1259 goto out; 1260 1261 filename = linker_basename(pathname); 1262 sx_xlock(&kld_sx); 1263 lf = linker_find_file_by_name(filename); 1264 if (lf) 1265 td->td_retval[0] = lf->id; 1266 else 1267 error = ENOENT; 1268 sx_xunlock(&kld_sx); 1269 out: 1270 free(pathname, M_TEMP); 1271 return (error); 1272 } 1273 1274 int 1275 sys_kldnext(struct thread *td, struct kldnext_args *uap) 1276 { 1277 linker_file_t lf; 1278 int error = 0; 1279 1280 #ifdef MAC 1281 error = mac_kld_check_stat(td->td_ucred); 1282 if (error) 1283 return (error); 1284 #endif 1285 1286 sx_xlock(&kld_sx); 1287 if (uap->fileid == 0) 1288 lf = TAILQ_FIRST(&linker_files); 1289 else { 1290 lf = linker_find_file_by_id(uap->fileid); 1291 if (lf == NULL) { 1292 error = ENOENT; 1293 goto out; 1294 } 1295 lf = TAILQ_NEXT(lf, link); 1296 } 1297 1298 /* Skip partially loaded files. */ 1299 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1300 lf = TAILQ_NEXT(lf, link); 1301 1302 if (lf) 1303 td->td_retval[0] = lf->id; 1304 else 1305 td->td_retval[0] = 0; 1306 out: 1307 sx_xunlock(&kld_sx); 1308 return (error); 1309 } 1310 1311 int 1312 sys_kldstat(struct thread *td, struct kldstat_args *uap) 1313 { 1314 struct kld_file_stat *stat; 1315 int error, version; 1316 1317 /* 1318 * Check the version of the user's structure. 1319 */ 1320 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 1321 != 0) 1322 return (error); 1323 if (version != sizeof(struct kld_file_stat_1) && 1324 version != sizeof(struct kld_file_stat)) 1325 return (EINVAL); 1326 1327 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 1328 error = kern_kldstat(td, uap->fileid, stat); 1329 if (error == 0) 1330 error = copyout(stat, uap->stat, version); 1331 free(stat, M_TEMP); 1332 return (error); 1333 } 1334 1335 int 1336 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat) 1337 { 1338 linker_file_t lf; 1339 int namelen; 1340 #ifdef MAC 1341 int error; 1342 1343 error = mac_kld_check_stat(td->td_ucred); 1344 if (error) 1345 return (error); 1346 #endif 1347 1348 sx_xlock(&kld_sx); 1349 lf = linker_find_file_by_id(fileid); 1350 if (lf == NULL) { 1351 sx_xunlock(&kld_sx); 1352 return (ENOENT); 1353 } 1354 1355 /* Version 1 fields: */ 1356 namelen = strlen(lf->filename) + 1; 1357 if (namelen > sizeof(stat->name)) 1358 namelen = sizeof(stat->name); 1359 bcopy(lf->filename, &stat->name[0], namelen); 1360 stat->refs = lf->refs; 1361 stat->id = lf->id; 1362 stat->address = lf->address; 1363 stat->size = lf->size; 1364 /* Version 2 fields: */ 1365 namelen = strlen(lf->pathname) + 1; 1366 if (namelen > sizeof(stat->pathname)) 1367 namelen = sizeof(stat->pathname); 1368 bcopy(lf->pathname, &stat->pathname[0], namelen); 1369 sx_xunlock(&kld_sx); 1370 1371 td->td_retval[0] = 0; 1372 return (0); 1373 } 1374 1375 #ifdef DDB 1376 DB_COMMAND(kldstat, db_kldstat) 1377 { 1378 linker_file_t lf; 1379 1380 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 1381 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1382 #undef POINTER_WIDTH 1383 TAILQ_FOREACH(lf, &linker_files, link) { 1384 if (db_pager_quit) 1385 return; 1386 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs, 1387 lf->address, lf->size, lf->filename); 1388 } 1389 } 1390 #endif /* DDB */ 1391 1392 int 1393 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1394 { 1395 linker_file_t lf; 1396 module_t mp; 1397 int error = 0; 1398 1399 #ifdef MAC 1400 error = mac_kld_check_stat(td->td_ucred); 1401 if (error) 1402 return (error); 1403 #endif 1404 1405 sx_xlock(&kld_sx); 1406 lf = linker_find_file_by_id(uap->fileid); 1407 if (lf) { 1408 MOD_SLOCK; 1409 mp = TAILQ_FIRST(&lf->modules); 1410 if (mp != NULL) 1411 td->td_retval[0] = module_getid(mp); 1412 else 1413 td->td_retval[0] = 0; 1414 MOD_SUNLOCK; 1415 } else 1416 error = ENOENT; 1417 sx_xunlock(&kld_sx); 1418 return (error); 1419 } 1420 1421 int 1422 sys_kldsym(struct thread *td, struct kldsym_args *uap) 1423 { 1424 char *symstr = NULL; 1425 c_linker_sym_t sym; 1426 linker_symval_t symval; 1427 linker_file_t lf; 1428 struct kld_sym_lookup lookup; 1429 int error = 0; 1430 1431 #ifdef MAC 1432 error = mac_kld_check_stat(td->td_ucred); 1433 if (error) 1434 return (error); 1435 #endif 1436 1437 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1438 return (error); 1439 if (lookup.version != sizeof(lookup) || 1440 uap->cmd != KLDSYM_LOOKUP) 1441 return (EINVAL); 1442 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1443 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1444 goto out; 1445 sx_xlock(&kld_sx); 1446 if (uap->fileid != 0) { 1447 lf = linker_find_file_by_id(uap->fileid); 1448 if (lf == NULL) 1449 error = ENOENT; 1450 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1451 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1452 lookup.symvalue = (uintptr_t) symval.value; 1453 lookup.symsize = symval.size; 1454 error = copyout(&lookup, uap->data, sizeof(lookup)); 1455 } else 1456 error = ENOENT; 1457 } else { 1458 TAILQ_FOREACH(lf, &linker_files, link) { 1459 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1460 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1461 lookup.symvalue = (uintptr_t)symval.value; 1462 lookup.symsize = symval.size; 1463 error = copyout(&lookup, uap->data, 1464 sizeof(lookup)); 1465 break; 1466 } 1467 } 1468 if (lf == NULL) 1469 error = ENOENT; 1470 } 1471 sx_xunlock(&kld_sx); 1472 out: 1473 free(symstr, M_TEMP); 1474 return (error); 1475 } 1476 1477 /* 1478 * Preloaded module support 1479 */ 1480 1481 static modlist_t 1482 modlist_lookup(const char *name, int ver) 1483 { 1484 modlist_t mod; 1485 1486 TAILQ_FOREACH(mod, &found_modules, link) { 1487 if (strcmp(mod->name, name) == 0 && 1488 (ver == 0 || mod->version == ver)) 1489 return (mod); 1490 } 1491 return (NULL); 1492 } 1493 1494 static modlist_t 1495 modlist_lookup2(const char *name, const struct mod_depend *verinfo) 1496 { 1497 modlist_t mod, bestmod; 1498 int ver; 1499 1500 if (verinfo == NULL) 1501 return (modlist_lookup(name, 0)); 1502 bestmod = NULL; 1503 TAILQ_FOREACH(mod, &found_modules, link) { 1504 if (strcmp(mod->name, name) != 0) 1505 continue; 1506 ver = mod->version; 1507 if (ver == verinfo->md_ver_preferred) 1508 return (mod); 1509 if (ver >= verinfo->md_ver_minimum && 1510 ver <= verinfo->md_ver_maximum && 1511 (bestmod == NULL || ver > bestmod->version)) 1512 bestmod = mod; 1513 } 1514 return (bestmod); 1515 } 1516 1517 static modlist_t 1518 modlist_newmodule(const char *modname, int version, linker_file_t container) 1519 { 1520 modlist_t mod; 1521 1522 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1523 if (mod == NULL) 1524 panic("no memory for module list"); 1525 mod->container = container; 1526 mod->name = modname; 1527 mod->version = version; 1528 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1529 return (mod); 1530 } 1531 1532 static void 1533 linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1534 struct mod_metadata **stop, int preload) 1535 { 1536 struct mod_metadata *mp, **mdp; 1537 const char *modname; 1538 int ver; 1539 1540 for (mdp = start; mdp < stop; mdp++) { 1541 mp = *mdp; 1542 if (mp->md_type != MDT_VERSION) 1543 continue; 1544 modname = mp->md_cval; 1545 ver = ((const struct mod_version *)mp->md_data)->mv_version; 1546 if (modlist_lookup(modname, ver) != NULL) { 1547 printf("module %s already present!\n", modname); 1548 /* XXX what can we do? this is a build error. :-( */ 1549 continue; 1550 } 1551 modlist_newmodule(modname, ver, lf); 1552 } 1553 } 1554 1555 static void 1556 linker_preload(void *arg) 1557 { 1558 caddr_t modptr; 1559 const char *modname, *nmodname; 1560 char *modtype; 1561 linker_file_t lf, nlf; 1562 linker_class_t lc; 1563 int error; 1564 linker_file_list_t loaded_files; 1565 linker_file_list_t depended_files; 1566 struct mod_metadata *mp, *nmp; 1567 struct mod_metadata **start, **stop, **mdp, **nmdp; 1568 const struct mod_depend *verinfo; 1569 int nver; 1570 int resolves; 1571 modlist_t mod; 1572 struct sysinit **si_start, **si_stop; 1573 1574 TAILQ_INIT(&loaded_files); 1575 TAILQ_INIT(&depended_files); 1576 TAILQ_INIT(&found_modules); 1577 error = 0; 1578 1579 modptr = NULL; 1580 sx_xlock(&kld_sx); 1581 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1582 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1583 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1584 if (modname == NULL) { 1585 printf("Preloaded module at %p does not have a" 1586 " name!\n", modptr); 1587 continue; 1588 } 1589 if (modtype == NULL) { 1590 printf("Preloaded module at %p does not have a type!\n", 1591 modptr); 1592 continue; 1593 } 1594 if (bootverbose) 1595 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1596 modptr); 1597 lf = NULL; 1598 TAILQ_FOREACH(lc, &classes, link) { 1599 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1600 if (!error) 1601 break; 1602 lf = NULL; 1603 } 1604 if (lf) 1605 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1606 } 1607 1608 /* 1609 * First get a list of stuff in the kernel. 1610 */ 1611 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1612 &stop, NULL) == 0) 1613 linker_addmodules(linker_kernel_file, start, stop, 1); 1614 1615 /* 1616 * This is a once-off kinky bubble sort to resolve relocation 1617 * dependency requirements. 1618 */ 1619 restart: 1620 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1621 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1622 &stop, NULL); 1623 /* 1624 * First, look to see if we would successfully link with this 1625 * stuff. 1626 */ 1627 resolves = 1; /* unless we know otherwise */ 1628 if (!error) { 1629 for (mdp = start; mdp < stop; mdp++) { 1630 mp = *mdp; 1631 if (mp->md_type != MDT_DEPEND) 1632 continue; 1633 modname = mp->md_cval; 1634 verinfo = mp->md_data; 1635 for (nmdp = start; nmdp < stop; nmdp++) { 1636 nmp = *nmdp; 1637 if (nmp->md_type != MDT_VERSION) 1638 continue; 1639 nmodname = nmp->md_cval; 1640 if (strcmp(modname, nmodname) == 0) 1641 break; 1642 } 1643 if (nmdp < stop) /* it's a self reference */ 1644 continue; 1645 1646 /* 1647 * ok, the module isn't here yet, we 1648 * are not finished 1649 */ 1650 if (modlist_lookup2(modname, verinfo) == NULL) 1651 resolves = 0; 1652 } 1653 } 1654 /* 1655 * OK, if we found our modules, we can link. So, "provide" 1656 * the modules inside and add it to the end of the link order 1657 * list. 1658 */ 1659 if (resolves) { 1660 if (!error) { 1661 for (mdp = start; mdp < stop; mdp++) { 1662 mp = *mdp; 1663 if (mp->md_type != MDT_VERSION) 1664 continue; 1665 modname = mp->md_cval; 1666 nver = ((const struct mod_version *) 1667 mp->md_data)->mv_version; 1668 if (modlist_lookup(modname, 1669 nver) != NULL) { 1670 printf("module %s already" 1671 " present!\n", modname); 1672 TAILQ_REMOVE(&loaded_files, 1673 lf, loaded); 1674 linker_file_unload(lf, 1675 LINKER_UNLOAD_FORCE); 1676 /* we changed tailq next ptr */ 1677 goto restart; 1678 } 1679 modlist_newmodule(modname, nver, lf); 1680 } 1681 } 1682 TAILQ_REMOVE(&loaded_files, lf, loaded); 1683 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1684 /* 1685 * Since we provided modules, we need to restart the 1686 * sort so that the previous files that depend on us 1687 * have a chance. Also, we've busted the tailq next 1688 * pointer with the REMOVE. 1689 */ 1690 goto restart; 1691 } 1692 } 1693 1694 /* 1695 * At this point, we check to see what could not be resolved.. 1696 */ 1697 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1698 TAILQ_REMOVE(&loaded_files, lf, loaded); 1699 printf("KLD file %s is missing dependencies\n", lf->filename); 1700 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1701 } 1702 1703 /* 1704 * We made it. Finish off the linking in the order we determined. 1705 */ 1706 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1707 if (linker_kernel_file) { 1708 linker_kernel_file->refs++; 1709 error = linker_file_add_dependency(lf, 1710 linker_kernel_file); 1711 if (error) 1712 panic("cannot add dependency"); 1713 } 1714 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1715 &stop, NULL); 1716 if (!error) { 1717 for (mdp = start; mdp < stop; mdp++) { 1718 mp = *mdp; 1719 if (mp->md_type != MDT_DEPEND) 1720 continue; 1721 modname = mp->md_cval; 1722 verinfo = mp->md_data; 1723 mod = modlist_lookup2(modname, verinfo); 1724 if (mod == NULL) { 1725 printf("KLD file %s - cannot find " 1726 "dependency \"%s\"\n", 1727 lf->filename, modname); 1728 goto fail; 1729 } 1730 /* Don't count self-dependencies */ 1731 if (lf == mod->container) 1732 continue; 1733 mod->container->refs++; 1734 error = linker_file_add_dependency(lf, 1735 mod->container); 1736 if (error) 1737 panic("cannot add dependency"); 1738 } 1739 } 1740 /* 1741 * Now do relocation etc using the symbol search paths 1742 * established by the dependencies 1743 */ 1744 error = LINKER_LINK_PRELOAD_FINISH(lf); 1745 if (error) { 1746 printf("KLD file %s - could not finalize loading\n", 1747 lf->filename); 1748 goto fail; 1749 } 1750 linker_file_register_modules(lf); 1751 if (!TAILQ_EMPTY(&lf->modules)) 1752 lf->flags |= LINKER_FILE_MODULES; 1753 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1754 &si_stop, NULL) == 0) 1755 sysinit_add(si_start, si_stop); 1756 linker_file_register_sysctls(lf, true); 1757 lf->flags |= LINKER_FILE_LINKED; 1758 continue; 1759 fail: 1760 TAILQ_REMOVE(&depended_files, lf, loaded); 1761 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1762 } 1763 sx_xunlock(&kld_sx); 1764 /* woohoo! we made it! */ 1765 } 1766 1767 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL); 1768 1769 /* 1770 * Handle preload files that failed to load any modules. 1771 */ 1772 static void 1773 linker_preload_finish(void *arg) 1774 { 1775 linker_file_t lf, nlf; 1776 1777 sx_xlock(&kld_sx); 1778 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) { 1779 /* 1780 * If all of the modules in this file failed to load, unload 1781 * the file and return an error of ENOEXEC. (Parity with 1782 * linker_load_file.) 1783 */ 1784 if ((lf->flags & LINKER_FILE_MODULES) != 0 && 1785 TAILQ_EMPTY(&lf->modules)) { 1786 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1787 continue; 1788 } 1789 1790 lf->flags &= ~LINKER_FILE_MODULES; 1791 lf->userrefs++; /* so we can (try to) kldunload it */ 1792 } 1793 sx_xunlock(&kld_sx); 1794 } 1795 1796 /* 1797 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be 1798 * scheduled at any subsystem and order, so run this as late as possible. init 1799 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that. 1800 */ 1801 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE, 1802 linker_preload_finish, NULL); 1803 1804 /* 1805 * Search for a not-loaded module by name. 1806 * 1807 * Modules may be found in the following locations: 1808 * 1809 * - preloaded (result is just the module name) - on disk (result is full path 1810 * to module) 1811 * 1812 * If the module name is qualified in any way (contains path, etc.) the we 1813 * simply return a copy of it. 1814 * 1815 * The search path can be manipulated via sysctl. Note that we use the ';' 1816 * character as a separator to be consistent with the bootloader. 1817 */ 1818 1819 static char linker_hintfile[] = "linker.hints"; 1820 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1821 1822 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path, 1823 sizeof(linker_path), "module load search path"); 1824 1825 TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1826 1827 static const char * const linker_ext_list[] = { 1828 "", 1829 ".ko", 1830 NULL 1831 }; 1832 1833 /* 1834 * Check if file actually exists either with or without extension listed in 1835 * the linker_ext_list. (probably should be generic for the rest of the 1836 * kernel) 1837 */ 1838 static char * 1839 linker_lookup_file(const char *path, int pathlen, const char *name, 1840 int namelen, struct vattr *vap) 1841 { 1842 struct nameidata nd; 1843 struct thread *td = curthread; /* XXX */ 1844 const char * const *cpp, *sep; 1845 char *result; 1846 int error, len, extlen, reclen, flags; 1847 enum vtype type; 1848 1849 extlen = 0; 1850 for (cpp = linker_ext_list; *cpp; cpp++) { 1851 len = strlen(*cpp); 1852 if (len > extlen) 1853 extlen = len; 1854 } 1855 extlen++; /* trailing '\0' */ 1856 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1857 1858 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1859 result = malloc(reclen, M_LINKER, M_WAITOK); 1860 for (cpp = linker_ext_list; *cpp; cpp++) { 1861 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1862 namelen, name, *cpp); 1863 /* 1864 * Attempt to open the file, and return the path if 1865 * we succeed and it's a regular file. 1866 */ 1867 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result); 1868 flags = FREAD; 1869 error = vn_open(&nd, &flags, 0, NULL); 1870 if (error == 0) { 1871 NDFREE(&nd, NDF_ONLY_PNBUF); 1872 type = nd.ni_vp->v_type; 1873 if (vap) 1874 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1875 VOP_UNLOCK(nd.ni_vp); 1876 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1877 if (type == VREG) 1878 return (result); 1879 } 1880 } 1881 free(result, M_LINKER); 1882 return (NULL); 1883 } 1884 1885 #define INT_ALIGN(base, ptr) ptr = \ 1886 (base) + roundup2((ptr) - (base), sizeof(int)) 1887 1888 /* 1889 * Lookup KLD which contains requested module in the "linker.hints" file. If 1890 * version specification is available, then try to find the best KLD. 1891 * Otherwise just find the latest one. 1892 */ 1893 static char * 1894 linker_hints_lookup(const char *path, int pathlen, const char *modname, 1895 int modnamelen, const struct mod_depend *verinfo) 1896 { 1897 struct thread *td = curthread; /* XXX */ 1898 struct ucred *cred = td ? td->td_ucred : NULL; 1899 struct nameidata nd; 1900 struct vattr vattr, mattr; 1901 const char *best, *sep; 1902 u_char *hints = NULL; 1903 u_char *cp, *recptr, *bufend, *result, *pathbuf; 1904 int error, ival, bestver, *intp, found, flags, clen, blen; 1905 ssize_t reclen; 1906 1907 result = NULL; 1908 bestver = found = 0; 1909 1910 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1911 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1912 strlen(sep) + 1; 1913 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1914 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1915 linker_hintfile); 1916 1917 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf); 1918 flags = FREAD; 1919 error = vn_open(&nd, &flags, 0, NULL); 1920 if (error) 1921 goto bad; 1922 NDFREE(&nd, NDF_ONLY_PNBUF); 1923 if (nd.ni_vp->v_type != VREG) 1924 goto bad; 1925 best = cp = NULL; 1926 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1927 if (error) 1928 goto bad; 1929 /* 1930 * XXX: we need to limit this number to some reasonable value 1931 */ 1932 if (vattr.va_size > LINKER_HINTS_MAX) { 1933 printf("linker.hints file too large %ld\n", (long)vattr.va_size); 1934 goto bad; 1935 } 1936 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1937 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1938 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1939 if (error) 1940 goto bad; 1941 VOP_UNLOCK(nd.ni_vp); 1942 vn_close(nd.ni_vp, FREAD, cred, td); 1943 nd.ni_vp = NULL; 1944 if (reclen != 0) { 1945 printf("can't read %zd\n", reclen); 1946 goto bad; 1947 } 1948 intp = (int *)hints; 1949 ival = *intp++; 1950 if (ival != LINKER_HINTS_VERSION) { 1951 printf("linker.hints file version mismatch %d\n", ival); 1952 goto bad; 1953 } 1954 bufend = hints + vattr.va_size; 1955 recptr = (u_char *)intp; 1956 clen = blen = 0; 1957 while (recptr < bufend && !found) { 1958 intp = (int *)recptr; 1959 reclen = *intp++; 1960 ival = *intp++; 1961 cp = (char *)intp; 1962 switch (ival) { 1963 case MDT_VERSION: 1964 clen = *cp++; 1965 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1966 break; 1967 cp += clen; 1968 INT_ALIGN(hints, cp); 1969 ival = *(int *)cp; 1970 cp += sizeof(int); 1971 clen = *cp++; 1972 if (verinfo == NULL || 1973 ival == verinfo->md_ver_preferred) { 1974 found = 1; 1975 break; 1976 } 1977 if (ival >= verinfo->md_ver_minimum && 1978 ival <= verinfo->md_ver_maximum && 1979 ival > bestver) { 1980 bestver = ival; 1981 best = cp; 1982 blen = clen; 1983 } 1984 break; 1985 default: 1986 break; 1987 } 1988 recptr += reclen + sizeof(int); 1989 } 1990 /* 1991 * Finally check if KLD is in the place 1992 */ 1993 if (found) 1994 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1995 else if (best) 1996 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1997 1998 /* 1999 * KLD is newer than hints file. What we should do now? 2000 */ 2001 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 2002 printf("warning: KLD '%s' is newer than the linker.hints" 2003 " file\n", result); 2004 bad: 2005 free(pathbuf, M_LINKER); 2006 if (hints) 2007 free(hints, M_TEMP); 2008 if (nd.ni_vp != NULL) { 2009 VOP_UNLOCK(nd.ni_vp); 2010 vn_close(nd.ni_vp, FREAD, cred, td); 2011 } 2012 /* 2013 * If nothing found or hints is absent - fallback to the old 2014 * way by using "kldname[.ko]" as module name. 2015 */ 2016 if (!found && !bestver && result == NULL) 2017 result = linker_lookup_file(path, pathlen, modname, 2018 modnamelen, NULL); 2019 return (result); 2020 } 2021 2022 /* 2023 * Lookup KLD which contains requested module in the all directories. 2024 */ 2025 static char * 2026 linker_search_module(const char *modname, int modnamelen, 2027 const struct mod_depend *verinfo) 2028 { 2029 char *cp, *ep, *result; 2030 2031 /* 2032 * traverse the linker path 2033 */ 2034 for (cp = linker_path; *cp; cp = ep + 1) { 2035 /* find the end of this component */ 2036 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 2037 result = linker_hints_lookup(cp, ep - cp, modname, 2038 modnamelen, verinfo); 2039 if (result != NULL) 2040 return (result); 2041 if (*ep == 0) 2042 break; 2043 } 2044 return (NULL); 2045 } 2046 2047 /* 2048 * Search for module in all directories listed in the linker_path. 2049 */ 2050 static char * 2051 linker_search_kld(const char *name) 2052 { 2053 char *cp, *ep, *result; 2054 int len; 2055 2056 /* qualified at all? */ 2057 if (strchr(name, '/')) 2058 return (strdup(name, M_LINKER)); 2059 2060 /* traverse the linker path */ 2061 len = strlen(name); 2062 for (ep = linker_path; *ep; ep++) { 2063 cp = ep; 2064 /* find the end of this component */ 2065 for (; *ep != 0 && *ep != ';'; ep++); 2066 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 2067 if (result != NULL) 2068 return (result); 2069 } 2070 return (NULL); 2071 } 2072 2073 static const char * 2074 linker_basename(const char *path) 2075 { 2076 const char *filename; 2077 2078 filename = strrchr(path, '/'); 2079 if (filename == NULL) 2080 return path; 2081 if (filename[1]) 2082 filename++; 2083 return (filename); 2084 } 2085 2086 #ifdef HWPMC_HOOKS 2087 /* 2088 * Inform hwpmc about the set of kernel modules currently loaded. 2089 */ 2090 void * 2091 linker_hwpmc_list_objects(void) 2092 { 2093 linker_file_t lf; 2094 struct pmckern_map_in *kobase; 2095 int i, nmappings; 2096 2097 nmappings = 0; 2098 sx_slock(&kld_sx); 2099 TAILQ_FOREACH(lf, &linker_files, link) 2100 nmappings++; 2101 2102 /* Allocate nmappings + 1 entries. */ 2103 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in), 2104 M_LINKER, M_WAITOK | M_ZERO); 2105 i = 0; 2106 TAILQ_FOREACH(lf, &linker_files, link) { 2107 /* Save the info for this linker file. */ 2108 kobase[i].pm_file = lf->filename; 2109 kobase[i].pm_address = (uintptr_t)lf->address; 2110 i++; 2111 } 2112 sx_sunlock(&kld_sx); 2113 2114 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?")); 2115 2116 /* The last entry of the malloced area comprises of all zeros. */ 2117 KASSERT(kobase[i].pm_file == NULL, 2118 ("linker_hwpmc_list_objects: last object not NULL")); 2119 2120 return ((void *)kobase); 2121 } 2122 #endif 2123 2124 /* check if root file system is not mounted */ 2125 static bool 2126 linker_root_mounted(void) 2127 { 2128 struct pwd *pwd; 2129 bool ret; 2130 2131 if (rootvnode == NULL) 2132 return (false); 2133 2134 pwd = pwd_hold(curthread); 2135 ret = pwd->pwd_rdir != NULL; 2136 pwd_drop(pwd); 2137 return (ret); 2138 } 2139 2140 /* 2141 * Find a file which contains given module and load it, if "parent" is not 2142 * NULL, register a reference to it. 2143 */ 2144 static int 2145 linker_load_module(const char *kldname, const char *modname, 2146 struct linker_file *parent, const struct mod_depend *verinfo, 2147 struct linker_file **lfpp) 2148 { 2149 linker_file_t lfdep; 2150 const char *filename; 2151 char *pathname; 2152 int error; 2153 2154 sx_assert(&kld_sx, SA_XLOCKED); 2155 if (modname == NULL) { 2156 /* 2157 * We have to load KLD 2158 */ 2159 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 2160 " is not NULL")); 2161 if (!linker_root_mounted()) 2162 return (ENXIO); 2163 pathname = linker_search_kld(kldname); 2164 } else { 2165 if (modlist_lookup2(modname, verinfo) != NULL) 2166 return (EEXIST); 2167 if (!linker_root_mounted()) 2168 return (ENXIO); 2169 if (kldname != NULL) 2170 pathname = strdup(kldname, M_LINKER); 2171 else 2172 /* 2173 * Need to find a KLD with required module 2174 */ 2175 pathname = linker_search_module(modname, 2176 strlen(modname), verinfo); 2177 } 2178 if (pathname == NULL) 2179 return (ENOENT); 2180 2181 /* 2182 * Can't load more than one file with the same basename XXX: 2183 * Actually it should be possible to have multiple KLDs with 2184 * the same basename but different path because they can 2185 * provide different versions of the same modules. 2186 */ 2187 filename = linker_basename(pathname); 2188 if (linker_find_file_by_name(filename)) 2189 error = EEXIST; 2190 else do { 2191 error = linker_load_file(pathname, &lfdep); 2192 if (error) 2193 break; 2194 if (modname && verinfo && 2195 modlist_lookup2(modname, verinfo) == NULL) { 2196 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2197 error = ENOENT; 2198 break; 2199 } 2200 if (parent) { 2201 error = linker_file_add_dependency(parent, lfdep); 2202 if (error) 2203 break; 2204 } 2205 if (lfpp) 2206 *lfpp = lfdep; 2207 } while (0); 2208 free(pathname, M_LINKER); 2209 return (error); 2210 } 2211 2212 /* 2213 * This routine is responsible for finding dependencies of userland initiated 2214 * kldload(2)'s of files. 2215 */ 2216 int 2217 linker_load_dependencies(linker_file_t lf) 2218 { 2219 linker_file_t lfdep; 2220 struct mod_metadata **start, **stop, **mdp, **nmdp; 2221 struct mod_metadata *mp, *nmp; 2222 const struct mod_depend *verinfo; 2223 modlist_t mod; 2224 const char *modname, *nmodname; 2225 int ver, error = 0; 2226 2227 /* 2228 * All files are dependent on /kernel. 2229 */ 2230 sx_assert(&kld_sx, SA_XLOCKED); 2231 if (linker_kernel_file) { 2232 linker_kernel_file->refs++; 2233 error = linker_file_add_dependency(lf, linker_kernel_file); 2234 if (error) 2235 return (error); 2236 } 2237 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2238 NULL) != 0) 2239 return (0); 2240 for (mdp = start; mdp < stop; mdp++) { 2241 mp = *mdp; 2242 if (mp->md_type != MDT_VERSION) 2243 continue; 2244 modname = mp->md_cval; 2245 ver = ((const struct mod_version *)mp->md_data)->mv_version; 2246 mod = modlist_lookup(modname, ver); 2247 if (mod != NULL) { 2248 printf("interface %s.%d already present in the KLD" 2249 " '%s'!\n", modname, ver, 2250 mod->container->filename); 2251 return (EEXIST); 2252 } 2253 } 2254 2255 for (mdp = start; mdp < stop; mdp++) { 2256 mp = *mdp; 2257 if (mp->md_type != MDT_DEPEND) 2258 continue; 2259 modname = mp->md_cval; 2260 verinfo = mp->md_data; 2261 nmodname = NULL; 2262 for (nmdp = start; nmdp < stop; nmdp++) { 2263 nmp = *nmdp; 2264 if (nmp->md_type != MDT_VERSION) 2265 continue; 2266 nmodname = nmp->md_cval; 2267 if (strcmp(modname, nmodname) == 0) 2268 break; 2269 } 2270 if (nmdp < stop)/* early exit, it's a self reference */ 2271 continue; 2272 mod = modlist_lookup2(modname, verinfo); 2273 if (mod) { /* woohoo, it's loaded already */ 2274 lfdep = mod->container; 2275 lfdep->refs++; 2276 error = linker_file_add_dependency(lf, lfdep); 2277 if (error) 2278 break; 2279 continue; 2280 } 2281 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2282 if (error) { 2283 printf("KLD %s: depends on %s - not available or" 2284 " version mismatch\n", lf->filename, modname); 2285 break; 2286 } 2287 } 2288 2289 if (error) 2290 return (error); 2291 linker_addmodules(lf, start, stop, 0); 2292 return (error); 2293 } 2294 2295 static int 2296 sysctl_kern_function_list_iterate(const char *name, void *opaque) 2297 { 2298 struct sysctl_req *req; 2299 2300 req = opaque; 2301 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2302 } 2303 2304 /* 2305 * Export a nul-separated, double-nul-terminated list of all function names 2306 * in the kernel. 2307 */ 2308 static int 2309 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2310 { 2311 linker_file_t lf; 2312 int error; 2313 2314 #ifdef MAC 2315 error = mac_kld_check_stat(req->td->td_ucred); 2316 if (error) 2317 return (error); 2318 #endif 2319 error = sysctl_wire_old_buffer(req, 0); 2320 if (error != 0) 2321 return (error); 2322 sx_xlock(&kld_sx); 2323 TAILQ_FOREACH(lf, &linker_files, link) { 2324 error = LINKER_EACH_FUNCTION_NAME(lf, 2325 sysctl_kern_function_list_iterate, req); 2326 if (error) { 2327 sx_xunlock(&kld_sx); 2328 return (error); 2329 } 2330 } 2331 sx_xunlock(&kld_sx); 2332 return (SYSCTL_OUT(req, "", 1)); 2333 } 2334 2335 SYSCTL_PROC(_kern, OID_AUTO, function_list, 2336 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 2337 sysctl_kern_function_list, "", 2338 "kernel function list"); 2339