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