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