module.h 18 KB

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