Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / include / linux / module.h
blob6dad1479105f51a9864601b0d6624f04e7417bcf
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>
20 #include <asm/module.h>
21 #include <asm/uaccess.h> /* For struct exception_table_entry */
23 /* Not Yet Implemented */
24 #define MODULE_AUTHOR(name)
25 #define MODULE_DESCRIPTION(desc)
26 #define MODULE_SUPPORTED_DEVICE(name)
27 #define MODULE_PARM_DESC(var,desc)
28 #define print_modules()
30 /* v850 toolchain uses a `_' prefix for all user symbols */
31 #ifndef MODULE_SYMBOL_PREFIX
32 #define MODULE_SYMBOL_PREFIX ""
33 #endif
35 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
36 struct kernel_symbol
38 unsigned long value;
39 const char *name;
42 /* These are either module local, or the kernel's dummy ones. */
43 extern int init_module(void);
44 extern void cleanup_module(void);
46 /* Archs provide a method of finding the correct exception table. */
47 const struct exception_table_entry *
48 search_extable(const struct exception_table_entry *first,
49 const struct exception_table_entry *last,
50 unsigned long value);
52 #ifdef MODULE
54 /* For replacement modutils, use an alias not a pointer. */
55 #define MODULE_GENERIC_TABLE(gtype,name) \
56 static const unsigned long __module_##gtype##_size \
57 __attribute__ ((unused)) = sizeof(struct gtype##_id); \
58 static const struct gtype##_id * __module_##gtype##_table \
59 __attribute__ ((unused)) = name; \
60 extern const struct gtype##_id __mod_##gtype##_table \
61 __attribute__ ((unused, alias(__stringify(name))))
63 #define THIS_MODULE (&__this_module)
64 #define MOD_INC_USE_COUNT _MOD_INC_USE_COUNT(THIS_MODULE)
65 #define MOD_DEC_USE_COUNT __MOD_DEC_USE_COUNT(THIS_MODULE)
68 * The following license idents are currently accepted as indicating free
69 * software modules
71 * "GPL" [GNU Public License v2 or later]
72 * "GPL v2" [GNU Public License v2]
73 * "GPL and additional rights" [GNU Public License v2 rights and more]
74 * "Dual BSD/GPL" [GNU Public License v2
75 * or BSD license choice]
76 * "Dual MPL/GPL" [GNU Public License v2
77 * or Mozilla license choice]
79 * The following other idents are available
81 * "Proprietary" [Non free products]
83 * There are dual licensed components, but when running with Linux it is the
84 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
85 * is a GPL combined work.
87 * This exists for several reasons
88 * 1. So modinfo can show license info for users wanting to vet their setup
89 * is free
90 * 2. So the community can ignore bug reports including proprietary modules
91 * 3. So vendors can do likewise based on their own policies
93 #define MODULE_LICENSE(license) \
94 static const char __module_license[] \
95 __attribute__((section(".init.license"))) = license
97 #else /* !MODULE */
99 #define MODULE_GENERIC_TABLE(gtype,name)
100 #define THIS_MODULE ((struct module *)0)
101 #define MOD_INC_USE_COUNT do { } while (0)
102 #define MOD_DEC_USE_COUNT do { } while (0)
103 #define MODULE_LICENSE(license)
104 #endif
106 #define MODULE_DEVICE_TABLE(type,name) \
107 MODULE_GENERIC_TABLE(type##_device,name)
109 struct kernel_symbol_group
111 /* Links us into the global symbol list */
112 struct list_head list;
114 /* Module which owns it (if any) */
115 struct module *owner;
117 /* Are we internal use only? */
118 int gplonly;
120 unsigned int num_syms;
121 const struct kernel_symbol *syms;
124 /* Given an address, look for it in the exception tables */
125 const struct exception_table_entry *search_exception_tables(unsigned long add);
127 struct exception_table
129 struct list_head list;
131 unsigned int num_entries;
132 const struct exception_table_entry *entry;
136 #ifdef CONFIG_MODULES
137 /* Get/put a kernel symbol (calls must be symmetric) */
138 void *__symbol_get(const char *symbol);
139 void *__symbol_get_gpl(const char *symbol);
140 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
142 /* For every exported symbol, place a struct in the __ksymtab section */
143 #define EXPORT_SYMBOL(sym) \
144 static const char __kstrtab_##sym[] \
145 __attribute__((section("__ksymtab_strings"))) \
146 = MODULE_SYMBOL_PREFIX #sym; \
147 static const struct kernel_symbol __ksymtab_##sym \
148 __attribute__((section("__ksymtab"))) \
149 = { (unsigned long)&sym, __kstrtab_##sym }
151 #define EXPORT_SYMBOL_NOVERS(sym) EXPORT_SYMBOL(sym)
153 #define EXPORT_SYMBOL_GPL(sym) \
154 static const char __kstrtab_##sym[] \
155 __attribute__((section("__ksymtab_strings"))) \
156 = MODULE_SYMBOL_PREFIX #sym; \
157 static const struct kernel_symbol __ksymtab_##sym \
158 __attribute__((section("__gpl_ksymtab"))) \
159 = { (unsigned long)&sym, __kstrtab_##sym }
161 struct module_ref
163 atomic_t count;
164 } ____cacheline_aligned;
166 enum module_state
168 MODULE_STATE_LIVE,
169 MODULE_STATE_COMING,
170 MODULE_STATE_GOING,
173 struct module
175 enum module_state state;
177 /* Member of list of modules */
178 struct list_head list;
180 /* Unique handle for this module */
181 char name[MODULE_NAME_LEN];
183 /* Exported symbols */
184 struct kernel_symbol_group symbols;
186 /* GPL-only exported symbols. */
187 struct kernel_symbol_group gpl_symbols;
189 /* Exception tables */
190 struct exception_table extable;
192 /* Startup function. */
193 int (*init)(void);
195 /* If this is non-NULL, vfree after init() returns */
196 void *module_init;
198 /* Here is the actual code + data, vfree'd on unload. */
199 void *module_core;
201 /* Here are the sizes of the init and core sections */
202 unsigned long init_size, core_size;
204 /* Arch-specific module values */
205 struct mod_arch_specific arch;
207 /* Am I unsafe to unload? */
208 int unsafe;
210 /* Am I GPL-compatible */
211 int license_gplok;
213 #ifdef CONFIG_MODULE_UNLOAD
214 /* Reference counts */
215 struct module_ref ref[NR_CPUS];
217 /* What modules depend on me? */
218 struct list_head modules_which_use_me;
220 /* Who is waiting for us to be unloaded */
221 struct task_struct *waiter;
223 /* Destruction function. */
224 void (*exit)(void);
225 #endif
227 #ifdef CONFIG_KALLSYMS
228 /* We keep the symbol and string tables for kallsyms. */
229 Elf_Sym *symtab;
230 unsigned long num_syms;
231 char *strtab;
232 #endif
234 /* The command line arguments (may be mangled). People like
235 keeping pointers to this stuff */
236 char *args;
239 /* FIXME: It'd be nice to isolate modules during init, too, so they
240 aren't used before they (may) fail. But presently too much code
241 (IDE & SCSI) require entry into the module during init.*/
242 static inline int module_is_live(struct module *mod)
244 return mod->state != MODULE_STATE_GOING;
247 /* Is this address in a module? */
248 int module_text_address(unsigned long addr);
250 #ifdef CONFIG_MODULE_UNLOAD
252 void __symbol_put(const char *symbol);
253 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
254 void symbol_put_addr(void *addr);
256 /* We only need protection against local interrupts. */
257 #ifndef __HAVE_ARCH_LOCAL_INC
258 #define local_inc(x) atomic_inc(x)
259 #define local_dec(x) atomic_dec(x)
260 #endif
262 static inline int try_module_get(struct module *module)
264 int ret = 1;
266 if (module) {
267 unsigned int cpu = get_cpu();
268 if (likely(module_is_live(module)))
269 local_inc(&module->ref[cpu].count);
270 else
271 ret = 0;
272 put_cpu();
274 return ret;
277 static inline void module_put(struct module *module)
279 if (module) {
280 unsigned int cpu = get_cpu();
281 local_dec(&module->ref[cpu].count);
282 /* Maybe they're waiting for us to drop reference? */
283 if (unlikely(!module_is_live(module)))
284 wake_up_process(module->waiter);
285 put_cpu();
289 #else /*!CONFIG_MODULE_UNLOAD*/
290 static inline int try_module_get(struct module *module)
292 return !module || module_is_live(module);
294 static inline void module_put(struct module *module)
297 #define symbol_put(x) do { } while(0)
298 #define symbol_put_addr(p) do { } while(0)
300 #endif /* CONFIG_MODULE_UNLOAD */
302 /* This is a #define so the string doesn't get put in every .o file */
303 #define module_name(mod) \
304 ({ \
305 struct module *__mod = (mod); \
306 __mod ? __mod->name : "kernel"; \
309 #define __unsafe(mod) \
310 do { \
311 if (mod && !(mod)->unsafe) { \
312 printk(KERN_WARNING \
313 "Module %s cannot be unloaded due to unsafe usage in" \
314 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
315 (mod)->unsafe = 1; \
317 } while(0)
319 /* For kallsyms to ask for address resolution. NULL means not found. */
320 const char *module_address_lookup(unsigned long addr,
321 unsigned long *symbolsize,
322 unsigned long *offset,
323 char **modname);
325 /* For extable.c to search modules' exception tables. */
326 const struct exception_table_entry *search_module_extables(unsigned long addr);
328 #else /* !CONFIG_MODULES... */
329 #define EXPORT_SYMBOL(sym)
330 #define EXPORT_SYMBOL_GPL(sym)
331 #define EXPORT_SYMBOL_NOVERS(sym)
333 /* Given an address, look for it in the exception tables. */
334 static inline const struct exception_table_entry *
335 search_module_extables(unsigned long addr)
337 return NULL;
340 /* Is this address in a module? */
341 static inline int module_text_address(unsigned long addr)
343 return 0;
346 /* Get/put a kernel symbol (calls should be symmetric) */
347 #define symbol_get(x) (&(x))
348 #define symbol_put(x) do { } while(0)
349 #define symbol_put_addr(x) do { } while(0)
351 static inline int try_module_get(struct module *module)
353 return 1;
356 static inline void module_put(struct module *module)
360 #define module_name(mod) "kernel"
362 #define __unsafe(mod)
364 /* For kallsyms to ask for address resolution. NULL means not found. */
365 static inline const char *module_address_lookup(unsigned long addr,
366 unsigned long *symbolsize,
367 unsigned long *offset,
368 char **modname)
370 return NULL;
372 #endif /* CONFIG_MODULES */
374 #ifdef MODULE
375 extern struct module __this_module;
376 #ifdef KBUILD_MODNAME
377 /* We make the linker do some of the work. */
378 struct module __this_module
379 __attribute__((section(".gnu.linkonce.this_module"))) = {
380 .name = __stringify(KBUILD_MODNAME),
381 .symbols = { .owner = &__this_module },
382 .gpl_symbols = { .owner = &__this_module, .gplonly = 1 },
383 .init = init_module,
384 #ifdef CONFIG_MODULE_UNLOAD
385 .exit = cleanup_module,
386 #endif
388 #endif /* KBUILD_MODNAME */
389 #endif /* MODULE */
391 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
393 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
394 static inline void __deprecated __MOD_INC_USE_COUNT(struct module *module)
396 __unsafe(module);
398 * Yes, we ignore the retval here, that's why it's deprecated.
400 try_module_get(module);
403 static inline void __deprecated __MOD_DEC_USE_COUNT(struct module *module)
405 module_put(module);
408 #define SET_MODULE_OWNER(dev) ((dev)->owner = THIS_MODULE)
410 struct obsolete_modparm {
411 char name[64];
412 char type[64-sizeof(void *)];
413 void *addr;
415 #ifdef MODULE
416 /* DEPRECATED: Do not use. */
417 #define MODULE_PARM(var,type) \
418 struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
419 { __stringify(var), type };
421 #else
422 #define MODULE_PARM(var,type)
423 #endif
425 /* People do this inside their init routines, when the module isn't
426 "live" yet. They should no longer be doing that, but
427 meanwhile... */
428 static inline void __deprecated _MOD_INC_USE_COUNT(struct module *module)
430 __unsafe(module);
432 #if defined(CONFIG_MODULE_UNLOAD) && defined(MODULE)
433 local_inc(&module->ref[get_cpu()].count);
434 put_cpu();
435 #else
436 try_module_get(module);
437 #endif
439 #define EXPORT_NO_SYMBOLS
440 #define __MODULE_STRING(x) __stringify(x)
443 * The exception and symbol tables, and the lock
444 * to protect them.
446 extern spinlock_t modlist_lock;
447 extern struct list_head extables;
448 extern struct list_head symbols;
450 /* Use symbol_get and symbol_put instead. You'll thank me. */
451 #define HAVE_INTER_MODULE
452 extern void inter_module_register(const char *, struct module *, const void *);
453 extern void inter_module_unregister(const char *);
454 extern const void *inter_module_get(const char *);
455 extern const void *inter_module_get_request(const char *, const char *);
456 extern void inter_module_put(const char *);
458 #endif /* _LINUX_MODULE_H */