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