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