xref: /titanic_44/usr/src/uts/common/sys/modctl.h (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2005 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	MODCLEANUP		30
224 #define	MODLOADMINORPERM	31
225 #define	MODADDMINORPERM		32
226 #define	MODREMMINORPERM		33
227 #define	MODREMDRVCLEANUP	34
228 
229 /*
230  * sub cmds for MODEVENTS
231  */
232 #define	MODEVENTS_FLUSH				0
233 #define	MODEVENTS_FLUSH_DUMP			1
234 #define	MODEVENTS_SET_DOOR_UPCALL_FILENAME	2
235 #define	MODEVENTS_GETDATA			3
236 #define	MODEVENTS_FREEDATA			4
237 #define	MODEVENTS_POST_EVENT			5
238 #define	MODEVENTS_REGISTER_EVENT		6
239 
240 /*
241  * Data structure passed to modconfig command in kernel to build devfs tree
242  */
243 
244 struct aliases {
245 	struct aliases *a_next;
246 	char *a_name;
247 	int a_len;
248 };
249 
250 #define	MAXMODCONFNAME	256
251 
252 struct modconfig {
253 	char drvname[MAXMODCONFNAME];
254 	char drvclass[MAXMODCONFNAME];
255 	int major;
256 	int num_aliases;
257 	struct aliases *ap;
258 };
259 
260 #if defined(_SYSCALL32)
261 
262 struct aliases32 {
263 	caddr32_t a_next;
264 	caddr32_t a_name;
265 	int32_t a_len;
266 };
267 
268 struct modconfig32 {
269 	char drvname[MAXMODCONFNAME];
270 	char drvclass[MAXMODCONFNAME];
271 	int32_t major;
272 	int32_t num_aliases;
273 	caddr32_t ap;
274 };
275 
276 #endif /* _SYSCALL32 */
277 
278 /*
279  * Max module path length
280  */
281 #define	MOD_MAXPATH	256
282 
283 /*
284  * Default search path for modules ADDITIONAL to the directory
285  * where the kernel components we booted from are.
286  *
287  * Most often, this will be "/platform/{platform}/kernel /kernel /usr/kernel",
288  * but we don't wire it down here.
289  */
290 #define	MOD_DEFPATH	"/kernel /usr/kernel"
291 
292 /*
293  * Default file name extension for autoloading modules.
294  */
295 #define	MOD_DEFEXT	""
296 
297 /*
298  * Parameters for modinfo
299  */
300 #define	MODMAXNAMELEN 32		/* max module name length */
301 #define	MODMAXLINKINFOLEN 32		/* max link info length */
302 
303 /*
304  * Module specific information.
305  */
306 struct modspecific_info {
307 	char	msi_linkinfo[MODMAXLINKINFOLEN]; /* name in linkage struct */
308 	int	msi_p0;			/* module specific information */
309 };
310 
311 /*
312  * Structure returned by modctl with MODINFO command.
313  */
314 #define	MODMAXLINK 10			/* max linkages modinfo can handle */
315 
316 struct modinfo {
317 	int		   mi_info;		/* Flags for info wanted */
318 	int		   mi_state;		/* Flags for module state */
319 	int		   mi_id;		/* id of this loaded module */
320 	int		   mi_nextid;		/* id of next module or -1 */
321 	caddr_t		   mi_base;		/* virtual addr of text */
322 	size_t		   mi_size;		/* size of module in bytes */
323 	int		   mi_rev;		/* loadable modules rev */
324 	int		   mi_loadcnt;		/* # of times loaded */
325 	char		   mi_name[MODMAXNAMELEN]; /* name of module */
326 	struct modspecific_info mi_msinfo[MODMAXLINK];
327 						/* mod specific info */
328 };
329 
330 
331 #if defined(_SYSCALL32)
332 
333 #define	MODMAXNAMELEN32 32		/* max module name length */
334 #define	MODMAXLINKINFOLEN32 32		/* max link info length */
335 #define	MODMAXLINK32 10			/* max linkages modinfo can handle */
336 
337 struct modspecific_info32 {
338 	char	msi_linkinfo[MODMAXLINKINFOLEN32]; /* name in linkage struct */
339 	int32_t	msi_p0;			/* module specific information */
340 };
341 
342 struct modinfo32 {
343 	int32_t		   mi_info;		/* Flags for info wanted */
344 	int32_t		   mi_state;		/* Flags for module state */
345 	int32_t		   mi_id;		/* id of this loaded module */
346 	int32_t		   mi_nextid;		/* id of next module or -1 */
347 	caddr32_t	   mi_base;		/* virtual addr of text */
348 	uint32_t	   mi_size;		/* size of module in bytes */
349 	int32_t		   mi_rev;		/* loadable modules rev */
350 	int32_t		   mi_loadcnt;		/* # of times loaded */
351 	char		   mi_name[MODMAXNAMELEN32]; /* name of module */
352 	struct modspecific_info32 mi_msinfo[MODMAXLINK32];
353 						/* mod specific info */
354 };
355 
356 #endif /* _SYSCALL32 */
357 
358 /* Values for mi_info flags */
359 #define	MI_INFO_ONE	1
360 #define	MI_INFO_ALL	2
361 #define	MI_INFO_CNT	4
362 #ifdef _KERNEL
363 #define	MI_INFO_LINKAGE	8	/* used internally to extract modlinkage */
364 #endif
365 /*
366  * MI_INFO_NOBASE indicates caller does not need mi_base. Failure to use this
367  * flag may lead 32-bit apps to receive an EOVERFLOW error from modctl(MODINFO)
368  * when used with a 64-bit kernel.
369  */
370 #define	MI_INFO_NOBASE	16
371 
372 /* Values for mi_state */
373 #define	MI_LOADED	1
374 #define	MI_INSTALLED	2
375 
376 /*
377  * Macros to vector to the appropriate module specific routine.
378  */
379 #define	MODL_INSTALL(MODL, MODLP) \
380 	(*(MODL)->misc_modops->modm_install)(MODL, MODLP)
381 #define	MODL_REMOVE(MODL, MODLP) \
382 	(*(MODL)->misc_modops->modm_remove)(MODL, MODLP)
383 #define	MODL_INFO(MODL, MODLP, P0) \
384 	(*(MODL)->misc_modops->modm_info)(MODL, MODLP, P0)
385 
386 /*
387  * Definitions for stubs
388  */
389 struct mod_stub_info {
390 	uintptr_t mods_func_adr;
391 	struct mod_modinfo *mods_modinfo;
392 	uintptr_t mods_stub_adr;
393 	int (*mods_errfcn)();
394 	int mods_flag;			/* flags defined below */
395 };
396 
397 /*
398  * Definitions for mods_flag.
399  */
400 #define	MODS_WEAK	0x01		/* weak stub (not loaded if called) */
401 #define	MODS_NOUNLOAD	0x02		/* module not unloadable (no _fini()) */
402 #define	MODS_INSTALLED	0x10		/* module installed */
403 
404 struct mod_modinfo {
405 	char *modm_module_name;
406 	struct modctl *mp;
407 	struct mod_stub_info modm_stubs[1];
408 };
409 
410 struct modctl_list {
411 	struct modctl_list *modl_next;
412 	struct modctl *modl_modp;
413 };
414 
415 /*
416  * Structure to manage a loadable module.
417  * Note: the module (mod_mp) structure's "text" and "text_size" information
418  * are replicated in the modctl structure so that mod_containing_pc()
419  * doesn't have to grab any locks (modctls are persistent; modules are not.)
420  */
421 struct modctl {
422 	struct modctl	*mod_next;	/* &modules based list */
423 	struct modctl	*mod_prev;
424 	int		mod_id;
425 	void		*mod_mp;
426 	kthread_t	*mod_inprogress_thread;
427 	struct mod_modinfo *mod_modinfo;
428 	struct modlinkage *mod_linkage;
429 	char		*mod_filename;
430 	char		*mod_modname;
431 
432 	char		mod_busy;	/* inprogress_thread has locked */
433 	char		mod_want;	/* someone waiting for unlock */
434 	char		mod_prim;	/* primary module */
435 
436 	int		mod_ref;	/* ref count - from dependent or stub */
437 
438 	char		mod_loaded;	/* module in memory */
439 	char		mod_installed;	/* post _init pre _fini */
440 	char		mod_loadflags;
441 	char		mod_delay_unload;	/* deferred unload */
442 
443 	struct modctl_list *mod_requisites;	/* mods this one depends on. */
444 	void		*__unused;	/* NOTE: reuse (same size) is OK, */
445 					/* deletion causes mdb.vs.core issues */
446 	int		mod_loadcnt;	/* number of times mod was loaded */
447 	int		mod_nenabled;	/* # of enabled DTrace probes in mod */
448 	char		*mod_text;
449 	size_t		mod_text_size;
450 
451 	int		mod_gencount;	/* # times loaded/unloaded */
452 	struct modctl	*mod_requisite_loading;	/* mod circular dependency */
453 };
454 
455 /*
456  * mod_loadflags
457  */
458 
459 #define	MOD_NOAUTOUNLOAD	0x1	/* Auto mod-unloader skips this mod */
460 #define	MOD_NONOTIFY		0x2	/* No krtld notifications on (un)load */
461 #define	MOD_NOUNLOAD		0x4	/* Assume EBUSY for all _fini's */
462 
463 #ifdef _KERNEL
464 
465 #define	MOD_BIND_HASHSIZE	64
466 #define	MOD_BIND_HASHMASK	(MOD_BIND_HASHSIZE-1)
467 
468 typedef int modid_t;
469 
470 /*
471  * global function and data declarations
472  */
473 extern kmutex_t mod_lock;
474 
475 extern char *systemfile;
476 extern char **syscallnames;
477 extern int moddebug;
478 
479 /*
480  * this is the head of a doubly linked list.  Only the next and prev
481  * pointers are used
482  */
483 extern struct modctl modules;
484 
485 extern void	mod_setup(void);
486 extern int	modload(char *, char *);
487 extern int	modloadonly(char *, char *);
488 extern int	modunload(int);
489 extern int	mod_hold_stub(struct mod_stub_info *);
490 extern void	modunload_disable(void);
491 extern void	modunload_enable(void);
492 extern int	mod_remove_by_name(char *);
493 extern int	mod_sysvar(const char *, const char *, u_longlong_t *);
494 extern int	mod_sysctl(int, void *);
495 struct sysparam;
496 extern int	mod_hold_by_modctl(struct modctl *, int);
497 #define		MOD_WAIT_ONCE		0x01
498 #define		MOD_WAIT_FOREVER	0x02
499 #define		MOD_LOCK_HELD		0x04
500 #define		MOD_LOCK_NOT_HELD	0x08
501 extern int	mod_sysctl_type(int, int (*)(struct sysparam *, void *),
502     void *);
503 extern void	mod_read_system_file(int);
504 extern void	mod_release_stub(struct mod_stub_info *);
505 extern void	mod_askparams(void);
506 extern void	mod_uninstall_daemon(void);
507 extern void	modreap(void);
508 extern struct modctl *mod_hold_by_name(char *);
509 extern void	mod_release_mod(struct modctl *);
510 extern uintptr_t	modlookup(char *, char *);
511 extern char	*modgetsymname(uintptr_t, unsigned long *);
512 extern void	mod_release_requisites(struct modctl *);
513 extern struct modctl *mod_load_requisite(struct modctl *, char *);
514 extern struct modctl *mod_find_by_filename(char *, char *);
515 extern uintptr_t	modgetsymvalue(char *, int);
516 
517 extern void	mod_rele_dev_by_major(major_t);
518 extern struct dev_ops *mod_hold_dev_by_major(major_t);
519 extern struct dev_ops *mod_hold_dev_by_devi(dev_info_t *);
520 extern void	mod_rele_dev_by_devi(dev_info_t *);
521 
522 extern int make_devname(char *, major_t);
523 
524 struct bind;
525 extern void make_aliases(struct bind **);
526 extern int read_binding_file(char *, struct bind **,
527     int (*line_parser)(char *, int, char *, struct bind **));
528 extern void clear_binding_hash(struct bind **);
529 
530 extern void read_class_file(void);
531 extern void setbootpath(char *);
532 extern void setbootfstype(char *);
533 
534 extern int install_stubs_by_name(struct modctl *, char *);
535 extern void install_stubs(struct modctl *);
536 extern void uninstall_stubs(struct modctl *);
537 extern void reset_stubs(struct modctl *);
538 extern struct modctl *mod_getctl(struct modlinkage *);
539 extern major_t mod_name_to_major(char *);
540 extern modid_t mod_name_to_modid(char *);
541 extern char *mod_major_to_name(major_t);
542 extern void init_devnamesp(int);
543 extern void init_syscallnames(int);
544 
545 extern char *mod_getsysname(int);
546 extern int mod_getsysnum(char *);
547 
548 extern char *mod_containing_pc(caddr_t);
549 extern int mod_in_autounload(void);
550 extern char	*mod_modname(struct modlinkage *);
551 
552 extern int dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp);
553 
554 /*
555  * Declarations used for dynamic linking support routines.  Interfaces
556  * are marked with the pragma "unknown_control_flow" to prevent tail call
557  * optimization, so that implementations can reliably use caller() to
558  * determine initiating module.
559  */
560 #define	KRTLD_MODE_FIRST	0x0001
561 typedef	struct __ddi_modhandle	*ddi_modhandle_t;
562 extern ddi_modhandle_t		ddi_modopen(const char *,
563 				    int, int *);
564 extern void			*ddi_modsym(ddi_modhandle_t,
565 				    const char *, int *);
566 extern int			ddi_modclose(ddi_modhandle_t);
567 #pragma	unknown_control_flow(ddi_modopen, ddi_modsym, ddi_modclose)
568 
569 /*
570  * Only the following are part of the DDI/DKI
571  */
572 extern int	_init(void);
573 extern int	_fini(void);
574 extern int	_info(struct modinfo *);
575 extern int	mod_install(struct modlinkage *);
576 extern int	mod_remove(struct modlinkage *);
577 extern int	mod_info(struct modlinkage *, struct modinfo *);
578 
579 #else	/* _KERNEL */
580 
581 extern int modctl(int, ...);
582 
583 #endif	/* _KERNEL */
584 
585 /*
586  * bit definitions for moddebug.
587  */
588 #define	MODDEBUG_LOADMSG	0x80000000	/* print "[un]loading..." msg */
589 #define	MODDEBUG_ERRMSG		0x40000000	/* print detailed error msgs */
590 #define	MODDEBUG_LOADMSG2	0x20000000	/* print 2nd level msgs */
591 #define	MODDEBUG_FINI_EBUSY	0x00020000	/* pretend fini returns EBUSY */
592 #define	MODDEBUG_NOAUL_IPP	0x00010000	/* no Autounloading ipp mods */
593 #define	MODDEBUG_NOAUL_DACF	0x00008000	/* no Autounloading dacf mods */
594 #define	MODDEBUG_KEEPTEXT	0x00004000	/* keep text after unloading */
595 #define	MODDEBUG_NOAUL_DRV	0x00001000	/* no Autounloading Drivers */
596 #define	MODDEBUG_NOAUL_EXEC	0x00000800	/* no Autounloading Execs */
597 #define	MODDEBUG_NOAUL_FS	0x00000400	/* no Autounloading File sys */
598 #define	MODDEBUG_NOAUL_MISC	0x00000200	/* no Autounloading misc */
599 #define	MODDEBUG_NOAUL_SCHED	0x00000100	/* no Autounloading scheds */
600 #define	MODDEBUG_NOAUL_STR	0x00000080	/* no Autounloading streams */
601 #define	MODDEBUG_NOAUL_SYS	0x00000040	/* no Autounloading syscalls */
602 #define	MODDEBUG_NOCTF		0x00000020	/* do not load CTF debug data */
603 #define	MODDEBUG_NOAUTOUNLOAD	0x00000010	/* no autounloading at all */
604 #define	MODDEBUG_DDI_MOD	0x00000008	/* ddi_mod{open,sym,close} */
605 #define	MODDEBUG_MP_MATCH	0x00000004	/* dev_minorperm */
606 #define	MODDEBUG_MINORPERM	0x00000002	/* minor perm modctls */
607 #define	MODDEBUG_USERDEBUG	0x00000001	/* bpt after init_module() */
608 
609 #ifdef	__cplusplus
610 }
611 #endif
612 
613 #endif	/* _SYS_MODCTL_H */
614