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