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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_MODCTL_H
27 #define	_SYS_MODCTL_H
28 
29 /*
30  * loadable module support.
31  */
32 
33 #include <sys/zfs_context.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 struct modlmisc;
40 struct modlinkage;
41 
42 /*
43  * The following structure defines the operations used by modctl
44  * to load and unload modules.  Each supported loadable module type
45  * requires a set of mod_ops.
46  */
47 struct mod_ops {
48 	int	(*modm_install)(struct modlmisc *, struct modlinkage *);
49 	int	(*modm_remove)(struct modlmisc *, struct modlinkage *);
50 	int	(*modm_info)(void *, struct modlinkage *, int *);
51 };
52 
53 /*
54  * The defined set of mod_ops structures for each loadable module type
55  * Defined in modctl.c
56  */
57 extern struct mod_ops mod_brandops;
58 #if defined(__i386) || defined(__amd64)
59 extern struct mod_ops mod_cpuops;
60 #endif
61 extern struct mod_ops mod_cryptoops;
62 extern struct mod_ops mod_driverops;
63 extern struct mod_ops mod_execops;
64 extern struct mod_ops mod_fsops;
65 extern struct mod_ops mod_miscops;
66 extern struct mod_ops mod_schedops;
67 extern struct mod_ops mod_strmodops;
68 extern struct mod_ops mod_syscallops;
69 extern struct mod_ops mod_sockmodops;
70 #ifdef _SYSCALL32_IMPL
71 extern struct mod_ops mod_syscallops32;
72 #endif
73 extern struct mod_ops mod_dacfops;
74 extern struct mod_ops mod_ippops;
75 extern struct mod_ops mod_pcbeops;
76 extern struct mod_ops mod_devfsops;
77 extern struct mod_ops mod_kiconvops;
78 
79 /*
80  * Definitions for the module specific linkage structures.
81  * The first two fields are the same in all of the structures.
82  * The linkinfo is for informational purposes only and is returned by
83  * modctl with the MODINFO cmd.
84  */
85 
86 /* For cryptographic providers */
87 struct modlcrypto {
88 	struct mod_ops		*crypto_modops;
89 	char			*crypto_linkinfo;
90 };
91 
92 /* For misc */
93 struct modlmisc {
94 	struct mod_ops		*misc_modops;
95 	char			*misc_linkinfo;
96 };
97 
98 /*
99  * Revision number of loadable modules support.  This is the value
100  * that must be used in the modlinkage structure.
101  */
102 #define	MODREV_1		1
103 
104 /*
105  * The modlinkage structure is the structure that the module writer
106  * provides to the routines to install, remove, and stat a module.
107  * The ml_linkage element is an array of pointers to linkage structures.
108  * For most modules there is only one linkage structure.  We allocate
109  * enough space for 3 linkage structures which happens to be the most
110  * we have in any sun supplied module.  For those modules with more
111  * than 3 linkage structures (which is very unlikely), a modlinkage
112  * structure must be kmem_alloc'd in the module wrapper to be big enough
113  * for all of the linkage structures.
114  */
115 struct modlinkage {
116 	int		ml_rev;		/* rev of loadable modules system */
117 #ifdef _LP64
118 	void		*ml_linkage[7];	/* more space in 64-bit OS */
119 #else
120 	void		*ml_linkage[4];	/* NULL terminated list of */
121 					/* linkage structures */
122 #endif
123 };
124 
125 /*
126  * commands.  These are the commands supported by the modctl system call.
127  */
128 #define	MODLOAD			0
129 #define	MODUNLOAD		1
130 #define	MODINFO			2
131 #define	MODRESERVED		3
132 #define	MODSETMINIROOT		4
133 #define	MODADDMAJBIND		5
134 #define	MODGETPATH		6
135 #define	MODREADSYSBIND		7
136 #define	MODGETMAJBIND		8
137 #define	MODGETNAME		9
138 #define	MODSIZEOF_DEVID		10
139 #define	MODGETDEVID		11
140 #define	MODSIZEOF_MINORNAME	12
141 #define	MODGETMINORNAME		13
142 #define	MODGETPATHLEN		14
143 #define	MODEVENTS		15
144 #define	MODGETFBNAME		16
145 #define	MODREREADDACF		17
146 #define	MODLOADDRVCONF		18
147 #define	MODUNLOADDRVCONF	19
148 #define	MODREMMAJBIND		20
149 #define	MODDEVT2INSTANCE	21
150 #define	MODGETDEVFSPATH_LEN	22
151 #define	MODGETDEVFSPATH		23
152 #define	MODDEVID2PATHS		24
153 #define	MODSETDEVPOLICY		26
154 #define	MODGETDEVPOLICY		27
155 #define	MODALLOCPRIV		28
156 #define	MODGETDEVPOLICYBYNAME	29
157 #define	MODLOADMINORPERM	31
158 #define	MODADDMINORPERM		32
159 #define	MODREMMINORPERM		33
160 #define	MODREMDRVCLEANUP	34
161 #define	MODDEVEXISTS		35
162 #define	MODDEVREADDIR		36
163 #define	MODDEVNAME		37
164 #define	MODGETDEVFSPATH_MI_LEN	38
165 #define	MODGETDEVFSPATH_MI	39
166 #define	MODRETIRE		40
167 #define	MODUNRETIRE		41
168 #define	MODISRETIRED		42
169 #define	MODDEVEMPTYDIR		43
170 #define	MODREMDRVALIAS		44
171 
172 /*
173  * sub cmds for MODEVENTS
174  */
175 #define	MODEVENTS_FLUSH				0
176 #define	MODEVENTS_FLUSH_DUMP			1
177 #define	MODEVENTS_SET_DOOR_UPCALL_FILENAME	2
178 #define	MODEVENTS_GETDATA			3
179 #define	MODEVENTS_FREEDATA			4
180 #define	MODEVENTS_POST_EVENT			5
181 #define	MODEVENTS_REGISTER_EVENT		6
182 
183 /*
184  * devname subcmds for MODDEVNAME
185  */
186 #define	MODDEVNAME_LOOKUPDOOR	0
187 #define	MODDEVNAME_DEVFSADMNODE	1
188 #define	MODDEVNAME_NSMAPS	2
189 #define	MODDEVNAME_PROFILE	3
190 #define	MODDEVNAME_RECONFIG	4
191 #define	MODDEVNAME_SYSAVAIL	5
192 
193 
194 /*
195  * Data structure passed to modconfig command in kernel to build devfs tree
196  */
197 
198 struct aliases {
199 	struct aliases *a_next;
200 	char *a_name;
201 	int a_len;
202 };
203 
204 #define	MAXMODCONFNAME	256
205 
206 struct modconfig {
207 	char drvname[MAXMODCONFNAME];
208 	char drvclass[MAXMODCONFNAME];
209 	int major;
210 	int flags;
211 	int num_aliases;
212 	struct aliases *ap;
213 };
214 
215 #if defined(_SYSCALL32)
216 
217 struct aliases32 {
218 	caddr32_t a_next;
219 	caddr32_t a_name;
220 	int32_t a_len;
221 };
222 
223 struct modconfig32 {
224 	char drvname[MAXMODCONFNAME];
225 	char drvclass[MAXMODCONFNAME];
226 	int32_t major;
227 	int32_t flags;
228 	int32_t num_aliases;
229 	caddr32_t ap;
230 };
231 
232 #endif /* _SYSCALL32 */
233 
234 /* flags for modconfig */
235 #define	MOD_UNBIND_OVERRIDE	0x01		/* fail unbind if in use */
236 
237 /*
238  * Max module path length
239  */
240 #define	MOD_MAXPATH	256
241 
242 /*
243  * Default search path for modules ADDITIONAL to the directory
244  * where the kernel components we booted from are.
245  *
246  * Most often, this will be "/platform/{platform}/kernel /kernel /usr/kernel",
247  * but we don't wire it down here.
248  */
249 #define	MOD_DEFPATH	"/kernel /usr/kernel"
250 
251 /*
252  * Default file name extension for autoloading modules.
253  */
254 #define	MOD_DEFEXT	""
255 
256 /*
257  * Parameters for modinfo
258  */
259 #define	MODMAXNAMELEN 32		/* max module name length */
260 #define	MODMAXLINKINFOLEN 32		/* max link info length */
261 
262 /*
263  * Module specific information.
264  */
265 struct modspecific_info {
266 	char	msi_linkinfo[MODMAXLINKINFOLEN]; /* name in linkage struct */
267 	int	msi_p0;			/* module specific information */
268 };
269 
270 /*
271  * Structure returned by modctl with MODINFO command.
272  */
273 #define	MODMAXLINK 10			/* max linkages modinfo can handle */
274 
275 struct modinfo {
276 	int		   mi_info;		/* Flags for info wanted */
277 	int		   mi_state;		/* Flags for module state */
278 	int		   mi_id;		/* id of this loaded module */
279 	int		   mi_nextid;		/* id of next module or -1 */
280 	caddr_t		   mi_base;		/* virtual addr of text */
281 	size_t		   mi_size;		/* size of module in bytes */
282 	int		   mi_rev;		/* loadable modules rev */
283 	int		   mi_loadcnt;		/* # of times loaded */
284 	char		   mi_name[MODMAXNAMELEN]; /* name of module */
285 	struct modspecific_info mi_msinfo[MODMAXLINK];
286 						/* mod specific info */
287 };
288 
289 
290 #if defined(_SYSCALL32)
291 
292 #define	MODMAXNAMELEN32 32		/* max module name length */
293 #define	MODMAXLINKINFOLEN32 32		/* max link info length */
294 #define	MODMAXLINK32 10			/* max linkages modinfo can handle */
295 
296 struct modspecific_info32 {
297 	char	msi_linkinfo[MODMAXLINKINFOLEN32]; /* name in linkage struct */
298 	int32_t	msi_p0;			/* module specific information */
299 };
300 
301 struct modinfo32 {
302 	int32_t		   mi_info;		/* Flags for info wanted */
303 	int32_t		   mi_state;		/* Flags for module state */
304 	int32_t		   mi_id;		/* id of this loaded module */
305 	int32_t		   mi_nextid;		/* id of next module or -1 */
306 	caddr32_t	   mi_base;		/* virtual addr of text */
307 	uint32_t	   mi_size;		/* size of module in bytes */
308 	int32_t		   mi_rev;		/* loadable modules rev */
309 	int32_t		   mi_loadcnt;		/* # of times loaded */
310 	char		   mi_name[MODMAXNAMELEN32]; /* name of module */
311 	struct modspecific_info32 mi_msinfo[MODMAXLINK32];
312 						/* mod specific info */
313 };
314 
315 #endif /* _SYSCALL32 */
316 
317 /* Values for mi_info flags */
318 #define	MI_INFO_ONE	1
319 #define	MI_INFO_ALL	2
320 #define	MI_INFO_CNT	4
321 #define	MI_INFO_LINKAGE	8	/* used internally to extract modlinkage */
322 /*
323  * MI_INFO_NOBASE indicates caller does not need mi_base. Failure to use this
324  * flag may lead 32-bit apps to receive an EOVERFLOW error from modctl(MODINFO)
325  * when used with a 64-bit kernel.
326  */
327 #define	MI_INFO_NOBASE	16
328 
329 /* Values for mi_state */
330 #define	MI_LOADED	1
331 #define	MI_INSTALLED	2
332 
333 /*
334  * Macros to vector to the appropriate module specific routine.
335  */
336 #define	MODL_INSTALL(MODL, MODLP) \
337 	(*(MODL)->misc_modops->modm_install)(MODL, MODLP)
338 #define	MODL_REMOVE(MODL, MODLP) \
339 	(*(MODL)->misc_modops->modm_remove)(MODL, MODLP)
340 #define	MODL_INFO(MODL, MODLP, P0) \
341 	(*(MODL)->misc_modops->modm_info)(MODL, MODLP, P0)
342 
343 /*
344  * Definitions for stubs
345  */
346 struct mod_stub_info {
347 	uintptr_t mods_func_adr;
348 	struct mod_modinfo *mods_modinfo;
349 	uintptr_t mods_stub_adr;
350 	int (*mods_errfcn)(void);
351 	int mods_flag;			/* flags defined below */
352 };
353 
354 /*
355  * Definitions for mods_flag.
356  */
357 #define	MODS_WEAK	0x01		/* weak stub (not loaded if called) */
358 #define	MODS_NOUNLOAD	0x02		/* module not unloadable (no _fini()) */
359 #define	MODS_INSTALLED	0x10		/* module installed */
360 
361 struct mod_modinfo {
362 	char *modm_module_name;
363 	struct modctl *mp;
364 	struct mod_stub_info modm_stubs[1];
365 };
366 
367 struct modctl_list {
368 	struct modctl_list *modl_next;
369 	struct modctl *modl_modp;
370 };
371 
372 /*
373  * Structure to manage a loadable module.
374  * Note: the module (mod_mp) structure's "text" and "text_size" information
375  * are replicated in the modctl structure so that mod_containing_pc()
376  * doesn't have to grab any locks (modctls are persistent; modules are not.)
377  */
378 typedef struct modctl {
379 	struct modctl	*mod_next;	/* &modules based list */
380 	struct modctl	*mod_prev;
381 	int		mod_id;
382 	void		*mod_mp;
383 	kthread_t	*mod_inprogress_thread;
384 	struct mod_modinfo *mod_modinfo;
385 	struct modlinkage *mod_linkage;
386 	char		*mod_filename;
387 	char		*mod_modname;
388 
389 	char		mod_busy;	/* inprogress_thread has locked */
390 	char		mod_want;	/* someone waiting for unlock */
391 	char		mod_prim;	/* primary module */
392 
393 	int		mod_ref;	/* ref count - from dependent or stub */
394 
395 	char		mod_loaded;	/* module in memory */
396 	char		mod_installed;	/* post _init pre _fini */
397 	char		mod_loadflags;
398 	char		mod_delay_unload;	/* deferred unload */
399 
400 	struct modctl_list *mod_requisites;	/* mods this one depends on. */
401 	void		*____unused;	/* NOTE: reuse (same size) is OK, */
402 					/* deletion causes mdb.vs.core issues */
403 	int		mod_loadcnt;	/* number of times mod was loaded */
404 	int		mod_nenabled;	/* # of enabled DTrace probes in mod */
405 	char		*mod_text;
406 	size_t		mod_text_size;
407 
408 	int		mod_gencount;	/* # times loaded/unloaded */
409 	struct modctl	*mod_requisite_loading;	/* mod circular dependency */
410 } modctl_t;
411 
412 /*
413  * mod_loadflags
414  */
415 
416 #define	MOD_NOAUTOUNLOAD	0x1	/* Auto mod-unloader skips this mod */
417 #define	MOD_NONOTIFY		0x2	/* No krtld notifications on (un)load */
418 #define	MOD_NOUNLOAD		0x4	/* Assume EBUSY for all _fini's */
419 
420 #define	MOD_BIND_HASHSIZE	64
421 #define	MOD_BIND_HASHMASK	(MOD_BIND_HASHSIZE-1)
422 
423 typedef int modid_t;
424 
425 /*
426  * global function and data declarations
427  */
428 extern kmutex_t mod_lock;
429 
430 extern char *systemfile;
431 extern char **syscallnames;
432 extern int moddebug;
433 
434 /*
435  * this is the head of a doubly linked list.  Only the next and prev
436  * pointers are used
437  */
438 extern modctl_t modules;
439 
440 /*
441  * Only the following are part of the DDI/DKI
442  */
443 extern int	mod_install(struct modlinkage *);
444 extern int	mod_remove(struct modlinkage *);
445 extern int	mod_info(struct modlinkage *, struct modinfo *);
446 
447 /*
448  * bit definitions for moddebug.
449  */
450 #define	MODDEBUG_LOADMSG	0x80000000	/* print "[un]loading..." msg */
451 #define	MODDEBUG_ERRMSG		0x40000000	/* print detailed error msgs */
452 #define	MODDEBUG_LOADMSG2	0x20000000	/* print 2nd level msgs */
453 #define	MODDEBUG_RETIRE		0x10000000	/* print retire msgs */
454 #define	MODDEBUG_BINDING	0x00040000	/* driver/alias binding */
455 #define	MODDEBUG_FINI_EBUSY	0x00020000	/* pretend fini returns EBUSY */
456 #define	MODDEBUG_NOAUL_IPP	0x00010000	/* no Autounloading ipp mods */
457 #define	MODDEBUG_NOAUL_DACF	0x00008000	/* no Autounloading dacf mods */
458 #define	MODDEBUG_KEEPTEXT	0x00004000	/* keep text after unloading */
459 #define	MODDEBUG_NOAUL_DRV	0x00001000	/* no Autounloading Drivers */
460 #define	MODDEBUG_NOAUL_EXEC	0x00000800	/* no Autounloading Execs */
461 #define	MODDEBUG_NOAUL_FS	0x00000400	/* no Autounloading File sys */
462 #define	MODDEBUG_NOAUL_MISC	0x00000200	/* no Autounloading misc */
463 #define	MODDEBUG_NOAUL_SCHED	0x00000100	/* no Autounloading scheds */
464 #define	MODDEBUG_NOAUL_STR	0x00000080	/* no Autounloading streams */
465 #define	MODDEBUG_NOAUL_SYS	0x00000040	/* no Autounloading syscalls */
466 #define	MODDEBUG_NOCTF		0x00000020	/* do not load CTF debug data */
467 #define	MODDEBUG_NOAUTOUNLOAD	0x00000010	/* no autounloading at all */
468 #define	MODDEBUG_DDI_MOD	0x00000008	/* ddi_mod{open,sym,close} */
469 #define	MODDEBUG_MP_MATCH	0x00000004	/* dev_minorperm */
470 #define	MODDEBUG_MINORPERM	0x00000002	/* minor perm modctls */
471 #define	MODDEBUG_USERDEBUG	0x00000001	/* bpt after init_module() */
472 
473 #ifdef	__cplusplus
474 }
475 #endif
476 
477 #endif	/* _SYS_MODCTL_H */
478