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/dtrace.h>
53 #include <sys/devpolicy.h>
54 #include <sys/modctl.h>
56 #include <sys/devops.h>
57 #include <sys/autoconf.h>
58 #include <sys/hwconf.h>
59 #include <sys/callb.h>
60 #include <sys/debug.h>
61 #include <sys/cpuvar.h>
62 #include <sys/sysmacros.h>
63 #include <sys/sysevent.h>
64 #include <sys/sysevent_impl.h>
65 #include <sys/instance.h>
66 #include <sys/modhash.h>
67 #include <sys/modhash_impl.h>
68 #include <sys/dacf_impl.h>
70 #include <sys/pathname.h>
71 #include <sys/console.h>
72 #include <sys/policy.h>
73 #include <ipp/ipp_impl.h>
74 #include <sys/fs/dv_node.h>
75 #include <sys/strsubr.h>
76 #include <sys/fs/sdev_impl.h>
78 static int mod_circdep(struct modctl
*);
79 static int modinfo(modid_t
, struct modinfo
*);
81 static void mod_uninstall_all(void);
82 static int mod_getinfo(struct modctl
*, struct modinfo
*);
83 static struct modctl
*allocate_modp(const char *, const char *);
85 static int mod_load(struct modctl
*, int);
86 static void mod_unload(struct modctl
*);
87 static int modinstall(struct modctl
*);
88 static int moduninstall(struct modctl
*);
90 static struct modctl
*mod_hold_by_name_common(struct modctl
*, const char *);
91 static struct modctl
*mod_hold_next_by_id(modid_t
);
92 static struct modctl
*mod_hold_loaded_mod(struct modctl
*, char *, int *);
93 static struct modctl
*mod_hold_installed_mod(char *, int, int, int *);
95 static void mod_release(struct modctl
*);
96 static void mod_make_requisite(struct modctl
*, struct modctl
*);
97 static int mod_install_requisites(struct modctl
*);
98 static void check_esc_sequences(char *, char *);
99 static struct modctl
*mod_hold_by_name_requisite(struct modctl
*, char *);
102 * module loading thread control structure. Calls to kobj_load_module()() are
103 * handled off to a separate thead using this structure.
113 static void modload_thread(struct loadmt
*);
116 kcondvar_t mod_uninstall_cv
; /* Communication between swapper */
117 /* and the uninstall daemon. */
118 kmutex_t mod_lock
; /* protects &modules insert linkage, */
119 /* mod_busy, mod_want, and mod_ref. */
120 /* blocking operations while holding */
121 /* mod_lock should be avoided */
122 kmutex_t mod_uninstall_lock
; /* protects mod_uninstall_cv */
123 kthread_id_t mod_aul_thread
;
126 kmutex_t modunload_wait_mutex
;
127 kcondvar_t modunload_wait_cv
;
128 int modunload_active_count
;
129 int modunload_disable_count
;
131 int isminiroot
; /* set if running as miniroot */
132 int modrootloaded
; /* set after root driver and fs are loaded */
133 int moddebug
= 0x0; /* debug flags for module writers */
134 int swaploaded
; /* set after swap driver and fs are loaded */
135 int bop_io_quiesced
= 0; /* set when BOP I/O can no longer be used */
137 clock_t mod_uninstall_interval
= 0;
138 int mod_uninstall_pass_max
= 6;
139 int mod_uninstall_ref_zero
; /* # modules that went mod_ref == 0 */
140 int mod_uninstall_pass_exc
; /* mod_uninstall_all left new stuff */
142 int ddi_modclose_unload
= 1; /* 0 -> just decrement reference */
144 int devcnt_incr
= 256; /* allow for additional drivers */
145 int devcnt_min
= 512; /* and always at least this number */
147 struct devnames
*devnamesp
;
148 struct devnames orphanlist
;
150 krwlock_t devinfo_tree_lock
; /* obsolete, to be removed */
152 #define MAJBINDFILE "/etc/name_to_major"
153 #define SYSBINDFILE "/etc/name_to_sysnum"
155 static char majbind
[] = MAJBINDFILE
;
156 static char sysbind
[] = SYSBINDFILE
;
157 static uint_t mod_autounload_key
; /* for module autounload detection */
161 #define DEBUGGER_PRESENT ((boothowto & RB_DEBUG) || (obpdebug != 0))
163 static int minorperm_loaded
= 0;
168 struct sysent
*callp
;
169 int callnum
, exectype
;
174 * Initialize the list of loaded driver dev_ops.
175 * XXX - This must be done before reading the system file so that
176 * forceloads of drivers will work.
178 num_devs
= read_binding_file(majbind
, mb_hashtab
, make_mbind
);
180 * Since read_binding_file is common code, it doesn't enforce that all
181 * of the binding file entries have major numbers <= MAXMAJ32. Thus,
182 * ensure that we don't allocate some massive amount of space due to a
183 * bad entry. We can't have major numbers bigger than MAXMAJ32
184 * until file system support for larger major numbers exists.
188 * Leave space for expansion, but not more than L_MAXMAJ32
190 devcnt
= MIN(num_devs
+ devcnt_incr
, L_MAXMAJ32
);
191 devcnt
= MAX(devcnt
, devcnt_min
);
192 devopsp
= kmem_alloc(devcnt
* sizeof (struct dev_ops
*), KM_SLEEP
);
193 for (i
= 0; i
< devcnt
; i
++)
194 devopsp
[i
] = &mod_nodev_ops
;
196 init_devnamesp(devcnt
);
199 * Sync up with the work that the stand-alone linker has already done.
203 if (boothowto
& RB_DEBUG
)
206 make_aliases(mb_hashtab
);
209 * Initialize streams device implementation structures.
211 devimpl
= kmem_zalloc(devcnt
* sizeof (cdevsw_impl_t
), KM_SLEEP
);
213 (void) read_binding_file(sysbind
, sb_hashtab
, make_mbind
);
214 init_syscallnames(NSYSCALL
);
217 * Start up dynamic autoconfiguration framework (dacf).
223 * Start up IP policy framework (ipp).
228 * Allocate loadable native system call locks.
230 for (callnum
= 0, callp
= sysent
; callnum
< NSYSCALL
;
231 callnum
++, callp
++) {
232 if (LOADABLE_SYSCALL(callp
)) {
233 if (mod_getsysname(callnum
) != NULL
) {
235 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
236 rw_init(callp
->sy_lock
, NULL
, RW_DEFAULT
, NULL
);
238 callp
->sy_flags
&= ~SE_LOADABLE
;
239 callp
->sy_callc
= nosys
;
244 * Do some sanity checks on the sysent table
246 switch (callp
->sy_flags
& SE_RVAL_MASK
) {
248 /* only r_val1 returned */
249 case SE_32RVAL1
| SE_32RVAL2
:
250 /* r_val1 and r_val2 returned */
252 /* 64-bit rval returned */
255 cmn_err(CE_WARN
, "sysent[%d]: bad flags %x",
256 callnum
, callp
->sy_flags
);
262 #ifdef _SYSCALL32_IMPL
264 * Allocate loadable system call locks for 32-bit compat syscalls
266 for (callnum
= 0, callp
= sysent32
; callnum
< NSYSCALL
;
267 callnum
++, callp
++) {
268 if (LOADABLE_SYSCALL(callp
)) {
269 if (mod_getsysname(callnum
) != NULL
) {
271 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
272 rw_init(callp
->sy_lock
, NULL
, RW_DEFAULT
, NULL
);
274 callp
->sy_flags
&= ~SE_LOADABLE
;
275 callp
->sy_callc
= nosys
;
280 * Do some sanity checks on the sysent table
282 switch (callp
->sy_flags
& SE_RVAL_MASK
) {
284 /* only r_val1 returned */
285 case SE_32RVAL1
| SE_32RVAL2
:
286 /* r_val1 and r_val2 returned */
288 /* 64-bit rval returned */
291 cmn_err(CE_WARN
, "sysent32[%d]: bad flags %x",
292 callnum
, callp
->sy_flags
);
297 * Cross-check the native and compatibility tables.
299 if (callp
->sy_callc
== nosys
||
300 sysent
[callnum
].sy_callc
== nosys
)
303 * If only one or the other slot is loadable, then
304 * there's an error -- they should match!
306 if ((callp
->sy_callc
== loadable_syscall
) ^
307 (sysent
[callnum
].sy_callc
== loadable_syscall
)) {
308 cmn_err(CE_WARN
, "sysent[%d] loadable?",
312 * This is more of a heuristic test -- if the
313 * system call returns two values in the 32-bit
314 * world, it should probably return two 32-bit
315 * values in the 64-bit world too.
317 if (((callp
->sy_flags
& SE_32RVAL2
) == 0) ^
318 ((sysent
[callnum
].sy_flags
& SE_32RVAL2
) == 0)) {
319 cmn_err(CE_WARN
, "sysent[%d] rval2 mismatch!",
326 #endif /* _SYSCALL32_IMPL */
329 * Allocate loadable exec locks. (Assumes all execs are loadable)
331 for (exectype
= 0; exectype
< nexectype
; exectype
++) {
332 execsw
[exectype
].exec_lock
=
333 kobj_zalloc(sizeof (krwlock_t
), KM_SLEEP
);
334 rw_init(execsw
[exectype
].exec_lock
, NULL
, RW_DEFAULT
, NULL
);
339 /* init thread specific structure for mod_uninstall_all */
340 tsd_create(&mod_autounload_key
, NULL
);
344 modctl_modload(int use_path
, char *filename
, int *rvp
)
351 filenamep
= kmem_zalloc(MOD_MAXPATH
, KM_SLEEP
);
353 if (copyinstr(filename
, filenamep
, MOD_MAXPATH
, 0)) {
358 filenamep
[MOD_MAXPATH
- 1] = 0;
359 modp
= mod_hold_installed_mod(filenamep
, use_path
, 0, &retval
);
364 modp
->mod_loadflags
|= MOD_NOAUTOUNLOAD
;
365 modid
= modp
->mod_id
;
366 mod_release_mod(modp
);
367 CPU_STATS_ADDQ(CPU
, sys
, modload
, 1);
368 if (rvp
!= NULL
&& copyout(&modid
, rvp
, sizeof (modid
)) != 0)
371 kmem_free(filenamep
, MOD_MAXPATH
);
377 modctl_modunload(modid_t id
)
384 * Turn on mod_uninstall_daemon
386 if (mod_uninstall_interval
== 0) {
387 mod_uninstall_interval
= 60;
394 rval
= modunload(id
);
400 modctl_modinfo(modid_t id
, struct modinfo
*umodi
)
404 #if defined(_SYSCALL32_IMPL)
406 struct modinfo32 modi32
;
409 if (get_udatamodel() == DATAMODEL_NATIVE
) {
410 if (copyin(umodi
, &modi
, sizeof (struct modinfo
)) != 0)
413 #ifdef _SYSCALL32_IMPL
415 bzero(&modi
, sizeof (modi
));
416 if (copyin(umodi
, &modi32
, sizeof (struct modinfo32
)) != 0)
418 modi
.mi_info
= modi32
.mi_info
;
419 modi
.mi_id
= modi32
.mi_id
;
420 modi
.mi_nextid
= modi32
.mi_nextid
;
421 nobase
= modi
.mi_info
& MI_INFO_NOBASE
;
425 * This flag is -only- for the kernels use.
427 modi
.mi_info
&= ~MI_INFO_LINKAGE
;
429 retval
= modinfo(id
, &modi
);
433 if (get_udatamodel() == DATAMODEL_NATIVE
) {
434 if (copyout(&modi
, umodi
, sizeof (struct modinfo
)) != 0)
436 #ifdef _SYSCALL32_IMPL
440 if (!nobase
&& (uintptr_t)modi
.mi_base
> UINT32_MAX
)
443 modi32
.mi_info
= modi
.mi_info
;
444 modi32
.mi_state
= modi
.mi_state
;
445 modi32
.mi_id
= modi
.mi_id
;
446 modi32
.mi_nextid
= modi
.mi_nextid
;
447 modi32
.mi_base
= (caddr32_t
)(uintptr_t)modi
.mi_base
;
448 modi32
.mi_size
= modi
.mi_size
;
449 modi32
.mi_rev
= modi
.mi_rev
;
450 modi32
.mi_loadcnt
= modi
.mi_loadcnt
;
451 bcopy(modi
.mi_name
, modi32
.mi_name
, sizeof (modi32
.mi_name
));
452 for (i
= 0; i
< MODMAXLINK32
; i
++) {
453 modi32
.mi_msinfo
[i
].msi_p0
= modi
.mi_msinfo
[i
].msi_p0
;
454 bcopy(modi
.mi_msinfo
[i
].msi_linkinfo
,
455 modi32
.mi_msinfo
[i
].msi_linkinfo
,
456 sizeof (modi32
.mi_msinfo
[0].msi_linkinfo
));
458 if (copyout(&modi32
, umodi
, sizeof (struct modinfo32
)) != 0)
467 * Return the last major number in the range of permissible major numbers.
471 modctl_modreserve(modid_t id
, int *data
)
473 if (copyout(&devcnt
, data
, sizeof (devcnt
)) != 0)
478 /* Add/Remove driver and binding aliases */
480 modctl_update_driver_aliases(int add
, int *data
)
484 struct aliases alias
;
486 char name
[MAXMODCONFNAME
];
487 char cname
[MAXMODCONFNAME
];
495 bzero(&mc
, sizeof (struct modconfig
));
496 if (get_udatamodel() == DATAMODEL_NATIVE
) {
497 if (copyin(data
, &mc
, sizeof (struct modconfig
)) != 0)
500 #ifdef _SYSCALL32_IMPL
502 struct modconfig32 modc32
;
503 if (copyin(data
, &modc32
, sizeof (struct modconfig32
)) != 0)
506 bcopy(modc32
.drvname
, mc
.drvname
,
507 sizeof (modc32
.drvname
));
508 bcopy(modc32
.drvclass
, mc
.drvclass
,
509 sizeof (modc32
.drvclass
));
510 mc
.major
= modc32
.major
;
511 mc
.flags
= modc32
.flags
;
512 mc
.num_aliases
= modc32
.num_aliases
;
513 mc
.ap
= (struct aliases
*)(uintptr_t)modc32
.ap
;
519 * If the driver is already in the mb_hashtab, and the name given
520 * doesn't match that driver's name, fail. Otherwise, pass, since
521 * we may be adding aliases.
523 drvname
= mod_major_to_name(mc
.major
);
524 if ((drvname
!= NULL
) && strcmp(drvname
, mc
.drvname
) != 0)
528 * Precede alias removal by unbinding as many devices as possible.
531 (void) i_ddi_unload_drvconf(mc
.major
);
532 i_ddi_unbind_devs(mc
.major
);
536 * Add/remove each supplied driver alias to/from mb_hashtab
539 if (mc
.num_aliases
> 0)
540 aliases
= kmem_zalloc(
541 mc
.num_aliases
* sizeof (struct alias_info
), KM_SLEEP
);
543 for (i
= 0; i
< mc
.num_aliases
; i
++) {
544 bzero(&alias
, sizeof (struct aliases
));
545 if (get_udatamodel() == DATAMODEL_NATIVE
) {
546 if (copyin(ap
, &alias
, sizeof (struct aliases
)) != 0) {
550 if (alias
.a_len
> MAXMODCONFNAME
) {
554 if (copyin(alias
.a_name
, name
, alias
.a_len
) != 0) {
558 if (name
[alias
.a_len
- 1] != '\0') {
563 #ifdef _SYSCALL32_IMPL
565 struct aliases32 al32
;
566 bzero(&al32
, sizeof (struct aliases32
));
567 if (copyin(ap
, &al32
, sizeof (struct aliases32
)) != 0) {
571 if (al32
.a_len
> MAXMODCONFNAME
) {
575 if (copyin((void *)(uintptr_t)al32
.a_name
,
576 name
, al32
.a_len
) != 0) {
580 if (name
[al32
.a_len
- 1] != '\0') {
584 alias
.a_next
= (void *)(uintptr_t)al32
.a_next
;
587 check_esc_sequences(name
, cname
);
588 aip
->alias_name
= strdup(cname
);
597 /* attempt to unbind all devices bound to each alias */
598 for (i
= 0; i
< mc
.num_aliases
; i
++) {
599 n
= i_ddi_unbind_devs_by_alias(
600 mc
.major
, aip
->alias_name
);
602 aip
->alias_resid
= n
;
606 * If some device bound to an alias remains in use,
607 * and override wasn't specified, no change is made to
608 * the binding state and we fail the operation.
610 if (resid
> 0 && ((mc
.flags
& MOD_UNBIND_OVERRIDE
) == 0)) {
616 * No device remains bound of any of the aliases,
617 * or force was requested. Mark each alias as
618 * inactive via delete_mbind so no future binds
619 * to this alias take place and that a new
620 * binding can be established.
623 for (i
= 0; i
< mc
.num_aliases
; i
++) {
624 if (moddebug
& MODDEBUG_BINDING
)
625 cmn_err(CE_CONT
, "Removing binding for %s "
626 "(%d active references)\n",
627 aip
->alias_name
, aip
->alias_resid
);
628 delete_mbind(aip
->alias_name
, mb_hashtab
);
634 for (i
= 0; i
< mc
.num_aliases
; i
++) {
635 if (moddebug
& MODDEBUG_BINDING
)
636 cmn_err(CE_NOTE
, "Adding binding for '%s'\n",
638 (void) make_mbind(aip
->alias_name
,
639 mc
.major
, NULL
, mb_hashtab
);
643 * Try to establish an mbinding for mc.drvname, and add it to
644 * devnames. Add class if any after establishing the major
647 (void) make_mbind(mc
.drvname
, mc
.major
, NULL
, mb_hashtab
);
648 if ((rv
= make_devname(mc
.drvname
, mc
.major
,
649 (mc
.flags
& MOD_ADDMAJBIND_UPDATE
) ?
650 DN_DRIVER_INACTIVE
: 0)) != 0) {
654 if (mc
.drvclass
[0] != '\0')
655 add_class(mc
.drvname
, mc
.drvclass
);
656 if ((mc
.flags
& MOD_ADDMAJBIND_UPDATE
) == 0) {
657 (void) i_ddi_load_drvconf(mc
.major
);
662 * Ensure that all nodes are bound to the most appropriate driver
663 * possible, attempting demotion and rebind when a more appropriate
664 * driver now exists. But not when adding a driver update-only.
666 if ((add
== 0) || ((mc
.flags
& MOD_ADDMAJBIND_UPDATE
) == 0)) {
668 i_ddi_di_cache_invalidate();
672 if (mc
.num_aliases
> 0) {
674 for (i
= 0; i
< mc
.num_aliases
; i
++) {
675 if (aip
->alias_name
!= NULL
)
676 strfree(aip
->alias_name
);
679 kmem_free(aliases
, mc
.num_aliases
* sizeof (struct alias_info
));
685 modctl_add_driver_aliases(int *data
)
687 return (modctl_update_driver_aliases(1, data
));
691 modctl_remove_driver_aliases(int *data
)
693 return (modctl_update_driver_aliases(0, data
));
697 modctl_rem_major(major_t major
)
699 struct devnames
*dnp
;
704 /* mark devnames as removed */
705 dnp
= &devnamesp
[major
];
706 LOCK_DEV_OPS(&dnp
->dn_lock
);
707 if (dnp
->dn_name
== NULL
||
708 (dnp
->dn_flags
& (DN_DRIVER_REMOVED
| DN_TAKEN_GETUDEV
))) {
709 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
712 dnp
->dn_flags
|= DN_DRIVER_REMOVED
;
713 pm_driver_removed(major
);
714 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
716 (void) i_ddi_unload_drvconf(major
);
717 i_ddi_unbind_devs(major
);
719 i_ddi_di_cache_invalidate();
721 /* purge all the bindings to this driver */
722 purge_mbind(major
, mb_hashtab
);
727 path_to_vfs(char *name
)
732 if (lookupname(name
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &vp
))
743 static int n_modpath
= 0;
744 static char *modpath_copy
;
745 static struct pathvfs
{
754 if (n_modpath
!= 0) {
755 for (i
= 0; i
< n_modpath
; i
++) {
756 vfsp
= path_to_vfs(pathvfs
[i
].path
);
757 if (vfsp
!= pathvfs
[i
].vfsp
) {
758 pathvfs
[i
].vfsp
= vfsp
;
767 * First call, initialize the pathvfs structure
769 modpath_copy
= i_ddi_strdup(default_path
, KM_SLEEP
);
772 tmp1
= strchr(tmp
, ' ');
777 tmp1
= strchr(tmp
, ' ');
780 pathvfs
= kmem_zalloc(n_modpath
* sizeof (struct pathvfs
), KM_SLEEP
);
782 for (i
= 0; i
< n_modpath
; i
++) {
783 pathvfs
[i
].path
= tmp
;
784 vfsp
= path_to_vfs(tmp
);
785 pathvfs
[i
].vfsp
= vfsp
;
786 tmp
+= strlen(tmp
) + 1;
788 return (1); /* always reread driver.conf the first time */
792 modctl_load_drvconf(major_t major
, int flags
)
797 * devfsadm -u - read all new driver.conf files
798 * and bind and configure devices for new drivers.
800 if (flags
& MOD_LOADDRVCONF_RECONF
) {
801 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE
);
803 i_ddi_di_cache_invalidate();
808 * update_drv <drv> - reload driver.conf for the specified driver
810 if (major
!= DDI_MAJOR_T_NONE
) {
811 ret
= i_ddi_load_drvconf(major
);
818 * We are invoked to rescan new driver.conf files. It is
819 * only necessary if a new file system was mounted in the
820 * module_path. Because rescanning driver.conf files can
821 * take some time on older platforms (sun4m), the following
822 * code skips unnecessary driver.conf rescans to optimize
825 if (new_vfs_in_modpath()) {
826 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE
);
828 * If we are still initializing io subsystem,
829 * load drivers with ddi-forceattach property
831 if (!i_ddi_io_initialized())
832 i_ddi_forceattach_drivers();
838 * Unload driver.conf file and follow up by attempting
839 * to rebind devices to more appropriate driver.
842 modctl_unload_drvconf(major_t major
)
849 ret
= i_ddi_unload_drvconf(major
);
852 (void) i_ddi_unbind_devs(major
);
859 check_esc_sequences(char *str
, char *cstr
)
866 for (i
= 0; i
< len
; i
++, str
++, cstr
++) {
872 * we only handle octal escape sequences for SPACE
874 if (*p
++ == '0' && *p
++ == '4' && *p
== '0') {
886 modctl_getmodpathlen(int *data
)
889 len
= strlen(default_path
);
890 if (copyout(&len
, data
, sizeof (len
)) != 0)
896 modctl_getmodpath(char *data
)
898 if (copyout(default_path
, data
, strlen(default_path
) + 1) != 0)
904 modctl_read_sysbinding_file(void)
906 (void) read_binding_file(sysbind
, sb_hashtab
, make_mbind
);
911 modctl_getmaj(char *uname
, uint_t ulen
, int *umajorp
)
919 if ((retval
= copyinstr(uname
, name
,
920 (ulen
< 256) ? ulen
: 256, 0)) != 0)
922 if ((major
= mod_name_to_major(name
)) == DDI_MAJOR_T_NONE
)
924 if (copyout(&major
, umajorp
, sizeof (major_t
)) != 0)
930 convert_constraint_string(char *constraints
, size_t len
)
937 ASSERT(constraints
!= NULL
);
940 for (i
= 0, p
= constraints
; strlen(p
) > 0; i
++, p
+= strlen(p
) + 1)
946 kmem_free(constraints
, len
);
950 array
= kmem_alloc((n
+ 1) * sizeof (char *), KM_SLEEP
);
952 for (i
= 0, p
= constraints
; i
< n
; i
++, p
+= strlen(p
) + 1) {
953 array
[i
] = i_ddi_strdup(p
, KM_SLEEP
);
957 kmem_free(constraints
, len
);
963 modctl_retire(char *path
, char *uconstraints
, size_t ulen
)
975 if ((uconstraints
== NULL
) ^ (ulen
== 0))
978 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
979 retval
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
981 kmem_free(pathbuf
, MAXPATHLEN
);
984 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
985 kmem_free(pathbuf
, MAXPATHLEN
);
988 * First check if the device is already retired.
989 * If it is, then persist the retire anyway, just in case the retire
990 * store has got out of sync with the boot archive.
992 if (e_ddi_device_retired(devpath
)) {
993 cmn_err(CE_NOTE
, "Device: already retired: %s", devpath
);
994 (void) e_ddi_retire_persist(devpath
);
995 kmem_free(devpath
, strlen(devpath
) + 1);
1001 constraints
= kmem_alloc(ulen
, KM_SLEEP
);
1002 if (copyin(uconstraints
, constraints
, ulen
)) {
1003 kmem_free(constraints
, ulen
);
1004 kmem_free(devpath
, strlen(devpath
) + 1);
1007 cons_array
= convert_constraint_string(constraints
, ulen
);
1011 * Try to retire the device first. The following
1012 * routine will return an error only if the device
1013 * is not retireable i.e. retire constraints forbid
1014 * a retire. A return of success from this routine
1015 * indicates that device is retireable.
1017 retval
= e_ddi_retire_device(devpath
, cons_array
);
1018 if (retval
!= DDI_SUCCESS
) {
1019 cmn_err(CE_WARN
, "constraints forbid retire: %s", devpath
);
1020 kmem_free(devpath
, strlen(devpath
) + 1);
1025 * Ok, the retire succeeded. Persist the retire.
1026 * If retiring a nexus, we need to only persist the
1027 * nexus retire. Any children of a retired nexus
1028 * are automatically covered by the retire store
1031 retval
= e_ddi_retire_persist(devpath
);
1033 cmn_err(CE_WARN
, "Failed to persist device retire: error %d: "
1034 "%s", retval
, devpath
);
1035 kmem_free(devpath
, strlen(devpath
) + 1);
1038 if (moddebug
& MODDEBUG_RETIRE
)
1039 cmn_err(CE_NOTE
, "Persisted retire of device: %s", devpath
);
1041 kmem_free(devpath
, strlen(devpath
) + 1);
1046 modctl_is_retired(char *path
, int *statep
)
1054 if (path
== NULL
|| statep
== NULL
)
1057 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1058 error
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
1060 kmem_free(pathbuf
, MAXPATHLEN
);
1063 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
1064 kmem_free(pathbuf
, MAXPATHLEN
);
1066 if (e_ddi_device_retired(devpath
))
1070 kmem_free(devpath
, strlen(devpath
) + 1);
1072 return (copyout(&status
, statep
, sizeof (status
)) ? EFAULT
: 0);
1076 modctl_unretire(char *path
)
1087 pathbuf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1088 retval
= copyinstr(path
, pathbuf
, MAXPATHLEN
, &pathsz
);
1090 kmem_free(pathbuf
, MAXPATHLEN
);
1093 devpath
= i_ddi_strdup(pathbuf
, KM_SLEEP
);
1094 kmem_free(pathbuf
, MAXPATHLEN
);
1097 * We check if a device is retired (first) before
1098 * unpersisting the retire, because we use the
1099 * retire store to determine if a device is retired.
1100 * If we unpersist first, the device will always appear
1101 * to be unretired. For the rationale behind unpersisting
1102 * a device that is not retired, see the next comment.
1104 retired
= e_ddi_device_retired(devpath
);
1107 * We call unpersist unconditionally because the lookup
1108 * for retired devices (e_ddi_device_retired()), skips "bypassed"
1109 * devices. We still want to be able remove "bypassed" entries
1110 * from the persistent store, so we unpersist unconditionally
1111 * i.e. whether or not the entry is found on a lookup.
1113 * e_ddi_retire_unpersist() returns 1 if it found and cleared
1114 * an entry from the retire store or 0 otherwise.
1116 if (e_ddi_retire_unpersist(devpath
))
1117 if (moddebug
& MODDEBUG_RETIRE
) {
1118 cmn_err(CE_NOTE
, "Unpersisted retire of device: %s",
1123 * Check if the device is already unretired. If so,
1124 * the unretire becomes a NOP
1127 cmn_err(CE_NOTE
, "Not retired: %s", devpath
);
1128 kmem_free(devpath
, strlen(devpath
) + 1);
1132 retval
= e_ddi_unretire_device(devpath
);
1134 cmn_err(CE_WARN
, "cannot unretire device: error %d, path %s\n",
1138 kmem_free(devpath
, strlen(devpath
) + 1);
1144 modctl_getname(char *uname
, uint_t ulen
, int *umajorp
)
1149 if (copyin(umajorp
, &major
, sizeof (major
)) != 0)
1151 if ((name
= mod_major_to_name(major
)) == NULL
)
1153 if ((strlen(name
) + 1) > ulen
)
1155 return (copyoutstr(name
, uname
, ulen
, NULL
));
1159 modctl_devt2instance(dev_t dev
, int *uinstancep
)
1163 if ((instance
= dev_to_instance(dev
)) == -1)
1166 return (copyout(&instance
, uinstancep
, sizeof (int)));
1170 * Return the sizeof of the device id.
1173 modctl_sizeof_devid(dev_t dev
, uint_t
*len
)
1179 if (ddi_lyr_get_devid(dev
, &devid
) == DDI_FAILURE
)
1182 sz
= ddi_devid_sizeof(devid
);
1183 ddi_devid_free(devid
);
1185 /* copyout device id size */
1186 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1193 * Return a copy of the device id.
1196 modctl_get_devid(dev_t dev
, uint_t len
, ddi_devid_t udevid
)
1203 if (ddi_lyr_get_devid(dev
, &devid
) == DDI_FAILURE
)
1206 sz
= ddi_devid_sizeof(devid
);
1208 /* Error if device id is larger than space allocated */
1210 ddi_devid_free(devid
);
1214 /* copy out device id */
1215 if (copyout(devid
, udevid
, sz
) != 0)
1217 ddi_devid_free(devid
);
1222 * return the /devices paths associated with the specified devid and
1227 modctl_devid2paths(ddi_devid_t udevid
, char *uminor_name
, uint_t flag
,
1228 size_t *ulensp
, char *upaths
)
1230 ddi_devid_t devid
= NULL
;
1232 char *minor_name
= NULL
;
1233 dev_info_t
*dip
= NULL
;
1235 struct ddi_minor_data
*dmdp
;
1240 dev_t
*devlist
= NULL
;
1246 * If upaths is NULL then we are only computing the amount of space
1247 * needed to hold the paths and returning the value in *ulensp. If we
1248 * are copying out paths then we get the amount of space allocated by
1249 * the caller. If the actual space needed for paths is larger, or
1250 * things are changing out from under us, then we return EAGAIN.
1255 if (copyin(ulensp
, &ulens
, sizeof (ulens
)) != 0)
1260 * copyin enough of the devid to determine the length then
1261 * reallocate and copy in the entire devid.
1263 devid_len
= ddi_devid_sizeof(NULL
);
1264 devid
= kmem_alloc(devid_len
, KM_SLEEP
);
1265 if (copyin(udevid
, devid
, devid_len
)) {
1270 devid_len
= ddi_devid_sizeof(devid
);
1271 kmem_free(devid
, len
);
1272 devid
= kmem_alloc(devid_len
, KM_SLEEP
);
1273 if (copyin(udevid
, devid
, devid_len
)) {
1278 /* copyin the minor name if specified. */
1279 minor_name
= uminor_name
;
1280 if ((minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1281 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1282 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
)) {
1283 minor_name
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1284 if (copyinstr(uminor_name
, minor_name
, MAXPATHLEN
, 0)) {
1291 * Use existing function to resolve the devid into a devlist.
1293 * NOTE: there is a loss of spectype information in the current
1294 * ddi_lyr_devid_to_devlist implementation. We work around this by not
1295 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
1296 * node forms in the loop processing the devlist below. It would be
1297 * best if at some point the use of this interface here was replaced
1298 * with a path oriented call.
1300 if (ddi_lyr_devid_to_devlist(devid
,
1301 (minor_name
== DEVID_MINOR_NAME_ALL
) ?
1302 DEVID_MINOR_NAME_ALL_CHR
: minor_name
,
1303 &ndevs
, &devlist
) != DDI_SUCCESS
) {
1309 * loop over the devlist, converting each devt to a path and doing
1310 * a copyout of the path and computation of the amount of space
1311 * needed to hold all the paths
1313 path
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1314 for (i
= 0, lens
= 0; i
< ndevs
; i
++) {
1316 /* find the dip associated with the dev_t */
1317 if ((dip
= e_ddi_hold_devi_by_dev(devlist
[i
], 0)) == NULL
)
1320 /* loop over all the minor nodes, skipping ones we don't want */
1321 ndi_devi_enter(dip
, &circ
);
1322 for (dmdp
= DEVI(dip
)->devi_minor
; dmdp
; dmdp
= dmdp
->next
) {
1323 if ((dmdp
->ddm_dev
!= devlist
[i
]) ||
1324 (dmdp
->type
!= DDM_MINOR
))
1327 if ((minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1328 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1329 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
) &&
1330 strcmp(minor_name
, dmdp
->ddm_name
))
1333 if ((minor_name
== DEVID_MINOR_NAME_ALL_CHR
) &&
1334 (dmdp
->ddm_spec_type
!= S_IFCHR
))
1336 if ((minor_name
== DEVID_MINOR_NAME_ALL_BLK
) &&
1337 (dmdp
->ddm_spec_type
!= S_IFBLK
))
1341 (void) ddi_pathname_minor(dmdp
, path
);
1342 len
= strlen(path
) + 1;
1343 *(path
+ len
) = '\0'; /* set double termination */
1346 /* copyout the path with double terminations */
1352 if (copyout(path
, upaths
, len
+ 1)) {
1359 ndi_devi_exit(dip
, circ
);
1360 ddi_release_devi(dip
);
1363 lens
++; /* add one for double termination */
1365 /* copy out the amount of space needed to hold the paths */
1366 if (ulensp
&& copyout(&lens
, ulensp
, sizeof (lens
))) {
1373 ndi_devi_exit(dip
, circ
);
1374 ddi_release_devi(dip
);
1377 kmem_free(path
, MAXPATHLEN
);
1379 ddi_lyr_free_devlist(devlist
, ndevs
);
1381 (minor_name
!= DEVID_MINOR_NAME_ALL
) &&
1382 (minor_name
!= DEVID_MINOR_NAME_ALL_CHR
) &&
1383 (minor_name
!= DEVID_MINOR_NAME_ALL_BLK
))
1384 kmem_free(minor_name
, MAXPATHLEN
);
1386 kmem_free(devid
, devid_len
);
1391 * Return the size of the minor name.
1394 modctl_sizeof_minorname(dev_t dev
, int spectype
, uint_t
*len
)
1399 /* get the minor name */
1400 if (ddi_lyr_get_minor_name(dev
, spectype
, &name
) == DDI_FAILURE
)
1403 sz
= strlen(name
) + 1;
1404 kmem_free(name
, sz
);
1406 /* copy out the size of the minor name */
1407 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1414 * Return the minor name.
1417 modctl_get_minorname(dev_t dev
, int spectype
, uint_t len
, char *uname
)
1423 /* get the minor name */
1424 if (ddi_lyr_get_minor_name(dev
, spectype
, &name
) == DDI_FAILURE
)
1427 sz
= strlen(name
) + 1;
1429 /* Error if the minor name is larger than the space allocated */
1431 kmem_free(name
, sz
);
1435 /* copy out the minor name */
1436 if (copyout(name
, uname
, sz
) != 0)
1438 kmem_free(name
, sz
);
1443 * Return the size of the (dev_t,spectype) devfspath name.
1446 modctl_devfspath_len(dev_t dev
, int spectype
, uint_t
*len
)
1451 /* get the path name */
1452 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1453 if (ddi_dev_pathname(dev
, spectype
, name
) == DDI_FAILURE
) {
1454 kmem_free(name
, MAXPATHLEN
);
1458 sz
= strlen(name
) + 1;
1459 kmem_free(name
, MAXPATHLEN
);
1461 /* copy out the size of the path name */
1462 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1469 * Return the (dev_t,spectype) devfspath name.
1472 modctl_devfspath(dev_t dev
, int spectype
, uint_t len
, char *uname
)
1478 /* get the path name */
1479 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1480 if (ddi_dev_pathname(dev
, spectype
, name
) == DDI_FAILURE
) {
1481 kmem_free(name
, MAXPATHLEN
);
1485 sz
= strlen(name
) + 1;
1487 /* Error if the path name is larger than the space allocated */
1489 kmem_free(name
, MAXPATHLEN
);
1493 /* copy out the path name */
1494 if (copyout(name
, uname
, sz
) != 0)
1496 kmem_free(name
, MAXPATHLEN
);
1501 * Return the size of the (major,instance) devfspath name.
1504 modctl_devfspath_mi_len(major_t major
, int instance
, uint_t
*len
)
1509 /* get the path name */
1510 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1511 if (e_ddi_majorinstance_to_path(major
, instance
, name
) != DDI_SUCCESS
) {
1512 kmem_free(name
, MAXPATHLEN
);
1516 sz
= strlen(name
) + 1;
1517 kmem_free(name
, MAXPATHLEN
);
1519 /* copy out the size of the path name */
1520 if (copyout(&sz
, len
, sizeof (sz
)) != 0)
1527 * Return the (major_instance) devfspath name.
1528 * NOTE: e_ddi_majorinstance_to_path does not require the device to attach to
1529 * return a path - it uses the instance tree.
1532 modctl_devfspath_mi(major_t major
, int instance
, uint_t len
, char *uname
)
1538 /* get the path name */
1539 name
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1540 if (e_ddi_majorinstance_to_path(major
, instance
, name
) != DDI_SUCCESS
) {
1541 kmem_free(name
, MAXPATHLEN
);
1545 sz
= strlen(name
) + 1;
1547 /* Error if the path name is larger than the space allocated */
1549 kmem_free(name
, MAXPATHLEN
);
1553 /* copy out the path name */
1554 if (copyout(name
, uname
, sz
) != 0)
1556 kmem_free(name
, MAXPATHLEN
);
1561 modctl_get_fbname(char *path
)
1564 char *pathname
= NULL
;
1567 /* make sure fbdev is set before we plunge in */
1571 pathname
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1572 if ((rval
= ddi_dev_pathname(fbdev
, S_IFCHR
,
1573 pathname
)) == DDI_SUCCESS
) {
1574 if (copyout(pathname
, path
, strlen(pathname
)+1) != 0) {
1578 kmem_free(pathname
, MAXPATHLEN
);
1583 * modctl_reread_dacf()
1584 * Reread the dacf rules database from the named binding file.
1585 * If NULL is specified, pass along the NULL, it means 'use the default'.
1588 modctl_reread_dacf(char *path
)
1591 char *filename
, *filenamep
;
1593 filename
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
1598 if (copyinstr(path
, filename
, MAXPATHLEN
, 0) != 0) {
1602 filenamep
= filename
;
1603 filenamep
[MAXPATHLEN
- 1] = '\0';
1606 rval
= read_dacf_binding_file(filenamep
);
1608 kmem_free(filename
, MAXPATHLEN
);
1614 modctl_modevents(int subcmd
, uintptr_t a2
, uintptr_t a3
, uintptr_t a4
,
1622 case MODEVENTS_FLUSH
:
1623 /* flush all currently queued events */
1624 log_sysevent_flushq(subcmd
, flag
);
1627 case MODEVENTS_SET_DOOR_UPCALL_FILENAME
:
1629 * bind door_upcall to filename
1630 * this should only be done once per invocation
1631 * of the event daemon.
1634 filenamep
= kmem_zalloc(MOD_MAXPATH
, KM_SLEEP
);
1636 if (copyinstr((char *)a2
, filenamep
, MOD_MAXPATH
, 0)) {
1639 error
= log_sysevent_filename(filenamep
);
1641 kmem_free(filenamep
, MOD_MAXPATH
);
1644 case MODEVENTS_GETDATA
:
1645 error
= log_sysevent_copyout_data((sysevent_id_t
*)a2
,
1646 (size_t)a3
, (caddr_t
)a4
);
1649 case MODEVENTS_FREEDATA
:
1650 error
= log_sysevent_free_data((sysevent_id_t
*)a2
);
1652 case MODEVENTS_POST_EVENT
:
1653 error
= log_usr_sysevent((sysevent_t
*)a2
, (uint32_t)a3
,
1654 (sysevent_id_t
*)a4
);
1656 case MODEVENTS_REGISTER_EVENT
:
1657 error
= log_sysevent_register((char *)a2
, (char *)a3
,
1668 free_mperm(mperm_t
*mp
)
1672 if (mp
->mp_minorname
) {
1673 len
= strlen(mp
->mp_minorname
) + 1;
1674 kmem_free(mp
->mp_minorname
, len
);
1676 kmem_free(mp
, sizeof (mperm_t
));
1679 #define MP_NO_DRV_ERR \
1680 "/etc/minor_perm: no driver for %s\n"
1682 #define MP_EMPTY_MINOR \
1683 "/etc/minor_perm: empty minor name for driver %s\n"
1685 #define MP_NO_MINOR \
1686 "/etc/minor_perm: no minor matching %s for driver %s\n"
1689 * Remove mperm entry with matching minorname
1692 rem_minorperm(major_t major
, char *drvname
, mperm_t
*mp
, int is_clone
)
1695 mperm_t
*freemp
= NULL
;
1696 struct devnames
*dnp
= &devnamesp
[major
];
1699 ASSERT(mp
->mp_minorname
&& strlen(mp
->mp_minorname
) > 0);
1701 LOCK_DEV_OPS(&dnp
->dn_lock
);
1702 if (strcmp(mp
->mp_minorname
, "*") == 0) {
1703 wildmp
= ((is_clone
== 0) ?
1704 &dnp
->dn_mperm_wild
: &dnp
->dn_mperm_clone
);
1709 mp_head
= &dnp
->dn_mperm
;
1711 if (strcmp((*mp_head
)->mp_minorname
,
1712 mp
->mp_minorname
) != 0) {
1713 mp_head
= &(*mp_head
)->mp_next
;
1716 /* remove the entry */
1718 *mp_head
= freemp
->mp_next
;
1723 if (moddebug
& MODDEBUG_MINORPERM
) {
1724 cmn_err(CE_CONT
, "< %s %s 0%o %d %d\n",
1725 drvname
, freemp
->mp_minorname
,
1726 freemp
->mp_mode
& 0777,
1727 freemp
->mp_uid
, freemp
->mp_gid
);
1731 if (moddebug
& MODDEBUG_MINORPERM
) {
1732 cmn_err(CE_CONT
, MP_NO_MINOR
,
1733 drvname
, mp
->mp_minorname
);
1737 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
1741 * Add minor perm entry
1744 add_minorperm(major_t major
, char *drvname
, mperm_t
*mp
, int is_clone
)
1747 mperm_t
*freemp
= NULL
;
1748 struct devnames
*dnp
= &devnamesp
[major
];
1751 ASSERT(mp
->mp_minorname
&& strlen(mp
->mp_minorname
) > 0);
1754 * Note that update_drv replace semantics require
1755 * replacing matching entries with the new permissions.
1757 LOCK_DEV_OPS(&dnp
->dn_lock
);
1758 if (strcmp(mp
->mp_minorname
, "*") == 0) {
1759 wildmp
= ((is_clone
== 0) ?
1760 &dnp
->dn_mperm_wild
: &dnp
->dn_mperm_clone
);
1765 mperm_t
*p
, *v
= NULL
;
1766 for (p
= dnp
->dn_mperm
; p
; v
= p
, p
= p
->mp_next
) {
1767 if (strcmp(p
->mp_minorname
, mp
->mp_minorname
) == 0) {
1772 mp
->mp_next
= p
->mp_next
;
1778 mp_head
= &dnp
->dn_mperm
;
1779 if (*mp_head
== NULL
) {
1782 mp
->mp_next
= *mp_head
;
1789 if (moddebug
& MODDEBUG_MINORPERM
) {
1790 cmn_err(CE_CONT
, "< %s %s 0%o %d %d\n",
1791 drvname
, freemp
->mp_minorname
,
1792 freemp
->mp_mode
& 0777,
1793 freemp
->mp_uid
, freemp
->mp_gid
);
1797 if (moddebug
& MODDEBUG_MINORPERM
) {
1798 cmn_err(CE_CONT
, "> %s %s 0%o %d %d\n",
1799 drvname
, mp
->mp_minorname
, mp
->mp_mode
& 0777,
1800 mp
->mp_uid
, mp
->mp_gid
);
1802 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
1807 process_minorperm(int cmd
, nvlist_t
*nvl
)
1817 ASSERT(cmd
== MODLOADMINORPERM
||
1818 cmd
== MODADDMINORPERM
|| cmd
== MODREMMINORPERM
);
1821 while ((nvp
= nvlist_next_nvpair(nvl
, nvp
)) != NULL
) {
1822 name
= nvpair_name(nvp
);
1825 (void) nvpair_value_string(nvp
, &minor
);
1826 major
= ddi_name_to_major(name
);
1827 if (major
!= DDI_MAJOR_T_NONE
) {
1828 mp
= kmem_zalloc(sizeof (*mp
), KM_SLEEP
);
1829 if (minor
== NULL
|| strlen(minor
) == 0) {
1830 if (moddebug
& MODDEBUG_MINORPERM
) {
1831 cmn_err(CE_CONT
, MP_EMPTY_MINOR
, name
);
1837 * The minor name of a node using the clone
1838 * driver must be the driver name. To avoid
1839 * multiple searches, we map entries in the form
1840 * clone:<driver> to <driver>:*. This also allows us
1841 * to filter out some of the litter in /etc/minor_perm.
1842 * Minor perm alias entries where the name is not
1843 * the driver kept on the clone list itself.
1844 * This all seems very fragile as a driver could
1845 * be introduced with an existing alias name.
1847 if (strcmp(name
, "clone") == 0) {
1848 minmaj
= ddi_name_to_major(minor
);
1849 if (minmaj
!= DDI_MAJOR_T_NONE
) {
1850 if (moddebug
& MODDEBUG_MINORPERM
) {
1852 "mapping %s:%s to %s:*\n",
1853 name
, minor
, minor
);
1864 i_ddi_strdup(minor
, KM_SLEEP
);
1868 if (moddebug
& MODDEBUG_MINORPERM
) {
1869 cmn_err(CE_CONT
, MP_NO_DRV_ERR
, name
);
1874 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1875 ASSERT(strcmp(nvpair_name(nvp
), "mode") == 0);
1877 (void) nvpair_value_int32(nvp
, (int *)&mp
->mp_mode
);
1879 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1880 ASSERT(strcmp(nvpair_name(nvp
), "uid") == 0);
1882 (void) nvpair_value_uint32(nvp
, &mp
->mp_uid
);
1884 nvp
= nvlist_next_nvpair(nvl
, nvp
);
1885 ASSERT(strcmp(nvpair_name(nvp
), "gid") == 0);
1887 (void) nvpair_value_uint32(nvp
, &mp
->mp_gid
);
1889 if (cmd
== MODREMMINORPERM
) {
1890 rem_minorperm(major
, name
, mp
, is_clone
);
1893 add_minorperm(major
, name
, mp
, is_clone
);
1898 if (cmd
== MODLOADMINORPERM
)
1899 minorperm_loaded
= 1;
1902 * Reset permissions of cached dv_nodes
1904 (void) devfs_reset_perm(DV_RESET_PERM
);
1910 modctl_minorperm(int cmd
, char *usrbuf
, size_t buflen
)
1914 char *buf
= kmem_alloc(buflen
, KM_SLEEP
);
1916 if ((error
= ddi_copyin(usrbuf
, buf
, buflen
, 0)) != 0) {
1917 kmem_free(buf
, buflen
);
1921 error
= nvlist_unpack(buf
, buflen
, &nvl
, KM_SLEEP
);
1922 kmem_free(buf
, buflen
);
1926 error
= process_minorperm(cmd
, nvl
);
1939 list_node_t pe_node
;
1945 modctl_inst_walker(const char *path
, in_node_t
*np
, in_drv_t
*dp
, void *arg
)
1947 struct walk_args
*wargs
= (struct walk_args
*)arg
;
1948 struct path_elem
*pe
;
1952 * Search may be restricted to a single driver in the case of rem_drv
1954 if (wargs
->wa_drvname
&&
1955 strcmp(dp
->ind_driver_name
, wargs
->wa_drvname
) != 0)
1956 return (INST_WALK_CONTINUE
);
1958 pe
= kmem_zalloc(sizeof (*pe
), KM_SLEEP
);
1959 pe
->pe_dir
= i_ddi_strdup((char *)path
, KM_SLEEP
);
1960 pe
->pe_dirlen
= strlen(pe
->pe_dir
) + 1;
1961 ASSERT(strrchr(pe
->pe_dir
, '/') != NULL
);
1962 nodename
= strrchr(pe
->pe_dir
, '/');
1964 pe
->pe_nodename
= nodename
;
1965 list_insert_tail(&wargs
->wa_pathlist
, pe
);
1967 return (INST_WALK_CONTINUE
);
1971 * /devices attribute nodes clean-up optionally performed
1972 * when removing a driver (rem_drv -C).
1974 * Removing attribute nodes allows a machine to be reprovisioned
1975 * without the side-effect of inadvertently picking up stale
1976 * device node ownership or permissions.
1978 * Preserving attributes (not performing cleanup) allows devices
1979 * attribute changes to be preserved across upgrades, as
1980 * upgrade rather heavy-handedly does a rem_drv/add_drv cycle.
1983 modctl_remdrv_cleanup(const char *u_drvname
)
1985 struct walk_args
*wargs
;
1986 struct path_elem
*pe
;
1990 drvname
= kmem_alloc(MAXMODCONFNAME
, KM_SLEEP
);
1991 if ((err
= copyinstr(u_drvname
, drvname
, MAXMODCONFNAME
, 0))) {
1992 kmem_free(drvname
, MAXMODCONFNAME
);
1997 * First go through the instance database. For each
1998 * instance of a device bound to the driver being
1999 * removed, remove any underlying devfs attribute nodes.
2001 * This is a two-step process. First we go through
2002 * the instance data itself, constructing a list of
2003 * the nodes discovered. The second step is then
2004 * to find and remove any devfs attribute nodes
2005 * for the instances discovered in the first step.
2006 * The two-step process avoids any difficulties
2007 * which could arise by holding the instance data
2008 * lock with simultaneous devfs operations.
2010 wargs
= kmem_zalloc(sizeof (*wargs
), KM_SLEEP
);
2012 wargs
->wa_drvname
= drvname
;
2013 list_create(&wargs
->wa_pathlist
,
2014 sizeof (struct path_elem
), offsetof(struct path_elem
, pe_node
));
2016 (void) e_ddi_walk_instances(modctl_inst_walker
, (void *)wargs
);
2018 for (pe
= list_head(&wargs
->wa_pathlist
); pe
!= NULL
;
2019 pe
= list_next(&wargs
->wa_pathlist
, pe
)) {
2020 err
= devfs_remdrv_cleanup((const char *)pe
->pe_dir
,
2021 (const char *)pe
->pe_nodename
);
2026 while ((pe
= list_head(&wargs
->wa_pathlist
)) != NULL
) {
2027 list_remove(&wargs
->wa_pathlist
, pe
);
2028 kmem_free(pe
->pe_dir
, pe
->pe_dirlen
);
2029 kmem_free(pe
, sizeof (*pe
));
2031 kmem_free(wargs
, sizeof (*wargs
));
2034 * Pseudo nodes aren't recorded in the instance database
2035 * so any such nodes need to be handled separately.
2037 err
= devfs_remdrv_cleanup("pseudo", (const char *)drvname
);
2041 kmem_free(drvname
, MAXMODCONFNAME
);
2046 * Perform a cleanup of non-existent /devices attribute nodes,
2047 * similar to rem_drv -C, but for all drivers/devices.
2048 * This is also optional, performed as part of devfsadm -C.
2051 dev_devices_cleanup()
2053 struct walk_args
*wargs
;
2054 struct path_elem
*pe
;
2060 * It's expected that all drivers have been loaded and
2061 * module unloading disabled while performing cleanup.
2063 ASSERT(modunload_disable_count
> 0);
2065 wargs
= kmem_zalloc(sizeof (*wargs
), KM_SLEEP
);
2066 wargs
->wa_drvname
= NULL
;
2067 list_create(&wargs
->wa_pathlist
,
2068 sizeof (struct path_elem
), offsetof(struct path_elem
, pe_node
));
2070 (void) e_ddi_walk_instances(modctl_inst_walker
, (void *)wargs
);
2072 path
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2074 for (pe
= list_head(&wargs
->wa_pathlist
); pe
!= NULL
;
2075 pe
= list_next(&wargs
->wa_pathlist
, pe
)) {
2076 (void) snprintf(path
, MAXPATHLEN
, "%s/%s",
2077 pe
->pe_dir
, pe
->pe_nodename
);
2078 devi
= e_ddi_hold_devi_by_path(path
, 0);
2080 ddi_release_devi(devi
);
2082 err
= devfs_remdrv_cleanup((const char *)pe
->pe_dir
,
2083 (const char *)pe
->pe_nodename
);
2086 "devfs: %s: clean-up error %d\n",
2092 while ((pe
= list_head(&wargs
->wa_pathlist
)) != NULL
) {
2093 list_remove(&wargs
->wa_pathlist
, pe
);
2094 kmem_free(pe
->pe_dir
, pe
->pe_dirlen
);
2095 kmem_free(pe
, sizeof (*pe
));
2097 kmem_free(wargs
, sizeof (*wargs
));
2098 kmem_free(path
, MAXPATHLEN
);
2102 modctl_allocpriv(const char *name
)
2104 char *pstr
= kmem_alloc(PRIVNAME_MAX
, KM_SLEEP
);
2107 if ((error
= copyinstr(name
, pstr
, PRIVNAME_MAX
, 0))) {
2108 kmem_free(pstr
, PRIVNAME_MAX
);
2111 error
= priv_getbyname(pstr
, PRIV_ALLOC
);
2116 kmem_free(pstr
, PRIVNAME_MAX
);
2121 modctl_devexists(const char *upath
, int pathlen
)
2127 * copy in the path, including the terminating null
2130 if (pathlen
<= 1 || pathlen
> MAXPATHLEN
)
2132 path
= kmem_zalloc(pathlen
+ 1, KM_SLEEP
);
2133 if ((ret
= copyinstr(upath
, path
, pathlen
, NULL
)) == 0) {
2134 ret
= sdev_modctl_devexists(path
);
2137 kmem_free(path
, pathlen
+ 1);
2142 modctl_devreaddir(const char *udir
, int udirlen
,
2143 char *upaths
, int64_t *ulensp
)
2146 char **dirlist
= NULL
;
2157 * If upaths is NULL then we are only computing the amount of space
2158 * needed to return the paths, with the value returned in *ulensp. If we
2159 * are copying out paths then we get the amount of space allocated by
2160 * the caller. If the actual space needed for paths is larger, or
2161 * things are changing out from under us, then we return EAGAIN.
2166 if (copyin(ulensp
, &ulens
, sizeof (ulens
)) != 0)
2171 * copyin the /dev path including terminating null
2174 if (udirlen
<= 1 || udirlen
> MAXPATHLEN
)
2176 dir
= kmem_zalloc(udirlen
+ 1, KM_SLEEP
);
2177 if ((ret
= copyinstr(udir
, dir
, udirlen
, NULL
)) != 0)
2180 if ((ret
= sdev_modctl_readdir(dir
, &dirlist
,
2181 &npaths
, &npaths_alloc
, 0)) != 0) {
2182 ASSERT(dirlist
== NULL
);
2187 for (i
= 0; i
< npaths
; i
++) {
2188 lens
+= strlen(dirlist
[i
]) + 1;
2190 lens
++; /* add one for double termination */
2198 paths
= kmem_alloc(lens
, KM_SLEEP
);
2201 for (i
= 0; i
< npaths
; i
++) {
2202 n
= strlen(dirlist
[i
]) + 1;
2203 bcopy(dirlist
[i
], p
, n
);
2208 if (copyout(paths
, upaths
, lens
)) {
2215 /* copy out the amount of space needed to hold the paths */
2216 if (copyout(&lens
, ulensp
, sizeof (lens
)))
2221 sdev_modctl_readdir_free(dirlist
, npaths
, npaths_alloc
);
2223 kmem_free(paths
, lens
);
2224 kmem_free(dir
, udirlen
+ 1);
2229 modctl_devemptydir(const char *udir
, int udirlen
, int *uempty
)
2233 char **dirlist
= NULL
;
2239 * copyin the /dev path including terminating null
2242 if (udirlen
<= 1 || udirlen
> MAXPATHLEN
)
2244 dir
= kmem_zalloc(udirlen
+ 1, KM_SLEEP
);
2245 if ((ret
= copyinstr(udir
, dir
, udirlen
, NULL
)) != 0)
2248 if ((ret
= sdev_modctl_readdir(dir
, &dirlist
,
2249 &npaths
, &npaths_alloc
, 1)) != 0) {
2253 empty
= npaths
? 0 : 1;
2254 if (copyout(&empty
, uempty
, sizeof (empty
)))
2259 sdev_modctl_readdir_free(dirlist
, npaths
, npaths_alloc
);
2260 kmem_free(dir
, udirlen
+ 1);
2265 modctl_hp(int subcmd
, const char *path
, char *cn_name
, uintptr_t arg
,
2269 size_t pathsz
, namesz
;
2270 char *devpath
, *cn_name_str
;
2275 devpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
2276 error
= copyinstr(path
, devpath
, MAXPATHLEN
, &pathsz
);
2278 kmem_free(devpath
, MAXPATHLEN
);
2282 cn_name_str
= kmem_zalloc(MAXNAMELEN
, KM_SLEEP
);
2283 error
= copyinstr(cn_name
, cn_name_str
, MAXNAMELEN
, &namesz
);
2285 kmem_free(devpath
, MAXPATHLEN
);
2286 kmem_free(cn_name_str
, MAXNAMELEN
);
2292 case MODHPOPS_CHANGE_STATE
:
2293 error
= ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE
, devpath
,
2294 cn_name_str
, arg
, (uintptr_t)NULL
);
2296 case MODHPOPS_CREATE_PORT
:
2297 /* Create an empty PORT */
2298 error
= ddihp_modctl(DDI_HPOP_CN_CREATE_PORT
, devpath
,
2299 cn_name_str
, (uintptr_t)NULL
, (uintptr_t)NULL
);
2301 case MODHPOPS_REMOVE_PORT
:
2302 /* Remove an empty PORT */
2303 error
= ddihp_modctl(DDI_HPOP_CN_REMOVE_PORT
, devpath
,
2304 cn_name_str
, (uintptr_t)NULL
, (uintptr_t)NULL
);
2306 case MODHPOPS_BUS_GET
:
2307 error
= ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY
, devpath
,
2308 cn_name_str
, arg
, rval
);
2310 case MODHPOPS_BUS_SET
:
2311 error
= ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY
, devpath
,
2312 cn_name_str
, arg
, rval
);
2319 kmem_free(devpath
, MAXPATHLEN
);
2320 kmem_free(cn_name_str
, MAXNAMELEN
);
2326 modctl_moddevname(int subcmd
, uintptr_t a1
, uintptr_t a2
)
2331 case MODDEVNAME_LOOKUPDOOR
:
2332 error
= devname_filename_register((char *)a1
);
2334 case MODDEVNAME_PROFILE
:
2335 error
= devname_profile_update((char *)a1
, (size_t)a2
);
2337 case MODDEVNAME_RECONFIG
:
2338 i_ddi_set_reconfig();
2340 case MODDEVNAME_SYSAVAIL
:
2341 i_ddi_set_sysavail();
2353 modctl(int cmd
, uintptr_t a1
, uintptr_t a2
, uintptr_t a3
, uintptr_t a4
,
2359 if (secpolicy_modctl(CRED(), cmd
) != 0)
2360 return (set_errno(EPERM
));
2363 case MODLOAD
: /* load a module */
2364 error
= modctl_modload((int)a1
, (char *)a2
, (int *)a3
);
2367 case MODUNLOAD
: /* unload a module */
2368 error
= modctl_modunload((modid_t
)a1
);
2371 case MODINFO
: /* get module status */
2372 error
= modctl_modinfo((modid_t
)a1
, (struct modinfo
*)a2
);
2375 case MODRESERVED
: /* get last major number in range */
2376 error
= modctl_modreserve((modid_t
)a1
, (int *)a2
);
2379 case MODSETMINIROOT
: /* we are running in miniroot */
2384 case MODADDMAJBIND
: /* add major / driver alias bindings */
2385 error
= modctl_add_driver_aliases((int *)a2
);
2388 case MODGETPATHLEN
: /* get modpath length */
2389 error
= modctl_getmodpathlen((int *)a2
);
2392 case MODGETPATH
: /* get modpath */
2393 error
= modctl_getmodpath((char *)a2
);
2396 case MODREADSYSBIND
: /* read system call binding file */
2397 error
= modctl_read_sysbinding_file();
2400 case MODGETMAJBIND
: /* get major number for named device */
2401 error
= modctl_getmaj((char *)a1
, (uint_t
)a2
, (int *)a3
);
2404 case MODGETNAME
: /* get name of device given major number */
2405 error
= modctl_getname((char *)a1
, (uint_t
)a2
, (int *)a3
);
2408 case MODDEVT2INSTANCE
:
2409 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2412 #ifdef _SYSCALL32_IMPL
2417 error
= modctl_devt2instance(dev
, (int *)a2
);
2420 case MODSIZEOF_DEVID
: /* sizeof device id of device given dev_t */
2421 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2424 #ifdef _SYSCALL32_IMPL
2429 error
= modctl_sizeof_devid(dev
, (uint_t
*)a2
);
2432 case MODGETDEVID
: /* get device id of device given dev_t */
2433 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2436 #ifdef _SYSCALL32_IMPL
2441 error
= modctl_get_devid(dev
, (uint_t
)a2
, (ddi_devid_t
)a3
);
2444 case MODSIZEOF_MINORNAME
: /* sizeof minor nm (dev_t,spectype) */
2445 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2446 error
= modctl_sizeof_minorname((dev_t
)a1
, (int)a2
,
2449 #ifdef _SYSCALL32_IMPL
2451 error
= modctl_sizeof_minorname(expldev(a1
), (int)a2
,
2458 case MODGETMINORNAME
: /* get minor name of (dev_t,spectype) */
2459 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2460 error
= modctl_get_minorname((dev_t
)a1
, (int)a2
,
2461 (uint_t
)a3
, (char *)a4
);
2463 #ifdef _SYSCALL32_IMPL
2465 error
= modctl_get_minorname(expldev(a1
), (int)a2
,
2466 (uint_t
)a3
, (char *)a4
);
2471 case MODGETDEVFSPATH_LEN
: /* sizeof path nm of (dev_t,spectype) */
2472 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2473 error
= modctl_devfspath_len((dev_t
)a1
, (int)a2
,
2476 #ifdef _SYSCALL32_IMPL
2478 error
= modctl_devfspath_len(expldev(a1
), (int)a2
,
2485 case MODGETDEVFSPATH
: /* get path name of (dev_t,spec) type */
2486 if (get_udatamodel() == DATAMODEL_NATIVE
) {
2487 error
= modctl_devfspath((dev_t
)a1
, (int)a2
,
2488 (uint_t
)a3
, (char *)a4
);
2490 #ifdef _SYSCALL32_IMPL
2492 error
= modctl_devfspath(expldev(a1
), (int)a2
,
2493 (uint_t
)a3
, (char *)a4
);
2498 case MODGETDEVFSPATH_MI_LEN
: /* sizeof path nm of (major,instance) */
2499 error
= modctl_devfspath_mi_len((major_t
)a1
, (int)a2
,
2503 case MODGETDEVFSPATH_MI
: /* get path name of (major,instance) */
2504 error
= modctl_devfspath_mi((major_t
)a1
, (int)a2
,
2505 (uint_t
)a3
, (char *)a4
);
2510 error
= modctl_modevents((int)a1
, a2
, a3
, a4
, (uint_t
)a5
);
2513 case MODGETFBNAME
: /* get the framebuffer name */
2514 error
= modctl_get_fbname((char *)a1
);
2517 case MODREREADDACF
: /* reread dacf rule database from given file */
2518 error
= modctl_reread_dacf((char *)a1
);
2521 case MODLOADDRVCONF
: /* load driver.conf file for major */
2522 error
= modctl_load_drvconf((major_t
)a1
, (int)a2
);
2525 case MODUNLOADDRVCONF
: /* unload driver.conf file for major */
2526 error
= modctl_unload_drvconf((major_t
)a1
);
2529 case MODREMMAJBIND
: /* remove a major binding */
2530 error
= modctl_rem_major((major_t
)a1
);
2533 case MODREMDRVALIAS
: /* remove a major/alias binding */
2534 error
= modctl_remove_driver_aliases((int *)a2
);
2537 case MODDEVID2PATHS
: /* get paths given devid */
2538 error
= modctl_devid2paths((ddi_devid_t
)a1
, (char *)a2
,
2539 (uint_t
)a3
, (size_t *)a4
, (char *)a5
);
2542 case MODSETDEVPOLICY
: /* establish device policy */
2543 error
= devpolicy_load((int)a1
, (size_t)a2
, (devplcysys_t
*)a3
);
2546 case MODGETDEVPOLICY
: /* get device policy */
2547 error
= devpolicy_get((int *)a1
, (size_t)a2
,
2548 (devplcysys_t
*)a3
);
2552 error
= modctl_allocpriv((const char *)a1
);
2555 case MODGETDEVPOLICYBYNAME
:
2556 error
= devpolicy_getbyname((size_t)a1
,
2557 (devplcysys_t
*)a2
, (char *)a3
);
2560 case MODLOADMINORPERM
:
2561 case MODADDMINORPERM
:
2562 case MODREMMINORPERM
:
2563 error
= modctl_minorperm(cmd
, (char *)a1
, (size_t)a2
);
2566 case MODREMDRVCLEANUP
:
2567 error
= modctl_remdrv_cleanup((const char *)a1
);
2570 case MODDEVEXISTS
: /* non-reconfiguring /dev lookup */
2571 error
= modctl_devexists((const char *)a1
, (size_t)a2
);
2574 case MODDEVREADDIR
: /* non-reconfiguring /dev readdir */
2575 error
= modctl_devreaddir((const char *)a1
, (size_t)a2
,
2576 (char *)a3
, (int64_t *)a4
);
2579 case MODDEVEMPTYDIR
: /* non-reconfiguring /dev emptydir */
2580 error
= modctl_devemptydir((const char *)a1
, (size_t)a2
,
2585 error
= modctl_moddevname((int)a1
, a2
, a3
);
2588 case MODRETIRE
: /* retire device named by physpath a1 */
2589 error
= modctl_retire((char *)a1
, (char *)a2
, (size_t)a3
);
2592 case MODISRETIRED
: /* check if a device is retired. */
2593 error
= modctl_is_retired((char *)a1
, (int *)a2
);
2596 case MODUNRETIRE
: /* unretire device named by physpath a1 */
2597 error
= modctl_unretire((char *)a1
);
2600 case MODHPOPS
: /* hotplug operations */
2601 /* device named by physpath a2 and Connection name a3 */
2602 error
= modctl_hp((int)a1
, (char *)a2
, (char *)a3
, a4
, a5
);
2610 return (error
? set_errno(error
) : 0);
2614 * Calls to kobj_load_module()() are handled off to this routine in a
2618 modload_thread(struct loadmt
*ltp
)
2620 /* load the module and signal the creator of this thread */
2624 mutex_init(&cpr_lk
, NULL
, MUTEX_DEFAULT
, NULL
);
2625 CALLB_CPR_INIT(&cpr_i
, &cpr_lk
, callb_generic_cpr
, "modload");
2626 /* borrow the devi lock from thread which invoked us */
2627 pm_borrow_lock(ltp
->owner
);
2628 ltp
->retval
= kobj_load_module(ltp
->mp
, ltp
->usepath
);
2631 mutex_enter(&cpr_lk
);
2632 CALLB_CPR_EXIT(&cpr_i
);
2633 mutex_destroy(&cpr_lk
);
2638 * load a module, adding a reference if caller specifies rmodp. If rmodp
2639 * is specified then an errno is returned, otherwise a module index is
2640 * returned (-1 on error).
2643 modrload(const char *subdir
, const char *filename
, struct modctl
**rmodp
)
2645 struct modctl
*modp
;
2648 int retval
= EINVAL
;
2652 *rmodp
= NULL
; /* avoid garbage */
2654 if (subdir
!= NULL
) {
2656 * refuse / in filename to prevent "../" escapes.
2658 if (strchr(filename
, '/') != NULL
)
2659 return (rmodp
? retval
: id
);
2662 * allocate enough space for <subdir>/<filename><NULL>
2664 size
= strlen(subdir
) + strlen(filename
) + 2;
2665 fullname
= kmem_zalloc(size
, KM_SLEEP
);
2666 (void) sprintf(fullname
, "%s/%s", subdir
, filename
);
2668 fullname
= (char *)filename
;
2671 modp
= mod_hold_installed_mod(fullname
, 1, 0, &retval
);
2675 /* add mod_ref and return *rmodp */
2676 mutex_enter(&mod_lock
);
2678 mutex_exit(&mod_lock
);
2681 mod_release_mod(modp
);
2682 CPU_STATS_ADDQ(CPU
, sys
, modload
, 1);
2685 done
: if (subdir
!= NULL
)
2686 kmem_free(fullname
, size
);
2687 return (rmodp
? retval
: id
);
2691 * This is the primary kernel interface to load a module. It loads and
2692 * installs the named module. It does not hold mod_ref of the module, so
2693 * a module unload attempt can occur at any time - it is up to the
2694 * _fini/mod_remove implementation to determine if unload will succeed.
2697 modload(const char *subdir
, const char *filename
)
2699 return (modrload(subdir
, filename
, NULL
));
2703 * Load a module using a series of qualified names from most specific to least
2704 * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
2705 * Value returned in *chosen
2706 * foo/bar.baz.1.2.3 3
2711 * Return the module ID on success; -1 if no module was loaded. On success
2712 * and if 'chosen' is not NULL we also return the number of suffices that
2713 * were in the module we chose to load.
2716 modload_qualified(const char *subdir
, const char *p1
,
2717 const char *p2
, const char *delim
, uint_t suffv
[], int suffc
, int *chosen
)
2719 char path
[MOD_MAXPATH
];
2720 size_t n
, resid
= sizeof (path
);
2728 n
= snprintf(p
, resid
, "%s/%s%s%s", subdir
, p1
, delim
, p2
);
2730 n
= snprintf(p
, resid
, "%s/%s", subdir
, p1
);
2737 dotv
= kmem_alloc(sizeof (char *) * (suffc
+ 1), KM_SLEEP
);
2739 for (i
= 0; i
< suffc
; i
++) {
2741 n
= snprintf(p
, resid
, "%s%u", delim
, suffv
[i
]);
2744 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2754 for (i
= suffc
; i
>= 0; i
--) {
2756 mp
= mod_hold_installed_mod(path
, 1, 1, &rc
);
2759 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2761 mod_release_mod(mp
);
2768 kmem_free(dotv
, sizeof (char *) * (suffc
+ 1));
2776 modloadonly(const char *subdir
, const char *filename
)
2778 struct modctl
*modp
;
2783 if (subdir
!= NULL
) {
2785 * allocate enough space for <subdir>/<filename><NULL>
2787 size
= strlen(subdir
) + strlen(filename
) + 2;
2788 fullname
= kmem_zalloc(size
, KM_SLEEP
);
2789 (void) sprintf(fullname
, "%s/%s", subdir
, filename
);
2791 fullname
= (char *)filename
;
2794 modp
= mod_hold_loaded_mod(NULL
, fullname
, &retval
);
2797 mod_release_mod(modp
);
2801 kmem_free(fullname
, size
);
2809 * Try to uninstall and unload a module, removing a reference if caller
2813 modunrload(modid_t id
, struct modctl
**rmodp
, int unload
)
2815 struct modctl
*modp
;
2819 *rmodp
= NULL
; /* avoid garbage */
2821 if ((modp
= mod_hold_by_id((modid_t
)id
)) == NULL
)
2825 mutex_enter(&mod_lock
);
2827 if (modp
->mod_ref
== 0)
2828 mod_uninstall_ref_zero
++;
2829 mutex_exit(&mod_lock
);
2834 retval
= moduninstall(modp
);
2837 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
2838 } else if (retval
== EALREADY
)
2839 retval
= 0; /* already unloaded, not an error */
2843 mod_release_mod(modp
);
2848 * Uninstall and unload a module.
2851 modunload(modid_t id
)
2855 /* synchronize with any active modunload_disable() */
2857 if (ddi_root_node())
2858 (void) devfs_clean(ddi_root_node(), NULL
, 0);
2859 retval
= modunrload(id
, NULL
, 1);
2865 * Return status of a loaded module.
2868 modinfo(modid_t id
, struct modinfo
*modinfop
)
2870 struct modctl
*modp
;
2874 mid
= modinfop
->mi_id
;
2875 if (modinfop
->mi_info
& MI_INFO_ALL
) {
2876 while ((modp
= mod_hold_next_by_id(mid
++)) != NULL
) {
2877 if ((modinfop
->mi_info
& MI_INFO_CNT
) ||
2878 modp
->mod_installed
)
2880 mod_release_mod(modp
);
2885 modp
= mod_hold_by_id(id
);
2888 if (!(modinfop
->mi_info
& MI_INFO_CNT
) &&
2889 (modp
->mod_installed
== 0)) {
2890 mod_release_mod(modp
);
2895 modinfop
->mi_rev
= 0;
2896 modinfop
->mi_state
= 0;
2897 for (i
= 0; i
< MODMAXLINK
; i
++) {
2898 modinfop
->mi_msinfo
[i
].msi_p0
= -1;
2899 modinfop
->mi_msinfo
[i
].msi_linkinfo
[0] = 0;
2901 if (modp
->mod_loaded
) {
2902 modinfop
->mi_state
= MI_LOADED
;
2903 kobj_getmodinfo(modp
->mod_mp
, modinfop
);
2905 if (modp
->mod_installed
) {
2906 modinfop
->mi_state
|= MI_INSTALLED
;
2908 (void) mod_getinfo(modp
, modinfop
);
2911 modinfop
->mi_id
= modp
->mod_id
;
2912 modinfop
->mi_loadcnt
= modp
->mod_loadcnt
;
2913 (void) strcpy(modinfop
->mi_name
, modp
->mod_modname
);
2915 mod_release_mod(modp
);
2919 static char mod_stub_err
[] = "mod_hold_stub: Couldn't load stub module %s";
2920 static char no_err
[] = "No error function for weak stub %s";
2923 * used by the stubs themselves to load and hold a module.
2924 * Returns 0 if the module is successfully held;
2925 * the stub needs to call mod_release_stub().
2926 * -1 if the stub should just call the err_fcn.
2927 * Note that this code is stretched out so that we avoid subroutine calls
2928 * and optimize for the most likely case. That is, the case where the
2929 * module is loaded and installed and not held. In that case we just inc
2930 * the mod_ref count and continue.
2933 mod_hold_stub(struct mod_stub_info
*stub
)
2936 struct mod_modinfo
*mip
;
2938 mip
= stub
->mods_modinfo
;
2940 mutex_enter(&mod_lock
);
2942 /* we do mod_hold_by_modctl inline for speed */
2945 if ((mp
= mip
->mp
) != NULL
) {
2946 if (mp
->mod_busy
== 0) {
2947 if (mp
->mod_installed
) {
2948 /* increment the reference count */
2950 ASSERT(mp
->mod_ref
&& mp
->mod_installed
);
2951 mutex_exit(&mod_lock
);
2955 mp
->mod_inprogress_thread
=
2956 (curthread
== NULL
?
2957 (kthread_id_t
)-1 : curthread
);
2961 * wait one time and then go see if someone
2962 * else has resolved the stub (set mip->mp).
2964 if (mod_hold_by_modctl(mp
,
2965 MOD_WAIT_ONCE
| MOD_LOCK_HELD
))
2966 goto mod_check_again
;
2969 * what we have now may have been unloaded!, in
2970 * that case, mip->mp will be NULL, we'll hit this
2971 * module and load again..
2973 cmn_err(CE_PANIC
, "mod_hold_stub should have blocked");
2975 mutex_exit(&mod_lock
);
2977 /* first time we've hit this module */
2978 mutex_exit(&mod_lock
);
2979 mp
= mod_hold_by_name(mip
->modm_module_name
);
2984 * If we are here, it means that the following conditions
2988 * this thread has set the mp->mod_busy = 1
2989 * mp->mod_installed = 0
2993 ASSERT(mp
->mod_busy
== 1);
2995 if (mp
->mod_installed
== 0) {
2996 /* Module not loaded, if weak stub don't load it */
2997 if (stub
->mods_flag
& MODS_WEAK
) {
2998 if (stub
->mods_errfcn
== NULL
) {
2999 mod_release_mod(mp
);
3000 cmn_err(CE_PANIC
, no_err
,
3001 mip
->modm_module_name
);
3004 /* Not a weak stub so load the module */
3006 if (mod_load(mp
, 1) != 0 || modinstall(mp
) != 0) {
3008 * If mod_load() was successful
3009 * and modinstall() failed, then
3010 * unload the module.
3015 mod_release_mod(mp
);
3016 if (stub
->mods_errfcn
== NULL
) {
3017 cmn_err(CE_PANIC
, mod_stub_err
,
3018 mip
->modm_module_name
);
3027 * At this point module is held and loaded. Release
3028 * the mod_busy and mod_inprogress_thread before
3029 * returning. We actually call mod_release() here so
3030 * that if another stub wants to access this module,
3031 * it can do so. mod_ref is incremented before mod_release()
3032 * is called to prevent someone else from snatching the
3033 * module from this thread.
3035 mutex_enter(&mod_lock
);
3037 ASSERT(mp
->mod_ref
&&
3038 (mp
->mod_loaded
|| (stub
->mods_flag
& MODS_WEAK
)));
3040 mutex_exit(&mod_lock
);
3045 mod_release_stub(struct mod_stub_info
*stub
)
3047 struct modctl
*mp
= stub
->mods_modinfo
->mp
;
3049 /* inline mod_release_mod */
3050 mutex_enter(&mod_lock
);
3051 ASSERT(mp
->mod_ref
&&
3052 (mp
->mod_loaded
|| (stub
->mods_flag
& MODS_WEAK
)));
3054 if (mp
->mod_ref
== 0)
3055 mod_uninstall_ref_zero
++;
3058 cv_broadcast(&mod_cv
);
3060 mutex_exit(&mod_lock
);
3063 static struct modctl
*
3064 mod_hold_loaded_mod(struct modctl
*dep
, char *filename
, int *status
)
3066 struct modctl
*modp
;
3072 modp
= mod_hold_by_name_requisite(dep
, filename
);
3074 retval
= mod_load(modp
, 1);
3076 mod_release_mod(modp
);
3085 * if dep is not NULL, clear the module dependency information.
3086 * This information is set in mod_hold_by_name_common().
3088 if (dep
!= NULL
&& dep
->mod_requisite_loading
!= NULL
) {
3089 ASSERT(dep
->mod_busy
);
3090 dep
->mod_requisite_loading
= NULL
;
3097 * hold, load, and install the named module
3099 static struct modctl
*
3100 mod_hold_installed_mod(char *name
, int usepath
, int forcecheck
, int *r
)
3102 struct modctl
*modp
;
3106 * Verify that that module in question actually exists on disk
3107 * before allocation of module structure by mod_hold_by_name.
3109 if (modrootloaded
&& swaploaded
|| forcecheck
) {
3110 if (!kobj_path_exists(name
, usepath
)) {
3119 modp
= mod_hold_by_name(name
);
3121 retval
= mod_load(modp
, usepath
);
3123 mod_release_mod(modp
);
3127 if ((*r
= modinstall(modp
)) != 0) {
3129 * We loaded it, but failed to _init() it.
3130 * Be kind to developers -- force it
3131 * out of memory now so that the next
3132 * attempt to use the module will cause
3133 * a reload. See 1093793.
3136 mod_release_mod(modp
);
3146 static char mod_excl_msg
[] =
3147 "module %s(%s) is EXCLUDED and will not be loaded\n";
3148 static char mod_init_msg
[] = "loadmodule:%s(%s): _init() error %d\n";
3151 * This routine is needed for dependencies. Users specify dependencies
3152 * by declaring a character array initialized to filenames of dependents.
3153 * So the code that handles dependents deals with filenames (and not
3154 * module names) because that's all it has. We load by filename and once
3155 * we've loaded a file we can get the module name.
3156 * Unfortunately there isn't a single unified filename/modulename namespace.
3159 * We allow the name being looked up to be prepended by an optional
3160 * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
3163 mod_find_by_filename(char *subdir
, char *filename
)
3168 ASSERT(!MUTEX_HELD(&mod_lock
));
3170 sublen
= strlen(subdir
);
3174 mutex_enter(&mod_lock
);
3178 char *mod_filename
= mp
->mod_filename
;
3180 if (strncmp(subdir
, mod_filename
, sublen
) == 0 &&
3181 mod_filename
[sublen
] == '/' &&
3182 strcmp(filename
, &mod_filename
[sublen
+ 1]) == 0) {
3183 mutex_exit(&mod_lock
);
3186 } else if (strcmp(filename
, mp
->mod_filename
) == 0) {
3187 mutex_exit(&mod_lock
);
3190 } while ((mp
= mp
->mod_next
) != &modules
);
3191 mutex_exit(&mod_lock
);
3196 * Check for circular dependencies. This is called from do_dependents()
3197 * in kobj.c. If we are the thread already loading this module, then
3198 * we're trying to load a dependent that we're already loading which
3199 * means the user specified circular dependencies.
3202 mod_circdep(struct modctl
*modp
)
3204 struct modctl
*rmod
;
3206 ASSERT(MUTEX_HELD(&mod_lock
));
3209 * Check the mod_inprogress_thread first.
3210 * mod_inprogress_thread is used in mod_hold_stub()
3211 * directly to improve performance.
3213 if (modp
->mod_inprogress_thread
== curthread
)
3217 * Check the module circular dependencies.
3219 for (rmod
= modp
; rmod
!= NULL
; rmod
= rmod
->mod_requisite_loading
) {
3221 * Check if there is a module circular dependency.
3223 if (rmod
->mod_requisite_loading
== modp
)
3230 mod_getinfo(struct modctl
*modp
, struct modinfo
*modinfop
)
3232 int (*func
)(struct modinfo
*);
3235 ASSERT(modp
->mod_busy
);
3237 /* primary modules don't do getinfo */
3241 func
= (int (*)(struct modinfo
*))kobj_lookup(modp
->mod_mp
, "_info");
3243 if (kobj_addrcheck(modp
->mod_mp
, (caddr_t
)func
)) {
3244 cmn_err(CE_WARN
, "_info() not defined properly in %s",
3245 modp
->mod_filename
);
3247 * The semantics of mod_info(9F) are that 0 is failure
3248 * and non-zero is success.
3252 retval
= (*func
)(modinfop
); /* call _info() function */
3254 if (moddebug
& MODDEBUG_USERDEBUG
)
3255 printf("Returned from _info, retval = %x\n", retval
);
3261 modadd(struct modctl
*mp
)
3263 ASSERT(MUTEX_HELD(&mod_lock
));
3265 mp
->mod_id
= last_module_id
++;
3266 mp
->mod_next
= &modules
;
3267 mp
->mod_prev
= modules
.mod_prev
;
3268 modules
.mod_prev
->mod_next
= mp
;
3269 modules
.mod_prev
= mp
;
3273 static struct modctl
*
3274 allocate_modp(const char *filename
, const char *modname
)
3278 mp
= kobj_zalloc(sizeof (*mp
), KM_SLEEP
);
3279 mp
->mod_modname
= kobj_zalloc(strlen(modname
) + 1, KM_SLEEP
);
3280 (void) strcpy(mp
->mod_modname
, modname
);
3285 * Get the value of a symbol. This is a wrapper routine that
3286 * calls kobj_getsymvalue(). kobj_getsymvalue() may go away but this
3287 * wrapper will prevent callers from noticing.
3290 modgetsymvalue(char *name
, int kernelonly
)
3292 return (kobj_getsymvalue(name
, kernelonly
));
3296 * Get the symbol nearest an address. This is a wrapper routine that
3297 * calls kobj_getsymname(). kobj_getsymname() may go away but this
3298 * wrapper will prevent callers from noticing.
3301 modgetsymname(uintptr_t value
, ulong_t
*offset
)
3303 return (kobj_getsymname(value
, offset
));
3307 * Lookup a symbol in a specified module. These are wrapper routines that
3308 * call kobj_lookup(). kobj_lookup() may go away but these wrappers will
3309 * prevent callers from noticing.
3312 modlookup(const char *modname
, const char *symname
)
3314 struct modctl
*modp
;
3317 if ((modp
= mod_hold_by_name(modname
)) == NULL
)
3319 val
= kobj_lookup(modp
->mod_mp
, symname
);
3320 mod_release_mod(modp
);
3325 modlookup_by_modctl(modctl_t
*modp
, const char *symname
)
3327 ASSERT(modp
->mod_ref
> 0 || modp
->mod_busy
);
3329 return (kobj_lookup(modp
->mod_mp
, symname
));
3333 * Ask the user for the name of the system file and the default path
3342 if ((fd
= kobj_open(systemfile
)) != -1L)
3347 /*CONSTANTCONDITION*/
3349 printf("Name of system file [%s]: ",
3350 systemfile
? systemfile
: "/dev/null");
3352 console_gets(s0
, sizeof (s0
));
3356 else if (strcmp(s0
, "/dev/null") == 0) {
3360 if ((fd
= kobj_open(s0
)) != -1L) {
3366 printf("can't find file %s\n", s0
);
3370 static char loading_msg
[] = "loading '%s' id %d\n";
3371 static char load_msg
[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
3374 * Common code for loading a module (but not installing it).
3375 * Handoff the task of module loading to a separate thread
3376 * with a large stack if possible, since this code may recurse a few times.
3377 * Return zero if there are no errors or an errno value.
3380 mod_load(struct modctl
*mp
, int usepath
)
3383 struct modinfo
*modinfop
= NULL
;
3386 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3387 ASSERT(mp
->mod_busy
);
3392 if (mod_sysctl(SYS_CHECK_EXCLUDE
, mp
->mod_modname
) != 0 ||
3393 mod_sysctl(SYS_CHECK_EXCLUDE
, mp
->mod_filename
) != 0) {
3394 if (moddebug
& MODDEBUG_LOADMSG
) {
3395 printf(mod_excl_msg
, mp
->mod_filename
,
3400 if (moddebug
& MODDEBUG_LOADMSG2
)
3401 printf(loading_msg
, mp
->mod_filename
, mp
->mod_id
);
3403 if (curthread
!= &t0
) {
3405 lt
.usepath
= usepath
;
3406 lt
.owner
= curthread
;
3407 sema_init(<
.sema
, 0, NULL
, SEMA_DEFAULT
, NULL
);
3409 /* create thread to hand of call to */
3410 (void) thread_create(NULL
, DEFAULTSTKSZ
* 2,
3411 modload_thread
, <
, 0, &p0
, TS_RUN
, maxclsyspri
);
3413 /* wait for thread to complete kobj_load_module */
3416 sema_destroy(<
.sema
);
3419 retval
= kobj_load_module(mp
, usepath
);
3422 ASSERT(retval
== 0);
3425 if (moddebug
& MODDEBUG_LOADMSG
) {
3426 printf(load_msg
, mp
->mod_filename
, mp
->mod_id
,
3427 (void *)((struct module
*)mp
->mod_mp
)->text
,
3428 (void *)((struct module
*)mp
->mod_mp
)->data
,
3429 ((struct module
*)mp
->mod_mp
)->text_size
,
3430 ((struct module
*)mp
->mod_mp
)->data_size
);
3434 * XXX - There should be a better way to get this.
3436 modinfop
= kmem_zalloc(sizeof (struct modinfo
), KM_SLEEP
);
3437 modinfop
->mi_info
= MI_INFO_LINKAGE
;
3438 if (mod_getinfo(mp
, modinfop
) == 0)
3439 mp
->mod_linkage
= NULL
;
3441 mp
->mod_linkage
= (void *)modinfop
->mi_base
;
3442 ASSERT(mp
->mod_linkage
->ml_rev
== MODREV_1
);
3445 kmem_free(modinfop
, sizeof (struct modinfo
));
3446 (void) mod_sysctl(SYS_SET_MVAR
, (void *)mp
);
3447 retval
= install_stubs_by_name(mp
, mp
->mod_modname
);
3450 * Now that the module is loaded, we need to give DTrace
3451 * a chance to notify its providers. This is done via
3452 * the dtrace_modload function pointer.
3454 if (strcmp(mp
->mod_modname
, "dtrace") != 0) {
3455 struct modctl
*dmp
= mod_hold_by_name("dtrace");
3457 if (dmp
!= NULL
&& dtrace_modload
!= NULL
)
3458 (*dtrace_modload
)(mp
);
3460 mod_release_mod(dmp
);
3465 * If load failed then we need to release any requisites
3466 * that we had established.
3469 mod_release_requisites(mp
);
3471 if (moddebug
& MODDEBUG_ERRMSG
)
3472 printf("error loading '%s', error %d\n",
3473 mp
->mod_filename
, retval
);
3478 static char unload_msg
[] = "unloading %s, module id %d, loadcnt %d.\n";
3481 mod_unload(struct modctl
*mp
)
3483 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3484 ASSERT(mp
->mod_busy
);
3485 ASSERT((mp
->mod_loaded
&& (mp
->mod_installed
== 0)) &&
3486 ((mp
->mod_prim
== 0) && (mp
->mod_ref
>= 0)));
3488 if (moddebug
& MODDEBUG_LOADMSG
)
3489 printf(unload_msg
, mp
->mod_modname
,
3490 mp
->mod_id
, mp
->mod_loadcnt
);
3493 * If mod_ref is not zero, it means some modules might still refer
3494 * to this module. Then you can't unload this module right now.
3495 * Instead, set 1 to mod_delay_unload to notify the system of
3496 * unloading this module later when it's not required any more.
3498 if (mp
->mod_ref
> 0) {
3499 mp
->mod_delay_unload
= 1;
3500 if (moddebug
& MODDEBUG_LOADMSG2
) {
3501 printf("module %s not unloaded,"
3502 " non-zero reference count (%d)",
3503 mp
->mod_modname
, mp
->mod_ref
);
3508 if (((mp
->mod_loaded
== 0) || mp
->mod_installed
) ||
3509 (mp
->mod_ref
|| mp
->mod_prim
)) {
3511 * A DEBUG kernel would ASSERT panic above, the code is broken
3512 * if we get this warning.
3514 cmn_err(CE_WARN
, "mod_unload: %s in incorrect state: %d %d %d",
3515 mp
->mod_filename
, mp
->mod_installed
, mp
->mod_loaded
,
3520 /* reset stub functions to call the binder again */
3524 * mark module as unloaded before the modctl structure is freed.
3525 * This is required not to reuse the modctl structure before
3526 * the module is marked as unloaded.
3529 mp
->mod_linkage
= NULL
;
3531 /* free the memory */
3532 kobj_unload_module(mp
);
3534 if (mp
->mod_delay_unload
) {
3535 mp
->mod_delay_unload
= 0;
3536 if (moddebug
& MODDEBUG_LOADMSG2
) {
3537 printf("deferred unload of module %s"
3538 " (id %d) successful",
3539 mp
->mod_modname
, mp
->mod_id
);
3543 /* release hold on requisites */
3544 mod_release_requisites(mp
);
3547 * Now that the module is gone, we need to give DTrace a chance to
3548 * remove any probes that it may have had in the module. This is
3549 * done via the dtrace_modunload function pointer.
3551 if (strcmp(mp
->mod_modname
, "dtrace") != 0) {
3552 struct modctl
*dmp
= mod_hold_by_name("dtrace");
3554 if (dmp
!= NULL
&& dtrace_modunload
!= NULL
)
3555 (*dtrace_modunload
)(mp
);
3557 mod_release_mod(dmp
);
3562 modinstall(struct modctl
*mp
)
3567 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3568 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
);
3570 if (mp
->mod_installed
)
3573 * If mod_delay_unload is on, it means the system chose the deferred
3574 * unload for this module. Then you can't install this module until
3575 * it's unloaded from the system.
3577 if (mp
->mod_delay_unload
)
3580 if (moddebug
& MODDEBUG_LOADMSG
)
3581 printf("installing %s, module id %d.\n",
3582 mp
->mod_modname
, mp
->mod_id
);
3584 ASSERT(mp
->mod_mp
!= NULL
);
3585 if (mod_install_requisites(mp
) != 0) {
3587 * Note that we can't call mod_unload(mp) here since
3588 * if modinstall() was called by mod_install_requisites(),
3589 * we won't be able to hold the dependent modules
3590 * (otherwise there would be a deadlock).
3595 if (moddebug
& MODDEBUG_ERRMSG
) {
3596 printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
3597 mp
->mod_filename
, mp
->mod_id
,
3598 (void *)((struct module
*)mp
->mod_mp
)->text
,
3599 (void *)((struct module
*)mp
->mod_mp
)->data
,
3600 ((struct module
*)mp
->mod_mp
)->text_size
,
3601 ((struct module
*)mp
->mod_mp
)->data_size
);
3604 func
= (int (*)())kobj_lookup(mp
->mod_mp
, "_init");
3606 if (kobj_addrcheck(mp
->mod_mp
, (caddr_t
)func
)) {
3607 cmn_err(CE_WARN
, "_init() not defined properly in %s",
3612 if (moddebug
& MODDEBUG_USERDEBUG
) {
3613 printf("breakpoint before calling %s:_init()\n",
3615 if (DEBUGGER_PRESENT
)
3616 debug_enter("_init");
3619 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3620 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
);
3621 val
= (*func
)(); /* call _init */
3623 if (moddebug
& MODDEBUG_USERDEBUG
)
3624 printf("Returned from _init, val = %x\n", val
);
3628 * Set the MODS_INSTALLED flag to enable this module
3632 mp
->mod_installed
= 1;
3633 } else if (moddebug
& MODDEBUG_ERRMSG
)
3634 printf(mod_init_msg
, mp
->mod_filename
, mp
->mod_modname
, val
);
3639 int detach_driver_unconfig
= 0;
3642 detach_driver(char *name
)
3648 * If being called from mod_uninstall_all() then the appropriate
3649 * driver detaches (leaf only) have already been done.
3651 if (mod_in_autounload())
3654 major
= ddi_name_to_major(name
);
3655 if (major
== DDI_MAJOR_T_NONE
)
3658 error
= ndi_devi_unconfig_driver(ddi_root_node(),
3659 NDI_DETACH_DRIVER
| detach_driver_unconfig
, major
);
3660 return (error
== NDI_SUCCESS
? 0 : -1);
3663 static char finiret_msg
[] = "Returned from _fini for %s, status = %x\n";
3666 moduninstall(struct modctl
*mp
)
3671 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3672 ASSERT(mp
->mod_busy
);
3675 * Verify that we need to do something and can uninstall the module.
3677 * If we should not uninstall the module or if the module is not in
3678 * the correct state to start an uninstall we return EBUSY to prevent
3679 * us from progressing to mod_unload. If the module has already been
3680 * uninstalled and unloaded we return EALREADY.
3682 if (mp
->mod_prim
|| mp
->mod_ref
|| mp
->mod_nenabled
!= 0)
3684 if ((mp
->mod_installed
== 0) || (mp
->mod_loaded
== 0))
3688 * To avoid devinfo / module deadlock we must release this module
3689 * prior to initiating the detach_driver, otherwise the detach_driver
3690 * might deadlock on a devinfo node held by another thread
3691 * coming top down and involving the module we have locked.
3693 * When we regrab the module we must reverify that it is OK
3694 * to proceed with the uninstall operation.
3696 mod_release_mod(mp
);
3697 status
= detach_driver(mp
->mod_modname
);
3698 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
3700 /* check detach status and reverify state with lock */
3701 mutex_enter(&mod_lock
);
3702 if ((status
!= 0) || mp
->mod_prim
|| mp
->mod_ref
) {
3703 mutex_exit(&mod_lock
);
3706 if ((mp
->mod_installed
== 0) || (mp
->mod_loaded
== 0)) {
3707 mutex_exit(&mod_lock
);
3710 mutex_exit(&mod_lock
);
3712 if (moddebug
& MODDEBUG_LOADMSG2
)
3713 printf("uninstalling %s\n", mp
->mod_modname
);
3716 * lookup _fini, return EBUSY if not defined.
3718 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
3719 * detach(9E) - it allows bufctl addresses to be resolved.
3721 func
= (int (*)())kobj_lookup(mp
->mod_mp
, "_fini");
3722 if ((func
== NULL
) || (mp
->mod_loadflags
& MOD_NOUNLOAD
) ||
3723 (moddebug
& MODDEBUG_FINI_EBUSY
))
3726 /* verify that _fini is in this module */
3727 if (kobj_addrcheck(mp
->mod_mp
, (caddr_t
)func
)) {
3728 cmn_err(CE_WARN
, "_fini() not defined properly in %s",
3734 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
3735 ASSERT(mp
->mod_busy
&& mp
->mod_loaded
&& mp
->mod_installed
);
3740 /* _fini returned success, the module is no longer installed */
3741 if (moddebug
& MODDEBUG_LOADMSG
)
3742 printf("uninstalled %s\n", mp
->mod_modname
);
3745 * Even though we only set mod_installed to zero here, a zero
3746 * return value means we are committed to a code path were
3747 * mod_loaded will also end up as zero - we have no other
3748 * way to get the module data and bss back to the pre _init
3749 * state except a reload. To ensure this, after return,
3750 * mod_busy must stay set until mod_loaded is cleared.
3752 mp
->mod_installed
= 0;
3755 * Clear the MODS_INSTALLED flag not to call functions
3756 * in the module directly from now on.
3758 uninstall_stubs(mp
);
3760 if (moddebug
& MODDEBUG_USERDEBUG
)
3761 printf(finiret_msg
, mp
->mod_filename
, status
);
3763 * By definition _fini is only allowed to return EBUSY or the
3764 * result of mod_remove (EBUSY or EINVAL). In the off chance
3765 * that a driver returns EALREADY we convert this to EINVAL
3766 * since to our caller EALREADY means module was already
3769 if (status
== EALREADY
)
3777 * Uninstall all modules.
3780 mod_uninstall_all(void)
3786 /* synchronize with any active modunload_disable() */
3789 /* mark this thread as doing autounloading */
3790 (void) tsd_set(mod_autounload_key
, (void *)1);
3792 (void) devfs_clean(ddi_root_node(), NULL
, 0);
3793 (void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH
);
3796 * Loop up to max times if we keep producing unreferenced modules.
3797 * A new unreferenced module is an opportunity to unload.
3799 for (pass
= 0; pass
< mod_uninstall_pass_max
; pass
++) {
3801 /* zero count of modules that go unreferenced during pass */
3802 mod_uninstall_ref_zero
= 0;
3805 while ((mp
= mod_hold_next_by_id(modid
)) != NULL
) {
3809 * Skip modules with the MOD_NOAUTOUNLOAD flag set
3811 if (mp
->mod_loadflags
& MOD_NOAUTOUNLOAD
) {
3812 mod_release_mod(mp
);
3816 if (moduninstall(mp
) == 0) {
3818 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
3820 mod_release_mod(mp
);
3823 /* break if no modules went unreferenced during pass */
3824 if (mod_uninstall_ref_zero
== 0)
3827 if (pass
>= mod_uninstall_pass_max
)
3828 mod_uninstall_pass_exc
++;
3830 (void) tsd_set(mod_autounload_key
, NULL
);
3834 /* wait for unloads that have begun before registering disable */
3836 modunload_disable(void)
3838 mutex_enter(&modunload_wait_mutex
);
3839 while (modunload_active_count
) {
3841 cv_wait(&modunload_wait_cv
, &modunload_wait_mutex
);
3844 modunload_disable_count
++;
3845 mutex_exit(&modunload_wait_mutex
);
3848 /* mark end of disable and signal waiters */
3850 modunload_enable(void)
3852 mutex_enter(&modunload_wait_mutex
);
3853 modunload_disable_count
--;
3854 if ((modunload_disable_count
== 0) && modunload_wait
)
3855 cv_broadcast(&modunload_wait_cv
);
3856 mutex_exit(&modunload_wait_mutex
);
3859 /* wait for disables to complete before begining unload */
3863 mutex_enter(&modunload_wait_mutex
);
3864 while (modunload_disable_count
) {
3866 cv_wait(&modunload_wait_cv
, &modunload_wait_mutex
);
3869 modunload_active_count
++;
3870 mutex_exit(&modunload_wait_mutex
);
3873 /* mark end of unload and signal waiters */
3877 mutex_enter(&modunload_wait_mutex
);
3878 modunload_active_count
--;
3879 if ((modunload_active_count
== 0) && modunload_wait
)
3880 cv_broadcast(&modunload_wait_cv
);
3881 mutex_exit(&modunload_wait_mutex
);
3885 mod_uninstall_daemon(void)
3887 callb_cpr_t cprinfo
;
3890 mod_aul_thread
= curthread
;
3892 CALLB_CPR_INIT(&cprinfo
, &mod_uninstall_lock
, callb_generic_cpr
, "mud");
3894 mutex_enter(&mod_uninstall_lock
);
3895 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
3897 * In DEBUG kernels, unheld drivers are uninstalled periodically
3898 * every mod_uninstall_interval seconds. Periodic uninstall can
3899 * be disabled by setting mod_uninstall_interval to 0 which is
3900 * the default for a non-DEBUG kernel.
3902 if (mod_uninstall_interval
) {
3903 ticks
= drv_usectohz(mod_uninstall_interval
* 1000000);
3904 (void) cv_reltimedwait(&mod_uninstall_cv
,
3905 &mod_uninstall_lock
, ticks
, TR_CLOCK_TICK
);
3907 cv_wait(&mod_uninstall_cv
, &mod_uninstall_lock
);
3910 * The whole daemon is safe for CPR except we don't want
3911 * the daemon to run if FREEZE is issued and this daemon
3912 * wakes up from the cv_wait above. In this case, it'll be
3913 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
3915 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
3916 * mod_uninstall_lock is used to protect cprinfo and
3917 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
3920 CALLB_CPR_SAFE_END(&cprinfo
, &mod_uninstall_lock
);
3921 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
3922 mutex_exit(&mod_uninstall_lock
);
3923 if ((modunload_disable_count
== 0) &&
3924 ((moddebug
& MODDEBUG_NOAUTOUNLOAD
) == 0)) {
3925 mod_uninstall_all();
3931 * Unload all uninstalled modules.
3936 mutex_enter(&mod_uninstall_lock
);
3937 cv_broadcast(&mod_uninstall_cv
);
3938 mutex_exit(&mod_uninstall_lock
);
3942 * Hold the specified module. This is the module holding primitive.
3944 * If MOD_LOCK_HELD then the caller already holds the mod_lock.
3947 * 0 ==> the module is held
3948 * 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
3949 * to determine how to retry.
3952 mod_hold_by_modctl(struct modctl
*mp
, int f
)
3954 ASSERT((f
& (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)) &&
3955 ((f
& (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)) !=
3956 (MOD_WAIT_ONCE
| MOD_WAIT_FOREVER
)));
3957 ASSERT((f
& (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)) &&
3958 ((f
& (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)) !=
3959 (MOD_LOCK_HELD
| MOD_LOCK_NOT_HELD
)));
3960 ASSERT((f
& MOD_LOCK_NOT_HELD
) || MUTEX_HELD(&mod_lock
));
3962 if (f
& MOD_LOCK_NOT_HELD
)
3963 mutex_enter(&mod_lock
);
3965 while (mp
->mod_busy
) {
3967 cv_wait(&mod_cv
, &mod_lock
);
3969 * Module may be unloaded by daemon.
3970 * Nevertheless, modctl structure is still in linked list
3971 * (i.e., off &modules), not freed!
3972 * Caller is not supposed to assume "mp" is valid, but there
3973 * is no reasonable way to detect this but using
3974 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
3975 * (or similar check depending on calling context)
3976 * DON'T free modctl structure, it will be very very
3979 if (f
& MOD_WAIT_ONCE
) {
3980 if (f
& MOD_LOCK_NOT_HELD
)
3981 mutex_exit(&mod_lock
);
3982 return (1); /* caller decides how to retry */
3987 mp
->mod_inprogress_thread
=
3988 (curthread
== NULL
? (kthread_id_t
)-1 : curthread
);
3990 if (f
& MOD_LOCK_NOT_HELD
)
3991 mutex_exit(&mod_lock
);
3995 static struct modctl
*
3996 mod_hold_by_name_common(struct modctl
*dep
, const char *filename
)
3998 const char *modname
;
4000 char *curname
, *newname
;
4003 mutex_enter(&mod_lock
);
4005 if ((modname
= strrchr(filename
, '/')) == NULL
)
4012 if (strcmp(modname
, mp
->mod_modname
) == 0) {
4016 } while ((mp
= mp
->mod_next
) != &modules
);
4019 mp
= allocate_modp(filename
, modname
);
4024 * if dep is not NULL, set the mp in mod_requisite_loading for
4025 * the module circular dependency check. This field is used in
4026 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
4029 ASSERT(dep
->mod_busy
&& dep
->mod_requisite_loading
== NULL
);
4030 dep
->mod_requisite_loading
= mp
;
4034 * If the module was held, then it must be us who has it held.
4036 if (mod_circdep(mp
))
4039 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4042 * If the name hadn't been set or has changed, allocate
4043 * space and set it. Free space used by previous name.
4045 * Do not change the name of primary modules, for primary
4046 * modules the mod_filename was allocated in standalone mode:
4047 * it is illegal to kobj_alloc in standalone mode and kobj_free
4048 * in non-standalone mode.
4050 curname
= mp
->mod_filename
;
4051 if (curname
== NULL
||
4052 ((mp
->mod_prim
== 0) &&
4053 (curname
!= filename
) &&
4054 (modname
!= filename
) &&
4055 (strcmp(curname
, filename
) != 0))) {
4056 newname
= kobj_zalloc(strlen(filename
) + 1, KM_SLEEP
);
4057 (void) strcpy(newname
, filename
);
4058 mp
->mod_filename
= newname
;
4059 if (curname
!= NULL
)
4060 kobj_free(curname
, strlen(curname
) + 1);
4064 mutex_exit(&mod_lock
);
4065 if (mp
&& moddebug
& MODDEBUG_LOADMSG2
)
4066 printf("Holding %s\n", mp
->mod_filename
);
4067 if (mp
== NULL
&& moddebug
& MODDEBUG_LOADMSG2
)
4068 printf("circular dependency loading %s\n", filename
);
4072 static struct modctl
*
4073 mod_hold_by_name_requisite(struct modctl
*dep
, char *filename
)
4075 return (mod_hold_by_name_common(dep
, filename
));
4079 mod_hold_by_name(const char *filename
)
4081 return (mod_hold_by_name_common(NULL
, filename
));
4085 mod_hold_by_id(modid_t modid
)
4090 mutex_enter(&mod_lock
);
4093 if (mp
->mod_id
== modid
) {
4097 } while ((mp
= mp
->mod_next
) != &modules
);
4099 if ((found
== 0) || mod_circdep(mp
))
4102 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4104 mutex_exit(&mod_lock
);
4108 static struct modctl
*
4109 mod_hold_next_by_id(modid_t modid
)
4117 mutex_enter(&mod_lock
);
4121 if (mp
->mod_id
> modid
) {
4125 } while ((mp
= mp
->mod_next
) != &modules
);
4127 if ((found
== 0) || mod_circdep(mp
))
4130 (void) mod_hold_by_modctl(mp
, MOD_WAIT_FOREVER
| MOD_LOCK_HELD
);
4132 mutex_exit(&mod_lock
);
4137 mod_release(struct modctl
*mp
)
4139 ASSERT(MUTEX_HELD(&mod_lock
));
4140 ASSERT(mp
->mod_busy
);
4143 mp
->mod_inprogress_thread
= NULL
;
4146 cv_broadcast(&mod_cv
);
4151 mod_release_mod(struct modctl
*mp
)
4153 if (moddebug
& MODDEBUG_LOADMSG2
)
4154 printf("Releasing %s\n", mp
->mod_filename
);
4155 mutex_enter(&mod_lock
);
4157 mutex_exit(&mod_lock
);
4161 mod_name_to_modid(char *filename
)
4166 mutex_enter(&mod_lock
);
4168 if ((modname
= strrchr(filename
, '/')) == NULL
)
4175 if (strcmp(modname
, mp
->mod_modname
) == 0) {
4176 mutex_exit(&mod_lock
);
4177 return (mp
->mod_id
);
4179 } while ((mp
= mp
->mod_next
) != &modules
);
4181 mutex_exit(&mod_lock
);
4187 mod_remove_by_name(char *name
)
4192 mp
= mod_hold_by_name(name
);
4197 if (mp
->mod_loadflags
& MOD_NOAUTOUNLOAD
) {
4199 * Do not unload forceloaded modules
4201 mod_release_mod(mp
);
4205 if ((retval
= moduninstall(mp
)) == 0) {
4207 CPU_STATS_ADDQ(CPU
, sys
, modunload
, 1);
4208 } else if (retval
== EALREADY
)
4209 retval
= 0; /* already unloaded, not an error */
4210 mod_release_mod(mp
);
4215 * Record that module "dep" is dependent on module "on_mod."
4218 mod_make_requisite(struct modctl
*dependent
, struct modctl
*on_mod
)
4220 struct modctl_list
**pmlnp
; /* previous next pointer */
4221 struct modctl_list
*mlp
;
4222 struct modctl_list
*new;
4224 ASSERT(dependent
->mod_busy
&& on_mod
->mod_busy
);
4225 mutex_enter(&mod_lock
);
4228 * Search dependent's requisite list to see if on_mod is recorded.
4229 * List is ordered by id.
4231 for (pmlnp
= &dependent
->mod_requisites
, mlp
= *pmlnp
;
4232 mlp
; pmlnp
= &mlp
->modl_next
, mlp
= *pmlnp
)
4233 if (mlp
->modl_modp
->mod_id
>= on_mod
->mod_id
)
4236 /* Create and insert if not already recorded */
4237 if ((mlp
== NULL
) || (mlp
->modl_modp
->mod_id
!= on_mod
->mod_id
)) {
4238 new = kobj_zalloc(sizeof (*new), KM_SLEEP
);
4239 new->modl_modp
= on_mod
;
4240 new->modl_next
= mlp
;
4244 * Increment the mod_ref count in our new requisite module.
4245 * This is what keeps a module that has other modules
4246 * which are dependent on it from being uninstalled and
4247 * unloaded. "on_mod"'s mod_ref count decremented in
4248 * mod_release_requisites when the "dependent" module
4249 * unload is complete. "on_mod" must be loaded, but may not
4253 ASSERT(on_mod
->mod_ref
&& on_mod
->mod_loaded
);
4256 mutex_exit(&mod_lock
);
4260 * release the hold associated with mod_make_requisite mod_ref++
4261 * as part of unload.
4264 mod_release_requisites(struct modctl
*modp
)
4266 struct modctl_list
*modl
;
4267 struct modctl_list
*next
;
4269 struct modctl_list
*start
= NULL
, *mod_garbage
;
4271 ASSERT(!quiesce_active
);
4272 ASSERT(modp
->mod_busy
);
4273 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
4275 mutex_enter(&mod_lock
); /* needed for manipulation of req */
4276 for (modl
= modp
->mod_requisites
; modl
; modl
= next
) {
4277 next
= modl
->modl_next
;
4278 req
= modl
->modl_modp
;
4279 ASSERT(req
->mod_ref
>= 1 && req
->mod_loaded
);
4281 if (req
->mod_ref
== 0)
4282 mod_uninstall_ref_zero
++;
4285 * Check if the module has to be unloaded or not.
4287 if (req
->mod_ref
== 0 && req
->mod_delay_unload
) {
4288 struct modctl_list
*new;
4290 * Allocate the modclt_list holding the garbage
4291 * module which should be unloaded later.
4293 new = kobj_zalloc(sizeof (struct modctl_list
),
4295 new->modl_modp
= req
;
4298 mod_garbage
= start
= new;
4300 mod_garbage
->modl_next
= new;
4305 /* free the list as we go */
4306 kobj_free(modl
, sizeof (*modl
));
4308 modp
->mod_requisites
= NULL
;
4309 mutex_exit(&mod_lock
);
4312 * Unload the garbage modules.
4314 for (mod_garbage
= start
; mod_garbage
!= NULL
; /* nothing */) {
4315 struct modctl_list
*old
= mod_garbage
;
4316 struct modctl
*mp
= mod_garbage
->modl_modp
;
4320 * Hold this module until it's unloaded completely.
4322 (void) mod_hold_by_modctl(mp
,
4323 MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
4325 * Check if the module is not unloaded yet and nobody requires
4326 * the module. If it's unloaded already or somebody still
4327 * requires the module, don't unload it now.
4329 if (mp
->mod_loaded
&& mp
->mod_ref
== 0)
4331 ASSERT((mp
->mod_loaded
== 0 && mp
->mod_delay_unload
== 0) ||
4333 mod_release_mod(mp
);
4335 mod_garbage
= mod_garbage
->modl_next
;
4336 kobj_free(old
, sizeof (struct modctl_list
));
4341 * Process dependency of the module represented by "dep" on the
4342 * module named by "on."
4344 * Called from kobj_do_dependents() to load a module "on" on which
4348 mod_load_requisite(struct modctl
*dep
, char *on
)
4350 struct modctl
*on_mod
;
4353 if ((on_mod
= mod_hold_loaded_mod(dep
, on
, &retval
)) != NULL
) {
4354 mod_make_requisite(dep
, on_mod
);
4355 } else if (moddebug
& MODDEBUG_ERRMSG
) {
4356 printf("error processing %s on which module %s depends\n",
4357 on
, dep
->mod_modname
);
4363 mod_install_requisites(struct modctl
*modp
)
4365 struct modctl_list
*modl
;
4369 ASSERT(MUTEX_NOT_HELD(&mod_lock
));
4370 ASSERT(modp
->mod_busy
);
4372 for (modl
= modp
->mod_requisites
; modl
; modl
= modl
->modl_next
) {
4373 req
= modl
->modl_modp
;
4374 (void) mod_hold_by_modctl(req
,
4375 MOD_WAIT_FOREVER
| MOD_LOCK_NOT_HELD
);
4376 status
= modinstall(req
);
4377 mod_release_mod(req
);
4386 * returns 1 if this thread is doing autounload, 0 otherwise.
4387 * see mod_uninstall_all.
4392 return ((int)(uintptr_t)tsd_get(mod_autounload_key
));
4396 * gmatch adapted from libc, stripping the wchar stuff
4398 #define popchar(p, c) { \
4406 gmatch(const char *s
, const char *p
)
4409 int ok
, lc
, notflag
;
4414 return (sc
== c
); /* nothing matches nothing */
4418 /* skip to quoted character */
4423 /* straight comparison */
4429 /* first char matches, move to remainder */
4430 return (sc
!= '\0' ? gmatch(s
, p
) : 0);
4437 /* * matches everything */
4441 /* undo skip at the beginning & iterate over substrings */
4451 /* match any char within [] */
4455 ok
= lc
= notflag
= 0;
4464 if (c
== '-' && lc
&& *p
!= ']') {
4465 /* test sc against range [c1-c2] */
4472 /* return 0 on mismatch */
4473 if (lc
<= sc
&& sc
<= c
)
4476 } else if (lc
<= sc
&& sc
<= c
) {
4479 /* keep going, may get a match next */
4480 } else if (c
== '\\') {
4481 /* skip to quoted character */
4489 } else if (sc
== lc
) {
4495 /* recurse on remainder of string */
4496 return (ok
? gmatch(s
, p
) : 0);
4503 * Get default perm for device from /etc/minor_perm. Return 0 if match found.
4505 * Pure wild-carded patterns are handled separately so the ordering of
4506 * these patterns doesn't matter. We're still dependent on ordering
4507 * however as the first matching entry is the one returned.
4508 * Not ideal but all existing examples and usage do imply this
4509 * ordering implicitly.
4511 * Drivers using the clone driver are always good for some entertainment.
4512 * Clone nodes under pseudo have the form clone@0:<driver>. Some minor
4513 * perm entries have the form clone:<driver>, others use <driver>:*
4514 * Examples are clone:llc1 vs. llc2:*, for example.
4516 * Minor perms in the clone:<driver> form are mapped to the drivers's
4517 * mperm list, not the clone driver, as wildcard entries for clone
4518 * reference only. In other words, a clone wildcard will match
4519 * references for clone@0:<driver> but never <driver>@<minor>.
4521 * Additional minor perms in the standard form are also supported,
4522 * for mixed usage, ie a node with an entry clone:<driver> could
4523 * provide further entries <driver>:<minor>.
4525 * Finally, some uses of clone use an alias as the minor name rather
4526 * than the driver name, with the alias as the minor perm entry.
4527 * This case is handled by attaching the driver to bring its
4528 * minor list into existence, then discover the alias via DDI_ALIAS.
4529 * The clone device's minor perm list can then be searched for
4534 dev_alias_minorperm(dev_info_t
*dip
, char *minor_name
, mperm_t
*rmp
)
4537 struct devnames
*dnp
;
4542 struct ddi_minor_data
*dmd
;
4544 major
= ddi_name_to_major(minor_name
);
4546 ASSERT(dip
== clone_dip
);
4547 ASSERT(major
!= DDI_MAJOR_T_NONE
);
4550 * Attach the driver named by the minor node, then
4551 * search its first instance's minor list for an
4554 if (ddi_hold_installed_driver(major
) == NULL
)
4557 dnp
= &devnamesp
[major
];
4558 LOCK_DEV_OPS(&dnp
->dn_lock
);
4560 if ((cdevi
= dnp
->dn_head
) != NULL
) {
4561 ndi_devi_enter(cdevi
, &circ
);
4562 for (dmd
= DEVI(cdevi
)->devi_minor
; dmd
; dmd
= dmd
->next
) {
4563 if (dmd
->type
== DDM_ALIAS
) {
4564 alias
= i_ddi_strdup(dmd
->ddm_name
, KM_SLEEP
);
4568 ndi_devi_exit(cdevi
, circ
);
4571 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4572 ddi_rele_driver(major
);
4574 if (alias
== NULL
) {
4575 if (moddebug
& MODDEBUG_MINORPERM
)
4576 cmn_err(CE_CONT
, "dev_minorperm: "
4577 "no alias for %s\n", minor_name
);
4581 major
= ddi_driver_major(clone_dip
);
4582 dnp
= &devnamesp
[major
];
4583 LOCK_DEV_OPS(&dnp
->dn_lock
);
4586 * Go through the clone driver's mperm list looking
4587 * for a match for the specified alias.
4589 for (mp
= dnp
->dn_mperm
; mp
; mp
= mp
->mp_next
) {
4590 if (strcmp(alias
, mp
->mp_minorname
) == 0) {
4596 if (moddebug
& MODDEBUG_MP_MATCH
) {
4598 "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
4599 minor_name
, alias
, mp
->mp_mode
,
4600 mp
->mp_uid
, mp
->mp_gid
);
4602 rmp
->mp_uid
= mp
->mp_uid
;
4603 rmp
->mp_gid
= mp
->mp_gid
;
4604 rmp
->mp_mode
= mp
->mp_mode
;
4606 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4608 kmem_free(alias
, strlen(alias
)+1);
4610 return (mp
== NULL
);
4614 dev_minorperm(dev_info_t
*dip
, char *name
, mperm_t
*rmp
)
4618 struct devnames
*dnp
;
4622 if (!minorperm_loaded
) {
4623 if (moddebug
& MODDEBUG_MINORPERM
)
4625 "%s: minor perm not yet loaded\n", name
);
4629 minor_name
= strchr(name
, ':');
4630 if (minor_name
== NULL
)
4635 * If it's the clone driver, search the driver as named
4636 * by the minor. All clone minor perm entries other than
4637 * alias nodes are actually installed on the real driver's list.
4639 if (dip
== clone_dip
) {
4640 major
= ddi_name_to_major(minor_name
);
4641 if (major
== DDI_MAJOR_T_NONE
) {
4642 if (moddebug
& MODDEBUG_MINORPERM
)
4643 cmn_err(CE_CONT
, "dev_minorperm: "
4644 "%s: no such driver\n", minor_name
);
4649 major
= ddi_driver_major(dip
);
4650 ASSERT(major
!= DDI_MAJOR_T_NONE
);
4653 dnp
= &devnamesp
[major
];
4654 LOCK_DEV_OPS(&dnp
->dn_lock
);
4657 * Go through the driver's mperm list looking for
4658 * a match for the specified minor. If there's
4659 * no matching pattern, use the wild card.
4660 * Defer to the clone wild for clone if specified,
4661 * otherwise fall back to the normal form.
4663 for (mp
= dnp
->dn_mperm
; mp
; mp
= mp
->mp_next
) {
4664 if (gmatch(minor_name
, mp
->mp_minorname
) != 0) {
4670 mp
= dnp
->dn_mperm_clone
;
4672 mp
= dnp
->dn_mperm_wild
;
4676 if (moddebug
& MODDEBUG_MP_MATCH
) {
4678 "minor perm defaults: %s %s 0%o %d %d\n",
4679 name
, mp
->mp_minorname
, mp
->mp_mode
,
4680 mp
->mp_uid
, mp
->mp_gid
);
4682 rmp
->mp_uid
= mp
->mp_uid
;
4683 rmp
->mp_gid
= mp
->mp_gid
;
4684 rmp
->mp_mode
= mp
->mp_mode
;
4686 UNLOCK_DEV_OPS(&dnp
->dn_lock
);
4689 * If no match can be found for a clone node,
4690 * search for a possible match for an alias.
4691 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
4692 * with minor perm entry clone:ptmx.
4694 if (mp
== NULL
&& is_clone
) {
4695 return (dev_alias_minorperm(dip
, minor_name
, rmp
));
4698 return (mp
== NULL
);
4702 * dynamicaly reference load a dl module/library, returning handle
4706 ddi_modopen(const char *modname
, int mode
, int *errnop
)
4711 struct modctl
*hmodp
= NULL
;
4712 int retval
= EINVAL
;
4714 ASSERT(modname
&& (mode
== KRTLD_MODE_FIRST
));
4715 if ((modname
== NULL
) || (mode
!= KRTLD_MODE_FIRST
))
4718 /* find last '/' in modname */
4719 mod
= strrchr(modname
, '/');
4722 /* for subdir string without modification to argument */
4724 subdirlen
= mod
- modname
;
4725 subdir
= kmem_alloc(subdirlen
, KM_SLEEP
);
4726 (void) strlcpy(subdir
, modname
, subdirlen
);
4730 mod
= (char *)modname
;
4733 /* reference load with errno return value */
4734 retval
= modrload(subdir
, mod
, &hmodp
);
4737 kmem_free(subdir
, subdirlen
);
4742 if (moddebug
& MODDEBUG_DDI_MOD
)
4743 printf("ddi_modopen %s mode %x: %s %p %d\n",
4744 modname
? modname
: "<unknown>", mode
,
4745 hmodp
? hmodp
->mod_filename
: "<unknown>",
4746 (void *)hmodp
, retval
);
4748 return ((ddi_modhandle_t
)hmodp
);
4751 /* lookup "name" in open dl module/library */
4753 ddi_modsym(ddi_modhandle_t h
, const char *name
, int *errnop
)
4755 struct modctl
*hmodp
= (struct modctl
*)h
;
4759 ASSERT(hmodp
&& name
&& hmodp
->mod_installed
&& (hmodp
->mod_ref
>= 1));
4760 if ((hmodp
== NULL
) || (name
== NULL
) ||
4761 (hmodp
->mod_installed
== 0) || (hmodp
->mod_ref
< 1)) {
4765 f
= (void *)kobj_lookup(hmodp
->mod_mp
, (char *)name
);
4772 if (moddebug
& MODDEBUG_DDI_MOD
)
4773 printf("ddi_modsym in %s of %s: %d %p\n",
4774 hmodp
? hmodp
->mod_modname
: "<unknown>",
4775 name
? name
: "<unknown>", retval
, f
);
4782 /* dynamic (un)reference unload of an open dl module/library */
4784 ddi_modclose(ddi_modhandle_t h
)
4786 struct modctl
*hmodp
= (struct modctl
*)h
;
4787 struct modctl
*modp
= NULL
;
4790 ASSERT(hmodp
&& hmodp
->mod_installed
&& (hmodp
->mod_ref
>= 1));
4791 if ((hmodp
== NULL
) ||
4792 (hmodp
->mod_installed
== 0) || (hmodp
->mod_ref
< 1)) {
4797 retval
= modunrload(hmodp
->mod_id
, &modp
, ddi_modclose_unload
);
4798 if (retval
== EBUSY
)
4799 retval
= 0; /* EBUSY is not an error */
4802 ASSERT(hmodp
== modp
);
4807 out
: if (moddebug
& MODDEBUG_DDI_MOD
)
4808 printf("ddi_modclose %s: %d\n",
4809 hmodp
? hmodp
->mod_modname
: "<unknown>", retval
);