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