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