|
root / base / usr / src / uts / common / os / modsubr.c
modsubr.c C 1155 lines 26.9 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/param.h>
#include <sys/modctl.h>
#include <sys/modhash.h>
#include <sys/open.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/mode.h>
#include <sys/pathname.h>
#include <sys/vnode.h>
#include <sys/ddi_impldefs.h>
#include <sys/ddi_implfuncs.h>
#include <sys/esunddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/systeminfo.h>
#include <sys/hwconf.h>
#include <sys/file.h>
#include <sys/varargs.h>
#include <sys/thread.h>
#include <sys/cred.h>
#include <sys/autoconf.h>
#include <sys/kobj.h>
#include <sys/consdev.h>
#include <sys/systm.h>
#include <sys/debug.h>
#include <sys/atomic.h>

extern struct dev_ops nodev_ops;
extern struct dev_ops mod_nodev_ops;

struct mod_noload {
	struct mod_noload *mn_next;
	char *mn_name;
};

/*
 * Function prototypes
 */
static int init_stubs(struct modctl *, struct mod_modinfo *);
static int nm_hash(const char *);
static void make_syscallname(const char *, int);
static void hwc_hash_init();
static void hwc_hash(struct hwc_spec *, major_t);
static void hwc_unhash(struct hwc_spec *);

int
major_valid(major_t major)
{
	return (major != DDI_MAJOR_T_NONE && major < devcnt);
}

int
driver_installed(major_t major)
{
	return (major_valid(major) && devnamesp[major].dn_name != NULL);
}

int
driver_active(major_t major)
{
	return (driver_installed(major) && !(devnamesp[major].dn_flags &
	    (DN_DRIVER_REMOVED|DN_DRIVER_INACTIVE)));
}

struct dev_ops *
mod_hold_dev_by_major(major_t major)
{
	struct dev_ops **devopspp, *ops;
	int loaded;
	char *drvname;

	if (!driver_active(major))
		return (NULL);

	LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
	devopspp = &devopsp[major];
	loaded = 1;
	while (loaded && !CB_DRV_INSTALLED(*devopspp)) {
		UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
		drvname = mod_major_to_name(major);
		if (drvname == NULL)
			return (NULL);
		loaded = (modload("drv", drvname) != -1);
		LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
	}
	if (loaded) {
		INCR_DEV_OPS_REF(*devopspp);
		ops = *devopspp;
	} else {
		ops = NULL;
	}
	UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
	return (ops);
}

#ifdef	DEBUG_RELE
static int mod_rele_pause = DEBUG_RELE;
#endif	/* DEBUG_RELE */

void
mod_rele_dev_by_major(major_t major)
{
	struct dev_ops *ops;
	struct devnames *dnp;

	if (!driver_active(major))
		return;

	dnp = &devnamesp[major];
	LOCK_DEV_OPS(&dnp->dn_lock);
	ops = devopsp[major];
	ASSERT(CB_DRV_INSTALLED(ops));

#ifdef	DEBUG_RELE
	if (!DEV_OPS_HELD(ops))  {
		char *s;
		static char *msg = "mod_rele_dev_by_major: unheld driver!";

		printf("mod_rele_dev_by_major: Major dev <%u>, name <%s>\n",
		    (uint_t)major,
		    (s = mod_major_to_name(major)) ? s : "unknown");
		if (mod_rele_pause)
			debug_enter(msg);
		else
			printf("%s\n", msg);
		UNLOCK_DEV_OPS(&dnp->dn_lock);
		return;			/* XXX: Note changed behavior */
	}

#endif	/* DEBUG_RELE */

	if (!DEV_OPS_HELD(ops)) {
		cmn_err(CE_PANIC,
		    "mod_rele_dev_by_major: Unheld driver: major number <%u>",
		    (uint_t)major);
	}
	DECR_DEV_OPS_REF(ops);
	UNLOCK_DEV_OPS(&dnp->dn_lock);
}

struct dev_ops *
mod_hold_dev_by_devi(dev_info_t *devi)
{
	major_t major;
	char *name;

	name = ddi_get_name(devi);
	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
		return (NULL);
	return (mod_hold_dev_by_major(major));
}

void
mod_rele_dev_by_devi(dev_info_t *devi)
{
	major_t major;
	char *name;

	name = ddi_get_name(devi);
	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
		return;
	mod_rele_dev_by_major(major);
}

