xref: /titanic_50/usr/src/uts/common/os/modsubr.c (revision fb60e41df3cdb2164356bc09f05d3b842c1a8365)
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 2008 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 			if (bp->b_num < 0 || bp->b_num >= size) {
707 				cmn_err(CE_WARN,
708 				    "!Couldn't add system call \"%s %d\". "
709 				    "Value out of range (0..%d) in "
710 				    "/etc/name_to_sysnum.",
711 				    bp->b_name, bp->b_num, size - 1);
712 				continue;
713 			}
714 			make_syscallname(bp->b_name, bp->b_num);
715 		}
716 	}
717 }
718 
719 static void
720 make_syscallname(char *name, int sysno)
721 {
722 	char **cp = &syscallnames[sysno];
723 
724 	if (*cp != NULL) {
725 		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
726 		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
727 		    name, sysno, *cp, sysno);
728 		return;
729 	}
730 	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
731 	(void) strcpy(*cp, name);
732 }
733 
734 /*
735  * Given a system call name, get its number.
736  */
737 int
738 mod_getsysnum(char *name)
739 {
740 	struct bind *mbind;
741 
742 	if ((mbind = find_mbind(name, sb_hashtab)) != NULL)
743 		return (mbind->b_num);
744 
745 	return (-1);
746 }
747 
748 /*
749  * Given a system call number, get the system call name.
750  */
751 char *
752 mod_getsysname(int sysnum)
753 {
754 	return (syscallnames[sysnum]);
755 }
756 
757 /*
758  * Find the name of the module containing the specified pc.
759  * Returns the name on success, "<unknown>" on failure.
760  * No mod_lock locking is required because things are never deleted from
761  * the &modules list.
762  */
763 char *
764 mod_containing_pc(caddr_t pc)
765 {
766 	struct modctl	*mcp = &modules;
767 
768 	do {
769 		if (mcp->mod_mp != NULL &&
770 		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
771 			return (mcp->mod_modname);
772 	} while ((mcp = mcp->mod_next) != &modules);
773 	return ("<unknown>");
774 }
775 
776 /*
777  * Hash tables for hwc_spec
778  *
779  * The purpose of these hash tables are to allow the framework to discover
780  * all possible .conf children for a given nexus. There are two hash tables.
781  * One is hashed based on parent name, the on the class name. Each
782  * driver.conf file translates to a list of hwc_spec's. Adding and
783  * removing the entire list is an atomic operation, protected by
784  * the hwc_hash_lock.
785  *
786  * What we get from all the hashing is the function hwc_get_child_spec().
787  */
788 #define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */
789 
790 static mod_hash_t *hwc_par_hash;	/* hash by parent name */
791 static mod_hash_t *hwc_class_hash;	/* hash by class name */
792 static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */
793 
794 /*
795  * Initialize hash tables for parent and class specs
796  */
797 static void
798 hwc_hash_init()
799 {
800 	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
801 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
802 	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
803 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
804 }
805 
806 /*
807  * Insert a spec into hash table. hwc_hash_lock must be held
808  */
809 static void
810 hwc_hash_insert(struct hwc_spec *spec, char *name, mod_hash_t *hash)
811 {
812 	mod_hash_key_t key;
813 	struct hwc_spec *entry = NULL;
814 
815 	ASSERT(name != NULL);
816 
817 	if (mod_hash_find(hash, (mod_hash_key_t)name,
818 	    (mod_hash_val_t)&entry) != 0) {
819 		/* Name doesn't exist, insert a new key */
820 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
821 		(void) strcpy((char *)key, name);
822 		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
823 			kmem_free(key, strlen(name) + 1);
824 			cmn_err(CE_WARN, "hwc hash state inconsistent");
825 		}
826 		return;
827 	}
828 
829 	/*
830 	 * Name is already present, append spec to the list.
831 	 * This is the case when driver.conf specifies multiple
832 	 * nodes under a single parent or class.
833 	 */
834 	while (entry->hwc_hash_next)
835 		entry = entry->hwc_hash_next;
836 	entry->hwc_hash_next = spec;
837 }
838 
839 /*
840  * Remove a spec entry from spec hash table, the spec itself is
841  * destroyed external to this function.
842  */
843 static void
844 hwc_hash_remove(struct hwc_spec *spec, char *name, mod_hash_t *hash)
845 {
846 	char *key;
847 	struct hwc_spec *entry;
848 
849 	ASSERT(name != NULL);
850 
851 	if (mod_hash_find(hash, (mod_hash_key_t)name,
852 	    (mod_hash_val_t)&entry) != 0) {
853 		return;	/* name not found in hash */
854 	}
855 
856 	/*
857 	 * If the head is the spec to be removed, either destroy the
858 	 * entry or replace it with the remaining list.
859 	 */
860 	if (entry == spec) {
861 		if (spec->hwc_hash_next == NULL) {
862 			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
863 			return;
864 		}
865 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
866 		(void) strcpy(key, name);
867 		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
868 		    (mod_hash_val_t)spec->hwc_hash_next);
869 		spec->hwc_hash_next = NULL;
870 		return;
871 	}
872 
873 	/*
874 	 * If the head is not the one, look for the spec in the
875 	 * hwc_hash_next linkage.
876 	 */
877 	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
878 		entry = entry->hwc_hash_next;
879 
880 	if (entry->hwc_hash_next) {
881 		entry->hwc_hash_next = spec->hwc_hash_next;
882 		spec->hwc_hash_next = NULL;
883 	}
884 }
885 
886 /*
887  * Hash a list of specs based on either parent name or class name
888  */
889 static void
890 hwc_hash(struct hwc_spec *spec_list, major_t major)
891 {
892 	struct hwc_spec *spec = spec_list;
893 
894 	mutex_enter(&hwc_hash_lock);
895 	while (spec) {
896 		/* Put driver major here so parent can find it */
897 		spec->hwc_major = major;
898 
899 		if (spec->hwc_parent_name != NULL) {
900 			hwc_hash_insert(spec, spec->hwc_parent_name,
901 			    hwc_par_hash);
902 		} else if (spec->hwc_class_name != NULL) {
903 			hwc_hash_insert(spec, spec->hwc_class_name,
904 			    hwc_class_hash);
905 		} else {
906 			cmn_err(CE_WARN,
907 			    "hwc_hash: No class or parent specified");
908 		}
909 		spec = spec->hwc_next;
910 	}
911 	mutex_exit(&hwc_hash_lock);
912 }
913 
914 /*
915  * Remove a list of specs from hash tables. Don't destroy the specs yet.
916  */
917 static void
918 hwc_unhash(struct hwc_spec *spec_list)
919 {
920 	struct hwc_spec *spec = spec_list;
921 
922 	mutex_enter(&hwc_hash_lock);
923 	while (spec) {
924 		if (spec->hwc_parent_name != NULL) {
925 			hwc_hash_remove(spec, spec->hwc_parent_name,
926 			    hwc_par_hash);
927 		} else if (spec->hwc_class_name != NULL) {
928 			hwc_hash_remove(spec, spec->hwc_class_name,
929 			    hwc_class_hash);
930 		} else {
931 			cmn_err(CE_WARN,
932 			    "hwc_unhash: No class or parent specified");
933 		}
934 		spec = spec->hwc_next;
935 	}
936 	mutex_exit(&hwc_hash_lock);
937 }
938 
939 /*
940  * Make a copy of specs in a hash entry and add to the end of listp.
941  * Called by nexus to locate a list of child specs.
942  *
943  * entry is a list of hwc_spec chained together with hwc_hash_next.
944  * listp points to list chained together with hwc_next.
945  */
946 static void
947 hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
948     major_t match_major)
949 {
950 	/* Find the tail of the list */
951 	while (*listp)
952 		listp = &(*listp)->hwc_next;
953 
954 	while (entry) {
955 		struct hwc_spec *spec;
956 
957 		if ((match_major != (major_t)-1) &&
958 		    (match_major != entry->hwc_major)) {
959 			entry = entry->hwc_hash_next;
960 			continue;
961 		}
962 
963 		/*
964 		 * Allocate spec and copy the content of entry.
965 		 * No need to copy class/parent name since caller
966 		 * already knows the parent dip.
967 		 */
968 		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
969 		spec->hwc_devi_name = i_ddi_strdup(
970 		    entry->hwc_devi_name, KM_SLEEP);
971 		spec->hwc_major = entry->hwc_major;
972 		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
973 		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);
974 
975 		*listp = spec;
976 		listp = &spec->hwc_next;
977 		entry = entry->hwc_hash_next;
978 	}
979 }
980 
981 /*
982  * Given a dip, find the list of child .conf specs from most specific
983  * (parent pathname) to least specific (class name).
984  *
985  * This function allows top-down loading to be implemented without
986  * changing the format of driver.conf file.
987  */
988 struct hwc_spec *
989 hwc_get_child_spec(dev_info_t *dip, major_t match_major)
990 {
991 	extern char *i_ddi_parname(dev_info_t *, char *);
992 	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
993 	extern void i_ddi_free_exported_classes(char **, int);
994 
995 	int i, nclass;
996 	char **classes;
997 	struct hwc_spec *list = NULL;
998 	mod_hash_val_t val;
999 	char *parname, *parname_buf;
1000 	char *deviname, *deviname_buf;
1001 	char *pathname, *pathname_buf;
1002 	char *bindname;
1003 	char *drvname;
1004 
1005 	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
1006 	deviname_buf = pathname_buf + MAXPATHLEN;
1007 	parname_buf = pathname_buf + (2 * MAXPATHLEN);
1008 
1009 	mutex_enter(&hwc_hash_lock);
1010 
1011 	/*
1012 	 * Lookup based on full path.
1013 	 * In the case of root node, ddi_pathname would return
1014 	 * null string so just skip calling it.
1015 	 * As the pathname always begins with /, no simpler
1016 	 * name can duplicate it.
1017 	 */
1018 	pathname = (dip == ddi_root_node()) ? "/" :
1019 	    ddi_pathname(dip, pathname_buf);
1020 	ASSERT(pathname != NULL);
1021 	ASSERT(*pathname == '/');
1022 
1023 	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
1024 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1025 	}
1026 
1027 	/*
1028 	 * Lookup nodename@address.
1029 	 * Note deviname cannot match pathname.
1030 	 */
1031 	deviname = ddi_deviname(dip, deviname_buf);
1032 	if (*deviname != '\0') {
1033 		/*
1034 		 * Skip leading / returned by ddi_deviname.
1035 		 */
1036 		ASSERT(*deviname == '/');
1037 		deviname++;
1038 		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
1039 		    (mod_hash_key_t)deviname, &val) == 0))
1040 			hwc_spec_add(&list,
1041 			    (struct hwc_spec *)val, match_major);
1042 	}
1043 
1044 	/*
1045 	 * Lookup bindingname@address.
1046 	 * Take care not to perform duplicate lookups.
1047 	 */
1048 	parname = i_ddi_parname(dip, parname_buf);
1049 	if (*parname != '\0') {
1050 		ASSERT(*parname != '/');
1051 		if ((strcmp(parname, deviname) != 0) &&
1052 		    (mod_hash_find(hwc_par_hash,
1053 		    (mod_hash_key_t)parname, &val) == 0)) {
1054 			hwc_spec_add(&list,
1055 			    (struct hwc_spec *)val, match_major);
1056 		}
1057 	}
1058 
1059 	/*
1060 	 * Lookup driver binding name
1061 	 */
1062 	bindname = ddi_binding_name(dip);
1063 	ASSERT(*bindname != '/');
1064 	if ((strcmp(bindname, parname) != 0) &&
1065 	    (strcmp(bindname, deviname) != 0) &&
1066 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
1067 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1068 
1069 	/*
1070 	 * Lookup driver name
1071 	 */
1072 	drvname = (char *)ddi_driver_name(dip);
1073 	ASSERT(*drvname != '/');
1074 	if ((strcmp(drvname, bindname) != 0) &&
1075 	    (strcmp(drvname, parname) != 0) &&
1076 	    (strcmp(drvname, deviname) != 0) &&
1077 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
1078 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
1079 
1080 	kmem_free(pathname_buf, 3 * MAXPATHLEN);
1081 
1082 	/*
1083 	 * Lookup classes exported by this node and lookup the
1084 	 * class hash table for all .conf specs
1085 	 */
1086 	nclass = i_ddi_get_exported_classes(dip, &classes);
1087 	for (i = 0; i < nclass; i++) {
1088 		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
1089 		    &val) == 0)
1090 			hwc_spec_add(&list, (struct hwc_spec *)val,
1091 			    match_major);
1092 	}
1093 	i_ddi_free_exported_classes(classes, nclass);
1094 
1095 	mutex_exit(&hwc_hash_lock);
1096 	return (list);
1097 }
1098