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