module.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <linux/marker.h>
  19. #include <linux/tracepoint.h>
  20. #include <asm/local.h>
  21. #include <asm/module.h>
  22. /* Not Yet Implemented */
  23. #define MODULE_SUPPORTED_DEVICE(name)
  24. /* some toolchains uses a `_' prefix for all user symbols */
  25. #ifndef MODULE_SYMBOL_PREFIX
  26. #define MODULE_SYMBOL_PREFIX ""
  27. #endif
  28. #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
  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. struct kobject *drivers_dir;
  54. struct module_param_attrs *mp;
  55. };
  56. /* These are either module local, or the kernel's dummy ones. */
  57. extern int init_module(void);
  58. extern void cleanup_module(void);
  59. /* Archs provide a method of finding the correct exception table. */
  60. struct exception_table_entry;
  61. const struct exception_table_entry *
  62. search_extable(const struct exception_table_entry *first,
  63. const struct exception_table_entry *last,
  64. unsigned long value);
  65. void sort_extable(struct exception_table_entry *start,
  66. struct exception_table_entry *finish);
  67. void sort_main_extable(void);
  68. #ifdef MODULE
  69. #define MODULE_GENERIC_TABLE(gtype,name) \
  70. extern const struct gtype##_id __mod_##gtype##_table \
  71. __attribute__ ((unused, alias(__stringify(name))))
  72. extern struct module __this_module;
  73. #define THIS_MODULE (&__this_module)
  74. #else /* !MODULE */
  75. #define MODULE_GENERIC_TABLE(gtype,name)
  76. #define THIS_MODULE ((struct module *)0)
  77. #endif
  78. /* Generic info of form tag = "info" */
  79. #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
  80. /* For userspace: you can also call me... */
  81. #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
  82. /*
  83. * The following license idents are currently accepted as indicating free
  84. * software modules
  85. *
  86. * "GPL" [GNU Public License v2 or later]
  87. * "GPL v2" [GNU Public License v2]
  88. * "GPL and additional rights" [GNU Public License v2 rights and more]
  89. * "Dual BSD/GPL" [GNU Public License v2
  90. * or BSD license choice]
  91. * "Dual MIT/GPL" [GNU Public License v2
  92. * or MIT 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. /* Author, ideally of form NAME[, NAME]*[ and NAME] */
  112. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  113. /* What your module does. */
  114. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  115. /* One for each parameter, describing how to use it. Some files do
  116. multiple of these per line, so can't just use MODULE_INFO. */
  117. #define MODULE_PARM_DESC(_parm, desc) \
  118. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  119. #define MODULE_DEVICE_TABLE(type,name) \
  120. MODULE_GENERIC_TABLE(type##_device,name)
  121. /* Version of form [<epoch>:]<version>[-<extra-version>].
  122. Or for CVS/RCS ID version, everything but the number is stripped.
  123. <epoch>: A (small) unsigned integer which allows you to start versions
  124. anew. If not mentioned, it's zero. eg. "2:1.0" is after
  125. "1:2.0".
  126. <version>: The <version> may contain only alphanumerics and the
  127. character `.'. Ordered by numeric sort for numeric parts,
  128. ascii sort for ascii parts (as per RPM or DEB algorithm).
  129. <extraversion>: Like <version>, but inserted for local
  130. customizations, eg "rh3" or "rusty1".
  131. Using this automatically adds a checksum of the .c files and the
  132. local headers in "srcversion".
  133. */
  134. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  135. /* Optional firmware file (or files) needed by the module
  136. * format is simply firmware file name. Multiple firmware
  137. * files require multiple MODULE_FIRMWARE() specifiers */
  138. #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  139. /* Given an address, look for it in the exception tables */
  140. const struct exception_table_entry *search_exception_tables(unsigned long add);
  141. struct notifier_block;
  142. #ifdef CONFIG_MODULES
  143. /* Get/put a kernel symbol (calls must be symmetric) */
  144. void *__symbol_get(const char *symbol);
  145. void *__symbol_get_gpl(const char *symbol);
  146. #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
  147. #ifndef __GENKSYMS__
  148. #ifdef CONFIG_MODVERSIONS
  149. /* Mark the CRC weak since genksyms apparently decides not to
  150. * generate a checksums for some symbols */
  151. #define __CRC_SYMBOL(sym, sec) \
  152. extern void *__crc_##sym __attribute__((weak)); \
  153. static const unsigned long __kcrctab_##sym \
  154. __used \
  155. __attribute__((section("__kcrctab" sec), unused)) \
  156. = (unsigned long) &__crc_##sym;
  157. #else
  158. #define __CRC_SYMBOL(sym, sec)
  159. #endif
  160. /* For every exported symbol, place a struct in the __ksymtab section */
  161. #define __EXPORT_SYMBOL(sym, sec) \
  162. extern typeof(sym) sym; \
  163. __CRC_SYMBOL(sym, sec) \
  164. static const char __kstrtab_##sym[] \
  165. __attribute__((section("__ksymtab_strings"), aligned(1))) \
  166. = MODULE_SYMBOL_PREFIX #sym; \
  167. static const struct kernel_symbol __ksymtab_##sym \
  168. __used \
  169. __attribute__((section("__ksymtab" sec), unused)) \
  170. = { (unsigned long)&sym, __kstrtab_##sym }
  171. #define EXPORT_SYMBOL(sym) \
  172. __EXPORT_SYMBOL(sym, "")
  173. #define EXPORT_SYMBOL_GPL(sym) \
  174. __EXPORT_SYMBOL(sym, "_gpl")
  175. #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
  176. __EXPORT_SYMBOL(sym, "_gpl_future")
  177. #ifdef CONFIG_UNUSED_SYMBOLS
  178. #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
  179. #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
  180. #else
  181. #define EXPORT_UNUSED_SYMBOL(sym)
  182. #define EXPORT_UNUSED_SYMBOL_GPL(sym)
  183. #endif
  184. #endif
  185. enum module_state
  186. {
  187. MODULE_STATE_LIVE,
  188. MODULE_STATE_COMING,
  189. MODULE_STATE_GOING,
  190. };
  191. struct module
  192. {
  193. enum module_state state;
  194. /* Member of list of modules */
  195. struct list_head list;
  196. /* Unique handle for this module */
  197. char name[MODULE_NAME_LEN];
  198. /* Sysfs stuff. */
  199. struct module_kobject mkobj;
  200. struct module_attribute *modinfo_attrs;
  201. const char *version;
  202. const char *srcversion;
  203. struct kobject *holders_dir;
  204. /* Exported symbols */
  205. const struct kernel_symbol *syms;
  206. const unsigned long *crcs;
  207. unsigned int num_syms;
  208. /* Kernel parameters. */
  209. struct kernel_param *kp;
  210. unsigned int num_kp;
  211. /* GPL-only exported symbols. */
  212. unsigned int num_gpl_syms;
  213. const struct kernel_symbol *gpl_syms;
  214. const unsigned long *gpl_crcs;
  215. #ifdef CONFIG_UNUSED_SYMBOLS
  216. /* unused exported symbols. */
  217. const struct kernel_symbol *unused_syms;
  218. const unsigned long *unused_crcs;
  219. unsigned int num_unused_syms;
  220. /* GPL-only, unused exported symbols. */
  221. unsigned int num_unused_gpl_syms;
  222. const struct kernel_symbol *unused_gpl_syms;
  223. const unsigned long *unused_gpl_crcs;
  224. #endif
  225. /* symbols that will be GPL-only in the near future. */
  226. const struct kernel_symbol *gpl_future_syms;
  227. const unsigned long *gpl_future_crcs;
  228. unsigned int num_gpl_future_syms;
  229. /* Exception table */
  230. unsigned int num_exentries;
  231. struct exception_table_entry *extable;
  232. /* Startup function. */
  233. int (*init)(void);
  234. /* If this is non-NULL, vfree after init() returns */
  235. void *module_init;
  236. /* Here is the actual code + data, vfree'd on unload. */
  237. void *module_core;
  238. /* Here are the sizes of the init and core sections */
  239. unsigned int init_size, core_size;
  240. /* The size of the executable code in each section. */
  241. unsigned int init_text_size, core_text_size;
  242. /* Arch-specific module values */
  243. struct mod_arch_specific arch;
  244. unsigned int taints; /* same bits as kernel:tainted */
  245. #ifdef CONFIG_GENERIC_BUG
  246. /* Support for BUG */
  247. unsigned num_bugs;
  248. struct list_head bug_list;
  249. struct bug_entry *bug_table;
  250. #endif
  251. #ifdef CONFIG_KALLSYMS
  252. /* We keep the symbol and string tables for kallsyms. */
  253. Elf_Sym *symtab;
  254. unsigned int num_symtab;
  255. char *strtab;
  256. /* Section attributes */
  257. struct module_sect_attrs *sect_attrs;
  258. /* Notes attributes */
  259. struct module_notes_attrs *notes_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. #ifdef CONFIG_MARKERS
  267. struct marker *markers;
  268. unsigned int num_markers;
  269. #endif
  270. #ifdef CONFIG_TRACEPOINTS
  271. struct tracepoint *tracepoints;
  272. unsigned int num_tracepoints;
  273. #endif
  274. #ifdef CONFIG_MODULE_UNLOAD
  275. /* What modules depend on me? */
  276. struct list_head modules_which_use_me;
  277. /* Who is waiting for us to be unloaded */
  278. struct task_struct *waiter;
  279. /* Destruction function. */
  280. void (*exit)(void);
  281. #ifdef CONFIG_SMP
  282. char *refptr;
  283. #else
  284. local_t ref;
  285. #endif
  286. #endif
  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. struct module *__module_address(unsigned long addr);
  302. bool is_module_address(unsigned long addr);
  303. bool is_module_text_address(unsigned long addr);
  304. static inline int within_module_core(unsigned long addr, struct module *mod)
  305. {
  306. return (unsigned long)mod->module_core <= addr &&
  307. addr < (unsigned long)mod->module_core + mod->core_size;
  308. }
  309. static inline int within_module_init(unsigned long addr, struct module *mod)
  310. {
  311. return (unsigned long)mod->module_init <= addr &&
  312. addr < (unsigned long)mod->module_init + mod->init_size;
  313. }
  314. /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
  315. symnum out of range. */
  316. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  317. char *name, char *module_name, int *exported);
  318. /* Look for this name: can be of form module:name. */
  319. unsigned long module_kallsyms_lookup_name(const char *name);
  320. extern void __module_put_and_exit(struct module *mod, long code)
  321. __attribute__((noreturn));
  322. #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
  323. #ifdef CONFIG_MODULE_UNLOAD
  324. unsigned int module_refcount(struct module *mod);
  325. void __symbol_put(const char *symbol);
  326. #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
  327. void symbol_put_addr(void *addr);
  328. static inline local_t *__module_ref_addr(struct module *mod, int cpu)
  329. {
  330. #ifdef CONFIG_SMP
  331. return (local_t *) (mod->refptr + per_cpu_offset(cpu));
  332. #else
  333. return &mod->ref;
  334. #endif
  335. }
  336. /* Sometimes we know we already have a refcount, and it's easier not
  337. to handle the error case (which only happens with rmmod --wait). */
  338. static inline void __module_get(struct module *module)
  339. {
  340. if (module) {
  341. local_inc(__module_ref_addr(module, get_cpu()));
  342. put_cpu();
  343. }
  344. }
  345. static inline int try_module_get(struct module *module)
  346. {
  347. int ret = 1;
  348. if (module) {
  349. unsigned int cpu = get_cpu();
  350. if (likely(module_is_live(module)))
  351. local_inc(__module_ref_addr(module, cpu));
  352. else
  353. ret = 0;
  354. put_cpu();
  355. }
  356. return ret;
  357. }
  358. extern void module_put(struct module *module);
  359. #else /*!CONFIG_MODULE_UNLOAD*/
  360. static inline int try_module_get(struct module *module)
  361. {
  362. return !module || module_is_live(module);
  363. }
  364. static inline void module_put(struct module *module)
  365. {
  366. }
  367. static inline void __module_get(struct module *module)
  368. {
  369. }
  370. #define symbol_put(x) do { } while(0)
  371. #define symbol_put_addr(p) do { } while(0)
  372. #endif /* CONFIG_MODULE_UNLOAD */
  373. /* This is a #define so the string doesn't get put in every .o file */
  374. #define module_name(mod) \
  375. ({ \
  376. struct module *__mod = (mod); \
  377. __mod ? __mod->name : "kernel"; \
  378. })
  379. /* For kallsyms to ask for address resolution. namebuf should be at
  380. * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
  381. * found, otherwise NULL. */
  382. const char *module_address_lookup(unsigned long addr,
  383. unsigned long *symbolsize,
  384. unsigned long *offset,
  385. char **modname,
  386. char *namebuf);
  387. int lookup_module_symbol_name(unsigned long addr, char *symname);
  388. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
  389. /* For extable.c to search modules' exception tables. */
  390. const struct exception_table_entry *search_module_extables(unsigned long addr);
  391. int register_module_notifier(struct notifier_block * nb);
  392. int unregister_module_notifier(struct notifier_block * nb);
  393. extern void print_modules(void);
  394. extern void module_update_markers(void);
  395. extern void module_update_tracepoints(void);
  396. extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
  397. #else /* !CONFIG_MODULES... */
  398. #define EXPORT_SYMBOL(sym)
  399. #define EXPORT_SYMBOL_GPL(sym)
  400. #define EXPORT_SYMBOL_GPL_FUTURE(sym)
  401. #define EXPORT_UNUSED_SYMBOL(sym)
  402. #define EXPORT_UNUSED_SYMBOL_GPL(sym)
  403. /* Given an address, look for it in the exception tables. */
  404. static inline const struct exception_table_entry *
  405. search_module_extables(unsigned long addr)
  406. {
  407. return NULL;
  408. }
  409. static inline struct module *module_text_address(unsigned long addr)
  410. {
  411. return NULL;
  412. }
  413. static inline struct module *__module_address(unsigned long addr)
  414. {
  415. return NULL;
  416. }
  417. static inline struct module *__module_text_address(unsigned long addr)
  418. {
  419. return NULL;
  420. }
  421. static inline bool is_module_address(unsigned long addr)
  422. {
  423. return false;
  424. }
  425. static inline bool is_module_text_address(unsigned long addr)
  426. {
  427. return false;
  428. }
  429. /* Get/put a kernel symbol (calls should be symmetric) */
  430. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
  431. #define symbol_put(x) do { } while(0)
  432. #define symbol_put_addr(x) do { } while(0)
  433. static inline void __module_get(struct module *module)
  434. {
  435. }
  436. static inline int try_module_get(struct module *module)
  437. {
  438. return 1;
  439. }
  440. static inline void module_put(struct module *module)
  441. {
  442. }
  443. #define module_name(mod) "kernel"
  444. /* For kallsyms to ask for address resolution. NULL means not found. */
  445. static inline const char *module_address_lookup(unsigned long addr,
  446. unsigned long *symbolsize,
  447. unsigned long *offset,
  448. char **modname,
  449. char *namebuf)
  450. {
  451. return NULL;
  452. }
  453. static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
  454. {
  455. return -ERANGE;
  456. }
  457. static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
  458. {
  459. return -ERANGE;
  460. }
  461. static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
  462. char *type, char *name,
  463. char *module_name, int *exported)
  464. {
  465. return -ERANGE;
  466. }
  467. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  468. {
  469. return 0;
  470. }
  471. static inline int register_module_notifier(struct notifier_block * nb)
  472. {
  473. /* no events will happen anyway, so this can always succeed */
  474. return 0;
  475. }
  476. static inline int unregister_module_notifier(struct notifier_block * nb)
  477. {
  478. return 0;
  479. }
  480. #define module_put_and_exit(code) do_exit(code)
  481. static inline void print_modules(void)
  482. {
  483. }
  484. static inline void module_update_markers(void)
  485. {
  486. }
  487. static inline void module_update_tracepoints(void)
  488. {
  489. }
  490. static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  491. {
  492. return 0;
  493. }
  494. #endif /* CONFIG_MODULES */
  495. struct device_driver;
  496. #ifdef CONFIG_SYSFS
  497. struct module;
  498. extern struct kset *module_kset;
  499. extern struct kobj_type module_ktype;
  500. extern int module_sysfs_initialized;
  501. int mod_sysfs_init(struct module *mod);
  502. int mod_sysfs_setup(struct module *mod,
  503. struct kernel_param *kparam,
  504. unsigned int num_params);
  505. int module_add_modinfo_attrs(struct module *mod);
  506. void module_remove_modinfo_attrs(struct module *mod);
  507. #else /* !CONFIG_SYSFS */
  508. static inline int mod_sysfs_init(struct module *mod)
  509. {
  510. return 0;
  511. }
  512. static inline int mod_sysfs_setup(struct module *mod,
  513. struct kernel_param *kparam,
  514. unsigned int num_params)
  515. {
  516. return 0;
  517. }
  518. static inline int module_add_modinfo_attrs(struct module *mod)
  519. {
  520. return 0;
  521. }
  522. static inline void module_remove_modinfo_attrs(struct module *mod)
  523. { }
  524. #endif /* CONFIG_SYSFS */
  525. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  526. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  527. #define __MODULE_STRING(x) __stringify(x)
  528. #endif /* _LINUX_MODULE_H */