- Separate out show_stack changes into own patch.
[linux-flexiantxendom0-3.2.10.git] / kernel / module.c
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.
4
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.
9
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.
14
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
18 */
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>
39
40 #if 0
41 #define DEBUGP printk
42 #else
43 #define DEBUGP(fmt , a...)
44 #endif
45
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
48 #endif
49
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))
52
53 #define symbol_is(literal, string)                              \
54         (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
55
56 /* Protects module list */
57 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
58
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
62
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
65
66 int register_module_notifier(struct notifier_block * nb)
67 {
68         int err;
69         down(&notify_mutex);
70         err = notifier_chain_register(&module_notify_list, nb);
71         up(&notify_mutex);
72         return err;
73 }
74 EXPORT_SYMBOL(register_module_notifier);
75
76 int unregister_module_notifier(struct notifier_block * nb)
77 {
78         int err;
79         down(&notify_mutex);
80         err = notifier_chain_unregister(&module_notify_list, nb);
81         up(&notify_mutex);
82         return err;
83 }
84 EXPORT_SYMBOL(unregister_module_notifier);
85
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
88 {
89         if (mod && mod->state == MODULE_STATE_COMING)
90                 return 0;
91         return try_module_get(mod);
92 }
93
94 /* Stub function for modules which don't have an initfn */
95 int init_module(void)
96 {
97         return 0;
98 }
99 EXPORT_SYMBOL(init_module);
100
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)
106 {
107         unsigned int i;
108
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;
115 }
116
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[];
124
125 #ifndef CONFIG_MODVERSIONS
126 #define symversion(base, idx) NULL
127 #else
128 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
129 #endif
130
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)
136 {
137         struct module *mod;
138         unsigned int i;
139
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;
146                 }
147         }
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;
153                         }
154         }
155
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;
163                         }
164
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;
170                                 }
171                         }
172                 }
173         }
174         DEBUGP("Failed to find symbol %s\n", name);
175         return 0;
176 }
177
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)
183 {
184         unsigned int i;
185         Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
186
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;
192         }
193         return 0;
194 }
195
196 /* Search for module by name: must hold module_mutex. */
197 static struct module *find_module(const char *name)
198 {
199         struct module *mod;
200
201         list_for_each_entry(mod, &modules, list) {
202                 if (strcmp(mod->name, name) == 0)
203                         return mod;
204         }
205         return NULL;
206 }
207
208 #ifdef CONFIG_MODULE_UNLOAD
209 /* Init the unload section of the module. */
210 static void module_unload_init(struct module *mod)
211 {
212         unsigned int i;
213
214         INIT_LIST_HEAD(&mod->modules_which_use_me);
215         for (i = 0; i < NR_CPUS; i++)
216                 atomic_set(&mod->ref[i].count, 0);
217         /* Backwards compatibility macros put refcount during init. */
218         mod->waiter = current;
219 }
220
221 /* modules using other modules */
222 struct module_use
223 {
224         struct list_head list;
225         struct module *module_which_uses;
226 };
227
228 /* Does a already use b? */
229 static int already_uses(struct module *a, struct module *b)
230 {
231         struct module_use *use;
232
233         list_for_each_entry(use, &b->modules_which_use_me, list) {
234                 if (use->module_which_uses == a) {
235                         DEBUGP("%s uses %s!\n", a->name, b->name);
236                         return 1;
237                 }
238         }
239         DEBUGP("%s does not use %s!\n", a->name, b->name);
240         return 0;
241 }
242
243 /* Module a uses b */
244 static int use_module(struct module *a, struct module *b)
245 {
246         struct module_use *use;
247         if (b == NULL || already_uses(a, b)) return 1;
248
249         if (!strong_try_module_get(b))
250                 return 0;
251
252         DEBUGP("Allocating new usage for %s.\n", a->name);
253         use = kmalloc(sizeof(*use), GFP_ATOMIC);
254         if (!use) {
255                 printk("%s: out of memory loading\n", a->name);
256                 module_put(b);
257                 return 0;
258         }
259
260         use->module_which_uses = a;
261         list_add(&use->list, &b->modules_which_use_me);
262         return 1;
263 }
264
265 /* Clear the unload stuff of the module. */
266 static void module_unload_free(struct module *mod)
267 {
268         struct module *i;
269
270         list_for_each_entry(i, &modules, list) {
271                 struct module_use *use;
272
273                 list_for_each_entry(use, &i->modules_which_use_me, list) {
274                         if (use->module_which_uses == mod) {
275                                 DEBUGP("%s unusing %s\n", mod->name, i->name);
276                                 module_put(i);
277                                 list_del(&use->list);
278                                 kfree(use);
279                                 /* There can be at most one match. */
280                                 break;
281                         }
282                 }
283         }
284 }
285
286 #ifdef CONFIG_SMP
287 /* Thread to stop each CPU in user context. */
288 enum stopref_state {
289         STOPREF_WAIT,
290         STOPREF_PREPARE,
291         STOPREF_DISABLE_IRQ,
292         STOPREF_EXIT,
293 };
294
295 static enum stopref_state stopref_state;
296 static unsigned int stopref_num_threads;
297 static atomic_t stopref_thread_ack;
298
299 static int stopref(void *cpu)
300 {
301         int irqs_disabled = 0;
302         int prepared = 0;
303
304         sprintf(current->comm, "kmodule%lu\n", (unsigned long)cpu);
305
306         /* Highest priority we can manage, and move to right CPU. */
307 #if 0 /* FIXME */
308         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
309         setscheduler(current->pid, SCHED_FIFO, &param);
310 #endif
311         set_cpus_allowed(current, 1UL << (unsigned long)cpu);
312
313         /* Ack: we are alive */
314         atomic_inc(&stopref_thread_ack);
315
316         /* Simple state machine */
317         while (stopref_state != STOPREF_EXIT) {
318                 if (stopref_state == STOPREF_DISABLE_IRQ && !irqs_disabled) {
319                         local_irq_disable();
320                         irqs_disabled = 1;
321                         /* Ack: irqs disabled. */
322                         atomic_inc(&stopref_thread_ack);
323                 } else if (stopref_state == STOPREF_PREPARE && !prepared) {
324                         /* Everyone is in place, hold CPU. */
325                         preempt_disable();
326                         prepared = 1;
327                         atomic_inc(&stopref_thread_ack);
328                 }
329                 if (irqs_disabled || prepared)
330                         cpu_relax();
331                 else
332                         yield();
333         }
334
335         /* Ack: we are exiting. */
336         atomic_inc(&stopref_thread_ack);
337
338         if (irqs_disabled)
339                 local_irq_enable();
340         if (prepared)
341                 preempt_enable();
342
343         return 0;
344 }
345
346 /* Change the thread state */
347 static void stopref_set_state(enum stopref_state state, int sleep)
348 {
349         atomic_set(&stopref_thread_ack, 0);
350         wmb();
351         stopref_state = state;
352         while (atomic_read(&stopref_thread_ack) != stopref_num_threads) {
353                 if (sleep)
354                         yield();
355                 else
356                         cpu_relax();
357         }
358 }
359
360 /* Stop the machine.  Disables irqs. */
361 static int stop_refcounts(void)
362 {
363         unsigned int i, cpu;
364         unsigned long old_allowed;
365         int ret = 0;
366
367         /* One thread per cpu.  We'll do our own. */
368         cpu = smp_processor_id();
369
370         /* FIXME: racy with set_cpus_allowed. */
371         old_allowed = current->cpus_allowed;
372         set_cpus_allowed(current, 1UL << (unsigned long)cpu);
373
374         atomic_set(&stopref_thread_ack, 0);
375         stopref_num_threads = 0;
376         stopref_state = STOPREF_WAIT;
377
378         /* No CPUs can come up or down during this. */
379         down(&cpucontrol);
380
381         for (i = 0; i < NR_CPUS; i++) {
382                 if (i == cpu || !cpu_online(i))
383                         continue;
384                 ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
385                 if (ret < 0)
386                         break;
387                 stopref_num_threads++;
388         }
389
390         /* Wait for them all to come to life. */
391         while (atomic_read(&stopref_thread_ack) != stopref_num_threads)
392                 yield();
393
394         /* If some failed, kill them all. */
395         if (ret < 0) {
396                 stopref_set_state(STOPREF_EXIT, 1);
397                 up(&cpucontrol);
398                 return ret;
399         }
400
401         /* Don't schedule us away at this point, please. */
402         preempt_disable();
403
404         /* Now they are all scheduled, make them hold the CPUs, ready. */
405         stopref_set_state(STOPREF_PREPARE, 0);
406
407         /* Make them disable irqs. */
408         stopref_set_state(STOPREF_DISABLE_IRQ, 0);
409
410         local_irq_disable();
411         return 0;
412 }
413
414 /* Restart the machine.  Re-enables irqs. */
415 static void restart_refcounts(void)
416 {
417         stopref_set_state(STOPREF_EXIT, 0);
418         local_irq_enable();
419         preempt_enable();
420         up(&cpucontrol);
421 }
422 #else /* ...!SMP */
423 static inline int stop_refcounts(void)
424 {
425         local_irq_disable();
426         return 0;
427 }
428 static inline void restart_refcounts(void)
429 {
430         local_irq_enable();
431 }
432 #endif
433
434 unsigned int module_refcount(struct module *mod)
435 {
436         unsigned int i, total = 0;
437
438         for (i = 0; i < NR_CPUS; i++)
439                 total += atomic_read(&mod->ref[i].count);
440         return total;
441 }
442 EXPORT_SYMBOL(module_refcount);
443
444 /* This exists whether we can unload or not */
445 static void free_module(struct module *mod);
446
447 #ifdef CONFIG_MODULE_FORCE_UNLOAD
448 static inline int try_force(unsigned int flags)
449 {
450         return (flags & O_TRUNC);
451 }
452 #else
453 static inline int try_force(unsigned int flags)
454 {
455         return 0;
456 }
457 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
458
459 /* Stub function for modules which don't have an exitfn */
460 void cleanup_module(void)
461 {
462 }
463 EXPORT_SYMBOL(cleanup_module);
464
465 asmlinkage long
466 sys_delete_module(const char __user *name_user, unsigned int flags)
467 {
468         struct module *mod;
469         char name[MODULE_NAME_LEN];
470         int ret, forced = 0;
471
472         if (!capable(CAP_SYS_MODULE))
473                 return -EPERM;
474
475         if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
476                 return -EFAULT;
477         name[MODULE_NAME_LEN-1] = '\0';
478
479         if (down_interruptible(&module_mutex) != 0)
480                 return -EINTR;
481
482         mod = find_module(name);
483         if (!mod) {
484                 ret = -ENOENT;
485                 goto out;
486         }
487
488         if (!list_empty(&mod->modules_which_use_me)) {
489                 /* Other modules depend on us: get rid of them first. */
490                 ret = -EWOULDBLOCK;
491                 goto out;
492         }
493
494         /* Already dying? */
495         if (mod->state == MODULE_STATE_GOING) {
496                 /* FIXME: if (force), slam module count and wake up
497                    waiter --RR */
498                 DEBUGP("%s already dying\n", mod->name);
499                 ret = -EBUSY;
500                 goto out;
501         }
502
503         /* Coming up?  Allow force on stuck modules. */
504         if (mod->state == MODULE_STATE_COMING) {
505                 forced = try_force(flags);
506                 if (!forced) {
507                         /* This module can't be removed */
508                         ret = -EBUSY;
509                         goto out;
510                 }
511         }
512
513         /* If it has an init func, it must have an exit func to unload */
514         if ((mod->init != init_module && mod->exit == cleanup_module)
515             || mod->unsafe) {
516                 forced = try_force(flags);
517                 if (!forced) {
518                         /* This module can't be removed */
519                         ret = -EBUSY;
520                         goto out;
521                 }
522         }
523         /* Stop the machine so refcounts can't move: irqs disabled. */
524         DEBUGP("Stopping refcounts...\n");
525         ret = stop_refcounts();
526         if (ret != 0)
527                 goto out;
528
529         /* If it's not unused, quit unless we are told to block. */
530         if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
531                 forced = try_force(flags);
532                 if (!forced)
533                         ret = -EWOULDBLOCK;
534         } else {
535                 mod->waiter = current;
536                 mod->state = MODULE_STATE_GOING;
537         }
538         restart_refcounts();
539
540         if (ret != 0)
541                 goto out;
542
543         if (forced)
544                 goto destroy;
545
546         /* Since we might sleep for some time, drop the semaphore first */
547         up(&module_mutex);
548         for (;;) {
549                 DEBUGP("Looking at refcount...\n");
550                 set_current_state(TASK_UNINTERRUPTIBLE);
551                 if (module_refcount(mod) == 0)
552                         break;
553                 schedule();
554         }
555         current->state = TASK_RUNNING;
556
557         DEBUGP("Regrabbing mutex...\n");
558         down(&module_mutex);
559
560  destroy:
561         /* Final destruction now noone is using it. */
562         mod->exit();
563         free_module(mod);
564
565  out:
566         up(&module_mutex);
567         return ret;
568 }
569
570 static void print_unload_info(struct seq_file *m, struct module *mod)
571 {
572         struct module_use *use;
573         int printed_something = 0;
574
575         seq_printf(m, " %u ", module_refcount(mod));
576
577         /* Always include a trailing , so userspace can differentiate
578            between this and the old multi-field proc format. */
579         list_for_each_entry(use, &mod->modules_which_use_me, list) {
580                 printed_something = 1;
581                 seq_printf(m, "%s,", use->module_which_uses->name);
582         }
583
584         if (mod->unsafe) {
585                 printed_something = 1;
586                 seq_printf(m, "[unsafe],");
587         }
588
589         if (mod->init != init_module && mod->exit == cleanup_module) {
590                 printed_something = 1;
591                 seq_printf(m, "[permanent],");
592         }
593
594         if (!printed_something)
595                 seq_printf(m, "-");
596 }
597
598 void __symbol_put(const char *symbol)
599 {
600         struct module *owner;
601         unsigned long flags;
602         const unsigned long *crc;
603
604         spin_lock_irqsave(&modlist_lock, flags);
605         if (!__find_symbol(symbol, &owner, &crc, 1))
606                 BUG();
607         module_put(owner);
608         spin_unlock_irqrestore(&modlist_lock, flags);
609 }
610 EXPORT_SYMBOL(__symbol_put);
611
612 void symbol_put_addr(void *addr)
613 {
614         unsigned long flags;
615
616         spin_lock_irqsave(&modlist_lock, flags);
617         if (!kernel_text_address((unsigned long)addr))
618                 BUG();
619
620         module_put(module_text_address((unsigned long)addr));
621         spin_unlock_irqrestore(&modlist_lock, flags);
622 }
623 EXPORT_SYMBOL_GPL(symbol_put_addr);
624
625 #else /* !CONFIG_MODULE_UNLOAD */
626 static void print_unload_info(struct seq_file *m, struct module *mod)
627 {
628         /* We don't know the usage count, or what modules are using. */
629         seq_printf(m, " - -");
630 }
631
632 static inline void module_unload_free(struct module *mod)
633 {
634 }
635
636 static inline int use_module(struct module *a, struct module *b)
637 {
638         return strong_try_module_get(b);
639 }
640
641 static inline void module_unload_init(struct module *mod)
642 {
643 }
644
645 asmlinkage long
646 sys_delete_module(const char *name_user, unsigned int flags)
647 {
648         return -ENOSYS;
649 }
650
651 #endif /* CONFIG_MODULE_UNLOAD */
652
653 #ifdef CONFIG_OBSOLETE_MODPARM
654 static int param_set_byte(const char *val, struct kernel_param *kp)  
655 {
656         char *endp;
657         long l;
658
659         if (!val) return -EINVAL;
660         l = simple_strtol(val, &endp, 0);
661         if (endp == val || *endp || ((char)l != l))
662                 return -EINVAL;
663         *((char *)kp->arg) = l;
664         return 0;
665 }
666
667 /* Bounds checking done below */
668 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
669 {
670         strcpy(kp->arg, val);
671         return 0;
672 }
673
674 int set_obsolete(const char *val, struct kernel_param *kp)
675 {
676         unsigned int min, max;
677         unsigned int size, maxsize;
678         char *endp;
679         const char *p;
680         struct obsolete_modparm *obsparm = kp->arg;
681
682         if (!val) {
683                 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
684                 return -EINVAL;
685         }
686
687         /* type is: [min[-max]]{b,h,i,l,s} */
688         p = obsparm->type;
689         min = simple_strtol(p, &endp, 10);
690         if (endp == obsparm->type)
691                 min = max = 1;
692         else if (*endp == '-') {
693                 p = endp+1;
694                 max = simple_strtol(p, &endp, 10);
695         } else
696                 max = min;
697         switch (*endp) {
698         case 'b':
699                 return param_array(kp->name, val, min, max, obsparm->addr,
700                                    1, param_set_byte);
701         case 'h':
702                 return param_array(kp->name, val, min, max, obsparm->addr,
703                                    sizeof(short), param_set_short);
704         case 'i':
705                 return param_array(kp->name, val, min, max, obsparm->addr,
706                                    sizeof(int), param_set_int);
707         case 'l':
708                 return param_array(kp->name, val, min, max, obsparm->addr,
709                                    sizeof(long), param_set_long);
710         case 's':
711                 return param_array(kp->name, val, min, max, obsparm->addr,
712                                    sizeof(char *), param_set_charp);
713
714         case 'c':
715                 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
716                    and the decl is "char xxx[5][50];" */
717                 p = endp+1;
718                 maxsize = simple_strtol(p, &endp, 10);
719                 /* We check lengths here (yes, this is a hack). */
720                 p = val;
721                 while (p[size = strcspn(p, ",")]) {
722                         if (size >= maxsize) 
723                                 goto oversize;
724                         p += size+1;
725                 }
726                 if (size >= maxsize) 
727                         goto oversize;
728                 return param_array(kp->name, val, min, max, obsparm->addr,
729                                    maxsize, obsparm_copy_string);
730         }
731         printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
732         return -EINVAL;
733  oversize:
734         printk(KERN_ERR
735                "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
736         return -EINVAL;
737 }
738
739 static int obsolete_params(const char *name,
740                            char *args,
741                            struct obsolete_modparm obsparm[],
742                            unsigned int num,
743                            Elf_Shdr *sechdrs,
744                            unsigned int symindex,
745                            const char *strtab)
746 {
747         struct kernel_param *kp;
748         unsigned int i;
749         int ret;
750
751         kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
752         if (!kp)
753                 return -ENOMEM;
754
755         for (i = 0; i < num; i++) {
756                 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
757
758                 snprintf(sym_name, sizeof(sym_name), "%s%s",
759                          MODULE_SYMBOL_PREFIX, obsparm[i].name);
760
761                 kp[i].name = obsparm[i].name;
762                 kp[i].perm = 000;
763                 kp[i].set = set_obsolete;
764                 kp[i].get = NULL;
765                 obsparm[i].addr
766                         = (void *)find_local_symbol(sechdrs, symindex, strtab,
767                                                     sym_name);
768                 if (!obsparm[i].addr) {
769                         printk("%s: falsely claims to have parameter %s\n",
770                                name, obsparm[i].name);
771                         ret = -EINVAL;
772                         goto out;
773                 }
774                 kp[i].arg = &obsparm[i];
775         }
776
777         ret = parse_args(name, args, kp, num, NULL);
778  out:
779         kfree(kp);
780         return ret;
781 }
782 #else
783 static int obsolete_params(const char *name,
784                            char *args,
785                            struct obsolete_modparm obsparm[],
786                            unsigned int num,
787                            Elf_Shdr *sechdrs,
788                            unsigned int symindex,
789                            const char *strtab)
790 {
791         if (num != 0)
792                 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
793                        name);
794         return 0;
795 }
796 #endif /* CONFIG_OBSOLETE_MODPARM */
797
798 static const char vermagic[] = VERMAGIC_STRING;
799
800 #ifdef CONFIG_MODVERSIONS
801 static int check_version(Elf_Shdr *sechdrs,
802                          unsigned int versindex,
803                          const char *symname,
804                          struct module *mod, 
805                          const unsigned long *crc)
806 {
807         unsigned int i, num_versions;
808         struct modversion_info *versions;
809
810         /* Exporting module didn't supply crcs?  OK, we're already tainted. */
811         if (!crc)
812                 return 1;
813
814         versions = (void *) sechdrs[versindex].sh_addr;
815         num_versions = sechdrs[versindex].sh_size
816                 / sizeof(struct modversion_info);
817
818         for (i = 0; i < num_versions; i++) {
819                 if (strcmp(versions[i].name, symname) != 0)
820                         continue;
821
822                 if (versions[i].crc == *crc)
823                         return 1;
824                 printk("%s: disagrees about version of symbol %s\n",
825                        mod->name, symname);
826                 DEBUGP("Found checksum %lX vs module %lX\n",
827                        *crc, versions[i].crc);
828                 return 0;
829         }
830         /* Not in module's version table.  OK, but that taints the kernel. */
831         if (!(tainted & TAINT_FORCED_MODULE)) {
832                 printk("%s: no version for \"%s\" found: kernel tainted.\n",
833                        mod->name, symname);
834                 tainted |= TAINT_FORCED_MODULE;
835         }
836         return 1;
837 }
838
839 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
840                                           unsigned int versindex,
841                                           struct module *mod)
842 {
843         const unsigned long *crc;
844         struct module *owner;
845
846         if (!__find_symbol("struct_module", &owner, &crc, 1))
847                 BUG();
848         return check_version(sechdrs, versindex, "struct_module", mod,
849                              crc);
850 }
851
852 /* First part is kernel version, which we ignore. */
853 static inline int same_magic(const char *amagic, const char *bmagic)
854 {
855         amagic += strcspn(amagic, " ");
856         bmagic += strcspn(bmagic, " ");
857         return strcmp(amagic, bmagic) == 0;
858 }
859 #else
860 static inline int check_version(Elf_Shdr *sechdrs,
861                                 unsigned int versindex,
862                                 const char *symname,
863                                 struct module *mod, 
864                                 const unsigned long *crc)
865 {
866         return 1;
867 }
868
869 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
870                                           unsigned int versindex,
871                                           struct module *mod)
872 {
873         return 1;
874 }
875
876 static inline int same_magic(const char *amagic, const char *bmagic)
877 {
878         return strcmp(amagic, bmagic) == 0;
879 }
880 #endif /* CONFIG_MODVERSIONS */
881
882 /* Resolve a symbol for this module.  I.e. if we find one, record usage.
883    Must be holding module_mutex. */
884 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
885                                     unsigned int versindex,
886                                     const char *name,
887                                     struct module *mod)
888 {
889         struct module *owner;
890         unsigned long ret;
891         const unsigned long *crc;
892
893         spin_lock_irq(&modlist_lock);
894         ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
895         if (ret) {
896                 /* use_module can fail due to OOM, or module unloading */
897                 if (!check_version(sechdrs, versindex, name, mod, crc) ||
898                     !use_module(mod, owner))
899                         ret = 0;
900         }
901         spin_unlock_irq(&modlist_lock);
902         return ret;
903 }
904
905 /* Free a module, remove from lists, etc (must hold module mutex). */
906 static void free_module(struct module *mod)
907 {
908         /* Delete from various lists */
909         spin_lock_irq(&modlist_lock);
910         list_del(&mod->list);
911         spin_unlock_irq(&modlist_lock);
912
913         /* Arch-specific cleanup. */
914         module_arch_cleanup(mod);
915
916         /* Module unload stuff */
917         module_unload_free(mod);
918
919         /* This may be NULL, but that's OK */
920         module_free(mod, mod->module_init);
921         kfree(mod->args);
922
923         /* Finally, free the core (containing the module structure) */
924         module_free(mod, mod->module_core);
925 }
926
927 void *__symbol_get(const char *symbol)
928 {
929         struct module *owner;
930         unsigned long value, flags;
931         const unsigned long *crc;
932
933         spin_lock_irqsave(&modlist_lock, flags);
934         value = __find_symbol(symbol, &owner, &crc, 1);
935         if (value && !strong_try_module_get(owner))
936                 value = 0;
937         spin_unlock_irqrestore(&modlist_lock, flags);
938
939         return (void *)value;
940 }
941 EXPORT_SYMBOL_GPL(__symbol_get);
942
943 /* Change all symbols so that sh_value encodes the pointer directly. */
944 static int simplify_symbols(Elf_Shdr *sechdrs,
945                             unsigned int symindex,
946                             const char *strtab,
947                             unsigned int versindex,
948                             struct module *mod)
949 {
950         Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
951         
952         unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
953         int ret = 0;
954
955         for (i = 1; i < n; i++) {
956                 switch (sym[i].st_shndx) {
957                 case SHN_COMMON:
958                         /* We compiled with -fno-common.  These are not
959                            supposed to happen.  */
960                         DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
961                         ret = -ENOEXEC;
962                         break;
963
964                 case SHN_ABS:
965                         /* Don't need to do anything */
966                         DEBUGP("Absolute symbol: 0x%08lx\n",
967                                (long)sym[i].st_value);
968                         break;
969
970                 case SHN_UNDEF:
971                         sym[i].st_value
972                           = resolve_symbol(sechdrs, versindex,
973                                            strtab + sym[i].st_name, mod);
974
975                         /* Ok if resolved.  */
976                         if (sym[i].st_value != 0)
977                                 break;
978                         /* Ok if weak.  */
979                         if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
980                                 break;
981
982                         printk(KERN_WARNING "%s: Unknown symbol %s\n",
983                                mod->name, strtab + sym[i].st_name);
984                         ret = -ENOENT;
985                         break;
986
987                 default:
988                         sym[i].st_value 
989                                 = (unsigned long)
990                                 (sechdrs[sym[i].st_shndx].sh_addr
991                                  + sym[i].st_value);
992                         break;
993                 }
994         }
995
996         return ret;
997 }
998
999 /* Update size with this section: return offset. */
1000 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1001 {
1002         long ret;
1003
1004         ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1005         *size = ret + sechdr->sh_size;
1006         return ret;
1007 }
1008
1009 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1010    might -- code, read-only data, read-write data, small data.  Tally
1011    sizes, and place the offsets into sh_entsize fields: high bit means it
1012    belongs in init. */
1013 static void layout_sections(struct module *mod,
1014                             const Elf_Ehdr *hdr,
1015                             Elf_Shdr *sechdrs,
1016                             const char *secstrings)
1017 {
1018         static unsigned long const masks[][2] = {
1019                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1020                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1021                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1022                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1023         };
1024         unsigned int m, i;
1025
1026         for (i = 0; i < hdr->e_shnum; i++)
1027                 sechdrs[i].sh_entsize = ~0UL;
1028
1029         DEBUGP("Core section allocation order:\n");
1030         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1031                 for (i = 0; i < hdr->e_shnum; ++i) {
1032                         Elf_Shdr *s = &sechdrs[i];
1033
1034                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1035                             || (s->sh_flags & masks[m][1])
1036                             || s->sh_entsize != ~0UL
1037                             || strstr(secstrings + s->sh_name, ".init"))
1038                                 continue;
1039                         s->sh_entsize = get_offset(&mod->core_size, s);
1040                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1041                 }
1042         }
1043
1044         DEBUGP("Init section allocation order:\n");
1045         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1046                 for (i = 0; i < hdr->e_shnum; ++i) {
1047                         Elf_Shdr *s = &sechdrs[i];
1048
1049                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1050                             || (s->sh_flags & masks[m][1])
1051                             || s->sh_entsize != ~0UL
1052                             || !strstr(secstrings + s->sh_name, ".init"))
1053                                 continue;
1054                         s->sh_entsize = (get_offset(&mod->init_size, s)
1055                                          | INIT_OFFSET_MASK);
1056                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1057                 }
1058         }
1059 }
1060
1061 static inline int license_is_gpl_compatible(const char *license)
1062 {
1063         return (strcmp(license, "GPL") == 0
1064                 || strcmp(license, "GPL v2") == 0
1065                 || strcmp(license, "GPL and additional rights") == 0
1066                 || strcmp(license, "Dual BSD/GPL") == 0
1067                 || strcmp(license, "Dual MPL/GPL") == 0);
1068 }
1069
1070 static void set_license(struct module *mod, const char *license)
1071 {
1072         if (!license)
1073                 license = "unspecified";
1074
1075         mod->license_gplok = license_is_gpl_compatible(license);
1076         if (!mod->license_gplok) {
1077                 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1078                        mod->name, license);
1079                 tainted |= TAINT_PROPRIETARY_MODULE;
1080         }
1081 }
1082
1083 /* Parse tag=value strings from .modinfo section */
1084 static char *next_string(char *string, unsigned long *secsize)
1085 {
1086         /* Skip non-zero chars */
1087         while (string[0]) {
1088                 string++;
1089                 if ((*secsize)-- <= 1)
1090                         return NULL;
1091         }
1092
1093         /* Skip any zero padding. */
1094         while (!string[0]) {
1095                 string++;
1096                 if ((*secsize)-- <= 1)
1097                         return NULL;
1098         }
1099         return string;
1100 }
1101
1102 static char *get_modinfo(Elf_Shdr *sechdrs,
1103                          unsigned int info,
1104                          const char *tag)
1105 {
1106         char *p;
1107         unsigned int taglen = strlen(tag);
1108         unsigned long size = sechdrs[info].sh_size;
1109
1110         for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1111                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1112                         return p + taglen + 1;
1113         }
1114         return NULL;
1115 }
1116
1117 /* Allocate and load the module: note that size of section 0 is always
1118    zero, and we rely on this for optional sections. */
1119 static struct module *load_module(void __user *umod,
1120                                   unsigned long len,
1121                                   const char __user *uargs)
1122 {
1123         Elf_Ehdr *hdr;
1124         Elf_Shdr *sechdrs;
1125         char *secstrings, *args, *modmagic, *strtab = NULL;
1126         unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1127                 exportindex, modindex, obsparmindex, infoindex, gplindex,
1128                 crcindex, gplcrcindex, versindex;
1129         long arglen;
1130         struct module *mod;
1131         long err = 0;
1132         void *ptr = NULL; /* Stops spurious gcc uninitialized warning */
1133
1134         DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1135                umod, len, uargs);
1136         if (len < sizeof(*hdr))
1137                 return ERR_PTR(-ENOEXEC);
1138
1139         /* Suck in entire file: we'll want most of it. */
1140         /* vmalloc barfs on "unusual" numbers.  Check here */
1141         if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1142                 return ERR_PTR(-ENOMEM);
1143         if (copy_from_user(hdr, umod, len) != 0) {
1144                 err = -EFAULT;
1145                 goto free_hdr;
1146         }
1147
1148         /* Sanity checks against insmoding binaries or wrong arch,
1149            weird elf version */
1150         if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1151             || hdr->e_type != ET_REL
1152             || !elf_check_arch(hdr)
1153             || hdr->e_shentsize != sizeof(*sechdrs)) {
1154                 err = -ENOEXEC;
1155                 goto free_hdr;
1156         }
1157
1158         /* Convenience variables */
1159         sechdrs = (void *)hdr + hdr->e_shoff;
1160         secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1161         sechdrs[0].sh_addr = 0;
1162
1163         /* And these should exist, but gcc whinges if we don't init them */
1164         symindex = strindex = 0;
1165
1166         for (i = 1; i < hdr->e_shnum; i++) {
1167                 /* Mark all sections sh_addr with their address in the
1168                    temporary image. */
1169                 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1170
1171                 /* Internal symbols and strings. */
1172                 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1173                         symindex = i;
1174                         strindex = sechdrs[i].sh_link;
1175                         strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1176                 }
1177 #ifndef CONFIG_MODULE_UNLOAD
1178                 /* Don't load .exit sections */
1179                 if (strstr(secstrings+sechdrs[i].sh_name, ".exit"))
1180                         sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1181 #endif
1182         }
1183
1184         modindex = find_sec(hdr, sechdrs, secstrings,
1185                             ".gnu.linkonce.this_module");
1186         if (!modindex) {
1187                 printk(KERN_WARNING "No module found in object\n");
1188                 err = -ENOEXEC;
1189                 goto free_hdr;
1190         }
1191         mod = (void *)sechdrs[modindex].sh_addr;
1192
1193         /* Optional sections */
1194         exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1195         gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1196         crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1197         gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1198         setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1199         exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1200         obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1201         versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1202         infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1203
1204         /* Don't keep modinfo section */
1205         sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1206 #ifdef CONFIG_KALLSYMS
1207         /* Keep symbol and string tables for decoding later. */
1208         sechdrs[symindex].sh_flags |= SHF_ALLOC;
1209         sechdrs[strindex].sh_flags |= SHF_ALLOC;
1210 #endif
1211
1212         /* Check module struct version now, before we try to use module. */
1213         if (!check_modstruct_version(sechdrs, versindex, mod)) {
1214                 err = -ENOEXEC;
1215                 goto free_hdr;
1216         }
1217
1218         modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1219         /* This is allowed: modprobe --force will invalidate it. */
1220         if (!modmagic) {
1221                 tainted |= TAINT_FORCED_MODULE;
1222                 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1223                        mod->name);
1224         } else if (!same_magic(modmagic, vermagic)) {
1225                 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1226                        mod->name, modmagic, vermagic);
1227                 err = -ENOEXEC;
1228                 goto free_hdr;
1229         }
1230
1231         /* Now copy in args */
1232         arglen = strlen_user(uargs);
1233         if (!arglen) {
1234                 err = -EFAULT;
1235                 goto free_hdr;
1236         }
1237         args = kmalloc(arglen, GFP_KERNEL);
1238         if (!args) {
1239                 err = -ENOMEM;
1240                 goto free_hdr;
1241         }
1242         if (copy_from_user(args, uargs, arglen) != 0) {
1243                 err = -EFAULT;
1244                 goto free_mod;
1245         }
1246
1247         if (find_module(mod->name)) {
1248                 err = -EEXIST;
1249                 goto free_mod;
1250         }
1251
1252         mod->state = MODULE_STATE_COMING;
1253
1254         /* Allow arches to frob section contents and sizes.  */
1255         err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1256         if (err < 0)
1257                 goto free_mod;
1258
1259         /* Determine total sizes, and put offsets in sh_entsize.  For now
1260            this is done generically; there doesn't appear to be any
1261            special cases for the architectures. */
1262         layout_sections(mod, hdr, sechdrs, secstrings);
1263
1264         /* Do the allocs. */
1265         ptr = module_alloc(mod->core_size);
1266         if (!ptr) {
1267                 err = -ENOMEM;
1268                 goto free_mod;
1269         }
1270         memset(ptr, 0, mod->core_size);
1271         mod->module_core = ptr;
1272
1273         ptr = module_alloc(mod->init_size);
1274         if (!ptr && mod->init_size) {
1275                 err = -ENOMEM;
1276                 goto free_core;
1277         }
1278         memset(ptr, 0, mod->init_size);
1279         mod->module_init = ptr;
1280
1281         /* Transfer each section which specifies SHF_ALLOC */
1282         DEBUGP("final section addresses:\n");
1283         for (i = 0; i < hdr->e_shnum; i++) {
1284                 void *dest;
1285
1286                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1287                         continue;
1288
1289                 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1290                         dest = mod->module_init
1291                                 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1292                 else
1293                         dest = mod->module_core + sechdrs[i].sh_entsize;
1294
1295                 if (sechdrs[i].sh_type != SHT_NOBITS)
1296                         memcpy(dest, (void *)sechdrs[i].sh_addr,
1297                                sechdrs[i].sh_size);
1298                 /* Update sh_addr to point to copy in image. */
1299                 sechdrs[i].sh_addr = (unsigned long)dest;
1300                 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1301         }
1302         /* Module has been moved. */
1303         mod = (void *)sechdrs[modindex].sh_addr;
1304
1305         /* Now we've moved module, initialize linked lists, etc. */
1306         module_unload_init(mod);
1307
1308         /* Set up license info based on the info section */
1309         set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1310
1311         /* Fix up syms, so that st_value is a pointer to location. */
1312         err = simplify_symbols(sechdrs, symindex, strtab, versindex, mod);
1313         if (err < 0)
1314                 goto cleanup;
1315
1316         /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1317         mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1318         mod->syms = (void *)sechdrs[exportindex].sh_addr;
1319         if (crcindex)
1320                 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1321         mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1322         mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1323         if (gplcrcindex)
1324                 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1325
1326 #ifdef CONFIG_MODVERSIONS
1327         if ((mod->num_syms && !crcindex) || 
1328             (mod->num_gpl_syms && !gplcrcindex)) {
1329                 printk(KERN_WARNING "%s: No versions for exported symbols."
1330                        " Tainting kernel.\n", mod->name);
1331                 tainted |= TAINT_FORCED_MODULE;
1332         }
1333 #endif
1334
1335         /* Set up exception table */
1336         mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1337         mod->extable = (void *)sechdrs[exindex].sh_addr;
1338
1339         /* Now do relocations. */
1340         for (i = 1; i < hdr->e_shnum; i++) {
1341                 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1342                 if (sechdrs[i].sh_type == SHT_REL)
1343                         err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1344                 else if (sechdrs[i].sh_type == SHT_RELA)
1345                         err = apply_relocate_add(sechdrs, strtab, symindex, i,
1346                                                  mod);
1347                 if (err < 0)
1348                         goto cleanup;
1349         }
1350
1351 #ifdef CONFIG_KALLSYMS
1352         mod->symtab = (void *)sechdrs[symindex].sh_addr;
1353         mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1354         mod->strtab = (void *)sechdrs[strindex].sh_addr;
1355 #endif
1356         err = module_finalize(hdr, sechdrs, mod);
1357         if (err < 0)
1358                 goto cleanup;
1359
1360         mod->args = args;
1361         if (obsparmindex) {
1362                 err = obsolete_params(mod->name, mod->args,
1363                                       (struct obsolete_modparm *)
1364                                       sechdrs[obsparmindex].sh_addr,
1365                                       sechdrs[obsparmindex].sh_size
1366                                       / sizeof(struct obsolete_modparm),
1367                                       sechdrs, symindex,
1368                                       (char *)sechdrs[strindex].sh_addr);
1369         } else {
1370                 /* Size of section 0 is 0, so this works well if no params */
1371                 err = parse_args(mod->name, mod->args,
1372                                  (struct kernel_param *)
1373                                  sechdrs[setupindex].sh_addr,
1374                                  sechdrs[setupindex].sh_size
1375                                  / sizeof(struct kernel_param),
1376                                  NULL);
1377         }
1378         if (err < 0)
1379                 goto cleanup;
1380
1381         /* Get rid of temporary copy */
1382         vfree(hdr);
1383
1384         /* Done! */
1385         return mod;
1386
1387  cleanup:
1388         module_unload_free(mod);
1389         module_free(mod, mod->module_init);
1390  free_core:
1391         module_free(mod, mod->module_core);
1392  free_mod:
1393         kfree(args);
1394  free_hdr:
1395         vfree(hdr);
1396         if (err < 0) return ERR_PTR(err);
1397         else return ptr;
1398 }
1399
1400 /* This is where the real work happens */
1401 asmlinkage long
1402 sys_init_module(void __user *umod,
1403                 unsigned long len,
1404                 const char __user *uargs)
1405 {
1406         struct module *mod;
1407         int ret;
1408
1409         /* Must have permission */
1410         if (!capable(CAP_SYS_MODULE))
1411                 return -EPERM;
1412
1413         /* Only one module load at a time, please */
1414         if (down_interruptible(&module_mutex) != 0)
1415                 return -EINTR;
1416
1417         /* Do all the hard work */
1418         mod = load_module(umod, len, uargs);
1419         if (IS_ERR(mod)) {
1420                 up(&module_mutex);
1421                 return PTR_ERR(mod);
1422         }
1423
1424         /* Flush the instruction cache, since we've played with text */
1425         if (mod->module_init)
1426                 flush_icache_range((unsigned long)mod->module_init,
1427                                    (unsigned long)mod->module_init
1428                                    + mod->init_size);
1429         flush_icache_range((unsigned long)mod->module_core,
1430                            (unsigned long)mod->module_core + mod->core_size);
1431
1432         /* Now sew it into the lists.  They won't access us, since
1433            strong_try_module_get() will fail. */
1434         spin_lock_irq(&modlist_lock);
1435         list_add(&mod->list, &modules);
1436         spin_unlock_irq(&modlist_lock);
1437
1438         /* Drop lock so they can recurse */
1439         up(&module_mutex);
1440
1441         down(&notify_mutex);
1442         notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1443         up(&notify_mutex);
1444
1445         /* Start the module */
1446         ret = mod->init();
1447         if (ret < 0) {
1448                 /* Init routine failed: abort.  Try to protect us from
1449                    buggy refcounters. */
1450                 mod->state = MODULE_STATE_GOING;
1451                 synchronize_kernel();
1452                 if (mod->unsafe)
1453                         printk(KERN_ERR "%s: module is now stuck!\n",
1454                                mod->name);
1455                 else {
1456                         down(&module_mutex);
1457                         free_module(mod);
1458                         up(&module_mutex);
1459                 }
1460                 return ret;
1461         }
1462
1463         /* Now it's a first class citizen! */
1464         down(&module_mutex);
1465         mod->state = MODULE_STATE_LIVE;
1466         module_free(mod, mod->module_init);
1467         mod->module_init = NULL;
1468         mod->init_size = 0;
1469         up(&module_mutex);
1470
1471         return 0;
1472 }
1473
1474 static inline int within(unsigned long addr, void *start, unsigned long size)
1475 {
1476         return ((void *)addr >= start && (void *)addr < start + size);
1477 }
1478
1479 #ifdef CONFIG_KALLSYMS
1480 static const char *get_ksymbol(struct module *mod,
1481                                unsigned long addr,
1482                                unsigned long *size,
1483                                unsigned long *offset)
1484 {
1485         unsigned int i, best = 0;
1486         unsigned long nextval;
1487
1488         /* At worse, next value is at end of module */
1489         if (within(addr, mod->module_init, mod->init_size))
1490                 nextval = (unsigned long)mod->module_init + mod->init_size;
1491         else 
1492                 nextval = (unsigned long)mod->module_core + mod->core_size;
1493
1494         /* Scan for closest preceeding symbol, and next symbol. (ELF
1495            starts real symbols at 1). */
1496         for (i = 1; i < mod->num_symtab; i++) {
1497                 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1498                         continue;
1499
1500                 if (mod->symtab[i].st_value <= addr
1501                     && mod->symtab[i].st_value > mod->symtab[best].st_value)
1502                         best = i;
1503                 if (mod->symtab[i].st_value > addr
1504                     && mod->symtab[i].st_value < nextval)
1505                         nextval = mod->symtab[i].st_value;
1506         }
1507
1508         if (!best)
1509                 return NULL;
1510
1511         *size = nextval - mod->symtab[best].st_value;
1512         *offset = addr - mod->symtab[best].st_value;
1513         return mod->strtab + mod->symtab[best].st_name;
1514 }
1515
1516 /* For kallsyms to ask for address resolution.  NULL means not found.
1517    We don't lock, as this is used for oops resolution and races are a
1518    lesser concern. */
1519 const char *module_address_lookup(unsigned long addr,
1520                                   unsigned long *size,
1521                                   unsigned long *offset,
1522                                   char **modname)
1523 {
1524         struct module *mod;
1525
1526         list_for_each_entry(mod, &modules, list) {
1527                 if (within(addr, mod->module_init, mod->init_size)
1528                     || within(addr, mod->module_core, mod->core_size)) {
1529                         *modname = mod->name;
1530                         return get_ksymbol(mod, addr, size, offset);
1531                 }
1532         }
1533         return NULL;
1534 }
1535 #endif /* CONFIG_KALLSYMS */
1536
1537 /* Called by the /proc file system to return a list of modules. */
1538 static void *m_start(struct seq_file *m, loff_t *pos)
1539 {
1540         struct list_head *i;
1541         loff_t n = 0;
1542
1543         down(&module_mutex);
1544         list_for_each(i, &modules) {
1545                 if (n++ == *pos)
1546                         break;
1547         }
1548         if (i == &modules)
1549                 return NULL;
1550         return i;
1551 }
1552
1553 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1554 {
1555         struct list_head *i = p;
1556         (*pos)++;
1557         if (i->next == &modules)
1558                 return NULL;
1559         return i->next;
1560 }
1561
1562 static void m_stop(struct seq_file *m, void *p)
1563 {
1564         up(&module_mutex);
1565 }
1566
1567 static int m_show(struct seq_file *m, void *p)
1568 {
1569         struct module *mod = list_entry(p, struct module, list);
1570         seq_printf(m, "%s %lu",
1571                    mod->name, mod->init_size + mod->core_size);
1572         print_unload_info(m, mod);
1573
1574         /* Informative for users. */
1575         seq_printf(m, " %s",
1576                    mod->state == MODULE_STATE_GOING ? "Unloading":
1577                    mod->state == MODULE_STATE_COMING ? "Loading":
1578                    "Live");
1579         /* Used by oprofile and other similar tools. */
1580         seq_printf(m, " 0x%p", mod->module_core);
1581
1582         seq_printf(m, "\n");
1583         return 0;
1584 }
1585
1586 /* Format: modulename size refcount deps address
1587
1588    Where refcount is a number or -, and deps is a comma-separated list
1589    of depends or -.
1590 */
1591 struct seq_operations modules_op = {
1592         .start  = m_start,
1593         .next   = m_next,
1594         .stop   = m_stop,
1595         .show   = m_show
1596 };
1597
1598 /* Given an address, look for it in the module exception tables. */
1599 const struct exception_table_entry *search_module_extables(unsigned long addr)
1600 {
1601         unsigned long flags;
1602         const struct exception_table_entry *e = NULL;
1603         struct module *mod;
1604
1605         spin_lock_irqsave(&modlist_lock, flags);
1606         list_for_each_entry(mod, &modules, list) {
1607                 if (mod->num_exentries == 0)
1608                         continue;
1609                                 
1610                 e = search_extable(mod->extable,
1611                                    mod->extable + mod->num_exentries - 1,
1612                                    addr);
1613                 if (e)
1614                         break;
1615         }
1616         spin_unlock_irqrestore(&modlist_lock, flags);
1617
1618         /* Now, if we found one, we are running inside it now, hence
1619            we cannot unload the module, hence no refcnt needed. */
1620         return e;
1621 }
1622
1623 /* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
1624 struct module *module_text_address(unsigned long addr)
1625 {
1626         struct module *mod;
1627
1628         list_for_each_entry(mod, &modules, list)
1629                 if (within(addr, mod->module_init, mod->init_size)
1630                     || within(addr, mod->module_core, mod->core_size))
1631                         return mod;
1632         return NULL;
1633 }
1634
1635 #ifdef CONFIG_MODVERSIONS
1636 /* Generate the signature for struct module here, too, for modversions. */
1637 void struct_module(struct module *mod) { return; }
1638 EXPORT_SYMBOL(struct_module);
1639 #endif