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];
49 struct module_attribute {
50 struct attribute attr;
51 ssize_t (*show)(struct module_attribute *, struct module *, char *);
52 ssize_t (*store)(struct module_attribute *, struct module *,
53 const char *, size_t count);
62 /* These are either module local, or the kernel's dummy ones. */
63 extern int init_module(void);
64 extern void cleanup_module(void);
66 /* Archs provide a method of finding the correct exception table. */
67 struct exception_table_entry;
69 const struct exception_table_entry *
70 search_extable(const struct exception_table_entry *first,
71 const struct exception_table_entry *last,
73 void sort_extable(struct exception_table_entry *start,
74 struct exception_table_entry *finish);
75 void sort_main_extable(void);
77 extern struct subsystem module_subsys;
80 #define ___module_cat(a,b) __mod_ ## a ## b
81 #define __module_cat(a,b) ___module_cat(a,b)
82 #define __MODULE_INFO(tag, name, info) \
83 static const char __module_cat(name,__LINE__)[] \
85 __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info
87 #define MODULE_GENERIC_TABLE(gtype,name) \
88 extern const struct gtype##_id __mod_##gtype##_table \
89 __attribute__ ((unused, alias(__stringify(name))))
91 extern struct module __this_module;
92 #define THIS_MODULE (&__this_module)
96 #define MODULE_GENERIC_TABLE(gtype,name)
97 #define __MODULE_INFO(tag, name, info)
98 #define THIS_MODULE ((struct module *)0)
101 /* Generic info of form tag = "info" */
102 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
104 /* For userspace: you can also call me... */
105 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
108 * The following license idents are currently accepted as indicating free
111 * "GPL" [GNU Public License v2 or later]
112 * "GPL v2" [GNU Public License v2]
113 * "GPL and additional rights" [GNU Public License v2 rights and more]
114 * "Dual BSD/GPL" [GNU Public License v2
115 * or BSD license choice]
116 * "Dual MPL/GPL" [GNU Public License v2
117 * or Mozilla license choice]
119 * The following other idents are available
121 * "Proprietary" [Non free products]
123 * There are dual licensed components, but when running with Linux it is the
124 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
125 * is a GPL combined work.
127 * This exists for several reasons
128 * 1. So modinfo can show license info for users wanting to vet their setup
130 * 2. So the community can ignore bug reports including proprietary modules
131 * 3. So vendors can do likewise based on their own policies
133 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
135 /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
136 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
138 /* What your module does. */
139 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
141 /* One for each parameter, describing how to use it. Some files do
142 multiple of these per line, so can't just use MODULE_INFO. */
143 #define MODULE_PARM_DESC(_parm, desc) \
144 __MODULE_INFO(parm, _parm, #_parm ":" desc)
146 #define MODULE_DEVICE_TABLE(type,name) \
147 MODULE_GENERIC_TABLE(type##_device,name)
149 /* Version of form [<epoch>:]<version>[-<extra-version>].
150 Or for CVS/RCS ID version, everything but the number is stripped.
151 <epoch>: A (small) unsigned integer which allows you to start versions
152 anew. If not mentioned, it's zero. eg. "2:1.0" is after
154 <version>: The <version> may contain only alphanumerics and the
155 character `.'. Ordered by numeric sort for numeric parts,
156 ascii sort for ascii parts (as per RPM or DEB algorithm).
157 <extraversion>: Like <version>, but inserted for local
158 customizations, eg "rh3" or "rusty1".
160 Using this automatically adds a checksum of the .c files and the
161 local headers in "srcversion".
163 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
165 /* Given an address, look for it in the exception tables */
166 const struct exception_table_entry *search_exception_tables(unsigned long add);
168 struct notifier_block;
170 #ifdef CONFIG_MODULES
172 /* Get/put a kernel symbol (calls must be symmetric) */
173 void *__symbol_get(const char *symbol);
174 void *__symbol_get_gpl(const char *symbol);
175 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
178 #ifdef CONFIG_MODVERSIONS
179 /* Mark the CRC weak since genksyms apparently decides not to
180 * generate a checksums for some symbols */
181 #define __CRC_SYMBOL(sym, sec) \
182 extern void *__crc_##sym __attribute__((weak)); \
183 static const unsigned long __kcrctab_##sym \
185 __attribute__((section("__kcrctab" sec), unused)) \
186 = (unsigned long) &__crc_##sym;
188 #define __CRC_SYMBOL(sym, sec)
191 /* For every exported symbol, place a struct in the __ksymtab section */
192 #define __EXPORT_SYMBOL(sym, sec) \
193 __CRC_SYMBOL(sym, sec) \
194 static const char __kstrtab_##sym[] \
195 __attribute__((section("__ksymtab_strings"))) \
196 = MODULE_SYMBOL_PREFIX #sym; \
197 static const struct kernel_symbol __ksymtab_##sym \
199 __attribute__((section("__ksymtab" sec), unused)) \
200 = { (unsigned long)&sym, __kstrtab_##sym }
202 #define EXPORT_SYMBOL(sym) \
203 __EXPORT_SYMBOL(sym, "")
205 #define EXPORT_SYMBOL_GPL(sym) \
206 __EXPORT_SYMBOL(sym, "_gpl")
213 } ____cacheline_aligned;
222 /* Similar stuff for section attributes. */
223 #define MODULE_SECT_NAME_LEN 32
224 struct module_sect_attr
226 struct module_attribute mattr;
227 char name[MODULE_SECT_NAME_LEN];
228 unsigned long address;
231 struct module_sect_attrs
233 struct attribute_group grp;
234 struct module_sect_attr attrs[0];
237 struct module_param_attrs;
241 enum module_state state;
243 /* Member of list of modules */
244 struct list_head list;
246 /* Unique handle for this module */
247 char name[MODULE_NAME_LEN];
250 struct module_kobject mkobj;
251 struct module_param_attrs *param_attrs;
253 /* Exported symbols */
254 const struct kernel_symbol *syms;
255 unsigned int num_syms;
256 const unsigned long *crcs;
258 /* GPL-only exported symbols. */
259 const struct kernel_symbol *gpl_syms;
260 unsigned int num_gpl_syms;
261 const unsigned long *gpl_crcs;
263 /* Exception table */
264 unsigned int num_exentries;
265 const struct exception_table_entry *extable;
267 /* Startup function. */
270 /* If this is non-NULL, vfree after init() returns */
273 /* Here is the actual code + data, vfree'd on unload. */
276 /* Here are the sizes of the init and core sections */
277 unsigned long init_size, core_size;
279 /* The size of the executable code in each section. */
280 unsigned long init_text_size, core_text_size;
282 /* Arch-specific module values */
283 struct mod_arch_specific arch;
285 /* Am I unsafe to unload? */
288 /* Am I GPL-compatible */
291 #ifdef CONFIG_MODULE_UNLOAD
292 /* Reference counts */
293 struct module_ref ref[NR_CPUS];
295 /* What modules depend on me? */
296 struct list_head modules_which_use_me;
298 /* Who is waiting for us to be unloaded */
299 struct task_struct *waiter;
301 /* Destruction function. */
305 #ifdef CONFIG_KALLSYMS
306 /* We keep the symbol and string tables for kallsyms. */
308 unsigned long num_symtab;
311 /* Section attributes */
312 struct module_sect_attrs *sect_attrs;
318 /* The command line arguments (may be mangled). People like
319 keeping pointers to this stuff */
323 /* FIXME: It'd be nice to isolate modules during init, too, so they
324 aren't used before they (may) fail. But presently too much code
325 (IDE & SCSI) require entry into the module during init.*/
326 static inline int module_is_live(struct module *mod)
328 return mod->state != MODULE_STATE_GOING;
331 /* Is this address in a module? (second is with no locks, for oops) */
332 struct module *module_text_address(unsigned long addr);
333 struct module *__module_text_address(unsigned long addr);
335 /* Returns module and fills in value, defined and namebuf, or NULL if
336 symnum out of range. */
337 struct module *module_get_kallsym(unsigned int symnum,
338 unsigned long *value,
342 /* Look for this name: can be of form module:name. */
343 unsigned long module_kallsyms_lookup_name(const char *name);
345 int is_exported(const char *name, const struct module *mod);
347 extern void __module_put_and_exit(struct module *mod, long code)
348 __attribute__((noreturn));
349 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
351 #ifdef CONFIG_MODULE_UNLOAD
352 unsigned int module_refcount(struct module *mod);
353 void __symbol_put(const char *symbol);
354 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
355 void symbol_put_addr(void *addr);
357 /* Sometimes we know we already have a refcount, and it's easier not
358 to handle the error case (which only happens with rmmod --wait). */
359 static inline void __module_get(struct module *module)
362 BUG_ON(module_refcount(module) == 0);
363 local_inc(&module->ref[get_cpu()].count);
368 static inline int try_module_get(struct module *module)
373 unsigned int cpu = get_cpu();
374 if (likely(module_is_live(module)))
375 local_inc(&module->ref[cpu].count);
383 static inline void module_put(struct module *module)
386 unsigned int cpu = get_cpu();
387 local_dec(&module->ref[cpu].count);
388 /* Maybe they're waiting for us to drop reference? */
389 if (unlikely(!module_is_live(module)))
390 wake_up_process(module->waiter);
395 #else /*!CONFIG_MODULE_UNLOAD*/
396 static inline int try_module_get(struct module *module)
398 return !module || module_is_live(module);
400 static inline void module_put(struct module *module)
403 static inline void __module_get(struct module *module)
406 #define symbol_put(x) do { } while(0)
407 #define symbol_put_addr(p) do { } while(0)
409 #endif /* CONFIG_MODULE_UNLOAD */
411 /* This is a #define so the string doesn't get put in every .o file */
412 #define module_name(mod) \
414 struct module *__mod = (mod); \
415 __mod ? __mod->name : "kernel"; \
418 #define __unsafe(mod) \
420 if (mod && !(mod)->unsafe) { \
421 printk(KERN_WARNING \
422 "Module %s cannot be unloaded due to unsafe usage in" \
423 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
428 /* For kallsyms to ask for address resolution. NULL means not found. */
429 const char *module_address_lookup(unsigned long addr,
430 unsigned long *symbolsize,
431 unsigned long *offset,
434 /* For extable.c to search modules' exception tables. */
435 const struct exception_table_entry *search_module_extables(unsigned long addr);
437 int register_module_notifier(struct notifier_block * nb);
438 int unregister_module_notifier(struct notifier_block * nb);
440 extern void print_modules(void);
442 struct device_driver;
443 void module_add_driver(struct module *, struct device_driver *);
444 void module_remove_driver(struct device_driver *);
446 #else /* !CONFIG_MODULES... */
447 #define EXPORT_SYMBOL(sym)
448 #define EXPORT_SYMBOL_GPL(sym)
450 /* Given an address, look for it in the exception tables. */
451 static inline const struct exception_table_entry *
452 search_module_extables(unsigned long addr)
457 /* Is this address in a module? */
458 static inline struct module *module_text_address(unsigned long addr)
463 /* Is this address in a module? (don't take a lock, we're oopsing) */
464 static inline struct module *__module_text_address(unsigned long addr)
469 /* Get/put a kernel symbol (calls should be symmetric) */
470 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
471 #define symbol_put(x) do { } while(0)
472 #define symbol_put_addr(x) do { } while(0)
474 static inline void __module_get(struct module *module)
478 static inline int try_module_get(struct module *module)
483 static inline void module_put(struct module *module)
487 #define module_name(mod) "kernel"
489 #define __unsafe(mod)
491 /* For kallsyms to ask for address resolution. NULL means not found. */
492 static inline const char *module_address_lookup(unsigned long addr,
493 unsigned long *symbolsize,
494 unsigned long *offset,
500 static inline struct module *module_get_kallsym(unsigned int symnum,
501 unsigned long *value,
508 static inline unsigned long module_kallsyms_lookup_name(const char *name)
513 static inline int is_exported(const char *name, const struct module *mod)
518 static inline int register_module_notifier(struct notifier_block * nb)
520 /* no events will happen anyway, so this can always succeed */
524 static inline int unregister_module_notifier(struct notifier_block * nb)
529 #define module_put_and_exit(code) do_exit(code)
531 static inline void print_modules(void)
535 struct device_driver;
538 static inline void module_add_driver(struct module *module, struct device_driver *driver)
542 static inline void module_remove_driver(struct device_driver *driver)
546 #endif /* CONFIG_MODULES */
548 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
550 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
552 struct obsolete_modparm {
554 char type[64-sizeof(void *)];
558 static inline void MODULE_PARM_(void) { }
560 /* DEPRECATED: Do not use. */
561 #define MODULE_PARM(var,type) \
562 struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
563 { __stringify(var), type, &MODULE_PARM_ };
565 #define MODULE_PARM(var,type) static void __attribute__((__unused__)) *__parm_##var = &MODULE_PARM_;
568 #define __MODULE_STRING(x) __stringify(x)
570 /* Use symbol_get and symbol_put instead. You'll thank me. */
571 #define HAVE_INTER_MODULE
572 extern void __deprecated inter_module_register(const char *,
573 struct module *, const void *);
574 extern void __deprecated inter_module_unregister(const char *);
575 extern const void * __deprecated inter_module_get(const char *);
576 extern const void * __deprecated inter_module_get_request(const char *,
578 extern void __deprecated inter_module_put(const char *);
580 #endif /* _LINUX_MODULE_H */