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