Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / kernel / module.c
blobea1cf83ee2b394765ef93cc4774e4cb0f3ea39b8
1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/cpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/vermagic.h>
34 #include <linux/notifier.h>
35 #include <asm/uaccess.h>
36 #include <asm/semaphore.h>
37 #include <asm/pgalloc.h>
38 #include <asm/cacheflush.h>
40 #if 0
41 #define DEBUGP printk
42 #else
43 #define DEBUGP(fmt , a...)
44 #endif
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
48 #endif
50 /* If this is set, the section belongs in the init part of the module */
51 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
53 #define symbol_is(literal, string) \
54 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
56 /* Protects module list */
57 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
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 /* Stub function for modules which don't have an initfn */
95 int init_module(void)
97 return 0;
99 EXPORT_SYMBOL(init_module);
101 /* Find a module section: 0 means not found. */
102 static unsigned int find_sec(Elf_Ehdr *hdr,
103 Elf_Shdr *sechdrs,
104 const char *secstrings,
105 const char *name)
107 unsigned int i;
109 for (i = 1; i < hdr->e_shnum; i++)
110 /* Alloc bit cleared means "ignore it." */
111 if ((sechdrs[i].sh_flags & SHF_ALLOC)
112 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
113 return i;
114 return 0;
117 /* Provided by the linker */
118 extern const struct kernel_symbol __start___ksymtab[];
119 extern const struct kernel_symbol __stop___ksymtab[];
120 extern const struct kernel_symbol __start___ksymtab_gpl[];
121 extern const struct kernel_symbol __stop___ksymtab_gpl[];
122 extern const unsigned long __start___kcrctab[];
123 extern const unsigned long __start___kcrctab_gpl[];
125 #ifndef CONFIG_MODVERSIONS
126 #define symversion(base, idx) NULL
127 #else
128 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
129 #endif
131 /* Find a symbol, return value, crc and module which owns it */
132 static unsigned long __find_symbol(const char *name,
133 struct module **owner,
134 const unsigned long **crc,
135 int gplok)
137 struct module *mod;
138 unsigned int i;
140 /* Core kernel first. */
141 *owner = NULL;
142 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
143 if (strcmp(__start___ksymtab[i].name, name) == 0) {
144 *crc = symversion(__start___kcrctab, i);
145 return __start___ksymtab[i].value;
148 if (gplok) {
149 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
150 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
151 *crc = symversion(__start___kcrctab_gpl, i);
152 return __start___ksymtab_gpl[i].value;
156 /* Now try modules. */
157 list_for_each_entry(mod, &modules, list) {
158 *owner = mod;
159 for (i = 0; i < mod->num_syms; i++)
160 if (strcmp(mod->syms[i].name, name) == 0) {
161 *crc = symversion(mod->crcs, i);
162 return mod->syms[i].value;
165 if (gplok) {
166 for (i = 0; i < mod->num_gpl_syms; i++) {
167 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
168 *crc = symversion(mod->gpl_crcs, i);
169 return mod->gpl_syms[i].value;
174 DEBUGP("Failed to find symbol %s\n", name);
175 return 0;
178 /* Find a symbol in this elf symbol table */
179 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
180 unsigned int symindex,
181 const char *strtab,
182 const char *name)
184 unsigned int i;
185 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
187 /* Search (defined) internal symbols first. */
188 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
189 if (sym[i].st_shndx != SHN_UNDEF
190 && strcmp(name, strtab + sym[i].st_name) == 0)
191 return sym[i].st_value;
193 return 0;
196 /* Search for module by name: must hold module_mutex. */
197 static struct module *find_module(const char *name)
199 struct module *mod;
201 list_for_each_entry(mod, &modules, list) {
202 if (strcmp(mod->name, name) == 0)
203 return mod;
205 return NULL;
208 #ifdef CONFIG_SMP
209 /* Number of blocks used and allocated. */
210 static unsigned int pcpu_num_used, pcpu_num_allocated;
211 /* Size of each block. -ve means used. */
212 static int *pcpu_size;
214 static int split_block(unsigned int i, unsigned short size)
216 /* Reallocation required? */
217 if (pcpu_num_used + 1 > pcpu_num_allocated) {
218 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
219 GFP_KERNEL);
220 if (!new)
221 return 0;
223 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
224 pcpu_num_allocated *= 2;
225 kfree(pcpu_size);
226 pcpu_size = new;
229 /* Insert a new subblock */
230 memmove(&pcpu_size[i+1], &pcpu_size[i],
231 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
232 pcpu_num_used++;
234 pcpu_size[i+1] -= size;
235 pcpu_size[i] = size;
236 return 1;
239 static inline unsigned int block_size(int val)
241 if (val < 0)
242 return -val;
243 return val;
246 /* Created by linker magic */
247 extern char __per_cpu_start[], __per_cpu_end[];
249 static void *percpu_modalloc(unsigned long size, unsigned long align)
251 unsigned long extra;
252 unsigned int i;
253 void *ptr;
255 BUG_ON(align > SMP_CACHE_BYTES);
257 ptr = __per_cpu_start;
258 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
259 /* Extra for alignment requirement. */
260 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
261 BUG_ON(i == 0 && extra != 0);
263 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
264 continue;
266 /* Transfer extra to previous block. */
267 if (pcpu_size[i-1] < 0)
268 pcpu_size[i-1] -= extra;
269 else
270 pcpu_size[i-1] += extra;
271 pcpu_size[i] -= extra;
272 ptr += extra;
274 /* Split block if warranted */
275 if (pcpu_size[i] - size > sizeof(unsigned long))
276 if (!split_block(i, size))
277 return NULL;
279 /* Mark allocated */
280 pcpu_size[i] = -pcpu_size[i];
281 return ptr;
284 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
285 size);
286 return NULL;
289 static void percpu_modfree(void *freeme)
291 unsigned int i;
292 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
294 /* First entry is core kernel percpu data. */
295 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
296 if (ptr == freeme) {
297 pcpu_size[i] = -pcpu_size[i];
298 goto free;
301 BUG();
303 free:
304 /* Merge with previous? */
305 if (pcpu_size[i-1] >= 0) {
306 pcpu_size[i-1] += pcpu_size[i];
307 pcpu_num_used--;
308 memmove(&pcpu_size[i], &pcpu_size[i+1],
309 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
310 i--;
312 /* Merge with next? */
313 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
314 pcpu_size[i] += pcpu_size[i+1];
315 pcpu_num_used--;
316 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
317 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
321 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
322 Elf_Shdr *sechdrs,
323 const char *secstrings)
325 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
328 static int percpu_modinit(void)
330 pcpu_num_used = 2;
331 pcpu_num_allocated = 2;
332 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
333 GFP_KERNEL);
334 /* Static in-kernel percpu data (used). */
335 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
336 /* Free room. */
337 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
338 if (pcpu_size[1] < 0) {
339 printk(KERN_ERR "No per-cpu room for modules.\n");
340 pcpu_num_used = 1;
343 return 0;
345 __initcall(percpu_modinit);
346 #else /* ... !CONFIG_SMP */
347 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
349 return NULL;
351 static inline void percpu_modfree(void *pcpuptr)
353 BUG();
355 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
356 Elf_Shdr *sechdrs,
357 const char *secstrings)
359 return 0;
361 static inline void percpu_modcopy(void *pcpudst, const void *src,
362 unsigned long size)
364 /* pcpusec should be 0, and size of that section should be 0. */
365 BUG_ON(size != 0);
367 #endif /* CONFIG_SMP */
369 #ifdef CONFIG_MODULE_UNLOAD
370 /* Init the unload section of the module. */
371 static void module_unload_init(struct module *mod)
373 unsigned int i;
375 INIT_LIST_HEAD(&mod->modules_which_use_me);
376 for (i = 0; i < NR_CPUS; i++)
377 atomic_set(&mod->ref[i].count, 0);
378 /* Hold reference count during initialization. */
379 atomic_set(&mod->ref[smp_processor_id()].count, 1);
380 /* Backwards compatibility macros put refcount during init. */
381 mod->waiter = current;
384 /* modules using other modules */
385 struct module_use
387 struct list_head list;
388 struct module *module_which_uses;
391 /* Does a already use b? */
392 static int already_uses(struct module *a, struct module *b)
394 struct module_use *use;
396 list_for_each_entry(use, &b->modules_which_use_me, list) {
397 if (use->module_which_uses == a) {
398 DEBUGP("%s uses %s!\n", a->name, b->name);
399 return 1;
402 DEBUGP("%s does not use %s!\n", a->name, b->name);
403 return 0;
406 /* Module a uses b */
407 static int use_module(struct module *a, struct module *b)
409 struct module_use *use;
410 if (b == NULL || already_uses(a, b)) return 1;
412 if (!strong_try_module_get(b))
413 return 0;
415 DEBUGP("Allocating new usage for %s.\n", a->name);
416 use = kmalloc(sizeof(*use), GFP_ATOMIC);
417 if (!use) {
418 printk("%s: out of memory loading\n", a->name);
419 module_put(b);
420 return 0;
423 use->module_which_uses = a;
424 list_add(&use->list, &b->modules_which_use_me);
425 return 1;
428 /* Clear the unload stuff of the module. */
429 static void module_unload_free(struct module *mod)
431 struct module *i;
433 list_for_each_entry(i, &modules, list) {
434 struct module_use *use;
436 list_for_each_entry(use, &i->modules_which_use_me, list) {
437 if (use->module_which_uses == mod) {
438 DEBUGP("%s unusing %s\n", mod->name, i->name);
439 module_put(i);
440 list_del(&use->list);
441 kfree(use);
442 /* There can be at most one match. */
443 break;
449 #ifdef CONFIG_SMP
450 /* Thread to stop each CPU in user context. */
451 enum stopref_state {
452 STOPREF_WAIT,
453 STOPREF_PREPARE,
454 STOPREF_DISABLE_IRQ,
455 STOPREF_EXIT,
458 static enum stopref_state stopref_state;
459 static unsigned int stopref_num_threads;
460 static atomic_t stopref_thread_ack;
462 static int stopref(void *cpu)
464 int irqs_disabled = 0;
465 int prepared = 0;
467 sprintf(current->comm, "kmodule%lu\n", (unsigned long)cpu);
469 /* Highest priority we can manage, and move to right CPU. */
470 #if 0 /* FIXME */
471 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
472 setscheduler(current->pid, SCHED_FIFO, &param);
473 #endif
474 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
476 /* Ack: we are alive */
477 atomic_inc(&stopref_thread_ack);
479 /* Simple state machine */
480 while (stopref_state != STOPREF_EXIT) {
481 if (stopref_state == STOPREF_DISABLE_IRQ && !irqs_disabled) {
482 local_irq_disable();
483 irqs_disabled = 1;
484 /* Ack: irqs disabled. */
485 atomic_inc(&stopref_thread_ack);
486 } else if (stopref_state == STOPREF_PREPARE && !prepared) {
487 /* Everyone is in place, hold CPU. */
488 preempt_disable();
489 prepared = 1;
490 atomic_inc(&stopref_thread_ack);
492 if (irqs_disabled || prepared)
493 cpu_relax();
494 else
495 yield();
498 /* Ack: we are exiting. */
499 atomic_inc(&stopref_thread_ack);
501 if (irqs_disabled)
502 local_irq_enable();
503 if (prepared)
504 preempt_enable();
506 return 0;
509 /* Change the thread state */
510 static void stopref_set_state(enum stopref_state state, int sleep)
512 atomic_set(&stopref_thread_ack, 0);
513 wmb();
514 stopref_state = state;
515 while (atomic_read(&stopref_thread_ack) != stopref_num_threads) {
516 if (sleep)
517 yield();
518 else
519 cpu_relax();
523 /* Stop the machine. Disables irqs. */
524 static int stop_refcounts(void)
526 unsigned int i, cpu;
527 unsigned long old_allowed;
528 int ret = 0;
530 /* One thread per cpu. We'll do our own. */
531 cpu = smp_processor_id();
533 /* FIXME: racy with set_cpus_allowed. */
534 old_allowed = current->cpus_allowed;
535 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
537 atomic_set(&stopref_thread_ack, 0);
538 stopref_num_threads = 0;
539 stopref_state = STOPREF_WAIT;
541 /* No CPUs can come up or down during this. */
542 down(&cpucontrol);
544 for (i = 0; i < NR_CPUS; i++) {
545 if (i == cpu || !cpu_online(i))
546 continue;
547 ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
548 if (ret < 0)
549 break;
550 stopref_num_threads++;
553 /* Wait for them all to come to life. */
554 while (atomic_read(&stopref_thread_ack) != stopref_num_threads)
555 yield();
557 /* If some failed, kill them all. */
558 if (ret < 0) {
559 stopref_set_state(STOPREF_EXIT, 1);
560 up(&cpucontrol);
561 return ret;
564 /* Don't schedule us away at this point, please. */
565 preempt_disable();
567 /* Now they are all scheduled, make them hold the CPUs, ready. */
568 stopref_set_state(STOPREF_PREPARE, 0);
570 /* Make them disable irqs. */
571 stopref_set_state(STOPREF_DISABLE_IRQ, 0);
573 local_irq_disable();
574 return 0;
577 /* Restart the machine. Re-enables irqs. */
578 static void restart_refcounts(void)
580 stopref_set_state(STOPREF_EXIT, 0);
581 local_irq_enable();
582 preempt_enable();
583 up(&cpucontrol);
585 #else /* ...!SMP */
586 static inline int stop_refcounts(void)
588 local_irq_disable();
589 return 0;
591 static inline void restart_refcounts(void)
593 local_irq_enable();
595 #endif
597 unsigned int module_refcount(struct module *mod)
599 unsigned int i, total = 0;
601 for (i = 0; i < NR_CPUS; i++)
602 total += atomic_read(&mod->ref[i].count);
603 return total;
605 EXPORT_SYMBOL(module_refcount);
607 /* This exists whether we can unload or not */
608 static void free_module(struct module *mod);
610 #ifdef CONFIG_MODULE_FORCE_UNLOAD
611 static inline int try_force(unsigned int flags)
613 return (flags & O_TRUNC);
615 #else
616 static inline int try_force(unsigned int flags)
618 return 0;
620 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
622 /* Stub function for modules which don't have an exitfn */
623 void cleanup_module(void)
626 EXPORT_SYMBOL(cleanup_module);
628 static void wait_for_zero_refcount(struct module *mod)
630 /* Since we might sleep for some time, drop the semaphore first */
631 up(&module_mutex);
632 for (;;) {
633 DEBUGP("Looking at refcount...\n");
634 set_current_state(TASK_UNINTERRUPTIBLE);
635 if (module_refcount(mod) == 0)
636 break;
637 schedule();
639 current->state = TASK_RUNNING;
640 down(&module_mutex);
643 asmlinkage long
644 sys_delete_module(const char __user *name_user, unsigned int flags)
646 struct module *mod;
647 char name[MODULE_NAME_LEN];
648 int ret, forced = 0;
650 if (!capable(CAP_SYS_MODULE))
651 return -EPERM;
653 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
654 return -EFAULT;
655 name[MODULE_NAME_LEN-1] = '\0';
657 if (down_interruptible(&module_mutex) != 0)
658 return -EINTR;
660 mod = find_module(name);
661 if (!mod) {
662 ret = -ENOENT;
663 goto out;
666 if (!list_empty(&mod->modules_which_use_me)) {
667 /* Other modules depend on us: get rid of them first. */
668 ret = -EWOULDBLOCK;
669 goto out;
672 /* Already dying? */
673 if (mod->state == MODULE_STATE_GOING) {
674 /* FIXME: if (force), slam module count and wake up
675 waiter --RR */
676 DEBUGP("%s already dying\n", mod->name);
677 ret = -EBUSY;
678 goto out;
681 /* If it has an init func, it must have an exit func to unload */
682 if ((mod->init != init_module && mod->exit == cleanup_module)
683 || mod->unsafe) {
684 forced = try_force(flags);
685 if (!forced) {
686 /* This module can't be removed */
687 ret = -EBUSY;
688 goto out;
691 /* Stop the machine so refcounts can't move: irqs disabled. */
692 DEBUGP("Stopping refcounts...\n");
693 ret = stop_refcounts();
694 if (ret != 0)
695 goto out;
697 /* If it's not unused, quit unless we are told to block. */
698 if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
699 forced = try_force(flags);
700 if (!forced) {
701 ret = -EWOULDBLOCK;
702 restart_refcounts();
703 goto out;
707 /* Mark it as dying. */
708 mod->waiter = current;
709 mod->state = MODULE_STATE_GOING;
710 restart_refcounts();
712 /* Never wait if forced. */
713 if (!forced && module_refcount(mod) != 0)
714 wait_for_zero_refcount(mod);
716 /* Final destruction now noone is using it. */
717 mod->exit();
718 free_module(mod);
720 out:
721 up(&module_mutex);
722 return ret;
725 static void print_unload_info(struct seq_file *m, struct module *mod)
727 struct module_use *use;
728 int printed_something = 0;
730 seq_printf(m, " %u ", module_refcount(mod));
732 /* Always include a trailing , so userspace can differentiate
733 between this and the old multi-field proc format. */
734 list_for_each_entry(use, &mod->modules_which_use_me, list) {
735 printed_something = 1;
736 seq_printf(m, "%s,", use->module_which_uses->name);
739 if (mod->unsafe) {
740 printed_something = 1;
741 seq_printf(m, "[unsafe],");
744 if (mod->init != init_module && mod->exit == cleanup_module) {
745 printed_something = 1;
746 seq_printf(m, "[permanent],");
749 if (!printed_something)
750 seq_printf(m, "-");
753 void __symbol_put(const char *symbol)
755 struct module *owner;
756 unsigned long flags;
757 const unsigned long *crc;
759 spin_lock_irqsave(&modlist_lock, flags);
760 if (!__find_symbol(symbol, &owner, &crc, 1))
761 BUG();
762 module_put(owner);
763 spin_unlock_irqrestore(&modlist_lock, flags);
765 EXPORT_SYMBOL(__symbol_put);
767 void symbol_put_addr(void *addr)
769 unsigned long flags;
771 spin_lock_irqsave(&modlist_lock, flags);
772 if (!kernel_text_address((unsigned long)addr))
773 BUG();
775 module_put(module_text_address((unsigned long)addr));
776 spin_unlock_irqrestore(&modlist_lock, flags);
778 EXPORT_SYMBOL_GPL(symbol_put_addr);
780 #else /* !CONFIG_MODULE_UNLOAD */
781 static void print_unload_info(struct seq_file *m, struct module *mod)
783 /* We don't know the usage count, or what modules are using. */
784 seq_printf(m, " - -");
787 static inline void module_unload_free(struct module *mod)
791 static inline int use_module(struct module *a, struct module *b)
793 return strong_try_module_get(b);
796 static inline void module_unload_init(struct module *mod)
800 asmlinkage long
801 sys_delete_module(const char *name_user, unsigned int flags)
803 return -ENOSYS;
806 #endif /* CONFIG_MODULE_UNLOAD */
808 #ifdef CONFIG_OBSOLETE_MODPARM
809 static int param_set_byte(const char *val, struct kernel_param *kp)
811 char *endp;
812 long l;
814 if (!val) return -EINVAL;
815 l = simple_strtol(val, &endp, 0);
816 if (endp == val || *endp || ((char)l != l))
817 return -EINVAL;
818 *((char *)kp->arg) = l;
819 return 0;
822 /* Bounds checking done below */
823 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
825 strcpy(kp->arg, val);
826 return 0;
829 int set_obsolete(const char *val, struct kernel_param *kp)
831 unsigned int min, max;
832 unsigned int size, maxsize;
833 char *endp;
834 const char *p;
835 struct obsolete_modparm *obsparm = kp->arg;
837 if (!val) {
838 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
839 return -EINVAL;
842 /* type is: [min[-max]]{b,h,i,l,s} */
843 p = obsparm->type;
844 min = simple_strtol(p, &endp, 10);
845 if (endp == obsparm->type)
846 min = max = 1;
847 else if (*endp == '-') {
848 p = endp+1;
849 max = simple_strtol(p, &endp, 10);
850 } else
851 max = min;
852 switch (*endp) {
853 case 'b':
854 return param_array(kp->name, val, min, max, obsparm->addr,
855 1, param_set_byte);
856 case 'h':
857 return param_array(kp->name, val, min, max, obsparm->addr,
858 sizeof(short), param_set_short);
859 case 'i':
860 return param_array(kp->name, val, min, max, obsparm->addr,
861 sizeof(int), param_set_int);
862 case 'l':
863 return param_array(kp->name, val, min, max, obsparm->addr,
864 sizeof(long), param_set_long);
865 case 's':
866 return param_array(kp->name, val, min, max, obsparm->addr,
867 sizeof(char *), param_set_charp);
869 case 'c':
870 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
871 and the decl is "char xxx[5][50];" */
872 p = endp+1;
873 maxsize = simple_strtol(p, &endp, 10);
874 /* We check lengths here (yes, this is a hack). */
875 p = val;
876 while (p[size = strcspn(p, ",")]) {
877 if (size >= maxsize)
878 goto oversize;
879 p += size+1;
881 if (size >= maxsize)
882 goto oversize;
883 return param_array(kp->name, val, min, max, obsparm->addr,
884 maxsize, obsparm_copy_string);
886 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
887 return -EINVAL;
888 oversize:
889 printk(KERN_ERR
890 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
891 return -EINVAL;
894 static int obsolete_params(const char *name,
895 char *args,
896 struct obsolete_modparm obsparm[],
897 unsigned int num,
898 Elf_Shdr *sechdrs,
899 unsigned int symindex,
900 const char *strtab)
902 struct kernel_param *kp;
903 unsigned int i;
904 int ret;
906 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
907 if (!kp)
908 return -ENOMEM;
910 for (i = 0; i < num; i++) {
911 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
913 snprintf(sym_name, sizeof(sym_name), "%s%s",
914 MODULE_SYMBOL_PREFIX, obsparm[i].name);
916 kp[i].name = obsparm[i].name;
917 kp[i].perm = 000;
918 kp[i].set = set_obsolete;
919 kp[i].get = NULL;
920 obsparm[i].addr
921 = (void *)find_local_symbol(sechdrs, symindex, strtab,
922 sym_name);
923 if (!obsparm[i].addr) {
924 printk("%s: falsely claims to have parameter %s\n",
925 name, obsparm[i].name);
926 ret = -EINVAL;
927 goto out;
929 kp[i].arg = &obsparm[i];
932 ret = parse_args(name, args, kp, num, NULL);
933 out:
934 kfree(kp);
935 return ret;
937 #else
938 static int obsolete_params(const char *name,
939 char *args,
940 struct obsolete_modparm obsparm[],
941 unsigned int num,
942 Elf_Shdr *sechdrs,
943 unsigned int symindex,
944 const char *strtab)
946 if (num != 0)
947 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
948 name);
949 return 0;
951 #endif /* CONFIG_OBSOLETE_MODPARM */
953 static const char vermagic[] = VERMAGIC_STRING;
955 #ifdef CONFIG_MODVERSIONS
956 static int check_version(Elf_Shdr *sechdrs,
957 unsigned int versindex,
958 const char *symname,
959 struct module *mod,
960 const unsigned long *crc)
962 unsigned int i, num_versions;
963 struct modversion_info *versions;
965 /* Exporting module didn't supply crcs? OK, we're already tainted. */
966 if (!crc)
967 return 1;
969 versions = (void *) sechdrs[versindex].sh_addr;
970 num_versions = sechdrs[versindex].sh_size
971 / sizeof(struct modversion_info);
973 for (i = 0; i < num_versions; i++) {
974 if (strcmp(versions[i].name, symname) != 0)
975 continue;
977 if (versions[i].crc == *crc)
978 return 1;
979 printk("%s: disagrees about version of symbol %s\n",
980 mod->name, symname);
981 DEBUGP("Found checksum %lX vs module %lX\n",
982 *crc, versions[i].crc);
983 return 0;
985 /* Not in module's version table. OK, but that taints the kernel. */
986 if (!(tainted & TAINT_FORCED_MODULE)) {
987 printk("%s: no version for \"%s\" found: kernel tainted.\n",
988 mod->name, symname);
989 tainted |= TAINT_FORCED_MODULE;
991 return 1;
994 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
995 unsigned int versindex,
996 struct module *mod)
998 const unsigned long *crc;
999 struct module *owner;
1001 if (!__find_symbol("struct_module", &owner, &crc, 1))
1002 BUG();
1003 return check_version(sechdrs, versindex, "struct_module", mod,
1004 crc);
1007 /* First part is kernel version, which we ignore. */
1008 static inline int same_magic(const char *amagic, const char *bmagic)
1010 amagic += strcspn(amagic, " ");
1011 bmagic += strcspn(bmagic, " ");
1012 return strcmp(amagic, bmagic) == 0;
1014 #else
1015 static inline int check_version(Elf_Shdr *sechdrs,
1016 unsigned int versindex,
1017 const char *symname,
1018 struct module *mod,
1019 const unsigned long *crc)
1021 return 1;
1024 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1025 unsigned int versindex,
1026 struct module *mod)
1028 return 1;
1031 static inline int same_magic(const char *amagic, const char *bmagic)
1033 return strcmp(amagic, bmagic) == 0;
1035 #endif /* CONFIG_MODVERSIONS */
1037 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1038 Must be holding module_mutex. */
1039 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1040 unsigned int versindex,
1041 const char *name,
1042 struct module *mod)
1044 struct module *owner;
1045 unsigned long ret;
1046 const unsigned long *crc;
1048 spin_lock_irq(&modlist_lock);
1049 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
1050 if (ret) {
1051 /* use_module can fail due to OOM, or module unloading */
1052 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1053 !use_module(mod, owner))
1054 ret = 0;
1056 spin_unlock_irq(&modlist_lock);
1057 return ret;
1060 /* Free a module, remove from lists, etc (must hold module mutex). */
1061 static void free_module(struct module *mod)
1063 /* Delete from various lists */
1064 spin_lock_irq(&modlist_lock);
1065 list_del(&mod->list);
1066 spin_unlock_irq(&modlist_lock);
1068 /* Arch-specific cleanup. */
1069 module_arch_cleanup(mod);
1071 /* Module unload stuff */
1072 module_unload_free(mod);
1074 /* This may be NULL, but that's OK */
1075 module_free(mod, mod->module_init);
1076 kfree(mod->args);
1077 if (mod->percpu)
1078 percpu_modfree(mod->percpu);
1080 /* Finally, free the core (containing the module structure) */
1081 module_free(mod, mod->module_core);
1084 void *__symbol_get(const char *symbol)
1086 struct module *owner;
1087 unsigned long value, flags;
1088 const unsigned long *crc;
1090 spin_lock_irqsave(&modlist_lock, flags);
1091 value = __find_symbol(symbol, &owner, &crc, 1);
1092 if (value && !strong_try_module_get(owner))
1093 value = 0;
1094 spin_unlock_irqrestore(&modlist_lock, flags);
1096 return (void *)value;
1098 EXPORT_SYMBOL_GPL(__symbol_get);
1100 /* Change all symbols so that sh_value encodes the pointer directly. */
1101 static int simplify_symbols(Elf_Shdr *sechdrs,
1102 unsigned int symindex,
1103 const char *strtab,
1104 unsigned int versindex,
1105 unsigned int pcpuindex,
1106 struct module *mod)
1108 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1109 unsigned long secbase;
1110 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1111 int ret = 0;
1113 for (i = 1; i < n; i++) {
1114 switch (sym[i].st_shndx) {
1115 case SHN_COMMON:
1116 /* We compiled with -fno-common. These are not
1117 supposed to happen. */
1118 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1119 ret = -ENOEXEC;
1120 break;
1122 case SHN_ABS:
1123 /* Don't need to do anything */
1124 DEBUGP("Absolute symbol: 0x%08lx\n",
1125 (long)sym[i].st_value);
1126 break;
1128 case SHN_UNDEF:
1129 sym[i].st_value
1130 = resolve_symbol(sechdrs, versindex,
1131 strtab + sym[i].st_name, mod);
1133 /* Ok if resolved. */
1134 if (sym[i].st_value != 0)
1135 break;
1136 /* Ok if weak. */
1137 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1138 break;
1140 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1141 mod->name, strtab + sym[i].st_name);
1142 ret = -ENOENT;
1143 break;
1145 default:
1146 /* Divert to percpu allocation if a percpu var. */
1147 if (sym[i].st_shndx == pcpuindex)
1148 secbase = (unsigned long)mod->percpu;
1149 else
1150 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1151 sym[i].st_value += secbase;
1152 break;
1156 return ret;
1159 /* Update size with this section: return offset. */
1160 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1162 long ret;
1164 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1165 *size = ret + sechdr->sh_size;
1166 return ret;
1169 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1170 might -- code, read-only data, read-write data, small data. Tally
1171 sizes, and place the offsets into sh_entsize fields: high bit means it
1172 belongs in init. */
1173 static void layout_sections(struct module *mod,
1174 const Elf_Ehdr *hdr,
1175 Elf_Shdr *sechdrs,
1176 const char *secstrings)
1178 static unsigned long const masks[][2] = {
1179 /* NOTE: all executable code must be the first section
1180 * in this array; otherwise modify the text_size
1181 * finder in the two loops below */
1182 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1183 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1184 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1185 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1187 unsigned int m, i;
1189 for (i = 0; i < hdr->e_shnum; i++)
1190 sechdrs[i].sh_entsize = ~0UL;
1192 DEBUGP("Core section allocation order:\n");
1193 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1194 for (i = 0; i < hdr->e_shnum; ++i) {
1195 Elf_Shdr *s = &sechdrs[i];
1197 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1198 || (s->sh_flags & masks[m][1])
1199 || s->sh_entsize != ~0UL
1200 || strstr(secstrings + s->sh_name, ".init"))
1201 continue;
1202 s->sh_entsize = get_offset(&mod->core_size, s);
1203 DEBUGP("\t%s\n", secstrings + s->sh_name);
1205 if (m == 0)
1206 mod->core_text_size = mod->core_size;
1209 DEBUGP("Init section allocation order:\n");
1210 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1211 for (i = 0; i < hdr->e_shnum; ++i) {
1212 Elf_Shdr *s = &sechdrs[i];
1214 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1215 || (s->sh_flags & masks[m][1])
1216 || s->sh_entsize != ~0UL
1217 || !strstr(secstrings + s->sh_name, ".init"))
1218 continue;
1219 s->sh_entsize = (get_offset(&mod->init_size, s)
1220 | INIT_OFFSET_MASK);
1221 DEBUGP("\t%s\n", secstrings + s->sh_name);
1223 if (m == 0)
1224 mod->init_text_size = mod->init_size;
1228 static inline int license_is_gpl_compatible(const char *license)
1230 return (strcmp(license, "GPL") == 0
1231 || strcmp(license, "GPL v2") == 0
1232 || strcmp(license, "GPL and additional rights") == 0
1233 || strcmp(license, "Dual BSD/GPL") == 0
1234 || strcmp(license, "Dual MPL/GPL") == 0);
1237 static void set_license(struct module *mod, const char *license)
1239 if (!license)
1240 license = "unspecified";
1242 mod->license_gplok = license_is_gpl_compatible(license);
1243 if (!mod->license_gplok) {
1244 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1245 mod->name, license);
1246 tainted |= TAINT_PROPRIETARY_MODULE;
1250 /* Parse tag=value strings from .modinfo section */
1251 static char *next_string(char *string, unsigned long *secsize)
1253 /* Skip non-zero chars */
1254 while (string[0]) {
1255 string++;
1256 if ((*secsize)-- <= 1)
1257 return NULL;
1260 /* Skip any zero padding. */
1261 while (!string[0]) {
1262 string++;
1263 if ((*secsize)-- <= 1)
1264 return NULL;
1266 return string;
1269 static char *get_modinfo(Elf_Shdr *sechdrs,
1270 unsigned int info,
1271 const char *tag)
1273 char *p;
1274 unsigned int taglen = strlen(tag);
1275 unsigned long size = sechdrs[info].sh_size;
1277 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1278 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1279 return p + taglen + 1;
1281 return NULL;
1284 #ifdef CONFIG_KALLSYMS
1285 int is_exported(const char *name, const struct module *mod)
1287 unsigned int i;
1289 if (!mod) {
1290 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1291 if (strcmp(__start___ksymtab[i].name, name) == 0)
1292 return 1;
1293 return 0;
1295 for (i = 0; i < mod->num_syms; i++)
1296 if (strcmp(mod->syms[i].name, name) == 0)
1297 return 1;
1298 return 0;
1301 /* As per nm */
1302 static char elf_type(const Elf_Sym *sym,
1303 Elf_Shdr *sechdrs,
1304 const char *secstrings,
1305 struct module *mod)
1307 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1308 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1309 return 'v';
1310 else
1311 return 'w';
1313 if (sym->st_shndx == SHN_UNDEF)
1314 return 'U';
1315 if (sym->st_shndx == SHN_ABS)
1316 return 'a';
1317 if (sym->st_shndx >= SHN_LORESERVE)
1318 return '?';
1319 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1320 return 't';
1321 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1322 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1323 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1324 return 'r';
1325 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1326 return 'g';
1327 else
1328 return 'd';
1330 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1331 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1332 return 's';
1333 else
1334 return 'b';
1336 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1337 ".debug", strlen(".debug")) == 0)
1338 return 'n';
1339 return '?';
1342 static void add_kallsyms(struct module *mod,
1343 Elf_Shdr *sechdrs,
1344 unsigned int symindex,
1345 unsigned int strindex,
1346 const char *secstrings)
1348 unsigned int i;
1350 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1351 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1352 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1354 /* Set types up while we still have access to sections. */
1355 for (i = 0; i < mod->num_symtab; i++)
1356 mod->symtab[i].st_info
1357 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1359 #endif
1361 /* Allocate and load the module: note that size of section 0 is always
1362 zero, and we rely on this for optional sections. */
1363 static struct module *load_module(void __user *umod,
1364 unsigned long len,
1365 const char __user *uargs)
1367 Elf_Ehdr *hdr;
1368 Elf_Shdr *sechdrs;
1369 char *secstrings, *args, *modmagic, *strtab = NULL;
1370 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1371 exportindex, modindex, obsparmindex, infoindex, gplindex,
1372 crcindex, gplcrcindex, versindex, pcpuindex;
1373 long arglen;
1374 struct module *mod;
1375 long err = 0;
1376 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1378 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1379 umod, len, uargs);
1380 if (len < sizeof(*hdr))
1381 return ERR_PTR(-ENOEXEC);
1383 /* Suck in entire file: we'll want most of it. */
1384 /* vmalloc barfs on "unusual" numbers. Check here */
1385 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1386 return ERR_PTR(-ENOMEM);
1387 if (copy_from_user(hdr, umod, len) != 0) {
1388 err = -EFAULT;
1389 goto free_hdr;
1392 /* Sanity checks against insmoding binaries or wrong arch,
1393 weird elf version */
1394 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1395 || hdr->e_type != ET_REL
1396 || !elf_check_arch(hdr)
1397 || hdr->e_shentsize != sizeof(*sechdrs)) {
1398 err = -ENOEXEC;
1399 goto free_hdr;
1402 /* Convenience variables */
1403 sechdrs = (void *)hdr + hdr->e_shoff;
1404 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1405 sechdrs[0].sh_addr = 0;
1407 /* And these should exist, but gcc whinges if we don't init them */
1408 symindex = strindex = 0;
1410 for (i = 1; i < hdr->e_shnum; i++) {
1411 /* Mark all sections sh_addr with their address in the
1412 temporary image. */
1413 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1415 /* Internal symbols and strings. */
1416 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1417 symindex = i;
1418 strindex = sechdrs[i].sh_link;
1419 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1421 #ifndef CONFIG_MODULE_UNLOAD
1422 /* Don't load .exit sections */
1423 if (strstr(secstrings+sechdrs[i].sh_name, ".exit"))
1424 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1425 #endif
1428 modindex = find_sec(hdr, sechdrs, secstrings,
1429 ".gnu.linkonce.this_module");
1430 if (!modindex) {
1431 printk(KERN_WARNING "No module found in object\n");
1432 err = -ENOEXEC;
1433 goto free_hdr;
1435 mod = (void *)sechdrs[modindex].sh_addr;
1437 /* Optional sections */
1438 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1439 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1440 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1441 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1442 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1443 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1444 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1445 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1446 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1447 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1449 /* Don't keep modinfo section */
1450 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1451 #ifdef CONFIG_KALLSYMS
1452 /* Keep symbol and string tables for decoding later. */
1453 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1454 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1455 #endif
1457 /* Check module struct version now, before we try to use module. */
1458 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1459 err = -ENOEXEC;
1460 goto free_hdr;
1463 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1464 /* This is allowed: modprobe --force will invalidate it. */
1465 if (!modmagic) {
1466 tainted |= TAINT_FORCED_MODULE;
1467 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1468 mod->name);
1469 } else if (!same_magic(modmagic, vermagic)) {
1470 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1471 mod->name, modmagic, vermagic);
1472 err = -ENOEXEC;
1473 goto free_hdr;
1476 /* Now copy in args */
1477 arglen = strlen_user(uargs);
1478 if (!arglen) {
1479 err = -EFAULT;
1480 goto free_hdr;
1482 args = kmalloc(arglen, GFP_KERNEL);
1483 if (!args) {
1484 err = -ENOMEM;
1485 goto free_hdr;
1487 if (copy_from_user(args, uargs, arglen) != 0) {
1488 err = -EFAULT;
1489 goto free_mod;
1492 if (find_module(mod->name)) {
1493 err = -EEXIST;
1494 goto free_mod;
1497 mod->state = MODULE_STATE_COMING;
1499 /* Allow arches to frob section contents and sizes. */
1500 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1501 if (err < 0)
1502 goto free_mod;
1504 if (pcpuindex) {
1505 /* We have a special allocation for this section. */
1506 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1507 sechdrs[pcpuindex].sh_addralign);
1508 if (!percpu) {
1509 err = -ENOMEM;
1510 goto free_mod;
1512 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1513 mod->percpu = percpu;
1516 /* Determine total sizes, and put offsets in sh_entsize. For now
1517 this is done generically; there doesn't appear to be any
1518 special cases for the architectures. */
1519 layout_sections(mod, hdr, sechdrs, secstrings);
1521 /* Do the allocs. */
1522 ptr = module_alloc(mod->core_size);
1523 if (!ptr) {
1524 err = -ENOMEM;
1525 goto free_percpu;
1527 memset(ptr, 0, mod->core_size);
1528 mod->module_core = ptr;
1530 ptr = module_alloc(mod->init_size);
1531 if (!ptr && mod->init_size) {
1532 err = -ENOMEM;
1533 goto free_core;
1535 memset(ptr, 0, mod->init_size);
1536 mod->module_init = ptr;
1538 /* Transfer each section which specifies SHF_ALLOC */
1539 DEBUGP("final section addresses:\n");
1540 for (i = 0; i < hdr->e_shnum; i++) {
1541 void *dest;
1543 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1544 continue;
1546 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1547 dest = mod->module_init
1548 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1549 else
1550 dest = mod->module_core + sechdrs[i].sh_entsize;
1552 if (sechdrs[i].sh_type != SHT_NOBITS)
1553 memcpy(dest, (void *)sechdrs[i].sh_addr,
1554 sechdrs[i].sh_size);
1555 /* Update sh_addr to point to copy in image. */
1556 sechdrs[i].sh_addr = (unsigned long)dest;
1557 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1559 /* Module has been moved. */
1560 mod = (void *)sechdrs[modindex].sh_addr;
1562 /* Now we've moved module, initialize linked lists, etc. */
1563 module_unload_init(mod);
1565 /* Set up license info based on the info section */
1566 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1568 /* Fix up syms, so that st_value is a pointer to location. */
1569 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1570 mod);
1571 if (err < 0)
1572 goto cleanup;
1574 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1575 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1576 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1577 if (crcindex)
1578 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1579 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1580 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1581 if (gplcrcindex)
1582 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1584 #ifdef CONFIG_MODVERSIONS
1585 if ((mod->num_syms && !crcindex) ||
1586 (mod->num_gpl_syms && !gplcrcindex)) {
1587 printk(KERN_WARNING "%s: No versions for exported symbols."
1588 " Tainting kernel.\n", mod->name);
1589 tainted |= TAINT_FORCED_MODULE;
1591 #endif
1593 /* Set up exception table */
1594 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1595 mod->extable = (void *)sechdrs[exindex].sh_addr;
1597 /* Now do relocations. */
1598 for (i = 1; i < hdr->e_shnum; i++) {
1599 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1600 if (sechdrs[i].sh_type == SHT_REL)
1601 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1602 else if (sechdrs[i].sh_type == SHT_RELA)
1603 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1604 mod);
1605 if (err < 0)
1606 goto cleanup;
1609 /* Finally, copy percpu area over. */
1610 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1611 sechdrs[pcpuindex].sh_size);
1613 err = module_finalize(hdr, sechdrs, mod);
1614 if (err < 0)
1615 goto cleanup;
1617 #ifdef CONFIG_KALLSYMS
1618 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1619 #endif
1621 mod->args = args;
1622 if (obsparmindex) {
1623 err = obsolete_params(mod->name, mod->args,
1624 (struct obsolete_modparm *)
1625 sechdrs[obsparmindex].sh_addr,
1626 sechdrs[obsparmindex].sh_size
1627 / sizeof(struct obsolete_modparm),
1628 sechdrs, symindex,
1629 (char *)sechdrs[strindex].sh_addr);
1630 } else {
1631 /* Size of section 0 is 0, so this works well if no params */
1632 err = parse_args(mod->name, mod->args,
1633 (struct kernel_param *)
1634 sechdrs[setupindex].sh_addr,
1635 sechdrs[setupindex].sh_size
1636 / sizeof(struct kernel_param),
1637 NULL);
1639 if (err < 0)
1640 goto cleanup;
1642 /* Get rid of temporary copy */
1643 vfree(hdr);
1645 /* Done! */
1646 return mod;
1648 cleanup:
1649 module_unload_free(mod);
1650 module_free(mod, mod->module_init);
1651 free_core:
1652 module_free(mod, mod->module_core);
1653 free_percpu:
1654 if (percpu)
1655 percpu_modfree(percpu);
1656 free_mod:
1657 kfree(args);
1658 free_hdr:
1659 vfree(hdr);
1660 if (err < 0) return ERR_PTR(err);
1661 else return ptr;
1664 /* This is where the real work happens */
1665 asmlinkage long
1666 sys_init_module(void __user *umod,
1667 unsigned long len,
1668 const char __user *uargs)
1670 struct module *mod;
1671 int ret;
1673 /* Must have permission */
1674 if (!capable(CAP_SYS_MODULE))
1675 return -EPERM;
1677 /* Only one module load at a time, please */
1678 if (down_interruptible(&module_mutex) != 0)
1679 return -EINTR;
1681 /* Do all the hard work */
1682 mod = load_module(umod, len, uargs);
1683 if (IS_ERR(mod)) {
1684 up(&module_mutex);
1685 return PTR_ERR(mod);
1688 /* Flush the instruction cache, since we've played with text */
1689 if (mod->module_init)
1690 flush_icache_range((unsigned long)mod->module_init,
1691 (unsigned long)mod->module_init
1692 + mod->init_size);
1693 flush_icache_range((unsigned long)mod->module_core,
1694 (unsigned long)mod->module_core + mod->core_size);
1696 /* Now sew it into the lists. They won't access us, since
1697 strong_try_module_get() will fail. */
1698 spin_lock_irq(&modlist_lock);
1699 list_add(&mod->list, &modules);
1700 spin_unlock_irq(&modlist_lock);
1702 /* Drop lock so they can recurse */
1703 up(&module_mutex);
1705 down(&notify_mutex);
1706 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1707 up(&notify_mutex);
1709 /* Start the module */
1710 ret = mod->init();
1711 if (ret < 0) {
1712 /* Init routine failed: abort. Try to protect us from
1713 buggy refcounters. */
1714 mod->state = MODULE_STATE_GOING;
1715 synchronize_kernel();
1716 if (mod->unsafe)
1717 printk(KERN_ERR "%s: module is now stuck!\n",
1718 mod->name);
1719 else {
1720 module_put(mod);
1721 down(&module_mutex);
1722 free_module(mod);
1723 up(&module_mutex);
1725 return ret;
1728 /* Now it's a first class citizen! */
1729 down(&module_mutex);
1730 mod->state = MODULE_STATE_LIVE;
1731 /* Drop initial reference. */
1732 module_put(mod);
1733 module_free(mod, mod->module_init);
1734 mod->module_init = NULL;
1735 mod->init_size = 0;
1736 mod->init_text_size = 0;
1737 up(&module_mutex);
1739 return 0;
1742 static inline int within(unsigned long addr, void *start, unsigned long size)
1744 return ((void *)addr >= start && (void *)addr < start + size);
1747 #ifdef CONFIG_KALLSYMS
1748 static const char *get_ksymbol(struct module *mod,
1749 unsigned long addr,
1750 unsigned long *size,
1751 unsigned long *offset)
1753 unsigned int i, best = 0;
1754 unsigned long nextval;
1756 /* At worse, next value is at end of module */
1757 if (within(addr, mod->module_init, mod->init_size))
1758 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1759 else
1760 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1762 /* Scan for closest preceeding symbol, and next symbol. (ELF
1763 starts real symbols at 1). */
1764 for (i = 1; i < mod->num_symtab; i++) {
1765 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1766 continue;
1768 /* We ignore unnamed symbols: they're uninformative
1769 * and inserted at a whim. */
1770 if (mod->symtab[i].st_value <= addr
1771 && mod->symtab[i].st_value > mod->symtab[best].st_value
1772 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1773 best = i;
1774 if (mod->symtab[i].st_value > addr
1775 && mod->symtab[i].st_value < nextval
1776 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1777 nextval = mod->symtab[i].st_value;
1780 if (!best)
1781 return NULL;
1783 *size = nextval - mod->symtab[best].st_value;
1784 *offset = addr - mod->symtab[best].st_value;
1785 return mod->strtab + mod->symtab[best].st_name;
1788 /* For kallsyms to ask for address resolution. NULL means not found.
1789 We don't lock, as this is used for oops resolution and races are a
1790 lesser concern. */
1791 const char *module_address_lookup(unsigned long addr,
1792 unsigned long *size,
1793 unsigned long *offset,
1794 char **modname)
1796 struct module *mod;
1798 list_for_each_entry(mod, &modules, list) {
1799 if (within(addr, mod->module_init, mod->init_size)
1800 || within(addr, mod->module_core, mod->core_size)) {
1801 *modname = mod->name;
1802 return get_ksymbol(mod, addr, size, offset);
1805 return NULL;
1808 struct module *module_get_kallsym(unsigned int symnum,
1809 unsigned long *value,
1810 char *type,
1811 char namebuf[128])
1813 struct module *mod;
1815 down(&module_mutex);
1816 list_for_each_entry(mod, &modules, list) {
1817 if (symnum < mod->num_symtab) {
1818 *value = mod->symtab[symnum].st_value;
1819 *type = mod->symtab[symnum].st_info;
1820 strncpy(namebuf,
1821 mod->strtab + mod->symtab[symnum].st_name,
1822 127);
1823 up(&module_mutex);
1824 return mod;
1826 symnum -= mod->num_symtab;
1828 up(&module_mutex);
1829 return NULL;
1831 #endif /* CONFIG_KALLSYMS */
1833 /* Called by the /proc file system to return a list of modules. */
1834 static void *m_start(struct seq_file *m, loff_t *pos)
1836 struct list_head *i;
1837 loff_t n = 0;
1839 down(&module_mutex);
1840 list_for_each(i, &modules) {
1841 if (n++ == *pos)
1842 break;
1844 if (i == &modules)
1845 return NULL;
1846 return i;
1849 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1851 struct list_head *i = p;
1852 (*pos)++;
1853 if (i->next == &modules)
1854 return NULL;
1855 return i->next;
1858 static void m_stop(struct seq_file *m, void *p)
1860 up(&module_mutex);
1863 static int m_show(struct seq_file *m, void *p)
1865 struct module *mod = list_entry(p, struct module, list);
1866 seq_printf(m, "%s %lu",
1867 mod->name, mod->init_size + mod->core_size);
1868 print_unload_info(m, mod);
1870 /* Informative for users. */
1871 seq_printf(m, " %s",
1872 mod->state == MODULE_STATE_GOING ? "Unloading":
1873 mod->state == MODULE_STATE_COMING ? "Loading":
1874 "Live");
1875 /* Used by oprofile and other similar tools. */
1876 seq_printf(m, " 0x%p", mod->module_core);
1878 seq_printf(m, "\n");
1879 return 0;
1882 /* Format: modulename size refcount deps address
1884 Where refcount is a number or -, and deps is a comma-separated list
1885 of depends or -.
1887 struct seq_operations modules_op = {
1888 .start = m_start,
1889 .next = m_next,
1890 .stop = m_stop,
1891 .show = m_show
1894 /* Given an address, look for it in the module exception tables. */
1895 const struct exception_table_entry *search_module_extables(unsigned long addr)
1897 unsigned long flags;
1898 const struct exception_table_entry *e = NULL;
1899 struct module *mod;
1901 spin_lock_irqsave(&modlist_lock, flags);
1902 list_for_each_entry(mod, &modules, list) {
1903 if (mod->num_exentries == 0)
1904 continue;
1906 e = search_extable(mod->extable,
1907 mod->extable + mod->num_exentries - 1,
1908 addr);
1909 if (e)
1910 break;
1912 spin_unlock_irqrestore(&modlist_lock, flags);
1914 /* Now, if we found one, we are running inside it now, hence
1915 we cannot unload the module, hence no refcnt needed. */
1916 return e;
1919 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1920 struct module *module_text_address(unsigned long addr)
1922 struct module *mod;
1924 list_for_each_entry(mod, &modules, list)
1925 if (within(addr, mod->module_init, mod->init_text_size)
1926 || within(addr, mod->module_core, mod->core_text_size))
1927 return mod;
1928 return NULL;
1931 #ifdef CONFIG_MODVERSIONS
1932 /* Generate the signature for struct module here, too, for modversions. */
1933 void struct_module(struct module *mod) { return; }
1934 EXPORT_SYMBOL(struct_module);
1935 #endif