[PATCH] crc16: remove w1 specific comments.
[linux/fpc-iii.git] / kernel / module.c
blob4b39d3793c720673452b6d8be18ade674360131a
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,
254 const char *name)
256 unsigned long extra;
257 unsigned int i;
258 void *ptr;
260 if (align > SMP_CACHE_BYTES) {
261 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
262 name, align, SMP_CACHE_BYTES);
263 align = SMP_CACHE_BYTES;
266 ptr = __per_cpu_start;
267 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
268 /* Extra for alignment requirement. */
269 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
270 BUG_ON(i == 0 && extra != 0);
272 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
273 continue;
275 /* Transfer extra to previous block. */
276 if (pcpu_size[i-1] < 0)
277 pcpu_size[i-1] -= extra;
278 else
279 pcpu_size[i-1] += extra;
280 pcpu_size[i] -= extra;
281 ptr += extra;
283 /* Split block if warranted */
284 if (pcpu_size[i] - size > sizeof(unsigned long))
285 if (!split_block(i, size))
286 return NULL;
288 /* Mark allocated */
289 pcpu_size[i] = -pcpu_size[i];
290 return ptr;
293 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
294 size);
295 return NULL;
298 static void percpu_modfree(void *freeme)
300 unsigned int i;
301 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
303 /* First entry is core kernel percpu data. */
304 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
305 if (ptr == freeme) {
306 pcpu_size[i] = -pcpu_size[i];
307 goto free;
310 BUG();
312 free:
313 /* Merge with previous? */
314 if (pcpu_size[i-1] >= 0) {
315 pcpu_size[i-1] += pcpu_size[i];
316 pcpu_num_used--;
317 memmove(&pcpu_size[i], &pcpu_size[i+1],
318 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
319 i--;
321 /* Merge with next? */
322 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
323 pcpu_size[i] += pcpu_size[i+1];
324 pcpu_num_used--;
325 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
326 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
330 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
331 Elf_Shdr *sechdrs,
332 const char *secstrings)
334 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
337 static int percpu_modinit(void)
339 pcpu_num_used = 2;
340 pcpu_num_allocated = 2;
341 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
342 GFP_KERNEL);
343 /* Static in-kernel percpu data (used). */
344 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
345 /* Free room. */
346 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
347 if (pcpu_size[1] < 0) {
348 printk(KERN_ERR "No per-cpu room for modules.\n");
349 pcpu_num_used = 1;
352 return 0;
354 __initcall(percpu_modinit);
355 #else /* ... !CONFIG_SMP */
356 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
357 const char *name)
359 return NULL;
361 static inline void percpu_modfree(void *pcpuptr)
363 BUG();
365 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
366 Elf_Shdr *sechdrs,
367 const char *secstrings)
369 return 0;
371 static inline void percpu_modcopy(void *pcpudst, const void *src,
372 unsigned long size)
374 /* pcpusec should be 0, and size of that section should be 0. */
375 BUG_ON(size != 0);
377 #endif /* CONFIG_SMP */
379 #ifdef CONFIG_MODULE_UNLOAD
380 #define MODINFO_ATTR(field) \
381 static void setup_modinfo_##field(struct module *mod, const char *s) \
383 mod->field = kstrdup(s, GFP_KERNEL); \
385 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
386 struct module *mod, char *buffer) \
388 return sprintf(buffer, "%s\n", mod->field); \
390 static int modinfo_##field##_exists(struct module *mod) \
392 return mod->field != NULL; \
394 static void free_modinfo_##field(struct module *mod) \
396 kfree(mod->field); \
397 mod->field = NULL; \
399 static struct module_attribute modinfo_##field = { \
400 .attr = { .name = __stringify(field), .mode = 0444, \
401 .owner = THIS_MODULE }, \
402 .show = show_modinfo_##field, \
403 .setup = setup_modinfo_##field, \
404 .test = modinfo_##field##_exists, \
405 .free = free_modinfo_##field, \
408 MODINFO_ATTR(version);
409 MODINFO_ATTR(srcversion);
411 static struct module_attribute *modinfo_attrs[] = {
412 &modinfo_version,
413 &modinfo_srcversion,
414 NULL,
417 /* Init the unload section of the module. */
418 static void module_unload_init(struct module *mod)
420 unsigned int i;
422 INIT_LIST_HEAD(&mod->modules_which_use_me);
423 for (i = 0; i < NR_CPUS; i++)
424 local_set(&mod->ref[i].count, 0);
425 /* Hold reference count during initialization. */
426 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
427 /* Backwards compatibility macros put refcount during init. */
428 mod->waiter = current;
431 /* modules using other modules */
432 struct module_use
434 struct list_head list;
435 struct module *module_which_uses;
438 /* Does a already use b? */
439 static int already_uses(struct module *a, struct module *b)
441 struct module_use *use;
443 list_for_each_entry(use, &b->modules_which_use_me, list) {
444 if (use->module_which_uses == a) {
445 DEBUGP("%s uses %s!\n", a->name, b->name);
446 return 1;
449 DEBUGP("%s does not use %s!\n", a->name, b->name);
450 return 0;
453 /* Module a uses b */
454 static int use_module(struct module *a, struct module *b)
456 struct module_use *use;
457 if (b == NULL || already_uses(a, b)) return 1;
459 if (!strong_try_module_get(b))
460 return 0;
462 DEBUGP("Allocating new usage for %s.\n", a->name);
463 use = kmalloc(sizeof(*use), GFP_ATOMIC);
464 if (!use) {
465 printk("%s: out of memory loading\n", a->name);
466 module_put(b);
467 return 0;
470 use->module_which_uses = a;
471 list_add(&use->list, &b->modules_which_use_me);
472 return 1;
475 /* Clear the unload stuff of the module. */
476 static void module_unload_free(struct module *mod)
478 struct module *i;
480 list_for_each_entry(i, &modules, list) {
481 struct module_use *use;
483 list_for_each_entry(use, &i->modules_which_use_me, list) {
484 if (use->module_which_uses == mod) {
485 DEBUGP("%s unusing %s\n", mod->name, i->name);
486 module_put(i);
487 list_del(&use->list);
488 kfree(use);
489 /* There can be at most one match. */
490 break;
496 #ifdef CONFIG_MODULE_FORCE_UNLOAD
497 static inline int try_force(unsigned int flags)
499 int ret = (flags & O_TRUNC);
500 if (ret)
501 tainted |= TAINT_FORCED_MODULE;
502 return ret;
504 #else
505 static inline int try_force(unsigned int flags)
507 return 0;
509 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
511 struct stopref
513 struct module *mod;
514 int flags;
515 int *forced;
518 /* Whole machine is stopped with interrupts off when this runs. */
519 static int __try_stop_module(void *_sref)
521 struct stopref *sref = _sref;
523 /* If it's not unused, quit unless we are told to block. */
524 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
525 if (!(*sref->forced = try_force(sref->flags)))
526 return -EWOULDBLOCK;
529 /* Mark it as dying. */
530 sref->mod->state = MODULE_STATE_GOING;
531 return 0;
534 static int try_stop_module(struct module *mod, int flags, int *forced)
536 struct stopref sref = { mod, flags, forced };
538 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
541 unsigned int module_refcount(struct module *mod)
543 unsigned int i, total = 0;
545 for (i = 0; i < NR_CPUS; i++)
546 total += local_read(&mod->ref[i].count);
547 return total;
549 EXPORT_SYMBOL(module_refcount);
551 /* This exists whether we can unload or not */
552 static void free_module(struct module *mod);
554 static void wait_for_zero_refcount(struct module *mod)
556 /* Since we might sleep for some time, drop the semaphore first */
557 up(&module_mutex);
558 for (;;) {
559 DEBUGP("Looking at refcount...\n");
560 set_current_state(TASK_UNINTERRUPTIBLE);
561 if (module_refcount(mod) == 0)
562 break;
563 schedule();
565 current->state = TASK_RUNNING;
566 down(&module_mutex);
569 asmlinkage long
570 sys_delete_module(const char __user *name_user, unsigned int flags)
572 struct module *mod;
573 char name[MODULE_NAME_LEN];
574 int ret, forced = 0;
576 if (!capable(CAP_SYS_MODULE))
577 return -EPERM;
579 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
580 return -EFAULT;
581 name[MODULE_NAME_LEN-1] = '\0';
583 if (down_interruptible(&module_mutex) != 0)
584 return -EINTR;
586 mod = find_module(name);
587 if (!mod) {
588 ret = -ENOENT;
589 goto out;
592 if (!list_empty(&mod->modules_which_use_me)) {
593 /* Other modules depend on us: get rid of them first. */
594 ret = -EWOULDBLOCK;
595 goto out;
598 /* Doing init or already dying? */
599 if (mod->state != MODULE_STATE_LIVE) {
600 /* FIXME: if (force), slam module count and wake up
601 waiter --RR */
602 DEBUGP("%s already dying\n", mod->name);
603 ret = -EBUSY;
604 goto out;
607 /* If it has an init func, it must have an exit func to unload */
608 if ((mod->init != NULL && mod->exit == NULL)
609 || mod->unsafe) {
610 forced = try_force(flags);
611 if (!forced) {
612 /* This module can't be removed */
613 ret = -EBUSY;
614 goto out;
618 /* Set this up before setting mod->state */
619 mod->waiter = current;
621 /* Stop the machine so refcounts can't move and disable module. */
622 ret = try_stop_module(mod, flags, &forced);
623 if (ret != 0)
624 goto out;
626 /* Never wait if forced. */
627 if (!forced && module_refcount(mod) != 0)
628 wait_for_zero_refcount(mod);
630 /* Final destruction now noone is using it. */
631 if (mod->exit != NULL) {
632 up(&module_mutex);
633 mod->exit();
634 down(&module_mutex);
636 free_module(mod);
638 out:
639 up(&module_mutex);
640 return ret;
643 static void print_unload_info(struct seq_file *m, struct module *mod)
645 struct module_use *use;
646 int printed_something = 0;
648 seq_printf(m, " %u ", module_refcount(mod));
650 /* Always include a trailing , so userspace can differentiate
651 between this and the old multi-field proc format. */
652 list_for_each_entry(use, &mod->modules_which_use_me, list) {
653 printed_something = 1;
654 seq_printf(m, "%s,", use->module_which_uses->name);
657 if (mod->unsafe) {
658 printed_something = 1;
659 seq_printf(m, "[unsafe],");
662 if (mod->init != NULL && mod->exit == NULL) {
663 printed_something = 1;
664 seq_printf(m, "[permanent],");
667 if (!printed_something)
668 seq_printf(m, "-");
671 void __symbol_put(const char *symbol)
673 struct module *owner;
674 unsigned long flags;
675 const unsigned long *crc;
677 spin_lock_irqsave(&modlist_lock, flags);
678 if (!__find_symbol(symbol, &owner, &crc, 1))
679 BUG();
680 module_put(owner);
681 spin_unlock_irqrestore(&modlist_lock, flags);
683 EXPORT_SYMBOL(__symbol_put);
685 void symbol_put_addr(void *addr)
687 unsigned long flags;
689 spin_lock_irqsave(&modlist_lock, flags);
690 if (!kernel_text_address((unsigned long)addr))
691 BUG();
693 module_put(module_text_address((unsigned long)addr));
694 spin_unlock_irqrestore(&modlist_lock, flags);
696 EXPORT_SYMBOL_GPL(symbol_put_addr);
698 static ssize_t show_refcnt(struct module_attribute *mattr,
699 struct module *mod, char *buffer)
701 /* sysfs holds a reference */
702 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
705 static struct module_attribute refcnt = {
706 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
707 .show = show_refcnt,
710 #else /* !CONFIG_MODULE_UNLOAD */
711 static void print_unload_info(struct seq_file *m, struct module *mod)
713 /* We don't know the usage count, or what modules are using. */
714 seq_printf(m, " - -");
717 static inline void module_unload_free(struct module *mod)
721 static inline int use_module(struct module *a, struct module *b)
723 return strong_try_module_get(b);
726 static inline void module_unload_init(struct module *mod)
729 #endif /* CONFIG_MODULE_UNLOAD */
731 #ifdef CONFIG_OBSOLETE_MODPARM
732 /* Bounds checking done below */
733 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
735 strcpy(kp->arg, val);
736 return 0;
739 static int set_obsolete(const char *val, struct kernel_param *kp)
741 unsigned int min, max;
742 unsigned int size, maxsize;
743 int dummy;
744 char *endp;
745 const char *p;
746 struct obsolete_modparm *obsparm = kp->arg;
748 if (!val) {
749 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
750 return -EINVAL;
753 /* type is: [min[-max]]{b,h,i,l,s} */
754 p = obsparm->type;
755 min = simple_strtol(p, &endp, 10);
756 if (endp == obsparm->type)
757 min = max = 1;
758 else if (*endp == '-') {
759 p = endp+1;
760 max = simple_strtol(p, &endp, 10);
761 } else
762 max = min;
763 switch (*endp) {
764 case 'b':
765 return param_array(kp->name, val, min, max, obsparm->addr,
766 1, param_set_byte, &dummy);
767 case 'h':
768 return param_array(kp->name, val, min, max, obsparm->addr,
769 sizeof(short), param_set_short, &dummy);
770 case 'i':
771 return param_array(kp->name, val, min, max, obsparm->addr,
772 sizeof(int), param_set_int, &dummy);
773 case 'l':
774 return param_array(kp->name, val, min, max, obsparm->addr,
775 sizeof(long), param_set_long, &dummy);
776 case 's':
777 return param_array(kp->name, val, min, max, obsparm->addr,
778 sizeof(char *), param_set_charp, &dummy);
780 case 'c':
781 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
782 and the decl is "char xxx[5][50];" */
783 p = endp+1;
784 maxsize = simple_strtol(p, &endp, 10);
785 /* We check lengths here (yes, this is a hack). */
786 p = val;
787 while (p[size = strcspn(p, ",")]) {
788 if (size >= maxsize)
789 goto oversize;
790 p += size+1;
792 if (size >= maxsize)
793 goto oversize;
794 return param_array(kp->name, val, min, max, obsparm->addr,
795 maxsize, obsparm_copy_string, &dummy);
797 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
798 return -EINVAL;
799 oversize:
800 printk(KERN_ERR
801 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
802 return -EINVAL;
805 static int obsolete_params(const char *name,
806 char *args,
807 struct obsolete_modparm obsparm[],
808 unsigned int num,
809 Elf_Shdr *sechdrs,
810 unsigned int symindex,
811 const char *strtab)
813 struct kernel_param *kp;
814 unsigned int i;
815 int ret;
817 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
818 if (!kp)
819 return -ENOMEM;
821 for (i = 0; i < num; i++) {
822 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
824 snprintf(sym_name, sizeof(sym_name), "%s%s",
825 MODULE_SYMBOL_PREFIX, obsparm[i].name);
827 kp[i].name = obsparm[i].name;
828 kp[i].perm = 000;
829 kp[i].set = set_obsolete;
830 kp[i].get = NULL;
831 obsparm[i].addr
832 = (void *)find_local_symbol(sechdrs, symindex, strtab,
833 sym_name);
834 if (!obsparm[i].addr) {
835 printk("%s: falsely claims to have parameter %s\n",
836 name, obsparm[i].name);
837 ret = -EINVAL;
838 goto out;
840 kp[i].arg = &obsparm[i];
843 ret = parse_args(name, args, kp, num, NULL);
844 out:
845 kfree(kp);
846 return ret;
848 #else
849 static int obsolete_params(const char *name,
850 char *args,
851 struct obsolete_modparm obsparm[],
852 unsigned int num,
853 Elf_Shdr *sechdrs,
854 unsigned int symindex,
855 const char *strtab)
857 if (num != 0)
858 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
859 name);
860 return 0;
862 #endif /* CONFIG_OBSOLETE_MODPARM */
864 static const char vermagic[] = VERMAGIC_STRING;
866 #ifdef CONFIG_MODVERSIONS
867 static int check_version(Elf_Shdr *sechdrs,
868 unsigned int versindex,
869 const char *symname,
870 struct module *mod,
871 const unsigned long *crc)
873 unsigned int i, num_versions;
874 struct modversion_info *versions;
876 /* Exporting module didn't supply crcs? OK, we're already tainted. */
877 if (!crc)
878 return 1;
880 versions = (void *) sechdrs[versindex].sh_addr;
881 num_versions = sechdrs[versindex].sh_size
882 / sizeof(struct modversion_info);
884 for (i = 0; i < num_versions; i++) {
885 if (strcmp(versions[i].name, symname) != 0)
886 continue;
888 if (versions[i].crc == *crc)
889 return 1;
890 printk("%s: disagrees about version of symbol %s\n",
891 mod->name, symname);
892 DEBUGP("Found checksum %lX vs module %lX\n",
893 *crc, versions[i].crc);
894 return 0;
896 /* Not in module's version table. OK, but that taints the kernel. */
897 if (!(tainted & TAINT_FORCED_MODULE)) {
898 printk("%s: no version for \"%s\" found: kernel tainted.\n",
899 mod->name, symname);
900 tainted |= TAINT_FORCED_MODULE;
902 return 1;
905 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
906 unsigned int versindex,
907 struct module *mod)
909 const unsigned long *crc;
910 struct module *owner;
912 if (!__find_symbol("struct_module", &owner, &crc, 1))
913 BUG();
914 return check_version(sechdrs, versindex, "struct_module", mod,
915 crc);
918 /* First part is kernel version, which we ignore. */
919 static inline int same_magic(const char *amagic, const char *bmagic)
921 amagic += strcspn(amagic, " ");
922 bmagic += strcspn(bmagic, " ");
923 return strcmp(amagic, bmagic) == 0;
925 #else
926 static inline int check_version(Elf_Shdr *sechdrs,
927 unsigned int versindex,
928 const char *symname,
929 struct module *mod,
930 const unsigned long *crc)
932 return 1;
935 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
936 unsigned int versindex,
937 struct module *mod)
939 return 1;
942 static inline int same_magic(const char *amagic, const char *bmagic)
944 return strcmp(amagic, bmagic) == 0;
946 #endif /* CONFIG_MODVERSIONS */
948 /* Resolve a symbol for this module. I.e. if we find one, record usage.
949 Must be holding module_mutex. */
950 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
951 unsigned int versindex,
952 const char *name,
953 struct module *mod)
955 struct module *owner;
956 unsigned long ret;
957 const unsigned long *crc;
959 spin_lock_irq(&modlist_lock);
960 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
961 if (ret) {
962 /* use_module can fail due to OOM, or module unloading */
963 if (!check_version(sechdrs, versindex, name, mod, crc) ||
964 !use_module(mod, owner))
965 ret = 0;
967 spin_unlock_irq(&modlist_lock);
968 return ret;
973 * /sys/module/foo/sections stuff
974 * J. Corbet <corbet@lwn.net>
976 #ifdef CONFIG_KALLSYMS
977 static ssize_t module_sect_show(struct module_attribute *mattr,
978 struct module *mod, char *buf)
980 struct module_sect_attr *sattr =
981 container_of(mattr, struct module_sect_attr, mattr);
982 return sprintf(buf, "0x%lx\n", sattr->address);
985 static void add_sect_attrs(struct module *mod, unsigned int nsect,
986 char *secstrings, Elf_Shdr *sechdrs)
988 unsigned int nloaded = 0, i, size[2];
989 struct module_sect_attrs *sect_attrs;
990 struct module_sect_attr *sattr;
991 struct attribute **gattr;
993 /* Count loaded sections and allocate structures */
994 for (i = 0; i < nsect; i++)
995 if (sechdrs[i].sh_flags & SHF_ALLOC)
996 nloaded++;
997 size[0] = ALIGN(sizeof(*sect_attrs)
998 + nloaded * sizeof(sect_attrs->attrs[0]),
999 sizeof(sect_attrs->grp.attrs[0]));
1000 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1001 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1002 return;
1004 /* Setup section attributes. */
1005 sect_attrs->grp.name = "sections";
1006 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1008 sattr = &sect_attrs->attrs[0];
1009 gattr = &sect_attrs->grp.attrs[0];
1010 for (i = 0; i < nsect; i++) {
1011 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1012 continue;
1013 sattr->address = sechdrs[i].sh_addr;
1014 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1015 MODULE_SECT_NAME_LEN);
1016 sattr->mattr.show = module_sect_show;
1017 sattr->mattr.store = NULL;
1018 sattr->mattr.attr.name = sattr->name;
1019 sattr->mattr.attr.owner = mod;
1020 sattr->mattr.attr.mode = S_IRUGO;
1021 *(gattr++) = &(sattr++)->mattr.attr;
1023 *gattr = NULL;
1025 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1026 goto out;
1028 mod->sect_attrs = sect_attrs;
1029 return;
1030 out:
1031 kfree(sect_attrs);
1034 static void remove_sect_attrs(struct module *mod)
1036 if (mod->sect_attrs) {
1037 sysfs_remove_group(&mod->mkobj.kobj,
1038 &mod->sect_attrs->grp);
1039 /* We are positive that no one is using any sect attrs
1040 * at this point. Deallocate immediately. */
1041 kfree(mod->sect_attrs);
1042 mod->sect_attrs = NULL;
1047 #else
1048 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1049 char *sectstrings, Elf_Shdr *sechdrs)
1053 static inline void remove_sect_attrs(struct module *mod)
1056 #endif /* CONFIG_KALLSYMS */
1059 #ifdef CONFIG_MODULE_UNLOAD
1060 static inline int module_add_refcnt_attr(struct module *mod)
1062 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1064 static void module_remove_refcnt_attr(struct module *mod)
1066 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1068 #else
1069 static inline int module_add_refcnt_attr(struct module *mod)
1071 return 0;
1073 static void module_remove_refcnt_attr(struct module *mod)
1076 #endif
1078 #ifdef CONFIG_MODULE_UNLOAD
1079 static int module_add_modinfo_attrs(struct module *mod)
1081 struct module_attribute *attr;
1082 int error = 0;
1083 int i;
1085 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1086 if (!attr->test ||
1087 (attr->test && attr->test(mod)))
1088 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1090 return error;
1093 static void module_remove_modinfo_attrs(struct module *mod)
1095 struct module_attribute *attr;
1096 int i;
1098 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1099 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1100 attr->free(mod);
1103 #endif
1105 static int mod_sysfs_setup(struct module *mod,
1106 struct kernel_param *kparam,
1107 unsigned int num_params)
1109 int err;
1111 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1112 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1113 if (err)
1114 goto out;
1115 kobj_set_kset_s(&mod->mkobj, module_subsys);
1116 mod->mkobj.mod = mod;
1117 err = kobject_register(&mod->mkobj.kobj);
1118 if (err)
1119 goto out;
1121 err = module_add_refcnt_attr(mod);
1122 if (err)
1123 goto out_unreg;
1125 err = module_param_sysfs_setup(mod, kparam, num_params);
1126 if (err)
1127 goto out_unreg;
1129 #ifdef CONFIG_MODULE_UNLOAD
1130 err = module_add_modinfo_attrs(mod);
1131 if (err)
1132 goto out_unreg;
1133 #endif
1135 return 0;
1137 out_unreg:
1138 kobject_unregister(&mod->mkobj.kobj);
1139 out:
1140 return err;
1143 static void mod_kobject_remove(struct module *mod)
1145 #ifdef CONFIG_MODULE_UNLOAD
1146 module_remove_modinfo_attrs(mod);
1147 #endif
1148 module_remove_refcnt_attr(mod);
1149 module_param_sysfs_remove(mod);
1151 kobject_unregister(&mod->mkobj.kobj);
1155 * unlink the module with the whole machine is stopped with interrupts off
1156 * - this defends against kallsyms not taking locks
1158 static int __unlink_module(void *_mod)
1160 struct module *mod = _mod;
1161 list_del(&mod->list);
1162 return 0;
1165 /* Free a module, remove from lists, etc (must hold module mutex). */
1166 static void free_module(struct module *mod)
1168 /* Delete from various lists */
1169 stop_machine_run(__unlink_module, mod, NR_CPUS);
1170 remove_sect_attrs(mod);
1171 mod_kobject_remove(mod);
1173 /* Arch-specific cleanup. */
1174 module_arch_cleanup(mod);
1176 /* Module unload stuff */
1177 module_unload_free(mod);
1179 /* This may be NULL, but that's OK */
1180 module_free(mod, mod->module_init);
1181 kfree(mod->args);
1182 if (mod->percpu)
1183 percpu_modfree(mod->percpu);
1185 /* Finally, free the core (containing the module structure) */
1186 module_free(mod, mod->module_core);
1189 void *__symbol_get(const char *symbol)
1191 struct module *owner;
1192 unsigned long value, flags;
1193 const unsigned long *crc;
1195 spin_lock_irqsave(&modlist_lock, flags);
1196 value = __find_symbol(symbol, &owner, &crc, 1);
1197 if (value && !strong_try_module_get(owner))
1198 value = 0;
1199 spin_unlock_irqrestore(&modlist_lock, flags);
1201 return (void *)value;
1203 EXPORT_SYMBOL_GPL(__symbol_get);
1205 /* Change all symbols so that sh_value encodes the pointer directly. */
1206 static int simplify_symbols(Elf_Shdr *sechdrs,
1207 unsigned int symindex,
1208 const char *strtab,
1209 unsigned int versindex,
1210 unsigned int pcpuindex,
1211 struct module *mod)
1213 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1214 unsigned long secbase;
1215 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1216 int ret = 0;
1218 for (i = 1; i < n; i++) {
1219 switch (sym[i].st_shndx) {
1220 case SHN_COMMON:
1221 /* We compiled with -fno-common. These are not
1222 supposed to happen. */
1223 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1224 printk("%s: please compile with -fno-common\n",
1225 mod->name);
1226 ret = -ENOEXEC;
1227 break;
1229 case SHN_ABS:
1230 /* Don't need to do anything */
1231 DEBUGP("Absolute symbol: 0x%08lx\n",
1232 (long)sym[i].st_value);
1233 break;
1235 case SHN_UNDEF:
1236 sym[i].st_value
1237 = resolve_symbol(sechdrs, versindex,
1238 strtab + sym[i].st_name, mod);
1240 /* Ok if resolved. */
1241 if (sym[i].st_value != 0)
1242 break;
1243 /* Ok if weak. */
1244 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1245 break;
1247 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1248 mod->name, strtab + sym[i].st_name);
1249 ret = -ENOENT;
1250 break;
1252 default:
1253 /* Divert to percpu allocation if a percpu var. */
1254 if (sym[i].st_shndx == pcpuindex)
1255 secbase = (unsigned long)mod->percpu;
1256 else
1257 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1258 sym[i].st_value += secbase;
1259 break;
1263 return ret;
1266 /* Update size with this section: return offset. */
1267 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1269 long ret;
1271 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1272 *size = ret + sechdr->sh_size;
1273 return ret;
1276 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1277 might -- code, read-only data, read-write data, small data. Tally
1278 sizes, and place the offsets into sh_entsize fields: high bit means it
1279 belongs in init. */
1280 static void layout_sections(struct module *mod,
1281 const Elf_Ehdr *hdr,
1282 Elf_Shdr *sechdrs,
1283 const char *secstrings)
1285 static unsigned long const masks[][2] = {
1286 /* NOTE: all executable code must be the first section
1287 * in this array; otherwise modify the text_size
1288 * finder in the two loops below */
1289 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1290 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1291 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1292 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1294 unsigned int m, i;
1296 for (i = 0; i < hdr->e_shnum; i++)
1297 sechdrs[i].sh_entsize = ~0UL;
1299 DEBUGP("Core section allocation order:\n");
1300 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1301 for (i = 0; i < hdr->e_shnum; ++i) {
1302 Elf_Shdr *s = &sechdrs[i];
1304 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1305 || (s->sh_flags & masks[m][1])
1306 || s->sh_entsize != ~0UL
1307 || strncmp(secstrings + s->sh_name,
1308 ".init", 5) == 0)
1309 continue;
1310 s->sh_entsize = get_offset(&mod->core_size, s);
1311 DEBUGP("\t%s\n", secstrings + s->sh_name);
1313 if (m == 0)
1314 mod->core_text_size = mod->core_size;
1317 DEBUGP("Init section allocation order:\n");
1318 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1319 for (i = 0; i < hdr->e_shnum; ++i) {
1320 Elf_Shdr *s = &sechdrs[i];
1322 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1323 || (s->sh_flags & masks[m][1])
1324 || s->sh_entsize != ~0UL
1325 || strncmp(secstrings + s->sh_name,
1326 ".init", 5) != 0)
1327 continue;
1328 s->sh_entsize = (get_offset(&mod->init_size, s)
1329 | INIT_OFFSET_MASK);
1330 DEBUGP("\t%s\n", secstrings + s->sh_name);
1332 if (m == 0)
1333 mod->init_text_size = mod->init_size;
1337 static inline int license_is_gpl_compatible(const char *license)
1339 return (strcmp(license, "GPL") == 0
1340 || strcmp(license, "GPL v2") == 0
1341 || strcmp(license, "GPL and additional rights") == 0
1342 || strcmp(license, "Dual BSD/GPL") == 0
1343 || strcmp(license, "Dual MPL/GPL") == 0);
1346 static void set_license(struct module *mod, const char *license)
1348 if (!license)
1349 license = "unspecified";
1351 mod->license_gplok = license_is_gpl_compatible(license);
1352 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1353 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1354 mod->name, license);
1355 tainted |= TAINT_PROPRIETARY_MODULE;
1359 /* Parse tag=value strings from .modinfo section */
1360 static char *next_string(char *string, unsigned long *secsize)
1362 /* Skip non-zero chars */
1363 while (string[0]) {
1364 string++;
1365 if ((*secsize)-- <= 1)
1366 return NULL;
1369 /* Skip any zero padding. */
1370 while (!string[0]) {
1371 string++;
1372 if ((*secsize)-- <= 1)
1373 return NULL;
1375 return string;
1378 static char *get_modinfo(Elf_Shdr *sechdrs,
1379 unsigned int info,
1380 const char *tag)
1382 char *p;
1383 unsigned int taglen = strlen(tag);
1384 unsigned long size = sechdrs[info].sh_size;
1386 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1387 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1388 return p + taglen + 1;
1390 return NULL;
1393 #ifdef CONFIG_MODULE_UNLOAD
1394 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1395 unsigned int infoindex)
1397 struct module_attribute *attr;
1398 int i;
1400 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1401 if (attr->setup)
1402 attr->setup(mod,
1403 get_modinfo(sechdrs,
1404 infoindex,
1405 attr->attr.name));
1408 #endif
1410 #ifdef CONFIG_KALLSYMS
1411 int is_exported(const char *name, const struct module *mod)
1413 unsigned int i;
1415 if (!mod) {
1416 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1417 if (strcmp(__start___ksymtab[i].name, name) == 0)
1418 return 1;
1419 return 0;
1421 for (i = 0; i < mod->num_syms; i++)
1422 if (strcmp(mod->syms[i].name, name) == 0)
1423 return 1;
1424 return 0;
1427 /* As per nm */
1428 static char elf_type(const Elf_Sym *sym,
1429 Elf_Shdr *sechdrs,
1430 const char *secstrings,
1431 struct module *mod)
1433 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1434 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1435 return 'v';
1436 else
1437 return 'w';
1439 if (sym->st_shndx == SHN_UNDEF)
1440 return 'U';
1441 if (sym->st_shndx == SHN_ABS)
1442 return 'a';
1443 if (sym->st_shndx >= SHN_LORESERVE)
1444 return '?';
1445 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1446 return 't';
1447 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1448 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1449 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1450 return 'r';
1451 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1452 return 'g';
1453 else
1454 return 'd';
1456 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1457 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1458 return 's';
1459 else
1460 return 'b';
1462 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1463 ".debug", strlen(".debug")) == 0)
1464 return 'n';
1465 return '?';
1468 static void add_kallsyms(struct module *mod,
1469 Elf_Shdr *sechdrs,
1470 unsigned int symindex,
1471 unsigned int strindex,
1472 const char *secstrings)
1474 unsigned int i;
1476 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1477 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1478 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1480 /* Set types up while we still have access to sections. */
1481 for (i = 0; i < mod->num_symtab; i++)
1482 mod->symtab[i].st_info
1483 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1485 #else
1486 static inline void add_kallsyms(struct module *mod,
1487 Elf_Shdr *sechdrs,
1488 unsigned int symindex,
1489 unsigned int strindex,
1490 const char *secstrings)
1493 #endif /* CONFIG_KALLSYMS */
1495 /* Allocate and load the module: note that size of section 0 is always
1496 zero, and we rely on this for optional sections. */
1497 static struct module *load_module(void __user *umod,
1498 unsigned long len,
1499 const char __user *uargs)
1501 Elf_Ehdr *hdr;
1502 Elf_Shdr *sechdrs;
1503 char *secstrings, *args, *modmagic, *strtab = NULL;
1504 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1505 exportindex, modindex, obsparmindex, infoindex, gplindex,
1506 crcindex, gplcrcindex, versindex, pcpuindex;
1507 long arglen;
1508 struct module *mod;
1509 long err = 0;
1510 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1511 struct exception_table_entry *extable;
1512 mm_segment_t old_fs;
1514 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1515 umod, len, uargs);
1516 if (len < sizeof(*hdr))
1517 return ERR_PTR(-ENOEXEC);
1519 /* Suck in entire file: we'll want most of it. */
1520 /* vmalloc barfs on "unusual" numbers. Check here */
1521 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1522 return ERR_PTR(-ENOMEM);
1523 if (copy_from_user(hdr, umod, len) != 0) {
1524 err = -EFAULT;
1525 goto free_hdr;
1528 /* Sanity checks against insmoding binaries or wrong arch,
1529 weird elf version */
1530 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1531 || hdr->e_type != ET_REL
1532 || !elf_check_arch(hdr)
1533 || hdr->e_shentsize != sizeof(*sechdrs)) {
1534 err = -ENOEXEC;
1535 goto free_hdr;
1538 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1539 goto truncated;
1541 /* Convenience variables */
1542 sechdrs = (void *)hdr + hdr->e_shoff;
1543 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1544 sechdrs[0].sh_addr = 0;
1546 for (i = 1; i < hdr->e_shnum; i++) {
1547 if (sechdrs[i].sh_type != SHT_NOBITS
1548 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1549 goto truncated;
1551 /* Mark all sections sh_addr with their address in the
1552 temporary image. */
1553 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1555 /* Internal symbols and strings. */
1556 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1557 symindex = i;
1558 strindex = sechdrs[i].sh_link;
1559 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1561 #ifndef CONFIG_MODULE_UNLOAD
1562 /* Don't load .exit sections */
1563 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1564 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1565 #endif
1568 modindex = find_sec(hdr, sechdrs, secstrings,
1569 ".gnu.linkonce.this_module");
1570 if (!modindex) {
1571 printk(KERN_WARNING "No module found in object\n");
1572 err = -ENOEXEC;
1573 goto free_hdr;
1575 mod = (void *)sechdrs[modindex].sh_addr;
1577 if (symindex == 0) {
1578 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1579 mod->name);
1580 err = -ENOEXEC;
1581 goto free_hdr;
1584 /* Optional sections */
1585 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1586 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1587 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1588 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1589 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1590 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1591 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1592 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1593 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1594 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1596 /* Don't keep modinfo section */
1597 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1598 #ifdef CONFIG_KALLSYMS
1599 /* Keep symbol and string tables for decoding later. */
1600 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1601 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1602 #endif
1604 /* Check module struct version now, before we try to use module. */
1605 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1606 err = -ENOEXEC;
1607 goto free_hdr;
1610 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1611 /* This is allowed: modprobe --force will invalidate it. */
1612 if (!modmagic) {
1613 tainted |= TAINT_FORCED_MODULE;
1614 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1615 mod->name);
1616 } else if (!same_magic(modmagic, vermagic)) {
1617 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1618 mod->name, modmagic, vermagic);
1619 err = -ENOEXEC;
1620 goto free_hdr;
1623 /* Now copy in args */
1624 arglen = strlen_user(uargs);
1625 if (!arglen) {
1626 err = -EFAULT;
1627 goto free_hdr;
1629 args = kmalloc(arglen, GFP_KERNEL);
1630 if (!args) {
1631 err = -ENOMEM;
1632 goto free_hdr;
1634 if (copy_from_user(args, uargs, arglen) != 0) {
1635 err = -EFAULT;
1636 goto free_mod;
1639 if (find_module(mod->name)) {
1640 err = -EEXIST;
1641 goto free_mod;
1644 mod->state = MODULE_STATE_COMING;
1646 /* Allow arches to frob section contents and sizes. */
1647 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1648 if (err < 0)
1649 goto free_mod;
1651 if (pcpuindex) {
1652 /* We have a special allocation for this section. */
1653 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1654 sechdrs[pcpuindex].sh_addralign,
1655 mod->name);
1656 if (!percpu) {
1657 err = -ENOMEM;
1658 goto free_mod;
1660 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1661 mod->percpu = percpu;
1664 /* Determine total sizes, and put offsets in sh_entsize. For now
1665 this is done generically; there doesn't appear to be any
1666 special cases for the architectures. */
1667 layout_sections(mod, hdr, sechdrs, secstrings);
1669 /* Do the allocs. */
1670 ptr = module_alloc(mod->core_size);
1671 if (!ptr) {
1672 err = -ENOMEM;
1673 goto free_percpu;
1675 memset(ptr, 0, mod->core_size);
1676 mod->module_core = ptr;
1678 ptr = module_alloc(mod->init_size);
1679 if (!ptr && mod->init_size) {
1680 err = -ENOMEM;
1681 goto free_core;
1683 memset(ptr, 0, mod->init_size);
1684 mod->module_init = ptr;
1686 /* Transfer each section which specifies SHF_ALLOC */
1687 DEBUGP("final section addresses:\n");
1688 for (i = 0; i < hdr->e_shnum; i++) {
1689 void *dest;
1691 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1692 continue;
1694 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1695 dest = mod->module_init
1696 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1697 else
1698 dest = mod->module_core + sechdrs[i].sh_entsize;
1700 if (sechdrs[i].sh_type != SHT_NOBITS)
1701 memcpy(dest, (void *)sechdrs[i].sh_addr,
1702 sechdrs[i].sh_size);
1703 /* Update sh_addr to point to copy in image. */
1704 sechdrs[i].sh_addr = (unsigned long)dest;
1705 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1707 /* Module has been moved. */
1708 mod = (void *)sechdrs[modindex].sh_addr;
1710 /* Now we've moved module, initialize linked lists, etc. */
1711 module_unload_init(mod);
1713 /* Set up license info based on the info section */
1714 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1716 #ifdef CONFIG_MODULE_UNLOAD
1717 /* Set up MODINFO_ATTR fields */
1718 setup_modinfo(mod, sechdrs, infoindex);
1719 #endif
1721 /* Fix up syms, so that st_value is a pointer to location. */
1722 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1723 mod);
1724 if (err < 0)
1725 goto cleanup;
1727 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1728 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1729 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1730 if (crcindex)
1731 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1732 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1733 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1734 if (gplcrcindex)
1735 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1737 #ifdef CONFIG_MODVERSIONS
1738 if ((mod->num_syms && !crcindex) ||
1739 (mod->num_gpl_syms && !gplcrcindex)) {
1740 printk(KERN_WARNING "%s: No versions for exported symbols."
1741 " Tainting kernel.\n", mod->name);
1742 tainted |= TAINT_FORCED_MODULE;
1744 #endif
1746 /* Now do relocations. */
1747 for (i = 1; i < hdr->e_shnum; i++) {
1748 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1749 unsigned int info = sechdrs[i].sh_info;
1751 /* Not a valid relocation section? */
1752 if (info >= hdr->e_shnum)
1753 continue;
1755 /* Don't bother with non-allocated sections */
1756 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1757 continue;
1759 if (sechdrs[i].sh_type == SHT_REL)
1760 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1761 else if (sechdrs[i].sh_type == SHT_RELA)
1762 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1763 mod);
1764 if (err < 0)
1765 goto cleanup;
1768 /* Set up and sort exception table */
1769 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1770 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1771 sort_extable(extable, extable + mod->num_exentries);
1773 /* Finally, copy percpu area over. */
1774 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1775 sechdrs[pcpuindex].sh_size);
1777 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1779 err = module_finalize(hdr, sechdrs, mod);
1780 if (err < 0)
1781 goto cleanup;
1783 /* flush the icache in correct context */
1784 old_fs = get_fs();
1785 set_fs(KERNEL_DS);
1788 * Flush the instruction cache, since we've played with text.
1789 * Do it before processing of module parameters, so the module
1790 * can provide parameter accessor functions of its own.
1792 if (mod->module_init)
1793 flush_icache_range((unsigned long)mod->module_init,
1794 (unsigned long)mod->module_init
1795 + mod->init_size);
1796 flush_icache_range((unsigned long)mod->module_core,
1797 (unsigned long)mod->module_core + mod->core_size);
1799 set_fs(old_fs);
1801 mod->args = args;
1802 if (obsparmindex) {
1803 err = obsolete_params(mod->name, mod->args,
1804 (struct obsolete_modparm *)
1805 sechdrs[obsparmindex].sh_addr,
1806 sechdrs[obsparmindex].sh_size
1807 / sizeof(struct obsolete_modparm),
1808 sechdrs, symindex,
1809 (char *)sechdrs[strindex].sh_addr);
1810 if (setupindex)
1811 printk(KERN_WARNING "%s: Ignoring new-style "
1812 "parameters in presence of obsolete ones\n",
1813 mod->name);
1814 } else {
1815 /* Size of section 0 is 0, so this works well if no params */
1816 err = parse_args(mod->name, mod->args,
1817 (struct kernel_param *)
1818 sechdrs[setupindex].sh_addr,
1819 sechdrs[setupindex].sh_size
1820 / sizeof(struct kernel_param),
1821 NULL);
1823 if (err < 0)
1824 goto arch_cleanup;
1826 err = mod_sysfs_setup(mod,
1827 (struct kernel_param *)
1828 sechdrs[setupindex].sh_addr,
1829 sechdrs[setupindex].sh_size
1830 / sizeof(struct kernel_param));
1831 if (err < 0)
1832 goto arch_cleanup;
1833 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1835 /* Get rid of temporary copy */
1836 vfree(hdr);
1838 /* Done! */
1839 return mod;
1841 arch_cleanup:
1842 module_arch_cleanup(mod);
1843 cleanup:
1844 module_unload_free(mod);
1845 module_free(mod, mod->module_init);
1846 free_core:
1847 module_free(mod, mod->module_core);
1848 free_percpu:
1849 if (percpu)
1850 percpu_modfree(percpu);
1851 free_mod:
1852 kfree(args);
1853 free_hdr:
1854 vfree(hdr);
1855 if (err < 0) return ERR_PTR(err);
1856 else return ptr;
1858 truncated:
1859 printk(KERN_ERR "Module len %lu truncated\n", len);
1860 err = -ENOEXEC;
1861 goto free_hdr;
1865 * link the module with the whole machine is stopped with interrupts off
1866 * - this defends against kallsyms not taking locks
1868 static int __link_module(void *_mod)
1870 struct module *mod = _mod;
1871 list_add(&mod->list, &modules);
1872 return 0;
1875 /* This is where the real work happens */
1876 asmlinkage long
1877 sys_init_module(void __user *umod,
1878 unsigned long len,
1879 const char __user *uargs)
1881 struct module *mod;
1882 int ret = 0;
1884 /* Must have permission */
1885 if (!capable(CAP_SYS_MODULE))
1886 return -EPERM;
1888 /* Only one module load at a time, please */
1889 if (down_interruptible(&module_mutex) != 0)
1890 return -EINTR;
1892 /* Do all the hard work */
1893 mod = load_module(umod, len, uargs);
1894 if (IS_ERR(mod)) {
1895 up(&module_mutex);
1896 return PTR_ERR(mod);
1899 /* Now sew it into the lists. They won't access us, since
1900 strong_try_module_get() will fail. */
1901 stop_machine_run(__link_module, mod, NR_CPUS);
1903 /* Drop lock so they can recurse */
1904 up(&module_mutex);
1906 down(&notify_mutex);
1907 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1908 up(&notify_mutex);
1910 /* Start the module */
1911 if (mod->init != NULL)
1912 ret = mod->init();
1913 if (ret < 0) {
1914 /* Init routine failed: abort. Try to protect us from
1915 buggy refcounters. */
1916 mod->state = MODULE_STATE_GOING;
1917 synchronize_sched();
1918 if (mod->unsafe)
1919 printk(KERN_ERR "%s: module is now stuck!\n",
1920 mod->name);
1921 else {
1922 module_put(mod);
1923 down(&module_mutex);
1924 free_module(mod);
1925 up(&module_mutex);
1927 return ret;
1930 /* Now it's a first class citizen! */
1931 down(&module_mutex);
1932 mod->state = MODULE_STATE_LIVE;
1933 /* Drop initial reference. */
1934 module_put(mod);
1935 module_free(mod, mod->module_init);
1936 mod->module_init = NULL;
1937 mod->init_size = 0;
1938 mod->init_text_size = 0;
1939 up(&module_mutex);
1941 return 0;
1944 static inline int within(unsigned long addr, void *start, unsigned long size)
1946 return ((void *)addr >= start && (void *)addr < start + size);
1949 #ifdef CONFIG_KALLSYMS
1951 * This ignores the intensely annoying "mapping symbols" found
1952 * in ARM ELF files: $a, $t and $d.
1954 static inline int is_arm_mapping_symbol(const char *str)
1956 return str[0] == '$' && strchr("atd", str[1])
1957 && (str[2] == '\0' || str[2] == '.');
1960 static const char *get_ksymbol(struct module *mod,
1961 unsigned long addr,
1962 unsigned long *size,
1963 unsigned long *offset)
1965 unsigned int i, best = 0;
1966 unsigned long nextval;
1968 /* At worse, next value is at end of module */
1969 if (within(addr, mod->module_init, mod->init_size))
1970 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1971 else
1972 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1974 /* Scan for closest preceeding symbol, and next symbol. (ELF
1975 starts real symbols at 1). */
1976 for (i = 1; i < mod->num_symtab; i++) {
1977 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1978 continue;
1980 /* We ignore unnamed symbols: they're uninformative
1981 * and inserted at a whim. */
1982 if (mod->symtab[i].st_value <= addr
1983 && mod->symtab[i].st_value > mod->symtab[best].st_value
1984 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1985 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1986 best = i;
1987 if (mod->symtab[i].st_value > addr
1988 && mod->symtab[i].st_value < nextval
1989 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1990 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1991 nextval = mod->symtab[i].st_value;
1994 if (!best)
1995 return NULL;
1997 *size = nextval - mod->symtab[best].st_value;
1998 *offset = addr - mod->symtab[best].st_value;
1999 return mod->strtab + mod->symtab[best].st_name;
2002 /* For kallsyms to ask for address resolution. NULL means not found.
2003 We don't lock, as this is used for oops resolution and races are a
2004 lesser concern. */
2005 const char *module_address_lookup(unsigned long addr,
2006 unsigned long *size,
2007 unsigned long *offset,
2008 char **modname)
2010 struct module *mod;
2012 list_for_each_entry(mod, &modules, list) {
2013 if (within(addr, mod->module_init, mod->init_size)
2014 || within(addr, mod->module_core, mod->core_size)) {
2015 *modname = mod->name;
2016 return get_ksymbol(mod, addr, size, offset);
2019 return NULL;
2022 struct module *module_get_kallsym(unsigned int symnum,
2023 unsigned long *value,
2024 char *type,
2025 char namebuf[128])
2027 struct module *mod;
2029 down(&module_mutex);
2030 list_for_each_entry(mod, &modules, list) {
2031 if (symnum < mod->num_symtab) {
2032 *value = mod->symtab[symnum].st_value;
2033 *type = mod->symtab[symnum].st_info;
2034 strncpy(namebuf,
2035 mod->strtab + mod->symtab[symnum].st_name,
2036 127);
2037 up(&module_mutex);
2038 return mod;
2040 symnum -= mod->num_symtab;
2042 up(&module_mutex);
2043 return NULL;
2046 static unsigned long mod_find_symname(struct module *mod, const char *name)
2048 unsigned int i;
2050 for (i = 0; i < mod->num_symtab; i++)
2051 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2052 return mod->symtab[i].st_value;
2053 return 0;
2056 /* Look for this name: can be of form module:name. */
2057 unsigned long module_kallsyms_lookup_name(const char *name)
2059 struct module *mod;
2060 char *colon;
2061 unsigned long ret = 0;
2063 /* Don't lock: we're in enough trouble already. */
2064 if ((colon = strchr(name, ':')) != NULL) {
2065 *colon = '\0';
2066 if ((mod = find_module(name)) != NULL)
2067 ret = mod_find_symname(mod, colon+1);
2068 *colon = ':';
2069 } else {
2070 list_for_each_entry(mod, &modules, list)
2071 if ((ret = mod_find_symname(mod, name)) != 0)
2072 break;
2074 return ret;
2076 #endif /* CONFIG_KALLSYMS */
2078 /* Called by the /proc file system to return a list of modules. */
2079 static void *m_start(struct seq_file *m, loff_t *pos)
2081 struct list_head *i;
2082 loff_t n = 0;
2084 down(&module_mutex);
2085 list_for_each(i, &modules) {
2086 if (n++ == *pos)
2087 break;
2089 if (i == &modules)
2090 return NULL;
2091 return i;
2094 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2096 struct list_head *i = p;
2097 (*pos)++;
2098 if (i->next == &modules)
2099 return NULL;
2100 return i->next;
2103 static void m_stop(struct seq_file *m, void *p)
2105 up(&module_mutex);
2108 static int m_show(struct seq_file *m, void *p)
2110 struct module *mod = list_entry(p, struct module, list);
2111 seq_printf(m, "%s %lu",
2112 mod->name, mod->init_size + mod->core_size);
2113 print_unload_info(m, mod);
2115 /* Informative for users. */
2116 seq_printf(m, " %s",
2117 mod->state == MODULE_STATE_GOING ? "Unloading":
2118 mod->state == MODULE_STATE_COMING ? "Loading":
2119 "Live");
2120 /* Used by oprofile and other similar tools. */
2121 seq_printf(m, " 0x%p", mod->module_core);
2123 seq_printf(m, "\n");
2124 return 0;
2127 /* Format: modulename size refcount deps address
2129 Where refcount is a number or -, and deps is a comma-separated list
2130 of depends or -.
2132 struct seq_operations modules_op = {
2133 .start = m_start,
2134 .next = m_next,
2135 .stop = m_stop,
2136 .show = m_show
2139 /* Given an address, look for it in the module exception tables. */
2140 const struct exception_table_entry *search_module_extables(unsigned long addr)
2142 unsigned long flags;
2143 const struct exception_table_entry *e = NULL;
2144 struct module *mod;
2146 spin_lock_irqsave(&modlist_lock, flags);
2147 list_for_each_entry(mod, &modules, list) {
2148 if (mod->num_exentries == 0)
2149 continue;
2151 e = search_extable(mod->extable,
2152 mod->extable + mod->num_exentries - 1,
2153 addr);
2154 if (e)
2155 break;
2157 spin_unlock_irqrestore(&modlist_lock, flags);
2159 /* Now, if we found one, we are running inside it now, hence
2160 we cannot unload the module, hence no refcnt needed. */
2161 return e;
2164 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2165 struct module *__module_text_address(unsigned long addr)
2167 struct module *mod;
2169 list_for_each_entry(mod, &modules, list)
2170 if (within(addr, mod->module_init, mod->init_text_size)
2171 || within(addr, mod->module_core, mod->core_text_size))
2172 return mod;
2173 return NULL;
2176 struct module *module_text_address(unsigned long addr)
2178 struct module *mod;
2179 unsigned long flags;
2181 spin_lock_irqsave(&modlist_lock, flags);
2182 mod = __module_text_address(addr);
2183 spin_unlock_irqrestore(&modlist_lock, flags);
2185 return mod;
2188 /* Don't grab lock, we're oopsing. */
2189 void print_modules(void)
2191 struct module *mod;
2193 printk("Modules linked in:");
2194 list_for_each_entry(mod, &modules, list)
2195 printk(" %s", mod->name);
2196 printk("\n");
2199 void module_add_driver(struct module *mod, struct device_driver *drv)
2201 if (!mod || !drv)
2202 return;
2204 /* Don't check return code; this call is idempotent */
2205 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2207 EXPORT_SYMBOL(module_add_driver);
2209 void module_remove_driver(struct device_driver *drv)
2211 if (!drv)
2212 return;
2213 sysfs_remove_link(&drv->kobj, "module");
2215 EXPORT_SYMBOL(module_remove_driver);
2217 #ifdef CONFIG_MODVERSIONS
2218 /* Generate the signature for struct module here, too, for modversions. */
2219 void struct_module(struct module *mod) { return; }
2220 EXPORT_SYMBOL(struct_module);
2221 #endif