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