int
nomod_zero()
{
	return (0);
}

int
nomod_minus_one()
{
	return (-1);
}

int
nomod_einval()
{
	return (EINVAL);
}

void
nomod_void()
{
	/* nothing */
}

/*
 * Install all the stubs for a module.
 * Return zero if there were no errors or an errno value.
 */
int
install_stubs_by_name(struct modctl *modp, const char *name)
{
	char *p;
	const char *filenamep, *iter;
	char namebuf[MODMAXNAMELEN + 12];
	struct mod_modinfo *mp;

	iter = name;
	filenamep = name;

	while (*iter) {
		if (*iter++ == '/')
			filenamep = iter;
	}

	/*
	 * Concatenate "name" with "_modname" then look up this symbol
	 * in the kernel.  If not found, we're done.
	 * If found, then find the "mod" info structure and call init_stubs().
	 */
	p = namebuf;

	while (*filenamep && *filenamep != '.')
		*p++ = *filenamep++;

	(void) strcpy(p, "_modinfo");

	if ((mp = (struct mod_modinfo *)modgetsymvalue(namebuf, 1)) != 0)
		return (init_stubs(modp, mp));
	else
		return (0);
}

static int
init_stubs(struct modctl *modp, struct mod_modinfo *mp)
{
	struct mod_stub_info *sp;
	int i;
	ulong_t offset;
	uintptr_t funcadr;
	char *funcname;

	modp->mod_modinfo = mp;

	/*
	 * Fill in all stubs for this module.  We can't be lazy, since
	 * some calls could come in from interrupt level, and we
	 * can't modlookup then (symbols may be paged out).
	 */
	sp = mp->modm_stubs;
	for (i = 0; sp->mods_func_adr; i++, sp++) {
		funcname = modgetsymname(sp->mods_stub_adr, &offset);
		if (funcname == NULL) {
			printf("init_stubs: couldn't find symbol "
			    "in module %s\n", mp->modm_module_name);
			return (EFAULT);
		}
		funcadr = kobj_lookup(modp->mod_mp, funcname);

		if (kobj_addrcheck(modp->mod_mp, (caddr_t)funcadr)) {
			printf("%s:%s() not defined properly\n",
			    mp->modm_module_name, funcname);
			return (EFAULT);
		}
		sp->mods_func_adr = funcadr;
	}
	mp->mp = modp;
	return (0);
}

/*
 * modp->mod_modinfo has to be checked in these functions before
 * mod_stub_info is accessed because it's not guranteed that all
 * modules define mod_stub_info structures.
 */
void
install_stubs(struct modctl *modp)
{
	struct mod_stub_info *stub;

	if (modp->mod_modinfo) {
		membar_producer();
		for (stub = modp->mod_modinfo->modm_stubs;
		    stub->mods_func_adr; stub++) {
			stub->mods_flag |= MODS_INSTALLED;
		}
		membar_producer();
	}
}

void
uninstall_stubs(struct modctl *modp)
{
	struct mod_stub_info *stub;

	if (modp->mod_modinfo) {
		membar_producer();
		for (stub = modp->mod_modinfo->modm_stubs;
		    stub->mods_func_adr; stub++) {
			stub->mods_flag &= ~MODS_INSTALLED;
		}
		membar_producer();
	}
}

void
reset_stubs(struct modctl *modp)
{
	struct mod_stub_info *stub;

	if (modp->mod_modinfo) {
		for (stub = modp->mod_modinfo->modm_stubs;
		    stub->mods_func_adr; stub++) {
			if (stub->mods_flag & (MODS_WEAK | MODS_NOUNLOAD))
				stub->mods_func_adr =
				    (uintptr_t)stub->mods_errfcn;
			else
				stub->mods_func_adr =
				    (uintptr_t)mod_hold_stub;
		}
		modp->mod_modinfo->mp = NULL;
	}
}

struct modctl *
mod_getctl(struct modlinkage *modlp)
{
	struct modctl	*modp;

	mutex_enter(&mod_lock);
	modp = &modules;
	do {
		if (modp->mod_linkage == modlp) {
			mutex_exit(&mod_lock);
			return (modp);
		}
	} while ((modp = modp->mod_next) != &modules);
	mutex_exit(&mod_lock);
	return (NULL);
}


/*
 * Attach driver.conf info to devnames for a driver
 */
