ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / linux / module.h
1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H
3 /*
4  * Dynamic loading of modules into the kernel.
5  *
6  * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
7  * Rewritten again by Rusty Russell, 2002
8  */
9 #include <linux/config.h>
10 #include <linux/sched.h>
11 #include <linux/spinlock.h>
12 #include <linux/list.h>
13 #include <linux/stat.h>
14 #include <linux/compiler.h>
15 #include <linux/cache.h>
16 #include <linux/kmod.h>
17 #include <linux/elf.h>
18 #include <linux/stringify.h>
19 #include <asm/local.h>
20
21 #include <asm/module.h>
22
23 /* Not Yet Implemented */
24 #define MODULE_SUPPORTED_DEVICE(name)
25 #define print_modules()
26
27 /* v850 toolchain uses a `_' prefix for all user symbols */
28 #ifndef MODULE_SYMBOL_PREFIX
29 #define MODULE_SYMBOL_PREFIX ""
30 #endif
31
32 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
33
34 struct kernel_symbol
35 {
36         unsigned long value;
37         const char *name;
38 };
39
40 struct modversion_info
41 {
42         unsigned long crc;
43         char name[MODULE_NAME_LEN];
44 };
45
46 /* These are either module local, or the kernel's dummy ones. */
47 extern int init_module(void);
48 extern void cleanup_module(void);
49
50 /* Archs provide a method of finding the correct exception table. */
51 struct exception_table_entry;
52
53 const struct exception_table_entry *
54 search_extable(const struct exception_table_entry *first,
55                const struct exception_table_entry *last,
56                unsigned long value);
57 void sort_extable(struct exception_table_entry *start,
58                   struct exception_table_entry *finish);
59 void sort_main_extable(void);
60
61 #ifdef MODULE
62 #define ___module_cat(a,b) __mod_ ## a ## b
63 #define __module_cat(a,b) ___module_cat(a,b)
64 #define __MODULE_INFO(tag, name, info)                                    \
65 static const char __module_cat(name,__LINE__)[]                           \
66   __attribute_used__                                                      \
67   __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info
68
69 #define MODULE_GENERIC_TABLE(gtype,name)                        \
70 extern const struct gtype##_id __mod_##gtype##_table            \
71   __attribute__ ((unused, alias(__stringify(name))))
72
73 extern struct module __this_module;
74 #define THIS_MODULE (&__this_module)
75
76 #else  /* !MODULE */
77
78 #define MODULE_GENERIC_TABLE(gtype,name)
79 #define __MODULE_INFO(tag, name, info)
80 #define THIS_MODULE ((struct module *)0)
81 #endif
82
83 /* Generic info of form tag = "info" */
84 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
85
86 /* For userspace: you can also call me... */
87 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
88
89 /*
90  * The following license idents are currently accepted as indicating free
91  * software modules
92  *
93  *      "GPL"                           [GNU Public License v2 or later]
94  *      "GPL v2"                        [GNU Public License v2]
95  *      "GPL and additional rights"     [GNU Public License v2 rights and more]
96  *      "Dual BSD/GPL"                  [GNU Public License v2
97  *                                       or BSD license choice]
98  *      "Dual MPL/GPL"                  [GNU Public License v2
99  *                                       or Mozilla license choice]
100  *
101  * The following other idents are available
102  *
103  *      "Proprietary"                   [Non free products]
104  *
105  * There are dual licensed components, but when running with Linux it is the
106  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
107  * is a GPL combined work.
108  *
109  * This exists for several reasons
110  * 1.   So modinfo can show license info for users wanting to vet their setup 
111  *      is free
112  * 2.   So the community can ignore bug reports including proprietary modules
113  * 3.   So vendors can do likewise based on their own policies
114  */
115 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
116
117 /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
118 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
119   
120 /* What your module does. */
121 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
122
123 /* One for each parameter, describing how to use it.  Some files do
124    multiple of these per line, so can't just use MODULE_INFO. */
125 #define MODULE_PARM_DESC(_parm, desc) \
126         __MODULE_INFO(parm, _parm, #_parm ":" desc)
127
128 #define MODULE_DEVICE_TABLE(type,name)          \
129   MODULE_GENERIC_TABLE(type##_device,name)
130
131 /* Version of form [<epoch>:]<version>[-<extra-version>].
132    Or for CVS/RCS ID version, everything but the number is stripped.
133   <epoch>: A (small) unsigned integer which allows you to start versions
134            anew. If not mentioned, it's zero.  eg. "2:1.0" is after
135            "1:2.0".
136   <version>: The <version> may contain only alphanumerics and the
137            character `.'.  Ordered by numeric sort for numeric parts,
138            ascii sort for ascii parts (as per RPM or DEB algorithm).
139   <extraversion>: Like <version>, but inserted for local
140            customizations, eg "rh3" or "rusty1".
141
142   Using this automatically adds a checksum of the .c files and the
143   local headers to the end.  Use MODULE_VERSION("") if you want just
144   this.  Macro includes room for this.
145 */
146 #define MODULE_VERSION(_version) \
147   MODULE_INFO(version, _version "\0xxxxxxxxxxxxxxxxxxxxxxxx")
148
149 /* Given an address, look for it in the exception tables */
150 const struct exception_table_entry *search_exception_tables(unsigned long add);
151
152 struct notifier_block;
153
154 #ifdef CONFIG_MODULES
155
156 /* Get/put a kernel symbol (calls must be symmetric) */
157 void *__symbol_get(const char *symbol);
158 void *__symbol_get_gpl(const char *symbol);
159 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
160
161 #ifndef __GENKSYMS__
162 #ifdef CONFIG_MODVERSIONS
163 /* Mark the CRC weak since genksyms apparently decides not to
164  * generate a checksums for some symbols */
165 #define __CRC_SYMBOL(sym, sec)                                  \
166         extern void *__crc_##sym __attribute__((weak));         \
167         static const unsigned long __kcrctab_##sym              \
168         __attribute_used__                                      \
169         __attribute__((section("__kcrctab" sec), unused))       \
170         = (unsigned long) &__crc_##sym;
171 #else
172 #define __CRC_SYMBOL(sym, sec)
173 #endif
174
175 /* For every exported symbol, place a struct in the __ksymtab section */
176 #define __EXPORT_SYMBOL(sym, sec)                               \
177         __CRC_SYMBOL(sym, sec)                                  \
178         static const char __kstrtab_##sym[]                     \
179         __attribute__((section("__ksymtab_strings")))           \
180         = MODULE_SYMBOL_PREFIX #sym;                            \
181         static const struct kernel_symbol __ksymtab_##sym       \
182         __attribute_used__                                      \
183         __attribute__((section("__ksymtab" sec), unused))       \
184         = { (unsigned long)&sym, __kstrtab_##sym }
185
186 #define EXPORT_SYMBOL(sym)                                      \
187         __EXPORT_SYMBOL(sym, "")
188
189 #define EXPORT_SYMBOL_GPL(sym)                                  \
190         __EXPORT_SYMBOL(sym, "_gpl")
191
192 #endif
193
194 /* We don't mangle the actual symbol anymore, so no need for
195  * special casing EXPORT_SYMBOL_NOVERS.  FIXME: Deprecated */
196 #define EXPORT_SYMBOL_NOVERS(sym) EXPORT_SYMBOL(sym)
197
198 struct module_ref
199 {
200         local_t count;
201 } ____cacheline_aligned;
202
203 enum module_state
204 {
205         MODULE_STATE_LIVE,
206         MODULE_STATE_COMING,
207         MODULE_STATE_GOING,
208 };
209
210 struct module
211 {
212         enum module_state state;
213
214         /* Member of list of modules */
215         struct list_head list;
216
217         /* Unique handle for this module */
218         char name[MODULE_NAME_LEN];
219
220         /* Exported symbols */
221         const struct kernel_symbol *syms;
222         unsigned int num_syms;
223         const unsigned long *crcs;
224
225         /* GPL-only exported symbols. */
226         const struct kernel_symbol *gpl_syms;
227         unsigned int num_gpl_syms;
228         const unsigned long *gpl_crcs;
229
230         /* Exception table */
231         unsigned int num_exentries;
232         const struct exception_table_entry *extable;
233
234         /* Startup function. */
235         int (*init)(void);
236
237         /* If this is non-NULL, vfree after init() returns */
238         void *module_init;
239
240         /* Here is the actual code + data, vfree'd on unload. */
241         void *module_core;
242
243         /* Here are the sizes of the init and core sections */
244         unsigned long init_size, core_size;
245
246         /* The size of the executable code in each section.  */
247         unsigned long init_text_size, core_text_size;
248
249         /* Arch-specific module values */
250         struct mod_arch_specific arch;
251
252         /* Am I unsafe to unload? */
253         int unsafe;
254
255         /* Am I GPL-compatible */
256         int license_gplok;
257
258 #ifdef CONFIG_MODULE_UNLOAD
259         /* Reference counts */
260         struct module_ref ref[NR_CPUS];
261
262         /* What modules depend on me? */
263         struct list_head modules_which_use_me;
264
265         /* Who is waiting for us to be unloaded */
266         struct task_struct *waiter;
267
268         /* Destruction function. */
269         void (*exit)(void);
270 #endif
271
272 #ifdef CONFIG_KALLSYMS
273         /* We keep the symbol and string tables for kallsyms. */
274         Elf_Sym *symtab;
275         unsigned long num_symtab;
276         char *strtab;
277 #endif
278
279         /* Per-cpu data. */
280         void *percpu;
281
282         /* The command line arguments (may be mangled).  People like
283            keeping pointers to this stuff */
284         char *args;
285 };
286
287 /* FIXME: It'd be nice to isolate modules during init, too, so they
288    aren't used before they (may) fail.  But presently too much code
289    (IDE & SCSI) require entry into the module during init.*/
290 static inline int module_is_live(struct module *mod)
291 {
292         return mod->state != MODULE_STATE_GOING;
293 }
294
295 /* Is this address in a module? */
296 struct module *module_text_address(unsigned long addr);
297
298 /* Returns module and fills in value, defined and namebuf, or NULL if
299    symnum out of range. */
300 struct module *module_get_kallsym(unsigned int symnum,
301                                   unsigned long *value,
302                                   char *type,
303                                   char namebuf[128]);
304
305 /* Look for this name: can be of form module:name. */
306 unsigned long module_kallsyms_lookup_name(const char *name);
307
308 int is_exported(const char *name, const struct module *mod);
309
310 extern void __module_put_and_exit(struct module *mod, long code)
311         __attribute__((noreturn));
312 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
313
314 #ifdef CONFIG_MODULE_UNLOAD
315 unsigned int module_refcount(struct module *mod);
316 void __symbol_put(const char *symbol);
317 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
318 void symbol_put_addr(void *addr);
319
320 /* Sometimes we know we already have a refcount, and it's easier not
321    to handle the error case (which only happens with rmmod --wait). */
322 static inline void __module_get(struct module *module)
323 {
324         if (module) {
325                 BUG_ON(module_refcount(module) == 0);
326                 local_inc(&module->ref[get_cpu()].count);
327                 put_cpu();
328         }
329 }
330
331 static inline int try_module_get(struct module *module)
332 {
333         int ret = 1;
334
335         if (module) {
336                 unsigned int cpu = get_cpu();
337                 if (likely(module_is_live(module)))
338                         local_inc(&module->ref[cpu].count);
339                 else
340                         ret = 0;
341                 put_cpu();
342         }
343         return ret;
344 }
345
346 static inline void module_put(struct module *module)
347 {
348         if (module) {
349                 unsigned int cpu = get_cpu();
350                 local_dec(&module->ref[cpu].count);
351                 /* Maybe they're waiting for us to drop reference? */
352                 if (unlikely(!module_is_live(module)))
353                         wake_up_process(module->waiter);
354                 put_cpu();
355         }
356 }
357
358 #else /*!CONFIG_MODULE_UNLOAD*/
359 static inline int try_module_get(struct module *module)
360 {
361         return !module || module_is_live(module);
362 }
363 static inline void module_put(struct module *module)
364 {
365 }
366 static inline void __module_get(struct module *module)
367 {
368 }
369 #define symbol_put(x) do { } while(0)
370 #define symbol_put_addr(p) do { } while(0)
371
372 #endif /* CONFIG_MODULE_UNLOAD */
373
374 /* This is a #define so the string doesn't get put in every .o file */
375 #define module_name(mod)                        \
376 ({                                              \
377         struct module *__mod = (mod);           \
378         __mod ? __mod->name : "kernel";         \
379 })
380
381 #define __unsafe(mod)                                                        \
382 do {                                                                         \
383         if (mod && !(mod)->unsafe) {                                         \
384                 printk(KERN_WARNING                                          \
385                        "Module %s cannot be unloaded due to unsafe usage in" \
386                        " %s:%u\n", (mod)->name, __FILE__, __LINE__);         \
387                 (mod)->unsafe = 1;                                           \
388         }                                                                    \
389 } while(0)
390
391 /* For kallsyms to ask for address resolution.  NULL means not found. */
392 const char *module_address_lookup(unsigned long addr,
393                                   unsigned long *symbolsize,
394                                   unsigned long *offset,
395                                   char **modname);
396
397 /* For extable.c to search modules' exception tables. */
398 const struct exception_table_entry *search_module_extables(unsigned long addr);
399
400 int register_module_notifier(struct notifier_block * nb);
401 int unregister_module_notifier(struct notifier_block * nb);
402
403 #else /* !CONFIG_MODULES... */
404 #define EXPORT_SYMBOL(sym)
405 #define EXPORT_SYMBOL_GPL(sym)
406 #define EXPORT_SYMBOL_NOVERS(sym)
407
408 /* Given an address, look for it in the exception tables. */
409 static inline const struct exception_table_entry *
410 search_module_extables(unsigned long addr)
411 {
412         return NULL;
413 }
414
415 /* Is this address in a module? */
416 static inline struct module *module_text_address(unsigned long addr)
417 {
418         return NULL;
419 }
420
421 /* Get/put a kernel symbol (calls should be symmetric) */
422 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
423 #define symbol_put(x) do { } while(0)
424 #define symbol_put_addr(x) do { } while(0)
425
426 static inline void __module_get(struct module *module)
427 {
428 }
429
430 static inline int try_module_get(struct module *module)
431 {
432         return 1;
433 }
434
435 static inline void module_put(struct module *module)
436 {
437 }
438
439 #define module_name(mod) "kernel"
440
441 #define __unsafe(mod)
442
443 /* For kallsyms to ask for address resolution.  NULL means not found. */
444 static inline const char *module_address_lookup(unsigned long addr,
445                                                 unsigned long *symbolsize,
446                                                 unsigned long *offset,
447                                                 char **modname)
448 {
449         return NULL;
450 }
451
452 static inline struct module *module_get_kallsym(unsigned int symnum,
453                                                 unsigned long *value,
454                                                 char *type,
455                                                 char namebuf[128])
456 {
457         return NULL;
458 }
459
460 static inline unsigned long module_kallsyms_lookup_name(const char *name)
461 {
462         return 0;
463 }
464
465 static inline int is_exported(const char *name, const struct module *mod)
466 {
467         return 0;
468 }
469
470 static inline int register_module_notifier(struct notifier_block * nb)
471 {
472         /* no events will happen anyway, so this can always succeed */
473         return 0;
474 }
475
476 static inline int unregister_module_notifier(struct notifier_block * nb)
477 {
478         return 0;
479 }
480
481 #define module_put_and_exit(code) do_exit(code)
482
483 #endif /* CONFIG_MODULES */
484
485 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
486
487 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
488
489 struct obsolete_modparm {
490         char name[64];
491         char type[64-sizeof(void *)];
492         void *addr;
493 };
494 #ifdef MODULE
495 /* DEPRECATED: Do not use. */
496 #define MODULE_PARM(var,type)                                               \
497 struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
498 { __stringify(var), type };
499
500 static inline void __deprecated MOD_INC_USE_COUNT(struct module *module)
501 {
502         __unsafe(module);
503
504 #if defined(CONFIG_MODULE_UNLOAD) && defined(MODULE)
505         local_inc(&module->ref[get_cpu()].count);
506         put_cpu();
507 #else
508         (void)try_module_get(module);
509 #endif
510 }
511
512 static inline void __deprecated MOD_DEC_USE_COUNT(struct module *module)
513 {
514         module_put(module);
515 }
516
517 #define MOD_INC_USE_COUNT       MOD_INC_USE_COUNT(THIS_MODULE)
518 #define MOD_DEC_USE_COUNT       MOD_DEC_USE_COUNT(THIS_MODULE)
519 #else
520 #define MODULE_PARM(var,type)
521 #define MOD_INC_USE_COUNT       do { } while (0)
522 #define MOD_DEC_USE_COUNT       do { } while (0)
523 #endif
524
525 #define __MODULE_STRING(x) __stringify(x)
526
527 /* Use symbol_get and symbol_put instead.  You'll thank me. */
528 #define HAVE_INTER_MODULE
529 extern void inter_module_register(const char *, struct module *, const void *);
530 extern void inter_module_unregister(const char *);
531 extern const void *inter_module_get(const char *);
532 extern const void *inter_module_get_request(const char *, const char *);
533 extern void inter_module_put(const char *);
534
535 #endif /* _LINUX_MODULE_H */