kern_module.c revision 1.17 1 /* $NetBSD: kern_module.c,v 1.17 2008/05/20 16:04:08 ad 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.17 2008/05/20 16:04:08 ad 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 static char module_base[64];
65 u_int module_count;
66 kmutex_t module_lock;
67
68 /* Ensure that the kernel's link set isn't empty. */
69 static modinfo_t module_dummy;
70 __link_set_add_rodata(modules, module_dummy);
71
72 static module_t *module_lookup(const char *);
73 static int module_do_load(const char *, bool, int, prop_dictionary_t,
74 module_t **);
75 static int module_do_unload(const char *);
76 static void module_error(const char *, ...);
77 static int module_do_builtin(const char *, module_t **);
78 static int module_fetch_info(module_t *);
79
80 /*
81 * module_error:
82 *
83 * Utility function: log an error.
84 */
85 static void
86 module_error(const char *fmt, ...)
87 {
88 va_list ap;
89
90 va_start(ap, fmt);
91 printf("WARNING: module error: ");
92 vprintf(fmt, ap);
93 printf("\n");
94 va_end(ap);
95 }
96
97 /*
98 * module_init:
99 *
100 * Initialize the module subsystem.
101 */
102 void
103 module_init(void)
104 {
105 extern struct vm_map *lkm_map;
106
107 if (lkm_map == NULL)
108 lkm_map = kernel_map;
109 mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
110 #ifdef MODULAR /* XXX */
111 module_init_md();
112 #endif
113
114 #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
115 snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
116 machine, osrelease);
117 #else /* release */
118 snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
119 machine, __NetBSD_Version__ / 100000000,
120 __NetBSD_Version__ / 1000000 % 100);
121 #endif
122 }
123
124 /*
125 * module_init_class:
126 *
127 * Initialize all built-in and pre-loaded modules of the
128 * specified class.
129 */
130 void
131 module_init_class(modclass_t class)
132 {
133 __link_set_decl(modules, modinfo_t);
134 modinfo_t *const *mip, *mi;
135 module_t *mod;
136
137 mutex_enter(&module_lock);
138 /*
139 * Builtins first. These can't depend on pre-loaded modules.
140 */
141 __link_set_foreach(mip, modules) {
142 mi = *mip;
143 if (mi == &module_dummy) {
144 continue;
145 }
146 if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
147 continue;
148 }
149 (void)module_do_builtin(mi->mi_name, NULL);
150 }
151 /*
152 * Now preloaded modules. These will be pulled off the
153 * list as we call module_do_load();
154 */
155 do {
156 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
157 mi = mod->mod_info;
158 if (class != MODULE_CLASS_ANY &&
159 class != mi->mi_class)
160 continue;
161 module_do_load(mi->mi_name, false, 0, NULL, NULL);
162 break;
163 }
164 } while (mod != NULL);
165 mutex_exit(&module_lock);
166 }
167
168 /*
169 * module_jettison:
170 *
171 * Return memory used by pre-loaded modules to the freelist.
172 */
173 void
174 module_jettison(void)
175 {
176
177 /* XXX nothing yet */
178 }
179
180 /*
181 * module_load:
182 *
183 * Load a single module from the file system.
184 */
185 int
186 module_load(const char *filename, int flags, prop_dictionary_t props)
187 {
188 int error;
189
190 mutex_enter(&module_lock);
191 error = module_do_load(filename, false, flags, props, NULL);
192 mutex_exit(&module_lock);
193
194 return error;
195 }
196
197 /*
198 * module_unload:
199 *
200 * Find and unload a module by name.
201 */
202 int
203 module_unload(const char *name)
204 {
205 int error;
206
207 mutex_enter(&module_lock);
208 error = module_do_unload(name);
209 mutex_exit(&module_lock);
210
211 return error;
212 }
213
214 /*
215 * module_lookup:
216 *
217 * Look up a module by name.
218 */
219 module_t *
220 module_lookup(const char *name)
221 {
222 module_t *mod;
223
224 KASSERT(mutex_owned(&module_lock));
225
226 TAILQ_FOREACH(mod, &module_list, mod_chain) {
227 if (strcmp(mod->mod_info->mi_name, name) == 0) {
228 break;
229 }
230 }
231
232 return mod;
233 }
234
235 /*
236 * module_hold:
237 *
238 * Add a single reference to a module. It's the caller's
239 * responsibility to ensure that the reference is dropped
240 * later.
241 */
242 int
243 module_hold(const char *name)
244 {
245 module_t *mod;
246
247 mutex_enter(&module_lock);
248 mod = module_lookup(name);
249 if (mod == NULL) {
250 mutex_exit(&module_lock);
251 return ENOENT;
252 }
253 mod->mod_refcnt++;
254 mutex_exit(&module_lock);
255
256 return 0;
257 }
258
259 /*
260 * module_rele:
261 *
262 * Release a reference acquired with module_hold().
263 */
264 void
265 module_rele(const char *name)
266 {
267 module_t *mod;
268
269 mutex_enter(&module_lock);
270 mod = module_lookup(name);
271 if (mod == NULL) {
272 mutex_exit(&module_lock);
273 panic("module_rele: gone");
274 }
275 mod->mod_refcnt--;
276 mutex_exit(&module_lock);
277 }
278
279 /*
280 * module_do_builtin:
281 *
282 * Initialize a single module from the list of modules that are
283 * built into the kernel (linked into the kernel image).
284 */
285 static int
286 module_do_builtin(const char *name, module_t **modp)
287 {
288 __link_set_decl(modules, modinfo_t);
289 modinfo_t *const *mip;
290 const char *p, *s;
291 char buf[MAXMODNAME];
292 modinfo_t *mi;
293 module_t *mod, *mod2;
294 size_t len;
295 int error, i;
296
297 KASSERT(mutex_owned(&module_lock));
298
299 /*
300 * Check to see if already loaded.
301 */
302 if ((mod = module_lookup(name)) != NULL) {
303 if (modp != NULL) {
304 *modp = mod;
305 }
306 return 0;
307 }
308
309 /*
310 * Search the list to see if we have a module by this name.
311 */
312 error = ENOENT;
313 __link_set_foreach(mip, modules) {
314 mi = *mip;
315 if (mi == &module_dummy) {
316 continue;
317 }
318 if (strcmp(mi->mi_name, name) == 0) {
319 error = 0;
320 break;
321 }
322 }
323 if (error != 0) {
324 return error;
325 }
326
327 /*
328 * Initialize pre-requisites.
329 */
330 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
331 if (mod == NULL) {
332 return ENOMEM;
333 }
334 if (modp != NULL) {
335 *modp = mod;
336 }
337 if (mi->mi_required != NULL) {
338 for (s = mi->mi_required; *s != '\0'; s = p) {
339 if (*s == ',')
340 s++;
341 p = s;
342 while (*p != '\0' && *p != ',')
343 p++;
344 len = min(p - s + 1, sizeof(buf));
345 strlcpy(buf, s, len);
346 if (buf[0] == '\0')
347 break;
348 if (mod->mod_nrequired == MAXMODDEPS - 1) {
349 module_error("too many required modules");
350 kmem_free(mod, sizeof(*mod));
351 return EINVAL;
352 }
353 error = module_do_builtin(buf, &mod2);
354 if (error != 0) {
355 kmem_free(mod, sizeof(*mod));
356 return error;
357 }
358 mod->mod_required[mod->mod_nrequired++] = mod2;
359 }
360 }
361
362 /*
363 * Try to initialize the module.
364 */
365 KASSERT(module_active == NULL);
366 module_active = mod;
367 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
368 module_active = NULL;
369 if (error != 0) {
370 module_error("builtin module `%s' "
371 "failed to init", mi->mi_name);
372 kmem_free(mod, sizeof(*mod));
373 return error;
374 }
375 mod->mod_info = mi;
376 mod->mod_source = MODULE_SOURCE_KERNEL;
377 module_count++;
378 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
379
380 /*
381 * If that worked, count dependencies.
382 */
383 for (i = 0; i < mod->mod_nrequired; i++) {
384 mod->mod_required[i]->mod_refcnt++;
385 }
386
387 return 0;
388 }
389
390 /*
391 * module_do_load:
392 *
393 * Helper routine: load a module from the file system, or one
394 * pushed by the boot loader.
395 */
396 static int
397 module_do_load(const char *filename, bool isdep, int flags,
398 prop_dictionary_t props, module_t **modp)
399 {
400 static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
401 static int depth;
402 const int maxdepth = 6;
403 modinfo_t *mi;
404 module_t *mod, *mod2;
405 char buf[MAXMODNAME];
406 const char *s, *p;
407 int error;
408 size_t len;
409 u_int i;
410
411 KASSERT(mutex_owned(&module_lock));
412
413 error = 0;
414
415 /*
416 * Avoid recursing too far.
417 */
418 if (++depth > maxdepth) {
419 module_error("too many required modules");
420 depth--;
421 return EMLINK;
422 }
423
424 /*
425 * Load the module and link. Before going to the file system,
426 * scan the list of modules loaded by the boot loader. Just
427 * before init is started the list of modules loaded at boot
428 * will be purged. Before init is started we can assume that
429 * `filename' is a module name and not a path name.
430 */
431 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
432 if (strcmp(mod->mod_info->mi_name, filename) == 0) {
433 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
434 break;
435 }
436 }
437 if (mod != NULL) {
438 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
439 } else {
440 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
441 if (mod == NULL) {
442 depth--;
443 return ENOMEM;
444 }
445 error = kobj_load_file(&mod->mod_kobj, filename, module_base);
446 if (error != 0) {
447 kmem_free(mod, sizeof(*mod));
448 depth--;
449 module_error("unable to load kernel object");
450 return error;
451 }
452 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
453 mod->mod_source = MODULE_SOURCE_FILESYS;
454 error = module_fetch_info(mod);
455 if (error != 0) {
456 goto fail;
457 }
458 }
459
460 /*
461 * Check compatibility.
462 */
463 mi = mod->mod_info;
464 if (strlen(mi->mi_name) >= MAXMODNAME) {
465 error = EINVAL;
466 module_error("module name too long");
467 goto fail;
468 }
469
470 /*
471 * If loading a dependency, `filename' is a plain module name.
472 * The name must match.
473 */
474 if (isdep && strcmp(mi->mi_name, filename) != 0) {
475 error = ENOENT;
476 goto fail;
477 }
478
479 /*
480 * Check to see if the module is already loaded. If so, we may
481 * have been recursively called to handle a dependency, so be sure
482 * to set modp.
483 */
484 if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
485 if (modp != NULL)
486 *modp = mod2;
487 error = EEXIST;
488 goto fail;
489 }
490
491 /*
492 * Block circular dependencies.
493 */
494 TAILQ_FOREACH(mod2, &pending, mod_chain) {
495 if (mod == mod2) {
496 continue;
497 }
498 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
499 error = EDEADLK;
500 module_error("circular dependency detected");
501 goto fail;
502 }
503 }
504
505 /*
506 * Now try to load any requisite modules.
507 */
508 if (mi->mi_required != NULL) {
509 for (s = mi->mi_required; *s != '\0'; s = p) {
510 if (*s == ',')
511 s++;
512 p = s;
513 while (*p != '\0' && *p != ',')
514 p++;
515 len = p - s + 1;
516 if (len >= MAXMODNAME) {
517 error = EINVAL;
518 module_error("required module name too long");
519 goto fail;
520 }
521 strlcpy(buf, s, len);
522 if (buf[0] == '\0')
523 break;
524 if (mod->mod_nrequired == MAXMODDEPS - 1) {
525 error = EINVAL;
526 module_error("too many required modules");
527 goto fail;
528 }
529 if (strcmp(buf, mi->mi_name) == 0) {
530 error = EDEADLK;
531 module_error("self-dependency detected");
532 goto fail;
533 }
534 error = module_do_load(buf, true, flags, NULL,
535 &mod->mod_required[mod->mod_nrequired++]);
536 if (error != 0 && error != EEXIST)
537 goto fail;
538 }
539 }
540
541 /*
542 * We loaded all needed modules successfully: perform global
543 * relocations and initialize.
544 */
545 error = kobj_affix(mod->mod_kobj, mi->mi_name);
546 if (error != 0) {
547 module_error("unable to affix module");
548 goto fail2;
549 }
550
551 KASSERT(module_active == NULL);
552 module_active = mod;
553 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
554 module_active = NULL;
555 if (error != 0) {
556 module_error("modctl function returned error %d", error);
557 goto fail;
558 }
559
560 /*
561 * Good, the module loaded successfully. Put it onto the
562 * list and add references to its requisite modules.
563 */
564 module_count++;
565 TAILQ_REMOVE(&pending, mod, mod_chain);
566 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
567 for (i = 0; i < mod->mod_nrequired; i++) {
568 KASSERT(mod->mod_required[i] != NULL);
569 mod->mod_required[i]->mod_refcnt++;
570 }
571 if (modp != NULL) {
572 *modp = mod;
573 }
574 depth--;
575 return 0;
576
577 fail:
578 kobj_unload(mod->mod_kobj);
579 fail2:
580 TAILQ_REMOVE(&pending, mod, mod_chain);
581 kmem_free(mod, sizeof(*mod));
582 depth--;
583 return error;
584 }
585
586 /*
587 * module_do_unload:
588 *
589 * Helper routine: do the dirty work of unloading a module.
590 */
591 static int
592 module_do_unload(const char *name)
593 {
594 module_t *mod;
595 int error;
596 u_int i;
597
598 KASSERT(mutex_owned(&module_lock));
599
600 mod = module_lookup(name);
601 if (mod == NULL) {
602 return ENOENT;
603 }
604 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
605 return EBUSY;
606 }
607 KASSERT(module_active == NULL);
608 module_active = mod;
609 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
610 module_active = NULL;
611 if (error != 0) {
612 return error;
613 }
614 module_count--;
615 TAILQ_REMOVE(&module_list, mod, mod_chain);
616 for (i = 0; i < mod->mod_nrequired; i++) {
617 mod->mod_required[i]->mod_refcnt--;
618 }
619 if (mod->mod_kobj != NULL) {
620 kobj_unload(mod->mod_kobj);
621 }
622 kmem_free(mod, sizeof(*mod));
623
624 return 0;
625 }
626
627 /*
628 * module_prime:
629 *
630 * Push a module loaded by the bootloader onto our internal
631 * list.
632 */
633 int
634 module_prime(void *base, size_t size)
635 {
636 module_t *mod;
637 int error;
638
639 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
640 if (mod == NULL) {
641 return ENOMEM;
642 }
643 mod->mod_source = MODULE_SOURCE_BOOT;
644
645 error = kobj_load_mem(&mod->mod_kobj, base, size);
646 if (error != 0) {
647 kmem_free(mod, sizeof(*mod));
648 module_error("unable to load object pushed by boot loader");
649 return error;
650 }
651 error = module_fetch_info(mod);
652 if (error != 0) {
653 kobj_unload(mod->mod_kobj);
654 kmem_free(mod, sizeof(*mod));
655 module_error("unable to load object pushed by boot loader");
656 return error;
657 }
658
659 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
660
661 return 0;
662 }
663
664 /*
665 * module_fetch_into:
666 *
667 * Fetch modinfo record from a loaded module.
668 */
669 static int
670 module_fetch_info(module_t *mod)
671 {
672 int error;
673 void *addr;
674 size_t size;
675
676 /*
677 * Find module info record and check compatibility.
678 */
679 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
680 &addr, &size);
681 if (error != 0) {
682 module_error("`link_set_modules' section not present");
683 return error;
684 }
685 if (size != sizeof(modinfo_t **)) {
686 module_error("`link_set_modules' section wrong size");
687 return error;
688 }
689 mod->mod_info = *(modinfo_t **)addr;
690
691 return 0;
692 }
693
694 /*
695 * module_find_section:
696 *
697 * Allows a module that is being initialized to look up a section
698 * within its ELF object.
699 */
700 int
701 module_find_section(const char *name, void **addr, size_t *size)
702 {
703
704 KASSERT(mutex_owned(&module_lock));
705 KASSERT(module_active != NULL);
706
707 return kobj_find_section(module_active->mod_kobj, name, addr, size);
708 }
709