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