1 /*        $NetBSD: kern_module.c,v 1.173 2025/05/05 00:31:48 pgoyette Exp $     */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software developed for The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Kernel module support.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.173 2025/05/05 00:31:48 pgoyette Exp $");
38 
39 #define _MODULE_INTERNAL
40 
41 #ifdef _KERNEL_OPT
42 #include "opt_ddb.h"
43 #include "opt_modular.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/lwp.h>
51 #include <sys/kauth.h>
52 #include <sys/kobj.h>
53 #include <sys/kmem.h>
54 #include <sys/module.h>
55 #include <sys/module_hook.h>
56 #include <sys/kthread.h>
57 #include <sys/sysctl.h>
58 #include <sys/lock.h>
59 #include <sys/evcnt.h>
60 
61 #include <uvm/uvm_extern.h>
62 
63 struct vm_map *module_map;
64 const char *module_machine;
65 char      module_base[MODULE_BASE_SIZE];
66 
67 struct modlist        module_list = TAILQ_HEAD_INITIALIZER(module_list);
68 struct modlist        module_builtins = TAILQ_HEAD_INITIALIZER(module_builtins);
69 static struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist);
70 
71 struct module_callbacks {
72           TAILQ_ENTRY(module_callbacks) modcb_list;
73           void (*modcb_load)(struct module *);
74           void (*modcb_unload)(struct module *);
75 };
76 TAILQ_HEAD(modcblist, module_callbacks);
77 static struct modcblist modcblist;
78 
79 static module_t *module_netbsd;
80 static const modinfo_t module_netbsd_modinfo = {
81           .mi_version = __NetBSD_Version__,
82           .mi_class = MODULE_CLASS_MISC,
83           .mi_name = "netbsd"
84 };
85 
86 static module_t     *module_active;
87 
88 __read_mostly
89 #ifdef MODULAR_DEFAULT_VERBOSE
90 bool                module_verbose_on = true;
91 #else
92 bool                module_verbose_on = false;
93 #endif
94 
95 __read_mostly
96 #ifdef MODULAR_DEFAULT_AUTOLOAD
97 bool                module_autoload_on = true;
98 #else
99 bool                module_autoload_on = false;
100 #endif
101 
102 __read_mostly
103 #ifdef MODULAR_DEFAULT_AUTOUNLOAD_UNSAFE
104 bool                module_autounload_unsafe = true;
105 #else
106 bool                module_autounload_unsafe = false;
107 #endif
108 u_int               module_count;
109 u_int               module_builtinlist;
110 u_int               module_autotime = 10;
111 u_int               module_gen = 1;
112 static kcondvar_t module_thread_cv;
113 static kmutex_t module_thread_lock;
114 static int          module_thread_ticks;
115 int (*module_load_vfs_vec)(const char *, int, bool, module_t *,
116                                  prop_dictionary_t *) = (void *)eopnotsupp;
117 
118 static kauth_listener_t       module_listener;
119 
120 static specificdata_domain_t module_specificdata_domain;
121 
122 /* Ensure that the kernel's link set isn't empty. */
123 static modinfo_t module_dummy;
124 __link_set_add_rodata(modules, module_dummy);
125 
126 static module_t     *module_newmodule(modsrc_t);
127 static void         module_free(module_t *);
128 static void         module_require_force(module_t *);
129 static int          module_do_load(const char *, bool, int, prop_dictionary_t,
130                         module_t **, modclass_t modclass, bool);
131 static int          module_do_unload(const char *, bool);
132 static int          module_do_builtin(const module_t *, const char *, module_t **,
133     prop_dictionary_t);
134 static int          module_fetch_info(module_t *);
135 static void         module_thread(void *);
136 
137 static module_t     *module_lookup(const char *);
138 static void         module_enqueue(module_t *);
139 
140 static bool         module_merge_dicts(prop_dictionary_t, const prop_dictionary_t);
141 
142 static void         sysctl_module_setup(void);
143 static int          sysctl_module_autotime(SYSCTLFN_PROTO);
144 
145 static void         module_callback_load(struct module *);
146 static void         module_callback_unload(struct module *);
147 
148 #define MODULE_CLASS_MATCH(mi, modclass) \
149           ((modclass) == MODULE_CLASS_ANY || (modclass) == (mi)->mi_class)
150 
151 static void
module_incompat(const modinfo_t * mi,int modclass)152 module_incompat(const modinfo_t *mi, int modclass)
153 {
154           module_error("Incompatible module class %d for `%s' (wanted %d)",
155               mi->mi_class, mi->mi_name, modclass);
156 }
157 
158 struct module *
module_kernel(void)159 module_kernel(void)
160 {
161 
162           return module_netbsd;
163 }
164 
165 /*
166  * module_error:
167  *
168  *        Utility function: log an error.
169  */
170 void
module_error(const char * fmt,...)171 module_error(const char *fmt, ...)
172 {
173           va_list ap;
174 
175           va_start(ap, fmt);
176           printf("WARNING: module error: ");
177           vprintf(fmt, ap);
178           printf("\n");
179           va_end(ap);
180 }
181 
182 /*
183  * module_print:
184  *
185  *        Utility function: log verbose output.
186  */
187 void
module_print(const char * fmt,...)188 module_print(const char *fmt, ...)
189 {
190           va_list ap;
191 
192           if (module_verbose_on) {
193                     va_start(ap, fmt);
194                     printf("DEBUG: module: ");
195                     vprintf(fmt, ap);
196                     printf("\n");
197                     va_end(ap);
198           }
199 }
200 
201 /*
202  * module_name:
203  *
204  *        Utility function: return the module's name.
205  */
206 const char *
module_name(struct module * mod)207 module_name(struct module *mod)
208 {
209 
210           return mod->mod_info->mi_name;
211 }
212 
213 /*
214  * module_source:
215  *
216  *        Utility function: return the module's source.
217  */
218 modsrc_t
module_source(struct module * mod)219 module_source(struct module *mod)
220 {
221 
222           return mod->mod_source;
223 }
224 
225 static int
module_listener_cb(kauth_cred_t cred,kauth_action_t action,void * cookie,void * arg0,void * arg1,void * arg2,void * arg3)226 module_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
227     void *arg0, void *arg1, void *arg2, void *arg3)
228 {
229           int result;
230 
231           result = KAUTH_RESULT_DEFER;
232 
233           if (action != KAUTH_SYSTEM_MODULE)
234                     return result;
235 
236           if ((uintptr_t)arg2 != 0)     /* autoload */
237                     result = KAUTH_RESULT_ALLOW;
238 
239           return result;
240 }
241 
242 /*
243  * Allocate a new module_t
244  */
245 static module_t *
module_newmodule(modsrc_t source)246 module_newmodule(modsrc_t source)
247 {
248           module_t *mod;
249 
250           mod = kmem_zalloc(sizeof(*mod), KM_SLEEP);
251           mod->mod_source = source;
252           specificdata_init(module_specificdata_domain, &mod->mod_sdref);
253           return mod;
254 }
255 
256 /*
257  * Free a module_t
258  */
259 static void
module_free(module_t * mod)260 module_free(module_t *mod)
261 {
262 
263           specificdata_fini(module_specificdata_domain, &mod->mod_sdref);
264           if (mod->mod_required)
265                     kmem_free(mod->mod_required, mod->mod_arequired *
266                         sizeof(module_t *));
267           kmem_free(mod, sizeof(*mod));
268 }
269 
270 /*
271  * Require the -f (force) flag to load a module
272  */
273 static void
module_require_force(struct module * mod)274 module_require_force(struct module *mod)
275 {
276           SET(mod->mod_flags, MODFLG_MUST_FORCE);
277 }
278 
279 /*
280  * Add modules to the builtin list.  This can done at boottime or
281  * at runtime if the module is linked into the kernel with an
282  * external linker.  All or none of the input will be handled.
283  * Optionally, the modules can be initialized.  If they are not
284  * initialized, module_init_class() or module_load() can be used
285  * later, but these are not guaranteed to give atomic results.
286  */
287 int
module_builtin_add(modinfo_t * const * mip,size_t nmodinfo,bool init)288 module_builtin_add(modinfo_t *const *mip, size_t nmodinfo, bool init)
289 {
290           struct module **modp = NULL, *mod_iter;
291           int rv = 0, i, mipskip;
292 
293           if (init) {
294                     rv = kauth_authorize_system(kauth_cred_get(),
295                         KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_LOAD,
296                         (void *)(uintptr_t)1, NULL);
297                     if (rv) {
298                               return rv;
299                     }
300           }
301 
302           for (i = 0, mipskip = 0; i < nmodinfo; i++) {
303                     if (mip[i] == &module_dummy) {
304                               KASSERT(nmodinfo > 0);
305                               nmodinfo--;
306                     }
307           }
308           if (nmodinfo == 0)
309                     return 0;
310 
311           modp = kmem_zalloc(sizeof(*modp) * nmodinfo, KM_SLEEP);
312           for (i = 0, mipskip = 0; i < nmodinfo; i++) {
313                     if (mip[i+mipskip] == &module_dummy) {
314                               mipskip++;
315                               continue;
316                     }
317                     modp[i] = module_newmodule(MODULE_SOURCE_KERNEL);
318                     modp[i]->mod_info = mip[i+mipskip];
319           }
320           kernconfig_lock();
321 
322           /* do this in three stages for error recovery and atomicity */
323 
324           /* first check for presence */
325           for (i = 0; i < nmodinfo; i++) {
326                     TAILQ_FOREACH(mod_iter, &module_builtins, mod_chain) {
327                               if (strcmp(mod_iter->mod_info->mi_name,
328                                   modp[i]->mod_info->mi_name) == 0)
329                                         break;
330                     }
331                     if (mod_iter) {
332                               rv = EEXIST;
333                               goto out;
334                     }
335 
336                     if (module_lookup(modp[i]->mod_info->mi_name) != NULL) {
337                               rv = EEXIST;
338                               goto out;
339                     }
340           }
341 
342           /* then add to list */
343           for (i = 0; i < nmodinfo; i++) {
344                     TAILQ_INSERT_TAIL(&module_builtins, modp[i], mod_chain);
345                     module_builtinlist++;
346           }
347 
348           /* finally, init (if required) */
349           if (init) {
350                     for (i = 0; i < nmodinfo; i++) {
351                               rv = module_do_builtin(modp[i],
352                                   modp[i]->mod_info->mi_name, NULL, NULL);
353                               /* throw in the towel, recovery hard & not worth it */
354                               if (rv)
355                                         panic("%s: builtin module \"%s\" init failed:"
356                                             " %d", __func__,
357                                             modp[i]->mod_info->mi_name, rv);
358                     }
359           }
360 
361  out:
362           kernconfig_unlock();
363           if (rv != 0) {
364                     for (i = 0; i < nmodinfo; i++) {
365                               if (modp[i])
366                                         module_free(modp[i]);
367                     }
368           }
369           kmem_free(modp, sizeof(*modp) * nmodinfo);
370           return rv;
371 }
372 
373 /*
374  * Optionally fini and remove builtin module from the kernel.
375  * Note: the module will now be unreachable except via mi && builtin_add.
376  */
377 int
module_builtin_remove(modinfo_t * mi,bool fini)378 module_builtin_remove(modinfo_t *mi, bool fini)
379 {
380           struct module *mod;
381           int rv = 0;
382 
383           if (fini) {
384                     rv = kauth_authorize_system(kauth_cred_get(),
385                         KAUTH_SYSTEM_MODULE, 0, (void *)(uintptr_t)MODCTL_UNLOAD,
386                         NULL, NULL);
387                     if (rv)
388                               return rv;
389 
390                     kernconfig_lock();
391                     rv = module_do_unload(mi->mi_name, true);
392                     if (rv) {
393                               goto out;
394                     }
395           } else {
396                     kernconfig_lock();
397           }
398           TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
399                     if (strcmp(mod->mod_info->mi_name, mi->mi_name) == 0)
400                               break;
401           }
402           if (mod) {
403                     TAILQ_REMOVE(&module_builtins, mod, mod_chain);
404                     module_builtinlist--;
405           } else {
406                     KASSERT(fini == false);
407                     rv = ENOENT;
408           }
409 
410  out:
411           kernconfig_unlock();
412           return rv;
413 }
414 
415 #if __NetBSD_Version__ / 1000000 % 100 == 99      /* -current */
416 #define LEGACY_MODULE_PATH                                            \
417           snprintf(module_base, sizeof(module_base),                  \
418               "/stand/%s/%s/modules", module_machine, osrelease);
419 #else                                                       /* release */
420 #define LEGACY_MODULE_PATH                                            \
421           snprintf(module_base, sizeof(module_base),                  \
422               "/stand/%s/%d.%d/modules", module_machine,              \
423               __NetBSD_Version__ / 100000000,                         \
424               __NetBSD_Version__ / 1000000 % 100);
425 #endif    /* if __NetBSD_Version__ */
426 
427 /*
428  * module_init:
429  *
430  *        Initialize the module subsystem.
431  */
432 void
module_init(void)433 module_init(void)
434 {
435           __link_set_decl(modules, modinfo_t);
436           modinfo_t *const *mip;
437           int rv;
438 
439           if (module_map == NULL) {
440                     module_map = kernel_map;
441           }
442           cv_init(&module_thread_cv, "mod_unld");
443           mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE);
444           TAILQ_INIT(&modcblist);
445 
446 #ifdef MODULAR      /* XXX */
447           module_init_md();
448 #endif
449 
450 #ifdef KERNEL_DIR
451           const char *booted_kernel = get_booted_kernel();
452           if (booted_kernel) {
453                     while (*booted_kernel == '/') /* ignore leading slashes */
454                               booted_kernel++;    /* boot lookup always at root */
455                     char *ptr = strrchr(booted_kernel, '/');
456                     if (ptr == NULL) {
457                               /* no dir name, use legacy module path */
458                               if (!module_machine)
459                                         module_machine = machine;
460                               LEGACY_MODULE_PATH;
461                     } else {
462                               snprintf(module_base, sizeof(module_base),
463                                    "/%.*s/modules",
464                                   (int)(ptr - booted_kernel), booted_kernel);
465                     }
466           } else {
467                     strlcpy(module_base, "/netbsd/modules", sizeof(module_base));
468                     printf("Cannot find kernel name, loading modules from \"%s\"\n",
469                         module_base);
470           }
471 #else     /* ifdef KERNEL_DIR */
472           if (!module_machine)
473                     module_machine = machine;
474           LEGACY_MODULE_PATH;
475 #endif
476 
477           module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
478               module_listener_cb, NULL);
479 
480           __link_set_foreach(mip, modules) {
481                     if ((rv = module_builtin_add(mip, 1, false)) != 0)
482                               module_error("Built-in `%s' failed: %d\n",
483                                   (*mip)->mi_name, rv);
484           }
485 
486           sysctl_module_setup();
487           module_specificdata_domain = specificdata_domain_create();
488 
489           module_netbsd = module_newmodule(MODULE_SOURCE_KERNEL);
490           module_netbsd->mod_refcnt = 1;
491           module_netbsd->mod_info = &module_netbsd_modinfo;
492 }
493 
494 /*
495  * module_start_unload_thread:
496  *
497  *        Start the auto unload kthread.
498  */
499 void
module_start_unload_thread(void)500 module_start_unload_thread(void)
501 {
502           int error;
503 
504           error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread,
505               NULL, NULL, "modunload");
506           if (error != 0)
507                     panic("%s: %d", __func__, error);
508 }
509 
510 /*
511  * module_builtin_require_force
512  *
513  * Require MODCTL_MUST_FORCE to load any built-in modules that have
514  * not yet been initialized
515  */
516 void
module_builtin_require_force(void)517 module_builtin_require_force(void)
518 {
519           module_t *mod;
520 
521           kernconfig_lock();
522           TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
523                     module_require_force(mod);
524           }
525           kernconfig_unlock();
526 }
527 
528 static struct sysctllog *module_sysctllog;
529 
530 static int
sysctl_module_autotime(SYSCTLFN_ARGS)531 sysctl_module_autotime(SYSCTLFN_ARGS)
532 {
533           struct sysctlnode node;
534           int t, error;
535 
536           t = *(int *)rnode->sysctl_data;
537 
538           node = *rnode;
539           node.sysctl_data = &t;
540           error = sysctl_lookup(SYSCTLFN_CALL(&node));
541           if (error || newp == NULL)
542                     return (error);
543 
544           if (t < 0)
545                     return (EINVAL);
546 
547           *(int *)rnode->sysctl_data = t;
548           return (0);
549 }
550 
551 static void
sysctl_module_setup(void)552 sysctl_module_setup(void)
553 {
554           const struct sysctlnode *node = NULL;
555 
556           sysctl_createv(&module_sysctllog, 0, NULL, &node,
557                     CTLFLAG_PERMANENT,
558                     CTLTYPE_NODE, "module",
559                     SYSCTL_DESCR("Module options"),
560                     NULL, 0, NULL, 0,
561                     CTL_KERN, CTL_CREATE, CTL_EOL);
562 
563           if (node == NULL)
564                     return;
565 
566           sysctl_createv(&module_sysctllog, 0, &node, NULL,
567                     CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
568                     CTLTYPE_BOOL, "autoload",
569                     SYSCTL_DESCR("Enable automatic load of modules"),
570                     NULL, 0, &module_autoload_on, 0,
571                     CTL_CREATE, CTL_EOL);
572           sysctl_createv(&module_sysctllog, 0, &node, NULL,
573                     CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
574                     CTLTYPE_BOOL, "autounload_unsafe",
575                     SYSCTL_DESCR("Enable automatic unload of unaudited modules"),
576                     NULL, 0, &module_autounload_unsafe, 0,
577                     CTL_CREATE, CTL_EOL);
578           sysctl_createv(&module_sysctllog, 0, &node, NULL,
579                     CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
580                     CTLTYPE_BOOL, "verbose",
581                     SYSCTL_DESCR("Enable verbose output"),
582                     NULL, 0, &module_verbose_on, 0,
583                     CTL_CREATE, CTL_EOL);
584           sysctl_createv(&module_sysctllog, 0, &node, NULL,
585                     CTLFLAG_PERMANENT | CTLFLAG_READONLY,
586                     CTLTYPE_STRING, "path",
587                     SYSCTL_DESCR("Default module load path"),
588                     NULL, 0, module_base, 0,
589                     CTL_CREATE, CTL_EOL);
590           sysctl_createv(&module_sysctllog, 0, &node, NULL,
591                     CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
592                     CTLTYPE_INT, "autotime",
593                     SYSCTL_DESCR("Auto-unload delay"),
594                     sysctl_module_autotime, 0, &module_autotime, 0,
595                     CTL_CREATE, CTL_EOL);
596 }
597 
598 /*
599  * module_init_class:
600  *
601  *        Initialize all built-in and pre-loaded modules of the
602  *        specified class.
603  */
604 void
module_init_class(modclass_t modclass)605 module_init_class(modclass_t modclass)
606 {
607           TAILQ_HEAD(, module) bi_fail = TAILQ_HEAD_INITIALIZER(bi_fail);
608           module_t *mod;
609           modinfo_t *mi;
610 
611           kernconfig_lock();
612           /*
613            * Builtins first.  These will not depend on pre-loaded modules
614            * (because the kernel would not link).
615            */
616           do {
617                     TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
618                               mi = mod->mod_info;
619                               if (!MODULE_CLASS_MATCH(mi, modclass))
620                                         continue;
621                               /*
622                                * If initializing a builtin module fails, don't try
623                                * to load it again.  But keep it around and queue it
624                                * on the builtins list after we're done with module
625                                * init.  Don't set it to MODFLG_MUST_FORCE in case a
626                                * future attempt to initialize can be successful.
627                                * (If the module has previously been set to
628                                * MODFLG_MUST_FORCE, don't try to override that!)
629                                */
630                               if (ISSET(mod->mod_flags, MODFLG_MUST_FORCE) ||
631                                   module_do_builtin(mod, mi->mi_name, NULL,
632                                   NULL) != 0) {
633                                         TAILQ_REMOVE(&module_builtins, mod, mod_chain);
634                                         TAILQ_INSERT_TAIL(&bi_fail, mod, mod_chain);
635                               }
636                               break;
637                     }
638           } while (mod != NULL);
639 
640           /*
641            * Now preloaded modules.  These will be pulled off the
642            * list as we call module_do_load();
643            */
644           do {
645                     TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
646                               mi = mod->mod_info;
647                               if (!MODULE_CLASS_MATCH(mi, modclass))
648                                         continue;
649                               module_do_load(mi->mi_name, false, 0, NULL, NULL,
650                                   modclass, false);
651                               break;
652                     }
653           } while (mod != NULL);
654 
655           /* return failed builtin modules to builtin list */
656           while ((mod = TAILQ_FIRST(&bi_fail)) != NULL) {
657                     TAILQ_REMOVE(&bi_fail, mod, mod_chain);
658                     TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
659           }
660 
661           kernconfig_unlock();
662 }
663 
664 /*
665  * module_compatible:
666  *
667  *        Return true if the two supplied kernel versions are said to
668  *        have the same binary interface for kernel code.  The entire
669  *        version is significant for the development tree (-current),
670  *        major and minor versions are significant for official
671  *        releases of the system.
672  */
673 bool
module_compatible(int v1,int v2)674 module_compatible(int v1, int v2)
675 {
676 
677 #if __NetBSD_Version__ / 1000000 % 100 == 99      /* -current */
678           return v1 == v2;
679 #else                                                       /* release */
680           return abs(v1 - v2) < 10000;
681 #endif
682 }
683 
684 /*
685  * module_load:
686  *
687  *        Load a single module from the file system.
688  */
689 int
module_load(const char * filename,int flags,prop_dictionary_t props,modclass_t modclass)690 module_load(const char *filename, int flags, prop_dictionary_t props,
691               modclass_t modclass)
692 {
693           module_t *mod;
694           int error;
695 
696           /* Test if we already have the module loaded before
697            * authorizing so we have the opportunity to return EEXIST. */
698           kernconfig_lock();
699           mod = module_lookup(filename);
700           if (mod != NULL) {
701                     module_print("%s module `%s' already loaded",
702                         "Requested", filename);
703                     error = EEXIST;
704                     goto out;
705           }
706 
707           /* Authorize. */
708           error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
709               0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL);
710           if (error != 0)
711                     goto out;
712 
713           error = module_do_load(filename, false, flags, props, NULL, modclass,
714               false);
715 
716 out:
717           kernconfig_unlock();
718           return error;
719 }
720 
721 /*
722  * module_autoload:
723  *
724  *        Load a single module from the file system, system initiated.
725  */
726 int
module_autoload(const char * filename,modclass_t modclass)727 module_autoload(const char *filename, modclass_t modclass)
728 {
729           int error;
730           struct proc *p = curlwp->l_proc;
731 
732           kernconfig_lock();
733 
734           module_print("Autoload for `%s' requested by pid %d (%s)",
735               filename, p->p_pid, p->p_comm);
736 
737           /* Nothing if the user has disabled it. */
738           if (!module_autoload_on) {
739                     module_print("Autoload disabled for `%s' ", filename);
740                     kernconfig_unlock();
741                     return EPERM;
742           }
743 
744         /* Disallow path separators and magic symlinks. */
745         if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL ||
746             strchr(filename, '.') != NULL) {
747                     module_print("Autoload illegal path for `%s' ", filename);
748                     kernconfig_unlock();
749           return EPERM;
750           }
751 
752           /* Authorize. */
753           error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
754               0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL);
755 
756           if (error != 0) {
757                     module_print("Autoload  not authorized for `%s' ", filename);
758                     kernconfig_unlock();
759                     return error;
760           }
761           error = module_do_load(filename, false, 0, NULL, NULL, modclass, true);
762 
763           module_print("Autoload for `%s' status %d", filename, error);
764           kernconfig_unlock();
765           return error;
766 }
767 
768 /*
769  * module_unload:
770  *
771  *        Find and unload a module by name.
772  */
773 int
module_unload(const char * name)774 module_unload(const char *name)
775 {
776           int error;
777 
778           /* Authorize. */
779           error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
780               0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL);
781           if (error != 0) {
782                     return error;
783           }
784 
785           kernconfig_lock();
786           error = module_do_unload(name, true);
787           kernconfig_unlock();
788 
789           return error;
790 }
791 
792 /*
793  * module_lookup:
794  *
795  *        Look up a module by name.
796  */
797 module_t *
module_lookup(const char * name)798 module_lookup(const char *name)
799 {
800           module_t *mod;
801 
802           KASSERT(kernconfig_is_held());
803 
804           TAILQ_FOREACH(mod, &module_list, mod_chain) {
805                     if (strcmp(mod->mod_info->mi_name, name) == 0)
806                               break;
807           }
808 
809           return mod;
810 }
811 
812 /*
813  * module_hold:
814  *
815  *        Add a single reference to a module.  It's the caller's
816  *        responsibility to ensure that the reference is dropped
817  *        later.
818  */
819 void
module_hold(module_t * mod)820 module_hold(module_t *mod)
821 {
822 
823           kernconfig_lock();
824           mod->mod_refcnt++;
825           kernconfig_unlock();
826 }
827 
828 /*
829  * module_rele:
830  *
831  *        Release a reference acquired with module_hold().
832  */
833 void
module_rele(module_t * mod)834 module_rele(module_t *mod)
835 {
836 
837           kernconfig_lock();
838           KASSERT(mod->mod_refcnt > 0);
839           mod->mod_refcnt--;
840           kernconfig_unlock();
841 }
842 
843 /*
844  * module_enqueue:
845  *
846  *        Put a module onto the global list and update counters.
847  */
848 void
module_enqueue(module_t * mod)849 module_enqueue(module_t *mod)
850 {
851           int i;
852 
853           KASSERT(kernconfig_is_held());
854 
855           /*
856            * Put new entry at the head of the queue so autounload can unload
857            * requisite modules with only one pass through the queue.
858            */
859           TAILQ_INSERT_HEAD(&module_list, mod, mod_chain);
860           if (mod->mod_nrequired) {
861 
862                     /* Add references to the requisite modules. */
863                     for (i = 0; i < mod->mod_nrequired; i++) {
864                               KASSERT((*mod->mod_required)[i] != NULL);
865                               (*mod->mod_required)[i]->mod_refcnt++;
866                     }
867           }
868           module_count++;
869           module_gen++;
870 }
871 
872 /*
873  * Our array of required module pointers starts with zero entries.  If we
874  * need to add a new entry, and the list is already full, we reallocate a
875  * larger array, adding MAXMODDEPS entries.
876  */
877 static void
alloc_required(module_t * mod)878 alloc_required(module_t *mod)
879 {
880           module_t *(*new)[], *(*old)[];
881           int areq;
882           int i;
883 
884           if (mod->mod_nrequired >= mod->mod_arequired) {
885                     areq = mod->mod_arequired + MAXMODDEPS;
886                     old = mod->mod_required;
887                     new = kmem_zalloc(areq * sizeof(module_t *), KM_SLEEP);
888                     for (i = 0; i < mod->mod_arequired; i++)
889                               (*new)[i] = (*old)[i];
890                     mod->mod_required = new;
891                     if (old)
892                               kmem_free(old, mod->mod_arequired * sizeof(module_t *));
893                     mod->mod_arequired = areq;
894           }
895 }
896 
897 /*
898  * module_do_builtin:
899  *
900  *        Initialize a module from the list of modules that are
901  *        already linked into the kernel.
902  */
903 static int
module_do_builtin(const module_t * pmod,const char * name,module_t ** modp,prop_dictionary_t props)904 module_do_builtin(const module_t *pmod, const char *name, module_t **modp,
905     prop_dictionary_t props)
906 {
907           const char *p, *s;
908           char buf[MAXMODNAME];
909           modinfo_t *mi = NULL;
910           module_t *mod, *mod2, *mod_loaded, *prev_active;
911           size_t len;
912           int error;
913 
914           KASSERT(kernconfig_is_held());
915 
916           /*
917            * Search the list to see if we have a module by this name.
918            */
919           TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
920                     if (strcmp(mod->mod_info->mi_name, name) == 0) {
921                               mi = mod->mod_info;
922                               break;
923                     }
924           }
925 
926           /*
927            * Check to see if already loaded.  This might happen if we
928            * were already loaded as a dependency.
929            */
930           if ((mod_loaded = module_lookup(name)) != NULL) {
931                     KASSERT(mod == NULL);
932                     if (modp)
933                               *modp = mod_loaded;
934                     return 0;
935           }
936 
937           /* Note! This is from TAILQ, not immediate above */
938           if (mi == NULL) {
939                     /*
940                      * XXX: We'd like to panic here, but currently in some
941                      * cases (such as nfsserver + nfs), the dependee can be
942                      * successfully linked without the dependencies.
943                      */
944                     module_error("Built-in module `%s' can't find built-in "
945                         "dependency `%s'", pmod->mod_info->mi_name, name);
946                     return ENOENT;
947           }
948 
949           /*
950            * Initialize pre-requisites.
951            */
952           KASSERT(mod->mod_required == NULL);
953           KASSERT(mod->mod_arequired == 0);
954           KASSERT(mod->mod_nrequired == 0);
955           if (mi->mi_required != NULL) {
956                     for (s = mi->mi_required; *s != '\0'; s = p) {
957                               if (*s == ',')
958                                         s++;
959                               p = s;
960                               while (*p != '\0' && *p != ',')
961                                         p++;
962                               len = uimin(p - s + 1, sizeof(buf));
963                               strlcpy(buf, s, len);
964                               if (buf[0] == '\0')
965                                         break;
966                               alloc_required(mod);
967                               error = module_do_builtin(mod, buf, &mod2, NULL);
968                               if (error != 0) {
969                                         module_error("Built-in module `%s' prerequisite "
970                                             "`%s' failed, error %d", name, buf, error);
971                                         goto fail;
972                               }
973                               (*mod->mod_required)[mod->mod_nrequired++] = mod2;
974                     }
975           }
976 
977           /*
978            * Try to initialize the module.
979            */
980           prev_active = module_active;
981           module_active = mod;
982           error = (*mi->mi_modcmd)(MODULE_CMD_INIT, props);
983           module_active = prev_active;
984           if (error != 0) {
985                     module_error("Built-in module `%s' failed its MODULE_CMD_INIT, "
986                         "error %d", mi->mi_name, error);
987                     goto fail;
988           }
989 
990           /* load always succeeds after this point */
991 
992           TAILQ_REMOVE(&module_builtins, mod, mod_chain);
993           module_builtinlist--;
994           if (modp != NULL) {
995                     *modp = mod;
996           }
997           module_enqueue(mod);
998           return 0;
999 
1000  fail:
1001           if (mod->mod_required)
1002                     kmem_free(mod->mod_required, mod->mod_arequired *
1003                         sizeof(module_t *));
1004           mod->mod_arequired = 0;
1005           mod->mod_nrequired = 0;
1006           mod->mod_required = NULL;
1007           return error;
1008 }
1009 
1010 /*
1011  * module_load_sysctl
1012  *
1013  * Check to see if a non-builtin module has any SYSCTL_SETUP() routine(s)
1014  * registered.  If so, call it (them).
1015  */
1016 
1017 static void
module_load_sysctl(module_t * mod)1018 module_load_sysctl(module_t *mod)
1019 {
1020           void (**ls_funcp)(struct sysctllog **);
1021           void *ls_start;
1022           size_t ls_size, count;
1023           int error;
1024 
1025           /*
1026            * Built-in modules don't have a mod_kobj so we cannot search
1027            * for their link_set_sysctl_funcs
1028            */
1029           if (mod->mod_source == MODULE_SOURCE_KERNEL)
1030                     return;
1031 
1032           error = kobj_find_section(mod->mod_kobj, "link_set_sysctl_funcs",
1033               &ls_start, &ls_size);
1034           if (error == 0) {
1035                     count = ls_size / sizeof(ls_start);
1036                     ls_funcp = ls_start;
1037                     while (count--) {
1038                               (**ls_funcp)(&mod->mod_sysctllog);
1039                               ls_funcp++;
1040                     }
1041           }
1042 }
1043 
1044 /*
1045  * module_load_evcnt
1046  *
1047  * Check to see if a non-builtin module has any static evcnt's defined;
1048  * if so, attach them.
1049  */
1050 
1051 static void
module_load_evcnt(module_t * mod)1052 module_load_evcnt(module_t *mod)
1053 {
1054           struct evcnt * const *ls_evp;
1055           void *ls_start;
1056           size_t ls_size, count;
1057           int error;
1058 
1059           /*
1060            * Built-in modules' static evcnt stuff will be handled
1061            * automatically as part of general kernel initialization
1062            */
1063           if (mod->mod_source == MODULE_SOURCE_KERNEL)
1064                     return;
1065 
1066           error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
1067               &ls_start, &ls_size);
1068           if (error == 0) {
1069                     count = ls_size / sizeof(*ls_evp);
1070                     ls_evp = ls_start;
1071                     while (count--) {
1072                               evcnt_attach_static(*ls_evp++);
1073                     }
1074           }
1075 }
1076 
1077 /*
1078  * module_unload_evcnt
1079  *
1080  * Check to see if a non-builtin module has any static evcnt's defined;
1081  * if so, detach them.
1082  */
1083 
1084 static void
module_unload_evcnt(module_t * mod)1085 module_unload_evcnt(module_t *mod)
1086 {
1087           struct evcnt * const *ls_evp;
1088           void *ls_start;
1089           size_t ls_size, count;
1090           int error;
1091 
1092           /*
1093            * Built-in modules' static evcnt stuff will be handled
1094            * automatically as part of general kernel initialization
1095            */
1096           if (mod->mod_source == MODULE_SOURCE_KERNEL)
1097                     return;
1098 
1099           error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
1100               &ls_start, &ls_size);
1101           if (error == 0) {
1102                     count = ls_size / sizeof(*ls_evp);
1103                     ls_evp = (void *)((char *)ls_start + ls_size);
1104                     while (count--) {
1105                               evcnt_detach(*--ls_evp);
1106                     }
1107           }
1108 }
1109 
1110 /*
1111  * module_do_load:
1112  *
1113  *        Helper routine: load a module from the file system, or one
1114  *        pushed by the boot loader.
1115  */
1116 static int
module_do_load(const char * name,bool isdep,int flags,prop_dictionary_t props,module_t ** modp,modclass_t modclass,bool autoload)1117 module_do_load(const char *name, bool isdep, int flags,
1118                  prop_dictionary_t props, module_t **modp, modclass_t modclass,
1119                  bool autoload)
1120 {
1121           /* The pending list for this level of recursion */
1122           TAILQ_HEAD(pending_t, module);
1123           struct pending_t *pending;
1124           struct pending_t new_pending = TAILQ_HEAD_INITIALIZER(new_pending);
1125 
1126           /* The stack of pending lists */
1127           static SLIST_HEAD(pend_head, pend_entry) pend_stack =
1128                     SLIST_HEAD_INITIALIZER(pend_stack);
1129           struct pend_entry {
1130                     SLIST_ENTRY(pend_entry) pe_entry;
1131                     struct pending_t *pe_pending;
1132           } my_pend_entry;
1133 
1134           modinfo_t *mi;
1135           module_t *mod, *mod2, *prev_active;
1136           prop_dictionary_t filedict;
1137           char buf[MAXMODNAME];
1138           const char *s, *p;
1139           int error;
1140           size_t len;
1141 
1142           KASSERT(kernconfig_is_held());
1143 
1144           filedict = NULL;
1145           error = 0;
1146 
1147           /*
1148            * Set up the pending list for this entry.  If this is an
1149            * internal entry (for a dependency), then use the same list
1150            * as for the outer call;  otherwise, it's an external entry
1151            * (possibly recursive, ie a module's xxx_modcmd(init, ...)
1152            * routine called us), so use the locally allocated list.  In
1153            * either case, add it to our stack.
1154            */
1155           if (isdep) {
1156                     KASSERT(SLIST_FIRST(&pend_stack) != NULL);
1157                     pending = SLIST_FIRST(&pend_stack)->pe_pending;
1158           } else
1159                     pending = &new_pending;
1160           my_pend_entry.pe_pending = pending;
1161           SLIST_INSERT_HEAD(&pend_stack, &my_pend_entry, pe_entry);
1162 
1163           /*
1164            * Search the list of disabled builtins first.
1165            */
1166           TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
1167                     if (strcmp(mod->mod_info->mi_name, name) == 0) {
1168                               break;
1169                     }
1170           }
1171           if (mod) {
1172                     if (ISSET(mod->mod_flags, MODFLG_MUST_FORCE) &&
1173                         !ISSET(flags, MODCTL_LOAD_FORCE)) {
1174                               if (!autoload) {
1175                                         module_error("Use -f to reinstate "
1176                                             "builtin module `%s'", name);
1177                               }
1178                               SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1179                               return EPERM;
1180                     } else {
1181                               SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1182                               error = module_do_builtin(mod, name, modp, props);
1183                               module_print("module_do_builtin() returned %d", error);
1184                               return error;
1185                     }
1186           }
1187 
1188           /*
1189            * Load the module and link.  Before going to the file system,
1190            * scan the list of modules loaded by the boot loader.
1191            */
1192           TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
1193                     if (strcmp(mod->mod_info->mi_name, name) == 0) {
1194                               TAILQ_REMOVE(&module_bootlist, mod, mod_chain);
1195                               break;
1196                     }
1197           }
1198           if (mod != NULL) {
1199                     TAILQ_INSERT_TAIL(pending, mod, mod_chain);
1200           } else {
1201                     /*
1202                      * Check to see if module is already present.
1203                      */
1204                     mod = module_lookup(name);
1205                     if (mod != NULL) {
1206                               if (modp != NULL) {
1207                                         *modp = mod;
1208                               }
1209                               module_print("%s module `%s' already loaded",
1210                                   isdep ? "Dependent" : "Requested", name);
1211                               SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1212                               return EEXIST;
1213                     }
1214 
1215                     mod = module_newmodule(MODULE_SOURCE_FILESYS);
1216                     if (mod == NULL) {
1217                               module_error("Out of memory for `%s'", name);
1218                               SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1219                               return ENOMEM;
1220                     }
1221 
1222                     error = module_load_vfs_vec(name, flags, autoload, mod,
1223                                                       &filedict);
1224                     if (error != 0) {
1225 #ifdef DEBUG
1226                               /*
1227                                * The exec class of modules contains a list of
1228                                * modules that is the union of all the modules
1229                                * available for each architecture, so we don't
1230                                * print an error if they are missing.
1231                                */
1232                               if ((modclass != MODULE_CLASS_EXEC || error != ENOENT)
1233                                   && root_device != NULL)
1234                                         module_error("module_load_vfs_vec() failed "
1235                                             "for `%s', error %d", name, error);
1236                               else
1237 #endif
1238                                         module_print("module_load_vfs_vec() failed "
1239                                             "for `%s', error %d", name, error);
1240                               SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1241                               module_free(mod);
1242                               return error;
1243                     }
1244                     TAILQ_INSERT_TAIL(pending, mod, mod_chain);
1245 
1246                     error = module_fetch_info(mod);
1247                     if (error != 0) {
1248                               module_error("Cannot fetch info for `%s', error %d",
1249                                   name, error);
1250                               goto fail;
1251                     }
1252           }
1253 
1254           /*
1255            * Check compatibility.
1256            */
1257           mi = mod->mod_info;
1258           if (strnlen(mi->mi_name, MAXMODNAME) >= MAXMODNAME) {
1259                     error = EINVAL;
1260                     module_error("Module name `%s' longer than %d", mi->mi_name,
1261                         MAXMODNAME);
1262                     goto fail;
1263           }
1264           if (mi->mi_class <= MODULE_CLASS_ANY ||
1265               mi->mi_class >= MODULE_CLASS_MAX) {
1266                     error = EINVAL;
1267                     module_error("Module `%s' has invalid class %d",
1268                         mi->mi_name, mi->mi_class);
1269                         goto fail;
1270           }
1271           if (!module_compatible(mi->mi_version, __NetBSD_Version__)) {
1272                     module_error("Module `%s' built for `%d', system `%d'",
1273                         mi->mi_name, mi->mi_version, __NetBSD_Version__);
1274                     if (ISSET(flags, MODCTL_LOAD_FORCE)) {
1275                               module_error("Forced load, system may be unstable");
1276                     } else {
1277                               error = EPROGMISMATCH;
1278                               goto fail;
1279                     }
1280           }
1281 
1282           /*
1283            * If a specific kind of module was requested, ensure that we have
1284            * a match.
1285            */
1286           if (!MODULE_CLASS_MATCH(mi, modclass)) {
1287                     module_incompat(mi, modclass);
1288                     error = ENOENT;
1289                     goto fail;
1290           }
1291 
1292           /*
1293            * If loading a dependency, `name' is a plain module name.
1294            * The name must match.
1295            */
1296           if (isdep && strcmp(mi->mi_name, name) != 0) {
1297                     module_error("Dependency name mismatch (`%s' != `%s')",
1298                         name, mi->mi_name);
1299                     error = ENOENT;
1300                     goto fail;
1301           }
1302 
1303           /*
1304            * If we loaded a module from the filesystem, check the actual
1305            * module name (from the modinfo_t) to ensure another module
1306            * with the same name doesn't already exist.  (There's no
1307            * guarantee the filename will match the module name, and the
1308            * dup-symbols check may not be sufficient.)
1309            */
1310           if (mod->mod_source == MODULE_SOURCE_FILESYS) {
1311                     mod2 = module_lookup(mod->mod_info->mi_name);
1312                     if ( mod2 && mod2 != mod) {
1313                               module_error("Module with name `%s' already loaded",
1314                                   mod2->mod_info->mi_name);
1315                               error = EEXIST;
1316                               if (modp != NULL)
1317                                         *modp = mod2;
1318                               goto fail;
1319                     }
1320           }
1321 
1322           /*
1323            * Block circular dependencies.
1324            */
1325           TAILQ_FOREACH(mod2, pending, mod_chain) {
1326                     if (mod == mod2) {
1327                               continue;
1328                     }
1329                     if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) {
1330                               error = EDEADLK;
1331                               module_error("Circular dependency detected for `%s'",
1332                                   mi->mi_name);
1333                               goto fail;
1334                     }
1335           }
1336 
1337           /*
1338            * Now try to load any requisite modules.
1339            */
1340           if (mi->mi_required != NULL) {
1341                     mod->mod_arequired = 0;
1342                     for (s = mi->mi_required; *s != '\0'; s = p) {
1343                               if (*s == ',')
1344                                         s++;
1345                               p = s;
1346                               while (*p != '\0' && *p != ',')
1347                                         p++;
1348                               len = p - s + 1;
1349                               if (len >= MAXMODNAME) {
1350                                         error = EINVAL;
1351                                         module_error("Required module name `%s' "
1352                                             "longer than %d", mi->mi_required,
1353                                             MAXMODNAME);
1354                                         goto fail;
1355                               }
1356                               strlcpy(buf, s, len);
1357                               if (buf[0] == '\0')
1358                                         break;
1359                               alloc_required(mod);
1360                               if (strcmp(buf, mi->mi_name) == 0) {
1361                                         error = EDEADLK;
1362                                         module_error("Self-dependency detected for "
1363                                            "`%s'", mi->mi_name);
1364                                         goto fail;
1365                               }
1366                               error = module_do_load(buf, true, flags, NULL,
1367                                   &mod2, MODULE_CLASS_ANY, true);
1368                               if (error != 0 && error != EEXIST) {
1369                                         module_error("Recursive load failed for `%s' "
1370                                             "(`%s' required), error %d", mi->mi_name,
1371                                             buf, error);
1372                                         goto fail;
1373                               }
1374                               (*mod->mod_required)[mod->mod_nrequired++] = mod2;
1375                     }
1376           }
1377 
1378           /*
1379            * We loaded all needed modules successfully: perform global
1380            * relocations and initialize.
1381            */
1382           {
1383                     char xname[MAXMODNAME];
1384 
1385                     /*
1386                      * In case of error the entire module is gone, so we
1387                      * need to save its name for possible error report.
1388                      */
1389 
1390                     strlcpy(xname, mi->mi_name, MAXMODNAME);
1391                     error = kobj_affix(mod->mod_kobj, mi->mi_name);
1392                     if (error != 0) {
1393                               module_error("Unable to affix module `%s', error %d",
1394                                   xname, error);
1395                               goto fail2;
1396                     }
1397           }
1398 
1399           if (filedict) {
1400                     if (!module_merge_dicts(filedict, props)) {
1401                               module_error("Module properties failed for %s", name);
1402                               error = EINVAL;
1403                               goto fail;
1404                     }
1405           }
1406 
1407           prev_active = module_active;
1408           module_active = mod;
1409 
1410           /*
1411            * Note that we handle sysctl and evcnt setup _before_ we
1412            * initialize the module itself.  This maintains a consistent
1413            * order between built-in and run-time-loaded modules.  If
1414            * initialization then fails, we'll need to undo these, too.
1415            */
1416           module_load_sysctl(mod);      /* Set-up module's sysctl if any */
1417           module_load_evcnt(mod);                 /* Attach any static evcnt needed */
1418 
1419 
1420           error = (*mi->mi_modcmd)(MODULE_CMD_INIT, filedict ? filedict : props);
1421           module_active = prev_active;
1422           if (filedict) {
1423                     prop_object_release(filedict);
1424                     filedict = NULL;
1425           }
1426           if (error != 0) {
1427                     module_error("modcmd(CMD_INIT) failed for `%s', error %d",
1428                         mi->mi_name, error);
1429                     goto fail3;
1430           }
1431 
1432           /*
1433            * If a recursive load already added a module with the same
1434            * name, abort.
1435            */
1436           mod2 = module_lookup(mi->mi_name);
1437           if (mod2 && mod2 != mod) {
1438                     module_error("Recursive load causes duplicate module `%s'",
1439                         mi->mi_name);
1440                     error = EEXIST;
1441                     goto fail1;
1442           }
1443 
1444           /*
1445            * Good, the module loaded successfully.  Put it onto the
1446            * list and add references to its requisite modules.
1447            */
1448           TAILQ_REMOVE(pending, mod, mod_chain);
1449           module_enqueue(mod);
1450           if (modp != NULL) {
1451                     *modp = mod;
1452           }
1453           if (autoload && module_autotime > 0) {
1454                     /*
1455                      * Arrange to try unloading the module after
1456                      * a short delay unless auto-unload is disabled.
1457                      */
1458                     mod->mod_autotime = time_second + module_autotime;
1459                     SET(mod->mod_flags, MODFLG_AUTO_LOADED);
1460                     module_thread_kick();
1461           }
1462           SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1463           module_print("Module `%s' loaded successfully", mi->mi_name);
1464           module_callback_load(mod);
1465           return 0;
1466 
1467  fail1:
1468           (*mi->mi_modcmd)(MODULE_CMD_FINI, NULL);
1469  fail3:
1470           /*
1471            * If there were any registered SYSCTL_SETUP funcs, make sure
1472            * we release the sysctl entries
1473            */
1474           if (mod->mod_sysctllog) {
1475                     sysctl_teardown(&mod->mod_sysctllog);
1476           }
1477           /* Also detach any static evcnt's */
1478           module_unload_evcnt(mod);
1479  fail:
1480           kobj_unload(mod->mod_kobj);
1481  fail2:
1482           if (filedict != NULL) {
1483                     prop_object_release(filedict);
1484                     filedict = NULL;
1485           }
1486           TAILQ_REMOVE(pending, mod, mod_chain);
1487           SLIST_REMOVE_HEAD(&pend_stack, pe_entry);
1488           module_free(mod);
1489           module_print("Load failed, error %d", error);
1490           return error;
1491 }
1492 
1493 /*
1494  * module_do_unload:
1495  *
1496  *        Helper routine: do the dirty work of unloading a module.
1497  */
1498 static int
module_do_unload(const char * name,bool load_requires_force)1499 module_do_unload(const char *name, bool load_requires_force)
1500 {
1501           module_t *mod, *prev_active;
1502           int error;
1503           u_int i;
1504 
1505           KASSERT(kernconfig_is_held());
1506           KASSERT(name != NULL);
1507 
1508           module_print("Unload requested for `%s' (requires_force %s)", name,
1509               load_requires_force ? "TRUE" : "FALSE");
1510           mod = module_lookup(name);
1511           if (mod == NULL) {
1512                     module_error("Module `%s' not found", name);
1513                     return ENOENT;
1514           }
1515           if (mod->mod_refcnt != 0) {
1516                     module_print("Module `%s' busy (%d refs)", name,
1517                         mod->mod_refcnt);
1518                     return EBUSY;
1519           }
1520 
1521           /*
1522            * Builtin secmodels are there to stay.
1523            */
1524           if (mod->mod_source == MODULE_SOURCE_KERNEL &&
1525               mod->mod_info->mi_class == MODULE_CLASS_SECMODEL) {
1526                     module_print("Cannot unload built-in secmodel module `%s'",
1527                         name);
1528                     return EPERM;
1529           }
1530 
1531           prev_active = module_active;
1532           module_active = mod;
1533           module_callback_unload(mod);
1534 
1535           /* let the module clean up after itself */
1536           error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
1537 
1538           /*
1539            * If there were any registered SYSCTL_SETUP funcs, make sure
1540            * we release the sysctl entries.  Same for static evcnt.
1541            */
1542           if (error == 0) {
1543                     if (mod->mod_sysctllog) {
1544                               sysctl_teardown(&mod->mod_sysctllog);
1545                     }
1546                     module_unload_evcnt(mod);
1547           }
1548           module_active = prev_active;
1549           if (error != 0) {
1550                     module_print("Could not unload module `%s' error=%d", name,
1551                         error);
1552                     return error;
1553           }
1554           module_count--;
1555           TAILQ_REMOVE(&module_list, mod, mod_chain);
1556           for (i = 0; i < mod->mod_nrequired; i++) {
1557                     (*mod->mod_required)[i]->mod_refcnt--;
1558           }
1559           module_print("Unloaded module `%s'", name);
1560           if (mod->mod_kobj != NULL) {
1561                     kobj_unload(mod->mod_kobj);
1562           }
1563           if (mod->mod_source == MODULE_SOURCE_KERNEL) {
1564                     if (mod->mod_required != NULL) {
1565                               /*
1566                                * release "required" resources - will be re-parsed
1567                                * if the module is re-enabled
1568                                */
1569                               kmem_free(mod->mod_required,
1570                                   mod->mod_arequired * sizeof(module_t *));
1571                               mod->mod_nrequired = 0;
1572                               mod->mod_arequired = 0;
1573                               mod->mod_required = NULL;
1574                     }
1575                     if (load_requires_force)
1576                               module_require_force(mod);
1577                     TAILQ_INSERT_TAIL(&module_builtins, mod, mod_chain);
1578                     module_builtinlist++;
1579           } else {
1580                     module_free(mod);
1581           }
1582           module_gen++;
1583 
1584           return 0;
1585 }
1586 
1587 /*
1588  * module_prime:
1589  *
1590  *        Push a module loaded by the bootloader onto our internal
1591  *        list.
1592  */
1593 int
module_prime(const char * name,void * base,size_t size)1594 module_prime(const char *name, void *base, size_t size)
1595 {
1596           __link_set_decl(modules, modinfo_t);
1597           modinfo_t *const *mip;
1598           module_t *mod;
1599           int error;
1600 
1601           /* Check for module name same as a built-in module */
1602 
1603           __link_set_foreach(mip, modules) {
1604                     if (*mip == &module_dummy)
1605                               continue;
1606                     if (strcmp((*mip)->mi_name, name) == 0) {
1607                               module_error("Module `%s' pushed by boot loader "
1608                                   "already exists", name);
1609                               return EEXIST;
1610                     }
1611           }
1612 
1613           /* Also eliminate duplicate boolist entries */
1614 
1615           TAILQ_FOREACH(mod, &module_bootlist, mod_chain) {
1616                     if (strcmp(mod->mod_info->mi_name, name) == 0) {
1617                               module_error("Duplicate bootlist entry for module "
1618                                   "`%s'", name);
1619                               return EEXIST;
1620                     }
1621           }
1622 
1623           mod = module_newmodule(MODULE_SOURCE_BOOT);
1624           if (mod == NULL) {
1625                     return ENOMEM;
1626           }
1627 
1628           error = kobj_load_mem(&mod->mod_kobj, name, base, size);
1629           if (error != 0) {
1630                     module_free(mod);
1631                     module_error("Unable to load `%s' pushed by boot loader, "
1632                         "error %d", name, error);
1633                     return error;
1634           }
1635           error = module_fetch_info(mod);
1636           if (error != 0) {
1637                     kobj_unload(mod->mod_kobj);
1638                     module_free(mod);
1639                     module_error("Unable to fetch_info for `%s' pushed by boot "
1640                         "loader, error %d", name, error);
1641                     return error;
1642           }
1643 
1644           TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain);
1645 
1646           return 0;
1647 }
1648 
1649 /*
1650  * module_fetch_into:
1651  *
1652  *        Fetch modinfo record from a loaded module.
1653  */
1654 static int
module_fetch_info(module_t * mod)1655 module_fetch_info(module_t *mod)
1656 {
1657           int error;
1658           void *addr;
1659           size_t size;
1660 
1661           /*
1662            * Find module info record and check compatibility.
1663            */
1664           error = kobj_find_section(mod->mod_kobj, "link_set_modules",
1665               &addr, &size);
1666           if (error != 0) {
1667                     module_error("`link_set_modules' section not present, "
1668                         "error %d", error);
1669                     return error;
1670           }
1671           if (size != sizeof(modinfo_t **)) {
1672                     if (size > sizeof(modinfo_t **) &&
1673                         (size % sizeof(modinfo_t **)) == 0) {
1674                               module_error("`link_set_modules' section wrong size "
1675                                   "(%zu different MODULE declarations?)",
1676                                   size / sizeof(modinfo_t **));
1677                     } else {
1678                               module_error("`link_set_modules' section wrong size "
1679                                   "(got %zu, wanted %zu)",
1680                                   size, sizeof(modinfo_t **));
1681                     }
1682                     return ENOEXEC;
1683           }
1684           mod->mod_info = *(modinfo_t **)addr;
1685 
1686           return 0;
1687 }
1688 
1689 /*
1690  * module_find_section:
1691  *
1692  *        Allows a module that is being initialized to look up a section
1693  *        within its ELF object.
1694  */
1695 int
module_find_section(const char * name,void ** addr,size_t * size)1696 module_find_section(const char *name, void **addr, size_t *size)
1697 {
1698 
1699           KASSERT(kernconfig_is_held());
1700           KASSERT(module_active != NULL);
1701 
1702           return kobj_find_section(module_active->mod_kobj, name, addr, size);
1703 }
1704 
1705 /*
1706  * module_thread:
1707  *
1708  *        Automatically unload modules.  We try once to unload autoloaded
1709  *        modules after module_autotime seconds.  If the system is under
1710  *        severe memory pressure, we'll try unloading all modules, else if
1711  *        module_autotime is zero, we don't try to unload, even if the
1712  *        module was previously scheduled for unload.
1713  */
1714 static void
module_thread(void * cookie)1715 module_thread(void *cookie)
1716 {
1717           module_t *mod, *next;
1718           modinfo_t *mi;
1719           int error;
1720 
1721           for (;;) {
1722                     kernconfig_lock();
1723                     for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) {
1724                               next = TAILQ_NEXT(mod, mod_chain);
1725 
1726                               /* skip built-in modules */
1727                               if (mod->mod_source == MODULE_SOURCE_KERNEL)
1728                                         continue;
1729                               /* skip modules that weren't auto-loaded */
1730                               if (!ISSET(mod->mod_flags, MODFLG_AUTO_LOADED))
1731                                         continue;
1732 
1733                               if (uvm_availmem(false) < uvmexp.freemin) {
1734                                         module_thread_ticks = hz;
1735                               } else if (module_autotime == 0 ||
1736                                            mod->mod_autotime == 0) {
1737                                         continue;
1738                               } else if (time_second < mod->mod_autotime) {
1739                                         module_thread_ticks = hz;
1740                                         continue;
1741                               } else {
1742                                         mod->mod_autotime = 0;
1743                               }
1744 
1745                               /*
1746                                * Ask the module if it can be safely unloaded.
1747                                *
1748                                * - Modules which have been audited to be OK
1749                                *   with that will return 0.
1750                                *
1751                                * - Modules which have not been audited for
1752                                *   safe autounload will return ENOTTY.
1753                                *
1754                                *   => With kern.module.autounload_unsafe=1,
1755                                *      we treat ENOTTY as acceptance.
1756                                *
1757                                * - Some modules would ping-ping in and out
1758                                *   because their use is transient but often.
1759                                *   Example: exec_script.  Other modules may
1760                                *   still be in use.  These modules can
1761                                *   prevent autounload in all cases by
1762                                *   returning EBUSY or some other error code.
1763                                */
1764                               mi = mod->mod_info;
1765                               error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL);
1766                               if (error == 0 ||
1767                                   (error == ENOTTY && module_autounload_unsafe)) {
1768                                         module_print("Requesting autounload for"
1769                                             "`%s'", mi->mi_name);
1770                                         (void)module_do_unload(mi->mi_name, false);
1771                               } else
1772                                         module_print("Module `%s' declined to be "
1773                                             "auto-unloaded error=%d", mi->mi_name,
1774                                             error);
1775                     }
1776                     kernconfig_unlock();
1777 
1778                     mutex_enter(&module_thread_lock);
1779                     (void)cv_timedwait(&module_thread_cv, &module_thread_lock,
1780                         module_thread_ticks);
1781                     module_thread_ticks = 0;
1782                     mutex_exit(&module_thread_lock);
1783           }
1784 }
1785 
1786 /*
1787  * module_thread:
1788  *
1789  *        Kick the module thread into action, perhaps because the
1790  *        system is low on memory.
1791  */
1792 void
module_thread_kick(void)1793 module_thread_kick(void)
1794 {
1795 
1796           mutex_enter(&module_thread_lock);
1797           module_thread_ticks = hz;
1798           cv_broadcast(&module_thread_cv);
1799           mutex_exit(&module_thread_lock);
1800 }
1801 
1802 #ifdef DDB
1803 /*
1804  * module_whatis:
1805  *
1806  *        Helper routine for DDB.
1807  */
1808 void
module_whatis(uintptr_t addr,void (* pr)(const char *,...))1809 module_whatis(uintptr_t addr, void (*pr)(const char *, ...))
1810 {
1811           module_t *mod;
1812           size_t msize;
1813           vaddr_t maddr;
1814 
1815           TAILQ_FOREACH(mod, &module_list, mod_chain) {
1816                     if (mod->mod_kobj == NULL) {
1817                               continue;
1818                     }
1819                     if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1820                               continue;
1821                     if (addr < maddr || addr >= maddr + msize) {
1822                               continue;
1823                     }
1824                     (*pr)("%p is %p+%zu, in kernel module `%s'\n",
1825                         (void *)addr, (void *)maddr,
1826                         (size_t)(addr - maddr), mod->mod_info->mi_name);
1827           }
1828 }
1829 
1830 /*
1831  * module_print_list:
1832  *
1833  *        Helper routine for DDB.
1834  */
1835 void
module_print_list(void (* pr)(const char *,...))1836 module_print_list(void (*pr)(const char *, ...))
1837 {
1838           const char *src;
1839           module_t *mod;
1840           size_t msize;
1841           vaddr_t maddr;
1842 
1843           (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE");
1844 
1845           TAILQ_FOREACH(mod, &module_list, mod_chain) {
1846                     switch (mod->mod_source) {
1847                     case MODULE_SOURCE_KERNEL:
1848                               src = "builtin";
1849                               break;
1850                     case MODULE_SOURCE_FILESYS:
1851                               src = "filesys";
1852                               break;
1853                     case MODULE_SOURCE_BOOT:
1854                               src = "boot";
1855                               break;
1856                     default:
1857                               src = "unknown";
1858                               break;
1859                     }
1860                     if (mod->mod_kobj == NULL) {
1861                               maddr = 0;
1862                               msize = 0;
1863                     } else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0)
1864                               continue;
1865                     (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name,
1866                         (long)maddr, (long)msize, src);
1867           }
1868 }
1869 #endif    /* DDB */
1870 
1871 static bool
module_merge_dicts(prop_dictionary_t existing_dict,const prop_dictionary_t new_dict)1872 module_merge_dicts(prop_dictionary_t existing_dict,
1873                        const prop_dictionary_t new_dict)
1874 {
1875           prop_dictionary_keysym_t props_keysym;
1876           prop_object_iterator_t props_iter;
1877           prop_object_t props_obj;
1878           const char *props_key;
1879           bool error;
1880 
1881           if (new_dict == NULL) {                           /* nothing to merge */
1882                     return true;
1883           }
1884 
1885           error = false;
1886           props_iter = prop_dictionary_iterator(new_dict);
1887           if (props_iter == NULL) {
1888                     return false;
1889           }
1890 
1891           while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
1892                     props_keysym = (prop_dictionary_keysym_t)props_obj;
1893                     props_key = prop_dictionary_keysym_value(props_keysym);
1894                     props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
1895                     if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
1896                         props_key, props_obj)) {
1897                               error = true;
1898                               goto out;
1899                     }
1900           }
1901           error = false;
1902 
1903 out:
1904           prop_object_iterator_release(props_iter);
1905 
1906           return !error;
1907 }
1908 
1909 /*
1910  * module_specific_key_create:
1911  *
1912  *        Create a key for subsystem module-specific data.
1913  */
1914 specificdata_key_t
module_specific_key_create(specificdata_key_t * keyp,specificdata_dtor_t dtor)1915 module_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1916 {
1917 
1918           return specificdata_key_create(module_specificdata_domain, keyp, dtor);
1919 }
1920 
1921 /*
1922  * module_specific_key_delete:
1923  *
1924  *        Delete a key for subsystem module-specific data.
1925  */
1926 void
module_specific_key_delete(specificdata_key_t key)1927 module_specific_key_delete(specificdata_key_t key)
1928 {
1929 
1930           return specificdata_key_delete(module_specificdata_domain, key);
1931 }
1932 
1933 /*
1934  * module_getspecific:
1935  *
1936  *        Return module-specific data corresponding to the specified key.
1937  */
1938 void *
module_getspecific(module_t * mod,specificdata_key_t key)1939 module_getspecific(module_t *mod, specificdata_key_t key)
1940 {
1941 
1942           return specificdata_getspecific(module_specificdata_domain,
1943               &mod->mod_sdref, key);
1944 }
1945 
1946 /*
1947  * module_setspecific:
1948  *
1949  *        Set module-specific data corresponding to the specified key.
1950  */
1951 void
module_setspecific(module_t * mod,specificdata_key_t key,void * data)1952 module_setspecific(module_t *mod, specificdata_key_t key, void *data)
1953 {
1954 
1955           specificdata_setspecific(module_specificdata_domain,
1956               &mod->mod_sdref, key, data);
1957 }
1958 
1959 /*
1960  * module_register_callbacks:
1961  *
1962  *        Register a new set of callbacks to be called on module load/unload.
1963  *        Call the load callback on each existing module.
1964  *        Return an opaque handle for unregistering these later.
1965  */
1966 void *
module_register_callbacks(void (* load)(struct module *),void (* unload)(struct module *))1967 module_register_callbacks(void (*load)(struct module *),
1968     void (*unload)(struct module *))
1969 {
1970           struct module_callbacks *modcb;
1971           struct module *mod;
1972 
1973           modcb = kmem_alloc(sizeof(*modcb), KM_SLEEP);
1974           modcb->modcb_load = load;
1975           modcb->modcb_unload = unload;
1976 
1977           kernconfig_lock();
1978           TAILQ_INSERT_TAIL(&modcblist, modcb, modcb_list);
1979           TAILQ_FOREACH_REVERSE(mod, &module_list, modlist, mod_chain)
1980                     load(mod);
1981           kernconfig_unlock();
1982 
1983           return modcb;
1984 }
1985 
1986 /*
1987  * module_unregister_callbacks:
1988  *
1989  *        Unregister a previously-registered set of module load/unload callbacks.
1990  *        Call the unload callback on each existing module.
1991  */
1992 void
module_unregister_callbacks(void * opaque)1993 module_unregister_callbacks(void *opaque)
1994 {
1995           struct module_callbacks *modcb;
1996           struct module *mod;
1997 
1998           modcb = opaque;
1999           kernconfig_lock();
2000           TAILQ_FOREACH(mod, &module_list, mod_chain)
2001                     modcb->modcb_unload(mod);
2002           TAILQ_REMOVE(&modcblist, modcb, modcb_list);
2003           kernconfig_unlock();
2004           kmem_free(modcb, sizeof(*modcb));
2005 }
2006 
2007 /*
2008  * module_callback_load:
2009  *
2010  *        Helper routine: call all load callbacks on a module being loaded.
2011  */
2012 static void
module_callback_load(struct module * mod)2013 module_callback_load(struct module *mod)
2014 {
2015           struct module_callbacks *modcb;
2016 
2017           TAILQ_FOREACH(modcb, &modcblist, modcb_list) {
2018                     modcb->modcb_load(mod);
2019           }
2020 }
2021 
2022 /*
2023  * module_callback_unload:
2024  *
2025  *        Helper routine: call all unload callbacks on a module being unloaded.
2026  */
2027 static void
module_callback_unload(struct module * mod)2028 module_callback_unload(struct module *mod)
2029 {
2030           struct module_callbacks *modcb;
2031 
2032           TAILQ_FOREACH(modcb, &modcblist, modcb_list) {
2033                     modcb->modcb_unload(mod);
2034           }
2035 }
2036