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