1 /* 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.
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.
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.
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
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/cpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/vermagic.h>
34 #include <linux/notifier.h>
35 #include <asm/uaccess.h>
36 #include <asm/semaphore.h>
37 #include <asm/pgalloc.h>
38 #include <asm/cacheflush.h>
43 #define DEBUGP(fmt , a...)
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
50 /* If this is set, the section belongs in the init part of the module */
51 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
53 #define symbol_is(literal, string) \
54 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
56 /* Protects module list */
57 static spinlock_t modlist_lock
= SPIN_LOCK_UNLOCKED
;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex
);
61 static LIST_HEAD(modules
);
63 static DECLARE_MUTEX(notify_mutex
);
64 static struct notifier_block
* module_notify_list
;
66 int register_module_notifier(struct notifier_block
* nb
)
70 err
= notifier_chain_register(&module_notify_list
, nb
);
74 EXPORT_SYMBOL(register_module_notifier
);
76 int unregister_module_notifier(struct notifier_block
* nb
)
80 err
= notifier_chain_unregister(&module_notify_list
, nb
);
84 EXPORT_SYMBOL(unregister_module_notifier
);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module
*mod
)
89 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
91 return try_module_get(mod
);
94 /* Stub function for modules which don't have an initfn */
99 EXPORT_SYMBOL(init_module
);
101 /* Find a module section: 0 means not found. */
102 static unsigned int find_sec(Elf_Ehdr
*hdr
,
104 const char *secstrings
,
109 for (i
= 1; i
< hdr
->e_shnum
; i
++)
110 /* Alloc bit cleared means "ignore it." */
111 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
112 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
117 /* Provided by the linker */
118 extern const struct kernel_symbol __start___ksymtab
[];
119 extern const struct kernel_symbol __stop___ksymtab
[];
120 extern const struct kernel_symbol __start___ksymtab_gpl
[];
121 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
122 extern const unsigned long __start___kcrctab
[];
123 extern const unsigned long __start___kcrctab_gpl
[];
125 #ifndef CONFIG_MODVERSIONS
126 #define symversion(base, idx) NULL
128 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
131 /* Find a symbol, return value, crc and module which owns it */
132 static unsigned long __find_symbol(const char *name
,
133 struct module
**owner
,
134 const unsigned long **crc
,
140 /* Core kernel first. */
142 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++) {
143 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0) {
144 *crc
= symversion(__start___kcrctab
, i
);
145 return __start___ksymtab
[i
].value
;
149 for (i
= 0; __start___ksymtab_gpl
+i
<__stop___ksymtab_gpl
; i
++)
150 if (strcmp(__start___ksymtab_gpl
[i
].name
, name
) == 0) {
151 *crc
= symversion(__start___kcrctab_gpl
, i
);
152 return __start___ksymtab_gpl
[i
].value
;
156 /* Now try modules. */
157 list_for_each_entry(mod
, &modules
, list
) {
159 for (i
= 0; i
< mod
->num_syms
; i
++)
160 if (strcmp(mod
->syms
[i
].name
, name
) == 0) {
161 *crc
= symversion(mod
->crcs
, i
);
162 return mod
->syms
[i
].value
;
166 for (i
= 0; i
< mod
->num_gpl_syms
; i
++) {
167 if (strcmp(mod
->gpl_syms
[i
].name
, name
) == 0) {
168 *crc
= symversion(mod
->gpl_crcs
, i
);
169 return mod
->gpl_syms
[i
].value
;
174 DEBUGP("Failed to find symbol %s\n", name
);
178 /* Find a symbol in this elf symbol table */
179 static unsigned long find_local_symbol(Elf_Shdr
*sechdrs
,
180 unsigned int symindex
,
185 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
187 /* Search (defined) internal symbols first. */
188 for (i
= 1; i
< sechdrs
[symindex
].sh_size
/sizeof(*sym
); i
++) {
189 if (sym
[i
].st_shndx
!= SHN_UNDEF
190 && strcmp(name
, strtab
+ sym
[i
].st_name
) == 0)
191 return sym
[i
].st_value
;
196 /* Search for module by name: must hold module_mutex. */
197 static struct module
*find_module(const char *name
)
201 list_for_each_entry(mod
, &modules
, list
) {
202 if (strcmp(mod
->name
, name
) == 0)
209 /* Number of blocks used and allocated. */
210 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
211 /* Size of each block. -ve means used. */
212 static int *pcpu_size
;
214 static int split_block(unsigned int i
, unsigned short size
)
216 /* Reallocation required? */
217 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
218 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated
*2,
223 memcpy(new, pcpu_size
, sizeof(new[0])*pcpu_num_allocated
);
224 pcpu_num_allocated
*= 2;
229 /* Insert a new subblock */
230 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
231 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
234 pcpu_size
[i
+1] -= size
;
239 static inline unsigned int block_size(int val
)
246 /* Created by linker magic */
247 extern char __per_cpu_start
[], __per_cpu_end
[];
249 static void *percpu_modalloc(unsigned long size
, unsigned long align
)
255 BUG_ON(align
> SMP_CACHE_BYTES
);
257 ptr
= __per_cpu_start
;
258 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
259 /* Extra for alignment requirement. */
260 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
261 BUG_ON(i
== 0 && extra
!= 0);
263 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
266 /* Transfer extra to previous block. */
267 if (pcpu_size
[i
-1] < 0)
268 pcpu_size
[i
-1] -= extra
;
270 pcpu_size
[i
-1] += extra
;
271 pcpu_size
[i
] -= extra
;
274 /* Split block if warranted */
275 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
276 if (!split_block(i
, size
))
280 pcpu_size
[i
] = -pcpu_size
[i
];
284 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
289 static void percpu_modfree(void *freeme
)
292 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
294 /* First entry is core kernel percpu data. */
295 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
297 pcpu_size
[i
] = -pcpu_size
[i
];
304 /* Merge with previous? */
305 if (pcpu_size
[i
-1] >= 0) {
306 pcpu_size
[i
-1] += pcpu_size
[i
];
308 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
309 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
312 /* Merge with next? */
313 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
314 pcpu_size
[i
] += pcpu_size
[i
+1];
316 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
317 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
321 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
323 const char *secstrings
)
325 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
328 static int percpu_modinit(void)
331 pcpu_num_allocated
= 2;
332 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
334 /* Static in-kernel percpu data (used). */
335 pcpu_size
[0] = -ALIGN(__per_cpu_end
-__per_cpu_start
, SMP_CACHE_BYTES
);
337 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
338 if (pcpu_size
[1] < 0) {
339 printk(KERN_ERR
"No per-cpu room for modules.\n");
345 __initcall(percpu_modinit
);
346 #else /* ... !CONFIG_SMP */
347 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
)
351 static inline void percpu_modfree(void *pcpuptr
)
355 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
357 const char *secstrings
)
361 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
364 /* pcpusec should be 0, and size of that section should be 0. */
367 #endif /* CONFIG_SMP */
369 #ifdef CONFIG_MODULE_UNLOAD
370 /* Init the unload section of the module. */
371 static void module_unload_init(struct module
*mod
)
375 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
376 for (i
= 0; i
< NR_CPUS
; i
++)
377 atomic_set(&mod
->ref
[i
].count
, 0);
378 /* Hold reference count during initialization. */
379 atomic_set(&mod
->ref
[smp_processor_id()].count
, 1);
380 /* Backwards compatibility macros put refcount during init. */
381 mod
->waiter
= current
;
384 /* modules using other modules */
387 struct list_head list
;
388 struct module
*module_which_uses
;
391 /* Does a already use b? */
392 static int already_uses(struct module
*a
, struct module
*b
)
394 struct module_use
*use
;
396 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
397 if (use
->module_which_uses
== a
) {
398 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
402 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
406 /* Module a uses b */
407 static int use_module(struct module
*a
, struct module
*b
)
409 struct module_use
*use
;
410 if (b
== NULL
|| already_uses(a
, b
)) return 1;
412 if (!strong_try_module_get(b
))
415 DEBUGP("Allocating new usage for %s.\n", a
->name
);
416 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
418 printk("%s: out of memory loading\n", a
->name
);
423 use
->module_which_uses
= a
;
424 list_add(&use
->list
, &b
->modules_which_use_me
);
428 /* Clear the unload stuff of the module. */
429 static void module_unload_free(struct module
*mod
)
433 list_for_each_entry(i
, &modules
, list
) {
434 struct module_use
*use
;
436 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
437 if (use
->module_which_uses
== mod
) {
438 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
440 list_del(&use
->list
);
442 /* There can be at most one match. */
450 /* Thread to stop each CPU in user context. */
458 static enum stopref_state stopref_state
;
459 static unsigned int stopref_num_threads
;
460 static atomic_t stopref_thread_ack
;
462 static int stopref(void *cpu
)
464 int irqs_disabled
= 0;
467 sprintf(current
->comm
, "kmodule%lu\n", (unsigned long)cpu
);
469 /* Highest priority we can manage, and move to right CPU. */
471 struct sched_param param
= { .sched_priority
= MAX_RT_PRIO
-1 };
472 setscheduler(current
->pid
, SCHED_FIFO
, ¶m
);
474 set_cpus_allowed(current
, 1UL << (unsigned long)cpu
);
476 /* Ack: we are alive */
477 atomic_inc(&stopref_thread_ack
);
479 /* Simple state machine */
480 while (stopref_state
!= STOPREF_EXIT
) {
481 if (stopref_state
== STOPREF_DISABLE_IRQ
&& !irqs_disabled
) {
484 /* Ack: irqs disabled. */
485 atomic_inc(&stopref_thread_ack
);
486 } else if (stopref_state
== STOPREF_PREPARE
&& !prepared
) {
487 /* Everyone is in place, hold CPU. */
490 atomic_inc(&stopref_thread_ack
);
492 if (irqs_disabled
|| prepared
)
498 /* Ack: we are exiting. */
499 atomic_inc(&stopref_thread_ack
);
509 /* Change the thread state */
510 static void stopref_set_state(enum stopref_state state
, int sleep
)
512 atomic_set(&stopref_thread_ack
, 0);
514 stopref_state
= state
;
515 while (atomic_read(&stopref_thread_ack
) != stopref_num_threads
) {
523 /* Stop the machine. Disables irqs. */
524 static int stop_refcounts(void)
527 unsigned long old_allowed
;
530 /* One thread per cpu. We'll do our own. */
531 cpu
= smp_processor_id();
533 /* FIXME: racy with set_cpus_allowed. */
534 old_allowed
= current
->cpus_allowed
;
535 set_cpus_allowed(current
, 1UL << (unsigned long)cpu
);
537 atomic_set(&stopref_thread_ack
, 0);
538 stopref_num_threads
= 0;
539 stopref_state
= STOPREF_WAIT
;
541 /* No CPUs can come up or down during this. */
544 for (i
= 0; i
< NR_CPUS
; i
++) {
545 if (i
== cpu
|| !cpu_online(i
))
547 ret
= kernel_thread(stopref
, (void *)(long)i
, CLONE_KERNEL
);
550 stopref_num_threads
++;
553 /* Wait for them all to come to life. */
554 while (atomic_read(&stopref_thread_ack
) != stopref_num_threads
)
557 /* If some failed, kill them all. */
559 stopref_set_state(STOPREF_EXIT
, 1);
564 /* Don't schedule us away at this point, please. */
567 /* Now they are all scheduled, make them hold the CPUs, ready. */
568 stopref_set_state(STOPREF_PREPARE
, 0);
570 /* Make them disable irqs. */
571 stopref_set_state(STOPREF_DISABLE_IRQ
, 0);
577 /* Restart the machine. Re-enables irqs. */
578 static void restart_refcounts(void)
580 stopref_set_state(STOPREF_EXIT
, 0);
586 static inline int stop_refcounts(void)
591 static inline void restart_refcounts(void)
597 unsigned int module_refcount(struct module
*mod
)
599 unsigned int i
, total
= 0;
601 for (i
= 0; i
< NR_CPUS
; i
++)
602 total
+= atomic_read(&mod
->ref
[i
].count
);
605 EXPORT_SYMBOL(module_refcount
);
607 /* This exists whether we can unload or not */
608 static void free_module(struct module
*mod
);
610 #ifdef CONFIG_MODULE_FORCE_UNLOAD
611 static inline int try_force(unsigned int flags
)
613 return (flags
& O_TRUNC
);
616 static inline int try_force(unsigned int flags
)
620 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
622 /* Stub function for modules which don't have an exitfn */
623 void cleanup_module(void)
626 EXPORT_SYMBOL(cleanup_module
);
628 static void wait_for_zero_refcount(struct module
*mod
)
630 /* Since we might sleep for some time, drop the semaphore first */
633 DEBUGP("Looking at refcount...\n");
634 set_current_state(TASK_UNINTERRUPTIBLE
);
635 if (module_refcount(mod
) == 0)
639 current
->state
= TASK_RUNNING
;
644 sys_delete_module(const char __user
*name_user
, unsigned int flags
)
647 char name
[MODULE_NAME_LEN
];
650 if (!capable(CAP_SYS_MODULE
))
653 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
655 name
[MODULE_NAME_LEN
-1] = '\0';
657 if (down_interruptible(&module_mutex
) != 0)
660 mod
= find_module(name
);
666 if (!list_empty(&mod
->modules_which_use_me
)) {
667 /* Other modules depend on us: get rid of them first. */
673 if (mod
->state
== MODULE_STATE_GOING
) {
674 /* FIXME: if (force), slam module count and wake up
676 DEBUGP("%s already dying\n", mod
->name
);
681 /* If it has an init func, it must have an exit func to unload */
682 if ((mod
->init
!= init_module
&& mod
->exit
== cleanup_module
)
684 forced
= try_force(flags
);
686 /* This module can't be removed */
691 /* Stop the machine so refcounts can't move: irqs disabled. */
692 DEBUGP("Stopping refcounts...\n");
693 ret
= stop_refcounts();
697 /* If it's not unused, quit unless we are told to block. */
698 if ((flags
& O_NONBLOCK
) && module_refcount(mod
) != 0) {
699 forced
= try_force(flags
);
707 /* Mark it as dying. */
708 mod
->waiter
= current
;
709 mod
->state
= MODULE_STATE_GOING
;
712 /* Never wait if forced. */
713 if (!forced
&& module_refcount(mod
) != 0)
714 wait_for_zero_refcount(mod
);
716 /* Final destruction now noone is using it. */
725 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
727 struct module_use
*use
;
728 int printed_something
= 0;
730 seq_printf(m
, " %u ", module_refcount(mod
));
732 /* Always include a trailing , so userspace can differentiate
733 between this and the old multi-field proc format. */
734 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
735 printed_something
= 1;
736 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
740 printed_something
= 1;
741 seq_printf(m
, "[unsafe],");
744 if (mod
->init
!= init_module
&& mod
->exit
== cleanup_module
) {
745 printed_something
= 1;
746 seq_printf(m
, "[permanent],");
749 if (!printed_something
)
753 void __symbol_put(const char *symbol
)
755 struct module
*owner
;
757 const unsigned long *crc
;
759 spin_lock_irqsave(&modlist_lock
, flags
);
760 if (!__find_symbol(symbol
, &owner
, &crc
, 1))
763 spin_unlock_irqrestore(&modlist_lock
, flags
);
765 EXPORT_SYMBOL(__symbol_put
);
767 void symbol_put_addr(void *addr
)
771 spin_lock_irqsave(&modlist_lock
, flags
);
772 if (!kernel_text_address((unsigned long)addr
))
775 module_put(module_text_address((unsigned long)addr
));
776 spin_unlock_irqrestore(&modlist_lock
, flags
);
778 EXPORT_SYMBOL_GPL(symbol_put_addr
);
780 #else /* !CONFIG_MODULE_UNLOAD */
781 static void print_unload_info(struct seq_file
*m
, struct module
*mod
)
783 /* We don't know the usage count, or what modules are using. */
784 seq_printf(m
, " - -");
787 static inline void module_unload_free(struct module
*mod
)
791 static inline int use_module(struct module
*a
, struct module
*b
)
793 return strong_try_module_get(b
);
796 static inline void module_unload_init(struct module
*mod
)
801 sys_delete_module(const char *name_user
, unsigned int flags
)
806 #endif /* CONFIG_MODULE_UNLOAD */
808 #ifdef CONFIG_OBSOLETE_MODPARM
809 static int param_set_byte(const char *val
, struct kernel_param
*kp
)
814 if (!val
) return -EINVAL
;
815 l
= simple_strtol(val
, &endp
, 0);
816 if (endp
== val
|| *endp
|| ((char)l
!= l
))
818 *((char *)kp
->arg
) = l
;
822 /* Bounds checking done below */
823 static int obsparm_copy_string(const char *val
, struct kernel_param
*kp
)
825 strcpy(kp
->arg
, val
);
829 int set_obsolete(const char *val
, struct kernel_param
*kp
)
831 unsigned int min
, max
;
832 unsigned int size
, maxsize
;
835 struct obsolete_modparm
*obsparm
= kp
->arg
;
838 printk(KERN_ERR
"Parameter %s needs an argument\n", kp
->name
);
842 /* type is: [min[-max]]{b,h,i,l,s} */
844 min
= simple_strtol(p
, &endp
, 10);
845 if (endp
== obsparm
->type
)
847 else if (*endp
== '-') {
849 max
= simple_strtol(p
, &endp
, 10);
854 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
857 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
858 sizeof(short), param_set_short
);
860 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
861 sizeof(int), param_set_int
);
863 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
864 sizeof(long), param_set_long
);
866 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
867 sizeof(char *), param_set_charp
);
870 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
871 and the decl is "char xxx[5][50];" */
873 maxsize
= simple_strtol(p
, &endp
, 10);
874 /* We check lengths here (yes, this is a hack). */
876 while (p
[size
= strcspn(p
, ",")]) {
883 return param_array(kp
->name
, val
, min
, max
, obsparm
->addr
,
884 maxsize
, obsparm_copy_string
);
886 printk(KERN_ERR
"Unknown obsolete parameter type %s\n", obsparm
->type
);
890 "Parameter %s doesn't fit in %u chars.\n", kp
->name
, maxsize
);
894 static int obsolete_params(const char *name
,
896 struct obsolete_modparm obsparm
[],
899 unsigned int symindex
,
902 struct kernel_param
*kp
;
906 kp
= kmalloc(sizeof(kp
[0]) * num
, GFP_KERNEL
);
910 for (i
= 0; i
< num
; i
++) {
911 char sym_name
[128 + sizeof(MODULE_SYMBOL_PREFIX
)];
913 snprintf(sym_name
, sizeof(sym_name
), "%s%s",
914 MODULE_SYMBOL_PREFIX
, obsparm
[i
].name
);
916 kp
[i
].name
= obsparm
[i
].name
;
918 kp
[i
].set
= set_obsolete
;
921 = (void *)find_local_symbol(sechdrs
, symindex
, strtab
,
923 if (!obsparm
[i
].addr
) {
924 printk("%s: falsely claims to have parameter %s\n",
925 name
, obsparm
[i
].name
);
929 kp
[i
].arg
= &obsparm
[i
];
932 ret
= parse_args(name
, args
, kp
, num
, NULL
);
938 static int obsolete_params(const char *name
,
940 struct obsolete_modparm obsparm
[],
943 unsigned int symindex
,
947 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
951 #endif /* CONFIG_OBSOLETE_MODPARM */
953 static const char vermagic
[] = VERMAGIC_STRING
;
955 #ifdef CONFIG_MODVERSIONS
956 static int check_version(Elf_Shdr
*sechdrs
,
957 unsigned int versindex
,
960 const unsigned long *crc
)
962 unsigned int i
, num_versions
;
963 struct modversion_info
*versions
;
965 /* Exporting module didn't supply crcs? OK, we're already tainted. */
969 versions
= (void *) sechdrs
[versindex
].sh_addr
;
970 num_versions
= sechdrs
[versindex
].sh_size
971 / sizeof(struct modversion_info
);
973 for (i
= 0; i
< num_versions
; i
++) {
974 if (strcmp(versions
[i
].name
, symname
) != 0)
977 if (versions
[i
].crc
== *crc
)
979 printk("%s: disagrees about version of symbol %s\n",
981 DEBUGP("Found checksum %lX vs module %lX\n",
982 *crc
, versions
[i
].crc
);
985 /* Not in module's version table. OK, but that taints the kernel. */
986 if (!(tainted
& TAINT_FORCED_MODULE
)) {
987 printk("%s: no version for \"%s\" found: kernel tainted.\n",
989 tainted
|= TAINT_FORCED_MODULE
;
994 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
995 unsigned int versindex
,
998 const unsigned long *crc
;
999 struct module
*owner
;
1001 if (!__find_symbol("struct_module", &owner
, &crc
, 1))
1003 return check_version(sechdrs
, versindex
, "struct_module", mod
,
1007 /* First part is kernel version, which we ignore. */
1008 static inline int same_magic(const char *amagic
, const char *bmagic
)
1010 amagic
+= strcspn(amagic
, " ");
1011 bmagic
+= strcspn(bmagic
, " ");
1012 return strcmp(amagic
, bmagic
) == 0;
1015 static inline int check_version(Elf_Shdr
*sechdrs
,
1016 unsigned int versindex
,
1017 const char *symname
,
1019 const unsigned long *crc
)
1024 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1025 unsigned int versindex
,
1031 static inline int same_magic(const char *amagic
, const char *bmagic
)
1033 return strcmp(amagic
, bmagic
) == 0;
1035 #endif /* CONFIG_MODVERSIONS */
1037 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1038 Must be holding module_mutex. */
1039 static unsigned long resolve_symbol(Elf_Shdr
*sechdrs
,
1040 unsigned int versindex
,
1044 struct module
*owner
;
1046 const unsigned long *crc
;
1048 spin_lock_irq(&modlist_lock
);
1049 ret
= __find_symbol(name
, &owner
, &crc
, mod
->license_gplok
);
1051 /* use_module can fail due to OOM, or module unloading */
1052 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1053 !use_module(mod
, owner
))
1056 spin_unlock_irq(&modlist_lock
);
1060 /* Free a module, remove from lists, etc (must hold module mutex). */
1061 static void free_module(struct module
*mod
)
1063 /* Delete from various lists */
1064 spin_lock_irq(&modlist_lock
);
1065 list_del(&mod
->list
);
1066 spin_unlock_irq(&modlist_lock
);
1068 /* Arch-specific cleanup. */
1069 module_arch_cleanup(mod
);
1071 /* Module unload stuff */
1072 module_unload_free(mod
);
1074 /* This may be NULL, but that's OK */
1075 module_free(mod
, mod
->module_init
);
1078 percpu_modfree(mod
->percpu
);
1080 /* Finally, free the core (containing the module structure) */
1081 module_free(mod
, mod
->module_core
);
1084 void *__symbol_get(const char *symbol
)
1086 struct module
*owner
;
1087 unsigned long value
, flags
;
1088 const unsigned long *crc
;
1090 spin_lock_irqsave(&modlist_lock
, flags
);
1091 value
= __find_symbol(symbol
, &owner
, &crc
, 1);
1092 if (value
&& !strong_try_module_get(owner
))
1094 spin_unlock_irqrestore(&modlist_lock
, flags
);
1096 return (void *)value
;
1098 EXPORT_SYMBOL_GPL(__symbol_get
);
1100 /* Change all symbols so that sh_value encodes the pointer directly. */
1101 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1102 unsigned int symindex
,
1104 unsigned int versindex
,
1105 unsigned int pcpuindex
,
1108 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1109 unsigned long secbase
;
1110 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1113 for (i
= 1; i
< n
; i
++) {
1114 switch (sym
[i
].st_shndx
) {
1116 /* We compiled with -fno-common. These are not
1117 supposed to happen. */
1118 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1123 /* Don't need to do anything */
1124 DEBUGP("Absolute symbol: 0x%08lx\n",
1125 (long)sym
[i
].st_value
);
1130 = resolve_symbol(sechdrs
, versindex
,
1131 strtab
+ sym
[i
].st_name
, mod
);
1133 /* Ok if resolved. */
1134 if (sym
[i
].st_value
!= 0)
1137 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1140 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1141 mod
->name
, strtab
+ sym
[i
].st_name
);
1146 /* Divert to percpu allocation if a percpu var. */
1147 if (sym
[i
].st_shndx
== pcpuindex
)
1148 secbase
= (unsigned long)mod
->percpu
;
1150 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1151 sym
[i
].st_value
+= secbase
;
1159 /* Update size with this section: return offset. */
1160 static long get_offset(unsigned long *size
, Elf_Shdr
*sechdr
)
1164 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1165 *size
= ret
+ sechdr
->sh_size
;
1169 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1170 might -- code, read-only data, read-write data, small data. Tally
1171 sizes, and place the offsets into sh_entsize fields: high bit means it
1173 static void layout_sections(struct module
*mod
,
1174 const Elf_Ehdr
*hdr
,
1176 const char *secstrings
)
1178 static unsigned long const masks
[][2] = {
1179 /* NOTE: all executable code must be the first section
1180 * in this array; otherwise modify the text_size
1181 * finder in the two loops below */
1182 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1183 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1184 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1185 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1189 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1190 sechdrs
[i
].sh_entsize
= ~0UL;
1192 DEBUGP("Core section allocation order:\n");
1193 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1194 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1195 Elf_Shdr
*s
= &sechdrs
[i
];
1197 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1198 || (s
->sh_flags
& masks
[m
][1])
1199 || s
->sh_entsize
!= ~0UL
1200 || strstr(secstrings
+ s
->sh_name
, ".init"))
1202 s
->sh_entsize
= get_offset(&mod
->core_size
, s
);
1203 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1206 mod
->core_text_size
= mod
->core_size
;
1209 DEBUGP("Init section allocation order:\n");
1210 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1211 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1212 Elf_Shdr
*s
= &sechdrs
[i
];
1214 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1215 || (s
->sh_flags
& masks
[m
][1])
1216 || s
->sh_entsize
!= ~0UL
1217 || !strstr(secstrings
+ s
->sh_name
, ".init"))
1219 s
->sh_entsize
= (get_offset(&mod
->init_size
, s
)
1220 | INIT_OFFSET_MASK
);
1221 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1224 mod
->init_text_size
= mod
->init_size
;
1228 static inline int license_is_gpl_compatible(const char *license
)
1230 return (strcmp(license
, "GPL") == 0
1231 || strcmp(license
, "GPL v2") == 0
1232 || strcmp(license
, "GPL and additional rights") == 0
1233 || strcmp(license
, "Dual BSD/GPL") == 0
1234 || strcmp(license
, "Dual MPL/GPL") == 0);
1237 static void set_license(struct module
*mod
, const char *license
)
1240 license
= "unspecified";
1242 mod
->license_gplok
= license_is_gpl_compatible(license
);
1243 if (!mod
->license_gplok
) {
1244 printk(KERN_WARNING
"%s: module license '%s' taints kernel.\n",
1245 mod
->name
, license
);
1246 tainted
|= TAINT_PROPRIETARY_MODULE
;
1250 /* Parse tag=value strings from .modinfo section */
1251 static char *next_string(char *string
, unsigned long *secsize
)
1253 /* Skip non-zero chars */
1256 if ((*secsize
)-- <= 1)
1260 /* Skip any zero padding. */
1261 while (!string
[0]) {
1263 if ((*secsize
)-- <= 1)
1269 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1274 unsigned int taglen
= strlen(tag
);
1275 unsigned long size
= sechdrs
[info
].sh_size
;
1277 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1278 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1279 return p
+ taglen
+ 1;
1284 #ifdef CONFIG_KALLSYMS
1285 int is_exported(const char *name
, const struct module
*mod
)
1290 for (i
= 0; __start___ksymtab
+i
< __stop___ksymtab
; i
++)
1291 if (strcmp(__start___ksymtab
[i
].name
, name
) == 0)
1295 for (i
= 0; i
< mod
->num_syms
; i
++)
1296 if (strcmp(mod
->syms
[i
].name
, name
) == 0)
1302 static char elf_type(const Elf_Sym
*sym
,
1304 const char *secstrings
,
1307 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1308 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1313 if (sym
->st_shndx
== SHN_UNDEF
)
1315 if (sym
->st_shndx
== SHN_ABS
)
1317 if (sym
->st_shndx
>= SHN_LORESERVE
)
1319 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1321 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1322 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1323 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1325 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1330 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1331 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1336 if (strncmp(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1337 ".debug", strlen(".debug")) == 0)
1342 static void add_kallsyms(struct module
*mod
,
1344 unsigned int symindex
,
1345 unsigned int strindex
,
1346 const char *secstrings
)
1350 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1351 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1352 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1354 /* Set types up while we still have access to sections. */
1355 for (i
= 0; i
< mod
->num_symtab
; i
++)
1356 mod
->symtab
[i
].st_info
1357 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1361 /* Allocate and load the module: note that size of section 0 is always
1362 zero, and we rely on this for optional sections. */
1363 static struct module
*load_module(void __user
*umod
,
1365 const char __user
*uargs
)
1369 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1370 unsigned int i
, symindex
= 0, strindex
= 0, setupindex
, exindex
,
1371 exportindex
, modindex
, obsparmindex
, infoindex
, gplindex
,
1372 crcindex
, gplcrcindex
, versindex
, pcpuindex
;
1376 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1378 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1380 if (len
< sizeof(*hdr
))
1381 return ERR_PTR(-ENOEXEC
);
1383 /* Suck in entire file: we'll want most of it. */
1384 /* vmalloc barfs on "unusual" numbers. Check here */
1385 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1386 return ERR_PTR(-ENOMEM
);
1387 if (copy_from_user(hdr
, umod
, len
) != 0) {
1392 /* Sanity checks against insmoding binaries or wrong arch,
1393 weird elf version */
1394 if (memcmp(hdr
->e_ident
, ELFMAG
, 4) != 0
1395 || hdr
->e_type
!= ET_REL
1396 || !elf_check_arch(hdr
)
1397 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1402 /* Convenience variables */
1403 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1404 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1405 sechdrs
[0].sh_addr
= 0;
1407 /* And these should exist, but gcc whinges if we don't init them */
1408 symindex
= strindex
= 0;
1410 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1411 /* Mark all sections sh_addr with their address in the
1413 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1415 /* Internal symbols and strings. */
1416 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1418 strindex
= sechdrs
[i
].sh_link
;
1419 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1421 #ifndef CONFIG_MODULE_UNLOAD
1422 /* Don't load .exit sections */
1423 if (strstr(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
1424 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1428 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1429 ".gnu.linkonce.this_module");
1431 printk(KERN_WARNING
"No module found in object\n");
1435 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1437 /* Optional sections */
1438 exportindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab");
1439 gplindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl");
1440 crcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab");
1441 gplcrcindex
= find_sec(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
1442 setupindex
= find_sec(hdr
, sechdrs
, secstrings
, "__param");
1443 exindex
= find_sec(hdr
, sechdrs
, secstrings
, "__ex_table");
1444 obsparmindex
= find_sec(hdr
, sechdrs
, secstrings
, "__obsparm");
1445 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1446 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1447 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1449 /* Don't keep modinfo section */
1450 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1451 #ifdef CONFIG_KALLSYMS
1452 /* Keep symbol and string tables for decoding later. */
1453 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1454 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1457 /* Check module struct version now, before we try to use module. */
1458 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1463 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1464 /* This is allowed: modprobe --force will invalidate it. */
1466 tainted
|= TAINT_FORCED_MODULE
;
1467 printk(KERN_WARNING
"%s: no version magic, tainting kernel.\n",
1469 } else if (!same_magic(modmagic
, vermagic
)) {
1470 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
1471 mod
->name
, modmagic
, vermagic
);
1476 /* Now copy in args */
1477 arglen
= strlen_user(uargs
);
1482 args
= kmalloc(arglen
, GFP_KERNEL
);
1487 if (copy_from_user(args
, uargs
, arglen
) != 0) {
1492 if (find_module(mod
->name
)) {
1497 mod
->state
= MODULE_STATE_COMING
;
1499 /* Allow arches to frob section contents and sizes. */
1500 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
1505 /* We have a special allocation for this section. */
1506 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
1507 sechdrs
[pcpuindex
].sh_addralign
);
1512 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1513 mod
->percpu
= percpu
;
1516 /* Determine total sizes, and put offsets in sh_entsize. For now
1517 this is done generically; there doesn't appear to be any
1518 special cases for the architectures. */
1519 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
1521 /* Do the allocs. */
1522 ptr
= module_alloc(mod
->core_size
);
1527 memset(ptr
, 0, mod
->core_size
);
1528 mod
->module_core
= ptr
;
1530 ptr
= module_alloc(mod
->init_size
);
1531 if (!ptr
&& mod
->init_size
) {
1535 memset(ptr
, 0, mod
->init_size
);
1536 mod
->module_init
= ptr
;
1538 /* Transfer each section which specifies SHF_ALLOC */
1539 DEBUGP("final section addresses:\n");
1540 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
1543 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1546 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
1547 dest
= mod
->module_init
1548 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
1550 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
1552 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
1553 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
1554 sechdrs
[i
].sh_size
);
1555 /* Update sh_addr to point to copy in image. */
1556 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
1557 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
1559 /* Module has been moved. */
1560 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1562 /* Now we've moved module, initialize linked lists, etc. */
1563 module_unload_init(mod
);
1565 /* Set up license info based on the info section */
1566 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
1568 /* Fix up syms, so that st_value is a pointer to location. */
1569 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
1574 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1575 mod
->num_syms
= sechdrs
[exportindex
].sh_size
/ sizeof(*mod
->syms
);
1576 mod
->syms
= (void *)sechdrs
[exportindex
].sh_addr
;
1578 mod
->crcs
= (void *)sechdrs
[crcindex
].sh_addr
;
1579 mod
->num_gpl_syms
= sechdrs
[gplindex
].sh_size
/ sizeof(*mod
->gpl_syms
);
1580 mod
->gpl_syms
= (void *)sechdrs
[gplindex
].sh_addr
;
1582 mod
->gpl_crcs
= (void *)sechdrs
[gplcrcindex
].sh_addr
;
1584 #ifdef CONFIG_MODVERSIONS
1585 if ((mod
->num_syms
&& !crcindex
) ||
1586 (mod
->num_gpl_syms
&& !gplcrcindex
)) {
1587 printk(KERN_WARNING
"%s: No versions for exported symbols."
1588 " Tainting kernel.\n", mod
->name
);
1589 tainted
|= TAINT_FORCED_MODULE
;
1593 /* Set up exception table */
1594 mod
->num_exentries
= sechdrs
[exindex
].sh_size
/ sizeof(*mod
->extable
);
1595 mod
->extable
= (void *)sechdrs
[exindex
].sh_addr
;
1597 /* Now do relocations. */
1598 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1599 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
1600 if (sechdrs
[i
].sh_type
== SHT_REL
)
1601 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
1602 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
1603 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
1609 /* Finally, copy percpu area over. */
1610 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
1611 sechdrs
[pcpuindex
].sh_size
);
1613 err
= module_finalize(hdr
, sechdrs
, mod
);
1617 #ifdef CONFIG_KALLSYMS
1618 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
1623 err
= obsolete_params(mod
->name
, mod
->args
,
1624 (struct obsolete_modparm
*)
1625 sechdrs
[obsparmindex
].sh_addr
,
1626 sechdrs
[obsparmindex
].sh_size
1627 / sizeof(struct obsolete_modparm
),
1629 (char *)sechdrs
[strindex
].sh_addr
);
1631 /* Size of section 0 is 0, so this works well if no params */
1632 err
= parse_args(mod
->name
, mod
->args
,
1633 (struct kernel_param
*)
1634 sechdrs
[setupindex
].sh_addr
,
1635 sechdrs
[setupindex
].sh_size
1636 / sizeof(struct kernel_param
),
1642 /* Get rid of temporary copy */
1649 module_unload_free(mod
);
1650 module_free(mod
, mod
->module_init
);
1652 module_free(mod
, mod
->module_core
);
1655 percpu_modfree(percpu
);
1660 if (err
< 0) return ERR_PTR(err
);
1664 /* This is where the real work happens */
1666 sys_init_module(void __user
*umod
,
1668 const char __user
*uargs
)
1673 /* Must have permission */
1674 if (!capable(CAP_SYS_MODULE
))
1677 /* Only one module load at a time, please */
1678 if (down_interruptible(&module_mutex
) != 0)
1681 /* Do all the hard work */
1682 mod
= load_module(umod
, len
, uargs
);
1685 return PTR_ERR(mod
);
1688 /* Flush the instruction cache, since we've played with text */
1689 if (mod
->module_init
)
1690 flush_icache_range((unsigned long)mod
->module_init
,
1691 (unsigned long)mod
->module_init
1693 flush_icache_range((unsigned long)mod
->module_core
,
1694 (unsigned long)mod
->module_core
+ mod
->core_size
);
1696 /* Now sew it into the lists. They won't access us, since
1697 strong_try_module_get() will fail. */
1698 spin_lock_irq(&modlist_lock
);
1699 list_add(&mod
->list
, &modules
);
1700 spin_unlock_irq(&modlist_lock
);
1702 /* Drop lock so they can recurse */
1705 down(¬ify_mutex
);
1706 notifier_call_chain(&module_notify_list
, MODULE_STATE_COMING
, mod
);
1709 /* Start the module */
1712 /* Init routine failed: abort. Try to protect us from
1713 buggy refcounters. */
1714 mod
->state
= MODULE_STATE_GOING
;
1715 synchronize_kernel();
1717 printk(KERN_ERR
"%s: module is now stuck!\n",
1721 down(&module_mutex
);
1728 /* Now it's a first class citizen! */
1729 down(&module_mutex
);
1730 mod
->state
= MODULE_STATE_LIVE
;
1731 /* Drop initial reference. */
1733 module_free(mod
, mod
->module_init
);
1734 mod
->module_init
= NULL
;
1736 mod
->init_text_size
= 0;
1742 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
1744 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
1747 #ifdef CONFIG_KALLSYMS
1748 static const char *get_ksymbol(struct module
*mod
,
1750 unsigned long *size
,
1751 unsigned long *offset
)
1753 unsigned int i
, best
= 0;
1754 unsigned long nextval
;
1756 /* At worse, next value is at end of module */
1757 if (within(addr
, mod
->module_init
, mod
->init_size
))
1758 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
1760 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
1762 /* Scan for closest preceeding symbol, and next symbol. (ELF
1763 starts real symbols at 1). */
1764 for (i
= 1; i
< mod
->num_symtab
; i
++) {
1765 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
1768 /* We ignore unnamed symbols: they're uninformative
1769 * and inserted at a whim. */
1770 if (mod
->symtab
[i
].st_value
<= addr
1771 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
1772 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0' )
1774 if (mod
->symtab
[i
].st_value
> addr
1775 && mod
->symtab
[i
].st_value
< nextval
1776 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0')
1777 nextval
= mod
->symtab
[i
].st_value
;
1783 *size
= nextval
- mod
->symtab
[best
].st_value
;
1784 *offset
= addr
- mod
->symtab
[best
].st_value
;
1785 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
1788 /* For kallsyms to ask for address resolution. NULL means not found.
1789 We don't lock, as this is used for oops resolution and races are a
1791 const char *module_address_lookup(unsigned long addr
,
1792 unsigned long *size
,
1793 unsigned long *offset
,
1798 list_for_each_entry(mod
, &modules
, list
) {
1799 if (within(addr
, mod
->module_init
, mod
->init_size
)
1800 || within(addr
, mod
->module_core
, mod
->core_size
)) {
1801 *modname
= mod
->name
;
1802 return get_ksymbol(mod
, addr
, size
, offset
);
1808 struct module
*module_get_kallsym(unsigned int symnum
,
1809 unsigned long *value
,
1815 down(&module_mutex
);
1816 list_for_each_entry(mod
, &modules
, list
) {
1817 if (symnum
< mod
->num_symtab
) {
1818 *value
= mod
->symtab
[symnum
].st_value
;
1819 *type
= mod
->symtab
[symnum
].st_info
;
1821 mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
1826 symnum
-= mod
->num_symtab
;
1831 #endif /* CONFIG_KALLSYMS */
1833 /* Called by the /proc file system to return a list of modules. */
1834 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
1836 struct list_head
*i
;
1839 down(&module_mutex
);
1840 list_for_each(i
, &modules
) {
1849 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1851 struct list_head
*i
= p
;
1853 if (i
->next
== &modules
)
1858 static void m_stop(struct seq_file
*m
, void *p
)
1863 static int m_show(struct seq_file
*m
, void *p
)
1865 struct module
*mod
= list_entry(p
, struct module
, list
);
1866 seq_printf(m
, "%s %lu",
1867 mod
->name
, mod
->init_size
+ mod
->core_size
);
1868 print_unload_info(m
, mod
);
1870 /* Informative for users. */
1871 seq_printf(m
, " %s",
1872 mod
->state
== MODULE_STATE_GOING
? "Unloading":
1873 mod
->state
== MODULE_STATE_COMING
? "Loading":
1875 /* Used by oprofile and other similar tools. */
1876 seq_printf(m
, " 0x%p", mod
->module_core
);
1878 seq_printf(m
, "\n");
1882 /* Format: modulename size refcount deps address
1884 Where refcount is a number or -, and deps is a comma-separated list
1887 struct seq_operations modules_op
= {
1894 /* Given an address, look for it in the module exception tables. */
1895 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
1897 unsigned long flags
;
1898 const struct exception_table_entry
*e
= NULL
;
1901 spin_lock_irqsave(&modlist_lock
, flags
);
1902 list_for_each_entry(mod
, &modules
, list
) {
1903 if (mod
->num_exentries
== 0)
1906 e
= search_extable(mod
->extable
,
1907 mod
->extable
+ mod
->num_exentries
- 1,
1912 spin_unlock_irqrestore(&modlist_lock
, flags
);
1914 /* Now, if we found one, we are running inside it now, hence
1915 we cannot unload the module, hence no refcnt needed. */
1919 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1920 struct module
*module_text_address(unsigned long addr
)
1924 list_for_each_entry(mod
, &modules
, list
)
1925 if (within(addr
, mod
->module_init
, mod
->init_text_size
)
1926 || within(addr
, mod
->module_core
, mod
->core_text_size
))
1931 #ifdef CONFIG_MODVERSIONS
1932 /* Generate the signature for struct module here, too, for modversions. */
1933 void struct_module(struct module
*mod
) { return; }
1934 EXPORT_SYMBOL(struct_module
);