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