kern_module.c revision 1.64 1 1.64 jruoho /* $NetBSD: kern_module.c,v 1.64 2010/04/19 11:20:56 jruoho 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.64 jruoho __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.64 2010/04/19 11:20:56 jruoho Exp $");
38 1.54 pooka
39 1.54 pooka #define _MODULE_INTERNAL
40 1.30 ad
41 1.30 ad #ifdef _KERNEL_OPT
42 1.30 ad #include "opt_ddb.h"
43 1.42 apb #include "opt_modular.h"
44 1.30 ad #endif
45 1.1 ad
46 1.1 ad #include <sys/param.h>
47 1.1 ad #include <sys/systm.h>
48 1.26 ad #include <sys/kernel.h>
49 1.1 ad #include <sys/proc.h>
50 1.1 ad #include <sys/kauth.h>
51 1.1 ad #include <sys/kobj.h>
52 1.1 ad #include <sys/kmem.h>
53 1.1 ad #include <sys/module.h>
54 1.18 ad #include <sys/kauth.h>
55 1.26 ad #include <sys/kthread.h>
56 1.34 ad #include <sys/sysctl.h>
57 1.46 jnemeth #include <sys/lock.h>
58 1.1 ad
59 1.1 ad #include <uvm/uvm_extern.h>
60 1.1 ad
61 1.1 ad #include <machine/stdarg.h>
62 1.1 ad
63 1.25 ad struct vm_map *module_map;
64 1.54 pooka char module_base[MODULE_BASE_SIZE];
65 1.2 ad
66 1.59 pooka struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
67 1.59 pooka struct modlist module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
68 1.59 pooka static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
69 1.59 pooka
70 1.12 ad static module_t *module_active;
71 1.34 ad static int module_verbose_on;
72 1.34 ad static int module_autoload_on = 1;
73 1.1 ad u_int module_count;
74 1.59 pooka u_int module_builtinlist;
75 1.1 ad kmutex_t module_lock;
76 1.26 ad u_int module_autotime = 10;
77 1.26 ad u_int module_gen = 1;
78 1.26 ad static kcondvar_t module_thread_cv;
79 1.26 ad static kmutex_t module_thread_lock;
80 1.26 ad static int module_thread_ticks;
81 1.1 ad
82 1.51 elad static kauth_listener_t module_listener;
83 1.51 elad
84 1.1 ad /* Ensure that the kernel's link set isn't empty. */
85 1.1 ad static modinfo_t module_dummy;
86 1.1 ad __link_set_add_rodata(modules, module_dummy);
87 1.1 ad
88 1.9 jmmv static int module_do_load(const char *, bool, int, prop_dictionary_t,
89 1.20 ad module_t **, modclass_t class, bool);
90 1.1 ad static int module_do_unload(const char *);
91 1.1 ad static int module_do_builtin(const char *, module_t **);
92 1.11 ad static int module_fetch_info(module_t *);
93 1.26 ad static void module_thread(void *);
94 1.54 pooka
95 1.60 pooka static module_t *module_lookup(const char *);
96 1.60 pooka static void module_enqueue(module_t *);
97 1.60 pooka
98 1.47 jnemeth static bool module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
99 1.1 ad
100 1.54 pooka int module_eopnotsupp(void);
101 1.54 pooka
102 1.54 pooka int
103 1.54 pooka module_eopnotsupp(void)
104 1.54 pooka {
105 1.54 pooka
106 1.54 pooka return EOPNOTSUPP;
107 1.54 pooka }
108 1.54 pooka __weak_alias(module_load_vfs,module_eopnotsupp);
109 1.54 pooka
110 1.1 ad /*
111 1.1 ad * module_error:
112 1.1 ad *
113 1.1 ad * Utility function: log an error.
114 1.1 ad */
115 1.54 pooka void
116 1.1 ad module_error(const char *fmt, ...)
117 1.1 ad {
118 1.1 ad va_list ap;
119 1.1 ad
120 1.1 ad va_start(ap, fmt);
121 1.1 ad printf("WARNING: module error: ");
122 1.1 ad vprintf(fmt, ap);
123 1.1 ad printf("\n");
124 1.1 ad va_end(ap);
125 1.1 ad }
126 1.1 ad
127 1.1 ad /*
128 1.34 ad * module_print:
129 1.34 ad *
130 1.34 ad * Utility function: log verbose output.
131 1.34 ad */
132 1.54 pooka void
133 1.34 ad module_print(const char *fmt, ...)
134 1.34 ad {
135 1.34 ad va_list ap;
136 1.34 ad
137 1.34 ad if (module_verbose_on) {
138 1.34 ad va_start(ap, fmt);
139 1.34 ad printf("DEBUG: module: ");
140 1.34 ad vprintf(fmt, ap);
141 1.34 ad printf("\n");
142 1.34 ad va_end(ap);
143 1.34 ad }
144 1.34 ad }
145 1.34 ad
146 1.55 elad static int
147 1.55 elad module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
148 1.55 elad void *arg0, void *arg1, void *arg2, void *arg3)
149 1.55 elad {
150 1.55 elad int result;
151 1.55 elad
152 1.55 elad result = KAUTH_RESULT_DEFER;
153 1.55 elad
154 1.55 elad if (action != KAUTH_SYSTEM_MODULE)
155 1.55 elad return result;
156 1.55 elad
157 1.55 elad if ((uintptr_t)arg2 != 0) /* autoload */
158 1.55 elad result = KAUTH_RESULT_ALLOW;
159 1.55 elad
160 1.55 elad return result;
161 1.55 elad }
162 1.55 elad
163 1.34 ad /*
164 1.59 pooka * Add modules to the builtin list. This can done at boottime or
165 1.59 pooka * at runtime if the module is linked into the kernel with an
166 1.59 pooka * external linker. All or none of the input will be handled.
167 1.59 pooka * Optionally, the modules can be initialized. If they are not
168 1.59 pooka * initialized, module_init_class() or module_load() can be used
169 1.59 pooka * later, but these are not guaranteed to give atomic results.
170 1.59 pooka */
171 1.59 pooka int
172 1.59 pooka module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
173 1.59 pooka {
174 1.59 pooka struct module **modp = NULL, *mod_iter;
175 1.59 pooka int rv = 0, i, mipskip;
176 1.59 pooka
177 1.59 pooka if (init) {
178 1.59 pooka rv = kauth_authorize_system(kauth_cred_get(),
179 1.59 pooka KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
180 1.59 pooka (void *)(uintptr_t)1, NULL);
181 1.59 pooka if (rv) {
182 1.59 pooka return rv;
183 1.59 pooka }
184 1.59 pooka }
185 1.59 pooka
186 1.59 pooka for (i = 0, mipskip = 0; i < nmodinfo; i++) {
187 1.59 pooka if (mip[i] == &module_dummy) {
188 1.59 pooka KASSERT(nmodinfo > 0);
189 1.59 pooka nmodinfo--;
190 1.59 pooka }
191 1.59 pooka }
192 1.59 pooka if (nmodinfo == 0)
193 1.59 pooka return 0;
194 1.59 pooka
195 1.59 pooka modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
196 1.59 pooka for (i = 0, mipskip = 0; i < nmodinfo; i++) {
197 1.59 pooka if (mip[i+mipskip] == &module_dummy) {
198 1.59 pooka mipskip++;
199 1.59 pooka continue;
200 1.59 pooka }
201 1.59 pooka modp[i] = kmem_zalloc(sizeof(*modp[i]), KM_SLEEP);
202 1.59 pooka modp[i]->mod_info = mip[i+mipskip];
203 1.59 pooka modp[i]->mod_source = MODULE_SOURCE_KERNEL;
204 1.59 pooka }
205 1.59 pooka mutex_enter(&module_lock);
206 1.59 pooka
207 1.59 pooka /* do this in three stages for error recovery and atomicity */
208 1.59 pooka
209 1.59 pooka /* first check for presence */
210 1.59 pooka for (i = 0; i < nmodinfo; i++) {
211 1.59 pooka TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
212 1.59 pooka if (strcmp(mod_iter->mod_info->mi_name,
213 1.59 pooka modp[i]->mod_info->mi_name) == 0)
214 1.59 pooka break;
215 1.59 pooka }
216 1.59 pooka if (mod_iter) {
217 1.59 pooka rv = EEXIST;
218 1.59 pooka goto out;
219 1.59 pooka }
220 1.59 pooka
221 1.59 pooka if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
222 1.59 pooka rv = EEXIST;
223 1.59 pooka goto out;
224 1.59 pooka }
225 1.59 pooka }
226 1.59 pooka
227 1.59 pooka /* then add to list */
228 1.59 pooka for (i = 0; i < nmodinfo; i++) {
229 1.59 pooka TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
230 1.59 pooka module_builtinlist++;
231 1.59 pooka }
232 1.59 pooka
233 1.59 pooka /* finally, init (if required) */
234 1.59 pooka if (init) {
235 1.59 pooka for (i = 0; i < nmodinfo; i++) {
236 1.59 pooka rv = module_do_builtin(modp[i]->mod_info->mi_name,NULL);
237 1.59 pooka /* throw in the towel, recovery hard & not worth it */
238 1.59 pooka if (rv)
239 1.59 pooka panic("builtin module \"%s\" init failed: %d",
240 1.59 pooka modp[i]->mod_info->mi_name, rv);
241 1.59 pooka }
242 1.59 pooka }
243 1.59 pooka
244 1.59 pooka out:
245 1.59 pooka mutex_exit(&module_lock);
246 1.59 pooka if (rv != 0) {
247 1.59 pooka for (i = 0; i < nmodinfo; i++) {
248 1.59 pooka if (modp[i])
249 1.59 pooka kmem_free(modp[i], sizeof(*modp[i]));
250 1.59 pooka }
251 1.59 pooka }
252 1.59 pooka kmem_free(modp, sizeof(*modp) * nmodinfo);
253 1.59 pooka return rv;
254 1.59 pooka }
255 1.59 pooka
256 1.59 pooka /*
257 1.59 pooka * Optionally fini and remove builtin module from the kernel.
258 1.59 pooka * Note: the module will now be unreachable except via mi && builtin_add.
259 1.59 pooka */
260 1.59 pooka int
261 1.59 pooka module_builtin_remove(modinfo_t *mi, bool fini)
262 1.59 pooka {
263 1.59 pooka struct module *mod;
264 1.59 pooka int rv = 0;
265 1.59 pooka
266 1.59 pooka if (fini) {
267 1.59 pooka rv = kauth_authorize_system(kauth_cred_get(),
268 1.59 pooka KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
269 1.59 pooka NULL, NULL);
270 1.59 pooka if (rv)
271 1.59 pooka return rv;
272 1.59 pooka
273 1.59 pooka mutex_enter(&module_lock);
274 1.59 pooka rv = module_do_unload(mi->mi_name);
275 1.59 pooka if (rv) {
276 1.59 pooka goto out;
277 1.59 pooka }
278 1.59 pooka } else {
279 1.59 pooka mutex_enter(&module_lock);
280 1.59 pooka }
281 1.59 pooka TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
282 1.59 pooka if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
283 1.59 pooka break;
284 1.59 pooka }
285 1.59 pooka if (mod) {
286 1.59 pooka TAILQ_REMOVE(&module_builtins, mod, mod_chain);
287 1.59 pooka module_builtinlist--;
288 1.59 pooka } else {
289 1.59 pooka KASSERT(fini == false);
290 1.59 pooka rv = ENOENT;
291 1.59 pooka }
292 1.59 pooka
293 1.59 pooka out:
294 1.59 pooka mutex_exit(&module_lock);
295 1.59 pooka return rv;
296 1.59 pooka }
297 1.59 pooka
298 1.59 pooka /*
299 1.1 ad * module_init:
300 1.1 ad *
301 1.1 ad * Initialize the module subsystem.
302 1.1 ad */
303 1.1 ad void
304 1.1 ad module_init(void)
305 1.1 ad {
306 1.59 pooka __link_set_decl(modules, modinfo_t);
307 1.25 ad extern struct vm_map *module_map;
308 1.59 pooka modinfo_t *const *mip;
309 1.59 pooka int rv;
310 1.1 ad
311 1.25 ad if (module_map == NULL) {
312 1.25 ad module_map = kernel_map;
313 1.25 ad }
314 1.1 ad mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
315 1.26 ad cv_init(&module_thread_cv, "modunload");
316 1.26 ad mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
317 1.13 ad #ifdef MODULAR /* XXX */
318 1.11 ad module_init_md();
319 1.12 ad #endif
320 1.16 ad
321 1.16 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
322 1.41 rmind snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules",
323 1.17 ad machine, osrelease);
324 1.16 ad #else /* release */
325 1.41 rmind snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules",
326 1.17 ad machine, __NetBSD_Version__ / 100000000,
327 1.16 ad __NetBSD_Version__ / 1000000 % 100);
328 1.16 ad #endif
329 1.50 elad
330 1.55 elad module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
331 1.55 elad module_listener_cb, NULL);
332 1.59 pooka
333 1.59 pooka __link_set_foreach(mip, modules) {
334 1.59 pooka if ((rv = module_builtin_add(mip, 1, false) != 0))
335 1.59 pooka module_error("builtin %s failed: %d\n",
336 1.59 pooka (*mip)->mi_name, rv);
337 1.59 pooka }
338 1.51 elad }
339 1.51 elad
340 1.50 elad /*
341 1.50 elad * module_init2:
342 1.50 elad *
343 1.50 elad * Start the auto unload kthread.
344 1.50 elad */
345 1.50 elad void
346 1.50 elad module_init2(void)
347 1.50 elad {
348 1.50 elad int error;
349 1.26 ad
350 1.26 ad error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
351 1.26 ad NULL, NULL, "modunload");
352 1.26 ad if (error != 0)
353 1.26 ad panic("module_init: %d", error);
354 1.1 ad }
355 1.1 ad
356 1.34 ad SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup")
357 1.34 ad {
358 1.34 ad const struct sysctlnode *node = NULL;
359 1.34 ad
360 1.34 ad sysctl_createv(clog, 0, NULL, NULL,
361 1.34 ad CTLFLAG_PERMANENT,
362 1.34 ad CTLTYPE_NODE, "kern", NULL,
363 1.34 ad NULL, 0, NULL, 0,
364 1.34 ad CTL_KERN, CTL_EOL);
365 1.34 ad sysctl_createv(clog, 0, NULL, &node,
366 1.34 ad CTLFLAG_PERMANENT,
367 1.34 ad CTLTYPE_NODE, "module",
368 1.34 ad SYSCTL_DESCR("Module options"),
369 1.34 ad NULL, 0, NULL, 0,
370 1.34 ad CTL_KERN, CTL_CREATE, CTL_EOL);
371 1.34 ad
372 1.34 ad if (node == NULL)
373 1.34 ad return;
374 1.34 ad
375 1.34 ad sysctl_createv(clog, 0, &node, NULL,
376 1.34 ad CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
377 1.64 jruoho CTLTYPE_BOOL, "autoload",
378 1.34 ad SYSCTL_DESCR("Enable automatic load of modules"),
379 1.34 ad NULL, 0, &module_autoload_on, 0,
380 1.34 ad CTL_CREATE, CTL_EOL);
381 1.34 ad sysctl_createv(clog, 0, &node, NULL,
382 1.34 ad CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
383 1.64 jruoho CTLTYPE_BOOL, "verbose",
384 1.34 ad SYSCTL_DESCR("Enable verbose output"),
385 1.34 ad NULL, 0, &module_verbose_on, 0,
386 1.34 ad CTL_CREATE, CTL_EOL);
387 1.34 ad }
388 1.34 ad
389 1.1 ad /*
390 1.1 ad * module_init_class:
391 1.1 ad *
392 1.1 ad * Initialize all built-in and pre-loaded modules of the
393 1.1 ad * specified class.
394 1.1 ad */
395 1.1 ad void
396 1.1 ad module_init_class(modclass_t class)
397 1.1 ad {
398 1.63 pooka TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
399 1.59 pooka module_t *mod;
400 1.59 pooka modinfo_t *mi;
401 1.1 ad
402 1.1 ad mutex_enter(&module_lock);
403 1.1 ad /*
404 1.59 pooka * Builtins first. These will not depend on pre-loaded modules
405 1.59 pooka * (because the kernel would not link).
406 1.1 ad */
407 1.59 pooka do {
408 1.59 pooka TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
409 1.59 pooka mi = mod->mod_info;
410 1.59 pooka if (class != MODULE_CLASS_ANY && class != mi->mi_class)
411 1.59 pooka continue;
412 1.63 pooka /*
413 1.63 pooka * If initializing a builtin module fails, don't try
414 1.63 pooka * to load it again. But keep it around and queue it
415 1.63 pooka * on the disabled list after we're done with module
416 1.63 pooka * init.
417 1.63 pooka */
418 1.63 pooka if (module_do_builtin(mi->mi_name, NULL) != 0) {
419 1.63 pooka TAILQ_REMOVE(&module_builtins, mod, mod_chain);
420 1.63 pooka TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
421 1.63 pooka }
422 1.59 pooka break;
423 1.1 ad }
424 1.59 pooka } while (mod != NULL);
425 1.59 pooka
426 1.1 ad /*
427 1.1 ad * Now preloaded modules. These will be pulled off the
428 1.1 ad * list as we call module_do_load();
429 1.1 ad */
430 1.11 ad do {
431 1.59 pooka TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
432 1.11 ad mi = mod->mod_info;
433 1.59 pooka if (class != MODULE_CLASS_ANY && class != mi->mi_class)
434 1.11 ad continue;
435 1.18 ad module_do_load(mi->mi_name, false, 0, NULL, NULL,
436 1.38 christos class, false);
437 1.11 ad break;
438 1.11 ad }
439 1.11 ad } while (mod != NULL);
440 1.63 pooka
441 1.63 pooka /* failed builtin modules remain disabled */
442 1.63 pooka while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
443 1.63 pooka TAILQ_REMOVE(&bi_fail, mod, mod_chain);
444 1.63 pooka TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
445 1.63 pooka }
446 1.63 pooka
447 1.1 ad mutex_exit(&module_lock);
448 1.1 ad }
449 1.1 ad
450 1.1 ad /*
451 1.21 ad * module_compatible:
452 1.1 ad *
453 1.21 ad * Return true if the two supplied kernel versions are said to
454 1.21 ad * have the same binary interface for kernel code. The entire
455 1.21 ad * version is signficant for the development tree (-current),
456 1.21 ad * major and minor versions are significant for official
457 1.21 ad * releases of the system.
458 1.1 ad */
459 1.22 pooka bool
460 1.21 ad module_compatible(int v1, int v2)
461 1.1 ad {
462 1.1 ad
463 1.21 ad #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */
464 1.21 ad return v1 == v2;
465 1.21 ad #else /* release */
466 1.21 ad return abs(v1 - v2) < 10000;
467 1.21 ad #endif
468 1.1 ad }
469 1.1 ad
470 1.1 ad /*
471 1.1 ad * module_load:
472 1.1 ad *
473 1.9 jmmv * Load a single module from the file system.
474 1.1 ad */
475 1.1 ad int
476 1.18 ad module_load(const char *filename, int flags, prop_dictionary_t props,
477 1.23 ad modclass_t class)
478 1.1 ad {
479 1.1 ad int error;
480 1.1 ad
481 1.18 ad /* Authorize. */
482 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
483 1.18 ad 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
484 1.18 ad if (error != 0) {
485 1.18 ad return error;
486 1.18 ad }
487 1.18 ad
488 1.1 ad mutex_enter(&module_lock);
489 1.20 ad error = module_do_load(filename, false, flags, props, NULL, class,
490 1.23 ad false);
491 1.1 ad mutex_exit(&module_lock);
492 1.1 ad
493 1.1 ad return error;
494 1.1 ad }
495 1.1 ad
496 1.1 ad /*
497 1.23 ad * module_autoload:
498 1.23 ad *
499 1.23 ad * Load a single module from the file system, system initiated.
500 1.23 ad */
501 1.23 ad int
502 1.23 ad module_autoload(const char *filename, modclass_t class)
503 1.23 ad {
504 1.23 ad int error;
505 1.23 ad
506 1.23 ad KASSERT(mutex_owned(&module_lock));
507 1.23 ad
508 1.34 ad /* Nothing if the user has disabled it. */
509 1.34 ad if (!module_autoload_on) {
510 1.34 ad return EPERM;
511 1.34 ad }
512 1.34 ad
513 1.56 dholland /* Disallow path separators and magic symlinks. */
514 1.29 ad if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
515 1.29 ad strchr(filename, '.') != NULL) {
516 1.29 ad return EPERM;
517 1.29 ad }
518 1.29 ad
519 1.23 ad /* Authorize. */
520 1.23 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
521 1.23 ad 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
522 1.23 ad if (error != 0) {
523 1.23 ad return error;
524 1.23 ad }
525 1.23 ad
526 1.23 ad return module_do_load(filename, false, 0, NULL, NULL, class, true);
527 1.23 ad }
528 1.23 ad
529 1.23 ad /*
530 1.1 ad * module_unload:
531 1.1 ad *
532 1.1 ad * Find and unload a module by name.
533 1.1 ad */
534 1.1 ad int
535 1.1 ad module_unload(const char *name)
536 1.1 ad {
537 1.1 ad int error;
538 1.1 ad
539 1.18 ad /* Authorize. */
540 1.18 ad error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
541 1.18 ad 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
542 1.18 ad if (error != 0) {
543 1.18 ad return error;
544 1.18 ad }
545 1.18 ad
546 1.1 ad mutex_enter(&module_lock);
547 1.1 ad error = module_do_unload(name);
548 1.1 ad mutex_exit(&module_lock);
549 1.1 ad
550 1.1 ad return error;
551 1.1 ad }
552 1.1 ad
553 1.1 ad /*
554 1.1 ad * module_lookup:
555 1.1 ad *
556 1.1 ad * Look up a module by name.
557 1.1 ad */
558 1.1 ad module_t *
559 1.1 ad module_lookup(const char *name)
560 1.1 ad {
561 1.1 ad module_t *mod;
562 1.1 ad
563 1.1 ad KASSERT(mutex_owned(&module_lock));
564 1.1 ad
565 1.1 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
566 1.1 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
567 1.1 ad break;
568 1.1 ad }
569 1.1 ad }
570 1.1 ad
571 1.1 ad return mod;
572 1.1 ad }
573 1.1 ad
574 1.1 ad /*
575 1.1 ad * module_hold:
576 1.1 ad *
577 1.1 ad * Add a single reference to a module. It's the caller's
578 1.1 ad * responsibility to ensure that the reference is dropped
579 1.1 ad * later.
580 1.1 ad */
581 1.1 ad int
582 1.1 ad module_hold(const char *name)
583 1.1 ad {
584 1.1 ad module_t *mod;
585 1.1 ad
586 1.1 ad mutex_enter(&module_lock);
587 1.1 ad mod = module_lookup(name);
588 1.1 ad if (mod == NULL) {
589 1.1 ad mutex_exit(&module_lock);
590 1.1 ad return ENOENT;
591 1.1 ad }
592 1.1 ad mod->mod_refcnt++;
593 1.1 ad mutex_exit(&module_lock);
594 1.1 ad
595 1.1 ad return 0;
596 1.1 ad }
597 1.1 ad
598 1.1 ad /*
599 1.1 ad * module_rele:
600 1.1 ad *
601 1.1 ad * Release a reference acquired with module_hold().
602 1.1 ad */
603 1.1 ad void
604 1.1 ad module_rele(const char *name)
605 1.1 ad {
606 1.1 ad module_t *mod;
607 1.1 ad
608 1.1 ad mutex_enter(&module_lock);
609 1.1 ad mod = module_lookup(name);
610 1.1 ad if (mod == NULL) {
611 1.1 ad mutex_exit(&module_lock);
612 1.1 ad panic("module_rele: gone");
613 1.1 ad }
614 1.1 ad mod->mod_refcnt--;
615 1.1 ad mutex_exit(&module_lock);
616 1.1 ad }
617 1.1 ad
618 1.1 ad /*
619 1.27 ad * module_enqueue:
620 1.27 ad *
621 1.27 ad * Put a module onto the global list and update counters.
622 1.27 ad */
623 1.53 pooka void
624 1.27 ad module_enqueue(module_t *mod)
625 1.27 ad {
626 1.27 ad int i;
627 1.27 ad
628 1.53 pooka KASSERT(mutex_owned(&module_lock));
629 1.53 pooka
630 1.27 ad /*
631 1.27 ad * If there are requisite modules, put at the head of the queue.
632 1.27 ad * This is so that autounload can unload requisite modules with
633 1.27 ad * only one pass through the queue.
634 1.27 ad */
635 1.27 ad if (mod->mod_nrequired) {
636 1.27 ad TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
637 1.27 ad
638 1.27 ad /* Add references to the requisite modules. */
639 1.27 ad for (i = 0; i < mod->mod_nrequired; i++) {
640 1.27 ad KASSERT(mod->mod_required[i] != NULL);
641 1.27 ad mod->mod_required[i]->mod_refcnt++;
642 1.27 ad }
643 1.27 ad } else {
644 1.27 ad TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
645 1.27 ad }
646 1.27 ad module_count++;
647 1.27 ad module_gen++;
648 1.27 ad }
649 1.27 ad
650 1.27 ad /*
651 1.1 ad * module_do_builtin:
652 1.1 ad *
653 1.59 pooka * Initialize a module from the list of modules that are
654 1.59 pooka * already linked into the kernel.
655 1.1 ad */
656 1.1 ad static int
657 1.1 ad module_do_builtin(const char *name, module_t **modp)
658 1.1 ad {
659 1.1 ad const char *p, *s;
660 1.1 ad char buf[MAXMODNAME];
661 1.59 pooka modinfo_t *mi = NULL;
662 1.59 pooka module_t *mod, *mod2, *mod_loaded;
663 1.1 ad size_t len;
664 1.27 ad int error;
665 1.1 ad
666 1.1 ad KASSERT(mutex_owned(&module_lock));
667 1.1 ad
668 1.1 ad /*
669 1.59 pooka * Search the list to see if we have a module by this name.
670 1.1 ad */
671 1.59 pooka TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
672 1.59 pooka if (strcmp(mod->mod_info->mi_name, name) == 0) {
673 1.59 pooka mi = mod->mod_info;
674 1.59 pooka break;
675 1.1 ad }
676 1.1 ad }
677 1.1 ad
678 1.1 ad /*
679 1.59 pooka * Check to see if already loaded. This might happen if we
680 1.59 pooka * were already loaded as a dependency.
681 1.1 ad */
682 1.59 pooka if ((mod_loaded = module_lookup(name)) != NULL) {
683 1.59 pooka KASSERT(mod == NULL);
684 1.59 pooka if (modp)
685 1.59 pooka *modp = mod_loaded;
686 1.59 pooka return 0;
687 1.1 ad }
688 1.59 pooka
689 1.59 pooka /* Note! This is from TAILQ, not immediate above */
690 1.59 pooka if (mi == NULL)
691 1.59 pooka panic("can't find `%s'", name);
692 1.1 ad
693 1.1 ad /*
694 1.1 ad * Initialize pre-requisites.
695 1.1 ad */
696 1.1 ad if (mi->mi_required != NULL) {
697 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
698 1.1 ad if (*s == ',')
699 1.1 ad s++;
700 1.1 ad p = s;
701 1.1 ad while (*p != '\0' && *p != ',')
702 1.1 ad p++;
703 1.1 ad len = min(p - s + 1, sizeof(buf));
704 1.1 ad strlcpy(buf, s, len);
705 1.1 ad if (buf[0] == '\0')
706 1.1 ad break;
707 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
708 1.1 ad module_error("too many required modules");
709 1.1 ad return EINVAL;
710 1.1 ad }
711 1.1 ad error = module_do_builtin(buf, &mod2);
712 1.1 ad if (error != 0) {
713 1.1 ad return error;
714 1.1 ad }
715 1.1 ad mod->mod_required[mod->mod_nrequired++] = mod2;
716 1.1 ad }
717 1.1 ad }
718 1.1 ad
719 1.1 ad /*
720 1.1 ad * Try to initialize the module.
721 1.1 ad */
722 1.12 ad KASSERT(module_active == NULL);
723 1.12 ad module_active = mod;
724 1.1 ad error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
725 1.12 ad module_active = NULL;
726 1.1 ad if (error != 0) {
727 1.1 ad module_error("builtin module `%s' "
728 1.1 ad "failed to init", mi->mi_name);
729 1.1 ad return error;
730 1.1 ad }
731 1.59 pooka
732 1.59 pooka /* load always succeeds after this point */
733 1.59 pooka
734 1.59 pooka TAILQ_REMOVE(&module_builtins, mod, mod_chain);
735 1.59 pooka module_builtinlist--;
736 1.59 pooka if (modp != NULL) {
737 1.59 pooka *modp = mod;
738 1.59 pooka }
739 1.50 elad if (mi->mi_class == MODULE_CLASS_SECMODEL)
740 1.50 elad secmodel_register();
741 1.27 ad module_enqueue(mod);
742 1.1 ad return 0;
743 1.1 ad }
744 1.1 ad
745 1.1 ad /*
746 1.1 ad * module_do_load:
747 1.1 ad *
748 1.1 ad * Helper routine: load a module from the file system, or one
749 1.1 ad * pushed by the boot loader.
750 1.1 ad */
751 1.1 ad static int
752 1.27 ad module_do_load(const char *name, bool isdep, int flags,
753 1.20 ad prop_dictionary_t props, module_t **modp, modclass_t class,
754 1.20 ad bool autoload)
755 1.1 ad {
756 1.1 ad static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
757 1.1 ad static int depth;
758 1.1 ad const int maxdepth = 6;
759 1.1 ad modinfo_t *mi;
760 1.1 ad module_t *mod, *mod2;
761 1.46 jnemeth prop_dictionary_t filedict;
762 1.54 pooka char buf[MAXMODNAME];
763 1.1 ad const char *s, *p;
764 1.1 ad int error;
765 1.54 pooka size_t len;
766 1.1 ad
767 1.1 ad KASSERT(mutex_owned(&module_lock));
768 1.1 ad
769 1.46 jnemeth filedict = NULL;
770 1.1 ad error = 0;
771 1.1 ad
772 1.1 ad /*
773 1.1 ad * Avoid recursing too far.
774 1.1 ad */
775 1.1 ad if (++depth > maxdepth) {
776 1.1 ad module_error("too many required modules");
777 1.1 ad depth--;
778 1.1 ad return EMLINK;
779 1.1 ad }
780 1.1 ad
781 1.1 ad /*
782 1.59 pooka * Search the list of disabled builtins first.
783 1.59 pooka */
784 1.59 pooka TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
785 1.59 pooka if (strcmp(mod->mod_info->mi_name, name) == 0) {
786 1.59 pooka break;
787 1.59 pooka }
788 1.59 pooka }
789 1.59 pooka if (mod) {
790 1.59 pooka if ((flags & MODCTL_LOAD_FORCE) == 0) {
791 1.62 pooka if (!autoload) {
792 1.62 pooka module_error("use -f to reinstate "
793 1.62 pooka "builtin module \"%s\"", name);
794 1.62 pooka }
795 1.59 pooka depth--;
796 1.59 pooka return EPERM;
797 1.59 pooka } else {
798 1.59 pooka error = module_do_builtin(name, NULL);
799 1.59 pooka depth--;
800 1.59 pooka return error;
801 1.59 pooka }
802 1.59 pooka }
803 1.59 pooka
804 1.59 pooka /*
805 1.1 ad * Load the module and link. Before going to the file system,
806 1.57 pooka * scan the list of modules loaded by the boot loader.
807 1.1 ad */
808 1.1 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
809 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
810 1.1 ad TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
811 1.1 ad break;
812 1.1 ad }
813 1.1 ad }
814 1.14 rumble if (mod != NULL) {
815 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
816 1.14 rumble } else {
817 1.27 ad /*
818 1.27 ad * If a requisite module, check to see if it is
819 1.27 ad * already present.
820 1.27 ad */
821 1.27 ad if (isdep) {
822 1.27 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
823 1.27 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
824 1.27 ad break;
825 1.27 ad }
826 1.27 ad }
827 1.27 ad if (mod != NULL) {
828 1.27 ad if (modp != NULL) {
829 1.27 ad *modp = mod;
830 1.27 ad }
831 1.27 ad depth--;
832 1.27 ad return 0;
833 1.27 ad }
834 1.27 ad }
835 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
836 1.1 ad if (mod == NULL) {
837 1.38 christos module_error("out of memory for `%s'", name);
838 1.1 ad depth--;
839 1.1 ad return ENOMEM;
840 1.1 ad }
841 1.54 pooka
842 1.54 pooka error = module_load_vfs(name, flags, autoload, mod, &filedict);
843 1.1 ad if (error != 0) {
844 1.1 ad kmem_free(mod, sizeof(*mod));
845 1.1 ad depth--;
846 1.8 rumble return error;
847 1.7 rumble }
848 1.54 pooka mod->mod_source = MODULE_SOURCE_FILESYS;
849 1.14 rumble TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
850 1.54 pooka
851 1.11 ad error = module_fetch_info(mod);
852 1.11 ad if (error != 0) {
853 1.33 ad module_error("cannot fetch module info for `%s'",
854 1.33 ad name);
855 1.11 ad goto fail;
856 1.11 ad }
857 1.1 ad }
858 1.1 ad
859 1.1 ad /*
860 1.11 ad * Check compatibility.
861 1.1 ad */
862 1.1 ad mi = mod->mod_info;
863 1.3 rumble if (strlen(mi->mi_name) >= MAXMODNAME) {
864 1.3 rumble error = EINVAL;
865 1.32 christos module_error("module name `%s' too long", mi->mi_name);
866 1.3 rumble goto fail;
867 1.3 rumble }
868 1.21 ad if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
869 1.33 ad module_error("module built for `%d', system `%d'",
870 1.32 christos mi->mi_version, __NetBSD_Version__);
871 1.24 ad if ((flags & MODCTL_LOAD_FORCE) != 0) {
872 1.24 ad module_error("forced load, system may be unstable");
873 1.24 ad } else {
874 1.24 ad error = EPROGMISMATCH;
875 1.24 ad goto fail;
876 1.24 ad }
877 1.21 ad }
878 1.3 rumble
879 1.1 ad /*
880 1.18 ad * If a specific kind of module was requested, ensure that we have
881 1.18 ad * a match.
882 1.18 ad */
883 1.18 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
884 1.34 ad module_print("incompatible module class for `%s' (%d != %d)",
885 1.32 christos name, class, mi->mi_class);
886 1.18 ad error = ENOENT;
887 1.18 ad goto fail;
888 1.18 ad }
889 1.18 ad
890 1.18 ad /*
891 1.27 ad * If loading a dependency, `name' is a plain module name.
892 1.1 ad * The name must match.
893 1.1 ad */
894 1.27 ad if (isdep && strcmp(mi->mi_name, name) != 0) {
895 1.32 christos module_error("dependency name mismatch (`%s' != `%s')",
896 1.32 christos name, mi->mi_name);
897 1.1 ad error = ENOENT;
898 1.1 ad goto fail;
899 1.1 ad }
900 1.1 ad
901 1.1 ad /*
902 1.3 rumble * Check to see if the module is already loaded. If so, we may
903 1.3 rumble * have been recursively called to handle a dependency, so be sure
904 1.3 rumble * to set modp.
905 1.1 ad */
906 1.3 rumble if ((mod2 = module_lookup(mi->mi_name)) != NULL) {
907 1.5 rumble if (modp != NULL)
908 1.5 rumble *modp = mod2;
909 1.34 ad module_print("module `%s' already loaded", mi->mi_name);
910 1.1 ad error = EEXIST;
911 1.1 ad goto fail;
912 1.1 ad }
913 1.3 rumble
914 1.3 rumble /*
915 1.3 rumble * Block circular dependencies.
916 1.3 rumble */
917 1.1 ad TAILQ_FOREACH(mod2, &pending, mod_chain) {
918 1.1 ad if (mod == mod2) {
919 1.1 ad continue;
920 1.1 ad }
921 1.1 ad if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
922 1.1 ad error = EDEADLK;
923 1.32 christos module_error("circular dependency detected for `%s'",
924 1.32 christos mi->mi_name);
925 1.1 ad goto fail;
926 1.1 ad }
927 1.1 ad }
928 1.1 ad
929 1.1 ad /*
930 1.1 ad * Now try to load any requisite modules.
931 1.1 ad */
932 1.1 ad if (mi->mi_required != NULL) {
933 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
934 1.1 ad if (*s == ',')
935 1.1 ad s++;
936 1.1 ad p = s;
937 1.1 ad while (*p != '\0' && *p != ',')
938 1.1 ad p++;
939 1.3 rumble len = p - s + 1;
940 1.3 rumble if (len >= MAXMODNAME) {
941 1.3 rumble error = EINVAL;
942 1.32 christos module_error("required module name `%s'"
943 1.32 christos " too long", mi->mi_required);
944 1.3 rumble goto fail;
945 1.3 rumble }
946 1.1 ad strlcpy(buf, s, len);
947 1.1 ad if (buf[0] == '\0')
948 1.1 ad break;
949 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
950 1.1 ad error = EINVAL;
951 1.32 christos module_error("too many required modules (%d)",
952 1.32 christos mod->mod_nrequired);
953 1.1 ad goto fail;
954 1.1 ad }
955 1.6 rumble if (strcmp(buf, mi->mi_name) == 0) {
956 1.6 rumble error = EDEADLK;
957 1.32 christos module_error("self-dependency detected for "
958 1.32 christos "`%s'", mi->mi_name);
959 1.6 rumble goto fail;
960 1.6 rumble }
961 1.9 jmmv error = module_do_load(buf, true, flags, NULL,
962 1.18 ad &mod->mod_required[mod->mod_nrequired++],
963 1.20 ad MODULE_CLASS_ANY, true);
964 1.27 ad if (error != 0)
965 1.1 ad goto fail;
966 1.1 ad }
967 1.1 ad }
968 1.1 ad
969 1.1 ad /*
970 1.15 ad * We loaded all needed modules successfully: perform global
971 1.15 ad * relocations and initialize.
972 1.1 ad */
973 1.15 ad error = kobj_affix(mod->mod_kobj, mi->mi_name);
974 1.15 ad if (error != 0) {
975 1.36 ad /* Cannot touch 'mi' as the module is now gone. */
976 1.36 ad module_error("unable to affix module `%s'", name);
977 1.15 ad goto fail2;
978 1.15 ad }
979 1.15 ad
980 1.54 pooka if (filedict) {
981 1.54 pooka if (!module_merge_dicts(filedict, props)) {
982 1.54 pooka module_error("module properties failed");
983 1.54 pooka error = EINVAL;
984 1.46 jnemeth goto fail;
985 1.46 jnemeth }
986 1.46 jnemeth }
987 1.12 ad KASSERT(module_active == NULL);
988 1.12 ad module_active = mod;
989 1.54 pooka error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
990 1.12 ad module_active = NULL;
991 1.54 pooka if (filedict) {
992 1.46 jnemeth prop_object_release(filedict);
993 1.54 pooka filedict = NULL;
994 1.46 jnemeth }
995 1.1 ad if (error != 0) {
996 1.32 christos module_error("modcmd function returned error %d for `%s'",
997 1.32 christos error, mi->mi_name);
998 1.1 ad goto fail;
999 1.1 ad }
1000 1.54 pooka
1001 1.50 elad if (mi->mi_class == MODULE_CLASS_SECMODEL)
1002 1.50 elad secmodel_register();
1003 1.1 ad
1004 1.1 ad /*
1005 1.1 ad * Good, the module loaded successfully. Put it onto the
1006 1.1 ad * list and add references to its requisite modules.
1007 1.1 ad */
1008 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
1009 1.27 ad module_enqueue(mod);
1010 1.1 ad if (modp != NULL) {
1011 1.1 ad *modp = mod;
1012 1.1 ad }
1013 1.26 ad if (autoload) {
1014 1.26 ad /*
1015 1.26 ad * Arrange to try unloading the module after
1016 1.26 ad * a short delay.
1017 1.26 ad */
1018 1.26 ad mod->mod_autotime = time_second + module_autotime;
1019 1.26 ad module_thread_kick();
1020 1.26 ad }
1021 1.1 ad depth--;
1022 1.1 ad return 0;
1023 1.7 rumble
1024 1.1 ad fail:
1025 1.15 ad kobj_unload(mod->mod_kobj);
1026 1.15 ad fail2:
1027 1.54 pooka if (filedict != NULL) {
1028 1.54 pooka prop_object_release(filedict);
1029 1.54 pooka filedict = NULL;
1030 1.54 pooka }
1031 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
1032 1.1 ad kmem_free(mod, sizeof(*mod));
1033 1.1 ad depth--;
1034 1.1 ad return error;
1035 1.1 ad }
1036 1.1 ad
1037 1.1 ad /*
1038 1.1 ad * module_do_unload:
1039 1.1 ad *
1040 1.1 ad * Helper routine: do the dirty work of unloading a module.
1041 1.1 ad */
1042 1.1 ad static int
1043 1.1 ad module_do_unload(const char *name)
1044 1.1 ad {
1045 1.1 ad module_t *mod;
1046 1.1 ad int error;
1047 1.1 ad u_int i;
1048 1.1 ad
1049 1.1 ad KASSERT(mutex_owned(&module_lock));
1050 1.1 ad
1051 1.1 ad mod = module_lookup(name);
1052 1.1 ad if (mod == NULL) {
1053 1.32 christos module_error("module `%s' not found", name);
1054 1.1 ad return ENOENT;
1055 1.1 ad }
1056 1.59 pooka if (mod->mod_refcnt != 0) {
1057 1.34 ad module_print("module `%s' busy", name);
1058 1.1 ad return EBUSY;
1059 1.1 ad }
1060 1.12 ad KASSERT(module_active == NULL);
1061 1.12 ad module_active = mod;
1062 1.1 ad error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
1063 1.12 ad module_active = NULL;
1064 1.1 ad if (error != 0) {
1065 1.34 ad module_print("cannot unload module `%s' error=%d", name,
1066 1.33 ad error);
1067 1.1 ad return error;
1068 1.1 ad }
1069 1.50 elad if (mod->mod_info->mi_class == MODULE_CLASS_SECMODEL)
1070 1.50 elad secmodel_deregister();
1071 1.1 ad module_count--;
1072 1.1 ad TAILQ_REMOVE(&module_list, mod, mod_chain);
1073 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
1074 1.1 ad mod->mod_required[i]->mod_refcnt--;
1075 1.1 ad }
1076 1.1 ad if (mod->mod_kobj != NULL) {
1077 1.1 ad kobj_unload(mod->mod_kobj);
1078 1.1 ad }
1079 1.59 pooka if (mod->mod_source == MODULE_SOURCE_KERNEL) {
1080 1.59 pooka mod->mod_nrequired = 0; /* will be re-parsed */
1081 1.59 pooka TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
1082 1.59 pooka module_builtinlist++;
1083 1.59 pooka } else {
1084 1.59 pooka kmem_free(mod, sizeof(*mod));
1085 1.59 pooka }
1086 1.26 ad module_gen++;
1087 1.1 ad
1088 1.1 ad return 0;
1089 1.1 ad }
1090 1.1 ad
1091 1.1 ad /*
1092 1.1 ad * module_prime:
1093 1.1 ad *
1094 1.1 ad * Push a module loaded by the bootloader onto our internal
1095 1.1 ad * list.
1096 1.1 ad */
1097 1.1 ad int
1098 1.1 ad module_prime(void *base, size_t size)
1099 1.1 ad {
1100 1.1 ad module_t *mod;
1101 1.1 ad int error;
1102 1.1 ad
1103 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
1104 1.1 ad if (mod == NULL) {
1105 1.1 ad return ENOMEM;
1106 1.1 ad }
1107 1.1 ad mod->mod_source = MODULE_SOURCE_BOOT;
1108 1.1 ad
1109 1.15 ad error = kobj_load_mem(&mod->mod_kobj, base, size);
1110 1.1 ad if (error != 0) {
1111 1.11 ad kmem_free(mod, sizeof(*mod));
1112 1.11 ad module_error("unable to load object pushed by boot loader");
1113 1.11 ad return error;
1114 1.11 ad }
1115 1.11 ad error = module_fetch_info(mod);
1116 1.11 ad if (error != 0) {
1117 1.11 ad kobj_unload(mod->mod_kobj);
1118 1.1 ad kmem_free(mod, sizeof(*mod));
1119 1.11 ad module_error("unable to load object pushed by boot loader");
1120 1.1 ad return error;
1121 1.1 ad }
1122 1.11 ad
1123 1.1 ad TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
1124 1.1 ad
1125 1.1 ad return 0;
1126 1.1 ad }
1127 1.11 ad
1128 1.11 ad /*
1129 1.11 ad * module_fetch_into:
1130 1.11 ad *
1131 1.11 ad * Fetch modinfo record from a loaded module.
1132 1.11 ad */
1133 1.11 ad static int
1134 1.11 ad module_fetch_info(module_t *mod)
1135 1.11 ad {
1136 1.11 ad int error;
1137 1.11 ad void *addr;
1138 1.11 ad size_t size;
1139 1.11 ad
1140 1.11 ad /*
1141 1.11 ad * Find module info record and check compatibility.
1142 1.11 ad */
1143 1.11 ad error = kobj_find_section(mod->mod_kobj, "link_set_modules",
1144 1.11 ad &addr, &size);
1145 1.11 ad if (error != 0) {
1146 1.11 ad module_error("`link_set_modules' section not present");
1147 1.11 ad return error;
1148 1.11 ad }
1149 1.11 ad if (size != sizeof(modinfo_t **)) {
1150 1.11 ad module_error("`link_set_modules' section wrong size");
1151 1.11 ad return error;
1152 1.11 ad }
1153 1.11 ad mod->mod_info = *(modinfo_t **)addr;
1154 1.11 ad
1155 1.11 ad return 0;
1156 1.11 ad }
1157 1.12 ad
1158 1.12 ad /*
1159 1.12 ad * module_find_section:
1160 1.12 ad *
1161 1.12 ad * Allows a module that is being initialized to look up a section
1162 1.12 ad * within its ELF object.
1163 1.12 ad */
1164 1.12 ad int
1165 1.12 ad module_find_section(const char *name, void **addr, size_t *size)
1166 1.12 ad {
1167 1.12 ad
1168 1.12 ad KASSERT(mutex_owned(&module_lock));
1169 1.12 ad KASSERT(module_active != NULL);
1170 1.12 ad
1171 1.12 ad return kobj_find_section(module_active->mod_kobj, name, addr, size);
1172 1.12 ad }
1173 1.26 ad
1174 1.26 ad /*
1175 1.26 ad * module_thread:
1176 1.26 ad *
1177 1.26 ad * Automatically unload modules. We try once to unload autoloaded
1178 1.26 ad * modules after module_autotime seconds. If the system is under
1179 1.26 ad * severe memory pressure, we'll try unloading all modules.
1180 1.26 ad */
1181 1.26 ad static void
1182 1.26 ad module_thread(void *cookie)
1183 1.26 ad {
1184 1.26 ad module_t *mod, *next;
1185 1.28 ad modinfo_t *mi;
1186 1.28 ad int error;
1187 1.26 ad
1188 1.26 ad for (;;) {
1189 1.26 ad mutex_enter(&module_lock);
1190 1.26 ad for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
1191 1.26 ad next = TAILQ_NEXT(mod, mod_chain);
1192 1.61 pooka if (mod->mod_source == MODULE_SOURCE_KERNEL)
1193 1.61 pooka continue;
1194 1.37 ad if (uvmexp.free < uvmexp.freemin) {
1195 1.26 ad module_thread_ticks = hz;
1196 1.26 ad } else if (mod->mod_autotime == 0) {
1197 1.26 ad continue;
1198 1.26 ad } else if (time_second < mod->mod_autotime) {
1199 1.26 ad module_thread_ticks = hz;
1200 1.26 ad continue;
1201 1.37 ad } else {
1202 1.26 ad mod->mod_autotime = 0;
1203 1.37 ad }
1204 1.28 ad /*
1205 1.28 ad * If this module wants to avoid autounload then
1206 1.28 ad * skip it. Some modules can ping-pong in and out
1207 1.28 ad * because their use is transient but often.
1208 1.28 ad * Example: exec_script.
1209 1.28 ad */
1210 1.28 ad mi = mod->mod_info;
1211 1.28 ad error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
1212 1.28 ad if (error == 0 || error == ENOTTY) {
1213 1.37 ad (void)module_do_unload(mi->mi_name);
1214 1.28 ad }
1215 1.26 ad }
1216 1.26 ad mutex_exit(&module_lock);
1217 1.26 ad
1218 1.26 ad mutex_enter(&module_thread_lock);
1219 1.26 ad (void)cv_timedwait(&module_thread_cv, &module_thread_lock,
1220 1.26 ad module_thread_ticks);
1221 1.26 ad module_thread_ticks = 0;
1222 1.26 ad mutex_exit(&module_thread_lock);
1223 1.26 ad }
1224 1.26 ad }
1225 1.26 ad
1226 1.26 ad /*
1227 1.26 ad * module_thread:
1228 1.26 ad *
1229 1.26 ad * Kick the module thread into action, perhaps because the
1230 1.26 ad * system is low on memory.
1231 1.26 ad */
1232 1.26 ad void
1233 1.26 ad module_thread_kick(void)
1234 1.26 ad {
1235 1.26 ad
1236 1.26 ad mutex_enter(&module_thread_lock);
1237 1.26 ad module_thread_ticks = hz;
1238 1.26 ad cv_broadcast(&module_thread_cv);
1239 1.26 ad mutex_exit(&module_thread_lock);
1240 1.26 ad }
1241 1.30 ad
1242 1.30 ad #ifdef DDB
1243 1.30 ad /*
1244 1.30 ad * module_whatis:
1245 1.30 ad *
1246 1.30 ad * Helper routine for DDB.
1247 1.30 ad */
1248 1.30 ad void
1249 1.30 ad module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
1250 1.30 ad {
1251 1.30 ad module_t *mod;
1252 1.30 ad size_t msize;
1253 1.30 ad vaddr_t maddr;
1254 1.30 ad
1255 1.30 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
1256 1.43 ad if (mod->mod_kobj == NULL) {
1257 1.43 ad continue;
1258 1.43 ad }
1259 1.49 dyoung if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1260 1.49 dyoung continue;
1261 1.30 ad if (addr < maddr || addr >= maddr + msize) {
1262 1.30 ad continue;
1263 1.30 ad }
1264 1.30 ad (*pr)("%p is %p+%zu, in kernel module `%s'\n",
1265 1.30 ad (void *)addr, (void *)maddr,
1266 1.30 ad (size_t)(addr - maddr), mod->mod_info->mi_name);
1267 1.30 ad }
1268 1.30 ad }
1269 1.30 ad
1270 1.30 ad /*
1271 1.30 ad * module_print_list:
1272 1.30 ad *
1273 1.30 ad * Helper routine for DDB.
1274 1.30 ad */
1275 1.30 ad void
1276 1.30 ad module_print_list(void (*pr)(const char *, ...))
1277 1.30 ad {
1278 1.30 ad const char *src;
1279 1.30 ad module_t *mod;
1280 1.30 ad size_t msize;
1281 1.30 ad vaddr_t maddr;
1282 1.30 ad
1283 1.30 ad (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
1284 1.30 ad
1285 1.30 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
1286 1.30 ad switch (mod->mod_source) {
1287 1.30 ad case MODULE_SOURCE_KERNEL:
1288 1.30 ad src = "builtin";
1289 1.30 ad break;
1290 1.30 ad case MODULE_SOURCE_FILESYS:
1291 1.30 ad src = "filesys";
1292 1.30 ad break;
1293 1.30 ad case MODULE_SOURCE_BOOT:
1294 1.30 ad src = "boot";
1295 1.30 ad break;
1296 1.30 ad default:
1297 1.30 ad src = "unknown";
1298 1.30 ad break;
1299 1.30 ad }
1300 1.49 dyoung if (mod->mod_kobj == NULL) {
1301 1.43 ad maddr = 0;
1302 1.43 ad msize = 0;
1303 1.49 dyoung } else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1304 1.49 dyoung continue;
1305 1.31 ad (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
1306 1.30 ad (long)maddr, (long)msize, src);
1307 1.30 ad }
1308 1.30 ad }
1309 1.30 ad #endif /* DDB */
1310 1.46 jnemeth
1311 1.47 jnemeth static bool
1312 1.47 jnemeth module_merge_dicts(prop_dictionary_t existing_dict,
1313 1.47 jnemeth const prop_dictionary_t new_dict)
1314 1.47 jnemeth {
1315 1.47 jnemeth prop_dictionary_keysym_t props_keysym;
1316 1.47 jnemeth prop_object_iterator_t props_iter;
1317 1.47 jnemeth prop_object_t props_obj;
1318 1.47 jnemeth const char *props_key;
1319 1.47 jnemeth bool error;
1320 1.47 jnemeth
1321 1.52 jnemeth if (new_dict == NULL) { /* nothing to merge */
1322 1.52 jnemeth return true;
1323 1.52 jnemeth }
1324 1.52 jnemeth
1325 1.47 jnemeth error = false;
1326 1.47 jnemeth props_iter = prop_dictionary_iterator(new_dict);
1327 1.47 jnemeth if (props_iter == NULL) {
1328 1.47 jnemeth return false;
1329 1.47 jnemeth }
1330 1.47 jnemeth
1331 1.47 jnemeth while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
1332 1.47 jnemeth props_keysym = (prop_dictionary_keysym_t)props_obj;
1333 1.47 jnemeth props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
1334 1.47 jnemeth props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
1335 1.47 jnemeth if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
1336 1.47 jnemeth props_key, props_obj)) {
1337 1.47 jnemeth error = true;
1338 1.47 jnemeth goto out;
1339 1.47 jnemeth }
1340 1.47 jnemeth }
1341 1.47 jnemeth error = false;
1342 1.47 jnemeth
1343 1.47 jnemeth out:
1344 1.47 jnemeth prop_object_iterator_release(props_iter);
1345 1.47 jnemeth
1346 1.47 jnemeth return !error;
1347 1.47 jnemeth }
1348