kern_module.c revision 1.2 1 /* $NetBSD: kern_module.c,v 1.2 2008/01/16 18:28:32 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 * 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.2 2008/01/16 18:28:32 ad 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
392 KASSERT(mutex_owned(&module_lock));
393
394 error = 0;
395
396 /*
397 * Avoid recursing too far.
398 */
399 if (++depth > maxdepth) {
400 module_error("too many required modules");
401 depth--;
402 return EMLINK;
403 }
404
405 /*
406 * Load the module and link. Before going to the file system,
407 * scan the list of modules loaded by the boot loader. Just
408 * before init is started the list of modules loaded at boot
409 * will be purged. Before init is started we can assume that
410 * `filename' is a module name and not a path name.
411 */
412 TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
413 if (strcmp(mod->mod_info->mi_name, filename) == 0) {
414 TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
415 break;
416 }
417 }
418 if (mod == NULL) {
419 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
420 if (mod == NULL) {
421 depth--;
422 return ENOMEM;
423 }
424 error = kobj_open_file(&mod->mod_kobj, filename);
425 if (error != 0) {
426 kmem_free(mod, sizeof(*mod));
427 depth--;
428 module_error("unable to open object file");
429 return error;
430 }
431 error = kobj_load(mod->mod_kobj);
432 mod->mod_source = MODULE_SOURCE_FILESYS;
433 }
434 TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
435 if (error != 0) {
436 goto fail;
437 }
438
439 /*
440 * Find module info record and check compatibility.
441 */
442 error = kobj_find_section(mod->mod_kobj, "link_set_modules",
443 &addr, &size);
444 if (error != 0) {
445 module_error("`link_set_modules' section not present");
446 goto fail;
447 }
448 if (size != sizeof(modinfo_t **)) {
449 module_error("`link_set_modules' section wrong size");
450 goto fail;
451 }
452 mod->mod_info = *(modinfo_t **)addr;
453 mi = mod->mod_info;
454
455 /*
456 * If loading a dependency, `filename' is a plain module name.
457 * The name must match.
458 */
459 if (isdep && strcmp(mi->mi_name, filename) != 0) {
460 error = ENOENT;
461 goto fail;
462 }
463
464 /*
465 * Check to see if the module is already loaded, and block
466 * circular dependencies.
467 */
468 if (module_lookup(mi->mi_name) != NULL) {
469 error = EEXIST;
470 goto fail;
471 }
472 TAILQ_FOREACH(mod2, &pending, mod_chain) {
473 if (mod == mod2) {
474 continue;
475 }
476 if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
477 error = EDEADLK;
478 module_error("circular dependency detected");
479 goto fail;
480 }
481 }
482
483 /*
484 * Pass proper name to kobj. This will register the module
485 * with the ksyms framework.
486 */
487 error = kobj_set_name(mod->mod_kobj, mi->mi_name);
488 if (error != 0) {
489 module_error("unable to set name");
490 goto fail;
491 }
492
493 /*
494 * Now try to load any requisite modules.
495 */
496 if (mi->mi_required != NULL) {
497 for (s = mi->mi_required; *s != '\0'; s = p) {
498 if (*s == ',')
499 s++;
500 p = s;
501 while (*p != '\0' && *p != ',')
502 p++;
503 len = min(p - s + 1, MAXMODNAME);
504 strlcpy(buf, s, len);
505 if (buf[0] == '\0')
506 break;
507 if (mod->mod_nrequired == MAXMODDEPS - 1) {
508 error = EINVAL;
509 module_error("too many required modules");
510 goto fail;
511 }
512 error = module_do_load(buf, true, force,
513 &mod->mod_required[mod->mod_nrequired++]);
514 if (error != 0 && error != EEXIST)
515 goto fail;
516 }
517 }
518
519 /*
520 * We loaded all needed modules successfully: initialize.
521 */
522 error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
523 if (error != 0) {
524 module_error("modctl function returned error %d", error);
525 goto fail;
526 }
527
528 /*
529 * Good, the module loaded successfully. Put it onto the
530 * list and add references to its requisite modules.
531 */
532 module_count++;
533 kobj_close(mod->mod_kobj);
534 TAILQ_REMOVE(&pending, mod, mod_chain);
535 TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
536 for (i = 0; i < mod->mod_nrequired; i++) {
537 mod->mod_required[i]->mod_refcnt++;
538 }
539 if (modp != NULL) {
540 *modp = mod;
541 }
542 depth--;
543 return 0;
544 fail:
545 kobj_close(mod->mod_kobj);
546 TAILQ_REMOVE(&pending, mod, mod_chain);
547 kobj_unload(mod->mod_kobj);
548 kmem_free(mod, sizeof(*mod));
549 depth--;
550 return error;
551 }
552
553 /*
554 * module_do_unload:
555 *
556 * Helper routine: do the dirty work of unloading a module.
557 */
558 static int
559 module_do_unload(const char *name)
560 {
561 module_t *mod;
562 int error;
563 u_int i;
564
565 KASSERT(mutex_owned(&module_lock));
566
567 mod = module_lookup(name);
568 if (mod == NULL) {
569 return ENOENT;
570 }
571 if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
572 return EBUSY;
573 }
574 error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
575 if (error != 0) {
576 return error;
577 }
578 module_count--;
579 TAILQ_REMOVE(&module_list, mod, mod_chain);
580 for (i = 0; i < mod->mod_nrequired; i++) {
581 mod->mod_required[i]->mod_refcnt--;
582 }
583 if (mod->mod_kobj != NULL) {
584 kobj_unload(mod->mod_kobj);
585 }
586 kmem_free(mod, sizeof(*mod));
587
588 return 0;
589 }
590
591 /*
592 * module_prime:
593 *
594 * Push a module loaded by the bootloader onto our internal
595 * list.
596 */
597 int
598 module_prime(void *base, size_t size)
599 {
600 module_t *mod;
601 int error;
602
603 mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
604 if (mod == NULL) {
605 return ENOMEM;
606 }
607 mod->mod_source = MODULE_SOURCE_BOOT;
608
609 error = kobj_open_mem(&mod->mod_kobj, base, size);
610 if (error == 0) {
611 kmem_free(mod, sizeof(*mod));
612 module_error("unable to open object pushed by boot loader");
613 return error;
614 }
615 error = kobj_load(mod->mod_kobj);
616 if (error != 0) {
617 kmem_free(mod, sizeof(*mod));
618 module_error("unable to push object pushed by boot loader");
619 return error;
620 }
621 mod->mod_source = MODULE_SOURCE_FILESYS;
622 TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
623
624 return 0;
625 }
626