module.h 16 KB

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