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