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