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 <[email protected]> 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/marker.h> 19 #include <linux/tracepoint.h> 20 21 #include <asm/local.h> 22 #include <asm/module.h> 23 24 #include <trace/events/module.h> 25 26 /* Not Yet Implemented */ 27 #define MODULE_SUPPORTED_DEVICE(name) 28 29 /* some toolchains uses a `_' prefix for all user symbols */ 30 #ifndef MODULE_SYMBOL_PREFIX 31 #define MODULE_SYMBOL_PREFIX "" 32 #endif 33 34 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN 35 36 struct kernel_symbol 37 { 38 unsigned long value; 39 const char *name; 40 }; 41 42 struct modversion_info 43 { 44 unsigned long crc; 45 char name[MODULE_NAME_LEN]; 46 }; 47 48 struct module; 49 50 struct module_attribute { 51 struct attribute attr; 52 ssize_t (*show)(struct module_attribute *, struct module *, char *); 53 ssize_t (*store)(struct module_attribute *, struct module *, 54 const char *, size_t count); 55 void (*setup)(struct module *, const char *); 56 int (*test)(struct module *); 57 void (*free)(struct module *); 58 }; 59 60 struct module_kobject 61 { 62 struct kobject kobj; 63 struct module *mod; 64 struct kobject *drivers_dir; 65 struct module_param_attrs *mp; 66 }; 67 68 /* These are either module local, or the kernel's dummy ones. */ 69 extern int init_module(void); 70 extern void cleanup_module(void); 71 72 /* Archs provide a method of finding the correct exception table. */ 73 struct exception_table_entry; 74 75 const struct exception_table_entry * 76 search_extable(const struct exception_table_entry *first, 77 const struct exception_table_entry *last, 78 unsigned long value); 79 void sort_extable(struct exception_table_entry *start, 80 struct exception_table_entry *finish); 81 void sort_main_extable(void); 82 void trim_init_extable(struct module *m); 83 84 #ifdef MODULE 85 #define MODULE_GENERIC_TABLE(gtype,name) \ 86 extern const struct gtype##_id __mod_##gtype##_table \ 87 __attribute__ ((unused, alias(__stringify(name)))) 88 89 extern struct module __this_module; 90 #define THIS_MODULE (&__this_module) 91 #else /* !MODULE */ 92 #define MODULE_GENERIC_TABLE(gtype,name) 93 #define THIS_MODULE ((struct module *)0) 94 #endif 95 96 /* Generic info of form tag = "info" */ 97 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info) 98 99 /* For userspace: you can also call me... */ 100 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias) 101 102 /* 103 * The following license idents are currently accepted as indicating free 104 * software modules 105 * 106 * "GPL" [GNU Public License v2 or later] 107 * "GPL v2" [GNU Public License v2] 108 * "GPL and additional rights" [GNU Public License v2 rights and more] 109 * "Dual BSD/GPL" [GNU Public License v2 110 * or BSD license choice] 111 * "Dual MIT/GPL" [GNU Public License v2 112 * or MIT license choice] 113 * "Dual MPL/GPL" [GNU Public License v2 114 * or Mozilla license choice] 115 * 116 * The following other idents are available 117 * 118 * "Proprietary" [Non free products] 119 * 120 * There are dual licensed components, but when running with Linux it is the 121 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL 122 * is a GPL combined work. 123 * 124 * This exists for several reasons 125 * 1. So modinfo can show license info for users wanting to vet their setup 126 * is free 127 * 2. So the community can ignore bug reports including proprietary modules 128 * 3. So vendors can do likewise based on their own policies 129 */ 130 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license) 131 132 /* Author, ideally of form NAME[, NAME]*[ and NAME] */ 133 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author) 134 135 /* What your module does. */ 136 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description) 137 138 /* One for each parameter, describing how to use it. Some files do 139 multiple of these per line, so can't just use MODULE_INFO. */ 140 #define MODULE_PARM_DESC(_parm, desc) \ 141 __MODULE_INFO(parm, _parm, #_parm ":" desc) 142 143 #define MODULE_DEVICE_TABLE(type,name) \ 144 MODULE_GENERIC_TABLE(type##_device,name) 145 146 /* Version of form [<epoch>:]<version>[-<extra-version>]. 147 Or for CVS/RCS ID version, everything but the number is stripped. 148 <epoch>: A (small) unsigned integer which allows you to start versions 149 anew. If not mentioned, it's zero. eg. "2:1.0" is after 150 "1:2.0". 151 <version>: The <version> may contain only alphanumerics and the 152 character `.'. Ordered by numeric sort for numeric parts, 153 ascii sort for ascii parts (as per RPM or DEB algorithm). 154 <extraversion>: Like <version>, but inserted for local 155 customizations, eg "rh3" or "rusty1". 156 157 Using this automatically adds a checksum of the .c files and the 158 local headers in "srcversion". 159 */ 160 #define MODULE_VERSION(_version) MODULE_INFO(version, _version) 161 162 /* Optional firmware file (or files) needed by the module 163 * format is simply firmware file name. Multiple firmware 164 * files require multiple MODULE_FIRMWARE() specifiers */ 165 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware) 166 167 /* Given an address, look for it in the exception tables */ 168 const struct exception_table_entry *search_exception_tables(unsigned long add); 169 170 struct notifier_block; 171 172 #ifdef CONFIG_MODULES 173 174 /* Get/put a kernel symbol (calls must be symmetric) */ 175 void *__symbol_get(const char *symbol); 176 void *__symbol_get_gpl(const char *symbol); 177 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x))) 178 179 #ifndef __GENKSYMS__ 180 #ifdef CONFIG_MODVERSIONS 181 /* Mark the CRC weak since genksyms apparently decides not to 182 * generate a checksums for some symbols */ 183 #define __CRC_SYMBOL(sym, sec) \ 184 extern void *__crc_##sym __attribute__((weak)); \ 185 static const unsigned long __kcrctab_##sym \ 186 __used \ 187 __attribute__((section("__kcrctab" sec), unused)) \ 188 = (unsigned long) &__crc_##sym; 189 #else 190 #define __CRC_SYMBOL(sym, sec) 191 #endif 192 193 /* For every exported symbol, place a struct in the __ksymtab section */ 194 #define __EXPORT_SYMBOL(sym, sec) \ 195 extern typeof(sym) sym; \ 196 __CRC_SYMBOL(sym, sec) \ 197 static const char __kstrtab_##sym[] \ 198 __attribute__((section("__ksymtab_strings"), aligned(1))) \ 199 = MODULE_SYMBOL_PREFIX #sym; \ 200 static const struct kernel_symbol __ksymtab_##sym \ 201 __used \ 202 __attribute__((section("__ksymtab" sec), unused)) \ 203 = { (unsigned long)&sym, __kstrtab_##sym } 204 205 #define EXPORT_SYMBOL(sym) \ 206 __EXPORT_SYMBOL(sym, "") 207 208 #define EXPORT_SYMBOL_GPL(sym) \ 209 __EXPORT_SYMBOL(sym, "_gpl") 210 211 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \ 212 __EXPORT_SYMBOL(sym, "_gpl_future") 213 214 215 #ifdef CONFIG_UNUSED_SYMBOLS 216 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused") 217 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl") 218 #else 219 #define EXPORT_UNUSED_SYMBOL(sym) 220 #define EXPORT_UNUSED_SYMBOL_GPL(sym) 221 #endif 222 223 #endif 224 225 enum module_state 226 { 227 MODULE_STATE_LIVE, 228 MODULE_STATE_COMING, 229 MODULE_STATE_GOING, 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 /* symbols that will be GPL-only in the near future. */ 276 const struct kernel_symbol *gpl_future_syms; 277 const unsigned long *gpl_future_crcs; 278 unsigned int num_gpl_future_syms; 279 280 /* Exception table */ 281 unsigned int num_exentries; 282 struct exception_table_entry *extable; 283 284 /* Startup function. */ 285 int (*init)(void); 286 287 /* If this is non-NULL, vfree after init() returns */ 288 void *module_init; 289 290 /* Here is the actual code + data, vfree'd on unload. */ 291 void *module_core; 292 293 /* Here are the sizes of the init and core sections */ 294 unsigned int init_size, core_size; 295 296 /* The size of the executable code in each section. */ 297 unsigned int init_text_size, core_text_size; 298 299 /* Arch-specific module values */ 300 struct mod_arch_specific arch; 301 302 unsigned int taints; /* same bits as kernel:tainted */ 303 304 #ifdef CONFIG_GENERIC_BUG 305 /* Support for BUG */ 306 unsigned num_bugs; 307 struct list_head bug_list; 308 struct bug_entry *bug_table; 309 #endif 310 311 #ifdef CONFIG_KALLSYMS 312 /* We keep the symbol and string tables for kallsyms. */ 313 Elf_Sym *symtab; 314 unsigned int num_symtab; 315 char *strtab; 316 317 /* Section attributes */ 318 struct module_sect_attrs *sect_attrs; 319 320 /* Notes attributes */ 321 struct module_notes_attrs *notes_attrs; 322 #endif 323 324 /* Per-cpu data. */ 325 void *percpu; 326 327 /* The command line arguments (may be mangled). People like 328 keeping pointers to this stuff */ 329 char *args; 330 #ifdef CONFIG_MARKERS 331 struct marker *markers; 332 unsigned int num_markers; 333 #endif 334 #ifdef CONFIG_TRACEPOINTS 335 struct tracepoint *tracepoints; 336 unsigned int num_tracepoints; 337 #endif 338 339 #ifdef CONFIG_TRACING 340 const char **trace_bprintk_fmt_start; 341 unsigned int num_trace_bprintk_fmt; 342 #endif 343 #ifdef CONFIG_EVENT_TRACING 344 struct ftrace_event_call *trace_events; 345 unsigned int num_trace_events; 346 #endif 347 #ifdef CONFIG_FTRACE_MCOUNT_RECORD 348 unsigned long *ftrace_callsites; 349 unsigned int num_ftrace_callsites; 350 #endif 351 352 #ifdef CONFIG_MODULE_UNLOAD 353 /* What modules depend on me? */ 354 struct list_head modules_which_use_me; 355 356 /* Who is waiting for us to be unloaded */ 357 struct task_struct *waiter; 358 359 /* Destruction function. */ 360 void (*exit)(void); 361 362 #ifdef CONFIG_SMP 363 char *refptr; 364 #else 365 local_t ref; 366 #endif 367 #endif 368 369 #ifdef CONFIG_CONSTRUCTORS 370 /* Constructor functions. */ 371 ctor_fn_t *ctors; 372 unsigned int num_ctors; 373 #endif 374 }; 375 #ifndef MODULE_ARCH_INIT 376 #define MODULE_ARCH_INIT {} 377 #endif 378 379 extern struct mutex module_mutex; 380 381 /* FIXME: It'd be nice to isolate modules during init, too, so they 382 aren't used before they (may) fail. But presently too much code 383 (IDE & SCSI) require entry into the module during init.*/ 384 static inline int module_is_live(struct module *mod) 385 { 386 return mod->state != MODULE_STATE_GOING; 387 } 388 389 struct module *__module_text_address(unsigned long addr); 390 struct module *__module_address(unsigned long addr); 391 bool is_module_address(unsigned long addr); 392 bool is_module_text_address(unsigned long addr); 393 394 static inline int within_module_core(unsigned long addr, struct module *mod) 395 { 396 return (unsigned long)mod->module_core <= addr && 397 addr < (unsigned long)mod->module_core + mod->core_size; 398 } 399 400 static inline int within_module_init(unsigned long addr, struct module *mod) 401 { 402 return (unsigned long)mod->module_init <= addr && 403 addr < (unsigned long)mod->module_init + mod->init_size; 404 } 405 406 /* Search for module by name: must hold module_mutex. */ 407 struct module *find_module(const char *name); 408 409 struct symsearch { 410 const struct kernel_symbol *start, *stop; 411 const unsigned long *crcs; 412 enum { 413 NOT_GPL_ONLY, 414 GPL_ONLY, 415 WILL_BE_GPL_ONLY, 416 } licence; 417 bool unused; 418 }; 419 420 /* Search for an exported symbol by name. */ 421 const struct kernel_symbol *find_symbol(const char *name, 422 struct module **owner, 423 const unsigned long **crc, 424 bool gplok, 425 bool warn); 426 427 /* Walk the exported symbol table */ 428 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner, 429 unsigned int symnum, void *data), void *data); 430 431 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if 432 symnum out of range. */ 433 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 434 char *name, char *module_name, int *exported); 435 436 /* Look for this name: can be of form module:name. */ 437 unsigned long module_kallsyms_lookup_name(const char *name); 438 439 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, 440 struct module *, unsigned long), 441 void *data); 442 443 extern void __module_put_and_exit(struct module *mod, long code) 444 __attribute__((noreturn)); 445 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code); 446 447 #ifdef CONFIG_MODULE_UNLOAD 448 unsigned int module_refcount(struct module *mod); 449 void __symbol_put(const char *symbol); 450 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x) 451 void symbol_put_addr(void *addr); 452 453 static inline local_t *__module_ref_addr(struct module *mod, int cpu) 454 { 455 #ifdef CONFIG_SMP 456 return (local_t *) (mod->refptr + per_cpu_offset(cpu)); 457 #else 458 return &mod->ref; 459 #endif 460 } 461 462 /* Sometimes we know we already have a refcount, and it's easier not 463 to handle the error case (which only happens with rmmod --wait). */ 464 static inline void __module_get(struct module *module) 465 { 466 if (module) { 467 unsigned int cpu = get_cpu(); 468 local_inc(__module_ref_addr(module, cpu)); 469 trace_module_get(module, _THIS_IP_, 470 local_read(__module_ref_addr(module, cpu))); 471 put_cpu(); 472 } 473 } 474 475 static inline int try_module_get(struct module *module) 476 { 477 int ret = 1; 478 479 if (module) { 480 unsigned int cpu = get_cpu(); 481 if (likely(module_is_live(module))) { 482 local_inc(__module_ref_addr(module, cpu)); 483 trace_module_get(module, _THIS_IP_, 484 local_read(__module_ref_addr(module, cpu))); 485 } 486 else 487 ret = 0; 488 put_cpu(); 489 } 490 return ret; 491 } 492 493 extern void module_put(struct module *module); 494 495 #else /*!CONFIG_MODULE_UNLOAD*/ 496 static inline int try_module_get(struct module *module) 497 { 498 return !module || module_is_live(module); 499 } 500 static inline void module_put(struct module *module) 501 { 502 } 503 static inline void __module_get(struct module *module) 504 { 505 } 506 #define symbol_put(x) do { } while(0) 507 #define symbol_put_addr(p) do { } while(0) 508 509 #endif /* CONFIG_MODULE_UNLOAD */ 510 int use_module(struct module *a, struct module *b); 511 512 /* This is a #define so the string doesn't get put in every .o file */ 513 #define module_name(mod) \ 514 ({ \ 515 struct module *__mod = (mod); \ 516 __mod ? __mod->name : "kernel"; \ 517 }) 518 519 /* For kallsyms to ask for address resolution. namebuf should be at 520 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if 521 * found, otherwise NULL. */ 522 const char *module_address_lookup(unsigned long addr, 523 unsigned long *symbolsize, 524 unsigned long *offset, 525 char **modname, 526 char *namebuf); 527 int lookup_module_symbol_name(unsigned long addr, char *symname); 528 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name); 529 530 /* For extable.c to search modules' exception tables. */ 531 const struct exception_table_entry *search_module_extables(unsigned long addr); 532 533 int register_module_notifier(struct notifier_block * nb); 534 int unregister_module_notifier(struct notifier_block * nb); 535 536 extern void print_modules(void); 537 538 extern void module_update_markers(void); 539 540 extern void module_update_tracepoints(void); 541 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter); 542 543 #else /* !CONFIG_MODULES... */ 544 #define EXPORT_SYMBOL(sym) 545 #define EXPORT_SYMBOL_GPL(sym) 546 #define EXPORT_SYMBOL_GPL_FUTURE(sym) 547 #define EXPORT_UNUSED_SYMBOL(sym) 548 #define EXPORT_UNUSED_SYMBOL_GPL(sym) 549 550 /* Given an address, look for it in the exception tables. */ 551 static inline const struct exception_table_entry * 552 search_module_extables(unsigned long addr) 553 { 554 return NULL; 555 } 556 557 static inline struct module *__module_address(unsigned long addr) 558 { 559 return NULL; 560 } 561 562 static inline struct module *__module_text_address(unsigned long addr) 563 { 564 return NULL; 565 } 566 567 static inline bool is_module_address(unsigned long addr) 568 { 569 return false; 570 } 571 572 static inline bool is_module_text_address(unsigned long addr) 573 { 574 return false; 575 } 576 577 /* Get/put a kernel symbol (calls should be symmetric) */ 578 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); }) 579 #define symbol_put(x) do { } while(0) 580 #define symbol_put_addr(x) do { } while(0) 581 582 static inline void __module_get(struct module *module) 583 { 584 } 585 586 static inline int try_module_get(struct module *module) 587 { 588 return 1; 589 } 590 591 static inline void module_put(struct module *module) 592 { 593 } 594 595 #define module_name(mod) "kernel" 596 597 /* For kallsyms to ask for address resolution. NULL means not found. */ 598 static inline const char *module_address_lookup(unsigned long addr, 599 unsigned long *symbolsize, 600 unsigned long *offset, 601 char **modname, 602 char *namebuf) 603 { 604 return NULL; 605 } 606 607 static inline int lookup_module_symbol_name(unsigned long addr, char *symname) 608 { 609 return -ERANGE; 610 } 611 612 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name) 613 { 614 return -ERANGE; 615 } 616 617 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value, 618 char *type, char *name, 619 char *module_name, int *exported) 620 { 621 return -ERANGE; 622 } 623 624 static inline unsigned long module_kallsyms_lookup_name(const char *name) 625 { 626 return 0; 627 } 628 629 static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, 630 struct module *, 631 unsigned long), 632 void *data) 633 { 634 return 0; 635 } 636 637 static inline int register_module_notifier(struct notifier_block * nb) 638 { 639 /* no events will happen anyway, so this can always succeed */ 640 return 0; 641 } 642 643 static inline int unregister_module_notifier(struct notifier_block * nb) 644 { 645 return 0; 646 } 647 648 #define module_put_and_exit(code) do_exit(code) 649 650 static inline void print_modules(void) 651 { 652 } 653 654 static inline void module_update_markers(void) 655 { 656 } 657 658 static inline void module_update_tracepoints(void) 659 { 660 } 661 662 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter) 663 { 664 return 0; 665 } 666 667 #endif /* CONFIG_MODULES */ 668 669 struct device_driver; 670 #ifdef CONFIG_SYSFS 671 struct module; 672 673 extern struct kset *module_kset; 674 extern struct kobj_type module_ktype; 675 extern int module_sysfs_initialized; 676 677 int mod_sysfs_init(struct module *mod); 678 int mod_sysfs_setup(struct module *mod, 679 struct kernel_param *kparam, 680 unsigned int num_params); 681 int module_add_modinfo_attrs(struct module *mod); 682 void module_remove_modinfo_attrs(struct module *mod); 683 684 #else /* !CONFIG_SYSFS */ 685 686 static inline int mod_sysfs_init(struct module *mod) 687 { 688 return 0; 689 } 690 691 static inline int mod_sysfs_setup(struct module *mod, 692 struct kernel_param *kparam, 693 unsigned int num_params) 694 { 695 return 0; 696 } 697 698 static inline int module_add_modinfo_attrs(struct module *mod) 699 { 700 return 0; 701 } 702 703 static inline void module_remove_modinfo_attrs(struct module *mod) 704 { } 705 706 #endif /* CONFIG_SYSFS */ 707 708 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) 709 710 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */ 711 712 #define __MODULE_STRING(x) __stringify(x) 713 714 715 #ifdef CONFIG_GENERIC_BUG 716 int module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *, 717 struct module *); 718 void module_bug_cleanup(struct module *); 719 720 #else /* !CONFIG_GENERIC_BUG */ 721 722 static inline int module_bug_finalize(const Elf_Ehdr *hdr, 723 const Elf_Shdr *sechdrs, 724 struct module *mod) 725 { 726 return 0; 727 } 728 static inline void module_bug_cleanup(struct module *mod) {} 729 #endif /* CONFIG_GENERIC_BUG */ 730 731 #endif /* _LINUX_MODULE_H */ 732