xref: /netbsd-src/sys/kern/kern_module.c (revision 11a6dbe72840351315e0652b2fc6663628c84cad)
1 /*	$NetBSD: kern_module.c,v 1.10 2008/04/28 20:24:03 martin 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.10 2008/04/28 20:24:03 martin 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 
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 }
106 
107 /*
108  * module_init_class:
109  *
110  *	Initialize all built-in and pre-loaded modules of the
111  *	specified class.
112  */
113 void
114 module_init_class(modclass_t class)
115 {
116 	__link_set_decl(modules, modinfo_t);
117 	modinfo_t *const *mip, *mi;
118 	module_t *mod;
119 
120 	mutex_enter(&module_lock);
121 	/*
122 	 * Builtins first.  These can't depend on pre-loaded modules.
123 	 */
124 	__link_set_foreach(mip, modules) {
125 		mi = *mip;
126 		if (mi == &module_dummy) {
127 			continue;
128 		}
129 		if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
130 			continue;
131 		}
132 		(void)module_do_builtin(mi->mi_name, NULL);
133 	}
134 	/*
135 	 * Now preloaded modules.  These will be pulled off the
136 	 * list as we call module_do_load();
137 	 */
138 	while ((mod = TAILQ_FIRST(&module_bootlist)) != NULL) {
139 		module_do_load(mod->mod_info->mi_name, false, 0,
140 		    NULL, NULL);
141 	}
142 	mutex_exit(&module_lock);
143 }
144 
145 /*
146  * module_jettison:
147  *
148  *	Return memory used by pre-loaded modules to the freelist.
149  */
150 void
151 module_jettison(void)
152 {
153 
154 	/* XXX nothing yet */
155 }
156 
157 /*
158  * module_load:
159  *
160  *	Load a single module from the file system.
161  */
162 int
163 module_load(const char *filename, int flags, prop_dictionary_t props)
164 {
165 	int error;
166 
167 	mutex_enter(&module_lock);
168 	error = module_do_load(filename, false, flags, props, NULL);
169 	mutex_exit(&module_lock);
170 
171 	return error;
172 }
173 
174 /*
175  * module_unload:
176  *
177  *	Find and unload a module by name.
178  */
179 int
180 module_unload(const char *name)
181 {
182 	int error;
183 
184 	mutex_enter(&module_lock);
185 	error = module_do_unload(name);
186 	mutex_exit(&module_lock);
187 
188 	return error;
189 }
190 
191 /*
192  * module_lookup:
193  *
194  *	Look up a module by name.
195  */
196 module_t *
197 module_lookup(const char *name)
198 {
199 	module_t *mod;
200 
201 	KASSERT(mutex_owned(&module_lock));
202 
203 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
204 		if (strcmp(mod->mod_info->mi_name, name) == 0) {
205 			break;
206 		}
207 	}
208 
209 	return mod;
210 }
211 
212 /*
213  * module_hold:
214  *
215  *	Add a single reference to a module.  It's the caller's
216  *	responsibility to ensure that the reference is dropped
217  *	later.
218  */
219 int
220 module_hold(const char *name)
221 {
222 	module_t *mod;
223 
224 	mutex_enter(&module_lock);
225 	mod = module_lookup(name);
226 	if (mod == NULL) {
227 		mutex_exit(&module_lock);
228 		return ENOENT;
229 	}
230 	mod->mod_refcnt++;
231 	mutex_exit(&module_lock);
232 
233 	return 0;
234 }
235 
236 /*
237  * module_rele:
238  *
239  *	Release a reference acquired with module_hold().
240  */
241 void
242 module_rele(const char *name)
243 {
244 	module_t *mod;
245 
246 	mutex_enter(&module_lock);
247 	mod = module_lookup(name);
248 	if (mod == NULL) {
249 		mutex_exit(&module_lock);
250 		panic("module_rele: gone");
251 	}
252 	mod->mod_refcnt--;
253 	mutex_exit(&module_lock);
254 }
255 
256 /*
257  * module_do_builtin:
258  *
259  *	Initialize a single module from the list of modules that are
260  *	built into the kernel (linked into the kernel image).
261  */
262 static int
263 module_do_builtin(const char *name, module_t **modp)
264 {
265 	__link_set_decl(modules, modinfo_t);
266 	modinfo_t *const *mip;
267 	const char *p, *s;
268 	char buf[MAXMODNAME];
269 	modinfo_t *mi;
270 	module_t *mod, *mod2;
271 	size_t len;
272 	int error, i;
273 
274 	KASSERT(mutex_owned(&module_lock));
275 
276 	/*
277 	 * Check to see if already loaded.
278 	 */
279 	if ((mod = module_lookup(name)) != NULL) {
280 		if (modp != NULL) {
281 			*modp = mod;
282 		}
283 		return 0;
284 	}
285 
286 	/*
287 	 * Search the list to see if we have a module by this name.
288 	 */
289 	error = ENOENT;
290 	__link_set_foreach(mip, modules) {
291 		mi = *mip;
292 		if (mi == &module_dummy) {
293 			continue;
294 		}
295 		if (strcmp(mi->mi_name, name) == 0) {
296 			error = 0;
297 			break;
298 		}
299 	}
300 	if (error != 0) {
301 		return error;
302 	}
303 
304 	/*
305 	 * Initialize pre-requisites.
306 	 */
307 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
308 	if (mod == NULL) {
309 		return ENOMEM;
310 	}
311 	if (modp != NULL) {
312 		*modp = mod;
313 	}
314 	if (mi->mi_required != NULL) {
315 		for (s = mi->mi_required; *s != '\0'; s = p) {
316 			if (*s == ',')
317 				s++;
318 			p = s;
319 			while (*p != '\0' && *p != ',')
320 				p++;
321 			len = min(p - s + 1, sizeof(buf));
322 			strlcpy(buf, s, len);
323 			if (buf[0] == '\0')
324 				break;
325 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
326 				module_error("too many required modules");
327 				kmem_free(mod, sizeof(*mod));
328 				return EINVAL;
329 			}
330 			error = module_do_builtin(buf, &mod2);
331 			if (error != 0) {
332 				kmem_free(mod, sizeof(*mod));
333 				return error;
334 			}
335 			mod->mod_required[mod->mod_nrequired++] = mod2;
336 		}
337 	}
338 
339 	/*
340 	 * Try to initialize the module.
341 	 */
342 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
343 	if (error != 0) {
344 		module_error("builtin module `%s' "
345 		    "failed to init", mi->mi_name);
346 		kmem_free(mod, sizeof(*mod));
347 		return error;
348 	}
349 	mod->mod_info = mi;
350 	mod->mod_source = MODULE_SOURCE_KERNEL;
351 	module_count++;
352 	TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
353 
354 	/*
355 	 * If that worked, count dependencies.
356 	 */
357 	for (i = 0; i < mod->mod_nrequired; i++) {
358 		mod->mod_required[i]->mod_refcnt++;
359 	}
360 
361 	return 0;
362 }
363 
364 /*
365  * module_do_load:
366  *
367  *	Helper routine: load a module from the file system, or one
368  *	pushed by the boot loader.
369  */
370 static int
371 module_do_load(const char *filename, bool isdep, int flags,
372     prop_dictionary_t props, module_t **modp)
373 {
374 	static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
375 	static int depth;
376 	const int maxdepth = 6;
377 	modinfo_t *mi;
378 	module_t *mod, *mod2;
379 	char buf[MAXMODNAME];
380 	const char *s, *p;
381 	void *addr;
382 	size_t size;
383 	int error;
384 	size_t len;
385 	u_int i;
386 	bool closed = false;
387 
388 	KASSERT(mutex_owned(&module_lock));
389 
390 	error = 0;
391 
392 	/*
393 	 * Avoid recursing too far.
394 	 */
395 	if (++depth > maxdepth) {
396 		module_error("too many required modules");
397 		depth--;
398 		return EMLINK;
399 	}
400 
401 	/*
402 	 * Load the module and link.  Before going to the file system,
403 	 * scan the list of modules loaded by the boot loader.  Just
404 	 * before init is started the list of modules loaded at boot
405 	 * will be purged.  Before init is started we can assume that
406 	 * `filename' is a module name and not a path name.
407 	 */
408 	TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
409 		if (strcmp(mod->mod_info->mi_name, filename) == 0) {
410 			TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
411 			break;
412 		}
413 	}
414 	if (mod == NULL) {
415 		mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
416 		if (mod == NULL) {
417 			depth--;
418 			return ENOMEM;
419 		}
420 		error = kobj_open_file(&mod->mod_kobj, filename);
421 		if (error != 0) {
422 			kmem_free(mod, sizeof(*mod));
423 			depth--;
424 			module_error("unable to open object file");
425 			return error;
426 		}
427 		error = kobj_load(mod->mod_kobj);
428 		if (error != 0) {
429 			kobj_close(mod->mod_kobj);
430 			kmem_free(mod, sizeof(*mod));
431 			depth--;
432 			module_error("unable to load kernel object");
433 			return error;
434 		}
435 		mod->mod_source = MODULE_SOURCE_FILESYS;
436 	}
437 	TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
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 	if (strlen(mi->mi_name) >= MAXMODNAME) {
456 		error = EINVAL;
457 		module_error("module name too long");
458 		goto fail;
459 	}
460 
461 	/*
462 	 * If loading a dependency, `filename' is a plain module name.
463 	 * The name must match.
464 	 */
465 	if (isdep && strcmp(mi->mi_name, filename) != 0) {
466 		error = ENOENT;
467 		goto fail;
468 	}
469 
470 	/*
471 	 * Check to see if the module is already loaded.  If so, we may
472 	 * have been recursively called to handle a dependency, so be sure
473 	 * to set modp.
474 	 */
475 	if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
476 		if (modp != NULL)
477 			*modp = mod2;
478 		error = EEXIST;
479 		goto fail;
480 	}
481 
482 	/*
483 	 * Block circular dependencies.
484 	 */
485 	TAILQ_FOREACH(mod2, &pending, mod_chain) {
486 		if (mod == mod2) {
487 			continue;
488 		}
489 		if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
490 		    	error = EDEADLK;
491 			module_error("circular dependency detected");
492 		    	goto fail;
493 		}
494 	}
495 
496 	/*
497 	 * Pass proper name to kobj.  This will register the module
498 	 * with the ksyms framework.
499 	 */
500 	error = kobj_set_name(mod->mod_kobj, mi->mi_name);
501 	if (error != 0) {
502 		module_error("unable to set name");
503 		goto fail;
504 	}
505 
506 	/*
507 	 * Close the kobj before handling dependencies since we're done
508 	 * with it and don't want to open an already locked file if a
509 	 * circular dependency exists.
510 	 */
511 	kobj_close(mod->mod_kobj);
512 	closed = true;
513 
514 	/*
515 	 * Now try to load any requisite modules.
516 	 */
517 	if (mi->mi_required != NULL) {
518 		for (s = mi->mi_required; *s != '\0'; s = p) {
519 			if (*s == ',')
520 				s++;
521 			p = s;
522 			while (*p != '\0' && *p != ',')
523 				p++;
524 			len = p - s + 1;
525 			if (len >= MAXMODNAME) {
526 				error = EINVAL;
527 				module_error("required module name too long");
528 				goto fail;
529 			}
530 			strlcpy(buf, s, len);
531 			if (buf[0] == '\0')
532 				break;
533 			if (mod->mod_nrequired == MAXMODDEPS - 1) {
534 				error = EINVAL;
535 				module_error("too many required modules");
536 				goto fail;
537 			}
538 			if (strcmp(buf, mi->mi_name) == 0) {
539 				error = EDEADLK;
540 				module_error("self-dependency detected");
541 				goto fail;
542 			}
543 			error = module_do_load(buf, true, flags, NULL,
544 			    &mod->mod_required[mod->mod_nrequired++]);
545 			if (error != 0 && error != EEXIST)
546 				goto fail;
547 		}
548 	}
549 
550 	/*
551 	 * We loaded all needed modules successfully: initialize.
552 	 */
553 	error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
554 	if (error != 0) {
555 		module_error("modctl function returned error %d", error);
556 		goto fail;
557 	}
558 
559 	/*
560 	 * Good, the module loaded successfully.  Put it onto the
561 	 * list and add references to its requisite modules.
562 	 */
563 	module_count++;
564 	if (!closed)
565 		kobj_close(mod->mod_kobj);
566 	TAILQ_REMOVE(&pending, mod, mod_chain);
567 	TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
568 	for (i = 0; i < mod->mod_nrequired; i++) {
569 		KASSERT(mod->mod_required[i] != NULL);
570 		mod->mod_required[i]->mod_refcnt++;
571 	}
572 	if (modp != NULL) {
573 		*modp = mod;
574 	}
575 	depth--;
576 	return 0;
577 
578  fail:
579 	if (!closed)
580 		kobj_close(mod->mod_kobj);
581 	TAILQ_REMOVE(&pending, mod, mod_chain);
582 	kobj_unload(mod->mod_kobj);
583 	kmem_free(mod, sizeof(*mod));
584 	depth--;
585 	return error;
586 }
587 
588 /*
589  * module_do_unload:
590  *
591  *	Helper routine: do the dirty work of unloading a module.
592  */
593 static int
594 module_do_unload(const char *name)
595 {
596 	module_t *mod;
597 	int error;
598 	u_int i;
599 
600 	KASSERT(mutex_owned(&module_lock));
601 
602 	mod = module_lookup(name);
603 	if (mod == NULL) {
604 		return ENOENT;
605 	}
606 	if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
607 		return EBUSY;
608 	}
609 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
610 	if (error != 0) {
611 		return error;
612 	}
613 	module_count--;
614 	TAILQ_REMOVE(&module_list, mod, mod_chain);
615 	for (i = 0; i < mod->mod_nrequired; i++) {
616 		mod->mod_required[i]->mod_refcnt--;
617 	}
618 	if (mod->mod_kobj != NULL) {
619 		kobj_unload(mod->mod_kobj);
620 	}
621 	kmem_free(mod, sizeof(*mod));
622 
623 	return 0;
624 }
625 
626 /*
627  * module_prime:
628  *
629  *	Push a module loaded by the bootloader onto our internal
630  *	list.
631  */
632 int
633 module_prime(void *base, size_t size)
634 {
635 	module_t *mod;
636 	int error;
637 
638 	mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
639 	if (mod == NULL) {
640 		return ENOMEM;
641 	}
642 	mod->mod_source = MODULE_SOURCE_BOOT;
643 
644 	error = kobj_open_mem(&mod->mod_kobj, base, size);
645 	if (error == 0) {
646 		kmem_free(mod, sizeof(*mod));
647 		module_error("unable to open object pushed by boot loader");
648 		return error;
649 	}
650 	error = kobj_load(mod->mod_kobj);
651 	if (error != 0) {
652 		kmem_free(mod, sizeof(*mod));
653 		module_error("unable to push object pushed by boot loader");
654 		return error;
655 	}
656 	mod->mod_source = MODULE_SOURCE_FILESYS;
657 	TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
658 
659 	return 0;
660 }
661