Subversion Repositories shark

Rev

Rev 422 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
422 giacomo 1
#ifndef _LINUX_MODULE_H
2
#define _LINUX_MODULE_H
3
/*
4
 * Dynamic loading of modules into the kernel.
5
 *
6
 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
7
 * Rewritten again by Rusty Russell, 2002
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 <asm/local.h>
20
 
21
#include <asm/module.h>
22
 
23
/* Not Yet Implemented */
24
#define MODULE_SUPPORTED_DEVICE(name)
25
#define print_modules()
26
 
27
/* v850 toolchain uses a `_' prefix for all user symbols */
28
#ifndef MODULE_SYMBOL_PREFIX
29
#define MODULE_SYMBOL_PREFIX ""
30
#endif
31
 
32
#define MODULE_NAME_LEN (64 - sizeof(unsigned long))
33
 
34
struct kernel_symbol
35
{
36
        unsigned long value;
37
        const char *name;
38
};
39
 
40
struct modversion_info
41
{
42
        unsigned long crc;
43
        char name[MODULE_NAME_LEN];
44
};
45
 
46
/* These are either module local, or the kernel's dummy ones. */
47
extern int init_module(void);
48
extern void cleanup_module(void);
49
 
50
/* Archs provide a method of finding the correct exception table. */
51
struct exception_table_entry;
52
 
53
const struct exception_table_entry *
54
search_extable(const struct exception_table_entry *first,
55
               const struct exception_table_entry *last,
56
               unsigned long value);
57
 
58
#ifdef MODULE
59
#define ___module_cat(a,b) __mod_ ## a ## b
60
#define __module_cat(a,b) ___module_cat(a,b)
61
#define __MODULE_INFO(tag, name, info)                                    \
62
static const char __module_cat(name,__LINE__)[]                           \
63
  __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info
64
 
65
#define MODULE_GENERIC_TABLE(gtype,name)                        \
66
extern const struct gtype##_id __mod_##gtype##_table            \
67
  __attribute__ ((unused, alias(__stringify(name))))
68
 
69
#define THIS_MODULE (&__this_module)
70
 
71
#else  /* !MODULE */
72
 
73
#define MODULE_GENERIC_TABLE(gtype,name)
74
#define __MODULE_INFO(tag, name, info)
75
#define THIS_MODULE ((struct module *)0)
76
#endif
77
 
78
/* Generic info of form tag = "info" */
79
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
80
 
81
/* For userspace: you can also call me... */
82
#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
83
 
84
/*
85
 * The following license idents are currently accepted as indicating free
86
 * software modules
87
 *
88
 *      "GPL"                           [GNU Public License v2 or later]
89
 *      "GPL v2"                        [GNU Public License v2]
90
 *      "GPL and additional rights"     [GNU Public License v2 rights and more]
91
 *      "Dual BSD/GPL"                  [GNU Public License v2
92
 *                                       or BSD license choice]
93
 *      "Dual MPL/GPL"                  [GNU Public License v2
94
 *                                       or Mozilla license choice]
95
 *
96
 * The following other idents are available
97
 *
98
 *      "Proprietary"                   [Non free products]
99
 *
100
 * There are dual licensed components, but when running with Linux it is the
101
 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
102
 * is a GPL combined work.
103
 *
104
 * This exists for several reasons
105
 * 1.   So modinfo can show license info for users wanting to vet their setup
106
 *      is free
107
 * 2.   So the community can ignore bug reports including proprietary modules
108
 * 3.   So vendors can do likewise based on their own policies
109
 */
110
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
111
 
112
/* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
113
#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
114
 
115
/* What your module does. */
116
#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
117
 
118
/* One for each parameter, describing how to use it.  Some files do
119
   multiple of these per line, so can't just use MODULE_INFO. */
