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_kobject {
54 struct kobject *drivers_dir;
55 struct module_param_attrs *mp;
58 struct module_attribute {
59 struct attribute attr;
60 ssize_t (*show)(struct module_attribute *, struct module_kobject *,
62 ssize_t (*store)(struct module_attribute *, struct module_kobject *,
63 const char *, size_t count);
64 void (*setup)(struct module *, const char *);
65 int (*test)(struct module *);
66 void (*free)(struct module *);
69 struct module_version_attribute {
70 struct module_attribute mattr;
71 const char *module_name;
73 } __attribute__ ((__aligned__(sizeof(void *))));
75 extern ssize_t __modver_version_show(struct module_attribute *,
76 struct module_kobject *, char *);
79 /* These are either module local, or the kernel's dummy ones. */
80 extern int init_module(void);
81 extern void cleanup_module(void);
83 /* Archs provide a method of finding the correct exception table. */
84 struct exception_table_entry;
86 const struct exception_table_entry *
87 search_extable(const struct exception_table_entry *first,
88 const struct exception_table_entry *last,
90 void sort_extable(struct exception_table_entry *start,
91 struct exception_table_entry *finish);
92 void sort_main_extable(void);
93 void trim_init_extable(struct module *m);
96 #define MODULE_GENERIC_TABLE(gtype,name) \
97 extern const struct gtype##_id __mod_##gtype##_table \
98 __attribute__ ((unused, alias(__stringify(name))))
100 extern struct module __this_module;
101 #define THIS_MODULE (&__this_module)
103 #define MODULE_GENERIC_TABLE(gtype,name)
104 #define THIS_MODULE ((struct module *)0)
107 /* Generic info of form tag = "info" */
108 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
110 /* For userspace: you can also call me... */
111 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
114 * The following license idents are currently accepted as indicating free
117 * "GPL" [GNU Public License v2 or later]
118 * "GPL v2" [GNU Public License v2]
119 * "GPL and additional rights" [GNU Public License v2 rights and more]
120 * "Dual BSD/GPL" [GNU Public License v2
121 * or BSD license choice]
122 * "Dual MIT/GPL" [GNU Public License v2
123 * or MIT license choice]
124 * "Dual MPL/GPL" [GNU Public License v2
125 * or Mozilla license choice]
127 * The following other idents are available
129 * "Proprietary" [Non free products]
131 * There are dual licensed components, but when running with Linux it is the
132 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
133 * is a GPL combined work.
135 * This exists for several reasons
136 * 1. So modinfo can show license info for users wanting to vet their setup
138 * 2. So the community can ignore bug reports including proprietary modules
139 * 3. So vendors can do likewise based on their own policies
141 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
144 * Author(s), use "Name <email>" or just "Name", for multiple
145 * authors use multiple MODULE_AUTHOR() statements/lines.
147 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
149 /* What your module does. */
150 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
152 /* One for each parameter, describing how to use it. Some files do
153 multiple of these per line, so can't just use MODULE_INFO. */
154 #define MODULE_PARM_DESC(_parm, desc) \
155 __MODULE_INFO(parm, _parm, #_parm ":" desc)
157 #define MODULE_DEVICE_TABLE(type,name) \
158 MODULE_GENERIC_TABLE(type##_device,name)
160 /* Version of form [<epoch>:]<version>[-<extra-version>].
161 Or for CVS/RCS ID version, everything but the number is stripped.
162 <epoch>: A (small) unsigned integer which allows you to start versions
163 anew. If not mentioned, it's zero. eg. "2:1.0" is after
165 <version>: The <version> may contain only alphanumerics and the
166 character `.'. Ordered by numeric sort for numeric parts,
167 ascii sort for ascii parts (as per RPM or DEB algorithm).
168 <extraversion>: Like <version>, but inserted for local
169 customizations, eg "rh3" or "rusty1".
171 Using this automatically adds a checksum of the .c files and the
172 local headers in "srcversion".
175 #if defined(MODULE) || !defined(CONFIG_SYSFS)
176 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
178 #define MODULE_VERSION(_version) \
179 static struct module_version_attribute ___modver_attr = { \
185 .show = __modver_version_show, \
187 .module_name = KBUILD_MODNAME, \
188 .version = _version, \
190 static const struct module_version_attribute \
191 __used __attribute__ ((__section__ ("__modver"))) \
192 * __moduleparam_const __modver_attr = &___modver_attr
195 /* Optional firmware file (or files) needed by the module
196 * format is simply firmware file name. Multiple firmware
197 * files require multiple MODULE_FIRMWARE() specifiers */
198 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
200 /* Given an address, look for it in the exception tables */
201 const struct exception_table_entry *search_exception_tables(unsigned long add);
203 struct notifier_block;
205 #ifdef CONFIG_MODULES
207 extern int modules_disabled; /* for sysctl */
208 /* Get/put a kernel symbol (calls must be symmetric) */
209 void *__symbol_get(const char *symbol);
210 void *__symbol_get_gpl(const char *symbol);
211 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
213 /* modules using other modules: kdb wants to see this. */
215 struct list_head source_list;
216 struct list_head target_list;
217 struct module *source, *target;
221 #ifdef CONFIG_MODVERSIONS
222 /* Mark the CRC weak since genksyms apparently decides not to
223 * generate a checksums for some symbols */
224 #define __CRC_SYMBOL(sym, sec) \
225 extern void *__crc_##sym __attribute__((weak)); \
226 static const unsigned long __kcrctab_##sym \
228 __attribute__((section("___kcrctab" sec "+" #sym), unused)) \
229 = (unsigned long) &__crc_##sym;
231 #define __CRC_SYMBOL(sym, sec)
234 /* For every exported symbol, place a struct in the __ksymtab section */
235 #define __EXPORT_SYMBOL(sym, sec) \
236 extern typeof(sym) sym; \
237 __CRC_SYMBOL(sym, sec) \
238 static const char __kstrtab_##sym[] \
239 __attribute__((section("__ksymtab_strings"), aligned(1))) \
240 = MODULE_SYMBOL_PREFIX #sym; \
241 static const struct kernel_symbol __ksymtab_##sym \
243 __attribute__((section("___ksymtab" sec "+" #sym), unused)) \
244 = { (unsigned long)&sym, __kstrtab_##sym }
246 #define EXPORT_SYMBOL(sym) \
247 __EXPORT_SYMBOL(sym, "")
249 #define EXPORT_SYMBOL_GPL(sym) \
250 __EXPORT_SYMBOL(sym, "_gpl")
252 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
253 __EXPORT_SYMBOL(sym, "_gpl_future")
256 #ifdef CONFIG_UNUSED_SYMBOLS
257 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
258 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
260 #define EXPORT_UNUSED_SYMBOL(sym)
261 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
275 enum module_state state;
277 /* Member of list of modules */
278 struct list_head list;
280 /* Unique handle for this module */
281 char name[MODULE_NAME_LEN];
284 struct module_kobject mkobj;
285 struct module_attribute *modinfo_attrs;
287 const char *srcversion;
288 struct kobject *holders_dir;
290 /* Exported symbols */
291 const struct kernel_symbol *syms;
292 const unsigned long *crcs;
293 unsigned int num_syms;
295 /* Kernel parameters. */
296 struct kernel_param *kp;
299 /* GPL-only exported symbols. */
300 unsigned int num_gpl_syms;
301 const struct kernel_symbol *gpl_syms;
302 const unsigned long *gpl_crcs;
304 #ifdef CONFIG_UNUSED_SYMBOLS
305 /* unused exported symbols. */
306 const struct kernel_symbol *unused_syms;
307 const unsigned long *unused_crcs;
308 unsigned int num_unused_syms;
310 /* GPL-only, unused exported symbols. */
311 unsigned int num_unused_gpl_syms;
312 const struct kernel_symbol *unused_gpl_syms;
313 const unsigned long *unused_gpl_crcs;
316 /* symbols that will be GPL-only in the near future. */
317 const struct kernel_symbol *gpl_future_syms;
318 const unsigned long *gpl_future_crcs;
319 unsigned int num_gpl_future_syms;
321 /* Exception table */
322 unsigned int num_exentries;
323 struct exception_table_entry *extable;
325 /* Startup function. */
328 /* If this is non-NULL, vfree after init() returns */
331 /* Here is the actual code + data, vfree'd on unload. */
334 /* Here are the sizes of the init and core sections */
335 unsigned int init_size, core_size;
337 /* The size of the executable code in each section. */
338 unsigned int init_text_size, core_text_size;
340 /* Size of RO sections of the module (text+rodata) */
341 unsigned int init_ro_size, core_ro_size;
343 /* Arch-specific module values */
344 struct mod_arch_specific arch;
346 unsigned int taints; /* same bits as kernel:tainted */
348 #ifdef CONFIG_GENERIC_BUG
349 /* Support for BUG */
351 struct list_head bug_list;
352 struct bug_entry *bug_table;
355 #ifdef CONFIG_KALLSYMS
357 * We keep the symbol and string tables for kallsyms.
358 * The core_* fields below are temporary, loader-only (they
359 * could really be discarded after module init).
361 Elf_Sym *symtab, *core_symtab;
362 unsigned int num_symtab, core_num_syms;
363 char *strtab, *core_strtab;
365 /* Section attributes */
366 struct module_sect_attrs *sect_attrs;
368 /* Notes attributes */
369 struct module_notes_attrs *notes_attrs;
372 /* The command line arguments (may be mangled). People like
373 keeping pointers to this stuff */
378 void __percpu *percpu;
379 unsigned int percpu_size;
382 #ifdef CONFIG_TRACEPOINTS
383 unsigned int num_tracepoints;
384 struct tracepoint * const *tracepoints_ptrs;
386 #ifdef HAVE_JUMP_LABEL
387 struct jump_entry *jump_entries;
388 unsigned int num_jump_entries;
390 #ifdef CONFIG_TRACING
391 unsigned int num_trace_bprintk_fmt;
392 const char **trace_bprintk_fmt_start;
394 #ifdef CONFIG_EVENT_TRACING
395 struct ftrace_event_call **trace_events;
396 unsigned int num_trace_events;
398 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
399 unsigned int num_ftrace_callsites;
400 unsigned long *ftrace_callsites;
403 #ifdef CONFIG_MODULE_UNLOAD
404 /* What modules depend on me? */
405 struct list_head source_list;
406 /* What modules do I depend on? */
407 struct list_head target_list;
409 /* Who is waiting for us to be unloaded */
410 struct task_struct *waiter;
412 /* Destruction function. */
421 #ifdef CONFIG_CONSTRUCTORS
422 /* Constructor functions. */
424 unsigned int num_ctors;
427 #ifndef MODULE_ARCH_INIT
428 #define MODULE_ARCH_INIT {}
431 extern struct mutex module_mutex;
433 /* FIXME: It'd be nice to isolate modules during init, too, so they
434 aren't used before they (may) fail. But presently too much code
435 (IDE & SCSI) require entry into the module during init.*/
436 static inline int module_is_live(struct module *mod)
438 return mod->state != MODULE_STATE_GOING;
441 struct module *__module_text_address(unsigned long addr);
442 struct module *__module_address(unsigned long addr);
443 bool is_module_address(unsigned long addr);
444 bool is_module_percpu_address(unsigned long addr);
445 bool is_module_text_address(unsigned long addr);
447 static inline int within_module_core(unsigned long addr, struct module *mod)
449 return (unsigned long)mod->module_core <= addr &&
450 addr < (unsigned long)mod->module_core + mod->core_size;
453 static inline int within_module_init(unsigned long addr, struct module *mod)
455 return (unsigned long)mod->module_init <= addr &&
456 addr < (unsigned long)mod->module_init + mod->init_size;
459 /* Search for module by name: must hold module_mutex. */
460 struct module *find_module(const char *name);
463 const struct kernel_symbol *start, *stop;
464 const unsigned long *crcs;
473 /* Search for an exported symbol by name. */
474 const struct kernel_symbol *find_symbol(const char *name,
475 struct module **owner,
476 const unsigned long **crc,
480 /* Walk the exported symbol table */
481 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
482 struct module *owner,
483 void *data), void *data);
485 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
486 symnum out of range. */
487 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
488 char *name, char *module_name, int *exported);
490 /* Look for this name: can be of form module:name. */
491 unsigned long module_kallsyms_lookup_name(const char *name);
493 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
494 struct module *, unsigned long),
497 extern void __module_put_and_exit(struct module *mod, long code)
498 __attribute__((noreturn));
499 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
501 #ifdef CONFIG_MODULE_UNLOAD
502 unsigned int module_refcount(struct module *mod);
503 void __symbol_put(const char *symbol);
504 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
505 void symbol_put_addr(void *addr);
507 /* Sometimes we know we already have a refcount, and it's easier not
508 to handle the error case (which only happens with rmmod --wait). */
509 static inline void __module_get(struct module *module)
513 __this_cpu_inc(module->refptr->incs);
514 trace_module_get(module, _THIS_IP_);
519 static inline int try_module_get(struct module *module)
526 if (likely(module_is_live(module))) {
527 __this_cpu_inc(module->refptr->incs);
528 trace_module_get(module, _THIS_IP_);
537 extern void module_put(struct module *module);
539 #else /*!CONFIG_MODULE_UNLOAD*/
540 static inline int try_module_get(struct module *module)
542 return !module || module_is_live(module);
544 static inline void module_put(struct module *module)
547 static inline void __module_get(struct module *module)
550 #define symbol_put(x) do { } while(0)
551 #define symbol_put_addr(p) do { } while(0)
553 #endif /* CONFIG_MODULE_UNLOAD */
554 int ref_module(struct module *a, struct module *b);
556 /* This is a #define so the string doesn't get put in every .o file */
557 #define module_name(mod) \
559 struct module *__mod = (mod); \
560 __mod ? __mod->name : "kernel"; \
563 /* For kallsyms to ask for address resolution. namebuf should be at
564 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
565 * found, otherwise NULL. */
566 const char *module_address_lookup(unsigned long addr,
567 unsigned long *symbolsize,
568 unsigned long *offset,
571 int lookup_module_symbol_name(unsigned long addr, char *symname);
572 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
574 /* For extable.c to search modules' exception tables. */
575 const struct exception_table_entry *search_module_extables(unsigned long addr);
577 int register_module_notifier(struct notifier_block * nb);
578 int unregister_module_notifier(struct notifier_block * nb);
580 extern void print_modules(void);
582 extern void module_update_tracepoints(void);
583 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
585 #else /* !CONFIG_MODULES... */
586 #define EXPORT_SYMBOL(sym)
587 #define EXPORT_SYMBOL_GPL(sym)
588 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
589 #define EXPORT_UNUSED_SYMBOL(sym)
590 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
592 /* Given an address, look for it in the exception tables. */
593 static inline const struct exception_table_entry *
594 search_module_extables(unsigned long addr)
599 static inline struct module *__module_address(unsigned long addr)
604 static inline struct module *__module_text_address(unsigned long addr)
609 static inline bool is_module_address(unsigned long addr)
614 static inline bool is_module_percpu_address(unsigned long addr)
619 static inline bool is_module_text_address(unsigned long addr)
624 /* Get/put a kernel symbol (calls should be symmetric) */
625 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
626 #define symbol_put(x) do { } while(0)
627 #define symbol_put_addr(x) do { } while(0)
629 static inline void __module_get(struct module *module)
633 static inline int try_module_get(struct module *module)
638 static inline void module_put(struct module *module)
642 #define module_name(mod) "kernel"
644 /* For kallsyms to ask for address resolution. NULL means not found. */
645 static inline const char *module_address_lookup(unsigned long addr,
646 unsigned long *symbolsize,
647 unsigned long *offset,
654 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
659 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
664 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
665 char *type, char *name,
666 char *module_name, int *exported)
671 static inline unsigned long module_kallsyms_lookup_name(const char *name)
676 static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
684 static inline int register_module_notifier(struct notifier_block * nb)
686 /* no events will happen anyway, so this can always succeed */
690 static inline int unregister_module_notifier(struct notifier_block * nb)
695 #define module_put_and_exit(code) do_exit(code)
697 static inline void print_modules(void)
701 static inline void module_update_tracepoints(void)
705 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
709 #endif /* CONFIG_MODULES */
712 extern struct kset *module_kset;
713 extern struct kobj_type module_ktype;
714 extern int module_sysfs_initialized;
715 #endif /* CONFIG_SYSFS */
717 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
719 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
721 #define __MODULE_STRING(x) __stringify(x)
723 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
724 extern void set_all_modules_text_rw(void);
725 extern void set_all_modules_text_ro(void);
727 static inline void set_all_modules_text_rw(void) { }
728 static inline void set_all_modules_text_ro(void) { }
731 #ifdef CONFIG_GENERIC_BUG
732 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
734 void module_bug_cleanup(struct module *);
736 #else /* !CONFIG_GENERIC_BUG */
738 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
739 const Elf_Shdr *sechdrs,
743 static inline void module_bug_cleanup(struct module *mod) {}
744 #endif /* CONFIG_GENERIC_BUG */
746 #endif /* _LINUX_MODULE_H */