summaryrefslogtreecommitdiffstats
path: root/sys/sys/lkm.h
blob: a5947868121ff5033b359f3bb92b6f92dce73090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Header file used by loadable kernel modules and loadable kernel module
 * utilities.
 *
 * 23 Jan 93	Terry Lambert		Original
 *
 * Copyright (c) 1992 Terrence R. Lambert.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Terrence R. Lambert.
 * 4. The name Terrence R. Lambert may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _SYS_LKM_H_
#define _SYS_LKM_H_

/*
 * Supported module types
 */
typedef enum loadmod {
	LM_SYSCALL,
	LM_VFS,
	LM_DEV,
	LM_STRMOD,
	LM_EXEC,
	LM_MISC
} MODTYPE;


#define	LKM_VERSION	1		/* version of module loader */
#define	MAXLKMNAME	32

/****************************************************************************/

#ifdef KERNEL


/*
 * Loadable system call
 */
struct lkm_syscall {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;		/* save/assign area */
	struct sysent	*lkm_sysent;
	struct sysent	lkm_oldent;	/* save area for unload */
};

/*
 * Loadable file system
 */
struct lkm_vfs {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
	struct  linker_set *lkm_vnodeops;
	struct	vfsconf *lkm_vfsconf;
};

/*
 * Supported device module types
 */
typedef enum devtype {
	LM_DT_BLOCK,
	LM_DT_CHAR
} DEVTYPE;

/*
 * Loadable device driver
 */
struct lkm_dev {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
	DEVTYPE	lkm_devtype;
	union {
		void	*anon;
		struct bdevsw	*bdev;
		struct cdevsw	*cdev;
	} lkm_dev;
	union {
		struct bdevsw	*bdev;
		struct cdevsw	*cdev;
	} lkm_olddev;
};

/*
 * Loadable streams module
 */
struct lkm_strmod {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
	/*
	 * Removed: future release
	 */
};

/*
 * Exec loader
 */
struct lkm_exec {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
	const struct execsw	*lkm_exec;
	struct execsw	lkm_oldexec;
};

/*
 * Miscellaneous module (complex load/unload, potentially complex stat
 */
struct lkm_misc {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
};

/*
 * Any module (to get type and name info without knowing type)
 */
struct lkm_any {
	MODTYPE	lkm_type;
	int	lkm_ver;
	char	*lkm_name;
	u_long	lkm_offset;
};


/*
 * Generic reference ala XEvent to allow single entry point in the xxxinit()
 * routine.
 */
union lkm_generic {
	struct lkm_any		*lkm_any;
	struct lkm_syscall	*lkm_syscall;
	struct lkm_vfs		*lkm_vfs;
	struct lkm_dev		*lkm_dev;
	struct lkm_strmod	*lkm_strmod;
	struct lkm_exec		*lkm_exec;
	struct lkm_misc		*lkm_misc;
};

union lkm_all {
	struct lkm_any		lkm_any;
	struct lkm_syscall	lkm_syscall;
	struct lkm_vfs		lkm_vfs;
	struct lkm_dev		lkm_dev;
	struct lkm_strmod	lkm_strmod;
	struct lkm_exec		lkm_exec;
	struct lkm_misc		lkm_misc;
};

/*
 * Per module information structure
 */
struct lkm_table {
	int	type;
	u_long	size;
	u_long	offset;
	u_long	area;
	char	used;

	int	ver;		/* version (INIT) */
	int	refcnt;		/* reference count (INIT) */
	int	depcnt;		/* dependency count (INIT) */
	int	id;		/* identifier (INIT) */

	int	(*entry) __P((struct lkm_table *, int, int));
				/* entry function */
	union lkm_generic	private;	/* module private data */
};


#define	LKM_E_LOAD	1
#define	LKM_E_UNLOAD	2
#define	LKM_E_STAT	3

#define	MOD_DECL(name)							    \
	static int name ## _load __P((struct lkm_table *lkmtp, int cmd));   \
	static int name ## _unload __P((struct lkm_table *lkmtp, int cmd)); \
	int name ## _mod __P((struct lkm_table *lkmtp, int cmd,	int ver))   \

#define	MOD_SYSCALL(name,callslot,sysentp)	\
	static struct lkm_syscall _module = {	\
		LM_SYSCALL,			\
		LKM_VERSION,			\
		name,				\
		callslot,			\
		sysentp				\
	}

#define	MOD_VFS(name,vfsslot,vnodeops,vfsconf)	\
	static struct lkm_vfs _module = {	\
		LM_VFS,				\
		LKM_VERSION,			\
		name,				\
		vfsslot,			\
		vnodeops,			\
		vfsconf				\
	}

