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 mtx_lock(&Giant); 297 for (oidp = start; oidp < stop; oidp++) 298 sysctl_register_oid(*oidp); 299 mtx_unlock(&Giant); 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 mtx_lock(&Giant); 314 for (oidp = start; oidp < stop; oidp++) 315 sysctl_unregister_oid(*oidp); 316 mtx_unlock(&Giant); 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 * Format not recognized or otherwise unloadable. 430 * When loading a module that is statically built into 431 * the kernel EEXIST percolates back up as the return 432 * value. Preserve this so that apps like sysinstall 433 * can recognize this special case and not post bogus 434 * dialog boxes. 435 */ 436 if (error != EEXIST) 437 error = ENOEXEC; 438 } else 439 error = ENOENT; /* Nothing found */ 440 return (error); 441 } 442 443 int 444 linker_reference_module(const char *modname, struct mod_depend *verinfo, 445 linker_file_t *result) 446 { 447 modlist_t mod; 448 int error; 449 450 KLD_LOCK(); 451 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) { 452 *result = mod->container; 453 (*result)->refs++; 454 KLD_UNLOCK(); 455 return (0); 456 } 457 458 error = linker_load_module(NULL, modname, NULL, verinfo, result); 459 KLD_UNLOCK(); 460 return (error); 461 } 462 463 int 464 linker_release_module(const char *modname, struct mod_depend *verinfo, 465 linker_file_t lf) 466 { 467 modlist_t mod; 468 int error; 469 470 KLD_LOCK(); 471 if (lf == NULL) { 472 KASSERT(modname != NULL, 473 ("linker_release_module: no file or name")); 474 mod = modlist_lookup2(modname, verinfo); 475 if (mod == NULL) { 476 KLD_UNLOCK(); 477 return (ESRCH); 478 } 479 lf = mod->container; 480 } else 481 KASSERT(modname == NULL && verinfo == NULL, 482 ("linker_release_module: both file and name")); 483 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL); 484 KLD_UNLOCK(); 485 return (error); 486 } 487 488 static linker_file_t 489 linker_find_file_by_name(const char *filename) 490 { 491 linker_file_t lf; 492 char *koname; 493 494 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 495 sprintf(koname, "%s.ko", filename); 496 497 KLD_LOCK_ASSERT(); 498 TAILQ_FOREACH(lf, &linker_files, link) { 499 if (strcmp(lf->filename, koname) == 0) 500 break; 501 if (strcmp(lf->filename, filename) == 0) 502 break; 503 } 504 free(koname, M_LINKER); 505 return (lf); 506 } 507 508 static linker_file_t 509 linker_find_file_by_id(int fileid) 510 { 511 linker_file_t lf; 512 513 KLD_LOCK_ASSERT(); 514 TAILQ_FOREACH(lf, &linker_files, link) 515 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED) 516 break; 517 return (lf); 518 } 519 520 int 521 linker_file_foreach(linker_predicate_t *predicate, void *context) 522 { 523 linker_file_t lf; 524 int retval = 0; 525 526 KLD_LOCK(); 527 TAILQ_FOREACH(lf, &linker_files, link) { 528 retval = predicate(lf, context); 529 if (retval != 0) 530 break; 531 } 532 KLD_UNLOCK(); 533 return (retval); 534 } 535 536 linker_file_t 537 linker_make_file(const char *pathname, linker_class_t lc) 538 { 539 linker_file_t lf; 540 const char *filename; 541 542 KLD_LOCK_ASSERT(); 543 filename = linker_basename(pathname); 544 545 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname)); 546 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK); 547 if (lf == NULL) 548 return (NULL); 549 lf->refs = 1; 550 lf->userrefs = 0; 551 lf->flags = 0; 552 lf->filename = linker_strdup(filename); 553 lf->pathname = linker_strdup(pathname); 554 LINKER_GET_NEXT_FILE_ID(lf->id); 555 lf->ndeps = 0; 556 lf->deps = NULL; 557 lf->loadcnt = ++loadcnt; 558 lf->sdt_probes = NULL; 559 lf->sdt_nprobes = 0; 560 STAILQ_INIT(&lf->common); 561 TAILQ_INIT(&lf->modules); 562 TAILQ_INSERT_TAIL(&linker_files, lf, link); 563 return (lf); 564 } 565 566 int 567 linker_file_unload(linker_file_t file, int flags) 568 { 569 module_t mod, next; 570 modlist_t ml, nextml; 571 struct common_symbol *cp; 572 int error, i; 573 574 /* Refuse to unload modules if securelevel raised. */ 575 if (securelevel > 0) 576 return (EPERM); 577 578 KLD_LOCK_ASSERT(); 579 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 580 581 /* Easy case of just dropping a reference. */ 582 if (file->refs > 1) { 583 file->refs--; 584 return (0); 585 } 586 587 KLD_DPF(FILE, ("linker_file_unload: file is unloading," 588 " informing modules\n")); 589 590 /* 591 * Quiesce all the modules to give them a chance to veto the unload. 592 */ 593 MOD_SLOCK; 594 for (mod = TAILQ_FIRST(&file->modules); mod; 595 mod = module_getfnext(mod)) { 596 597 error = module_quiesce(mod); 598 if (error != 0 && flags != LINKER_UNLOAD_FORCE) { 599 KLD_DPF(FILE, ("linker_file_unload: module %s" 600 " vetoed unload\n", module_getname(mod))); 601 /* 602 * XXX: Do we need to tell all the quiesced modules 603 * that they can resume work now via a new module 604 * event? 605 */ 606 MOD_SUNLOCK; 607 return (error); 608 } 609 } 610 MOD_SUNLOCK; 611 612 /* 613 * Inform any modules associated with this file that they are 614 * being be unloaded. 615 */ 616 MOD_XLOCK; 617 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 618 next = module_getfnext(mod); 619 MOD_XUNLOCK; 620 621 /* 622 * Give the module a chance to veto the unload. 623 */ 624 if ((error = module_unload(mod)) != 0) { 625 KLD_DPF(FILE, ("linker_file_unload: module %s" 626 " failed unload\n", mod)); 627 return (error); 628 } 629 MOD_XLOCK; 630 module_release(mod); 631 } 632 MOD_XUNLOCK; 633 634 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) { 635 if (ml->container == file) { 636 TAILQ_REMOVE(&found_modules, ml, link); 637 free(ml, M_LINKER); 638 } 639 } 640 641 /* 642 * Don't try to run SYSUNINITs if we are unloaded due to a 643 * link error. 644 */ 645 if (file->flags & LINKER_FILE_LINKED) { 646 linker_file_sysuninit(file); 647 linker_file_unregister_sysctls(file); 648 } 649 TAILQ_REMOVE(&linker_files, file, link); 650 651 if (file->deps) { 652 for (i = 0; i < file->ndeps; i++) 653 linker_file_unload(file->deps[i], flags); 654 free(file->deps, M_LINKER); 655 file->deps = NULL; 656 } 657 while ((cp = STAILQ_FIRST(&file->common)) != NULL) { 658 STAILQ_REMOVE_HEAD(&file->common, link); 659 free(cp, M_LINKER); 660 } 661 662 LINKER_UNLOAD(file); 663 if (file->filename) { 664 free(file->filename, M_LINKER); 665 file->filename = NULL; 666 } 667 if (file->pathname) { 668 free(file->pathname, M_LINKER); 669 file->pathname = NULL; 670 } 671 kobj_delete((kobj_t) file, M_LINKER); 672 return (0); 673 } 674 675 int 676 linker_ctf_get(linker_file_t file, linker_ctf_t *lc) 677 { 678 return (LINKER_CTF_GET(file, lc)); 679 } 680 681 static int 682 linker_file_add_dependency(linker_file_t file, linker_file_t dep) 683 { 684 linker_file_t *newdeps; 685 686 KLD_LOCK_ASSERT(); 687 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *), 688 M_LINKER, M_WAITOK | M_ZERO); 689 if (newdeps == NULL) 690 return (ENOMEM); 691 692 if (file->deps) { 693 bcopy(file->deps, newdeps, 694 file->ndeps * sizeof(linker_file_t *)); 695 free(file->deps, M_LINKER); 696 } 697 file->deps = newdeps; 698 file->deps[file->ndeps] = dep; 699 file->ndeps++; 700 return (0); 701 } 702 703 /* 704 * Locate a linker set and its contents. This is a helper function to avoid 705 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **. 706 * This function is used in this file so we can avoid having lots of (void **) 707 * casts. 708 */ 709 int 710 linker_file_lookup_set(linker_file_t file, const char *name, 711 void *firstp, void *lastp, int *countp) 712 { 713 int error, locked; 714 715 locked = KLD_LOCKED(); 716 if (!locked) 717 KLD_LOCK(); 718 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp); 719 if (!locked) 720 KLD_UNLOCK(); 721 return (error); 722 } 723 724 /* 725 * List all functions in a file. 726 */ 727 int 728 linker_file_function_listall(linker_file_t lf, 729 linker_function_nameval_callback_t callback_func, void *arg) 730 { 731 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg)); 732 } 733 734 caddr_t 735 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps) 736 { 737 caddr_t sym; 738 int locked; 739 740 locked = KLD_LOCKED(); 741 if (!locked) 742 KLD_LOCK(); 743 sym = linker_file_lookup_symbol_internal(file, name, deps); 744 if (!locked) 745 KLD_UNLOCK(); 746 return (sym); 747 } 748 749 static caddr_t 750 linker_file_lookup_symbol_internal(linker_file_t file, const char *name, 751 int deps) 752 { 753 c_linker_sym_t sym; 754 linker_symval_t symval; 755 caddr_t address; 756 size_t common_size = 0; 757 int i; 758 759 KLD_LOCK_ASSERT(); 760 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 761 file, name, deps)); 762 763 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 764 LINKER_SYMBOL_VALUES(file, sym, &symval); 765 if (symval.value == 0) 766 /* 767 * For commons, first look them up in the 768 * dependencies and only allocate space if not found 769 * there. 770 */ 771 common_size = symval.size; 772 else { 773 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol" 774 ".value=%p\n", symval.value)); 775 return (symval.value); 776 } 777 } 778 if (deps) { 779 for (i = 0; i < file->ndeps; i++) { 780 address = linker_file_lookup_symbol_internal( 781 file->deps[i], name, 0); 782 if (address) { 783 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 784 " deps value=%p\n", address)); 785 return (address); 786 } 787 } 788 } 789 if (common_size > 0) { 790 /* 791 * This is a common symbol which was not found in the 792 * dependencies. We maintain a simple common symbol table in 793 * the file object. 794 */ 795 struct common_symbol *cp; 796 797 STAILQ_FOREACH(cp, &file->common, link) { 798 if (strcmp(cp->name, name) == 0) { 799 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 800 " old common value=%p\n", cp->address)); 801 return (cp->address); 802 } 803 } 804 /* 805 * Round the symbol size up to align. 806 */ 807 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 808 cp = malloc(sizeof(struct common_symbol) 809 + common_size + strlen(name) + 1, M_LINKER, 810 M_WAITOK | M_ZERO); 811 cp->address = (caddr_t)(cp + 1); 812 cp->name = cp->address + common_size; 813 strcpy(cp->name, name); 814 bzero(cp->address, common_size); 815 STAILQ_INSERT_TAIL(&file->common, cp, link); 816 817 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common" 818 " value=%p\n", cp->address)); 819 return (cp->address); 820 } 821 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 822 return (0); 823 } 824 825 /* 826 * Both DDB and stack(9) rely on the kernel linker to provide forward and 827 * backward lookup of symbols. However, DDB and sometimes stack(9) need to 828 * do this in a lockfree manner. We provide a set of internal helper 829 * routines to perform these operations without locks, and then wrappers that 830 * optionally lock. 831 * 832 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB. 833 */ 834 #ifdef DDB 835 static int 836 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym) 837 { 838 linker_file_t lf; 839 840 TAILQ_FOREACH(lf, &linker_files, link) { 841 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 842 return (0); 843 } 844 return (ENOENT); 845 } 846 #endif 847 848 static int 849 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 850 { 851 linker_file_t lf; 852 c_linker_sym_t best, es; 853 u_long diff, bestdiff, off; 854 855 best = 0; 856 off = (uintptr_t)value; 857 bestdiff = off; 858 TAILQ_FOREACH(lf, &linker_files, link) { 859 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 860 continue; 861 if (es != 0 && diff < bestdiff) { 862 best = es; 863 bestdiff = diff; 864 } 865 if (bestdiff == 0) 866 break; 867 } 868 if (best) { 869 *sym = best; 870 *diffp = bestdiff; 871 return (0); 872 } else { 873 *sym = 0; 874 *diffp = off; 875 return (ENOENT); 876 } 877 } 878 879 static int 880 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 881 { 882 linker_file_t lf; 883 884 TAILQ_FOREACH(lf, &linker_files, link) { 885 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 886 return (0); 887 } 888 return (ENOENT); 889 } 890 891 static int 892 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen, 893 long *offset) 894 { 895 linker_symval_t symval; 896 c_linker_sym_t sym; 897 int error; 898 899 *offset = 0; 900 error = linker_debug_search_symbol(value, &sym, offset); 901 if (error) 902 return (error); 903 error = linker_debug_symbol_values(sym, &symval); 904 if (error) 905 return (error); 906 strlcpy(buf, symval.name, buflen); 907 return (0); 908 } 909 910 #ifdef DDB 911 /* 912 * DDB Helpers. DDB has to look across multiple files with their own symbol 913 * tables and string tables. 914 * 915 * Note that we do not obey list locking protocols here. We really don't need 916 * DDB to hang because somebody's got the lock held. We'll take the chance 917 * that the files list is inconsistant instead. 918 */ 919 int 920 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 921 { 922 923 return (linker_debug_lookup(symstr, sym)); 924 } 925 926 int 927 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 928 { 929 930 return (linker_debug_search_symbol(value, sym, diffp)); 931 } 932 933 int 934 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 935 { 936 937 return (linker_debug_symbol_values(sym, symval)); 938 } 939 940 int 941 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, 942 long *offset) 943 { 944 945 return (linker_debug_search_symbol_name(value, buf, buflen, offset)); 946 } 947 #endif 948 949 /* 950 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do 951 * obey locking protocols, and offer a significantly less complex interface. 952 */ 953 int 954 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 955 long *offset) 956 { 957 int error; 958 959 KLD_LOCK(); 960 error = linker_debug_search_symbol_name(value, buf, buflen, offset); 961 KLD_UNLOCK(); 962 return (error); 963 } 964 965 /* 966 * Syscalls. 967 */ 968 int 969 kern_kldload(struct thread *td, const char *file, int *fileid) 970 { 971 #ifdef HWPMC_HOOKS 972 struct pmckern_map_in pkm; 973 #endif 974 const char *kldname, *modname; 975 linker_file_t lf; 976 int error; 977 978 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 979 return (error); 980 981 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 982 return (error); 983 984 /* 985 * If file does not contain a qualified name or any dot in it 986 * (kldname.ko, or kldname.ver.ko) treat it as an interface 987 * name. 988 */ 989 if (index(file, '/') || index(file, '.')) { 990 kldname = file; 991 modname = NULL; 992 } else { 993 kldname = NULL; 994 modname = file; 995 } 996 997 KLD_LOCK(); 998 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 999 if (error) 1000 goto unlock; 1001 #ifdef HWPMC_HOOKS 1002 pkm.pm_file = lf->filename; 1003 pkm.pm_address = (uintptr_t) lf->address; 1004 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm); 1005 #endif 1006 lf->userrefs++; 1007 if (fileid != NULL) 1008 *fileid = lf->id; 1009 unlock: 1010 KLD_UNLOCK(); 1011 return (error); 1012 } 1013 1014 int 1015 kldload(struct thread *td, struct kldload_args *uap) 1016 { 1017 char *pathname = NULL; 1018 int error, fileid; 1019 1020 td->td_retval[0] = -1; 1021 1022 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1023 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1024 if (error == 0) { 1025 error = kern_kldload(td, pathname, &fileid); 1026 if (error == 0) 1027 td->td_retval[0] = fileid; 1028 } 1029 free(pathname, M_TEMP); 1030 return (error); 1031 } 1032 1033 int 1034 kern_kldunload(struct thread *td, int fileid, int flags) 1035 { 1036 #ifdef HWPMC_HOOKS 1037 struct pmckern_map_out pkm; 1038 #endif 1039 linker_file_t lf; 1040 int error = 0; 1041 1042 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1043 return (error); 1044 1045 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1046 return (error); 1047 1048 KLD_LOCK(); 1049 lf = linker_find_file_by_id(fileid); 1050 if (lf) { 1051 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1052 1053 /* Check if there are DTrace probes enabled on this file. */ 1054 if (lf->nenabled > 0) { 1055 printf("kldunload: attempt to unload file that has" 1056 " DTrace probes enabled\n"); 1057 error = EBUSY; 1058 } else if (lf->userrefs == 0) { 1059 /* 1060 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1061 */ 1062 printf("kldunload: attempt to unload file that was" 1063 " loaded by the kernel\n"); 1064 error = EBUSY; 1065 } else { 1066 #ifdef HWPMC_HOOKS 1067 /* Save data needed by hwpmc(4) before unloading. */ 1068 pkm.pm_address = (uintptr_t) lf->address; 1069 pkm.pm_size = lf->size; 1070 #endif 1071 lf->userrefs--; 1072 error = linker_file_unload(lf, flags); 1073 if (error) 1074 lf->userrefs++; 1075 } 1076 } else 1077 error = ENOENT; 1078 1079 #ifdef HWPMC_HOOKS 1080 if (error == 0) 1081 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm); 1082 #endif 1083 KLD_UNLOCK(); 1084 return (error); 1085 } 1086 1087 int 1088 kldunload(struct thread *td, struct kldunload_args *uap) 1089 { 1090 1091 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1092 } 1093 1094 int 1095 kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1096 { 1097 1098 if (uap->flags != LINKER_UNLOAD_NORMAL && 1099 uap->flags != LINKER_UNLOAD_FORCE) 1100 return (EINVAL); 1101 return (kern_kldunload(td, uap->fileid, uap->flags)); 1102 } 1103 1104 int 1105 kldfind(struct thread *td, struct kldfind_args *uap) 1106 { 1107 char *pathname; 1108 const char *filename; 1109 linker_file_t lf; 1110 int error; 1111 1112 #ifdef MAC 1113 error = mac_kld_check_stat(td->td_ucred); 1114 if (error) 1115 return (error); 1116 #endif 1117 1118 td->td_retval[0] = -1; 1119 1120 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1121 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1122 goto out; 1123 1124 filename = linker_basename(pathname); 1125 KLD_LOCK(); 1126 lf = linker_find_file_by_name(filename); 1127 if (lf) 1128 td->td_retval[0] = lf->id; 1129 else 1130 error = ENOENT; 1131 KLD_UNLOCK(); 1132 out: 1133 free(pathname, M_TEMP); 1134 return (error); 1135 } 1136 1137 int 1138 kldnext(struct thread *td, struct kldnext_args *uap) 1139 { 1140 linker_file_t lf; 1141 int error = 0; 1142 1143 #ifdef MAC 1144 error = mac_kld_check_stat(td->td_ucred); 1145 if (error) 1146 return (error); 1147 #endif 1148 1149 KLD_LOCK(); 1150 if (uap->fileid == 0) 1151 lf = TAILQ_FIRST(&linker_files); 1152 else { 1153 lf = linker_find_file_by_id(uap->fileid); 1154 if (lf == NULL) { 1155 error = ENOENT; 1156 goto out; 1157 } 1158 lf = TAILQ_NEXT(lf, link); 1159 } 1160 1161 /* Skip partially loaded files. */ 1162 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1163 lf = TAILQ_NEXT(lf, link); 1164 1165 if (lf) 1166 td->td_retval[0] = lf->id; 1167 else 1168 td->td_retval[0] = 0; 1169 out: 1170 KLD_UNLOCK(); 1171 return (error); 1172 } 1173 1174 int 1175 kldstat(struct thread *td, struct kldstat_args *uap) 1176 { 1177 struct kld_file_stat stat; 1178 linker_file_t lf; 1179 int error, namelen, version, version_num; 1180 1181 /* 1182 * Check the version of the user's structure. 1183 */ 1184 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0) 1185 return (error); 1186 if (version == sizeof(struct kld_file_stat_1)) 1187 version_num = 1; 1188 else if (version == sizeof(struct kld_file_stat)) 1189 version_num = 2; 1190 else 1191 return (EINVAL); 1192 1193 #ifdef MAC 1194 error = mac_kld_check_stat(td->td_ucred); 1195 if (error) 1196 return (error); 1197 #endif 1198 1199 KLD_LOCK(); 1200 lf = linker_find_file_by_id(uap->fileid); 1201 if (lf == NULL) { 1202 KLD_UNLOCK(); 1203 return (ENOENT); 1204 } 1205 1206 /* Version 1 fields: */ 1207 namelen = strlen(lf->filename) + 1; 1208 if (namelen > MAXPATHLEN) 1209 namelen = MAXPATHLEN; 1210 bcopy(lf->filename, &stat.name[0], namelen); 1211 stat.refs = lf->refs; 1212 stat.id = lf->id; 1213 stat.address = lf->address; 1214 stat.size = lf->size; 1215 if (version_num > 1) { 1216 /* Version 2 fields: */ 1217 namelen = strlen(lf->pathname) + 1; 1218 if (namelen > MAXPATHLEN) 1219 namelen = MAXPATHLEN; 1220 bcopy(lf->pathname, &stat.pathname[0], namelen); 1221 } 1222 KLD_UNLOCK(); 1223 1224 td->td_retval[0] = 0; 1225 1226 return (copyout(&stat, uap->stat, version)); 1227 } 1228 1229 int 1230 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1231 { 1232 linker_file_t lf; 1233 module_t mp; 1234 int error = 0; 1235 1236 #ifdef MAC 1237 error = mac_kld_check_stat(td->td_ucred); 1238 if (error) 1239 return (error); 1240 #endif 1241 1242 KLD_LOCK(); 1243 lf = linker_find_file_by_id(uap->fileid); 1244 if (lf) { 1245 MOD_SLOCK; 1246 mp = TAILQ_FIRST(&lf->modules); 1247 if (mp != NULL) 1248 td->td_retval[0] = module_getid(mp); 1249 else 1250 td->td_retval[0] = 0; 1251 MOD_SUNLOCK; 1252 } else 1253 error = ENOENT; 1254 KLD_UNLOCK(); 1255 return (error); 1256 } 1257 1258 int 1259 kldsym(struct thread *td, struct kldsym_args *uap) 1260 { 1261 char *symstr = NULL; 1262 c_linker_sym_t sym; 1263 linker_symval_t symval; 1264 linker_file_t lf; 1265 struct kld_sym_lookup lookup; 1266 int error = 0; 1267 1268 #ifdef MAC 1269 error = mac_kld_check_stat(td->td_ucred); 1270 if (error) 1271 return (error); 1272 #endif 1273 1274 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1275 return (error); 1276 if (lookup.version != sizeof(lookup) || 1277 uap->cmd != KLDSYM_LOOKUP) 1278 return (EINVAL); 1279 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1280 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1281 goto out; 1282 KLD_LOCK(); 1283 if (uap->fileid != 0) { 1284 lf = linker_find_file_by_id(uap->fileid); 1285 if (lf == NULL) 1286 error = ENOENT; 1287 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1288 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1289 lookup.symvalue = (uintptr_t) symval.value; 1290 lookup.symsize = symval.size; 1291 error = copyout(&lookup, uap->data, sizeof(lookup)); 1292 } else 1293 error = ENOENT; 1294 } else { 1295 TAILQ_FOREACH(lf, &linker_files, link) { 1296 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1297 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1298 lookup.symvalue = (uintptr_t)symval.value; 1299 lookup.symsize = symval.size; 1300 error = copyout(&lookup, uap->data, 1301 sizeof(lookup)); 1302 break; 1303 } 1304 } 1305 #ifndef VIMAGE_GLOBALS 1306 /* 1307 * If the symbol is not found in global namespace, 1308 * try to look it up in the current vimage namespace. 1309 */ 1310 if (lf == NULL) { 1311 CURVNET_SET(TD_TO_VNET(td)); 1312 error = vi_symlookup(&lookup, symstr); 1313 CURVNET_RESTORE(); 1314 if (error == 0) 1315 error = copyout(&lookup, uap->data, 1316 sizeof(lookup)); 1317 } 1318 #else 1319 if (lf == NULL) 1320 error = ENOENT; 1321 #endif 1322 } 1323 KLD_UNLOCK(); 1324 out: 1325 free(symstr, M_TEMP); 1326 return (error); 1327 } 1328 1329 /* 1330 * Preloaded module support 1331 */ 1332 1333 static modlist_t 1334 modlist_lookup(const char *name, int ver) 1335 { 1336 modlist_t mod; 1337 1338 TAILQ_FOREACH(mod, &found_modules, link) { 1339 if (strcmp(mod->name, name) == 0 && 1340 (ver == 0 || mod->version == ver)) 1341 return (mod); 1342 } 1343 return (NULL); 1344 } 1345 1346 static modlist_t 1347 modlist_lookup2(const char *name, struct mod_depend *verinfo) 1348 { 1349 modlist_t mod, bestmod; 1350 int ver; 1351 1352 if (verinfo == NULL) 1353 return (modlist_lookup(name, 0)); 1354 bestmod = NULL; 1355 TAILQ_FOREACH(mod, &found_modules, link) { 1356 if (strcmp(mod->name, name) != 0) 1357 continue; 1358 ver = mod->version; 1359 if (ver == verinfo->md_ver_preferred) 1360 return (mod); 1361 if (ver >= verinfo->md_ver_minimum && 1362 ver <= verinfo->md_ver_maximum && 1363 (bestmod == NULL || ver > bestmod->version)) 1364 bestmod = mod; 1365 } 1366 return (bestmod); 1367 } 1368 1369 static modlist_t 1370 modlist_newmodule(const char *modname, int version, linker_file_t container) 1371 { 1372 modlist_t mod; 1373 1374 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1375 if (mod == NULL) 1376 panic("no memory for module list"); 1377 mod->container = container; 1378 mod->name = modname; 1379 mod->version = version; 1380 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1381 return (mod); 1382 } 1383 1384 static void 1385 linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1386 struct mod_metadata **stop, int preload) 1387 { 1388 struct mod_metadata *mp, **mdp; 1389 const char *modname; 1390 int ver; 1391 1392 for (mdp = start; mdp < stop; mdp++) { 1393 mp = *mdp; 1394 if (mp->md_type != MDT_VERSION) 1395 continue; 1396 modname = mp->md_cval; 1397 ver = ((struct mod_version *)mp->md_data)->mv_version; 1398 if (modlist_lookup(modname, ver) != NULL) { 1399 printf("module %s already present!\n", modname); 1400 /* XXX what can we do? this is a build error. :-( */ 1401 continue; 1402 } 1403 modlist_newmodule(modname, ver, lf); 1404 } 1405 } 1406 1407 static void 1408 linker_preload(void *arg) 1409 { 1410 caddr_t modptr; 1411 const char *modname, *nmodname; 1412 char *modtype; 1413 linker_file_t lf, nlf; 1414 linker_class_t lc; 1415 int error; 1416 linker_file_list_t loaded_files; 1417 linker_file_list_t depended_files; 1418 struct mod_metadata *mp, *nmp; 1419 struct mod_metadata **start, **stop, **mdp, **nmdp; 1420 struct mod_depend *verinfo; 1421 int nver; 1422 int resolves; 1423 modlist_t mod; 1424 struct sysinit **si_start, **si_stop; 1425 1426 TAILQ_INIT(&loaded_files); 1427 TAILQ_INIT(&depended_files); 1428 TAILQ_INIT(&found_modules); 1429 error = 0; 1430 1431 modptr = NULL; 1432 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1433 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1434 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1435 if (modname == NULL) { 1436 printf("Preloaded module at %p does not have a" 1437 " name!\n", modptr); 1438 continue; 1439 } 1440 if (modtype == NULL) { 1441 printf("Preloaded module at %p does not have a type!\n", 1442 modptr); 1443 continue; 1444 } 1445 if (bootverbose) 1446 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1447 modptr); 1448 lf = NULL; 1449 TAILQ_FOREACH(lc, &classes, link) { 1450 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1451 if (!error) 1452 break; 1453 lf = NULL; 1454 } 1455 if (lf) 1456 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1457 } 1458 1459 /* 1460 * First get a list of stuff in the kernel. 1461 */ 1462 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1463 &stop, NULL) == 0) 1464 linker_addmodules(linker_kernel_file, start, stop, 1); 1465 1466 /* 1467 * This is a once-off kinky bubble sort to resolve relocation 1468 * dependency requirements. 1469 */ 1470 restart: 1471 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1472 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1473 &stop, NULL); 1474 /* 1475 * First, look to see if we would successfully link with this 1476 * stuff. 1477 */ 1478 resolves = 1; /* unless we know otherwise */ 1479 if (!error) { 1480 for (mdp = start; mdp < stop; mdp++) { 1481 mp = *mdp; 1482 if (mp->md_type != MDT_DEPEND) 1483 continue; 1484 modname = mp->md_cval; 1485 verinfo = mp->md_data; 1486 for (nmdp = start; nmdp < stop; nmdp++) { 1487 nmp = *nmdp; 1488 if (nmp->md_type != MDT_VERSION) 1489 continue; 1490 nmodname = nmp->md_cval; 1491 if (strcmp(modname, nmodname) == 0) 1492 break; 1493 } 1494 if (nmdp < stop) /* it's a self reference */ 1495 continue; 1496 1497 /* 1498 * ok, the module isn't here yet, we 1499 * are not finished 1500 */ 1501 if (modlist_lookup2(modname, verinfo) == NULL) 1502 resolves = 0; 1503 } 1504 } 1505 /* 1506 * OK, if we found our modules, we can link. So, "provide" 1507 * the modules inside and add it to the end of the link order 1508 * list. 1509 */ 1510 if (resolves) { 1511 if (!error) { 1512 for (mdp = start; mdp < stop; mdp++) { 1513 mp = *mdp; 1514 if (mp->md_type != MDT_VERSION) 1515 continue; 1516 modname = mp->md_cval; 1517 nver = ((struct mod_version *) 1518 mp->md_data)->mv_version; 1519 if (modlist_lookup(modname, 1520 nver) != NULL) { 1521 printf("module %s already" 1522 " present!\n", modname); 1523 TAILQ_REMOVE(&loaded_files, 1524 lf, loaded); 1525 linker_file_unload(lf, 1526 LINKER_UNLOAD_FORCE); 1527 /* we changed tailq next ptr */ 1528 goto restart; 1529 } 1530 modlist_newmodule(modname, nver, lf); 1531 } 1532 } 1533 TAILQ_REMOVE(&loaded_files, lf, loaded); 1534 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1535 /* 1536 * Since we provided modules, we need to restart the 1537 * sort so that the previous files that depend on us 1538 * have a chance. Also, we've busted the tailq next 1539 * pointer with the REMOVE. 1540 */ 1541 goto restart; 1542 } 1543 } 1544 1545 /* 1546 * At this point, we check to see what could not be resolved.. 1547 */ 1548 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1549 TAILQ_REMOVE(&loaded_files, lf, loaded); 1550 printf("KLD file %s is missing dependencies\n", lf->filename); 1551 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1552 } 1553 1554 /* 1555 * We made it. Finish off the linking in the order we determined. 1556 */ 1557 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1558 if (linker_kernel_file) { 1559 linker_kernel_file->refs++; 1560 error = linker_file_add_dependency(lf, 1561 linker_kernel_file); 1562 if (error) 1563 panic("cannot add dependency"); 1564 } 1565 lf->userrefs++; /* so we can (try to) kldunload it */ 1566 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1567 &stop, NULL); 1568 if (!error) { 1569 for (mdp = start; mdp < stop; mdp++) { 1570 mp = *mdp; 1571 if (mp->md_type != MDT_DEPEND) 1572 continue; 1573 modname = mp->md_cval; 1574 verinfo = mp->md_data; 1575 mod = modlist_lookup2(modname, verinfo); 1576 /* Don't count self-dependencies */ 1577 if (lf == mod->container) 1578 continue; 1579 mod->container->refs++; 1580 error = linker_file_add_dependency(lf, 1581 mod->container); 1582 if (error) 1583 panic("cannot add dependency"); 1584 } 1585 } 1586 /* 1587 * Now do relocation etc using the symbol search paths 1588 * established by the dependencies 1589 */ 1590 error = LINKER_LINK_PRELOAD_FINISH(lf); 1591 if (error) { 1592 TAILQ_REMOVE(&depended_files, lf, loaded); 1593 printf("KLD file %s - could not finalize loading\n", 1594 lf->filename); 1595 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1596 continue; 1597 } 1598 linker_file_register_modules(lf); 1599 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1600 &si_stop, NULL) == 0) 1601 sysinit_add(si_start, si_stop); 1602 linker_file_register_sysctls(lf); 1603 lf->flags |= LINKER_FILE_LINKED; 1604 } 1605 /* woohoo! we made it! */ 1606 } 1607 1608 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1609 1610 /* 1611 * Search for a not-loaded module by name. 1612 * 1613 * Modules may be found in the following locations: 1614 * 1615 * - preloaded (result is just the module name) - on disk (result is full path 1616 * to module) 1617 * 1618 * If the module name is qualified in any way (contains path, etc.) the we 1619 * simply return a copy of it. 1620 * 1621 * The search path can be manipulated via sysctl. Note that we use the ';' 1622 * character as a separator to be consistent with the bootloader. 1623 */ 1624 1625 static char linker_hintfile[] = "linker.hints"; 1626 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1627 1628 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1629 sizeof(linker_path), "module load search path"); 1630 1631 TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1632 1633 static char *linker_ext_list[] = { 1634 "", 1635 ".ko", 1636 NULL 1637 }; 1638 1639 /* 1640 * Check if file actually exists either with or without extension listed in 1641 * the linker_ext_list. (probably should be generic for the rest of the 1642 * kernel) 1643 */ 1644 static char * 1645 linker_lookup_file(const char *path, int pathlen, const char *name, 1646 int namelen, struct vattr *vap) 1647 { 1648 struct nameidata nd; 1649 struct thread *td = curthread; /* XXX */ 1650 char *result, **cpp, *sep; 1651 int error, len, extlen, reclen, flags, vfslocked; 1652 enum vtype type; 1653 1654 extlen = 0; 1655 for (cpp = linker_ext_list; *cpp; cpp++) { 1656 len = strlen(*cpp); 1657 if (len > extlen) 1658 extlen = len; 1659 } 1660 extlen++; /* trailing '\0' */ 1661 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1662 1663 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1664 result = malloc(reclen, M_LINKER, M_WAITOK); 1665 for (cpp = linker_ext_list; *cpp; cpp++) { 1666 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1667 namelen, name, *cpp); 1668 /* 1669 * Attempt to open the file, and return the path if 1670 * we succeed and it's a regular file. 1671 */ 1672 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td); 1673 flags = FREAD; 1674 error = vn_open(&nd, &flags, 0, NULL); 1675 if (error == 0) { 1676 vfslocked = NDHASGIANT(&nd); 1677 NDFREE(&nd, NDF_ONLY_PNBUF); 1678 type = nd.ni_vp->v_type; 1679 if (vap) 1680 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1681 VOP_UNLOCK(nd.ni_vp, 0); 1682 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1683 VFS_UNLOCK_GIANT(vfslocked); 1684 if (type == VREG) 1685 return (result); 1686 } 1687 } 1688 free(result, M_LINKER); 1689 return (NULL); 1690 } 1691 1692 #define INT_ALIGN(base, ptr) ptr = \ 1693 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1694 1695 /* 1696 * Lookup KLD which contains requested module in the "linker.hints" file. If 1697 * version specification is available, then try to find the best KLD. 1698 * Otherwise just find the latest one. 1699 */ 1700 static char * 1701 linker_hints_lookup(const char *path, int pathlen, const char *modname, 1702 int modnamelen, struct mod_depend *verinfo) 1703 { 1704 struct thread *td = curthread; /* XXX */ 1705 struct ucred *cred = td ? td->td_ucred : NULL; 1706 struct nameidata nd; 1707 struct vattr vattr, mattr; 1708 u_char *hints = NULL; 1709 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1710 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1711 int vfslocked = 0; 1712 1713 result = NULL; 1714 bestver = found = 0; 1715 1716 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1717 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1718 strlen(sep) + 1; 1719 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1720 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1721 linker_hintfile); 1722 1723 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1724 flags = FREAD; 1725 error = vn_open(&nd, &flags, 0, NULL); 1726 if (error) 1727 goto bad; 1728 vfslocked = NDHASGIANT(&nd); 1729 NDFREE(&nd, NDF_ONLY_PNBUF); 1730 if (nd.ni_vp->v_type != VREG) 1731 goto bad; 1732 best = cp = NULL; 1733 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1734 if (error) 1735 goto bad; 1736 /* 1737 * XXX: we need to limit this number to some reasonable value 1738 */ 1739 if (vattr.va_size > 100 * 1024) { 1740 printf("hints file too large %ld\n", (long)vattr.va_size); 1741 goto bad; 1742 } 1743 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1744 if (hints == NULL) 1745 goto bad; 1746 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1747 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1748 if (error) 1749 goto bad; 1750 VOP_UNLOCK(nd.ni_vp, 0); 1751 vn_close(nd.ni_vp, FREAD, cred, td); 1752 VFS_UNLOCK_GIANT(vfslocked); 1753 nd.ni_vp = NULL; 1754 if (reclen != 0) { 1755 printf("can't read %d\n", reclen); 1756 goto bad; 1757 } 1758 intp = (int *)hints; 1759 ival = *intp++; 1760 if (ival != LINKER_HINTS_VERSION) { 1761 printf("hints file version mismatch %d\n", ival); 1762 goto bad; 1763 } 1764 bufend = hints + vattr.va_size; 1765 recptr = (u_char *)intp; 1766 clen = blen = 0; 1767 while (recptr < bufend && !found) { 1768 intp = (int *)recptr; 1769 reclen = *intp++; 1770 ival = *intp++; 1771 cp = (char *)intp; 1772 switch (ival) { 1773 case MDT_VERSION: 1774 clen = *cp++; 1775 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1776 break; 1777 cp += clen; 1778 INT_ALIGN(hints, cp); 1779 ival = *(int *)cp; 1780 cp += sizeof(int); 1781 clen = *cp++; 1782 if (verinfo == NULL || 1783 ival == verinfo->md_ver_preferred) { 1784 found = 1; 1785 break; 1786 } 1787 if (ival >= verinfo->md_ver_minimum && 1788 ival <= verinfo->md_ver_maximum && 1789 ival > bestver) { 1790 bestver = ival; 1791 best = cp; 1792 blen = clen; 1793 } 1794 break; 1795 default: 1796 break; 1797 } 1798 recptr += reclen + sizeof(int); 1799 } 1800 /* 1801 * Finally check if KLD is in the place 1802 */ 1803 if (found) 1804 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1805 else if (best) 1806 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1807 1808 /* 1809 * KLD is newer than hints file. What we should do now? 1810 */ 1811 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1812 printf("warning: KLD '%s' is newer than the linker.hints" 1813 " file\n", result); 1814 bad: 1815 free(pathbuf, M_LINKER); 1816 if (hints) 1817 free(hints, M_TEMP); 1818 if (nd.ni_vp != NULL) { 1819 VOP_UNLOCK(nd.ni_vp, 0); 1820 vn_close(nd.ni_vp, FREAD, cred, td); 1821 VFS_UNLOCK_GIANT(vfslocked); 1822 } 1823 /* 1824 * If nothing found or hints is absent - fallback to the old 1825 * way by using "kldname[.ko]" as module name. 1826 */ 1827 if (!found && !bestver && result == NULL) 1828 result = linker_lookup_file(path, pathlen, modname, 1829 modnamelen, NULL); 1830 return (result); 1831 } 1832 1833 /* 1834 * Lookup KLD which contains requested module in the all directories. 1835 */ 1836 static char * 1837 linker_search_module(const char *modname, int modnamelen, 1838 struct mod_depend *verinfo) 1839 { 1840 char *cp, *ep, *result; 1841 1842 /* 1843 * traverse the linker path 1844 */ 1845 for (cp = linker_path; *cp; cp = ep + 1) { 1846 /* find the end of this component */ 1847 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1848 result = linker_hints_lookup(cp, ep - cp, modname, 1849 modnamelen, verinfo); 1850 if (result != NULL) 1851 return (result); 1852 if (*ep == 0) 1853 break; 1854 } 1855 return (NULL); 1856 } 1857 1858 /* 1859 * Search for module in all directories listed in the linker_path. 1860 */ 1861 static char * 1862 linker_search_kld(const char *name) 1863 { 1864 char *cp, *ep, *result; 1865 int len; 1866 1867 /* qualified at all? */ 1868 if (index(name, '/')) 1869 return (linker_strdup(name)); 1870 1871 /* traverse the linker path */ 1872 len = strlen(name); 1873 for (ep = linker_path; *ep; ep++) { 1874 cp = ep; 1875 /* find the end of this component */ 1876 for (; *ep != 0 && *ep != ';'; ep++); 1877 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1878 if (result != NULL) 1879 return (result); 1880 } 1881 return (NULL); 1882 } 1883 1884 static const char * 1885 linker_basename(const char *path) 1886 { 1887 const char *filename; 1888 1889 filename = rindex(path, '/'); 1890 if (filename == NULL) 1891 return path; 1892 if (filename[1]) 1893 filename++; 1894 return (filename); 1895 } 1896 1897 #ifdef HWPMC_HOOKS 1898 1899 struct hwpmc_context { 1900 int nobjects; 1901 int nmappings; 1902 struct pmckern_map_in *kobase; 1903 }; 1904 1905 static int 1906 linker_hwpmc_list_object(linker_file_t lf, void *arg) 1907 { 1908 struct hwpmc_context *hc; 1909 1910 hc = arg; 1911 1912 /* If we run out of mappings, fail. */ 1913 if (hc->nobjects >= hc->nmappings) 1914 return (1); 1915 1916 /* Save the info for this linker file. */ 1917 hc->kobase[hc->nobjects].pm_file = lf->filename; 1918 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address; 1919 hc->nobjects++; 1920 return (0); 1921 } 1922 1923 /* 1924 * Inform hwpmc about the set of kernel modules currently loaded. 1925 */ 1926 void * 1927 linker_hwpmc_list_objects(void) 1928 { 1929 struct hwpmc_context hc; 1930 1931 hc.nmappings = 15; /* a reasonable default */ 1932 1933 retry: 1934 /* allocate nmappings+1 entries */ 1935 hc.kobase = malloc((hc.nmappings + 1) * sizeof(struct pmckern_map_in), 1936 M_LINKER, M_WAITOK | M_ZERO); 1937 1938 hc.nobjects = 0; 1939 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) { 1940 hc.nmappings = hc.nobjects; 1941 free(hc.kobase, M_LINKER); 1942 goto retry; 1943 } 1944 1945 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel " 1946 "objects?")); 1947 1948 /* The last entry of the malloced area comprises of all zeros. */ 1949 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL, 1950 ("linker_hwpmc_list_objects: last object not NULL")); 1951 1952 return ((void *)hc.kobase); 1953 } 1954 #endif 1955 1956 /* 1957 * Find a file which contains given module and load it, if "parent" is not 1958 * NULL, register a reference to it. 1959 */ 1960 static int 1961 linker_load_module(const char *kldname, const char *modname, 1962 struct linker_file *parent, struct mod_depend *verinfo, 1963 struct linker_file **lfpp) 1964 { 1965 linker_file_t lfdep; 1966 const char *filename; 1967 char *pathname; 1968 int error; 1969 1970 KLD_LOCK_ASSERT(); 1971 if (modname == NULL) { 1972 /* 1973 * We have to load KLD 1974 */ 1975 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 1976 " is not NULL")); 1977 pathname = linker_search_kld(kldname); 1978 } else { 1979 if (modlist_lookup2(modname, verinfo) != NULL) 1980 return (EEXIST); 1981 if (kldname != NULL) 1982 pathname = linker_strdup(kldname); 1983 else if (rootvnode == NULL) 1984 pathname = NULL; 1985 else 1986 /* 1987 * Need to find a KLD with required module 1988 */ 1989 pathname = linker_search_module(modname, 1990 strlen(modname), verinfo); 1991 } 1992 if (pathname == NULL) 1993 return (ENOENT); 1994 1995 /* 1996 * Can't load more than one file with the same basename XXX: 1997 * Actually it should be possible to have multiple KLDs with 1998 * the same basename but different path because they can 1999 * provide different versions of the same modules. 2000 */ 2001 filename = linker_basename(pathname); 2002 if (linker_find_file_by_name(filename)) 2003 error = EEXIST; 2004 else do { 2005 error = linker_load_file(pathname, &lfdep); 2006 if (error) 2007 break; 2008 if (modname && verinfo && 2009 modlist_lookup2(modname, verinfo) == NULL) { 2010 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2011 error = ENOENT; 2012 break; 2013 } 2014 if (parent) { 2015 error = linker_file_add_dependency(parent, lfdep); 2016 if (error) 2017 break; 2018 } 2019 if (lfpp) 2020 *lfpp = lfdep; 2021 } while (0); 2022 free(pathname, M_LINKER); 2023 return (error); 2024 } 2025 2026 /* 2027 * This routine is responsible for finding dependencies of userland initiated 2028 * kldload(2)'s of files. 2029 */ 2030 int 2031 linker_load_dependencies(linker_file_t lf) 2032 { 2033 linker_file_t lfdep; 2034 struct mod_metadata **start, **stop, **mdp, **nmdp; 2035 struct mod_metadata *mp, *nmp; 2036 struct mod_depend *verinfo; 2037 modlist_t mod; 2038 const char *modname, *nmodname; 2039 int ver, error = 0, count; 2040 2041 /* 2042 * All files are dependant on /kernel. 2043 */ 2044 KLD_LOCK_ASSERT(); 2045 if (linker_kernel_file) { 2046 linker_kernel_file->refs++; 2047 error = linker_file_add_dependency(lf, linker_kernel_file); 2048 if (error) 2049 return (error); 2050 } 2051 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2052 &count) != 0) 2053 return (0); 2054 for (mdp = start; mdp < stop; mdp++) { 2055 mp = *mdp; 2056 if (mp->md_type != MDT_VERSION) 2057 continue; 2058 modname = mp->md_cval; 2059 ver = ((struct mod_version *)mp->md_data)->mv_version; 2060 mod = modlist_lookup(modname, ver); 2061 if (mod != NULL) { 2062 printf("interface %s.%d already present in the KLD" 2063 " '%s'!\n", modname, ver, 2064 mod->container->filename); 2065 return (EEXIST); 2066 } 2067 } 2068 2069 for (mdp = start; mdp < stop; mdp++) { 2070 mp = *mdp; 2071 if (mp->md_type != MDT_DEPEND) 2072 continue; 2073 modname = mp->md_cval; 2074 verinfo = mp->md_data; 2075 nmodname = NULL; 2076 for (nmdp = start; nmdp < stop; nmdp++) { 2077 nmp = *nmdp; 2078 if (nmp->md_type != MDT_VERSION) 2079 continue; 2080 nmodname = nmp->md_cval; 2081 if (strcmp(modname, nmodname) == 0) 2082 break; 2083 } 2084 if (nmdp < stop)/* early exit, it's a self reference */ 2085 continue; 2086 mod = modlist_lookup2(modname, verinfo); 2087 if (mod) { /* woohoo, it's loaded already */ 2088 lfdep = mod->container; 2089 lfdep->refs++; 2090 error = linker_file_add_dependency(lf, lfdep); 2091 if (error) 2092 break; 2093 continue; 2094 } 2095 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2096 if (error) { 2097 printf("KLD %s: depends on %s - not available\n", 2098 lf->filename, modname); 2099 break; 2100 } 2101 } 2102 2103 if (error) 2104 return (error); 2105 linker_addmodules(lf, start, stop, 0); 2106 return (error); 2107 } 2108 2109 static int 2110 sysctl_kern_function_list_iterate(const char *name, void *opaque) 2111 { 2112 struct sysctl_req *req; 2113 2114 req = opaque; 2115 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2116 } 2117 2118 /* 2119 * Export a nul-separated, double-nul-terminated list of all function names 2120 * in the kernel. 2121 */ 2122 static int 2123 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2124 { 2125 linker_file_t lf; 2126 int error; 2127 2128 #ifdef MAC 2129 error = mac_kld_check_stat(req->td->td_ucred); 2130 if (error) 2131 return (error); 2132 #endif 2133 error = sysctl_wire_old_buffer(req, 0); 2134 if (error != 0) 2135 return (error); 2136 KLD_LOCK(); 2137 TAILQ_FOREACH(lf, &linker_files, link) { 2138 error = LINKER_EACH_FUNCTION_NAME(lf, 2139 sysctl_kern_function_list_iterate, req); 2140 if (error) { 2141 KLD_UNLOCK(); 2142 return (error); 2143 } 2144 } 2145 KLD_UNLOCK(); 2146 return (SYSCTL_OUT(req, "", 1)); 2147 } 2148 2149 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 2150 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2151