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