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