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