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