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/marker.h>
19 #include <linux/tracepoint.h>
21 #include <asm/local.h>
22 #include <asm/module.h>
24 #include <trace/events/module.h>
26 /* Not Yet Implemented */
27 #define MODULE_SUPPORTED_DEVICE(name)
29 /* some toolchains uses a `_' prefix for all user symbols */
30 #ifndef MODULE_SYMBOL_PREFIX
31 #define MODULE_SYMBOL_PREFIX ""
34 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
42 struct modversion_info
45 char name
[MODULE_NAME_LEN
];
50 struct module_attribute
{
51 struct attribute attr
;
52 ssize_t (*show
)(struct module_attribute
*, struct module
*, char *);
53 ssize_t (*store
)(struct module_attribute
*, struct module
*,
54 const char *, size_t count
);
55 void (*setup
)(struct module
*, const char *);
56 int (*test
)(struct module
*);
57 void (*free
)(struct module
*);
64 struct kobject
*drivers_dir
;
65 struct module_param_attrs
*mp
;
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))))
89 extern struct module __this_module
;
90 #define THIS_MODULE (&__this_module)
92 #define MODULE_GENERIC_TABLE(gtype,name)
93 #define THIS_MODULE ((struct module *)0)
96 /* Generic info of form tag = "info" */
97 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
99 /* For userspace: you can also call me... */
100 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
103 * The following license idents are currently accepted as indicating free
106 * "GPL" [GNU Public License v2 or later]
107 * "GPL v2" [GNU Public License v2]
108 * "GPL and additional rights" [GNU Public License v2 rights and more]
109 * "Dual BSD/GPL" [GNU Public License v2
110 * or BSD license choice]
111 * "Dual MIT/GPL" [GNU Public License v2
112 * or MIT license choice]
113 * "Dual MPL/GPL" [GNU Public License v2
114 * or Mozilla license choice]
116 * The following other idents are available
118 * "Proprietary" [Non free products]
120 * There are dual licensed components, but when running with Linux it is the
121 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
122 * is a GPL combined work.
124 * This exists for several reasons
125 * 1. So modinfo can show license info for users wanting to vet their setup
127 * 2. So the community can ignore bug reports including proprietary modules
128 * 3. So vendors can do likewise based on their own policies
130 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
132 /* Author, ideally of form NAME[, NAME]*[ and NAME] */
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".
160 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
162 /* Optional firmware file (or files) needed by the module
163 * format is simply firmware file name. Multiple firmware
164 * files require multiple MODULE_FIRMWARE() specifiers */
165 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
167 /* Given an address, look for it in the exception tables */
168 const struct exception_table_entry
*search_exception_tables(unsigned long add
);
170 struct notifier_block
;
172 #ifdef CONFIG_MODULES
174 /* Get/put a kernel symbol (calls must be symmetric) */
175 void *__symbol_get(const char *symbol
);
176 void *__symbol_get_gpl(const char *symbol
);
177 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
180 #ifdef CONFIG_MODVERSIONS
181 /* Mark the CRC weak since genksyms apparently decides not to
182 * generate a checksums for some symbols */
183 #define __CRC_SYMBOL(sym, sec) \
184 extern void *__crc_##sym __attribute__((weak)); \
185 static const unsigned long __kcrctab_##sym \
187 __attribute__((section("__kcrctab" sec), unused)) \
188 = (unsigned long) &__crc_##sym;
190 #define __CRC_SYMBOL(sym, sec)
193 /* For every exported symbol, place a struct in the __ksymtab section */
194 #define __EXPORT_SYMBOL(sym, sec) \
195 extern typeof(sym) sym; \
196 __CRC_SYMBOL(sym, sec) \
197 static const char __kstrtab_##sym[] \
198 __attribute__((section("__ksymtab_strings"), aligned(1))) \
199 = MODULE_SYMBOL_PREFIX #sym; \
200 static const struct kernel_symbol __ksymtab_##sym \
202 __attribute__((section("__ksymtab" sec), unused)) \
203 = { (unsigned long)&sym, __kstrtab_##sym }
205 #define EXPORT_SYMBOL(sym) \
206 __EXPORT_SYMBOL(sym, "")
208 #define EXPORT_SYMBOL_GPL(sym) \
209 __EXPORT_SYMBOL(sym, "_gpl")
211 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
212 __EXPORT_SYMBOL(sym, "_gpl_future")
215 #ifdef CONFIG_UNUSED_SYMBOLS
216 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
217 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
219 #define EXPORT_UNUSED_SYMBOL(sym)
220 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
234 enum module_state state
;
236 /* Member of list of modules */
237 struct list_head list
;
239 /* Unique handle for this module */
240 char name
[MODULE_NAME_LEN
];
243 struct module_kobject mkobj
;
244 struct module_attribute
*modinfo_attrs
;
246 const char *srcversion
;
247 struct kobject
*holders_dir
;
249 /* Exported symbols */
250 const struct kernel_symbol
*syms
;
251 const unsigned long *crcs
;
252 unsigned int num_syms
;
254 /* Kernel parameters. */
255 struct kernel_param
*kp
;
258 /* GPL-only exported symbols. */
259 unsigned int num_gpl_syms
;
260 const struct kernel_symbol
*gpl_syms
;
261 const unsigned long *gpl_crcs
;
263 #ifdef CONFIG_UNUSED_SYMBOLS
264 /* unused exported symbols. */
265 const struct kernel_symbol
*unused_syms
;
266 const unsigned long *unused_crcs
;
267 unsigned int num_unused_syms
;
269 /* GPL-only, unused exported symbols. */
270 unsigned int num_unused_gpl_syms
;
271 const struct kernel_symbol
*unused_gpl_syms
;
272 const unsigned long *unused_gpl_crcs
;
275 /* symbols that will be GPL-only in the near future. */
276 const struct kernel_symbol
*gpl_future_syms
;
277 const unsigned long *gpl_future_crcs
;
278 unsigned int num_gpl_future_syms
;
280 /* Exception table */
281 unsigned int num_exentries
;
282 struct exception_table_entry
*extable
;
284 /* Startup function. */
287 /* If this is non-NULL, vfree after init() returns */
290 /* Here is the actual code + data, vfree'd on unload. */
293 /* Here are the sizes of the init and core sections */
294 unsigned int init_size
, core_size
;
296 /* The size of the executable code in each section. */
297 unsigned int init_text_size
, core_text_size
;
299 /* Arch-specific module values */
300 struct mod_arch_specific arch
;
302 unsigned int taints
; /* same bits as kernel:tainted */
304 #ifdef CONFIG_GENERIC_BUG
305 /* Support for BUG */
307 struct list_head bug_list
;
308 struct bug_entry
*bug_table
;
311 #ifdef CONFIG_KALLSYMS
312 /* We keep the symbol and string tables for kallsyms. */
314 unsigned int num_symtab
;
317 /* Section attributes */
318 struct module_sect_attrs
*sect_attrs
;
320 /* Notes attributes */
321 struct module_notes_attrs
*notes_attrs
;
327 /* The command line arguments (may be mangled). People like
328 keeping pointers to this stuff */
330 #ifdef CONFIG_MARKERS
331 struct marker
*markers
;
332 unsigned int num_markers
;
334 #ifdef CONFIG_TRACEPOINTS
335 struct tracepoint
*tracepoints
;
336 unsigned int num_tracepoints
;
339 #ifdef CONFIG_TRACING
340 const char **trace_bprintk_fmt_start
;
341 unsigned int num_trace_bprintk_fmt
;
343 #ifdef CONFIG_EVENT_TRACING
344 struct ftrace_event_call
*trace_events
;
345 unsigned int num_trace_events
;
347 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
348 unsigned long *ftrace_callsites
;
349 unsigned int num_ftrace_callsites
;
352 #ifdef CONFIG_MODULE_UNLOAD
353 /* What modules depend on me? */
354 struct list_head modules_which_use_me
;
356 /* Who is waiting for us to be unloaded */
357 struct task_struct
*waiter
;
359 /* Destruction function. */
369 #ifdef CONFIG_CONSTRUCTORS
370 /* Constructor functions. */
372 unsigned int num_ctors
;
375 #ifndef MODULE_ARCH_INIT
376 #define MODULE_ARCH_INIT {}
379 extern struct mutex module_mutex
;
381 /* FIXME: It'd be nice to isolate modules during init, too, so they
382 aren't used before they (may) fail. But presently too much code
383 (IDE & SCSI) require entry into the module during init.*/
384 static inline int module_is_live(struct module
*mod
)
386 return mod
->state
!= MODULE_STATE_GOING
;
389 struct module
*__module_text_address(unsigned long addr
);
390 struct module
*__module_address(unsigned long addr
);
391 bool is_module_address(unsigned long addr
);
392 bool is_module_text_address(unsigned long addr
);
394 static inline int within_module_core(unsigned long addr
, struct module
*mod
)
396 return (unsigned long)mod
->module_core
<= addr
&&
397 addr
< (unsigned long)mod
->module_core
+ mod
->core_size
;
400 static inline int within_module_init(unsigned long addr
, struct module
*mod
)
402 return (unsigned long)mod
->module_init
<= addr
&&
403 addr
< (unsigned long)mod
->module_init
+ mod
->init_size
;
406 /* Search for module by name: must hold module_mutex. */
407 struct module
*find_module(const char *name
);
410 const struct kernel_symbol
*start
, *stop
;
411 const unsigned long *crcs
;
420 /* Search for an exported symbol by name. */
421 const struct kernel_symbol
*find_symbol(const char *name
,
422 struct module
**owner
,
423 const unsigned long **crc
,
427 /* Walk the exported symbol table */
428 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
429 unsigned int symnum
, void *data
), void *data
);
431 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
432 symnum out of range. */
433 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
434 char *name
, char *module_name
, int *exported
);
436 /* Look for this name: can be of form module:name. */
437 unsigned long module_kallsyms_lookup_name(const char *name
);
439 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
440 struct module
*, unsigned long),
443 extern void __module_put_and_exit(struct module
*mod
, long code
)
444 __attribute__((noreturn
));
445 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
447 #ifdef CONFIG_MODULE_UNLOAD
448 unsigned int module_refcount(struct module
*mod
);
449 void __symbol_put(const char *symbol
);
450 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
451 void symbol_put_addr(void *addr
);
453 static inline local_t
*__module_ref_addr(struct module
*mod
, int cpu
)
456 return (local_t
*) (mod
->refptr
+ per_cpu_offset(cpu
));
462 /* Sometimes we know we already have a refcount, and it's easier not
463 to handle the error case (which only happens with rmmod --wait). */
464 static inline void __module_get(struct module
*module
)
467 unsigned int cpu
= get_cpu();
468 local_inc(__module_ref_addr(module
, cpu
));
469 trace_module_get(module
, _THIS_IP_
,
470 local_read(__module_ref_addr(module
, cpu
)));
475 static inline int try_module_get(struct module
*module
)
480 unsigned int cpu
= get_cpu();
481 if (likely(module_is_live(module
))) {
482 local_inc(__module_ref_addr(module
, cpu
));
483 trace_module_get(module
, _THIS_IP_
,
484 local_read(__module_ref_addr(module
, cpu
)));
493 extern void module_put(struct module
*module
);
495 #else /*!CONFIG_MODULE_UNLOAD*/
496 static inline int try_module_get(struct module
*module
)
498 return !module
|| module_is_live(module
);
500 static inline void module_put(struct module
*module
)
503 static inline void __module_get(struct module
*module
)
506 #define symbol_put(x) do { } while(0)
507 #define symbol_put_addr(p) do { } while(0)
509 #endif /* CONFIG_MODULE_UNLOAD */
510 int use_module(struct module
*a
, struct module
*b
);
512 /* This is a #define so the string doesn't get put in every .o file */
513 #define module_name(mod) \
515 struct module *__mod = (mod); \
516 __mod ? __mod->name : "kernel"; \
519 /* For kallsyms to ask for address resolution. namebuf should be at
520 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
521 * found, otherwise NULL. */
522 const char *module_address_lookup(unsigned long addr
,
523 unsigned long *symbolsize
,
524 unsigned long *offset
,
527 int lookup_module_symbol_name(unsigned long addr
, char *symname
);
528 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
, unsigned long *offset
, char *modname
, char *name
);
530 /* For extable.c to search modules' exception tables. */
531 const struct exception_table_entry
*search_module_extables(unsigned long addr
);
533 int register_module_notifier(struct notifier_block
* nb
);
534 int unregister_module_notifier(struct notifier_block
* nb
);
536 extern void print_modules(void);
538 extern void module_update_markers(void);
540 extern void module_update_tracepoints(void);
541 extern int module_get_iter_tracepoints(struct tracepoint_iter
*iter
);
543 #else /* !CONFIG_MODULES... */
544 #define EXPORT_SYMBOL(sym)
545 #define EXPORT_SYMBOL_GPL(sym)
546 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
547 #define EXPORT_UNUSED_SYMBOL(sym)
548 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
550 /* Given an address, look for it in the exception tables. */
551 static inline const struct exception_table_entry
*
552 search_module_extables(unsigned long addr
)
557 static inline struct module
*__module_address(unsigned long addr
)
562 static inline struct module
*__module_text_address(unsigned long addr
)
567 static inline bool is_module_address(unsigned long addr
)
572 static inline bool is_module_text_address(unsigned long addr
)
577 /* Get/put a kernel symbol (calls should be symmetric) */
578 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
579 #define symbol_put(x) do { } while(0)
580 #define symbol_put_addr(x) do { } while(0)
582 static inline void __module_get(struct module
*module
)
586 static inline int try_module_get(struct module
*module
)
591 static inline void module_put(struct module
*module
)
595 #define module_name(mod) "kernel"
597 /* For kallsyms to ask for address resolution. NULL means not found. */
598 static inline const char *module_address_lookup(unsigned long addr
,
599 unsigned long *symbolsize
,
600 unsigned long *offset
,
607 static inline int lookup_module_symbol_name(unsigned long addr
, char *symname
)
612 static inline int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
, unsigned long *offset
, char *modname
, char *name
)
617 static inline int module_get_kallsym(unsigned int symnum
, unsigned long *value
,
618 char *type
, char *name
,
619 char *module_name
, int *exported
)
624 static inline unsigned long module_kallsyms_lookup_name(const char *name
)
629 static inline int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
637 static inline int register_module_notifier(struct notifier_block
* nb
)
639 /* no events will happen anyway, so this can always succeed */
643 static inline int unregister_module_notifier(struct notifier_block
* nb
)
648 #define module_put_and_exit(code) do_exit(code)
650 static inline void print_modules(void)
654 static inline void module_update_markers(void)
658 static inline void module_update_tracepoints(void)
662 static inline int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
667 #endif /* CONFIG_MODULES */
669 struct device_driver
;
673 extern struct kset
*module_kset
;
674 extern struct kobj_type module_ktype
;
675 extern int module_sysfs_initialized
;
677 int mod_sysfs_init(struct module
*mod
);
678 int mod_sysfs_setup(struct module
*mod
,
679 struct kernel_param
*kparam
,
680 unsigned int num_params
);
681 int module_add_modinfo_attrs(struct module
*mod
);
682 void module_remove_modinfo_attrs(struct module
*mod
);
684 #else /* !CONFIG_SYSFS */
686 static inline int mod_sysfs_init(struct module
*mod
)
691 static inline int mod_sysfs_setup(struct module
*mod
,
692 struct kernel_param
*kparam
,
693 unsigned int num_params
)
698 static inline int module_add_modinfo_attrs(struct module
*mod
)
703 static inline void module_remove_modinfo_attrs(struct module
*mod
)
706 #endif /* CONFIG_SYSFS */
708 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
710 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
712 #define __MODULE_STRING(x) __stringify(x)
715 #ifdef CONFIG_GENERIC_BUG
716 int module_bug_finalize(const Elf_Ehdr
*, const Elf_Shdr
*,
718 void module_bug_cleanup(struct module
*);
720 #else /* !CONFIG_GENERIC_BUG */
722 static inline int module_bug_finalize(const Elf_Ehdr
*hdr
,
723 const Elf_Shdr
*sechdrs
,
728 static inline void module_bug_cleanup(struct module
*mod
) {}
729 #endif /* CONFIG_GENERIC_BUG */
731 #endif /* _LINUX_MODULE_H */