module.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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/config.h>
  10. #include <linux/sched.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/list.h>
  13. #include <linux/stat.h>
  14. #include <linux/compiler.h>
  15. #include <linux/cache.h>
  16. #include <linux/kmod.h>
  17. #include <linux/elf.h>
  18. #include <linux/stringify.h>
  19. #include <linux/kobject.h>
  20. #include <linux/moduleparam.h>
  21. #include <asm/local.h>
  22. #include <asm/module.h>
  23. /* Not Yet Implemented */
  24. #define MODULE_SUPPORTED_DEVICE(name)
  25. /* v850 toolchain uses a `_' prefix for all user symbols */
  26. #ifndef MODULE_SYMBOL_PREFIX
  27. #define MODULE_SYMBOL_PREFIX ""
  28. #endif
  29. #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
  30. struct kernel_symbol
  31. {
  32. unsigned long value;
  33. const char *name;
  34. };
  35. struct modversion_info
  36. {
  37. unsigned long crc;
  38. char name[MODULE_NAME_LEN];
  39. };
  40. struct module;
  41. struct module_attribute {
  42. struct attribute attr;
  43. ssize_t (*show)(struct module_attribute *, struct module *, char *);
  44. ssize_t (*store)(struct module_attribute *, struct module *,
  45. const char *, size_t count);
  46. };
  47. struct module_kobject
  48. {
  49. struct kobject kobj;
  50. struct module *mod;
  51. };
  52. /* These are either module local, or the kernel's dummy ones. */
  53. extern int init_module(void);
  54. extern void cleanup_module(void);
  55. /* Archs provide a method of finding the correct exception table. */
  56. struct exception_table_entry;
  57. const struct exception_table_entry *
  58. search_extable(const struct exception_table_entry *first,
  59. const struct exception_table_entry *last,
  60. unsigned long value);
  61. void sort_extable(struct exception_table_entry *start,
  62. struct exception_table_entry *finish);
  63. void sort_main_extable(void);
  64. extern struct subsystem module_subsys;
  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 MPL/GPL" [GNU Public License v2
  89. * or Mozilla license choice]
  90. *
  91. * The following other idents are available
  92. *
  93. * "Proprietary" [Non free products]
  94. *
  95. * There are dual licensed components, but when running with Linux it is the
  96. * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
  97. * is a GPL combined work.
  98. *
  99. * This exists for several reasons
  100. * 1. So modinfo can show license info for users wanting to vet their setup
  101. * is free
  102. * 2. So the community can ignore bug reports including proprietary modules
  103. * 3. So vendors can do likewise based on their own policies
  104. */
  105. #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
  106. /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
  107. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  108. /* What your module does. */
  109. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  110. /* One for each parameter, describing how to use it. Some files do
  111. multiple of these per line, so can't just use MODULE_INFO. */
  112. #define MODULE_PARM_DESC(_parm, desc) \
  113. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  114. #define MODULE_DEVICE_TABLE(type,name) \
  115. MODULE_GENERIC_TABLE(type##_device,name)
  116. /* Version of form [<epoch>:]<version>[-<extra-version>].
  117. Or for CVS/RCS ID version, everything but the number is stripped.
  118. <epoch>: A (small) unsigned integer which allows you to start versions
  119. anew. If not mentioned, it's zero. eg. "2:1.0" is after
  120. "1:2.0".
  121. <version>: The <version> may contain only alphanumerics and the
  122. character `.'. Ordered by numeric sort for numeric parts,
  123. ascii sort for ascii parts (as per RPM or DEB algorithm).
  124. <extraversion>: Like <version>, but inserted for local
  125. customizations, eg "rh3" or "rusty1".
  126. Using this automatically adds a checksum of the .c files and the
  127. local headers in "srcversion".
  128. */
  129. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  130. /* Given an address, look for it in the exception tables */
  131. const struct exception_table_entry *search_exception_tables(unsigned long add);
  132. struct notifier_block;
  133. #ifdef CONFIG_MODULES
  134. /* Get/put a kernel symbol (calls must be symmetric) */
  135. void *__symbol_get(const char *symbol);
  136. void *__symbol_get_gpl(const char *symbol);
  137. #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
  138. #ifndef __GENKSYMS__
  139. #ifdef CONFIG_MODVERSIONS
  140. /* Mark the CRC weak since genksyms apparently decides not to
  141. * generate a checksums for some symbols */
  142. #define __CRC_SYMBOL(sym, sec) \
  143. extern void *__crc_##sym __attribute__((weak)); \
  144. static const unsigned long __kcrctab_##sym \
  145. __attribute_used__ \
  146. __attribute__((section("__kcrctab" sec), unused)) \
  147. = (unsigned long) &__crc_##sym;
  148. #else
  149. #define __CRC_SYMBOL(sym, sec)
  150. #endif
  151. /* For every exported symbol, place a struct in the __ksymtab section */
  152. #define __EXPORT_SYMBOL(sym, sec) \
  153. __CRC_SYMBOL(sym, sec) \
  154. static const char __kstrtab_##sym[] \
  155. __attribute__((section("__ksymtab_strings"))) \
  156. = MODULE_SYMBOL_PREFIX #sym; \
  157. static const struct kernel_symbol __ksymtab_##sym \
  158. __attribute_used__ \
  159. __attribute__((section("__ksymtab" sec), unused)) \
  160. = { (unsigned long)&sym, __kstrtab_##sym }
  161. #define EXPORT_SYMBOL(sym) \
  162. __EXPORT_SYMBOL(sym, "")
  163. #define EXPORT_SYMBOL_GPL(sym) \
  164. __EXPORT_SYMBOL(sym, "_gpl")
  165. #endif
  166. struct module_ref
  167. {
  168. local_t count;
  169. } ____cacheline_aligned;
  170. enum module_state
  171. {
  172. MODULE_STATE_LIVE,
  173. MODULE_STATE_COMING,
  174. MODULE_STATE_GOING,
  175. };
  176. /* Similar stuff for section attributes. */
  177. #define MODULE_SECT_NAME_LEN 32
  178. struct module_sect_attr
  179. {
  180. struct module_attribute mattr;
  181. char name[MODULE_SECT_NAME_LEN];
  182. unsigned long address;
  183. };
  184. struct module_sect_attrs
  185. {
  186. struct attribute_group grp;
  187. struct module_sect_attr attrs[0];
  188. };
  189. struct module_param_attrs;
  190. struct module
  191. {
  192. enum module_state state;
  193. /* Member of list of modules */
  194. struct list_head list;
  195. /* Unique handle for this module */
  196. char name[MODULE_NAME_LEN];
  197. /* Sysfs stuff. */
  198. struct module_kobject mkobj;
  199. struct module_param_attrs *param_attrs;
  200. /* Exported symbols */
  201. const struct kernel_symbol *syms;
  202. unsigned int num_syms;
  203. const unsigned long *crcs;
  204. /* GPL-only exported symbols. */
  205. const struct kernel_symbol *gpl_syms;
  206. unsigned int num_gpl_syms;
  207. const unsigned long *gpl_crcs;
  208. /* Exception table */
  209. unsigned int num_exentries;
  210. const struct exception_table_entry *extable;
  211. /* Startup function. */
  212. int (*init)(void);
  213. /* If this is non-NULL, vfree after init() returns */
  214. void *module_init;
  215. /* Here is the actual code + data, vfree'd on unload. */
  216. void *module_core;
  217. /* Here are the sizes of the init and core sections */
  218. unsigned long init_size, core_size;
  219. /* The size of the executable code in each section. */
  220. unsigned long init_text_size, core_text_size;
  221. /* Arch-specific module values */
  222. struct mod_arch_specific arch;
  223. /* Am I unsafe to unload? */
  224. int unsafe;
  225. /* Am I GPL-compatible */
  226. int license_gplok;
  227. #ifdef CONFIG_MODULE_UNLOAD
  228. /* Reference counts */
  229. struct module_ref ref[NR_CPUS];
  230. /* What modules depend on me? */
  231. struct list_head modules_which_use_me;
  232. /* Who is waiting for us to be unloaded */
  233. struct task_struct *waiter;
  234. /* Destruction function. */
  235. void (*exit)(void);
  236. #endif
  237. #ifdef CONFIG_KALLSYMS
  238. /* We keep the symbol and string tables for kallsyms. */
  239. Elf_Sym *symtab;
  240. unsigned long num_symtab;
  241. char *strtab;
  242. /* Section attributes */
  243. struct module_sect_attrs *sect_attrs;
  244. #endif
  245. /* Per-cpu data. */
  246. void *percpu;
  247. /* The command line arguments (may be mangled). People like
  248. keeping pointers to this stuff */
  249. char *args;
  250. };
  251. /* FIXME: It'd be nice to isolate modules during init, too, so they
  252. aren't used before they (may) fail. But presently too much code
  253. (IDE & SCSI) require entry into the module during init.*/
  254. static inline int module_is_live(struct module *mod)
  255. {
  256. return mod->state != MODULE_STATE_GOING;
  257. }
  258. /* Is this address in a module? (second is with no locks, for oops) */
  259. struct module *module_text_address(unsigned long addr);
  260. struct module *__module_text_address(unsigned long addr);
  261. /* Returns module and fills in value, defined and namebuf, or NULL if
  262. symnum out of range. */
  263. struct module *module_get_kallsym(unsigned int symnum,
  264. unsigned long *value,
  265. char *type,
  266. char namebuf[128]);
  267. /* Look for this name: can be of form module:name. */
  268. unsigned long module_kallsyms_lookup_name(const char *name);
  269. int is_exported(const char *name, const struct module *mod);
  270. extern void __module_put_and_exit(struct module *mod, long code)
  271. __attribute__((noreturn));
  272. #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
  273. #ifdef CONFIG_MODULE_UNLOAD
  274. unsigned int module_refcount(struct module *mod);
  275. void __symbol_put(const char *symbol);
  276. #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
  277. void symbol_put_addr(void *addr);
  278. /* Sometimes we know we already have a refcount, and it's easier not
  279. to handle the error case (which only happens with rmmod --wait). */
  280. static inline void __module_get(struct module *module)
  281. {
  282. if (module) {
  283. BUG_ON(module_refcount(module) == 0);
  284. local_inc(&module->ref[get_cpu()].count);
  285. put_cpu();
  286. }
  287. }
  288. static inline int try_module_get(struct module *module)
  289. {
  290. int ret = 1;
  291. if (module) {
  292. unsigned int cpu = get_cpu();
  293. if (likely(module_is_live(module)))
  294. local_inc(&module->ref[cpu].count);
  295. else
  296. ret = 0;
  297. put_cpu();
  298. }
  299. return ret;
  300. }
  301. static inline void module_put(struct module *module)
  302. {
  303. if (module) {
  304. unsigned int cpu = get_cpu();
  305. local_dec(&module->ref[cpu].count);
  306. /* Maybe they're waiting for us to drop reference? */
  307. if (unlikely(!module_is_live(module)))
  308. wake_up_process(module->waiter);
  309. put_cpu();
  310. }
  311. }
  312. #else /*!CONFIG_MODULE_UNLOAD*/
  313. static inline int try_module_get(struct module *module)
  314. {
  315. return !module || module_is_live(module);
  316. }
  317. static inline void module_put(struct module *module)
  318. {
  319. }
  320. static inline void __module_get(struct module *module)
  321. {
  322. }
  323. #define symbol_put(x) do { } while(0)
  324. #define symbol_put_addr(p) do { } while(0)
  325. #endif /* CONFIG_MODULE_UNLOAD */
  326. /* This is a #define so the string doesn't get put in every .o file */
  327. #define module_name(mod) \
  328. ({ \
  329. struct module *__mod = (mod); \
  330. __mod ? __mod->name : "kernel"; \
  331. })
  332. #define __unsafe(mod) \
  333. do { \
  334. if (mod && !(mod)->unsafe) { \
  335. printk(KERN_WARNING \
  336. "Module %s cannot be unloaded due to unsafe usage in" \
  337. " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
  338. (mod)->unsafe = 1; \
  339. } \
  340. } while(0)
  341. /* For kallsyms to ask for address resolution. NULL means not found. */
  342. const char *module_address_lookup(unsigned long addr,
  343. unsigned long *symbolsize,
  344. unsigned long *offset,
  345. char **modname);
  346. /* For extable.c to search modules' exception tables. */
  347. const struct exception_table_entry *search_module_extables(unsigned long addr);
  348. int register_module_notifier(struct notifier_block * nb);
  349. int unregister_module_notifier(struct notifier_block * nb);
  350. extern void print_modules(void);
  351. struct device_driver;
  352. void module_add_driver(struct module *, struct device_driver *);
  353. void module_remove_driver(struct device_driver *);
  354. #else /* !CONFIG_MODULES... */
  355. #define EXPORT_SYMBOL(sym)
  356. #define EXPORT_SYMBOL_GPL(sym)
  357. /* Given an address, look for it in the exception tables. */
  358. static inline const struct exception_table_entry *
  359. search_module_extables(unsigned long addr)
  360. {
  361. return NULL;
  362. }
  363. /* Is this address in a module? */
  364. static inline struct module *module_text_address(unsigned long addr)
  365. {
  366. return NULL;
  367. }
  368. /* Is this address in a module? (don't take a lock, we're oopsing) */
  369. static inline struct module *__module_text_address(unsigned long addr)
  370. {
  371. return NULL;
  372. }
  373. /* Get/put a kernel symbol (calls should be symmetric) */
  374. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
  375. #define symbol_put(x) do { } while(0)
  376. #define symbol_put_addr(x) do { } while(0)
  377. static inline void __module_get(struct module *module)
  378. {
  379. }
  380. static inline int try_module_get(struct module *module)
  381. {
  382. return 1;
  383. }
  384. static inline void module_put(struct module *module)
  385. {
  386. }
  387. #define module_name(mod) "kernel"
  388. #define __unsafe(mod)
  389. /* For kallsyms to ask for address resolution. NULL means not found. */
  390. static inline const char *module_address_lookup(unsigned long addr,
  391. unsigned long *symbolsize,
  392. unsigned long *offset,
  393. char **modname)
  394. {
  395. return NULL;
  396. }
  397. static inline struct module *module_get_kallsym(unsigned int symnum,
  398. unsigned long *value,
  399. char *type,
  400. char namebuf[128])
  401. {
  402. return NULL;
  403. }
  404. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  405. {
  406. return 0;
  407. }
  408. static inline int is_exported(const char *name, const struct module *mod)
  409. {
  410. return 0;
  411. }
  412. static inline int register_module_notifier(struct notifier_block * nb)
  413. {
  414. /* no events will happen anyway, so this can always succeed */
  415. return 0;
  416. }
  417. static inline int unregister_module_notifier(struct notifier_block * nb)
  418. {
  419. return 0;
  420. }
  421. #define module_put_and_exit(code) do_exit(code)
  422. static inline void print_modules(void)
  423. {
  424. }
  425. struct device_driver;
  426. struct module;
  427. static inline void module_add_driver(struct module *module, struct device_driver *driver)
  428. {
  429. }
  430. static inline void module_remove_driver(struct device_driver *driver)
  431. {
  432. }
  433. #endif /* CONFIG_MODULES */
  434. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  435. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  436. struct obsolete_modparm {
  437. char name[64];
  438. char type[64-sizeof(void *)];
  439. void *addr;
  440. };
  441. static inline void MODULE_PARM_(void) { }
  442. #ifdef MODULE
  443. /* DEPRECATED: Do not use. */
  444. #define MODULE_PARM(var,type) \
  445. struct obsolete_modparm __parm_##var __attribute__((section("__obsparm"))) = \
  446. { __stringify(var), type, &MODULE_PARM_ }; \
  447. __MODULE_PARM_TYPE(var, type);
  448. #else
  449. #define MODULE_PARM(var,type) static void __attribute__((__unused__)) *__parm_##var = &MODULE_PARM_;
  450. #endif
  451. #define __MODULE_STRING(x) __stringify(x)
  452. /* Use symbol_get and symbol_put instead. You'll thank me. */
  453. #define HAVE_INTER_MODULE
  454. extern void __deprecated inter_module_register(const char *,
  455. struct module *, const void *);
  456. extern void __deprecated inter_module_unregister(const char *);
  457. extern const void * __deprecated inter_module_get_request(const char *,
  458. const char *);
  459. extern void __deprecated inter_module_put(const char *);
  460. #endif /* _LINUX_MODULE_H */