1 /*
2  * Dynamic loading of modules into the kernel.
3  *
4  * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
5  */
6 
7 #ifndef _LINUX_MODULE_H
8 #define _LINUX_MODULE_H
9 
10 #include <linux/config.h>
11 #include <linux/compiler.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 
15 #ifdef __GENKSYMS__
16 #  define _set_ver(sym) sym
17 #  undef  MODVERSIONS
18 #  define MODVERSIONS
19 #else /* ! __GENKSYMS__ */
20 # if !defined(MODVERSIONS) && defined(EXPORT_SYMTAB)
21 #   define _set_ver(sym) sym
22 #   include <linux/modversions.h>
23 # endif
24 #endif /* __GENKSYMS__ */
25 
26 #include <asm/atomic.h>
27 
28 /* Don't need to bring in all of uaccess.h just for this decl.  */
29 struct exception_table_entry;
30 
31 /* Used by get_kernel_syms, which is obsolete.  */
32 struct kernel_sym
33 {
34 	unsigned long value;
35 	char name[60];		/* should have been 64-sizeof(long); oh well */
36 };
37 
38 struct module_symbol
39 {
40 	unsigned long value;
41 	const char *name;
42 };
43 
44 struct module_ref
45 {
46 	struct module *dep;	/* "parent" pointer */
47 	struct module *ref;	/* "child" pointer */
48 	struct module_ref *next_ref;
49 };
50 
51 /* TBD */
52 struct module_persist;
53 
54 struct module
55 {
56 	unsigned long size_of_struct;	/* == sizeof(module) */
57 	struct module *next;
58 	const char *name;
59 	unsigned long size;
60 
61 	union
62 	{
63 		atomic_t usecount;
64 		long pad;
65 	} uc;				/* Needs to keep its size - so says rth */
66 
67 	unsigned long flags;		/* AUTOCLEAN et al */
68 
69 	unsigned nsyms;
70 	unsigned ndeps;
71 
72 	struct module_symbol *syms;
73 	struct module_ref *deps;
74 	struct module_ref *refs;
75 	int (*init)(void);
76 	void (*cleanup)(void);
77 	const struct exception_table_entry *ex_table_start;
78 	const struct exception_table_entry *ex_table_end;
79 #ifdef __alpha__
80 	unsigned long gp;
81 #endif
82 	/* Members past this point are extensions to the basic
83 	   module support and are optional.  Use mod_member_present()
84 	   to examine them.  */
85 	const struct module_persist *persist_start;
86 	const struct module_persist *persist_end;
87 	int (*can_unload)(void);
88 	int runsize;			/* In modutils, not currently used */
89 	const char *kallsyms_start;	/* All symbols for kernel debugging */
90 	const char *kallsyms_end;
91 	const char *archdata_start;	/* arch specific data for module */
92 	const char *archdata_end;
93 	const char *kernel_data;	/* Reserved for kernel internal use */
94 };
95 
96 struct module_info
97 {
98 	unsigned long addr;
99 	unsigned long size;
100 	unsigned long flags;
101 	long usecount;
102 };
103 
104 /* Bits of module.flags.  */
105 
106 #define MOD_UNINITIALIZED	0
107 #define MOD_RUNNING		1
108 #define MOD_DELETED		2
109 #define MOD_AUTOCLEAN		4
110 #define MOD_VISITED  		8
111 #define MOD_USED_ONCE		16
112 #define MOD_JUST_FREED		32
113 #define MOD_INITIALIZING	64
114 
115 /* Values for query_module's which.  */
116 
117 #define QM_MODULES	1
118 #define QM_DEPS		2
119 #define QM_REFS		3
120 #define QM_SYMBOLS	4
121 #define QM_INFO		5
122 
123 /* Can the module be queried? */
124 #define MOD_CAN_QUERY(mod) (((mod)->flags & (MOD_RUNNING | MOD_INITIALIZING)) && !((mod)->flags & MOD_DELETED))
125 
126 /* When struct module is extended, we must test whether the new member
127    is present in the header received from insmod before we can use it.
128    This function returns true if the member is present.  */
129 
130 #define mod_member_present(mod,member) 					\
131 	((unsigned long)(&((struct module *)0L)->member + 1)		\
132 	 <= (mod)->size_of_struct)
133 
134 /*
135  * Ditto for archdata.  Assumes mod->archdata_start and mod->archdata_end
136  * are validated elsewhere.
137  */
138 #define mod_archdata_member_present(mod, type, member)			\
139 	(((unsigned long)(&((type *)0L)->member) +			\
140 	  sizeof(((type *)0L)->member)) <=				\
141 	 ((mod)->archdata_end - (mod)->archdata_start))
142 
143 
144 /* Check if an address p with number of entries n is within the body of module m */
145 #define mod_bound(p, n, m) ((unsigned long)(p) >= ((unsigned long)(m) + ((m)->size_of_struct)) && \
146 	         (unsigned long)((p)+(n)) <= (unsigned long)(m) + (m)->size)
147 
148 /* Backwards compatibility definition.  */
149 
150 #define GET_USE_COUNT(module)	(atomic_read(&(module)->uc.usecount))
151 
152 /* Poke the use count of a module.  */
153 
154 #define __MOD_INC_USE_COUNT(mod)					\
155 	(atomic_inc(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED|MOD_USED_ONCE)
156 #define __MOD_DEC_USE_COUNT(mod)					\
157 	(atomic_dec(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED)
158 #define __MOD_IN_USE(mod)						\
159 	(mod_member_present((mod), can_unload) && (mod)->can_unload	\
160 	 ? (mod)->can_unload() : atomic_read(&(mod)->uc.usecount))
161 
162 /* Indirect stringification.  */
163 
164 #define __MODULE_STRING_1(x)	#x
165 #define __MODULE_STRING(x)	__MODULE_STRING_1(x)
166 
167 /* Generic inter module communication.
168  *
169  * NOTE: This interface is intended for small amounts of data that are
170  *       passed between two objects and either or both of the objects
171  *       might be compiled as modules.  Do not over use this interface.
172  *
173  *       If more than two objects need to communicate then you probably
174  *       need a specific interface instead of abusing this generic
175  *       interface.  If both objects are *always* built into the kernel
176  *       then a global extern variable is good enough, you do not need
177  *       this interface.
178  *
179  * Keith Owens <kaos@ocs.com.au> 28 Oct 2000.
180  */
181 
182 #ifdef __KERNEL__
183 #define HAVE_INTER_MODULE
184 extern void inter_module_register(const char *, struct module *, const void *);
185 extern void inter_module_unregister(const char *);
186 extern const void *inter_module_get(const char *);
187 extern const void *inter_module_get_request(const char *, const char *);
188 extern void inter_module_put(const char *);
189 
190 struct inter_module_entry {
191 	struct list_head list;
192 	const char *im_name;
193 	struct module *owner;
194 	const void *userdata;
195 };
196 
197 extern int try_inc_mod_count(struct module *mod);
198 #endif /* __KERNEL__ */
199 
200 #if defined(MODULE) && !defined(__GENKSYMS__)
201 
202 /* Embedded module documentation macros.  */
203 
204 /* For documentation purposes only.  */
205 
206 #define MODULE_AUTHOR(name)						   \
207 const char __module_author[] __attribute__((section(".modinfo"))) = 	   \
208 "author=" name
209 
210 #define MODULE_DESCRIPTION(desc)					   \
211 const char __module_description[] __attribute__((section(".modinfo"))) =   \
212 "description=" desc
213 
214 /* Could potentially be used by kmod...  */
215 
216 #define MODULE_SUPPORTED_DEVICE(dev)					   \
217 const char __module_device[] __attribute__((section(".modinfo"))) = 	   \
218 "device=" dev
219 
220 /* Used to verify parameters given to the module.  The TYPE arg should
221    be a string in the following format:
222    	[min[-max]]{b,h,i,l,s}
223    The MIN and MAX specifiers delimit the length of the array.  If MAX
224    is omitted, it defaults to MIN; if both are omitted, the default is 1.
225    The final character is a type specifier:
226 	b	byte
227 	h	short
228 	i	int
229 	l	long
230 	s	string
231 */
232 
233 #define MODULE_PARM(var,type)			\
234 const char __module_parm_##var[]		\
235 __attribute__((section(".modinfo"))) =		\
236 "parm_" __MODULE_STRING(var) "=" type
237 
238 #define MODULE_PARM_DESC(var,desc)		\
239 const char __module_parm_desc_##var[]		\
240 __attribute__((section(".modinfo"))) =		\
241 "parm_desc_" __MODULE_STRING(var) "=" desc
242 
243 /*
244  * MODULE_DEVICE_TABLE exports information about devices
245  * currently supported by this module.  A device type, such as PCI,
246  * is a C-like identifier passed as the first arg to this macro.
247  * The second macro arg is the variable containing the device
248  * information being made public.
249  *
250  * The following is a list of known device types (arg 1),
251  * and the C types which are to be passed as arg 2.
252  * pci - struct pci_device_id - List of PCI ids supported by this module
253  * isapnp - struct isapnp_device_id - List of ISA PnP ids supported by this module
254  * usb - struct usb_device_id - List of USB ids supported by this module
255  */
256 #define MODULE_GENERIC_TABLE(gtype,name)	\
257 static const unsigned long __module_##gtype##_size \
258   __attribute_used__ = sizeof(struct gtype##_id); \
259 static const struct gtype##_id * __module_##gtype##_table \
260   __attribute_used__ = name
261 
262 /*
263  * The following license idents are currently accepted as indicating free
264  * software modules
265  *
266  *	"GPL"				[GNU Public License v2 or later]
267  *	"GPL v2"			[GNU Public License v2]
268  *	"GPL and additional rights"	[GNU Public License v2 rights and more]
269  *	"Dual BSD/GPL"			[GNU Public License v2 or BSD license choice]
270  *	"Dual MPL/GPL"			[GNU Public License v2 or Mozilla license choice]
271  *
272  * The following other idents are available
273  *
274  *	"Proprietary"			[Non free products]
275  *
276  * There are dual licensed components, but when running with Linux it is the
277  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
278  * is a GPL combined work.
279  *
280  * This exists for several reasons
281  * 1.	So modinfo can show license info for users wanting to vet their setup
282  *	is free
283  * 2.	So the community can ignore bug reports including proprietary modules
284  * 3.	So vendors can do likewise based on their own policies
285  */
286 
287 #define MODULE_LICENSE(license) 	\
288 static const char __module_license[] __attribute_used__ __attribute__((section(".modinfo"))) =   \
289 "license=" license
290 
291 /* Define the module variable, and usage macros.  */
292 extern struct module __this_module;
293 
294 #define THIS_MODULE		(&__this_module)
295 #define MOD_INC_USE_COUNT	__MOD_INC_USE_COUNT(THIS_MODULE)
296 #define MOD_DEC_USE_COUNT	__MOD_DEC_USE_COUNT(THIS_MODULE)
297 #define MOD_IN_USE		__MOD_IN_USE(THIS_MODULE)
298 
299 #include <linux/version.h>
300 static const char __module_kernel_version[] __attribute_used__ __attribute__((section(".modinfo"))) =
301 "kernel_version=" UTS_RELEASE;
302 #ifdef MODVERSIONS
303 static const char __module_using_checksums[] __attribute_used__ __attribute__((section(".modinfo"))) =
304 "using_checksums=1";
305 #endif
306 
307 #else /* MODULE */
308 
309 #define MODULE_AUTHOR(name)
310 #define MODULE_LICENSE(license)
311 #define MODULE_DESCRIPTION(desc)
312 #define MODULE_SUPPORTED_DEVICE(name)
313 #define MODULE_PARM(var,type)
314 #define MODULE_PARM_DESC(var,desc)
315 
316 /* Create a dummy reference to the table to suppress gcc unused warnings.  Put
317  * the reference in the .data.exit section which is discarded when code is built
318  * in, so the reference does not bloat the running kernel.  Note: cannot be
319  * const, other exit data may be writable.
320  */
321 #define MODULE_GENERIC_TABLE(gtype,name) \
322 static const struct gtype##_id * __module_##gtype##_table \
323   __attribute_used__ __attribute__ ((__section__(".data.exit"))) = name
324 
325 #ifndef __GENKSYMS__
326 
327 #define THIS_MODULE		NULL
328 #define MOD_INC_USE_COUNT	do { } while (0)
329 #define MOD_DEC_USE_COUNT	do { } while (0)
330 #define MOD_IN_USE		1
331 
332 extern struct module *module_list;
333 
334 #endif /* !__GENKSYMS__ */
335 
336 #endif /* MODULE */
337 
338 #define MODULE_DEVICE_TABLE(type,name)		\
339   MODULE_GENERIC_TABLE(type##_device,name)
340 
341 /* Export a symbol either from the kernel or a module.
342 
343    In the kernel, the symbol is added to the kernel's global symbol table.
344 
345    In a module, it controls which variables are exported.  If no
346    variables are explicitly exported, the action is controled by the
347    insmod -[xX] flags.  Otherwise, only the variables listed are exported.
348    This obviates the need for the old register_symtab() function.  */
349 
350 #if defined(__GENKSYMS__)
351 
352 /* We want the EXPORT_SYMBOL tag left intact for recognition.  */
353 
354 #elif !defined(AUTOCONF_INCLUDED)
355 
356 #define __EXPORT_SYMBOL(sym,str)   error config_must_be_included_before_module
357 #define EXPORT_SYMBOL(var)	   error config_must_be_included_before_module
358 #define EXPORT_SYMBOL_NOVERS(var)  error config_must_be_included_before_module
359 #define EXPORT_SYMBOL_GPL(var)  error config_must_be_included_before_module
360 
361 #elif !defined(CONFIG_MODULES)
362 
363 #define __EXPORT_SYMBOL(sym,str)
364 #define EXPORT_SYMBOL(var)
365 #define EXPORT_SYMBOL_NOVERS(var)
366 #define EXPORT_SYMBOL_GPL(var)
367 
368 #elif !defined(EXPORT_SYMTAB)
369 
370 #define __EXPORT_SYMBOL(sym,str)   error this_object_must_be_defined_as_export_objs_in_the_Makefile
371 #define EXPORT_SYMBOL(var)	   error this_object_must_be_defined_as_export_objs_in_the_Makefile
372 #define EXPORT_SYMBOL_NOVERS(var)  error this_object_must_be_defined_as_export_objs_in_the_Makefile
373 #define EXPORT_SYMBOL_GPL(var)  error this_object_must_be_defined_as_export_objs_in_the_Makefile
374 
375 #else
376 
377 #define __EXPORT_SYMBOL(sym, str)			\
378 const char __kstrtab_##sym[]				\
379 __attribute__((section(".kstrtab"))) = str;		\
380 const struct module_symbol __ksymtab_##sym 		\
381 __attribute__((section("__ksymtab"))) =			\
382 { (unsigned long)&sym, __kstrtab_##sym }
383 
384 #define __EXPORT_SYMBOL_GPL(sym, str)			\
385 const char __kstrtab_##sym[]				\
386 __attribute__((section(".kstrtab"))) = "GPLONLY_" str;	\
387 const struct module_symbol __ksymtab_##sym		\
388 __attribute__((section("__ksymtab"))) =			\
389 { (unsigned long)&sym, __kstrtab_##sym }
390 
391 #if defined(MODVERSIONS) || !defined(CONFIG_MODVERSIONS)
392 #define EXPORT_SYMBOL(var)  __EXPORT_SYMBOL(var, __MODULE_STRING(var))
393 #define EXPORT_SYMBOL_GPL(var)  __EXPORT_SYMBOL_GPL(var, __MODULE_STRING(var))
394 #else
395 #define EXPORT_SYMBOL(var)  __EXPORT_SYMBOL(var, __MODULE_STRING(__VERSIONED_SYMBOL(var)))
396 #define EXPORT_SYMBOL_GPL(var)  __EXPORT_SYMBOL(var, __MODULE_STRING(__VERSIONED_SYMBOL(var)))
397 #endif
398 
399 #define EXPORT_SYMBOL_NOVERS(var)  __EXPORT_SYMBOL(var, __MODULE_STRING(var))
400 
401 #endif /* __GENKSYMS__ */
402 
403 #ifdef MODULE
404 /* Force a module to export no symbols.  */
405 #define EXPORT_NO_SYMBOLS  __asm__(".section __ksymtab\n.previous")
406 #else
407 #define EXPORT_NO_SYMBOLS
408 #endif /* MODULE */
409 
410 #ifdef CONFIG_MODULES
411 #define SET_MODULE_OWNER(some_struct) do { (some_struct)->owner = THIS_MODULE; } while (0)
412 #else
413 #define SET_MODULE_OWNER(some_struct) do { } while (0)
414 #endif
415 
416 #endif /* _LINUX_MODULE_H */
417