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