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