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