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