xref: /linux-6.15/include/linux/module.h (revision 2425bcb9)
11da177e4SLinus Torvalds #ifndef _LINUX_MODULE_H
21da177e4SLinus Torvalds #define _LINUX_MODULE_H
31da177e4SLinus Torvalds /*
41da177e4SLinus Torvalds  * Dynamic loading of modules into the kernel.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Rewritten by Richard Henderson <[email protected]> Dec 1996
71da177e4SLinus Torvalds  * Rewritten again by Rusty Russell, 2002
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/list.h>
101da177e4SLinus Torvalds #include <linux/stat.h>
111da177e4SLinus Torvalds #include <linux/compiler.h>
121da177e4SLinus Torvalds #include <linux/cache.h>
131da177e4SLinus Torvalds #include <linux/kmod.h>
141da177e4SLinus Torvalds #include <linux/elf.h>
151da177e4SLinus Torvalds #include <linux/stringify.h>
161da177e4SLinus Torvalds #include <linux/kobject.h>
171da177e4SLinus Torvalds #include <linux/moduleparam.h>
18b2e285fcSSteven Rostedt (Red Hat) #include <linux/jump_label.h>
19f5016932SPaul Gortmaker #include <linux/export.h>
201da177e4SLinus Torvalds 
21e1783a24SChristoph Lameter #include <linux/percpu.h>
221da177e4SLinus Torvalds #include <asm/module.h>
231da177e4SLinus Torvalds 
24106a4ee2SRusty Russell /* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */
25106a4ee2SRusty Russell #define MODULE_SIG_STRING "~Module signature appended~\n"
26106a4ee2SRusty Russell 
271da177e4SLinus Torvalds /* Not Yet Implemented */
281da177e4SLinus Torvalds #define MODULE_SUPPORTED_DEVICE(name)
291da177e4SLinus Torvalds 
30730b69d2SRusty Russell #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
311da177e4SLinus Torvalds 
32e865d06bSSeunghun Lee struct modversion_info {
331da177e4SLinus Torvalds 	unsigned long crc;
341da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct module;
381da177e4SLinus Torvalds 
394befb026SKay Sievers struct module_kobject {
404befb026SKay Sievers 	struct kobject kobj;
414befb026SKay Sievers 	struct module *mod;
424befb026SKay Sievers 	struct kobject *drivers_dir;
434befb026SKay Sievers 	struct module_param_attrs *mp;
44942e4431SLi Zhong 	struct completion *kobj_completion;
454befb026SKay Sievers };
464befb026SKay Sievers 
471da177e4SLinus Torvalds struct module_attribute {
481da177e4SLinus Torvalds 	struct attribute attr;
494befb026SKay Sievers 	ssize_t (*show)(struct module_attribute *, struct module_kobject *,
504befb026SKay Sievers 			char *);
514befb026SKay Sievers 	ssize_t (*store)(struct module_attribute *, struct module_kobject *,
521da177e4SLinus Torvalds 			 const char *, size_t count);
53c988d2b2SMatt Domsch 	void (*setup)(struct module *, const char *);
54c988d2b2SMatt Domsch 	int (*test)(struct module *);
55c988d2b2SMatt Domsch 	void (*free)(struct module *);
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
58e94965edSDmitry Torokhov struct module_version_attribute {
59e94965edSDmitry Torokhov 	struct module_attribute mattr;
60e94965edSDmitry Torokhov 	const char *module_name;
61e94965edSDmitry Torokhov 	const char *version;
6298562ad8SDmitry Torokhov } __attribute__ ((__aligned__(sizeof(void *))));
63e94965edSDmitry Torokhov 
649b73a584SDmitry Torokhov extern ssize_t __modver_version_show(struct module_attribute *,
654befb026SKay Sievers 				     struct module_kobject *, char *);
669b73a584SDmitry Torokhov 
6788bfa324SKay Sievers extern struct module_attribute module_uevent;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* These are either module local, or the kernel's dummy ones. */
701da177e4SLinus Torvalds extern int init_module(void);
711da177e4SLinus Torvalds extern void cleanup_module(void);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Archs provide a method of finding the correct exception table. */
741da177e4SLinus Torvalds struct exception_table_entry;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds const struct exception_table_entry *
771da177e4SLinus Torvalds search_extable(const struct exception_table_entry *first,
781da177e4SLinus Torvalds 	       const struct exception_table_entry *last,
791da177e4SLinus Torvalds 	       unsigned long value);
801da177e4SLinus Torvalds void sort_extable(struct exception_table_entry *start,
811da177e4SLinus Torvalds 		  struct exception_table_entry *finish);
821da177e4SLinus Torvalds void sort_main_extable(void);
83ad6561dfSRusty Russell void trim_init_extable(struct module *m);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Generic info of form tag = "info" */
861da177e4SLinus Torvalds #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* For userspace: you can also call me... */
891da177e4SLinus Torvalds #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
901da177e4SLinus Torvalds 
917cb14ba7SAndreas Robinson /* Soft module dependencies. See man modprobe.d for details.
927cb14ba7SAndreas Robinson  * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
937cb14ba7SAndreas Robinson  */
947cb14ba7SAndreas Robinson #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
957cb14ba7SAndreas Robinson 
961da177e4SLinus Torvalds /*
971da177e4SLinus Torvalds  * The following license idents are currently accepted as indicating free
981da177e4SLinus Torvalds  * software modules
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  *	"GPL"				[GNU Public License v2 or later]
1011da177e4SLinus Torvalds  *	"GPL v2"			[GNU Public License v2]
1021da177e4SLinus Torvalds  *	"GPL and additional rights"	[GNU Public License v2 rights and more]
1031da177e4SLinus Torvalds  *	"Dual BSD/GPL"			[GNU Public License v2
1041da177e4SLinus Torvalds  *					 or BSD license choice]
1058d27e908SXose Vazquez Perez  *	"Dual MIT/GPL"			[GNU Public License v2
1068d27e908SXose Vazquez Perez  *					 or MIT license choice]
1071da177e4SLinus Torvalds  *	"Dual MPL/GPL"			[GNU Public License v2
1081da177e4SLinus Torvalds  *					 or Mozilla license choice]
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  * The following other idents are available
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  *	"Proprietary"			[Non free products]
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * There are dual licensed components, but when running with Linux it is the
1151da177e4SLinus Torvalds  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
1161da177e4SLinus Torvalds  * is a GPL combined work.
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  * This exists for several reasons
1191da177e4SLinus Torvalds  * 1.	So modinfo can show license info for users wanting to vet their setup
1201da177e4SLinus Torvalds  *	is free
1211da177e4SLinus Torvalds  * 2.	So the community can ignore bug reports including proprietary modules
1221da177e4SLinus Torvalds  * 3.	So vendors can do likewise based on their own policies
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
1251da177e4SLinus Torvalds 
1261d7015caSJohannes Berg /*
1271d7015caSJohannes Berg  * Author(s), use "Name <email>" or just "Name", for multiple
1281d7015caSJohannes Berg  * authors use multiple MODULE_AUTHOR() statements/lines.
1291d7015caSJohannes Berg  */
1301da177e4SLinus Torvalds #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* What your module does. */
1331da177e4SLinus Torvalds #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
1341da177e4SLinus Torvalds 
135cff26a51SRusty Russell #ifdef MODULE
136cff26a51SRusty Russell /* Creates an alias so file2alias.c can find device table. */
1371da177e4SLinus Torvalds #define MODULE_DEVICE_TABLE(type, name)					\
1386301939dSAndrey Ryabinin extern const typeof(name) __mod_##type##__##name##_device_table		\
139cff26a51SRusty Russell   __attribute__ ((unused, alias(__stringify(name))))
140cff26a51SRusty Russell #else  /* !MODULE */
141cff26a51SRusty Russell #define MODULE_DEVICE_TABLE(type, name)
142cff26a51SRusty Russell #endif
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /* Version of form [<epoch>:]<version>[-<extra-version>].
145e865d06bSSeunghun Lee  * Or for CVS/RCS ID version, everything but the number is stripped.
146e865d06bSSeunghun Lee  * <epoch>: A (small) unsigned integer which allows you to start versions
147e865d06bSSeunghun Lee  * anew. If not mentioned, it's zero.  eg. "2:1.0" is after
148e865d06bSSeunghun Lee  * "1:2.0".
1491da177e4SLinus Torvalds 
150e865d06bSSeunghun Lee  * <version>: The <version> may contain only alphanumerics and the
151e865d06bSSeunghun Lee  * character `.'.  Ordered by numeric sort for numeric parts,
152e865d06bSSeunghun Lee  * ascii sort for ascii parts (as per RPM or DEB algorithm).
153e865d06bSSeunghun Lee 
154e865d06bSSeunghun Lee  * <extraversion>: Like <version>, but inserted for local
155e865d06bSSeunghun Lee  * customizations, eg "rh3" or "rusty1".
156e865d06bSSeunghun Lee 
157e865d06bSSeunghun Lee  * Using this automatically adds a checksum of the .c files and the
158e865d06bSSeunghun Lee  * local headers in "srcversion".
1591da177e4SLinus Torvalds  */
160e94965edSDmitry Torokhov 
1613b90a5b2SRusty Russell #if defined(MODULE) || !defined(CONFIG_SYSFS)
1621da177e4SLinus Torvalds #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
163e94965edSDmitry Torokhov #else
164e94965edSDmitry Torokhov #define MODULE_VERSION(_version)					\
165b4bc8428SDmitry Torokhov 	static struct module_version_attribute ___modver_attr = {	\
166e94965edSDmitry Torokhov 		.mattr	= {						\
167e94965edSDmitry Torokhov 			.attr	= {					\
168e94965edSDmitry Torokhov 				.name	= "version",			\
169e94965edSDmitry Torokhov 				.mode	= S_IRUGO,			\
170e94965edSDmitry Torokhov 			},						\
171e94965edSDmitry Torokhov 			.show	= __modver_version_show,		\
172e94965edSDmitry Torokhov 		},							\
173e94965edSDmitry Torokhov 		.module_name	= KBUILD_MODNAME,			\
174e94965edSDmitry Torokhov 		.version	= _version,				\
175b4bc8428SDmitry Torokhov 	};								\
176b4bc8428SDmitry Torokhov 	static const struct module_version_attribute			\
177b4bc8428SDmitry Torokhov 	__used __attribute__ ((__section__ ("__modver")))		\
178b4bc8428SDmitry Torokhov 	* __moduleparam_const __modver_attr = &___modver_attr
179e94965edSDmitry Torokhov #endif
1801da177e4SLinus Torvalds 
181187afbedSJon Masters /* Optional firmware file (or files) needed by the module
182187afbedSJon Masters  * format is simply firmware file name.  Multiple firmware
183187afbedSJon Masters  * files require multiple MODULE_FIRMWARE() specifiers */
184187afbedSJon Masters #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
185187afbedSJon Masters 
1861da177e4SLinus Torvalds /* Given an address, look for it in the exception tables */
1871da177e4SLinus Torvalds const struct exception_table_entry *search_exception_tables(unsigned long add);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds struct notifier_block;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds #ifdef CONFIG_MODULES
1921da177e4SLinus Torvalds 
1935ed10910SDave Young extern int modules_disabled; /* for sysctl */
1941da177e4SLinus Torvalds /* Get/put a kernel symbol (calls must be symmetric) */
1951da177e4SLinus Torvalds void *__symbol_get(const char *symbol);
1961da177e4SLinus Torvalds void *__symbol_get_gpl(const char *symbol);
197b92021b0SRusty Russell #define symbol_get(x) ((typeof(&x))(__symbol_get(VMLINUX_SYMBOL_STR(x))))
1981da177e4SLinus Torvalds 
199c8e21cedSRusty Russell /* modules using other modules: kdb wants to see this. */
200c8e21cedSRusty Russell struct module_use {
201c8e21cedSRusty Russell 	struct list_head source_list;
202c8e21cedSRusty Russell 	struct list_head target_list;
203c8e21cedSRusty Russell 	struct module *source, *target;
204c8e21cedSRusty Russell };
205c8e21cedSRusty Russell 
2060d21b0e3SRusty Russell enum module_state {
2070d21b0e3SRusty Russell 	MODULE_STATE_LIVE,	/* Normal state. */
2080d21b0e3SRusty Russell 	MODULE_STATE_COMING,	/* Full formed, running module_init. */
2090d21b0e3SRusty Russell 	MODULE_STATE_GOING,	/* Going away. */
2100d21b0e3SRusty Russell 	MODULE_STATE_UNFORMED,	/* Still setting it up. */
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
213e865d06bSSeunghun Lee struct module {
2141da177e4SLinus Torvalds 	enum module_state state;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* Member of list of modules */
2171da177e4SLinus Torvalds 	struct list_head list;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* Unique handle for this module */
2201da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* Sysfs stuff. */
2231da177e4SLinus Torvalds 	struct module_kobject mkobj;
22403e88ae1SGreg Kroah-Hartman 	struct module_attribute *modinfo_attrs;
225c988d2b2SMatt Domsch 	const char *version;
226c988d2b2SMatt Domsch 	const char *srcversion;
227270a6c4cSKay Sievers 	struct kobject *holders_dir;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* Exported symbols */
2301da177e4SLinus Torvalds 	const struct kernel_symbol *syms;
2311da177e4SLinus Torvalds 	const unsigned long *crcs;
232af540689SRichard Kennedy 	unsigned int num_syms;
2331da177e4SLinus Torvalds 
234e180a6b7SRusty Russell 	/* Kernel parameters. */
235e180a6b7SRusty Russell 	struct kernel_param *kp;
236e180a6b7SRusty Russell 	unsigned int num_kp;
237e180a6b7SRusty Russell 
2381da177e4SLinus Torvalds 	/* GPL-only exported symbols. */
2391da177e4SLinus Torvalds 	unsigned int num_gpl_syms;
240af540689SRichard Kennedy 	const struct kernel_symbol *gpl_syms;
2411da177e4SLinus Torvalds 	const unsigned long *gpl_crcs;
2421da177e4SLinus Torvalds 
243f7f5b675SDenys Vlasenko #ifdef CONFIG_UNUSED_SYMBOLS
244f71d20e9SArjan van de Ven 	/* unused exported symbols. */
245f71d20e9SArjan van de Ven 	const struct kernel_symbol *unused_syms;
246f71d20e9SArjan van de Ven 	const unsigned long *unused_crcs;
247af540689SRichard Kennedy 	unsigned int num_unused_syms;
248af540689SRichard Kennedy 
249f71d20e9SArjan van de Ven 	/* GPL-only, unused exported symbols. */
250f71d20e9SArjan van de Ven 	unsigned int num_unused_gpl_syms;
251af540689SRichard Kennedy 	const struct kernel_symbol *unused_gpl_syms;
252f71d20e9SArjan van de Ven 	const unsigned long *unused_gpl_crcs;
253f7f5b675SDenys Vlasenko #endif
254f71d20e9SArjan van de Ven 
255106a4ee2SRusty Russell #ifdef CONFIG_MODULE_SIG
256106a4ee2SRusty Russell 	/* Signature was verified. */
257106a4ee2SRusty Russell 	bool sig_ok;
258106a4ee2SRusty Russell #endif
259106a4ee2SRusty Russell 
2609f28bb7eSGreg Kroah-Hartman 	/* symbols that will be GPL-only in the near future. */
2619f28bb7eSGreg Kroah-Hartman 	const struct kernel_symbol *gpl_future_syms;
2629f28bb7eSGreg Kroah-Hartman 	const unsigned long *gpl_future_crcs;
263af540689SRichard Kennedy 	unsigned int num_gpl_future_syms;
2649f28bb7eSGreg Kroah-Hartman 
2651da177e4SLinus Torvalds 	/* Exception table */
2661da177e4SLinus Torvalds 	unsigned int num_exentries;
2675e458cc0SRusty Russell 	struct exception_table_entry *extable;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/* Startup function. */
2701da177e4SLinus Torvalds 	int (*init)(void);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/* If this is non-NULL, vfree after init() returns */
2731da177e4SLinus Torvalds 	void *module_init;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Here is the actual code + data, vfree'd on unload. */
2761da177e4SLinus Torvalds 	void *module_core;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/* Here are the sizes of the init and core sections */
2792f0f2a33SDenys Vlasenko 	unsigned int init_size, core_size;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	/* The size of the executable code in each section.  */
2822f0f2a33SDenys Vlasenko 	unsigned int init_text_size, core_text_size;
2831da177e4SLinus Torvalds 
28484e1c6bbSmatthieu castet 	/* Size of RO sections of the module (text+rodata) */
28584e1c6bbSmatthieu castet 	unsigned int init_ro_size, core_ro_size;
28684e1c6bbSmatthieu castet 
2871da177e4SLinus Torvalds 	/* Arch-specific module values */
2881da177e4SLinus Torvalds 	struct mod_arch_specific arch;
2891da177e4SLinus Torvalds 
2902bc2d61aSRandy Dunlap 	unsigned int taints;	/* same bits as kernel:tainted */
2912bc2d61aSRandy Dunlap 
2927664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
2937664c5a1SJeremy Fitzhardinge 	/* Support for BUG */
294af540689SRichard Kennedy 	unsigned num_bugs;
2957664c5a1SJeremy Fitzhardinge 	struct list_head bug_list;
2967664c5a1SJeremy Fitzhardinge 	struct bug_entry *bug_table;
2971da177e4SLinus Torvalds #endif
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3004a496226SJan Beulich 	/*
3014a496226SJan Beulich 	 * We keep the symbol and string tables for kallsyms.
3024a496226SJan Beulich 	 * The core_* fields below are temporary, loader-only (they
3034a496226SJan Beulich 	 * could really be discarded after module init).
3044a496226SJan Beulich 	 */
3054a496226SJan Beulich 	Elf_Sym *symtab, *core_symtab;
3064a496226SJan Beulich 	unsigned int num_symtab, core_num_syms;
307554bdfe5SJan Beulich 	char *strtab, *core_strtab;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* Section attributes */
3101da177e4SLinus Torvalds 	struct module_sect_attrs *sect_attrs;
3116d760133SRoland McGrath 
3126d760133SRoland McGrath 	/* Notes attributes */
3136d760133SRoland McGrath 	struct module_notes_attrs *notes_attrs;
3141da177e4SLinus Torvalds #endif
3151da177e4SLinus Torvalds 
316a288bd65SRichard Kennedy 	/* The command line arguments (may be mangled).  People like
317a288bd65SRichard Kennedy 	   keeping pointers to this stuff */
318a288bd65SRichard Kennedy 	char *args;
319a288bd65SRichard Kennedy 
320259354deSTejun Heo #ifdef CONFIG_SMP
3211da177e4SLinus Torvalds 	/* Per-cpu data. */
322259354deSTejun Heo 	void __percpu *percpu;
323259354deSTejun Heo 	unsigned int percpu_size;
324259354deSTejun Heo #endif
3251da177e4SLinus Torvalds 
32697e1c18eSMathieu Desnoyers #ifdef CONFIG_TRACEPOINTS
32797e1c18eSMathieu Desnoyers 	unsigned int num_tracepoints;
328a288bd65SRichard Kennedy 	struct tracepoint * const *tracepoints_ptrs;
32997e1c18eSMathieu Desnoyers #endif
330bf5438fcSJason Baron #ifdef HAVE_JUMP_LABEL
331bf5438fcSJason Baron 	struct jump_entry *jump_entries;
332bf5438fcSJason Baron 	unsigned int num_jump_entries;
333bf5438fcSJason Baron #endif
334769b0441SFrederic Weisbecker #ifdef CONFIG_TRACING
3351ba28e02SLai Jiangshan 	unsigned int num_trace_bprintk_fmt;
336a288bd65SRichard Kennedy 	const char **trace_bprintk_fmt_start;
3371ba28e02SLai Jiangshan #endif
3386d723736SSteven Rostedt #ifdef CONFIG_EVENT_TRACING
339*2425bcb9SSteven Rostedt (Red Hat) 	struct trace_event_call **trace_events;
3406d723736SSteven Rostedt 	unsigned int num_trace_events;
3413673b8e4SSteven Rostedt (Red Hat) 	struct trace_enum_map **trace_enums;
3423673b8e4SSteven Rostedt (Red Hat) 	unsigned int num_trace_enums;
3436d723736SSteven Rostedt #endif
34493eb677dSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
34593eb677dSSteven Rostedt 	unsigned int num_ftrace_callsites;
346a288bd65SRichard Kennedy 	unsigned long *ftrace_callsites;
34793eb677dSSteven Rostedt #endif
3481ba28e02SLai Jiangshan 
3498cb2c2dcSPetr Mladek #ifdef CONFIG_LIVEPATCH
3508cb2c2dcSPetr Mladek 	bool klp_alive;
3518cb2c2dcSPetr Mladek #endif
3528cb2c2dcSPetr Mladek 
353af540689SRichard Kennedy #ifdef CONFIG_MODULE_UNLOAD
354af540689SRichard Kennedy 	/* What modules depend on me? */
3552c02dfe7SLinus Torvalds 	struct list_head source_list;
3562c02dfe7SLinus Torvalds 	/* What modules do I depend on? */
3572c02dfe7SLinus Torvalds 	struct list_head target_list;
358af540689SRichard Kennedy 
359af540689SRichard Kennedy 	/* Destruction function. */
360af540689SRichard Kennedy 	void (*exit)(void);
361af540689SRichard Kennedy 
3622f35c41fSMasami Hiramatsu 	atomic_t refcnt;
363af540689SRichard Kennedy #endif
364b99b87f7SPeter Oberparleiter 
365b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
366b99b87f7SPeter Oberparleiter 	/* Constructor functions. */
367b99b87f7SPeter Oberparleiter 	ctor_fn_t *ctors;
368b99b87f7SPeter Oberparleiter 	unsigned int num_ctors;
369b99b87f7SPeter Oberparleiter #endif
3701da177e4SLinus Torvalds };
371e61a1c1cSRoman Zippel #ifndef MODULE_ARCH_INIT
372e61a1c1cSRoman Zippel #define MODULE_ARCH_INIT {}
373e61a1c1cSRoman Zippel #endif
3741da177e4SLinus Torvalds 
375c6b37801STim Abbott extern struct mutex module_mutex;
376c6b37801STim Abbott 
3771da177e4SLinus Torvalds /* FIXME: It'd be nice to isolate modules during init, too, so they
3781da177e4SLinus Torvalds    aren't used before they (may) fail.  But presently too much code
3791da177e4SLinus Torvalds    (IDE & SCSI) require entry into the module during init.*/
3801da177e4SLinus Torvalds static inline int module_is_live(struct module *mod)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	return mod->state != MODULE_STATE_GOING;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds struct module *__module_text_address(unsigned long addr);
386e610499eSRusty Russell struct module *__module_address(unsigned long addr);
387e610499eSRusty Russell bool is_module_address(unsigned long addr);
38810fad5e4STejun Heo bool is_module_percpu_address(unsigned long addr);
389e610499eSRusty Russell bool is_module_text_address(unsigned long addr);
3901da177e4SLinus Torvalds 
39176681c8fSPetr Mladek static inline bool within_module_core(unsigned long addr,
39276681c8fSPetr Mladek 				      const struct module *mod)
393a06f6211SMasami Hiramatsu {
394a06f6211SMasami Hiramatsu 	return (unsigned long)mod->module_core <= addr &&
395a06f6211SMasami Hiramatsu 	       addr < (unsigned long)mod->module_core + mod->core_size;
396a06f6211SMasami Hiramatsu }
397a06f6211SMasami Hiramatsu 
39876681c8fSPetr Mladek static inline bool within_module_init(unsigned long addr,
39976681c8fSPetr Mladek 				      const struct module *mod)
400a06f6211SMasami Hiramatsu {
401a06f6211SMasami Hiramatsu 	return (unsigned long)mod->module_init <= addr &&
402a06f6211SMasami Hiramatsu 	       addr < (unsigned long)mod->module_init + mod->init_size;
403a06f6211SMasami Hiramatsu }
404a06f6211SMasami Hiramatsu 
40576681c8fSPetr Mladek static inline bool within_module(unsigned long addr, const struct module *mod)
4069b20a352SPetr Mladek {
4079b20a352SPetr Mladek 	return within_module_init(addr, mod) || within_module_core(addr, mod);
4089b20a352SPetr Mladek }
4099b20a352SPetr Mladek 
410c6b37801STim Abbott /* Search for module by name: must hold module_mutex. */
411c6b37801STim Abbott struct module *find_module(const char *name);
412c6b37801STim Abbott 
413c6b37801STim Abbott struct symsearch {
414c6b37801STim Abbott 	const struct kernel_symbol *start, *stop;
415c6b37801STim Abbott 	const unsigned long *crcs;
416c6b37801STim Abbott 	enum {
417c6b37801STim Abbott 		NOT_GPL_ONLY,
418c6b37801STim Abbott 		GPL_ONLY,
419c6b37801STim Abbott 		WILL_BE_GPL_ONLY,
420c6b37801STim Abbott 	} licence;
421c6b37801STim Abbott 	bool unused;
422c6b37801STim Abbott };
423c6b37801STim Abbott 
424c6b37801STim Abbott /* Search for an exported symbol by name. */
425c6b37801STim Abbott const struct kernel_symbol *find_symbol(const char *name,
426c6b37801STim Abbott 					struct module **owner,
427c6b37801STim Abbott 					const unsigned long **crc,
428c6b37801STim Abbott 					bool gplok,
429c6b37801STim Abbott 					bool warn);
430c6b37801STim Abbott 
431c6b37801STim Abbott /* Walk the exported symbol table */
432de4d8d53SRusty Russell bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
433de4d8d53SRusty Russell 				    struct module *owner,
434de4d8d53SRusty Russell 				    void *data), void *data);
435c6b37801STim Abbott 
436ea07890aSAlexey Dobriyan /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
4371da177e4SLinus Torvalds    symnum out of range. */
438ea07890aSAlexey Dobriyan int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
439ea07890aSAlexey Dobriyan 			char *name, char *module_name, int *exported);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds /* Look for this name: can be of form module:name. */
4421da177e4SLinus Torvalds unsigned long module_kallsyms_lookup_name(const char *name);
4431da177e4SLinus Torvalds 
44475a66614SAnders Kaseorg int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
44575a66614SAnders Kaseorg 					     struct module *, unsigned long),
44675a66614SAnders Kaseorg 				   void *data);
44775a66614SAnders Kaseorg 
4481da177e4SLinus Torvalds extern void __module_put_and_exit(struct module *mod, long code)
4491da177e4SLinus Torvalds 	__attribute__((noreturn));
45074e22facSJoe Perches #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code)
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds #ifdef CONFIG_MODULE_UNLOAD
453d5db139aSRusty Russell int module_refcount(struct module *mod);
4541da177e4SLinus Torvalds void __symbol_put(const char *symbol);
455b92021b0SRusty Russell #define symbol_put(x) __symbol_put(VMLINUX_SYMBOL_STR(x))
4561da177e4SLinus Torvalds void symbol_put_addr(void *addr);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds /* Sometimes we know we already have a refcount, and it's easier not
4591da177e4SLinus Torvalds    to handle the error case (which only happens with rmmod --wait). */
460d53799beSSteven Rostedt extern void __module_get(struct module *module);
4611da177e4SLinus Torvalds 
462d53799beSSteven Rostedt /* This is the Right Way to get a module: if it fails, it's being removed,
463d53799beSSteven Rostedt  * so pretend it's not there. */
464d53799beSSteven Rostedt extern bool try_module_get(struct module *module);
4651da177e4SLinus Torvalds 
466f6a57033SAl Viro extern void module_put(struct module *module);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds #else /*!CONFIG_MODULE_UNLOAD*/
4691da177e4SLinus Torvalds static inline int try_module_get(struct module *module)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	return !module || module_is_live(module);
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds static inline void module_put(struct module *module)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds static inline void __module_get(struct module *module)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
4801da177e4SLinus Torvalds #define symbol_put_addr(p) do { } while (0)
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds #endif /* CONFIG_MODULE_UNLOAD */
483dfd62d1dSAnders Kaseorg int ref_module(struct module *a, struct module *b);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* This is a #define so the string doesn't get put in every .o file */
4861da177e4SLinus Torvalds #define module_name(mod)			\
4871da177e4SLinus Torvalds ({						\
4881da177e4SLinus Torvalds 	struct module *__mod = (mod);		\
4891da177e4SLinus Torvalds 	__mod ? __mod->name : "kernel";		\
4901da177e4SLinus Torvalds })
4911da177e4SLinus Torvalds 
4926dd06c9fSRusty Russell /* For kallsyms to ask for address resolution.  namebuf should be at
4936dd06c9fSRusty Russell  * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
4946dd06c9fSRusty Russell  * found, otherwise NULL. */
49592dfc9dcSAndrew Morton const char *module_address_lookup(unsigned long addr,
4961da177e4SLinus Torvalds 			    unsigned long *symbolsize,
4971da177e4SLinus Torvalds 			    unsigned long *offset,
4986dd06c9fSRusty Russell 			    char **modname,
4996dd06c9fSRusty Russell 			    char *namebuf);
5009d65cb4aSAlexey Dobriyan int lookup_module_symbol_name(unsigned long addr, char *symname);
501a5c43daeSAlexey Dobriyan int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /* For extable.c to search modules' exception tables. */
5041da177e4SLinus Torvalds const struct exception_table_entry *search_module_extables(unsigned long addr);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds int register_module_notifier(struct notifier_block *nb);
5071da177e4SLinus Torvalds int unregister_module_notifier(struct notifier_block *nb);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds extern void print_modules(void);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds #else /* !CONFIG_MODULES... */
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /* Given an address, look for it in the exception tables. */
5141da177e4SLinus Torvalds static inline const struct exception_table_entry *
5151da177e4SLinus Torvalds search_module_extables(unsigned long addr)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	return NULL;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
520e610499eSRusty Russell static inline struct module *__module_address(unsigned long addr)
521e610499eSRusty Russell {
522e610499eSRusty Russell 	return NULL;
523e610499eSRusty Russell }
524e610499eSRusty Russell 
5251da177e4SLinus Torvalds static inline struct module *__module_text_address(unsigned long addr)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	return NULL;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
530e610499eSRusty Russell static inline bool is_module_address(unsigned long addr)
5314d435f9dSIngo Molnar {
532e610499eSRusty Russell 	return false;
533e610499eSRusty Russell }
534e610499eSRusty Russell 
535d5e50dafSRandy Dunlap static inline bool is_module_percpu_address(unsigned long addr)
536d5e50dafSRandy Dunlap {
537d5e50dafSRandy Dunlap 	return false;
538d5e50dafSRandy Dunlap }
539d5e50dafSRandy Dunlap 
540e610499eSRusty Russell static inline bool is_module_text_address(unsigned long addr)
541e610499eSRusty Russell {
542e610499eSRusty Russell 	return false;
5434d435f9dSIngo Molnar }
5444d435f9dSIngo Molnar 
5451da177e4SLinus Torvalds /* Get/put a kernel symbol (calls should be symmetric) */
5461da177e4SLinus Torvalds #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
5471da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
5481da177e4SLinus Torvalds #define symbol_put_addr(x) do { } while (0)
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static inline void __module_get(struct module *module)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds static inline int try_module_get(struct module *module)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	return 1;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds static inline void module_put(struct module *module)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds #define module_name(mod) "kernel"
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /* For kallsyms to ask for address resolution.  NULL means not found. */
56692dfc9dcSAndrew Morton static inline const char *module_address_lookup(unsigned long addr,
5671da177e4SLinus Torvalds 					  unsigned long *symbolsize,
5681da177e4SLinus Torvalds 					  unsigned long *offset,
5696dd06c9fSRusty Russell 					  char **modname,
5706dd06c9fSRusty Russell 					  char *namebuf)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	return NULL;
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5759d65cb4aSAlexey Dobriyan static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
5769d65cb4aSAlexey Dobriyan {
5779d65cb4aSAlexey Dobriyan 	return -ERANGE;
5789d65cb4aSAlexey Dobriyan }
5799d65cb4aSAlexey Dobriyan 
580a5c43daeSAlexey Dobriyan static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
581a5c43daeSAlexey Dobriyan {
582a5c43daeSAlexey Dobriyan 	return -ERANGE;
583a5c43daeSAlexey Dobriyan }
584a5c43daeSAlexey Dobriyan 
585ea07890aSAlexey Dobriyan static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
586ea07890aSAlexey Dobriyan 					char *type, char *name,
587ea07890aSAlexey Dobriyan 					char *module_name, int *exported)
5881da177e4SLinus Torvalds {
589ea07890aSAlexey Dobriyan 	return -ERANGE;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds static inline unsigned long module_kallsyms_lookup_name(const char *name)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	return 0;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
59775a66614SAnders Kaseorg static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
59875a66614SAnders Kaseorg 							   struct module *,
59975a66614SAnders Kaseorg 							   unsigned long),
60075a66614SAnders Kaseorg 						 void *data)
60175a66614SAnders Kaseorg {
60275a66614SAnders Kaseorg 	return 0;
60375a66614SAnders Kaseorg }
60475a66614SAnders Kaseorg 
6051da177e4SLinus Torvalds static inline int register_module_notifier(struct notifier_block *nb)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	/* no events will happen anyway, so this can always succeed */
6081da177e4SLinus Torvalds 	return 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds static inline int unregister_module_notifier(struct notifier_block *nb)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds #define module_put_and_exit(code) do_exit(code)
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds static inline void print_modules(void)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds }
621ef665c1aSRandy Dunlap #endif /* CONFIG_MODULES */
622ef665c1aSRandy Dunlap 
623ef665c1aSRandy Dunlap #ifdef CONFIG_SYSFS
6247405c1e1SGreg Kroah-Hartman extern struct kset *module_kset;
6257405c1e1SGreg Kroah-Hartman extern struct kobj_type module_ktype;
6267405c1e1SGreg Kroah-Hartman extern int module_sysfs_initialized;
627ef665c1aSRandy Dunlap #endif /* CONFIG_SYSFS */
628ef665c1aSRandy Dunlap 
6291da177e4SLinus Torvalds #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds #define __MODULE_STRING(x) __stringify(x)
6341da177e4SLinus Torvalds 
63584e1c6bbSmatthieu castet #ifdef CONFIG_DEBUG_SET_MODULE_RONX
63684e1c6bbSmatthieu castet extern void set_all_modules_text_rw(void);
63784e1c6bbSmatthieu castet extern void set_all_modules_text_ro(void);
63884e1c6bbSmatthieu castet #else
63984e1c6bbSmatthieu castet static inline void set_all_modules_text_rw(void) { }
64084e1c6bbSmatthieu castet static inline void set_all_modules_text_ro(void) { }
64184e1c6bbSmatthieu castet #endif
6420d9c25ddSAndrew Morton 
6430d9c25ddSAndrew Morton #ifdef CONFIG_GENERIC_BUG
6445336377dSLinus Torvalds void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
6450d9c25ddSAndrew Morton 			 struct module *);
6460d9c25ddSAndrew Morton void module_bug_cleanup(struct module *);
6470d9c25ddSAndrew Morton 
6480d9c25ddSAndrew Morton #else	/* !CONFIG_GENERIC_BUG */
6490d9c25ddSAndrew Morton 
6505336377dSLinus Torvalds static inline void module_bug_finalize(const Elf_Ehdr *hdr,
6510d9c25ddSAndrew Morton 					const Elf_Shdr *sechdrs,
6520d9c25ddSAndrew Morton 					struct module *mod)
6530d9c25ddSAndrew Morton {
6540d9c25ddSAndrew Morton }
6550d9c25ddSAndrew Morton static inline void module_bug_cleanup(struct module *mod) {}
6560d9c25ddSAndrew Morton #endif	/* CONFIG_GENERIC_BUG */
6570d9c25ddSAndrew Morton 
6581da177e4SLinus Torvalds #endif /* _LINUX_MODULE_H */
659