xref: /illumos-gate/usr/src/uts/common/os/modsubr.c (revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57)
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 2007 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 "
259 			    "in module %s\n", 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 		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_OPEN_RETURNS_EINTR,
401 		    DDI_PROP_TYPE_INT, &props))
402 			dnp->dn_flags |= DN_OPEN_RETURNS_EINTR;
403 	}
404 
405 	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_VHCI_CLASS,
406 	    DDI_PROP_TYPE_STRING, &props))
407 		dnp->dn_flags |= DN_PHCI_DRIVER;
408 
409 	dnp->dn_flags |= DN_CONF_PARSED;
410 	dnp->dn_pl = pl;
411 	return (pl);
412 }
413 
414 /*
415  * Destroy driver.conf info in devnames array for a driver
416  */
417 int
418 impl_free_parlist(major_t major)
419 {
420 	struct par_list *pl;
421 	struct devnames *dnp = &devnamesp[major];
422 
423 	/*
424 	 * Unref driver global property list. Don't destroy it
425 	 * because some instances may still be referencing it.
426 	 * The property list will be freed when the last ref
427 	 * goes away.
428 	 */
429 	if (dnp->dn_global_prop_ptr) {
430 		i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp);
431 		dnp->dn_global_prop_ptr = NULL;
432 	}
433 
434 	/*
435 	 * remove specs from hash table
436 	 */
437 	for (pl = dnp->dn_pl; pl; pl = pl->par_next)
438 		hwc_unhash(pl->par_specs);
439 
440 	impl_delete_par_list(dnp->dn_pl);
441 	dnp->dn_pl = NULL;
442 	dnp->dn_flags &= ~DN_CONF_PARSED;
443 	return (0);
444 }
445 
446 struct bind *mb_hashtab[MOD_BIND_HASHSIZE];
447 struct bind *sb_hashtab[MOD_BIND_HASHSIZE];
448 
449 static int
450 nm_hash(char *name)
451 {
452 	char c;
453 	int hash = 0;
454 
455 	for (c = *name++; c; c = *name++)
456 		hash ^= c;
457 
458 	return (hash & MOD_BIND_HASHMASK);
459 }
460 
461 void
462 clear_binding_hash(struct bind **bhash)
463 {
464 	int i;
465 	struct bind *bp, *bp1;
466 
467 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
468 		bp = bhash[i];
469 		while (bp != NULL) {
470 			kmem_free(bp->b_name, strlen(bp->b_name) + 1);
471 			if (bp->b_bind_name) {
472 				kmem_free(bp->b_bind_name,
473 				    strlen(bp->b_bind_name) + 1);
474 			}
475 			bp1 = bp;
476 			bp = bp->b_next;
477 			kmem_free(bp1, sizeof (struct bind));
478 		}
479 		bhash[i] = NULL;
480 	}
481 }
482 
483 static struct bind *
484 find_mbind(char *name, struct bind **hashtab)
485 {
486 	int hashndx;
487 	struct bind *mb;
488 
489 	hashndx = nm_hash(name);
490 	for (mb = hashtab[hashndx]; mb; mb = mb->b_next) {
491 		if (strcmp(name, mb->b_name) == 0)
492 			break;
493 	}
494 
495 	return (mb);
496 }
497 
498 /*
499  * Create an entry for the given (name, major, bind_name) tuple in the
500  * hash table supplied.  Reject the attempt to do so if 'name' is already
501  * in the hash table.
502  *
503  * Does not provide synchronization, so use only during boot or with
504  * externally provided locking.
505  */
506 int
507 make_mbind(char *name, int major, char *bind_name, struct bind **hashtab)
508 {
509 	struct bind *bp;
510 	int hashndx;
511 
512 	ASSERT(hashtab != NULL);
513 
514 	/*
515 	 * Fail if the key being added is already in the hash table
516 	 */
517 	if (find_mbind(name, hashtab) != NULL)
518 		return (-1);
519 
520 	bp = kmem_zalloc(sizeof (struct bind), KM_SLEEP);
521 	bp->b_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
522 	(void) strcpy(bp->b_name, name);
523 	bp->b_num = major;
524 	if (bind_name != NULL) {
525 		bp->b_bind_name = kmem_alloc(strlen(bind_name) + 1, KM_SLEEP);
526 		(void) strcpy(bp->b_bind_name, bind_name);
527 	}
528 	hashndx = nm_hash(name);
529 	bp->b_next = hashtab[hashndx];
530 	hashtab[hashndx] = bp;
531 
532 	return (0);
533 }
534 
535 /*
536  * Delete a binding from a binding-hash.
537  *
538  * Does not provide synchronization, so use only during boot or with
539  * externally provided locking.
540  */
541 void
542 delete_mbind(char *name, struct bind **hashtab)
543 {
544 	int hashndx;
545 	struct bind *b, *bparent = NULL;
546 	struct bind *t = NULL;		/* target to delete */
547 
548 	hashndx = nm_hash(name);
549 
550 	if (hashtab[hashndx] == NULL)
551 		return;
552 
553 	b = hashtab[hashndx];
554 	if (strcmp(name, b->b_name) == 0) {	/* special case first elem. */
555 		hashtab[hashndx] = b->b_next;
556 		t = b;
557 	} else {
558 		for (b = hashtab[hashndx]; b; b = b->b_next) {
559 			if (strcmp(name, b->b_name) == 0) {
560 				ASSERT(bparent);
561 				t = b;
562 				bparent->b_next = b->b_next;
563 				break;
564 			}
565 			bparent = b;
566 		}
567 	}
568 
569 	if (t != NULL) {	/* delete the target */
570 		ASSERT(t->b_name);
571 		kmem_free(t->b_name, strlen(t->b_name) + 1);
572 		if (t->b_bind_name)
573 			kmem_free(t->b_bind_name, strlen(t->b_bind_name) + 1);
574 		kmem_free(t, sizeof (struct bind));
575 	}
576 }
577 
578 
579 major_t
580 mod_name_to_major(char *name)
581 {
582 	struct bind *mbind;
583 
584 	if ((mbind = find_mbind(name, mb_hashtab)) != NULL)
585 		return ((major_t)mbind->b_num);
586 
587 	return ((major_t)-1);
588 }
589 
590 char *
591 mod_major_to_name(major_t major)
592 {
593 	if (major >= devcnt)
594 		return (NULL);
595 	return ((&devnamesp[major])->dn_name);
596 }
597 
598 /*
599  * Set up the devnames array.  Error check for duplicate entries.
600  */
601 void
602 init_devnamesp(int size)
603 {
604 	int hshndx;
605 	struct bind *bp;
606 	static char dupwarn[] =
607 	    "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" "
608 	    "in /etc/name_to_major.";
609 	static char badmaj[] = "The major number %u is invalid.";
610 
611 	ASSERT(size <= L_MAXMAJ32 && size > 0);
612 
613 	/*
614 	 * Allocate the devnames array.  All mutexes and cv's will be
615 	 * automagically initialized.
616 	 */
617 	devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP);
618 
619 	/*
620 	 * Stick the contents of mb_hashtab into the devnames array.  Warn if
621 	 * two hash entries correspond to the same major number, or if a
622 	 * major number is out of range.
623 	 */
624 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
625 		for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) {
626 			if (make_devname(bp->b_name, (major_t)bp->b_num) != 0) {
627 				/*
628 				 * If there is not an entry at b_num already,
629 				 * then this must be a bad major number.
630 				 */
631 				char *nm = mod_major_to_name(bp->b_num);
632 				if (nm == NULL) {
633 					cmn_err(CE_WARN, badmaj,
634 					    (uint_t)bp->b_num);
635 				} else {
636 					cmn_err(CE_WARN, dupwarn, bp->b_name,
637 					    bp->b_num, nm, bp->b_num);
638 				}
639 			}
640 		}
641 	}
642 
643 	/* Initialize hash table for hwc_spec's */
644 	hwc_hash_init();
645 }
646 
647 int
648 make_devname(char *name, major_t major)
649 {
650 	struct devnames *dnp;
651 	char *copy;
652 
653 	/*
654 	 * Until on-disk support for major nums > 14 bits arrives, fail
655 	 * any major numbers that are too big.
656 	 */
657 	if (major > L_MAXMAJ32)
658 		return (EINVAL);
659 
660 	dnp = &devnamesp[major];
661 	LOCK_DEV_OPS(&dnp->dn_lock);
662 	if (dnp->dn_name) {
663 		if (strcmp(dnp->dn_name, name) != 0) {
664 			/* Another driver already here */
665 			UNLOCK_DEV_OPS(&dnp->dn_lock);
666 			return (EINVAL);
667 		}
668 		/* Adding back a removed driver */
669 		dnp->dn_flags &= ~DN_DRIVER_REMOVED;
670 		UNLOCK_DEV_OPS(&dnp->dn_lock);
671 		return (0);
672 	}
673 
674 	/*
675 	 * Check if flag is taken by getudev()
676 	 */
677 	if (dnp->dn_flags & DN_TAKEN_GETUDEV) {
678 		UNLOCK_DEV_OPS(&dnp->dn_lock);
679 		return (EINVAL);
680 	}
681 
682 	copy = kmem_alloc(strlen(name) + 1, KM_SLEEP);
683 	(void) strcpy(copy, name);
684 
685 	/* Make sure string is copied before setting dn_name */
686 	membar_producer();
687 	dnp->dn_name = copy;
688 	dnp->dn_flags = 0;
689 	UNLOCK_DEV_OPS(&dnp->dn_lock);
690 	return (0);
691 }
692 
693 /*
694  * Set up the syscallnames array.
695  */
696 void
697 init_syscallnames(int size)
698 {
699 	int hshndx;
700 	struct bind *bp;
701 
702 	syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP);
703 
704 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
705 		for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) {
706 			make_syscallname(bp->b_name, bp->b_num);
707 		}
708 	}
709 }
710 
711 static void
712 make_syscallname(char *name, int sysno)
713 {
714 	char **cp = &syscallnames[sysno];
715 
716 	if (*cp != NULL) {
717 		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
718 		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
719 		    name, sysno, *cp, sysno);
720 		return;
721 	}
722 	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
723 	(void) strcpy(*cp, name);
724 }
725 
726 /*
727  * Given a system call name, get its number.
728  */
729 int
730 mod_getsysnum(char *name)
731 {
732 	struct bind *mbind;
733 
734 	if ((mbind = find_mbind(name, sb_hashtab)) != NULL)
735 		return (mbind->b_num);
736 
737 	return (-1);
738 }
739 
740 /*
741  * Given a system call number, get the system call name.
742  */
743 char *
744 mod_getsysname(int sysnum)
745 {
746 	return (syscallnames[sysnum]);
747 }
748 
749 /*
750  * Find the name of the module containing the specified pc.
751  * Returns the name on success, "<unknown>" on failure.
752  * No mod_lock locking is required because things are never deleted from
753  * the &modules list.
754  */
755 char *
756 mod_containing_pc(caddr_t pc)
757 {
758 	struct modctl	*mcp = &modules;
759 
760 	do {
761 		if (mcp->mod_mp != NULL &&
762 		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
763 			return (mcp->mod_modname);
764 	} while ((mcp = mcp->mod_next) != &modules);
765 	return ("<unknown>");
766 }
767 
768 /*
769  * Hash tables for hwc_spec
770  *
771  * The purpose of these hash tables are to allow the framework to discover
772  * all possible .conf children for a given nexus. There are two hash tables.
773  * One is hashed based on parent name, the on the class name. Each
774  * driver.conf file translates to a list of hwc_spec's. Adding and
775  * removing the entire list is an atomic operation, protected by
776  * the hwc_hash_lock.
777  *
778  * What we get from all the hashing is the function hwc_get_child_spec().
779  */
780 #define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */
781 
782 static mod_hash_t *hwc_par_hash;	/* hash by parent name */
783 static mod_hash_t *hwc_class_hash;	/* hash by class name */
784 static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */
785 
786 /*
787  * Initialize hash tables for parent and class specs
788  */
789 static void
790 hwc_hash_init()
791 {
792 	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
793 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
794 	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
795 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
796 }
797 
798 /*
799  * Insert a spec into hash table. hwc_hash_lock must be held
800  */
801 static void
802 hwc_hash_insert(struct hwc_spec *spec, char *name, mod_hash_t *hash)
803 {
804 	mod_hash_key_t key;
805 	struct hwc_spec *entry = NULL;
806 
807 	ASSERT(name != NULL);
808 
809 	if (mod_hash_find(hash, (mod_hash_key_t)name,
810 	    (mod_hash_val_t)&entry) != 0) {
811 		/* Name doesn't exist, insert a new key */
812 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
813 		(void) strcpy((char *)key, name);
814 		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
815 			kmem_free(key, strlen(name) + 1);
816 			cmn_err(CE_WARN, "hwc hash state inconsistent");
817 		}
818 		return;
819 	}
820 
821 	/*
822 	 * Name is already present, append spec to the list.
823 	 * This is the case when driver.conf specifies multiple
824 	 * nodes under a single parent or class.
825 	 */
826 	while (entry->hwc_hash_next)
827 		entry = entry->hwc_hash_next;
828 	entry->hwc_hash_next = spec;
829 }
830 
831 /*
832  * Remove a spec entry from spec hash table, the spec itself is
833  * destroyed external to this function.
834  */
835 static void
836 hwc_hash_remove(struct hwc_spec *spec, char *name, mod_hash_t *hash)
837 {
838 	char *key;
839 	struct hwc_spec *entry;
840 
841 	ASSERT(name != NULL);
842 
843 	if (mod_hash_find(hash, (mod_hash_key_t)name,
844 	    (mod_hash_val_t)&entry) != 0) {
845 		return;	/* name not found in hash */
846 	}
847 
848 	/*
849 	 * If the head is the spec to be removed, either destroy the
850 	 * entry or replace it with the remaining list.
851 	 */
852 	if (entry == spec) {
853 		if (spec->hwc_hash_next == NULL) {
854 			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
855 			return;
856 		}
857 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
858 		(void) strcpy(key, name);
859 		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
860 		    (mod_hash_val_t)spec->hwc_hash_next);
861 		spec->hwc_hash_next = NULL;
862 		return;
863 	}
864 
865 	/*
866 	 * If the head is not the one, look for the spec in the
867 	 * hwc_hash_next linkage.
868 	 */
869 	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
870 		entry = entry->hwc_hash_next;
871 
872 	if (entry->hwc_hash_next) {
873 		entry->hwc_hash_next = spec->hwc_hash_next;
874 		spec->hwc_hash_next = NULL;
875 	}
876 }
877 
878 /*
879  * Hash a list of specs based on either parent name or class name
880  */
881 static void
882 hwc_hash(struct hwc_spec *spec_list, major_t major)
883 {
884 	struct hwc_spec *spec = spec_list;
885 
886 	mutex_enter(&hwc_hash_lock);
887 	while (spec) {
888 		/* Put driver major here so parent can find it */
889 		spec->hwc_major = major;
890 
891 		if (spec->hwc_parent_name != NULL) {
892 			hwc_hash_insert(spec, spec->hwc_parent_name,
893 			    hwc_par_hash);
894 		} else if (spec->hwc_class_name != NULL) {
895 			hwc_hash_insert(spec, spec->hwc_class_name,
896 			    hwc_class_hash);
897 		} else {
898 			cmn_err(CE_WARN,
899 			    "hwc_hash: No class or parent specified");
900 		}
901 		spec = spec->hwc_next;
902 	}
903 	mutex_exit(&hwc_hash_lock);
904 }
905 
906 /*
907  * Remove a list of specs from hash tables. Don't destroy the specs yet.
908  */
909 static void
910 hwc_unhash(struct hwc_spec *spec_list)
911 {
912 	struct hwc_spec *spec = spec_list;
913 
914 	mutex_enter(&hwc_hash_lock);
915 	while (spec) {
916 		if (spec->hwc_parent_name != NULL) {
917 			hwc_hash_remove(spec, spec->hwc_parent_name,
918 			    hwc_par_hash);
919 		} else if (spec->hwc_class_name != NULL) {
920 			hwc_hash_remove(spec, spec->hwc_class_name,
921 			    hwc_class_hash);
922 		} else {
923 			cmn_err(CE_WARN,
924 			    "hwc_unhash: No class or parent specified");
925 		}
926 		spec = spec->hwc_next;
927 	}
928 	mutex_exit(&hwc_hash_lock);
929 }
930 
931 /*
932  * Make a copy of specs in a hash entry and add to the end of listp.
933  * Called by nexus to locate a list of child specs.
934  *
935  * entry is a list of hwc_spec chained together with hwc_hash_next.
936  * listp points to list chained together with hwc_next.
937  */
938 static void
939 hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
940     major_t match_major)
941 {
942 	/* Find the tail of the list */
943 	while (*listp)
944 		listp = &(*listp)->hwc_next;
945 
946 	while (entry) {
947 		struct hwc_spec *spec;
948 
949 		if ((match_major != (major_t)-1) &&
950 		    (match_major != entry->hwc_major)) {
951 			entry = entry->hwc_hash_next;
952 			continue;
953 		}
954 
955 		/*
956 		 * Allocate spec and copy the content of entry.
957 		 * No need to copy class/parent name since caller
958 		 * already knows the parent dip.
959 		 */
960 		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
961 		spec->hwc_devi_name = i_ddi_strdup(
962 		    entry->hwc_devi_name, KM_SLEEP);
963 		spec->hwc_major = entry->hwc_major;
964 		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
965 		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);
966 
967 		*listp = spec;
968 		listp = &spec->hwc_next;
969 		entry = entry->hwc_hash_next;
970 	}
971 }
972 
973 /*
974  * Given a dip, find the list of child .conf specs from most specific
975  * (parent pathname) to least specific (class name).
976  *
977  * This function allows top-down loading to be implemented without
978  * changing the format of driver.conf file.
979  */
980 struct hwc_spec *
981 hwc_get_child_spec(dev_info_t *dip, major_t match_major)
982 {
983 	extern char *i_ddi_parname(dev_info_t *, char *);
984 	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
985 	extern void i_ddi_free_exported_classes(char **, int);
986 
987 	int i, nclass;
988 	char **classes;
989 	struct hwc_spec *list = NULL;
990 	mod_hash_val_t val;
991 	char *parname, *parname_buf;
992 	char *deviname, *deviname_buf;
993 	char *pathname, *pathname_buf;
994 	char *bindname;
995 	char *drvname;
996 
997 	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
998 	deviname_buf = pathname_buf + MAXPATHLEN;
999 	parname_buf = pathname_buf + (2 * MAXPATHLEN);
1000 
1001 	mutex_enter(&hwc_hash_lock);
1002 
1003 	/*
1004 	 * Lookup based on full path.
1005 	 * In the case of root node, ddi_pathname would return
1006 	 * null string so just skip calling it.
1007 	 * As the pathname always begins with /, no simpler
1008 	 * name can duplicate it.
1009 	 */
1010 	pathname = (dip == ddi_root_node()) ? "/" :
1011 	    ddi_pathname(dip, pathname_buf);
1012 	ASSERT(pathname != NULL);
1013 	ASSERT(*pathname == '/');
1014 
1015 	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
1016 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1017 	}
1018 
1019 	/*
1020 	 * Lookup nodename@address.
1021 	 * Note deviname cannot match pathname.
1022 	 */
1023 	deviname = ddi_deviname(dip, deviname_buf);
1024 	if (*deviname != '\0') {
1025 		/*
1026 		 * Skip leading / returned by ddi_deviname.
1027 		 */
1028 		ASSERT(*deviname == '/');
1029 		deviname++;
1030 		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
1031 		    (mod_hash_key_t)deviname, &val) == 0))
1032 			hwc_spec_add(&list,
1033 			    (struct hwc_spec *)val, match_major);
1034 	}
1035 
1036 	/*
1037 	 * Lookup bindingname@address.
1038 	 * Take care not to perform duplicate lookups.
1039 	 */
1040 	parname = i_ddi_parname(dip, parname_buf);
1041 	if (*parname != '\0') {
1042 		ASSERT(*parname != '/');
1043 		if ((strcmp(parname, deviname) != 0) &&
1044 		    (mod_hash_find(hwc_par_hash,
1045 		    (mod_hash_key_t)parname, &val) == 0)) {
1046 			hwc_spec_add(&list,
1047 			    (struct hwc_spec *)val, match_major);
1048 		}
1049 	}
1050 
1051 	/*
1052 	 * Lookup driver binding name
1053 	 */
1054 	bindname = ddi_binding_name(dip);
1055 	ASSERT(*bindname != '/');
1056 	if ((strcmp(bindname, parname) != 0) &&
1057 	    (strcmp(bindname, deviname) != 0) &&
1058 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
1059 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1060 
1061 	/*
1062 	 * Lookup driver name
1063 	 */
1064 	drvname = (char *)ddi_driver_name(dip);
1065 	ASSERT(*drvname != '/');
1066 	if ((strcmp(drvname, bindname) != 0) &&
1067 	    (strcmp(drvname, parname) != 0) &&
1068 	    (strcmp(drvname, deviname) != 0) &&
1069 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
1070 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1071 
1072 	kmem_free(pathname_buf, 3 * MAXPATHLEN);
1073 
1074 	/*
1075 	 * Lookup classes exported by this node and lookup the
1076 	 * class hash table for all .conf specs
1077 	 */
1078 	nclass = i_ddi_get_exported_classes(dip, &classes);
1079 	for (i = 0; i < nclass; i++) {
1080 		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
1081 		    &val) == 0)
1082 			hwc_spec_add(&list, (struct hwc_spec *)val,
1083 			    match_major);
1084 	}
1085 	i_ddi_free_exported_classes(classes, nclass);
1086 
1087 	mutex_exit(&hwc_hash_lock);
1088 	return (list);
1089 }
1090