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