struct par_list *
impl_make_parlist(major_t major)
{
	int err;
	struct par_list *pl = NULL, *tmp;
	ddi_prop_t *props = NULL;
	char *confname, *drvname;
	struct devnames *dnp;

	dnp = &devnamesp[major];

	ASSERT(mutex_owned(&dnp->dn_lock));

	/*
	 * If .conf file already parsed or driver removed, just return.
	 * May return NULL.
	 */
	if (dnp->dn_flags & (DN_CONF_PARSED | DN_DRIVER_REMOVED))
		return (dnp->dn_pl);

	drvname = mod_major_to_name(major);
	if (drvname == NULL)
		return (NULL);

	confname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
	(void) snprintf(confname, MAXNAMELEN, "drv/%s.conf", drvname);
	err = hwc_parse(confname, &pl, &props);
	kmem_free(confname, MAXNAMELEN);
	if (err)	/* file doesn't exist */
		return (NULL);

	/*
	 * If there are global properties, reference it from dnp.
	 */
	if (props)
		dnp->dn_global_prop_ptr = i_ddi_prop_list_create(props);

	/*
	 * Hash specs to be looked up by nexus drivers
	 */
	tmp = pl;
	while (tmp) {
		(void) hwc_hash(tmp->par_specs, major);
		tmp = tmp->par_next;
	}

	if (!i_ddi_io_initialized()) {
		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_FORCEATTACH,
		    DDI_PROP_TYPE_INT, &props))
			dnp->dn_flags |= DN_FORCE_ATTACH;
		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_OPEN_RETURNS_EINTR,
		    DDI_PROP_TYPE_INT, &props))
			dnp->dn_flags |= DN_OPEN_RETURNS_EINTR;
		if (i_ddi_prop_search(DDI_DEV_T_ANY, "scsi-size-clean",
		    DDI_PROP_TYPE_INT, &props))
			dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
	}

	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_VHCI_CLASS,
	    DDI_PROP_TYPE_STRING, &props))
		dnp->dn_flags |= DN_PHCI_DRIVER;

	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_DEVID_REGISTRANT,
	    DDI_PROP_TYPE_INT, &props)) {
		dnp->dn_flags |= DN_DEVID_REGISTRANT;
	}

	dnp->dn_flags |= DN_CONF_PARSED;
	dnp->dn_pl = pl;
	return (pl);
}

/*
 * Destroy driver.conf info in devnames array for a driver
 */
int
impl_free_parlist(major_t major)
{
	struct par_list *pl;
	struct devnames *dnp = &devnamesp[major];

	/*
	 * Unref driver global property list. Don't destroy it
	 * because some instances may still be referencing it.
	 * The property list will be freed when the last ref
	 * goes away.
	 */
	if (dnp->dn_global_prop_ptr) {
		i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp);
		dnp->dn_global_prop_ptr = NULL;
	}

	/*
	 * remove specs from hash table
	 */
	for (pl = dnp->dn_pl; pl; pl = pl->par_next)
		hwc_unhash(pl->par_specs);

	impl_delete_par_list(dnp->dn_pl);
	dnp->dn_pl = NULL;
	dnp->dn_flags &= ~DN_CONF_PARSED;
	return (0);
}

struct bind *mb_hashtab[MOD_BIND_HASHSIZE];
struct bind *sb_hashtab[MOD_BIND_HASHSIZE];

static int
nm_hash(const char *name)
{
	char c;
	int hash = 0;

	for (c = *name++; c; c = *name++)
		hash ^= c;

	return (hash & MOD_BIND_HASHMASK);
}

void
clear_binding_hash(struct bind **bhash)
{
	int i;
	struct bind *bp, *bp1;

	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
		bp = bhash[i];
		while (bp != NULL) {
			kmem_free(bp->b_name, strlen(bp->b_name) + 1);
			if (bp->b_bind_name) {
				kmem_free(bp->b_bind_name,
				    strlen(bp->b_bind_name) + 1);
			}
			bp1 = bp;
			bp = bp->b_next;
			kmem_free(bp1, sizeof (struct bind));
		}
		bhash[i] = NULL;
	}
}

/* Find an mbind by name match (caller can ask for deleted match) */
static struct bind *
find_mbind(const char *name, struct bind **hashtab, int deleted)
{
	struct bind	*mb;

	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
		if (deleted && (mb->b_num >= 0))
			continue;			/* skip active */
		if (!deleted && (mb->b_num < 0))
			continue;			/* skip deleted */

		/* return if name matches */
		if (strcmp(name, mb->b_name) == 0) {
			break;
		}
	}
	return (mb);
}

