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