blob: f2d2af342aa5cc48b85b0b4ad76a5c56b05e0c96 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001#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 */
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>
20
21#include <linux/percpu.h>
22#include <asm/module.h>
23
24#define MODULE_SUPPORTED_DEVICE(name)
25
26#define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
27
28struct modversion_info
29{
30 unsigned long crc;
31 char name[MODULE_NAME_LEN];
32};
33
34struct module;
35
36struct module_kobject {
37 struct kobject kobj;
38 struct module *mod;
39 struct kobject *drivers_dir;
40 struct module_param_attrs *mp;
41};
42
43struct module_attribute {
44 struct attribute attr;
45 ssize_t (*show)(struct module_attribute *, struct module_kobject *,
46 char *);
47 ssize_t (*store)(struct module_attribute *, struct module_kobject *,
48 const char *, size_t count);
49 void (*setup)(struct module *, const char *);
50 int (*test)(struct module *);
51 void (*free)(struct module *);
52};
53
54struct module_version_attribute {
55 struct module_attribute mattr;
56 const char *module_name;
57 const char *version;
58} __attribute__ ((__aligned__(sizeof(void *))));
59
60extern ssize_t __modver_version_show(struct module_attribute *,
61 struct module_kobject *, char *);
62
63extern struct module_attribute module_uevent;
64
65extern int init_module(void);
66extern void cleanup_module(void);
67
68struct exception_table_entry;
69
70const struct exception_table_entry *
71search_extable(const struct exception_table_entry *first,
72 const struct exception_table_entry *last,
73 unsigned long value);
74void sort_extable(struct exception_table_entry *start,
75 struct exception_table_entry *finish);
76void sort_main_extable(void);
77void trim_init_extable(struct module *m);
78
79#ifdef MODULE
80#define MODULE_GENERIC_TABLE(gtype,name) \
81extern const struct gtype##_id __mod_##gtype##_table \
82 __attribute__ ((unused, alias(__stringify(name))))
83
84#else
85#define MODULE_GENERIC_TABLE(gtype,name)
86#endif
87
88#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
89
90#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
91
92/*
93 * The following license idents are currently accepted as indicating free
94 * software modules
95 *
96 * "GPL" [GNU Public License v2 or later]
97 * "GPL v2" [GNU Public License v2]
98 * "GPL and additional rights" [GNU Public License v2 rights and more]
99 * "Dual BSD/GPL" [GNU Public License v2
100 * or BSD license choice]
101 * "Dual MIT/GPL" [GNU Public License v2
102 * or MIT license choice]
103 * "Dual MPL/GPL" [GNU Public License v2
104 * or Mozilla license choice]
105 *
106 * The following other idents are available
107 *
108 * "Proprietary" [Non free products]
109 *
110 * There are dual licensed components, but when running with Linux it is the
111 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
112 * is a GPL combined work.
113 *
114 * This exists for several reasons
115 * 1. So modinfo can show license info for users wanting to vet their setup
116 * is free
117 * 2. So the community can ignore bug reports including proprietary modules
118 * 3. So vendors can do likewise based on their own policies
119 */
120#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
121
122#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
123
124#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
125
126#define MODULE_DEVICE_TABLE(type,name) \
127 MODULE_GENERIC_TABLE(type##_device,name)
128
129
130#if defined(MODULE) || !defined(CONFIG_SYSFS)
131#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
132#else
133#define MODULE_VERSION(_version) \
134 static struct module_version_attribute ___modver_attr = { \
135 .mattr = { \
136 .attr = { \
137 .name = "version", \
138 .mode = S_IRUGO, \
139 }, \
140 .show = __modver_version_show, \
141 }, \
142 .module_name = KBUILD_MODNAME, \
143 .version = _version, \
144 }; \
145 static const struct module_version_attribute \
146 __used __attribute__ ((__section__ ("__modver"))) \
147 * __moduleparam_const __modver_attr = &___modver_attr
148#endif
149
150#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
151
152const struct exception_table_entry *search_exception_tables(unsigned long add);
153
154struct notifier_block;
155
156#ifdef CONFIG_MODULES
157
158extern int modules_disabled;
159void *__symbol_get(const char *symbol);
160void *__symbol_get_gpl(const char *symbol);
161#define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
162
163struct module_use {
164 struct list_head source_list;
165 struct list_head target_list;
166 struct module *source, *target;
167};
168
169enum module_state
170{
171 MODULE_STATE_LIVE,
172 MODULE_STATE_COMING,
173 MODULE_STATE_GOING,
174};
175
176struct module_ref {
177 unsigned long incs;
178 unsigned long decs;
179} __attribute((aligned(2 * sizeof(unsigned long))));
180
181struct module
182{
183 enum module_state state;
184
185
186 struct list_head list;
187
188
189 char name[MODULE_NAME_LEN];
190
191
192 struct module_kobject mkobj;
193 struct module_attribute *modinfo_attrs;
194 const char *version;
195 const char *srcversion;
196 struct kobject *holders_dir;
197
198
199 const struct kernel_symbol *syms;
200 const unsigned long *crcs;
201 unsigned int num_syms;
202
203
204 struct kernel_param *kp;
205 unsigned int num_kp;
206
207 /* GPL-only exported symbols. */
208 unsigned int num_gpl_syms;
209 const struct kernel_symbol *gpl_syms;
210 const unsigned long *gpl_crcs;
211
212#ifdef CONFIG_UNUSED_SYMBOLS
213
214 const struct kernel_symbol *unused_syms;
215 const unsigned long *unused_crcs;
216 unsigned int num_unused_syms;
217
218 /* GPL-only, unused exported symbols. */
219 unsigned int num_unused_gpl_syms;
220 const struct kernel_symbol *unused_gpl_syms;
221 const unsigned long *unused_gpl_crcs;
222#endif
223
224 /* symbols that will be GPL-only in the near future. */
225 const struct kernel_symbol *gpl_future_syms;
226 const unsigned long *gpl_future_crcs;
227 unsigned int num_gpl_future_syms;
228
229
230 unsigned int num_exentries;
231 struct exception_table_entry *extable;
232
233
234 int (*init)(void);
235
236
237 void *module_init;
238
239
240 void *module_core;
241
242
243 unsigned int init_size, core_size;
244
245
246 unsigned int init_text_size, core_text_size;
247
248
249 unsigned int init_ro_size, core_ro_size;
250
251
252 struct mod_arch_specific arch;
253
254 unsigned int taints;
255
256#ifdef CONFIG_GENERIC_BUG
257
258 unsigned num_bugs;
259 struct list_head bug_list;
260 struct bug_entry *bug_table;
261#endif
262
263#ifdef CONFIG_KALLSYMS
264 Elf_Sym *symtab, *core_symtab;
265 unsigned int num_symtab, core_num_syms;
266 char *strtab, *core_strtab;
267
268
269 struct module_sect_attrs *sect_attrs;
270
271
272 struct module_notes_attrs *notes_attrs;
273#endif
274
275 char *args;
276
277#ifdef CONFIG_SMP
278
279 void __percpu *percpu;
280 unsigned int percpu_size;
281#endif
282
283#ifdef CONFIG_TRACEPOINTS
284 unsigned int num_tracepoints;
285 struct tracepoint * const *tracepoints_ptrs;
286#endif
287#ifdef HAVE_JUMP_LABEL
288 struct jump_entry *jump_entries;
289 unsigned int num_jump_entries;
290#endif
291#ifdef CONFIG_TRACING
292 unsigned int num_trace_bprintk_fmt;
293 const char **trace_bprintk_fmt_start;
294#endif
295#ifdef CONFIG_EVENT_TRACING
296 struct ftrace_event_call **trace_events;
297 unsigned int num_trace_events;
298#endif
299#ifdef CONFIG_FTRACE_MCOUNT_RECORD
300 unsigned int num_ftrace_callsites;
301 unsigned long *ftrace_callsites;
302#endif
303
304#ifdef CONFIG_MODULE_UNLOAD
305
306 struct list_head source_list;
307
308 struct list_head target_list;
309
310
311 struct task_struct *waiter;
312
313
314 void (*exit)(void);
315
316 struct module_ref __percpu *refptr;
317#endif
318
319#ifdef CONFIG_CONSTRUCTORS
320
321 ctor_fn_t *ctors;
322 unsigned int num_ctors;
323#endif
324};
325#ifndef MODULE_ARCH_INIT
326#define MODULE_ARCH_INIT {}
327#endif
328
329extern struct mutex module_mutex;
330
331static inline int module_is_live(struct module *mod)
332{
333 return mod->state != MODULE_STATE_GOING;
334}
335
336struct module *__module_text_address(unsigned long addr);
337struct module *__module_address(unsigned long addr);
338bool is_module_address(unsigned long addr);
339bool is_module_percpu_address(unsigned long addr);
340bool is_module_text_address(unsigned long addr);
341
342static inline int within_module_core(unsigned long addr, struct module *mod)
343{
344 return (unsigned long)mod->module_core <= addr &&
345 addr < (unsigned long)mod->module_core + mod->core_size;
346}
347
348static inline int within_module_init(unsigned long addr, struct module *mod)
349{
350 return (unsigned long)mod->module_init <= addr &&
351 addr < (unsigned long)mod->module_init + mod->init_size;
352}
353
354struct module *find_module(const char *name);
355
356struct symsearch {
357 const struct kernel_symbol *start, *stop;
358 const unsigned long *crcs;
359 enum {
360 NOT_GPL_ONLY,
361 GPL_ONLY,
362 WILL_BE_GPL_ONLY,
363 } licence;
364 bool unused;
365};
366
367const struct kernel_symbol *find_symbol(const char *name,
368 struct module **owner,
369 const unsigned long **crc,
370 bool gplok,
371 bool warn);
372
373bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
374 struct module *owner,
375 void *data), void *data);
376
377int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
378 char *name, char *module_name, int *exported);
379
380unsigned long module_kallsyms_lookup_name(const char *name);
381
382int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
383 struct module *, unsigned long),
384 void *data);
385
386extern void __module_put_and_exit(struct module *mod, long code)
387 __attribute__((noreturn));
388#define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
389
390#ifdef CONFIG_MODULE_UNLOAD
391unsigned long module_refcount(struct module *mod);
392void __symbol_put(const char *symbol);
393#define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
394void symbol_put_addr(void *addr);
395
396extern void __module_get(struct module *module);
397
398extern bool try_module_get(struct module *module);
399
400extern void module_put(struct module *module);
401
402#else
403static inline int try_module_get(struct module *module)
404{
405 return !module || module_is_live(module);
406}
407static inline void module_put(struct module *module)
408{
409}
410static inline void __module_get(struct module *module)
411{
412}
413#define symbol_put(x) do { } while(0)
414#define symbol_put_addr(p) do { } while(0)
415
416#endif
417int ref_module(struct module *a, struct module *b);
418
419#define module_name(mod) \
420({ \
421 struct module *__mod = (mod); \
422 __mod ? __mod->name : "kernel"; \
423})
424
425const char *module_address_lookup(unsigned long addr,
426 unsigned long *symbolsize,
427 unsigned long *offset,
428 char **modname,
429 char *namebuf);
430int lookup_module_symbol_name(unsigned long addr, char *symname);
431int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
432
433const struct exception_table_entry *search_module_extables(unsigned long addr);
434
435int register_module_notifier(struct notifier_block * nb);
436int unregister_module_notifier(struct notifier_block * nb);
437
438extern void print_modules(void);
439
440#else
441
442static inline const struct exception_table_entry *
443search_module_extables(unsigned long addr)
444{
445 return NULL;
446}
447
448static inline struct module *__module_address(unsigned long addr)
449{
450 return NULL;
451}
452
453static inline struct module *__module_text_address(unsigned long addr)
454{
455 return NULL;
456}
457
458static inline bool is_module_address(unsigned long addr)
459{
460 return false;
461}
462
463static inline bool is_module_percpu_address(unsigned long addr)
464{
465 return false;
466}
467
468static inline bool is_module_text_address(unsigned long addr)
469{
470 return false;
471}
472
473#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
474#define symbol_put(x) do { } while(0)
475#define symbol_put_addr(x) do { } while(0)
476
477static inline void __module_get(struct module *module)
478{
479}
480
481static inline int try_module_get(struct module *module)
482{
483 return 1;
484}
485
486static inline void module_put(struct module *module)
487{
488}
489
490#define module_name(mod) "kernel"
491
492static inline const char *module_address_lookup(unsigned long addr,
493 unsigned long *symbolsize,
494 unsigned long *offset,
495 char **modname,
496 char *namebuf)
497{
498 return NULL;
499}
500
501static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
502{
503 return -ERANGE;
504}
505
506static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
507{
508 return -ERANGE;
509}
510
511static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
512 char *type, char *name,
513 char *module_name, int *exported)
514{
515 return -ERANGE;
516}
517
518static inline unsigned long module_kallsyms_lookup_name(const char *name)
519{
520 return 0;
521}
522
523static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
524 struct module *,
525 unsigned long),
526 void *data)
527{
528 return 0;
529}
530
531static inline int register_module_notifier(struct notifier_block * nb)
532{
533
534 return 0;
535}
536
537static inline int unregister_module_notifier(struct notifier_block * nb)
538{
539 return 0;
540}
541
542#define module_put_and_exit(code) do_exit(code)
543
544static inline void print_modules(void)
545{
546}
547#endif
548
549#ifdef CONFIG_SYSFS
550extern struct kset *module_kset;
551extern struct kobj_type module_ktype;
552extern int module_sysfs_initialized;
553#endif
554
555#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
556
557
558#define __MODULE_STRING(x) __stringify(x)
559
560#ifdef CONFIG_DEBUG_SET_MODULE_RONX
561extern void set_all_modules_text_rw(void);
562extern void set_all_modules_text_ro(void);
563#else
564static inline void set_all_modules_text_rw(void) { }
565static inline void set_all_modules_text_ro(void) { }
566#endif
567
568#ifdef CONFIG_GENERIC_BUG
569void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
570 struct module *);
571void module_bug_cleanup(struct module *);
572
573#else
574
575static inline void module_bug_finalize(const Elf_Ehdr *hdr,
576 const Elf_Shdr *sechdrs,
577 struct module *mod)
578{
579}
580static inline void module_bug_cleanup(struct module *mod) {}
581#endif
582
583#endif