/*
 * Create an entry for the given (name, major, bind_name) tuple in the
 * hash table supplied.  Reject the attempt to do so if 'name' is already
 * in the hash table.
 *
 * Does not provide synchronization, so use only during boot or with
 * externally provided locking.
 */
int
make_mbind(char *name, int num, char *bind_name, struct bind **hashtab)
{
	struct bind	*mb;
	struct bind	**pmb;

	ASSERT(hashtab != NULL);
	ASSERT(num >= 0);

	/* Fail if the key being added is already established */
	if (find_mbind(name, hashtab, 0) != NULL)
		return (-1);

	/* Allocate new mbind */
	mb = kmem_zalloc(sizeof (struct bind), KM_SLEEP);
	mb->b_name = i_ddi_strdup(name, KM_SLEEP);
	mb->b_num = num;
	if (bind_name != NULL)
		mb->b_bind_name = i_ddi_strdup(bind_name, KM_SLEEP);

	/* Insert at head of hash */
	pmb = &hashtab[nm_hash(name)];
	mb->b_next = *pmb;
	*pmb = mb;
	return (0);
}

/*
 * Delete a binding from a binding-hash. Since there is no locking we
 * delete an mbind by making its b_num negative. We also support find_mbind
 * of deleted entries, so we still need deleted items on the list.
 */
void
delete_mbind(const char *name, struct bind **hashtab)
{
	struct bind	*mb;

	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
		if ((mb->b_num >= 0) && (strcmp(name, mb->b_name) == 0)) {
			/* delete by making b_num negative */
			if (moddebug & MODDEBUG_BINDING) {
				cmn_err(CE_CONT, "mbind: %s %d deleted\n",
				    name, mb->b_num);
			}
			mb->b_num = -mb->b_num;
			break;
		}
	}
}

/*
 * Delete all items in an mbind associated with specified num.
 * An example would be rem_drv deleting all aliases associated with a
 * driver major number.
 */
void
purge_mbind(int num, struct bind **hashtab)
{
	int		i;
	struct bind	*mb;

	/* search all hash lists for items that associated with 'num' */
	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
		for (mb = hashtab[i]; mb; mb = mb->b_next) {
			if (mb->b_num == num) {
				if (moddebug & MODDEBUG_BINDING)
					cmn_err(CE_CONT,
					    "mbind: %s %d purged\n",
					    mb->b_name, num);
				/* purge by changing the sign */
				mb->b_num = -num;
			}
		}
	}
}

major_t
mod_name_to_major(const char *name)
{
	struct bind	*mbind;
	major_t		maj;

	/* Search for non-deleted match. */
	if ((mbind = find_mbind(name, mb_hashtab, 0)) != NULL) {
		if (moddebug & MODDEBUG_BINDING) {
			if (find_mbind(name, mb_hashtab, 1))
				cmn_err(CE_CONT,
				    "'%s' has deleted match too\n", name);
		}
		return ((major_t)mbind->b_num);
	}

	/*
	 * Search for deleted match: We may find that we have dependencies
	 * on drivers that have been deleted (but the old driver may still
	 * be bound to a node). These callers should be converted to use
	 * ddi_driver_major(i.e. devi_major).
	 */
	if (moddebug & MODDEBUG_BINDING) {
		if ((mbind = find_mbind(name, mb_hashtab, 1)) != NULL) {
			maj = (major_t)(-(mbind->b_num));
			cmn_err(CE_CONT, "Reference to deleted alias '%s' %d\n",
			    name, maj);
		}
	}

	return (DDI_MAJOR_T_NONE);
}

char *
mod_major_to_name(major_t major)
{
	if (!driver_installed(major))
		return (NULL);
	return ((&devnamesp[major])->dn_name);
}

/*
 * Set up the devnames array.  Error check for duplicate entries.
 */
