kern_module.c revision 1.1 1 1.1 ad /* $NetBSD: kern_module.c,v 1.1 2008/01/16 12:34:52 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.1 ad * Redistribution and use in source and binary forms, with or without
8 1.1 ad * modification, are permitted provided that the following conditions
9 1.1 ad * are met:
10 1.1 ad * 1. Redistributions of source code must retain the above copyright
11 1.1 ad * notice, this list of conditions and the following disclaimer.
12 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ad * notice, this list of conditions and the following disclaimer in the
14 1.1 ad * documentation and/or other materials provided with the distribution.
15 1.1 ad * 3. All advertising materials mentioning features or use of this software
16 1.1 ad * must display the following acknowledgement:
17 1.1 ad * This product includes software developed by the NetBSD
18 1.1 ad * Foundation, Inc. and its contributors.
19 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1 ad * contributors may be used to endorse or promote products derived
21 1.1 ad * from this software without specific prior written permission.
22 1.1 ad *
23 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
34 1.1 ad */
35 1.1 ad
36 1.1 ad /*
37 1.1 ad * Kernel modules.
38 1.1 ad */
39 1.1 ad
40 1.1 ad #include <sys/cdefs.h>
41 1.1 ad __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.1 2008/01/16 12:34:52 ad Exp $");
42 1.1 ad
43 1.1 ad #include <sys/param.h>
44 1.1 ad #include <sys/systm.h>
45 1.1 ad #include <sys/fcntl.h>
46 1.1 ad #include <sys/proc.h>
47 1.1 ad #include <sys/kauth.h>
48 1.1 ad #include <sys/kobj.h>
49 1.1 ad #include <sys/kmem.h>
50 1.1 ad #include <sys/module.h>
51 1.1 ad #include <sys/syscall.h>
52 1.1 ad #include <sys/syscallargs.h>
53 1.1 ad
54 1.1 ad #include <uvm/uvm_extern.h>
55 1.1 ad
56 1.1 ad #include <machine/stdarg.h>
57 1.1 ad
58 1.1 ad struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list);
59 1.1 ad struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
60 1.1 ad u_int module_count;
61 1.1 ad kmutex_t module_lock;
62 1.1 ad
63 1.1 ad /* Ensure that the kernel's link set isn't empty. */
64 1.1 ad static modinfo_t module_dummy;
65 1.1 ad __link_set_add_rodata(modules, module_dummy);
66 1.1 ad
67 1.1 ad static module_t *module_lookup(const char *);
68 1.1 ad static int module_do_load(const char *, bool, bool, module_t **);
69 1.1 ad static int module_do_unload(const char *);
70 1.1 ad static void module_error(const char *, ...);
71 1.1 ad static int module_do_builtin(const char *, module_t **);
72 1.1 ad
73 1.1 ad /*
74 1.1 ad * module_error:
75 1.1 ad *
76 1.1 ad * Utility function: log an error.
77 1.1 ad */
78 1.1 ad static void
79 1.1 ad module_error(const char *fmt, ...)
80 1.1 ad {
81 1.1 ad va_list ap;
82 1.1 ad
83 1.1 ad va_start(ap, fmt);
84 1.1 ad printf("WARNING: module error: ");
85 1.1 ad vprintf(fmt, ap);
86 1.1 ad printf("\n");
87 1.1 ad va_end(ap);
88 1.1 ad }
89 1.1 ad
90 1.1 ad /*
91 1.1 ad * module_init:
92 1.1 ad *
93 1.1 ad * Initialize the module subsystem.
94 1.1 ad */
95 1.1 ad void
96 1.1 ad module_init(void)
97 1.1 ad {
98 1.1 ad extern struct vm_map *lkm_map;
99 1.1 ad
100 1.1 ad if (lkm_map == NULL)
101 1.1 ad lkm_map = kernel_map;
102 1.1 ad mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE);
103 1.1 ad }
104 1.1 ad
105 1.1 ad /*
106 1.1 ad * module_init_class:
107 1.1 ad *
108 1.1 ad * Initialize all built-in and pre-loaded modules of the
109 1.1 ad * specified class.
110 1.1 ad */
111 1.1 ad void
112 1.1 ad module_init_class(modclass_t class)
113 1.1 ad {
114 1.1 ad __link_set_decl(modules, modinfo_t);
115 1.1 ad modinfo_t *const *mip, *mi;
116 1.1 ad module_t *mod;
117 1.1 ad
118 1.1 ad mutex_enter(&module_lock);
119 1.1 ad /*
120 1.1 ad * Builtins first. These can't depend on pre-loaded modules.
121 1.1 ad */
122 1.1 ad __link_set_foreach(mip, modules) {
123 1.1 ad mi = *mip;
124 1.1 ad if (mi == &module_dummy) {
125 1.1 ad continue;
126 1.1 ad }
127 1.1 ad if (class != MODULE_CLASS_ANY && class != mi->mi_class) {
128 1.1 ad continue;
129 1.1 ad }
130 1.1 ad (void)module_do_builtin(mi->mi_name, NULL);
131 1.1 ad }
132 1.1 ad /*
133 1.1 ad * Now preloaded modules. These will be pulled off the
134 1.1 ad * list as we call module_do_load();
135 1.1 ad */
136 1.1 ad while ((mod = TAILQ_FIRST(&module_bootlist)) != NULL) {
137 1.1 ad module_do_load(mod->mod_info->mi_name, false, false, NULL);
138 1.1 ad }
139 1.1 ad mutex_exit(&module_lock);
140 1.1 ad }
141 1.1 ad
142 1.1 ad /*
143 1.1 ad * module_jettison:
144 1.1 ad *
145 1.1 ad * Return memory used by pre-loaded modules to the freelist.
146 1.1 ad */
147 1.1 ad void
148 1.1 ad module_jettison(void)
149 1.1 ad {
150 1.1 ad
151 1.1 ad /* XXX nothing yet */
152 1.1 ad }
153 1.1 ad
154 1.1 ad /*
155 1.1 ad * module_load:
156 1.1 ad *
157 1.1 ad * Load a single module from the file system. If force is set,
158 1.1 ad * bypass the version check.
159 1.1 ad */
160 1.1 ad int
161 1.1 ad module_load(const char *filename, bool force)
162 1.1 ad {
163 1.1 ad int error;
164 1.1 ad
165 1.1 ad mutex_enter(&module_lock);
166 1.1 ad error = module_do_load(filename, false, force, NULL);
167 1.1 ad mutex_exit(&module_lock);
168 1.1 ad
169 1.1 ad return error;
170 1.1 ad }
171 1.1 ad
172 1.1 ad /*
173 1.1 ad * module_unload:
174 1.1 ad *
175 1.1 ad * Find and unload a module by name.
176 1.1 ad */
177 1.1 ad int
178 1.1 ad module_unload(const char *name)
179 1.1 ad {
180 1.1 ad int error;
181 1.1 ad
182 1.1 ad mutex_enter(&module_lock);
183 1.1 ad error = module_do_unload(name);
184 1.1 ad mutex_exit(&module_lock);
185 1.1 ad
186 1.1 ad return error;
187 1.1 ad }
188 1.1 ad
189 1.1 ad /*
190 1.1 ad * module_lookup:
191 1.1 ad *
192 1.1 ad * Look up a module by name.
193 1.1 ad */
194 1.1 ad module_t *
195 1.1 ad module_lookup(const char *name)
196 1.1 ad {
197 1.1 ad module_t *mod;
198 1.1 ad
199 1.1 ad KASSERT(mutex_owned(&module_lock));
200 1.1 ad
201 1.1 ad TAILQ_FOREACH(mod, &module_list, mod_chain) {
202 1.1 ad if (strcmp(mod->mod_info->mi_name, name) == 0) {
203 1.1 ad break;
204 1.1 ad }
205 1.1 ad }
206 1.1 ad
207 1.1 ad return mod;
208 1.1 ad }
209 1.1 ad
210 1.1 ad /*
211 1.1 ad * module_hold:
212 1.1 ad *
213 1.1 ad * Add a single reference to a module. It's the caller's
214 1.1 ad * responsibility to ensure that the reference is dropped
215 1.1 ad * later.
216 1.1 ad */
217 1.1 ad int
218 1.1 ad module_hold(const char *name)
219 1.1 ad {
220 1.1 ad module_t *mod;
221 1.1 ad
222 1.1 ad mutex_enter(&module_lock);
223 1.1 ad mod = module_lookup(name);
224 1.1 ad if (mod == NULL) {
225 1.1 ad mutex_exit(&module_lock);
226 1.1 ad return ENOENT;
227 1.1 ad }
228 1.1 ad mod->mod_refcnt++;
229 1.1 ad mutex_exit(&module_lock);
230 1.1 ad
231 1.1 ad return 0;
232 1.1 ad }
233 1.1 ad
234 1.1 ad /*
235 1.1 ad * module_rele:
236 1.1 ad *
237 1.1 ad * Release a reference acquired with module_hold().
238 1.1 ad */
239 1.1 ad void
240 1.1 ad module_rele(const char *name)
241 1.1 ad {
242 1.1 ad module_t *mod;
243 1.1 ad
244 1.1 ad mutex_enter(&module_lock);
245 1.1 ad mod = module_lookup(name);
246 1.1 ad if (mod == NULL) {
247 1.1 ad mutex_exit(&module_lock);
248 1.1 ad panic("module_rele: gone");
249 1.1 ad }
250 1.1 ad mod->mod_refcnt--;
251 1.1 ad mutex_exit(&module_lock);
252 1.1 ad }
253 1.1 ad
254 1.1 ad /*
255 1.1 ad * module_do_builtin:
256 1.1 ad *
257 1.1 ad * Initialize a single module from the list of modules that are
258 1.1 ad * built into the kernel (linked into the kernel image).
259 1.1 ad */
260 1.1 ad static int
261 1.1 ad module_do_builtin(const char *name, module_t **modp)
262 1.1 ad {
263 1.1 ad __link_set_decl(modules, modinfo_t);
264 1.1 ad modinfo_t *const *mip;
265 1.1 ad const char *p, *s;
266 1.1 ad char buf[MAXMODNAME];
267 1.1 ad modinfo_t *mi;
268 1.1 ad module_t *mod, *mod2;
269 1.1 ad size_t len;
270 1.1 ad int error, i;
271 1.1 ad
272 1.1 ad KASSERT(mutex_owned(&module_lock));
273 1.1 ad
274 1.1 ad /*
275 1.1 ad * Check to see if already loaded.
276 1.1 ad */
277 1.1 ad if ((mod = module_lookup(name)) != NULL) {
278 1.1 ad if (modp != NULL) {
279 1.1 ad *modp = mod;
280 1.1 ad }
281 1.1 ad return 0;
282 1.1 ad }
283 1.1 ad
284 1.1 ad /*
285 1.1 ad * Search the list to see if we have a module by this name.
286 1.1 ad */
287 1.1 ad error = ENOENT;
288 1.1 ad __link_set_foreach(mip, modules) {
289 1.1 ad mi = *mip;
290 1.1 ad if (mi == &module_dummy) {
291 1.1 ad continue;
292 1.1 ad }
293 1.1 ad if (strcmp(mi->mi_name, name) == 0) {
294 1.1 ad error = 0;
295 1.1 ad break;
296 1.1 ad }
297 1.1 ad }
298 1.1 ad if (error != 0) {
299 1.1 ad return error;
300 1.1 ad }
301 1.1 ad
302 1.1 ad /*
303 1.1 ad * Initialize pre-requisites.
304 1.1 ad */
305 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
306 1.1 ad if (mod == NULL) {
307 1.1 ad return ENOMEM;
308 1.1 ad }
309 1.1 ad if (modp != NULL) {
310 1.1 ad *modp = mod;
311 1.1 ad }
312 1.1 ad if (mi->mi_required != NULL) {
313 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
314 1.1 ad if (*s == ',')
315 1.1 ad s++;
316 1.1 ad p = s;
317 1.1 ad while (*p != '\0' && *p != ',')
318 1.1 ad p++;
319 1.1 ad len = min(p - s + 1, sizeof(buf));
320 1.1 ad strlcpy(buf, s, len);
321 1.1 ad if (buf[0] == '\0')
322 1.1 ad break;
323 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
324 1.1 ad module_error("too many required modules");
325 1.1 ad kmem_free(mod, sizeof(*mod));
326 1.1 ad return EINVAL;
327 1.1 ad }
328 1.1 ad error = module_do_builtin(buf, &mod2);
329 1.1 ad if (error != 0) {
330 1.1 ad kmem_free(mod, sizeof(*mod));
331 1.1 ad return error;
332 1.1 ad }
333 1.1 ad mod->mod_required[mod->mod_nrequired++] = mod2;
334 1.1 ad }
335 1.1 ad }
336 1.1 ad
337 1.1 ad /*
338 1.1 ad * Try to initialize the module.
339 1.1 ad */
340 1.1 ad error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
341 1.1 ad if (error != 0) {
342 1.1 ad module_error("builtin module `%s' "
343 1.1 ad "failed to init", mi->mi_name);
344 1.1 ad kmem_free(mod, sizeof(*mod));
345 1.1 ad return error;
346 1.1 ad }
347 1.1 ad mod->mod_info = mi;
348 1.1 ad mod->mod_source = MODULE_SOURCE_KERNEL;
349 1.1 ad module_count++;
350 1.1 ad TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
351 1.1 ad
352 1.1 ad /*
353 1.1 ad * If that worked, count dependencies.
354 1.1 ad */
355 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
356 1.1 ad mod->mod_required[i]->mod_refcnt++;
357 1.1 ad }
358 1.1 ad
359 1.1 ad return 0;
360 1.1 ad }
361 1.1 ad
362 1.1 ad /*
363 1.1 ad * module_do_load:
364 1.1 ad *
365 1.1 ad * Helper routine: load a module from the file system, or one
366 1.1 ad * pushed by the boot loader.
367 1.1 ad */
368 1.1 ad static int
369 1.1 ad module_do_load(const char *filename, bool isdep, bool force, module_t **modp)
370 1.1 ad {
371 1.1 ad static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending);
372 1.1 ad static int depth;
373 1.1 ad const int maxdepth = 6;
374 1.1 ad modinfo_t *mi;
375 1.1 ad module_t *mod, *mod2;
376 1.1 ad char buf[MAXMODNAME];
377 1.1 ad const char *s, *p;
378 1.1 ad void *addr;
379 1.1 ad size_t size;
380 1.1 ad int error;
381 1.1 ad size_t len;
382 1.1 ad u_int i;
383 1.1 ad
384 1.1 ad KASSERT(mutex_owned(&module_lock));
385 1.1 ad
386 1.1 ad error = 0;
387 1.1 ad
388 1.1 ad /*
389 1.1 ad * Avoid recursing too far.
390 1.1 ad */
391 1.1 ad if (++depth > maxdepth) {
392 1.1 ad module_error("too many required modules");
393 1.1 ad depth--;
394 1.1 ad return EMLINK;
395 1.1 ad }
396 1.1 ad
397 1.1 ad /*
398 1.1 ad * Load the module and link. Before going to the file system,
399 1.1 ad * scan the list of modules loaded by the boot loader. Just
400 1.1 ad * before init is started the list of modules loaded at boot
401 1.1 ad * will be purged. Before init is started we can assume that
402 1.1 ad * `filename' is a module name and not a path name.
403 1.1 ad */
404 1.1 ad TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
405 1.1 ad if (strcmp(mod->mod_info->mi_name, filename) == 0) {
406 1.1 ad TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
407 1.1 ad break;
408 1.1 ad }
409 1.1 ad }
410 1.1 ad if (mod == NULL) {
411 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
412 1.1 ad if (mod == NULL) {
413 1.1 ad depth--;
414 1.1 ad return ENOMEM;
415 1.1 ad }
416 1.1 ad error = kobj_open_file(&mod->mod_kobj, filename);
417 1.1 ad if (error != 0) {
418 1.1 ad kmem_free(mod, sizeof(*mod));
419 1.1 ad depth--;
420 1.1 ad module_error("unable to open object file");
421 1.1 ad return error;
422 1.1 ad }
423 1.1 ad error = kobj_load(mod->mod_kobj);
424 1.1 ad mod->mod_source = MODULE_SOURCE_FILESYS;
425 1.1 ad }
426 1.1 ad TAILQ_INSERT_TAIL(&pending, mod, mod_chain);
427 1.1 ad if (error != 0) {
428 1.1 ad goto fail;
429 1.1 ad }
430 1.1 ad
431 1.1 ad /*
432 1.1 ad * Find module info record and check compatibility.
433 1.1 ad */
434 1.1 ad error = kobj_find_section(mod->mod_kobj, "link_set_modules",
435 1.1 ad &addr, &size);
436 1.1 ad if (error != 0) {
437 1.1 ad module_error("`link_set_modules' section not present");
438 1.1 ad goto fail;
439 1.1 ad }
440 1.1 ad if (size != sizeof(modinfo_t **)) {
441 1.1 ad module_error("`link_set_modules' section wrong size");
442 1.1 ad goto fail;
443 1.1 ad }
444 1.1 ad mod->mod_info = *(modinfo_t **)addr;
445 1.1 ad mi = mod->mod_info;
446 1.1 ad
447 1.1 ad /*
448 1.1 ad * If loading a dependency, `filename' is a plain module name.
449 1.1 ad * The name must match.
450 1.1 ad */
451 1.1 ad if (isdep && strcmp(mi->mi_name, filename) != 0) {
452 1.1 ad error = ENOENT;
453 1.1 ad goto fail;
454 1.1 ad }
455 1.1 ad
456 1.1 ad /*
457 1.1 ad * Check to see if the module is already loaded, and block
458 1.1 ad * circular dependencies.
459 1.1 ad */
460 1.1 ad if (module_lookup(mi->mi_name) != NULL) {
461 1.1 ad error = EEXIST;
462 1.1 ad goto fail;
463 1.1 ad }
464 1.1 ad TAILQ_FOREACH(mod2, &pending, mod_chain) {
465 1.1 ad if (mod == mod2) {
466 1.1 ad continue;
467 1.1 ad }
468 1.1 ad if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
469 1.1 ad error = EDEADLK;
470 1.1 ad module_error("circular dependency detected");
471 1.1 ad goto fail;
472 1.1 ad }
473 1.1 ad }
474 1.1 ad
475 1.1 ad /*
476 1.1 ad * Pass proper name to kobj. This will register the module
477 1.1 ad * with the ksyms framework.
478 1.1 ad */
479 1.1 ad error = kobj_set_name(mod->mod_kobj, mi->mi_name);
480 1.1 ad if (error != 0) {
481 1.1 ad module_error("unable to set name");
482 1.1 ad goto fail;
483 1.1 ad }
484 1.1 ad
485 1.1 ad /*
486 1.1 ad * Now try to load any requisite modules.
487 1.1 ad */
488 1.1 ad if (mi->mi_required != NULL) {
489 1.1 ad for (s = mi->mi_required; *s != '\0'; s = p) {
490 1.1 ad if (*s == ',')
491 1.1 ad s++;
492 1.1 ad p = s;
493 1.1 ad while (*p != '\0' && *p != ',')
494 1.1 ad p++;
495 1.1 ad len = min(p - s + 1, MAXMODNAME);
496 1.1 ad strlcpy(buf, s, len);
497 1.1 ad if (buf[0] == '\0')
498 1.1 ad break;
499 1.1 ad if (mod->mod_nrequired == MAXMODDEPS - 1) {
500 1.1 ad error = EINVAL;
501 1.1 ad module_error("too many required modules");
502 1.1 ad goto fail;
503 1.1 ad }
504 1.1 ad error = module_do_load(buf, true, force,
505 1.1 ad &mod->mod_required[mod->mod_nrequired++]);
506 1.1 ad if (error != 0 && error != EEXIST)
507 1.1 ad goto fail;
508 1.1 ad }
509 1.1 ad }
510 1.1 ad
511 1.1 ad /*
512 1.1 ad * We loaded all needed modules successfully: initialize.
513 1.1 ad */
514 1.1 ad error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL);
515 1.1 ad if (error != 0) {
516 1.1 ad module_error("modctl function returned error %d", error);
517 1.1 ad goto fail;
518 1.1 ad }
519 1.1 ad
520 1.1 ad /*
521 1.1 ad * Good, the module loaded successfully. Put it onto the
522 1.1 ad * list and add references to its requisite modules.
523 1.1 ad */
524 1.1 ad module_count++;
525 1.1 ad kobj_close(mod->mod_kobj);
526 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
527 1.1 ad TAILQ_INSERT_TAIL(&module_list, mod, mod_chain);
528 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
529 1.1 ad mod->mod_required[i]->mod_refcnt++;
530 1.1 ad }
531 1.1 ad if (modp != NULL) {
532 1.1 ad *modp = mod;
533 1.1 ad }
534 1.1 ad depth--;
535 1.1 ad return 0;
536 1.1 ad fail:
537 1.1 ad kobj_close(mod->mod_kobj);
538 1.1 ad TAILQ_REMOVE(&pending, mod, mod_chain);
539 1.1 ad kobj_unload(mod->mod_kobj);
540 1.1 ad kmem_free(mod, sizeof(*mod));
541 1.1 ad depth--;
542 1.1 ad return error;
543 1.1 ad }
544 1.1 ad
545 1.1 ad /*
546 1.1 ad * module_do_unload:
547 1.1 ad *
548 1.1 ad * Helper routine: do the dirty work of unloading a module.
549 1.1 ad */
550 1.1 ad static int
551 1.1 ad module_do_unload(const char *name)
552 1.1 ad {
553 1.1 ad module_t *mod;
554 1.1 ad int error;
555 1.1 ad u_int i;
556 1.1 ad
557 1.1 ad KASSERT(mutex_owned(&module_lock));
558 1.1 ad
559 1.1 ad mod = module_lookup(name);
560 1.1 ad if (mod == NULL) {
561 1.1 ad return ENOENT;
562 1.1 ad }
563 1.1 ad if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) {
564 1.1 ad return EBUSY;
565 1.1 ad }
566 1.1 ad error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
567 1.1 ad if (error != 0) {
568 1.1 ad return error;
569 1.1 ad }
570 1.1 ad module_count--;
571 1.1 ad TAILQ_REMOVE(&module_list, mod, mod_chain);
572 1.1 ad for (i = 0; i < mod->mod_nrequired; i++) {
573 1.1 ad mod->mod_required[i]->mod_refcnt--;
574 1.1 ad }
575 1.1 ad if (mod->mod_kobj != NULL) {
576 1.1 ad kobj_unload(mod->mod_kobj);
577 1.1 ad }
578 1.1 ad kmem_free(mod, sizeof(*mod));
579 1.1 ad
580 1.1 ad return 0;
581 1.1 ad }
582 1.1 ad
583 1.1 ad /*
584 1.1 ad * module_prime:
585 1.1 ad *
586 1.1 ad * Push a module loaded by the bootloader onto our internal
587 1.1 ad * list.
588 1.1 ad */
589 1.1 ad int
590 1.1 ad module_prime(void *base, size_t size)
591 1.1 ad {
592 1.1 ad module_t *mod;
593 1.1 ad int error;
594 1.1 ad
595 1.1 ad mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
596 1.1 ad if (mod == NULL) {
597 1.1 ad return ENOMEM;
598 1.1 ad }
599 1.1 ad mod->mod_source = MODULE_SOURCE_BOOT;
600 1.1 ad
601 1.1 ad error = kobj_open_mem(&mod->mod_kobj, base, size);
602 1.1 ad if (error == 0) {
603 1.1 ad kmem_free(mod, sizeof(*mod));
604 1.1 ad module_error("unable to open object pushed by boot loader");
605 1.1 ad return error;
606 1.1 ad }
607 1.1 ad error = kobj_load(mod->mod_kobj);
608 1.1 ad if (error != 0) {
609 1.1 ad kmem_free(mod, sizeof(*mod));
610 1.1 ad module_error("unable to push object pushed by boot loader");
611 1.1 ad return error;
612 1.1 ad }
613 1.1 ad mod->mod_source = MODULE_SOURCE_FILESYS;
614 1.1 ad TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
615 1.1 ad
616 1.1 ad return 0;
617 1.1 ad }
618