kern_module.c revision 1.29 1 1.29 ad /* $NetBSD: kern_module.c,v 1.29 2008/11/19 13:07:42 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.29 ad __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.29 2008/11/19 13:07:42 ad Exp $");
38 1.1 ad
39 1.1 ad #include <sys/param.h>
40 1.1 ad #include <sys/systm.h>
41 1.26 ad #include <sys/kernel.h>
42 1.1 ad #include <sys/fcntl.h>
43 1.1 ad #include <sys/proc.h>
44 1.1 ad #include <sys/kauth.h>
45 1.1 ad #include <sys/kobj.h>
46 1.1 ad #include <sys/kmem.h>
47 1.1 ad #include <sys/module.h>
48 1.18 ad #include <sys/kauth.h>
49 1.26 ad #include <sys/kthread.h>
50 1.1 ad
51 1.1 ad #include <uvm/uvm_extern.h>
52 1.1 ad
53 1.1 ad #include <machine/stdarg.h>
54 1.1 ad
55 1.25 ad struct vm_map *module_map;
56 1.2 ad
57 1.1 ad struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
58 1.1 ad struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
59 1.12 ad static module_t *module_active;
60 1.16 ad static char module_base[64];
61 1.1 ad u_int module_count;
62 1.1 ad kmutex_t module_lock;
63 1.26 ad u_int module_autotime = 10;
64 1.26 ad u_int module_gen = 1;
65 1.26 ad static kcondvar_t module_thread_cv;
66 1.26 ad static kmutex_t module_thread_lock;
67 1.26 ad static int module_thread_ticks;
68 1.1 ad
69 1.1 ad /* Ensure that the kernel's link set isn't empty. */
70 1.1 ad static modinfo_t module_dummy;
71 1.1 ad __link_set_add_rodata(modules, module_dummy);
72 1.1 ad
73 1.1 ad static module_t *module_lookup(const char *);
74 1.9 jmmv static int module_do_load(const char *, bool, int, prop_dictionary_t,
75 1.20 ad module_t **, modclass_t class, bool);
76 1.1 ad static int module_do_unload(const char *);
77 1.1 ad static void module_error(const char *, ...);
78 1.1 ad static int module_do_builtin(const char *, module_t **);
79 1.11 ad static int module_fetch_info(module_t *);
80 1.26 ad static void module_thread(void *);
81 1.1 ad
82 1.1 ad /*
83 1.1 ad * module_error:
84 1.1 ad *
85 1.1 ad * Utility function: log an error.
86 1.1 ad */
87 1.1 ad static void
88 1.1 ad module_error(const char *fmt, ...)
89 1.1 ad {
90 1.1 ad va_list ap;
91 1.1 ad
92 1.1 ad va_start(ap, fmt);
93 1.1 ad printf("WARNING: module error: ");
94 1.1 ad vprintf(fmt, ap);
95 1.1 ad printf("\n");
96 1.1 ad va_end(ap);
97 1.1 ad }
98 1.1 ad
99 1.1 ad /*
100 1.1 ad * module_init:
101 1.1 ad *
102 1.1 ad * Initialize the module subsystem.
103 1.1 ad */
104 1.1 ad void
105 1.1 ad module_init(void)
106 1.1 ad {
107 1.25 ad extern struct vm_map *module_map;
108 1.26 ad int error;
109 1.1 ad
110 1.25 ad if (module_map == NULL) {
111 1.25 ad module_map = kernel_map;
112 1.25 ad }
113 1.1 ad mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
114 1.26 ad cv_init(&module_thread_cv, "modunload");
115 1.26 ad mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
116 1.13 ad #ifdef MODULAR /* XXX */
117 1.11 ad module_init_md();
118 1.12 ad #endif
119 1.16 ad
120 1.16 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
121 1.17 ad snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
122 1.17 ad machine, osrelease);
123 1.16 ad #else /* release */
124 1.17 ad snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
125 1.17 ad machine, __NetBSD_Version__ / 100000000,
126 1.16 ad __NetBSD_Version__ / 1000000 % 100);
127 1.16 ad #endif
128 1.26 ad
129 1.26 ad error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
130 1.26 ad NULL, NULL, "modunload");
131 1.26 ad if (error != 0)
132 1.26 ad panic("module_init: %d", error);
133 1.1 ad }
134 1.1 ad
135 1.1 ad /*
136 1.1 ad * module_init_class:
137 1.1 ad *
138 1.1 ad * Initialize all built-in and pre-loaded modules of the
139 1.1 ad * specified class.
140 1.1 ad */
141 1.1 ad void
142 1.1 ad module_init_class(modclass_t class)
143 1.1 ad {
144 1.1 ad __link_set_decl(modules, modinfo_t);
145 1.1 ad modinfo_t *const *mip, *mi;
146 1.1 ad module_t *mod;
147 1.1 ad
148 1.1 ad mutex_enter(&module_lock);
149 1.1 ad /*
150 1.1 ad * Builtins first. These can't depend on pre-loaded modules.
151 1.1 ad */
152 1.1 ad __link_set_foreach(mip, modules) {
153 1.1 ad mi = *mip;
154 1.1 ad if (mi == &module_dummy) {
155 1.1 ad continue;
156 1.1 ad }
157 1.1 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
158 1.1 ad continue;
159 1.1 ad }
160 1.1 ad (void)module_do_builtin(mi->mi_name, NULL);
161 1.1 ad }
162 1.1 ad /*
163 1.1 ad * Now preloaded modules. These will be pulled off the
164 1.1 ad * list as we call module_do_load();
165 1.1 ad */
166 1.11 ad do {
167 1.11 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
168 1.11 ad mi = mod->mod_info;
169 1.11 ad if (class != MODULE_CLASS_ANY &&
170 1.11 ad class != mi->mi_class)
171 1.11 ad continue;
172 1.18 ad module_do_load(mi->mi_name, false, 0, NULL, NULL,
173 1.20 ad class, true);
174 1.11 ad break;
175 1.11 ad }
176 1.11 ad } while (mod != NULL);
177 1.1 ad mutex_exit(&module_lock);
178 1.1 ad }
179 1.1 ad
180 1.1 ad /*
181 1.21 ad * module_compatible:
182 1.1 ad *
183 1.21 ad * Return true if the two supplied kernel versions are said to
184 1.21 ad * have the same binary interface for kernel code. The entire
185 1.21 ad * version is signficant for the development tree (-current),
186 1.21 ad * major and minor versions are significant for official
187 1.21 ad * releases of the system.
188 1.1 ad */
189 1.22 pooka bool
190 1.21 ad module_compatible(int v1, int v2)
191 1.1 ad {
192 1.1 ad
193 1.21 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
194 1.21 ad return v1 == v2;
195 1.21 ad #else /* release */
196 1.21 ad return abs(v1 - v2) < 10000;
197 1.21 ad #endif
198 1.1 ad }
199 1.1 ad
200 1.1 ad /*
201 1.1 ad * module_load:
202 1.1 ad *
203 1.9 jmmv * Load a single module from the file system.
204 1.1 ad */
205 1.1 ad int
206 1.18 ad module_load(const char *filename, int flags, prop_dictionary_t props,
207 1.23 ad modclass_t class)
208 1.1 ad {
209 1.1 ad int error;
210 1.1 ad
211 1.18 ad /* Authorize. */
212 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
213 1.18 ad 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
214 1.18 ad if (error != 0) {
215 1.18 ad return error;
216 1.18 ad }
217 1.18 ad
218 1.1 ad mutex_enter(&module_lock);
219 1.20 ad error = module_do_load(filename, false, flags, props, NULL, class,
220 1.23 ad false);
221 1.1 ad mutex_exit(&module_lock);
222 1.1 ad
223 1.1 ad return error;
224 1.1 ad }
225 1.1 ad
226 1.1 ad /*
227 1.23 ad * module_autoload:
228 1.23 ad *
229 1.23 ad * Load a single module from the file system, system initiated.
230 1.23 ad */
231 1.23 ad int
232 1.23 ad module_autoload(const char *filename, modclass_t class)
233 1.23 ad {
234 1.23 ad int error;
235 1.23 ad
236 1.23 ad KASSERT(mutex_owned(&module_lock));
237 1.23 ad
238 1.29 ad /* Disallow path seperators and magic symlinks. */
239 1.29 ad if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
240 1.29 ad strchr(filename, '.') != NULL) {
241 1.29 ad return EPERM;
242 1.29 ad }
243 1.29 ad
244 1.23 ad /* Authorize. */
245 1.23 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
246 1.23 ad 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
247 1.23 ad if (error != 0) {
248 1.23 ad return error;
249 1.23 ad }
250 1.23 ad
251 1.23 ad return module_do_load(filename, false, 0, NULL, NULL, class, true);
252 1.23 ad }
253 1.23 ad
254 1.23 ad /*
255 1.1 ad * module_unload:
256 1.1 ad *
257 1.1 ad * Find and unload a module by name.
258 1.1 ad */
259 1.1 ad int
260 1.1 ad module_unload(const char *name)
261 1.1 ad {
262 1.1 ad int error;
263 1.1 ad
264 1.18 ad /* Authorize. */
265 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
266 1.18 ad 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
267 1.18 ad if (error != 0) {
268 1.18 ad return error;
269 1.18 ad }
270 1.18 ad
271 1.1 ad mutex_enter(&module_lock);
272 1.1 ad error = module_do_unload(name);
273 1.1 ad mutex_exit(&module_lock);
274 1.1 ad
275 1.1 ad return error;
276 1.1 ad }
277 1.1 ad
278 1.1 ad /*
279 1.1 ad * module_lookup:
280 1.1 ad *
281 1.1 ad * Look up a module by name.
282 1.1 ad */
283 1.1 ad module_t *
284 1.1 ad module_lookup(const char *name)
285 1.1 ad {
286 1.1 ad module_t *mod;
287 1.1 ad
288 1.1 ad KASSERT(mutex_owned(&module_lock));
289 1.1 ad
290 1.1 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
291 1.1 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
292 1.1 ad break;
293 1.1 ad }
294 1.1 ad }
295 1.1 ad
296 1.1 ad return mod;
297 1.1 ad }
298 1.1 ad
299 1.1 ad /*
300 1.1 ad * module_hold:
301 1.1 ad *
302 1.1 ad * Add a single reference to a module. It's the caller's
303 1.1 ad * responsibility to ensure that the reference is dropped
304 1.1 ad * later.
305 1.1 ad */
306 1.1 ad int
307 1.1 ad module_hold(const char *name)
308 1.1 ad {
309 1.1 ad module_t *mod;
310 1.1 ad
311 1.1 ad mutex_enter(&module_lock);
312 1.1 ad mod = module_lookup(name);
313 1.1 ad if (mod == NULL) {
314 1.1 ad mutex_exit(&module_lock);
315 1.1 ad return ENOENT;
316 1.1 ad }
317 1.1 ad mod->mod_refcnt++;
318 1.1 ad mutex_exit(&module_lock);
319 1.1 ad
320 1.1 ad return 0;
321 1.1 ad }
322 1.1 ad
323 1.1 ad /*
324 1.1 ad * module_rele:
325 1.1 ad *
326 1.1 ad * Release a reference acquired with module_hold().
327 1.1 ad */
328 1.1 ad void
329 1.1 ad module_rele(const char *name)
330 1.1 ad {
331 1.1 ad module_t *mod;
332 1.1 ad
333 1.1 ad mutex_enter(&module_lock);
334 1.1 ad mod = module_lookup(name);
335 1.1 ad if (mod == NULL) {
336 1.1 ad mutex_exit(&module_lock);
337 1.1 ad panic("module_rele: gone");
338 1.1 ad }
339 1.1 ad mod->mod_refcnt--;
340 1.1 ad mutex_exit(&module_lock);
341 1.1 ad }
342 1.1 ad
343 1.1 ad /*
344 1.27 ad * module_enqueue:
345 1.27 ad *
346 1.27 ad * Put a module onto the global list and update counters.
347 1.27 ad */
348 1.27 ad static void
349 1.27 ad module_enqueue(module_t *mod)
350 1.27 ad {
351 1.27 ad int i;
352 1.27 ad
353 1.27 ad /*
354 1.27 ad * If there are requisite modules, put at the head of the queue.
355 1.27 ad * This is so that autounload can unload requisite modules with
356 1.27 ad * only one pass through the queue.
357 1.27 ad */
358 1.27 ad if (mod->mod_nrequired) {
359 1.27 ad TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
360 1.27 ad
361 1.27 ad /* Add references to the requisite modules. */
362 1.27 ad for (i = 0; i < mod->mod_nrequired; i++) {
363 1.27 ad KASSERT(mod->mod_required[i] != NULL);
364 1.27 ad mod->mod_required[i]->mod_refcnt++;
365 1.27 ad }
366 1.27 ad } else {
367 1.27 ad TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
368 1.27 ad }
369 1.27 ad module_count++;
370 1.27 ad module_gen++;
371 1.27 ad }
372 1.27 ad
373 1.27 ad /*
374 1.1 ad * module_do_builtin:
375 1.1 ad *
376 1.1 ad * Initialize a single module from the list of modules that are
377 1.1 ad * built into the kernel (linked into the kernel image).
378 1.1 ad */
379 1.1 ad static int
380 1.1 ad module_do_builtin(const char *name, module_t **modp)
381 1.1 ad {
382 1.1 ad __link_set_decl(modules, modinfo_t);
383 1.1 ad modinfo_t *const *mip;
384 1.1 ad const char *p, *s;
385 1.1 ad char buf[MAXMODNAME];
386 1.1 ad modinfo_t *mi;
387 1.1 ad module_t *mod, *mod2;
388 1.1 ad size_t len;
389 1.27 ad int error;
390 1.1 ad
391 1.1 ad KASSERT(mutex_owned(&module_lock));
392 1.1 ad
393 1.1 ad /*
394 1.1 ad * Check to see if already loaded.
395 1.1 ad */
396 1.1 ad if ((mod = module_lookup(name)) != NULL) {
397 1.1 ad if (modp != NULL) {
398 1.1 ad *modp = mod;
399 1.1 ad }
400 1.1 ad return 0;
401 1.1 ad }
402 1.1 ad
403 1.1 ad /*
404 1.1 ad * Search the list to see if we have a module by this name.
405 1.1 ad */
406 1.1 ad error = ENOENT;
407 1.1 ad __link_set_foreach(mip, modules) {
408 1.1 ad mi = *mip;
409 1.1 ad if (mi == &module_dummy) {
410 1.1 ad continue;
411 1.1 ad }
412 1.1 ad if (strcmp(mi->mi_name, name) == 0) {
413 1.1 ad error = 0;
414 1.1 ad break;
415 1.1 ad }
416 1.1 ad }
417 1.1 ad if (error != 0) {
418 1.1 ad return error;
419 1.1 ad }
420 1.1 ad
421 1.1 ad /*
422 1.1 ad * Initialize pre-requisites.
423 1.1 ad */
424 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
425 1.1 ad if (mod == NULL) {
426 1.1 ad return ENOMEM;
427 1.1 ad }
428 1.1 ad if (modp != NULL) {
429 1.1 ad *modp = mod;
430 1.1 ad }
431 1.1 ad if (mi->mi_required != NULL) {
432 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
433 1.1 ad if (*s == ',')
434 1.1 ad s++;
435 1.1 ad p = s;
436 1.1 ad while (*p != '\0' && *p != ',')
437 1.1 ad p++;
438 1.1 ad len = min(p - s + 1, sizeof(buf));
439 1.1 ad strlcpy(buf, s, len);
440 1.1 ad if (buf[0] == '\0')
441 1.1 ad break;
442 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
443 1.1 ad module_error("too many required modules");
444 1.1 ad kmem_free(mod, sizeof(*mod));
445 1.1 ad return EINVAL;
446 1.1 ad }
447 1.1 ad error = module_do_builtin(buf, &mod2);
448 1.1 ad if (error != 0) {
449 1.1 ad kmem_free(mod, sizeof(*mod));
450 1.1 ad return error;
451 1.1 ad }
452 1.1 ad mod->mod_required[mod->mod_nrequired++] = mod2;
453 1.1 ad }
454 1.1 ad }
455 1.1 ad
456 1.1 ad /*
457 1.1 ad * Try to initialize the module.
458 1.1 ad */
459 1.12 ad KASSERT(module_active == NULL);
460 1.12 ad module_active = mod;
461 1.1 ad error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
462 1.12 ad module_active = NULL;
463 1.1 ad if (error != 0) {
464 1.1 ad module_error("builtin module `%s' "
465 1.1 ad "failed to init", mi->mi_name);
466 1.1 ad kmem_free(mod, sizeof(*mod));
467 1.1 ad return error;
468 1.1 ad }
469 1.1 ad mod->mod_info = mi;
470 1.1 ad mod->mod_source = MODULE_SOURCE_KERNEL;
471 1.27 ad module_enqueue(mod);
472 1.1 ad return 0;
473 1.1 ad }
474 1.1 ad
475 1.1 ad /*
476 1.1 ad * module_do_load:
477 1.1 ad *
478 1.1 ad * Helper routine: load a module from the file system, or one
479 1.1 ad * pushed by the boot loader.
480 1.1 ad */
481 1.1 ad static int
482 1.27 ad module_do_load(const char *name, bool isdep, int flags,
483 1.20 ad prop_dictionary_t props, module_t **modp, modclass_t class,
484 1.20 ad bool autoload)
485 1.1 ad {
486 1.1 ad static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
487 1.1 ad static int depth;
488 1.1 ad const int maxdepth = 6;
489 1.1 ad modinfo_t *mi;
490 1.1 ad module_t *mod, *mod2;
491 1.1 ad char buf[MAXMODNAME];
492 1.1 ad const char *s, *p;
493 1.1 ad int error;
494 1.1 ad size_t len;
495 1.1 ad
496 1.1 ad KASSERT(mutex_owned(&module_lock));
497 1.1 ad
498 1.1 ad error = 0;
499 1.1 ad
500 1.1 ad /*
501 1.1 ad * Avoid recursing too far.
502 1.1 ad */
503 1.1 ad if (++depth > maxdepth) {
504 1.1 ad module_error("too many required modules");
505 1.1 ad depth--;
506 1.1 ad return EMLINK;
507 1.1 ad }
508 1.1 ad
509 1.1 ad /*
510 1.1 ad * Load the module and link. Before going to the file system,
511 1.1 ad * scan the list of modules loaded by the boot loader. Just
512 1.1 ad * before init is started the list of modules loaded at boot
513 1.1 ad * will be purged. Before init is started we can assume that
514 1.27 ad * `name' is a module name and not a path name.
515 1.1 ad */
516 1.1 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
517 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
518 1.1 ad TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
519 1.1 ad break;
520 1.1 ad }
521 1.1 ad }
522 1.14 rumble if (mod != NULL) {
523 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
524 1.14 rumble } else {
525 1.27 ad /*
526 1.27 ad * If a requisite module, check to see if it is
527 1.27 ad * already present.
528 1.27 ad */
529 1.27 ad if (isdep) {
530 1.27 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
531 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
532 1.27 ad break;
533 1.27 ad }
534 1.27 ad }
535 1.27 ad if (mod != NULL) {
536 1.27 ad if (modp != NULL) {
537 1.27 ad *modp = mod;
538 1.27 ad }
539 1.27 ad depth--;
540 1.27 ad return 0;
541 1.27 ad }
542 1.27 ad }
543 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
544 1.1 ad if (mod == NULL) {
545 1.1 ad depth--;
546 1.1 ad return ENOMEM;
547 1.1 ad }
548 1.27 ad error = kobj_load_file(&mod->mod_kobj, name, module_base,
549 1.20 ad autoload);
550 1.1 ad if (error != 0) {
551 1.1 ad kmem_free(mod, sizeof(*mod));
552 1.1 ad depth--;
553 1.26 ad if (!autoload) {
554 1.26 ad module_error("unable to load kernel object");
555 1.26 ad }
556 1.8 rumble return error;
557 1.7 rumble }
558 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
559 1.1 ad mod->mod_source = MODULE_SOURCE_FILESYS;
560 1.11 ad error = module_fetch_info(mod);
561 1.11 ad if (error != 0) {
562 1.11 ad goto fail;
563 1.11 ad }
564 1.1 ad }
565 1.1 ad
566 1.1 ad /*
567 1.11 ad * Check compatibility.
568 1.1 ad */
569 1.1 ad mi = mod->mod_info;
570 1.3 rumble if (strlen(mi->mi_name) >= MAXMODNAME) {
571 1.3 rumble error = EINVAL;
572 1.3 rumble module_error("module name too long");
573 1.3 rumble goto fail;
574 1.3 rumble }
575 1.21 ad if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
576 1.21 ad module_error("module built for different version of system");
577 1.24 ad if ((flags & MODCTL_LOAD_FORCE) != 0) {
578 1.24 ad module_error("forced load, system may be unstable");
579 1.24 ad } else {
580 1.24 ad error = EPROGMISMATCH;
581 1.24 ad goto fail;
582 1.24 ad }
583 1.21 ad }
584 1.3 rumble
585 1.1 ad /*
586 1.18 ad * If a specific kind of module was requested, ensure that we have
587 1.18 ad * a match.
588 1.18 ad */
589 1.18 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
590 1.18 ad error = ENOENT;
591 1.18 ad goto fail;
592 1.18 ad }
593 1.18 ad
594 1.18 ad /*
595 1.27 ad * If loading a dependency, `name' is a plain module name.
596 1.1 ad * The name must match.
597 1.1 ad */
598 1.27 ad if (isdep && strcmp(mi->mi_name, name) != 0) {
599 1.1 ad error = ENOENT;
600 1.1 ad goto fail;
601 1.1 ad }
602 1.1 ad
603 1.1 ad /*
604 1.3 rumble * Check to see if the module is already loaded. If so, we may
605 1.3 rumble * have been recursively called to handle a dependency, so be sure
606 1.3 rumble * to set modp.
607 1.1 ad */
608 1.3 rumble if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
609 1.5 rumble if (modp != NULL)
610 1.5 rumble *modp = mod2;
611 1.1 ad error = EEXIST;
612 1.1 ad goto fail;
613 1.1 ad }
614 1.3 rumble
615 1.3 rumble /*
616 1.3 rumble * Block circular dependencies.
617 1.3 rumble */
618 1.1 ad TAILQ_FOREACH(mod2, &pending, mod_chain) {
619 1.1 ad if (mod == mod2) {
620 1.1 ad continue;
621 1.1 ad }
622 1.1 ad if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
623 1.1 ad error = EDEADLK;
624 1.1 ad module_error("circular dependency detected");
625 1.1 ad goto fail;
626 1.1 ad }
627 1.1 ad }
628 1.1 ad
629 1.1 ad /*
630 1.1 ad * Now try to load any requisite modules.
631 1.1 ad */
632 1.1 ad if (mi->mi_required != NULL) {
633 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
634 1.1 ad if (*s == ',')
635 1.1 ad s++;
636 1.1 ad p = s;
637 1.1 ad while (*p != '\0' && *p != ',')
638 1.1 ad p++;
639 1.3 rumble len = p - s + 1;
640 1.3 rumble if (len >= MAXMODNAME) {
641 1.3 rumble error = EINVAL;
642 1.3 rumble module_error("required module name too long");
643 1.3 rumble goto fail;
644 1.3 rumble }
645 1.1 ad strlcpy(buf, s, len);
646 1.1 ad if (buf[0] == '\0')
647 1.1 ad break;
648 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
649 1.1 ad error = EINVAL;
650 1.1 ad module_error("too many required modules");
651 1.1 ad goto fail;
652 1.1 ad }
653 1.6 rumble if (strcmp(buf, mi->mi_name) == 0) {
654 1.6 rumble error = EDEADLK;
655 1.6 rumble module_error("self-dependency detected");
656 1.6 rumble goto fail;
657 1.6 rumble }
658 1.9 jmmv error = module_do_load(buf, true, flags, NULL,
659 1.18 ad &mod->mod_required[mod->mod_nrequired++],
660 1.20 ad MODULE_CLASS_ANY, true);
661 1.27 ad if (error != 0)
662 1.1 ad goto fail;
663 1.1 ad }
664 1.1 ad }
665 1.1 ad
666 1.1 ad /*
667 1.15 ad * We loaded all needed modules successfully: perform global
668 1.15 ad * relocations and initialize.
669 1.1 ad */
670 1.15 ad error = kobj_affix(mod->mod_kobj, mi->mi_name);
671 1.15 ad if (error != 0) {
672 1.15 ad module_error("unable to affix module");
673 1.15 ad goto fail2;
674 1.15 ad }
675 1.15 ad
676 1.12 ad KASSERT(module_active == NULL);
677 1.12 ad module_active = mod;
678 1.9 jmmv error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
679 1.12 ad module_active = NULL;
680 1.1 ad if (error != 0) {
681 1.1 ad module_error("modctl function returned error %d", error);
682 1.1 ad goto fail;
683 1.1 ad }
684 1.1 ad
685 1.1 ad /*
686 1.1 ad * Good, the module loaded successfully. Put it onto the
687 1.1 ad * list and add references to its requisite modules.
688 1.1 ad */
689 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
690 1.27 ad module_enqueue(mod);
691 1.1 ad if (modp != NULL) {
692 1.1 ad *modp = mod;
693 1.1 ad }
694 1.26 ad if (autoload) {
695 1.26 ad /*
696 1.26 ad * Arrange to try unloading the module after
697 1.26 ad * a short delay.
698 1.26 ad */
699 1.26 ad mod->mod_autotime = time_second + module_autotime;
700 1.26 ad module_thread_kick();
701 1.26 ad }
702 1.1 ad depth--;
703 1.1 ad return 0;
704 1.7 rumble
705 1.1 ad fail:
706 1.15 ad kobj_unload(mod->mod_kobj);
707 1.15 ad fail2:
708 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
709 1.1 ad kmem_free(mod, sizeof(*mod));
710 1.1 ad depth--;
711 1.1 ad return error;
712 1.1 ad }
713 1.1 ad
714 1.1 ad /*
715 1.1 ad * module_do_unload:
716 1.1 ad *
717 1.1 ad * Helper routine: do the dirty work of unloading a module.
718 1.1 ad */
719 1.1 ad static int
720 1.1 ad module_do_unload(const char *name)
721 1.1 ad {
722 1.1 ad module_t *mod;
723 1.1 ad int error;
724 1.1 ad u_int i;
725 1.1 ad
726 1.1 ad KASSERT(mutex_owned(&module_lock));
727 1.1 ad
728 1.1 ad mod = module_lookup(name);
729 1.1 ad if (mod == NULL) {
730 1.1 ad return ENOENT;
731 1.1 ad }
732 1.1 ad if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
733 1.1 ad return EBUSY;
734 1.1 ad }
735 1.12 ad KASSERT(module_active == NULL);
736 1.12 ad module_active = mod;
737 1.1 ad error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
738 1.12 ad module_active = NULL;
739 1.1 ad if (error != 0) {
740 1.1 ad return error;
741 1.1 ad }
742 1.1 ad module_count--;
743 1.1 ad TAILQ_REMOVE(&module_list, mod, mod_chain);
744 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
745 1.1 ad mod->mod_required[i]->mod_refcnt--;
746 1.1 ad }
747 1.1 ad if (mod->mod_kobj != NULL) {
748 1.1 ad kobj_unload(mod->mod_kobj);
749 1.1 ad }
750 1.1 ad kmem_free(mod, sizeof(*mod));
751 1.26 ad module_gen++;
752 1.1 ad
753 1.1 ad return 0;
754 1.1 ad }
755 1.1 ad
756 1.1 ad /*
757 1.1 ad * module_prime:
758 1.1 ad *
759 1.1 ad * Push a module loaded by the bootloader onto our internal
760 1.1 ad * list.
761 1.1 ad */
762 1.1 ad int
763 1.1 ad module_prime(void *base, size_t size)
764 1.1 ad {
765 1.1 ad module_t *mod;
766 1.1 ad int error;
767 1.1 ad
768 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
769 1.1 ad if (mod == NULL) {
770 1.1 ad return ENOMEM;
771 1.1 ad }
772 1.1 ad mod->mod_source = MODULE_SOURCE_BOOT;
773 1.1 ad
774 1.15 ad error = kobj_load_mem(&mod->mod_kobj, base, size);
775 1.1 ad if (error != 0) {
776 1.11 ad kmem_free(mod, sizeof(*mod));
777 1.11 ad module_error("unable to load object pushed by boot loader");
778 1.11 ad return error;
779 1.11 ad }
780 1.11 ad error = module_fetch_info(mod);
781 1.11 ad if (error != 0) {
782 1.11 ad kobj_unload(mod->mod_kobj);
783 1.1 ad kmem_free(mod, sizeof(*mod));
784 1.11 ad module_error("unable to load object pushed by boot loader");
785 1.1 ad return error;
786 1.1 ad }
787 1.11 ad
788 1.1 ad TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
789 1.1 ad
790 1.1 ad return 0;
791 1.1 ad }
792 1.11 ad
793 1.11 ad /*
794 1.11 ad * module_fetch_into:
795 1.11 ad *
796 1.11 ad * Fetch modinfo record from a loaded module.
797 1.11 ad */
798 1.11 ad static int
799 1.11 ad module_fetch_info(module_t *mod)
800 1.11 ad {
801 1.11 ad int error;
802 1.11 ad void *addr;
803 1.11 ad size_t size;
804 1.11 ad
805 1.11 ad /*
806 1.11 ad * Find module info record and check compatibility.
807 1.11 ad */
808 1.11 ad error = kobj_find_section(mod->mod_kobj, "link_set_modules",
809 1.11 ad &addr, &size);
810 1.11 ad if (error != 0) {
811 1.11 ad module_error("`link_set_modules' section not present");
812 1.11 ad return error;
813 1.11 ad }
814 1.11 ad if (size != sizeof(modinfo_t **)) {
815 1.11 ad module_error("`link_set_modules' section wrong size");
816 1.11 ad return error;
817 1.11 ad }
818 1.11 ad mod->mod_info = *(modinfo_t **)addr;
819 1.11 ad
820 1.11 ad return 0;
821 1.11 ad }
822 1.12 ad
823 1.12 ad /*
824 1.12 ad * module_find_section:
825 1.12 ad *
826 1.12 ad * Allows a module that is being initialized to look up a section
827 1.12 ad * within its ELF object.
828 1.12 ad */
829 1.12 ad int
830 1.12 ad module_find_section(const char *name, void **addr, size_t *size)
831 1.12 ad {
832 1.12 ad
833 1.12 ad KASSERT(mutex_owned(&module_lock));
834 1.12 ad KASSERT(module_active != NULL);
835 1.12 ad
836 1.12 ad return kobj_find_section(module_active->mod_kobj, name, addr, size);
837 1.12 ad }
838 1.26 ad
839 1.26 ad /*
840 1.26 ad * module_thread:
841 1.26 ad *
842 1.26 ad * Automatically unload modules. We try once to unload autoloaded
843 1.26 ad * modules after module_autotime seconds. If the system is under
844 1.26 ad * severe memory pressure, we'll try unloading all modules.
845 1.26 ad */
846 1.26 ad static void
847 1.26 ad module_thread(void *cookie)
848 1.26 ad {
849 1.26 ad module_t *mod, *next;
850 1.28 ad modinfo_t *mi;
851 1.28 ad int error;
852 1.26 ad
853 1.26 ad for (;;) {
854 1.26 ad mutex_enter(&module_lock);
855 1.26 ad for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
856 1.26 ad next = TAILQ_NEXT(mod, mod_chain);
857 1.26 ad if (uvmexp.free < uvmexp.freemin) {
858 1.26 ad module_thread_ticks = hz;
859 1.26 ad } else if (mod->mod_autotime == 0) {
860 1.26 ad continue;
861 1.26 ad } else if (time_second < mod->mod_autotime) {
862 1.26 ad module_thread_ticks = hz;
863 1.26 ad continue;
864 1.26 ad } else {
865 1.26 ad mod->mod_autotime = 0;
866 1.26 ad }
867 1.28 ad /*
868 1.28 ad * If this module wants to avoid autounload then
869 1.28 ad * skip it. Some modules can ping-pong in and out
870 1.28 ad * because their use is transient but often.
871 1.28 ad * Example: exec_script.
872 1.28 ad */
873 1.28 ad mi = mod->mod_info;
874 1.28 ad error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
875 1.28 ad if (error == 0 || error == ENOTTY) {
876 1.28 ad (void)module_do_unload(mi->mi_name);
877 1.28 ad }
878 1.26 ad }
879 1.26 ad mutex_exit(&module_lock);
880 1.26 ad
881 1.26 ad mutex_enter(&module_thread_lock);
882 1.26 ad (void)cv_timedwait(&module_thread_cv, &module_thread_lock,
883 1.26 ad module_thread_ticks);
884 1.26 ad module_thread_ticks = 0;
885 1.26 ad mutex_exit(&module_thread_lock);
886 1.26 ad }
887 1.26 ad }
888 1.26 ad
889 1.26 ad /*
890 1.26 ad * module_thread:
891 1.26 ad *
892 1.26 ad * Kick the module thread into action, perhaps because the
893 1.26 ad * system is low on memory.
894 1.26 ad */
895 1.26 ad void
896 1.26 ad module_thread_kick(void)
897 1.26 ad {
898 1.26 ad
899 1.26 ad mutex_enter(&module_thread_lock);
900 1.26 ad module_thread_ticks = hz;
901 1.26 ad cv_broadcast(&module_thread_cv);
902 1.26 ad mutex_exit(&module_thread_lock);
903 1.26 ad }
904