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