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