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