2 * Copyright (c) 1997-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/sysproto.h>
39 #include <sys/sysent.h>
43 #include <sys/mutex.h>
45 #include <sys/module.h>
46 #include <sys/mount.h>
47 #include <sys/linker.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
51 #include <sys/libkern.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
63 #include <security/mac/mac_framework.h>
65 #include "linker_if.h"
68 #include <sys/pmckern.h>
73 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
74 &kld_debug, 0, "Set various levels of KLD debug");
77 /* These variables are used by kernel debuggers to enumerate loaded files. */
78 const int kld_off_address = offsetof(struct linker_file, address);
79 const int kld_off_filename = offsetof(struct linker_file, filename);
80 const int kld_off_pathname = offsetof(struct linker_file, pathname);
81 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
84 * static char *linker_search_path(const char *name, struct mod_depend
87 static const char *linker_basename(const char *path);
90 * Find a currently loaded file given its filename.
92 static linker_file_t linker_find_file_by_name(const char* _filename);
95 * Find a currently loaded file given its file id.
97 static linker_file_t linker_find_file_by_id(int _fileid);
99 /* Metadata from the static kernel */
100 SET_DECLARE(modmetadata_set, struct mod_metadata);
102 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
104 linker_file_t linker_kernel_file;
106 static struct sx kld_sx; /* kernel linker lock */
109 * Load counter used by clients to determine if a linker file has been
110 * re-loaded. This counter is incremented for each file load.
114 static linker_class_list_t classes;
115 static linker_file_list_t linker_files;
116 static int next_file_id = 1;
117 static int linker_no_more_classes = 0;
119 #define LINKER_GET_NEXT_FILE_ID(a) do { \
120 linker_file_t lftmp; \
123 sx_assert(&kld_sx, SA_XLOCKED); \
125 TAILQ_FOREACH(lftmp, &linker_files, link) { \
126 if (next_file_id == lftmp->id) { \
131 (a) = next_file_id; \
135 /* XXX wrong name; we're looking at version provision tags here, not modules */
136 typedef TAILQ_HEAD(, modlist) modlisthead_t;
138 TAILQ_ENTRY(modlist) link; /* chain together all modules */
139 linker_file_t container;
143 typedef struct modlist *modlist_t;
144 static modlisthead_t found_modules;
146 static int linker_file_add_dependency(linker_file_t file,
148 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
149 const char* name, int deps);
150 static int linker_load_module(const char *kldname,
151 const char *modname, struct linker_file *parent,
152 const struct mod_depend *verinfo, struct linker_file **lfpp);
153 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
156 linker_init(void *arg)
159 sx_init(&kld_sx, "kernel linker");
160 TAILQ_INIT(&classes);
161 TAILQ_INIT(&linker_files);
164 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
167 linker_stop_class_add(void *arg)
170 linker_no_more_classes = 1;
173 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
176 linker_add_class(linker_class_t lc)
180 * We disallow any class registration past SI_ORDER_ANY
181 * of SI_SUB_KLD. We bump the reference count to keep the
182 * ops from being freed.
184 if (linker_no_more_classes == 1)
186 kobj_class_compile((kobj_class_t) lc);
187 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
188 TAILQ_INSERT_TAIL(&classes, lc, link);
193 linker_file_sysinit(linker_file_t lf)
195 struct sysinit **start, **stop, **sipp, **xipp, *save;
197 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
200 sx_assert(&kld_sx, SA_XLOCKED);
202 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
205 * Perform a bubble sort of the system initialization objects by
206 * their subsystem (primary key) and order (secondary key).
208 * Since some things care about execution order, this is the operation
209 * which ensures continued function.
211 for (sipp = start; sipp < stop; sipp++) {
212 for (xipp = sipp + 1; xipp < stop; xipp++) {
213 if ((*sipp)->subsystem < (*xipp)->subsystem ||
214 ((*sipp)->subsystem == (*xipp)->subsystem &&
215 (*sipp)->order <= (*xipp)->order))
224 * Traverse the (now) ordered list of system initialization tasks.
225 * Perform each task, and continue on to the next task.
229 for (sipp = start; sipp < stop; sipp++) {
230 if ((*sipp)->subsystem == SI_SUB_DUMMY)
231 continue; /* skip dummy task(s) */
234 (*((*sipp)->func)) ((*sipp)->udata);
241 linker_file_sysuninit(linker_file_t lf)
243 struct sysinit **start, **stop, **sipp, **xipp, *save;
245 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
248 sx_assert(&kld_sx, SA_XLOCKED);
250 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
255 * Perform a reverse bubble sort of the system initialization objects
256 * by their subsystem (primary key) and order (secondary key).
258 * Since some things care about execution order, this is the operation
259 * which ensures continued function.
261 for (sipp = start; sipp < stop; sipp++) {
262 for (xipp = sipp + 1; xipp < stop; xipp++) {
263 if ((*sipp)->subsystem > (*xipp)->subsystem ||
264 ((*sipp)->subsystem == (*xipp)->subsystem &&
265 (*sipp)->order >= (*xipp)->order))
274 * Traverse the (now) ordered list of system initialization tasks.
275 * Perform each task, and continue on to the next task.
279 for (sipp = start; sipp < stop; sipp++) {
280 if ((*sipp)->subsystem == SI_SUB_DUMMY)
281 continue; /* skip dummy task(s) */
284 (*((*sipp)->func)) ((*sipp)->udata);
291 linker_file_register_sysctls(linker_file_t lf)
293 struct sysctl_oid **start, **stop, **oidp;
296 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
299 sx_assert(&kld_sx, SA_XLOCKED);
301 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
306 for (oidp = start; oidp < stop; oidp++)
307 sysctl_register_oid(*oidp);
313 linker_file_unregister_sysctls(linker_file_t lf)
315 struct sysctl_oid **start, **stop, **oidp;
317 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
318 " for %s\n", lf->filename));
320 sx_assert(&kld_sx, SA_XLOCKED);
322 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
327 for (oidp = start; oidp < stop; oidp++)
328 sysctl_unregister_oid(*oidp);
334 linker_file_register_modules(linker_file_t lf)
336 struct mod_metadata **start, **stop, **mdp;
337 const moduledata_t *moddata;
338 int first_error, error;
340 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
341 " in %s\n", lf->filename));
343 sx_assert(&kld_sx, SA_XLOCKED);
345 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
348 * This fallback should be unnecessary, but if we get booted
349 * from boot2 instead of loader and we are missing our
350 * metadata then we have to try the best we can.
352 if (lf == linker_kernel_file) {
353 start = SET_BEGIN(modmetadata_set);
354 stop = SET_LIMIT(modmetadata_set);
359 for (mdp = start; mdp < stop; mdp++) {
360 if ((*mdp)->md_type != MDT_MODULE)
362 moddata = (*mdp)->md_data;
363 KLD_DPF(FILE, ("Registering module %s in %s\n",
364 moddata->name, lf->filename));
365 error = module_register(moddata, lf);
367 printf("Module %s failed to register: %d\n",
368 moddata->name, error);
369 if (first_error == 0)
373 return (first_error);
377 linker_init_kernel_modules(void)
381 linker_file_register_modules(linker_kernel_file);
385 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
389 linker_load_file(const char *filename, linker_file_t *result)
393 int foundfile, error, modules;
395 /* Refuse to load modules if securelevel raised */
396 if (prison0.pr_securelevel > 0)
399 sx_assert(&kld_sx, SA_XLOCKED);
400 lf = linker_find_file_by_name(filename);
402 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
403 " incrementing refs\n", filename));
412 * We do not need to protect (lock) classes here because there is
413 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
414 * and there is no class deregistration mechanism at this time.
416 TAILQ_FOREACH(lc, &classes, link) {
417 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
419 error = LINKER_LOAD_FILE(lc, filename, &lf);
421 * If we got something other than ENOENT, then it exists but
422 * we cannot load it for some other reason.
427 error = linker_file_register_modules(lf);
428 if (error == EEXIST) {
429 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
432 modules = !TAILQ_EMPTY(&lf->modules);
433 linker_file_register_sysctls(lf);
434 linker_file_sysinit(lf);
435 lf->flags |= LINKER_FILE_LINKED;
438 * If all of the modules in this file failed
439 * to load, unload the file and return an
442 if (modules && TAILQ_EMPTY(&lf->modules)) {
443 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
446 EVENTHANDLER_INVOKE(kld_load, lf);
452 * Less than ideal, but tells the user whether it failed to load or
453 * the module was not found.
458 * If the file type has not been recognized by the last try
459 * printout a message before to fail.
462 printf("linker_load_file: Unsupported file type\n");
465 * Format not recognized or otherwise unloadable.
466 * When loading a module that is statically built into
467 * the kernel EEXIST percolates back up as the return
468 * value. Preserve this so that apps like sysinstall
469 * can recognize this special case and not post bogus
475 error = ENOENT; /* Nothing found */
480 linker_reference_module(const char *modname, struct mod_depend *verinfo,
481 linker_file_t *result)
487 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
488 *result = mod->container;
494 error = linker_load_module(NULL, modname, NULL, verinfo, result);
500 linker_release_module(const char *modname, struct mod_depend *verinfo,
508 KASSERT(modname != NULL,
509 ("linker_release_module: no file or name"));
510 mod = modlist_lookup2(modname, verinfo);
517 KASSERT(modname == NULL && verinfo == NULL,
518 ("linker_release_module: both file and name"));
519 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
525 linker_find_file_by_name(const char *filename)
530 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
531 sprintf(koname, "%s.ko", filename);
533 sx_assert(&kld_sx, SA_XLOCKED);
534 TAILQ_FOREACH(lf, &linker_files, link) {
535 if (strcmp(lf->filename, koname) == 0)
537 if (strcmp(lf->filename, filename) == 0)
540 free(koname, M_LINKER);
545 linker_find_file_by_id(int fileid)
549 sx_assert(&kld_sx, SA_XLOCKED);
550 TAILQ_FOREACH(lf, &linker_files, link)
551 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
557 linker_file_foreach(linker_predicate_t *predicate, void *context)
563 TAILQ_FOREACH(lf, &linker_files, link) {
564 retval = predicate(lf, context);
573 linker_make_file(const char *pathname, linker_class_t lc)
576 const char *filename;
579 sx_assert(&kld_sx, SA_XLOCKED);
580 filename = linker_basename(pathname);
582 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
583 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
591 lf->filename = strdup(filename, M_LINKER);
592 lf->pathname = strdup(pathname, M_LINKER);
593 LINKER_GET_NEXT_FILE_ID(lf->id);
596 lf->loadcnt = ++loadcnt;
597 STAILQ_INIT(&lf->common);
598 TAILQ_INIT(&lf->modules);
599 TAILQ_INSERT_TAIL(&linker_files, lf, link);
604 linker_file_unload(linker_file_t file, int flags)
607 modlist_t ml, nextml;
608 struct common_symbol *cp;
611 /* Refuse to unload modules if securelevel raised. */
612 if (prison0.pr_securelevel > 0)
615 sx_assert(&kld_sx, SA_XLOCKED);
616 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
618 /* Easy case of just dropping a reference. */
619 if (file->refs > 1) {
624 /* Give eventhandlers a chance to prevent the unload. */
626 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
630 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
631 " informing modules\n"));
634 * Quiesce all the modules to give them a chance to veto the unload.
637 for (mod = TAILQ_FIRST(&file->modules); mod;
638 mod = module_getfnext(mod)) {
640 error = module_quiesce(mod);
641 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
642 KLD_DPF(FILE, ("linker_file_unload: module %s"
643 " vetoed unload\n", module_getname(mod)));
645 * XXX: Do we need to tell all the quiesced modules
646 * that they can resume work now via a new module
656 * Inform any modules associated with this file that they are
660 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
661 next = module_getfnext(mod);
665 * Give the module a chance to veto the unload.
667 if ((error = module_unload(mod)) != 0) {
670 KLD_DPF(FILE, ("linker_file_unload: module %s"
671 " failed unload\n", module_getname(mod)));
681 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
682 if (ml->container == file) {
683 TAILQ_REMOVE(&found_modules, ml, link);
689 * Don't try to run SYSUNINITs if we are unloaded due to a
692 if (file->flags & LINKER_FILE_LINKED) {
693 file->flags &= ~LINKER_FILE_LINKED;
694 linker_file_sysuninit(file);
695 linker_file_unregister_sysctls(file);
697 TAILQ_REMOVE(&linker_files, file, link);
700 for (i = 0; i < file->ndeps; i++)
701 linker_file_unload(file->deps[i], flags);
702 free(file->deps, M_LINKER);
705 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
706 STAILQ_REMOVE_HEAD(&file->common, link);
712 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
715 if (file->filename) {
716 free(file->filename, M_LINKER);
717 file->filename = NULL;
719 if (file->pathname) {
720 free(file->pathname, M_LINKER);
721 file->pathname = NULL;
723 kobj_delete((kobj_t) file, M_LINKER);
728 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
730 return (LINKER_CTF_GET(file, lc));
734 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
736 linker_file_t *newdeps;
738 sx_assert(&kld_sx, SA_XLOCKED);
739 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
740 M_LINKER, M_WAITOK | M_ZERO);
741 file->deps[file->ndeps] = dep;
743 KLD_DPF(FILE, ("linker_file_add_dependency:"
744 " adding %s as dependency for %s\n",
745 dep->filename, file->filename));
750 * Locate a linker set and its contents. This is a helper function to avoid
751 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
752 * This function is used in this file so we can avoid having lots of (void **)
756 linker_file_lookup_set(linker_file_t file, const char *name,
757 void *firstp, void *lastp, int *countp)
760 sx_assert(&kld_sx, SA_LOCKED);
761 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
765 * List all functions in a file.
768 linker_file_function_listall(linker_file_t lf,
769 linker_function_nameval_callback_t callback_func, void *arg)
771 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
775 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
780 locked = sx_xlocked(&kld_sx);
783 sym = linker_file_lookup_symbol_internal(file, name, deps);
790 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
794 linker_symval_t symval;
796 size_t common_size = 0;
799 sx_assert(&kld_sx, SA_XLOCKED);
800 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
803 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
804 LINKER_SYMBOL_VALUES(file, sym, &symval);
805 if (symval.value == 0)
807 * For commons, first look them up in the
808 * dependencies and only allocate space if not found
811 common_size = symval.size;
813 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
814 ".value=%p\n", symval.value));
815 return (symval.value);
819 for (i = 0; i < file->ndeps; i++) {
820 address = linker_file_lookup_symbol_internal(
821 file->deps[i], name, 0);
823 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
824 " deps value=%p\n", address));
829 if (common_size > 0) {
831 * This is a common symbol which was not found in the
832 * dependencies. We maintain a simple common symbol table in
835 struct common_symbol *cp;
837 STAILQ_FOREACH(cp, &file->common, link) {
838 if (strcmp(cp->name, name) == 0) {
839 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
840 " old common value=%p\n", cp->address));
841 return (cp->address);
845 * Round the symbol size up to align.
847 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
848 cp = malloc(sizeof(struct common_symbol)
849 + common_size + strlen(name) + 1, M_LINKER,
851 cp->address = (caddr_t)(cp + 1);
852 cp->name = cp->address + common_size;
853 strcpy(cp->name, name);
854 bzero(cp->address, common_size);
855 STAILQ_INSERT_TAIL(&file->common, cp, link);
857 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
858 " value=%p\n", cp->address));
859 return (cp->address);
861 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
866 * Both DDB and stack(9) rely on the kernel linker to provide forward and
867 * backward lookup of symbols. However, DDB and sometimes stack(9) need to
868 * do this in a lockfree manner. We provide a set of internal helper
869 * routines to perform these operations without locks, and then wrappers that
872 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
876 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
880 TAILQ_FOREACH(lf, &linker_files, link) {
881 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
889 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
892 c_linker_sym_t best, es;
893 u_long diff, bestdiff, off;
896 off = (uintptr_t)value;
898 TAILQ_FOREACH(lf, &linker_files, link) {
899 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
901 if (es != 0 && diff < bestdiff) {
920 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
924 TAILQ_FOREACH(lf, &linker_files, link) {
925 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
932 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
935 linker_symval_t symval;
940 error = linker_debug_search_symbol(value, &sym, offset);
943 error = linker_debug_symbol_values(sym, &symval);
946 strlcpy(buf, symval.name, buflen);
951 * DDB Helpers. DDB has to look across multiple files with their own symbol
952 * tables and string tables.
954 * Note that we do not obey list locking protocols here. We really don't need
955 * DDB to hang because somebody's got the lock held. We'll take the chance
956 * that the files list is inconsistent instead.
960 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
963 return (linker_debug_lookup(symstr, sym));
968 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
971 return (linker_debug_search_symbol(value, sym, diffp));
975 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
978 return (linker_debug_symbol_values(sym, symval));
982 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
986 return (linker_debug_search_symbol_name(value, buf, buflen, offset));
990 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
991 * obey locking protocols, and offer a significantly less complex interface.
994 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1000 error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1001 sx_sunlock(&kld_sx);
1009 kern_kldload(struct thread *td, const char *file, int *fileid)
1011 const char *kldname, *modname;
1015 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1018 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1022 * It is possible that kldloaded module will attach a new ifnet,
1023 * so vnet context must be set when this ocurs.
1025 CURVNET_SET(TD_TO_VNET(td));
1028 * If file does not contain a qualified name or any dot in it
1029 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1032 if (strchr(file, '/') || strchr(file, '.')) {
1041 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1043 sx_xunlock(&kld_sx);
1049 sx_xunlock(&kld_sx);
1057 sys_kldload(struct thread *td, struct kldload_args *uap)
1059 char *pathname = NULL;
1062 td->td_retval[0] = -1;
1064 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1065 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1067 error = kern_kldload(td, pathname, &fileid);
1069 td->td_retval[0] = fileid;
1071 free(pathname, M_TEMP);
1076 kern_kldunload(struct thread *td, int fileid, int flags)
1081 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1084 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1087 CURVNET_SET(TD_TO_VNET(td));
1089 lf = linker_find_file_by_id(fileid);
1091 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1093 if (lf->userrefs == 0) {
1095 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1097 printf("kldunload: attempt to unload file that was"
1098 " loaded by the kernel\n");
1102 error = linker_file_unload(lf, flags);
1108 sx_xunlock(&kld_sx);
1115 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1118 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1122 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1125 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1126 uap->flags != LINKER_UNLOAD_FORCE)
1128 return (kern_kldunload(td, uap->fileid, uap->flags));
1132 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1135 const char *filename;
1140 error = mac_kld_check_stat(td->td_ucred);
1145 td->td_retval[0] = -1;
1147 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1148 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1151 filename = linker_basename(pathname);
1153 lf = linker_find_file_by_name(filename);
1155 td->td_retval[0] = lf->id;
1158 sx_xunlock(&kld_sx);
1160 free(pathname, M_TEMP);
1165 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1171 error = mac_kld_check_stat(td->td_ucred);
1177 if (uap->fileid == 0)
1178 lf = TAILQ_FIRST(&linker_files);
1180 lf = linker_find_file_by_id(uap->fileid);
1185 lf = TAILQ_NEXT(lf, link);
1188 /* Skip partially loaded files. */
1189 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1190 lf = TAILQ_NEXT(lf, link);
1193 td->td_retval[0] = lf->id;
1195 td->td_retval[0] = 0;
1197 sx_xunlock(&kld_sx);
1202 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1204 struct kld_file_stat stat;
1208 * Check the version of the user's structure.
1210 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1213 if (version != sizeof(struct kld_file_stat_1) &&
1214 version != sizeof(struct kld_file_stat))
1217 error = kern_kldstat(td, uap->fileid, &stat);
1220 return (copyout(&stat, uap->stat, version));
1224 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1231 error = mac_kld_check_stat(td->td_ucred);
1237 lf = linker_find_file_by_id(fileid);
1239 sx_xunlock(&kld_sx);
1243 /* Version 1 fields: */
1244 namelen = strlen(lf->filename) + 1;
1245 if (namelen > MAXPATHLEN)
1246 namelen = MAXPATHLEN;
1247 bcopy(lf->filename, &stat->name[0], namelen);
1248 stat->refs = lf->refs;
1250 stat->address = lf->address;
1251 stat->size = lf->size;
1252 /* Version 2 fields: */
1253 namelen = strlen(lf->pathname) + 1;
1254 if (namelen > MAXPATHLEN)
1255 namelen = MAXPATHLEN;
1256 bcopy(lf->pathname, &stat->pathname[0], namelen);
1257 sx_xunlock(&kld_sx);
1259 td->td_retval[0] = 0;
1264 DB_COMMAND(kldstat, db_kldstat)
1268 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
1269 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
1270 #undef POINTER_WIDTH
1271 TAILQ_FOREACH(lf, &linker_files, link) {
1274 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1275 lf->address, lf->size, lf->filename);
1281 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1288 error = mac_kld_check_stat(td->td_ucred);
1294 lf = linker_find_file_by_id(uap->fileid);
1297 mp = TAILQ_FIRST(&lf->modules);
1299 td->td_retval[0] = module_getid(mp);
1301 td->td_retval[0] = 0;
1305 sx_xunlock(&kld_sx);
1310 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1312 char *symstr = NULL;
1314 linker_symval_t symval;
1316 struct kld_sym_lookup lookup;
1320 error = mac_kld_check_stat(td->td_ucred);
1325 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1327 if (lookup.version != sizeof(lookup) ||
1328 uap->cmd != KLDSYM_LOOKUP)
1330 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1331 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1334 if (uap->fileid != 0) {
1335 lf = linker_find_file_by_id(uap->fileid);
1338 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1339 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1340 lookup.symvalue = (uintptr_t) symval.value;
1341 lookup.symsize = symval.size;
1342 error = copyout(&lookup, uap->data, sizeof(lookup));
1346 TAILQ_FOREACH(lf, &linker_files, link) {
1347 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1348 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1349 lookup.symvalue = (uintptr_t)symval.value;
1350 lookup.symsize = symval.size;
1351 error = copyout(&lookup, uap->data,
1359 sx_xunlock(&kld_sx);
1361 free(symstr, M_TEMP);
1366 * Preloaded module support
1370 modlist_lookup(const char *name, int ver)
1374 TAILQ_FOREACH(mod, &found_modules, link) {
1375 if (strcmp(mod->name, name) == 0 &&
1376 (ver == 0 || mod->version == ver))
1383 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1385 modlist_t mod, bestmod;
1388 if (verinfo == NULL)
1389 return (modlist_lookup(name, 0));
1391 TAILQ_FOREACH(mod, &found_modules, link) {
1392 if (strcmp(mod->name, name) != 0)
1395 if (ver == verinfo->md_ver_preferred)
1397 if (ver >= verinfo->md_ver_minimum &&
1398 ver <= verinfo->md_ver_maximum &&
1399 (bestmod == NULL || ver > bestmod->version))
1406 modlist_newmodule(const char *modname, int version, linker_file_t container)
1410 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1412 panic("no memory for module list");
1413 mod->container = container;
1414 mod->name = modname;
1415 mod->version = version;
1416 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1421 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1422 struct mod_metadata **stop, int preload)
1424 struct mod_metadata *mp, **mdp;
1425 const char *modname;
1428 for (mdp = start; mdp < stop; mdp++) {
1430 if (mp->md_type != MDT_VERSION)
1432 modname = mp->md_cval;
1433 ver = ((const struct mod_version *)mp->md_data)->mv_version;
1434 if (modlist_lookup(modname, ver) != NULL) {
1435 printf("module %s already present!\n", modname);
1436 /* XXX what can we do? this is a build error. :-( */
1439 modlist_newmodule(modname, ver, lf);
1444 linker_preload(void *arg)
1447 const char *modname, *nmodname;
1449 linker_file_t lf, nlf;
1452 linker_file_list_t loaded_files;
1453 linker_file_list_t depended_files;
1454 struct mod_metadata *mp, *nmp;
1455 struct mod_metadata **start, **stop, **mdp, **nmdp;
1456 const struct mod_depend *verinfo;
1460 struct sysinit **si_start, **si_stop;
1462 TAILQ_INIT(&loaded_files);
1463 TAILQ_INIT(&depended_files);
1464 TAILQ_INIT(&found_modules);
1469 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1470 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1471 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1472 if (modname == NULL) {
1473 printf("Preloaded module at %p does not have a"
1474 " name!\n", modptr);
1477 if (modtype == NULL) {
1478 printf("Preloaded module at %p does not have a type!\n",
1483 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1486 TAILQ_FOREACH(lc, &classes, link) {
1487 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1493 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1497 * First get a list of stuff in the kernel.
1499 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1501 linker_addmodules(linker_kernel_file, start, stop, 1);
1504 * This is a once-off kinky bubble sort to resolve relocation
1505 * dependency requirements.
1508 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1509 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1512 * First, look to see if we would successfully link with this
1515 resolves = 1; /* unless we know otherwise */
1517 for (mdp = start; mdp < stop; mdp++) {
1519 if (mp->md_type != MDT_DEPEND)
1521 modname = mp->md_cval;
1522 verinfo = mp->md_data;
1523 for (nmdp = start; nmdp < stop; nmdp++) {
1525 if (nmp->md_type != MDT_VERSION)
1527 nmodname = nmp->md_cval;
1528 if (strcmp(modname, nmodname) == 0)
1531 if (nmdp < stop) /* it's a self reference */
1535 * ok, the module isn't here yet, we
1538 if (modlist_lookup2(modname, verinfo) == NULL)
1543 * OK, if we found our modules, we can link. So, "provide"
1544 * the modules inside and add it to the end of the link order
1549 for (mdp = start; mdp < stop; mdp++) {
1551 if (mp->md_type != MDT_VERSION)
1553 modname = mp->md_cval;
1554 nver = ((const struct mod_version *)
1555 mp->md_data)->mv_version;
1556 if (modlist_lookup(modname,
1558 printf("module %s already"
1559 " present!\n", modname);
1560 TAILQ_REMOVE(&loaded_files,
1562 linker_file_unload(lf,
1563 LINKER_UNLOAD_FORCE);
1564 /* we changed tailq next ptr */
1567 modlist_newmodule(modname, nver, lf);
1570 TAILQ_REMOVE(&loaded_files, lf, loaded);
1571 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1573 * Since we provided modules, we need to restart the
1574 * sort so that the previous files that depend on us
1575 * have a chance. Also, we've busted the tailq next
1576 * pointer with the REMOVE.
1583 * At this point, we check to see what could not be resolved..
1585 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1586 TAILQ_REMOVE(&loaded_files, lf, loaded);
1587 printf("KLD file %s is missing dependencies\n", lf->filename);
1588 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1592 * We made it. Finish off the linking in the order we determined.
1594 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1595 if (linker_kernel_file) {
1596 linker_kernel_file->refs++;
1597 error = linker_file_add_dependency(lf,
1598 linker_kernel_file);
1600 panic("cannot add dependency");
1602 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1605 for (mdp = start; mdp < stop; mdp++) {
1607 if (mp->md_type != MDT_DEPEND)
1609 modname = mp->md_cval;
1610 verinfo = mp->md_data;
1611 mod = modlist_lookup2(modname, verinfo);
1613 printf("KLD file %s - cannot find "
1614 "dependency \"%s\"\n",
1615 lf->filename, modname);
1618 /* Don't count self-dependencies */
1619 if (lf == mod->container)
1621 mod->container->refs++;
1622 error = linker_file_add_dependency(lf,
1625 panic("cannot add dependency");
1629 * Now do relocation etc using the symbol search paths
1630 * established by the dependencies
1632 error = LINKER_LINK_PRELOAD_FINISH(lf);
1634 printf("KLD file %s - could not finalize loading\n",
1638 linker_file_register_modules(lf);
1639 if (!TAILQ_EMPTY(&lf->modules))
1640 lf->flags |= LINKER_FILE_MODULES;
1641 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1642 &si_stop, NULL) == 0)
1643 sysinit_add(si_start, si_stop);
1644 linker_file_register_sysctls(lf);
1645 lf->flags |= LINKER_FILE_LINKED;
1648 TAILQ_REMOVE(&depended_files, lf, loaded);
1649 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1651 sx_xunlock(&kld_sx);
1652 /* woohoo! we made it! */
1655 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
1658 * Handle preload files that failed to load any modules.
1661 linker_preload_finish(void *arg)
1663 linker_file_t lf, nlf;
1666 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1668 * If all of the modules in this file failed to load, unload
1669 * the file and return an error of ENOEXEC. (Parity with
1670 * linker_load_file.)
1672 if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1673 TAILQ_EMPTY(&lf->modules)) {
1674 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1678 lf->flags &= ~LINKER_FILE_MODULES;
1679 lf->userrefs++; /* so we can (try to) kldunload it */
1681 sx_xunlock(&kld_sx);
1685 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
1686 * scheduled at any subsystem and order, so run this as late as possible. init
1687 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1689 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1690 linker_preload_finish, 0);
1693 * Search for a not-loaded module by name.
1695 * Modules may be found in the following locations:
1697 * - preloaded (result is just the module name) - on disk (result is full path
1700 * If the module name is qualified in any way (contains path, etc.) the we
1701 * simply return a copy of it.
1703 * The search path can be manipulated via sysctl. Note that we use the ';'
1704 * character as a separator to be consistent with the bootloader.
1707 static char linker_hintfile[] = "linker.hints";
1708 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1710 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1711 sizeof(linker_path), "module load search path");
1713 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1715 static char *linker_ext_list[] = {
1722 * Check if file actually exists either with or without extension listed in
1723 * the linker_ext_list. (probably should be generic for the rest of the
1727 linker_lookup_file(const char *path, int pathlen, const char *name,
1728 int namelen, struct vattr *vap)
1730 struct nameidata nd;
1731 struct thread *td = curthread; /* XXX */
1732 char *result, **cpp, *sep;
1733 int error, len, extlen, reclen, flags;
1737 for (cpp = linker_ext_list; *cpp; cpp++) {
1742 extlen++; /* trailing '\0' */
1743 sep = (path[pathlen - 1] != '/') ? "/" : "";
1745 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1746 result = malloc(reclen, M_LINKER, M_WAITOK);
1747 for (cpp = linker_ext_list; *cpp; cpp++) {
1748 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1749 namelen, name, *cpp);
1751 * Attempt to open the file, and return the path if
1752 * we succeed and it's a regular file.
1754 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1756 error = vn_open(&nd, &flags, 0, NULL);
1758 NDFREE(&nd, NDF_ONLY_PNBUF);
1759 type = nd.ni_vp->v_type;
1761 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1762 VOP_UNLOCK(nd.ni_vp, 0);
1763 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1768 free(result, M_LINKER);
1772 #define INT_ALIGN(base, ptr) ptr = \
1773 (base) + roundup2((ptr) - (base), sizeof(int))
1776 * Lookup KLD which contains requested module in the "linker.hints" file. If
1777 * version specification is available, then try to find the best KLD.
1778 * Otherwise just find the latest one.
1781 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1782 int modnamelen, const struct mod_depend *verinfo)
1784 struct thread *td = curthread; /* XXX */
1785 struct ucred *cred = td ? td->td_ucred : NULL;
1786 struct nameidata nd;
1787 struct vattr vattr, mattr;
1788 u_char *hints = NULL;
1789 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1790 int error, ival, bestver, *intp, found, flags, clen, blen;
1794 bestver = found = 0;
1796 sep = (path[pathlen - 1] != '/') ? "/" : "";
1797 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1799 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1800 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1803 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1805 error = vn_open(&nd, &flags, 0, NULL);
1808 NDFREE(&nd, NDF_ONLY_PNBUF);
1809 if (nd.ni_vp->v_type != VREG)
1812 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1816 * XXX: we need to limit this number to some reasonable value
1818 if (vattr.va_size > LINKER_HINTS_MAX) {
1819 printf("hints file too large %ld\n", (long)vattr.va_size);
1822 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1823 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1824 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1827 VOP_UNLOCK(nd.ni_vp, 0);
1828 vn_close(nd.ni_vp, FREAD, cred, td);
1831 printf("can't read %zd\n", reclen);
1834 intp = (int *)hints;
1836 if (ival != LINKER_HINTS_VERSION) {
1837 printf("hints file version mismatch %d\n", ival);
1840 bufend = hints + vattr.va_size;
1841 recptr = (u_char *)intp;
1843 while (recptr < bufend && !found) {
1844 intp = (int *)recptr;
1851 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1854 INT_ALIGN(hints, cp);
1858 if (verinfo == NULL ||
1859 ival == verinfo->md_ver_preferred) {
1863 if (ival >= verinfo->md_ver_minimum &&
1864 ival <= verinfo->md_ver_maximum &&
1874 recptr += reclen + sizeof(int);
1877 * Finally check if KLD is in the place
1880 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1882 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1885 * KLD is newer than hints file. What we should do now?
1887 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1888 printf("warning: KLD '%s' is newer than the linker.hints"
1891 free(pathbuf, M_LINKER);
1893 free(hints, M_TEMP);
1894 if (nd.ni_vp != NULL) {
1895 VOP_UNLOCK(nd.ni_vp, 0);
1896 vn_close(nd.ni_vp, FREAD, cred, td);
1899 * If nothing found or hints is absent - fallback to the old
1900 * way by using "kldname[.ko]" as module name.
1902 if (!found && !bestver && result == NULL)
1903 result = linker_lookup_file(path, pathlen, modname,
1909 * Lookup KLD which contains requested module in the all directories.
1912 linker_search_module(const char *modname, int modnamelen,
1913 const struct mod_depend *verinfo)
1915 char *cp, *ep, *result;
1918 * traverse the linker path
1920 for (cp = linker_path; *cp; cp = ep + 1) {
1921 /* find the end of this component */
1922 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1923 result = linker_hints_lookup(cp, ep - cp, modname,
1924 modnamelen, verinfo);
1934 * Search for module in all directories listed in the linker_path.
1937 linker_search_kld(const char *name)
1939 char *cp, *ep, *result;
1942 /* qualified at all? */
1943 if (strchr(name, '/'))
1944 return (strdup(name, M_LINKER));
1946 /* traverse the linker path */
1948 for (ep = linker_path; *ep; ep++) {
1950 /* find the end of this component */
1951 for (; *ep != 0 && *ep != ';'; ep++);
1952 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1960 linker_basename(const char *path)
1962 const char *filename;
1964 filename = strrchr(path, '/');
1965 if (filename == NULL)
1974 * Inform hwpmc about the set of kernel modules currently loaded.
1977 linker_hwpmc_list_objects(void)
1980 struct pmckern_map_in *kobase;
1985 TAILQ_FOREACH(lf, &linker_files, link)
1988 /* Allocate nmappings + 1 entries. */
1989 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1990 M_LINKER, M_WAITOK | M_ZERO);
1992 TAILQ_FOREACH(lf, &linker_files, link) {
1994 /* Save the info for this linker file. */
1995 kobase[i].pm_file = lf->filename;
1996 kobase[i].pm_address = (uintptr_t)lf->address;
1999 sx_sunlock(&kld_sx);
2001 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2003 /* The last entry of the malloced area comprises of all zeros. */
2004 KASSERT(kobase[i].pm_file == NULL,
2005 ("linker_hwpmc_list_objects: last object not NULL"));
2007 return ((void *)kobase);
2012 * Find a file which contains given module and load it, if "parent" is not
2013 * NULL, register a reference to it.
2016 linker_load_module(const char *kldname, const char *modname,
2017 struct linker_file *parent, const struct mod_depend *verinfo,
2018 struct linker_file **lfpp)
2020 linker_file_t lfdep;
2021 const char *filename;
2025 sx_assert(&kld_sx, SA_XLOCKED);
2026 if (modname == NULL) {
2028 * We have to load KLD
2030 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2032 pathname = linker_search_kld(kldname);
2034 if (modlist_lookup2(modname, verinfo) != NULL)
2036 if (kldname != NULL)
2037 pathname = strdup(kldname, M_LINKER);
2038 else if (rootvnode == NULL)
2042 * Need to find a KLD with required module
2044 pathname = linker_search_module(modname,
2045 strlen(modname), verinfo);
2047 if (pathname == NULL)
2051 * Can't load more than one file with the same basename XXX:
2052 * Actually it should be possible to have multiple KLDs with
2053 * the same basename but different path because they can
2054 * provide different versions of the same modules.
2056 filename = linker_basename(pathname);
2057 if (linker_find_file_by_name(filename))
2060 error = linker_load_file(pathname, &lfdep);
2063 if (modname && verinfo &&
2064 modlist_lookup2(modname, verinfo) == NULL) {
2065 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2070 error = linker_file_add_dependency(parent, lfdep);
2077 free(pathname, M_LINKER);
2082 * This routine is responsible for finding dependencies of userland initiated
2083 * kldload(2)'s of files.
2086 linker_load_dependencies(linker_file_t lf)
2088 linker_file_t lfdep;
2089 struct mod_metadata **start, **stop, **mdp, **nmdp;
2090 struct mod_metadata *mp, *nmp;
2091 const struct mod_depend *verinfo;
2093 const char *modname, *nmodname;
2094 int ver, error = 0, count;
2097 * All files are dependent on /kernel.
2099 sx_assert(&kld_sx, SA_XLOCKED);
2100 if (linker_kernel_file) {
2101 linker_kernel_file->refs++;
2102 error = linker_file_add_dependency(lf, linker_kernel_file);
2106 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2109 for (mdp = start; mdp < stop; mdp++) {
2111 if (mp->md_type != MDT_VERSION)
2113 modname = mp->md_cval;
2114 ver = ((const struct mod_version *)mp->md_data)->mv_version;
2115 mod = modlist_lookup(modname, ver);
2117 printf("interface %s.%d already present in the KLD"
2118 " '%s'!\n", modname, ver,
2119 mod->container->filename);
2124 for (mdp = start; mdp < stop; mdp++) {
2126 if (mp->md_type != MDT_DEPEND)
2128 modname = mp->md_cval;
2129 verinfo = mp->md_data;
2131 for (nmdp = start; nmdp < stop; nmdp++) {
2133 if (nmp->md_type != MDT_VERSION)
2135 nmodname = nmp->md_cval;
2136 if (strcmp(modname, nmodname) == 0)
2139 if (nmdp < stop)/* early exit, it's a self reference */
2141 mod = modlist_lookup2(modname, verinfo);
2142 if (mod) { /* woohoo, it's loaded already */
2143 lfdep = mod->container;
2145 error = linker_file_add_dependency(lf, lfdep);
2150 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2152 printf("KLD %s: depends on %s - not available or"
2153 " version mismatch\n", lf->filename, modname);
2160 linker_addmodules(lf, start, stop, 0);
2165 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2167 struct sysctl_req *req;
2170 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2174 * Export a nul-separated, double-nul-terminated list of all function names
2178 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2184 error = mac_kld_check_stat(req->td->td_ucred);
2188 error = sysctl_wire_old_buffer(req, 0);
2192 TAILQ_FOREACH(lf, &linker_files, link) {
2193 error = LINKER_EACH_FUNCTION_NAME(lf,
2194 sysctl_kern_function_list_iterate, req);
2196 sx_xunlock(&kld_sx);
2200 sx_xunlock(&kld_sx);
2201 return (SYSCTL_OUT(req, "", 1));
2204 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
2205 NULL, 0, sysctl_kern_function_list, "", "kernel function list");