kern_linker.c revision 159808
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 159808 2006-06-20 21:31:38Z 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, 0) != 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 */ 650int 651linker_file_lookup_set(linker_file_t file, const char *name, 652 void *firstp, void *lastp, int *countp) 653{ 654 655 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp)); 656} 657 658caddr_t 659linker_file_lookup_symbol(linker_file_t file, const char *name, int deps) 660{ 661 c_linker_sym_t sym; 662 linker_symval_t symval; 663 caddr_t address; 664 size_t common_size = 0; 665 int i; 666 667 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 668 file, name, deps)); 669 670 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 671 LINKER_SYMBOL_VALUES(file, sym, &symval); 672 if (symval.value == 0) 673 /* 674 * For commons, first look them up in the 675 * dependencies and only allocate space if not found 676 * there. 677 */ 678 common_size = symval.size; 679 else { 680 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol" 681 ".value=%p\n", symval.value)); 682 return (symval.value); 683 } 684 } 685 if (deps) { 686 for (i = 0; i < file->ndeps; i++) { 687 address = linker_file_lookup_symbol(file->deps[i], 688 name, 0); 689 if (address) { 690 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 691 " deps value=%p\n", address)); 692 return (address); 693 } 694 } 695 } 696 if (common_size > 0) { 697 /* 698 * This is a common symbol which was not found in the 699 * dependencies. We maintain a simple common symbol table in 700 * the file object. 701 */ 702 struct common_symbol *cp; 703 704 STAILQ_FOREACH(cp, &file->common, link) { 705 if (strcmp(cp->name, name) == 0) { 706 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 707 " old common value=%p\n", cp->address)); 708 return (cp->address); 709 } 710 } 711 /* 712 * Round the symbol size up to align. 713 */ 714 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 715 cp = malloc(sizeof(struct common_symbol) 716 + common_size + strlen(name) + 1, M_LINKER, 717 M_WAITOK | M_ZERO); 718 cp->address = (caddr_t)(cp + 1); 719 cp->name = cp->address + common_size; 720 strcpy(cp->name, name); 721 bzero(cp->address, common_size); 722 STAILQ_INSERT_TAIL(&file->common, cp, link); 723 724 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common" 725 " value=%p\n", cp->address)); 726 return (cp->address); 727 } 728 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 729 return (0); 730} 731 732#ifdef DDB 733/* 734 * DDB Helpers. DDB has to look across multiple files with their own symbol 735 * tables and string tables. 736 * 737 * Note that we do not obey list locking protocols here. We really don't need 738 * DDB to hang because somebody's got the lock held. We'll take the chance 739 * that the files list is inconsistant instead. 740 */ 741 742int 743linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 744{ 745 linker_file_t lf; 746 747 TAILQ_FOREACH(lf, &linker_files, link) { 748 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 749 return (0); 750 } 751 return (ENOENT); 752} 753 754int 755linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 756{ 757 linker_file_t lf; 758 c_linker_sym_t best, es; 759 u_long diff, bestdiff, off; 760 761 best = 0; 762 off = (uintptr_t)value; 763 bestdiff = off; 764 TAILQ_FOREACH(lf, &linker_files, link) { 765 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 766 continue; 767 if (es != 0 && diff < bestdiff) { 768 best = es; 769 bestdiff = diff; 770 } 771 if (bestdiff == 0) 772 break; 773 } 774 if (best) { 775 *sym = best; 776 *diffp = bestdiff; 777 return (0); 778 } else { 779 *sym = 0; 780 *diffp = off; 781 return (ENOENT); 782 } 783} 784 785int 786linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 787{ 788 linker_file_t lf; 789 790 TAILQ_FOREACH(lf, &linker_files, link) { 791 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 792 return (0); 793 } 794 return (ENOENT); 795} 796#endif 797 798/* 799 * Syscalls. 800 */ 801/* 802 * MPSAFE 803 */ 804int 805kern_kldload(struct thread *td, const char *file, int *fileid) 806{ 807#ifdef HWPMC_HOOKS 808 struct pmckern_map_in pkm; 809#endif 810 const char *kldname, *modname; 811 linker_file_t lf; 812 int error; 813 814 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 815 return (error); 816 817 if ((error = suser(td)) != 0) 818 return (error); 819 820 /* 821 * If file does not contain qualified name or any dot in it 822 * (kldname.ko, or kldname.ver.ko) treat it as interface 823 * name. 824 */ 825 if (index(file, '/') || index(file, '.')) { 826 kldname = file; 827 modname = NULL; 828 } else { 829 kldname = NULL; 830 modname = file; 831 } 832 833 mtx_lock(&Giant); 834 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 835 if (error) 836 goto unlock; 837 838#ifdef HWPMC_HOOKS 839 pkm.pm_file = lf->filename; 840 pkm.pm_address = (uintptr_t) lf->address; 841 PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm); 842#endif 843 lf->userrefs++; 844 if (fileid != NULL) 845 *fileid = lf->id; 846unlock: 847 mtx_unlock(&Giant); 848 return (error); 849} 850 851int 852kldload(struct thread *td, struct kldload_args *uap) 853{ 854 char *pathname = NULL; 855 int error, fileid; 856 857 td->td_retval[0] = -1; 858 859 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 860 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 861 if (error == 0) { 862 error = kern_kldload(td, pathname, &fileid); 863 if (error == 0) 864 td->td_retval[0] = fileid; 865 } 866 free(pathname, M_TEMP); 867 return (error); 868} 869 870/* 871 * MPSAFE 872 */ 873int 874kern_kldunload(struct thread *td, int fileid, int flags) 875{ 876#ifdef HWPMC_HOOKS 877 struct pmckern_map_out pkm; 878#endif 879 linker_file_t lf; 880 int error = 0; 881 882 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 883 return (error); 884 885 if ((error = suser(td)) != 0) 886 return (error); 887 888 mtx_lock(&Giant); 889 lf = linker_find_file_by_id(fileid); 890 if (lf) { 891 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 892 if (lf->userrefs == 0) { 893 /* 894 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 895 */ 896 printf("kldunload: attempt to unload file that was" 897 " loaded by the kernel\n"); 898 error = EBUSY; 899 } else { 900#ifdef HWPMC_HOOKS 901 /* Save data needed by hwpmc(4) before unloading. */ 902 pkm.pm_address = (uintptr_t) lf->address; 903 pkm.pm_size = lf->size; 904#endif 905 lf->userrefs--; 906 error = linker_file_unload(lf, flags); 907 if (error) 908 lf->userrefs++; 909 } 910 } else 911 error = ENOENT; 912 913#ifdef HWPMC_HOOKS 914 if (error == 0) 915 PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm); 916#endif 917 mtx_unlock(&Giant); 918 return (error); 919} 920 921/* 922 * MPSAFE 923 */ 924int 925kldunload(struct thread *td, struct kldunload_args *uap) 926{ 927 928 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 929} 930 931/* 932 * MPSAFE 933 */ 934int 935kldunloadf(struct thread *td, struct kldunloadf_args *uap) 936{ 937 938 if (uap->flags != LINKER_UNLOAD_NORMAL && 939 uap->flags != LINKER_UNLOAD_FORCE) 940 return (EINVAL); 941 return (kern_kldunload(td, uap->fileid, uap->flags)); 942} 943 944/* 945 * MPSAFE 946 */ 947int 948kldfind(struct thread *td, struct kldfind_args *uap) 949{ 950 char *pathname; 951 const char *filename; 952 linker_file_t lf; 953 int error; 954 955#ifdef MAC 956 error = mac_check_kld_stat(td->td_ucred); 957 if (error) 958 return (error); 959#endif 960 961 mtx_lock(&Giant); 962 td->td_retval[0] = -1; 963 964 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 965 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 966 goto out; 967 968 filename = linker_basename(pathname); 969 lf = linker_find_file_by_name(filename); 970 if (lf) 971 td->td_retval[0] = lf->id; 972 else 973 error = ENOENT; 974out: 975 free(pathname, M_TEMP); 976 mtx_unlock(&Giant); 977 return (error); 978} 979 980/* 981 * MPSAFE 982 */ 983int 984kldnext(struct thread *td, struct kldnext_args *uap) 985{ 986 linker_file_t lf; 987 int error = 0; 988 989#ifdef MAC 990 error = mac_check_kld_stat(td->td_ucred); 991 if (error) 992 return (error); 993#endif 994 995 mtx_lock(&Giant); 996 997 if (uap->fileid == 0) { 998 mtx_lock(&kld_mtx); 999 if (TAILQ_FIRST(&linker_files)) 1000 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id; 1001 else 1002 td->td_retval[0] = 0; 1003 mtx_unlock(&kld_mtx); 1004 goto out; 1005 } 1006 lf = linker_find_file_by_id(uap->fileid); 1007 if (lf) { 1008 if (TAILQ_NEXT(lf, link)) 1009 td->td_retval[0] = TAILQ_NEXT(lf, link)->id; 1010 else 1011 td->td_retval[0] = 0; 1012 } else 1013 error = ENOENT; 1014out: 1015 mtx_unlock(&Giant); 1016 return (error); 1017} 1018 1019/* 1020 * MPSAFE 1021 */ 1022int 1023kldstat(struct thread *td, struct kldstat_args *uap) 1024{ 1025 struct kld_file_stat stat; 1026 linker_file_t lf; 1027 int error, namelen; 1028 1029 /* 1030 * Check the version of the user's structure. 1031 */ 1032 error = copyin(uap->stat, &stat, sizeof(struct kld_file_stat)); 1033 if (error) 1034 return (error); 1035 if (stat.version != sizeof(struct kld_file_stat)) 1036 return (EINVAL); 1037 1038#ifdef MAC 1039 error = mac_check_kld_stat(td->td_ucred); 1040 if (error) 1041 return (error); 1042#endif 1043 1044 mtx_lock(&Giant); 1045 1046 lf = linker_find_file_by_id(uap->fileid); 1047 if (lf == NULL) { 1048 mtx_unlock(&Giant); 1049 return (ENOENT); 1050 } 1051 1052 namelen = strlen(lf->filename) + 1; 1053 if (namelen > MAXPATHLEN) 1054 namelen = MAXPATHLEN; 1055 bcopy(lf->filename, &stat.name[0], namelen); 1056 stat.refs = lf->refs; 1057 stat.id = lf->id; 1058 stat.address = lf->address; 1059 stat.size = lf->size; 1060 mtx_unlock(&Giant); 1061 1062 td->td_retval[0] = 0; 1063 1064 return (copyout(&stat, uap->stat, sizeof(struct kld_file_stat))); 1065} 1066 1067/* 1068 * MPSAFE 1069 */ 1070int 1071kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1072{ 1073 linker_file_t lf; 1074 module_t mp; 1075 int error = 0; 1076 1077#ifdef MAC 1078 error = mac_check_kld_stat(td->td_ucred); 1079 if (error) 1080 return (error); 1081#endif 1082 1083 mtx_lock(&Giant); 1084 lf = linker_find_file_by_id(uap->fileid); 1085 if (lf) { 1086 MOD_SLOCK; 1087 mp = TAILQ_FIRST(&lf->modules); 1088 if (mp != NULL) 1089 td->td_retval[0] = module_getid(mp); 1090 else 1091 td->td_retval[0] = 0; 1092 MOD_SUNLOCK; 1093 } else 1094 error = ENOENT; 1095 mtx_unlock(&Giant); 1096 return (error); 1097} 1098 1099/* 1100 * MPSAFE 1101 */ 1102int 1103kldsym(struct thread *td, struct kldsym_args *uap) 1104{ 1105 char *symstr = NULL; 1106 c_linker_sym_t sym; 1107 linker_symval_t symval; 1108 linker_file_t lf; 1109 struct kld_sym_lookup lookup; 1110 int error = 0; 1111 1112#ifdef MAC 1113 error = mac_check_kld_stat(td->td_ucred); 1114 if (error) 1115 return (error); 1116#endif 1117 1118 mtx_lock(&Giant); 1119 1120 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1121 goto out; 1122 if (lookup.version != sizeof(lookup) || 1123 uap->cmd != KLDSYM_LOOKUP) { 1124 error = EINVAL; 1125 goto out; 1126 } 1127 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1128 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1129 goto out; 1130 if (uap->fileid != 0) { 1131 lf = linker_find_file_by_id(uap->fileid); 1132 if (lf == NULL) { 1133 error = ENOENT; 1134 goto out; 1135 } 1136 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1137 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1138 lookup.symvalue = (uintptr_t) symval.value; 1139 lookup.symsize = symval.size; 1140 error = copyout(&lookup, uap->data, sizeof(lookup)); 1141 } else 1142 error = ENOENT; 1143 } else { 1144 mtx_lock(&kld_mtx); 1145 TAILQ_FOREACH(lf, &linker_files, link) { 1146 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1147 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1148 lookup.symvalue = (uintptr_t)symval.value; 1149 lookup.symsize = symval.size; 1150 error = copyout(&lookup, uap->data, 1151 sizeof(lookup)); 1152 break; 1153 } 1154 } 1155 mtx_unlock(&kld_mtx); 1156 if (lf == NULL) 1157 error = ENOENT; 1158 } 1159out: 1160 if (symstr) 1161 free(symstr, M_TEMP); 1162 mtx_unlock(&Giant); 1163 return (error); 1164} 1165 1166/* 1167 * Preloaded module support 1168 */ 1169 1170static modlist_t 1171modlist_lookup(const char *name, int ver) 1172{ 1173 modlist_t mod; 1174 1175 TAILQ_FOREACH(mod, &found_modules, link) { 1176 if (strcmp(mod->name, name) == 0 && 1177 (ver == 0 || mod->version == ver)) 1178 return (mod); 1179 } 1180 return (NULL); 1181} 1182 1183static modlist_t 1184modlist_lookup2(const char *name, struct mod_depend *verinfo) 1185{ 1186 modlist_t mod, bestmod; 1187 int ver; 1188 1189 if (verinfo == NULL) 1190 return (modlist_lookup(name, 0)); 1191 bestmod = NULL; 1192 TAILQ_FOREACH(mod, &found_modules, link) { 1193 if (strcmp(mod->name, name) != 0) 1194 continue; 1195 ver = mod->version; 1196 if (ver == verinfo->md_ver_preferred) 1197 return (mod); 1198 if (ver >= verinfo->md_ver_minimum && 1199 ver <= verinfo->md_ver_maximum && 1200 (bestmod == NULL || ver > bestmod->version)) 1201 bestmod = mod; 1202 } 1203 return (bestmod); 1204} 1205 1206static modlist_t 1207modlist_newmodule(const char *modname, int version, linker_file_t container) 1208{ 1209 modlist_t mod; 1210 1211 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1212 if (mod == NULL) 1213 panic("no memory for module list"); 1214 mod->container = container; 1215 mod->name = modname; 1216 mod->version = version; 1217 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1218 return (mod); 1219} 1220 1221static void 1222linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1223 struct mod_metadata **stop, int preload) 1224{ 1225 struct mod_metadata *mp, **mdp; 1226 const char *modname; 1227 int ver; 1228 1229 for (mdp = start; mdp < stop; mdp++) { 1230 mp = *mdp; 1231 if (mp->md_type != MDT_VERSION) 1232 continue; 1233 modname = mp->md_cval; 1234 ver = ((struct mod_version *)mp->md_data)->mv_version; 1235 if (modlist_lookup(modname, ver) != NULL) { 1236 printf("module %s already present!\n", modname); 1237 /* XXX what can we do? this is a build error. :-( */ 1238 continue; 1239 } 1240 modlist_newmodule(modname, ver, lf); 1241 } 1242} 1243 1244static void 1245linker_preload(void *arg) 1246{ 1247 caddr_t modptr; 1248 const char *modname, *nmodname; 1249 char *modtype; 1250 linker_file_t lf; 1251 linker_class_t lc; 1252 int error; 1253 linker_file_list_t loaded_files; 1254 linker_file_list_t depended_files; 1255 struct mod_metadata *mp, *nmp; 1256 struct mod_metadata **start, **stop, **mdp, **nmdp; 1257 struct mod_depend *verinfo; 1258 int nver; 1259 int resolves; 1260 modlist_t mod; 1261 struct sysinit **si_start, **si_stop; 1262 1263 TAILQ_INIT(&loaded_files); 1264 TAILQ_INIT(&depended_files); 1265 TAILQ_INIT(&found_modules); 1266 error = 0; 1267 1268 modptr = NULL; 1269 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1270 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1271 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1272 if (modname == NULL) { 1273 printf("Preloaded module at %p does not have a" 1274 " name!\n", modptr); 1275 continue; 1276 } 1277 if (modtype == NULL) { 1278 printf("Preloaded module at %p does not have a type!\n", 1279 modptr); 1280 continue; 1281 } 1282 if (bootverbose) 1283 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1284 modptr); 1285 lf = NULL; 1286 TAILQ_FOREACH(lc, &classes, link) { 1287 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1288 if (!error) 1289 break; 1290 lf = NULL; 1291 } 1292 if (lf) 1293 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1294 } 1295 1296 /* 1297 * First get a list of stuff in the kernel. 1298 */ 1299 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1300 &stop, NULL) == 0) 1301 linker_addmodules(linker_kernel_file, start, stop, 1); 1302 1303 /* 1304 * this is a once-off kinky bubble sort resolve relocation dependency 1305 * requirements 1306 */ 1307restart: 1308 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1309 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1310 &stop, NULL); 1311 /* 1312 * First, look to see if we would successfully link with this 1313 * stuff. 1314 */ 1315 resolves = 1; /* unless we know otherwise */ 1316 if (!error) { 1317 for (mdp = start; mdp < stop; mdp++) { 1318 mp = *mdp; 1319 if (mp->md_type != MDT_DEPEND) 1320 continue; 1321 modname = mp->md_cval; 1322 verinfo = mp->md_data; 1323 for (nmdp = start; nmdp < stop; nmdp++) { 1324 nmp = *nmdp; 1325 if (nmp->md_type != MDT_VERSION) 1326 continue; 1327 nmodname = nmp->md_cval; 1328 if (strcmp(modname, nmodname) == 0) 1329 break; 1330 } 1331 if (nmdp < stop) /* it's a self reference */ 1332 continue; 1333 1334 /* 1335 * ok, the module isn't here yet, we 1336 * are not finished 1337 */ 1338 if (modlist_lookup2(modname, verinfo) == NULL) 1339 resolves = 0; 1340 } 1341 } 1342 /* 1343 * OK, if we found our modules, we can link. So, "provide" 1344 * the modules inside and add it to the end of the link order 1345 * list. 1346 */ 1347 if (resolves) { 1348 if (!error) { 1349 for (mdp = start; mdp < stop; mdp++) { 1350 mp = *mdp; 1351 if (mp->md_type != MDT_VERSION) 1352 continue; 1353 modname = mp->md_cval; 1354 nver = ((struct mod_version *) 1355 mp->md_data)->mv_version; 1356 if (modlist_lookup(modname, 1357 nver) != NULL) { 1358 printf("module %s already" 1359 " present!\n", modname); 1360 linker_file_unload(lf, 1361 LINKER_UNLOAD_FORCE); 1362 TAILQ_REMOVE(&loaded_files, 1363 lf, loaded); 1364 /* we changed tailq next ptr */ 1365 goto restart; 1366 } 1367 modlist_newmodule(modname, nver, lf); 1368 } 1369 } 1370 TAILQ_REMOVE(&loaded_files, lf, loaded); 1371 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1372 /* 1373 * Since we provided modules, we need to restart the 1374 * sort so that the previous files that depend on us 1375 * have a chance. Also, we've busted the tailq next 1376 * pointer with the REMOVE. 1377 */ 1378 goto restart; 1379 } 1380 } 1381 1382 /* 1383 * At this point, we check to see what could not be resolved.. 1384 */ 1385 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1386 printf("KLD file %s is missing dependencies\n", lf->filename); 1387 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1388 TAILQ_REMOVE(&loaded_files, lf, loaded); 1389 } 1390 1391 /* 1392 * We made it. Finish off the linking in the order we determined. 1393 */ 1394 TAILQ_FOREACH(lf, &depended_files, loaded) { 1395 if (linker_kernel_file) { 1396 linker_kernel_file->refs++; 1397 error = linker_file_add_dependency(lf, 1398 linker_kernel_file); 1399 if (error) 1400 panic("cannot add dependency"); 1401 } 1402 lf->userrefs++; /* so we can (try to) kldunload it */ 1403 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1404 &stop, NULL); 1405 if (!error) { 1406 for (mdp = start; mdp < stop; mdp++) { 1407 mp = *mdp; 1408 if (mp->md_type != MDT_DEPEND) 1409 continue; 1410 modname = mp->md_cval; 1411 verinfo = mp->md_data; 1412 mod = modlist_lookup2(modname, verinfo); 1413 /* Don't count self-dependencies */ 1414 if (lf == mod->container) 1415 continue; 1416 mod->container->refs++; 1417 error = linker_file_add_dependency(lf, 1418 mod->container); 1419 if (error) 1420 panic("cannot add dependency"); 1421 } 1422 } 1423 /* 1424 * Now do relocation etc using the symbol search paths 1425 * established by the dependencies 1426 */ 1427 error = LINKER_LINK_PRELOAD_FINISH(lf); 1428 if (error) { 1429 printf("KLD file %s - could not finalize loading\n", 1430 lf->filename); 1431 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1432 continue; 1433 } 1434 linker_file_register_modules(lf); 1435 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1436 &si_stop, NULL) == 0) 1437 sysinit_add(si_start, si_stop); 1438 linker_file_register_sysctls(lf); 1439 lf->flags |= LINKER_FILE_LINKED; 1440 } 1441 /* woohoo! we made it! */ 1442} 1443 1444SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0) 1445 1446/* 1447 * Search for a not-loaded module by name. 1448 * 1449 * Modules may be found in the following locations: 1450 * 1451 * - preloaded (result is just the module name) - on disk (result is full path 1452 * to module) 1453 * 1454 * If the module name is qualified in any way (contains path, etc.) the we 1455 * simply return a copy of it. 1456 * 1457 * The search path can be manipulated via sysctl. Note that we use the ';' 1458 * character as a separator to be consistent with the bootloader. 1459 */ 1460 1461static char linker_hintfile[] = "linker.hints"; 1462static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1463 1464SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1465 sizeof(linker_path), "module load search path"); 1466 1467TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1468 1469static char *linker_ext_list[] = { 1470 "", 1471 ".ko", 1472 NULL 1473}; 1474 1475/* 1476 * Check if file actually exists either with or without extension listed in 1477 * the linker_ext_list. (probably should be generic for the rest of the 1478 * kernel) 1479 */ 1480static char * 1481linker_lookup_file(const char *path, int pathlen, const char *name, 1482 int namelen, struct vattr *vap) 1483{ 1484 struct nameidata nd; 1485 struct thread *td = curthread; /* XXX */ 1486 char *result, **cpp, *sep; 1487 int error, len, extlen, reclen, flags, vfslocked; 1488 enum vtype type; 1489 1490 extlen = 0; 1491 for (cpp = linker_ext_list; *cpp; cpp++) { 1492 len = strlen(*cpp); 1493 if (len > extlen) 1494 extlen = len; 1495 } 1496 extlen++; /* trailing '\0' */ 1497 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1498 1499 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1500 result = malloc(reclen, M_LINKER, M_WAITOK); 1501 for (cpp = linker_ext_list; *cpp; cpp++) { 1502 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1503 namelen, name, *cpp); 1504 /* 1505 * Attempt to open the file, and return the path if 1506 * we succeed and it's a regular file. 1507 */ 1508 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td); 1509 flags = FREAD; 1510 error = vn_open(&nd, &flags, 0, -1); 1511 if (error == 0) { 1512 vfslocked = NDHASGIANT(&nd); 1513 NDFREE(&nd, NDF_ONLY_PNBUF); 1514 type = nd.ni_vp->v_type; 1515 if (vap) 1516 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td); 1517 VOP_UNLOCK(nd.ni_vp, 0, td); 1518 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1519 VFS_UNLOCK_GIANT(vfslocked); 1520 if (type == VREG) 1521 return (result); 1522 } 1523 } 1524 free(result, M_LINKER); 1525 return (NULL); 1526} 1527 1528#define INT_ALIGN(base, ptr) ptr = \ 1529 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1530 1531/* 1532 * Lookup KLD which contains requested module in the "linker.hints" file. If 1533 * version specification is available, then try to find the best KLD. 1534 * Otherwise just find the latest one. 1535 */ 1536static char * 1537linker_hints_lookup(const char *path, int pathlen, const char *modname, 1538 int modnamelen, struct mod_depend *verinfo) 1539{ 1540 struct thread *td = curthread; /* XXX */ 1541 struct ucred *cred = td ? td->td_ucred : NULL; 1542 struct nameidata nd; 1543 struct vattr vattr, mattr; 1544 u_char *hints = NULL; 1545 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1546 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1547 int vfslocked = 0; 1548 1549 result = NULL; 1550 bestver = found = 0; 1551 1552 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1553 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1554 strlen(sep) + 1; 1555 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1556 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1557 linker_hintfile); 1558 1559 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1560 flags = FREAD; 1561 error = vn_open(&nd, &flags, 0, -1); 1562 if (error) 1563 goto bad; 1564 vfslocked = NDHASGIANT(&nd); 1565 NDFREE(&nd, NDF_ONLY_PNBUF); 1566 if (nd.ni_vp->v_type != VREG) 1567 goto bad; 1568 best = cp = NULL; 1569 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td); 1570 if (error) 1571 goto bad; 1572 /* 1573 * XXX: we need to limit this number to some reasonable value 1574 */ 1575 if (vattr.va_size > 100 * 1024) { 1576 printf("hints file too large %ld\n", (long)vattr.va_size); 1577 goto bad; 1578 } 1579 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1580 if (hints == NULL) 1581 goto bad; 1582 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1583 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1584 if (error) 1585 goto bad; 1586 VOP_UNLOCK(nd.ni_vp, 0, td); 1587 vn_close(nd.ni_vp, FREAD, cred, td); 1588 VFS_UNLOCK_GIANT(vfslocked); 1589 nd.ni_vp = NULL; 1590 if (reclen != 0) { 1591 printf("can't read %d\n", reclen); 1592 goto bad; 1593 } 1594 intp = (int *)hints; 1595 ival = *intp++; 1596 if (ival != LINKER_HINTS_VERSION) { 1597 printf("hints file version mismatch %d\n", ival); 1598 goto bad; 1599 } 1600 bufend = hints + vattr.va_size; 1601 recptr = (u_char *)intp; 1602 clen = blen = 0; 1603 while (recptr < bufend && !found) { 1604 intp = (int *)recptr; 1605 reclen = *intp++; 1606 ival = *intp++; 1607 cp = (char *)intp; 1608 switch (ival) { 1609 case MDT_VERSION: 1610 clen = *cp++; 1611 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1612 break; 1613 cp += clen; 1614 INT_ALIGN(hints, cp); 1615 ival = *(int *)cp; 1616 cp += sizeof(int); 1617 clen = *cp++; 1618 if (verinfo == NULL || 1619 ival == verinfo->md_ver_preferred) { 1620 found = 1; 1621 break; 1622 } 1623 if (ival >= verinfo->md_ver_minimum && 1624 ival <= verinfo->md_ver_maximum && 1625 ival > bestver) { 1626 bestver = ival; 1627 best = cp; 1628 blen = clen; 1629 } 1630 break; 1631 default: 1632 break; 1633 } 1634 recptr += reclen + sizeof(int); 1635 } 1636 /* 1637 * Finally check if KLD is in the place 1638 */ 1639 if (found) 1640 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1641 else if (best) 1642 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1643 1644 /* 1645 * KLD is newer than hints file. What we should do now? 1646 */ 1647 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1648 printf("warning: KLD '%s' is newer than the linker.hints" 1649 " file\n", result); 1650bad: 1651 free(pathbuf, M_LINKER); 1652 if (hints) 1653 free(hints, M_TEMP); 1654 if (nd.ni_vp != NULL) { 1655 VOP_UNLOCK(nd.ni_vp, 0, td); 1656 vn_close(nd.ni_vp, FREAD, cred, td); 1657 VFS_UNLOCK_GIANT(vfslocked); 1658 } 1659 /* 1660 * If nothing found or hints is absent - fallback to the old 1661 * way by using "kldname[.ko]" as module name. 1662 */ 1663 if (!found && !bestver && result == NULL) 1664 result = linker_lookup_file(path, pathlen, modname, 1665 modnamelen, NULL); 1666 return (result); 1667} 1668 1669/* 1670 * Lookup KLD which contains requested module in the all directories. 1671 */ 1672static char * 1673linker_search_module(const char *modname, int modnamelen, 1674 struct mod_depend *verinfo) 1675{ 1676 char *cp, *ep, *result; 1677 1678 /* 1679 * traverse the linker path 1680 */ 1681 for (cp = linker_path; *cp; cp = ep + 1) { 1682 /* find the end of this component */ 1683 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1684 result = linker_hints_lookup(cp, ep - cp, modname, 1685 modnamelen, verinfo); 1686 if (result != NULL) 1687 return (result); 1688 if (*ep == 0) 1689 break; 1690 } 1691 return (NULL); 1692} 1693 1694/* 1695 * Search for module in all directories listed in the linker_path. 1696 */ 1697static char * 1698linker_search_kld(const char *name) 1699{ 1700 char *cp, *ep, *result; 1701 int len; 1702 1703 /* qualified at all? */ 1704 if (index(name, '/')) 1705 return (linker_strdup(name)); 1706 1707 /* traverse the linker path */ 1708 len = strlen(name); 1709 for (ep = linker_path; *ep; ep++) { 1710 cp = ep; 1711 /* find the end of this component */ 1712 for (; *ep != 0 && *ep != ';'; ep++); 1713 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1714 if (result != NULL) 1715 return (result); 1716 } 1717 return (NULL); 1718} 1719 1720static const char * 1721linker_basename(const char *path) 1722{ 1723 const char *filename; 1724 1725 filename = rindex(path, '/'); 1726 if (filename == NULL) 1727 return path; 1728 if (filename[1]) 1729 filename++; 1730 return (filename); 1731} 1732 1733#ifdef HWPMC_HOOKS 1734 1735struct hwpmc_context { 1736 int nobjects; 1737 int nmappings; 1738 struct pmckern_map_in *kobase; 1739}; 1740 1741static int 1742linker_hwpmc_list_object(linker_file_t lf, void *arg) 1743{ 1744 struct hwpmc_context *hc; 1745 1746 hc = arg; 1747 1748 /* If we run out of mappings, fail. */ 1749 if (hc->nobjects >= hc->nmappings) 1750 return (1); 1751 1752 /* Save the info for this linker file. */ 1753 hc->kobase[hc->nobjects].pm_file = lf->filename; 1754 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address; 1755 hc->nobjects++; 1756 return (0); 1757} 1758 1759/* 1760 * Inform hwpmc about the set of kernel modules currently loaded. 1761 */ 1762void * 1763linker_hwpmc_list_objects(void) 1764{ 1765 struct hwpmc_context hc; 1766 1767 hc.nmappings = 15; /* a reasonable default */ 1768 1769 retry: 1770 /* allocate nmappings+1 entries */ 1771 MALLOC(hc.kobase, struct pmckern_map_in *, 1772 (hc.nmappings + 1) * sizeof(struct pmckern_map_in), M_LINKER, 1773 M_WAITOK | M_ZERO); 1774 1775 hc.nobjects = 0; 1776 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) { 1777 hc.nmappings = hc.nobjects; 1778 FREE(hc.kobase, M_LINKER); 1779 goto retry; 1780 } 1781 1782 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel " 1783 "objects?")); 1784 1785 /* The last entry of the malloced area comprises of all zeros. */ 1786 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL, 1787 ("linker_hwpmc_list_objects: last object not NULL")); 1788 1789 return ((void *)hc.kobase); 1790} 1791#endif 1792 1793/* 1794 * Find a file which contains given module and load it, if "parent" is not 1795 * NULL, register a reference to it. 1796 */ 1797static int 1798linker_load_module(const char *kldname, const char *modname, 1799 struct linker_file *parent, struct mod_depend *verinfo, 1800 struct linker_file **lfpp) 1801{ 1802 linker_file_t lfdep; 1803 const char *filename; 1804 char *pathname; 1805 int error; 1806 1807 if (modname == NULL) { 1808 /* 1809 * We have to load KLD 1810 */ 1811 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 1812 " is not NULL")); 1813 pathname = linker_search_kld(kldname); 1814 } else { 1815 if (modlist_lookup2(modname, verinfo) != NULL) 1816 return (EEXIST); 1817 if (kldname != NULL) 1818 pathname = linker_strdup(kldname); 1819 else if (rootvnode == NULL) 1820 pathname = NULL; 1821 else 1822 /* 1823 * Need to find a KLD with required module 1824 */ 1825 pathname = linker_search_module(modname, 1826 strlen(modname), verinfo); 1827 } 1828 if (pathname == NULL) 1829 return (ENOENT); 1830 1831 /* 1832 * Can't load more than one file with the same basename XXX: 1833 * Actually it should be possible to have multiple KLDs with 1834 * the same basename but different path because they can 1835 * provide different versions of the same modules. 1836 */ 1837 filename = linker_basename(pathname); 1838 if (linker_find_file_by_name(filename)) 1839 error = EEXIST; 1840 else do { 1841 error = linker_load_file(pathname, &lfdep); 1842 if (error) 1843 break; 1844 if (modname && verinfo && 1845 modlist_lookup2(modname, verinfo) == NULL) { 1846 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 1847 error = ENOENT; 1848 break; 1849 } 1850 if (parent) { 1851 error = linker_file_add_dependency(parent, lfdep); 1852 if (error) 1853 break; 1854 } 1855 if (lfpp) 1856 *lfpp = lfdep; 1857 } while (0); 1858 free(pathname, M_LINKER); 1859 return (error); 1860} 1861 1862/* 1863 * This routine is responsible for finding dependencies of userland initiated 1864 * kldload(2)'s of files. 1865 */ 1866int 1867linker_load_dependencies(linker_file_t lf) 1868{ 1869 linker_file_t lfdep; 1870 struct mod_metadata **start, **stop, **mdp, **nmdp; 1871 struct mod_metadata *mp, *nmp; 1872 struct mod_depend *verinfo; 1873 modlist_t mod; 1874 const char *modname, *nmodname; 1875 int ver, error = 0, count; 1876 1877 /* 1878 * All files are dependant on /kernel. 1879 */ 1880 if (linker_kernel_file) { 1881 linker_kernel_file->refs++; 1882 error = linker_file_add_dependency(lf, linker_kernel_file); 1883 if (error) 1884 return (error); 1885 } 1886 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 1887 &count) != 0) 1888 return (0); 1889 for (mdp = start; mdp < stop; mdp++) { 1890 mp = *mdp; 1891 if (mp->md_type != MDT_VERSION) 1892 continue; 1893 modname = mp->md_cval; 1894 ver = ((struct mod_version *)mp->md_data)->mv_version; 1895 mod = modlist_lookup(modname, ver); 1896 if (mod != NULL) { 1897 printf("interface %s.%d already present in the KLD" 1898 " '%s'!\n", modname, ver, 1899 mod->container->filename); 1900 return (EEXIST); 1901 } 1902 } 1903 1904 for (mdp = start; mdp < stop; mdp++) { 1905 mp = *mdp; 1906 if (mp->md_type != MDT_DEPEND) 1907 continue; 1908 modname = mp->md_cval; 1909 verinfo = mp->md_data; 1910 nmodname = NULL; 1911 for (nmdp = start; nmdp < stop; nmdp++) { 1912 nmp = *nmdp; 1913 if (nmp->md_type != MDT_VERSION) 1914 continue; 1915 nmodname = nmp->md_cval; 1916 if (strcmp(modname, nmodname) == 0) 1917 break; 1918 } 1919 if (nmdp < stop)/* early exit, it's a self reference */ 1920 continue; 1921 mod = modlist_lookup2(modname, verinfo); 1922 if (mod) { /* woohoo, it's loaded already */ 1923 lfdep = mod->container; 1924 lfdep->refs++; 1925 error = linker_file_add_dependency(lf, lfdep); 1926 if (error) 1927 break; 1928 continue; 1929 } 1930 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 1931 if (error) { 1932 printf("KLD %s: depends on %s - not available\n", 1933 lf->filename, modname); 1934 break; 1935 } 1936 } 1937 1938 if (error) 1939 return (error); 1940 linker_addmodules(lf, start, stop, 0); 1941 return (error); 1942} 1943 1944static int 1945sysctl_kern_function_list_iterate(const char *name, void *opaque) 1946{ 1947 struct sysctl_req *req; 1948 1949 req = opaque; 1950 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 1951} 1952 1953/* 1954 * Export a nul-separated, double-nul-terminated list of all function names 1955 * in the kernel. 1956 */ 1957static int 1958sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 1959{ 1960 linker_file_t lf; 1961 int error; 1962 1963#ifdef MAC 1964 error = mac_check_kld_stat(req->td->td_ucred); 1965 if (error) 1966 return (error); 1967#endif 1968 error = sysctl_wire_old_buffer(req, 0); 1969 if (error != 0) 1970 return (error); 1971 mtx_lock(&kld_mtx); 1972 TAILQ_FOREACH(lf, &linker_files, link) { 1973 error = LINKER_EACH_FUNCTION_NAME(lf, 1974 sysctl_kern_function_list_iterate, req); 1975 if (error) { 1976 mtx_unlock(&kld_mtx); 1977 return (error); 1978 } 1979 } 1980 mtx_unlock(&kld_mtx); 1981 return (SYSCTL_OUT(req, "", 1)); 1982} 1983 1984SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 1985 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 1986