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