xref: /titanic_44/usr/src/uts/common/os/modctl.c (revision facf4a8d7b59fde89a8662b4f4c73a758e6c402c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*facf4a8dSllai1  * Common Development and Distribution License (the "License").
6*facf4a8dSllai1  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217aec1d6eScindi 
227c478bd9Sstevel@tonic-gate /*
23a7aa4df7Scth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * modctl system call for loadable module support.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/user.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/exec.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/conf.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
427c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
497c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
507c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
527c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
537c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
547c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
557c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
567c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
597c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
607c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
617c478bd9Sstevel@tonic-gate #include <sys/devops.h>
627c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
637c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
647c478bd9Sstevel@tonic-gate #include <sys/callb.h>
657c478bd9Sstevel@tonic-gate #include <sys/debug.h>
667c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
677c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
687c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
697c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
707c478bd9Sstevel@tonic-gate #include <sys/instance.h>
717c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
727c478bd9Sstevel@tonic-gate #include <sys/modhash_impl.h>
737c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
747c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
757c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
767c478bd9Sstevel@tonic-gate #include <sys/console.h>
777c478bd9Sstevel@tonic-gate #include <sys/policy.h>
787c478bd9Sstevel@tonic-gate #include <ipp/ipp_impl.h>
797c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
807c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
81*facf4a8dSllai1 #include <sys/fs/sdev_node.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static int		mod_circdep(struct modctl *);
847c478bd9Sstevel@tonic-gate static int		modinfo(modid_t, struct modinfo *);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void		mod_uninstall_all(void);
877c478bd9Sstevel@tonic-gate static int		mod_getinfo(struct modctl *, struct modinfo *);
887aec1d6eScindi static struct modctl	*allocate_modp(const char *, const char *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int		mod_load(struct modctl *, int);
917c478bd9Sstevel@tonic-gate static void		mod_unload(struct modctl *);
927c478bd9Sstevel@tonic-gate static int		modinstall(struct modctl *);
937c478bd9Sstevel@tonic-gate static int		moduninstall(struct modctl *);
947c478bd9Sstevel@tonic-gate 
957aec1d6eScindi static struct modctl	*mod_hold_by_name_common(struct modctl *, const char *);
967c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_next_by_id(modid_t);
977c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_loaded_mod(struct modctl *, char *, int *);
987c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_installed_mod(char *, int, int *);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void		mod_release(struct modctl *);
1017c478bd9Sstevel@tonic-gate static void		mod_make_requisite(struct modctl *, struct modctl *);
1027c478bd9Sstevel@tonic-gate static int		mod_install_requisites(struct modctl *);
1037c478bd9Sstevel@tonic-gate static void		check_esc_sequences(char *, char *);
1047c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_by_name_requisite(struct modctl *, char *);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * module loading thread control structure. Calls to kobj_load_module()() are
1087c478bd9Sstevel@tonic-gate  * handled off to a separate thead using this structure.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate struct loadmt {
1117c478bd9Sstevel@tonic-gate 	ksema_t		sema;
1127c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
1137c478bd9Sstevel@tonic-gate 	int		usepath;
1147c478bd9Sstevel@tonic-gate 	kthread_t	*owner;
1157c478bd9Sstevel@tonic-gate 	int		retval;
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static void	modload_thread(struct loadmt *);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate kcondvar_t	mod_cv;
1217c478bd9Sstevel@tonic-gate kcondvar_t	mod_uninstall_cv;	/* Communication between swapper */
1227c478bd9Sstevel@tonic-gate 					/* and the uninstall daemon. */
1237c478bd9Sstevel@tonic-gate kmutex_t	mod_lock;		/* protects &modules insert linkage, */
1247c478bd9Sstevel@tonic-gate 					/* mod_busy, mod_want, and mod_ref. */
1257c478bd9Sstevel@tonic-gate 					/* blocking operations while holding */
1267c478bd9Sstevel@tonic-gate 					/* mod_lock should be avoided */
1277c478bd9Sstevel@tonic-gate kmutex_t	mod_uninstall_lock;	/* protects mod_uninstall_cv */
1287c478bd9Sstevel@tonic-gate kthread_id_t	mod_aul_thread;
1297c478bd9Sstevel@tonic-gate 
130a7aa4df7Scth int		modunload_wait;
131a7aa4df7Scth kmutex_t	modunload_wait_mutex;
132a7aa4df7Scth kcondvar_t	modunload_wait_cv;
133a7aa4df7Scth int		modunload_active_count;
134a7aa4df7Scth int		modunload_disable_count;
135a7aa4df7Scth 
1367c478bd9Sstevel@tonic-gate int	isminiroot;		/* set if running as miniroot */
1377c478bd9Sstevel@tonic-gate int	modrootloaded;		/* set after root driver and fs are loaded */
1387c478bd9Sstevel@tonic-gate int	moddebug = 0x0;		/* debug flags for module writers */
1397c478bd9Sstevel@tonic-gate int	swaploaded;		/* set after swap driver and fs are loaded */
1407c478bd9Sstevel@tonic-gate int	bop_io_quiesced = 0;	/* set when BOP I/O can no longer be used */
1417c478bd9Sstevel@tonic-gate int	last_module_id;
1427c478bd9Sstevel@tonic-gate clock_t	mod_uninstall_interval = 0;
1437c478bd9Sstevel@tonic-gate int	ddi_modclose_unload = 1;	/* 0 -> just decrement reference */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate struct devnames *devnamesp;
1467c478bd9Sstevel@tonic-gate struct devnames orphanlist;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate krwlock_t	devinfo_tree_lock;	/* obsolete, to be removed */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	MAJBINDFILE "/etc/name_to_major"
1517c478bd9Sstevel@tonic-gate #define	SYSBINDFILE "/etc/name_to_sysnum"
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static char	majbind[] = MAJBINDFILE;
1547c478bd9Sstevel@tonic-gate static char	sysbind[] = SYSBINDFILE;
1557c478bd9Sstevel@tonic-gate static uint_t	mod_autounload_key;	/* for module autounload detection */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate extern int obpdebug;
1587c478bd9Sstevel@tonic-gate extern int make_mbind(char *, int, char *, struct bind **);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	DEBUGGER_PRESENT	((boothowto & RB_DEBUG) || (obpdebug != 0))
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static int minorperm_loaded = 0;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate void
1677c478bd9Sstevel@tonic-gate mod_setup(void)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	struct sysent *callp;
1707c478bd9Sstevel@tonic-gate 	int callnum, exectype;
1717c478bd9Sstevel@tonic-gate 	int	num_devs;
1727c478bd9Sstevel@tonic-gate 	int	i;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Initialize the list of loaded driver dev_ops.
1767c478bd9Sstevel@tonic-gate 	 * XXX - This must be done before reading the system file so that
1777c478bd9Sstevel@tonic-gate 	 * forceloads of drivers will work.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * Since read_binding_file is common code, it doesn't enforce that all
1827c478bd9Sstevel@tonic-gate 	 * of the binding file entries have major numbers <= MAXMAJ32.  Thus,
1837c478bd9Sstevel@tonic-gate 	 * ensure that we don't allocate some massive amount of space due to a
1847c478bd9Sstevel@tonic-gate 	 * bad entry.  We can't have major numbers bigger than MAXMAJ32
1857c478bd9Sstevel@tonic-gate 	 * until file system support for larger major numbers exists.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * Leave space for expansion, but not more than L_MAXMAJ32
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	devcnt = MIN(num_devs + 30, L_MAXMAJ32);
1927c478bd9Sstevel@tonic-gate 	devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
1937c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++)
1947c478bd9Sstevel@tonic-gate 		devopsp[i] = &mod_nodev_ops;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	init_devnamesp(devcnt);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Sync up with the work that the stand-alone linker has already done.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	(void) kobj_sync();
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
2047c478bd9Sstevel@tonic-gate 		kdi_dvec_modavail();
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	make_aliases(mb_hashtab);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Initialize streams device implementation structures.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * If the cl_bootstrap module is present,
2157c478bd9Sstevel@tonic-gate 	 * we should be configured as a cluster. Loading this module
2167c478bd9Sstevel@tonic-gate 	 * will set "cluster_bootflags" to non-zero.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	(void) modload("misc", "cl_bootstrap");
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
2217c478bd9Sstevel@tonic-gate 	init_syscallnames(NSYSCALL);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Start up dynamic autoconfiguration framework (dacf).
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	mod_hash_init();
2277c478bd9Sstevel@tonic-gate 	dacf_init();
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Start up IP policy framework (ipp).
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	ipp_init();
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * Allocate loadable native system call locks.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	for (callnum = 0, callp = sysent; callnum < NSYSCALL;
2387c478bd9Sstevel@tonic-gate 	    callnum++, callp++) {
2397c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2407c478bd9Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2417c478bd9Sstevel@tonic-gate 				callp->sy_lock =
2427c478bd9Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2437c478bd9Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2447c478bd9Sstevel@tonic-gate 			} else {
2457c478bd9Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2467c478bd9Sstevel@tonic-gate 				callp->sy_callc = nosys;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate #ifdef DEBUG
2497c478bd9Sstevel@tonic-gate 		} else {
2507c478bd9Sstevel@tonic-gate 			/*
2517c478bd9Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2527c478bd9Sstevel@tonic-gate 			 */
2537c478bd9Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2547c478bd9Sstevel@tonic-gate 			case SE_32RVAL1:
2557c478bd9Sstevel@tonic-gate 				/* only r_val1 returned */
2567c478bd9Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2577c478bd9Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2587c478bd9Sstevel@tonic-gate 			case SE_64RVAL:
2597c478bd9Sstevel@tonic-gate 				/* 64-bit rval returned */
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			default:
2627c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
2637c478bd9Sstevel@tonic-gate 				    callnum, callp->sy_flags);
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate #endif
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Allocate loadable system call locks for 32-bit compat syscalls
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
2747c478bd9Sstevel@tonic-gate 	    callnum++, callp++) {
2757c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2767c478bd9Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2777c478bd9Sstevel@tonic-gate 				callp->sy_lock =
2787c478bd9Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2797c478bd9Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2807c478bd9Sstevel@tonic-gate 			} else {
2817c478bd9Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2827c478bd9Sstevel@tonic-gate 				callp->sy_callc = nosys;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate #ifdef DEBUG
2857c478bd9Sstevel@tonic-gate 		} else {
2867c478bd9Sstevel@tonic-gate 			/*
2877c478bd9Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2887c478bd9Sstevel@tonic-gate 			 */
2897c478bd9Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2907c478bd9Sstevel@tonic-gate 			case SE_32RVAL1:
2917c478bd9Sstevel@tonic-gate 				/* only r_val1 returned */
2927c478bd9Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2937c478bd9Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2947c478bd9Sstevel@tonic-gate 			case SE_64RVAL:
2957c478bd9Sstevel@tonic-gate 				/* 64-bit rval returned */
2967c478bd9Sstevel@tonic-gate 				break;
2977c478bd9Sstevel@tonic-gate 			default:
2987c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
2997c478bd9Sstevel@tonic-gate 				    callnum, callp->sy_flags);
3007c478bd9Sstevel@tonic-gate 				goto skip;
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			/*
3047c478bd9Sstevel@tonic-gate 			 * Cross-check the native and compatibility tables.
3057c478bd9Sstevel@tonic-gate 			 */
3067c478bd9Sstevel@tonic-gate 			if (callp->sy_callc == nosys ||
3077c478bd9Sstevel@tonic-gate 			    sysent[callnum].sy_callc == nosys)
3087c478bd9Sstevel@tonic-gate 				continue;
3097c478bd9Sstevel@tonic-gate 			/*
3107c478bd9Sstevel@tonic-gate 			 * If only one or the other slot is loadable, then
3117c478bd9Sstevel@tonic-gate 			 * there's an error -- they should match!
3127c478bd9Sstevel@tonic-gate 			 */
3137c478bd9Sstevel@tonic-gate 			if ((callp->sy_callc == loadable_syscall) ^
3147c478bd9Sstevel@tonic-gate 			    (sysent[callnum].sy_callc == loadable_syscall)) {
3157c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] loadable?",
3167c478bd9Sstevel@tonic-gate 				    callnum);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			/*
3197c478bd9Sstevel@tonic-gate 			 * This is more of a heuristic test -- if the
3207c478bd9Sstevel@tonic-gate 			 * system call returns two values in the 32-bit
3217c478bd9Sstevel@tonic-gate 			 * world, it should probably return two 32-bit
3227c478bd9Sstevel@tonic-gate 			 * values in the 64-bit world too.
3237c478bd9Sstevel@tonic-gate 			 */
3247c478bd9Sstevel@tonic-gate 			if (((callp->sy_flags & SE_32RVAL2) == 0) ^
3257c478bd9Sstevel@tonic-gate 			    ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
3267c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
3277c478bd9Sstevel@tonic-gate 				    callnum);
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate skip:;
3307c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * Allocate loadable exec locks.  (Assumes all execs are loadable)
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	for (exectype = 0; exectype < nexectype; exectype++) {
3397c478bd9Sstevel@tonic-gate 		execsw[exectype].exec_lock =
3407c478bd9Sstevel@tonic-gate 		    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
3417c478bd9Sstevel@tonic-gate 		rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	read_class_file();
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* init thread specific structure for mod_uninstall_all */
3477c478bd9Sstevel@tonic-gate 	tsd_create(&mod_autounload_key, NULL);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static int
3517c478bd9Sstevel@tonic-gate modctl_modload(int use_path, char *filename, int *rvp)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	struct modctl *modp;
3547c478bd9Sstevel@tonic-gate 	int retval = 0;
3557c478bd9Sstevel@tonic-gate 	char *filenamep;
3567c478bd9Sstevel@tonic-gate 	int modid;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
3617c478bd9Sstevel@tonic-gate 		retval = EFAULT;
3627c478bd9Sstevel@tonic-gate 		goto out;
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	filenamep[MOD_MAXPATH - 1] = 0;
3667c478bd9Sstevel@tonic-gate 	modp = mod_hold_installed_mod(filenamep, use_path, &retval);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (modp == NULL)
3697c478bd9Sstevel@tonic-gate 		goto out;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
3727c478bd9Sstevel@tonic-gate 	modid = modp->mod_id;
3737c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
3740b38a8bdSahl 	CPU_STATS_ADDQ(CPU, sys, modload, 1);
3757c478bd9Sstevel@tonic-gate 	if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
3767c478bd9Sstevel@tonic-gate 		retval = EFAULT;
3777c478bd9Sstevel@tonic-gate out:
3787c478bd9Sstevel@tonic-gate 	kmem_free(filenamep, MOD_MAXPATH);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return (retval);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static int
3847c478bd9Sstevel@tonic-gate modctl_modunload(modid_t id)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	int rval = 0;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (id == 0) {
3897c478bd9Sstevel@tonic-gate #ifdef DEBUG
3907c478bd9Sstevel@tonic-gate 		/*
3917c478bd9Sstevel@tonic-gate 		 * Turn on mod_uninstall_daemon
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if (mod_uninstall_interval == 0) {
3947c478bd9Sstevel@tonic-gate 			mod_uninstall_interval = 60;
3957c478bd9Sstevel@tonic-gate 			modreap();
3967c478bd9Sstevel@tonic-gate 			return (rval);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate #endif
3997c478bd9Sstevel@tonic-gate 		mod_uninstall_all();
4007c478bd9Sstevel@tonic-gate 	} else {
4017c478bd9Sstevel@tonic-gate 		rval = modunload(id);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	return (rval);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static int
4077c478bd9Sstevel@tonic-gate modctl_modinfo(modid_t id, struct modinfo *umodi)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	int retval;
4107c478bd9Sstevel@tonic-gate 	struct modinfo modi;
4117c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4127c478bd9Sstevel@tonic-gate 	int nobase;
4137c478bd9Sstevel@tonic-gate 	struct modinfo32 modi32;
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4177c478bd9Sstevel@tonic-gate 		if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
4187c478bd9Sstevel@tonic-gate 			return (EFAULT);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4217c478bd9Sstevel@tonic-gate 	else {
4227c478bd9Sstevel@tonic-gate 		bzero(&modi, sizeof (modi));
4237c478bd9Sstevel@tonic-gate 		if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
4247c478bd9Sstevel@tonic-gate 			return (EFAULT);
4257c478bd9Sstevel@tonic-gate 		modi.mi_info = modi32.mi_info;
4267c478bd9Sstevel@tonic-gate 		modi.mi_id = modi32.mi_id;
4277c478bd9Sstevel@tonic-gate 		modi.mi_nextid = modi32.mi_nextid;
4287c478bd9Sstevel@tonic-gate 		nobase = modi.mi_info & MI_INFO_NOBASE;
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate #endif
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * This flag is -only- for the kernels use.
4337c478bd9Sstevel@tonic-gate 	 */
4347c478bd9Sstevel@tonic-gate 	modi.mi_info &= ~MI_INFO_LINKAGE;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	retval = modinfo(id, &modi);
4377c478bd9Sstevel@tonic-gate 	if (retval)
4387c478bd9Sstevel@tonic-gate 		return (retval);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4417c478bd9Sstevel@tonic-gate 		if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
4427c478bd9Sstevel@tonic-gate 			retval = EFAULT;
4437c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4447c478bd9Sstevel@tonic-gate 	} else {
4457c478bd9Sstevel@tonic-gate 		int i;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
4487c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		modi32.mi_info = modi.mi_info;
4517c478bd9Sstevel@tonic-gate 		modi32.mi_state = modi.mi_state;
4527c478bd9Sstevel@tonic-gate 		modi32.mi_id = modi.mi_id;
4537c478bd9Sstevel@tonic-gate 		modi32.mi_nextid = modi.mi_nextid;
4547c478bd9Sstevel@tonic-gate 		modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
4557c478bd9Sstevel@tonic-gate 		modi32.mi_size = modi.mi_size;
4567c478bd9Sstevel@tonic-gate 		modi32.mi_rev = modi.mi_rev;
4577c478bd9Sstevel@tonic-gate 		modi32.mi_loadcnt = modi.mi_loadcnt;
4587c478bd9Sstevel@tonic-gate 		bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
4597c478bd9Sstevel@tonic-gate 		for (i = 0; i < MODMAXLINK32; i++) {
4607c478bd9Sstevel@tonic-gate 			modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
4617c478bd9Sstevel@tonic-gate 			bcopy(modi.mi_msinfo[i].msi_linkinfo,
4627c478bd9Sstevel@tonic-gate 			    modi32.mi_msinfo[i].msi_linkinfo,
4637c478bd9Sstevel@tonic-gate 			    sizeof (modi32.mi_msinfo[0].msi_linkinfo));
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 		if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
4667c478bd9Sstevel@tonic-gate 			retval = EFAULT;
4677c478bd9Sstevel@tonic-gate #endif
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	return (retval);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Return the last major number in the range of permissible major numbers.
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4777c478bd9Sstevel@tonic-gate static int
4787c478bd9Sstevel@tonic-gate modctl_modreserve(modid_t id, int *data)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
4817c478bd9Sstevel@tonic-gate 		return (EFAULT);
4827c478bd9Sstevel@tonic-gate 	return (0);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static int
4867c478bd9Sstevel@tonic-gate modctl_add_major(int *data)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	struct modconfig mc;
4897c478bd9Sstevel@tonic-gate 	int i, rv;
4907c478bd9Sstevel@tonic-gate 	struct aliases alias;
4917c478bd9Sstevel@tonic-gate 	struct aliases *ap;
4927c478bd9Sstevel@tonic-gate 	char name[MAXMODCONFNAME];
4937c478bd9Sstevel@tonic-gate 	char cname[MAXMODCONFNAME];
4947c478bd9Sstevel@tonic-gate 	char *drvname;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	bzero(&mc, sizeof (struct modconfig));
4977c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4987c478bd9Sstevel@tonic-gate 		if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
4997c478bd9Sstevel@tonic-gate 			return (EFAULT);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5027c478bd9Sstevel@tonic-gate 	else {
5037c478bd9Sstevel@tonic-gate 		struct modconfig32 modc32;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
5067c478bd9Sstevel@tonic-gate 			return (EFAULT);
5077c478bd9Sstevel@tonic-gate 		else {
5087c478bd9Sstevel@tonic-gate 			bcopy(modc32.drvname, mc.drvname,
5097c478bd9Sstevel@tonic-gate 			    sizeof (modc32.drvname));
5107c478bd9Sstevel@tonic-gate 			bcopy(modc32.drvclass, mc.drvclass,
5117c478bd9Sstevel@tonic-gate 			    sizeof (modc32.drvclass));
5127c478bd9Sstevel@tonic-gate 			mc.major = modc32.major;
5137c478bd9Sstevel@tonic-gate 			mc.num_aliases = modc32.num_aliases;
5147c478bd9Sstevel@tonic-gate 			mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate #endif
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * If the driver is already in the mb_hashtab, and the name given
5217c478bd9Sstevel@tonic-gate 	 * doesn't match that driver's name, fail.  Otherwise, pass, since
5227c478bd9Sstevel@tonic-gate 	 * we may be adding aliases.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	if ((drvname = mod_major_to_name(mc.major)) != NULL &&
5257c478bd9Sstevel@tonic-gate 	    strcmp(drvname, mc.drvname) != 0)
5267c478bd9Sstevel@tonic-gate 		return (EINVAL);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * Add each supplied driver alias to mb_hashtab
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	ap = mc.ap;
5327c478bd9Sstevel@tonic-gate 	for (i = 0; i < mc.num_aliases; i++) {
5337c478bd9Sstevel@tonic-gate 		bzero(&alias, sizeof (struct aliases));
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
5367c478bd9Sstevel@tonic-gate 			if (copyin(ap, &alias, sizeof (struct aliases)) != 0)
5377c478bd9Sstevel@tonic-gate 				return (EFAULT);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			if (alias.a_len > MAXMODCONFNAME)
5407c478bd9Sstevel@tonic-gate 				return (EINVAL);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			if (copyin(alias.a_name, name, alias.a_len) != 0)
5437c478bd9Sstevel@tonic-gate 				return (EFAULT);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 			if (name[alias.a_len - 1] != '\0')
5467c478bd9Sstevel@tonic-gate 				return (EINVAL);
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5497c478bd9Sstevel@tonic-gate 		else {
5507c478bd9Sstevel@tonic-gate 			struct aliases32 al32;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			bzero(&al32, sizeof (struct aliases32));
5537c478bd9Sstevel@tonic-gate 			if (copyin(ap, &al32, sizeof (struct aliases32)) != 0)
5547c478bd9Sstevel@tonic-gate 				return (EFAULT);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 			if (al32.a_len > MAXMODCONFNAME)
5577c478bd9Sstevel@tonic-gate 				return (EINVAL);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 			if (copyin((void *)(uintptr_t)al32.a_name,
5607c478bd9Sstevel@tonic-gate 			    name, al32.a_len) != 0)
5617c478bd9Sstevel@tonic-gate 				return (EFAULT);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			if (name[al32.a_len - 1] != '\0')
5647c478bd9Sstevel@tonic-gate 				return (EINVAL);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			alias.a_next = (void *)(uintptr_t)al32.a_next;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate #endif
5697c478bd9Sstevel@tonic-gate 		check_esc_sequences(name, cname);
5707c478bd9Sstevel@tonic-gate 		(void) make_mbind(cname, mc.major, NULL, mb_hashtab);
5717c478bd9Sstevel@tonic-gate 		ap = alias.a_next;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * Try to establish an mbinding for mc.drvname, and add it to devnames.
5767c478bd9Sstevel@tonic-gate 	 * Add class if any after establishing the major number
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	(void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
5797c478bd9Sstevel@tonic-gate 	rv = make_devname(mc.drvname, mc.major);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (rv == 0) {
5827c478bd9Sstevel@tonic-gate 		if (mc.drvclass[0] != '\0')
5837c478bd9Sstevel@tonic-gate 			add_class(mc.drvname, mc.drvclass);
5847c478bd9Sstevel@tonic-gate 		(void) i_ddi_load_drvconf(mc.major);
5857c478bd9Sstevel@tonic-gate 		i_ddi_bind_devs();
5867c478bd9Sstevel@tonic-gate 		i_ddi_di_cache_invalidate(KM_SLEEP);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	return (rv);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate static int
5927c478bd9Sstevel@tonic-gate modctl_rem_major(major_t major)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
5977c478bd9Sstevel@tonic-gate 		return (EINVAL);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	/* mark devnames as removed */
6007c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
6017c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6027c478bd9Sstevel@tonic-gate 	if (dnp->dn_name == NULL ||
6037c478bd9Sstevel@tonic-gate 	    (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
6047c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
6057c478bd9Sstevel@tonic-gate 		return (EINVAL);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_REMOVED;
6087c478bd9Sstevel@tonic-gate 	pm_driver_removed(major);
6097c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	(void) i_ddi_unload_drvconf(major);
6127c478bd9Sstevel@tonic-gate 	i_ddi_unbind_devs(major);
6137c478bd9Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
6147c478bd9Sstevel@tonic-gate 	return (0);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate static struct vfs *
6187c478bd9Sstevel@tonic-gate path_to_vfs(char *name)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	vnode_t *vp;
6217c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
6247c478bd9Sstevel@tonic-gate 		return (NULL);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
6277c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6287c478bd9Sstevel@tonic-gate 	return (vfsp);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate static int
6327c478bd9Sstevel@tonic-gate new_vfs_in_modpath()
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	static int n_modpath = 0;
6357c478bd9Sstevel@tonic-gate 	static char *modpath_copy;
6367c478bd9Sstevel@tonic-gate 	static struct pathvfs {
6377c478bd9Sstevel@tonic-gate 		char *path;
6387c478bd9Sstevel@tonic-gate 		struct vfs *vfsp;
6397c478bd9Sstevel@tonic-gate 	} *pathvfs;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	int i, new_vfs = 0;
6427c478bd9Sstevel@tonic-gate 	char *tmp, *tmp1;
6437c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (n_modpath != 0) {
6467c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_modpath; i++) {
6477c478bd9Sstevel@tonic-gate 			vfsp = path_to_vfs(pathvfs[i].path);
6487c478bd9Sstevel@tonic-gate 			if (vfsp != pathvfs[i].vfsp) {
6497c478bd9Sstevel@tonic-gate 				pathvfs[i].vfsp = vfsp;
6507c478bd9Sstevel@tonic-gate 				if (vfsp)
6517c478bd9Sstevel@tonic-gate 					new_vfs = 1;
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 		return (new_vfs);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * First call, initialize the pathvfs structure
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
6617c478bd9Sstevel@tonic-gate 	tmp = modpath_copy;
6627c478bd9Sstevel@tonic-gate 	n_modpath = 1;
6637c478bd9Sstevel@tonic-gate 	tmp1 = strchr(tmp, ' ');
6647c478bd9Sstevel@tonic-gate 	while (tmp1) {
6657c478bd9Sstevel@tonic-gate 		*tmp1 = '\0';
6667c478bd9Sstevel@tonic-gate 		n_modpath++;
6677c478bd9Sstevel@tonic-gate 		tmp = tmp1 + 1;
6687c478bd9Sstevel@tonic-gate 		tmp1 = strchr(tmp, ' ');
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
6727c478bd9Sstevel@tonic-gate 	tmp = modpath_copy;
6737c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_modpath; i++) {
6747c478bd9Sstevel@tonic-gate 		pathvfs[i].path = tmp;
6757c478bd9Sstevel@tonic-gate 		vfsp = path_to_vfs(tmp);
6767c478bd9Sstevel@tonic-gate 		pathvfs[i].vfsp = vfsp;
6777c478bd9Sstevel@tonic-gate 		tmp += strlen(tmp) + 1;
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	return (1);	/* always reread driver.conf the first time */
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
682*facf4a8dSllai1 static int
683*facf4a8dSllai1 modctl_load_drvconf(major_t major)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	int ret;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (major != (major_t)-1) {
6887c478bd9Sstevel@tonic-gate 		ret = i_ddi_load_drvconf(major);
6897c478bd9Sstevel@tonic-gate 		if (ret == 0)
6907c478bd9Sstevel@tonic-gate 			i_ddi_bind_devs();
6917c478bd9Sstevel@tonic-gate 		return (ret);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * We are invoked to rescan new driver.conf files. It is
6967c478bd9Sstevel@tonic-gate 	 * only necessary if a new file system was mounted in the
6977c478bd9Sstevel@tonic-gate 	 * module_path. Because rescanning driver.conf files can
6987c478bd9Sstevel@tonic-gate 	 * take some time on older platforms (sun4m), the following
6997c478bd9Sstevel@tonic-gate 	 * code skips unnecessary driver.conf rescans to optimize
7007c478bd9Sstevel@tonic-gate 	 * boot performance.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	if (new_vfs_in_modpath()) {
7037c478bd9Sstevel@tonic-gate 		(void) i_ddi_load_drvconf((major_t)-1);
7047c478bd9Sstevel@tonic-gate 		/*
7057c478bd9Sstevel@tonic-gate 		 * If we are still initializing io subsystem,
7067c478bd9Sstevel@tonic-gate 		 * load drivers with ddi-forceattach property
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		if (!i_ddi_io_initialized())
7097c478bd9Sstevel@tonic-gate 			i_ddi_forceattach_drivers();
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 	return (0);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate static int
7157c478bd9Sstevel@tonic-gate modctl_unload_drvconf(major_t major)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	int ret;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
7207c478bd9Sstevel@tonic-gate 		return (EINVAL);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	ret = i_ddi_unload_drvconf(major);
7237c478bd9Sstevel@tonic-gate 	if (ret != 0)
7247c478bd9Sstevel@tonic-gate 		return (ret);
7257c478bd9Sstevel@tonic-gate 	(void) i_ddi_unbind_devs(major);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	return (0);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate static void
7317c478bd9Sstevel@tonic-gate check_esc_sequences(char *str, char *cstr)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	int i;
7347c478bd9Sstevel@tonic-gate 	size_t len;
7357c478bd9Sstevel@tonic-gate 	char *p;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	len = strlen(str);
7387c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++, cstr++) {
7397c478bd9Sstevel@tonic-gate 		if (*str != '\\') {
7407c478bd9Sstevel@tonic-gate 			*cstr = *str;
7417c478bd9Sstevel@tonic-gate 		} else {
7427c478bd9Sstevel@tonic-gate 			p = str + 1;
7437c478bd9Sstevel@tonic-gate 			/*
7447c478bd9Sstevel@tonic-gate 			 * we only handle octal escape sequences for SPACE
7457c478bd9Sstevel@tonic-gate 			 */
7467c478bd9Sstevel@tonic-gate 			if (*p++ == '0' && *p++ == '4' && *p == '0') {
7477c478bd9Sstevel@tonic-gate 				*cstr = ' ';
7487c478bd9Sstevel@tonic-gate 				str += 3;
7497c478bd9Sstevel@tonic-gate 			} else {
7507c478bd9Sstevel@tonic-gate 				*cstr = *str;
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 	*cstr = 0;
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate static int
7587c478bd9Sstevel@tonic-gate modctl_getmodpathlen(int *data)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	int len;
7617c478bd9Sstevel@tonic-gate 	len = strlen(default_path);
7627c478bd9Sstevel@tonic-gate 	if (copyout(&len, data, sizeof (len)) != 0)
7637c478bd9Sstevel@tonic-gate 		return (EFAULT);
7647c478bd9Sstevel@tonic-gate 	return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static int
7687c478bd9Sstevel@tonic-gate modctl_getmodpath(char *data)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	if (copyout(default_path, data, strlen(default_path) + 1) != 0)
7717c478bd9Sstevel@tonic-gate 		return (EFAULT);
7727c478bd9Sstevel@tonic-gate 	return (0);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate static int
7767c478bd9Sstevel@tonic-gate modctl_read_sysbinding_file(void)
7777c478bd9Sstevel@tonic-gate {
7787c478bd9Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
7797c478bd9Sstevel@tonic-gate 	return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate static int
7837c478bd9Sstevel@tonic-gate modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	char name[256];
7867c478bd9Sstevel@tonic-gate 	int retval;
7877c478bd9Sstevel@tonic-gate 	major_t major;
7887c478bd9Sstevel@tonic-gate 
789*facf4a8dSllai1 	if (ulen == 0)
790*facf4a8dSllai1 		return (EINVAL);
7917c478bd9Sstevel@tonic-gate 	if ((retval = copyinstr(uname, name,
7927c478bd9Sstevel@tonic-gate 	    (ulen < 256) ? ulen : 256, 0)) != 0)
7937c478bd9Sstevel@tonic-gate 		return (retval);
7947c478bd9Sstevel@tonic-gate 	if ((major = mod_name_to_major(name)) == (major_t)-1)
7957c478bd9Sstevel@tonic-gate 		return (ENODEV);
7967c478bd9Sstevel@tonic-gate 	if (copyout(&major, umajorp, sizeof (major_t)) != 0)
7977c478bd9Sstevel@tonic-gate 		return (EFAULT);
7987c478bd9Sstevel@tonic-gate 	return (0);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate static int
8027c478bd9Sstevel@tonic-gate modctl_getname(char *uname, uint_t ulen, int *umajorp)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	char *name;
8057c478bd9Sstevel@tonic-gate 	major_t major;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if (copyin(umajorp, &major, sizeof (major)) != 0)
8087c478bd9Sstevel@tonic-gate 		return (EFAULT);
8097c478bd9Sstevel@tonic-gate 	if ((name = mod_major_to_name(major)) == NULL)
8107c478bd9Sstevel@tonic-gate 		return (ENODEV);
8117c478bd9Sstevel@tonic-gate 	if ((strlen(name) + 1) > ulen)
8127c478bd9Sstevel@tonic-gate 		return (ENOSPC);
8137c478bd9Sstevel@tonic-gate 	return (copyoutstr(name, uname, ulen, NULL));
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate static int
8177c478bd9Sstevel@tonic-gate modctl_devt2instance(dev_t dev, int *uinstancep)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	int	instance;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if ((instance = dev_to_instance(dev)) == -1)
8227c478bd9Sstevel@tonic-gate 		return (EINVAL);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	return (copyout(&instance, uinstancep, sizeof (int)));
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * Return the sizeof of the device id.
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate modctl_sizeof_devid(dev_t dev, uint_t *len)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	uint_t		sz;
8347c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/* get device id */
8377c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
8387c478bd9Sstevel@tonic-gate 		return (EINVAL);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
8417c478bd9Sstevel@tonic-gate 	ddi_devid_free(devid);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	/* copyout device id size */
8447c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
8457c478bd9Sstevel@tonic-gate 		return (EFAULT);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	return (0);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * Return a copy of the device id.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate static int
8547c478bd9Sstevel@tonic-gate modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	uint_t		sz;
8577c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
8587c478bd9Sstevel@tonic-gate 	int		err = 0;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/* get device id */
8617c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
8627c478bd9Sstevel@tonic-gate 		return (EINVAL);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* Error if device id is larger than space allocated */
8677c478bd9Sstevel@tonic-gate 	if (sz > len) {
8687c478bd9Sstevel@tonic-gate 		ddi_devid_free(devid);
8697c478bd9Sstevel@tonic-gate 		return (ENOSPC);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* copy out device id */
8737c478bd9Sstevel@tonic-gate 	if (copyout(devid, udevid, sz) != 0)
8747c478bd9Sstevel@tonic-gate 		err = EFAULT;
8757c478bd9Sstevel@tonic-gate 	ddi_devid_free(devid);
8767c478bd9Sstevel@tonic-gate 	return (err);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate  * return the /devices paths associated with the specified devid and
8817c478bd9Sstevel@tonic-gate  * minor name.
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8847c478bd9Sstevel@tonic-gate static int
8857c478bd9Sstevel@tonic-gate modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
8867c478bd9Sstevel@tonic-gate 	size_t *ulensp, char *upaths)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid = NULL;
8897c478bd9Sstevel@tonic-gate 	int		devid_len;
8907c478bd9Sstevel@tonic-gate 	char		*minor_name = NULL;
8917c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
8927c478bd9Sstevel@tonic-gate 	struct ddi_minor_data   *dmdp;
8937c478bd9Sstevel@tonic-gate 	char		*path = NULL;
8947c478bd9Sstevel@tonic-gate 	int		ulens;
8957c478bd9Sstevel@tonic-gate 	int		lens;
8967c478bd9Sstevel@tonic-gate 	int		len;
8977c478bd9Sstevel@tonic-gate 	dev_t		*devlist = NULL;
8987c478bd9Sstevel@tonic-gate 	int		ndevs;
8997c478bd9Sstevel@tonic-gate 	int		i;
9007c478bd9Sstevel@tonic-gate 	int		ret = 0;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * If upaths is NULL then we are only computing the amount of space
9047c478bd9Sstevel@tonic-gate 	 * needed to hold the paths and returning the value in *ulensp. If we
9057c478bd9Sstevel@tonic-gate 	 * are copying out paths then we get the amount of space allocated by
9067c478bd9Sstevel@tonic-gate 	 * the caller. If the actual space needed for paths is larger, or
9077c478bd9Sstevel@tonic-gate 	 * things are changing out from under us, then we return EAGAIN.
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate 	if (upaths) {
9107c478bd9Sstevel@tonic-gate 		if (ulensp == NULL)
9117c478bd9Sstevel@tonic-gate 			return (EINVAL);
9127c478bd9Sstevel@tonic-gate 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
9137c478bd9Sstevel@tonic-gate 			return (EFAULT);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * copyin enough of the devid to determine the length then
9187c478bd9Sstevel@tonic-gate 	 * reallocate and copy in the entire devid.
9197c478bd9Sstevel@tonic-gate 	 */
9207c478bd9Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(NULL);
9217c478bd9Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
9227c478bd9Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
9237c478bd9Sstevel@tonic-gate 		ret = EFAULT;
9247c478bd9Sstevel@tonic-gate 		goto out;
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	len = devid_len;
9277c478bd9Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(devid);
9287c478bd9Sstevel@tonic-gate 	kmem_free(devid, len);
9297c478bd9Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
9307c478bd9Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
9317c478bd9Sstevel@tonic-gate 		ret = EFAULT;
9327c478bd9Sstevel@tonic-gate 		goto out;
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/* copyin the minor name if specified. */
9367c478bd9Sstevel@tonic-gate 	minor_name = uminor_name;
9377c478bd9Sstevel@tonic-gate 	if ((minor_name != DEVID_MINOR_NAME_ALL) &&
9387c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
9397c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
9407c478bd9Sstevel@tonic-gate 		minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9417c478bd9Sstevel@tonic-gate 		if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
9427c478bd9Sstevel@tonic-gate 			ret = EFAULT;
9437c478bd9Sstevel@tonic-gate 			goto out;
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * Use existing function to resolve the devid into a devlist.
9497c478bd9Sstevel@tonic-gate 	 *
9507c478bd9Sstevel@tonic-gate 	 * NOTE: there is a loss of spectype information in the current
9517c478bd9Sstevel@tonic-gate 	 * ddi_lyr_devid_to_devlist implementation. We work around this by not
9527c478bd9Sstevel@tonic-gate 	 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
9537c478bd9Sstevel@tonic-gate 	 * node forms in the loop processing the devlist below. It would be
9547c478bd9Sstevel@tonic-gate 	 * best if at some point the use of this interface here was replaced
9557c478bd9Sstevel@tonic-gate 	 * with a path oriented call.
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	if (ddi_lyr_devid_to_devlist(devid,
9587c478bd9Sstevel@tonic-gate 	    (minor_name == DEVID_MINOR_NAME_ALL) ?
9597c478bd9Sstevel@tonic-gate 	    DEVID_MINOR_NAME_ALL_CHR : minor_name,
9607c478bd9Sstevel@tonic-gate 	    &ndevs, &devlist) != DDI_SUCCESS) {
9617c478bd9Sstevel@tonic-gate 		ret = EINVAL;
9627c478bd9Sstevel@tonic-gate 		goto out;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * loop over the devlist, converting each devt to a path and doing
9677c478bd9Sstevel@tonic-gate 	 * a copyout of the path and computation of the amount of space
9687c478bd9Sstevel@tonic-gate 	 * needed to hold all the paths
9697c478bd9Sstevel@tonic-gate 	 */
9707c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9717c478bd9Sstevel@tonic-gate 	for (i = 0, lens = 0; i < ndevs; i++) {
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		/* find the dip associated with the dev_t */
9747c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
9757c478bd9Sstevel@tonic-gate 			continue;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		/* loop over all the minor nodes, skipping ones we don't want */
9787c478bd9Sstevel@tonic-gate 		for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
9797c478bd9Sstevel@tonic-gate 			if ((dmdp->ddm_dev != devlist[i]) ||
9807c478bd9Sstevel@tonic-gate 			    (dmdp->type != DDM_MINOR))
9817c478bd9Sstevel@tonic-gate 				continue;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 			if ((minor_name != DEVID_MINOR_NAME_ALL) &&
9847c478bd9Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
9857c478bd9Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
9867c478bd9Sstevel@tonic-gate 			    strcmp(minor_name, dmdp->ddm_name))
9877c478bd9Sstevel@tonic-gate 				continue;
9887c478bd9Sstevel@tonic-gate 			else {
9897c478bd9Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
9907c478bd9Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFCHR))
9917c478bd9Sstevel@tonic-gate 					continue;
9927c478bd9Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
9937c478bd9Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFBLK))
9947c478bd9Sstevel@tonic-gate 					continue;
9957c478bd9Sstevel@tonic-gate 			}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			/* XXX need ddi_pathname_minor(dmdp, path); interface */
9987c478bd9Sstevel@tonic-gate 			if (ddi_dev_pathname(dmdp->ddm_dev, dmdp->ddm_spec_type,
9997c478bd9Sstevel@tonic-gate 			    path) != DDI_SUCCESS) {
10007c478bd9Sstevel@tonic-gate 				ret = EAGAIN;
10017c478bd9Sstevel@tonic-gate 				goto out;
10027c478bd9Sstevel@tonic-gate 			}
10037c478bd9Sstevel@tonic-gate 			len = strlen(path) + 1;
10047c478bd9Sstevel@tonic-gate 			*(path + len) = '\0';	/* set double termination */
10057c478bd9Sstevel@tonic-gate 			lens += len;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 			/* copyout the path with double terminations */
10087c478bd9Sstevel@tonic-gate 			if (upaths) {
10097c478bd9Sstevel@tonic-gate 				if (lens > ulens) {
10107c478bd9Sstevel@tonic-gate 					ret = EAGAIN;
10117c478bd9Sstevel@tonic-gate 					goto out;
10127c478bd9Sstevel@tonic-gate 				}
10137c478bd9Sstevel@tonic-gate 				if (copyout(path, upaths, len + 1)) {
10147c478bd9Sstevel@tonic-gate 					ret = EFAULT;
10157c478bd9Sstevel@tonic-gate 					goto out;
10167c478bd9Sstevel@tonic-gate 				}
10177c478bd9Sstevel@tonic-gate 				upaths += len;
10187c478bd9Sstevel@tonic-gate 			}
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
10217c478bd9Sstevel@tonic-gate 		dip = NULL;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	lens++;		/* add one for double termination */
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/* copy out the amount of space needed to hold the paths */
10267c478bd9Sstevel@tonic-gate 	if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
10277c478bd9Sstevel@tonic-gate 		ret = EFAULT;
10287c478bd9Sstevel@tonic-gate 		goto out;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	ret = 0;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate out:	if (dip)
10337c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
10347c478bd9Sstevel@tonic-gate 	if (path)
10357c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
10367c478bd9Sstevel@tonic-gate 	if (devlist)
10377c478bd9Sstevel@tonic-gate 		ddi_lyr_free_devlist(devlist, ndevs);
10387c478bd9Sstevel@tonic-gate 	if (minor_name &&
10397c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL) &&
10407c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
10417c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK))
10427c478bd9Sstevel@tonic-gate 		kmem_free(minor_name, MAXPATHLEN);
10437c478bd9Sstevel@tonic-gate 	if (devid)
10447c478bd9Sstevel@tonic-gate 		kmem_free(devid, devid_len);
10457c478bd9Sstevel@tonic-gate 	return (ret);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate /*
10497c478bd9Sstevel@tonic-gate  * Return the size of the minor name.
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate static int
10527c478bd9Sstevel@tonic-gate modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	uint_t	sz;
10557c478bd9Sstevel@tonic-gate 	char	*name;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/* get the minor name */
10587c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
10597c478bd9Sstevel@tonic-gate 		return (EINVAL);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
10627c478bd9Sstevel@tonic-gate 	kmem_free(name, sz);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/* copy out the size of the minor name */
10657c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
10667c478bd9Sstevel@tonic-gate 		return (EFAULT);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	return (0);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate  * Return the minor name.
10737c478bd9Sstevel@tonic-gate  */
10747c478bd9Sstevel@tonic-gate static int
10757c478bd9Sstevel@tonic-gate modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	uint_t	sz;
10787c478bd9Sstevel@tonic-gate 	char	*name;
10797c478bd9Sstevel@tonic-gate 	int	err = 0;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/* get the minor name */
10827c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
10837c478bd9Sstevel@tonic-gate 		return (EINVAL);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	/* Error if the minor name is larger than the space allocated */
10887c478bd9Sstevel@tonic-gate 	if (sz > len) {
10897c478bd9Sstevel@tonic-gate 		kmem_free(name, sz);
10907c478bd9Sstevel@tonic-gate 		return (ENOSPC);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* copy out the minor name */
10947c478bd9Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
10957c478bd9Sstevel@tonic-gate 		err = EFAULT;
10967c478bd9Sstevel@tonic-gate 	kmem_free(name, sz);
10977c478bd9Sstevel@tonic-gate 	return (err);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate /*
11017c478bd9Sstevel@tonic-gate  * Return the size of the devfspath name.
11027c478bd9Sstevel@tonic-gate  */
11037c478bd9Sstevel@tonic-gate static int
11047c478bd9Sstevel@tonic-gate modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	uint_t	sz;
11077c478bd9Sstevel@tonic-gate 	char	*name;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/* get the path name */
11107c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11117c478bd9Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
11127c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11137c478bd9Sstevel@tonic-gate 		return (EINVAL);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
11177c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/* copy out the size of the path name */
11207c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
11217c478bd9Sstevel@tonic-gate 		return (EFAULT);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	return (0);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate  * Return the devfspath name.
11287c478bd9Sstevel@tonic-gate  */
11297c478bd9Sstevel@tonic-gate static int
11307c478bd9Sstevel@tonic-gate modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	uint_t	sz;
11337c478bd9Sstevel@tonic-gate 	char	*name;
11347c478bd9Sstevel@tonic-gate 	int	err = 0;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/* get the path name */
11377c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11387c478bd9Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
11397c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11407c478bd9Sstevel@tonic-gate 		return (EINVAL);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/* Error if the path name is larger than the space allocated */
11467c478bd9Sstevel@tonic-gate 	if (sz > len) {
11477c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11487c478bd9Sstevel@tonic-gate 		return (ENOSPC);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/* copy out the path name */
11527c478bd9Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
11537c478bd9Sstevel@tonic-gate 		err = EFAULT;
11547c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
11557c478bd9Sstevel@tonic-gate 	return (err);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate static int
11597c478bd9Sstevel@tonic-gate modctl_get_fbname(char *path)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	extern dev_t fbdev;
11627c478bd9Sstevel@tonic-gate 	char *pathname = NULL;
11637c478bd9Sstevel@tonic-gate 	int rval = 0;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	/* make sure fbdev is set before we plunge in */
11667c478bd9Sstevel@tonic-gate 	if (fbdev == NODEV)
11677c478bd9Sstevel@tonic-gate 		return (ENODEV);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11707c478bd9Sstevel@tonic-gate 	if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
11717c478bd9Sstevel@tonic-gate 	    pathname)) == DDI_SUCCESS) {
11727c478bd9Sstevel@tonic-gate 		if (copyout(pathname, path, strlen(pathname)+1) != 0) {
11737c478bd9Sstevel@tonic-gate 			rval = EFAULT;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
11777c478bd9Sstevel@tonic-gate 	return (rval);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate /*
11817c478bd9Sstevel@tonic-gate  * modctl_reread_dacf()
11827c478bd9Sstevel@tonic-gate  *	Reread the dacf rules database from the named binding file.
11837c478bd9Sstevel@tonic-gate  *	If NULL is specified, pass along the NULL, it means 'use the default'.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate static int
11867c478bd9Sstevel@tonic-gate modctl_reread_dacf(char *path)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	int rval = 0;
11897c478bd9Sstevel@tonic-gate 	char *filename, *filenamep;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (path == NULL) {
11947c478bd9Sstevel@tonic-gate 		filenamep = NULL;
11957c478bd9Sstevel@tonic-gate 	} else {
11967c478bd9Sstevel@tonic-gate 		if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
11977c478bd9Sstevel@tonic-gate 			rval = EFAULT;
11987c478bd9Sstevel@tonic-gate 			goto out;
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 		filenamep = filename;
12017c478bd9Sstevel@tonic-gate 		filenamep[MAXPATHLEN - 1] = '\0';
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	rval = read_dacf_binding_file(filenamep);
12057c478bd9Sstevel@tonic-gate out:
12067c478bd9Sstevel@tonic-gate 	kmem_free(filename, MAXPATHLEN);
12077c478bd9Sstevel@tonic-gate 	return (rval);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12117c478bd9Sstevel@tonic-gate static int
12127c478bd9Sstevel@tonic-gate modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
12137c478bd9Sstevel@tonic-gate     uint_t flag)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	int error = 0;
12167c478bd9Sstevel@tonic-gate 	char *filenamep;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	switch (subcmd) {
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	case MODEVENTS_FLUSH:
12217c478bd9Sstevel@tonic-gate 		/* flush all currently queued events */
12227c478bd9Sstevel@tonic-gate 		log_sysevent_flushq(subcmd, flag);
12237c478bd9Sstevel@tonic-gate 		break;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
12267c478bd9Sstevel@tonic-gate 		/*
12277c478bd9Sstevel@tonic-gate 		 * bind door_upcall to filename
12287c478bd9Sstevel@tonic-gate 		 * this should only be done once per invocation
12297c478bd9Sstevel@tonic-gate 		 * of the event daemon.
12307c478bd9Sstevel@tonic-gate 		 */
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 		if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
12357c478bd9Sstevel@tonic-gate 			error = EFAULT;
12367c478bd9Sstevel@tonic-gate 		} else {
12377c478bd9Sstevel@tonic-gate 			error = log_sysevent_filename(filenamep);
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		kmem_free(filenamep, MOD_MAXPATH);
12407c478bd9Sstevel@tonic-gate 		break;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	case MODEVENTS_GETDATA:
12437c478bd9Sstevel@tonic-gate 		error = log_sysevent_copyout_data((sysevent_id_t *)a2,
12447c478bd9Sstevel@tonic-gate 		    (size_t)a3, (caddr_t)a4);
12457c478bd9Sstevel@tonic-gate 		break;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	case MODEVENTS_FREEDATA:
12487c478bd9Sstevel@tonic-gate 		error = log_sysevent_free_data((sysevent_id_t *)a2);
12497c478bd9Sstevel@tonic-gate 		break;
12507c478bd9Sstevel@tonic-gate 	case MODEVENTS_POST_EVENT:
12517c478bd9Sstevel@tonic-gate 		error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
12527c478bd9Sstevel@tonic-gate 			(sysevent_id_t *)a4);
12537c478bd9Sstevel@tonic-gate 		break;
12547c478bd9Sstevel@tonic-gate 	case MODEVENTS_REGISTER_EVENT:
12557c478bd9Sstevel@tonic-gate 		error = log_sysevent_register((char *)a2, (char *)a3,
12567c478bd9Sstevel@tonic-gate 		    (se_pubsub_t *)a4);
12577c478bd9Sstevel@tonic-gate 		break;
12587c478bd9Sstevel@tonic-gate 	default:
12597c478bd9Sstevel@tonic-gate 		error = EINVAL;
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	return (error);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate static void
12667c478bd9Sstevel@tonic-gate free_mperm(mperm_t *mp)
12677c478bd9Sstevel@tonic-gate {
12687c478bd9Sstevel@tonic-gate 	int len;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	if (mp->mp_minorname) {
12717c478bd9Sstevel@tonic-gate 		len = strlen(mp->mp_minorname) + 1;
12727c478bd9Sstevel@tonic-gate 		kmem_free(mp->mp_minorname, len);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	kmem_free(mp, sizeof (mperm_t));
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate #define	MP_NO_DRV_ERR	\
12787c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: no driver for %s\n"
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate #define	MP_EMPTY_MINOR	\
12817c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: empty minor name for driver %s\n"
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate #define	MP_NO_MINOR	\
12847c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: no minor matching %s for driver %s\n"
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate /*
12877c478bd9Sstevel@tonic-gate  * Remove mperm entry with matching minorname
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate static void
12907c478bd9Sstevel@tonic-gate rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	mperm_t **mp_head;
12937c478bd9Sstevel@tonic-gate 	mperm_t *freemp = NULL;
12947c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
12957c478bd9Sstevel@tonic-gate 	mperm_t **wildmp;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
13007c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
13017c478bd9Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
13027c478bd9Sstevel@tonic-gate 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
13037c478bd9Sstevel@tonic-gate 		if (*wildmp)
13047c478bd9Sstevel@tonic-gate 			freemp = *wildmp;
13057c478bd9Sstevel@tonic-gate 		*wildmp = NULL;
13067c478bd9Sstevel@tonic-gate 	} else {
13077c478bd9Sstevel@tonic-gate 		mp_head = &dnp->dn_mperm;
13087c478bd9Sstevel@tonic-gate 		while (*mp_head) {
13097c478bd9Sstevel@tonic-gate 			if (strcmp((*mp_head)->mp_minorname,
13107c478bd9Sstevel@tonic-gate 			    mp->mp_minorname) != 0) {
13117c478bd9Sstevel@tonic-gate 				mp_head = &(*mp_head)->mp_next;
13127c478bd9Sstevel@tonic-gate 				continue;
13137c478bd9Sstevel@tonic-gate 			}
13147c478bd9Sstevel@tonic-gate 			/* remove the entry */
13157c478bd9Sstevel@tonic-gate 			freemp = *mp_head;
13167c478bd9Sstevel@tonic-gate 			*mp_head = freemp->mp_next;
13177c478bd9Sstevel@tonic-gate 			break;
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	if (freemp) {
13217c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13227c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
13237c478bd9Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
13247c478bd9Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
13257c478bd9Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 		free_mperm(freemp);
13287c478bd9Sstevel@tonic-gate 	} else {
13297c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13307c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, MP_NO_MINOR,
13317c478bd9Sstevel@tonic-gate 				drvname, mp->mp_minorname);
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * Add minor perm entry
13407c478bd9Sstevel@tonic-gate  */
13417c478bd9Sstevel@tonic-gate static void
13427c478bd9Sstevel@tonic-gate add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
13437c478bd9Sstevel@tonic-gate {
13447c478bd9Sstevel@tonic-gate 	mperm_t **mp_head;
13457c478bd9Sstevel@tonic-gate 	mperm_t *freemp = NULL;
13467c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
13477c478bd9Sstevel@tonic-gate 	mperm_t **wildmp;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * Note that update_drv replace semantics require
13537c478bd9Sstevel@tonic-gate 	 * replacing matching entries with the new permissions.
13547c478bd9Sstevel@tonic-gate 	 */
13557c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
13567c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
13577c478bd9Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
13587c478bd9Sstevel@tonic-gate 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
13597c478bd9Sstevel@tonic-gate 		if (*wildmp)
13607c478bd9Sstevel@tonic-gate 			freemp = *wildmp;
13617c478bd9Sstevel@tonic-gate 		*wildmp = mp;
13627c478bd9Sstevel@tonic-gate 	} else {
13637c478bd9Sstevel@tonic-gate 		mperm_t *p, *v = NULL;
13647c478bd9Sstevel@tonic-gate 		for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
13657c478bd9Sstevel@tonic-gate 			if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
13667c478bd9Sstevel@tonic-gate 				if (v == NULL)
13677c478bd9Sstevel@tonic-gate 					dnp->dn_mperm = mp;
13687c478bd9Sstevel@tonic-gate 				else
13697c478bd9Sstevel@tonic-gate 					v->mp_next = mp;
13707c478bd9Sstevel@tonic-gate 				mp->mp_next = p->mp_next;
13717c478bd9Sstevel@tonic-gate 				freemp = p;
13727c478bd9Sstevel@tonic-gate 				goto replaced;
13737c478bd9Sstevel@tonic-gate 			}
13747c478bd9Sstevel@tonic-gate 		}
13757c478bd9Sstevel@tonic-gate 		if (p == NULL) {
13767c478bd9Sstevel@tonic-gate 			mp_head = &dnp->dn_mperm;
13777c478bd9Sstevel@tonic-gate 			if (*mp_head == NULL) {
13787c478bd9Sstevel@tonic-gate 				*mp_head = mp;
13797c478bd9Sstevel@tonic-gate 			} else {
13807c478bd9Sstevel@tonic-gate 				mp->mp_next = *mp_head;
13817c478bd9Sstevel@tonic-gate 				*mp_head = mp;
13827c478bd9Sstevel@tonic-gate 			}
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate replaced:
13867c478bd9Sstevel@tonic-gate 	if (freemp) {
13877c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13887c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
13897c478bd9Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
13907c478bd9Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
13917c478bd9Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
13927c478bd9Sstevel@tonic-gate 		}
13937c478bd9Sstevel@tonic-gate 		free_mperm(freemp);
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_MINORPERM) {
13967c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
13977c478bd9Sstevel@tonic-gate 		    drvname, mp->mp_minorname, mp->mp_mode & 0777,
13987c478bd9Sstevel@tonic-gate 		    mp->mp_uid, mp->mp_gid);
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate static int
14057c478bd9Sstevel@tonic-gate process_minorperm(int cmd, nvlist_t *nvl)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate 	char *minor;
14087c478bd9Sstevel@tonic-gate 	major_t major;
14097c478bd9Sstevel@tonic-gate 	mperm_t *mp;
14107c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
14117c478bd9Sstevel@tonic-gate 	char *name;
14127c478bd9Sstevel@tonic-gate 	int is_clone;
14137c478bd9Sstevel@tonic-gate 	major_t minmaj;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	ASSERT(cmd == MODLOADMINORPERM ||
14167c478bd9Sstevel@tonic-gate 	    cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	nvp = NULL;
14197c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
14207c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 		is_clone = 0;
14237c478bd9Sstevel@tonic-gate 		(void) nvpair_value_string(nvp, &minor);
14247c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(name);
14257c478bd9Sstevel@tonic-gate 		if (major != (major_t)-1) {
14267c478bd9Sstevel@tonic-gate 			mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
14277c478bd9Sstevel@tonic-gate 			if (minor == NULL || strlen(minor) == 0) {
14287c478bd9Sstevel@tonic-gate 				if (moddebug & MODDEBUG_MINORPERM) {
14297c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
14307c478bd9Sstevel@tonic-gate 				}
14317c478bd9Sstevel@tonic-gate 				minor = "*";
14327c478bd9Sstevel@tonic-gate 			}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 			/*
14357c478bd9Sstevel@tonic-gate 			 * The minor name of a node using the clone
14367c478bd9Sstevel@tonic-gate 			 * driver must be the driver name.  To avoid
14377c478bd9Sstevel@tonic-gate 			 * multiple searches, we map entries in the form
14387c478bd9Sstevel@tonic-gate 			 * clone:<driver> to <driver>:*.  This also allows us
14397c478bd9Sstevel@tonic-gate 			 * to filter out some of the litter in /etc/minor_perm.
14407c478bd9Sstevel@tonic-gate 			 * Minor perm alias entries where the name is not
14417c478bd9Sstevel@tonic-gate 			 * the driver kept on the clone list itself.
14427c478bd9Sstevel@tonic-gate 			 * This all seems very fragile as a driver could
14437c478bd9Sstevel@tonic-gate 			 * be introduced with an existing alias name.
14447c478bd9Sstevel@tonic-gate 			 */
14457c478bd9Sstevel@tonic-gate 			if (strcmp(name, "clone") == 0) {
14467c478bd9Sstevel@tonic-gate 				minmaj = ddi_name_to_major(minor);
14477c478bd9Sstevel@tonic-gate 				if (minmaj != (major_t)-1) {
14487c478bd9Sstevel@tonic-gate 					if (moddebug & MODDEBUG_MINORPERM) {
14497c478bd9Sstevel@tonic-gate 						cmn_err(CE_CONT,
14507c478bd9Sstevel@tonic-gate 						    "mapping %s:%s to %s:*\n",
14517c478bd9Sstevel@tonic-gate 						    name, minor, minor);
14527c478bd9Sstevel@tonic-gate 					}
14537c478bd9Sstevel@tonic-gate 					major = minmaj;
14547c478bd9Sstevel@tonic-gate 					name = minor;
14557c478bd9Sstevel@tonic-gate 					minor = "*";
14567c478bd9Sstevel@tonic-gate 					is_clone = 1;
14577c478bd9Sstevel@tonic-gate 				}
14587c478bd9Sstevel@tonic-gate 			}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 			if (mp) {
14617c478bd9Sstevel@tonic-gate 				mp->mp_minorname =
14627c478bd9Sstevel@tonic-gate 				    i_ddi_strdup(minor, KM_SLEEP);
14637c478bd9Sstevel@tonic-gate 			}
14647c478bd9Sstevel@tonic-gate 		} else {
14657c478bd9Sstevel@tonic-gate 			mp = NULL;
14667c478bd9Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM) {
14677c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
14687c478bd9Sstevel@tonic-gate 			}
14697c478bd9Sstevel@tonic-gate 		}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 		/* mode */
14727c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14737c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
14747c478bd9Sstevel@tonic-gate 		if (mp)
14757c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
14767c478bd9Sstevel@tonic-gate 		/* uid */
14777c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14787c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
14797c478bd9Sstevel@tonic-gate 		if (mp)
14807c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, &mp->mp_uid);
14817c478bd9Sstevel@tonic-gate 		/* gid */
14827c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14837c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
14847c478bd9Sstevel@tonic-gate 		if (mp) {
14857c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, &mp->mp_gid);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 			if (cmd == MODREMMINORPERM) {
14887c478bd9Sstevel@tonic-gate 				rem_minorperm(major, name, mp, is_clone);
14897c478bd9Sstevel@tonic-gate 				free_mperm(mp);
14907c478bd9Sstevel@tonic-gate 			} else {
14917c478bd9Sstevel@tonic-gate 				add_minorperm(major, name, mp, is_clone);
14927c478bd9Sstevel@tonic-gate 			}
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 	}
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	if (cmd == MODLOADMINORPERM)
14977c478bd9Sstevel@tonic-gate 		minorperm_loaded = 1;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	/*
15007c478bd9Sstevel@tonic-gate 	 * Reset permissions of cached dv_nodes
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	(void) devfs_reset_perm(DV_RESET_PERM);
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	return (0);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate static int
15087c478bd9Sstevel@tonic-gate modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	int error;
15117c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
15127c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(buflen, KM_SLEEP);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
15157c478bd9Sstevel@tonic-gate 		kmem_free(buf, buflen);
15167c478bd9Sstevel@tonic-gate 		return (error);
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
15207c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
15217c478bd9Sstevel@tonic-gate 	if (error)
15227c478bd9Sstevel@tonic-gate 		return (error);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	error = process_minorperm(cmd, nvl);
15257c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
15267c478bd9Sstevel@tonic-gate 	return (error);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate struct walk_args {
15307c478bd9Sstevel@tonic-gate 	char		*wa_drvname;
15317c478bd9Sstevel@tonic-gate 	list_t		wa_pathlist;
15327c478bd9Sstevel@tonic-gate };
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate struct path_elem {
15357c478bd9Sstevel@tonic-gate 	char		*pe_dir;
15367c478bd9Sstevel@tonic-gate 	char		*pe_nodename;
15377c478bd9Sstevel@tonic-gate 	list_node_t	pe_node;
15387c478bd9Sstevel@tonic-gate 	int		pe_dirlen;
15397c478bd9Sstevel@tonic-gate };
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15427c478bd9Sstevel@tonic-gate static int
15437c478bd9Sstevel@tonic-gate modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	struct walk_args *wargs = (struct walk_args *)arg;
15467c478bd9Sstevel@tonic-gate 	struct path_elem *pe;
15477c478bd9Sstevel@tonic-gate 	char *nodename;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
15507c478bd9Sstevel@tonic-gate 		return (INST_WALK_CONTINUE);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
15537c478bd9Sstevel@tonic-gate 	pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
15547c478bd9Sstevel@tonic-gate 	pe->pe_dirlen = strlen(pe->pe_dir) + 1;
15557c478bd9Sstevel@tonic-gate 	ASSERT(strrchr(pe->pe_dir, '/') != NULL);
15567c478bd9Sstevel@tonic-gate 	nodename = strrchr(pe->pe_dir, '/');
15577c478bd9Sstevel@tonic-gate 	*nodename++ = 0;
15587c478bd9Sstevel@tonic-gate 	pe->pe_nodename = nodename;
15597c478bd9Sstevel@tonic-gate 	list_insert_tail(&wargs->wa_pathlist, pe);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	return (INST_WALK_CONTINUE);
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate static int
15657c478bd9Sstevel@tonic-gate modctl_remdrv_cleanup(const char *u_drvname)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	struct walk_args *wargs;
15687c478bd9Sstevel@tonic-gate 	struct path_elem *pe;
15697c478bd9Sstevel@tonic-gate 	char *drvname;
15707c478bd9Sstevel@tonic-gate 	int err, rval = 0;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
15737c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
15747c478bd9Sstevel@tonic-gate 		kmem_free(drvname, MAXMODCONFNAME);
15757c478bd9Sstevel@tonic-gate 		return (err);
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * First go through the instance database.  For each
15807c478bd9Sstevel@tonic-gate 	 * instance of a device bound to the driver being
15817c478bd9Sstevel@tonic-gate 	 * removed, remove any underlying devfs attribute nodes.
15827c478bd9Sstevel@tonic-gate 	 *
15837c478bd9Sstevel@tonic-gate 	 * This is a two-step process.  First we go through
15847c478bd9Sstevel@tonic-gate 	 * the instance data itself, constructing a list of
15857c478bd9Sstevel@tonic-gate 	 * the nodes discovered.  The second step is then
15867c478bd9Sstevel@tonic-gate 	 * to find and remove any devfs attribute nodes
15877c478bd9Sstevel@tonic-gate 	 * for the instances discovered in the first step.
15887c478bd9Sstevel@tonic-gate 	 * The two-step process avoids any difficulties
15897c478bd9Sstevel@tonic-gate 	 * which could arise by holding the instance data
15907c478bd9Sstevel@tonic-gate 	 * lock with simultaneous devfs operations.
15917c478bd9Sstevel@tonic-gate 	 */
15927c478bd9Sstevel@tonic-gate 	wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	wargs->wa_drvname = drvname;
15957c478bd9Sstevel@tonic-gate 	list_create(&wargs->wa_pathlist,
15967c478bd9Sstevel@tonic-gate 	    sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	(void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
16017c478bd9Sstevel@tonic-gate 	    pe = list_next(&wargs->wa_pathlist, pe)) {
16027c478bd9Sstevel@tonic-gate 		err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
16037c478bd9Sstevel@tonic-gate 			(const char *)pe->pe_nodename);
16047c478bd9Sstevel@tonic-gate 		if (rval == 0)
16057c478bd9Sstevel@tonic-gate 			rval = err;
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
16097c478bd9Sstevel@tonic-gate 		list_remove(&wargs->wa_pathlist, pe);
16107c478bd9Sstevel@tonic-gate 		kmem_free(pe->pe_dir, pe->pe_dirlen);
16117c478bd9Sstevel@tonic-gate 		kmem_free(pe, sizeof (*pe));
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 	kmem_free(wargs, sizeof (*wargs));
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * Pseudo nodes aren't recorded in the instance database
16177c478bd9Sstevel@tonic-gate 	 * so any such nodes need to be handled separately.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
16207c478bd9Sstevel@tonic-gate 	if (rval == 0)
16217c478bd9Sstevel@tonic-gate 		rval = err;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	kmem_free(drvname, MAXMODCONFNAME);
16247c478bd9Sstevel@tonic-gate 	return (rval);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate static int
16287c478bd9Sstevel@tonic-gate modctl_allocpriv(const char *name)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
16317c478bd9Sstevel@tonic-gate 	int error;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
16347c478bd9Sstevel@tonic-gate 		kmem_free(pstr, PRIVNAME_MAX);
16357c478bd9Sstevel@tonic-gate 		return (error);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 	error = priv_getbyname(pstr, PRIV_ALLOC);
16387c478bd9Sstevel@tonic-gate 	if (error < 0)
16397c478bd9Sstevel@tonic-gate 		error = -error;
16407c478bd9Sstevel@tonic-gate 	else
16417c478bd9Sstevel@tonic-gate 		error = 0;
16427c478bd9Sstevel@tonic-gate 	kmem_free(pstr, PRIVNAME_MAX);
16437c478bd9Sstevel@tonic-gate 	return (error);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
1646*facf4a8dSllai1 static int
1647*facf4a8dSllai1 modctl_devexists(const char *upath, int pathlen)
1648*facf4a8dSllai1 {
1649*facf4a8dSllai1 	char	*path;
1650*facf4a8dSllai1 	int	ret;
1651*facf4a8dSllai1 
1652*facf4a8dSllai1 	/*
1653*facf4a8dSllai1 	 * copy in the path, including the terminating null
1654*facf4a8dSllai1 	 */
1655*facf4a8dSllai1 	pathlen++;
1656*facf4a8dSllai1 	if (pathlen <= 1 || pathlen > MAXPATHLEN)
1657*facf4a8dSllai1 		return (EINVAL);
1658*facf4a8dSllai1 	path = kmem_zalloc(pathlen + 1, KM_SLEEP);
1659*facf4a8dSllai1 	if ((ret = copyinstr(upath, path, pathlen, NULL)) == 0) {
1660*facf4a8dSllai1 		ret = sdev_modctl_devexists(path);
1661*facf4a8dSllai1 	}
1662*facf4a8dSllai1 
1663*facf4a8dSllai1 	kmem_free(path, pathlen + 1);
1664*facf4a8dSllai1 	return (ret);
1665*facf4a8dSllai1 }
1666*facf4a8dSllai1 
1667*facf4a8dSllai1 static int
1668*facf4a8dSllai1 modctl_devreaddir(const char *udir, int udirlen,
1669*facf4a8dSllai1     char *upaths, int64_t *ulensp)
1670*facf4a8dSllai1 {
1671*facf4a8dSllai1 	char	*paths = NULL;
1672*facf4a8dSllai1 	char	**dirlist = NULL;
1673*facf4a8dSllai1 	char	*dir;
1674*facf4a8dSllai1 	int64_t	ulens;
1675*facf4a8dSllai1 	int64_t	lens;
1676*facf4a8dSllai1 	int	i, n;
1677*facf4a8dSllai1 	int	ret = 0;
1678*facf4a8dSllai1 	char	*p;
1679*facf4a8dSllai1 	int	npaths;
1680*facf4a8dSllai1 	int	npaths_alloc;
1681*facf4a8dSllai1 
1682*facf4a8dSllai1 	/*
1683*facf4a8dSllai1 	 * If upaths is NULL then we are only computing the amount of space
1684*facf4a8dSllai1 	 * needed to return the paths, with the value returned in *ulensp. If we
1685*facf4a8dSllai1 	 * are copying out paths then we get the amount of space allocated by
1686*facf4a8dSllai1 	 * the caller. If the actual space needed for paths is larger, or
1687*facf4a8dSllai1 	 * things are changing out from under us, then we return EAGAIN.
1688*facf4a8dSllai1 	 */
1689*facf4a8dSllai1 	if (upaths) {
1690*facf4a8dSllai1 		if (ulensp == NULL)
1691*facf4a8dSllai1 			return (EINVAL);
1692*facf4a8dSllai1 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
1693*facf4a8dSllai1 			return (EFAULT);
1694*facf4a8dSllai1 	}
1695*facf4a8dSllai1 
1696*facf4a8dSllai1 	/*
1697*facf4a8dSllai1 	 * copyin the /dev path including terminating null
1698*facf4a8dSllai1 	 */
1699*facf4a8dSllai1 	udirlen++;
1700*facf4a8dSllai1 	if (udirlen <= 1 || udirlen > MAXPATHLEN)
1701*facf4a8dSllai1 		return (EINVAL);
1702*facf4a8dSllai1 	dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
1703*facf4a8dSllai1 	if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
1704*facf4a8dSllai1 		goto err;
1705*facf4a8dSllai1 
1706*facf4a8dSllai1 	if ((ret = sdev_modctl_readdir(dir, &dirlist,
1707*facf4a8dSllai1 	    &npaths, &npaths_alloc)) != 0) {
1708*facf4a8dSllai1 		ASSERT(dirlist == NULL);
1709*facf4a8dSllai1 		goto err;
1710*facf4a8dSllai1 	}
1711*facf4a8dSllai1 
1712*facf4a8dSllai1 	lens = 0;
1713*facf4a8dSllai1 	for (i = 0; i < npaths; i++) {
1714*facf4a8dSllai1 		lens += strlen(dirlist[i]) + 1;
1715*facf4a8dSllai1 	}
1716*facf4a8dSllai1 	lens++;		/* add one for double termination */
1717*facf4a8dSllai1 
1718*facf4a8dSllai1 	if (upaths) {
1719*facf4a8dSllai1 		if (lens > ulens) {
1720*facf4a8dSllai1 			ret = EAGAIN;
1721*facf4a8dSllai1 			goto out;
1722*facf4a8dSllai1 		}
1723*facf4a8dSllai1 
1724*facf4a8dSllai1 		paths = kmem_alloc(lens, KM_SLEEP);
1725*facf4a8dSllai1 
1726*facf4a8dSllai1 		p = paths;
1727*facf4a8dSllai1 		for (i = 0; i < npaths; i++) {
1728*facf4a8dSllai1 			n = strlen(dirlist[i]) + 1;
1729*facf4a8dSllai1 			bcopy(dirlist[i], p, n);
1730*facf4a8dSllai1 			p += n;
1731*facf4a8dSllai1 		}
1732*facf4a8dSllai1 		*p = 0;
1733*facf4a8dSllai1 
1734*facf4a8dSllai1 		if (copyout(paths, upaths, lens)) {
1735*facf4a8dSllai1 			ret = EFAULT;
1736*facf4a8dSllai1 			goto err;
1737*facf4a8dSllai1 		}
1738*facf4a8dSllai1 	}
1739*facf4a8dSllai1 
1740*facf4a8dSllai1 out:
1741*facf4a8dSllai1 	/* copy out the amount of space needed to hold the paths */
1742*facf4a8dSllai1 	if (copyout(&lens, ulensp, sizeof (lens)))
1743*facf4a8dSllai1 		ret = EFAULT;
1744*facf4a8dSllai1 
1745*facf4a8dSllai1 err:
1746*facf4a8dSllai1 	if (dirlist)
1747*facf4a8dSllai1 		sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
1748*facf4a8dSllai1 	if (paths)
1749*facf4a8dSllai1 		kmem_free(paths, lens);
1750*facf4a8dSllai1 	kmem_free(dir, udirlen + 1);
1751*facf4a8dSllai1 	return (ret);
1752*facf4a8dSllai1 }
1753*facf4a8dSllai1 
1754*facf4a8dSllai1 int
1755*facf4a8dSllai1 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
1756*facf4a8dSllai1 {
1757*facf4a8dSllai1 	int error = 0;
1758*facf4a8dSllai1 
1759*facf4a8dSllai1 	switch (subcmd) {
1760*facf4a8dSllai1 	case MODDEVNAME_LOOKUPDOOR:
1761*facf4a8dSllai1 	case MODDEVNAME_DEVFSADMNODE:
1762*facf4a8dSllai1 		error = devname_filename_register(subcmd, (char *)a1);
1763*facf4a8dSllai1 		break;
1764*facf4a8dSllai1 	case MODDEVNAME_NSMAPS:
1765*facf4a8dSllai1 		error = devname_nsmaps_register((char *)a1, (size_t)a2);
1766*facf4a8dSllai1 		break;
1767*facf4a8dSllai1 	case MODDEVNAME_PROFILE:
1768*facf4a8dSllai1 		error = devname_profile_update((char *)a1, (size_t)a2);
1769*facf4a8dSllai1 		break;
1770*facf4a8dSllai1 	case MODDEVNAME_RECONFIG:
1771*facf4a8dSllai1 		i_ddi_set_reconfig();
1772*facf4a8dSllai1 		break;
1773*facf4a8dSllai1 	case MODDEVNAME_SYSAVAIL:
1774*facf4a8dSllai1 		i_ddi_set_sysavail();
1775*facf4a8dSllai1 		break;
1776*facf4a8dSllai1 	default:
1777*facf4a8dSllai1 		error = EINVAL;
1778*facf4a8dSllai1 		break;
1779*facf4a8dSllai1 	}
1780*facf4a8dSllai1 
1781*facf4a8dSllai1 	return (error);
1782*facf4a8dSllai1 }
1783*facf4a8dSllai1 
17847c478bd9Sstevel@tonic-gate /*ARGSUSED5*/
17857c478bd9Sstevel@tonic-gate int
17867c478bd9Sstevel@tonic-gate modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
17877c478bd9Sstevel@tonic-gate     uintptr_t a5)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	int	error = EINVAL;
17907c478bd9Sstevel@tonic-gate 	dev_t	dev;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	if (secpolicy_modctl(CRED(), cmd) != 0)
17937c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	switch (cmd) {
17967c478bd9Sstevel@tonic-gate 	case MODLOAD:		/* load a module */
17977c478bd9Sstevel@tonic-gate 		error = modctl_modload((int)a1, (char *)a2, (int *)a3);
17987c478bd9Sstevel@tonic-gate 		break;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	case MODUNLOAD:		/* unload a module */
18017c478bd9Sstevel@tonic-gate 		error = modctl_modunload((modid_t)a1);
18027c478bd9Sstevel@tonic-gate 		break;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	case MODINFO:		/* get module status */
18057c478bd9Sstevel@tonic-gate 		error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
18067c478bd9Sstevel@tonic-gate 		break;
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	case MODRESERVED:	/* get last major number in range */
18097c478bd9Sstevel@tonic-gate 		error = modctl_modreserve((modid_t)a1, (int *)a2);
18107c478bd9Sstevel@tonic-gate 		break;
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	case MODSETMINIROOT:	/* we are running in miniroot */
18137c478bd9Sstevel@tonic-gate 		isminiroot = 1;
18147c478bd9Sstevel@tonic-gate 		error = 0;
18157c478bd9Sstevel@tonic-gate 		break;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	case MODADDMAJBIND:	/* read major binding file */
18187c478bd9Sstevel@tonic-gate 		error = modctl_add_major((int *)a2);
18197c478bd9Sstevel@tonic-gate 		break;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	case MODGETPATHLEN:	/* get modpath length */
18227c478bd9Sstevel@tonic-gate 		error = modctl_getmodpathlen((int *)a2);
18237c478bd9Sstevel@tonic-gate 		break;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	case MODGETPATH:	/* get modpath */
18267c478bd9Sstevel@tonic-gate 		error = modctl_getmodpath((char *)a2);
18277c478bd9Sstevel@tonic-gate 		break;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	case MODREADSYSBIND:	/* read system call binding file */
18307c478bd9Sstevel@tonic-gate 		error = modctl_read_sysbinding_file();
18317c478bd9Sstevel@tonic-gate 		break;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	case MODGETMAJBIND:	/* get major number for named device */
18347c478bd9Sstevel@tonic-gate 		error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
18357c478bd9Sstevel@tonic-gate 		break;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	case MODGETNAME:	/* get name of device given major number */
18387c478bd9Sstevel@tonic-gate 		error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
18397c478bd9Sstevel@tonic-gate 		break;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	case MODDEVT2INSTANCE:
18427c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
18437c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
18447c478bd9Sstevel@tonic-gate 		}
18457c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
18467c478bd9Sstevel@tonic-gate 		else {
18477c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate #endif
18507c478bd9Sstevel@tonic-gate 		error = modctl_devt2instance(dev, (int *)a2);
18517c478bd9Sstevel@tonic-gate 		break;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	case MODSIZEOF_DEVID:	/* sizeof device id of device given dev_t */
18547c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
18557c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
18587c478bd9Sstevel@tonic-gate 		else {
18597c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate #endif
18627c478bd9Sstevel@tonic-gate 		error = modctl_sizeof_devid(dev, (uint_t *)a2);
18637c478bd9Sstevel@tonic-gate 		break;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	case MODGETDEVID:	/* get device id of device given dev_t */
18667c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
18677c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
18687c478bd9Sstevel@tonic-gate 		}
18697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
18707c478bd9Sstevel@tonic-gate 		else {
18717c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate #endif
18747c478bd9Sstevel@tonic-gate 		error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
18757c478bd9Sstevel@tonic-gate 		break;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	case MODSIZEOF_MINORNAME:	/* sizeof minor nm of dev_t/spectype */
18787c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
18797c478bd9Sstevel@tonic-gate 			error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
18807c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
18837c478bd9Sstevel@tonic-gate 		else {
18847c478bd9Sstevel@tonic-gate 			error = modctl_sizeof_minorname(expldev(a1), (int)a2,
18857c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
18867c478bd9Sstevel@tonic-gate 		}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate #endif
18897c478bd9Sstevel@tonic-gate 		break;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	case MODGETMINORNAME:   /* get minor name of dev_t and spec type */
18927c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
18937c478bd9Sstevel@tonic-gate 			error = modctl_get_minorname((dev_t)a1, (int)a2,
18947c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
18977c478bd9Sstevel@tonic-gate 		else {
18987c478bd9Sstevel@tonic-gate 			error = modctl_get_minorname(expldev(a1), (int)a2,
18997c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate #endif
19027c478bd9Sstevel@tonic-gate 		break;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	case MODGETDEVFSPATH_LEN:	/* sizeof path nm of dev_t/spectype */
19057c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
19067c478bd9Sstevel@tonic-gate 			error = modctl_devfspath_len((dev_t)a1, (int)a2,
19077c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
19107c478bd9Sstevel@tonic-gate 		else {
19117c478bd9Sstevel@tonic-gate 			error = modctl_devfspath_len(expldev(a1), (int)a2,
19127c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
19137c478bd9Sstevel@tonic-gate 		}
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate #endif
19167c478bd9Sstevel@tonic-gate 		break;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	case MODGETDEVFSPATH:   /* get path name of dev_t and spec type */
19197c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
19207c478bd9Sstevel@tonic-gate 			error = modctl_devfspath((dev_t)a1, (int)a2,
19217c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
19227c478bd9Sstevel@tonic-gate 		}
19237c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
19247c478bd9Sstevel@tonic-gate 		else {
19257c478bd9Sstevel@tonic-gate 			error = modctl_devfspath(expldev(a1), (int)a2,
19267c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
19277c478bd9Sstevel@tonic-gate 		}
19287c478bd9Sstevel@tonic-gate #endif
19297c478bd9Sstevel@tonic-gate 		break;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	case MODEVENTS:
19337c478bd9Sstevel@tonic-gate 		error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
19347c478bd9Sstevel@tonic-gate 		break;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	case MODGETFBNAME:	/* get the framebuffer name */
19377c478bd9Sstevel@tonic-gate 		error = modctl_get_fbname((char *)a1);
19387c478bd9Sstevel@tonic-gate 		break;
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	case MODREREADDACF:	/* reread dacf rule database from given file */
19417c478bd9Sstevel@tonic-gate 		error = modctl_reread_dacf((char *)a1);
19427c478bd9Sstevel@tonic-gate 		break;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	case MODLOADDRVCONF:	/* load driver.conf file for major */
19457c478bd9Sstevel@tonic-gate 		error = modctl_load_drvconf((major_t)a1);
19467c478bd9Sstevel@tonic-gate 		break;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	case MODUNLOADDRVCONF:	/* unload driver.conf file for major */
19497c478bd9Sstevel@tonic-gate 		error = modctl_unload_drvconf((major_t)a1);
19507c478bd9Sstevel@tonic-gate 		break;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	case MODREMMAJBIND:	/* remove a major binding */
19537c478bd9Sstevel@tonic-gate 		error = modctl_rem_major((major_t)a1);
19547c478bd9Sstevel@tonic-gate 		break;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	case MODDEVID2PATHS:	/* get paths given devid */
19577c478bd9Sstevel@tonic-gate 		error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
19587c478bd9Sstevel@tonic-gate 		    (uint_t)a3, (size_t *)a4, (char *)a5);
19597c478bd9Sstevel@tonic-gate 		break;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	case MODSETDEVPOLICY:	/* establish device policy */
19627c478bd9Sstevel@tonic-gate 		error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
19637c478bd9Sstevel@tonic-gate 		break;
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICY:	/* get device policy */
19667c478bd9Sstevel@tonic-gate 		error = devpolicy_get((int *)a1, (size_t)a2,
19677c478bd9Sstevel@tonic-gate 				(devplcysys_t *)a3);
19687c478bd9Sstevel@tonic-gate 		break;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	case MODALLOCPRIV:
19717c478bd9Sstevel@tonic-gate 		error = modctl_allocpriv((const char *)a1);
19727c478bd9Sstevel@tonic-gate 		break;
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
19757c478bd9Sstevel@tonic-gate 		error = devpolicy_getbyname((size_t)a1,
19767c478bd9Sstevel@tonic-gate 		    (devplcysys_t *)a2, (char *)a3);
19777c478bd9Sstevel@tonic-gate 		break;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	case MODLOADMINORPERM:
19807c478bd9Sstevel@tonic-gate 	case MODADDMINORPERM:
19817c478bd9Sstevel@tonic-gate 	case MODREMMINORPERM:
19827c478bd9Sstevel@tonic-gate 		error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
19837c478bd9Sstevel@tonic-gate 		break;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	case MODREMDRVCLEANUP:
19867c478bd9Sstevel@tonic-gate 		error = modctl_remdrv_cleanup((const char *)a1);
19877c478bd9Sstevel@tonic-gate 		break;
19887c478bd9Sstevel@tonic-gate 
1989*facf4a8dSllai1 	case MODDEVEXISTS:	/* non-reconfiguring /dev lookup */
1990*facf4a8dSllai1 		error = modctl_devexists((const char *)a1, (size_t)a2);
1991*facf4a8dSllai1 		break;
1992*facf4a8dSllai1 
1993*facf4a8dSllai1 	case MODDEVREADDIR:	/* non-reconfiguring /dev readdir */
1994*facf4a8dSllai1 		error = modctl_devreaddir((const char *)a1, (size_t)a2,
1995*facf4a8dSllai1 		    (char *)a3, (int64_t *)a4);
1996*facf4a8dSllai1 		break;
1997*facf4a8dSllai1 
1998*facf4a8dSllai1 	case MODDEVNAME:
1999*facf4a8dSllai1 		error = modctl_moddevname((int)a1, a2, a3);
2000*facf4a8dSllai1 		break;
2001*facf4a8dSllai1 
20027c478bd9Sstevel@tonic-gate 	default:
20037c478bd9Sstevel@tonic-gate 		error = EINVAL;
20047c478bd9Sstevel@tonic-gate 		break;
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate /*
20117c478bd9Sstevel@tonic-gate  * Calls to kobj_load_module()() are handled off to this routine in a
20127c478bd9Sstevel@tonic-gate  * separate thread.
20137c478bd9Sstevel@tonic-gate  */
20147c478bd9Sstevel@tonic-gate static void
20157c478bd9Sstevel@tonic-gate modload_thread(struct loadmt *ltp)
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	/* load the module and signal the creator of this thread */
20187c478bd9Sstevel@tonic-gate 	kmutex_t	cpr_lk;
20197c478bd9Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
20227c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
20237c478bd9Sstevel@tonic-gate 	/* borrow the devi lock from thread which invoked us */
20247c478bd9Sstevel@tonic-gate 	pm_borrow_lock(ltp->owner);
20257c478bd9Sstevel@tonic-gate 	ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
20267c478bd9Sstevel@tonic-gate 	pm_return_lock();
20277c478bd9Sstevel@tonic-gate 	sema_v(&ltp->sema);
20287c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
20297c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
20307c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
20317c478bd9Sstevel@tonic-gate 	thread_exit();
20327c478bd9Sstevel@tonic-gate }
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate /*
20357c478bd9Sstevel@tonic-gate  * load a module, adding a reference if caller specifies rmodp.  If rmodp
20367c478bd9Sstevel@tonic-gate  * is specified then an errno is returned, otherwise a module index is
20377c478bd9Sstevel@tonic-gate  * returned (-1 on error).
20387c478bd9Sstevel@tonic-gate  */
20397c478bd9Sstevel@tonic-gate static int
20407c478bd9Sstevel@tonic-gate modrload(char *subdir, char *filename, struct modctl **rmodp)
20417c478bd9Sstevel@tonic-gate {
20427c478bd9Sstevel@tonic-gate 	struct modctl *modp;
20437c478bd9Sstevel@tonic-gate 	size_t size;
20447c478bd9Sstevel@tonic-gate 	char *fullname;
20457c478bd9Sstevel@tonic-gate 	int retval = EINVAL;
20467c478bd9Sstevel@tonic-gate 	int id = -1;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	if (rmodp)
20497c478bd9Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	if (subdir != NULL) {
20527c478bd9Sstevel@tonic-gate 		/*
20537c478bd9Sstevel@tonic-gate 		 * refuse / in filename to prevent "../" escapes.
20547c478bd9Sstevel@tonic-gate 		 */
20557c478bd9Sstevel@tonic-gate 		if (strchr(filename, '/') != NULL)
20567c478bd9Sstevel@tonic-gate 			return (rmodp ? retval : id);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 		/*
20597c478bd9Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
20607c478bd9Sstevel@tonic-gate 		 */
20617c478bd9Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
20627c478bd9Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
20637c478bd9Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
20647c478bd9Sstevel@tonic-gate 	} else {
20657c478bd9Sstevel@tonic-gate 		fullname = filename;
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	modp = mod_hold_installed_mod(fullname, 1, &retval);
20697c478bd9Sstevel@tonic-gate 	if (modp != NULL) {
20707c478bd9Sstevel@tonic-gate 		id = modp->mod_id;
20717c478bd9Sstevel@tonic-gate 		if (rmodp) {
20727c478bd9Sstevel@tonic-gate 			/* add mod_ref and return *rmodp */
20737c478bd9Sstevel@tonic-gate 			mutex_enter(&mod_lock);
20747c478bd9Sstevel@tonic-gate 			modp->mod_ref++;
20757c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
20767c478bd9Sstevel@tonic-gate 			*rmodp = modp;
20777c478bd9Sstevel@tonic-gate 		}
20787c478bd9Sstevel@tonic-gate 		mod_release_mod(modp);
20797c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, modload, 1);
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate done:	if (subdir != NULL)
20837c478bd9Sstevel@tonic-gate 		kmem_free(fullname, size);
20847c478bd9Sstevel@tonic-gate 	return (rmodp ? retval : id);
20857c478bd9Sstevel@tonic-gate }
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate /*
20887c478bd9Sstevel@tonic-gate  * This is the primary kernel interface to load a module. It loads and
20897c478bd9Sstevel@tonic-gate  * installs the named module.  It does not hold mod_ref of the module, so
20907c478bd9Sstevel@tonic-gate  * a module unload attempt can occur at any time - it is up to the
20917c478bd9Sstevel@tonic-gate  * _fini/mod_remove implementation to determine if unload will succeed.
20927c478bd9Sstevel@tonic-gate  */
20937c478bd9Sstevel@tonic-gate int
20947c478bd9Sstevel@tonic-gate modload(char *subdir, char *filename)
20957c478bd9Sstevel@tonic-gate {
20967c478bd9Sstevel@tonic-gate 	return (modrload(subdir, filename, NULL));
20977c478bd9Sstevel@tonic-gate }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007aec1d6eScindi  * Load a module using a series of qualified names from most specific to least
21017aec1d6eScindi  * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
21027aec1d6eScindi  *
21037aec1d6eScindi  * foo/bar.baz.1.2.3
21047aec1d6eScindi  * foo/bar.baz.1.2
21057aec1d6eScindi  * foo/bar.baz.1
21067aec1d6eScindi  *
21077aec1d6eScindi  * Return the module ID on success; -1 if no module was loaded.
21087aec1d6eScindi  */
21097aec1d6eScindi int
21107aec1d6eScindi modload_qualified(const char *subdir, const char *p1,
21117aec1d6eScindi     const char *p2, const char *delim, uint_t suffv[], int suffc)
21127aec1d6eScindi {
21137aec1d6eScindi 	char path[MOD_MAXPATH];
21147aec1d6eScindi 	size_t n, resid = sizeof (path);
21157aec1d6eScindi 	char *p = path;
21167aec1d6eScindi 
21177aec1d6eScindi 	char **dotv;
21187aec1d6eScindi 	int i, rc, id;
21197aec1d6eScindi 	modctl_t *mp;
21207aec1d6eScindi 
21217aec1d6eScindi 	if (p2 != NULL)
21227aec1d6eScindi 		n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
21237aec1d6eScindi 	else
21247aec1d6eScindi 		n = snprintf(p, resid, "%s/%s", subdir, p1);
21257aec1d6eScindi 
21267aec1d6eScindi 	if (n >= resid)
21277aec1d6eScindi 		return (-1);
21287aec1d6eScindi 
21297aec1d6eScindi 	p += n;
21307aec1d6eScindi 	resid -= n;
21317aec1d6eScindi 	dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
21327aec1d6eScindi 
21337aec1d6eScindi 	for (i = 0; i < suffc; i++) {
21347aec1d6eScindi 		dotv[i] = p;
21357aec1d6eScindi 		n = snprintf(p, resid, "%s%u", delim, suffv[i]);
21367aec1d6eScindi 
21377aec1d6eScindi 		if (n >= resid) {
21387aec1d6eScindi 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
21397aec1d6eScindi 			return (-1);
21407aec1d6eScindi 		}
21417aec1d6eScindi 
21427aec1d6eScindi 		p += n;
21437aec1d6eScindi 		resid -= n;
21447aec1d6eScindi 	}
21457aec1d6eScindi 
21467aec1d6eScindi 	dotv[suffc] = p;
21477aec1d6eScindi 
21487aec1d6eScindi 	for (i = suffc; i >= 0; i--) {
21497aec1d6eScindi 		dotv[i][0] = '\0';
21507aec1d6eScindi 		mp = mod_hold_installed_mod(path, 1, &rc);
21517aec1d6eScindi 
21527aec1d6eScindi 		if (mp != NULL) {
21537aec1d6eScindi 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
21547aec1d6eScindi 			id = mp->mod_id;
21557aec1d6eScindi 			mod_release_mod(mp);
21567aec1d6eScindi 			return (id);
21577aec1d6eScindi 		}
21587aec1d6eScindi 	}
21597aec1d6eScindi 
21607aec1d6eScindi 	kmem_free(dotv, sizeof (char *) * (suffc + 1));
21617aec1d6eScindi 	return (-1);
21627aec1d6eScindi }
21637aec1d6eScindi 
21647aec1d6eScindi /*
21657c478bd9Sstevel@tonic-gate  * Load a module.
21667c478bd9Sstevel@tonic-gate  */
21677c478bd9Sstevel@tonic-gate int
21687c478bd9Sstevel@tonic-gate modloadonly(char *subdir, char *filename)
21697c478bd9Sstevel@tonic-gate {
21707c478bd9Sstevel@tonic-gate 	struct modctl *modp;
21717c478bd9Sstevel@tonic-gate 	char *fullname;
21727c478bd9Sstevel@tonic-gate 	size_t size;
21737c478bd9Sstevel@tonic-gate 	int id, retval;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (subdir != NULL) {
21767c478bd9Sstevel@tonic-gate 		/*
21777c478bd9Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
21787c478bd9Sstevel@tonic-gate 		 */
21797c478bd9Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
21807c478bd9Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
21817c478bd9Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
21827c478bd9Sstevel@tonic-gate 	} else {
21837c478bd9Sstevel@tonic-gate 		fullname = filename;
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	modp = mod_hold_loaded_mod(NULL, fullname, &retval);
21877c478bd9Sstevel@tonic-gate 	if (modp) {
21887c478bd9Sstevel@tonic-gate 		id = modp->mod_id;
21897c478bd9Sstevel@tonic-gate 		mod_release_mod(modp);
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	if (subdir != NULL)
21937c478bd9Sstevel@tonic-gate 		kmem_free(fullname, size);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (retval == 0)
21967c478bd9Sstevel@tonic-gate 		return (id);
21977c478bd9Sstevel@tonic-gate 	return (-1);
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate /*
22017c478bd9Sstevel@tonic-gate  * Try to uninstall and unload a module, removing a reference if caller
22027c478bd9Sstevel@tonic-gate  * specifies rmodp.
22037c478bd9Sstevel@tonic-gate  */
22047c478bd9Sstevel@tonic-gate static int
22057c478bd9Sstevel@tonic-gate modunrload(modid_t id, struct modctl **rmodp, int unload)
22067c478bd9Sstevel@tonic-gate {
22077c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
22087c478bd9Sstevel@tonic-gate 	int		retval;
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	if (rmodp)
22117c478bd9Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
22147c478bd9Sstevel@tonic-gate 		return (EINVAL);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	if (rmodp) {
22177c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
22187c478bd9Sstevel@tonic-gate 		modp->mod_ref--;
22197c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
22207c478bd9Sstevel@tonic-gate 		*rmodp = modp;
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	if (unload) {
22247c478bd9Sstevel@tonic-gate 		retval = moduninstall(modp);
22257c478bd9Sstevel@tonic-gate 		if (retval == 0) {
22267c478bd9Sstevel@tonic-gate 			mod_unload(modp);
22277c478bd9Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
22287c478bd9Sstevel@tonic-gate 		} else if (retval == EALREADY)
22297c478bd9Sstevel@tonic-gate 			retval = 0;	/* already unloaded, not an error */
22307c478bd9Sstevel@tonic-gate 	} else
22317c478bd9Sstevel@tonic-gate 		retval = 0;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
22347c478bd9Sstevel@tonic-gate 	return (retval);
22357c478bd9Sstevel@tonic-gate }
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate /*
22387c478bd9Sstevel@tonic-gate  * Uninstall and unload a module.
22397c478bd9Sstevel@tonic-gate  */
22407c478bd9Sstevel@tonic-gate int
22417c478bd9Sstevel@tonic-gate modunload(modid_t id)
22427c478bd9Sstevel@tonic-gate {
2243a7aa4df7Scth 	int		retval;
2244a7aa4df7Scth 
2245a7aa4df7Scth 	/* synchronize with any active modunload_disable() */
2246a7aa4df7Scth 	modunload_begin();
2247a7aa4df7Scth 	if (ddi_root_node())
2248a7aa4df7Scth 		(void) devfs_clean(ddi_root_node(), NULL, 0);
2249a7aa4df7Scth 	retval = modunrload(id, NULL, 1);
2250a7aa4df7Scth 	modunload_end();
2251a7aa4df7Scth 	return (retval);
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate /*
22557c478bd9Sstevel@tonic-gate  * Return status of a loaded module.
22567c478bd9Sstevel@tonic-gate  */
22577c478bd9Sstevel@tonic-gate static int
22587c478bd9Sstevel@tonic-gate modinfo(modid_t id, struct modinfo *modinfop)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
22617c478bd9Sstevel@tonic-gate 	modid_t		mid;
22627c478bd9Sstevel@tonic-gate 	int		i;
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	mid = modinfop->mi_id;
22657c478bd9Sstevel@tonic-gate 	if (modinfop->mi_info & MI_INFO_ALL) {
22667c478bd9Sstevel@tonic-gate 		while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
22677c478bd9Sstevel@tonic-gate 			if ((modinfop->mi_info & MI_INFO_CNT) ||
22687c478bd9Sstevel@tonic-gate 			    modp->mod_installed)
22697c478bd9Sstevel@tonic-gate 				break;
22707c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
22717c478bd9Sstevel@tonic-gate 		}
22727c478bd9Sstevel@tonic-gate 		if (modp == NULL)
22737c478bd9Sstevel@tonic-gate 			return (EINVAL);
22747c478bd9Sstevel@tonic-gate 	} else {
22757c478bd9Sstevel@tonic-gate 		modp = mod_hold_by_id(id);
22767c478bd9Sstevel@tonic-gate 		if (modp == NULL)
22777c478bd9Sstevel@tonic-gate 			return (EINVAL);
22787c478bd9Sstevel@tonic-gate 		if (!(modinfop->mi_info & MI_INFO_CNT) &&
22797c478bd9Sstevel@tonic-gate 		    (modp->mod_installed == 0)) {
22807c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
22817c478bd9Sstevel@tonic-gate 			return (EINVAL);
22827c478bd9Sstevel@tonic-gate 		}
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	modinfop->mi_rev = 0;
22867c478bd9Sstevel@tonic-gate 	modinfop->mi_state = 0;
22877c478bd9Sstevel@tonic-gate 	for (i = 0; i < MODMAXLINK; i++) {
22887c478bd9Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_p0 = -1;
22897c478bd9Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate 	if (modp->mod_loaded) {
22927c478bd9Sstevel@tonic-gate 		modinfop->mi_state = MI_LOADED;
22937c478bd9Sstevel@tonic-gate 		kobj_getmodinfo(modp->mod_mp, modinfop);
22947c478bd9Sstevel@tonic-gate 	}
22957c478bd9Sstevel@tonic-gate 	if (modp->mod_installed) {
22967c478bd9Sstevel@tonic-gate 		modinfop->mi_state |= MI_INSTALLED;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		(void) mod_getinfo(modp, modinfop);
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	modinfop->mi_id = modp->mod_id;
23027c478bd9Sstevel@tonic-gate 	modinfop->mi_loadcnt = modp->mod_loadcnt;
23037c478bd9Sstevel@tonic-gate 	(void) strcpy(modinfop->mi_name, modp->mod_modname);
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
23067c478bd9Sstevel@tonic-gate 	return (0);
23077c478bd9Sstevel@tonic-gate }
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
23107c478bd9Sstevel@tonic-gate static char no_err[] = "No error function for weak stub %s";
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate /*
23137c478bd9Sstevel@tonic-gate  * used by the stubs themselves to load and hold a module.
23147c478bd9Sstevel@tonic-gate  * Returns  0 if the module is successfully held;
23157c478bd9Sstevel@tonic-gate  *	    the stub needs to call mod_release_stub().
23167c478bd9Sstevel@tonic-gate  *	    -1 if the stub should just call the err_fcn.
23177c478bd9Sstevel@tonic-gate  * Note that this code is stretched out so that we avoid subroutine calls
23187c478bd9Sstevel@tonic-gate  * and optimize for the most likely case.  That is, the case where the
23197c478bd9Sstevel@tonic-gate  * module is loaded and installed and not held.  In that case we just inc
23207c478bd9Sstevel@tonic-gate  * the mod_ref count and continue.
23217c478bd9Sstevel@tonic-gate  */
23227c478bd9Sstevel@tonic-gate int
23237c478bd9Sstevel@tonic-gate mod_hold_stub(struct mod_stub_info *stub)
23247c478bd9Sstevel@tonic-gate {
23257c478bd9Sstevel@tonic-gate 	struct modctl *mp;
23267c478bd9Sstevel@tonic-gate 	struct mod_modinfo *mip;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	mip = stub->mods_modinfo;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	/* we do mod_hold_by_modctl inline for speed */
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate mod_check_again:
23357c478bd9Sstevel@tonic-gate 	if ((mp = mip->mp) != NULL) {
23367c478bd9Sstevel@tonic-gate 		if (mp->mod_busy == 0) {
23377c478bd9Sstevel@tonic-gate 			if (mp->mod_installed) {
23387c478bd9Sstevel@tonic-gate 				/* increment the reference count */
23397c478bd9Sstevel@tonic-gate 				mp->mod_ref++;
23407c478bd9Sstevel@tonic-gate 				ASSERT(mp->mod_ref && mp->mod_installed);
23417c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
23427c478bd9Sstevel@tonic-gate 				return (0);
23437c478bd9Sstevel@tonic-gate 			} else {
23447c478bd9Sstevel@tonic-gate 				mp->mod_busy = 1;
23457c478bd9Sstevel@tonic-gate 				mp->mod_inprogress_thread =
23467c478bd9Sstevel@tonic-gate 				    (curthread == NULL ?
23477c478bd9Sstevel@tonic-gate 				    (kthread_id_t)-1 : curthread);
23487c478bd9Sstevel@tonic-gate 			}
23497c478bd9Sstevel@tonic-gate 		} else {
23507c478bd9Sstevel@tonic-gate 			/*
23517c478bd9Sstevel@tonic-gate 			 * wait one time and then go see if someone
23527c478bd9Sstevel@tonic-gate 			 * else has resolved the stub (set mip->mp).
23537c478bd9Sstevel@tonic-gate 			 */
23547c478bd9Sstevel@tonic-gate 			if (mod_hold_by_modctl(mp,
23557c478bd9Sstevel@tonic-gate 			    MOD_WAIT_ONCE | MOD_LOCK_HELD))
23567c478bd9Sstevel@tonic-gate 				goto mod_check_again;
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 			/*
23597c478bd9Sstevel@tonic-gate 			 * what we have now may have been unloaded!, in
23607c478bd9Sstevel@tonic-gate 			 * that case, mip->mp will be NULL, we'll hit this
23617c478bd9Sstevel@tonic-gate 			 * module and load again..
23627c478bd9Sstevel@tonic-gate 			 */
23637c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
23647c478bd9Sstevel@tonic-gate 		}
23657c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
23667c478bd9Sstevel@tonic-gate 	} else {
23677c478bd9Sstevel@tonic-gate 		/* first time we've hit this module */
23687c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
23697c478bd9Sstevel@tonic-gate 		mp = mod_hold_by_name(mip->modm_module_name);
23707c478bd9Sstevel@tonic-gate 		mip->mp = mp;
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	/*
23747c478bd9Sstevel@tonic-gate 	 * If we are here, it means that the following conditions
23757c478bd9Sstevel@tonic-gate 	 * are satisfied.
23767c478bd9Sstevel@tonic-gate 	 *
23777c478bd9Sstevel@tonic-gate 	 * mip->mp != NULL
23787c478bd9Sstevel@tonic-gate 	 * this thread has set the mp->mod_busy = 1
23797c478bd9Sstevel@tonic-gate 	 * mp->mod_installed = 0
23807c478bd9Sstevel@tonic-gate 	 *
23817c478bd9Sstevel@tonic-gate 	 */
23827c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
23837c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy == 1);
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	if (mp->mod_installed == 0) {
23867c478bd9Sstevel@tonic-gate 		/* Module not loaded, if weak stub don't load it */
23877c478bd9Sstevel@tonic-gate 		if (stub->mods_flag & MODS_WEAK) {
23887c478bd9Sstevel@tonic-gate 			if (stub->mods_errfcn == NULL) {
23897c478bd9Sstevel@tonic-gate 				mod_release_mod(mp);
23907c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, no_err,
23917c478bd9Sstevel@tonic-gate 				    mip->modm_module_name);
23927c478bd9Sstevel@tonic-gate 			}
23937c478bd9Sstevel@tonic-gate 		} else {
23947c478bd9Sstevel@tonic-gate 			/* Not a weak stub so load the module */
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 			if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
23977c478bd9Sstevel@tonic-gate 				/*
23987c478bd9Sstevel@tonic-gate 				 * If mod_load() was successful
23997c478bd9Sstevel@tonic-gate 				 * and modinstall() failed, then
24007c478bd9Sstevel@tonic-gate 				 * unload the module.
24017c478bd9Sstevel@tonic-gate 				 */
24027c478bd9Sstevel@tonic-gate 				if (mp->mod_loaded)
24037c478bd9Sstevel@tonic-gate 					mod_unload(mp);
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 				mod_release_mod(mp);
24067c478bd9Sstevel@tonic-gate 				if (stub->mods_errfcn == NULL) {
24077c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, mod_stub_err,
24087c478bd9Sstevel@tonic-gate 					    mip->modm_module_name);
24097c478bd9Sstevel@tonic-gate 				} else {
24107c478bd9Sstevel@tonic-gate 					return (-1);
24117c478bd9Sstevel@tonic-gate 				}
24127c478bd9Sstevel@tonic-gate 			}
24137c478bd9Sstevel@tonic-gate 		}
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	/*
24177c478bd9Sstevel@tonic-gate 	 * At this point module is held and loaded. Release
24187c478bd9Sstevel@tonic-gate 	 * the mod_busy and mod_inprogress_thread before
24197c478bd9Sstevel@tonic-gate 	 * returning. We actually call mod_release() here so
24207c478bd9Sstevel@tonic-gate 	 * that if another stub wants to access this module,
24217c478bd9Sstevel@tonic-gate 	 * it can do so. mod_ref is incremented before mod_release()
24227c478bd9Sstevel@tonic-gate 	 * is called to prevent someone else from snatching the
24237c478bd9Sstevel@tonic-gate 	 * module from this thread.
24247c478bd9Sstevel@tonic-gate 	 */
24257c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
24267c478bd9Sstevel@tonic-gate 	mp->mod_ref++;
24277c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
24287c478bd9Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
24297c478bd9Sstevel@tonic-gate 	mod_release(mp);
24307c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
24317c478bd9Sstevel@tonic-gate 	return (0);
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate void
24357c478bd9Sstevel@tonic-gate mod_release_stub(struct mod_stub_info *stub)
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate 	struct modctl *mp = stub->mods_modinfo->mp;
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	/* inline mod_release_mod */
24407c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
24417c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
24427c478bd9Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
24437c478bd9Sstevel@tonic-gate 	mp->mod_ref--;
24447c478bd9Sstevel@tonic-gate 	if (mp->mod_want) {
24457c478bd9Sstevel@tonic-gate 		mp->mod_want = 0;
24467c478bd9Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
24477c478bd9Sstevel@tonic-gate 	}
24487c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
24497c478bd9Sstevel@tonic-gate }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate static struct modctl *
24527c478bd9Sstevel@tonic-gate mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
24537c478bd9Sstevel@tonic-gate {
24547c478bd9Sstevel@tonic-gate 	struct modctl *modp;
24557c478bd9Sstevel@tonic-gate 	int retval;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	/*
24587c478bd9Sstevel@tonic-gate 	 * Hold the module.
24597c478bd9Sstevel@tonic-gate 	 */
24607c478bd9Sstevel@tonic-gate 	modp = mod_hold_by_name_requisite(dep, filename);
24617c478bd9Sstevel@tonic-gate 	if (modp) {
24627c478bd9Sstevel@tonic-gate 		retval = mod_load(modp, 1);
24637c478bd9Sstevel@tonic-gate 		if (retval != 0) {
24647c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
24657c478bd9Sstevel@tonic-gate 			modp = NULL;
24667c478bd9Sstevel@tonic-gate 		}
24677c478bd9Sstevel@tonic-gate 		*status = retval;
24687c478bd9Sstevel@tonic-gate 	} else {
24697c478bd9Sstevel@tonic-gate 		*status = ENOSPC;
24707c478bd9Sstevel@tonic-gate 	}
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	/*
24737c478bd9Sstevel@tonic-gate 	 * if dep is not NULL, clear the module dependency information.
24747c478bd9Sstevel@tonic-gate 	 * This information is set in mod_hold_by_name_common().
24757c478bd9Sstevel@tonic-gate 	 */
24767c478bd9Sstevel@tonic-gate 	if (dep != NULL && dep->mod_requisite_loading != NULL) {
24777c478bd9Sstevel@tonic-gate 		ASSERT(dep->mod_busy);
24787c478bd9Sstevel@tonic-gate 		dep->mod_requisite_loading = NULL;
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	return (modp);
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate /*
24857c478bd9Sstevel@tonic-gate  * hold, load, and install the named module
24867c478bd9Sstevel@tonic-gate  */
24877c478bd9Sstevel@tonic-gate static struct modctl *
24887c478bd9Sstevel@tonic-gate mod_hold_installed_mod(char *name, int usepath, int *r)
24897c478bd9Sstevel@tonic-gate {
24907c478bd9Sstevel@tonic-gate 	struct modctl *modp;
24917c478bd9Sstevel@tonic-gate 	int retval;
24922bac1547Scth 
24932bac1547Scth 	/*
24942bac1547Scth 	 * Verify that that module in question actually exists on disk
24952bac1547Scth 	 * before allocation of module structure by mod_hold_by_name.
24962bac1547Scth 	 */
24972bac1547Scth 	if (modrootloaded && swaploaded) {
24985c311300Scth 		if (!kobj_path_exists(name, usepath)) {
24992bac1547Scth 			*r = ENOENT;
25002bac1547Scth 			return (NULL);
25012bac1547Scth 		}
25022bac1547Scth 	}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	/*
25057c478bd9Sstevel@tonic-gate 	 * Hold the module.
25067c478bd9Sstevel@tonic-gate 	 */
25077c478bd9Sstevel@tonic-gate 	modp = mod_hold_by_name(name);
25087c478bd9Sstevel@tonic-gate 	if (modp) {
25097c478bd9Sstevel@tonic-gate 		retval = mod_load(modp, usepath);
25107c478bd9Sstevel@tonic-gate 		if (retval != 0) {
25117c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
25127c478bd9Sstevel@tonic-gate 			modp = NULL;
25137c478bd9Sstevel@tonic-gate 			*r = retval;
25147c478bd9Sstevel@tonic-gate 		} else {
25157c478bd9Sstevel@tonic-gate 			if ((*r = modinstall(modp)) != 0) {
25167c478bd9Sstevel@tonic-gate 				/*
25177c478bd9Sstevel@tonic-gate 				 * We loaded it, but failed to _init() it.
25187c478bd9Sstevel@tonic-gate 				 * Be kind to developers -- force it
25197c478bd9Sstevel@tonic-gate 				 * out of memory now so that the next
25207c478bd9Sstevel@tonic-gate 				 * attempt to use the module will cause
25217c478bd9Sstevel@tonic-gate 				 * a reload.  See 1093793.
25227c478bd9Sstevel@tonic-gate 				 */
25237c478bd9Sstevel@tonic-gate 				mod_unload(modp);
25247c478bd9Sstevel@tonic-gate 				mod_release_mod(modp);
25257c478bd9Sstevel@tonic-gate 				modp = NULL;
25267c478bd9Sstevel@tonic-gate 			}
25277c478bd9Sstevel@tonic-gate 		}
25287c478bd9Sstevel@tonic-gate 	} else {
25297c478bd9Sstevel@tonic-gate 		*r = ENOSPC;
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate 	return (modp);
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate static char mod_excl_msg[] =
25357c478bd9Sstevel@tonic-gate 	"module %s(%s) is EXCLUDED and will not be loaded\n";
25367c478bd9Sstevel@tonic-gate static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate /*
25397c478bd9Sstevel@tonic-gate  * This routine is needed for dependencies.  Users specify dependencies
25407c478bd9Sstevel@tonic-gate  * by declaring a character array initialized to filenames of dependents.
25417c478bd9Sstevel@tonic-gate  * So the code that handles dependents deals with filenames (and not
25427c478bd9Sstevel@tonic-gate  * module names) because that's all it has.  We load by filename and once
25437c478bd9Sstevel@tonic-gate  * we've loaded a file we can get the module name.
25447c478bd9Sstevel@tonic-gate  * Unfortunately there isn't a single unified filename/modulename namespace.
25457c478bd9Sstevel@tonic-gate  * C'est la vie.
25467c478bd9Sstevel@tonic-gate  *
25477c478bd9Sstevel@tonic-gate  * We allow the name being looked up to be prepended by an optional
25487c478bd9Sstevel@tonic-gate  * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
25497c478bd9Sstevel@tonic-gate  */
25507c478bd9Sstevel@tonic-gate struct modctl *
25517c478bd9Sstevel@tonic-gate mod_find_by_filename(char *subdir, char *filename)
25527c478bd9Sstevel@tonic-gate {
25537c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
25547c478bd9Sstevel@tonic-gate 	size_t		sublen;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
25577c478bd9Sstevel@tonic-gate 	if (subdir != NULL)
25587c478bd9Sstevel@tonic-gate 		sublen = strlen(subdir);
25597c478bd9Sstevel@tonic-gate 	else
25607c478bd9Sstevel@tonic-gate 		sublen = 0;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
25637c478bd9Sstevel@tonic-gate 	mp = &modules;
25647c478bd9Sstevel@tonic-gate 	do {
25657c478bd9Sstevel@tonic-gate 		if (sublen) {
25667c478bd9Sstevel@tonic-gate 			char *mod_filename = mp->mod_filename;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 			if (strncmp(subdir, mod_filename, sublen) == 0 &&
25697c478bd9Sstevel@tonic-gate 			    mod_filename[sublen] == '/' &&
25707c478bd9Sstevel@tonic-gate 			    strcmp(filename, &mod_filename[sublen + 1]) == 0) {
25717c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
25727c478bd9Sstevel@tonic-gate 				return (mp);
25737c478bd9Sstevel@tonic-gate 			}
25747c478bd9Sstevel@tonic-gate 		} else if (strcmp(filename, mp->mod_filename) == 0) {
25757c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
25767c478bd9Sstevel@tonic-gate 			return (mp);
25777c478bd9Sstevel@tonic-gate 		}
25787c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
25797c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
25807c478bd9Sstevel@tonic-gate 	return (NULL);
25817c478bd9Sstevel@tonic-gate }
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate /*
25847c478bd9Sstevel@tonic-gate  * Check for circular dependencies.  This is called from do_dependents()
25857c478bd9Sstevel@tonic-gate  * in kobj.c.  If we are the thread already loading this module, then
25867c478bd9Sstevel@tonic-gate  * we're trying to load a dependent that we're already loading which
25877c478bd9Sstevel@tonic-gate  * means the user specified circular dependencies.
25887c478bd9Sstevel@tonic-gate  */
25897c478bd9Sstevel@tonic-gate static int
25907c478bd9Sstevel@tonic-gate mod_circdep(struct modctl *modp)
25917c478bd9Sstevel@tonic-gate {
25927c478bd9Sstevel@tonic-gate 	struct modctl	*rmod;
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	/*
25977c478bd9Sstevel@tonic-gate 	 * Check the mod_inprogress_thread first.
25987c478bd9Sstevel@tonic-gate 	 * mod_inprogress_thread is used in mod_hold_stub()
25997c478bd9Sstevel@tonic-gate 	 * directly to improve performance.
26007c478bd9Sstevel@tonic-gate 	 */
26017c478bd9Sstevel@tonic-gate 	if (modp->mod_inprogress_thread == curthread)
26027c478bd9Sstevel@tonic-gate 		return (1);
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	/*
26057c478bd9Sstevel@tonic-gate 	 * Check the module circular dependencies.
26067c478bd9Sstevel@tonic-gate 	 */
26077c478bd9Sstevel@tonic-gate 	for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
26087c478bd9Sstevel@tonic-gate 		/*
26097c478bd9Sstevel@tonic-gate 		 * Check if there is a module circular dependency.
26107c478bd9Sstevel@tonic-gate 		 */
26117c478bd9Sstevel@tonic-gate 		if (rmod->mod_requisite_loading == modp)
26127c478bd9Sstevel@tonic-gate 			return (1);
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 	return (0);
26157c478bd9Sstevel@tonic-gate }
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate static int
26187c478bd9Sstevel@tonic-gate mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	int (*func)(struct modinfo *);
26217c478bd9Sstevel@tonic-gate 	int retval;
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	/* primary modules don't do getinfo */
26267c478bd9Sstevel@tonic-gate 	if (modp->mod_prim)
26277c478bd9Sstevel@tonic-gate 		return (0);
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
26327c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_info() not defined properly in %s",
26337c478bd9Sstevel@tonic-gate 		    modp->mod_filename);
26347c478bd9Sstevel@tonic-gate 		/*
26357c478bd9Sstevel@tonic-gate 		 * The semantics of mod_info(9F) are that 0 is failure
26367c478bd9Sstevel@tonic-gate 		 * and non-zero is success.
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		retval = 0;
26397c478bd9Sstevel@tonic-gate 	} else
26407c478bd9Sstevel@tonic-gate 		retval = (*func)(modinfop);	/* call _info() function */
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
26437c478bd9Sstevel@tonic-gate 		printf("Returned from _info, retval = %x\n", retval);
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	return (retval);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate static void
26497c478bd9Sstevel@tonic-gate modadd(struct modctl *mp)
26507c478bd9Sstevel@tonic-gate {
26517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	mp->mod_id = last_module_id++;
26547c478bd9Sstevel@tonic-gate 	mp->mod_next = &modules;
26557c478bd9Sstevel@tonic-gate 	mp->mod_prev = modules.mod_prev;
26567c478bd9Sstevel@tonic-gate 	modules.mod_prev->mod_next = mp;
26577c478bd9Sstevel@tonic-gate 	modules.mod_prev = mp;
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26617c478bd9Sstevel@tonic-gate static struct modctl *
26627aec1d6eScindi allocate_modp(const char *filename, const char *modname)
26637c478bd9Sstevel@tonic-gate {
26647c478bd9Sstevel@tonic-gate 	struct modctl *mp;
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
26677c478bd9Sstevel@tonic-gate 	mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
26687c478bd9Sstevel@tonic-gate 	(void) strcpy(mp->mod_modname, modname);
26697c478bd9Sstevel@tonic-gate 	return (mp);
26707c478bd9Sstevel@tonic-gate }
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate /*
26737c478bd9Sstevel@tonic-gate  * Get the value of a symbol.  This is a wrapper routine that
26747c478bd9Sstevel@tonic-gate  * calls kobj_getsymvalue().  kobj_getsymvalue() may go away but this
26757c478bd9Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
26767c478bd9Sstevel@tonic-gate  */
26777c478bd9Sstevel@tonic-gate uintptr_t
26787c478bd9Sstevel@tonic-gate modgetsymvalue(char *name, int kernelonly)
26797c478bd9Sstevel@tonic-gate {
26807c478bd9Sstevel@tonic-gate 	return (kobj_getsymvalue(name, kernelonly));
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate /*
26847c478bd9Sstevel@tonic-gate  * Get the symbol nearest an address.  This is a wrapper routine that
26857c478bd9Sstevel@tonic-gate  * calls kobj_getsymname().  kobj_getsymname() may go away but this
26867c478bd9Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
26877c478bd9Sstevel@tonic-gate  */
26887c478bd9Sstevel@tonic-gate char *
26897c478bd9Sstevel@tonic-gate modgetsymname(uintptr_t value, ulong_t *offset)
26907c478bd9Sstevel@tonic-gate {
26917c478bd9Sstevel@tonic-gate 	return (kobj_getsymname(value, offset));
26927c478bd9Sstevel@tonic-gate }
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate /*
26957aec1d6eScindi  * Lookup a symbol in a specified module.  These are wrapper routines that
26967aec1d6eScindi  * call kobj_lookup().  kobj_lookup() may go away but these wrappers will
26977aec1d6eScindi  * prevent callers from noticing.
26987c478bd9Sstevel@tonic-gate  */
26997c478bd9Sstevel@tonic-gate uintptr_t
27007aec1d6eScindi modlookup(const char *modname, const char *symname)
27017c478bd9Sstevel@tonic-gate {
27027c478bd9Sstevel@tonic-gate 	struct modctl *modp;
27037c478bd9Sstevel@tonic-gate 	uintptr_t val;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	if ((modp = mod_hold_by_name(modname)) == NULL)
27067c478bd9Sstevel@tonic-gate 		return (0);
27077c478bd9Sstevel@tonic-gate 	val = kobj_lookup(modp->mod_mp, symname);
27087c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
27097c478bd9Sstevel@tonic-gate 	return (val);
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127aec1d6eScindi uintptr_t
27137aec1d6eScindi modlookup_by_modctl(modctl_t *modp, const char *symname)
27147aec1d6eScindi {
27157aec1d6eScindi 	ASSERT(modp->mod_ref > 0 || modp->mod_busy);
27167aec1d6eScindi 
27177aec1d6eScindi 	return (kobj_lookup(modp->mod_mp, symname));
27187aec1d6eScindi }
27197aec1d6eScindi 
27207c478bd9Sstevel@tonic-gate /*
27217c478bd9Sstevel@tonic-gate  * Ask the user for the name of the system file and the default path
27227c478bd9Sstevel@tonic-gate  * for modules.
27237c478bd9Sstevel@tonic-gate  */
27247c478bd9Sstevel@tonic-gate void
27257c478bd9Sstevel@tonic-gate mod_askparams()
27267c478bd9Sstevel@tonic-gate {
27277c478bd9Sstevel@tonic-gate 	static char s0[64];
27287c478bd9Sstevel@tonic-gate 	intptr_t fd;
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 	if ((fd = kobj_open(systemfile)) != -1L)
27317c478bd9Sstevel@tonic-gate 		kobj_close(fd);
27327c478bd9Sstevel@tonic-gate 	else
27337c478bd9Sstevel@tonic-gate 		systemfile = NULL;
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
27367c478bd9Sstevel@tonic-gate 	while (1) {
27377c478bd9Sstevel@tonic-gate 		printf("Name of system file [%s]:  ",
27387c478bd9Sstevel@tonic-gate 			systemfile ? systemfile : "/dev/null");
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 		console_gets(s0, sizeof (s0));
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 		if (s0[0] == '\0')
27437c478bd9Sstevel@tonic-gate 			break;
27447c478bd9Sstevel@tonic-gate 		else if (strcmp(s0, "/dev/null") == 0) {
27457c478bd9Sstevel@tonic-gate 			systemfile = NULL;
27467c478bd9Sstevel@tonic-gate 			break;
27477c478bd9Sstevel@tonic-gate 		} else {
27487c478bd9Sstevel@tonic-gate 			if ((fd = kobj_open(s0)) != -1L) {
27497c478bd9Sstevel@tonic-gate 				kobj_close(fd);
27507c478bd9Sstevel@tonic-gate 				systemfile = s0;
27517c478bd9Sstevel@tonic-gate 				break;
27527c478bd9Sstevel@tonic-gate 			}
27537c478bd9Sstevel@tonic-gate 		}
27547c478bd9Sstevel@tonic-gate 		printf("can't find file %s\n", s0);
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate static char loading_msg[] = "loading '%s' id %d\n";
27597c478bd9Sstevel@tonic-gate static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate /*
27627c478bd9Sstevel@tonic-gate  * Common code for loading a module (but not installing it).
27637c478bd9Sstevel@tonic-gate  * Handoff the task of module loading to a seperate thread
27647c478bd9Sstevel@tonic-gate  * with a large stack if possible, since this code may recurse a few times.
27657c478bd9Sstevel@tonic-gate  * Return zero if there are no errors or an errno value.
27667c478bd9Sstevel@tonic-gate  */
27677c478bd9Sstevel@tonic-gate static int
27687c478bd9Sstevel@tonic-gate mod_load(struct modctl *mp, int usepath)
27697c478bd9Sstevel@tonic-gate {
27707c478bd9Sstevel@tonic-gate 	int		retval;
27717c478bd9Sstevel@tonic-gate 	struct modinfo	*modinfop = NULL;
27727c478bd9Sstevel@tonic-gate 	struct loadmt	lt;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
27757c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	if (mp->mod_loaded)
27787c478bd9Sstevel@tonic-gate 		return (0);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
27817c478bd9Sstevel@tonic-gate 	    mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
27827c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
27837c478bd9Sstevel@tonic-gate 			printf(mod_excl_msg, mp->mod_filename,
27847c478bd9Sstevel@tonic-gate 				mp->mod_modname);
27857c478bd9Sstevel@tonic-gate 		}
27867c478bd9Sstevel@tonic-gate 		return (ENXIO);
27877c478bd9Sstevel@tonic-gate 	}
27887c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
27897c478bd9Sstevel@tonic-gate 		printf(loading_msg, mp->mod_filename, mp->mod_id);
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if (curthread != &t0) {
27927c478bd9Sstevel@tonic-gate 		lt.mp = mp;
27937c478bd9Sstevel@tonic-gate 		lt.usepath = usepath;
27947c478bd9Sstevel@tonic-gate 		lt.owner = curthread;
27957c478bd9Sstevel@tonic-gate 		sema_init(&lt.sema, 0, NULL, SEMA_DEFAULT, NULL);
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		/* create thread to hand of call to */
27987c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
27997c478bd9Sstevel@tonic-gate 		    modload_thread, &lt, 0, &p0, TS_RUN, maxclsyspri);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 		/* wait for thread to complete kobj_load_module */
28027c478bd9Sstevel@tonic-gate 		sema_p(&lt.sema);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 		sema_destroy(&lt.sema);
28057c478bd9Sstevel@tonic-gate 		retval = lt.retval;
28067c478bd9Sstevel@tonic-gate 	} else
28077c478bd9Sstevel@tonic-gate 		retval = kobj_load_module(mp, usepath);
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	if (mp->mod_mp) {
28107c478bd9Sstevel@tonic-gate 		ASSERT(retval == 0);
28117c478bd9Sstevel@tonic-gate 		mp->mod_loaded = 1;
28127c478bd9Sstevel@tonic-gate 		mp->mod_loadcnt++;
28137c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
28147c478bd9Sstevel@tonic-gate 			printf(load_msg, mp->mod_filename, mp->mod_id,
28157c478bd9Sstevel@tonic-gate 				(void *)((struct module *)mp->mod_mp)->text,
28167c478bd9Sstevel@tonic-gate 				(void *)((struct module *)mp->mod_mp)->data,
28177c478bd9Sstevel@tonic-gate 				((struct module *)mp->mod_mp)->text_size,
28187c478bd9Sstevel@tonic-gate 				((struct module *)mp->mod_mp)->data_size);
28197c478bd9Sstevel@tonic-gate 		}
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 		/*
28227c478bd9Sstevel@tonic-gate 		 * XXX - There should be a better way to get this.
28237c478bd9Sstevel@tonic-gate 		 */
28247c478bd9Sstevel@tonic-gate 		modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
28257c478bd9Sstevel@tonic-gate 		modinfop->mi_info = MI_INFO_LINKAGE;
28267c478bd9Sstevel@tonic-gate 		if (mod_getinfo(mp, modinfop) == 0)
28277c478bd9Sstevel@tonic-gate 			mp->mod_linkage = NULL;
28287c478bd9Sstevel@tonic-gate 		else {
28297c478bd9Sstevel@tonic-gate 			mp->mod_linkage = (void *)modinfop->mi_base;
28307c478bd9Sstevel@tonic-gate 			ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
28317c478bd9Sstevel@tonic-gate 		}
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 		/*
28347c478bd9Sstevel@tonic-gate 		 * DCS: bootstrapping code. If the driver is loaded
28357c478bd9Sstevel@tonic-gate 		 * before root mount, it is assumed that the driver
28367c478bd9Sstevel@tonic-gate 		 * may be used before mounting root. In order to
28377c478bd9Sstevel@tonic-gate 		 * access mappings of global to local minor no.'s
28387c478bd9Sstevel@tonic-gate 		 * during installation/open of the driver, we load
28397c478bd9Sstevel@tonic-gate 		 * them into memory here while the BOP_interfaces
28407c478bd9Sstevel@tonic-gate 		 * are still up.
28417c478bd9Sstevel@tonic-gate 		 */
28427c478bd9Sstevel@tonic-gate 		if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
28437c478bd9Sstevel@tonic-gate 			retval = clboot_modload(mp);
28447c478bd9Sstevel@tonic-gate 		}
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 		kmem_free(modinfop, sizeof (struct modinfo));
28477c478bd9Sstevel@tonic-gate 		(void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
28487c478bd9Sstevel@tonic-gate 		retval = install_stubs_by_name(mp, mp->mod_modname);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 		/*
28517c478bd9Sstevel@tonic-gate 		 * Now that the module is loaded, we need to give DTrace
28527c478bd9Sstevel@tonic-gate 		 * a chance to notify its providers.  This is done via
28537c478bd9Sstevel@tonic-gate 		 * the dtrace_modload function pointer.
28547c478bd9Sstevel@tonic-gate 		 */
28557c478bd9Sstevel@tonic-gate 		if (strcmp(mp->mod_modname, "dtrace") != 0) {
28567c478bd9Sstevel@tonic-gate 			struct modctl *dmp = mod_hold_by_name("dtrace");
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 			if (dmp != NULL && dtrace_modload != NULL)
28597c478bd9Sstevel@tonic-gate 				(*dtrace_modload)(mp);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 			mod_release_mod(dmp);
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	} else {
28657c478bd9Sstevel@tonic-gate 		/*
28667c478bd9Sstevel@tonic-gate 		 * If load failed then we need to release any requisites
28677c478bd9Sstevel@tonic-gate 		 * that we had established.
28687c478bd9Sstevel@tonic-gate 		 */
28697c478bd9Sstevel@tonic-gate 		ASSERT(retval);
28707c478bd9Sstevel@tonic-gate 		mod_release_requisites(mp);
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_ERRMSG)
28737c478bd9Sstevel@tonic-gate 			printf("error loading '%s', error %d\n",
28747c478bd9Sstevel@tonic-gate 			    mp->mod_filename, retval);
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 	return (retval);
28777c478bd9Sstevel@tonic-gate }
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate static void
28827c478bd9Sstevel@tonic-gate mod_unload(struct modctl *mp)
28837c478bd9Sstevel@tonic-gate {
28847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
28857c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
28867c478bd9Sstevel@tonic-gate 	ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
28877c478bd9Sstevel@tonic-gate 	    ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
28907c478bd9Sstevel@tonic-gate 		printf(unload_msg, mp->mod_modname,
28917c478bd9Sstevel@tonic-gate 			mp->mod_id, mp->mod_loadcnt);
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	/*
28947c478bd9Sstevel@tonic-gate 	 * If mod_ref is not zero, it means some modules might still refer
28957c478bd9Sstevel@tonic-gate 	 * to this module. Then you can't unload this module right now.
28967c478bd9Sstevel@tonic-gate 	 * Instead, set 1 to mod_delay_unload to notify the system of
28977c478bd9Sstevel@tonic-gate 	 * unloading this module later when it's not required any more.
28987c478bd9Sstevel@tonic-gate 	 */
28997c478bd9Sstevel@tonic-gate 	if (mp->mod_ref > 0) {
29007c478bd9Sstevel@tonic-gate 		mp->mod_delay_unload = 1;
29017c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
29027c478bd9Sstevel@tonic-gate 			printf("module %s not unloaded,"
29037c478bd9Sstevel@tonic-gate 			    " non-zero reference count (%d)",
29047c478bd9Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_ref);
29057c478bd9Sstevel@tonic-gate 		}
29067c478bd9Sstevel@tonic-gate 		return;
29077c478bd9Sstevel@tonic-gate 	}
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (((mp->mod_loaded == 0) || mp->mod_installed) ||
29107c478bd9Sstevel@tonic-gate 	    (mp->mod_ref || mp->mod_prim)) {
29117c478bd9Sstevel@tonic-gate 		/*
29127c478bd9Sstevel@tonic-gate 		 * A DEBUG kernel would ASSERT panic above, the code is broken
29137c478bd9Sstevel@tonic-gate 		 * if we get this warning.
29147c478bd9Sstevel@tonic-gate 		 */
29157c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
29167c478bd9Sstevel@tonic-gate 		    mp->mod_filename, mp->mod_installed, mp->mod_loaded,
29177c478bd9Sstevel@tonic-gate 		    mp->mod_ref);
29187c478bd9Sstevel@tonic-gate 		return;
29197c478bd9Sstevel@tonic-gate 	}
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	/* reset stub functions to call the binder again */
29227c478bd9Sstevel@tonic-gate 	reset_stubs(mp);
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	/*
29257c478bd9Sstevel@tonic-gate 	 * mark module as unloaded before the modctl structure is freed.
29267c478bd9Sstevel@tonic-gate 	 * This is required not to reuse the modctl structure before
29277c478bd9Sstevel@tonic-gate 	 * the module is marked as unloaded.
29287c478bd9Sstevel@tonic-gate 	 */
29297c478bd9Sstevel@tonic-gate 	mp->mod_loaded = 0;
29307c478bd9Sstevel@tonic-gate 	mp->mod_linkage = NULL;
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	/* free the memory */
29337c478bd9Sstevel@tonic-gate 	kobj_unload_module(mp);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (mp->mod_delay_unload) {
29367c478bd9Sstevel@tonic-gate 		mp->mod_delay_unload = 0;
29377c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
29387c478bd9Sstevel@tonic-gate 			printf("deferred unload of module %s"
29397c478bd9Sstevel@tonic-gate 			    " (id %d) successful",
29407c478bd9Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_id);
29417c478bd9Sstevel@tonic-gate 		}
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	/* release hold on requisites */
29457c478bd9Sstevel@tonic-gate 	mod_release_requisites(mp);
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 	/*
29487c478bd9Sstevel@tonic-gate 	 * Now that the module is gone, we need to give DTrace a chance to
29497c478bd9Sstevel@tonic-gate 	 * remove any probes that it may have had in the module.  This is
29507c478bd9Sstevel@tonic-gate 	 * done via the dtrace_modunload function pointer.
29517c478bd9Sstevel@tonic-gate 	 */
29527c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mod_modname, "dtrace") != 0) {
29537c478bd9Sstevel@tonic-gate 		struct modctl *dmp = mod_hold_by_name("dtrace");
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 		if (dmp != NULL && dtrace_modunload != NULL)
29567c478bd9Sstevel@tonic-gate 			(*dtrace_modunload)(mp);
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 		mod_release_mod(dmp);
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate }
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate static int
29637c478bd9Sstevel@tonic-gate modinstall(struct modctl *mp)
29647c478bd9Sstevel@tonic-gate {
29657c478bd9Sstevel@tonic-gate 	int val;
29667c478bd9Sstevel@tonic-gate 	int (*func)(void);
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
29697c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if (mp->mod_installed)
29727c478bd9Sstevel@tonic-gate 		return (0);
29737c478bd9Sstevel@tonic-gate 	/*
29747c478bd9Sstevel@tonic-gate 	 * If mod_delay_unload is on, it means the system chose the deferred
29757c478bd9Sstevel@tonic-gate 	 * unload for this module. Then you can't install this module until
29767c478bd9Sstevel@tonic-gate 	 * it's unloaded from the system.
29777c478bd9Sstevel@tonic-gate 	 */
29787c478bd9Sstevel@tonic-gate 	if (mp->mod_delay_unload)
29797c478bd9Sstevel@tonic-gate 		return (ENXIO);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
29827c478bd9Sstevel@tonic-gate 		printf("installing %s, module id %d.\n",
29837c478bd9Sstevel@tonic-gate 			mp->mod_modname, mp->mod_id);
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_mp != NULL);
29867c478bd9Sstevel@tonic-gate 	if (mod_install_requisites(mp) != 0) {
29877c478bd9Sstevel@tonic-gate 		/*
29887c478bd9Sstevel@tonic-gate 		 * Note that we can't call mod_unload(mp) here since
29897c478bd9Sstevel@tonic-gate 		 * if modinstall() was called by mod_install_requisites(),
29907c478bd9Sstevel@tonic-gate 		 * we won't be able to hold the dependent modules
29917c478bd9Sstevel@tonic-gate 		 * (otherwise there would be a deadlock).
29927c478bd9Sstevel@tonic-gate 		 */
29937c478bd9Sstevel@tonic-gate 		return (ENXIO);
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_ERRMSG) {
29977c478bd9Sstevel@tonic-gate 		printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
29987c478bd9Sstevel@tonic-gate 			mp->mod_filename, mp->mod_id,
29997c478bd9Sstevel@tonic-gate 			(void *)((struct module *)mp->mod_mp)->text,
30007c478bd9Sstevel@tonic-gate 			(void *)((struct module *)mp->mod_mp)->data,
30017c478bd9Sstevel@tonic-gate 			((struct module *)mp->mod_mp)->text_size,
30027c478bd9Sstevel@tonic-gate 			((struct module *)mp->mod_mp)->data_size);
30037c478bd9Sstevel@tonic-gate 	}
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
30087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_init() not defined properly in %s",
30097c478bd9Sstevel@tonic-gate 		    mp->mod_filename);
30107c478bd9Sstevel@tonic-gate 		return (EFAULT);
30117c478bd9Sstevel@tonic-gate 	}
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG) {
30147c478bd9Sstevel@tonic-gate 		printf("breakpoint before calling %s:_init()\n",
30157c478bd9Sstevel@tonic-gate 		    mp->mod_modname);
30167c478bd9Sstevel@tonic-gate 		if (DEBUGGER_PRESENT)
30177c478bd9Sstevel@tonic-gate 			debug_enter("_init");
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
30217c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
30227c478bd9Sstevel@tonic-gate 	val = (*func)();		/* call _init */
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
30257c478bd9Sstevel@tonic-gate 		printf("Returned from _init, val = %x\n", val);
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	if (val == 0) {
30287c478bd9Sstevel@tonic-gate 		/*
30297c478bd9Sstevel@tonic-gate 		 * Set the MODS_INSTALLED flag to enable this module
30307c478bd9Sstevel@tonic-gate 		 * being called now.
30317c478bd9Sstevel@tonic-gate 		 */
30327c478bd9Sstevel@tonic-gate 		install_stubs(mp);
30337c478bd9Sstevel@tonic-gate 		mp->mod_installed = 1;
30347c478bd9Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG)
30357c478bd9Sstevel@tonic-gate 		printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	return (val);
30387c478bd9Sstevel@tonic-gate }
30397c478bd9Sstevel@tonic-gate 
3040a7aa4df7Scth int	detach_driver_unconfig = 0;
3041a7aa4df7Scth 
30427c478bd9Sstevel@tonic-gate static int
30437c478bd9Sstevel@tonic-gate detach_driver(char *name)
30447c478bd9Sstevel@tonic-gate {
30457c478bd9Sstevel@tonic-gate 	major_t major;
30467c478bd9Sstevel@tonic-gate 	int error;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	/*
30497c478bd9Sstevel@tonic-gate 	 * If being called from mod_uninstall_all() then the appropriate
30507c478bd9Sstevel@tonic-gate 	 * driver detaches (leaf only) have already been done.
30517c478bd9Sstevel@tonic-gate 	 */
30527c478bd9Sstevel@tonic-gate 	if (mod_in_autounload())
30537c478bd9Sstevel@tonic-gate 		return (0);
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(name);
30567c478bd9Sstevel@tonic-gate 	if (major == (major_t)-1)
30577c478bd9Sstevel@tonic-gate 		return (0);
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 	error = ndi_devi_unconfig_driver(ddi_root_node(),
3060a7aa4df7Scth 	    NDI_DETACH_DRIVER | detach_driver_unconfig, major);
30617c478bd9Sstevel@tonic-gate 	return (error == NDI_SUCCESS ? 0 : -1);
30627c478bd9Sstevel@tonic-gate }
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate static int
30677c478bd9Sstevel@tonic-gate moduninstall(struct modctl *mp)
30687c478bd9Sstevel@tonic-gate {
30697c478bd9Sstevel@tonic-gate 	int status = 0;
30707c478bd9Sstevel@tonic-gate 	int (*func)(void);
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
30737c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	/*
30767c478bd9Sstevel@tonic-gate 	 * Verify that we need to do something and can uninstall the module.
30777c478bd9Sstevel@tonic-gate 	 *
30787c478bd9Sstevel@tonic-gate 	 * If we should not uninstall the module or if the module is not in
30797c478bd9Sstevel@tonic-gate 	 * the correct state to start an uninstall we return EBUSY to prevent
30807c478bd9Sstevel@tonic-gate 	 * us from progressing to mod_unload.  If the module has already been
30817c478bd9Sstevel@tonic-gate 	 * uninstalled and unloaded we return EALREADY.
30827c478bd9Sstevel@tonic-gate 	 */
30837c478bd9Sstevel@tonic-gate 	if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
30847c478bd9Sstevel@tonic-gate 		return (EBUSY);
30857c478bd9Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
30867c478bd9Sstevel@tonic-gate 		return (EALREADY);
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 	/*
30897c478bd9Sstevel@tonic-gate 	 * To avoid devinfo / module deadlock we must release this module
30907c478bd9Sstevel@tonic-gate 	 * prior to initiating the detach_driver, otherwise the detach_driver
30917c478bd9Sstevel@tonic-gate 	 * might deadlock on a devinfo node held by another thread
30927c478bd9Sstevel@tonic-gate 	 * coming top down and involving the module we have locked.
30937c478bd9Sstevel@tonic-gate 	 *
30947c478bd9Sstevel@tonic-gate 	 * When we regrab the module we must reverify that it is OK
30957c478bd9Sstevel@tonic-gate 	 * to proceed with the uninstall operation.
30967c478bd9Sstevel@tonic-gate 	 */
30977c478bd9Sstevel@tonic-gate 	mod_release_mod(mp);
30987c478bd9Sstevel@tonic-gate 	status = detach_driver(mp->mod_modname);
30997c478bd9Sstevel@tonic-gate 	(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/* check detach status and reverify state with lock */
31027c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
31037c478bd9Sstevel@tonic-gate 	if ((status != 0) || mp->mod_prim || mp->mod_ref) {
31047c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
31057c478bd9Sstevel@tonic-gate 		return (EBUSY);
31067c478bd9Sstevel@tonic-gate 	}
31077c478bd9Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
31087c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
31097c478bd9Sstevel@tonic-gate 		return (EALREADY);
31107c478bd9Sstevel@tonic-gate 	}
31117c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
31147c478bd9Sstevel@tonic-gate 		printf("uninstalling %s\n", mp->mod_modname);
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	/*
31177c478bd9Sstevel@tonic-gate 	 * lookup _fini, return EBUSY if not defined.
31187c478bd9Sstevel@tonic-gate 	 *
31197c478bd9Sstevel@tonic-gate 	 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
31207c478bd9Sstevel@tonic-gate 	 * detach(9E) - it allows bufctl addresses to be resolved.
31217c478bd9Sstevel@tonic-gate 	 */
31227c478bd9Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
31237c478bd9Sstevel@tonic-gate 	if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
31247c478bd9Sstevel@tonic-gate 	    (moddebug & MODDEBUG_FINI_EBUSY))
31257c478bd9Sstevel@tonic-gate 		return (EBUSY);
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	/* verify that _fini is in this module */
31287c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
31297c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_fini() not defined properly in %s",
31307c478bd9Sstevel@tonic-gate 		    mp->mod_filename);
31317c478bd9Sstevel@tonic-gate 		return (EFAULT);
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	/* call _fini() */
31357c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
31367c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	status = (*func)();
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	if (status == 0) {
31417c478bd9Sstevel@tonic-gate 		/* _fini returned success, the module is no longer installed */
31427c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG)
31437c478bd9Sstevel@tonic-gate 			printf("uninstalled %s\n", mp->mod_modname);
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 		/*
31467c478bd9Sstevel@tonic-gate 		 * Even though we only set mod_installed to zero here, a zero
31477c478bd9Sstevel@tonic-gate 		 * return value means we are commited to a code path were
31487c478bd9Sstevel@tonic-gate 		 * mod_loaded will also end up as zero - we have no other
31497c478bd9Sstevel@tonic-gate 		 * way to get the module data and bss back to the pre _init
31507c478bd9Sstevel@tonic-gate 		 * state except a reload. To ensure this, after return,
31517c478bd9Sstevel@tonic-gate 		 * mod_busy must stay set until mod_loaded is cleared.
31527c478bd9Sstevel@tonic-gate 		 */
31537c478bd9Sstevel@tonic-gate 		mp->mod_installed = 0;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 		/*
31567c478bd9Sstevel@tonic-gate 		 * Clear the MODS_INSTALLED flag not to call functions
31577c478bd9Sstevel@tonic-gate 		 * in the module directly from now on.
31587c478bd9Sstevel@tonic-gate 		 */
31597c478bd9Sstevel@tonic-gate 		uninstall_stubs(mp);
31607c478bd9Sstevel@tonic-gate 	} else {
31617c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_USERDEBUG)
31627c478bd9Sstevel@tonic-gate 			printf(finiret_msg, mp->mod_filename, status);
31637c478bd9Sstevel@tonic-gate 		/*
31647c478bd9Sstevel@tonic-gate 		 * By definition _fini is only allowed to return EBUSY or the
31657c478bd9Sstevel@tonic-gate 		 * result of mod_remove (EBUSY or EINVAL).  In the off chance
31667c478bd9Sstevel@tonic-gate 		 * that a driver returns EALREADY we convert this to EINVAL
31677c478bd9Sstevel@tonic-gate 		 * since to our caller EALREADY means module was already
31687c478bd9Sstevel@tonic-gate 		 * removed.
31697c478bd9Sstevel@tonic-gate 		 */
31707c478bd9Sstevel@tonic-gate 		if (status == EALREADY)
31717c478bd9Sstevel@tonic-gate 			status = EINVAL;
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	return (status);
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate /*
31787c478bd9Sstevel@tonic-gate  * Uninstall all modules.
31797c478bd9Sstevel@tonic-gate  */
31807c478bd9Sstevel@tonic-gate static void
31817c478bd9Sstevel@tonic-gate mod_uninstall_all(void)
31827c478bd9Sstevel@tonic-gate {
31837c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
31847c478bd9Sstevel@tonic-gate 	modid_t		modid = 0;
31857c478bd9Sstevel@tonic-gate 
3186a7aa4df7Scth 	/* synchronize with any active modunload_disable() */
3187a7aa4df7Scth 	modunload_begin();
3188a7aa4df7Scth 
31897c478bd9Sstevel@tonic-gate 	/* mark this thread as doing autounloading */
31907c478bd9Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, (void *)1);
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	(void) devfs_clean(ddi_root_node(), NULL, 0);
31937c478bd9Sstevel@tonic-gate 	(void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	while ((mp = mod_hold_next_by_id(modid)) != NULL) {
31967c478bd9Sstevel@tonic-gate 		modid = mp->mod_id;
31977c478bd9Sstevel@tonic-gate 		/*
31987c478bd9Sstevel@tonic-gate 		 * Skip modules with the MOD_NOAUTOUNLOAD flag set
31997c478bd9Sstevel@tonic-gate 		 */
32007c478bd9Sstevel@tonic-gate 		if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
32017c478bd9Sstevel@tonic-gate 			mod_release_mod(mp);
32027c478bd9Sstevel@tonic-gate 			continue;
32037c478bd9Sstevel@tonic-gate 		}
32047c478bd9Sstevel@tonic-gate 
32050b38a8bdSahl 		if (moduninstall(mp) == 0) {
32067c478bd9Sstevel@tonic-gate 			mod_unload(mp);
32070b38a8bdSahl 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
32080b38a8bdSahl 		}
32097c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
32107c478bd9Sstevel@tonic-gate 	}
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, NULL);
3213a7aa4df7Scth 	modunload_end();
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
3216a7aa4df7Scth /* wait for unloads that have begun before registering disable */
32177c478bd9Sstevel@tonic-gate void
32187c478bd9Sstevel@tonic-gate modunload_disable(void)
32197c478bd9Sstevel@tonic-gate {
3220a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3221a7aa4df7Scth 	while (modunload_active_count) {
3222a7aa4df7Scth 		modunload_wait++;
3223a7aa4df7Scth 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3224a7aa4df7Scth 		modunload_wait--;
3225a7aa4df7Scth 	}
3226a7aa4df7Scth 	modunload_disable_count++;
3227a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
32287c478bd9Sstevel@tonic-gate }
32297c478bd9Sstevel@tonic-gate 
3230a7aa4df7Scth /* mark end of disable and signal waiters */
32317c478bd9Sstevel@tonic-gate void
32327c478bd9Sstevel@tonic-gate modunload_enable(void)
32337c478bd9Sstevel@tonic-gate {
3234a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3235a7aa4df7Scth 	modunload_disable_count--;
3236a7aa4df7Scth 	if ((modunload_disable_count == 0) && modunload_wait)
3237a7aa4df7Scth 		cv_broadcast(&modunload_wait_cv);
3238a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
3239a7aa4df7Scth }
3240a7aa4df7Scth 
3241a7aa4df7Scth /* wait for disables to complete before begining unload */
3242a7aa4df7Scth void
3243a7aa4df7Scth modunload_begin()
3244a7aa4df7Scth {
3245a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3246a7aa4df7Scth 	while (modunload_disable_count) {
3247a7aa4df7Scth 		modunload_wait++;
3248a7aa4df7Scth 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3249a7aa4df7Scth 		modunload_wait--;
3250a7aa4df7Scth 	}
3251a7aa4df7Scth 	modunload_active_count++;
3252a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
3253a7aa4df7Scth }
3254a7aa4df7Scth 
3255a7aa4df7Scth /* mark end of unload and signal waiters */
3256a7aa4df7Scth void
3257a7aa4df7Scth modunload_end()
3258a7aa4df7Scth {
3259a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3260a7aa4df7Scth 	modunload_active_count--;
3261a7aa4df7Scth 	if ((modunload_active_count == 0) && modunload_wait)
3262a7aa4df7Scth 		cv_broadcast(&modunload_wait_cv);
3263a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
32647c478bd9Sstevel@tonic-gate }
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate void
32677c478bd9Sstevel@tonic-gate mod_uninstall_daemon(void)
32687c478bd9Sstevel@tonic-gate {
32697c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
32707c478bd9Sstevel@tonic-gate 	clock_t		ticks = 0;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	mod_aul_thread = curthread;
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
32757c478bd9Sstevel@tonic-gate 	for (;;) {
32767c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_uninstall_lock);
32777c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
32787c478bd9Sstevel@tonic-gate 		/*
32797c478bd9Sstevel@tonic-gate 		 * In DEBUG kernels, unheld drivers are uninstalled periodically
32807c478bd9Sstevel@tonic-gate 		 * every mod_uninstall_interval seconds.  Periodic uninstall can
32817c478bd9Sstevel@tonic-gate 		 * be disabled by setting mod_uninstall_interval to 0 which is
32827c478bd9Sstevel@tonic-gate 		 * the default for a non-DEBUG kernel.
32837c478bd9Sstevel@tonic-gate 		 */
32847c478bd9Sstevel@tonic-gate 		if (mod_uninstall_interval) {
32857c478bd9Sstevel@tonic-gate 			ticks = ddi_get_lbolt() +
32867c478bd9Sstevel@tonic-gate 				drv_usectohz(mod_uninstall_interval * 1000000);
32877c478bd9Sstevel@tonic-gate 			(void) cv_timedwait(&mod_uninstall_cv,
32887c478bd9Sstevel@tonic-gate 				&mod_uninstall_lock, ticks);
32897c478bd9Sstevel@tonic-gate 		} else {
32907c478bd9Sstevel@tonic-gate 			cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
32917c478bd9Sstevel@tonic-gate 		}
32927c478bd9Sstevel@tonic-gate 		/*
32937c478bd9Sstevel@tonic-gate 		 * The whole daemon is safe for CPR except we don't want
32947c478bd9Sstevel@tonic-gate 		 * the daemon to run if FREEZE is issued and this daemon
32957c478bd9Sstevel@tonic-gate 		 * wakes up from the cv_wait above. In this case, it'll be
32967c478bd9Sstevel@tonic-gate 		 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
32977c478bd9Sstevel@tonic-gate 		 *
32987c478bd9Sstevel@tonic-gate 		 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
32997c478bd9Sstevel@tonic-gate 		 * mod_uninstall_lock is used to protect cprinfo and
33007c478bd9Sstevel@tonic-gate 		 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
33017c478bd9Sstevel@tonic-gate 		 * called.
33027c478bd9Sstevel@tonic-gate 		 */
33037c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
33047c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
33057c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_uninstall_lock);
33067c478bd9Sstevel@tonic-gate 		if ((modunload_disable_count == 0) &&
33077c478bd9Sstevel@tonic-gate 		    ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
33087c478bd9Sstevel@tonic-gate 			mod_uninstall_all();
33097c478bd9Sstevel@tonic-gate 		}
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate }
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate /*
33147c478bd9Sstevel@tonic-gate  * Unload all uninstalled modules.
33157c478bd9Sstevel@tonic-gate  */
33167c478bd9Sstevel@tonic-gate void
33177c478bd9Sstevel@tonic-gate modreap(void)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_uninstall_lock);
33207c478bd9Sstevel@tonic-gate 	cv_broadcast(&mod_uninstall_cv);
33217c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_uninstall_lock);
33227c478bd9Sstevel@tonic-gate }
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate /*
33257c478bd9Sstevel@tonic-gate  * Hold the specified module. This is the module holding primitive.
33267c478bd9Sstevel@tonic-gate  *
33277c478bd9Sstevel@tonic-gate  * If MOD_LOCK_HELD then the caller already holds the mod_lock.
33287c478bd9Sstevel@tonic-gate  *
33297c478bd9Sstevel@tonic-gate  * Return values:
33307c478bd9Sstevel@tonic-gate  *	 0 ==> the module is held
33317c478bd9Sstevel@tonic-gate  *	 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
33327c478bd9Sstevel@tonic-gate  *		to determine how to retry.
33337c478bd9Sstevel@tonic-gate  */
33347c478bd9Sstevel@tonic-gate int
33357c478bd9Sstevel@tonic-gate mod_hold_by_modctl(struct modctl *mp, int f)
33367c478bd9Sstevel@tonic-gate {
33377c478bd9Sstevel@tonic-gate 	ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
33387c478bd9Sstevel@tonic-gate 	    ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
33397c478bd9Sstevel@tonic-gate 	    (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
33407c478bd9Sstevel@tonic-gate 	ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
33417c478bd9Sstevel@tonic-gate 	    ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
33427c478bd9Sstevel@tonic-gate 	    (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
33437c478bd9Sstevel@tonic-gate 	ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
33467c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	while (mp->mod_busy) {
33497c478bd9Sstevel@tonic-gate 		mp->mod_want = 1;
33507c478bd9Sstevel@tonic-gate 		cv_wait(&mod_cv, &mod_lock);
33517c478bd9Sstevel@tonic-gate 		/*
33527c478bd9Sstevel@tonic-gate 		 * Module may be unloaded by daemon.
33537c478bd9Sstevel@tonic-gate 		 * Nevertheless, modctl structure is still in linked list
33547c478bd9Sstevel@tonic-gate 		 * (i.e., off &modules), not freed!
33557c478bd9Sstevel@tonic-gate 		 * Caller is not supposed to assume "mp" is valid, but there
33567c478bd9Sstevel@tonic-gate 		 * is no reasonable way to detect this but using
33577c478bd9Sstevel@tonic-gate 		 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
33587c478bd9Sstevel@tonic-gate 		 *   (or similar check depending on calling context)
33597c478bd9Sstevel@tonic-gate 		 * DON'T free modctl structure, it will be very very
33607c478bd9Sstevel@tonic-gate 		 * problematic.
33617c478bd9Sstevel@tonic-gate 		 */
33627c478bd9Sstevel@tonic-gate 		if (f & MOD_WAIT_ONCE) {
33637c478bd9Sstevel@tonic-gate 			if (f & MOD_LOCK_NOT_HELD)
33647c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
33657c478bd9Sstevel@tonic-gate 			return (1);	/* caller decides how to retry */
33667c478bd9Sstevel@tonic-gate 		}
33677c478bd9Sstevel@tonic-gate 	}
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	mp->mod_busy = 1;
33707c478bd9Sstevel@tonic-gate 	mp->mod_inprogress_thread =
33717c478bd9Sstevel@tonic-gate 	    (curthread == NULL ? (kthread_id_t)-1 : curthread);
33727c478bd9Sstevel@tonic-gate 
33737c478bd9Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
33747c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
33757c478bd9Sstevel@tonic-gate 	return (0);
33767c478bd9Sstevel@tonic-gate }
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate static struct modctl *
33797aec1d6eScindi mod_hold_by_name_common(struct modctl *dep, const char *filename)
33807c478bd9Sstevel@tonic-gate {
33817aec1d6eScindi 	const char	*modname;
33827c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
33837c478bd9Sstevel@tonic-gate 	char		*curname, *newname;
33847c478bd9Sstevel@tonic-gate 	int		found = 0;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
33897c478bd9Sstevel@tonic-gate 		modname = filename;
33907c478bd9Sstevel@tonic-gate 	else
33917c478bd9Sstevel@tonic-gate 		modname++;
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 	mp = &modules;
33947c478bd9Sstevel@tonic-gate 	do {
33957c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
33967c478bd9Sstevel@tonic-gate 			found = 1;
33977c478bd9Sstevel@tonic-gate 			break;
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	if (found == 0) {
34027c478bd9Sstevel@tonic-gate 		mp = allocate_modp(filename, modname);
34037c478bd9Sstevel@tonic-gate 		modadd(mp);
34047c478bd9Sstevel@tonic-gate 	}
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate 	/*
34077c478bd9Sstevel@tonic-gate 	 * if dep is not NULL, set the mp in mod_requisite_loading for
34087c478bd9Sstevel@tonic-gate 	 * the module circular dependency check. This field is used in
34097c478bd9Sstevel@tonic-gate 	 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
34107c478bd9Sstevel@tonic-gate 	 */
34117c478bd9Sstevel@tonic-gate 	if (dep != NULL) {
34127c478bd9Sstevel@tonic-gate 		ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
34137c478bd9Sstevel@tonic-gate 		dep->mod_requisite_loading = mp;
34147c478bd9Sstevel@tonic-gate 	}
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	/*
34177c478bd9Sstevel@tonic-gate 	 * If the module was held, then it must be us who has it held.
34187c478bd9Sstevel@tonic-gate 	 */
34197c478bd9Sstevel@tonic-gate 	if (mod_circdep(mp))
34207c478bd9Sstevel@tonic-gate 		mp = NULL;
34217c478bd9Sstevel@tonic-gate 	else {
34227c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 		/*
34257c478bd9Sstevel@tonic-gate 		 * If the name hadn't been set or has changed, allocate
34267c478bd9Sstevel@tonic-gate 		 * space and set it.  Free space used by previous name.
34277c478bd9Sstevel@tonic-gate 		 *
34287c478bd9Sstevel@tonic-gate 		 * Do not change the name of primary modules, for primary
34297c478bd9Sstevel@tonic-gate 		 * modules the mod_filename was allocated in standalone mode:
34307c478bd9Sstevel@tonic-gate 		 * it is illegal to kobj_alloc in standalone mode and kobj_free
34317c478bd9Sstevel@tonic-gate 		 * in non-standalone mode.
34327c478bd9Sstevel@tonic-gate 		 */
34337c478bd9Sstevel@tonic-gate 		curname = mp->mod_filename;
34347c478bd9Sstevel@tonic-gate 		if (curname == NULL ||
34357c478bd9Sstevel@tonic-gate 		    ((mp->mod_prim == 0) &&
34367c478bd9Sstevel@tonic-gate 		    (curname != filename) &&
34377c478bd9Sstevel@tonic-gate 		    (modname != filename) &&
34387c478bd9Sstevel@tonic-gate 		    (strcmp(curname, filename) != 0))) {
34397c478bd9Sstevel@tonic-gate 			newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
34407c478bd9Sstevel@tonic-gate 			(void) strcpy(newname, filename);
34417c478bd9Sstevel@tonic-gate 			mp->mod_filename = newname;
34427c478bd9Sstevel@tonic-gate 			if (curname != NULL)
34437c478bd9Sstevel@tonic-gate 				kobj_free(curname, strlen(curname) + 1);
34447c478bd9Sstevel@tonic-gate 		}
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
34487c478bd9Sstevel@tonic-gate 	if (mp && moddebug & MODDEBUG_LOADMSG2)
34497c478bd9Sstevel@tonic-gate 		printf("Holding %s\n", mp->mod_filename);
34507c478bd9Sstevel@tonic-gate 	if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
34517c478bd9Sstevel@tonic-gate 		printf("circular dependency loading %s\n", filename);
34527c478bd9Sstevel@tonic-gate 	return (mp);
34537c478bd9Sstevel@tonic-gate }
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate static struct modctl *
34567c478bd9Sstevel@tonic-gate mod_hold_by_name_requisite(struct modctl *dep, char *filename)
34577c478bd9Sstevel@tonic-gate {
34587c478bd9Sstevel@tonic-gate 	return (mod_hold_by_name_common(dep, filename));
34597c478bd9Sstevel@tonic-gate }
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate struct modctl *
34627aec1d6eScindi mod_hold_by_name(const char *filename)
34637c478bd9Sstevel@tonic-gate {
34647c478bd9Sstevel@tonic-gate 	return (mod_hold_by_name_common(NULL, filename));
34657c478bd9Sstevel@tonic-gate }
34667c478bd9Sstevel@tonic-gate 
34677aec1d6eScindi struct modctl *
34687c478bd9Sstevel@tonic-gate mod_hold_by_id(modid_t modid)
34697c478bd9Sstevel@tonic-gate {
34707c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
34717c478bd9Sstevel@tonic-gate 	int		found = 0;
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
34747c478bd9Sstevel@tonic-gate 	mp = &modules;
34757c478bd9Sstevel@tonic-gate 	do {
34767c478bd9Sstevel@tonic-gate 		if (mp->mod_id == modid) {
34777c478bd9Sstevel@tonic-gate 			found = 1;
34787c478bd9Sstevel@tonic-gate 			break;
34797c478bd9Sstevel@tonic-gate 		}
34807c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
34837c478bd9Sstevel@tonic-gate 		mp = NULL;
34847c478bd9Sstevel@tonic-gate 	else
34857c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
34887c478bd9Sstevel@tonic-gate 	return (mp);
34897c478bd9Sstevel@tonic-gate }
34907c478bd9Sstevel@tonic-gate 
34917c478bd9Sstevel@tonic-gate static struct modctl *
34927c478bd9Sstevel@tonic-gate mod_hold_next_by_id(modid_t modid)
34937c478bd9Sstevel@tonic-gate {
34947c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
34957c478bd9Sstevel@tonic-gate 	int		found = 0;
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	if (modid < -1)
34987c478bd9Sstevel@tonic-gate 		return (NULL);
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	mp = &modules;
35037c478bd9Sstevel@tonic-gate 	do {
35047c478bd9Sstevel@tonic-gate 		if (mp->mod_id > modid) {
35057c478bd9Sstevel@tonic-gate 			found = 1;
35067c478bd9Sstevel@tonic-gate 			break;
35077c478bd9Sstevel@tonic-gate 		}
35087c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
35117c478bd9Sstevel@tonic-gate 		mp = NULL;
35127c478bd9Sstevel@tonic-gate 	else
35137c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
35167c478bd9Sstevel@tonic-gate 	return (mp);
35177c478bd9Sstevel@tonic-gate }
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate static void
35207c478bd9Sstevel@tonic-gate mod_release(struct modctl *mp)
35217c478bd9Sstevel@tonic-gate {
35227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
35237c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	mp->mod_busy = 0;
35267c478bd9Sstevel@tonic-gate 	mp->mod_inprogress_thread = NULL;
35277c478bd9Sstevel@tonic-gate 	if (mp->mod_want) {
35287c478bd9Sstevel@tonic-gate 		mp->mod_want = 0;
35297c478bd9Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
35307c478bd9Sstevel@tonic-gate 	}
35317c478bd9Sstevel@tonic-gate }
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate void
35347c478bd9Sstevel@tonic-gate mod_release_mod(struct modctl *mp)
35357c478bd9Sstevel@tonic-gate {
35367c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
35377c478bd9Sstevel@tonic-gate 		printf("Releasing %s\n", mp->mod_filename);
35387c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
35397c478bd9Sstevel@tonic-gate 	mod_release(mp);
35407c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate modid_t
35447c478bd9Sstevel@tonic-gate mod_name_to_modid(char *filename)
35457c478bd9Sstevel@tonic-gate {
35467c478bd9Sstevel@tonic-gate 	char		*modname;
35477c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
35527c478bd9Sstevel@tonic-gate 		modname = filename;
35537c478bd9Sstevel@tonic-gate 	else
35547c478bd9Sstevel@tonic-gate 		modname++;
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	mp = &modules;
35577c478bd9Sstevel@tonic-gate 	do {
35587c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
35597c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
35607c478bd9Sstevel@tonic-gate 			return (mp->mod_id);
35617c478bd9Sstevel@tonic-gate 		}
35627c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
35657c478bd9Sstevel@tonic-gate 	return (-1);
35667c478bd9Sstevel@tonic-gate }
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate int
35707c478bd9Sstevel@tonic-gate mod_remove_by_name(char *name)
35717c478bd9Sstevel@tonic-gate {
35727c478bd9Sstevel@tonic-gate 	struct modctl *mp;
35737c478bd9Sstevel@tonic-gate 	int retval;
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 	mp = mod_hold_by_name(name);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	if (mp == NULL)
35787c478bd9Sstevel@tonic-gate 		return (EINVAL);
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
35817c478bd9Sstevel@tonic-gate 		/*
35827c478bd9Sstevel@tonic-gate 		 * Do not unload forceloaded modules
35837c478bd9Sstevel@tonic-gate 		 */
35847c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
35857c478bd9Sstevel@tonic-gate 		return (0);
35867c478bd9Sstevel@tonic-gate 	}
35877c478bd9Sstevel@tonic-gate 
35880b38a8bdSahl 	if ((retval = moduninstall(mp)) == 0) {
35897c478bd9Sstevel@tonic-gate 		mod_unload(mp);
35900b38a8bdSahl 		CPU_STATS_ADDQ(CPU, sys, modunload, 1);
35910b38a8bdSahl 	} else if (retval == EALREADY)
35927c478bd9Sstevel@tonic-gate 		retval = 0;		/* already unloaded, not an error */
35937c478bd9Sstevel@tonic-gate 	mod_release_mod(mp);
35947c478bd9Sstevel@tonic-gate 	return (retval);
35957c478bd9Sstevel@tonic-gate }
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate /*
35987c478bd9Sstevel@tonic-gate  * Record that module "dep" is dependent on module "on_mod."
35997c478bd9Sstevel@tonic-gate  */
36007c478bd9Sstevel@tonic-gate static void
36017c478bd9Sstevel@tonic-gate mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
36027c478bd9Sstevel@tonic-gate {
36037c478bd9Sstevel@tonic-gate 	struct modctl_list **pmlnp;	/* previous next pointer */
36047c478bd9Sstevel@tonic-gate 	struct modctl_list *mlp;
36057c478bd9Sstevel@tonic-gate 	struct modctl_list *new;
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate 	ASSERT(dependent->mod_busy && on_mod->mod_busy);
36087c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	/*
36117c478bd9Sstevel@tonic-gate 	 * Search dependent's requisite list to see if on_mod is recorded.
36127c478bd9Sstevel@tonic-gate 	 * List is ordered by id.
36137c478bd9Sstevel@tonic-gate 	 */
36147c478bd9Sstevel@tonic-gate 	for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
36157c478bd9Sstevel@tonic-gate 	    mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
36167c478bd9Sstevel@tonic-gate 		if (mlp->modl_modp->mod_id >= on_mod->mod_id)
36177c478bd9Sstevel@tonic-gate 			break;
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	/* Create and insert if not already recorded */
36207c478bd9Sstevel@tonic-gate 	if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
36217c478bd9Sstevel@tonic-gate 		new = kobj_zalloc(sizeof (*new), KM_SLEEP);
36227c478bd9Sstevel@tonic-gate 		new->modl_modp = on_mod;
36237c478bd9Sstevel@tonic-gate 		new->modl_next = mlp;
36247c478bd9Sstevel@tonic-gate 		*pmlnp = new;
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 		/*
36277c478bd9Sstevel@tonic-gate 		 * Increment the mod_ref count in our new requisite module.
36287c478bd9Sstevel@tonic-gate 		 * This is what keeps a module that has other modules
36297c478bd9Sstevel@tonic-gate 		 * which are dependent on it from being uninstalled and
36307c478bd9Sstevel@tonic-gate 		 * unloaded. "on_mod"'s mod_ref count decremented in
36317c478bd9Sstevel@tonic-gate 		 * mod_release_requisites when the "dependent" module
36327c478bd9Sstevel@tonic-gate 		 * unload is complete.  "on_mod" must be loaded, but may not
36337c478bd9Sstevel@tonic-gate 		 * yet be installed.
36347c478bd9Sstevel@tonic-gate 		 */
36357c478bd9Sstevel@tonic-gate 		on_mod->mod_ref++;
36367c478bd9Sstevel@tonic-gate 		ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
36377c478bd9Sstevel@tonic-gate 	}
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
36407c478bd9Sstevel@tonic-gate }
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate /*
36437c478bd9Sstevel@tonic-gate  * release the hold associated with mod_make_requisite mod_ref++
36447c478bd9Sstevel@tonic-gate  * as part of unload.
36457c478bd9Sstevel@tonic-gate  */
36467c478bd9Sstevel@tonic-gate void
36477c478bd9Sstevel@tonic-gate mod_release_requisites(struct modctl *modp)
36487c478bd9Sstevel@tonic-gate {
36497c478bd9Sstevel@tonic-gate 	struct modctl_list *modl;
36507c478bd9Sstevel@tonic-gate 	struct modctl_list *next;
36517c478bd9Sstevel@tonic-gate 	struct modctl *req;
36527c478bd9Sstevel@tonic-gate 	struct modctl_list *start = NULL, *mod_garbage;
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
36557c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);		/* needed for manipulation of req */
36587c478bd9Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = next) {
36597c478bd9Sstevel@tonic-gate 		next = modl->modl_next;
36607c478bd9Sstevel@tonic-gate 		req = modl->modl_modp;
36617c478bd9Sstevel@tonic-gate 		ASSERT(req->mod_ref >= 1 && req->mod_loaded);
36627c478bd9Sstevel@tonic-gate 		req->mod_ref--;
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 		/*
36657c478bd9Sstevel@tonic-gate 		 * Check if the module has to be unloaded or not.
36667c478bd9Sstevel@tonic-gate 		 */
36677c478bd9Sstevel@tonic-gate 		if (req->mod_ref == 0 && req->mod_delay_unload) {
36687c478bd9Sstevel@tonic-gate 			struct modctl_list *new;
36697c478bd9Sstevel@tonic-gate 			/*
36707c478bd9Sstevel@tonic-gate 			 * Allocate the modclt_list holding the garbage
36717c478bd9Sstevel@tonic-gate 			 * module which should be unloaded later.
36727c478bd9Sstevel@tonic-gate 			 */
36737c478bd9Sstevel@tonic-gate 			new = kobj_zalloc(sizeof (struct modctl_list),
36747c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
36757c478bd9Sstevel@tonic-gate 			new->modl_modp = req;
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 			if (start == NULL)
36787c478bd9Sstevel@tonic-gate 				mod_garbage = start = new;
36797c478bd9Sstevel@tonic-gate 			else {
36807c478bd9Sstevel@tonic-gate 				mod_garbage->modl_next = new;
36817c478bd9Sstevel@tonic-gate 				mod_garbage = new;
36827c478bd9Sstevel@tonic-gate 			}
36837c478bd9Sstevel@tonic-gate 		}
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 		/* free the list as we go */
36867c478bd9Sstevel@tonic-gate 		kobj_free(modl, sizeof (*modl));
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate 	modp->mod_requisites = NULL;
36897c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate 	/*
36927c478bd9Sstevel@tonic-gate 	 * Unload the garbage modules.
36937c478bd9Sstevel@tonic-gate 	 */
36947c478bd9Sstevel@tonic-gate 	for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
36957c478bd9Sstevel@tonic-gate 		struct modctl_list *old = mod_garbage;
36967c478bd9Sstevel@tonic-gate 		struct modctl *mp = mod_garbage->modl_modp;
36977c478bd9Sstevel@tonic-gate 		ASSERT(mp != NULL);
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 		/*
37007c478bd9Sstevel@tonic-gate 		 * Hold this module until it's unloaded completely.
37017c478bd9Sstevel@tonic-gate 		 */
37027c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp,
37037c478bd9Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
37047c478bd9Sstevel@tonic-gate 		/*
37057c478bd9Sstevel@tonic-gate 		 * Check if the module is not unloaded yet and nobody requires
37067c478bd9Sstevel@tonic-gate 		 * the module. If it's unloaded already or somebody still
37077c478bd9Sstevel@tonic-gate 		 * requires the module, don't unload it now.
37087c478bd9Sstevel@tonic-gate 		 */
37097c478bd9Sstevel@tonic-gate 		if (mp->mod_loaded && mp->mod_ref == 0)
37107c478bd9Sstevel@tonic-gate 			mod_unload(mp);
37117c478bd9Sstevel@tonic-gate 		ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
37127c478bd9Sstevel@tonic-gate 		    (mp->mod_ref > 0));
37137c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 		mod_garbage = mod_garbage->modl_next;
37167c478bd9Sstevel@tonic-gate 		kobj_free(old, sizeof (struct modctl_list));
37177c478bd9Sstevel@tonic-gate 	}
37187c478bd9Sstevel@tonic-gate }
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate /*
37217c478bd9Sstevel@tonic-gate  * Process dependency of the module represented by "dep" on the
37227c478bd9Sstevel@tonic-gate  * module named by "on."
37237c478bd9Sstevel@tonic-gate  *
37247c478bd9Sstevel@tonic-gate  * Called from kobj_do_dependents() to load a module "on" on which
37257c478bd9Sstevel@tonic-gate  * "dep" depends.
37267c478bd9Sstevel@tonic-gate  */
37277c478bd9Sstevel@tonic-gate struct modctl *
37287c478bd9Sstevel@tonic-gate mod_load_requisite(struct modctl *dep, char *on)
37297c478bd9Sstevel@tonic-gate {
37307c478bd9Sstevel@tonic-gate 	struct modctl *on_mod;
37317c478bd9Sstevel@tonic-gate 	int retval;
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
37347c478bd9Sstevel@tonic-gate 		mod_make_requisite(dep, on_mod);
37357c478bd9Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG) {
37367c478bd9Sstevel@tonic-gate 		printf("error processing %s on which module %s depends\n",
37377c478bd9Sstevel@tonic-gate 			on, dep->mod_modname);
37387c478bd9Sstevel@tonic-gate 	}
37397c478bd9Sstevel@tonic-gate 	return (on_mod);
37407c478bd9Sstevel@tonic-gate }
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate static int
37437c478bd9Sstevel@tonic-gate mod_install_requisites(struct modctl *modp)
37447c478bd9Sstevel@tonic-gate {
37457c478bd9Sstevel@tonic-gate 	struct modctl_list *modl;
37467c478bd9Sstevel@tonic-gate 	struct modctl *req;
37477c478bd9Sstevel@tonic-gate 	int status = 0;
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
37507c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
37537c478bd9Sstevel@tonic-gate 		req = modl->modl_modp;
37547c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(req,
37557c478bd9Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
37567c478bd9Sstevel@tonic-gate 		status = modinstall(req);
37577c478bd9Sstevel@tonic-gate 		mod_release_mod(req);
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 		if (status != 0)
37607c478bd9Sstevel@tonic-gate 			break;
37617c478bd9Sstevel@tonic-gate 	}
37627c478bd9Sstevel@tonic-gate 	return (status);
37637c478bd9Sstevel@tonic-gate }
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate /*
37667c478bd9Sstevel@tonic-gate  * returns 1 if this thread is doing autounload, 0 otherwise.
37677c478bd9Sstevel@tonic-gate  * see mod_uninstall_all.
37687c478bd9Sstevel@tonic-gate  */
37697c478bd9Sstevel@tonic-gate int
37707c478bd9Sstevel@tonic-gate mod_in_autounload()
37717c478bd9Sstevel@tonic-gate {
37727c478bd9Sstevel@tonic-gate 	return ((int)(uintptr_t)tsd_get(mod_autounload_key));
37737c478bd9Sstevel@tonic-gate }
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate /*
37767c478bd9Sstevel@tonic-gate  * gmatch adapted from libc, stripping the wchar stuff
37777c478bd9Sstevel@tonic-gate  */
37787c478bd9Sstevel@tonic-gate #define	popchar(p, c) \
37797c478bd9Sstevel@tonic-gate 	c = *p++; \
37807c478bd9Sstevel@tonic-gate 	if (c == 0) \
37817c478bd9Sstevel@tonic-gate 		return (0);
37827c478bd9Sstevel@tonic-gate 
3783*facf4a8dSllai1 int
37847c478bd9Sstevel@tonic-gate gmatch(const char *s, const char *p)
37857c478bd9Sstevel@tonic-gate {
37867c478bd9Sstevel@tonic-gate 	int c, sc;
37877c478bd9Sstevel@tonic-gate 	int ok, lc, notflag;
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 	sc = *s++;
37907c478bd9Sstevel@tonic-gate 	c = *p++;
37917c478bd9Sstevel@tonic-gate 	if (c == 0)
37927c478bd9Sstevel@tonic-gate 		return (sc == c);	/* nothing matches nothing */
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate 	switch (c) {
37957c478bd9Sstevel@tonic-gate 	case '\\':
37967c478bd9Sstevel@tonic-gate 		/* skip to quoted character */
37977c478bd9Sstevel@tonic-gate 		popchar(p, c)
37987c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	default:
38017c478bd9Sstevel@tonic-gate 		/* straight comparison */
38027c478bd9Sstevel@tonic-gate 		if (c != sc)
38037c478bd9Sstevel@tonic-gate 			return (0);
38047c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	case '?':
38077c478bd9Sstevel@tonic-gate 		/* first char matches, move to remainder */
38087c478bd9Sstevel@tonic-gate 		return (sc != '\0' ? gmatch(s, p) : 0);
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 	case '*':
38127c478bd9Sstevel@tonic-gate 		while (*p == '*')
38137c478bd9Sstevel@tonic-gate 			p++;
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 		/* * matches everything */
38167c478bd9Sstevel@tonic-gate 		if (*p == 0)
38177c478bd9Sstevel@tonic-gate 			return (1);
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate 		/* undo skip at the beginning & iterate over substrings */
38207c478bd9Sstevel@tonic-gate 		--s;
38217c478bd9Sstevel@tonic-gate 		while (*s) {
38227c478bd9Sstevel@tonic-gate 			if (gmatch(s, p))
38237c478bd9Sstevel@tonic-gate 				return (1);
38247c478bd9Sstevel@tonic-gate 			s++;
38257c478bd9Sstevel@tonic-gate 		}
38267c478bd9Sstevel@tonic-gate 		return (0);
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	case '[':
38297c478bd9Sstevel@tonic-gate 		/* match any char within [] */
38307c478bd9Sstevel@tonic-gate 		if (sc == 0)
38317c478bd9Sstevel@tonic-gate 			return (0);
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 		ok = lc = notflag = 0;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 		if (*p == '!') {
38367c478bd9Sstevel@tonic-gate 			notflag = 1;
38377c478bd9Sstevel@tonic-gate 			p++;
38387c478bd9Sstevel@tonic-gate 		}
38397c478bd9Sstevel@tonic-gate 		popchar(p, c)
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 		do {
38427c478bd9Sstevel@tonic-gate 			if (c == '-' && lc && *p != ']') {
38437c478bd9Sstevel@tonic-gate 				/* test sc against range [c1-c2] */
38447c478bd9Sstevel@tonic-gate 				popchar(p, c)
38457c478bd9Sstevel@tonic-gate 				if (c == '\\') {
38467c478bd9Sstevel@tonic-gate 					popchar(p, c)
38477c478bd9Sstevel@tonic-gate 				}
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 				if (notflag) {
38507c478bd9Sstevel@tonic-gate 					/* return 0 on mismatch */
38517c478bd9Sstevel@tonic-gate 					if (lc <= sc && sc <= c)
38527c478bd9Sstevel@tonic-gate 						return (0);
38537c478bd9Sstevel@tonic-gate 					ok++;
38547c478bd9Sstevel@tonic-gate 				} else if (lc <= sc && sc <= c) {
38557c478bd9Sstevel@tonic-gate 					ok++;
38567c478bd9Sstevel@tonic-gate 				}
38577c478bd9Sstevel@tonic-gate 				/* keep going, may get a match next */
38587c478bd9Sstevel@tonic-gate 			} else if (c == '\\') {
38597c478bd9Sstevel@tonic-gate 				/* skip to quoted character */
38607c478bd9Sstevel@tonic-gate 				popchar(p, c)
38617c478bd9Sstevel@tonic-gate 			}
38627c478bd9Sstevel@tonic-gate 			lc = c;
38637c478bd9Sstevel@tonic-gate 			if (notflag) {
38647c478bd9Sstevel@tonic-gate 				if (sc == lc)
38657c478bd9Sstevel@tonic-gate 					return (0);
38667c478bd9Sstevel@tonic-gate 				ok++;
38677c478bd9Sstevel@tonic-gate 			} else if (sc == lc) {
38687c478bd9Sstevel@tonic-gate 				ok++;
38697c478bd9Sstevel@tonic-gate 			}
38707c478bd9Sstevel@tonic-gate 			popchar(p, c)
38717c478bd9Sstevel@tonic-gate 		} while (c != ']');
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 		/* recurse on remainder of string */
38747c478bd9Sstevel@tonic-gate 		return (ok ? gmatch(s, p) : 0);
38757c478bd9Sstevel@tonic-gate 	}
38767c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
38777c478bd9Sstevel@tonic-gate }
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate /*
38817c478bd9Sstevel@tonic-gate  * Get default perm for device from /etc/minor_perm. Return 0 if match found.
38827c478bd9Sstevel@tonic-gate  *
38837c478bd9Sstevel@tonic-gate  * Pure wild-carded patterns are handled separately so the ordering of
38847c478bd9Sstevel@tonic-gate  * these patterns doesn't matter.  We're still dependent on ordering
38857c478bd9Sstevel@tonic-gate  * however as the first matching entry is the one returned.
38867c478bd9Sstevel@tonic-gate  * Not ideal but all existing examples and usage do imply this
38877c478bd9Sstevel@tonic-gate  * ordering implicitly.
38887c478bd9Sstevel@tonic-gate  *
38897c478bd9Sstevel@tonic-gate  * Drivers using the clone driver are always good for some entertainment.
38907c478bd9Sstevel@tonic-gate  * Clone nodes under pseudo have the form clone@0:<driver>.  Some minor
38917c478bd9Sstevel@tonic-gate  * perm entries have the form clone:<driver>, others use <driver>:*
38927c478bd9Sstevel@tonic-gate  * Examples are clone:llc1 vs. llc2:*, for example.
38937c478bd9Sstevel@tonic-gate  *
38947c478bd9Sstevel@tonic-gate  * Minor perms in the clone:<driver> form are mapped to the drivers's
38957c478bd9Sstevel@tonic-gate  * mperm list, not the clone driver, as wildcard entries for clone
38967c478bd9Sstevel@tonic-gate  * reference only.  In other words, a clone wildcard will match
38977c478bd9Sstevel@tonic-gate  * references for clone@0:<driver> but never <driver>@<minor>.
38987c478bd9Sstevel@tonic-gate  *
38997c478bd9Sstevel@tonic-gate  * Additional minor perms in the standard form are also supported,
39007c478bd9Sstevel@tonic-gate  * for mixed usage, ie a node with an entry clone:<driver> could
39017c478bd9Sstevel@tonic-gate  * provide further entries <driver>:<minor>.
39027c478bd9Sstevel@tonic-gate  *
39037c478bd9Sstevel@tonic-gate  * Finally, some uses of clone use an alias as the minor name rather
39047c478bd9Sstevel@tonic-gate  * than the driver name, with the alias as the minor perm entry.
39057c478bd9Sstevel@tonic-gate  * This case is handled by attaching the driver to bring its
39067c478bd9Sstevel@tonic-gate  * minor list into existence, then discover the alias via DDI_ALIAS.
39077c478bd9Sstevel@tonic-gate  * The clone device's minor perm list can then be searched for
39087c478bd9Sstevel@tonic-gate  * that alias.
39097c478bd9Sstevel@tonic-gate  */
39107c478bd9Sstevel@tonic-gate 
39117c478bd9Sstevel@tonic-gate static int
39127c478bd9Sstevel@tonic-gate dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
39137c478bd9Sstevel@tonic-gate {
39147c478bd9Sstevel@tonic-gate 	major_t major;
39157c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
39167c478bd9Sstevel@tonic-gate 	mperm_t *mp;
39177c478bd9Sstevel@tonic-gate 	char *alias = NULL;
39187c478bd9Sstevel@tonic-gate 	dev_info_t *cdevi;
39197c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmd;
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(minor_name);
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 	ASSERT(dip == clone_dip);
39247c478bd9Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	/*
39277c478bd9Sstevel@tonic-gate 	 * Attach the driver named by the minor node, then
39287c478bd9Sstevel@tonic-gate 	 * search its first instance's minor list for an
39297c478bd9Sstevel@tonic-gate 	 * alias node.
39307c478bd9Sstevel@tonic-gate 	 */
39317c478bd9Sstevel@tonic-gate 	if (ddi_hold_installed_driver(major) == NULL)
39327c478bd9Sstevel@tonic-gate 		return (1);
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
39357c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	if ((cdevi = dnp->dn_head) != NULL) {
39387c478bd9Sstevel@tonic-gate 		mutex_enter(&DEVI(cdevi)->devi_lock);
39397c478bd9Sstevel@tonic-gate 		for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
39407c478bd9Sstevel@tonic-gate 			if (dmd->type == DDM_ALIAS) {
39417c478bd9Sstevel@tonic-gate 				alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
39427c478bd9Sstevel@tonic-gate 				break;
39437c478bd9Sstevel@tonic-gate 			}
39447c478bd9Sstevel@tonic-gate 		}
39457c478bd9Sstevel@tonic-gate 		mutex_exit(&DEVI(cdevi)->devi_lock);
39467c478bd9Sstevel@tonic-gate 	}
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
39497c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate 	if (alias == NULL) {
39527c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
39537c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "dev_minorperm: "
39547c478bd9Sstevel@tonic-gate 			    "no alias for %s\n", minor_name);
39557c478bd9Sstevel@tonic-gate 		return (1);
39567c478bd9Sstevel@tonic-gate 	}
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(clone_dip);
39597c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
39607c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 	/*
39637c478bd9Sstevel@tonic-gate 	 * Go through the clone driver's mperm list looking
39647c478bd9Sstevel@tonic-gate 	 * for a match for the specified alias.
39657c478bd9Sstevel@tonic-gate 	 */
39667c478bd9Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
39677c478bd9Sstevel@tonic-gate 		if (strcmp(alias, mp->mp_minorname) == 0) {
39687c478bd9Sstevel@tonic-gate 			break;
39697c478bd9Sstevel@tonic-gate 		}
39707c478bd9Sstevel@tonic-gate 	}
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	if (mp) {
39737c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
39747c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
39757c478bd9Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
39767c478bd9Sstevel@tonic-gate 			    minor_name, alias, mp->mp_mode,
39777c478bd9Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
39787c478bd9Sstevel@tonic-gate 		}
39797c478bd9Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
39807c478bd9Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
39817c478bd9Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	kmem_free(alias, strlen(alias)+1);
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	return (mp == NULL);
39887c478bd9Sstevel@tonic-gate }
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate int
39917c478bd9Sstevel@tonic-gate dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
39927c478bd9Sstevel@tonic-gate {
39937c478bd9Sstevel@tonic-gate 	major_t major;
39947c478bd9Sstevel@tonic-gate 	char *minor_name;
39957c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
39967c478bd9Sstevel@tonic-gate 	mperm_t *mp;
39977c478bd9Sstevel@tonic-gate 	int is_clone = 0;
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 	if (!minorperm_loaded) {
40007c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
40017c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
40027c478bd9Sstevel@tonic-gate 			    "%s: minor perm not yet loaded\n", name);
40037c478bd9Sstevel@tonic-gate 		return (1);
40047c478bd9Sstevel@tonic-gate 	}
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	minor_name = strchr(name, ':');
40077c478bd9Sstevel@tonic-gate 	if (minor_name == NULL)
40087c478bd9Sstevel@tonic-gate 		return (1);
40097c478bd9Sstevel@tonic-gate 	minor_name++;
40107c478bd9Sstevel@tonic-gate 
40117c478bd9Sstevel@tonic-gate 	/*
40127c478bd9Sstevel@tonic-gate 	 * If it's the clone driver, search the driver as named
40137c478bd9Sstevel@tonic-gate 	 * by the minor.  All clone minor perm entries other than
40147c478bd9Sstevel@tonic-gate 	 * alias nodes are actually installed on the real driver's list.
40157c478bd9Sstevel@tonic-gate 	 */
40167c478bd9Sstevel@tonic-gate 	if (dip == clone_dip) {
40177c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(minor_name);
40187c478bd9Sstevel@tonic-gate 		if (major == (major_t)-1) {
40197c478bd9Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM)
40207c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "dev_minorperm: "
40217c478bd9Sstevel@tonic-gate 				    "%s: no such driver\n", minor_name);
40227c478bd9Sstevel@tonic-gate 			return (1);
40237c478bd9Sstevel@tonic-gate 		}
40247c478bd9Sstevel@tonic-gate 		is_clone = 1;
40257c478bd9Sstevel@tonic-gate 	} else {
40267c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
40277c478bd9Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
40287c478bd9Sstevel@tonic-gate 	}
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
40317c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	/*
40347c478bd9Sstevel@tonic-gate 	 * Go through the driver's mperm list looking for
40357c478bd9Sstevel@tonic-gate 	 * a match for the specified minor.  If there's
40367c478bd9Sstevel@tonic-gate 	 * no matching pattern, use the wild card.
40377c478bd9Sstevel@tonic-gate 	 * Defer to the clone wild for clone if specified,
40387c478bd9Sstevel@tonic-gate 	 * otherwise fall back to the normal form.
40397c478bd9Sstevel@tonic-gate 	 */
40407c478bd9Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
40417c478bd9Sstevel@tonic-gate 		if (gmatch(minor_name, mp->mp_minorname) != 0) {
40427c478bd9Sstevel@tonic-gate 			break;
40437c478bd9Sstevel@tonic-gate 		}
40447c478bd9Sstevel@tonic-gate 	}
40457c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
40467c478bd9Sstevel@tonic-gate 		if (is_clone)
40477c478bd9Sstevel@tonic-gate 			mp = dnp->dn_mperm_clone;
40487c478bd9Sstevel@tonic-gate 		if (mp == NULL)
40497c478bd9Sstevel@tonic-gate 			mp = dnp->dn_mperm_wild;
40507c478bd9Sstevel@tonic-gate 	}
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 	if (mp) {
40537c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
40547c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
40557c478bd9Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d\n",
40567c478bd9Sstevel@tonic-gate 			    name, mp->mp_minorname, mp->mp_mode,
40577c478bd9Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
40587c478bd9Sstevel@tonic-gate 		}
40597c478bd9Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
40607c478bd9Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
40617c478bd9Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
40627c478bd9Sstevel@tonic-gate 	}
40637c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	/*
40667c478bd9Sstevel@tonic-gate 	 * If no match can be found for a clone node,
40677c478bd9Sstevel@tonic-gate 	 * search for a possible match for an alias.
40687c478bd9Sstevel@tonic-gate 	 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
40697c478bd9Sstevel@tonic-gate 	 * with minor perm entry clone:ptmx.
40707c478bd9Sstevel@tonic-gate 	 */
40717c478bd9Sstevel@tonic-gate 	if (mp == NULL && is_clone) {
40727c478bd9Sstevel@tonic-gate 		return (dev_alias_minorperm(dip, minor_name, rmp));
40737c478bd9Sstevel@tonic-gate 	}
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 	return (mp == NULL);
40767c478bd9Sstevel@tonic-gate }
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate /*
40797c478bd9Sstevel@tonic-gate  * dynamicaly reference load a dl module/library, returning handle
40807c478bd9Sstevel@tonic-gate  */
40817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40827c478bd9Sstevel@tonic-gate ddi_modhandle_t
40837c478bd9Sstevel@tonic-gate ddi_modopen(const char *modname, int mode, int *errnop)
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	char		*subdir;
40867c478bd9Sstevel@tonic-gate 	char		*mod;
40877c478bd9Sstevel@tonic-gate 	int		subdirlen;
40887c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = NULL;
40897c478bd9Sstevel@tonic-gate 	int		retval = EINVAL;
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate 	ASSERT(modname && (mode == KRTLD_MODE_FIRST));
40927c478bd9Sstevel@tonic-gate 	if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
40937c478bd9Sstevel@tonic-gate 		goto out;
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	/* find optional first '/' in modname */
40967c478bd9Sstevel@tonic-gate 	mod = strchr(modname, '/');
40977c478bd9Sstevel@tonic-gate 	if (mod != strrchr(modname, '/'))
40987c478bd9Sstevel@tonic-gate 		goto out;		/* only one '/' is legal */
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	if (mod) {
41017c478bd9Sstevel@tonic-gate 		/* for subdir string without modification to argument */
41027c478bd9Sstevel@tonic-gate 		mod++;
41037c478bd9Sstevel@tonic-gate 		subdirlen = mod - modname;
41047c478bd9Sstevel@tonic-gate 		subdir = kmem_alloc(subdirlen, KM_SLEEP);
41057c478bd9Sstevel@tonic-gate 		(void) strlcpy(subdir, modname, subdirlen);
41067c478bd9Sstevel@tonic-gate 	} else {
41077c478bd9Sstevel@tonic-gate 		subdirlen = 0;
41087c478bd9Sstevel@tonic-gate 		subdir = "misc";
41097c478bd9Sstevel@tonic-gate 		mod = (char *)modname;
41107c478bd9Sstevel@tonic-gate 	}
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 	/* reference load with errno return value */
41137c478bd9Sstevel@tonic-gate 	retval = modrload(subdir, mod, &hmodp);
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate 	if (subdirlen)
41167c478bd9Sstevel@tonic-gate 		kmem_free(subdir, subdirlen);
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate out:	if (errnop)
41197c478bd9Sstevel@tonic-gate 		*errnop = retval;
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
41227c478bd9Sstevel@tonic-gate 		printf("ddi_modopen %s mode %x: %s %p %d\n",
41237c478bd9Sstevel@tonic-gate 		    modname ? modname : "<unknown>", mode,
41247c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_filename : "<unknown>",
41257c478bd9Sstevel@tonic-gate 		    (void *)hmodp, retval);
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 	return ((ddi_modhandle_t)hmodp);
41287c478bd9Sstevel@tonic-gate }
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate /* lookup "name" in open dl module/library */
41317c478bd9Sstevel@tonic-gate void *
41327c478bd9Sstevel@tonic-gate ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
41337c478bd9Sstevel@tonic-gate {
41347c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
41357c478bd9Sstevel@tonic-gate 	void		*f;
41367c478bd9Sstevel@tonic-gate 	int		retval;
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate 	ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
41397c478bd9Sstevel@tonic-gate 	if ((hmodp == NULL) || (name == NULL) ||
41407c478bd9Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
41417c478bd9Sstevel@tonic-gate 		f = NULL;
41427c478bd9Sstevel@tonic-gate 		retval = EINVAL;
41437c478bd9Sstevel@tonic-gate 	} else {
41447c478bd9Sstevel@tonic-gate 		f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
41457c478bd9Sstevel@tonic-gate 		if (f)
41467c478bd9Sstevel@tonic-gate 			retval = 0;
41477c478bd9Sstevel@tonic-gate 		else
41487c478bd9Sstevel@tonic-gate 			retval = ENOTSUP;
41497c478bd9Sstevel@tonic-gate 	}
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
41527c478bd9Sstevel@tonic-gate 		printf("ddi_modsym in %s of %s: %d %p\n",
41537c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>",
41547c478bd9Sstevel@tonic-gate 		    name ? name : "<unknown>", retval, f);
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 	if (errnop)
41577c478bd9Sstevel@tonic-gate 		*errnop = retval;
41587c478bd9Sstevel@tonic-gate 	return (f);
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /* dynamic (un)reference unload of an open dl module/library */
41627c478bd9Sstevel@tonic-gate int
41637c478bd9Sstevel@tonic-gate ddi_modclose(ddi_modhandle_t h)
41647c478bd9Sstevel@tonic-gate {
41657c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
41667c478bd9Sstevel@tonic-gate 	struct modctl	*modp = NULL;
41677c478bd9Sstevel@tonic-gate 	int		retval;
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 	ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
41707c478bd9Sstevel@tonic-gate 	if ((hmodp == NULL) ||
41717c478bd9Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
41727c478bd9Sstevel@tonic-gate 		retval = EINVAL;
41737c478bd9Sstevel@tonic-gate 		goto out;
41747c478bd9Sstevel@tonic-gate 	}
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate 	retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
41777c478bd9Sstevel@tonic-gate 	if (retval == EBUSY)
41787c478bd9Sstevel@tonic-gate 		retval = 0;	/* EBUSY is not an error */
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 	if (retval == 0) {
41817c478bd9Sstevel@tonic-gate 		ASSERT(hmodp == modp);
41827c478bd9Sstevel@tonic-gate 		if (hmodp != modp)
41837c478bd9Sstevel@tonic-gate 			retval = EINVAL;
41847c478bd9Sstevel@tonic-gate 	}
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate out:	if (moddebug & MODDEBUG_DDI_MOD)
41877c478bd9Sstevel@tonic-gate 		printf("ddi_modclose %s: %d\n",
41887c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>", retval);
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate 	return (retval);
41917c478bd9Sstevel@tonic-gate }
4192