xref: /titanic_41/usr/src/uts/common/os/modctl.c (revision 700c902c445eb3882848aaddc19d13638818cfd6)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * modctl system call for loadable module support.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/user.h>
35 #include <sys/systm.h>
36 #include <sys/exec.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/conf.h>
40 #include <sys/time.h>
41 #include <sys/reboot.h>
42 #include <sys/fs/ufs_fsdir.h>
43 #include <sys/kmem.h>
44 #include <sys/sysconf.h>
45 #include <sys/cmn_err.h>
46 #include <sys/ddi.h>
47 #include <sys/sunddi.h>
48 #include <sys/sunndi.h>
49 #include <sys/ndi_impldefs.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/ddi_implfuncs.h>
52 #include <sys/bootconf.h>
53 #include <sys/dc_ki.h>
54 #include <sys/cladm.h>
55 #include <sys/dtrace.h>
56 #include <sys/kdi.h>
57 
58 #include <sys/devpolicy.h>
59 #include <sys/modctl.h>
60 #include <sys/kobj.h>
61 #include <sys/devops.h>
62 #include <sys/autoconf.h>
63 #include <sys/hwconf.h>
64 #include <sys/callb.h>
65 #include <sys/debug.h>
66 #include <sys/cpuvar.h>
67 #include <sys/sysmacros.h>
68 #include <sys/sysevent.h>
69 #include <sys/sysevent_impl.h>
70 #include <sys/instance.h>
71 #include <sys/modhash.h>
72 #include <sys/modhash_impl.h>
73 #include <sys/dacf_impl.h>
74 #include <sys/vfs.h>
75 #include <sys/pathname.h>
76 #include <sys/console.h>
77 #include <sys/policy.h>
78 #include <ipp/ipp_impl.h>
79 #include <sys/fs/dv_node.h>
80 #include <sys/strsubr.h>
81 #include <sys/fs/sdev_node.h>
82 
83 static int		mod_circdep(struct modctl *);
84 static int		modinfo(modid_t, struct modinfo *);
85 
86 static void		mod_uninstall_all(void);
87 static int		mod_getinfo(struct modctl *, struct modinfo *);
88 static struct modctl	*allocate_modp(const char *, const char *);
89 
90 static int		mod_load(struct modctl *, int);
91 static void		mod_unload(struct modctl *);
92 static int		modinstall(struct modctl *);
93 static int		moduninstall(struct modctl *);
94 
95 static struct modctl	*mod_hold_by_name_common(struct modctl *, const char *);
96 static struct modctl	*mod_hold_next_by_id(modid_t);
97 static struct modctl	*mod_hold_loaded_mod(struct modctl *, char *, int *);
98 static struct modctl	*mod_hold_installed_mod(char *, int, int *);
99 
100 static void		mod_release(struct modctl *);
101 static void		mod_make_requisite(struct modctl *, struct modctl *);
102 static int		mod_install_requisites(struct modctl *);
103 static void		check_esc_sequences(char *, char *);
104 static struct modctl	*mod_hold_by_name_requisite(struct modctl *, char *);
105 
106 /*
107  * module loading thread control structure. Calls to kobj_load_module()() are
108  * handled off to a separate thead using this structure.
109  */
110 struct loadmt {
111 	ksema_t		sema;
112 	struct modctl	*mp;
113 	int		usepath;
114 	kthread_t	*owner;
115 	int		retval;
116 };
117 
118 static void	modload_thread(struct loadmt *);
119 
120 kcondvar_t	mod_cv;
121 kcondvar_t	mod_uninstall_cv;	/* Communication between swapper */
122 					/* and the uninstall daemon. */
123 kmutex_t	mod_lock;		/* protects &modules insert linkage, */
124 					/* mod_busy, mod_want, and mod_ref. */
125 					/* blocking operations while holding */
126 					/* mod_lock should be avoided */
127 kmutex_t	mod_uninstall_lock;	/* protects mod_uninstall_cv */
128 kthread_id_t	mod_aul_thread;
129 
130 int		modunload_wait;
131 kmutex_t	modunload_wait_mutex;
132 kcondvar_t	modunload_wait_cv;
133 int		modunload_active_count;
134 int		modunload_disable_count;
135 
136 int	isminiroot;		/* set if running as miniroot */
137 int	modrootloaded;		/* set after root driver and fs are loaded */
138 int	moddebug = 0x0;		/* debug flags for module writers */
139 int	swaploaded;		/* set after swap driver and fs are loaded */
140 int	bop_io_quiesced = 0;	/* set when BOP I/O can no longer be used */
141 int	last_module_id;
142 clock_t	mod_uninstall_interval = 0;
143 int	ddi_modclose_unload = 1;	/* 0 -> just decrement reference */
144 
145 struct devnames *devnamesp;
146 struct devnames orphanlist;
147 
148 krwlock_t	devinfo_tree_lock;	/* obsolete, to be removed */
149 
150 #define	MAJBINDFILE "/etc/name_to_major"
151 #define	SYSBINDFILE "/etc/name_to_sysnum"
152 
153 static char	majbind[] = MAJBINDFILE;
154 static char	sysbind[] = SYSBINDFILE;
155 static uint_t	mod_autounload_key;	/* for module autounload detection */
156 
157 extern int obpdebug;
158 extern int make_mbind(char *, int, char *, struct bind **);
159 
160 #define	DEBUGGER_PRESENT	((boothowto & RB_DEBUG) || (obpdebug != 0))
161 
162 static int minorperm_loaded = 0;
163 
164 
165 
166 void
167 mod_setup(void)
168 {
169 	struct sysent *callp;
170 	int callnum, exectype;
171 	int	num_devs;
172 	int	i;
173 
174 	/*
175 	 * Initialize the list of loaded driver dev_ops.
176 	 * XXX - This must be done before reading the system file so that
177 	 * forceloads of drivers will work.
178 	 */
179 	num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
180 	/*
181 	 * Since read_binding_file is common code, it doesn't enforce that all
182 	 * of the binding file entries have major numbers <= MAXMAJ32.  Thus,
183 	 * ensure that we don't allocate some massive amount of space due to a
184 	 * bad entry.  We can't have major numbers bigger than MAXMAJ32
185 	 * until file system support for larger major numbers exists.
186 	 */
187 
188 	/*
189 	 * Leave space for expansion, but not more than L_MAXMAJ32
190 	 */
191 	devcnt = MIN(num_devs + 30, L_MAXMAJ32);
192 	devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
193 	for (i = 0; i < devcnt; i++)
194 		devopsp[i] = &mod_nodev_ops;
195 
196 	init_devnamesp(devcnt);
197 
198 	/*
199 	 * Sync up with the work that the stand-alone linker has already done.
200 	 */
201 	(void) kobj_sync();
202 
203 	if (boothowto & RB_DEBUG)
204 		kdi_dvec_modavail();
205 
206 	make_aliases(mb_hashtab);
207 
208 	/*
209 	 * Initialize streams device implementation structures.
210 	 */
211 	devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
212 
213 	/*
214 	 * If the cl_bootstrap module is present,
215 	 * we should be configured as a cluster. Loading this module
216 	 * will set "cluster_bootflags" to non-zero.
217 	 */
218 	(void) modload("misc", "cl_bootstrap");
219 
220 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
221 	init_syscallnames(NSYSCALL);
222 
223 	/*
224 	 * Start up dynamic autoconfiguration framework (dacf).
225 	 */
226 	mod_hash_init();
227 	dacf_init();
228 
229 	/*
230 	 * Start up IP policy framework (ipp).
231 	 */
232 	ipp_init();
233 
234 	/*
235 	 * Allocate loadable native system call locks.
236 	 */
237 	for (callnum = 0, callp = sysent; callnum < NSYSCALL;
238 	    callnum++, callp++) {
239 		if (LOADABLE_SYSCALL(callp)) {
240 			if (mod_getsysname(callnum) != NULL) {
241 				callp->sy_lock =
242 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
243 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
244 			} else {
245 				callp->sy_flags &= ~SE_LOADABLE;
246 				callp->sy_callc = nosys;
247 			}
248 #ifdef DEBUG
249 		} else {
250 			/*
251 			 * Do some sanity checks on the sysent table
252 			 */
253 			switch (callp->sy_flags & SE_RVAL_MASK) {
254 			case SE_32RVAL1:
255 				/* only r_val1 returned */
256 			case SE_32RVAL1 | SE_32RVAL2:
257 				/* r_val1 and r_val2 returned */
258 			case SE_64RVAL:
259 				/* 64-bit rval returned */
260 				break;
261 			default:
262 				cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
263 				    callnum, callp->sy_flags);
264 			}
265 #endif
266 		}
267 	}
268 
269 #ifdef _SYSCALL32_IMPL
270 	/*
271 	 * Allocate loadable system call locks for 32-bit compat syscalls
272 	 */
273 	for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
274 	    callnum++, callp++) {
275 		if (LOADABLE_SYSCALL(callp)) {
276 			if (mod_getsysname(callnum) != NULL) {
277 				callp->sy_lock =
278 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
279 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
280 			} else {
281 				callp->sy_flags &= ~SE_LOADABLE;
282 				callp->sy_callc = nosys;
283 			}
284 #ifdef DEBUG
285 		} else {
286 			/*
287 			 * Do some sanity checks on the sysent table
288 			 */
289 			switch (callp->sy_flags & SE_RVAL_MASK) {
290 			case SE_32RVAL1:
291 				/* only r_val1 returned */
292 			case SE_32RVAL1 | SE_32RVAL2:
293 				/* r_val1 and r_val2 returned */
294 			case SE_64RVAL:
295 				/* 64-bit rval returned */
296 				break;
297 			default:
298 				cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
299 				    callnum, callp->sy_flags);
300 				goto skip;
301 			}
302 
303 			/*
304 			 * Cross-check the native and compatibility tables.
305 			 */
306 			if (callp->sy_callc == nosys ||
307 			    sysent[callnum].sy_callc == nosys)
308 				continue;
309 			/*
310 			 * If only one or the other slot is loadable, then
311 			 * there's an error -- they should match!
312 			 */
313 			if ((callp->sy_callc == loadable_syscall) ^
314 			    (sysent[callnum].sy_callc == loadable_syscall)) {
315 				cmn_err(CE_WARN, "sysent[%d] loadable?",
316 				    callnum);
317 			}
318 			/*
319 			 * This is more of a heuristic test -- if the
320 			 * system call returns two values in the 32-bit
321 			 * world, it should probably return two 32-bit
322 			 * values in the 64-bit world too.
323 			 */
324 			if (((callp->sy_flags & SE_32RVAL2) == 0) ^
325 			    ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
326 				cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
327 				    callnum);
328 			}
329 skip:;
330 #endif	/* DEBUG */
331 		}
332 	}
333 #endif	/* _SYSCALL32_IMPL */
334 
335 	/*
336 	 * Allocate loadable exec locks.  (Assumes all execs are loadable)
337 	 */
338 	for (exectype = 0; exectype < nexectype; exectype++) {
339 		execsw[exectype].exec_lock =
340 		    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
341 		rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
342 	}
343 
344 	read_class_file();
345 
346 	/* init thread specific structure for mod_uninstall_all */
347 	tsd_create(&mod_autounload_key, NULL);
348 }
349 
350 static int
351 modctl_modload(int use_path, char *filename, int *rvp)
352 {
353 	struct modctl *modp;
354 	int retval = 0;
355 	char *filenamep;
356 	int modid;
357 
358 	filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
359 
360 	if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
361 		retval = EFAULT;
362 		goto out;
363 	}
364 
365 	filenamep[MOD_MAXPATH - 1] = 0;
366 	modp = mod_hold_installed_mod(filenamep, use_path, &retval);
367 
368 	if (modp == NULL)
369 		goto out;
370 
371 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
372 	modid = modp->mod_id;
373 	mod_release_mod(modp);
374 	CPU_STATS_ADDQ(CPU, sys, modload, 1);
375 	if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
376 		retval = EFAULT;
377 out:
378 	kmem_free(filenamep, MOD_MAXPATH);
379 
380 	return (retval);
381 }
382 
383 static int
384 modctl_modunload(modid_t id)
385 {
386 	int rval = 0;
387 
388 	if (id == 0) {
389 #ifdef DEBUG
390 		/*
391 		 * Turn on mod_uninstall_daemon
392 		 */
393 		if (mod_uninstall_interval == 0) {
394 			mod_uninstall_interval = 60;
395 			modreap();
396 			return (rval);
397 		}
398 #endif
399 		mod_uninstall_all();
400 	} else {
401 		rval = modunload(id);
402 	}
403 	return (rval);
404 }
405 
406 static int
407 modctl_modinfo(modid_t id, struct modinfo *umodi)
408 {
409 	int retval;
410 	struct modinfo modi;
411 #if defined(_SYSCALL32_IMPL)
412 	int nobase;
413 	struct modinfo32 modi32;
414 #endif
415 
416 	if (get_udatamodel() == DATAMODEL_NATIVE) {
417 		if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
418 			return (EFAULT);
419 	}
420 #ifdef _SYSCALL32_IMPL
421 	else {
422 		bzero(&modi, sizeof (modi));
423 		if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
424 			return (EFAULT);
425 		modi.mi_info = modi32.mi_info;
426 		modi.mi_id = modi32.mi_id;
427 		modi.mi_nextid = modi32.mi_nextid;
428 		nobase = modi.mi_info & MI_INFO_NOBASE;
429 	}
430 #endif
431 	/*
432 	 * This flag is -only- for the kernels use.
433 	 */
434 	modi.mi_info &= ~MI_INFO_LINKAGE;
435 
436 	retval = modinfo(id, &modi);
437 	if (retval)
438 		return (retval);
439 
440 	if (get_udatamodel() == DATAMODEL_NATIVE) {
441 		if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
442 			retval = EFAULT;
443 #ifdef _SYSCALL32_IMPL
444 	} else {
445 		int i;
446 
447 		if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
448 			return (EOVERFLOW);
449 
450 		modi32.mi_info = modi.mi_info;
451 		modi32.mi_state = modi.mi_state;
452 		modi32.mi_id = modi.mi_id;
453 		modi32.mi_nextid = modi.mi_nextid;
454 		modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
455 		modi32.mi_size = modi.mi_size;
456 		modi32.mi_rev = modi.mi_rev;
457 		modi32.mi_loadcnt = modi.mi_loadcnt;
458 		bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
459 		for (i = 0; i < MODMAXLINK32; i++) {
460 			modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
461 			bcopy(modi.mi_msinfo[i].msi_linkinfo,
462 			    modi32.mi_msinfo[i].msi_linkinfo,
463 			    sizeof (modi32.mi_msinfo[0].msi_linkinfo));
464 		}
465 		if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
466 			retval = EFAULT;
467 #endif
468 	}
469 
470 	return (retval);
471 }
472 
473 /*
474  * Return the last major number in the range of permissible major numbers.
475  */
476 /*ARGSUSED*/
477 static int
478 modctl_modreserve(modid_t id, int *data)
479 {
480 	if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
481 		return (EFAULT);
482 	return (0);
483 }
484 
485 static int
486 modctl_add_major(int *data)
487 {
488 	struct modconfig mc;
489 	int i, rv;
490 	struct aliases alias;
491 	struct aliases *ap;
492 	char name[MAXMODCONFNAME];
493 	char cname[MAXMODCONFNAME];
494 	char *drvname;
495 
496 	bzero(&mc, sizeof (struct modconfig));
497 	if (get_udatamodel() == DATAMODEL_NATIVE) {
498 		if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
499 			return (EFAULT);
500 	}
501 #ifdef _SYSCALL32_IMPL
502 	else {
503 		struct modconfig32 modc32;
504 
505 		if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
506 			return (EFAULT);
507 		else {
508 			bcopy(modc32.drvname, mc.drvname,
509 			    sizeof (modc32.drvname));
510 			bcopy(modc32.drvclass, mc.drvclass,
511 			    sizeof (modc32.drvclass));
512 			mc.major = modc32.major;
513 			mc.num_aliases = modc32.num_aliases;
514 			mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
515 		}
516 	}
517 #endif
518 
519 	/*
520 	 * If the driver is already in the mb_hashtab, and the name given
521 	 * doesn't match that driver's name, fail.  Otherwise, pass, since
522 	 * we may be adding aliases.
523 	 */
524 	if ((drvname = mod_major_to_name(mc.major)) != NULL &&
525 	    strcmp(drvname, mc.drvname) != 0)
526 		return (EINVAL);
527 
528 	/*
529 	 * Add each supplied driver alias to mb_hashtab
530 	 */
531 	ap = mc.ap;
532 	for (i = 0; i < mc.num_aliases; i++) {
533 		bzero(&alias, sizeof (struct aliases));
534 
535 		if (get_udatamodel() == DATAMODEL_NATIVE) {
536 			if (copyin(ap, &alias, sizeof (struct aliases)) != 0)
537 				return (EFAULT);
538 
539 			if (alias.a_len > MAXMODCONFNAME)
540 				return (EINVAL);
541 
542 			if (copyin(alias.a_name, name, alias.a_len) != 0)
543 				return (EFAULT);
544 
545 			if (name[alias.a_len - 1] != '\0')
546 				return (EINVAL);
547 		}
548 #ifdef _SYSCALL32_IMPL
549 		else {
550 			struct aliases32 al32;
551 
552 			bzero(&al32, sizeof (struct aliases32));
553 			if (copyin(ap, &al32, sizeof (struct aliases32)) != 0)
554 				return (EFAULT);
555 
556 			if (al32.a_len > MAXMODCONFNAME)
557 				return (EINVAL);
558 
559 			if (copyin((void *)(uintptr_t)al32.a_name,
560 			    name, al32.a_len) != 0)
561 				return (EFAULT);
562 
563 			if (name[al32.a_len - 1] != '\0')
564 				return (EINVAL);
565 
566 			alias.a_next = (void *)(uintptr_t)al32.a_next;
567 		}
568 #endif
569 		check_esc_sequences(name, cname);
570 		(void) make_mbind(cname, mc.major, NULL, mb_hashtab);
571 		ap = alias.a_next;
572 	}
573 
574 	/*
575 	 * Try to establish an mbinding for mc.drvname, and add it to devnames.
576 	 * Add class if any after establishing the major number
577 	 */
578 	(void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
579 	rv = make_devname(mc.drvname, mc.major);
580 
581 	if (rv == 0) {
582 		if (mc.drvclass[0] != '\0')
583 			add_class(mc.drvname, mc.drvclass);
584 		(void) i_ddi_load_drvconf(mc.major);
585 		i_ddi_bind_devs();
586 		i_ddi_di_cache_invalidate(KM_SLEEP);
587 	}
588 	return (rv);
589 }
590 
591 static int
592 modctl_rem_major(major_t major)
593 {
594 	struct devnames *dnp;
595 
596 	if (major >= devcnt)
597 		return (EINVAL);
598 
599 	/* mark devnames as removed */
600 	dnp = &devnamesp[major];
601 	LOCK_DEV_OPS(&dnp->dn_lock);
602 	if (dnp->dn_name == NULL ||
603 	    (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
604 		UNLOCK_DEV_OPS(&dnp->dn_lock);
605 		return (EINVAL);
606 	}
607 	dnp->dn_flags |= DN_DRIVER_REMOVED;
608 	pm_driver_removed(major);
609 	UNLOCK_DEV_OPS(&dnp->dn_lock);
610 
611 	(void) i_ddi_unload_drvconf(major);
612 	i_ddi_unbind_devs(major);
613 	i_ddi_di_cache_invalidate(KM_SLEEP);
614 	return (0);
615 }
616 
617 static struct vfs *
618 path_to_vfs(char *name)
619 {
620 	vnode_t *vp;
621 	struct vfs *vfsp;
622 
623 	if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
624 		return (NULL);
625 
626 	vfsp = vp->v_vfsp;
627 	VN_RELE(vp);
628 	return (vfsp);
629 }
630 
631 static int
632 new_vfs_in_modpath()
633 {
634 	static int n_modpath = 0;
635 	static char *modpath_copy;
636 	static struct pathvfs {
637 		char *path;
638 		struct vfs *vfsp;
639 	} *pathvfs;
640 
641 	int i, new_vfs = 0;
642 	char *tmp, *tmp1;
643 	struct vfs *vfsp;
644 
645 	if (n_modpath != 0) {
646 		for (i = 0; i < n_modpath; i++) {
647 			vfsp = path_to_vfs(pathvfs[i].path);
648 			if (vfsp != pathvfs[i].vfsp) {
649 				pathvfs[i].vfsp = vfsp;
650 				if (vfsp)
651 					new_vfs = 1;
652 			}
653 		}
654 		return (new_vfs);
655 	}
656 
657 	/*
658 	 * First call, initialize the pathvfs structure
659 	 */
660 	modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
661 	tmp = modpath_copy;
662 	n_modpath = 1;
663 	tmp1 = strchr(tmp, ' ');
664 	while (tmp1) {
665 		*tmp1 = '\0';
666 		n_modpath++;
667 		tmp = tmp1 + 1;
668 		tmp1 = strchr(tmp, ' ');
669 	}
670 
671 	pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
672 	tmp = modpath_copy;
673 	for (i = 0; i < n_modpath; i++) {
674 		pathvfs[i].path = tmp;
675 		vfsp = path_to_vfs(tmp);
676 		pathvfs[i].vfsp = vfsp;
677 		tmp += strlen(tmp) + 1;
678 	}
679 	return (1);	/* always reread driver.conf the first time */
680 }
681 
682 static int
683 modctl_load_drvconf(major_t major)
684 {
685 	int ret;
686 
687 	if (major != (major_t)-1) {
688 		ret = i_ddi_load_drvconf(major);
689 		if (ret == 0)
690 			i_ddi_bind_devs();
691 		return (ret);
692 	}
693 
694 	/*
695 	 * We are invoked to rescan new driver.conf files. It is
696 	 * only necessary if a new file system was mounted in the
697 	 * module_path. Because rescanning driver.conf files can
698 	 * take some time on older platforms (sun4m), the following
699 	 * code skips unnecessary driver.conf rescans to optimize
700 	 * boot performance.
701 	 */
702 	if (new_vfs_in_modpath()) {
703 		(void) i_ddi_load_drvconf((major_t)-1);
704 		/*
705 		 * If we are still initializing io subsystem,
706 		 * load drivers with ddi-forceattach property
707 		 */
708 		if (!i_ddi_io_initialized())
709 			i_ddi_forceattach_drivers();
710 	}
711 	return (0);
712 }
713 
714 static int
715 modctl_unload_drvconf(major_t major)
716 {
717 	int ret;
718 
719 	if (major >= devcnt)
720 		return (EINVAL);
721 
722 	ret = i_ddi_unload_drvconf(major);
723 	if (ret != 0)
724 		return (ret);
725 	(void) i_ddi_unbind_devs(major);
726 
727 	return (0);
728 }
729 
730 static void
731 check_esc_sequences(char *str, char *cstr)
732 {
733 	int i;
734 	size_t len;
735 	char *p;
736 
737 	len = strlen(str);
738 	for (i = 0; i < len; i++, str++, cstr++) {
739 		if (*str != '\\') {
740 			*cstr = *str;
741 		} else {
742 			p = str + 1;
743 			/*
744 			 * we only handle octal escape sequences for SPACE
745 			 */
746 			if (*p++ == '0' && *p++ == '4' && *p == '0') {
747 				*cstr = ' ';
748 				str += 3;
749 			} else {
750 				*cstr = *str;
751 			}
752 		}
753 	}
754 	*cstr = 0;
755 }
756 
757 static int
758 modctl_getmodpathlen(int *data)
759 {
760 	int len;
761 	len = strlen(default_path);
762 	if (copyout(&len, data, sizeof (len)) != 0)
763 		return (EFAULT);
764 	return (0);
765 }
766 
767 static int
768 modctl_getmodpath(char *data)
769 {
770 	if (copyout(default_path, data, strlen(default_path) + 1) != 0)
771 		return (EFAULT);
772 	return (0);
773 }
774 
775 static int
776 modctl_read_sysbinding_file(void)
777 {
778 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
779 	return (0);
780 }
781 
782 static int
783 modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
784 {
785 	char name[256];
786 	int retval;
787 	major_t major;
788 
789 	if (ulen == 0)
790 		return (EINVAL);
791 	if ((retval = copyinstr(uname, name,
792 	    (ulen < 256) ? ulen : 256, 0)) != 0)
793 		return (retval);
794 	if ((major = mod_name_to_major(name)) == (major_t)-1)
795 		return (ENODEV);
796 	if (copyout(&major, umajorp, sizeof (major_t)) != 0)
797 		return (EFAULT);
798 	return (0);
799 }
800 
801 static int
802 modctl_getname(char *uname, uint_t ulen, int *umajorp)
803 {
804 	char *name;
805 	major_t major;
806 
807 	if (copyin(umajorp, &major, sizeof (major)) != 0)
808 		return (EFAULT);
809 	if ((name = mod_major_to_name(major)) == NULL)
810 		return (ENODEV);
811 	if ((strlen(name) + 1) > ulen)
812 		return (ENOSPC);
813 	return (copyoutstr(name, uname, ulen, NULL));
814 }
815 
816 static int
817 modctl_devt2instance(dev_t dev, int *uinstancep)
818 {
819 	int	instance;
820 
821 	if ((instance = dev_to_instance(dev)) == -1)
822 		return (EINVAL);
823 
824 	return (copyout(&instance, uinstancep, sizeof (int)));
825 }
826 
827 /*
828  * Return the sizeof of the device id.
829  */
830 static int
831 modctl_sizeof_devid(dev_t dev, uint_t *len)
832 {
833 	uint_t		sz;
834 	ddi_devid_t	devid;
835 
836 	/* get device id */
837 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
838 		return (EINVAL);
839 
840 	sz = ddi_devid_sizeof(devid);
841 	ddi_devid_free(devid);
842 
843 	/* copyout device id size */
844 	if (copyout(&sz, len, sizeof (sz)) != 0)
845 		return (EFAULT);
846 
847 	return (0);
848 }
849 
850 /*
851  * Return a copy of the device id.
852  */
853 static int
854 modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
855 {
856 	uint_t		sz;
857 	ddi_devid_t	devid;
858 	int		err = 0;
859 
860 	/* get device id */
861 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
862 		return (EINVAL);
863 
864 	sz = ddi_devid_sizeof(devid);
865 
866 	/* Error if device id is larger than space allocated */
867 	if (sz > len) {
868 		ddi_devid_free(devid);
869 		return (ENOSPC);
870 	}
871 
872 	/* copy out device id */
873 	if (copyout(devid, udevid, sz) != 0)
874 		err = EFAULT;
875 	ddi_devid_free(devid);
876 	return (err);
877 }
878 
879 /*
880  * return the /devices paths associated with the specified devid and
881  * minor name.
882  */
883 /*ARGSUSED*/
884 static int
885 modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
886 	size_t *ulensp, char *upaths)
887 {
888 	ddi_devid_t	devid = NULL;
889 	int		devid_len;
890 	char		*minor_name = NULL;
891 	dev_info_t	*dip = NULL;
892 	struct ddi_minor_data   *dmdp;
893 	char		*path = NULL;
894 	int		ulens;
895 	int		lens;
896 	int		len;
897 	dev_t		*devlist = NULL;
898 	int		ndevs;
899 	int		i;
900 	int		ret = 0;
901 
902 	/*
903 	 * If upaths is NULL then we are only computing the amount of space
904 	 * needed to hold the paths and returning the value in *ulensp. If we
905 	 * are copying out paths then we get the amount of space allocated by
906 	 * the caller. If the actual space needed for paths is larger, or
907 	 * things are changing out from under us, then we return EAGAIN.
908 	 */
909 	if (upaths) {
910 		if (ulensp == NULL)
911 			return (EINVAL);
912 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
913 			return (EFAULT);
914 	}
915 
916 	/*
917 	 * copyin enough of the devid to determine the length then
918 	 * reallocate and copy in the entire devid.
919 	 */
920 	devid_len = ddi_devid_sizeof(NULL);
921 	devid = kmem_alloc(devid_len, KM_SLEEP);
922 	if (copyin(udevid, devid, devid_len)) {
923 		ret = EFAULT;
924 		goto out;
925 	}
926 	len = devid_len;
927 	devid_len = ddi_devid_sizeof(devid);
928 	kmem_free(devid, len);
929 	devid = kmem_alloc(devid_len, KM_SLEEP);
930 	if (copyin(udevid, devid, devid_len)) {
931 		ret = EFAULT;
932 		goto out;
933 	}
934 
935 	/* copyin the minor name if specified. */
936 	minor_name = uminor_name;
937 	if ((minor_name != DEVID_MINOR_NAME_ALL) &&
938 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
939 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
940 		minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
941 		if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
942 			ret = EFAULT;
943 			goto out;
944 		}
945 	}
946 
947 	/*
948 	 * Use existing function to resolve the devid into a devlist.
949 	 *
950 	 * NOTE: there is a loss of spectype information in the current
951 	 * ddi_lyr_devid_to_devlist implementation. We work around this by not
952 	 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
953 	 * node forms in the loop processing the devlist below. It would be
954 	 * best if at some point the use of this interface here was replaced
955 	 * with a path oriented call.
956 	 */
957 	if (ddi_lyr_devid_to_devlist(devid,
958 	    (minor_name == DEVID_MINOR_NAME_ALL) ?
959 	    DEVID_MINOR_NAME_ALL_CHR : minor_name,
960 	    &ndevs, &devlist) != DDI_SUCCESS) {
961 		ret = EINVAL;
962 		goto out;
963 	}
964 
965 	/*
966 	 * loop over the devlist, converting each devt to a path and doing
967 	 * a copyout of the path and computation of the amount of space
968 	 * needed to hold all the paths
969 	 */
970 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
971 	for (i = 0, lens = 0; i < ndevs; i++) {
972 
973 		/* find the dip associated with the dev_t */
974 		if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
975 			continue;
976 
977 		/* loop over all the minor nodes, skipping ones we don't want */
978 		for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
979 			if ((dmdp->ddm_dev != devlist[i]) ||
980 			    (dmdp->type != DDM_MINOR))
981 				continue;
982 
983 			if ((minor_name != DEVID_MINOR_NAME_ALL) &&
984 			    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
985 			    (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
986 			    strcmp(minor_name, dmdp->ddm_name))
987 				continue;
988 			else {
989 				if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
990 				    (dmdp->ddm_spec_type != S_IFCHR))
991 					continue;
992 				if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
993 				    (dmdp->ddm_spec_type != S_IFBLK))
994 					continue;
995 			}
996 
997 			/* XXX need ddi_pathname_minor(dmdp, path); interface */
998 			if (ddi_dev_pathname(dmdp->ddm_dev, dmdp->ddm_spec_type,
999 			    path) != DDI_SUCCESS) {
1000 				ret = EAGAIN;
1001 				goto out;
1002 			}
1003 			len = strlen(path) + 1;
1004 			*(path + len) = '\0';	/* set double termination */
1005 			lens += len;
1006 
1007 			/* copyout the path with double terminations */
1008 			if (upaths) {
1009 				if (lens > ulens) {
1010 					ret = EAGAIN;
1011 					goto out;
1012 				}
1013 				if (copyout(path, upaths, len + 1)) {
1014 					ret = EFAULT;
1015 					goto out;
1016 				}
1017 				upaths += len;
1018 			}
1019 		}
1020 		ddi_release_devi(dip);
1021 		dip = NULL;
1022 	}
1023 	lens++;		/* add one for double termination */
1024 
1025 	/* copy out the amount of space needed to hold the paths */
1026 	if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
1027 		ret = EFAULT;
1028 		goto out;
1029 	}
1030 	ret = 0;
1031 
1032 out:	if (dip)
1033 		ddi_release_devi(dip);
1034 	if (path)
1035 		kmem_free(path, MAXPATHLEN);
1036 	if (devlist)
1037 		ddi_lyr_free_devlist(devlist, ndevs);
1038 	if (minor_name &&
1039 	    (minor_name != DEVID_MINOR_NAME_ALL) &&
1040 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
1041 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK))
1042 		kmem_free(minor_name, MAXPATHLEN);
1043 	if (devid)
1044 		kmem_free(devid, devid_len);
1045 	return (ret);
1046 }
1047 
1048 /*
1049  * Return the size of the minor name.
1050  */
1051 static int
1052 modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
1053 {
1054 	uint_t	sz;
1055 	char	*name;
1056 
1057 	/* get the minor name */
1058 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1059 		return (EINVAL);
1060 
1061 	sz = strlen(name) + 1;
1062 	kmem_free(name, sz);
1063 
1064 	/* copy out the size of the minor name */
1065 	if (copyout(&sz, len, sizeof (sz)) != 0)
1066 		return (EFAULT);
1067 
1068 	return (0);
1069 }
1070 
1071 /*
1072  * Return the minor name.
1073  */
1074 static int
1075 modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
1076 {
1077 	uint_t	sz;
1078 	char	*name;
1079 	int	err = 0;
1080 
1081 	/* get the minor name */
1082 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1083 		return (EINVAL);
1084 
1085 	sz = strlen(name) + 1;
1086 
1087 	/* Error if the minor name is larger than the space allocated */
1088 	if (sz > len) {
1089 		kmem_free(name, sz);
1090 		return (ENOSPC);
1091 	}
1092 
1093 	/* copy out the minor name */
1094 	if (copyout(name, uname, sz) != 0)
1095 		err = EFAULT;
1096 	kmem_free(name, sz);
1097 	return (err);
1098 }
1099 
1100 /*
1101  * Return the size of the devfspath name.
1102  */
1103 static int
1104 modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
1105 {
1106 	uint_t	sz;
1107 	char	*name;
1108 
1109 	/* get the path name */
1110 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1111 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
1112 		kmem_free(name, MAXPATHLEN);
1113 		return (EINVAL);
1114 	}
1115 
1116 	sz = strlen(name) + 1;
1117 	kmem_free(name, MAXPATHLEN);
1118 
1119 	/* copy out the size of the path name */
1120 	if (copyout(&sz, len, sizeof (sz)) != 0)
1121 		return (EFAULT);
1122 
1123 	return (0);
1124 }
1125 
1126 /*
1127  * Return the devfspath name.
1128  */
1129 static int
1130 modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
1131 {
1132 	uint_t	sz;
1133 	char	*name;
1134 	int	err = 0;
1135 
1136 	/* get the path name */
1137 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1138 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
1139 		kmem_free(name, MAXPATHLEN);
1140 		return (EINVAL);
1141 	}
1142 
1143 	sz = strlen(name) + 1;
1144 
1145 	/* Error if the path name is larger than the space allocated */
1146 	if (sz > len) {
1147 		kmem_free(name, MAXPATHLEN);
1148 		return (ENOSPC);
1149 	}
1150 
1151 	/* copy out the path name */
1152 	if (copyout(name, uname, sz) != 0)
1153 		err = EFAULT;
1154 	kmem_free(name, MAXPATHLEN);
1155 	return (err);
1156 }
1157 
1158 static int
1159 modctl_get_fbname(char *path)
1160 {
1161 	extern dev_t fbdev;
1162 	char *pathname = NULL;
1163 	int rval = 0;
1164 
1165 	/* make sure fbdev is set before we plunge in */
1166 	if (fbdev == NODEV)
1167 		return (ENODEV);
1168 
1169 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1170 	if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
1171 	    pathname)) == DDI_SUCCESS) {
1172 		if (copyout(pathname, path, strlen(pathname)+1) != 0) {
1173 			rval = EFAULT;
1174 		}
1175 	}
1176 	kmem_free(pathname, MAXPATHLEN);
1177 	return (rval);
1178 }
1179 
1180 /*
1181  * modctl_reread_dacf()
1182  *	Reread the dacf rules database from the named binding file.
1183  *	If NULL is specified, pass along the NULL, it means 'use the default'.
1184  */
1185 static int
1186 modctl_reread_dacf(char *path)
1187 {
1188 	int rval = 0;
1189 	char *filename, *filenamep;
1190 
1191 	filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1192 
1193 	if (path == NULL) {
1194 		filenamep = NULL;
1195 	} else {
1196 		if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
1197 			rval = EFAULT;
1198 			goto out;
1199 		}
1200 		filenamep = filename;
1201 		filenamep[MAXPATHLEN - 1] = '\0';
1202 	}
1203 
1204 	rval = read_dacf_binding_file(filenamep);
1205 out:
1206 	kmem_free(filename, MAXPATHLEN);
1207 	return (rval);
1208 }
1209 
1210 /*ARGSUSED*/
1211 static int
1212 modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
1213     uint_t flag)
1214 {
1215 	int error = 0;
1216 	char *filenamep;
1217 
1218 	switch (subcmd) {
1219 
1220 	case MODEVENTS_FLUSH:
1221 		/* flush all currently queued events */
1222 		log_sysevent_flushq(subcmd, flag);
1223 		break;
1224 
1225 	case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
1226 		/*
1227 		 * bind door_upcall to filename
1228 		 * this should only be done once per invocation
1229 		 * of the event daemon.
1230 		 */
1231 
1232 		filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
1233 
1234 		if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
1235 			error = EFAULT;
1236 		} else {
1237 			error = log_sysevent_filename(filenamep);
1238 		}
1239 		kmem_free(filenamep, MOD_MAXPATH);
1240 		break;
1241 
1242 	case MODEVENTS_GETDATA:
1243 		error = log_sysevent_copyout_data((sysevent_id_t *)a2,
1244 		    (size_t)a3, (caddr_t)a4);
1245 		break;
1246 
1247 	case MODEVENTS_FREEDATA:
1248 		error = log_sysevent_free_data((sysevent_id_t *)a2);
1249 		break;
1250 	case MODEVENTS_POST_EVENT:
1251 		error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
1252 			(sysevent_id_t *)a4);
1253 		break;
1254 	case MODEVENTS_REGISTER_EVENT:
1255 		error = log_sysevent_register((char *)a2, (char *)a3,
1256 		    (se_pubsub_t *)a4);
1257 		break;
1258 	default:
1259 		error = EINVAL;
1260 	}
1261 
1262 	return (error);
1263 }
1264 
1265 static void
1266 free_mperm(mperm_t *mp)
1267 {
1268 	int len;
1269 
1270 	if (mp->mp_minorname) {
1271 		len = strlen(mp->mp_minorname) + 1;
1272 		kmem_free(mp->mp_minorname, len);
1273 	}
1274 	kmem_free(mp, sizeof (mperm_t));
1275 }
1276 
1277 #define	MP_NO_DRV_ERR	\
1278 	"/etc/minor_perm: no driver for %s\n"
1279 
1280 #define	MP_EMPTY_MINOR	\
1281 	"/etc/minor_perm: empty minor name for driver %s\n"
1282 
1283 #define	MP_NO_MINOR	\
1284 	"/etc/minor_perm: no minor matching %s for driver %s\n"
1285 
1286 /*
1287  * Remove mperm entry with matching minorname
1288  */
1289 static void
1290 rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1291 {
1292 	mperm_t **mp_head;
1293 	mperm_t *freemp = NULL;
1294 	struct devnames *dnp = &devnamesp[major];
1295 	mperm_t **wildmp;
1296 
1297 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
1298 
1299 	LOCK_DEV_OPS(&dnp->dn_lock);
1300 	if (strcmp(mp->mp_minorname, "*") == 0) {
1301 		wildmp = ((is_clone == 0) ?
1302 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
1303 		if (*wildmp)
1304 			freemp = *wildmp;
1305 		*wildmp = NULL;
1306 	} else {
1307 		mp_head = &dnp->dn_mperm;
1308 		while (*mp_head) {
1309 			if (strcmp((*mp_head)->mp_minorname,
1310 			    mp->mp_minorname) != 0) {
1311 				mp_head = &(*mp_head)->mp_next;
1312 				continue;
1313 			}
1314 			/* remove the entry */
1315 			freemp = *mp_head;
1316 			*mp_head = freemp->mp_next;
1317 			break;
1318 		}
1319 	}
1320 	if (freemp) {
1321 		if (moddebug & MODDEBUG_MINORPERM) {
1322 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
1323 			    drvname, freemp->mp_minorname,
1324 			    freemp->mp_mode & 0777,
1325 			    freemp->mp_uid, freemp->mp_gid);
1326 		}
1327 		free_mperm(freemp);
1328 	} else {
1329 		if (moddebug & MODDEBUG_MINORPERM) {
1330 			cmn_err(CE_CONT, MP_NO_MINOR,
1331 				drvname, mp->mp_minorname);
1332 		}
1333 	}
1334 
1335 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1336 }
1337 
1338 /*
1339  * Add minor perm entry
1340  */
1341 static void
1342 add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1343 {
1344 	mperm_t **mp_head;
1345 	mperm_t *freemp = NULL;
1346 	struct devnames *dnp = &devnamesp[major];
1347 	mperm_t **wildmp;
1348 
1349 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
1350 
1351 	/*
1352 	 * Note that update_drv replace semantics require
1353 	 * replacing matching entries with the new permissions.
1354 	 */
1355 	LOCK_DEV_OPS(&dnp->dn_lock);
1356 	if (strcmp(mp->mp_minorname, "*") == 0) {
1357 		wildmp = ((is_clone == 0) ?
1358 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
1359 		if (*wildmp)
1360 			freemp = *wildmp;
1361 		*wildmp = mp;
1362 	} else {
1363 		mperm_t *p, *v = NULL;
1364 		for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
1365 			if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
1366 				if (v == NULL)
1367 					dnp->dn_mperm = mp;
1368 				else
1369 					v->mp_next = mp;
1370 				mp->mp_next = p->mp_next;
1371 				freemp = p;
1372 				goto replaced;
1373 			}
1374 		}
1375 		if (p == NULL) {
1376 			mp_head = &dnp->dn_mperm;
1377 			if (*mp_head == NULL) {
1378 				*mp_head = mp;
1379 			} else {
1380 				mp->mp_next = *mp_head;
1381 				*mp_head = mp;
1382 			}
1383 		}
1384 	}
1385 replaced:
1386 	if (freemp) {
1387 		if (moddebug & MODDEBUG_MINORPERM) {
1388 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
1389 			    drvname, freemp->mp_minorname,
1390 			    freemp->mp_mode & 0777,
1391 			    freemp->mp_uid, freemp->mp_gid);
1392 		}
1393 		free_mperm(freemp);
1394 	}
1395 	if (moddebug & MODDEBUG_MINORPERM) {
1396 		cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
1397 		    drvname, mp->mp_minorname, mp->mp_mode & 0777,
1398 		    mp->mp_uid, mp->mp_gid);
1399 	}
1400 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1401 }
1402 
1403 
1404 static int
1405 process_minorperm(int cmd, nvlist_t *nvl)
1406 {
1407 	char *minor;
1408 	major_t major;
1409 	mperm_t *mp;
1410 	nvpair_t *nvp;
1411 	char *name;
1412 	int is_clone;
1413 	major_t minmaj;
1414 
1415 	ASSERT(cmd == MODLOADMINORPERM ||
1416 	    cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
1417 
1418 	nvp = NULL;
1419 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1420 		name = nvpair_name(nvp);
1421 
1422 		is_clone = 0;
1423 		(void) nvpair_value_string(nvp, &minor);
1424 		major = ddi_name_to_major(name);
1425 		if (major != (major_t)-1) {
1426 			mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
1427 			if (minor == NULL || strlen(minor) == 0) {
1428 				if (moddebug & MODDEBUG_MINORPERM) {
1429 					cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
1430 				}
1431 				minor = "*";
1432 			}
1433 
1434 			/*
1435 			 * The minor name of a node using the clone
1436 			 * driver must be the driver name.  To avoid
1437 			 * multiple searches, we map entries in the form
1438 			 * clone:<driver> to <driver>:*.  This also allows us
1439 			 * to filter out some of the litter in /etc/minor_perm.
1440 			 * Minor perm alias entries where the name is not
1441 			 * the driver kept on the clone list itself.
1442 			 * This all seems very fragile as a driver could
1443 			 * be introduced with an existing alias name.
1444 			 */
1445 			if (strcmp(name, "clone") == 0) {
1446 				minmaj = ddi_name_to_major(minor);
1447 				if (minmaj != (major_t)-1) {
1448 					if (moddebug & MODDEBUG_MINORPERM) {
1449 						cmn_err(CE_CONT,
1450 						    "mapping %s:%s to %s:*\n",
1451 						    name, minor, minor);
1452 					}
1453 					major = minmaj;
1454 					name = minor;
1455 					minor = "*";
1456 					is_clone = 1;
1457 				}
1458 			}
1459 
1460 			if (mp) {
1461 				mp->mp_minorname =
1462 				    i_ddi_strdup(minor, KM_SLEEP);
1463 			}
1464 		} else {
1465 			mp = NULL;
1466 			if (moddebug & MODDEBUG_MINORPERM) {
1467 				cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
1468 			}
1469 		}
1470 
1471 		/* mode */
1472 		nvp = nvlist_next_nvpair(nvl, nvp);
1473 		ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
1474 		if (mp)
1475 			(void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
1476 		/* uid */
1477 		nvp = nvlist_next_nvpair(nvl, nvp);
1478 		ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
1479 		if (mp)
1480 			(void) nvpair_value_int32(nvp, &mp->mp_uid);
1481 		/* gid */
1482 		nvp = nvlist_next_nvpair(nvl, nvp);
1483 		ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
1484 		if (mp) {
1485 			(void) nvpair_value_int32(nvp, &mp->mp_gid);
1486 
1487 			if (cmd == MODREMMINORPERM) {
1488 				rem_minorperm(major, name, mp, is_clone);
1489 				free_mperm(mp);
1490 			} else {
1491 				add_minorperm(major, name, mp, is_clone);
1492 			}
1493 		}
1494 	}
1495 
1496 	if (cmd == MODLOADMINORPERM)
1497 		minorperm_loaded = 1;
1498 
1499 	/*
1500 	 * Reset permissions of cached dv_nodes
1501 	 */
1502 	(void) devfs_reset_perm(DV_RESET_PERM);
1503 
1504 	return (0);
1505 }
1506 
1507 static int
1508 modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
1509 {
1510 	int error;
1511 	nvlist_t *nvl;
1512 	char *buf = kmem_alloc(buflen, KM_SLEEP);
1513 
1514 	if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
1515 		kmem_free(buf, buflen);
1516 		return (error);
1517 	}
1518 
1519 	error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
1520 	kmem_free(buf, buflen);
1521 	if (error)
1522 		return (error);
1523 
1524 	error = process_minorperm(cmd, nvl);
1525 	nvlist_free(nvl);
1526 	return (error);
1527 }
1528 
1529 struct walk_args {
1530 	char		*wa_drvname;
1531 	list_t		wa_pathlist;
1532 };
1533 
1534 struct path_elem {
1535 	char		*pe_dir;
1536 	char		*pe_nodename;
1537 	list_node_t	pe_node;
1538 	int		pe_dirlen;
1539 };
1540 
1541 /*ARGSUSED*/
1542 static int
1543 modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
1544 {
1545 	struct walk_args *wargs = (struct walk_args *)arg;
1546 	struct path_elem *pe;
1547 	char *nodename;
1548 
1549 	if (strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
1550 		return (INST_WALK_CONTINUE);
1551 
1552 	pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
1553 	pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
1554 	pe->pe_dirlen = strlen(pe->pe_dir) + 1;
1555 	ASSERT(strrchr(pe->pe_dir, '/') != NULL);
1556 	nodename = strrchr(pe->pe_dir, '/');
1557 	*nodename++ = 0;
1558 	pe->pe_nodename = nodename;
1559 	list_insert_tail(&wargs->wa_pathlist, pe);
1560 
1561 	return (INST_WALK_CONTINUE);
1562 }
1563 
1564 static int
1565 modctl_remdrv_cleanup(const char *u_drvname)
1566 {
1567 	struct walk_args *wargs;
1568 	struct path_elem *pe;
1569 	char *drvname;
1570 	int err, rval = 0;
1571 
1572 	drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
1573 	if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
1574 		kmem_free(drvname, MAXMODCONFNAME);
1575 		return (err);
1576 	}
1577 
1578 	/*
1579 	 * First go through the instance database.  For each
1580 	 * instance of a device bound to the driver being
1581 	 * removed, remove any underlying devfs attribute nodes.
1582 	 *
1583 	 * This is a two-step process.  First we go through
1584 	 * the instance data itself, constructing a list of
1585 	 * the nodes discovered.  The second step is then
1586 	 * to find and remove any devfs attribute nodes
1587 	 * for the instances discovered in the first step.
1588 	 * The two-step process avoids any difficulties
1589 	 * which could arise by holding the instance data
1590 	 * lock with simultaneous devfs operations.
1591 	 */
1592 	wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
1593 
1594 	wargs->wa_drvname = drvname;
1595 	list_create(&wargs->wa_pathlist,
1596 	    sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
1597 
1598 	(void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
1599 
1600 	for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
1601 	    pe = list_next(&wargs->wa_pathlist, pe)) {
1602 		err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
1603 			(const char *)pe->pe_nodename);
1604 		if (rval == 0)
1605 			rval = err;
1606 	}
1607 
1608 	while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
1609 		list_remove(&wargs->wa_pathlist, pe);
1610 		kmem_free(pe->pe_dir, pe->pe_dirlen);
1611 		kmem_free(pe, sizeof (*pe));
1612 	}
1613 	kmem_free(wargs, sizeof (*wargs));
1614 
1615 	/*
1616 	 * Pseudo nodes aren't recorded in the instance database
1617 	 * so any such nodes need to be handled separately.
1618 	 */
1619 	err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
1620 	if (rval == 0)
1621 		rval = err;
1622 
1623 	kmem_free(drvname, MAXMODCONFNAME);
1624 	return (rval);
1625 }
1626 
1627 static int
1628 modctl_allocpriv(const char *name)
1629 {
1630 	char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
1631 	int error;
1632 
1633 	if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
1634 		kmem_free(pstr, PRIVNAME_MAX);
1635 		return (error);
1636 	}
1637 	error = priv_getbyname(pstr, PRIV_ALLOC);
1638 	if (error < 0)
1639 		error = -error;
1640 	else
1641 		error = 0;
1642 	kmem_free(pstr, PRIVNAME_MAX);
1643 	return (error);
1644 }
1645 
1646 static int
1647 modctl_devexists(const char *upath, int pathlen)
1648 {
1649 	char	*path;
1650 	int	ret;
1651 
1652 	/*
1653 	 * copy in the path, including the terminating null
1654 	 */
1655 	pathlen++;
1656 	if (pathlen <= 1 || pathlen > MAXPATHLEN)
1657 		return (EINVAL);
1658 	path = kmem_zalloc(pathlen + 1, KM_SLEEP);
1659 	if ((ret = copyinstr(upath, path, pathlen, NULL)) == 0) {
1660 		ret = sdev_modctl_devexists(path);
1661 	}
1662 
1663 	kmem_free(path, pathlen + 1);
1664 	return (ret);
1665 }
1666 
1667 static int
1668 modctl_devreaddir(const char *udir, int udirlen,
1669     char *upaths, int64_t *ulensp)
1670 {
1671 	char	*paths = NULL;
1672 	char	**dirlist = NULL;
1673 	char	*dir;
1674 	int64_t	ulens;
1675 	int64_t	lens;
1676 	int	i, n;
1677 	int	ret = 0;
1678 	char	*p;
1679 	int	npaths;
1680 	int	npaths_alloc;
1681 
1682 	/*
1683 	 * If upaths is NULL then we are only computing the amount of space
1684 	 * needed to return the paths, with the value returned in *ulensp. If we
1685 	 * are copying out paths then we get the amount of space allocated by
1686 	 * the caller. If the actual space needed for paths is larger, or
1687 	 * things are changing out from under us, then we return EAGAIN.
1688 	 */
1689 	if (upaths) {
1690 		if (ulensp == NULL)
1691 			return (EINVAL);
1692 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
1693 			return (EFAULT);
1694 	}
1695 
1696 	/*
1697 	 * copyin the /dev path including terminating null
1698 	 */
1699 	udirlen++;
1700 	if (udirlen <= 1 || udirlen > MAXPATHLEN)
1701 		return (EINVAL);
1702 	dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
1703 	if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
1704 		goto err;
1705 
1706 	if ((ret = sdev_modctl_readdir(dir, &dirlist,
1707 	    &npaths, &npaths_alloc)) != 0) {
1708 		ASSERT(dirlist == NULL);
1709 		goto err;
1710 	}
1711 
1712 	lens = 0;
1713 	for (i = 0; i < npaths; i++) {
1714 		lens += strlen(dirlist[i]) + 1;
1715 	}
1716 	lens++;		/* add one for double termination */
1717 
1718 	if (upaths) {
1719 		if (lens > ulens) {
1720 			ret = EAGAIN;
1721 			goto out;
1722 		}
1723 
1724 		paths = kmem_alloc(lens, KM_SLEEP);
1725 
1726 		p = paths;
1727 		for (i = 0; i < npaths; i++) {
1728 			n = strlen(dirlist[i]) + 1;
1729 			bcopy(dirlist[i], p, n);
1730 			p += n;
1731 		}
1732 		*p = 0;
1733 
1734 		if (copyout(paths, upaths, lens)) {
1735 			ret = EFAULT;
1736 			goto err;
1737 		}
1738 	}
1739 
1740 out:
1741 	/* copy out the amount of space needed to hold the paths */
1742 	if (copyout(&lens, ulensp, sizeof (lens)))
1743 		ret = EFAULT;
1744 
1745 err:
1746 	if (dirlist)
1747 		sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
1748 	if (paths)
1749 		kmem_free(paths, lens);
1750 	kmem_free(dir, udirlen + 1);
1751 	return (ret);
1752 }
1753 
1754 int
1755 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
1756 {
1757 	int error = 0;
1758 
1759 	switch (subcmd) {
1760 	case MODDEVNAME_LOOKUPDOOR:
1761 	case MODDEVNAME_DEVFSADMNODE:
1762 		error = devname_filename_register(subcmd, (char *)a1);
1763 		break;
1764 	case MODDEVNAME_NSMAPS:
1765 		error = devname_nsmaps_register((char *)a1, (size_t)a2);
1766 		break;
1767 	case MODDEVNAME_PROFILE:
1768 		error = devname_profile_update((char *)a1, (size_t)a2);
1769 		break;
1770 	case MODDEVNAME_RECONFIG:
1771 		i_ddi_set_reconfig();
1772 		break;
1773 	case MODDEVNAME_SYSAVAIL:
1774 		i_ddi_set_sysavail();
1775 		break;
1776 	default:
1777 		error = EINVAL;
1778 		break;
1779 	}
1780 
1781 	return (error);
1782 }
1783 
1784 /*ARGSUSED5*/
1785 int
1786 modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
1787     uintptr_t a5)
1788 {
1789 	int	error = EINVAL;
1790 	dev_t	dev;
1791 
1792 	if (secpolicy_modctl(CRED(), cmd) != 0)
1793 		return (set_errno(EPERM));
1794 
1795 	switch (cmd) {
1796 	case MODLOAD:		/* load a module */
1797 		error = modctl_modload((int)a1, (char *)a2, (int *)a3);
1798 		break;
1799 
1800 	case MODUNLOAD:		/* unload a module */
1801 		error = modctl_modunload((modid_t)a1);
1802 		break;
1803 
1804 	case MODINFO:		/* get module status */
1805 		error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
1806 		break;
1807 
1808 	case MODRESERVED:	/* get last major number in range */
1809 		error = modctl_modreserve((modid_t)a1, (int *)a2);
1810 		break;
1811 
1812 	case MODSETMINIROOT:	/* we are running in miniroot */
1813 		isminiroot = 1;
1814 		error = 0;
1815 		break;
1816 
1817 	case MODADDMAJBIND:	/* read major binding file */
1818 		error = modctl_add_major((int *)a2);
1819 		break;
1820 
1821 	case MODGETPATHLEN:	/* get modpath length */
1822 		error = modctl_getmodpathlen((int *)a2);
1823 		break;
1824 
1825 	case MODGETPATH:	/* get modpath */
1826 		error = modctl_getmodpath((char *)a2);
1827 		break;
1828 
1829 	case MODREADSYSBIND:	/* read system call binding file */
1830 		error = modctl_read_sysbinding_file();
1831 		break;
1832 
1833 	case MODGETMAJBIND:	/* get major number for named device */
1834 		error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
1835 		break;
1836 
1837 	case MODGETNAME:	/* get name of device given major number */
1838 		error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
1839 		break;
1840 
1841 	case MODDEVT2INSTANCE:
1842 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1843 			dev = (dev_t)a1;
1844 		}
1845 #ifdef _SYSCALL32_IMPL
1846 		else {
1847 			dev = expldev(a1);
1848 		}
1849 #endif
1850 		error = modctl_devt2instance(dev, (int *)a2);
1851 		break;
1852 
1853 	case MODSIZEOF_DEVID:	/* sizeof device id of device given dev_t */
1854 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1855 			dev = (dev_t)a1;
1856 		}
1857 #ifdef _SYSCALL32_IMPL
1858 		else {
1859 			dev = expldev(a1);
1860 		}
1861 #endif
1862 		error = modctl_sizeof_devid(dev, (uint_t *)a2);
1863 		break;
1864 
1865 	case MODGETDEVID:	/* get device id of device given dev_t */
1866 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1867 			dev = (dev_t)a1;
1868 		}
1869 #ifdef _SYSCALL32_IMPL
1870 		else {
1871 			dev = expldev(a1);
1872 		}
1873 #endif
1874 		error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
1875 		break;
1876 
1877 	case MODSIZEOF_MINORNAME:	/* sizeof minor nm of dev_t/spectype */
1878 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1879 			error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
1880 			    (uint_t *)a3);
1881 		}
1882 #ifdef _SYSCALL32_IMPL
1883 		else {
1884 			error = modctl_sizeof_minorname(expldev(a1), (int)a2,
1885 			    (uint_t *)a3);
1886 		}
1887 
1888 #endif
1889 		break;
1890 
1891 	case MODGETMINORNAME:   /* get minor name of dev_t and spec type */
1892 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1893 			error = modctl_get_minorname((dev_t)a1, (int)a2,
1894 			    (uint_t)a3, (char *)a4);
1895 		}
1896 #ifdef _SYSCALL32_IMPL
1897 		else {
1898 			error = modctl_get_minorname(expldev(a1), (int)a2,
1899 			    (uint_t)a3, (char *)a4);
1900 		}
1901 #endif
1902 		break;
1903 
1904 	case MODGETDEVFSPATH_LEN:	/* sizeof path nm of dev_t/spectype */
1905 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1906 			error = modctl_devfspath_len((dev_t)a1, (int)a2,
1907 			    (uint_t *)a3);
1908 		}
1909 #ifdef _SYSCALL32_IMPL
1910 		else {
1911 			error = modctl_devfspath_len(expldev(a1), (int)a2,
1912 			    (uint_t *)a3);
1913 		}
1914 
1915 #endif
1916 		break;
1917 
1918 	case MODGETDEVFSPATH:   /* get path name of dev_t and spec type */
1919 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1920 			error = modctl_devfspath((dev_t)a1, (int)a2,
1921 			    (uint_t)a3, (char *)a4);
1922 		}
1923 #ifdef _SYSCALL32_IMPL
1924 		else {
1925 			error = modctl_devfspath(expldev(a1), (int)a2,
1926 			    (uint_t)a3, (char *)a4);
1927 		}
1928 #endif
1929 		break;
1930 
1931 
1932 	case MODEVENTS:
1933 		error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
1934 		break;
1935 
1936 	case MODGETFBNAME:	/* get the framebuffer name */
1937 		error = modctl_get_fbname((char *)a1);
1938 		break;
1939 
1940 	case MODREREADDACF:	/* reread dacf rule database from given file */
1941 		error = modctl_reread_dacf((char *)a1);
1942 		break;
1943 
1944 	case MODLOADDRVCONF:	/* load driver.conf file for major */
1945 		error = modctl_load_drvconf((major_t)a1);
1946 		break;
1947 
1948 	case MODUNLOADDRVCONF:	/* unload driver.conf file for major */
1949 		error = modctl_unload_drvconf((major_t)a1);
1950 		break;
1951 
1952 	case MODREMMAJBIND:	/* remove a major binding */
1953 		error = modctl_rem_major((major_t)a1);
1954 		break;
1955 
1956 	case MODDEVID2PATHS:	/* get paths given devid */
1957 		error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
1958 		    (uint_t)a3, (size_t *)a4, (char *)a5);
1959 		break;
1960 
1961 	case MODSETDEVPOLICY:	/* establish device policy */
1962 		error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
1963 		break;
1964 
1965 	case MODGETDEVPOLICY:	/* get device policy */
1966 		error = devpolicy_get((int *)a1, (size_t)a2,
1967 				(devplcysys_t *)a3);
1968 		break;
1969 
1970 	case MODALLOCPRIV:
1971 		error = modctl_allocpriv((const char *)a1);
1972 		break;
1973 
1974 	case MODGETDEVPOLICYBYNAME:
1975 		error = devpolicy_getbyname((size_t)a1,
1976 		    (devplcysys_t *)a2, (char *)a3);
1977 		break;
1978 
1979 	case MODLOADMINORPERM:
1980 	case MODADDMINORPERM:
1981 	case MODREMMINORPERM:
1982 		error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
1983 		break;
1984 
1985 	case MODREMDRVCLEANUP:
1986 		error = modctl_remdrv_cleanup((const char *)a1);
1987 		break;
1988 
1989 	case MODDEVEXISTS:	/* non-reconfiguring /dev lookup */
1990 		error = modctl_devexists((const char *)a1, (size_t)a2);
1991 		break;
1992 
1993 	case MODDEVREADDIR:	/* non-reconfiguring /dev readdir */
1994 		error = modctl_devreaddir((const char *)a1, (size_t)a2,
1995 		    (char *)a3, (int64_t *)a4);
1996 		break;
1997 
1998 	case MODDEVNAME:
1999 		error = modctl_moddevname((int)a1, a2, a3);
2000 		break;
2001 
2002 	default:
2003 		error = EINVAL;
2004 		break;
2005 	}
2006 
2007 	return (error ? set_errno(error) : 0);
2008 }
2009 
2010 /*
2011  * Calls to kobj_load_module()() are handled off to this routine in a
2012  * separate thread.
2013  */
2014 static void
2015 modload_thread(struct loadmt *ltp)
2016 {
2017 	/* load the module and signal the creator of this thread */
2018 	kmutex_t	cpr_lk;
2019 	callb_cpr_t	cpr_i;
2020 
2021 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
2022 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
2023 	/* borrow the devi lock from thread which invoked us */
2024 	pm_borrow_lock(ltp->owner);
2025 	ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
2026 	pm_return_lock();
2027 	sema_v(&ltp->sema);
2028 	mutex_enter(&cpr_lk);
2029 	CALLB_CPR_EXIT(&cpr_i);
2030 	mutex_destroy(&cpr_lk);
2031 	thread_exit();
2032 }
2033 
2034 /*
2035  * load a module, adding a reference if caller specifies rmodp.  If rmodp
2036  * is specified then an errno is returned, otherwise a module index is
2037  * returned (-1 on error).
2038  */
2039 static int
2040 modrload(char *subdir, char *filename, struct modctl **rmodp)
2041 {
2042 	struct modctl *modp;
2043 	size_t size;
2044 	char *fullname;
2045 	int retval = EINVAL;
2046 	int id = -1;
2047 
2048 	if (rmodp)
2049 		*rmodp = NULL;			/* avoid garbage */
2050 
2051 	if (subdir != NULL) {
2052 		/*
2053 		 * refuse / in filename to prevent "../" escapes.
2054 		 */
2055 		if (strchr(filename, '/') != NULL)
2056 			return (rmodp ? retval : id);
2057 
2058 		/*
2059 		 * allocate enough space for <subdir>/<filename><NULL>
2060 		 */
2061 		size = strlen(subdir) + strlen(filename) + 2;
2062 		fullname = kmem_zalloc(size, KM_SLEEP);
2063 		(void) sprintf(fullname, "%s/%s", subdir, filename);
2064 	} else {
2065 		fullname = filename;
2066 	}
2067 
2068 	modp = mod_hold_installed_mod(fullname, 1, &retval);
2069 	if (modp != NULL) {
2070 		id = modp->mod_id;
2071 		if (rmodp) {
2072 			/* add mod_ref and return *rmodp */
2073 			mutex_enter(&mod_lock);
2074 			modp->mod_ref++;
2075 			mutex_exit(&mod_lock);
2076 			*rmodp = modp;
2077 		}
2078 		mod_release_mod(modp);
2079 		CPU_STATS_ADDQ(CPU, sys, modload, 1);
2080 	}
2081 
2082 done:	if (subdir != NULL)
2083 		kmem_free(fullname, size);
2084 	return (rmodp ? retval : id);
2085 }
2086 
2087 /*
2088  * This is the primary kernel interface to load a module. It loads and
2089  * installs the named module.  It does not hold mod_ref of the module, so
2090  * a module unload attempt can occur at any time - it is up to the
2091  * _fini/mod_remove implementation to determine if unload will succeed.
2092  */
2093 int
2094 modload(char *subdir, char *filename)
2095 {
2096 	return (modrload(subdir, filename, NULL));
2097 }
2098 
2099 /*
2100  * Load a module using a series of qualified names from most specific to least
2101  * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
2102  *
2103  * foo/bar.baz.1.2.3
2104  * foo/bar.baz.1.2
2105  * foo/bar.baz.1
2106  *
2107  * Return the module ID on success; -1 if no module was loaded.
2108  */
2109 int
2110 modload_qualified(const char *subdir, const char *p1,
2111     const char *p2, const char *delim, uint_t suffv[], int suffc)
2112 {
2113 	char path[MOD_MAXPATH];
2114 	size_t n, resid = sizeof (path);
2115 	char *p = path;
2116 
2117 	char **dotv;
2118 	int i, rc, id;
2119 	modctl_t *mp;
2120 
2121 	if (p2 != NULL)
2122 		n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
2123 	else
2124 		n = snprintf(p, resid, "%s/%s", subdir, p1);
2125 
2126 	if (n >= resid)
2127 		return (-1);
2128 
2129 	p += n;
2130 	resid -= n;
2131 	dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
2132 
2133 	for (i = 0; i < suffc; i++) {
2134 		dotv[i] = p;
2135 		n = snprintf(p, resid, "%s%u", delim, suffv[i]);
2136 
2137 		if (n >= resid) {
2138 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
2139 			return (-1);
2140 		}
2141 
2142 		p += n;
2143 		resid -= n;
2144 	}
2145 
2146 	dotv[suffc] = p;
2147 
2148 	for (i = suffc; i >= 0; i--) {
2149 		dotv[i][0] = '\0';
2150 		mp = mod_hold_installed_mod(path, 1, &rc);
2151 
2152 		if (mp != NULL) {
2153 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
2154 			id = mp->mod_id;
2155 			mod_release_mod(mp);
2156 			return (id);
2157 		}
2158 	}
2159 
2160 	kmem_free(dotv, sizeof (char *) * (suffc + 1));
2161 	return (-1);
2162 }
2163 
2164 /*
2165  * Load a module.
2166  */
2167 int
2168 modloadonly(char *subdir, char *filename)
2169 {
2170 	struct modctl *modp;
2171 	char *fullname;
2172 	size_t size;
2173 	int id, retval;
2174 
2175 	if (subdir != NULL) {
2176 		/*
2177 		 * allocate enough space for <subdir>/<filename><NULL>
2178 		 */
2179 		size = strlen(subdir) + strlen(filename) + 2;
2180 		fullname = kmem_zalloc(size, KM_SLEEP);
2181 		(void) sprintf(fullname, "%s/%s", subdir, filename);
2182 	} else {
2183 		fullname = filename;
2184 	}
2185 
2186 	modp = mod_hold_loaded_mod(NULL, fullname, &retval);
2187 	if (modp) {
2188 		id = modp->mod_id;
2189 		mod_release_mod(modp);
2190 	}
2191 
2192 	if (subdir != NULL)
2193 		kmem_free(fullname, size);
2194 
2195 	if (retval == 0)
2196 		return (id);
2197 	return (-1);
2198 }
2199 
2200 /*
2201  * Try to uninstall and unload a module, removing a reference if caller
2202  * specifies rmodp.
2203  */
2204 static int
2205 modunrload(modid_t id, struct modctl **rmodp, int unload)
2206 {
2207 	struct modctl	*modp;
2208 	int		retval;
2209 
2210 	if (rmodp)
2211 		*rmodp = NULL;			/* avoid garbage */
2212 
2213 	if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
2214 		return (EINVAL);
2215 
2216 	if (rmodp) {
2217 		mutex_enter(&mod_lock);
2218 		modp->mod_ref--;
2219 		mutex_exit(&mod_lock);
2220 		*rmodp = modp;
2221 	}
2222 
2223 	if (unload) {
2224 		retval = moduninstall(modp);
2225 		if (retval == 0) {
2226 			mod_unload(modp);
2227 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
2228 		} else if (retval == EALREADY)
2229 			retval = 0;	/* already unloaded, not an error */
2230 	} else
2231 		retval = 0;
2232 
2233 	mod_release_mod(modp);
2234 	return (retval);
2235 }
2236 
2237 /*
2238  * Uninstall and unload a module.
2239  */
2240 int
2241 modunload(modid_t id)
2242 {
2243 	int		retval;
2244 
2245 	/* synchronize with any active modunload_disable() */
2246 	modunload_begin();
2247 	if (ddi_root_node())
2248 		(void) devfs_clean(ddi_root_node(), NULL, 0);
2249 	retval = modunrload(id, NULL, 1);
2250 	modunload_end();
2251 	return (retval);
2252 }
2253 
2254 /*
2255  * Return status of a loaded module.
2256  */
2257 static int
2258 modinfo(modid_t id, struct modinfo *modinfop)
2259 {
2260 	struct modctl	*modp;
2261 	modid_t		mid;
2262 	int		i;
2263 
2264 	mid = modinfop->mi_id;
2265 	if (modinfop->mi_info & MI_INFO_ALL) {
2266 		while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
2267 			if ((modinfop->mi_info & MI_INFO_CNT) ||
2268 			    modp->mod_installed)
2269 				break;
2270 			mod_release_mod(modp);
2271 		}
2272 		if (modp == NULL)
2273 			return (EINVAL);
2274 	} else {
2275 		modp = mod_hold_by_id(id);
2276 		if (modp == NULL)
2277 			return (EINVAL);
2278 		if (!(modinfop->mi_info & MI_INFO_CNT) &&
2279 		    (modp->mod_installed == 0)) {
2280 			mod_release_mod(modp);
2281 			return (EINVAL);
2282 		}
2283 	}
2284 
2285 	modinfop->mi_rev = 0;
2286 	modinfop->mi_state = 0;
2287 	for (i = 0; i < MODMAXLINK; i++) {
2288 		modinfop->mi_msinfo[i].msi_p0 = -1;
2289 		modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
2290 	}
2291 	if (modp->mod_loaded) {
2292 		modinfop->mi_state = MI_LOADED;
2293 		kobj_getmodinfo(modp->mod_mp, modinfop);
2294 	}
2295 	if (modp->mod_installed) {
2296 		modinfop->mi_state |= MI_INSTALLED;
2297 
2298 		(void) mod_getinfo(modp, modinfop);
2299 	}
2300 
2301 	modinfop->mi_id = modp->mod_id;
2302 	modinfop->mi_loadcnt = modp->mod_loadcnt;
2303 	(void) strcpy(modinfop->mi_name, modp->mod_modname);
2304 
2305 	mod_release_mod(modp);
2306 	return (0);
2307 }
2308 
2309 static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
2310 static char no_err[] = "No error function for weak stub %s";
2311 
2312 /*
2313  * used by the stubs themselves to load and hold a module.
2314  * Returns  0 if the module is successfully held;
2315  *	    the stub needs to call mod_release_stub().
2316  *	    -1 if the stub should just call the err_fcn.
2317  * Note that this code is stretched out so that we avoid subroutine calls
2318  * and optimize for the most likely case.  That is, the case where the
2319  * module is loaded and installed and not held.  In that case we just inc
2320  * the mod_ref count and continue.
2321  */
2322 int
2323 mod_hold_stub(struct mod_stub_info *stub)
2324 {
2325 	struct modctl *mp;
2326 	struct mod_modinfo *mip;
2327 
2328 	mip = stub->mods_modinfo;
2329 
2330 	mutex_enter(&mod_lock);
2331 
2332 	/* we do mod_hold_by_modctl inline for speed */
2333 
2334 mod_check_again:
2335 	if ((mp = mip->mp) != NULL) {
2336 		if (mp->mod_busy == 0) {
2337 			if (mp->mod_installed) {
2338 				/* increment the reference count */
2339 				mp->mod_ref++;
2340 				ASSERT(mp->mod_ref && mp->mod_installed);
2341 				mutex_exit(&mod_lock);
2342 				return (0);
2343 			} else {
2344 				mp->mod_busy = 1;
2345 				mp->mod_inprogress_thread =
2346 				    (curthread == NULL ?
2347 				    (kthread_id_t)-1 : curthread);
2348 			}
2349 		} else {
2350 			/*
2351 			 * wait one time and then go see if someone
2352 			 * else has resolved the stub (set mip->mp).
2353 			 */
2354 			if (mod_hold_by_modctl(mp,
2355 			    MOD_WAIT_ONCE | MOD_LOCK_HELD))
2356 				goto mod_check_again;
2357 
2358 			/*
2359 			 * what we have now may have been unloaded!, in
2360 			 * that case, mip->mp will be NULL, we'll hit this
2361 			 * module and load again..
2362 			 */
2363 			cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
2364 		}
2365 		mutex_exit(&mod_lock);
2366 	} else {
2367 		/* first time we've hit this module */
2368 		mutex_exit(&mod_lock);
2369 		mp = mod_hold_by_name(mip->modm_module_name);
2370 		mip->mp = mp;
2371 	}
2372 
2373 	/*
2374 	 * If we are here, it means that the following conditions
2375 	 * are satisfied.
2376 	 *
2377 	 * mip->mp != NULL
2378 	 * this thread has set the mp->mod_busy = 1
2379 	 * mp->mod_installed = 0
2380 	 *
2381 	 */
2382 	ASSERT(mp != NULL);
2383 	ASSERT(mp->mod_busy == 1);
2384 
2385 	if (mp->mod_installed == 0) {
2386 		/* Module not loaded, if weak stub don't load it */
2387 		if (stub->mods_flag & MODS_WEAK) {
2388 			if (stub->mods_errfcn == NULL) {
2389 				mod_release_mod(mp);
2390 				cmn_err(CE_PANIC, no_err,
2391 				    mip->modm_module_name);
2392 			}
2393 		} else {
2394 			/* Not a weak stub so load the module */
2395 
2396 			if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
2397 				/*
2398 				 * If mod_load() was successful
2399 				 * and modinstall() failed, then
2400 				 * unload the module.
2401 				 */
2402 				if (mp->mod_loaded)
2403 					mod_unload(mp);
2404 
2405 				mod_release_mod(mp);
2406 				if (stub->mods_errfcn == NULL) {
2407 					cmn_err(CE_PANIC, mod_stub_err,
2408 					    mip->modm_module_name);
2409 				} else {
2410 					return (-1);
2411 				}
2412 			}
2413 		}
2414 	}
2415 
2416 	/*
2417 	 * At this point module is held and loaded. Release
2418 	 * the mod_busy and mod_inprogress_thread before
2419 	 * returning. We actually call mod_release() here so
2420 	 * that if another stub wants to access this module,
2421 	 * it can do so. mod_ref is incremented before mod_release()
2422 	 * is called to prevent someone else from snatching the
2423 	 * module from this thread.
2424 	 */
2425 	mutex_enter(&mod_lock);
2426 	mp->mod_ref++;
2427 	ASSERT(mp->mod_ref &&
2428 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
2429 	mod_release(mp);
2430 	mutex_exit(&mod_lock);
2431 	return (0);
2432 }
2433 
2434 void
2435 mod_release_stub(struct mod_stub_info *stub)
2436 {
2437 	struct modctl *mp = stub->mods_modinfo->mp;
2438 
2439 	/* inline mod_release_mod */
2440 	mutex_enter(&mod_lock);
2441 	ASSERT(mp->mod_ref &&
2442 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
2443 	mp->mod_ref--;
2444 	if (mp->mod_want) {
2445 		mp->mod_want = 0;
2446 		cv_broadcast(&mod_cv);
2447 	}
2448 	mutex_exit(&mod_lock);
2449 }
2450 
2451 static struct modctl *
2452 mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
2453 {
2454 	struct modctl *modp;
2455 	int retval;
2456 
2457 	/*
2458 	 * Hold the module.
2459 	 */
2460 	modp = mod_hold_by_name_requisite(dep, filename);
2461 	if (modp) {
2462 		retval = mod_load(modp, 1);
2463 		if (retval != 0) {
2464 			mod_release_mod(modp);
2465 			modp = NULL;
2466 		}
2467 		*status = retval;
2468 	} else {
2469 		*status = ENOSPC;
2470 	}
2471 
2472 	/*
2473 	 * if dep is not NULL, clear the module dependency information.
2474 	 * This information is set in mod_hold_by_name_common().
2475 	 */
2476 	if (dep != NULL && dep->mod_requisite_loading != NULL) {
2477 		ASSERT(dep->mod_busy);
2478 		dep->mod_requisite_loading = NULL;
2479 	}
2480 
2481 	return (modp);
2482 }
2483 
2484 /*
2485  * hold, load, and install the named module
2486  */
2487 static struct modctl *
2488 mod_hold_installed_mod(char *name, int usepath, int *r)
2489 {
2490 	struct modctl *modp;
2491 	int retval;
2492 
2493 	/*
2494 	 * Verify that that module in question actually exists on disk
2495 	 * before allocation of module structure by mod_hold_by_name.
2496 	 */
2497 	if (modrootloaded && swaploaded) {
2498 		if (!kobj_path_exists(name, usepath)) {
2499 			*r = ENOENT;
2500 			return (NULL);
2501 		}
2502 	}
2503 
2504 	/*
2505 	 * Hold the module.
2506 	 */
2507 	modp = mod_hold_by_name(name);
2508 	if (modp) {
2509 		retval = mod_load(modp, usepath);
2510 		if (retval != 0) {
2511 			mod_release_mod(modp);
2512 			modp = NULL;
2513 			*r = retval;
2514 		} else {
2515 			if ((*r = modinstall(modp)) != 0) {
2516 				/*
2517 				 * We loaded it, but failed to _init() it.
2518 				 * Be kind to developers -- force it
2519 				 * out of memory now so that the next
2520 				 * attempt to use the module will cause
2521 				 * a reload.  See 1093793.
2522 				 */
2523 				mod_unload(modp);
2524 				mod_release_mod(modp);
2525 				modp = NULL;
2526 			}
2527 		}
2528 	} else {
2529 		*r = ENOSPC;
2530 	}
2531 	return (modp);
2532 }
2533 
2534 static char mod_excl_msg[] =
2535 	"module %s(%s) is EXCLUDED and will not be loaded\n";
2536 static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
2537 
2538 /*
2539  * This routine is needed for dependencies.  Users specify dependencies
2540  * by declaring a character array initialized to filenames of dependents.
2541  * So the code that handles dependents deals with filenames (and not
2542  * module names) because that's all it has.  We load by filename and once
2543  * we've loaded a file we can get the module name.
2544  * Unfortunately there isn't a single unified filename/modulename namespace.
2545  * C'est la vie.
2546  *
2547  * We allow the name being looked up to be prepended by an optional
2548  * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
2549  */
2550 struct modctl *
2551 mod_find_by_filename(char *subdir, char *filename)
2552 {
2553 	struct modctl	*mp;
2554 	size_t		sublen;
2555 
2556 	ASSERT(!MUTEX_HELD(&mod_lock));
2557 	if (subdir != NULL)
2558 		sublen = strlen(subdir);
2559 	else
2560 		sublen = 0;
2561 
2562 	mutex_enter(&mod_lock);
2563 	mp = &modules;
2564 	do {
2565 		if (sublen) {
2566 			char *mod_filename = mp->mod_filename;
2567 
2568 			if (strncmp(subdir, mod_filename, sublen) == 0 &&
2569 			    mod_filename[sublen] == '/' &&
2570 			    strcmp(filename, &mod_filename[sublen + 1]) == 0) {
2571 				mutex_exit(&mod_lock);
2572 				return (mp);
2573 			}
2574 		} else if (strcmp(filename, mp->mod_filename) == 0) {
2575 			mutex_exit(&mod_lock);
2576 			return (mp);
2577 		}
2578 	} while ((mp = mp->mod_next) != &modules);
2579 	mutex_exit(&mod_lock);
2580 	return (NULL);
2581 }
2582 
2583 /*
2584  * Check for circular dependencies.  This is called from do_dependents()
2585  * in kobj.c.  If we are the thread already loading this module, then
2586  * we're trying to load a dependent that we're already loading which
2587  * means the user specified circular dependencies.
2588  */
2589 static int
2590 mod_circdep(struct modctl *modp)
2591 {
2592 	struct modctl	*rmod;
2593 
2594 	ASSERT(MUTEX_HELD(&mod_lock));
2595 
2596 	/*
2597 	 * Check the mod_inprogress_thread first.
2598 	 * mod_inprogress_thread is used in mod_hold_stub()
2599 	 * directly to improve performance.
2600 	 */
2601 	if (modp->mod_inprogress_thread == curthread)
2602 		return (1);
2603 
2604 	/*
2605 	 * Check the module circular dependencies.
2606 	 */
2607 	for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
2608 		/*
2609 		 * Check if there is a module circular dependency.
2610 		 */
2611 		if (rmod->mod_requisite_loading == modp)
2612 			return (1);
2613 	}
2614 	return (0);
2615 }
2616 
2617 static int
2618 mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
2619 {
2620 	int (*func)(struct modinfo *);
2621 	int retval;
2622 
2623 	ASSERT(modp->mod_busy);
2624 
2625 	/* primary modules don't do getinfo */
2626 	if (modp->mod_prim)
2627 		return (0);
2628 
2629 	func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
2630 
2631 	if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
2632 		cmn_err(CE_WARN, "_info() not defined properly in %s",
2633 		    modp->mod_filename);
2634 		/*
2635 		 * The semantics of mod_info(9F) are that 0 is failure
2636 		 * and non-zero is success.
2637 		 */
2638 		retval = 0;
2639 	} else
2640 		retval = (*func)(modinfop);	/* call _info() function */
2641 
2642 	if (moddebug & MODDEBUG_USERDEBUG)
2643 		printf("Returned from _info, retval = %x\n", retval);
2644 
2645 	return (retval);
2646 }
2647 
2648 static void
2649 modadd(struct modctl *mp)
2650 {
2651 	ASSERT(MUTEX_HELD(&mod_lock));
2652 
2653 	mp->mod_id = last_module_id++;
2654 	mp->mod_next = &modules;
2655 	mp->mod_prev = modules.mod_prev;
2656 	modules.mod_prev->mod_next = mp;
2657 	modules.mod_prev = mp;
2658 }
2659 
2660 /*ARGSUSED*/
2661 static struct modctl *
2662 allocate_modp(const char *filename, const char *modname)
2663 {
2664 	struct modctl *mp;
2665 
2666 	mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
2667 	mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
2668 	(void) strcpy(mp->mod_modname, modname);
2669 	return (mp);
2670 }
2671 
2672 /*
2673  * Get the value of a symbol.  This is a wrapper routine that
2674  * calls kobj_getsymvalue().  kobj_getsymvalue() may go away but this
2675  * wrapper will prevent callers from noticing.
2676  */
2677 uintptr_t
2678 modgetsymvalue(char *name, int kernelonly)
2679 {
2680 	return (kobj_getsymvalue(name, kernelonly));
2681 }
2682 
2683 /*
2684  * Get the symbol nearest an address.  This is a wrapper routine that
2685  * calls kobj_getsymname().  kobj_getsymname() may go away but this
2686  * wrapper will prevent callers from noticing.
2687  */
2688 char *
2689 modgetsymname(uintptr_t value, ulong_t *offset)
2690 {
2691 	return (kobj_getsymname(value, offset));
2692 }
2693 
2694 /*
2695  * Lookup a symbol in a specified module.  These are wrapper routines that
2696  * call kobj_lookup().  kobj_lookup() may go away but these wrappers will
2697  * prevent callers from noticing.
2698  */
2699 uintptr_t
2700 modlookup(const char *modname, const char *symname)
2701 {
2702 	struct modctl *modp;
2703 	uintptr_t val;
2704 
2705 	if ((modp = mod_hold_by_name(modname)) == NULL)
2706 		return (0);
2707 	val = kobj_lookup(modp->mod_mp, symname);
2708 	mod_release_mod(modp);
2709 	return (val);
2710 }
2711 
2712 uintptr_t
2713 modlookup_by_modctl(modctl_t *modp, const char *symname)
2714 {
2715 	ASSERT(modp->mod_ref > 0 || modp->mod_busy);
2716 
2717 	return (kobj_lookup(modp->mod_mp, symname));
2718 }
2719 
2720 /*
2721  * Ask the user for the name of the system file and the default path
2722  * for modules.
2723  */
2724 void
2725 mod_askparams()
2726 {
2727 	static char s0[64];
2728 	intptr_t fd;
2729 
2730 	if ((fd = kobj_open(systemfile)) != -1L)
2731 		kobj_close(fd);
2732 	else
2733 		systemfile = NULL;
2734 
2735 	/*CONSTANTCONDITION*/
2736 	while (1) {
2737 		printf("Name of system file [%s]:  ",
2738 			systemfile ? systemfile : "/dev/null");
2739 
2740 		console_gets(s0, sizeof (s0));
2741 
2742 		if (s0[0] == '\0')
2743 			break;
2744 		else if (strcmp(s0, "/dev/null") == 0) {
2745 			systemfile = NULL;
2746 			break;
2747 		} else {
2748 			if ((fd = kobj_open(s0)) != -1L) {
2749 				kobj_close(fd);
2750 				systemfile = s0;
2751 				break;
2752 			}
2753 		}
2754 		printf("can't find file %s\n", s0);
2755 	}
2756 }
2757 
2758 static char loading_msg[] = "loading '%s' id %d\n";
2759 static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
2760 
2761 /*
2762  * Common code for loading a module (but not installing it).
2763  * Handoff the task of module loading to a seperate thread
2764  * with a large stack if possible, since this code may recurse a few times.
2765  * Return zero if there are no errors or an errno value.
2766  */
2767 static int
2768 mod_load(struct modctl *mp, int usepath)
2769 {
2770 	int		retval;
2771 	struct modinfo	*modinfop = NULL;
2772 	struct loadmt	lt;
2773 
2774 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
2775 	ASSERT(mp->mod_busy);
2776 
2777 	if (mp->mod_loaded)
2778 		return (0);
2779 
2780 	if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
2781 	    mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
2782 		if (moddebug & MODDEBUG_LOADMSG) {
2783 			printf(mod_excl_msg, mp->mod_filename,
2784 				mp->mod_modname);
2785 		}
2786 		return (ENXIO);
2787 	}
2788 	if (moddebug & MODDEBUG_LOADMSG2)
2789 		printf(loading_msg, mp->mod_filename, mp->mod_id);
2790 
2791 	if (curthread != &t0) {
2792 		lt.mp = mp;
2793 		lt.usepath = usepath;
2794 		lt.owner = curthread;
2795 		sema_init(&lt.sema, 0, NULL, SEMA_DEFAULT, NULL);
2796 
2797 		/* create thread to hand of call to */
2798 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
2799 		    modload_thread, &lt, 0, &p0, TS_RUN, maxclsyspri);
2800 
2801 		/* wait for thread to complete kobj_load_module */
2802 		sema_p(&lt.sema);
2803 
2804 		sema_destroy(&lt.sema);
2805 		retval = lt.retval;
2806 	} else
2807 		retval = kobj_load_module(mp, usepath);
2808 
2809 	if (mp->mod_mp) {
2810 		ASSERT(retval == 0);
2811 		mp->mod_loaded = 1;
2812 		mp->mod_loadcnt++;
2813 		if (moddebug & MODDEBUG_LOADMSG) {
2814 			printf(load_msg, mp->mod_filename, mp->mod_id,
2815 				(void *)((struct module *)mp->mod_mp)->text,
2816 				(void *)((struct module *)mp->mod_mp)->data,
2817 				((struct module *)mp->mod_mp)->text_size,
2818 				((struct module *)mp->mod_mp)->data_size);
2819 		}
2820 
2821 		/*
2822 		 * XXX - There should be a better way to get this.
2823 		 */
2824 		modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
2825 		modinfop->mi_info = MI_INFO_LINKAGE;
2826 		if (mod_getinfo(mp, modinfop) == 0)
2827 			mp->mod_linkage = NULL;
2828 		else {
2829 			mp->mod_linkage = (void *)modinfop->mi_base;
2830 			ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
2831 		}
2832 
2833 		/*
2834 		 * DCS: bootstrapping code. If the driver is loaded
2835 		 * before root mount, it is assumed that the driver
2836 		 * may be used before mounting root. In order to
2837 		 * access mappings of global to local minor no.'s
2838 		 * during installation/open of the driver, we load
2839 		 * them into memory here while the BOP_interfaces
2840 		 * are still up.
2841 		 */
2842 		if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
2843 			retval = clboot_modload(mp);
2844 		}
2845 
2846 		kmem_free(modinfop, sizeof (struct modinfo));
2847 		(void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
2848 		retval = install_stubs_by_name(mp, mp->mod_modname);
2849 
2850 		/*
2851 		 * Now that the module is loaded, we need to give DTrace
2852 		 * a chance to notify its providers.  This is done via
2853 		 * the dtrace_modload function pointer.
2854 		 */
2855 		if (strcmp(mp->mod_modname, "dtrace") != 0) {
2856 			struct modctl *dmp = mod_hold_by_name("dtrace");
2857 
2858 			if (dmp != NULL && dtrace_modload != NULL)
2859 				(*dtrace_modload)(mp);
2860 
2861 			mod_release_mod(dmp);
2862 		}
2863 
2864 	} else {
2865 		/*
2866 		 * If load failed then we need to release any requisites
2867 		 * that we had established.
2868 		 */
2869 		ASSERT(retval);
2870 		mod_release_requisites(mp);
2871 
2872 		if (moddebug & MODDEBUG_ERRMSG)
2873 			printf("error loading '%s', error %d\n",
2874 			    mp->mod_filename, retval);
2875 	}
2876 	return (retval);
2877 }
2878 
2879 static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
2880 
2881 static void
2882 mod_unload(struct modctl *mp)
2883 {
2884 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
2885 	ASSERT(mp->mod_busy);
2886 	ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
2887 	    ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
2888 
2889 	if (moddebug & MODDEBUG_LOADMSG)
2890 		printf(unload_msg, mp->mod_modname,
2891 			mp->mod_id, mp->mod_loadcnt);
2892 
2893 	/*
2894 	 * If mod_ref is not zero, it means some modules might still refer
2895 	 * to this module. Then you can't unload this module right now.
2896 	 * Instead, set 1 to mod_delay_unload to notify the system of
2897 	 * unloading this module later when it's not required any more.
2898 	 */
2899 	if (mp->mod_ref > 0) {
2900 		mp->mod_delay_unload = 1;
2901 		if (moddebug & MODDEBUG_LOADMSG2) {
2902 			printf("module %s not unloaded,"
2903 			    " non-zero reference count (%d)",
2904 			    mp->mod_modname, mp->mod_ref);
2905 		}
2906 		return;
2907 	}
2908 
2909 	if (((mp->mod_loaded == 0) || mp->mod_installed) ||
2910 	    (mp->mod_ref || mp->mod_prim)) {
2911 		/*
2912 		 * A DEBUG kernel would ASSERT panic above, the code is broken
2913 		 * if we get this warning.
2914 		 */
2915 		cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
2916 		    mp->mod_filename, mp->mod_installed, mp->mod_loaded,
2917 		    mp->mod_ref);
2918 		return;
2919 	}
2920 
2921 	/* reset stub functions to call the binder again */
2922 	reset_stubs(mp);
2923 
2924 	/*
2925 	 * mark module as unloaded before the modctl structure is freed.
2926 	 * This is required not to reuse the modctl structure before
2927 	 * the module is marked as unloaded.
2928 	 */
2929 	mp->mod_loaded = 0;
2930 	mp->mod_linkage = NULL;
2931 
2932 	/* free the memory */
2933 	kobj_unload_module(mp);
2934 
2935 	if (mp->mod_delay_unload) {
2936 		mp->mod_delay_unload = 0;
2937 		if (moddebug & MODDEBUG_LOADMSG2) {
2938 			printf("deferred unload of module %s"
2939 			    " (id %d) successful",
2940 			    mp->mod_modname, mp->mod_id);
2941 		}
2942 	}
2943 
2944 	/* release hold on requisites */
2945 	mod_release_requisites(mp);
2946 
2947 	/*
2948 	 * Now that the module is gone, we need to give DTrace a chance to
2949 	 * remove any probes that it may have had in the module.  This is
2950 	 * done via the dtrace_modunload function pointer.
2951 	 */
2952 	if (strcmp(mp->mod_modname, "dtrace") != 0) {
2953 		struct modctl *dmp = mod_hold_by_name("dtrace");
2954 
2955 		if (dmp != NULL && dtrace_modunload != NULL)
2956 			(*dtrace_modunload)(mp);
2957 
2958 		mod_release_mod(dmp);
2959 	}
2960 }
2961 
2962 static int
2963 modinstall(struct modctl *mp)
2964 {
2965 	int val;
2966 	int (*func)(void);
2967 
2968 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
2969 	ASSERT(mp->mod_busy && mp->mod_loaded);
2970 
2971 	if (mp->mod_installed)
2972 		return (0);
2973 	/*
2974 	 * If mod_delay_unload is on, it means the system chose the deferred
2975 	 * unload for this module. Then you can't install this module until
2976 	 * it's unloaded from the system.
2977 	 */
2978 	if (mp->mod_delay_unload)
2979 		return (ENXIO);
2980 
2981 	if (moddebug & MODDEBUG_LOADMSG)
2982 		printf("installing %s, module id %d.\n",
2983 			mp->mod_modname, mp->mod_id);
2984 
2985 	ASSERT(mp->mod_mp != NULL);
2986 	if (mod_install_requisites(mp) != 0) {
2987 		/*
2988 		 * Note that we can't call mod_unload(mp) here since
2989 		 * if modinstall() was called by mod_install_requisites(),
2990 		 * we won't be able to hold the dependent modules
2991 		 * (otherwise there would be a deadlock).
2992 		 */
2993 		return (ENXIO);
2994 	}
2995 
2996 	if (moddebug & MODDEBUG_ERRMSG) {
2997 		printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
2998 			mp->mod_filename, mp->mod_id,
2999 			(void *)((struct module *)mp->mod_mp)->text,
3000 			(void *)((struct module *)mp->mod_mp)->data,
3001 			((struct module *)mp->mod_mp)->text_size,
3002 			((struct module *)mp->mod_mp)->data_size);
3003 	}
3004 
3005 	func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
3006 
3007 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
3008 		cmn_err(CE_WARN, "_init() not defined properly in %s",
3009 		    mp->mod_filename);
3010 		return (EFAULT);
3011 	}
3012 
3013 	if (moddebug & MODDEBUG_USERDEBUG) {
3014 		printf("breakpoint before calling %s:_init()\n",
3015 		    mp->mod_modname);
3016 		if (DEBUGGER_PRESENT)
3017 			debug_enter("_init");
3018 	}
3019 
3020 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
3021 	ASSERT(mp->mod_busy && mp->mod_loaded);
3022 	val = (*func)();		/* call _init */
3023 
3024 	if (moddebug & MODDEBUG_USERDEBUG)
3025 		printf("Returned from _init, val = %x\n", val);
3026 
3027 	if (val == 0) {
3028 		/*
3029 		 * Set the MODS_INSTALLED flag to enable this module
3030 		 * being called now.
3031 		 */
3032 		install_stubs(mp);
3033 		mp->mod_installed = 1;
3034 	} else if (moddebug & MODDEBUG_ERRMSG)
3035 		printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
3036 
3037 	return (val);
3038 }
3039 
3040 int	detach_driver_unconfig = 0;
3041 
3042 static int
3043 detach_driver(char *name)
3044 {
3045 	major_t major;
3046 	int error;
3047 
3048 	/*
3049 	 * If being called from mod_uninstall_all() then the appropriate
3050 	 * driver detaches (leaf only) have already been done.
3051 	 */
3052 	if (mod_in_autounload())
3053 		return (0);
3054 
3055 	major = ddi_name_to_major(name);
3056 	if (major == (major_t)-1)
3057 		return (0);
3058 
3059 	error = ndi_devi_unconfig_driver(ddi_root_node(),
3060 	    NDI_DETACH_DRIVER | detach_driver_unconfig, major);
3061 	return (error == NDI_SUCCESS ? 0 : -1);
3062 }
3063 
3064 static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
3065 
3066 static int
3067 moduninstall(struct modctl *mp)
3068 {
3069 	int status = 0;
3070 	int (*func)(void);
3071 
3072 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
3073 	ASSERT(mp->mod_busy);
3074 
3075 	/*
3076 	 * Verify that we need to do something and can uninstall the module.
3077 	 *
3078 	 * If we should not uninstall the module or if the module is not in
3079 	 * the correct state to start an uninstall we return EBUSY to prevent
3080 	 * us from progressing to mod_unload.  If the module has already been
3081 	 * uninstalled and unloaded we return EALREADY.
3082 	 */
3083 	if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
3084 		return (EBUSY);
3085 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
3086 		return (EALREADY);
3087 
3088 	/*
3089 	 * To avoid devinfo / module deadlock we must release this module
3090 	 * prior to initiating the detach_driver, otherwise the detach_driver
3091 	 * might deadlock on a devinfo node held by another thread
3092 	 * coming top down and involving the module we have locked.
3093 	 *
3094 	 * When we regrab the module we must reverify that it is OK
3095 	 * to proceed with the uninstall operation.
3096 	 */
3097 	mod_release_mod(mp);
3098 	status = detach_driver(mp->mod_modname);
3099 	(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
3100 
3101 	/* check detach status and reverify state with lock */
3102 	mutex_enter(&mod_lock);
3103 	if ((status != 0) || mp->mod_prim || mp->mod_ref) {
3104 		mutex_exit(&mod_lock);
3105 		return (EBUSY);
3106 	}
3107 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
3108 		mutex_exit(&mod_lock);
3109 		return (EALREADY);
3110 	}
3111 	mutex_exit(&mod_lock);
3112 
3113 	if (moddebug & MODDEBUG_LOADMSG2)
3114 		printf("uninstalling %s\n", mp->mod_modname);
3115 
3116 	/*
3117 	 * lookup _fini, return EBUSY if not defined.
3118 	 *
3119 	 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
3120 	 * detach(9E) - it allows bufctl addresses to be resolved.
3121 	 */
3122 	func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
3123 	if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
3124 	    (moddebug & MODDEBUG_FINI_EBUSY))
3125 		return (EBUSY);
3126 
3127 	/* verify that _fini is in this module */
3128 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
3129 		cmn_err(CE_WARN, "_fini() not defined properly in %s",
3130 		    mp->mod_filename);
3131 		return (EFAULT);
3132 	}
3133 
3134 	/* call _fini() */
3135 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
3136 	ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
3137 
3138 	status = (*func)();
3139 
3140 	if (status == 0) {
3141 		/* _fini returned success, the module is no longer installed */
3142 		if (moddebug & MODDEBUG_LOADMSG)
3143 			printf("uninstalled %s\n", mp->mod_modname);
3144 
3145 		/*
3146 		 * Even though we only set mod_installed to zero here, a zero
3147 		 * return value means we are commited to a code path were
3148 		 * mod_loaded will also end up as zero - we have no other
3149 		 * way to get the module data and bss back to the pre _init
3150 		 * state except a reload. To ensure this, after return,
3151 		 * mod_busy must stay set until mod_loaded is cleared.
3152 		 */
3153 		mp->mod_installed = 0;
3154 
3155 		/*
3156 		 * Clear the MODS_INSTALLED flag not to call functions
3157 		 * in the module directly from now on.
3158 		 */
3159 		uninstall_stubs(mp);
3160 	} else {
3161 		if (moddebug & MODDEBUG_USERDEBUG)
3162 			printf(finiret_msg, mp->mod_filename, status);
3163 		/*
3164 		 * By definition _fini is only allowed to return EBUSY or the
3165 		 * result of mod_remove (EBUSY or EINVAL).  In the off chance
3166 		 * that a driver returns EALREADY we convert this to EINVAL
3167 		 * since to our caller EALREADY means module was already
3168 		 * removed.
3169 		 */
3170 		if (status == EALREADY)
3171 			status = EINVAL;
3172 	}
3173 
3174 	return (status);
3175 }
3176 
3177 /*
3178  * Uninstall all modules.
3179  */
3180 static void
3181 mod_uninstall_all(void)
3182 {
3183 	struct modctl	*mp;
3184 	modid_t		modid = 0;
3185 
3186 	/* synchronize with any active modunload_disable() */
3187 	modunload_begin();
3188 
3189 	/* mark this thread as doing autounloading */
3190 	(void) tsd_set(mod_autounload_key, (void *)1);
3191 
3192 	(void) devfs_clean(ddi_root_node(), NULL, 0);
3193 	(void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
3194 
3195 	while ((mp = mod_hold_next_by_id(modid)) != NULL) {
3196 		modid = mp->mod_id;
3197 		/*
3198 		 * Skip modules with the MOD_NOAUTOUNLOAD flag set
3199 		 */
3200 		if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
3201 			mod_release_mod(mp);
3202 			continue;
3203 		}
3204 
3205 		if (moduninstall(mp) == 0) {
3206 			mod_unload(mp);
3207 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
3208 		}
3209 		mod_release_mod(mp);
3210 	}
3211 
3212 	(void) tsd_set(mod_autounload_key, NULL);
3213 	modunload_end();
3214 }
3215 
3216 /* wait for unloads that have begun before registering disable */
3217 void
3218 modunload_disable(void)
3219 {
3220 	mutex_enter(&modunload_wait_mutex);
3221 	while (modunload_active_count) {
3222 		modunload_wait++;
3223 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3224 		modunload_wait--;
3225 	}
3226 	modunload_disable_count++;
3227 	mutex_exit(&modunload_wait_mutex);
3228 }
3229 
3230 /* mark end of disable and signal waiters */
3231 void
3232 modunload_enable(void)
3233 {
3234 	mutex_enter(&modunload_wait_mutex);
3235 	modunload_disable_count--;
3236 	if ((modunload_disable_count == 0) && modunload_wait)
3237 		cv_broadcast(&modunload_wait_cv);
3238 	mutex_exit(&modunload_wait_mutex);
3239 }
3240 
3241 /* wait for disables to complete before begining unload */
3242 void
3243 modunload_begin()
3244 {
3245 	mutex_enter(&modunload_wait_mutex);
3246 	while (modunload_disable_count) {
3247 		modunload_wait++;
3248 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3249 		modunload_wait--;
3250 	}
3251 	modunload_active_count++;
3252 	mutex_exit(&modunload_wait_mutex);
3253 }
3254 
3255 /* mark end of unload and signal waiters */
3256 void
3257 modunload_end()
3258 {
3259 	mutex_enter(&modunload_wait_mutex);
3260 	modunload_active_count--;
3261 	if ((modunload_active_count == 0) && modunload_wait)
3262 		cv_broadcast(&modunload_wait_cv);
3263 	mutex_exit(&modunload_wait_mutex);
3264 }
3265 
3266 void
3267 mod_uninstall_daemon(void)
3268 {
3269 	callb_cpr_t	cprinfo;
3270 	clock_t		ticks = 0;
3271 
3272 	mod_aul_thread = curthread;
3273 
3274 	CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
3275 	for (;;) {
3276 		mutex_enter(&mod_uninstall_lock);
3277 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3278 		/*
3279 		 * In DEBUG kernels, unheld drivers are uninstalled periodically
3280 		 * every mod_uninstall_interval seconds.  Periodic uninstall can
3281 		 * be disabled by setting mod_uninstall_interval to 0 which is
3282 		 * the default for a non-DEBUG kernel.
3283 		 */
3284 		if (mod_uninstall_interval) {
3285 			ticks = ddi_get_lbolt() +
3286 				drv_usectohz(mod_uninstall_interval * 1000000);
3287 			(void) cv_timedwait(&mod_uninstall_cv,
3288 				&mod_uninstall_lock, ticks);
3289 		} else {
3290 			cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
3291 		}
3292 		/*
3293 		 * The whole daemon is safe for CPR except we don't want
3294 		 * the daemon to run if FREEZE is issued and this daemon
3295 		 * wakes up from the cv_wait above. In this case, it'll be
3296 		 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
3297 		 *
3298 		 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
3299 		 * mod_uninstall_lock is used to protect cprinfo and
3300 		 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
3301 		 * called.
3302 		 */
3303 		CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
3304 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3305 		mutex_exit(&mod_uninstall_lock);
3306 		if ((modunload_disable_count == 0) &&
3307 		    ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
3308 			mod_uninstall_all();
3309 		}
3310 	}
3311 }
3312 
3313 /*
3314  * Unload all uninstalled modules.
3315  */
3316 void
3317 modreap(void)
3318 {
3319 	mutex_enter(&mod_uninstall_lock);
3320 	cv_broadcast(&mod_uninstall_cv);
3321 	mutex_exit(&mod_uninstall_lock);
3322 }
3323 
3324 /*
3325  * Hold the specified module. This is the module holding primitive.
3326  *
3327  * If MOD_LOCK_HELD then the caller already holds the mod_lock.
3328  *
3329  * Return values:
3330  *	 0 ==> the module is held
3331  *	 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
3332  *		to determine how to retry.
3333  */
3334 int
3335 mod_hold_by_modctl(struct modctl *mp, int f)
3336 {
3337 	ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
3338 	    ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
3339 	    (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
3340 	ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
3341 	    ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
3342 	    (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
3343 	ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
3344 
3345 	if (f & MOD_LOCK_NOT_HELD)
3346 		mutex_enter(&mod_lock);
3347 
3348 	while (mp->mod_busy) {
3349 		mp->mod_want = 1;
3350 		cv_wait(&mod_cv, &mod_lock);
3351 		/*
3352 		 * Module may be unloaded by daemon.
3353 		 * Nevertheless, modctl structure is still in linked list
3354 		 * (i.e., off &modules), not freed!
3355 		 * Caller is not supposed to assume "mp" is valid, but there
3356 		 * is no reasonable way to detect this but using
3357 		 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
3358 		 *   (or similar check depending on calling context)
3359 		 * DON'T free modctl structure, it will be very very
3360 		 * problematic.
3361 		 */
3362 		if (f & MOD_WAIT_ONCE) {
3363 			if (f & MOD_LOCK_NOT_HELD)
3364 				mutex_exit(&mod_lock);
3365 			return (1);	/* caller decides how to retry */
3366 		}
3367 	}
3368 
3369 	mp->mod_busy = 1;
3370 	mp->mod_inprogress_thread =
3371 	    (curthread == NULL ? (kthread_id_t)-1 : curthread);
3372 
3373 	if (f & MOD_LOCK_NOT_HELD)
3374 		mutex_exit(&mod_lock);
3375 	return (0);
3376 }
3377 
3378 static struct modctl *
3379 mod_hold_by_name_common(struct modctl *dep, const char *filename)
3380 {
3381 	const char	*modname;
3382 	struct modctl	*mp;
3383 	char		*curname, *newname;
3384 	int		found = 0;
3385 
3386 	mutex_enter(&mod_lock);
3387 
3388 	if ((modname = strrchr(filename, '/')) == NULL)
3389 		modname = filename;
3390 	else
3391 		modname++;
3392 
3393 	mp = &modules;
3394 	do {
3395 		if (strcmp(modname, mp->mod_modname) == 0) {
3396 			found = 1;
3397 			break;
3398 		}
3399 	} while ((mp = mp->mod_next) != &modules);
3400 
3401 	if (found == 0) {
3402 		mp = allocate_modp(filename, modname);
3403 		modadd(mp);
3404 	}
3405 
3406 	/*
3407 	 * if dep is not NULL, set the mp in mod_requisite_loading for
3408 	 * the module circular dependency check. This field is used in
3409 	 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
3410 	 */
3411 	if (dep != NULL) {
3412 		ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
3413 		dep->mod_requisite_loading = mp;
3414 	}
3415 
3416 	/*
3417 	 * If the module was held, then it must be us who has it held.
3418 	 */
3419 	if (mod_circdep(mp))
3420 		mp = NULL;
3421 	else {
3422 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
3423 
3424 		/*
3425 		 * If the name hadn't been set or has changed, allocate
3426 		 * space and set it.  Free space used by previous name.
3427 		 *
3428 		 * Do not change the name of primary modules, for primary
3429 		 * modules the mod_filename was allocated in standalone mode:
3430 		 * it is illegal to kobj_alloc in standalone mode and kobj_free
3431 		 * in non-standalone mode.
3432 		 */
3433 		curname = mp->mod_filename;
3434 		if (curname == NULL ||
3435 		    ((mp->mod_prim == 0) &&
3436 		    (curname != filename) &&
3437 		    (modname != filename) &&
3438 		    (strcmp(curname, filename) != 0))) {
3439 			newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
3440 			(void) strcpy(newname, filename);
3441 			mp->mod_filename = newname;
3442 			if (curname != NULL)
3443 				kobj_free(curname, strlen(curname) + 1);
3444 		}
3445 	}
3446 
3447 	mutex_exit(&mod_lock);
3448 	if (mp && moddebug & MODDEBUG_LOADMSG2)
3449 		printf("Holding %s\n", mp->mod_filename);
3450 	if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
3451 		printf("circular dependency loading %s\n", filename);
3452 	return (mp);
3453 }
3454 
3455 static struct modctl *
3456 mod_hold_by_name_requisite(struct modctl *dep, char *filename)
3457 {
3458 	return (mod_hold_by_name_common(dep, filename));
3459 }
3460 
3461 struct modctl *
3462 mod_hold_by_name(const char *filename)
3463 {
3464 	return (mod_hold_by_name_common(NULL, filename));
3465 }
3466 
3467 struct modctl *
3468 mod_hold_by_id(modid_t modid)
3469 {
3470 	struct modctl	*mp;
3471 	int		found = 0;
3472 
3473 	mutex_enter(&mod_lock);
3474 	mp = &modules;
3475 	do {
3476 		if (mp->mod_id == modid) {
3477 			found = 1;
3478 			break;
3479 		}
3480 	} while ((mp = mp->mod_next) != &modules);
3481 
3482 	if ((found == 0) || mod_circdep(mp))
3483 		mp = NULL;
3484 	else
3485 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
3486 
3487 	mutex_exit(&mod_lock);
3488 	return (mp);
3489 }
3490 
3491 static struct modctl *
3492 mod_hold_next_by_id(modid_t modid)
3493 {
3494 	struct modctl	*mp;
3495 	int		found = 0;
3496 
3497 	if (modid < -1)
3498 		return (NULL);
3499 
3500 	mutex_enter(&mod_lock);
3501 
3502 	mp = &modules;
3503 	do {
3504 		if (mp->mod_id > modid) {
3505 			found = 1;
3506 			break;
3507 		}
3508 	} while ((mp = mp->mod_next) != &modules);
3509 
3510 	if ((found == 0) || mod_circdep(mp))
3511 		mp = NULL;
3512 	else
3513 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
3514 
3515 	mutex_exit(&mod_lock);
3516 	return (mp);
3517 }
3518 
3519 static void
3520 mod_release(struct modctl *mp)
3521 {
3522 	ASSERT(MUTEX_HELD(&mod_lock));
3523 	ASSERT(mp->mod_busy);
3524 
3525 	mp->mod_busy = 0;
3526 	mp->mod_inprogress_thread = NULL;
3527 	if (mp->mod_want) {
3528 		mp->mod_want = 0;
3529 		cv_broadcast(&mod_cv);
3530 	}
3531 }
3532 
3533 void
3534 mod_release_mod(struct modctl *mp)
3535 {
3536 	if (moddebug & MODDEBUG_LOADMSG2)
3537 		printf("Releasing %s\n", mp->mod_filename);
3538 	mutex_enter(&mod_lock);
3539 	mod_release(mp);
3540 	mutex_exit(&mod_lock);
3541 }
3542 
3543 modid_t
3544 mod_name_to_modid(char *filename)
3545 {
3546 	char		*modname;
3547 	struct modctl	*mp;
3548 
3549 	mutex_enter(&mod_lock);
3550 
3551 	if ((modname = strrchr(filename, '/')) == NULL)
3552 		modname = filename;
3553 	else
3554 		modname++;
3555 
3556 	mp = &modules;
3557 	do {
3558 		if (strcmp(modname, mp->mod_modname) == 0) {
3559 			mutex_exit(&mod_lock);
3560 			return (mp->mod_id);
3561 		}
3562 	} while ((mp = mp->mod_next) != &modules);
3563 
3564 	mutex_exit(&mod_lock);
3565 	return (-1);
3566 }
3567 
3568 
3569 int
3570 mod_remove_by_name(char *name)
3571 {
3572 	struct modctl *mp;
3573 	int retval;
3574 
3575 	mp = mod_hold_by_name(name);
3576 
3577 	if (mp == NULL)
3578 		return (EINVAL);
3579 
3580 	if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
3581 		/*
3582 		 * Do not unload forceloaded modules
3583 		 */
3584 		mod_release_mod(mp);
3585 		return (0);
3586 	}
3587 
3588 	if ((retval = moduninstall(mp)) == 0) {
3589 		mod_unload(mp);
3590 		CPU_STATS_ADDQ(CPU, sys, modunload, 1);
3591 	} else if (retval == EALREADY)
3592 		retval = 0;		/* already unloaded, not an error */
3593 	mod_release_mod(mp);
3594 	return (retval);
3595 }
3596 
3597 /*
3598  * Record that module "dep" is dependent on module "on_mod."
3599  */
3600 static void
3601 mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
3602 {
3603 	struct modctl_list **pmlnp;	/* previous next pointer */
3604 	struct modctl_list *mlp;
3605 	struct modctl_list *new;
3606 
3607 	ASSERT(dependent->mod_busy && on_mod->mod_busy);
3608 	mutex_enter(&mod_lock);
3609 
3610 	/*
3611 	 * Search dependent's requisite list to see if on_mod is recorded.
3612 	 * List is ordered by id.
3613 	 */
3614 	for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
3615 	    mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
3616 		if (mlp->modl_modp->mod_id >= on_mod->mod_id)
3617 			break;
3618 
3619 	/* Create and insert if not already recorded */
3620 	if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
3621 		new = kobj_zalloc(sizeof (*new), KM_SLEEP);
3622 		new->modl_modp = on_mod;
3623 		new->modl_next = mlp;
3624 		*pmlnp = new;
3625 
3626 		/*
3627 		 * Increment the mod_ref count in our new requisite module.
3628 		 * This is what keeps a module that has other modules
3629 		 * which are dependent on it from being uninstalled and
3630 		 * unloaded. "on_mod"'s mod_ref count decremented in
3631 		 * mod_release_requisites when the "dependent" module
3632 		 * unload is complete.  "on_mod" must be loaded, but may not
3633 		 * yet be installed.
3634 		 */
3635 		on_mod->mod_ref++;
3636 		ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
3637 	}
3638 
3639 	mutex_exit(&mod_lock);
3640 }
3641 
3642 /*
3643  * release the hold associated with mod_make_requisite mod_ref++
3644  * as part of unload.
3645  */
3646 void
3647 mod_release_requisites(struct modctl *modp)
3648 {
3649 	struct modctl_list *modl;
3650 	struct modctl_list *next;
3651 	struct modctl *req;
3652 	struct modctl_list *start = NULL, *mod_garbage;
3653 
3654 	ASSERT(modp->mod_busy);
3655 	ASSERT(!MUTEX_HELD(&mod_lock));
3656 
3657 	mutex_enter(&mod_lock);		/* needed for manipulation of req */
3658 	for (modl = modp->mod_requisites; modl; modl = next) {
3659 		next = modl->modl_next;
3660 		req = modl->modl_modp;
3661 		ASSERT(req->mod_ref >= 1 && req->mod_loaded);
3662 		req->mod_ref--;
3663 
3664 		/*
3665 		 * Check if the module has to be unloaded or not.
3666 		 */
3667 		if (req->mod_ref == 0 && req->mod_delay_unload) {
3668 			struct modctl_list *new;
3669 			/*
3670 			 * Allocate the modclt_list holding the garbage
3671 			 * module which should be unloaded later.
3672 			 */
3673 			new = kobj_zalloc(sizeof (struct modctl_list),
3674 			    KM_SLEEP);
3675 			new->modl_modp = req;
3676 
3677 			if (start == NULL)
3678 				mod_garbage = start = new;
3679 			else {
3680 				mod_garbage->modl_next = new;
3681 				mod_garbage = new;
3682 			}
3683 		}
3684 
3685 		/* free the list as we go */
3686 		kobj_free(modl, sizeof (*modl));
3687 	}
3688 	modp->mod_requisites = NULL;
3689 	mutex_exit(&mod_lock);
3690 
3691 	/*
3692 	 * Unload the garbage modules.
3693 	 */
3694 	for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
3695 		struct modctl_list *old = mod_garbage;
3696 		struct modctl *mp = mod_garbage->modl_modp;
3697 		ASSERT(mp != NULL);
3698 
3699 		/*
3700 		 * Hold this module until it's unloaded completely.
3701 		 */
3702 		(void) mod_hold_by_modctl(mp,
3703 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
3704 		/*
3705 		 * Check if the module is not unloaded yet and nobody requires
3706 		 * the module. If it's unloaded already or somebody still
3707 		 * requires the module, don't unload it now.
3708 		 */
3709 		if (mp->mod_loaded && mp->mod_ref == 0)
3710 			mod_unload(mp);
3711 		ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
3712 		    (mp->mod_ref > 0));
3713 		mod_release_mod(mp);
3714 
3715 		mod_garbage = mod_garbage->modl_next;
3716 		kobj_free(old, sizeof (struct modctl_list));
3717 	}
3718 }
3719 
3720 /*
3721  * Process dependency of the module represented by "dep" on the
3722  * module named by "on."
3723  *
3724  * Called from kobj_do_dependents() to load a module "on" on which
3725  * "dep" depends.
3726  */
3727 struct modctl *
3728 mod_load_requisite(struct modctl *dep, char *on)
3729 {
3730 	struct modctl *on_mod;
3731 	int retval;
3732 
3733 	if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
3734 		mod_make_requisite(dep, on_mod);
3735 	} else if (moddebug & MODDEBUG_ERRMSG) {
3736 		printf("error processing %s on which module %s depends\n",
3737 			on, dep->mod_modname);
3738 	}
3739 	return (on_mod);
3740 }
3741 
3742 static int
3743 mod_install_requisites(struct modctl *modp)
3744 {
3745 	struct modctl_list *modl;
3746 	struct modctl *req;
3747 	int status = 0;
3748 
3749 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
3750 	ASSERT(modp->mod_busy);
3751 
3752 	for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
3753 		req = modl->modl_modp;
3754 		(void) mod_hold_by_modctl(req,
3755 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
3756 		status = modinstall(req);
3757 		mod_release_mod(req);
3758 
3759 		if (status != 0)
3760 			break;
3761 	}
3762 	return (status);
3763 }
3764 
3765 /*
3766  * returns 1 if this thread is doing autounload, 0 otherwise.
3767  * see mod_uninstall_all.
3768  */
3769 int
3770 mod_in_autounload()
3771 {
3772 	return ((int)(uintptr_t)tsd_get(mod_autounload_key));
3773 }
3774 
3775 /*
3776  * gmatch adapted from libc, stripping the wchar stuff
3777  */
3778 #define	popchar(p, c) \
3779 	c = *p++; \
3780 	if (c == 0) \
3781 		return (0);
3782 
3783 int
3784 gmatch(const char *s, const char *p)
3785 {
3786 	int c, sc;
3787 	int ok, lc, notflag;
3788 
3789 	sc = *s++;
3790 	c = *p++;
3791 	if (c == 0)
3792 		return (sc == c);	/* nothing matches nothing */
3793 
3794 	switch (c) {
3795 	case '\\':
3796 		/* skip to quoted character */
3797 		popchar(p, c)
3798 		/*FALLTHRU*/
3799 
3800 	default:
3801 		/* straight comparison */
3802 		if (c != sc)
3803 			return (0);
3804 		/*FALLTHRU*/
3805 
3806 	case '?':
3807 		/* first char matches, move to remainder */
3808 		return (sc != '\0' ? gmatch(s, p) : 0);
3809 
3810 
3811 	case '*':
3812 		while (*p == '*')
3813 			p++;
3814 
3815 		/* * matches everything */
3816 		if (*p == 0)
3817 			return (1);
3818 
3819 		/* undo skip at the beginning & iterate over substrings */
3820 		--s;
3821 		while (*s) {
3822 			if (gmatch(s, p))
3823 				return (1);
3824 			s++;
3825 		}
3826 		return (0);
3827 
3828 	case '[':
3829 		/* match any char within [] */
3830 		if (sc == 0)
3831 			return (0);
3832 
3833 		ok = lc = notflag = 0;
3834 
3835 		if (*p == '!') {
3836 			notflag = 1;
3837 			p++;
3838 		}
3839 		popchar(p, c)
3840 
3841 		do {
3842 			if (c == '-' && lc && *p != ']') {
3843 				/* test sc against range [c1-c2] */
3844 				popchar(p, c)
3845 				if (c == '\\') {
3846 					popchar(p, c)
3847 				}
3848 
3849 				if (notflag) {
3850 					/* return 0 on mismatch */
3851 					if (lc <= sc && sc <= c)
3852 						return (0);
3853 					ok++;
3854 				} else if (lc <= sc && sc <= c) {
3855 					ok++;
3856 				}
3857 				/* keep going, may get a match next */
3858 			} else if (c == '\\') {
3859 				/* skip to quoted character */
3860 				popchar(p, c)
3861 			}
3862 			lc = c;
3863 			if (notflag) {
3864 				if (sc == lc)
3865 					return (0);
3866 				ok++;
3867 			} else if (sc == lc) {
3868 				ok++;
3869 			}
3870 			popchar(p, c)
3871 		} while (c != ']');
3872 
3873 		/* recurse on remainder of string */
3874 		return (ok ? gmatch(s, p) : 0);
3875 	}
3876 	/*NOTREACHED*/
3877 }
3878 
3879 
3880 /*
3881  * Get default perm for device from /etc/minor_perm. Return 0 if match found.
3882  *
3883  * Pure wild-carded patterns are handled separately so the ordering of
3884  * these patterns doesn't matter.  We're still dependent on ordering
3885  * however as the first matching entry is the one returned.
3886  * Not ideal but all existing examples and usage do imply this
3887  * ordering implicitly.
3888  *
3889  * Drivers using the clone driver are always good for some entertainment.
3890  * Clone nodes under pseudo have the form clone@0:<driver>.  Some minor
3891  * perm entries have the form clone:<driver>, others use <driver>:*
3892  * Examples are clone:llc1 vs. llc2:*, for example.
3893  *
3894  * Minor perms in the clone:<driver> form are mapped to the drivers's
3895  * mperm list, not the clone driver, as wildcard entries for clone
3896  * reference only.  In other words, a clone wildcard will match
3897  * references for clone@0:<driver> but never <driver>@<minor>.
3898  *
3899  * Additional minor perms in the standard form are also supported,
3900  * for mixed usage, ie a node with an entry clone:<driver> could
3901  * provide further entries <driver>:<minor>.
3902  *
3903  * Finally, some uses of clone use an alias as the minor name rather
3904  * than the driver name, with the alias as the minor perm entry.
3905  * This case is handled by attaching the driver to bring its
3906  * minor list into existence, then discover the alias via DDI_ALIAS.
3907  * The clone device's minor perm list can then be searched for
3908  * that alias.
3909  */
3910 
3911 static int
3912 dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
3913 {
3914 	major_t major;
3915 	struct devnames *dnp;
3916 	mperm_t *mp;
3917 	char *alias = NULL;
3918 	dev_info_t *cdevi;
3919 	struct ddi_minor_data *dmd;
3920 
3921 	major = ddi_name_to_major(minor_name);
3922 
3923 	ASSERT(dip == clone_dip);
3924 	ASSERT(major != (major_t)-1);
3925 
3926 	/*
3927 	 * Attach the driver named by the minor node, then
3928 	 * search its first instance's minor list for an
3929 	 * alias node.
3930 	 */
3931 	if (ddi_hold_installed_driver(major) == NULL)
3932 		return (1);
3933 
3934 	dnp = &devnamesp[major];
3935 	LOCK_DEV_OPS(&dnp->dn_lock);
3936 
3937 	if ((cdevi = dnp->dn_head) != NULL) {
3938 		mutex_enter(&DEVI(cdevi)->devi_lock);
3939 		for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
3940 			if (dmd->type == DDM_ALIAS) {
3941 				alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
3942 				break;
3943 			}
3944 		}
3945 		mutex_exit(&DEVI(cdevi)->devi_lock);
3946 	}
3947 
3948 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3949 	ddi_rele_driver(major);
3950 
3951 	if (alias == NULL) {
3952 		if (moddebug & MODDEBUG_MINORPERM)
3953 			cmn_err(CE_CONT, "dev_minorperm: "
3954 			    "no alias for %s\n", minor_name);
3955 		return (1);
3956 	}
3957 
3958 	major = ddi_driver_major(clone_dip);
3959 	dnp = &devnamesp[major];
3960 	LOCK_DEV_OPS(&dnp->dn_lock);
3961 
3962 	/*
3963 	 * Go through the clone driver's mperm list looking
3964 	 * for a match for the specified alias.
3965 	 */
3966 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
3967 		if (strcmp(alias, mp->mp_minorname) == 0) {
3968 			break;
3969 		}
3970 	}
3971 
3972 	if (mp) {
3973 		if (moddebug & MODDEBUG_MP_MATCH) {
3974 			cmn_err(CE_CONT,
3975 			    "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
3976 			    minor_name, alias, mp->mp_mode,
3977 			    mp->mp_uid, mp->mp_gid);
3978 		}
3979 		rmp->mp_uid = mp->mp_uid;
3980 		rmp->mp_gid = mp->mp_gid;
3981 		rmp->mp_mode = mp->mp_mode;
3982 	}
3983 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3984 
3985 	kmem_free(alias, strlen(alias)+1);
3986 
3987 	return (mp == NULL);
3988 }
3989 
3990 int
3991 dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
3992 {
3993 	major_t major;
3994 	char *minor_name;
3995 	struct devnames *dnp;
3996 	mperm_t *mp;
3997 	int is_clone = 0;
3998 
3999 	if (!minorperm_loaded) {
4000 		if (moddebug & MODDEBUG_MINORPERM)
4001 			cmn_err(CE_CONT,
4002 			    "%s: minor perm not yet loaded\n", name);
4003 		return (1);
4004 	}
4005 
4006 	minor_name = strchr(name, ':');
4007 	if (minor_name == NULL)
4008 		return (1);
4009 	minor_name++;
4010 
4011 	/*
4012 	 * If it's the clone driver, search the driver as named
4013 	 * by the minor.  All clone minor perm entries other than
4014 	 * alias nodes are actually installed on the real driver's list.
4015 	 */
4016 	if (dip == clone_dip) {
4017 		major = ddi_name_to_major(minor_name);
4018 		if (major == (major_t)-1) {
4019 			if (moddebug & MODDEBUG_MINORPERM)
4020 				cmn_err(CE_CONT, "dev_minorperm: "
4021 				    "%s: no such driver\n", minor_name);
4022 			return (1);
4023 		}
4024 		is_clone = 1;
4025 	} else {
4026 		major = ddi_driver_major(dip);
4027 		ASSERT(major != (major_t)-1);
4028 	}
4029 
4030 	dnp = &devnamesp[major];
4031 	LOCK_DEV_OPS(&dnp->dn_lock);
4032 
4033 	/*
4034 	 * Go through the driver's mperm list looking for
4035 	 * a match for the specified minor.  If there's
4036 	 * no matching pattern, use the wild card.
4037 	 * Defer to the clone wild for clone if specified,
4038 	 * otherwise fall back to the normal form.
4039 	 */
4040 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
4041 		if (gmatch(minor_name, mp->mp_minorname) != 0) {
4042 			break;
4043 		}
4044 	}
4045 	if (mp == NULL) {
4046 		if (is_clone)
4047 			mp = dnp->dn_mperm_clone;
4048 		if (mp == NULL)
4049 			mp = dnp->dn_mperm_wild;
4050 	}
4051 
4052 	if (mp) {
4053 		if (moddebug & MODDEBUG_MP_MATCH) {
4054 			cmn_err(CE_CONT,
4055 			    "minor perm defaults: %s %s 0%o %d %d\n",
4056 			    name, mp->mp_minorname, mp->mp_mode,
4057 			    mp->mp_uid, mp->mp_gid);
4058 		}
4059 		rmp->mp_uid = mp->mp_uid;
4060 		rmp->mp_gid = mp->mp_gid;
4061 		rmp->mp_mode = mp->mp_mode;
4062 	}
4063 	UNLOCK_DEV_OPS(&dnp->dn_lock);
4064 
4065 	/*
4066 	 * If no match can be found for a clone node,
4067 	 * search for a possible match for an alias.
4068 	 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
4069 	 * with minor perm entry clone:ptmx.
4070 	 */
4071 	if (mp == NULL && is_clone) {
4072 		return (dev_alias_minorperm(dip, minor_name, rmp));
4073 	}
4074 
4075 	return (mp == NULL);
4076 }
4077 
4078 /*
4079  * dynamicaly reference load a dl module/library, returning handle
4080  */
4081 /*ARGSUSED*/
4082 ddi_modhandle_t
4083 ddi_modopen(const char *modname, int mode, int *errnop)
4084 {
4085 	char		*subdir;
4086 	char		*mod;
4087 	int		subdirlen;
4088 	struct modctl	*hmodp = NULL;
4089 	int		retval = EINVAL;
4090 
4091 	ASSERT(modname && (mode == KRTLD_MODE_FIRST));
4092 	if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
4093 		goto out;
4094 
4095 	/* find optional first '/' in modname */
4096 	mod = strchr(modname, '/');
4097 	if (mod != strrchr(modname, '/'))
4098 		goto out;		/* only one '/' is legal */
4099 
4100 	if (mod) {
4101 		/* for subdir string without modification to argument */
4102 		mod++;
4103 		subdirlen = mod - modname;
4104 		subdir = kmem_alloc(subdirlen, KM_SLEEP);
4105 		(void) strlcpy(subdir, modname, subdirlen);
4106 	} else {
4107 		subdirlen = 0;
4108 		subdir = "misc";
4109 		mod = (char *)modname;
4110 	}
4111 
4112 	/* reference load with errno return value */
4113 	retval = modrload(subdir, mod, &hmodp);
4114 
4115 	if (subdirlen)
4116 		kmem_free(subdir, subdirlen);
4117 
4118 out:	if (errnop)
4119 		*errnop = retval;
4120 
4121 	if (moddebug & MODDEBUG_DDI_MOD)
4122 		printf("ddi_modopen %s mode %x: %s %p %d\n",
4123 		    modname ? modname : "<unknown>", mode,
4124 		    hmodp ? hmodp->mod_filename : "<unknown>",
4125 		    (void *)hmodp, retval);
4126 
4127 	return ((ddi_modhandle_t)hmodp);
4128 }
4129 
4130 /* lookup "name" in open dl module/library */
4131 void *
4132 ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
4133 {
4134 	struct modctl	*hmodp = (struct modctl *)h;
4135 	void		*f;
4136 	int		retval;
4137 
4138 	ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
4139 	if ((hmodp == NULL) || (name == NULL) ||
4140 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
4141 		f = NULL;
4142 		retval = EINVAL;
4143 	} else {
4144 		f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
4145 		if (f)
4146 			retval = 0;
4147 		else
4148 			retval = ENOTSUP;
4149 	}
4150 
4151 	if (moddebug & MODDEBUG_DDI_MOD)
4152 		printf("ddi_modsym in %s of %s: %d %p\n",
4153 		    hmodp ? hmodp->mod_modname : "<unknown>",
4154 		    name ? name : "<unknown>", retval, f);
4155 
4156 	if (errnop)
4157 		*errnop = retval;
4158 	return (f);
4159 }
4160 
4161 /* dynamic (un)reference unload of an open dl module/library */
4162 int
4163 ddi_modclose(ddi_modhandle_t h)
4164 {
4165 	struct modctl	*hmodp = (struct modctl *)h;
4166 	struct modctl	*modp = NULL;
4167 	int		retval;
4168 
4169 	ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
4170 	if ((hmodp == NULL) ||
4171 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
4172 		retval = EINVAL;
4173 		goto out;
4174 	}
4175 
4176 	retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
4177 	if (retval == EBUSY)
4178 		retval = 0;	/* EBUSY is not an error */
4179 
4180 	if (retval == 0) {
4181 		ASSERT(hmodp == modp);
4182 		if (hmodp != modp)
4183 			retval = EINVAL;
4184 	}
4185 
4186 out:	if (moddebug & MODDEBUG_DDI_MOD)
4187 		printf("ddi_modclose %s: %d\n",
4188 		    hmodp ? hmodp->mod_modname : "<unknown>", retval);
4189 
4190 	return (retval);
4191 }
4192