module.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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/tracepoint.h>
  19. #include <linux/export.h>
  20. #include <linux/percpu.h>
  21. #include <asm/module.h>
  22. /* Not Yet Implemented */
  23. #define MODULE_SUPPORTED_DEVICE(name)
  24. #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
  25. struct modversion_info
  26. {
  27. unsigned long crc;
  28. char name[MODULE_NAME_LEN];
  29. };
  30. struct module;
  31. struct module_kobject {
  32. struct kobject kobj;
  33. struct module *mod;
  34. struct kobject *drivers_dir;
  35. struct module_param_attrs *mp;
  36. };
  37. struct module_attribute {
  38. struct attribute attr;
  39. ssize_t (*show)(struct module_attribute *, struct module_kobject *,
  40. char *);
  41. ssize_t (*store)(struct module_attribute *, struct module_kobject *,
  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_version_attribute {
  48. struct module_attribute mattr;
  49. const char *module_name;
  50. const char *version;
  51. } __attribute__ ((__aligned__(sizeof(void *))));
  52. extern ssize_t __modver_version_show(struct module_attribute *,
  53. struct module_kobject *, char *);
  54. extern struct module_attribute module_uevent;
  55. /* These are either module local, or the kernel's dummy ones. */
  56. extern int init_module(void);
  57. extern void cleanup_module(void);
  58. /* Archs provide a method of finding the correct exception table. */
  59. struct exception_table_entry;
  60. const struct exception_table_entry *
  61. search_extable(const struct exception_table_entry *first,
  62. const struct exception_table_entry *last,
  63. unsigned long value);
  64. void sort_extable(struct exception_table_entry *start,
  65. struct exception_table_entry *finish);
  66. void sort_main_extable(void);
  67. void trim_init_extable(struct module *m);
  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. #else /* !MODULE */
  73. #define MODULE_GENERIC_TABLE(gtype,name)
  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. /*
  109. * Author(s), use "Name <email>" or just "Name", for multiple
  110. * authors use multiple MODULE_AUTHOR() statements/lines.
  111. */
  112. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  113. /* What your module does. */
  114. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  115. #define MODULE_DEVICE_TABLE(type,name) \
  116. MODULE_GENERIC_TABLE(type##_device,name)
  117. /* Version of form [<epoch>:]<version>[-<extra-version>].
  118. Or for CVS/RCS ID version, everything but the number is stripped.
  119. <epoch>: A (small) unsigned integer which allows you to start versions
  120. anew. If not mentioned, it's zero. eg. "2:1.0" is after
  121. "1:2.0".
  122. <version>: The <version> may contain only alphanumerics and the
  123. character `.'. Ordered by numeric sort for numeric parts,
  124. ascii sort for ascii parts (as per RPM or DEB algorithm).
  125. <extraversion>: Like <version>, but inserted for local
  126. customizations, eg "rh3" or "rusty1".
  127. Using this automatically adds a checksum of the .c files and the
  128. local headers in "srcversion".
  129. */
  130. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  131. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  132. #else
  133. #define MODULE_VERSION(_version) \
  134. static struct module_version_attribute ___modver_attr = { \
  135. .mattr = { \
  136. .attr = { \
  137. .name = "version", \
  138. .mode = S_IRUGO, \
  139. }, \
  140. .show = __modver_version_show, \
  141. }, \
  142. .module_name = KBUILD_MODNAME, \
  143. .version = _version, \
  144. }; \
  145. static const struct module_version_attribute \
  146. __used __attribute__ ((__section__ ("__modver"))) \
  147. * __moduleparam_const __modver_attr = &___modver_attr
  148. #endif
  149. /* Optional firmware file (or files) needed by the module
  150. * format is simply firmware file name. Multiple firmware
  151. * files require multiple MODULE_FIRMWARE() specifiers */
  152. #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  153. /* Given an address, look for it in the exception tables */
  154. const struct exception_table_entry *search_exception_tables(unsigned long add);
  155. struct notifier_block;
  156. #ifdef CONFIG_MODULES
  157. extern int modules_disabled; /* for sysctl */
  158. /* Get/put a kernel symbol (calls must be symmetric) */
  159. void *__symbol_get(const char *symbol);
  160. void *__symbol_get_gpl(const char *symbol);
  161. #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
  162. /* modules using other modules: kdb wants to see this. */
  163. struct module_use {
  164. struct list_head source_list;
  165. struct list_head target_list;
  166. struct module *source, *target;
  167. };
  168. enum module_state
  169. {
  170. MODULE_STATE_LIVE,
  171. MODULE_STATE_COMING,
  172. MODULE_STATE_GOING,
  173. };
  174. /**
  175. * struct module_ref - per cpu module reference counts
  176. * @incs: number of module get on this cpu
  177. * @decs: number of module put on this cpu
  178. *
  179. * We force an alignment on 8 or 16 bytes, so that alloc_percpu()
  180. * put @incs/@decs in same cache line, with no extra memory cost,
  181. * since alloc_percpu() is fine grained.
  182. */
  183. struct module_ref {
  184. unsigned long incs;
  185. unsigned long decs;
  186. } __attribute((aligned(2 * sizeof(unsigned long))));
  187. struct module
  188. {
  189. enum module_state state;
  190. /* Member of list of modules */
  191. struct list_head list;
  192. /* Unique handle for this module */
  193. char name[MODULE_NAME_LEN];
  194. /* Sysfs stuff. */
  195. struct module_kobject mkobj;
  196. struct module_attribute *modinfo_attrs;
  197. const char *version;
  198. const char *srcversion;
  199. struct kobject *holders_dir;
  200. /* Exported symbols */
  201. const struct kernel_symbol *syms;
  202. const unsigned long *crcs;
  203. unsigned int num_syms;
  204. /* Kernel parameters. */
  205. struct kernel_param *kp;
  206. unsigned int num_kp;
  207. /* GPL-only exported symbols. */
  208. unsigned int num_gpl_syms;
  209. const struct kernel_symbol *gpl_syms;
  210. const unsigned long *gpl_crcs;
  211. #ifdef CONFIG_UNUSED_SYMBOLS
  212. /* unused exported symbols. */
  213. const struct kernel_symbol *unused_syms;
  214. const unsigned long *unused_crcs;
  215. unsigned int num_unused_syms;
  216. /* GPL-only, unused exported symbols. */
  217. unsigned int num_unused_gpl_syms;
  218. const struct kernel_symbol *unused_gpl_syms;
  219. const unsigned long *unused_gpl_crcs;
  220. #endif
  221. /* symbols that will be GPL-only in the near future. */
  222. const struct kernel_symbol *gpl_future_syms;
  223. const unsigned long *gpl_future_crcs;
  224. unsigned int num_gpl_future_syms;
  225. /* Exception table */
  226. unsigned int num_exentries;
  227. struct exception_table_entry *extable;
  228. /* Startup function. */
  229. int (*init)(void);
  230. /* If this is non-NULL, vfree after init() returns */
  231. void *module_init;
  232. /* Here is the actual code + data, vfree'd on unload. */
  233. void *module_core;
  234. /* Here are the sizes of the init and core sections */
  235. unsigned int init_size, core_size;
  236. /* The size of the executable code in each section. */
  237. unsigned int init_text_size, core_text_size;
  238. /* Size of RO sections of the module (text+rodata) */
  239. unsigned int init_ro_size, core_ro_size;
  240. /* Arch-specific module values */
  241. struct mod_arch_specific arch;
  242. unsigned int taints; /* same bits as kernel:tainted */
  243. #ifdef CONFIG_GENERIC_BUG
  244. /* Support for BUG */
  245. unsigned num_bugs;
  246. struct list_head bug_list;
  247. struct bug_entry *bug_table;
  248. #endif
  249. #ifdef CONFIG_KALLSYMS
  250. /*
  251. * We keep the symbol and string tables for kallsyms.
  252. * The core_* fields below are temporary, loader-only (they
  253. * could really be discarded after module init).
  254. */
  255. Elf_Sym *symtab, *core_symtab;
  256. unsigned int num_symtab, core_num_syms;
  257. char *strtab, *core_strtab;
  258. /* Section attributes */
  259. struct module_sect_attrs *sect_attrs;
  260. /* Notes attributes */
  261. struct module_notes_attrs *notes_attrs;
  262. #endif
  263. /* The command line arguments (may be mangled). People like
  264. keeping pointers to this stuff */
  265. char *args;
  266. #ifdef CONFIG_SMP
  267. /* Per-cpu data. */
  268. void __percpu *percpu;
  269. unsigned int percpu_size;
  270. #endif
  271. #ifdef CONFIG_TRACEPOINTS
  272. unsigned int num_tracepoints;
  273. struct tracepoint * const *tracepoints_ptrs;
  274. #endif
  275. #ifdef HAVE_JUMP_LABEL
  276. struct jump_entry *jump_entries;
  277. unsigned int num_jump_entries;
  278. #endif
  279. #ifdef CONFIG_TRACING
  280. unsigned int num_trace_bprintk_fmt;
  281. const char **trace_bprintk_fmt_start;
  282. #endif
  283. #ifdef CONFIG_EVENT_TRACING
  284. struct ftrace_event_call **trace_events;
  285. unsigned int num_trace_events;
  286. #endif
  287. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  288. unsigned int num_ftrace_callsites;
  289. unsigned long *ftrace_callsites;
  290. #endif
  291. #ifdef CONFIG_MODULE_UNLOAD
  292. /* What modules depend on me? */
  293. struct list_head source_list;
  294. /* What modules do I depend on? */
  295. struct list_head target_list;
  296. /* Who is waiting for us to be unloaded */
  297. struct task_struct *waiter;
  298. /* Destruction function. */
  299. void (*exit)(void);
  300. struct module_ref __percpu *refptr;
  301. #endif
  302. #ifdef CONFIG_CONSTRUCTORS
  303. /* Constructor functions. */
  304. ctor_fn_t *ctors;
  305. unsigned int num_ctors;
  306. #endif
  307. };
  308. #ifndef MODULE_ARCH_INIT
  309. #define MODULE_ARCH_INIT {}
  310. #endif
  311. extern struct mutex module_mutex;
  312. /* FIXME: It'd be nice to isolate modules during init, too, so they
  313. aren't used before they (may) fail. But presently too much code
  314. (IDE & SCSI) require entry into the module during init.*/
  315. static inline int module_is_live(struct module *mod)
  316. {
  317. return mod->state != MODULE_STATE_GOING;
  318. }
  319. struct module *__module_text_address(unsigned long addr);
  320. struct module *__module_address(unsigned long addr);
  321. bool is_module_address(unsigned long addr);
  322. bool is_module_percpu_address(unsigned long addr);
  323. bool is_module_text_address(unsigned long addr);
  324. static inline int within_module_core(unsigned long addr, struct module *mod)
  325. {
  326. return (unsigned long)mod->module_core <= addr &&
  327. addr < (unsigned long)mod->module_core + mod->core_size;
  328. }
  329. static inline int within_module_init(unsigned long addr, struct module *mod)
  330. {
  331. return (unsigned long)mod->module_init <= addr &&
  332. addr < (unsigned long)mod->module_init + mod->init_size;
  333. }
  334. /* Search for module by name: must hold module_mutex. */
  335. struct module *find_module(const char *name);
  336. struct symsearch {
  337. const struct kernel_symbol *start, *stop;
  338. const unsigned long *crcs;
  339. enum {
  340. NOT_GPL_ONLY,
  341. GPL_ONLY,
  342. WILL_BE_GPL_ONLY,
  343. } licence;
  344. bool unused;
  345. };
  346. /* Search for an exported symbol by name. */
  347. const struct kernel_symbol *find_symbol(const char *name,
  348. struct module **owner,
  349. const unsigned long **crc,
  350. bool gplok,
  351. bool warn);
  352. /* Walk the exported symbol table */
  353. bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
  354. struct module *owner,
  355. void *data), void *data);
  356. /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
  357. symnum out of range. */
  358. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  359. char *name, char *module_name, int *exported);
  360. /* Look for this name: can be of form module:name. */
  361. unsigned long module_kallsyms_lookup_name(const char *name);
  362. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  363. struct module *, unsigned long),
  364. void *data);
  365. extern void __module_put_and_exit(struct module *mod, long code)
  366. __attribute__((noreturn));
  367. #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
  368. #ifdef CONFIG_MODULE_UNLOAD
  369. unsigned long module_refcount(struct module *mod);
  370. void __symbol_put(const char *symbol);
  371. #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
  372. void symbol_put_addr(void *addr);
  373. /* Sometimes we know we already have a refcount, and it's easier not
  374. to handle the error case (which only happens with rmmod --wait). */
  375. extern void __module_get(struct module *module);
  376. /* This is the Right Way to get a module: if it fails, it's being removed,
  377. * so pretend it's not there. */
  378. extern bool try_module_get(struct module *module);
  379. extern void module_put(struct module *module);
  380. #else /*!CONFIG_MODULE_UNLOAD*/
  381. static inline int try_module_get(struct module *module)
  382. {
  383. return !module || module_is_live(module);
  384. }
  385. static inline void module_put(struct module *module)
  386. {
  387. }
  388. static inline void __module_get(struct module *module)
  389. {
  390. }
  391. #define symbol_put(x) do { } while(0)
  392. #define symbol_put_addr(p) do { } while(0)
  393. #endif /* CONFIG_MODULE_UNLOAD */
  394. int ref_module(struct module *a, struct module *b);
  395. /* This is a #define so the string doesn't get put in every .o file */
  396. #define module_name(mod) \
  397. ({ \
  398. struct module *__mod = (mod); \
  399. __mod ? __mod->name : "kernel"; \
  400. })
  401. /* For kallsyms to ask for address resolution. namebuf should be at
  402. * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
  403. * found, otherwise NULL. */
  404. const char *module_address_lookup(unsigned long addr,
  405. unsigned long *symbolsize,
  406. unsigned long *offset,
  407. char **modname,
  408. char *namebuf);
  409. int lookup_module_symbol_name(unsigned long addr, char *symname);
  410. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
  411. /* For extable.c to search modules' exception tables. */
  412. const struct exception_table_entry *search_module_extables(unsigned long addr);
  413. int register_module_notifier(struct notifier_block * nb);
  414. int unregister_module_notifier(struct notifier_block * nb);
  415. extern void print_modules(void);
  416. #else /* !CONFIG_MODULES... */
  417. /* Given an address, look for it in the exception tables. */
  418. static inline const struct exception_table_entry *
  419. search_module_extables(unsigned long addr)
  420. {
  421. return NULL;
  422. }
  423. static inline struct module *__module_address(unsigned long addr)
  424. {
  425. return NULL;
  426. }
  427. static inline struct module *__module_text_address(unsigned long addr)
  428. {
  429. return NULL;
  430. }
  431. static inline bool is_module_address(unsigned long addr)
  432. {
  433. return false;
  434. }
  435. static inline bool is_module_percpu_address(unsigned long addr)
  436. {
  437. return false;
  438. }
  439. static inline bool is_module_text_address(unsigned long addr)
  440. {
  441. return false;
  442. }
  443. /* Get/put a kernel symbol (calls should be symmetric) */
  444. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
  445. #define symbol_put(x) do { } while(0)
  446. #define symbol_put_addr(x) do { } while(0)
  447. static inline void __module_get(struct module *module)
  448. {
  449. }
  450. static inline int try_module_get(struct module *module)
  451. {
  452. return 1;
  453. }
  454. static inline void module_put(struct module *module)
  455. {
  456. }
  457. #define module_name(mod) "kernel"
  458. /* For kallsyms to ask for address resolution. NULL means not found. */
  459. static inline const char *module_address_lookup(unsigned long addr,
  460. unsigned long *symbolsize,
  461. unsigned long *offset,
  462. char **modname,
  463. char *namebuf)
  464. {
  465. return NULL;
  466. }
  467. static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
  468. {
  469. return -ERANGE;
  470. }
  471. static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
  472. {
  473. return -ERANGE;
  474. }
  475. static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
  476. char *type, char *name,
  477. char *module_name, int *exported)
  478. {
  479. return -ERANGE;
  480. }
  481. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  482. {
  483. return 0;
  484. }
  485. static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  486. struct module *,
  487. unsigned long),
  488. void *data)
  489. {
  490. return 0;
  491. }
  492. static inline int register_module_notifier(struct notifier_block * nb)
  493. {
  494. /* no events will happen anyway, so this can always succeed */
  495. return 0;
  496. }
  497. static inline int unregister_module_notifier(struct notifier_block * nb)
  498. {
  499. return 0;
  500. }
  501. #define module_put_and_exit(code) do_exit(code)
  502. static inline void print_modules(void)
  503. {
  504. }
  505. #endif /* CONFIG_MODULES */
  506. #ifdef CONFIG_SYSFS
  507. extern struct kset *module_kset;
  508. extern struct kobj_type module_ktype;
  509. extern int module_sysfs_initialized;
  510. #endif /* CONFIG_SYSFS */
  511. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  512. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  513. #define __MODULE_STRING(x) __stringify(x)
  514. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  515. extern void set_all_modules_text_rw(void);
  516. extern void set_all_modules_text_ro(void);
  517. #else
  518. static inline void set_all_modules_text_rw(void) { }
  519. static inline void set_all_modules_text_ro(void) { }
  520. #endif
  521. #ifdef CONFIG_GENERIC_BUG
  522. void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
  523. struct module *);
  524. void module_bug_cleanup(struct module *);
  525. #else /* !CONFIG_GENERIC_BUG */
  526. static inline void module_bug_finalize(const Elf_Ehdr *hdr,
  527. const Elf_Shdr *sechdrs,
  528. struct module *mod)
  529. {
  530. }
  531. static inline void module_bug_cleanup(struct module *mod) {}
  532. #endif /* CONFIG_GENERIC_BUG */
  533. #endif /* _LINUX_MODULE_H */