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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 * loadable module support.
33 #include <sys/types.h>
34 #include <sys/ioccom.h>
35 #include <sys/nexusdefs.h>
36 #include <sys/thread.h>
37 #include <sys/t_lock.h>
38 #include <sys/dditypes.h>
39 #include <sys/hwconf.h>
46 * The following structure defines the operations used by modctl
47 * to load and unload modules. Each supported loadable module type
48 * requires a set of mod_ops.
51 int (*modm_install
)(); /* install module in kernel */
52 int (*modm_remove
)(); /* remove from kernel */
53 int (*modm_info
)(); /* module info */
59 * The defined set of mod_ops structures for each loadable module type
62 extern struct mod_ops mod_brandops
;
63 #if defined(__i386) || defined(__amd64)
64 extern struct mod_ops mod_cpuops
;
66 extern struct mod_ops mod_cryptoops
;
67 extern struct mod_ops mod_driverops
;
68 extern struct mod_ops mod_execops
;
69 extern struct mod_ops mod_fsops
;
70 extern struct mod_ops mod_miscops
;
71 extern struct mod_ops mod_schedops
;
72 extern struct mod_ops mod_strmodops
;
73 extern struct mod_ops mod_syscallops
;
74 extern struct mod_ops mod_sockmodops
;
75 #ifdef _SYSCALL32_IMPL
76 extern struct mod_ops mod_syscallops32
;
78 extern struct mod_ops mod_dacfops
;
79 extern struct mod_ops mod_ippops
;
80 extern struct mod_ops mod_pcbeops
;
81 extern struct mod_ops mod_devfsops
;
82 extern struct mod_ops mod_kiconvops
;
87 * Definitions for the module specific linkage structures.
88 * The first two fields are the same in all of the structures.
89 * The linkinfo is for informational purposes only and is returned by
90 * modctl with the MODINFO cmd.
95 struct mod_ops
*drv_modops
;
97 struct dev_ops
*drv_dev_ops
;
100 /* For system calls */
102 struct mod_ops
*sys_modops
;
104 struct sysent
*sys_sysent
;
107 /* For filesystems */
109 struct mod_ops
*fs_modops
;
111 struct vfsdef_v5
*fs_vfsdef
; /* version may actually vary */
114 #if defined(__i386) || defined(__amd64)
117 /* For CPU modules */
119 struct mod_ops
*cpu_modops
;
121 struct cmi_ops
*cpu_cmiops
;
125 /* For cryptographic providers */
127 struct mod_ops
*crypto_modops
;
128 char *crypto_linkinfo
;
133 struct mod_ops
*misc_modops
;
139 struct mod_ops
*ipp_modops
;
141 struct ipp_ops
*ipp_ops
;
144 /* For Streams Modules. */
146 struct mod_ops
*strmod_modops
;
147 char *strmod_linkinfo
;
148 struct fmodsw
*strmod_fmodsw
;
151 /* For Scheduling classes */
153 struct mod_ops
*sched_modops
;
154 char *sched_linkinfo
;
155 struct sclass
*sched_class
;
158 /* For Exec file type (like ELF, ...) */
160 struct mod_ops
*exec_modops
;
162 struct execsw
*exec_execsw
;
165 /* For dacf modules */
167 struct mod_ops
*dacf_modops
;
169 struct dacfsw
*dacf_dacfsw
;
172 /* For PCBE modules */
174 struct mod_ops
*pcbe_modops
;
176 struct __pcbe_ops
*pcbe_ops
;
179 /* For Brand modules */
181 struct mod_ops
*brand_modops
;
182 char *brand_linkinfo
;
183 struct brand
*brand_branddef
;
188 struct mod_ops
*dev_modops
;
190 struct devname_ops
*dev_ops
;
193 /* For socket Modules. */
195 struct mod_ops
*sockmod_modops
;
196 char *sockmod_linkinfo
;
197 struct smod_reg_s
*sockmod_reg_info
;
200 /* For kiconv modules */
202 struct mod_ops
*kiconv_modops
;
203 char *kiconv_linkinfo
;
204 struct kiconv_mod_info
*kiconv_moddef
;
208 * Revision number of loadable modules support. This is the value
209 * that must be used in the modlinkage structure.
214 * The modlinkage structure is the structure that the module writer
215 * provides to the routines to install, remove, and stat a module.
216 * The ml_linkage element is an array of pointers to linkage structures.
217 * For most modules there is only one linkage structure. We allocate
218 * enough space for 3 linkage structures which happens to be the most
219 * we have in any sun supplied module. For those modules with more
220 * than 3 linkage structures (which is very unlikely), a modlinkage
221 * structure must be kmem_alloc'd in the module wrapper to be big enough
222 * for all of the linkage structures.
225 int ml_rev
; /* rev of loadable modules system */
227 void *ml_linkage
[7]; /* more space in 64-bit OS */
229 void *ml_linkage
[4]; /* NULL terminated list of */
230 /* linkage structures */
235 * commands. These are the commands supported by the modctl system call.
240 #define MODRESERVED 3
241 #define MODSETMINIROOT 4
242 #define MODADDMAJBIND 5
244 #define MODREADSYSBIND 7
245 #define MODGETMAJBIND 8
247 #define MODSIZEOF_DEVID 10
248 #define MODGETDEVID 11
249 #define MODSIZEOF_MINORNAME 12
250 #define MODGETMINORNAME 13
251 #define MODGETPATHLEN 14
253 #define MODGETFBNAME 16
254 #define MODREREADDACF 17
255 #define MODLOADDRVCONF 18
256 #define MODUNLOADDRVCONF 19
257 #define MODREMMAJBIND 20
258 #define MODDEVT2INSTANCE 21
259 #define MODGETDEVFSPATH_LEN 22
260 #define MODGETDEVFSPATH 23
261 #define MODDEVID2PATHS 24
262 #define MODSETDEVPOLICY 26
263 #define MODGETDEVPOLICY 27
264 #define MODALLOCPRIV 28
265 #define MODGETDEVPOLICYBYNAME 29
266 #define MODLOADMINORPERM 31
267 #define MODADDMINORPERM 32
268 #define MODREMMINORPERM 33
269 #define MODREMDRVCLEANUP 34
270 #define MODDEVEXISTS 35
271 #define MODDEVREADDIR 36
272 #define MODDEVNAME 37
273 #define MODGETDEVFSPATH_MI_LEN 38
274 #define MODGETDEVFSPATH_MI 39
276 #define MODUNRETIRE 41
277 #define MODISRETIRED 42
278 #define MODDEVEMPTYDIR 43
279 #define MODREMDRVALIAS 44
282 * sub cmds for MODEVENTS
284 #define MODEVENTS_FLUSH 0
285 #define MODEVENTS_FLUSH_DUMP 1
286 #define MODEVENTS_SET_DOOR_UPCALL_FILENAME 2
287 #define MODEVENTS_GETDATA 3
288 #define MODEVENTS_FREEDATA 4
289 #define MODEVENTS_POST_EVENT 5
290 #define MODEVENTS_REGISTER_EVENT 6
293 * devname subcmds for MODDEVNAME
295 #define MODDEVNAME_LOOKUPDOOR 0
296 #define MODDEVNAME_DEVFSADMNODE 1
297 #define MODDEVNAME_NSMAPS 2
298 #define MODDEVNAME_PROFILE 3
299 #define MODDEVNAME_RECONFIG 4
300 #define MODDEVNAME_SYSAVAIL 5
304 * Data structure passed to modconfig command in kernel to build devfs tree
308 struct aliases
*a_next
;
313 #define MAXMODCONFNAME 256
316 char drvname
[MAXMODCONFNAME
];
317 char drvclass
[MAXMODCONFNAME
];
324 #if defined(_SYSCALL32)
333 char drvname
[MAXMODCONFNAME
];
334 char drvclass
[MAXMODCONFNAME
];
341 #endif /* _SYSCALL32 */
343 /* flags for modconfig */
344 #define MOD_UNBIND_OVERRIDE 0x01 /* fail unbind if in use */
347 * Max module path length
349 #define MOD_MAXPATH 256
352 * Default search path for modules ADDITIONAL to the directory
353 * where the kernel components we booted from are.
355 * Most often, this will be "/platform/{platform}/kernel /kernel /usr/kernel",
356 * but we don't wire it down here.
358 #define MOD_DEFPATH "/kernel /usr/kernel"
361 * Default file name extension for autoloading modules.
363 #define MOD_DEFEXT ""
366 * Parameters for modinfo
368 #define MODMAXNAMELEN 32 /* max module name length */
369 #define MODMAXLINKINFOLEN 32 /* max link info length */
372 * Module specific information.
374 struct modspecific_info
{
375 char msi_linkinfo
[MODMAXLINKINFOLEN
]; /* name in linkage struct */
376 int msi_p0
; /* module specific information */
380 * Structure returned by modctl with MODINFO command.
382 #define MODMAXLINK 10 /* max linkages modinfo can handle */
385 int mi_info
; /* Flags for info wanted */
386 int mi_state
; /* Flags for module state */
387 int mi_id
; /* id of this loaded module */
388 int mi_nextid
; /* id of next module or -1 */
389 caddr_t mi_base
; /* virtual addr of text */
390 size_t mi_size
; /* size of module in bytes */
391 int mi_rev
; /* loadable modules rev */
392 int mi_loadcnt
; /* # of times loaded */
393 char mi_name
[MODMAXNAMELEN
]; /* name of module */
394 struct modspecific_info mi_msinfo
[MODMAXLINK
];
395 /* mod specific info */
399 #if defined(_SYSCALL32)
401 #define MODMAXNAMELEN32 32 /* max module name length */
402 #define MODMAXLINKINFOLEN32 32 /* max link info length */
403 #define MODMAXLINK32 10 /* max linkages modinfo can handle */
405 struct modspecific_info32
{
406 char msi_linkinfo
[MODMAXLINKINFOLEN32
]; /* name in linkage struct */
407 int32_t msi_p0
; /* module specific information */
411 int32_t mi_info
; /* Flags for info wanted */
412 int32_t mi_state
; /* Flags for module state */
413 int32_t mi_id
; /* id of this loaded module */
414 int32_t mi_nextid
; /* id of next module or -1 */
415 caddr32_t mi_base
; /* virtual addr of text */
416 uint32_t mi_size
; /* size of module in bytes */
417 int32_t mi_rev
; /* loadable modules rev */
418 int32_t mi_loadcnt
; /* # of times loaded */
419 char mi_name
[MODMAXNAMELEN32
]; /* name of module */
420 struct modspecific_info32 mi_msinfo
[MODMAXLINK32
];
421 /* mod specific info */
424 #endif /* _SYSCALL32 */
426 /* Values for mi_info flags */
427 #define MI_INFO_ONE 1
428 #define MI_INFO_ALL 2
429 #define MI_INFO_CNT 4
431 #define MI_INFO_LINKAGE 8 /* used internally to extract modlinkage */
434 * MI_INFO_NOBASE indicates caller does not need mi_base. Failure to use this
435 * flag may lead 32-bit apps to receive an EOVERFLOW error from modctl(MODINFO)
436 * when used with a 64-bit kernel.
438 #define MI_INFO_NOBASE 16
440 /* Values for mi_state */
442 #define MI_INSTALLED 2
445 * Macros to vector to the appropriate module specific routine.
447 #define MODL_INSTALL(MODL, MODLP) \
448 (*(MODL)->misc_modops->modm_install)(MODL, MODLP)
449 #define MODL_REMOVE(MODL, MODLP) \
450 (*(MODL)->misc_modops->modm_remove)(MODL, MODLP)
451 #define MODL_INFO(MODL, MODLP, P0) \
452 (*(MODL)->misc_modops->modm_info)(MODL, MODLP, P0)
455 * Definitions for stubs
457 struct mod_stub_info
{
458 uintptr_t mods_func_adr
;
459 struct mod_modinfo
*mods_modinfo
;
460 uintptr_t mods_stub_adr
;
461 int (*mods_errfcn
)();
462 int mods_flag
; /* flags defined below */
466 * Definitions for mods_flag.
468 #define MODS_WEAK 0x01 /* weak stub (not loaded if called) */
469 #define MODS_NOUNLOAD 0x02 /* module not unloadable (no _fini()) */
470 #define MODS_INSTALLED 0x10 /* module installed */
473 char *modm_module_name
;
475 struct mod_stub_info modm_stubs
[1];
479 struct modctl_list
*modl_next
;
480 struct modctl
*modl_modp
;
484 * Structure to manage a loadable module.
485 * Note: the module (mod_mp) structure's "text" and "text_size" information
486 * are replicated in the modctl structure so that mod_containing_pc()
487 * doesn't have to grab any locks (modctls are persistent; modules are not.)
489 typedef struct modctl
{
490 struct modctl
*mod_next
; /* &modules based list */
491 struct modctl
*mod_prev
;
494 kthread_t
*mod_inprogress_thread
;
495 struct mod_modinfo
*mod_modinfo
;
496 struct modlinkage
*mod_linkage
;
500 char mod_busy
; /* inprogress_thread has locked */
501 char mod_want
; /* someone waiting for unlock */
502 char mod_prim
; /* primary module */
504 int mod_ref
; /* ref count - from dependent or stub */
506 char mod_loaded
; /* module in memory */
507 char mod_installed
; /* post _init pre _fini */
509 char mod_delay_unload
; /* deferred unload */
511 struct modctl_list
*mod_requisites
; /* mods this one depends on. */
512 void *__unused
; /* NOTE: reuse (same size) is OK, */
513 /* deletion causes mdb.vs.core issues */
514 int mod_loadcnt
; /* number of times mod was loaded */
515 int mod_nenabled
; /* # of enabled DTrace probes in mod */
517 size_t mod_text_size
;
519 int mod_gencount
; /* # times loaded/unloaded */
520 struct modctl
*mod_requisite_loading
; /* mod circular dependency */
527 #define MOD_NOAUTOUNLOAD 0x1 /* Auto mod-unloader skips this mod */
528 #define MOD_NONOTIFY 0x2 /* No krtld notifications on (un)load */
529 #define MOD_NOUNLOAD 0x4 /* Assume EBUSY for all _fini's */
534 #define MOD_BIND_HASHSIZE 64
535 #define MOD_BIND_HASHMASK (MOD_BIND_HASHSIZE-1)
540 * global function and data declarations
542 extern kmutex_t mod_lock
;
544 extern char *systemfile
;
545 extern char **syscallnames
;
549 * this is the head of a doubly linked list. Only the next and prev
552 extern modctl_t modules
;
554 extern int modload_qualified(const char *,
555 const char *, const char *, const char *, uint_t
[], int, int *);
557 extern void mod_setup(void);
558 extern int modload(const char *, const char *);
559 extern int modloadonly(const char *, const char *);
560 extern int modunload(int);
561 extern int mod_hold_stub(struct mod_stub_info
*);
562 extern void modunload_disable(void);
563 extern void modunload_enable(void);
564 extern void modunload_begin(void);
565 extern void modunload_end(void);
566 extern int mod_remove_by_name(char *);
567 extern int mod_sysvar(const char *, const char *, u_longlong_t
*);
568 extern int mod_sysctl(int, void *);
570 extern int mod_hold_by_modctl(modctl_t
*, int);
571 #define MOD_WAIT_ONCE 0x01
572 #define MOD_WAIT_FOREVER 0x02
573 #define MOD_LOCK_HELD 0x04
574 #define MOD_LOCK_NOT_HELD 0x08
575 extern int mod_sysctl_type(int, int (*)(struct sysparam
*, void *),
577 extern void mod_read_system_file(int);
578 extern void mod_release_stub(struct mod_stub_info
*);
579 extern void mod_askparams(void);
580 extern void mod_uninstall_daemon(void);
581 extern void modreap(void);
582 extern modctl_t
*mod_hold_by_id(modid_t
);
583 extern modctl_t
*mod_hold_by_name(const char *);
584 extern void mod_release_mod(modctl_t
*);
585 extern uintptr_t modlookup(const char *, const char *);
586 extern uintptr_t modlookup_by_modctl(modctl_t
*, const char *);
587 extern char *modgetsymname(uintptr_t, unsigned long *);
588 extern void mod_release_requisites(modctl_t
*);
589 extern modctl_t
*mod_load_requisite(modctl_t
*, char *);
590 extern modctl_t
*mod_find_by_filename(char *, char *);
591 extern uintptr_t modgetsymvalue(char *, int);
593 extern void mod_rele_dev_by_major(major_t
);
594 extern struct dev_ops
*mod_hold_dev_by_major(major_t
);
595 extern struct dev_ops
*mod_hold_dev_by_devi(dev_info_t
*);
596 extern void mod_rele_dev_by_devi(dev_info_t
*);
598 extern int make_devname(char *, major_t
);
599 extern int gmatch(const char *, const char *);
601 extern void make_aliases(struct bind
**);
602 extern int read_binding_file(char *, struct bind
**,
603 int (*line_parser
)(char *, int, char *, struct bind
**));
604 extern void clear_binding_hash(struct bind
**);
606 extern void read_class_file(void);
607 extern void setbootpath(char *);
608 extern void setbootfstype(char *);
610 extern int install_stubs_by_name(modctl_t
*, char *);
611 extern void install_stubs(modctl_t
*);
612 extern void uninstall_stubs(modctl_t
*);
613 extern void reset_stubs(modctl_t
*);
614 extern modctl_t
*mod_getctl(struct modlinkage
*);
615 extern major_t
mod_name_to_major(char *);
616 extern modid_t
mod_name_to_modid(char *);
617 extern char *mod_major_to_name(major_t
);
618 extern void init_devnamesp(int);
619 extern void init_syscallnames(int);
621 extern char *mod_getsysname(int);
622 extern int mod_getsysnum(char *);
624 extern char *mod_containing_pc(caddr_t
);
625 extern int mod_in_autounload(void);
626 extern const char *mod_modname(struct modlinkage
*);
628 extern int dev_minorperm(dev_info_t
*, char *, mperm_t
*);
629 extern void dev_devices_cleanup(void);
632 * Declarations used for dynamic linking support routines. Interfaces
633 * are marked with the pragma "unknown_control_flow" to prevent tail call
634 * optimization, so that implementations can reliably use caller() to
635 * determine initiating module.
637 #define KRTLD_MODE_FIRST 0x0001
638 typedef struct __ddi_modhandle
*ddi_modhandle_t
;
639 extern ddi_modhandle_t
ddi_modopen(const char *,
641 extern void *ddi_modsym(ddi_modhandle_t
,
642 const char *, int *);
643 extern int ddi_modclose(ddi_modhandle_t
);
644 #pragma unknown_control_flow(ddi_modopen, ddi_modsym, ddi_modclose)
647 * Only the following are part of the DDI/DKI
649 extern int _init(void);
650 extern int _fini(void);
651 extern int _info(struct modinfo
*);
652 extern int mod_install(struct modlinkage
*);
653 extern int mod_remove(struct modlinkage
*);
654 extern int mod_info(struct modlinkage
*, struct modinfo
*);
658 extern int modctl(int, ...);
663 * bit definitions for moddebug.
665 #define MODDEBUG_LOADMSG 0x80000000 /* print "[un]loading..." msg */
666 #define MODDEBUG_ERRMSG 0x40000000 /* print detailed error msgs */
667 #define MODDEBUG_LOADMSG2 0x20000000 /* print 2nd level msgs */
668 #define MODDEBUG_RETIRE 0x10000000 /* print retire msgs */
669 #define MODDEBUG_BINDING 0x00040000 /* driver/alias binding */
670 #define MODDEBUG_FINI_EBUSY 0x00020000 /* pretend fini returns EBUSY */
671 #define MODDEBUG_NOAUL_IPP 0x00010000 /* no Autounloading ipp mods */
672 #define MODDEBUG_NOAUL_DACF 0x00008000 /* no Autounloading dacf mods */
673 #define MODDEBUG_KEEPTEXT 0x00004000 /* keep text after unloading */
674 #define MODDEBUG_NOAUL_DRV 0x00001000 /* no Autounloading Drivers */
675 #define MODDEBUG_NOAUL_EXEC 0x00000800 /* no Autounloading Execs */
676 #define MODDEBUG_NOAUL_FS 0x00000400 /* no Autounloading File sys */
677 #define MODDEBUG_NOAUL_MISC 0x00000200 /* no Autounloading misc */
678 #define MODDEBUG_NOAUL_SCHED 0x00000100 /* no Autounloading scheds */
679 #define MODDEBUG_NOAUL_STR 0x00000080 /* no Autounloading streams */
680 #define MODDEBUG_NOAUL_SYS 0x00000040 /* no Autounloading syscalls */
681 #define MODDEBUG_NOCTF 0x00000020 /* do not load CTF debug data */
682 #define MODDEBUG_NOAUTOUNLOAD 0x00000010 /* no autounloading at all */
683 #define MODDEBUG_DDI_MOD 0x00000008 /* ddi_mod{open,sym,close} */
684 #define MODDEBUG_MP_MATCH 0x00000004 /* dev_minorperm */
685 #define MODDEBUG_MINORPERM 0x00000002 /* minor perm modctls */
686 #define MODDEBUG_USERDEBUG 0x00000001 /* bpt after init_module() */
692 #endif /* _SYS_MODCTL_H */