kern_linker.c revision 191917
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 191917 2009-05-08 14:34:25Z 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#ifdef VIMAGE 996 /* Only the default vimage is permitted to kldload modules. */ 997 if (!IS_DEFAULT_VIMAGE(TD_TO_VIMAGE(td))) 998 return (EPERM); 999#endif 1000 1001 /* 1002 * It is possible that kldloaded module will attach a new ifnet, 1003 * so vnet context must be set when this ocurs. 1004 */ 1005 CURVNET_SET(TD_TO_VNET(td)); 1006 1007 /* 1008 * If file does not contain a qualified name or any dot in it 1009 * (kldname.ko, or kldname.ver.ko) treat it as an interface 1010 * name. 1011 */ 1012 if (index(file, '/') || index(file, '.')) { 1013 kldname = file; 1014 modname = NULL; 1015 } else { 1016 kldname = NULL; 1017 modname = file; 1018 } 1019 1020 KLD_LOCK(); 1021 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 1022 if (error) 1023 goto unlock; 1024#ifdef HWPMC_HOOKS 1025 pkm.pm_file = lf->filename; 1026 pkm.pm_address = (uintptr_t) lf->address; 1027 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm); 1028#endif 1029 lf->userrefs++; 1030 if (fileid != NULL) 1031 *fileid = lf->id; 1032unlock: 1033 KLD_UNLOCK(); 1034 CURVNET_RESTORE(); 1035 return (error); 1036} 1037 1038int 1039kldload(struct thread *td, struct kldload_args *uap) 1040{ 1041 char *pathname = NULL; 1042 int error, fileid; 1043 1044 td->td_retval[0] = -1; 1045 1046 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1047 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1048 if (error == 0) { 1049 error = kern_kldload(td, pathname, &fileid); 1050 if (error == 0) 1051 td->td_retval[0] = fileid; 1052 } 1053 free(pathname, M_TEMP); 1054 return (error); 1055} 1056 1057int 1058kern_kldunload(struct thread *td, int fileid, int flags) 1059{ 1060#ifdef HWPMC_HOOKS 1061 struct pmckern_map_out pkm; 1062#endif 1063 linker_file_t lf; 1064 int error = 0; 1065 1066 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1067 return (error); 1068 1069 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1070 return (error); 1071 1072#ifdef VIMAGE 1073 /* Only the default vimage is permitted to kldunload modules. */ 1074 if (!IS_DEFAULT_VIMAGE(TD_TO_VIMAGE(td))) 1075 return (EPERM); 1076#endif 1077 1078 CURVNET_SET(TD_TO_VNET(td)); 1079 KLD_LOCK(); 1080 lf = linker_find_file_by_id(fileid); 1081 if (lf) { 1082 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1083 1084 /* Check if there are DTrace probes enabled on this file. */ 1085 if (lf->nenabled > 0) { 1086 printf("kldunload: attempt to unload file that has" 1087 " DTrace probes enabled\n"); 1088 error = EBUSY; 1089 } else if (lf->userrefs == 0) { 1090 /* 1091 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1092 */ 1093 printf("kldunload: attempt to unload file that was" 1094 " loaded by the kernel\n"); 1095 error = EBUSY; 1096 } else { 1097#ifdef HWPMC_HOOKS 1098 /* Save data needed by hwpmc(4) before unloading. */ 1099 pkm.pm_address = (uintptr_t) lf->address; 1100 pkm.pm_size = lf->size; 1101#endif 1102 lf->userrefs--; 1103 error = linker_file_unload(lf, flags); 1104 if (error) 1105 lf->userrefs++; 1106 } 1107 } else 1108 error = ENOENT; 1109 1110#ifdef HWPMC_HOOKS 1111 if (error == 0) 1112 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm); 1113#endif 1114 KLD_UNLOCK(); 1115 CURVNET_RESTORE(); 1116 return (error); 1117} 1118 1119int 1120kldunload(struct thread *td, struct kldunload_args *uap) 1121{ 1122 1123 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1124} 1125 1126int 1127kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1128{ 1129 1130 if (uap->flags != LINKER_UNLOAD_NORMAL && 1131 uap->flags != LINKER_UNLOAD_FORCE) 1132 return (EINVAL); 1133 return (kern_kldunload(td, uap->fileid, uap->flags)); 1134} 1135 1136int 1137kldfind(struct thread *td, struct kldfind_args *uap) 1138{ 1139 char *pathname; 1140 const char *filename; 1141 linker_file_t lf; 1142 int error; 1143 1144#ifdef MAC 1145 error = mac_kld_check_stat(td->td_ucred); 1146 if (error) 1147 return (error); 1148#endif 1149 1150 td->td_retval[0] = -1; 1151 1152 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1153 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1154 goto out; 1155 1156 filename = linker_basename(pathname); 1157 KLD_LOCK(); 1158 lf = linker_find_file_by_name(filename); 1159 if (lf) 1160 td->td_retval[0] = lf->id; 1161 else 1162 error = ENOENT; 1163 KLD_UNLOCK(); 1164out: 1165 free(pathname, M_TEMP); 1166 return (error); 1167} 1168 1169int 1170kldnext(struct thread *td, struct kldnext_args *uap) 1171{ 1172 linker_file_t lf; 1173 int error = 0; 1174 1175#ifdef MAC 1176 error = mac_kld_check_stat(td->td_ucred); 1177 if (error) 1178 return (error); 1179#endif 1180 1181 KLD_LOCK(); 1182 if (uap->fileid == 0) 1183 lf = TAILQ_FIRST(&linker_files); 1184 else { 1185 lf = linker_find_file_by_id(uap->fileid); 1186 if (lf == NULL) { 1187 error = ENOENT; 1188 goto out; 1189 } 1190 lf = TAILQ_NEXT(lf, link); 1191 } 1192 1193 /* Skip partially loaded files. */ 1194 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1195 lf = TAILQ_NEXT(lf, link); 1196 1197 if (lf) 1198 td->td_retval[0] = lf->id; 1199 else 1200 td->td_retval[0] = 0; 1201out: 1202 KLD_UNLOCK(); 1203 return (error); 1204} 1205 1206int 1207kldstat(struct thread *td, struct kldstat_args *uap) 1208{ 1209 struct kld_file_stat stat; 1210 linker_file_t lf; 1211 int error, namelen, version, version_num; 1212 1213 /* 1214 * Check the version of the user's structure. 1215 */ 1216 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) != 0) 1217 return (error); 1218 if (version == sizeof(struct kld_file_stat_1)) 1219 version_num = 1; 1220 else if (version == sizeof(struct kld_file_stat)) 1221 version_num = 2; 1222 else 1223 return (EINVAL); 1224 1225#ifdef MAC 1226 error = mac_kld_check_stat(td->td_ucred); 1227 if (error) 1228 return (error); 1229#endif 1230 1231 KLD_LOCK(); 1232 lf = linker_find_file_by_id(uap->fileid); 1233 if (lf == NULL) { 1234 KLD_UNLOCK(); 1235 return (ENOENT); 1236 } 1237 1238 /* Version 1 fields: */ 1239 namelen = strlen(lf->filename) + 1; 1240 if (namelen > MAXPATHLEN) 1241 namelen = MAXPATHLEN; 1242 bcopy(lf->filename, &stat.name[0], namelen); 1243 stat.refs = lf->refs; 1244 stat.id = lf->id; 1245 stat.address = lf->address; 1246 stat.size = lf->size; 1247 if (version_num > 1) { 1248 /* Version 2 fields: */ 1249 namelen = strlen(lf->pathname) + 1; 1250 if (namelen > MAXPATHLEN) 1251 namelen = MAXPATHLEN; 1252 bcopy(lf->pathname, &stat.pathname[0], namelen); 1253 } 1254 KLD_UNLOCK(); 1255 1256 td->td_retval[0] = 0; 1257 1258 return (copyout(&stat, uap->stat, version)); 1259} 1260 1261int 1262kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1263{ 1264 linker_file_t lf; 1265 module_t mp; 1266 int error = 0; 1267 1268#ifdef MAC 1269 error = mac_kld_check_stat(td->td_ucred); 1270 if (error) 1271 return (error); 1272#endif 1273 1274 KLD_LOCK(); 1275 lf = linker_find_file_by_id(uap->fileid); 1276 if (lf) { 1277 MOD_SLOCK; 1278 mp = TAILQ_FIRST(&lf->modules); 1279 if (mp != NULL) 1280 td->td_retval[0] = module_getid(mp); 1281 else 1282 td->td_retval[0] = 0; 1283 MOD_SUNLOCK; 1284 } else 1285 error = ENOENT; 1286 KLD_UNLOCK(); 1287 return (error); 1288} 1289 1290int 1291kldsym(struct thread *td, struct kldsym_args *uap) 1292{ 1293 char *symstr = NULL; 1294 c_linker_sym_t sym; 1295 linker_symval_t symval; 1296 linker_file_t lf; 1297 struct kld_sym_lookup lookup; 1298 int error = 0; 1299 1300#ifdef MAC 1301 error = mac_kld_check_stat(td->td_ucred); 1302 if (error) 1303 return (error); 1304#endif 1305 1306 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1307 return (error); 1308 if (lookup.version != sizeof(lookup) || 1309 uap->cmd != KLDSYM_LOOKUP) 1310 return (EINVAL); 1311 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1312 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1313 goto out; 1314 KLD_LOCK(); 1315 if (uap->fileid != 0) { 1316 lf = linker_find_file_by_id(uap->fileid); 1317 if (lf == NULL) 1318 error = ENOENT; 1319 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1320 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1321 lookup.symvalue = (uintptr_t) symval.value; 1322 lookup.symsize = symval.size; 1323 error = copyout(&lookup, uap->data, sizeof(lookup)); 1324 } else 1325 error = ENOENT; 1326 } else { 1327 TAILQ_FOREACH(lf, &linker_files, link) { 1328 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1329 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1330 lookup.symvalue = (uintptr_t)symval.value; 1331 lookup.symsize = symval.size; 1332 error = copyout(&lookup, uap->data, 1333 sizeof(lookup)); 1334 break; 1335 } 1336 } 1337#ifndef VIMAGE_GLOBALS 1338 /* 1339 * If the symbol is not found in global namespace, 1340 * try to look it up in the current vimage namespace. 1341 */ 1342 if (lf == NULL) { 1343 CURVNET_SET(TD_TO_VNET(td)); 1344 error = vi_symlookup(&lookup, symstr); 1345 CURVNET_RESTORE(); 1346 if (error == 0) 1347 error = copyout(&lookup, uap->data, 1348 sizeof(lookup)); 1349 } 1350#else 1351 if (lf == NULL) 1352 error = ENOENT; 1353#endif 1354 } 1355 KLD_UNLOCK(); 1356out: 1357 free(symstr, M_TEMP); 1358 return (error); 1359} 1360 1361/* 1362 * Preloaded module support 1363 */ 1364 1365static modlist_t 1366modlist_lookup(const char *name, int ver) 1367{ 1368 modlist_t mod; 1369 1370 TAILQ_FOREACH(mod, &found_modules, link) { 1371 if (strcmp(mod->name, name) == 0 && 1372 (ver == 0 || mod->version == ver)) 1373 return (mod); 1374 } 1375 return (NULL); 1376} 1377 1378static modlist_t 1379modlist_lookup2(const char *name, struct mod_depend *verinfo) 1380{ 1381 modlist_t mod, bestmod; 1382 int ver; 1383 1384 if (verinfo == NULL) 1385 return (modlist_lookup(name, 0)); 1386 bestmod = NULL; 1387 TAILQ_FOREACH(mod, &found_modules, link) { 1388 if (strcmp(mod->name, name) != 0) 1389 continue; 1390 ver = mod->version; 1391 if (ver == verinfo->md_ver_preferred) 1392 return (mod); 1393 if (ver >= verinfo->md_ver_minimum && 1394 ver <= verinfo->md_ver_maximum && 1395 (bestmod == NULL || ver > bestmod->version)) 1396 bestmod = mod; 1397 } 1398 return (bestmod); 1399} 1400 1401static modlist_t 1402modlist_newmodule(const char *modname, int version, linker_file_t container) 1403{ 1404 modlist_t mod; 1405 1406 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1407 if (mod == NULL) 1408 panic("no memory for module list"); 1409 mod->container = container; 1410 mod->name = modname; 1411 mod->version = version; 1412 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1413 return (mod); 1414} 1415 1416static void 1417linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1418 struct mod_metadata **stop, int preload) 1419{ 1420 struct mod_metadata *mp, **mdp; 1421 const char *modname; 1422 int ver; 1423 1424 for (mdp = start; mdp < stop; mdp++) { 1425 mp = *mdp; 1426 if (mp->md_type != MDT_VERSION) 1427 continue; 1428 modname = mp->md_cval; 1429 ver = ((struct mod_version *)mp->md_data)->mv_version; 1430 if (modlist_lookup(modname, ver) != NULL) { 1431 printf("module %s already present!\n", modname); 1432 /* XXX what can we do? this is a build error. :-( */ 1433 continue; 1434 } 1435 modlist_newmodule(modname, ver, lf); 1436 } 1437} 1438 1439static void 1440linker_preload(void *arg) 1441{ 1442 caddr_t modptr; 1443 const char *modname, *nmodname; 1444 char *modtype; 1445 linker_file_t lf, nlf; 1446 linker_class_t lc; 1447 int error; 1448 linker_file_list_t loaded_files; 1449 linker_file_list_t depended_files; 1450 struct mod_metadata *mp, *nmp; 1451 struct mod_metadata **start, **stop, **mdp, **nmdp; 1452 struct mod_depend *verinfo; 1453 int nver; 1454 int resolves; 1455 modlist_t mod; 1456 struct sysinit **si_start, **si_stop; 1457 1458 TAILQ_INIT(&loaded_files); 1459 TAILQ_INIT(&depended_files); 1460 TAILQ_INIT(&found_modules); 1461 error = 0; 1462 1463 modptr = NULL; 1464 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1465 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1466 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1467 if (modname == NULL) { 1468 printf("Preloaded module at %p does not have a" 1469 " name!\n", modptr); 1470 continue; 1471 } 1472 if (modtype == NULL) { 1473 printf("Preloaded module at %p does not have a type!\n", 1474 modptr); 1475 continue; 1476 } 1477 if (bootverbose) 1478 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1479 modptr); 1480 lf = NULL; 1481 TAILQ_FOREACH(lc, &classes, link) { 1482 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1483 if (!error) 1484 break; 1485 lf = NULL; 1486 } 1487 if (lf) 1488 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1489 } 1490 1491 /* 1492 * First get a list of stuff in the kernel. 1493 */ 1494 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1495 &stop, NULL) == 0) 1496 linker_addmodules(linker_kernel_file, start, stop, 1); 1497 1498 /* 1499 * This is a once-off kinky bubble sort to resolve relocation 1500 * dependency requirements. 1501 */ 1502restart: 1503 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1504 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1505 &stop, NULL); 1506 /* 1507 * First, look to see if we would successfully link with this 1508 * stuff. 1509 */ 1510 resolves = 1; /* unless we know otherwise */ 1511 if (!error) { 1512 for (mdp = start; mdp < stop; mdp++) { 1513 mp = *mdp; 1514 if (mp->md_type != MDT_DEPEND) 1515 continue; 1516 modname = mp->md_cval; 1517 verinfo = mp->md_data; 1518 for (nmdp = start; nmdp < stop; nmdp++) { 1519 nmp = *nmdp; 1520 if (nmp->md_type != MDT_VERSION) 1521 continue; 1522 nmodname = nmp->md_cval; 1523 if (strcmp(modname, nmodname) == 0) 1524 break; 1525 } 1526 if (nmdp < stop) /* it's a self reference */ 1527 continue; 1528 1529 /* 1530 * ok, the module isn't here yet, we 1531 * are not finished 1532 */ 1533 if (modlist_lookup2(modname, verinfo) == NULL) 1534 resolves = 0; 1535 } 1536 } 1537 /* 1538 * OK, if we found our modules, we can link. So, "provide" 1539 * the modules inside and add it to the end of the link order 1540 * list. 1541 */ 1542 if (resolves) { 1543 if (!error) { 1544 for (mdp = start; mdp < stop; mdp++) { 1545 mp = *mdp; 1546 if (mp->md_type != MDT_VERSION) 1547 continue; 1548 modname = mp->md_cval; 1549 nver = ((struct mod_version *) 1550 mp->md_data)->mv_version; 1551 if (modlist_lookup(modname, 1552 nver) != NULL) { 1553 printf("module %s already" 1554 " present!\n", modname); 1555 TAILQ_REMOVE(&loaded_files, 1556 lf, loaded); 1557 linker_file_unload(lf, 1558 LINKER_UNLOAD_FORCE); 1559 /* we changed tailq next ptr */ 1560 goto restart; 1561 } 1562 modlist_newmodule(modname, nver, lf); 1563 } 1564 } 1565 TAILQ_REMOVE(&loaded_files, lf, loaded); 1566 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1567 /* 1568 * Since we provided modules, we need to restart the 1569 * sort so that the previous files that depend on us 1570 * have a chance. Also, we've busted the tailq next 1571 * pointer with the REMOVE. 1572 */ 1573 goto restart; 1574 } 1575 } 1576 1577 /* 1578 * At this point, we check to see what could not be resolved.. 1579 */ 1580 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1581 TAILQ_REMOVE(&loaded_files, lf, loaded); 1582 printf("KLD file %s is missing dependencies\n", lf->filename); 1583 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1584 } 1585 1586 /* 1587 * We made it. Finish off the linking in the order we determined. 1588 */ 1589 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1590 if (linker_kernel_file) { 1591 linker_kernel_file->refs++; 1592 error = linker_file_add_dependency(lf, 1593 linker_kernel_file); 1594 if (error) 1595 panic("cannot add dependency"); 1596 } 1597 lf->userrefs++; /* so we can (try to) kldunload it */ 1598 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1599 &stop, NULL); 1600 if (!error) { 1601 for (mdp = start; mdp < stop; mdp++) { 1602 mp = *mdp; 1603 if (mp->md_type != MDT_DEPEND) 1604 continue; 1605 modname = mp->md_cval; 1606 verinfo = mp->md_data; 1607 mod = modlist_lookup2(modname, verinfo); 1608 /* Don't count self-dependencies */ 1609 if (lf == mod->container) 1610 continue; 1611 mod->container->refs++; 1612 error = linker_file_add_dependency(lf, 1613 mod->container); 1614 if (error) 1615 panic("cannot add dependency"); 1616 } 1617 } 1618 /* 1619 * Now do relocation etc using the symbol search paths 1620 * established by the dependencies 1621 */ 1622 error = LINKER_LINK_PRELOAD_FINISH(lf); 1623 if (error) { 1624 TAILQ_REMOVE(&depended_files, lf, loaded); 1625 printf("KLD file %s - could not finalize loading\n", 1626 lf->filename); 1627 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1628 continue; 1629 } 1630 linker_file_register_modules(lf); 1631 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1632 &si_stop, NULL) == 0) 1633 sysinit_add(si_start, si_stop); 1634 linker_file_register_sysctls(lf); 1635 lf->flags |= LINKER_FILE_LINKED; 1636 } 1637 /* woohoo! we made it! */ 1638} 1639 1640SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1641 1642/* 1643 * Search for a not-loaded module by name. 1644 * 1645 * Modules may be found in the following locations: 1646 * 1647 * - preloaded (result is just the module name) - on disk (result is full path 1648 * to module) 1649 * 1650 * If the module name is qualified in any way (contains path, etc.) the we 1651 * simply return a copy of it. 1652 * 1653 * The search path can be manipulated via sysctl. Note that we use the ';' 1654 * character as a separator to be consistent with the bootloader. 1655 */ 1656 1657static char linker_hintfile[] = "linker.hints"; 1658static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1659 1660SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1661 sizeof(linker_path), "module load search path"); 1662 1663TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1664 1665static char *linker_ext_list[] = { 1666 "", 1667 ".ko", 1668 NULL 1669}; 1670 1671/* 1672 * Check if file actually exists either with or without extension listed in 1673 * the linker_ext_list. (probably should be generic for the rest of the 1674 * kernel) 1675 */ 1676static char * 1677linker_lookup_file(const char *path, int pathlen, const char *name, 1678 int namelen, struct vattr *vap) 1679{ 1680 struct nameidata nd; 1681 struct thread *td = curthread; /* XXX */ 1682 char *result, **cpp, *sep; 1683 int error, len, extlen, reclen, flags, vfslocked; 1684 enum vtype type; 1685 1686 extlen = 0; 1687 for (cpp = linker_ext_list; *cpp; cpp++) { 1688 len = strlen(*cpp); 1689 if (len > extlen) 1690 extlen = len; 1691 } 1692 extlen++; /* trailing '\0' */ 1693 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1694 1695 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1696 result = malloc(reclen, M_LINKER, M_WAITOK); 1697 for (cpp = linker_ext_list; *cpp; cpp++) { 1698 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1699 namelen, name, *cpp); 1700 /* 1701 * Attempt to open the file, and return the path if 1702 * we succeed and it's a regular file. 1703 */ 1704 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td); 1705 flags = FREAD; 1706 error = vn_open(&nd, &flags, 0, NULL); 1707 if (error == 0) { 1708 vfslocked = NDHASGIANT(&nd); 1709 NDFREE(&nd, NDF_ONLY_PNBUF); 1710 type = nd.ni_vp->v_type; 1711 if (vap) 1712 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1713 VOP_UNLOCK(nd.ni_vp, 0); 1714 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1715 VFS_UNLOCK_GIANT(vfslocked); 1716 if (type == VREG) 1717 return (result); 1718 } 1719 } 1720 free(result, M_LINKER); 1721 return (NULL); 1722} 1723 1724#define INT_ALIGN(base, ptr) ptr = \ 1725 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1726 1727/* 1728 * Lookup KLD which contains requested module in the "linker.hints" file. If 1729 * version specification is available, then try to find the best KLD. 1730 * Otherwise just find the latest one. 1731 */ 1732static char * 1733linker_hints_lookup(const char *path, int pathlen, const char *modname, 1734 int modnamelen, struct mod_depend *verinfo) 1735{ 1736 struct thread *td = curthread; /* XXX */ 1737 struct ucred *cred = td ? td->td_ucred : NULL; 1738 struct nameidata nd; 1739 struct vattr vattr, mattr; 1740 u_char *hints = NULL; 1741 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1742 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1743 int vfslocked = 0; 1744 1745 result = NULL; 1746 bestver = found = 0; 1747 1748 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1749 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1750 strlen(sep) + 1; 1751 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1752 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1753 linker_hintfile); 1754 1755 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1756 flags = FREAD; 1757 error = vn_open(&nd, &flags, 0, NULL); 1758 if (error) 1759 goto bad; 1760 vfslocked = NDHASGIANT(&nd); 1761 NDFREE(&nd, NDF_ONLY_PNBUF); 1762 if (nd.ni_vp->v_type != VREG) 1763 goto bad; 1764 best = cp = NULL; 1765 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1766 if (error) 1767 goto bad; 1768 /* 1769 * XXX: we need to limit this number to some reasonable value 1770 */ 1771 if (vattr.va_size > 100 * 1024) { 1772 printf("hints file too large %ld\n", (long)vattr.va_size); 1773 goto bad; 1774 } 1775 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1776 if (hints == NULL) 1777 goto bad; 1778 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1779 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1780 if (error) 1781 goto bad; 1782 VOP_UNLOCK(nd.ni_vp, 0); 1783 vn_close(nd.ni_vp, FREAD, cred, td); 1784 VFS_UNLOCK_GIANT(vfslocked); 1785 nd.ni_vp = NULL; 1786 if (reclen != 0) { 1787 printf("can't read %d\n", reclen); 1788 goto bad; 1789 } 1790 intp = (int *)hints; 1791 ival = *intp++; 1792 if (ival != LINKER_HINTS_VERSION) { 1793 printf("hints file version mismatch %d\n", ival); 1794 goto bad; 1795 } 1796 bufend = hints + vattr.va_size; 1797 recptr = (u_char *)intp; 1798 clen = blen = 0; 1799 while (recptr < bufend && !found) { 1800 intp = (int *)recptr; 1801 reclen = *intp++; 1802 ival = *intp++; 1803 cp = (char *)intp; 1804 switch (ival) { 1805 case MDT_VERSION: 1806 clen = *cp++; 1807 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1808 break; 1809 cp += clen; 1810 INT_ALIGN(hints, cp); 1811 ival = *(int *)cp; 1812 cp += sizeof(int); 1813 clen = *cp++; 1814 if (verinfo == NULL || 1815 ival == verinfo->md_ver_preferred) { 1816 found = 1; 1817 break; 1818 } 1819 if (ival >= verinfo->md_ver_minimum && 1820 ival <= verinfo->md_ver_maximum && 1821 ival > bestver) { 1822 bestver = ival; 1823 best = cp; 1824 blen = clen; 1825 } 1826 break; 1827 default: 1828 break; 1829 } 1830 recptr += reclen + sizeof(int); 1831 } 1832 /* 1833 * Finally check if KLD is in the place 1834 */ 1835 if (found) 1836 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1837 else if (best) 1838 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1839 1840 /* 1841 * KLD is newer than hints file. What we should do now? 1842 */ 1843 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1844 printf("warning: KLD '%s' is newer than the linker.hints" 1845 " file\n", result); 1846bad: 1847 free(pathbuf, M_LINKER); 1848 if (hints) 1849 free(hints, M_TEMP); 1850 if (nd.ni_vp != NULL) { 1851 VOP_UNLOCK(nd.ni_vp, 0); 1852 vn_close(nd.ni_vp, FREAD, cred, td); 1853 VFS_UNLOCK_GIANT(vfslocked); 1854 } 1855 /* 1856 * If nothing found or hints is absent - fallback to the old 1857 * way by using "kldname[.ko]" as module name. 1858 */ 1859 if (!found && !bestver && result == NULL) 1860 result = linker_lookup_file(path, pathlen, modname, 1861 modnamelen, NULL); 1862 return (result); 1863} 1864 1865/* 1866 * Lookup KLD which contains requested module in the all directories. 1867 */ 1868static char * 1869linker_search_module(const char *modname, int modnamelen, 1870 struct mod_depend *verinfo) 1871{ 1872 char *cp, *ep, *result; 1873 1874 /* 1875 * traverse the linker path 1876 */ 1877 for (cp = linker_path; *cp; cp = ep + 1) { 1878 /* find the end of this component */ 1879 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1880 result = linker_hints_lookup(cp, ep - cp, modname, 1881 modnamelen, verinfo); 1882 if (result != NULL) 1883 return (result); 1884 if (*ep == 0) 1885 break; 1886 } 1887 return (NULL); 1888} 1889 1890/* 1891 * Search for module in all directories listed in the linker_path. 1892 */ 1893static char * 1894linker_search_kld(const char *name) 1895{ 1896 char *cp, *ep, *result; 1897 int len; 1898 1899 /* qualified at all? */ 1900 if (index(name, '/')) 1901 return (linker_strdup(name)); 1902 1903 /* traverse the linker path */ 1904 len = strlen(name); 1905 for (ep = linker_path; *ep; ep++) { 1906 cp = ep; 1907 /* find the end of this component */ 1908 for (; *ep != 0 && *ep != ';'; ep++); 1909 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1910 if (result != NULL) 1911 return (result); 1912 } 1913 return (NULL); 1914} 1915 1916static const char * 1917linker_basename(const char *path) 1918{ 1919 const char *filename; 1920 1921 filename = rindex(path, '/'); 1922 if (filename == NULL) 1923 return path; 1924 if (filename[1]) 1925 filename++; 1926 return (filename); 1927} 1928 1929#ifdef HWPMC_HOOKS 1930 1931struct hwpmc_context { 1932 int nobjects; 1933 int nmappings; 1934 struct pmckern_map_in *kobase; 1935}; 1936 1937static int 1938linker_hwpmc_list_object(linker_file_t lf, void *arg) 1939{ 1940 struct hwpmc_context *hc; 1941 1942 hc = arg; 1943 1944 /* If we run out of mappings, fail. */ 1945 if (hc->nobjects >= hc->nmappings) 1946 return (1); 1947 1948 /* Save the info for this linker file. */ 1949 hc->kobase[hc->nobjects].pm_file = lf->filename; 1950 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address; 1951 hc->nobjects++; 1952 return (0); 1953} 1954 1955/* 1956 * Inform hwpmc about the set of kernel modules currently loaded. 1957 */ 1958void * 1959linker_hwpmc_list_objects(void) 1960{ 1961 struct hwpmc_context hc; 1962 1963 hc.nmappings = 15; /* a reasonable default */ 1964 1965 retry: 1966 /* allocate nmappings+1 entries */ 1967 hc.kobase = malloc((hc.nmappings + 1) * sizeof(struct pmckern_map_in), 1968 M_LINKER, M_WAITOK | M_ZERO); 1969 1970 hc.nobjects = 0; 1971 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) { 1972 hc.nmappings = hc.nobjects; 1973 free(hc.kobase, M_LINKER); 1974 goto retry; 1975 } 1976 1977 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel " 1978 "objects?")); 1979 1980 /* The last entry of the malloced area comprises of all zeros. */ 1981 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL, 1982 ("linker_hwpmc_list_objects: last object not NULL")); 1983 1984 return ((void *)hc.kobase); 1985} 1986#endif 1987 1988/* 1989 * Find a file which contains given module and load it, if "parent" is not 1990 * NULL, register a reference to it. 1991 */ 1992static int 1993linker_load_module(const char *kldname, const char *modname, 1994 struct linker_file *parent, struct mod_depend *verinfo, 1995 struct linker_file **lfpp) 1996{ 1997 linker_file_t lfdep; 1998 const char *filename; 1999 char *pathname; 2000 int error; 2001 2002 KLD_LOCK_ASSERT(); 2003 if (modname == NULL) { 2004 /* 2005 * We have to load KLD 2006 */ 2007 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 2008 " is not NULL")); 2009 pathname = linker_search_kld(kldname); 2010 } else { 2011 if (modlist_lookup2(modname, verinfo) != NULL) 2012 return (EEXIST); 2013 if (kldname != NULL) 2014 pathname = linker_strdup(kldname); 2015 else if (rootvnode == NULL) 2016 pathname = NULL; 2017 else 2018 /* 2019 * Need to find a KLD with required module 2020 */ 2021 pathname = linker_search_module(modname, 2022 strlen(modname), verinfo); 2023 } 2024 if (pathname == NULL) 2025 return (ENOENT); 2026 2027 /* 2028 * Can't load more than one file with the same basename XXX: 2029 * Actually it should be possible to have multiple KLDs with 2030 * the same basename but different path because they can 2031 * provide different versions of the same modules. 2032 */ 2033 filename = linker_basename(pathname); 2034 if (linker_find_file_by_name(filename)) 2035 error = EEXIST; 2036 else do { 2037 error = linker_load_file(pathname, &lfdep); 2038 if (error) 2039 break; 2040 if (modname && verinfo && 2041 modlist_lookup2(modname, verinfo) == NULL) { 2042 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2043 error = ENOENT; 2044 break; 2045 } 2046 if (parent) { 2047 error = linker_file_add_dependency(parent, lfdep); 2048 if (error) 2049 break; 2050 } 2051 if (lfpp) 2052 *lfpp = lfdep; 2053 } while (0); 2054 free(pathname, M_LINKER); 2055 return (error); 2056} 2057 2058/* 2059 * This routine is responsible for finding dependencies of userland initiated 2060 * kldload(2)'s of files. 2061 */ 2062int 2063linker_load_dependencies(linker_file_t lf) 2064{ 2065 linker_file_t lfdep; 2066 struct mod_metadata **start, **stop, **mdp, **nmdp; 2067 struct mod_metadata *mp, *nmp; 2068 struct mod_depend *verinfo; 2069 modlist_t mod; 2070 const char *modname, *nmodname; 2071 int ver, error = 0, count; 2072 2073 /* 2074 * All files are dependant on /kernel. 2075 */ 2076 KLD_LOCK_ASSERT(); 2077 if (linker_kernel_file) { 2078 linker_kernel_file->refs++; 2079 error = linker_file_add_dependency(lf, linker_kernel_file); 2080 if (error) 2081 return (error); 2082 } 2083 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2084 &count) != 0) 2085 return (0); 2086 for (mdp = start; mdp < stop; mdp++) { 2087 mp = *mdp; 2088 if (mp->md_type != MDT_VERSION) 2089 continue; 2090 modname = mp->md_cval; 2091 ver = ((struct mod_version *)mp->md_data)->mv_version; 2092 mod = modlist_lookup(modname, ver); 2093 if (mod != NULL) { 2094 printf("interface %s.%d already present in the KLD" 2095 " '%s'!\n", modname, ver, 2096 mod->container->filename); 2097 return (EEXIST); 2098 } 2099 } 2100 2101 for (mdp = start; mdp < stop; mdp++) { 2102 mp = *mdp; 2103 if (mp->md_type != MDT_DEPEND) 2104 continue; 2105 modname = mp->md_cval; 2106 verinfo = mp->md_data; 2107 nmodname = NULL; 2108 for (nmdp = start; nmdp < stop; nmdp++) { 2109 nmp = *nmdp; 2110 if (nmp->md_type != MDT_VERSION) 2111 continue; 2112 nmodname = nmp->md_cval; 2113 if (strcmp(modname, nmodname) == 0) 2114 break; 2115 } 2116 if (nmdp < stop)/* early exit, it's a self reference */ 2117 continue; 2118 mod = modlist_lookup2(modname, verinfo); 2119 if (mod) { /* woohoo, it's loaded already */ 2120 lfdep = mod->container; 2121 lfdep->refs++; 2122 error = linker_file_add_dependency(lf, lfdep); 2123 if (error) 2124 break; 2125 continue; 2126 } 2127 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2128 if (error) { 2129 printf("KLD %s: depends on %s - not available\n", 2130 lf->filename, modname); 2131 break; 2132 } 2133 } 2134 2135 if (error) 2136 return (error); 2137 linker_addmodules(lf, start, stop, 0); 2138 return (error); 2139} 2140 2141static int 2142sysctl_kern_function_list_iterate(const char *name, void *opaque) 2143{ 2144 struct sysctl_req *req; 2145 2146 req = opaque; 2147 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2148} 2149 2150/* 2151 * Export a nul-separated, double-nul-terminated list of all function names 2152 * in the kernel. 2153 */ 2154static int 2155sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2156{ 2157 linker_file_t lf; 2158 int error; 2159 2160#ifdef MAC 2161 error = mac_kld_check_stat(req->td->td_ucred); 2162 if (error) 2163 return (error); 2164#endif 2165 error = sysctl_wire_old_buffer(req, 0); 2166 if (error != 0) 2167 return (error); 2168 KLD_LOCK(); 2169 TAILQ_FOREACH(lf, &linker_files, link) { 2170 error = LINKER_EACH_FUNCTION_NAME(lf, 2171 sysctl_kern_function_list_iterate, req); 2172 if (error) { 2173 KLD_UNLOCK(); 2174 return (error); 2175 } 2176 } 2177 KLD_UNLOCK(); 2178 return (SYSCTL_OUT(req, "", 1)); 2179} 2180 2181SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 2182 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2183