upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.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/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
40 #include <asm/cacheflush.h>
41 #include "module-verify.h"
42
43 #if 0
44 #define DEBUGP printk
45 #else
46 #define DEBUGP(fmt , a...)
47 #endif
48
49 #ifndef ARCH_SHF_SMALL
50 #define ARCH_SHF_SMALL 0
51 #endif
52
53 /* If this is set, the section belongs in the init part of the module */
54 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
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 /* A thread that wants to hold a reference to a module only while it
95  * is running can call ths to safely exit.
96  * nfsd and lockd use this.
97  */
98 void __module_put_and_exit(struct module *mod, long code)
99 {
100         module_put(mod);
101         do_exit(code);
102 }
103 EXPORT_SYMBOL(__module_put_and_exit);
104         
105 /* Find a module section: 0 means not found. */
106 static unsigned int find_sec(Elf_Ehdr *hdr,
107                              Elf_Shdr *sechdrs,
108                              const char *secstrings,
109                              const char *name)
110 {
111         unsigned int i;
112
113         for (i = 1; i < hdr->e_shnum; i++)
114                 /* Alloc bit cleared means "ignore it." */
115                 if ((sechdrs[i].sh_flags & SHF_ALLOC)
116                     && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
117                         return i;
118         return 0;
119 }
120
121 /* Provided by the linker */
122 extern const struct kernel_symbol __start___ksymtab[];
123 extern const struct kernel_symbol __stop___ksymtab[];
124 extern const struct kernel_symbol __start___ksymtab_gpl[];
125 extern const struct kernel_symbol __stop___ksymtab_gpl[];
126 extern const unsigned long __start___kcrctab[];
127 extern const unsigned long __start___kcrctab_gpl[];
128
129 #ifndef CONFIG_MODVERSIONS
130 #define symversion(base, idx) NULL
131 #else
132 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
133 #endif
134
135 /* Find a symbol, return value, crc and module which owns it */
136 static unsigned long __find_symbol(const char *name,
137                                    struct module **owner,
138                                    const unsigned long **crc,
139                                    int gplok)
140 {
141         struct module *mod;
142         unsigned int i;
143
144         /* Core kernel first. */ 
145         *owner = NULL;
146         for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
147                 if (strcmp(__start___ksymtab[i].name, name) == 0) {
148                         *crc = symversion(__start___kcrctab, i);
149                         return __start___ksymtab[i].value;
150                 }
151         }
152         if (gplok) {
153                 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
154                         if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
155                                 *crc = symversion(__start___kcrctab_gpl, i);
156                                 return __start___ksymtab_gpl[i].value;
157                         }
158         }
159
160         /* Now try modules. */ 
161         list_for_each_entry(mod, &modules, list) {
162                 *owner = mod;
163                 for (i = 0; i < mod->num_syms; i++)
164                         if (strcmp(mod->syms[i].name, name) == 0) {
165                                 *crc = symversion(mod->crcs, i);
166                                 return mod->syms[i].value;
167                         }
168
169                 if (gplok) {
170                         for (i = 0; i < mod->num_gpl_syms; i++) {
171                                 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
172                                         *crc = symversion(mod->gpl_crcs, i);
173                                         return mod->gpl_syms[i].value;
174                                 }
175                         }
176                 }
177         }
178         DEBUGP("Failed to find symbol %s\n", name);
179         return 0;
180 }
181
182 /* Find a symbol in this elf symbol table */
183 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
184                                        unsigned int symindex,
185                                        const char *strtab,
186                                        const char *name)
187 {
188         unsigned int i;
189         Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
190
191         /* Search (defined) internal symbols first. */
192         for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
193                 if (sym[i].st_shndx != SHN_UNDEF
194                     && strcmp(name, strtab + sym[i].st_name) == 0)
195                         return sym[i].st_value;
196         }
197         return 0;
198 }
199
200 /* Search for module by name: must hold module_mutex. */
201 static struct module *find_module(const char *name)
202 {
203         struct module *mod;
204
205         list_for_each_entry(mod, &modules, list) {
206                 if (strcmp(mod->name, name) == 0)
207                         return mod;
208         }
209         return NULL;
210 }
211
212 #ifdef CONFIG_SMP
213 /* Number of blocks used and allocated. */
214 static unsigned int pcpu_num_used, pcpu_num_allocated;
215 /* Size of each block.  -ve means used. */
216 static int *pcpu_size;
217
218 static int split_block(unsigned int i, unsigned short size)
219 {
220         /* Reallocation required? */
221         if (pcpu_num_used + 1 > pcpu_num_allocated) {
222                 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
223                                    GFP_KERNEL);
224                 if (!new)
225                         return 0;
226
227                 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
228                 pcpu_num_allocated *= 2;
229                 kfree(pcpu_size);
230                 pcpu_size = new;
231         }
232
233         /* Insert a new subblock */
234         memmove(&pcpu_size[i+1], &pcpu_size[i],
235                 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
236         pcpu_num_used++;
237
238         pcpu_size[i+1] -= size;
239         pcpu_size[i] = size;
240         return 1;
241 }
242
243 static inline unsigned int block_size(int val)
244 {
245         if (val < 0)
246                 return -val;
247         return val;
248 }
249
250 /* Created by linker magic */
251 extern char __per_cpu_start[], __per_cpu_end[];
252
253 static void *percpu_modalloc(unsigned long size, unsigned long align)
254 {
255         unsigned long extra;
256         unsigned int i;
257         void *ptr;
258
259         BUG_ON(align > SMP_CACHE_BYTES);
260
261         ptr = __per_cpu_start;
262         for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
263                 /* Extra for alignment requirement. */
264                 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
265                 BUG_ON(i == 0 && extra != 0);
266
267                 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
268                         continue;
269
270                 /* Transfer extra to previous block. */
271                 if (pcpu_size[i-1] < 0)
272                         pcpu_size[i-1] -= extra;
273                 else
274                         pcpu_size[i-1] += extra;
275                 pcpu_size[i] -= extra;
276                 ptr += extra;
277
278                 /* Split block if warranted */
279                 if (pcpu_size[i] - size > sizeof(unsigned long))
280                         if (!split_block(i, size))
281                                 return NULL;
282
283                 /* Mark allocated */
284                 pcpu_size[i] = -pcpu_size[i];
285                 return ptr;
286         }
287
288         printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
289                size);
290         return NULL;
291 }
292
293 static void percpu_modfree(void *freeme)
294 {
295         unsigned int i;
296         void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
297
298         /* First entry is core kernel percpu data. */
299         for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
300                 if (ptr == freeme) {
301                         pcpu_size[i] = -pcpu_size[i];
302                         goto free;
303                 }
304         }
305         BUG();
306
307  free:
308         /* Merge with previous? */
309         if (pcpu_size[i-1] >= 0) {
310                 pcpu_size[i-1] += pcpu_size[i];
311                 pcpu_num_used--;
312                 memmove(&pcpu_size[i], &pcpu_size[i+1],
313                         (pcpu_num_used - i) * sizeof(pcpu_size[0]));
314                 i--;
315         }
316         /* Merge with next? */
317         if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
318                 pcpu_size[i] += pcpu_size[i+1];
319                 pcpu_num_used--;
320                 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
321                         (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
322         }
323 }
324
325 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
326                                  Elf_Shdr *sechdrs,
327                                  const char *secstrings)
328 {
329         return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
330 }
331
332 static int percpu_modinit(void)
333 {
334         pcpu_num_used = 2;
335         pcpu_num_allocated = 2;
336         pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
337                             GFP_KERNEL);
338         /* Static in-kernel percpu data (used). */
339         pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
340         /* Free room. */
341         pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
342         if (pcpu_size[1] < 0) {
343                 printk(KERN_ERR "No per-cpu room for modules.\n");
344                 pcpu_num_used = 1;
345         }
346
347         return 0;
348 }       
349 __initcall(percpu_modinit);
350 #else /* ... !CONFIG_SMP */
351 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
352 {
353         return NULL;
354 }
355 static inline void percpu_modfree(void *pcpuptr)
356 {
357         BUG();
358 }
359 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
360                                         Elf_Shdr *sechdrs,
361                                         const char *secstrings)
362 {
363         return 0;
364 }
365 static inline void percpu_modcopy(void *pcpudst, const void *src,
366                                   unsigned long size)
367 {
368         /* pcpusec should be 0, and size of that section should be 0. */
369         BUG_ON(size != 0);
370 }
371 #endif /* CONFIG_SMP */
372
373 #ifdef CONFIG_MODULE_UNLOAD
374 /* Init the unload section of the module. */
375 static void module_unload_init(struct module *mod)
376 {
377         unsigned int i;
378
379         INIT_LIST_HEAD(&mod->modules_which_use_me);
380         for (i = 0; i < NR_CPUS; i++)
381                 local_set(&mod->ref[i].count, 0);
382         /* Hold reference count during initialization. */
383         local_set(&mod->ref[smp_processor_id()].count, 1);
384         /* Backwards compatibility macros put refcount during init. */
385         mod->waiter = current;
386 }
387
388 /* modules using other modules */
389 struct module_use
390 {
391         struct list_head list;
392         struct module *module_which_uses;
393 };
394
395 /* Does a already use b? */
396 static int already_uses(struct module *a, struct module *b)
397 {
398         struct module_use *use;
399
400         list_for_each_entry(use, &b->modules_which_use_me, list) {
401                 if (use->module_which_uses == a) {
402                         DEBUGP("%s uses %s!\n", a->name, b->name);
403                         return 1;
404                 }
405         }
406         DEBUGP("%s does not use %s!\n", a->name, b->name);
407         return 0;
408 }
409
410 /* Module a uses b */
411 static int use_module(struct module *a, struct module *b)
412 {
413         struct module_use *use;
414         if (b == NULL || already_uses(a, b)) return 1;
415
416         if (!strong_try_module_get(b))
417                 return 0;
418
419         DEBUGP("Allocating new usage for %s.\n", a->name);
420         use = kmalloc(sizeof(*use), GFP_ATOMIC);
421         if (!use) {
422                 printk("%s: out of memory loading\n", a->name);
423                 module_put(b);
424                 return 0;
425         }
426
427         use->module_which_uses = a;
428         list_add(&use->list, &b->modules_which_use_me);
429         return 1;
430 }
431
432 /* Clear the unload stuff of the module. */
433 static void module_unload_free(struct module *mod)
434 {
435         struct module *i;
436
437         list_for_each_entry(i, &modules, list) {
438                 struct module_use *use;
439
440                 list_for_each_entry(use, &i->modules_which_use_me, list) {
441                         if (use->module_which_uses == mod) {
442                                 DEBUGP("%s unusing %s\n", mod->name, i->name);
443                                 module_put(i);
444                                 list_del(&use->list);
445                                 kfree(use);
446                                 /* There can be at most one match. */
447                                 break;
448                         }
449                 }
450         }
451 }
452
453 #ifdef CONFIG_MODULE_FORCE_UNLOAD
454 static inline int try_force(unsigned int flags)
455 {
456         int ret = (flags & O_TRUNC);
457         if (ret)
458                 tainted |= TAINT_FORCED_MODULE;
459         return ret;
460 }
461 #else
462 static inline int try_force(unsigned int flags)
463 {
464         return 0;
465 }
466 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
467
468 struct stopref
469 {
470         struct module *mod;
471         int flags;
472         int *forced;
473 };
474
475 /* Whole machine is stopped with interrupts off when this runs. */
476 static inline int __try_stop_module(void *_sref)
477 {
478         struct stopref *sref = _sref;
479
480         /* If it's not unused, quit unless we are told to block. */
481         if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
482                 if (!(*sref->forced = try_force(sref->flags)))
483                         return -EWOULDBLOCK;
484         }
485
486         /* Mark it as dying. */
487         sref->mod->state = MODULE_STATE_GOING;
488         return 0;
489 }
490
491 static int try_stop_module(struct module *mod, int flags, int *forced)
492 {
493         struct stopref sref = { mod, flags, forced };
494
495         return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
496 }
497
498 unsigned int module_refcount(struct module *mod)
499 {
500         unsigned int i, total = 0;
501
502         for (i = 0; i < NR_CPUS; i++)
503                 total += local_read(&mod->ref[i].count);
504         return total;
505 }
506 EXPORT_SYMBOL(module_refcount);
507
508 /* This exists whether we can unload or not */
509 static void free_module(struct module *mod);
510
511 static void wait_for_zero_refcount(struct module *mod)
512 {
513         /* Since we might sleep for some time, drop the semaphore first */
514         up(&module_mutex);
515         for (;;) {
516                 DEBUGP("Looking at refcount...\n");
517                 set_current_state(TASK_UNINTERRUPTIBLE);
518                 if (module_refcount(mod) == 0)
519                         break;
520                 schedule();
521         }
522         current->state = TASK_RUNNING;
523         down(&module_mutex);
524 }
525
526 asmlinkage long
527 sys_delete_module(const char __user *name_user, unsigned int flags)
528 {
529         struct module *mod;
530         char name[MODULE_NAME_LEN];
531         int ret, forced = 0;
532
533         if (!capable(CAP_SYS_MODULE))
534                 return -EPERM;
535
536         if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
537                 return -EFAULT;
538         name[MODULE_NAME_LEN-1] = '\0';
539
540         if (down_interruptible(&module_mutex) != 0)
541                 return -EINTR;
542
543         mod = find_module(name);
544         if (!mod) {
545                 ret = -ENOENT;
546                 goto out;
547         }
548
549         if (!list_empty(&mod->modules_which_use_me)) {
550                 /* Other modules depend on us: get rid of them first. */
551                 ret = -EWOULDBLOCK;
552                 goto out;
553         }
554
555         /* Doing init or already dying? */
556         if (mod->state != MODULE_STATE_LIVE) {
557                 /* FIXME: if (force), slam module count and wake up
558                    waiter --RR */
559                 DEBUGP("%s already dying\n", mod->name);
560                 ret = -EBUSY;
561                 goto out;
562         }
563
564         /* If it has an init func, it must have an exit func to unload */
565         if ((mod->init != NULL && mod->exit == NULL)
566             || mod->unsafe) {
567                 forced = try_force(flags);
568                 if (!forced) {
569                         /* This module can't be removed */
570                         ret = -EBUSY;
571                         goto out;
572                 }
573         }
574
575         /* Set this up before setting mod->state */
576         mod->waiter = current;
577
578         /* Stop the machine so refcounts can't move and disable module. */
579         ret = try_stop_module(mod, flags, &forced);
580         if (ret != 0)
581                 goto out;
582
583         /* Never wait if forced. */
584         if (!forced && module_refcount(mod) != 0)
585                 wait_for_zero_refcount(mod);
586
587         /* Final destruction now noone is using it. */
588         if (mod->exit != NULL) {
589                 up(&module_mutex);
590                 mod->exit();
591                 down(&module_mutex);
592         }
593         free_module(mod);
594
595  out:
596         up(&module_mutex);
597         return ret;
598 }
599
600 static void print_unload_info(struct seq_file *m, struct module *mod)
601 {
602         struct module_use *use;
603         int printed_something = 0;
604
605         seq_printf(m, " %u ", module_refcount(mod));
606
607         /* Always include a trailing , so userspace can differentiate
608            between this and the old multi-field proc format. */
609         list_for_each_entry(use, &mod->modules_which_use_me, list) {
610                 printed_something = 1;
611                 seq_printf(m, "%s,", use->module_which_uses->name);
612         }
613
614         if (mod->unsafe) {
615                 printed_something = 1;
616                 seq_printf(m, "[unsafe],");
617         }
618
619         if (mod->init != NULL && mod->exit == NULL) {
620                 printed_something = 1;
621                 seq_printf(m, "[permanent],");
622         }
623
624         if (!printed_something)
625                 seq_printf(m, "-");
626 }
627
628 void __symbol_put(const char *symbol)
629 {
630         struct module *owner;
631         unsigned long flags;
632         const unsigned long *crc;
633
634         spin_lock_irqsave(&modlist_lock, flags);
635         if (!__find_symbol(symbol, &owner, &crc, 1))
636                 BUG();
637         module_put(owner);
638         spin_unlock_irqrestore(&modlist_lock, flags);
639 }
640 EXPORT_SYMBOL(__symbol_put);
641
642 void symbol_put_addr(void *addr)
643 {
644         unsigned long flags;
645
646         spin_lock_irqsave(&modlist_lock, flags);
647         if (!kernel_text_address((unsigned long)addr))
648                 BUG();
649
650         module_put(module_text_address((unsigned long)addr));
651         spin_unlock_irqrestore(&modlist_lock, flags);
652 }
653 EXPORT_SYMBOL_GPL(symbol_put_addr);
654
655 static ssize_t show_refcnt(struct module *mod, char *buffer)
656 {
657         /* sysfs holds a reference */
658         return sprintf(buffer, "%u\n", module_refcount(mod)-1);
659 }
660
661 static struct module_attribute refcnt = {
662         .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
663         .show = show_refcnt,
664 };
665
666 #else /* !CONFIG_MODULE_UNLOAD */
667 static void print_unload_info(struct seq_file *m, struct module *mod)
668 {
669         /* We don't know the usage count, or what modules are using. */
670         seq_printf(m, " - -");
671 }
672
673 static inline void module_unload_free(struct module *mod)
674 {
675 }
676
677 static inline int use_module(struct module *a, struct module *b)
678 {
679         return strong_try_module_get(b);
680 }
681
682 static inline void module_unload_init(struct module *mod)
683 {
684 }
685
686 asmlinkage long
687 sys_delete_module(const char __user *name_user, unsigned int flags)
688 {
689         return -ENOSYS;
690 }
691
692 #endif /* CONFIG_MODULE_UNLOAD */
693
694 #ifdef CONFIG_OBSOLETE_MODPARM
695 /* Bounds checking done below */
696 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
697 {
698         strcpy(kp->arg, val);
699         return 0;
700 }
701
702 int set_obsolete(const char *val, struct kernel_param *kp)
703 {
704         unsigned int min, max;
705         unsigned int size, maxsize;
706         int dummy;
707         char *endp;
708         const char *p;
709         struct obsolete_modparm *obsparm = kp->arg;
710
711         if (!val) {
712                 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
713                 return -EINVAL;
714         }
715
716         /* type is: [min[-max]]{b,h,i,l,s} */
717         p = obsparm->type;
718         min = simple_strtol(p, &endp, 10);
719         if (endp == obsparm->type)
720                 min = max = 1;
721         else if (*endp == '-') {
722                 p = endp+1;
723                 max = simple_strtol(p, &endp, 10);
724         } else
725                 max = min;
726         switch (*endp) {
727         case 'b':
728                 return param_array(kp->name, val, min, max, obsparm->addr,
729                                    1, param_set_byte, &dummy);
730         case 'h':
731                 return param_array(kp->name, val, min, max, obsparm->addr,
732                                    sizeof(short), param_set_short, &dummy);
733         case 'i':
734                 return param_array(kp->name, val, min, max, obsparm->addr,
735                                    sizeof(int), param_set_int, &dummy);
736         case 'l':
737                 return param_array(kp->name, val, min, max, obsparm->addr,
738                                    sizeof(long), param_set_long, &dummy);
739         case 's':
740                 return param_array(kp->name, val, min, max, obsparm->addr,
741                                    sizeof(char *), param_set_charp, &dummy);
742
743         case 'c':
744                 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
745                    and the decl is "char xxx[5][50];" */
746                 p = endp+1;
747                 maxsize = simple_strtol(p, &endp, 10);
748                 /* We check lengths here (yes, this is a hack). */
749                 p = val;
750                 while (p[size = strcspn(p, ",")]) {
751                         if (size >= maxsize) 
752                                 goto oversize;
753                         p += size+1;
754                 }
755                 if (size >= maxsize) 
756                         goto oversize;
757                 return param_array(kp->name, val, min, max, obsparm->addr,
758                                    maxsize, obsparm_copy_string, &dummy);
759         }
760         printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
761         return -EINVAL;
762  oversize:
763         printk(KERN_ERR
764                "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
765         return -EINVAL;
766 }
767
768 static int obsolete_params(const char *name,
769                            char *args,
770                            struct obsolete_modparm obsparm[],
771                            unsigned int num,
772                            Elf_Shdr *sechdrs,
773                            unsigned int symindex,
774                            const char *strtab)
775 {
776         struct kernel_param *kp;
777         unsigned int i;
778         int ret;
779
780         kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
781         if (!kp)
782                 return -ENOMEM;
783
784         for (i = 0; i < num; i++) {
785                 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
786
787                 snprintf(sym_name, sizeof(sym_name), "%s%s",
788                          MODULE_SYMBOL_PREFIX, obsparm[i].name);
789
790                 kp[i].name = obsparm[i].name;
791                 kp[i].perm = 000;
792                 kp[i].set = set_obsolete;
793                 kp[i].get = NULL;
794                 obsparm[i].addr
795                         = (void *)find_local_symbol(sechdrs, symindex, strtab,
796                                                     sym_name);
797                 if (!obsparm[i].addr) {
798                         printk("%s: falsely claims to have parameter %s\n",
799                                name, obsparm[i].name);
800                         ret = -EINVAL;
801                         goto out;
802                 }
803                 kp[i].arg = &obsparm[i];
804         }
805
806         ret = parse_args(name, args, kp, num, NULL);
807  out:
808         kfree(kp);
809         return ret;
810 }
811 #else
812 static int obsolete_params(const char *name,
813                            char *args,
814                            struct obsolete_modparm obsparm[],
815                            unsigned int num,
816                            Elf_Shdr *sechdrs,
817                            unsigned int symindex,
818                            const char *strtab)
819 {
820         if (num != 0)
821                 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
822                        name);
823         return 0;
824 }
825 #endif /* CONFIG_OBSOLETE_MODPARM */
826
827 static const char vermagic[] = VERMAGIC_STRING;
828
829 #ifdef CONFIG_MODVERSIONS
830 static int check_version(Elf_Shdr *sechdrs,
831                          unsigned int versindex,
832                          const char *symname,
833                          struct module *mod, 
834                          const unsigned long *crc)
835 {
836         unsigned int i, num_versions;
837         struct modversion_info *versions;
838
839         /* Exporting module didn't supply crcs?  OK, we're already tainted. */
840         if (!crc)
841                 return 1;
842
843         versions = (void *) sechdrs[versindex].sh_addr;
844         num_versions = sechdrs[versindex].sh_size
845                 / sizeof(struct modversion_info);
846
847         for (i = 0; i < num_versions; i++) {
848                 if (strcmp(versions[i].name, symname) != 0)
849                         continue;
850
851                 if (versions[i].crc == *crc)
852                         return 1;
853                 printk("%s: disagrees about version of symbol %s\n",
854                        mod->name, symname);
855                 DEBUGP("Found checksum %lX vs module %lX\n",
856                        *crc, versions[i].crc);
857                 return 0;
858         }
859         /* Not in module's version table.  OK, but that taints the kernel. */
860         if (!(tainted & TAINT_FORCED_MODULE)) {
861                 printk("%s: no version for \"%s\" found: kernel tainted.\n",
862                        mod->name, symname);
863                 tainted |= TAINT_FORCED_MODULE;
864         }
865         return 1;
866 }
867
868 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
869                                           unsigned int versindex,
870                                           struct module *mod)
871 {
872         const unsigned long *crc;
873         struct module *owner;
874
875         if (!__find_symbol("struct_module", &owner, &crc, 1))
876                 BUG();
877         return check_version(sechdrs, versindex, "struct_module", mod,
878                              crc);
879 }
880
881 /* First part is kernel version, which we ignore. */
882 static inline int same_magic(const char *amagic, const char *bmagic)
883 {
884         amagic += strcspn(amagic, " ");
885         bmagic += strcspn(bmagic, " ");
886         return strcmp(amagic, bmagic) == 0;
887 }
888 #else
889 static inline int check_version(Elf_Shdr *sechdrs,
890                                 unsigned int versindex,
891                                 const char *symname,
892                                 struct module *mod, 
893                                 const unsigned long *crc)
894 {
895         return 1;
896 }
897
898 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
899                                           unsigned int versindex,
900                                           struct module *mod)
901 {
902         return 1;
903 }
904
905 static inline int same_magic(const char *amagic, const char *bmagic)
906 {
907         return strcmp(amagic, bmagic) == 0;
908 }
909 #endif /* CONFIG_MODVERSIONS */
910
911 /* Resolve a symbol for this module.  I.e. if we find one, record usage.
912    Must be holding module_mutex. */
913 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
914                                     unsigned int versindex,
915                                     const char *name,
916                                     struct module *mod)
917 {
918         struct module *owner;
919         unsigned long ret;
920         const unsigned long *crc;
921
922         spin_lock_irq(&modlist_lock);
923         ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
924         if (ret) {
925                 /* use_module can fail due to OOM, or module unloading */
926                 if (!check_version(sechdrs, versindex, name, mod, crc) ||
927                     !use_module(mod, owner))
928                         ret = 0;
929         }
930         spin_unlock_irq(&modlist_lock);
931         return ret;
932 }
933
934
935 /*
936  * /sys/module/foo/sections stuff
937  * J. Corbet <corbet@lwn.net>
938  */
939 #ifdef CONFIG_KALLSYMS
940 static void module_sect_attrs_release(struct kobject *kobj)
941 {
942         kfree(container_of(kobj, struct module_sections, kobj));
943 }
944
945 static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
946                 char *buf)
947 {
948         struct module_sect_attr *sattr =
949                 container_of(attr, struct module_sect_attr, attr);
950         return sprintf(buf, "0x%lx\n", sattr->address);
951 }
952
953 static struct sysfs_ops module_sect_ops = {
954         .show = module_sect_show,
955 };
956
957 static struct kobj_type module_sect_ktype = {
958         .sysfs_ops = &module_sect_ops,
959         .release =   module_sect_attrs_release,
960 };
961
962 static void add_sect_attrs(struct module *mod, unsigned int nsect,
963                 char *secstrings, Elf_Shdr *sechdrs)
964 {
965         unsigned int nloaded = 0, i;
966         struct module_sect_attr *sattr;
967         
968         if (!mod->mkobj)
969                 return;
970         
971         /* Count loaded sections and allocate structures */
972         for (i = 0; i < nsect; i++)
973                 if (sechdrs[i].sh_flags & SHF_ALLOC)
974                         nloaded++;
975         mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
976                         nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
977         if (! mod->sect_attrs)
978                 return;
979
980         /* sections entry setup */
981         memset(mod->sect_attrs, 0, sizeof(struct module_sections));
982         if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
983                 goto out;
984         mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
985         mod->sect_attrs->kobj.ktype = &module_sect_ktype;
986         if (kobject_register(&mod->sect_attrs->kobj))
987                 goto out;
988
989         /* And the section attributes. */
990         sattr = &mod->sect_attrs->attrs[0];
991         for (i = 0; i < nsect; i++) {
992                 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
993                         continue;
994                 sattr->address = sechdrs[i].sh_addr;
995                 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
996                                 MODULE_SECT_NAME_LEN);
997                 sattr->attr.name = sattr->name;
998                 sattr->attr.owner = mod;
999                 sattr->attr.mode = S_IRUGO;
1000                 (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
1001                 sattr++;
1002         }
1003         return;
1004   out:
1005         kfree(mod->sect_attrs);
1006         mod->sect_attrs = NULL;
1007 }
1008
1009 static void remove_sect_attrs(struct module *mod)
1010 {
1011         if (mod->sect_attrs) {
1012                 kobject_unregister(&mod->sect_attrs->kobj);
1013                 mod->sect_attrs = NULL;
1014         }
1015 }
1016
1017
1018 #else
1019 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1020                 char *sectstrings, Elf_Shdr *sechdrs)
1021 {
1022 }
1023
1024 static inline void remove_sect_attrs(struct module *mod)
1025 {
1026 }
1027 #endif /* CONFIG_KALLSYMS */
1028
1029
1030 #ifdef CONFIG_MODULE_UNLOAD
1031 static inline int module_add_refcnt_attr(struct module *mod)
1032 {
1033         return sysfs_create_file(&mod->mkobj->kobj, &refcnt.attr);
1034 }
1035 static void module_remove_refcnt_attr(struct module *mod)
1036 {
1037         return sysfs_remove_file(&mod->mkobj->kobj, &refcnt.attr);
1038 }
1039 #else
1040 static inline int module_add_refcnt_attr(struct module *mod)
1041 {
1042         return 0;
1043 }
1044 static void module_remove_refcnt_attr(struct module *mod)
1045 {
1046 }
1047 #endif
1048
1049
1050 static int mod_sysfs_setup(struct module *mod,
1051                            struct kernel_param *kparam,
1052                            unsigned int num_params)
1053 {
1054         int err;
1055
1056         mod->mkobj = kmalloc(sizeof(struct module_kobject), GFP_KERNEL);
1057         if (!mod->mkobj)
1058                 return -ENOMEM;
1059
1060         memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1061         err = kobject_set_name(&mod->mkobj->kobj, "%s", mod->name);
1062         if (err)
1063                 goto out;
1064         kobj_set_kset_s(mod->mkobj, module_subsys);
1065         mod->mkobj->mod = mod;
1066         err = kobject_register(&mod->mkobj->kobj);
1067         if (err)
1068                 goto out;
1069
1070         err = module_add_refcnt_attr(mod);
1071         if (err)
1072                 goto out_unreg;
1073
1074         err = module_param_sysfs_setup(mod, kparam, num_params);
1075         if (err)
1076                 goto out_unreg;
1077
1078         return 0;
1079
1080 out_unreg:
1081         /* Calls module_kobj_release */
1082         kobject_unregister(&mod->mkobj->kobj);
1083         return err;
1084 out:
1085         kfree(mod->mkobj);
1086         return err;
1087 }
1088
1089 static void mod_kobject_remove(struct module *mod)
1090 {
1091         module_remove_refcnt_attr(mod);
1092         module_param_sysfs_remove(mod);
1093
1094         /* Calls module_kobj_release */
1095         kobject_unregister(&mod->mkobj->kobj);
1096 }
1097
1098 /* Free a module, remove from lists, etc (must hold module mutex). */
1099 static void free_module(struct module *mod)
1100 {
1101         /* Delete from various lists */
1102         spin_lock_irq(&modlist_lock);
1103         list_del(&mod->list);
1104         spin_unlock_irq(&modlist_lock);
1105
1106         remove_sect_attrs(mod);
1107         mod_kobject_remove(mod);
1108
1109         /* Arch-specific cleanup. */
1110         module_arch_cleanup(mod);
1111
1112         /* Module unload stuff */
1113         module_unload_free(mod);
1114
1115         /* This may be NULL, but that's OK */
1116         module_free(mod, mod->module_init);
1117         kfree(mod->args);
1118         if (mod->percpu)
1119                 percpu_modfree(mod->percpu);
1120
1121         /* Finally, free the core (containing the module structure) */
1122         module_free(mod, mod->module_core);
1123 }
1124
1125 void *__symbol_get(const char *symbol)
1126 {
1127         struct module *owner;
1128         unsigned long value, flags;
1129         const unsigned long *crc;
1130
1131         spin_lock_irqsave(&modlist_lock, flags);
1132         value = __find_symbol(symbol, &owner, &crc, 1);
1133         if (value && !strong_try_module_get(owner))
1134                 value = 0;
1135         spin_unlock_irqrestore(&modlist_lock, flags);
1136
1137         return (void *)value;
1138 }
1139 EXPORT_SYMBOL_GPL(__symbol_get);
1140
1141 /* Change all symbols so that sh_value encodes the pointer directly. */
1142 static int simplify_symbols(Elf_Shdr *sechdrs,
1143                             unsigned int symindex,
1144                             const char *strtab,
1145                             unsigned int versindex,
1146                             unsigned int pcpuindex,
1147                             struct module *mod)
1148 {
1149         Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1150         unsigned long secbase;
1151         unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1152         int ret = 0;
1153
1154         for (i = 1; i < n; i++) {
1155                 switch (sym[i].st_shndx) {
1156                 case SHN_COMMON:
1157                         /* We compiled with -fno-common.  These are not
1158                            supposed to happen.  */
1159                         DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1160                         printk("%s: please compile with -fno-common\n",
1161                                mod->name);
1162                         ret = -ENOEXEC;
1163                         break;
1164
1165                 case SHN_ABS:
1166                         /* Don't need to do anything */
1167                         DEBUGP("Absolute symbol: 0x%08lx\n",
1168                                (long)sym[i].st_value);
1169                         break;
1170
1171                 case SHN_UNDEF:
1172                         sym[i].st_value
1173                           = resolve_symbol(sechdrs, versindex,
1174                                            strtab + sym[i].st_name, mod);
1175
1176                         /* Ok if resolved.  */
1177                         if (sym[i].st_value != 0)
1178                                 break;
1179                         /* Ok if weak.  */
1180                         if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1181                                 break;
1182
1183                         printk(KERN_WARNING "%s: Unknown symbol %s\n",
1184                                mod->name, strtab + sym[i].st_name);
1185                         ret = -ENOENT;
1186                         break;
1187
1188                 default:
1189                         /* Divert to percpu allocation if a percpu var. */
1190                         if (sym[i].st_shndx == pcpuindex)
1191                                 secbase = (unsigned long)mod->percpu;
1192                         else
1193                                 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1194                         sym[i].st_value += secbase;
1195                         break;
1196                 }
1197         }
1198
1199         return ret;
1200 }
1201
1202 /* Update size with this section: return offset. */
1203 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1204 {
1205         long ret;
1206
1207         ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1208         *size = ret + sechdr->sh_size;
1209         return ret;
1210 }
1211
1212 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1213    might -- code, read-only data, read-write data, small data.  Tally
1214    sizes, and place the offsets into sh_entsize fields: high bit means it
1215    belongs in init. */
1216 static void layout_sections(struct module *mod,
1217                             const Elf_Ehdr *hdr,
1218                             Elf_Shdr *sechdrs,
1219                             const char *secstrings)
1220 {
1221         static unsigned long const masks[][2] = {
1222                 /* NOTE: all executable code must be the first section
1223                  * in this array; otherwise modify the text_size
1224                  * finder in the two loops below */
1225                 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1226                 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1227                 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1228                 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1229         };
1230         unsigned int m, i;
1231
1232         for (i = 0; i < hdr->e_shnum; i++)
1233                 sechdrs[i].sh_entsize = ~0UL;
1234
1235         DEBUGP("Core section allocation order:\n");
1236         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1237                 for (i = 0; i < hdr->e_shnum; ++i) {
1238                         Elf_Shdr *s = &sechdrs[i];
1239
1240                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1241                             || (s->sh_flags & masks[m][1])
1242                             || s->sh_entsize != ~0UL
1243                             || strncmp(secstrings + s->sh_name,
1244                                        ".init", 5) == 0)
1245                                 continue;
1246                         s->sh_entsize = get_offset(&mod->core_size, s);
1247                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1248                 }
1249                 if (m == 0)
1250                         mod->core_text_size = mod->core_size;
1251         }
1252
1253         DEBUGP("Init section allocation order:\n");
1254         for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1255                 for (i = 0; i < hdr->e_shnum; ++i) {
1256                         Elf_Shdr *s = &sechdrs[i];
1257
1258                         if ((s->sh_flags & masks[m][0]) != masks[m][0]
1259                             || (s->sh_flags & masks[m][1])
1260                             || s->sh_entsize != ~0UL
1261                             || strncmp(secstrings + s->sh_name,
1262                                        ".init", 5) != 0)
1263                                 continue;
1264                         s->sh_entsize = (get_offset(&mod->init_size, s)
1265                                          | INIT_OFFSET_MASK);
1266                         DEBUGP("\t%s\n", secstrings + s->sh_name);
1267                 }
1268                 if (m == 0)
1269                         mod->init_text_size = mod->init_size;
1270         }
1271 }
1272
1273 static inline int license_is_gpl_compatible(const char *license)
1274 {
1275         return (strcmp(license, "GPL") == 0
1276                 || strcmp(license, "GPL v2") == 0
1277                 || strcmp(license, "GPL and additional rights") == 0
1278                 || strcmp(license, "Dual BSD/GPL") == 0
1279                 || strcmp(license, "Dual MPL/GPL") == 0);
1280 }
1281
1282 static void set_license(struct module *mod, const char *license)
1283 {
1284         if (!license)
1285                 license = "unspecified";
1286
1287         mod->license_gplok = license_is_gpl_compatible(license);
1288         if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1289                 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1290                        mod->name, license);
1291                 tainted |= TAINT_PROPRIETARY_MODULE;
1292         }
1293 }
1294
1295 /* Parse tag=value strings from .modinfo section */
1296 static char *next_string(char *string, unsigned long *secsize)
1297 {
1298         /* Skip non-zero chars */
1299         while (string[0]) {
1300                 string++;
1301                 if ((*secsize)-- <= 1)
1302                         return NULL;
1303         }
1304
1305         /* Skip any zero padding. */
1306         while (!string[0]) {
1307                 string++;
1308                 if ((*secsize)-- <= 1)
1309                         return NULL;
1310         }
1311         return string;
1312 }
1313
1314 static char *get_modinfo(Elf_Shdr *sechdrs,
1315                          unsigned int info,
1316                          const char *tag)
1317 {
1318         char *p;
1319         unsigned int taglen = strlen(tag);
1320         unsigned long size = sechdrs[info].sh_size;
1321
1322         for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1323                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1324                         return p + taglen + 1;
1325         }
1326         return NULL;
1327 }
1328
1329 #ifdef CONFIG_KALLSYMS
1330 int is_exported(const char *name, const struct module *mod)
1331 {
1332         unsigned int i;
1333
1334         if (!mod) {
1335                 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1336                         if (strcmp(__start___ksymtab[i].name, name) == 0)
1337                                 return 1;
1338                 return 0;
1339         }
1340         for (i = 0; i < mod->num_syms; i++)
1341                 if (strcmp(mod->syms[i].name, name) == 0)
1342                         return 1;
1343         return 0;
1344 }
1345
1346 /* As per nm */
1347 static char elf_type(const Elf_Sym *sym,
1348                      Elf_Shdr *sechdrs,
1349                      const char *secstrings,
1350                      struct module *mod)
1351 {
1352         if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1353                 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1354                         return 'v';
1355                 else
1356                         return 'w';
1357         }
1358         if (sym->st_shndx == SHN_UNDEF)
1359                 return 'U';
1360         if (sym->st_shndx == SHN_ABS)
1361                 return 'a';
1362         if (sym->st_shndx >= SHN_LORESERVE)
1363                 return '?';
1364         if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1365                 return 't';
1366         if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1367             && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1368                 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1369                         return 'r';
1370                 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1371                         return 'g';
1372                 else
1373                         return 'd';
1374         }
1375         if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1376                 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1377                         return 's';
1378                 else
1379                         return 'b';
1380         }
1381         if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1382                     ".debug", strlen(".debug")) == 0)
1383                 return 'n';
1384         return '?';
1385 }
1386
1387 static void add_kallsyms(struct module *mod,
1388                          Elf_Shdr *sechdrs,
1389                          unsigned int symindex,
1390                          unsigned int strindex,
1391                          const char *secstrings)
1392 {
1393         unsigned int i;
1394
1395         mod->symtab = (void *)sechdrs[symindex].sh_addr;
1396         mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1397         mod->strtab = (void *)sechdrs[strindex].sh_addr;
1398
1399         /* Set types up while we still have access to sections. */
1400         for (i = 0; i < mod->num_symtab; i++)
1401                 mod->symtab[i].st_info
1402                         = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1403 }
1404 #else
1405 static inline void add_kallsyms(struct module *mod,
1406                                 Elf_Shdr *sechdrs,
1407                                 unsigned int symindex,
1408                                 unsigned int strindex,
1409                                 const char *secstrings)
1410 {
1411 }
1412 #endif /* CONFIG_KALLSYMS */
1413
1414 /* Allocate and load the module: note that size of section 0 is always
1415    zero, and we rely on this for optional sections. */
1416 static struct module *load_module(void __user *umod,
1417                                   unsigned long len,
1418                                   const char __user *uargs)
1419 {
1420         Elf_Ehdr *hdr;
1421         Elf_Shdr *sechdrs;
1422         char *secstrings, *args, *modmagic, *strtab = NULL;
1423         unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1424                 exportindex, modindex, obsparmindex, infoindex, gplindex,
1425                 crcindex, gplcrcindex, versindex, pcpuindex;
1426         long arglen;
1427         struct module *mod;
1428         long err = 0;
1429         void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1430         struct exception_table_entry *extable;
1431         int gpgsig_ok;
1432
1433         DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1434                umod, len, uargs);
1435         if (len < sizeof(*hdr))
1436                 return ERR_PTR(-ENOEXEC);
1437
1438         /* Suck in entire file: we'll want most of it. */
1439         /* vmalloc barfs on "unusual" numbers.  Check here */
1440         if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1441                 return ERR_PTR(-ENOMEM);
1442         if (copy_from_user(hdr, umod, len) != 0) {
1443                 err = -EFAULT;
1444                 goto free_hdr;
1445         }
1446
1447         /* Sanity checks against insmoding binaries or wrong arch,
1448            weird elf version */
1449         if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1450             || hdr->e_type != ET_REL
1451             || !elf_check_arch(hdr)
1452             || hdr->e_shentsize != sizeof(*sechdrs)) {
1453                 err = -ENOEXEC;
1454                 goto free_hdr;
1455         }
1456
1457         /* verify the module (validates ELF and checks signature) */
1458         gpgsig_ok = 0;
1459         err = module_verify(hdr, len);
1460         if (err < 0)
1461                 goto free_hdr;
1462         if (err == 1)
1463                 gpgsig_ok = 1;
1464
1465         /* Convenience variables */
1466         sechdrs = (void *)hdr + hdr->e_shoff;
1467         secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1468         sechdrs[0].sh_addr = 0;
1469
1470         for (i = 1; i < hdr->e_shnum; i++) {
1471                 if (sechdrs[i].sh_type != SHT_NOBITS
1472                     && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1473                         goto truncated;
1474
1475                 /* Mark all sections sh_addr with their address in the
1476                    temporary image. */
1477                 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1478
1479                 /* Internal symbols and strings. */
1480                 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1481                         symindex = i;
1482                         strindex = sechdrs[i].sh_link;
1483                         strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1484                 }
1485 #ifndef CONFIG_MODULE_UNLOAD
1486                 /* Don't load .exit sections */
1487                 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1488                         sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1489 #endif
1490         }
1491
1492         modindex = find_sec(hdr, sechdrs, secstrings,
1493                             ".gnu.linkonce.this_module");
1494         if (!modindex) {
1495                 printk(KERN_WARNING "No module found in object\n");
1496                 err = -ENOEXEC;
1497                 goto free_hdr;
1498         }
1499         mod = (void *)sechdrs[modindex].sh_addr;
1500         mod->gpgsig_ok = gpgsig_ok;
1501
1502         if (symindex == 0) {
1503                 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1504                        mod->name);
1505                 err = -ENOEXEC;
1506                 goto free_hdr;
1507         }
1508
1509         /* Optional sections */
1510         exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1511         gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1512         crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1513         gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1514         setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1515         exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1516         obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1517         versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1518         infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1519         pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1520
1521         /* Don't keep modinfo section */
1522         sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1523 #ifdef CONFIG_KALLSYMS
1524         /* Keep symbol and string tables for decoding later. */
1525         sechdrs[symindex].sh_flags |= SHF_ALLOC;
1526         sechdrs[strindex].sh_flags |= SHF_ALLOC;
1527 #endif
1528
1529         /* Check module struct version now, before we try to use module. */
1530         if (!check_modstruct_version(sechdrs, versindex, mod)) {
1531                 err = -ENOEXEC;
1532                 goto free_hdr;
1533         }
1534
1535         modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1536         /* This is allowed: modprobe --force will invalidate it. */
1537         if (!modmagic) {
1538                 tainted |= TAINT_FORCED_MODULE;
1539                 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1540                        mod->name);
1541         } else if (!same_magic(modmagic, vermagic)) {
1542                 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1543                        mod->name, modmagic, vermagic);
1544                 err = -ENOEXEC;
1545                 goto free_hdr;
1546         }
1547
1548         /* Now copy in args */
1549         arglen = strlen_user(uargs);
1550         if (!arglen) {
1551                 err = -EFAULT;
1552                 goto free_hdr;
1553         }
1554         args = kmalloc(arglen, GFP_KERNEL);
1555         if (!args) {
1556                 err = -ENOMEM;
1557                 goto free_hdr;
1558         }
1559         if (copy_from_user(args, uargs, arglen) != 0) {
1560                 err = -EFAULT;
1561                 goto free_mod;
1562         }
1563
1564         if (find_module(mod->name)) {
1565                 err = -EEXIST;
1566                 goto free_mod;
1567         }
1568
1569         mod->state = MODULE_STATE_COMING;
1570
1571         /* Allow arches to frob section contents and sizes.  */
1572         err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1573         if (err < 0)
1574                 goto free_mod;
1575
1576         if (pcpuindex) {
1577                 /* We have a special allocation for this section. */
1578                 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1579                                          sechdrs[pcpuindex].sh_addralign);
1580                 if (!percpu) {
1581                         err = -ENOMEM;
1582                         goto free_mod;
1583                 }
1584                 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1585                 mod->percpu = percpu;
1586         }
1587
1588         /* Determine total sizes, and put offsets in sh_entsize.  For now
1589            this is done generically; there doesn't appear to be any
1590            special cases for the architectures. */
1591         layout_sections(mod, hdr, sechdrs, secstrings);
1592
1593         /* Do the allocs. */
1594         ptr = module_alloc(mod->core_size);
1595         if (!ptr) {
1596                 err = -ENOMEM;
1597                 goto free_percpu;
1598         }
1599         memset(ptr, 0, mod->core_size);
1600         mod->module_core = ptr;
1601
1602         ptr = module_alloc(mod->init_size);
1603         if (!ptr && mod->init_size) {
1604                 err = -ENOMEM;
1605                 goto free_core;
1606         }
1607         memset(ptr, 0, mod->init_size);
1608         mod->module_init = ptr;
1609
1610         /* Transfer each section which specifies SHF_ALLOC */
1611         DEBUGP("final section addresses:\n");
1612         for (i = 0; i < hdr->e_shnum; i++) {
1613                 void *dest;
1614
1615                 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1616                         continue;
1617
1618                 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1619                         dest = mod->module_init
1620                                 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1621                 else
1622                         dest = mod->module_core + sechdrs[i].sh_entsize;
1623
1624                 if (sechdrs[i].sh_type != SHT_NOBITS)
1625                         memcpy(dest, (void *)sechdrs[i].sh_addr,
1626                                sechdrs[i].sh_size);
1627                 /* Update sh_addr to point to copy in image. */
1628                 sechdrs[i].sh_addr = (unsigned long)dest;
1629                 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1630         }
1631         /* Module has been moved. */
1632         mod = (void *)sechdrs[modindex].sh_addr;
1633
1634         /* Now we've moved module, initialize linked lists, etc. */
1635         module_unload_init(mod);
1636
1637         /* Set up license info based on the info section */
1638         set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1639
1640         /* Fix up syms, so that st_value is a pointer to location. */
1641         err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1642                                mod);
1643         if (err < 0)
1644                 goto cleanup;
1645
1646         /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1647         mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1648         mod->syms = (void *)sechdrs[exportindex].sh_addr;
1649         if (crcindex)
1650                 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1651         mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1652         mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1653         if (gplcrcindex)
1654                 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1655
1656 #ifdef CONFIG_MODVERSIONS
1657         if ((mod->num_syms && !crcindex) || 
1658             (mod->num_gpl_syms && !gplcrcindex)) {
1659                 printk(KERN_WARNING "%s: No versions for exported symbols."
1660                        " Tainting kernel.\n", mod->name);
1661                 tainted |= TAINT_FORCED_MODULE;
1662         }
1663 #endif
1664
1665         /* Now do relocations. */
1666         for (i = 1; i < hdr->e_shnum; i++) {
1667                 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1668                 unsigned int info = sechdrs[i].sh_info;
1669
1670                 /* Not a valid relocation section? */
1671                 if (info >= hdr->e_shnum)
1672                         continue;
1673
1674                 /* Don't bother with non-allocated sections */
1675                 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1676                         continue;
1677
1678                 if (sechdrs[i].sh_type == SHT_REL)
1679                         err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1680                 else if (sechdrs[i].sh_type == SHT_RELA)
1681                         err = apply_relocate_add(sechdrs, strtab, symindex, i,
1682                                                  mod);
1683                 if (err < 0)
1684                         goto cleanup;
1685         }
1686
1687         /* Set up and sort exception table */
1688         mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1689         mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1690         sort_extable(extable, extable + mod->num_exentries);
1691
1692         /* Finally, copy percpu area over. */
1693         percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1694                        sechdrs[pcpuindex].sh_size);
1695
1696         add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1697
1698         err = module_finalize(hdr, sechdrs, mod);
1699         if (err < 0)
1700                 goto cleanup;
1701
1702         mod->args = args;
1703         if (obsparmindex) {
1704                 err = obsolete_params(mod->name, mod->args,
1705                                       (struct obsolete_modparm *)
1706                                       sechdrs[obsparmindex].sh_addr,
1707                                       sechdrs[obsparmindex].sh_size
1708                                       / sizeof(struct obsolete_modparm),
1709                                       sechdrs, symindex,
1710                                       (char *)sechdrs[strindex].sh_addr);
1711                 if (setupindex)
1712                         printk(KERN_WARNING "%s: Ignoring new-style "
1713                                "parameters in presence of obsolete ones\n",
1714                                mod->name);
1715         } else {
1716                 /* Size of section 0 is 0, so this works well if no params */
1717                 err = parse_args(mod->name, mod->args,
1718                                  (struct kernel_param *)
1719                                  sechdrs[setupindex].sh_addr,
1720                                  sechdrs[setupindex].sh_size
1721                                  / sizeof(struct kernel_param),
1722                                  NULL);
1723         }
1724
1725         err = mod_sysfs_setup(mod, 
1726                               (struct kernel_param *)
1727                               sechdrs[setupindex].sh_addr,
1728                               sechdrs[setupindex].sh_size
1729                               / sizeof(struct kernel_param));
1730         if (err < 0)
1731                 goto arch_cleanup;
1732         add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1733
1734         /* Get rid of temporary copy */
1735         vfree(hdr);
1736
1737         /* Done! */
1738         return mod;
1739
1740  arch_cleanup:
1741         module_arch_cleanup(mod);
1742  cleanup:
1743         module_unload_free(mod);
1744         module_free(mod, mod->module_init);
1745  free_core:
1746         module_free(mod, mod->module_core);
1747  free_percpu:
1748         if (percpu)
1749                 percpu_modfree(percpu);
1750  free_mod:
1751         kfree(args);
1752  free_hdr:
1753         vfree(hdr);
1754         if (err < 0) return ERR_PTR(err);
1755         else return ptr;
1756
1757  truncated:
1758         printk(KERN_ERR "Module len %lu truncated\n", len);
1759         err = -ENOEXEC;
1760         goto free_hdr;
1761 }
1762
1763 /* This is where the real work happens */
1764 asmlinkage long
1765 sys_init_module(void __user *umod,
1766                 unsigned long len,
1767                 const char __user *uargs)
1768 {
1769         struct module *mod;
1770         int ret = 0;
1771
1772         /* Must have permission */
1773         if (!capable(CAP_SYS_MODULE))
1774                 return -EPERM;
1775
1776         /* Only one module load at a time, please */
1777         if (down_interruptible(&module_mutex) != 0)
1778                 return -EINTR;
1779
1780         /* Do all the hard work */
1781         mod = load_module(umod, len, uargs);
1782         if (IS_ERR(mod)) {
1783                 up(&module_mutex);
1784                 return PTR_ERR(mod);
1785         }
1786
1787         /* Flush the instruction cache, since we've played with text */
1788         if (mod->module_init)
1789                 flush_icache_range((unsigned long)mod->module_init,
1790                                    (unsigned long)mod->module_init
1791                                    + mod->init_size);
1792         flush_icache_range((unsigned long)mod->module_core,
1793                            (unsigned long)mod->module_core + mod->core_size);
1794
1795         /* Now sew it into the lists.  They won't access us, since
1796            strong_try_module_get() will fail. */
1797         spin_lock_irq(&modlist_lock);
1798         list_add(&mod->list, &modules);
1799         spin_unlock_irq(&modlist_lock);
1800
1801         /* Drop lock so they can recurse */
1802         up(&module_mutex);
1803
1804         down(&notify_mutex);
1805         notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1806         up(&notify_mutex);
1807
1808         /* Start the module */
1809         if (mod->init != NULL)
1810                 ret = mod->init();
1811         if (ret < 0) {
1812                 /* Init routine failed: abort.  Try to protect us from
1813                    buggy refcounters. */
1814                 mod->state = MODULE_STATE_GOING;
1815                 synchronize_kernel();
1816                 if (mod->unsafe)
1817                         printk(KERN_ERR "%s: module is now stuck!\n",
1818                                mod->name);
1819                 else {
1820                         module_put(mod);
1821                         down(&module_mutex);
1822                         free_module(mod);
1823                         up(&module_mutex);
1824                 }
1825                 return ret;
1826         }
1827
1828         /* Now it's a first class citizen! */
1829         down(&module_mutex);
1830         mod->state = MODULE_STATE_LIVE;
1831         /* Drop initial reference. */
1832         module_put(mod);
1833         module_free(mod, mod->module_init);
1834         mod->module_init = NULL;
1835         mod->init_size = 0;
1836         mod->init_text_size = 0;
1837         up(&module_mutex);
1838
1839         return 0;
1840 }
1841
1842 static inline int within(unsigned long addr, void *start, unsigned long size)
1843 {
1844         return ((void *)addr >= start && (void *)addr < start + size);
1845 }
1846
1847 #ifdef CONFIG_KALLSYMS
1848 /*
1849  * This ignores the intensely annoying "mapping symbols" found
1850  * in ARM ELF files: $a, $t and $d.
1851  */
1852 static inline int is_arm_mapping_symbol(const char *str)
1853 {
1854         return str[0] == '$' && strchr("atd", str[1]) 
1855                && (str[2] == '\0' || str[2] == '.');
1856 }
1857
1858 static const char *get_ksymbol(struct module *mod,
1859                                unsigned long addr,
1860                                unsigned long *size,
1861                                unsigned long *offset)
1862 {
1863         unsigned int i, best = 0;
1864         unsigned long nextval;
1865
1866         /* At worse, next value is at end of module */
1867         if (within(addr, mod->module_init, mod->init_size))
1868                 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1869         else 
1870                 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1871
1872         /* Scan for closest preceeding symbol, and next symbol. (ELF
1873            starts real symbols at 1). */
1874         for (i = 1; i < mod->num_symtab; i++) {
1875                 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1876                         continue;
1877
1878                 /* We ignore unnamed symbols: they're uninformative
1879                  * and inserted at a whim. */
1880                 if (mod->symtab[i].st_value <= addr
1881                     && mod->symtab[i].st_value > mod->symtab[best].st_value
1882                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1883                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1884                         best = i;
1885                 if (mod->symtab[i].st_value > addr
1886                     && mod->symtab[i].st_value < nextval
1887                     && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1888                     && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1889                         nextval = mod->symtab[i].st_value;
1890         }
1891
1892         if (!best)
1893                 return NULL;
1894
1895         *size = nextval - mod->symtab[best].st_value;
1896         *offset = addr - mod->symtab[best].st_value;
1897         return mod->strtab + mod->symtab[best].st_name;
1898 }
1899
1900 /* For kallsyms to ask for address resolution.  NULL means not found.
1901    We don't lock, as this is used for oops resolution and races are a
1902    lesser concern. */
1903 const char *module_address_lookup(unsigned long addr,
1904                                   unsigned long *size,
1905                                   unsigned long *offset,
1906                                   char **modname)
1907 {
1908         struct module *mod;
1909
1910         list_for_each_entry(mod, &modules, list) {
1911                 if (within(addr, mod->module_init, mod->init_size)
1912                     || within(addr, mod->module_core, mod->core_size)) {
1913                         *modname = mod->name;
1914                         return get_ksymbol(mod, addr, size, offset);
1915                 }
1916         }
1917         return NULL;
1918 }
1919
1920 struct module *module_get_kallsym(unsigned int symnum,
1921                                   unsigned long *value,
1922                                   char *type,
1923                                   char namebuf[128])
1924 {
1925         struct module *mod;
1926
1927         down(&module_mutex);
1928         list_for_each_entry(mod, &modules, list) {
1929                 if (symnum < mod->num_symtab) {
1930                         *value = mod->symtab[symnum].st_value;
1931                         *type = mod->symtab[symnum].st_info;
1932                         strncpy(namebuf,
1933                                 mod->strtab + mod->symtab[symnum].st_name,
1934                                 127);
1935                         up(&module_mutex);
1936                         return mod;
1937                 }
1938                 symnum -= mod->num_symtab;
1939         }
1940         up(&module_mutex);
1941         return NULL;
1942 }
1943
1944 static unsigned long mod_find_symname(struct module *mod, const char *name)
1945 {
1946         unsigned int i;
1947
1948         for (i = 0; i < mod->num_symtab; i++)
1949                 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
1950                         return mod->symtab[i].st_value;
1951         return 0;
1952 }
1953
1954 /* Look for this name: can be of form module:name. */
1955 unsigned long module_kallsyms_lookup_name(const char *name)
1956 {
1957         struct module *mod;
1958         char *colon;
1959         unsigned long ret = 0;
1960
1961         /* Don't lock: we're in enough trouble already. */
1962         if ((colon = strchr(name, ':')) != NULL) {
1963                 *colon = '\0';
1964                 if ((mod = find_module(name)) != NULL)
1965                         ret = mod_find_symname(mod, colon+1);
1966                 *colon = ':';
1967         } else {
1968                 list_for_each_entry(mod, &modules, list)
1969                         if ((ret = mod_find_symname(mod, name)) != 0)
1970                                 break;
1971         }
1972         return ret;
1973 }
1974 #endif /* CONFIG_KALLSYMS */
1975
1976 /* Called by the /proc file system to return a list of modules. */
1977 static void *m_start(struct seq_file *m, loff_t *pos)
1978 {
1979         struct list_head *i;
1980         loff_t n = 0;
1981
1982         down(&module_mutex);
1983         list_for_each(i, &modules) {
1984                 if (n++ == *pos)
1985                         break;
1986         }
1987         if (i == &modules)
1988                 return NULL;
1989         return i;
1990 }
1991
1992 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1993 {
1994         struct list_head *i = p;
1995         (*pos)++;
1996         if (i->next == &modules)
1997                 return NULL;
1998         return i->next;
1999 }
2000
2001 static void m_stop(struct seq_file *m, void *p)
2002 {
2003         up(&module_mutex);
2004 }
2005
2006 static int m_show(struct seq_file *m, void *p)
2007 {
2008         struct module *mod = list_entry(p, struct module, list);
2009         seq_printf(m, "%s %lu",
2010                    mod->name, mod->init_size + mod->core_size);
2011         print_unload_info(m, mod);
2012
2013         /* Informative for users. */
2014         seq_printf(m, " %s",
2015                    mod->state == MODULE_STATE_GOING ? "Unloading":
2016                    mod->state == MODULE_STATE_COMING ? "Loading":
2017                    "Live");
2018         /* Used by oprofile and other similar tools. */
2019         seq_printf(m, " 0x%p", mod->module_core);
2020
2021         seq_printf(m, "\n");
2022         return 0;
2023 }
2024
2025 /* Format: modulename size refcount deps address
2026
2027    Where refcount is a number or -, and deps is a comma-separated list
2028    of depends or -.
2029 */
2030 struct seq_operations modules_op = {
2031         .start  = m_start,
2032         .next   = m_next,
2033         .stop   = m_stop,
2034         .show   = m_show
2035 };
2036
2037 /* Given an address, look for it in the module exception tables. */
2038 const struct exception_table_entry *search_module_extables(unsigned long addr)
2039 {
2040         unsigned long flags;
2041         const struct exception_table_entry *e = NULL;
2042         struct module *mod;
2043
2044         spin_lock_irqsave(&modlist_lock, flags);
2045         list_for_each_entry(mod, &modules, list) {
2046                 if (mod->num_exentries == 0)
2047                         continue;
2048                                 
2049                 e = search_extable(mod->extable,
2050                                    mod->extable + mod->num_exentries - 1,
2051                                    addr);
2052                 if (e)
2053                         break;
2054         }
2055         spin_unlock_irqrestore(&modlist_lock, flags);
2056
2057         /* Now, if we found one, we are running inside it now, hence
2058            we cannot unload the module, hence no refcnt needed. */
2059         return e;
2060 }
2061
2062 /* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
2063 struct module *__module_text_address(unsigned long addr)
2064 {
2065         struct module *mod;
2066
2067         list_for_each_entry(mod, &modules, list)
2068                 if (within(addr, mod->module_init, mod->init_text_size)
2069                     || within(addr, mod->module_core, mod->core_text_size))
2070                         return mod;
2071         return NULL;
2072 }
2073
2074 struct module *module_text_address(unsigned long addr)
2075 {
2076         struct module *mod;
2077         unsigned long flags;
2078
2079         spin_lock_irqsave(&modlist_lock, flags);
2080         mod = __module_text_address(addr);
2081         spin_unlock_irqrestore(&modlist_lock, flags);
2082
2083         return mod;
2084 }
2085
2086 /* Don't grab lock, we're oopsing. */
2087 void print_modules(void)
2088 {
2089         struct module *mod;
2090
2091         printk("Modules linked in:");
2092         list_for_each_entry(mod, &modules, list) {
2093                 printk(" %s", mod->name);
2094 #if CONFIG_MODULE_SIG           
2095                 if (!mod->gpgsig_ok)
2096                         printk("(U)");
2097 #endif          
2098         }
2099         printk("\n");
2100 }
2101
2102 void module_add_driver(struct module *mod, struct device_driver *drv)
2103 {
2104         if (!mod || !drv)
2105                 return;
2106         if (!mod->mkobj)
2107                 return;
2108
2109         /* Don't check return code; this call is idempotent */
2110         sysfs_create_link(&drv->kobj, &mod->mkobj->kobj, "module");
2111 }
2112 EXPORT_SYMBOL(module_add_driver);
2113
2114 void module_remove_driver(struct device_driver *drv)
2115 {
2116         if (!drv)
2117                 return;
2118         sysfs_remove_link(&drv->kobj, "module");
2119 }
2120 EXPORT_SYMBOL(module_remove_driver);
2121
2122 #ifdef CONFIG_MODVERSIONS
2123 /* Generate the signature for struct module here, too, for modversions. */
2124 void struct_module(struct module *mod) { return; }
2125 EXPORT_SYMBOL(struct_module);
2126 #endif