#define	MOD_DEV(name,devtype,devslot,devp)	\
	MOD_DECL(name);				\
	static struct lkm_dev name ## _module = {	\
		LM_DEV,				\
		LKM_VERSION,			\
		#name ## "_mod",		\
		devslot,			\
		devtype,			\
		(void *)devp			\
	}

#define	MOD_EXEC(name,execslot,execsw)		\
	MOD_DECL(name);				\
	static struct lkm_exec _module = {	\
		LM_EXEC,			\
		LKM_VERSION,			\
		#name ## "_mod",		\
		execslot,			\
		execsw				\
	}

#define	MOD_MISC(name)				\
	MOD_DECL(name);				\
	static struct lkm_misc _module = {	\
		LM_MISC,			\
		LKM_VERSION,			\
		#name ## "_mod"			\
	}


/*
 * DISPATCH -- body function for use in module entry point function;
 * generally, the function body will consist entirely of a single
 * DISPATCH line.
 *
 * Call load/unload/stat on each corresponding entry instance.  "cmd" is
 * passed to each function so that a single function can be used if desired.
 */
#define	DISPATCH(lkmtp,cmd,ver,load,unload,stat)			\
	if (ver != LKM_VERSION)						\
		return EINVAL;	/* version mismatch */			\
	switch (cmd) {							\
	int	error;							\
	case LKM_E_LOAD:						\
		lkmtp->private.lkm_any = (struct lkm_any *)&_module;	\
		if (lkmexists(lkmtp)) /* !!! */				\
			return EEXIST;					\
		if ((error = load(lkmtp, cmd)))				\
			return error;					\
		break;							\
	case LKM_E_UNLOAD:						\
		if ((error = unload(lkmtp, cmd)))			\
			return error;					\
		break;							\
	case LKM_E_STAT:						\
		if ((error = stat(lkmtp, cmd)))				\
			return error;					\
		break;							\
	}								\
	return lkmdispatch(lkmtp, cmd);

int lkmdispatch __P((struct lkm_table *lkmtp, int cmd));
int lkmexists	__P((struct lkm_table *lkmtp));
int lkm_nullcmd __P((struct lkm_table *lkmtp, int cmd));

#endif /* KERNEL */

/****************************************************************************/

/*
 * IOCTL's recognized by /dev/lkm
 */
#define	LMRESERV	_IOWR('K', 0, struct lmc_resrv)
#define	LMLOADBUF	_IOW('K', 1, struct lmc_loadbuf)
#define	LMUNRESRV	_IO('K', 2)
#define	LMREADY		_IOW('K', 3, int)

#define	LMLOAD		_IOW('K', 9, struct lmc_load)
#define	LMUNLOAD	_IOWR('K', 10, struct lmc_unload)
#define	LMSTAT		_IOWR('K', 11, struct lmc_stat)

#define	MODIOBUF	512		/* # of bytes at a time to loadbuf */

/*
 * IOCTL arguments
 */


/*
 * Reserve a page-aligned block of kernel memory for the module
 */
struct lmc_resrv {
	u_long	size;		/* IN: size of module to reserve */
	char	*name;		/* IN: name (must be provided */
	int	slot;		/* OUT: allocated slot (module ID) */
	u_long	addr;		/* OUT: Link-to address */
};


/*
 * Copy a buffer at a time into the allocated area in the kernel; writes
 * are assumed to occur contiguously.
 */
struct lmc_loadbuf {
	int	cnt;		/* IN: # of chars pointed to by data */
	char	*data;		/* IN: pointer to data buffer */
};


/*
 * Load a module (assumes it's been mmapped to address before call)
 */
struct lmc_load {
	caddr_t	address;	/* IN: user space mmap address */
	int	status;		/* OUT: status of operation */
	int	id;		/* OUT: module ID if loaded */
};

/*
 * Unload a module (by name/id)
 */
struct lmc_unload {
	int	id;		/* IN: module ID to unload */
	char	*name;		/* IN: module name to unload if id -1 */
	int	status;		/* OUT: status of operation */
};


/*
 * Get module information for a given id (or name if id == -1).
 */
struct lmc_stat {
	int	id;			/* IN: module ID to unload */
	char	name[MAXLKMNAME];	/* IN/OUT: name of module */
	u_long	offset;			/* OUT: target table offset */
	MODTYPE	type;			/* OUT: type of module */
	u_long	area;			/* OUT: kernel load addr */
	u_long	size;			/* OUT: module size (pages) */
	u_long	private;		/* OUT: module private data */
	int	ver;			/* OUT: lkm compile version */
};

#endif	/* !_SYS_LKM_H_ */
OpenPOWER on IntegriCloud