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