27 #include <sys/cdefs.h>
31 #include "opt_hwpmc_hooks.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
42 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/mount.h>
46 #include <sys/linker.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fcntl.h>
50 #include <sys/libkern.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
58 #include <security/mac/mac_framework.h>
60 #include "linker_if.h"
63 #include <sys/pmckern.h>
68 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RW,
69 &kld_debug, 0,
"Set various levels of KLD debug");
108 #define LINKER_GET_NEXT_FILE_ID(a) do { \
109 linker_file_t lftmp; \
112 sx_assert(&kld_sx, SA_XLOCKED); \
114 TAILQ_FOREACH(lftmp, &linker_files, link) { \
115 if (next_file_id == lftmp->id) { \
120 (a) = next_file_id; \
127 TAILQ_ENTRY(modlist) link;
128 linker_file_t container;
138 const char*
name,
int deps);
140 const char *modname,
struct linker_file *
parent,
141 struct mod_depend *verinfo,
struct linker_file **lfpp);
149 if ((result =
malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
158 sx_init(&
kld_sx,
"kernel linker");
186 ((kobj_class_t)lc)->refs++;
187 TAILQ_INSERT_TAIL(&
classes, lc, link);
196 KLD_DPF(FILE, (
"linker_file_sysinit: calling SYSINITs for %s\n",
199 sx_assert(&
kld_sx, SA_XLOCKED);
210 for (sipp = start; sipp <
stop; sipp++) {
211 for (xipp = sipp + 1; xipp <
stop; xipp++) {
212 if ((*sipp)->subsystem < (*xipp)->subsystem ||
213 ((*sipp)->subsystem == (*xipp)->subsystem &&
214 (*sipp)->order <= (*xipp)->order))
228 for (sipp = start; sipp <
stop; sipp++) {
229 if ((*sipp)->subsystem == SI_SUB_DUMMY)
233 (*((*sipp)->func)) ((*sipp)->udata);
244 KLD_DPF(FILE, (
"linker_file_sysuninit: calling SYSUNINITs for %s\n",
247 sx_assert(&
kld_sx, SA_XLOCKED);
260 for (sipp = start; sipp <
stop; sipp++) {
261 for (xipp = sipp + 1; xipp <
stop; xipp++) {
262 if ((*sipp)->subsystem > (*xipp)->subsystem ||
263 ((*sipp)->subsystem == (*xipp)->subsystem &&
264 (*sipp)->order >= (*xipp)->order))
278 for (sipp = start; sipp <
stop; sipp++) {
279 if ((*sipp)->subsystem == SI_SUB_DUMMY)
283 (*((*sipp)->func)) ((*sipp)->udata);
292 struct sysctl_oid **
start, **
stop, **oidp;
295 (
"linker_file_register_sysctls: registering SYSCTLs for %s\n",
298 sx_assert(&
kld_sx, SA_XLOCKED);
305 for (oidp = start; oidp <
stop; oidp++)
314 struct sysctl_oid **
start, **
stop, **oidp;
316 KLD_DPF(FILE, (
"linker_file_unregister_sysctls: registering SYSCTLs"
317 " for %s\n", lf->filename));
319 sx_assert(&
kld_sx, SA_XLOCKED);
326 for (oidp = start; oidp <
stop; oidp++)
335 struct mod_metadata **
start, **
stop, **mdp;
336 const moduledata_t *moddata;
337 int first_error, error;
339 KLD_DPF(FILE, (
"linker_file_register_modules: registering modules"
340 " in %s\n", lf->filename));
342 sx_assert(&
kld_sx, SA_XLOCKED);
352 start = SET_BEGIN(modmetadata_set);
353 stop = SET_LIMIT(modmetadata_set);
358 for (mdp = start; mdp <
stop; mdp++) {
359 if ((*mdp)->md_type != MDT_MODULE)
361 moddata = (*mdp)->md_data;
362 KLD_DPF(FILE, (
"Registering module %s in %s\n",
363 moddata->name, lf->filename));
366 printf(
"Module %s failed to register: %d\n",
367 moddata->name, error);
368 if (first_error == 0)
372 return (first_error);
392 int foundfile, error, modules;
395 if (
prison0.pr_securelevel > 0)
398 sx_assert(&
kld_sx, SA_XLOCKED);
401 KLD_DPF(FILE, (
"linker_load_file: file %s is already loaded,"
402 " incrementing refs\n", filename));
415 TAILQ_FOREACH(lc, &
classes, link) {
416 KLD_DPF(FILE, (
"linker_load_file: trying to load %s\n",
418 error = LINKER_LOAD_FILE(lc, filename, &lf);
427 if (error == EEXIST) {
431 modules = !TAILQ_EMPTY(&lf->modules);
434 lf->flags |= LINKER_FILE_LINKED;
441 if (modules && TAILQ_EMPTY(&lf->modules)) {
445 EVENTHANDLER_INVOKE(kld_load, lf);
461 printf(
"linker_load_file: Unsupported file type\n");
487 *result = mod->container;
507 KASSERT(modname != NULL,
508 (
"linker_release_module: no file or name"));
516 KASSERT(modname == NULL && verinfo == NULL,
517 (
"linker_release_module: both file and name"));
529 koname =
malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
530 sprintf(koname,
"%s.ko", filename);
532 sx_assert(&
kld_sx, SA_XLOCKED);
534 if (strcmp(lf->filename, koname) == 0)
536 if (strcmp(lf->filename, filename) == 0)
539 free(koname, M_LINKER);
548 sx_assert(&
kld_sx, SA_XLOCKED);
550 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
563 retval = predicate(lf, context);
578 sx_assert(&
kld_sx, SA_XLOCKED);
581 KLD_DPF(FILE, (
"linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
582 lf = (linker_file_t)
kobj_create((kobj_class_t)
lc, M_LINKER, M_WAITOK);
594 STAILQ_INIT(&lf->common);
595 TAILQ_INIT(&lf->modules);
605 struct common_symbol *cp;
609 if (
prison0.pr_securelevel > 0)
612 sx_assert(&
kld_sx, SA_XLOCKED);
613 KLD_DPF(FILE, (
"linker_file_unload: lf->refs=%d\n", file->refs));
616 if (file->refs > 1) {
623 EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
627 KLD_DPF(FILE, (
"linker_file_unload: file is unloading,"
628 " informing modules\n"));
634 for (mod = TAILQ_FIRST(&file->modules); mod;
638 if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
639 KLD_DPF(FILE, (
"linker_file_unload: module %s"
657 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
667 KLD_DPF(FILE, (
"linker_file_unload: module %s"
679 if (ml->container == file) {
689 if (file->flags & LINKER_FILE_LINKED) {
690 file->flags &= ~LINKER_FILE_LINKED;
697 for (i = 0; i < file->ndeps; i++)
699 free(file->deps, M_LINKER);
702 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
703 STAILQ_REMOVE_HEAD(&file->common, link);
709 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
712 if (file->filename) {
713 free(file->filename, M_LINKER);
714 file->filename = NULL;
716 if (file->pathname) {
717 free(file->pathname, M_LINKER);
718 file->pathname = NULL;
727 return (LINKER_CTF_GET(file, lc));
733 linker_file_t *newdeps;
735 sx_assert(&
kld_sx, SA_XLOCKED);
736 file->deps =
realloc(file->deps, (file->ndeps + 1) *
sizeof(*newdeps),
737 M_LINKER, M_WAITOK | M_ZERO);
738 file->deps[file->ndeps] = dep;
740 KLD_DPF(FILE, (
"linker_file_add_dependency:"
741 " adding %s as dependency for %s\n",
742 dep->filename, file->filename));
754 void *firstp,
void *lastp,
int *countp)
757 sx_assert(&
kld_sx, SA_LOCKED);
758 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
766 linker_function_nameval_callback_t callback_func,
void *arg)
768 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
777 locked = sx_xlocked(&
kld_sx);
791 linker_symval_t symval;
793 size_t common_size = 0;
796 sx_assert(&
kld_sx, SA_XLOCKED);
797 KLD_DPF(SYM, (
"linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
800 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
801 LINKER_SYMBOL_VALUES(file, sym, &symval);
802 if (symval.value == 0)
808 common_size = symval.size;
810 KLD_DPF(SYM, (
"linker_file_lookup_symbol: symbol"
811 ".value=%p\n", symval.value));
812 return (symval.value);
816 for (i = 0; i < file->ndeps; i++) {
818 file->deps[i], name, 0);
820 KLD_DPF(SYM, (
"linker_file_lookup_symbol:"
821 " deps value=%p\n", address));
826 if (common_size > 0) {
832 struct common_symbol *cp;
834 STAILQ_FOREACH(cp, &file->common, link) {
835 if (strcmp(cp->name, name) == 0) {
836 KLD_DPF(SYM, (
"linker_file_lookup_symbol:"
837 " old common value=%p\n", cp->address));
838 return (cp->address);
844 common_size = (common_size +
sizeof(int) - 1) & -
sizeof(int);
845 cp =
malloc(
sizeof(
struct common_symbol)
846 + common_size + strlen(name) + 1, M_LINKER,
848 cp->address = (caddr_t)(cp + 1);
849 cp->name = cp->address + common_size;
850 strcpy(cp->name, name);
851 bzero(cp->address, common_size);
852 STAILQ_INSERT_TAIL(&file->common, cp, link);
854 KLD_DPF(SYM, (
"linker_file_lookup_symbol: new common"
855 " value=%p\n", cp->address));
856 return (cp->address);
858 KLD_DPF(SYM, (
"linker_file_lookup_symbol: fail\n"));
873 linker_debug_lookup(
const char *symstr, c_linker_sym_t *
sym)
878 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
889 c_linker_sym_t best, es;
890 u_long diff, bestdiff, off;
893 off = (uintptr_t)value;
896 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
898 if (es != 0 && diff < bestdiff) {
922 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
932 linker_symval_t symval;
943 strlcpy(buf, symval.name, buflen);
957 linker_ddb_lookup(
const char *symstr, c_linker_sym_t *sym)
960 return (linker_debug_lookup(symstr, sym));
1008 const char *kldname, *modname;
1015 if ((error =
priv_check(td, PRIV_KLD_LOAD)) != 0)
1022 CURVNET_SET(TD_TO_VNET(td));
1029 if (index(file,
'/') || index(file,
'.')) {
1056 char *pathname = NULL;
1059 td->td_retval[0] = -1;
1061 pathname =
malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1062 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1066 td->td_retval[0] = fileid;
1068 free(pathname, M_TEMP);
1081 if ((error =
priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1084 CURVNET_SET(TD_TO_VNET(td));
1088 KLD_DPF(FILE, (
"kldunload: lf->userrefs=%d\n", lf->userrefs));
1090 if (lf->userrefs == 0) {
1094 printf(
"kldunload: attempt to unload file that was"
1095 " loaded by the kernel\n");
1122 if (uap->flags != LINKER_UNLOAD_NORMAL &&
1123 uap->flags != LINKER_UNLOAD_FORCE)
1137 error = mac_kld_check_stat(td->td_ucred);
1142 td->td_retval[0] = -1;
1144 pathname =
malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1145 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1152 td->td_retval[0] = lf->id;
1157 free(pathname, M_TEMP);
1168 error = mac_kld_check_stat(td->td_ucred);
1174 if (uap->fileid == 0)
1182 lf = TAILQ_NEXT(lf, link);
1186 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1187 lf = TAILQ_NEXT(lf, link);
1190 td->td_retval[0] = lf->id;
1192 td->td_retval[0] = 0;
1201 struct kld_file_stat stat;
1207 if ((error = copyin(&uap->stat->version, &version,
sizeof(version)))
1210 if (version !=
sizeof(
struct kld_file_stat_1) &&
1211 version !=
sizeof(
struct kld_file_stat))
1217 return (copyout(&stat, uap->stat, version));
1228 error = mac_kld_check_stat(td->td_ucred);
1241 namelen = strlen(lf->filename) + 1;
1242 if (namelen > MAXPATHLEN)
1243 namelen = MAXPATHLEN;
1244 bcopy(lf->filename, &stat->name[0], namelen);
1245 stat->refs = lf->refs;
1247 stat->address = lf->address;
1248 stat->size = lf->size;
1250 namelen = strlen(lf->pathname) + 1;
1251 if (namelen > MAXPATHLEN)
1252 namelen = MAXPATHLEN;
1253 bcopy(lf->pathname, &stat->pathname[0], namelen);
1256 td->td_retval[0] = 0;
1268 error = mac_kld_check_stat(td->td_ucred);
1277 mp = TAILQ_FIRST(&lf->modules);
1281 td->td_retval[0] = 0;
1292 char *symstr = NULL;
1294 linker_symval_t symval;
1296 struct kld_sym_lookup lookup;
1300 error = mac_kld_check_stat(td->td_ucred);
1305 if ((error = copyin(uap->data, &lookup,
sizeof(lookup))) != 0)
1307 if (lookup.version !=
sizeof(lookup) ||
1308 uap->cmd != KLDSYM_LOOKUP)
1310 symstr =
malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1311 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1314 if (uap->fileid != 0) {
1318 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1319 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1320 lookup.symvalue = (uintptr_t) symval.value;
1321 lookup.symsize = symval.size;
1322 error = copyout(&lookup, uap->data,
sizeof(lookup));
1327 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1328 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1329 lookup.symvalue = (uintptr_t)symval.value;
1330 lookup.symsize = symval.size;
1331 error = copyout(&lookup, uap->data,
1341 free(symstr, M_TEMP);
1355 if (strcmp(mod->name, name) == 0 &&
1356 (ver == 0 || mod->version == ver))
1368 if (verinfo == NULL)
1372 if (strcmp(mod->name, name) != 0)
1375 if (ver == verinfo->md_ver_preferred)
1377 if (ver >= verinfo->md_ver_minimum &&
1378 ver <= verinfo->md_ver_maximum &&
1379 (bestmod == NULL || ver > bestmod->version))
1390 mod =
malloc(
sizeof(
struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1392 panic(
"no memory for module list");
1393 mod->container = container;
1394 mod->name = modname;
1395 mod->version = version;
1402 struct mod_metadata **
stop,
int preload)
1404 struct mod_metadata *mp, **mdp;
1405 const char *modname;
1408 for (mdp = start; mdp <
stop; mdp++) {
1410 if (mp->md_type != MDT_VERSION)
1412 modname = mp->md_cval;
1413 ver = ((
struct mod_version *)mp->md_data)->mv_version;
1415 printf(
"module %s already present!\n", modname);
1427 const char *modname, *nmodname;
1429 linker_file_t lf, nlf;
1432 linker_file_list_t loaded_files;
1433 linker_file_list_t depended_files;
1434 struct mod_metadata *mp, *nmp;
1435 struct mod_metadata **
start, **
stop, **mdp, **nmdp;
1436 struct mod_depend *verinfo;
1440 struct sysinit **si_start, **si_stop;
1442 TAILQ_INIT(&loaded_files);
1443 TAILQ_INIT(&depended_files);
1452 if (modname == NULL) {
1453 printf(
"Preloaded module at %p does not have a"
1454 " name!\n", modptr);
1457 if (modtype == NULL) {
1458 printf(
"Preloaded module at %p does not have a type!\n",
1463 printf(
"Preloaded %s \"%s\" at %p.\n", modtype, modname,
1466 TAILQ_FOREACH(lc, &
classes, link) {
1467 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1473 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1488 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1497 for (mdp = start; mdp <
stop; mdp++) {
1499 if (mp->md_type != MDT_DEPEND)
1501 modname = mp->md_cval;
1502 verinfo = mp->md_data;
1503 for (nmdp = start; nmdp <
stop; nmdp++) {
1505 if (nmp->md_type != MDT_VERSION)
1507 nmodname = nmp->md_cval;
1508 if (strcmp(modname, nmodname) == 0)
1529 for (mdp = start; mdp <
stop; mdp++) {
1531 if (mp->md_type != MDT_VERSION)
1533 modname = mp->md_cval;
1534 nver = ((
struct mod_version *)
1535 mp->md_data)->mv_version;
1538 printf(
"module %s already"
1539 " present!\n", modname);
1540 TAILQ_REMOVE(&loaded_files,
1543 LINKER_UNLOAD_FORCE);
1550 TAILQ_REMOVE(&loaded_files, lf, loaded);
1551 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1565 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1566 TAILQ_REMOVE(&loaded_files, lf, loaded);
1567 printf(
"KLD file %s is missing dependencies\n", lf->filename);
1574 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1580 panic(
"cannot add dependency");
1586 for (mdp = start; mdp <
stop; mdp++) {
1588 if (mp->md_type != MDT_DEPEND)
1590 modname = mp->md_cval;
1591 verinfo = mp->md_data;
1594 printf(
"KLD file %s - cannot find "
1595 "dependency \"%s\"\n",
1596 lf->filename, modname);
1600 if (lf == mod->container)
1602 mod->container->refs++;
1606 panic(
"cannot add dependency");
1613 error = LINKER_LINK_PRELOAD_FINISH(lf);
1615 printf(
"KLD file %s - could not finalize loading\n",
1621 &si_stop, NULL) == 0)
1624 lf->flags |= LINKER_FILE_LINKED;
1627 TAILQ_REMOVE(&depended_files, lf, loaded);
1654 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1655 sizeof(linker_path),
"module load search path");
1657 TUNABLE_STR(
"module_path", linker_path,
sizeof(linker_path));
1672 int namelen,
struct vattr *vap)
1674 struct nameidata nd;
1675 struct thread *td = curthread;
1676 char *
result, **cpp, *sep;
1677 int error, len, extlen, reclen, flags, vfslocked;
1681 for (cpp = linker_ext_list; *cpp; cpp++) {
1687 sep = (path[pathlen - 1] !=
'/') ?
"/" :
"";
1689 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1690 result =
malloc(reclen, M_LINKER, M_WAITOK);
1691 for (cpp = linker_ext_list; *cpp; cpp++) {
1692 snprintf(result, reclen,
"%.*s%s%.*s%s", pathlen, path, sep,
1693 namelen, name, *cpp);
1698 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, result, td);
1700 error =
vn_open(&nd, &flags, 0, NULL);
1702 vfslocked = NDHASGIANT(&nd);
1703 NDFREE(&nd, NDF_ONLY_PNBUF);
1704 type = nd.ni_vp->v_type;
1706 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1707 VOP_UNLOCK(nd.ni_vp, 0);
1708 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1709 VFS_UNLOCK_GIANT(vfslocked);
1714 free(result, M_LINKER);
1718 #define INT_ALIGN(base, ptr) ptr = \
1719 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1728 int modnamelen,
struct mod_depend *verinfo)
1730 struct thread *td = curthread;
1731 struct ucred *cred = td ? td->td_ucred : NULL;
1732 struct nameidata nd;
1733 struct vattr vattr, mattr;
1734 u_char *hints = NULL;
1735 u_char *cp, *recptr, *bufend, *
result, *best, *pathbuf, *sep;
1736 int error, ival, bestver, *intp, found, flags, clen, blen;
1741 bestver = found = 0;
1743 sep = (path[pathlen - 1] !=
'/') ?
"/" :
"";
1744 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1746 pathbuf =
malloc(reclen, M_LINKER, M_WAITOK);
1747 snprintf(pathbuf, reclen,
"%.*s%s%s", pathlen, path, sep,
1750 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, pathbuf, td);
1752 error =
vn_open(&nd, &flags, 0, NULL);
1755 vfslocked = NDHASGIANT(&nd);
1756 NDFREE(&nd, NDF_ONLY_PNBUF);
1757 if (nd.ni_vp->v_type != VREG)
1760 error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
1766 if (vattr.va_size > 100 * 1024) {
1767 printf(
"hints file too large %ld\n", (
long)vattr.va_size);
1770 hints =
malloc(vattr.va_size, M_TEMP, M_WAITOK);
1773 error =
vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1774 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1777 VOP_UNLOCK(nd.ni_vp, 0);
1778 vn_close(nd.ni_vp, FREAD, cred, td);
1779 VFS_UNLOCK_GIANT(vfslocked);
1782 printf(
"can't read %zd\n", reclen);
1785 intp = (
int *)hints;
1787 if (ival != LINKER_HINTS_VERSION) {
1788 printf(
"hints file version mismatch %d\n", ival);
1791 bufend = hints + vattr.va_size;
1792 recptr = (u_char *)intp;
1794 while (recptr < bufend && !found) {
1795 intp = (
int *)recptr;
1802 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1809 if (verinfo == NULL ||
1810 ival == verinfo->md_ver_preferred) {
1814 if (ival >= verinfo->md_ver_minimum &&
1815 ival <= verinfo->md_ver_maximum &&
1825 recptr += reclen +
sizeof(int);
1838 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1839 printf(
"warning: KLD '%s' is newer than the linker.hints"
1842 free(pathbuf, M_LINKER);
1844 free(hints, M_TEMP);
1845 if (nd.ni_vp != NULL) {
1846 VOP_UNLOCK(nd.ni_vp, 0);
1847 vn_close(nd.ni_vp, FREAD, cred, td);
1848 VFS_UNLOCK_GIANT(vfslocked);
1854 if (!found && !bestver && result == NULL)
1865 struct mod_depend *verinfo)
1872 for (cp = linker_path; *cp; cp = ep + 1) {
1874 for (ep = cp; (*ep != 0) && (*ep !=
';'); ep++);
1876 modnamelen, verinfo);
1895 if (index(name,
'/'))
1900 for (ep = linker_path; *ep; ep++) {
1903 for (; *ep != 0 && *ep !=
';'; ep++);
1916 filename = rindex(path,
'/');
1917 if (filename == NULL)
1929 linker_hwpmc_list_objects(
void)
1932 struct pmckern_map_in *kobase;
1941 kobase =
malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
1942 M_LINKER, M_WAITOK | M_ZERO);
1947 kobase[i].pm_file = lf->filename;
1948 kobase[i].pm_address = (uintptr_t)lf->address;
1953 KASSERT(i > 0, (
"linker_hpwmc_list_objects: no kernel objects?"));
1956 KASSERT(kobase[i].pm_file == NULL,
1957 (
"linker_hwpmc_list_objects: last object not NULL"));
1959 return ((
void *)kobase);
1969 struct linker_file *
parent,
struct mod_depend *verinfo,
1970 struct linker_file **lfpp)
1972 linker_file_t lfdep;
1977 sx_assert(&
kld_sx, SA_XLOCKED);
1978 if (modname == NULL) {
1982 KASSERT(verinfo == NULL, (
"linker_load_module: verinfo"
1988 if (kldname != NULL)
1997 strlen(modname), verinfo);
1999 if (pathname == NULL)
2015 if (modname && verinfo &&
2029 free(pathname, M_LINKER);
2040 linker_file_t lfdep;
2041 struct mod_metadata **
start, **
stop, **mdp, **nmdp;
2042 struct mod_metadata *mp, *nmp;
2043 struct mod_depend *verinfo;
2045 const char *modname, *nmodname;
2046 int ver, error = 0,
count;
2051 sx_assert(&
kld_sx, SA_XLOCKED);
2061 for (mdp = start; mdp <
stop; mdp++) {
2063 if (mp->md_type != MDT_VERSION)
2065 modname = mp->md_cval;
2066 ver = ((
struct mod_version *)mp->md_data)->mv_version;
2069 printf(
"interface %s.%d already present in the KLD"
2070 " '%s'!\n", modname, ver,
2071 mod->container->filename);
2076 for (mdp = start; mdp <
stop; mdp++) {
2078 if (mp->md_type != MDT_DEPEND)
2080 modname = mp->md_cval;
2081 verinfo = mp->md_data;
2083 for (nmdp = start; nmdp <
stop; nmdp++) {
2085 if (nmp->md_type != MDT_VERSION)
2087 nmodname = nmp->md_cval;
2088 if (strcmp(modname, nmodname) == 0)
2095 lfdep = mod->container;
2104 printf(
"KLD %s: depends on %s - not available or"
2105 " version mismatch\n", lf->filename, modname);
2119 struct sysctl_req *req;
2122 return (SYSCTL_OUT(req, name, strlen(name) + 1));
2136 error = mac_kld_check_stat(req->td->td_ucred);
2145 error = LINKER_EACH_FUNCTION_NAME(lf,
2153 return (SYSCTL_OUT(req,
"", 1));
2156 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
int linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
static char * linker_search_module(const char *modname, int modnamelen, struct mod_depend *verinfo)
void * realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
static void linker_stop_class_add(void *arg)
void NDFREE(struct nameidata *ndp, const u_int flags)
int snprintf(char *str, size_t size, const char *format,...)
static modlist_t modlist_lookup2(const char *name, struct mod_depend *verinfo)
static void linker_file_sysinit(linker_file_t lf)
int kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
static char * linker_ext_list[]
int linker_file_foreach(linker_predicate_t *predicate, void *context)
static int linker_no_more_classes
int module_getid(module_t mod)
int sprintf(char *buf, const char *cfmt,...)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
static modlisthead_t found_modules
SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, sizeof(linker_path),"module load search path")
static int linker_load_file(const char *filename, linker_file_t *result)
void sysctl_register_oid(struct sysctl_oid *oidp)
void panic(const char *fmt,...)
int sys_kldunload(struct thread *td, struct kldunload_args *uap)
int module_unload(module_t mod)
static char * linker_lookup_file(const char *path, int pathlen, const char *name, int namelen, struct vattr *vap)
static char linker_path[MAXPATHLEN]
SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD,&boothowto, 0,"Boot control flags, passed from loader")
SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
#define LINKER_GET_NEXT_FILE_ID(a)
int module_quiesce(module_t mod)
static linker_file_t linker_find_file_by_name(const char *_filename)
int linker_file_lookup_set(linker_file_t file, const char *name, void *firstp, void *lastp, int *countp)
MALLOC_DEFINE(M_LINKER,"linker","kernel linker")
caddr_t preload_search_next_name(caddr_t base)
int priv_check(struct thread *td, int priv)
static char linker_hintfile[]
static char * linker_hints_lookup(const char *path, int pathlen, const char *modname, int modnamelen, struct mod_depend *verinfo)
static modlist_t modlist_newmodule(const char *modname, int version, linker_file_t container)
int linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
static int linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
module_t module_getfnext(module_t mod)
int sys_kldsym(struct thread *td, struct kldsym_args *uap)
void kobj_delete(kobj_t obj, struct malloc_type *mtype)
static linker_class_list_t classes
static void linker_init(void *arg)
static linker_file_t linker_find_file_by_id(int _fileid)
struct sysinit ** sysinit
SET_DECLARE(modmetadata_set, struct mod_metadata)
static int linker_load_module(const char *kldname, const char *modname, struct linker_file *parent, struct mod_depend *verinfo, struct linker_file **lfpp)
int module_register(const moduledata_t *data, linker_file_t container)
int vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred, struct ucred *file_cred, ssize_t *aresid, struct thread *td)
int linker_file_function_listall(linker_file_t lf, linker_function_nameval_callback_t callback_func, void *arg)
static linker_file_list_t linker_files
static int linker_file_register_modules(linker_file_t lf)
int sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
static char * linker_search_kld(const char *name)
caddr_t linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
static int linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen, long *offset)
void free(void *addr, struct malloc_type *mtp)
static int linker_file_add_dependency(linker_file_t file, linker_file_t dep)
int vn_close(struct vnode *vp, int flags, struct ucred *file_cred, struct thread *td)
int printf(const char *fmt,...)
int linker_reference_module(const char *modname, struct mod_depend *verinfo, linker_file_t *result)
int sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
static char * linker_strdup(const char *str)
int kern_kldload(struct thread *td, const char *file, int *fileid)
SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0, sysctl_kern_function_list,"","kernel function list")
const char * module_getname(module_t mod)
caddr_t preload_search_info(caddr_t mod, int inf)
static void linker_preload(void *arg)
int linker_add_class(linker_class_t lc)
int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
int linker_file_unload(linker_file_t file, int flags)
int linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
linker_file_t linker_make_file(const char *pathname, linker_class_t lc)
kobj_t kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags)
static void linker_file_register_sysctls(linker_file_t lf)
static int sysctl_kern_function_list_iterate(const char *name, void *opaque)
int linker_release_module(const char *modname, struct mod_depend *verinfo, linker_file_t lf)
static void linker_init_kernel_modules(void)
void module_release(module_t mod)
linker_file_t linker_kernel_file
TUNABLE_STR("module_path", linker_path, sizeof(linker_path))
void sysctl_unregister_oid(struct sysctl_oid *oidp)
#define INT_ALIGN(base, ptr)
static caddr_t linker_file_lookup_symbol_internal(linker_file_t file, const char *name, int deps)
void kobj_class_compile(kobj_class_t cls)
static void linker_file_sysuninit(linker_file_t lf)
void sysinit_add(struct sysinit **set, struct sysinit **set_end)
struct modlist * modlist_t
int securelevel_gt(struct ucred *cr, int level)
int linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, long *offset)
int linker_load_dependencies(linker_file_t lf)
int vn_open(struct nameidata *ndp, int *flagp, int cmode, struct file *fp)
static void linker_addmodules(linker_file_t lf, struct mod_metadata **start, struct mod_metadata **stop, int preload)
int kern_kldunload(struct thread *td, int fileid, int flags)
static int linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
int sys_kldstat(struct thread *td, struct kldstat_args *uap)
static const char * linker_basename(const char *path)
static int sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
static modlist_t modlist_lookup(const char *name, int ver)
int sys_kldnext(struct thread *td, struct kldnext_args *uap)
int linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, long *offset)
int sys_kldfind(struct thread *td, struct kldfind_args *uap)
typedef TAILQ_HEAD(modlist)
int sys_kldload(struct thread *td, struct kldload_args *uap)
static void linker_file_unregister_sysctls(linker_file_t lf)