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