void
init_devnamesp(int size)
{
	int hshndx;
	struct bind *bp;
	static char dupwarn[] =
	    "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" "
	    "in /etc/name_to_major.";
	static char badmaj[] = "The major number %u is invalid.";

	ASSERT(size <= L_MAXMAJ32 && size > 0);

	/*
	 * Allocate the devnames array.  All mutexes and cv's will be
	 * automagically initialized.
	 */
	devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP);

	/*
	 * Stick the contents of mb_hashtab into the devnames array.  Warn if
	 * two hash entries correspond to the same major number, or if a
	 * major number is out of range.
	 */
	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
		for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) {
			if (make_devname(bp->b_name,
			    (major_t)bp->b_num, 0) != 0) {
				/*
				 * If there is not an entry at b_num already,
				 * then this must be a bad major number.
				 */
				char *nm = mod_major_to_name(bp->b_num);
				if (nm == NULL) {
					cmn_err(CE_WARN, badmaj,
					    (uint_t)bp->b_num);
				} else {
					cmn_err(CE_WARN, dupwarn, bp->b_name,
					    bp->b_num, nm, bp->b_num);
				}
			}
		}
	}

	/* Initialize hash table for hwc_spec's */
	hwc_hash_init();
}

int
make_devname(const char *name, major_t major, int dn_flags)
{
	struct devnames *dnp;
	char *copy;

	/*
	 * Until on-disk support for major nums > 14 bits arrives, fail
	 * any major numbers that are too big.
	 */
	if (major > L_MAXMAJ32)
		return (EINVAL);

	dnp = &devnamesp[major];
	LOCK_DEV_OPS(&dnp->dn_lock);
	if (dnp->dn_name) {
		if (strcmp(dnp->dn_name, name) != 0) {
			/* Another driver already here */
			UNLOCK_DEV_OPS(&dnp->dn_lock);
			return (EINVAL);
		}
		/* Adding back a removed driver */
		dnp->dn_flags &= ~DN_DRIVER_REMOVED;
		dnp->dn_flags |= dn_flags;
		UNLOCK_DEV_OPS(&dnp->dn_lock);
		return (0);
	}

	/*
	 * Check if flag is taken by getudev()
	 */
	if (dnp->dn_flags & DN_TAKEN_GETUDEV) {
		UNLOCK_DEV_OPS(&dnp->dn_lock);
		return (EINVAL);
	}

	copy = kmem_alloc(strlen(name) + 1, KM_SLEEP);
	(void) strcpy(copy, name);

	/* Make sure string is copied before setting dn_name */
	membar_producer();
	dnp->dn_name = copy;
	dnp->dn_flags = dn_flags;
	UNLOCK_DEV_OPS(&dnp->dn_lock);
	return (0);
}

/*
 * Set up the syscallnames array.
 */
void
init_syscallnames(int size)
{
	int hshndx;
	struct bind *bp;

	syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP);

	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
		for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) {
			if (bp->b_num < 0 || bp->b_num >= size) {
				cmn_err(CE_WARN,
				    "!Couldn't add system call \"%s %d\". "
				    "Value out of range (0..%d) in "
				    "/etc/name_to_sysnum.",
				    bp->b_name, bp->b_num, size - 1);
				continue;
			}
			make_syscallname(bp->b_name, bp->b_num);
		}
	}
}

static void
make_syscallname(const char *name, int sysno)
{
	char **cp = &syscallnames[sysno];

	if (*cp != NULL) {
		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
		    name, sysno, *cp, sysno);
		return;
	}
	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
	(void) strcpy(*cp, name);
}

/*
 * Given a system call name, get its number.
 */
int
mod_getsysnum(const char *name)
{
	struct bind *mbind;

	if ((mbind = find_mbind(name, sb_hashtab, 0)) != NULL)
		return (mbind->b_num);

	return (-1);
}

/*
 * Given a system call number, get the system call name.
 */
char *
mod_getsysname(int sysnum)
{
	return (syscallnames[sysnum]);
}

/*
 * Find the name of the module containing the specified pc.
 * Returns the name on success, "<unknown>" on failure.
 * No mod_lock locking is required because things are never deleted from
 * the &modules list.
 */
