Initial commit
[cbs-scheduler.git] / include / linux / module.h
blobe418da12f60a3c8fa04ce1fb9849c7a8f3647bad
1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H
3 /*
4 * Dynamic loading of modules into the kernel.
6 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
7 * Rewritten again by Rusty Russell, 2002
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/marker.h>
19 #include <linux/tracepoint.h>
20 #include <asm/local.h>
22 #include <asm/module.h>
24 /* Not Yet Implemented */
25 #define MODULE_SUPPORTED_DEVICE(name)
27 /* some toolchains uses a `_' prefix for all user symbols */
28 #ifndef MODULE_SYMBOL_PREFIX
29 #define MODULE_SYMBOL_PREFIX ""
30 #endif
32 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
34 struct kernel_symbol
36 unsigned long value;
37 const char *name;
40 struct modversion_info
42 unsigned long crc;
43 char name[MODULE_NAME_LEN];
46 struct module;
48 struct module_attribute {
49 struct attribute attr;
50 ssize_t (*show)(struct module_attribute *, struct module *, char *);
51 ssize_t (*store)(struct module_attribute *, struct module *,
52 const char *, size_t count);
53 void (*setup)(struct module *, const char *);
54 int (*test)(struct module *);
55 void (*free)(struct module *);
58 struct module_kobject
60 struct kobject kobj;
61 struct module *mod;
62 struct kobject *drivers_dir;
63 struct module_param_attrs *mp;
66 /* These are either module local, or the kernel's dummy ones. */
67 extern int init_module(void);
68 extern void cleanup_module(void);
70 /* Archs provide a method of finding the correct exception table. */
71 struct exception_table_entry;
73 const struct exception_table_entry *
74 search_extable(const struct exception_table_entry *first,
75 const struct exception_table_entry *last,
76 unsigned long value);
77 void sort_extable(struct exception_table_entry *start,
78 struct exception_table_entry *finish);
79 void sort_main_extable(void);
82 * Return a pointer to the current module, but only if within a module
84 #ifdef MODULE
85 extern struct module __this_module;
86 #define THIS_MODULE (&__this_module)
87 #else /* !MODULE */
88 #define THIS_MODULE ((struct module *)0)
89 #endif
92 * Declare a module table
93 * - this suppresses "'name' defined but not used" warnings from the compiler
94 * as the table may not actually be used by the code within the module
96 #ifdef MODULE
97 #define MODULE_GENERIC_TABLE(gtype,name) \
98 extern const struct gtype##_id __mod_##gtype##_table \
99 __attribute__ ((unused, alias(__stringify(name))))
100 #define MODULE_STATIC_GENERIC_TABLE(gtype,name) \
101 extern const struct gtype##_id __mod_##gtype##_table \
102 __attribute__ ((unused, alias(__stringify(name))))
103 #else
104 #define MODULE_GENERIC_TABLE(gtype,name)
105 #define MODULE_STATIC_GENERIC_TABLE(gtype,name) \
106 static __typeof__((name)) name __attribute__((unused));
107 #endif
109 /* Generic info of form tag = "info" */
110 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
112 /* For userspace: you can also call me... */
113 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
116 * The following license idents are currently accepted as indicating free
117 * software modules
119 * "GPL" [GNU Public License v2 or later]
120 * "GPL v2" [GNU Public License v2]
121 * "GPL and additional rights" [GNU Public License v2 rights and more]
122 * "Dual BSD/GPL" [GNU Public License v2
123 * or BSD license choice]
124 * "Dual MIT/GPL" [GNU Public License v2
125 * or MIT license choice]
126 * "Dual MPL/GPL" [GNU Public License v2
127 * or Mozilla license choice]
129 * The following other idents are available
131 * "Proprietary" [Non free products]
133 * There are dual licensed components, but when running with Linux it is the
134 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
135 * is a GPL combined work.
137 * This exists for several reasons
138 * 1. So modinfo can show license info for users wanting to vet their setup
139 * is free
140 * 2. So the community can ignore bug reports including proprietary modules
141 * 3. So vendors can do likewise based on their own policies
143 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
145 /* Author, ideally of form NAME[, NAME]*[ and NAME] */
146 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
148 /* What your module does. */
149 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
151 /* One for each parameter, describing how to use it. Some files do
152 multiple of these per line, so can't just use MODULE_INFO. */
153 #define MODULE_PARM_DESC(_parm, desc) \
154 __MODULE_INFO(parm, _parm, #_parm ":" desc)
156 #define MODULE_DEVICE_TABLE(type,name) \
157 MODULE_GENERIC_TABLE(type##_device,name)
158 #define MODULE_STATIC_DEVICE_TABLE(type,name) \
159 MODULE_STATIC_GENERIC_TABLE(type##_device,name)
161 /* Version of form [<epoch>:]<version>[-<extra-version>].
162 Or for CVS/RCS ID version, everything but the number is stripped.
163 <epoch>: A (small) unsigned integer which allows you to start versions
164 anew. If not mentioned, it's zero. eg. "2:1.0" is after
165 "1:2.0".
166 <version>: The <version> may contain only alphanumerics and the
167 character `.'. Ordered by numeric sort for numeric parts,
168 ascii sort for ascii parts (as per RPM or DEB algorithm).
169 <extraversion>: Like <version>, but inserted for local
170 customizations, eg "rh3" or "rusty1".
172 Using this automatically adds a checksum of the .c files and the
173 local headers in "srcversion".
175 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
177 /* Optional firmware file (or files) needed by the module
178 * format is simply firmware file name. Multiple firmware
179 * files require multiple MODULE_FIRMWARE() specifiers */
180 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
182 /* Given an address, look for it in the exception tables */
183 const struct exception_table_entry *search_exception_tables(unsigned long add);
185 struct notifier_block;
187 #ifdef CONFIG_MODULES
189 /* Get/put a kernel symbol (calls must be symmetric) */
190 void *__symbol_get(const char *symbol);
191 void *__symbol_get_gpl(const char *symbol);
192 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
194 #ifndef __GENKSYMS__
195 #ifdef CONFIG_MODVERSIONS
196 /* Mark the CRC weak since genksyms apparently decides not to
197 * generate a checksums for some symbols */
198 #define __CRC_SYMBOL(sym, sec) \
199 extern void *__crc_##sym __attribute__((weak)); \
200 static const unsigned long __kcrctab_##sym \
201 __used \
202 __attribute__((section("__kcrctab" sec), unused)) \
203 = (unsigned long) &__crc_##sym;
204 #else
205 #define __CRC_SYMBOL(sym, sec)
206 #endif
208 /* For every exported symbol, place a struct in the __ksymtab section */
209 #define __EXPORT_SYMBOL(sym, sec) \
210 extern typeof(sym) sym; \
211 __CRC_SYMBOL(sym, sec) \
212 static const char __kstrtab_##sym[] \
213 __attribute__((section("__ksymtab_strings"), aligned(1))) \
214 = MODULE_SYMBOL_PREFIX #sym; \
215 static const struct kernel_symbol __ksymtab_##sym \
216 __used \
217 __attribute__((section("__ksymtab" sec), unused)) \
218 = { (unsigned long)&sym, __kstrtab_##sym }
220 #define EXPORT_SYMBOL(sym) \
221 __EXPORT_SYMBOL(sym, "")
223 #define EXPORT_SYMBOL_GPL(sym) \
224 __EXPORT_SYMBOL(sym, "_gpl")
226 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
227 __EXPORT_SYMBOL(sym, "_gpl_future")
230 #ifdef CONFIG_UNUSED_SYMBOLS
231 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
232 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
233 #else
234 #define EXPORT_UNUSED_SYMBOL(sym)
235 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
236 #endif
238 #endif
240 enum module_state
242 MODULE_STATE_LIVE,
243 MODULE_STATE_COMING,
244 MODULE_STATE_GOING,
247 struct module
249 enum module_state state;
251 /* Member of list of modules */
252 struct list_head list;
254 /* Unique handle for this module */
255 char name[MODULE_NAME_LEN];
257 /* Sysfs stuff. */
258 struct module_kobject mkobj;
259 struct module_attribute *modinfo_attrs;
260 const char *version;
261 const char *srcversion;
262 struct kobject *holders_dir;
264 /* Exported symbols */
265 const struct kernel_symbol *syms;
266 const unsigned long *crcs;
267 unsigned int num_syms;
269 /* GPL-only exported symbols. */
270 unsigned int num_gpl_syms;
271 const struct kernel_symbol *gpl_syms;
272 const unsigned long *gpl_crcs;
274 #ifdef CONFIG_UNUSED_SYMBOLS
275 /* unused exported symbols. */
276 const struct kernel_symbol *unused_syms;
277 const unsigned long *unused_crcs;
278 unsigned int num_unused_syms;
280 /* GPL-only, unused exported symbols. */
281 unsigned int num_unused_gpl_syms;
282 const struct kernel_symbol *unused_gpl_syms;
283 const unsigned long *unused_gpl_crcs;
284 #endif
286 /* symbols that will be GPL-only in the near future. */
287 const struct kernel_symbol *gpl_future_syms;
288 const unsigned long *gpl_future_crcs;
289 unsigned int num_gpl_future_syms;
291 /* Exception table */
292 unsigned int num_exentries;
293 struct exception_table_entry *extable;
295 /* Startup function. */
296 int (*init)(void);
298 /* If this is non-NULL, vfree after init() returns */
299 void *module_init;
301 /* Here is the actual code + data, vfree'd on unload. */
302 void *module_core;
304 /* Here are the sizes of the init and core sections */
305 unsigned int init_size, core_size;
307 /* The size of the executable code in each section. */
308 unsigned int init_text_size, core_text_size;
310 /* Arch-specific module values */
311 struct mod_arch_specific arch;
313 unsigned int taints; /* same bits as kernel:tainted */
315 #ifdef CONFIG_GENERIC_BUG
316 /* Support for BUG */
317 unsigned num_bugs;
318 struct list_head bug_list;
319 struct bug_entry *bug_table;
320 #endif
322 #ifdef CONFIG_KALLSYMS
323 /* We keep the symbol and string tables for kallsyms. */
324 Elf_Sym *symtab;
325 unsigned int num_symtab;
326 char *strtab;
328 /* Section attributes */
329 struct module_sect_attrs *sect_attrs;
331 /* Notes attributes */
332 struct module_notes_attrs *notes_attrs;
333 #endif
335 /* Per-cpu data. */
336 void *percpu;
338 /* The command line arguments (may be mangled). People like
339 keeping pointers to this stuff */
340 char *args;
341 #ifdef CONFIG_MARKERS
342 struct marker *markers;
343 unsigned int num_markers;
344 #endif
345 #ifdef CONFIG_TRACEPOINTS
346 struct tracepoint *tracepoints;
347 unsigned int num_tracepoints;
348 #endif
350 #ifdef CONFIG_TRACING
351 const char **trace_bprintk_fmt_start;
352 unsigned int num_trace_bprintk_fmt;
353 #endif
355 #ifdef CONFIG_MODULE_UNLOAD
356 /* What modules depend on me? */
357 struct list_head modules_which_use_me;
359 /* Who is waiting for us to be unloaded */
360 struct task_struct *waiter;
362 /* Destruction function. */
363 void (*exit)(void);
365 #ifdef CONFIG_SMP
366 char *refptr;
367 #else
368 local_t ref;
369 #endif
370 #endif
372 #ifndef MODULE_ARCH_INIT
373 #define MODULE_ARCH_INIT {}
374 #endif
376 /* FIXME: It'd be nice to isolate modules during init, too, so they
377 aren't used before they (may) fail. But presently too much code
378 (IDE & SCSI) require entry into the module during init.*/
379 static inline int module_is_live(struct module *mod)
381 return mod->state != MODULE_STATE_GOING;
384 /* Is this address in a module? (second is with no locks, for oops) */
385 struct module *module_text_address(unsigned long addr);
386 struct module *__module_text_address(unsigned long addr);
387 int is_module_address(unsigned long addr);
389 static inline int within_module_core(unsigned long addr, struct module *mod)
391 return (unsigned long)mod->module_core <= addr &&
392 addr < (unsigned long)mod->module_core + mod->core_size;
395 static inline int within_module_init(unsigned long addr, struct module *mod)
397 return (unsigned long)mod->module_init <= addr &&
398 addr < (unsigned long)mod->module_init + mod->init_size;
401 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
402 symnum out of range. */
403 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
404 char *name, char *module_name, int *exported);
406 /* Look for this name: can be of form module:name. */
407 unsigned long module_kallsyms_lookup_name(const char *name);
409 extern void __module_put_and_exit(struct module *mod, long code)
410 __attribute__((noreturn));
411 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
413 #ifdef CONFIG_MODULE_UNLOAD
414 unsigned int module_refcount(struct module *mod);
415 void __symbol_put(const char *symbol);
416 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
417 void symbol_put_addr(void *addr);
419 static inline local_t *__module_ref_addr(struct module *mod, int cpu)
421 #ifdef CONFIG_SMP
422 return (local_t *) (mod->refptr + per_cpu_offset(cpu));
423 #else
424 return &mod->ref;
425 #endif
428 /* Sometimes we know we already have a refcount, and it's easier not
429 to handle the error case (which only happens with rmmod --wait). */
430 static inline void __module_get(struct module *module)
432 if (module) {
433 local_inc(__module_ref_addr(module, get_cpu()));
434 put_cpu();
438 static inline int try_module_get(struct module *module)
440 int ret = 1;
442 if (module) {
443 unsigned int cpu = get_cpu();
444 if (likely(module_is_live(module)))
445 local_inc(__module_ref_addr(module, cpu));
446 else
447 ret = 0;
448 put_cpu();
450 return ret;
453 extern void module_put(struct module *module);
455 #else /*!CONFIG_MODULE_UNLOAD*/
456 static inline int try_module_get(struct module *module)
458 return !module || module_is_live(module);
460 static inline void module_put(struct module *module)
463 static inline void __module_get(struct module *module)
466 #define symbol_put(x) do { } while(0)
467 #define symbol_put_addr(p) do { } while(0)
469 #endif /* CONFIG_MODULE_UNLOAD */
471 /* This is a #define so the string doesn't get put in every .o file */
472 #define module_name(mod) \
473 ({ \
474 struct module *__mod = (mod); \
475 __mod ? __mod->name : "kernel"; \
478 /* For kallsyms to ask for address resolution. namebuf should be at
479 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
480 * found, otherwise NULL. */
481 const char *module_address_lookup(unsigned long addr,
482 unsigned long *symbolsize,
483 unsigned long *offset,
484 char **modname,
485 char *namebuf);
486 int lookup_module_symbol_name(unsigned long addr, char *symname);
487 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
489 /* For extable.c to search modules' exception tables. */
490 const struct exception_table_entry *search_module_extables(unsigned long addr);
492 int register_module_notifier(struct notifier_block * nb);
493 int unregister_module_notifier(struct notifier_block * nb);
495 extern void print_modules(void);
497 extern void module_update_markers(void);
499 extern void module_update_tracepoints(void);
500 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
502 #else /* !CONFIG_MODULES... */
503 #define EXPORT_SYMBOL(sym)
504 #define EXPORT_SYMBOL_GPL(sym)
505 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
506 #define EXPORT_UNUSED_SYMBOL(sym)
507 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
509 /* Given an address, look for it in the exception tables. */
510 static inline const struct exception_table_entry *
511 search_module_extables(unsigned long addr)
513 return NULL;
516 /* Is this address in a module? */
517 static inline struct module *module_text_address(unsigned long addr)
519 return NULL;
522 /* Is this address in a module? (don't take a lock, we're oopsing) */
523 static inline struct module *__module_text_address(unsigned long addr)
525 return NULL;
528 static inline int is_module_address(unsigned long addr)
530 return 0;
533 /* Get/put a kernel symbol (calls should be symmetric) */
534 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
535 #define symbol_put(x) do { } while(0)
536 #define symbol_put_addr(x) do { } while(0)
538 static inline void __module_get(struct module *module)
542 static inline int try_module_get(struct module *module)
544 return 1;
547 static inline void module_put(struct module *module)
551 #define module_name(mod) "kernel"
553 /* For kallsyms to ask for address resolution. NULL means not found. */
554 static inline const char *module_address_lookup(unsigned long addr,
555 unsigned long *symbolsize,
556 unsigned long *offset,
557 char **modname,
558 char *namebuf)
560 return NULL;
563 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
565 return -ERANGE;
568 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
570 return -ERANGE;
573 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
574 char *type, char *name,
575 char *module_name, int *exported)
577 return -ERANGE;
580 static inline unsigned long module_kallsyms_lookup_name(const char *name)
582 return 0;
585 static inline int register_module_notifier(struct notifier_block * nb)
587 /* no events will happen anyway, so this can always succeed */
588 return 0;
591 static inline int unregister_module_notifier(struct notifier_block * nb)
593 return 0;
596 #define module_put_and_exit(code) do_exit(code)
598 static inline void print_modules(void)
602 static inline void module_update_markers(void)
606 static inline void module_update_tracepoints(void)
610 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
612 return 0;
615 #endif /* CONFIG_MODULES */
617 struct device_driver;
618 #ifdef CONFIG_SYSFS
619 struct module;
621 extern struct kset *module_kset;
622 extern struct kobj_type module_ktype;
623 extern int module_sysfs_initialized;
625 int mod_sysfs_init(struct module *mod);
626 int mod_sysfs_setup(struct module *mod,
627 struct kernel_param *kparam,
628 unsigned int num_params);
629 int module_add_modinfo_attrs(struct module *mod);
630 void module_remove_modinfo_attrs(struct module *mod);
632 #else /* !CONFIG_SYSFS */
634 static inline int mod_sysfs_init(struct module *mod)
636 return 0;
639 static inline int mod_sysfs_setup(struct module *mod,
640 struct kernel_param *kparam,
641 unsigned int num_params)
643 return 0;
646 static inline int module_add_modinfo_attrs(struct module *mod)
648 return 0;
651 static inline void module_remove_modinfo_attrs(struct module *mod)
654 #endif /* CONFIG_SYSFS */
656 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
658 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
660 #define __MODULE_STRING(x) __stringify(x)
662 #endif /* _LINUX_MODULE_H */