Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / modctl.c
blob26d3a4a6edd4fdab39f25ec4e6a604af67f3582f
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
31 #include <sys/user.h>
32 #include <sys/systm.h>
33 #include <sys/exec.h>
34 #include <sys/file.h>
35 #include <sys/stat.h>
36 #include <sys/conf.h>
37 #include <sys/time.h>
38 #include <sys/reboot.h>
39 #include <sys/fs/ufs_fsdir.h>
40 #include <sys/kmem.h>
41 #include <sys/sysconf.h>
42 #include <sys/cmn_err.h>
43 #include <sys/ddi.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>
51 #include <sys/kdi.h>
53 #include <sys/devpolicy.h>
54 #include <sys/modctl.h>
55 #include <sys/kobj.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>
69 #include <sys/vfs.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.
105 struct loadmt {
106 ksema_t sema;
107 struct modctl *mp;
108 int usepath;
109 kthread_t *owner;
110 int retval;
113 static void modload_thread(struct loadmt *);
115 kcondvar_t mod_cv;
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;
125 int modunload_wait;
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 */
136 int last_module_id;
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 */
159 extern int obpdebug;
161 #define DEBUGGER_PRESENT ((boothowto & RB_DEBUG) || (obpdebug != 0))
163 static int minorperm_loaded = 0;
165 void
166 mod_setup(void)
168 struct sysent *callp;
169 int callnum, exectype;
170 int num_devs;
171 int i;
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.
201 (void) kobj_sync();
203 if (boothowto & RB_DEBUG)
204 kdi_dvec_modavail();
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).
219 mod_hash_init();
220 dacf_init();
223 * Start up IP policy framework (ipp).
225 ipp_init();
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) {
234 callp->sy_lock =
235 kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
236 rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
237 } else {
238 callp->sy_flags &= ~SE_LOADABLE;
239 callp->sy_callc = nosys;
241 #ifdef DEBUG
242 } else {
244 * Do some sanity checks on the sysent table
246 switch (callp->sy_flags & SE_RVAL_MASK) {
247 case SE_32RVAL1:
248 /* only r_val1 returned */
249 case SE_32RVAL1 | SE_32RVAL2:
250 /* r_val1 and r_val2 returned */
251 case SE_64RVAL:
252 /* 64-bit rval returned */
253 break;
254 default:
255 cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
256 callnum, callp->sy_flags);
258 #endif
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) {
270 callp->sy_lock =
271 kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
272 rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
273 } else {
274 callp->sy_flags &= ~SE_LOADABLE;
275 callp->sy_callc = nosys;
277 #ifdef DEBUG
278 } else {
280 * Do some sanity checks on the sysent table
282 switch (callp->sy_flags & SE_RVAL_MASK) {
283 case SE_32RVAL1:
284 /* only r_val1 returned */
285 case SE_32RVAL1 | SE_32RVAL2:
286 /* r_val1 and r_val2 returned */
287 case SE_64RVAL:
288 /* 64-bit rval returned */
289 break;
290 default:
291 cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
292 callnum, callp->sy_flags);
293 goto skip;
297 * Cross-check the native and compatibility tables.
299 if (callp->sy_callc == nosys ||
300 sysent[callnum].sy_callc == nosys)
301 continue;
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?",
309 callnum);
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!",
320 callnum);
322 skip:;
323 #endif /* DEBUG */
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);
337 read_class_file();
339 /* init thread specific structure for mod_uninstall_all */
340 tsd_create(&mod_autounload_key, NULL);
343 static int
344 modctl_modload(int use_path, char *filename, int *rvp)
346 struct modctl *modp;
347 int retval = 0;
348 char *filenamep;
349 int modid;
351 filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
353 if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
354 retval = EFAULT;
355 goto out;
358 filenamep[MOD_MAXPATH - 1] = 0;
359 modp = mod_hold_installed_mod(filenamep, use_path, 0, &retval);
361 if (modp == NULL)
362 goto out;
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)
369 retval = EFAULT;
370 out:
371 kmem_free(filenamep, MOD_MAXPATH);
373 return (retval);
376 static int
377 modctl_modunload(modid_t id)
379 int rval = 0;
381 if (id == 0) {
382 #ifdef DEBUG
384 * Turn on mod_uninstall_daemon
386 if (mod_uninstall_interval == 0) {
387 mod_uninstall_interval = 60;
388 modreap();
389 return (rval);
391 #endif
392 mod_uninstall_all();
393 } else {
394 rval = modunload(id);
396 return (rval);
399 static int
400 modctl_modinfo(modid_t id, struct modinfo *umodi)
402 int retval;
403 struct modinfo modi;
404 #if defined(_SYSCALL32_IMPL)
405 int nobase;
406 struct modinfo32 modi32;
407 #endif
409 if (get_udatamodel() == DATAMODEL_NATIVE) {
410 if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
411 return (EFAULT);
413 #ifdef _SYSCALL32_IMPL
414 else {
415 bzero(&modi, sizeof (modi));
416 if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
417 return (EFAULT);
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;
423 #endif
425 * This flag is -only- for the kernels use.
427 modi.mi_info &= ~MI_INFO_LINKAGE;
429 retval = modinfo(id, &modi);
430 if (retval)
431 return (retval);
433 if (get_udatamodel() == DATAMODEL_NATIVE) {
434 if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
435 retval = EFAULT;
436 #ifdef _SYSCALL32_IMPL
437 } else {
438 int i;
440 if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
441 return (EOVERFLOW);
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)
459 retval = EFAULT;
460 #endif
463 return (retval);
467 * Return the last major number in the range of permissible major numbers.
469 /*ARGSUSED*/
470 static int
471 modctl_modreserve(modid_t id, int *data)
473 if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
474 return (EFAULT);
475 return (0);
478 /* Add/Remove driver and binding aliases */
479 static int
480 modctl_update_driver_aliases(int add, int *data)
482 struct modconfig mc;
483 int i, n, rv = 0;
484 struct aliases alias;
485 struct aliases *ap;
486 char name[MAXMODCONFNAME];
487 char cname[MAXMODCONFNAME];
488 char *drvname;
489 int resid;
490 struct alias_info {
491 char *alias_name;
492 int alias_resid;
493 } *aliases, *aip;
495 bzero(&mc, sizeof (struct modconfig));
496 if (get_udatamodel() == DATAMODEL_NATIVE) {
497 if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
498 return (EFAULT);
500 #ifdef _SYSCALL32_IMPL
501 else {
502 struct modconfig32 modc32;
503 if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
504 return (EFAULT);
505 else {
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;
516 #endif
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)
525 return (EINVAL);
528 * Precede alias removal by unbinding as many devices as possible.
530 if (add == 0) {
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
538 ap = mc.ap;
539 if (mc.num_aliases > 0)
540 aliases = kmem_zalloc(
541 mc.num_aliases * sizeof (struct alias_info), KM_SLEEP);
542 aip = aliases;
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) {
547 rv = EFAULT;
548 goto error;
550 if (alias.a_len > MAXMODCONFNAME) {
551 rv = EINVAL;
552 goto error;
554 if (copyin(alias.a_name, name, alias.a_len) != 0) {
555 rv = EFAULT;
556 goto error;
558 if (name[alias.a_len - 1] != '\0') {
559 rv = EINVAL;
560 goto error;
563 #ifdef _SYSCALL32_IMPL
564 else {
565 struct aliases32 al32;
566 bzero(&al32, sizeof (struct aliases32));
567 if (copyin(ap, &al32, sizeof (struct aliases32)) != 0) {
568 rv = EFAULT;
569 goto error;
571 if (al32.a_len > MAXMODCONFNAME) {
572 rv = EINVAL;
573 goto error;
575 if (copyin((void *)(uintptr_t)al32.a_name,
576 name, al32.a_len) != 0) {
577 rv = EFAULT;
578 goto error;
580 if (name[al32.a_len - 1] != '\0') {
581 rv = EINVAL;
582 goto error;
584 alias.a_next = (void *)(uintptr_t)al32.a_next;
586 #endif
587 check_esc_sequences(name, cname);
588 aip->alias_name = strdup(cname);
589 ap = alias.a_next;
590 aip++;
593 if (add == 0) {
594 ap = mc.ap;
595 resid = 0;
596 aip = aliases;
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);
601 resid += n;
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)) {
611 rv = EBUSY;
612 goto error;
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.
622 aip = aliases;
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);
629 aip++;
631 rv = 0;
632 } else {
633 aip = aliases;
634 for (i = 0; i < mc.num_aliases; i++) {
635 if (moddebug & MODDEBUG_BINDING)
636 cmn_err(CE_NOTE, "Adding binding for '%s'\n",
637 aip->alias_name);
638 (void) make_mbind(aip->alias_name,
639 mc.major, NULL, mb_hashtab);
640 aip++;
643 * Try to establish an mbinding for mc.drvname, and add it to
644 * devnames. Add class if any after establishing the major
645 * number.
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) {
651 goto error;
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)) {
667 i_ddi_bind_devs();
668 i_ddi_di_cache_invalidate();
671 error:
672 if (mc.num_aliases > 0) {
673 aip = aliases;
674 for (i = 0; i < mc.num_aliases; i++) {
675 if (aip->alias_name != NULL)
676 strfree(aip->alias_name);
677 aip++;
679 kmem_free(aliases, mc.num_aliases * sizeof (struct alias_info));
681 return (rv);
684 static int
685 modctl_add_driver_aliases(int *data)
687 return (modctl_update_driver_aliases(1, data));
690 static int
691 modctl_remove_driver_aliases(int *data)
693 return (modctl_update_driver_aliases(0, data));
696 static int
697 modctl_rem_major(major_t major)
699 struct devnames *dnp;
701 if (major >= devcnt)
702 return (EINVAL);
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);
710 return (EINVAL);
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);
718 i_ddi_bind_devs();
719 i_ddi_di_cache_invalidate();
721 /* purge all the bindings to this driver */
722 purge_mbind(major, mb_hashtab);
723 return (0);
726 static struct vfs *
727 path_to_vfs(char *name)
729 vnode_t *vp;
730 struct vfs *vfsp;
732 if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
733 return (NULL);
735 vfsp = vp->v_vfsp;
736 VN_RELE(vp);
737 return (vfsp);
740 static int
741 new_vfs_in_modpath()
743 static int n_modpath = 0;
744 static char *modpath_copy;
745 static struct pathvfs {
746 char *path;
747 struct vfs *vfsp;
748 } *pathvfs;
750 int i, new_vfs = 0;
751 char *tmp, *tmp1;
752 struct vfs *vfsp;
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;
759 if (vfsp)
760 new_vfs = 1;
763 return (new_vfs);
767 * First call, initialize the pathvfs structure
769 modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
770 tmp = modpath_copy;
771 n_modpath = 1;
772 tmp1 = strchr(tmp, ' ');
773 while (tmp1) {
774 *tmp1 = '\0';
775 n_modpath++;
776 tmp = tmp1 + 1;
777 tmp1 = strchr(tmp, ' ');
780 pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
781 tmp = modpath_copy;
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 */
791 static int
792 modctl_load_drvconf(major_t major, int flags)
794 int ret;
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);
802 i_ddi_bind_devs();
803 i_ddi_di_cache_invalidate();
804 return (0);
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);
812 if (ret == 0)
813 i_ddi_bind_devs();
814 return (ret);
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
823 * boot performance.
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();
834 return (0);
838 * Unload driver.conf file and follow up by attempting
839 * to rebind devices to more appropriate driver.
841 static int
842 modctl_unload_drvconf(major_t major)
844 int ret;
846 if (major >= devcnt)
847 return (EINVAL);
849 ret = i_ddi_unload_drvconf(major);
850 if (ret != 0)
851 return (ret);
852 (void) i_ddi_unbind_devs(major);
853 i_ddi_bind_devs();
855 return (0);
858 static void
859 check_esc_sequences(char *str, char *cstr)
861 int i;
862 size_t len;
863 char *p;
865 len = strlen(str);
866 for (i = 0; i < len; i++, str++, cstr++) {
867 if (*str != '\\') {
868 *cstr = *str;
869 } else {
870 p = str + 1;
872 * we only handle octal escape sequences for SPACE
874 if (*p++ == '0' && *p++ == '4' && *p == '0') {
875 *cstr = ' ';
876 str += 3;
877 } else {
878 *cstr = *str;
882 *cstr = 0;
885 static int
886 modctl_getmodpathlen(int *data)
888 int len;
889 len = strlen(default_path);
890 if (copyout(&len, data, sizeof (len)) != 0)
891 return (EFAULT);
892 return (0);
895 static int
896 modctl_getmodpath(char *data)
898 if (copyout(default_path, data, strlen(default_path) + 1) != 0)
899 return (EFAULT);
900 return (0);
903 static int
904 modctl_read_sysbinding_file(void)
906 (void) read_binding_file(sysbind, sb_hashtab, make_mbind);
907 return (0);
910 static int
911 modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
913 char name[256];
914 int retval;
915 major_t major;
917 if (ulen == 0)
918 return (EINVAL);
919 if ((retval = copyinstr(uname, name,
920 (ulen < 256) ? ulen : 256, 0)) != 0)
921 return (retval);
922 if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
923 return (ENODEV);
924 if (copyout(&major, umajorp, sizeof (major_t)) != 0)
925 return (EFAULT);
926 return (0);
929 static char **
930 convert_constraint_string(char *constraints, size_t len)
932 int i;
933 int n;
934 char *p;
935 char **array;
937 ASSERT(constraints != NULL);
938 ASSERT(len > 0);
940 for (i = 0, p = constraints; strlen(p) > 0; i++, p += strlen(p) + 1)
943 n = i;
945 if (n == 0) {
946 kmem_free(constraints, len);
947 return (NULL);
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);
955 array[n] = NULL;
957 kmem_free(constraints, len);
959 return (array);
961 /*ARGSUSED*/
962 static int
963 modctl_retire(char *path, char *uconstraints, size_t ulen)
965 char *pathbuf;
966 char *devpath;
967 size_t pathsz;
968 int retval;
969 char *constraints;
970 char **cons_array;
972 if (path == NULL)
973 return (EINVAL);
975 if ((uconstraints == NULL) ^ (ulen == 0))
976 return (EINVAL);
978 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
979 retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
980 if (retval != 0) {
981 kmem_free(pathbuf, MAXPATHLEN);
982 return (retval);
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);
996 return (0);
999 cons_array = NULL;
1000 if (uconstraints) {
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);
1005 return (EFAULT);
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);
1021 return (ENOTSUP);
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
1029 * code.
1031 retval = e_ddi_retire_persist(devpath);
1032 if (retval != 0) {
1033 cmn_err(CE_WARN, "Failed to persist device retire: error %d: "
1034 "%s", retval, devpath);
1035 kmem_free(devpath, strlen(devpath) + 1);
1036 return (retval);
1038 if (moddebug & MODDEBUG_RETIRE)
1039 cmn_err(CE_NOTE, "Persisted retire of device: %s", devpath);
1041 kmem_free(devpath, strlen(devpath) + 1);
1042 return (0);
1045 static int
1046 modctl_is_retired(char *path, int *statep)
1048 char *pathbuf;
1049 char *devpath;
1050 size_t pathsz;
1051 int error;
1052 int status;
1054 if (path == NULL || statep == NULL)
1055 return (EINVAL);
1057 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1058 error = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
1059 if (error != 0) {
1060 kmem_free(pathbuf, MAXPATHLEN);
1061 return (error);
1063 devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
1064 kmem_free(pathbuf, MAXPATHLEN);
1066 if (e_ddi_device_retired(devpath))
1067 status = 1;
1068 else
1069 status = 0;
1070 kmem_free(devpath, strlen(devpath) + 1);
1072 return (copyout(&status, statep, sizeof (status)) ? EFAULT : 0);
1075 static int
1076 modctl_unretire(char *path)
1078 char *pathbuf;
1079 char *devpath;
1080 size_t pathsz;
1081 int retired;
1082 int retval;
1084 if (path == NULL)
1085 return (EINVAL);
1087 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1088 retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
1089 if (retval != 0) {
1090 kmem_free(pathbuf, MAXPATHLEN);
1091 return (retval);
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",
1119 devpath);
1123 * Check if the device is already unretired. If so,
1124 * the unretire becomes a NOP
1126 if (!retired) {
1127 cmn_err(CE_NOTE, "Not retired: %s", devpath);
1128 kmem_free(devpath, strlen(devpath) + 1);
1129 return (0);
1132 retval = e_ddi_unretire_device(devpath);
1133 if (retval != 0) {
1134 cmn_err(CE_WARN, "cannot unretire device: error %d, path %s\n",
1135 retval, devpath);
1138 kmem_free(devpath, strlen(devpath) + 1);
1140 return (retval);
1143 static int
1144 modctl_getname(char *uname, uint_t ulen, int *umajorp)
1146 char *name;
1147 major_t major;
1149 if (copyin(umajorp, &major, sizeof (major)) != 0)
1150 return (EFAULT);
1151 if ((name = mod_major_to_name(major)) == NULL)
1152 return (ENODEV);
1153 if ((strlen(name) + 1) > ulen)
1154 return (ENOSPC);
1155 return (copyoutstr(name, uname, ulen, NULL));
1158 static int
1159 modctl_devt2instance(dev_t dev, int *uinstancep)
1161 int instance;
1163 if ((instance = dev_to_instance(dev)) == -1)
1164 return (EINVAL);
1166 return (copyout(&instance, uinstancep, sizeof (int)));
1170 * Return the sizeof of the device id.
1172 static int
1173 modctl_sizeof_devid(dev_t dev, uint_t *len)
1175 uint_t sz;
1176 ddi_devid_t devid;
1178 /* get device id */
1179 if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
1180 return (EINVAL);
1182 sz = ddi_devid_sizeof(devid);
1183 ddi_devid_free(devid);
1185 /* copyout device id size */
1186 if (copyout(&sz, len, sizeof (sz)) != 0)
1187 return (EFAULT);
1189 return (0);
1193 * Return a copy of the device id.
1195 static int
1196 modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
1198 uint_t sz;
1199 ddi_devid_t devid;
1200 int err = 0;
1202 /* get device id */
1203 if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
1204 return (EINVAL);
1206 sz = ddi_devid_sizeof(devid);
1208 /* Error if device id is larger than space allocated */
1209 if (sz > len) {
1210 ddi_devid_free(devid);
1211 return (ENOSPC);
1214 /* copy out device id */
1215 if (copyout(devid, udevid, sz) != 0)
1216 err = EFAULT;
1217 ddi_devid_free(devid);
1218 return (err);
1222 * return the /devices paths associated with the specified devid and
1223 * minor name.
1225 /*ARGSUSED*/
1226 static int
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;
1231 int devid_len;
1232 char *minor_name = NULL;
1233 dev_info_t *dip = NULL;
1234 int circ;
1235 struct ddi_minor_data *dmdp;
1236 char *path = NULL;
1237 int ulens;
1238 int lens;
1239 int len;
1240 dev_t *devlist = NULL;
1241 int ndevs;
1242 int i;
1243 int ret = 0;
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.
1252 if (upaths) {
1253 if (ulensp == NULL)
1254 return (EINVAL);
1255 if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
1256 return (EFAULT);
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)) {
1266 ret = EFAULT;
1267 goto out;
1269 len = 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)) {
1274 ret = EFAULT;
1275 goto out;
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)) {
1285 ret = EFAULT;
1286 goto out;
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) {
1304 ret = EINVAL;
1305 goto out;
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)
1318 continue;
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))
1325 continue;
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))
1331 continue;
1332 else {
1333 if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
1334 (dmdp->ddm_spec_type != S_IFCHR))
1335 continue;
1336 if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
1337 (dmdp->ddm_spec_type != S_IFBLK))
1338 continue;
1341 (void) ddi_pathname_minor(dmdp, path);
1342 len = strlen(path) + 1;
1343 *(path + len) = '\0'; /* set double termination */
1344 lens += len;
1346 /* copyout the path with double terminations */
1347 if (upaths) {
1348 if (lens > ulens) {
1349 ret = EAGAIN;
1350 goto out;
1352 if (copyout(path, upaths, len + 1)) {
1353 ret = EFAULT;
1354 goto out;
1356 upaths += len;
1359 ndi_devi_exit(dip, circ);
1360 ddi_release_devi(dip);
1361 dip = NULL;
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))) {
1367 ret = EFAULT;
1368 goto out;
1370 ret = 0;
1372 out: if (dip) {
1373 ndi_devi_exit(dip, circ);
1374 ddi_release_devi(dip);
1376 if (path)
1377 kmem_free(path, MAXPATHLEN);
1378 if (devlist)
1379 ddi_lyr_free_devlist(devlist, ndevs);
1380 if (minor_name &&
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);
1385 if (devid)
1386 kmem_free(devid, devid_len);
1387 return (ret);
1391 * Return the size of the minor name.
1393 static int
1394 modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
1396 uint_t sz;
1397 char *name;
1399 /* get the minor name */
1400 if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1401 return (EINVAL);
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)
1408 return (EFAULT);
1410 return (0);
1414 * Return the minor name.
1416 static int
1417 modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
1419 uint_t sz;
1420 char *name;
1421 int err = 0;
1423 /* get the minor name */
1424 if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1425 return (EINVAL);
1427 sz = strlen(name) + 1;
1429 /* Error if the minor name is larger than the space allocated */
1430 if (sz > len) {
1431 kmem_free(name, sz);
1432 return (ENOSPC);
1435 /* copy out the minor name */
1436 if (copyout(name, uname, sz) != 0)
1437 err = EFAULT;
1438 kmem_free(name, sz);
1439 return (err);
1443 * Return the size of the (dev_t,spectype) devfspath name.
1445 static int
1446 modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
1448 uint_t sz;
1449 char *name;
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);
1455 return (EINVAL);
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)
1463 return (EFAULT);
1465 return (0);
1469 * Return the (dev_t,spectype) devfspath name.
1471 static int
1472 modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
1474 uint_t sz;
1475 char *name;
1476 int err = 0;
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);
1482 return (EINVAL);
1485 sz = strlen(name) + 1;
1487 /* Error if the path name is larger than the space allocated */
1488 if (sz > len) {
1489 kmem_free(name, MAXPATHLEN);
1490 return (ENOSPC);
1493 /* copy out the path name */
1494 if (copyout(name, uname, sz) != 0)
1495 err = EFAULT;
1496 kmem_free(name, MAXPATHLEN);
1497 return (err);
1501 * Return the size of the (major,instance) devfspath name.
1503 static int
1504 modctl_devfspath_mi_len(major_t major, int instance, uint_t *len)
1506 uint_t sz;
1507 char *name;
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);
1513 return (EINVAL);
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)
1521 return (EFAULT);
1523 return (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.
1531 static int
1532 modctl_devfspath_mi(major_t major, int instance, uint_t len, char *uname)
1534 uint_t sz;
1535 char *name;
1536 int err = 0;
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);
1542 return (EINVAL);
1545 sz = strlen(name) + 1;
1547 /* Error if the path name is larger than the space allocated */
1548 if (sz > len) {
1549 kmem_free(name, MAXPATHLEN);
1550 return (ENOSPC);
1553 /* copy out the path name */
1554 if (copyout(name, uname, sz) != 0)
1555 err = EFAULT;
1556 kmem_free(name, MAXPATHLEN);
1557 return (err);
1560 static int
1561 modctl_get_fbname(char *path)
1563 extern dev_t fbdev;
1564 char *pathname = NULL;
1565 int rval = 0;
1567 /* make sure fbdev is set before we plunge in */
1568 if (fbdev == NODEV)
1569 return (ENODEV);
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) {
1575 rval = EFAULT;
1578 kmem_free(pathname, MAXPATHLEN);
1579 return (rval);
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'.
1587 static int
1588 modctl_reread_dacf(char *path)
1590 int rval = 0;
1591 char *filename, *filenamep;
1593 filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1595 if (path == NULL) {
1596 filenamep = NULL;
1597 } else {
1598 if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
1599 rval = EFAULT;
1600 goto out;
1602 filenamep = filename;
1603 filenamep[MAXPATHLEN - 1] = '\0';
1606 rval = read_dacf_binding_file(filenamep);
1607 out:
1608 kmem_free(filename, MAXPATHLEN);
1609 return (rval);
1612 /*ARGSUSED*/
1613 static int
1614 modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
1615 uint_t flag)
1617 int error = 0;
1618 char *filenamep;
1620 switch (subcmd) {
1622 case MODEVENTS_FLUSH:
1623 /* flush all currently queued events */
1624 log_sysevent_flushq(subcmd, flag);
1625 break;
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)) {
1637 error = EFAULT;
1638 } else {
1639 error = log_sysevent_filename(filenamep);
1641 kmem_free(filenamep, MOD_MAXPATH);
1642 break;
1644 case MODEVENTS_GETDATA:
1645 error = log_sysevent_copyout_data((sysevent_id_t *)a2,
1646 (size_t)a3, (caddr_t)a4);
1647 break;
1649 case MODEVENTS_FREEDATA:
1650 error = log_sysevent_free_data((sysevent_id_t *)a2);
1651 break;
1652 case MODEVENTS_POST_EVENT:
1653 error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
1654 (sysevent_id_t *)a4);
1655 break;
1656 case MODEVENTS_REGISTER_EVENT:
1657 error = log_sysevent_register((char *)a2, (char *)a3,
1658 (se_pubsub_t *)a4);
1659 break;
1660 default:
1661 error = EINVAL;
1664 return (error);
1667 static void
1668 free_mperm(mperm_t *mp)
1670 int len;
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
1691 static void
1692 rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1694 mperm_t **mp_head;
1695 mperm_t *freemp = NULL;
1696 struct devnames *dnp = &devnamesp[major];
1697 mperm_t **wildmp;
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);
1705 if (*wildmp)
1706 freemp = *wildmp;
1707 *wildmp = NULL;
1708 } else {
1709 mp_head = &dnp->dn_mperm;
1710 while (*mp_head) {
1711 if (strcmp((*mp_head)->mp_minorname,
1712 mp->mp_minorname) != 0) {
1713 mp_head = &(*mp_head)->mp_next;
1714 continue;
1716 /* remove the entry */
1717 freemp = *mp_head;
1718 *mp_head = freemp->mp_next;
1719 break;
1722 if (freemp) {
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);
1729 free_mperm(freemp);
1730 } else {
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
1743 static void
1744 add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1746 mperm_t **mp_head;
1747 mperm_t *freemp = NULL;
1748 struct devnames *dnp = &devnamesp[major];
1749 mperm_t **wildmp;
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);
1761 if (*wildmp)
1762 freemp = *wildmp;
1763 *wildmp = mp;
1764 } else {
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) {
1768 if (v == NULL)
1769 dnp->dn_mperm = mp;
1770 else
1771 v->mp_next = mp;
1772 mp->mp_next = p->mp_next;
1773 freemp = p;
1774 goto replaced;
1777 if (p == NULL) {
1778 mp_head = &dnp->dn_mperm;
1779 if (*mp_head == NULL) {
1780 *mp_head = mp;
1781 } else {
1782 mp->mp_next = *mp_head;
1783 *mp_head = mp;
1787 replaced:
1788 if (freemp) {
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);
1795 free_mperm(freemp);
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);
1806 static int
1807 process_minorperm(int cmd, nvlist_t *nvl)
1809 char *minor;
1810 major_t major;
1811 mperm_t *mp;
1812 nvpair_t *nvp;
1813 char *name;
1814 int is_clone;
1815 major_t minmaj;
1817 ASSERT(cmd == MODLOADMINORPERM ||
1818 cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
1820 nvp = NULL;
1821 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1822 name = nvpair_name(nvp);
1824 is_clone = 0;
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);
1833 minor = "*";
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) {
1851 cmn_err(CE_CONT,
1852 "mapping %s:%s to %s:*\n",
1853 name, minor, minor);
1855 major = minmaj;
1856 name = minor;
1857 minor = "*";
1858 is_clone = 1;
1862 if (mp) {
1863 mp->mp_minorname =
1864 i_ddi_strdup(minor, KM_SLEEP);
1866 } else {
1867 mp = NULL;
1868 if (moddebug & MODDEBUG_MINORPERM) {
1869 cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
1873 /* mode */
1874 nvp = nvlist_next_nvpair(nvl, nvp);
1875 ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
1876 if (mp)
1877 (void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
1878 /* uid */
1879 nvp = nvlist_next_nvpair(nvl, nvp);
1880 ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
1881 if (mp)
1882 (void) nvpair_value_uint32(nvp, &mp->mp_uid);
1883 /* gid */
1884 nvp = nvlist_next_nvpair(nvl, nvp);
1885 ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
1886 if (mp) {
1887 (void) nvpair_value_uint32(nvp, &mp->mp_gid);
1889 if (cmd == MODREMMINORPERM) {
1890 rem_minorperm(major, name, mp, is_clone);
1891 free_mperm(mp);
1892 } else {
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);
1906 return (0);
1909 static int
1910 modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
1912 int error;
1913 nvlist_t *nvl;
1914 char *buf = kmem_alloc(buflen, KM_SLEEP);
1916 if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
1917 kmem_free(buf, buflen);
1918 return (error);
1921 error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
1922 kmem_free(buf, buflen);
1923 if (error)
1924 return (error);
1926 error = process_minorperm(cmd, nvl);
1927 nvlist_free(nvl);
1928 return (error);
1931 struct walk_args {
1932 char *wa_drvname;
1933 list_t wa_pathlist;
1936 struct path_elem {
1937 char *pe_dir;
1938 char *pe_nodename;
1939 list_node_t pe_node;
1940 int pe_dirlen;
1943 /*ARGSUSED*/
1944 static int
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;
1949 char *nodename;
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, '/');
1963 *nodename++ = 0;
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.
1982 static int
1983 modctl_remdrv_cleanup(const char *u_drvname)
1985 struct walk_args *wargs;
1986 struct path_elem *pe;
1987 char *drvname;
1988 int err, rval = 0;
1990 drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
1991 if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
1992 kmem_free(drvname, MAXMODCONFNAME);
1993 return (err);
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);
2022 if (rval == 0)
2023 rval = err;
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);
2038 if (rval == 0)
2039 rval = err;
2041 kmem_free(drvname, MAXMODCONFNAME);
2042 return (rval);
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.
2050 void
2051 dev_devices_cleanup()
2053 struct walk_args *wargs;
2054 struct path_elem *pe;
2055 dev_info_t *devi;
2056 char *path;
2057 int err;
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);
2079 if (devi != NULL) {
2080 ddi_release_devi(devi);
2081 } else {
2082 err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
2083 (const char *)pe->pe_nodename);
2084 if (err) {
2085 cmn_err(CE_CONT,
2086 "devfs: %s: clean-up error %d\n",
2087 path, err);
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);
2101 static int
2102 modctl_allocpriv(const char *name)
2104 char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
2105 int error;
2107 if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
2108 kmem_free(pstr, PRIVNAME_MAX);
2109 return (error);
2111 error = priv_getbyname(pstr, PRIV_ALLOC);
2112 if (error < 0)
2113 error = -error;
2114 else
2115 error = 0;
2116 kmem_free(pstr, PRIVNAME_MAX);
2117 return (error);
2120 static int
2121 modctl_devexists(const char *upath, int pathlen)
2123 char *path;
2124 int ret;
2127 * copy in the path, including the terminating null
2129 pathlen++;
2130 if (pathlen <= 1 || pathlen > MAXPATHLEN)
2131 return (EINVAL);
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);
2138 return (ret);
2141 static int
2142 modctl_devreaddir(const char *udir, int udirlen,
2143 char *upaths, int64_t *ulensp)
2145 char *paths = NULL;
2146 char **dirlist = NULL;
2147 char *dir;
2148 int64_t ulens;
2149 int64_t lens;
2150 int i, n;
2151 int ret = 0;
2152 char *p;
2153 int npaths;
2154 int npaths_alloc;
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.
2163 if (upaths) {
2164 if (ulensp == NULL)
2165 return (EINVAL);
2166 if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
2167 return (EFAULT);
2171 * copyin the /dev path including terminating null
2173 udirlen++;
2174 if (udirlen <= 1 || udirlen > MAXPATHLEN)
2175 return (EINVAL);
2176 dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2177 if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2178 goto err;
2180 if ((ret = sdev_modctl_readdir(dir, &dirlist,
2181 &npaths, &npaths_alloc, 0)) != 0) {
2182 ASSERT(dirlist == NULL);
2183 goto err;
2186 lens = 0;
2187 for (i = 0; i < npaths; i++) {
2188 lens += strlen(dirlist[i]) + 1;
2190 lens++; /* add one for double termination */
2192 if (upaths) {
2193 if (lens > ulens) {
2194 ret = EAGAIN;
2195 goto out;
2198 paths = kmem_alloc(lens, KM_SLEEP);
2200 p = paths;
2201 for (i = 0; i < npaths; i++) {
2202 n = strlen(dirlist[i]) + 1;
2203 bcopy(dirlist[i], p, n);
2204 p += n;
2206 *p = 0;
2208 if (copyout(paths, upaths, lens)) {
2209 ret = EFAULT;
2210 goto err;
2214 out:
2215 /* copy out the amount of space needed to hold the paths */
2216 if (copyout(&lens, ulensp, sizeof (lens)))
2217 ret = EFAULT;
2219 err:
2220 if (dirlist)
2221 sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2222 if (paths)
2223 kmem_free(paths, lens);
2224 kmem_free(dir, udirlen + 1);
2225 return (ret);
2228 static int
2229 modctl_devemptydir(const char *udir, int udirlen, int *uempty)
2231 char *dir;
2232 int ret;
2233 char **dirlist = NULL;
2234 int npaths;
2235 int npaths_alloc;
2236 int empty;
2239 * copyin the /dev path including terminating null
2241 udirlen++;
2242 if (udirlen <= 1 || udirlen > MAXPATHLEN)
2243 return (EINVAL);
2244 dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2245 if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2246 goto err;
2248 if ((ret = sdev_modctl_readdir(dir, &dirlist,
2249 &npaths, &npaths_alloc, 1)) != 0) {
2250 goto err;
2253 empty = npaths ? 0 : 1;
2254 if (copyout(&empty, uempty, sizeof (empty)))
2255 ret = EFAULT;
2257 err:
2258 if (dirlist)
2259 sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2260 kmem_free(dir, udirlen + 1);
2261 return (ret);
2264 static int
2265 modctl_hp(int subcmd, const char *path, char *cn_name, uintptr_t arg,
2266 uintptr_t rval)
2268 int error = 0;
2269 size_t pathsz, namesz;
2270 char *devpath, *cn_name_str;
2272 if (path == NULL)
2273 return (EINVAL);
2275 devpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
2276 error = copyinstr(path, devpath, MAXPATHLEN, &pathsz);
2277 if (error != 0) {
2278 kmem_free(devpath, MAXPATHLEN);
2279 return (EFAULT);
2282 cn_name_str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
2283 error = copyinstr(cn_name, cn_name_str, MAXNAMELEN, &namesz);
2284 if (error != 0) {
2285 kmem_free(devpath, MAXPATHLEN);
2286 kmem_free(cn_name_str, MAXNAMELEN);
2288 return (EFAULT);
2291 switch (subcmd) {
2292 case MODHPOPS_CHANGE_STATE:
2293 error = ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE, devpath,
2294 cn_name_str, arg, (uintptr_t)NULL);
2295 break;
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);
2300 break;
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);
2305 break;
2306 case MODHPOPS_BUS_GET:
2307 error = ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY, devpath,
2308 cn_name_str, arg, rval);
2309 break;
2310 case MODHPOPS_BUS_SET:
2311 error = ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY, devpath,
2312 cn_name_str, arg, rval);
2313 break;
2314 default:
2315 error = ENOTSUP;
2316 break;
2319 kmem_free(devpath, MAXPATHLEN);
2320 kmem_free(cn_name_str, MAXNAMELEN);
2322 return (error);
2326 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
2328 int error = 0;
2330 switch (subcmd) {
2331 case MODDEVNAME_LOOKUPDOOR:
2332 error = devname_filename_register((char *)a1);
2333 break;
2334 case MODDEVNAME_PROFILE:
2335 error = devname_profile_update((char *)a1, (size_t)a2);
2336 break;
2337 case MODDEVNAME_RECONFIG:
2338 i_ddi_set_reconfig();
2339 break;
2340 case MODDEVNAME_SYSAVAIL:
2341 i_ddi_set_sysavail();
2342 break;
2343 default:
2344 error = EINVAL;
2345 break;
2348 return (error);
2351 /*ARGSUSED5*/
2353 modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
2354 uintptr_t a5)
2356 int error = EINVAL;
2357 dev_t dev;
2359 if (secpolicy_modctl(CRED(), cmd) != 0)
2360 return (set_errno(EPERM));
2362 switch (cmd) {
2363 case MODLOAD: /* load a module */
2364 error = modctl_modload((int)a1, (char *)a2, (int *)a3);
2365 break;
2367 case MODUNLOAD: /* unload a module */
2368 error = modctl_modunload((modid_t)a1);
2369 break;
2371 case MODINFO: /* get module status */
2372 error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
2373 break;
2375 case MODRESERVED: /* get last major number in range */
2376 error = modctl_modreserve((modid_t)a1, (int *)a2);
2377 break;
2379 case MODSETMINIROOT: /* we are running in miniroot */
2380 isminiroot = 1;
2381 error = 0;
2382 break;
2384 case MODADDMAJBIND: /* add major / driver alias bindings */
2385 error = modctl_add_driver_aliases((int *)a2);
2386 break;
2388 case MODGETPATHLEN: /* get modpath length */
2389 error = modctl_getmodpathlen((int *)a2);
2390 break;
2392 case MODGETPATH: /* get modpath */
2393 error = modctl_getmodpath((char *)a2);
2394 break;
2396 case MODREADSYSBIND: /* read system call binding file */
2397 error = modctl_read_sysbinding_file();
2398 break;
2400 case MODGETMAJBIND: /* get major number for named device */
2401 error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
2402 break;
2404 case MODGETNAME: /* get name of device given major number */
2405 error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
2406 break;
2408 case MODDEVT2INSTANCE:
2409 if (get_udatamodel() == DATAMODEL_NATIVE) {
2410 dev = (dev_t)a1;
2412 #ifdef _SYSCALL32_IMPL
2413 else {
2414 dev = expldev(a1);
2416 #endif
2417 error = modctl_devt2instance(dev, (int *)a2);
2418 break;
2420 case MODSIZEOF_DEVID: /* sizeof device id of device given dev_t */
2421 if (get_udatamodel() == DATAMODEL_NATIVE) {
2422 dev = (dev_t)a1;
2424 #ifdef _SYSCALL32_IMPL
2425 else {
2426 dev = expldev(a1);
2428 #endif
2429 error = modctl_sizeof_devid(dev, (uint_t *)a2);
2430 break;
2432 case MODGETDEVID: /* get device id of device given dev_t */
2433 if (get_udatamodel() == DATAMODEL_NATIVE) {
2434 dev = (dev_t)a1;
2436 #ifdef _SYSCALL32_IMPL
2437 else {
2438 dev = expldev(a1);
2440 #endif
2441 error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
2442 break;
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,
2447 (uint_t *)a3);
2449 #ifdef _SYSCALL32_IMPL
2450 else {
2451 error = modctl_sizeof_minorname(expldev(a1), (int)a2,
2452 (uint_t *)a3);
2455 #endif
2456 break;
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
2464 else {
2465 error = modctl_get_minorname(expldev(a1), (int)a2,
2466 (uint_t)a3, (char *)a4);
2468 #endif
2469 break;
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,
2474 (uint_t *)a3);
2476 #ifdef _SYSCALL32_IMPL
2477 else {
2478 error = modctl_devfspath_len(expldev(a1), (int)a2,
2479 (uint_t *)a3);
2482 #endif
2483 break;
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
2491 else {
2492 error = modctl_devfspath(expldev(a1), (int)a2,
2493 (uint_t)a3, (char *)a4);
2495 #endif
2496 break;
2498 case MODGETDEVFSPATH_MI_LEN: /* sizeof path nm of (major,instance) */
2499 error = modctl_devfspath_mi_len((major_t)a1, (int)a2,
2500 (uint_t *)a3);
2501 break;
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);
2506 break;
2509 case MODEVENTS:
2510 error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
2511 break;
2513 case MODGETFBNAME: /* get the framebuffer name */
2514 error = modctl_get_fbname((char *)a1);
2515 break;
2517 case MODREREADDACF: /* reread dacf rule database from given file */
2518 error = modctl_reread_dacf((char *)a1);
2519 break;
2521 case MODLOADDRVCONF: /* load driver.conf file for major */
2522 error = modctl_load_drvconf((major_t)a1, (int)a2);
2523 break;
2525 case MODUNLOADDRVCONF: /* unload driver.conf file for major */
2526 error = modctl_unload_drvconf((major_t)a1);
2527 break;
2529 case MODREMMAJBIND: /* remove a major binding */
2530 error = modctl_rem_major((major_t)a1);
2531 break;
2533 case MODREMDRVALIAS: /* remove a major/alias binding */
2534 error = modctl_remove_driver_aliases((int *)a2);
2535 break;
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);
2540 break;
2542 case MODSETDEVPOLICY: /* establish device policy */
2543 error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
2544 break;
2546 case MODGETDEVPOLICY: /* get device policy */
2547 error = devpolicy_get((int *)a1, (size_t)a2,
2548 (devplcysys_t *)a3);
2549 break;
2551 case MODALLOCPRIV:
2552 error = modctl_allocpriv((const char *)a1);
2553 break;
2555 case MODGETDEVPOLICYBYNAME:
2556 error = devpolicy_getbyname((size_t)a1,
2557 (devplcysys_t *)a2, (char *)a3);
2558 break;
2560 case MODLOADMINORPERM:
2561 case MODADDMINORPERM:
2562 case MODREMMINORPERM:
2563 error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
2564 break;
2566 case MODREMDRVCLEANUP:
2567 error = modctl_remdrv_cleanup((const char *)a1);
2568 break;
2570 case MODDEVEXISTS: /* non-reconfiguring /dev lookup */
2571 error = modctl_devexists((const char *)a1, (size_t)a2);
2572 break;
2574 case MODDEVREADDIR: /* non-reconfiguring /dev readdir */
2575 error = modctl_devreaddir((const char *)a1, (size_t)a2,
2576 (char *)a3, (int64_t *)a4);
2577 break;
2579 case MODDEVEMPTYDIR: /* non-reconfiguring /dev emptydir */
2580 error = modctl_devemptydir((const char *)a1, (size_t)a2,
2581 (int *)a3);
2582 break;
2584 case MODDEVNAME:
2585 error = modctl_moddevname((int)a1, a2, a3);
2586 break;
2588 case MODRETIRE: /* retire device named by physpath a1 */
2589 error = modctl_retire((char *)a1, (char *)a2, (size_t)a3);
2590 break;
2592 case MODISRETIRED: /* check if a device is retired. */
2593 error = modctl_is_retired((char *)a1, (int *)a2);
2594 break;
2596 case MODUNRETIRE: /* unretire device named by physpath a1 */
2597 error = modctl_unretire((char *)a1);
2598 break;
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);
2603 break;
2605 default:
2606 error = EINVAL;
2607 break;
2610 return (error ? set_errno(error) : 0);
2614 * Calls to kobj_load_module()() are handled off to this routine in a
2615 * separate thread.
2617 static void
2618 modload_thread(struct loadmt *ltp)
2620 /* load the module and signal the creator of this thread */
2621 kmutex_t cpr_lk;
2622 callb_cpr_t cpr_i;
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);
2629 pm_return_lock();
2630 sema_v(&ltp->sema);
2631 mutex_enter(&cpr_lk);
2632 CALLB_CPR_EXIT(&cpr_i);
2633 mutex_destroy(&cpr_lk);
2634 thread_exit();
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).
2642 static int
2643 modrload(const char *subdir, const char *filename, struct modctl **rmodp)
2645 struct modctl *modp;
2646 size_t size;
2647 char *fullname;
2648 int retval = EINVAL;
2649 int id = -1;
2651 if (rmodp)
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);
2667 } else {
2668 fullname = (char *)filename;
2671 modp = mod_hold_installed_mod(fullname, 1, 0, &retval);
2672 if (modp != NULL) {
2673 id = modp->mod_id;
2674 if (rmodp) {
2675 /* add mod_ref and return *rmodp */
2676 mutex_enter(&mod_lock);
2677 modp->mod_ref++;
2678 mutex_exit(&mod_lock);
2679 *rmodp = modp;
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
2707 * foo/bar.baz.1.2 2
2708 * foo/bar.baz.1 1
2709 * foo/bar.baz 0
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);
2721 char *p = path;
2723 char **dotv;
2724 int i, rc, id;
2725 modctl_t *mp;
2727 if (p2 != NULL)
2728 n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
2729 else
2730 n = snprintf(p, resid, "%s/%s", subdir, p1);
2732 if (n >= resid)
2733 return (-1);
2735 p += n;
2736 resid -= n;
2737 dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
2739 for (i = 0; i < suffc; i++) {
2740 dotv[i] = p;
2741 n = snprintf(p, resid, "%s%u", delim, suffv[i]);
2743 if (n >= resid) {
2744 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2745 return (-1);
2748 p += n;
2749 resid -= n;
2752 dotv[suffc] = p;
2754 for (i = suffc; i >= 0; i--) {
2755 dotv[i][0] = '\0';
2756 mp = mod_hold_installed_mod(path, 1, 1, &rc);
2758 if (mp != NULL) {
2759 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2760 id = mp->mod_id;
2761 mod_release_mod(mp);
2762 if (chosen != NULL)
2763 *chosen = i;
2764 return (id);
2768 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2769 return (-1);
2773 * Load a module.
2776 modloadonly(const char *subdir, const char *filename)
2778 struct modctl *modp;
2779 char *fullname;
2780 size_t size;
2781 int id, retval;
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);
2790 } else {
2791 fullname = (char *)filename;
2794 modp = mod_hold_loaded_mod(NULL, fullname, &retval);
2795 if (modp) {
2796 id = modp->mod_id;
2797 mod_release_mod(modp);
2800 if (subdir != NULL)
2801 kmem_free(fullname, size);
2803 if (retval == 0)
2804 return (id);
2805 return (-1);
2809 * Try to uninstall and unload a module, removing a reference if caller
2810 * specifies rmodp.
2812 static int
2813 modunrload(modid_t id, struct modctl **rmodp, int unload)
2815 struct modctl *modp;
2816 int retval;
2818 if (rmodp)
2819 *rmodp = NULL; /* avoid garbage */
2821 if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
2822 return (EINVAL);
2824 if (rmodp) {
2825 mutex_enter(&mod_lock);
2826 modp->mod_ref--;
2827 if (modp->mod_ref == 0)
2828 mod_uninstall_ref_zero++;
2829 mutex_exit(&mod_lock);
2830 *rmodp = modp;
2833 if (unload) {
2834 retval = moduninstall(modp);
2835 if (retval == 0) {
2836 mod_unload(modp);
2837 CPU_STATS_ADDQ(CPU, sys, modunload, 1);
2838 } else if (retval == EALREADY)
2839 retval = 0; /* already unloaded, not an error */
2840 } else
2841 retval = 0;
2843 mod_release_mod(modp);
2844 return (retval);
2848 * Uninstall and unload a module.
2851 modunload(modid_t id)
2853 int retval;
2855 /* synchronize with any active modunload_disable() */
2856 modunload_begin();
2857 if (ddi_root_node())
2858 (void) devfs_clean(ddi_root_node(), NULL, 0);
2859 retval = modunrload(id, NULL, 1);
2860 modunload_end();
2861 return (retval);
2865 * Return status of a loaded module.
2867 static int
2868 modinfo(modid_t id, struct modinfo *modinfop)
2870 struct modctl *modp;
2871 modid_t mid;
2872 int i;
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)
2879 break;
2880 mod_release_mod(modp);
2882 if (modp == NULL)
2883 return (EINVAL);
2884 } else {
2885 modp = mod_hold_by_id(id);
2886 if (modp == NULL)
2887 return (EINVAL);
2888 if (!(modinfop->mi_info & MI_INFO_CNT) &&
2889 (modp->mod_installed == 0)) {
2890 mod_release_mod(modp);
2891 return (EINVAL);
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);
2916 return (0);
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)
2935 struct modctl *mp;
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 */
2944 mod_check_again:
2945 if ((mp = mip->mp) != NULL) {
2946 if (mp->mod_busy == 0) {
2947 if (mp->mod_installed) {
2948 /* increment the reference count */
2949 mp->mod_ref++;
2950 ASSERT(mp->mod_ref && mp->mod_installed);
2951 mutex_exit(&mod_lock);
2952 return (0);
2953 } else {
2954 mp->mod_busy = 1;
2955 mp->mod_inprogress_thread =
2956 (curthread == NULL ?
2957 (kthread_id_t)-1 : curthread);
2959 } else {
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);
2976 } else {
2977 /* first time we've hit this module */
2978 mutex_exit(&mod_lock);
2979 mp = mod_hold_by_name(mip->modm_module_name);
2980 mip->mp = mp;
2984 * If we are here, it means that the following conditions
2985 * are satisfied.
2987 * mip->mp != NULL
2988 * this thread has set the mp->mod_busy = 1
2989 * mp->mod_installed = 0
2992 ASSERT(mp != NULL);
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);
3003 } else {
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.
3012 if (mp->mod_loaded)
3013 mod_unload(mp);
3015 mod_release_mod(mp);
3016 if (stub->mods_errfcn == NULL) {
3017 cmn_err(CE_PANIC, mod_stub_err,
3018 mip->modm_module_name);
3019 } else {
3020 return (-1);
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);
3036 mp->mod_ref++;
3037 ASSERT(mp->mod_ref &&
3038 (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
3039 mod_release(mp);
3040 mutex_exit(&mod_lock);
3041 return (0);
3044 void
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)));
3053 mp->mod_ref--;
3054 if (mp->mod_ref == 0)
3055 mod_uninstall_ref_zero++;
3056 if (mp->mod_want) {
3057 mp->mod_want = 0;
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;
3067 int retval;
3070 * Hold the module.
3072 modp = mod_hold_by_name_requisite(dep, filename);
3073 if (modp) {
3074 retval = mod_load(modp, 1);
3075 if (retval != 0) {
3076 mod_release_mod(modp);
3077 modp = NULL;
3079 *status = retval;
3080 } else {
3081 *status = ENOSPC;
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;
3093 return (modp);
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;
3103 int retval;
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)) {
3111 *r = ENOENT;
3112 return (NULL);
3117 * Hold the module.
3119 modp = mod_hold_by_name(name);
3120 if (modp) {
3121 retval = mod_load(modp, usepath);
3122 if (retval != 0) {
3123 mod_release_mod(modp);
3124 modp = NULL;
3125 *r = retval;
3126 } else {
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.
3135 mod_unload(modp);
3136 mod_release_mod(modp);
3137 modp = NULL;
3140 } else {
3141 *r = ENOSPC;
3143 return (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.
3157 * C'est la vie.
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")
3162 struct modctl *
3163 mod_find_by_filename(char *subdir, char *filename)
3165 struct modctl *mp;
3166 size_t sublen;
3168 ASSERT(!MUTEX_HELD(&mod_lock));
3169 if (subdir != NULL)
3170 sublen = strlen(subdir);
3171 else
3172 sublen = 0;
3174 mutex_enter(&mod_lock);
3175 mp = &modules;
3176 do {
3177 if (sublen) {
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);
3184 return (mp);
3186 } else if (strcmp(filename, mp->mod_filename) == 0) {
3187 mutex_exit(&mod_lock);
3188 return (mp);
3190 } while ((mp = mp->mod_next) != &modules);
3191 mutex_exit(&mod_lock);
3192 return (NULL);
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.
3201 static int
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)
3214 return (1);
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)
3224 return (1);
3226 return (0);
3229 static int
3230 mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
3232 int (*func)(struct modinfo *);
3233 int retval;
3235 ASSERT(modp->mod_busy);
3237 /* primary modules don't do getinfo */
3238 if (modp->mod_prim)
3239 return (0);
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.
3250 retval = 0;
3251 } else
3252 retval = (*func)(modinfop); /* call _info() function */
3254 if (moddebug & MODDEBUG_USERDEBUG)
3255 printf("Returned from _info, retval = %x\n", retval);
3257 return (retval);
3260 static void
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;
3272 /*ARGSUSED*/
3273 static struct modctl *
3274 allocate_modp(const char *filename, const char *modname)
3276 struct modctl *mp;
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);
3281 return (mp);
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.
3289 uintptr_t
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.
3300 char *
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.
3311 uintptr_t
3312 modlookup(const char *modname, const char *symname)
3314 struct modctl *modp;
3315 uintptr_t val;
3317 if ((modp = mod_hold_by_name(modname)) == NULL)
3318 return (0);
3319 val = kobj_lookup(modp->mod_mp, symname);
3320 mod_release_mod(modp);
3321 return (val);
3324 uintptr_t
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
3334 * for modules.
3336 void
3337 mod_askparams()
3339 static char s0[64];
3340 intptr_t fd;
3342 if ((fd = kobj_open(systemfile)) != -1L)
3343 kobj_close(fd);
3344 else
3345 systemfile = NULL;
3347 /*CONSTANTCONDITION*/
3348 while (1) {
3349 printf("Name of system file [%s]: ",
3350 systemfile ? systemfile : "/dev/null");
3352 console_gets(s0, sizeof (s0));
3354 if (s0[0] == '\0')
3355 break;
3356 else if (strcmp(s0, "/dev/null") == 0) {
3357 systemfile = NULL;
3358 break;
3359 } else {
3360 if ((fd = kobj_open(s0)) != -1L) {
3361 kobj_close(fd);
3362 systemfile = s0;
3363 break;
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.
3379 static int
3380 mod_load(struct modctl *mp, int usepath)
3382 int retval;
3383 struct modinfo *modinfop = NULL;
3384 struct loadmt lt;
3386 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3387 ASSERT(mp->mod_busy);
3389 if (mp->mod_loaded)
3390 return (0);
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,
3396 mp->mod_modname);
3398 return (ENXIO);
3400 if (moddebug & MODDEBUG_LOADMSG2)
3401 printf(loading_msg, mp->mod_filename, mp->mod_id);
3403 if (curthread != &t0) {
3404 lt.mp = mp;
3405 lt.usepath = usepath;
3406 lt.owner = curthread;
3407 sema_init(&lt.sema, 0, NULL, SEMA_DEFAULT, NULL);
3409 /* create thread to hand of call to */
3410 (void) thread_create(NULL, DEFAULTSTKSZ * 2,
3411 modload_thread, &lt, 0, &p0, TS_RUN, maxclsyspri);
3413 /* wait for thread to complete kobj_load_module */
3414 sema_p(&lt.sema);
3416 sema_destroy(&lt.sema);
3417 retval = lt.retval;
3418 } else
3419 retval = kobj_load_module(mp, usepath);
3421 if (mp->mod_mp) {
3422 ASSERT(retval == 0);
3423 mp->mod_loaded = 1;
3424 mp->mod_loadcnt++;
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;
3440 else {
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);
3463 } else {
3465 * If load failed then we need to release any requisites
3466 * that we had established.
3468 ASSERT(retval);
3469 mod_release_requisites(mp);
3471 if (moddebug & MODDEBUG_ERRMSG)
3472 printf("error loading '%s', error %d\n",
3473 mp->mod_filename, retval);
3475 return (retval);
3478 static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
3480 static void
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);
3505 return;
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,
3516 mp->mod_ref);
3517 return;
3520 /* reset stub functions to call the binder again */
3521 reset_stubs(mp);
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.
3528 mp->mod_loaded = 0;
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);
3561 static int
3562 modinstall(struct modctl *mp)
3564 int val;
3565 int (*func)(void);
3567 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3568 ASSERT(mp->mod_busy && mp->mod_loaded);
3570 if (mp->mod_installed)
3571 return (0);
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)
3578 return (ENXIO);
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).
3592 return (ENXIO);
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",
3608 mp->mod_filename);
3609 return (EFAULT);
3612 if (moddebug & MODDEBUG_USERDEBUG) {
3613 printf("breakpoint before calling %s:_init()\n",
3614 mp->mod_modname);
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);
3626 if (val == 0) {
3628 * Set the MODS_INSTALLED flag to enable this module
3629 * being called now.
3631 install_stubs(mp);
3632 mp->mod_installed = 1;
3633 } else if (moddebug & MODDEBUG_ERRMSG)
3634 printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
3636 return (val);
3639 int detach_driver_unconfig = 0;
3641 static int
3642 detach_driver(char *name)
3644 major_t major;
3645 int error;
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())
3652 return (0);
3654 major = ddi_name_to_major(name);
3655 if (major == DDI_MAJOR_T_NONE)
3656 return (0);
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";
3665 static int
3666 moduninstall(struct modctl *mp)
3668 int status = 0;
3669 int (*func)(void);
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)
3683 return (EBUSY);
3684 if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
3685 return (EALREADY);
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);
3704 return (EBUSY);
3706 if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
3707 mutex_exit(&mod_lock);
3708 return (EALREADY);
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))
3724 return (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",
3729 mp->mod_filename);
3730 return (EFAULT);
3733 /* call _fini() */
3734 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3735 ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
3737 status = (*func)();
3739 if (status == 0) {
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);
3759 } else {
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
3767 * removed.
3769 if (status == EALREADY)
3770 status = EINVAL;
3773 return (status);
3777 * Uninstall all modules.
3779 static void
3780 mod_uninstall_all(void)
3782 struct modctl *mp;
3783 int pass;
3784 modid_t modid;
3786 /* synchronize with any active modunload_disable() */
3787 modunload_begin();
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;
3804 modid = 0;
3805 while ((mp = mod_hold_next_by_id(modid)) != NULL) {
3806 modid = mp->mod_id;
3809 * Skip modules with the MOD_NOAUTOUNLOAD flag set
3811 if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
3812 mod_release_mod(mp);
3813 continue;
3816 if (moduninstall(mp) == 0) {
3817 mod_unload(mp);
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)
3825 break;
3827 if (pass >= mod_uninstall_pass_max)
3828 mod_uninstall_pass_exc++;
3830 (void) tsd_set(mod_autounload_key, NULL);
3831 modunload_end();
3834 /* wait for unloads that have begun before registering disable */
3835 void
3836 modunload_disable(void)
3838 mutex_enter(&modunload_wait_mutex);
3839 while (modunload_active_count) {
3840 modunload_wait++;
3841 cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3842 modunload_wait--;
3844 modunload_disable_count++;
3845 mutex_exit(&modunload_wait_mutex);
3848 /* mark end of disable and signal waiters */
3849 void
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 */
3860 void
3861 modunload_begin()
3863 mutex_enter(&modunload_wait_mutex);
3864 while (modunload_disable_count) {
3865 modunload_wait++;
3866 cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3867 modunload_wait--;
3869 modunload_active_count++;
3870 mutex_exit(&modunload_wait_mutex);
3873 /* mark end of unload and signal waiters */
3874 void
3875 modunload_end()
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);
3884 void
3885 mod_uninstall_daemon(void)
3887 callb_cpr_t cprinfo;
3888 clock_t ticks;
3890 mod_aul_thread = curthread;
3892 CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
3893 for (;;) {
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);
3906 } else {
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
3918 * called.
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.
3933 void
3934 modreap(void)
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.
3946 * Return values:
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) {
3966 mp->mod_want = 1;
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
3977 * problematic.
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 */
3986 mp->mod_busy = 1;
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);
3992 return (0);
3995 static struct modctl *
3996 mod_hold_by_name_common(struct modctl *dep, const char *filename)
3998 const char *modname;
3999 struct modctl *mp;
4000 char *curname, *newname;
4001 int found = 0;
4003 mutex_enter(&mod_lock);
4005 if ((modname = strrchr(filename, '/')) == NULL)
4006 modname = filename;
4007 else
4008 modname++;
4010 mp = &modules;
4011 do {
4012 if (strcmp(modname, mp->mod_modname) == 0) {
4013 found = 1;
4014 break;
4016 } while ((mp = mp->mod_next) != &modules);
4018 if (found == 0) {
4019 mp = allocate_modp(filename, modname);
4020 modadd(mp);
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().
4028 if (dep != NULL) {
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))
4037 mp = NULL;
4038 else {
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);
4069 return (mp);
4072 static struct modctl *
4073 mod_hold_by_name_requisite(struct modctl *dep, char *filename)
4075 return (mod_hold_by_name_common(dep, filename));
4078 struct modctl *
4079 mod_hold_by_name(const char *filename)
4081 return (mod_hold_by_name_common(NULL, filename));
4084 struct modctl *
4085 mod_hold_by_id(modid_t modid)
4087 struct modctl *mp;
4088 int found = 0;
4090 mutex_enter(&mod_lock);
4091 mp = &modules;
4092 do {
4093 if (mp->mod_id == modid) {
4094 found = 1;
4095 break;
4097 } while ((mp = mp->mod_next) != &modules);
4099 if ((found == 0) || mod_circdep(mp))
4100 mp = NULL;
4101 else
4102 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
4104 mutex_exit(&mod_lock);
4105 return (mp);
4108 static struct modctl *
4109 mod_hold_next_by_id(modid_t modid)
4111 struct modctl *mp;
4112 int found = 0;
4114 if (modid < -1)
4115 return (NULL);
4117 mutex_enter(&mod_lock);
4119 mp = &modules;
4120 do {
4121 if (mp->mod_id > modid) {
4122 found = 1;
4123 break;
4125 } while ((mp = mp->mod_next) != &modules);
4127 if ((found == 0) || mod_circdep(mp))
4128 mp = NULL;
4129 else
4130 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
4132 mutex_exit(&mod_lock);
4133 return (mp);
4136 static void
4137 mod_release(struct modctl *mp)
4139 ASSERT(MUTEX_HELD(&mod_lock));
4140 ASSERT(mp->mod_busy);
4142 mp->mod_busy = 0;
4143 mp->mod_inprogress_thread = NULL;
4144 if (mp->mod_want) {
4145 mp->mod_want = 0;
4146 cv_broadcast(&mod_cv);
4150 void
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);
4156 mod_release(mp);
4157 mutex_exit(&mod_lock);
4160 modid_t
4161 mod_name_to_modid(char *filename)
4163 char *modname;
4164 struct modctl *mp;
4166 mutex_enter(&mod_lock);
4168 if ((modname = strrchr(filename, '/')) == NULL)
4169 modname = filename;
4170 else
4171 modname++;
4173 mp = &modules;
4174 do {
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);
4182 return (-1);
4187 mod_remove_by_name(char *name)
4189 struct modctl *mp;
4190 int retval;
4192 mp = mod_hold_by_name(name);
4194 if (mp == NULL)
4195 return (EINVAL);
4197 if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
4199 * Do not unload forceloaded modules
4201 mod_release_mod(mp);
4202 return (0);
4205 if ((retval = moduninstall(mp)) == 0) {
4206 mod_unload(mp);
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);
4211 return (retval);
4215 * Record that module "dep" is dependent on module "on_mod."
4217 static void
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)
4234 break;
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;
4241 *pmlnp = new;
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
4250 * yet be installed.
4252 on_mod->mod_ref++;
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.
4263 void
4264 mod_release_requisites(struct modctl *modp)
4266 struct modctl_list *modl;
4267 struct modctl_list *next;
4268 struct modctl *req;
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);
4280 req->mod_ref--;
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),
4294 KM_SLEEP);
4295 new->modl_modp = req;
4297 if (start == NULL)
4298 mod_garbage = start = new;
4299 else {
4300 mod_garbage->modl_next = new;
4301 mod_garbage = 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;
4317 ASSERT(mp != NULL);
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)
4330 mod_unload(mp);
4331 ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
4332 (mp->mod_ref > 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
4345 * "dep" depends.
4347 struct modctl *
4348 mod_load_requisite(struct modctl *dep, char *on)
4350 struct modctl *on_mod;
4351 int retval;
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);
4359 return (on_mod);
4362 static int
4363 mod_install_requisites(struct modctl *modp)
4365 struct modctl_list *modl;
4366 struct modctl *req;
4367 int status = 0;
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);
4379 if (status != 0)
4380 break;
4382 return (status);
4386 * returns 1 if this thread is doing autounload, 0 otherwise.
4387 * see mod_uninstall_all.
4390 mod_in_autounload()
4392 return ((int)(uintptr_t)tsd_get(mod_autounload_key));
4396 * gmatch adapted from libc, stripping the wchar stuff
4398 #define popchar(p, c) { \
4399 c = *p++; \
4400 if (c == 0) { \
4401 return (0); \
4406 gmatch(const char *s, const char *p)
4408 int c, sc;
4409 int ok, lc, notflag;
4411 sc = *s++;
4412 c = *p++;
4413 if (c == 0)
4414 return (sc == c); /* nothing matches nothing */
4416 switch (c) {
4417 case '\\':
4418 /* skip to quoted character */
4419 popchar(p, c);
4420 /*FALLTHRU*/
4422 default:
4423 /* straight comparison */
4424 if (c != sc)
4425 return (0);
4426 /*FALLTHRU*/
4428 case '?':
4429 /* first char matches, move to remainder */
4430 return (sc != '\0' ? gmatch(s, p) : 0);
4433 case '*':
4434 while (*p == '*')
4435 p++;
4437 /* * matches everything */
4438 if (*p == 0)
4439 return (1);
4441 /* undo skip at the beginning & iterate over substrings */
4442 --s;
4443 while (*s) {
4444 if (gmatch(s, p))
4445 return (1);
4446 s++;
4448 return (0);
4450 case '[':
4451 /* match any char within [] */
4452 if (sc == 0)
4453 return (0);
4455 ok = lc = notflag = 0;
4457 if (*p == '!') {
4458 notflag = 1;
4459 p++;
4461 popchar(p, c);
4463 do {
4464 if (c == '-' && lc && *p != ']') {
4465 /* test sc against range [c1-c2] */
4466 popchar(p, c);
4467 if (c == '\\') {
4468 popchar(p, c);
4471 if (notflag) {
4472 /* return 0 on mismatch */
4473 if (lc <= sc && sc <= c)
4474 return (0);
4475 ok++;
4476 } else if (lc <= sc && sc <= c) {
4477 ok++;
4479 /* keep going, may get a match next */
4480 } else if (c == '\\') {
4481 /* skip to quoted character */
4482 popchar(p, c);
4484 lc = c;
4485 if (notflag) {
4486 if (sc == lc)
4487 return (0);
4488 ok++;
4489 } else if (sc == lc) {
4490 ok++;
4492 popchar(p, c);
4493 } while (c != ']');
4495 /* recurse on remainder of string */
4496 return (ok ? gmatch(s, p) : 0);
4498 /*NOTREACHED*/
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
4530 * that alias.
4533 static int
4534 dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
4536 major_t major;
4537 struct devnames *dnp;
4538 mperm_t *mp;
4539 char *alias = NULL;
4540 dev_info_t *cdevi;
4541 int circ;
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
4552 * alias node.
4554 if (ddi_hold_installed_driver(major) == NULL)
4555 return (1);
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);
4565 break;
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);
4578 return (1);
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) {
4591 break;
4595 if (mp) {
4596 if (moddebug & MODDEBUG_MP_MATCH) {
4597 cmn_err(CE_CONT,
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)
4616 major_t major;
4617 char *minor_name;
4618 struct devnames *dnp;
4619 mperm_t *mp;
4620 int is_clone = 0;
4622 if (!minorperm_loaded) {
4623 if (moddebug & MODDEBUG_MINORPERM)
4624 cmn_err(CE_CONT,
4625 "%s: minor perm not yet loaded\n", name);
4626 return (1);
4629 minor_name = strchr(name, ':');
4630 if (minor_name == NULL)
4631 return (1);
4632 minor_name++;
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);
4645 return (1);
4647 is_clone = 1;
4648 } else {
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) {
4665 break;
4668 if (mp == NULL) {
4669 if (is_clone)
4670 mp = dnp->dn_mperm_clone;
4671 if (mp == NULL)
4672 mp = dnp->dn_mperm_wild;
4675 if (mp) {
4676 if (moddebug & MODDEBUG_MP_MATCH) {
4677 cmn_err(CE_CONT,
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
4704 /*ARGSUSED*/
4705 ddi_modhandle_t
4706 ddi_modopen(const char *modname, int mode, int *errnop)
4708 char *subdir;
4709 char *mod;
4710 int subdirlen;
4711 struct modctl *hmodp = NULL;
4712 int retval = EINVAL;
4714 ASSERT(modname && (mode == KRTLD_MODE_FIRST));
4715 if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
4716 goto out;
4718 /* find last '/' in modname */
4719 mod = strrchr(modname, '/');
4721 if (mod) {
4722 /* for subdir string without modification to argument */
4723 mod++;
4724 subdirlen = mod - modname;
4725 subdir = kmem_alloc(subdirlen, KM_SLEEP);
4726 (void) strlcpy(subdir, modname, subdirlen);
4727 } else {
4728 subdirlen = 0;
4729 subdir = "misc";
4730 mod = (char *)modname;
4733 /* reference load with errno return value */
4734 retval = modrload(subdir, mod, &hmodp);
4736 if (subdirlen)
4737 kmem_free(subdir, subdirlen);
4739 out: if (errnop)
4740 *errnop = retval;
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 */
4752 void *
4753 ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
4755 struct modctl *hmodp = (struct modctl *)h;
4756 void *f;
4757 int retval;
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)) {
4762 f = NULL;
4763 retval = EINVAL;
4764 } else {
4765 f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
4766 if (f)
4767 retval = 0;
4768 else
4769 retval = ENOTSUP;
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);
4777 if (errnop)
4778 *errnop = retval;
4779 return (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;
4788 int retval;
4790 ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
4791 if ((hmodp == NULL) ||
4792 (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
4793 retval = EINVAL;
4794 goto out;
4797 retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
4798 if (retval == EBUSY)
4799 retval = 0; /* EBUSY is not an error */
4801 if (retval == 0) {
4802 ASSERT(hmodp == modp);
4803 if (hmodp != modp)
4804 retval = EINVAL;
4807 out: if (moddebug & MODDEBUG_DDI_MOD)
4808 printf("ddi_modclose %s: %d\n",
4809 hmodp ? hmodp->mod_modname : "<unknown>", retval);
4811 return (retval);