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