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