module.h 16 KB

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