4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
27 * modctl system call for loadable module support.
30 #include <sys/param.h>
32 #include <sys/systm.h>
38 #include <sys/reboot.h>
39 #include <sys/fs/ufs_fsdir.h>
41 #include <sys/sysconf.h>
42 #include <sys/cmn_err.h>
44 #include <sys/sunddi.h>
45 #include <sys/sunndi.h>
46 #include <sys/ndi_impldefs.h>
47 #include <sys/ddi_impldefs.h>
48 #include <sys/ddi_implfuncs.h>
49 #include <sys/bootconf.h>
50 #include <sys/dc_ki.h>
51 #include <sys/cladm.h>
52 #include <sys/dtrace.h>
55 #include <sys/devpolicy.h>
56 #include <sys/modctl.h>
58 #include <sys/devops.h>
59 #include <sys/autoconf.h>
60 #include <sys/hwconf.h>
61 #include <sys/callb.h>
62 #include <sys/debug.h>
63 #include <sys/cpuvar.h>
64 #include <sys/sysmacros.h>
65 #include <sys/sysevent.h>
66 #include <sys/sysevent_impl.h>
67 #include <sys/instance.h>
68 #include <sys/modhash.h>
69 #include <sys/modhash_impl.h>
70 #include <sys/dacf_impl.h>
72 #include <sys/pathname.h>
73 #include <sys/console.h>
74 #include <sys/policy.h>
75 #include <ipp/ipp_impl.h>
76 #include <sys/fs/dv_node.h>
77 #include <sys/strsubr.h>
78 #include <sys/fs/sdev_impl.h>
80 static int mod_circdep(struct modctl
*);
81 static int modinfo(modid_t
, struct modinfo
*);
83 static void mod_uninstall_all(void);
84 static int mod_getinfo(struct modctl
*, struct modinfo
*);
85 static struct modctl
*allocate_modp(const char *, const char *);
87 static int mod_load(struct modctl
*, int);
88 static void mod_unload(struct modctl
*);
89 static int modinstall(struct modctl
*);
90 static int moduninstall(struct modctl
*);
92 static struct modctl
*mod_hold_by_name_common(struct modctl
*, const char *);
93 static struct modctl
*mod_hold_next_by_id(modid_t
);
94 static struct modctl
*mod_hold_loaded_mod(struct modctl
*, char *, int *);
95 static struct modctl
*mod_hold_installed_mod(char *, int, int, int *);
97 static void mod_release(struct modctl
*);
98 static void mod_make_requisite(struct modctl
*, struct modctl
*);
99 static int mod_install_requisites(struct modctl
*);
100 static void check_esc_sequences(char *, char *);
101 static struct modctl
*mod_hold_by_name_requisite(struct modctl
*, char *);
104 * module loading thread control structure. Calls to kobj_load_module()() are
105 * handled off to a separate thead using this structure.
115 static void modload_thread(struct loadmt
*);
118 kcondvar_t mod_uninstall_cv
; /* Communication between swapper */
119 /* and the uninstall daemon. */
120 kmutex_t mod_lock
; /* protects &modules insert linkage, */
121 /* mod_busy, mod_want, and mod_ref. */
122 /* blocking operations while holding */
123 /* mod_lock should be avoided */
124 kmutex_t mod_uninstall_lock
; /* protects mod_uninstall_cv */
125 kthread_id_t mod_aul_thread
;
128 kmutex_t modunload_wait_mutex
;
129 kcondvar_t modunload_wait_cv
;
130 int modunload_active_count
;
131 int modunload_disable_count
;
133 int isminiroot
; /* set if running as miniroot */
134 int modrootloaded
; /* set after root driver and fs are loaded */
135 int moddebug
= 0x0; /* debug flags for module writers */
136 int swaploaded
; /* set after swap driver and fs are loaded */
137 int bop_io_quiesced
= 0; /* set when BOP I/O can no longer be used */
139 clock_t mod_uninstall_interval
= 0;
140 int mod_uninstall_pass_max
= 6;
141 int mod_uninstall_ref_zero
; /* # modules that went mod_ref == 0 */
142 int mod_uninstall_pass_exc
; /* mod_uninstall_all left new stuff */
144 int ddi_modclose_unload
= 1; /* 0 -> just decrement reference */
146 int devcnt_incr
= 256; /* allow for additional drivers */
147 int devcnt_min
= 512; /* and always at least this number */
149 struct devnames
*devnamesp
;
150 struct devnames orphanlist
;
152 krwlock_t devinfo_tree_lock
; /* obsolete, to be removed */
154 #define MAJBINDFILE "/etc/name_to_major"
155 #define SYSBINDFILE "/etc/name_to_sysnum"
157 static char majbind
[] = MAJBINDFILE
;
158 static char sysbind
[] = SYSBINDFILE
;
159 static uint_t mod_autounload_key
; /* for module autounload detection */
163 #define DEBUGGER_PRESENT ((boothowto & RB_DEBUG) || (obpdebug != 0))
165 static int minorperm_loaded
= 0;
170 struct sysent
*callp
;
171 int callnum
, exectype
;
176 * Initialize the list of loaded driver dev_ops.
177 * XXX - This must be done before reading the system file so that
178 * forceloads of drivers will work.
180 num_devs
= read_binding_file(majbind
, mb_hashtab
, make_mbind
);
182 * Since read_binding_file is common code, it doesn't enforce that all
183 * of the binding file entries have major numbers <= MAXMAJ32. Thus,
184 * ensure that we don't allocate some massive amount of space due to a
185 * bad entry. We can't have major numbers bigger than MAXMAJ32
186 * until file system support for larger major numbers exists.
190 * Leave space for expansion, but not more than L_MAXMAJ32
192 devcnt
= MIN(num_devs
+ devcnt_incr
, L_MAXMAJ32
);
193 devcnt
= MAX(devcnt
, devcnt_min
);
194 devopsp
= kmem_alloc(devcnt
* sizeof (struct dev_ops
*), KM_SLEEP
);
195 for (i
= 0; i
< devcnt
; i
++)
196 devopsp
[i
] = &mod_nodev_ops
;
198 init_devnamesp(devcnt
);
201 * Sync up with the work that the stand-alone linker has already done.
205 if (boothowto
& RB_DEBUG
)
208 make_aliases(mb_hashtab
);
211 * Initialize streams device implementation structures.
213 devimpl
= kmem_zalloc(devcnt
* sizeof (cdevsw_impl_t
), KM_SLEEP
);
216 * If the cl_bootstrap module is present,
217 * we should be configured as a cluster. Loading this module
218 * will set "cluster_bootflags" to non-zero.
220 (void) modload("misc", "cl_bootstrap");
222 (void) read_binding_file(sysbind
, sb_hashtab
, make_mbind
);
223 init_syscallnames(NSYSCALL
);
226 * Start up dynamic autoconfiguration framework (dacf).
232 * Start up IP policy framework (ipp).
237 * Allocate loadable native system call locks.
239 for (callnum
= 0, callp
= sysent
; callnum
< NSYSCALL
;
240 callnum
++, callp
++) {
241 if (LOADABLE_SYSCALL(callp
)) {
242 if (mod_getsysname(callnum
) != NULL
) {
244 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
245 rw_init(callp
->sy_lock
, NULL
, RW_DEFAULT
, NULL
);
247 callp
->sy_flags
&= ~SE_LOADABLE
;
248 callp
->sy_callc
= nosys
;
253 * Do some sanity checks on the sysent table
255 switch (callp
->sy_flags
& SE_RVAL_MASK
) {
257 /* only r_val1 returned */
258 case SE_32RVAL1
| SE_32RVAL2
:
259 /* r_val1 and r_val2 returned */
261 /* 64-bit rval returned */
264 cmn_err(CE_WARN
, "sysent[%d]: bad flags %x",
265 callnum
, callp
->sy_flags
);
271 #ifdef _SYSCALL32_IMPL
273 * Allocate loadable system call locks for 32-bit compat syscalls
275 for (callnum
= 0, callp
= sysent32
; callnum
< NSYSCALL
;
276 callnum
++, callp
++) {
277 if (LOADABLE_SYSCALL(callp
)) {
278 if (mod_getsysname(callnum
) != NULL
) {
280 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
281 rw_init(callp
->sy_lock
, NULL
, RW_DEFAULT
, NULL
);
283 callp
->sy_flags
&= ~SE_LOADABLE
;
284 callp
->sy_callc
= nosys
;
289 * Do some sanity checks on the sysent table
291 switch (callp
->sy_flags
& SE_RVAL_MASK
) {
293 /* only r_val1 returned */
294 case SE_32RVAL1
| SE_32RVAL2
:
295 /* r_val1 and r_val2 returned */
297 /* 64-bit rval returned */
300 cmn_err(CE_WARN
, "sysent32[%d]: bad flags %x",
301 callnum
, callp
->sy_flags
);
306 * Cross-check the native and compatibility tables.
308 if (callp
->sy_callc
== nosys
||
309 sysent
[callnum
].sy_callc
== nosys
)
312 * If only one or the other slot is loadable, then
313 * there's an error -- they should match!
315 if ((callp
->sy_callc
== loadable_syscall
) ^
316 (sysent
[callnum
].sy_callc
== loadable_syscall
)) {
317 cmn_err(CE_WARN
, "sysent[%d] loadable?",
321 * This is more of a heuristic test -- if the
322 * system call returns two values in the 32-bit
323 * world, it should probably return two 32-bit
324 * values in the 64-bit world too.
326 if (((callp
->sy_flags
& SE_32RVAL2
) == 0) ^
327 ((sysent
[callnum
].sy_flags
& SE_32RVAL2
) == 0)) {
328 cmn_err(CE_WARN
, "sysent[%d] rval2 mismatch!",
335 #endif /* _SYSCALL32_IMPL */
338 * Allocate loadable exec locks. (Assumes all execs are loadable)
340 for (exectype
= 0; exectype
< nexectype
; exectype
++) {
341 execsw
[exectype
].exec_lock
=
342 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
343 rw_init(execsw
[exectype
].exec_lock
, NULL
, RW_DEFAULT
, NULL
);
348 /* init thread specific structure for mod_uninstall_all */
349 tsd_create(&mod_autounload_key
, NULL
);
353 modctl_modload(int use_path
, char *filename
, int *rvp
)
360 filenamep
= kmem_zalloc(MOD_MAXPATH
, KM_SLEEP
);
362 if (copyinstr(filename
, filenamep
, MOD_MAXPATH
, 0)) {
367 filenamep
[MOD_MAXPATH
- 1] = 0;
368 modp
= mod_hold_installed_mod(filenamep
, use_path
, 0, &retval
);
373 modp
->mod_loadflags
|= MOD_NOAUTOUNLOAD
;
374 modid
= modp
->mod_id
;
375 mod_release_mod(modp
);
376 CPU_STATS_ADDQ(CPU
, sys
, modload
, 1);
377 if (rvp
!= NULL
&& copyout(&modid
, rvp
, sizeof (modid
)) != 0)
380 kmem_free(filenamep
, MOD_MAXPATH
);
386 modctl_modunload(modid_t id
)
393 * Turn on mod_uninstall_daemon
395 if (mod_uninstall_interval
== 0) {
396 mod_uninstall_interval
= 60;
403 rval
= modunload(id
);
409 modctl_modinfo(modid_t id
, struct modinfo
*umodi
)
413 #if defined(_SYSCALL32_IMPL)
415 struct modinfo32 modi32
;
418 if (get_udatamodel() == DATAMODEL_NATIVE
) {
419 if (copyin(umodi
, &modi
, sizeof (struct modinfo
)) != 0)
422 #ifdef _SYSCALL32_IMPL
424 bzero(&modi
, sizeof (modi
));
425 if (copyin(umodi
, &modi32
, sizeof (struct modinfo32
)) != 0)
427 modi
.mi_info
= modi32
.mi_info
;
428 modi
.mi_id
= modi32
.mi_id
;
429 modi
.mi_nextid
= modi32
.mi_nextid
;
430 nobase
= modi
.mi_info
& MI_INFO_NOBASE
;
434 * This flag is -only- for the kernels use.
436 modi
.mi_info
&= ~MI_INFO_LINKAGE
;
438 retval
= modinfo(id
, &modi
);
442 if (get_udatamodel() == DATAMODEL_NATIVE
) {
443 if (copyout(&modi
, umodi
, sizeof (struct modinfo
)) != 0)
445 #ifdef _SYSCALL32_IMPL
449 if (!nobase
&& (uintptr_t)modi
.mi_base
> UINT32_MAX
)
452 modi32
.mi_info
= modi
.mi_info
;
453 modi32
.mi_state
= modi
.mi_state
;
454 modi32
.mi_id
= modi
.mi_id
;
455 modi32
.mi_nextid
= modi
.mi_nextid
;
456 modi32
.mi_base
= (caddr32_t
)(uintptr_t)modi
.mi_base
;
457 modi32
.mi_size
= modi
.mi_size
;
458 modi32
.mi_rev
= modi
.mi_rev
;
459 modi32
.mi_loadcnt
= modi
.mi_loadcnt
;
460 bcopy(modi
.mi_name
, modi32
.mi_name
, sizeof (modi32
.mi_name
));
461 for (i
= 0; i
< MODMAXLINK32
; i
++) {
462 modi32
.mi_msinfo
[i
].msi_p0
= modi
.mi_msinfo
[i
].msi_p0
;
463 bcopy(modi
.mi_msinfo
[i
].msi_linkinfo
,
464 modi32
.mi_msinfo
[i
].msi_linkinfo
,
465 sizeof (modi32
.mi_msinfo
[0].msi_linkinfo
));
467 if (copyout(&modi32
, umodi
, sizeof (struct modinfo32
)) != 0)
476 * Return the last major number in the range of permissible major numbers.
480 modctl_modreserve(modid_t id
, int *data
)
482 if (copyout(&devcnt
, data
, sizeof (devcnt
)) != 0)
487 /* Add/Remove driver and binding aliases */
489 modctl_update_driver_aliases(int add
, int *data
)
493 struct aliases alias
;
495 char name
[MAXMODCONFNAME
];
496 char cname
[MAXMODCONFNAME
];
504 bzero(&mc
, sizeof (struct modconfig
));
505 if (get_udatamodel() == DATAMODEL_NATIVE
) {
506 if (copyin(data
, &mc
, sizeof (struct modconfig
)) != 0)
509 #ifdef _SYSCALL32_IMPL
511 struct modconfig32 modc32
;
512 if (copyin(data
, &modc32
, sizeof (struct modconfig32
)) != 0)
515 bcopy(modc32
.drvname
, mc
.drvname
,
516 sizeof (modc32
.drvname
));
517 bcopy(modc32
.drvclass
, mc
.drvclass
,
518 sizeof (modc32
.drvclass
));
519 mc
.major
= modc32
.major
;
520 mc
.flags
= modc32
.flags
;
521 mc
.num_aliases
= modc32
.num_aliases
;
522 mc
.ap
= (struct aliases
*)(uintptr_t)modc32
.ap
;
528 * If the driver is already in the mb_hashtab, and the name given
529 * doesn't match that driver's name, fail. Otherwise, pass, since
530 * we may be adding aliases.
532 drvname
= mod_major_to_name(mc
.major
);
533 if ((drvname
!= NULL
) && strcmp(drvname
, mc
.drvname
) != 0)
537 * Precede alias removal by unbinding as many devices as possible.
540 (void) i_ddi_unload_drvconf(mc
.major
);
541 i_ddi_unbind_devs(mc
.major
);
545 * Add/remove each supplied driver alias to/from mb_hashtab
548 if (mc
.num_aliases
> 0)
549 aliases
= kmem_zalloc(
550 mc
.num_aliases
* sizeof (struct alias_info
), KM_SLEEP
);
552 for (i
= 0; i
< mc
.num_aliases
; i
++) {
553 bzero(&alias
, sizeof (struct aliases
));
554 if (get_udatamodel() == DATAMODEL_NATIVE
) {
555 if (copyin(ap
, &alias
, sizeof (struct aliases
)) != 0) {
559 if (alias
.a_len
> MAXMODCONFNAME
) {
563 if (copyin(alias
.a_name
, name
, alias
.a_len
) != 0) {
567 if (name
[alias
.a_len
- 1] != '\0') {
572 #ifdef _SYSCALL32_IMPL
574 struct aliases32 al32
;
575 bzero(&al32
, sizeof (struct aliases32
));
576 if (copyin(ap
, &al32
, sizeof (struct aliases32
)) != 0) {
580 if (al32
.a_len
> MAXMODCONFNAME
) {
584 if (copyin((void *)(uintptr_t)al32
.a_name
,
585 name
, al32
.a_len
) != 0) {
589 if (name
[al32
.a_len
- 1] != '\0') {
593 alias
.a_next
= (void *)(uintptr_t)al32
.a_next
;
596 check_esc_sequences(name
, cname
);
597 aip
->alias_name
= strdup(cname
);
606 /* attempt to unbind all devices bound to each alias */
607 for (i
= 0; i
< mc
.num_aliases
; i
++) {
608 n
= i_ddi_unbind_devs_by_alias(
609 mc
.major
, aip
->alias_name
);
611 aip
->alias_resid
= n
;
615 * If some device bound to an alias remains in use,
616 * and override wasn't specified, no change is made to
617 * the binding state and we fail the operation.
619 if (resid
> 0 && ((mc
.flags
& MOD_UNBIND_OVERRIDE
) == 0)) {
625 * No device remains bound of any of the aliases,
626 * or force was requested. Mark each alias as
627 * inactive via delete_mbind so no future binds
628 * to this alias take place and that a new
629 * binding can be established.
632 for (i
= 0; i
< mc
.num_aliases
; i
++) {
633 if (moddebug
& MODDEBUG_BINDING
)
634 cmn_err(CE_CONT
, "Removing binding for %s "
635 "(%d active references)\n",
636 aip
->alias_name
, aip
->alias_resid
);
637 delete_mbind(aip
->alias_name
, mb_hashtab
);
643 for (i
= 0; i
< mc
.num_aliases
; i
++) {
644 if (moddebug
& MODDEBUG_BINDING
)
645 cmn_err(CE_NOTE
, "Adding binding for '%s'\n",
647 (void) make_mbind(aip
->alias_name
,
648 mc
.major
, NULL
, mb_hashtab
);
652 * Try to establish an mbinding for mc.drvname, and add it to
653 * devnames. Add class if any after establishing the major
656 (void) make_mbind(mc
.drvname
, mc
.major
, NULL
, mb_hashtab
);
657 if ((rv
= make_devname(mc
.drvname
, mc
.major
,
658 (mc
.flags
& MOD_ADDMAJBIND_UPDATE
) ?
659 DN_DRIVER_INACTIVE
: 0)) != 0) {
663 if (mc
.drvclass
[0] != '\0')
664 add_class(mc
.drvname
, mc
.drvclass
);
665 if ((mc
.flags
& MOD_ADDMAJBIND_UPDATE
) == 0) {
666 (void) i_ddi_load_drvconf(mc
.major
);
671 * Ensure that all nodes are bound to the most appropriate driver
672 * possible, attempting demotion and rebind when a more appropriate
673 * driver now exists. But not when adding a driver update-only.
675 if ((add
== 0) || ((mc
.flags
& MOD_ADDMAJBIND_UPDATE
) == 0)) {
677 i_ddi_di_cache_invalidate();
681 if (mc
.num_aliases
> 0) {
683 for (i
= 0; i
< mc
.num_aliases
; i
++) {
684 if (aip
->alias_name
!= NULL
)
685 strfree(aip
->alias_name
);
688 kmem_free(aliases
, mc
.num_aliases
* sizeof (struct alias_info
));
694 modctl_add_driver_aliases(int *data
)
696 return (modctl_update_driver_aliases(1, data
));
700 modctl_remove_driver_aliases(int *data
)
702 return (modctl_update_driver_aliases(0, data
));
706 modctl_rem_major(major_t major
)
708 struct devnames
*dnp
;
713 /* mark devnames as removed */
714 dnp
= &devnamesp
[major
];
715 LOCK_DEV_OPS(&dnp
->dn_lock
);
716 if (dnp
->dn_name
== NULL
||
717 (dnp
->dn_flags
& (DN_DRIVER_REMOVED
| DN_TAKEN_GETUDEV
))) {
718 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
721 dnp
->dn_flags
|= DN_DRIVER_REMOVED
;
722 pm_driver_removed(major
);
723 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
725 (void) i_ddi_unload_drvconf(major
);
726 i_ddi_unbind_devs(major
);
728 i_ddi_di_cache_invalidate();
730 /* purge all the bindings to this driver */
731 purge_mbind(major
, mb_hashtab
);
736 path_to_vfs(char *name
)
741 if (lookupname(name
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &vp
))
752 static int n_modpath
= 0;
753 static char *modpath_copy
;
754 static struct pathvfs
{
763 if (n_modpath
!= 0) {
764 for (i
= 0; i
< n_modpath
; i
++) {
765 vfsp
= path_to_vfs(pathvfs
[i
].path
);
766 if (vfsp
!= pathvfs
[i
].vfsp
) {
767 pathvfs
[i
].vfsp
= vfsp
;
776 * First call, initialize the pathvfs structure
778 modpath_copy
= i_ddi_strdup(default_path
, KM_SLEEP
);
781 tmp1
= strchr(tmp
, ' ');
786 tmp1
= strchr(tmp
, ' ');
789 pathvfs
= kmem_zalloc(n_modpath
* sizeof (struct pathvfs
), KM_SLEEP
);
791 for (i
= 0; i
< n_modpath
; i
++) {
792 pathvfs
[i
].path
= tmp
;
793 vfsp
= path_to_vfs(tmp
);
794 pathvfs
[i
].vfsp
= vfsp
;
795 tmp
+= strlen(tmp
) + 1;
797 return (1); /* always reread driver.conf the first time */
801 modctl_load_drvconf(major_t major
, int flags
)
806 * devfsadm -u - read all new driver.conf files
807 * and bind and configure devices for new drivers.
809 if (flags
& MOD_LOADDRVCONF_RECONF
) {
810 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE
);
812 i_ddi_di_cache_invalidate();
817 * update_drv <drv> - reload driver.conf for the specified driver
819 if (major
!= DDI_MAJOR_T_NONE
) {
820 ret
= i_ddi_load_drvconf(major
);
827 * We are invoked to rescan new driver.conf files. It is
828 * only necessary if a new file system was mounted in the
829 * module_path. Because rescanning driver.conf files can
830 * take some time on older platforms (sun4m), the following
831 * code skips unnecessary driver.conf rescans to optimize
834 if (new_vfs_in_modpath()) {
835 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE
);
837 * If we are still initializing io subsystem,
838 * load drivers with ddi-forceattach property
840 if (!i_ddi_io_initialized())
841 i_ddi_forceattach_drivers();
847 * Unload driver.conf file and follow up by attempting
848 * to rebind devices to more appropriate driver.
851 modctl_unload_drvconf(major_t major
)
858 ret
= i_ddi_unload_drvconf(major
);
861 (void) i_ddi_unbind_devs(major
);
868 check_esc_sequences(char *str
, char *cstr
)
875 for (i
= 0; i
< len
; i
++, str
++, cstr
++) {
881 * we only handle octal escape sequences for SPACE
883 if (*p
++ == '0' && *p
++ == '4' && *p
== '0') {
895 modctl_getmodpathlen(int *data
)
898 len
= strlen(default_path
);
899 if (copyout(&len
, data
, sizeof (len
)) != 0)
905 modctl_getmodpath(char *data
)
907 if (copyout(default_path
, data
, strlen(default_path
) + 1) != 0)
913 modctl_read_sysbinding_file(void)
915 (void) read_binding_file(sysbind
, sb_hashtab
, make_mbind
);
920 modctl_getmaj(char *uname
, uint_t ulen
, int *umajorp
)
928 if ((retval
= copyinstr(uname
, name
,
929 (ulen
< 256) ? ulen
: 256, 0)) != 0)
931 if ((major
= mod_name_to_major(name
)) == DDI_MAJOR_T_NONE
)
933 if (copyout(&major
, umajorp
, sizeof (major_t
)) != 0)
939 convert_constraint_string(char *constraints
, size_t len
)
946 ASSERT(constraints
!= NULL
);
949 for (i
= 0, p
= constraints
; strlen(p
) > 0; i
++, p
+= strlen(p
) + 1)
955 kmem_free(constraints
, len
);
959 array
= kmem_alloc((n
+ 1) * sizeof (char *), KM_SLEEP
);
961 for (i
= 0, p
= constraints
; i
< n
; i
++, p
+= strlen(p
) + 1) {
962 array
[i
] = i_ddi_strdup(p
, KM_SLEEP
);
966 kmem_free(constraints
, len
);
972 modctl_retire(char *path
, char *uconstraints
, size_t ulen
)
984 if ((uconstraints
== NULL
) ^ (ulen
== 0))
987 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
988 retval
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
990 kmem_free(pathbuf
, MAXPATHLEN
);
993 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
994 kmem_free(pathbuf
, MAXPATHLEN
);
997 * First check if the device is already retired.
998 * If it is, then persist the retire anyway, just in case the retire
999 * store has got out of sync with the boot archive.
1001 if (e_ddi_device_retired(devpath
)) {
1002 cmn_err(CE_NOTE
, "Device: already retired: %s", devpath
);
1003 (void) e_ddi_retire_persist(devpath
);
1004 kmem_free(devpath
, strlen(devpath
) + 1);
1010 constraints
= kmem_alloc(ulen
, KM_SLEEP
);
1011 if (copyin(uconstraints
, constraints
, ulen
)) {
1012 kmem_free(constraints
, ulen
);
1013 kmem_free(devpath
, strlen(devpath
) + 1);
1016 cons_array
= convert_constraint_string(constraints
, ulen
);
1020 * Try to retire the device first. The following
1021 * routine will return an error only if the device
1022 * is not retireable i.e. retire constraints forbid
1023 * a retire. A return of success from this routine
1024 * indicates that device is retireable.
1026 retval
= e_ddi_retire_device(devpath
, cons_array
);
1027 if (retval
!= DDI_SUCCESS
) {
1028 cmn_err(CE_WARN
, "constraints forbid retire: %s", devpath
);
1029 kmem_free(devpath
, strlen(devpath
) + 1);
1034 * Ok, the retire succeeded. Persist the retire.
1035 * If retiring a nexus, we need to only persist the
1036 * nexus retire. Any children of a retired nexus
1037 * are automatically covered by the retire store
1040 retval
= e_ddi_retire_persist(devpath
);
1042 cmn_err(CE_WARN
, "Failed to persist device retire: error %d: "
1043 "%s", retval
, devpath
);
1044 kmem_free(devpath
, strlen(devpath
) + 1);
1047 if (moddebug
& MODDEBUG_RETIRE
)
1048 cmn_err(CE_NOTE
, "Persisted retire of device: %s", devpath
);
1050 kmem_free(devpath
, strlen(devpath
) + 1);
1055 modctl_is_retired(char *path
, int *statep
)
1063 if (path
== NULL
|| statep
== NULL
)
1066 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1067 error
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
1069 kmem_free(pathbuf
, MAXPATHLEN
);
1072 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
1073 kmem_free(pathbuf
, MAXPATHLEN
);
1075 if (e_ddi_device_retired(devpath
))
1079 kmem_free(devpath
, strlen(devpath
) + 1);
1081 return (copyout(&status
, statep
, sizeof (status
)) ? EFAULT
: 0);
1085 modctl_unretire(char *path
)
1096 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1097 retval
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
1099 kmem_free(pathbuf
, MAXPATHLEN
);
1102 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
1103 kmem_free(pathbuf
, MAXPATHLEN
);
1106 * We check if a device is retired (first) before
1107 * unpersisting the retire, because we use the
1108 * retire store to determine if a device is retired.
1109 * If we unpersist first, the device will always appear
1110 * to be unretired. For the rationale behind unpersisting
1111 * a device that is not retired, see the next comment.
1113 retired
= e_ddi_device_retired(devpath
);
1116 * We call unpersist unconditionally because the lookup
1117 * for retired devices (e_ddi_device_retired()), skips "bypassed"
1118 * devices. We still want to be able remove "bypassed" entries
1119 * from the persistent store, so we unpersist unconditionally
1120 * i.e. whether or not the entry is found on a lookup.
1122 * e_ddi_retire_unpersist() returns 1 if it found and cleared
1123 * an entry from the retire store or 0 otherwise.
1125 if (e_ddi_retire_unpersist(devpath
))
1126 if (moddebug
& MODDEBUG_RETIRE
) {
1127 cmn_err(CE_NOTE
, "Unpersisted retire of device: %s",
1132 * Check if the device is already unretired. If so,
1133 * the unretire becomes a NOP
1136 cmn_err(CE_NOTE
, "Not retired: %s", devpath
);
1137 kmem_free(devpath
, strlen(devpath
) + 1);
1141 retval
= e_ddi_unretire_device(devpath
);
1143 cmn_err(CE_WARN
, "cannot unretire device: error %d, path %s\n",
1147 kmem_free(devpath
, strlen(devpath
) + 1);
1153 modctl_getname(char *uname
, uint_t ulen
, int *umajorp
)
1158 if (copyin(umajorp
, &major
, sizeof (major
)) != 0)
1160 if ((name
= mod_major_to_name(major
)) == NULL
)
1162 if ((strlen(name
) + 1) > ulen
)
1164 return (copyoutstr(name
, uname
, ulen
, NULL
));
1168 modctl_devt2instance(dev_t dev
, int *uinstancep
)
1172 if ((instance
= dev_to_instance(dev
)) == -1)
1175 return (copyout(&instance
, uinstancep
, sizeof (int)));
1179 * Return the sizeof of the device id.
1182 modctl_sizeof_devid(dev_t dev
, uint_t
*len
)
1188 if (ddi_lyr_get_devid(dev
, &devid
) == DDI_FAILURE
)
1191 sz
= ddi_devid_sizeof(devid
);
1192 ddi_devid_free(devid
);
1194 /* copyout device id size */
1195 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1202 * Return a copy of the device id.
1205 modctl_get_devid(dev_t dev
, uint_t len
, ddi_devid_t udevid
)
1212 if (ddi_lyr_get_devid(dev
, &devid
) == DDI_FAILURE
)
1215 sz
= ddi_devid_sizeof(devid
);
1217 /* Error if device id is larger than space allocated */
1219 ddi_devid_free(devid
);
1223 /* copy out device id */
1224 if (copyout(devid
, udevid
, sz
) != 0)
1226 ddi_devid_free(devid
);
1231 * return the /devices paths associated with the specified devid and
1236 modctl_devid2paths(ddi_devid_t udevid
, char *uminor_name
, uint_t flag
,
1237 size_t *ulensp
, char *upaths
)
1239 ddi_devid_t devid
= NULL
;
1241 char *minor_name
= NULL
;
1242 dev_info_t
*dip
= NULL
;
1244 struct ddi_minor_data
*dmdp
;
1249 dev_t
*devlist
= NULL
;
1255 * If upaths is NULL then we are only computing the amount of space
1256 * needed to hold the paths and returning the value in *ulensp. If we
1257 * are copying out paths then we get the amount of space allocated by
1258 * the caller. If the actual space needed for paths is larger, or
1259 * things are changing out from under us, then we return EAGAIN.
1264 if (copyin(ulensp
, &ulens
, sizeof (ulens
)) != 0)
1269 * copyin enough of the devid to determine the length then
1270 * reallocate and copy in the entire devid.
1272 devid_len
= ddi_devid_sizeof(NULL
);
1273 devid
= kmem_alloc(devid_len
, KM_SLEEP
);
1274 if (copyin(udevid
, devid
, devid_len
)) {
1279 devid_len
= ddi_devid_sizeof(devid
);
1280 kmem_free(devid
, len
);
1281 devid
= kmem_alloc(devid_len
, KM_SLEEP
);
1282 if (copyin(udevid
, devid
, devid_len
)) {
1287 /* copyin the minor name if specified. */
1288 minor_name
= uminor_name
;
1289 if ((minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1290 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1291 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
)) {
1292 minor_name
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1293 if (copyinstr(uminor_name
, minor_name
, MAXPATHLEN
, 0)) {
1300 * Use existing function to resolve the devid into a devlist.
1302 * NOTE: there is a loss of spectype information in the current
1303 * ddi_lyr_devid_to_devlist implementation. We work around this by not
1304 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
1305 * node forms in the loop processing the devlist below. It would be
1306 * best if at some point the use of this interface here was replaced
1307 * with a path oriented call.
1309 if (ddi_lyr_devid_to_devlist(devid
,
1310 (minor_name
== DEVID_MINOR_NAME_ALL
) ?
1311 DEVID_MINOR_NAME_ALL_CHR
: minor_name
,
1312 &ndevs
, &devlist
) != DDI_SUCCESS
) {
1318 * loop over the devlist, converting each devt to a path and doing
1319 * a copyout of the path and computation of the amount of space
1320 * needed to hold all the paths
1322 path
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1323 for (i
= 0, lens
= 0; i
< ndevs
; i
++) {
1325 /* find the dip associated with the dev_t */
1326 if ((dip
= e_ddi_hold_devi_by_dev(devlist
[i
], 0)) == NULL
)
1329 /* loop over all the minor nodes, skipping ones we don't want */
1330 ndi_devi_enter(dip
, &circ
);
1331 for (dmdp
= DEVI(dip
)->devi_minor
; dmdp
; dmdp
= dmdp
->next
) {
1332 if ((dmdp
->ddm_dev
!= devlist
[i
]) ||
1333 (dmdp
->type
!= DDM_MINOR
))
1336 if ((minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1337 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1338 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
) &&
1339 strcmp(minor_name
, dmdp
->ddm_name
))
1342 if ((minor_name
== DEVID_MINOR_NAME_ALL_CHR
) &&
1343 (dmdp
->ddm_spec_type
!= S_IFCHR
))
1345 if ((minor_name
== DEVID_MINOR_NAME_ALL_BLK
) &&
1346 (dmdp
->ddm_spec_type
!= S_IFBLK
))
1350 (void) ddi_pathname_minor(dmdp
, path
);
1351 len
= strlen(path
) + 1;
1352 *(path
+ len
) = '\0'; /* set double termination */
1355 /* copyout the path with double terminations */
1361 if (copyout(path
, upaths
, len
+ 1)) {
1368 ndi_devi_exit(dip
, circ
);
1369 ddi_release_devi(dip
);
1372 lens
++; /* add one for double termination */
1374 /* copy out the amount of space needed to hold the paths */
1375 if (ulensp
&& copyout(&lens
, ulensp
, sizeof (lens
))) {
1382 ndi_devi_exit(dip
, circ
);
1383 ddi_release_devi(dip
);
1386 kmem_free(path
, MAXPATHLEN
);
1388 ddi_lyr_free_devlist(devlist
, ndevs
);
1390 (minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1391 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1392 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
))
1393 kmem_free(minor_name
, MAXPATHLEN
);
1395 kmem_free(devid
, devid_len
);
1400 * Return the size of the minor name.
1403 modctl_sizeof_minorname(dev_t dev
, int spectype
, uint_t
*len
)
1408 /* get the minor name */
1409 if (ddi_lyr_get_minor_name(dev
, spectype
, &name
) == DDI_FAILURE
)
1412 sz
= strlen(name
) + 1;
1413 kmem_free(name
, sz
);
1415 /* copy out the size of the minor name */
1416 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1423 * Return the minor name.
1426 modctl_get_minorname(dev_t dev
, int spectype
, uint_t len
, char *uname
)
1432 /* get the minor name */
1433 if (ddi_lyr_get_minor_name(dev
, spectype
, &name
) == DDI_FAILURE
)
1436 sz
= strlen(name
) + 1;
1438 /* Error if the minor name is larger than the space allocated */
1440 kmem_free(name
, sz
);
1444 /* copy out the minor name */
1445 if (copyout(name
, uname
, sz
) != 0)
1447 kmem_free(name
, sz
);
1452 * Return the size of the (dev_t,spectype) devfspath name.
1455 modctl_devfspath_len(dev_t dev
, int spectype
, uint_t
*len
)
1460 /* get the path name */
1461 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1462 if (ddi_dev_pathname(dev
, spectype
, name
) == DDI_FAILURE
) {
1463 kmem_free(name
, MAXPATHLEN
);
1467 sz
= strlen(name
) + 1;
1468 kmem_free(name
, MAXPATHLEN
);
1470 /* copy out the size of the path name */
1471 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1478 * Return the (dev_t,spectype) devfspath name.
1481 modctl_devfspath(dev_t dev
, int spectype
, uint_t len
, char *uname
)
1487 /* get the path name */
1488 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1489 if (ddi_dev_pathname(dev
, spectype
, name
) == DDI_FAILURE
) {
1490 kmem_free(name
, MAXPATHLEN
);
1494 sz
= strlen(name
) + 1;
1496 /* Error if the path name is larger than the space allocated */
1498 kmem_free(name
, MAXPATHLEN
);
1502 /* copy out the path name */
1503 if (copyout(name
, uname
, sz
) != 0)
1505 kmem_free(name
, MAXPATHLEN
);
1510 * Return the size of the (major,instance) devfspath name.
1513 modctl_devfspath_mi_len(major_t major
, int instance
, uint_t
*len
)
1518 /* get the path name */
1519 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1520 if (e_ddi_majorinstance_to_path(major
, instance
, name
) != DDI_SUCCESS
) {
1521 kmem_free(name
, MAXPATHLEN
);
1525 sz
= strlen(name
) + 1;
1526 kmem_free(name
, MAXPATHLEN
);
1528 /* copy out the size of the path name */
1529 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1536 * Return the (major_instance) devfspath name.
1537 * NOTE: e_ddi_majorinstance_to_path does not require the device to attach to
1538 * return a path - it uses the instance tree.
1541 modctl_devfspath_mi(major_t major
, int instance
, uint_t len
, char *uname
)
1547 /* get the path name */
1548 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1549 if (e_ddi_majorinstance_to_path(major
, instance
, name
) != DDI_SUCCESS
) {
1550 kmem_free(name
, MAXPATHLEN
);
1554 sz
= strlen(name
) + 1;
1556 /* Error if the path name is larger than the space allocated */
1558 kmem_free(name
, MAXPATHLEN
);
1562 /* copy out the path name */
1563 if (copyout(name
, uname
, sz
) != 0)
1565 kmem_free(name
, MAXPATHLEN
);
1570 modctl_get_fbname(char *path
)
1573 char *pathname
= NULL
;
1576 /* make sure fbdev is set before we plunge in */
1580 pathname
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1581 if ((rval
= ddi_dev_pathname(fbdev
, S_IFCHR
,
1582 pathname
)) == DDI_SUCCESS
) {
1583 if (copyout(pathname
, path
, strlen(pathname
)+1) != 0) {
1587 kmem_free(pathname
, MAXPATHLEN
);
1592 * modctl_reread_dacf()
1593 * Reread the dacf rules database from the named binding file.
1594 * If NULL is specified, pass along the NULL, it means 'use the default'.
1597 modctl_reread_dacf(char *path
)
1600 char *filename
, *filenamep
;
1602 filename
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1607 if (copyinstr(path
, filename
, MAXPATHLEN
, 0) != 0) {
1611 filenamep
= filename
;
1612 filenamep
[MAXPATHLEN
- 1] = '\0';
1615 rval
= read_dacf_binding_file(filenamep
);
1617 kmem_free(filename
, MAXPATHLEN
);
1623 modctl_modevents(int subcmd
, uintptr_t a2
, uintptr_t a3
, uintptr_t a4
,
1631 case MODEVENTS_FLUSH
:
1632 /* flush all currently queued events */
1633 log_sysevent_flushq(subcmd
, flag
);
1636 case MODEVENTS_SET_DOOR_UPCALL_FILENAME
:
1638 * bind door_upcall to filename
1639 * this should only be done once per invocation
1640 * of the event daemon.
1643 filenamep
= kmem_zalloc(MOD_MAXPATH
, KM_SLEEP
);
1645 if (copyinstr((char *)a2
, filenamep
, MOD_MAXPATH
, 0)) {
1648 error
= log_sysevent_filename(filenamep
);
1650 kmem_free(filenamep
, MOD_MAXPATH
);
1653 case MODEVENTS_GETDATA
:
1654 error
= log_sysevent_copyout_data((sysevent_id_t
*)a2
,
1655 (size_t)a3
, (caddr_t
)a4
);
1658 case MODEVENTS_FREEDATA
:
1659 error
= log_sysevent_free_data((sysevent_id_t
*)a2
);
1661 case MODEVENTS_POST_EVENT
:
1662 error
= log_usr_sysevent((sysevent_t
*)a2
, (uint32_t)a3
,
1663 (sysevent_id_t
*)a4
);
1665 case MODEVENTS_REGISTER_EVENT
:
1666 error
= log_sysevent_register((char *)a2
, (char *)a3
,
1677 free_mperm(mperm_t
*mp
)
1681 if (mp
->mp_minorname
) {
1682 len
= strlen(mp
->mp_minorname
) + 1;
1683 kmem_free(mp
->mp_minorname
, len
);
1685 kmem_free(mp
, sizeof (mperm_t
));
1688 #define MP_NO_DRV_ERR \
1689 "/etc/minor_perm: no driver for %s\n"
1691 #define MP_EMPTY_MINOR \
1692 "/etc/minor_perm: empty minor name for driver %s\n"
1694 #define MP_NO_MINOR \
1695 "/etc/minor_perm: no minor matching %s for driver %s\n"
1698 * Remove mperm entry with matching minorname
1701 rem_minorperm(major_t major
, char *drvname
, mperm_t
*mp
, int is_clone
)
1704 mperm_t
*freemp
= NULL
;
1705 struct devnames
*dnp
= &devnamesp
[major
];
1708 ASSERT(mp
->mp_minorname
&& strlen(mp
->mp_minorname
) > 0);
1710 LOCK_DEV_OPS(&dnp
->dn_lock
);
1711 if (strcmp(mp
->mp_minorname
, "*") == 0) {
1712 wildmp
= ((is_clone
== 0) ?
1713 &dnp
->dn_mperm_wild
: &dnp
->dn_mperm_clone
);
1718 mp_head
= &dnp
->dn_mperm
;
1720 if (strcmp((*mp_head
)->mp_minorname
,
1721 mp
->mp_minorname
) != 0) {
1722 mp_head
= &(*mp_head
)->mp_next
;
1725 /* remove the entry */
1727 *mp_head
= freemp
->mp_next
;
1732 if (moddebug
& MODDEBUG_MINORPERM
) {
1733 cmn_err(CE_CONT
, "< %s %s 0%o %d %d\n",
1734 drvname
, freemp
->mp_minorname
,
1735 freemp
->mp_mode
& 0777,
1736 freemp
->mp_uid
, freemp
->mp_gid
);
1740 if (moddebug
& MODDEBUG_MINORPERM
) {
1741 cmn_err(CE_CONT
, MP_NO_MINOR
,
1742 drvname
, mp
->mp_minorname
);
1746 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
1750 * Add minor perm entry
1753 add_minorperm(major_t major
, char *drvname
, mperm_t
*mp
, int is_clone
)
1756 mperm_t
*freemp
= NULL
;
1757 struct devnames
*dnp
= &devnamesp
[major
];
1760 ASSERT(mp
->mp_minorname
&& strlen(mp
->mp_minorname
) > 0);
1763 * Note that update_drv replace semantics require
1764 * replacing matching entries with the new permissions.
1766 LOCK_DEV_OPS(&dnp
->dn_lock
);
1767 if (strcmp(mp
->mp_minorname
, "*") == 0) {
1768 wildmp
= ((is_clone
== 0) ?
1769 &dnp
->dn_mperm_wild
: &dnp
->dn_mperm_clone
);
1774 mperm_t
*p
, *v
= NULL
;
1775 for (p
= dnp
->dn_mperm
; p
; v
= p
, p
= p
->mp_next
) {
1776 if (strcmp(p
->mp_minorname
, mp
->mp_minorname
) == 0) {
1781 mp
->mp_next
= p
->mp_next
;
1787 mp_head
= &dnp
->dn_mperm
;
1788 if (*mp_head
== NULL
) {
1791 mp
->mp_next
= *mp_head
;
1798 if (moddebug
& MODDEBUG_MINORPERM
) {
1799 cmn_err(CE_CONT
, "< %s %s 0%o %d %d\n",
1800 drvname
, freemp
->mp_minorname
,
1801 freemp
->mp_mode
& 0777,
1802 freemp
->mp_uid
, freemp
->mp_gid
);
1806 if (moddebug
& MODDEBUG_MINORPERM
) {
1807 cmn_err(CE_CONT
, "> %s %s 0%o %d %d\n",
1808 drvname
, mp
->mp_minorname
, mp
->mp_mode
& 0777,
1809 mp
->mp_uid
, mp
->mp_gid
);
1811 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
1816 process_minorperm(int cmd
, nvlist_t
*nvl
)
1826 ASSERT(cmd
== MODLOADMINORPERM
||
1827 cmd
== MODADDMINORPERM
|| cmd
== MODREMMINORPERM
);
1830 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
1831 name
= nvpair_name(nvp
);
1834 (void) nvpair_value_string(nvp
, &minor
);
1835 major
= ddi_name_to_major(name
);
1836 if (major
!= DDI_MAJOR_T_NONE
) {
1837 mp
= kmem_zalloc(sizeof (*mp
), KM_SLEEP
);
1838 if (minor
== NULL
|| strlen(minor
) == 0) {
1839 if (moddebug
& MODDEBUG_MINORPERM
) {
1840 cmn_err(CE_CONT
, MP_EMPTY_MINOR
, name
);
1846 * The minor name of a node using the clone
1847 * driver must be the driver name. To avoid
1848 * multiple searches, we map entries in the form
1849 * clone:<driver> to <driver>:*. This also allows us
1850 * to filter out some of the litter in /etc/minor_perm.
1851 * Minor perm alias entries where the name is not
1852 * the driver kept on the clone list itself.
1853 * This all seems very fragile as a driver could
1854 * be introduced with an existing alias name.
1856 if (strcmp(name
, "clone") == 0) {
1857 minmaj
= ddi_name_to_major(minor
);
1858 if (minmaj
!= DDI_MAJOR_T_NONE
) {
1859 if (moddebug
& MODDEBUG_MINORPERM
) {
1861 "mapping %s:%s to %s:*\n",
1862 name
, minor
, minor
);
1873 i_ddi_strdup(minor
, KM_SLEEP
);
1877 if (moddebug
& MODDEBUG_MINORPERM
) {
1878 cmn_err(CE_CONT
, MP_NO_DRV_ERR
, name
);
1883 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1884 ASSERT(strcmp(nvpair_name(nvp
), "mode") == 0);
1886 (void) nvpair_value_int32(nvp
, (int *)&mp
->mp_mode
);
1888 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1889 ASSERT(strcmp(nvpair_name(nvp
), "uid") == 0);
1891 (void) nvpair_value_uint32(nvp
, &mp
->mp_uid
);
1893 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1894 ASSERT(strcmp(nvpair_name(nvp
), "gid") == 0);
1896 (void) nvpair_value_uint32(nvp
, &mp
->mp_gid
);
1898 if (cmd
== MODREMMINORPERM
) {
1899 rem_minorperm(major
, name
, mp
, is_clone
);
1902 add_minorperm(major
, name
, mp
, is_clone
);
1907 if (cmd
== MODLOADMINORPERM
)
1908 minorperm_loaded
= 1;
1911 * Reset permissions of cached dv_nodes
1913 (void) devfs_reset_perm(DV_RESET_PERM
);
1919 modctl_minorperm(int cmd
, char *usrbuf
, size_t buflen
)
1923 char *buf
= kmem_alloc(buflen
, KM_SLEEP
);
1925 if ((error
= ddi_copyin(usrbuf
, buf
, buflen
, 0)) != 0) {
1926 kmem_free(buf
, buflen
);
1930 error
= nvlist_unpack(buf
, buflen
, &nvl
, KM_SLEEP
);
1931 kmem_free(buf
, buflen
);
1935 error
= process_minorperm(cmd
, nvl
);
1948 list_node_t pe_node
;
1954 modctl_inst_walker(const char *path
, in_node_t
*np
, in_drv_t
*dp
, void *arg
)
1956 struct walk_args
*wargs
= (struct walk_args
*)arg
;
1957 struct path_elem
*pe
;
1961 * Search may be restricted to a single driver in the case of rem_drv
1963 if (wargs
->wa_drvname
&&
1964 strcmp(dp
->ind_driver_name
, wargs
->wa_drvname
) != 0)
1965 return (INST_WALK_CONTINUE
);
1967 pe
= kmem_zalloc(sizeof (*pe
), KM_SLEEP
);
1968 pe
->pe_dir
= i_ddi_strdup((char *)path
, KM_SLEEP
);
1969 pe
->pe_dirlen
= strlen(pe
->pe_dir
) + 1;
1970 ASSERT(strrchr(pe
->pe_dir
, '/') != NULL
);
1971 nodename
= strrchr(pe
->pe_dir
, '/');
1973 pe
->pe_nodename
= nodename
;
1974 list_insert_tail(&wargs
->wa_pathlist
, pe
);
1976 return (INST_WALK_CONTINUE
);
1980 * /devices attribute nodes clean-up optionally performed
1981 * when removing a driver (rem_drv -C).
1983 * Removing attribute nodes allows a machine to be reprovisioned
1984 * without the side-effect of inadvertently picking up stale
1985 * device node ownership or permissions.
1987 * Preserving attributes (not performing cleanup) allows devices
1988 * attribute changes to be preserved across upgrades, as
1989 * upgrade rather heavy-handedly does a rem_drv/add_drv cycle.
1992 modctl_remdrv_cleanup(const char *u_drvname
)
1994 struct walk_args
*wargs
;
1995 struct path_elem
*pe
;
1999 drvname
= kmem_alloc(MAXMODCONFNAME
, KM_SLEEP
);
2000 if ((err
= copyinstr(u_drvname
, drvname
, MAXMODCONFNAME
, 0))) {
2001 kmem_free(drvname
, MAXMODCONFNAME
);
2006 * First go through the instance database. For each
2007 * instance of a device bound to the driver being
2008 * removed, remove any underlying devfs attribute nodes.
2010 * This is a two-step process. First we go through
2011 * the instance data itself, constructing a list of
2012 * the nodes discovered. The second step is then
2013 * to find and remove any devfs attribute nodes
2014 * for the instances discovered in the first step.
2015 * The two-step process avoids any difficulties
2016 * which could arise by holding the instance data
2017 * lock with simultaneous devfs operations.
2019 wargs
= kmem_zalloc(sizeof (*wargs
), KM_SLEEP
);
2021 wargs
->wa_drvname
= drvname
;
2022 list_create(&wargs
->wa_pathlist
,
2023 sizeof (struct path_elem
), offsetof(struct path_elem
, pe_node
));
2025 (void) e_ddi_walk_instances(modctl_inst_walker
, (void *)wargs
);
2027 for (pe
= list_head(&wargs
->wa_pathlist
); pe
!= NULL
;
2028 pe
= list_next(&wargs
->wa_pathlist
, pe
)) {
2029 err
= devfs_remdrv_cleanup((const char *)pe
->pe_dir
,
2030 (const char *)pe
->pe_nodename
);
2035 while ((pe
= list_head(&wargs
->wa_pathlist
)) != NULL
) {
2036 list_remove(&wargs
->wa_pathlist
, pe
);
2037 kmem_free(pe
->pe_dir
, pe
->pe_dirlen
);
2038 kmem_free(pe
, sizeof (*pe
));
2040 kmem_free(wargs
, sizeof (*wargs
));
2043 * Pseudo nodes aren't recorded in the instance database
2044 * so any such nodes need to be handled separately.
2046 err
= devfs_remdrv_cleanup("pseudo", (const char *)drvname
);
2050 kmem_free(drvname
, MAXMODCONFNAME
);
2055 * Perform a cleanup of non-existent /devices attribute nodes,
2056 * similar to rem_drv -C, but for all drivers/devices.
2057 * This is also optional, performed as part of devfsadm -C.
2060 dev_devices_cleanup()
2062 struct walk_args
*wargs
;
2063 struct path_elem
*pe
;
2069 * It's expected that all drivers have been loaded and
2070 * module unloading disabled while performing cleanup.
2072 ASSERT(modunload_disable_count
> 0);
2074 wargs
= kmem_zalloc(sizeof (*wargs
), KM_SLEEP
);
2075 wargs
->wa_drvname
= NULL
;
2076 list_create(&wargs
->wa_pathlist
,
2077 sizeof (struct path_elem
), offsetof(struct path_elem
, pe_node
));
2079 (void) e_ddi_walk_instances(modctl_inst_walker
, (void *)wargs
);
2081 path
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2083 for (pe
= list_head(&wargs
->wa_pathlist
); pe
!= NULL
;
2084 pe
= list_next(&wargs
->wa_pathlist
, pe
)) {
2085 (void) snprintf(path
, MAXPATHLEN
, "%s/%s",
2086 pe
->pe_dir
, pe
->pe_nodename
);
2087 devi
= e_ddi_hold_devi_by_path(path
, 0);
2089 ddi_release_devi(devi
);
2091 err
= devfs_remdrv_cleanup((const char *)pe
->pe_dir
,
2092 (const char *)pe
->pe_nodename
);
2095 "devfs: %s: clean-up error %d\n",
2101 while ((pe
= list_head(&wargs
->wa_pathlist
)) != NULL
) {
2102 list_remove(&wargs
->wa_pathlist
, pe
);
2103 kmem_free(pe
->pe_dir
, pe
->pe_dirlen
);
2104 kmem_free(pe
, sizeof (*pe
));
2106 kmem_free(wargs
, sizeof (*wargs
));
2107 kmem_free(path
, MAXPATHLEN
);
2111 modctl_allocpriv(const char *name
)
2113 char *pstr
= kmem_alloc(PRIVNAME_MAX
, KM_SLEEP
);
2116 if ((error
= copyinstr(name
, pstr
, PRIVNAME_MAX
, 0))) {
2117 kmem_free(pstr
, PRIVNAME_MAX
);
2120 error
= priv_getbyname(pstr
, PRIV_ALLOC
);
2125 kmem_free(pstr
, PRIVNAME_MAX
);
2130 modctl_devexists(const char *upath
, int pathlen
)
2136 * copy in the path, including the terminating null
2139 if (pathlen
<= 1 || pathlen
> MAXPATHLEN
)
2141 path
= kmem_zalloc(pathlen
+ 1, KM_SLEEP
);
2142 if ((ret
= copyinstr(upath
, path
, pathlen
, NULL
)) == 0) {
2143 ret
= sdev_modctl_devexists(path
);
2146 kmem_free(path
, pathlen
+ 1);
2151 modctl_devreaddir(const char *udir
, int udirlen
,
2152 char *upaths
, int64_t *ulensp
)
2155 char **dirlist
= NULL
;
2166 * If upaths is NULL then we are only computing the amount of space
2167 * needed to return the paths, with the value returned in *ulensp. If we
2168 * are copying out paths then we get the amount of space allocated by
2169 * the caller. If the actual space needed for paths is larger, or
2170 * things are changing out from under us, then we return EAGAIN.
2175 if (copyin(ulensp
, &ulens
, sizeof (ulens
)) != 0)
2180 * copyin the /dev path including terminating null
2183 if (udirlen
<= 1 || udirlen
> MAXPATHLEN
)
2185 dir
= kmem_zalloc(udirlen
+ 1, KM_SLEEP
);
2186 if ((ret
= copyinstr(udir
, dir
, udirlen
, NULL
)) != 0)
2189 if ((ret
= sdev_modctl_readdir(dir
, &dirlist
,
2190 &npaths
, &npaths_alloc
, 0)) != 0) {
2191 ASSERT(dirlist
== NULL
);
2196 for (i
= 0; i
< npaths
; i
++) {
2197 lens
+= strlen(dirlist
[i
]) + 1;
2199 lens
++; /* add one for double termination */
2207 paths
= kmem_alloc(lens
, KM_SLEEP
);
2210 for (i
= 0; i
< npaths
; i
++) {
2211 n
= strlen(dirlist
[i
]) + 1;
2212 bcopy(dirlist
[i
], p
, n
);
2217 if (copyout(paths
, upaths
, lens
)) {
2224 /* copy out the amount of space needed to hold the paths */
2225 if (copyout(&lens
, ulensp
, sizeof (lens
)))
2230 sdev_modctl_readdir_free(dirlist
, npaths
, npaths_alloc
);
2232 kmem_free(paths
, lens
);
2233 kmem_free(dir
, udirlen
+ 1);
2238 modctl_devemptydir(const char *udir
, int udirlen
, int *uempty
)
2242 char **dirlist
= NULL
;
2248 * copyin the /dev path including terminating null
2251 if (udirlen
<= 1 || udirlen
> MAXPATHLEN
)
2253 dir
= kmem_zalloc(udirlen
+ 1, KM_SLEEP
);
2254 if ((ret
= copyinstr(udir
, dir
, udirlen
, NULL
)) != 0)
2257 if ((ret
= sdev_modctl_readdir(dir
, &dirlist
,
2258 &npaths
, &npaths_alloc
, 1)) != 0) {
2262 empty
= npaths
? 0 : 1;
2263 if (copyout(&empty
, uempty
, sizeof (empty
)))
2268 sdev_modctl_readdir_free(dirlist
, npaths
, npaths_alloc
);
2269 kmem_free(dir
, udirlen
+ 1);
2274 modctl_hp(int subcmd
, const char *path
, char *cn_name
, uintptr_t arg
,
2278 size_t pathsz
, namesz
;
2279 char *devpath
, *cn_name_str
;
2284 devpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
2285 error
= copyinstr(path
, devpath
, MAXPATHLEN
, &pathsz
);
2287 kmem_free(devpath
, MAXPATHLEN
);
2291 cn_name_str
= kmem_zalloc(MAXNAMELEN
, KM_SLEEP
);
2292 error
= copyinstr(cn_name
, cn_name_str
, MAXNAMELEN
, &namesz
);
2294 kmem_free(devpath
, MAXPATHLEN
);
2295 kmem_free(cn_name_str
, MAXNAMELEN
);
2301 case MODHPOPS_CHANGE_STATE
:
2302 error
= ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE
, devpath
,
2303 cn_name_str
, arg
, NULL
);
2305 case MODHPOPS_CREATE_PORT
:
2306 /* Create an empty PORT */
2307 error
= ddihp_modctl(DDI_HPOP_CN_CREATE_PORT
, devpath
,
2308 cn_name_str
, NULL
, NULL
);
2310 case MODHPOPS_REMOVE_PORT
:
2311 /* Remove an empty PORT */
2312 error
= ddihp_modctl(DDI_HPOP_CN_REMOVE_PORT
, devpath
,
2313 cn_name_str
, NULL
, NULL
);
2315 case MODHPOPS_BUS_GET
:
2316 error
= ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY
, devpath
,
2317 cn_name_str
, arg
, rval
);
2319 case MODHPOPS_BUS_SET
:
2320 error
= ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY
, devpath
,
2321 cn_name_str
, arg
, rval
);
2328 kmem_free(devpath
, MAXPATHLEN
);
2329 kmem_free(cn_name_str
, MAXNAMELEN
);
2335 modctl_moddevname(int subcmd
, uintptr_t a1
, uintptr_t a2
)
2340 case MODDEVNAME_LOOKUPDOOR
:
2341 error
= devname_filename_register((char *)a1
);
2343 case MODDEVNAME_PROFILE
:
2344 error
= devname_profile_update((char *)a1
, (size_t)a2
);
2346 case MODDEVNAME_RECONFIG
:
2347 i_ddi_set_reconfig();
2349 case MODDEVNAME_SYSAVAIL
:
2350 i_ddi_set_sysavail();
2362 modctl(int cmd
, uintptr_t a1
, uintptr_t a2
, uintptr_t a3
, uintptr_t a4
,
2368 if (secpolicy_modctl(CRED(), cmd
) != 0)
2369 return (set_errno(EPERM
));
2372 case MODLOAD
: /* load a module */
2373 error
= modctl_modload((int)a1
, (char *)a2
, (int *)a3
);
2376 case MODUNLOAD
: /* unload a module */
2377 error
= modctl_modunload((modid_t
)a1
);
2380 case MODINFO
: /* get module status */
2381 error
= modctl_modinfo((modid_t
)a1
, (struct modinfo
*)a2
);
2384 case MODRESERVED
: /* get last major number in range */
2385 error
= modctl_modreserve((modid_t
)a1
, (int *)a2
);
2388 case MODSETMINIROOT
: /* we are running in miniroot */
2393 case MODADDMAJBIND
: /* add major / driver alias bindings */
2394 error
= modctl_add_driver_aliases((int *)a2
);
2397 case MODGETPATHLEN
: /* get modpath length */
2398 error
= modctl_getmodpathlen((int *)a2
);
2401 case MODGETPATH
: /* get modpath */
2402 error
= modctl_getmodpath((char *)a2
);
2405 case MODREADSYSBIND
: /* read system call binding file */
2406 error
= modctl_read_sysbinding_file();
2409 case MODGETMAJBIND
: /* get major number for named device */
2410 error
= modctl_getmaj((char *)a1
, (uint_t
)a2
, (int *)a3
);
2413 case MODGETNAME
: /* get name of device given major number */
2414 error
= modctl_getname((char *)a1
, (uint_t
)a2
, (int *)a3
);
2417 case MODDEVT2INSTANCE
:
2418 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2421 #ifdef _SYSCALL32_IMPL
2426 error
= modctl_devt2instance(dev
, (int *)a2
);
2429 case MODSIZEOF_DEVID
: /* sizeof device id of device given dev_t */
2430 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2433 #ifdef _SYSCALL32_IMPL
2438 error
= modctl_sizeof_devid(dev
, (uint_t
*)a2
);
2441 case MODGETDEVID
: /* get device id of device given dev_t */
2442 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2445 #ifdef _SYSCALL32_IMPL
2450 error
= modctl_get_devid(dev
, (uint_t
)a2
, (ddi_devid_t
)a3
);
2453 case MODSIZEOF_MINORNAME
: /* sizeof minor nm (dev_t,spectype) */
2454 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2455 error
= modctl_sizeof_minorname((dev_t
)a1
, (int)a2
,
2458 #ifdef _SYSCALL32_IMPL
2460 error
= modctl_sizeof_minorname(expldev(a1
), (int)a2
,
2467 case MODGETMINORNAME
: /* get minor name of (dev_t,spectype) */
2468 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2469 error
= modctl_get_minorname((dev_t
)a1
, (int)a2
,
2470 (uint_t
)a3
, (char *)a4
);
2472 #ifdef _SYSCALL32_IMPL
2474 error
= modctl_get_minorname(expldev(a1
), (int)a2
,
2475 (uint_t
)a3
, (char *)a4
);
2480 case MODGETDEVFSPATH_LEN
: /* sizeof path nm of (dev_t,spectype) */
2481 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2482 error
= modctl_devfspath_len((dev_t
)a1
, (int)a2
,
2485 #ifdef _SYSCALL32_IMPL
2487 error
= modctl_devfspath_len(expldev(a1
), (int)a2
,
2494 case MODGETDEVFSPATH
: /* get path name of (dev_t,spec) type */
2495 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2496 error
= modctl_devfspath((dev_t
)a1
, (int)a2
,
2497 (uint_t
)a3
, (char *)a4
);
2499 #ifdef _SYSCALL32_IMPL
2501 error
= modctl_devfspath(expldev(a1
), (int)a2
,
2502 (uint_t
)a3
, (char *)a4
);
2507 case MODGETDEVFSPATH_MI_LEN
: /* sizeof path nm of (major,instance) */
2508 error
= modctl_devfspath_mi_len((major_t
)a1
, (int)a2
,
2512 case MODGETDEVFSPATH_MI
: /* get path name of (major,instance) */
2513 error
= modctl_devfspath_mi((major_t
)a1
, (int)a2
,
2514 (uint_t
)a3
, (char *)a4
);
2519 error
= modctl_modevents((int)a1
, a2
, a3
, a4
, (uint_t
)a5
);
2522 case MODGETFBNAME
: /* get the framebuffer name */
2523 error
= modctl_get_fbname((char *)a1
);
2526 case MODREREADDACF
: /* reread dacf rule database from given file */
2527 error
= modctl_reread_dacf((char *)a1
);
2530 case MODLOADDRVCONF
: /* load driver.conf file for major */
2531 error
= modctl_load_drvconf((major_t
)a1
, (int)a2
);
2534 case MODUNLOADDRVCONF
: /* unload driver.conf file for major */
2535 error
= modctl_unload_drvconf((major_t
)a1
);
2538 case MODREMMAJBIND
: /* remove a major binding */
2539 error
= modctl_rem_major((major_t
)a1
);
2542 case MODREMDRVALIAS
: /* remove a major/alias binding */
2543 error
= modctl_remove_driver_aliases((int *)a2
);
2546 case MODDEVID2PATHS
: /* get paths given devid */
2547 error
= modctl_devid2paths((ddi_devid_t
)a1
, (char *)a2
,
2548 (uint_t
)a3
, (size_t *)a4
, (char *)a5
);
2551 case MODSETDEVPOLICY
: /* establish device policy */
2552 error
= devpolicy_load((int)a1
, (size_t)a2
, (devplcysys_t
*)a3
);
2555 case MODGETDEVPOLICY
: /* get device policy */
2556 error
= devpolicy_get((int *)a1
, (size_t)a2
,
2557 (devplcysys_t
*)a3
);
2561 error
= modctl_allocpriv((const char *)a1
);
2564 case MODGETDEVPOLICYBYNAME
:
2565 error
= devpolicy_getbyname((size_t)a1
,
2566 (devplcysys_t
*)a2
, (char *)a3
);
2569 case MODLOADMINORPERM
:
2570 case MODADDMINORPERM
:
2571 case MODREMMINORPERM
:
2572 error
= modctl_minorperm(cmd
, (char *)a1
, (size_t)a2
);
2575 case MODREMDRVCLEANUP
:
2576 error
= modctl_remdrv_cleanup((const char *)a1
);
2579 case MODDEVEXISTS
: /* non-reconfiguring /dev lookup */
2580 error
= modctl_devexists((const char *)a1
, (size_t)a2
);
2583 case MODDEVREADDIR
: /* non-reconfiguring /dev readdir */
2584 error
= modctl_devreaddir((const char *)a1
, (size_t)a2
,
2585 (char *)a3
, (int64_t *)a4
);
2588 case MODDEVEMPTYDIR
: /* non-reconfiguring /dev emptydir */
2589 error
= modctl_devemptydir((const char *)a1
, (size_t)a2
,
2594 error
= modctl_moddevname((int)a1
, a2
, a3
);
2597 case MODRETIRE
: /* retire device named by physpath a1 */
2598 error
= modctl_retire((char *)a1
, (char *)a2
, (size_t)a3
);
2601 case MODISRETIRED
: /* check if a device is retired. */
2602 error
= modctl_is_retired((char *)a1
, (int *)a2
);
2605 case MODUNRETIRE
: /* unretire device named by physpath a1 */
2606 error
= modctl_unretire((char *)a1
);
2609 case MODHPOPS
: /* hotplug operations */
2610 /* device named by physpath a2 and Connection name a3 */
2611 error
= modctl_hp((int)a1
, (char *)a2
, (char *)a3
, a4
, a5
);
2619 return (error
? set_errno(error
) : 0);
2623 * Calls to kobj_load_module()() are handled off to this routine in a
2627 modload_thread(struct loadmt
*ltp
)
2629 /* load the module and signal the creator of this thread */
2633 mutex_init(&cpr_lk
, NULL
, MUTEX_DEFAULT
, NULL
);
2634 CALLB_CPR_INIT(&cpr_i
, &cpr_lk
, callb_generic_cpr
, "modload");
2635 /* borrow the devi lock from thread which invoked us */
2636 pm_borrow_lock(ltp
->owner
);
2637 ltp
->retval
= kobj_load_module(ltp
->mp
, ltp
->usepath
);
2640 mutex_enter(&cpr_lk
);
2641 CALLB_CPR_EXIT(&cpr_i
);
2642 mutex_destroy(&cpr_lk
);
2647 * load a module, adding a reference if caller specifies rmodp. If rmodp
2648 * is specified then an errno is returned, otherwise a module index is
2649 * returned (-1 on error).
2652 modrload(const char *subdir
, const char *filename
, struct modctl
**rmodp
)
2654 struct modctl
*modp
;
2657 int retval
= EINVAL
;
2661 *rmodp
= NULL
; /* avoid garbage */
2663 if (subdir
!= NULL
) {
2665 * refuse / in filename to prevent "../" escapes.
2667 if (strchr(filename
, '/') != NULL
)
2668 return (rmodp
? retval
: id
);
2671 * allocate enough space for <subdir>/<filename><NULL>
2673 size
= strlen(subdir
) + strlen(filename
) + 2;
2674 fullname
= kmem_zalloc(size
, KM_SLEEP
);
2675 (void) sprintf(fullname
, "%s/%s", subdir
, filename
);
2677 fullname
= (char *)filename
;
2680 modp
= mod_hold_installed_mod(fullname
, 1, 0, &retval
);
2684 /* add mod_ref and return *rmodp */
2685 mutex_enter(&mod_lock
);
2687 mutex_exit(&mod_lock
);
2690 mod_release_mod(modp
);
2691 CPU_STATS_ADDQ(CPU
, sys
, modload
, 1);
2694 done
: if (subdir
!= NULL
)
2695 kmem_free(fullname
, size
);
2696 return (rmodp
? retval
: id
);
2700 * This is the primary kernel interface to load a module. It loads and
2701 * installs the named module. It does not hold mod_ref of the module, so
2702 * a module unload attempt can occur at any time - it is up to the
2703 * _fini/mod_remove implementation to determine if unload will succeed.
2706 modload(const char *subdir
, const char *filename
)
2708 return (modrload(subdir
, filename
, NULL
));
2712 * Load a module using a series of qualified names from most specific to least
2713 * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
2714 * Value returned in *chosen
2715 * foo/bar.baz.1.2.3 3
2720 * Return the module ID on success; -1 if no module was loaded. On success
2721 * and if 'chosen' is not NULL we also return the number of suffices that
2722 * were in the module we chose to load.
2725 modload_qualified(const char *subdir
, const char *p1
,
2726 const char *p2
, const char *delim
, uint_t suffv
[], int suffc
, int *chosen
)
2728 char path
[MOD_MAXPATH
];
2729 size_t n
, resid
= sizeof (path
);
2737 n
= snprintf(p
, resid
, "%s/%s%s%s", subdir
, p1
, delim
, p2
);
2739 n
= snprintf(p
, resid
, "%s/%s", subdir
, p1
);
2746 dotv
= kmem_alloc(sizeof (char *) * (suffc
+ 1), KM_SLEEP
);
2748 for (i
= 0; i
< suffc
; i
++) {
2750 n
= snprintf(p
, resid
, "%s%u", delim
, suffv
[i
]);
2753 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2763 for (i
= suffc
; i
>= 0; i
--) {
2765 mp
= mod_hold_installed_mod(path
, 1, 1, &rc
);
2768 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2770 mod_release_mod(mp
);
2777 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2785 modloadonly(const char *subdir
, const char *filename
)
2787 struct modctl
*modp
;
2792 if (subdir
!= NULL
) {
2794 * allocate enough space for <subdir>/<filename><NULL>
2796 size
= strlen(subdir
) + strlen(filename
) + 2;
2797 fullname
= kmem_zalloc(size
, KM_SLEEP
);
2798 (void) sprintf(fullname
, "%s/%s", subdir
, filename
);
2800 fullname
= (char *)filename
;
2803 modp
= mod_hold_loaded_mod(NULL
, fullname
, &retval
);
2806 mod_release_mod(modp
);
2810 kmem_free(fullname
, size
);
2818 * Try to uninstall and unload a module, removing a reference if caller
2822 modunrload(modid_t id
, struct modctl
**rmodp
, int unload
)
2824 struct modctl
*modp
;
2828 *rmodp
= NULL
; /* avoid garbage */
2830 if ((modp
= mod_hold_by_id((modid_t
)id
)) == NULL
)
2834 mutex_enter(&mod_lock
);
2836 if (modp
->mod_ref
== 0)
2837 mod_uninstall_ref_zero
++;
2838 mutex_exit(&mod_lock
);
2843 retval
= moduninstall(modp
);
2846 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
2847 } else if (retval
== EALREADY
)
2848 retval
= 0; /* already unloaded, not an error */
2852 mod_release_mod(modp
);
2857 * Uninstall and unload a module.
2860 modunload(modid_t id
)
2864 /* synchronize with any active modunload_disable() */
2866 if (ddi_root_node())
2867 (void) devfs_clean(ddi_root_node(), NULL
, 0);
2868 retval
= modunrload(id
, NULL
, 1);
2874 * Return status of a loaded module.
2877 modinfo(modid_t id
, struct modinfo
*modinfop
)
2879 struct modctl
*modp
;
2883 mid
= modinfop
->mi_id
;
2884 if (modinfop
->mi_info
& MI_INFO_ALL
) {
2885 while ((modp
= mod_hold_next_by_id(mid
++)) != NULL
) {
2886 if ((modinfop
->mi_info
& MI_INFO_CNT
) ||
2887 modp
->mod_installed
)
2889 mod_release_mod(modp
);
2894 modp
= mod_hold_by_id(id
);
2897 if (!(modinfop
->mi_info
& MI_INFO_CNT
) &&
2898 (modp
->mod_installed
== 0)) {
2899 mod_release_mod(modp
);
2904 modinfop
->mi_rev
= 0;
2905 modinfop
->mi_state
= 0;
2906 for (i
= 0; i
< MODMAXLINK
; i
++) {
2907 modinfop
->mi_msinfo
[i
].msi_p0
= -1;
2908 modinfop
->mi_msinfo
[i
].msi_linkinfo
[0] = 0;
2910 if (modp
->mod_loaded
) {
2911 modinfop
->mi_state
= MI_LOADED
;
2912 kobj_getmodinfo(modp
->mod_mp
, modinfop
);
2914 if (modp
->mod_installed
) {
2915 modinfop
->mi_state
|= MI_INSTALLED
;
2917 (void) mod_getinfo(modp
, modinfop
);
2920 modinfop
->mi_id
= modp
->mod_id
;
2921 modinfop
->mi_loadcnt
= modp
->mod_loadcnt
;
2922 (void) strcpy(modinfop
->mi_name
, modp
->mod_modname
);
2924 mod_release_mod(modp
);
2928 static char mod_stub_err
[] = "mod_hold_stub: Couldn't load stub module %s";
2929 static char no_err
[] = "No error function for weak stub %s";
2932 * used by the stubs themselves to load and hold a module.
2933 * Returns 0 if the module is successfully held;
2934 * the stub needs to call mod_release_stub().
2935 * -1 if the stub should just call the err_fcn.
2936 * Note that this code is stretched out so that we avoid subroutine calls
2937 * and optimize for the most likely case. That is, the case where the
2938 * module is loaded and installed and not held. In that case we just inc
2939 * the mod_ref count and continue.
2942 mod_hold_stub(struct mod_stub_info
*stub
)
2945 struct mod_modinfo
*mip
;
2947 mip
= stub
->mods_modinfo
;
2949 mutex_enter(&mod_lock
);
2951 /* we do mod_hold_by_modctl inline for speed */
2954 if ((mp
= mip
->mp
) != NULL
) {
2955 if (mp
->mod_busy
== 0) {
2956 if (mp
->mod_installed
) {
2957 /* increment the reference count */
2959 ASSERT(mp
->mod_ref
&& mp
->mod_installed
);
2960 mutex_exit(&mod_lock
);
2964 mp
->mod_inprogress_thread
=
2965 (curthread
== NULL
?
2966 (kthread_id_t
)-1 : curthread
);
2970 * wait one time and then go see if someone
2971 * else has resolved the stub (set mip->mp).
2973 if (mod_hold_by_modctl(mp
,
2974 MOD_WAIT_ONCE
| MOD_LOCK_HELD
))
2975 goto mod_check_again
;
2978 * what we have now may have been unloaded!, in
2979 * that case, mip->mp will be NULL, we'll hit this
2980 * module and load again..
2982 cmn_err(CE_PANIC
, "mod_hold_stub should have blocked");
2984 mutex_exit(&mod_lock
);
2986 /* first time we've hit this module */
2987 mutex_exit(&mod_lock
);
2988 mp
= mod_hold_by_name(mip
->modm_module_name
);
2993 * If we are here, it means that the following conditions
2997 * this thread has set the mp->mod_busy = 1
2998 * mp->mod_installed = 0
3002 ASSERT(mp
->mod_busy
== 1);
3004 if (mp
->mod_installed
== 0) {
3005 /* Module not loaded, if weak stub don't load it */
3006 if (stub
->mods_flag
& MODS_WEAK
) {
3007 if (stub
->mods_errfcn
== NULL
) {
3008 mod_release_mod(mp
);
3009 cmn_err(CE_PANIC
, no_err
,
3010 mip
->modm_module_name
);
3013 /* Not a weak stub so load the module */
3015 if (mod_load(mp
, 1) != 0 || modinstall(mp
) != 0) {
3017 * If mod_load() was successful
3018 * and modinstall() failed, then
3019 * unload the module.
3024 mod_release_mod(mp
);
3025 if (stub
->mods_errfcn
== NULL
) {
3026 cmn_err(CE_PANIC
, mod_stub_err
,
3027 mip
->modm_module_name
);
3036 * At this point module is held and loaded. Release
3037 * the mod_busy and mod_inprogress_thread before
3038 * returning. We actually call mod_release() here so
3039 * that if another stub wants to access this module,
3040 * it can do so. mod_ref is incremented before mod_release()
3041 * is called to prevent someone else from snatching the
3042 * module from this thread.
3044 mutex_enter(&mod_lock
);
3046 ASSERT(mp
->mod_ref
&&
3047 (mp
->mod_loaded
|| (stub
->mods_flag
& MODS_WEAK
)));
3049 mutex_exit(&mod_lock
);
3054 mod_release_stub(struct mod_stub_info
*stub
)
3056 struct modctl
*mp
= stub
->mods_modinfo
->mp
;
3058 /* inline mod_release_mod */
3059 mutex_enter(&mod_lock
);
3060 ASSERT(mp
->mod_ref
&&
3061 (mp
->mod_loaded
|| (stub
->mods_flag
& MODS_WEAK
)));
3063 if (mp
->mod_ref
== 0)
3064 mod_uninstall_ref_zero
++;
3067 cv_broadcast(&mod_cv
);
3069 mutex_exit(&mod_lock
);
3072 static struct modctl
*
3073 mod_hold_loaded_mod(struct modctl
*dep
, char *filename
, int *status
)
3075 struct modctl
*modp
;
3081 modp
= mod_hold_by_name_requisite(dep
, filename
);
3083 retval
= mod_load(modp
, 1);
3085 mod_release_mod(modp
);
3094 * if dep is not NULL, clear the module dependency information.
3095 * This information is set in mod_hold_by_name_common().
3097 if (dep
!= NULL
&& dep
->mod_requisite_loading
!= NULL
) {
3098 ASSERT(dep
->mod_busy
);
3099 dep
->mod_requisite_loading
= NULL
;
3106 * hold, load, and install the named module
3108 static struct modctl
*
3109 mod_hold_installed_mod(char *name
, int usepath
, int forcecheck
, int *r
)
3111 struct modctl
*modp
;
3115 * Verify that that module in question actually exists on disk
3116 * before allocation of module structure by mod_hold_by_name.
3118 if (modrootloaded
&& swaploaded
|| forcecheck
) {
3119 if (!kobj_path_exists(name
, usepath
)) {
3128 modp
= mod_hold_by_name(name
);
3130 retval
= mod_load(modp
, usepath
);
3132 mod_release_mod(modp
);
3136 if ((*r
= modinstall(modp
)) != 0) {
3138 * We loaded it, but failed to _init() it.
3139 * Be kind to developers -- force it
3140 * out of memory now so that the next
3141 * attempt to use the module will cause
3142 * a reload. See 1093793.
3145 mod_release_mod(modp
);
3155 static char mod_excl_msg
[] =
3156 "module %s(%s) is EXCLUDED and will not be loaded\n";
3157 static char mod_init_msg
[] = "loadmodule:%s(%s): _init() error %d\n";
3160 * This routine is needed for dependencies. Users specify dependencies
3161 * by declaring a character array initialized to filenames of dependents.
3162 * So the code that handles dependents deals with filenames (and not
3163 * module names) because that's all it has. We load by filename and once
3164 * we've loaded a file we can get the module name.
3165 * Unfortunately there isn't a single unified filename/modulename namespace.
3168 * We allow the name being looked up to be prepended by an optional
3169 * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
3172 mod_find_by_filename(char *subdir
, char *filename
)
3177 ASSERT(!MUTEX_HELD(&mod_lock
));
3179 sublen
= strlen(subdir
);
3183 mutex_enter(&mod_lock
);
3187 char *mod_filename
= mp
->mod_filename
;
3189 if (strncmp(subdir
, mod_filename
, sublen
) == 0 &&
3190 mod_filename
[sublen
] == '/' &&
3191 strcmp(filename
, &mod_filename
[sublen
+ 1]) == 0) {
3192 mutex_exit(&mod_lock
);
3195 } else if (strcmp(filename
, mp
->mod_filename
) == 0) {
3196 mutex_exit(&mod_lock
);
3199 } while ((mp
= mp
->mod_next
) != &modules
);
3200 mutex_exit(&mod_lock
);
3205 * Check for circular dependencies. This is called from do_dependents()
3206 * in kobj.c. If we are the thread already loading this module, then
3207 * we're trying to load a dependent that we're already loading which
3208 * means the user specified circular dependencies.
3211 mod_circdep(struct modctl
*modp
)
3213 struct modctl
*rmod
;
3215 ASSERT(MUTEX_HELD(&mod_lock
));
3218 * Check the mod_inprogress_thread first.
3219 * mod_inprogress_thread is used in mod_hold_stub()
3220 * directly to improve performance.
3222 if (modp
->mod_inprogress_thread
== curthread
)
3226 * Check the module circular dependencies.
3228 for (rmod
= modp
; rmod
!= NULL
; rmod
= rmod
->mod_requisite_loading
) {
3230 * Check if there is a module circular dependency.
3232 if (rmod
->mod_requisite_loading
== modp
)
3239 mod_getinfo(struct modctl
*modp
, struct modinfo
*modinfop
)
3241 int (*func
)(struct modinfo
*);
3244 ASSERT(modp
->mod_busy
);
3246 /* primary modules don't do getinfo */
3250 func
= (int (*)(struct modinfo
*))kobj_lookup(modp
->mod_mp
, "_info");
3252 if (kobj_addrcheck(modp
->mod_mp
, (caddr_t
)func
)) {
3253 cmn_err(CE_WARN
, "_info() not defined properly in %s",
3254 modp
->mod_filename
);
3256 * The semantics of mod_info(9F) are that 0 is failure
3257 * and non-zero is success.
3261 retval
= (*func
)(modinfop
); /* call _info() function */
3263 if (moddebug
& MODDEBUG_USERDEBUG
)
3264 printf("Returned from _info, retval = %x\n", retval
);
3270 modadd(struct modctl
*mp
)
3272 ASSERT(MUTEX_HELD(&mod_lock
));
3274 mp
->mod_id
= last_module_id
++;
3275 mp
->mod_next
= &modules
;
3276 mp
->mod_prev
= modules
.mod_prev
;
3277 modules
.mod_prev
->mod_next
= mp
;
3278 modules
.mod_prev
= mp
;
3282 static struct modctl
*
3283 allocate_modp(const char *filename
, const char *modname
)
3287 mp
= kobj_zalloc(sizeof (*mp
), KM_SLEEP
);
3288 mp
->mod_modname
= kobj_zalloc(strlen(modname
) + 1, KM_SLEEP
);
3289 (void) strcpy(mp
->mod_modname
, modname
);
3294 * Get the value of a symbol. This is a wrapper routine that
3295 * calls kobj_getsymvalue(). kobj_getsymvalue() may go away but this
3296 * wrapper will prevent callers from noticing.
3299 modgetsymvalue(char *name
, int kernelonly
)
3301 return (kobj_getsymvalue(name
, kernelonly
));
3305 * Get the symbol nearest an address. This is a wrapper routine that
3306 * calls kobj_getsymname(). kobj_getsymname() may go away but this
3307 * wrapper will prevent callers from noticing.
3310 modgetsymname(uintptr_t value
, ulong_t
*offset
)
3312 return (kobj_getsymname(value
, offset
));
3316 * Lookup a symbol in a specified module. These are wrapper routines that
3317 * call kobj_lookup(). kobj_lookup() may go away but these wrappers will
3318 * prevent callers from noticing.
3321 modlookup(const char *modname
, const char *symname
)
3323 struct modctl
*modp
;
3326 if ((modp
= mod_hold_by_name(modname
)) == NULL
)
3328 val
= kobj_lookup(modp
->mod_mp
, symname
);
3329 mod_release_mod(modp
);
3334 modlookup_by_modctl(modctl_t
*modp
, const char *symname
)
3336 ASSERT(modp
->mod_ref
> 0 || modp
->mod_busy
);
3338 return (kobj_lookup(modp
->mod_mp
, symname
));
3342 * Ask the user for the name of the system file and the default path
3351 if ((fd
= kobj_open(systemfile
)) != -1L)
3356 /*CONSTANTCONDITION*/
3358 printf("Name of system file [%s]: ",
3359 systemfile
? systemfile
: "/dev/null");
3361 console_gets(s0
, sizeof (s0
));
3365 else if (strcmp(s0
, "/dev/null") == 0) {
3369 if ((fd
= kobj_open(s0
)) != -1L) {
3375 printf("can't find file %s\n", s0
);
3379 static char loading_msg
[] = "loading '%s' id %d\n";
3380 static char load_msg
[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
3383 * Common code for loading a module (but not installing it).
3384 * Handoff the task of module loading to a separate thread
3385 * with a large stack if possible, since this code may recurse a few times.
3386 * Return zero if there are no errors or an errno value.
3389 mod_load(struct modctl
*mp
, int usepath
)
3392 struct modinfo
*modinfop
= NULL
;
3395 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3396 ASSERT(mp
->mod_busy
);
3401 if (mod_sysctl(SYS_CHECK_EXCLUDE
, mp
->mod_modname
) != 0 ||
3402 mod_sysctl(SYS_CHECK_EXCLUDE
, mp
->mod_filename
) != 0) {
3403 if (moddebug
& MODDEBUG_LOADMSG
) {
3404 printf(mod_excl_msg
, mp
->mod_filename
,
3409 if (moddebug
& MODDEBUG_LOADMSG2
)
3410 printf(loading_msg
, mp
->mod_filename
, mp
->mod_id
);
3412 if (curthread
!= &t0
) {
3414 lt
.usepath
= usepath
;
3415 lt
.owner
= curthread
;
3416 sema_init(<
.sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3418 /* create thread to hand of call to */
3419 (void) thread_create(NULL
, DEFAULTSTKSZ
* 2,
3420 modload_thread
, <
, 0, &p0
, TS_RUN
, maxclsyspri
);
3422 /* wait for thread to complete kobj_load_module */
3425 sema_destroy(<
.sema
);
3428 retval
= kobj_load_module(mp
, usepath
);
3431 ASSERT(retval
== 0);
3434 if (moddebug
& MODDEBUG_LOADMSG
) {
3435 printf(load_msg
, mp
->mod_filename
, mp
->mod_id
,
3436 (void *)((struct module
*)mp
->mod_mp
)->text
,
3437 (void *)((struct module
*)mp
->mod_mp
)->data
,
3438 ((struct module
*)mp
->mod_mp
)->text_size
,
3439 ((struct module
*)mp
->mod_mp
)->data_size
);
3443 * XXX - There should be a better way to get this.
3445 modinfop
= kmem_zalloc(sizeof (struct modinfo
), KM_SLEEP
);
3446 modinfop
->mi_info
= MI_INFO_LINKAGE
;
3447 if (mod_getinfo(mp
, modinfop
) == 0)
3448 mp
->mod_linkage
= NULL
;
3450 mp
->mod_linkage
= (void *)modinfop
->mi_base
;
3451 ASSERT(mp
->mod_linkage
->ml_rev
== MODREV_1
);
3455 * DCS: bootstrapping code. If the driver is loaded
3456 * before root mount, it is assumed that the driver
3457 * may be used before mounting root. In order to
3458 * access mappings of global to local minor no.'s
3459 * during installation/open of the driver, we load
3460 * them into memory here while the BOP_interfaces
3463 if ((cluster_bootflags
& CLUSTER_BOOTED
) && !modrootloaded
) {
3464 retval
= clboot_modload(mp
);
3467 kmem_free(modinfop
, sizeof (struct modinfo
));
3468 (void) mod_sysctl(SYS_SET_MVAR
, (void *)mp
);
3469 retval
= install_stubs_by_name(mp
, mp
->mod_modname
);
3472 * Now that the module is loaded, we need to give DTrace
3473 * a chance to notify its providers. This is done via
3474 * the dtrace_modload function pointer.
3476 if (strcmp(mp
->mod_modname
, "dtrace") != 0) {
3477 struct modctl
*dmp
= mod_hold_by_name("dtrace");
3479 if (dmp
!= NULL
&& dtrace_modload
!= NULL
)
3480 (*dtrace_modload
)(mp
);
3482 mod_release_mod(dmp
);
3487 * If load failed then we need to release any requisites
3488 * that we had established.
3491 mod_release_requisites(mp
);
3493 if (moddebug
& MODDEBUG_ERRMSG
)
3494 printf("error loading '%s', error %d\n",
3495 mp
->mod_filename
, retval
);
3500 static char unload_msg
[] = "unloading %s, module id %d, loadcnt %d.\n";
3503 mod_unload(struct modctl
*mp
)
3505 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3506 ASSERT(mp
->mod_busy
);
3507 ASSERT((mp
->mod_loaded
&& (mp
->mod_installed
== 0)) &&
3508 ((mp
->mod_prim
== 0) && (mp
->mod_ref
>= 0)));
3510 if (moddebug
& MODDEBUG_LOADMSG
)
3511 printf(unload_msg
, mp
->mod_modname
,
3512 mp
->mod_id
, mp
->mod_loadcnt
);
3515 * If mod_ref is not zero, it means some modules might still refer
3516 * to this module. Then you can't unload this module right now.
3517 * Instead, set 1 to mod_delay_unload to notify the system of
3518 * unloading this module later when it's not required any more.
3520 if (mp
->mod_ref
> 0) {
3521 mp
->mod_delay_unload
= 1;
3522 if (moddebug
& MODDEBUG_LOADMSG2
) {
3523 printf("module %s not unloaded,"
3524 " non-zero reference count (%d)",
3525 mp
->mod_modname
, mp
->mod_ref
);
3530 if (((mp
->mod_loaded
== 0) || mp
->mod_installed
) ||
3531 (mp
->mod_ref
|| mp
->mod_prim
)) {
3533 * A DEBUG kernel would ASSERT panic above, the code is broken
3534 * if we get this warning.
3536 cmn_err(CE_WARN
, "mod_unload: %s in incorrect state: %d %d %d",
3537 mp
->mod_filename
, mp
->mod_installed
, mp
->mod_loaded
,
3542 /* reset stub functions to call the binder again */
3546 * mark module as unloaded before the modctl structure is freed.
3547 * This is required not to reuse the modctl structure before
3548 * the module is marked as unloaded.
3551 mp
->mod_linkage
= NULL
;
3553 /* free the memory */
3554 kobj_unload_module(mp
);
3556 if (mp
->mod_delay_unload
) {
3557 mp
->mod_delay_unload
= 0;
3558 if (moddebug
& MODDEBUG_LOADMSG2
) {
3559 printf("deferred unload of module %s"
3560 " (id %d) successful",
3561 mp
->mod_modname
, mp
->mod_id
);
3565 /* release hold on requisites */
3566 mod_release_requisites(mp
);
3569 * Now that the module is gone, we need to give DTrace a chance to
3570 * remove any probes that it may have had in the module. This is
3571 * done via the dtrace_modunload function pointer.
3573 if (strcmp(mp
->mod_modname
, "dtrace") != 0) {
3574 struct modctl
*dmp
= mod_hold_by_name("dtrace");
3576 if (dmp
!= NULL
&& dtrace_modunload
!= NULL
)
3577 (*dtrace_modunload
)(mp
);
3579 mod_release_mod(dmp
);
3584 modinstall(struct modctl
*mp
)
3589 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3590 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
);
3592 if (mp
->mod_installed
)
3595 * If mod_delay_unload is on, it means the system chose the deferred
3596 * unload for this module. Then you can't install this module until
3597 * it's unloaded from the system.
3599 if (mp
->mod_delay_unload
)
3602 if (moddebug
& MODDEBUG_LOADMSG
)
3603 printf("installing %s, module id %d.\n",
3604 mp
->mod_modname
, mp
->mod_id
);
3606 ASSERT(mp
->mod_mp
!= NULL
);
3607 if (mod_install_requisites(mp
) != 0) {
3609 * Note that we can't call mod_unload(mp) here since
3610 * if modinstall() was called by mod_install_requisites(),
3611 * we won't be able to hold the dependent modules
3612 * (otherwise there would be a deadlock).
3617 if (moddebug
& MODDEBUG_ERRMSG
) {
3618 printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
3619 mp
->mod_filename
, mp
->mod_id
,
3620 (void *)((struct module
*)mp
->mod_mp
)->text
,
3621 (void *)((struct module
*)mp
->mod_mp
)->data
,
3622 ((struct module
*)mp
->mod_mp
)->text_size
,
3623 ((struct module
*)mp
->mod_mp
)->data_size
);
3626 func
= (int (*)())kobj_lookup(mp
->mod_mp
, "_init");
3628 if (kobj_addrcheck(mp
->mod_mp
, (caddr_t
)func
)) {
3629 cmn_err(CE_WARN
, "_init() not defined properly in %s",
3634 if (moddebug
& MODDEBUG_USERDEBUG
) {
3635 printf("breakpoint before calling %s:_init()\n",
3637 if (DEBUGGER_PRESENT
)
3638 debug_enter("_init");
3641 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3642 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
);
3643 val
= (*func
)(); /* call _init */
3645 if (moddebug
& MODDEBUG_USERDEBUG
)
3646 printf("Returned from _init, val = %x\n", val
);
3650 * Set the MODS_INSTALLED flag to enable this module
3654 mp
->mod_installed
= 1;
3655 } else if (moddebug
& MODDEBUG_ERRMSG
)
3656 printf(mod_init_msg
, mp
->mod_filename
, mp
->mod_modname
, val
);
3661 int detach_driver_unconfig
= 0;
3664 detach_driver(char *name
)
3670 * If being called from mod_uninstall_all() then the appropriate
3671 * driver detaches (leaf only) have already been done.
3673 if (mod_in_autounload())
3676 major
= ddi_name_to_major(name
);
3677 if (major
== DDI_MAJOR_T_NONE
)
3680 error
= ndi_devi_unconfig_driver(ddi_root_node(),
3681 NDI_DETACH_DRIVER
| detach_driver_unconfig
, major
);
3682 return (error
== NDI_SUCCESS
? 0 : -1);
3685 static char finiret_msg
[] = "Returned from _fini for %s, status = %x\n";
3688 moduninstall(struct modctl
*mp
)
3693 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3694 ASSERT(mp
->mod_busy
);
3697 * Verify that we need to do something and can uninstall the module.
3699 * If we should not uninstall the module or if the module is not in
3700 * the correct state to start an uninstall we return EBUSY to prevent
3701 * us from progressing to mod_unload. If the module has already been
3702 * uninstalled and unloaded we return EALREADY.
3704 if (mp
->mod_prim
|| mp
->mod_ref
|| mp
->mod_nenabled
!= 0)
3706 if ((mp
->mod_installed
== 0) || (mp
->mod_loaded
== 0))
3710 * To avoid devinfo / module deadlock we must release this module
3711 * prior to initiating the detach_driver, otherwise the detach_driver
3712 * might deadlock on a devinfo node held by another thread
3713 * coming top down and involving the module we have locked.
3715 * When we regrab the module we must reverify that it is OK
3716 * to proceed with the uninstall operation.
3718 mod_release_mod(mp
);
3719 status
= detach_driver(mp
->mod_modname
);
3720 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
3722 /* check detach status and reverify state with lock */
3723 mutex_enter(&mod_lock
);
3724 if ((status
!= 0) || mp
->mod_prim
|| mp
->mod_ref
) {
3725 mutex_exit(&mod_lock
);
3728 if ((mp
->mod_installed
== 0) || (mp
->mod_loaded
== 0)) {
3729 mutex_exit(&mod_lock
);
3732 mutex_exit(&mod_lock
);
3734 if (moddebug
& MODDEBUG_LOADMSG2
)
3735 printf("uninstalling %s\n", mp
->mod_modname
);
3738 * lookup _fini, return EBUSY if not defined.
3740 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
3741 * detach(9E) - it allows bufctl addresses to be resolved.
3743 func
= (int (*)())kobj_lookup(mp
->mod_mp
, "_fini");
3744 if ((func
== NULL
) || (mp
->mod_loadflags
& MOD_NOUNLOAD
) ||
3745 (moddebug
& MODDEBUG_FINI_EBUSY
))
3748 /* verify that _fini is in this module */
3749 if (kobj_addrcheck(mp
->mod_mp
, (caddr_t
)func
)) {
3750 cmn_err(CE_WARN
, "_fini() not defined properly in %s",
3756 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3757 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
&& mp
->mod_installed
);
3762 /* _fini returned success, the module is no longer installed */
3763 if (moddebug
& MODDEBUG_LOADMSG
)
3764 printf("uninstalled %s\n", mp
->mod_modname
);
3767 * Even though we only set mod_installed to zero here, a zero
3768 * return value means we are committed to a code path were
3769 * mod_loaded will also end up as zero - we have no other
3770 * way to get the module data and bss back to the pre _init
3771 * state except a reload. To ensure this, after return,
3772 * mod_busy must stay set until mod_loaded is cleared.
3774 mp
->mod_installed
= 0;
3777 * Clear the MODS_INSTALLED flag not to call functions
3778 * in the module directly from now on.
3780 uninstall_stubs(mp
);
3782 if (moddebug
& MODDEBUG_USERDEBUG
)
3783 printf(finiret_msg
, mp
->mod_filename
, status
);
3785 * By definition _fini is only allowed to return EBUSY or the
3786 * result of mod_remove (EBUSY or EINVAL). In the off chance
3787 * that a driver returns EALREADY we convert this to EINVAL
3788 * since to our caller EALREADY means module was already
3791 if (status
== EALREADY
)
3799 * Uninstall all modules.
3802 mod_uninstall_all(void)
3808 /* synchronize with any active modunload_disable() */
3811 /* mark this thread as doing autounloading */
3812 (void) tsd_set(mod_autounload_key
, (void *)1);
3814 (void) devfs_clean(ddi_root_node(), NULL
, 0);
3815 (void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH
);
3818 * Loop up to max times if we keep producing unreferenced modules.
3819 * A new unreferenced module is an opportunity to unload.
3821 for (pass
= 0; pass
< mod_uninstall_pass_max
; pass
++) {
3823 /* zero count of modules that go unreferenced during pass */
3824 mod_uninstall_ref_zero
= 0;
3827 while ((mp
= mod_hold_next_by_id(modid
)) != NULL
) {
3831 * Skip modules with the MOD_NOAUTOUNLOAD flag set
3833 if (mp
->mod_loadflags
& MOD_NOAUTOUNLOAD
) {
3834 mod_release_mod(mp
);
3838 if (moduninstall(mp
) == 0) {
3840 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
3842 mod_release_mod(mp
);
3845 /* break if no modules went unreferenced during pass */
3846 if (mod_uninstall_ref_zero
== 0)
3849 if (pass
>= mod_uninstall_pass_max
)
3850 mod_uninstall_pass_exc
++;
3852 (void) tsd_set(mod_autounload_key
, NULL
);
3856 /* wait for unloads that have begun before registering disable */
3858 modunload_disable(void)
3860 mutex_enter(&modunload_wait_mutex
);
3861 while (modunload_active_count
) {
3863 cv_wait(&modunload_wait_cv
, &modunload_wait_mutex
);
3866 modunload_disable_count
++;
3867 mutex_exit(&modunload_wait_mutex
);
3870 /* mark end of disable and signal waiters */
3872 modunload_enable(void)
3874 mutex_enter(&modunload_wait_mutex
);
3875 modunload_disable_count
--;
3876 if ((modunload_disable_count
== 0) && modunload_wait
)
3877 cv_broadcast(&modunload_wait_cv
);
3878 mutex_exit(&modunload_wait_mutex
);
3881 /* wait for disables to complete before begining unload */
3885 mutex_enter(&modunload_wait_mutex
);
3886 while (modunload_disable_count
) {
3888 cv_wait(&modunload_wait_cv
, &modunload_wait_mutex
);
3891 modunload_active_count
++;
3892 mutex_exit(&modunload_wait_mutex
);
3895 /* mark end of unload and signal waiters */
3899 mutex_enter(&modunload_wait_mutex
);
3900 modunload_active_count
--;
3901 if ((modunload_active_count
== 0) && modunload_wait
)
3902 cv_broadcast(&modunload_wait_cv
);
3903 mutex_exit(&modunload_wait_mutex
);
3907 mod_uninstall_daemon(void)
3909 callb_cpr_t cprinfo
;
3912 mod_aul_thread
= curthread
;
3914 CALLB_CPR_INIT(&cprinfo
, &mod_uninstall_lock
, callb_generic_cpr
, "mud");
3916 mutex_enter(&mod_uninstall_lock
);
3917 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
3919 * In DEBUG kernels, unheld drivers are uninstalled periodically
3920 * every mod_uninstall_interval seconds. Periodic uninstall can
3921 * be disabled by setting mod_uninstall_interval to 0 which is
3922 * the default for a non-DEBUG kernel.
3924 if (mod_uninstall_interval
) {
3925 ticks
= drv_usectohz(mod_uninstall_interval
* 1000000);
3926 (void) cv_reltimedwait(&mod_uninstall_cv
,
3927 &mod_uninstall_lock
, ticks
, TR_CLOCK_TICK
);
3929 cv_wait(&mod_uninstall_cv
, &mod_uninstall_lock
);
3932 * The whole daemon is safe for CPR except we don't want
3933 * the daemon to run if FREEZE is issued and this daemon
3934 * wakes up from the cv_wait above. In this case, it'll be
3935 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
3937 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
3938 * mod_uninstall_lock is used to protect cprinfo and
3939 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
3942 CALLB_CPR_SAFE_END(&cprinfo
, &mod_uninstall_lock
);
3943 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
3944 mutex_exit(&mod_uninstall_lock
);
3945 if ((modunload_disable_count
== 0) &&
3946 ((moddebug
& MODDEBUG_NOAUTOUNLOAD
) == 0)) {
3947 mod_uninstall_all();
3953 * Unload all uninstalled modules.
3958 mutex_enter(&mod_uninstall_lock
);
3959 cv_broadcast(&mod_uninstall_cv
);
3960 mutex_exit(&mod_uninstall_lock
);
3964 * Hold the specified module. This is the module holding primitive.
3966 * If MOD_LOCK_HELD then the caller already holds the mod_lock.
3969 * 0 ==> the module is held
3970 * 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
3971 * to determine how to retry.
3974 mod_hold_by_modctl(struct modctl
*mp
, int f
)
3976 ASSERT((f
& (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)) &&
3977 ((f
& (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)) !=
3978 (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)));
3979 ASSERT((f
& (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)) &&
3980 ((f
& (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)) !=
3981 (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)));
3982 ASSERT((f
& MOD_LOCK_NOT_HELD
) || MUTEX_HELD(&mod_lock
));
3984 if (f
& MOD_LOCK_NOT_HELD
)
3985 mutex_enter(&mod_lock
);
3987 while (mp
->mod_busy
) {
3989 cv_wait(&mod_cv
, &mod_lock
);
3991 * Module may be unloaded by daemon.
3992 * Nevertheless, modctl structure is still in linked list
3993 * (i.e., off &modules), not freed!
3994 * Caller is not supposed to assume "mp" is valid, but there
3995 * is no reasonable way to detect this but using
3996 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
3997 * (or similar check depending on calling context)
3998 * DON'T free modctl structure, it will be very very
4001 if (f
& MOD_WAIT_ONCE
) {
4002 if (f
& MOD_LOCK_NOT_HELD
)
4003 mutex_exit(&mod_lock
);
4004 return (1); /* caller decides how to retry */
4009 mp
->mod_inprogress_thread
=
4010 (curthread
== NULL
? (kthread_id_t
)-1 : curthread
);
4012 if (f
& MOD_LOCK_NOT_HELD
)
4013 mutex_exit(&mod_lock
);
4017 static struct modctl
*
4018 mod_hold_by_name_common(struct modctl
*dep
, const char *filename
)
4020 const char *modname
;
4022 char *curname
, *newname
;
4025 mutex_enter(&mod_lock
);
4027 if ((modname
= strrchr(filename
, '/')) == NULL
)
4034 if (strcmp(modname
, mp
->mod_modname
) == 0) {
4038 } while ((mp
= mp
->mod_next
) != &modules
);
4041 mp
= allocate_modp(filename
, modname
);
4046 * if dep is not NULL, set the mp in mod_requisite_loading for
4047 * the module circular dependency check. This field is used in
4048 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
4051 ASSERT(dep
->mod_busy
&& dep
->mod_requisite_loading
== NULL
);
4052 dep
->mod_requisite_loading
= mp
;
4056 * If the module was held, then it must be us who has it held.
4058 if (mod_circdep(mp
))
4061 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4064 * If the name hadn't been set or has changed, allocate
4065 * space and set it. Free space used by previous name.
4067 * Do not change the name of primary modules, for primary
4068 * modules the mod_filename was allocated in standalone mode:
4069 * it is illegal to kobj_alloc in standalone mode and kobj_free
4070 * in non-standalone mode.
4072 curname
= mp
->mod_filename
;
4073 if (curname
== NULL
||
4074 ((mp
->mod_prim
== 0) &&
4075 (curname
!= filename
) &&
4076 (modname
!= filename
) &&
4077 (strcmp(curname
, filename
) != 0))) {
4078 newname
= kobj_zalloc(strlen(filename
) + 1, KM_SLEEP
);
4079 (void) strcpy(newname
, filename
);
4080 mp
->mod_filename
= newname
;
4081 if (curname
!= NULL
)
4082 kobj_free(curname
, strlen(curname
) + 1);
4086 mutex_exit(&mod_lock
);
4087 if (mp
&& moddebug
& MODDEBUG_LOADMSG2
)
4088 printf("Holding %s\n", mp
->mod_filename
);
4089 if (mp
== NULL
&& moddebug
& MODDEBUG_LOADMSG2
)
4090 printf("circular dependency loading %s\n", filename
);
4094 static struct modctl
*
4095 mod_hold_by_name_requisite(struct modctl
*dep
, char *filename
)
4097 return (mod_hold_by_name_common(dep
, filename
));
4101 mod_hold_by_name(const char *filename
)
4103 return (mod_hold_by_name_common(NULL
, filename
));
4107 mod_hold_by_id(modid_t modid
)
4112 mutex_enter(&mod_lock
);
4115 if (mp
->mod_id
== modid
) {
4119 } while ((mp
= mp
->mod_next
) != &modules
);
4121 if ((found
== 0) || mod_circdep(mp
))
4124 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4126 mutex_exit(&mod_lock
);
4130 static struct modctl
*
4131 mod_hold_next_by_id(modid_t modid
)
4139 mutex_enter(&mod_lock
);
4143 if (mp
->mod_id
> modid
) {
4147 } while ((mp
= mp
->mod_next
) != &modules
);
4149 if ((found
== 0) || mod_circdep(mp
))
4152 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4154 mutex_exit(&mod_lock
);
4159 mod_release(struct modctl
*mp
)
4161 ASSERT(MUTEX_HELD(&mod_lock
));
4162 ASSERT(mp
->mod_busy
);
4165 mp
->mod_inprogress_thread
= NULL
;
4168 cv_broadcast(&mod_cv
);
4173 mod_release_mod(struct modctl
*mp
)
4175 if (moddebug
& MODDEBUG_LOADMSG2
)
4176 printf("Releasing %s\n", mp
->mod_filename
);
4177 mutex_enter(&mod_lock
);
4179 mutex_exit(&mod_lock
);
4183 mod_name_to_modid(char *filename
)
4188 mutex_enter(&mod_lock
);
4190 if ((modname
= strrchr(filename
, '/')) == NULL
)
4197 if (strcmp(modname
, mp
->mod_modname
) == 0) {
4198 mutex_exit(&mod_lock
);
4199 return (mp
->mod_id
);
4201 } while ((mp
= mp
->mod_next
) != &modules
);
4203 mutex_exit(&mod_lock
);
4209 mod_remove_by_name(char *name
)
4214 mp
= mod_hold_by_name(name
);
4219 if (mp
->mod_loadflags
& MOD_NOAUTOUNLOAD
) {
4221 * Do not unload forceloaded modules
4223 mod_release_mod(mp
);
4227 if ((retval
= moduninstall(mp
)) == 0) {
4229 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
4230 } else if (retval
== EALREADY
)
4231 retval
= 0; /* already unloaded, not an error */
4232 mod_release_mod(mp
);
4237 * Record that module "dep" is dependent on module "on_mod."
4240 mod_make_requisite(struct modctl
*dependent
, struct modctl
*on_mod
)
4242 struct modctl_list
**pmlnp
; /* previous next pointer */
4243 struct modctl_list
*mlp
;
4244 struct modctl_list
*new;
4246 ASSERT(dependent
->mod_busy
&& on_mod
->mod_busy
);
4247 mutex_enter(&mod_lock
);
4250 * Search dependent's requisite list to see if on_mod is recorded.
4251 * List is ordered by id.
4253 for (pmlnp
= &dependent
->mod_requisites
, mlp
= *pmlnp
;
4254 mlp
; pmlnp
= &mlp
->modl_next
, mlp
= *pmlnp
)
4255 if (mlp
->modl_modp
->mod_id
>= on_mod
->mod_id
)
4258 /* Create and insert if not already recorded */
4259 if ((mlp
== NULL
) || (mlp
->modl_modp
->mod_id
!= on_mod
->mod_id
)) {
4260 new = kobj_zalloc(sizeof (*new), KM_SLEEP
);
4261 new->modl_modp
= on_mod
;
4262 new->modl_next
= mlp
;
4266 * Increment the mod_ref count in our new requisite module.
4267 * This is what keeps a module that has other modules
4268 * which are dependent on it from being uninstalled and
4269 * unloaded. "on_mod"'s mod_ref count decremented in
4270 * mod_release_requisites when the "dependent" module
4271 * unload is complete. "on_mod" must be loaded, but may not
4275 ASSERT(on_mod
->mod_ref
&& on_mod
->mod_loaded
);
4278 mutex_exit(&mod_lock
);
4282 * release the hold associated with mod_make_requisite mod_ref++
4283 * as part of unload.
4286 mod_release_requisites(struct modctl
*modp
)
4288 struct modctl_list
*modl
;
4289 struct modctl_list
*next
;
4291 struct modctl_list
*start
= NULL
, *mod_garbage
;
4293 ASSERT(!quiesce_active
);
4294 ASSERT(modp
->mod_busy
);
4295 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
4297 mutex_enter(&mod_lock
); /* needed for manipulation of req */
4298 for (modl
= modp
->mod_requisites
; modl
; modl
= next
) {
4299 next
= modl
->modl_next
;
4300 req
= modl
->modl_modp
;
4301 ASSERT(req
->mod_ref
>= 1 && req
->mod_loaded
);
4303 if (req
->mod_ref
== 0)
4304 mod_uninstall_ref_zero
++;
4307 * Check if the module has to be unloaded or not.
4309 if (req
->mod_ref
== 0 && req
->mod_delay_unload
) {
4310 struct modctl_list
*new;
4312 * Allocate the modclt_list holding the garbage
4313 * module which should be unloaded later.
4315 new = kobj_zalloc(sizeof (struct modctl_list
),
4317 new->modl_modp
= req
;
4320 mod_garbage
= start
= new;
4322 mod_garbage
->modl_next
= new;
4327 /* free the list as we go */
4328 kobj_free(modl
, sizeof (*modl
));
4330 modp
->mod_requisites
= NULL
;
4331 mutex_exit(&mod_lock
);
4334 * Unload the garbage modules.
4336 for (mod_garbage
= start
; mod_garbage
!= NULL
; /* nothing */) {
4337 struct modctl_list
*old
= mod_garbage
;
4338 struct modctl
*mp
= mod_garbage
->modl_modp
;
4342 * Hold this module until it's unloaded completely.
4344 (void) mod_hold_by_modctl(mp
,
4345 MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
4347 * Check if the module is not unloaded yet and nobody requires
4348 * the module. If it's unloaded already or somebody still
4349 * requires the module, don't unload it now.
4351 if (mp
->mod_loaded
&& mp
->mod_ref
== 0)
4353 ASSERT((mp
->mod_loaded
== 0 && mp
->mod_delay_unload
== 0) ||
4355 mod_release_mod(mp
);
4357 mod_garbage
= mod_garbage
->modl_next
;
4358 kobj_free(old
, sizeof (struct modctl_list
));
4363 * Process dependency of the module represented by "dep" on the
4364 * module named by "on."
4366 * Called from kobj_do_dependents() to load a module "on" on which
4370 mod_load_requisite(struct modctl
*dep
, char *on
)
4372 struct modctl
*on_mod
;
4375 if ((on_mod
= mod_hold_loaded_mod(dep
, on
, &retval
)) != NULL
) {
4376 mod_make_requisite(dep
, on_mod
);
4377 } else if (moddebug
& MODDEBUG_ERRMSG
) {
4378 printf("error processing %s on which module %s depends\n",
4379 on
, dep
->mod_modname
);
4385 mod_install_requisites(struct modctl
*modp
)
4387 struct modctl_list
*modl
;
4391 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
4392 ASSERT(modp
->mod_busy
);
4394 for (modl
= modp
->mod_requisites
; modl
; modl
= modl
->modl_next
) {
4395 req
= modl
->modl_modp
;
4396 (void) mod_hold_by_modctl(req
,
4397 MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
4398 status
= modinstall(req
);
4399 mod_release_mod(req
);
4408 * returns 1 if this thread is doing autounload, 0 otherwise.
4409 * see mod_uninstall_all.
4414 return ((int)(uintptr_t)tsd_get(mod_autounload_key
));
4418 * gmatch adapted from libc, stripping the wchar stuff
4420 #define popchar(p, c) { \
4428 gmatch(const char *s
, const char *p
)
4431 int ok
, lc
, notflag
;
4436 return (sc
== c
); /* nothing matches nothing */
4440 /* skip to quoted character */
4445 /* straight comparison */
4451 /* first char matches, move to remainder */
4452 return (sc
!= '\0' ? gmatch(s
, p
) : 0);
4459 /* * matches everything */
4463 /* undo skip at the beginning & iterate over substrings */
4473 /* match any char within [] */
4477 ok
= lc
= notflag
= 0;
4486 if (c
== '-' && lc
&& *p
!= ']') {
4487 /* test sc against range [c1-c2] */
4494 /* return 0 on mismatch */
4495 if (lc
<= sc
&& sc
<= c
)
4498 } else if (lc
<= sc
&& sc
<= c
) {
4501 /* keep going, may get a match next */
4502 } else if (c
== '\\') {
4503 /* skip to quoted character */
4511 } else if (sc
== lc
) {
4517 /* recurse on remainder of string */
4518 return (ok
? gmatch(s
, p
) : 0);
4525 * Get default perm for device from /etc/minor_perm. Return 0 if match found.
4527 * Pure wild-carded patterns are handled separately so the ordering of
4528 * these patterns doesn't matter. We're still dependent on ordering
4529 * however as the first matching entry is the one returned.
4530 * Not ideal but all existing examples and usage do imply this
4531 * ordering implicitly.
4533 * Drivers using the clone driver are always good for some entertainment.
4534 * Clone nodes under pseudo have the form clone@0:<driver>. Some minor
4535 * perm entries have the form clone:<driver>, others use <driver>:*
4536 * Examples are clone:llc1 vs. llc2:*, for example.
4538 * Minor perms in the clone:<driver> form are mapped to the drivers's
4539 * mperm list, not the clone driver, as wildcard entries for clone
4540 * reference only. In other words, a clone wildcard will match
4541 * references for clone@0:<driver> but never <driver>@<minor>.
4543 * Additional minor perms in the standard form are also supported,
4544 * for mixed usage, ie a node with an entry clone:<driver> could
4545 * provide further entries <driver>:<minor>.
4547 * Finally, some uses of clone use an alias as the minor name rather
4548 * than the driver name, with the alias as the minor perm entry.
4549 * This case is handled by attaching the driver to bring its
4550 * minor list into existence, then discover the alias via DDI_ALIAS.
4551 * The clone device's minor perm list can then be searched for
4556 dev_alias_minorperm(dev_info_t
*dip
, char *minor_name
, mperm_t
*rmp
)
4559 struct devnames
*dnp
;
4564 struct ddi_minor_data
*dmd
;
4566 major
= ddi_name_to_major(minor_name
);
4568 ASSERT(dip
== clone_dip
);
4569 ASSERT(major
!= DDI_MAJOR_T_NONE
);
4572 * Attach the driver named by the minor node, then
4573 * search its first instance's minor list for an
4576 if (ddi_hold_installed_driver(major
) == NULL
)
4579 dnp
= &devnamesp
[major
];
4580 LOCK_DEV_OPS(&dnp
->dn_lock
);
4582 if ((cdevi
= dnp
->dn_head
) != NULL
) {
4583 ndi_devi_enter(cdevi
, &circ
);
4584 for (dmd
= DEVI(cdevi
)->devi_minor
; dmd
; dmd
= dmd
->next
) {
4585 if (dmd
->type
== DDM_ALIAS
) {
4586 alias
= i_ddi_strdup(dmd
->ddm_name
, KM_SLEEP
);
4590 ndi_devi_exit(cdevi
, circ
);
4593 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4594 ddi_rele_driver(major
);
4596 if (alias
== NULL
) {
4597 if (moddebug
& MODDEBUG_MINORPERM
)
4598 cmn_err(CE_CONT
, "dev_minorperm: "
4599 "no alias for %s\n", minor_name
);
4603 major
= ddi_driver_major(clone_dip
);
4604 dnp
= &devnamesp
[major
];
4605 LOCK_DEV_OPS(&dnp
->dn_lock
);
4608 * Go through the clone driver's mperm list looking
4609 * for a match for the specified alias.
4611 for (mp
= dnp
->dn_mperm
; mp
; mp
= mp
->mp_next
) {
4612 if (strcmp(alias
, mp
->mp_minorname
) == 0) {
4618 if (moddebug
& MODDEBUG_MP_MATCH
) {
4620 "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
4621 minor_name
, alias
, mp
->mp_mode
,
4622 mp
->mp_uid
, mp
->mp_gid
);
4624 rmp
->mp_uid
= mp
->mp_uid
;
4625 rmp
->mp_gid
= mp
->mp_gid
;
4626 rmp
->mp_mode
= mp
->mp_mode
;
4628 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4630 kmem_free(alias
, strlen(alias
)+1);
4632 return (mp
== NULL
);
4636 dev_minorperm(dev_info_t
*dip
, char *name
, mperm_t
*rmp
)
4640 struct devnames
*dnp
;
4644 if (!minorperm_loaded
) {
4645 if (moddebug
& MODDEBUG_MINORPERM
)
4647 "%s: minor perm not yet loaded\n", name
);
4651 minor_name
= strchr(name
, ':');
4652 if (minor_name
== NULL
)
4657 * If it's the clone driver, search the driver as named
4658 * by the minor. All clone minor perm entries other than
4659 * alias nodes are actually installed on the real driver's list.
4661 if (dip
== clone_dip
) {
4662 major
= ddi_name_to_major(minor_name
);
4663 if (major
== DDI_MAJOR_T_NONE
) {
4664 if (moddebug
& MODDEBUG_MINORPERM
)
4665 cmn_err(CE_CONT
, "dev_minorperm: "
4666 "%s: no such driver\n", minor_name
);
4671 major
= ddi_driver_major(dip
);
4672 ASSERT(major
!= DDI_MAJOR_T_NONE
);
4675 dnp
= &devnamesp
[major
];
4676 LOCK_DEV_OPS(&dnp
->dn_lock
);
4679 * Go through the driver's mperm list looking for
4680 * a match for the specified minor. If there's
4681 * no matching pattern, use the wild card.
4682 * Defer to the clone wild for clone if specified,
4683 * otherwise fall back to the normal form.
4685 for (mp
= dnp
->dn_mperm
; mp
; mp
= mp
->mp_next
) {
4686 if (gmatch(minor_name
, mp
->mp_minorname
) != 0) {
4692 mp
= dnp
->dn_mperm_clone
;
4694 mp
= dnp
->dn_mperm_wild
;
4698 if (moddebug
& MODDEBUG_MP_MATCH
) {
4700 "minor perm defaults: %s %s 0%o %d %d\n",
4701 name
, mp
->mp_minorname
, mp
->mp_mode
,
4702 mp
->mp_uid
, mp
->mp_gid
);
4704 rmp
->mp_uid
= mp
->mp_uid
;
4705 rmp
->mp_gid
= mp
->mp_gid
;
4706 rmp
->mp_mode
= mp
->mp_mode
;
4708 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4711 * If no match can be found for a clone node,
4712 * search for a possible match for an alias.
4713 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
4714 * with minor perm entry clone:ptmx.
4716 if (mp
== NULL
&& is_clone
) {
4717 return (dev_alias_minorperm(dip
, minor_name
, rmp
));
4720 return (mp
== NULL
);
4724 * dynamicaly reference load a dl module/library, returning handle
4728 ddi_modopen(const char *modname
, int mode
, int *errnop
)
4733 struct modctl
*hmodp
= NULL
;
4734 int retval
= EINVAL
;
4736 ASSERT(modname
&& (mode
== KRTLD_MODE_FIRST
));
4737 if ((modname
== NULL
) || (mode
!= KRTLD_MODE_FIRST
))
4740 /* find last '/' in modname */
4741 mod
= strrchr(modname
, '/');
4744 /* for subdir string without modification to argument */
4746 subdirlen
= mod
- modname
;
4747 subdir
= kmem_alloc(subdirlen
, KM_SLEEP
);
4748 (void) strlcpy(subdir
, modname
, subdirlen
);
4752 mod
= (char *)modname
;
4755 /* reference load with errno return value */
4756 retval
= modrload(subdir
, mod
, &hmodp
);
4759 kmem_free(subdir
, subdirlen
);
4764 if (moddebug
& MODDEBUG_DDI_MOD
)
4765 printf("ddi_modopen %s mode %x: %s %p %d\n",
4766 modname
? modname
: "<unknown>", mode
,
4767 hmodp
? hmodp
->mod_filename
: "<unknown>",
4768 (void *)hmodp
, retval
);
4770 return ((ddi_modhandle_t
)hmodp
);
4773 /* lookup "name" in open dl module/library */
4775 ddi_modsym(ddi_modhandle_t h
, const char *name
, int *errnop
)
4777 struct modctl
*hmodp
= (struct modctl
*)h
;
4781 ASSERT(hmodp
&& name
&& hmodp
->mod_installed
&& (hmodp
->mod_ref
>= 1));
4782 if ((hmodp
== NULL
) || (name
== NULL
) ||
4783 (hmodp
->mod_installed
== 0) || (hmodp
->mod_ref
< 1)) {
4787 f
= (void *)kobj_lookup(hmodp
->mod_mp
, (char *)name
);
4794 if (moddebug
& MODDEBUG_DDI_MOD
)
4795 printf("ddi_modsym in %s of %s: %d %p\n",
4796 hmodp
? hmodp
->mod_modname
: "<unknown>",
4797 name
? name
: "<unknown>", retval
, f
);
4804 /* dynamic (un)reference unload of an open dl module/library */
4806 ddi_modclose(ddi_modhandle_t h
)
4808 struct modctl
*hmodp
= (struct modctl
*)h
;
4809 struct modctl
*modp
= NULL
;
4812 ASSERT(hmodp
&& hmodp
->mod_installed
&& (hmodp
->mod_ref
>= 1));
4813 if ((hmodp
== NULL
) ||
4814 (hmodp
->mod_installed
== 0) || (hmodp
->mod_ref
< 1)) {
4819 retval
= modunrload(hmodp
->mod_id
, &modp
, ddi_modclose_unload
);
4820 if (retval
== EBUSY
)
4821 retval
= 0; /* EBUSY is not an error */
4824 ASSERT(hmodp
== modp
);
4829 out
: if (moddebug
& MODDEBUG_DDI_MOD
)
4830 printf("ddi_modclose %s: %d\n",
4831 hmodp
? hmodp
->mod_modname
: "<unknown>", retval
);