1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H
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
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 ""
33 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
41 struct modversion_info
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,
58 void sort_extable(struct exception_table_entry *start,
59 struct exception_table_entry *finish);
60 void sort_main_extable(void);
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__)[] \
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)
79 #define MODULE_GENERIC_TABLE(gtype,name)
80 #define __MODULE_INFO(tag, name, info)
81 #define THIS_MODULE ((struct module *)0)
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
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
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
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)))
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 \
170 __attribute__((section("__kcrctab" sec), unused)) \
171 = (unsigned long) &__crc_##sym;
173 #define __CRC_SYMBOL(sym, sec)
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 \
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")
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)
202 } ____cacheline_aligned;
212 struct module_attribute
214 struct attribute attr;
215 struct kernel_param *param;
218 struct module_kobject
220 /* Everyone should have one of these. */
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
240 struct module_sect_attr attrs[0];
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];
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. */
274 /* If this is non-NULL, vfree after init() returns */
277 /* Here is the actual code + data, vfree'd on unload. */
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? */
292 /* Am I GPL-compatible */
295 /* Am I gpg signed */
298 #ifdef CONFIG_MODULE_UNLOAD
299 /* Reference counts */
300 struct module_ref ref[NR_CPUS];
302 /* What modules depend on me? */
303 struct list_head modules_which_use_me;
305 /* Who is waiting for us to be unloaded */
306 struct task_struct *waiter;
308 /* Destruction function. */
311 /* Fake kernel param for refcnt. */
312 struct kernel_param refcnt_param;
315 #ifdef CONFIG_KALLSYMS
316 /* We keep the symbol and string tables for kallsyms. */
318 unsigned long num_symtab;
321 /* Section attributes */
322 struct module_sections *sect_attrs;
328 /* The command line arguments (may be mangled). People like
329 keeping pointers to this stuff */
333 /* FIXME: It'd be nice to isolate modules during init, too, so they
334 aren't used before they (may) fail. But presently too much code
335 (IDE & SCSI) require entry into the module during init.*/
336 static inline int module_is_live(struct module *mod)
338 return mod->state != MODULE_STATE_GOING;
341 /* Is this address in a module? (second is with no locks, for oops) */
342 struct module *module_text_address(unsigned long addr);
343 struct module *__module_text_address(unsigned long addr);
345 /* Returns module and fills in value, defined and namebuf, or NULL if
346 symnum out of range. */
347 struct module *module_get_kallsym(unsigned int symnum,
348 unsigned long *value,
352 /* Look for this name: can be of form module:name. */
353 unsigned long module_kallsyms_lookup_name(const char *name);
355 int is_exported(const char *name, const struct module *mod);
357 extern void __module_put_and_exit(struct module *mod, long code)
358 __attribute__((noreturn));
359 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
361 #ifdef CONFIG_MODULE_UNLOAD
362 unsigned int module_refcount(struct module *mod);
363 void __symbol_put(const char *symbol);
364 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
365 void symbol_put_addr(void *addr);
367 /* Sometimes we know we already have a refcount, and it's easier not
368 to handle the error case (which only happens with rmmod --wait). */
369 static inline void __module_get(struct module *module)
372 BUG_ON(module_refcount(module) == 0);
373 local_inc(&module->ref[get_cpu()].count);
378 static inline int try_module_get(struct module *module)
383 unsigned int cpu = get_cpu();
384 if (likely(module_is_live(module)))
385 local_inc(&module->ref[cpu].count);
393 static inline void module_put(struct module *module)
396 unsigned int cpu = get_cpu();
397 local_dec(&module->ref[cpu].count);
398 /* Maybe they're waiting for us to drop reference? */
399 if (unlikely(!module_is_live(module)))
400 wake_up_process(module->waiter);
405 #else /*!CONFIG_MODULE_UNLOAD*/
406 static inline int try_module_get(struct module *module)
408 return !module || module_is_live(module);
410 static inline void module_put(struct module *module)
413 static inline void __module_get(struct module *module)
416 #define symbol_put(x) do { } while(0)
417 #define symbol_put_addr(p) do { } while(0)
419 #endif /* CONFIG_MODULE_UNLOAD */
421 /* This is a #define so the string doesn't get put in every .o file */
422 #define module_name(mod) \
424 struct module *__mod = (mod); \
425 __mod ? __mod->name : "kernel"; \
428 #define __unsafe(mod) \
430 if (mod && !(mod)->unsafe) { \
431 printk(KERN_WARNING \
432 "Module %s cannot be unloaded due to unsafe usage in" \
433 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
438 /* For kallsyms to ask for address resolution. NULL means not found. */
439 const char *module_address_lookup(unsigned long addr,
440 unsigned long *symbolsize,
441 unsigned long *offset,
444 /* For extable.c to search modules' exception tables. */
445 const struct exception_table_entry *search_module_extables(unsigned long addr);
447 int register_module_notifier(struct notifier_block * nb);
448 int unregister_module_notifier(struct notifier_block * nb);
450 extern void print_modules(void);
451 #else /* !CONFIG_MODULES... */
452 #define EXPORT_SYMBOL(sym)
453 #define EXPORT_SYMBOL_GPL(sym)
454 #define EXPORT_SYMBOL_NOVERS(sym)
456 /* Given an address, look for it in the exception tables. */
457 static inline const struct exception_table_entry *
458 search_module_extables(unsigned long addr)
463 /* Is this address in a module? */
464 static inline struct module *module_text_address(unsigned long addr)
469 /* Is this address in a module? (don't take a lock, we're oopsing) */
470 static inline struct module *__module_text_address(unsigned long addr)
475 /* Get/put a kernel symbol (calls should be symmetric) */
476 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
477 #define symbol_put(x) do { } while(0)
478 #define symbol_put_addr(x) do { } while(0)
480 static inline void __module_get(struct module *module)
484 static inline int try_module_get(struct module *module)
489 static inline void module_put(struct module *module)
493 #define module_name(mod) "kernel"
495 #define __unsafe(mod)
497 /* For kallsyms to ask for address resolution. NULL means not found. */
498 static inline const char *module_address_lookup(unsigned long addr,
499 unsigned long *symbolsize,
500 unsigned long *offset,
506 static inline struct module *module_get_kallsym(unsigned int symnum,
507 unsigned long *value,
514 static inline unsigned long module_kallsyms_lookup_name(const char *name)
519 static inline int is_exported(const char *name, const struct module *mod)
524 static inline int register_module_notifier(struct notifier_block * nb)
526 /* no events will happen anyway, so this can always succeed */
530 static inline int unregister_module_notifier(struct notifier_block * nb)
535 #define module_put_and_exit(code) do_exit(code)
537 static inline void print_modules(void)
540 #endif /* CONFIG_MODULES */
542 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
544 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
546 struct obsolete_modparm {
548 char type[64-sizeof(void *)];
552 static inline void __deprecated MODULE_PARM_(void) { }
554 /* DEPRECATED: Do not use. */
555 #define MODULE_PARM(var,type) \
556 struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
557 { __stringify(var), type, &MODULE_PARM_ };
559 #define MODULE_PARM(var,type) static void __attribute__((__unused__)) *__parm_##var = &MODULE_PARM_;
562 #define __MODULE_STRING(x) __stringify(x)
564 /* Use symbol_get and symbol_put instead. You'll thank me. */
565 #define HAVE_INTER_MODULE
566 extern void __deprecated inter_module_register(const char *,
567 struct module *, const void *);
568 extern void __deprecated inter_module_unregister(const char *);
569 extern const void * __deprecated inter_module_get(const char *);
570 extern const void * __deprecated inter_module_get_request(const char *,
572 extern void __deprecated inter_module_put(const char *);
574 #endif /* _LINUX_MODULE_H */