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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _SYS_MODCTL_H 28 #define _SYS_MODCTL_H 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * loadable module support. 34 */ 35 36 #include <sys/types.h> 37 #include <sys/ioccom.h> 38 #include <sys/nexusdefs.h> 39 #include <sys/thread.h> 40 #include <sys/t_lock.h> 41 #include <sys/dditypes.h> 42 #include <sys/hwconf.h> 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 /* 49 * The following structure defines the operations used by modctl 50 * to load and unload modules. Each supported loadable module type 51 * requires a set of mod_ops. 52 */ 53 struct mod_ops { 54 int (*modm_install)(); /* install module in kernel */ 55 int (*modm_remove)(); /* remove from kernel */ 56 int (*modm_info)(); /* module info */ 57 }; 58 59 #ifdef _KERNEL 60 61 /* 62 * The defined set of mod_ops structures for each loadable module type 63 * Defined in modctl.c 64 */ 65 #if defined(__i386) || defined(__amd64) 66 extern struct mod_ops mod_cpuops; 67 #endif 68 extern struct mod_ops mod_cryptoops; 69 extern struct mod_ops mod_driverops; 70 extern struct mod_ops mod_execops; 71 extern struct mod_ops mod_fsops; 72 extern struct mod_ops mod_miscops; 73 extern struct mod_ops mod_schedops; 74 extern struct mod_ops mod_strmodops; 75 extern struct mod_ops mod_syscallops; 76 #ifdef _SYSCALL32_IMPL 77 extern struct mod_ops mod_syscallops32; 78 #endif 79 extern struct mod_ops mod_dacfops; 80 extern struct mod_ops mod_ippops; 81 extern struct mod_ops mod_pcbeops; 82 83 #endif /* _KERNEL */ 84 85 /* 86 * Definitions for the module specific linkage structures. 87 * The first two fields are the same in all of the structures. 88 * The linkinfo is for informational purposes only and is returned by 89 * modctl with the MODINFO cmd. 90 */ 91 92 /* For drivers */ 93 struct modldrv { 94 struct mod_ops *drv_modops; 95 char *drv_linkinfo; 96 struct dev_ops *drv_dev_ops; 97 }; 98 99 /* For system calls */ 100 struct modlsys { 101 struct mod_ops *sys_modops; 102 char *sys_linkinfo; 103 struct sysent *sys_sysent; 104 }; 105 106 /* For filesystems */ 107 struct modlfs { 108 struct mod_ops *fs_modops; 109 char *fs_linkinfo; 110 struct vfsdef_v3 *fs_vfsdef; /* version may actually vary */ 111 }; 112 113 #if defined(__i386) || defined(__amd64) 114 struct cmi_ops; 115 116 /* For CPU modules */ 117 struct modlcpu { 118 struct mod_ops *cpu_modops; 119 char *cpu_linkinfo; 120 struct cmi_ops *cpu_cmiops; 121 }; 122 #endif 123 124 /* For cryptographic providers */ 125 struct modlcrypto { 126 struct mod_ops *crypto_modops; 127 char *crypto_linkinfo; 128 }; 129 130 /* For misc */ 131 struct modlmisc { 132 struct mod_ops *misc_modops; 133 char *misc_linkinfo; 134 }; 135 136 /* For IP Modules */ 137 struct modlipp { 138 struct mod_ops *ipp_modops; 139 char *ipp_linkinfo; 140 struct ipp_ops *ipp_ops; 141 }; 142 143 /* For Streams Modules. */ 144 struct modlstrmod { 145 struct mod_ops *strmod_modops; 146 char *strmod_linkinfo; 147 struct fmodsw *strmod_fmodsw; 148 }; 149 150 /* For Scheduling classes */ 151 struct modlsched { 152 struct mod_ops *sched_modops; 153 char *sched_linkinfo; 154 struct sclass *sched_class; 155 }; 156 157 /* For Exec file type (like ELF, ...) */ 158 struct modlexec { 159 struct mod_ops *exec_modops; 160 char *exec_linkinfo; 161 struct execsw *exec_execsw; 162 }; 163 164 /* For dacf modules */ 165 struct modldacf { 166 struct mod_ops *dacf_modops; 167 char *dacf_linkinfo; 168 struct dacfsw *dacf_dacfsw; 169 }; 170 171 /* For PCBE modules */ 172 struct modlpcbe { 173 struct mod_ops *pcbe_modops; 174 char *pcbe_linkinfo; 175 struct __pcbe_ops *pcbe_ops; 176 }; 177 178 /* 179 * Revision number of loadable modules support. This is the value 180 * that must be used in the modlinkage structure. 181 */ 182 #define MODREV_1 1 183 184 /* 185 * The modlinkage structure is the structure that the module writer 186 * provides to the routines to install, remove, and stat a module. 187 * The ml_linkage element is an array of pointers to linkage structures. 188 * For most modules there is only one linkage structure. We allocate 189 * enough space for 3 linkage structures which happens to be the most 190 * we have in any sun supplied module. For those modules with more 191 * than 3 linkage structures (which is very unlikely), a modlinkage 192 * structure must be kmem_alloc'd in the module wrapper to be big enough 193 * for all of the linkage structures. 194 */ 195 struct modlinkage { 196 int ml_rev; /* rev of loadable modules system */ 197 #ifdef _LP64 198 void *ml_linkage[7]; /* more space in 64-bit OS */ 199 #else 200 void *ml_linkage[4]; /* NULL terminated list of */ 201 /* linkage structures */ 202 #endif 203 }; 204 205 /* 206 * commands. These are the commands supported by the modctl system call. 207 */ 208 #define MODLOAD 0 209 #define MODUNLOAD 1 210 #define MODINFO 2 211 #define MODRESERVED 3 212 #define MODSETMINIROOT 4 213 #define MODADDMAJBIND 5 214 #define MODGETPATH 6 215 #define MODREADSYSBIND 7 216 #define MODGETMAJBIND 8 217 #define MODGETNAME 9 218 #define MODSIZEOF_DEVID 10 219 #define MODGETDEVID 11 220 #define MODSIZEOF_MINORNAME 12 221 #define MODGETMINORNAME 13 222 #define MODGETPATHLEN 14 223 #define MODEVENTS 15 224 #define MODGETFBNAME 16 225 #define MODREREADDACF 17 226 #define MODLOADDRVCONF 18 227 #define MODUNLOADDRVCONF 19 228 #define MODREMMAJBIND 20 229 #define MODDEVT2INSTANCE 21 230 #define MODGETDEVFSPATH_LEN 22 231 #define MODGETDEVFSPATH 23 232 #define MODDEVID2PATHS 24 233 #define MODSETDEVPOLICY 26 234 #define MODGETDEVPOLICY 27 235 #define MODALLOCPRIV 28 236 #define MODGETDEVPOLICYBYNAME 29 237 #define MODLOADMINORPERM 31 238 #define MODADDMINORPERM 32 239 #define MODREMMINORPERM 33 240 #define MODREMDRVCLEANUP 34 241 242 /* 243 * sub cmds for MODEVENTS 244 */ 245 #define MODEVENTS_FLUSH 0 246 #define MODEVENTS_FLUSH_DUMP 1 247 #define MODEVENTS_SET_DOOR_UPCALL_FILENAME 2 248 #define MODEVENTS_GETDATA 3 249 #define MODEVENTS_FREEDATA 4 250 #define MODEVENTS_POST_EVENT 5 251 #define MODEVENTS_REGISTER_EVENT 6 252 253 /* 254 * Data structure passed to modconfig command in kernel to build devfs tree 255 */ 256 257 struct aliases { 258 struct aliases *a_next; 259 char *a_name; 260 int a_len; 261 }; 262 263 #define MAXMODCONFNAME 256 264 265 struct modconfig { 266 char drvname[MAXMODCONFNAME]; 267 char drvclass[MAXMODCONFNAME]; 268 int major; 269 int num_aliases; 270 struct aliases *ap; 271 }; 272 273 #if defined(_SYSCALL32) 274 275 struct aliases32 { 276 caddr32_t a_next; 277 caddr32_t a_name; 278 int32_t a_len; 279 }; 280 281 struct modconfig32 { 282 char drvname[MAXMODCONFNAME]; 283 char drvclass[MAXMODCONFNAME]; 284 int32_t major; 285 int32_t num_aliases; 286 caddr32_t ap; 287 }; 288 289 #endif /* _SYSCALL32 */ 290 291 /* 292 * Max module path length 293 */ 294 #define MOD_MAXPATH 256 295 296 /* 297 * Default search path for modules ADDITIONAL to the directory 298 * where the kernel components we booted from are. 299 * 300 * Most often, this will be "/platform/{platform}/kernel /kernel /usr/kernel", 301 * but we don't wire it down here. 302 */ 303 #define MOD_DEFPATH "/kernel /usr/kernel" 304 305 /* 306 * Default file name extension for autoloading modules. 307 */ 308 #define MOD_DEFEXT "" 309 310 /* 311 * Parameters for modinfo 312 */ 313 #define MODMAXNAMELEN 32 /* max module name length */ 314 #define MODMAXLINKINFOLEN 32 /* max link info length */ 315 316 /* 317 * Module specific information. 318 */ 319 struct modspecific_info { 320 char msi_linkinfo[MODMAXLINKINFOLEN]; /* name in linkage struct */ 321 int msi_p0; /* module specific information */ 322 }; 323 324 /* 325 * Structure returned by modctl with MODINFO command. 326 */ 327 #define MODMAXLINK 10 /* max linkages modinfo can handle */ 328 329 struct modinfo { 330 int mi_info; /* Flags for info wanted */ 331 int mi_state; /* Flags for module state */ 332 int mi_id; /* id of this loaded module */ 333 int mi_nextid; /* id of next module or -1 */ 334 caddr_t mi_base; /* virtual addr of text */ 335 size_t mi_size; /* size of module in bytes */ 336 int mi_rev; /* loadable modules rev */ 337 int mi_loadcnt; /* # of times loaded */ 338 char mi_name[MODMAXNAMELEN]; /* name of module */ 339 struct modspecific_info mi_msinfo[MODMAXLINK]; 340 /* mod specific info */ 341 }; 342 343 344 #if defined(_SYSCALL32) 345 346 #define MODMAXNAMELEN32 32 /* max module name length */ 347 #define MODMAXLINKINFOLEN32 32 /* max link info length */ 348 #define MODMAXLINK32 10 /* max linkages modinfo can handle */ 349 350 struct modspecific_info32 { 351 char msi_linkinfo[MODMAXLINKINFOLEN32]; /* name in linkage struct */ 352 int32_t msi_p0; /* module specific information */ 353 }; 354 355 struct modinfo32 { 356 int32_t mi_info; /* Flags for info wanted */ 357 int32_t mi_state; /* Flags for module state */ 358 int32_t mi_id; /* id of this loaded module */ 359 int32_t mi_nextid; /* id of next module or -1 */ 360 caddr32_t mi_base; /* virtual addr of text */ 361 uint32_t mi_size; /* size of module in bytes */ 362 int32_t mi_rev; /* loadable modules rev */ 363 int32_t mi_loadcnt; /* # of times loaded */ 364 char mi_name[MODMAXNAMELEN32]; /* name of module */ 365 struct modspecific_info32 mi_msinfo[MODMAXLINK32]; 366 /* mod specific info */ 367 }; 368 369 #endif /* _SYSCALL32 */ 370 371 /* Values for mi_info flags */ 372 #define MI_INFO_ONE 1 373 #define MI_INFO_ALL 2 374 #define MI_INFO_CNT 4 375 #ifdef _KERNEL 376 #define MI_INFO_LINKAGE 8 /* used internally to extract modlinkage */ 377 #endif 378 /* 379 * MI_INFO_NOBASE indicates caller does not need mi_base. Failure to use this 380 * flag may lead 32-bit apps to receive an EOVERFLOW error from modctl(MODINFO) 381 * when used with a 64-bit kernel. 382 */ 383 #define MI_INFO_NOBASE 16 384 385 /* Values for mi_state */ 386 #define MI_LOADED 1 387 #define MI_INSTALLED 2 388 389 /* 390 * Macros to vector to the appropriate module specific routine. 391 */ 392 #define MODL_INSTALL(MODL, MODLP) \ 393 (*(MODL)->misc_modops->modm_install)(MODL, MODLP) 394 #define MODL_REMOVE(MODL, MODLP) \ 395 (*(MODL)->misc_modops->modm_remove)(MODL, MODLP) 396 #define MODL_INFO(MODL, MODLP, P0) \ 397 (*(MODL)->misc_modops->modm_info)(MODL, MODLP, P0) 398 399 /* 400 * Definitions for stubs 401 */ 402 struct mod_stub_info { 403 uintptr_t mods_func_adr; 404 struct mod_modinfo *mods_modinfo; 405 uintptr_t mods_stub_adr; 406 int (*mods_errfcn)(); 407 int mods_flag; /* flags defined below */ 408 }; 409 410 /* 411 * Definitions for mods_flag. 412 */ 413 #define MODS_WEAK 0x01 /* weak stub (not loaded if called) */ 414 #define MODS_NOUNLOAD 0x02 /* module not unloadable (no _fini()) */ 415 #define MODS_INSTALLED 0x10 /* module installed */ 416 417 struct mod_modinfo { 418 char *modm_module_name; 419 struct modctl *mp; 420 struct mod_stub_info modm_stubs[1]; 421 }; 422 423 struct modctl_list { 424 struct modctl_list *modl_next; 425 struct modctl *modl_modp; 426 }; 427 428 /* 429 * Structure to manage a loadable module. 430 * Note: the module (mod_mp) structure's "text" and "text_size" information 431 * are replicated in the modctl structure so that mod_containing_pc() 432 * doesn't have to grab any locks (modctls are persistent; modules are not.) 433 */ 434 typedef struct modctl { 435 struct modctl *mod_next; /* &modules based list */ 436 struct modctl *mod_prev; 437 int mod_id; 438 void *mod_mp; 439 kthread_t *mod_inprogress_thread; 440 struct mod_modinfo *mod_modinfo; 441 struct modlinkage *mod_linkage; 442 char *mod_filename; 443 char *mod_modname; 444 445 char mod_busy; /* inprogress_thread has locked */ 446 char mod_want; /* someone waiting for unlock */ 447 char mod_prim; /* primary module */ 448 449 int mod_ref; /* ref count - from dependent or stub */ 450 451 char mod_loaded; /* module in memory */ 452 char mod_installed; /* post _init pre _fini */ 453 char mod_loadflags; 454 char mod_delay_unload; /* deferred unload */ 455 456 struct modctl_list *mod_requisites; /* mods this one depends on. */ 457 void *__unused; /* NOTE: reuse (same size) is OK, */ 458 /* deletion causes mdb.vs.core issues */ 459 int mod_loadcnt; /* number of times mod was loaded */ 460 int mod_nenabled; /* # of enabled DTrace probes in mod */ 461 char *mod_text; 462 size_t mod_text_size; 463 464 int mod_gencount; /* # times loaded/unloaded */ 465 struct modctl *mod_requisite_loading; /* mod circular dependency */ 466 } modctl_t; 467 468 /* 469 * mod_loadflags 470 */ 471 472 #define MOD_NOAUTOUNLOAD 0x1 /* Auto mod-unloader skips this mod */ 473 #define MOD_NONOTIFY 0x2 /* No krtld notifications on (un)load */ 474 #define MOD_NOUNLOAD 0x4 /* Assume EBUSY for all _fini's */ 475 476 #ifdef _KERNEL 477 478 #define MOD_BIND_HASHSIZE 64 479 #define MOD_BIND_HASHMASK (MOD_BIND_HASHSIZE-1) 480 481 typedef int modid_t; 482 483 /* 484 * global function and data declarations 485 */ 486 extern kmutex_t mod_lock; 487 488 extern char *systemfile; 489 extern char **syscallnames; 490 extern int moddebug; 491 492 /* 493 * this is the head of a doubly linked list. Only the next and prev 494 * pointers are used 495 */ 496 extern modctl_t modules; 497 498 extern int modload_qualified(const char *, 499 const char *, const char *, const char *, uint_t[], int); 500 501 extern void mod_setup(void); 502 extern int modload(char *, char *); 503 extern int modloadonly(char *, char *); 504 extern int modunload(int); 505 extern int mod_hold_stub(struct mod_stub_info *); 506 extern void modunload_disable(void); 507 extern void modunload_enable(void); 508 extern void modunload_begin(void); 509 extern void modunload_end(void); 510 extern int mod_remove_by_name(char *); 511 extern int mod_sysvar(const char *, const char *, u_longlong_t *); 512 extern int mod_sysctl(int, void *); 513 struct sysparam; 514 extern int mod_hold_by_modctl(modctl_t *, int); 515 #define MOD_WAIT_ONCE 0x01 516 #define MOD_WAIT_FOREVER 0x02 517 #define MOD_LOCK_HELD 0x04 518 #define MOD_LOCK_NOT_HELD 0x08 519 extern int mod_sysctl_type(int, int (*)(struct sysparam *, void *), 520 void *); 521 extern void mod_read_system_file(int); 522 extern void mod_release_stub(struct mod_stub_info *); 523 extern void mod_askparams(void); 524 extern void mod_uninstall_daemon(void); 525 extern void modreap(void); 526 extern modctl_t *mod_hold_by_id(modid_t); 527 extern modctl_t *mod_hold_by_name(const char *); 528 extern void mod_release_mod(modctl_t *); 529 extern uintptr_t modlookup(const char *, const char *); 530 extern uintptr_t modlookup_by_modctl(modctl_t *, const char *); 531 extern char *modgetsymname(uintptr_t, unsigned long *); 532 extern void mod_release_requisites(modctl_t *); 533 extern modctl_t *mod_load_requisite(modctl_t *, char *); 534 extern modctl_t *mod_find_by_filename(char *, char *); 535 extern uintptr_t modgetsymvalue(char *, int); 536 537 extern void mod_rele_dev_by_major(major_t); 538 extern struct dev_ops *mod_hold_dev_by_major(major_t); 539 extern struct dev_ops *mod_hold_dev_by_devi(dev_info_t *); 540 extern void mod_rele_dev_by_devi(dev_info_t *); 541 542 extern int make_devname(char *, major_t); 543 544 struct bind; 545 extern void make_aliases(struct bind **); 546 extern int read_binding_file(char *, struct bind **, 547 int (*line_parser)(char *, int, char *, struct bind **)); 548 extern void clear_binding_hash(struct bind **); 549 550 extern void read_class_file(void); 551 extern void setbootpath(char *); 552 extern void setbootfstype(char *); 553 554 extern int install_stubs_by_name(modctl_t *, char *); 555 extern void install_stubs(modctl_t *); 556 extern void uninstall_stubs(modctl_t *); 557 extern void reset_stubs(modctl_t *); 558 extern modctl_t *mod_getctl(struct modlinkage *); 559 extern major_t mod_name_to_major(char *); 560 extern modid_t mod_name_to_modid(char *); 561 extern char *mod_major_to_name(major_t); 562 extern void init_devnamesp(int); 563 extern void init_syscallnames(int); 564 565 extern char *mod_getsysname(int); 566 extern int mod_getsysnum(char *); 567 568 extern char *mod_containing_pc(caddr_t); 569 extern int mod_in_autounload(void); 570 extern char *mod_modname(struct modlinkage *); 571 572 extern int dev_minorperm(dev_info_t *, char *, mperm_t *); 573 574 /* 575 * Declarations used for dynamic linking support routines. Interfaces 576 * are marked with the pragma "unknown_control_flow" to prevent tail call 577 * optimization, so that implementations can reliably use caller() to 578 * determine initiating module. 579 */ 580 #define KRTLD_MODE_FIRST 0x0001 581 typedef struct __ddi_modhandle *ddi_modhandle_t; 582 extern ddi_modhandle_t ddi_modopen(const char *, 583 int, int *); 584 extern void *ddi_modsym(ddi_modhandle_t, 585 const char *, int *); 586 extern int ddi_modclose(ddi_modhandle_t); 587 #pragma unknown_control_flow(ddi_modopen, ddi_modsym, ddi_modclose) 588 589 /* 590 * Only the following are part of the DDI/DKI 591 */ 592 extern int _init(void); 593 extern int _fini(void); 594 extern int _info(struct modinfo *); 595 extern int mod_install(struct modlinkage *); 596 extern int mod_remove(struct modlinkage *); 597 extern int mod_info(struct modlinkage *, struct modinfo *); 598 599 #else /* _KERNEL */ 600 601 extern int modctl(int, ...); 602 603 #endif /* _KERNEL */ 604 605 /* 606 * bit definitions for moddebug. 607 */ 608 #define MODDEBUG_LOADMSG 0x80000000 /* print "[un]loading..." msg */ 609 #define MODDEBUG_ERRMSG 0x40000000 /* print detailed error msgs */ 610 #define MODDEBUG_LOADMSG2 0x20000000 /* print 2nd level msgs */ 611 #define MODDEBUG_FINI_EBUSY 0x00020000 /* pretend fini returns EBUSY */ 612 #define MODDEBUG_NOAUL_IPP 0x00010000 /* no Autounloading ipp mods */ 613 #define MODDEBUG_NOAUL_DACF 0x00008000 /* no Autounloading dacf mods */ 614 #define MODDEBUG_KEEPTEXT 0x00004000 /* keep text after unloading */ 615 #define MODDEBUG_NOAUL_DRV 0x00001000 /* no Autounloading Drivers */ 616 #define MODDEBUG_NOAUL_EXEC 0x00000800 /* no Autounloading Execs */ 617 #define MODDEBUG_NOAUL_FS 0x00000400 /* no Autounloading File sys */ 618 #define MODDEBUG_NOAUL_MISC 0x00000200 /* no Autounloading misc */ 619 #define MODDEBUG_NOAUL_SCHED 0x00000100 /* no Autounloading scheds */ 620 #define MODDEBUG_NOAUL_STR 0x00000080 /* no Autounloading streams */ 621 #define MODDEBUG_NOAUL_SYS 0x00000040 /* no Autounloading syscalls */ 622 #define MODDEBUG_NOCTF 0x00000020 /* do not load CTF debug data */ 623 #define MODDEBUG_NOAUTOUNLOAD 0x00000010 /* no autounloading at all */ 624 #define MODDEBUG_DDI_MOD 0x00000008 /* ddi_mod{open,sym,close} */ 625 #define MODDEBUG_MP_MATCH 0x00000004 /* dev_minorperm */ 626 #define MODDEBUG_MINORPERM 0x00000002 /* minor perm modctls */ 627 #define MODDEBUG_USERDEBUG 0x00000001 /* bpt after init_module() */ 628 629 #ifdef __cplusplus 630 } 631 #endif 632 633 #endif /* _SYS_MODCTL_H */ 634