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.15 1998/11/06 15:10:17 peter 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 51 int kld_debug = 0; 52 #endif 53 54 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 55 linker_file_t linker_current_file; 56 linker_file_t linker_kernel_file; 57 58 static struct lock lock; /* lock for the file list */ 59 static linker_class_list_t classes; 60 static linker_file_list_t files; 61 static int next_file_id = 1; 62 63 static void 64 linker_init(void* arg) 65 { 66 lockinit(&lock, PVM, "klink", 0, 0); 67 TAILQ_INIT(&classes); 68 TAILQ_INIT(&files); 69 } 70 71 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 72 73 int 74 linker_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 92 static void 93 linker_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 #if !defined(SMP) 155 /* kernel thread*/ 156 if (fork1(&proc0, RFFDG|RFPROC|RFMEM)) 157 panic("fork kernel thread"); 158 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 159 (*sipp)->func, (*sipp)->udata); 160 break; 161 #endif 162 163 case SI_TYPE_KPROCESS: 164 /* kernel thread*/ 165 if (fork1(&proc0, RFFDG|RFPROC)) 166 panic("fork kernel process"); 167 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 168 (*sipp)->func, (*sipp)->udata); 169 break; 170 171 default: 172 panic ("linker_file_sysinit: unrecognized init type"); 173 } 174 } 175 } 176 177 static void 178 linker_file_sysuninit(linker_file_t lf) 179 { 180 struct linker_set* sysuninits; 181 struct sysinit** sipp; 182 struct sysinit** xipp; 183 struct sysinit* save; 184 185 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 186 lf->filename)); 187 188 sysuninits = (struct linker_set*) 189 linker_file_lookup_symbol(lf, "sysuninit_set", 0); 190 191 KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits)); 192 if (!sysuninits) 193 return; 194 195 /* 196 * Perform a reverse bubble sort of the system initialization objects 197 * by their subsystem (primary key) and order (secondary key). 198 * 199 * Since some things care about execution order, this is the 200 * operation which ensures continued function. 201 */ 202 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 203 for (xipp = sipp + 1; *xipp; xipp++) { 204 if ((*sipp)->subsystem >= (*xipp)->subsystem || 205 ((*sipp)->subsystem == (*xipp)->subsystem && 206 (*sipp)->order >= (*xipp)->order)) 207 continue; /* skip*/ 208 save = *sipp; 209 *sipp = *xipp; 210 *xipp = save; 211 } 212 } 213 214 215 /* 216 * Traverse the (now) ordered list of system initialization tasks. 217 * Perform each task, and continue on to the next task. 218 */ 219 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 220 if ((*sipp)->subsystem == SI_SUB_DUMMY) 221 continue; /* skip dummy task(s)*/ 222 223 switch ((*sipp)->type) { 224 case SI_TYPE_DEFAULT: 225 /* no special processing*/ 226 (*((*sipp)->func))((*sipp)->udata); 227 break; 228 229 default: 230 panic("linker_file_sysuninit: unrecognized uninit type"); 231 } 232 } 233 } 234 235 int 236 linker_load_file(const char* filename, linker_file_t* result) 237 { 238 linker_class_t lc; 239 linker_file_t lf; 240 int error = 0; 241 char *koname = NULL; 242 243 lf = linker_find_file_by_name(filename); 244 if (lf) { 245 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 246 *result = lf; 247 lf->refs++; 248 goto out; 249 } 250 251 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 252 if (koname == NULL) { 253 error = ENOMEM; 254 goto out; 255 } 256 sprintf(koname, "%s.ko", filename); 257 lf = NULL; 258 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 259 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 260 filename, lc->desc)); 261 error = lc->ops->load_file(koname, &lf); 262 if (lf == NULL && error && error != ENOENT) 263 goto out; 264 if (lf == NULL) 265 error = lc->ops->load_file(filename, &lf); 266 if (lf == NULL && error && error != ENOENT) 267 goto out; 268 if (lf) { 269 linker_file_sysinit(lf); 270 271 *result = lf; 272 error = 0; 273 goto out; 274 } 275 } 276 error = ENOEXEC; /* format not recognised */ 277 278 out: 279 if (koname) 280 free(koname, M_LINKER); 281 return error; 282 } 283 284 linker_file_t 285 linker_find_file_by_name(const char* filename) 286 { 287 linker_file_t lf = 0; 288 char *koname; 289 290 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 291 if (koname == NULL) 292 goto out; 293 sprintf(koname, "%s.ko", filename); 294 295 lockmgr(&lock, LK_SHARED, 0, curproc); 296 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 297 if (!strcmp(lf->filename, koname)) 298 break; 299 if (!strcmp(lf->filename, filename)) 300 break; 301 } 302 lockmgr(&lock, LK_RELEASE, 0, curproc); 303 304 out: 305 if (koname) 306 free(koname, M_LINKER); 307 return lf; 308 } 309 310 linker_file_t 311 linker_find_file_by_id(int fileid) 312 { 313 linker_file_t lf = 0; 314 315 lockmgr(&lock, LK_SHARED, 0, curproc); 316 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) 317 if (lf->id == fileid) 318 break; 319 lockmgr(&lock, LK_RELEASE, 0, curproc); 320 321 return lf; 322 } 323 324 linker_file_t 325 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 326 { 327 linker_file_t lf = 0; 328 int namelen; 329 const char *filename; 330 331 filename = rindex(pathname, '/'); 332 if (filename && filename[1]) 333 filename++; 334 else 335 filename = pathname; 336 337 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 338 lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); 339 namelen = strlen(filename) + 1; 340 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); 341 if (!lf) 342 goto out; 343 bzero(lf, sizeof(*lf)); 344 345 lf->refs = 1; 346 lf->userrefs = 0; 347 lf->filename = (char*) (lf + 1); 348 strcpy(lf->filename, filename); 349 lf->id = next_file_id++; 350 lf->ndeps = 0; 351 lf->deps = NULL; 352 STAILQ_INIT(&lf->common); 353 TAILQ_INIT(&lf->modules); 354 355 lf->priv = priv; 356 lf->ops = ops; 357 TAILQ_INSERT_TAIL(&files, lf, link); 358 359 out: 360 lockmgr(&lock, LK_RELEASE, 0, curproc); 361 return lf; 362 } 363 364 int 365 linker_file_unload(linker_file_t file) 366 { 367 module_t mod, next; 368 struct common_symbol* cp; 369 int error = 0; 370 int i; 371 372 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 373 lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); 374 if (file->refs == 1) { 375 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 376 /* 377 * Inform any modules associated with this file. 378 */ 379 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 380 next = module_getfnext(mod); 381 382 /* 383 * Give the module a chance to veto the unload. 384 */ 385 if (error = module_unload(mod)) { 386 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 387 mod)); 388 lockmgr(&lock, LK_RELEASE, 0, curproc); 389 goto out; 390 } 391 392 module_release(mod); 393 } 394 } 395 396 file->refs--; 397 if (file->refs > 0) { 398 lockmgr(&lock, LK_RELEASE, 0, curproc); 399 goto out; 400 } 401 402 linker_file_sysuninit(file); 403 404 TAILQ_REMOVE(&files, file, link); 405 lockmgr(&lock, LK_RELEASE, 0, curproc); 406 407 for (i = 0; i < file->ndeps; i++) 408 linker_file_unload(file->deps[i]); 409 free(file->deps, M_LINKER); 410 411 for (cp = STAILQ_FIRST(&file->common); cp; 412 cp = STAILQ_FIRST(&file->common)) { 413 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 414 free(cp, M_LINKER); 415 } 416 417 file->ops->unload(file); 418 free(file, M_LINKER); 419 420 out: 421 return error; 422 } 423 424 int 425 linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 426 { 427 linker_file_t* newdeps; 428 429 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 430 M_LINKER, M_WAITOK); 431 if (newdeps == NULL) 432 return ENOMEM; 433 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*)); 434 435 if (file->deps) { 436 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 437 free(file->deps, M_LINKER); 438 } 439 file->deps = newdeps; 440 file->deps[file->ndeps] = dep; 441 file->ndeps++; 442 443 return 0; 444 } 445 446 caddr_t 447 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 448 { 449 linker_sym_t sym; 450 linker_symval_t symval; 451 caddr_t address; 452 size_t common_size = 0; 453 int i; 454 455 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 456 file, name, deps)); 457 458 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 459 file->ops->symbol_values(file, sym, &symval); 460 if (symval.value == 0) 461 /* 462 * For commons, first look them up in the dependancies and 463 * only allocate space if not found there. 464 */ 465 common_size = symval.size; 466 else { 467 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 468 return symval.value; 469 } 470 } 471 472 if (deps) 473 for (i = 0; i < file->ndeps; i++) { 474 address = linker_file_lookup_symbol(file->deps[i], name, 0); 475 if (address) { 476 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 477 return address; 478 } 479 } 480 481 if (common_size > 0) { 482 /* 483 * This is a common symbol which was not found in the 484 * dependancies. We maintain a simple common symbol table in 485 * the file object. 486 */ 487 struct common_symbol* cp; 488 489 for (cp = STAILQ_FIRST(&file->common); cp; 490 cp = STAILQ_NEXT(cp, link)) 491 if (!strcmp(cp->name, name)) { 492 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 493 return cp->address; 494 } 495 496 /* 497 * Round the symbol size up to align. 498 */ 499 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 500 cp = malloc(sizeof(struct common_symbol) 501 + common_size 502 + strlen(name) + 1, 503 M_LINKER, M_WAITOK); 504 if (!cp) { 505 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 506 return 0; 507 } 508 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); 509 510 cp->address = (caddr_t) (cp + 1); 511 cp->name = cp->address + common_size; 512 strcpy(cp->name, name); 513 bzero(cp->address, common_size); 514 STAILQ_INSERT_TAIL(&file->common, cp, link); 515 516 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 517 return cp->address; 518 } 519 520 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 521 return 0; 522 } 523 524 #ifdef DDB 525 /* 526 * DDB Helpers. DDB has to look across multiple files with their own 527 * symbol tables and string tables. 528 * 529 * Note that we do not obey list locking protocols here. We really don't 530 * need DDB to hang because somebody's got the lock held. We'll take the 531 * chance that the files list is inconsistant instead. 532 */ 533 534 int 535 linker_ddb_lookup(char *symstr, linker_sym_t *sym) 536 { 537 linker_file_t lf; 538 539 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 540 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 541 return 0; 542 } 543 return ENOENT; 544 } 545 546 int 547 linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp) 548 { 549 linker_file_t lf; 550 u_long off = (u_long)value; 551 u_long diff, bestdiff; 552 linker_sym_t best; 553 linker_sym_t es; 554 555 best = 0; 556 bestdiff = off; 557 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 558 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 559 continue; 560 if (es != 0 && diff < bestdiff) { 561 best = es; 562 bestdiff = diff; 563 } 564 if (bestdiff == 0) 565 break; 566 } 567 if (best) { 568 *sym = best; 569 *diffp = bestdiff; 570 return 0; 571 } else { 572 *sym = 0; 573 *diffp = off; 574 return ENOENT; 575 } 576 } 577 578 int 579 linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval) 580 { 581 linker_file_t lf; 582 583 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 584 if (lf->ops->symbol_values(lf, sym, symval) == 0) 585 return 0; 586 } 587 return ENOENT; 588 } 589 590 #endif 591 592 /* 593 * Syscalls. 594 */ 595 596 int 597 kldload(struct proc* p, struct kldload_args* uap) 598 { 599 char* filename = NULL; 600 linker_file_t lf; 601 int error = 0; 602 603 p->p_retval[0] = -1; 604 605 if (securelevel > 0) 606 return EPERM; 607 608 if (error = suser(p->p_ucred, &p->p_acflag)) 609 return error; 610 611 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 612 if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) 613 goto out; 614 615 if (error = linker_load_file(filename, &lf)) 616 goto out; 617 618 lf->userrefs++; 619 p->p_retval[0] = lf->id; 620 621 out: 622 if (filename) 623 free(filename, M_TEMP); 624 return error; 625 } 626 627 int 628 kldunload(struct proc* p, struct kldunload_args* uap) 629 { 630 linker_file_t lf; 631 int error = 0; 632 633 if (securelevel > 0) 634 return EPERM; 635 636 if (error = suser(p->p_ucred, &p->p_acflag)) 637 return error; 638 639 lf = linker_find_file_by_id(SCARG(uap, fileid)); 640 if (lf) { 641 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 642 if (lf->userrefs == 0) { 643 printf("linkerunload: attempt to unload file which was not loaded by user\n"); 644 error = EBUSY; 645 goto out; 646 } 647 lf->userrefs--; 648 error = linker_file_unload(lf); 649 } else 650 error = ENOENT; 651 652 out: 653 return error; 654 } 655 656 int 657 kldfind(struct proc* p, struct kldfind_args* uap) 658 { 659 char* filename = NULL; 660 linker_file_t lf; 661 int error = 0; 662 663 p->p_retval[0] = -1; 664 665 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 666 if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) 667 goto out; 668 669 lf = linker_find_file_by_name(filename); 670 if (lf) 671 p->p_retval[0] = lf->id; 672 else 673 error = ENOENT; 674 675 out: 676 if (filename) 677 free(filename, M_TEMP); 678 return error; 679 } 680 681 int 682 kldnext(struct proc* p, struct kldnext_args* uap) 683 { 684 linker_file_t lf; 685 int error = 0; 686 687 if (SCARG(uap, fileid) == 0) { 688 if (TAILQ_FIRST(&files)) 689 p->p_retval[0] = TAILQ_FIRST(&files)->id; 690 else 691 p->p_retval[0] = 0; 692 return 0; 693 } 694 695 lf = linker_find_file_by_id(SCARG(uap, fileid)); 696 if (lf) { 697 if (TAILQ_NEXT(lf, link)) 698 p->p_retval[0] = TAILQ_NEXT(lf, link)->id; 699 else 700 p->p_retval[0] = 0; 701 } else 702 error = ENOENT; 703 704 return error; 705 } 706 707 int 708 kldstat(struct proc* p, struct kldstat_args* uap) 709 { 710 linker_file_t lf; 711 int error = 0; 712 int version; 713 struct kld_file_stat* stat; 714 int namelen; 715 716 lf = linker_find_file_by_id(SCARG(uap, fileid)); 717 if (!lf) { 718 error = ENOENT; 719 goto out; 720 } 721 722 stat = SCARG(uap, stat); 723 724 /* 725 * Check the version of the user's structure. 726 */ 727 if (error = copyin(&stat->version, &version, sizeof(version))) 728 goto out; 729 if (version != sizeof(struct kld_file_stat)) { 730 error = EINVAL; 731 goto out; 732 } 733 734 namelen = strlen(lf->filename) + 1; 735 if (namelen > MAXPATHLEN) 736 namelen = MAXPATHLEN; 737 if (error = copyout(lf->filename, &stat->name[0], namelen)) 738 goto out; 739 if (error = copyout(&lf->refs, &stat->refs, sizeof(int))) 740 goto out; 741 if (error = copyout(&lf->id, &stat->id, sizeof(int))) 742 goto out; 743 if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) 744 goto out; 745 if (error = copyout(&lf->size, &stat->size, sizeof(size_t))) 746 goto out; 747 748 p->p_retval[0] = 0; 749 750 out: 751 return error; 752 } 753 754 int 755 kldfirstmod(struct proc* p, struct kldfirstmod_args* uap) 756 { 757 linker_file_t lf; 758 int error = 0; 759 760 lf = linker_find_file_by_id(SCARG(uap, fileid)); 761 if (lf) { 762 if (TAILQ_FIRST(&lf->modules)) 763 p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 764 else 765 p->p_retval[0] = 0; 766 } else 767 error = ENOENT; 768 769 return error; 770 } 771 772 /* 773 * Preloaded module support 774 */ 775 776 static void 777 linker_preload(void* arg) 778 { 779 caddr_t modptr; 780 char *modname; 781 char *modtype; 782 linker_file_t lf; 783 linker_class_t lc; 784 int error; 785 struct linker_set *sysinits; 786 struct sysinit **sipp; 787 moduledata_t *moddata; 788 789 modptr = NULL; 790 while ((modptr = preload_search_next_name(modptr)) != NULL) { 791 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 792 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 793 if (modname == NULL) { 794 printf("Preloaded module at %p does not have a name!\n", modptr); 795 continue; 796 } 797 if (modtype == NULL) { 798 printf("Preloaded module at %p does not have a type!\n", modptr); 799 continue; 800 } 801 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 802 lf = linker_find_file_by_name(modname); 803 if (lf) { 804 lf->userrefs++; 805 continue; 806 } 807 lf = NULL; 808 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 809 error = lc->ops->load_file(modname, &lf); 810 if (error) { 811 lf = NULL; 812 break; 813 } 814 } 815 if (lf) { 816 lf->userrefs++; 817 818 sysinits = (struct linker_set*) 819 linker_file_lookup_symbol(lf, "sysinit_set", 0); 820 if (sysinits) { 821 /* HACK ALERT! 822 * This is to set the sysinit moduledata so that the module 823 * can attach itself to the correct containing file. 824 * The sysinit could be run at *any* time. 825 */ 826 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 827 if ((*sipp)->func == module_register_init) { 828 moddata = (*sipp)->udata; 829 moddata->_file = lf; 830 } 831 } 832 sysinit_add((struct sysinit **)sysinits->ls_items); 833 } 834 } 835 } 836 } 837 838 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 839 840 /* 841 * Search for a not-loaded module by name. 842 * 843 * Modules may be found in the following locations: 844 * 845 * - preloaded (result is just the module name) 846 * - on disk (result is full path to module) 847 * 848 * If the module name is qualified in any way (contains path, etc.) 849 * the we simply return a copy of it. 850 * 851 * The search path can be manipulated via sysctl. Note that we use the ';' 852 * character as a separator to be consistent with the bootloader. 853 */ 854 855 static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/"; 856 857 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 858 sizeof(linker_path), "module load search path"); 859 860 static char * 861 linker_strdup(const char *str) 862 { 863 char *result; 864 865 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 866 strcpy(result, str); 867 return(result); 868 } 869 870 char * 871 linker_search_path(const char *name) 872 { 873 struct nameidata nd; 874 struct proc *p = curproc; /* XXX */ 875 char *cp, *ep, *result; 876 int error; 877 enum vtype type; 878 879 /* qualified at all? */ 880 if (index(name, '/')) 881 return(linker_strdup(name)); 882 883 /* traverse the linker path */ 884 cp = linker_path; 885 for (;;) { 886 887 /* find the end of this component */ 888 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 889 ; 890 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK); 891 if (result == NULL) /* actually ENOMEM */ 892 return(NULL); 893 894 strncpy(result, cp, ep - cp); 895 strcpy(result + (ep - cp), name); 896 897 /* 898 * Attempt to open the file, and return the path if we succeed and it's 899 * a regular file. 900 */ 901 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p); 902 error = vn_open(&nd, FREAD, 0); 903 if (error == 0) { 904 type = nd.ni_vp->v_type; 905 VOP_UNLOCK(nd.ni_vp, 0, p); 906 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 907 if (type == VREG) 908 return(result); 909 } 910 free(result, M_LINKER); 911 912 if (*ep == 0) 913 break; 914 cp = ep + 1; 915 } 916 return(NULL); 917 } 918