blob: b87e7625a98c5999a71ec9aa0df28971f79f773b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_MODULE_H
2#define _LINUX_MODULE_H
3/*
4 * Dynamic loading of modules into the kernel.
5 *
6 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
7 * Rewritten again by Rusty Russell, 2002
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040018#include <linux/tracepoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Christoph Lametere1783a22010-01-05 15:34:50 +090020#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/module.h>
22
Li Zefan7ead8b82009-08-17 16:56:28 +080023#include <trace/events/module.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Not Yet Implemented */
26#define MODULE_SUPPORTED_DEVICE(name)
27
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000028/* Some toolchains use a `_' prefix for all user symbols. */
29#ifdef CONFIG_SYMBOL_PREFIX
30#define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
31#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define MODULE_SYMBOL_PREFIX ""
33#endif
34
Rusty Russell730b69d2008-10-22 10:00:22 -050035#define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct kernel_symbol
38{
39 unsigned long value;
40 const char *name;
41};
42
43struct modversion_info
44{
45 unsigned long crc;
46 char name[MODULE_NAME_LEN];
47};
48
49struct module;
50
Kay Sievers4befb022011-07-24 22:06:04 +093051struct module_kobject {
52 struct kobject kobj;
53 struct module *mod;
54 struct kobject *drivers_dir;
55 struct module_param_attrs *mp;
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct module_attribute {
Kay Sievers4befb022011-07-24 22:06:04 +093059 struct attribute attr;
60 ssize_t (*show)(struct module_attribute *, struct module_kobject *,
61 char *);
62 ssize_t (*store)(struct module_attribute *, struct module_kobject *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 const char *, size_t count);
Matt Domschc988d2b2005-06-23 22:05:15 -070064 void (*setup)(struct module *, const char *);
65 int (*test)(struct module *);
66 void (*free)(struct module *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Dmitry Torokhove94965e2010-12-15 14:00:19 -080069struct module_version_attribute {
70 struct module_attribute mattr;
71 const char *module_name;
72 const char *version;
Dmitry Torokhov98562ad2011-02-04 13:30:10 -080073} __attribute__ ((__aligned__(sizeof(void *))));
Dmitry Torokhove94965e2010-12-15 14:00:19 -080074
Dmitry Torokhov9b73a582011-02-07 16:02:27 -080075extern ssize_t __modver_version_show(struct module_attribute *,
Kay Sievers4befb022011-07-24 22:06:04 +093076 struct module_kobject *, char *);
Dmitry Torokhov9b73a582011-02-07 16:02:27 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* These are either module local, or the kernel's dummy ones. */
80extern int init_module(void);
81extern void cleanup_module(void);
82
83/* Archs provide a method of finding the correct exception table. */
84struct exception_table_entry;
85
86const struct exception_table_entry *
87search_extable(const struct exception_table_entry *first,
88 const struct exception_table_entry *last,
89 unsigned long value);
90void sort_extable(struct exception_table_entry *start,
91 struct exception_table_entry *finish);
92void sort_main_extable(void);
Rusty Russellad6561d2009-06-12 21:47:03 -060093void trim_init_extable(struct module *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#ifdef MODULE
96#define MODULE_GENERIC_TABLE(gtype,name) \
97extern const struct gtype##_id __mod_##gtype##_table \
98 __attribute__ ((unused, alias(__stringify(name))))
99
100extern struct module __this_module;
101#define THIS_MODULE (&__this_module)
102#else /* !MODULE */
103#define MODULE_GENERIC_TABLE(gtype,name)
104#define THIS_MODULE ((struct module *)0)
105#endif
106
107/* Generic info of form tag = "info" */
108#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
109
110/* For userspace: you can also call me... */
111#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
112
113/*
114 * The following license idents are currently accepted as indicating free
115 * software modules
116 *
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]
Xose Vazquez Perez8d27e902006-06-23 02:05:13 -0700122 * "Dual MIT/GPL" [GNU Public License v2
123 * or MIT license choice]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * "Dual MPL/GPL" [GNU Public License v2
125 * or Mozilla license choice]
126 *
127 * The following other idents are available
128 *
129 * "Proprietary" [Non free products]
130 *
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.
134 *
135 * This exists for several reasons
136 * 1. So modinfo can show license info for users wanting to vet their setup
137 * is free
138 * 2. So the community can ignore bug reports including proprietary modules
139 * 3. So vendors can do likewise based on their own policies
140 */
141#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
142
Johannes Berg1d7015c2009-09-25 00:32:58 -0600143/*
144 * Author(s), use "Name <email>" or just "Name", for multiple
145 * authors use multiple MODULE_AUTHOR() statements/lines.
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
148
149/* What your module does. */
150#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
151
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)
156
157#define MODULE_DEVICE_TABLE(type,name) \
158 MODULE_GENERIC_TABLE(type##_device,name)
159
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
164 "1:2.0".
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".
170
171 Using this automatically adds a checksum of the .c files and the
172 local headers in "srcversion".
173*/
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800174
Rusty Russell3b90a5b2011-01-24 14:32:51 -0600175#if defined(MODULE) || !defined(CONFIG_SYSFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800177#else
178#define MODULE_VERSION(_version) \
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800179 static struct module_version_attribute ___modver_attr = { \
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800180 .mattr = { \
181 .attr = { \
182 .name = "version", \
183 .mode = S_IRUGO, \
184 }, \
185 .show = __modver_version_show, \
186 }, \
187 .module_name = KBUILD_MODNAME, \
188 .version = _version, \
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800189 }; \
190 static const struct module_version_attribute \
191 __used __attribute__ ((__section__ ("__modver"))) \
192 * __moduleparam_const __modver_attr = &___modver_attr
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jon Masters187afbe2006-08-28 17:08:21 -0500195/* 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)
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* Given an address, look for it in the exception tables */
201const struct exception_table_entry *search_exception_tables(unsigned long add);
202
203struct notifier_block;
204
205#ifdef CONFIG_MODULES
206
Dave Young5ed10912010-03-10 15:24:06 -0800207extern int modules_disabled; /* for sysctl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* Get/put a kernel symbol (calls must be symmetric) */
209void *__symbol_get(const char *symbol);
210void *__symbol_get_gpl(const char *symbol);
211#define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
212
Rusty Russellc8e21ce2010-06-05 11:17:35 -0600213/* modules using other modules: kdb wants to see this. */
214struct module_use {
215 struct list_head source_list;
216 struct list_head target_list;
217 struct module *source, *target;
218};
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#ifndef __GENKSYMS__
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 \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100227 __used \
Alessio Igor Boganif02e8a62011-04-14 14:59:39 +0200228 __attribute__((section("___kcrctab" sec "+" #sym), unused)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 = (unsigned long) &__crc_##sym;
230#else
231#define __CRC_SYMBOL(sym, sec)
232#endif
233
234/* For every exported symbol, place a struct in the __ksymtab section */
235#define __EXPORT_SYMBOL(sym, sec) \
Patrick McHardya1a8fee2006-03-23 22:07:34 -0800236 extern typeof(sym) sym; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 __CRC_SYMBOL(sym, sec) \
238 static const char __kstrtab_##sym[] \
Rusty Russellea01e792008-03-13 09:02:17 +0000239 __attribute__((section("__ksymtab_strings"), aligned(1))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 = MODULE_SYMBOL_PREFIX #sym; \
241 static const struct kernel_symbol __ksymtab_##sym \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100242 __used \
Alessio Igor Boganif02e8a62011-04-14 14:59:39 +0200243 __attribute__((section("___ksymtab" sec "+" #sym), unused)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 = { (unsigned long)&sym, __kstrtab_##sym }
245
246#define EXPORT_SYMBOL(sym) \
247 __EXPORT_SYMBOL(sym, "")
248
249#define EXPORT_SYMBOL_GPL(sym) \
250 __EXPORT_SYMBOL(sym, "_gpl")
251
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800252#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
253 __EXPORT_SYMBOL(sym, "_gpl_future")
254
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700255
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")
259#else
260#define EXPORT_UNUSED_SYMBOL(sym)
261#define EXPORT_UNUSED_SYMBOL_GPL(sym)
262#endif
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266enum module_state
267{
268 MODULE_STATE_LIVE,
269 MODULE_STATE_COMING,
270 MODULE_STATE_GOING,
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273struct module
274{
275 enum module_state state;
276
277 /* Member of list of modules */
278 struct list_head list;
279
280 /* Unique handle for this module */
281 char name[MODULE_NAME_LEN];
282
283 /* Sysfs stuff. */
284 struct module_kobject mkobj;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800285 struct module_attribute *modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -0700286 const char *version;
287 const char *srcversion;
Kay Sievers270a6c42007-01-18 13:26:15 +0100288 struct kobject *holders_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Exported symbols */
291 const struct kernel_symbol *syms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 const unsigned long *crcs;
Richard Kennedyaf540682008-07-22 19:24:26 -0500293 unsigned int num_syms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Rusty Russelle180a6b2009-03-31 13:05:29 -0600295 /* Kernel parameters. */
296 struct kernel_param *kp;
297 unsigned int num_kp;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* GPL-only exported symbols. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned int num_gpl_syms;
Richard Kennedyaf540682008-07-22 19:24:26 -0500301 const struct kernel_symbol *gpl_syms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const unsigned long *gpl_crcs;
303
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500304#ifdef CONFIG_UNUSED_SYMBOLS
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700305 /* unused exported symbols. */
306 const struct kernel_symbol *unused_syms;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700307 const unsigned long *unused_crcs;
Richard Kennedyaf540682008-07-22 19:24:26 -0500308 unsigned int num_unused_syms;
309
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700310 /* GPL-only, unused exported symbols. */
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700311 unsigned int num_unused_gpl_syms;
Richard Kennedyaf540682008-07-22 19:24:26 -0500312 const struct kernel_symbol *unused_gpl_syms;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700313 const unsigned long *unused_gpl_crcs;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500314#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700315
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800316 /* symbols that will be GPL-only in the near future. */
317 const struct kernel_symbol *gpl_future_syms;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800318 const unsigned long *gpl_future_crcs;
Richard Kennedyaf540682008-07-22 19:24:26 -0500319 unsigned int num_gpl_future_syms;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Exception table */
322 unsigned int num_exentries;
Rusty Russell5e458cc2008-10-22 10:00:13 -0500323 struct exception_table_entry *extable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Startup function. */
326 int (*init)(void);
327
328 /* If this is non-NULL, vfree after init() returns */
329 void *module_init;
330
331 /* Here is the actual code + data, vfree'd on unload. */
332 void *module_core;
333
334 /* Here are the sizes of the init and core sections */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -0500335 unsigned int init_size, core_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* The size of the executable code in each section. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -0500338 unsigned int init_text_size, core_text_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
matthieu castet84e1c6b2010-11-16 22:35:16 +0100340 /* Size of RO sections of the module (text+rodata) */
341 unsigned int init_ro_size, core_ro_size;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Arch-specific module values */
344 struct mod_arch_specific arch;
345
Randy Dunlap2bc2d612006-10-02 02:17:02 -0700346 unsigned int taints; /* same bits as kernel:tainted */
347
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800348#ifdef CONFIG_GENERIC_BUG
349 /* Support for BUG */
Richard Kennedyaf540682008-07-22 19:24:26 -0500350 unsigned num_bugs;
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800351 struct list_head bug_list;
352 struct bug_entry *bug_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#endif
354
355#ifdef CONFIG_KALLSYMS
Jan Beulich4a496222009-07-06 14:50:42 +0100356 /*
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).
360 */
361 Elf_Sym *symtab, *core_symtab;
362 unsigned int num_symtab, core_num_syms;
Jan Beulich554bdfe2009-07-06 14:51:44 +0100363 char *strtab, *core_strtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* Section attributes */
366 struct module_sect_attrs *sect_attrs;
Roland McGrath6d760132007-10-16 23:26:40 -0700367
368 /* Notes attributes */
369 struct module_notes_attrs *notes_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#endif
371
Richard Kennedya288bd62011-05-19 16:55:25 -0600372 /* The command line arguments (may be mangled). People like
373 keeping pointers to this stuff */
374 char *args;
375
Tejun Heo259354d2010-03-10 18:56:10 +0900376#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Per-cpu data. */
Tejun Heo259354d2010-03-10 18:56:10 +0900378 void __percpu *percpu;
379 unsigned int percpu_size;
380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400382#ifdef CONFIG_TRACEPOINTS
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400383 unsigned int num_tracepoints;
Richard Kennedya288bd62011-05-19 16:55:25 -0600384 struct tracepoint * const *tracepoints_ptrs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400385#endif
Jason Baronbf5438fc2010-09-17 11:09:00 -0400386#ifdef HAVE_JUMP_LABEL
387 struct jump_entry *jump_entries;
388 unsigned int num_jump_entries;
389#endif
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100390#ifdef CONFIG_TRACING
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100391 unsigned int num_trace_bprintk_fmt;
Richard Kennedya288bd62011-05-19 16:55:25 -0600392 const char **trace_bprintk_fmt_start;
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100393#endif
Steven Rostedt6d723732009-04-10 14:53:50 -0400394#ifdef CONFIG_EVENT_TRACING
Steven Rostedte4a9ea52011-01-27 09:15:30 -0500395 struct ftrace_event_call **trace_events;
Steven Rostedt6d723732009-04-10 14:53:50 -0400396 unsigned int num_trace_events;
397#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -0400398#ifdef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedt93eb6772009-04-15 13:24:06 -0400399 unsigned int num_ftrace_callsites;
Richard Kennedya288bd62011-05-19 16:55:25 -0600400 unsigned long *ftrace_callsites;
Steven Rostedt93eb6772009-04-15 13:24:06 -0400401#endif
Lai Jiangshan1ba28e02009-03-06 17:21:48 +0100402
Richard Kennedyaf540682008-07-22 19:24:26 -0500403#ifdef CONFIG_MODULE_UNLOAD
404 /* What modules depend on me? */
Linus Torvalds2c02dfe2010-05-31 12:19:37 -0700405 struct list_head source_list;
406 /* What modules do I depend on? */
407 struct list_head target_list;
Richard Kennedyaf540682008-07-22 19:24:26 -0500408
409 /* Who is waiting for us to be unloaded */
410 struct task_struct *waiter;
411
412 /* Destruction function. */
413 void (*exit)(void);
414
Christoph Lametere1783a22010-01-05 15:34:50 +0900415 struct module_ref {
Nick Piggin5fbfb182010-04-01 19:09:40 +1100416 unsigned int incs;
417 unsigned int decs;
Tejun Heo43cf38e2010-02-02 14:38:57 +0900418 } __percpu *refptr;
Richard Kennedyaf540682008-07-22 19:24:26 -0500419#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -0700420
421#ifdef CONFIG_CONSTRUCTORS
422 /* Constructor functions. */
423 ctor_fn_t *ctors;
424 unsigned int num_ctors;
425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
Roman Zippele61a1c12007-05-09 02:35:15 -0700427#ifndef MODULE_ARCH_INIT
428#define MODULE_ARCH_INIT {}
429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Tim Abbottc6b37802008-12-05 19:03:59 -0500431extern struct mutex module_mutex;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/* 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.*/
436static inline int module_is_live(struct module *mod)
437{
438 return mod->state != MODULE_STATE_GOING;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441struct module *__module_text_address(unsigned long addr);
Rusty Russelle6104992009-03-31 13:05:31 -0600442struct module *__module_address(unsigned long addr);
443bool is_module_address(unsigned long addr);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900444bool is_module_percpu_address(unsigned long addr);
Rusty Russelle6104992009-03-31 13:05:31 -0600445bool is_module_text_address(unsigned long addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Masami Hiramatsua06f6212009-01-06 14:41:49 -0800447static inline int within_module_core(unsigned long addr, struct module *mod)
448{
449 return (unsigned long)mod->module_core <= addr &&
450 addr < (unsigned long)mod->module_core + mod->core_size;
451}
452
453static inline int within_module_init(unsigned long addr, struct module *mod)
454{
455 return (unsigned long)mod->module_init <= addr &&
456 addr < (unsigned long)mod->module_init + mod->init_size;
457}
458
Tim Abbottc6b37802008-12-05 19:03:59 -0500459/* Search for module by name: must hold module_mutex. */
460struct module *find_module(const char *name);
461
462struct symsearch {
463 const struct kernel_symbol *start, *stop;
464 const unsigned long *crcs;
465 enum {
466 NOT_GPL_ONLY,
467 GPL_ONLY,
468 WILL_BE_GPL_ONLY,
469 } licence;
470 bool unused;
471};
472
473/* Search for an exported symbol by name. */
474const struct kernel_symbol *find_symbol(const char *name,
475 struct module **owner,
476 const unsigned long **crc,
477 bool gplok,
478 bool warn);
479
480/* Walk the exported symbol table */
Rusty Russellde4d8d52011-04-19 21:49:58 +0200481bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
482 struct module *owner,
483 void *data), void *data);
Tim Abbottc6b37802008-12-05 19:03:59 -0500484
Alexey Dobriyanea078902007-05-08 00:28:39 -0700485/* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 symnum out of range. */
Alexey Dobriyanea078902007-05-08 00:28:39 -0700487int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
488 char *name, char *module_name, int *exported);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/* Look for this name: can be of form module:name. */
491unsigned long module_kallsyms_lookup_name(const char *name);
492
Anders Kaseorg75a66612008-12-05 19:03:58 -0500493int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
494 struct module *, unsigned long),
495 void *data);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497extern 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);
500
501#ifdef CONFIG_MODULE_UNLOAD
502unsigned int module_refcount(struct module *mod);
503void __symbol_put(const char *symbol);
504#define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
505void symbol_put_addr(void *addr);
506
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). */
509static inline void __module_get(struct module *module)
510{
511 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900512 preempt_disable();
Nick Piggin5fbfb182010-04-01 19:09:40 +1100513 __this_cpu_inc(module->refptr->incs);
Li Zefanae832d12010-03-24 10:57:43 +0800514 trace_module_get(module, _THIS_IP_);
Christoph Lametere1783a22010-01-05 15:34:50 +0900515 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517}
518
519static inline int try_module_get(struct module *module)
520{
521 int ret = 1;
522
523 if (module) {
Christoph Lametere1783a22010-01-05 15:34:50 +0900524 preempt_disable();
525
Li Zefan7ead8b82009-08-17 16:56:28 +0800526 if (likely(module_is_live(module))) {
Nick Piggin5fbfb182010-04-01 19:09:40 +1100527 __this_cpu_inc(module->refptr->incs);
Li Zefanae832d12010-03-24 10:57:43 +0800528 trace_module_get(module, _THIS_IP_);
Nick Piggin5fbfb182010-04-01 19:09:40 +1100529 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ret = 0;
Christoph Lametere1783a22010-01-05 15:34:50 +0900531
532 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 return ret;
535}
536
Al Virof6a57032006-10-18 01:47:25 -0400537extern void module_put(struct module *module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539#else /*!CONFIG_MODULE_UNLOAD*/
540static inline int try_module_get(struct module *module)
541{
542 return !module || module_is_live(module);
543}
544static inline void module_put(struct module *module)
545{
546}
547static inline void __module_get(struct module *module)
548{
549}
550#define symbol_put(x) do { } while(0)
551#define symbol_put_addr(p) do { } while(0)
552
553#endif /* CONFIG_MODULE_UNLOAD */
Anders Kaseorgdfd62d12010-11-24 15:21:10 -0600554int ref_module(struct module *a, struct module *b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556/* This is a #define so the string doesn't get put in every .o file */
557#define module_name(mod) \
558({ \
559 struct module *__mod = (mod); \
560 __mod ? __mod->name : "kernel"; \
561})
562
Rusty Russell6dd06c92008-01-29 17:13:22 -0500563/* 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. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -0800566const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -0500567 unsigned long *symbolsize,
568 unsigned long *offset,
569 char **modname,
570 char *namebuf);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700571int lookup_module_symbol_name(unsigned long addr, char *symname);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700572int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574/* For extable.c to search modules' exception tables. */
575const struct exception_table_entry *search_module_extables(unsigned long addr);
576
577int register_module_notifier(struct notifier_block * nb);
578int unregister_module_notifier(struct notifier_block * nb);
579
580extern void print_modules(void);
581
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400582extern void module_update_tracepoints(void);
583extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#else /* !CONFIG_MODULES... */
586#define EXPORT_SYMBOL(sym)
587#define EXPORT_SYMBOL_GPL(sym)
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800588#define EXPORT_SYMBOL_GPL_FUTURE(sym)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700589#define EXPORT_UNUSED_SYMBOL(sym)
590#define EXPORT_UNUSED_SYMBOL_GPL(sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/* Given an address, look for it in the exception tables. */
593static inline const struct exception_table_entry *
594search_module_extables(unsigned long addr)
595{
596 return NULL;
597}
598
Rusty Russelle6104992009-03-31 13:05:31 -0600599static inline struct module *__module_address(unsigned long addr)
600{
601 return NULL;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static inline struct module *__module_text_address(unsigned long addr)
605{
606 return NULL;
607}
608
Rusty Russelle6104992009-03-31 13:05:31 -0600609static inline bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -0700610{
Rusty Russelle6104992009-03-31 13:05:31 -0600611 return false;
612}
613
Randy Dunlapd5e50da2010-03-31 11:33:42 +0900614static inline bool is_module_percpu_address(unsigned long addr)
615{
616 return false;
617}
618
Rusty Russelle6104992009-03-31 13:05:31 -0600619static inline bool is_module_text_address(unsigned long addr)
620{
621 return false;
Ingo Molnar4d435f92006-07-03 00:24:24 -0700622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* 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)
628
629static inline void __module_get(struct module *module)
630{
631}
632
633static inline int try_module_get(struct module *module)
634{
635 return 1;
636}
637
638static inline void module_put(struct module *module)
639{
640}
641
642#define module_name(mod) "kernel"
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/* For kallsyms to ask for address resolution. NULL means not found. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -0800645static inline const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -0500646 unsigned long *symbolsize,
647 unsigned long *offset,
648 char **modname,
649 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return NULL;
652}
653
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700654static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
655{
656 return -ERANGE;
657}
658
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700659static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
660{
661 return -ERANGE;
662}
663
Alexey Dobriyanea078902007-05-08 00:28:39 -0700664static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
665 char *type, char *name,
666 char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Alexey Dobriyanea078902007-05-08 00:28:39 -0700668 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static inline unsigned long module_kallsyms_lookup_name(const char *name)
672{
673 return 0;
674}
675
Anders Kaseorg75a66612008-12-05 19:03:58 -0500676static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
677 struct module *,
678 unsigned long),
679 void *data)
680{
681 return 0;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static inline int register_module_notifier(struct notifier_block * nb)
685{
686 /* no events will happen anyway, so this can always succeed */
687 return 0;
688}
689
690static inline int unregister_module_notifier(struct notifier_block * nb)
691{
692 return 0;
693}
694
695#define module_put_and_exit(code) do_exit(code)
696
697static inline void print_modules(void)
698{
699}
700
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400701static inline void module_update_tracepoints(void)
702{
703}
704
705static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
706{
707 return 0;
708}
Randy Dunlapef665c12007-02-13 15:19:06 -0800709#endif /* CONFIG_MODULES */
710
Randy Dunlapef665c12007-02-13 15:19:06 -0800711#ifdef CONFIG_SYSFS
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700712extern struct kset *module_kset;
713extern struct kobj_type module_ktype;
714extern int module_sysfs_initialized;
Randy Dunlapef665c12007-02-13 15:19:06 -0800715#endif /* CONFIG_SYSFS */
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
718
719/* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#define __MODULE_STRING(x) __stringify(x)
722
matthieu castet84e1c6b2010-11-16 22:35:16 +0100723#ifdef CONFIG_DEBUG_SET_MODULE_RONX
724extern void set_all_modules_text_rw(void);
725extern void set_all_modules_text_ro(void);
726#else
727static inline void set_all_modules_text_rw(void) { }
728static inline void set_all_modules_text_ro(void) { }
729#endif
Andrew Morton0d9c25d2009-06-16 15:33:37 -0700730
731#ifdef CONFIG_GENERIC_BUG
Linus Torvalds53363772010-10-05 11:29:27 -0700732void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
Andrew Morton0d9c25d2009-06-16 15:33:37 -0700733 struct module *);
734void module_bug_cleanup(struct module *);
735
736#else /* !CONFIG_GENERIC_BUG */
737
Linus Torvalds53363772010-10-05 11:29:27 -0700738static inline void module_bug_finalize(const Elf_Ehdr *hdr,
Andrew Morton0d9c25d2009-06-16 15:33:37 -0700739 const Elf_Shdr *sechdrs,
740 struct module *mod)
741{
Andrew Morton0d9c25d2009-06-16 15:33:37 -0700742}
743static inline void module_bug_cleanup(struct module *mod) {}
744#endif /* CONFIG_GENERIC_BUG */
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#endif /* _LINUX_MODULE_H */