120
#define MODULE_PARM_DESC(_parm, desc) \
121
        __MODULE_INFO(parm, _parm, #_parm ":" desc)
122
 
123
#define MODULE_DEVICE_TABLE(type,name)          \
124
  MODULE_GENERIC_TABLE(type##_device,name)
125
 
126
/* Given an address, look for it in the exception tables */
127
const struct exception_table_entry *search_exception_tables(unsigned long add);
128
 
129
struct notifier_block;
130
 
131
#ifdef CONFIG_MODULES
132
 
133
/* Get/put a kernel symbol (calls must be symmetric) */
134
void *__symbol_get(const char *symbol);
135
void *__symbol_get_gpl(const char *symbol);
136
#define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
137
 
138
#ifndef __GENKSYMS__
139
#ifdef CONFIG_MODVERSIONS
140
/* Mark the CRC weak since genksyms apparently decides not to
141
 * generate a checksums for some symbols */
142
#define __CRC_SYMBOL(sym, sec)                                  \
143
        extern void *__crc_##sym __attribute__((weak));         \
144
        static const unsigned long __kcrctab_##sym              \
145
        __attribute__((section("__kcrctab" sec), unused))       \
146
        = (unsigned long) &__crc_##sym;
147
#else
148
#define __CRC_SYMBOL(sym, sec)
149
#endif
150
 
151
/* For every exported symbol, place a struct in the __ksymtab section */
152
#define __EXPORT_SYMBOL(sym, sec)                               \
153
        __CRC_SYMBOL(sym, sec)                                  \
154
        static const char __kstrtab_##sym[]                     \
155
        __attribute__((section("__ksymtab_strings")))           \
156
        = MODULE_SYMBOL_PREFIX #sym;                            \
157
        static const struct kernel_symbol __ksymtab_##sym       \
158
        __attribute__((section("__ksymtab" sec), unused))       \
159
        = { (unsigned long)&sym, __kstrtab_##sym }
160
 
161
#define EXPORT_SYMBOL(sym)                                      \
162
        __EXPORT_SYMBOL(sym, "")
163
 
164
#define EXPORT_SYMBOL_GPL(sym)                                  \
165
        __EXPORT_SYMBOL(sym, "_gpl")
166
 
167
#endif
168
 
169
/* We don't mangle the actual symbol anymore, so no need for
170
 * special casing EXPORT_SYMBOL_NOVERS.  FIXME: Deprecated */
171
#define EXPORT_SYMBOL_NOVERS(sym) EXPORT_SYMBOL(sym)
172
 
173
struct module_ref
174
{
175
        local_t count;
176
} ____cacheline_aligned;
177
 
178
enum module_state
179
{
180
        MODULE_STATE_LIVE,
181
        MODULE_STATE_COMING,
182
        MODULE_STATE_GOING,
183
};
184
 
185
struct module
186
{
187
        enum module_state state;
188
 
189
        /* Member of list of modules */
190
        struct list_head list;
191
 
192
        /* Unique handle for this module */
193
        char name[MODULE_NAME_LEN];
194
 
195
        /* Exported symbols */
196
        const struct kernel_symbol *syms;
197
        unsigned int num_syms;
198
        const unsigned long *crcs;
199
 
200
        /* GPL-only exported symbols. */
201
        const struct kernel_symbol *gpl_syms;
202
        unsigned int num_gpl_syms;
203
        const unsigned long *gpl_crcs;
204
 
205
        /* Exception table */
206
        unsigned int num_exentries;
207
        const struct exception_table_entry *extable;
208
 
209
        /* Startup function. */
210
        int (*init)(void);
211
 
212
        /* If this is non-NULL, vfree after init() returns */
213
        void *module_init;
214
 
215
        /* Here is the actual code + data, vfree'd on unload. */
216
        void *module_core;
217
 
218
        /* Here are the sizes of the init and core sections */
219
        unsigned long init_size, core_size;
220
 
221
        /* The size of the executable code in each section.  */
222
        unsigned long init_text_size, core_text_size;
223
 
224
        /* Arch-specific module values */
225
        struct mod_arch_specific arch;
226
 
227
        /* Am I unsafe to unload? */
228
        int unsafe;
229
 
230
        /* Am I GPL-compatible */
231
        int license_gplok;
232
 
233
#ifdef CONFIG_MODULE_UNLOAD
234
        /* Reference counts */
235
        struct module_ref ref[NR_CPUS];
236
 
237
        /* What modules depend on me? */
238
        struct list_head modules_which_use_me;
239
 
240
        /* Who is waiting for us to be unloaded */
241
        struct task_struct *waiter;
242
 
243
        /* Destruction function. */
244
        void (*exit)(void);
245
#endif
246
 
247
#ifdef CONFIG_KALLSYMS
248
        /* We keep the symbol and string tables for kallsyms. */
249
        Elf_Sym *symtab;
250
        unsigned long num_symtab;
251
        char *strtab;
252
#endif
253
 
254
        /* Per-cpu data. */
255
        void *percpu;
256
 
257
        /* The command line arguments (may be mangled).  People like
258
           keeping pointers to this stuff */
259
        char *args;
260
};
261
 
262
/* FIXME: It'd be nice to isolate modules during init, too, so they
263
   aren't used before they (may) fail.  But presently too much code
264
   (IDE & SCSI) require entry into the module during init.*/
265
static inline int module_is_live(struct module *mod)
266
{
267
        return mod->state != MODULE_STATE_GOING;
268
}
269
 
270
/* Is this address in a module? */
271
struct module *module_text_address(unsigned long addr);
272
 
273
/* Returns module and fills in value, defined and namebuf, or NULL if
274
   symnum out of range. */
275
struct module *module_get_kallsym(unsigned int symnum,
276
                                  unsigned long *value,
277
                                  char *type,
278
                                  char namebuf[128]);
279
int is_exported(const char *name, const struct module *mod);
280
 
281
extern void __module_put_and_exit(struct module *mod, long code)
282
        __attribute__((noreturn));
283
#define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
284
 
285
#ifdef CONFIG_MODULE_UNLOAD
286
unsigned int module_refcount(struct module *mod);
287
void __symbol_put(const char *symbol);
288
#define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
289
void symbol_put_addr(void *addr);
290
 
291
/* Sometimes we know we already have a refcount, and it's easier not
292
   to handle the error case (which only happens with rmmod --wait). */
293
static inline void __module_get(struct module *module)
294
{
295
        if (module) {
296
                BUG_ON(module_refcount(module) == 0);
297
                local_inc(&module->ref[get_cpu()].count);
298
                put_cpu();
299
        }
300
}
301
 
302
static inline int try_module_get(struct module *module)
303
{
304
        int ret = 1;
305
 
306
        if (module) {
307
                unsigned int cpu = get_cpu();
308
                if (likely(module_is_live(module)))
309
                        local_inc(&module->ref[cpu].count);
310
                else
311
                        ret = 0;
312
                put_cpu();
313
        }
314
        return ret;
315
}
316
 
317
static inline void module_put(struct module *module)
318
{
319
        if (module) {
320
                unsigned int cpu = get_cpu();
321
                local_dec(&module->ref[cpu].count);
322
                /* Maybe they're waiting for us to drop reference? */
323
                if (unlikely(!module_is_live(module)))
324
                        wake_up_process(module->waiter);
325
                put_cpu();
326
        }
327
}
328
 
329
#else /*!CONFIG_MODULE_UNLOAD*/
330
static inline int try_module_get(struct module *module)
331
{
332
        return !module || module_is_live(module);
333
}
334
static inline void module_put(struct module *module)
335
{
336
}
337
static inline void __module_get(struct module *module)
338
{
339
}
340
#define symbol_put(x) do { } while(0)
341
#define symbol_put_addr(p) do { } while(0)
342
 
343
#endif /* CONFIG_MODULE_UNLOAD */
344
 
345
/* This is a #define so the string doesn't get put in every .o file */
346
#define module_name(mod)                        \
347
({                                              \
348
        struct module *__mod = (mod);           \
349
        __mod ? __mod->name : "kernel";         \
350
})
351
 
352
#define __unsafe(mod)                                                        \
353
do {                                                                         \
354
        if (mod && !(mod)->unsafe) {                                         \
355
                printk(KERN_WARNING                                          \
356
                       "Module %s cannot be unloaded due to unsafe usage in" \
357
                       " %s:%u\n", (mod)->name, __FILE__, __LINE__);         \
358
                (mod)->unsafe = 1;                                           \
359
        }                                                                    \
360
} while(0)
361
 
362
/* For kallsyms to ask for address resolution.  NULL means not found. */
363
const char *module_address_lookup(unsigned long addr,
364
                                  unsigned long *symbolsize,
365
                                  unsigned long *offset,
366
                                  char **modname);
367
 
368
/* For extable.c to search modules' exception tables. */
369
const struct exception_table_entry *search_module_extables(unsigned long addr);
370
 
371
int register_module_notifier(struct notifier_block * nb);
372
int unregister_module_notifier(struct notifier_block * nb);
373
 
374
#else /* !CONFIG_MODULES... */
375
#define EXPORT_SYMBOL(sym)
376
#define EXPORT_SYMBOL_GPL(sym)
377
#define EXPORT_SYMBOL_NOVERS(sym)
378
 
379
/* Given an address, look for it in the exception tables. */
380
static inline const struct exception_table_entry *
381
search_module_extables(unsigned long addr)
382
{
383
        return NULL;
384
}
385
 
386
/* Is this address in a module? */
387
static inline struct module *module_text_address(unsigned long addr)
388
{
389
        return NULL;
390
}
391
 
392
/* Get/put a kernel symbol (calls should be symmetric) */
393
#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
394
#define symbol_put(x) do { } while(0)
395
#define symbol_put_addr(x) do { } while(0)
396
 
397
static inline void __module_get(struct module *module)
398
{
399
}
400
 
401
static inline int try_module_get(struct module *module)
402
{
403
        return 1;
404
}
405
 
406
static inline void module_put(struct module *module)
407
{
408
}
409
 
410
#define module_name(mod) "kernel"
411
 
412
#define __unsafe(mod)
413
 
414
/* For kallsyms to ask for address resolution.  NULL means not found. */
415
static inline const char *module_address_lookup(unsigned long addr,
416
                                                unsigned long *symbolsize,
417
                                                unsigned long *offset,
418
                                                char **modname)
419
{
420
        return NULL;
421
}
422
 
423
static inline struct module *module_get_kallsym(unsigned int symnum,
424
                                                unsigned long *value,
425
                                                char *type,
426
                                                char namebuf[128])
427
{
428
        return NULL;
429
}
430
 
431
static inline int is_exported(const char *name, const struct module *mod)
432
{
433
        return 0;
434
}
435
 
436
static inline int register_module_notifier(struct notifier_block * nb)
437
{
438
        /* no events will happen anyway, so this can always succeed */
439
        return 0;
440
}
441
 
442
static inline int unregister_module_notifier(struct notifier_block * nb)
443
{
444
        return 0;
445
}
446
 
447
#define module_put_and_exit(code) do_exit(code)
448
 
449
#endif /* CONFIG_MODULES */
450
 
451
#ifdef MODULE
452
extern struct module __this_module;
453
#ifdef KBUILD_MODNAME
454
/* We make the linker do some of the work. */
455
struct module __this_module
456
__attribute__((section(".gnu.linkonce.this_module"))) = {
457
        .name = __stringify(KBUILD_MODNAME),
458
        .init = init_module,
459
#ifdef CONFIG_MODULE_UNLOAD
460
        .exit = cleanup_module,
461
#endif
462
};
463
#endif /* KBUILD_MODNAME */
464
#endif /* MODULE */
465
 
466
#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
467
 
468
/* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
469
 
470
struct obsolete_modparm {
471
        char name[64];
472
        char type[64-sizeof(void *)];
473
        void *addr;
474
};
475
#ifdef MODULE
476
/* DEPRECATED: Do not use. */
477
#define MODULE_PARM(var,type)                                               \
478
struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
479
{ __stringify(var), type };
480
 
481
static inline void __deprecated MOD_INC_USE_COUNT(struct module *module)
482
{
483
        __unsafe(module);
484
 
485
#if defined(CONFIG_MODULE_UNLOAD) && defined(MODULE)
486
        local_inc(&module->ref[get_cpu()].count);
487
        put_cpu();
488
#else
489
        (void)try_module_get(module);
490
#endif
491
}
492
 
493
static inline void __deprecated MOD_DEC_USE_COUNT(struct module *module)
494
{
495
        module_put(module);
496
}
497
 
498
#define MOD_INC_USE_COUNT       MOD_INC_USE_COUNT(THIS_MODULE)
499
#define MOD_DEC_USE_COUNT       MOD_DEC_USE_COUNT(THIS_MODULE)
500
#else
501
#define MODULE_PARM(var,type)
502
#define MOD_INC_USE_COUNT       do { } while (0)
503
#define MOD_DEC_USE_COUNT       do { } while (0)
504
#endif
505
 
506
#define __MODULE_STRING(x) __stringify(x)
507
 
508
/* Use symbol_get and symbol_put instead.  You'll thank me. */
509
#define HAVE_INTER_MODULE
510
extern void inter_module_register(const char *, struct module *, const void *);
511
extern void inter_module_unregister(const char *);
512
extern const void *inter_module_get(const char *);
513
extern const void *inter_module_get_request(const char *, const char *);
514
extern void inter_module_put(const char *);
515
 
516
#endif /* _LINUX_MODULE_H */