[PATCH] pcmcia: release_class
[linux-2.6/openmoko-kernel/knife-kernel.git] / kernel / module.c
blob068e271ab3a538761c9129ec4b5fae0fdbe50910
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/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <linux/string.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <asm/cacheflush.h>
43 #if 0
44 #define DEBUGP printk
45 #else
46 #define DEBUGP(fmt , a...)
47 #endif
49 #ifndef ARCH_SHF_SMALL
50 #define ARCH_SHF_SMALL 0
51 #endif
53 /* If this is set, the section belongs in the init part of the module */
54 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
56 /* Protects module list */
57 static DEFINE_SPINLOCK(modlist_lock);
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)
68 int err;
69 down(&notify_mutex);
70 err = notifier_chain_register(&module_notify_list, nb);
71 up(&notify_mutex);
72 return err;
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
78 int err;
79 down(&notify_mutex);
80 err = notifier_chain_unregister(&module_notify_list, nb);
81 up(&notify_mutex);
82 return err;
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)
90 return 0;
91 return try_module_get(mod);
94 /* A thread that wants to hold a reference to a module only while it
95 * is running can call ths to safely exit.
96 * nfsd and lockd use this.
98 void __module_put_and_exit(struct module *mod, long code)
100 module_put(mod);
101 do_exit(code);
103 EXPORT_SYMBOL(__module_put_and_exit);
105 /* Find a module section: 0 means not found. */
106 static unsigned int find_sec(Elf_Ehdr *hdr,
107 Elf_Shdr *sechdrs,
108 const char *secstrings,
109 const char *name)
111 unsigned int i;
113 for (i = 1; i < hdr->e_shnum; i++)
114 /* Alloc bit cleared means "ignore it." */
115 if ((sechdrs[i].sh_flags & SHF_ALLOC)
116 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
117 return i;
118 return 0;
121 /* Provided by the linker */
122 extern const struct kernel_symbol __start___ksymtab[];
123 extern const struct kernel_symbol __stop___ksymtab[];
124 extern const struct kernel_symbol __start___ksymtab_gpl[];
125 extern const struct kernel_symbol __stop___ksymtab_gpl[];
126 extern const unsigned long __start___kcrctab[];
127 extern const unsigned long __start___kcrctab_gpl[];
129 #ifndef CONFIG_MODVERSIONS
130 #define symversion(base, idx) NULL
131 #else
132 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
133 #endif
135 /* Find a symbol, return value, crc and module which owns it */
136 static unsigned long __find_symbol(const char *name,
137 struct module **owner,
138 const unsigned long **crc,
139 int gplok)
141 struct module *mod;
142 unsigned int i;
144 /* Core kernel first. */
145 *owner = NULL;
146 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
147 if (strcmp(__start___ksymtab[i].name, name) == 0) {
148 *crc = symversion(__start___kcrctab, i);
149 return __start___ksymtab[i].value;
152 if (gplok) {
153 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
154 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
155 *crc = symversion(__start___kcrctab_gpl, i);
156 return __start___ksymtab_gpl[i].value;
160 /* Now try modules. */
161 list_for_each_entry(mod, &modules, list) {
162 *owner = mod;
163 for (i = 0; i < mod->num_syms; i++)
164 if (strcmp(mod->syms[i].name, name) == 0) {
165 *crc = symversion(mod->crcs, i);
166 return mod->syms[i].value;
169 if (gplok) {
170 for (i = 0; i < mod->num_gpl_syms; i++) {
171 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
172 *crc = symversion(mod->gpl_crcs, i);
173 return mod->gpl_syms[i].value;
178 DEBUGP("Failed to find symbol %s\n", name);
179 return 0;
182 /* Find a symbol in this elf symbol table */
183 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
184 unsigned int symindex,
185 const char *strtab,
186 const char *name)
188 unsigned int i;
189 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
191 /* Search (defined) internal symbols first. */
192 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
193 if (sym[i].st_shndx != SHN_UNDEF
194 && strcmp(name, strtab + sym[i].st_name) == 0)
195 return sym[i].st_value;
197 return 0;
200 /* Search for module by name: must hold module_mutex. */
201 static struct module *find_module(const char *name)
203 struct module *mod;
205 list_for_each_entry(mod, &modules, list) {
206 if (strcmp(mod->name, name) == 0)
207 return mod;
209 return NULL;
212 #ifdef CONFIG_SMP
213 /* Number of blocks used and allocated. */
214 static unsigned int pcpu_num_used, pcpu_num_allocated;
215 /* Size of each block. -ve means used. */
216 static int *pcpu_size;
218 static int split_block(unsigned int i, unsigned short size)
220 /* Reallocation required? */
221 if (pcpu_num_used + 1 > pcpu_num_allocated) {
222 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
223 GFP_KERNEL);
224 if (!new)
225 return 0;
227 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
228 pcpu_num_allocated *= 2;
229 kfree(pcpu_size);
230 pcpu_size = new;
233 /* Insert a new subblock */
234 memmove(&pcpu_size[i+1], &pcpu_size[i],
235 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
236 pcpu_num_used++;
238 pcpu_size[i+1] -= size;
239 pcpu_size[i] = size;
240 return 1;
243 static inline unsigned int block_size(int val)
245 if (val < 0)
246 return -val;
247 return val;
250 /* Created by linker magic */
251 extern char __per_cpu_start[], __per_cpu_end[];
253 static void *percpu_modalloc(unsigned long size, unsigned long align)
255 unsigned long extra;
256 unsigned int i;
257 void *ptr;
259 BUG_ON(align > SMP_CACHE_BYTES);
261 ptr = __per_cpu_start;
262 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
263 /* Extra for alignment requirement. */
264 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
265 BUG_ON(i == 0 && extra != 0);
267 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
268 continue;
270 /* Transfer extra to previous block. */
271 if (pcpu_size[i-1] < 0)
272 pcpu_size[i-1] -= extra;
273 else
274 pcpu_size[i-1] += extra;
275 pcpu_size[i] -= extra;
276 ptr += extra;
278 /* Split block if warranted */
279 if (pcpu_size[i] - size > sizeof(unsigned long))
280 if (!split_block(i, size))
281 return NULL;
283 /* Mark allocated */
284 pcpu_size[i] = -pcpu_size[i];
285 return ptr;
288 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
289 size);
290 return NULL;
293 static void percpu_modfree(void *freeme)
295 unsigned int i;
296 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
298 /* First entry is core kernel percpu data. */
299 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
300 if (ptr == freeme) {
301 pcpu_size[i] = -pcpu_size[i];
302 goto free;
305 BUG();
307 free:
308 /* Merge with previous? */
309 if (pcpu_size[i-1] >= 0) {
310 pcpu_size[i-1] += pcpu_size[i];
311 pcpu_num_used--;
312 memmove(&pcpu_size[i], &pcpu_size[i+1],
313 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
314 i--;
316 /* Merge with next? */
317 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
318 pcpu_size[i] += pcpu_size[i+1];
319 pcpu_num_used--;
320 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
321 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
325 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
326 Elf_Shdr *sechdrs,
327 const char *secstrings)
329 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
332 static int percpu_modinit(void)
334 pcpu_num_used = 2;
335 pcpu_num_allocated = 2;
336 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
337 GFP_KERNEL);
338 /* Static in-kernel percpu data (used). */
339 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
340 /* Free room. */
341 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
342 if (pcpu_size[1] < 0) {
343 printk(KERN_ERR "No per-cpu room for modules.\n");
344 pcpu_num_used = 1;
347 return 0;
349 __initcall(percpu_modinit);
350 #else /* ... !CONFIG_SMP */
351 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
353 return NULL;
355 static inline void percpu_modfree(void *pcpuptr)
357 BUG();
359 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
360 Elf_Shdr *sechdrs,
361 const char *secstrings)
363 return 0;
365 static inline void percpu_modcopy(void *pcpudst, const void *src,
366 unsigned long size)
368 /* pcpusec should be 0, and size of that section should be 0. */
369 BUG_ON(size != 0);
371 #endif /* CONFIG_SMP */
373 #ifdef CONFIG_MODULE_UNLOAD
374 #define MODINFO_ATTR(field) \
375 static void setup_modinfo_##field(struct module *mod, const char *s) \
377 mod->field = kstrdup(s, GFP_KERNEL); \
379 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
380 struct module *mod, char *buffer) \
382 return sprintf(buffer, "%s\n", mod->field); \
384 static int modinfo_##field##_exists(struct module *mod) \
386 return mod->field != NULL; \
388 static void free_modinfo_##field(struct module *mod) \
390 kfree(mod->field); \
391 mod->field = NULL; \
393 static struct module_attribute modinfo_##field = { \
394 .attr = { .name = __stringify(field), .mode = 0444, \
395 .owner = THIS_MODULE }, \
396 .show = show_modinfo_##field, \
397 .setup = setup_modinfo_##field, \
398 .test = modinfo_##field##_exists, \
399 .free = free_modinfo_##field, \
402 MODINFO_ATTR(version);
403 MODINFO_ATTR(srcversion);
405 static struct module_attribute *modinfo_attrs[] = {
406 &modinfo_version,
407 &modinfo_srcversion,
408 NULL,
411 /* Init the unload section of the module. */
412 static void module_unload_init(struct module *mod)
414 unsigned int i;
416 INIT_LIST_HEAD(&mod->modules_which_use_me);
417 for (i = 0; i < NR_CPUS; i++)
418 local_set(&mod->ref[i].count, 0);
419 /* Hold reference count during initialization. */
420 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
421 /* Backwards compatibility macros put refcount during init. */
422 mod->waiter = current;
425 /* modules using other modules */
426 struct module_use
428 struct list_head list;
429 struct module *module_which_uses;
432 /* Does a already use b? */
433 static int already_uses(struct module *a, struct module *b)
435 struct module_use *use;
437 list_for_each_entry(use, &b->modules_which_use_me, list) {
438 if (use->module_which_uses == a) {
439 DEBUGP("%s uses %s!\n", a->name, b->name);
440 return 1;
443 DEBUGP("%s does not use %s!\n", a->name, b->name);
444 return 0;
447 /* Module a uses b */
448 static int use_module(struct module *a, struct module *b)
450 struct module_use *use;
451 if (b == NULL || already_uses(a, b)) return 1;
453 if (!strong_try_module_get(b))
454 return 0;
456 DEBUGP("Allocating new usage for %s.\n", a->name);
457 use = kmalloc(sizeof(*use), GFP_ATOMIC);
458 if (!use) {
459 printk("%s: out of memory loading\n", a->name);
460 module_put(b);
461 return 0;
464 use->module_which_uses = a;
465 list_add(&use->list, &b->modules_which_use_me);
466 return 1;
469 /* Clear the unload stuff of the module. */
470 static void module_unload_free(struct module *mod)
472 struct module *i;
474 list_for_each_entry(i, &modules, list) {
475 struct module_use *use;
477 list_for_each_entry(use, &i->modules_which_use_me, list) {
478 if (use->module_which_uses == mod) {
479 DEBUGP("%s unusing %s\n", mod->name, i->name);
480 module_put(i);
481 list_del(&use->list);
482 kfree(use);
483 /* There can be at most one match. */
484 break;
490 #ifdef CONFIG_MODULE_FORCE_UNLOAD
491 static inline int try_force(unsigned int flags)
493 int ret = (flags & O_TRUNC);
494 if (ret)
495 tainted |= TAINT_FORCED_MODULE;
496 return ret;
498 #else
499 static inline int try_force(unsigned int flags)
501 return 0;
503 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
505 struct stopref
507 struct module *mod;
508 int flags;
509 int *forced;
512 /* Whole machine is stopped with interrupts off when this runs. */
513 static int __try_stop_module(void *_sref)
515 struct stopref *sref = _sref;
517 /* If it's not unused, quit unless we are told to block. */
518 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
519 if (!(*sref->forced = try_force(sref->flags)))
520 return -EWOULDBLOCK;
523 /* Mark it as dying. */
524 sref->mod->state = MODULE_STATE_GOING;
525 return 0;
528 static int try_stop_module(struct module *mod, int flags, int *forced)
530 struct stopref sref = { mod, flags, forced };
532 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
535 unsigned int module_refcount(struct module *mod)
537 unsigned int i, total = 0;
539 for (i = 0; i < NR_CPUS; i++)
540 total += local_read(&mod->ref[i].count);
541 return total;
543 EXPORT_SYMBOL(module_refcount);
545 /* This exists whether we can unload or not */
546 static void free_module(struct module *mod);
548 static void wait_for_zero_refcount(struct module *mod)
550 /* Since we might sleep for some time, drop the semaphore first */
551 up(&module_mutex);
552 for (;;) {
553 DEBUGP("Looking at refcount...\n");
554 set_current_state(TASK_UNINTERRUPTIBLE);
555 if (module_refcount(mod) == 0)
556 break;
557 schedule();
559 current->state = TASK_RUNNING;
560 down(&module_mutex);
563 asmlinkage long
564 sys_delete_module(const char __user *name_user, unsigned int flags)
566 struct module *mod;
567 char name[MODULE_NAME_LEN];
568 int ret, forced = 0;
570 if (!capable(CAP_SYS_MODULE))
571 return -EPERM;
573 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
574 return -EFAULT;
575 name[MODULE_NAME_LEN-1] = '\0';
577 if (down_interruptible(&module_mutex) != 0)
578 return -EINTR;
580 mod = find_module(name);
581 if (!mod) {
582 ret = -ENOENT;
583 goto out;
586 if (!list_empty(&mod->modules_which_use_me)) {
587 /* Other modules depend on us: get rid of them first. */
588 ret = -EWOULDBLOCK;
589 goto out;
592 /* Doing init or already dying? */
593 if (mod->state != MODULE_STATE_LIVE) {
594 /* FIXME: if (force), slam module count and wake up
595 waiter --RR */
596 DEBUGP("%s already dying\n", mod->name);
597 ret = -EBUSY;
598 goto out;
601 /* If it has an init func, it must have an exit func to unload */
602 if ((mod->init != NULL && mod->exit == NULL)
603 || mod->unsafe) {
604 forced = try_force(flags);
605 if (!forced) {
606 /* This module can't be removed */
607 ret = -EBUSY;
608 goto out;
612 /* Set this up before setting mod->state */
613 mod->waiter = current;
615 /* Stop the machine so refcounts can't move and disable module. */
616 ret = try_stop_module(mod, flags, &forced);
617 if (ret != 0)
618 goto out;
620 /* Never wait if forced. */
621 if (!forced && module_refcount(mod) != 0)
622 wait_for_zero_refcount(mod);
624 /* Final destruction now noone is using it. */
625 if (mod->exit != NULL) {
626 up(&module_mutex);
627 mod->exit();
628 down(&module_mutex);
630 free_module(mod);
632 out:
633 up(&module_mutex);
634 return ret;
637 static void print_unload_info(struct seq_file *m, struct module *mod)
639 struct module_use *use;
640 int printed_something = 0;
642 seq_printf(m, " %u ", module_refcount(mod));
644 /* Always include a trailing , so userspace can differentiate
645 between this and the old multi-field proc format. */
646 list_for_each_entry(use, &mod->modules_which_use_me, list) {
647 printed_something = 1;
648 seq_printf(m, "%s,", use->module_which_uses->name);
651 if (mod->unsafe) {
652 printed_something = 1;
653 seq_printf(m, "[unsafe],");
656 if (mod->init != NULL && mod->exit == NULL) {
657 printed_something = 1;
658 seq_printf(m, "[permanent],");
661 if (!printed_something)
662 seq_printf(m, "-");
665 void __symbol_put(const char *symbol)
667 struct module *owner;
668 unsigned long flags;
669 const unsigned long *crc;
671 spin_lock_irqsave(&modlist_lock, flags);
672 if (!__find_symbol(symbol, &owner, &crc, 1))
673 BUG();
674 module_put(owner);
675 spin_unlock_irqrestore(&modlist_lock, flags);
677 EXPORT_SYMBOL(__symbol_put);
679 void symbol_put_addr(void *addr)
681 unsigned long flags;
683 spin_lock_irqsave(&modlist_lock, flags);
684 if (!kernel_text_address((unsigned long)addr))
685 BUG();
687 module_put(module_text_address((unsigned long)addr));
688 spin_unlock_irqrestore(&modlist_lock, flags);
690 EXPORT_SYMBOL_GPL(symbol_put_addr);
692 static ssize_t show_refcnt(struct module_attribute *mattr,
693 struct module *mod, char *buffer)
695 /* sysfs holds a reference */
696 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
699 static struct module_attribute refcnt = {
700 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
701 .show = show_refcnt,
704 #else /* !CONFIG_MODULE_UNLOAD */
705 static void print_unload_info(struct seq_file *m, struct module *mod)
707 /* We don't know the usage count, or what modules are using. */
708 seq_printf(m, " - -");
711 static inline void module_unload_free(struct module *mod)
715 static inline int use_module(struct module *a, struct module *b)
717 return strong_try_module_get(b);
720 static inline void module_unload_init(struct module *mod)
723 #endif /* CONFIG_MODULE_UNLOAD */
725 #ifdef CONFIG_OBSOLETE_MODPARM
726 /* Bounds checking done below */
727 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
729 strcpy(kp->arg, val);
730 return 0;
733 static int set_obsolete(const char *val, struct kernel_param *kp)
735 unsigned int min, max;
736 unsigned int size, maxsize;
737 int dummy;
738 char *endp;
739 const char *p;
740 struct obsolete_modparm *obsparm = kp->arg;
742 if (!val) {
743 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
744 return -EINVAL;
747 /* type is: [min[-max]]{b,h,i,l,s} */
748 p = obsparm->type;
749 min = simple_strtol(p, &endp, 10);
750 if (endp == obsparm->type)
751 min = max = 1;
752 else if (*endp == '-') {
753 p = endp+1;
754 max = simple_strtol(p, &endp, 10);
755 } else
756 max = min;
757 switch (*endp) {
758 case 'b':
759 return param_array(kp->name, val, min, max, obsparm->addr,
760 1, param_set_byte, &dummy);
761 case 'h':
762 return param_array(kp->name, val, min, max, obsparm->addr,
763 sizeof(short), param_set_short, &dummy);
764 case 'i':
765 return param_array(kp->name, val, min, max, obsparm->addr,
766 sizeof(int), param_set_int, &dummy);
767 case 'l':
768 return param_array(kp->name, val, min, max, obsparm->addr,
769 sizeof(long), param_set_long, &dummy);
770 case 's':
771 return param_array(kp->name, val, min, max, obsparm->addr,
772 sizeof(char *), param_set_charp, &dummy);
774 case 'c':
775 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
776 and the decl is "char xxx[5][50];" */
777 p = endp+1;
778 maxsize = simple_strtol(p, &endp, 10);
779 /* We check lengths here (yes, this is a hack). */
780 p = val;
781 while (p[size = strcspn(p, ",")]) {
782 if (size >= maxsize)
783 goto oversize;
784 p += size+1;
786 if (size >= maxsize)
787 goto oversize;
788 return param_array(kp->name, val, min, max, obsparm->addr,
789 maxsize, obsparm_copy_string, &dummy);
791 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
792 return -EINVAL;
793 oversize:
794 printk(KERN_ERR
795 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
796 return -EINVAL;
799 static int obsolete_params(const char *name,
800 char *args,
801 struct obsolete_modparm obsparm[],
802 unsigned int num,
803 Elf_Shdr *sechdrs,
804 unsigned int symindex,
805 const char *strtab)
807 struct kernel_param *kp;
808 unsigned int i;
809 int ret;
811 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
812 if (!kp)
813 return -ENOMEM;
815 for (i = 0; i < num; i++) {
816 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
818 snprintf(sym_name, sizeof(sym_name), "%s%s",
819 MODULE_SYMBOL_PREFIX, obsparm[i].name);
821 kp[i].name = obsparm[i].name;
822 kp[i].perm = 000;
823 kp[i].set = set_obsolete;
824 kp[i].get = NULL;
825 obsparm[i].addr
826 = (void *)find_local_symbol(sechdrs, symindex, strtab,
827 sym_name);
828 if (!obsparm[i].addr) {
829 printk("%s: falsely claims to have parameter %s\n",
830 name, obsparm[i].name);
831 ret = -EINVAL;
832 goto out;
834 kp[i].arg = &obsparm[i];
837 ret = parse_args(name, args, kp, num, NULL);
838 out:
839 kfree(kp);
840 return ret;
842 #else
843 static int obsolete_params(const char *name,
844 char *args,
845 struct obsolete_modparm obsparm[],
846 unsigned int num,
847 Elf_Shdr *sechdrs,
848 unsigned int symindex,
849 const char *strtab)
851 if (num != 0)
852 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
853 name);
854 return 0;
856 #endif /* CONFIG_OBSOLETE_MODPARM */
858 static const char vermagic[] = VERMAGIC_STRING;
860 #ifdef CONFIG_MODVERSIONS
861 static int check_version(Elf_Shdr *sechdrs,
862 unsigned int versindex,
863 const char *symname,
864 struct module *mod,
865 const unsigned long *crc)
867 unsigned int i, num_versions;
868 struct modversion_info *versions;
870 /* Exporting module didn't supply crcs? OK, we're already tainted. */
871 if (!crc)
872 return 1;
874 versions = (void *) sechdrs[versindex].sh_addr;
875 num_versions = sechdrs[versindex].sh_size
876 / sizeof(struct modversion_info);
878 for (i = 0; i < num_versions; i++) {
879 if (strcmp(versions[i].name, symname) != 0)
880 continue;
882 if (versions[i].crc == *crc)
883 return 1;
884 printk("%s: disagrees about version of symbol %s\n",
885 mod->name, symname);
886 DEBUGP("Found checksum %lX vs module %lX\n",
887 *crc, versions[i].crc);
888 return 0;
890 /* Not in module's version table. OK, but that taints the kernel. */
891 if (!(tainted & TAINT_FORCED_MODULE)) {
892 printk("%s: no version for \"%s\" found: kernel tainted.\n",
893 mod->name, symname);
894 tainted |= TAINT_FORCED_MODULE;
896 return 1;
899 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
900 unsigned int versindex,
901 struct module *mod)
903 const unsigned long *crc;
904 struct module *owner;
906 if (!__find_symbol("struct_module", &owner, &crc, 1))
907 BUG();
908 return check_version(sechdrs, versindex, "struct_module", mod,
909 crc);
912 /* First part is kernel version, which we ignore. */
913 static inline int same_magic(const char *amagic, const char *bmagic)
915 amagic += strcspn(amagic, " ");
916 bmagic += strcspn(bmagic, " ");
917 return strcmp(amagic, bmagic) == 0;
919 #else
920 static inline int check_version(Elf_Shdr *sechdrs,
921 unsigned int versindex,
922 const char *symname,
923 struct module *mod,
924 const unsigned long *crc)
926 return 1;
929 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
931 struct module *mod)
933 return 1;
936 static inline int same_magic(const char *amagic, const char *bmagic)
938 return strcmp(amagic, bmagic) == 0;
940 #endif /* CONFIG_MODVERSIONS */
942 /* Resolve a symbol for this module. I.e. if we find one, record usage.
943 Must be holding module_mutex. */
944 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
945 unsigned int versindex,
946 const char *name,
947 struct module *mod)
949 struct module *owner;
950 unsigned long ret;
951 const unsigned long *crc;
953 spin_lock_irq(&modlist_lock);
954 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
955 if (ret) {
956 /* use_module can fail due to OOM, or module unloading */
957 if (!check_version(sechdrs, versindex, name, mod, crc) ||
958 !use_module(mod, owner))
959 ret = 0;
961 spin_unlock_irq(&modlist_lock);
962 return ret;
967 * /sys/module/foo/sections stuff
968 * J. Corbet <corbet@lwn.net>
970 #ifdef CONFIG_KALLSYMS
971 static ssize_t module_sect_show(struct module_attribute *mattr,
972 struct module *mod, char *buf)
974 struct module_sect_attr *sattr =
975 container_of(mattr, struct module_sect_attr, mattr);
976 return sprintf(buf, "0x%lx\n", sattr->address);
979 static void add_sect_attrs(struct module *mod, unsigned int nsect,
980 char *secstrings, Elf_Shdr *sechdrs)
982 unsigned int nloaded = 0, i, size[2];
983 struct module_sect_attrs *sect_attrs;
984 struct module_sect_attr *sattr;
985 struct attribute **gattr;
987 /* Count loaded sections and allocate structures */
988 for (i = 0; i < nsect; i++)
989 if (sechdrs[i].sh_flags & SHF_ALLOC)
990 nloaded++;
991 size[0] = ALIGN(sizeof(*sect_attrs)
992 + nloaded * sizeof(sect_attrs->attrs[0]),
993 sizeof(sect_attrs->grp.attrs[0]));
994 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
995 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
996 return;
998 /* Setup section attributes. */
999 sect_attrs->grp.name = "sections";
1000 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1002 sattr = &sect_attrs->attrs[0];
1003 gattr = &sect_attrs->grp.attrs[0];
1004 for (i = 0; i < nsect; i++) {
1005 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1006 continue;
1007 sattr->address = sechdrs[i].sh_addr;
1008 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1009 MODULE_SECT_NAME_LEN);
1010 sattr->mattr.show = module_sect_show;
1011 sattr->mattr.store = NULL;
1012 sattr->mattr.attr.name = sattr->name;
1013 sattr->mattr.attr.owner = mod;
1014 sattr->mattr.attr.mode = S_IRUGO;
1015 *(gattr++) = &(sattr++)->mattr.attr;
1017 *gattr = NULL;
1019 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1020 goto out;
1022 mod->sect_attrs = sect_attrs;
1023 return;
1024 out:
1025 kfree(sect_attrs);
1028 static void remove_sect_attrs(struct module *mod)
1030 if (mod->sect_attrs) {
1031 sysfs_remove_group(&mod->mkobj.kobj,
1032 &mod->sect_attrs->grp);
1033 /* We are positive that no one is using any sect attrs
1034 * at this point. Deallocate immediately. */
1035 kfree(mod->sect_attrs);
1036 mod->sect_attrs = NULL;
1041 #else
1042 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1043 char *sectstrings, Elf_Shdr *sechdrs)
1047 static inline void remove_sect_attrs(struct module *mod)
1050 #endif /* CONFIG_KALLSYMS */
1053 #ifdef CONFIG_MODULE_UNLOAD
1054 static inline int module_add_refcnt_attr(struct module *mod)
1056 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1058 static void module_remove_refcnt_attr(struct module *mod)
1060 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1062 #else
1063 static inline int module_add_refcnt_attr(struct module *mod)
1065 return 0;
1067 static void module_remove_refcnt_attr(struct module *mod)
1070 #endif
1072 #ifdef CONFIG_MODULE_UNLOAD
1073 static int module_add_modinfo_attrs(struct module *mod)
1075 struct module_attribute *attr;
1076 int error = 0;
1077 int i;
1079 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1080 if (!attr->test ||
1081 (attr->test && attr->test(mod)))
1082 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1084 return error;
1087 static void module_remove_modinfo_attrs(struct module *mod)
1089 struct module_attribute *attr;
1090 int i;
1092 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1093 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1094 attr->free(mod);
1097 #endif
1099 static int mod_sysfs_setup(struct module *mod,
1100 struct kernel_param *kparam,
1101 unsigned int num_params)
1103 int err;
1105 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1106 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1107 if (err)
1108 goto out;
1109 kobj_set_kset_s(&mod->mkobj, module_subsys);
1110 mod->mkobj.mod = mod;
1111 err = kobject_register(&mod->mkobj.kobj);
1112 if (err)
1113 goto out;
1115 err = module_add_refcnt_attr(mod);
1116 if (err)
1117 goto out_unreg;
1119 err = module_param_sysfs_setup(mod, kparam, num_params);
1120 if (err)
1121 goto out_unreg;
1123 #ifdef CONFIG_MODULE_UNLOAD
1124 err = module_add_modinfo_attrs(mod);
1125 if (err)
1126 goto out_unreg;
1127 #endif
1129 return 0;
1131 out_unreg:
1132 kobject_unregister(&mod->mkobj.kobj);
1133 out:
1134 return err;
1137 static void mod_kobject_remove(struct module *mod)
1139 #ifdef CONFIG_MODULE_UNLOAD
1140 module_remove_modinfo_attrs(mod);
1141 #endif
1142 module_remove_refcnt_attr(mod);
1143 module_param_sysfs_remove(mod);
1145 kobject_unregister(&mod->mkobj.kobj);
1149 * unlink the module with the whole machine is stopped with interrupts off
1150 * - this defends against kallsyms not taking locks
1152 static int __unlink_module(void *_mod)
1154 struct module *mod = _mod;
1155 list_del(&mod->list);
1156 return 0;
1159 /* Free a module, remove from lists, etc (must hold module mutex). */
1160 static void free_module(struct module *mod)
1162 /* Delete from various lists */
1163 stop_machine_run(__unlink_module, mod, NR_CPUS);
1164 remove_sect_attrs(mod);
1165 mod_kobject_remove(mod);
1167 /* Arch-specific cleanup. */
1168 module_arch_cleanup(mod);
1170 /* Module unload stuff */
1171 module_unload_free(mod);
1173 /* This may be NULL, but that's OK */
1174 module_free(mod, mod->module_init);
1175 kfree(mod->args);
1176 if (mod->percpu)
1177 percpu_modfree(mod->percpu);
1179 /* Finally, free the core (containing the module structure) */
1180 module_free(mod, mod->module_core);
1183 void *__symbol_get(const char *symbol)
1185 struct module *owner;
1186 unsigned long value, flags;
1187 const unsigned long *crc;
1189 spin_lock_irqsave(&modlist_lock, flags);
1190 value = __find_symbol(symbol, &owner, &crc, 1);
1191 if (value && !strong_try_module_get(owner))
1192 value = 0;
1193 spin_unlock_irqrestore(&modlist_lock, flags);
1195 return (void *)value;
1197 EXPORT_SYMBOL_GPL(__symbol_get);
1199 /* Change all symbols so that sh_value encodes the pointer directly. */
1200 static int simplify_symbols(Elf_Shdr *sechdrs,
1201 unsigned int symindex,
1202 const char *strtab,
1203 unsigned int versindex,
1204 unsigned int pcpuindex,
1205 struct module *mod)
1207 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1208 unsigned long secbase;
1209 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1210 int ret = 0;
1212 for (i = 1; i < n; i++) {
1213 switch (sym[i].st_shndx) {
1214 case SHN_COMMON:
1215 /* We compiled with -fno-common. These are not
1216 supposed to happen. */
1217 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1218 printk("%s: please compile with -fno-common\n",
1219 mod->name);
1220 ret = -ENOEXEC;
1221 break;
1223 case SHN_ABS:
1224 /* Don't need to do anything */
1225 DEBUGP("Absolute symbol: 0x%08lx\n",
1226 (long)sym[i].st_value);
1227 break;
1229 case SHN_UNDEF:
1230 sym[i].st_value
1231 = resolve_symbol(sechdrs, versindex,
1232 strtab + sym[i].st_name, mod);
1234 /* Ok if resolved. */
1235 if (sym[i].st_value != 0)
1236 break;
1237 /* Ok if weak. */
1238 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1239 break;
1241 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1242 mod->name, strtab + sym[i].st_name);
1243 ret = -ENOENT;
1244 break;
1246 default:
1247 /* Divert to percpu allocation if a percpu var. */
1248 if (sym[i].st_shndx == pcpuindex)
1249 secbase = (unsigned long)mod->percpu;
1250 else
1251 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1252 sym[i].st_value += secbase;
1253 break;
1257 return ret;
1260 /* Update size with this section: return offset. */
1261 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1263 long ret;
1265 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1266 *size = ret + sechdr->sh_size;
1267 return ret;
1270 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1271 might -- code, read-only data, read-write data, small data. Tally
1272 sizes, and place the offsets into sh_entsize fields: high bit means it
1273 belongs in init. */
1274 static void layout_sections(struct module *mod,
1275 const Elf_Ehdr *hdr,
1276 Elf_Shdr *sechdrs,
1277 const char *secstrings)
1279 static unsigned long const masks[][2] = {
1280 /* NOTE: all executable code must be the first section
1281 * in this array; otherwise modify the text_size
1282 * finder in the two loops below */
1283 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1284 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1285 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1286 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1288 unsigned int m, i;
1290 for (i = 0; i < hdr->e_shnum; i++)
1291 sechdrs[i].sh_entsize = ~0UL;
1293 DEBUGP("Core section allocation order:\n");
1294 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1295 for (i = 0; i < hdr->e_shnum; ++i) {
1296 Elf_Shdr *s = &sechdrs[i];
1298 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1299 || (s->sh_flags & masks[m][1])
1300 || s->sh_entsize != ~0UL
1301 || strncmp(secstrings + s->sh_name,
1302 ".init", 5) == 0)
1303 continue;
1304 s->sh_entsize = get_offset(&mod->core_size, s);
1305 DEBUGP("\t%s\n", secstrings + s->sh_name);
1307 if (m == 0)
1308 mod->core_text_size = mod->core_size;
1311 DEBUGP("Init section allocation order:\n");
1312 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1313 for (i = 0; i < hdr->e_shnum; ++i) {
1314 Elf_Shdr *s = &sechdrs[i];
1316 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1317 || (s->sh_flags & masks[m][1])
1318 || s->sh_entsize != ~0UL
1319 || strncmp(secstrings + s->sh_name,
1320 ".init", 5) != 0)
1321 continue;
1322 s->sh_entsize = (get_offset(&mod->init_size, s)
1323 | INIT_OFFSET_MASK);
1324 DEBUGP("\t%s\n", secstrings + s->sh_name);
1326 if (m == 0)
1327 mod->init_text_size = mod->init_size;
1331 static inline int license_is_gpl_compatible(const char *license)
1333 return (strcmp(license, "GPL") == 0
1334 || strcmp(license, "GPL v2") == 0
1335 || strcmp(license, "GPL and additional rights") == 0
1336 || strcmp(license, "Dual BSD/GPL") == 0
1337 || strcmp(license, "Dual MPL/GPL") == 0);
1340 static void set_license(struct module *mod, const char *license)
1342 if (!license)
1343 license = "unspecified";
1345 mod->license_gplok = license_is_gpl_compatible(license);
1346 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1347 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1348 mod->name, license);
1349 tainted |= TAINT_PROPRIETARY_MODULE;
1353 /* Parse tag=value strings from .modinfo section */
1354 static char *next_string(char *string, unsigned long *secsize)
1356 /* Skip non-zero chars */
1357 while (string[0]) {
1358 string++;
1359 if ((*secsize)-- <= 1)
1360 return NULL;
1363 /* Skip any zero padding. */
1364 while (!string[0]) {
1365 string++;
1366 if ((*secsize)-- <= 1)
1367 return NULL;
1369 return string;
1372 static char *get_modinfo(Elf_Shdr *sechdrs,
1373 unsigned int info,
1374 const char *tag)
1376 char *p;
1377 unsigned int taglen = strlen(tag);
1378 unsigned long size = sechdrs[info].sh_size;
1380 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1381 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1382 return p + taglen + 1;
1384 return NULL;
1387 #ifdef CONFIG_MODULE_UNLOAD
1388 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1389 unsigned int infoindex)
1391 struct module_attribute *attr;
1392 int i;
1394 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1395 if (attr->setup)
1396 attr->setup(mod,
1397 get_modinfo(sechdrs,
1398 infoindex,
1399 attr->attr.name));
1402 #endif
1404 #ifdef CONFIG_KALLSYMS
1405 int is_exported(const char *name, const struct module *mod)
1407 unsigned int i;
1409 if (!mod) {
1410 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1411 if (strcmp(__start___ksymtab[i].name, name) == 0)
1412 return 1;
1413 return 0;
1415 for (i = 0; i < mod->num_syms; i++)
1416 if (strcmp(mod->syms[i].name, name) == 0)
1417 return 1;
1418 return 0;
1421 /* As per nm */
1422 static char elf_type(const Elf_Sym *sym,
1423 Elf_Shdr *sechdrs,
1424 const char *secstrings,
1425 struct module *mod)
1427 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1428 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1429 return 'v';
1430 else
1431 return 'w';
1433 if (sym->st_shndx == SHN_UNDEF)
1434 return 'U';
1435 if (sym->st_shndx == SHN_ABS)
1436 return 'a';
1437 if (sym->st_shndx >= SHN_LORESERVE)
1438 return '?';
1439 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1440 return 't';
1441 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1442 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1443 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1444 return 'r';
1445 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1446 return 'g';
1447 else
1448 return 'd';
1450 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1451 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1452 return 's';
1453 else
1454 return 'b';
1456 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1457 ".debug", strlen(".debug")) == 0)
1458 return 'n';
1459 return '?';
1462 static void add_kallsyms(struct module *mod,
1463 Elf_Shdr *sechdrs,
1464 unsigned int symindex,
1465 unsigned int strindex,
1466 const char *secstrings)
1468 unsigned int i;
1470 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1471 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1472 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1474 /* Set types up while we still have access to sections. */
1475 for (i = 0; i < mod->num_symtab; i++)
1476 mod->symtab[i].st_info
1477 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1479 #else
1480 static inline void add_kallsyms(struct module *mod,
1481 Elf_Shdr *sechdrs,
1482 unsigned int symindex,
1483 unsigned int strindex,
1484 const char *secstrings)
1487 #endif /* CONFIG_KALLSYMS */
1489 /* Allocate and load the module: note that size of section 0 is always
1490 zero, and we rely on this for optional sections. */
1491 static struct module *load_module(void __user *umod,
1492 unsigned long len,
1493 const char __user *uargs)
1495 Elf_Ehdr *hdr;
1496 Elf_Shdr *sechdrs;
1497 char *secstrings, *args, *modmagic, *strtab = NULL;
1498 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1499 exportindex, modindex, obsparmindex, infoindex, gplindex,
1500 crcindex, gplcrcindex, versindex, pcpuindex;
1501 long arglen;
1502 struct module *mod;
1503 long err = 0;
1504 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1505 struct exception_table_entry *extable;
1507 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1508 umod, len, uargs);
1509 if (len < sizeof(*hdr))
1510 return ERR_PTR(-ENOEXEC);
1512 /* Suck in entire file: we'll want most of it. */
1513 /* vmalloc barfs on "unusual" numbers. Check here */
1514 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1515 return ERR_PTR(-ENOMEM);
1516 if (copy_from_user(hdr, umod, len) != 0) {
1517 err = -EFAULT;
1518 goto free_hdr;
1521 /* Sanity checks against insmoding binaries or wrong arch,
1522 weird elf version */
1523 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1524 || hdr->e_type != ET_REL
1525 || !elf_check_arch(hdr)
1526 || hdr->e_shentsize != sizeof(*sechdrs)) {
1527 err = -ENOEXEC;
1528 goto free_hdr;
1531 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1532 goto truncated;
1534 /* Convenience variables */
1535 sechdrs = (void *)hdr + hdr->e_shoff;
1536 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1537 sechdrs[0].sh_addr = 0;
1539 for (i = 1; i < hdr->e_shnum; i++) {
1540 if (sechdrs[i].sh_type != SHT_NOBITS
1541 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1542 goto truncated;
1544 /* Mark all sections sh_addr with their address in the
1545 temporary image. */
1546 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1548 /* Internal symbols and strings. */
1549 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1550 symindex = i;
1551 strindex = sechdrs[i].sh_link;
1552 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1554 #ifndef CONFIG_MODULE_UNLOAD
1555 /* Don't load .exit sections */
1556 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1557 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1558 #endif
1561 modindex = find_sec(hdr, sechdrs, secstrings,
1562 ".gnu.linkonce.this_module");
1563 if (!modindex) {
1564 printk(KERN_WARNING "No module found in object\n");
1565 err = -ENOEXEC;
1566 goto free_hdr;
1568 mod = (void *)sechdrs[modindex].sh_addr;
1570 if (symindex == 0) {
1571 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1572 mod->name);
1573 err = -ENOEXEC;
1574 goto free_hdr;
1577 /* Optional sections */
1578 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1579 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1580 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1581 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1582 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1583 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1584 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1585 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1586 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1587 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1589 /* Don't keep modinfo section */
1590 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1591 #ifdef CONFIG_KALLSYMS
1592 /* Keep symbol and string tables for decoding later. */
1593 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1594 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1595 #endif
1597 /* Check module struct version now, before we try to use module. */
1598 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1599 err = -ENOEXEC;
1600 goto free_hdr;
1603 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1604 /* This is allowed: modprobe --force will invalidate it. */
1605 if (!modmagic) {
1606 tainted |= TAINT_FORCED_MODULE;
1607 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1608 mod->name);
1609 } else if (!same_magic(modmagic, vermagic)) {
1610 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1611 mod->name, modmagic, vermagic);
1612 err = -ENOEXEC;
1613 goto free_hdr;
1616 /* Now copy in args */
1617 arglen = strlen_user(uargs);
1618 if (!arglen) {
1619 err = -EFAULT;
1620 goto free_hdr;
1622 args = kmalloc(arglen, GFP_KERNEL);
1623 if (!args) {
1624 err = -ENOMEM;
1625 goto free_hdr;
1627 if (copy_from_user(args, uargs, arglen) != 0) {
1628 err = -EFAULT;
1629 goto free_mod;
1632 if (find_module(mod->name)) {
1633 err = -EEXIST;
1634 goto free_mod;
1637 mod->state = MODULE_STATE_COMING;
1639 /* Allow arches to frob section contents and sizes. */
1640 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1641 if (err < 0)
1642 goto free_mod;
1644 if (pcpuindex) {
1645 /* We have a special allocation for this section. */
1646 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1647 sechdrs[pcpuindex].sh_addralign);
1648 if (!percpu) {
1649 err = -ENOMEM;
1650 goto free_mod;
1652 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1653 mod->percpu = percpu;
1656 /* Determine total sizes, and put offsets in sh_entsize. For now
1657 this is done generically; there doesn't appear to be any
1658 special cases for the architectures. */
1659 layout_sections(mod, hdr, sechdrs, secstrings);
1661 /* Do the allocs. */
1662 ptr = module_alloc(mod->core_size);
1663 if (!ptr) {
1664 err = -ENOMEM;
1665 goto free_percpu;
1667 memset(ptr, 0, mod->core_size);
1668 mod->module_core = ptr;
1670 ptr = module_alloc(mod->init_size);
1671 if (!ptr && mod->init_size) {
1672 err = -ENOMEM;
1673 goto free_core;
1675 memset(ptr, 0, mod->init_size);
1676 mod->module_init = ptr;
1678 /* Transfer each section which specifies SHF_ALLOC */
1679 DEBUGP("final section addresses:\n");
1680 for (i = 0; i < hdr->e_shnum; i++) {
1681 void *dest;
1683 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1684 continue;
1686 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1687 dest = mod->module_init
1688 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1689 else
1690 dest = mod->module_core + sechdrs[i].sh_entsize;
1692 if (sechdrs[i].sh_type != SHT_NOBITS)
1693 memcpy(dest, (void *)sechdrs[i].sh_addr,
1694 sechdrs[i].sh_size);
1695 /* Update sh_addr to point to copy in image. */
1696 sechdrs[i].sh_addr = (unsigned long)dest;
1697 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1699 /* Module has been moved. */
1700 mod = (void *)sechdrs[modindex].sh_addr;
1702 /* Now we've moved module, initialize linked lists, etc. */
1703 module_unload_init(mod);
1705 /* Set up license info based on the info section */
1706 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1708 #ifdef CONFIG_MODULE_UNLOAD
1709 /* Set up MODINFO_ATTR fields */
1710 setup_modinfo(mod, sechdrs, infoindex);
1711 #endif
1713 /* Fix up syms, so that st_value is a pointer to location. */
1714 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1715 mod);
1716 if (err < 0)
1717 goto cleanup;
1719 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1720 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1721 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1722 if (crcindex)
1723 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1724 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1725 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1726 if (gplcrcindex)
1727 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1729 #ifdef CONFIG_MODVERSIONS
1730 if ((mod->num_syms && !crcindex) ||
1731 (mod->num_gpl_syms && !gplcrcindex)) {
1732 printk(KERN_WARNING "%s: No versions for exported symbols."
1733 " Tainting kernel.\n", mod->name);
1734 tainted |= TAINT_FORCED_MODULE;
1736 #endif
1738 /* Now do relocations. */
1739 for (i = 1; i < hdr->e_shnum; i++) {
1740 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1741 unsigned int info = sechdrs[i].sh_info;
1743 /* Not a valid relocation section? */
1744 if (info >= hdr->e_shnum)
1745 continue;
1747 /* Don't bother with non-allocated sections */
1748 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1749 continue;
1751 if (sechdrs[i].sh_type == SHT_REL)
1752 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1753 else if (sechdrs[i].sh_type == SHT_RELA)
1754 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1755 mod);
1756 if (err < 0)
1757 goto cleanup;
1760 /* Set up and sort exception table */
1761 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1762 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1763 sort_extable(extable, extable + mod->num_exentries);
1765 /* Finally, copy percpu area over. */
1766 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1767 sechdrs[pcpuindex].sh_size);
1769 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1771 err = module_finalize(hdr, sechdrs, mod);
1772 if (err < 0)
1773 goto cleanup;
1775 mod->args = args;
1776 if (obsparmindex) {
1777 err = obsolete_params(mod->name, mod->args,
1778 (struct obsolete_modparm *)
1779 sechdrs[obsparmindex].sh_addr,
1780 sechdrs[obsparmindex].sh_size
1781 / sizeof(struct obsolete_modparm),
1782 sechdrs, symindex,
1783 (char *)sechdrs[strindex].sh_addr);
1784 if (setupindex)
1785 printk(KERN_WARNING "%s: Ignoring new-style "
1786 "parameters in presence of obsolete ones\n",
1787 mod->name);
1788 } else {
1789 /* Size of section 0 is 0, so this works well if no params */
1790 err = parse_args(mod->name, mod->args,
1791 (struct kernel_param *)
1792 sechdrs[setupindex].sh_addr,
1793 sechdrs[setupindex].sh_size
1794 / sizeof(struct kernel_param),
1795 NULL);
1797 if (err < 0)
1798 goto arch_cleanup;
1800 err = mod_sysfs_setup(mod,
1801 (struct kernel_param *)
1802 sechdrs[setupindex].sh_addr,
1803 sechdrs[setupindex].sh_size
1804 / sizeof(struct kernel_param));
1805 if (err < 0)
1806 goto arch_cleanup;
1807 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1809 /* Get rid of temporary copy */
1810 vfree(hdr);
1812 /* Done! */
1813 return mod;
1815 arch_cleanup:
1816 module_arch_cleanup(mod);
1817 cleanup:
1818 module_unload_free(mod);
1819 module_free(mod, mod->module_init);
1820 free_core:
1821 module_free(mod, mod->module_core);
1822 free_percpu:
1823 if (percpu)
1824 percpu_modfree(percpu);
1825 free_mod:
1826 kfree(args);
1827 free_hdr:
1828 vfree(hdr);
1829 if (err < 0) return ERR_PTR(err);
1830 else return ptr;
1832 truncated:
1833 printk(KERN_ERR "Module len %lu truncated\n", len);
1834 err = -ENOEXEC;
1835 goto free_hdr;
1839 * link the module with the whole machine is stopped with interrupts off
1840 * - this defends against kallsyms not taking locks
1842 static int __link_module(void *_mod)
1844 struct module *mod = _mod;
1845 list_add(&mod->list, &modules);
1846 return 0;
1849 /* This is where the real work happens */
1850 asmlinkage long
1851 sys_init_module(void __user *umod,
1852 unsigned long len,
1853 const char __user *uargs)
1855 struct module *mod;
1856 mm_segment_t old_fs = get_fs();
1857 int ret = 0;
1859 /* Must have permission */
1860 if (!capable(CAP_SYS_MODULE))
1861 return -EPERM;
1863 /* Only one module load at a time, please */
1864 if (down_interruptible(&module_mutex) != 0)
1865 return -EINTR;
1867 /* Do all the hard work */
1868 mod = load_module(umod, len, uargs);
1869 if (IS_ERR(mod)) {
1870 up(&module_mutex);
1871 return PTR_ERR(mod);
1874 /* flush the icache in correct context */
1875 set_fs(KERNEL_DS);
1877 /* Flush the instruction cache, since we've played with text */
1878 if (mod->module_init)
1879 flush_icache_range((unsigned long)mod->module_init,
1880 (unsigned long)mod->module_init
1881 + mod->init_size);
1882 flush_icache_range((unsigned long)mod->module_core,
1883 (unsigned long)mod->module_core + mod->core_size);
1885 set_fs(old_fs);
1887 /* Now sew it into the lists. They won't access us, since
1888 strong_try_module_get() will fail. */
1889 stop_machine_run(__link_module, mod, NR_CPUS);
1891 /* Drop lock so they can recurse */
1892 up(&module_mutex);
1894 down(&notify_mutex);
1895 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1896 up(&notify_mutex);
1898 /* Start the module */
1899 if (mod->init != NULL)
1900 ret = mod->init();
1901 if (ret < 0) {
1902 /* Init routine failed: abort. Try to protect us from
1903 buggy refcounters. */
1904 mod->state = MODULE_STATE_GOING;
1905 synchronize_sched();
1906 if (mod->unsafe)
1907 printk(KERN_ERR "%s: module is now stuck!\n",
1908 mod->name);
1909 else {
1910 module_put(mod);
1911 down(&module_mutex);
1912 free_module(mod);
1913 up(&module_mutex);
1915 return ret;
1918 /* Now it's a first class citizen! */
1919 down(&module_mutex);
1920 mod->state = MODULE_STATE_LIVE;
1921 /* Drop initial reference. */
1922 module_put(mod);
1923 module_free(mod, mod->module_init);
1924 mod->module_init = NULL;
1925 mod->init_size = 0;
1926 mod->init_text_size = 0;
1927 up(&module_mutex);
1929 return 0;
1932 static inline int within(unsigned long addr, void *start, unsigned long size)
1934 return ((void *)addr >= start && (void *)addr < start + size);
1937 #ifdef CONFIG_KALLSYMS
1939 * This ignores the intensely annoying "mapping symbols" found
1940 * in ARM ELF files: $a, $t and $d.
1942 static inline int is_arm_mapping_symbol(const char *str)
1944 return str[0] == '$' && strchr("atd", str[1])
1945 && (str[2] == '\0' || str[2] == '.');
1948 static const char *get_ksymbol(struct module *mod,
1949 unsigned long addr,
1950 unsigned long *size,
1951 unsigned long *offset)
1953 unsigned int i, best = 0;
1954 unsigned long nextval;
1956 /* At worse, next value is at end of module */
1957 if (within(addr, mod->module_init, mod->init_size))
1958 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1959 else
1960 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1962 /* Scan for closest preceeding symbol, and next symbol. (ELF
1963 starts real symbols at 1). */
1964 for (i = 1; i < mod->num_symtab; i++) {
1965 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1966 continue;
1968 /* We ignore unnamed symbols: they're uninformative
1969 * and inserted at a whim. */
1970 if (mod->symtab[i].st_value <= addr
1971 && mod->symtab[i].st_value > mod->symtab[best].st_value
1972 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1973 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1974 best = i;
1975 if (mod->symtab[i].st_value > addr
1976 && mod->symtab[i].st_value < nextval
1977 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1978 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1979 nextval = mod->symtab[i].st_value;
1982 if (!best)
1983 return NULL;
1985 *size = nextval - mod->symtab[best].st_value;
1986 *offset = addr - mod->symtab[best].st_value;
1987 return mod->strtab + mod->symtab[best].st_name;
1990 /* For kallsyms to ask for address resolution. NULL means not found.
1991 We don't lock, as this is used for oops resolution and races are a
1992 lesser concern. */
1993 const char *module_address_lookup(unsigned long addr,
1994 unsigned long *size,
1995 unsigned long *offset,
1996 char **modname)
1998 struct module *mod;
2000 list_for_each_entry(mod, &modules, list) {
2001 if (within(addr, mod->module_init, mod->init_size)
2002 || within(addr, mod->module_core, mod->core_size)) {
2003 *modname = mod->name;
2004 return get_ksymbol(mod, addr, size, offset);
2007 return NULL;
2010 struct module *module_get_kallsym(unsigned int symnum,
2011 unsigned long *value,
2012 char *type,
2013 char namebuf[128])
2015 struct module *mod;
2017 down(&module_mutex);
2018 list_for_each_entry(mod, &modules, list) {
2019 if (symnum < mod->num_symtab) {
2020 *value = mod->symtab[symnum].st_value;
2021 *type = mod->symtab[symnum].st_info;
2022 strncpy(namebuf,
2023 mod->strtab + mod->symtab[symnum].st_name,
2024 127);
2025 up(&module_mutex);
2026 return mod;
2028 symnum -= mod->num_symtab;
2030 up(&module_mutex);
2031 return NULL;
2034 static unsigned long mod_find_symname(struct module *mod, const char *name)
2036 unsigned int i;
2038 for (i = 0; i < mod->num_symtab; i++)
2039 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2040 return mod->symtab[i].st_value;
2041 return 0;
2044 /* Look for this name: can be of form module:name. */
2045 unsigned long module_kallsyms_lookup_name(const char *name)
2047 struct module *mod;
2048 char *colon;
2049 unsigned long ret = 0;
2051 /* Don't lock: we're in enough trouble already. */
2052 if ((colon = strchr(name, ':')) != NULL) {
2053 *colon = '\0';
2054 if ((mod = find_module(name)) != NULL)
2055 ret = mod_find_symname(mod, colon+1);
2056 *colon = ':';
2057 } else {
2058 list_for_each_entry(mod, &modules, list)
2059 if ((ret = mod_find_symname(mod, name)) != 0)
2060 break;
2062 return ret;
2064 #endif /* CONFIG_KALLSYMS */
2066 /* Called by the /proc file system to return a list of modules. */
2067 static void *m_start(struct seq_file *m, loff_t *pos)
2069 struct list_head *i;
2070 loff_t n = 0;
2072 down(&module_mutex);
2073 list_for_each(i, &modules) {
2074 if (n++ == *pos)
2075 break;
2077 if (i == &modules)
2078 return NULL;
2079 return i;
2082 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2084 struct list_head *i = p;
2085 (*pos)++;
2086 if (i->next == &modules)
2087 return NULL;
2088 return i->next;
2091 static void m_stop(struct seq_file *m, void *p)
2093 up(&module_mutex);
2096 static int m_show(struct seq_file *m, void *p)
2098 struct module *mod = list_entry(p, struct module, list);
2099 seq_printf(m, "%s %lu",
2100 mod->name, mod->init_size + mod->core_size);
2101 print_unload_info(m, mod);
2103 /* Informative for users. */
2104 seq_printf(m, " %s",
2105 mod->state == MODULE_STATE_GOING ? "Unloading":
2106 mod->state == MODULE_STATE_COMING ? "Loading":
2107 "Live");
2108 /* Used by oprofile and other similar tools. */
2109 seq_printf(m, " 0x%p", mod->module_core);
2111 seq_printf(m, "\n");
2112 return 0;
2115 /* Format: modulename size refcount deps address
2117 Where refcount is a number or -, and deps is a comma-separated list
2118 of depends or -.
2120 struct seq_operations modules_op = {
2121 .start = m_start,
2122 .next = m_next,
2123 .stop = m_stop,
2124 .show = m_show
2127 /* Given an address, look for it in the module exception tables. */
2128 const struct exception_table_entry *search_module_extables(unsigned long addr)
2130 unsigned long flags;
2131 const struct exception_table_entry *e = NULL;
2132 struct module *mod;
2134 spin_lock_irqsave(&modlist_lock, flags);
2135 list_for_each_entry(mod, &modules, list) {
2136 if (mod->num_exentries == 0)
2137 continue;
2139 e = search_extable(mod->extable,
2140 mod->extable + mod->num_exentries - 1,
2141 addr);
2142 if (e)
2143 break;
2145 spin_unlock_irqrestore(&modlist_lock, flags);
2147 /* Now, if we found one, we are running inside it now, hence
2148 we cannot unload the module, hence no refcnt needed. */
2149 return e;
2152 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2153 struct module *__module_text_address(unsigned long addr)
2155 struct module *mod;
2157 list_for_each_entry(mod, &modules, list)
2158 if (within(addr, mod->module_init, mod->init_text_size)
2159 || within(addr, mod->module_core, mod->core_text_size))
2160 return mod;
2161 return NULL;
2164 struct module *module_text_address(unsigned long addr)
2166 struct module *mod;
2167 unsigned long flags;
2169 spin_lock_irqsave(&modlist_lock, flags);
2170 mod = __module_text_address(addr);
2171 spin_unlock_irqrestore(&modlist_lock, flags);
2173 return mod;
2176 /* Don't grab lock, we're oopsing. */
2177 void print_modules(void)
2179 struct module *mod;
2181 printk("Modules linked in:");
2182 list_for_each_entry(mod, &modules, list)
2183 printk(" %s", mod->name);
2184 printk("\n");
2187 void module_add_driver(struct module *mod, struct device_driver *drv)
2189 if (!mod || !drv)
2190 return;
2192 /* Don't check return code; this call is idempotent */
2193 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2195 EXPORT_SYMBOL(module_add_driver);
2197 void module_remove_driver(struct device_driver *drv)
2199 if (!drv)
2200 return;
2201 sysfs_remove_link(&drv->kobj, "module");
2203 EXPORT_SYMBOL(module_remove_driver);
2205 #ifdef CONFIG_MODVERSIONS
2206 /* Generate the signature for struct module here, too, for modversions. */
2207 void struct_module(struct module *mod) { return; }
2208 EXPORT_SYMBOL(struct_module);
2209 #endif