kern_module.c revision 1.14 1 /* $NetBSD: kern_module.c,v 1.14 2008/05/04 21:35:12 rumble Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Kernel module support.
31 *
32 * XXX Deps for loadable modules don't work, because we must load the
33 * module in order to find out which modules it requires. Linking may
34 * fail because of missing symbols.
35 */
36
37 #include "opt_modular.h"
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.14 2008/05/04 21:35:12 rumble Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/proc.h>
46 #include <sys/kauth.h>
47 #include <sys/kobj.h>
48 #include <sys/kmem.h>
49 #include <sys/module.h>
50 #include <sys/syscall.h>
51 #include <sys/syscallargs.h>
52
53 #include <uvm/uvm_extern.h>
54
55 #include <machine/stdarg.h>
56
57 #ifndef LKM /* XXX */
58 struct vm_map *lkm_map;
59 #endif
60
61 struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
62 struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
63 static module_t *module_active;
64 u_int module_count;
65 kmutex_t module_lock;
66
67 /* Ensure that the kernel's link set isn't empty. */
68 static modinfo_t module_dummy;
69 __link_set_add_rodata(modules, module_dummy);
70
71 static module_t *module_lookup(const char *);
72 static int module_do_load(const char *, bool, int, prop_dictionary_t,
73 module_t **);
74 static int module_do_unload(const char *);
75 static void module_error(const char *, ...);
76 static int module_do_builtin(const char *, module_t **);
77 static int module_fetch_info(module_t *);
78
79 /*
80 * module_error:
81 *
82 * Utility function: log an error.
83 */
84 static void
85 module_error(const char *fmt, ...)
86 {
87 va_list ap;
88
89 va_start(ap, fmt);
90 printf("WARNING: module error: ");
91 vprintf(fmt, ap);
92 printf("\n");
93 va_end(ap);
94 }
95
96 /*
97 * module_init:
98 *
99 * Initialize the module subsystem.
100 */
101 void
102 module_init(void)
103 {
104 extern struct vm_map *lkm_map;
105
106 if (lkm_map == NULL)
107 lkm_map = kernel_map;
108 mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
109 #ifdef MODULAR /* XXX */
110 module_init_md();
111 #endif
112 }
113
114 /*
115 * module_init_class:
116 *
117 * Initialize all built-in and pre-loaded modules of the
118 * specified class.
119 */
120 void
121 module_init_class(modclass_t class)
122 {
123 __link_set_decl(modules, modinfo_t);
124 modinfo_t *const *mip, *mi;
125 module_t *mod;
126
127 mutex_enter(&module_lock);
128 /*
129 * Builtins first. These can't depend on pre-loaded modules.
130 */
131 __link_set_foreach(mip, modules) {
132 mi = *mip;
133 if (mi == &module_dummy) {
134 continue;
135 }
136 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
137 continue;
138 }
139 (void)module_do_builtin(mi->mi_name, NULL);
140 }
141 /*
142 * Now preloaded modules. These will be pulled off the
143 * list as we call module_do_load();
144 */
145 do {
146 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
147 mi = mod->mod_info;
148 if (class != MODULE_CLASS_ANY &&
149 class != mi->mi_class)
150 continue;
151 module_do_load(mi->mi_name, false, 0, NULL, NULL);
152 break;
153 }
154 } while (mod != NULL);
155 mutex_exit(&module_lock);
156 }
157
158 /*
159 * module_jettison:
160 *
161 * Return memory used by pre-loaded modules to the freelist.
162 */
163 void
164 module_jettison(void)
165 {
166
167 /* XXX nothing yet */
168 }
169
170 /*
171 * module_load:
172 *
173 * Load a single module from the file system.
174 */
175 int
176 module_load(const char *filename, int flags, prop_dictionary_t props)
177 {
178 int error;
179
180 mutex_enter(&module_lock);
181 error = module_do_load(filename, false, flags, props, NULL);
182 mutex_exit(&module_lock);
183
184 return error;
185 }
186
187 /*
188 * module_unload:
189 *
190 * Find and unload a module by name.
191 */
192 int
193 module_unload(const char *name)
194 {
195 int error;
196
197 mutex_enter(&module_lock);
198 error = module_do_unload(name);
199 mutex_exit(&module_lock);
200
201 return error;
202 }
203
204 /*
205 * module_lookup:
206 *
207 * Look up a module by name.
208 */
209 module_t *
210 module_lookup(const char *name)
211 {
212 module_t *mod;
213
214 KASSERT(mutex_owned(&module_lock));
215
216 TAILQ_FOREACH(mod, &module_list, mod_chain) {
217 if (strcmp(mod->mod_info->mi_name, name) == 0) {
218 break;
219 }
220 }
221
222 return mod;
223 }
224
225 /*
226 * module_hold:
227 *
228 * Add a single reference to a module. It's the caller's
229 * responsibility to ensure that the reference is dropped
230 * later.
231 */
232 int
233 module_hold(const char *name)
234 {
235 module_t *mod;
236
237 mutex_enter(&module_lock);
238 mod = module_lookup(name);
239 if (mod == NULL) {
240 mutex_exit(&module_lock);
241 return ENOENT;
242 }
243 mod->mod_refcnt++;
244 mutex_exit(&module_lock);
245
246 return 0;
247 }
248
249 /*
250 * module_rele:
251 *
252 * Release a reference acquired with module_hold().
253 */
254 void
255 module_rele(const char *name)
256 {
257 module_t *mod;
258
259 mutex_enter(&module_lock);
260 mod = module_lookup(name);
261 if (mod == NULL) {
262 mutex_exit(&module_lock);
263 panic("module_rele: gone");
264 }
265 mod->mod_refcnt--;
266 mutex_exit(&module_lock);
267 }
268
269 /*
270 * module_do_builtin:
271 *
272 * Initialize a single module from the list of modules that are
273 * built into the kernel (linked into the kernel image).
274 */
275 static int
276 module_do_builtin(const char *name, module_t **modp)
277 {
278 __link_set_decl(modules, modinfo_t);
279 modinfo_t *const *mip;
280 const char *p, *s;
281 char buf[MAXMODNAME];
282 modinfo_t *mi;
283 module_t *mod, *mod2;
284 size_t len;
285 int error, i;
286
287 KASSERT(mutex_owned(&module_lock));
288
289 /*
290 * Check to see if already loaded.
291 */
292 if ((mod = module_lookup(name)) != NULL) {
293 if (modp != NULL) {
294 *modp = mod;
295 }
296 return 0;
297 }
298
299 /*
300 * Search the list to see if we have a module by this name.
301 */
302 error = ENOENT;
303 __link_set_foreach(mip, modules) {
304 mi = *mip;
305 if (mi == &module_dummy) {
306 continue;
307 }
308 if (strcmp(mi->mi_name, name) == 0) {
309 error = 0;
310 break;
311 }
312 }
313 if (error != 0) {
314 return error;
315 }
316
317 /*
318 * Initialize pre-requisites.
319 */
320 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
321 if (mod == NULL) {
322 return ENOMEM;
323 }
324 if (modp != NULL) {
325 *modp = mod;
326 }
327 if (mi->mi_required != NULL) {
328 for (s = mi->mi_required; *s != '\0'; s = p) {
329 if (*s == ',')
330 s++;
331 p = s;
332 while (*p != '\0' && *p != ',')
333 p++;
334 len = min(p - s + 1, sizeof(buf));
335 strlcpy(buf, s, len);
336 if (buf[0] == '\0')
337 break;
338 if (mod->mod_nrequired == MAXMODDEPS - 1) {
339 module_error("too many required modules");
340 kmem_free(mod, sizeof(*mod));
341 return EINVAL;
342 }
343 error = module_do_builtin(buf, &mod2);
344 if (error != 0) {
345 kmem_free(mod, sizeof(*mod));
346 return error;
347 }
348 mod->mod_required[mod->mod_nrequired++] = mod2;
349 }
350 }
351
352 /*
353 * Try to initialize the module.
354 */
355 KASSERT(module_active == NULL);
356 module_active = mod;
357 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
358 module_active = NULL;
359 if (error != 0) {
360 module_error("builtin module `%s' "
361 "failed to init", mi->mi_name);
362 kmem_free(mod, sizeof(*mod));
363 return error;
364 }
365 mod->mod_info = mi;
366 mod->mod_source = MODULE_SOURCE_KERNEL;
367 module_count++;
368 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
369
370 /*
371 * If that worked, count dependencies.
372 */
373 for (i = 0; i < mod->mod_nrequired; i++) {
374 mod->mod_required[i]->mod_refcnt++;
375 }
376
377 return 0;
378 }
379
380 /*
381 * module_do_load:
382 *
383 * Helper routine: load a module from the file system, or one
384 * pushed by the boot loader.
385 */
386 static int
387 module_do_load(const char *filename, bool isdep, int flags,
388 prop_dictionary_t props, module_t **modp)
389 {
390 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
391 static int depth;
392 const int maxdepth = 6;
393 modinfo_t *mi;
394 module_t *mod, *mod2;
395 char buf[MAXMODNAME];
396 const char *s, *p;
397 int error;
398 size_t len;
399 u_int i;
400 bool closed = false;
401
402 KASSERT(mutex_owned(&module_lock));
403
404 error = 0;
405
406 /*
407 * Avoid recursing too far.
408 */
409 if (++depth > maxdepth) {
410 module_error("too many required modules");
411 depth--;
412 return EMLINK;
413 }
414
415 /*
416 * Load the module and link. Before going to the file system,
417 * scan the list of modules loaded by the boot loader. Just
418 * before init is started the list of modules loaded at boot
419 * will be purged. Before init is started we can assume that
420 * `filename' is a module name and not a path name.
421 */
422 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
423 if (strcmp(mod->mod_info->mi_name, filename) == 0) {
424 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
425 break;
426 }
427 }
428 if (mod != NULL) {
429 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
430 } else {
431 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
432 if (mod == NULL) {
433 depth--;
434 return ENOMEM;
435 }
436 error = kobj_open_file(&mod->mod_kobj, filename);
437 if (error != 0) {
438 kmem_free(mod, sizeof(*mod));
439 depth--;
440 module_error("unable to open object file");
441 return error;
442 }
443 error = kobj_load(mod->mod_kobj);
444 if (error != 0) {
445 kobj_close(mod->mod_kobj);
446 kmem_free(mod, sizeof(*mod));
447 depth--;
448 module_error("unable to load kernel object");
449 return error;
450 }
451 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
452 mod->mod_source = MODULE_SOURCE_FILESYS;
453 error = module_fetch_info(mod);
454 if (error != 0) {
455 goto fail;
456 }
457 }
458
459 /*
460 * Check compatibility.
461 */
462 mi = mod->mod_info;
463 if (strlen(mi->mi_name) >= MAXMODNAME) {
464 error = EINVAL;
465 module_error("module name too long");
466 goto fail;
467 }
468
469 /*
470 * If loading a dependency, `filename' is a plain module name.
471 * The name must match.
472 */
473 if (isdep && strcmp(mi->mi_name, filename) != 0) {
474 error = ENOENT;
475 goto fail;
476 }
477
478 /*
479 * Check to see if the module is already loaded. If so, we may
480 * have been recursively called to handle a dependency, so be sure
481 * to set modp.
482 */
483 if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
484 if (modp != NULL)
485 *modp = mod2;
486 error = EEXIST;
487 goto fail;
488 }
489
490 /*
491 * Block circular dependencies.
492 */
493 TAILQ_FOREACH(mod2, &pending, mod_chain) {
494 if (mod == mod2) {
495 continue;
496 }
497 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
498 error = EDEADLK;
499 module_error("circular dependency detected");
500 goto fail;
501 }
502 }
503
504 /*
505 * Pass proper name to kobj. This will register the module
506 * with the ksyms framework.
507 */
508 error = kobj_set_name(mod->mod_kobj, mi->mi_name);
509 if (error != 0) {
510 module_error("unable to set name");
511 goto fail;
512 }
513
514 /*
515 * Close the kobj before handling dependencies since we're done
516 * with it and don't want to open an already locked file if a
517 * circular dependency exists.
518 */
519 kobj_close(mod->mod_kobj);
520 closed = true;
521
522 /*
523 * Now try to load any requisite modules.
524 */
525 if (mi->mi_required != NULL) {
526 for (s = mi->mi_required; *s != '\0'; s = p) {
527 if (*s == ',')
528 s++;
529 p = s;
530 while (*p != '\0' && *p != ',')
531 p++;
532 len = p - s + 1;
533 if (len >= MAXMODNAME) {
534 error = EINVAL;
535 module_error("required module name too long");
536 goto fail;
537 }
538 strlcpy(buf, s, len);
539 if (buf[0] == '\0')
540 break;
541 if (mod->mod_nrequired == MAXMODDEPS - 1) {
542 error = EINVAL;
543 module_error("too many required modules");
544 goto fail;
545 }
546 if (strcmp(buf, mi->mi_name) == 0) {
547 error = EDEADLK;
548 module_error("self-dependency detected");
549 goto fail;
550 }
551 error = module_do_load(buf, true, flags, NULL,
552 &mod->mod_required[mod->mod_nrequired++]);
553 if (error != 0 && error != EEXIST)
554 goto fail;
555 }
556 }
557
558 /*
559 * We loaded all needed modules successfully: initialize.
560 */
561 KASSERT(module_active == NULL);
562 module_active = mod;
563 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
564 module_active = NULL;
565 if (error != 0) {
566 module_error("modctl function returned error %d", error);
567 goto fail;
568 }
569
570 /*
571 * Good, the module loaded successfully. Put it onto the
572 * list and add references to its requisite modules.
573 */
574 module_count++;
575 if (!closed)
576 kobj_close(mod->mod_kobj);
577 TAILQ_REMOVE(&pending, mod, mod_chain);
578 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
579 for (i = 0; i < mod->mod_nrequired; i++) {
580 KASSERT(mod->mod_required[i] != NULL);
581 mod->mod_required[i]->mod_refcnt++;
582 }
583 if (modp != NULL) {
584 *modp = mod;
585 }
586 depth--;
587 return 0;
588
589 fail:
590 if (!closed)
591 kobj_close(mod->mod_kobj);
592 TAILQ_REMOVE(&pending, mod, mod_chain);
593 kobj_unload(mod->mod_kobj);
594 kmem_free(mod, sizeof(*mod));
595 depth--;
596 return error;
597 }
598
599 /*
600 * module_do_unload:
601 *
602 * Helper routine: do the dirty work of unloading a module.
603 */
604 static int
605 module_do_unload(const char *name)
606 {
607 module_t *mod;
608 int error;
609 u_int i;
610
611 KASSERT(mutex_owned(&module_lock));
612
613 mod = module_lookup(name);
614 if (mod == NULL) {
615 return ENOENT;
616 }
617 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
618 return EBUSY;
619 }
620 KASSERT(module_active == NULL);
621 module_active = mod;
622 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
623 module_active = NULL;
624 if (error != 0) {
625 return error;
626 }
627 module_count--;
628 TAILQ_REMOVE(&module_list, mod, mod_chain);
629 for (i = 0; i < mod->mod_nrequired; i++) {
630 mod->mod_required[i]->mod_refcnt--;
631 }
632 if (mod->mod_kobj != NULL) {
633 kobj_unload(mod->mod_kobj);
634 }
635 kmem_free(mod, sizeof(*mod));
636
637 return 0;
638 }
639
640 /*
641 * module_prime:
642 *
643 * Push a module loaded by the bootloader onto our internal
644 * list.
645 */
646 int
647 module_prime(void *base, size_t size)
648 {
649 module_t *mod;
650 int error;
651
652 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
653 if (mod == NULL) {
654 return ENOMEM;
655 }
656 mod->mod_source = MODULE_SOURCE_BOOT;
657
658 error = kobj_open_mem(&mod->mod_kobj, base, size);
659 if (error != 0) {
660 kmem_free(mod, sizeof(*mod));
661 module_error("unable to open object pushed by boot loader");
662 return error;
663 }
664
665 error = kobj_load(mod->mod_kobj);
666 if (error != 0) {
667 kobj_close(mod->mod_kobj);
668 kmem_free(mod, sizeof(*mod));
669 module_error("unable to load object pushed by boot loader");
670 return error;
671 }
672 error = module_fetch_info(mod);
673 if (error != 0) {
674 kobj_close(mod->mod_kobj);
675 kobj_unload(mod->mod_kobj);
676 kmem_free(mod, sizeof(*mod));
677 module_error("unable to load object pushed by boot loader");
678 return error;
679 }
680
681 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
682
683 return 0;
684 }
685
686 /*
687 * module_fetch_into:
688 *
689 * Fetch modinfo record from a loaded module.
690 */
691 static int
692 module_fetch_info(module_t *mod)
693 {
694 int error;
695 void *addr;
696 size_t size;
697
698 /*
699 * Find module info record and check compatibility.
700 */
701 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
702 &addr, &size);
703 if (error != 0) {
704 module_error("`link_set_modules' section not present");
705 return error;
706 }
707 if (size != sizeof(modinfo_t **)) {
708 module_error("`link_set_modules' section wrong size");
709 return error;
710 }
711 mod->mod_info = *(modinfo_t **)addr;
712
713 return 0;
714 }
715
716 /*
717 * module_find_section:
718 *
719 * Allows a module that is being initialized to look up a section
720 * within its ELF object.
721 */
722 int
723 module_find_section(const char *name, void **addr, size_t *size)
724 {
725
726 KASSERT(mutex_owned(&module_lock));
727 KASSERT(module_active != NULL);
728
729 return kobj_find_section(module_active->mod_kobj, name, addr, size);
730 }
731