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