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