kern_linker.c revision 159840
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 159840 2006-06-21 17:47:45Z 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#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, vfslocked; 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 | MPSAFE, UIO_SYSSPACE, result, td); 1508 flags = FREAD; 1509 error = vn_open(&nd, &flags, 0, -1); 1510 if (error == 0) { 1511 vfslocked = NDHASGIANT(&nd); 1512 NDFREE(&nd, NDF_ONLY_PNBUF); 1513 type = nd.ni_vp->v_type; 1514 if (vap) 1515 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td); 1516 VOP_UNLOCK(nd.ni_vp, 0, td); 1517 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1518 VFS_UNLOCK_GIANT(vfslocked); 1519 if (type == VREG) 1520 return (result); 1521 } 1522 } 1523 free(result, M_LINKER); 1524 return (NULL); 1525} 1526 1527#define INT_ALIGN(base, ptr) ptr = \ 1528 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1)) 1529 1530/* 1531 * Lookup KLD which contains requested module in the "linker.hints" file. If 1532 * version specification is available, then try to find the best KLD. 1533 * Otherwise just find the latest one. 1534 */ 1535static char * 1536linker_hints_lookup(const char *path, int pathlen, const char *modname, 1537 int modnamelen, struct mod_depend *verinfo) 1538{ 1539 struct thread *td = curthread; /* XXX */ 1540 struct ucred *cred = td ? td->td_ucred : NULL; 1541 struct nameidata nd; 1542 struct vattr vattr, mattr; 1543 u_char *hints = NULL; 1544 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1545 int error, ival, bestver, *intp, reclen, found, flags, clen, blen; 1546 int vfslocked = 0; 1547 1548 result = NULL; 1549 bestver = found = 0; 1550 1551 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1552 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1553 strlen(sep) + 1; 1554 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1555 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1556 linker_hintfile); 1557 1558 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td); 1559 flags = FREAD; 1560 error = vn_open(&nd, &flags, 0, -1); 1561 if (error) 1562 goto bad; 1563 vfslocked = NDHASGIANT(&nd); 1564 NDFREE(&nd, NDF_ONLY_PNBUF); 1565 if (nd.ni_vp->v_type != VREG) 1566 goto bad; 1567 best = cp = NULL; 1568 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td); 1569 if (error) 1570 goto bad; 1571 /* 1572 * XXX: we need to limit this number to some reasonable value 1573 */ 1574 if (vattr.va_size > 100 * 1024) { 1575 printf("hints file too large %ld\n", (long)vattr.va_size); 1576 goto bad; 1577 } 1578 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1579 if (hints == NULL) 1580 goto bad; 1581 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1582 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1583 if (error) 1584 goto bad; 1585 VOP_UNLOCK(nd.ni_vp, 0, td); 1586 vn_close(nd.ni_vp, FREAD, cred, td); 1587 VFS_UNLOCK_GIANT(vfslocked); 1588 nd.ni_vp = NULL; 1589 if (reclen != 0) { 1590 printf("can't read %d\n", reclen); 1591 goto bad; 1592 } 1593 intp = (int *)hints; 1594 ival = *intp++; 1595 if (ival != LINKER_HINTS_VERSION) { 1596 printf("hints file version mismatch %d\n", ival); 1597 goto bad; 1598 } 1599 bufend = hints + vattr.va_size; 1600 recptr = (u_char *)intp; 1601 clen = blen = 0; 1602 while (recptr < bufend && !found) { 1603 intp = (int *)recptr; 1604 reclen = *intp++; 1605 ival = *intp++; 1606 cp = (char *)intp; 1607 switch (ival) { 1608 case MDT_VERSION: 1609 clen = *cp++; 1610 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1611 break; 1612 cp += clen; 1613 INT_ALIGN(hints, cp); 1614 ival = *(int *)cp; 1615 cp += sizeof(int); 1616 clen = *cp++; 1617 if (verinfo == NULL || 1618 ival == verinfo->md_ver_preferred) { 1619 found = 1; 1620 break; 1621 } 1622 if (ival >= verinfo->md_ver_minimum && 1623 ival <= verinfo->md_ver_maximum && 1624 ival > bestver) { 1625 bestver = ival; 1626 best = cp; 1627 blen = clen; 1628 } 1629 break; 1630 default: 1631 break; 1632 } 1633 recptr += reclen + sizeof(int); 1634 } 1635 /* 1636 * Finally check if KLD is in the place 1637 */ 1638 if (found) 1639 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1640 else if (best) 1641 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1642 1643 /* 1644 * KLD is newer than hints file. What we should do now? 1645 */ 1646 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1647 printf("warning: KLD '%s' is newer than the linker.hints" 1648 " file\n", result); 1649bad: 1650 free(pathbuf, M_LINKER); 1651 if (hints) 1652 free(hints, M_TEMP); 1653 if (nd.ni_vp != NULL) { 1654 VOP_UNLOCK(nd.ni_vp, 0, td); 1655 vn_close(nd.ni_vp, FREAD, cred, td); 1656 VFS_UNLOCK_GIANT(vfslocked); 1657 } 1658 /* 1659 * If nothing found or hints is absent - fallback to the old 1660 * way by using "kldname[.ko]" as module name. 1661 */ 1662 if (!found && !bestver && result == NULL) 1663 result = linker_lookup_file(path, pathlen, modname, 1664 modnamelen, NULL); 1665 return (result); 1666} 1667 1668/* 1669 * Lookup KLD which contains requested module in the all directories. 1670 */ 1671static char * 1672linker_search_module(const char *modname, int modnamelen, 1673 struct mod_depend *verinfo) 1674{ 1675 char *cp, *ep, *result; 1676 1677 /* 1678 * traverse the linker path 1679 */ 1680 for (cp = linker_path; *cp; cp = ep + 1) { 1681 /* find the end of this component */ 1682 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1683 result = linker_hints_lookup(cp, ep - cp, modname, 1684 modnamelen, verinfo); 1685 if (result != NULL) 1686 return (result); 1687 if (*ep == 0) 1688 break; 1689 } 1690 return (NULL); 1691} 1692 1693/* 1694 * Search for module in all directories listed in the linker_path. 1695 */ 1696static char * 1697linker_search_kld(const char *name) 1698{ 1699 char *cp, *ep, *result; 1700 int len; 1701 1702 /* qualified at all? */ 1703 if (index(name, '/')) 1704 return (linker_strdup(name)); 1705 1706 /* traverse the linker path */ 1707 len = strlen(name); 1708 for (ep = linker_path; *ep; ep++) { 1709 cp = ep; 1710 /* find the end of this component */ 1711 for (; *ep != 0 && *ep != ';'; ep++); 1712 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1713 if (result != NULL) 1714 return (result); 1715 } 1716 return (NULL); 1717} 1718 1719static const char * 1720linker_basename(const char *path) 1721{ 1722 const char *filename; 1723 1724 filename = rindex(path, '/'); 1725 if (filename == NULL) 1726 return path; 1727 if (filename[1]) 1728 filename++; 1729 return (filename); 1730} 1731 1732#ifdef HWPMC_HOOKS 1733 1734struct hwpmc_context { 1735 int nobjects; 1736 int nmappings; 1737 struct pmckern_map_in *kobase; 1738}; 1739 1740static int 1741linker_hwpmc_list_object(linker_file_t lf, void *arg) 1742{ 1743 struct hwpmc_context *hc; 1744 1745 hc = arg; 1746 1747 /* If we run out of mappings, fail. */ 1748 if (hc->nobjects >= hc->nmappings) 1749 return (1); 1750 1751 /* Save the info for this linker file. */ 1752 hc->kobase[hc->nobjects].pm_file = lf->filename; 1753 hc->kobase[hc->nobjects].pm_address = (uintptr_t)lf->address; 1754 hc->nobjects++; 1755 return (0); 1756} 1757 1758/* 1759 * Inform hwpmc about the set of kernel modules currently loaded. 1760 */ 1761void * 1762linker_hwpmc_list_objects(void) 1763{ 1764 struct hwpmc_context hc; 1765 1766 hc.nmappings = 15; /* a reasonable default */ 1767 1768 retry: 1769 /* allocate nmappings+1 entries */ 1770 MALLOC(hc.kobase, struct pmckern_map_in *, 1771 (hc.nmappings + 1) * sizeof(struct pmckern_map_in), M_LINKER, 1772 M_WAITOK | M_ZERO); 1773 1774 hc.nobjects = 0; 1775 if (linker_file_foreach(linker_hwpmc_list_object, &hc) != 0) { 1776 hc.nmappings = hc.nobjects; 1777 FREE(hc.kobase, M_LINKER); 1778 goto retry; 1779 } 1780 1781 KASSERT(hc.nobjects > 0, ("linker_hpwmc_list_objects: no kernel " 1782 "objects?")); 1783 1784 /* The last entry of the malloced area comprises of all zeros. */ 1785 KASSERT(hc.kobase[hc.nobjects].pm_file == NULL, 1786 ("linker_hwpmc_list_objects: last object not NULL")); 1787 1788 return ((void *)hc.kobase); 1789} 1790#endif 1791 1792/* 1793 * Find a file which contains given module and load it, if "parent" is not 1794 * NULL, register a reference to it. 1795 */ 1796static int 1797linker_load_module(const char *kldname, const char *modname, 1798 struct linker_file *parent, struct mod_depend *verinfo, 1799 struct linker_file **lfpp) 1800{ 1801 linker_file_t lfdep; 1802 const char *filename; 1803 char *pathname; 1804 int error; 1805 1806 if (modname == NULL) { 1807 /* 1808 * We have to load KLD 1809 */ 1810 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 1811 " is not NULL")); 1812 pathname = linker_search_kld(kldname); 1813 } else { 1814 if (modlist_lookup2(modname, verinfo) != NULL) 1815 return (EEXIST); 1816 if (kldname != NULL) 1817 pathname = linker_strdup(kldname); 1818 else if (rootvnode == NULL) 1819 pathname = NULL; 1820 else 1821 /* 1822 * Need to find a KLD with required module 1823 */ 1824 pathname = linker_search_module(modname, 1825 strlen(modname), verinfo); 1826 } 1827 if (pathname == NULL) 1828 return (ENOENT); 1829 1830 /* 1831 * Can't load more than one file with the same basename XXX: 1832 * Actually it should be possible to have multiple KLDs with 1833 * the same basename but different path because they can 1834 * provide different versions of the same modules. 1835 */ 1836 filename = linker_basename(pathname); 1837 if (linker_find_file_by_name(filename)) 1838 error = EEXIST; 1839 else do { 1840 error = linker_load_file(pathname, &lfdep); 1841 if (error) 1842 break; 1843 if (modname && verinfo && 1844 modlist_lookup2(modname, verinfo) == NULL) { 1845 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 1846 error = ENOENT; 1847 break; 1848 } 1849 if (parent) { 1850 error = linker_file_add_dependency(parent, lfdep); 1851 if (error) 1852 break; 1853 } 1854 if (lfpp) 1855 *lfpp = lfdep; 1856 } while (0); 1857 free(pathname, M_LINKER); 1858 return (error); 1859} 1860 1861/* 1862 * This routine is responsible for finding dependencies of userland initiated 1863 * kldload(2)'s of files. 1864 */ 1865int 1866linker_load_dependencies(linker_file_t lf) 1867{ 1868 linker_file_t lfdep; 1869 struct mod_metadata **start, **stop, **mdp, **nmdp; 1870 struct mod_metadata *mp, *nmp; 1871 struct mod_depend *verinfo; 1872 modlist_t mod; 1873 const char *modname, *nmodname; 1874 int ver, error = 0, count; 1875 1876 /* 1877 * All files are dependant on /kernel. 1878 */ 1879 if (linker_kernel_file) { 1880 linker_kernel_file->refs++; 1881 error = linker_file_add_dependency(lf, linker_kernel_file); 1882 if (error) 1883 return (error); 1884 } 1885 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 1886 &count) != 0) 1887 return (0); 1888 for (mdp = start; mdp < stop; mdp++) { 1889 mp = *mdp; 1890 if (mp->md_type != MDT_VERSION) 1891 continue; 1892 modname = mp->md_cval; 1893 ver = ((struct mod_version *)mp->md_data)->mv_version; 1894 mod = modlist_lookup(modname, ver); 1895 if (mod != NULL) { 1896 printf("interface %s.%d already present in the KLD" 1897 " '%s'!\n", modname, ver, 1898 mod->container->filename); 1899 return (EEXIST); 1900 } 1901 } 1902 1903 for (mdp = start; mdp < stop; mdp++) { 1904 mp = *mdp; 1905 if (mp->md_type != MDT_DEPEND) 1906 continue; 1907 modname = mp->md_cval; 1908 verinfo = mp->md_data; 1909 nmodname = NULL; 1910 for (nmdp = start; nmdp < stop; nmdp++) { 1911 nmp = *nmdp; 1912 if (nmp->md_type != MDT_VERSION) 1913 continue; 1914 nmodname = nmp->md_cval; 1915 if (strcmp(modname, nmodname) == 0) 1916 break; 1917 } 1918 if (nmdp < stop)/* early exit, it's a self reference */ 1919 continue; 1920 mod = modlist_lookup2(modname, verinfo); 1921 if (mod) { /* woohoo, it's loaded already */ 1922 lfdep = mod->container; 1923 lfdep->refs++; 1924 error = linker_file_add_dependency(lf, lfdep); 1925 if (error) 1926 break; 1927 continue; 1928 } 1929 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 1930 if (error) { 1931 printf("KLD %s: depends on %s - not available\n", 1932 lf->filename, modname); 1933 break; 1934 } 1935 } 1936 1937 if (error) 1938 return (error); 1939 linker_addmodules(lf, start, stop, 0); 1940 return (error); 1941} 1942 1943static int 1944sysctl_kern_function_list_iterate(const char *name, void *opaque) 1945{ 1946 struct sysctl_req *req; 1947 1948 req = opaque; 1949 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 1950} 1951 1952/* 1953 * Export a nul-separated, double-nul-terminated list of all function names 1954 * in the kernel. 1955 */ 1956static int 1957sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 1958{ 1959 linker_file_t lf; 1960 int error; 1961 1962#ifdef MAC 1963 error = mac_check_kld_stat(req->td->td_ucred); 1964 if (error) 1965 return (error); 1966#endif 1967 error = sysctl_wire_old_buffer(req, 0); 1968 if (error != 0) 1969 return (error); 1970 mtx_lock(&kld_mtx); 1971 TAILQ_FOREACH(lf, &linker_files, link) { 1972 error = LINKER_EACH_FUNCTION_NAME(lf, 1973 sysctl_kern_function_list_iterate, req); 1974 if (error) { 1975 mtx_unlock(&kld_mtx); 1976 return (error); 1977 } 1978 } 1979 mtx_unlock(&kld_mtx); 1980 return (SYSCTL_OUT(req, "", 1)); 1981} 1982 1983SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD, 1984 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 1985