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
52621Sllai1 * Common Development and Distribution License (the "License").
62621Sllai1 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211414Scindi
220Sstevel@tonic-gate /*
2312107SStephen.Hanson@Sun.COM * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * modctl system call for loadable module support.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/user.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/exec.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/conf.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate #include <sys/reboot.h>
390Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/sysconf.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/sunndi.h>
460Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
470Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
480Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
490Sstevel@tonic-gate #include <sys/bootconf.h>
500Sstevel@tonic-gate #include <sys/dc_ki.h>
510Sstevel@tonic-gate #include <sys/cladm.h>
520Sstevel@tonic-gate #include <sys/dtrace.h>
530Sstevel@tonic-gate #include <sys/kdi.h>
540Sstevel@tonic-gate
550Sstevel@tonic-gate #include <sys/devpolicy.h>
560Sstevel@tonic-gate #include <sys/modctl.h>
570Sstevel@tonic-gate #include <sys/kobj.h>
580Sstevel@tonic-gate #include <sys/devops.h>
590Sstevel@tonic-gate #include <sys/autoconf.h>
600Sstevel@tonic-gate #include <sys/hwconf.h>
610Sstevel@tonic-gate #include <sys/callb.h>
620Sstevel@tonic-gate #include <sys/debug.h>
630Sstevel@tonic-gate #include <sys/cpuvar.h>
640Sstevel@tonic-gate #include <sys/sysmacros.h>
650Sstevel@tonic-gate #include <sys/sysevent.h>
660Sstevel@tonic-gate #include <sys/sysevent_impl.h>
670Sstevel@tonic-gate #include <sys/instance.h>
680Sstevel@tonic-gate #include <sys/modhash.h>
690Sstevel@tonic-gate #include <sys/modhash_impl.h>
700Sstevel@tonic-gate #include <sys/dacf_impl.h>
710Sstevel@tonic-gate #include <sys/vfs.h>
720Sstevel@tonic-gate #include <sys/pathname.h>
730Sstevel@tonic-gate #include <sys/console.h>
740Sstevel@tonic-gate #include <sys/policy.h>
750Sstevel@tonic-gate #include <ipp/ipp_impl.h>
760Sstevel@tonic-gate #include <sys/fs/dv_node.h>
770Sstevel@tonic-gate #include <sys/strsubr.h>
7810097SEric.Taylor@Sun.COM #include <sys/fs/sdev_impl.h>
790Sstevel@tonic-gate
800Sstevel@tonic-gate static int mod_circdep(struct modctl *);
810Sstevel@tonic-gate static int modinfo(modid_t, struct modinfo *);
820Sstevel@tonic-gate
830Sstevel@tonic-gate static void mod_uninstall_all(void);
840Sstevel@tonic-gate static int mod_getinfo(struct modctl *, struct modinfo *);
851414Scindi static struct modctl *allocate_modp(const char *, const char *);
860Sstevel@tonic-gate
870Sstevel@tonic-gate static int mod_load(struct modctl *, int);
880Sstevel@tonic-gate static void mod_unload(struct modctl *);
890Sstevel@tonic-gate static int modinstall(struct modctl *);
900Sstevel@tonic-gate static int moduninstall(struct modctl *);
910Sstevel@tonic-gate
921414Scindi static struct modctl *mod_hold_by_name_common(struct modctl *, const char *);
930Sstevel@tonic-gate static struct modctl *mod_hold_next_by_id(modid_t);
940Sstevel@tonic-gate static struct modctl *mod_hold_loaded_mod(struct modctl *, char *, int *);
955254Sgavinm static struct modctl *mod_hold_installed_mod(char *, int, int, int *);
960Sstevel@tonic-gate
970Sstevel@tonic-gate static void mod_release(struct modctl *);
980Sstevel@tonic-gate static void mod_make_requisite(struct modctl *, struct modctl *);
990Sstevel@tonic-gate static int mod_install_requisites(struct modctl *);
1000Sstevel@tonic-gate static void check_esc_sequences(char *, char *);
1010Sstevel@tonic-gate static struct modctl *mod_hold_by_name_requisite(struct modctl *, char *);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * module loading thread control structure. Calls to kobj_load_module()() are
1050Sstevel@tonic-gate * handled off to a separate thead using this structure.
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate struct loadmt {
1080Sstevel@tonic-gate ksema_t sema;
1090Sstevel@tonic-gate struct modctl *mp;
1100Sstevel@tonic-gate int usepath;
1110Sstevel@tonic-gate kthread_t *owner;
1120Sstevel@tonic-gate int retval;
1130Sstevel@tonic-gate };
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static void modload_thread(struct loadmt *);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate kcondvar_t mod_cv;
1180Sstevel@tonic-gate kcondvar_t mod_uninstall_cv; /* Communication between swapper */
1190Sstevel@tonic-gate /* and the uninstall daemon. */
1200Sstevel@tonic-gate kmutex_t mod_lock; /* protects &modules insert linkage, */
1210Sstevel@tonic-gate /* mod_busy, mod_want, and mod_ref. */
1220Sstevel@tonic-gate /* blocking operations while holding */
1230Sstevel@tonic-gate /* mod_lock should be avoided */
1240Sstevel@tonic-gate kmutex_t mod_uninstall_lock; /* protects mod_uninstall_cv */
1250Sstevel@tonic-gate kthread_id_t mod_aul_thread;
1260Sstevel@tonic-gate
1271218Scth int modunload_wait;
1281218Scth kmutex_t modunload_wait_mutex;
1291218Scth kcondvar_t modunload_wait_cv;
1301218Scth int modunload_active_count;
1311218Scth int modunload_disable_count;
1321218Scth
1330Sstevel@tonic-gate int isminiroot; /* set if running as miniroot */
1340Sstevel@tonic-gate int modrootloaded; /* set after root driver and fs are loaded */
1350Sstevel@tonic-gate int moddebug = 0x0; /* debug flags for module writers */
1360Sstevel@tonic-gate int swaploaded; /* set after swap driver and fs are loaded */
1370Sstevel@tonic-gate int bop_io_quiesced = 0; /* set when BOP I/O can no longer be used */
1380Sstevel@tonic-gate int last_module_id;
1390Sstevel@tonic-gate clock_t mod_uninstall_interval = 0;
140*12207SChris.Horne@Sun.COM int mod_uninstall_pass_max = 6;
141*12207SChris.Horne@Sun.COM int mod_uninstall_ref_zero; /* # modules that went mod_ref == 0 */
142*12207SChris.Horne@Sun.COM int mod_uninstall_pass_exc; /* mod_uninstall_all left new stuff */
143*12207SChris.Horne@Sun.COM
1440Sstevel@tonic-gate int ddi_modclose_unload = 1; /* 0 -> just decrement reference */
1450Sstevel@tonic-gate
14611320SJerry.Gilliam@Sun.COM int devcnt_incr = 256; /* allow for additional drivers */
14711320SJerry.Gilliam@Sun.COM int devcnt_min = 512; /* and always at least this number */
14811320SJerry.Gilliam@Sun.COM
1490Sstevel@tonic-gate struct devnames *devnamesp;
1500Sstevel@tonic-gate struct devnames orphanlist;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate krwlock_t devinfo_tree_lock; /* obsolete, to be removed */
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate #define MAJBINDFILE "/etc/name_to_major"
1550Sstevel@tonic-gate #define SYSBINDFILE "/etc/name_to_sysnum"
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate static char majbind[] = MAJBINDFILE;
1580Sstevel@tonic-gate static char sysbind[] = SYSBINDFILE;
1590Sstevel@tonic-gate static uint_t mod_autounload_key; /* for module autounload detection */
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate extern int obpdebug;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate #define DEBUGGER_PRESENT ((boothowto & RB_DEBUG) || (obpdebug != 0))
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate static int minorperm_loaded = 0;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate void
mod_setup(void)1680Sstevel@tonic-gate mod_setup(void)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate struct sysent *callp;
1710Sstevel@tonic-gate int callnum, exectype;
1720Sstevel@tonic-gate int num_devs;
1730Sstevel@tonic-gate int i;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * Initialize the list of loaded driver dev_ops.
1770Sstevel@tonic-gate * XXX - This must be done before reading the system file so that
1780Sstevel@tonic-gate * forceloads of drivers will work.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * Since read_binding_file is common code, it doesn't enforce that all
18310923SEvan.Yan@Sun.COM * of the binding file entries have major numbers <= MAXMAJ32. Thus,
1840Sstevel@tonic-gate * ensure that we don't allocate some massive amount of space due to a
1850Sstevel@tonic-gate * bad entry. We can't have major numbers bigger than MAXMAJ32
1860Sstevel@tonic-gate * until file system support for larger major numbers exists.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * Leave space for expansion, but not more than L_MAXMAJ32
1910Sstevel@tonic-gate */
19211320SJerry.Gilliam@Sun.COM devcnt = MIN(num_devs + devcnt_incr, L_MAXMAJ32);
19311320SJerry.Gilliam@Sun.COM devcnt = MAX(devcnt, devcnt_min);
1940Sstevel@tonic-gate devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
1950Sstevel@tonic-gate for (i = 0; i < devcnt; i++)
1960Sstevel@tonic-gate devopsp[i] = &mod_nodev_ops;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate init_devnamesp(devcnt);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Sync up with the work that the stand-alone linker has already done.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate (void) kobj_sync();
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (boothowto & RB_DEBUG)
2060Sstevel@tonic-gate kdi_dvec_modavail();
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate make_aliases(mb_hashtab);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Initialize streams device implementation structures.
2120Sstevel@tonic-gate */
2130Sstevel@tonic-gate devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * If the cl_bootstrap module is present,
2170Sstevel@tonic-gate * we should be configured as a cluster. Loading this module
2180Sstevel@tonic-gate * will set "cluster_bootflags" to non-zero.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate (void) modload("misc", "cl_bootstrap");
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate (void) read_binding_file(sysbind, sb_hashtab, make_mbind);
2230Sstevel@tonic-gate init_syscallnames(NSYSCALL);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * Start up dynamic autoconfiguration framework (dacf).
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate mod_hash_init();
2290Sstevel@tonic-gate dacf_init();
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * Start up IP policy framework (ipp).
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate ipp_init();
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * Allocate loadable native system call locks.
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate for (callnum = 0, callp = sysent; callnum < NSYSCALL;
2400Sstevel@tonic-gate callnum++, callp++) {
2410Sstevel@tonic-gate if (LOADABLE_SYSCALL(callp)) {
2420Sstevel@tonic-gate if (mod_getsysname(callnum) != NULL) {
2430Sstevel@tonic-gate callp->sy_lock =
2440Sstevel@tonic-gate kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2450Sstevel@tonic-gate rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2460Sstevel@tonic-gate } else {
2470Sstevel@tonic-gate callp->sy_flags &= ~SE_LOADABLE;
2480Sstevel@tonic-gate callp->sy_callc = nosys;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate #ifdef DEBUG
2510Sstevel@tonic-gate } else {
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate * Do some sanity checks on the sysent table
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate switch (callp->sy_flags & SE_RVAL_MASK) {
2560Sstevel@tonic-gate case SE_32RVAL1:
2570Sstevel@tonic-gate /* only r_val1 returned */
2580Sstevel@tonic-gate case SE_32RVAL1 | SE_32RVAL2:
2590Sstevel@tonic-gate /* r_val1 and r_val2 returned */
2600Sstevel@tonic-gate case SE_64RVAL:
2610Sstevel@tonic-gate /* 64-bit rval returned */
2620Sstevel@tonic-gate break;
2630Sstevel@tonic-gate default:
2640Sstevel@tonic-gate cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
2650Sstevel@tonic-gate callnum, callp->sy_flags);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate #endif
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Allocate loadable system call locks for 32-bit compat syscalls
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
2760Sstevel@tonic-gate callnum++, callp++) {
2770Sstevel@tonic-gate if (LOADABLE_SYSCALL(callp)) {
2780Sstevel@tonic-gate if (mod_getsysname(callnum) != NULL) {
2790Sstevel@tonic-gate callp->sy_lock =
2800Sstevel@tonic-gate kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2810Sstevel@tonic-gate rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2820Sstevel@tonic-gate } else {
2830Sstevel@tonic-gate callp->sy_flags &= ~SE_LOADABLE;
2840Sstevel@tonic-gate callp->sy_callc = nosys;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate #ifdef DEBUG
2870Sstevel@tonic-gate } else {
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * Do some sanity checks on the sysent table
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate switch (callp->sy_flags & SE_RVAL_MASK) {
2920Sstevel@tonic-gate case SE_32RVAL1:
2930Sstevel@tonic-gate /* only r_val1 returned */
2940Sstevel@tonic-gate case SE_32RVAL1 | SE_32RVAL2:
2950Sstevel@tonic-gate /* r_val1 and r_val2 returned */
2960Sstevel@tonic-gate case SE_64RVAL:
2970Sstevel@tonic-gate /* 64-bit rval returned */
2980Sstevel@tonic-gate break;
2990Sstevel@tonic-gate default:
3000Sstevel@tonic-gate cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
3010Sstevel@tonic-gate callnum, callp->sy_flags);
3020Sstevel@tonic-gate goto skip;
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * Cross-check the native and compatibility tables.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate if (callp->sy_callc == nosys ||
3090Sstevel@tonic-gate sysent[callnum].sy_callc == nosys)
3100Sstevel@tonic-gate continue;
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate * If only one or the other slot is loadable, then
3130Sstevel@tonic-gate * there's an error -- they should match!
3140Sstevel@tonic-gate */
3150Sstevel@tonic-gate if ((callp->sy_callc == loadable_syscall) ^
3160Sstevel@tonic-gate (sysent[callnum].sy_callc == loadable_syscall)) {
3170Sstevel@tonic-gate cmn_err(CE_WARN, "sysent[%d] loadable?",
3180Sstevel@tonic-gate callnum);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * This is more of a heuristic test -- if the
3220Sstevel@tonic-gate * system call returns two values in the 32-bit
3230Sstevel@tonic-gate * world, it should probably return two 32-bit
3240Sstevel@tonic-gate * values in the 64-bit world too.
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate if (((callp->sy_flags & SE_32RVAL2) == 0) ^
3270Sstevel@tonic-gate ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
3280Sstevel@tonic-gate cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
3290Sstevel@tonic-gate callnum);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate skip:;
3320Sstevel@tonic-gate #endif /* DEBUG */
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate * Allocate loadable exec locks. (Assumes all execs are loadable)
3390Sstevel@tonic-gate */
3400Sstevel@tonic-gate for (exectype = 0; exectype < nexectype; exectype++) {
3410Sstevel@tonic-gate execsw[exectype].exec_lock =
3420Sstevel@tonic-gate kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
3430Sstevel@tonic-gate rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate read_class_file();
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /* init thread specific structure for mod_uninstall_all */
3490Sstevel@tonic-gate tsd_create(&mod_autounload_key, NULL);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate static int
modctl_modload(int use_path,char * filename,int * rvp)3530Sstevel@tonic-gate modctl_modload(int use_path, char *filename, int *rvp)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate struct modctl *modp;
3560Sstevel@tonic-gate int retval = 0;
3570Sstevel@tonic-gate char *filenamep;
3580Sstevel@tonic-gate int modid;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
3630Sstevel@tonic-gate retval = EFAULT;
3640Sstevel@tonic-gate goto out;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate filenamep[MOD_MAXPATH - 1] = 0;
3685254Sgavinm modp = mod_hold_installed_mod(filenamep, use_path, 0, &retval);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (modp == NULL)
3710Sstevel@tonic-gate goto out;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
3740Sstevel@tonic-gate modid = modp->mod_id;
3750Sstevel@tonic-gate mod_release_mod(modp);
376191Sahl CPU_STATS_ADDQ(CPU, sys, modload, 1);
3770Sstevel@tonic-gate if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
3780Sstevel@tonic-gate retval = EFAULT;
3790Sstevel@tonic-gate out:
3800Sstevel@tonic-gate kmem_free(filenamep, MOD_MAXPATH);
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate return (retval);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate static int
modctl_modunload(modid_t id)3860Sstevel@tonic-gate modctl_modunload(modid_t id)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate int rval = 0;
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate if (id == 0) {
3910Sstevel@tonic-gate #ifdef DEBUG
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate * Turn on mod_uninstall_daemon
3940Sstevel@tonic-gate */
3950Sstevel@tonic-gate if (mod_uninstall_interval == 0) {
3960Sstevel@tonic-gate mod_uninstall_interval = 60;
3970Sstevel@tonic-gate modreap();
3980Sstevel@tonic-gate return (rval);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate #endif
4010Sstevel@tonic-gate mod_uninstall_all();
4020Sstevel@tonic-gate } else {
4030Sstevel@tonic-gate rval = modunload(id);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate return (rval);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate static int
modctl_modinfo(modid_t id,struct modinfo * umodi)4090Sstevel@tonic-gate modctl_modinfo(modid_t id, struct modinfo *umodi)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate int retval;
4120Sstevel@tonic-gate struct modinfo modi;
4130Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4140Sstevel@tonic-gate int nobase;
4150Sstevel@tonic-gate struct modinfo32 modi32;
4160Sstevel@tonic-gate #endif
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
4190Sstevel@tonic-gate if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
4200Sstevel@tonic-gate return (EFAULT);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4230Sstevel@tonic-gate else {
4240Sstevel@tonic-gate bzero(&modi, sizeof (modi));
4250Sstevel@tonic-gate if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
4260Sstevel@tonic-gate return (EFAULT);
4270Sstevel@tonic-gate modi.mi_info = modi32.mi_info;
4280Sstevel@tonic-gate modi.mi_id = modi32.mi_id;
4290Sstevel@tonic-gate modi.mi_nextid = modi32.mi_nextid;
4300Sstevel@tonic-gate nobase = modi.mi_info & MI_INFO_NOBASE;
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate #endif
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate * This flag is -only- for the kernels use.
4350Sstevel@tonic-gate */
4360Sstevel@tonic-gate modi.mi_info &= ~MI_INFO_LINKAGE;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate retval = modinfo(id, &modi);
4390Sstevel@tonic-gate if (retval)
4400Sstevel@tonic-gate return (retval);
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
4430Sstevel@tonic-gate if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
4440Sstevel@tonic-gate retval = EFAULT;
4450Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4460Sstevel@tonic-gate } else {
4470Sstevel@tonic-gate int i;
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
4500Sstevel@tonic-gate return (EOVERFLOW);
4510Sstevel@tonic-gate
4520Sstevel@tonic-gate modi32.mi_info = modi.mi_info;
4530Sstevel@tonic-gate modi32.mi_state = modi.mi_state;
4540Sstevel@tonic-gate modi32.mi_id = modi.mi_id;
4550Sstevel@tonic-gate modi32.mi_nextid = modi.mi_nextid;
4560Sstevel@tonic-gate modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
4570Sstevel@tonic-gate modi32.mi_size = modi.mi_size;
4580Sstevel@tonic-gate modi32.mi_rev = modi.mi_rev;
4590Sstevel@tonic-gate modi32.mi_loadcnt = modi.mi_loadcnt;
4600Sstevel@tonic-gate bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
4610Sstevel@tonic-gate for (i = 0; i < MODMAXLINK32; i++) {
4620Sstevel@tonic-gate modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
4630Sstevel@tonic-gate bcopy(modi.mi_msinfo[i].msi_linkinfo,
4640Sstevel@tonic-gate modi32.mi_msinfo[i].msi_linkinfo,
4650Sstevel@tonic-gate sizeof (modi32.mi_msinfo[0].msi_linkinfo));
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
4680Sstevel@tonic-gate retval = EFAULT;
4690Sstevel@tonic-gate #endif
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate return (retval);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate * Return the last major number in the range of permissible major numbers.
4770Sstevel@tonic-gate */
4780Sstevel@tonic-gate /*ARGSUSED*/
4790Sstevel@tonic-gate static int
modctl_modreserve(modid_t id,int * data)4800Sstevel@tonic-gate modctl_modreserve(modid_t id, int *data)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
4830Sstevel@tonic-gate return (EFAULT);
4840Sstevel@tonic-gate return (0);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4878831SJerry.Gilliam@Sun.COM /* Add/Remove driver and binding aliases */
4888831SJerry.Gilliam@Sun.COM static int
modctl_update_driver_aliases(int add,int * data)4898831SJerry.Gilliam@Sun.COM modctl_update_driver_aliases(int add, int *data)
4908831SJerry.Gilliam@Sun.COM {
4918831SJerry.Gilliam@Sun.COM struct modconfig mc;
4928831SJerry.Gilliam@Sun.COM int i, n, rv = 0;
4938831SJerry.Gilliam@Sun.COM struct aliases alias;
4948831SJerry.Gilliam@Sun.COM struct aliases *ap;
4958831SJerry.Gilliam@Sun.COM char name[MAXMODCONFNAME];
4968831SJerry.Gilliam@Sun.COM char cname[MAXMODCONFNAME];
4978831SJerry.Gilliam@Sun.COM char *drvname;
4988831SJerry.Gilliam@Sun.COM int resid;
4998831SJerry.Gilliam@Sun.COM struct alias_info {
5008831SJerry.Gilliam@Sun.COM char *alias_name;
5018831SJerry.Gilliam@Sun.COM int alias_resid;
5028831SJerry.Gilliam@Sun.COM } *aliases, *aip;
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate bzero(&mc, sizeof (struct modconfig));
5050Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
5060Sstevel@tonic-gate if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
5070Sstevel@tonic-gate return (EFAULT);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5100Sstevel@tonic-gate else {
5110Sstevel@tonic-gate struct modconfig32 modc32;
5120Sstevel@tonic-gate if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
5130Sstevel@tonic-gate return (EFAULT);
5140Sstevel@tonic-gate else {
5150Sstevel@tonic-gate bcopy(modc32.drvname, mc.drvname,
5160Sstevel@tonic-gate sizeof (modc32.drvname));
5170Sstevel@tonic-gate bcopy(modc32.drvclass, mc.drvclass,
5180Sstevel@tonic-gate sizeof (modc32.drvclass));
5190Sstevel@tonic-gate mc.major = modc32.major;
5208831SJerry.Gilliam@Sun.COM mc.flags = modc32.flags;
5210Sstevel@tonic-gate mc.num_aliases = modc32.num_aliases;
5220Sstevel@tonic-gate mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate * If the driver is already in the mb_hashtab, and the name given
5290Sstevel@tonic-gate * doesn't match that driver's name, fail. Otherwise, pass, since
5300Sstevel@tonic-gate * we may be adding aliases.
5310Sstevel@tonic-gate */
5328831SJerry.Gilliam@Sun.COM drvname = mod_major_to_name(mc.major);
5338831SJerry.Gilliam@Sun.COM if ((drvname != NULL) && strcmp(drvname, mc.drvname) != 0)
5340Sstevel@tonic-gate return (EINVAL);
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate /*
5378831SJerry.Gilliam@Sun.COM * Precede alias removal by unbinding as many devices as possible.
5388831SJerry.Gilliam@Sun.COM */
5398831SJerry.Gilliam@Sun.COM if (add == 0) {
5408831SJerry.Gilliam@Sun.COM (void) i_ddi_unload_drvconf(mc.major);
5418831SJerry.Gilliam@Sun.COM i_ddi_unbind_devs(mc.major);
5428831SJerry.Gilliam@Sun.COM }
5438831SJerry.Gilliam@Sun.COM
5448831SJerry.Gilliam@Sun.COM /*
5458831SJerry.Gilliam@Sun.COM * Add/remove each supplied driver alias to/from mb_hashtab
5460Sstevel@tonic-gate */
5470Sstevel@tonic-gate ap = mc.ap;
5488831SJerry.Gilliam@Sun.COM if (mc.num_aliases > 0)
5498831SJerry.Gilliam@Sun.COM aliases = kmem_zalloc(
5508831SJerry.Gilliam@Sun.COM mc.num_aliases * sizeof (struct alias_info), KM_SLEEP);
5518831SJerry.Gilliam@Sun.COM aip = aliases;
5520Sstevel@tonic-gate for (i = 0; i < mc.num_aliases; i++) {
5530Sstevel@tonic-gate bzero(&alias, sizeof (struct aliases));
5540Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
5558831SJerry.Gilliam@Sun.COM if (copyin(ap, &alias, sizeof (struct aliases)) != 0) {
5568831SJerry.Gilliam@Sun.COM rv = EFAULT;
5578831SJerry.Gilliam@Sun.COM goto error;
5588831SJerry.Gilliam@Sun.COM }
5598831SJerry.Gilliam@Sun.COM if (alias.a_len > MAXMODCONFNAME) {
5608831SJerry.Gilliam@Sun.COM rv = EINVAL;
5618831SJerry.Gilliam@Sun.COM goto error;
5628831SJerry.Gilliam@Sun.COM }
5638831SJerry.Gilliam@Sun.COM if (copyin(alias.a_name, name, alias.a_len) != 0) {
5648831SJerry.Gilliam@Sun.COM rv = EFAULT;
5658831SJerry.Gilliam@Sun.COM goto error;
5668831SJerry.Gilliam@Sun.COM }
5678831SJerry.Gilliam@Sun.COM if (name[alias.a_len - 1] != '\0') {
5688831SJerry.Gilliam@Sun.COM rv = EINVAL;
5698831SJerry.Gilliam@Sun.COM goto error;
5708831SJerry.Gilliam@Sun.COM }
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5730Sstevel@tonic-gate else {
5740Sstevel@tonic-gate struct aliases32 al32;
5750Sstevel@tonic-gate bzero(&al32, sizeof (struct aliases32));
5768831SJerry.Gilliam@Sun.COM if (copyin(ap, &al32, sizeof (struct aliases32)) != 0) {
5778831SJerry.Gilliam@Sun.COM rv = EFAULT;
5788831SJerry.Gilliam@Sun.COM goto error;
5798831SJerry.Gilliam@Sun.COM }
5808831SJerry.Gilliam@Sun.COM if (al32.a_len > MAXMODCONFNAME) {
5818831SJerry.Gilliam@Sun.COM rv = EINVAL;
5828831SJerry.Gilliam@Sun.COM goto error;
5838831SJerry.Gilliam@Sun.COM }
5840Sstevel@tonic-gate if (copyin((void *)(uintptr_t)al32.a_name,
5858831SJerry.Gilliam@Sun.COM name, al32.a_len) != 0) {
5868831SJerry.Gilliam@Sun.COM rv = EFAULT;
5878831SJerry.Gilliam@Sun.COM goto error;
5888831SJerry.Gilliam@Sun.COM }
5898831SJerry.Gilliam@Sun.COM if (name[al32.a_len - 1] != '\0') {
5908831SJerry.Gilliam@Sun.COM rv = EINVAL;
5918831SJerry.Gilliam@Sun.COM goto error;
5928831SJerry.Gilliam@Sun.COM }
5930Sstevel@tonic-gate alias.a_next = (void *)(uintptr_t)al32.a_next;
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate #endif
5960Sstevel@tonic-gate check_esc_sequences(name, cname);
5978863SEdward.Pilatowicz@Sun.COM aip->alias_name = strdup(cname);
5980Sstevel@tonic-gate ap = alias.a_next;
5998831SJerry.Gilliam@Sun.COM aip++;
6008831SJerry.Gilliam@Sun.COM }
6018831SJerry.Gilliam@Sun.COM
6028831SJerry.Gilliam@Sun.COM if (add == 0) {
6038831SJerry.Gilliam@Sun.COM ap = mc.ap;
6048831SJerry.Gilliam@Sun.COM resid = 0;
6058831SJerry.Gilliam@Sun.COM aip = aliases;
6068831SJerry.Gilliam@Sun.COM /* attempt to unbind all devices bound to each alias */
6078831SJerry.Gilliam@Sun.COM for (i = 0; i < mc.num_aliases; i++) {
6088831SJerry.Gilliam@Sun.COM n = i_ddi_unbind_devs_by_alias(
6098831SJerry.Gilliam@Sun.COM mc.major, aip->alias_name);
6108831SJerry.Gilliam@Sun.COM resid += n;
6118831SJerry.Gilliam@Sun.COM aip->alias_resid = n;
6128831SJerry.Gilliam@Sun.COM }
6138831SJerry.Gilliam@Sun.COM
6148831SJerry.Gilliam@Sun.COM /*
6158831SJerry.Gilliam@Sun.COM * If some device bound to an alias remains in use,
6168831SJerry.Gilliam@Sun.COM * and override wasn't specified, no change is made to
6178831SJerry.Gilliam@Sun.COM * the binding state and we fail the operation.
6188831SJerry.Gilliam@Sun.COM */
6198831SJerry.Gilliam@Sun.COM if (resid > 0 && ((mc.flags & MOD_UNBIND_OVERRIDE) == 0)) {
6208831SJerry.Gilliam@Sun.COM rv = EBUSY;
6218831SJerry.Gilliam@Sun.COM goto error;
6228831SJerry.Gilliam@Sun.COM }
6238831SJerry.Gilliam@Sun.COM
6248831SJerry.Gilliam@Sun.COM /*
6258831SJerry.Gilliam@Sun.COM * No device remains bound of any of the aliases,
6268831SJerry.Gilliam@Sun.COM * or force was requested. Mark each alias as
6278831SJerry.Gilliam@Sun.COM * inactive via delete_mbind so no future binds
6288831SJerry.Gilliam@Sun.COM * to this alias take place and that a new
6298831SJerry.Gilliam@Sun.COM * binding can be established.
6308831SJerry.Gilliam@Sun.COM */
6318831SJerry.Gilliam@Sun.COM aip = aliases;
6328831SJerry.Gilliam@Sun.COM for (i = 0; i < mc.num_aliases; i++) {
6338831SJerry.Gilliam@Sun.COM if (moddebug & MODDEBUG_BINDING)
6348831SJerry.Gilliam@Sun.COM cmn_err(CE_CONT, "Removing binding for %s "
6358831SJerry.Gilliam@Sun.COM "(%d active references)\n",
6368831SJerry.Gilliam@Sun.COM aip->alias_name, aip->alias_resid);
6378831SJerry.Gilliam@Sun.COM delete_mbind(aip->alias_name, mb_hashtab);
6388831SJerry.Gilliam@Sun.COM aip++;
6398831SJerry.Gilliam@Sun.COM }
6408831SJerry.Gilliam@Sun.COM rv = 0;
6418831SJerry.Gilliam@Sun.COM } else {
6428831SJerry.Gilliam@Sun.COM aip = aliases;
6438831SJerry.Gilliam@Sun.COM for (i = 0; i < mc.num_aliases; i++) {
6448831SJerry.Gilliam@Sun.COM if (moddebug & MODDEBUG_BINDING)
6458831SJerry.Gilliam@Sun.COM cmn_err(CE_NOTE, "Adding binding for '%s'\n",
6468831SJerry.Gilliam@Sun.COM aip->alias_name);
6478831SJerry.Gilliam@Sun.COM (void) make_mbind(aip->alias_name,
6488831SJerry.Gilliam@Sun.COM mc.major, NULL, mb_hashtab);
6498831SJerry.Gilliam@Sun.COM aip++;
6508831SJerry.Gilliam@Sun.COM }
6518831SJerry.Gilliam@Sun.COM /*
6528831SJerry.Gilliam@Sun.COM * Try to establish an mbinding for mc.drvname, and add it to
6538831SJerry.Gilliam@Sun.COM * devnames. Add class if any after establishing the major
6548831SJerry.Gilliam@Sun.COM * number.
6558831SJerry.Gilliam@Sun.COM */
6568831SJerry.Gilliam@Sun.COM (void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
65710842SJerry.Gilliam@Sun.COM if ((rv = make_devname(mc.drvname, mc.major,
65810842SJerry.Gilliam@Sun.COM (mc.flags & MOD_ADDMAJBIND_UPDATE) ?
65910842SJerry.Gilliam@Sun.COM DN_DRIVER_INACTIVE : 0)) != 0) {
6608831SJerry.Gilliam@Sun.COM goto error;
66110842SJerry.Gilliam@Sun.COM }
6628831SJerry.Gilliam@Sun.COM
6638831SJerry.Gilliam@Sun.COM if (mc.drvclass[0] != '\0')
6648831SJerry.Gilliam@Sun.COM add_class(mc.drvname, mc.drvclass);
66510842SJerry.Gilliam@Sun.COM if ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0) {
66610842SJerry.Gilliam@Sun.COM (void) i_ddi_load_drvconf(mc.major);
66710842SJerry.Gilliam@Sun.COM }
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate /*
6718831SJerry.Gilliam@Sun.COM * Ensure that all nodes are bound to the most appropriate driver
6728831SJerry.Gilliam@Sun.COM * possible, attempting demotion and rebind when a more appropriate
67310842SJerry.Gilliam@Sun.COM * driver now exists. But not when adding a driver update-only.
6740Sstevel@tonic-gate */
67510842SJerry.Gilliam@Sun.COM if ((add == 0) || ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0)) {
67610842SJerry.Gilliam@Sun.COM i_ddi_bind_devs();
67710842SJerry.Gilliam@Sun.COM i_ddi_di_cache_invalidate();
67810842SJerry.Gilliam@Sun.COM }
6798831SJerry.Gilliam@Sun.COM
6808831SJerry.Gilliam@Sun.COM error:
6818831SJerry.Gilliam@Sun.COM if (mc.num_aliases > 0) {
6828831SJerry.Gilliam@Sun.COM aip = aliases;
6838831SJerry.Gilliam@Sun.COM for (i = 0; i < mc.num_aliases; i++) {
6848831SJerry.Gilliam@Sun.COM if (aip->alias_name != NULL)
6858831SJerry.Gilliam@Sun.COM strfree(aip->alias_name);
6868831SJerry.Gilliam@Sun.COM aip++;
6878831SJerry.Gilliam@Sun.COM }
6888831SJerry.Gilliam@Sun.COM kmem_free(aliases, mc.num_aliases * sizeof (struct alias_info));
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate return (rv);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate static int
modctl_add_driver_aliases(int * data)6948831SJerry.Gilliam@Sun.COM modctl_add_driver_aliases(int *data)
6958831SJerry.Gilliam@Sun.COM {
6968831SJerry.Gilliam@Sun.COM return (modctl_update_driver_aliases(1, data));
6978831SJerry.Gilliam@Sun.COM }
6988831SJerry.Gilliam@Sun.COM
6998831SJerry.Gilliam@Sun.COM static int
modctl_remove_driver_aliases(int * data)7008831SJerry.Gilliam@Sun.COM modctl_remove_driver_aliases(int *data)
7018831SJerry.Gilliam@Sun.COM {
7028831SJerry.Gilliam@Sun.COM return (modctl_update_driver_aliases(0, data));
7038831SJerry.Gilliam@Sun.COM }
7048831SJerry.Gilliam@Sun.COM
7058831SJerry.Gilliam@Sun.COM static int
modctl_rem_major(major_t major)7060Sstevel@tonic-gate modctl_rem_major(major_t major)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate struct devnames *dnp;
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate if (major >= devcnt)
7110Sstevel@tonic-gate return (EINVAL);
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate /* mark devnames as removed */
7140Sstevel@tonic-gate dnp = &devnamesp[major];
7150Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
7160Sstevel@tonic-gate if (dnp->dn_name == NULL ||
7170Sstevel@tonic-gate (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
7180Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
7190Sstevel@tonic-gate return (EINVAL);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate dnp->dn_flags |= DN_DRIVER_REMOVED;
7220Sstevel@tonic-gate pm_driver_removed(major);
7230Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate (void) i_ddi_unload_drvconf(major);
7260Sstevel@tonic-gate i_ddi_unbind_devs(major);
7278831SJerry.Gilliam@Sun.COM i_ddi_bind_devs();
72810696SDavid.Hollister@Sun.COM i_ddi_di_cache_invalidate();
7298831SJerry.Gilliam@Sun.COM
7308831SJerry.Gilliam@Sun.COM /* purge all the bindings to this driver */
7318831SJerry.Gilliam@Sun.COM purge_mbind(major, mb_hashtab);
7320Sstevel@tonic-gate return (0);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate static struct vfs *
path_to_vfs(char * name)7360Sstevel@tonic-gate path_to_vfs(char *name)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate vnode_t *vp;
7390Sstevel@tonic-gate struct vfs *vfsp;
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
7420Sstevel@tonic-gate return (NULL);
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate vfsp = vp->v_vfsp;
7450Sstevel@tonic-gate VN_RELE(vp);
7460Sstevel@tonic-gate return (vfsp);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate static int
new_vfs_in_modpath()7500Sstevel@tonic-gate new_vfs_in_modpath()
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate static int n_modpath = 0;
7530Sstevel@tonic-gate static char *modpath_copy;
7540Sstevel@tonic-gate static struct pathvfs {
7550Sstevel@tonic-gate char *path;
7560Sstevel@tonic-gate struct vfs *vfsp;
7570Sstevel@tonic-gate } *pathvfs;
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate int i, new_vfs = 0;
7600Sstevel@tonic-gate char *tmp, *tmp1;
7610Sstevel@tonic-gate struct vfs *vfsp;
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate if (n_modpath != 0) {
7640Sstevel@tonic-gate for (i = 0; i < n_modpath; i++) {
7650Sstevel@tonic-gate vfsp = path_to_vfs(pathvfs[i].path);
7660Sstevel@tonic-gate if (vfsp != pathvfs[i].vfsp) {
7670Sstevel@tonic-gate pathvfs[i].vfsp = vfsp;
7680Sstevel@tonic-gate if (vfsp)
7690Sstevel@tonic-gate new_vfs = 1;
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate return (new_vfs);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate * First call, initialize the pathvfs structure
7770Sstevel@tonic-gate */
7780Sstevel@tonic-gate modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
7790Sstevel@tonic-gate tmp = modpath_copy;
7800Sstevel@tonic-gate n_modpath = 1;
7810Sstevel@tonic-gate tmp1 = strchr(tmp, ' ');
7820Sstevel@tonic-gate while (tmp1) {
7830Sstevel@tonic-gate *tmp1 = '\0';
7840Sstevel@tonic-gate n_modpath++;
7850Sstevel@tonic-gate tmp = tmp1 + 1;
7860Sstevel@tonic-gate tmp1 = strchr(tmp, ' ');
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
7900Sstevel@tonic-gate tmp = modpath_copy;
7910Sstevel@tonic-gate for (i = 0; i < n_modpath; i++) {
7920Sstevel@tonic-gate pathvfs[i].path = tmp;
7930Sstevel@tonic-gate vfsp = path_to_vfs(tmp);
7940Sstevel@tonic-gate pathvfs[i].vfsp = vfsp;
7950Sstevel@tonic-gate tmp += strlen(tmp) + 1;
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate return (1); /* always reread driver.conf the first time */
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8002621Sllai1 static int
modctl_load_drvconf(major_t major,int flags)80110842SJerry.Gilliam@Sun.COM modctl_load_drvconf(major_t major, int flags)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate int ret;
8040Sstevel@tonic-gate
80510842SJerry.Gilliam@Sun.COM /*
80610842SJerry.Gilliam@Sun.COM * devfsadm -u - read all new driver.conf files
80710842SJerry.Gilliam@Sun.COM * and bind and configure devices for new drivers.
80810842SJerry.Gilliam@Sun.COM */
80910842SJerry.Gilliam@Sun.COM if (flags & MOD_LOADDRVCONF_RECONF) {
81010842SJerry.Gilliam@Sun.COM (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
81110842SJerry.Gilliam@Sun.COM i_ddi_bind_devs();
81210842SJerry.Gilliam@Sun.COM i_ddi_di_cache_invalidate();
81310842SJerry.Gilliam@Sun.COM return (0);
81410842SJerry.Gilliam@Sun.COM }
81510842SJerry.Gilliam@Sun.COM
81610842SJerry.Gilliam@Sun.COM /*
81710842SJerry.Gilliam@Sun.COM * update_drv <drv> - reload driver.conf for the specified driver
81810842SJerry.Gilliam@Sun.COM */
8197009Scth if (major != DDI_MAJOR_T_NONE) {
8200Sstevel@tonic-gate ret = i_ddi_load_drvconf(major);
8210Sstevel@tonic-gate if (ret == 0)
8220Sstevel@tonic-gate i_ddi_bind_devs();
8230Sstevel@tonic-gate return (ret);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate * We are invoked to rescan new driver.conf files. It is
8280Sstevel@tonic-gate * only necessary if a new file system was mounted in the
8290Sstevel@tonic-gate * module_path. Because rescanning driver.conf files can
8300Sstevel@tonic-gate * take some time on older platforms (sun4m), the following
8310Sstevel@tonic-gate * code skips unnecessary driver.conf rescans to optimize
8320Sstevel@tonic-gate * boot performance.
8330Sstevel@tonic-gate */
8340Sstevel@tonic-gate if (new_vfs_in_modpath()) {
8357009Scth (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate * If we are still initializing io subsystem,
8380Sstevel@tonic-gate * load drivers with ddi-forceattach property
8390Sstevel@tonic-gate */
8400Sstevel@tonic-gate if (!i_ddi_io_initialized())
8410Sstevel@tonic-gate i_ddi_forceattach_drivers();
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate return (0);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
8468831SJerry.Gilliam@Sun.COM /*
8478831SJerry.Gilliam@Sun.COM * Unload driver.conf file and follow up by attempting
8488831SJerry.Gilliam@Sun.COM * to rebind devices to more appropriate driver.
8498831SJerry.Gilliam@Sun.COM */
8500Sstevel@tonic-gate static int
modctl_unload_drvconf(major_t major)8510Sstevel@tonic-gate modctl_unload_drvconf(major_t major)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate int ret;
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate if (major >= devcnt)
8560Sstevel@tonic-gate return (EINVAL);
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate ret = i_ddi_unload_drvconf(major);
8590Sstevel@tonic-gate if (ret != 0)
8600Sstevel@tonic-gate return (ret);
8610Sstevel@tonic-gate (void) i_ddi_unbind_devs(major);
8628831SJerry.Gilliam@Sun.COM i_ddi_bind_devs();
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate return (0);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate static void
check_esc_sequences(char * str,char * cstr)8680Sstevel@tonic-gate check_esc_sequences(char *str, char *cstr)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate int i;
8710Sstevel@tonic-gate size_t len;
8720Sstevel@tonic-gate char *p;
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate len = strlen(str);
8750Sstevel@tonic-gate for (i = 0; i < len; i++, str++, cstr++) {
8760Sstevel@tonic-gate if (*str != '\\') {
8770Sstevel@tonic-gate *cstr = *str;
8780Sstevel@tonic-gate } else {
8790Sstevel@tonic-gate p = str + 1;
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate * we only handle octal escape sequences for SPACE
8820Sstevel@tonic-gate */
8830Sstevel@tonic-gate if (*p++ == '0' && *p++ == '4' && *p == '0') {
8840Sstevel@tonic-gate *cstr = ' ';
8850Sstevel@tonic-gate str += 3;
8860Sstevel@tonic-gate } else {
8870Sstevel@tonic-gate *cstr = *str;
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate *cstr = 0;
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate static int
modctl_getmodpathlen(int * data)8950Sstevel@tonic-gate modctl_getmodpathlen(int *data)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate int len;
8980Sstevel@tonic-gate len = strlen(default_path);
8990Sstevel@tonic-gate if (copyout(&len, data, sizeof (len)) != 0)
9000Sstevel@tonic-gate return (EFAULT);
9010Sstevel@tonic-gate return (0);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate static int
modctl_getmodpath(char * data)9050Sstevel@tonic-gate modctl_getmodpath(char *data)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate if (copyout(default_path, data, strlen(default_path) + 1) != 0)
9080Sstevel@tonic-gate return (EFAULT);
9090Sstevel@tonic-gate return (0);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate static int
modctl_read_sysbinding_file(void)9130Sstevel@tonic-gate modctl_read_sysbinding_file(void)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate (void) read_binding_file(sysbind, sb_hashtab, make_mbind);
9160Sstevel@tonic-gate return (0);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate static int
modctl_getmaj(char * uname,uint_t ulen,int * umajorp)9200Sstevel@tonic-gate modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
9210Sstevel@tonic-gate {
9220Sstevel@tonic-gate char name[256];
9230Sstevel@tonic-gate int retval;
9240Sstevel@tonic-gate major_t major;
9250Sstevel@tonic-gate
9262621Sllai1 if (ulen == 0)
9272621Sllai1 return (EINVAL);
9280Sstevel@tonic-gate if ((retval = copyinstr(uname, name,
9290Sstevel@tonic-gate (ulen < 256) ? ulen : 256, 0)) != 0)
9300Sstevel@tonic-gate return (retval);
9317009Scth if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
9320Sstevel@tonic-gate return (ENODEV);
9330Sstevel@tonic-gate if (copyout(&major, umajorp, sizeof (major_t)) != 0)
9340Sstevel@tonic-gate return (EFAULT);
9350Sstevel@tonic-gate return (0);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9384845Svikram static char **
convert_constraint_string(char * constraints,size_t len)9394845Svikram convert_constraint_string(char *constraints, size_t len)
9404845Svikram {
9414845Svikram int i;
9424845Svikram int n;
9434845Svikram char *p;
9444845Svikram char **array;
9454845Svikram
9464845Svikram ASSERT(constraints != NULL);
9474845Svikram ASSERT(len > 0);
9484845Svikram
9494851Scth for (i = 0, p = constraints; strlen(p) > 0; i++, p += strlen(p) + 1)
9504851Scth ;
9514845Svikram
9524845Svikram n = i;
9534845Svikram
9544845Svikram if (n == 0) {
9554845Svikram kmem_free(constraints, len);
9564845Svikram return (NULL);
9574845Svikram }
9584845Svikram
9594845Svikram array = kmem_alloc((n + 1) * sizeof (char *), KM_SLEEP);
9604845Svikram
9614845Svikram for (i = 0, p = constraints; i < n; i++, p += strlen(p) + 1) {
9624845Svikram array[i] = i_ddi_strdup(p, KM_SLEEP);
9634845Svikram }
9644845Svikram array[n] = NULL;
9654845Svikram
9664845Svikram kmem_free(constraints, len);
9674845Svikram
9684845Svikram return (array);
9694845Svikram }
9704845Svikram /*ARGSUSED*/
9714845Svikram static int
modctl_retire(char * path,char * uconstraints,size_t ulen)9724845Svikram modctl_retire(char *path, char *uconstraints, size_t ulen)
9734845Svikram {
9744845Svikram char *pathbuf;
9754845Svikram char *devpath;
9764845Svikram size_t pathsz;
9774845Svikram int retval;
9784845Svikram char *constraints;
9794845Svikram char **cons_array;
9804845Svikram
9814845Svikram if (path == NULL)
9824845Svikram return (EINVAL);
9834845Svikram
9844845Svikram if ((uconstraints == NULL) ^ (ulen == 0))
9854845Svikram return (EINVAL);
9864845Svikram
9874845Svikram pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9884845Svikram retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
9894845Svikram if (retval != 0) {
9904845Svikram kmem_free(pathbuf, MAXPATHLEN);
9914845Svikram return (retval);
9924845Svikram }
9934845Svikram devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
9944845Svikram kmem_free(pathbuf, MAXPATHLEN);
9954845Svikram
9964845Svikram /*
9974845Svikram * First check if the device is already retired.
99812107SStephen.Hanson@Sun.COM * If it is, then persist the retire anyway, just in case the retire
99912107SStephen.Hanson@Sun.COM * store has got out of sync with the boot archive.
10004845Svikram */
10014845Svikram if (e_ddi_device_retired(devpath)) {
10024845Svikram cmn_err(CE_NOTE, "Device: already retired: %s", devpath);
100312107SStephen.Hanson@Sun.COM (void) e_ddi_retire_persist(devpath);
10044845Svikram kmem_free(devpath, strlen(devpath) + 1);
10054845Svikram return (0);
10064845Svikram }
10074845Svikram
10084845Svikram cons_array = NULL;
10094845Svikram if (uconstraints) {
10104845Svikram constraints = kmem_alloc(ulen, KM_SLEEP);
10114845Svikram if (copyin(uconstraints, constraints, ulen)) {
10124845Svikram kmem_free(constraints, ulen);
10134845Svikram kmem_free(devpath, strlen(devpath) + 1);
10144845Svikram return (EFAULT);
10154845Svikram }
10164845Svikram cons_array = convert_constraint_string(constraints, ulen);
10174845Svikram }
10184845Svikram
10194845Svikram /*
10204845Svikram * Try to retire the device first. The following
10214845Svikram * routine will return an error only if the device
10224845Svikram * is not retireable i.e. retire constraints forbid
10234845Svikram * a retire. A return of success from this routine
10244845Svikram * indicates that device is retireable.
10254845Svikram */
10264845Svikram retval = e_ddi_retire_device(devpath, cons_array);
10274845Svikram if (retval != DDI_SUCCESS) {
10284845Svikram cmn_err(CE_WARN, "constraints forbid retire: %s", devpath);
10294845Svikram kmem_free(devpath, strlen(devpath) + 1);
10304845Svikram return (ENOTSUP);
10314845Svikram }
10324845Svikram
10334845Svikram /*
10344845Svikram * Ok, the retire succeeded. Persist the retire.
10354845Svikram * If retiring a nexus, we need to only persist the
10364845Svikram * nexus retire. Any children of a retired nexus
10374845Svikram * are automatically covered by the retire store
10384845Svikram * code.
10394845Svikram */
10404845Svikram retval = e_ddi_retire_persist(devpath);
10414845Svikram if (retval != 0) {
10424845Svikram cmn_err(CE_WARN, "Failed to persist device retire: error %d: "
10434845Svikram "%s", retval, devpath);
10444845Svikram kmem_free(devpath, strlen(devpath) + 1);
10454845Svikram return (retval);
10464845Svikram }
10474845Svikram if (moddebug & MODDEBUG_RETIRE)
10484845Svikram cmn_err(CE_NOTE, "Persisted retire of device: %s", devpath);
10494845Svikram
10504845Svikram kmem_free(devpath, strlen(devpath) + 1);
10514845Svikram return (0);
10524845Svikram }
10534845Svikram
10544845Svikram static int
modctl_is_retired(char * path,int * statep)10554845Svikram modctl_is_retired(char *path, int *statep)
10564845Svikram {
10574845Svikram char *pathbuf;
10584845Svikram char *devpath;
10594845Svikram size_t pathsz;
10604845Svikram int error;
10614845Svikram int status;
10624845Svikram
10634845Svikram if (path == NULL || statep == NULL)
10644845Svikram return (EINVAL);
10654845Svikram
10664845Svikram pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
10674845Svikram error = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
10684845Svikram if (error != 0) {
10694845Svikram kmem_free(pathbuf, MAXPATHLEN);
10704845Svikram return (error);
10714845Svikram }
10724845Svikram devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
10734845Svikram kmem_free(pathbuf, MAXPATHLEN);
10744845Svikram
10754845Svikram if (e_ddi_device_retired(devpath))
10764845Svikram status = 1;
10774845Svikram else
10784845Svikram status = 0;
10794845Svikram kmem_free(devpath, strlen(devpath) + 1);
10804845Svikram
10814845Svikram return (copyout(&status, statep, sizeof (status)) ? EFAULT : 0);
10824845Svikram }
10834845Svikram
10844845Svikram static int
modctl_unretire(char * path)10854845Svikram modctl_unretire(char *path)
10864845Svikram {
10874845Svikram char *pathbuf;
10884845Svikram char *devpath;
10894845Svikram size_t pathsz;
10904845Svikram int retired;
10914845Svikram int retval;
10924845Svikram
10934845Svikram if (path == NULL)
10944845Svikram return (EINVAL);
10954845Svikram
10964845Svikram pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
10974845Svikram retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
10984845Svikram if (retval != 0) {
10994845Svikram kmem_free(pathbuf, MAXPATHLEN);
11004845Svikram return (retval);
11014845Svikram }
11024845Svikram devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
11034845Svikram kmem_free(pathbuf, MAXPATHLEN);
11044845Svikram
11054845Svikram /*
11064845Svikram * We check if a device is retired (first) before
11074845Svikram * unpersisting the retire, because we use the
11084845Svikram * retire store to determine if a device is retired.
11094845Svikram * If we unpersist first, the device will always appear
11104845Svikram * to be unretired. For the rationale behind unpersisting
11114845Svikram * a device that is not retired, see the next comment.
11124845Svikram */
11134845Svikram retired = e_ddi_device_retired(devpath);
11144845Svikram
11154845Svikram /*
11164845Svikram * We call unpersist unconditionally because the lookup
11174845Svikram * for retired devices (e_ddi_device_retired()), skips "bypassed"
11184845Svikram * devices. We still want to be able remove "bypassed" entries
11194845Svikram * from the persistent store, so we unpersist unconditionally
11204845Svikram * i.e. whether or not the entry is found on a lookup.
11214845Svikram *
11224845Svikram * e_ddi_retire_unpersist() returns 1 if it found and cleared
11234845Svikram * an entry from the retire store or 0 otherwise.
11244845Svikram */
11254845Svikram if (e_ddi_retire_unpersist(devpath))
11264845Svikram if (moddebug & MODDEBUG_RETIRE) {
11274845Svikram cmn_err(CE_NOTE, "Unpersisted retire of device: %s",
11284845Svikram devpath);
11294845Svikram }
11304845Svikram
11314845Svikram /*
11324845Svikram * Check if the device is already unretired. If so,
11334845Svikram * the unretire becomes a NOP
11344845Svikram */
11354845Svikram if (!retired) {
11364845Svikram cmn_err(CE_NOTE, "Not retired: %s", devpath);
11374845Svikram kmem_free(devpath, strlen(devpath) + 1);
11384845Svikram return (0);
11394845Svikram }
11404845Svikram
11414845Svikram retval = e_ddi_unretire_device(devpath);
11424845Svikram if (retval != 0) {
11434845Svikram cmn_err(CE_WARN, "cannot unretire device: error %d, path %s\n",
11444845Svikram retval, devpath);
11454845Svikram }
11464845Svikram
11474845Svikram kmem_free(devpath, strlen(devpath) + 1);
11484845Svikram
11494845Svikram return (retval);
11504845Svikram }
11514845Svikram
11520Sstevel@tonic-gate static int
modctl_getname(char * uname,uint_t ulen,int * umajorp)11530Sstevel@tonic-gate modctl_getname(char *uname, uint_t ulen, int *umajorp)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate char *name;
11560Sstevel@tonic-gate major_t major;
11570Sstevel@tonic-gate
11580Sstevel@tonic-gate if (copyin(umajorp, &major, sizeof (major)) != 0)
11590Sstevel@tonic-gate return (EFAULT);
11600Sstevel@tonic-gate if ((name = mod_major_to_name(major)) == NULL)
11610Sstevel@tonic-gate return (ENODEV);
11620Sstevel@tonic-gate if ((strlen(name) + 1) > ulen)
11630Sstevel@tonic-gate return (ENOSPC);
11640Sstevel@tonic-gate return (copyoutstr(name, uname, ulen, NULL));
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate static int
modctl_devt2instance(dev_t dev,int * uinstancep)11680Sstevel@tonic-gate modctl_devt2instance(dev_t dev, int *uinstancep)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate int instance;
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate if ((instance = dev_to_instance(dev)) == -1)
11730Sstevel@tonic-gate return (EINVAL);
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate return (copyout(&instance, uinstancep, sizeof (int)));
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate * Return the sizeof of the device id.
11800Sstevel@tonic-gate */
11810Sstevel@tonic-gate static int
modctl_sizeof_devid(dev_t dev,uint_t * len)11820Sstevel@tonic-gate modctl_sizeof_devid(dev_t dev, uint_t *len)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate uint_t sz;
11850Sstevel@tonic-gate ddi_devid_t devid;
11860Sstevel@tonic-gate
11870Sstevel@tonic-gate /* get device id */
11880Sstevel@tonic-gate if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
11890Sstevel@tonic-gate return (EINVAL);
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate sz = ddi_devid_sizeof(devid);
11920Sstevel@tonic-gate ddi_devid_free(devid);
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate /* copyout device id size */
11950Sstevel@tonic-gate if (copyout(&sz, len, sizeof (sz)) != 0)
11960Sstevel@tonic-gate return (EFAULT);
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate return (0);
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate /*
12020Sstevel@tonic-gate * Return a copy of the device id.
12030Sstevel@tonic-gate */
12040Sstevel@tonic-gate static int
modctl_get_devid(dev_t dev,uint_t len,ddi_devid_t udevid)12050Sstevel@tonic-gate modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
12060Sstevel@tonic-gate {
12070Sstevel@tonic-gate uint_t sz;
12080Sstevel@tonic-gate ddi_devid_t devid;
12090Sstevel@tonic-gate int err = 0;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate /* get device id */
12120Sstevel@tonic-gate if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
12130Sstevel@tonic-gate return (EINVAL);
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate sz = ddi_devid_sizeof(devid);
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate /* Error if device id is larger than space allocated */
12180Sstevel@tonic-gate if (sz > len) {
12190Sstevel@tonic-gate ddi_devid_free(devid);
12200Sstevel@tonic-gate return (ENOSPC);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate
12230Sstevel@tonic-gate /* copy out device id */
12240Sstevel@tonic-gate if (copyout(devid, udevid, sz) != 0)
12250Sstevel@tonic-gate err = EFAULT;
12260Sstevel@tonic-gate ddi_devid_free(devid);
12270Sstevel@tonic-gate return (err);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate * return the /devices paths associated with the specified devid and
12320Sstevel@tonic-gate * minor name.
12330Sstevel@tonic-gate */
12340Sstevel@tonic-gate /*ARGSUSED*/
12350Sstevel@tonic-gate static int
modctl_devid2paths(ddi_devid_t udevid,char * uminor_name,uint_t flag,size_t * ulensp,char * upaths)12360Sstevel@tonic-gate modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
12370Sstevel@tonic-gate size_t *ulensp, char *upaths)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate ddi_devid_t devid = NULL;
12400Sstevel@tonic-gate int devid_len;
12410Sstevel@tonic-gate char *minor_name = NULL;
12420Sstevel@tonic-gate dev_info_t *dip = NULL;
12437224Scth int circ;
124410923SEvan.Yan@Sun.COM struct ddi_minor_data *dmdp;
12450Sstevel@tonic-gate char *path = NULL;
12460Sstevel@tonic-gate int ulens;
12470Sstevel@tonic-gate int lens;
12480Sstevel@tonic-gate int len;
12490Sstevel@tonic-gate dev_t *devlist = NULL;
12500Sstevel@tonic-gate int ndevs;
12510Sstevel@tonic-gate int i;
12520Sstevel@tonic-gate int ret = 0;
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate /*
12550Sstevel@tonic-gate * If upaths is NULL then we are only computing the amount of space
12560Sstevel@tonic-gate * needed to hold the paths and returning the value in *ulensp. If we
12570Sstevel@tonic-gate * are copying out paths then we get the amount of space allocated by
12580Sstevel@tonic-gate * the caller. If the actual space needed for paths is larger, or
12590Sstevel@tonic-gate * things are changing out from under us, then we return EAGAIN.
12600Sstevel@tonic-gate */
12610Sstevel@tonic-gate if (upaths) {
12620Sstevel@tonic-gate if (ulensp == NULL)
12630Sstevel@tonic-gate return (EINVAL);
12640Sstevel@tonic-gate if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
12650Sstevel@tonic-gate return (EFAULT);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate /*
12690Sstevel@tonic-gate * copyin enough of the devid to determine the length then
12700Sstevel@tonic-gate * reallocate and copy in the entire devid.
12710Sstevel@tonic-gate */
12720Sstevel@tonic-gate devid_len = ddi_devid_sizeof(NULL);
12730Sstevel@tonic-gate devid = kmem_alloc(devid_len, KM_SLEEP);
12740Sstevel@tonic-gate if (copyin(udevid, devid, devid_len)) {
12750Sstevel@tonic-gate ret = EFAULT;
12760Sstevel@tonic-gate goto out;
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate len = devid_len;
12790Sstevel@tonic-gate devid_len = ddi_devid_sizeof(devid);
12800Sstevel@tonic-gate kmem_free(devid, len);
12810Sstevel@tonic-gate devid = kmem_alloc(devid_len, KM_SLEEP);
12820Sstevel@tonic-gate if (copyin(udevid, devid, devid_len)) {
12830Sstevel@tonic-gate ret = EFAULT;
12840Sstevel@tonic-gate goto out;
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate /* copyin the minor name if specified. */
12880Sstevel@tonic-gate minor_name = uminor_name;
12890Sstevel@tonic-gate if ((minor_name != DEVID_MINOR_NAME_ALL) &&
12900Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
12910Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
12920Sstevel@tonic-gate minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
12930Sstevel@tonic-gate if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
12940Sstevel@tonic-gate ret = EFAULT;
12950Sstevel@tonic-gate goto out;
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate * Use existing function to resolve the devid into a devlist.
13010Sstevel@tonic-gate *
13020Sstevel@tonic-gate * NOTE: there is a loss of spectype information in the current
13030Sstevel@tonic-gate * ddi_lyr_devid_to_devlist implementation. We work around this by not
13040Sstevel@tonic-gate * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
13050Sstevel@tonic-gate * node forms in the loop processing the devlist below. It would be
13060Sstevel@tonic-gate * best if at some point the use of this interface here was replaced
13070Sstevel@tonic-gate * with a path oriented call.
13080Sstevel@tonic-gate */
13090Sstevel@tonic-gate if (ddi_lyr_devid_to_devlist(devid,
13100Sstevel@tonic-gate (minor_name == DEVID_MINOR_NAME_ALL) ?
13110Sstevel@tonic-gate DEVID_MINOR_NAME_ALL_CHR : minor_name,
13120Sstevel@tonic-gate &ndevs, &devlist) != DDI_SUCCESS) {
13130Sstevel@tonic-gate ret = EINVAL;
13140Sstevel@tonic-gate goto out;
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate /*
13180Sstevel@tonic-gate * loop over the devlist, converting each devt to a path and doing
13190Sstevel@tonic-gate * a copyout of the path and computation of the amount of space
13200Sstevel@tonic-gate * needed to hold all the paths
13210Sstevel@tonic-gate */
13220Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13230Sstevel@tonic-gate for (i = 0, lens = 0; i < ndevs; i++) {
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate /* find the dip associated with the dev_t */
13260Sstevel@tonic-gate if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
13270Sstevel@tonic-gate continue;
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate /* loop over all the minor nodes, skipping ones we don't want */
13307224Scth ndi_devi_enter(dip, &circ);
13310Sstevel@tonic-gate for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
13320Sstevel@tonic-gate if ((dmdp->ddm_dev != devlist[i]) ||
13330Sstevel@tonic-gate (dmdp->type != DDM_MINOR))
13340Sstevel@tonic-gate continue;
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate if ((minor_name != DEVID_MINOR_NAME_ALL) &&
13370Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
13380Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
13390Sstevel@tonic-gate strcmp(minor_name, dmdp->ddm_name))
13400Sstevel@tonic-gate continue;
13410Sstevel@tonic-gate else {
13420Sstevel@tonic-gate if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
13430Sstevel@tonic-gate (dmdp->ddm_spec_type != S_IFCHR))
13440Sstevel@tonic-gate continue;
13450Sstevel@tonic-gate if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
13460Sstevel@tonic-gate (dmdp->ddm_spec_type != S_IFBLK))
13470Sstevel@tonic-gate continue;
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate
13507627SChris.Horne@Sun.COM (void) ddi_pathname_minor(dmdp, path);
13510Sstevel@tonic-gate len = strlen(path) + 1;
13520Sstevel@tonic-gate *(path + len) = '\0'; /* set double termination */
13530Sstevel@tonic-gate lens += len;
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate /* copyout the path with double terminations */
13560Sstevel@tonic-gate if (upaths) {
13570Sstevel@tonic-gate if (lens > ulens) {
13580Sstevel@tonic-gate ret = EAGAIN;
13590Sstevel@tonic-gate goto out;
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate if (copyout(path, upaths, len + 1)) {
13620Sstevel@tonic-gate ret = EFAULT;
13630Sstevel@tonic-gate goto out;
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate upaths += len;
13660Sstevel@tonic-gate }
13670Sstevel@tonic-gate }
13687224Scth ndi_devi_exit(dip, circ);
13690Sstevel@tonic-gate ddi_release_devi(dip);
13700Sstevel@tonic-gate dip = NULL;
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate lens++; /* add one for double termination */
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate /* copy out the amount of space needed to hold the paths */
13750Sstevel@tonic-gate if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
13760Sstevel@tonic-gate ret = EFAULT;
13770Sstevel@tonic-gate goto out;
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate ret = 0;
13800Sstevel@tonic-gate
13817224Scth out: if (dip) {
13827224Scth ndi_devi_exit(dip, circ);
13830Sstevel@tonic-gate ddi_release_devi(dip);
13847224Scth }
13850Sstevel@tonic-gate if (path)
13860Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
13870Sstevel@tonic-gate if (devlist)
13880Sstevel@tonic-gate ddi_lyr_free_devlist(devlist, ndevs);
13890Sstevel@tonic-gate if (minor_name &&
13900Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL) &&
13910Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
13920Sstevel@tonic-gate (minor_name != DEVID_MINOR_NAME_ALL_BLK))
13930Sstevel@tonic-gate kmem_free(minor_name, MAXPATHLEN);
13940Sstevel@tonic-gate if (devid)
13950Sstevel@tonic-gate kmem_free(devid, devid_len);
13960Sstevel@tonic-gate return (ret);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate /*
14000Sstevel@tonic-gate * Return the size of the minor name.
14010Sstevel@tonic-gate */
14020Sstevel@tonic-gate static int
modctl_sizeof_minorname(dev_t dev,int spectype,uint_t * len)14030Sstevel@tonic-gate modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate uint_t sz;
14060Sstevel@tonic-gate char *name;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /* get the minor name */
14090Sstevel@tonic-gate if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
14100Sstevel@tonic-gate return (EINVAL);
14110Sstevel@tonic-gate
14120Sstevel@tonic-gate sz = strlen(name) + 1;
14130Sstevel@tonic-gate kmem_free(name, sz);
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate /* copy out the size of the minor name */
14160Sstevel@tonic-gate if (copyout(&sz, len, sizeof (sz)) != 0)
14170Sstevel@tonic-gate return (EFAULT);
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate return (0);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate * Return the minor name.
14240Sstevel@tonic-gate */
14250Sstevel@tonic-gate static int
modctl_get_minorname(dev_t dev,int spectype,uint_t len,char * uname)14260Sstevel@tonic-gate modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate uint_t sz;
14290Sstevel@tonic-gate char *name;
14300Sstevel@tonic-gate int err = 0;
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate /* get the minor name */
14330Sstevel@tonic-gate if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
14340Sstevel@tonic-gate return (EINVAL);
14350Sstevel@tonic-gate
14360Sstevel@tonic-gate sz = strlen(name) + 1;
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate /* Error if the minor name is larger than the space allocated */
14390Sstevel@tonic-gate if (sz > len) {
14400Sstevel@tonic-gate kmem_free(name, sz);
14410Sstevel@tonic-gate return (ENOSPC);
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate /* copy out the minor name */
14450Sstevel@tonic-gate if (copyout(name, uname, sz) != 0)
14460Sstevel@tonic-gate err = EFAULT;
14470Sstevel@tonic-gate kmem_free(name, sz);
14480Sstevel@tonic-gate return (err);
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate /*
14522723Scth * Return the size of the (dev_t,spectype) devfspath name.
14530Sstevel@tonic-gate */
14540Sstevel@tonic-gate static int
modctl_devfspath_len(dev_t dev,int spectype,uint_t * len)14550Sstevel@tonic-gate modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate uint_t sz;
14580Sstevel@tonic-gate char *name;
14590Sstevel@tonic-gate
14600Sstevel@tonic-gate /* get the path name */
14610Sstevel@tonic-gate name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14620Sstevel@tonic-gate if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
14630Sstevel@tonic-gate kmem_free(name, MAXPATHLEN);
14640Sstevel@tonic-gate return (EINVAL);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate
14670Sstevel@tonic-gate sz = strlen(name) + 1;
14680Sstevel@tonic-gate kmem_free(name, MAXPATHLEN);
14690Sstevel@tonic-gate
14700Sstevel@tonic-gate /* copy out the size of the path name */
14710Sstevel@tonic-gate if (copyout(&sz, len, sizeof (sz)) != 0)
14720Sstevel@tonic-gate return (EFAULT);
14730Sstevel@tonic-gate
14740Sstevel@tonic-gate return (0);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate
14770Sstevel@tonic-gate /*
14782723Scth * Return the (dev_t,spectype) devfspath name.
14790Sstevel@tonic-gate */
14800Sstevel@tonic-gate static int
modctl_devfspath(dev_t dev,int spectype,uint_t len,char * uname)14810Sstevel@tonic-gate modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate uint_t sz;
14840Sstevel@tonic-gate char *name;
14850Sstevel@tonic-gate int err = 0;
14860Sstevel@tonic-gate
14870Sstevel@tonic-gate /* get the path name */
14880Sstevel@tonic-gate name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14890Sstevel@tonic-gate if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
14900Sstevel@tonic-gate kmem_free(name, MAXPATHLEN);
14910Sstevel@tonic-gate return (EINVAL);
14920Sstevel@tonic-gate }
14930Sstevel@tonic-gate
14940Sstevel@tonic-gate sz = strlen(name) + 1;
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate /* Error if the path name is larger than the space allocated */
14970Sstevel@tonic-gate if (sz > len) {
14980Sstevel@tonic-gate kmem_free(name, MAXPATHLEN);
14990Sstevel@tonic-gate return (ENOSPC);
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate
15020Sstevel@tonic-gate /* copy out the path name */
15030Sstevel@tonic-gate if (copyout(name, uname, sz) != 0)
15040Sstevel@tonic-gate err = EFAULT;
15050Sstevel@tonic-gate kmem_free(name, MAXPATHLEN);
15060Sstevel@tonic-gate return (err);
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate
15092723Scth /*
15102723Scth * Return the size of the (major,instance) devfspath name.
15112723Scth */
15122723Scth static int
modctl_devfspath_mi_len(major_t major,int instance,uint_t * len)15132723Scth modctl_devfspath_mi_len(major_t major, int instance, uint_t *len)
15142723Scth {
15152723Scth uint_t sz;
15162723Scth char *name;
15172723Scth
15182723Scth /* get the path name */
15192723Scth name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15202723Scth if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
15212723Scth kmem_free(name, MAXPATHLEN);
15222723Scth return (EINVAL);
15232723Scth }
15242723Scth
15252723Scth sz = strlen(name) + 1;
15262723Scth kmem_free(name, MAXPATHLEN);
15272723Scth
15282723Scth /* copy out the size of the path name */
15292723Scth if (copyout(&sz, len, sizeof (sz)) != 0)
15302723Scth return (EFAULT);
15312723Scth
15322723Scth return (0);
15332723Scth }
15342723Scth
15352723Scth /*
15362723Scth * Return the (major_instance) devfspath name.
15372723Scth * NOTE: e_ddi_majorinstance_to_path does not require the device to attach to
15382723Scth * return a path - it uses the instance tree.
15392723Scth */
15402723Scth static int
modctl_devfspath_mi(major_t major,int instance,uint_t len,char * uname)15412723Scth modctl_devfspath_mi(major_t major, int instance, uint_t len, char *uname)
15422723Scth {
15432723Scth uint_t sz;
15442723Scth char *name;
15452723Scth int err = 0;
15462723Scth
15472723Scth /* get the path name */
15482723Scth name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15492723Scth if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
15502723Scth kmem_free(name, MAXPATHLEN);
15512723Scth return (EINVAL);
15522723Scth }
15532723Scth
15542723Scth sz = strlen(name) + 1;
15552723Scth
15562723Scth /* Error if the path name is larger than the space allocated */
15572723Scth if (sz > len) {
15582723Scth kmem_free(name, MAXPATHLEN);
15592723Scth return (ENOSPC);
15602723Scth }
15612723Scth
15622723Scth /* copy out the path name */
15632723Scth if (copyout(name, uname, sz) != 0)
15642723Scth err = EFAULT;
15652723Scth kmem_free(name, MAXPATHLEN);
15662723Scth return (err);
15672723Scth }
15682723Scth
15690Sstevel@tonic-gate static int
modctl_get_fbname(char * path)15700Sstevel@tonic-gate modctl_get_fbname(char *path)
15710Sstevel@tonic-gate {
15720Sstevel@tonic-gate extern dev_t fbdev;
15730Sstevel@tonic-gate char *pathname = NULL;
15740Sstevel@tonic-gate int rval = 0;
15750Sstevel@tonic-gate
15760Sstevel@tonic-gate /* make sure fbdev is set before we plunge in */
15770Sstevel@tonic-gate if (fbdev == NODEV)
15780Sstevel@tonic-gate return (ENODEV);
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15810Sstevel@tonic-gate if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
15820Sstevel@tonic-gate pathname)) == DDI_SUCCESS) {
15830Sstevel@tonic-gate if (copyout(pathname, path, strlen(pathname)+1) != 0) {
15840Sstevel@tonic-gate rval = EFAULT;
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN);
15880Sstevel@tonic-gate return (rval);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate * modctl_reread_dacf()
15930Sstevel@tonic-gate * Reread the dacf rules database from the named binding file.
15940Sstevel@tonic-gate * If NULL is specified, pass along the NULL, it means 'use the default'.
15950Sstevel@tonic-gate */
15960Sstevel@tonic-gate static int
modctl_reread_dacf(char * path)15970Sstevel@tonic-gate modctl_reread_dacf(char *path)
15980Sstevel@tonic-gate {
15990Sstevel@tonic-gate int rval = 0;
16000Sstevel@tonic-gate char *filename, *filenamep;
16010Sstevel@tonic-gate
16020Sstevel@tonic-gate filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate if (path == NULL) {
16050Sstevel@tonic-gate filenamep = NULL;
16060Sstevel@tonic-gate } else {
16070Sstevel@tonic-gate if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
16080Sstevel@tonic-gate rval = EFAULT;
16090Sstevel@tonic-gate goto out;
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate filenamep = filename;
16120Sstevel@tonic-gate filenamep[MAXPATHLEN - 1] = '\0';
16130Sstevel@tonic-gate }
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate rval = read_dacf_binding_file(filenamep);
16160Sstevel@tonic-gate out:
16170Sstevel@tonic-gate kmem_free(filename, MAXPATHLEN);
16180Sstevel@tonic-gate return (rval);
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate
16210Sstevel@tonic-gate /*ARGSUSED*/
16220Sstevel@tonic-gate static int
modctl_modevents(int subcmd,uintptr_t a2,uintptr_t a3,uintptr_t a4,uint_t flag)16230Sstevel@tonic-gate modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
16240Sstevel@tonic-gate uint_t flag)
16250Sstevel@tonic-gate {
16260Sstevel@tonic-gate int error = 0;
16270Sstevel@tonic-gate char *filenamep;
16280Sstevel@tonic-gate
16290Sstevel@tonic-gate switch (subcmd) {
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate case MODEVENTS_FLUSH:
16320Sstevel@tonic-gate /* flush all currently queued events */
16330Sstevel@tonic-gate log_sysevent_flushq(subcmd, flag);
16340Sstevel@tonic-gate break;
16350Sstevel@tonic-gate
16360Sstevel@tonic-gate case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
16370Sstevel@tonic-gate /*
16380Sstevel@tonic-gate * bind door_upcall to filename
16390Sstevel@tonic-gate * this should only be done once per invocation
16400Sstevel@tonic-gate * of the event daemon.
16410Sstevel@tonic-gate */
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
16440Sstevel@tonic-gate
16450Sstevel@tonic-gate if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
16460Sstevel@tonic-gate error = EFAULT;
16470Sstevel@tonic-gate } else {
16480Sstevel@tonic-gate error = log_sysevent_filename(filenamep);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate kmem_free(filenamep, MOD_MAXPATH);
16510Sstevel@tonic-gate break;
16520Sstevel@tonic-gate
16530Sstevel@tonic-gate case MODEVENTS_GETDATA:
16540Sstevel@tonic-gate error = log_sysevent_copyout_data((sysevent_id_t *)a2,
16550Sstevel@tonic-gate (size_t)a3, (caddr_t)a4);
16560Sstevel@tonic-gate break;
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate case MODEVENTS_FREEDATA:
16590Sstevel@tonic-gate error = log_sysevent_free_data((sysevent_id_t *)a2);
16600Sstevel@tonic-gate break;
16610Sstevel@tonic-gate case MODEVENTS_POST_EVENT:
16620Sstevel@tonic-gate error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
16634851Scth (sysevent_id_t *)a4);
16640Sstevel@tonic-gate break;
16650Sstevel@tonic-gate case MODEVENTS_REGISTER_EVENT:
16660Sstevel@tonic-gate error = log_sysevent_register((char *)a2, (char *)a3,
16670Sstevel@tonic-gate (se_pubsub_t *)a4);
16680Sstevel@tonic-gate break;
16690Sstevel@tonic-gate default:
16700Sstevel@tonic-gate error = EINVAL;
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate return (error);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate static void
free_mperm(mperm_t * mp)16770Sstevel@tonic-gate free_mperm(mperm_t *mp)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate int len;
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate if (mp->mp_minorname) {
16820Sstevel@tonic-gate len = strlen(mp->mp_minorname) + 1;
16830Sstevel@tonic-gate kmem_free(mp->mp_minorname, len);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate kmem_free(mp, sizeof (mperm_t));
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate
16880Sstevel@tonic-gate #define MP_NO_DRV_ERR \
16890Sstevel@tonic-gate "/etc/minor_perm: no driver for %s\n"
16900Sstevel@tonic-gate
16910Sstevel@tonic-gate #define MP_EMPTY_MINOR \
16920Sstevel@tonic-gate "/etc/minor_perm: empty minor name for driver %s\n"
16930Sstevel@tonic-gate
16940Sstevel@tonic-gate #define MP_NO_MINOR \
16950Sstevel@tonic-gate "/etc/minor_perm: no minor matching %s for driver %s\n"
16960Sstevel@tonic-gate
16970Sstevel@tonic-gate /*
16980Sstevel@tonic-gate * Remove mperm entry with matching minorname
16990Sstevel@tonic-gate */
17000Sstevel@tonic-gate static void
rem_minorperm(major_t major,char * drvname,mperm_t * mp,int is_clone)17010Sstevel@tonic-gate rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate mperm_t **mp_head;
17040Sstevel@tonic-gate mperm_t *freemp = NULL;
17050Sstevel@tonic-gate struct devnames *dnp = &devnamesp[major];
17060Sstevel@tonic-gate mperm_t **wildmp;
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
17090Sstevel@tonic-gate
17100Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
17110Sstevel@tonic-gate if (strcmp(mp->mp_minorname, "*") == 0) {
17120Sstevel@tonic-gate wildmp = ((is_clone == 0) ?
17134851Scth &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
17140Sstevel@tonic-gate if (*wildmp)
17150Sstevel@tonic-gate freemp = *wildmp;
17160Sstevel@tonic-gate *wildmp = NULL;
17170Sstevel@tonic-gate } else {
17180Sstevel@tonic-gate mp_head = &dnp->dn_mperm;
17190Sstevel@tonic-gate while (*mp_head) {
17200Sstevel@tonic-gate if (strcmp((*mp_head)->mp_minorname,
17210Sstevel@tonic-gate mp->mp_minorname) != 0) {
17220Sstevel@tonic-gate mp_head = &(*mp_head)->mp_next;
17230Sstevel@tonic-gate continue;
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate /* remove the entry */
17260Sstevel@tonic-gate freemp = *mp_head;
17270Sstevel@tonic-gate *mp_head = freemp->mp_next;
17280Sstevel@tonic-gate break;
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate if (freemp) {
17320Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
17330Sstevel@tonic-gate cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
17340Sstevel@tonic-gate drvname, freemp->mp_minorname,
17350Sstevel@tonic-gate freemp->mp_mode & 0777,
17360Sstevel@tonic-gate freemp->mp_uid, freemp->mp_gid);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate free_mperm(freemp);
17390Sstevel@tonic-gate } else {
17400Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
17410Sstevel@tonic-gate cmn_err(CE_CONT, MP_NO_MINOR,
17424851Scth drvname, mp->mp_minorname);
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
17470Sstevel@tonic-gate }
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate /*
17500Sstevel@tonic-gate * Add minor perm entry
17510Sstevel@tonic-gate */
17520Sstevel@tonic-gate static void
add_minorperm(major_t major,char * drvname,mperm_t * mp,int is_clone)17530Sstevel@tonic-gate add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
17540Sstevel@tonic-gate {
17550Sstevel@tonic-gate mperm_t **mp_head;
17560Sstevel@tonic-gate mperm_t *freemp = NULL;
17570Sstevel@tonic-gate struct devnames *dnp = &devnamesp[major];
17580Sstevel@tonic-gate mperm_t **wildmp;
17590Sstevel@tonic-gate
17600Sstevel@tonic-gate ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
17610Sstevel@tonic-gate
17620Sstevel@tonic-gate /*
17630Sstevel@tonic-gate * Note that update_drv replace semantics require
17640Sstevel@tonic-gate * replacing matching entries with the new permissions.
17650Sstevel@tonic-gate */
17660Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
17670Sstevel@tonic-gate if (strcmp(mp->mp_minorname, "*") == 0) {
17680Sstevel@tonic-gate wildmp = ((is_clone == 0) ?
17694851Scth &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
17700Sstevel@tonic-gate if (*wildmp)
17710Sstevel@tonic-gate freemp = *wildmp;
17720Sstevel@tonic-gate *wildmp = mp;
17730Sstevel@tonic-gate } else {
17740Sstevel@tonic-gate mperm_t *p, *v = NULL;
17750Sstevel@tonic-gate for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
17760Sstevel@tonic-gate if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
17770Sstevel@tonic-gate if (v == NULL)
17780Sstevel@tonic-gate dnp->dn_mperm = mp;
17790Sstevel@tonic-gate else
17800Sstevel@tonic-gate v->mp_next = mp;
17810Sstevel@tonic-gate mp->mp_next = p->mp_next;
17820Sstevel@tonic-gate freemp = p;
17830Sstevel@tonic-gate goto replaced;
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate if (p == NULL) {
17870Sstevel@tonic-gate mp_head = &dnp->dn_mperm;
17880Sstevel@tonic-gate if (*mp_head == NULL) {
17890Sstevel@tonic-gate *mp_head = mp;
17900Sstevel@tonic-gate } else {
17910Sstevel@tonic-gate mp->mp_next = *mp_head;
17920Sstevel@tonic-gate *mp_head = mp;
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate replaced:
17970Sstevel@tonic-gate if (freemp) {
17980Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
17990Sstevel@tonic-gate cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
18000Sstevel@tonic-gate drvname, freemp->mp_minorname,
18010Sstevel@tonic-gate freemp->mp_mode & 0777,
18020Sstevel@tonic-gate freemp->mp_uid, freemp->mp_gid);
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate free_mperm(freemp);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
18070Sstevel@tonic-gate cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
18080Sstevel@tonic-gate drvname, mp->mp_minorname, mp->mp_mode & 0777,
18090Sstevel@tonic-gate mp->mp_uid, mp->mp_gid);
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate
18140Sstevel@tonic-gate
18150Sstevel@tonic-gate static int
process_minorperm(int cmd,nvlist_t * nvl)18160Sstevel@tonic-gate process_minorperm(int cmd, nvlist_t *nvl)
18170Sstevel@tonic-gate {
18180Sstevel@tonic-gate char *minor;
18190Sstevel@tonic-gate major_t major;
18200Sstevel@tonic-gate mperm_t *mp;
18210Sstevel@tonic-gate nvpair_t *nvp;
18220Sstevel@tonic-gate char *name;
18230Sstevel@tonic-gate int is_clone;
18240Sstevel@tonic-gate major_t minmaj;
18250Sstevel@tonic-gate
18260Sstevel@tonic-gate ASSERT(cmd == MODLOADMINORPERM ||
18270Sstevel@tonic-gate cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate nvp = NULL;
18300Sstevel@tonic-gate while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
18310Sstevel@tonic-gate name = nvpair_name(nvp);
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate is_clone = 0;
18340Sstevel@tonic-gate (void) nvpair_value_string(nvp, &minor);
18350Sstevel@tonic-gate major = ddi_name_to_major(name);
18367009Scth if (major != DDI_MAJOR_T_NONE) {
18370Sstevel@tonic-gate mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
18380Sstevel@tonic-gate if (minor == NULL || strlen(minor) == 0) {
18390Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
18400Sstevel@tonic-gate cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
18410Sstevel@tonic-gate }
18420Sstevel@tonic-gate minor = "*";
18430Sstevel@tonic-gate }
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate /*
18460Sstevel@tonic-gate * The minor name of a node using the clone
18470Sstevel@tonic-gate * driver must be the driver name. To avoid
18480Sstevel@tonic-gate * multiple searches, we map entries in the form
18490Sstevel@tonic-gate * clone:<driver> to <driver>:*. This also allows us
18500Sstevel@tonic-gate * to filter out some of the litter in /etc/minor_perm.
18510Sstevel@tonic-gate * Minor perm alias entries where the name is not
18520Sstevel@tonic-gate * the driver kept on the clone list itself.
18530Sstevel@tonic-gate * This all seems very fragile as a driver could
18540Sstevel@tonic-gate * be introduced with an existing alias name.
18550Sstevel@tonic-gate */
18560Sstevel@tonic-gate if (strcmp(name, "clone") == 0) {
18570Sstevel@tonic-gate minmaj = ddi_name_to_major(minor);
18587009Scth if (minmaj != DDI_MAJOR_T_NONE) {
18590Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
18600Sstevel@tonic-gate cmn_err(CE_CONT,
18610Sstevel@tonic-gate "mapping %s:%s to %s:*\n",
18620Sstevel@tonic-gate name, minor, minor);
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate major = minmaj;
18650Sstevel@tonic-gate name = minor;
18660Sstevel@tonic-gate minor = "*";
18670Sstevel@tonic-gate is_clone = 1;
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate
18710Sstevel@tonic-gate if (mp) {
18720Sstevel@tonic-gate mp->mp_minorname =
18730Sstevel@tonic-gate i_ddi_strdup(minor, KM_SLEEP);
18740Sstevel@tonic-gate }
18750Sstevel@tonic-gate } else {
18760Sstevel@tonic-gate mp = NULL;
18770Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM) {
18780Sstevel@tonic-gate cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate }
18810Sstevel@tonic-gate
18820Sstevel@tonic-gate /* mode */
18830Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp);
18840Sstevel@tonic-gate ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
18850Sstevel@tonic-gate if (mp)
18860Sstevel@tonic-gate (void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
18870Sstevel@tonic-gate /* uid */
18880Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp);
18890Sstevel@tonic-gate ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
18900Sstevel@tonic-gate if (mp)
18914321Scasper (void) nvpair_value_uint32(nvp, &mp->mp_uid);
18920Sstevel@tonic-gate /* gid */
18930Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp);
18940Sstevel@tonic-gate ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
18950Sstevel@tonic-gate if (mp) {
18964321Scasper (void) nvpair_value_uint32(nvp, &mp->mp_gid);
18970Sstevel@tonic-gate
18980Sstevel@tonic-gate if (cmd == MODREMMINORPERM) {
18990Sstevel@tonic-gate rem_minorperm(major, name, mp, is_clone);
19000Sstevel@tonic-gate free_mperm(mp);
19010Sstevel@tonic-gate } else {
19020Sstevel@tonic-gate add_minorperm(major, name, mp, is_clone);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate
19070Sstevel@tonic-gate if (cmd == MODLOADMINORPERM)
19080Sstevel@tonic-gate minorperm_loaded = 1;
19090Sstevel@tonic-gate
19100Sstevel@tonic-gate /*
19110Sstevel@tonic-gate * Reset permissions of cached dv_nodes
19120Sstevel@tonic-gate */
19130Sstevel@tonic-gate (void) devfs_reset_perm(DV_RESET_PERM);
19140Sstevel@tonic-gate
19150Sstevel@tonic-gate return (0);
19160Sstevel@tonic-gate }
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate static int
modctl_minorperm(int cmd,char * usrbuf,size_t buflen)19190Sstevel@tonic-gate modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
19200Sstevel@tonic-gate {
19210Sstevel@tonic-gate int error;
19220Sstevel@tonic-gate nvlist_t *nvl;
19230Sstevel@tonic-gate char *buf = kmem_alloc(buflen, KM_SLEEP);
19240Sstevel@tonic-gate
19250Sstevel@tonic-gate if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
19260Sstevel@tonic-gate kmem_free(buf, buflen);
19270Sstevel@tonic-gate return (error);
19280Sstevel@tonic-gate }
19290Sstevel@tonic-gate
19300Sstevel@tonic-gate error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
19310Sstevel@tonic-gate kmem_free(buf, buflen);
19320Sstevel@tonic-gate if (error)
19330Sstevel@tonic-gate return (error);
19340Sstevel@tonic-gate
19350Sstevel@tonic-gate error = process_minorperm(cmd, nvl);
19360Sstevel@tonic-gate nvlist_free(nvl);
19370Sstevel@tonic-gate return (error);
19380Sstevel@tonic-gate }
19390Sstevel@tonic-gate
19400Sstevel@tonic-gate struct walk_args {
19410Sstevel@tonic-gate char *wa_drvname;
19420Sstevel@tonic-gate list_t wa_pathlist;
19430Sstevel@tonic-gate };
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate struct path_elem {
19460Sstevel@tonic-gate char *pe_dir;
19470Sstevel@tonic-gate char *pe_nodename;
19480Sstevel@tonic-gate list_node_t pe_node;
19490Sstevel@tonic-gate int pe_dirlen;
19500Sstevel@tonic-gate };
19510Sstevel@tonic-gate
19520Sstevel@tonic-gate /*ARGSUSED*/
19530Sstevel@tonic-gate static int
modctl_inst_walker(const char * path,in_node_t * np,in_drv_t * dp,void * arg)19540Sstevel@tonic-gate modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
19550Sstevel@tonic-gate {
19560Sstevel@tonic-gate struct walk_args *wargs = (struct walk_args *)arg;
19570Sstevel@tonic-gate struct path_elem *pe;
19580Sstevel@tonic-gate char *nodename;
19590Sstevel@tonic-gate
19604870Sjg /*
19614870Sjg * Search may be restricted to a single driver in the case of rem_drv
19624870Sjg */
19634870Sjg if (wargs->wa_drvname &&
19644870Sjg strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
19650Sstevel@tonic-gate return (INST_WALK_CONTINUE);
19660Sstevel@tonic-gate
19670Sstevel@tonic-gate pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
19680Sstevel@tonic-gate pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
19690Sstevel@tonic-gate pe->pe_dirlen = strlen(pe->pe_dir) + 1;
19700Sstevel@tonic-gate ASSERT(strrchr(pe->pe_dir, '/') != NULL);
19710Sstevel@tonic-gate nodename = strrchr(pe->pe_dir, '/');
19720Sstevel@tonic-gate *nodename++ = 0;
19730Sstevel@tonic-gate pe->pe_nodename = nodename;
19740Sstevel@tonic-gate list_insert_tail(&wargs->wa_pathlist, pe);
19750Sstevel@tonic-gate
19760Sstevel@tonic-gate return (INST_WALK_CONTINUE);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate
19794870Sjg /*
19804870Sjg * /devices attribute nodes clean-up optionally performed
19814870Sjg * when removing a driver (rem_drv -C).
19824870Sjg *
19834870Sjg * Removing attribute nodes allows a machine to be reprovisioned
19844870Sjg * without the side-effect of inadvertently picking up stale
19854870Sjg * device node ownership or permissions.
19864870Sjg *
19874870Sjg * Preserving attributes (not performing cleanup) allows devices
19884870Sjg * attribute changes to be preserved across upgrades, as
19894870Sjg * upgrade rather heavy-handedly does a rem_drv/add_drv cycle.
19904870Sjg */
19910Sstevel@tonic-gate static int
modctl_remdrv_cleanup(const char * u_drvname)19920Sstevel@tonic-gate modctl_remdrv_cleanup(const char *u_drvname)
19930Sstevel@tonic-gate {
19940Sstevel@tonic-gate struct walk_args *wargs;
19950Sstevel@tonic-gate struct path_elem *pe;
19960Sstevel@tonic-gate char *drvname;
19970Sstevel@tonic-gate int err, rval = 0;
19980Sstevel@tonic-gate
19990Sstevel@tonic-gate drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
20000Sstevel@tonic-gate if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
20010Sstevel@tonic-gate kmem_free(drvname, MAXMODCONFNAME);
20020Sstevel@tonic-gate return (err);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate
20050Sstevel@tonic-gate /*
20060Sstevel@tonic-gate * First go through the instance database. For each
20070Sstevel@tonic-gate * instance of a device bound to the driver being
20080Sstevel@tonic-gate * removed, remove any underlying devfs attribute nodes.
20090Sstevel@tonic-gate *
201010923SEvan.Yan@Sun.COM * This is a two-step process. First we go through
20110Sstevel@tonic-gate * the instance data itself, constructing a list of
20120Sstevel@tonic-gate * the nodes discovered. The second step is then
20130Sstevel@tonic-gate * to find and remove any devfs attribute nodes
20140Sstevel@tonic-gate * for the instances discovered in the first step.
20150Sstevel@tonic-gate * The two-step process avoids any difficulties
20160Sstevel@tonic-gate * which could arise by holding the instance data
20170Sstevel@tonic-gate * lock with simultaneous devfs operations.
20180Sstevel@tonic-gate */
20190Sstevel@tonic-gate wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
20200Sstevel@tonic-gate
20210Sstevel@tonic-gate wargs->wa_drvname = drvname;
20220Sstevel@tonic-gate list_create(&wargs->wa_pathlist,
20230Sstevel@tonic-gate sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
20240Sstevel@tonic-gate
20250Sstevel@tonic-gate (void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
20260Sstevel@tonic-gate
20270Sstevel@tonic-gate for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
20280Sstevel@tonic-gate pe = list_next(&wargs->wa_pathlist, pe)) {
20290Sstevel@tonic-gate err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
20304851Scth (const char *)pe->pe_nodename);
20310Sstevel@tonic-gate if (rval == 0)
20320Sstevel@tonic-gate rval = err;
20330Sstevel@tonic-gate }
20340Sstevel@tonic-gate
20350Sstevel@tonic-gate while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
20360Sstevel@tonic-gate list_remove(&wargs->wa_pathlist, pe);
20370Sstevel@tonic-gate kmem_free(pe->pe_dir, pe->pe_dirlen);
20380Sstevel@tonic-gate kmem_free(pe, sizeof (*pe));
20390Sstevel@tonic-gate }
20400Sstevel@tonic-gate kmem_free(wargs, sizeof (*wargs));
20410Sstevel@tonic-gate
20420Sstevel@tonic-gate /*
20430Sstevel@tonic-gate * Pseudo nodes aren't recorded in the instance database
20440Sstevel@tonic-gate * so any such nodes need to be handled separately.
20450Sstevel@tonic-gate */
20460Sstevel@tonic-gate err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
20470Sstevel@tonic-gate if (rval == 0)
20480Sstevel@tonic-gate rval = err;
20490Sstevel@tonic-gate
20500Sstevel@tonic-gate kmem_free(drvname, MAXMODCONFNAME);
20510Sstevel@tonic-gate return (rval);
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate
20544870Sjg /*
20554870Sjg * Perform a cleanup of non-existent /devices attribute nodes,
20564870Sjg * similar to rem_drv -C, but for all drivers/devices.
20574870Sjg * This is also optional, performed as part of devfsadm -C.
20584870Sjg */
20594870Sjg void
dev_devices_cleanup()20604870Sjg dev_devices_cleanup()
20614870Sjg {
20624870Sjg struct walk_args *wargs;
20634870Sjg struct path_elem *pe;
20644870Sjg dev_info_t *devi;
20654870Sjg char *path;
20664870Sjg int err;
20674870Sjg
20684870Sjg /*
20694870Sjg * It's expected that all drivers have been loaded and
20704870Sjg * module unloading disabled while performing cleanup.
20714870Sjg */
20724870Sjg ASSERT(modunload_disable_count > 0);
20734870Sjg
20744870Sjg wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
20754870Sjg wargs->wa_drvname = NULL;
20764870Sjg list_create(&wargs->wa_pathlist,
20774870Sjg sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
20784870Sjg
20794870Sjg (void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
20804870Sjg
20814870Sjg path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
20824870Sjg
20834870Sjg for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
20844870Sjg pe = list_next(&wargs->wa_pathlist, pe)) {
20854870Sjg (void) snprintf(path, MAXPATHLEN, "%s/%s",
20864870Sjg pe->pe_dir, pe->pe_nodename);
20874870Sjg devi = e_ddi_hold_devi_by_path(path, 0);
20884870Sjg if (devi != NULL) {
20894870Sjg ddi_release_devi(devi);
20904870Sjg } else {
20914870Sjg err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
20924870Sjg (const char *)pe->pe_nodename);
20934870Sjg if (err) {
20944870Sjg cmn_err(CE_CONT,
20954870Sjg "devfs: %s: clean-up error %d\n",
20964870Sjg path, err);
20974870Sjg }
20984870Sjg }
20994870Sjg }
21004870Sjg
21014870Sjg while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
21024870Sjg list_remove(&wargs->wa_pathlist, pe);
21034870Sjg kmem_free(pe->pe_dir, pe->pe_dirlen);
21044870Sjg kmem_free(pe, sizeof (*pe));
21054870Sjg }
21064870Sjg kmem_free(wargs, sizeof (*wargs));
21074870Sjg kmem_free(path, MAXPATHLEN);
21084870Sjg }
21094870Sjg
21100Sstevel@tonic-gate static int
modctl_allocpriv(const char * name)21110Sstevel@tonic-gate modctl_allocpriv(const char *name)
21120Sstevel@tonic-gate {
21130Sstevel@tonic-gate char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
21140Sstevel@tonic-gate int error;
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
21170Sstevel@tonic-gate kmem_free(pstr, PRIVNAME_MAX);
21180Sstevel@tonic-gate return (error);
21190Sstevel@tonic-gate }
21200Sstevel@tonic-gate error = priv_getbyname(pstr, PRIV_ALLOC);
21210Sstevel@tonic-gate if (error < 0)
21220Sstevel@tonic-gate error = -error;
21230Sstevel@tonic-gate else
21240Sstevel@tonic-gate error = 0;
21250Sstevel@tonic-gate kmem_free(pstr, PRIVNAME_MAX);
21260Sstevel@tonic-gate return (error);
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate
21292621Sllai1 static int
modctl_devexists(const char * upath,int pathlen)21302621Sllai1 modctl_devexists(const char *upath, int pathlen)
21312621Sllai1 {
21322621Sllai1 char *path;
21332621Sllai1 int ret;
21342621Sllai1
21352621Sllai1 /*
21362621Sllai1 * copy in the path, including the terminating null
21372621Sllai1 */
21382621Sllai1 pathlen++;
21392621Sllai1 if (pathlen <= 1 || pathlen > MAXPATHLEN)
21402621Sllai1 return (EINVAL);
21412621Sllai1 path = kmem_zalloc(pathlen + 1, KM_SLEEP);
21422621Sllai1 if ((ret = copyinstr(upath, path, pathlen, NULL)) == 0) {
21432621Sllai1 ret = sdev_modctl_devexists(path);
21442621Sllai1 }
21452621Sllai1
21462621Sllai1 kmem_free(path, pathlen + 1);
21472621Sllai1 return (ret);
21482621Sllai1 }
21492621Sllai1
21502621Sllai1 static int
modctl_devreaddir(const char * udir,int udirlen,char * upaths,int64_t * ulensp)21512621Sllai1 modctl_devreaddir(const char *udir, int udirlen,
21522621Sllai1 char *upaths, int64_t *ulensp)
21532621Sllai1 {
21542621Sllai1 char *paths = NULL;
21552621Sllai1 char **dirlist = NULL;
21562621Sllai1 char *dir;
21572621Sllai1 int64_t ulens;
21582621Sllai1 int64_t lens;
21592621Sllai1 int i, n;
21602621Sllai1 int ret = 0;
21612621Sllai1 char *p;
21622621Sllai1 int npaths;
21632621Sllai1 int npaths_alloc;
21642621Sllai1
21652621Sllai1 /*
21662621Sllai1 * If upaths is NULL then we are only computing the amount of space
21672621Sllai1 * needed to return the paths, with the value returned in *ulensp. If we
21682621Sllai1 * are copying out paths then we get the amount of space allocated by
21692621Sllai1 * the caller. If the actual space needed for paths is larger, or
21702621Sllai1 * things are changing out from under us, then we return EAGAIN.
21712621Sllai1 */
21722621Sllai1 if (upaths) {
21732621Sllai1 if (ulensp == NULL)
21742621Sllai1 return (EINVAL);
21752621Sllai1 if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
21762621Sllai1 return (EFAULT);
21772621Sllai1 }
21782621Sllai1
21792621Sllai1 /*
21802621Sllai1 * copyin the /dev path including terminating null
21812621Sllai1 */
21822621Sllai1 udirlen++;
21832621Sllai1 if (udirlen <= 1 || udirlen > MAXPATHLEN)
21842621Sllai1 return (EINVAL);
21852621Sllai1 dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
21862621Sllai1 if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
21872621Sllai1 goto err;
21882621Sllai1
21892621Sllai1 if ((ret = sdev_modctl_readdir(dir, &dirlist,
21906065Scth &npaths, &npaths_alloc, 0)) != 0) {
21912621Sllai1 ASSERT(dirlist == NULL);
21922621Sllai1 goto err;
21932621Sllai1 }
21942621Sllai1
21952621Sllai1 lens = 0;
21962621Sllai1 for (i = 0; i < npaths; i++) {
21972621Sllai1 lens += strlen(dirlist[i]) + 1;
21982621Sllai1 }
21992621Sllai1 lens++; /* add one for double termination */
22002621Sllai1
22012621Sllai1 if (upaths) {
22022621Sllai1 if (lens > ulens) {
22032621Sllai1 ret = EAGAIN;
22042621Sllai1 goto out;
22052621Sllai1 }
22062621Sllai1
22072621Sllai1 paths = kmem_alloc(lens, KM_SLEEP);
22082621Sllai1
22092621Sllai1 p = paths;
22102621Sllai1 for (i = 0; i < npaths; i++) {
22112621Sllai1 n = strlen(dirlist[i]) + 1;
22122621Sllai1 bcopy(dirlist[i], p, n);
22132621Sllai1 p += n;
22142621Sllai1 }
22152621Sllai1 *p = 0;
22162621Sllai1
22172621Sllai1 if (copyout(paths, upaths, lens)) {
22182621Sllai1 ret = EFAULT;
22192621Sllai1 goto err;
22202621Sllai1 }
22212621Sllai1 }
22222621Sllai1
22232621Sllai1 out:
22242621Sllai1 /* copy out the amount of space needed to hold the paths */
22252621Sllai1 if (copyout(&lens, ulensp, sizeof (lens)))
22262621Sllai1 ret = EFAULT;
22272621Sllai1
22282621Sllai1 err:
22292621Sllai1 if (dirlist)
22302621Sllai1 sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
22312621Sllai1 if (paths)
22322621Sllai1 kmem_free(paths, lens);
22332621Sllai1 kmem_free(dir, udirlen + 1);
22342621Sllai1 return (ret);
22352621Sllai1 }
22362621Sllai1
22376065Scth static int
modctl_devemptydir(const char * udir,int udirlen,int * uempty)22386065Scth modctl_devemptydir(const char *udir, int udirlen, int *uempty)
22396065Scth {
22406065Scth char *dir;
22416065Scth int ret;
22426065Scth char **dirlist = NULL;
22436065Scth int npaths;
22446065Scth int npaths_alloc;
22456065Scth int empty;
22466065Scth
22476065Scth /*
22486065Scth * copyin the /dev path including terminating null
22496065Scth */
22506065Scth udirlen++;
22516065Scth if (udirlen <= 1 || udirlen > MAXPATHLEN)
22526065Scth return (EINVAL);
22536065Scth dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
22546065Scth if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
22556065Scth goto err;
22566065Scth
22576065Scth if ((ret = sdev_modctl_readdir(dir, &dirlist,
22586065Scth &npaths, &npaths_alloc, 1)) != 0) {
22596065Scth goto err;
22606065Scth }
22616065Scth
22626065Scth empty = npaths ? 0 : 1;
22636065Scth if (copyout(&empty, uempty, sizeof (empty)))
22646065Scth ret = EFAULT;
22656065Scth
22666065Scth err:
22676065Scth if (dirlist)
22686065Scth sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
22696065Scth kmem_free(dir, udirlen + 1);
22706065Scth return (ret);
22716065Scth }
22726065Scth
227310923SEvan.Yan@Sun.COM static int
modctl_hp(int subcmd,const char * path,char * cn_name,uintptr_t arg,uintptr_t rval)227410923SEvan.Yan@Sun.COM modctl_hp(int subcmd, const char *path, char *cn_name, uintptr_t arg,
227510923SEvan.Yan@Sun.COM uintptr_t rval)
227610923SEvan.Yan@Sun.COM {
227710923SEvan.Yan@Sun.COM int error = 0;
227810923SEvan.Yan@Sun.COM size_t pathsz, namesz;
227910923SEvan.Yan@Sun.COM char *devpath, *cn_name_str;
228010923SEvan.Yan@Sun.COM
228110923SEvan.Yan@Sun.COM if (path == NULL)
228210923SEvan.Yan@Sun.COM return (EINVAL);
228310923SEvan.Yan@Sun.COM
228410923SEvan.Yan@Sun.COM devpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
228510923SEvan.Yan@Sun.COM error = copyinstr(path, devpath, MAXPATHLEN, &pathsz);
228610923SEvan.Yan@Sun.COM if (error != 0) {
228710923SEvan.Yan@Sun.COM kmem_free(devpath, MAXPATHLEN);
228810923SEvan.Yan@Sun.COM return (EFAULT);
228910923SEvan.Yan@Sun.COM }
229010923SEvan.Yan@Sun.COM
229110923SEvan.Yan@Sun.COM cn_name_str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
229210923SEvan.Yan@Sun.COM error = copyinstr(cn_name, cn_name_str, MAXNAMELEN, &namesz);
229310923SEvan.Yan@Sun.COM if (error != 0) {
229410923SEvan.Yan@Sun.COM kmem_free(devpath, MAXPATHLEN);
229510923SEvan.Yan@Sun.COM kmem_free(cn_name_str, MAXNAMELEN);
229610923SEvan.Yan@Sun.COM
229710923SEvan.Yan@Sun.COM return (EFAULT);
229810923SEvan.Yan@Sun.COM }
229910923SEvan.Yan@Sun.COM
230010923SEvan.Yan@Sun.COM switch (subcmd) {
230110923SEvan.Yan@Sun.COM case MODHPOPS_CHANGE_STATE:
230210923SEvan.Yan@Sun.COM error = ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE, devpath,
230310923SEvan.Yan@Sun.COM cn_name_str, arg, NULL);
230410923SEvan.Yan@Sun.COM break;
230510923SEvan.Yan@Sun.COM case MODHPOPS_CREATE_PORT:
230610923SEvan.Yan@Sun.COM /* Create an empty PORT */
230710923SEvan.Yan@Sun.COM error = ddihp_modctl(DDI_HPOP_CN_CREATE_PORT, devpath,
230810923SEvan.Yan@Sun.COM cn_name_str, NULL, NULL);
230910923SEvan.Yan@Sun.COM break;
231010923SEvan.Yan@Sun.COM case MODHPOPS_REMOVE_PORT:
231110923SEvan.Yan@Sun.COM /* Remove an empty PORT */
231210923SEvan.Yan@Sun.COM error = ddihp_modctl(DDI_HPOP_CN_REMOVE_PORT, devpath,
231310923SEvan.Yan@Sun.COM cn_name_str, NULL, NULL);
231410923SEvan.Yan@Sun.COM break;
231510923SEvan.Yan@Sun.COM case MODHPOPS_BUS_GET:
231610923SEvan.Yan@Sun.COM error = ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY, devpath,
231710923SEvan.Yan@Sun.COM cn_name_str, arg, rval);
231810923SEvan.Yan@Sun.COM break;
231910923SEvan.Yan@Sun.COM case MODHPOPS_BUS_SET:
232010923SEvan.Yan@Sun.COM error = ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY, devpath,
232110923SEvan.Yan@Sun.COM cn_name_str, arg, rval);
232210923SEvan.Yan@Sun.COM break;
232310923SEvan.Yan@Sun.COM default:
232410923SEvan.Yan@Sun.COM error = ENOTSUP;
232510923SEvan.Yan@Sun.COM break;
232610923SEvan.Yan@Sun.COM }
232710923SEvan.Yan@Sun.COM
232810923SEvan.Yan@Sun.COM kmem_free(devpath, MAXPATHLEN);
232910923SEvan.Yan@Sun.COM kmem_free(cn_name_str, MAXNAMELEN);
233010923SEvan.Yan@Sun.COM
233110923SEvan.Yan@Sun.COM return (error);
233210923SEvan.Yan@Sun.COM }
233310923SEvan.Yan@Sun.COM
23342621Sllai1 int
modctl_moddevname(int subcmd,uintptr_t a1,uintptr_t a2)23352621Sllai1 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
23362621Sllai1 {
23372621Sllai1 int error = 0;
23382621Sllai1
23392621Sllai1 switch (subcmd) {
23402621Sllai1 case MODDEVNAME_LOOKUPDOOR:
234110097SEric.Taylor@Sun.COM error = devname_filename_register((char *)a1);
23422621Sllai1 break;
23432621Sllai1 case MODDEVNAME_PROFILE:
23442621Sllai1 error = devname_profile_update((char *)a1, (size_t)a2);
23452621Sllai1 break;
23462621Sllai1 case MODDEVNAME_RECONFIG:
23472621Sllai1 i_ddi_set_reconfig();
23482621Sllai1 break;
23492621Sllai1 case MODDEVNAME_SYSAVAIL:
23502621Sllai1 i_ddi_set_sysavail();
23512621Sllai1 break;
23522621Sllai1 default:
23532621Sllai1 error = EINVAL;
23542621Sllai1 break;
23552621Sllai1 }
23562621Sllai1
23572621Sllai1 return (error);
23582621Sllai1 }
23592621Sllai1
23600Sstevel@tonic-gate /*ARGSUSED5*/
23610Sstevel@tonic-gate int
modctl(int cmd,uintptr_t a1,uintptr_t a2,uintptr_t a3,uintptr_t a4,uintptr_t a5)23620Sstevel@tonic-gate modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
23630Sstevel@tonic-gate uintptr_t a5)
23640Sstevel@tonic-gate {
23650Sstevel@tonic-gate int error = EINVAL;
23660Sstevel@tonic-gate dev_t dev;
23670Sstevel@tonic-gate
23680Sstevel@tonic-gate if (secpolicy_modctl(CRED(), cmd) != 0)
23690Sstevel@tonic-gate return (set_errno(EPERM));
23700Sstevel@tonic-gate
23710Sstevel@tonic-gate switch (cmd) {
23720Sstevel@tonic-gate case MODLOAD: /* load a module */
23730Sstevel@tonic-gate error = modctl_modload((int)a1, (char *)a2, (int *)a3);
23740Sstevel@tonic-gate break;
23750Sstevel@tonic-gate
23760Sstevel@tonic-gate case MODUNLOAD: /* unload a module */
23770Sstevel@tonic-gate error = modctl_modunload((modid_t)a1);
23780Sstevel@tonic-gate break;
23790Sstevel@tonic-gate
23800Sstevel@tonic-gate case MODINFO: /* get module status */
23810Sstevel@tonic-gate error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
23820Sstevel@tonic-gate break;
23830Sstevel@tonic-gate
23840Sstevel@tonic-gate case MODRESERVED: /* get last major number in range */
23850Sstevel@tonic-gate error = modctl_modreserve((modid_t)a1, (int *)a2);
23860Sstevel@tonic-gate break;
23870Sstevel@tonic-gate
23880Sstevel@tonic-gate case MODSETMINIROOT: /* we are running in miniroot */
23890Sstevel@tonic-gate isminiroot = 1;
23900Sstevel@tonic-gate error = 0;
23910Sstevel@tonic-gate break;
23920Sstevel@tonic-gate
23938831SJerry.Gilliam@Sun.COM case MODADDMAJBIND: /* add major / driver alias bindings */
23948831SJerry.Gilliam@Sun.COM error = modctl_add_driver_aliases((int *)a2);
23950Sstevel@tonic-gate break;
23960Sstevel@tonic-gate
23970Sstevel@tonic-gate case MODGETPATHLEN: /* get modpath length */
23980Sstevel@tonic-gate error = modctl_getmodpathlen((int *)a2);
23990Sstevel@tonic-gate break;
24000Sstevel@tonic-gate
24010Sstevel@tonic-gate case MODGETPATH: /* get modpath */
24020Sstevel@tonic-gate error = modctl_getmodpath((char *)a2);
24030Sstevel@tonic-gate break;
24040Sstevel@tonic-gate
24050Sstevel@tonic-gate case MODREADSYSBIND: /* read system call binding file */
24060Sstevel@tonic-gate error = modctl_read_sysbinding_file();
24070Sstevel@tonic-gate break;
24080Sstevel@tonic-gate
24090Sstevel@tonic-gate case MODGETMAJBIND: /* get major number for named device */
24100Sstevel@tonic-gate error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
24110Sstevel@tonic-gate break;
24120Sstevel@tonic-gate
24130Sstevel@tonic-gate case MODGETNAME: /* get name of device given major number */
24140Sstevel@tonic-gate error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
24150Sstevel@tonic-gate break;
24160Sstevel@tonic-gate
24170Sstevel@tonic-gate case MODDEVT2INSTANCE:
24180Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24190Sstevel@tonic-gate dev = (dev_t)a1;
24200Sstevel@tonic-gate }
24210Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24220Sstevel@tonic-gate else {
24230Sstevel@tonic-gate dev = expldev(a1);
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate #endif
24260Sstevel@tonic-gate error = modctl_devt2instance(dev, (int *)a2);
24270Sstevel@tonic-gate break;
24280Sstevel@tonic-gate
24290Sstevel@tonic-gate case MODSIZEOF_DEVID: /* sizeof device id of device given dev_t */
24300Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24310Sstevel@tonic-gate dev = (dev_t)a1;
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24340Sstevel@tonic-gate else {
24350Sstevel@tonic-gate dev = expldev(a1);
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate #endif
24380Sstevel@tonic-gate error = modctl_sizeof_devid(dev, (uint_t *)a2);
24390Sstevel@tonic-gate break;
24400Sstevel@tonic-gate
24410Sstevel@tonic-gate case MODGETDEVID: /* get device id of device given dev_t */
24420Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24430Sstevel@tonic-gate dev = (dev_t)a1;
24440Sstevel@tonic-gate }
24450Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24460Sstevel@tonic-gate else {
24470Sstevel@tonic-gate dev = expldev(a1);
24480Sstevel@tonic-gate }
24490Sstevel@tonic-gate #endif
24500Sstevel@tonic-gate error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
24510Sstevel@tonic-gate break;
24520Sstevel@tonic-gate
24532723Scth case MODSIZEOF_MINORNAME: /* sizeof minor nm (dev_t,spectype) */
24540Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24550Sstevel@tonic-gate error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
24560Sstevel@tonic-gate (uint_t *)a3);
24570Sstevel@tonic-gate }
24580Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24590Sstevel@tonic-gate else {
24600Sstevel@tonic-gate error = modctl_sizeof_minorname(expldev(a1), (int)a2,
24610Sstevel@tonic-gate (uint_t *)a3);
24620Sstevel@tonic-gate }
24630Sstevel@tonic-gate
24640Sstevel@tonic-gate #endif
24650Sstevel@tonic-gate break;
24660Sstevel@tonic-gate
24672723Scth case MODGETMINORNAME: /* get minor name of (dev_t,spectype) */
24680Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24690Sstevel@tonic-gate error = modctl_get_minorname((dev_t)a1, (int)a2,
24700Sstevel@tonic-gate (uint_t)a3, (char *)a4);
24710Sstevel@tonic-gate }
24720Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24730Sstevel@tonic-gate else {
24740Sstevel@tonic-gate error = modctl_get_minorname(expldev(a1), (int)a2,
24750Sstevel@tonic-gate (uint_t)a3, (char *)a4);
24760Sstevel@tonic-gate }
24770Sstevel@tonic-gate #endif
24780Sstevel@tonic-gate break;
24790Sstevel@tonic-gate
24802723Scth case MODGETDEVFSPATH_LEN: /* sizeof path nm of (dev_t,spectype) */
24810Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24820Sstevel@tonic-gate error = modctl_devfspath_len((dev_t)a1, (int)a2,
24830Sstevel@tonic-gate (uint_t *)a3);
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24860Sstevel@tonic-gate else {
24870Sstevel@tonic-gate error = modctl_devfspath_len(expldev(a1), (int)a2,
24880Sstevel@tonic-gate (uint_t *)a3);
24890Sstevel@tonic-gate }
24900Sstevel@tonic-gate
24910Sstevel@tonic-gate #endif
24920Sstevel@tonic-gate break;
24930Sstevel@tonic-gate
249410923SEvan.Yan@Sun.COM case MODGETDEVFSPATH: /* get path name of (dev_t,spec) type */
24950Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
24960Sstevel@tonic-gate error = modctl_devfspath((dev_t)a1, (int)a2,
24970Sstevel@tonic-gate (uint_t)a3, (char *)a4);
24980Sstevel@tonic-gate }
24990Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
25000Sstevel@tonic-gate else {
25010Sstevel@tonic-gate error = modctl_devfspath(expldev(a1), (int)a2,
25020Sstevel@tonic-gate (uint_t)a3, (char *)a4);
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate #endif
25050Sstevel@tonic-gate break;
25060Sstevel@tonic-gate
25072723Scth case MODGETDEVFSPATH_MI_LEN: /* sizeof path nm of (major,instance) */
25082723Scth error = modctl_devfspath_mi_len((major_t)a1, (int)a2,
25094851Scth (uint_t *)a3);
25102723Scth break;
25112723Scth
251210923SEvan.Yan@Sun.COM case MODGETDEVFSPATH_MI: /* get path name of (major,instance) */
25132723Scth error = modctl_devfspath_mi((major_t)a1, (int)a2,
25144851Scth (uint_t)a3, (char *)a4);
25152723Scth break;
25162723Scth
25170Sstevel@tonic-gate
25180Sstevel@tonic-gate case MODEVENTS:
25190Sstevel@tonic-gate error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
25200Sstevel@tonic-gate break;
25210Sstevel@tonic-gate
25220Sstevel@tonic-gate case MODGETFBNAME: /* get the framebuffer name */
25230Sstevel@tonic-gate error = modctl_get_fbname((char *)a1);
25240Sstevel@tonic-gate break;
25250Sstevel@tonic-gate
25260Sstevel@tonic-gate case MODREREADDACF: /* reread dacf rule database from given file */
25270Sstevel@tonic-gate error = modctl_reread_dacf((char *)a1);
25280Sstevel@tonic-gate break;
25290Sstevel@tonic-gate
25300Sstevel@tonic-gate case MODLOADDRVCONF: /* load driver.conf file for major */
253110842SJerry.Gilliam@Sun.COM error = modctl_load_drvconf((major_t)a1, (int)a2);
25320Sstevel@tonic-gate break;
25330Sstevel@tonic-gate
25340Sstevel@tonic-gate case MODUNLOADDRVCONF: /* unload driver.conf file for major */
25350Sstevel@tonic-gate error = modctl_unload_drvconf((major_t)a1);
25360Sstevel@tonic-gate break;
25370Sstevel@tonic-gate
25380Sstevel@tonic-gate case MODREMMAJBIND: /* remove a major binding */
25390Sstevel@tonic-gate error = modctl_rem_major((major_t)a1);
25400Sstevel@tonic-gate break;
25410Sstevel@tonic-gate
25428831SJerry.Gilliam@Sun.COM case MODREMDRVALIAS: /* remove a major/alias binding */
25438831SJerry.Gilliam@Sun.COM error = modctl_remove_driver_aliases((int *)a2);
25448831SJerry.Gilliam@Sun.COM break;
25458831SJerry.Gilliam@Sun.COM
25460Sstevel@tonic-gate case MODDEVID2PATHS: /* get paths given devid */
25470Sstevel@tonic-gate error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
25480Sstevel@tonic-gate (uint_t)a3, (size_t *)a4, (char *)a5);
25490Sstevel@tonic-gate break;
25500Sstevel@tonic-gate
25510Sstevel@tonic-gate case MODSETDEVPOLICY: /* establish device policy */
25520Sstevel@tonic-gate error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
25530Sstevel@tonic-gate break;
25540Sstevel@tonic-gate
25550Sstevel@tonic-gate case MODGETDEVPOLICY: /* get device policy */
25560Sstevel@tonic-gate error = devpolicy_get((int *)a1, (size_t)a2,
25574851Scth (devplcysys_t *)a3);
25580Sstevel@tonic-gate break;
25590Sstevel@tonic-gate
25600Sstevel@tonic-gate case MODALLOCPRIV:
25610Sstevel@tonic-gate error = modctl_allocpriv((const char *)a1);
25620Sstevel@tonic-gate break;
25630Sstevel@tonic-gate
25640Sstevel@tonic-gate case MODGETDEVPOLICYBYNAME:
25650Sstevel@tonic-gate error = devpolicy_getbyname((size_t)a1,
25660Sstevel@tonic-gate (devplcysys_t *)a2, (char *)a3);
25670Sstevel@tonic-gate break;
25680Sstevel@tonic-gate
25690Sstevel@tonic-gate case MODLOADMINORPERM:
25700Sstevel@tonic-gate case MODADDMINORPERM:
25710Sstevel@tonic-gate case MODREMMINORPERM:
25720Sstevel@tonic-gate error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
25730Sstevel@tonic-gate break;
25740Sstevel@tonic-gate
25750Sstevel@tonic-gate case MODREMDRVCLEANUP:
25760Sstevel@tonic-gate error = modctl_remdrv_cleanup((const char *)a1);
25770Sstevel@tonic-gate break;
25780Sstevel@tonic-gate
25792621Sllai1 case MODDEVEXISTS: /* non-reconfiguring /dev lookup */
25802621Sllai1 error = modctl_devexists((const char *)a1, (size_t)a2);
25812621Sllai1 break;
25822621Sllai1
25832621Sllai1 case MODDEVREADDIR: /* non-reconfiguring /dev readdir */
25842621Sllai1 error = modctl_devreaddir((const char *)a1, (size_t)a2,
25852621Sllai1 (char *)a3, (int64_t *)a4);
25862621Sllai1 break;
25872621Sllai1
25886065Scth case MODDEVEMPTYDIR: /* non-reconfiguring /dev emptydir */
25896065Scth error = modctl_devemptydir((const char *)a1, (size_t)a2,
25906065Scth (int *)a3);
25916065Scth break;
25926065Scth
25932621Sllai1 case MODDEVNAME:
25942621Sllai1 error = modctl_moddevname((int)a1, a2, a3);
25952621Sllai1 break;
25962621Sllai1
25974845Svikram case MODRETIRE: /* retire device named by physpath a1 */
25984845Svikram error = modctl_retire((char *)a1, (char *)a2, (size_t)a3);
25994845Svikram break;
26004845Svikram
26014845Svikram case MODISRETIRED: /* check if a device is retired. */
26024845Svikram error = modctl_is_retired((char *)a1, (int *)a2);
26034845Svikram break;
26044845Svikram
26054845Svikram case MODUNRETIRE: /* unretire device named by physpath a1 */
26064845Svikram error = modctl_unretire((char *)a1);
26074845Svikram break;
26084845Svikram
260910923SEvan.Yan@Sun.COM case MODHPOPS: /* hotplug operations */
261010923SEvan.Yan@Sun.COM /* device named by physpath a2 and Connection name a3 */
261110923SEvan.Yan@Sun.COM error = modctl_hp((int)a1, (char *)a2, (char *)a3, a4, a5);
261210923SEvan.Yan@Sun.COM break;
261310923SEvan.Yan@Sun.COM
26140Sstevel@tonic-gate default:
26150Sstevel@tonic-gate error = EINVAL;
26160Sstevel@tonic-gate break;
26170Sstevel@tonic-gate }
26180Sstevel@tonic-gate
26190Sstevel@tonic-gate return (error ? set_errno(error) : 0);
26200Sstevel@tonic-gate }
26210Sstevel@tonic-gate
26220Sstevel@tonic-gate /*
26230Sstevel@tonic-gate * Calls to kobj_load_module()() are handled off to this routine in a
26240Sstevel@tonic-gate * separate thread.
26250Sstevel@tonic-gate */
26260Sstevel@tonic-gate static void
modload_thread(struct loadmt * ltp)26270Sstevel@tonic-gate modload_thread(struct loadmt *ltp)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate /* load the module and signal the creator of this thread */
26300Sstevel@tonic-gate kmutex_t cpr_lk;
26310Sstevel@tonic-gate callb_cpr_t cpr_i;
26320Sstevel@tonic-gate
26330Sstevel@tonic-gate mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
26340Sstevel@tonic-gate CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
26350Sstevel@tonic-gate /* borrow the devi lock from thread which invoked us */
26360Sstevel@tonic-gate pm_borrow_lock(ltp->owner);
26370Sstevel@tonic-gate ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
26380Sstevel@tonic-gate pm_return_lock();
26390Sstevel@tonic-gate sema_v(<p->sema);
26400Sstevel@tonic-gate mutex_enter(&cpr_lk);
26410Sstevel@tonic-gate CALLB_CPR_EXIT(&cpr_i);
26420Sstevel@tonic-gate mutex_destroy(&cpr_lk);
26430Sstevel@tonic-gate thread_exit();
26440Sstevel@tonic-gate }
26450Sstevel@tonic-gate
26460Sstevel@tonic-gate /*
26470Sstevel@tonic-gate * load a module, adding a reference if caller specifies rmodp. If rmodp
26480Sstevel@tonic-gate * is specified then an errno is returned, otherwise a module index is
26490Sstevel@tonic-gate * returned (-1 on error).
26500Sstevel@tonic-gate */
26510Sstevel@tonic-gate static int
modrload(const char * subdir,const char * filename,struct modctl ** rmodp)26526855Sjohnlev modrload(const char *subdir, const char *filename, struct modctl **rmodp)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate struct modctl *modp;
26550Sstevel@tonic-gate size_t size;
26560Sstevel@tonic-gate char *fullname;
26570Sstevel@tonic-gate int retval = EINVAL;
26580Sstevel@tonic-gate int id = -1;
26590Sstevel@tonic-gate
26600Sstevel@tonic-gate if (rmodp)
26610Sstevel@tonic-gate *rmodp = NULL; /* avoid garbage */
26620Sstevel@tonic-gate
26630Sstevel@tonic-gate if (subdir != NULL) {
26640Sstevel@tonic-gate /*
26650Sstevel@tonic-gate * refuse / in filename to prevent "../" escapes.
26660Sstevel@tonic-gate */
26670Sstevel@tonic-gate if (strchr(filename, '/') != NULL)
26680Sstevel@tonic-gate return (rmodp ? retval : id);
26690Sstevel@tonic-gate
26700Sstevel@tonic-gate /*
26710Sstevel@tonic-gate * allocate enough space for <subdir>/<filename><NULL>
26720Sstevel@tonic-gate */
26730Sstevel@tonic-gate size = strlen(subdir) + strlen(filename) + 2;
26740Sstevel@tonic-gate fullname = kmem_zalloc(size, KM_SLEEP);
26750Sstevel@tonic-gate (void) sprintf(fullname, "%s/%s", subdir, filename);
26760Sstevel@tonic-gate } else {
26776855Sjohnlev fullname = (char *)filename;
26780Sstevel@tonic-gate }
26790Sstevel@tonic-gate
26805254Sgavinm modp = mod_hold_installed_mod(fullname, 1, 0, &retval);
26810Sstevel@tonic-gate if (modp != NULL) {
26820Sstevel@tonic-gate id = modp->mod_id;
26830Sstevel@tonic-gate if (rmodp) {
26840Sstevel@tonic-gate /* add mod_ref and return *rmodp */
26850Sstevel@tonic-gate mutex_enter(&mod_lock);
26860Sstevel@tonic-gate modp->mod_ref++;
26870Sstevel@tonic-gate mutex_exit(&mod_lock);
26880Sstevel@tonic-gate *rmodp = modp;
26890Sstevel@tonic-gate }
26900Sstevel@tonic-gate mod_release_mod(modp);
26910Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, sys, modload, 1);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate
26940Sstevel@tonic-gate done: if (subdir != NULL)
26950Sstevel@tonic-gate kmem_free(fullname, size);
26960Sstevel@tonic-gate return (rmodp ? retval : id);
26970Sstevel@tonic-gate }
26980Sstevel@tonic-gate
26990Sstevel@tonic-gate /*
27000Sstevel@tonic-gate * This is the primary kernel interface to load a module. It loads and
27010Sstevel@tonic-gate * installs the named module. It does not hold mod_ref of the module, so
27020Sstevel@tonic-gate * a module unload attempt can occur at any time - it is up to the
27030Sstevel@tonic-gate * _fini/mod_remove implementation to determine if unload will succeed.
27040Sstevel@tonic-gate */
27050Sstevel@tonic-gate int
modload(const char * subdir,const char * filename)27066855Sjohnlev modload(const char *subdir, const char *filename)
27070Sstevel@tonic-gate {
27080Sstevel@tonic-gate return (modrload(subdir, filename, NULL));
27090Sstevel@tonic-gate }
27100Sstevel@tonic-gate
27110Sstevel@tonic-gate /*
27121414Scindi * Load a module using a series of qualified names from most specific to least
27131414Scindi * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
27145254Sgavinm * Value returned in *chosen
27155254Sgavinm * foo/bar.baz.1.2.3 3
27165254Sgavinm * foo/bar.baz.1.2 2
27175254Sgavinm * foo/bar.baz.1 1
27185254Sgavinm * foo/bar.baz 0
27191414Scindi *
27205254Sgavinm * Return the module ID on success; -1 if no module was loaded. On success
27215254Sgavinm * and if 'chosen' is not NULL we also return the number of suffices that
27225254Sgavinm * were in the module we chose to load.
27231414Scindi */
27241414Scindi int
modload_qualified(const char * subdir,const char * p1,const char * p2,const char * delim,uint_t suffv[],int suffc,int * chosen)27251414Scindi modload_qualified(const char *subdir, const char *p1,
27265254Sgavinm const char *p2, const char *delim, uint_t suffv[], int suffc, int *chosen)
27271414Scindi {
27281414Scindi char path[MOD_MAXPATH];
27291414Scindi size_t n, resid = sizeof (path);
27301414Scindi char *p = path;
27311414Scindi
27321414Scindi char **dotv;
27331414Scindi int i, rc, id;
27341414Scindi modctl_t *mp;
27351414Scindi
27361414Scindi if (p2 != NULL)
27371414Scindi n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
27381414Scindi else
27391414Scindi n = snprintf(p, resid, "%s/%s", subdir, p1);
27401414Scindi
27411414Scindi if (n >= resid)
27421414Scindi return (-1);
27431414Scindi
27441414Scindi p += n;
27451414Scindi resid -= n;
27461414Scindi dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
27471414Scindi
27481414Scindi for (i = 0; i < suffc; i++) {
27491414Scindi dotv[i] = p;
27501414Scindi n = snprintf(p, resid, "%s%u", delim, suffv[i]);
27511414Scindi
27521414Scindi if (n >= resid) {
27531414Scindi kmem_free(dotv, sizeof (char *) * (suffc + 1));
27541414Scindi return (-1);
27551414Scindi }
27561414Scindi
27571414Scindi p += n;
27581414Scindi resid -= n;
27591414Scindi }
27601414Scindi
27611414Scindi dotv[suffc] = p;
27621414Scindi
27631414Scindi for (i = suffc; i >= 0; i--) {
27641414Scindi dotv[i][0] = '\0';
27655254Sgavinm mp = mod_hold_installed_mod(path, 1, 1, &rc);
27661414Scindi
27671414Scindi if (mp != NULL) {
27681414Scindi kmem_free(dotv, sizeof (char *) * (suffc + 1));
27691414Scindi id = mp->mod_id;
27701414Scindi mod_release_mod(mp);
27715254Sgavinm if (chosen != NULL)
27725254Sgavinm *chosen = i;
27731414Scindi return (id);
27741414Scindi }
27751414Scindi }
27761414Scindi
27771414Scindi kmem_free(dotv, sizeof (char *) * (suffc + 1));
27781414Scindi return (-1);
27791414Scindi }
27801414Scindi
27811414Scindi /*
27820Sstevel@tonic-gate * Load a module.
27830Sstevel@tonic-gate */
27840Sstevel@tonic-gate int
modloadonly(const char * subdir,const char * filename)27856855Sjohnlev modloadonly(const char *subdir, const char *filename)
27860Sstevel@tonic-gate {
27870Sstevel@tonic-gate struct modctl *modp;
27880Sstevel@tonic-gate char *fullname;
27890Sstevel@tonic-gate size_t size;
27900Sstevel@tonic-gate int id, retval;
27910Sstevel@tonic-gate
27920Sstevel@tonic-gate if (subdir != NULL) {
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate * allocate enough space for <subdir>/<filename><NULL>
27950Sstevel@tonic-gate */
27960Sstevel@tonic-gate size = strlen(subdir) + strlen(filename) + 2;
27970Sstevel@tonic-gate fullname = kmem_zalloc(size, KM_SLEEP);
27980Sstevel@tonic-gate (void) sprintf(fullname, "%s/%s", subdir, filename);
27990Sstevel@tonic-gate } else {
28006855Sjohnlev fullname = (char *)filename;
28010Sstevel@tonic-gate }
28020Sstevel@tonic-gate
28030Sstevel@tonic-gate modp = mod_hold_loaded_mod(NULL, fullname, &retval);
28040Sstevel@tonic-gate if (modp) {
28050Sstevel@tonic-gate id = modp->mod_id;
28060Sstevel@tonic-gate mod_release_mod(modp);
28070Sstevel@tonic-gate }
28080Sstevel@tonic-gate
28090Sstevel@tonic-gate if (subdir != NULL)
28100Sstevel@tonic-gate kmem_free(fullname, size);
28110Sstevel@tonic-gate
28120Sstevel@tonic-gate if (retval == 0)
28130Sstevel@tonic-gate return (id);
28140Sstevel@tonic-gate return (-1);
28150Sstevel@tonic-gate }
28160Sstevel@tonic-gate
28170Sstevel@tonic-gate /*
28180Sstevel@tonic-gate * Try to uninstall and unload a module, removing a reference if caller
28190Sstevel@tonic-gate * specifies rmodp.
28200Sstevel@tonic-gate */
28210Sstevel@tonic-gate static int
modunrload(modid_t id,struct modctl ** rmodp,int unload)28220Sstevel@tonic-gate modunrload(modid_t id, struct modctl **rmodp, int unload)
28230Sstevel@tonic-gate {
28240Sstevel@tonic-gate struct modctl *modp;
28250Sstevel@tonic-gate int retval;
28260Sstevel@tonic-gate
28270Sstevel@tonic-gate if (rmodp)
28280Sstevel@tonic-gate *rmodp = NULL; /* avoid garbage */
28290Sstevel@tonic-gate
28300Sstevel@tonic-gate if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
28310Sstevel@tonic-gate return (EINVAL);
28320Sstevel@tonic-gate
28330Sstevel@tonic-gate if (rmodp) {
28340Sstevel@tonic-gate mutex_enter(&mod_lock);
28350Sstevel@tonic-gate modp->mod_ref--;
2836*12207SChris.Horne@Sun.COM if (modp->mod_ref == 0)
2837*12207SChris.Horne@Sun.COM mod_uninstall_ref_zero++;
28380Sstevel@tonic-gate mutex_exit(&mod_lock);
28390Sstevel@tonic-gate *rmodp = modp;
28400Sstevel@tonic-gate }
28410Sstevel@tonic-gate
28420Sstevel@tonic-gate if (unload) {
28430Sstevel@tonic-gate retval = moduninstall(modp);
28440Sstevel@tonic-gate if (retval == 0) {
28450Sstevel@tonic-gate mod_unload(modp);
28460Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, sys, modunload, 1);
28470Sstevel@tonic-gate } else if (retval == EALREADY)
28480Sstevel@tonic-gate retval = 0; /* already unloaded, not an error */
28490Sstevel@tonic-gate } else
28500Sstevel@tonic-gate retval = 0;
28510Sstevel@tonic-gate
28520Sstevel@tonic-gate mod_release_mod(modp);
28530Sstevel@tonic-gate return (retval);
28540Sstevel@tonic-gate }
28550Sstevel@tonic-gate
28560Sstevel@tonic-gate /*
28570Sstevel@tonic-gate * Uninstall and unload a module.
28580Sstevel@tonic-gate */
28590Sstevel@tonic-gate int
modunload(modid_t id)28600Sstevel@tonic-gate modunload(modid_t id)
28610Sstevel@tonic-gate {
28621218Scth int retval;
28631218Scth
28641218Scth /* synchronize with any active modunload_disable() */
28651218Scth modunload_begin();
28661218Scth if (ddi_root_node())
28671218Scth (void) devfs_clean(ddi_root_node(), NULL, 0);
28681218Scth retval = modunrload(id, NULL, 1);
28691218Scth modunload_end();
28701218Scth return (retval);
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate
28730Sstevel@tonic-gate /*
28740Sstevel@tonic-gate * Return status of a loaded module.
28750Sstevel@tonic-gate */
28760Sstevel@tonic-gate static int
modinfo(modid_t id,struct modinfo * modinfop)28770Sstevel@tonic-gate modinfo(modid_t id, struct modinfo *modinfop)
28780Sstevel@tonic-gate {
28790Sstevel@tonic-gate struct modctl *modp;
28800Sstevel@tonic-gate modid_t mid;
28810Sstevel@tonic-gate int i;
28820Sstevel@tonic-gate
28830Sstevel@tonic-gate mid = modinfop->mi_id;
28840Sstevel@tonic-gate if (modinfop->mi_info & MI_INFO_ALL) {
28850Sstevel@tonic-gate while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
28860Sstevel@tonic-gate if ((modinfop->mi_info & MI_INFO_CNT) ||
28870Sstevel@tonic-gate modp->mod_installed)
28880Sstevel@tonic-gate break;
28890Sstevel@tonic-gate mod_release_mod(modp);
28900Sstevel@tonic-gate }
28910Sstevel@tonic-gate if (modp == NULL)
28920Sstevel@tonic-gate return (EINVAL);
28930Sstevel@tonic-gate } else {
28940Sstevel@tonic-gate modp = mod_hold_by_id(id);
28950Sstevel@tonic-gate if (modp == NULL)
28960Sstevel@tonic-gate return (EINVAL);
28970Sstevel@tonic-gate if (!(modinfop->mi_info & MI_INFO_CNT) &&
28980Sstevel@tonic-gate (modp->mod_installed == 0)) {
28990Sstevel@tonic-gate mod_release_mod(modp);
29000Sstevel@tonic-gate return (EINVAL);
29010Sstevel@tonic-gate }
29020Sstevel@tonic-gate }
29030Sstevel@tonic-gate
29040Sstevel@tonic-gate modinfop->mi_rev = 0;
29050Sstevel@tonic-gate modinfop->mi_state = 0;
29060Sstevel@tonic-gate for (i = 0; i < MODMAXLINK; i++) {
29070Sstevel@tonic-gate modinfop->mi_msinfo[i].msi_p0 = -1;
29080Sstevel@tonic-gate modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
29090Sstevel@tonic-gate }
29100Sstevel@tonic-gate if (modp->mod_loaded) {
29110Sstevel@tonic-gate modinfop->mi_state = MI_LOADED;
29120Sstevel@tonic-gate kobj_getmodinfo(modp->mod_mp, modinfop);
29130Sstevel@tonic-gate }
29140Sstevel@tonic-gate if (modp->mod_installed) {
29150Sstevel@tonic-gate modinfop->mi_state |= MI_INSTALLED;
29160Sstevel@tonic-gate
29170Sstevel@tonic-gate (void) mod_getinfo(modp, modinfop);
29180Sstevel@tonic-gate }
29190Sstevel@tonic-gate
29200Sstevel@tonic-gate modinfop->mi_id = modp->mod_id;
29210Sstevel@tonic-gate modinfop->mi_loadcnt = modp->mod_loadcnt;
29220Sstevel@tonic-gate (void) strcpy(modinfop->mi_name, modp->mod_modname);
29230Sstevel@tonic-gate
29240Sstevel@tonic-gate mod_release_mod(modp);
29250Sstevel@tonic-gate return (0);
29260Sstevel@tonic-gate }
29270Sstevel@tonic-gate
29280Sstevel@tonic-gate static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
29290Sstevel@tonic-gate static char no_err[] = "No error function for weak stub %s";
29300Sstevel@tonic-gate
29310Sstevel@tonic-gate /*
29320Sstevel@tonic-gate * used by the stubs themselves to load and hold a module.
29330Sstevel@tonic-gate * Returns 0 if the module is successfully held;
29340Sstevel@tonic-gate * the stub needs to call mod_release_stub().
29350Sstevel@tonic-gate * -1 if the stub should just call the err_fcn.
29360Sstevel@tonic-gate * Note that this code is stretched out so that we avoid subroutine calls
29370Sstevel@tonic-gate * and optimize for the most likely case. That is, the case where the
29380Sstevel@tonic-gate * module is loaded and installed and not held. In that case we just inc
29390Sstevel@tonic-gate * the mod_ref count and continue.
29400Sstevel@tonic-gate */
29410Sstevel@tonic-gate int
mod_hold_stub(struct mod_stub_info * stub)29420Sstevel@tonic-gate mod_hold_stub(struct mod_stub_info *stub)
29430Sstevel@tonic-gate {
29440Sstevel@tonic-gate struct modctl *mp;
29450Sstevel@tonic-gate struct mod_modinfo *mip;
29460Sstevel@tonic-gate
29470Sstevel@tonic-gate mip = stub->mods_modinfo;
29480Sstevel@tonic-gate
29490Sstevel@tonic-gate mutex_enter(&mod_lock);
29500Sstevel@tonic-gate
29510Sstevel@tonic-gate /* we do mod_hold_by_modctl inline for speed */
29520Sstevel@tonic-gate
29530Sstevel@tonic-gate mod_check_again:
29540Sstevel@tonic-gate if ((mp = mip->mp) != NULL) {
29550Sstevel@tonic-gate if (mp->mod_busy == 0) {
29560Sstevel@tonic-gate if (mp->mod_installed) {
29570Sstevel@tonic-gate /* increment the reference count */
29580Sstevel@tonic-gate mp->mod_ref++;
29590Sstevel@tonic-gate ASSERT(mp->mod_ref && mp->mod_installed);
29600Sstevel@tonic-gate mutex_exit(&mod_lock);
29610Sstevel@tonic-gate return (0);
29620Sstevel@tonic-gate } else {
29630Sstevel@tonic-gate mp->mod_busy = 1;
29640Sstevel@tonic-gate mp->mod_inprogress_thread =
29650Sstevel@tonic-gate (curthread == NULL ?
29660Sstevel@tonic-gate (kthread_id_t)-1 : curthread);
29670Sstevel@tonic-gate }
29680Sstevel@tonic-gate } else {
29690Sstevel@tonic-gate /*
29700Sstevel@tonic-gate * wait one time and then go see if someone
29710Sstevel@tonic-gate * else has resolved the stub (set mip->mp).
29720Sstevel@tonic-gate */
29730Sstevel@tonic-gate if (mod_hold_by_modctl(mp,
29740Sstevel@tonic-gate MOD_WAIT_ONCE | MOD_LOCK_HELD))
29750Sstevel@tonic-gate goto mod_check_again;
29760Sstevel@tonic-gate
29770Sstevel@tonic-gate /*
29780Sstevel@tonic-gate * what we have now may have been unloaded!, in
29790Sstevel@tonic-gate * that case, mip->mp will be NULL, we'll hit this
29800Sstevel@tonic-gate * module and load again..
29810Sstevel@tonic-gate */
29820Sstevel@tonic-gate cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
29830Sstevel@tonic-gate }
29840Sstevel@tonic-gate mutex_exit(&mod_lock);
29850Sstevel@tonic-gate } else {
29860Sstevel@tonic-gate /* first time we've hit this module */
29870Sstevel@tonic-gate mutex_exit(&mod_lock);
29880Sstevel@tonic-gate mp = mod_hold_by_name(mip->modm_module_name);
29890Sstevel@tonic-gate mip->mp = mp;
29900Sstevel@tonic-gate }
29910Sstevel@tonic-gate
29920Sstevel@tonic-gate /*
29930Sstevel@tonic-gate * If we are here, it means that the following conditions
29940Sstevel@tonic-gate * are satisfied.
29950Sstevel@tonic-gate *
29960Sstevel@tonic-gate * mip->mp != NULL
29970Sstevel@tonic-gate * this thread has set the mp->mod_busy = 1
29980Sstevel@tonic-gate * mp->mod_installed = 0
29990Sstevel@tonic-gate *
30000Sstevel@tonic-gate */
30010Sstevel@tonic-gate ASSERT(mp != NULL);
30020Sstevel@tonic-gate ASSERT(mp->mod_busy == 1);
30030Sstevel@tonic-gate
30040Sstevel@tonic-gate if (mp->mod_installed == 0) {
30050Sstevel@tonic-gate /* Module not loaded, if weak stub don't load it */
30060Sstevel@tonic-gate if (stub->mods_flag & MODS_WEAK) {
30070Sstevel@tonic-gate if (stub->mods_errfcn == NULL) {
30080Sstevel@tonic-gate mod_release_mod(mp);
30090Sstevel@tonic-gate cmn_err(CE_PANIC, no_err,
30100Sstevel@tonic-gate mip->modm_module_name);
30110Sstevel@tonic-gate }
30120Sstevel@tonic-gate } else {
30130Sstevel@tonic-gate /* Not a weak stub so load the module */
30140Sstevel@tonic-gate
30150Sstevel@tonic-gate if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
30160Sstevel@tonic-gate /*
30170Sstevel@tonic-gate * If mod_load() was successful
30180Sstevel@tonic-gate * and modinstall() failed, then
30190Sstevel@tonic-gate * unload the module.
30200Sstevel@tonic-gate */
30210Sstevel@tonic-gate if (mp->mod_loaded)
30220Sstevel@tonic-gate mod_unload(mp);
30230Sstevel@tonic-gate
30240Sstevel@tonic-gate mod_release_mod(mp);
30250Sstevel@tonic-gate if (stub->mods_errfcn == NULL) {
30260Sstevel@tonic-gate cmn_err(CE_PANIC, mod_stub_err,
30270Sstevel@tonic-gate mip->modm_module_name);
30280Sstevel@tonic-gate } else {
30290Sstevel@tonic-gate return (-1);
30300Sstevel@tonic-gate }
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate }
30330Sstevel@tonic-gate }
30340Sstevel@tonic-gate
30350Sstevel@tonic-gate /*
30360Sstevel@tonic-gate * At this point module is held and loaded. Release
30370Sstevel@tonic-gate * the mod_busy and mod_inprogress_thread before
30380Sstevel@tonic-gate * returning. We actually call mod_release() here so
30390Sstevel@tonic-gate * that if another stub wants to access this module,
30400Sstevel@tonic-gate * it can do so. mod_ref is incremented before mod_release()
30410Sstevel@tonic-gate * is called to prevent someone else from snatching the
30420Sstevel@tonic-gate * module from this thread.
30430Sstevel@tonic-gate */
30440Sstevel@tonic-gate mutex_enter(&mod_lock);
30450Sstevel@tonic-gate mp->mod_ref++;
30460Sstevel@tonic-gate ASSERT(mp->mod_ref &&
30470Sstevel@tonic-gate (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
30480Sstevel@tonic-gate mod_release(mp);
30490Sstevel@tonic-gate mutex_exit(&mod_lock);
30500Sstevel@tonic-gate return (0);
30510Sstevel@tonic-gate }
30520Sstevel@tonic-gate
30530Sstevel@tonic-gate void
mod_release_stub(struct mod_stub_info * stub)30540Sstevel@tonic-gate mod_release_stub(struct mod_stub_info *stub)
30550Sstevel@tonic-gate {
30560Sstevel@tonic-gate struct modctl *mp = stub->mods_modinfo->mp;
30570Sstevel@tonic-gate
30580Sstevel@tonic-gate /* inline mod_release_mod */
30590Sstevel@tonic-gate mutex_enter(&mod_lock);
30600Sstevel@tonic-gate ASSERT(mp->mod_ref &&
30610Sstevel@tonic-gate (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
30620Sstevel@tonic-gate mp->mod_ref--;
3063*12207SChris.Horne@Sun.COM if (mp->mod_ref == 0)
3064*12207SChris.Horne@Sun.COM mod_uninstall_ref_zero++;
30650Sstevel@tonic-gate if (mp->mod_want) {
30660Sstevel@tonic-gate mp->mod_want = 0;
30670Sstevel@tonic-gate cv_broadcast(&mod_cv);
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate mutex_exit(&mod_lock);
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate
30720Sstevel@tonic-gate static struct modctl *
mod_hold_loaded_mod(struct modctl * dep,char * filename,int * status)30730Sstevel@tonic-gate mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
30740Sstevel@tonic-gate {
30750Sstevel@tonic-gate struct modctl *modp;
30760Sstevel@tonic-gate int retval;
30770Sstevel@tonic-gate
30780Sstevel@tonic-gate /*
30790Sstevel@tonic-gate * Hold the module.
30800Sstevel@tonic-gate */
30810Sstevel@tonic-gate modp = mod_hold_by_name_requisite(dep, filename);
30820Sstevel@tonic-gate if (modp) {
30830Sstevel@tonic-gate retval = mod_load(modp, 1);
30840Sstevel@tonic-gate if (retval != 0) {
30850Sstevel@tonic-gate mod_release_mod(modp);
30860Sstevel@tonic-gate modp = NULL;
30870Sstevel@tonic-gate }
30880Sstevel@tonic-gate *status = retval;
30890Sstevel@tonic-gate } else {
30900Sstevel@tonic-gate *status = ENOSPC;
30910Sstevel@tonic-gate }
30920Sstevel@tonic-gate
30930Sstevel@tonic-gate /*
30940Sstevel@tonic-gate * if dep is not NULL, clear the module dependency information.
30950Sstevel@tonic-gate * This information is set in mod_hold_by_name_common().
30960Sstevel@tonic-gate */
30970Sstevel@tonic-gate if (dep != NULL && dep->mod_requisite_loading != NULL) {
30980Sstevel@tonic-gate ASSERT(dep->mod_busy);
30990Sstevel@tonic-gate dep->mod_requisite_loading = NULL;
31000Sstevel@tonic-gate }
31010Sstevel@tonic-gate
31020Sstevel@tonic-gate return (modp);
31030Sstevel@tonic-gate }
31040Sstevel@tonic-gate
31050Sstevel@tonic-gate /*
31060Sstevel@tonic-gate * hold, load, and install the named module
31070Sstevel@tonic-gate */
31080Sstevel@tonic-gate static struct modctl *
mod_hold_installed_mod(char * name,int usepath,int forcecheck,int * r)31095254Sgavinm mod_hold_installed_mod(char *name, int usepath, int forcecheck, int *r)
31100Sstevel@tonic-gate {
31110Sstevel@tonic-gate struct modctl *modp;
31120Sstevel@tonic-gate int retval;
3113169Scth
3114169Scth /*
3115169Scth * Verify that that module in question actually exists on disk
3116169Scth * before allocation of module structure by mod_hold_by_name.
3117169Scth */
31185254Sgavinm if (modrootloaded && swaploaded || forcecheck) {
3119309Scth if (!kobj_path_exists(name, usepath)) {
3120169Scth *r = ENOENT;
3121169Scth return (NULL);
3122169Scth }
3123169Scth }
31240Sstevel@tonic-gate
31250Sstevel@tonic-gate /*
31260Sstevel@tonic-gate * Hold the module.
31270Sstevel@tonic-gate */
31280Sstevel@tonic-gate modp = mod_hold_by_name(name);
31290Sstevel@tonic-gate if (modp) {
31300Sstevel@tonic-gate retval = mod_load(modp, usepath);
31310Sstevel@tonic-gate if (retval != 0) {
31320Sstevel@tonic-gate mod_release_mod(modp);
31330Sstevel@tonic-gate modp = NULL;
31340Sstevel@tonic-gate *r = retval;
31350Sstevel@tonic-gate } else {
31360Sstevel@tonic-gate if ((*r = modinstall(modp)) != 0) {
31370Sstevel@tonic-gate /*
31380Sstevel@tonic-gate * We loaded it, but failed to _init() it.
31390Sstevel@tonic-gate * Be kind to developers -- force it
31400Sstevel@tonic-gate * out of memory now so that the next
31410Sstevel@tonic-gate * attempt to use the module will cause
31420Sstevel@tonic-gate * a reload. See 1093793.
31430Sstevel@tonic-gate */
31440Sstevel@tonic-gate mod_unload(modp);
31450Sstevel@tonic-gate mod_release_mod(modp);
31460Sstevel@tonic-gate modp = NULL;
31470Sstevel@tonic-gate }
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate } else {
31500Sstevel@tonic-gate *r = ENOSPC;
31510Sstevel@tonic-gate }
31520Sstevel@tonic-gate return (modp);
31530Sstevel@tonic-gate }
31540Sstevel@tonic-gate
31550Sstevel@tonic-gate static char mod_excl_msg[] =
31560Sstevel@tonic-gate "module %s(%s) is EXCLUDED and will not be loaded\n";
31570Sstevel@tonic-gate static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
31580Sstevel@tonic-gate
31590Sstevel@tonic-gate /*
31600Sstevel@tonic-gate * This routine is needed for dependencies. Users specify dependencies
31610Sstevel@tonic-gate * by declaring a character array initialized to filenames of dependents.
31620Sstevel@tonic-gate * So the code that handles dependents deals with filenames (and not
31630Sstevel@tonic-gate * module names) because that's all it has. We load by filename and once
31640Sstevel@tonic-gate * we've loaded a file we can get the module name.
31650Sstevel@tonic-gate * Unfortunately there isn't a single unified filename/modulename namespace.
31660Sstevel@tonic-gate * C'est la vie.
31670Sstevel@tonic-gate *
31680Sstevel@tonic-gate * We allow the name being looked up to be prepended by an optional
31690Sstevel@tonic-gate * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
31700Sstevel@tonic-gate */
31710Sstevel@tonic-gate struct modctl *
mod_find_by_filename(char * subdir,char * filename)31720Sstevel@tonic-gate mod_find_by_filename(char *subdir, char *filename)
31730Sstevel@tonic-gate {
31740Sstevel@tonic-gate struct modctl *mp;
31750Sstevel@tonic-gate size_t sublen;
31760Sstevel@tonic-gate
31770Sstevel@tonic-gate ASSERT(!MUTEX_HELD(&mod_lock));
31780Sstevel@tonic-gate if (subdir != NULL)
31790Sstevel@tonic-gate sublen = strlen(subdir);
31800Sstevel@tonic-gate else
31810Sstevel@tonic-gate sublen = 0;
31820Sstevel@tonic-gate
31830Sstevel@tonic-gate mutex_enter(&mod_lock);
31840Sstevel@tonic-gate mp = &modules;
31850Sstevel@tonic-gate do {
31860Sstevel@tonic-gate if (sublen) {
31870Sstevel@tonic-gate char *mod_filename = mp->mod_filename;
31880Sstevel@tonic-gate
31890Sstevel@tonic-gate if (strncmp(subdir, mod_filename, sublen) == 0 &&
31900Sstevel@tonic-gate mod_filename[sublen] == '/' &&
31910Sstevel@tonic-gate strcmp(filename, &mod_filename[sublen + 1]) == 0) {
31920Sstevel@tonic-gate mutex_exit(&mod_lock);
31930Sstevel@tonic-gate return (mp);
31940Sstevel@tonic-gate }
31950Sstevel@tonic-gate } else if (strcmp(filename, mp->mod_filename) == 0) {
31960Sstevel@tonic-gate mutex_exit(&mod_lock);
31970Sstevel@tonic-gate return (mp);
31980Sstevel@tonic-gate }
31990Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules);
32000Sstevel@tonic-gate mutex_exit(&mod_lock);
32010Sstevel@tonic-gate return (NULL);
32020Sstevel@tonic-gate }
32030Sstevel@tonic-gate
32040Sstevel@tonic-gate /*
32050Sstevel@tonic-gate * Check for circular dependencies. This is called from do_dependents()
32060Sstevel@tonic-gate * in kobj.c. If we are the thread already loading this module, then
32070Sstevel@tonic-gate * we're trying to load a dependent that we're already loading which
32080Sstevel@tonic-gate * means the user specified circular dependencies.
32090Sstevel@tonic-gate */
32100Sstevel@tonic-gate static int
mod_circdep(struct modctl * modp)32110Sstevel@tonic-gate mod_circdep(struct modctl *modp)
32120Sstevel@tonic-gate {
32130Sstevel@tonic-gate struct modctl *rmod;
32140Sstevel@tonic-gate
32150Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock));
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate /*
32180Sstevel@tonic-gate * Check the mod_inprogress_thread first.
32190Sstevel@tonic-gate * mod_inprogress_thread is used in mod_hold_stub()
32200Sstevel@tonic-gate * directly to improve performance.
32210Sstevel@tonic-gate */
32220Sstevel@tonic-gate if (modp->mod_inprogress_thread == curthread)
32230Sstevel@tonic-gate return (1);
32240Sstevel@tonic-gate
32250Sstevel@tonic-gate /*
32260Sstevel@tonic-gate * Check the module circular dependencies.
32270Sstevel@tonic-gate */
32280Sstevel@tonic-gate for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
32290Sstevel@tonic-gate /*
32300Sstevel@tonic-gate * Check if there is a module circular dependency.
32310Sstevel@tonic-gate */
32320Sstevel@tonic-gate if (rmod->mod_requisite_loading == modp)
32330Sstevel@tonic-gate return (1);
32340Sstevel@tonic-gate }
32350Sstevel@tonic-gate return (0);
32360Sstevel@tonic-gate }
32370Sstevel@tonic-gate
32380Sstevel@tonic-gate static int
mod_getinfo(struct modctl * modp,struct modinfo * modinfop)32390Sstevel@tonic-gate mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
32400Sstevel@tonic-gate {
32410Sstevel@tonic-gate int (*func)(struct modinfo *);
32420Sstevel@tonic-gate int retval;
32430Sstevel@tonic-gate
32440Sstevel@tonic-gate ASSERT(modp->mod_busy);
32450Sstevel@tonic-gate
32460Sstevel@tonic-gate /* primary modules don't do getinfo */
32470Sstevel@tonic-gate if (modp->mod_prim)
32480Sstevel@tonic-gate return (0);
32490Sstevel@tonic-gate
32500Sstevel@tonic-gate func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
32510Sstevel@tonic-gate
32520Sstevel@tonic-gate if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
32530Sstevel@tonic-gate cmn_err(CE_WARN, "_info() not defined properly in %s",
32540Sstevel@tonic-gate modp->mod_filename);
32550Sstevel@tonic-gate /*
32560Sstevel@tonic-gate * The semantics of mod_info(9F) are that 0 is failure
32570Sstevel@tonic-gate * and non-zero is success.
32580Sstevel@tonic-gate */
32590Sstevel@tonic-gate retval = 0;
32600Sstevel@tonic-gate } else
32610Sstevel@tonic-gate retval = (*func)(modinfop); /* call _info() function */
32620Sstevel@tonic-gate
32630Sstevel@tonic-gate if (moddebug & MODDEBUG_USERDEBUG)
32640Sstevel@tonic-gate printf("Returned from _info, retval = %x\n", retval);
32650Sstevel@tonic-gate
32660Sstevel@tonic-gate return (retval);
32670Sstevel@tonic-gate }
32680Sstevel@tonic-gate
32690Sstevel@tonic-gate static void
modadd(struct modctl * mp)32700Sstevel@tonic-gate modadd(struct modctl *mp)
32710Sstevel@tonic-gate {
32720Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock));
32730Sstevel@tonic-gate
32740Sstevel@tonic-gate mp->mod_id = last_module_id++;
32750Sstevel@tonic-gate mp->mod_next = &modules;
32760Sstevel@tonic-gate mp->mod_prev = modules.mod_prev;
32770Sstevel@tonic-gate modules.mod_prev->mod_next = mp;
32780Sstevel@tonic-gate modules.mod_prev = mp;
32790Sstevel@tonic-gate }
32800Sstevel@tonic-gate
32810Sstevel@tonic-gate /*ARGSUSED*/
32820Sstevel@tonic-gate static struct modctl *
allocate_modp(const char * filename,const char * modname)32831414Scindi allocate_modp(const char *filename, const char *modname)
32840Sstevel@tonic-gate {
32850Sstevel@tonic-gate struct modctl *mp;
32860Sstevel@tonic-gate
32870Sstevel@tonic-gate mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
32880Sstevel@tonic-gate mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
32890Sstevel@tonic-gate (void) strcpy(mp->mod_modname, modname);
32900Sstevel@tonic-gate return (mp);
32910Sstevel@tonic-gate }
32920Sstevel@tonic-gate
32930Sstevel@tonic-gate /*
32940Sstevel@tonic-gate * Get the value of a symbol. This is a wrapper routine that
32950Sstevel@tonic-gate * calls kobj_getsymvalue(). kobj_getsymvalue() may go away but this
32960Sstevel@tonic-gate * wrapper will prevent callers from noticing.
32970Sstevel@tonic-gate */
32980Sstevel@tonic-gate uintptr_t
modgetsymvalue(char * name,int kernelonly)32990Sstevel@tonic-gate modgetsymvalue(char *name, int kernelonly)
33000Sstevel@tonic-gate {
33010Sstevel@tonic-gate return (kobj_getsymvalue(name, kernelonly));
33020Sstevel@tonic-gate }
33030Sstevel@tonic-gate
33040Sstevel@tonic-gate /*
33050Sstevel@tonic-gate * Get the symbol nearest an address. This is a wrapper routine that
33060Sstevel@tonic-gate * calls kobj_getsymname(). kobj_getsymname() may go away but this
33070Sstevel@tonic-gate * wrapper will prevent callers from noticing.
33080Sstevel@tonic-gate */
33090Sstevel@tonic-gate char *
modgetsymname(uintptr_t value,ulong_t * offset)33100Sstevel@tonic-gate modgetsymname(uintptr_t value, ulong_t *offset)
33110Sstevel@tonic-gate {
33120Sstevel@tonic-gate return (kobj_getsymname(value, offset));
33130Sstevel@tonic-gate }
33140Sstevel@tonic-gate
33150Sstevel@tonic-gate /*
33161414Scindi * Lookup a symbol in a specified module. These are wrapper routines that
331710923SEvan.Yan@Sun.COM * call kobj_lookup(). kobj_lookup() may go away but these wrappers will
33181414Scindi * prevent callers from noticing.
33190Sstevel@tonic-gate */
33200Sstevel@tonic-gate uintptr_t
modlookup(const char * modname,const char * symname)33211414Scindi modlookup(const char *modname, const char *symname)
33220Sstevel@tonic-gate {
33230Sstevel@tonic-gate struct modctl *modp;
33240Sstevel@tonic-gate uintptr_t val;
33250Sstevel@tonic-gate
33260Sstevel@tonic-gate if ((modp = mod_hold_by_name(modname)) == NULL)
33270Sstevel@tonic-gate return (0);
33280Sstevel@tonic-gate val = kobj_lookup(modp->mod_mp, symname);
33290Sstevel@tonic-gate mod_release_mod(modp);
33300Sstevel@tonic-gate return (val);
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate
33331414Scindi uintptr_t
modlookup_by_modctl(modctl_t * modp,const char * symname)33341414Scindi modlookup_by_modctl(modctl_t *modp, const char *symname)
33351414Scindi {
33361414Scindi ASSERT(modp->mod_ref > 0 || modp->mod_busy);
33371414Scindi
33381414Scindi return (kobj_lookup(modp->mod_mp, symname));
33391414Scindi }
33401414Scindi
33410Sstevel@tonic-gate /*
33420Sstevel@tonic-gate * Ask the user for the name of the system file and the default path
33430Sstevel@tonic-gate * for modules.
33440Sstevel@tonic-gate */
33450Sstevel@tonic-gate void
mod_askparams()33460Sstevel@tonic-gate mod_askparams()
33470Sstevel@tonic-gate {
33480Sstevel@tonic-gate static char s0[64];
33490Sstevel@tonic-gate intptr_t fd;
33500Sstevel@tonic-gate
33510Sstevel@tonic-gate if ((fd = kobj_open(systemfile)) != -1L)
33520Sstevel@tonic-gate kobj_close(fd);
33530Sstevel@tonic-gate else
33540Sstevel@tonic-gate systemfile = NULL;
33550Sstevel@tonic-gate
33560Sstevel@tonic-gate /*CONSTANTCONDITION*/
33570Sstevel@tonic-gate while (1) {
33580Sstevel@tonic-gate printf("Name of system file [%s]: ",
33594851Scth systemfile ? systemfile : "/dev/null");
33600Sstevel@tonic-gate
33610Sstevel@tonic-gate console_gets(s0, sizeof (s0));
33620Sstevel@tonic-gate
33630Sstevel@tonic-gate if (s0[0] == '\0')
33640Sstevel@tonic-gate break;
33650Sstevel@tonic-gate else if (strcmp(s0, "/dev/null") == 0) {
33660Sstevel@tonic-gate systemfile = NULL;
33670Sstevel@tonic-gate break;
33680Sstevel@tonic-gate } else {
33690Sstevel@tonic-gate if ((fd = kobj_open(s0)) != -1L) {
33700Sstevel@tonic-gate kobj_close(fd);
33710Sstevel@tonic-gate systemfile = s0;
33720Sstevel@tonic-gate break;
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate }
33750Sstevel@tonic-gate printf("can't find file %s\n", s0);
33760Sstevel@tonic-gate }
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate
33790Sstevel@tonic-gate static char loading_msg[] = "loading '%s' id %d\n";
33800Sstevel@tonic-gate static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
33810Sstevel@tonic-gate
33820Sstevel@tonic-gate /*
33830Sstevel@tonic-gate * Common code for loading a module (but not installing it).
33845331Samw * Handoff the task of module loading to a separate thread
33850Sstevel@tonic-gate * with a large stack if possible, since this code may recurse a few times.
33860Sstevel@tonic-gate * Return zero if there are no errors or an errno value.
33870Sstevel@tonic-gate */
33880Sstevel@tonic-gate static int
mod_load(struct modctl * mp,int usepath)33890Sstevel@tonic-gate mod_load(struct modctl *mp, int usepath)
33900Sstevel@tonic-gate {
33910Sstevel@tonic-gate int retval;
33920Sstevel@tonic-gate struct modinfo *modinfop = NULL;
33930Sstevel@tonic-gate struct loadmt lt;
33940Sstevel@tonic-gate
33950Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
33960Sstevel@tonic-gate ASSERT(mp->mod_busy);
33970Sstevel@tonic-gate
33980Sstevel@tonic-gate if (mp->mod_loaded)
33990Sstevel@tonic-gate return (0);
34000Sstevel@tonic-gate
34010Sstevel@tonic-gate if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
34020Sstevel@tonic-gate mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
34030Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG) {
34040Sstevel@tonic-gate printf(mod_excl_msg, mp->mod_filename,
34054851Scth mp->mod_modname);
34060Sstevel@tonic-gate }
34070Sstevel@tonic-gate return (ENXIO);
34080Sstevel@tonic-gate }
34090Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG2)
34100Sstevel@tonic-gate printf(loading_msg, mp->mod_filename, mp->mod_id);
34110Sstevel@tonic-gate
34120Sstevel@tonic-gate if (curthread != &t0) {
34130Sstevel@tonic-gate lt.mp = mp;
34140Sstevel@tonic-gate lt.usepath = usepath;
34150Sstevel@tonic-gate lt.owner = curthread;
34160Sstevel@tonic-gate sema_init(<.sema, 0, NULL, SEMA_DEFAULT, NULL);
34170Sstevel@tonic-gate
34180Sstevel@tonic-gate /* create thread to hand of call to */
34190Sstevel@tonic-gate (void) thread_create(NULL, DEFAULTSTKSZ * 2,
34200Sstevel@tonic-gate modload_thread, <, 0, &p0, TS_RUN, maxclsyspri);
34210Sstevel@tonic-gate
34220Sstevel@tonic-gate /* wait for thread to complete kobj_load_module */
34230Sstevel@tonic-gate sema_p(<.sema);
34240Sstevel@tonic-gate
34250Sstevel@tonic-gate sema_destroy(<.sema);
34260Sstevel@tonic-gate retval = lt.retval;
34270Sstevel@tonic-gate } else
34280Sstevel@tonic-gate retval = kobj_load_module(mp, usepath);
34290Sstevel@tonic-gate
34300Sstevel@tonic-gate if (mp->mod_mp) {
34310Sstevel@tonic-gate ASSERT(retval == 0);
34320Sstevel@tonic-gate mp->mod_loaded = 1;
34330Sstevel@tonic-gate mp->mod_loadcnt++;
34340Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG) {
34350Sstevel@tonic-gate printf(load_msg, mp->mod_filename, mp->mod_id,
34364851Scth (void *)((struct module *)mp->mod_mp)->text,
34374851Scth (void *)((struct module *)mp->mod_mp)->data,
34384851Scth ((struct module *)mp->mod_mp)->text_size,
34394851Scth ((struct module *)mp->mod_mp)->data_size);
34400Sstevel@tonic-gate }
34410Sstevel@tonic-gate
34420Sstevel@tonic-gate /*
34430Sstevel@tonic-gate * XXX - There should be a better way to get this.
34440Sstevel@tonic-gate */
34450Sstevel@tonic-gate modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
34460Sstevel@tonic-gate modinfop->mi_info = MI_INFO_LINKAGE;
34470Sstevel@tonic-gate if (mod_getinfo(mp, modinfop) == 0)
34480Sstevel@tonic-gate mp->mod_linkage = NULL;
34490Sstevel@tonic-gate else {
34500Sstevel@tonic-gate mp->mod_linkage = (void *)modinfop->mi_base;
34510Sstevel@tonic-gate ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
34520Sstevel@tonic-gate }
34530Sstevel@tonic-gate
34540Sstevel@tonic-gate /*
34550Sstevel@tonic-gate * DCS: bootstrapping code. If the driver is loaded
34560Sstevel@tonic-gate * before root mount, it is assumed that the driver
34570Sstevel@tonic-gate * may be used before mounting root. In order to
34580Sstevel@tonic-gate * access mappings of global to local minor no.'s
34590Sstevel@tonic-gate * during installation/open of the driver, we load
34600Sstevel@tonic-gate * them into memory here while the BOP_interfaces
34610Sstevel@tonic-gate * are still up.
34620Sstevel@tonic-gate */
34630Sstevel@tonic-gate if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
34640Sstevel@tonic-gate retval = clboot_modload(mp);
34650Sstevel@tonic-gate }
34660Sstevel@tonic-gate
34670Sstevel@tonic-gate kmem_free(modinfop, sizeof (struct modinfo));
34680Sstevel@tonic-gate (void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
34690Sstevel@tonic-gate retval = install_stubs_by_name(mp, mp->mod_modname);
34700Sstevel@tonic-gate
34710Sstevel@tonic-gate /*
34720Sstevel@tonic-gate * Now that the module is loaded, we need to give DTrace
34730Sstevel@tonic-gate * a chance to notify its providers. This is done via
34740Sstevel@tonic-gate * the dtrace_modload function pointer.
34750Sstevel@tonic-gate */
34760Sstevel@tonic-gate if (strcmp(mp->mod_modname, "dtrace") != 0) {
34770Sstevel@tonic-gate struct modctl *dmp = mod_hold_by_name("dtrace");
34780Sstevel@tonic-gate
34790Sstevel@tonic-gate if (dmp != NULL && dtrace_modload != NULL)
34800Sstevel@tonic-gate (*dtrace_modload)(mp);
34810Sstevel@tonic-gate
34820Sstevel@tonic-gate mod_release_mod(dmp);
34830Sstevel@tonic-gate }
34840Sstevel@tonic-gate
34850Sstevel@tonic-gate } else {
34860Sstevel@tonic-gate /*
34870Sstevel@tonic-gate * If load failed then we need to release any requisites
34880Sstevel@tonic-gate * that we had established.
34890Sstevel@tonic-gate */
34900Sstevel@tonic-gate ASSERT(retval);
34910Sstevel@tonic-gate mod_release_requisites(mp);
34920Sstevel@tonic-gate
34930Sstevel@tonic-gate if (moddebug & MODDEBUG_ERRMSG)
34940Sstevel@tonic-gate printf("error loading '%s', error %d\n",
34950Sstevel@tonic-gate mp->mod_filename, retval);
34960Sstevel@tonic-gate }
34970Sstevel@tonic-gate return (retval);
34980Sstevel@tonic-gate }
34990Sstevel@tonic-gate
35000Sstevel@tonic-gate static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
35010Sstevel@tonic-gate
35020Sstevel@tonic-gate static void
mod_unload(struct modctl * mp)35030Sstevel@tonic-gate mod_unload(struct modctl *mp)
35040Sstevel@tonic-gate {
35050Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
35060Sstevel@tonic-gate ASSERT(mp->mod_busy);
35070Sstevel@tonic-gate ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
35080Sstevel@tonic-gate ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
35090Sstevel@tonic-gate
35100Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG)
35110Sstevel@tonic-gate printf(unload_msg, mp->mod_modname,
35124851Scth mp->mod_id, mp->mod_loadcnt);
35130Sstevel@tonic-gate
35140Sstevel@tonic-gate /*
35150Sstevel@tonic-gate * If mod_ref is not zero, it means some modules might still refer
35160Sstevel@tonic-gate * to this module. Then you can't unload this module right now.
35170Sstevel@tonic-gate * Instead, set 1 to mod_delay_unload to notify the system of
35180Sstevel@tonic-gate * unloading this module later when it's not required any more.
35190Sstevel@tonic-gate */
35200Sstevel@tonic-gate if (mp->mod_ref > 0) {
35210Sstevel@tonic-gate mp->mod_delay_unload = 1;
35220Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG2) {
35230Sstevel@tonic-gate printf("module %s not unloaded,"
35240Sstevel@tonic-gate " non-zero reference count (%d)",
35250Sstevel@tonic-gate mp->mod_modname, mp->mod_ref);
35260Sstevel@tonic-gate }
35270Sstevel@tonic-gate return;
35280Sstevel@tonic-gate }
35290Sstevel@tonic-gate
35300Sstevel@tonic-gate if (((mp->mod_loaded == 0) || mp->mod_installed) ||
35310Sstevel@tonic-gate (mp->mod_ref || mp->mod_prim)) {
35320Sstevel@tonic-gate /*
35330Sstevel@tonic-gate * A DEBUG kernel would ASSERT panic above, the code is broken
35340Sstevel@tonic-gate * if we get this warning.
35350Sstevel@tonic-gate */
35360Sstevel@tonic-gate cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
35370Sstevel@tonic-gate mp->mod_filename, mp->mod_installed, mp->mod_loaded,
35380Sstevel@tonic-gate mp->mod_ref);
35390Sstevel@tonic-gate return;
35400Sstevel@tonic-gate }
35410Sstevel@tonic-gate
35420Sstevel@tonic-gate /* reset stub functions to call the binder again */
35430Sstevel@tonic-gate reset_stubs(mp);
35440Sstevel@tonic-gate
35450Sstevel@tonic-gate /*
35460Sstevel@tonic-gate * mark module as unloaded before the modctl structure is freed.
35470Sstevel@tonic-gate * This is required not to reuse the modctl structure before
35480Sstevel@tonic-gate * the module is marked as unloaded.
35490Sstevel@tonic-gate */
35500Sstevel@tonic-gate mp->mod_loaded = 0;
35510Sstevel@tonic-gate mp->mod_linkage = NULL;
35520Sstevel@tonic-gate
35530Sstevel@tonic-gate /* free the memory */
35540Sstevel@tonic-gate kobj_unload_module(mp);
35550Sstevel@tonic-gate
35560Sstevel@tonic-gate if (mp->mod_delay_unload) {
35570Sstevel@tonic-gate mp->mod_delay_unload = 0;
35580Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG2) {
35590Sstevel@tonic-gate printf("deferred unload of module %s"
35600Sstevel@tonic-gate " (id %d) successful",
35610Sstevel@tonic-gate mp->mod_modname, mp->mod_id);
35620Sstevel@tonic-gate }
35630Sstevel@tonic-gate }
35640Sstevel@tonic-gate
35650Sstevel@tonic-gate /* release hold on requisites */
35660Sstevel@tonic-gate mod_release_requisites(mp);
35670Sstevel@tonic-gate
35680Sstevel@tonic-gate /*
35690Sstevel@tonic-gate * Now that the module is gone, we need to give DTrace a chance to
35700Sstevel@tonic-gate * remove any probes that it may have had in the module. This is
35710Sstevel@tonic-gate * done via the dtrace_modunload function pointer.
35720Sstevel@tonic-gate */
35730Sstevel@tonic-gate if (strcmp(mp->mod_modname, "dtrace") != 0) {
35740Sstevel@tonic-gate struct modctl *dmp = mod_hold_by_name("dtrace");
35750Sstevel@tonic-gate
35760Sstevel@tonic-gate if (dmp != NULL && dtrace_modunload != NULL)
35770Sstevel@tonic-gate (*dtrace_modunload)(mp);
35780Sstevel@tonic-gate
35790Sstevel@tonic-gate mod_release_mod(dmp);
35800Sstevel@tonic-gate }
35810Sstevel@tonic-gate }
35820Sstevel@tonic-gate
35830Sstevel@tonic-gate static int
modinstall(struct modctl * mp)35840Sstevel@tonic-gate modinstall(struct modctl *mp)
35850Sstevel@tonic-gate {
35860Sstevel@tonic-gate int val;
35870Sstevel@tonic-gate int (*func)(void);
35880Sstevel@tonic-gate
35890Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
35900Sstevel@tonic-gate ASSERT(mp->mod_busy && mp->mod_loaded);
35910Sstevel@tonic-gate
35920Sstevel@tonic-gate if (mp->mod_installed)
35930Sstevel@tonic-gate return (0);
35940Sstevel@tonic-gate /*
35950Sstevel@tonic-gate * If mod_delay_unload is on, it means the system chose the deferred
35960Sstevel@tonic-gate * unload for this module. Then you can't install this module until
35970Sstevel@tonic-gate * it's unloaded from the system.
35980Sstevel@tonic-gate */
35990Sstevel@tonic-gate if (mp->mod_delay_unload)
36000Sstevel@tonic-gate return (ENXIO);
36010Sstevel@tonic-gate
36020Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG)
36030Sstevel@tonic-gate printf("installing %s, module id %d.\n",
36044851Scth mp->mod_modname, mp->mod_id);
36050Sstevel@tonic-gate
36060Sstevel@tonic-gate ASSERT(mp->mod_mp != NULL);
36070Sstevel@tonic-gate if (mod_install_requisites(mp) != 0) {
36080Sstevel@tonic-gate /*
36090Sstevel@tonic-gate * Note that we can't call mod_unload(mp) here since
36100Sstevel@tonic-gate * if modinstall() was called by mod_install_requisites(),
36110Sstevel@tonic-gate * we won't be able to hold the dependent modules
36120Sstevel@tonic-gate * (otherwise there would be a deadlock).
36130Sstevel@tonic-gate */
36140Sstevel@tonic-gate return (ENXIO);
36150Sstevel@tonic-gate }
36160Sstevel@tonic-gate
36170Sstevel@tonic-gate if (moddebug & MODDEBUG_ERRMSG) {
36180Sstevel@tonic-gate printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
36194851Scth mp->mod_filename, mp->mod_id,
36204851Scth (void *)((struct module *)mp->mod_mp)->text,
36214851Scth (void *)((struct module *)mp->mod_mp)->data,
36224851Scth ((struct module *)mp->mod_mp)->text_size,
36234851Scth ((struct module *)mp->mod_mp)->data_size);
36240Sstevel@tonic-gate }
36250Sstevel@tonic-gate
36260Sstevel@tonic-gate func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
36270Sstevel@tonic-gate
36280Sstevel@tonic-gate if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
36290Sstevel@tonic-gate cmn_err(CE_WARN, "_init() not defined properly in %s",
36300Sstevel@tonic-gate mp->mod_filename);
36310Sstevel@tonic-gate return (EFAULT);
36320Sstevel@tonic-gate }
36330Sstevel@tonic-gate
36340Sstevel@tonic-gate if (moddebug & MODDEBUG_USERDEBUG) {
36350Sstevel@tonic-gate printf("breakpoint before calling %s:_init()\n",
36360Sstevel@tonic-gate mp->mod_modname);
36370Sstevel@tonic-gate if (DEBUGGER_PRESENT)
36380Sstevel@tonic-gate debug_enter("_init");
36390Sstevel@tonic-gate }
36400Sstevel@tonic-gate
36410Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
36420Sstevel@tonic-gate ASSERT(mp->mod_busy && mp->mod_loaded);
36430Sstevel@tonic-gate val = (*func)(); /* call _init */
36440Sstevel@tonic-gate
36450Sstevel@tonic-gate if (moddebug & MODDEBUG_USERDEBUG)
36460Sstevel@tonic-gate printf("Returned from _init, val = %x\n", val);
36470Sstevel@tonic-gate
36480Sstevel@tonic-gate if (val == 0) {
36490Sstevel@tonic-gate /*
36500Sstevel@tonic-gate * Set the MODS_INSTALLED flag to enable this module
36510Sstevel@tonic-gate * being called now.
36520Sstevel@tonic-gate */
36530Sstevel@tonic-gate install_stubs(mp);
36540Sstevel@tonic-gate mp->mod_installed = 1;
36550Sstevel@tonic-gate } else if (moddebug & MODDEBUG_ERRMSG)
36560Sstevel@tonic-gate printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
36570Sstevel@tonic-gate
36580Sstevel@tonic-gate return (val);
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate
36611218Scth int detach_driver_unconfig = 0;
36621218Scth
36630Sstevel@tonic-gate static int
detach_driver(char * name)36640Sstevel@tonic-gate detach_driver(char *name)
36650Sstevel@tonic-gate {
36660Sstevel@tonic-gate major_t major;
36670Sstevel@tonic-gate int error;
36680Sstevel@tonic-gate
36690Sstevel@tonic-gate /*
36700Sstevel@tonic-gate * If being called from mod_uninstall_all() then the appropriate
36710Sstevel@tonic-gate * driver detaches (leaf only) have already been done.
36720Sstevel@tonic-gate */
36730Sstevel@tonic-gate if (mod_in_autounload())
36740Sstevel@tonic-gate return (0);
36750Sstevel@tonic-gate
36760Sstevel@tonic-gate major = ddi_name_to_major(name);
36777009Scth if (major == DDI_MAJOR_T_NONE)
36780Sstevel@tonic-gate return (0);
36790Sstevel@tonic-gate
36800Sstevel@tonic-gate error = ndi_devi_unconfig_driver(ddi_root_node(),
36811218Scth NDI_DETACH_DRIVER | detach_driver_unconfig, major);
36820Sstevel@tonic-gate return (error == NDI_SUCCESS ? 0 : -1);
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate
36850Sstevel@tonic-gate static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
36860Sstevel@tonic-gate
36870Sstevel@tonic-gate static int
moduninstall(struct modctl * mp)36880Sstevel@tonic-gate moduninstall(struct modctl *mp)
36890Sstevel@tonic-gate {
36900Sstevel@tonic-gate int status = 0;
36910Sstevel@tonic-gate int (*func)(void);
36920Sstevel@tonic-gate
36930Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
36940Sstevel@tonic-gate ASSERT(mp->mod_busy);
36950Sstevel@tonic-gate
36960Sstevel@tonic-gate /*
36970Sstevel@tonic-gate * Verify that we need to do something and can uninstall the module.
36980Sstevel@tonic-gate *
36990Sstevel@tonic-gate * If we should not uninstall the module or if the module is not in
37000Sstevel@tonic-gate * the correct state to start an uninstall we return EBUSY to prevent
37010Sstevel@tonic-gate * us from progressing to mod_unload. If the module has already been
37020Sstevel@tonic-gate * uninstalled and unloaded we return EALREADY.
37030Sstevel@tonic-gate */
37040Sstevel@tonic-gate if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
37050Sstevel@tonic-gate return (EBUSY);
37060Sstevel@tonic-gate if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
37070Sstevel@tonic-gate return (EALREADY);
37080Sstevel@tonic-gate
37090Sstevel@tonic-gate /*
37100Sstevel@tonic-gate * To avoid devinfo / module deadlock we must release this module
37110Sstevel@tonic-gate * prior to initiating the detach_driver, otherwise the detach_driver
37120Sstevel@tonic-gate * might deadlock on a devinfo node held by another thread
37130Sstevel@tonic-gate * coming top down and involving the module we have locked.
37140Sstevel@tonic-gate *
37150Sstevel@tonic-gate * When we regrab the module we must reverify that it is OK
37160Sstevel@tonic-gate * to proceed with the uninstall operation.
37170Sstevel@tonic-gate */
37180Sstevel@tonic-gate mod_release_mod(mp);
37190Sstevel@tonic-gate status = detach_driver(mp->mod_modname);
37200Sstevel@tonic-gate (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
37210Sstevel@tonic-gate
37220Sstevel@tonic-gate /* check detach status and reverify state with lock */
37230Sstevel@tonic-gate mutex_enter(&mod_lock);
37240Sstevel@tonic-gate if ((status != 0) || mp->mod_prim || mp->mod_ref) {
37250Sstevel@tonic-gate mutex_exit(&mod_lock);
37260Sstevel@tonic-gate return (EBUSY);
37270Sstevel@tonic-gate }
37280Sstevel@tonic-gate if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
37290Sstevel@tonic-gate mutex_exit(&mod_lock);
37300Sstevel@tonic-gate return (EALREADY);
37310Sstevel@tonic-gate }
37320Sstevel@tonic-gate mutex_exit(&mod_lock);
37330Sstevel@tonic-gate
37340Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG2)
37350Sstevel@tonic-gate printf("uninstalling %s\n", mp->mod_modname);
37360Sstevel@tonic-gate
37370Sstevel@tonic-gate /*
37380Sstevel@tonic-gate * lookup _fini, return EBUSY if not defined.
37390Sstevel@tonic-gate *
37400Sstevel@tonic-gate * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
37410Sstevel@tonic-gate * detach(9E) - it allows bufctl addresses to be resolved.
37420Sstevel@tonic-gate */
37430Sstevel@tonic-gate func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
37440Sstevel@tonic-gate if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
37450Sstevel@tonic-gate (moddebug & MODDEBUG_FINI_EBUSY))
37460Sstevel@tonic-gate return (EBUSY);
37470Sstevel@tonic-gate
37480Sstevel@tonic-gate /* verify that _fini is in this module */
37490Sstevel@tonic-gate if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
37500Sstevel@tonic-gate cmn_err(CE_WARN, "_fini() not defined properly in %s",
37510Sstevel@tonic-gate mp->mod_filename);
37520Sstevel@tonic-gate return (EFAULT);
37530Sstevel@tonic-gate }
37540Sstevel@tonic-gate
37550Sstevel@tonic-gate /* call _fini() */
37560Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
37570Sstevel@tonic-gate ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
37580Sstevel@tonic-gate
37590Sstevel@tonic-gate status = (*func)();
37600Sstevel@tonic-gate
37610Sstevel@tonic-gate if (status == 0) {
37620Sstevel@tonic-gate /* _fini returned success, the module is no longer installed */
37630Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG)
37640Sstevel@tonic-gate printf("uninstalled %s\n", mp->mod_modname);
37650Sstevel@tonic-gate
37660Sstevel@tonic-gate /*
37670Sstevel@tonic-gate * Even though we only set mod_installed to zero here, a zero
37685331Samw * return value means we are committed to a code path were
37690Sstevel@tonic-gate * mod_loaded will also end up as zero - we have no other
37700Sstevel@tonic-gate * way to get the module data and bss back to the pre _init
37710Sstevel@tonic-gate * state except a reload. To ensure this, after return,
37720Sstevel@tonic-gate * mod_busy must stay set until mod_loaded is cleared.
37730Sstevel@tonic-gate */
37740Sstevel@tonic-gate mp->mod_installed = 0;
37750Sstevel@tonic-gate
37760Sstevel@tonic-gate /*
37770Sstevel@tonic-gate * Clear the MODS_INSTALLED flag not to call functions
37780Sstevel@tonic-gate * in the module directly from now on.
37790Sstevel@tonic-gate */
37800Sstevel@tonic-gate uninstall_stubs(mp);
37810Sstevel@tonic-gate } else {
37820Sstevel@tonic-gate if (moddebug & MODDEBUG_USERDEBUG)
37830Sstevel@tonic-gate printf(finiret_msg, mp->mod_filename, status);
37840Sstevel@tonic-gate /*
37850Sstevel@tonic-gate * By definition _fini is only allowed to return EBUSY or the
37860Sstevel@tonic-gate * result of mod_remove (EBUSY or EINVAL). In the off chance
37870Sstevel@tonic-gate * that a driver returns EALREADY we convert this to EINVAL
37880Sstevel@tonic-gate * since to our caller EALREADY means module was already
37890Sstevel@tonic-gate * removed.
37900Sstevel@tonic-gate */
37910Sstevel@tonic-gate if (status == EALREADY)
37920Sstevel@tonic-gate status = EINVAL;
37930Sstevel@tonic-gate }
37940Sstevel@tonic-gate
37950Sstevel@tonic-gate return (status);
37960Sstevel@tonic-gate }
37970Sstevel@tonic-gate
37980Sstevel@tonic-gate /*
37990Sstevel@tonic-gate * Uninstall all modules.
38000Sstevel@tonic-gate */
38010Sstevel@tonic-gate static void
mod_uninstall_all(void)38020Sstevel@tonic-gate mod_uninstall_all(void)
38030Sstevel@tonic-gate {
38040Sstevel@tonic-gate struct modctl *mp;
3805*12207SChris.Horne@Sun.COM int pass;
3806*12207SChris.Horne@Sun.COM modid_t modid;
38070Sstevel@tonic-gate
38081218Scth /* synchronize with any active modunload_disable() */
38091218Scth modunload_begin();
38101218Scth
38110Sstevel@tonic-gate /* mark this thread as doing autounloading */
38120Sstevel@tonic-gate (void) tsd_set(mod_autounload_key, (void *)1);
38130Sstevel@tonic-gate
38140Sstevel@tonic-gate (void) devfs_clean(ddi_root_node(), NULL, 0);
38150Sstevel@tonic-gate (void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
38160Sstevel@tonic-gate
3817*12207SChris.Horne@Sun.COM /*
3818*12207SChris.Horne@Sun.COM * Loop up to max times if we keep producing unreferenced modules.
3819*12207SChris.Horne@Sun.COM * A new unreferenced module is an opportunity to unload.
3820*12207SChris.Horne@Sun.COM */
3821*12207SChris.Horne@Sun.COM for (pass = 0; pass < mod_uninstall_pass_max; pass++) {
3822*12207SChris.Horne@Sun.COM
3823*12207SChris.Horne@Sun.COM /* zero count of modules that go unreferenced during pass */
3824*12207SChris.Horne@Sun.COM mod_uninstall_ref_zero = 0;
3825*12207SChris.Horne@Sun.COM
3826*12207SChris.Horne@Sun.COM modid = 0;
3827*12207SChris.Horne@Sun.COM while ((mp = mod_hold_next_by_id(modid)) != NULL) {
3828*12207SChris.Horne@Sun.COM modid = mp->mod_id;
3829*12207SChris.Horne@Sun.COM
3830*12207SChris.Horne@Sun.COM /*
3831*12207SChris.Horne@Sun.COM * Skip modules with the MOD_NOAUTOUNLOAD flag set
3832*12207SChris.Horne@Sun.COM */
3833*12207SChris.Horne@Sun.COM if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
3834*12207SChris.Horne@Sun.COM mod_release_mod(mp);
3835*12207SChris.Horne@Sun.COM continue;
3836*12207SChris.Horne@Sun.COM }
3837*12207SChris.Horne@Sun.COM
3838*12207SChris.Horne@Sun.COM if (moduninstall(mp) == 0) {
3839*12207SChris.Horne@Sun.COM mod_unload(mp);
3840*12207SChris.Horne@Sun.COM CPU_STATS_ADDQ(CPU, sys, modunload, 1);
3841*12207SChris.Horne@Sun.COM }
38420Sstevel@tonic-gate mod_release_mod(mp);
38430Sstevel@tonic-gate }
38440Sstevel@tonic-gate
3845*12207SChris.Horne@Sun.COM /* break if no modules went unreferenced during pass */
3846*12207SChris.Horne@Sun.COM if (mod_uninstall_ref_zero == 0)
3847*12207SChris.Horne@Sun.COM break;
38480Sstevel@tonic-gate }
3849*12207SChris.Horne@Sun.COM if (pass >= mod_uninstall_pass_max)
3850*12207SChris.Horne@Sun.COM mod_uninstall_pass_exc++;
38510Sstevel@tonic-gate
38520Sstevel@tonic-gate (void) tsd_set(mod_autounload_key, NULL);
38531218Scth modunload_end();
38540Sstevel@tonic-gate }
38550Sstevel@tonic-gate
38561218Scth /* wait for unloads that have begun before registering disable */
38570Sstevel@tonic-gate void
modunload_disable(void)38580Sstevel@tonic-gate modunload_disable(void)
38590Sstevel@tonic-gate {
38601218Scth mutex_enter(&modunload_wait_mutex);
38611218Scth while (modunload_active_count) {
38621218Scth modunload_wait++;
38631218Scth cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
38641218Scth modunload_wait--;
38651218Scth }
38661218Scth modunload_disable_count++;
38671218Scth mutex_exit(&modunload_wait_mutex);
38680Sstevel@tonic-gate }
38690Sstevel@tonic-gate
38701218Scth /* mark end of disable and signal waiters */
38710Sstevel@tonic-gate void
modunload_enable(void)38720Sstevel@tonic-gate modunload_enable(void)
38730Sstevel@tonic-gate {
38741218Scth mutex_enter(&modunload_wait_mutex);
38751218Scth modunload_disable_count--;
38761218Scth if ((modunload_disable_count == 0) && modunload_wait)
38771218Scth cv_broadcast(&modunload_wait_cv);
38781218Scth mutex_exit(&modunload_wait_mutex);
38791218Scth }
38801218Scth
38811218Scth /* wait for disables to complete before begining unload */
38821218Scth void
modunload_begin()38831218Scth modunload_begin()
38841218Scth {
38851218Scth mutex_enter(&modunload_wait_mutex);
38861218Scth while (modunload_disable_count) {
38871218Scth modunload_wait++;
38881218Scth cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
38891218Scth modunload_wait--;
38901218Scth }
38911218Scth modunload_active_count++;
38921218Scth mutex_exit(&modunload_wait_mutex);
38931218Scth }
38941218Scth
38951218Scth /* mark end of unload and signal waiters */
38961218Scth void
modunload_end()38971218Scth modunload_end()
38981218Scth {
38991218Scth mutex_enter(&modunload_wait_mutex);
39001218Scth modunload_active_count--;
39011218Scth if ((modunload_active_count == 0) && modunload_wait)
39021218Scth cv_broadcast(&modunload_wait_cv);
39031218Scth mutex_exit(&modunload_wait_mutex);
39040Sstevel@tonic-gate }
39050Sstevel@tonic-gate
39060Sstevel@tonic-gate void
mod_uninstall_daemon(void)39070Sstevel@tonic-gate mod_uninstall_daemon(void)
39080Sstevel@tonic-gate {
39090Sstevel@tonic-gate callb_cpr_t cprinfo;
391011066Srafael.vanoni@sun.com clock_t ticks;
39110Sstevel@tonic-gate
39120Sstevel@tonic-gate mod_aul_thread = curthread;
39130Sstevel@tonic-gate
39140Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
39150Sstevel@tonic-gate for (;;) {
39160Sstevel@tonic-gate mutex_enter(&mod_uninstall_lock);
39170Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
39180Sstevel@tonic-gate /*
39190Sstevel@tonic-gate * In DEBUG kernels, unheld drivers are uninstalled periodically
39200Sstevel@tonic-gate * every mod_uninstall_interval seconds. Periodic uninstall can
39210Sstevel@tonic-gate * be disabled by setting mod_uninstall_interval to 0 which is
39220Sstevel@tonic-gate * the default for a non-DEBUG kernel.
39230Sstevel@tonic-gate */
39240Sstevel@tonic-gate if (mod_uninstall_interval) {
392511066Srafael.vanoni@sun.com ticks = drv_usectohz(mod_uninstall_interval * 1000000);
392611066Srafael.vanoni@sun.com (void) cv_reltimedwait(&mod_uninstall_cv,
392711066Srafael.vanoni@sun.com &mod_uninstall_lock, ticks, TR_CLOCK_TICK);
39280Sstevel@tonic-gate } else {
39290Sstevel@tonic-gate cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
39300Sstevel@tonic-gate }
39310Sstevel@tonic-gate /*
39320Sstevel@tonic-gate * The whole daemon is safe for CPR except we don't want
39330Sstevel@tonic-gate * the daemon to run if FREEZE is issued and this daemon
39340Sstevel@tonic-gate * wakes up from the cv_wait above. In this case, it'll be
39350Sstevel@tonic-gate * blocked in CALLB_CPR_SAFE_END until THAW is issued.
39360Sstevel@tonic-gate *
39370Sstevel@tonic-gate * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
39380Sstevel@tonic-gate * mod_uninstall_lock is used to protect cprinfo and
39390Sstevel@tonic-gate * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
39400Sstevel@tonic-gate * called.
39410Sstevel@tonic-gate */
39420Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
39430Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
39440Sstevel@tonic-gate mutex_exit(&mod_uninstall_lock);
39450Sstevel@tonic-gate if ((modunload_disable_count == 0) &&
39460Sstevel@tonic-gate ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
39470Sstevel@tonic-gate mod_uninstall_all();
39480Sstevel@tonic-gate }
39490Sstevel@tonic-gate }
39500Sstevel@tonic-gate }
39510Sstevel@tonic-gate
39520Sstevel@tonic-gate /*
39530Sstevel@tonic-gate * Unload all uninstalled modules.
39540Sstevel@tonic-gate */
39550Sstevel@tonic-gate void
modreap(void)39560Sstevel@tonic-gate modreap(void)
39570Sstevel@tonic-gate {
39580Sstevel@tonic-gate mutex_enter(&mod_uninstall_lock);
39590Sstevel@tonic-gate cv_broadcast(&mod_uninstall_cv);
39600Sstevel@tonic-gate mutex_exit(&mod_uninstall_lock);
39610Sstevel@tonic-gate }
39620Sstevel@tonic-gate
39630Sstevel@tonic-gate /*
39640Sstevel@tonic-gate * Hold the specified module. This is the module holding primitive.
39650Sstevel@tonic-gate *
39660Sstevel@tonic-gate * If MOD_LOCK_HELD then the caller already holds the mod_lock.
39670Sstevel@tonic-gate *
39680Sstevel@tonic-gate * Return values:
39690Sstevel@tonic-gate * 0 ==> the module is held
39700Sstevel@tonic-gate * 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
39710Sstevel@tonic-gate * to determine how to retry.
39720Sstevel@tonic-gate */
39730Sstevel@tonic-gate int
mod_hold_by_modctl(struct modctl * mp,int f)39740Sstevel@tonic-gate mod_hold_by_modctl(struct modctl *mp, int f)
39750Sstevel@tonic-gate {
39760Sstevel@tonic-gate ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
39770Sstevel@tonic-gate ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
39780Sstevel@tonic-gate (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
39790Sstevel@tonic-gate ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
39800Sstevel@tonic-gate ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
39810Sstevel@tonic-gate (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
39820Sstevel@tonic-gate ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
39830Sstevel@tonic-gate
39840Sstevel@tonic-gate if (f & MOD_LOCK_NOT_HELD)
39850Sstevel@tonic-gate mutex_enter(&mod_lock);
39860Sstevel@tonic-gate
39870Sstevel@tonic-gate while (mp->mod_busy) {
39880Sstevel@tonic-gate mp->mod_want = 1;
39890Sstevel@tonic-gate cv_wait(&mod_cv, &mod_lock);
39900Sstevel@tonic-gate /*
39910Sstevel@tonic-gate * Module may be unloaded by daemon.
39920Sstevel@tonic-gate * Nevertheless, modctl structure is still in linked list
39930Sstevel@tonic-gate * (i.e., off &modules), not freed!
39940Sstevel@tonic-gate * Caller is not supposed to assume "mp" is valid, but there
39950Sstevel@tonic-gate * is no reasonable way to detect this but using
39960Sstevel@tonic-gate * mp->mod_modinfo->mp == NULL check (follow the back pointer)
39970Sstevel@tonic-gate * (or similar check depending on calling context)
39980Sstevel@tonic-gate * DON'T free modctl structure, it will be very very
39990Sstevel@tonic-gate * problematic.
40000Sstevel@tonic-gate */
40010Sstevel@tonic-gate if (f & MOD_WAIT_ONCE) {
40020Sstevel@tonic-gate if (f & MOD_LOCK_NOT_HELD)
40030Sstevel@tonic-gate mutex_exit(&mod_lock);
40040Sstevel@tonic-gate return (1); /* caller decides how to retry */
40050Sstevel@tonic-gate }
40060Sstevel@tonic-gate }
40070Sstevel@tonic-gate
40080Sstevel@tonic-gate mp->mod_busy = 1;
40090Sstevel@tonic-gate mp->mod_inprogress_thread =
40100Sstevel@tonic-gate (curthread == NULL ? (kthread_id_t)-1 : curthread);
40110Sstevel@tonic-gate
40120Sstevel@tonic-gate if (f & MOD_LOCK_NOT_HELD)
40130Sstevel@tonic-gate mutex_exit(&mod_lock);
40140Sstevel@tonic-gate return (0);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate
40170Sstevel@tonic-gate static struct modctl *
mod_hold_by_name_common(struct modctl * dep,const char * filename)40181414Scindi mod_hold_by_name_common(struct modctl *dep, const char *filename)
40190Sstevel@tonic-gate {
40201414Scindi const char *modname;
40210Sstevel@tonic-gate struct modctl *mp;
40220Sstevel@tonic-gate char *curname, *newname;
40230Sstevel@tonic-gate int found = 0;
40240Sstevel@tonic-gate
40250Sstevel@tonic-gate mutex_enter(&mod_lock);
40260Sstevel@tonic-gate
40270Sstevel@tonic-gate if ((modname = strrchr(filename, '/')) == NULL)
40280Sstevel@tonic-gate modname = filename;
40290Sstevel@tonic-gate else
40300Sstevel@tonic-gate modname++;
40310Sstevel@tonic-gate
40320Sstevel@tonic-gate mp = &modules;
40330Sstevel@tonic-gate do {
40340Sstevel@tonic-gate if (strcmp(modname, mp->mod_modname) == 0) {
40350Sstevel@tonic-gate found = 1;
40360Sstevel@tonic-gate break;
40370Sstevel@tonic-gate }
40380Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules);
40390Sstevel@tonic-gate
40400Sstevel@tonic-gate if (found == 0) {
40410Sstevel@tonic-gate mp = allocate_modp(filename, modname);
40420Sstevel@tonic-gate modadd(mp);
40430Sstevel@tonic-gate }
40440Sstevel@tonic-gate
40450Sstevel@tonic-gate /*
40460Sstevel@tonic-gate * if dep is not NULL, set the mp in mod_requisite_loading for
40470Sstevel@tonic-gate * the module circular dependency check. This field is used in
40480Sstevel@tonic-gate * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
40490Sstevel@tonic-gate */
40500Sstevel@tonic-gate if (dep != NULL) {
40510Sstevel@tonic-gate ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
40520Sstevel@tonic-gate dep->mod_requisite_loading = mp;
40530Sstevel@tonic-gate }
40540Sstevel@tonic-gate
40550Sstevel@tonic-gate /*
40560Sstevel@tonic-gate * If the module was held, then it must be us who has it held.
40570Sstevel@tonic-gate */
40580Sstevel@tonic-gate if (mod_circdep(mp))
40590Sstevel@tonic-gate mp = NULL;
40600Sstevel@tonic-gate else {
40610Sstevel@tonic-gate (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
40620Sstevel@tonic-gate
40630Sstevel@tonic-gate /*
40640Sstevel@tonic-gate * If the name hadn't been set or has changed, allocate
40650Sstevel@tonic-gate * space and set it. Free space used by previous name.
40660Sstevel@tonic-gate *
40670Sstevel@tonic-gate * Do not change the name of primary modules, for primary
40680Sstevel@tonic-gate * modules the mod_filename was allocated in standalone mode:
40690Sstevel@tonic-gate * it is illegal to kobj_alloc in standalone mode and kobj_free
40700Sstevel@tonic-gate * in non-standalone mode.
40710Sstevel@tonic-gate */
40720Sstevel@tonic-gate curname = mp->mod_filename;
40730Sstevel@tonic-gate if (curname == NULL ||
40740Sstevel@tonic-gate ((mp->mod_prim == 0) &&
40750Sstevel@tonic-gate (curname != filename) &&
40760Sstevel@tonic-gate (modname != filename) &&
40770Sstevel@tonic-gate (strcmp(curname, filename) != 0))) {
40780Sstevel@tonic-gate newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
40790Sstevel@tonic-gate (void) strcpy(newname, filename);
40800Sstevel@tonic-gate mp->mod_filename = newname;
40810Sstevel@tonic-gate if (curname != NULL)
40820Sstevel@tonic-gate kobj_free(curname, strlen(curname) + 1);
40830Sstevel@tonic-gate }
40840Sstevel@tonic-gate }
40850Sstevel@tonic-gate
40860Sstevel@tonic-gate mutex_exit(&mod_lock);
40870Sstevel@tonic-gate if (mp && moddebug & MODDEBUG_LOADMSG2)
40880Sstevel@tonic-gate printf("Holding %s\n", mp->mod_filename);
40890Sstevel@tonic-gate if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
40900Sstevel@tonic-gate printf("circular dependency loading %s\n", filename);
40910Sstevel@tonic-gate return (mp);
40920Sstevel@tonic-gate }
40930Sstevel@tonic-gate
40940Sstevel@tonic-gate static struct modctl *
mod_hold_by_name_requisite(struct modctl * dep,char * filename)40950Sstevel@tonic-gate mod_hold_by_name_requisite(struct modctl *dep, char *filename)
40960Sstevel@tonic-gate {
40970Sstevel@tonic-gate return (mod_hold_by_name_common(dep, filename));
40980Sstevel@tonic-gate }
40990Sstevel@tonic-gate
41000Sstevel@tonic-gate struct modctl *
mod_hold_by_name(const char * filename)41011414Scindi mod_hold_by_name(const char *filename)
41020Sstevel@tonic-gate {
41030Sstevel@tonic-gate return (mod_hold_by_name_common(NULL, filename));
41040Sstevel@tonic-gate }
41050Sstevel@tonic-gate
41061414Scindi struct modctl *
mod_hold_by_id(modid_t modid)41070Sstevel@tonic-gate mod_hold_by_id(modid_t modid)
41080Sstevel@tonic-gate {
41090Sstevel@tonic-gate struct modctl *mp;
41100Sstevel@tonic-gate int found = 0;
41110Sstevel@tonic-gate
41120Sstevel@tonic-gate mutex_enter(&mod_lock);
41130Sstevel@tonic-gate mp = &modules;
41140Sstevel@tonic-gate do {
41150Sstevel@tonic-gate if (mp->mod_id == modid) {
41160Sstevel@tonic-gate found = 1;
41170Sstevel@tonic-gate break;
41180Sstevel@tonic-gate }
41190Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules);
41200Sstevel@tonic-gate
41210Sstevel@tonic-gate if ((found == 0) || mod_circdep(mp))
41220Sstevel@tonic-gate mp = NULL;
41230Sstevel@tonic-gate else
41240Sstevel@tonic-gate (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
41250Sstevel@tonic-gate
41260Sstevel@tonic-gate mutex_exit(&mod_lock);
41270Sstevel@tonic-gate return (mp);
41280Sstevel@tonic-gate }
41290Sstevel@tonic-gate
41300Sstevel@tonic-gate static struct modctl *
mod_hold_next_by_id(modid_t modid)41310Sstevel@tonic-gate mod_hold_next_by_id(modid_t modid)
41320Sstevel@tonic-gate {
41330Sstevel@tonic-gate struct modctl *mp;
41340Sstevel@tonic-gate int found = 0;
41350Sstevel@tonic-gate
41360Sstevel@tonic-gate if (modid < -1)
41370Sstevel@tonic-gate return (NULL);
41380Sstevel@tonic-gate
41390Sstevel@tonic-gate mutex_enter(&mod_lock);
41400Sstevel@tonic-gate
41410Sstevel@tonic-gate mp = &modules;
41420Sstevel@tonic-gate do {
41430Sstevel@tonic-gate if (mp->mod_id > modid) {
41440Sstevel@tonic-gate found = 1;
41450Sstevel@tonic-gate break;
41460Sstevel@tonic-gate }
41470Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules);
41480Sstevel@tonic-gate
41490Sstevel@tonic-gate if ((found == 0) || mod_circdep(mp))
41500Sstevel@tonic-gate mp = NULL;
41510Sstevel@tonic-gate else
41520Sstevel@tonic-gate (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
41530Sstevel@tonic-gate
41540Sstevel@tonic-gate mutex_exit(&mod_lock);
41550Sstevel@tonic-gate return (mp);
41560Sstevel@tonic-gate }
41570Sstevel@tonic-gate
41580Sstevel@tonic-gate static void
mod_release(struct modctl * mp)41590Sstevel@tonic-gate mod_release(struct modctl *mp)
41600Sstevel@tonic-gate {
41610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock));
41620Sstevel@tonic-gate ASSERT(mp->mod_busy);
41630Sstevel@tonic-gate
41640Sstevel@tonic-gate mp->mod_busy = 0;
41650Sstevel@tonic-gate mp->mod_inprogress_thread = NULL;
41660Sstevel@tonic-gate if (mp->mod_want) {
41670Sstevel@tonic-gate mp->mod_want = 0;
41680Sstevel@tonic-gate cv_broadcast(&mod_cv);
41690Sstevel@tonic-gate }
41700Sstevel@tonic-gate }
41710Sstevel@tonic-gate
41720Sstevel@tonic-gate void
mod_release_mod(struct modctl * mp)41730Sstevel@tonic-gate mod_release_mod(struct modctl *mp)
41740Sstevel@tonic-gate {
41750Sstevel@tonic-gate if (moddebug & MODDEBUG_LOADMSG2)
41760Sstevel@tonic-gate printf("Releasing %s\n", mp->mod_filename);
41770Sstevel@tonic-gate mutex_enter(&mod_lock);
41780Sstevel@tonic-gate mod_release(mp);
41790Sstevel@tonic-gate mutex_exit(&mod_lock);
41800Sstevel@tonic-gate }
41810Sstevel@tonic-gate
41820Sstevel@tonic-gate modid_t
mod_name_to_modid(char * filename)41830Sstevel@tonic-gate mod_name_to_modid(char *filename)
41840Sstevel@tonic-gate {
41850Sstevel@tonic-gate char *modname;
41860Sstevel@tonic-gate struct modctl *mp;
41870Sstevel@tonic-gate
41880Sstevel@tonic-gate mutex_enter(&mod_lock);
41890Sstevel@tonic-gate
41900Sstevel@tonic-gate if ((modname = strrchr(filename, '/')) == NULL)
41910Sstevel@tonic-gate modname = filename;
41920Sstevel@tonic-gate else
41930Sstevel@tonic-gate modname++;
41940Sstevel@tonic-gate
41950Sstevel@tonic-gate mp = &modules;
41960Sstevel@tonic-gate do {
41970Sstevel@tonic-gate if (strcmp(modname, mp->mod_modname) == 0) {
41980Sstevel@tonic-gate mutex_exit(&mod_lock);
41990Sstevel@tonic-gate return (mp->mod_id);
42000Sstevel@tonic-gate }
42010Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules);
42020Sstevel@tonic-gate
42030Sstevel@tonic-gate mutex_exit(&mod_lock);
42040Sstevel@tonic-gate return (-1);
42050Sstevel@tonic-gate }
42060Sstevel@tonic-gate
42070Sstevel@tonic-gate
42080Sstevel@tonic-gate int
mod_remove_by_name(char * name)42090Sstevel@tonic-gate mod_remove_by_name(char *name)
42100Sstevel@tonic-gate {
42110Sstevel@tonic-gate struct modctl *mp;
42120Sstevel@tonic-gate int retval;
42130Sstevel@tonic-gate
42140Sstevel@tonic-gate mp = mod_hold_by_name(name);
42150Sstevel@tonic-gate
42160Sstevel@tonic-gate if (mp == NULL)
42170Sstevel@tonic-gate return (EINVAL);
42180Sstevel@tonic-gate
42190Sstevel@tonic-gate if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
42200Sstevel@tonic-gate /*
42210Sstevel@tonic-gate * Do not unload forceloaded modules
42220Sstevel@tonic-gate */
42230Sstevel@tonic-gate mod_release_mod(mp);
42240Sstevel@tonic-gate return (0);
42250Sstevel@tonic-gate }
42260Sstevel@tonic-gate
4227191Sahl if ((retval = moduninstall(mp)) == 0) {
42280Sstevel@tonic-gate mod_unload(mp);
4229191Sahl CPU_STATS_ADDQ(CPU, sys, modunload, 1);
4230191Sahl } else if (retval == EALREADY)
42310Sstevel@tonic-gate retval = 0; /* already unloaded, not an error */
42320Sstevel@tonic-gate mod_release_mod(mp);
42330Sstevel@tonic-gate return (retval);
42340Sstevel@tonic-gate }
42350Sstevel@tonic-gate
42360Sstevel@tonic-gate /*
42370Sstevel@tonic-gate * Record that module "dep" is dependent on module "on_mod."
42380Sstevel@tonic-gate */
42390Sstevel@tonic-gate static void
mod_make_requisite(struct modctl * dependent,struct modctl * on_mod)42400Sstevel@tonic-gate mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
42410Sstevel@tonic-gate {
42420Sstevel@tonic-gate struct modctl_list **pmlnp; /* previous next pointer */
42430Sstevel@tonic-gate struct modctl_list *mlp;
42440Sstevel@tonic-gate struct modctl_list *new;
42450Sstevel@tonic-gate
42460Sstevel@tonic-gate ASSERT(dependent->mod_busy && on_mod->mod_busy);
42470Sstevel@tonic-gate mutex_enter(&mod_lock);
42480Sstevel@tonic-gate
42490Sstevel@tonic-gate /*
42500Sstevel@tonic-gate * Search dependent's requisite list to see if on_mod is recorded.
42510Sstevel@tonic-gate * List is ordered by id.
42520Sstevel@tonic-gate */
42530Sstevel@tonic-gate for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
42540Sstevel@tonic-gate mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
42550Sstevel@tonic-gate if (mlp->modl_modp->mod_id >= on_mod->mod_id)
42560Sstevel@tonic-gate break;
42570Sstevel@tonic-gate
42580Sstevel@tonic-gate /* Create and insert if not already recorded */
42590Sstevel@tonic-gate if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
42600Sstevel@tonic-gate new = kobj_zalloc(sizeof (*new), KM_SLEEP);
42610Sstevel@tonic-gate new->modl_modp = on_mod;
42620Sstevel@tonic-gate new->modl_next = mlp;
42630Sstevel@tonic-gate *pmlnp = new;
42640Sstevel@tonic-gate
42650Sstevel@tonic-gate /*
42660Sstevel@tonic-gate * Increment the mod_ref count in our new requisite module.
42670Sstevel@tonic-gate * This is what keeps a module that has other modules
42680Sstevel@tonic-gate * which are dependent on it from being uninstalled and
42690Sstevel@tonic-gate * unloaded. "on_mod"'s mod_ref count decremented in
42700Sstevel@tonic-gate * mod_release_requisites when the "dependent" module
427110923SEvan.Yan@Sun.COM * unload is complete. "on_mod" must be loaded, but may not
42720Sstevel@tonic-gate * yet be installed.
42730Sstevel@tonic-gate */
42740Sstevel@tonic-gate on_mod->mod_ref++;
42750Sstevel@tonic-gate ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
42760Sstevel@tonic-gate }
42770Sstevel@tonic-gate
42780Sstevel@tonic-gate mutex_exit(&mod_lock);
42790Sstevel@tonic-gate }
42800Sstevel@tonic-gate
42810Sstevel@tonic-gate /*
42820Sstevel@tonic-gate * release the hold associated with mod_make_requisite mod_ref++
42830Sstevel@tonic-gate * as part of unload.
42840Sstevel@tonic-gate */
42850Sstevel@tonic-gate void
mod_release_requisites(struct modctl * modp)42860Sstevel@tonic-gate mod_release_requisites(struct modctl *modp)
42870Sstevel@tonic-gate {
42880Sstevel@tonic-gate struct modctl_list *modl;
42890Sstevel@tonic-gate struct modctl_list *next;
42900Sstevel@tonic-gate struct modctl *req;
42910Sstevel@tonic-gate struct modctl_list *start = NULL, *mod_garbage;
42920Sstevel@tonic-gate
429311084SJerry.Gilliam@Sun.COM ASSERT(!quiesce_active);
42940Sstevel@tonic-gate ASSERT(modp->mod_busy);
429511084SJerry.Gilliam@Sun.COM ASSERT(MUTEX_NOT_HELD(&mod_lock));
42960Sstevel@tonic-gate
42970Sstevel@tonic-gate mutex_enter(&mod_lock); /* needed for manipulation of req */
42980Sstevel@tonic-gate for (modl = modp->mod_requisites; modl; modl = next) {
42990Sstevel@tonic-gate next = modl->modl_next;
43000Sstevel@tonic-gate req = modl->modl_modp;
43010Sstevel@tonic-gate ASSERT(req->mod_ref >= 1 && req->mod_loaded);
43020Sstevel@tonic-gate req->mod_ref--;
4303*12207SChris.Horne@Sun.COM if (req->mod_ref == 0)
4304*12207SChris.Horne@Sun.COM mod_uninstall_ref_zero++;
43050Sstevel@tonic-gate
43060Sstevel@tonic-gate /*
43070Sstevel@tonic-gate * Check if the module has to be unloaded or not.
43080Sstevel@tonic-gate */
43090Sstevel@tonic-gate if (req->mod_ref == 0 && req->mod_delay_unload) {
43100Sstevel@tonic-gate struct modctl_list *new;
43110Sstevel@tonic-gate /*
43120Sstevel@tonic-gate * Allocate the modclt_list holding the garbage
43130Sstevel@tonic-gate * module which should be unloaded later.
43140Sstevel@tonic-gate */
43150Sstevel@tonic-gate new = kobj_zalloc(sizeof (struct modctl_list),
43160Sstevel@tonic-gate KM_SLEEP);
43170Sstevel@tonic-gate new->modl_modp = req;
43180Sstevel@tonic-gate
43190Sstevel@tonic-gate if (start == NULL)
43200Sstevel@tonic-gate mod_garbage = start = new;
43210Sstevel@tonic-gate else {
43220Sstevel@tonic-gate mod_garbage->modl_next = new;
43230Sstevel@tonic-gate mod_garbage = new;
43240Sstevel@tonic-gate }
43250Sstevel@tonic-gate }
43260Sstevel@tonic-gate
43270Sstevel@tonic-gate /* free the list as we go */
43280Sstevel@tonic-gate kobj_free(modl, sizeof (*modl));
43290Sstevel@tonic-gate }
43300Sstevel@tonic-gate modp->mod_requisites = NULL;
43310Sstevel@tonic-gate mutex_exit(&mod_lock);
43320Sstevel@tonic-gate
43330Sstevel@tonic-gate /*
43340Sstevel@tonic-gate * Unload the garbage modules.
43350Sstevel@tonic-gate */
43360Sstevel@tonic-gate for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
43370Sstevel@tonic-gate struct modctl_list *old = mod_garbage;
43380Sstevel@tonic-gate struct modctl *mp = mod_garbage->modl_modp;
43390Sstevel@tonic-gate ASSERT(mp != NULL);
43400Sstevel@tonic-gate
43410Sstevel@tonic-gate /*
43420Sstevel@tonic-gate * Hold this module until it's unloaded completely.
43430Sstevel@tonic-gate */
43440Sstevel@tonic-gate (void) mod_hold_by_modctl(mp,
43450Sstevel@tonic-gate MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
43460Sstevel@tonic-gate /*
43470Sstevel@tonic-gate * Check if the module is not unloaded yet and nobody requires
43480Sstevel@tonic-gate * the module. If it's unloaded already or somebody still
43490Sstevel@tonic-gate * requires the module, don't unload it now.
43500Sstevel@tonic-gate */
43510Sstevel@tonic-gate if (mp->mod_loaded && mp->mod_ref == 0)
43520Sstevel@tonic-gate mod_unload(mp);
43530Sstevel@tonic-gate ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
43540Sstevel@tonic-gate (mp->mod_ref > 0));
43550Sstevel@tonic-gate mod_release_mod(mp);
43560Sstevel@tonic-gate
43570Sstevel@tonic-gate mod_garbage = mod_garbage->modl_next;
43580Sstevel@tonic-gate kobj_free(old, sizeof (struct modctl_list));
43590Sstevel@tonic-gate }
43600Sstevel@tonic-gate }
43610Sstevel@tonic-gate
43620Sstevel@tonic-gate /*
43630Sstevel@tonic-gate * Process dependency of the module represented by "dep" on the
43640Sstevel@tonic-gate * module named by "on."
43650Sstevel@tonic-gate *
43660Sstevel@tonic-gate * Called from kobj_do_dependents() to load a module "on" on which
43670Sstevel@tonic-gate * "dep" depends.
43680Sstevel@tonic-gate */
43690Sstevel@tonic-gate struct modctl *
mod_load_requisite(struct modctl * dep,char * on)43700Sstevel@tonic-gate mod_load_requisite(struct modctl *dep, char *on)
43710Sstevel@tonic-gate {
43720Sstevel@tonic-gate struct modctl *on_mod;
43730Sstevel@tonic-gate int retval;
43740Sstevel@tonic-gate
43750Sstevel@tonic-gate if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
43760Sstevel@tonic-gate mod_make_requisite(dep, on_mod);
43770Sstevel@tonic-gate } else if (moddebug & MODDEBUG_ERRMSG) {
43780Sstevel@tonic-gate printf("error processing %s on which module %s depends\n",
43794851Scth on, dep->mod_modname);
43800Sstevel@tonic-gate }
43810Sstevel@tonic-gate return (on_mod);
43820Sstevel@tonic-gate }
43830Sstevel@tonic-gate
43840Sstevel@tonic-gate static int
mod_install_requisites(struct modctl * modp)43850Sstevel@tonic-gate mod_install_requisites(struct modctl *modp)
43860Sstevel@tonic-gate {
43870Sstevel@tonic-gate struct modctl_list *modl;
43880Sstevel@tonic-gate struct modctl *req;
43890Sstevel@tonic-gate int status = 0;
43900Sstevel@tonic-gate
43910Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&mod_lock));
43920Sstevel@tonic-gate ASSERT(modp->mod_busy);
43930Sstevel@tonic-gate
43940Sstevel@tonic-gate for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
43950Sstevel@tonic-gate req = modl->modl_modp;
43960Sstevel@tonic-gate (void) mod_hold_by_modctl(req,
43970Sstevel@tonic-gate MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
43980Sstevel@tonic-gate status = modinstall(req);
43990Sstevel@tonic-gate mod_release_mod(req);
44000Sstevel@tonic-gate
44010Sstevel@tonic-gate if (status != 0)
44020Sstevel@tonic-gate break;
44030Sstevel@tonic-gate }
44040Sstevel@tonic-gate return (status);
44050Sstevel@tonic-gate }
44060Sstevel@tonic-gate
44070Sstevel@tonic-gate /*
44080Sstevel@tonic-gate * returns 1 if this thread is doing autounload, 0 otherwise.
44090Sstevel@tonic-gate * see mod_uninstall_all.
44100Sstevel@tonic-gate */
44110Sstevel@tonic-gate int
mod_in_autounload()44120Sstevel@tonic-gate mod_in_autounload()
44130Sstevel@tonic-gate {
44140Sstevel@tonic-gate return ((int)(uintptr_t)tsd_get(mod_autounload_key));
44150Sstevel@tonic-gate }
44160Sstevel@tonic-gate
44170Sstevel@tonic-gate /*
44180Sstevel@tonic-gate * gmatch adapted from libc, stripping the wchar stuff
44190Sstevel@tonic-gate */
44204870Sjg #define popchar(p, c) { \
44214870Sjg c = *p++; \
44224870Sjg if (c == 0) { \
44234870Sjg return (0); \
44244870Sjg } \
44254870Sjg }
44260Sstevel@tonic-gate
44272621Sllai1 int
gmatch(const char * s,const char * p)44280Sstevel@tonic-gate gmatch(const char *s, const char *p)
44290Sstevel@tonic-gate {
44300Sstevel@tonic-gate int c, sc;
44310Sstevel@tonic-gate int ok, lc, notflag;
44320Sstevel@tonic-gate
44330Sstevel@tonic-gate sc = *s++;
44340Sstevel@tonic-gate c = *p++;
44350Sstevel@tonic-gate if (c == 0)
44360Sstevel@tonic-gate return (sc == c); /* nothing matches nothing */
44370Sstevel@tonic-gate
44380Sstevel@tonic-gate switch (c) {
44390Sstevel@tonic-gate case '\\':
44400Sstevel@tonic-gate /* skip to quoted character */
44414851Scth popchar(p, c);
44420Sstevel@tonic-gate /*FALLTHRU*/
44430Sstevel@tonic-gate
44440Sstevel@tonic-gate default:
44450Sstevel@tonic-gate /* straight comparison */
44460Sstevel@tonic-gate if (c != sc)
44470Sstevel@tonic-gate return (0);
44480Sstevel@tonic-gate /*FALLTHRU*/
44490Sstevel@tonic-gate
44500Sstevel@tonic-gate case '?':
44510Sstevel@tonic-gate /* first char matches, move to remainder */
44520Sstevel@tonic-gate return (sc != '\0' ? gmatch(s, p) : 0);
44530Sstevel@tonic-gate
44540Sstevel@tonic-gate
44550Sstevel@tonic-gate case '*':
44560Sstevel@tonic-gate while (*p == '*')
44570Sstevel@tonic-gate p++;
44580Sstevel@tonic-gate
44590Sstevel@tonic-gate /* * matches everything */
44600Sstevel@tonic-gate if (*p == 0)
44610Sstevel@tonic-gate return (1);
44620Sstevel@tonic-gate
44630Sstevel@tonic-gate /* undo skip at the beginning & iterate over substrings */
44640Sstevel@tonic-gate --s;
44650Sstevel@tonic-gate while (*s) {
44660Sstevel@tonic-gate if (gmatch(s, p))
44670Sstevel@tonic-gate return (1);
44680Sstevel@tonic-gate s++;
44690Sstevel@tonic-gate }
44700Sstevel@tonic-gate return (0);
44710Sstevel@tonic-gate
44720Sstevel@tonic-gate case '[':
44730Sstevel@tonic-gate /* match any char within [] */
44740Sstevel@tonic-gate if (sc == 0)
44750Sstevel@tonic-gate return (0);
44760Sstevel@tonic-gate
44770Sstevel@tonic-gate ok = lc = notflag = 0;
44780Sstevel@tonic-gate
44790Sstevel@tonic-gate if (*p == '!') {
44800Sstevel@tonic-gate notflag = 1;
44810Sstevel@tonic-gate p++;
44820Sstevel@tonic-gate }
44834851Scth popchar(p, c);
44840Sstevel@tonic-gate
44850Sstevel@tonic-gate do {
44860Sstevel@tonic-gate if (c == '-' && lc && *p != ']') {
44870Sstevel@tonic-gate /* test sc against range [c1-c2] */
44884851Scth popchar(p, c);
44890Sstevel@tonic-gate if (c == '\\') {
44904851Scth popchar(p, c);
44910Sstevel@tonic-gate }
44920Sstevel@tonic-gate
44930Sstevel@tonic-gate if (notflag) {
44940Sstevel@tonic-gate /* return 0 on mismatch */
44950Sstevel@tonic-gate if (lc <= sc && sc <= c)
44960Sstevel@tonic-gate return (0);
44970Sstevel@tonic-gate ok++;
44980Sstevel@tonic-gate } else if (lc <= sc && sc <= c) {
44990Sstevel@tonic-gate ok++;
45000Sstevel@tonic-gate }
45010Sstevel@tonic-gate /* keep going, may get a match next */
45020Sstevel@tonic-gate } else if (c == '\\') {
45030Sstevel@tonic-gate /* skip to quoted character */
45044851Scth popchar(p, c);
45050Sstevel@tonic-gate }
45060Sstevel@tonic-gate lc = c;
45070Sstevel@tonic-gate if (notflag) {
45080Sstevel@tonic-gate if (sc == lc)
45090Sstevel@tonic-gate return (0);
45100Sstevel@tonic-gate ok++;
45110Sstevel@tonic-gate } else if (sc == lc) {
45120Sstevel@tonic-gate ok++;
45130Sstevel@tonic-gate }
45144851Scth popchar(p, c);
45150Sstevel@tonic-gate } while (c != ']');
45160Sstevel@tonic-gate
45170Sstevel@tonic-gate /* recurse on remainder of string */
45180Sstevel@tonic-gate return (ok ? gmatch(s, p) : 0);
45190Sstevel@tonic-gate }
45200Sstevel@tonic-gate /*NOTREACHED*/
45210Sstevel@tonic-gate }
45220Sstevel@tonic-gate
45230Sstevel@tonic-gate
45240Sstevel@tonic-gate /*
45250Sstevel@tonic-gate * Get default perm for device from /etc/minor_perm. Return 0 if match found.
45260Sstevel@tonic-gate *
45270Sstevel@tonic-gate * Pure wild-carded patterns are handled separately so the ordering of
45280Sstevel@tonic-gate * these patterns doesn't matter. We're still dependent on ordering
45290Sstevel@tonic-gate * however as the first matching entry is the one returned.
45300Sstevel@tonic-gate * Not ideal but all existing examples and usage do imply this
45310Sstevel@tonic-gate * ordering implicitly.
45320Sstevel@tonic-gate *
45330Sstevel@tonic-gate * Drivers using the clone driver are always good for some entertainment.
45340Sstevel@tonic-gate * Clone nodes under pseudo have the form clone@0:<driver>. Some minor
45350Sstevel@tonic-gate * perm entries have the form clone:<driver>, others use <driver>:*
45360Sstevel@tonic-gate * Examples are clone:llc1 vs. llc2:*, for example.
45370Sstevel@tonic-gate *
45380Sstevel@tonic-gate * Minor perms in the clone:<driver> form are mapped to the drivers's
45390Sstevel@tonic-gate * mperm list, not the clone driver, as wildcard entries for clone
45400Sstevel@tonic-gate * reference only. In other words, a clone wildcard will match
45410Sstevel@tonic-gate * references for clone@0:<driver> but never <driver>@<minor>.
45420Sstevel@tonic-gate *
45430Sstevel@tonic-gate * Additional minor perms in the standard form are also supported,
45440Sstevel@tonic-gate * for mixed usage, ie a node with an entry clone:<driver> could
45450Sstevel@tonic-gate * provide further entries <driver>:<minor>.
45460Sstevel@tonic-gate *
45470Sstevel@tonic-gate * Finally, some uses of clone use an alias as the minor name rather
45480Sstevel@tonic-gate * than the driver name, with the alias as the minor perm entry.
45490Sstevel@tonic-gate * This case is handled by attaching the driver to bring its
45500Sstevel@tonic-gate * minor list into existence, then discover the alias via DDI_ALIAS.
45510Sstevel@tonic-gate * The clone device's minor perm list can then be searched for
45520Sstevel@tonic-gate * that alias.
45530Sstevel@tonic-gate */
45540Sstevel@tonic-gate
45550Sstevel@tonic-gate static int
dev_alias_minorperm(dev_info_t * dip,char * minor_name,mperm_t * rmp)45560Sstevel@tonic-gate dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
45570Sstevel@tonic-gate {
45587224Scth major_t major;
45597224Scth struct devnames *dnp;
45607224Scth mperm_t *mp;
45617224Scth char *alias = NULL;
45627224Scth dev_info_t *cdevi;
45637224Scth int circ;
45647224Scth struct ddi_minor_data *dmd;
45650Sstevel@tonic-gate
45660Sstevel@tonic-gate major = ddi_name_to_major(minor_name);
45670Sstevel@tonic-gate
45680Sstevel@tonic-gate ASSERT(dip == clone_dip);
45697009Scth ASSERT(major != DDI_MAJOR_T_NONE);
45700Sstevel@tonic-gate
45710Sstevel@tonic-gate /*
45720Sstevel@tonic-gate * Attach the driver named by the minor node, then
45730Sstevel@tonic-gate * search its first instance's minor list for an
45740Sstevel@tonic-gate * alias node.
45750Sstevel@tonic-gate */
45760Sstevel@tonic-gate if (ddi_hold_installed_driver(major) == NULL)
45770Sstevel@tonic-gate return (1);
45780Sstevel@tonic-gate
45790Sstevel@tonic-gate dnp = &devnamesp[major];
45800Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
45810Sstevel@tonic-gate
45820Sstevel@tonic-gate if ((cdevi = dnp->dn_head) != NULL) {
45837224Scth ndi_devi_enter(cdevi, &circ);
45840Sstevel@tonic-gate for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
45850Sstevel@tonic-gate if (dmd->type == DDM_ALIAS) {
45860Sstevel@tonic-gate alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
45870Sstevel@tonic-gate break;
45880Sstevel@tonic-gate }
45890Sstevel@tonic-gate }
45907224Scth ndi_devi_exit(cdevi, circ);
45910Sstevel@tonic-gate }
45920Sstevel@tonic-gate
45930Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
45940Sstevel@tonic-gate ddi_rele_driver(major);
45950Sstevel@tonic-gate
45960Sstevel@tonic-gate if (alias == NULL) {
45970Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM)
45980Sstevel@tonic-gate cmn_err(CE_CONT, "dev_minorperm: "
45990Sstevel@tonic-gate "no alias for %s\n", minor_name);
46000Sstevel@tonic-gate return (1);
46010Sstevel@tonic-gate }
46020Sstevel@tonic-gate
46030Sstevel@tonic-gate major = ddi_driver_major(clone_dip);
46040Sstevel@tonic-gate dnp = &devnamesp[major];
46050Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
46060Sstevel@tonic-gate
46070Sstevel@tonic-gate /*
46080Sstevel@tonic-gate * Go through the clone driver's mperm list looking
46090Sstevel@tonic-gate * for a match for the specified alias.
46100Sstevel@tonic-gate */
46110Sstevel@tonic-gate for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
46120Sstevel@tonic-gate if (strcmp(alias, mp->mp_minorname) == 0) {
46130Sstevel@tonic-gate break;
46140Sstevel@tonic-gate }
46150Sstevel@tonic-gate }
46160Sstevel@tonic-gate
46170Sstevel@tonic-gate if (mp) {
46180Sstevel@tonic-gate if (moddebug & MODDEBUG_MP_MATCH) {
46190Sstevel@tonic-gate cmn_err(CE_CONT,
46200Sstevel@tonic-gate "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
46210Sstevel@tonic-gate minor_name, alias, mp->mp_mode,
46220Sstevel@tonic-gate mp->mp_uid, mp->mp_gid);
46230Sstevel@tonic-gate }
46240Sstevel@tonic-gate rmp->mp_uid = mp->mp_uid;
46250Sstevel@tonic-gate rmp->mp_gid = mp->mp_gid;
46260Sstevel@tonic-gate rmp->mp_mode = mp->mp_mode;
46270Sstevel@tonic-gate }
46280Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
46290Sstevel@tonic-gate
46300Sstevel@tonic-gate kmem_free(alias, strlen(alias)+1);
46310Sstevel@tonic-gate
46320Sstevel@tonic-gate return (mp == NULL);
46330Sstevel@tonic-gate }
46340Sstevel@tonic-gate
46350Sstevel@tonic-gate int
dev_minorperm(dev_info_t * dip,char * name,mperm_t * rmp)46360Sstevel@tonic-gate dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
46370Sstevel@tonic-gate {
46380Sstevel@tonic-gate major_t major;
46390Sstevel@tonic-gate char *minor_name;
46400Sstevel@tonic-gate struct devnames *dnp;
46410Sstevel@tonic-gate mperm_t *mp;
46420Sstevel@tonic-gate int is_clone = 0;
46430Sstevel@tonic-gate
46440Sstevel@tonic-gate if (!minorperm_loaded) {
46450Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM)
46460Sstevel@tonic-gate cmn_err(CE_CONT,
46470Sstevel@tonic-gate "%s: minor perm not yet loaded\n", name);
46480Sstevel@tonic-gate return (1);
46490Sstevel@tonic-gate }
46500Sstevel@tonic-gate
46510Sstevel@tonic-gate minor_name = strchr(name, ':');
46520Sstevel@tonic-gate if (minor_name == NULL)
46530Sstevel@tonic-gate return (1);
46540Sstevel@tonic-gate minor_name++;
46550Sstevel@tonic-gate
46560Sstevel@tonic-gate /*
46570Sstevel@tonic-gate * If it's the clone driver, search the driver as named
46580Sstevel@tonic-gate * by the minor. All clone minor perm entries other than
46590Sstevel@tonic-gate * alias nodes are actually installed on the real driver's list.
46600Sstevel@tonic-gate */
46610Sstevel@tonic-gate if (dip == clone_dip) {
46620Sstevel@tonic-gate major = ddi_name_to_major(minor_name);
46637009Scth if (major == DDI_MAJOR_T_NONE) {
46640Sstevel@tonic-gate if (moddebug & MODDEBUG_MINORPERM)
46650Sstevel@tonic-gate cmn_err(CE_CONT, "dev_minorperm: "
46660Sstevel@tonic-gate "%s: no such driver\n", minor_name);
46670Sstevel@tonic-gate return (1);
46680Sstevel@tonic-gate }
46690Sstevel@tonic-gate is_clone = 1;
46700Sstevel@tonic-gate } else {
46710Sstevel@tonic-gate major = ddi_driver_major(dip);
46727009Scth ASSERT(major != DDI_MAJOR_T_NONE);
46730Sstevel@tonic-gate }
46740Sstevel@tonic-gate
46750Sstevel@tonic-gate dnp = &devnamesp[major];
46760Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock);
46770Sstevel@tonic-gate
46780Sstevel@tonic-gate /*
46790Sstevel@tonic-gate * Go through the driver's mperm list looking for
46800Sstevel@tonic-gate * a match for the specified minor. If there's
46810Sstevel@tonic-gate * no matching pattern, use the wild card.
46820Sstevel@tonic-gate * Defer to the clone wild for clone if specified,
46830Sstevel@tonic-gate * otherwise fall back to the normal form.
46840Sstevel@tonic-gate */
46850Sstevel@tonic-gate for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
46860Sstevel@tonic-gate if (gmatch(minor_name, mp->mp_minorname) != 0) {
46870Sstevel@tonic-gate break;
46880Sstevel@tonic-gate }
46890Sstevel@tonic-gate }
46900Sstevel@tonic-gate if (mp == NULL) {
46910Sstevel@tonic-gate if (is_clone)
46920Sstevel@tonic-gate mp = dnp->dn_mperm_clone;
46930Sstevel@tonic-gate if (mp == NULL)
46940Sstevel@tonic-gate mp = dnp->dn_mperm_wild;
46950Sstevel@tonic-gate }
46960Sstevel@tonic-gate
46970Sstevel@tonic-gate if (mp) {
46980Sstevel@tonic-gate if (moddebug & MODDEBUG_MP_MATCH) {
46990Sstevel@tonic-gate cmn_err(CE_CONT,
47000Sstevel@tonic-gate "minor perm defaults: %s %s 0%o %d %d\n",
47010Sstevel@tonic-gate name, mp->mp_minorname, mp->mp_mode,
47020Sstevel@tonic-gate mp->mp_uid, mp->mp_gid);
47030Sstevel@tonic-gate }
47040Sstevel@tonic-gate rmp->mp_uid = mp->mp_uid;
47050Sstevel@tonic-gate rmp->mp_gid = mp->mp_gid;
47060Sstevel@tonic-gate rmp->mp_mode = mp->mp_mode;
47070Sstevel@tonic-gate }
47080Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock);
47090Sstevel@tonic-gate
47100Sstevel@tonic-gate /*
47110Sstevel@tonic-gate * If no match can be found for a clone node,
47120Sstevel@tonic-gate * search for a possible match for an alias.
47130Sstevel@tonic-gate * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
47140Sstevel@tonic-gate * with minor perm entry clone:ptmx.
47150Sstevel@tonic-gate */
47160Sstevel@tonic-gate if (mp == NULL && is_clone) {
47170Sstevel@tonic-gate return (dev_alias_minorperm(dip, minor_name, rmp));
47180Sstevel@tonic-gate }
47190Sstevel@tonic-gate
47200Sstevel@tonic-gate return (mp == NULL);
47210Sstevel@tonic-gate }
47220Sstevel@tonic-gate
47230Sstevel@tonic-gate /*
47240Sstevel@tonic-gate * dynamicaly reference load a dl module/library, returning handle
47250Sstevel@tonic-gate */
47260Sstevel@tonic-gate /*ARGSUSED*/
47270Sstevel@tonic-gate ddi_modhandle_t
ddi_modopen(const char * modname,int mode,int * errnop)47280Sstevel@tonic-gate ddi_modopen(const char *modname, int mode, int *errnop)
47290Sstevel@tonic-gate {
47300Sstevel@tonic-gate char *subdir;
47310Sstevel@tonic-gate char *mod;
47320Sstevel@tonic-gate int subdirlen;
47330Sstevel@tonic-gate struct modctl *hmodp = NULL;
47340Sstevel@tonic-gate int retval = EINVAL;
47350Sstevel@tonic-gate
47360Sstevel@tonic-gate ASSERT(modname && (mode == KRTLD_MODE_FIRST));
47370Sstevel@tonic-gate if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
47380Sstevel@tonic-gate goto out;
47390Sstevel@tonic-gate
47404851Scth /* find last '/' in modname */
47414851Scth mod = strrchr(modname, '/');
47420Sstevel@tonic-gate
47430Sstevel@tonic-gate if (mod) {
47440Sstevel@tonic-gate /* for subdir string without modification to argument */
47450Sstevel@tonic-gate mod++;
47460Sstevel@tonic-gate subdirlen = mod - modname;
47470Sstevel@tonic-gate subdir = kmem_alloc(subdirlen, KM_SLEEP);
47480Sstevel@tonic-gate (void) strlcpy(subdir, modname, subdirlen);
47490Sstevel@tonic-gate } else {
47500Sstevel@tonic-gate subdirlen = 0;
47510Sstevel@tonic-gate subdir = "misc";
47520Sstevel@tonic-gate mod = (char *)modname;
47530Sstevel@tonic-gate }
47540Sstevel@tonic-gate
47550Sstevel@tonic-gate /* reference load with errno return value */
47560Sstevel@tonic-gate retval = modrload(subdir, mod, &hmodp);
47570Sstevel@tonic-gate
47580Sstevel@tonic-gate if (subdirlen)
47590Sstevel@tonic-gate kmem_free(subdir, subdirlen);
47600Sstevel@tonic-gate
47610Sstevel@tonic-gate out: if (errnop)
47620Sstevel@tonic-gate *errnop = retval;
47630Sstevel@tonic-gate
47640Sstevel@tonic-gate if (moddebug & MODDEBUG_DDI_MOD)
47650Sstevel@tonic-gate printf("ddi_modopen %s mode %x: %s %p %d\n",
47660Sstevel@tonic-gate modname ? modname : "<unknown>", mode,
47670Sstevel@tonic-gate hmodp ? hmodp->mod_filename : "<unknown>",
47680Sstevel@tonic-gate (void *)hmodp, retval);
47690Sstevel@tonic-gate
47700Sstevel@tonic-gate return ((ddi_modhandle_t)hmodp);
47710Sstevel@tonic-gate }
47720Sstevel@tonic-gate
47730Sstevel@tonic-gate /* lookup "name" in open dl module/library */
47740Sstevel@tonic-gate void *
ddi_modsym(ddi_modhandle_t h,const char * name,int * errnop)47750Sstevel@tonic-gate ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
47760Sstevel@tonic-gate {
47770Sstevel@tonic-gate struct modctl *hmodp = (struct modctl *)h;
47780Sstevel@tonic-gate void *f;
47790Sstevel@tonic-gate int retval;
47800Sstevel@tonic-gate
47810Sstevel@tonic-gate ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
47820Sstevel@tonic-gate if ((hmodp == NULL) || (name == NULL) ||
47830Sstevel@tonic-gate (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
47840Sstevel@tonic-gate f = NULL;
47850Sstevel@tonic-gate retval = EINVAL;
47860Sstevel@tonic-gate } else {
47870Sstevel@tonic-gate f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
47880Sstevel@tonic-gate if (f)
47890Sstevel@tonic-gate retval = 0;
47900Sstevel@tonic-gate else
47910Sstevel@tonic-gate retval = ENOTSUP;
47920Sstevel@tonic-gate }
47930Sstevel@tonic-gate
47940Sstevel@tonic-gate if (moddebug & MODDEBUG_DDI_MOD)
47950Sstevel@tonic-gate printf("ddi_modsym in %s of %s: %d %p\n",
47960Sstevel@tonic-gate hmodp ? hmodp->mod_modname : "<unknown>",
47970Sstevel@tonic-gate name ? name : "<unknown>", retval, f);
47980Sstevel@tonic-gate
47990Sstevel@tonic-gate if (errnop)
48000Sstevel@tonic-gate *errnop = retval;
48010Sstevel@tonic-gate return (f);
48020Sstevel@tonic-gate }
48030Sstevel@tonic-gate
48040Sstevel@tonic-gate /* dynamic (un)reference unload of an open dl module/library */
48050Sstevel@tonic-gate int
ddi_modclose(ddi_modhandle_t h)48060Sstevel@tonic-gate ddi_modclose(ddi_modhandle_t h)
48070Sstevel@tonic-gate {
48080Sstevel@tonic-gate struct modctl *hmodp = (struct modctl *)h;
48090Sstevel@tonic-gate struct modctl *modp = NULL;
48100Sstevel@tonic-gate int retval;
48110Sstevel@tonic-gate
48120Sstevel@tonic-gate ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
48130Sstevel@tonic-gate if ((hmodp == NULL) ||
48140Sstevel@tonic-gate (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
48150Sstevel@tonic-gate retval = EINVAL;
48160Sstevel@tonic-gate goto out;
48170Sstevel@tonic-gate }
48180Sstevel@tonic-gate
48190Sstevel@tonic-gate retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
48200Sstevel@tonic-gate if (retval == EBUSY)
48210Sstevel@tonic-gate retval = 0; /* EBUSY is not an error */
48220Sstevel@tonic-gate
48230Sstevel@tonic-gate if (retval == 0) {
48240Sstevel@tonic-gate ASSERT(hmodp == modp);
48250Sstevel@tonic-gate if (hmodp != modp)
48260Sstevel@tonic-gate retval = EINVAL;
48270Sstevel@tonic-gate }
48280Sstevel@tonic-gate
48290Sstevel@tonic-gate out: if (moddebug & MODDEBUG_DDI_MOD)
48300Sstevel@tonic-gate printf("ddi_modclose %s: %d\n",
48310Sstevel@tonic-gate hmodp ? hmodp->mod_modname : "<unknown>", retval);
48320Sstevel@tonic-gate
48330Sstevel@tonic-gate return (retval);
48340Sstevel@tonic-gate }
4835