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/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>
20 #include <linux/percpu.h>
21 #include <asm/module.h>
23 #include <trace/events/module.h>
25 /* Not Yet Implemented */
26 #define MODULE_SUPPORTED_DEVICE(name)
28 /* Some toolchains use a `_' prefix for all user symbols. */
29 #ifdef CONFIG_SYMBOL_PREFIX
30 #define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
32 #define MODULE_SYMBOL_PREFIX ""
35 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
43 struct modversion_info
46 char name[MODULE_NAME_LEN];
51 struct module_attribute {
52 struct attribute attr;
53 ssize_t (*show)(struct module_attribute *, struct module *, char *);
54 ssize_t (*store)(struct module_attribute *, struct module *,
55 const char *, size_t count);
56 void (*setup)(struct module *, const char *);
57 int (*test)(struct module *);
58 void (*free)(struct module *);
61 struct module_version_attribute {
62 struct module_attribute mattr;
63 const char *module_name;
65 } __attribute__ ((__aligned__(sizeof(void *))));
71 struct kobject *drivers_dir;
72 struct module_param_attrs *mp;
75 /* These are either module local, or the kernel's dummy ones. */
76 extern int init_module(void);
77 extern void cleanup_module(void);
79 /* Archs provide a method of finding the correct exception table. */
80 struct exception_table_entry;
82 const struct exception_table_entry *
83 search_extable(const struct exception_table_entry *first,
84 const struct exception_table_entry *last,
86 void sort_extable(struct exception_table_entry *start,
87 struct exception_table_entry *finish);
88 void sort_main_extable(void);
89 void trim_init_extable(struct module *m);
92 #define MODULE_GENERIC_TABLE(gtype,name) \
93 extern const struct gtype##_id __mod_##gtype##_table \
94 __attribute__ ((unused, alias(__stringify(name))))
96 extern struct module __this_module;
97 #define THIS_MODULE (&__this_module)
99 #define MODULE_GENERIC_TABLE(gtype,name)
100 #define THIS_MODULE ((struct module *)0)
103 /* Generic info of form tag = "info" */
104 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
106 /* For userspace: you can also call me... */
107 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
110 * The following license idents are currently accepted as indicating free
113 * "GPL" [GNU Public License v2 or later]
114 * "GPL v2" [GNU Public License v2]
115 * "GPL and additional rights" [GNU Public License v2 rights and more]
116 * "Dual BSD/GPL" [GNU Public License v2
117 * or BSD license choice]
118 * "Dual MIT/GPL" [GNU Public License v2
119 * or MIT license choice]
120 * "Dual MPL/GPL" [GNU Public License v2
121 * or Mozilla license choice]
123 * The following other idents are available
125 * "Proprietary" [Non free products]
127 * There are dual licensed components, but when running with Linux it is the
128 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
129 * is a GPL combined work.
131 * This exists for several reasons
132 * 1. So modinfo can show license info for users wanting to vet their setup
134 * 2. So the community can ignore bug reports including proprietary modules
135 * 3. So vendors can do likewise based on their own policies
137 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
140 * Author(s), use "Name <email>" or just "Name", for multiple
141 * authors use multiple MODULE_AUTHOR() statements/lines.
143 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
145 /* What your module does. */
146 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
148 /* One for each parameter, describing how to use it. Some files do
149 multiple of these per line, so can't just use MODULE_INFO. */
150 #define MODULE_PARM_DESC(_parm, desc) \
151 __MODULE_INFO(parm, _parm, #_parm ":" desc)
153 #define MODULE_DEVICE_TABLE(type,name) \
154 MODULE_GENERIC_TABLE(type##_device,name)
156 /* Version of form [<epoch>:]<version>[-<extra-version>].
157 Or for CVS/RCS ID version, everything but the number is stripped.
158 <epoch>: A (small) unsigned integer which allows you to start versions
159 anew. If not mentioned, it's zero. eg. "2:1.0" is after
161 <version>: The <version> may contain only alphanumerics and the
162 character `.'. Ordered by numeric sort for numeric parts,
163 ascii sort for ascii parts (as per RPM or DEB algorithm).
164 <extraversion>: Like <version>, but inserted for local
165 customizations, eg "rh3" or "rusty1".
167 Using this automatically adds a checksum of the .c files and the
168 local headers in "srcversion".
171 #if defined(MODULE) || !defined(CONFIG_SYSFS)
172 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
174 #define MODULE_VERSION(_version) \
175 extern ssize_t __modver_version_show(struct module_attribute *, \
176 struct module *, char *); \
177 static struct module_version_attribute __modver_version_attr \
179 __attribute__ ((__section__ ("__modver"),aligned(sizeof(void *)))) \
186 .show = __modver_version_show, \
188 .module_name = KBUILD_MODNAME, \
189 .version = _version, \
193 /* Optional firmware file (or files) needed by the module
194 * format is simply firmware file name. Multiple firmware
195 * files require multiple MODULE_FIRMWARE() specifiers */
196 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
198 /* Given an address, look for it in the exception tables */
199 const struct exception_table_entry *search_exception_tables(unsigned long add);
201 struct notifier_block;
203 #ifdef CONFIG_MODULES
205 extern int modules_disabled; /* for sysctl */
206 /* Get/put a kernel symbol (calls must be symmetric) */
207 void *__symbol_get(const char *symbol);
208 void *__symbol_get_gpl(const char *symbol);
209 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
211 /* modules using other modules: kdb wants to see this. */
213 struct list_head source_list;
214 struct list_head target_list;
215 struct module *source, *target;
219 #ifdef CONFIG_MODVERSIONS
220 /* Mark the CRC weak since genksyms apparently decides not to
221 * generate a checksums for some symbols */
222 #define __CRC_SYMBOL(sym, sec) \
223 extern void *__crc_##sym __attribute__((weak)); \
224 static const unsigned long __kcrctab_##sym \
226 __attribute__((section("__kcrctab" sec), unused)) \
227 = (unsigned long) &__crc_##sym;
229 #define __CRC_SYMBOL(sym, sec)
232 /* For every exported symbol, place a struct in the __ksymtab section */
233 #define __EXPORT_SYMBOL(sym, sec) \
234 extern typeof(sym) sym; \
235 __CRC_SYMBOL(sym, sec) \
236 static const char __kstrtab_##sym[] \
237 __attribute__((section("__ksymtab_strings"), aligned(1))) \
238 = MODULE_SYMBOL_PREFIX #sym; \
239 static const struct kernel_symbol __ksymtab_##sym \
241 __attribute__((section("__ksymtab" sec), unused)) \
242 = { (unsigned long)&sym, __kstrtab_##sym }
244 #define EXPORT_SYMBOL(sym) \
245 __EXPORT_SYMBOL(sym, "")
247 #define EXPORT_SYMBOL_GPL(sym) \
248 __EXPORT_SYMBOL(sym, "_gpl")
250 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
251 __EXPORT_SYMBOL(sym, "_gpl_future")
254 #ifdef CONFIG_UNUSED_SYMBOLS
255 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
256 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
258 #define EXPORT_UNUSED_SYMBOL(sym)
259 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
273 enum module_state state;
275 /* Member of list of modules */
276 struct list_head list;
278 /* Unique handle for this module */
279 char name[MODULE_NAME_LEN];
282 struct module_kobject mkobj;
283 struct module_attribute *modinfo_attrs;
285 const char *srcversion;
286 struct kobject *holders_dir;
288 /* Exported symbols */
289 const struct kernel_symbol *syms;
290 const unsigned long *crcs;
291 unsigned int num_syms;
293 /* Kernel parameters. */
294 struct kernel_param *kp;
297 /* GPL-only exported symbols. */
298 unsigned int num_gpl_syms;
299 const struct kernel_symbol *gpl_syms;
300 const unsigned long *gpl_crcs;
302 #ifdef CONFIG_UNUSED_SYMBOLS
303 /* unused exported symbols. */
304 const struct kernel_symbol *unused_syms;
305 const unsigned long *unused_crcs;
306 unsigned int num_unused_syms;
308 /* GPL-only, unused exported symbols. */
309 unsigned int num_unused_gpl_syms;
310 const struct kernel_symbol *unused_gpl_syms;
311 const unsigned long *unused_gpl_crcs;
314 /* symbols that will be GPL-only in the near future. */
315 const struct kernel_symbol *gpl_future_syms;
316 const unsigned long *gpl_future_crcs;
317 unsigned int num_gpl_future_syms;
319 /* Exception table */
320 unsigned int num_exentries;
321 struct exception_table_entry *extable;
323 /* Startup function. */
326 /* If this is non-NULL, vfree after init() returns */
329 /* Here is the actual code + data, vfree'd on unload. */
332 /* Here are the sizes of the init and core sections */
333 unsigned int init_size, core_size;
335 /* The size of the executable code in each section. */
336 unsigned int init_text_size, core_text_size;
338 /* Size of RO sections of the module (text+rodata) */
339 unsigned int init_ro_size, core_ro_size;
341 /* Arch-specific module values */
342 struct mod_arch_specific arch;
344 unsigned int taints; /* same bits as kernel:tainted */
346 #ifdef CONFIG_GENERIC_BUG
347 /* Support for BUG */
349 struct list_head bug_list;
350 struct bug_entry *bug_table;
353 #ifdef CONFIG_KALLSYMS
355 * We keep the symbol and string tables for kallsyms.
356 * The core_* fields below are temporary, loader-only (they
357 * could really be discarded after module init).
359 Elf_Sym *symtab, *core_symtab;
360 unsigned int num_symtab, core_num_syms;
361 char *strtab, *core_strtab;
363 /* Section attributes */
364 struct module_sect_attrs *sect_attrs;
366 /* Notes attributes */
367 struct module_notes_attrs *notes_attrs;
372 void __percpu *percpu;
373 unsigned int percpu_size;
376 /* The command line arguments (may be mangled). People like
377 keeping pointers to this stuff */
379 #ifdef CONFIG_TRACEPOINTS
380 struct tracepoint * const *tracepoints_ptrs;
381 unsigned int num_tracepoints;
383 #ifdef HAVE_JUMP_LABEL
384 struct jump_entry *jump_entries;
385 unsigned int num_jump_entries;
387 #ifdef CONFIG_TRACING
388 const char **trace_bprintk_fmt_start;
389 unsigned int num_trace_bprintk_fmt;
391 #ifdef CONFIG_EVENT_TRACING
392 struct ftrace_event_call **trace_events;
393 unsigned int num_trace_events;
395 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
396 unsigned long *ftrace_callsites;
397 unsigned int num_ftrace_callsites;
400 #ifdef CONFIG_MODULE_UNLOAD
401 /* What modules depend on me? */
402 struct list_head source_list;
403 /* What modules do I depend on? */
404 struct list_head target_list;
406 /* Who is waiting for us to be unloaded */
407 struct task_struct *waiter;
409 /* Destruction function. */
418 #ifdef CONFIG_CONSTRUCTORS
419 /* Constructor functions. */
421 unsigned int num_ctors;
424 #ifndef MODULE_ARCH_INIT
425 #define MODULE_ARCH_INIT {}
428 extern struct mutex module_mutex;
430 /* FIXME: It'd be nice to isolate modules during init, too, so they
431 aren't used before they (may) fail. But presently too much code
432 (IDE & SCSI) require entry into the module during init.*/
433 static inline int module_is_live(struct module *mod)
435 return mod->state != MODULE_STATE_GOING;
438 struct module *__module_text_address(unsigned long addr);
439 struct module *__module_address(unsigned long addr);
440 bool is_module_address(unsigned long addr);
441 bool is_module_percpu_address(unsigned long addr);
442 bool is_module_text_address(unsigned long addr);
444 static inline int within_module_core(unsigned long addr, struct module *mod)
446 return (unsigned long)mod->module_core <= addr &&
447 addr < (unsigned long)mod->module_core + mod->core_size;
450 static inline int within_module_init(unsigned long addr, struct module *mod)
452 return (unsigned long)mod->module_init <= addr &&
453 addr < (unsigned long)mod->module_init + mod->init_size;
456 /* Search for module by name: must hold module_mutex. */
457 struct module *find_module(const char *name);
460 const struct kernel_symbol *start, *stop;
461 const unsigned long *crcs;
470 /* Search for an exported symbol by name. */
471 const struct kernel_symbol *find_symbol(const char *name,
472 struct module **owner,
473 const unsigned long **crc,
477 /* Walk the exported symbol table */
478 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
479 unsigned int symnum, void *data), void *data);
481 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
482 symnum out of range. */
483 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
484 char *name, char *module_name, int *exported);
486 /* Look for this name: can be of form module:name. */
487 unsigned long module_kallsyms_lookup_name(const char *name);
489 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
490 struct module *, unsigned long),
493 extern void __module_put_and_exit(struct module *mod, long code)
494 __attribute__((noreturn));
495 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
497 #ifdef CONFIG_MODULE_UNLOAD
498 unsigned int module_refcount(struct module *mod);
499 void __symbol_put(const char *symbol);
500 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
501 void symbol_put_addr(void *addr);
503 /* Sometimes we know we already have a refcount, and it's easier not
504 to handle the error case (which only happens with rmmod --wait). */
505 static inline void __module_get(struct module *module)
509 __this_cpu_inc(module->refptr->incs);
510 trace_module_get(module, _THIS_IP_);
515 static inline int try_module_get(struct module *module)
522 if (likely(module_is_live(module))) {
523 __this_cpu_inc(module->refptr->incs);
524 trace_module_get(module, _THIS_IP_);
533 extern void module_put(struct module *module);
535 #else /*!CONFIG_MODULE_UNLOAD*/
536 static inline int try_module_get(struct module *module)
538 return !module || module_is_live(module);
540 static inline void module_put(struct module *module)
543 static inline void __module_get(struct module *module)
546 #define symbol_put(x) do { } while(0)
547 #define symbol_put_addr(p) do { } while(0)
549 #endif /* CONFIG_MODULE_UNLOAD */
550 int ref_module(struct module *a, struct module *b);
552 /* This is a #define so the string doesn't get put in every .o file */
553 #define module_name(mod) \
555 struct module *__mod = (mod); \
556 __mod ? __mod->name : "kernel"; \
559 /* For kallsyms to ask for address resolution. namebuf should be at
560 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
561 * found, otherwise NULL. */
562 const char *module_address_lookup(unsigned long addr,
563 unsigned long *symbolsize,
564 unsigned long *offset,
567 int lookup_module_symbol_name(unsigned long addr, char *symname);
568 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
570 /* For extable.c to search modules' exception tables. */
571 const struct exception_table_entry *search_module_extables(unsigned long addr);
573 int register_module_notifier(struct notifier_block * nb);
574 int unregister_module_notifier(struct notifier_block * nb);
576 extern void print_modules(void);
578 extern void module_update_tracepoints(void);
579 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
581 #else /* !CONFIG_MODULES... */
582 #define EXPORT_SYMBOL(sym)
583 #define EXPORT_SYMBOL_GPL(sym)
584 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
585 #define EXPORT_UNUSED_SYMBOL(sym)
586 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
588 /* Given an address, look for it in the exception tables. */
589 static inline const struct exception_table_entry *
590 search_module_extables(unsigned long addr)
595 static inline struct module *__module_address(unsigned long addr)
600 static inline struct module *__module_text_address(unsigned long addr)
605 static inline bool is_module_address(unsigned long addr)
610 static inline bool is_module_percpu_address(unsigned long addr)
615 static inline bool is_module_text_address(unsigned long addr)
620 /* Get/put a kernel symbol (calls should be symmetric) */
621 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
622 #define symbol_put(x) do { } while(0)
623 #define symbol_put_addr(x) do { } while(0)
625 static inline void __module_get(struct module *module)
629 static inline int try_module_get(struct module *module)
634 static inline void module_put(struct module *module)
638 #define module_name(mod) "kernel"
640 /* For kallsyms to ask for address resolution. NULL means not found. */
641 static inline const char *module_address_lookup(unsigned long addr,
642 unsigned long *symbolsize,
643 unsigned long *offset,
650 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
655 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
660 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
661 char *type, char *name,
662 char *module_name, int *exported)
667 static inline unsigned long module_kallsyms_lookup_name(const char *name)
672 static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
680 static inline int register_module_notifier(struct notifier_block * nb)
682 /* no events will happen anyway, so this can always succeed */
686 static inline int unregister_module_notifier(struct notifier_block * nb)
691 #define module_put_and_exit(code) do_exit(code)
693 static inline void print_modules(void)
697 static inline void module_update_tracepoints(void)
701 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
705 #endif /* CONFIG_MODULES */
708 extern struct kset *module_kset;
709 extern struct kobj_type module_ktype;
710 extern int module_sysfs_initialized;
711 #endif /* CONFIG_SYSFS */
713 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
715 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
717 #define __MODULE_STRING(x) __stringify(x)
719 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
720 extern void set_all_modules_text_rw(void);
721 extern void set_all_modules_text_ro(void);
723 static inline void set_all_modules_text_rw(void) { }
724 static inline void set_all_modules_text_ro(void) { }
727 #ifdef CONFIG_GENERIC_BUG
728 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
730 void module_bug_cleanup(struct module *);
732 #else /* !CONFIG_GENERIC_BUG */
734 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
735 const Elf_Shdr *sechdrs,
739 static inline void module_bug_cleanup(struct module *mod) {}
740 #endif /* CONFIG_GENERIC_BUG */
742 #endif /* _LINUX_MODULE_H */