kern_module.c revision 1.43 1 1.43 ad /* $NetBSD: kern_module.c,v 1.43 2009/05/24 14:54:17 ad Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.25 ad * This code is derived from software developed for The NetBSD Foundation
8 1.25 ad * by Andrew Doran.
9 1.25 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad /*
33 1.2 ad * Kernel module support.
34 1.1 ad */
35 1.1 ad
36 1.1 ad #include <sys/cdefs.h>
37 1.43 ad __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.43 2009/05/24 14:54:17 ad Exp $");
38 1.30 ad
39 1.30 ad #ifdef _KERNEL_OPT
40 1.30 ad #include "opt_ddb.h"
41 1.42 apb #include "opt_modular.h"
42 1.30 ad #endif
43 1.1 ad
44 1.1 ad #include <sys/param.h>
45 1.1 ad #include <sys/systm.h>
46 1.26 ad #include <sys/kernel.h>
47 1.1 ad #include <sys/fcntl.h>
48 1.1 ad #include <sys/proc.h>
49 1.1 ad #include <sys/kauth.h>
50 1.1 ad #include <sys/kobj.h>
51 1.1 ad #include <sys/kmem.h>
52 1.1 ad #include <sys/module.h>
53 1.18 ad #include <sys/kauth.h>
54 1.26 ad #include <sys/kthread.h>
55 1.34 ad #include <sys/sysctl.h>
56 1.1 ad
57 1.1 ad #include <uvm/uvm_extern.h>
58 1.1 ad
59 1.1 ad #include <machine/stdarg.h>
60 1.1 ad
61 1.25 ad struct vm_map *module_map;
62 1.2 ad
63 1.1 ad struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
64 1.1 ad struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
65 1.12 ad static module_t *module_active;
66 1.16 ad static char module_base[64];
67 1.34 ad static int module_verbose_on;
68 1.34 ad static int module_autoload_on = 1;
69 1.1 ad u_int module_count;
70 1.1 ad kmutex_t module_lock;
71 1.26 ad u_int module_autotime = 10;
72 1.26 ad u_int module_gen = 1;
73 1.26 ad static kcondvar_t module_thread_cv;
74 1.26 ad static kmutex_t module_thread_lock;
75 1.26 ad static int module_thread_ticks;
76 1.1 ad
77 1.1 ad /* Ensure that the kernel's link set isn't empty. */
78 1.1 ad static modinfo_t module_dummy;
79 1.1 ad __link_set_add_rodata(modules, module_dummy);
80 1.1 ad
81 1.1 ad static module_t *module_lookup(const char *);
82 1.9 jmmv static int module_do_load(const char *, bool, int, prop_dictionary_t,
83 1.20 ad module_t **, modclass_t class, bool);
84 1.1 ad static int module_do_unload(const char *);
85 1.39 drochner static void module_error(const char *, ...)
86 1.39 drochner __attribute__((__format__(__printf__,1,2)));
87 1.39 drochner static void module_print(const char *, ...)
88 1.39 drochner __attribute__((__format__(__printf__,1,2)));
89 1.1 ad static int module_do_builtin(const char *, module_t **);
90 1.11 ad static int module_fetch_info(module_t *);
91 1.26 ad static void module_thread(void *);
92 1.1 ad
93 1.1 ad /*
94 1.1 ad * module_error:
95 1.1 ad *
96 1.1 ad * Utility function: log an error.
97 1.1 ad */
98 1.1 ad static void
99 1.1 ad module_error(const char *fmt, ...)
100 1.1 ad {
101 1.1 ad va_list ap;
102 1.1 ad
103 1.1 ad va_start(ap, fmt);
104 1.1 ad printf("WARNING: module error: ");
105 1.1 ad vprintf(fmt, ap);
106 1.1 ad printf("\n");
107 1.1 ad va_end(ap);
108 1.1 ad }
109 1.1 ad
110 1.1 ad /*
111 1.34 ad * module_print:
112 1.34 ad *
113 1.34 ad * Utility function: log verbose output.
114 1.34 ad */
115 1.34 ad static void
116 1.34 ad module_print(const char *fmt, ...)
117 1.34 ad {
118 1.34 ad va_list ap;
119 1.34 ad
120 1.34 ad if (module_verbose_on) {
121 1.34 ad va_start(ap, fmt);
122 1.34 ad printf("DEBUG: module: ");
123 1.34 ad vprintf(fmt, ap);
124 1.34 ad printf("\n");
125 1.34 ad va_end(ap);
126 1.34 ad }
127 1.34 ad }
128 1.34 ad
129 1.34 ad /*
130 1.1 ad * module_init:
131 1.1 ad *
132 1.1 ad * Initialize the module subsystem.
133 1.1 ad */
134 1.1 ad void
135 1.1 ad module_init(void)
136 1.1 ad {
137 1.25 ad extern struct vm_map *module_map;
138 1.26 ad int error;
139 1.1 ad
140 1.25 ad if (module_map == NULL) {
141 1.25 ad module_map = kernel_map;
142 1.25 ad }
143 1.1 ad mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
144 1.26 ad cv_init(&module_thread_cv, "modunload");
145 1.26 ad mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
146 1.13 ad #ifdef MODULAR /* XXX */
147 1.11 ad module_init_md();
148 1.12 ad #endif
149 1.16 ad
150 1.16 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
151 1.41 rmind snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
152 1.17 ad machine, osrelease);
153 1.16 ad #else /* release */
154 1.41 rmind snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
155 1.17 ad machine, __NetBSD_Version__ / 100000000,
156 1.16 ad __NetBSD_Version__ / 1000000 % 100);
157 1.16 ad #endif
158 1.26 ad
159 1.26 ad error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
160 1.26 ad NULL, NULL, "modunload");
161 1.26 ad if (error != 0)
162 1.26 ad panic("module_init: %d", error);
163 1.1 ad }
164 1.1 ad
165 1.34 ad SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup")
166 1.34 ad {
167 1.34 ad const struct sysctlnode *node = NULL;
168 1.34 ad
169 1.34 ad sysctl_createv(clog, 0, NULL, NULL,
170 1.34 ad CTLFLAG_PERMANENT,
171 1.34 ad CTLTYPE_NODE, "kern", NULL,
172 1.34 ad NULL, 0, NULL, 0,
173 1.34 ad CTL_KERN, CTL_EOL);
174 1.34 ad sysctl_createv(clog, 0, NULL, &node,
175 1.34 ad CTLFLAG_PERMANENT,
176 1.34 ad CTLTYPE_NODE, "module",
177 1.34 ad SYSCTL_DESCR("Module options"),
178 1.34 ad NULL, 0, NULL, 0,
179 1.34 ad CTL_KERN, CTL_CREATE, CTL_EOL);
180 1.34 ad
181 1.34 ad if (node == NULL)
182 1.34 ad return;
183 1.34 ad
184 1.34 ad sysctl_createv(clog, 0, &node, NULL,
185 1.34 ad CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
186 1.34 ad CTLTYPE_INT, "autoload",
187 1.34 ad SYSCTL_DESCR("Enable automatic load of modules"),
188 1.34 ad NULL, 0, &module_autoload_on, 0,
189 1.34 ad CTL_CREATE, CTL_EOL);
190 1.34 ad sysctl_createv(clog, 0, &node, NULL,
191 1.34 ad CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
192 1.34 ad CTLTYPE_INT, "verbose",
193 1.34 ad SYSCTL_DESCR("Enable verbose output"),
194 1.34 ad NULL, 0, &module_verbose_on, 0,
195 1.34 ad CTL_CREATE, CTL_EOL);
196 1.34 ad }
197 1.34 ad
198 1.1 ad /*
199 1.1 ad * module_init_class:
200 1.1 ad *
201 1.1 ad * Initialize all built-in and pre-loaded modules of the
202 1.1 ad * specified class.
203 1.1 ad */
204 1.1 ad void
205 1.1 ad module_init_class(modclass_t class)
206 1.1 ad {
207 1.1 ad __link_set_decl(modules, modinfo_t);
208 1.1 ad modinfo_t *const *mip, *mi;
209 1.1 ad module_t *mod;
210 1.1 ad
211 1.1 ad mutex_enter(&module_lock);
212 1.1 ad /*
213 1.1 ad * Builtins first. These can't depend on pre-loaded modules.
214 1.1 ad */
215 1.1 ad __link_set_foreach(mip, modules) {
216 1.1 ad mi = *mip;
217 1.1 ad if (mi == &module_dummy) {
218 1.1 ad continue;
219 1.1 ad }
220 1.1 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
221 1.1 ad continue;
222 1.1 ad }
223 1.1 ad (void)module_do_builtin(mi->mi_name, NULL);
224 1.1 ad }
225 1.1 ad /*
226 1.1 ad * Now preloaded modules. These will be pulled off the
227 1.1 ad * list as we call module_do_load();
228 1.1 ad */
229 1.11 ad do {
230 1.11 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
231 1.11 ad mi = mod->mod_info;
232 1.11 ad if (class != MODULE_CLASS_ANY &&
233 1.11 ad class != mi->mi_class)
234 1.11 ad continue;
235 1.18 ad module_do_load(mi->mi_name, false, 0, NULL, NULL,
236 1.38 christos class, false);
237 1.11 ad break;
238 1.11 ad }
239 1.11 ad } while (mod != NULL);
240 1.1 ad mutex_exit(&module_lock);
241 1.1 ad }
242 1.1 ad
243 1.1 ad /*
244 1.21 ad * module_compatible:
245 1.1 ad *
246 1.21 ad * Return true if the two supplied kernel versions are said to
247 1.21 ad * have the same binary interface for kernel code. The entire
248 1.21 ad * version is signficant for the development tree (-current),
249 1.21 ad * major and minor versions are significant for official
250 1.21 ad * releases of the system.
251 1.1 ad */
252 1.22 pooka bool
253 1.21 ad module_compatible(int v1, int v2)
254 1.1 ad {
255 1.1 ad
256 1.21 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
257 1.21 ad return v1 == v2;
258 1.21 ad #else /* release */
259 1.21 ad return abs(v1 - v2) < 10000;
260 1.21 ad #endif
261 1.1 ad }
262 1.1 ad
263 1.1 ad /*
264 1.1 ad * module_load:
265 1.1 ad *
266 1.9 jmmv * Load a single module from the file system.
267 1.1 ad */
268 1.1 ad int
269 1.18 ad module_load(const char *filename, int flags, prop_dictionary_t props,
270 1.23 ad modclass_t class)
271 1.1 ad {
272 1.1 ad int error;
273 1.1 ad
274 1.18 ad /* Authorize. */
275 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
276 1.18 ad 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
277 1.18 ad if (error != 0) {
278 1.18 ad return error;
279 1.18 ad }
280 1.18 ad
281 1.1 ad mutex_enter(&module_lock);
282 1.20 ad error = module_do_load(filename, false, flags, props, NULL, class,
283 1.23 ad false);
284 1.1 ad mutex_exit(&module_lock);
285 1.1 ad
286 1.1 ad return error;
287 1.1 ad }
288 1.1 ad
289 1.1 ad /*
290 1.23 ad * module_autoload:
291 1.23 ad *
292 1.23 ad * Load a single module from the file system, system initiated.
293 1.23 ad */
294 1.23 ad int
295 1.23 ad module_autoload(const char *filename, modclass_t class)
296 1.23 ad {
297 1.23 ad int error;
298 1.23 ad
299 1.23 ad KASSERT(mutex_owned(&module_lock));
300 1.23 ad
301 1.34 ad /* Nothing if the user has disabled it. */
302 1.34 ad if (!module_autoload_on) {
303 1.34 ad return EPERM;
304 1.34 ad }
305 1.34 ad
306 1.29 ad /* Disallow path seperators and magic symlinks. */
307 1.29 ad if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
308 1.29 ad strchr(filename, '.') != NULL) {
309 1.29 ad return EPERM;
310 1.29 ad }
311 1.29 ad
312 1.23 ad /* Authorize. */
313 1.23 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
314 1.23 ad 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
315 1.23 ad if (error != 0) {
316 1.23 ad return error;
317 1.23 ad }
318 1.23 ad
319 1.23 ad return module_do_load(filename, false, 0, NULL, NULL, class, true);
320 1.23 ad }
321 1.23 ad
322 1.23 ad /*
323 1.1 ad * module_unload:
324 1.1 ad *
325 1.1 ad * Find and unload a module by name.
326 1.1 ad */
327 1.1 ad int
328 1.1 ad module_unload(const char *name)
329 1.1 ad {
330 1.1 ad int error;
331 1.1 ad
332 1.18 ad /* Authorize. */
333 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
334 1.18 ad 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
335 1.18 ad if (error != 0) {
336 1.18 ad return error;
337 1.18 ad }
338 1.18 ad
339 1.1 ad mutex_enter(&module_lock);
340 1.1 ad error = module_do_unload(name);
341 1.1 ad mutex_exit(&module_lock);
342 1.1 ad
343 1.1 ad return error;
344 1.1 ad }
345 1.1 ad
346 1.1 ad /*
347 1.1 ad * module_lookup:
348 1.1 ad *
349 1.1 ad * Look up a module by name.
350 1.1 ad */
351 1.1 ad module_t *
352 1.1 ad module_lookup(const char *name)
353 1.1 ad {
354 1.1 ad module_t *mod;
355 1.1 ad
356 1.1 ad KASSERT(mutex_owned(&module_lock));
357 1.1 ad
358 1.1 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
359 1.1 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
360 1.1 ad break;
361 1.1 ad }
362 1.1 ad }
363 1.1 ad
364 1.1 ad return mod;
365 1.1 ad }
366 1.1 ad
367 1.1 ad /*
368 1.1 ad * module_hold:
369 1.1 ad *
370 1.1 ad * Add a single reference to a module. It's the caller's
371 1.1 ad * responsibility to ensure that the reference is dropped
372 1.1 ad * later.
373 1.1 ad */
374 1.1 ad int
375 1.1 ad module_hold(const char *name)
376 1.1 ad {
377 1.1 ad module_t *mod;
378 1.1 ad
379 1.1 ad mutex_enter(&module_lock);
380 1.1 ad mod = module_lookup(name);
381 1.1 ad if (mod == NULL) {
382 1.1 ad mutex_exit(&module_lock);
383 1.1 ad return ENOENT;
384 1.1 ad }
385 1.1 ad mod->mod_refcnt++;
386 1.1 ad mutex_exit(&module_lock);
387 1.1 ad
388 1.1 ad return 0;
389 1.1 ad }
390 1.1 ad
391 1.1 ad /*
392 1.1 ad * module_rele:
393 1.1 ad *
394 1.1 ad * Release a reference acquired with module_hold().
395 1.1 ad */
396 1.1 ad void
397 1.1 ad module_rele(const char *name)
398 1.1 ad {
399 1.1 ad module_t *mod;
400 1.1 ad
401 1.1 ad mutex_enter(&module_lock);
402 1.1 ad mod = module_lookup(name);
403 1.1 ad if (mod == NULL) {
404 1.1 ad mutex_exit(&module_lock);
405 1.1 ad panic("module_rele: gone");
406 1.1 ad }
407 1.1 ad mod->mod_refcnt--;
408 1.1 ad mutex_exit(&module_lock);
409 1.1 ad }
410 1.1 ad
411 1.1 ad /*
412 1.27 ad * module_enqueue:
413 1.27 ad *
414 1.27 ad * Put a module onto the global list and update counters.
415 1.27 ad */
416 1.27 ad static void
417 1.27 ad module_enqueue(module_t *mod)
418 1.27 ad {
419 1.27 ad int i;
420 1.27 ad
421 1.27 ad /*
422 1.27 ad * If there are requisite modules, put at the head of the queue.
423 1.27 ad * This is so that autounload can unload requisite modules with
424 1.27 ad * only one pass through the queue.
425 1.27 ad */
426 1.27 ad if (mod->mod_nrequired) {
427 1.27 ad TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
428 1.27 ad
429 1.27 ad /* Add references to the requisite modules. */
430 1.27 ad for (i = 0; i < mod->mod_nrequired; i++) {
431 1.27 ad KASSERT(mod->mod_required[i] != NULL);
432 1.27 ad mod->mod_required[i]->mod_refcnt++;
433 1.27 ad }
434 1.27 ad } else {
435 1.27 ad TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
436 1.27 ad }
437 1.27 ad module_count++;
438 1.27 ad module_gen++;
439 1.27 ad }
440 1.27 ad
441 1.27 ad /*
442 1.1 ad * module_do_builtin:
443 1.1 ad *
444 1.1 ad * Initialize a single module from the list of modules that are
445 1.1 ad * built into the kernel (linked into the kernel image).
446 1.1 ad */
447 1.1 ad static int
448 1.1 ad module_do_builtin(const char *name, module_t **modp)
449 1.1 ad {
450 1.1 ad __link_set_decl(modules, modinfo_t);
451 1.1 ad modinfo_t *const *mip;
452 1.1 ad const char *p, *s;
453 1.1 ad char buf[MAXMODNAME];
454 1.1 ad modinfo_t *mi;
455 1.1 ad module_t *mod, *mod2;
456 1.1 ad size_t len;
457 1.27 ad int error;
458 1.1 ad
459 1.1 ad KASSERT(mutex_owned(&module_lock));
460 1.1 ad
461 1.1 ad /*
462 1.1 ad * Check to see if already loaded.
463 1.1 ad */
464 1.1 ad if ((mod = module_lookup(name)) != NULL) {
465 1.1 ad if (modp != NULL) {
466 1.1 ad *modp = mod;
467 1.1 ad }
468 1.1 ad return 0;
469 1.1 ad }
470 1.1 ad
471 1.1 ad /*
472 1.1 ad * Search the list to see if we have a module by this name.
473 1.1 ad */
474 1.1 ad error = ENOENT;
475 1.1 ad __link_set_foreach(mip, modules) {
476 1.1 ad mi = *mip;
477 1.1 ad if (mi == &module_dummy) {
478 1.1 ad continue;
479 1.1 ad }
480 1.1 ad if (strcmp(mi->mi_name, name) == 0) {
481 1.1 ad error = 0;
482 1.1 ad break;
483 1.1 ad }
484 1.1 ad }
485 1.1 ad if (error != 0) {
486 1.38 christos module_error("can't find `%s'", name);
487 1.1 ad return error;
488 1.1 ad }
489 1.1 ad
490 1.1 ad /*
491 1.1 ad * Initialize pre-requisites.
492 1.1 ad */
493 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
494 1.1 ad if (mod == NULL) {
495 1.38 christos module_error("out of memory for `%s'", name);
496 1.1 ad return ENOMEM;
497 1.1 ad }
498 1.1 ad if (modp != NULL) {
499 1.1 ad *modp = mod;
500 1.1 ad }
501 1.1 ad if (mi->mi_required != NULL) {
502 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
503 1.1 ad if (*s == ',')
504 1.1 ad s++;
505 1.1 ad p = s;
506 1.1 ad while (*p != '\0' && *p != ',')
507 1.1 ad p++;
508 1.1 ad len = min(p - s + 1, sizeof(buf));
509 1.1 ad strlcpy(buf, s, len);
510 1.1 ad if (buf[0] == '\0')
511 1.1 ad break;
512 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
513 1.1 ad module_error("too many required modules");
514 1.1 ad kmem_free(mod, sizeof(*mod));
515 1.1 ad return EINVAL;
516 1.1 ad }
517 1.1 ad error = module_do_builtin(buf, &mod2);
518 1.1 ad if (error != 0) {
519 1.1 ad kmem_free(mod, sizeof(*mod));
520 1.1 ad return error;
521 1.1 ad }
522 1.1 ad mod->mod_required[mod->mod_nrequired++] = mod2;
523 1.1 ad }
524 1.1 ad }
525 1.1 ad
526 1.1 ad /*
527 1.1 ad * Try to initialize the module.
528 1.1 ad */
529 1.12 ad KASSERT(module_active == NULL);
530 1.12 ad module_active = mod;
531 1.1 ad error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
532 1.12 ad module_active = NULL;
533 1.1 ad if (error != 0) {
534 1.1 ad module_error("builtin module `%s' "
535 1.1 ad "failed to init", mi->mi_name);
536 1.1 ad kmem_free(mod, sizeof(*mod));
537 1.1 ad return error;
538 1.1 ad }
539 1.1 ad mod->mod_info = mi;
540 1.1 ad mod->mod_source = MODULE_SOURCE_KERNEL;
541 1.27 ad module_enqueue(mod);
542 1.1 ad return 0;
543 1.1 ad }
544 1.1 ad
545 1.1 ad /*
546 1.1 ad * module_do_load:
547 1.1 ad *
548 1.1 ad * Helper routine: load a module from the file system, or one
549 1.1 ad * pushed by the boot loader.
550 1.1 ad */
551 1.1 ad static int
552 1.27 ad module_do_load(const char *name, bool isdep, int flags,
553 1.20 ad prop_dictionary_t props, module_t **modp, modclass_t class,
554 1.20 ad bool autoload)
555 1.1 ad {
556 1.1 ad static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
557 1.1 ad static int depth;
558 1.1 ad const int maxdepth = 6;
559 1.1 ad modinfo_t *mi;
560 1.1 ad module_t *mod, *mod2;
561 1.1 ad char buf[MAXMODNAME];
562 1.1 ad const char *s, *p;
563 1.1 ad int error;
564 1.1 ad size_t len;
565 1.1 ad
566 1.1 ad KASSERT(mutex_owned(&module_lock));
567 1.1 ad
568 1.1 ad error = 0;
569 1.1 ad
570 1.1 ad /*
571 1.1 ad * Avoid recursing too far.
572 1.1 ad */
573 1.1 ad if (++depth > maxdepth) {
574 1.1 ad module_error("too many required modules");
575 1.1 ad depth--;
576 1.1 ad return EMLINK;
577 1.1 ad }
578 1.1 ad
579 1.1 ad /*
580 1.1 ad * Load the module and link. Before going to the file system,
581 1.1 ad * scan the list of modules loaded by the boot loader. Just
582 1.1 ad * before init is started the list of modules loaded at boot
583 1.1 ad * will be purged. Before init is started we can assume that
584 1.27 ad * `name' is a module name and not a path name.
585 1.1 ad */
586 1.1 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
587 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
588 1.1 ad TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
589 1.1 ad break;
590 1.1 ad }
591 1.1 ad }
592 1.14 rumble if (mod != NULL) {
593 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
594 1.14 rumble } else {
595 1.27 ad /*
596 1.27 ad * If a requisite module, check to see if it is
597 1.27 ad * already present.
598 1.27 ad */
599 1.27 ad if (isdep) {
600 1.27 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
601 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
602 1.27 ad break;
603 1.27 ad }
604 1.27 ad }
605 1.27 ad if (mod != NULL) {
606 1.27 ad if (modp != NULL) {
607 1.27 ad *modp = mod;
608 1.27 ad }
609 1.27 ad depth--;
610 1.27 ad return 0;
611 1.27 ad }
612 1.27 ad }
613 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
614 1.1 ad if (mod == NULL) {
615 1.38 christos module_error("out of memory for `%s'", name);
616 1.1 ad depth--;
617 1.1 ad return ENOMEM;
618 1.1 ad }
619 1.27 ad error = kobj_load_file(&mod->mod_kobj, name, module_base,
620 1.20 ad autoload);
621 1.1 ad if (error != 0) {
622 1.1 ad kmem_free(mod, sizeof(*mod));
623 1.1 ad depth--;
624 1.34 ad if (autoload) {
625 1.34 ad module_print("Cannot load kernel object `%s'"
626 1.34 ad " error=%d", name, error);
627 1.34 ad } else {
628 1.32 christos module_error("Cannot load kernel object `%s'"
629 1.33 ad " error=%d", name, error);
630 1.34 ad }
631 1.8 rumble return error;
632 1.7 rumble }
633 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
634 1.1 ad mod->mod_source = MODULE_SOURCE_FILESYS;
635 1.11 ad error = module_fetch_info(mod);
636 1.11 ad if (error != 0) {
637 1.33 ad module_error("cannot fetch module info for `%s'",
638 1.33 ad name);
639 1.11 ad goto fail;
640 1.11 ad }
641 1.1 ad }
642 1.1 ad
643 1.1 ad /*
644 1.11 ad * Check compatibility.
645 1.1 ad */
646 1.1 ad mi = mod->mod_info;
647 1.3 rumble if (strlen(mi->mi_name) >= MAXMODNAME) {
648 1.3 rumble error = EINVAL;
649 1.32 christos module_error("module name `%s' too long", mi->mi_name);
650 1.3 rumble goto fail;
651 1.3 rumble }
652 1.21 ad if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
653 1.33 ad module_error("module built for `%d', system `%d'",
654 1.32 christos mi->mi_version, __NetBSD_Version__);
655 1.24 ad if ((flags & MODCTL_LOAD_FORCE) != 0) {
656 1.24 ad module_error("forced load, system may be unstable");
657 1.24 ad } else {
658 1.24 ad error = EPROGMISMATCH;
659 1.24 ad goto fail;
660 1.24 ad }
661 1.21 ad }
662 1.3 rumble
663 1.1 ad /*
664 1.18 ad * If a specific kind of module was requested, ensure that we have
665 1.18 ad * a match.
666 1.18 ad */
667 1.18 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
668 1.34 ad module_print("incompatible module class for `%s' (%d != %d)",
669 1.32 christos name, class, mi->mi_class);
670 1.18 ad error = ENOENT;
671 1.18 ad goto fail;
672 1.18 ad }
673 1.18 ad
674 1.18 ad /*
675 1.27 ad * If loading a dependency, `name' is a plain module name.
676 1.1 ad * The name must match.
677 1.1 ad */
678 1.27 ad if (isdep && strcmp(mi->mi_name, name) != 0) {
679 1.32 christos module_error("dependency name mismatch (`%s' != `%s')",
680 1.32 christos name, mi->mi_name);
681 1.1 ad error = ENOENT;
682 1.1 ad goto fail;
683 1.1 ad }
684 1.1 ad
685 1.1 ad /*
686 1.3 rumble * Check to see if the module is already loaded. If so, we may
687 1.3 rumble * have been recursively called to handle a dependency, so be sure
688 1.3 rumble * to set modp.
689 1.1 ad */
690 1.3 rumble if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
691 1.5 rumble if (modp != NULL)
692 1.5 rumble *modp = mod2;
693 1.34 ad module_print("module `%s' already loaded", mi->mi_name);
694 1.1 ad error = EEXIST;
695 1.1 ad goto fail;
696 1.1 ad }
697 1.3 rumble
698 1.3 rumble /*
699 1.3 rumble * Block circular dependencies.
700 1.3 rumble */
701 1.1 ad TAILQ_FOREACH(mod2, &pending, mod_chain) {
702 1.1 ad if (mod == mod2) {
703 1.1 ad continue;
704 1.1 ad }
705 1.1 ad if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
706 1.1 ad error = EDEADLK;
707 1.32 christos module_error("circular dependency detected for `%s'",
708 1.32 christos mi->mi_name);
709 1.1 ad goto fail;
710 1.1 ad }
711 1.1 ad }
712 1.1 ad
713 1.1 ad /*
714 1.1 ad * Now try to load any requisite modules.
715 1.1 ad */
716 1.1 ad if (mi->mi_required != NULL) {
717 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
718 1.1 ad if (*s == ',')
719 1.1 ad s++;
720 1.1 ad p = s;
721 1.1 ad while (*p != '\0' && *p != ',')
722 1.1 ad p++;
723 1.3 rumble len = p - s + 1;
724 1.3 rumble if (len >= MAXMODNAME) {
725 1.3 rumble error = EINVAL;
726 1.32 christos module_error("required module name `%s'"
727 1.32 christos " too long", mi->mi_required);
728 1.3 rumble goto fail;
729 1.3 rumble }
730 1.1 ad strlcpy(buf, s, len);
731 1.1 ad if (buf[0] == '\0')
732 1.1 ad break;
733 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
734 1.1 ad error = EINVAL;
735 1.32 christos module_error("too many required modules (%d)",
736 1.32 christos mod->mod_nrequired);
737 1.1 ad goto fail;
738 1.1 ad }
739 1.6 rumble if (strcmp(buf, mi->mi_name) == 0) {
740 1.6 rumble error = EDEADLK;
741 1.32 christos module_error("self-dependency detected for "
742 1.32 christos "`%s'", mi->mi_name);
743 1.6 rumble goto fail;
744 1.6 rumble }
745 1.9 jmmv error = module_do_load(buf, true, flags, NULL,
746 1.18 ad &mod->mod_required[mod->mod_nrequired++],
747 1.20 ad MODULE_CLASS_ANY, true);
748 1.27 ad if (error != 0)
749 1.1 ad goto fail;
750 1.1 ad }
751 1.1 ad }
752 1.1 ad
753 1.1 ad /*
754 1.15 ad * We loaded all needed modules successfully: perform global
755 1.15 ad * relocations and initialize.
756 1.1 ad */
757 1.15 ad error = kobj_affix(mod->mod_kobj, mi->mi_name);
758 1.15 ad if (error != 0) {
759 1.36 ad /* Cannot touch 'mi' as the module is now gone. */
760 1.36 ad module_error("unable to affix module `%s'", name);
761 1.15 ad goto fail2;
762 1.15 ad }
763 1.15 ad
764 1.12 ad KASSERT(module_active == NULL);
765 1.12 ad module_active = mod;
766 1.9 jmmv error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
767 1.12 ad module_active = NULL;
768 1.1 ad if (error != 0) {
769 1.32 christos module_error("modcmd function returned error %d for `%s'",
770 1.32 christos error, mi->mi_name);
771 1.1 ad goto fail;
772 1.1 ad }
773 1.1 ad
774 1.1 ad /*
775 1.1 ad * Good, the module loaded successfully. Put it onto the
776 1.1 ad * list and add references to its requisite modules.
777 1.1 ad */
778 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
779 1.27 ad module_enqueue(mod);
780 1.1 ad if (modp != NULL) {
781 1.1 ad *modp = mod;
782 1.1 ad }
783 1.26 ad if (autoload) {
784 1.26 ad /*
785 1.26 ad * Arrange to try unloading the module after
786 1.26 ad * a short delay.
787 1.26 ad */
788 1.26 ad mod->mod_autotime = time_second + module_autotime;
789 1.26 ad module_thread_kick();
790 1.26 ad }
791 1.1 ad depth--;
792 1.1 ad return 0;
793 1.7 rumble
794 1.1 ad fail:
795 1.15 ad kobj_unload(mod->mod_kobj);
796 1.15 ad fail2:
797 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
798 1.1 ad kmem_free(mod, sizeof(*mod));
799 1.1 ad depth--;
800 1.1 ad return error;
801 1.1 ad }
802 1.1 ad
803 1.1 ad /*
804 1.1 ad * module_do_unload:
805 1.1 ad *
806 1.1 ad * Helper routine: do the dirty work of unloading a module.
807 1.1 ad */
808 1.1 ad static int
809 1.1 ad module_do_unload(const char *name)
810 1.1 ad {
811 1.1 ad module_t *mod;
812 1.1 ad int error;
813 1.1 ad u_int i;
814 1.1 ad
815 1.1 ad KASSERT(mutex_owned(&module_lock));
816 1.1 ad
817 1.1 ad mod = module_lookup(name);
818 1.1 ad if (mod == NULL) {
819 1.32 christos module_error("module `%s' not found", name);
820 1.1 ad return ENOENT;
821 1.1 ad }
822 1.1 ad if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
823 1.34 ad module_print("module `%s' busy", name);
824 1.1 ad return EBUSY;
825 1.1 ad }
826 1.12 ad KASSERT(module_active == NULL);
827 1.12 ad module_active = mod;
828 1.1 ad error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
829 1.12 ad module_active = NULL;
830 1.1 ad if (error != 0) {
831 1.34 ad module_print("cannot unload module `%s' error=%d", name,
832 1.33 ad error);
833 1.1 ad return error;
834 1.1 ad }
835 1.1 ad module_count--;
836 1.1 ad TAILQ_REMOVE(&module_list, mod, mod_chain);
837 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
838 1.1 ad mod->mod_required[i]->mod_refcnt--;
839 1.1 ad }
840 1.1 ad if (mod->mod_kobj != NULL) {
841 1.1 ad kobj_unload(mod->mod_kobj);
842 1.1 ad }
843 1.1 ad kmem_free(mod, sizeof(*mod));
844 1.26 ad module_gen++;
845 1.1 ad
846 1.1 ad return 0;
847 1.1 ad }
848 1.1 ad
849 1.1 ad /*
850 1.1 ad * module_prime:
851 1.1 ad *
852 1.1 ad * Push a module loaded by the bootloader onto our internal
853 1.1 ad * list.
854 1.1 ad */
855 1.1 ad int
856 1.1 ad module_prime(void *base, size_t size)
857 1.1 ad {
858 1.1 ad module_t *mod;
859 1.1 ad int error;
860 1.1 ad
861 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
862 1.1 ad if (mod == NULL) {
863 1.1 ad return ENOMEM;
864 1.1 ad }
865 1.1 ad mod->mod_source = MODULE_SOURCE_BOOT;
866 1.1 ad
867 1.15 ad error = kobj_load_mem(&mod->mod_kobj, base, size);
868 1.1 ad if (error != 0) {
869 1.11 ad kmem_free(mod, sizeof(*mod));
870 1.11 ad module_error("unable to load object pushed by boot loader");
871 1.11 ad return error;
872 1.11 ad }
873 1.11 ad error = module_fetch_info(mod);
874 1.11 ad if (error != 0) {
875 1.11 ad kobj_unload(mod->mod_kobj);
876 1.1 ad kmem_free(mod, sizeof(*mod));
877 1.11 ad module_error("unable to load object pushed by boot loader");
878 1.1 ad return error;
879 1.1 ad }
880 1.11 ad
881 1.1 ad TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
882 1.1 ad
883 1.1 ad return 0;
884 1.1 ad }
885 1.11 ad
886 1.11 ad /*
887 1.11 ad * module_fetch_into:
888 1.11 ad *
889 1.11 ad * Fetch modinfo record from a loaded module.
890 1.11 ad */
891 1.11 ad static int
892 1.11 ad module_fetch_info(module_t *mod)
893 1.11 ad {
894 1.11 ad int error;
895 1.11 ad void *addr;
896 1.11 ad size_t size;
897 1.11 ad
898 1.11 ad /*
899 1.11 ad * Find module info record and check compatibility.
900 1.11 ad */
901 1.11 ad error = kobj_find_section(mod->mod_kobj, "link_set_modules",
902 1.11 ad &addr, &size);
903 1.11 ad if (error != 0) {
904 1.11 ad module_error("`link_set_modules' section not present");
905 1.11 ad return error;
906 1.11 ad }
907 1.11 ad if (size != sizeof(modinfo_t **)) {
908 1.11 ad module_error("`link_set_modules' section wrong size");
909 1.11 ad return error;
910 1.11 ad }
911 1.11 ad mod->mod_info = *(modinfo_t **)addr;
912 1.11 ad
913 1.11 ad return 0;
914 1.11 ad }
915 1.12 ad
916 1.12 ad /*
917 1.12 ad * module_find_section:
918 1.12 ad *
919 1.12 ad * Allows a module that is being initialized to look up a section
920 1.12 ad * within its ELF object.
921 1.12 ad */
922 1.12 ad int
923 1.12 ad module_find_section(const char *name, void **addr, size_t *size)
924 1.12 ad {
925 1.12 ad
926 1.12 ad KASSERT(mutex_owned(&module_lock));
927 1.12 ad KASSERT(module_active != NULL);
928 1.12 ad
929 1.12 ad return kobj_find_section(module_active->mod_kobj, name, addr, size);
930 1.12 ad }
931 1.26 ad
932 1.26 ad /*
933 1.26 ad * module_thread:
934 1.26 ad *
935 1.26 ad * Automatically unload modules. We try once to unload autoloaded
936 1.26 ad * modules after module_autotime seconds. If the system is under
937 1.26 ad * severe memory pressure, we'll try unloading all modules.
938 1.26 ad */
939 1.26 ad static void
940 1.26 ad module_thread(void *cookie)
941 1.26 ad {
942 1.26 ad module_t *mod, *next;
943 1.28 ad modinfo_t *mi;
944 1.28 ad int error;
945 1.26 ad
946 1.26 ad for (;;) {
947 1.26 ad mutex_enter(&module_lock);
948 1.26 ad for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
949 1.26 ad next = TAILQ_NEXT(mod, mod_chain);
950 1.37 ad if (uvmexp.free < uvmexp.freemin) {
951 1.26 ad module_thread_ticks = hz;
952 1.26 ad } else if (mod->mod_autotime == 0) {
953 1.26 ad continue;
954 1.26 ad } else if (time_second < mod->mod_autotime) {
955 1.26 ad module_thread_ticks = hz;
956 1.26 ad continue;
957 1.37 ad } else {
958 1.26 ad mod->mod_autotime = 0;
959 1.37 ad }
960 1.28 ad /*
961 1.28 ad * If this module wants to avoid autounload then
962 1.28 ad * skip it. Some modules can ping-pong in and out
963 1.28 ad * because their use is transient but often.
964 1.28 ad * Example: exec_script.
965 1.28 ad */
966 1.28 ad mi = mod->mod_info;
967 1.28 ad error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
968 1.28 ad if (error == 0 || error == ENOTTY) {
969 1.37 ad (void)module_do_unload(mi->mi_name);
970 1.28 ad }
971 1.26 ad }
972 1.26 ad mutex_exit(&module_lock);
973 1.26 ad
974 1.26 ad mutex_enter(&module_thread_lock);
975 1.26 ad (void)cv_timedwait(&module_thread_cv, &module_thread_lock,
976 1.26 ad module_thread_ticks);
977 1.26 ad module_thread_ticks = 0;
978 1.26 ad mutex_exit(&module_thread_lock);
979 1.26 ad }
980 1.26 ad }
981 1.26 ad
982 1.26 ad /*
983 1.26 ad * module_thread:
984 1.26 ad *
985 1.26 ad * Kick the module thread into action, perhaps because the
986 1.26 ad * system is low on memory.
987 1.26 ad */
988 1.26 ad void
989 1.26 ad module_thread_kick(void)
990 1.26 ad {
991 1.26 ad
992 1.26 ad mutex_enter(&module_thread_lock);
993 1.26 ad module_thread_ticks = hz;
994 1.26 ad cv_broadcast(&module_thread_cv);
995 1.26 ad mutex_exit(&module_thread_lock);
996 1.26 ad }
997 1.30 ad
998 1.30 ad #ifdef DDB
999 1.30 ad /*
1000 1.30 ad * module_whatis:
1001 1.30 ad *
1002 1.30 ad * Helper routine for DDB.
1003 1.30 ad */
1004 1.30 ad void
1005 1.30 ad module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
1006 1.30 ad {
1007 1.30 ad module_t *mod;
1008 1.30 ad size_t msize;
1009 1.30 ad vaddr_t maddr;
1010 1.30 ad
1011 1.30 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
1012 1.43 ad if (mod->mod_kobj == NULL) {
1013 1.43 ad continue;
1014 1.43 ad }
1015 1.30 ad kobj_stat(mod->mod_kobj, &maddr, &msize);
1016 1.30 ad if (addr < maddr || addr >= maddr + msize) {
1017 1.30 ad continue;
1018 1.30 ad }
1019 1.30 ad (*pr)("%p is %p+%zu, in kernel module `%s'\n",
1020 1.30 ad (void *)addr, (void *)maddr,
1021 1.30 ad (size_t)(addr - maddr), mod->mod_info->mi_name);
1022 1.30 ad }
1023 1.30 ad }
1024 1.30 ad
1025 1.30 ad /*
1026 1.30 ad * module_print_list:
1027 1.30 ad *
1028 1.30 ad * Helper routine for DDB.
1029 1.30 ad */
1030 1.30 ad void
1031 1.30 ad module_print_list(void (*pr)(const char *, ...))
1032 1.30 ad {
1033 1.30 ad const char *src;
1034 1.30 ad module_t *mod;
1035 1.30 ad size_t msize;
1036 1.30 ad vaddr_t maddr;
1037 1.30 ad
1038 1.30 ad (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
1039 1.30 ad
1040 1.30 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
1041 1.30 ad switch (mod->mod_source) {
1042 1.30 ad case MODULE_SOURCE_KERNEL:
1043 1.30 ad src = "builtin";
1044 1.30 ad break;
1045 1.30 ad case MODULE_SOURCE_FILESYS:
1046 1.30 ad src = "filesys";
1047 1.30 ad break;
1048 1.30 ad case MODULE_SOURCE_BOOT:
1049 1.30 ad src = "boot";
1050 1.30 ad break;
1051 1.30 ad default:
1052 1.30 ad src = "unknown";
1053 1.30 ad break;
1054 1.30 ad }
1055 1.43 ad if (mod->mod_kobj != NULL) {
1056 1.43 ad kobj_stat(mod->mod_kobj, &maddr, &msize);
1057 1.43 ad } else {
1058 1.43 ad maddr = 0;
1059 1.43 ad msize = 0;
1060 1.43 ad }
1061 1.31 ad (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
1062 1.30 ad (long)maddr, (long)msize, src);
1063 1.30 ad }
1064 1.30 ad }
1065 1.30 ad #endif /* DDB */
1066