1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/cpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/vermagic.h>
34 #include <linux/notifier.h>
35 #include <asm/uaccess.h>
36 #include <asm/semaphore.h>
37 #include <asm/pgalloc.h>
38 #include <asm/cacheflush.h>
43 #define DEBUGP(fmt , a...)
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
50 /* If this is set, the section belongs in the init part of the module */
51 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
53 #define symbol_is(literal, string) \
54 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
56 /* Protects module list */
57 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
70 err = notifier_chain_register(&module_notify_list, nb);
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
80 err = notifier_chain_unregister(&module_notify_list, nb);
84 EXPORT_SYMBOL(unregister_module_notifier);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
89 if (mod && mod->state == MODULE_STATE_COMING)
91 return try_module_get(mod);
94 /* Stub function for modules which don't have an initfn */
99 EXPORT_SYMBOL(init_module);
101 /* Find a module section: 0 means not found. */
102 static unsigned int find_sec(Elf_Ehdr *hdr,
104 const char *secstrings,
109 for (i = 1; i < hdr->e_shnum; i++)
110 /* Alloc bit cleared means "ignore it." */
111 if ((sechdrs[i].sh_flags & SHF_ALLOC)
112 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
117 /* Provided by the linker */
118 extern const struct kernel_symbol __start___ksymtab[];
119 extern const struct kernel_symbol __stop___ksymtab[];
120 extern const struct kernel_symbol __start___ksymtab_gpl[];
121 extern const struct kernel_symbol __stop___ksymtab_gpl[];
122 extern const unsigned long __start___kcrctab[];
123 extern const unsigned long __start___kcrctab_gpl[];
125 #ifndef CONFIG_MODVERSIONS
126 #define symversion(base, idx) NULL
128 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
131 /* Find a symbol, return value, crc and module which owns it */
132 static unsigned long __find_symbol(const char *name,
133 struct module **owner,
134 const unsigned long **crc,
140 /* Core kernel first. */
142 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
143 if (strcmp(__start___ksymtab[i].name, name) == 0) {
144 *crc = symversion(__start___kcrctab, i);
145 return __start___ksymtab[i].value;
149 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
150 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
151 *crc = symversion(__start___kcrctab_gpl, i);
152 return __start___ksymtab_gpl[i].value;
156 /* Now try modules. */
157 list_for_each_entry(mod, &modules, list) {
159 for (i = 0; i < mod->num_syms; i++)
160 if (strcmp(mod->syms[i].name, name) == 0) {
161 *crc = symversion(mod->crcs, i);
162 return mod->syms[i].value;
166 for (i = 0; i < mod->num_gpl_syms; i++) {
167 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
168 *crc = symversion(mod->gpl_crcs, i);
169 return mod->gpl_syms[i].value;
174 DEBUGP("Failed to find symbol %s\n", name);
178 /* Find a symbol in this elf symbol table */
179 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
180 unsigned int symindex,
185 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
187 /* Search (defined) internal symbols first. */
188 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
189 if (sym[i].st_shndx != SHN_UNDEF
190 && strcmp(name, strtab + sym[i].st_name) == 0)
191 return sym[i].st_value;
196 /* Search for module by name: must hold module_mutex. */
197 static struct module *find_module(const char *name)
201 list_for_each_entry(mod, &modules, list) {
202 if (strcmp(mod->name, name) == 0)
208 #ifdef CONFIG_MODULE_UNLOAD
209 /* Init the unload section of the module. */
210 static void module_unload_init(struct module *mod)
214 INIT_LIST_HEAD(&mod->modules_which_use_me);
215 for (i = 0; i < NR_CPUS; i++)
216 atomic_set(&mod->ref[i].count, 0);
217 /* Backwards compatibility macros put refcount during init. */
218 mod->waiter = current;
221 /* modules using other modules */
224 struct list_head list;
225 struct module *module_which_uses;
228 /* Does a already use b? */
229 static int already_uses(struct module *a, struct module *b)
231 struct module_use *use;
233 list_for_each_entry(use, &b->modules_which_use_me, list) {
234 if (use->module_which_uses == a) {
235 DEBUGP("%s uses %s!\n", a->name, b->name);
239 DEBUGP("%s does not use %s!\n", a->name, b->name);
243 /* Module a uses b */
244 static int use_module(struct module *a, struct module *b)
246 struct module_use *use;
247 if (b == NULL || already_uses(a, b)) return 1;
249 if (!strong_try_module_get(b))
252 DEBUGP("Allocating new usage for %s.\n", a->name);
253 use = kmalloc(sizeof(*use), GFP_ATOMIC);
255 printk("%s: out of memory loading\n", a->name);
260 use->module_which_uses = a;
261 list_add(&use->list, &b->modules_which_use_me);
265 /* Clear the unload stuff of the module. */
266 static void module_unload_free(struct module *mod)
270 list_for_each_entry(i, &modules, list) {
271 struct module_use *use;
273 list_for_each_entry(use, &i->modules_which_use_me, list) {
274 if (use->module_which_uses == mod) {
275 DEBUGP("%s unusing %s\n", mod->name, i->name);
277 list_del(&use->list);
279 /* There can be at most one match. */
287 /* Thread to stop each CPU in user context. */
295 static enum stopref_state stopref_state;
296 static unsigned int stopref_num_threads;
297 static atomic_t stopref_thread_ack;
299 static int stopref(void *cpu)
301 int irqs_disabled = 0;
304 sprintf(current->comm, "kmodule%lu\n", (unsigned long)cpu);
306 /* Highest priority we can manage, and move to right CPU. */
308 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
309 setscheduler(current->pid, SCHED_FIFO, ¶m);
311 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
313 /* Ack: we are alive */
314 atomic_inc(&stopref_thread_ack);
316 /* Simple state machine */
317 while (stopref_state != STOPREF_EXIT) {
318 if (stopref_state == STOPREF_DISABLE_IRQ && !irqs_disabled) {
321 /* Ack: irqs disabled. */
322 atomic_inc(&stopref_thread_ack);
323 } else if (stopref_state == STOPREF_PREPARE && !prepared) {
324 /* Everyone is in place, hold CPU. */
327 atomic_inc(&stopref_thread_ack);
329 if (irqs_disabled || prepared)
335 /* Ack: we are exiting. */
336 atomic_inc(&stopref_thread_ack);
346 /* Change the thread state */
347 static void stopref_set_state(enum stopref_state state, int sleep)
349 atomic_set(&stopref_thread_ack, 0);
351 stopref_state = state;
352 while (atomic_read(&stopref_thread_ack) != stopref_num_threads) {
360 /* Stop the machine. Disables irqs. */
361 static int stop_refcounts(void)
364 unsigned long old_allowed;
367 /* One thread per cpu. We'll do our own. */
368 cpu = smp_processor_id();
370 /* FIXME: racy with set_cpus_allowed. */
371 old_allowed = current->cpus_allowed;
372 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
374 atomic_set(&stopref_thread_ack, 0);
375 stopref_num_threads = 0;
376 stopref_state = STOPREF_WAIT;
378 /* No CPUs can come up or down during this. */
381 for (i = 0; i < NR_CPUS; i++) {
382 if (i == cpu || !cpu_online(i))
384 ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
387 stopref_num_threads++;
390 /* Wait for them all to come to life. */
391 while (atomic_read(&stopref_thread_ack) != stopref_num_threads)
394 /* If some failed, kill them all. */
396 stopref_set_state(STOPREF_EXIT, 1);
401 /* Don't schedule us away at this point, please. */
404 /* Now they are all scheduled, make them hold the CPUs, ready. */
405 stopref_set_state(STOPREF_PREPARE, 0);
407 /* Make them disable irqs. */
408 stopref_set_state(STOPREF_DISABLE_IRQ, 0);
414 /* Restart the machine. Re-enables irqs. */
415 static void restart_refcounts(void)
417 stopref_set_state(STOPREF_EXIT, 0);
423 static inline int stop_refcounts(void)
428 static inline void restart_refcounts(void)
434 unsigned int module_refcount(struct module *mod)
436 unsigned int i, total = 0;
438 for (i = 0; i < NR_CPUS; i++)
439 total += atomic_read(&mod->ref[i].count);
442 EXPORT_SYMBOL(module_refcount);
444 /* This exists whether we can unload or not */
445 static void free_module(struct module *mod);
447 #ifdef CONFIG_MODULE_FORCE_UNLOAD
448 static inline int try_force(unsigned int flags)
450 return (flags & O_TRUNC);
453 static inline int try_force(unsigned int flags)
457 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
459 /* Stub function for modules which don't have an exitfn */
460 void cleanup_module(void)
463 EXPORT_SYMBOL(cleanup_module);
466 sys_delete_module(const char __user *name_user, unsigned int flags)
469 char name[MODULE_NAME_LEN];
472 if (!capable(CAP_SYS_MODULE))
475 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
477 name[MODULE_NAME_LEN-1] = '\0';
479 if (down_interruptible(&module_mutex) != 0)
482 mod = find_module(name);
488 if (!list_empty(&mod->modules_which_use_me)) {
489 /* Other modules depend on us: get rid of them first. */
495 if (mod->state == MODULE_STATE_GOING) {
496 /* FIXME: if (force), slam module count and wake up
498 DEBUGP("%s already dying\n", mod->name);
503 /* Coming up? Allow force on stuck modules. */
504 if (mod->state == MODULE_STATE_COMING) {
505 forced = try_force(flags);
507 /* This module can't be removed */
513 /* If it has an init func, it must have an exit func to unload */
514 if ((mod->init != init_module && mod->exit == cleanup_module)
516 forced = try_force(flags);
518 /* This module can't be removed */
523 /* Stop the machine so refcounts can't move: irqs disabled. */
524 DEBUGP("Stopping refcounts...\n");
525 ret = stop_refcounts();
529 /* If it's not unused, quit unless we are told to block. */
530 if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
531 forced = try_force(flags);
535 mod->waiter = current;
536 mod->state = MODULE_STATE_GOING;
546 /* Since we might sleep for some time, drop the semaphore first */
549 DEBUGP("Looking at refcount...\n");
550 set_current_state(TASK_UNINTERRUPTIBLE);
551 if (module_refcount(mod) == 0)
555 current->state = TASK_RUNNING;
557 DEBUGP("Regrabbing mutex...\n");
561 /* Final destruction now noone is using it. */
570 static void print_unload_info(struct seq_file *m, struct module *mod)
572 struct module_use *use;
573 int printed_something = 0;
575 seq_printf(m, " %u ", module_refcount(mod));
577 /* Always include a trailing , so userspace can differentiate
578 between this and the old multi-field proc format. */
579 list_for_each_entry(use, &mod->modules_which_use_me, list) {
580 printed_something = 1;
581 seq_printf(m, "%s,", use->module_which_uses->name);
585 printed_something = 1;
586 seq_printf(m, "[unsafe],");
589 if (mod->init != init_module && mod->exit == cleanup_module) {
590 printed_something = 1;
591 seq_printf(m, "[permanent],");
594 if (!printed_something)
598 void __symbol_put(const char *symbol)
600 struct module *owner;
602 const unsigned long *crc;
604 spin_lock_irqsave(&modlist_lock, flags);
605 if (!__find_symbol(symbol, &owner, &crc, 1))
608 spin_unlock_irqrestore(&modlist_lock, flags);
610 EXPORT_SYMBOL(__symbol_put);
612 void symbol_put_addr(void *addr)
616 spin_lock_irqsave(&modlist_lock, flags);
617 if (!kernel_text_address((unsigned long)addr))
620 module_put(module_text_address((unsigned long)addr));
621 spin_unlock_irqrestore(&modlist_lock, flags);
623 EXPORT_SYMBOL_GPL(symbol_put_addr);
625 #else /* !CONFIG_MODULE_UNLOAD */
626 static void print_unload_info(struct seq_file *m, struct module *mod)
628 /* We don't know the usage count, or what modules are using. */
629 seq_printf(m, " - -");
632 static inline void module_unload_free(struct module *mod)
636 static inline int use_module(struct module *a, struct module *b)
638 return strong_try_module_get(b);
641 static inline void module_unload_init(struct module *mod)
646 sys_delete_module(const char *name_user, unsigned int flags)
651 #endif /* CONFIG_MODULE_UNLOAD */
653 #ifdef CONFIG_OBSOLETE_MODPARM
654 static int param_set_byte(const char *val, struct kernel_param *kp)
659 if (!val) return -EINVAL;
660 l = simple_strtol(val, &endp, 0);
661 if (endp == val || *endp || ((char)l != l))
663 *((char *)kp->arg) = l;
667 /* Bounds checking done below */
668 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
670 strcpy(kp->arg, val);
674 int set_obsolete(const char *val, struct kernel_param *kp)
676 unsigned int min, max;
677 unsigned int size, maxsize;
680 struct obsolete_modparm *obsparm = kp->arg;
683 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
687 /* type is: [min[-max]]{b,h,i,l,s} */
689 min = simple_strtol(p, &endp, 10);
690 if (endp == obsparm->type)
692 else if (*endp == '-') {
694 max = simple_strtol(p, &endp, 10);
699 return param_array(kp->name, val, min, max, obsparm->addr,
702 return param_array(kp->name, val, min, max, obsparm->addr,
703 sizeof(short), param_set_short);
705 return param_array(kp->name, val, min, max, obsparm->addr,
706 sizeof(int), param_set_int);
708 return param_array(kp->name, val, min, max, obsparm->addr,
709 sizeof(long), param_set_long);
711 return param_array(kp->name, val, min, max, obsparm->addr,
712 sizeof(char *), param_set_charp);
715 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
716 and the decl is "char xxx[5][50];" */
718 maxsize = simple_strtol(p, &endp, 10);
719 /* We check lengths here (yes, this is a hack). */
721 while (p[size = strcspn(p, ",")]) {
728 return param_array(kp->name, val, min, max, obsparm->addr,
729 maxsize, obsparm_copy_string);
731 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
735 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
739 static int obsolete_params(const char *name,
741 struct obsolete_modparm obsparm[],
744 unsigned int symindex,
747 struct kernel_param *kp;
751 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
755 for (i = 0; i < num; i++) {
756 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
758 snprintf(sym_name, sizeof(sym_name), "%s%s",
759 MODULE_SYMBOL_PREFIX, obsparm[i].name);
761 kp[i].name = obsparm[i].name;
763 kp[i].set = set_obsolete;
766 = (void *)find_local_symbol(sechdrs, symindex, strtab,
768 if (!obsparm[i].addr) {
769 printk("%s: falsely claims to have parameter %s\n",
770 name, obsparm[i].name);
774 kp[i].arg = &obsparm[i];
777 ret = parse_args(name, args, kp, num, NULL);
783 static int obsolete_params(const char *name,
785 struct obsolete_modparm obsparm[],
788 unsigned int symindex,
792 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
796 #endif /* CONFIG_OBSOLETE_MODPARM */
798 static const char vermagic[] = VERMAGIC_STRING;
800 #ifdef CONFIG_MODVERSIONS
801 static int check_version(Elf_Shdr *sechdrs,
802 unsigned int versindex,
805 const unsigned long *crc)
807 unsigned int i, num_versions;
808 struct modversion_info *versions;
810 /* Exporting module didn't supply crcs? OK, we're already tainted. */
814 versions = (void *) sechdrs[versindex].sh_addr;
815 num_versions = sechdrs[versindex].sh_size
816 / sizeof(struct modversion_info);
818 for (i = 0; i < num_versions; i++) {
819 if (strcmp(versions[i].name, symname) != 0)
822 if (versions[i].crc == *crc)
824 printk("%s: disagrees about version of symbol %s\n",
826 DEBUGP("Found checksum %lX vs module %lX\n",
827 *crc, versions[i].crc);
830 /* Not in module's version table. OK, but that taints the kernel. */
831 if (!(tainted & TAINT_FORCED_MODULE)) {
832 printk("%s: no version for \"%s\" found: kernel tainted.\n",
834 tainted |= TAINT_FORCED_MODULE;
839 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
840 unsigned int versindex,
843 const unsigned long *crc;
844 struct module *owner;
846 if (!__find_symbol("struct_module", &owner, &crc, 1))
848 return check_version(sechdrs, versindex, "struct_module", mod,
852 /* First part is kernel version, which we ignore. */
853 static inline int same_magic(const char *amagic, const char *bmagic)
855 amagic += strcspn(amagic, " ");
856 bmagic += strcspn(bmagic, " ");
857 return strcmp(amagic, bmagic) == 0;
860 static inline int check_version(Elf_Shdr *sechdrs,
861 unsigned int versindex,
864 const unsigned long *crc)
869 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
870 unsigned int versindex,
876 static inline int same_magic(const char *amagic, const char *bmagic)
878 return strcmp(amagic, bmagic) == 0;
880 #endif /* CONFIG_MODVERSIONS */
882 /* Resolve a symbol for this module. I.e. if we find one, record usage.
883 Must be holding module_mutex. */
884 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
885 unsigned int versindex,
889 struct module *owner;
891 const unsigned long *crc;
893 spin_lock_irq(&modlist_lock);
894 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
896 /* use_module can fail due to OOM, or module unloading */
897 if (!check_version(sechdrs, versindex, name, mod, crc) ||
898 !use_module(mod, owner))
901 spin_unlock_irq(&modlist_lock);
905 /* Free a module, remove from lists, etc (must hold module mutex). */
906 static void free_module(struct module *mod)
908 /* Delete from various lists */
909 spin_lock_irq(&modlist_lock);
910 list_del(&mod->list);
911 spin_unlock_irq(&modlist_lock);
913 /* Arch-specific cleanup. */
914 module_arch_cleanup(mod);
916 /* Module unload stuff */
917 module_unload_free(mod);
919 /* This may be NULL, but that's OK */
920 module_free(mod, mod->module_init);
923 /* Finally, free the core (containing the module structure) */
924 module_free(mod, mod->module_core);
927 void *__symbol_get(const char *symbol)
929 struct module *owner;
930 unsigned long value, flags;
931 const unsigned long *crc;
933 spin_lock_irqsave(&modlist_lock, flags);
934 value = __find_symbol(symbol, &owner, &crc, 1);
935 if (value && !strong_try_module_get(owner))
937 spin_unlock_irqrestore(&modlist_lock, flags);
939 return (void *)value;
941 EXPORT_SYMBOL_GPL(__symbol_get);
943 /* Change all symbols so that sh_value encodes the pointer directly. */
944 static int simplify_symbols(Elf_Shdr *sechdrs,
945 unsigned int symindex,
947 unsigned int versindex,
950 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
952 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
955 for (i = 1; i < n; i++) {
956 switch (sym[i].st_shndx) {
958 /* We compiled with -fno-common. These are not
959 supposed to happen. */
960 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
965 /* Don't need to do anything */
966 DEBUGP("Absolute symbol: 0x%08lx\n",
967 (long)sym[i].st_value);
972 = resolve_symbol(sechdrs, versindex,
973 strtab + sym[i].st_name, mod);
975 /* Ok if resolved. */
976 if (sym[i].st_value != 0)
979 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
982 printk(KERN_WARNING "%s: Unknown symbol %s\n",
983 mod->name, strtab + sym[i].st_name);
990 (sechdrs[sym[i].st_shndx].sh_addr
999 /* Update size with this section: return offset. */
1000 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1004 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1005 *size = ret + sechdr->sh_size;
1009 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1010 might -- code, read-only data, read-write data, small data. Tally
1011 sizes, and place the offsets into sh_entsize fields: high bit means it
1013 static void layout_sections(struct module *mod,
1014 const Elf_Ehdr *hdr,
1016 const char *secstrings)
1018 static unsigned long const masks[][2] = {
1019 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1020 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1021 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1022 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1026 for (i = 0; i < hdr->e_shnum; i++)
1027 sechdrs[i].sh_entsize = ~0UL;
1029 DEBUGP("Core section allocation order:\n");
1030 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1031 for (i = 0; i < hdr->e_shnum; ++i) {
1032 Elf_Shdr *s = &sechdrs[i];
1034 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1035 || (s->sh_flags & masks[m][1])
1036 || s->sh_entsize != ~0UL
1037 || strstr(secstrings + s->sh_name, ".init"))
1039 s->sh_entsize = get_offset(&mod->core_size, s);
1040 DEBUGP("\t%s\n", secstrings + s->sh_name);
1044 DEBUGP("Init section allocation order:\n");
1045 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1046 for (i = 0; i < hdr->e_shnum; ++i) {
1047 Elf_Shdr *s = &sechdrs[i];
1049 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1050 || (s->sh_flags & masks[m][1])
1051 || s->sh_entsize != ~0UL
1052 || !strstr(secstrings + s->sh_name, ".init"))
1054 s->sh_entsize = (get_offset(&mod->init_size, s)
1055 | INIT_OFFSET_MASK);
1056 DEBUGP("\t%s\n", secstrings + s->sh_name);
1061 static inline int license_is_gpl_compatible(const char *license)
1063 return (strcmp(license, "GPL") == 0
1064 || strcmp(license, "GPL v2") == 0
1065 || strcmp(license, "GPL and additional rights") == 0
1066 || strcmp(license, "Dual BSD/GPL") == 0
1067 || strcmp(license, "Dual MPL/GPL") == 0);
1070 static void set_license(struct module *mod, const char *license)
1073 license = "unspecified";
1075 mod->license_gplok = license_is_gpl_compatible(license);
1076 if (!mod->license_gplok) {
1077 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1078 mod->name, license);
1079 tainted |= TAINT_PROPRIETARY_MODULE;
1083 /* Parse tag=value strings from .modinfo section */
1084 static char *next_string(char *string, unsigned long *secsize)
1086 /* Skip non-zero chars */
1089 if ((*secsize)-- <= 1)
1093 /* Skip any zero padding. */
1094 while (!string[0]) {
1096 if ((*secsize)-- <= 1)
1102 static char *get_modinfo(Elf_Shdr *sechdrs,
1107 unsigned int taglen = strlen(tag);
1108 unsigned long size = sechdrs[info].sh_size;
1110 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1111 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1112 return p + taglen + 1;
1117 /* Allocate and load the module: note that size of section 0 is always
1118 zero, and we rely on this for optional sections. */
1119 static struct module *load_module(void __user *umod,
1121 const char __user *uargs)
1125 char *secstrings, *args, *modmagic, *strtab = NULL;
1126 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1127 exportindex, modindex, obsparmindex, infoindex, gplindex,
1128 crcindex, gplcrcindex, versindex;
1132 void *ptr = NULL; /* Stops spurious gcc uninitialized warning */
1134 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1136 if (len < sizeof(*hdr))
1137 return ERR_PTR(-ENOEXEC);
1139 /* Suck in entire file: we'll want most of it. */
1140 /* vmalloc barfs on "unusual" numbers. Check here */
1141 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1142 return ERR_PTR(-ENOMEM);
1143 if (copy_from_user(hdr, umod, len) != 0) {
1148 /* Sanity checks against insmoding binaries or wrong arch,
1149 weird elf version */
1150 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1151 || hdr->e_type != ET_REL
1152 || !elf_check_arch(hdr)
1153 || hdr->e_shentsize != sizeof(*sechdrs)) {
1158 /* Convenience variables */
1159 sechdrs = (void *)hdr + hdr->e_shoff;
1160 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1161 sechdrs[0].sh_addr = 0;
1163 /* And these should exist, but gcc whinges if we don't init them */
1164 symindex = strindex = 0;
1166 for (i = 1; i < hdr->e_shnum; i++) {
1167 /* Mark all sections sh_addr with their address in the
1169 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1171 /* Internal symbols and strings. */
1172 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1174 strindex = sechdrs[i].sh_link;
1175 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1177 #ifndef CONFIG_MODULE_UNLOAD
1178 /* Don't load .exit sections */
1179 if (strstr(secstrings+sechdrs[i].sh_name, ".exit"))
1180 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1184 modindex = find_sec(hdr, sechdrs, secstrings,
1185 ".gnu.linkonce.this_module");
1187 printk(KERN_WARNING "No module found in object\n");
1191 mod = (void *)sechdrs[modindex].sh_addr;
1193 /* Optional sections */
1194 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1195 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1196 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1197 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1198 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1199 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1200 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1201 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1202 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1204 /* Don't keep modinfo section */
1205 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1206 #ifdef CONFIG_KALLSYMS
1207 /* Keep symbol and string tables for decoding later. */
1208 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1209 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1212 /* Check module struct version now, before we try to use module. */
1213 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1218 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1219 /* This is allowed: modprobe --force will invalidate it. */
1221 tainted |= TAINT_FORCED_MODULE;
1222 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1224 } else if (!same_magic(modmagic, vermagic)) {
1225 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1226 mod->name, modmagic, vermagic);
1231 /* Now copy in args */
1232 arglen = strlen_user(uargs);
1237 args = kmalloc(arglen, GFP_KERNEL);
1242 if (copy_from_user(args, uargs, arglen) != 0) {
1247 if (find_module(mod->name)) {
1252 mod->state = MODULE_STATE_COMING;
1254 /* Allow arches to frob section contents and sizes. */
1255 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1259 /* Determine total sizes, and put offsets in sh_entsize. For now
1260 this is done generically; there doesn't appear to be any
1261 special cases for the architectures. */
1262 layout_sections(mod, hdr, sechdrs, secstrings);
1264 /* Do the allocs. */
1265 ptr = module_alloc(mod->core_size);
1270 memset(ptr, 0, mod->core_size);
1271 mod->module_core = ptr;
1273 ptr = module_alloc(mod->init_size);
1274 if (!ptr && mod->init_size) {
1278 memset(ptr, 0, mod->init_size);
1279 mod->module_init = ptr;
1281 /* Transfer each section which specifies SHF_ALLOC */
1282 DEBUGP("final section addresses:\n");
1283 for (i = 0; i < hdr->e_shnum; i++) {
1286 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1289 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1290 dest = mod->module_init
1291 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1293 dest = mod->module_core + sechdrs[i].sh_entsize;
1295 if (sechdrs[i].sh_type != SHT_NOBITS)
1296 memcpy(dest, (void *)sechdrs[i].sh_addr,
1297 sechdrs[i].sh_size);
1298 /* Update sh_addr to point to copy in image. */
1299 sechdrs[i].sh_addr = (unsigned long)dest;
1300 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1302 /* Module has been moved. */
1303 mod = (void *)sechdrs[modindex].sh_addr;
1305 /* Now we've moved module, initialize linked lists, etc. */
1306 module_unload_init(mod);
1308 /* Set up license info based on the info section */
1309 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1311 /* Fix up syms, so that st_value is a pointer to location. */
1312 err = simplify_symbols(sechdrs, symindex, strtab, versindex, mod);
1316 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1317 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1318 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1320 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1321 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1322 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1324 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1326 #ifdef CONFIG_MODVERSIONS
1327 if ((mod->num_syms && !crcindex) ||
1328 (mod->num_gpl_syms && !gplcrcindex)) {
1329 printk(KERN_WARNING "%s: No versions for exported symbols."
1330 " Tainting kernel.\n", mod->name);
1331 tainted |= TAINT_FORCED_MODULE;
1335 /* Set up exception table */
1336 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1337 mod->extable = (void *)sechdrs[exindex].sh_addr;
1339 /* Now do relocations. */
1340 for (i = 1; i < hdr->e_shnum; i++) {
1341 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1342 if (sechdrs[i].sh_type == SHT_REL)
1343 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1344 else if (sechdrs[i].sh_type == SHT_RELA)
1345 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1351 #ifdef CONFIG_KALLSYMS
1352 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1353 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1354 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1356 err = module_finalize(hdr, sechdrs, mod);
1362 err = obsolete_params(mod->name, mod->args,
1363 (struct obsolete_modparm *)
1364 sechdrs[obsparmindex].sh_addr,
1365 sechdrs[obsparmindex].sh_size
1366 / sizeof(struct obsolete_modparm),
1368 (char *)sechdrs[strindex].sh_addr);
1370 /* Size of section 0 is 0, so this works well if no params */
1371 err = parse_args(mod->name, mod->args,
1372 (struct kernel_param *)
1373 sechdrs[setupindex].sh_addr,
1374 sechdrs[setupindex].sh_size
1375 / sizeof(struct kernel_param),
1381 /* Get rid of temporary copy */
1388 module_unload_free(mod);
1389 module_free(mod, mod->module_init);
1391 module_free(mod, mod->module_core);
1396 if (err < 0) return ERR_PTR(err);
1400 /* This is where the real work happens */
1402 sys_init_module(void __user *umod,
1404 const char __user *uargs)
1409 /* Must have permission */
1410 if (!capable(CAP_SYS_MODULE))
1413 /* Only one module load at a time, please */
1414 if (down_interruptible(&module_mutex) != 0)
1417 /* Do all the hard work */
1418 mod = load_module(umod, len, uargs);
1421 return PTR_ERR(mod);
1424 /* Flush the instruction cache, since we've played with text */
1425 if (mod->module_init)
1426 flush_icache_range((unsigned long)mod->module_init,
1427 (unsigned long)mod->module_init
1429 flush_icache_range((unsigned long)mod->module_core,
1430 (unsigned long)mod->module_core + mod->core_size);
1432 /* Now sew it into the lists. They won't access us, since
1433 strong_try_module_get() will fail. */
1434 spin_lock_irq(&modlist_lock);
1435 list_add(&mod->list, &modules);
1436 spin_unlock_irq(&modlist_lock);
1438 /* Drop lock so they can recurse */
1441 down(¬ify_mutex);
1442 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1445 /* Start the module */
1448 /* Init routine failed: abort. Try to protect us from
1449 buggy refcounters. */
1450 mod->state = MODULE_STATE_GOING;
1451 synchronize_kernel();
1453 printk(KERN_ERR "%s: module is now stuck!\n",
1456 down(&module_mutex);
1463 /* Now it's a first class citizen! */
1464 down(&module_mutex);
1465 mod->state = MODULE_STATE_LIVE;
1466 module_free(mod, mod->module_init);
1467 mod->module_init = NULL;
1474 static inline int within(unsigned long addr, void *start, unsigned long size)
1476 return ((void *)addr >= start && (void *)addr < start + size);
1479 #ifdef CONFIG_KALLSYMS
1480 static const char *get_ksymbol(struct module *mod,
1482 unsigned long *size,
1483 unsigned long *offset)
1485 unsigned int i, best = 0;
1486 unsigned long nextval;
1488 /* At worse, next value is at end of module */
1489 if (within(addr, mod->module_init, mod->init_size))
1490 nextval = (unsigned long)mod->module_init + mod->init_size;
1492 nextval = (unsigned long)mod->module_core + mod->core_size;
1494 /* Scan for closest preceeding symbol, and next symbol. (ELF
1495 starts real symbols at 1). */
1496 for (i = 1; i < mod->num_symtab; i++) {
1497 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1500 if (mod->symtab[i].st_value <= addr
1501 && mod->symtab[i].st_value > mod->symtab[best].st_value)
1503 if (mod->symtab[i].st_value > addr
1504 && mod->symtab[i].st_value < nextval)
1505 nextval = mod->symtab[i].st_value;
1511 *size = nextval - mod->symtab[best].st_value;
1512 *offset = addr - mod->symtab[best].st_value;
1513 return mod->strtab + mod->symtab[best].st_name;
1516 /* For kallsyms to ask for address resolution. NULL means not found.
1517 We don't lock, as this is used for oops resolution and races are a
1519 const char *module_address_lookup(unsigned long addr,
1520 unsigned long *size,
1521 unsigned long *offset,
1526 list_for_each_entry(mod, &modules, list) {
1527 if (within(addr, mod->module_init, mod->init_size)
1528 || within(addr, mod->module_core, mod->core_size)) {
1529 *modname = mod->name;
1530 return get_ksymbol(mod, addr, size, offset);
1535 #endif /* CONFIG_KALLSYMS */
1537 /* Called by the /proc file system to return a list of modules. */
1538 static void *m_start(struct seq_file *m, loff_t *pos)
1540 struct list_head *i;
1543 down(&module_mutex);
1544 list_for_each(i, &modules) {
1553 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1555 struct list_head *i = p;
1557 if (i->next == &modules)
1562 static void m_stop(struct seq_file *m, void *p)
1567 static int m_show(struct seq_file *m, void *p)
1569 struct module *mod = list_entry(p, struct module, list);
1570 seq_printf(m, "%s %lu",
1571 mod->name, mod->init_size + mod->core_size);
1572 print_unload_info(m, mod);
1574 /* Informative for users. */
1575 seq_printf(m, " %s",
1576 mod->state == MODULE_STATE_GOING ? "Unloading":
1577 mod->state == MODULE_STATE_COMING ? "Loading":
1579 /* Used by oprofile and other similar tools. */
1580 seq_printf(m, " 0x%p", mod->module_core);
1582 seq_printf(m, "\n");
1586 /* Format: modulename size refcount deps address
1588 Where refcount is a number or -, and deps is a comma-separated list
1591 struct seq_operations modules_op = {
1598 /* Given an address, look for it in the module exception tables. */
1599 const struct exception_table_entry *search_module_extables(unsigned long addr)
1601 unsigned long flags;
1602 const struct exception_table_entry *e = NULL;
1605 spin_lock_irqsave(&modlist_lock, flags);
1606 list_for_each_entry(mod, &modules, list) {
1607 if (mod->num_exentries == 0)
1610 e = search_extable(mod->extable,
1611 mod->extable + mod->num_exentries - 1,
1616 spin_unlock_irqrestore(&modlist_lock, flags);
1618 /* Now, if we found one, we are running inside it now, hence
1619 we cannot unload the module, hence no refcnt needed. */
1623 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1624 struct module *module_text_address(unsigned long addr)
1628 list_for_each_entry(mod, &modules, list)
1629 if (within(addr, mod->module_init, mod->init_size)
1630 || within(addr, mod->module_core, mod->core_size))
1635 #ifdef CONFIG_MODVERSIONS
1636 /* Generate the signature for struct module here, too, for modversions. */
1637 void struct_module(struct module *mod) { return; }
1638 EXPORT_SYMBOL(struct_module);