module.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. void (*setup)(struct module *, const char *);
  47. int (*test)(struct module *);
  48. void (*free)(struct module *);
  49. };
  50. struct module_kobject
  51. {
  52. struct kobject kobj;
  53. struct module *mod;
  54. };
  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. extern struct subsystem module_subsys;
  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 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. /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
  110. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  111. /* What your module does. */
  112. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  113. /* One for each parameter, describing how to use it. Some files do
  114. multiple of these per line, so can't just use MODULE_INFO. */
  115. #define MODULE_PARM_DESC(_parm, desc) \
  116. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  117. #define MODULE_DEVICE_TABLE(type,name) \
  118. MODULE_GENERIC_TABLE(type##_device,name)
  119. /* Version of form [<epoch>:]<version>[-<extra-version>].
  120. Or for CVS/RCS ID version, everything but the number is stripped.
  121. <epoch>: A (small) unsigned integer which allows you to start versions
  122. anew. If not mentioned, it's zero. eg. "2:1.0" is after
  123. "1:2.0".
  124. <version>: The <version> may contain only alphanumerics and the
  125. character `.'. Ordered by numeric sort for numeric parts,
  126. ascii sort for ascii parts (as per RPM or DEB algorithm).
  127. <extraversion>: Like <version>, but inserted for local
  128. customizations, eg "rh3" or "rusty1".
  129. Using this automatically adds a checksum of the .c files and the
  130. local headers in "srcversion".
  131. */
  132. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  133. /* Given an address, look for it in the exception tables */
  134. const struct exception_table_entry *search_exception_tables(unsigned long add);
  135. struct notifier_block;
  136. #ifdef CONFIG_MODULES
  137. /* Get/put a kernel symbol (calls must be symmetric) */
  138. void *__symbol_get(const char *symbol);
  139. void *__symbol_get_gpl(const char *symbol);
  140. #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
  141. #ifndef __GENKSYMS__
  142. #ifdef CONFIG_MODVERSIONS
  143. /* Mark the CRC weak since genksyms apparently decides not to
  144. * generate a checksums for some symbols */
  145. #define __CRC_SYMBOL(sym, sec) \
  146. extern void *__crc_##sym __attribute__((weak)); \
  147. static const unsigned long __kcrctab_##sym \
  148. __attribute_used__ \
  149. __attribute__((section("__kcrctab" sec), unused)) \
  150. = (unsigned long) &__crc_##sym;
  151. #else
  152. #define __CRC_SYMBOL(sym, sec)
  153. #endif
  154. /* For every exported symbol, place a struct in the __ksymtab section */
  155. #define __EXPORT_SYMBOL(sym, sec) \
  156. __CRC_SYMBOL(sym, sec) \
  157. static const char __kstrtab_##sym[] \
  158. __attribute__((section("__ksymtab_strings"))) \
  159. = MODULE_SYMBOL_PREFIX #sym; \
  160. static const struct kernel_symbol __ksymtab_##sym \
  161. __attribute_used__ \
  162. __attribute__((section("__ksymtab" sec), unused)) \
  163. = { (unsigned long)&sym, __kstrtab_##sym }
  164. #define EXPORT_SYMBOL(sym) \
  165. __EXPORT_SYMBOL(sym, "")
  166. #define EXPORT_SYMBOL_GPL(sym) \
  167. __EXPORT_SYMBOL(sym, "_gpl")
  168. #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
  169. __EXPORT_SYMBOL(sym, "_gpl_future")
  170. #endif
  171. struct module_ref
  172. {
  173. local_t count;
  174. } ____cacheline_aligned;
  175. enum module_state
  176. {
  177. MODULE_STATE_LIVE,
  178. MODULE_STATE_COMING,
  179. MODULE_STATE_GOING,
  180. };
  181. /* Similar stuff for section attributes. */
  182. #define MODULE_SECT_NAME_LEN 32
  183. struct module_sect_attr
  184. {
  185. struct module_attribute mattr;
  186. char name[MODULE_SECT_NAME_LEN];
  187. unsigned long address;
  188. };
  189. struct module_sect_attrs
  190. {
  191. struct attribute_group grp;
  192. struct module_sect_attr attrs[0];
  193. };
  194. struct module_param_attrs;
  195. struct module
  196. {
  197. enum module_state state;
  198. /* Member of list of modules */
  199. struct list_head list;
  200. /* Unique handle for this module */
  201. char name[MODULE_NAME_LEN];
  202. /* Sysfs stuff. */
  203. struct module_kobject mkobj;
  204. struct module_param_attrs *param_attrs;
  205. struct module_attribute *modinfo_attrs;
  206. const char *version;
  207. const char *srcversion;
  208. /* Exported symbols */
  209. const struct kernel_symbol *syms;
  210. unsigned int num_syms;
  211. const unsigned long *crcs;
  212. /* GPL-only exported symbols. */
  213. const struct kernel_symbol *gpl_syms;
  214. unsigned int num_gpl_syms;
  215. const unsigned long *gpl_crcs;
  216. /* symbols that will be GPL-only in the near future. */
  217. const struct kernel_symbol *gpl_future_syms;
  218. unsigned int num_gpl_future_syms;
  219. const unsigned long *gpl_future_crcs;
  220. /* Exception table */
  221. unsigned int num_exentries;
  222. const 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 long init_size, core_size;
  231. /* The size of the executable code in each section. */
  232. unsigned long init_text_size, core_text_size;
  233. /* Arch-specific module values */
  234. struct mod_arch_specific arch;
  235. /* Am I unsafe to unload? */
  236. int unsafe;
  237. /* Am I GPL-compatible */
  238. int license_gplok;
  239. #ifdef CONFIG_MODULE_UNLOAD
  240. /* Reference counts */
  241. struct module_ref ref[NR_CPUS];
  242. /* What modules depend on me? */
  243. struct list_head modules_which_use_me;
  244. /* Who is waiting for us to be unloaded */
  245. struct task_struct *waiter;
  246. /* Destruction function. */
  247. void (*exit)(void);
  248. #endif
  249. #ifdef CONFIG_KALLSYMS
  250. /* We keep the symbol and string tables for kallsyms. */
  251. Elf_Sym *symtab;
  252. unsigned long num_symtab;
  253. char *strtab;
  254. /* Section attributes */
  255. struct module_sect_attrs *sect_attrs;
  256. #endif
  257. /* Per-cpu data. */
  258. void *percpu;
  259. /* The command line arguments (may be mangled). People like
  260. keeping pointers to this stuff */
  261. char *args;
  262. };
  263. /* FIXME: It'd be nice to isolate modules during init, too, so they
  264. aren't used before they (may) fail. But presently too much code
  265. (IDE & SCSI) require entry into the module during init.*/
  266. static inline int module_is_live(struct module *mod)
  267. {
  268. return mod->state != MODULE_STATE_GOING;
  269. }
  270. /* Is this address in a module? (second is with no locks, for oops) */
  271. struct module *module_text_address(unsigned long addr);
  272. struct module *__module_text_address(unsigned long addr);
  273. /* Returns module and fills in value, defined and namebuf, or NULL if
  274. symnum out of range. */
  275. struct module *module_get_kallsym(unsigned int symnum,
  276. unsigned long *value,
  277. char *type,
  278. char namebuf[128]);
  279. /* Look for this name: can be of form module:name. */
  280. unsigned long module_kallsyms_lookup_name(const char *name);
  281. int is_exported(const char *name, const struct module *mod);
  282. extern void __module_put_and_exit(struct module *mod, long code)
  283. __attribute__((noreturn));
  284. #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
  285. #ifdef CONFIG_MODULE_UNLOAD
  286. unsigned int module_refcount(struct module *mod);
  287. void __symbol_put(const char *symbol);
  288. #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
  289. void symbol_put_addr(void *addr);
  290. /* Sometimes we know we already have a refcount, and it's easier not
  291. to handle the error case (which only happens with rmmod --wait). */
  292. static inline void __module_get(struct module *module)
  293. {
  294. if (module) {
  295. BUG_ON(module_refcount(module) == 0);
  296. local_inc(&module->ref[get_cpu()].count);
  297. put_cpu();
  298. }
  299. }
  300. static inline int try_module_get(struct module *module)
  301. {
  302. int ret = 1;
  303. if (module) {
  304. unsigned int cpu = get_cpu();
  305. if (likely(module_is_live(module)))
  306. local_inc(&module->ref[cpu].count);
  307. else
  308. ret = 0;
  309. put_cpu();
  310. }
  311. return ret;
  312. }
  313. static inline void module_put(struct module *module)
  314. {
  315. if (module) {
  316. unsigned int cpu = get_cpu();
  317. local_dec(&module->ref[cpu].count);
  318. /* Maybe they're waiting for us to drop reference? */
  319. if (unlikely(!module_is_live(module)))
  320. wake_up_process(module->waiter);
  321. put_cpu();
  322. }
  323. }
  324. #else /*!CONFIG_MODULE_UNLOAD*/
  325. static inline int try_module_get(struct module *module)
  326. {
  327. return !module || module_is_live(module);
  328. }
  329. static inline void module_put(struct module *module)
  330. {
  331. }
  332. static inline void __module_get(struct module *module)
  333. {
  334. }
  335. #define symbol_put(x) do { } while(0)
  336. #define symbol_put_addr(p) do { } while(0)
  337. #endif /* CONFIG_MODULE_UNLOAD */
  338. /* This is a #define so the string doesn't get put in every .o file */
  339. #define module_name(mod) \
  340. ({ \
  341. struct module *__mod = (mod); \
  342. __mod ? __mod->name : "kernel"; \
  343. })
  344. #define __unsafe(mod) \
  345. do { \
  346. if (mod && !(mod)->unsafe) { \
  347. printk(KERN_WARNING \
  348. "Module %s cannot be unloaded due to unsafe usage in" \
  349. " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
  350. (mod)->unsafe = 1; \
  351. } \
  352. } while(0)
  353. /* For kallsyms to ask for address resolution. NULL means not found. */
  354. const char *module_address_lookup(unsigned long addr,
  355. unsigned long *symbolsize,
  356. unsigned long *offset,
  357. char **modname);
  358. /* For extable.c to search modules' exception tables. */
  359. const struct exception_table_entry *search_module_extables(unsigned long addr);
  360. int register_module_notifier(struct notifier_block * nb);
  361. int unregister_module_notifier(struct notifier_block * nb);
  362. extern void print_modules(void);
  363. struct device_driver;
  364. void module_add_driver(struct module *, struct device_driver *);
  365. void module_remove_driver(struct device_driver *);
  366. #else /* !CONFIG_MODULES... */
  367. #define EXPORT_SYMBOL(sym)
  368. #define EXPORT_SYMBOL_GPL(sym)
  369. #define EXPORT_SYMBOL_GPL_FUTURE(sym)
  370. /* Given an address, look for it in the exception tables. */
  371. static inline const struct exception_table_entry *
  372. search_module_extables(unsigned long addr)
  373. {
  374. return NULL;
  375. }
  376. /* Is this address in a module? */
  377. static inline struct module *module_text_address(unsigned long addr)
  378. {
  379. return NULL;
  380. }
  381. /* Is this address in a module? (don't take a lock, we're oopsing) */
  382. static inline struct module *__module_text_address(unsigned long addr)
  383. {
  384. return NULL;
  385. }
  386. /* Get/put a kernel symbol (calls should be symmetric) */
  387. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
  388. #define symbol_put(x) do { } while(0)
  389. #define symbol_put_addr(x) do { } while(0)
  390. static inline void __module_get(struct module *module)
  391. {
  392. }
  393. static inline int try_module_get(struct module *module)
  394. {
  395. return 1;
  396. }
  397. static inline void module_put(struct module *module)
  398. {
  399. }
  400. #define module_name(mod) "kernel"
  401. #define __unsafe(mod)
  402. /* For kallsyms to ask for address resolution. NULL means not found. */
  403. static inline const char *module_address_lookup(unsigned long addr,
  404. unsigned long *symbolsize,
  405. unsigned long *offset,
  406. char **modname)
  407. {
  408. return NULL;
  409. }
  410. static inline struct module *module_get_kallsym(unsigned int symnum,
  411. unsigned long *value,
  412. char *type,
  413. char namebuf[128])
  414. {
  415. return NULL;
  416. }
  417. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  418. {
  419. return 0;
  420. }
  421. static inline int is_exported(const char *name, const struct module *mod)
  422. {
  423. return 0;
  424. }
  425. static inline int register_module_notifier(struct notifier_block * nb)
  426. {
  427. /* no events will happen anyway, so this can always succeed */
  428. return 0;
  429. }
  430. static inline int unregister_module_notifier(struct notifier_block * nb)
  431. {
  432. return 0;
  433. }
  434. #define module_put_and_exit(code) do_exit(code)
  435. static inline void print_modules(void)
  436. {
  437. }
  438. struct device_driver;
  439. struct module;
  440. static inline void module_add_driver(struct module *module, struct device_driver *driver)
  441. {
  442. }
  443. static inline void module_remove_driver(struct device_driver *driver)
  444. {
  445. }
  446. #endif /* CONFIG_MODULES */
  447. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  448. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  449. struct obsolete_modparm {
  450. char name[64];
  451. char type[64-sizeof(void *)];
  452. void *addr;
  453. };
  454. static inline void MODULE_PARM_(void) { }
  455. #ifdef MODULE
  456. /* DEPRECATED: Do not use. */
  457. #define MODULE_PARM(var,type) \
  458. extern struct obsolete_modparm __parm_##var \
  459. __attribute__((section("__obsparm"))); \
  460. struct obsolete_modparm __parm_##var = \
  461. { __stringify(var), type, &MODULE_PARM_ }; \
  462. __MODULE_PARM_TYPE(var, type);
  463. #else
  464. #define MODULE_PARM(var,type) static void __attribute__((__unused__)) *__parm_##var = &MODULE_PARM_;
  465. #endif
  466. #define __MODULE_STRING(x) __stringify(x)
  467. /* Use symbol_get and symbol_put instead. You'll thank me. */
  468. #define HAVE_INTER_MODULE
  469. extern void __deprecated inter_module_register(const char *,
  470. struct module *, const void *);
  471. extern void __deprecated inter_module_unregister(const char *);
  472. extern const void * __deprecated inter_module_get_request(const char *,
  473. const char *);
  474. extern void __deprecated inter_module_put(const char *);
  475. #endif /* _LINUX_MODULE_H */