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