kern_linker.c revision 195699
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 195699 2009-07-14 22:48:30Z rwatson $"); 29 30#include "opt_ddb.h" 31#include "opt_hwpmc_hooks.h" 32 33#include <sys/param.h> 34#include <sys/kernel.h> 35#include <sys/systm.h> 36#include <sys/malloc.h> 37#include <sys/sysproto.h> 38#include <sys/sysent.h> 39#include <sys/priv.h> 40#include <sys/proc.h> 41#include <sys/lock.h> 42#include <sys/mutex.h> 43#include <sys/sx.h> 44#include <sys/module.h> 45#include <sys/mount.h> 46#include <sys/linker.h> 47#include <sys/fcntl.h> 48#include <sys/jail.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 (prison0.pr_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 (prison0.pr_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 if (lf == NULL) 1338 error = ENOENT; 1339 } 1340 KLD_UNLOCK(); 1341out: 1342 free(symstr, M_TEMP); 1343 return (error); 1344} 1345 1346/* 1347 * Preloaded module support 1348 */ 1349 1350static modlist_t 1351modlist_lookup(const char *name, int ver) 1352{ 1353 modlist_t mod; 1354 1355 TAILQ_FOREACH(mod, &found_modules, link) { 1356 if (strcmp(mod->name, name) == 0 && 1357 (ver == 0 || mod->version == ver)) 1358 return (mod); 1359 } 1360 return (NULL); 1361} 1362 1363static modlist_t 1364modlist_lookup2(const char *name, struct mod_depend *verinfo) 1365{ 1366 modlist_t mod, bestmod; 1367 int ver; 1368 1369 if (verinfo == NULL) 1370 return (modlist_lookup(name, 0)); 1371 bestmod = NULL; 1372 TAILQ_FOREACH(mod, &found_modules, link) { 1373 if (strcmp(mod->name, name) != 0) 1374 continue; 1375 ver = mod->version; 1376 if (ver == verinfo->md_ver_preferred) 1377 return (mod); 1378 if (ver >= verinfo->md_ver_minimum && 1379 ver <= verinfo->md_ver_maximum && 1380 (bestmod == NULL || ver > bestmod->version)) 1381 bestmod = mod; 1382 } 1383 return (bestmod); 1384} 1385 1386static modlist_t 1387modlist_newmodule(const char *modname, int version, linker_file_t container) 1388{ 1389 modlist_t mod; 1390 1391 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1392 if (mod == NULL) 1393 panic("no memory for module list"); 1394 mod->container = container; 1395 mod->name = modname; 1396 mod->version = version; 1397 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1398 return (mod); 1399} 1400 1401static void 1402linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1403 struct mod_metadata **stop, int preload) 1404{ 1405 struct mod_metadata *mp, **mdp; 1406 const char *modname; 1407 int ver; 1408 1409 for (mdp = start; mdp < stop; mdp++) { 1410 mp = *mdp; 1411 if (mp->md_type != MDT_VERSION) 1412 continue; 1413 modname = mp->md_cval; 1414 ver = ((struct mod_version *)mp->md_data)->mv_version; 1415 if (modlist_lookup(modname, ver) != NULL) { 1416 printf("module %s already present!\n", modname); 1417 /* XXX what can we do? this is a build error. :-( */ 1418 continue; 1419 } 1420 modlist_newmodule(modname, ver, lf); 1421 } 1422} 1423 1424static void 1425linker_preload(void *arg) 1426{ 1427 caddr_t modptr; 1428 const char *modname, *nmodname; 1429 char *modtype; 1430 linker_file_t lf, nlf; 1431 linker_class_t lc; 1432 int error; 1433 linker_file_list_t loaded_files; 1434 linker_file_list_t depended_files; 1435 struct mod_metadata *mp, *nmp; 1436 struct mod_metadata **start, **stop, **mdp, **nmdp; 1437 struct mod_depend *verinfo; 1438 int nver; 1439 int resolves; 1440 modlist_t mod; 1441 struct sysinit **si_start, **si_stop; 1442 1443 TAILQ_INIT(&loaded_files); 1444 TAILQ_INIT(&depended_files); 1445 TAILQ_INIT(&found_modules); 1446 error = 0; 1447 1448 modptr = NULL; 1449 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1450 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1451 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1452 if (modname == NULL) { 1453 printf("Preloaded module at %p does not have a" 1454 " name!\n", modptr); 1455 continue; 1456 } 1457 if (modtype == NULL) { 1458 printf("Preloaded module at %p does not have a type!\n", 1459 modptr); 1460 continue; 1461 } 1462 if (bootverbose) 1463 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1464 modptr); 1465 lf = NULL; 1466 TAILQ_FOREACH(lc, &classes, link) { 1467 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1468 if (!error) 1469 break; 1470 lf = NULL; 1471 } 1472 if (lf) 1473 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1474 } 1475 1476 /* 1477 * First get a list of stuff in the kernel. 1478 */ 1479 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1480 &stop, NULL) == 0) 1481 linker_addmodules(linker_kernel_file, start, stop, 1); 1482 1483 /* 1484 * This is a once-off kinky bubble sort to resolve relocation 1485 * dependency requirements. 1486 */ 1487restart: 1488 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1489 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1490 &stop, NULL); 1491 /* 1492 * First, look to see if we would successfully link with this 1493 * stuff. 1494 */ 1495 resolves = 1; /* unless we know otherwise */ 1496 if (!error) { 1497 for (mdp = start; mdp < stop; mdp++) { 1498 mp = *mdp; 1499 if (mp->md_type != MDT_DEPEND) 1500 continue; 1501 modname = mp->md_cval; 1502 verinfo = mp->md_data; 1503 for (nmdp = start; nmdp < stop; nmdp++) { 1504 nmp = *nmdp; 1505 if (nmp->md_type != MDT_VERSION) 1506 continue; 1507 nmodname = nmp->md_cval; 1508 if (strcmp(modname, nmodname) == 0) 1509 break; 1510 } 1511 if (nmdp < stop) /* it's a self reference */ 1512 continue; 1513 1514 /* 1515 * ok, the module isn't here yet, we 1516 * are not finished 1517 */ 1518 if (modlist_lookup2(modname, verinfo) == NULL) 1519 resolves = 0; 1520 } 1521 } 1522 /* 1523 * OK, if we found our modules, we can link. So, "provide" 1524 * the modules inside and add it to the end of the link order 1525 * list. 1526 */ 1527 if (resolves) { 1528 if (!error) { 1529 for (mdp = start; mdp < stop; mdp++) { 1530 mp = *mdp; 1531 if (mp->md_type != MDT_VERSION) 1532 continue; 1533 modname = mp->md_cval; 1534 nver = ((struct mod_version *) 1535 mp->md_data)->mv_version; 1536 if (modlist_lookup(modname, 1537 nver) != NULL) { 1538 printf("module %s already" 1539 " present!\n", modname); 1540 TAILQ_REMOVE(&loaded_files, 1541 lf, loaded); 1542 linker_file_unload(lf, 1543 LINKER_UNLOAD_FORCE); 1544 /* we changed tailq next ptr */ 1545 goto restart; 1546 } 1547 modlist_newmodule(modname, nver, lf); 1548 } 1549 } 1550 TAILQ_REMOVE(&loaded_files, lf, loaded); 1551 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1552 /* 1553 * Since we provided modules, we need to restart the 1554 * sort so that the previous files that depend on us 1555 * have a chance. Also, we've busted the tailq next 1556 * pointer with the REMOVE. 1557 */ 1558 goto restart; 1559 } 1560 } 1561 1562 /* 1563 * At this point, we check to see what could not be resolved.. 1564 */ 1565 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1566 TAILQ_REMOVE(&loaded_files, lf, loaded); 1567 printf("KLD file %s is missing dependencies\n", lf->filename); 1568 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1569 } 1570 1571 /* 1572 * We made it. Finish off the linking in the order we determined. 1573 */ 1574 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1575 if (linker_kernel_file) { 1576 linker_kernel_file->refs++; 1577 error = linker_file_add_dependency(lf, 1578 linker_kernel_file); 1579 if (error) 1580 panic("cannot add dependency"); 1581 } 1582 lf->userrefs++; /* so we can (try to) kldunload it */ 1583 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1584 &stop, NULL); 1585 if (!error) { 1586 for (mdp = start; mdp < stop; mdp++) { 1587 mp = *mdp; 1588 if (mp->md_type != MDT_DEPEND) 1589 continue; 1590 modname = mp->md_cval; 1591 verinfo = mp->md_data; 1592 mod = modlist_lookup2(modname, verinfo); 1593 /* Don't count self-dependencies */ 1594 if (lf == mod->container) 1595 continue; 1596 mod->container->refs++; 1597 error = linker_file_add_dependency(lf, 1598 mod->container); 1599 if (error) 1600 panic("cannot add dependency"); 1601 } 1602 } 1603 /* 1604 * Now do relocation etc using the symbol search paths 1605 * established by the dependencies 1606 */ 1607 error = LINKER_LINK_PRELOAD_FINISH(lf); 1608 if (error) { 1609 TAILQ_REMOVE(&depended_files, lf, loaded); 1610 printf("KLD file %s - could not finalize loading\n", 1611 lf->filename); 1612 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1613 continue; 1614 } 1615 linker_file_register_modules(lf); 1616 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1617 &si_stop, NULL) == 0) 1618 sysinit_add(si_start, si_stop); 1619 linker_file_register_sysctls(lf); 1620 lf->flags |= LINKER_FILE_LINKED; 1621 } 1622 /* woohoo! we made it! */ 1623} 1624 1625SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1626 1627/* 1628 * Search for a not-loaded module by name. 1629 * 1630 * Modules may be found in the following locations: 1631 * 1632 * - preloaded (result is just the module name) - on disk (result is full path 1633 * to module) 1634 * 1635 * If the module name is qualified in any way (contains path, etc.) the we 1636 * simply return a copy of it. 1637 * 1638 * The search path can be manipulated via sysctl. Note that we use the ';' 1639 * character as a separator to be consistent with the bootloader. 1640 */ 1641 1642static char linker_hintfile[] = "linker.hints"; 1643static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1644 1645SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1646 sizeof(linker_path), "module load search path"); 1647 1648TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1649 1650static char *linker_ext_list[] = { 1651 "", 1652 ".ko", 1653 NULL 1654}; 1655 1656/* 1657 * Check if file actually exists either with or without extension listed in 1658 * the linker_ext_list. (probably should be generic for the rest of the 1659 * kernel) 1660 */ 1661static char * 1662linker_lookup_file(const char *path, int pathlen, const char *name, 1663 int namelen, struct vattr *vap) 1664{ 1665 struct nameidata nd; 1666 struct thread *td = curthread; /* XXX */ 1667 char *result, **cpp, *sep; 1668 int error, len, extlen, reclen, flags, vfslocked; 1669 enum vtype type; 1670 1671 extlen = 0; 1672 for (cpp = linker_ext_list; *cpp; cpp++) { 1673 len = strlen(*cpp); 1674 if (len > extlen) 1675 extlen = len; 1676 } 1677 extlen++; /* trailing '\0' */ 1678 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1679 1680 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1681 result = malloc(reclen, M_LINKER, M_WAITOK); 1682 for (cpp = linker_ext_list; *cpp; cpp++) { 1683 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1684 namelen, name, *cpp); 1685 /* 1686 * Attempt to open the file, and return the path if 1687 * we succeed and it's a regular file. 1688 */ 1689 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td); 1690 flags = FREAD; 1691 error = vn_open(&nd, &flags, 0, NULL); 1692 if (error == 0) { 1693 vfslocked = NDHASGIANT(&nd); 1694 NDFREE(&nd, NDF_ONLY_PNBUF); 1695 type = nd.ni_vp->v_type; 1696 if (vap) 1697 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1698 VOP_UNLOCK(nd.ni_vp, 0); 1699 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1700 VFS_UNLOCK_GIANT(vfslocked); 1701 if (type == VREG) 1702 return (result); 1703 } 1704 } 1705 free(result, M_LINKER); 1706 return (NULL); 1707} 1708 1709#define INT_ALIGN(base, ptr) ptr = \ 1710 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1711 1712/* 1713 * Lookup KLD which contains requested module in the "linker.hints" file. If 1714 * version specification is available, then try to find the best KLD. 1715 * Otherwise just find the latest one. 1716 */ 1717static char * 1718linker_hints_lookup(const char *path, int pathlen, const char *modname, 1719 int modnamelen, struct mod_depend *verinfo) 1720{ 1721 struct thread *td = curthread; /* XXX */ 1722 struct ucred *cred = td ? td->td_ucred : NULL; 1723 struct nameidata nd; 1724 struct vattr vattr, mattr; 1725 u_char *hints = NULL; 1726 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1727 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1728 int vfslocked = 0; 1729 1730 result = NULL; 1731 bestver = found = 0; 1732 1733 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1734 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1735 strlen(sep) + 1; 1736 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1737 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1738 linker_hintfile); 1739 1740 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1741 flags = FREAD; 1742 error = vn_open(&nd, &flags, 0, NULL); 1743 if (error) 1744 goto bad; 1745 vfslocked = NDHASGIANT(&nd); 1746 NDFREE(&nd, NDF_ONLY_PNBUF); 1747 if (nd.ni_vp->v_type != VREG) 1748 goto bad; 1749 best = cp = NULL; 1750 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1751 if (error) 1752 goto bad; 1753 /* 1754 * XXX: we need to limit this number to some reasonable value 1755 */ 1756 if (vattr.va_size > 100 * 1024) { 1757 printf("hints file too large %ld\n", (long)vattr.va_size); 1758 goto bad; 1759 } 1760 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1761 if (hints == NULL) 1762 goto bad; 1763 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1764 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1765 if (error) 1766 goto bad; 1767 VOP_UNLOCK(nd.ni_vp, 0); 1768 vn_close(nd.ni_vp, FREAD, cred, td); 1769 VFS_UNLOCK_GIANT(vfslocked); 1770 nd.ni_vp = NULL; 1771 if (reclen != 0) { 1772 printf("can't read %d\n", reclen); 1773 goto bad; 1774 } 1775 intp = (int *)hints; 1776 ival = *intp++; 1777 if (ival != LINKER_HINTS_VERSION) { 1778 printf("hints file version mismatch %d\n", ival); 1779 goto bad; 1780 } 1781 bufend = hints + vattr.va_size; 1782 recptr = (u_char *)intp; 1783 clen = blen = 0; 1784 while (recptr < bufend && !found) { 1785 intp = (int *)recptr; 1786 reclen = *intp++; 1787 ival = *intp++; 1788 cp = (char *)intp; 1789 switch (ival) { 1790 case MDT_VERSION: 1791 clen = *cp++; 1792 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1793 break; 1794 cp += clen; 1795 INT_ALIGN(hints, cp); 1796 ival = *(int *)cp; 1797 cp += sizeof(int); 1798 clen = *cp++; 1799 if (verinfo == NULL || 1800 ival == verinfo->md_ver_preferred) { 1801 found = 1; 1802 break; 1803 } 1804 if (ival >= verinfo->md_ver_minimum && 1805 ival <= verinfo->md_ver_maximum && 1806 ival > bestver) { 1807 bestver = ival; 1808 best = cp; 1809 blen = clen; 1810 } 1811 break; 1812 default: 1813 break; 1814 } 1815 recptr += reclen + sizeof(int); 1816 } 1817 /* 1818 * Finally check if KLD is in the place 1819 */ 1820 if (found) 1821 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1822 else if (best) 1823 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1824 1825 /* 1826 * KLD is newer than hints file. What we should do now? 1827 */ 1828 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1829 printf("warning: KLD '%s' is newer than the linker.hints" 1830 " file\n", result); 1831bad: 1832 free(pathbuf, M_LINKER); 1833 if (hints) 1834 free(hints, M_TEMP); 1835 if (nd.ni_vp != NULL) { 1836 VOP_UNLOCK(nd.ni_vp, 0); 1837 vn_close(nd.ni_vp, FREAD, cred, td); 1838 VFS_UNLOCK_GIANT(vfslocked); 1839 } 1840 /* 1841 * If nothing found or hints is absent - fallback to the old 1842 * way by using "kldname[.ko]" as module name. 1843 */ 1844 if (!found && !bestver && result == NULL) 1845 result = linker_lookup_file(path, pathlen, modname, 1846 modnamelen, NULL); 1847 return (result); 1848} 1849 1850/* 1851 * Lookup KLD which contains requested module in the all directories. 1852 */ 1853static char * 1854linker_search_module(const char *modname, int modnamelen, 1855 struct mod_depend *verinfo) 1856{ 1857 char *cp, *ep, *result; 1858 1859 /* 1860 * traverse the linker path 1861 */ 1862 for (cp = linker_path; *cp; cp = ep + 1) { 1863 /* find the end of this component */ 1864 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1865 result = linker_hints_lookup(cp, ep - cp, modname, 1866 modnamelen, verinfo); 1867 if (result != NULL) 1868 return (result); 1869 if (*ep == 0) 1870 break; 1871 } 1872 return (NULL); 1873} 1874 1875/* 1876 * Search for module in all directories listed in the linker_path. 1877 */ 1878static char * 1879linker_search_kld(const char *name) 1880{ 1881 char *cp, *ep, *result; 1882 int len; 1883 1884 /* qualified at all? */ 1885 if (index(name, '/')) 1886 return (linker_strdup(name)); 1887 1888 /* traverse the linker path */ 1889 len = strlen(name); 1890 for (ep = linker_path; *ep; ep++) { 1891 cp = ep; 1892 /* find the end of this component */ 1893 for (; *ep != 0 && *ep != ';'; ep++); 1894 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1895 if (result != NULL) 1896 return (result); 1897 } 1898 return (NULL); 1899} 1900 1901static const char * 1902linker_basename(const char *path) 1903{ 1904 const char *filename; 1905 1906 filename = rindex(path, '/'); 1907 if (filename == NULL) 1908 return path; 1909 if (filename[1]) 1910 filename++; 1911 return (filename); 1912} 1913 1914#ifdef HWPMC_HOOKS 1915/* 1916 * Inform hwpmc about the set of kernel modules currently loaded. 1917 */ 1918void * 1919linker_hwpmc_list_objects(void) 1920{ 1921 linker_file_t lf; 1922 struct pmckern_map_in *kobase; 1923 int i, nmappings; 1924 1925 nmappings = 0; 1926 KLD_LOCK(); 1927 TAILQ_FOREACH(lf, &linker_files, link) 1928 nmappings++; 1929 1930 /* Allocate nmappings + 1 entries. */ 1931 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in), 1932 M_LINKER, M_WAITOK | M_ZERO); 1933 i = 0; 1934 TAILQ_FOREACH(lf, &linker_files, link) { 1935 1936 /* Save the info for this linker file. */ 1937 kobase[i].pm_file = lf->filename; 1938 kobase[i].pm_address = (uintptr_t)lf->address; 1939 i++; 1940 } 1941 KLD_UNLOCK(); 1942 1943 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?")); 1944 1945 /* The last entry of the malloced area comprises of all zeros. */ 1946 KASSERT(kobase[i].pm_file == NULL, 1947 ("linker_hwpmc_list_objects: last object not NULL")); 1948 1949 return ((void *)kobase); 1950} 1951#endif 1952 1953/* 1954 * Find a file which contains given module and load it, if "parent" is not 1955 * NULL, register a reference to it. 1956 */ 1957static int 1958linker_load_module(const char *kldname, const char *modname, 1959 struct linker_file *parent, struct mod_depend *verinfo, 1960 struct linker_file **lfpp) 1961{ 1962 linker_file_t lfdep; 1963 const char *filename; 1964 char *pathname; 1965 int error; 1966 1967 KLD_LOCK_ASSERT(); 1968 if (modname == NULL) { 1969 /* 1970 * We have to load KLD 1971 */ 1972 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 1973 " is not NULL")); 1974 pathname = linker_search_kld(kldname); 1975 } else { 1976 if (modlist_lookup2(modname, verinfo) != NULL) 1977 return (EEXIST); 1978 if (kldname != NULL) 1979 pathname = linker_strdup(kldname); 1980 else if (rootvnode == NULL) 1981 pathname = NULL; 1982 else 1983 /* 1984 * Need to find a KLD with required module 1985 */ 1986 pathname = linker_search_module(modname, 1987 strlen(modname), verinfo); 1988 } 1989 if (pathname == NULL) 1990 return (ENOENT); 1991 1992 /* 1993 * Can't load more than one file with the same basename XXX: 1994 * Actually it should be possible to have multiple KLDs with 1995 * the same basename but different path because they can 1996 * provide different versions of the same modules. 1997 */ 1998 filename = linker_basename(pathname); 1999 if (linker_find_file_by_name(filename)) 2000 error = EEXIST; 2001 else do { 2002 error = linker_load_file(pathname, &lfdep); 2003 if (error) 2004 break; 2005 if (modname && verinfo && 2006 modlist_lookup2(modname, verinfo) == NULL) { 2007 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2008 error = ENOENT; 2009 break; 2010 } 2011 if (parent) { 2012 error = linker_file_add_dependency(parent, lfdep); 2013 if (error) 2014 break; 2015 } 2016 if (lfpp) 2017 *lfpp = lfdep; 2018 } while (0); 2019 free(pathname, M_LINKER); 2020 return (error); 2021} 2022 2023/* 2024 * This routine is responsible for finding dependencies of userland initiated 2025 * kldload(2)'s of files. 2026 */ 2027int 2028linker_load_dependencies(linker_file_t lf) 2029{ 2030 linker_file_t lfdep; 2031 struct mod_metadata **start, **stop, **mdp, **nmdp; 2032 struct mod_metadata *mp, *nmp; 2033 struct mod_depend *verinfo; 2034 modlist_t mod; 2035 const char *modname, *nmodname; 2036 int ver, error = 0, count; 2037 2038 /* 2039 * All files are dependant on /kernel. 2040 */ 2041 KLD_LOCK_ASSERT(); 2042 if (linker_kernel_file) { 2043 linker_kernel_file->refs++; 2044 error = linker_file_add_dependency(lf, linker_kernel_file); 2045 if (error) 2046 return (error); 2047 } 2048 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2049 &count) != 0) 2050 return (0); 2051 for (mdp = start; mdp < stop; mdp++) { 2052 mp = *mdp; 2053 if (mp->md_type != MDT_VERSION) 2054 continue; 2055 modname = mp->md_cval; 2056 ver = ((struct mod_version *)mp->md_data)->mv_version; 2057 mod = modlist_lookup(modname, ver); 2058 if (mod != NULL) { 2059 printf("interface %s.%d already present in the KLD" 2060 " '%s'!\n", modname, ver, 2061 mod->container->filename); 2062 return (EEXIST); 2063 } 2064 } 2065 2066 for (mdp = start; mdp < stop; mdp++) { 2067 mp = *mdp; 2068 if (mp->md_type != MDT_DEPEND) 2069 continue; 2070 modname = mp->md_cval; 2071 verinfo = mp->md_data; 2072 nmodname = NULL; 2073 for (nmdp = start; nmdp < stop; nmdp++) { 2074 nmp = *nmdp; 2075 if (nmp->md_type != MDT_VERSION) 2076 continue; 2077 nmodname = nmp->md_cval; 2078 if (strcmp(modname, nmodname) == 0) 2079 break; 2080 } 2081 if (nmdp < stop)/* early exit, it's a self reference */ 2082 continue; 2083 mod = modlist_lookup2(modname, verinfo); 2084 if (mod) { /* woohoo, it's loaded already */ 2085 lfdep = mod->container; 2086 lfdep->refs++; 2087 error = linker_file_add_dependency(lf, lfdep); 2088 if (error) 2089 break; 2090 continue; 2091 } 2092 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2093 if (error) { 2094 printf("KLD %s: depends on %s - not available\n", 2095 lf->filename, modname); 2096 break; 2097 } 2098 } 2099 2100 if (error) 2101 return (error); 2102 linker_addmodules(lf, start, stop, 0); 2103 return (error); 2104} 2105 2106static int 2107sysctl_kern_function_list_iterate(const char *name, void *opaque) 2108{ 2109 struct sysctl_req *req; 2110 2111 req = opaque; 2112 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2113} 2114 2115/* 2116 * Export a nul-separated, double-nul-terminated list of all function names 2117 * in the kernel. 2118 */ 2119static int 2120sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2121{ 2122 linker_file_t lf; 2123 int error; 2124 2125#ifdef MAC 2126 error = mac_kld_check_stat(req->td->td_ucred); 2127 if (error) 2128 return (error); 2129#endif 2130 error = sysctl_wire_old_buffer(req, 0); 2131 if (error != 0) 2132 return (error); 2133 KLD_LOCK(); 2134 TAILQ_FOREACH(lf, &linker_files, link) { 2135 error = LINKER_EACH_FUNCTION_NAME(lf, 2136 sysctl_kern_function_list_iterate, req); 2137 if (error) { 2138 KLD_UNLOCK(); 2139 return (error); 2140 } 2141 } 2142 KLD_UNLOCK(); 2143 return (SYSCTL_OUT(req, "", 1)); 2144} 2145 2146SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 2147 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2148