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