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 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $ 27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.15 2003/11/20 22:07:33 dillon Exp $ 28 */ 29 30 #include "opt_ddb.h" 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/sysproto.h> 37 #include <sys/sysent.h> 38 #include <sys/proc.h> 39 #include <sys/lock.h> 40 #include <sys/module.h> 41 #include <sys/linker.h> 42 #include <sys/fcntl.h> 43 #include <sys/libkern.h> 44 #include <sys/namei.h> 45 #include <sys/vnode.h> 46 #include <sys/sysctl.h> 47 48 #include <vm/vm_zone.h> 49 50 #ifdef KLD_DEBUG 51 int kld_debug = 0; 52 #endif 53 54 /* Metadata from the static kernel */ 55 SET_DECLARE(modmetadata_set, struct mod_metadata); 56 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 57 58 linker_file_t linker_current_file; 59 linker_file_t linker_kernel_file; 60 61 static struct lock lock; /* lock for the file list */ 62 static linker_class_list_t classes; 63 static linker_file_list_t linker_files; 64 static int next_file_id = 1; 65 66 static void 67 linker_init(void* arg) 68 { 69 lockinit(&lock, 0, "klink", 0, 0); 70 TAILQ_INIT(&classes); 71 TAILQ_INIT(&linker_files); 72 } 73 74 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 75 76 int 77 linker_add_class(const char* desc, void* priv, 78 struct linker_class_ops* ops) 79 { 80 linker_class_t lc; 81 82 lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT); 83 if (!lc) 84 return ENOMEM; 85 bzero(lc, sizeof(*lc)); 86 87 lc->desc = desc; 88 lc->priv = priv; 89 lc->ops = ops; 90 TAILQ_INSERT_HEAD(&classes, lc, link); 91 92 return 0; 93 } 94 95 static int 96 linker_file_sysinit(linker_file_t lf) 97 { 98 struct sysinit** start, ** stop; 99 struct sysinit** sipp; 100 struct sysinit** xipp; 101 struct sysinit* save; 102 const moduledata_t *moddata; 103 int error; 104 105 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 106 lf->filename)); 107 108 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 109 return 0; /* XXX is this correct ? No sysinit ? */ 110 111 /* HACK ALERT! */ 112 for (sipp = start; sipp < stop; sipp++) { 113 if ((*sipp)->func == module_register_init) { 114 moddata = (*sipp)->udata; 115 error = module_register(moddata, lf); 116 if (error) { 117 printf("linker_file_sysinit \"%s\" failed to register! %d\n", 118 lf->filename, error); 119 return error; 120 } 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 = start; sipp < stop; sipp++) { 132 for (xipp = sipp + 1; xipp < stop; 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 = start; sipp < stop; sipp++) { 149 if ((*sipp)->subsystem == SI_SUB_DUMMY) 150 continue; /* skip dummy task(s)*/ 151 152 /* Call function */ 153 (*((*sipp)->func))((*sipp)->udata); 154 } 155 return 0; /* no errors */ 156 } 157 158 static void 159 linker_file_sysuninit(linker_file_t lf) 160 { 161 struct sysinit** start, ** stop; 162 struct sysinit** sipp; 163 struct sysinit** xipp; 164 struct sysinit* save; 165 166 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 167 lf->filename)); 168 169 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0) 170 return; 171 172 /* 173 * Perform a reverse bubble sort of the system initialization objects 174 * by their subsystem (primary key) and order (secondary key). 175 * 176 * Since some things care about execution order, this is the 177 * operation 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 /* 193 * Traverse the (now) ordered list of system initialization tasks. 194 * Perform each task, and continue on to the next task. 195 */ 196 for (sipp = start; sipp < stop; sipp++) { 197 if ((*sipp)->subsystem == SI_SUB_DUMMY) 198 continue; /* skip dummy task(s)*/ 199 200 /* Call function */ 201 (*((*sipp)->func))((*sipp)->udata); 202 } 203 } 204 205 static void 206 linker_file_register_sysctls(linker_file_t lf) 207 { 208 struct sysctl_oid **start, **stop, **oidp; 209 210 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 211 lf->filename)); 212 213 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 214 return; 215 for (oidp = start; oidp < stop; oidp++) 216 sysctl_register_oid(*oidp); 217 } 218 219 static void 220 linker_file_unregister_sysctls(linker_file_t lf) 221 { 222 struct sysctl_oid **start, **stop, **oidp; 223 224 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 225 lf->filename)); 226 227 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 228 return; 229 for (oidp = start; oidp < stop; oidp++) 230 sysctl_unregister_oid(*oidp); 231 } 232 233 int 234 linker_load_file(const char* filename, linker_file_t* result) 235 { 236 linker_class_t lc; 237 linker_file_t lf; 238 int foundfile, error = 0; 239 char *koname = NULL; 240 241 /* Refuse to load modules if securelevel raised */ 242 if (securelevel > 0) 243 return EPERM; 244 245 lf = linker_find_file_by_name(filename); 246 if (lf) { 247 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 248 *result = lf; 249 lf->refs++; 250 goto out; 251 } 252 if (find_mod_metadata(filename)) { 253 if (linker_kernel_file) 254 ++linker_kernel_file->refs; 255 *result = linker_kernel_file; 256 goto out; 257 } 258 259 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 260 if (koname == NULL) { 261 error = ENOMEM; 262 goto out; 263 } 264 sprintf(koname, "%s.ko", filename); 265 lf = NULL; 266 foundfile = 0; 267 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 268 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 269 filename, lc->desc)); 270 271 error = lc->ops->load_file(koname, &lf); /* First with .ko */ 272 if (lf == NULL && error == ENOENT) 273 error = lc->ops->load_file(filename, &lf); /* Then try without */ 274 /* 275 * If we got something other than ENOENT, then it exists but we cannot 276 * load it for some other reason. 277 */ 278 if (error != ENOENT) 279 foundfile = 1; 280 if (lf) { 281 linker_file_register_sysctls(lf); 282 error = linker_file_sysinit(lf); 283 284 *result = lf; 285 goto out; 286 } 287 } 288 /* 289 * Less than ideal, but tells the user whether it failed to load or 290 * the module was not found. 291 */ 292 if (foundfile) { 293 /* 294 * Format not recognized or otherwise unloadable. 295 * When loading a module that is statically built into 296 * the kernel EEXIST percolates back up as the return 297 * value. Preserve this so that apps like sysinstall 298 * can recognize this special case and not post bogus 299 * dialog messages. 300 */ 301 if (error != EEXIST) 302 error = ENOEXEC; 303 } else { 304 error = ENOENT; /* Nothing found */ 305 } 306 307 out: 308 if (koname) 309 free(koname, M_LINKER); 310 return error; 311 } 312 313 linker_file_t 314 linker_find_file_by_name(const char* filename) 315 { 316 linker_file_t lf = 0; 317 char *koname; 318 319 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 320 if (koname == NULL) 321 goto out; 322 sprintf(koname, "%s.ko", filename); 323 324 lockmgr(&lock, LK_SHARED, 0, curthread); 325 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 326 if (!strcmp(lf->filename, koname)) 327 break; 328 if (!strcmp(lf->filename, filename)) 329 break; 330 } 331 lockmgr(&lock, LK_RELEASE, 0, curthread); 332 333 out: 334 if (koname) 335 free(koname, M_LINKER); 336 return lf; 337 } 338 339 linker_file_t 340 linker_find_file_by_id(int fileid) 341 { 342 linker_file_t lf = 0; 343 344 lockmgr(&lock, LK_SHARED, 0, curthread); 345 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) 346 if (lf->id == fileid) 347 break; 348 lockmgr(&lock, LK_RELEASE, 0, curthread); 349 350 return lf; 351 } 352 353 linker_file_t 354 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 355 { 356 linker_file_t lf = 0; 357 int namelen; 358 const char *filename; 359 360 filename = rindex(pathname, '/'); 361 if (filename && filename[1]) 362 filename++; 363 else 364 filename = pathname; 365 366 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 367 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 368 namelen = strlen(filename) + 1; 369 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); 370 if (!lf) 371 goto out; 372 bzero(lf, sizeof(*lf)); 373 374 lf->refs = 1; 375 lf->userrefs = 0; 376 lf->flags = 0; 377 lf->filename = (char*) (lf + 1); 378 strcpy(lf->filename, filename); 379 lf->id = next_file_id++; 380 lf->ndeps = 0; 381 lf->deps = NULL; 382 STAILQ_INIT(&lf->common); 383 TAILQ_INIT(&lf->modules); 384 385 lf->priv = priv; 386 lf->ops = ops; 387 TAILQ_INSERT_TAIL(&linker_files, lf, link); 388 389 out: 390 lockmgr(&lock, LK_RELEASE, 0, curthread); 391 return lf; 392 } 393 394 int 395 linker_file_unload(linker_file_t file) 396 { 397 module_t mod, next; 398 struct common_symbol* cp; 399 int error = 0; 400 int i; 401 402 /* Refuse to unload modules if securelevel raised */ 403 if (securelevel > 0) 404 return EPERM; 405 406 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 407 lockmgr(&lock, LK_EXCLUSIVE, 0, curthread); 408 if (file->refs == 1) { 409 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 410 /* 411 * Inform any modules associated with this file. 412 */ 413 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 414 next = module_getfnext(mod); 415 416 /* 417 * Give the module a chance to veto the unload. 418 */ 419 if ((error = module_unload(mod)) != 0) { 420 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 421 mod)); 422 lockmgr(&lock, LK_RELEASE, 0, curthread); 423 goto out; 424 } 425 426 module_release(mod); 427 } 428 } 429 430 file->refs--; 431 if (file->refs > 0) { 432 lockmgr(&lock, LK_RELEASE, 0, curthread); 433 goto out; 434 } 435 436 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 437 if (file->flags & LINKER_FILE_LINKED) { 438 linker_file_sysuninit(file); 439 linker_file_unregister_sysctls(file); 440 } 441 442 TAILQ_REMOVE(&linker_files, file, link); 443 lockmgr(&lock, LK_RELEASE, 0, curthread); 444 445 for (i = 0; i < file->ndeps; i++) 446 linker_file_unload(file->deps[i]); 447 free(file->deps, M_LINKER); 448 449 for (cp = STAILQ_FIRST(&file->common); cp; 450 cp = STAILQ_FIRST(&file->common)) { 451 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 452 free(cp, M_LINKER); 453 } 454 455 file->ops->unload(file); 456 free(file, M_LINKER); 457 458 out: 459 return error; 460 } 461 462 int 463 linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 464 { 465 linker_file_t* newdeps; 466 467 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 468 M_LINKER, M_WAITOK); 469 if (newdeps == NULL) 470 return ENOMEM; 471 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*)); 472 473 if (file->deps) { 474 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 475 free(file->deps, M_LINKER); 476 } 477 file->deps = newdeps; 478 file->deps[file->ndeps] = dep; 479 file->ndeps++; 480 481 return 0; 482 } 483 484 /* 485 * Locate a linker set and its contents. 486 * This is a helper function to avoid linker_if.h exposure elsewhere. 487 * Note: firstp and lastp are really void *** 488 */ 489 int 490 linker_file_lookup_set(linker_file_t file, const char *name, 491 void *firstp, void *lastp, int *countp) 492 { 493 return file->ops->lookup_set(file, name, firstp, lastp, countp); 494 } 495 496 int 497 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr) 498 { 499 c_linker_sym_t sym; 500 linker_symval_t symval; 501 linker_file_t lf; 502 size_t common_size = 0; 503 int i; 504 505 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 506 file, name, deps)); 507 508 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 509 file->ops->symbol_values(file, sym, &symval); 510 511 /* 512 * XXX Assume a common symbol if its value is 0 and it has a non-zero 513 * size, otherwise it could be an absolute symbol with a value of 0. 514 */ 515 if (symval.value == 0 && symval.size != 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 *raddr = symval.value; 524 return 0; 525 } 526 } 527 if (deps) { 528 for (i = 0; i < file->ndeps; i++) { 529 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) { 530 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr)); 531 return 0; 532 } 533 } 534 535 /* If we have not found it in the dependencies, search globally */ 536 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 537 /* But skip the current file if it's on the list */ 538 if (lf == file) 539 continue; 540 /* And skip the files we searched above */ 541 for (i = 0; i < file->ndeps; i++) 542 if (lf == file->deps[i]) 543 break; 544 if (i < file->ndeps) 545 continue; 546 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) { 547 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr)); 548 return 0; 549 } 550 } 551 } 552 553 if (common_size > 0) { 554 /* 555 * This is a common symbol which was not found in the 556 * dependancies. We maintain a simple common symbol table in 557 * the file object. 558 */ 559 struct common_symbol* cp; 560 561 for (cp = STAILQ_FIRST(&file->common); cp; 562 cp = STAILQ_NEXT(cp, link)) 563 if (!strcmp(cp->name, name)) { 564 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 565 *raddr = cp->address; 566 return 0; 567 } 568 569 /* 570 * Round the symbol size up to align. 571 */ 572 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 573 cp = malloc(sizeof(struct common_symbol) 574 + common_size 575 + strlen(name) + 1, 576 M_LINKER, M_WAITOK); 577 if (!cp) { 578 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 579 return ENOMEM; 580 } 581 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); 582 583 cp->address = (caddr_t) (cp + 1); 584 cp->name = cp->address + common_size; 585 strcpy(cp->name, name); 586 bzero(cp->address, common_size); 587 STAILQ_INSERT_TAIL(&file->common, cp, link); 588 589 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 590 *raddr = cp->address; 591 return 0; 592 } 593 594 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 595 return ENOENT; 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 608 int 609 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 610 { 611 linker_file_t lf; 612 613 for (lf = TAILQ_FIRST(&linker_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 620 int 621 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 622 { 623 linker_file_t lf; 624 u_long off = (uintptr_t)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(&linker_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 652 int 653 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 654 { 655 linker_file_t lf; 656 657 for (lf = TAILQ_FIRST(&linker_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 670 int 671 kldload(struct kldload_args *uap) 672 { 673 struct thread *td = curthread; 674 char* filename = NULL, *modulename; 675 linker_file_t lf; 676 int error = 0; 677 678 uap->sysmsg_result = -1; 679 680 if (securelevel > 0) /* redundant, but that's OK */ 681 return EPERM; 682 683 if ((error = suser(td)) != 0) 684 return error; 685 686 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 687 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 688 goto out; 689 690 /* Can't load more than one module with the same name */ 691 modulename = rindex(filename, '/'); 692 if (modulename == NULL) 693 modulename = filename; 694 else 695 modulename++; 696 if (linker_find_file_by_name(modulename)) { 697 error = EEXIST; 698 goto out; 699 } 700 701 if ((error = linker_load_file(filename, &lf)) != 0) 702 goto out; 703 704 lf->userrefs++; 705 uap->sysmsg_result = lf->id; 706 707 out: 708 if (filename) 709 free(filename, M_TEMP); 710 return error; 711 } 712 713 int 714 kldunload(struct kldunload_args *uap) 715 { 716 struct thread *td = curthread; 717 linker_file_t lf; 718 int error = 0; 719 720 if (securelevel > 0) /* redundant, but that's OK */ 721 return EPERM; 722 723 if ((error = suser(td)) != 0) 724 return error; 725 726 lf = linker_find_file_by_id(SCARG(uap, fileid)); 727 if (lf) { 728 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 729 if (lf->userrefs == 0) { 730 printf("linkerunload: attempt to unload file that was loaded by the kernel\n"); 731 error = EBUSY; 732 goto out; 733 } 734 lf->userrefs--; 735 error = linker_file_unload(lf); 736 if (error) 737 lf->userrefs++; 738 } else 739 error = ENOENT; 740 741 out: 742 return error; 743 } 744 745 int 746 kldfind(struct kldfind_args *uap) 747 { 748 char *filename = NULL, *modulename; 749 linker_file_t lf; 750 int error = 0; 751 752 uap->sysmsg_result = -1; 753 754 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 755 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 756 goto out; 757 758 modulename = rindex(filename, '/'); 759 if (modulename == NULL) 760 modulename = filename; 761 762 lf = linker_find_file_by_name(modulename); 763 if (lf) 764 uap->sysmsg_result = lf->id; 765 else 766 error = ENOENT; 767 768 out: 769 if (filename) 770 free(filename, M_TEMP); 771 return error; 772 } 773 774 int 775 kldnext(struct kldnext_args *uap) 776 { 777 linker_file_t lf; 778 int error = 0; 779 780 if (SCARG(uap, fileid) == 0) { 781 if (TAILQ_FIRST(&linker_files)) 782 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id; 783 else 784 uap->sysmsg_result = 0; 785 return 0; 786 } 787 788 lf = linker_find_file_by_id(SCARG(uap, fileid)); 789 if (lf) { 790 if (TAILQ_NEXT(lf, link)) 791 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id; 792 else 793 uap->sysmsg_result = 0; 794 } else 795 error = ENOENT; 796 797 return error; 798 } 799 800 int 801 kldstat(struct kldstat_args *uap) 802 { 803 linker_file_t lf; 804 int error = 0; 805 int version; 806 struct kld_file_stat* stat; 807 int namelen; 808 809 lf = linker_find_file_by_id(SCARG(uap, fileid)); 810 if (!lf) { 811 error = ENOENT; 812 goto out; 813 } 814 815 stat = SCARG(uap, stat); 816 817 /* 818 * Check the version of the user's structure. 819 */ 820 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 821 goto out; 822 if (version != sizeof(struct kld_file_stat)) { 823 error = EINVAL; 824 goto out; 825 } 826 827 namelen = strlen(lf->filename) + 1; 828 if (namelen > MAXPATHLEN) 829 namelen = MAXPATHLEN; 830 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 831 goto out; 832 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 833 goto out; 834 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 835 goto out; 836 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 837 goto out; 838 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 839 goto out; 840 841 uap->sysmsg_result = 0; 842 843 out: 844 return error; 845 } 846 847 int 848 kldfirstmod(struct kldfirstmod_args *uap) 849 { 850 linker_file_t lf; 851 int error = 0; 852 853 lf = linker_find_file_by_id(SCARG(uap, fileid)); 854 if (lf) { 855 if (TAILQ_FIRST(&lf->modules)) 856 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules)); 857 else 858 uap->sysmsg_result = 0; 859 } else 860 error = ENOENT; 861 862 return error; 863 } 864 865 int 866 kldsym(struct kldsym_args *uap) 867 { 868 char *symstr = NULL; 869 c_linker_sym_t sym; 870 linker_symval_t symval; 871 linker_file_t lf; 872 struct kld_sym_lookup lookup; 873 int error = 0; 874 875 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 876 goto out; 877 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 878 error = EINVAL; 879 goto out; 880 } 881 882 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 883 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 884 goto out; 885 886 if (SCARG(uap, fileid) != 0) { 887 lf = linker_find_file_by_id(SCARG(uap, fileid)); 888 if (lf == NULL) { 889 error = ENOENT; 890 goto out; 891 } 892 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 893 lf->ops->symbol_values(lf, sym, &symval) == 0) { 894 lookup.symvalue = (uintptr_t)symval.value; 895 lookup.symsize = symval.size; 896 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 897 } else 898 error = ENOENT; 899 } else { 900 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 901 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 902 lf->ops->symbol_values(lf, sym, &symval) == 0) { 903 lookup.symvalue = (uintptr_t)symval.value; 904 lookup.symsize = symval.size; 905 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 906 break; 907 } 908 } 909 if (!lf) 910 error = ENOENT; 911 } 912 out: 913 if (symstr) 914 free(symstr, M_TEMP); 915 return error; 916 } 917 918 /* 919 * Look for module metadata in the static kernel 920 */ 921 struct mod_metadata * 922 find_mod_metadata(const char *modname) 923 { 924 int len; 925 struct mod_metadata **mdp; 926 struct mod_metadata *mdt; 927 928 /* 929 * Strip path prefixes and any dot extension. MDT_MODULE names 930 * are just the module name without a path or ".ko". 931 */ 932 for (len = strlen(modname) - 1; len >= 0; --len) { 933 if (modname[len] == '/') 934 break; 935 } 936 modname += len + 1; 937 for (len = 0; modname[len] && modname[len] != '.'; ++len) 938 ; 939 940 /* 941 * Look for the module declaration 942 */ 943 SET_FOREACH(mdp, modmetadata_set) { 944 mdt = *mdp; 945 if (mdt->md_type != MDT_MODULE) 946 continue; 947 if (strlen(mdt->md_cval) == len && 948 strncmp(mdt->md_cval, modname, len) == 0 949 ) { 950 return(mdt); 951 } 952 } 953 return(NULL); 954 } 955 956 /* 957 * Preloaded module support 958 */ 959 static void 960 linker_preload(void* arg) 961 { 962 caddr_t modptr; 963 char *modname; 964 char *modtype; 965 linker_file_t lf; 966 linker_class_t lc; 967 int error; 968 struct sysinit **sipp; 969 const moduledata_t *moddata; 970 struct sysinit **si_start, **si_stop; 971 972 modptr = NULL; 973 while ((modptr = preload_search_next_name(modptr)) != NULL) { 974 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 975 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 976 if (modname == NULL) { 977 printf("Preloaded module at %p does not have a name!\n", modptr); 978 continue; 979 } 980 if (modtype == NULL) { 981 printf("Preloaded module at %p does not have a type!\n", modptr); 982 continue; 983 } 984 985 /* 986 * This is a hack at the moment, but what's in FreeBSD-5 is even 987 * worse so I'd rather the hack. 988 */ 989 printf("Preloaded %s \"%s\" at %p", modtype, modname, modptr); 990 if (find_mod_metadata(modname)) { 991 printf(" (ignored, already in static kernel)\n"); 992 continue; 993 } 994 printf(".\n"); 995 996 lf = linker_find_file_by_name(modname); 997 if (lf) { 998 lf->userrefs++; 999 continue; 1000 } 1001 lf = NULL; 1002 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 1003 error = lc->ops->load_file(modname, &lf); 1004 if (error) { 1005 lf = NULL; 1006 break; 1007 } 1008 } 1009 if (lf) { 1010 lf->userrefs++; 1011 1012 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) { 1013 /* HACK ALERT! 1014 * This is to set the sysinit moduledata so that the module 1015 * can attach itself to the correct containing file. 1016 * The sysinit could be run at *any* time. 1017 */ 1018 for (sipp = si_start; sipp < si_stop; sipp++) { 1019 if ((*sipp)->func == module_register_init) { 1020 moddata = (*sipp)->udata; 1021 error = module_register(moddata, lf); 1022 if (error) 1023 printf("Preloaded %s \"%s\" failed to register: %d\n", 1024 modtype, modname, error); 1025 } 1026 } 1027 sysinit_add(si_start, si_stop); 1028 } 1029 linker_file_register_sysctls(lf); 1030 } 1031 } 1032 } 1033 1034 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1035 1036 /* 1037 * Search for a not-loaded module by name. 1038 * 1039 * Modules may be found in the following locations: 1040 * 1041 * - preloaded (result is just the module name) 1042 * - on disk (result is full path to module) 1043 * 1044 * If the module name is qualified in any way (contains path, etc.) 1045 * the we simply return a copy of it. 1046 * 1047 * The search path can be manipulated via sysctl. Note that we use the ';' 1048 * character as a separator to be consistent with the bootloader. 1049 */ 1050 1051 static char linker_path[MAXPATHLEN] = "/;/boot/;/modules/"; 1052 1053 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1054 sizeof(linker_path), "module load search path"); 1055 1056 static char * 1057 linker_strdup(const char *str) 1058 { 1059 char *result; 1060 1061 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 1062 strcpy(result, str); 1063 return(result); 1064 } 1065 1066 char * 1067 linker_search_path(const char *name) 1068 { 1069 struct nameidata nd; 1070 struct thread *td = curthread; 1071 char *cp, *ep, *result; 1072 int error; 1073 enum vtype type; 1074 1075 /* qualified at all? */ 1076 if (index(name, '/')) 1077 return(linker_strdup(name)); 1078 1079 /* traverse the linker path */ 1080 cp = linker_path; 1081 for (;;) { 1082 1083 /* find the end of this component */ 1084 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1085 ; 1086 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK); 1087 if (result == NULL) /* actually ENOMEM */ 1088 return(NULL); 1089 1090 strncpy(result, cp, ep - cp); 1091 strcpy(result + (ep - cp), name); 1092 1093 /* 1094 * Attempt to open the file, and return the path if we succeed and it's 1095 * a regular file. 1096 */ 1097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, result, td); 1098 error = vn_open(&nd, FREAD, 0); 1099 if (error == 0) { 1100 NDFREE(&nd, NDF_ONLY_PNBUF); 1101 type = nd.ni_vp->v_type; 1102 VOP_UNLOCK(nd.ni_vp, 0, td); 1103 vn_close(nd.ni_vp, FREAD, td); 1104 if (type == VREG) 1105 return(result); 1106 } 1107 free(result, M_LINKER); 1108 1109 if (*ep == 0) 1110 break; 1111 cp = ep + 1; 1112 } 1113 return(NULL); 1114 } 1115