char *
mod_containing_pc(caddr_t pc)
{
	struct modctl	*mcp = &modules;

	do {
		if (mcp->mod_mp != NULL &&
		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
			return (mcp->mod_modname);
	} while ((mcp = mcp->mod_next) != &modules);
	return ("<unknown>");
}

/*
 * Hash tables for hwc_spec
 *
 * The purpose of these hash tables are to allow the framework to discover
 * all possible .conf children for a given nexus. There are two hash tables.
 * One is hashed based on parent name, the on the class name. Each
 * driver.conf file translates to a list of hwc_spec's. Adding and
 * removing the entire list is an atomic operation, protected by
 * the hwc_hash_lock.
 *
 * What we get from all the hashing is the function hwc_get_child_spec().
 */
#define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */

static mod_hash_t *hwc_par_hash;	/* hash by parent name */
static mod_hash_t *hwc_class_hash;	/* hash by class name */
static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */

/*
 * Initialize hash tables for parent and class specs
 */
static void
hwc_hash_init()
{
	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
}

/*
 * Insert a spec into hash table. hwc_hash_lock must be held
 */
static void
hwc_hash_insert(struct hwc_spec *spec, const char *name, mod_hash_t *hash)
{
	mod_hash_key_t key;
	struct hwc_spec *entry = NULL;

	ASSERT(name != NULL);

	if (mod_hash_find(hash, (mod_hash_key_t)name,
	    (mod_hash_val_t)&entry) != 0) {
		/* Name doesn't exist, insert a new key */
		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
		(void) strcpy((char *)key, name);
		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
			kmem_free(key, strlen(name) + 1);
			cmn_err(CE_WARN, "hwc hash state inconsistent");
		}
		return;
	}

	/*
	 * Name is already present, append spec to the list.
	 * This is the case when driver.conf specifies multiple
	 * nodes under a single parent or class.
	 */
	while (entry->hwc_hash_next)
		entry = entry->hwc_hash_next;
	entry->hwc_hash_next = spec;
}

/*
 * Remove a spec entry from spec hash table, the spec itself is
 * destroyed external to this function.
 */
static void
hwc_hash_remove(struct hwc_spec *spec, const char *name, mod_hash_t *hash)
{
	char *key;
	struct hwc_spec *entry;

	ASSERT(name != NULL);

	if (mod_hash_find(hash, (mod_hash_key_t)name,
	    (mod_hash_val_t)&entry) != 0) {
		return;	/* name not found in hash */
	}

	/*
	 * If the head is the spec to be removed, either destroy the
	 * entry or replace it with the remaining list.
	 */
	if (entry == spec) {
		if (spec->hwc_hash_next == NULL) {
			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
			return;
		}
		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
		(void) strcpy(key, name);
		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
		    (mod_hash_val_t)spec->hwc_hash_next);
		spec->hwc_hash_next = NULL;
		return;
	}

	/*
	 * If the head is not the one, look for the spec in the
	 * hwc_hash_next linkage.
	 */
	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
		entry = entry->hwc_hash_next;

	if (entry->hwc_hash_next) {
		entry->hwc_hash_next = spec->hwc_hash_next;
		spec->hwc_hash_next = NULL;
	}
}

/*
 * Hash a list of specs based on either parent name or class name
 */
static void
hwc_hash(struct hwc_spec *spec_list, major_t major)
{
	struct hwc_spec *spec = spec_list;

	mutex_enter(&hwc_hash_lock);
	while (spec) {
		/* Put driver major here so parent can find it */
		spec->hwc_major = major;

		if (spec->hwc_parent_name != NULL) {
			hwc_hash_insert(spec, spec->hwc_parent_name,
			    hwc_par_hash);
		} else if (spec->hwc_class_name != NULL) {
			hwc_hash_insert(spec, spec->hwc_class_name,
			    hwc_class_hash);
		} else {
			cmn_err(CE_WARN,
			    "hwc_hash: No class or parent specified");
		}
		spec = spec->hwc_next;
	}
	mutex_exit(&hwc_hash_lock);
}

/*
 * Remove a list of specs from hash tables. Don't destroy the specs yet.
 */
static void
hwc_unhash(struct hwc_spec *spec_list)
{
	struct hwc_spec *spec = spec_list;

	mutex_enter(&hwc_hash_lock);
	while (spec) {
		if (spec->hwc_parent_name != NULL) {
			hwc_hash_remove(spec, spec->hwc_parent_name,
			    hwc_par_hash);
		} else if (spec->hwc_class_name != NULL) {
			hwc_hash_remove(spec, spec->hwc_class_name,
			    hwc_class_hash);
		} else {
			cmn_err(CE_WARN,
			    "hwc_unhash: No class or parent specified");
		}
		spec = spec->hwc_next;
	}
	mutex_exit(&hwc_hash_lock);
}

/*
 * Make a copy of specs in a hash entry and add to the end of listp.
 * Called by nexus to locate a list of child specs.
 *
 * entry is a list of hwc_spec chained together with hwc_hash_next.
 * listp points to list chained together with hwc_next.
 */
