MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / include / linux / module.h
blobfbf2dfc90015e69de79d452cea32e2d4f276e891
1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H
3 /*
4 * Dynamic loading of modules into the kernel.
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>
23 #include <asm/module.h>
25 /* Not Yet Implemented */
26 #define MODULE_SUPPORTED_DEVICE(name)
28 /* v850 toolchain uses a `_' prefix for all user symbols */
29 #ifndef MODULE_SYMBOL_PREFIX
30 #define MODULE_SYMBOL_PREFIX ""
31 #endif
33 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
35 struct kernel_symbol
37 unsigned long value;
38 const char *name;
41 struct modversion_info
43 unsigned long crc;
44 char name[MODULE_NAME_LEN];
47 /* These are either module local, or the kernel's dummy ones. */
48 extern int init_module(void);
49 extern void cleanup_module(void);
51 /* Archs provide a method of finding the correct exception table. */
52 struct exception_table_entry;
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);
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
70 #define MODULE_GENERIC_TABLE(gtype,name) \
71 extern const struct gtype##_id __mod_##gtype##_table \
72 __attribute__ ((unused, alias(__stringify(name))))
74 extern struct module __this_module;
75 #define THIS_MODULE (&__this_module)
77 #else /* !MODULE */
79 #define MODULE_GENERIC_TABLE(gtype,name)
80 #define __MODULE_INFO(tag, name, info)
81 #define THIS_MODULE ((struct module *)0)
82 #endif
84 /* Generic info of form tag = "info" */
85 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
87 /* For userspace: you can also call me... */
88 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
91 * The following license idents are currently accepted as indicating free
92 * software modules
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]
102 * The following other idents are available
104 * "Proprietary" [Non free products]
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.
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
116 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
118 /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
119 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
121 /* What your module does. */
122 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
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)
129 #define MODULE_DEVICE_TABLE(type,name) \
130 MODULE_GENERIC_TABLE(type##_device,name)
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".
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.
147 #define MODULE_VERSION(_version) \
148 MODULE_INFO(version, _version "\0xxxxxxxxxxxxxxxxxxxxxxxx")
150 /* Given an address, look for it in the exception tables */
151 const struct exception_table_entry *search_exception_tables(unsigned long add);
153 struct notifier_block;
155 #ifdef CONFIG_MODULES
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)))
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
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 }
187 #define EXPORT_SYMBOL(sym) \
188 __EXPORT_SYMBOL(sym, "")
190 #define EXPORT_SYMBOL_GPL(sym) \
191 __EXPORT_SYMBOL(sym, "_gpl")
193 #endif
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)
199 struct module_ref
201 local_t count;
202 } ____cacheline_aligned;
204 enum module_state
206 MODULE_STATE_LIVE,
207 MODULE_STATE_COMING,
208 MODULE_STATE_GOING,
211 /* sysfs stuff */
212 struct module_attribute
214 struct attribute attr;
215 struct kernel_param *param;
218 struct module_kobject
220 /* Everyone should have one of these. */
221 struct kobject kobj;
223 /* We always have refcnt, we may have others from module_param(). */
224 unsigned int num_attributes;
225 struct module_attribute attr[0];
228 /* Similar stuff for section attributes. */
229 #define MODULE_SECT_NAME_LEN 32
230 struct module_sect_attr
232 struct attribute attr;
233 char name[MODULE_SECT_NAME_LEN];
234 unsigned long address;
237 struct module_sections
239 struct kobject kobj;
240 struct module_sect_attr attrs[0];
244 struct module
246 enum module_state state;
248 /* Member of list of modules */
249 struct list_head list;
251 /* Unique handle for this module */
252 char name[MODULE_NAME_LEN];
254 /* Sysfs stuff. */
255 struct module_kobject *mkobj;
257 /* Exported symbols */
258 const struct kernel_symbol *syms;
259 unsigned int num_syms;
260 const unsigned long *crcs;
262 /* GPL-only exported symbols. */
263 const struct kernel_symbol *gpl_syms;
264 unsigned int num_gpl_syms;
265 const unsigned long *gpl_crcs;
267 /* Exception table */
268 unsigned int num_exentries;
269 const struct exception_table_entry *extable;
271 /* Startup function. */
272 int (*init)(void);
274 /* If this is non-NULL, vfree after init() returns */
275 void *module_init;
277 /* Here is the actual code + data, vfree'd on unload. */
278 void *module_core;
280 /* Here are the sizes of the init and core sections */
281 unsigned long init_size, core_size;
283 /* The size of the executable code in each section. */
284 unsigned long init_text_size, core_text_size;
286 /* Arch-specific module values */
287 struct mod_arch_specific arch;
289 /* Am I unsafe to unload? */
290 int unsafe;
292 /* Am I GPL-compatible */
293 int license_gplok;
295 #ifdef CONFIG_MODULE_UNLOAD
296 /* Reference counts */
297 struct module_ref ref[NR_CPUS];
299 /* What modules depend on me? */
300 struct list_head modules_which_use_me;
302 /* Who is waiting for us to be unloaded */
303 struct task_struct *waiter;
305 /* Destruction function. */
306 void (*exit)(void);
308 /* Fake kernel param for refcnt. */
309 struct kernel_param refcnt_param;
310 #endif
312 #ifdef CONFIG_KALLSYMS
313 /* We keep the symbol and string tables for kallsyms. */
314 Elf_Sym *symtab;
315 unsigned long num_symtab;
316 char *strtab;
318 /* Section attributes */
319 struct module_sections *sect_attrs;
320 #endif
322 /* Per-cpu data. */
323 void *percpu;
325 /* The command line arguments (may be mangled). People like
326 keeping pointers to this stuff */
327 char *args;
330 /* FIXME: It'd be nice to isolate modules during init, too, so they
331 aren't used before they (may) fail. But presently too much code
332 (IDE & SCSI) require entry into the module during init.*/
333 static inline int module_is_live(struct module *mod)
335 return mod->state != MODULE_STATE_GOING;
338 /* Is this address in a module? (second is with no locks, for oops) */
339 struct module *module_text_address(unsigned long addr);
340 struct module *__module_text_address(unsigned long addr);
342 /* Returns module and fills in value, defined and namebuf, or NULL if
343 symnum out of range. */
344 struct module *module_get_kallsym(unsigned int symnum,
345 unsigned long *value,
346 char *type,
347 char namebuf[128]);
349 /* Look for this name: can be of form module:name. */
350 unsigned long module_kallsyms_lookup_name(const char *name);
352 int is_exported(const char *name, const struct module *mod);
354 extern void __module_put_and_exit(struct module *mod, long code)
355 __attribute__((noreturn));
356 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
358 #ifdef CONFIG_MODULE_UNLOAD
359 unsigned int module_refcount(struct module *mod);
360 void __symbol_put(const char *symbol);
361 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
362 void symbol_put_addr(void *addr);
364 /* Sometimes we know we already have a refcount, and it's easier not
365 to handle the error case (which only happens with rmmod --wait). */
366 static inline void __module_get(struct module *module)
368 if (module) {
369 BUG_ON(module_refcount(module) == 0);
370 local_inc(&module->ref[get_cpu()].count);
371 put_cpu();
375 static inline int try_module_get(struct module *module)
377 int ret = 1;
379 if (module) {
380 unsigned int cpu = get_cpu();
381 if (likely(module_is_live(module)))
382 local_inc(&module->ref[cpu].count);
383 else
384 ret = 0;
385 put_cpu();
387 return ret;
390 static inline void module_put(struct module *module)
392 if (module) {
393 unsigned int cpu = get_cpu();
394 local_dec(&module->ref[cpu].count);
395 /* Maybe they're waiting for us to drop reference? */
396 if (unlikely(!module_is_live(module)))
397 wake_up_process(module->waiter);
398 put_cpu();
402 #else /*!CONFIG_MODULE_UNLOAD*/
403 static inline int try_module_get(struct module *module)
405 return !module || module_is_live(module);
407 static inline void module_put(struct module *module)
410 static inline void __module_get(struct module *module)
413 #define symbol_put(x) do { } while(0)
414 #define symbol_put_addr(p) do { } while(0)
416 #endif /* CONFIG_MODULE_UNLOAD */
418 /* This is a #define so the string doesn't get put in every .o file */
419 #define module_name(mod) \
420 ({ \
421 struct module *__mod = (mod); \
422 __mod ? __mod->name : "kernel"; \
425 #define __unsafe(mod) \
426 do { \
427 if (mod && !(mod)->unsafe) { \
428 printk(KERN_WARNING \
429 "Module %s cannot be unloaded due to unsafe usage in" \
430 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
431 (mod)->unsafe = 1; \
433 } while(0)
435 /* For kallsyms to ask for address resolution. NULL means not found. */
436 const char *module_address_lookup(unsigned long addr,
437 unsigned long *symbolsize,
438 unsigned long *offset,
439 char **modname);
441 /* For extable.c to search modules' exception tables. */
442 const struct exception_table_entry *search_module_extables(unsigned long addr);
444 int register_module_notifier(struct notifier_block * nb);
445 int unregister_module_notifier(struct notifier_block * nb);
447 extern void print_modules(void);
448 #else /* !CONFIG_MODULES... */
449 #define EXPORT_SYMBOL(sym)
450 #define EXPORT_SYMBOL_GPL(sym)
451 #define EXPORT_SYMBOL_NOVERS(sym)
453 /* Given an address, look for it in the exception tables. */
454 static inline const struct exception_table_entry *
455 search_module_extables(unsigned long addr)
457 return NULL;
460 /* Is this address in a module? */
461 static inline struct module *module_text_address(unsigned long addr)
463 return NULL;
466 /* Is this address in a module? (don't take a lock, we're oopsing) */
467 static inline struct module *__module_text_address(unsigned long addr)
469 return NULL;
472 /* Get/put a kernel symbol (calls should be symmetric) */
473 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
474 #define symbol_put(x) do { } while(0)
475 #define symbol_put_addr(x) do { } while(0)
477 static inline void __module_get(struct module *module)
481 static inline int try_module_get(struct module *module)
483 return 1;
486 static inline void module_put(struct module *module)
490 #define module_name(mod) "kernel"
492 #define __unsafe(mod)
494 /* For kallsyms to ask for address resolution. NULL means not found. */
495 static inline const char *module_address_lookup(unsigned long addr,
496 unsigned long *symbolsize,
497 unsigned long *offset,
498 char **modname)
500 return NULL;
503 static inline struct module *module_get_kallsym(unsigned int symnum,
504 unsigned long *value,
505 char *type,
506 char namebuf[128])
508 return NULL;
511 static inline unsigned long module_kallsyms_lookup_name(const char *name)
513 return 0;
516 static inline int is_exported(const char *name, const struct module *mod)
518 return 0;
521 static inline int register_module_notifier(struct notifier_block * nb)
523 /* no events will happen anyway, so this can always succeed */
524 return 0;
527 static inline int unregister_module_notifier(struct notifier_block * nb)
529 return 0;
532 #define module_put_and_exit(code) do_exit(code)
534 static inline void print_modules(void)
537 #endif /* CONFIG_MODULES */
539 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
541 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
543 struct obsolete_modparm {
544 char name[64];
545 char type[64-sizeof(void *)];
546 void *addr;
548 #ifdef MODULE
549 /* DEPRECATED: Do not use. */
550 #define MODULE_PARM(var,type) \
551 struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
552 { __stringify(var), type };
554 static inline void __deprecated MOD_INC_USE_COUNT(struct module *module)
556 __unsafe(module);
558 #if defined(CONFIG_MODULE_UNLOAD) && defined(MODULE)
559 local_inc(&module->ref[get_cpu()].count);
560 put_cpu();
561 #else
562 (void)try_module_get(module);
563 #endif
566 static inline void __deprecated MOD_DEC_USE_COUNT(struct module *module)
568 module_put(module);
571 #define MOD_INC_USE_COUNT MOD_INC_USE_COUNT(THIS_MODULE)
572 #define MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT(THIS_MODULE)
573 #else
574 #define MODULE_PARM(var,type)
575 #define MOD_INC_USE_COUNT do { } while (0)
576 #define MOD_DEC_USE_COUNT do { } while (0)
577 #endif
579 #define __MODULE_STRING(x) __stringify(x)
581 /* Use symbol_get and symbol_put instead. You'll thank me. */
582 #define HAVE_INTER_MODULE
583 extern void inter_module_register(const char *, struct module *, const void *);
584 extern void inter_module_unregister(const char *);
585 extern const void *inter_module_get(const char *);
586 extern const void *inter_module_get_request(const char *, const char *);
587 extern void inter_module_put(const char *);
589 #endif /* _LINUX_MODULE_H */