1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #include <sys/param.h> 26 #include <sys/modctl.h> 27 #include <sys/modhash.h> 28 #include <sys/open.h> 29 #include <sys/conf.h> 30 #include <sys/errno.h> 31 #include <sys/sysmacros.h> 32 #include <sys/kmem.h> 33 #include <sys/cmn_err.h> 34 #include <sys/stat.h> 35 #include <sys/mode.h> 36 #include <sys/pathname.h> 37 #include <sys/vnode.h> 38 #include <sys/ddi_impldefs.h> 39 #include <sys/ddi_implfuncs.h> 40 #include <sys/esunddi.h> 41 #include <sys/sunddi.h> 42 #include <sys/sunndi.h> 43 #include <sys/systeminfo.h> 44 #include <sys/hwconf.h> 45 #include <sys/file.h> 46 #include <sys/varargs.h> 47 #include <sys/thread.h> 48 #include <sys/cred.h> 49 #include <sys/autoconf.h> 50 #include <sys/kobj.h> 51 #include <sys/consdev.h> 52 #include <sys/systm.h> 53 #include <sys/debug.h> 54 #include <sys/atomic.h> 55 56 extern struct dev_ops nodev_ops; 57 extern struct dev_ops mod_nodev_ops; 58 59 struct mod_noload { 60 struct mod_noload *mn_next; 61 char *mn_name; 62 }; 63 64 /* 65 * Function prototypes 66 */ 67 static int init_stubs(struct modctl *, struct mod_modinfo *); 68 static int nm_hash(char *); 69 static void make_syscallname(char *, int); 70 static void hwc_hash_init(); 71 static void hwc_hash(struct hwc_spec *, major_t); 72 static void hwc_unhash(struct hwc_spec *); 73 74 int 75 major_valid(major_t major) 76 { 77 return (major != DDI_MAJOR_T_NONE && 78 (major >= 0 && major < devcnt)); 79 } 80 81 int 82 driver_installed(major_t major) 83 { 84 return (major_valid(major) && devnamesp[major].dn_name != NULL); 85 } 86 87 int 88 driver_active(major_t major) 89 { 90 return (driver_installed(major) && !(devnamesp[major].dn_flags & 91 (DN_DRIVER_REMOVED|DN_DRIVER_INACTIVE))); 92 } 93 94 struct dev_ops * 95 mod_hold_dev_by_major(major_t major) 96 { 97 struct dev_ops **devopspp, *ops; 98 int loaded; 99 char *drvname; 100 101 if (!driver_active(major)) 102 return (NULL); 103 104 LOCK_DEV_OPS(&(devnamesp[major].dn_lock)); 105 devopspp = &devopsp[major]; 106 loaded = 1; 107 while (loaded && !CB_DRV_INSTALLED(*devopspp)) { 108 UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock)); 109 drvname = mod_major_to_name(major); 110 if (drvname == NULL) 111 return (NULL); 112 loaded = (modload("drv", drvname) != -1); 113 LOCK_DEV_OPS(&(devnamesp[major].dn_lock)); 114 } 115 if (loaded) { 116 INCR_DEV_OPS_REF(*devopspp); 117 ops = *devopspp; 118 } else { 119 ops = NULL; 120 } 121 UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock)); 122 return (ops); 123 } 124 125 #ifdef DEBUG_RELE 126 static int mod_rele_pause = DEBUG_RELE; 127 #endif /* DEBUG_RELE */ 128 129 void 130 mod_rele_dev_by_major(major_t major) 131 { 132 struct dev_ops *ops; 133 struct devnames *dnp; 134 135 if (!driver_active(major)) 136 return; 137 138 dnp = &devnamesp[major]; 139 LOCK_DEV_OPS(&dnp->dn_lock); 140 ops = devopsp[major]; 141 ASSERT(CB_DRV_INSTALLED(ops)); 142 143 #ifdef DEBUG_RELE 144 if (!DEV_OPS_HELD(ops)) { 145 char *s; 146 static char *msg = "mod_rele_dev_by_major: unheld driver!"; 147 148 printf("mod_rele_dev_by_major: Major dev <%u>, name <%s>\n", 149 (uint_t)major, 150 (s = mod_major_to_name(major)) ? s : "unknown"); 151 if (mod_rele_pause) 152 debug_enter(msg); 153 else 154 printf("%s\n", msg); 155 UNLOCK_DEV_OPS(&dnp->dn_lock); 156 return; /* XXX: Note changed behavior */ 157 } 158 159 #endif /* DEBUG_RELE */ 160 161 if (!DEV_OPS_HELD(ops)) { 162 cmn_err(CE_PANIC, 163 "mod_rele_dev_by_major: Unheld driver: major number <%u>", 164 (uint_t)major); 165 } 166 DECR_DEV_OPS_REF(ops); 167 UNLOCK_DEV_OPS(&dnp->dn_lock); 168 } 169 170 struct dev_ops * 171 mod_hold_dev_by_devi(dev_info_t *devi) 172 { 173 major_t major; 174 char *name; 175 176 name = ddi_get_name(devi); 177 if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE) 178 return (NULL); 179 return (mod_hold_dev_by_major(major)); 180 } 181 182 void 183 mod_rele_dev_by_devi(dev_info_t *devi) 184 { 185 major_t major; 186 char *name; 187 188 name = ddi_get_name(devi); 189 if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE) 190 return; 191 mod_rele_dev_by_major(major); 192 } 193 194 int 195 nomod_zero() 196 { 197 return (0); 198 } 199 200 int 201 nomod_minus_one() 202 { 203 return (-1); 204 } 205 206 int 207 nomod_einval() 208 { 209 return (EINVAL); 210 } 211 212 void 213 nomod_void() 214 { 215 /* nothing */ 216 } 217 218 /* 219 * Install all the stubs for a module. 220 * Return zero if there were no errors or an errno value. 221 */ 222 int 223 install_stubs_by_name(struct modctl *modp, char *name) 224 { 225 char *p; 226 char *filenamep; 227 char namebuf[MODMAXNAMELEN + 12]; 228 struct mod_modinfo *mp; 229 230 p = name; 231 filenamep = name; 232 233 while (*p) 234 if (*p++ == '/') 235 filenamep = p; 236 237 /* 238 * Concatenate "name" with "_modname" then look up this symbol 239 * in the kernel. If not found, we're done. 240 * If found, then find the "mod" info structure and call init_stubs(). 241 */ 242 p = namebuf; 243 244 while (*filenamep && *filenamep != '.') 245 *p++ = *filenamep++; 246 247 (void) strcpy(p, "_modinfo"); 248 249 if ((mp = (struct mod_modinfo *)modgetsymvalue(namebuf, 1)) != 0) 250 return (init_stubs(modp, mp)); 251 else 252 return (0); 253 } 254 255 static int 256 init_stubs(struct modctl *modp, struct mod_modinfo *mp) 257 { 258 struct mod_stub_info *sp; 259 int i; 260 ulong_t offset; 261 uintptr_t funcadr; 262 char *funcname; 263 264 modp->mod_modinfo = mp; 265 266 /* 267 * Fill in all stubs for this module. We can't be lazy, since 268 * some calls could come in from interrupt level, and we 269 * can't modlookup then (symbols may be paged out). 270 */ 271 sp = mp->modm_stubs; 272 for (i = 0; sp->mods_func_adr; i++, sp++) { 273 funcname = modgetsymname(sp->mods_stub_adr, &offset); 274 if (funcname == NULL) { 275 printf("init_stubs: couldn't find symbol " 276 "in module %s\n", mp->modm_module_name); 277 return (EFAULT); 278 } 279 funcadr = kobj_lookup(modp->mod_mp, funcname); 280 281 if (kobj_addrcheck(modp->mod_mp, (caddr_t)funcadr)) { 282 printf("%s:%s() not defined properly\n", 283 mp->modm_module_name, funcname); 284 return (EFAULT); 285 } 286 sp->mods_func_adr = funcadr; 287 } 288 mp->mp = modp; 289 return (0); 290 } 291 292 /* 293 * modp->mod_modinfo has to be checked in these functions before 294 * mod_stub_info is accessed because it's not guranteed that all 295 * modules define mod_stub_info structures. 296 */ 297 void 298 install_stubs(struct modctl *modp) 299 { 300 struct mod_stub_info *stub; 301 302 if (modp->mod_modinfo) { 303 membar_producer(); 304 for (stub = modp->mod_modinfo->modm_stubs; 305 stub->mods_func_adr; stub++) { 306 stub->mods_flag |= MODS_INSTALLED; 307 } 308 membar_producer(); 309 } 310 } 311 312 void 313 uninstall_stubs(struct modctl *modp) 314 { 315 struct mod_stub_info *stub; 316 317 if (modp->mod_modinfo) { 318 membar_producer(); 319 for (stub = modp->mod_modinfo->modm_stubs; 320 stub->mods_func_adr; stub++) { 321 stub->mods_flag &= ~MODS_INSTALLED; 322 } 323 membar_producer(); 324 } 325 } 326 327 void 328 reset_stubs(struct modctl *modp) 329 { 330 struct mod_stub_info *stub; 331 332 if (modp->mod_modinfo) { 333 for (stub = modp->mod_modinfo->modm_stubs; 334 stub->mods_func_adr; stub++) { 335 if (stub->mods_flag & (MODS_WEAK | MODS_NOUNLOAD)) 336 stub->mods_func_adr = 337 (uintptr_t)stub->mods_errfcn; 338 else 339 stub->mods_func_adr = 340 (uintptr_t)mod_hold_stub; 341 } 342 modp->mod_modinfo->mp = NULL; 343 } 344 } 345 346 struct modctl * 347 mod_getctl(struct modlinkage *modlp) 348 { 349 struct modctl *modp; 350 351 mutex_enter(&mod_lock); 352 modp = &modules; 353 do { 354 if (modp->mod_linkage == modlp) { 355 mutex_exit(&mod_lock); 356 return (modp); 357 } 358 } while ((modp = modp->mod_next) != &modules); 359 mutex_exit(&mod_lock); 360 return (NULL); 361 } 362 363 364 /* 365 * Attach driver.conf info to devnames for a driver 366 */ 367 struct par_list * 368 impl_make_parlist(major_t major) 369 { 370 int err; 371 struct par_list *pl = NULL, *tmp; 372 ddi_prop_t *props = NULL; 373 char *confname, *drvname; 374 struct devnames *dnp; 375 376 dnp = &devnamesp[major]; 377 378 ASSERT(mutex_owned(&dnp->dn_lock)); 379 380 /* 381 * If .conf file already parsed or driver removed, just return. 382 * May return NULL. 383 */ 384 if (dnp->dn_flags & (DN_CONF_PARSED | DN_DRIVER_REMOVED)) 385 return (dnp->dn_pl); 386 387 drvname = mod_major_to_name(major); 388 if (drvname == NULL) 389 return (NULL); 390 391 confname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 392 (void) snprintf(confname, MAXNAMELEN, "drv/%s.conf", drvname); 393 err = hwc_parse(confname, &pl, &props); 394 kmem_free(confname, MAXNAMELEN); 395 if (err) /* file doesn't exist */ 396 return (NULL); 397 398 /* 399 * If there are global properties, reference it from dnp. 400 */ 401 if (props) 402 dnp->dn_global_prop_ptr = i_ddi_prop_list_create(props); 403 404 /* 405 * Hash specs to be looked up by nexus drivers 406 */ 407 tmp = pl; 408 while (tmp) { 409 (void) hwc_hash(tmp->par_specs, major); 410 tmp = tmp->par_next; 411 } 412 413 if (!i_ddi_io_initialized()) { 414 if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_FORCEATTACH, 415 DDI_PROP_TYPE_INT, &props)) 416 dnp->dn_flags |= DN_FORCE_ATTACH; 417 if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_OPEN_RETURNS_EINTR, 418 DDI_PROP_TYPE_INT, &props)) 419 dnp->dn_flags |= DN_OPEN_RETURNS_EINTR; 420 if (i_ddi_prop_search(DDI_DEV_T_ANY, "scsi-size-clean", 421 DDI_PROP_TYPE_INT, &props)) 422 dnp->dn_flags |= DN_SCSI_SIZE_CLEAN; 423 } 424 425 if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_VHCI_CLASS, 426 DDI_PROP_TYPE_STRING, &props)) 427 dnp->dn_flags |= DN_PHCI_DRIVER; 428 429 dnp->dn_flags |= DN_CONF_PARSED; 430 dnp->dn_pl = pl; 431 return (pl); 432 } 433 434 /* 435 * Destroy driver.conf info in devnames array for a driver 436 */ 437 int 438 impl_free_parlist(major_t major) 439 { 440 struct par_list *pl; 441 struct devnames *dnp = &devnamesp[major]; 442 443 /* 444 * Unref driver global property list. Don't destroy it 445 * because some instances may still be referencing it. 446 * The property list will be freed when the last ref 447 * goes away. 448 */ 449 if (dnp->dn_global_prop_ptr) { 450 i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp); 451 dnp->dn_global_prop_ptr = NULL; 452 } 453 454 /* 455 * remove specs from hash table 456 */ 457 for (pl = dnp->dn_pl; pl; pl = pl->par_next) 458 hwc_unhash(pl->par_specs); 459 460 impl_delete_par_list(dnp->dn_pl); 461 dnp->dn_pl = NULL; 462 dnp->dn_flags &= ~DN_CONF_PARSED; 463 return (0); 464 } 465 466 struct bind *mb_hashtab[MOD_BIND_HASHSIZE]; 467 struct bind *sb_hashtab[MOD_BIND_HASHSIZE]; 468 469 static int 470 nm_hash(char *name) 471 { 472 char c; 473 int hash = 0; 474 475 for (c = *name++; c; c = *name++) 476 hash ^= c; 477 478 return (hash & MOD_BIND_HASHMASK); 479 } 480 481 void 482 clear_binding_hash(struct bind **bhash) 483 { 484 int i; 485 struct bind *bp, *bp1; 486 487 for (i = 0; i < MOD_BIND_HASHSIZE; i++) { 488 bp = bhash[i]; 489 while (bp != NULL) { 490 kmem_free(bp->b_name, strlen(bp->b_name) + 1); 491 if (bp->b_bind_name) { 492 kmem_free(bp->b_bind_name, 493 strlen(bp->b_bind_name) + 1); 494 } 495 bp1 = bp; 496 bp = bp->b_next; 497 kmem_free(bp1, sizeof (struct bind)); 498 } 499 bhash[i] = NULL; 500 } 501 } 502 503 /* Find an mbind by name match (caller can ask for deleted match) */ 504 static struct bind * 505 find_mbind(char *name, struct bind **hashtab, int deleted) 506 { 507 struct bind *mb; 508 509 for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) { 510 if (deleted && (mb->b_num >= 0)) 511 continue; /* skip active */ 512 if (!deleted && (mb->b_num < 0)) 513 continue; /* skip deleted */ 514 515 /* return if name matches */ 516 if (strcmp(name, mb->b_name) == 0) { 517 break; 518 } 519 } 520 return (mb); 521 } 522 523 /* 524 * Create an entry for the given (name, major, bind_name) tuple in the 525 * hash table supplied. Reject the attempt to do so if 'name' is already 526 * in the hash table. 527 * 528 * Does not provide synchronization, so use only during boot or with 529 * externally provided locking. 530 */ 531 int 532 make_mbind(char *name, int num, char *bind_name, struct bind **hashtab) 533 { 534 struct bind *mb; 535 struct bind **pmb; 536 537 ASSERT(hashtab != NULL); 538 ASSERT(num >= 0); 539 540 /* Fail if the key being added is already established */ 541 if (find_mbind(name, hashtab, 0) != NULL) 542 return (-1); 543 544 /* Allocate new mbind */ 545 mb = kmem_zalloc(sizeof (struct bind), KM_SLEEP); 546 mb->b_name = i_ddi_strdup(name, KM_SLEEP); 547 mb->b_num = num; 548 if (bind_name != NULL) 549 mb->b_bind_name = i_ddi_strdup(bind_name, KM_SLEEP); 550 551 /* Insert at head of hash */ 552 pmb = &hashtab[nm_hash(name)]; 553 mb->b_next = *pmb; 554 *pmb = mb; 555 return (0); 556 } 557 558 /* 559 * Delete a binding from a binding-hash. Since there is no locking we 560 * delete an mbind by making its b_num negative. We also support find_mbind 561 * of deleted entries, so we still need deleted items on the list. 562 */ 563 void 564 delete_mbind(char *name, struct bind **hashtab) 565 { 566 struct bind *mb; 567 568 for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) { 569 if ((mb->b_num >= 0) && (strcmp(name, mb->b_name) == 0)) { 570 /* delete by making b_num negative */ 571 if (moddebug & MODDEBUG_BINDING) { 572 cmn_err(CE_CONT, "mbind: %s %d deleted\n", 573 name, mb->b_num); 574 } 575 mb->b_num = -mb->b_num; 576 break; 577 } 578 } 579 } 580 581 /* 582 * Delete all items in an mbind associated with specified num. 583 * An example would be rem_drv deleting all aliases associated with a 584 * driver major number. 585 */ 586 void 587 purge_mbind(int num, struct bind **hashtab) 588 { 589 int i; 590 struct bind *mb; 591 592 /* search all hash lists for items that associated with 'num' */ 593 for (i = 0; i < MOD_BIND_HASHSIZE; i++) { 594 for (mb = hashtab[i]; mb; mb = mb->b_next) { 595 if (mb->b_num == num) { 596 if (moddebug & MODDEBUG_BINDING) 597 cmn_err(CE_CONT, 598 "mbind: %s %d purged\n", 599 mb->b_name, num); 600 /* purge by changing the sign */ 601 mb->b_num = -num; 602 } 603 } 604 } 605 } 606 607 major_t 608 mod_name_to_major(char *name) 609 { 610 struct bind *mbind; 611 major_t maj; 612 613 /* Search for non-deleted match. */ 614 if ((mbind = find_mbind(name, mb_hashtab, 0)) != NULL) { 615 if (moddebug & MODDEBUG_BINDING) { 616 if (find_mbind(name, mb_hashtab, 1)) 617 cmn_err(CE_CONT, 618 "'%s' has deleted match too\n", name); 619 } 620 return ((major_t)mbind->b_num); 621 } 622 623 /* 624 * Search for deleted match: We may find that we have dependencies 625 * on drivers that have been deleted (but the old driver may still 626 * be bound to a node). These callers should be converted to use 627 * ddi_driver_major(i.e. devi_major). 628 */ 629 if (moddebug & MODDEBUG_BINDING) { 630 if ((mbind = find_mbind(name, mb_hashtab, 1)) != NULL) { 631 maj = (major_t)(-(mbind->b_num)); 632 cmn_err(CE_CONT, "Reference to deleted alias '%s' %d\n", 633 name, maj); 634 } 635 } 636 637 return (DDI_MAJOR_T_NONE); 638 } 639 640 char * 641 mod_major_to_name(major_t major) 642 { 643 if (!driver_installed(major)) 644 return (NULL); 645 return ((&devnamesp[major])->dn_name); 646 } 647 648 /* 649 * Set up the devnames array. Error check for duplicate entries. 650 */ 651 void 652 init_devnamesp(int size) 653 { 654 int hshndx; 655 struct bind *bp; 656 static char dupwarn[] = 657 "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" " 658 "in /etc/name_to_major."; 659 static char badmaj[] = "The major number %u is invalid."; 660 661 ASSERT(size <= L_MAXMAJ32 && size > 0); 662 663 /* 664 * Allocate the devnames array. All mutexes and cv's will be 665 * automagically initialized. 666 */ 667 devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP); 668 669 /* 670 * Stick the contents of mb_hashtab into the devnames array. Warn if 671 * two hash entries correspond to the same major number, or if a 672 * major number is out of range. 673 */ 674 for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) { 675 for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) { 676 if (make_devname(bp->b_name, 677 (major_t)bp->b_num, 0) != 0) { 678 /* 679 * If there is not an entry at b_num already, 680 * then this must be a bad major number. 681 */ 682 char *nm = mod_major_to_name(bp->b_num); 683 if (nm == NULL) { 684 cmn_err(CE_WARN, badmaj, 685 (uint_t)bp->b_num); 686 } else { 687 cmn_err(CE_WARN, dupwarn, bp->b_name, 688 bp->b_num, nm, bp->b_num); 689 } 690 } 691 } 692 } 693 694 /* Initialize hash table for hwc_spec's */ 695 hwc_hash_init(); 696 } 697 698 int 699 make_devname(char *name, major_t major, int dn_flags) 700 { 701 struct devnames *dnp; 702 char *copy; 703 704 /* 705 * Until on-disk support for major nums > 14 bits arrives, fail 706 * any major numbers that are too big. 707 */ 708 if (major > L_MAXMAJ32) 709 return (EINVAL); 710 711 dnp = &devnamesp[major]; 712 LOCK_DEV_OPS(&dnp->dn_lock); 713 if (dnp->dn_name) { 714 if (strcmp(dnp->dn_name, name) != 0) { 715 /* Another driver already here */ 716 UNLOCK_DEV_OPS(&dnp->dn_lock); 717 return (EINVAL); 718 } 719 /* Adding back a removed driver */ 720 dnp->dn_flags &= ~DN_DRIVER_REMOVED; 721 dnp->dn_flags |= dn_flags; 722 UNLOCK_DEV_OPS(&dnp->dn_lock); 723 return (0); 724 } 725 726 /* 727 * Check if flag is taken by getudev() 728 */ 729 if (dnp->dn_flags & DN_TAKEN_GETUDEV) { 730 UNLOCK_DEV_OPS(&dnp->dn_lock); 731 return (EINVAL); 732 } 733 734 copy = kmem_alloc(strlen(name) + 1, KM_SLEEP); 735 (void) strcpy(copy, name); 736 737 /* Make sure string is copied before setting dn_name */ 738 membar_producer(); 739 dnp->dn_name = copy; 740 dnp->dn_flags = dn_flags; 741 UNLOCK_DEV_OPS(&dnp->dn_lock); 742 return (0); 743 } 744 745 /* 746 * Set up the syscallnames array. 747 */ 748 void 749 init_syscallnames(int size) 750 { 751 int hshndx; 752 struct bind *bp; 753 754 syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP); 755 756 for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) { 757 for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) { 758 if (bp->b_num < 0 || bp->b_num >= size) { 759 cmn_err(CE_WARN, 760 "!Couldn't add system call \"%s %d\". " 761 "Value out of range (0..%d) in " 762 "/etc/name_to_sysnum.", 763 bp->b_name, bp->b_num, size - 1); 764 continue; 765 } 766 make_syscallname(bp->b_name, bp->b_num); 767 } 768 } 769 } 770 771 static void 772 make_syscallname(char *name, int sysno) 773 { 774 char **cp = &syscallnames[sysno]; 775 776 if (*cp != NULL) { 777 cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". " 778 "It conflicts with \"%s %d\" in /etc/name_to_sysnum.", 779 name, sysno, *cp, sysno); 780 return; 781 } 782 *cp = kmem_alloc(strlen(name) + 1, KM_SLEEP); 783 (void) strcpy(*cp, name); 784 } 785 786 /* 787 * Given a system call name, get its number. 788 */ 789 int 790 mod_getsysnum(char *name) 791 { 792 struct bind *mbind; 793 794 if ((mbind = find_mbind(name, sb_hashtab, 0)) != NULL) 795 return (mbind->b_num); 796 797 return (-1); 798 } 799 800 /* 801 * Given a system call number, get the system call name. 802 */ 803 char * 804 mod_getsysname(int sysnum) 805 { 806 return (syscallnames[sysnum]); 807 } 808 809 /* 810 * Find the name of the module containing the specified pc. 811 * Returns the name on success, "<unknown>" on failure. 812 * No mod_lock locking is required because things are never deleted from 813 * the &modules list. 814 */ 815 char * 816 mod_containing_pc(caddr_t pc) 817 { 818 struct modctl *mcp = &modules; 819 820 do { 821 if (mcp->mod_mp != NULL && 822 (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size) 823 return (mcp->mod_modname); 824 } while ((mcp = mcp->mod_next) != &modules); 825 return ("<unknown>"); 826 } 827 828 /* 829 * Hash tables for hwc_spec 830 * 831 * The purpose of these hash tables are to allow the framework to discover 832 * all possible .conf children for a given nexus. There are two hash tables. 833 * One is hashed based on parent name, the on the class name. Each 834 * driver.conf file translates to a list of hwc_spec's. Adding and 835 * removing the entire list is an atomic operation, protected by 836 * the hwc_hash_lock. 837 * 838 * What we get from all the hashing is the function hwc_get_child_spec(). 839 */ 840 #define HWC_SPEC_HASHSIZE (1 << 6) /* 64 */ 841 842 static mod_hash_t *hwc_par_hash; /* hash by parent name */ 843 static mod_hash_t *hwc_class_hash; /* hash by class name */ 844 static kmutex_t hwc_hash_lock; /* lock protecting hwc hashes */ 845 846 /* 847 * Initialize hash tables for parent and class specs 848 */ 849 static void 850 hwc_hash_init() 851 { 852 hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash", 853 HWC_SPEC_HASHSIZE, mod_hash_null_valdtor); 854 hwc_class_hash = mod_hash_create_strhash("hwc class spec hash", 855 HWC_SPEC_HASHSIZE, mod_hash_null_valdtor); 856 } 857 858 /* 859 * Insert a spec into hash table. hwc_hash_lock must be held 860 */ 861 static void 862 hwc_hash_insert(struct hwc_spec *spec, char *name, mod_hash_t *hash) 863 { 864 mod_hash_key_t key; 865 struct hwc_spec *entry = NULL; 866 867 ASSERT(name != NULL); 868 869 if (mod_hash_find(hash, (mod_hash_key_t)name, 870 (mod_hash_val_t)&entry) != 0) { 871 /* Name doesn't exist, insert a new key */ 872 key = kmem_alloc(strlen(name) + 1, KM_SLEEP); 873 (void) strcpy((char *)key, name); 874 if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) { 875 kmem_free(key, strlen(name) + 1); 876 cmn_err(CE_WARN, "hwc hash state inconsistent"); 877 } 878 return; 879 } 880 881 /* 882 * Name is already present, append spec to the list. 883 * This is the case when driver.conf specifies multiple 884 * nodes under a single parent or class. 885 */ 886 while (entry->hwc_hash_next) 887 entry = entry->hwc_hash_next; 888 entry->hwc_hash_next = spec; 889 } 890 891 /* 892 * Remove a spec entry from spec hash table, the spec itself is 893 * destroyed external to this function. 894 */ 895 static void 896 hwc_hash_remove(struct hwc_spec *spec, char *name, mod_hash_t *hash) 897 { 898 char *key; 899 struct hwc_spec *entry; 900 901 ASSERT(name != NULL); 902 903 if (mod_hash_find(hash, (mod_hash_key_t)name, 904 (mod_hash_val_t)&entry) != 0) { 905 return; /* name not found in hash */ 906 } 907 908 /* 909 * If the head is the spec to be removed, either destroy the 910 * entry or replace it with the remaining list. 911 */ 912 if (entry == spec) { 913 if (spec->hwc_hash_next == NULL) { 914 (void) mod_hash_destroy(hash, (mod_hash_key_t)name); 915 return; 916 } 917 key = kmem_alloc(strlen(name) + 1, KM_SLEEP); 918 (void) strcpy(key, name); 919 (void) mod_hash_replace(hash, (mod_hash_key_t)key, 920 (mod_hash_val_t)spec->hwc_hash_next); 921 spec->hwc_hash_next = NULL; 922 return; 923 } 924 925 /* 926 * If the head is not the one, look for the spec in the 927 * hwc_hash_next linkage. 928 */ 929 while (entry->hwc_hash_next && (entry->hwc_hash_next != spec)) 930 entry = entry->hwc_hash_next; 931 932 if (entry->hwc_hash_next) { 933 entry->hwc_hash_next = spec->hwc_hash_next; 934 spec->hwc_hash_next = NULL; 935 } 936 } 937 938 /* 939 * Hash a list of specs based on either parent name or class name 940 */ 941 static void 942 hwc_hash(struct hwc_spec *spec_list, major_t major) 943 { 944 struct hwc_spec *spec = spec_list; 945 946 mutex_enter(&hwc_hash_lock); 947 while (spec) { 948 /* Put driver major here so parent can find it */ 949 spec->hwc_major = major; 950 951 if (spec->hwc_parent_name != NULL) { 952 hwc_hash_insert(spec, spec->hwc_parent_name, 953 hwc_par_hash); 954 } else if (spec->hwc_class_name != NULL) { 955 hwc_hash_insert(spec, spec->hwc_class_name, 956 hwc_class_hash); 957 } else { 958 cmn_err(CE_WARN, 959 "hwc_hash: No class or parent specified"); 960 } 961 spec = spec->hwc_next; 962 } 963 mutex_exit(&hwc_hash_lock); 964 } 965 966 /* 967 * Remove a list of specs from hash tables. Don't destroy the specs yet. 968 */ 969 static void 970 hwc_unhash(struct hwc_spec *spec_list) 971 { 972 struct hwc_spec *spec = spec_list; 973 974 mutex_enter(&hwc_hash_lock); 975 while (spec) { 976 if (spec->hwc_parent_name != NULL) { 977 hwc_hash_remove(spec, spec->hwc_parent_name, 978 hwc_par_hash); 979 } else if (spec->hwc_class_name != NULL) { 980 hwc_hash_remove(spec, spec->hwc_class_name, 981 hwc_class_hash); 982 } else { 983 cmn_err(CE_WARN, 984 "hwc_unhash: No class or parent specified"); 985 } 986 spec = spec->hwc_next; 987 } 988 mutex_exit(&hwc_hash_lock); 989 } 990 991 /* 992 * Make a copy of specs in a hash entry and add to the end of listp. 993 * Called by nexus to locate a list of child specs. 994 * 995 * entry is a list of hwc_spec chained together with hwc_hash_next. 996 * listp points to list chained together with hwc_next. 997 */ 998 static void 999 hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry, 1000 major_t match_major) 1001 { 1002 /* Find the tail of the list */ 1003 while (*listp) 1004 listp = &(*listp)->hwc_next; 1005 1006 while (entry) { 1007 struct hwc_spec *spec; 1008 1009 if ((match_major != DDI_MAJOR_T_NONE) && 1010 (match_major != entry->hwc_major)) { 1011 entry = entry->hwc_hash_next; 1012 continue; 1013 } 1014 1015 /* 1016 * Allocate spec and copy the content of entry. 1017 * No need to copy class/parent name since caller 1018 * already knows the parent dip. 1019 */ 1020 spec = kmem_zalloc(sizeof (*spec), KM_SLEEP); 1021 spec->hwc_devi_name = i_ddi_strdup( 1022 entry->hwc_devi_name, KM_SLEEP); 1023 spec->hwc_major = entry->hwc_major; 1024 spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup( 1025 entry->hwc_devi_sys_prop_ptr, KM_SLEEP); 1026 1027 *listp = spec; 1028 listp = &spec->hwc_next; 1029 entry = entry->hwc_hash_next; 1030 } 1031 } 1032 1033 /* 1034 * Given a dip, find the list of child .conf specs from most specific 1035 * (parent pathname) to least specific (class name). 1036 * 1037 * This function allows top-down loading to be implemented without 1038 * changing the format of driver.conf file. 1039 */ 1040 struct hwc_spec * 1041 hwc_get_child_spec(dev_info_t *dip, major_t match_major) 1042 { 1043 extern char *i_ddi_parname(dev_info_t *, char *); 1044 extern int i_ddi_get_exported_classes(dev_info_t *, char ***); 1045 extern void i_ddi_free_exported_classes(char **, int); 1046 1047 int i, nclass; 1048 char **classes; 1049 struct hwc_spec *list = NULL; 1050 mod_hash_val_t val; 1051 char *parname, *parname_buf; 1052 char *deviname, *deviname_buf; 1053 char *pathname, *pathname_buf; 1054 char *bindname; 1055 char *drvname; 1056 1057 pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP); 1058 deviname_buf = pathname_buf + MAXPATHLEN; 1059 parname_buf = pathname_buf + (2 * MAXPATHLEN); 1060 1061 mutex_enter(&hwc_hash_lock); 1062 1063 /* 1064 * Lookup based on full path. 1065 * In the case of root node, ddi_pathname would return 1066 * null string so just skip calling it. 1067 * As the pathname always begins with /, no simpler 1068 * name can duplicate it. 1069 */ 1070 pathname = (dip == ddi_root_node()) ? "/" : 1071 ddi_pathname(dip, pathname_buf); 1072 ASSERT(pathname != NULL); 1073 ASSERT(*pathname == '/'); 1074 1075 if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) { 1076 hwc_spec_add(&list, (struct hwc_spec *)val, match_major); 1077 } 1078 1079 /* 1080 * Lookup nodename@address. 1081 * Note deviname cannot match pathname. 1082 */ 1083 deviname = ddi_deviname(dip, deviname_buf); 1084 if (*deviname != '\0') { 1085 /* 1086 * Skip leading / returned by ddi_deviname. 1087 */ 1088 ASSERT(*deviname == '/'); 1089 deviname++; 1090 if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash, 1091 (mod_hash_key_t)deviname, &val) == 0)) 1092 hwc_spec_add(&list, 1093 (struct hwc_spec *)val, match_major); 1094 } 1095 1096 /* 1097 * Lookup bindingname@address. 1098 * Take care not to perform duplicate lookups. 1099 */ 1100 parname = i_ddi_parname(dip, parname_buf); 1101 if (*parname != '\0') { 1102 ASSERT(*parname != '/'); 1103 if ((strcmp(parname, deviname) != 0) && 1104 (mod_hash_find(hwc_par_hash, 1105 (mod_hash_key_t)parname, &val) == 0)) { 1106 hwc_spec_add(&list, 1107 (struct hwc_spec *)val, match_major); 1108 } 1109 } 1110 1111 /* 1112 * Lookup driver binding name 1113 */ 1114 bindname = ddi_binding_name(dip); 1115 ASSERT(*bindname != '/'); 1116 if ((strcmp(bindname, parname) != 0) && 1117 (strcmp(bindname, deviname) != 0) && 1118 (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0)) 1119 hwc_spec_add(&list, (struct hwc_spec *)val, match_major); 1120 1121 /* 1122 * Lookup driver name 1123 */ 1124 drvname = (char *)ddi_driver_name(dip); 1125 ASSERT(*drvname != '/'); 1126 if ((strcmp(drvname, bindname) != 0) && 1127 (strcmp(drvname, parname) != 0) && 1128 (strcmp(drvname, deviname) != 0) && 1129 (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0)) 1130 hwc_spec_add(&list, (struct hwc_spec *)val, match_major); 1131 1132 kmem_free(pathname_buf, 3 * MAXPATHLEN); 1133 1134 /* 1135 * Lookup classes exported by this node and lookup the 1136 * class hash table for all .conf specs 1137 */ 1138 nclass = i_ddi_get_exported_classes(dip, &classes); 1139 for (i = 0; i < nclass; i++) { 1140 if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i], 1141 &val) == 0) 1142 hwc_spec_add(&list, (struct hwc_spec *)val, 1143 match_major); 1144 } 1145 i_ddi_free_exported_classes(classes, nclass); 1146 1147 mutex_exit(&hwc_hash_lock); 1148 return (list); 1149 } 1150