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