module.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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/sched.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/list.h>
  12. #include <linux/stat.h>
  13. #include <linux/compiler.h>
  14. #include <linux/cache.h>
  15. #include <linux/kmod.h>
  16. #include <linux/elf.h>
  17. #include <linux/stringify.h>
  18. #include <linux/kobject.h>
  19. #include <linux/moduleparam.h>
  20. #include <asm/local.h>
  21. #include <asm/module.h>
  22. /* Not Yet Implemented */
  23. #define MODULE_SUPPORTED_DEVICE(name)
  24. /* v850 toolchain uses a `_' prefix for all user symbols */
  25. #ifndef MODULE_SYMBOL_PREFIX
  26. #define MODULE_SYMBOL_PREFIX ""
  27. #endif
  28. #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
  29. struct kernel_symbol
  30. {
  31. unsigned long value;
  32. const char *name;
  33. };
  34. struct modversion_info
  35. {
  36. unsigned long crc;
  37. char name[MODULE_NAME_LEN];
  38. };
  39. struct module;
  40. struct module_attribute {
  41. struct attribute attr;
  42. ssize_t (*show)(struct module_attribute *, struct module *, char *);
  43. ssize_t (*store)(struct module_attribute *, struct module *,
  44. const char *, size_t count);
  45. void (*setup)(struct module *, const char *);
  46. int (*test)(struct module *);
  47. void (*free)(struct module *);
  48. };
  49. struct module_kobject
  50. {
  51. struct kobject kobj;
  52. struct module *mod;
  53. };
  54. /* These are either module local, or the kernel's dummy ones. */
  55. extern int init_module(void);
  56. extern void cleanup_module(void);
  57. /* Archs provide a method of finding the correct exception table. */
  58. struct exception_table_entry;
  59. const struct exception_table_entry *
  60. search_extable(const struct exception_table_entry *first,
  61. const struct exception_table_entry *last,
  62. unsigned long value);
  63. void sort_extable(struct exception_table_entry *start,
  64. struct exception_table_entry *finish);
  65. void sort_main_extable(void);
  66. extern struct subsystem module_subsys;
  67. #ifdef MODULE
  68. #define MODULE_GENERIC_TABLE(gtype,name) \
  69. extern const struct gtype##_id __mod_##gtype##_table \
  70. __attribute__ ((unused, alias(__stringify(name))))
  71. extern struct module __this_module;
  72. #define THIS_MODULE (&__this_module)
  73. #else /* !MODULE */
  74. #define MODULE_GENERIC_TABLE(gtype,name)
  75. #define THIS_MODULE ((struct module *)0)
  76. #endif
  77. /* Generic info of form tag = "info" */
  78. #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
  79. /* For userspace: you can also call me... */
  80. #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
  81. /*
  82. * The following license idents are currently accepted as indicating free
  83. * software modules
  84. *
  85. * "GPL" [GNU Public License v2 or later]
  86. * "GPL v2" [GNU Public License v2]
  87. * "GPL and additional rights" [GNU Public License v2 rights and more]
  88. * "Dual BSD/GPL" [GNU Public License v2
  89. * or BSD license choice]
  90. * "Dual MIT/GPL" [GNU Public License v2
  91. * or MIT license choice]
  92. * "Dual MPL/GPL" [GNU Public License v2
  93. * or Mozilla license choice]
  94. *
  95. * The following other idents are available
  96. *
  97. * "Proprietary" [Non free products]
  98. *
  99. * There are dual licensed components, but when running with Linux it is the
  100. * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
  101. * is a GPL combined work.
  102. *
  103. * This exists for several reasons
  104. * 1. So modinfo can show license info for users wanting to vet their setup
  105. * is free
  106. * 2. So the community can ignore bug reports including proprietary modules
  107. * 3. So vendors can do likewise based on their own policies
  108. */
  109. #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
  110. /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
  111. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  112. /* What your module does. */
  113. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  114. /* One for each parameter, describing how to use it. Some files do
  115. multiple of these per line, so can't just use MODULE_INFO. */
  116. #define MODULE_PARM_DESC(_parm, desc) \
  117. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  118. #define MODULE_DEVICE_TABLE(type,name) \
  119. MODULE_GENERIC_TABLE(type##_device,name)
  120. /* Version of form [<epoch>:]<version>[-<extra-version>].
  121. Or for CVS/RCS ID version, everything but the number is stripped.
  122. <epoch>: A (small) unsigned integer which allows you to start versions
  123. anew. If not mentioned, it's zero. eg. "2:1.0" is after
  124. "1:2.0".
  125. <version>: The <version> may contain only alphanumerics and the
  126. character `.'. Ordered by numeric sort for numeric parts,
  127. ascii sort for ascii parts (as per RPM or DEB algorithm).
  128. <extraversion>: Like <version>, but inserted for local
  129. customizations, eg "rh3" or "rusty1".
  130. Using this automatically adds a checksum of the .c files and the
  131. local headers in "srcversion".
  132. */
  133. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  134. /* Given an address, look for it in the exception tables */
  135. const struct exception_table_entry *search_exception_tables(unsigned long add);
  136. struct notifier_block;
  137. #ifdef CONFIG_MODULES
  138. /* Get/put a kernel symbol (calls must be symmetric) */
  139. void *__symbol_get(const char *symbol);
  140. void *__symbol_get_gpl(const char *symbol);
  141. #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
  142. #ifndef __GENKSYMS__
  143. #ifdef CONFIG_MODVERSIONS
  144. /* Mark the CRC weak since genksyms apparently decides not to
  145. * generate a checksums for some symbols */
  146. #define __CRC_SYMBOL(sym, sec) \
  147. extern void *__crc_##sym __attribute__((weak)); \
  148. static const unsigned long __kcrctab_##sym \
  149. __attribute_used__ \
  150. __attribute__((section("__kcrctab" sec), unused)) \
  151. = (unsigned long) &__crc_##sym;
  152. #else
  153. #define __CRC_SYMBOL(sym, sec)
  154. #endif
  155. /* For every exported symbol, place a struct in the __ksymtab section */
  156. #define __EXPORT_SYMBOL(sym, sec) \
  157. extern typeof(sym) sym; \
  158. __CRC_SYMBOL(sym, sec) \
  159. static const char __kstrtab_##sym[] \
  160. __attribute__((section("__ksymtab_strings"))) \
  161. = MODULE_SYMBOL_PREFIX #sym; \
  162. static const struct kernel_symbol __ksymtab_##sym \
  163. __attribute_used__ \
  164. __attribute__((section("__ksymtab" sec), unused)) \
  165. = { (unsigned long)&sym, __kstrtab_##sym }
  166. #define EXPORT_SYMBOL(sym) \
  167. __EXPORT_SYMBOL(sym, "")
  168. #define EXPORT_SYMBOL_GPL(sym) \
  169. __EXPORT_SYMBOL(sym, "_gpl")
  170. #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
  171. __EXPORT_SYMBOL(sym, "_gpl_future")
  172. #endif
  173. struct module_ref
  174. {
  175. local_t count;
  176. } ____cacheline_aligned;
  177. enum module_state
  178. {
  179. MODULE_STATE_LIVE,
  180. MODULE_STATE_COMING,
  181. MODULE_STATE_GOING,
  182. };
  183. /* Similar stuff for section attributes. */
  184. #define MODULE_SECT_NAME_LEN 32
  185. struct module_sect_attr
  186. {
  187. struct module_attribute mattr;
  188. char name[MODULE_SECT_NAME_LEN];
  189. unsigned long address;
  190. };
  191. struct module_sect_attrs
  192. {
  193. struct attribute_group grp;
  194. struct module_sect_attr attrs[0];
  195. };
  196. struct module_param_attrs;
  197. struct module
  198. {
  199. enum module_state state;
  200. /* Member of list of modules */
  201. struct list_head list;
  202. /* Unique handle for this module */
  203. char name[MODULE_NAME_LEN];
  204. /* Sysfs stuff. */
  205. struct module_kobject mkobj;
  206. struct module_param_attrs *param_attrs;
  207. struct module_attribute *modinfo_attrs;
  208. const char *version;
  209. const char *srcversion;
  210. /* Exported symbols */
  211. const struct kernel_symbol *syms;
  212. unsigned int num_syms;
  213. const unsigned long *crcs;
  214. /* GPL-only exported symbols. */
  215. const struct kernel_symbol *gpl_syms;
  216. unsigned int num_gpl_syms;
  217. const unsigned long *gpl_crcs;
  218. /* symbols that will be GPL-only in the near future. */
  219. const struct kernel_symbol *gpl_future_syms;
  220. unsigned int num_gpl_future_syms;
  221. const unsigned long *gpl_future_crcs;
  222. /* Exception table */
  223. unsigned int num_exentries;
  224. const struct exception_table_entry *extable;
  225. /* Startup function. */
  226. int (*init)(void);
  227. /* If this is non-NULL, vfree after init() returns */
  228. void *module_init;
  229. /* Here is the actual code + data, vfree'd on unload. */
  230. void *module_core;
  231. /* Here are the sizes of the init and core sections */
  232. unsigned long init_size, core_size;
  233. /* The size of the executable code in each section. */
  234. unsigned long init_text_size, core_text_size;
  235. /* The handle returned from unwind_add_table. */
  236. void *unwind_info;
  237. /* Arch-specific module values */
  238. struct mod_arch_specific arch;
  239. /* Am I unsafe to unload? */
  240. int unsafe;
  241. /* Am I GPL-compatible */
  242. int license_gplok;
  243. #ifdef CONFIG_MODULE_UNLOAD
  244. /* Reference counts */
  245. struct module_ref ref[NR_CPUS];
  246. /* What modules depend on me? */
  247. struct list_head modules_which_use_me;
  248. /* Who is waiting for us to be unloaded */
  249. struct task_struct *waiter;
  250. /* Destruction function. */
  251. void (*exit)(void);
  252. #endif
  253. #ifdef CONFIG_KALLSYMS
  254. /* We keep the symbol and string tables for kallsyms. */
  255. Elf_Sym *symtab;
  256. unsigned long num_symtab;
  257. char *strtab;
  258. /* Section attributes */
  259. struct module_sect_attrs *sect_attrs;
  260. #endif
  261. /* Per-cpu data. */
  262. void *percpu;
  263. /* The command line arguments (may be mangled). People like
  264. keeping pointers to this stuff */
  265. char *args;
  266. };
  267. /* FIXME: It'd be nice to isolate modules during init, too, so they
  268. aren't used before they (may) fail. But presently too much code
  269. (IDE & SCSI) require entry into the module during init.*/
  270. static inline int module_is_live(struct module *mod)
  271. {
  272. return mod->state != MODULE_STATE_GOING;
  273. }
  274. /* Is this address in a module? (second is with no locks, for oops) */
  275. struct module *module_text_address(unsigned long addr);
  276. struct module *__module_text_address(unsigned long addr);
  277. /* Returns module and fills in value, defined and namebuf, or NULL if
  278. symnum out of range. */
  279. struct module *module_get_kallsym(unsigned int symnum,
  280. unsigned long *value,
  281. char *type,
  282. char namebuf[128]);
  283. /* Look for this name: can be of form module:name. */
  284. unsigned long module_kallsyms_lookup_name(const char *name);
  285. int is_exported(const char *name, const struct module *mod);
  286. extern void __module_put_and_exit(struct module *mod, long code)
  287. __attribute__((noreturn));
  288. #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
  289. #ifdef CONFIG_MODULE_UNLOAD
  290. unsigned int module_refcount(struct module *mod);
  291. void __symbol_put(const char *symbol);
  292. #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
  293. void symbol_put_addr(void *addr);
  294. /* Sometimes we know we already have a refcount, and it's easier not
  295. to handle the error case (which only happens with rmmod --wait). */
  296. static inline void __module_get(struct module *module)
  297. {
  298. if (module) {
  299. BUG_ON(module_refcount(module) == 0);
  300. local_inc(&module->ref[get_cpu()].count);
  301. put_cpu();
  302. }
  303. }
  304. static inline int try_module_get(struct module *module)
  305. {
  306. int ret = 1;
  307. if (module) {
  308. unsigned int cpu = get_cpu();
  309. if (likely(module_is_live(module)))
  310. local_inc(&module->ref[cpu].count);
  311. else
  312. ret = 0;
  313. put_cpu();
  314. }
  315. return ret;
  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. #else /*!CONFIG_MODULE_UNLOAD*/
  329. static inline int try_module_get(struct module *module)
  330. {
  331. return !module || module_is_live(module);
  332. }
  333. static inline void module_put(struct module *module)
  334. {
  335. }
  336. static inline void __module_get(struct module *module)
  337. {
  338. }
  339. #define symbol_put(x) do { } while(0)
  340. #define symbol_put_addr(p) do { } while(0)
  341. #endif /* CONFIG_MODULE_UNLOAD */
  342. /* This is a #define so the string doesn't get put in every .o file */
  343. #define module_name(mod) \
  344. ({ \
  345. struct module *__mod = (mod); \
  346. __mod ? __mod->name : "kernel"; \
  347. })
  348. #define __unsafe(mod) \
  349. do { \
  350. if (mod && !(mod)->unsafe) { \
  351. printk(KERN_WARNING \
  352. "Module %s cannot be unloaded due to unsafe usage in" \
  353. " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
  354. (mod)->unsafe = 1; \
  355. } \
  356. } while(0)
  357. /* For kallsyms to ask for address resolution. NULL means not found. */
  358. const char *module_address_lookup(unsigned long addr,
  359. unsigned long *symbolsize,
  360. unsigned long *offset,
  361. char **modname);
  362. /* For extable.c to search modules' exception tables. */
  363. const struct exception_table_entry *search_module_extables(unsigned long addr);
  364. int register_module_notifier(struct notifier_block * nb);
  365. int unregister_module_notifier(struct notifier_block * nb);
  366. extern void print_modules(void);
  367. struct device_driver;
  368. void module_add_driver(struct module *, struct device_driver *);
  369. void module_remove_driver(struct device_driver *);
  370. #else /* !CONFIG_MODULES... */
  371. #define EXPORT_SYMBOL(sym)
  372. #define EXPORT_SYMBOL_GPL(sym)
  373. #define EXPORT_SYMBOL_GPL_FUTURE(sym)
  374. /* Given an address, look for it in the exception tables. */
  375. static inline const struct exception_table_entry *
  376. search_module_extables(unsigned long addr)
  377. {
  378. return NULL;
  379. }
  380. /* Is this address in a module? */
  381. static inline struct module *module_text_address(unsigned long addr)
  382. {
  383. return NULL;
  384. }
  385. /* Is this address in a module? (don't take a lock, we're oopsing) */
  386. static inline struct module *__module_text_address(unsigned long addr)
  387. {
  388. return NULL;
  389. }
  390. /* Get/put a kernel symbol (calls should be symmetric) */
  391. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
  392. #define symbol_put(x) do { } while(0)
  393. #define symbol_put_addr(x) do { } while(0)
  394. static inline void __module_get(struct module *module)
  395. {
  396. }
  397. static inline int try_module_get(struct module *module)
  398. {
  399. return 1;
  400. }
  401. static inline void module_put(struct module *module)
  402. {
  403. }
  404. #define module_name(mod) "kernel"
  405. #define __unsafe(mod)
  406. /* For kallsyms to ask for address resolution. NULL means not found. */
  407. static inline const char *module_address_lookup(unsigned long addr,
  408. unsigned long *symbolsize,
  409. unsigned long *offset,
  410. char **modname)
  411. {
  412. return NULL;
  413. }
  414. static inline struct module *module_get_kallsym(unsigned int symnum,
  415. unsigned long *value,
  416. char *type,
  417. char namebuf[128])
  418. {
  419. return NULL;
  420. }
  421. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  422. {
  423. return 0;
  424. }
  425. static inline int is_exported(const char *name, const struct module *mod)
  426. {
  427. return 0;
  428. }
  429. static inline int register_module_notifier(struct notifier_block * nb)
  430. {
  431. /* no events will happen anyway, so this can always succeed */
  432. return 0;
  433. }
  434. static inline int unregister_module_notifier(struct notifier_block * nb)
  435. {
  436. return 0;
  437. }
  438. #define module_put_and_exit(code) do_exit(code)
  439. static inline void print_modules(void)
  440. {
  441. }
  442. struct device_driver;
  443. struct module;
  444. static inline void module_add_driver(struct module *module, struct device_driver *driver)
  445. {
  446. }
  447. static inline void module_remove_driver(struct device_driver *driver)
  448. {
  449. }
  450. #endif /* CONFIG_MODULES */
  451. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  452. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  453. #define __MODULE_STRING(x) __stringify(x)
  454. #endif /* _LINUX_MODULE_H */