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>
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
34 char name
[MODULE_NAME_LEN
];
39 struct module_kobject
{
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
*,
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
;
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
,
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
);
85 #define MODULE_GENERIC_TABLE(gtype,name) \
86 extern const struct gtype##_id __mod_##gtype##_table \
87 __attribute__ ((unused, alias(__stringify(name))))
90 #define MODULE_GENERIC_TABLE(gtype,name)
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
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
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
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)
164 #define MODULE_VERSION(_version) \
165 static struct module_version_attribute ___modver_attr = { \
171 .show = __modver_version_show, \
173 .module_name = KBUILD_MODNAME, \
174 .version = _version, \
176 static const struct module_version_attribute \
177 __used __attribute__ ((__section__ ("__modver"))) \
178 * __moduleparam_const __modver_attr = &___modver_attr
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. */
201 struct list_head source_list
;
202 struct list_head target_list
;
203 struct module
*source
, *target
;
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
];
224 struct module_kobject mkobj
;
225 struct module_attribute
*modinfo_attrs
;
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
;
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
;
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. */
268 /* If this is non-NULL, vfree after init() returns */
271 /* Here is the actual code + data, vfree'd on unload. */
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 */
291 struct list_head bug_list
;
292 struct bug_entry
*bug_table
;
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
;
312 /* The command line arguments (may be mangled). People like
313 keeping pointers to this stuff */
318 void __percpu
*percpu
;
319 unsigned int percpu_size
;
322 #ifdef CONFIG_TRACEPOINTS
323 unsigned int num_tracepoints
;
324 struct tracepoint
* const *tracepoints_ptrs
;
326 #ifdef HAVE_JUMP_LABEL
327 struct jump_entry
*jump_entries
;
328 unsigned int num_jump_entries
;
330 #ifdef CONFIG_TRACING
331 unsigned int num_trace_bprintk_fmt
;
332 const char **trace_bprintk_fmt_start
;
334 #ifdef CONFIG_EVENT_TRACING
335 struct ftrace_event_call
**trace_events
;
336 unsigned int num_trace_events
;
338 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
339 unsigned int num_ftrace_callsites
;
340 unsigned long *ftrace_callsites
;
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. */
361 #ifdef CONFIG_CONSTRUCTORS
362 /* Constructor functions. */
364 unsigned int num_ctors
;
367 #ifndef MODULE_ARCH_INIT
368 #define MODULE_ARCH_INIT {}
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
);
403 const struct kernel_symbol
*start
, *stop
;
404 const unsigned long *crcs
;
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
,
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),
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
)
453 __this_cpu_inc(module
->refptr
->incs
);
454 trace_module_get(module
, _THIS_IP_
);
459 static inline int try_module_get(struct module
*module
)
466 if (likely(module_is_live(module
))) {
467 __this_cpu_inc(module
->refptr
->incs
);
468 trace_module_get(module
, _THIS_IP_
);
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) \
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
,
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
)
534 static inline struct module
*__module_address(unsigned long addr
)
539 static inline struct module
*__module_text_address(unsigned long addr
)
544 static inline bool is_module_address(unsigned long addr
)
549 static inline bool is_module_percpu_address(unsigned long addr
)
554 static inline bool is_module_text_address(unsigned long addr
)
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
)
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
,
589 static inline int lookup_module_symbol_name(unsigned long addr
, char *symname
)
594 static inline int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
, unsigned long *offset
, char *modname
, char *name
)
599 static inline int module_get_kallsym(unsigned int symnum
, unsigned long *value
,
600 char *type
, char *name
,
601 char *module_name
, int *exported
)
606 static inline unsigned long module_kallsyms_lookup_name(const char *name
)
611 static inline int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
619 static inline int register_module_notifier(struct notifier_block
* nb
)
621 /* no events will happen anyway, so this can always succeed */
625 static inline int unregister_module_notifier(struct notifier_block
* nb
)
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
)
644 #endif /* CONFIG_MODULES */
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);
662 static inline void set_all_modules_text_rw(void) { }
663 static inline void set_all_modules_text_ro(void) { }
666 #ifdef CONFIG_GENERIC_BUG
667 void module_bug_finalize(const Elf_Ehdr
*, const Elf_Shdr
*,
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
,
678 static inline void module_bug_cleanup(struct module
*mod
) {}
679 #endif /* CONFIG_GENERIC_BUG */
681 #endif /* _LINUX_MODULE_H */