kern_linker.c revision 46129
1/*- 2 * Copyright (c) 1997 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 * $Id: kern_linker.c,v 1.30 1999/04/27 11:15:57 phk Exp $ 27 */ 28 29#include "opt_ddb.h" 30 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/systm.h> 34#include <sys/malloc.h> 35#include <sys/sysproto.h> 36#include <sys/sysent.h> 37#include <sys/proc.h> 38#include <sys/lock.h> 39#include <machine/cpu.h> 40#include <machine/bootinfo.h> 41#include <sys/module.h> 42#include <sys/linker.h> 43#include <sys/unistd.h> 44#include <sys/fcntl.h> 45#include <sys/libkern.h> 46#include <sys/namei.h> 47#include <sys/vnode.h> 48#include <sys/sysctl.h> 49 50#ifdef KLD_DEBUG 51int kld_debug = 0; 52#endif 53 54MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 55linker_file_t linker_current_file; 56linker_file_t linker_kernel_file; 57 58static struct lock lock; /* lock for the file list */ 59static linker_class_list_t classes; 60static linker_file_list_t files; 61static int next_file_id = 1; 62 63static void 64linker_init(void* arg) 65{ 66 lockinit(&lock, PVM, "klink", 0, 0); 67 TAILQ_INIT(&classes); 68 TAILQ_INIT(&files); 69} 70 71SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 72 73int 74linker_add_class(const char* desc, void* priv, 75 struct linker_class_ops* ops) 76{ 77 linker_class_t lc; 78 79 lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT); 80 if (!lc) 81 return ENOMEM; 82 bzero(lc, sizeof(*lc)); 83 84 lc->desc = desc; 85 lc->priv = priv; 86 lc->ops = ops; 87 TAILQ_INSERT_HEAD(&classes, lc, link); 88 89 return 0; 90} 91 92static void 93linker_file_sysinit(linker_file_t lf) 94{ 95 struct linker_set* sysinits; 96 struct sysinit** sipp; 97 struct sysinit** xipp; 98 struct sysinit* save; 99 moduledata_t *moddata; 100 101 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 102 lf->filename)); 103 104 sysinits = (struct linker_set*) 105 linker_file_lookup_symbol(lf, "sysinit_set", 0); 106 107 KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits)); 108 if (!sysinits) 109 return; 110 111 /* HACK ALERT! */ 112 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 113 if ((*sipp)->func == module_register_init) { 114 moddata = (*sipp)->udata; 115 moddata->_file = lf; 116 } 117 } 118 119 /* 120 * Perform a bubble sort of the system initialization objects by 121 * their subsystem (primary key) and order (secondary key). 122 * 123 * Since some things care about execution order, this is the 124 * operation which ensures continued function. 125 */ 126 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 127 for (xipp = sipp + 1; *xipp; xipp++) { 128 if ((*sipp)->subsystem <= (*xipp)->subsystem || 129 ((*sipp)->subsystem == (*xipp)->subsystem && 130 (*sipp)->order <= (*xipp)->order)) 131 continue; /* skip*/ 132 save = *sipp; 133 *sipp = *xipp; 134 *xipp = save; 135 } 136 } 137 138 139 /* 140 * Traverse the (now) ordered list of system initialization tasks. 141 * Perform each task, and continue on to the next task. 142 */ 143 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 144 if ((*sipp)->subsystem == SI_SUB_DUMMY) 145 continue; /* skip dummy task(s)*/ 146 147 switch ((*sipp)->type) { 148 case SI_TYPE_DEFAULT: 149 /* no special processing*/ 150 (*((*sipp)->func))((*sipp)->udata); 151 break; 152 153 case SI_TYPE_KTHREAD: 154 /* kernel thread*/ 155 if (fork1(&proc0, RFFDG|RFPROC|RFMEM)) 156 panic("fork kernel thread"); 157 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 158 (*sipp)->func, (*sipp)->udata); 159 break; 160 161 case SI_TYPE_KPROCESS: 162 /* kernel thread*/ 163 if (fork1(&proc0, RFFDG|RFPROC)) 164 panic("fork kernel process"); 165 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 166 (*sipp)->func, (*sipp)->udata); 167 break; 168 169 default: 170 panic ("linker_file_sysinit: unrecognized init type"); 171 } 172 } 173} 174 175static void 176linker_file_sysuninit(linker_file_t lf) 177{ 178 struct linker_set* sysuninits; 179 struct sysinit** sipp; 180 struct sysinit** xipp; 181 struct sysinit* save; 182 183 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 184 lf->filename)); 185 186 sysuninits = (struct linker_set*) 187 linker_file_lookup_symbol(lf, "sysuninit_set", 0); 188 189 KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits)); 190 if (!sysuninits) 191 return; 192 193 /* 194 * Perform a reverse bubble sort of the system initialization objects 195 * by their subsystem (primary key) and order (secondary key). 196 * 197 * Since some things care about execution order, this is the 198 * operation which ensures continued function. 199 */ 200 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 201 for (xipp = sipp + 1; *xipp; xipp++) { 202 if ((*sipp)->subsystem >= (*xipp)->subsystem || 203 ((*sipp)->subsystem == (*xipp)->subsystem && 204 (*sipp)->order >= (*xipp)->order)) 205 continue; /* skip*/ 206 save = *sipp; 207 *sipp = *xipp; 208 *xipp = save; 209 } 210 } 211 212 213 /* 214 * Traverse the (now) ordered list of system initialization tasks. 215 * Perform each task, and continue on to the next task. 216 */ 217 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 218 if ((*sipp)->subsystem == SI_SUB_DUMMY) 219 continue; /* skip dummy task(s)*/ 220 221 switch ((*sipp)->type) { 222 case SI_TYPE_DEFAULT: 223 /* no special processing*/ 224 (*((*sipp)->func))((*sipp)->udata); 225 break; 226 227 default: 228 panic("linker_file_sysuninit: unrecognized uninit type"); 229 } 230 } 231} 232 233static void 234linker_file_register_sysctls(linker_file_t lf) 235{ 236 struct linker_set* sysctls; 237 238 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 239 lf->filename)); 240 241 sysctls = (struct linker_set*) 242 linker_file_lookup_symbol(lf, "sysctl_set", 0); 243 244 KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls)); 245 if (!sysctls) 246 return; 247 248 sysctl_register_set(sysctls); 249} 250 251static void 252linker_file_unregister_sysctls(linker_file_t lf) 253{ 254 struct linker_set* sysctls; 255 256 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 257 lf->filename)); 258 259 sysctls = (struct linker_set*) 260 linker_file_lookup_symbol(lf, "sysctl_set", 0); 261 262 KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls)); 263 if (!sysctls) 264 return; 265 266 sysctl_unregister_set(sysctls); 267} 268 269int 270linker_load_file(const char* filename, linker_file_t* result) 271{ 272 linker_class_t lc; 273 linker_file_t lf; 274 int foundfile, error = 0; 275 char *koname = NULL; 276 277 lf = linker_find_file_by_name(filename); 278 if (lf) { 279 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 280 *result = lf; 281 lf->refs++; 282 goto out; 283 } 284 285 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 286 if (koname == NULL) { 287 error = ENOMEM; 288 goto out; 289 } 290 sprintf(koname, "%s.ko", filename); 291 lf = NULL; 292 foundfile = 0; 293 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 294 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 295 filename, lc->desc)); 296 297 error = lc->ops->load_file(koname, &lf); /* First with .ko */ 298 if (lf == NULL && error == ENOENT) 299 error = lc->ops->load_file(filename, &lf); /* Then try without */ 300 /* 301 * If we got something other than ENOENT, then it exists but we cannot 302 * load it for some other reason. 303 */ 304 if (error != ENOENT) 305 foundfile = 1; 306 if (lf) { 307 linker_file_register_sysctls(lf); 308 linker_file_sysinit(lf); 309 310 *result = lf; 311 error = 0; 312 goto out; 313 } 314 } 315 /* 316 * Less than ideal, but tells the user whether it failed to load or 317 * the module was not found. 318 */ 319 if (foundfile) 320 error = ENOEXEC; /* Format not recognised (or unloadable) */ 321 else 322 error = ENOENT; /* Nothing found */ 323 324out: 325 if (koname) 326 free(koname, M_LINKER); 327 return error; 328} 329 330linker_file_t 331linker_find_file_by_name(const char* filename) 332{ 333 linker_file_t lf = 0; 334 char *koname; 335 336 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 337 if (koname == NULL) 338 goto out; 339 sprintf(koname, "%s.ko", filename); 340 341 lockmgr(&lock, LK_SHARED, 0, curproc); 342 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 343 if (!strcmp(lf->filename, koname)) 344 break; 345 if (!strcmp(lf->filename, filename)) 346 break; 347 } 348 lockmgr(&lock, LK_RELEASE, 0, curproc); 349 350out: 351 if (koname) 352 free(koname, M_LINKER); 353 return lf; 354} 355 356linker_file_t 357linker_find_file_by_id(int fileid) 358{ 359 linker_file_t lf = 0; 360 361 lockmgr(&lock, LK_SHARED, 0, curproc); 362 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) 363 if (lf->id == fileid) 364 break; 365 lockmgr(&lock, LK_RELEASE, 0, curproc); 366 367 return lf; 368} 369 370linker_file_t 371linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 372{ 373 linker_file_t lf = 0; 374 int namelen; 375 const char *filename; 376 377 filename = rindex(pathname, '/'); 378 if (filename && filename[1]) 379 filename++; 380 else 381 filename = pathname; 382 383 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 384 lockmgr(&lock, LK_EXCLUSIVE, 0, curproc); 385 namelen = strlen(filename) + 1; 386 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); 387 if (!lf) 388 goto out; 389 bzero(lf, sizeof(*lf)); 390 391 lf->refs = 1; 392 lf->userrefs = 0; 393 lf->flags = 0; 394 lf->filename = (char*) (lf + 1); 395 strcpy(lf->filename, filename); 396 lf->id = next_file_id++; 397 lf->ndeps = 0; 398 lf->deps = NULL; 399 STAILQ_INIT(&lf->common); 400 TAILQ_INIT(&lf->modules); 401 402 lf->priv = priv; 403 lf->ops = ops; 404 TAILQ_INSERT_TAIL(&files, lf, link); 405 406out: 407 lockmgr(&lock, LK_RELEASE, 0, curproc); 408 return lf; 409} 410 411int 412linker_file_unload(linker_file_t file) 413{ 414 module_t mod, next; 415 struct common_symbol* cp; 416 int error = 0; 417 int i; 418 419 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 420 lockmgr(&lock, LK_EXCLUSIVE, 0, curproc); 421 if (file->refs == 1) { 422 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 423 /* 424 * Inform any modules associated with this file. 425 */ 426 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 427 next = module_getfnext(mod); 428 429 /* 430 * Give the module a chance to veto the unload. 431 */ 432 if ((error = module_unload(mod)) != 0) { 433 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 434 mod)); 435 lockmgr(&lock, LK_RELEASE, 0, curproc); 436 goto out; 437 } 438 439 module_release(mod); 440 } 441 } 442 443 file->refs--; 444 if (file->refs > 0) { 445 lockmgr(&lock, LK_RELEASE, 0, curproc); 446 goto out; 447 } 448 449 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 450 if (file->flags & LINKER_FILE_LINKED) { 451 linker_file_sysuninit(file); 452 linker_file_unregister_sysctls(file); 453 } 454 455 TAILQ_REMOVE(&files, file, link); 456 lockmgr(&lock, LK_RELEASE, 0, curproc); 457 458 for (i = 0; i < file->ndeps; i++) 459 linker_file_unload(file->deps[i]); 460 free(file->deps, M_LINKER); 461 462 for (cp = STAILQ_FIRST(&file->common); cp; 463 cp = STAILQ_FIRST(&file->common)) { 464 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 465 free(cp, M_LINKER); 466 } 467 468 file->ops->unload(file); 469 free(file, M_LINKER); 470 471out: 472 return error; 473} 474 475int 476linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 477{ 478 linker_file_t* newdeps; 479 480 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 481 M_LINKER, M_WAITOK); 482 if (newdeps == NULL) 483 return ENOMEM; 484 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*)); 485 486 if (file->deps) { 487 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 488 free(file->deps, M_LINKER); 489 } 490 file->deps = newdeps; 491 file->deps[file->ndeps] = dep; 492 file->ndeps++; 493 494 return 0; 495} 496 497caddr_t 498linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 499{ 500 c_linker_sym_t sym; 501 linker_symval_t symval; 502 linker_file_t lf; 503 caddr_t address; 504 size_t common_size = 0; 505 int i; 506 507 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 508 file, name, deps)); 509 510 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 511 file->ops->symbol_values(file, sym, &symval); 512 if (symval.value == 0) 513 /* 514 * For commons, first look them up in the dependancies and 515 * only allocate space if not found there. 516 */ 517 common_size = symval.size; 518 else { 519 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 520 return symval.value; 521 } 522 } 523 524 if (deps) { 525 for (i = 0; i < file->ndeps; i++) { 526 address = linker_file_lookup_symbol(file->deps[i], name, 0); 527 if (address) { 528 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 529 return address; 530 } 531 } 532 533 /* If we have not found it in the dependencies, search globally */ 534 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 535 /* But skip the current file if it's on the list */ 536 if (lf == file) 537 continue; 538 /* And skip the files we searched above */ 539 for (i = 0; i < file->ndeps; i++) 540 if (lf == file->deps[i]) 541 break; 542 if (i < file->ndeps) 543 continue; 544 address = linker_file_lookup_symbol(lf, name, 0); 545 if (address) { 546 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address)); 547 return address; 548 } 549 } 550 } 551 552 if (common_size > 0) { 553 /* 554 * This is a common symbol which was not found in the 555 * dependancies. We maintain a simple common symbol table in 556 * the file object. 557 */ 558 struct common_symbol* cp; 559 560 for (cp = STAILQ_FIRST(&file->common); cp; 561 cp = STAILQ_NEXT(cp, link)) 562 if (!strcmp(cp->name, name)) { 563 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 564 return cp->address; 565 } 566 567 /* 568 * Round the symbol size up to align. 569 */ 570 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 571 cp = malloc(sizeof(struct common_symbol) 572 + common_size 573 + strlen(name) + 1, 574 M_LINKER, M_WAITOK); 575 if (!cp) { 576 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 577 return 0; 578 } 579 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); 580 581 cp->address = (caddr_t) (cp + 1); 582 cp->name = cp->address + common_size; 583 strcpy(cp->name, name); 584 bzero(cp->address, common_size); 585 STAILQ_INSERT_TAIL(&file->common, cp, link); 586 587 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 588 return cp->address; 589 } 590 591 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 592 return 0; 593} 594 595#ifdef DDB 596/* 597 * DDB Helpers. DDB has to look across multiple files with their own 598 * symbol tables and string tables. 599 * 600 * Note that we do not obey list locking protocols here. We really don't 601 * need DDB to hang because somebody's got the lock held. We'll take the 602 * chance that the files list is inconsistant instead. 603 */ 604 605int 606linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 607{ 608 linker_file_t lf; 609 610 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 611 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 612 return 0; 613 } 614 return ENOENT; 615} 616 617int 618linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 619{ 620 linker_file_t lf; 621 u_long off = (u_long)value; 622 u_long diff, bestdiff; 623 c_linker_sym_t best; 624 c_linker_sym_t es; 625 626 best = 0; 627 bestdiff = off; 628 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 629 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 630 continue; 631 if (es != 0 && diff < bestdiff) { 632 best = es; 633 bestdiff = diff; 634 } 635 if (bestdiff == 0) 636 break; 637 } 638 if (best) { 639 *sym = best; 640 *diffp = bestdiff; 641 return 0; 642 } else { 643 *sym = 0; 644 *diffp = off; 645 return ENOENT; 646 } 647} 648 649int 650linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 651{ 652 linker_file_t lf; 653 654 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 655 if (lf->ops->symbol_values(lf, sym, symval) == 0) 656 return 0; 657 } 658 return ENOENT; 659} 660 661#endif 662 663/* 664 * Syscalls. 665 */ 666 667int 668kldload(struct proc* p, struct kldload_args* uap) 669{ 670 char* filename = NULL, *modulename; 671 linker_file_t lf; 672 int error = 0; 673 674 p->p_retval[0] = -1; 675 676 if (securelevel > 0) 677 return EPERM; 678 679 if ((error = suser(p)) != 0) 680 return error; 681 682 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 683 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 684 goto out; 685 686 /* Can't load more than one module with the same name */ 687 modulename = rindex(filename, '/'); 688 if (modulename == NULL) 689 modulename = filename; 690 else 691 modulename++; 692 if (linker_find_file_by_name(modulename)) { 693 error = EEXIST; 694 goto out; 695 } 696 697 if ((error = linker_load_file(filename, &lf)) != 0) 698 goto out; 699 700 lf->userrefs++; 701 p->p_retval[0] = lf->id; 702 703out: 704 if (filename) 705 free(filename, M_TEMP); 706 return error; 707} 708 709int 710kldunload(struct proc* p, struct kldunload_args* uap) 711{ 712 linker_file_t lf; 713 int error = 0; 714 715 if (securelevel > 0) 716 return EPERM; 717 718 if ((error = suser(p)) != 0) 719 return error; 720 721 lf = linker_find_file_by_id(SCARG(uap, fileid)); 722 if (lf) { 723 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 724 if (lf->userrefs == 0) { 725 printf("linkerunload: attempt to unload file that was loaded by the kernel\n"); 726 error = EBUSY; 727 goto out; 728 } 729 lf->userrefs--; 730 error = linker_file_unload(lf); 731 if (error) 732 lf->userrefs++; 733 } else 734 error = ENOENT; 735 736out: 737 return error; 738} 739 740int 741kldfind(struct proc* p, struct kldfind_args* uap) 742{ 743 char* filename = NULL, *modulename; 744 linker_file_t lf; 745 int error = 0; 746 747 p->p_retval[0] = -1; 748 749 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 750 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 751 goto out; 752 753 modulename = rindex(filename, '/'); 754 if (modulename == NULL) 755 modulename = filename; 756 757 lf = linker_find_file_by_name(modulename); 758 if (lf) 759 p->p_retval[0] = lf->id; 760 else 761 error = ENOENT; 762 763out: 764 if (filename) 765 free(filename, M_TEMP); 766 return error; 767} 768 769int 770kldnext(struct proc* p, struct kldnext_args* uap) 771{ 772 linker_file_t lf; 773 int error = 0; 774 775 if (SCARG(uap, fileid) == 0) { 776 if (TAILQ_FIRST(&files)) 777 p->p_retval[0] = TAILQ_FIRST(&files)->id; 778 else 779 p->p_retval[0] = 0; 780 return 0; 781 } 782 783 lf = linker_find_file_by_id(SCARG(uap, fileid)); 784 if (lf) { 785 if (TAILQ_NEXT(lf, link)) 786 p->p_retval[0] = TAILQ_NEXT(lf, link)->id; 787 else 788 p->p_retval[0] = 0; 789 } else 790 error = ENOENT; 791 792 return error; 793} 794 795int 796kldstat(struct proc* p, struct kldstat_args* uap) 797{ 798 linker_file_t lf; 799 int error = 0; 800 int version; 801 struct kld_file_stat* stat; 802 int namelen; 803 804 lf = linker_find_file_by_id(SCARG(uap, fileid)); 805 if (!lf) { 806 error = ENOENT; 807 goto out; 808 } 809 810 stat = SCARG(uap, stat); 811 812 /* 813 * Check the version of the user's structure. 814 */ 815 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 816 goto out; 817 if (version != sizeof(struct kld_file_stat)) { 818 error = EINVAL; 819 goto out; 820 } 821 822 namelen = strlen(lf->filename) + 1; 823 if (namelen > MAXPATHLEN) 824 namelen = MAXPATHLEN; 825 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 826 goto out; 827 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 828 goto out; 829 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 830 goto out; 831 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 832 goto out; 833 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 834 goto out; 835 836 p->p_retval[0] = 0; 837 838out: 839 return error; 840} 841 842int 843kldfirstmod(struct proc* p, struct kldfirstmod_args* uap) 844{ 845 linker_file_t lf; 846 int error = 0; 847 848 lf = linker_find_file_by_id(SCARG(uap, fileid)); 849 if (lf) { 850 if (TAILQ_FIRST(&lf->modules)) 851 p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 852 else 853 p->p_retval[0] = 0; 854 } else 855 error = ENOENT; 856 857 return error; 858} 859 860int 861kldsym(struct proc *p, struct kldsym_args *uap) 862{ 863 char *symstr = NULL; 864 c_linker_sym_t sym; 865 linker_symval_t symval; 866 linker_file_t lf; 867 struct kld_sym_lookup lookup; 868 int error = 0; 869 870 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 871 goto out; 872 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 873 error = EINVAL; 874 goto out; 875 } 876 877 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 878 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 879 goto out; 880 881 if (SCARG(uap, fileid) != 0) { 882 lf = linker_find_file_by_id(SCARG(uap, fileid)); 883 if (lf == NULL) { 884 error = ENOENT; 885 goto out; 886 } 887 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 888 lf->ops->symbol_values(lf, sym, &symval) == 0) { 889 lookup.symvalue = (u_long)symval.value; 890 lookup.symsize = symval.size; 891 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 892 } else 893 error = ENOENT; 894 } else { 895 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 896 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 897 lf->ops->symbol_values(lf, sym, &symval) == 0) { 898 lookup.symvalue = (u_long)symval.value; 899 lookup.symsize = symval.size; 900 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 901 break; 902 } 903 } 904 if (!lf) 905 error = ENOENT; 906 } 907out: 908 if (symstr) 909 free(symstr, M_TEMP); 910 return error; 911} 912 913/* 914 * Preloaded module support 915 */ 916 917static void 918linker_preload(void* arg) 919{ 920 caddr_t modptr; 921 char *modname; 922 char *modtype; 923 linker_file_t lf; 924 linker_class_t lc; 925 int error; 926 struct linker_set *sysinits; 927 struct sysinit **sipp; 928 moduledata_t *moddata; 929 930 modptr = NULL; 931 while ((modptr = preload_search_next_name(modptr)) != NULL) { 932 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 933 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 934 if (modname == NULL) { 935 printf("Preloaded module at %p does not have a name!\n", modptr); 936 continue; 937 } 938 if (modtype == NULL) { 939 printf("Preloaded module at %p does not have a type!\n", modptr); 940 continue; 941 } 942 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 943 lf = linker_find_file_by_name(modname); 944 if (lf) { 945 lf->userrefs++; 946 continue; 947 } 948 lf = NULL; 949 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 950 error = lc->ops->load_file(modname, &lf); 951 if (error) { 952 lf = NULL; 953 break; 954 } 955 } 956 if (lf) { 957 lf->userrefs++; 958 959 sysinits = (struct linker_set*) 960 linker_file_lookup_symbol(lf, "sysinit_set", 0); 961 if (sysinits) { 962 /* HACK ALERT! 963 * This is to set the sysinit moduledata so that the module 964 * can attach itself to the correct containing file. 965 * The sysinit could be run at *any* time. 966 */ 967 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 968 if ((*sipp)->func == module_register_init) { 969 moddata = (*sipp)->udata; 970 moddata->_file = lf; 971 } 972 } 973 sysinit_add((struct sysinit **)sysinits->ls_items); 974 } 975 linker_file_register_sysctls(lf); 976 } 977 } 978} 979 980SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 981 982/* 983 * Search for a not-loaded module by name. 984 * 985 * Modules may be found in the following locations: 986 * 987 * - preloaded (result is just the module name) 988 * - on disk (result is full path to module) 989 * 990 * If the module name is qualified in any way (contains path, etc.) 991 * the we simply return a copy of it. 992 * 993 * The search path can be manipulated via sysctl. Note that we use the ';' 994 * character as a separator to be consistent with the bootloader. 995 */ 996 997static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/"; 998 999SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1000 sizeof(linker_path), "module load search path"); 1001 1002static char * 1003linker_strdup(const char *str) 1004{ 1005 char *result; 1006 1007 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 1008 strcpy(result, str); 1009 return(result); 1010} 1011 1012char * 1013linker_search_path(const char *name) 1014{ 1015 struct nameidata nd; 1016 struct proc *p = curproc; /* XXX */ 1017 char *cp, *ep, *result; 1018 int error; 1019 enum vtype type; 1020 1021 /* qualified at all? */ 1022 if (index(name, '/')) 1023 return(linker_strdup(name)); 1024 1025 /* traverse the linker path */ 1026 cp = linker_path; 1027 for (;;) { 1028 1029 /* find the end of this component */ 1030 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1031 ; 1032 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK); 1033 if (result == NULL) /* actually ENOMEM */ 1034 return(NULL); 1035 1036 strncpy(result, cp, ep - cp); 1037 strcpy(result + (ep - cp), name); 1038 1039 /* 1040 * Attempt to open the file, and return the path if we succeed and it's 1041 * a regular file. 1042 */ 1043 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p); 1044 error = vn_open(&nd, FREAD, 0); 1045 if (error == 0) { 1046 type = nd.ni_vp->v_type; 1047 VOP_UNLOCK(nd.ni_vp, 0, p); 1048 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 1049 if (type == VREG) 1050 return(result); 1051 } 1052 free(result, M_LINKER); 1053 1054 if (*ep == 0) 1055 break; 1056 cp = ep + 1; 1057 } 1058 return(NULL); 1059} 1060