kern_linker.c revision 179238
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 179238 2008-05-23 07:08:59Z jb $"); 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 * Inform any modules associated with this file. 591 */ 592 MOD_XLOCK; 593 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 594 next = module_getfnext(mod); 595 MOD_XUNLOCK; 596 597 /* 598 * Give the module a chance to veto the unload. 599 */ 600 if ((error = module_unload(mod, flags)) != 0) { 601 KLD_DPF(FILE, ("linker_file_unload: module %p" 602 " vetoes unload\n", mod)); 603 return (error); 604 } 605 MOD_XLOCK; 606 module_release(mod); 607 } 608 MOD_XUNLOCK; 609 610 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) { 611 if (ml->container == file) { 612 TAILQ_REMOVE(&found_modules, ml, link); 613 free(ml, M_LINKER); 614 } 615 } 616 617 /* 618 * Don't try to run SYSUNINITs if we are unloaded due to a 619 * link error. 620 */ 621 if (file->flags & LINKER_FILE_LINKED) { 622 linker_file_sysuninit(file); 623 linker_file_unregister_sysctls(file); 624 } 625 TAILQ_REMOVE(&linker_files, file, link); 626 627 if (file->deps) { 628 for (i = 0; i < file->ndeps; i++) 629 linker_file_unload(file->deps[i], flags); 630 free(file->deps, M_LINKER); 631 file->deps = NULL; 632 } 633 while ((cp = STAILQ_FIRST(&file->common)) != NULL) { 634 STAILQ_REMOVE_HEAD(&file->common, link); 635 free(cp, M_LINKER); 636 } 637 638 LINKER_UNLOAD(file); 639 if (file->filename) { 640 free(file->filename, M_LINKER); 641 file->filename = NULL; 642 } 643 if (file->pathname) { 644 free(file->pathname, M_LINKER); 645 file->pathname = NULL; 646 } 647 kobj_delete((kobj_t) file, M_LINKER); 648 return (0); 649} 650 651int 652linker_ctf_get(linker_file_t file, linker_ctf_t *lc) 653{ 654 return (LINKER_CTF_GET(file, lc)); 655} 656 657static int 658linker_file_add_dependency(linker_file_t file, linker_file_t dep) 659{ 660 linker_file_t *newdeps; 661 662 KLD_LOCK_ASSERT(); 663 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *), 664 M_LINKER, M_WAITOK | M_ZERO); 665 if (newdeps == NULL) 666 return (ENOMEM); 667 668 if (file->deps) { 669 bcopy(file->deps, newdeps, 670 file->ndeps * sizeof(linker_file_t *)); 671 free(file->deps, M_LINKER); 672 } 673 file->deps = newdeps; 674 file->deps[file->ndeps] = dep; 675 file->ndeps++; 676 return (0); 677} 678 679/* 680 * Locate a linker set and its contents. This is a helper function to avoid 681 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **. 682 * This function is used in this file so we can avoid having lots of (void **) 683 * casts. 684 */ 685int 686linker_file_lookup_set(linker_file_t file, const char *name, 687 void *firstp, void *lastp, int *countp) 688{ 689 int error, locked; 690 691 locked = KLD_LOCKED(); 692 if (!locked) 693 KLD_LOCK(); 694 error = LINKER_LOOKUP_SET(file, name, firstp, lastp, countp); 695 if (!locked) 696 KLD_UNLOCK(); 697 return (error); 698} 699 700/* 701 * List all functions in a file. 702 */ 703int 704linker_file_function_listall(linker_file_t lf, 705 linker_function_nameval_callback_t callback_func, void *arg) 706{ 707 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg)); 708} 709 710caddr_t 711linker_file_lookup_symbol(linker_file_t file, const char *name, int deps) 712{ 713 caddr_t sym; 714 int locked; 715 716 locked = KLD_LOCKED(); 717 if (!locked) 718 KLD_LOCK(); 719 sym = linker_file_lookup_symbol_internal(file, name, deps); 720 if (!locked) 721 KLD_UNLOCK(); 722 return (sym); 723} 724 725static caddr_t 726linker_file_lookup_symbol_internal(linker_file_t file, const char *name, 727 int deps) 728{ 729 c_linker_sym_t sym; 730 linker_symval_t symval; 731 caddr_t address; 732 size_t common_size = 0; 733 int i; 734 735 KLD_LOCK_ASSERT(); 736 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 737 file, name, deps)); 738 739 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 740 LINKER_SYMBOL_VALUES(file, sym, &symval); 741 if (symval.value == 0) 742 /* 743 * For commons, first look them up in the 744 * dependencies and only allocate space if not found 745 * there. 746 */ 747 common_size = symval.size; 748 else { 749 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol" 750 ".value=%p\n", symval.value)); 751 return (symval.value); 752 } 753 } 754 if (deps) { 755 for (i = 0; i < file->ndeps; i++) { 756 address = linker_file_lookup_symbol_internal( 757 file->deps[i], name, 0); 758 if (address) { 759 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 760 " deps value=%p\n", address)); 761 return (address); 762 } 763 } 764 } 765 if (common_size > 0) { 766 /* 767 * This is a common symbol which was not found in the 768 * dependencies. We maintain a simple common symbol table in 769 * the file object. 770 */ 771 struct common_symbol *cp; 772 773 STAILQ_FOREACH(cp, &file->common, link) { 774 if (strcmp(cp->name, name) == 0) { 775 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 776 " old common value=%p\n", cp->address)); 777 return (cp->address); 778 } 779 } 780 /* 781 * Round the symbol size up to align. 782 */ 783 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 784 cp = malloc(sizeof(struct common_symbol) 785 + common_size + strlen(name) + 1, M_LINKER, 786 M_WAITOK | M_ZERO); 787 cp->address = (caddr_t)(cp + 1); 788 cp->name = cp->address + common_size; 789 strcpy(cp->name, name); 790 bzero(cp->address, common_size); 791 STAILQ_INSERT_TAIL(&file->common, cp, link); 792 793 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common" 794 " value=%p\n", cp->address)); 795 return (cp->address); 796 } 797 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 798 return (0); 799} 800 801/* 802 * Both DDB and stack(9) rely on the kernel linker to provide forward and 803 * backward lookup of symbols. However, DDB and sometimes stack(9) need to 804 * do this in a lockfree manner. We provide a set of internal helper 805 * routines to perform these operations without locks, and then wrappers that 806 * optionally lock. 807 * 808 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB. 809 */ 810#ifdef DDB 811static int 812linker_debug_lookup(const char *symstr, c_linker_sym_t *sym) 813{ 814 linker_file_t lf; 815 816 TAILQ_FOREACH(lf, &linker_files, link) { 817 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 818 return (0); 819 } 820 return (ENOENT); 821} 822#endif 823 824static int 825linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 826{ 827 linker_file_t lf; 828 c_linker_sym_t best, es; 829 u_long diff, bestdiff, off; 830 831 best = 0; 832 off = (uintptr_t)value; 833 bestdiff = off; 834 TAILQ_FOREACH(lf, &linker_files, link) { 835 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 836 continue; 837 if (es != 0 && diff < bestdiff) { 838 best = es; 839 bestdiff = diff; 840 } 841 if (bestdiff == 0) 842 break; 843 } 844 if (best) { 845 *sym = best; 846 *diffp = bestdiff; 847 return (0); 848 } else { 849 *sym = 0; 850 *diffp = off; 851 return (ENOENT); 852 } 853} 854 855static int 856linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 857{ 858 linker_file_t lf; 859 860 TAILQ_FOREACH(lf, &linker_files, link) { 861 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 862 return (0); 863 } 864 return (ENOENT); 865} 866 867static int 868linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen, 869 long *offset) 870{ 871 linker_symval_t symval; 872 c_linker_sym_t sym; 873 int error; 874 875 *offset = 0; 876 error = linker_debug_search_symbol(value, &sym, offset); 877 if (error) 878 return (error); 879 error = linker_debug_symbol_values(sym, &symval); 880 if (error) 881 return (error); 882 strlcpy(buf, symval.name, buflen); 883 return (0); 884} 885 886#ifdef DDB 887/* 888 * DDB Helpers. DDB has to look across multiple files with their own symbol 889 * tables and string tables. 890 * 891 * Note that we do not obey list locking protocols here. We really don't need 892 * DDB to hang because somebody's got the lock held. We'll take the chance 893 * that the files list is inconsistant instead. 894 */ 895int 896linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 897{ 898 899 return (linker_debug_lookup(symstr, sym)); 900} 901 902int 903linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 904{ 905 906 return (linker_debug_search_symbol(value, sym, diffp)); 907} 908 909int 910linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 911{ 912 913 return (linker_debug_symbol_values(sym, symval)); 914} 915 916int 917linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, 918 long *offset) 919{ 920 921 return (linker_debug_search_symbol_name(value, buf, buflen, offset)); 922} 923#endif 924 925/* 926 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do 927 * obey locking protocols, and offer a significantly less complex interface. 928 */ 929int 930linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 931 long *offset) 932{ 933 int error; 934 935 KLD_LOCK(); 936 error = linker_debug_search_symbol_name(value, buf, buflen, offset); 937 KLD_UNLOCK(); 938 return (error); 939} 940 941/* 942 * Syscalls. 943 */ 944int 945kern_kldload(struct thread *td, const char *file, int *fileid) 946{ 947#ifdef HWPMC_HOOKS 948 struct pmckern_map_in pkm; 949#endif 950 const char *kldname, *modname; 951 linker_file_t lf; 952 int error; 953 954 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 955 return (error); 956 957 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 958 return (error); 959 960 /* 961 * If file does not contain a qualified name or any dot in it 962 * (kldname.ko, or kldname.ver.ko) treat it as an interface 963 * name. 964 */ 965 if (index(file, '/') || index(file, '.')) { 966 kldname = file; 967 modname = NULL; 968 } else { 969 kldname = NULL; 970 modname = file; 971 } 972 973 KLD_LOCK(); 974 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 975 if (error) 976 goto unlock; 977#ifdef HWPMC_HOOKS 978 pkm.pm_file = lf->filename; 979 pkm.pm_address = (uintptr_t) lf->address; 980 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm); 981#endif 982 lf->userrefs++; 983 if (fileid != NULL) 984 *fileid = lf->id; 985unlock: 986 KLD_UNLOCK(); 987 return (error); 988} 989 990int 991kldload(struct thread *td, struct kldload_args *uap) 992{ 993 char *pathname = NULL; 994 int error, fileid; 995 996 td->td_retval[0] = -1; 997 998 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 999 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1000 if (error == 0) { 1001 error = kern_kldload(td, pathname, &fileid); 1002 if (error == 0) 1003 td->td_retval[0] = fileid; 1004 } 1005 free(pathname, M_TEMP); 1006 return (error); 1007} 1008 1009int 1010kern_kldunload(struct thread *td, int fileid, int flags) 1011{ 1012#ifdef HWPMC_HOOKS 1013 struct pmckern_map_out pkm; 1014#endif 1015 linker_file_t lf; 1016 int error = 0; 1017 1018 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1019 return (error); 1020 1021 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1022 return (error); 1023 1024 KLD_LOCK(); 1025 lf = linker_find_file_by_id(fileid); 1026 if (lf) { 1027 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1028 1029 /* Check if there are DTrace probes enabled on this file. */ 1030 if (lf->nenabled > 0) { 1031 printf("kldunload: attempt to unload file that has" 1032 " DTrace probes enabled\n"); 1033 error = EBUSY; 1034 } else if (lf->userrefs == 0) { 1035 /* 1036 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1037 */ 1038 printf("kldunload: attempt to unload file that was" 1039 " loaded by the kernel\n"); 1040 error = EBUSY; 1041 } else { 1042#ifdef HWPMC_HOOKS 1043 /* Save data needed by hwpmc(4) before unloading. */ 1044 pkm.pm_address = (uintptr_t) lf->address; 1045 pkm.pm_size = lf->size; 1046#endif 1047 lf->userrefs--; 1048 error = linker_file_unload(lf, flags); 1049 if (error) 1050 lf->userrefs++; 1051 } 1052 } else 1053 error = ENOENT; 1054 1055#ifdef HWPMC_HOOKS 1056 if (error == 0) 1057 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm); 1058#endif 1059 KLD_UNLOCK(); 1060 return (error); 1061} 1062 1063int 1064kldunload(struct thread *td, struct kldunload_args *uap) 1065{ 1066 1067 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1068} 1069 1070int 1071kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1072{ 1073 1074 if (uap->flags != LINKER_UNLOAD_NORMAL && 1075 uap->flags != LINKER_UNLOAD_FORCE) 1076 return (EINVAL); 1077 return (kern_kldunload(td, uap->fileid, uap->flags)); 1078} 1079 1080int 1081kldfind(struct thread *td, struct kldfind_args *uap) 1082{ 1083 char *pathname; 1084 const char *filename; 1085 linker_file_t lf; 1086 int error; 1087 1088#ifdef MAC 1089 error = mac_kld_check_stat(td->td_ucred); 1090 if (error) 1091 return (error); 1092#endif 1093 1094 td->td_retval[0] = -1; 1095 1096 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1097 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1098 goto out; 1099 1100 filename = linker_basename(pathname); 1101 KLD_LOCK(); 1102 lf = linker_find_file_by_name(filename); 1103 if (lf) 1104 td->td_retval[0] = lf->id; 1105 else 1106 error = ENOENT; 1107 KLD_UNLOCK(); 1108out: 1109 free(pathname, M_TEMP); 1110 return (error); 1111} 1112 1113int 1114kldnext(struct thread *td, struct kldnext_args *uap) 1115{ 1116 linker_file_t lf; 1117 int error = 0; 1118 1119#ifdef MAC 1120 error = mac_kld_check_stat(td->td_ucred); 1121 if (error) 1122 return (error); 1123#endif 1124 1125 KLD_LOCK(); 1126 if (uap->fileid == 0) 1127 lf = TAILQ_FIRST(&linker_files); 1128 else { 1129 lf = linker_find_file_by_id(uap->fileid); 1130 if (lf == NULL) { 1131 error = ENOENT; 1132 goto out; 1133 } 1134 lf = TAILQ_NEXT(lf, link); 1135 } 1136 1137 /* Skip partially loaded files. */ 1138 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1139 lf = TAILQ_NEXT(lf, link); 1140 1141 if (lf) 1142 td->td_retval[0] = lf->id; 1143 else 1144 td->td_retval[0] = 0; 1145out: 1146 KLD_UNLOCK(); 1147 return (error); 1148} 1149 1150int 1151kldstat(struct thread *td, struct kldstat_args *uap) 1152{ 1153 struct kld_file_stat stat; 1154 linker_file_t lf; 1155 int error, namelen, version, version_num; 1156 1157 /* 1158 * Check the version of the user's structure. 1159 */ 1160 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0) 1161 return (error); 1162 if (version == sizeof(struct kld_file_stat_1)) 1163 version_num = 1; 1164 else if (version == sizeof(struct kld_file_stat)) 1165 version_num = 2; 1166 else 1167 return (EINVAL); 1168 1169#ifdef MAC 1170 error = mac_kld_check_stat(td->td_ucred); 1171 if (error) 1172 return (error); 1173#endif 1174 1175 KLD_LOCK(); 1176 lf = linker_find_file_by_id(uap->fileid); 1177 if (lf == NULL) { 1178 KLD_UNLOCK(); 1179 return (ENOENT); 1180 } 1181 1182 /* Version 1 fields: */ 1183 namelen = strlen(lf->filename) + 1; 1184 if (namelen > MAXPATHLEN) 1185 namelen = MAXPATHLEN; 1186 bcopy(lf->filename, &stat.name[0], namelen); 1187 stat.refs = lf->refs; 1188 stat.id = lf->id; 1189 stat.address = lf->address; 1190 stat.size = lf->size; 1191 if (version_num > 1) { 1192 /* Version 2 fields: */ 1193 namelen = strlen(lf->pathname) + 1; 1194 if (namelen > MAXPATHLEN) 1195 namelen = MAXPATHLEN; 1196 bcopy(lf->pathname, &stat.pathname[0], namelen); 1197 } 1198 KLD_UNLOCK(); 1199 1200 td->td_retval[0] = 0; 1201 1202 return (copyout(&stat, uap->stat, version)); 1203} 1204 1205int 1206kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1207{ 1208 linker_file_t lf; 1209 module_t mp; 1210 int error = 0; 1211 1212#ifdef MAC 1213 error = mac_kld_check_stat(td->td_ucred); 1214 if (error) 1215 return (error); 1216#endif 1217 1218 KLD_LOCK(); 1219 lf = linker_find_file_by_id(uap->fileid); 1220 if (lf) { 1221 MOD_SLOCK; 1222 mp = TAILQ_FIRST(&lf->modules); 1223 if (mp != NULL) 1224 td->td_retval[0] = module_getid(mp); 1225 else 1226 td->td_retval[0] = 0; 1227 MOD_SUNLOCK; 1228 } else 1229 error = ENOENT; 1230 KLD_UNLOCK(); 1231 return (error); 1232} 1233 1234int 1235kldsym(struct thread *td, struct kldsym_args *uap) 1236{ 1237 char *symstr = NULL; 1238 c_linker_sym_t sym; 1239 linker_symval_t symval; 1240 linker_file_t lf; 1241 struct kld_sym_lookup lookup; 1242 int error = 0; 1243 1244#ifdef MAC 1245 error = mac_kld_check_stat(td->td_ucred); 1246 if (error) 1247 return (error); 1248#endif 1249 1250 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1251 return (error); 1252 if (lookup.version != sizeof(lookup) || 1253 uap->cmd != KLDSYM_LOOKUP) 1254 return (EINVAL); 1255 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1256 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1257 goto out; 1258 KLD_LOCK(); 1259 if (uap->fileid != 0) { 1260 lf = linker_find_file_by_id(uap->fileid); 1261 if (lf == NULL) 1262 error = ENOENT; 1263 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1264 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1265 lookup.symvalue = (uintptr_t) symval.value; 1266 lookup.symsize = symval.size; 1267 error = copyout(&lookup, uap->data, sizeof(lookup)); 1268 } else 1269 error = ENOENT; 1270 } else { 1271 TAILQ_FOREACH(lf, &linker_files, link) { 1272 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1273 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1274 lookup.symvalue = (uintptr_t)symval.value; 1275 lookup.symsize = symval.size; 1276 error = copyout(&lookup, uap->data, 1277 sizeof(lookup)); 1278 break; 1279 } 1280 } 1281 if (lf == NULL) 1282 error = ENOENT; 1283 } 1284 KLD_UNLOCK(); 1285out: 1286 free(symstr, M_TEMP); 1287 return (error); 1288} 1289 1290/* 1291 * Preloaded module support 1292 */ 1293 1294static modlist_t 1295modlist_lookup(const char *name, int ver) 1296{ 1297 modlist_t mod; 1298 1299 TAILQ_FOREACH(mod, &found_modules, link) { 1300 if (strcmp(mod->name, name) == 0 && 1301 (ver == 0 || mod->version == ver)) 1302 return (mod); 1303 } 1304 return (NULL); 1305} 1306 1307static modlist_t 1308modlist_lookup2(const char *name, struct mod_depend *verinfo) 1309{ 1310 modlist_t mod, bestmod; 1311 int ver; 1312 1313 if (verinfo == NULL) 1314 return (modlist_lookup(name, 0)); 1315 bestmod = NULL; 1316 TAILQ_FOREACH(mod, &found_modules, link) { 1317 if (strcmp(mod->name, name) != 0) 1318 continue; 1319 ver = mod->version; 1320 if (ver == verinfo->md_ver_preferred) 1321 return (mod); 1322 if (ver >= verinfo->md_ver_minimum && 1323 ver <= verinfo->md_ver_maximum && 1324 (bestmod == NULL || ver > bestmod->version)) 1325 bestmod = mod; 1326 } 1327 return (bestmod); 1328} 1329 1330static modlist_t 1331modlist_newmodule(const char *modname, int version, linker_file_t container) 1332{ 1333 modlist_t mod; 1334 1335 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1336 if (mod == NULL) 1337 panic("no memory for module list"); 1338 mod->container = container; 1339 mod->name = modname; 1340 mod->version = version; 1341 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1342 return (mod); 1343} 1344 1345static void 1346linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1347 struct mod_metadata **stop, int preload) 1348{ 1349 struct mod_metadata *mp, **mdp; 1350 const char *modname; 1351 int ver; 1352 1353 for (mdp = start; mdp < stop; mdp++) { 1354 mp = *mdp; 1355 if (mp->md_type != MDT_VERSION) 1356 continue; 1357 modname = mp->md_cval; 1358 ver = ((struct mod_version *)mp->md_data)->mv_version; 1359 if (modlist_lookup(modname, ver) != NULL) { 1360 printf("module %s already present!\n", modname); 1361 /* XXX what can we do? this is a build error. :-( */ 1362 continue; 1363 } 1364 modlist_newmodule(modname, ver, lf); 1365 } 1366} 1367 1368static void 1369linker_preload(void *arg) 1370{ 1371 caddr_t modptr; 1372 const char *modname, *nmodname; 1373 char *modtype; 1374 linker_file_t lf, nlf; 1375 linker_class_t lc; 1376 int error; 1377 linker_file_list_t loaded_files; 1378 linker_file_list_t depended_files; 1379 struct mod_metadata *mp, *nmp; 1380 struct mod_metadata **start, **stop, **mdp, **nmdp; 1381 struct mod_depend *verinfo; 1382 int nver; 1383 int resolves; 1384 modlist_t mod; 1385 struct sysinit **si_start, **si_stop; 1386 1387 TAILQ_INIT(&loaded_files); 1388 TAILQ_INIT(&depended_files); 1389 TAILQ_INIT(&found_modules); 1390 error = 0; 1391 1392 modptr = NULL; 1393 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1394 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1395 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1396 if (modname == NULL) { 1397 printf("Preloaded module at %p does not have a" 1398 " name!\n", modptr); 1399 continue; 1400 } 1401 if (modtype == NULL) { 1402 printf("Preloaded module at %p does not have a type!\n", 1403 modptr); 1404 continue; 1405 } 1406 if (bootverbose) 1407 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1408 modptr); 1409 lf = NULL; 1410 TAILQ_FOREACH(lc, &classes, link) { 1411 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1412 if (!error) 1413 break; 1414 lf = NULL; 1415 } 1416 if (lf) 1417 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1418 } 1419 1420 /* 1421 * First get a list of stuff in the kernel. 1422 */ 1423 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1424 &stop, NULL) == 0) 1425 linker_addmodules(linker_kernel_file, start, stop, 1); 1426 1427 /* 1428 * This is a once-off kinky bubble sort to resolve relocation 1429 * dependency requirements. 1430 */ 1431restart: 1432 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1433 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1434 &stop, NULL); 1435 /* 1436 * First, look to see if we would successfully link with this 1437 * stuff. 1438 */ 1439 resolves = 1; /* unless we know otherwise */ 1440 if (!error) { 1441 for (mdp = start; mdp < stop; mdp++) { 1442 mp = *mdp; 1443 if (mp->md_type != MDT_DEPEND) 1444 continue; 1445 modname = mp->md_cval; 1446 verinfo = mp->md_data; 1447 for (nmdp = start; nmdp < stop; nmdp++) { 1448 nmp = *nmdp; 1449 if (nmp->md_type != MDT_VERSION) 1450 continue; 1451 nmodname = nmp->md_cval; 1452 if (strcmp(modname, nmodname) == 0) 1453 break; 1454 } 1455 if (nmdp < stop) /* it's a self reference */ 1456 continue; 1457 1458 /* 1459 * ok, the module isn't here yet, we 1460 * are not finished 1461 */ 1462 if (modlist_lookup2(modname, verinfo) == NULL) 1463 resolves = 0; 1464 } 1465 } 1466 /* 1467 * OK, if we found our modules, we can link. So, "provide" 1468 * the modules inside and add it to the end of the link order 1469 * list. 1470 */ 1471 if (resolves) { 1472 if (!error) { 1473 for (mdp = start; mdp < stop; mdp++) { 1474 mp = *mdp; 1475 if (mp->md_type != MDT_VERSION) 1476 continue; 1477 modname = mp->md_cval; 1478 nver = ((struct mod_version *) 1479 mp->md_data)->mv_version; 1480 if (modlist_lookup(modname, 1481 nver) != NULL) { 1482 printf("module %s already" 1483 " present!\n", modname); 1484 TAILQ_REMOVE(&loaded_files, 1485 lf, loaded); 1486 linker_file_unload(lf, 1487 LINKER_UNLOAD_FORCE); 1488 /* we changed tailq next ptr */ 1489 goto restart; 1490 } 1491 modlist_newmodule(modname, nver, lf); 1492 } 1493 } 1494 TAILQ_REMOVE(&loaded_files, lf, loaded); 1495 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1496 /* 1497 * Since we provided modules, we need to restart the 1498 * sort so that the previous files that depend on us 1499 * have a chance. Also, we've busted the tailq next 1500 * pointer with the REMOVE. 1501 */ 1502 goto restart; 1503 } 1504 } 1505 1506 /* 1507 * At this point, we check to see what could not be resolved.. 1508 */ 1509 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1510 TAILQ_REMOVE(&loaded_files, lf, loaded); 1511 printf("KLD file %s is missing dependencies\n", lf->filename); 1512 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1513 } 1514 1515 /* 1516 * We made it. Finish off the linking in the order we determined. 1517 */ 1518 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1519 if (linker_kernel_file) { 1520 linker_kernel_file->refs++; 1521 error = linker_file_add_dependency(lf, 1522 linker_kernel_file); 1523 if (error) 1524 panic("cannot add dependency"); 1525 } 1526 lf->userrefs++; /* so we can (try to) kldunload it */ 1527 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1528 &stop, NULL); 1529 if (!error) { 1530 for (mdp = start; mdp < stop; mdp++) { 1531 mp = *mdp; 1532 if (mp->md_type != MDT_DEPEND) 1533 continue; 1534 modname = mp->md_cval; 1535 verinfo = mp->md_data; 1536 mod = modlist_lookup2(modname, verinfo); 1537 /* Don't count self-dependencies */ 1538 if (lf == mod->container) 1539 continue; 1540 mod->container->refs++; 1541 error = linker_file_add_dependency(lf, 1542 mod->container); 1543 if (error) 1544 panic("cannot add dependency"); 1545 } 1546 } 1547 /* 1548 * Now do relocation etc using the symbol search paths 1549 * established by the dependencies 1550 */ 1551 error = LINKER_LINK_PRELOAD_FINISH(lf); 1552 if (error) { 1553 TAILQ_REMOVE(&depended_files, lf, loaded); 1554 printf("KLD file %s - could not finalize loading\n", 1555 lf->filename); 1556 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1557 continue; 1558 } 1559 linker_file_register_modules(lf); 1560 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1561 &si_stop, NULL) == 0) 1562 sysinit_add(si_start, si_stop); 1563 linker_file_register_sysctls(lf); 1564 lf->flags |= LINKER_FILE_LINKED; 1565 } 1566 /* woohoo! we made it! */ 1567} 1568 1569SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1570 1571/* 1572 * Search for a not-loaded module by name. 1573 * 1574 * Modules may be found in the following locations: 1575 * 1576 * - preloaded (result is just the module name) - on disk (result is full path 1577 * to module) 1578 * 1579 * If the module name is qualified in any way (contains path, etc.) the we 1580 * simply return a copy of it. 1581 * 1582 * The search path can be manipulated via sysctl. Note that we use the ';' 1583 * character as a separator to be consistent with the bootloader. 1584 */ 1585 1586static char linker_hintfile[] = "linker.hints"; 1587static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1588 1589SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1590 sizeof(linker_path), "module load search path"); 1591 1592TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1593 1594static char *linker_ext_list[] = { 1595 "", 1596 ".ko", 1597 NULL 1598}; 1599 1600/* 1601 * Check if file actually exists either with or without extension listed in 1602 * the linker_ext_list. (probably should be generic for the rest of the 1603 * kernel) 1604 */ 1605static char * 1606linker_lookup_file(const char *path, int pathlen, const char *name, 1607 int namelen, struct vattr *vap) 1608{ 1609 struct nameidata nd; 1610 struct thread *td = curthread; /* XXX */ 1611 char *result, **cpp, *sep; 1612 int error, len, extlen, reclen, flags, vfslocked; 1613 enum vtype type; 1614 1615 extlen = 0; 1616 for (cpp = linker_ext_list; *cpp; cpp++) { 1617 len = strlen(*cpp); 1618 if (len > extlen) 1619 extlen = len; 1620 } 1621 extlen++; /* trailing '\0' */ 1622 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1623 1624 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1625 result = malloc(reclen, M_LINKER, M_WAITOK); 1626 for (cpp = linker_ext_list; *cpp; cpp++) { 1627 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1628 namelen, name, *cpp); 1629 /* 1630 * Attempt to open the file, and return the path if 1631 * we succeed and it's a regular file. 1632 */ 1633 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td); 1634 flags = FREAD; 1635 error = vn_open(&nd, &flags, 0, NULL); 1636 if (error == 0) { 1637 vfslocked = NDHASGIANT(&nd); 1638 NDFREE(&nd, NDF_ONLY_PNBUF); 1639 type = nd.ni_vp->v_type; 1640 if (vap) 1641 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td); 1642 VOP_UNLOCK(nd.ni_vp, 0); 1643 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1644 VFS_UNLOCK_GIANT(vfslocked); 1645 if (type == VREG) 1646 return (result); 1647 } 1648 } 1649 free(result, M_LINKER); 1650 return (NULL); 1651} 1652 1653#define INT_ALIGN(base, ptr) ptr = \ 1654 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1655 1656/* 1657 * Lookup KLD which contains requested module in the "linker.hints" file. If 1658 * version specification is available, then try to find the best KLD. 1659 * Otherwise just find the latest one. 1660 */ 1661static char * 1662linker_hints_lookup(const char *path, int pathlen, const char *modname, 1663 int modnamelen, struct mod_depend *verinfo) 1664{ 1665 struct thread *td = curthread; /* XXX */ 1666 struct ucred *cred = td ? td->td_ucred : NULL; 1667 struct nameidata nd; 1668 struct vattr vattr, mattr; 1669 u_char *hints = NULL; 1670 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1671 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1672 int vfslocked = 0; 1673 1674 result = NULL; 1675 bestver = found = 0; 1676 1677 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1678 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1679 strlen(sep) + 1; 1680 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1681 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1682 linker_hintfile); 1683 1684 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1685 flags = FREAD; 1686 error = vn_open(&nd, &flags, 0, NULL); 1687 if (error) 1688 goto bad; 1689 vfslocked = NDHASGIANT(&nd); 1690 NDFREE(&nd, NDF_ONLY_PNBUF); 1691 if (nd.ni_vp->v_type != VREG) 1692 goto bad; 1693 best = cp = NULL; 1694 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td); 1695 if (error) 1696 goto bad; 1697 /* 1698 * XXX: we need to limit this number to some reasonable value 1699 */ 1700 if (vattr.va_size > 100 * 1024) { 1701 printf("hints file too large %ld\n", (long)vattr.va_size); 1702 goto bad; 1703 } 1704 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1705 if (hints == NULL) 1706 goto bad; 1707 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1708 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1709 if (error) 1710 goto bad; 1711 VOP_UNLOCK(nd.ni_vp, 0); 1712 vn_close(nd.ni_vp, FREAD, cred, td); 1713 VFS_UNLOCK_GIANT(vfslocked); 1714 nd.ni_vp = NULL; 1715 if (reclen != 0) { 1716 printf("can't read %d\n", reclen); 1717 goto bad; 1718 } 1719 intp = (int *)hints; 1720 ival = *intp++; 1721 if (ival != LINKER_HINTS_VERSION) { 1722 printf("hints file version mismatch %d\n", ival); 1723 goto bad; 1724 } 1725 bufend = hints + vattr.va_size; 1726 recptr = (u_char *)intp; 1727 clen = blen = 0; 1728 while (recptr < bufend && !found) { 1729 intp = (int *)recptr; 1730 reclen = *intp++; 1731 ival = *intp++; 1732 cp = (char *)intp; 1733 switch (ival) { 1734 case MDT_VERSION: 1735 clen = *cp++; 1736 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1737 break; 1738 cp += clen; 1739 INT_ALIGN(hints, cp); 1740 ival = *(int *)cp; 1741 cp += sizeof(int); 1742 clen = *cp++; 1743 if (verinfo == NULL || 1744 ival == verinfo->md_ver_preferred) { 1745 found = 1; 1746 break; 1747 } 1748 if (ival >= verinfo->md_ver_minimum && 1749 ival <= verinfo->md_ver_maximum && 1750 ival > bestver) { 1751 bestver = ival; 1752 best = cp; 1753 blen = clen; 1754 } 1755 break; 1756 default: 1757 break; 1758 } 1759 recptr += reclen + sizeof(int); 1760 } 1761 /* 1762 * Finally check if KLD is in the place 1763 */ 1764 if (found) 1765 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1766 else if (best) 1767 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1768 1769 /* 1770 * KLD is newer than hints file. What we should do now? 1771 */ 1772 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1773 printf("warning: KLD '%s' is newer than the linker.hints" 1774 " file\n", result); 1775bad: 1776 free(pathbuf, M_LINKER); 1777 if (hints) 1778 free(hints, M_TEMP); 1779 if (nd.ni_vp != NULL) { 1780 VOP_UNLOCK(nd.ni_vp, 0); 1781 vn_close(nd.ni_vp, FREAD, cred, td); 1782 VFS_UNLOCK_GIANT(vfslocked); 1783 } 1784 /* 1785 * If nothing found or hints is absent - fallback to the old 1786 * way by using "kldname[.ko]" as module name. 1787 */ 1788 if (!found && !bestver && result == NULL) 1789 result = linker_lookup_file(path, pathlen, modname, 1790 modnamelen, NULL); 1791 return (result); 1792} 1793 1794/* 1795 * Lookup KLD which contains requested module in the all directories. 1796 */ 1797static char * 1798linker_search_module(const char *modname, int modnamelen, 1799 struct mod_depend *verinfo) 1800{ 1801 char *cp, *ep, *result; 1802 1803 /* 1804 * traverse the linker path 1805 */ 1806 for (cp = linker_path; *cp; cp = ep + 1) { 1807 /* find the end of this component */ 1808 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1809 result = linker_hints_lookup(cp, ep - cp, modname, 1810 modnamelen, verinfo); 1811 if (result != NULL) 1812 return (result); 1813 if (*ep == 0) 1814 break; 1815 } 1816 return (NULL); 1817} 1818 1819/* 1820 * Search for module in all directories listed in the linker_path. 1821 */ 1822static char * 1823linker_search_kld(const char *name) 1824{ 1825 char *cp, *ep, *result; 1826 int len; 1827 1828 /* qualified at all? */ 1829 if (index(name, '/')) 1830 return (linker_strdup(name)); 1831 1832 /* traverse the linker path */ 1833 len = strlen(name); 1834 for (ep = linker_path; *ep; ep++) { 1835 cp = ep; 1836 /* find the end of this component */ 1837 for (; *ep != 0 && *ep != ';'; ep++); 1838 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1839 if (result != NULL) 1840 return (result); 1841 } 1842 return (NULL); 1843} 1844 1845static const char * 1846linker_basename(const char *path) 1847{ 1848 const char *filename; 1849 1850 filename = rindex(path, '/'); 1851 if (filename == NULL) 1852 return path; 1853 if (filename[1]) 1854 filename++; 1855 return (filename); 1856} 1857 1858#ifdef HWPMC_HOOKS 1859 1860struct hwpmc_context { 1861 int nobjects; 1862 int nmappings; 1863 struct pmckern_map_in *kobase; 1864}; 1865 1866static int 1867linker_hwpmc_list_object(linker_file_t lf, void *arg) 1868{ 1869 struct hwpmc_context *hc; 1870 1871 hc = arg; 1872 1873 /* If we run out of mappings, fail. */ 1874 if (hc->nobjects >= hc->nmappings) 1875 return (1); 1876 1877 /* Save the info for this linker file. */ 1878 hc->kobase[hc->nobjects].pm_file = lf->filename; 1879 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address; 1880 hc->nobjects++; 1881 return (0); 1882} 1883 1884/* 1885 * Inform hwpmc about the set of kernel modules currently loaded. 1886 */ 1887void * 1888linker_hwpmc_list_objects(void) 1889{ 1890 struct hwpmc_context hc; 1891 1892 hc.nmappings = 15; /* a reasonable default */ 1893 1894 retry: 1895 /* allocate nmappings+1 entries */ 1896 MALLOC(hc.kobase, struct pmckern_map_in *, 1897 (hc.nmappings + 1) * sizeof(struct pmckern_map_in), M_LINKER, 1898 M_WAITOK | M_ZERO); 1899 1900 hc.nobjects = 0; 1901 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) { 1902 hc.nmappings = hc.nobjects; 1903 FREE(hc.kobase, M_LINKER); 1904 goto retry; 1905 } 1906 1907 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel " 1908 "objects?")); 1909 1910 /* The last entry of the malloced area comprises of all zeros. */ 1911 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL, 1912 ("linker_hwpmc_list_objects: last object not NULL")); 1913 1914 return ((void *)hc.kobase); 1915} 1916#endif 1917 1918/* 1919 * Find a file which contains given module and load it, if "parent" is not 1920 * NULL, register a reference to it. 1921 */ 1922static int 1923linker_load_module(const char *kldname, const char *modname, 1924 struct linker_file *parent, struct mod_depend *verinfo, 1925 struct linker_file **lfpp) 1926{ 1927 linker_file_t lfdep; 1928 const char *filename; 1929 char *pathname; 1930 int error; 1931 1932 KLD_LOCK_ASSERT(); 1933 if (modname == NULL) { 1934 /* 1935 * We have to load KLD 1936 */ 1937 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 1938 " is not NULL")); 1939 pathname = linker_search_kld(kldname); 1940 } else { 1941 if (modlist_lookup2(modname, verinfo) != NULL) 1942 return (EEXIST); 1943 if (kldname != NULL) 1944 pathname = linker_strdup(kldname); 1945 else if (rootvnode == NULL) 1946 pathname = NULL; 1947 else 1948 /* 1949 * Need to find a KLD with required module 1950 */ 1951 pathname = linker_search_module(modname, 1952 strlen(modname), verinfo); 1953 } 1954 if (pathname == NULL) 1955 return (ENOENT); 1956 1957 /* 1958 * Can't load more than one file with the same basename XXX: 1959 * Actually it should be possible to have multiple KLDs with 1960 * the same basename but different path because they can 1961 * provide different versions of the same modules. 1962 */ 1963 filename = linker_basename(pathname); 1964 if (linker_find_file_by_name(filename)) 1965 error = EEXIST; 1966 else do { 1967 error = linker_load_file(pathname, &lfdep); 1968 if (error) 1969 break; 1970 if (modname && verinfo && 1971 modlist_lookup2(modname, verinfo) == NULL) { 1972 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 1973 error = ENOENT; 1974 break; 1975 } 1976 if (parent) { 1977 error = linker_file_add_dependency(parent, lfdep); 1978 if (error) 1979 break; 1980 } 1981 if (lfpp) 1982 *lfpp = lfdep; 1983 } while (0); 1984 free(pathname, M_LINKER); 1985 return (error); 1986} 1987 1988/* 1989 * This routine is responsible for finding dependencies of userland initiated 1990 * kldload(2)'s of files. 1991 */ 1992int 1993linker_load_dependencies(linker_file_t lf) 1994{ 1995 linker_file_t lfdep; 1996 struct mod_metadata **start, **stop, **mdp, **nmdp; 1997 struct mod_metadata *mp, *nmp; 1998 struct mod_depend *verinfo; 1999 modlist_t mod; 2000 const char *modname, *nmodname; 2001 int ver, error = 0, count; 2002 2003 /* 2004 * All files are dependant on /kernel. 2005 */ 2006 KLD_LOCK_ASSERT(); 2007 if (linker_kernel_file) { 2008 linker_kernel_file->refs++; 2009 error = linker_file_add_dependency(lf, linker_kernel_file); 2010 if (error) 2011 return (error); 2012 } 2013 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2014 &count) != 0) 2015 return (0); 2016 for (mdp = start; mdp < stop; mdp++) { 2017 mp = *mdp; 2018 if (mp->md_type != MDT_VERSION) 2019 continue; 2020 modname = mp->md_cval; 2021 ver = ((struct mod_version *)mp->md_data)->mv_version; 2022 mod = modlist_lookup(modname, ver); 2023 if (mod != NULL) { 2024 printf("interface %s.%d already present in the KLD" 2025 " '%s'!\n", modname, ver, 2026 mod->container->filename); 2027 return (EEXIST); 2028 } 2029 } 2030 2031 for (mdp = start; mdp < stop; mdp++) { 2032 mp = *mdp; 2033 if (mp->md_type != MDT_DEPEND) 2034 continue; 2035 modname = mp->md_cval; 2036 verinfo = mp->md_data; 2037 nmodname = NULL; 2038 for (nmdp = start; nmdp < stop; nmdp++) { 2039 nmp = *nmdp; 2040 if (nmp->md_type != MDT_VERSION) 2041 continue; 2042 nmodname = nmp->md_cval; 2043 if (strcmp(modname, nmodname) == 0) 2044 break; 2045 } 2046 if (nmdp < stop)/* early exit, it's a self reference */ 2047 continue; 2048 mod = modlist_lookup2(modname, verinfo); 2049 if (mod) { /* woohoo, it's loaded already */ 2050 lfdep = mod->container; 2051 lfdep->refs++; 2052 error = linker_file_add_dependency(lf, lfdep); 2053 if (error) 2054 break; 2055 continue; 2056 } 2057 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2058 if (error) { 2059 printf("KLD %s: depends on %s - not available\n", 2060 lf->filename, modname); 2061 break; 2062 } 2063 } 2064 2065 if (error) 2066 return (error); 2067 linker_addmodules(lf, start, stop, 0); 2068 return (error); 2069} 2070 2071static int 2072sysctl_kern_function_list_iterate(const char *name, void *opaque) 2073{ 2074 struct sysctl_req *req; 2075 2076 req = opaque; 2077 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2078} 2079 2080/* 2081 * Export a nul-separated, double-nul-terminated list of all function names 2082 * in the kernel. 2083 */ 2084static int 2085sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2086{ 2087 linker_file_t lf; 2088 int error; 2089 2090#ifdef MAC 2091 error = mac_kld_check_stat(req->td->td_ucred); 2092 if (error) 2093 return (error); 2094#endif 2095 error = sysctl_wire_old_buffer(req, 0); 2096 if (error != 0) 2097 return (error); 2098 KLD_LOCK(); 2099 TAILQ_FOREACH(lf, &linker_files, link) { 2100 error = LINKER_EACH_FUNCTION_NAME(lf, 2101 sysctl_kern_function_list_iterate, req); 2102 if (error) { 2103 KLD_UNLOCK(); 2104 return (error); 2105 } 2106 } 2107 KLD_UNLOCK(); 2108 return (SYSCTL_OUT(req, "", 1)); 2109} 2110 2111SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 2112 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2113