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