Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / include / linux / module.h
blobbbd77fa05b100ad05a18c6a33177519f3824f384
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/list.h>
10 #include <linux/stat.h>
11 #include <linux/compiler.h>
12 #include <linux/cache.h>
13 #include <linux/kmod.h>
14 #include <linux/elf.h>
15 #include <linux/stringify.h>
16 #include <linux/kobject.h>
17 #include <linux/moduleparam.h>
18 #include <linux/tracepoint.h>
19 #include <linux/export.h>
21 #include <linux/percpu.h>
22 #include <asm/module.h>
24 #include <trace/events/module.h>
26 /* Not Yet Implemented */
27 #define MODULE_SUPPORTED_DEVICE(name)
29 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
31 struct modversion_info
33 unsigned long crc;
34 char name[MODULE_NAME_LEN];
37 struct module;
39 struct module_kobject {
40 struct kobject kobj;
41 struct module *mod;
42 struct kobject *drivers_dir;
43 struct module_param_attrs *mp;
46 struct module_attribute {
47 struct attribute attr;
48 ssize_t (*show)(struct module_attribute *, struct module_kobject *,
49 char *);
50 ssize_t (*store)(struct module_attribute *, struct module_kobject *,
51 const char *, size_t count);
52 void (*setup)(struct module *, const char *);
53 int (*test)(struct module *);
54 void (*free)(struct module *);
57 struct module_version_attribute {
58 struct module_attribute mattr;
59 const char *module_name;
60 const char *version;
61 } __attribute__ ((__aligned__(sizeof(void *))));
63 extern ssize_t __modver_version_show(struct module_attribute *,
64 struct module_kobject *, char *);
66 extern struct module_attribute module_uevent;
68 /* These are either module local, or the kernel's dummy ones. */
69 extern int init_module(void);
70 extern void cleanup_module(void);
72 /* Archs provide a method of finding the correct exception table. */
73 struct exception_table_entry;
75 const struct exception_table_entry *
76 search_extable(const struct exception_table_entry *first,
77 const struct exception_table_entry *last,
78 unsigned long value);
79 void sort_extable(struct exception_table_entry *start,
80 struct exception_table_entry *finish);
81 void sort_main_extable(void);
82 void trim_init_extable(struct module *m);
84 #ifdef MODULE
85 #define MODULE_GENERIC_TABLE(gtype,name) \
86 extern const struct gtype##_id __mod_##gtype##_table \
87 __attribute__ ((unused, alias(__stringify(name))))
89 #else /* !MODULE */
90 #define MODULE_GENERIC_TABLE(gtype,name)
91 #endif
93 /* Generic info of form tag = "info" */
94 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
96 /* For userspace: you can also call me... */
97 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
100 * The following license idents are currently accepted as indicating free
101 * software modules
103 * "GPL" [GNU Public License v2 or later]
104 * "GPL v2" [GNU Public License v2]
105 * "GPL and additional rights" [GNU Public License v2 rights and more]
106 * "Dual BSD/GPL" [GNU Public License v2
107 * or BSD license choice]
108 * "Dual MIT/GPL" [GNU Public License v2
109 * or MIT license choice]
110 * "Dual MPL/GPL" [GNU Public License v2
111 * or Mozilla license choice]
113 * The following other idents are available
115 * "Proprietary" [Non free products]
117 * There are dual licensed components, but when running with Linux it is the
118 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
119 * is a GPL combined work.
121 * This exists for several reasons
122 * 1. So modinfo can show license info for users wanting to vet their setup
123 * is free
124 * 2. So the community can ignore bug reports including proprietary modules
125 * 3. So vendors can do likewise based on their own policies
127 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
130 * Author(s), use "Name <email>" or just "Name", for multiple
131 * authors use multiple MODULE_AUTHOR() statements/lines.
133 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
135 /* What your module does. */
136 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
138 /* One for each parameter, describing how to use it. Some files do
139 multiple of these per line, so can't just use MODULE_INFO. */
140 #define MODULE_PARM_DESC(_parm, desc) \
141 __MODULE_INFO(parm, _parm, #_parm ":" desc)
143 #define MODULE_DEVICE_TABLE(type,name) \
144 MODULE_GENERIC_TABLE(type##_device,name)
146 /* Version of form [<epoch>:]<version>[-<extra-version>].
147 Or for CVS/RCS ID version, everything but the number is stripped.
148 <epoch>: A (small) unsigned integer which allows you to start versions
149 anew. If not mentioned, it's zero. eg. "2:1.0" is after
150 "1:2.0".
151 <version>: The <version> may contain only alphanumerics and the
152 character `.'. Ordered by numeric sort for numeric parts,
153 ascii sort for ascii parts (as per RPM or DEB algorithm).
154 <extraversion>: Like <version>, but inserted for local
155 customizations, eg "rh3" or "rusty1".
157 Using this automatically adds a checksum of the .c files and the
158 local headers in "srcversion".
161 #if defined(MODULE) || !defined(CONFIG_SYSFS)
162 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
163 #else
164 #define MODULE_VERSION(_version) \
165 static struct module_version_attribute ___modver_attr = { \
166 .mattr = { \
167 .attr = { \
168 .name = "version", \
169 .mode = S_IRUGO, \
170 }, \
171 .show = __modver_version_show, \
172 }, \
173 .module_name = KBUILD_MODNAME, \
174 .version = _version, \
175 }; \
176 static const struct module_version_attribute \
177 __used __attribute__ ((__section__ ("__modver"))) \
178 * __moduleparam_const __modver_attr = &___modver_attr
179 #endif
181 /* Optional firmware file (or files) needed by the module
182 * format is simply firmware file name. Multiple firmware
183 * files require multiple MODULE_FIRMWARE() specifiers */
184 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
186 /* Given an address, look for it in the exception tables */
187 const struct exception_table_entry *search_exception_tables(unsigned long add);
189 struct notifier_block;
191 #ifdef CONFIG_MODULES
193 extern int modules_disabled; /* for sysctl */
194 /* Get/put a kernel symbol (calls must be symmetric) */
195 void *__symbol_get(const char *symbol);
196 void *__symbol_get_gpl(const char *symbol);
197 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
199 /* modules using other modules: kdb wants to see this. */
200 struct module_use {
201 struct list_head source_list;
202 struct list_head target_list;
203 struct module *source, *target;
206 enum module_state
208 MODULE_STATE_LIVE,
209 MODULE_STATE_COMING,
210 MODULE_STATE_GOING,
213 struct module
215 enum module_state state;
217 /* Member of list of modules */
218 struct list_head list;
220 /* Unique handle for this module */
221 char name[MODULE_NAME_LEN];
223 /* Sysfs stuff. */
224 struct module_kobject mkobj;
225 struct module_attribute *modinfo_attrs;
226 const char *version;
227 const char *srcversion;
228 struct kobject *holders_dir;
230 /* Exported symbols */
231 const struct kernel_symbol *syms;
232 const unsigned long *crcs;
233 unsigned int num_syms;
235 /* Kernel parameters. */
236 struct kernel_param *kp;
237 unsigned int num_kp;
239 /* GPL-only exported symbols. */
240 unsigned int num_gpl_syms;
241 const struct kernel_symbol *gpl_syms;
242 const unsigned long *gpl_crcs;
244 #ifdef CONFIG_UNUSED_SYMBOLS
245 /* unused exported symbols. */
246 const struct kernel_symbol *unused_syms;
247 const unsigned long *unused_crcs;
248 unsigned int num_unused_syms;
250 /* GPL-only, unused exported symbols. */
251 unsigned int num_unused_gpl_syms;
252 const struct kernel_symbol *unused_gpl_syms;
253 const unsigned long *unused_gpl_crcs;
254 #endif
256 /* symbols that will be GPL-only in the near future. */
257 const struct kernel_symbol *gpl_future_syms;
258 const unsigned long *gpl_future_crcs;
259 unsigned int num_gpl_future_syms;
261 /* Exception table */
262 unsigned int num_exentries;
263 struct exception_table_entry *extable;
265 /* Startup function. */
266 int (*init)(void);
268 /* If this is non-NULL, vfree after init() returns */
269 void *module_init;
271 /* Here is the actual code + data, vfree'd on unload. */
272 void *module_core;
274 /* Here are the sizes of the init and core sections */
275 unsigned int init_size, core_size;
277 /* The size of the executable code in each section. */
278 unsigned int init_text_size, core_text_size;
280 /* Size of RO sections of the module (text+rodata) */
281 unsigned int init_ro_size, core_ro_size;
283 /* Arch-specific module values */
284 struct mod_arch_specific arch;
286 unsigned int taints; /* same bits as kernel:tainted */
288 #ifdef CONFIG_GENERIC_BUG
289 /* Support for BUG */
290 unsigned num_bugs;
291 struct list_head bug_list;
292 struct bug_entry *bug_table;
293 #endif
295 #ifdef CONFIG_KALLSYMS
297 * We keep the symbol and string tables for kallsyms.
298 * The core_* fields below are temporary, loader-only (they
299 * could really be discarded after module init).
301 Elf_Sym *symtab, *core_symtab;
302 unsigned int num_symtab, core_num_syms;
303 char *strtab, *core_strtab;
305 /* Section attributes */
306 struct module_sect_attrs *sect_attrs;
308 /* Notes attributes */
309 struct module_notes_attrs *notes_attrs;
310 #endif
312 /* The command line arguments (may be mangled). People like
313 keeping pointers to this stuff */
314 char *args;
316 #ifdef CONFIG_SMP
317 /* Per-cpu data. */
318 void __percpu *percpu;
319 unsigned int percpu_size;
320 #endif
322 #ifdef CONFIG_TRACEPOINTS
323 unsigned int num_tracepoints;
324 struct tracepoint * const *tracepoints_ptrs;
325 #endif
326 #ifdef HAVE_JUMP_LABEL
327 struct jump_entry *jump_entries;
328 unsigned int num_jump_entries;
329 #endif
330 #ifdef CONFIG_TRACING
331 unsigned int num_trace_bprintk_fmt;
332 const char **trace_bprintk_fmt_start;
333 #endif
334 #ifdef CONFIG_EVENT_TRACING
335 struct ftrace_event_call **trace_events;
336 unsigned int num_trace_events;
337 #endif
338 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
339 unsigned int num_ftrace_callsites;
340 unsigned long *ftrace_callsites;
341 #endif
343 #ifdef CONFIG_MODULE_UNLOAD
344 /* What modules depend on me? */
345 struct list_head source_list;
346 /* What modules do I depend on? */
347 struct list_head target_list;
349 /* Who is waiting for us to be unloaded */
350 struct task_struct *waiter;
352 /* Destruction function. */
353 void (*exit)(void);
355 struct module_ref {
356 unsigned int incs;
357 unsigned int decs;
358 } __percpu *refptr;
359 #endif
361 #ifdef CONFIG_CONSTRUCTORS
362 /* Constructor functions. */
363 ctor_fn_t *ctors;
364 unsigned int num_ctors;
365 #endif
367 #ifndef MODULE_ARCH_INIT
368 #define MODULE_ARCH_INIT {}
369 #endif
371 extern struct mutex module_mutex;
373 /* FIXME: It'd be nice to isolate modules during init, too, so they
374 aren't used before they (may) fail. But presently too much code
375 (IDE & SCSI) require entry into the module during init.*/
376 static inline int module_is_live(struct module *mod)
378 return mod->state != MODULE_STATE_GOING;
381 struct module *__module_text_address(unsigned long addr);
382 struct module *__module_address(unsigned long addr);
383 bool is_module_address(unsigned long addr);
384 bool is_module_percpu_address(unsigned long addr);
385 bool is_module_text_address(unsigned long addr);
387 static inline int within_module_core(unsigned long addr, struct module *mod)
389 return (unsigned long)mod->module_core <= addr &&
390 addr < (unsigned long)mod->module_core + mod->core_size;
393 static inline int within_module_init(unsigned long addr, struct module *mod)
395 return (unsigned long)mod->module_init <= addr &&
396 addr < (unsigned long)mod->module_init + mod->init_size;
399 /* Search for module by name: must hold module_mutex. */
400 struct module *find_module(const char *name);
402 struct symsearch {
403 const struct kernel_symbol *start, *stop;
404 const unsigned long *crcs;
405 enum {
406 NOT_GPL_ONLY,
407 GPL_ONLY,
408 WILL_BE_GPL_ONLY,
409 } licence;
410 bool unused;
413 /* Search for an exported symbol by name. */
414 const struct kernel_symbol *find_symbol(const char *name,
415 struct module **owner,
416 const unsigned long **crc,
417 bool gplok,
418 bool warn);
420 /* Walk the exported symbol table */
421 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
422 struct module *owner,
423 void *data), void *data);
425 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
426 symnum out of range. */
427 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
428 char *name, char *module_name, int *exported);
430 /* Look for this name: can be of form module:name. */
431 unsigned long module_kallsyms_lookup_name(const char *name);
433 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
434 struct module *, unsigned long),
435 void *data);
437 extern void __module_put_and_exit(struct module *mod, long code)
438 __attribute__((noreturn));
439 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
441 #ifdef CONFIG_MODULE_UNLOAD
442 unsigned int module_refcount(struct module *mod);
443 void __symbol_put(const char *symbol);
444 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
445 void symbol_put_addr(void *addr);
447 /* Sometimes we know we already have a refcount, and it's easier not
448 to handle the error case (which only happens with rmmod --wait). */
449 static inline void __module_get(struct module *module)
451 if (module) {
452 preempt_disable();
453 __this_cpu_inc(module->refptr->incs);
454 trace_module_get(module, _THIS_IP_);
455 preempt_enable();
459 static inline int try_module_get(struct module *module)
461 int ret = 1;
463 if (module) {
464 preempt_disable();
466 if (likely(module_is_live(module))) {
467 __this_cpu_inc(module->refptr->incs);
468 trace_module_get(module, _THIS_IP_);
469 } else
470 ret = 0;
472 preempt_enable();
474 return ret;
477 extern void module_put(struct module *module);
479 #else /*!CONFIG_MODULE_UNLOAD*/
480 static inline int try_module_get(struct module *module)
482 return !module || module_is_live(module);
484 static inline void module_put(struct module *module)
487 static inline void __module_get(struct module *module)
490 #define symbol_put(x) do { } while(0)
491 #define symbol_put_addr(p) do { } while(0)
493 #endif /* CONFIG_MODULE_UNLOAD */
494 int ref_module(struct module *a, struct module *b);
496 /* This is a #define so the string doesn't get put in every .o file */
497 #define module_name(mod) \
498 ({ \
499 struct module *__mod = (mod); \
500 __mod ? __mod->name : "kernel"; \
503 /* For kallsyms to ask for address resolution. namebuf should be at
504 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
505 * found, otherwise NULL. */
506 const char *module_address_lookup(unsigned long addr,
507 unsigned long *symbolsize,
508 unsigned long *offset,
509 char **modname,
510 char *namebuf);
511 int lookup_module_symbol_name(unsigned long addr, char *symname);
512 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
514 /* For extable.c to search modules' exception tables. */
515 const struct exception_table_entry *search_module_extables(unsigned long addr);
517 int register_module_notifier(struct notifier_block * nb);
518 int unregister_module_notifier(struct notifier_block * nb);
520 extern void print_modules(void);
522 extern void module_update_tracepoints(void);
523 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
525 #else /* !CONFIG_MODULES... */
527 /* Given an address, look for it in the exception tables. */
528 static inline const struct exception_table_entry *
529 search_module_extables(unsigned long addr)
531 return NULL;
534 static inline struct module *__module_address(unsigned long addr)
536 return NULL;
539 static inline struct module *__module_text_address(unsigned long addr)
541 return NULL;
544 static inline bool is_module_address(unsigned long addr)
546 return false;
549 static inline bool is_module_percpu_address(unsigned long addr)
551 return false;
554 static inline bool is_module_text_address(unsigned long addr)
556 return false;
559 /* Get/put a kernel symbol (calls should be symmetric) */
560 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
561 #define symbol_put(x) do { } while(0)
562 #define symbol_put_addr(x) do { } while(0)
564 static inline void __module_get(struct module *module)
568 static inline int try_module_get(struct module *module)
570 return 1;
573 static inline void module_put(struct module *module)
577 #define module_name(mod) "kernel"
579 /* For kallsyms to ask for address resolution. NULL means not found. */
580 static inline const char *module_address_lookup(unsigned long addr,
581 unsigned long *symbolsize,
582 unsigned long *offset,
583 char **modname,
584 char *namebuf)
586 return NULL;
589 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
591 return -ERANGE;
594 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
596 return -ERANGE;
599 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
600 char *type, char *name,
601 char *module_name, int *exported)
603 return -ERANGE;
606 static inline unsigned long module_kallsyms_lookup_name(const char *name)
608 return 0;
611 static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
612 struct module *,
613 unsigned long),
614 void *data)
616 return 0;
619 static inline int register_module_notifier(struct notifier_block * nb)
621 /* no events will happen anyway, so this can always succeed */
622 return 0;
625 static inline int unregister_module_notifier(struct notifier_block * nb)
627 return 0;
630 #define module_put_and_exit(code) do_exit(code)
632 static inline void print_modules(void)
636 static inline void module_update_tracepoints(void)
640 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
642 return 0;
644 #endif /* CONFIG_MODULES */
646 #ifdef CONFIG_SYSFS
647 extern struct kset *module_kset;
648 extern struct kobj_type module_ktype;
649 extern int module_sysfs_initialized;
650 #endif /* CONFIG_SYSFS */
652 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
654 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
656 #define __MODULE_STRING(x) __stringify(x)
658 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
659 extern void set_all_modules_text_rw(void);
660 extern void set_all_modules_text_ro(void);
661 #else
662 static inline void set_all_modules_text_rw(void) { }
663 static inline void set_all_modules_text_ro(void) { }
664 #endif
666 #ifdef CONFIG_GENERIC_BUG
667 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
668 struct module *);
669 void module_bug_cleanup(struct module *);
671 #else /* !CONFIG_GENERIC_BUG */
673 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
674 const Elf_Shdr *sechdrs,
675 struct module *mod)
678 static inline void module_bug_cleanup(struct module *mod) {}
679 #endif /* CONFIG_GENERIC_BUG */
681 #endif /* _LINUX_MODULE_H */