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