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