xref: /onnv-gate/usr/src/uts/common/os/modctl.c (revision 169)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * modctl system call for loadable module support.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/exec.h>
370Sstevel@tonic-gate #include <sys/file.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/conf.h>
400Sstevel@tonic-gate #include <sys/time.h>
410Sstevel@tonic-gate #include <sys/reboot.h>
420Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/sysconf.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/ddi.h>
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <sys/sunndi.h>
490Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
500Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
510Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
520Sstevel@tonic-gate #include <sys/bootconf.h>
530Sstevel@tonic-gate #include <sys/dc_ki.h>
540Sstevel@tonic-gate #include <sys/cladm.h>
550Sstevel@tonic-gate #include <sys/dtrace.h>
560Sstevel@tonic-gate #include <sys/kdi.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/devpolicy.h>
590Sstevel@tonic-gate #include <sys/modctl.h>
600Sstevel@tonic-gate #include <sys/kobj.h>
610Sstevel@tonic-gate #include <sys/devops.h>
620Sstevel@tonic-gate #include <sys/autoconf.h>
630Sstevel@tonic-gate #include <sys/hwconf.h>
640Sstevel@tonic-gate #include <sys/callb.h>
650Sstevel@tonic-gate #include <sys/debug.h>
660Sstevel@tonic-gate #include <sys/cpuvar.h>
670Sstevel@tonic-gate #include <sys/sysmacros.h>
680Sstevel@tonic-gate #include <sys/sysevent.h>
690Sstevel@tonic-gate #include <sys/sysevent_impl.h>
700Sstevel@tonic-gate #include <sys/instance.h>
710Sstevel@tonic-gate #include <sys/modhash.h>
720Sstevel@tonic-gate #include <sys/modhash_impl.h>
730Sstevel@tonic-gate #include <sys/dacf_impl.h>
740Sstevel@tonic-gate #include <sys/vfs.h>
750Sstevel@tonic-gate #include <sys/pathname.h>
760Sstevel@tonic-gate #include <sys/console.h>
770Sstevel@tonic-gate #include <sys/policy.h>
780Sstevel@tonic-gate #include <ipp/ipp_impl.h>
790Sstevel@tonic-gate #include <sys/fs/dv_node.h>
800Sstevel@tonic-gate #include <sys/strsubr.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int		mod_circdep(struct modctl *);
830Sstevel@tonic-gate static int		modinfo(modid_t, struct modinfo *);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static void		mod_uninstall_all(void);
860Sstevel@tonic-gate static int		mod_getinfo(struct modctl *, struct modinfo *);
870Sstevel@tonic-gate static struct modctl	*allocate_modp(char *, char *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int		mod_load(struct modctl *, int);
900Sstevel@tonic-gate static void		mod_unload(struct modctl *);
910Sstevel@tonic-gate static int		modinstall(struct modctl *);
920Sstevel@tonic-gate static int		moduninstall(struct modctl *);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static struct modctl	*mod_hold_by_name_common(struct modctl *, char *);
950Sstevel@tonic-gate static struct modctl	*mod_hold_by_id(modid_t);
960Sstevel@tonic-gate static struct modctl	*mod_hold_next_by_id(modid_t);
970Sstevel@tonic-gate static struct modctl	*mod_hold_loaded_mod(struct modctl *, char *, int *);
980Sstevel@tonic-gate static struct modctl	*mod_hold_installed_mod(char *, int, int *);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static void		mod_release(struct modctl *);
1010Sstevel@tonic-gate static void		mod_make_requisite(struct modctl *, struct modctl *);
1020Sstevel@tonic-gate static int		mod_install_requisites(struct modctl *);
1030Sstevel@tonic-gate static void		check_esc_sequences(char *, char *);
1040Sstevel@tonic-gate static struct modctl	*mod_hold_by_name_requisite(struct modctl *, char *);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * module loading thread control structure. Calls to kobj_load_module()() are
1080Sstevel@tonic-gate  * handled off to a separate thead using this structure.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate struct loadmt {
1110Sstevel@tonic-gate 	ksema_t		sema;
1120Sstevel@tonic-gate 	struct modctl	*mp;
1130Sstevel@tonic-gate 	int		usepath;
1140Sstevel@tonic-gate 	kthread_t	*owner;
1150Sstevel@tonic-gate 	int		retval;
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static void	modload_thread(struct loadmt *);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate kcondvar_t	mod_cv;
1210Sstevel@tonic-gate kcondvar_t	mod_uninstall_cv;	/* Communication between swapper */
1220Sstevel@tonic-gate 					/* and the uninstall daemon. */
1230Sstevel@tonic-gate kmutex_t	mod_lock;		/* protects &modules insert linkage, */
1240Sstevel@tonic-gate 					/* mod_busy, mod_want, and mod_ref. */
1250Sstevel@tonic-gate 					/* blocking operations while holding */
1260Sstevel@tonic-gate 					/* mod_lock should be avoided */
1270Sstevel@tonic-gate kmutex_t	mod_uninstall_lock;	/* protects mod_uninstall_cv */
1280Sstevel@tonic-gate kthread_id_t	mod_aul_thread;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate int	isminiroot;		/* set if running as miniroot */
1310Sstevel@tonic-gate int	modrootloaded;		/* set after root driver and fs are loaded */
1320Sstevel@tonic-gate int	moddebug = 0x0;		/* debug flags for module writers */
1330Sstevel@tonic-gate int	swaploaded;		/* set after swap driver and fs are loaded */
1340Sstevel@tonic-gate int	bop_io_quiesced = 0;	/* set when BOP I/O can no longer be used */
1350Sstevel@tonic-gate int	last_module_id;
1360Sstevel@tonic-gate clock_t	mod_uninstall_interval = 0;
1370Sstevel@tonic-gate int	ddi_modclose_unload = 1;	/* 0 -> just decrement reference */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate struct devnames *devnamesp;
1400Sstevel@tonic-gate struct devnames orphanlist;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate krwlock_t	devinfo_tree_lock;	/* obsolete, to be removed */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	MAJBINDFILE "/etc/name_to_major"
1450Sstevel@tonic-gate #define	SYSBINDFILE "/etc/name_to_sysnum"
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static char	majbind[] = MAJBINDFILE;
1480Sstevel@tonic-gate static char	sysbind[] = SYSBINDFILE;
1490Sstevel@tonic-gate static uint_t	mod_autounload_key;	/* for module autounload detection */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate extern int obpdebug;
1520Sstevel@tonic-gate extern int make_mbind(char *, int, char *, struct bind **);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #define	DEBUGGER_PRESENT	((boothowto & RB_DEBUG) || (obpdebug != 0))
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static int minorperm_loaded = 0;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate void
1610Sstevel@tonic-gate mod_setup(void)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	struct sysent *callp;
1640Sstevel@tonic-gate 	int callnum, exectype;
1650Sstevel@tonic-gate 	int	num_devs;
1660Sstevel@tonic-gate 	int	i;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * Initialize the list of loaded driver dev_ops.
1700Sstevel@tonic-gate 	 * XXX - This must be done before reading the system file so that
1710Sstevel@tonic-gate 	 * forceloads of drivers will work.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * Since read_binding_file is common code, it doesn't enforce that all
1760Sstevel@tonic-gate 	 * of the binding file entries have major numbers <= MAXMAJ32.  Thus,
1770Sstevel@tonic-gate 	 * ensure that we don't allocate some massive amount of space due to a
1780Sstevel@tonic-gate 	 * bad entry.  We can't have major numbers bigger than MAXMAJ32
1790Sstevel@tonic-gate 	 * until file system support for larger major numbers exists.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * Leave space for expansion, but not more than L_MAXMAJ32
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	devcnt = MIN(num_devs + 30, L_MAXMAJ32);
1860Sstevel@tonic-gate 	devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
1870Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++)
1880Sstevel@tonic-gate 		devopsp[i] = &mod_nodev_ops;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	init_devnamesp(devcnt);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * Sync up with the work that the stand-alone linker has already done.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	(void) kobj_sync();
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
1980Sstevel@tonic-gate 		kdi_dvec_modavail();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	make_aliases(mb_hashtab);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Initialize streams device implementation structures.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * If the cl_bootstrap module is present,
2090Sstevel@tonic-gate 	 * we should be configured as a cluster. Loading this module
2100Sstevel@tonic-gate 	 * will set "cluster_bootflags" to non-zero.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	(void) modload("misc", "cl_bootstrap");
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
2150Sstevel@tonic-gate 	init_syscallnames(NSYSCALL);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * Start up dynamic autoconfiguration framework (dacf).
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	mod_hash_init();
2210Sstevel@tonic-gate 	dacf_init();
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * Start up IP policy framework (ipp).
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	ipp_init();
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Allocate loadable native system call locks.
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 	for (callnum = 0, callp = sysent; callnum < NSYSCALL;
2320Sstevel@tonic-gate 	    callnum++, callp++) {
2330Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2340Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2350Sstevel@tonic-gate 				callp->sy_lock =
2360Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2370Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2380Sstevel@tonic-gate 			} else {
2390Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2400Sstevel@tonic-gate 				callp->sy_callc = nosys;
2410Sstevel@tonic-gate 			}
2420Sstevel@tonic-gate #ifdef DEBUG
2430Sstevel@tonic-gate 		} else {
2440Sstevel@tonic-gate 			/*
2450Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2460Sstevel@tonic-gate 			 */
2470Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2480Sstevel@tonic-gate 			case SE_32RVAL1:
2490Sstevel@tonic-gate 				/* only r_val1 returned */
2500Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2510Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2520Sstevel@tonic-gate 			case SE_64RVAL:
2530Sstevel@tonic-gate 				/* 64-bit rval returned */
2540Sstevel@tonic-gate 				break;
2550Sstevel@tonic-gate 			default:
2560Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
2570Sstevel@tonic-gate 				    callnum, callp->sy_flags);
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate #endif
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * Allocate loadable system call locks for 32-bit compat syscalls
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
2680Sstevel@tonic-gate 	    callnum++, callp++) {
2690Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2700Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2710Sstevel@tonic-gate 				callp->sy_lock =
2720Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2730Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2740Sstevel@tonic-gate 			} else {
2750Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2760Sstevel@tonic-gate 				callp->sy_callc = nosys;
2770Sstevel@tonic-gate 			}
2780Sstevel@tonic-gate #ifdef DEBUG
2790Sstevel@tonic-gate 		} else {
2800Sstevel@tonic-gate 			/*
2810Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2820Sstevel@tonic-gate 			 */
2830Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2840Sstevel@tonic-gate 			case SE_32RVAL1:
2850Sstevel@tonic-gate 				/* only r_val1 returned */
2860Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2870Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2880Sstevel@tonic-gate 			case SE_64RVAL:
2890Sstevel@tonic-gate 				/* 64-bit rval returned */
2900Sstevel@tonic-gate 				break;
2910Sstevel@tonic-gate 			default:
2920Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
2930Sstevel@tonic-gate 				    callnum, callp->sy_flags);
2940Sstevel@tonic-gate 				goto skip;
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 			/*
2980Sstevel@tonic-gate 			 * Cross-check the native and compatibility tables.
2990Sstevel@tonic-gate 			 */
3000Sstevel@tonic-gate 			if (callp->sy_callc == nosys ||
3010Sstevel@tonic-gate 			    sysent[callnum].sy_callc == nosys)
3020Sstevel@tonic-gate 				continue;
3030Sstevel@tonic-gate 			/*
3040Sstevel@tonic-gate 			 * If only one or the other slot is loadable, then
3050Sstevel@tonic-gate 			 * there's an error -- they should match!
3060Sstevel@tonic-gate 			 */
3070Sstevel@tonic-gate 			if ((callp->sy_callc == loadable_syscall) ^
3080Sstevel@tonic-gate 			    (sysent[callnum].sy_callc == loadable_syscall)) {
3090Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] loadable?",
3100Sstevel@tonic-gate 				    callnum);
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 			/*
3130Sstevel@tonic-gate 			 * This is more of a heuristic test -- if the
3140Sstevel@tonic-gate 			 * system call returns two values in the 32-bit
3150Sstevel@tonic-gate 			 * world, it should probably return two 32-bit
3160Sstevel@tonic-gate 			 * values in the 64-bit world too.
3170Sstevel@tonic-gate 			 */
3180Sstevel@tonic-gate 			if (((callp->sy_flags & SE_32RVAL2) == 0) ^
3190Sstevel@tonic-gate 			    ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
3200Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
3210Sstevel@tonic-gate 				    callnum);
3220Sstevel@tonic-gate 			}
3230Sstevel@tonic-gate skip:;
3240Sstevel@tonic-gate #endif	/* DEBUG */
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Allocate loadable exec locks.  (Assumes all execs are loadable)
3310Sstevel@tonic-gate 	 */
3320Sstevel@tonic-gate 	for (exectype = 0; exectype < nexectype; exectype++) {
3330Sstevel@tonic-gate 		execsw[exectype].exec_lock =
3340Sstevel@tonic-gate 		    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
3350Sstevel@tonic-gate 		rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	read_class_file();
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/* init thread specific structure for mod_uninstall_all */
3410Sstevel@tonic-gate 	tsd_create(&mod_autounload_key, NULL);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static int
3450Sstevel@tonic-gate modctl_modload(int use_path, char *filename, int *rvp)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	struct modctl *modp;
3480Sstevel@tonic-gate 	int retval = 0;
3490Sstevel@tonic-gate 	char *filenamep;
3500Sstevel@tonic-gate 	int modid;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
3550Sstevel@tonic-gate 		retval = EFAULT;
3560Sstevel@tonic-gate 		goto out;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	filenamep[MOD_MAXPATH - 1] = 0;
3600Sstevel@tonic-gate 	modp = mod_hold_installed_mod(filenamep, use_path, &retval);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (modp == NULL)
3630Sstevel@tonic-gate 		goto out;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
3660Sstevel@tonic-gate 	modid = modp->mod_id;
3670Sstevel@tonic-gate 	mod_release_mod(modp);
3680Sstevel@tonic-gate 	if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
3690Sstevel@tonic-gate 		retval = EFAULT;
3700Sstevel@tonic-gate out:
3710Sstevel@tonic-gate 	kmem_free(filenamep, MOD_MAXPATH);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (retval);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static int
3770Sstevel@tonic-gate modctl_modunload(modid_t id)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	int rval = 0;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (id == 0) {
3820Sstevel@tonic-gate #ifdef DEBUG
3830Sstevel@tonic-gate 		/*
3840Sstevel@tonic-gate 		 * Turn on mod_uninstall_daemon
3850Sstevel@tonic-gate 		 */
3860Sstevel@tonic-gate 		if (mod_uninstall_interval == 0) {
3870Sstevel@tonic-gate 			mod_uninstall_interval = 60;
3880Sstevel@tonic-gate 			modreap();
3890Sstevel@tonic-gate 			return (rval);
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate #endif
3920Sstevel@tonic-gate 		mod_uninstall_all();
3930Sstevel@tonic-gate 	} else {
3940Sstevel@tonic-gate 		(void) devfs_clean(ddi_root_node(), NULL, 0);
3950Sstevel@tonic-gate 		rval = modunload(id);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	return (rval);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate static int
4010Sstevel@tonic-gate modctl_modinfo(modid_t id, struct modinfo *umodi)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	int retval;
4040Sstevel@tonic-gate 	struct modinfo modi;
4050Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4060Sstevel@tonic-gate 	int nobase;
4070Sstevel@tonic-gate 	struct modinfo32 modi32;
4080Sstevel@tonic-gate #endif
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4110Sstevel@tonic-gate 		if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
4120Sstevel@tonic-gate 			return (EFAULT);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4150Sstevel@tonic-gate 	else {
4160Sstevel@tonic-gate 		bzero(&modi, sizeof (modi));
4170Sstevel@tonic-gate 		if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
4180Sstevel@tonic-gate 			return (EFAULT);
4190Sstevel@tonic-gate 		modi.mi_info = modi32.mi_info;
4200Sstevel@tonic-gate 		modi.mi_id = modi32.mi_id;
4210Sstevel@tonic-gate 		modi.mi_nextid = modi32.mi_nextid;
4220Sstevel@tonic-gate 		nobase = modi.mi_info & MI_INFO_NOBASE;
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate #endif
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * This flag is -only- for the kernels use.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	modi.mi_info &= ~MI_INFO_LINKAGE;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	retval = modinfo(id, &modi);
4310Sstevel@tonic-gate 	if (retval)
4320Sstevel@tonic-gate 		return (retval);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4350Sstevel@tonic-gate 		if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
4360Sstevel@tonic-gate 			retval = EFAULT;
4370Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 		int i;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
4420Sstevel@tonic-gate 			return (EOVERFLOW);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		modi32.mi_info = modi.mi_info;
4450Sstevel@tonic-gate 		modi32.mi_state = modi.mi_state;
4460Sstevel@tonic-gate 		modi32.mi_id = modi.mi_id;
4470Sstevel@tonic-gate 		modi32.mi_nextid = modi.mi_nextid;
4480Sstevel@tonic-gate 		modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
4490Sstevel@tonic-gate 		modi32.mi_size = modi.mi_size;
4500Sstevel@tonic-gate 		modi32.mi_rev = modi.mi_rev;
4510Sstevel@tonic-gate 		modi32.mi_loadcnt = modi.mi_loadcnt;
4520Sstevel@tonic-gate 		bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
4530Sstevel@tonic-gate 		for (i = 0; i < MODMAXLINK32; i++) {
4540Sstevel@tonic-gate 			modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
4550Sstevel@tonic-gate 			bcopy(modi.mi_msinfo[i].msi_linkinfo,
4560Sstevel@tonic-gate 			    modi32.mi_msinfo[i].msi_linkinfo,
4570Sstevel@tonic-gate 			    sizeof (modi32.mi_msinfo[0].msi_linkinfo));
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 		if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
4600Sstevel@tonic-gate 			retval = EFAULT;
4610Sstevel@tonic-gate #endif
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	return (retval);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * Return the last major number in the range of permissible major numbers.
4690Sstevel@tonic-gate  */
4700Sstevel@tonic-gate /*ARGSUSED*/
4710Sstevel@tonic-gate static int
4720Sstevel@tonic-gate modctl_modreserve(modid_t id, int *data)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
4750Sstevel@tonic-gate 		return (EFAULT);
4760Sstevel@tonic-gate 	return (0);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static int
4800Sstevel@tonic-gate modctl_add_major(int *data)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	struct modconfig mc;
4830Sstevel@tonic-gate 	int i, rv;
4840Sstevel@tonic-gate 	struct aliases alias;
4850Sstevel@tonic-gate 	struct aliases *ap;
4860Sstevel@tonic-gate 	char name[MAXMODCONFNAME];
4870Sstevel@tonic-gate 	char cname[MAXMODCONFNAME];
4880Sstevel@tonic-gate 	char *drvname;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	bzero(&mc, sizeof (struct modconfig));
4910Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4920Sstevel@tonic-gate 		if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
4930Sstevel@tonic-gate 			return (EFAULT);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4960Sstevel@tonic-gate 	else {
4970Sstevel@tonic-gate 		struct modconfig32 modc32;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
5000Sstevel@tonic-gate 			return (EFAULT);
5010Sstevel@tonic-gate 		else {
5020Sstevel@tonic-gate 			bcopy(modc32.drvname, mc.drvname,
5030Sstevel@tonic-gate 			    sizeof (modc32.drvname));
5040Sstevel@tonic-gate 			bcopy(modc32.drvclass, mc.drvclass,
5050Sstevel@tonic-gate 			    sizeof (modc32.drvclass));
5060Sstevel@tonic-gate 			mc.major = modc32.major;
5070Sstevel@tonic-gate 			mc.num_aliases = modc32.num_aliases;
5080Sstevel@tonic-gate 			mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate #endif
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * If the driver is already in the mb_hashtab, and the name given
5150Sstevel@tonic-gate 	 * doesn't match that driver's name, fail.  Otherwise, pass, since
5160Sstevel@tonic-gate 	 * we may be adding aliases.
5170Sstevel@tonic-gate 	 */
5180Sstevel@tonic-gate 	if ((drvname = mod_major_to_name(mc.major)) != NULL &&
5190Sstevel@tonic-gate 	    strcmp(drvname, mc.drvname) != 0)
5200Sstevel@tonic-gate 		return (EINVAL);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Add each supplied driver alias to mb_hashtab
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	ap = mc.ap;
5260Sstevel@tonic-gate 	for (i = 0; i < mc.num_aliases; i++) {
5270Sstevel@tonic-gate 		bzero(&alias, sizeof (struct aliases));
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
5300Sstevel@tonic-gate 			if (copyin(ap, &alias, sizeof (struct aliases)) != 0)
5310Sstevel@tonic-gate 				return (EFAULT);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 			if (alias.a_len > MAXMODCONFNAME)
5340Sstevel@tonic-gate 				return (EINVAL);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 			if (copyin(alias.a_name, name, alias.a_len) != 0)
5370Sstevel@tonic-gate 				return (EFAULT);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 			if (name[alias.a_len - 1] != '\0')
5400Sstevel@tonic-gate 				return (EINVAL);
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5430Sstevel@tonic-gate 		else {
5440Sstevel@tonic-gate 			struct aliases32 al32;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 			bzero(&al32, sizeof (struct aliases32));
5470Sstevel@tonic-gate 			if (copyin(ap, &al32, sizeof (struct aliases32)) != 0)
5480Sstevel@tonic-gate 				return (EFAULT);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 			if (al32.a_len > MAXMODCONFNAME)
5510Sstevel@tonic-gate 				return (EINVAL);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 			if (copyin((void *)(uintptr_t)al32.a_name,
5540Sstevel@tonic-gate 			    name, al32.a_len) != 0)
5550Sstevel@tonic-gate 				return (EFAULT);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 			if (name[al32.a_len - 1] != '\0')
5580Sstevel@tonic-gate 				return (EINVAL);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 			alias.a_next = (void *)(uintptr_t)al32.a_next;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 		check_esc_sequences(name, cname);
5640Sstevel@tonic-gate 		(void) make_mbind(cname, mc.major, NULL, mb_hashtab);
5650Sstevel@tonic-gate 		ap = alias.a_next;
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/*
5690Sstevel@tonic-gate 	 * Try to establish an mbinding for mc.drvname, and add it to devnames.
5700Sstevel@tonic-gate 	 * Add class if any after establishing the major number
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	(void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
5730Sstevel@tonic-gate 	rv = make_devname(mc.drvname, mc.major);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (rv == 0) {
5760Sstevel@tonic-gate 		if (mc.drvclass[0] != '\0')
5770Sstevel@tonic-gate 			add_class(mc.drvname, mc.drvclass);
5780Sstevel@tonic-gate 		(void) i_ddi_load_drvconf(mc.major);
5790Sstevel@tonic-gate 		i_ddi_bind_devs();
5800Sstevel@tonic-gate 		i_ddi_di_cache_invalidate(KM_SLEEP);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 	return (rv);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate static int
5860Sstevel@tonic-gate modctl_rem_major(major_t major)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	struct devnames *dnp;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (major >= devcnt)
5910Sstevel@tonic-gate 		return (EINVAL);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/* mark devnames as removed */
5940Sstevel@tonic-gate 	dnp = &devnamesp[major];
5950Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
5960Sstevel@tonic-gate 	if (dnp->dn_name == NULL ||
5970Sstevel@tonic-gate 	    (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
5980Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
5990Sstevel@tonic-gate 		return (EINVAL);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_REMOVED;
6020Sstevel@tonic-gate 	pm_driver_removed(major);
6030Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	(void) i_ddi_unload_drvconf(major);
6060Sstevel@tonic-gate 	i_ddi_unbind_devs(major);
6070Sstevel@tonic-gate 	i_ddi_di_cache_invalidate(KM_SLEEP);
6080Sstevel@tonic-gate 	return (0);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate static struct vfs *
6120Sstevel@tonic-gate path_to_vfs(char *name)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	vnode_t *vp;
6150Sstevel@tonic-gate 	struct vfs *vfsp;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
6180Sstevel@tonic-gate 		return (NULL);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
6210Sstevel@tonic-gate 	VN_RELE(vp);
6220Sstevel@tonic-gate 	return (vfsp);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate static int
6260Sstevel@tonic-gate new_vfs_in_modpath()
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	static int n_modpath = 0;
6290Sstevel@tonic-gate 	static char *modpath_copy;
6300Sstevel@tonic-gate 	static struct pathvfs {
6310Sstevel@tonic-gate 		char *path;
6320Sstevel@tonic-gate 		struct vfs *vfsp;
6330Sstevel@tonic-gate 	} *pathvfs;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	int i, new_vfs = 0;
6360Sstevel@tonic-gate 	char *tmp, *tmp1;
6370Sstevel@tonic-gate 	struct vfs *vfsp;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (n_modpath != 0) {
6400Sstevel@tonic-gate 		for (i = 0; i < n_modpath; i++) {
6410Sstevel@tonic-gate 			vfsp = path_to_vfs(pathvfs[i].path);
6420Sstevel@tonic-gate 			if (vfsp != pathvfs[i].vfsp) {
6430Sstevel@tonic-gate 				pathvfs[i].vfsp = vfsp;
6440Sstevel@tonic-gate 				if (vfsp)
6450Sstevel@tonic-gate 					new_vfs = 1;
6460Sstevel@tonic-gate 			}
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 		return (new_vfs);
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * First call, initialize the pathvfs structure
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
6550Sstevel@tonic-gate 	tmp = modpath_copy;
6560Sstevel@tonic-gate 	n_modpath = 1;
6570Sstevel@tonic-gate 	tmp1 = strchr(tmp, ' ');
6580Sstevel@tonic-gate 	while (tmp1) {
6590Sstevel@tonic-gate 		*tmp1 = '\0';
6600Sstevel@tonic-gate 		n_modpath++;
6610Sstevel@tonic-gate 		tmp = tmp1 + 1;
6620Sstevel@tonic-gate 		tmp1 = strchr(tmp, ' ');
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
6660Sstevel@tonic-gate 	tmp = modpath_copy;
6670Sstevel@tonic-gate 	for (i = 0; i < n_modpath; i++) {
6680Sstevel@tonic-gate 		pathvfs[i].path = tmp;
6690Sstevel@tonic-gate 		vfsp = path_to_vfs(tmp);
6700Sstevel@tonic-gate 		pathvfs[i].vfsp = vfsp;
6710Sstevel@tonic-gate 		tmp += strlen(tmp) + 1;
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	return (1);	/* always reread driver.conf the first time */
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate static int modctl_load_drvconf(major_t major)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	int ret;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (major != (major_t)-1) {
6810Sstevel@tonic-gate 		ret = i_ddi_load_drvconf(major);
6820Sstevel@tonic-gate 		if (ret == 0)
6830Sstevel@tonic-gate 			i_ddi_bind_devs();
6840Sstevel@tonic-gate 		return (ret);
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * We are invoked to rescan new driver.conf files. It is
6890Sstevel@tonic-gate 	 * only necessary if a new file system was mounted in the
6900Sstevel@tonic-gate 	 * module_path. Because rescanning driver.conf files can
6910Sstevel@tonic-gate 	 * take some time on older platforms (sun4m), the following
6920Sstevel@tonic-gate 	 * code skips unnecessary driver.conf rescans to optimize
6930Sstevel@tonic-gate 	 * boot performance.
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if (new_vfs_in_modpath()) {
6960Sstevel@tonic-gate 		(void) i_ddi_load_drvconf((major_t)-1);
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * If we are still initializing io subsystem,
6990Sstevel@tonic-gate 		 * load drivers with ddi-forceattach property
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		if (!i_ddi_io_initialized())
7020Sstevel@tonic-gate 			i_ddi_forceattach_drivers();
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	return (0);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate static int
7080Sstevel@tonic-gate modctl_unload_drvconf(major_t major)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	int ret;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (major >= devcnt)
7130Sstevel@tonic-gate 		return (EINVAL);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	ret = i_ddi_unload_drvconf(major);
7160Sstevel@tonic-gate 	if (ret != 0)
7170Sstevel@tonic-gate 		return (ret);
7180Sstevel@tonic-gate 	(void) i_ddi_unbind_devs(major);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (0);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate static void
7240Sstevel@tonic-gate check_esc_sequences(char *str, char *cstr)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	int i;
7270Sstevel@tonic-gate 	size_t len;
7280Sstevel@tonic-gate 	char *p;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	len = strlen(str);
7310Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++, cstr++) {
7320Sstevel@tonic-gate 		if (*str != '\\') {
7330Sstevel@tonic-gate 			*cstr = *str;
7340Sstevel@tonic-gate 		} else {
7350Sstevel@tonic-gate 			p = str + 1;
7360Sstevel@tonic-gate 			/*
7370Sstevel@tonic-gate 			 * we only handle octal escape sequences for SPACE
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			if (*p++ == '0' && *p++ == '4' && *p == '0') {
7400Sstevel@tonic-gate 				*cstr = ' ';
7410Sstevel@tonic-gate 				str += 3;
7420Sstevel@tonic-gate 			} else {
7430Sstevel@tonic-gate 				*cstr = *str;
7440Sstevel@tonic-gate 			}
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	*cstr = 0;
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate static int
7510Sstevel@tonic-gate modctl_getmodpathlen(int *data)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate 	int len;
7540Sstevel@tonic-gate 	len = strlen(default_path);
7550Sstevel@tonic-gate 	if (copyout(&len, data, sizeof (len)) != 0)
7560Sstevel@tonic-gate 		return (EFAULT);
7570Sstevel@tonic-gate 	return (0);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate static int
7610Sstevel@tonic-gate modctl_getmodpath(char *data)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	if (copyout(default_path, data, strlen(default_path) + 1) != 0)
7640Sstevel@tonic-gate 		return (EFAULT);
7650Sstevel@tonic-gate 	return (0);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate static int
7690Sstevel@tonic-gate modctl_read_sysbinding_file(void)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
7720Sstevel@tonic-gate 	return (0);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate static int
7760Sstevel@tonic-gate modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	char name[256];
7790Sstevel@tonic-gate 	int retval;
7800Sstevel@tonic-gate 	major_t major;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	if ((retval = copyinstr(uname, name,
7830Sstevel@tonic-gate 	    (ulen < 256) ? ulen : 256, 0)) != 0)
7840Sstevel@tonic-gate 		return (retval);
7850Sstevel@tonic-gate 	if ((major = mod_name_to_major(name)) == (major_t)-1)
7860Sstevel@tonic-gate 		return (ENODEV);
7870Sstevel@tonic-gate 	if (copyout(&major, umajorp, sizeof (major_t)) != 0)
7880Sstevel@tonic-gate 		return (EFAULT);
7890Sstevel@tonic-gate 	return (0);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate static int
7930Sstevel@tonic-gate modctl_getname(char *uname, uint_t ulen, int *umajorp)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	char *name;
7960Sstevel@tonic-gate 	major_t major;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if (copyin(umajorp, &major, sizeof (major)) != 0)
7990Sstevel@tonic-gate 		return (EFAULT);
8000Sstevel@tonic-gate 	if ((name = mod_major_to_name(major)) == NULL)
8010Sstevel@tonic-gate 		return (ENODEV);
8020Sstevel@tonic-gate 	if ((strlen(name) + 1) > ulen)
8030Sstevel@tonic-gate 		return (ENOSPC);
8040Sstevel@tonic-gate 	return (copyoutstr(name, uname, ulen, NULL));
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate static int
8080Sstevel@tonic-gate modctl_devt2instance(dev_t dev, int *uinstancep)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	int	instance;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if ((instance = dev_to_instance(dev)) == -1)
8130Sstevel@tonic-gate 		return (EINVAL);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	return (copyout(&instance, uinstancep, sizeof (int)));
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Return the sizeof of the device id.
8200Sstevel@tonic-gate  */
8210Sstevel@tonic-gate static int
8220Sstevel@tonic-gate modctl_sizeof_devid(dev_t dev, uint_t *len)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	uint_t		sz;
8250Sstevel@tonic-gate 	ddi_devid_t	devid;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/* get device id */
8280Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
8290Sstevel@tonic-gate 		return (EINVAL);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
8320Sstevel@tonic-gate 	ddi_devid_free(devid);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	/* copyout device id size */
8350Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
8360Sstevel@tonic-gate 		return (EFAULT);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	return (0);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate /*
8420Sstevel@tonic-gate  * Return a copy of the device id.
8430Sstevel@tonic-gate  */
8440Sstevel@tonic-gate static int
8450Sstevel@tonic-gate modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate 	uint_t		sz;
8480Sstevel@tonic-gate 	ddi_devid_t	devid;
8490Sstevel@tonic-gate 	int		err = 0;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/* get device id */
8520Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
8530Sstevel@tonic-gate 		return (EINVAL);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* Error if device id is larger than space allocated */
8580Sstevel@tonic-gate 	if (sz > len) {
8590Sstevel@tonic-gate 		ddi_devid_free(devid);
8600Sstevel@tonic-gate 		return (ENOSPC);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/* copy out device id */
8640Sstevel@tonic-gate 	if (copyout(devid, udevid, sz) != 0)
8650Sstevel@tonic-gate 		err = EFAULT;
8660Sstevel@tonic-gate 	ddi_devid_free(devid);
8670Sstevel@tonic-gate 	return (err);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * return the /devices paths associated with the specified devid and
8720Sstevel@tonic-gate  * minor name.
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate /*ARGSUSED*/
8750Sstevel@tonic-gate static int
8760Sstevel@tonic-gate modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
8770Sstevel@tonic-gate 	size_t *ulensp, char *upaths)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	ddi_devid_t	devid = NULL;
8800Sstevel@tonic-gate 	int		devid_len;
8810Sstevel@tonic-gate 	char		*minor_name = NULL;
8820Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
8830Sstevel@tonic-gate 	struct ddi_minor_data   *dmdp;
8840Sstevel@tonic-gate 	char		*path = NULL;
8850Sstevel@tonic-gate 	int		ulens;
8860Sstevel@tonic-gate 	int		lens;
8870Sstevel@tonic-gate 	int		len;
8880Sstevel@tonic-gate 	dev_t		*devlist = NULL;
8890Sstevel@tonic-gate 	int		ndevs;
8900Sstevel@tonic-gate 	int		i;
8910Sstevel@tonic-gate 	int		ret = 0;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/*
8940Sstevel@tonic-gate 	 * If upaths is NULL then we are only computing the amount of space
8950Sstevel@tonic-gate 	 * needed to hold the paths and returning the value in *ulensp. If we
8960Sstevel@tonic-gate 	 * are copying out paths then we get the amount of space allocated by
8970Sstevel@tonic-gate 	 * the caller. If the actual space needed for paths is larger, or
8980Sstevel@tonic-gate 	 * things are changing out from under us, then we return EAGAIN.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	if (upaths) {
9010Sstevel@tonic-gate 		if (ulensp == NULL)
9020Sstevel@tonic-gate 			return (EINVAL);
9030Sstevel@tonic-gate 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
9040Sstevel@tonic-gate 			return (EFAULT);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * copyin enough of the devid to determine the length then
9090Sstevel@tonic-gate 	 * reallocate and copy in the entire devid.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(NULL);
9120Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
9130Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
9140Sstevel@tonic-gate 		ret = EFAULT;
9150Sstevel@tonic-gate 		goto out;
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 	len = devid_len;
9180Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(devid);
9190Sstevel@tonic-gate 	kmem_free(devid, len);
9200Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
9210Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
9220Sstevel@tonic-gate 		ret = EFAULT;
9230Sstevel@tonic-gate 		goto out;
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/* copyin the minor name if specified. */
9270Sstevel@tonic-gate 	minor_name = uminor_name;
9280Sstevel@tonic-gate 	if ((minor_name != DEVID_MINOR_NAME_ALL) &&
9290Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
9300Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
9310Sstevel@tonic-gate 		minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9320Sstevel@tonic-gate 		if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
9330Sstevel@tonic-gate 			ret = EFAULT;
9340Sstevel@tonic-gate 			goto out;
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * Use existing function to resolve the devid into a devlist.
9400Sstevel@tonic-gate 	 *
9410Sstevel@tonic-gate 	 * NOTE: there is a loss of spectype information in the current
9420Sstevel@tonic-gate 	 * ddi_lyr_devid_to_devlist implementation. We work around this by not
9430Sstevel@tonic-gate 	 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
9440Sstevel@tonic-gate 	 * node forms in the loop processing the devlist below. It would be
9450Sstevel@tonic-gate 	 * best if at some point the use of this interface here was replaced
9460Sstevel@tonic-gate 	 * with a path oriented call.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	if (ddi_lyr_devid_to_devlist(devid,
9490Sstevel@tonic-gate 	    (minor_name == DEVID_MINOR_NAME_ALL) ?
9500Sstevel@tonic-gate 	    DEVID_MINOR_NAME_ALL_CHR : minor_name,
9510Sstevel@tonic-gate 	    &ndevs, &devlist) != DDI_SUCCESS) {
9520Sstevel@tonic-gate 		ret = EINVAL;
9530Sstevel@tonic-gate 		goto out;
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * loop over the devlist, converting each devt to a path and doing
9580Sstevel@tonic-gate 	 * a copyout of the path and computation of the amount of space
9590Sstevel@tonic-gate 	 * needed to hold all the paths
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9620Sstevel@tonic-gate 	for (i = 0, lens = 0; i < ndevs; i++) {
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		/* find the dip associated with the dev_t */
9650Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
9660Sstevel@tonic-gate 			continue;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		/* loop over all the minor nodes, skipping ones we don't want */
9690Sstevel@tonic-gate 		for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
9700Sstevel@tonic-gate 			if ((dmdp->ddm_dev != devlist[i]) ||
9710Sstevel@tonic-gate 			    (dmdp->type != DDM_MINOR))
9720Sstevel@tonic-gate 				continue;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 			if ((minor_name != DEVID_MINOR_NAME_ALL) &&
9750Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
9760Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
9770Sstevel@tonic-gate 			    strcmp(minor_name, dmdp->ddm_name))
9780Sstevel@tonic-gate 				continue;
9790Sstevel@tonic-gate 			else {
9800Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
9810Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFCHR))
9820Sstevel@tonic-gate 					continue;
9830Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
9840Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFBLK))
9850Sstevel@tonic-gate 					continue;
9860Sstevel@tonic-gate 			}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 			/* XXX need ddi_pathname_minor(dmdp, path); interface */
9890Sstevel@tonic-gate 			if (ddi_dev_pathname(dmdp->ddm_dev, dmdp->ddm_spec_type,
9900Sstevel@tonic-gate 			    path) != DDI_SUCCESS) {
9910Sstevel@tonic-gate 				ret = EAGAIN;
9920Sstevel@tonic-gate 				goto out;
9930Sstevel@tonic-gate 			}
9940Sstevel@tonic-gate 			len = strlen(path) + 1;
9950Sstevel@tonic-gate 			*(path + len) = '\0';	/* set double termination */
9960Sstevel@tonic-gate 			lens += len;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			/* copyout the path with double terminations */
9990Sstevel@tonic-gate 			if (upaths) {
10000Sstevel@tonic-gate 				if (lens > ulens) {
10010Sstevel@tonic-gate 					ret = EAGAIN;
10020Sstevel@tonic-gate 					goto out;
10030Sstevel@tonic-gate 				}
10040Sstevel@tonic-gate 				if (copyout(path, upaths, len + 1)) {
10050Sstevel@tonic-gate 					ret = EFAULT;
10060Sstevel@tonic-gate 					goto out;
10070Sstevel@tonic-gate 				}
10080Sstevel@tonic-gate 				upaths += len;
10090Sstevel@tonic-gate 			}
10100Sstevel@tonic-gate 		}
10110Sstevel@tonic-gate 		ddi_release_devi(dip);
10120Sstevel@tonic-gate 		dip = NULL;
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 	lens++;		/* add one for double termination */
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	/* copy out the amount of space needed to hold the paths */
10170Sstevel@tonic-gate 	if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
10180Sstevel@tonic-gate 		ret = EFAULT;
10190Sstevel@tonic-gate 		goto out;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 	ret = 0;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate out:	if (dip)
10240Sstevel@tonic-gate 		ddi_release_devi(dip);
10250Sstevel@tonic-gate 	if (path)
10260Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
10270Sstevel@tonic-gate 	if (devlist)
10280Sstevel@tonic-gate 		ddi_lyr_free_devlist(devlist, ndevs);
10290Sstevel@tonic-gate 	if (minor_name &&
10300Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL) &&
10310Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
10320Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK))
10330Sstevel@tonic-gate 		kmem_free(minor_name, MAXPATHLEN);
10340Sstevel@tonic-gate 	if (devid)
10350Sstevel@tonic-gate 		kmem_free(devid, devid_len);
10360Sstevel@tonic-gate 	return (ret);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate  * Return the size of the minor name.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate static int
10430Sstevel@tonic-gate modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	uint_t	sz;
10460Sstevel@tonic-gate 	char	*name;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	/* get the minor name */
10490Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
10500Sstevel@tonic-gate 		return (EINVAL);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	sz = strlen(name) + 1;
10530Sstevel@tonic-gate 	kmem_free(name, sz);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/* copy out the size of the minor name */
10560Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
10570Sstevel@tonic-gate 		return (EFAULT);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	return (0);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * Return the minor name.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate static int
10660Sstevel@tonic-gate modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	uint_t	sz;
10690Sstevel@tonic-gate 	char	*name;
10700Sstevel@tonic-gate 	int	err = 0;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/* get the minor name */
10730Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
10740Sstevel@tonic-gate 		return (EINVAL);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	sz = strlen(name) + 1;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/* Error if the minor name is larger than the space allocated */
10790Sstevel@tonic-gate 	if (sz > len) {
10800Sstevel@tonic-gate 		kmem_free(name, sz);
10810Sstevel@tonic-gate 		return (ENOSPC);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/* copy out the minor name */
10850Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
10860Sstevel@tonic-gate 		err = EFAULT;
10870Sstevel@tonic-gate 	kmem_free(name, sz);
10880Sstevel@tonic-gate 	return (err);
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate  * Return the size of the devfspath name.
10930Sstevel@tonic-gate  */
10940Sstevel@tonic-gate static int
10950Sstevel@tonic-gate modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate 	uint_t	sz;
10980Sstevel@tonic-gate 	char	*name;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/* get the path name */
11010Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11020Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
11030Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11040Sstevel@tonic-gate 		return (EINVAL);
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	sz = strlen(name) + 1;
11080Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/* copy out the size of the path name */
11110Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
11120Sstevel@tonic-gate 		return (EFAULT);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	return (0);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Return the devfspath name.
11190Sstevel@tonic-gate  */
11200Sstevel@tonic-gate static int
11210Sstevel@tonic-gate modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	uint_t	sz;
11240Sstevel@tonic-gate 	char	*name;
11250Sstevel@tonic-gate 	int	err = 0;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/* get the path name */
11280Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11290Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
11300Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11310Sstevel@tonic-gate 		return (EINVAL);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	sz = strlen(name) + 1;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/* Error if the path name is larger than the space allocated */
11370Sstevel@tonic-gate 	if (sz > len) {
11380Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
11390Sstevel@tonic-gate 		return (ENOSPC);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/* copy out the path name */
11430Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
11440Sstevel@tonic-gate 		err = EFAULT;
11450Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
11460Sstevel@tonic-gate 	return (err);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate static int
11500Sstevel@tonic-gate modctl_get_fbname(char *path)
11510Sstevel@tonic-gate {
11520Sstevel@tonic-gate 	extern dev_t fbdev;
11530Sstevel@tonic-gate 	char *pathname = NULL;
11540Sstevel@tonic-gate 	int rval = 0;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* make sure fbdev is set before we plunge in */
11570Sstevel@tonic-gate 	if (fbdev == NODEV)
11580Sstevel@tonic-gate 		return (ENODEV);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11610Sstevel@tonic-gate 	if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
11620Sstevel@tonic-gate 	    pathname)) == DDI_SUCCESS) {
11630Sstevel@tonic-gate 		if (copyout(pathname, path, strlen(pathname)+1) != 0) {
11640Sstevel@tonic-gate 			rval = EFAULT;
11650Sstevel@tonic-gate 		}
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
11680Sstevel@tonic-gate 	return (rval);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate  * modctl_reread_dacf()
11730Sstevel@tonic-gate  *	Reread the dacf rules database from the named binding file.
11740Sstevel@tonic-gate  *	If NULL is specified, pass along the NULL, it means 'use the default'.
11750Sstevel@tonic-gate  */
11760Sstevel@tonic-gate static int
11770Sstevel@tonic-gate modctl_reread_dacf(char *path)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate 	int rval = 0;
11800Sstevel@tonic-gate 	char *filename, *filenamep;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (path == NULL) {
11850Sstevel@tonic-gate 		filenamep = NULL;
11860Sstevel@tonic-gate 	} else {
11870Sstevel@tonic-gate 		if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
11880Sstevel@tonic-gate 			rval = EFAULT;
11890Sstevel@tonic-gate 			goto out;
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 		filenamep = filename;
11920Sstevel@tonic-gate 		filenamep[MAXPATHLEN - 1] = '\0';
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	rval = read_dacf_binding_file(filenamep);
11960Sstevel@tonic-gate out:
11970Sstevel@tonic-gate 	kmem_free(filename, MAXPATHLEN);
11980Sstevel@tonic-gate 	return (rval);
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate /*ARGSUSED*/
12020Sstevel@tonic-gate static int
12030Sstevel@tonic-gate modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
12040Sstevel@tonic-gate     uint_t flag)
12050Sstevel@tonic-gate {
12060Sstevel@tonic-gate 	int error = 0;
12070Sstevel@tonic-gate 	char *filenamep;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	switch (subcmd) {
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	case MODEVENTS_FLUSH:
12120Sstevel@tonic-gate 		/* flush all currently queued events */
12130Sstevel@tonic-gate 		log_sysevent_flushq(subcmd, flag);
12140Sstevel@tonic-gate 		break;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
12170Sstevel@tonic-gate 		/*
12180Sstevel@tonic-gate 		 * bind door_upcall to filename
12190Sstevel@tonic-gate 		 * this should only be done once per invocation
12200Sstevel@tonic-gate 		 * of the event daemon.
12210Sstevel@tonic-gate 		 */
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 		if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
12260Sstevel@tonic-gate 			error = EFAULT;
12270Sstevel@tonic-gate 		} else {
12280Sstevel@tonic-gate 			error = log_sysevent_filename(filenamep);
12290Sstevel@tonic-gate 		}
12300Sstevel@tonic-gate 		kmem_free(filenamep, MOD_MAXPATH);
12310Sstevel@tonic-gate 		break;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	case MODEVENTS_GETDATA:
12340Sstevel@tonic-gate 		error = log_sysevent_copyout_data((sysevent_id_t *)a2,
12350Sstevel@tonic-gate 		    (size_t)a3, (caddr_t)a4);
12360Sstevel@tonic-gate 		break;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	case MODEVENTS_FREEDATA:
12390Sstevel@tonic-gate 		error = log_sysevent_free_data((sysevent_id_t *)a2);
12400Sstevel@tonic-gate 		break;
12410Sstevel@tonic-gate 	case MODEVENTS_POST_EVENT:
12420Sstevel@tonic-gate 		error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
12430Sstevel@tonic-gate 			(sysevent_id_t *)a4);
12440Sstevel@tonic-gate 		break;
12450Sstevel@tonic-gate 	case MODEVENTS_REGISTER_EVENT:
12460Sstevel@tonic-gate 		error = log_sysevent_register((char *)a2, (char *)a3,
12470Sstevel@tonic-gate 		    (se_pubsub_t *)a4);
12480Sstevel@tonic-gate 		break;
12490Sstevel@tonic-gate 	default:
12500Sstevel@tonic-gate 		error = EINVAL;
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	return (error);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate static void
12570Sstevel@tonic-gate free_mperm(mperm_t *mp)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	int len;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	if (mp->mp_minorname) {
12620Sstevel@tonic-gate 		len = strlen(mp->mp_minorname) + 1;
12630Sstevel@tonic-gate 		kmem_free(mp->mp_minorname, len);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 	kmem_free(mp, sizeof (mperm_t));
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate #define	MP_NO_DRV_ERR	\
12690Sstevel@tonic-gate 	"/etc/minor_perm: no driver for %s\n"
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate #define	MP_EMPTY_MINOR	\
12720Sstevel@tonic-gate 	"/etc/minor_perm: empty minor name for driver %s\n"
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate #define	MP_NO_MINOR	\
12750Sstevel@tonic-gate 	"/etc/minor_perm: no minor matching %s for driver %s\n"
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate /*
12780Sstevel@tonic-gate  * Remove mperm entry with matching minorname
12790Sstevel@tonic-gate  */
12800Sstevel@tonic-gate static void
12810Sstevel@tonic-gate rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	mperm_t **mp_head;
12840Sstevel@tonic-gate 	mperm_t *freemp = NULL;
12850Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
12860Sstevel@tonic-gate 	mperm_t **wildmp;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
12910Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
12920Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
12930Sstevel@tonic-gate 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
12940Sstevel@tonic-gate 		if (*wildmp)
12950Sstevel@tonic-gate 			freemp = *wildmp;
12960Sstevel@tonic-gate 		*wildmp = NULL;
12970Sstevel@tonic-gate 	} else {
12980Sstevel@tonic-gate 		mp_head = &dnp->dn_mperm;
12990Sstevel@tonic-gate 		while (*mp_head) {
13000Sstevel@tonic-gate 			if (strcmp((*mp_head)->mp_minorname,
13010Sstevel@tonic-gate 			    mp->mp_minorname) != 0) {
13020Sstevel@tonic-gate 				mp_head = &(*mp_head)->mp_next;
13030Sstevel@tonic-gate 				continue;
13040Sstevel@tonic-gate 			}
13050Sstevel@tonic-gate 			/* remove the entry */
13060Sstevel@tonic-gate 			freemp = *mp_head;
13070Sstevel@tonic-gate 			*mp_head = freemp->mp_next;
13080Sstevel@tonic-gate 			break;
13090Sstevel@tonic-gate 		}
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 	if (freemp) {
13120Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13130Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
13140Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
13150Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
13160Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
13170Sstevel@tonic-gate 		}
13180Sstevel@tonic-gate 		free_mperm(freemp);
13190Sstevel@tonic-gate 	} else {
13200Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13210Sstevel@tonic-gate 			cmn_err(CE_CONT, MP_NO_MINOR,
13220Sstevel@tonic-gate 				drvname, mp->mp_minorname);
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate /*
13300Sstevel@tonic-gate  * Add minor perm entry
13310Sstevel@tonic-gate  */
13320Sstevel@tonic-gate static void
13330Sstevel@tonic-gate add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate 	mperm_t **mp_head;
13360Sstevel@tonic-gate 	mperm_t *freemp = NULL;
13370Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
13380Sstevel@tonic-gate 	mperm_t **wildmp;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	/*
13430Sstevel@tonic-gate 	 * Note that update_drv replace semantics require
13440Sstevel@tonic-gate 	 * replacing matching entries with the new permissions.
13450Sstevel@tonic-gate 	 */
13460Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
13470Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
13480Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
13490Sstevel@tonic-gate 			&dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
13500Sstevel@tonic-gate 		if (*wildmp)
13510Sstevel@tonic-gate 			freemp = *wildmp;
13520Sstevel@tonic-gate 		*wildmp = mp;
13530Sstevel@tonic-gate 	} else {
13540Sstevel@tonic-gate 		mperm_t *p, *v = NULL;
13550Sstevel@tonic-gate 		for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
13560Sstevel@tonic-gate 			if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
13570Sstevel@tonic-gate 				if (v == NULL)
13580Sstevel@tonic-gate 					dnp->dn_mperm = mp;
13590Sstevel@tonic-gate 				else
13600Sstevel@tonic-gate 					v->mp_next = mp;
13610Sstevel@tonic-gate 				mp->mp_next = p->mp_next;
13620Sstevel@tonic-gate 				freemp = p;
13630Sstevel@tonic-gate 				goto replaced;
13640Sstevel@tonic-gate 			}
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 		if (p == NULL) {
13670Sstevel@tonic-gate 			mp_head = &dnp->dn_mperm;
13680Sstevel@tonic-gate 			if (*mp_head == NULL) {
13690Sstevel@tonic-gate 				*mp_head = mp;
13700Sstevel@tonic-gate 			} else {
13710Sstevel@tonic-gate 				mp->mp_next = *mp_head;
13720Sstevel@tonic-gate 				*mp_head = mp;
13730Sstevel@tonic-gate 			}
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate replaced:
13770Sstevel@tonic-gate 	if (freemp) {
13780Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
13790Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
13800Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
13810Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
13820Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
13830Sstevel@tonic-gate 		}
13840Sstevel@tonic-gate 		free_mperm(freemp);
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 	if (moddebug & MODDEBUG_MINORPERM) {
13870Sstevel@tonic-gate 		cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
13880Sstevel@tonic-gate 		    drvname, mp->mp_minorname, mp->mp_mode & 0777,
13890Sstevel@tonic-gate 		    mp->mp_uid, mp->mp_gid);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate static int
13960Sstevel@tonic-gate process_minorperm(int cmd, nvlist_t *nvl)
13970Sstevel@tonic-gate {
13980Sstevel@tonic-gate 	char *minor;
13990Sstevel@tonic-gate 	major_t major;
14000Sstevel@tonic-gate 	mperm_t *mp;
14010Sstevel@tonic-gate 	nvpair_t *nvp;
14020Sstevel@tonic-gate 	char *name;
14030Sstevel@tonic-gate 	int is_clone;
14040Sstevel@tonic-gate 	major_t minmaj;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	ASSERT(cmd == MODLOADMINORPERM ||
14070Sstevel@tonic-gate 	    cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	nvp = NULL;
14100Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
14110Sstevel@tonic-gate 		name = nvpair_name(nvp);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 		is_clone = 0;
14140Sstevel@tonic-gate 		(void) nvpair_value_string(nvp, &minor);
14150Sstevel@tonic-gate 		major = ddi_name_to_major(name);
14160Sstevel@tonic-gate 		if (major != (major_t)-1) {
14170Sstevel@tonic-gate 			mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
14180Sstevel@tonic-gate 			if (minor == NULL || strlen(minor) == 0) {
14190Sstevel@tonic-gate 				if (moddebug & MODDEBUG_MINORPERM) {
14200Sstevel@tonic-gate 					cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
14210Sstevel@tonic-gate 				}
14220Sstevel@tonic-gate 				minor = "*";
14230Sstevel@tonic-gate 			}
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 			/*
14260Sstevel@tonic-gate 			 * The minor name of a node using the clone
14270Sstevel@tonic-gate 			 * driver must be the driver name.  To avoid
14280Sstevel@tonic-gate 			 * multiple searches, we map entries in the form
14290Sstevel@tonic-gate 			 * clone:<driver> to <driver>:*.  This also allows us
14300Sstevel@tonic-gate 			 * to filter out some of the litter in /etc/minor_perm.
14310Sstevel@tonic-gate 			 * Minor perm alias entries where the name is not
14320Sstevel@tonic-gate 			 * the driver kept on the clone list itself.
14330Sstevel@tonic-gate 			 * This all seems very fragile as a driver could
14340Sstevel@tonic-gate 			 * be introduced with an existing alias name.
14350Sstevel@tonic-gate 			 */
14360Sstevel@tonic-gate 			if (strcmp(name, "clone") == 0) {
14370Sstevel@tonic-gate 				minmaj = ddi_name_to_major(minor);
14380Sstevel@tonic-gate 				if (minmaj != (major_t)-1) {
14390Sstevel@tonic-gate 					if (moddebug & MODDEBUG_MINORPERM) {
14400Sstevel@tonic-gate 						cmn_err(CE_CONT,
14410Sstevel@tonic-gate 						    "mapping %s:%s to %s:*\n",
14420Sstevel@tonic-gate 						    name, minor, minor);
14430Sstevel@tonic-gate 					}
14440Sstevel@tonic-gate 					major = minmaj;
14450Sstevel@tonic-gate 					name = minor;
14460Sstevel@tonic-gate 					minor = "*";
14470Sstevel@tonic-gate 					is_clone = 1;
14480Sstevel@tonic-gate 				}
14490Sstevel@tonic-gate 			}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 			if (mp) {
14520Sstevel@tonic-gate 				mp->mp_minorname =
14530Sstevel@tonic-gate 				    i_ddi_strdup(minor, KM_SLEEP);
14540Sstevel@tonic-gate 			}
14550Sstevel@tonic-gate 		} else {
14560Sstevel@tonic-gate 			mp = NULL;
14570Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM) {
14580Sstevel@tonic-gate 				cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
14590Sstevel@tonic-gate 			}
14600Sstevel@tonic-gate 		}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		/* mode */
14630Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14640Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
14650Sstevel@tonic-gate 		if (mp)
14660Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
14670Sstevel@tonic-gate 		/* uid */
14680Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14690Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
14700Sstevel@tonic-gate 		if (mp)
14710Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, &mp->mp_uid);
14720Sstevel@tonic-gate 		/* gid */
14730Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
14740Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
14750Sstevel@tonic-gate 		if (mp) {
14760Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, &mp->mp_gid);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 			if (cmd == MODREMMINORPERM) {
14790Sstevel@tonic-gate 				rem_minorperm(major, name, mp, is_clone);
14800Sstevel@tonic-gate 				free_mperm(mp);
14810Sstevel@tonic-gate 			} else {
14820Sstevel@tonic-gate 				add_minorperm(major, name, mp, is_clone);
14830Sstevel@tonic-gate 			}
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	if (cmd == MODLOADMINORPERM)
14880Sstevel@tonic-gate 		minorperm_loaded = 1;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * Reset permissions of cached dv_nodes
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	(void) devfs_reset_perm(DV_RESET_PERM);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	return (0);
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate static int
14990Sstevel@tonic-gate modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	int error;
15020Sstevel@tonic-gate 	nvlist_t *nvl;
15030Sstevel@tonic-gate 	char *buf = kmem_alloc(buflen, KM_SLEEP);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
15060Sstevel@tonic-gate 		kmem_free(buf, buflen);
15070Sstevel@tonic-gate 		return (error);
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
15110Sstevel@tonic-gate 	kmem_free(buf, buflen);
15120Sstevel@tonic-gate 	if (error)
15130Sstevel@tonic-gate 		return (error);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	error = process_minorperm(cmd, nvl);
15160Sstevel@tonic-gate 	nvlist_free(nvl);
15170Sstevel@tonic-gate 	return (error);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate struct walk_args {
15210Sstevel@tonic-gate 	char		*wa_drvname;
15220Sstevel@tonic-gate 	list_t		wa_pathlist;
15230Sstevel@tonic-gate };
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate struct path_elem {
15260Sstevel@tonic-gate 	char		*pe_dir;
15270Sstevel@tonic-gate 	char		*pe_nodename;
15280Sstevel@tonic-gate 	list_node_t	pe_node;
15290Sstevel@tonic-gate 	int		pe_dirlen;
15300Sstevel@tonic-gate };
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*ARGSUSED*/
15330Sstevel@tonic-gate static int
15340Sstevel@tonic-gate modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
15350Sstevel@tonic-gate {
15360Sstevel@tonic-gate 	struct walk_args *wargs = (struct walk_args *)arg;
15370Sstevel@tonic-gate 	struct path_elem *pe;
15380Sstevel@tonic-gate 	char *nodename;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if (strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
15410Sstevel@tonic-gate 		return (INST_WALK_CONTINUE);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
15440Sstevel@tonic-gate 	pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
15450Sstevel@tonic-gate 	pe->pe_dirlen = strlen(pe->pe_dir) + 1;
15460Sstevel@tonic-gate 	ASSERT(strrchr(pe->pe_dir, '/') != NULL);
15470Sstevel@tonic-gate 	nodename = strrchr(pe->pe_dir, '/');
15480Sstevel@tonic-gate 	*nodename++ = 0;
15490Sstevel@tonic-gate 	pe->pe_nodename = nodename;
15500Sstevel@tonic-gate 	list_insert_tail(&wargs->wa_pathlist, pe);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	return (INST_WALK_CONTINUE);
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate static int
15560Sstevel@tonic-gate modctl_remdrv_cleanup(const char *u_drvname)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate 	struct walk_args *wargs;
15590Sstevel@tonic-gate 	struct path_elem *pe;
15600Sstevel@tonic-gate 	char *drvname;
15610Sstevel@tonic-gate 	int err, rval = 0;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
15640Sstevel@tonic-gate 	if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
15650Sstevel@tonic-gate 		kmem_free(drvname, MAXMODCONFNAME);
15660Sstevel@tonic-gate 		return (err);
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	/*
15700Sstevel@tonic-gate 	 * First go through the instance database.  For each
15710Sstevel@tonic-gate 	 * instance of a device bound to the driver being
15720Sstevel@tonic-gate 	 * removed, remove any underlying devfs attribute nodes.
15730Sstevel@tonic-gate 	 *
15740Sstevel@tonic-gate 	 * This is a two-step process.  First we go through
15750Sstevel@tonic-gate 	 * the instance data itself, constructing a list of
15760Sstevel@tonic-gate 	 * the nodes discovered.  The second step is then
15770Sstevel@tonic-gate 	 * to find and remove any devfs attribute nodes
15780Sstevel@tonic-gate 	 * for the instances discovered in the first step.
15790Sstevel@tonic-gate 	 * The two-step process avoids any difficulties
15800Sstevel@tonic-gate 	 * which could arise by holding the instance data
15810Sstevel@tonic-gate 	 * lock with simultaneous devfs operations.
15820Sstevel@tonic-gate 	 */
15830Sstevel@tonic-gate 	wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	wargs->wa_drvname = drvname;
15860Sstevel@tonic-gate 	list_create(&wargs->wa_pathlist,
15870Sstevel@tonic-gate 	    sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	(void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
15920Sstevel@tonic-gate 	    pe = list_next(&wargs->wa_pathlist, pe)) {
15930Sstevel@tonic-gate 		err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
15940Sstevel@tonic-gate 			(const char *)pe->pe_nodename);
15950Sstevel@tonic-gate 		if (rval == 0)
15960Sstevel@tonic-gate 			rval = err;
15970Sstevel@tonic-gate 	}
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
16000Sstevel@tonic-gate 		list_remove(&wargs->wa_pathlist, pe);
16010Sstevel@tonic-gate 		kmem_free(pe->pe_dir, pe->pe_dirlen);
16020Sstevel@tonic-gate 		kmem_free(pe, sizeof (*pe));
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 	kmem_free(wargs, sizeof (*wargs));
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	/*
16070Sstevel@tonic-gate 	 * Pseudo nodes aren't recorded in the instance database
16080Sstevel@tonic-gate 	 * so any such nodes need to be handled separately.
16090Sstevel@tonic-gate 	 */
16100Sstevel@tonic-gate 	err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
16110Sstevel@tonic-gate 	if (rval == 0)
16120Sstevel@tonic-gate 		rval = err;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	kmem_free(drvname, MAXMODCONFNAME);
16150Sstevel@tonic-gate 	return (rval);
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate static int
16190Sstevel@tonic-gate modctl_allocpriv(const char *name)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate 	char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
16220Sstevel@tonic-gate 	int error;
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
16250Sstevel@tonic-gate 		kmem_free(pstr, PRIVNAME_MAX);
16260Sstevel@tonic-gate 		return (error);
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate 	error = priv_getbyname(pstr, PRIV_ALLOC);
16290Sstevel@tonic-gate 	if (error < 0)
16300Sstevel@tonic-gate 		error = -error;
16310Sstevel@tonic-gate 	else
16320Sstevel@tonic-gate 		error = 0;
16330Sstevel@tonic-gate 	kmem_free(pstr, PRIVNAME_MAX);
16340Sstevel@tonic-gate 	return (error);
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate /*ARGSUSED5*/
16380Sstevel@tonic-gate int
16390Sstevel@tonic-gate modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
16400Sstevel@tonic-gate     uintptr_t a5)
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	int	error = EINVAL;
16430Sstevel@tonic-gate 	dev_t	dev;
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	if (secpolicy_modctl(CRED(), cmd) != 0)
16460Sstevel@tonic-gate 		return (set_errno(EPERM));
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	switch (cmd) {
16490Sstevel@tonic-gate 	case MODLOAD:		/* load a module */
16500Sstevel@tonic-gate 		error = modctl_modload((int)a1, (char *)a2, (int *)a3);
16510Sstevel@tonic-gate 		break;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	case MODUNLOAD:		/* unload a module */
16540Sstevel@tonic-gate 		error = modctl_modunload((modid_t)a1);
16550Sstevel@tonic-gate 		break;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	case MODINFO:		/* get module status */
16580Sstevel@tonic-gate 		error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
16590Sstevel@tonic-gate 		break;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	case MODRESERVED:	/* get last major number in range */
16620Sstevel@tonic-gate 		error = modctl_modreserve((modid_t)a1, (int *)a2);
16630Sstevel@tonic-gate 		break;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	case MODSETMINIROOT:	/* we are running in miniroot */
16660Sstevel@tonic-gate 		isminiroot = 1;
16670Sstevel@tonic-gate 		error = 0;
16680Sstevel@tonic-gate 		break;
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	case MODADDMAJBIND:	/* read major binding file */
16710Sstevel@tonic-gate 		error = modctl_add_major((int *)a2);
16720Sstevel@tonic-gate 		break;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	case MODGETPATHLEN:	/* get modpath length */
16750Sstevel@tonic-gate 		error = modctl_getmodpathlen((int *)a2);
16760Sstevel@tonic-gate 		break;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	case MODGETPATH:	/* get modpath */
16790Sstevel@tonic-gate 		error = modctl_getmodpath((char *)a2);
16800Sstevel@tonic-gate 		break;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	case MODREADSYSBIND:	/* read system call binding file */
16830Sstevel@tonic-gate 		error = modctl_read_sysbinding_file();
16840Sstevel@tonic-gate 		break;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	case MODGETMAJBIND:	/* get major number for named device */
16870Sstevel@tonic-gate 		error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
16880Sstevel@tonic-gate 		break;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	case MODGETNAME:	/* get name of device given major number */
16910Sstevel@tonic-gate 		error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
16920Sstevel@tonic-gate 		break;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	case MODDEVT2INSTANCE:
16950Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
16960Sstevel@tonic-gate 			dev = (dev_t)a1;
16970Sstevel@tonic-gate 		}
16980Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16990Sstevel@tonic-gate 		else {
17000Sstevel@tonic-gate 			dev = expldev(a1);
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate #endif
17030Sstevel@tonic-gate 		error = modctl_devt2instance(dev, (int *)a2);
17040Sstevel@tonic-gate 		break;
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	case MODSIZEOF_DEVID:	/* sizeof device id of device given dev_t */
17070Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17080Sstevel@tonic-gate 			dev = (dev_t)a1;
17090Sstevel@tonic-gate 		}
17100Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17110Sstevel@tonic-gate 		else {
17120Sstevel@tonic-gate 			dev = expldev(a1);
17130Sstevel@tonic-gate 		}
17140Sstevel@tonic-gate #endif
17150Sstevel@tonic-gate 		error = modctl_sizeof_devid(dev, (uint_t *)a2);
17160Sstevel@tonic-gate 		break;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	case MODGETDEVID:	/* get device id of device given dev_t */
17190Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17200Sstevel@tonic-gate 			dev = (dev_t)a1;
17210Sstevel@tonic-gate 		}
17220Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17230Sstevel@tonic-gate 		else {
17240Sstevel@tonic-gate 			dev = expldev(a1);
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate #endif
17270Sstevel@tonic-gate 		error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
17280Sstevel@tonic-gate 		break;
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	case MODSIZEOF_MINORNAME:	/* sizeof minor nm of dev_t/spectype */
17310Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17320Sstevel@tonic-gate 			error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
17330Sstevel@tonic-gate 			    (uint_t *)a3);
17340Sstevel@tonic-gate 		}
17350Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17360Sstevel@tonic-gate 		else {
17370Sstevel@tonic-gate 			error = modctl_sizeof_minorname(expldev(a1), (int)a2,
17380Sstevel@tonic-gate 			    (uint_t *)a3);
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate #endif
17420Sstevel@tonic-gate 		break;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	case MODGETMINORNAME:   /* get minor name of dev_t and spec type */
17450Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17460Sstevel@tonic-gate 			error = modctl_get_minorname((dev_t)a1, (int)a2,
17470Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
17480Sstevel@tonic-gate 		}
17490Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17500Sstevel@tonic-gate 		else {
17510Sstevel@tonic-gate 			error = modctl_get_minorname(expldev(a1), (int)a2,
17520Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
17530Sstevel@tonic-gate 		}
17540Sstevel@tonic-gate #endif
17550Sstevel@tonic-gate 		break;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	case MODGETDEVFSPATH_LEN:	/* sizeof path nm of dev_t/spectype */
17580Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17590Sstevel@tonic-gate 			error = modctl_devfspath_len((dev_t)a1, (int)a2,
17600Sstevel@tonic-gate 			    (uint_t *)a3);
17610Sstevel@tonic-gate 		}
17620Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17630Sstevel@tonic-gate 		else {
17640Sstevel@tonic-gate 			error = modctl_devfspath_len(expldev(a1), (int)a2,
17650Sstevel@tonic-gate 			    (uint_t *)a3);
17660Sstevel@tonic-gate 		}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate #endif
17690Sstevel@tonic-gate 		break;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	case MODGETDEVFSPATH:   /* get path name of dev_t and spec type */
17720Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
17730Sstevel@tonic-gate 			error = modctl_devfspath((dev_t)a1, (int)a2,
17740Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17770Sstevel@tonic-gate 		else {
17780Sstevel@tonic-gate 			error = modctl_devfspath(expldev(a1), (int)a2,
17790Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
17800Sstevel@tonic-gate 		}
17810Sstevel@tonic-gate #endif
17820Sstevel@tonic-gate 		break;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	case MODEVENTS:
17860Sstevel@tonic-gate 		error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
17870Sstevel@tonic-gate 		break;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	case MODGETFBNAME:	/* get the framebuffer name */
17900Sstevel@tonic-gate 		error = modctl_get_fbname((char *)a1);
17910Sstevel@tonic-gate 		break;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	case MODREREADDACF:	/* reread dacf rule database from given file */
17940Sstevel@tonic-gate 		error = modctl_reread_dacf((char *)a1);
17950Sstevel@tonic-gate 		break;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	case MODLOADDRVCONF:	/* load driver.conf file for major */
17980Sstevel@tonic-gate 		error = modctl_load_drvconf((major_t)a1);
17990Sstevel@tonic-gate 		break;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	case MODUNLOADDRVCONF:	/* unload driver.conf file for major */
18020Sstevel@tonic-gate 		error = modctl_unload_drvconf((major_t)a1);
18030Sstevel@tonic-gate 		break;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	case MODREMMAJBIND:	/* remove a major binding */
18060Sstevel@tonic-gate 		error = modctl_rem_major((major_t)a1);
18070Sstevel@tonic-gate 		break;
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	case MODDEVID2PATHS:	/* get paths given devid */
18100Sstevel@tonic-gate 		error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
18110Sstevel@tonic-gate 		    (uint_t)a3, (size_t *)a4, (char *)a5);
18120Sstevel@tonic-gate 		break;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	case MODSETDEVPOLICY:	/* establish device policy */
18150Sstevel@tonic-gate 		error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
18160Sstevel@tonic-gate 		break;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	case MODGETDEVPOLICY:	/* get device policy */
18190Sstevel@tonic-gate 		error = devpolicy_get((int *)a1, (size_t)a2,
18200Sstevel@tonic-gate 				(devplcysys_t *)a3);
18210Sstevel@tonic-gate 		break;
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	case MODALLOCPRIV:
18240Sstevel@tonic-gate 		error = modctl_allocpriv((const char *)a1);
18250Sstevel@tonic-gate 		break;
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
18280Sstevel@tonic-gate 		error = devpolicy_getbyname((size_t)a1,
18290Sstevel@tonic-gate 		    (devplcysys_t *)a2, (char *)a3);
18300Sstevel@tonic-gate 		break;
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	case MODCLEANUP:
18330Sstevel@tonic-gate 		e_devid_cache_cleanup();
18340Sstevel@tonic-gate 		error = 0;
18350Sstevel@tonic-gate 		break;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	case MODLOADMINORPERM:
18380Sstevel@tonic-gate 	case MODADDMINORPERM:
18390Sstevel@tonic-gate 	case MODREMMINORPERM:
18400Sstevel@tonic-gate 		error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
18410Sstevel@tonic-gate 		break;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	case MODREMDRVCLEANUP:
18440Sstevel@tonic-gate 		error = modctl_remdrv_cleanup((const char *)a1);
18450Sstevel@tonic-gate 		break;
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	default:
18480Sstevel@tonic-gate 		error = EINVAL;
18490Sstevel@tonic-gate 		break;
18500Sstevel@tonic-gate 	}
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
18530Sstevel@tonic-gate }
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate /*
18560Sstevel@tonic-gate  * Calls to kobj_load_module()() are handled off to this routine in a
18570Sstevel@tonic-gate  * separate thread.
18580Sstevel@tonic-gate  */
18590Sstevel@tonic-gate static void
18600Sstevel@tonic-gate modload_thread(struct loadmt *ltp)
18610Sstevel@tonic-gate {
18620Sstevel@tonic-gate 	/* load the module and signal the creator of this thread */
18630Sstevel@tonic-gate 	kmutex_t	cpr_lk;
18640Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
18670Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
18680Sstevel@tonic-gate 	/* borrow the devi lock from thread which invoked us */
18690Sstevel@tonic-gate 	pm_borrow_lock(ltp->owner);
18700Sstevel@tonic-gate 	ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
18710Sstevel@tonic-gate 	pm_return_lock();
18720Sstevel@tonic-gate 	sema_v(&ltp->sema);
18730Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
18740Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
18750Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
18760Sstevel@tonic-gate 	thread_exit();
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate  * load a module, adding a reference if caller specifies rmodp.  If rmodp
18810Sstevel@tonic-gate  * is specified then an errno is returned, otherwise a module index is
18820Sstevel@tonic-gate  * returned (-1 on error).
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate static int
18850Sstevel@tonic-gate modrload(char *subdir, char *filename, struct modctl **rmodp)
18860Sstevel@tonic-gate {
18870Sstevel@tonic-gate 	struct modctl *modp;
18880Sstevel@tonic-gate 	size_t size;
18890Sstevel@tonic-gate 	char *fullname;
18900Sstevel@tonic-gate 	int retval = EINVAL;
18910Sstevel@tonic-gate 	int id = -1;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	if (rmodp)
18940Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	if (subdir != NULL) {
18970Sstevel@tonic-gate 		/*
18980Sstevel@tonic-gate 		 * refuse / in filename to prevent "../" escapes.
18990Sstevel@tonic-gate 		 */
19000Sstevel@tonic-gate 		if (strchr(filename, '/') != NULL)
19010Sstevel@tonic-gate 			return (rmodp ? retval : id);
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 		/*
19040Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
19050Sstevel@tonic-gate 		 */
19060Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
19070Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
19080Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
19090Sstevel@tonic-gate 	} else {
19100Sstevel@tonic-gate 		fullname = filename;
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	modp = mod_hold_installed_mod(fullname, 1, &retval);
19140Sstevel@tonic-gate 	if (modp != NULL) {
19150Sstevel@tonic-gate 		id = modp->mod_id;
19160Sstevel@tonic-gate 		if (rmodp) {
19170Sstevel@tonic-gate 			/* add mod_ref and return *rmodp */
19180Sstevel@tonic-gate 			mutex_enter(&mod_lock);
19190Sstevel@tonic-gate 			modp->mod_ref++;
19200Sstevel@tonic-gate 			mutex_exit(&mod_lock);
19210Sstevel@tonic-gate 			*rmodp = modp;
19220Sstevel@tonic-gate 		}
19230Sstevel@tonic-gate 		mod_release_mod(modp);
19240Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, modload, 1);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate done:	if (subdir != NULL)
19280Sstevel@tonic-gate 		kmem_free(fullname, size);
19290Sstevel@tonic-gate 	return (rmodp ? retval : id);
19300Sstevel@tonic-gate }
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate /*
19330Sstevel@tonic-gate  * This is the primary kernel interface to load a module. It loads and
19340Sstevel@tonic-gate  * installs the named module.  It does not hold mod_ref of the module, so
19350Sstevel@tonic-gate  * a module unload attempt can occur at any time - it is up to the
19360Sstevel@tonic-gate  * _fini/mod_remove implementation to determine if unload will succeed.
19370Sstevel@tonic-gate  */
19380Sstevel@tonic-gate int
19390Sstevel@tonic-gate modload(char *subdir, char *filename)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	return (modrload(subdir, filename, NULL));
19420Sstevel@tonic-gate }
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate /*
19450Sstevel@tonic-gate  * Load a module.
19460Sstevel@tonic-gate  */
19470Sstevel@tonic-gate int
19480Sstevel@tonic-gate modloadonly(char *subdir, char *filename)
19490Sstevel@tonic-gate {
19500Sstevel@tonic-gate 	struct modctl *modp;
19510Sstevel@tonic-gate 	char *fullname;
19520Sstevel@tonic-gate 	size_t size;
19530Sstevel@tonic-gate 	int id, retval;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	if (subdir != NULL) {
19560Sstevel@tonic-gate 		/*
19570Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
19580Sstevel@tonic-gate 		 */
19590Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
19600Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
19610Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
19620Sstevel@tonic-gate 	} else {
19630Sstevel@tonic-gate 		fullname = filename;
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	modp = mod_hold_loaded_mod(NULL, fullname, &retval);
19670Sstevel@tonic-gate 	if (modp) {
19680Sstevel@tonic-gate 		id = modp->mod_id;
19690Sstevel@tonic-gate 		mod_release_mod(modp);
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	if (subdir != NULL)
19730Sstevel@tonic-gate 		kmem_free(fullname, size);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	if (retval == 0)
19760Sstevel@tonic-gate 		return (id);
19770Sstevel@tonic-gate 	return (-1);
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate /*
19810Sstevel@tonic-gate  * Try to uninstall and unload a module, removing a reference if caller
19820Sstevel@tonic-gate  * specifies rmodp.
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate static int
19850Sstevel@tonic-gate modunrload(modid_t id, struct modctl **rmodp, int unload)
19860Sstevel@tonic-gate {
19870Sstevel@tonic-gate 	struct modctl	*modp;
19880Sstevel@tonic-gate 	int		retval;
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	if (rmodp)
19910Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
19940Sstevel@tonic-gate 		return (EINVAL);
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	if (rmodp) {
19970Sstevel@tonic-gate 		mutex_enter(&mod_lock);
19980Sstevel@tonic-gate 		modp->mod_ref--;
19990Sstevel@tonic-gate 		mutex_exit(&mod_lock);
20000Sstevel@tonic-gate 		*rmodp = modp;
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	if (unload) {
20040Sstevel@tonic-gate 		retval = moduninstall(modp);
20050Sstevel@tonic-gate 		if (retval == 0) {
20060Sstevel@tonic-gate 			mod_unload(modp);
20070Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
20080Sstevel@tonic-gate 		} else if (retval == EALREADY)
20090Sstevel@tonic-gate 			retval = 0;	/* already unloaded, not an error */
20100Sstevel@tonic-gate 	} else
20110Sstevel@tonic-gate 		retval = 0;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	mod_release_mod(modp);
20140Sstevel@tonic-gate 	return (retval);
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate /*
20180Sstevel@tonic-gate  * Uninstall and unload a module.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate int
20210Sstevel@tonic-gate modunload(modid_t id)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	return (modunrload(id, NULL, 1));
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * Return status of a loaded module.
20280Sstevel@tonic-gate  */
20290Sstevel@tonic-gate static int
20300Sstevel@tonic-gate modinfo(modid_t id, struct modinfo *modinfop)
20310Sstevel@tonic-gate {
20320Sstevel@tonic-gate 	struct modctl	*modp;
20330Sstevel@tonic-gate 	modid_t		mid;
20340Sstevel@tonic-gate 	int		i;
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	mid = modinfop->mi_id;
20370Sstevel@tonic-gate 	if (modinfop->mi_info & MI_INFO_ALL) {
20380Sstevel@tonic-gate 		while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
20390Sstevel@tonic-gate 			if ((modinfop->mi_info & MI_INFO_CNT) ||
20400Sstevel@tonic-gate 			    modp->mod_installed)
20410Sstevel@tonic-gate 				break;
20420Sstevel@tonic-gate 			mod_release_mod(modp);
20430Sstevel@tonic-gate 		}
20440Sstevel@tonic-gate 		if (modp == NULL)
20450Sstevel@tonic-gate 			return (EINVAL);
20460Sstevel@tonic-gate 	} else {
20470Sstevel@tonic-gate 		modp = mod_hold_by_id(id);
20480Sstevel@tonic-gate 		if (modp == NULL)
20490Sstevel@tonic-gate 			return (EINVAL);
20500Sstevel@tonic-gate 		if (!(modinfop->mi_info & MI_INFO_CNT) &&
20510Sstevel@tonic-gate 		    (modp->mod_installed == 0)) {
20520Sstevel@tonic-gate 			mod_release_mod(modp);
20530Sstevel@tonic-gate 			return (EINVAL);
20540Sstevel@tonic-gate 		}
20550Sstevel@tonic-gate 	}
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	modinfop->mi_rev = 0;
20580Sstevel@tonic-gate 	modinfop->mi_state = 0;
20590Sstevel@tonic-gate 	for (i = 0; i < MODMAXLINK; i++) {
20600Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_p0 = -1;
20610Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 	if (modp->mod_loaded) {
20640Sstevel@tonic-gate 		modinfop->mi_state = MI_LOADED;
20650Sstevel@tonic-gate 		kobj_getmodinfo(modp->mod_mp, modinfop);
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate 	if (modp->mod_installed) {
20680Sstevel@tonic-gate 		modinfop->mi_state |= MI_INSTALLED;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 		(void) mod_getinfo(modp, modinfop);
20710Sstevel@tonic-gate 	}
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	modinfop->mi_id = modp->mod_id;
20740Sstevel@tonic-gate 	modinfop->mi_loadcnt = modp->mod_loadcnt;
20750Sstevel@tonic-gate 	(void) strcpy(modinfop->mi_name, modp->mod_modname);
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	mod_release_mod(modp);
20780Sstevel@tonic-gate 	return (0);
20790Sstevel@tonic-gate }
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
20820Sstevel@tonic-gate static char no_err[] = "No error function for weak stub %s";
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate /*
20850Sstevel@tonic-gate  * used by the stubs themselves to load and hold a module.
20860Sstevel@tonic-gate  * Returns  0 if the module is successfully held;
20870Sstevel@tonic-gate  *	    the stub needs to call mod_release_stub().
20880Sstevel@tonic-gate  *	    -1 if the stub should just call the err_fcn.
20890Sstevel@tonic-gate  * Note that this code is stretched out so that we avoid subroutine calls
20900Sstevel@tonic-gate  * and optimize for the most likely case.  That is, the case where the
20910Sstevel@tonic-gate  * module is loaded and installed and not held.  In that case we just inc
20920Sstevel@tonic-gate  * the mod_ref count and continue.
20930Sstevel@tonic-gate  */
20940Sstevel@tonic-gate int
20950Sstevel@tonic-gate mod_hold_stub(struct mod_stub_info *stub)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate 	struct modctl *mp;
20980Sstevel@tonic-gate 	struct mod_modinfo *mip;
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	mip = stub->mods_modinfo;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	mutex_enter(&mod_lock);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	/* we do mod_hold_by_modctl inline for speed */
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate mod_check_again:
21070Sstevel@tonic-gate 	if ((mp = mip->mp) != NULL) {
21080Sstevel@tonic-gate 		if (mp->mod_busy == 0) {
21090Sstevel@tonic-gate 			if (mp->mod_installed) {
21100Sstevel@tonic-gate 				/* increment the reference count */
21110Sstevel@tonic-gate 				mp->mod_ref++;
21120Sstevel@tonic-gate 				ASSERT(mp->mod_ref && mp->mod_installed);
21130Sstevel@tonic-gate 				mutex_exit(&mod_lock);
21140Sstevel@tonic-gate 				return (0);
21150Sstevel@tonic-gate 			} else {
21160Sstevel@tonic-gate 				mp->mod_busy = 1;
21170Sstevel@tonic-gate 				mp->mod_inprogress_thread =
21180Sstevel@tonic-gate 				    (curthread == NULL ?
21190Sstevel@tonic-gate 				    (kthread_id_t)-1 : curthread);
21200Sstevel@tonic-gate 			}
21210Sstevel@tonic-gate 		} else {
21220Sstevel@tonic-gate 			/*
21230Sstevel@tonic-gate 			 * wait one time and then go see if someone
21240Sstevel@tonic-gate 			 * else has resolved the stub (set mip->mp).
21250Sstevel@tonic-gate 			 */
21260Sstevel@tonic-gate 			if (mod_hold_by_modctl(mp,
21270Sstevel@tonic-gate 			    MOD_WAIT_ONCE | MOD_LOCK_HELD))
21280Sstevel@tonic-gate 				goto mod_check_again;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 			/*
21310Sstevel@tonic-gate 			 * what we have now may have been unloaded!, in
21320Sstevel@tonic-gate 			 * that case, mip->mp will be NULL, we'll hit this
21330Sstevel@tonic-gate 			 * module and load again..
21340Sstevel@tonic-gate 			 */
21350Sstevel@tonic-gate 			cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 		mutex_exit(&mod_lock);
21380Sstevel@tonic-gate 	} else {
21390Sstevel@tonic-gate 		/* first time we've hit this module */
21400Sstevel@tonic-gate 		mutex_exit(&mod_lock);
21410Sstevel@tonic-gate 		mp = mod_hold_by_name(mip->modm_module_name);
21420Sstevel@tonic-gate 		mip->mp = mp;
21430Sstevel@tonic-gate 	}
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	/*
21460Sstevel@tonic-gate 	 * If we are here, it means that the following conditions
21470Sstevel@tonic-gate 	 * are satisfied.
21480Sstevel@tonic-gate 	 *
21490Sstevel@tonic-gate 	 * mip->mp != NULL
21500Sstevel@tonic-gate 	 * this thread has set the mp->mod_busy = 1
21510Sstevel@tonic-gate 	 * mp->mod_installed = 0
21520Sstevel@tonic-gate 	 *
21530Sstevel@tonic-gate 	 */
21540Sstevel@tonic-gate 	ASSERT(mp != NULL);
21550Sstevel@tonic-gate 	ASSERT(mp->mod_busy == 1);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	if (mp->mod_installed == 0) {
21580Sstevel@tonic-gate 		/* Module not loaded, if weak stub don't load it */
21590Sstevel@tonic-gate 		if (stub->mods_flag & MODS_WEAK) {
21600Sstevel@tonic-gate 			if (stub->mods_errfcn == NULL) {
21610Sstevel@tonic-gate 				mod_release_mod(mp);
21620Sstevel@tonic-gate 				cmn_err(CE_PANIC, no_err,
21630Sstevel@tonic-gate 				    mip->modm_module_name);
21640Sstevel@tonic-gate 			}
21650Sstevel@tonic-gate 		} else {
21660Sstevel@tonic-gate 			/* Not a weak stub so load the module */
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 			if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
21690Sstevel@tonic-gate 				/*
21700Sstevel@tonic-gate 				 * If mod_load() was successful
21710Sstevel@tonic-gate 				 * and modinstall() failed, then
21720Sstevel@tonic-gate 				 * unload the module.
21730Sstevel@tonic-gate 				 */
21740Sstevel@tonic-gate 				if (mp->mod_loaded)
21750Sstevel@tonic-gate 					mod_unload(mp);
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 				mod_release_mod(mp);
21780Sstevel@tonic-gate 				if (stub->mods_errfcn == NULL) {
21790Sstevel@tonic-gate 					cmn_err(CE_PANIC, mod_stub_err,
21800Sstevel@tonic-gate 					    mip->modm_module_name);
21810Sstevel@tonic-gate 				} else {
21820Sstevel@tonic-gate 					return (-1);
21830Sstevel@tonic-gate 				}
21840Sstevel@tonic-gate 			}
21850Sstevel@tonic-gate 		}
21860Sstevel@tonic-gate 	}
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	/*
21890Sstevel@tonic-gate 	 * At this point module is held and loaded. Release
21900Sstevel@tonic-gate 	 * the mod_busy and mod_inprogress_thread before
21910Sstevel@tonic-gate 	 * returning. We actually call mod_release() here so
21920Sstevel@tonic-gate 	 * that if another stub wants to access this module,
21930Sstevel@tonic-gate 	 * it can do so. mod_ref is incremented before mod_release()
21940Sstevel@tonic-gate 	 * is called to prevent someone else from snatching the
21950Sstevel@tonic-gate 	 * module from this thread.
21960Sstevel@tonic-gate 	 */
21970Sstevel@tonic-gate 	mutex_enter(&mod_lock);
21980Sstevel@tonic-gate 	mp->mod_ref++;
21990Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
22000Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
22010Sstevel@tonic-gate 	mod_release(mp);
22020Sstevel@tonic-gate 	mutex_exit(&mod_lock);
22030Sstevel@tonic-gate 	return (0);
22040Sstevel@tonic-gate }
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate void
22070Sstevel@tonic-gate mod_release_stub(struct mod_stub_info *stub)
22080Sstevel@tonic-gate {
22090Sstevel@tonic-gate 	struct modctl *mp = stub->mods_modinfo->mp;
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	/* inline mod_release_mod */
22120Sstevel@tonic-gate 	mutex_enter(&mod_lock);
22130Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
22140Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
22150Sstevel@tonic-gate 	mp->mod_ref--;
22160Sstevel@tonic-gate 	if (mp->mod_want) {
22170Sstevel@tonic-gate 		mp->mod_want = 0;
22180Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
22190Sstevel@tonic-gate 	}
22200Sstevel@tonic-gate 	mutex_exit(&mod_lock);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate static struct modctl *
22240Sstevel@tonic-gate mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
22250Sstevel@tonic-gate {
22260Sstevel@tonic-gate 	struct modctl *modp;
22270Sstevel@tonic-gate 	int retval;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	/*
22300Sstevel@tonic-gate 	 * Hold the module.
22310Sstevel@tonic-gate 	 */
22320Sstevel@tonic-gate 	modp = mod_hold_by_name_requisite(dep, filename);
22330Sstevel@tonic-gate 	if (modp) {
22340Sstevel@tonic-gate 		retval = mod_load(modp, 1);
22350Sstevel@tonic-gate 		if (retval != 0) {
22360Sstevel@tonic-gate 			mod_release_mod(modp);
22370Sstevel@tonic-gate 			modp = NULL;
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 		*status = retval;
22400Sstevel@tonic-gate 	} else {
22410Sstevel@tonic-gate 		*status = ENOSPC;
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	/*
22450Sstevel@tonic-gate 	 * if dep is not NULL, clear the module dependency information.
22460Sstevel@tonic-gate 	 * This information is set in mod_hold_by_name_common().
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	if (dep != NULL && dep->mod_requisite_loading != NULL) {
22490Sstevel@tonic-gate 		ASSERT(dep->mod_busy);
22500Sstevel@tonic-gate 		dep->mod_requisite_loading = NULL;
22510Sstevel@tonic-gate 	}
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	return (modp);
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate /*
22570Sstevel@tonic-gate  * hold, load, and install the named module
22580Sstevel@tonic-gate  */
22590Sstevel@tonic-gate static struct modctl *
22600Sstevel@tonic-gate mod_hold_installed_mod(char *name, int usepath, int *r)
22610Sstevel@tonic-gate {
22620Sstevel@tonic-gate 	struct modctl *modp;
22630Sstevel@tonic-gate 	int retval;
2264*169Scth 	struct _buf *file;
2265*169Scth 
2266*169Scth 	/*
2267*169Scth 	 * Verify that that module in question actually exists on disk
2268*169Scth 	 * before allocation of module structure by mod_hold_by_name.
2269*169Scth 	 */
2270*169Scth 	if (modrootloaded && swaploaded) {
2271*169Scth 		file = kobj_open_path(name, usepath, 1);
2272*169Scth #ifdef	MODDIR_SUFFIX
2273*169Scth 		if (file == (struct _buf *)-1)
2274*169Scth 			file = kobj_open_path(name, usepath, 0);
2275*169Scth #endif	/* MODDIR_SUFFIX */
2276*169Scth 		if (file == (struct _buf *)-1) {
2277*169Scth 			*r = ENOENT;
2278*169Scth 			return (NULL);
2279*169Scth 		}
2280*169Scth 		kobj_close_file(file);
2281*169Scth 	}
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	/*
22840Sstevel@tonic-gate 	 * Hold the module.
22850Sstevel@tonic-gate 	 */
22860Sstevel@tonic-gate 	modp = mod_hold_by_name(name);
22870Sstevel@tonic-gate 	if (modp) {
22880Sstevel@tonic-gate 		retval = mod_load(modp, usepath);
22890Sstevel@tonic-gate 		if (retval != 0) {
22900Sstevel@tonic-gate 			mod_release_mod(modp);
22910Sstevel@tonic-gate 			modp = NULL;
22920Sstevel@tonic-gate 			*r = retval;
22930Sstevel@tonic-gate 		} else {
22940Sstevel@tonic-gate 			if ((*r = modinstall(modp)) != 0) {
22950Sstevel@tonic-gate 				/*
22960Sstevel@tonic-gate 				 * We loaded it, but failed to _init() it.
22970Sstevel@tonic-gate 				 * Be kind to developers -- force it
22980Sstevel@tonic-gate 				 * out of memory now so that the next
22990Sstevel@tonic-gate 				 * attempt to use the module will cause
23000Sstevel@tonic-gate 				 * a reload.  See 1093793.
23010Sstevel@tonic-gate 				 */
23020Sstevel@tonic-gate 				mod_unload(modp);
23030Sstevel@tonic-gate 				mod_release_mod(modp);
23040Sstevel@tonic-gate 				modp = NULL;
23050Sstevel@tonic-gate 			}
23060Sstevel@tonic-gate 		}
23070Sstevel@tonic-gate 	} else {
23080Sstevel@tonic-gate 		*r = ENOSPC;
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 	return (modp);
23110Sstevel@tonic-gate }
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate static char mod_excl_msg[] =
23140Sstevel@tonic-gate 	"module %s(%s) is EXCLUDED and will not be loaded\n";
23150Sstevel@tonic-gate static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate /*
23180Sstevel@tonic-gate  * This routine is needed for dependencies.  Users specify dependencies
23190Sstevel@tonic-gate  * by declaring a character array initialized to filenames of dependents.
23200Sstevel@tonic-gate  * So the code that handles dependents deals with filenames (and not
23210Sstevel@tonic-gate  * module names) because that's all it has.  We load by filename and once
23220Sstevel@tonic-gate  * we've loaded a file we can get the module name.
23230Sstevel@tonic-gate  * Unfortunately there isn't a single unified filename/modulename namespace.
23240Sstevel@tonic-gate  * C'est la vie.
23250Sstevel@tonic-gate  *
23260Sstevel@tonic-gate  * We allow the name being looked up to be prepended by an optional
23270Sstevel@tonic-gate  * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
23280Sstevel@tonic-gate  */
23290Sstevel@tonic-gate struct modctl *
23300Sstevel@tonic-gate mod_find_by_filename(char *subdir, char *filename)
23310Sstevel@tonic-gate {
23320Sstevel@tonic-gate 	struct modctl	*mp;
23330Sstevel@tonic-gate 	size_t		sublen;
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
23360Sstevel@tonic-gate 	if (subdir != NULL)
23370Sstevel@tonic-gate 		sublen = strlen(subdir);
23380Sstevel@tonic-gate 	else
23390Sstevel@tonic-gate 		sublen = 0;
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	mutex_enter(&mod_lock);
23420Sstevel@tonic-gate 	mp = &modules;
23430Sstevel@tonic-gate 	do {
23440Sstevel@tonic-gate 		if (sublen) {
23450Sstevel@tonic-gate 			char *mod_filename = mp->mod_filename;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 			if (strncmp(subdir, mod_filename, sublen) == 0 &&
23480Sstevel@tonic-gate 			    mod_filename[sublen] == '/' &&
23490Sstevel@tonic-gate 			    strcmp(filename, &mod_filename[sublen + 1]) == 0) {
23500Sstevel@tonic-gate 				mutex_exit(&mod_lock);
23510Sstevel@tonic-gate 				return (mp);
23520Sstevel@tonic-gate 			}
23530Sstevel@tonic-gate 		} else if (strcmp(filename, mp->mod_filename) == 0) {
23540Sstevel@tonic-gate 			mutex_exit(&mod_lock);
23550Sstevel@tonic-gate 			return (mp);
23560Sstevel@tonic-gate 		}
23570Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
23580Sstevel@tonic-gate 	mutex_exit(&mod_lock);
23590Sstevel@tonic-gate 	return (NULL);
23600Sstevel@tonic-gate }
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate /*
23630Sstevel@tonic-gate  * Check for circular dependencies.  This is called from do_dependents()
23640Sstevel@tonic-gate  * in kobj.c.  If we are the thread already loading this module, then
23650Sstevel@tonic-gate  * we're trying to load a dependent that we're already loading which
23660Sstevel@tonic-gate  * means the user specified circular dependencies.
23670Sstevel@tonic-gate  */
23680Sstevel@tonic-gate static int
23690Sstevel@tonic-gate mod_circdep(struct modctl *modp)
23700Sstevel@tonic-gate {
23710Sstevel@tonic-gate 	struct modctl	*rmod;
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	/*
23760Sstevel@tonic-gate 	 * Check the mod_inprogress_thread first.
23770Sstevel@tonic-gate 	 * mod_inprogress_thread is used in mod_hold_stub()
23780Sstevel@tonic-gate 	 * directly to improve performance.
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	if (modp->mod_inprogress_thread == curthread)
23810Sstevel@tonic-gate 		return (1);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	/*
23840Sstevel@tonic-gate 	 * Check the module circular dependencies.
23850Sstevel@tonic-gate 	 */
23860Sstevel@tonic-gate 	for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
23870Sstevel@tonic-gate 		/*
23880Sstevel@tonic-gate 		 * Check if there is a module circular dependency.
23890Sstevel@tonic-gate 		 */
23900Sstevel@tonic-gate 		if (rmod->mod_requisite_loading == modp)
23910Sstevel@tonic-gate 			return (1);
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 	return (0);
23940Sstevel@tonic-gate }
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate static int
23970Sstevel@tonic-gate mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
23980Sstevel@tonic-gate {
23990Sstevel@tonic-gate 	int (*func)(struct modinfo *);
24000Sstevel@tonic-gate 	int retval;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	/* primary modules don't do getinfo */
24050Sstevel@tonic-gate 	if (modp->mod_prim)
24060Sstevel@tonic-gate 		return (0);
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
24110Sstevel@tonic-gate 		cmn_err(CE_WARN, "_info() not defined properly in %s",
24120Sstevel@tonic-gate 		    modp->mod_filename);
24130Sstevel@tonic-gate 		/*
24140Sstevel@tonic-gate 		 * The semantics of mod_info(9F) are that 0 is failure
24150Sstevel@tonic-gate 		 * and non-zero is success.
24160Sstevel@tonic-gate 		 */
24170Sstevel@tonic-gate 		retval = 0;
24180Sstevel@tonic-gate 	} else
24190Sstevel@tonic-gate 		retval = (*func)(modinfop);	/* call _info() function */
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
24220Sstevel@tonic-gate 		printf("Returned from _info, retval = %x\n", retval);
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	return (retval);
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate static void
24280Sstevel@tonic-gate modadd(struct modctl *mp)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	mp->mod_id = last_module_id++;
24330Sstevel@tonic-gate 	mp->mod_next = &modules;
24340Sstevel@tonic-gate 	mp->mod_prev = modules.mod_prev;
24350Sstevel@tonic-gate 	modules.mod_prev->mod_next = mp;
24360Sstevel@tonic-gate 	modules.mod_prev = mp;
24370Sstevel@tonic-gate }
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate /*ARGSUSED*/
24400Sstevel@tonic-gate static struct modctl *
24410Sstevel@tonic-gate allocate_modp(char *filename, char *modname)
24420Sstevel@tonic-gate {
24430Sstevel@tonic-gate 	struct modctl *mp;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
24460Sstevel@tonic-gate 	mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
24470Sstevel@tonic-gate 	(void) strcpy(mp->mod_modname, modname);
24480Sstevel@tonic-gate 	return (mp);
24490Sstevel@tonic-gate }
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate /*
24520Sstevel@tonic-gate  * Get the value of a symbol.  This is a wrapper routine that
24530Sstevel@tonic-gate  * calls kobj_getsymvalue().  kobj_getsymvalue() may go away but this
24540Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
24550Sstevel@tonic-gate  */
24560Sstevel@tonic-gate uintptr_t
24570Sstevel@tonic-gate modgetsymvalue(char *name, int kernelonly)
24580Sstevel@tonic-gate {
24590Sstevel@tonic-gate 	return (kobj_getsymvalue(name, kernelonly));
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate /*
24630Sstevel@tonic-gate  * Get the symbol nearest an address.  This is a wrapper routine that
24640Sstevel@tonic-gate  * calls kobj_getsymname().  kobj_getsymname() may go away but this
24650Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
24660Sstevel@tonic-gate  */
24670Sstevel@tonic-gate char *
24680Sstevel@tonic-gate modgetsymname(uintptr_t value, ulong_t *offset)
24690Sstevel@tonic-gate {
24700Sstevel@tonic-gate 	return (kobj_getsymname(value, offset));
24710Sstevel@tonic-gate }
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate /*
24740Sstevel@tonic-gate  * Lookup a symbol in a specified module.  This is a wrapper routine that
24750Sstevel@tonic-gate  * calls kobj_lookup().  kobj_lookup() may go away but this
24760Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
24770Sstevel@tonic-gate  */
24780Sstevel@tonic-gate uintptr_t
24790Sstevel@tonic-gate modlookup(char *modname, char *symname)
24800Sstevel@tonic-gate {
24810Sstevel@tonic-gate 	struct modctl *modp;
24820Sstevel@tonic-gate 	uintptr_t val;
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	if ((modp = mod_hold_by_name(modname)) == NULL)
24850Sstevel@tonic-gate 		return (0);
24860Sstevel@tonic-gate 	val = kobj_lookup(modp->mod_mp, symname);
24870Sstevel@tonic-gate 	mod_release_mod(modp);
24880Sstevel@tonic-gate 	return (val);
24890Sstevel@tonic-gate }
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate /*
24920Sstevel@tonic-gate  * Ask the user for the name of the system file and the default path
24930Sstevel@tonic-gate  * for modules.
24940Sstevel@tonic-gate  */
24950Sstevel@tonic-gate void
24960Sstevel@tonic-gate mod_askparams()
24970Sstevel@tonic-gate {
24980Sstevel@tonic-gate 	static char s0[64];
24990Sstevel@tonic-gate 	intptr_t fd;
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	if ((fd = kobj_open(systemfile)) != -1L)
25020Sstevel@tonic-gate 		kobj_close(fd);
25030Sstevel@tonic-gate 	else
25040Sstevel@tonic-gate 		systemfile = NULL;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
25070Sstevel@tonic-gate 	while (1) {
25080Sstevel@tonic-gate 		printf("Name of system file [%s]:  ",
25090Sstevel@tonic-gate 			systemfile ? systemfile : "/dev/null");
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 		console_gets(s0, sizeof (s0));
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 		if (s0[0] == '\0')
25140Sstevel@tonic-gate 			break;
25150Sstevel@tonic-gate 		else if (strcmp(s0, "/dev/null") == 0) {
25160Sstevel@tonic-gate 			systemfile = NULL;
25170Sstevel@tonic-gate 			break;
25180Sstevel@tonic-gate 		} else {
25190Sstevel@tonic-gate 			if ((fd = kobj_open(s0)) != -1L) {
25200Sstevel@tonic-gate 				kobj_close(fd);
25210Sstevel@tonic-gate 				systemfile = s0;
25220Sstevel@tonic-gate 				break;
25230Sstevel@tonic-gate 			}
25240Sstevel@tonic-gate 		}
25250Sstevel@tonic-gate 		printf("can't find file %s\n", s0);
25260Sstevel@tonic-gate 	}
25270Sstevel@tonic-gate }
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate static char loading_msg[] = "loading '%s' id %d\n";
25300Sstevel@tonic-gate static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate /*
25330Sstevel@tonic-gate  * Common code for loading a module (but not installing it).
25340Sstevel@tonic-gate  * Handoff the task of module loading to a seperate thread
25350Sstevel@tonic-gate  * with a large stack if possible, since this code may recurse a few times.
25360Sstevel@tonic-gate  * Return zero if there are no errors or an errno value.
25370Sstevel@tonic-gate  */
25380Sstevel@tonic-gate static int
25390Sstevel@tonic-gate mod_load(struct modctl *mp, int usepath)
25400Sstevel@tonic-gate {
25410Sstevel@tonic-gate 	int		retval;
25420Sstevel@tonic-gate 	struct modinfo	*modinfop = NULL;
25430Sstevel@tonic-gate 	struct loadmt	lt;
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
25460Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	if (mp->mod_loaded)
25490Sstevel@tonic-gate 		return (0);
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
25520Sstevel@tonic-gate 	    mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
25530Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
25540Sstevel@tonic-gate 			printf(mod_excl_msg, mp->mod_filename,
25550Sstevel@tonic-gate 				mp->mod_modname);
25560Sstevel@tonic-gate 		}
25570Sstevel@tonic-gate 		return (ENXIO);
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
25600Sstevel@tonic-gate 		printf(loading_msg, mp->mod_filename, mp->mod_id);
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	if (curthread != &t0) {
25630Sstevel@tonic-gate 		lt.mp = mp;
25640Sstevel@tonic-gate 		lt.usepath = usepath;
25650Sstevel@tonic-gate 		lt.owner = curthread;
25660Sstevel@tonic-gate 		sema_init(&lt.sema, 0, NULL, SEMA_DEFAULT, NULL);
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 		/* create thread to hand of call to */
25690Sstevel@tonic-gate 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
25700Sstevel@tonic-gate 		    modload_thread, &lt, 0, &p0, TS_RUN, maxclsyspri);
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 		/* wait for thread to complete kobj_load_module */
25730Sstevel@tonic-gate 		sema_p(&lt.sema);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		sema_destroy(&lt.sema);
25760Sstevel@tonic-gate 		retval = lt.retval;
25770Sstevel@tonic-gate 	} else
25780Sstevel@tonic-gate 		retval = kobj_load_module(mp, usepath);
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	if (mp->mod_mp) {
25810Sstevel@tonic-gate 		ASSERT(retval == 0);
25820Sstevel@tonic-gate 		mp->mod_loaded = 1;
25830Sstevel@tonic-gate 		mp->mod_loadcnt++;
25840Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
25850Sstevel@tonic-gate 			printf(load_msg, mp->mod_filename, mp->mod_id,
25860Sstevel@tonic-gate 				(void *)((struct module *)mp->mod_mp)->text,
25870Sstevel@tonic-gate 				(void *)((struct module *)mp->mod_mp)->data,
25880Sstevel@tonic-gate 				((struct module *)mp->mod_mp)->text_size,
25890Sstevel@tonic-gate 				((struct module *)mp->mod_mp)->data_size);
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 		/*
25930Sstevel@tonic-gate 		 * XXX - There should be a better way to get this.
25940Sstevel@tonic-gate 		 */
25950Sstevel@tonic-gate 		modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
25960Sstevel@tonic-gate 		modinfop->mi_info = MI_INFO_LINKAGE;
25970Sstevel@tonic-gate 		if (mod_getinfo(mp, modinfop) == 0)
25980Sstevel@tonic-gate 			mp->mod_linkage = NULL;
25990Sstevel@tonic-gate 		else {
26000Sstevel@tonic-gate 			mp->mod_linkage = (void *)modinfop->mi_base;
26010Sstevel@tonic-gate 			ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 		/*
26050Sstevel@tonic-gate 		 * DCS: bootstrapping code. If the driver is loaded
26060Sstevel@tonic-gate 		 * before root mount, it is assumed that the driver
26070Sstevel@tonic-gate 		 * may be used before mounting root. In order to
26080Sstevel@tonic-gate 		 * access mappings of global to local minor no.'s
26090Sstevel@tonic-gate 		 * during installation/open of the driver, we load
26100Sstevel@tonic-gate 		 * them into memory here while the BOP_interfaces
26110Sstevel@tonic-gate 		 * are still up.
26120Sstevel@tonic-gate 		 */
26130Sstevel@tonic-gate 		if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
26140Sstevel@tonic-gate 			retval = clboot_modload(mp);
26150Sstevel@tonic-gate 		}
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 		kmem_free(modinfop, sizeof (struct modinfo));
26180Sstevel@tonic-gate 		(void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
26190Sstevel@tonic-gate 		retval = install_stubs_by_name(mp, mp->mod_modname);
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 		/*
26220Sstevel@tonic-gate 		 * Now that the module is loaded, we need to give DTrace
26230Sstevel@tonic-gate 		 * a chance to notify its providers.  This is done via
26240Sstevel@tonic-gate 		 * the dtrace_modload function pointer.
26250Sstevel@tonic-gate 		 */
26260Sstevel@tonic-gate 		if (strcmp(mp->mod_modname, "dtrace") != 0) {
26270Sstevel@tonic-gate 			struct modctl *dmp = mod_hold_by_name("dtrace");
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 			if (dmp != NULL && dtrace_modload != NULL)
26300Sstevel@tonic-gate 				(*dtrace_modload)(mp);
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 			mod_release_mod(dmp);
26330Sstevel@tonic-gate 		}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	} else {
26360Sstevel@tonic-gate 		/*
26370Sstevel@tonic-gate 		 * If load failed then we need to release any requisites
26380Sstevel@tonic-gate 		 * that we had established.
26390Sstevel@tonic-gate 		 */
26400Sstevel@tonic-gate 		ASSERT(retval);
26410Sstevel@tonic-gate 		mod_release_requisites(mp);
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 		if (moddebug & MODDEBUG_ERRMSG)
26440Sstevel@tonic-gate 			printf("error loading '%s', error %d\n",
26450Sstevel@tonic-gate 			    mp->mod_filename, retval);
26460Sstevel@tonic-gate 	}
26470Sstevel@tonic-gate 	return (retval);
26480Sstevel@tonic-gate }
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate static void
26530Sstevel@tonic-gate mod_unload(struct modctl *mp)
26540Sstevel@tonic-gate {
26550Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
26560Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
26570Sstevel@tonic-gate 	ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
26580Sstevel@tonic-gate 	    ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
26610Sstevel@tonic-gate 		printf(unload_msg, mp->mod_modname,
26620Sstevel@tonic-gate 			mp->mod_id, mp->mod_loadcnt);
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 	/*
26650Sstevel@tonic-gate 	 * If mod_ref is not zero, it means some modules might still refer
26660Sstevel@tonic-gate 	 * to this module. Then you can't unload this module right now.
26670Sstevel@tonic-gate 	 * Instead, set 1 to mod_delay_unload to notify the system of
26680Sstevel@tonic-gate 	 * unloading this module later when it's not required any more.
26690Sstevel@tonic-gate 	 */
26700Sstevel@tonic-gate 	if (mp->mod_ref > 0) {
26710Sstevel@tonic-gate 		mp->mod_delay_unload = 1;
26720Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
26730Sstevel@tonic-gate 			printf("module %s not unloaded,"
26740Sstevel@tonic-gate 			    " non-zero reference count (%d)",
26750Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_ref);
26760Sstevel@tonic-gate 		}
26770Sstevel@tonic-gate 		return;
26780Sstevel@tonic-gate 	}
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	if (((mp->mod_loaded == 0) || mp->mod_installed) ||
26810Sstevel@tonic-gate 	    (mp->mod_ref || mp->mod_prim)) {
26820Sstevel@tonic-gate 		/*
26830Sstevel@tonic-gate 		 * A DEBUG kernel would ASSERT panic above, the code is broken
26840Sstevel@tonic-gate 		 * if we get this warning.
26850Sstevel@tonic-gate 		 */
26860Sstevel@tonic-gate 		cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
26870Sstevel@tonic-gate 		    mp->mod_filename, mp->mod_installed, mp->mod_loaded,
26880Sstevel@tonic-gate 		    mp->mod_ref);
26890Sstevel@tonic-gate 		return;
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	/* reset stub functions to call the binder again */
26930Sstevel@tonic-gate 	reset_stubs(mp);
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	/*
26960Sstevel@tonic-gate 	 * mark module as unloaded before the modctl structure is freed.
26970Sstevel@tonic-gate 	 * This is required not to reuse the modctl structure before
26980Sstevel@tonic-gate 	 * the module is marked as unloaded.
26990Sstevel@tonic-gate 	 */
27000Sstevel@tonic-gate 	mp->mod_loaded = 0;
27010Sstevel@tonic-gate 	mp->mod_linkage = NULL;
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 	/* free the memory */
27040Sstevel@tonic-gate 	kobj_unload_module(mp);
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	if (mp->mod_delay_unload) {
27070Sstevel@tonic-gate 		mp->mod_delay_unload = 0;
27080Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
27090Sstevel@tonic-gate 			printf("deferred unload of module %s"
27100Sstevel@tonic-gate 			    " (id %d) successful",
27110Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_id);
27120Sstevel@tonic-gate 		}
27130Sstevel@tonic-gate 	}
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	/* release hold on requisites */
27160Sstevel@tonic-gate 	mod_release_requisites(mp);
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	/*
27190Sstevel@tonic-gate 	 * Now that the module is gone, we need to give DTrace a chance to
27200Sstevel@tonic-gate 	 * remove any probes that it may have had in the module.  This is
27210Sstevel@tonic-gate 	 * done via the dtrace_modunload function pointer.
27220Sstevel@tonic-gate 	 */
27230Sstevel@tonic-gate 	if (strcmp(mp->mod_modname, "dtrace") != 0) {
27240Sstevel@tonic-gate 		struct modctl *dmp = mod_hold_by_name("dtrace");
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 		if (dmp != NULL && dtrace_modunload != NULL)
27270Sstevel@tonic-gate 			(*dtrace_modunload)(mp);
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 		mod_release_mod(dmp);
27300Sstevel@tonic-gate 	}
27310Sstevel@tonic-gate }
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate static int
27340Sstevel@tonic-gate modinstall(struct modctl *mp)
27350Sstevel@tonic-gate {
27360Sstevel@tonic-gate 	int val;
27370Sstevel@tonic-gate 	int (*func)(void);
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
27400Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	if (mp->mod_installed)
27430Sstevel@tonic-gate 		return (0);
27440Sstevel@tonic-gate 	/*
27450Sstevel@tonic-gate 	 * If mod_delay_unload is on, it means the system chose the deferred
27460Sstevel@tonic-gate 	 * unload for this module. Then you can't install this module until
27470Sstevel@tonic-gate 	 * it's unloaded from the system.
27480Sstevel@tonic-gate 	 */
27490Sstevel@tonic-gate 	if (mp->mod_delay_unload)
27500Sstevel@tonic-gate 		return (ENXIO);
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
27530Sstevel@tonic-gate 		printf("installing %s, module id %d.\n",
27540Sstevel@tonic-gate 			mp->mod_modname, mp->mod_id);
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	ASSERT(mp->mod_mp != NULL);
27570Sstevel@tonic-gate 	if (mod_install_requisites(mp) != 0) {
27580Sstevel@tonic-gate 		/*
27590Sstevel@tonic-gate 		 * Note that we can't call mod_unload(mp) here since
27600Sstevel@tonic-gate 		 * if modinstall() was called by mod_install_requisites(),
27610Sstevel@tonic-gate 		 * we won't be able to hold the dependent modules
27620Sstevel@tonic-gate 		 * (otherwise there would be a deadlock).
27630Sstevel@tonic-gate 		 */
27640Sstevel@tonic-gate 		return (ENXIO);
27650Sstevel@tonic-gate 	}
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	if (moddebug & MODDEBUG_ERRMSG) {
27680Sstevel@tonic-gate 		printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
27690Sstevel@tonic-gate 			mp->mod_filename, mp->mod_id,
27700Sstevel@tonic-gate 			(void *)((struct module *)mp->mod_mp)->text,
27710Sstevel@tonic-gate 			(void *)((struct module *)mp->mod_mp)->data,
27720Sstevel@tonic-gate 			((struct module *)mp->mod_mp)->text_size,
27730Sstevel@tonic-gate 			((struct module *)mp->mod_mp)->data_size);
27740Sstevel@tonic-gate 	}
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
27790Sstevel@tonic-gate 		cmn_err(CE_WARN, "_init() not defined properly in %s",
27800Sstevel@tonic-gate 		    mp->mod_filename);
27810Sstevel@tonic-gate 		return (EFAULT);
27820Sstevel@tonic-gate 	}
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG) {
27850Sstevel@tonic-gate 		printf("breakpoint before calling %s:_init()\n",
27860Sstevel@tonic-gate 		    mp->mod_modname);
27870Sstevel@tonic-gate 		if (DEBUGGER_PRESENT)
27880Sstevel@tonic-gate 			debug_enter("_init");
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
27920Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
27930Sstevel@tonic-gate 	val = (*func)();		/* call _init */
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
27960Sstevel@tonic-gate 		printf("Returned from _init, val = %x\n", val);
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate 	if (val == 0) {
27990Sstevel@tonic-gate 		/*
28000Sstevel@tonic-gate 		 * Set the MODS_INSTALLED flag to enable this module
28010Sstevel@tonic-gate 		 * being called now.
28020Sstevel@tonic-gate 		 */
28030Sstevel@tonic-gate 		install_stubs(mp);
28040Sstevel@tonic-gate 		mp->mod_installed = 1;
28050Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG)
28060Sstevel@tonic-gate 		printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	return (val);
28090Sstevel@tonic-gate }
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate static int
28120Sstevel@tonic-gate detach_driver(char *name)
28130Sstevel@tonic-gate {
28140Sstevel@tonic-gate 	major_t major;
28150Sstevel@tonic-gate 	int error;
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	/*
28180Sstevel@tonic-gate 	 * If being called from mod_uninstall_all() then the appropriate
28190Sstevel@tonic-gate 	 * driver detaches (leaf only) have already been done.
28200Sstevel@tonic-gate 	 */
28210Sstevel@tonic-gate 	if (mod_in_autounload())
28220Sstevel@tonic-gate 		return (0);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	major = ddi_name_to_major(name);
28250Sstevel@tonic-gate 	if (major == (major_t)-1)
28260Sstevel@tonic-gate 		return (0);
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	error = ndi_devi_unconfig_driver(ddi_root_node(),
28290Sstevel@tonic-gate 	    NDI_DETACH_DRIVER, major);
28300Sstevel@tonic-gate 	return (error == NDI_SUCCESS ? 0 : -1);
28310Sstevel@tonic-gate }
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate static int
28360Sstevel@tonic-gate moduninstall(struct modctl *mp)
28370Sstevel@tonic-gate {
28380Sstevel@tonic-gate 	int status = 0;
28390Sstevel@tonic-gate 	int (*func)(void);
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
28420Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	/*
28450Sstevel@tonic-gate 	 * Verify that we need to do something and can uninstall the module.
28460Sstevel@tonic-gate 	 *
28470Sstevel@tonic-gate 	 * If we should not uninstall the module or if the module is not in
28480Sstevel@tonic-gate 	 * the correct state to start an uninstall we return EBUSY to prevent
28490Sstevel@tonic-gate 	 * us from progressing to mod_unload.  If the module has already been
28500Sstevel@tonic-gate 	 * uninstalled and unloaded we return EALREADY.
28510Sstevel@tonic-gate 	 */
28520Sstevel@tonic-gate 	if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
28530Sstevel@tonic-gate 		return (EBUSY);
28540Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
28550Sstevel@tonic-gate 		return (EALREADY);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	/*
28580Sstevel@tonic-gate 	 * To avoid devinfo / module deadlock we must release this module
28590Sstevel@tonic-gate 	 * prior to initiating the detach_driver, otherwise the detach_driver
28600Sstevel@tonic-gate 	 * might deadlock on a devinfo node held by another thread
28610Sstevel@tonic-gate 	 * coming top down and involving the module we have locked.
28620Sstevel@tonic-gate 	 *
28630Sstevel@tonic-gate 	 * When we regrab the module we must reverify that it is OK
28640Sstevel@tonic-gate 	 * to proceed with the uninstall operation.
28650Sstevel@tonic-gate 	 */
28660Sstevel@tonic-gate 	mod_release_mod(mp);
28670Sstevel@tonic-gate 	status = detach_driver(mp->mod_modname);
28680Sstevel@tonic-gate 	(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 	/* check detach status and reverify state with lock */
28710Sstevel@tonic-gate 	mutex_enter(&mod_lock);
28720Sstevel@tonic-gate 	if ((status != 0) || mp->mod_prim || mp->mod_ref) {
28730Sstevel@tonic-gate 		mutex_exit(&mod_lock);
28740Sstevel@tonic-gate 		return (EBUSY);
28750Sstevel@tonic-gate 	}
28760Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
28770Sstevel@tonic-gate 		mutex_exit(&mod_lock);
28780Sstevel@tonic-gate 		return (EALREADY);
28790Sstevel@tonic-gate 	}
28800Sstevel@tonic-gate 	mutex_exit(&mod_lock);
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
28830Sstevel@tonic-gate 		printf("uninstalling %s\n", mp->mod_modname);
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 	/*
28860Sstevel@tonic-gate 	 * lookup _fini, return EBUSY if not defined.
28870Sstevel@tonic-gate 	 *
28880Sstevel@tonic-gate 	 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
28890Sstevel@tonic-gate 	 * detach(9E) - it allows bufctl addresses to be resolved.
28900Sstevel@tonic-gate 	 */
28910Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
28920Sstevel@tonic-gate 	if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
28930Sstevel@tonic-gate 	    (moddebug & MODDEBUG_FINI_EBUSY))
28940Sstevel@tonic-gate 		return (EBUSY);
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/* verify that _fini is in this module */
28970Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
28980Sstevel@tonic-gate 		cmn_err(CE_WARN, "_fini() not defined properly in %s",
28990Sstevel@tonic-gate 		    mp->mod_filename);
29000Sstevel@tonic-gate 		return (EFAULT);
29010Sstevel@tonic-gate 	}
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 	/* call _fini() */
29040Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
29050Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 	status = (*func)();
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	if (status == 0) {
29100Sstevel@tonic-gate 		/* _fini returned success, the module is no longer installed */
29110Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG)
29120Sstevel@tonic-gate 			printf("uninstalled %s\n", mp->mod_modname);
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 		/*
29150Sstevel@tonic-gate 		 * Even though we only set mod_installed to zero here, a zero
29160Sstevel@tonic-gate 		 * return value means we are commited to a code path were
29170Sstevel@tonic-gate 		 * mod_loaded will also end up as zero - we have no other
29180Sstevel@tonic-gate 		 * way to get the module data and bss back to the pre _init
29190Sstevel@tonic-gate 		 * state except a reload. To ensure this, after return,
29200Sstevel@tonic-gate 		 * mod_busy must stay set until mod_loaded is cleared.
29210Sstevel@tonic-gate 		 */
29220Sstevel@tonic-gate 		mp->mod_installed = 0;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 		/*
29250Sstevel@tonic-gate 		 * Clear the MODS_INSTALLED flag not to call functions
29260Sstevel@tonic-gate 		 * in the module directly from now on.
29270Sstevel@tonic-gate 		 */
29280Sstevel@tonic-gate 		uninstall_stubs(mp);
29290Sstevel@tonic-gate 	} else {
29300Sstevel@tonic-gate 		if (moddebug & MODDEBUG_USERDEBUG)
29310Sstevel@tonic-gate 			printf(finiret_msg, mp->mod_filename, status);
29320Sstevel@tonic-gate 		/*
29330Sstevel@tonic-gate 		 * By definition _fini is only allowed to return EBUSY or the
29340Sstevel@tonic-gate 		 * result of mod_remove (EBUSY or EINVAL).  In the off chance
29350Sstevel@tonic-gate 		 * that a driver returns EALREADY we convert this to EINVAL
29360Sstevel@tonic-gate 		 * since to our caller EALREADY means module was already
29370Sstevel@tonic-gate 		 * removed.
29380Sstevel@tonic-gate 		 */
29390Sstevel@tonic-gate 		if (status == EALREADY)
29400Sstevel@tonic-gate 			status = EINVAL;
29410Sstevel@tonic-gate 	}
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	return (status);
29440Sstevel@tonic-gate }
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate /*
29470Sstevel@tonic-gate  * Uninstall all modules.
29480Sstevel@tonic-gate  */
29490Sstevel@tonic-gate static void
29500Sstevel@tonic-gate mod_uninstall_all(void)
29510Sstevel@tonic-gate {
29520Sstevel@tonic-gate 	struct modctl	*mp;
29530Sstevel@tonic-gate 	modid_t		modid = 0;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	/* mark this thread as doing autounloading */
29560Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, (void *)1);
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	(void) devfs_clean(ddi_root_node(), NULL, 0);
29590Sstevel@tonic-gate 	(void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	while ((mp = mod_hold_next_by_id(modid)) != NULL) {
29620Sstevel@tonic-gate 		modid = mp->mod_id;
29630Sstevel@tonic-gate 		/*
29640Sstevel@tonic-gate 		 * Skip modules with the MOD_NOAUTOUNLOAD flag set
29650Sstevel@tonic-gate 		 */
29660Sstevel@tonic-gate 		if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
29670Sstevel@tonic-gate 			mod_release_mod(mp);
29680Sstevel@tonic-gate 			continue;
29690Sstevel@tonic-gate 		}
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 		if (moduninstall(mp) == 0)
29720Sstevel@tonic-gate 			mod_unload(mp);
29730Sstevel@tonic-gate 		mod_release_mod(mp);
29740Sstevel@tonic-gate 	}
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, NULL);
29770Sstevel@tonic-gate }
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate static int modunload_disable_count;
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate void
29820Sstevel@tonic-gate modunload_disable(void)
29830Sstevel@tonic-gate {
29840Sstevel@tonic-gate 	INCR_COUNT(&modunload_disable_count, &mod_uninstall_lock);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate void
29880Sstevel@tonic-gate modunload_enable(void)
29890Sstevel@tonic-gate {
29900Sstevel@tonic-gate 	DECR_COUNT(&modunload_disable_count, &mod_uninstall_lock);
29910Sstevel@tonic-gate }
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate void
29940Sstevel@tonic-gate mod_uninstall_daemon(void)
29950Sstevel@tonic-gate {
29960Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
29970Sstevel@tonic-gate 	clock_t		ticks = 0;
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	mod_aul_thread = curthread;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
30020Sstevel@tonic-gate 	for (;;) {
30030Sstevel@tonic-gate 		mutex_enter(&mod_uninstall_lock);
30040Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
30050Sstevel@tonic-gate 		/*
30060Sstevel@tonic-gate 		 * In DEBUG kernels, unheld drivers are uninstalled periodically
30070Sstevel@tonic-gate 		 * every mod_uninstall_interval seconds.  Periodic uninstall can
30080Sstevel@tonic-gate 		 * be disabled by setting mod_uninstall_interval to 0 which is
30090Sstevel@tonic-gate 		 * the default for a non-DEBUG kernel.
30100Sstevel@tonic-gate 		 */
30110Sstevel@tonic-gate 		if (mod_uninstall_interval) {
30120Sstevel@tonic-gate 			ticks = ddi_get_lbolt() +
30130Sstevel@tonic-gate 				drv_usectohz(mod_uninstall_interval * 1000000);
30140Sstevel@tonic-gate 			(void) cv_timedwait(&mod_uninstall_cv,
30150Sstevel@tonic-gate 				&mod_uninstall_lock, ticks);
30160Sstevel@tonic-gate 		} else {
30170Sstevel@tonic-gate 			cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
30180Sstevel@tonic-gate 		}
30190Sstevel@tonic-gate 		/*
30200Sstevel@tonic-gate 		 * The whole daemon is safe for CPR except we don't want
30210Sstevel@tonic-gate 		 * the daemon to run if FREEZE is issued and this daemon
30220Sstevel@tonic-gate 		 * wakes up from the cv_wait above. In this case, it'll be
30230Sstevel@tonic-gate 		 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
30240Sstevel@tonic-gate 		 *
30250Sstevel@tonic-gate 		 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
30260Sstevel@tonic-gate 		 * mod_uninstall_lock is used to protect cprinfo and
30270Sstevel@tonic-gate 		 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
30280Sstevel@tonic-gate 		 * called.
30290Sstevel@tonic-gate 		 */
30300Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
30310Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
30320Sstevel@tonic-gate 		mutex_exit(&mod_uninstall_lock);
30330Sstevel@tonic-gate 		if ((modunload_disable_count == 0) &&
30340Sstevel@tonic-gate 		    ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
30350Sstevel@tonic-gate 			mod_uninstall_all();
30360Sstevel@tonic-gate 		}
30370Sstevel@tonic-gate 	}
30380Sstevel@tonic-gate }
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate /*
30410Sstevel@tonic-gate  * Unload all uninstalled modules.
30420Sstevel@tonic-gate  */
30430Sstevel@tonic-gate void
30440Sstevel@tonic-gate modreap(void)
30450Sstevel@tonic-gate {
30460Sstevel@tonic-gate 	mutex_enter(&mod_uninstall_lock);
30470Sstevel@tonic-gate 	cv_broadcast(&mod_uninstall_cv);
30480Sstevel@tonic-gate 	mutex_exit(&mod_uninstall_lock);
30490Sstevel@tonic-gate }
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate /*
30520Sstevel@tonic-gate  * Hold the specified module. This is the module holding primitive.
30530Sstevel@tonic-gate  *
30540Sstevel@tonic-gate  * If MOD_LOCK_HELD then the caller already holds the mod_lock.
30550Sstevel@tonic-gate  *
30560Sstevel@tonic-gate  * Return values:
30570Sstevel@tonic-gate  *	 0 ==> the module is held
30580Sstevel@tonic-gate  *	 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
30590Sstevel@tonic-gate  *		to determine how to retry.
30600Sstevel@tonic-gate  */
30610Sstevel@tonic-gate int
30620Sstevel@tonic-gate mod_hold_by_modctl(struct modctl *mp, int f)
30630Sstevel@tonic-gate {
30640Sstevel@tonic-gate 	ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
30650Sstevel@tonic-gate 	    ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
30660Sstevel@tonic-gate 	    (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
30670Sstevel@tonic-gate 	ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
30680Sstevel@tonic-gate 	    ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
30690Sstevel@tonic-gate 	    (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
30700Sstevel@tonic-gate 	ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
30730Sstevel@tonic-gate 		mutex_enter(&mod_lock);
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 	while (mp->mod_busy) {
30760Sstevel@tonic-gate 		mp->mod_want = 1;
30770Sstevel@tonic-gate 		cv_wait(&mod_cv, &mod_lock);
30780Sstevel@tonic-gate 		/*
30790Sstevel@tonic-gate 		 * Module may be unloaded by daemon.
30800Sstevel@tonic-gate 		 * Nevertheless, modctl structure is still in linked list
30810Sstevel@tonic-gate 		 * (i.e., off &modules), not freed!
30820Sstevel@tonic-gate 		 * Caller is not supposed to assume "mp" is valid, but there
30830Sstevel@tonic-gate 		 * is no reasonable way to detect this but using
30840Sstevel@tonic-gate 		 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
30850Sstevel@tonic-gate 		 *   (or similar check depending on calling context)
30860Sstevel@tonic-gate 		 * DON'T free modctl structure, it will be very very
30870Sstevel@tonic-gate 		 * problematic.
30880Sstevel@tonic-gate 		 */
30890Sstevel@tonic-gate 		if (f & MOD_WAIT_ONCE) {
30900Sstevel@tonic-gate 			if (f & MOD_LOCK_NOT_HELD)
30910Sstevel@tonic-gate 				mutex_exit(&mod_lock);
30920Sstevel@tonic-gate 			return (1);	/* caller decides how to retry */
30930Sstevel@tonic-gate 		}
30940Sstevel@tonic-gate 	}
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	mp->mod_busy = 1;
30970Sstevel@tonic-gate 	mp->mod_inprogress_thread =
30980Sstevel@tonic-gate 	    (curthread == NULL ? (kthread_id_t)-1 : curthread);
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
31010Sstevel@tonic-gate 		mutex_exit(&mod_lock);
31020Sstevel@tonic-gate 	return (0);
31030Sstevel@tonic-gate }
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate static struct modctl *
31060Sstevel@tonic-gate mod_hold_by_name_common(struct modctl *dep, char *filename)
31070Sstevel@tonic-gate {
31080Sstevel@tonic-gate 	char		*modname;
31090Sstevel@tonic-gate 	struct modctl	*mp;
31100Sstevel@tonic-gate 	char		*curname, *newname;
31110Sstevel@tonic-gate 	int		found = 0;
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 	mutex_enter(&mod_lock);
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
31160Sstevel@tonic-gate 		modname = filename;
31170Sstevel@tonic-gate 	else
31180Sstevel@tonic-gate 		modname++;
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	mp = &modules;
31210Sstevel@tonic-gate 	do {
31220Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
31230Sstevel@tonic-gate 			found = 1;
31240Sstevel@tonic-gate 			break;
31250Sstevel@tonic-gate 		}
31260Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	if (found == 0) {
31290Sstevel@tonic-gate 		mp = allocate_modp(filename, modname);
31300Sstevel@tonic-gate 		modadd(mp);
31310Sstevel@tonic-gate 	}
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate 	/*
31340Sstevel@tonic-gate 	 * if dep is not NULL, set the mp in mod_requisite_loading for
31350Sstevel@tonic-gate 	 * the module circular dependency check. This field is used in
31360Sstevel@tonic-gate 	 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
31370Sstevel@tonic-gate 	 */
31380Sstevel@tonic-gate 	if (dep != NULL) {
31390Sstevel@tonic-gate 		ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
31400Sstevel@tonic-gate 		dep->mod_requisite_loading = mp;
31410Sstevel@tonic-gate 	}
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	/*
31440Sstevel@tonic-gate 	 * If the module was held, then it must be us who has it held.
31450Sstevel@tonic-gate 	 */
31460Sstevel@tonic-gate 	if (mod_circdep(mp))
31470Sstevel@tonic-gate 		mp = NULL;
31480Sstevel@tonic-gate 	else {
31490Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 		/*
31520Sstevel@tonic-gate 		 * If the name hadn't been set or has changed, allocate
31530Sstevel@tonic-gate 		 * space and set it.  Free space used by previous name.
31540Sstevel@tonic-gate 		 *
31550Sstevel@tonic-gate 		 * Do not change the name of primary modules, for primary
31560Sstevel@tonic-gate 		 * modules the mod_filename was allocated in standalone mode:
31570Sstevel@tonic-gate 		 * it is illegal to kobj_alloc in standalone mode and kobj_free
31580Sstevel@tonic-gate 		 * in non-standalone mode.
31590Sstevel@tonic-gate 		 */
31600Sstevel@tonic-gate 		curname = mp->mod_filename;
31610Sstevel@tonic-gate 		if (curname == NULL ||
31620Sstevel@tonic-gate 		    ((mp->mod_prim == 0) &&
31630Sstevel@tonic-gate 		    (curname != filename) &&
31640Sstevel@tonic-gate 		    (modname != filename) &&
31650Sstevel@tonic-gate 		    (strcmp(curname, filename) != 0))) {
31660Sstevel@tonic-gate 			newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
31670Sstevel@tonic-gate 			(void) strcpy(newname, filename);
31680Sstevel@tonic-gate 			mp->mod_filename = newname;
31690Sstevel@tonic-gate 			if (curname != NULL)
31700Sstevel@tonic-gate 				kobj_free(curname, strlen(curname) + 1);
31710Sstevel@tonic-gate 		}
31720Sstevel@tonic-gate 	}
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 	mutex_exit(&mod_lock);
31750Sstevel@tonic-gate 	if (mp && moddebug & MODDEBUG_LOADMSG2)
31760Sstevel@tonic-gate 		printf("Holding %s\n", mp->mod_filename);
31770Sstevel@tonic-gate 	if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
31780Sstevel@tonic-gate 		printf("circular dependency loading %s\n", filename);
31790Sstevel@tonic-gate 	return (mp);
31800Sstevel@tonic-gate }
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate static struct modctl *
31830Sstevel@tonic-gate mod_hold_by_name_requisite(struct modctl *dep, char *filename)
31840Sstevel@tonic-gate {
31850Sstevel@tonic-gate 	return (mod_hold_by_name_common(dep, filename));
31860Sstevel@tonic-gate }
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate struct modctl *
31890Sstevel@tonic-gate mod_hold_by_name(char *filename)
31900Sstevel@tonic-gate {
31910Sstevel@tonic-gate 	return (mod_hold_by_name_common(NULL, filename));
31920Sstevel@tonic-gate }
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate static struct modctl *
31950Sstevel@tonic-gate mod_hold_by_id(modid_t modid)
31960Sstevel@tonic-gate {
31970Sstevel@tonic-gate 	struct modctl	*mp;
31980Sstevel@tonic-gate 	int		found = 0;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 	mutex_enter(&mod_lock);
32010Sstevel@tonic-gate 	mp = &modules;
32020Sstevel@tonic-gate 	do {
32030Sstevel@tonic-gate 		if (mp->mod_id == modid) {
32040Sstevel@tonic-gate 			found = 1;
32050Sstevel@tonic-gate 			break;
32060Sstevel@tonic-gate 		}
32070Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
32100Sstevel@tonic-gate 		mp = NULL;
32110Sstevel@tonic-gate 	else
32120Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	mutex_exit(&mod_lock);
32150Sstevel@tonic-gate 	return (mp);
32160Sstevel@tonic-gate }
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate static struct modctl *
32190Sstevel@tonic-gate mod_hold_next_by_id(modid_t modid)
32200Sstevel@tonic-gate {
32210Sstevel@tonic-gate 	struct modctl	*mp;
32220Sstevel@tonic-gate 	int		found = 0;
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	if (modid < -1)
32250Sstevel@tonic-gate 		return (NULL);
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 	mutex_enter(&mod_lock);
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 	mp = &modules;
32300Sstevel@tonic-gate 	do {
32310Sstevel@tonic-gate 		if (mp->mod_id > modid) {
32320Sstevel@tonic-gate 			found = 1;
32330Sstevel@tonic-gate 			break;
32340Sstevel@tonic-gate 		}
32350Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
32380Sstevel@tonic-gate 		mp = NULL;
32390Sstevel@tonic-gate 	else
32400Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	mutex_exit(&mod_lock);
32430Sstevel@tonic-gate 	return (mp);
32440Sstevel@tonic-gate }
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate static void
32470Sstevel@tonic-gate mod_release(struct modctl *mp)
32480Sstevel@tonic-gate {
32490Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
32500Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	mp->mod_busy = 0;
32530Sstevel@tonic-gate 	mp->mod_inprogress_thread = NULL;
32540Sstevel@tonic-gate 	if (mp->mod_want) {
32550Sstevel@tonic-gate 		mp->mod_want = 0;
32560Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate }
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate void
32610Sstevel@tonic-gate mod_release_mod(struct modctl *mp)
32620Sstevel@tonic-gate {
32630Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
32640Sstevel@tonic-gate 		printf("Releasing %s\n", mp->mod_filename);
32650Sstevel@tonic-gate 	mutex_enter(&mod_lock);
32660Sstevel@tonic-gate 	mod_release(mp);
32670Sstevel@tonic-gate 	mutex_exit(&mod_lock);
32680Sstevel@tonic-gate }
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate modid_t
32710Sstevel@tonic-gate mod_name_to_modid(char *filename)
32720Sstevel@tonic-gate {
32730Sstevel@tonic-gate 	char		*modname;
32740Sstevel@tonic-gate 	struct modctl	*mp;
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	mutex_enter(&mod_lock);
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
32790Sstevel@tonic-gate 		modname = filename;
32800Sstevel@tonic-gate 	else
32810Sstevel@tonic-gate 		modname++;
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 	mp = &modules;
32840Sstevel@tonic-gate 	do {
32850Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
32860Sstevel@tonic-gate 			mutex_exit(&mod_lock);
32870Sstevel@tonic-gate 			return (mp->mod_id);
32880Sstevel@tonic-gate 		}
32890Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	mutex_exit(&mod_lock);
32920Sstevel@tonic-gate 	return (-1);
32930Sstevel@tonic-gate }
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate int
32970Sstevel@tonic-gate mod_remove_by_name(char *name)
32980Sstevel@tonic-gate {
32990Sstevel@tonic-gate 	struct modctl *mp;
33000Sstevel@tonic-gate 	int retval;
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 	mp = mod_hold_by_name(name);
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 	if (mp == NULL)
33050Sstevel@tonic-gate 		return (EINVAL);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
33080Sstevel@tonic-gate 		/*
33090Sstevel@tonic-gate 		 * Do not unload forceloaded modules
33100Sstevel@tonic-gate 		 */
33110Sstevel@tonic-gate 		mod_release_mod(mp);
33120Sstevel@tonic-gate 		return (0);
33130Sstevel@tonic-gate 	}
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	if ((retval = moduninstall(mp)) == 0)
33160Sstevel@tonic-gate 		mod_unload(mp);
33170Sstevel@tonic-gate 	else if (retval == EALREADY)
33180Sstevel@tonic-gate 		retval = 0;		/* already unloaded, not an error */
33190Sstevel@tonic-gate 	mod_release_mod(mp);
33200Sstevel@tonic-gate 	return (retval);
33210Sstevel@tonic-gate }
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate /*
33240Sstevel@tonic-gate  * Record that module "dep" is dependent on module "on_mod."
33250Sstevel@tonic-gate  */
33260Sstevel@tonic-gate static void
33270Sstevel@tonic-gate mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
33280Sstevel@tonic-gate {
33290Sstevel@tonic-gate 	struct modctl_list **pmlnp;	/* previous next pointer */
33300Sstevel@tonic-gate 	struct modctl_list *mlp;
33310Sstevel@tonic-gate 	struct modctl_list *new;
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	ASSERT(dependent->mod_busy && on_mod->mod_busy);
33340Sstevel@tonic-gate 	mutex_enter(&mod_lock);
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 	/*
33370Sstevel@tonic-gate 	 * Search dependent's requisite list to see if on_mod is recorded.
33380Sstevel@tonic-gate 	 * List is ordered by id.
33390Sstevel@tonic-gate 	 */
33400Sstevel@tonic-gate 	for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
33410Sstevel@tonic-gate 	    mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
33420Sstevel@tonic-gate 		if (mlp->modl_modp->mod_id >= on_mod->mod_id)
33430Sstevel@tonic-gate 			break;
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	/* Create and insert if not already recorded */
33460Sstevel@tonic-gate 	if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
33470Sstevel@tonic-gate 		new = kobj_zalloc(sizeof (*new), KM_SLEEP);
33480Sstevel@tonic-gate 		new->modl_modp = on_mod;
33490Sstevel@tonic-gate 		new->modl_next = mlp;
33500Sstevel@tonic-gate 		*pmlnp = new;
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 		/*
33530Sstevel@tonic-gate 		 * Increment the mod_ref count in our new requisite module.
33540Sstevel@tonic-gate 		 * This is what keeps a module that has other modules
33550Sstevel@tonic-gate 		 * which are dependent on it from being uninstalled and
33560Sstevel@tonic-gate 		 * unloaded. "on_mod"'s mod_ref count decremented in
33570Sstevel@tonic-gate 		 * mod_release_requisites when the "dependent" module
33580Sstevel@tonic-gate 		 * unload is complete.  "on_mod" must be loaded, but may not
33590Sstevel@tonic-gate 		 * yet be installed.
33600Sstevel@tonic-gate 		 */
33610Sstevel@tonic-gate 		on_mod->mod_ref++;
33620Sstevel@tonic-gate 		ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
33630Sstevel@tonic-gate 	}
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	mutex_exit(&mod_lock);
33660Sstevel@tonic-gate }
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate /*
33690Sstevel@tonic-gate  * release the hold associated with mod_make_requisite mod_ref++
33700Sstevel@tonic-gate  * as part of unload.
33710Sstevel@tonic-gate  */
33720Sstevel@tonic-gate void
33730Sstevel@tonic-gate mod_release_requisites(struct modctl *modp)
33740Sstevel@tonic-gate {
33750Sstevel@tonic-gate 	struct modctl_list *modl;
33760Sstevel@tonic-gate 	struct modctl_list *next;
33770Sstevel@tonic-gate 	struct modctl *req;
33780Sstevel@tonic-gate 	struct modctl_list *start = NULL, *mod_garbage;
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
33810Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	mutex_enter(&mod_lock);		/* needed for manipulation of req */
33840Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = next) {
33850Sstevel@tonic-gate 		next = modl->modl_next;
33860Sstevel@tonic-gate 		req = modl->modl_modp;
33870Sstevel@tonic-gate 		ASSERT(req->mod_ref >= 1 && req->mod_loaded);
33880Sstevel@tonic-gate 		req->mod_ref--;
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 		/*
33910Sstevel@tonic-gate 		 * Check if the module has to be unloaded or not.
33920Sstevel@tonic-gate 		 */
33930Sstevel@tonic-gate 		if (req->mod_ref == 0 && req->mod_delay_unload) {
33940Sstevel@tonic-gate 			struct modctl_list *new;
33950Sstevel@tonic-gate 			/*
33960Sstevel@tonic-gate 			 * Allocate the modclt_list holding the garbage
33970Sstevel@tonic-gate 			 * module which should be unloaded later.
33980Sstevel@tonic-gate 			 */
33990Sstevel@tonic-gate 			new = kobj_zalloc(sizeof (struct modctl_list),
34000Sstevel@tonic-gate 			    KM_SLEEP);
34010Sstevel@tonic-gate 			new->modl_modp = req;
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate 			if (start == NULL)
34040Sstevel@tonic-gate 				mod_garbage = start = new;
34050Sstevel@tonic-gate 			else {
34060Sstevel@tonic-gate 				mod_garbage->modl_next = new;
34070Sstevel@tonic-gate 				mod_garbage = new;
34080Sstevel@tonic-gate 			}
34090Sstevel@tonic-gate 		}
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 		/* free the list as we go */
34120Sstevel@tonic-gate 		kobj_free(modl, sizeof (*modl));
34130Sstevel@tonic-gate 	}
34140Sstevel@tonic-gate 	modp->mod_requisites = NULL;
34150Sstevel@tonic-gate 	mutex_exit(&mod_lock);
34160Sstevel@tonic-gate 
34170Sstevel@tonic-gate 	/*
34180Sstevel@tonic-gate 	 * Unload the garbage modules.
34190Sstevel@tonic-gate 	 */
34200Sstevel@tonic-gate 	for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
34210Sstevel@tonic-gate 		struct modctl_list *old = mod_garbage;
34220Sstevel@tonic-gate 		struct modctl *mp = mod_garbage->modl_modp;
34230Sstevel@tonic-gate 		ASSERT(mp != NULL);
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 		/*
34260Sstevel@tonic-gate 		 * Hold this module until it's unloaded completely.
34270Sstevel@tonic-gate 		 */
34280Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp,
34290Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
34300Sstevel@tonic-gate 		/*
34310Sstevel@tonic-gate 		 * Check if the module is not unloaded yet and nobody requires
34320Sstevel@tonic-gate 		 * the module. If it's unloaded already or somebody still
34330Sstevel@tonic-gate 		 * requires the module, don't unload it now.
34340Sstevel@tonic-gate 		 */
34350Sstevel@tonic-gate 		if (mp->mod_loaded && mp->mod_ref == 0)
34360Sstevel@tonic-gate 			mod_unload(mp);
34370Sstevel@tonic-gate 		ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
34380Sstevel@tonic-gate 		    (mp->mod_ref > 0));
34390Sstevel@tonic-gate 		mod_release_mod(mp);
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 		mod_garbage = mod_garbage->modl_next;
34420Sstevel@tonic-gate 		kobj_free(old, sizeof (struct modctl_list));
34430Sstevel@tonic-gate 	}
34440Sstevel@tonic-gate }
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate /*
34470Sstevel@tonic-gate  * Process dependency of the module represented by "dep" on the
34480Sstevel@tonic-gate  * module named by "on."
34490Sstevel@tonic-gate  *
34500Sstevel@tonic-gate  * Called from kobj_do_dependents() to load a module "on" on which
34510Sstevel@tonic-gate  * "dep" depends.
34520Sstevel@tonic-gate  */
34530Sstevel@tonic-gate struct modctl *
34540Sstevel@tonic-gate mod_load_requisite(struct modctl *dep, char *on)
34550Sstevel@tonic-gate {
34560Sstevel@tonic-gate 	struct modctl *on_mod;
34570Sstevel@tonic-gate 	int retval;
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
34600Sstevel@tonic-gate 		mod_make_requisite(dep, on_mod);
34610Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG) {
34620Sstevel@tonic-gate 		printf("error processing %s on which module %s depends\n",
34630Sstevel@tonic-gate 			on, dep->mod_modname);
34640Sstevel@tonic-gate 	}
34650Sstevel@tonic-gate 	return (on_mod);
34660Sstevel@tonic-gate }
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate static int
34690Sstevel@tonic-gate mod_install_requisites(struct modctl *modp)
34700Sstevel@tonic-gate {
34710Sstevel@tonic-gate 	struct modctl_list *modl;
34720Sstevel@tonic-gate 	struct modctl *req;
34730Sstevel@tonic-gate 	int status = 0;
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
34760Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
34790Sstevel@tonic-gate 		req = modl->modl_modp;
34800Sstevel@tonic-gate 		(void) mod_hold_by_modctl(req,
34810Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
34820Sstevel@tonic-gate 		status = modinstall(req);
34830Sstevel@tonic-gate 		mod_release_mod(req);
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 		if (status != 0)
34860Sstevel@tonic-gate 			break;
34870Sstevel@tonic-gate 	}
34880Sstevel@tonic-gate 	return (status);
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate /*
34920Sstevel@tonic-gate  * returns 1 if this thread is doing autounload, 0 otherwise.
34930Sstevel@tonic-gate  * see mod_uninstall_all.
34940Sstevel@tonic-gate  */
34950Sstevel@tonic-gate int
34960Sstevel@tonic-gate mod_in_autounload()
34970Sstevel@tonic-gate {
34980Sstevel@tonic-gate 	return ((int)(uintptr_t)tsd_get(mod_autounload_key));
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate /*
35020Sstevel@tonic-gate  * gmatch adapted from libc, stripping the wchar stuff
35030Sstevel@tonic-gate  */
35040Sstevel@tonic-gate #define	popchar(p, c) \
35050Sstevel@tonic-gate 	c = *p++; \
35060Sstevel@tonic-gate 	if (c == 0) \
35070Sstevel@tonic-gate 		return (0);
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate static int
35100Sstevel@tonic-gate gmatch(const char *s, const char *p)
35110Sstevel@tonic-gate {
35120Sstevel@tonic-gate 	int c, sc;
35130Sstevel@tonic-gate 	int ok, lc, notflag;
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	sc = *s++;
35160Sstevel@tonic-gate 	c = *p++;
35170Sstevel@tonic-gate 	if (c == 0)
35180Sstevel@tonic-gate 		return (sc == c);	/* nothing matches nothing */
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 	switch (c) {
35210Sstevel@tonic-gate 	case '\\':
35220Sstevel@tonic-gate 		/* skip to quoted character */
35230Sstevel@tonic-gate 		popchar(p, c)
35240Sstevel@tonic-gate 		/*FALLTHRU*/
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 	default:
35270Sstevel@tonic-gate 		/* straight comparison */
35280Sstevel@tonic-gate 		if (c != sc)
35290Sstevel@tonic-gate 			return (0);
35300Sstevel@tonic-gate 		/*FALLTHRU*/
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	case '?':
35330Sstevel@tonic-gate 		/* first char matches, move to remainder */
35340Sstevel@tonic-gate 		return (sc != '\0' ? gmatch(s, p) : 0);
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 
35370Sstevel@tonic-gate 	case '*':
35380Sstevel@tonic-gate 		while (*p == '*')
35390Sstevel@tonic-gate 			p++;
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 		/* * matches everything */
35420Sstevel@tonic-gate 		if (*p == 0)
35430Sstevel@tonic-gate 			return (1);
35440Sstevel@tonic-gate 
35450Sstevel@tonic-gate 		/* undo skip at the beginning & iterate over substrings */
35460Sstevel@tonic-gate 		--s;
35470Sstevel@tonic-gate 		while (*s) {
35480Sstevel@tonic-gate 			if (gmatch(s, p))
35490Sstevel@tonic-gate 				return (1);
35500Sstevel@tonic-gate 			s++;
35510Sstevel@tonic-gate 		}
35520Sstevel@tonic-gate 		return (0);
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 	case '[':
35550Sstevel@tonic-gate 		/* match any char within [] */
35560Sstevel@tonic-gate 		if (sc == 0)
35570Sstevel@tonic-gate 			return (0);
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 		ok = lc = notflag = 0;
35600Sstevel@tonic-gate 
35610Sstevel@tonic-gate 		if (*p == '!') {
35620Sstevel@tonic-gate 			notflag = 1;
35630Sstevel@tonic-gate 			p++;
35640Sstevel@tonic-gate 		}
35650Sstevel@tonic-gate 		popchar(p, c)
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 		do {
35680Sstevel@tonic-gate 			if (c == '-' && lc && *p != ']') {
35690Sstevel@tonic-gate 				/* test sc against range [c1-c2] */
35700Sstevel@tonic-gate 				popchar(p, c)
35710Sstevel@tonic-gate 				if (c == '\\') {
35720Sstevel@tonic-gate 					popchar(p, c)
35730Sstevel@tonic-gate 				}
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 				if (notflag) {
35760Sstevel@tonic-gate 					/* return 0 on mismatch */
35770Sstevel@tonic-gate 					if (lc <= sc && sc <= c)
35780Sstevel@tonic-gate 						return (0);
35790Sstevel@tonic-gate 					ok++;
35800Sstevel@tonic-gate 				} else if (lc <= sc && sc <= c) {
35810Sstevel@tonic-gate 					ok++;
35820Sstevel@tonic-gate 				}
35830Sstevel@tonic-gate 				/* keep going, may get a match next */
35840Sstevel@tonic-gate 			} else if (c == '\\') {
35850Sstevel@tonic-gate 				/* skip to quoted character */
35860Sstevel@tonic-gate 				popchar(p, c)
35870Sstevel@tonic-gate 			}
35880Sstevel@tonic-gate 			lc = c;
35890Sstevel@tonic-gate 			if (notflag) {
35900Sstevel@tonic-gate 				if (sc == lc)
35910Sstevel@tonic-gate 					return (0);
35920Sstevel@tonic-gate 				ok++;
35930Sstevel@tonic-gate 			} else if (sc == lc) {
35940Sstevel@tonic-gate 				ok++;
35950Sstevel@tonic-gate 			}
35960Sstevel@tonic-gate 			popchar(p, c)
35970Sstevel@tonic-gate 		} while (c != ']');
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 		/* recurse on remainder of string */
36000Sstevel@tonic-gate 		return (ok ? gmatch(s, p) : 0);
36010Sstevel@tonic-gate 	}
36020Sstevel@tonic-gate 	/*NOTREACHED*/
36030Sstevel@tonic-gate }
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 
36060Sstevel@tonic-gate /*
36070Sstevel@tonic-gate  * Get default perm for device from /etc/minor_perm. Return 0 if match found.
36080Sstevel@tonic-gate  *
36090Sstevel@tonic-gate  * Pure wild-carded patterns are handled separately so the ordering of
36100Sstevel@tonic-gate  * these patterns doesn't matter.  We're still dependent on ordering
36110Sstevel@tonic-gate  * however as the first matching entry is the one returned.
36120Sstevel@tonic-gate  * Not ideal but all existing examples and usage do imply this
36130Sstevel@tonic-gate  * ordering implicitly.
36140Sstevel@tonic-gate  *
36150Sstevel@tonic-gate  * Drivers using the clone driver are always good for some entertainment.
36160Sstevel@tonic-gate  * Clone nodes under pseudo have the form clone@0:<driver>.  Some minor
36170Sstevel@tonic-gate  * perm entries have the form clone:<driver>, others use <driver>:*
36180Sstevel@tonic-gate  * Examples are clone:llc1 vs. llc2:*, for example.
36190Sstevel@tonic-gate  *
36200Sstevel@tonic-gate  * Minor perms in the clone:<driver> form are mapped to the drivers's
36210Sstevel@tonic-gate  * mperm list, not the clone driver, as wildcard entries for clone
36220Sstevel@tonic-gate  * reference only.  In other words, a clone wildcard will match
36230Sstevel@tonic-gate  * references for clone@0:<driver> but never <driver>@<minor>.
36240Sstevel@tonic-gate  *
36250Sstevel@tonic-gate  * Additional minor perms in the standard form are also supported,
36260Sstevel@tonic-gate  * for mixed usage, ie a node with an entry clone:<driver> could
36270Sstevel@tonic-gate  * provide further entries <driver>:<minor>.
36280Sstevel@tonic-gate  *
36290Sstevel@tonic-gate  * Finally, some uses of clone use an alias as the minor name rather
36300Sstevel@tonic-gate  * than the driver name, with the alias as the minor perm entry.
36310Sstevel@tonic-gate  * This case is handled by attaching the driver to bring its
36320Sstevel@tonic-gate  * minor list into existence, then discover the alias via DDI_ALIAS.
36330Sstevel@tonic-gate  * The clone device's minor perm list can then be searched for
36340Sstevel@tonic-gate  * that alias.
36350Sstevel@tonic-gate  */
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate static int
36380Sstevel@tonic-gate dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
36390Sstevel@tonic-gate {
36400Sstevel@tonic-gate 	major_t major;
36410Sstevel@tonic-gate 	struct devnames *dnp;
36420Sstevel@tonic-gate 	mperm_t *mp;
36430Sstevel@tonic-gate 	char *alias = NULL;
36440Sstevel@tonic-gate 	dev_info_t *cdevi;
36450Sstevel@tonic-gate 	struct ddi_minor_data *dmd;
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 	major = ddi_name_to_major(minor_name);
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	ASSERT(dip == clone_dip);
36500Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	/*
36530Sstevel@tonic-gate 	 * Attach the driver named by the minor node, then
36540Sstevel@tonic-gate 	 * search its first instance's minor list for an
36550Sstevel@tonic-gate 	 * alias node.
36560Sstevel@tonic-gate 	 */
36570Sstevel@tonic-gate 	if (ddi_hold_installed_driver(major) == NULL)
36580Sstevel@tonic-gate 		return (1);
36590Sstevel@tonic-gate 
36600Sstevel@tonic-gate 	dnp = &devnamesp[major];
36610Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 	if ((cdevi = dnp->dn_head) != NULL) {
36640Sstevel@tonic-gate 		mutex_enter(&DEVI(cdevi)->devi_lock);
36650Sstevel@tonic-gate 		for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
36660Sstevel@tonic-gate 			if (dmd->type == DDM_ALIAS) {
36670Sstevel@tonic-gate 				alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
36680Sstevel@tonic-gate 				break;
36690Sstevel@tonic-gate 			}
36700Sstevel@tonic-gate 		}
36710Sstevel@tonic-gate 		mutex_exit(&DEVI(cdevi)->devi_lock);
36720Sstevel@tonic-gate 	}
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
36750Sstevel@tonic-gate 	ddi_rele_driver(major);
36760Sstevel@tonic-gate 
36770Sstevel@tonic-gate 	if (alias == NULL) {
36780Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
36790Sstevel@tonic-gate 			cmn_err(CE_CONT, "dev_minorperm: "
36800Sstevel@tonic-gate 			    "no alias for %s\n", minor_name);
36810Sstevel@tonic-gate 		return (1);
36820Sstevel@tonic-gate 	}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	major = ddi_driver_major(clone_dip);
36850Sstevel@tonic-gate 	dnp = &devnamesp[major];
36860Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	/*
36890Sstevel@tonic-gate 	 * Go through the clone driver's mperm list looking
36900Sstevel@tonic-gate 	 * for a match for the specified alias.
36910Sstevel@tonic-gate 	 */
36920Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
36930Sstevel@tonic-gate 		if (strcmp(alias, mp->mp_minorname) == 0) {
36940Sstevel@tonic-gate 			break;
36950Sstevel@tonic-gate 		}
36960Sstevel@tonic-gate 	}
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 	if (mp) {
36990Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
37000Sstevel@tonic-gate 			cmn_err(CE_CONT,
37010Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
37020Sstevel@tonic-gate 			    minor_name, alias, mp->mp_mode,
37030Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
37040Sstevel@tonic-gate 		}
37050Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
37060Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
37070Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
37080Sstevel@tonic-gate 	}
37090Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate 	kmem_free(alias, strlen(alias)+1);
37120Sstevel@tonic-gate 
37130Sstevel@tonic-gate 	return (mp == NULL);
37140Sstevel@tonic-gate }
37150Sstevel@tonic-gate 
37160Sstevel@tonic-gate int
37170Sstevel@tonic-gate dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
37180Sstevel@tonic-gate {
37190Sstevel@tonic-gate 	major_t major;
37200Sstevel@tonic-gate 	char *minor_name;
37210Sstevel@tonic-gate 	struct devnames *dnp;
37220Sstevel@tonic-gate 	mperm_t *mp;
37230Sstevel@tonic-gate 	int is_clone = 0;
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 	if (!minorperm_loaded) {
37260Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
37270Sstevel@tonic-gate 			cmn_err(CE_CONT,
37280Sstevel@tonic-gate 			    "%s: minor perm not yet loaded\n", name);
37290Sstevel@tonic-gate 		return (1);
37300Sstevel@tonic-gate 	}
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate 	minor_name = strchr(name, ':');
37330Sstevel@tonic-gate 	if (minor_name == NULL)
37340Sstevel@tonic-gate 		return (1);
37350Sstevel@tonic-gate 	minor_name++;
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 	/*
37380Sstevel@tonic-gate 	 * If it's the clone driver, search the driver as named
37390Sstevel@tonic-gate 	 * by the minor.  All clone minor perm entries other than
37400Sstevel@tonic-gate 	 * alias nodes are actually installed on the real driver's list.
37410Sstevel@tonic-gate 	 */
37420Sstevel@tonic-gate 	if (dip == clone_dip) {
37430Sstevel@tonic-gate 		major = ddi_name_to_major(minor_name);
37440Sstevel@tonic-gate 		if (major == (major_t)-1) {
37450Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM)
37460Sstevel@tonic-gate 				cmn_err(CE_CONT, "dev_minorperm: "
37470Sstevel@tonic-gate 				    "%s: no such driver\n", minor_name);
37480Sstevel@tonic-gate 			return (1);
37490Sstevel@tonic-gate 		}
37500Sstevel@tonic-gate 		is_clone = 1;
37510Sstevel@tonic-gate 	} else {
37520Sstevel@tonic-gate 		major = ddi_driver_major(dip);
37530Sstevel@tonic-gate 		ASSERT(major != (major_t)-1);
37540Sstevel@tonic-gate 	}
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	dnp = &devnamesp[major];
37570Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate 	/*
37600Sstevel@tonic-gate 	 * Go through the driver's mperm list looking for
37610Sstevel@tonic-gate 	 * a match for the specified minor.  If there's
37620Sstevel@tonic-gate 	 * no matching pattern, use the wild card.
37630Sstevel@tonic-gate 	 * Defer to the clone wild for clone if specified,
37640Sstevel@tonic-gate 	 * otherwise fall back to the normal form.
37650Sstevel@tonic-gate 	 */
37660Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
37670Sstevel@tonic-gate 		if (gmatch(minor_name, mp->mp_minorname) != 0) {
37680Sstevel@tonic-gate 			break;
37690Sstevel@tonic-gate 		}
37700Sstevel@tonic-gate 	}
37710Sstevel@tonic-gate 	if (mp == NULL) {
37720Sstevel@tonic-gate 		if (is_clone)
37730Sstevel@tonic-gate 			mp = dnp->dn_mperm_clone;
37740Sstevel@tonic-gate 		if (mp == NULL)
37750Sstevel@tonic-gate 			mp = dnp->dn_mperm_wild;
37760Sstevel@tonic-gate 	}
37770Sstevel@tonic-gate 
37780Sstevel@tonic-gate 	if (mp) {
37790Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
37800Sstevel@tonic-gate 			cmn_err(CE_CONT,
37810Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d\n",
37820Sstevel@tonic-gate 			    name, mp->mp_minorname, mp->mp_mode,
37830Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
37840Sstevel@tonic-gate 		}
37850Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
37860Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
37870Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
37880Sstevel@tonic-gate 	}
37890Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
37900Sstevel@tonic-gate 
37910Sstevel@tonic-gate 	/*
37920Sstevel@tonic-gate 	 * If no match can be found for a clone node,
37930Sstevel@tonic-gate 	 * search for a possible match for an alias.
37940Sstevel@tonic-gate 	 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
37950Sstevel@tonic-gate 	 * with minor perm entry clone:ptmx.
37960Sstevel@tonic-gate 	 */
37970Sstevel@tonic-gate 	if (mp == NULL && is_clone) {
37980Sstevel@tonic-gate 		return (dev_alias_minorperm(dip, minor_name, rmp));
37990Sstevel@tonic-gate 	}
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	return (mp == NULL);
38020Sstevel@tonic-gate }
38030Sstevel@tonic-gate 
38040Sstevel@tonic-gate /*
38050Sstevel@tonic-gate  * dynamicaly reference load a dl module/library, returning handle
38060Sstevel@tonic-gate  */
38070Sstevel@tonic-gate /*ARGSUSED*/
38080Sstevel@tonic-gate ddi_modhandle_t
38090Sstevel@tonic-gate ddi_modopen(const char *modname, int mode, int *errnop)
38100Sstevel@tonic-gate {
38110Sstevel@tonic-gate 	char		*subdir;
38120Sstevel@tonic-gate 	char		*mod;
38130Sstevel@tonic-gate 	int		subdirlen;
38140Sstevel@tonic-gate 	struct modctl	*hmodp = NULL;
38150Sstevel@tonic-gate 	int		retval = EINVAL;
38160Sstevel@tonic-gate 
38170Sstevel@tonic-gate 	ASSERT(modname && (mode == KRTLD_MODE_FIRST));
38180Sstevel@tonic-gate 	if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
38190Sstevel@tonic-gate 		goto out;
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate 	/* find optional first '/' in modname */
38220Sstevel@tonic-gate 	mod = strchr(modname, '/');
38230Sstevel@tonic-gate 	if (mod != strrchr(modname, '/'))
38240Sstevel@tonic-gate 		goto out;		/* only one '/' is legal */
38250Sstevel@tonic-gate 
38260Sstevel@tonic-gate 	if (mod) {
38270Sstevel@tonic-gate 		/* for subdir string without modification to argument */
38280Sstevel@tonic-gate 		mod++;
38290Sstevel@tonic-gate 		subdirlen = mod - modname;
38300Sstevel@tonic-gate 		subdir = kmem_alloc(subdirlen, KM_SLEEP);
38310Sstevel@tonic-gate 		(void) strlcpy(subdir, modname, subdirlen);
38320Sstevel@tonic-gate 	} else {
38330Sstevel@tonic-gate 		subdirlen = 0;
38340Sstevel@tonic-gate 		subdir = "misc";
38350Sstevel@tonic-gate 		mod = (char *)modname;
38360Sstevel@tonic-gate 	}
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	/* reference load with errno return value */
38390Sstevel@tonic-gate 	retval = modrload(subdir, mod, &hmodp);
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 	if (subdirlen)
38420Sstevel@tonic-gate 		kmem_free(subdir, subdirlen);
38430Sstevel@tonic-gate 
38440Sstevel@tonic-gate out:	if (errnop)
38450Sstevel@tonic-gate 		*errnop = retval;
38460Sstevel@tonic-gate 
38470Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
38480Sstevel@tonic-gate 		printf("ddi_modopen %s mode %x: %s %p %d\n",
38490Sstevel@tonic-gate 		    modname ? modname : "<unknown>", mode,
38500Sstevel@tonic-gate 		    hmodp ? hmodp->mod_filename : "<unknown>",
38510Sstevel@tonic-gate 		    (void *)hmodp, retval);
38520Sstevel@tonic-gate 
38530Sstevel@tonic-gate 	return ((ddi_modhandle_t)hmodp);
38540Sstevel@tonic-gate }
38550Sstevel@tonic-gate 
38560Sstevel@tonic-gate /* lookup "name" in open dl module/library */
38570Sstevel@tonic-gate void *
38580Sstevel@tonic-gate ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
38590Sstevel@tonic-gate {
38600Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
38610Sstevel@tonic-gate 	void		*f;
38620Sstevel@tonic-gate 	int		retval;
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 	ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
38650Sstevel@tonic-gate 	if ((hmodp == NULL) || (name == NULL) ||
38660Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
38670Sstevel@tonic-gate 		f = NULL;
38680Sstevel@tonic-gate 		retval = EINVAL;
38690Sstevel@tonic-gate 	} else {
38700Sstevel@tonic-gate 		f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
38710Sstevel@tonic-gate 		if (f)
38720Sstevel@tonic-gate 			retval = 0;
38730Sstevel@tonic-gate 		else
38740Sstevel@tonic-gate 			retval = ENOTSUP;
38750Sstevel@tonic-gate 	}
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
38780Sstevel@tonic-gate 		printf("ddi_modsym in %s of %s: %d %p\n",
38790Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>",
38800Sstevel@tonic-gate 		    name ? name : "<unknown>", retval, f);
38810Sstevel@tonic-gate 
38820Sstevel@tonic-gate 	if (errnop)
38830Sstevel@tonic-gate 		*errnop = retval;
38840Sstevel@tonic-gate 	return (f);
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate /* dynamic (un)reference unload of an open dl module/library */
38880Sstevel@tonic-gate int
38890Sstevel@tonic-gate ddi_modclose(ddi_modhandle_t h)
38900Sstevel@tonic-gate {
38910Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
38920Sstevel@tonic-gate 	struct modctl	*modp = NULL;
38930Sstevel@tonic-gate 	int		retval;
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
38960Sstevel@tonic-gate 	if ((hmodp == NULL) ||
38970Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
38980Sstevel@tonic-gate 		retval = EINVAL;
38990Sstevel@tonic-gate 		goto out;
39000Sstevel@tonic-gate 	}
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 	retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
39030Sstevel@tonic-gate 	if (retval == EBUSY)
39040Sstevel@tonic-gate 		retval = 0;	/* EBUSY is not an error */
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 	if (retval == 0) {
39070Sstevel@tonic-gate 		ASSERT(hmodp == modp);
39080Sstevel@tonic-gate 		if (hmodp != modp)
39090Sstevel@tonic-gate 			retval = EINVAL;
39100Sstevel@tonic-gate 	}
39110Sstevel@tonic-gate 
39120Sstevel@tonic-gate out:	if (moddebug & MODDEBUG_DDI_MOD)
39130Sstevel@tonic-gate 		printf("ddi_modclose %s: %d\n",
39140Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>", retval);
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 	return (retval);
39170Sstevel@tonic-gate }
3918