static void
hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
    major_t match_major)
{
	/* Find the tail of the list */
	while (*listp)
		listp = &(*listp)->hwc_next;

	while (entry) {
		struct hwc_spec *spec;

		if ((match_major != DDI_MAJOR_T_NONE) &&
		    (match_major != entry->hwc_major)) {
			entry = entry->hwc_hash_next;
			continue;
		}

		/*
		 * Allocate spec and copy the content of entry.
		 * No need to copy class/parent name since caller
		 * already knows the parent dip.
		 */
		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
		spec->hwc_devi_name = i_ddi_strdup(
		    entry->hwc_devi_name, KM_SLEEP);
		spec->hwc_major = entry->hwc_major;
		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);

		*listp = spec;
		listp = &spec->hwc_next;
		entry = entry->hwc_hash_next;
	}
}

/*
 * Given a dip, find the list of child .conf specs from most specific
 * (parent pathname) to least specific (class name).
 *
 * This function allows top-down loading to be implemented without
 * changing the format of driver.conf file.
 */
struct hwc_spec *
hwc_get_child_spec(dev_info_t *dip, major_t match_major)
{
	extern char *i_ddi_parname(dev_info_t *, char *);
	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
	extern void i_ddi_free_exported_classes(char **, int);

	int i, nclass;
	char **classes;
	struct hwc_spec *list = NULL;
	mod_hash_val_t val;
	char *parname, *parname_buf;
	char *deviname, *deviname_buf;
	char *pathname, *pathname_buf;
	char *bindname;
	char *drvname;

	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
	deviname_buf = pathname_buf + MAXPATHLEN;
	parname_buf = pathname_buf + (2 * MAXPATHLEN);

	mutex_enter(&hwc_hash_lock);

	/*
	 * Lookup based on full path.
	 * In the case of root node, ddi_pathname would return
	 * null string so just skip calling it.
	 * As the pathname always begins with /, no simpler
	 * name can duplicate it.
	 */
	pathname = (dip == ddi_root_node()) ? "/" :
	    ddi_pathname(dip, pathname_buf);
	ASSERT(pathname != NULL);
	ASSERT(*pathname == '/');

	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
	}

	/*
	 * Lookup nodename@address.
	 * Note deviname cannot match pathname.
	 */
	deviname = ddi_deviname(dip, deviname_buf);
	if (*deviname != '\0') {
		/*
		 * Skip leading / returned by ddi_deviname.
		 */
		ASSERT(*deviname == '/');
		deviname++;
		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
		    (mod_hash_key_t)deviname, &val) == 0))
			hwc_spec_add(&list,
			    (struct hwc_spec *)val, match_major);
	}

	/*
	 * Lookup bindingname@address.
	 * Take care not to perform duplicate lookups.
	 */
	parname = i_ddi_parname(dip, parname_buf);
	if (*parname != '\0') {
		ASSERT(*parname != '/');
		if ((strcmp(parname, deviname) != 0) &&
		    (mod_hash_find(hwc_par_hash,
		    (mod_hash_key_t)parname, &val) == 0)) {
			hwc_spec_add(&list,
			    (struct hwc_spec *)val, match_major);
		}
	}

	/*
	 * Lookup driver binding name
	 */
	bindname = ddi_binding_name(dip);
	ASSERT(*bindname != '/');
	if ((strcmp(bindname, parname) != 0) &&
	    (strcmp(bindname, deviname) != 0) &&
	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);

	/*
	 * Lookup driver name
	 */
	drvname = (char *)ddi_driver_name(dip);
	ASSERT(*drvname != '/');
	if ((strcmp(drvname, bindname) != 0) &&
	    (strcmp(drvname, parname) != 0) &&
	    (strcmp(drvname, deviname) != 0) &&
	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);

	kmem_free(pathname_buf, 3 * MAXPATHLEN);

	/*
	 * Lookup classes exported by this node and lookup the
	 * class hash table for all .conf specs
	 */
	nclass = i_ddi_get_exported_classes(dip, &classes);
	for (i = 0; i < nclass; i++) {
		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
		    &val) == 0)
			hwc_spec_add(&list, (struct hwc_spec *)val,
			    match_major);
	}
	i_ddi_free_exported_classes(classes, nclass);

	mutex_exit(&hwc_hash_lock);
	return (list);
}