1#ifndef _LINUX_MODULE_H
2#define _LINUX_MODULE_H
3
4
5
6
7
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
20#include <linux/percpu.h>
21#include <asm/module.h>
22
23#include <trace/events/module.h>
24
25
26#define MODULE_SUPPORTED_DEVICE(name)
27
28
29#ifdef CONFIG_SYMBOL_PREFIX
30#define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
31#else
32#define MODULE_SYMBOL_PREFIX ""
33#endif
34
35#define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
36
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
51struct module_attribute {
52 struct attribute attr;
53 ssize_t (*show)(struct module_attribute *, struct module *, char *);
54 ssize_t (*store)(struct module_attribute *, struct module *,
55 const char *, size_t count);
56 void (*setup)(struct module *, const char *);
57 int (*test)(struct module *);
58 void (*free)(struct module *);
59};
60
61struct module_version_attribute {
62 struct module_attribute mattr;
63 const char *module_name;
64 const char *version;
65} __attribute__ ((__aligned__(sizeof(void *))));
66
67struct module_kobject
68{
69 struct kobject kobj;
70 struct module *mod;
71 struct kobject *drivers_dir;
72 struct module_param_attrs *mp;
73};
74
75
76extern int init_module(void);
77extern void cleanup_module(void);
78
79
80struct exception_table_entry;
81
82const struct exception_table_entry *
83search_extable(const struct exception_table_entry *first,
84 const struct exception_table_entry *last,
85 unsigned long value);
86void sort_extable(struct exception_table_entry *start,
87 struct exception_table_entry *finish);
88void sort_main_extable(void);
89void trim_init_extable(struct module *m);
90
91#ifdef MODULE
92#define MODULE_GENERIC_TABLE(gtype,name) \
93extern const struct gtype##_id __mod_##gtype##_table \
94 __attribute__ ((unused, alias(__stringify(name))))
95
96extern struct module __this_module;
97#define THIS_MODULE (&__this_module)
98#else
99#define MODULE_GENERIC_TABLE(gtype,name)
100#define THIS_MODULE ((struct module *)0)
101#endif
102
103
104#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
105
106
107#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
138
139
140
141
142
143#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
144
145
146#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
147
148
149
150#define MODULE_PARM_DESC(_parm, desc) \
151 __MODULE_INFO(parm, _parm, #_parm ":" desc)
152
153#define MODULE_DEVICE_TABLE(type,name) \
154 MODULE_GENERIC_TABLE(type##_device,name)
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171#if defined(MODULE) || !defined(CONFIG_SYSFS)
172#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
173#else
174#define MODULE_VERSION(_version) \
175 extern ssize_t __modver_version_show(struct module_attribute *, \
176 struct module *, char *); \
177 static struct module_version_attribute __modver_version_attr \
178 __used \
179 __attribute__ ((__section__ ("__modver"),aligned(sizeof(void *)))) \
180 = { \
181 .mattr = { \
182 .attr = { \
183 .name = "version", \
184 .mode = S_IRUGO, \
185 }, \
186 .show = __modver_version_show, \
187 }, \
188 .module_name = KBUILD_MODNAME, \
189 .version = _version, \
190 }
191#endif
192
193
194
195
196#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
197
198
199const struct exception_table_entry *search_exception_tables(unsigned long add);
200
201struct notifier_block;
202
203#ifdef CONFIG_MODULES
204
205extern int modules_disabled;
206
207void *__symbol_get(const char *symbol);
208void *__symbol_get_gpl(const char *symbol);
209#define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
210
211
212struct module_use {
213 struct list_head source_list;
214 struct list_head target_list;
215 struct module *source, *target;
216};
217
218#ifndef __GENKSYMS__
219#ifdef CONFIG_MODVERSIONS
220
221
222#define __CRC_SYMBOL(sym, sec) \
223 extern void *__crc_##sym __attribute__((weak)); \
224 static const unsigned long __kcrctab_##sym \
225 __used \
226 __attribute__((section("__kcrctab" sec), unused)) \
227 = (unsigned long) &__crc_##sym;
228#else
229#define __CRC_SYMBOL(sym, sec)
230#endif
231
232
233#define __EXPORT_SYMBOL(sym, sec) \
234 extern typeof(sym) sym; \
235 __CRC_SYMBOL(sym, sec) \
236 static const char __kstrtab_##sym[] \
237 __attribute__((section("__ksymtab_strings"), aligned(1))) \
238 = MODULE_SYMBOL_PREFIX #sym; \
239 static const struct kernel_symbol __ksymtab_##sym \
240 __used \
241 __attribute__((section("__ksymtab" sec), unused)) \
242 = { (unsigned long)&sym, __kstrtab_##sym }
243
244#define EXPORT_SYMBOL(sym) \
245 __EXPORT_SYMBOL(sym, "")
246
247#define EXPORT_SYMBOL_GPL(sym) \
248 __EXPORT_SYMBOL(sym, "_gpl")
249
250#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
251 __EXPORT_SYMBOL(sym, "_gpl_future")
252
253
254#ifdef CONFIG_UNUSED_SYMBOLS
255#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
256#define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
257#else
258#define EXPORT_UNUSED_SYMBOL(sym)
259#define EXPORT_UNUSED_SYMBOL_GPL(sym)
260#endif
261
262#endif
263
264enum module_state
265{
266 MODULE_STATE_LIVE,
267 MODULE_STATE_COMING,
268 MODULE_STATE_GOING,
269};
270
271struct module
272{
273 enum module_state state;
274
275
276 struct list_head list;
277
278
279 char name[MODULE_NAME_LEN];
280
281
282 struct module_kobject mkobj;
283 struct module_attribute *modinfo_attrs;
284 const char *version;
285 const char *srcversion;
286 struct kobject *holders_dir;
287
288
289 const struct kernel_symbol *syms;
290 const unsigned long *crcs;
291 unsigned int num_syms;
292
293
294 struct kernel_param *kp;
295 unsigned int num_kp;
296
297
298 unsigned int num_gpl_syms;
299 const struct kernel_symbol *gpl_syms;
300 const unsigned long *gpl_crcs;
301
302#ifdef CONFIG_UNUSED_SYMBOLS
303
304 const struct kernel_symbol *unused_syms;
305 const unsigned long *unused_crcs;
306 unsigned int num_unused_syms;
307
308
309 unsigned int num_unused_gpl_syms;
310 const struct kernel_symbol *unused_gpl_syms;
311 const unsigned long *unused_gpl_crcs;
312#endif
313
314
315 const struct kernel_symbol *gpl_future_syms;
316 const unsigned long *gpl_future_crcs;
317 unsigned int num_gpl_future_syms;
318
319
320 unsigned int num_exentries;
321 struct exception_table_entry *extable;
322
323
324 int (*init)(void);
325
326
327 void *module_init;
328
329
330 void *module_core;
331
332
333 unsigned int init_size, core_size;
334
335
336 unsigned int init_text_size, core_text_size;
337
338
339 unsigned int init_ro_size, core_ro_size;
340
341
342 struct mod_arch_specific arch;
343
344 unsigned int taints;
345
346#ifdef CONFIG_GENERIC_BUG
347
348 unsigned num_bugs;
349 struct list_head bug_list;
350 struct bug_entry *bug_table;
351#endif
352
353#ifdef CONFIG_KALLSYMS
354
355
356
357
358
359 Elf_Sym *symtab, *core_symtab;
360 unsigned int num_symtab, core_num_syms;
361 char *strtab, *core_strtab;
362
363
364 struct module_sect_attrs *sect_attrs;
365
366
367 struct module_notes_attrs *notes_attrs;
368#endif
369
370#ifdef CONFIG_SMP
371
372 void __percpu *percpu;
373 unsigned int percpu_size;
374#endif
375
376
377
378 char *args;
379#ifdef CONFIG_TRACEPOINTS
380 struct tracepoint * const *tracepoints_ptrs;
381 unsigned int num_tracepoints;
382#endif
383#ifdef HAVE_JUMP_LABEL
384 struct jump_entry *jump_entries;
385 unsigned int num_jump_entries;
386#endif
387#ifdef CONFIG_TRACING
388 const char **trace_bprintk_fmt_start;
389 unsigned int num_trace_bprintk_fmt;
390#endif
391#ifdef CONFIG_EVENT_TRACING
392 struct ftrace_event_call **trace_events;
393 unsigned int num_trace_events;
394#endif
395#ifdef CONFIG_FTRACE_MCOUNT_RECORD
396 unsigned long *ftrace_callsites;
397 unsigned int num_ftrace_callsites;
398#endif
399
400#ifdef CONFIG_MODULE_UNLOAD
401
402 struct list_head source_list;
403
404 struct list_head target_list;
405
406
407 struct task_struct *waiter;
408
409
410 void (*exit)(void);
411
412 struct module_ref {
413 unsigned int incs;
414 unsigned int decs;
415 } __percpu *refptr;
416#endif
417
418#ifdef CONFIG_CONSTRUCTORS
419
420 ctor_fn_t *ctors;
421 unsigned int num_ctors;
422#endif
423};
424#ifndef MODULE_ARCH_INIT
425#define MODULE_ARCH_INIT {}
426#endif
427
428extern struct mutex module_mutex;
429
430
431
432
433static inline int module_is_live(struct module *mod)
434{
435 return mod->state != MODULE_STATE_GOING;
436}
437
438struct module *__module_text_address(unsigned long addr);
439struct module *__module_address(unsigned long addr);
440bool is_module_address(unsigned long addr);
441bool is_module_percpu_address(unsigned long addr);
442bool is_module_text_address(unsigned long addr);
443
444static inline int within_module_core(unsigned long addr, struct module *mod)
445{
446 return (unsigned long)mod->module_core <= addr &&
447 addr < (unsigned long)mod->module_core + mod->core_size;
448}
449
450static inline int within_module_init(unsigned long addr, struct module *mod)
451{
452 return (unsigned long)mod->module_init <= addr &&
453 addr < (unsigned long)mod->module_init + mod->init_size;
454}
455
456
457struct module *find_module(const char *name);
458
459struct symsearch {
460 const struct kernel_symbol *start, *stop;
461 const unsigned long *crcs;
462 enum {
463 NOT_GPL_ONLY,
464 GPL_ONLY,
465 WILL_BE_GPL_ONLY,
466 } licence;
467 bool unused;
468};
469
470
471const struct kernel_symbol *find_symbol(const char *name,
472 struct module **owner,
473 const unsigned long **crc,
474 bool gplok,
475 bool warn);
476
477
478bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
479 unsigned int symnum, void *data), void *data);
480
481
482
483int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
484 char *name, char *module_name, int *exported);
485
486
487unsigned long module_kallsyms_lookup_name(const char *name);
488
489int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
490 struct module *, unsigned long),
491 void *data);
492
493extern void __module_put_and_exit(struct module *mod, long code)
494 __attribute__((noreturn));
495#define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
496
497#ifdef CONFIG_MODULE_UNLOAD
498unsigned int module_refcount(struct module *mod);
499void __symbol_put(const char *symbol);
500#define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
501void symbol_put_addr(void *addr);
502
503
504
505static inline void __module_get(struct module *module)
506{
507 if (module) {
508 preempt_disable();
509 __this_cpu_inc(module->refptr->incs);
510 trace_module_get(module, _THIS_IP_);
511 preempt_enable();
512 }
513}
514
515static inline int try_module_get(struct module *module)
516{
517 int ret = 1;
518
519 if (module) {
520 preempt_disable();
521
522 if (likely(module_is_live(module))) {
523 __this_cpu_inc(module->refptr->incs);
524 trace_module_get(module, _THIS_IP_);
525 } else
526 ret = 0;
527
528 preempt_enable();
529 }
530 return ret;
531}
532
533extern void module_put(struct module *module);
534
535#else
536static inline int try_module_get(struct module *module)
537{
538 return !module || module_is_live(module);
539}
540static inline void module_put(struct module *module)
541{
542}
543static inline void __module_get(struct module *module)
544{
545}
546#define symbol_put(x) do { } while(0)
547#define symbol_put_addr(p) do { } while(0)
548
549#endif
550int ref_module(struct module *a, struct module *b);
551
552
553#define module_name(mod) \
554({ \
555 struct module *__mod = (mod); \
556 __mod ? __mod->name : "kernel"; \
557})
558
559
560
561
562const char *module_address_lookup(unsigned long addr,
563 unsigned long *symbolsize,
564 unsigned long *offset,
565 char **modname,
566 char *namebuf);
567int lookup_module_symbol_name(unsigned long addr, char *symname);
568int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
569
570
571const struct exception_table_entry *search_module_extables(unsigned long addr);
572
573int register_module_notifier(struct notifier_block * nb);
574int unregister_module_notifier(struct notifier_block * nb);
575
576extern void print_modules(void);
577
578extern void module_update_tracepoints(void);
579extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
580
581#else
582#define EXPORT_SYMBOL(sym)
583#define EXPORT_SYMBOL_GPL(sym)
584#define EXPORT_SYMBOL_GPL_FUTURE(sym)
585#define EXPORT_UNUSED_SYMBOL(sym)
586#define EXPORT_UNUSED_SYMBOL_GPL(sym)
587
588
589static inline const struct exception_table_entry *
590search_module_extables(unsigned long addr)
591{
592 return NULL;
593}
594
595static inline struct module *__module_address(unsigned long addr)
596{
597 return NULL;
598}
599
600static inline struct module *__module_text_address(unsigned long addr)
601{
602 return NULL;
603}
604
605static inline bool is_module_address(unsigned long addr)
606{
607 return false;
608}
609
610static inline bool is_module_percpu_address(unsigned long addr)
611{
612 return false;
613}
614
615static inline bool is_module_text_address(unsigned long addr)
616{
617 return false;
618}
619
620
621#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
622#define symbol_put(x) do { } while(0)
623#define symbol_put_addr(x) do { } while(0)
624
625static inline void __module_get(struct module *module)
626{
627}
628
629static inline int try_module_get(struct module *module)
630{
631 return 1;
632}
633
634static inline void module_put(struct module *module)
635{
636}
637
638#define module_name(mod) "kernel"
639
640
641static inline const char *module_address_lookup(unsigned long addr,
642 unsigned long *symbolsize,
643 unsigned long *offset,
644 char **modname,
645 char *namebuf)
646{
647 return NULL;
648}
649
650static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
651{
652 return -ERANGE;
653}
654
655static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
656{
657 return -ERANGE;
658}
659
660static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
661 char *type, char *name,
662 char *module_name, int *exported)
663{
664 return -ERANGE;
665}
666
667static inline unsigned long module_kallsyms_lookup_name(const char *name)
668{
669 return 0;
670}
671
672static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
673 struct module *,
674 unsigned long),
675 void *data)
676{
677 return 0;
678}
679
680static inline int register_module_notifier(struct notifier_block * nb)
681{
682
683 return 0;
684}
685
686static inline int unregister_module_notifier(struct notifier_block * nb)
687{
688 return 0;
689}
690
691#define module_put_and_exit(code) do_exit(code)
692
693static inline void print_modules(void)
694{
695}
696
697static inline void module_update_tracepoints(void)
698{
699}
700
701static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
702{
703 return 0;
704}
705#endif
706
707#ifdef CONFIG_SYSFS
708extern struct kset *module_kset;
709extern struct kobj_type module_ktype;
710extern int module_sysfs_initialized;
711#endif
712
713#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
714
715
716
717#define __MODULE_STRING(x) __stringify(x)
718
719#ifdef CONFIG_DEBUG_SET_MODULE_RONX
720extern void set_all_modules_text_rw(void);
721extern void set_all_modules_text_ro(void);
722#else
723static inline void set_all_modules_text_rw(void) { }
724static inline void set_all_modules_text_ro(void) { }
725#endif
726
727#ifdef CONFIG_GENERIC_BUG
728void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
729 struct module *);
730void module_bug_cleanup(struct module *);
731
732#else
733
734static inline void module_bug_finalize(const Elf_Ehdr *hdr,
735 const Elf_Shdr *sechdrs,
736 struct module *mod)
737{
738}
739static inline void module_bug_cleanup(struct module *mod) {}
740#endif
741
742#endif
743