Linux-2.4.0-test2
[davej-history.git] / kernel / module.c
blobc0c5c9053fa5ddcdea487ccc3f469d6e081fc397
1 #include <linux/config.h>
2 #include <linux/mm.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 */
32 NULL, /* next */
33 "", /* name */
34 0, /* size */
35 {ATOMIC_INIT(1)}, /* usecount */
36 MOD_RUNNING, /* flags */
37 0, /* nsyms -- to filled in in init_modules */
38 0, /* ndeps */
39 __start___ksymtab, /* syms */
40 NULL, /* deps */
41 NULL, /* refs */
42 NULL, /* init */
43 NULL, /* cleanup */
44 __start___ex_table, /* ex_table_start */
45 __stop___ex_table, /* ex_table_end */
46 /* Rest are NULL */
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);
58 * Called at boot time
61 void __init init_modules(void)
63 kernel_module.nsyms = __stop___ksymtab - __start___ksymtab;
65 #ifdef __alpha__
66 __asm__("stq $29,%0" : "=m"(kernel_module.gp));
67 #endif
71 * Copy the name of a module from user space.
74 static inline long
75 get_mod_name(const char *user_name, char **buf)
77 unsigned long page;
78 long retval;
80 page = __get_free_page(GFP_KERNEL);
81 if (!page)
82 return -ENOMEM;
84 retval = strncpy_from_user((char *)page, user_name, PAGE_SIZE);
85 if (retval > 0) {
86 if (retval < PAGE_SIZE) {
87 *buf = (char *)page;
88 return retval;
90 retval = -ENAMETOOLONG;
91 } else if (!retval)
92 retval = -EINVAL;
94 free_page(page);
95 return retval;
98 static inline void
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)
111 char *name;
112 long namelen, error;
113 struct module *mod;
115 if (!capable(CAP_SYS_MODULE))
116 return -EPERM;
117 lock_kernel();
118 if ((namelen = get_mod_name(name_user, &name)) < 0) {
119 error = namelen;
120 goto err0;
122 if (size < sizeof(struct module)+namelen) {
123 error = -EINVAL;
124 goto err1;
126 if (find_module(name) != NULL) {
127 error = -EEXIST;
128 goto err1;
130 if ((mod = (struct module *)module_map(size)) == NULL) {
131 error = -ENOMEM;
132 goto err1;
135 memset(mod, 0, sizeof(*mod));
136 mod->size_of_struct = sizeof(*mod);
137 mod->next = module_list;
138 mod->name = (char *)(mod + 1);
139 mod->size = size;
140 memcpy((char*)(mod+1), name, namelen+1);
142 put_mod_name(name);
144 module_list = mod; /* link it in */
146 error = (long) mod;
147 goto err0;
148 err1:
149 put_mod_name(name);
150 err0:
151 unlock_kernel();
152 return error;
156 * Initialize a module.
159 asmlinkage long
160 sys_init_module(const char *name_user, struct module *mod_user)
162 struct module mod_tmp, *mod;
163 char *name, *n_name;
164 long namelen, n_namelen, i, error;
165 unsigned long mod_user_size;
166 struct module_ref *dep;
168 if (!capable(CAP_SYS_MODULE))
169 return -EPERM;
170 lock_kernel();
171 if ((namelen = get_mod_name(name_user, &name)) < 0) {
172 error = namelen;
173 goto err0;
175 if ((mod = find_module(name)) == NULL) {
176 error = -ENOENT;
177 goto err1;
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)
184 goto err1;
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 "
189 "needed.\n");
190 error = -EINVAL;
191 goto err1;
194 /* Hold the current contents while we play with the user's idea
195 of righteousness. */
196 mod_tmp = *mod;
198 error = copy_from_user(mod, mod_user, sizeof(struct module));
199 if (error) {
200 error = -EFAULT;
201 goto err2;
204 /* Sanity check the size of the module. */
205 error = -EINVAL;
207 if (mod->size > mod_tmp.size) {
208 printk(KERN_ERR "init_module: Size of initialized module "
209 "exceeds size of created module.\n");
210 goto err2;
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");
220 goto err2;
222 if (mod->nsyms && !bound(mod->syms, mod->nsyms, mod)) {
223 printk(KERN_ERR "init_module: mod->syms out of bounds.\n");
224 goto err2;
226 if (mod->ndeps && !bound(mod->deps, mod->ndeps, mod)) {
227 printk(KERN_ERR "init_module: mod->deps out of bounds.\n");
228 goto err2;
230 if (mod->init && !bound(mod->init, 0, mod)) {
231 printk(KERN_ERR "init_module: mod->init out of bounds.\n");
232 goto err2;
234 if (mod->cleanup && !bound(mod->cleanup, 0, mod)) {
235 printk(KERN_ERR "init_module: mod->cleanup out of bounds.\n");
236 goto err2;
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");
247 goto err2;
249 if (mod->flags & ~MOD_AUTOCLEAN) {
250 printk(KERN_ERR "init_module: mod->flags invalid.\n");
251 goto err2;
253 #ifdef __alpha__
254 if (!bound(mod->gp - 0x8000, 0, mod)) {
255 printk(KERN_ERR "init_module: mod->gp out of bounds.\n");
256 goto err2;
258 #endif
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");
262 goto err2;
265 #undef bound
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,
271 &n_name)) < 0) {
272 error = n_namelen;
273 goto err2;
275 if (namelen != n_namelen || strcmp(n_name, mod_tmp.name) != 0) {
276 printk(KERN_ERR "init_module: changed module name to "
277 "`%s' from `%s'\n",
278 n_name, mod_tmp.name);
279 goto err3;
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))) {
285 error = -EFAULT;
286 goto err3;
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;
295 mod->refs = NULL;
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. */
300 if (d == mod) {
301 printk(KERN_ERR "init_module: self-referential "
302 "dependency in mod->deps.\n");
303 goto err3;
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");
311 goto err3;
313 found_dep:
314 dep->ref = mod;
315 dep->next_ref = d->refs;
316 d->refs = dep;
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);
324 put_mod_name(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;
332 error = -EBUSY;
333 goto err0;
335 atomic_dec(&mod->uc.usecount);
337 /* And set it running. */
338 mod->flags = (mod->flags | MOD_RUNNING) & ~MOD_INITIALIZING;
339 error = 0;
340 goto err0;
342 err3:
343 put_mod_name(n_name);
344 err2:
345 *mod = mod_tmp;
346 err1:
347 put_mod_name(name);
348 err0:
349 unlock_kernel();
350 return error;
353 static spinlock_t unload_lock = SPIN_LOCK_UNLOCKED;
354 int try_inc_mod_count(struct module *mod)
356 int res = 1;
357 if (mod) {
358 spin_lock(&unload_lock);
359 if (mod->flags & MOD_DELETED)
360 res = 0;
361 else
362 __MOD_INC_USE_COUNT(mod);
363 spin_unlock(&unload_lock);
365 return res;
368 asmlinkage long
369 sys_delete_module(const char *name_user)
371 struct module *mod, *next;
372 char *name;
373 long error;
374 int something_changed;
376 if (!capable(CAP_SYS_MODULE))
377 return -EPERM;
379 lock_kernel();
380 if (name_user) {
381 if ((error = get_mod_name(name_user, &name)) < 0)
382 goto out;
383 if (error == 0) {
384 error = -EINVAL;
385 put_mod_name(name);
386 goto out;
388 error = -ENOENT;
389 if ((mod = find_module(name)) == NULL) {
390 put_mod_name(name);
391 goto out;
393 put_mod_name(name);
394 error = -EBUSY;
395 if (mod->refs != NULL)
396 goto out;
398 spin_lock(&unload_lock);
399 if (!__MOD_IN_USE(mod)) {
400 mod->flags |= MOD_DELETED;
401 spin_unlock(&unload_lock);
402 free_module(mod, 0);
403 error = 0;
404 } else {
405 spin_unlock(&unload_lock);
407 goto out;
410 /* Do automatic reaping */
411 restart:
412 something_changed = 0;
413 for (mod = module_list; mod != &kernel_module; mod = next) {
414 next = 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;
426 } else {
427 mod->flags |= MOD_DELETED;
428 spin_unlock(&unload_lock);
429 free_module(mod, 1);
430 something_changed = 1;
432 } else {
433 spin_unlock(&unload_lock);
436 if (something_changed)
437 goto restart;
438 for (mod = module_list; mod != &kernel_module; mod = mod->next)
439 mod->flags &= ~MOD_JUST_FREED;
440 error = 0;
441 out:
442 unlock_kernel();
443 return error;
446 /* Query various bits about modules. */
448 static int
449 qm_modules(char *buf, size_t bufsize, size_t *ret)
451 struct module *mod;
452 size_t nmod, space, len;
454 nmod = space = 0;
456 for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
457 len = strlen(mod->name)+1;
458 if (len > bufsize)
459 goto calc_space_needed;
460 if (copy_to_user(buf, mod->name, len))
461 return -EFAULT;
462 buf += len;
463 bufsize -= len;
464 space += len;
467 if (put_user(nmod, ret))
468 return -EFAULT;
469 else
470 return 0;
472 calc_space_needed:
473 space += len;
474 while ((mod = mod->next) != &kernel_module)
475 space += strlen(mod->name)+1;
477 if (put_user(space, ret))
478 return -EFAULT;
479 else
480 return -ENOSPC;
483 static int
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)
489 return -EINVAL;
490 if (!MOD_CAN_QUERY(mod))
491 if (put_user(0, ret))
492 return -EFAULT;
493 else
494 return 0;
496 space = 0;
497 for (i = 0; i < mod->ndeps; ++i) {
498 const char *dep_name = mod->deps[i].dep->name;
500 len = strlen(dep_name)+1;
501 if (len > bufsize)
502 goto calc_space_needed;
503 if (copy_to_user(buf, dep_name, len))
504 return -EFAULT;
505 buf += len;
506 bufsize -= len;
507 space += len;
510 if (put_user(i, ret))
511 return -EFAULT;
512 else
513 return 0;
515 calc_space_needed:
516 space += len;
517 while (++i < mod->ndeps)
518 space += strlen(mod->deps[i].dep->name)+1;
520 if (put_user(space, ret))
521 return -EFAULT;
522 else
523 return -ENOSPC;
526 static int
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)
533 return -EINVAL;
534 if (!MOD_CAN_QUERY(mod))
535 if (put_user(0, ret))
536 return -EFAULT;
537 else
538 return 0;
540 space = 0;
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;
545 if (len > bufsize)
546 goto calc_space_needed;
547 if (copy_to_user(buf, ref_name, len))
548 return -EFAULT;
549 buf += len;
550 bufsize -= len;
551 space += len;
554 if (put_user(nrefs, ret))
555 return -EFAULT;
556 else
557 return 0;
559 calc_space_needed:
560 space += len;
561 while ((ref = ref->next_ref) != NULL)
562 space += strlen(ref->ref->name)+1;
564 if (put_user(space, ret))
565 return -EFAULT;
566 else
567 return -ENOSPC;
570 static int
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;
575 char *strings;
576 unsigned long *vals;
578 if (!MOD_CAN_QUERY(mod))
579 if (put_user(0, ret))
580 return -EFAULT;
581 else
582 return 0;
584 space = mod->nsyms * 2*sizeof(void *);
586 i = len = 0;
587 s = mod->syms;
589 if (space > bufsize)
590 goto calc_space_needed;
592 if (!access_ok(VERIFY_WRITE, buf, space))
593 return -EFAULT;
595 bufsize -= space;
596 vals = (unsigned long *)buf;
597 strings = buf+space;
599 for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
600 len = strlen(s->name)+1;
601 if (len > bufsize)
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))
607 return -EFAULT;
609 strings += len;
610 bufsize -= len;
611 space += len;
614 if (put_user(i, ret))
615 return -EFAULT;
616 else
617 return 0;
619 calc_space_needed:
620 for (; i < mod->nsyms; ++i, ++s)
621 space += strlen(s->name)+1;
623 if (put_user(space, ret))
624 return -EFAULT;
625 else
626 return -ENOSPC;
629 static int
630 qm_info(struct module *mod, char *buf, size_t bufsize, size_t *ret)
632 int error = 0;
634 if (mod == &kernel_module)
635 return -EINVAL;
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)))
646 return -EFAULT;
647 } else
648 error = -ENOSPC;
650 if (put_user(sizeof(struct module_info), ret))
651 return -EFAULT;
653 return error;
656 asmlinkage long
657 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
658 size_t *ret)
660 struct module *mod;
661 int err;
663 lock_kernel();
664 if (name_user == NULL)
665 mod = &kernel_module;
666 else {
667 long namelen;
668 char *name;
670 if ((namelen = get_mod_name(name_user, &name)) < 0) {
671 err = namelen;
672 goto out;
674 err = -ENOENT;
675 if (namelen == 0)
676 mod = &kernel_module;
677 else if ((mod = find_module(name)) == NULL) {
678 put_mod_name(name);
679 goto out;
681 put_mod_name(name);
684 switch (which)
686 case 0:
687 err = 0;
688 break;
689 case QM_MODULES:
690 err = qm_modules(buf, bufsize, ret);
691 break;
692 case QM_DEPS:
693 err = qm_deps(mod, buf, bufsize, ret);
694 break;
695 case QM_REFS:
696 err = qm_refs(mod, buf, bufsize, ret);
697 break;
698 case QM_SYMBOLS:
699 err = qm_symbols(mod, buf, bufsize, ret);
700 break;
701 case QM_INFO:
702 err = qm_info(mod, buf, bufsize, ret);
703 break;
704 default:
705 err = -EINVAL;
706 break;
708 out:
709 unlock_kernel();
710 return err;
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.
721 asmlinkage long
722 sys_get_kernel_syms(struct kernel_sym *table)
724 struct module *mod;
725 int i;
726 struct kernel_sym ksym;
728 lock_kernel();
729 for (mod = module_list, i = 0; mod; mod = mod->next) {
730 /* include the count for the module name! */
731 i += mod->nsyms + 1;
734 if (table == NULL)
735 goto out;
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;
742 unsigned int j;
744 if (!MOD_CAN_QUERY(mod))
745 continue;
747 /* magic: write module info as a pseudo symbol */
748 ksym.value = (unsigned long)mod;
749 ksym.name[0] = '#';
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)
754 goto out;
755 ++i, ++table;
757 if (mod->nsyms == 0)
758 continue;
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)
766 goto out;
767 ++i, ++table;
770 out:
771 unlock_kernel();
772 return i;
776 * Look for a module by name, ignoring modules marked for deletion.
779 static struct module *
780 find_module(const char *name)
782 struct module *mod;
784 for (mod = module_list; mod ; mod = mod->next) {
785 if (mod->flags & MOD_DELETED)
786 continue;
787 if (!strcmp(mod->name, name))
788 break;
791 return mod;
795 * Free the given module.
798 static void
799 free_module(struct module *mod, int tag_freed)
801 struct module_ref *dep;
802 unsigned i;
804 /* Let the module clean up. */
806 if (mod->flags & MOD_RUNNING)
808 if(mod->cleanup)
809 mod->cleanup();
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)
818 continue;
819 *pp = dep->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;
828 } else {
829 struct module *p;
830 for (p = module_list; p->next != mod; p = p->next)
831 continue;
832 p->next = mod->next;
835 /* And free the memory. */
837 module_unmap(mod);
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;
847 struct module *mod;
848 char tmpstr[64];
849 struct module_ref *ref;
851 for (mod = module_list; mod != &kernel_module; mod = mod->next) {
852 long len;
853 const char *q;
855 #define safe_copy_str(str, len) \
856 do { \
857 if (left < len) \
858 goto fini; \
859 memcpy(p, str, len); p += len, left -= len; \
860 } while (0)
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) {
867 if (left < len)
868 goto fini;
869 memset(p, ' ', len);
870 p += len;
871 left -= len;
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)
880 && 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)");
895 else
896 safe_copy_cstr(" (uninitialized)");
898 if ((ref = mod->refs) != NULL) {
899 safe_copy_cstr(" [");
900 while (1) {
901 q = ref->ref->name;
902 len = strlen(q);
903 safe_copy_str(q, len);
905 if ((ref = ref->next_ref) != NULL)
906 safe_copy_cstr(" ");
907 else
908 break;
910 safe_copy_cstr("]");
912 safe_copy_cstr("\n");
914 #undef safe_copy_str
915 #undef safe_copy_cstr
918 fini:
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)
929 struct module *mod;
930 char *p = buf;
931 int len = 0; /* code from net/ipv4/proc.c */
932 off_t pos = 0;
933 off_t begin = 0;
935 for (mod = module_list; mod; mod = mod->next) {
936 unsigned i;
937 struct module_symbol *sym;
939 if (!MOD_CAN_QUERY(mod))
940 continue;
942 for (i = mod->nsyms, sym = mod->syms; i > 0; --i, ++sym) {
943 p = buf + len;
944 if (*mod->name) {
945 len += sprintf(p, "%0*lx %s\t[%s]\n",
946 (int)(2*sizeof(void*)),
947 sym->value, sym->name,
948 mod->name);
949 } else {
950 len += sprintf(p, "%0*lx %s\n",
951 (int)(2*sizeof(void*)),
952 sym->value, sym->name);
954 pos = begin + len;
955 if (pos < offset) {
956 len = 0;
957 begin = pos;
959 pos = begin + len;
960 if (pos > offset+length)
961 goto leave_the_loop;
964 leave_the_loop:
965 *start = buf + (offset - begin);
966 len -= (offset - begin);
967 if (len > length)
968 len = length;
969 return len;
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.
980 unsigned long
981 get_module_symbol(char *modname, char *symname)
983 struct module *mp;
984 struct module_symbol *sym;
985 int i;
987 spin_lock(&unload_lock);
988 for (mp = module_list; mp; mp = mp->next) {
989 if (((modname == NULL) || (strcmp(mp->name, modname) == 0)) &&
990 MOD_CAN_QUERY(mp) &&
991 (mp->nsyms > 0)) {
992 for (i = mp->nsyms, sym = mp->syms;
993 i > 0; --i, ++sym) {
995 if (strcmp(sym->name, symname) == 0) {
996 __MOD_INC_USE_COUNT(mp);
997 spin_unlock(&unload_lock);
998 return sym->value;
1003 spin_unlock(&unload_lock);
1004 return 0;
1007 /* Decrease the use count of the module containing a symbol with the
1008 * address passed.
1010 void put_module_symbol(unsigned long addr)
1012 struct module *mp;
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);
1019 return;
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)
1031 return -ENOSYS;
1034 asmlinkage long
1035 sys_init_module(const char *name_user, struct module *mod_user)
1037 return -ENOSYS;
1040 asmlinkage long
1041 sys_delete_module(const char *name_user)
1043 return -ENOSYS;
1046 asmlinkage long
1047 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
1048 size_t *ret)
1050 /* Let the program know about the new interface. Not that
1051 it'll do them much good. */
1052 if (which == 0)
1053 return 0;
1055 return -ENOSYS;
1058 asmlinkage long
1059 sys_get_kernel_syms(struct kernel_sym *table)
1061 return -ENOSYS;
1064 int try_inc_mod_count(struct module *mod)
1066 return 1;
1069 #endif /* CONFIG_MODULES */