1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 */
26
27 /*
28 * modctl system call for loadable module support.
29 */
30
31 #include <sys/param.h>
32 #include <sys/user.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/file.h>
36 #include <sys/stat.h>
37 #include <sys/conf.h>
38 #include <sys/time.h>
39 #include <sys/reboot.h>
40 #include <sys/fs/ufs_fsdir.h>
41 #include <sys/kmem.h>
42 #include <sys/sysconf.h>
43 #include <sys/cmn_err.h>
44 #include <sys/ddi.h>
45 #include <sys/sunddi.h>
46 #include <sys/sunndi.h>
47 #include <sys/ndi_impldefs.h>
48 #include <sys/ddi_impldefs.h>
49 #include <sys/ddi_implfuncs.h>
50 #include <sys/bootconf.h>
51 #include <sys/dc_ki.h>
52 #include <sys/cladm.h>
53 #include <sys/dtrace.h>
54 #include <sys/kdi.h>
55
56 #include <sys/devpolicy.h>
57 #include <sys/modctl.h>
58 #include <sys/kobj.h>
59 #include <sys/devops.h>
60 #include <sys/autoconf.h>
61 #include <sys/hwconf.h>
62 #include <sys/callb.h>
63 #include <sys/debug.h>
64 #include <sys/cpuvar.h>
65 #include <sys/sysmacros.h>
66 #include <sys/sysevent.h>
67 #include <sys/sysevent_impl.h>
68 #include <sys/sysevent/eventdefs.h>
69 #include <sys/sysevent/dev.h>
70 #include <sys/instance.h>
71 #include <sys/modhash.h>
72 #include <sys/modhash_impl.h>
73 #include <sys/dacf_impl.h>
74 #include <sys/vfs.h>
75 #include <sys/pathname.h>
76 #include <sys/console.h>
77 #include <sys/policy.h>
78 #include <ipp/ipp_impl.h>
79 #include <sys/fs/dv_node.h>
80 #include <sys/strsubr.h>
81 #include <sys/fs/sdev_impl.h>
82
83 static int mod_circdep(struct modctl *);
84 static int modinfo(modid_t, struct modinfo *);
85
86 static void mod_uninstall_all(void);
87 static int mod_getinfo(struct modctl *, struct modinfo *);
88 static struct modctl *allocate_modp(const char *, const char *);
89
90 static int mod_load(struct modctl *, int);
91 static void mod_unload(struct modctl *);
92 static int modinstall(struct modctl *);
93 static int moduninstall(struct modctl *);
94
95 static struct modctl *mod_hold_by_name_common(struct modctl *, const char *);
96 static struct modctl *mod_hold_next_by_id(modid_t);
97 static struct modctl *mod_hold_loaded_mod(struct modctl *, char *, int *);
98 static struct modctl *mod_hold_installed_mod(char *, int, int, int *);
99
100 static void mod_release(struct modctl *);
101 static void mod_make_requisite(struct modctl *, struct modctl *);
102 static int mod_install_requisites(struct modctl *);
103 static void check_esc_sequences(char *, char *);
104 static struct modctl *mod_hold_by_name_requisite(struct modctl *, char *);
105
106 /*
107 * module loading thread control structure. Calls to kobj_load_module()() are
108 * handled off to a separate thead using this structure.
109 */
110 struct loadmt {
111 ksema_t sema;
112 struct modctl *mp;
113 int usepath;
114 kthread_t *owner;
115 int retval;
116 };
117
118 static void modload_thread(struct loadmt *);
119
120 kcondvar_t mod_cv;
121 kcondvar_t mod_uninstall_cv; /* Communication between swapper */
122 /* and the uninstall daemon. */
123 kmutex_t mod_lock; /* protects &modules insert linkage, */
124 /* mod_busy, mod_want, and mod_ref. */
125 /* blocking operations while holding */
126 /* mod_lock should be avoided */
127 kmutex_t mod_uninstall_lock; /* protects mod_uninstall_cv */
128 kthread_id_t mod_aul_thread;
129
130 int modunload_wait;
131 kmutex_t modunload_wait_mutex;
132 kcondvar_t modunload_wait_cv;
133 int modunload_active_count;
134 int modunload_disable_count;
135
136 int isminiroot; /* set if running as miniroot */
137 int modrootloaded; /* set after root driver and fs are loaded */
138 volatile int moddebug = 0x0; /* debug flags for module writers */
139 int swaploaded; /* set after swap driver and fs are loaded */
140 int bop_io_quiesced = 0; /* set when BOP I/O can no longer be used */
141 int last_module_id;
142 clock_t mod_uninstall_interval = 0;
143 int mod_uninstall_pass_max = 6;
144 int mod_uninstall_ref_zero; /* # modules that went mod_ref == 0 */
145 int mod_uninstall_pass_exc; /* mod_uninstall_all left new stuff */
146
147 int ddi_modclose_unload = 1; /* 0 -> just decrement reference */
148
149 int devcnt_incr = 256; /* allow for additional drivers */
150 int devcnt_min = 512; /* and always at least this number */
151
152 struct devnames *devnamesp;
153 struct devnames orphanlist;
154
155 krwlock_t devinfo_tree_lock; /* obsolete, to be removed */
156
157 #define MAJBINDFILE "/etc/name_to_major"
158 #define SYSBINDFILE "/etc/name_to_sysnum"
159
160 static char majbind[] = MAJBINDFILE;
161 static char sysbind[] = SYSBINDFILE;
162 static uint_t mod_autounload_key; /* for module autounload detection */
163
164 extern int obpdebug;
165
166 #define DEBUGGER_PRESENT ((boothowto & RB_DEBUG) || (obpdebug != 0))
167
168 static int minorperm_loaded = 0;
169
170 void
171 mod_setup(void)
172 {
173 struct sysent *callp;
174 int callnum, exectype;
175 int num_devs;
176 int i;
177
178 /*
179 * Initialize the list of loaded driver dev_ops.
180 * XXX - This must be done before reading the system file so that
181 * forceloads of drivers will work.
182 */
183 num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
184 /*
185 * Since read_binding_file is common code, it doesn't enforce that all
186 * of the binding file entries have major numbers <= MAXMAJ32. Thus,
187 * ensure that we don't allocate some massive amount of space due to a
188 * bad entry. We can't have major numbers bigger than MAXMAJ32
189 * until file system support for larger major numbers exists.
190 */
191
192 /*
193 * Leave space for expansion, but not more than L_MAXMAJ32
194 */
195 devcnt = MIN(num_devs + devcnt_incr, L_MAXMAJ32);
196 devcnt = MAX(devcnt, devcnt_min);
197 devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
198 for (i = 0; i < devcnt; i++)
199 devopsp[i] = &mod_nodev_ops;
200
201 init_devnamesp(devcnt);
202
203 /*
204 * Sync up with the work that the stand-alone linker has already done.
205 */
206 (void) kobj_sync();
207
208 if (boothowto & RB_DEBUG)
209 kdi_dvec_modavail();
210
211 make_aliases(mb_hashtab);
212
213 /*
214 * Initialize streams device implementation structures.
215 */
216 devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
217
218 /*
219 * If the cl_bootstrap module is present,
220 * we should be configured as a cluster. Loading this module
221 * will set "cluster_bootflags" to non-zero.
222 */
223 (void) modload("misc", "cl_bootstrap");
224
225 (void) read_binding_file(sysbind, sb_hashtab, make_mbind);
226 init_syscallnames(NSYSCALL);
227
228 /*
229 * Start up dynamic autoconfiguration framework (dacf).
230 */
231 mod_hash_init();
232 dacf_init();
233
234 /*
235 * Start up IP policy framework (ipp).
236 */
237 ipp_init();
238
239 /*
240 * Allocate loadable native system call locks.
241 */
242 for (callnum = 0, callp = sysent; callnum < NSYSCALL;
243 callnum++, callp++) {
244 if (LOADABLE_SYSCALL(callp)) {
245 if (mod_getsysname(callnum) != NULL) {
246 callp->sy_lock =
247 kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
248 rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
249 } else {
250 callp->sy_flags &= ~SE_LOADABLE;
251 callp->sy_callc = nosys;
252 }
253 #ifdef DEBUG
254 } else {
255 /*
256 * Do some sanity checks on the sysent table
257 */
258 switch (callp->sy_flags & SE_RVAL_MASK) {
259 case SE_32RVAL1:
260 /* only r_val1 returned */
261 case SE_32RVAL1 | SE_32RVAL2:
262 /* r_val1 and r_val2 returned */
263 case SE_64RVAL:
264 /* 64-bit rval returned */
265 break;
266 default:
267 cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
268 callnum, callp->sy_flags);
269 }
270 #endif
271 }
272 }
273
274 #ifdef _SYSCALL32_IMPL
275 /*
276 * Allocate loadable system call locks for 32-bit compat syscalls
277 */
278 for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
279 callnum++, callp++) {
280 if (LOADABLE_SYSCALL(callp)) {
281 if (mod_getsysname(callnum) != NULL) {
282 callp->sy_lock =
283 kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
284 rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
285 } else {
286 callp->sy_flags &= ~SE_LOADABLE;
287 callp->sy_callc = nosys;
288 }
289 #ifdef DEBUG
290 } else {
291 /*
292 * Do some sanity checks on the sysent table
293 */
294 switch (callp->sy_flags & SE_RVAL_MASK) {
295 case SE_32RVAL1:
296 /* only r_val1 returned */
297 case SE_32RVAL1 | SE_32RVAL2:
298 /* r_val1 and r_val2 returned */
299 case SE_64RVAL:
300 /* 64-bit rval returned */
301 break;
302 default:
303 cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
304 callnum, callp->sy_flags);
305 goto skip;
306 }
307
308 /*
309 * Cross-check the native and compatibility tables.
310 */
311 if (callp->sy_callc == nosys ||
312 sysent[callnum].sy_callc == nosys)
313 continue;
314 /*
315 * If only one or the other slot is loadable, then
316 * there's an error -- they should match!
317 */
318 if ((callp->sy_callc == loadable_syscall) ^
319 (sysent[callnum].sy_callc == loadable_syscall)) {
320 cmn_err(CE_WARN, "sysent[%d] loadable?",
321 callnum);
322 }
323 /*
324 * This is more of a heuristic test -- if the
325 * system call returns two values in the 32-bit
326 * world, it should probably return two 32-bit
327 * values in the 64-bit world too.
328 */
329 if (((callp->sy_flags & SE_32RVAL2) == 0) ^
330 ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
331 cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
332 callnum);
333 }
334 skip:;
335 #endif /* DEBUG */
336 }
337 }
338 #endif /* _SYSCALL32_IMPL */
339
340 /*
341 * Allocate loadable exec locks. (Assumes all execs are loadable)
342 */
343 for (exectype = 0; exectype < nexectype; exectype++) {
344 execsw[exectype].exec_lock =
345 kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
346 rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
347 }
348
349 read_class_file();
350
351 /* init thread specific structure for mod_uninstall_all */
352 tsd_create(&mod_autounload_key, NULL);
353 }
354
355 static int
356 modctl_modload(int use_path, char *filename, int *rvp)
357 {
358 struct modctl *modp;
359 int retval = 0;
360 char *filenamep;
361 int modid;
362
363 filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
364
365 if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
366 retval = EFAULT;
367 goto out;
368 }
369
370 filenamep[MOD_MAXPATH - 1] = 0;
371 modp = mod_hold_installed_mod(filenamep, use_path, 0, &retval);
372
373 if (modp == NULL)
374 goto out;
375
376 modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
377 modid = modp->mod_id;
378 mod_release_mod(modp);
379 CPU_STATS_ADDQ(CPU, sys, modload, 1);
380 if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
381 retval = EFAULT;
382 out:
383 kmem_free(filenamep, MOD_MAXPATH);
384
385 return (retval);
386 }
387
388 static int
389 modctl_modunload(modid_t id)
390 {
391 int rval = 0;
392
393 if (id == 0) {
394 #ifdef DEBUG
395 /*
396 * Turn on mod_uninstall_daemon
397 */
398 if (mod_uninstall_interval == 0) {
399 mod_uninstall_interval = 60;
400 modreap();
401 return (rval);
402 }
403 #endif
404 mod_uninstall_all();
405 } else {
406 rval = modunload(id);
407 }
408 return (rval);
409 }
410
411 static int
412 modctl_modinfo(modid_t id, struct modinfo *umodi)
413 {
414 int retval;
415 struct modinfo modi;
416 #if defined(_SYSCALL32_IMPL)
417 int nobase;
418 struct modinfo32 modi32;
419 #endif
420
421 if (get_udatamodel() == DATAMODEL_NATIVE) {
422 if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
423 return (EFAULT);
424 }
425 #ifdef _SYSCALL32_IMPL
426 else {
427 bzero(&modi, sizeof (modi));
428 if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
429 return (EFAULT);
430 modi.mi_info = modi32.mi_info;
431 modi.mi_id = modi32.mi_id;
432 modi.mi_nextid = modi32.mi_nextid;
433 nobase = modi.mi_info & MI_INFO_NOBASE;
434 }
435 #endif
436 /*
437 * This flag is -only- for the kernels use.
438 */
439 modi.mi_info &= ~MI_INFO_LINKAGE;
440
441 retval = modinfo(id, &modi);
442 if (retval)
443 return (retval);
444
445 if (get_udatamodel() == DATAMODEL_NATIVE) {
446 if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
447 retval = EFAULT;
448 #ifdef _SYSCALL32_IMPL
449 } else {
450 int i;
451
452 if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
453 return (EOVERFLOW);
454
455 modi32.mi_info = modi.mi_info;
456 modi32.mi_state = modi.mi_state;
457 modi32.mi_id = modi.mi_id;
458 modi32.mi_nextid = modi.mi_nextid;
459 modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
460 modi32.mi_size = modi.mi_size;
461 modi32.mi_rev = modi.mi_rev;
462 modi32.mi_loadcnt = modi.mi_loadcnt;
463 bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
464 for (i = 0; i < MODMAXLINK32; i++) {
465 modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
466 bcopy(modi.mi_msinfo[i].msi_linkinfo,
467 modi32.mi_msinfo[i].msi_linkinfo,
468 sizeof (modi32.mi_msinfo[0].msi_linkinfo));
469 }
470 if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
471 retval = EFAULT;
472 #endif
473 }
474
475 return (retval);
476 }
477
478 /*
479 * Return the last major number in the range of permissible major numbers.
480 */
481 /*ARGSUSED*/
482 static int
483 modctl_modreserve(modid_t id, int *data)
484 {
485 if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
486 return (EFAULT);
487 return (0);
488 }
489
490 /* Add/Remove driver and binding aliases */
491 static int
492 modctl_update_driver_aliases(int add, int *data)
493 {
494 struct modconfig mc;
495 int i, n, rv = 0;
496 struct aliases alias;
497 struct aliases *ap;
498 char name[MAXMODCONFNAME];
499 char cname[MAXMODCONFNAME];
500 char *drvname;
501 int resid;
502 struct alias_info {
503 char *alias_name;
504 int alias_resid;
505 } *aliases, *aip;
506
507 bzero(&mc, sizeof (struct modconfig));
508 if (get_udatamodel() == DATAMODEL_NATIVE) {
509 if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
510 return (EFAULT);
511 }
512 #ifdef _SYSCALL32_IMPL
513 else {
514 struct modconfig32 modc32;
515 if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
516 return (EFAULT);
517 else {
518 bcopy(modc32.drvname, mc.drvname,
519 sizeof (modc32.drvname));
520 bcopy(modc32.drvclass, mc.drvclass,
521 sizeof (modc32.drvclass));
522 mc.major = modc32.major;
523 mc.flags = modc32.flags;
524 mc.num_aliases = modc32.num_aliases;
525 mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
526 }
527 }
528 #endif
529
530 /*
531 * If the driver is already in the mb_hashtab, and the name given
532 * doesn't match that driver's name, fail. Otherwise, pass, since
533 * we may be adding aliases.
534 */
535 drvname = mod_major_to_name(mc.major);
536 if ((drvname != NULL) && strcmp(drvname, mc.drvname) != 0)
537 return (EINVAL);
538
539 /*
540 * Precede alias removal by unbinding as many devices as possible.
541 */
542 if (add == 0) {
543 (void) i_ddi_unload_drvconf(mc.major);
544 i_ddi_unbind_devs(mc.major);
545 }
546
547 /*
548 * Add/remove each supplied driver alias to/from mb_hashtab
549 */
550 ap = mc.ap;
551 if (mc.num_aliases > 0)
552 aliases = kmem_zalloc(
553 mc.num_aliases * sizeof (struct alias_info), KM_SLEEP);
554 aip = aliases;
555 for (i = 0; i < mc.num_aliases; i++) {
556 bzero(&alias, sizeof (struct aliases));
557 if (get_udatamodel() == DATAMODEL_NATIVE) {
558 if (copyin(ap, &alias, sizeof (struct aliases)) != 0) {
559 rv = EFAULT;
560 goto error;
561 }
562 if (alias.a_len > MAXMODCONFNAME) {
563 rv = EINVAL;
564 goto error;
565 }
566 if (copyin(alias.a_name, name, alias.a_len) != 0) {
567 rv = EFAULT;
568 goto error;
569 }
570 if (name[alias.a_len - 1] != '\0') {
571 rv = EINVAL;
572 goto error;
573 }
574 }
575 #ifdef _SYSCALL32_IMPL
576 else {
577 struct aliases32 al32;
578 bzero(&al32, sizeof (struct aliases32));
579 if (copyin(ap, &al32, sizeof (struct aliases32)) != 0) {
580 rv = EFAULT;
581 goto error;
582 }
583 if (al32.a_len > MAXMODCONFNAME) {
584 rv = EINVAL;
585 goto error;
586 }
587 if (copyin((void *)(uintptr_t)al32.a_name,
588 name, al32.a_len) != 0) {
589 rv = EFAULT;
590 goto error;
591 }
592 if (name[al32.a_len - 1] != '\0') {
593 rv = EINVAL;
594 goto error;
595 }
596 alias.a_next = (void *)(uintptr_t)al32.a_next;
597 }
598 #endif
599 check_esc_sequences(name, cname);
600 aip->alias_name = strdup(cname);
601 ap = alias.a_next;
602 aip++;
603 }
604
605 if (add == 0) {
606 ap = mc.ap;
607 resid = 0;
608 aip = aliases;
609 /* attempt to unbind all devices bound to each alias */
610 for (i = 0; i < mc.num_aliases; i++) {
611 n = i_ddi_unbind_devs_by_alias(
612 mc.major, aip->alias_name);
613 resid += n;
614 aip->alias_resid = n;
615 }
616
617 /*
618 * If some device bound to an alias remains in use,
619 * and override wasn't specified, no change is made to
620 * the binding state and we fail the operation.
621 */
622 if (resid > 0 && ((mc.flags & MOD_UNBIND_OVERRIDE) == 0)) {
623 rv = EBUSY;
624 goto error;
625 }
626
627 /*
628 * No device remains bound of any of the aliases,
629 * or force was requested. Mark each alias as
630 * inactive via delete_mbind so no future binds
631 * to this alias take place and that a new
632 * binding can be established.
633 */
634 aip = aliases;
635 for (i = 0; i < mc.num_aliases; i++) {
636 if (moddebug & MODDEBUG_BINDING)
637 cmn_err(CE_CONT, "Removing binding for %s "
638 "(%d active references)\n",
639 aip->alias_name, aip->alias_resid);
640 delete_mbind(aip->alias_name, mb_hashtab);
641 aip++;
642 }
643 rv = 0;
644 } else {
645 aip = aliases;
646 for (i = 0; i < mc.num_aliases; i++) {
647 if (moddebug & MODDEBUG_BINDING)
648 cmn_err(CE_NOTE, "Adding binding for '%s'\n",
649 aip->alias_name);
650 (void) make_mbind(aip->alias_name,
651 mc.major, NULL, mb_hashtab);
652 aip++;
653 }
654 /*
655 * Try to establish an mbinding for mc.drvname, and add it to
656 * devnames. Add class if any after establishing the major
657 * number.
658 */
659 (void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
660 if ((rv = make_devname(mc.drvname, mc.major,
661 (mc.flags & MOD_ADDMAJBIND_UPDATE) ?
662 DN_DRIVER_INACTIVE : 0)) != 0) {
663 goto error;
664 }
665
666 if (mc.drvclass[0] != '\0')
667 add_class(mc.drvname, mc.drvclass);
668 if ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0) {
669 (void) i_ddi_load_drvconf(mc.major);
670 }
671 }
672
673 /*
674 * Ensure that all nodes are bound to the most appropriate driver
675 * possible, attempting demotion and rebind when a more appropriate
676 * driver now exists. But not when adding a driver update-only.
677 */
678 if ((add == 0) || ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0)) {
679 i_ddi_bind_devs();
680 i_ddi_di_cache_invalidate();
681 }
682
683 error:
684 if (mc.num_aliases > 0) {
685 aip = aliases;
686 for (i = 0; i < mc.num_aliases; i++) {
687 if (aip->alias_name != NULL)
688 strfree(aip->alias_name);
689 aip++;
690 }
691 kmem_free(aliases, mc.num_aliases * sizeof (struct alias_info));
692 }
693 return (rv);
694 }
695
696 static int
697 modctl_add_driver_aliases(int *data)
698 {
699 return (modctl_update_driver_aliases(1, data));
700 }
701
702 static int
703 modctl_remove_driver_aliases(int *data)
704 {
705 return (modctl_update_driver_aliases(0, data));
706 }
707
708 static int
709 modctl_rem_major(major_t major)
710 {
711 struct devnames *dnp;
712
713 if (major >= devcnt)
714 return (EINVAL);
715
716 /* mark devnames as removed */
717 dnp = &devnamesp[major];
718 LOCK_DEV_OPS(&dnp->dn_lock);
719 if (dnp->dn_name == NULL ||
720 (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
721 UNLOCK_DEV_OPS(&dnp->dn_lock);
722 return (EINVAL);
723 }
724 dnp->dn_flags |= DN_DRIVER_REMOVED;
725 pm_driver_removed(major);
726 UNLOCK_DEV_OPS(&dnp->dn_lock);
727
728 (void) i_ddi_unload_drvconf(major);
729 i_ddi_unbind_devs(major);
730 i_ddi_bind_devs();
731 i_ddi_di_cache_invalidate();
732
733 /* purge all the bindings to this driver */
734 purge_mbind(major, mb_hashtab);
735 return (0);
736 }
737
738 static struct vfs *
739 path_to_vfs(char *name)
740 {
741 vnode_t *vp;
742 struct vfs *vfsp;
743
744 if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
745 return (NULL);
746
747 vfsp = vp->v_vfsp;
748 VN_RELE(vp);
749 return (vfsp);
750 }
751
752 static int
753 new_vfs_in_modpath()
754 {
755 static int n_modpath = 0;
756 static char *modpath_copy;
757 static struct pathvfs {
758 char *path;
759 struct vfs *vfsp;
760 } *pathvfs;
761
762 int i, new_vfs = 0;
763 char *tmp, *tmp1;
764 struct vfs *vfsp;
765
766 if (n_modpath != 0) {
767 for (i = 0; i < n_modpath; i++) {
768 vfsp = path_to_vfs(pathvfs[i].path);
769 if (vfsp != pathvfs[i].vfsp) {
770 pathvfs[i].vfsp = vfsp;
771 if (vfsp)
772 new_vfs = 1;
773 }
774 }
775 return (new_vfs);
776 }
777
778 /*
779 * First call, initialize the pathvfs structure
780 */
781 modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
782 tmp = modpath_copy;
783 n_modpath = 1;
784 tmp1 = strchr(tmp, ' ');
785 while (tmp1) {
786 *tmp1 = '\0';
787 n_modpath++;
788 tmp = tmp1 + 1;
789 tmp1 = strchr(tmp, ' ');
790 }
791
792 pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
793 tmp = modpath_copy;
794 for (i = 0; i < n_modpath; i++) {
795 pathvfs[i].path = tmp;
796 vfsp = path_to_vfs(tmp);
797 pathvfs[i].vfsp = vfsp;
798 tmp += strlen(tmp) + 1;
799 }
800 return (1); /* always reread driver.conf the first time */
801 }
802
803 static int
804 modctl_load_drvconf(major_t major, int flags)
805 {
806 int ret;
807
808 /*
809 * devfsadm -u - read all new driver.conf files
810 * and bind and configure devices for new drivers.
811 */
812 if (flags & MOD_LOADDRVCONF_RECONF) {
813 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
814 i_ddi_bind_devs();
815 i_ddi_di_cache_invalidate();
816 return (0);
817 }
818
819 /*
820 * update_drv <drv> - reload driver.conf for the specified driver
821 */
822 if (major != DDI_MAJOR_T_NONE) {
823 ret = i_ddi_load_drvconf(major);
824 if (ret == 0)
825 i_ddi_bind_devs();
826 return (ret);
827 }
828
829 /*
830 * We are invoked to rescan new driver.conf files. It is
831 * only necessary if a new file system was mounted in the
832 * module_path. Because rescanning driver.conf files can
833 * take some time on older platforms (sun4m), the following
834 * code skips unnecessary driver.conf rescans to optimize
835 * boot performance.
836 */
837 if (new_vfs_in_modpath()) {
838 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
839 /*
840 * If we are still initializing io subsystem,
841 * load drivers with ddi-forceattach property
842 */
843 if (!i_ddi_io_initialized())
844 i_ddi_forceattach_drivers();
845 }
846 return (0);
847 }
848
849 /*
850 * Unload driver.conf file and follow up by attempting
851 * to rebind devices to more appropriate driver.
852 */
853 static int
854 modctl_unload_drvconf(major_t major)
855 {
856 int ret;
857
858 if (major >= devcnt)
859 return (EINVAL);
860
861 ret = i_ddi_unload_drvconf(major);
862 if (ret != 0)
863 return (ret);
864 (void) i_ddi_unbind_devs(major);
865 i_ddi_bind_devs();
866
867 return (0);
868 }
869
870 static void
871 check_esc_sequences(char *str, char *cstr)
872 {
873 int i;
874 size_t len;
875 char *p;
876
877 len = strlen(str);
878 for (i = 0; i < len; i++, str++, cstr++) {
879 if (*str != '\\') {
880 *cstr = *str;
881 } else {
882 p = str + 1;
883 /*
884 * we only handle octal escape sequences for SPACE
885 */
886 if (*p++ == '0' && *p++ == '4' && *p == '0') {
887 *cstr = ' ';
888 str += 3;
889 } else {
890 *cstr = *str;
891 }
892 }
893 }
894 *cstr = 0;
895 }
896
897 static int
898 modctl_getmodpathlen(int *data)
899 {
900 int len;
901 len = strlen(default_path);
902 if (copyout(&len, data, sizeof (len)) != 0)
903 return (EFAULT);
904 return (0);
905 }
906
907 static int
908 modctl_getmodpath(char *data)
909 {
910 if (copyout(default_path, data, strlen(default_path) + 1) != 0)
911 return (EFAULT);
912 return (0);
913 }
914
915 static int
916 modctl_read_sysbinding_file(void)
917 {
918 (void) read_binding_file(sysbind, sb_hashtab, make_mbind);
919 return (0);
920 }
921
922 static int
923 modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
924 {
925 char name[256];
926 int retval;
927 major_t major;
928
929 if (ulen == 0)
930 return (EINVAL);
931 if ((retval = copyinstr(uname, name,
932 (ulen < 256) ? ulen : 256, 0)) != 0)
933 return (retval);
934 if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
935 return (ENODEV);
936 if (copyout(&major, umajorp, sizeof (major_t)) != 0)
937 return (EFAULT);
938 return (0);
939 }
940
941 static char **
942 convert_constraint_string(char *constraints, size_t len)
943 {
944 int i;
945 int n;
946 char *p;
947 char **array;
948
949 ASSERT(constraints != NULL);
950 ASSERT(len > 0);
951
952 for (i = 0, p = constraints; strlen(p) > 0; i++, p += strlen(p) + 1)
953 ;
954
955 n = i;
956
957 if (n == 0) {
958 kmem_free(constraints, len);
959 return (NULL);
960 }
961
962 array = kmem_alloc((n + 1) * sizeof (char *), KM_SLEEP);
963
964 for (i = 0, p = constraints; i < n; i++, p += strlen(p) + 1) {
965 array[i] = i_ddi_strdup(p, KM_SLEEP);
966 }
967 array[n] = NULL;
968
969 kmem_free(constraints, len);
970
971 return (array);
972 }
973
974 static void
975 publish_retire_event(char *path, int retire)
976 {
977 sysevent_t *ev;
978 sysevent_id_t eid;
979 sysevent_value_t se_val;
980 sysevent_attr_list_t *ev_attr_list = NULL;
981
982 ev = sysevent_alloc(EC_DEV_STATUS,
983 retire ? ESC_DEV_RETIRE : ESC_DEV_UNRETIRE, EC_DEVFS, SE_SLEEP);
984 if (ev == NULL) {
985 goto fail;
986 }
987
988 se_val.value_type = SE_DATA_TYPE_STRING;
989 se_val.value.sv_string = path;
990
991 if (sysevent_add_attr(&ev_attr_list, DEV_PHYS_PATH,
992 &se_val, SE_SLEEP) != 0) {
993 goto fail;
994 }
995
996 if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
997 goto fail;
998 }
999
1000 if (log_sysevent(ev, SE_SLEEP, &eid) != 0) {
1001 goto fail;
1002 }
1003
1004 sysevent_free(ev);
1005 return;
1006
1007 fail:
1008
1009 cmn_err(CE_WARN, "failed to log device %s event for %s",
1010 retire ? "retire" : "unretire", path);
1011
1012 if (ev_attr_list != NULL) {
1013 sysevent_free_attr(ev_attr_list);
1014 }
1015
1016 sysevent_free(ev);
1017 }
1018
1019 /*ARGSUSED*/
1020 static int
1021 modctl_retire(char *path, char *uconstraints, size_t ulen)
1022 {
1023 char *pathbuf;
1024 char *devpath;
1025 size_t pathsz;
1026 int retval;
1027 char *constraints;
1028 char **cons_array;
1029
1030 if (path == NULL)
1031 return (EINVAL);
1032
1033 if ((uconstraints == NULL) ^ (ulen == 0))
1034 return (EINVAL);
1035
1036 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1037 retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
1038 if (retval != 0) {
1039 kmem_free(pathbuf, MAXPATHLEN);
1040 return (retval);
1041 }
1042 devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
1043 kmem_free(pathbuf, MAXPATHLEN);
1044
1045 /*
1046 * First check if the device is already retired.
1047 * If it is, then persist the retire anyway, just in case the retire
1048 * store has got out of sync with the boot archive.
1049 */
1050 if (e_ddi_device_retired(devpath)) {
1051 cmn_err(CE_NOTE, "Device: already retired: %s", devpath);
1052 (void) e_ddi_retire_persist(devpath);
1053 kmem_free(devpath, strlen(devpath) + 1);
1054 return (0);
1055 }
1056
1057 cons_array = NULL;
1058 if (uconstraints) {
1059 constraints = kmem_alloc(ulen, KM_SLEEP);
1060 if (copyin(uconstraints, constraints, ulen)) {
1061 kmem_free(constraints, ulen);
1062 kmem_free(devpath, strlen(devpath) + 1);
1063 return (EFAULT);
1064 }
1065 cons_array = convert_constraint_string(constraints, ulen);
1066 }
1067
1068 /*
1069 * Try to retire the device first. The following
1070 * routine will return an error only if the device
1071 * is not retireable i.e. retire constraints forbid
1072 * a retire. A return of success from this routine
1073 * indicates that device is retireable.
1074 */
1075 retval = e_ddi_retire_device(devpath, cons_array);
1076 if (retval != DDI_SUCCESS) {
1077 cmn_err(CE_WARN, "constraints forbid retire: %s", devpath);
1078 kmem_free(devpath, strlen(devpath) + 1);
1079 return (ENOTSUP);
1080 }
1081
1082 /*
1083 * Ok, the retire succeeded. Persist the retire.
1084 * If retiring a nexus, we need to only persist the
1085 * nexus retire. Any children of a retired nexus
1086 * are automatically covered by the retire store
1087 * code.
1088 */
1089 retval = e_ddi_retire_persist(devpath);
1090 if (retval != 0) {
1091 cmn_err(CE_WARN, "Failed to persist device retire: error %d: "
1092 "%s", retval, devpath);
1093 kmem_free(devpath, strlen(devpath) + 1);
1094 return (retval);
1095 }
1096 if (moddebug & MODDEBUG_RETIRE)
1097 cmn_err(CE_NOTE, "Persisted retire of device: %s", devpath);
1098
1099 /* Issue sysevent. */
1100 publish_retire_event(devpath, 1);
1101
1102 kmem_free(devpath, strlen(devpath) + 1);
1103 return (0);
1104 }
1105
1106 static int
1107 modctl_is_retired(char *path, int *statep)
1108 {
1109 char *pathbuf;
1110 char *devpath;
1111 size_t pathsz;
1112 int error;
1113 int status;
1114
1115 if (path == NULL || statep == NULL)
1116 return (EINVAL);
1117
1118 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1119 error = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
1120 if (error != 0) {
1121 kmem_free(pathbuf, MAXPATHLEN);
1122 return (error);
1123 }
1124 devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
1125 kmem_free(pathbuf, MAXPATHLEN);
1126
1127 if (e_ddi_device_retired(devpath))
1128 status = 1;
1129 else
1130 status = 0;
1131 kmem_free(devpath, strlen(devpath) + 1);
1132
1133 return (copyout(&status, statep, sizeof (status)) ? EFAULT : 0);
1134 }
1135
1136 static int
1137 modctl_unretire(char *path)
1138 {
1139 char *pathbuf;
1140 char *devpath;
1141 size_t pathsz;
1142 int retired;
1143 int retval;
1144
1145 if (path == NULL)
1146 return (EINVAL);
1147
1148 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1149 retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
1150 if (retval != 0) {
1151 kmem_free(pathbuf, MAXPATHLEN);
1152 return (retval);
1153 }
1154 devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
1155 kmem_free(pathbuf, MAXPATHLEN);
1156
1157 /*
1158 * We check if a device is retired (first) before
1159 * unpersisting the retire, because we use the
1160 * retire store to determine if a device is retired.
1161 * If we unpersist first, the device will always appear
1162 * to be unretired. For the rationale behind unpersisting
1163 * a device that is not retired, see the next comment.
1164 */
1165 retired = e_ddi_device_retired(devpath);
1166
1167 /*
1168 * We call unpersist unconditionally because the lookup
1169 * for retired devices (e_ddi_device_retired()), skips "bypassed"
1170 * devices. We still want to be able remove "bypassed" entries
1171 * from the persistent store, so we unpersist unconditionally
1172 * i.e. whether or not the entry is found on a lookup.
1173 *
1174 * e_ddi_retire_unpersist() returns 1 if it found and cleared
1175 * an entry from the retire store or 0 otherwise.
1176 */
1177 if (e_ddi_retire_unpersist(devpath))
1178 if (moddebug & MODDEBUG_RETIRE) {
1179 cmn_err(CE_NOTE, "Unpersisted retire of device: %s",
1180 devpath);
1181 }
1182
1183 /*
1184 * Check if the device is already unretired. If so,
1185 * the unretire becomes a NOP
1186 */
1187 if (!retired) {
1188 cmn_err(CE_NOTE, "Not retired: %s", devpath);
1189 kmem_free(devpath, strlen(devpath) + 1);
1190 return (0);
1191 }
1192
1193 retval = e_ddi_unretire_device(devpath);
1194 if (retval != 0) {
1195 cmn_err(CE_WARN, "cannot unretire device: error %d, path %s\n",
1196 retval, devpath);
1197 }
1198
1199 /* Issue sysevent. */
1200 publish_retire_event(devpath, 0);
1201
1202 kmem_free(devpath, strlen(devpath) + 1);
1203
1204 return (retval);
1205 }
1206
1207 static int
1208 modctl_getname(char *uname, uint_t ulen, int *umajorp)
1209 {
1210 char *name;
1211 major_t major;
1212
1213 if (copyin(umajorp, &major, sizeof (major)) != 0)
1214 return (EFAULT);
1215 if ((name = mod_major_to_name(major)) == NULL)
1216 return (ENODEV);
1217 if ((strlen(name) + 1) > ulen)
1218 return (ENOSPC);
1219 return (copyoutstr(name, uname, ulen, NULL));
1220 }
1221
1222 static int
1223 modctl_devt2instance(dev_t dev, int *uinstancep)
1224 {
1225 int instance;
1226
1227 if ((instance = dev_to_instance(dev)) == -1)
1228 return (EINVAL);
1229
1230 return (copyout(&instance, uinstancep, sizeof (int)));
1231 }
1232
1233 /*
1234 * Return the sizeof of the device id.
1235 */
1236 static int
1237 modctl_sizeof_devid(dev_t dev, uint_t *len)
1238 {
1239 uint_t sz;
1240 ddi_devid_t devid;
1241
1242 /* get device id */
1243 if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
1244 return (EINVAL);
1245
1246 sz = ddi_devid_sizeof(devid);
1247 ddi_devid_free(devid);
1248
1249 /* copyout device id size */
1250 if (copyout(&sz, len, sizeof (sz)) != 0)
1251 return (EFAULT);
1252
1253 return (0);
1254 }
1255
1256 /*
1257 * Return a copy of the device id.
1258 */
1259 static int
1260 modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
1261 {
1262 uint_t sz;
1263 ddi_devid_t devid;
1264 int err = 0;
1265
1266 /* get device id */
1267 if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
1268 return (EINVAL);
1269
1270 sz = ddi_devid_sizeof(devid);
1271
1272 /* Error if device id is larger than space allocated */
1273 if (sz > len) {
1274 ddi_devid_free(devid);
1275 return (ENOSPC);
1276 }
1277
1278 /* copy out device id */
1279 if (copyout(devid, udevid, sz) != 0)
1280 err = EFAULT;
1281 ddi_devid_free(devid);
1282 return (err);
1283 }
1284
1285 /*
1286 * return the /devices paths associated with the specified devid and
1287 * minor name.
1288 */
1289 /*ARGSUSED*/
1290 static int
1291 modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
1292 size_t *ulensp, char *upaths)
1293 {
1294 ddi_devid_t devid = NULL;
1295 int devid_len;
1296 char *minor_name = NULL;
1297 dev_info_t *dip = NULL;
1298 int circ;
1299 struct ddi_minor_data *dmdp;
1300 char *path = NULL;
1301 int ulens;
1302 int lens;
1303 int len;
1304 dev_t *devlist = NULL;
1305 int ndevs;
1306 int i;
1307 int ret = 0;
1308
1309 /*
1310 * If upaths is NULL then we are only computing the amount of space
1311 * needed to hold the paths and returning the value in *ulensp. If we
1312 * are copying out paths then we get the amount of space allocated by
1313 * the caller. If the actual space needed for paths is larger, or
1314 * things are changing out from under us, then we return EAGAIN.
1315 */
1316 if (upaths) {
1317 if (ulensp == NULL)
1318 return (EINVAL);
1319 if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
1320 return (EFAULT);
1321 }
1322
1323 /*
1324 * copyin enough of the devid to determine the length then
1325 * reallocate and copy in the entire devid.
1326 */
1327 devid_len = ddi_devid_sizeof(NULL);
1328 devid = kmem_alloc(devid_len, KM_SLEEP);
1329 if (copyin(udevid, devid, devid_len)) {
1330 ret = EFAULT;
1331 goto out;
1332 }
1333 len = devid_len;
1334 devid_len = ddi_devid_sizeof(devid);
1335 kmem_free(devid, len);
1336 devid = kmem_alloc(devid_len, KM_SLEEP);
1337 if (copyin(udevid, devid, devid_len)) {
1338 ret = EFAULT;
1339 goto out;
1340 }
1341
1342 /* copyin the minor name if specified. */
1343 minor_name = uminor_name;
1344 if ((minor_name != DEVID_MINOR_NAME_ALL) &&
1345 (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
1346 (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
1347 minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1348 if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
1349 ret = EFAULT;
1350 goto out;
1351 }
1352 }
1353
1354 /*
1355 * Use existing function to resolve the devid into a devlist.
1356 *
1357 * NOTE: there is a loss of spectype information in the current
1358 * ddi_lyr_devid_to_devlist implementation. We work around this by not
1359 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
1360 * node forms in the loop processing the devlist below. It would be
1361 * best if at some point the use of this interface here was replaced
1362 * with a path oriented call.
1363 */
1364 if (ddi_lyr_devid_to_devlist(devid,
1365 (minor_name == DEVID_MINOR_NAME_ALL) ?
1366 DEVID_MINOR_NAME_ALL_CHR : minor_name,
1367 &ndevs, &devlist) != DDI_SUCCESS) {
1368 ret = EINVAL;
1369 goto out;
1370 }
1371
1372 /*
1373 * loop over the devlist, converting each devt to a path and doing
1374 * a copyout of the path and computation of the amount of space
1375 * needed to hold all the paths
1376 */
1377 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1378 for (i = 0, lens = 0; i < ndevs; i++) {
1379
1380 /* find the dip associated with the dev_t */
1381 if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
1382 continue;
1383
1384 /* loop over all the minor nodes, skipping ones we don't want */
1385 ndi_devi_enter(dip, &circ);
1386 for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
1387 if ((dmdp->ddm_dev != devlist[i]) ||
1388 (dmdp->type != DDM_MINOR))
1389 continue;
1390
1391 if ((minor_name != DEVID_MINOR_NAME_ALL) &&
1392 (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
1393 (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
1394 strcmp(minor_name, dmdp->ddm_name))
1395 continue;
1396 else {
1397 if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
1398 (dmdp->ddm_spec_type != S_IFCHR))
1399 continue;
1400 if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
1401 (dmdp->ddm_spec_type != S_IFBLK))
1402 continue;
1403 }
1404
1405 (void) ddi_pathname_minor(dmdp, path);
1406 len = strlen(path) + 1;
1407 *(path + len) = '\0'; /* set double termination */
1408 lens += len;
1409
1410 /* copyout the path with double terminations */
1411 if (upaths) {
1412 if (lens > ulens) {
1413 ret = EAGAIN;
1414 goto out;
1415 }
1416 if (copyout(path, upaths, len + 1)) {
1417 ret = EFAULT;
1418 goto out;
1419 }
1420 upaths += len;
1421 }
1422 }
1423 ndi_devi_exit(dip, circ);
1424 ddi_release_devi(dip);
1425 dip = NULL;
1426 }
1427 lens++; /* add one for double termination */
1428
1429 /* copy out the amount of space needed to hold the paths */
1430 if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
1431 ret = EFAULT;
1432 goto out;
1433 }
1434 ret = 0;
1435
1436 out: if (dip) {
1437 ndi_devi_exit(dip, circ);
1438 ddi_release_devi(dip);
1439 }
1440 if (path)
1441 kmem_free(path, MAXPATHLEN);
1442 if (devlist)
1443 ddi_lyr_free_devlist(devlist, ndevs);
1444 if (minor_name &&
1445 (minor_name != DEVID_MINOR_NAME_ALL) &&
1446 (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
1447 (minor_name != DEVID_MINOR_NAME_ALL_BLK))
1448 kmem_free(minor_name, MAXPATHLEN);
1449 if (devid)
1450 kmem_free(devid, devid_len);
1451 return (ret);
1452 }
1453
1454 /*
1455 * Return the size of the minor name.
1456 */
1457 static int
1458 modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
1459 {
1460 uint_t sz;
1461 char *name;
1462
1463 /* get the minor name */
1464 if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1465 return (EINVAL);
1466
1467 sz = strlen(name) + 1;
1468 kmem_free(name, sz);
1469
1470 /* copy out the size of the minor name */
1471 if (copyout(&sz, len, sizeof (sz)) != 0)
1472 return (EFAULT);
1473
1474 return (0);
1475 }
1476
1477 /*
1478 * Return the minor name.
1479 */
1480 static int
1481 modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
1482 {
1483 uint_t sz;
1484 char *name;
1485 int err = 0;
1486
1487 /* get the minor name */
1488 if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
1489 return (EINVAL);
1490
1491 sz = strlen(name) + 1;
1492
1493 /* Error if the minor name is larger than the space allocated */
1494 if (sz > len) {
1495 kmem_free(name, sz);
1496 return (ENOSPC);
1497 }
1498
1499 /* copy out the minor name */
1500 if (copyout(name, uname, sz) != 0)
1501 err = EFAULT;
1502 kmem_free(name, sz);
1503 return (err);
1504 }
1505
1506 /*
1507 * Return the size of the (dev_t,spectype) devfspath name.
1508 */
1509 static int
1510 modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
1511 {
1512 uint_t sz;
1513 char *name;
1514
1515 /* get the path name */
1516 name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1517 if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
1518 kmem_free(name, MAXPATHLEN);
1519 return (EINVAL);
1520 }
1521
1522 sz = strlen(name) + 1;
1523 kmem_free(name, MAXPATHLEN);
1524
1525 /* copy out the size of the path name */
1526 if (copyout(&sz, len, sizeof (sz)) != 0)
1527 return (EFAULT);
1528
1529 return (0);
1530 }
1531
1532 /*
1533 * Return the (dev_t,spectype) devfspath name.
1534 */
1535 static int
1536 modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
1537 {
1538 uint_t sz;
1539 char *name;
1540 int err = 0;
1541
1542 /* get the path name */
1543 name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1544 if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
1545 kmem_free(name, MAXPATHLEN);
1546 return (EINVAL);
1547 }
1548
1549 sz = strlen(name) + 1;
1550
1551 /* Error if the path name is larger than the space allocated */
1552 if (sz > len) {
1553 kmem_free(name, MAXPATHLEN);
1554 return (ENOSPC);
1555 }
1556
1557 /* copy out the path name */
1558 if (copyout(name, uname, sz) != 0)
1559 err = EFAULT;
1560 kmem_free(name, MAXPATHLEN);
1561 return (err);
1562 }
1563
1564 /*
1565 * Return the size of the (major,instance) devfspath name.
1566 */
1567 static int
1568 modctl_devfspath_mi_len(major_t major, int instance, uint_t *len)
1569 {
1570 uint_t sz;
1571 char *name;
1572
1573 /* get the path name */
1574 name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1575 if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
1576 kmem_free(name, MAXPATHLEN);
1577 return (EINVAL);
1578 }
1579
1580 sz = strlen(name) + 1;
1581 kmem_free(name, MAXPATHLEN);
1582
1583 /* copy out the size of the path name */
1584 if (copyout(&sz, len, sizeof (sz)) != 0)
1585 return (EFAULT);
1586
1587 return (0);
1588 }
1589
1590 /*
1591 * Return the (major_instance) devfspath name.
1592 * NOTE: e_ddi_majorinstance_to_path does not require the device to attach to
1593 * return a path - it uses the instance tree.
1594 */
1595 static int
1596 modctl_devfspath_mi(major_t major, int instance, uint_t len, char *uname)
1597 {
1598 uint_t sz;
1599 char *name;
1600 int err = 0;
1601
1602 /* get the path name */
1603 name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1604 if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
1605 kmem_free(name, MAXPATHLEN);
1606 return (EINVAL);
1607 }
1608
1609 sz = strlen(name) + 1;
1610
1611 /* Error if the path name is larger than the space allocated */
1612 if (sz > len) {
1613 kmem_free(name, MAXPATHLEN);
1614 return (ENOSPC);
1615 }
1616
1617 /* copy out the path name */
1618 if (copyout(name, uname, sz) != 0)
1619 err = EFAULT;
1620 kmem_free(name, MAXPATHLEN);
1621 return (err);
1622 }
1623
1624 static int
1625 modctl_get_fbname(char *path)
1626 {
1627 extern dev_t fbdev;
1628 char *pathname = NULL;
1629 int rval = 0;
1630
1631 /* make sure fbdev is set before we plunge in */
1632 if (fbdev == NODEV)
1633 return (ENODEV);
1634
1635 pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1636 if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
1637 pathname)) == DDI_SUCCESS) {
1638 if (copyout(pathname, path, strlen(pathname)+1) != 0) {
1639 rval = EFAULT;
1640 }
1641 }
1642 kmem_free(pathname, MAXPATHLEN);
1643 return (rval);
1644 }
1645
1646 /*
1647 * modctl_reread_dacf()
1648 * Reread the dacf rules database from the named binding file.
1649 * If NULL is specified, pass along the NULL, it means 'use the default'.
1650 */
1651 static int
1652 modctl_reread_dacf(char *path)
1653 {
1654 int rval = 0;
1655 char *filename, *filenamep;
1656
1657 filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1658
1659 if (path == NULL) {
1660 filenamep = NULL;
1661 } else {
1662 if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
1663 rval = EFAULT;
1664 goto out;
1665 }
1666 filenamep = filename;
1667 filenamep[MAXPATHLEN - 1] = '\0';
1668 }
1669
1670 rval = read_dacf_binding_file(filenamep);
1671 out:
1672 kmem_free(filename, MAXPATHLEN);
1673 return (rval);
1674 }
1675
1676 /*ARGSUSED*/
1677 static int
1678 modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
1679 uint_t flag)
1680 {
1681 int error = 0;
1682 char *filenamep;
1683
1684 switch (subcmd) {
1685
1686 case MODEVENTS_FLUSH:
1687 /* flush all currently queued events */
1688 log_sysevent_flushq(subcmd, flag);
1689 break;
1690
1691 case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
1692 /*
1693 * bind door_upcall to filename
1694 * this should only be done once per invocation
1695 * of the event daemon.
1696 */
1697
1698 filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
1699
1700 if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
1701 error = EFAULT;
1702 } else {
1703 error = log_sysevent_filename(filenamep);
1704 }
1705 kmem_free(filenamep, MOD_MAXPATH);
1706 break;
1707
1708 case MODEVENTS_GETDATA:
1709 error = log_sysevent_copyout_data((sysevent_id_t *)a2,
1710 (size_t)a3, (caddr_t)a4);
1711 break;
1712
1713 case MODEVENTS_FREEDATA:
1714 error = log_sysevent_free_data((sysevent_id_t *)a2);
1715 break;
1716 case MODEVENTS_POST_EVENT:
1717 error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
1718 (sysevent_id_t *)a4);
1719 break;
1720 case MODEVENTS_REGISTER_EVENT:
1721 error = log_sysevent_register((char *)a2, (char *)a3,
1722 (se_pubsub_t *)a4);
1723 break;
1724 default:
1725 error = EINVAL;
1726 }
1727
1728 return (error);
1729 }
1730
1731 static void
1732 free_mperm(mperm_t *mp)
1733 {
1734 int len;
1735
1736 if (mp->mp_minorname) {
1737 len = strlen(mp->mp_minorname) + 1;
1738 kmem_free(mp->mp_minorname, len);
1739 }
1740 kmem_free(mp, sizeof (mperm_t));
1741 }
1742
1743 #define MP_NO_DRV_ERR \
1744 "/etc/minor_perm: no driver for %s\n"
1745
1746 #define MP_EMPTY_MINOR \
1747 "/etc/minor_perm: empty minor name for driver %s\n"
1748
1749 #define MP_NO_MINOR \
1750 "/etc/minor_perm: no minor matching %s for driver %s\n"
1751
1752 /*
1753 * Remove mperm entry with matching minorname
1754 */
1755 static void
1756 rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1757 {
1758 mperm_t **mp_head;
1759 mperm_t *freemp = NULL;
1760 struct devnames *dnp = &devnamesp[major];
1761 mperm_t **wildmp;
1762
1763 ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
1764
1765 LOCK_DEV_OPS(&dnp->dn_lock);
1766 if (strcmp(mp->mp_minorname, "*") == 0) {
1767 wildmp = ((is_clone == 0) ?
1768 &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
1769 if (*wildmp)
1770 freemp = *wildmp;
1771 *wildmp = NULL;
1772 } else {
1773 mp_head = &dnp->dn_mperm;
1774 while (*mp_head) {
1775 if (strcmp((*mp_head)->mp_minorname,
1776 mp->mp_minorname) != 0) {
1777 mp_head = &(*mp_head)->mp_next;
1778 continue;
1779 }
1780 /* remove the entry */
1781 freemp = *mp_head;
1782 *mp_head = freemp->mp_next;
1783 break;
1784 }
1785 }
1786 if (freemp) {
1787 if (moddebug & MODDEBUG_MINORPERM) {
1788 cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
1789 drvname, freemp->mp_minorname,
1790 freemp->mp_mode & 0777,
1791 freemp->mp_uid, freemp->mp_gid);
1792 }
1793 free_mperm(freemp);
1794 } else {
1795 if (moddebug & MODDEBUG_MINORPERM) {
1796 cmn_err(CE_CONT, MP_NO_MINOR,
1797 drvname, mp->mp_minorname);
1798 }
1799 }
1800
1801 UNLOCK_DEV_OPS(&dnp->dn_lock);
1802 }
1803
1804 /*
1805 * Add minor perm entry
1806 */
1807 static void
1808 add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
1809 {
1810 mperm_t **mp_head;
1811 mperm_t *freemp = NULL;
1812 struct devnames *dnp = &devnamesp[major];
1813 mperm_t **wildmp;
1814
1815 ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
1816
1817 /*
1818 * Note that update_drv replace semantics require
1819 * replacing matching entries with the new permissions.
1820 */
1821 LOCK_DEV_OPS(&dnp->dn_lock);
1822 if (strcmp(mp->mp_minorname, "*") == 0) {
1823 wildmp = ((is_clone == 0) ?
1824 &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
1825 if (*wildmp)
1826 freemp = *wildmp;
1827 *wildmp = mp;
1828 } else {
1829 mperm_t *p, *v = NULL;
1830 for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
1831 if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
1832 if (v == NULL)
1833 dnp->dn_mperm = mp;
1834 else
1835 v->mp_next = mp;
1836 mp->mp_next = p->mp_next;
1837 freemp = p;
1838 goto replaced;
1839 }
1840 }
1841 if (p == NULL) {
1842 mp_head = &dnp->dn_mperm;
1843 if (*mp_head == NULL) {
1844 *mp_head = mp;
1845 } else {
1846 mp->mp_next = *mp_head;
1847 *mp_head = mp;
1848 }
1849 }
1850 }
1851 replaced:
1852 if (freemp) {
1853 if (moddebug & MODDEBUG_MINORPERM) {
1854 cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
1855 drvname, freemp->mp_minorname,
1856 freemp->mp_mode & 0777,
1857 freemp->mp_uid, freemp->mp_gid);
1858 }
1859 free_mperm(freemp);
1860 }
1861 if (moddebug & MODDEBUG_MINORPERM) {
1862 cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
1863 drvname, mp->mp_minorname, mp->mp_mode & 0777,
1864 mp->mp_uid, mp->mp_gid);
1865 }
1866 UNLOCK_DEV_OPS(&dnp->dn_lock);
1867 }
1868
1869
1870 static int
1871 process_minorperm(int cmd, nvlist_t *nvl)
1872 {
1873 char *minor;
1874 major_t major;
1875 mperm_t *mp;
1876 nvpair_t *nvp;
1877 char *name;
1878 int is_clone;
1879 major_t minmaj;
1880
1881 ASSERT(cmd == MODLOADMINORPERM ||
1882 cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
1883
1884 nvp = NULL;
1885 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1886 name = nvpair_name(nvp);
1887
1888 is_clone = 0;
1889 (void) nvpair_value_string(nvp, &minor);
1890 major = ddi_name_to_major(name);
1891 if (major != DDI_MAJOR_T_NONE) {
1892 mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
1893 if (minor == NULL || strlen(minor) == 0) {
1894 if (moddebug & MODDEBUG_MINORPERM) {
1895 cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
1896 }
1897 minor = "*";
1898 }
1899
1900 /*
1901 * The minor name of a node using the clone
1902 * driver must be the driver name. To avoid
1903 * multiple searches, we map entries in the form
1904 * clone:<driver> to <driver>:*. This also allows us
1905 * to filter out some of the litter in /etc/minor_perm.
1906 * Minor perm alias entries where the name is not
1907 * the driver kept on the clone list itself.
1908 * This all seems very fragile as a driver could
1909 * be introduced with an existing alias name.
1910 */
1911 if (strcmp(name, "clone") == 0) {
1912 minmaj = ddi_name_to_major(minor);
1913 if (minmaj != DDI_MAJOR_T_NONE) {
1914 if (moddebug & MODDEBUG_MINORPERM) {
1915 cmn_err(CE_CONT,
1916 "mapping %s:%s to %s:*\n",
1917 name, minor, minor);
1918 }
1919 major = minmaj;
1920 name = minor;
1921 minor = "*";
1922 is_clone = 1;
1923 }
1924 }
1925
1926 if (mp) {
1927 mp->mp_minorname =
1928 i_ddi_strdup(minor, KM_SLEEP);
1929 }
1930 } else {
1931 mp = NULL;
1932 if (moddebug & MODDEBUG_MINORPERM) {
1933 cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
1934 }
1935 }
1936
1937 /* mode */
1938 nvp = nvlist_next_nvpair(nvl, nvp);
1939 ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
1940 if (mp)
1941 (void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
1942 /* uid */
1943 nvp = nvlist_next_nvpair(nvl, nvp);
1944 ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
1945 if (mp)
1946 (void) nvpair_value_uint32(nvp, &mp->mp_uid);
1947 /* gid */
1948 nvp = nvlist_next_nvpair(nvl, nvp);
1949 ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
1950 if (mp) {
1951 (void) nvpair_value_uint32(nvp, &mp->mp_gid);
1952
1953 if (cmd == MODREMMINORPERM) {
1954 rem_minorperm(major, name, mp, is_clone);
1955 free_mperm(mp);
1956 } else {
1957 add_minorperm(major, name, mp, is_clone);
1958 }
1959 }
1960 }
1961
1962 if (cmd == MODLOADMINORPERM)
1963 minorperm_loaded = 1;
1964
1965 /*
1966 * Reset permissions of cached dv_nodes
1967 */
1968 (void) devfs_reset_perm(DV_RESET_PERM);
1969
1970 return (0);
1971 }
1972
1973 static int
1974 modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
1975 {
1976 int error;
1977 nvlist_t *nvl;
1978 char *buf = kmem_alloc(buflen, KM_SLEEP);
1979
1980 if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
1981 kmem_free(buf, buflen);
1982 return (error);
1983 }
1984
1985 error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
1986 kmem_free(buf, buflen);
1987 if (error)
1988 return (error);
1989
1990 error = process_minorperm(cmd, nvl);
1991 nvlist_free(nvl);
1992 return (error);
1993 }
1994
1995 struct walk_args {
1996 char *wa_drvname;
1997 list_t wa_pathlist;
1998 };
1999
2000 struct path_elem {
2001 char *pe_dir;
2002 char *pe_nodename;
2003 list_node_t pe_node;
2004 int pe_dirlen;
2005 };
2006
2007 /*ARGSUSED*/
2008 static int
2009 modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
2010 {
2011 struct walk_args *wargs = (struct walk_args *)arg;
2012 struct path_elem *pe;
2013 char *nodename;
2014
2015 /*
2016 * Search may be restricted to a single driver in the case of rem_drv
2017 */
2018 if (wargs->wa_drvname &&
2019 strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
2020 return (INST_WALK_CONTINUE);
2021
2022 pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
2023 pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
2024 pe->pe_dirlen = strlen(pe->pe_dir) + 1;
2025 ASSERT(strrchr(pe->pe_dir, '/') != NULL);
2026 nodename = strrchr(pe->pe_dir, '/');
2027 *nodename++ = 0;
2028 pe->pe_nodename = nodename;
2029 list_insert_tail(&wargs->wa_pathlist, pe);
2030
2031 return (INST_WALK_CONTINUE);
2032 }
2033
2034 /*
2035 * /devices attribute nodes clean-up optionally performed
2036 * when removing a driver (rem_drv -C).
2037 *
2038 * Removing attribute nodes allows a machine to be reprovisioned
2039 * without the side-effect of inadvertently picking up stale
2040 * device node ownership or permissions.
2041 *
2042 * Preserving attributes (not performing cleanup) allows devices
2043 * attribute changes to be preserved across upgrades, as
2044 * upgrade rather heavy-handedly does a rem_drv/add_drv cycle.
2045 */
2046 static int
2047 modctl_remdrv_cleanup(const char *u_drvname)
2048 {
2049 struct walk_args *wargs;
2050 struct path_elem *pe;
2051 char *drvname;
2052 int err, rval = 0;
2053
2054 drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
2055 if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
2056 kmem_free(drvname, MAXMODCONFNAME);
2057 return (err);
2058 }
2059
2060 /*
2061 * First go through the instance database. For each
2062 * instance of a device bound to the driver being
2063 * removed, remove any underlying devfs attribute nodes.
2064 *
2065 * This is a two-step process. First we go through
2066 * the instance data itself, constructing a list of
2067 * the nodes discovered. The second step is then
2068 * to find and remove any devfs attribute nodes
2069 * for the instances discovered in the first step.
2070 * The two-step process avoids any difficulties
2071 * which could arise by holding the instance data
2072 * lock with simultaneous devfs operations.
2073 */
2074 wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
2075
2076 wargs->wa_drvname = drvname;
2077 list_create(&wargs->wa_pathlist,
2078 sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
2079
2080 (void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
2081
2082 for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
2083 pe = list_next(&wargs->wa_pathlist, pe)) {
2084 err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
2085 (const char *)pe->pe_nodename);
2086 if (rval == 0)
2087 rval = err;
2088 }
2089
2090 while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
2091 list_remove(&wargs->wa_pathlist, pe);
2092 kmem_free(pe->pe_dir, pe->pe_dirlen);
2093 kmem_free(pe, sizeof (*pe));
2094 }
2095 kmem_free(wargs, sizeof (*wargs));
2096
2097 /*
2098 * Pseudo nodes aren't recorded in the instance database
2099 * so any such nodes need to be handled separately.
2100 */
2101 err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
2102 if (rval == 0)
2103 rval = err;
2104
2105 kmem_free(drvname, MAXMODCONFNAME);
2106 return (rval);
2107 }
2108
2109 /*
2110 * Perform a cleanup of non-existent /devices attribute nodes,
2111 * similar to rem_drv -C, but for all drivers/devices.
2112 * This is also optional, performed as part of devfsadm -C.
2113 */
2114 void
2115 dev_devices_cleanup()
2116 {
2117 struct walk_args *wargs;
2118 struct path_elem *pe;
2119 dev_info_t *devi;
2120 char *path;
2121 int err;
2122
2123 /*
2124 * It's expected that all drivers have been loaded and
2125 * module unloading disabled while performing cleanup.
2126 */
2127 ASSERT(modunload_disable_count > 0);
2128
2129 wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
2130 wargs->wa_drvname = NULL;
2131 list_create(&wargs->wa_pathlist,
2132 sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
2133
2134 (void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
2135
2136 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2137
2138 for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
2139 pe = list_next(&wargs->wa_pathlist, pe)) {
2140 (void) snprintf(path, MAXPATHLEN, "%s/%s",
2141 pe->pe_dir, pe->pe_nodename);
2142 devi = e_ddi_hold_devi_by_path(path, 0);
2143 if (devi != NULL) {
2144 ddi_release_devi(devi);
2145 } else {
2146 err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
2147 (const char *)pe->pe_nodename);
2148 if (err) {
2149 cmn_err(CE_CONT,
2150 "devfs: %s: clean-up error %d\n",
2151 path, err);
2152 }
2153 }
2154 }
2155
2156 while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
2157 list_remove(&wargs->wa_pathlist, pe);
2158 kmem_free(pe->pe_dir, pe->pe_dirlen);
2159 kmem_free(pe, sizeof (*pe));
2160 }
2161 kmem_free(wargs, sizeof (*wargs));
2162 kmem_free(path, MAXPATHLEN);
2163 }
2164
2165 static int
2166 modctl_allocpriv(const char *name)
2167 {
2168 char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
2169 int error;
2170
2171 if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
2172 kmem_free(pstr, PRIVNAME_MAX);
2173 return (error);
2174 }
2175 error = priv_getbyname(pstr, PRIV_ALLOC);
2176 if (error < 0)
2177 error = -error;
2178 else
2179 error = 0;
2180 kmem_free(pstr, PRIVNAME_MAX);
2181 return (error);
2182 }
2183
2184 static int
2185 modctl_devexists(const char *upath, int pathlen)
2186 {
2187 char *path;
2188 int ret;
2189
2190 /*
2191 * copy in the path, including the terminating null
2192 */
2193 pathlen++;
2194 if (pathlen <= 1 || pathlen > MAXPATHLEN)
2195 return (EINVAL);
2196 path = kmem_zalloc(pathlen + 1, KM_SLEEP);
2197 if ((ret = copyinstr(upath, path, pathlen, NULL)) == 0) {
2198 ret = sdev_modctl_devexists(path);
2199 }
2200
2201 kmem_free(path, pathlen + 1);
2202 return (ret);
2203 }
2204
2205 static int
2206 modctl_devreaddir(const char *udir, int udirlen,
2207 char *upaths, int64_t *ulensp)
2208 {
2209 char *paths = NULL;
2210 char **dirlist = NULL;
2211 char *dir;
2212 int64_t ulens;
2213 int64_t lens;
2214 int i, n;
2215 int ret = 0;
2216 char *p;
2217 int npaths;
2218 int npaths_alloc;
2219
2220 /*
2221 * If upaths is NULL then we are only computing the amount of space
2222 * needed to return the paths, with the value returned in *ulensp. If we
2223 * are copying out paths then we get the amount of space allocated by
2224 * the caller. If the actual space needed for paths is larger, or
2225 * things are changing out from under us, then we return EAGAIN.
2226 */
2227 if (upaths) {
2228 if (ulensp == NULL)
2229 return (EINVAL);
2230 if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
2231 return (EFAULT);
2232 }
2233
2234 /*
2235 * copyin the /dev path including terminating null
2236 */
2237 udirlen++;
2238 if (udirlen <= 1 || udirlen > MAXPATHLEN)
2239 return (EINVAL);
2240 dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2241 if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2242 goto err;
2243
2244 if ((ret = sdev_modctl_readdir(dir, &dirlist,
2245 &npaths, &npaths_alloc, 0)) != 0) {
2246 ASSERT(dirlist == NULL);
2247 goto err;
2248 }
2249
2250 lens = 0;
2251 for (i = 0; i < npaths; i++) {
2252 lens += strlen(dirlist[i]) + 1;
2253 }
2254 lens++; /* add one for double termination */
2255
2256 if (upaths) {
2257 if (lens > ulens) {
2258 ret = EAGAIN;
2259 goto out;
2260 }
2261
2262 paths = kmem_alloc(lens, KM_SLEEP);
2263
2264 p = paths;
2265 for (i = 0; i < npaths; i++) {
2266 n = strlen(dirlist[i]) + 1;
2267 bcopy(dirlist[i], p, n);
2268 p += n;
2269 }
2270 *p = 0;
2271
2272 if (copyout(paths, upaths, lens)) {
2273 ret = EFAULT;
2274 goto err;
2275 }
2276 }
2277
2278 out:
2279 /* copy out the amount of space needed to hold the paths */
2280 if (copyout(&lens, ulensp, sizeof (lens)))
2281 ret = EFAULT;
2282
2283 err:
2284 if (dirlist)
2285 sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2286 if (paths)
2287 kmem_free(paths, lens);
2288 kmem_free(dir, udirlen + 1);
2289 return (ret);
2290 }
2291
2292 static int
2293 modctl_devemptydir(const char *udir, int udirlen, int *uempty)
2294 {
2295 char *dir;
2296 int ret;
2297 char **dirlist = NULL;
2298 int npaths;
2299 int npaths_alloc;
2300 int empty;
2301
2302 /*
2303 * copyin the /dev path including terminating null
2304 */
2305 udirlen++;
2306 if (udirlen <= 1 || udirlen > MAXPATHLEN)
2307 return (EINVAL);
2308 dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2309 if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2310 goto err;
2311
2312 if ((ret = sdev_modctl_readdir(dir, &dirlist,
2313 &npaths, &npaths_alloc, 1)) != 0) {
2314 goto err;
2315 }
2316
2317 empty = npaths ? 0 : 1;
2318 if (copyout(&empty, uempty, sizeof (empty)))
2319 ret = EFAULT;
2320
2321 err:
2322 if (dirlist)
2323 sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2324 kmem_free(dir, udirlen + 1);
2325 return (ret);
2326 }
2327
2328 static int
2329 modctl_hp(int subcmd, const char *path, char *cn_name, uintptr_t arg,
2330 uintptr_t rval)
2331 {
2332 int error = 0;
2333 size_t pathsz, namesz;
2334 char *devpath, *cn_name_str;
2335
2336 if (path == NULL)
2337 return (EINVAL);
2338
2339 devpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
2340 error = copyinstr(path, devpath, MAXPATHLEN, &pathsz);
2341 if (error != 0) {
2342 kmem_free(devpath, MAXPATHLEN);
2343 return (EFAULT);
2344 }
2345
2346 cn_name_str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
2347 error = copyinstr(cn_name, cn_name_str, MAXNAMELEN, &namesz);
2348 if (error != 0) {
2349 kmem_free(devpath, MAXPATHLEN);
2350 kmem_free(cn_name_str, MAXNAMELEN);
2351
2352 return (EFAULT);
2353 }
2354
2355 switch (subcmd) {
2356 case MODHPOPS_CHANGE_STATE:
2357 error = ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE, devpath,
2358 cn_name_str, arg, NULL);
2359 break;
2360 case MODHPOPS_CREATE_PORT:
2361 /* Create an empty PORT */
2362 error = ddihp_modctl(DDI_HPOP_CN_CREATE_PORT, devpath,
2363 cn_name_str, NULL, NULL);
2364 break;
2365 case MODHPOPS_REMOVE_PORT:
2366 /* Remove an empty PORT */
2367 error = ddihp_modctl(DDI_HPOP_CN_REMOVE_PORT, devpath,
2368 cn_name_str, NULL, NULL);
2369 break;
2370 case MODHPOPS_BUS_GET:
2371 error = ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY, devpath,
2372 cn_name_str, arg, rval);
2373 break;
2374 case MODHPOPS_BUS_SET:
2375 error = ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY, devpath,
2376 cn_name_str, arg, rval);
2377 break;
2378 default:
2379 error = ENOTSUP;
2380 break;
2381 }
2382
2383 kmem_free(devpath, MAXPATHLEN);
2384 kmem_free(cn_name_str, MAXNAMELEN);
2385
2386 return (error);
2387 }
2388
2389 int
2390 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
2391 {
2392 int error = 0;
2393
2394 switch (subcmd) {
2395 case MODDEVNAME_LOOKUPDOOR:
2396 error = devname_filename_register((char *)a1);
2397 break;
2398 case MODDEVNAME_PROFILE:
2399 error = devname_profile_update((char *)a1, (size_t)a2);
2400 break;
2401 case MODDEVNAME_RECONFIG:
2402 i_ddi_set_reconfig();
2403 break;
2404 case MODDEVNAME_SYSAVAIL:
2405 i_ddi_set_sysavail();
2406 break;
2407 default:
2408 error = EINVAL;
2409 break;
2410 }
2411
2412 return (error);
2413 }
2414
2415 /*ARGSUSED5*/
2416 int
2417 modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
2418 uintptr_t a5)
2419 {
2420 int error = EINVAL;
2421 dev_t dev;
2422
2423 if (secpolicy_modctl(CRED(), cmd) != 0)
2424 return (set_errno(EPERM));
2425
2426 switch (cmd) {
2427 case MODLOAD: /* load a module */
2428 error = modctl_modload((int)a1, (char *)a2, (int *)a3);
2429 break;
2430
2431 case MODUNLOAD: /* unload a module */
2432 error = modctl_modunload((modid_t)a1);
2433 break;
2434
2435 case MODINFO: /* get module status */
2436 error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
2437 break;
2438
2439 case MODRESERVED: /* get last major number in range */
2440 error = modctl_modreserve((modid_t)a1, (int *)a2);
2441 break;
2442
2443 case MODSETMINIROOT: /* we are running in miniroot */
2444 isminiroot = 1;
2445 error = 0;
2446 break;
2447
2448 case MODADDMAJBIND: /* add major / driver alias bindings */
2449 error = modctl_add_driver_aliases((int *)a2);
2450 break;
2451
2452 case MODGETPATHLEN: /* get modpath length */
2453 error = modctl_getmodpathlen((int *)a2);
2454 break;
2455
2456 case MODGETPATH: /* get modpath */
2457 error = modctl_getmodpath((char *)a2);
2458 break;
2459
2460 case MODREADSYSBIND: /* read system call binding file */
2461 error = modctl_read_sysbinding_file();
2462 break;
2463
2464 case MODGETMAJBIND: /* get major number for named device */
2465 error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
2466 break;
2467
2468 case MODGETNAME: /* get name of device given major number */
2469 error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
2470 break;
2471
2472 case MODDEVT2INSTANCE:
2473 if (get_udatamodel() == DATAMODEL_NATIVE) {
2474 dev = (dev_t)a1;
2475 }
2476 #ifdef _SYSCALL32_IMPL
2477 else {
2478 dev = expldev(a1);
2479 }
2480 #endif
2481 error = modctl_devt2instance(dev, (int *)a2);
2482 break;
2483
2484 case MODSIZEOF_DEVID: /* sizeof device id of device given dev_t */
2485 if (get_udatamodel() == DATAMODEL_NATIVE) {
2486 dev = (dev_t)a1;
2487 }
2488 #ifdef _SYSCALL32_IMPL
2489 else {
2490 dev = expldev(a1);
2491 }
2492 #endif
2493 error = modctl_sizeof_devid(dev, (uint_t *)a2);
2494 break;
2495
2496 case MODGETDEVID: /* get device id of device given dev_t */
2497 if (get_udatamodel() == DATAMODEL_NATIVE) {
2498 dev = (dev_t)a1;
2499 }
2500 #ifdef _SYSCALL32_IMPL
2501 else {
2502 dev = expldev(a1);
2503 }
2504 #endif
2505 error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
2506 break;
2507
2508 case MODSIZEOF_MINORNAME: /* sizeof minor nm (dev_t,spectype) */
2509 if (get_udatamodel() == DATAMODEL_NATIVE) {
2510 error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
2511 (uint_t *)a3);
2512 }
2513 #ifdef _SYSCALL32_IMPL
2514 else {
2515 error = modctl_sizeof_minorname(expldev(a1), (int)a2,
2516 (uint_t *)a3);
2517 }
2518
2519 #endif
2520 break;
2521
2522 case MODGETMINORNAME: /* get minor name of (dev_t,spectype) */
2523 if (get_udatamodel() == DATAMODEL_NATIVE) {
2524 error = modctl_get_minorname((dev_t)a1, (int)a2,
2525 (uint_t)a3, (char *)a4);
2526 }
2527 #ifdef _SYSCALL32_IMPL
2528 else {
2529 error = modctl_get_minorname(expldev(a1), (int)a2,
2530 (uint_t)a3, (char *)a4);
2531 }
2532 #endif
2533 break;
2534
2535 case MODGETDEVFSPATH_LEN: /* sizeof path nm of (dev_t,spectype) */
2536 if (get_udatamodel() == DATAMODEL_NATIVE) {
2537 error = modctl_devfspath_len((dev_t)a1, (int)a2,
2538 (uint_t *)a3);
2539 }
2540 #ifdef _SYSCALL32_IMPL
2541 else {
2542 error = modctl_devfspath_len(expldev(a1), (int)a2,
2543 (uint_t *)a3);
2544 }
2545
2546 #endif
2547 break;
2548
2549 case MODGETDEVFSPATH: /* get path name of (dev_t,spec) type */
2550 if (get_udatamodel() == DATAMODEL_NATIVE) {
2551 error = modctl_devfspath((dev_t)a1, (int)a2,
2552 (uint_t)a3, (char *)a4);
2553 }
2554 #ifdef _SYSCALL32_IMPL
2555 else {
2556 error = modctl_devfspath(expldev(a1), (int)a2,
2557 (uint_t)a3, (char *)a4);
2558 }
2559 #endif
2560 break;
2561
2562 case MODGETDEVFSPATH_MI_LEN: /* sizeof path nm of (major,instance) */
2563 error = modctl_devfspath_mi_len((major_t)a1, (int)a2,
2564 (uint_t *)a3);
2565 break;
2566
2567 case MODGETDEVFSPATH_MI: /* get path name of (major,instance) */
2568 error = modctl_devfspath_mi((major_t)a1, (int)a2,
2569 (uint_t)a3, (char *)a4);
2570 break;
2571
2572
2573 case MODEVENTS:
2574 error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
2575 break;
2576
2577 case MODGETFBNAME: /* get the framebuffer name */
2578 error = modctl_get_fbname((char *)a1);
2579 break;
2580
2581 case MODREREADDACF: /* reread dacf rule database from given file */
2582 error = modctl_reread_dacf((char *)a1);
2583 break;
2584
2585 case MODLOADDRVCONF: /* load driver.conf file for major */
2586 error = modctl_load_drvconf((major_t)a1, (int)a2);
2587 break;
2588
2589 case MODUNLOADDRVCONF: /* unload driver.conf file for major */
2590 error = modctl_unload_drvconf((major_t)a1);
2591 break;
2592
2593 case MODREMMAJBIND: /* remove a major binding */
2594 error = modctl_rem_major((major_t)a1);
2595 break;
2596
2597 case MODREMDRVALIAS: /* remove a major/alias binding */
2598 error = modctl_remove_driver_aliases((int *)a2);
2599 break;
2600
2601 case MODDEVID2PATHS: /* get paths given devid */
2602 error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
2603 (uint_t)a3, (size_t *)a4, (char *)a5);
2604 break;
2605
2606 case MODSETDEVPOLICY: /* establish device policy */
2607 error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
2608 break;
2609
2610 case MODGETDEVPOLICY: /* get device policy */
2611 error = devpolicy_get((int *)a1, (size_t)a2,
2612 (devplcysys_t *)a3);
2613 break;
2614
2615 case MODALLOCPRIV:
2616 error = modctl_allocpriv((const char *)a1);
2617 break;
2618
2619 case MODGETDEVPOLICYBYNAME:
2620 error = devpolicy_getbyname((size_t)a1,
2621 (devplcysys_t *)a2, (char *)a3);
2622 break;
2623
2624 case MODLOADMINORPERM:
2625 case MODADDMINORPERM:
2626 case MODREMMINORPERM:
2627 error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
2628 break;
2629
2630 case MODREMDRVCLEANUP:
2631 error = modctl_remdrv_cleanup((const char *)a1);
2632 break;
2633
2634 case MODDEVEXISTS: /* non-reconfiguring /dev lookup */
2635 error = modctl_devexists((const char *)a1, (size_t)a2);
2636 break;
2637
2638 case MODDEVREADDIR: /* non-reconfiguring /dev readdir */
2639 error = modctl_devreaddir((const char *)a1, (size_t)a2,
2640 (char *)a3, (int64_t *)a4);
2641 break;
2642
2643 case MODDEVEMPTYDIR: /* non-reconfiguring /dev emptydir */
2644 error = modctl_devemptydir((const char *)a1, (size_t)a2,
2645 (int *)a3);
2646 break;
2647
2648 case MODDEVNAME:
2649 error = modctl_moddevname((int)a1, a2, a3);
2650 break;
2651
2652 case MODRETIRE: /* retire device named by physpath a1 */
2653 error = modctl_retire((char *)a1, (char *)a2, (size_t)a3);
2654 break;
2655
2656 case MODISRETIRED: /* check if a device is retired. */
2657 error = modctl_is_retired((char *)a1, (int *)a2);
2658 break;
2659
2660 case MODUNRETIRE: /* unretire device named by physpath a1 */
2661 error = modctl_unretire((char *)a1);
2662 break;
2663
2664 case MODHPOPS: /* hotplug operations */
2665 /* device named by physpath a2 and Connection name a3 */
2666 error = modctl_hp((int)a1, (char *)a2, (char *)a3, a4, a5);
2667 break;
2668
2669 default:
2670 error = EINVAL;
2671 break;
2672 }
2673
2674 return (error ? set_errno(error) : 0);
2675 }
2676
2677 /*
2678 * Calls to kobj_load_module()() are handled off to this routine in a
2679 * separate thread.
2680 */
2681 static void
2682 modload_thread(struct loadmt *ltp)
2683 {
2684 /* load the module and signal the creator of this thread */
2685 kmutex_t cpr_lk;
2686 callb_cpr_t cpr_i;
2687
2688 mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
2689 CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
2690 /* borrow the devi lock from thread which invoked us */
2691 pm_borrow_lock(ltp->owner);
2692 ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
2693 pm_return_lock();
2694 sema_v(<p->sema);
2695 mutex_enter(&cpr_lk);
2696 CALLB_CPR_EXIT(&cpr_i);
2697 mutex_destroy(&cpr_lk);
2698 thread_exit();
2699 }
2700
2701 /*
2702 * load a module, adding a reference if caller specifies rmodp. If rmodp
2703 * is specified then an errno is returned, otherwise a module index is
2704 * returned (-1 on error).
2705 */
2706 static int
2707 modrload(const char *subdir, const char *filename, struct modctl **rmodp)
2708 {
2709 struct modctl *modp;
2710 size_t size;
2711 char *fullname;
2712 int retval = EINVAL;
2713 int id = -1;
2714
2715 if (rmodp)
2716 *rmodp = NULL; /* avoid garbage */
2717
2718 if (subdir != NULL) {
2719 /*
2720 * refuse / in filename to prevent "../" escapes.
2721 */
2722 if (strchr(filename, '/') != NULL)
2723 return (rmodp ? retval : id);
2724
2725 /*
2726 * allocate enough space for <subdir>/<filename><NULL>
2727 */
2728 size = strlen(subdir) + strlen(filename) + 2;
2729 fullname = kmem_zalloc(size, KM_SLEEP);
2730 (void) sprintf(fullname, "%s/%s", subdir, filename);
2731 } else {
2732 fullname = (char *)filename;
2733 }
2734
2735 modp = mod_hold_installed_mod(fullname, 1, 0, &retval);
2736 if (modp != NULL) {
2737 id = modp->mod_id;
2738 if (rmodp) {
2739 /* add mod_ref and return *rmodp */
2740 mutex_enter(&mod_lock);
2741 modp->mod_ref++;
2742 mutex_exit(&mod_lock);
2743 *rmodp = modp;
2744 }
2745 mod_release_mod(modp);
2746 CPU_STATS_ADDQ(CPU, sys, modload, 1);
2747 }
2748
2749 done: if (subdir != NULL)
2750 kmem_free(fullname, size);
2751 return (rmodp ? retval : id);
2752 }
2753
2754 /*
2755 * This is the primary kernel interface to load a module. It loads and
2756 * installs the named module. It does not hold mod_ref of the module, so
2757 * a module unload attempt can occur at any time - it is up to the
2758 * _fini/mod_remove implementation to determine if unload will succeed.
2759 */
2760 int
2761 modload(const char *subdir, const char *filename)
2762 {
2763 return (modrload(subdir, filename, NULL));
2764 }
2765
2766 /*
2767 * Load a module using a series of qualified names from most specific to least
2768 * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
2769 * Value returned in *chosen
2770 * foo/bar.baz.1.2.3 3
2771 * foo/bar.baz.1.2 2
2772 * foo/bar.baz.1 1
2773 * foo/bar.baz 0
2774 *
2775 * Return the module ID on success; -1 if no module was loaded. On success
2776 * and if 'chosen' is not NULL we also return the number of suffices that
2777 * were in the module we chose to load.
2778 */
2779 int
2780 modload_qualified(const char *subdir, const char *p1,
2781 const char *p2, const char *delim, uint_t suffv[], int suffc, int *chosen)
2782 {
2783 char path[MOD_MAXPATH];
2784 size_t n, resid = sizeof (path);
2785 char *p = path;
2786
2787 char **dotv;
2788 int i, rc, id;
2789 modctl_t *mp;
2790
2791 if (p2 != NULL)
2792 n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
2793 else
2794 n = snprintf(p, resid, "%s/%s", subdir, p1);
2795
2796 if (n >= resid)
2797 return (-1);
2798
2799 p += n;
2800 resid -= n;
2801 dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
2802
2803 for (i = 0; i < suffc; i++) {
2804 dotv[i] = p;
2805 n = snprintf(p, resid, "%s%u", delim, suffv[i]);
2806
2807 if (n >= resid) {
2808 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2809 return (-1);
2810 }
2811
2812 p += n;
2813 resid -= n;
2814 }
2815
2816 dotv[suffc] = p;
2817
2818 for (i = suffc; i >= 0; i--) {
2819 dotv[i][0] = '\0';
2820 mp = mod_hold_installed_mod(path, 1, 1, &rc);
2821
2822 if (mp != NULL) {
2823 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2824 id = mp->mod_id;
2825 mod_release_mod(mp);
2826 if (chosen != NULL)
2827 *chosen = i;
2828 return (id);
2829 }
2830 }
2831
2832 kmem_free(dotv, sizeof (char *) * (suffc + 1));
2833 return (-1);
2834 }
2835
2836 /*
2837 * Load a module.
2838 */
2839 int
2840 modloadonly(const char *subdir, const char *filename)
2841 {
2842 struct modctl *modp;
2843 char *fullname;
2844 size_t size;
2845 int id, retval;
2846
2847 if (subdir != NULL) {
2848 /*
2849 * allocate enough space for <subdir>/<filename><NULL>
2850 */
2851 size = strlen(subdir) + strlen(filename) + 2;
2852 fullname = kmem_zalloc(size, KM_SLEEP);
2853 (void) sprintf(fullname, "%s/%s", subdir, filename);
2854 } else {
2855 fullname = (char *)filename;
2856 }
2857
2858 modp = mod_hold_loaded_mod(NULL, fullname, &retval);
2859 if (modp) {
2860 id = modp->mod_id;
2861 mod_release_mod(modp);
2862 }
2863
2864 if (subdir != NULL)
2865 kmem_free(fullname, size);
2866
2867 if (retval == 0)
2868 return (id);
2869 return (-1);
2870 }
2871
2872 /*
2873 * Try to uninstall and unload a module, removing a reference if caller
2874 * specifies rmodp.
2875 */
2876 static int
2877 modunrload(modid_t id, struct modctl **rmodp, int unload)
2878 {
2879 struct modctl *modp;
2880 int retval;
2881
2882 if (rmodp)
2883 *rmodp = NULL; /* avoid garbage */
2884
2885 if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
2886 return (EINVAL);
2887
2888 if (rmodp) {
2889 mutex_enter(&mod_lock);
2890 modp->mod_ref--;
2891 if (modp->mod_ref == 0)
2892 mod_uninstall_ref_zero++;
2893 mutex_exit(&mod_lock);
2894 *rmodp = modp;
2895 }
2896
2897 if (unload) {
2898 retval = moduninstall(modp);
2899 if (retval == 0) {
2900 mod_unload(modp);
2901 CPU_STATS_ADDQ(CPU, sys, modunload, 1);
2902 } else if (retval == EALREADY)
2903 retval = 0; /* already unloaded, not an error */
2904 } else
2905 retval = 0;
2906
2907 mod_release_mod(modp);
2908 return (retval);
2909 }
2910
2911 /*
2912 * Uninstall and unload a module.
2913 */
2914 int
2915 modunload(modid_t id)
2916 {
2917 int retval;
2918
2919 /* synchronize with any active modunload_disable() */
2920 modunload_begin();
2921 if (ddi_root_node())
2922 (void) devfs_clean(ddi_root_node(), NULL, 0);
2923 retval = modunrload(id, NULL, 1);
2924 modunload_end();
2925 return (retval);
2926 }
2927
2928 /*
2929 * Return status of a loaded module.
2930 */
2931 static int
2932 modinfo(modid_t id, struct modinfo *modinfop)
2933 {
2934 struct modctl *modp;
2935 modid_t mid;
2936 int i;
2937
2938 mid = modinfop->mi_id;
2939 if (modinfop->mi_info & MI_INFO_ALL) {
2940 while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
2941 if ((modinfop->mi_info & MI_INFO_CNT) ||
2942 modp->mod_installed)
2943 break;
2944 mod_release_mod(modp);
2945 }
2946 if (modp == NULL)
2947 return (EINVAL);
2948 } else {
2949 modp = mod_hold_by_id(id);
2950 if (modp == NULL)
2951 return (EINVAL);
2952 if (!(modinfop->mi_info & MI_INFO_CNT) &&
2953 (modp->mod_installed == 0)) {
2954 mod_release_mod(modp);
2955 return (EINVAL);
2956 }
2957 }
2958
2959 modinfop->mi_rev = 0;
2960 modinfop->mi_state = 0;
2961 for (i = 0; i < MODMAXLINK; i++) {
2962 modinfop->mi_msinfo[i].msi_p0 = -1;
2963 modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
2964 }
2965 if (modp->mod_loaded) {
2966 modinfop->mi_state = MI_LOADED;
2967 kobj_getmodinfo(modp->mod_mp, modinfop);
2968 }
2969 if (modp->mod_installed) {
2970 modinfop->mi_state |= MI_INSTALLED;
2971
2972 (void) mod_getinfo(modp, modinfop);
2973 }
2974
2975 modinfop->mi_id = modp->mod_id;
2976 modinfop->mi_loadcnt = modp->mod_loadcnt;
2977 (void) strcpy(modinfop->mi_name, modp->mod_modname);
2978
2979 mod_release_mod(modp);
2980 return (0);
2981 }
2982
2983 static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
2984 static char no_err[] = "No error function for weak stub %s";
2985
2986 /*
2987 * used by the stubs themselves to load and hold a module.
2988 * Returns 0 if the module is successfully held;
2989 * the stub needs to call mod_release_stub().
2990 * -1 if the stub should just call the err_fcn.
2991 * Note that this code is stretched out so that we avoid subroutine calls
2992 * and optimize for the most likely case. That is, the case where the
2993 * module is loaded and installed and not held. In that case we just inc
2994 * the mod_ref count and continue.
2995 */
2996 int
2997 mod_hold_stub(struct mod_stub_info *stub)
2998 {
2999 struct modctl *mp;
3000 struct mod_modinfo *mip;
3001
3002 mip = stub->mods_modinfo;
3003
3004 mutex_enter(&mod_lock);
3005
3006 /* we do mod_hold_by_modctl inline for speed */
3007
3008 mod_check_again:
3009 if ((mp = mip->mp) != NULL) {
3010 if (mp->mod_busy == 0) {
3011 if (mp->mod_installed) {
3012 /* increment the reference count */
3013 mp->mod_ref++;
3014 ASSERT(mp->mod_ref && mp->mod_installed);
3015 mutex_exit(&mod_lock);
3016 return (0);
3017 } else {
3018 mp->mod_busy = 1;
3019 mp->mod_inprogress_thread =
3020 (curthread == NULL ?
3021 (kthread_id_t)-1 : curthread);
3022 }
3023 } else {
3024 /*
3025 * wait one time and then go see if someone
3026 * else has resolved the stub (set mip->mp).
3027 */
3028 if (mod_hold_by_modctl(mp,
3029 MOD_WAIT_ONCE | MOD_LOCK_HELD))
3030 goto mod_check_again;
3031
3032 /*
3033 * what we have now may have been unloaded!, in
3034 * that case, mip->mp will be NULL, we'll hit this
3035 * module and load again..
3036 */
3037 cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
3038 }
3039 mutex_exit(&mod_lock);
3040 } else {
3041 /* first time we've hit this module */
3042 mutex_exit(&mod_lock);
3043 mp = mod_hold_by_name(mip->modm_module_name);
3044 mip->mp = mp;
3045 }
3046
3047 /*
3048 * If we are here, it means that the following conditions
3049 * are satisfied.
3050 *
3051 * mip->mp != NULL
3052 * this thread has set the mp->mod_busy = 1
3053 * mp->mod_installed = 0
3054 *
3055 */
3056 ASSERT(mp != NULL);
3057 ASSERT(mp->mod_busy == 1);
3058
3059 if (mp->mod_installed == 0) {
3060 /* Module not loaded, if weak stub don't load it */
3061 if (stub->mods_flag & MODS_WEAK) {
3062 if (stub->mods_errfcn == NULL) {
3063 mod_release_mod(mp);
3064 cmn_err(CE_PANIC, no_err,
3065 mip->modm_module_name);
3066 }
3067 } else {
3068 /* Not a weak stub so load the module */
3069
3070 if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
3071 /*
3072 * If mod_load() was successful
3073 * and modinstall() failed, then
3074 * unload the module.
3075 */
3076 if (mp->mod_loaded)
3077 mod_unload(mp);
3078
3079 mod_release_mod(mp);
3080 if (stub->mods_errfcn == NULL) {
3081 cmn_err(CE_PANIC, mod_stub_err,
3082 mip->modm_module_name);
3083 } else {
3084 return (-1);
3085 }
3086 }
3087 }
3088 }
3089
3090 /*
3091 * At this point module is held and loaded. Release
3092 * the mod_busy and mod_inprogress_thread before
3093 * returning. We actually call mod_release() here so
3094 * that if another stub wants to access this module,
3095 * it can do so. mod_ref is incremented before mod_release()
3096 * is called to prevent someone else from snatching the
3097 * module from this thread.
3098 */
3099 mutex_enter(&mod_lock);
3100 mp->mod_ref++;
3101 ASSERT(mp->mod_ref &&
3102 (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
3103 mod_release(mp);
3104 mutex_exit(&mod_lock);
3105 return (0);
3106 }
3107
3108 void
3109 mod_release_stub(struct mod_stub_info *stub)
3110 {
3111 struct modctl *mp = stub->mods_modinfo->mp;
3112
3113 /* inline mod_release_mod */
3114 mutex_enter(&mod_lock);
3115 ASSERT(mp->mod_ref &&
3116 (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
3117 mp->mod_ref--;
3118 if (mp->mod_ref == 0)
3119 mod_uninstall_ref_zero++;
3120 if (mp->mod_want) {
3121 mp->mod_want = 0;
3122 cv_broadcast(&mod_cv);
3123 }
3124 mutex_exit(&mod_lock);
3125 }
3126
3127 static struct modctl *
3128 mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
3129 {
3130 struct modctl *modp;
3131 int retval;
3132
3133 /*
3134 * Hold the module.
3135 */
3136 modp = mod_hold_by_name_requisite(dep, filename);
3137 if (modp) {
3138 retval = mod_load(modp, 1);
3139 if (retval != 0) {
3140 mod_release_mod(modp);
3141 modp = NULL;
3142 }
3143 *status = retval;
3144 } else {
3145 *status = ENOSPC;
3146 }
3147
3148 /*
3149 * if dep is not NULL, clear the module dependency information.
3150 * This information is set in mod_hold_by_name_common().
3151 */
3152 if (dep != NULL && dep->mod_requisite_loading != NULL) {
3153 ASSERT(dep->mod_busy);
3154 dep->mod_requisite_loading = NULL;
3155 }
3156
3157 return (modp);
3158 }
3159
3160 /*
3161 * hold, load, and install the named module
3162 */
3163 static struct modctl *
3164 mod_hold_installed_mod(char *name, int usepath, int forcecheck, int *r)
3165 {
3166 struct modctl *modp;
3167 int retval;
3168
3169 /*
3170 * Verify that that module in question actually exists on disk
3171 * before allocation of module structure by mod_hold_by_name.
3172 */
3173 if (modrootloaded && swaploaded || forcecheck) {
3174 if (!kobj_path_exists(name, usepath)) {
3175 *r = ENOENT;
3176 return (NULL);
3177 }
3178 }
3179
3180 /*
3181 * Hold the module.
3182 */
3183 modp = mod_hold_by_name(name);
3184 if (modp) {
3185 retval = mod_load(modp, usepath);
3186 if (retval != 0) {
3187 mod_release_mod(modp);
3188 modp = NULL;
3189 *r = retval;
3190 } else {
3191 if ((*r = modinstall(modp)) != 0) {
3192 /*
3193 * We loaded it, but failed to _init() it.
3194 * Be kind to developers -- force it
3195 * out of memory now so that the next
3196 * attempt to use the module will cause
3197 * a reload. See 1093793.
3198 */
3199 mod_unload(modp);
3200 mod_release_mod(modp);
3201 modp = NULL;
3202 }
3203 }
3204 } else {
3205 *r = ENOSPC;
3206 }
3207 return (modp);
3208 }
3209
3210 static char mod_excl_msg[] =
3211 "module %s(%s) is EXCLUDED and will not be loaded\n";
3212 static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
3213
3214 /*
3215 * This routine is needed for dependencies. Users specify dependencies
3216 * by declaring a character array initialized to filenames of dependents.
3217 * So the code that handles dependents deals with filenames (and not
3218 * module names) because that's all it has. We load by filename and once
3219 * we've loaded a file we can get the module name.
3220 * Unfortunately there isn't a single unified filename/modulename namespace.
3221 * C'est la vie.
3222 *
3223 * We allow the name being looked up to be prepended by an optional
3224 * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
3225 */
3226 struct modctl *
3227 mod_find_by_filename(char *subdir, char *filename)
3228 {
3229 struct modctl *mp;
3230 size_t sublen;
3231
3232 ASSERT(!MUTEX_HELD(&mod_lock));
3233 if (subdir != NULL)
3234 sublen = strlen(subdir);
3235 else
3236 sublen = 0;
3237
3238 mutex_enter(&mod_lock);
3239 mp = &modules;
3240 do {
3241 if (sublen) {
3242 char *mod_filename = mp->mod_filename;
3243
3244 if (strncmp(subdir, mod_filename, sublen) == 0 &&
3245 mod_filename[sublen] == '/' &&
3246 strcmp(filename, &mod_filename[sublen + 1]) == 0) {
3247 mutex_exit(&mod_lock);
3248 return (mp);
3249 }
3250 } else if (strcmp(filename, mp->mod_filename) == 0) {
3251 mutex_exit(&mod_lock);
3252 return (mp);
3253 }
3254 } while ((mp = mp->mod_next) != &modules);
3255 mutex_exit(&mod_lock);
3256 return (NULL);
3257 }
3258
3259 /*
3260 * Check for circular dependencies. This is called from do_dependents()
3261 * in kobj.c. If we are the thread already loading this module, then
3262 * we're trying to load a dependent that we're already loading which
3263 * means the user specified circular dependencies.
3264 */
3265 static int
3266 mod_circdep(struct modctl *modp)
3267 {
3268 struct modctl *rmod;
3269
3270 ASSERT(MUTEX_HELD(&mod_lock));
3271
3272 /*
3273 * Check the mod_inprogress_thread first.
3274 * mod_inprogress_thread is used in mod_hold_stub()
3275 * directly to improve performance.
3276 */
3277 if (modp->mod_inprogress_thread == curthread)
3278 return (1);
3279
3280 /*
3281 * Check the module circular dependencies.
3282 */
3283 for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
3284 /*
3285 * Check if there is a module circular dependency.
3286 */
3287 if (rmod->mod_requisite_loading == modp)
3288 return (1);
3289 }
3290 return (0);
3291 }
3292
3293 static int
3294 mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
3295 {
3296 int (*func)(struct modinfo *);
3297 int retval;
3298
3299 ASSERT(modp->mod_busy);
3300
3301 /* primary modules don't do getinfo */
3302 if (modp->mod_prim)
3303 return (0);
3304
3305 func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
3306
3307 if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
3308 cmn_err(CE_WARN, "_info() not defined properly in %s",
3309 modp->mod_filename);
3310 /*
3311 * The semantics of mod_info(9F) are that 0 is failure
3312 * and non-zero is success.
3313 */
3314 retval = 0;
3315 } else
3316 retval = (*func)(modinfop); /* call _info() function */
3317
3318 if (moddebug & MODDEBUG_USERDEBUG)
3319 printf("Returned from _info, retval = %x\n", retval);
3320
3321 return (retval);
3322 }
3323
3324 static void
3325 modadd(struct modctl *mp)
3326 {
3327 ASSERT(MUTEX_HELD(&mod_lock));
3328
3329 mp->mod_id = last_module_id++;
3330 mp->mod_next = &modules;
3331 mp->mod_prev = modules.mod_prev;
3332 modules.mod_prev->mod_next = mp;
3333 modules.mod_prev = mp;
3334 }
3335
3336 /*ARGSUSED*/
3337 static struct modctl *
3338 allocate_modp(const char *filename, const char *modname)
3339 {
3340 struct modctl *mp;
3341
3342 mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
3343 mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
3344 (void) strcpy(mp->mod_modname, modname);
3345 return (mp);
3346 }
3347
3348 /*
3349 * Get the value of a symbol. This is a wrapper routine that
3350 * calls kobj_getsymvalue(). kobj_getsymvalue() may go away but this
3351 * wrapper will prevent callers from noticing.
3352 */
3353 uintptr_t
3354 modgetsymvalue(char *name, int kernelonly)
3355 {
3356 return (kobj_getsymvalue(name, kernelonly));
3357 }
3358
3359 /*
3360 * Get the symbol nearest an address. This is a wrapper routine that
3361 * calls kobj_getsymname(). kobj_getsymname() may go away but this
3362 * wrapper will prevent callers from noticing.
3363 */
3364 char *
3365 modgetsymname(uintptr_t value, ulong_t *offset)
3366 {
3367 return (kobj_getsymname(value, offset));
3368 }
3369
3370 /*
3371 * Lookup a symbol in a specified module. These are wrapper routines that
3372 * call kobj_lookup(). kobj_lookup() may go away but these wrappers will
3373 * prevent callers from noticing.
3374 */
3375 uintptr_t
3376 modlookup(const char *modname, const char *symname)
3377 {
3378 struct modctl *modp;
3379 uintptr_t val;
3380
3381 if ((modp = mod_hold_by_name(modname)) == NULL)
3382 return (0);
3383 val = kobj_lookup(modp->mod_mp, symname);
3384 mod_release_mod(modp);
3385 return (val);
3386 }
3387
3388 uintptr_t
3389 modlookup_by_modctl(modctl_t *modp, const char *symname)
3390 {
3391 ASSERT(modp->mod_ref > 0 || modp->mod_busy);
3392
3393 return (kobj_lookup(modp->mod_mp, symname));
3394 }
3395
3396 /*
3397 * Ask the user for the name of the system file and the default path
3398 * for modules.
3399 */
3400 void
3401 mod_askparams()
3402 {
3403 static char s0[64];
3404 intptr_t fd;
3405
3406 if ((fd = kobj_open(systemfile)) != -1L)
3407 kobj_close(fd);
3408 else
3409 systemfile = NULL;
3410
3411 /*CONSTANTCONDITION*/
3412 while (1) {
3413 printf("Name of system file [%s]: ",
3414 systemfile ? systemfile : "/dev/null");
3415
3416 console_gets(s0, sizeof (s0));
3417
3418 if (s0[0] == '\0')
3419 break;
3420 else if (strcmp(s0, "/dev/null") == 0) {
3421 systemfile = NULL;
3422 break;
3423 } else {
3424 if ((fd = kobj_open(s0)) != -1L) {
3425 kobj_close(fd);
3426 systemfile = s0;
3427 break;
3428 }
3429 }
3430 printf("can't find file %s\n", s0);
3431 }
3432 }
3433
3434 static char loading_msg[] = "loading '%s' id %d\n";
3435 static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
3436
3437 /*
3438 * Common code for loading a module (but not installing it).
3439 * Handoff the task of module loading to a separate thread
3440 * with a large stack if possible, since this code may recurse a few times.
3441 * Return zero if there are no errors or an errno value.
3442 */
3443 static int
3444 mod_load(struct modctl *mp, int usepath)
3445 {
3446 int retval;
3447 struct modinfo *modinfop = NULL;
3448 struct loadmt lt;
3449
3450 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3451 ASSERT(mp->mod_busy);
3452
3453 if (mp->mod_loaded)
3454 return (0);
3455
3456 if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
3457 mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
3458 if (moddebug & MODDEBUG_LOADMSG) {
3459 printf(mod_excl_msg, mp->mod_filename,
3460 mp->mod_modname);
3461 }
3462 return (ENXIO);
3463 }
3464 if (moddebug & MODDEBUG_LOADMSG2)
3465 printf(loading_msg, mp->mod_filename, mp->mod_id);
3466
3467 if (curthread != &t0) {
3468 lt.mp = mp;
3469 lt.usepath = usepath;
3470 lt.owner = curthread;
3471 sema_init(<.sema, 0, NULL, SEMA_DEFAULT, NULL);
3472
3473 /* create thread to hand of call to */
3474 (void) thread_create(NULL, DEFAULTSTKSZ * 2,
3475 modload_thread, <, 0, &p0, TS_RUN, maxclsyspri);
3476
3477 /* wait for thread to complete kobj_load_module */
3478 sema_p(<.sema);
3479
3480 sema_destroy(<.sema);
3481 retval = lt.retval;
3482 } else
3483 retval = kobj_load_module(mp, usepath);
3484
3485 if (mp->mod_mp) {
3486 ASSERT(retval == 0);
3487 mp->mod_loaded = 1;
3488 mp->mod_loadcnt++;
3489 if (moddebug & MODDEBUG_LOADMSG) {
3490 printf(load_msg, mp->mod_filename, mp->mod_id,
3491 (void *)((struct module *)mp->mod_mp)->text,
3492 (void *)((struct module *)mp->mod_mp)->data,
3493 ((struct module *)mp->mod_mp)->text_size,
3494 ((struct module *)mp->mod_mp)->data_size);
3495 }
3496
3497 /*
3498 * XXX - There should be a better way to get this.
3499 */
3500 modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
3501 modinfop->mi_info = MI_INFO_LINKAGE;
3502 if (mod_getinfo(mp, modinfop) == 0)
3503 mp->mod_linkage = NULL;
3504 else {
3505 mp->mod_linkage = (void *)modinfop->mi_base;
3506 ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
3507 }
3508
3509 /*
3510 * DCS: bootstrapping code. If the driver is loaded
3511 * before root mount, it is assumed that the driver
3512 * may be used before mounting root. In order to
3513 * access mappings of global to local minor no.'s
3514 * during installation/open of the driver, we load
3515 * them into memory here while the BOP_interfaces
3516 * are still up.
3517 */
3518 if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
3519 retval = clboot_modload(mp);
3520 }
3521
3522 kmem_free(modinfop, sizeof (struct modinfo));
3523 (void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
3524 retval = install_stubs_by_name(mp, mp->mod_modname);
3525
3526 /*
3527 * Now that the module is loaded, we need to give DTrace
3528 * a chance to notify its providers. This is done via
3529 * the dtrace_modload function pointer.
3530 */
3531 if (strcmp(mp->mod_modname, "dtrace") != 0) {
3532 struct modctl *dmp = mod_hold_by_name("dtrace");
3533
3534 if (dmp != NULL && dtrace_modload != NULL)
3535 (*dtrace_modload)(mp);
3536
3537 mod_release_mod(dmp);
3538 }
3539
3540 } else {
3541 /*
3542 * If load failed then we need to release any requisites
3543 * that we had established.
3544 */
3545 ASSERT(retval);
3546 mod_release_requisites(mp);
3547
3548 if (moddebug & MODDEBUG_ERRMSG)
3549 printf("error loading '%s', error %d\n",
3550 mp->mod_filename, retval);
3551 }
3552 return (retval);
3553 }
3554
3555 static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
3556
3557 static void
3558 mod_unload(struct modctl *mp)
3559 {
3560 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3561 ASSERT(mp->mod_busy);
3562 ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
3563 ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
3564
3565 if (moddebug & MODDEBUG_LOADMSG)
3566 printf(unload_msg, mp->mod_modname,
3567 mp->mod_id, mp->mod_loadcnt);
3568
3569 /*
3570 * If mod_ref is not zero, it means some modules might still refer
3571 * to this module. Then you can't unload this module right now.
3572 * Instead, set 1 to mod_delay_unload to notify the system of
3573 * unloading this module later when it's not required any more.
3574 */
3575 if (mp->mod_ref > 0) {
3576 mp->mod_delay_unload = 1;
3577 if (moddebug & MODDEBUG_LOADMSG2) {
3578 printf("module %s not unloaded,"
3579 " non-zero reference count (%d)",
3580 mp->mod_modname, mp->mod_ref);
3581 }
3582 return;
3583 }
3584
3585 if (((mp->mod_loaded == 0) || mp->mod_installed) ||
3586 (mp->mod_ref || mp->mod_prim)) {
3587 /*
3588 * A DEBUG kernel would ASSERT panic above, the code is broken
3589 * if we get this warning.
3590 */
3591 cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
3592 mp->mod_filename, mp->mod_installed, mp->mod_loaded,
3593 mp->mod_ref);
3594 return;
3595 }
3596
3597 /* reset stub functions to call the binder again */
3598 reset_stubs(mp);
3599
3600 /*
3601 * mark module as unloaded before the modctl structure is freed.
3602 * This is required not to reuse the modctl structure before
3603 * the module is marked as unloaded.
3604 */
3605 mp->mod_loaded = 0;
3606 mp->mod_linkage = NULL;
3607
3608 /* free the memory */
3609 kobj_unload_module(mp);
3610
3611 if (mp->mod_delay_unload) {
3612 mp->mod_delay_unload = 0;
3613 if (moddebug & MODDEBUG_LOADMSG2) {
3614 printf("deferred unload of module %s"
3615 " (id %d) successful",
3616 mp->mod_modname, mp->mod_id);
3617 }
3618 }
3619
3620 /* release hold on requisites */
3621 mod_release_requisites(mp);
3622
3623 /*
3624 * Now that the module is gone, we need to give DTrace a chance to
3625 * remove any probes that it may have had in the module. This is
3626 * done via the dtrace_modunload function pointer.
3627 */
3628 if (strcmp(mp->mod_modname, "dtrace") != 0) {
3629 struct modctl *dmp = mod_hold_by_name("dtrace");
3630
3631 if (dmp != NULL && dtrace_modunload != NULL)
3632 (*dtrace_modunload)(mp);
3633
3634 mod_release_mod(dmp);
3635 }
3636 }
3637
3638 static int
3639 modinstall(struct modctl *mp)
3640 {
3641 int val;
3642 int (*func)(void);
3643
3644 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3645 ASSERT(mp->mod_busy && mp->mod_loaded);
3646
3647 if (mp->mod_installed)
3648 return (0);
3649 /*
3650 * If mod_delay_unload is on, it means the system chose the deferred
3651 * unload for this module. Then you can't install this module until
3652 * it's unloaded from the system.
3653 */
3654 if (mp->mod_delay_unload)
3655 return (ENXIO);
3656
3657 if (moddebug & MODDEBUG_LOADMSG)
3658 printf("installing %s, module id %d.\n",
3659 mp->mod_modname, mp->mod_id);
3660
3661 ASSERT(mp->mod_mp != NULL);
3662 if (mod_install_requisites(mp) != 0) {
3663 /*
3664 * Note that we can't call mod_unload(mp) here since
3665 * if modinstall() was called by mod_install_requisites(),
3666 * we won't be able to hold the dependent modules
3667 * (otherwise there would be a deadlock).
3668 */
3669 return (ENXIO);
3670 }
3671
3672 if (moddebug & MODDEBUG_ERRMSG) {
3673 printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
3674 mp->mod_filename, mp->mod_id,
3675 (void *)((struct module *)mp->mod_mp)->text,
3676 (void *)((struct module *)mp->mod_mp)->data,
3677 ((struct module *)mp->mod_mp)->text_size,
3678 ((struct module *)mp->mod_mp)->data_size);
3679 }
3680
3681 func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
3682
3683 if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
3684 cmn_err(CE_WARN, "_init() not defined properly in %s",
3685 mp->mod_filename);
3686 return (EFAULT);
3687 }
3688
3689 if (moddebug & MODDEBUG_USERDEBUG) {
3690 printf("breakpoint before calling %s:_init()\n",
3691 mp->mod_modname);
3692 if (DEBUGGER_PRESENT)
3693 debug_enter("_init");
3694 }
3695
3696 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3697 ASSERT(mp->mod_busy && mp->mod_loaded);
3698 val = (*func)(); /* call _init */
3699
3700 if (moddebug & MODDEBUG_USERDEBUG)
3701 printf("Returned from _init, val = %x\n", val);
3702
3703 if (val == 0) {
3704 /*
3705 * Set the MODS_INSTALLED flag to enable this module
3706 * being called now.
3707 */
3708 install_stubs(mp);
3709 mp->mod_installed = 1;
3710 } else if (moddebug & MODDEBUG_ERRMSG)
3711 printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
3712
3713 return (val);
3714 }
3715
3716 int detach_driver_unconfig = 0;
3717
3718 static int
3719 detach_driver(char *name)
3720 {
3721 major_t major;
3722 int error;
3723
3724 /*
3725 * If being called from mod_uninstall_all() then the appropriate
3726 * driver detaches (leaf only) have already been done.
3727 */
3728 if (mod_in_autounload())
3729 return (0);
3730
3731 major = ddi_name_to_major(name);
3732 if (major == DDI_MAJOR_T_NONE)
3733 return (0);
3734
3735 error = ndi_devi_unconfig_driver(ddi_root_node(),
3736 NDI_DETACH_DRIVER | detach_driver_unconfig, major);
3737 return (error == NDI_SUCCESS ? 0 : -1);
3738 }
3739
3740 static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
3741
3742 static int
3743 moduninstall(struct modctl *mp)
3744 {
3745 int status = 0;
3746 int (*func)(void);
3747
3748 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3749 ASSERT(mp->mod_busy);
3750
3751 /*
3752 * Verify that we need to do something and can uninstall the module.
3753 *
3754 * If we should not uninstall the module or if the module is not in
3755 * the correct state to start an uninstall we return EBUSY to prevent
3756 * us from progressing to mod_unload. If the module has already been
3757 * uninstalled and unloaded we return EALREADY.
3758 */
3759 if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
3760 return (EBUSY);
3761 if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
3762 return (EALREADY);
3763
3764 /*
3765 * To avoid devinfo / module deadlock we must release this module
3766 * prior to initiating the detach_driver, otherwise the detach_driver
3767 * might deadlock on a devinfo node held by another thread
3768 * coming top down and involving the module we have locked.
3769 *
3770 * When we regrab the module we must reverify that it is OK
3771 * to proceed with the uninstall operation.
3772 */
3773 mod_release_mod(mp);
3774 status = detach_driver(mp->mod_modname);
3775 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
3776
3777 /* check detach status and reverify state with lock */
3778 mutex_enter(&mod_lock);
3779 if ((status != 0) || mp->mod_prim || mp->mod_ref) {
3780 mutex_exit(&mod_lock);
3781 return (EBUSY);
3782 }
3783 if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
3784 mutex_exit(&mod_lock);
3785 return (EALREADY);
3786 }
3787 mutex_exit(&mod_lock);
3788
3789 if (moddebug & MODDEBUG_LOADMSG2)
3790 printf("uninstalling %s\n", mp->mod_modname);
3791
3792 /*
3793 * lookup _fini, return EBUSY if not defined.
3794 *
3795 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
3796 * detach(9E) - it allows bufctl addresses to be resolved.
3797 */
3798 func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
3799 if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
3800 (moddebug & MODDEBUG_FINI_EBUSY))
3801 return (EBUSY);
3802
3803 /* verify that _fini is in this module */
3804 if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
3805 cmn_err(CE_WARN, "_fini() not defined properly in %s",
3806 mp->mod_filename);
3807 return (EFAULT);
3808 }
3809
3810 /* call _fini() */
3811 ASSERT(MUTEX_NOT_HELD(&mod_lock));
3812 ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
3813
3814 status = (*func)();
3815
3816 if (status == 0) {
3817 /* _fini returned success, the module is no longer installed */
3818 if (moddebug & MODDEBUG_LOADMSG)
3819 printf("uninstalled %s\n", mp->mod_modname);
3820
3821 /*
3822 * Even though we only set mod_installed to zero here, a zero
3823 * return value means we are committed to a code path were
3824 * mod_loaded will also end up as zero - we have no other
3825 * way to get the module data and bss back to the pre _init
3826 * state except a reload. To ensure this, after return,
3827 * mod_busy must stay set until mod_loaded is cleared.
3828 */
3829 mp->mod_installed = 0;
3830
3831 /*
3832 * Clear the MODS_INSTALLED flag not to call functions
3833 * in the module directly from now on.
3834 */
3835 uninstall_stubs(mp);
3836 } else {
3837 if (moddebug & MODDEBUG_USERDEBUG)
3838 printf(finiret_msg, mp->mod_filename, status);
3839 /*
3840 * By definition _fini is only allowed to return EBUSY or the
3841 * result of mod_remove (EBUSY or EINVAL). In the off chance
3842 * that a driver returns EALREADY we convert this to EINVAL
3843 * since to our caller EALREADY means module was already
3844 * removed.
3845 */
3846 if (status == EALREADY)
3847 status = EINVAL;
3848 }
3849
3850 return (status);
3851 }
3852
3853 /*
3854 * Uninstall all modules.
3855 */
3856 static void
3857 mod_uninstall_all(void)
3858 {
3859 struct modctl *mp;
3860 int pass;
3861 modid_t modid;
3862
3863 /* synchronize with any active modunload_disable() */
3864 modunload_begin();
3865
3866 /* mark this thread as doing autounloading */
3867 (void) tsd_set(mod_autounload_key, (void *)1);
3868
3869 (void) devfs_clean(ddi_root_node(), NULL, 0);
3870 (void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
3871
3872 /*
3873 * Loop up to max times if we keep producing unreferenced modules.
3874 * A new unreferenced module is an opportunity to unload.
3875 */
3876 for (pass = 0; pass < mod_uninstall_pass_max; pass++) {
3877
3878 /* zero count of modules that go unreferenced during pass */
3879 mod_uninstall_ref_zero = 0;
3880
3881 modid = 0;
3882 while ((mp = mod_hold_next_by_id(modid)) != NULL) {
3883 modid = mp->mod_id;
3884
3885 /*
3886 * Skip modules with the MOD_NOAUTOUNLOAD flag set
3887 */
3888 if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
3889 mod_release_mod(mp);
3890 continue;
3891 }
3892
3893 if (moduninstall(mp) == 0) {
3894 mod_unload(mp);
3895 CPU_STATS_ADDQ(CPU, sys, modunload, 1);
3896 }
3897 mod_release_mod(mp);
3898 }
3899
3900 /* break if no modules went unreferenced during pass */
3901 if (mod_uninstall_ref_zero == 0)
3902 break;
3903 }
3904 if (pass >= mod_uninstall_pass_max)
3905 mod_uninstall_pass_exc++;
3906
3907 (void) tsd_set(mod_autounload_key, NULL);
3908 modunload_end();
3909 }
3910
3911 /* wait for unloads that have begun before registering disable */
3912 void
3913 modunload_disable(void)
3914 {
3915 mutex_enter(&modunload_wait_mutex);
3916 while (modunload_active_count) {
3917 modunload_wait++;
3918 cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3919 modunload_wait--;
3920 }
3921 modunload_disable_count++;
3922 mutex_exit(&modunload_wait_mutex);
3923 }
3924
3925 /* mark end of disable and signal waiters */
3926 void
3927 modunload_enable(void)
3928 {
3929 mutex_enter(&modunload_wait_mutex);
3930 modunload_disable_count--;
3931 if ((modunload_disable_count == 0) && modunload_wait)
3932 cv_broadcast(&modunload_wait_cv);
3933 mutex_exit(&modunload_wait_mutex);
3934 }
3935
3936 /* wait for disables to complete before begining unload */
3937 void
3938 modunload_begin()
3939 {
3940 mutex_enter(&modunload_wait_mutex);
3941 while (modunload_disable_count) {
3942 modunload_wait++;
3943 cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3944 modunload_wait--;
3945 }
3946 modunload_active_count++;
3947 mutex_exit(&modunload_wait_mutex);
3948 }
3949
3950 /* mark end of unload and signal waiters */
3951 void
3952 modunload_end()
3953 {
3954 mutex_enter(&modunload_wait_mutex);
3955 modunload_active_count--;
3956 if ((modunload_active_count == 0) && modunload_wait)
3957 cv_broadcast(&modunload_wait_cv);
3958 mutex_exit(&modunload_wait_mutex);
3959 }
3960
3961 void
3962 mod_uninstall_daemon(void)
3963 {
3964 callb_cpr_t cprinfo;
3965 clock_t ticks;
3966
3967 mod_aul_thread = curthread;
3968
3969 CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
3970 for (;;) {
3971 mutex_enter(&mod_uninstall_lock);
3972 CALLB_CPR_SAFE_BEGIN(&cprinfo);
3973 /*
3974 * In DEBUG kernels, unheld drivers are uninstalled periodically
3975 * every mod_uninstall_interval seconds. Periodic uninstall can
3976 * be disabled by setting mod_uninstall_interval to 0 which is
3977 * the default for a non-DEBUG kernel.
3978 */
3979 if (mod_uninstall_interval) {
3980 ticks = drv_usectohz(mod_uninstall_interval * 1000000);
3981 (void) cv_reltimedwait(&mod_uninstall_cv,
3982 &mod_uninstall_lock, ticks, TR_CLOCK_TICK);
3983 } else {
3984 cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
3985 }
3986 /*
3987 * The whole daemon is safe for CPR except we don't want
3988 * the daemon to run if FREEZE is issued and this daemon
3989 * wakes up from the cv_wait above. In this case, it'll be
3990 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
3991 *
3992 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
3993 * mod_uninstall_lock is used to protect cprinfo and
3994 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
3995 * called.
3996 */
3997 CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
3998 CALLB_CPR_SAFE_BEGIN(&cprinfo);
3999 mutex_exit(&mod_uninstall_lock);
4000 if ((modunload_disable_count == 0) &&
4001 ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
4002 mod_uninstall_all();
4003 }
4004 }
4005 }
4006
4007 /*
4008 * Unload all uninstalled modules.
4009 */
4010 void
4011 modreap(void)
4012 {
4013 mutex_enter(&mod_uninstall_lock);
4014 cv_broadcast(&mod_uninstall_cv);
4015 mutex_exit(&mod_uninstall_lock);
4016 }
4017
4018 /*
4019 * Hold the specified module. This is the module holding primitive.
4020 *
4021 * If MOD_LOCK_HELD then the caller already holds the mod_lock.
4022 *
4023 * Return values:
4024 * 0 ==> the module is held
4025 * 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
4026 * to determine how to retry.
4027 */
4028 int
4029 mod_hold_by_modctl(struct modctl *mp, int f)
4030 {
4031 ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
4032 ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
4033 (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
4034 ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
4035 ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
4036 (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
4037 ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
4038
4039 if (f & MOD_LOCK_NOT_HELD)
4040 mutex_enter(&mod_lock);
4041
4042 while (mp->mod_busy) {
4043 mp->mod_want = 1;
4044 cv_wait(&mod_cv, &mod_lock);
4045 /*
4046 * Module may be unloaded by daemon.
4047 * Nevertheless, modctl structure is still in linked list
4048 * (i.e., off &modules), not freed!
4049 * Caller is not supposed to assume "mp" is valid, but there
4050 * is no reasonable way to detect this but using
4051 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
4052 * (or similar check depending on calling context)
4053 * DON'T free modctl structure, it will be very very
4054 * problematic.
4055 */
4056 if (f & MOD_WAIT_ONCE) {
4057 if (f & MOD_LOCK_NOT_HELD)
4058 mutex_exit(&mod_lock);
4059 return (1); /* caller decides how to retry */
4060 }
4061 }
4062
4063 mp->mod_busy = 1;
4064 mp->mod_inprogress_thread =
4065 (curthread == NULL ? (kthread_id_t)-1 : curthread);
4066
4067 if (f & MOD_LOCK_NOT_HELD)
4068 mutex_exit(&mod_lock);
4069 return (0);
4070 }
4071
4072 static struct modctl *
4073 mod_hold_by_name_common(struct modctl *dep, const char *filename)
4074 {
4075 const char *modname;
4076 struct modctl *mp;
4077 char *curname, *newname;
4078 int found = 0;
4079
4080 mutex_enter(&mod_lock);
4081
4082 if ((modname = strrchr(filename, '/')) == NULL)
4083 modname = filename;
4084 else
4085 modname++;
4086
4087 mp = &modules;
4088 do {
4089 if (strcmp(modname, mp->mod_modname) == 0) {
4090 found = 1;
4091 break;
4092 }
4093 } while ((mp = mp->mod_next) != &modules);
4094
4095 if (found == 0) {
4096 mp = allocate_modp(filename, modname);
4097 modadd(mp);
4098 }
4099
4100 /*
4101 * if dep is not NULL, set the mp in mod_requisite_loading for
4102 * the module circular dependency check. This field is used in
4103 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
4104 */
4105 if (dep != NULL) {
4106 ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
4107 dep->mod_requisite_loading = mp;
4108 }
4109
4110 /*
4111 * If the module was held, then it must be us who has it held.
4112 */
4113 if (mod_circdep(mp))
4114 mp = NULL;
4115 else {
4116 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
4117
4118 /*
4119 * If the name hadn't been set or has changed, allocate
4120 * space and set it. Free space used by previous name.
4121 *
4122 * Do not change the name of primary modules, for primary
4123 * modules the mod_filename was allocated in standalone mode:
4124 * it is illegal to kobj_alloc in standalone mode and kobj_free
4125 * in non-standalone mode.
4126 */
4127 curname = mp->mod_filename;
4128 if (curname == NULL ||
4129 ((mp->mod_prim == 0) &&
4130 (curname != filename) &&
4131 (modname != filename) &&
4132 (strcmp(curname, filename) != 0))) {
4133 newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
4134 (void) strcpy(newname, filename);
4135 mp->mod_filename = newname;
4136 if (curname != NULL)
4137 kobj_free(curname, strlen(curname) + 1);
4138 }
4139 }
4140
4141 mutex_exit(&mod_lock);
4142 if (mp && moddebug & MODDEBUG_LOADMSG2)
4143 printf("Holding %s\n", mp->mod_filename);
4144 if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
4145 printf("circular dependency loading %s\n", filename);
4146 return (mp);
4147 }
4148
4149 static struct modctl *
4150 mod_hold_by_name_requisite(struct modctl *dep, char *filename)
4151 {
4152 return (mod_hold_by_name_common(dep, filename));
4153 }
4154
4155 struct modctl *
4156 mod_hold_by_name(const char *filename)
4157 {
4158 return (mod_hold_by_name_common(NULL, filename));
4159 }
4160
4161 struct modctl *
4162 mod_hold_by_id(modid_t modid)
4163 {
4164 struct modctl *mp;
4165 int found = 0;
4166
4167 mutex_enter(&mod_lock);
4168 mp = &modules;
4169 do {
4170 if (mp->mod_id == modid) {
4171 found = 1;
4172 break;
4173 }
4174 } while ((mp = mp->mod_next) != &modules);
4175
4176 if ((found == 0) || mod_circdep(mp))
4177 mp = NULL;
4178 else
4179 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
4180
4181 mutex_exit(&mod_lock);
4182 return (mp);
4183 }
4184
4185 static struct modctl *
4186 mod_hold_next_by_id(modid_t modid)
4187 {
4188 struct modctl *mp;
4189 int found = 0;
4190
4191 if (modid < -1)
4192 return (NULL);
4193
4194 mutex_enter(&mod_lock);
4195
4196 mp = &modules;
4197 do {
4198 if (mp->mod_id > modid) {
4199 found = 1;
4200 break;
4201 }
4202 } while ((mp = mp->mod_next) != &modules);
4203
4204 if ((found == 0) || mod_circdep(mp))
4205 mp = NULL;
4206 else
4207 (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
4208
4209 mutex_exit(&mod_lock);
4210 return (mp);
4211 }
4212
4213 static void
4214 mod_release(struct modctl *mp)
4215 {
4216 ASSERT(MUTEX_HELD(&mod_lock));
4217 ASSERT(mp->mod_busy);
4218
4219 mp->mod_busy = 0;
4220 mp->mod_inprogress_thread = NULL;
4221 if (mp->mod_want) {
4222 mp->mod_want = 0;
4223 cv_broadcast(&mod_cv);
4224 }
4225 }
4226
4227 void
4228 mod_release_mod(struct modctl *mp)
4229 {
4230 if (moddebug & MODDEBUG_LOADMSG2)
4231 printf("Releasing %s\n", mp->mod_filename);
4232 mutex_enter(&mod_lock);
4233 mod_release(mp);
4234 mutex_exit(&mod_lock);
4235 }
4236
4237 modid_t
4238 mod_name_to_modid(char *filename)
4239 {
4240 char *modname;
4241 struct modctl *mp;
4242
4243 mutex_enter(&mod_lock);
4244
4245 if ((modname = strrchr(filename, '/')) == NULL)
4246 modname = filename;
4247 else
4248 modname++;
4249
4250 mp = &modules;
4251 do {
4252 if (strcmp(modname, mp->mod_modname) == 0) {
4253 mutex_exit(&mod_lock);
4254 return (mp->mod_id);
4255 }
4256 } while ((mp = mp->mod_next) != &modules);
4257
4258 mutex_exit(&mod_lock);
4259 return (-1);
4260 }
4261
4262
4263 int
4264 mod_remove_by_name(char *name)
4265 {
4266 struct modctl *mp;
4267 int retval;
4268
4269 mp = mod_hold_by_name(name);
4270
4271 if (mp == NULL)
4272 return (EINVAL);
4273
4274 if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
4275 /*
4276 * Do not unload forceloaded modules
4277 */
4278 mod_release_mod(mp);
4279 return (0);
4280 }
4281
4282 if ((retval = moduninstall(mp)) == 0) {
4283 mod_unload(mp);
4284 CPU_STATS_ADDQ(CPU, sys, modunload, 1);
4285 } else if (retval == EALREADY)
4286 retval = 0; /* already unloaded, not an error */
4287 mod_release_mod(mp);
4288 return (retval);
4289 }
4290
4291 /*
4292 * Record that module "dep" is dependent on module "on_mod."
4293 */
4294 static void
4295 mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
4296 {
4297 struct modctl_list **pmlnp; /* previous next pointer */
4298 struct modctl_list *mlp;
4299 struct modctl_list *new;
4300
4301 ASSERT(dependent->mod_busy && on_mod->mod_busy);
4302 mutex_enter(&mod_lock);
4303
4304 /*
4305 * Search dependent's requisite list to see if on_mod is recorded.
4306 * List is ordered by id.
4307 */
4308 for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
4309 mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
4310 if (mlp->modl_modp->mod_id >= on_mod->mod_id)
4311 break;
4312
4313 /* Create and insert if not already recorded */
4314 if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
4315 new = kobj_zalloc(sizeof (*new), KM_SLEEP);
4316 new->modl_modp = on_mod;
4317 new->modl_next = mlp;
4318 *pmlnp = new;
4319
4320 /*
4321 * Increment the mod_ref count in our new requisite module.
4322 * This is what keeps a module that has other modules
4323 * which are dependent on it from being uninstalled and
4324 * unloaded. "on_mod"'s mod_ref count decremented in
4325 * mod_release_requisites when the "dependent" module
4326 * unload is complete. "on_mod" must be loaded, but may not
4327 * yet be installed.
4328 */
4329 on_mod->mod_ref++;
4330 ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
4331 }
4332
4333 mutex_exit(&mod_lock);
4334 }
4335
4336 /*
4337 * release the hold associated with mod_make_requisite mod_ref++
4338 * as part of unload.
4339 */
4340 void
4341 mod_release_requisites(struct modctl *modp)
4342 {
4343 struct modctl_list *modl;
4344 struct modctl_list *next;
4345 struct modctl *req;
4346 struct modctl_list *start = NULL, *mod_garbage;
4347
4348 ASSERT(!quiesce_active);
4349 ASSERT(modp->mod_busy);
4350 ASSERT(MUTEX_NOT_HELD(&mod_lock));
4351
4352 mutex_enter(&mod_lock); /* needed for manipulation of req */
4353 for (modl = modp->mod_requisites; modl; modl = next) {
4354 next = modl->modl_next;
4355 req = modl->modl_modp;
4356 ASSERT(req->mod_ref >= 1 && req->mod_loaded);
4357 req->mod_ref--;
4358 if (req->mod_ref == 0)
4359 mod_uninstall_ref_zero++;
4360
4361 /*
4362 * Check if the module has to be unloaded or not.
4363 */
4364 if (req->mod_ref == 0 && req->mod_delay_unload) {
4365 struct modctl_list *new;
4366 /*
4367 * Allocate the modclt_list holding the garbage
4368 * module which should be unloaded later.
4369 */
4370 new = kobj_zalloc(sizeof (struct modctl_list),
4371 KM_SLEEP);
4372 new->modl_modp = req;
4373
4374 if (start == NULL)
4375 mod_garbage = start = new;
4376 else {
4377 mod_garbage->modl_next = new;
4378 mod_garbage = new;
4379 }
4380 }
4381
4382 /* free the list as we go */
4383 kobj_free(modl, sizeof (*modl));
4384 }
4385 modp->mod_requisites = NULL;
4386 mutex_exit(&mod_lock);
4387
4388 /*
4389 * Unload the garbage modules.
4390 */
4391 for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
4392 struct modctl_list *old = mod_garbage;
4393 struct modctl *mp = mod_garbage->modl_modp;
4394 ASSERT(mp != NULL);
4395
4396 /*
4397 * Hold this module until it's unloaded completely.
4398 */
4399 (void) mod_hold_by_modctl(mp,
4400 MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
4401 /*
4402 * Check if the module is not unloaded yet and nobody requires
4403 * the module. If it's unloaded already or somebody still
4404 * requires the module, don't unload it now.
4405 */
4406 if (mp->mod_loaded && mp->mod_ref == 0)
4407 mod_unload(mp);
4408 ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
4409 (mp->mod_ref > 0));
4410 mod_release_mod(mp);
4411
4412 mod_garbage = mod_garbage->modl_next;
4413 kobj_free(old, sizeof (struct modctl_list));
4414 }
4415 }
4416
4417 /*
4418 * Process dependency of the module represented by "dep" on the
4419 * module named by "on."
4420 *
4421 * Called from kobj_do_dependents() to load a module "on" on which
4422 * "dep" depends.
4423 */
4424 struct modctl *
4425 mod_load_requisite(struct modctl *dep, char *on)
4426 {
4427 struct modctl *on_mod;
4428 int retval;
4429
4430 if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
4431 mod_make_requisite(dep, on_mod);
4432 } else if (moddebug & MODDEBUG_ERRMSG) {
4433 printf("error processing %s on which module %s depends\n",
4434 on, dep->mod_modname);
4435 }
4436 return (on_mod);
4437 }
4438
4439 static int
4440 mod_install_requisites(struct modctl *modp)
4441 {
4442 struct modctl_list *modl;
4443 struct modctl *req;
4444 int status = 0;
4445
4446 ASSERT(MUTEX_NOT_HELD(&mod_lock));
4447 ASSERT(modp->mod_busy);
4448
4449 for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
4450 req = modl->modl_modp;
4451 (void) mod_hold_by_modctl(req,
4452 MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
4453 status = modinstall(req);
4454 mod_release_mod(req);
4455
4456 if (status != 0)
4457 break;
4458 }
4459 return (status);
4460 }
4461
4462 /*
4463 * returns 1 if this thread is doing autounload, 0 otherwise.
4464 * see mod_uninstall_all.
4465 */
4466 int
4467 mod_in_autounload()
4468 {
4469 return ((int)(uintptr_t)tsd_get(mod_autounload_key));
4470 }
4471
4472 /*
4473 * gmatch adapted from libc, stripping the wchar stuff
4474 */
4475 #define popchar(p, c) { \
4476 c = *p++; \
4477 if (c == 0) { \
4478 return (0); \
4479 } \
4480 }
4481
4482 int
4483 gmatch(const char *s, const char *p)
4484 {
4485 int c, sc;
4486 int ok, lc, notflag;
4487
4488 sc = *s++;
4489 c = *p++;
4490 if (c == 0)
4491 return (sc == c); /* nothing matches nothing */
4492
4493 switch (c) {
4494 case '\\':
4495 /* skip to quoted character */
4496 popchar(p, c);
4497 /*FALLTHRU*/
4498
4499 default:
4500 /* straight comparison */
4501 if (c != sc)
4502 return (0);
4503 /*FALLTHRU*/
4504
4505 case '?':
4506 /* first char matches, move to remainder */
4507 return (sc != '\0' ? gmatch(s, p) : 0);
4508
4509
4510 case '*':
4511 while (*p == '*')
4512 p++;
4513
4514 /* * matches everything */
4515 if (*p == 0)
4516 return (1);
4517
4518 /* undo skip at the beginning & iterate over substrings */
4519 --s;
4520 while (*s) {
4521 if (gmatch(s, p))
4522 return (1);
4523 s++;
4524 }
4525 return (0);
4526
4527 case '[':
4528 /* match any char within [] */
4529 if (sc == 0)
4530 return (0);
4531
4532 ok = lc = notflag = 0;
4533
4534 if (*p == '!') {
4535 notflag = 1;
4536 p++;
4537 }
4538 popchar(p, c);
4539
4540 do {
4541 if (c == '-' && lc && *p != ']') {
4542 /* test sc against range [c1-c2] */
4543 popchar(p, c);
4544 if (c == '\\') {
4545 popchar(p, c);
4546 }
4547
4548 if (notflag) {
4549 /* return 0 on mismatch */
4550 if (lc <= sc && sc <= c)
4551 return (0);
4552 ok++;
4553 } else if (lc <= sc && sc <= c) {
4554 ok++;
4555 }
4556 /* keep going, may get a match next */
4557 } else if (c == '\\') {
4558 /* skip to quoted character */
4559 popchar(p, c);
4560 }
4561 lc = c;
4562 if (notflag) {
4563 if (sc == lc)
4564 return (0);
4565 ok++;
4566 } else if (sc == lc) {
4567 ok++;
4568 }
4569 popchar(p, c);
4570 } while (c != ']');
4571
4572 /* recurse on remainder of string */
4573 return (ok ? gmatch(s, p) : 0);
4574 }
4575 /*NOTREACHED*/
4576 }
4577
4578
4579 /*
4580 * Get default perm for device from /etc/minor_perm. Return 0 if match found.
4581 *
4582 * Pure wild-carded patterns are handled separately so the ordering of
4583 * these patterns doesn't matter. We're still dependent on ordering
4584 * however as the first matching entry is the one returned.
4585 * Not ideal but all existing examples and usage do imply this
4586 * ordering implicitly.
4587 *
4588 * Drivers using the clone driver are always good for some entertainment.
4589 * Clone nodes under pseudo have the form clone@0:<driver>. Some minor
4590 * perm entries have the form clone:<driver>, others use <driver>:*
4591 * Examples are clone:llc1 vs. llc2:*, for example.
4592 *
4593 * Minor perms in the clone:<driver> form are mapped to the drivers's
4594 * mperm list, not the clone driver, as wildcard entries for clone
4595 * reference only. In other words, a clone wildcard will match
4596 * references for clone@0:<driver> but never <driver>@<minor>.
4597 *
4598 * Additional minor perms in the standard form are also supported,
4599 * for mixed usage, ie a node with an entry clone:<driver> could
4600 * provide further entries <driver>:<minor>.
4601 *
4602 * Finally, some uses of clone use an alias as the minor name rather
4603 * than the driver name, with the alias as the minor perm entry.
4604 * This case is handled by attaching the driver to bring its
4605 * minor list into existence, then discover the alias via DDI_ALIAS.
4606 * The clone device's minor perm list can then be searched for
4607 * that alias.
4608 */
4609
4610 static int
4611 dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
4612 {
4613 major_t major;
4614 struct devnames *dnp;
4615 mperm_t *mp;
4616 char *alias = NULL;
4617 dev_info_t *cdevi;
4618 int circ;
4619 struct ddi_minor_data *dmd;
4620
4621 major = ddi_name_to_major(minor_name);
4622
4623 ASSERT(dip == clone_dip);
4624 ASSERT(major != DDI_MAJOR_T_NONE);
4625
4626 /*
4627 * Attach the driver named by the minor node, then
4628 * search its first instance's minor list for an
4629 * alias node.
4630 */
4631 if (ddi_hold_installed_driver(major) == NULL)
4632 return (1);
4633
4634 dnp = &devnamesp[major];
4635 LOCK_DEV_OPS(&dnp->dn_lock);
4636
4637 if ((cdevi = dnp->dn_head) != NULL) {
4638 ndi_devi_enter(cdevi, &circ);
4639 for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
4640 if (dmd->type == DDM_ALIAS) {
4641 alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
4642 break;
4643 }
4644 }
4645 ndi_devi_exit(cdevi, circ);
4646 }
4647
4648 UNLOCK_DEV_OPS(&dnp->dn_lock);
4649 ddi_rele_driver(major);
4650
4651 if (alias == NULL) {
4652 if (moddebug & MODDEBUG_MINORPERM)
4653 cmn_err(CE_CONT, "dev_minorperm: "
4654 "no alias for %s\n", minor_name);
4655 return (1);
4656 }
4657
4658 major = ddi_driver_major(clone_dip);
4659 dnp = &devnamesp[major];
4660 LOCK_DEV_OPS(&dnp->dn_lock);
4661
4662 /*
4663 * Go through the clone driver's mperm list looking
4664 * for a match for the specified alias.
4665 */
4666 for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
4667 if (strcmp(alias, mp->mp_minorname) == 0) {
4668 break;
4669 }
4670 }
4671
4672 if (mp) {
4673 if (moddebug & MODDEBUG_MP_MATCH) {
4674 cmn_err(CE_CONT,
4675 "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
4676 minor_name, alias, mp->mp_mode,
4677 mp->mp_uid, mp->mp_gid);
4678 }
4679 rmp->mp_uid = mp->mp_uid;
4680 rmp->mp_gid = mp->mp_gid;
4681 rmp->mp_mode = mp->mp_mode;
4682 }
4683 UNLOCK_DEV_OPS(&dnp->dn_lock);
4684
4685 kmem_free(alias, strlen(alias)+1);
4686
4687 return (mp == NULL);
4688 }
4689
4690 int
4691 dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
4692 {
4693 major_t major;
4694 char *minor_name;
4695 struct devnames *dnp;
4696 mperm_t *mp;
4697 int is_clone = 0;
4698
4699 if (!minorperm_loaded) {
4700 if (moddebug & MODDEBUG_MINORPERM)
4701 cmn_err(CE_CONT,
4702 "%s: minor perm not yet loaded\n", name);
4703 return (1);
4704 }
4705
4706 minor_name = strchr(name, ':');
4707 if (minor_name == NULL)
4708 return (1);
4709 minor_name++;
4710
4711 /*
4712 * If it's the clone driver, search the driver as named
4713 * by the minor. All clone minor perm entries other than
4714 * alias nodes are actually installed on the real driver's list.
4715 */
4716 if (dip == clone_dip) {
4717 major = ddi_name_to_major(minor_name);
4718 if (major == DDI_MAJOR_T_NONE) {
4719 if (moddebug & MODDEBUG_MINORPERM)
4720 cmn_err(CE_CONT, "dev_minorperm: "
4721 "%s: no such driver\n", minor_name);
4722 return (1);
4723 }
4724 is_clone = 1;
4725 } else {
4726 major = ddi_driver_major(dip);
4727 ASSERT(major != DDI_MAJOR_T_NONE);
4728 }
4729
4730 dnp = &devnamesp[major];
4731 LOCK_DEV_OPS(&dnp->dn_lock);
4732
4733 /*
4734 * Go through the driver's mperm list looking for
4735 * a match for the specified minor. If there's
4736 * no matching pattern, use the wild card.
4737 * Defer to the clone wild for clone if specified,
4738 * otherwise fall back to the normal form.
4739 */
4740 for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
4741 if (gmatch(minor_name, mp->mp_minorname) != 0) {
4742 break;
4743 }
4744 }
4745 if (mp == NULL) {
4746 if (is_clone)
4747 mp = dnp->dn_mperm_clone;
4748 if (mp == NULL)
4749 mp = dnp->dn_mperm_wild;
4750 }
4751
4752 if (mp) {
4753 if (moddebug & MODDEBUG_MP_MATCH) {
4754 cmn_err(CE_CONT,
4755 "minor perm defaults: %s %s 0%o %d %d\n",
4756 name, mp->mp_minorname, mp->mp_mode,
4757 mp->mp_uid, mp->mp_gid);
4758 }
4759 rmp->mp_uid = mp->mp_uid;
4760 rmp->mp_gid = mp->mp_gid;
4761 rmp->mp_mode = mp->mp_mode;
4762 }
4763 UNLOCK_DEV_OPS(&dnp->dn_lock);
4764
4765 /*
4766 * If no match can be found for a clone node,
4767 * search for a possible match for an alias.
4768 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
4769 * with minor perm entry clone:ptmx.
4770 */
4771 if (mp == NULL && is_clone) {
4772 return (dev_alias_minorperm(dip, minor_name, rmp));
4773 }
4774
4775 return (mp == NULL);
4776 }
4777
4778 /*
4779 * dynamicaly reference load a dl module/library, returning handle
4780 */
4781 /*ARGSUSED*/
4782 ddi_modhandle_t
4783 ddi_modopen(const char *modname, int mode, int *errnop)
4784 {
4785 char *subdir;
4786 char *mod;
4787 int subdirlen;
4788 struct modctl *hmodp = NULL;
4789 int retval = EINVAL;
4790
4791 ASSERT(modname && (mode == KRTLD_MODE_FIRST));
4792 if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
4793 goto out;
4794
4795 /* find last '/' in modname */
4796 mod = strrchr(modname, '/');
4797
4798 if (mod) {
4799 /* for subdir string without modification to argument */
4800 mod++;
4801 subdirlen = mod - modname;
4802 subdir = kmem_alloc(subdirlen, KM_SLEEP);
4803 (void) strlcpy(subdir, modname, subdirlen);
4804 } else {
4805 subdirlen = 0;
4806 subdir = "misc";
4807 mod = (char *)modname;
4808 }
4809
4810 /* reference load with errno return value */
4811 retval = modrload(subdir, mod, &hmodp);
4812
4813 if (subdirlen)
4814 kmem_free(subdir, subdirlen);
4815
4816 out: if (errnop)
4817 *errnop = retval;
4818
4819 if (moddebug & MODDEBUG_DDI_MOD)
4820 printf("ddi_modopen %s mode %x: %s %p %d\n",
4821 modname ? modname : "<unknown>", mode,
4822 hmodp ? hmodp->mod_filename : "<unknown>",
4823 (void *)hmodp, retval);
4824
4825 return ((ddi_modhandle_t)hmodp);
4826 }
4827
4828 /* lookup "name" in open dl module/library */
4829 void *
4830 ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
4831 {
4832 struct modctl *hmodp = (struct modctl *)h;
4833 void *f;
4834 int retval;
4835
4836 ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
4837 if ((hmodp == NULL) || (name == NULL) ||
4838 (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
4839 f = NULL;
4840 retval = EINVAL;
4841 } else {
4842 f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
4843 if (f)
4844 retval = 0;
4845 else
4846 retval = ENOTSUP;
4847 }
4848
4849 if (moddebug & MODDEBUG_DDI_MOD)
4850 printf("ddi_modsym in %s of %s: %d %p\n",
4851 hmodp ? hmodp->mod_modname : "<unknown>",
4852 name ? name : "<unknown>", retval, f);
4853
4854 if (errnop)
4855 *errnop = retval;
4856 return (f);
4857 }
4858
4859 /* dynamic (un)reference unload of an open dl module/library */
4860 int
4861 ddi_modclose(ddi_modhandle_t h)
4862 {
4863 struct modctl *hmodp = (struct modctl *)h;
4864 struct modctl *modp = NULL;
4865 int retval;
4866
4867 ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
4868 if ((hmodp == NULL) ||
4869 (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
4870 retval = EINVAL;
4871 goto out;
4872 }
4873
4874 retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
4875 if (retval == EBUSY)
4876 retval = 0; /* EBUSY is not an error */
4877
4878 if (retval == 0) {
4879 ASSERT(hmodp == modp);
4880 if (hmodp != modp)
4881 retval = EINVAL;
4882 }
4883
4884 out: if (moddebug & MODDEBUG_DDI_MOD)
4885 printf("ddi_modclose %s: %d\n",
4886 hmodp ? hmodp->mod_modname : "<unknown>", retval);
4887
4888 return (retval);
4889 }