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