1 #include <linux/config.h>
3 #include <linux/module.h>
4 #include <asm/uaccess.h>
5 #include <linux/vmalloc.h>
6 #include <linux/smp_lock.h>
7 #include <asm/pgalloc.h>
8 #include <linux/init.h>
11 * Originally by Anonymous (as far as I know...)
12 * Linux version by Bas Laarhoven <bas@vimec.nl>
13 * 0.99.14 version by Jon Tombs <jon@gtex02.us.es>,
14 * Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C)
15 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
16 * Add MOD_INITIALIZING Keith Owens <kaos@ocs.com.au> Nov 1999
18 * This source is covered by the GNU GPL, the same as all kernel sources.
21 #ifdef CONFIG_MODULES /* a *big* #ifdef block... */
23 extern struct module_symbol __start___ksymtab
[];
24 extern struct module_symbol __stop___ksymtab
[];
26 extern const struct exception_table_entry __start___ex_table
[];
27 extern const struct exception_table_entry __stop___ex_table
[];
29 static struct module kernel_module
=
31 sizeof(struct module
), /* size_of_struct */
35 {ATOMIC_INIT(1)}, /* usecount */
36 MOD_RUNNING
, /* flags */
37 0, /* nsyms -- to filled in in init_modules */
39 __start___ksymtab
, /* syms */
44 __start___ex_table
, /* ex_table_start */
45 __stop___ex_table
, /* ex_table_end */
49 struct module
*module_list
= &kernel_module
;
51 static long get_mod_name(const char *user_name
, char **buf
);
52 static void put_mod_name(char *buf
);
53 static struct module
*find_module(const char *name
);
54 static void free_module(struct module
*, int tag_freed
);
61 void __init
init_modules(void)
63 kernel_module
.nsyms
= __stop___ksymtab
- __start___ksymtab
;
66 __asm__("stq $29,%0" : "=m"(kernel_module
.gp
));
71 * Copy the name of a module from user space.
75 get_mod_name(const char *user_name
, char **buf
)
80 page
= __get_free_page(GFP_KERNEL
);
84 retval
= strncpy_from_user((char *)page
, user_name
, PAGE_SIZE
);
86 if (retval
< PAGE_SIZE
) {
90 retval
= -ENAMETOOLONG
;
99 put_mod_name(char *buf
)
101 free_page((unsigned long)buf
);
105 * Allocate space for a module.
108 asmlinkage
unsigned long
109 sys_create_module(const char *name_user
, size_t size
)
115 if (!capable(CAP_SYS_MODULE
))
118 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
122 if (size
< sizeof(struct module
)+namelen
) {
126 if (find_module(name
) != NULL
) {
130 if ((mod
= (struct module
*)module_map(size
)) == NULL
) {
135 memset(mod
, 0, sizeof(*mod
));
136 mod
->size_of_struct
= sizeof(*mod
);
137 mod
->next
= module_list
;
138 mod
->name
= (char *)(mod
+ 1);
140 memcpy((char*)(mod
+1), name
, namelen
+1);
144 module_list
= mod
; /* link it in */
156 * Initialize a module.
160 sys_init_module(const char *name_user
, struct module
*mod_user
)
162 struct module mod_tmp
, *mod
;
164 long namelen
, n_namelen
, i
, error
;
165 unsigned long mod_user_size
;
166 struct module_ref
*dep
;
168 if (!capable(CAP_SYS_MODULE
))
171 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
175 if ((mod
= find_module(name
)) == NULL
) {
180 /* Check module header size. We allow a bit of slop over the
181 size we are familiar with to cope with a version of insmod
182 for a newer kernel. But don't over do it. */
183 if ((error
= get_user(mod_user_size
, &mod_user
->size_of_struct
)) != 0)
185 if (mod_user_size
< (unsigned long)&((struct module
*)0L)->persist_start
186 || mod_user_size
> sizeof(struct module
) + 16*sizeof(void*)) {
187 printk(KERN_ERR
"init_module: Invalid module header size.\n"
188 KERN_ERR
"A new version of the modutils is likely "
194 /* Hold the current contents while we play with the user's idea
198 error
= copy_from_user(mod
, mod_user
, sizeof(struct module
));
204 /* Sanity check the size of the module. */
207 if (mod
->size
> mod_tmp
.size
) {
208 printk(KERN_ERR
"init_module: Size of initialized module "
209 "exceeds size of created module.\n");
213 /* Make sure all interesting pointers are sane. */
215 #define bound(p, n, m) ((unsigned long)(p) >= (unsigned long)(m+1) && \
216 (unsigned long)((p)+(n)) <= (unsigned long)(m) + (m)->size)
218 if (!bound(mod
->name
, namelen
, mod
)) {
219 printk(KERN_ERR
"init_module: mod->name out of bounds.\n");
222 if (mod
->nsyms
&& !bound(mod
->syms
, mod
->nsyms
, mod
)) {
223 printk(KERN_ERR
"init_module: mod->syms out of bounds.\n");
226 if (mod
->ndeps
&& !bound(mod
->deps
, mod
->ndeps
, mod
)) {
227 printk(KERN_ERR
"init_module: mod->deps out of bounds.\n");
230 if (mod
->init
&& !bound(mod
->init
, 0, mod
)) {
231 printk(KERN_ERR
"init_module: mod->init out of bounds.\n");
234 if (mod
->cleanup
&& !bound(mod
->cleanup
, 0, mod
)) {
235 printk(KERN_ERR
"init_module: mod->cleanup out of bounds.\n");
238 if (mod
->ex_table_start
> mod
->ex_table_end
239 || (mod
->ex_table_start
&&
240 !((unsigned long)mod
->ex_table_start
>= (unsigned long)(mod
+1)
241 && ((unsigned long)mod
->ex_table_end
242 < (unsigned long)mod
+ mod
->size
)))
243 || (((unsigned long)mod
->ex_table_start
244 - (unsigned long)mod
->ex_table_end
)
245 % sizeof(struct exception_table_entry
))) {
246 printk(KERN_ERR
"init_module: mod->ex_table_* invalid.\n");
249 if (mod
->flags
& ~MOD_AUTOCLEAN
) {
250 printk(KERN_ERR
"init_module: mod->flags invalid.\n");
254 if (!bound(mod
->gp
- 0x8000, 0, mod
)) {
255 printk(KERN_ERR
"init_module: mod->gp out of bounds.\n");
259 if (mod_member_present(mod
, can_unload
)
260 && mod
->can_unload
&& !bound(mod
->can_unload
, 0, mod
)) {
261 printk(KERN_ERR
"init_module: mod->can_unload out of bounds.\n");
267 /* Check that the user isn't doing something silly with the name. */
269 if ((n_namelen
= get_mod_name(mod
->name
- (unsigned long)mod
270 + (unsigned long)mod_user
,
275 if (namelen
!= n_namelen
|| strcmp(n_name
, mod_tmp
.name
) != 0) {
276 printk(KERN_ERR
"init_module: changed module name to "
278 n_name
, mod_tmp
.name
);
282 /* Ok, that's about all the sanity we can stomach; copy the rest. */
284 if (copy_from_user(mod
+1, mod_user
+1, mod
->size
-sizeof(*mod
))) {
289 /* On some machines it is necessary to do something here
290 to make the I and D caches consistent. */
291 flush_icache_range((unsigned long)mod
, (unsigned long)mod
+ mod
->size
);
293 /* Update module references. */
294 mod
->next
= mod_tmp
.next
;
296 for (i
= 0, dep
= mod
->deps
; i
< mod
->ndeps
; ++i
, ++dep
) {
297 struct module
*o
, *d
= dep
->dep
;
299 /* Make sure the indicated dependencies are really modules. */
301 printk(KERN_ERR
"init_module: self-referential "
302 "dependency in mod->deps.\n");
306 for (o
= module_list
; o
!= &kernel_module
; o
= o
->next
)
307 if (o
== d
) goto found_dep
;
309 printk(KERN_ERR
"init_module: found dependency that is "
310 "(no longer?) a module.\n");
315 dep
->next_ref
= d
->refs
;
317 /* Being referenced by a dependent module counts as a
318 use as far as kmod is concerned. */
319 d
->flags
|= MOD_USED_ONCE
;
322 /* Free our temporary memory. */
323 put_mod_name(n_name
);
326 /* Initialize the module. */
327 mod
->flags
|= MOD_INITIALIZING
;
328 atomic_set(&mod
->uc
.usecount
,1);
329 if (mod
->init
&& mod
->init() != 0) {
330 atomic_set(&mod
->uc
.usecount
,0);
331 mod
->flags
&= ~MOD_INITIALIZING
;
335 atomic_dec(&mod
->uc
.usecount
);
337 /* And set it running. */
338 mod
->flags
= (mod
->flags
| MOD_RUNNING
) & ~MOD_INITIALIZING
;
343 put_mod_name(n_name
);
353 static spinlock_t unload_lock
= SPIN_LOCK_UNLOCKED
;
354 int try_inc_mod_count(struct module
*mod
)
358 spin_lock(&unload_lock
);
359 if (mod
->flags
& MOD_DELETED
)
362 __MOD_INC_USE_COUNT(mod
);
363 spin_unlock(&unload_lock
);
369 sys_delete_module(const char *name_user
)
371 struct module
*mod
, *next
;
374 int something_changed
;
376 if (!capable(CAP_SYS_MODULE
))
381 if ((error
= get_mod_name(name_user
, &name
)) < 0)
389 if ((mod
= find_module(name
)) == NULL
) {
395 if (mod
->refs
!= NULL
)
398 spin_lock(&unload_lock
);
399 if (!__MOD_IN_USE(mod
)) {
400 mod
->flags
|= MOD_DELETED
;
401 spin_unlock(&unload_lock
);
405 spin_unlock(&unload_lock
);
410 /* Do automatic reaping */
412 something_changed
= 0;
413 for (mod
= module_list
; mod
!= &kernel_module
; mod
= next
) {
415 spin_lock(&unload_lock
);
416 if (mod
->refs
== NULL
417 && (mod
->flags
& MOD_AUTOCLEAN
)
418 && (mod
->flags
& MOD_RUNNING
)
419 && !(mod
->flags
& MOD_DELETED
)
420 && (mod
->flags
& MOD_USED_ONCE
)
421 && !__MOD_IN_USE(mod
)) {
422 if ((mod
->flags
& MOD_VISITED
)
423 && !(mod
->flags
& MOD_JUST_FREED
)) {
424 spin_unlock(&unload_lock
);
425 mod
->flags
&= ~MOD_VISITED
;
427 mod
->flags
|= MOD_DELETED
;
428 spin_unlock(&unload_lock
);
430 something_changed
= 1;
433 spin_unlock(&unload_lock
);
436 if (something_changed
)
438 for (mod
= module_list
; mod
!= &kernel_module
; mod
= mod
->next
)
439 mod
->flags
&= ~MOD_JUST_FREED
;
446 /* Query various bits about modules. */
449 qm_modules(char *buf
, size_t bufsize
, size_t *ret
)
452 size_t nmod
, space
, len
;
456 for (mod
=module_list
; mod
!= &kernel_module
; mod
=mod
->next
, ++nmod
) {
457 len
= strlen(mod
->name
)+1;
459 goto calc_space_needed
;
460 if (copy_to_user(buf
, mod
->name
, len
))
467 if (put_user(nmod
, ret
))
474 while ((mod
= mod
->next
) != &kernel_module
)
475 space
+= strlen(mod
->name
)+1;
477 if (put_user(space
, ret
))
484 qm_deps(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
486 size_t i
, space
, len
;
488 if (mod
== &kernel_module
)
490 if (!MOD_CAN_QUERY(mod
))
491 if (put_user(0, ret
))
497 for (i
= 0; i
< mod
->ndeps
; ++i
) {
498 const char *dep_name
= mod
->deps
[i
].dep
->name
;
500 len
= strlen(dep_name
)+1;
502 goto calc_space_needed
;
503 if (copy_to_user(buf
, dep_name
, len
))
510 if (put_user(i
, ret
))
517 while (++i
< mod
->ndeps
)
518 space
+= strlen(mod
->deps
[i
].dep
->name
)+1;
520 if (put_user(space
, ret
))
527 qm_refs(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
529 size_t nrefs
, space
, len
;
530 struct module_ref
*ref
;
532 if (mod
== &kernel_module
)
534 if (!MOD_CAN_QUERY(mod
))
535 if (put_user(0, ret
))
541 for (nrefs
= 0, ref
= mod
->refs
; ref
; ++nrefs
, ref
= ref
->next_ref
) {
542 const char *ref_name
= ref
->ref
->name
;
544 len
= strlen(ref_name
)+1;
546 goto calc_space_needed
;
547 if (copy_to_user(buf
, ref_name
, len
))
554 if (put_user(nrefs
, ret
))
561 while ((ref
= ref
->next_ref
) != NULL
)
562 space
+= strlen(ref
->ref
->name
)+1;
564 if (put_user(space
, ret
))
571 qm_symbols(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
573 size_t i
, space
, len
;
574 struct module_symbol
*s
;
578 if (!MOD_CAN_QUERY(mod
))
579 if (put_user(0, ret
))
584 space
= mod
->nsyms
* 2*sizeof(void *);
590 goto calc_space_needed
;
592 if (!access_ok(VERIFY_WRITE
, buf
, space
))
596 vals
= (unsigned long *)buf
;
599 for (; i
< mod
->nsyms
; ++i
, ++s
, vals
+= 2) {
600 len
= strlen(s
->name
)+1;
602 goto calc_space_needed
;
604 if (copy_to_user(strings
, s
->name
, len
)
605 || __put_user(s
->value
, vals
+0)
606 || __put_user(space
, vals
+1))
614 if (put_user(i
, ret
))
620 for (; i
< mod
->nsyms
; ++i
, ++s
)
621 space
+= strlen(s
->name
)+1;
623 if (put_user(space
, ret
))
630 qm_info(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
634 if (mod
== &kernel_module
)
637 if (sizeof(struct module_info
) <= bufsize
) {
638 struct module_info info
;
639 info
.addr
= (unsigned long)mod
;
640 info
.size
= mod
->size
;
641 info
.flags
= mod
->flags
;
642 info
.usecount
= (mod_member_present(mod
, can_unload
)
643 && mod
->can_unload
? -1 : atomic_read(&mod
->uc
.usecount
));
645 if (copy_to_user(buf
, &info
, sizeof(struct module_info
)))
650 if (put_user(sizeof(struct module_info
), ret
))
657 sys_query_module(const char *name_user
, int which
, char *buf
, size_t bufsize
,
664 if (name_user
== NULL
)
665 mod
= &kernel_module
;
670 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
676 mod
= &kernel_module
;
677 else if ((mod
= find_module(name
)) == NULL
) {
690 err
= qm_modules(buf
, bufsize
, ret
);
693 err
= qm_deps(mod
, buf
, bufsize
, ret
);
696 err
= qm_refs(mod
, buf
, bufsize
, ret
);
699 err
= qm_symbols(mod
, buf
, bufsize
, ret
);
702 err
= qm_info(mod
, buf
, bufsize
, ret
);
714 * Copy the kernel symbol table to user space. If the argument is
715 * NULL, just return the size of the table.
717 * This call is obsolete. New programs should use query_module+QM_SYMBOLS
718 * which does not arbitrarily limit the length of symbols.
722 sys_get_kernel_syms(struct kernel_sym
*table
)
726 struct kernel_sym ksym
;
729 for (mod
= module_list
, i
= 0; mod
; mod
= mod
->next
) {
730 /* include the count for the module name! */
737 /* So that we don't give the user our stack content */
738 memset (&ksym
, 0, sizeof (ksym
));
740 for (mod
= module_list
, i
= 0; mod
; mod
= mod
->next
) {
741 struct module_symbol
*msym
;
744 if (!MOD_CAN_QUERY(mod
))
747 /* magic: write module info as a pseudo symbol */
748 ksym
.value
= (unsigned long)mod
;
750 strncpy(ksym
.name
+1, mod
->name
, sizeof(ksym
.name
)-1);
751 ksym
.name
[sizeof(ksym
.name
)-1] = '\0';
753 if (copy_to_user(table
, &ksym
, sizeof(ksym
)) != 0)
760 for (j
= 0, msym
= mod
->syms
; j
< mod
->nsyms
; ++j
, ++msym
) {
761 ksym
.value
= msym
->value
;
762 strncpy(ksym
.name
, msym
->name
, sizeof(ksym
.name
));
763 ksym
.name
[sizeof(ksym
.name
)-1] = '\0';
765 if (copy_to_user(table
, &ksym
, sizeof(ksym
)) != 0)
776 * Look for a module by name, ignoring modules marked for deletion.
779 static struct module
*
780 find_module(const char *name
)
784 for (mod
= module_list
; mod
; mod
= mod
->next
) {
785 if (mod
->flags
& MOD_DELETED
)
787 if (!strcmp(mod
->name
, name
))
795 * Free the given module.
799 free_module(struct module
*mod
, int tag_freed
)
801 struct module_ref
*dep
;
804 /* Let the module clean up. */
806 if (mod
->flags
& MOD_RUNNING
)
810 mod
->flags
&= ~MOD_RUNNING
;
813 /* Remove the module from the dependency lists. */
815 for (i
= 0, dep
= mod
->deps
; i
< mod
->ndeps
; ++i
, ++dep
) {
816 struct module_ref
**pp
;
817 for (pp
= &dep
->dep
->refs
; *pp
!= dep
; pp
= &(*pp
)->next_ref
)
820 if (tag_freed
&& dep
->dep
->refs
== NULL
)
821 dep
->dep
->flags
|= MOD_JUST_FREED
;
824 /* And from the main module list. */
826 if (mod
== module_list
) {
827 module_list
= mod
->next
;
830 for (p
= module_list
; p
->next
!= mod
; p
= p
->next
)
835 /* And free the memory. */
841 * Called by the /proc file system to return a current list of modules.
844 int get_module_list(char *p
)
846 size_t left
= PAGE_SIZE
;
849 struct module_ref
*ref
;
851 for (mod
= module_list
; mod
!= &kernel_module
; mod
= mod
->next
) {
855 #define safe_copy_str(str, len) \
859 memcpy(p, str, len); p += len, left -= len; \
861 #define safe_copy_cstr(str) safe_copy_str(str, sizeof(str)-1)
863 len
= strlen(mod
->name
);
864 safe_copy_str(mod
->name
, len
);
866 if ((len
= 20 - len
) > 0) {
874 len
= sprintf(tmpstr
, "%8lu", mod
->size
);
875 safe_copy_str(tmpstr
, len
);
877 if (mod
->flags
& MOD_RUNNING
) {
878 len
= sprintf(tmpstr
, "%4ld",
879 (mod_member_present(mod
, can_unload
)
881 ? -1L : (long)atomic_read(&mod
->uc
.usecount
)));
882 safe_copy_str(tmpstr
, len
);
885 if (mod
->flags
& MOD_DELETED
)
886 safe_copy_cstr(" (deleted)");
887 else if (mod
->flags
& MOD_RUNNING
) {
888 if (mod
->flags
& MOD_AUTOCLEAN
)
889 safe_copy_cstr(" (autoclean)");
890 if (!(mod
->flags
& MOD_USED_ONCE
))
891 safe_copy_cstr(" (unused)");
893 else if (mod
->flags
& MOD_INITIALIZING
)
894 safe_copy_cstr(" (initializing)");
896 safe_copy_cstr(" (uninitialized)");
898 if ((ref
= mod
->refs
) != NULL
) {
899 safe_copy_cstr(" [");
903 safe_copy_str(q
, len
);
905 if ((ref
= ref
->next_ref
) != NULL
)
912 safe_copy_cstr("\n");
915 #undef safe_copy_cstr
919 return PAGE_SIZE
- left
;
923 * Called by the /proc file system to return a current list of ksyms.
927 get_ksyms_list(char *buf
, char **start
, off_t offset
, int length
)
931 int len
= 0; /* code from net/ipv4/proc.c */
935 for (mod
= module_list
; mod
; mod
= mod
->next
) {
937 struct module_symbol
*sym
;
939 if (!MOD_CAN_QUERY(mod
))
942 for (i
= mod
->nsyms
, sym
= mod
->syms
; i
> 0; --i
, ++sym
) {
945 len
+= sprintf(p
, "%0*lx %s\t[%s]\n",
946 (int)(2*sizeof(void*)),
947 sym
->value
, sym
->name
,
950 len
+= sprintf(p
, "%0*lx %s\n",
951 (int)(2*sizeof(void*)),
952 sym
->value
, sym
->name
);
960 if (pos
> offset
+length
)
965 *start
= buf
+ (offset
- begin
);
966 len
-= (offset
- begin
);
973 * Gets the address for a symbol in the given module. If modname is
974 * NULL, it looks for the name in any registered symbol table. If the
975 * modname is an empty string, it looks for the symbol in kernel exported
976 * symbol tables. Increase the usage count of the module in which the
977 * symbol was found - it's the only way we can guarantee that it's still
978 * there by the time our caller actually uses it.
981 get_module_symbol(char *modname
, char *symname
)
984 struct module_symbol
*sym
;
987 spin_lock(&unload_lock
);
988 for (mp
= module_list
; mp
; mp
= mp
->next
) {
989 if (((modname
== NULL
) || (strcmp(mp
->name
, modname
) == 0)) &&
992 for (i
= mp
->nsyms
, sym
= mp
->syms
;
995 if (strcmp(sym
->name
, symname
) == 0) {
996 __MOD_INC_USE_COUNT(mp
);
997 spin_unlock(&unload_lock
);
1003 spin_unlock(&unload_lock
);
1007 /* Decrease the use count of the module containing a symbol with the
1010 void put_module_symbol(unsigned long addr
)
1014 for (mp
= module_list
; mp
; mp
= mp
->next
) {
1015 if (MOD_CAN_QUERY(mp
) &&
1016 addr
>= (unsigned long)mp
&&
1017 addr
< (unsigned long)mp
+ mp
->size
) {
1018 __MOD_DEC_USE_COUNT(mp
);
1024 #else /* CONFIG_MODULES */
1026 /* Dummy syscalls for people who don't want modules */
1028 asmlinkage
unsigned long
1029 sys_create_module(const char *name_user
, size_t size
)
1035 sys_init_module(const char *name_user
, struct module
*mod_user
)
1041 sys_delete_module(const char *name_user
)
1047 sys_query_module(const char *name_user
, int which
, char *buf
, size_t bufsize
,
1050 /* Let the program know about the new interface. Not that
1051 it'll do them much good. */
1059 sys_get_kernel_syms(struct kernel_sym
*table
)
1064 int try_inc_mod_count(struct module
*mod
)
1069 #endif /* CONFIG_MODULES */