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