1#ifndef _LINUX_MODULE_H
2#define _LINUX_MODULE_H
3
4
5
6
7
8
9#include <linux/config.h>
10#include <linux/sched.h>
11#include <linux/spinlock.h>
12#include <linux/list.h>
13#include <linux/stat.h>
14#include <linux/compiler.h>
15#include <linux/cache.h>
16#include <linux/kmod.h>
17#include <linux/elf.h>
18#include <linux/stringify.h>
19#include <linux/kobject.h>
20#include <linux/moduleparam.h>
21#include <asm/local.h>
22
23#include <asm/module.h>
24
25
26#define MODULE_SUPPORTED_DEVICE(name)
27
28
29#ifndef MODULE_SYMBOL_PREFIX
30#define MODULE_SYMBOL_PREFIX ""
31#endif
32
33#define MODULE_NAME_LEN (64 - sizeof(unsigned long))
34
35struct kernel_symbol
36{
37 unsigned long value;
38 const char *name;
39};
40
41struct modversion_info
42{
43 unsigned long crc;
44 char name[MODULE_NAME_LEN];
45};
46
47struct module;
48
49struct module_attribute {
50 struct attribute attr;
51 ssize_t (*show)(struct module_attribute *, struct module *, char *);
52 ssize_t (*store)(struct module_attribute *, struct module *,
53 const char *, size_t count);
54};
55
56struct module_kobject
57{
58 struct kobject kobj;
59 struct module *mod;
60};
61
62
63extern int init_module(void);
64extern void cleanup_module(void);
65
66
67struct exception_table_entry;
68
69const struct exception_table_entry *
70search_extable(const struct exception_table_entry *first,
71 const struct exception_table_entry *last,
72 unsigned long value);
73void sort_extable(struct exception_table_entry *start,
74 struct exception_table_entry *finish);
75void sort_main_extable(void);
76
77extern struct subsystem module_subsys;
78
79#ifdef MODULE
80#define ___module_cat(a,b) __mod_ ## a ## b
81#define __module_cat(a,b) ___module_cat(a,b)
82#define __MODULE_INFO(tag, name, info) \
83static const char __module_cat(name,__LINE__)[] \
84 __attribute_used__ \
85 __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info
86
87#define MODULE_GENERIC_TABLE(gtype,name) \
88extern const struct gtype##_id __mod_##gtype##_table \
89 __attribute__ ((unused, alias(__stringify(name))))
90
91extern struct module __this_module;
92#define THIS_MODULE (&__this_module)
93
94#else
95
96#define MODULE_GENERIC_TABLE(gtype,name)
97#define __MODULE_INFO(tag, name, info)
98#define THIS_MODULE ((struct module *)0)
99#endif
100
101
102#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
103
104
105#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
106
107
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#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
134
135
136#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
137
138
139#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
140
141
142
143#define MODULE_PARM_DESC(_parm, desc) \
144 __MODULE_INFO(parm, _parm, #_parm ":" desc)
145
146#define MODULE_DEVICE_TABLE(type,name) \
147 MODULE_GENERIC_TABLE(type##_device,name)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
164
165
166const struct exception_table_entry *search_exception_tables(unsigned long add);
167
168struct notifier_block;
169
170#ifdef CONFIG_MODULES
171
172
173void *__symbol_get(const char *symbol);
174void *__symbol_get_gpl(const char *symbol);
175#define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
176
177#ifndef __GENKSYMS__
178#ifdef CONFIG_MODVERSIONS
179
180
181#define __CRC_SYMBOL(sym, sec) \
182 extern void *__crc_##sym __attribute__((weak)); \
183 static const unsigned long __kcrctab_##sym \
184 __attribute_used__ \
185 __attribute__((section("__kcrctab" sec), unused)) \
186 = (unsigned long) &__crc_##sym;
187#else
188#define __CRC_SYMBOL(sym, sec)
189#endif
190
191
192#define __EXPORT_SYMBOL(sym, sec) \
193 __CRC_SYMBOL(sym, sec) \
194 static const char __kstrtab_##sym[] \
195 __attribute__((section("__ksymtab_strings"))) \
196 = MODULE_SYMBOL_PREFIX #sym; \
197 static const struct kernel_symbol __ksymtab_##sym \
198 __attribute_used__ \
199 __attribute__((section("__ksymtab" sec), unused)) \
200 = { (unsigned long)&sym, __kstrtab_##sym }
201
202#define EXPORT_SYMBOL(sym) \
203 __EXPORT_SYMBOL(sym, "")
204
205#define EXPORT_SYMBOL_GPL(sym) \
206 __EXPORT_SYMBOL(sym, "_gpl")
207
208#endif
209
210struct module_ref
211{
212 local_t count;
213} ____cacheline_aligned;
214
215enum module_state
216{
217 MODULE_STATE_LIVE,
218 MODULE_STATE_COMING,
219 MODULE_STATE_GOING,
220};
221
222
223#define MODULE_SECT_NAME_LEN 32
224struct module_sect_attr
225{
226 struct module_attribute mattr;
227 char name[MODULE_SECT_NAME_LEN];
228 unsigned long address;
229};
230
231struct module_sect_attrs
232{
233 struct attribute_group grp;
234 struct module_sect_attr attrs[0];
235};
236
237struct module_param_attrs;
238
239struct module
240{
241 enum module_state state;
242
243
244 struct list_head list;
245
246
247 char name[MODULE_NAME_LEN];
248
249
250 struct module_kobject mkobj;
251 struct module_param_attrs *param_attrs;
252
253
254 const struct kernel_symbol *syms;
255 unsigned int num_syms;
256 const unsigned long *crcs;
257
258
259 const struct kernel_symbol *gpl_syms;
260 unsigned int num_gpl_syms;
261 const unsigned long *gpl_crcs;
262
263
264 unsigned int num_exentries;
265 const struct exception_table_entry *extable;
266
267
268 int (*init)(void);
269
270
271 void *module_init;
272
273
274 void *module_core;
275
276
277 unsigned long init_size, core_size;
278
279
280 unsigned long init_text_size, core_text_size;
281
282
283 struct mod_arch_specific arch;
284
285
286 int unsafe;
287
288
289 int license_gplok;
290
291#ifdef CONFIG_MODULE_UNLOAD
292
293 struct module_ref ref[NR_CPUS];
294
295
296 struct list_head modules_which_use_me;
297
298
299 struct task_struct *waiter;
300
301
302 void (*exit)(void);
303#endif
304
305#ifdef CONFIG_KALLSYMS
306
307 Elf_Sym *symtab;
308 unsigned long num_symtab;
309 char *strtab;
310
311
312 struct module_sect_attrs *sect_attrs;
313#endif
314
315
316 void *percpu;
317
318
319
320 char *args;
321};
322
323
324
325
326static inline int module_is_live(struct module *mod)
327{
328 return mod->state != MODULE_STATE_GOING;
329}
330
331
332struct module *module_text_address(unsigned long addr);
333struct module *__module_text_address(unsigned long addr);
334
335
336
337struct module *module_get_kallsym(unsigned int symnum,
338 unsigned long *value,
339 char *type,
340 char namebuf[128]);
341
342
343unsigned long module_kallsyms_lookup_name(const char *name);
344
345int is_exported(const char *name, const struct module *mod);
346
347extern void __module_put_and_exit(struct module *mod, long code)
348 __attribute__((noreturn));
349#define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
350
351#ifdef CONFIG_MODULE_UNLOAD
352unsigned int module_refcount(struct module *mod);
353void __symbol_put(const char *symbol);
354#define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
355void symbol_put_addr(void *addr);
356
357
358
359static inline void __module_get(struct module *module)
360{
361 if (module) {
362 BUG_ON(module_refcount(module) == 0);
363 local_inc(&module->ref[get_cpu()].count);
364 put_cpu();
365 }
366}
367
368static inline int try_module_get(struct module *module)
369{
370 int ret = 1;
371
372 if (module) {
373 unsigned int cpu = get_cpu();
374 if (likely(module_is_live(module)))
375 local_inc(&module->ref[cpu].count);
376 else
377 ret = 0;
378 put_cpu();
379 }
380 return ret;
381}
382
383static inline void module_put(struct module *module)
384{
385 if (module) {
386 unsigned int cpu = get_cpu();
387 local_dec(&module->ref[cpu].count);
388
389 if (unlikely(!module_is_live(module)))
390 wake_up_process(module->waiter);
391 put_cpu();
392 }
393}
394
395#else
396static inline int try_module_get(struct module *module)
397{
398 return !module || module_is_live(module);
399}
400static inline void module_put(struct module *module)
401{
402}
403static inline void __module_get(struct module *module)
404{
405}
406#define symbol_put(x) do { } while(0)
407#define symbol_put_addr(p) do { } while(0)
408
409#endif
410
411
412#define module_name(mod) \
413({ \
414 struct module *__mod = (mod); \
415 __mod ? __mod->name : "kernel"; \
416})
417
418#define __unsafe(mod) \
419do { \
420 if (mod && !(mod)->unsafe) { \
421 printk(KERN_WARNING \
422 "Module %s cannot be unloaded due to unsafe usage in" \
423 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
424 (mod)->unsafe = 1; \
425 } \
426} while(0)
427
428
429const char *module_address_lookup(unsigned long addr,
430 unsigned long *symbolsize,
431 unsigned long *offset,
432 char **modname);
433
434
435const struct exception_table_entry *search_module_extables(unsigned long addr);
436
437int register_module_notifier(struct notifier_block * nb);
438int unregister_module_notifier(struct notifier_block * nb);
439
440extern void print_modules(void);
441
442struct device_driver;
443void module_add_driver(struct module *, struct device_driver *);
444void module_remove_driver(struct device_driver *);
445
446#else
447#define EXPORT_SYMBOL(sym)
448#define EXPORT_SYMBOL_GPL(sym)
449
450
451static inline const struct exception_table_entry *
452search_module_extables(unsigned long addr)
453{
454 return NULL;
455}
456
457
458static inline struct module *module_text_address(unsigned long addr)
459{
460 return NULL;
461}
462
463
464static inline struct module *__module_text_address(unsigned long addr)
465{
466 return NULL;
467}
468
469
470#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
471#define symbol_put(x) do { } while(0)
472#define symbol_put_addr(x) do { } while(0)
473
474static inline void __module_get(struct module *module)
475{
476}
477
478static inline int try_module_get(struct module *module)
479{
480 return 1;
481}
482
483static inline void module_put(struct module *module)
484{
485}
486
487#define module_name(mod) "kernel"
488
489#define __unsafe(mod)
490
491
492static inline const char *module_address_lookup(unsigned long addr,
493 unsigned long *symbolsize,
494 unsigned long *offset,
495 char **modname)
496{
497 return NULL;
498}
499
500static inline struct module *module_get_kallsym(unsigned int symnum,
501 unsigned long *value,
502 char *type,
503 char namebuf[128])
504{
505 return NULL;
506}
507
508static inline unsigned long module_kallsyms_lookup_name(const char *name)
509{
510 return 0;
511}
512
513static inline int is_exported(const char *name, const struct module *mod)
514{
515 return 0;
516}
517
518static inline int register_module_notifier(struct notifier_block * nb)
519{
520
521 return 0;
522}
523
524static inline int unregister_module_notifier(struct notifier_block * nb)
525{
526 return 0;
527}
528
529#define module_put_and_exit(code) do_exit(code)
530
531static inline void print_modules(void)
532{
533}
534
535struct device_driver;
536struct module;
537
538static inline void module_add_driver(struct module *module, struct device_driver *driver)
539{
540}
541
542static inline void module_remove_driver(struct device_driver *driver)
543{
544}
545
546#endif
547
548#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
549
550
551
552struct obsolete_modparm {
553 char name[64];
554 char type[64-sizeof(void *)];
555 void *addr;
556};
557
558static inline void MODULE_PARM_(void) { }
559#ifdef MODULE
560
561#define MODULE_PARM(var,type) \
562struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
563{ __stringify(var), type, &MODULE_PARM_ };
564#else
565#define MODULE_PARM(var,type) static void __attribute__((__unused__)) *__parm_##var = &MODULE_PARM_;
566#endif
567
568#define __MODULE_STRING(x) __stringify(x)
569
570
571#define HAVE_INTER_MODULE
572extern void __deprecated inter_module_register(const char *,
573 struct module *, const void *);
574extern void __deprecated inter_module_unregister(const char *);
575extern const void * __deprecated inter_module_get(const char *);
576extern const void * __deprecated inter_module_get_request(const char *,
577 const char *);
578extern void __deprecated inter_module_put(const char *);
579
580#endif
581