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