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