1997-05-07 16:05:47 +00:00
|
|
|
/*-
|
2000-04-24 17:08:04 +00:00
|
|
|
* Copyright (c) 1997-2000 Doug Rabson
|
1997-05-07 16:05:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
#include "opt_ddb.h"
|
2011-11-06 08:10:41 +00:00
|
|
|
#include "opt_kld.h"
|
2006-03-26 12:20:54 +00:00
|
|
|
#include "opt_hwpmc_hooks.h"
|
1998-10-10 00:07:53 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/sysent.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
2001-09-01 19:04:37 +00:00
|
|
|
#include <sys/mutex.h>
|
2002-03-18 07:45:30 +00:00
|
|
|
#include <sys/sx.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/module.h>
|
2006-06-20 21:31:38 +00:00
|
|
|
#include <sys/mount.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
#include <sys/linker.h>
|
2013-08-13 03:07:49 +00:00
|
|
|
#include <sys/eventhandler.h>
|
1998-10-10 00:07:53 +00:00
|
|
|
#include <sys/fcntl.h>
|
2009-05-27 14:11:23 +00:00
|
|
|
#include <sys/jail.h>
|
1998-10-10 00:07:53 +00:00
|
|
|
#include <sys/libkern.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
2006-06-13 21:28:18 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
1998-10-10 00:07:53 +00:00
|
|
|
#include <sys/sysctl.h>
|
2009-08-01 19:26:27 +00:00
|
|
|
|
2016-06-09 18:27:41 +00:00
|
|
|
#ifdef DDB
|
|
|
|
#include <ddb/ddb.h>
|
|
|
|
#endif
|
|
|
|
|
2009-08-01 19:26:27 +00:00
|
|
|
#include <net/vnet.h>
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2006-10-22 11:52:19 +00:00
|
|
|
#include <security/mac/mac_framework.h>
|
|
|
|
|
2000-04-24 17:08:04 +00:00
|
|
|
#include "linker_if.h"
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
#ifdef HWPMC_HOOKS
|
|
|
|
#include <sys/pmckern.h>
|
|
|
|
#endif
|
|
|
|
|
1998-11-06 15:10:17 +00:00
|
|
|
#ifdef KLD_DEBUG
|
|
|
|
int kld_debug = 0;
|
2014-06-28 03:56:17 +00:00
|
|
|
SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
|
2012-03-15 06:01:21 +00:00
|
|
|
&kld_debug, 0, "Set various levels of KLD debug");
|
1998-11-06 15:10:17 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-12 22:00:59 +00:00
|
|
|
/* These variables are used by kernel debuggers to enumerate loaded files. */
|
|
|
|
const int kld_off_address = offsetof(struct linker_file, address);
|
|
|
|
const int kld_off_filename = offsetof(struct linker_file, filename);
|
|
|
|
const int kld_off_pathname = offsetof(struct linker_file, pathname);
|
|
|
|
const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* static char *linker_search_path(const char *name, struct mod_depend
|
|
|
|
* *verinfo);
|
|
|
|
*/
|
|
|
|
static const char *linker_basename(const char *path);
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2006-06-20 20:41:15 +00:00
|
|
|
/*
|
|
|
|
* Find a currently loaded file given its filename.
|
|
|
|
*/
|
|
|
|
static linker_file_t linker_find_file_by_name(const char* _filename);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a currently loaded file given its file id.
|
|
|
|
*/
|
|
|
|
static linker_file_t linker_find_file_by_id(int _fileid);
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/* Metadata from the static kernel */
|
|
|
|
SET_DECLARE(modmetadata_set, struct mod_metadata);
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
|
|
|
|
|
1998-11-04 15:20:58 +00:00
|
|
|
linker_file_t linker_kernel_file;
|
1997-11-20 20:07:59 +00:00
|
|
|
|
2006-06-21 20:42:08 +00:00
|
|
|
static struct sx kld_sx; /* kernel linker lock */
|
2002-06-19 21:25:59 +00:00
|
|
|
|
2007-10-22 04:12:57 +00:00
|
|
|
/*
|
|
|
|
* Load counter used by clients to determine if a linker file has been
|
|
|
|
* re-loaded. This counter is incremented for each file load.
|
|
|
|
*/
|
|
|
|
static int loadcnt;
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
static linker_class_list_t classes;
|
1999-08-20 00:18:07 +00:00
|
|
|
static linker_file_list_t linker_files;
|
1997-05-07 16:05:47 +00:00
|
|
|
static int next_file_id = 1;
|
2002-06-19 21:25:59 +00:00
|
|
|
static int linker_no_more_classes = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2001-11-18 18:19:35 +00:00
|
|
|
#define LINKER_GET_NEXT_FILE_ID(a) do { \
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lftmp; \
|
2001-11-18 18:19:35 +00:00
|
|
|
\
|
2013-08-24 21:07:04 +00:00
|
|
|
if (!cold) \
|
|
|
|
sx_assert(&kld_sx, SA_XLOCKED); \
|
2001-11-18 18:19:35 +00:00
|
|
|
retry: \
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lftmp, &linker_files, link) { \
|
|
|
|
if (next_file_id == lftmp->id) { \
|
|
|
|
next_file_id++; \
|
|
|
|
goto retry; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
(a) = next_file_id; \
|
2001-11-18 18:19:35 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/* XXX wrong name; we're looking at version provision tags here, not modules */
|
2000-05-26 02:09:24 +00:00
|
|
|
typedef TAILQ_HEAD(, modlist) modlisthead_t;
|
2000-04-29 13:19:31 +00:00
|
|
|
struct modlist {
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_ENTRY(modlist) link; /* chain together all modules */
|
|
|
|
linker_file_t container;
|
|
|
|
const char *name;
|
|
|
|
int version;
|
2000-04-29 13:19:31 +00:00
|
|
|
};
|
2002-02-22 04:14:49 +00:00
|
|
|
typedef struct modlist *modlist_t;
|
|
|
|
static modlisthead_t found_modules;
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2006-06-20 20:18:42 +00:00
|
|
|
static int linker_file_add_dependency(linker_file_t file,
|
|
|
|
linker_file_t dep);
|
2006-06-21 20:42:08 +00:00
|
|
|
static caddr_t linker_file_lookup_symbol_internal(linker_file_t file,
|
|
|
|
const char* name, int deps);
|
2006-06-20 20:18:42 +00:00
|
|
|
static int linker_load_module(const char *kldname,
|
|
|
|
const char *modname, struct linker_file *parent,
|
2014-12-03 22:14:13 +00:00
|
|
|
const struct mod_depend *verinfo, struct linker_file **lfpp);
|
|
|
|
static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
|
2002-04-10 01:13:57 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
static void
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_init(void *arg)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2006-06-21 20:42:08 +00:00
|
|
|
sx_init(&kld_sx, "kernel linker");
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_INIT(&classes);
|
|
|
|
TAILQ_INIT(&linker_files);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 10:58:09 +00:00
|
|
|
SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-06-19 21:25:59 +00:00
|
|
|
static void
|
|
|
|
linker_stop_class_add(void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
linker_no_more_classes = 1;
|
|
|
|
}
|
|
|
|
|
2008-03-16 10:58:09 +00:00
|
|
|
SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
|
2002-06-19 21:25:59 +00:00
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2000-04-24 17:08:04 +00:00
|
|
|
linker_add_class(linker_class_t lc)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2002-06-19 21:25:59 +00:00
|
|
|
/*
|
2005-03-31 22:49:31 +00:00
|
|
|
* We disallow any class registration past SI_ORDER_ANY
|
|
|
|
* of SI_SUB_KLD. We bump the reference count to keep the
|
|
|
|
* ops from being freed.
|
2002-06-19 21:25:59 +00:00
|
|
|
*/
|
|
|
|
if (linker_no_more_classes == 1)
|
|
|
|
return (EPERM);
|
2002-02-22 04:14:49 +00:00
|
|
|
kobj_class_compile((kobj_class_t) lc);
|
2005-03-31 22:49:31 +00:00
|
|
|
((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_INSERT_TAIL(&classes, lc, link);
|
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_sysinit(linker_file_t lf)
|
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct sysinit **start, **stop, **sipp, **xipp, *save;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
|
|
|
|
lf->filename));
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Perform a bubble sort of the system initialization objects by
|
|
|
|
* their subsystem (primary key) and order (secondary key).
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Since some things care about execution order, this is the operation
|
|
|
|
* which ensures continued function.
|
|
|
|
*/
|
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
for (xipp = sipp + 1; xipp < stop; xipp++) {
|
|
|
|
if ((*sipp)->subsystem < (*xipp)->subsystem ||
|
|
|
|
((*sipp)->subsystem == (*xipp)->subsystem &&
|
|
|
|
(*sipp)->order <= (*xipp)->order))
|
|
|
|
continue; /* skip */
|
|
|
|
save = *sipp;
|
|
|
|
*sipp = *xipp;
|
|
|
|
*xipp = save;
|
|
|
|
}
|
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* Traverse the (now) ordered list of system initialization tasks.
|
|
|
|
* Perform each task, and continue on to the next task.
|
|
|
|
*/
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-07-06 21:39:39 +00:00
|
|
|
mtx_lock(&Giant);
|
2002-02-22 04:14:49 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
if ((*sipp)->subsystem == SI_SUB_DUMMY)
|
|
|
|
continue; /* skip dummy task(s) */
|
|
|
|
|
|
|
|
/* Call function */
|
|
|
|
(*((*sipp)->func)) ((*sipp)->udata);
|
|
|
|
}
|
2006-07-06 21:39:39 +00:00
|
|
|
mtx_unlock(&Giant);
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xlock(&kld_sx);
|
1998-11-10 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_sysuninit(linker_file_t lf)
|
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct sysinit **start, **stop, **sipp, **xipp, *save;
|
|
|
|
|
|
|
|
KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
|
2002-02-22 04:14:49 +00:00
|
|
|
NULL) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a reverse bubble sort of the system initialization objects
|
|
|
|
* by their subsystem (primary key) and order (secondary key).
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Since some things care about execution order, this is the operation
|
|
|
|
* which ensures continued function.
|
|
|
|
*/
|
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
for (xipp = sipp + 1; xipp < stop; xipp++) {
|
|
|
|
if ((*sipp)->subsystem > (*xipp)->subsystem ||
|
|
|
|
((*sipp)->subsystem == (*xipp)->subsystem &&
|
|
|
|
(*sipp)->order >= (*xipp)->order))
|
|
|
|
continue; /* skip */
|
|
|
|
save = *sipp;
|
|
|
|
*sipp = *xipp;
|
|
|
|
*xipp = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the (now) ordered list of system initialization tasks.
|
|
|
|
* Perform each task, and continue on to the next task.
|
|
|
|
*/
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-07-06 21:39:39 +00:00
|
|
|
mtx_lock(&Giant);
|
2002-02-22 04:14:49 +00:00
|
|
|
for (sipp = start; sipp < stop; sipp++) {
|
|
|
|
if ((*sipp)->subsystem == SI_SUB_DUMMY)
|
|
|
|
continue; /* skip dummy task(s) */
|
|
|
|
|
|
|
|
/* Call function */
|
|
|
|
(*((*sipp)->func)) ((*sipp)->udata);
|
1998-11-10 08:49:28 +00:00
|
|
|
}
|
2006-07-06 21:39:39 +00:00
|
|
|
mtx_unlock(&Giant);
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xlock(&kld_sx);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
1999-02-16 10:49:55 +00:00
|
|
|
static void
|
2017-10-05 12:32:14 +00:00
|
|
|
linker_file_register_sysctls(linker_file_t lf, bool enable)
|
1999-02-16 10:49:55 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct sysctl_oid **start, **stop, **oidp;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(FILE,
|
|
|
|
("linker_file_register_sysctls: registering SYSCTLs for %s\n",
|
|
|
|
lf->filename));
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2017-10-05 12:32:14 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sx_xunlock(&kld_sx);
|
|
|
|
sysctl_wlock();
|
|
|
|
for (oidp = start; oidp < stop; oidp++) {
|
|
|
|
if (enable)
|
|
|
|
sysctl_register_oid(*oidp);
|
|
|
|
else
|
|
|
|
sysctl_register_disabled_oid(*oidp);
|
|
|
|
}
|
|
|
|
sysctl_wunlock();
|
|
|
|
sx_xlock(&kld_sx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_enable_sysctls(linker_file_t lf)
|
|
|
|
{
|
|
|
|
struct sysctl_oid **start, **stop, **oidp;
|
|
|
|
|
|
|
|
KLD_DPF(FILE,
|
|
|
|
("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
|
|
|
|
lf->filename));
|
|
|
|
|
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
return;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wlock();
|
2002-02-22 04:14:49 +00:00
|
|
|
for (oidp = start; oidp < stop; oidp++)
|
2017-10-05 12:32:14 +00:00
|
|
|
sysctl_enable_oid(*oidp);
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wunlock();
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xlock(&kld_sx);
|
1999-02-16 10:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_file_unregister_sysctls(linker_file_t lf)
|
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct sysctl_oid **start, **stop, **oidp;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2012-03-22 09:29:07 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
|
2002-02-22 04:14:49 +00:00
|
|
|
" for %s\n", lf->filename));
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
return;
|
1999-02-16 10:49:55 +00:00
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wlock();
|
2002-02-22 04:14:49 +00:00
|
|
|
for (oidp = start; oidp < stop; oidp++)
|
|
|
|
sysctl_unregister_oid(*oidp);
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wunlock();
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_xlock(&kld_sx);
|
1999-02-16 10:49:55 +00:00
|
|
|
}
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static int
|
|
|
|
linker_file_register_modules(linker_file_t lf)
|
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct mod_metadata **start, **stop, **mdp;
|
|
|
|
const moduledata_t *moddata;
|
2005-05-28 23:20:05 +00:00
|
|
|
int first_error, error;
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
|
|
|
|
" in %s\n", lf->filename));
|
|
|
|
|
2013-08-24 21:08:55 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "modmetadata_set", &start,
|
2006-06-21 17:48:03 +00:00
|
|
|
&stop, NULL) != 0) {
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* This fallback should be unnecessary, but if we get booted
|
|
|
|
* from boot2 instead of loader and we are missing our
|
|
|
|
* metadata then we have to try the best we can.
|
|
|
|
*/
|
|
|
|
if (lf == linker_kernel_file) {
|
|
|
|
start = SET_BEGIN(modmetadata_set);
|
|
|
|
stop = SET_LIMIT(modmetadata_set);
|
|
|
|
} else
|
|
|
|
return (0);
|
2001-06-13 10:58:39 +00:00
|
|
|
}
|
2005-05-28 23:20:05 +00:00
|
|
|
first_error = 0;
|
2002-02-22 04:14:49 +00:00
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
|
|
|
if ((*mdp)->md_type != MDT_MODULE)
|
|
|
|
continue;
|
|
|
|
moddata = (*mdp)->md_data;
|
|
|
|
KLD_DPF(FILE, ("Registering module %s in %s\n",
|
|
|
|
moddata->name, lf->filename));
|
|
|
|
error = module_register(moddata, lf);
|
2005-05-28 22:29:44 +00:00
|
|
|
if (error) {
|
2002-02-22 13:43:56 +00:00
|
|
|
printf("Module %s failed to register: %d\n",
|
2002-02-22 04:14:49 +00:00
|
|
|
moddata->name, error);
|
2005-05-28 23:20:05 +00:00
|
|
|
if (first_error == 0)
|
|
|
|
first_error = error;
|
2005-05-28 22:29:44 +00:00
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2005-05-28 23:20:05 +00:00
|
|
|
return (first_error);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
linker_init_kernel_modules(void)
|
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_register_modules(linker_kernel_file);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 10:58:09 +00:00
|
|
|
SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
|
|
|
|
0);
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2002-08-02 20:56:07 +00:00
|
|
|
static int
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_load_file(const char *filename, linker_file_t *result)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_class_t lc;
|
|
|
|
linker_file_t lf;
|
2012-04-12 14:49:25 +00:00
|
|
|
int foundfile, error, modules;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
/* Refuse to load modules if securelevel raised */
|
2009-05-27 14:11:23 +00:00
|
|
|
if (prison0.pr_securelevel > 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
return (EPERM);
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
lf = linker_find_file_by_name(filename);
|
1997-05-07 16:05:47 +00:00
|
|
|
if (lf) {
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
|
|
|
|
" incrementing refs\n", filename));
|
|
|
|
*result = lf;
|
|
|
|
lf->refs++;
|
2006-06-13 20:27:23 +00:00
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
foundfile = 0;
|
2006-06-13 20:27:23 +00:00
|
|
|
error = 0;
|
2002-06-19 21:25:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not need to protect (lock) classes here because there is
|
|
|
|
* no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
|
|
|
|
* and there is no class deregistration mechanism at this time.
|
|
|
|
*/
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lc, &classes, link) {
|
|
|
|
KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
|
|
|
|
filename));
|
|
|
|
error = LINKER_LOAD_FILE(lc, filename, &lf);
|
|
|
|
/*
|
|
|
|
* If we got something other than ENOENT, then it exists but
|
|
|
|
* we cannot load it for some other reason.
|
|
|
|
*/
|
|
|
|
if (error != ENOENT)
|
|
|
|
foundfile = 1;
|
|
|
|
if (lf) {
|
2005-05-28 22:29:44 +00:00
|
|
|
error = linker_file_register_modules(lf);
|
|
|
|
if (error == EEXIST) {
|
|
|
|
linker_file_unload(lf, LINKER_UNLOAD_FORCE);
|
2006-06-13 20:27:23 +00:00
|
|
|
return (error);
|
2005-05-28 22:29:44 +00:00
|
|
|
}
|
2012-04-12 14:49:25 +00:00
|
|
|
modules = !TAILQ_EMPTY(&lf->modules);
|
2017-10-05 12:32:14 +00:00
|
|
|
linker_file_register_sysctls(lf, false);
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_sysinit(lf);
|
|
|
|
lf->flags |= LINKER_FILE_LINKED;
|
2012-04-12 14:49:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If all of the modules in this file failed
|
|
|
|
* to load, unload the file and return an
|
|
|
|
* error of ENOEXEC.
|
|
|
|
*/
|
|
|
|
if (modules && TAILQ_EMPTY(&lf->modules)) {
|
|
|
|
linker_file_unload(lf, LINKER_UNLOAD_FORCE);
|
|
|
|
return (ENOEXEC);
|
|
|
|
}
|
2017-10-05 12:32:14 +00:00
|
|
|
linker_file_enable_sysctls(lf);
|
2013-12-19 03:48:36 +00:00
|
|
|
EVENTHANDLER_INVOKE(kld_load, lf);
|
2002-02-22 04:14:49 +00:00
|
|
|
*result = lf;
|
2006-06-13 20:27:23 +00:00
|
|
|
return (0);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Less than ideal, but tells the user whether it failed to load or
|
|
|
|
* the module was not found.
|
|
|
|
*/
|
2002-10-17 17:28:57 +00:00
|
|
|
if (foundfile) {
|
2009-02-10 15:50:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file type has not been recognized by the last try
|
|
|
|
* printout a message before to fail.
|
|
|
|
*/
|
|
|
|
if (error == ENOSYS)
|
2017-03-09 00:56:07 +00:00
|
|
|
printf("%s: %s - unsupported file type\n",
|
|
|
|
__func__, filename);
|
2009-02-10 15:50:19 +00:00
|
|
|
|
2002-10-17 17:28:57 +00:00
|
|
|
/*
|
|
|
|
* Format not recognized or otherwise unloadable.
|
|
|
|
* When loading a module that is statically built into
|
|
|
|
* the kernel EEXIST percolates back up as the return
|
|
|
|
* value. Preserve this so that apps like sysinstall
|
|
|
|
* can recognize this special case and not post bogus
|
|
|
|
* dialog boxes.
|
|
|
|
*/
|
|
|
|
if (error != EEXIST)
|
|
|
|
error = ENOEXEC;
|
|
|
|
} else
|
2002-02-22 13:43:56 +00:00
|
|
|
error = ENOENT; /* Nothing found */
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-06-18 15:09:33 +00:00
|
|
|
int
|
2002-04-10 01:13:57 +00:00
|
|
|
linker_reference_module(const char *modname, struct mod_depend *verinfo,
|
|
|
|
linker_file_t *result)
|
2001-06-18 15:09:33 +00:00
|
|
|
{
|
2002-04-10 01:13:57 +00:00
|
|
|
modlist_t mod;
|
2006-06-20 20:54:13 +00:00
|
|
|
int error;
|
2002-04-10 01:13:57 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-04-10 01:13:57 +00:00
|
|
|
if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
|
|
|
|
*result = mod->container;
|
|
|
|
(*result)->refs++;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2002-04-10 01:13:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-06-20 20:54:13 +00:00
|
|
|
error = linker_load_module(NULL, modname, NULL, verinfo, result);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-20 20:54:13 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linker_release_module(const char *modname, struct mod_depend *verinfo,
|
|
|
|
linker_file_t lf)
|
|
|
|
{
|
|
|
|
modlist_t mod;
|
|
|
|
int error;
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2006-06-20 20:54:13 +00:00
|
|
|
if (lf == NULL) {
|
|
|
|
KASSERT(modname != NULL,
|
|
|
|
("linker_release_module: no file or name"));
|
|
|
|
mod = modlist_lookup2(modname, verinfo);
|
|
|
|
if (mod == NULL) {
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-20 20:54:13 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
lf = mod->container;
|
|
|
|
} else
|
|
|
|
KASSERT(modname == NULL && verinfo == NULL,
|
|
|
|
("linker_release_module: both file and name"));
|
|
|
|
error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-20 20:54:13 +00:00
|
|
|
return (error);
|
2001-06-18 15:09:33 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 20:41:15 +00:00
|
|
|
static linker_file_t
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_find_file_by_name(const char *filename)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2006-06-13 20:27:23 +00:00
|
|
|
linker_file_t lf;
|
2002-02-22 04:14:49 +00:00
|
|
|
char *koname;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
|
2002-02-22 04:14:49 +00:00
|
|
|
sprintf(koname, "%s.ko", filename);
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(lf->filename, koname) == 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(lf->filename, filename) == 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-06-13 20:27:23 +00:00
|
|
|
free(koname, M_LINKER);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (lf);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 20:41:15 +00:00
|
|
|
static linker_file_t
|
1997-05-07 16:05:47 +00:00
|
|
|
linker_find_file_by_id(int fileid)
|
|
|
|
{
|
2006-06-13 20:27:23 +00:00
|
|
|
linker_file_t lf;
|
2006-06-21 20:42:08 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link)
|
2007-02-23 19:46:59 +00:00
|
|
|
if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
|
|
|
return (lf);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 20:37:17 +00:00
|
|
|
int
|
|
|
|
linker_file_foreach(linker_predicate_t *predicate, void *context)
|
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int retval = 0;
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2006-06-20 20:37:17 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
retval = predicate(lf, context);
|
|
|
|
if (retval != 0)
|
|
|
|
break;
|
|
|
|
}
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-20 20:37:17 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
linker_file_t
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_make_file(const char *pathname, linker_class_t lc)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
const char *filename;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
if (!cold)
|
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
filename = linker_basename(pathname);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2007-10-22 04:12:57 +00:00
|
|
|
KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
|
2003-02-19 05:47:46 +00:00
|
|
|
lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (lf == NULL)
|
2006-06-13 20:27:23 +00:00
|
|
|
return (NULL);
|
2014-10-20 17:04:03 +00:00
|
|
|
lf->ctors_addr = 0;
|
|
|
|
lf->ctors_size = 0;
|
2002-02-22 04:14:49 +00:00
|
|
|
lf->refs = 1;
|
|
|
|
lf->userrefs = 0;
|
|
|
|
lf->flags = 0;
|
2013-08-16 03:41:41 +00:00
|
|
|
lf->filename = strdup(filename, M_LINKER);
|
|
|
|
lf->pathname = strdup(pathname, M_LINKER);
|
2002-02-22 04:14:49 +00:00
|
|
|
LINKER_GET_NEXT_FILE_ID(lf->id);
|
|
|
|
lf->ndeps = 0;
|
|
|
|
lf->deps = NULL;
|
2007-10-22 04:12:57 +00:00
|
|
|
lf->loadcnt = ++loadcnt;
|
2002-02-22 04:14:49 +00:00
|
|
|
STAILQ_INIT(&lf->common);
|
|
|
|
TAILQ_INIT(&lf->modules);
|
|
|
|
TAILQ_INSERT_TAIL(&linker_files, lf, link);
|
|
|
|
return (lf);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-07-13 19:36:59 +00:00
|
|
|
linker_file_unload(linker_file_t file, int flags)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
module_t mod, next;
|
|
|
|
modlist_t ml, nextml;
|
|
|
|
struct common_symbol *cp;
|
|
|
|
int error, i;
|
|
|
|
|
|
|
|
/* Refuse to unload modules if securelevel raised. */
|
2009-05-27 14:11:23 +00:00
|
|
|
if (prison0.pr_securelevel > 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
return (EPERM);
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
|
2006-06-13 19:45:08 +00:00
|
|
|
|
|
|
|
/* Easy case of just dropping a reference. */
|
|
|
|
if (file->refs > 1) {
|
|
|
|
file->refs--;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-12-19 03:48:36 +00:00
|
|
|
/* Give eventhandlers a chance to prevent the unload. */
|
|
|
|
error = 0;
|
|
|
|
EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
|
|
|
|
if (error != 0)
|
|
|
|
return (EBUSY);
|
|
|
|
|
2006-06-13 19:45:08 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
|
|
|
|
" informing modules\n"));
|
|
|
|
|
|
|
|
/*
|
2008-12-05 13:40:25 +00:00
|
|
|
* Quiesce all the modules to give them a chance to veto the unload.
|
|
|
|
*/
|
|
|
|
MOD_SLOCK;
|
|
|
|
for (mod = TAILQ_FIRST(&file->modules); mod;
|
|
|
|
mod = module_getfnext(mod)) {
|
|
|
|
|
|
|
|
error = module_quiesce(mod);
|
|
|
|
if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
|
|
|
|
KLD_DPF(FILE, ("linker_file_unload: module %s"
|
|
|
|
" vetoed unload\n", module_getname(mod)));
|
|
|
|
/*
|
|
|
|
* XXX: Do we need to tell all the quiesced modules
|
|
|
|
* that they can resume work now via a new module
|
|
|
|
* event?
|
|
|
|
*/
|
|
|
|
MOD_SUNLOCK;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOD_SUNLOCK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inform any modules associated with this file that they are
|
2012-04-12 14:49:25 +00:00
|
|
|
* being unloaded.
|
2006-06-13 19:45:08 +00:00
|
|
|
*/
|
|
|
|
MOD_XLOCK;
|
|
|
|
for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
|
|
|
|
next = module_getfnext(mod);
|
|
|
|
MOD_XUNLOCK;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2006-06-13 19:45:08 +00:00
|
|
|
* Give the module a chance to veto the unload.
|
2002-02-22 04:14:49 +00:00
|
|
|
*/
|
2008-12-05 13:40:25 +00:00
|
|
|
if ((error = module_unload(mod)) != 0) {
|
2012-03-21 09:48:32 +00:00
|
|
|
#ifdef KLD_DEBUG
|
|
|
|
MOD_SLOCK;
|
2008-12-05 13:40:25 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_unload: module %s"
|
2009-11-17 21:56:12 +00:00
|
|
|
" failed unload\n", module_getname(mod)));
|
2012-03-21 09:48:32 +00:00
|
|
|
MOD_SUNLOCK;
|
|
|
|
#endif
|
2006-06-13 19:45:08 +00:00
|
|
|
return (error);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
2006-06-13 19:45:08 +00:00
|
|
|
MOD_XLOCK;
|
|
|
|
module_release(mod);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
2006-06-13 19:45:08 +00:00
|
|
|
MOD_XUNLOCK;
|
|
|
|
|
2006-06-13 20:49:07 +00:00
|
|
|
TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
|
2004-04-09 15:27:38 +00:00
|
|
|
if (ml->container == file) {
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_REMOVE(&found_modules, ml, link);
|
2004-04-09 15:27:38 +00:00
|
|
|
free(ml, M_LINKER);
|
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2006-06-21 17:47:45 +00:00
|
|
|
/*
|
|
|
|
* Don't try to run SYSUNINITs if we are unloaded due to a
|
2002-02-22 04:14:49 +00:00
|
|
|
* link error.
|
|
|
|
*/
|
|
|
|
if (file->flags & LINKER_FILE_LINKED) {
|
2009-02-05 23:01:36 +00:00
|
|
|
file->flags &= ~LINKER_FILE_LINKED;
|
2017-10-01 16:37:54 +00:00
|
|
|
linker_file_unregister_sysctls(file);
|
2017-10-05 12:32:14 +00:00
|
|
|
linker_file_sysuninit(file);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&linker_files, file, link);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
if (file->deps) {
|
|
|
|
for (i = 0; i < file->ndeps; i++)
|
2004-07-13 19:36:59 +00:00
|
|
|
linker_file_unload(file->deps[i], flags);
|
2002-02-22 04:14:49 +00:00
|
|
|
free(file->deps, M_LINKER);
|
|
|
|
file->deps = NULL;
|
|
|
|
}
|
2006-07-10 19:28:57 +00:00
|
|
|
while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
|
|
|
|
STAILQ_REMOVE_HEAD(&file->common, link);
|
2002-02-22 04:14:49 +00:00
|
|
|
free(cp, M_LINKER);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
LINKER_UNLOAD(file);
|
2013-12-19 03:48:36 +00:00
|
|
|
|
|
|
|
EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
|
|
|
|
file->size);
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
if (file->filename) {
|
|
|
|
free(file->filename, M_LINKER);
|
|
|
|
file->filename = NULL;
|
|
|
|
}
|
2007-10-22 04:12:57 +00:00
|
|
|
if (file->pathname) {
|
|
|
|
free(file->pathname, M_LINKER);
|
|
|
|
file->pathname = NULL;
|
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
kobj_delete((kobj_t) file, M_LINKER);
|
2006-06-13 19:45:08 +00:00
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-23 07:08:59 +00:00
|
|
|
int
|
|
|
|
linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
|
|
|
|
{
|
|
|
|
return (LINKER_CTF_GET(file, lc));
|
|
|
|
}
|
|
|
|
|
2006-06-20 20:18:42 +00:00
|
|
|
static int
|
2001-11-16 21:08:40 +00:00
|
|
|
linker_file_add_dependency(linker_file_t file, linker_file_t dep)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t *newdeps;
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2014-04-05 20:44:52 +00:00
|
|
|
file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
|
|
|
|
M_LINKER, M_WAITOK | M_ZERO);
|
2002-02-22 04:14:49 +00:00
|
|
|
file->deps[file->ndeps] = dep;
|
|
|
|
file->ndeps++;
|
2009-11-17 21:56:12 +00:00
|
|
|
KLD_DPF(FILE, ("linker_file_add_dependency:"
|
|
|
|
" adding %s as dependency for %s\n",
|
|
|
|
dep->filename, file->filename));
|
2002-02-22 04:14:49 +00:00
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Locate a linker set and its contents. This is a helper function to avoid
|
2006-06-21 17:48:03 +00:00
|
|
|
* linker_if.h exposure elsewhere. Note: firstp and lastp are really void **.
|
|
|
|
* This function is used in this file so we can avoid having lots of (void **)
|
|
|
|
* casts.
|
2001-06-13 10:58:39 +00:00
|
|
|
*/
|
2015-06-08 14:06:47 +00:00
|
|
|
int
|
|
|
|
linker_file_lookup_set(linker_file_t file, const char *name,
|
|
|
|
void *firstp, void *lastp, int *countp)
|
|
|
|
{
|
|
|
|
|
2015-06-08 18:06:00 +00:00
|
|
|
sx_assert(&kld_sx, SA_LOCKED);
|
|
|
|
return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
|
2015-06-08 14:06:47 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 00:23:31 +00:00
|
|
|
/*
|
|
|
|
* List all functions in a file.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
linker_file_function_listall(linker_file_t lf,
|
2008-05-23 07:08:59 +00:00
|
|
|
linker_function_nameval_callback_t callback_func, void *arg)
|
2007-11-18 00:23:31 +00:00
|
|
|
{
|
|
|
|
return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
caddr_t
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
|
2006-06-21 20:42:08 +00:00
|
|
|
{
|
|
|
|
caddr_t sym;
|
|
|
|
int locked;
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
locked = sx_xlocked(&kld_sx);
|
2006-06-21 20:42:08 +00:00
|
|
|
if (!locked)
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2006-06-21 20:42:08 +00:00
|
|
|
sym = linker_file_lookup_symbol_internal(file, name, deps);
|
|
|
|
if (!locked)
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-21 20:42:08 +00:00
|
|
|
return (sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
static caddr_t
|
|
|
|
linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
|
|
|
|
int deps)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
c_linker_sym_t sym;
|
|
|
|
linker_symval_t symval;
|
|
|
|
caddr_t address;
|
|
|
|
size_t common_size = 0;
|
2002-03-10 23:12:43 +00:00
|
|
|
int i;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2003-01-21 02:42:44 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
|
2002-02-22 04:14:49 +00:00
|
|
|
file, name, deps));
|
|
|
|
|
|
|
|
if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
|
|
|
|
LINKER_SYMBOL_VALUES(file, sym, &symval);
|
|
|
|
if (symval.value == 0)
|
|
|
|
/*
|
|
|
|
* For commons, first look them up in the
|
|
|
|
* dependencies and only allocate space if not found
|
|
|
|
* there.
|
|
|
|
*/
|
|
|
|
common_size = symval.size;
|
|
|
|
else {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
|
2003-01-21 02:42:44 +00:00
|
|
|
".value=%p\n", symval.value));
|
2002-02-22 04:14:49 +00:00
|
|
|
return (symval.value);
|
|
|
|
}
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
if (deps) {
|
|
|
|
for (i = 0; i < file->ndeps; i++) {
|
2006-06-21 20:42:08 +00:00
|
|
|
address = linker_file_lookup_symbol_internal(
|
|
|
|
file->deps[i], name, 0);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (address) {
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol:"
|
2003-01-21 02:42:44 +00:00
|
|
|
" deps value=%p\n", address));
|
2002-02-22 04:14:49 +00:00
|
|
|
return (address);
|
|
|
|
}
|
|
|
|
}
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
if (common_size > 0) {
|
|
|
|
/*
|
|
|
|
* This is a common symbol which was not found in the
|
|
|
|
* dependencies. We maintain a simple common symbol table in
|
|
|
|
* the file object.
|
|
|
|
*/
|
|
|
|
struct common_symbol *cp;
|
|
|
|
|
|
|
|
STAILQ_FOREACH(cp, &file->common, link) {
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(cp->name, name) == 0) {
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol:"
|
2003-01-21 02:42:44 +00:00
|
|
|
" old common value=%p\n", cp->address));
|
2002-02-22 04:14:49 +00:00
|
|
|
return (cp->address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Round the symbol size up to align.
|
|
|
|
*/
|
|
|
|
common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
|
|
|
|
cp = malloc(sizeof(struct common_symbol)
|
|
|
|
+ common_size + strlen(name) + 1, M_LINKER,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_WAITOK | M_ZERO);
|
2002-02-22 04:14:49 +00:00
|
|
|
cp->address = (caddr_t)(cp + 1);
|
|
|
|
cp->name = cp->address + common_size;
|
|
|
|
strcpy(cp->name, name);
|
|
|
|
bzero(cp->address, common_size);
|
|
|
|
STAILQ_INSERT_TAIL(&file->common, cp, link);
|
|
|
|
|
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
|
2003-01-21 02:42:44 +00:00
|
|
|
" value=%p\n", cp->address));
|
2002-02-22 04:14:49 +00:00
|
|
|
return (cp->address);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
|
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/*
|
2007-12-01 19:24:28 +00:00
|
|
|
* Both DDB and stack(9) rely on the kernel linker to provide forward and
|
|
|
|
* backward lookup of symbols. However, DDB and sometimes stack(9) need to
|
|
|
|
* do this in a lockfree manner. We provide a set of internal helper
|
|
|
|
* routines to perform these operations without locks, and then wrappers that
|
|
|
|
* optionally lock.
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
2007-12-01 19:24:28 +00:00
|
|
|
* linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
|
1998-10-10 00:07:53 +00:00
|
|
|
*/
|
2007-12-01 19:24:28 +00:00
|
|
|
#ifdef DDB
|
|
|
|
static int
|
|
|
|
linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (ENOENT);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2007-12-01 19:24:28 +00:00
|
|
|
#endif
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2007-12-01 19:24:28 +00:00
|
|
|
static int
|
|
|
|
linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
c_linker_sym_t best, es;
|
|
|
|
u_long diff, bestdiff, off;
|
|
|
|
|
|
|
|
best = 0;
|
|
|
|
off = (uintptr_t)value;
|
|
|
|
bestdiff = off;
|
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
|
|
|
|
continue;
|
|
|
|
if (es != 0 && diff < bestdiff) {
|
|
|
|
best = es;
|
|
|
|
bestdiff = diff;
|
|
|
|
}
|
|
|
|
if (bestdiff == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (best) {
|
|
|
|
*sym = best;
|
|
|
|
*diffp = bestdiff;
|
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
*sym = 0;
|
|
|
|
*diffp = off;
|
|
|
|
return (ENOENT);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-01 19:24:28 +00:00
|
|
|
static int
|
|
|
|
linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (ENOENT);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2007-12-01 19:24:28 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
|
|
|
|
long *offset)
|
|
|
|
{
|
|
|
|
linker_symval_t symval;
|
|
|
|
c_linker_sym_t sym;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
*offset = 0;
|
|
|
|
error = linker_debug_search_symbol(value, &sym, offset);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
error = linker_debug_symbol_values(sym, &symval);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
strlcpy(buf, symval.name, buflen);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DDB Helpers. DDB has to look across multiple files with their own symbol
|
|
|
|
* tables and string tables.
|
|
|
|
*
|
|
|
|
* Note that we do not obey list locking protocols here. We really don't need
|
|
|
|
* DDB to hang because somebody's got the lock held. We'll take the chance
|
2016-04-29 22:15:33 +00:00
|
|
|
* that the files list is inconsistent instead.
|
2007-12-01 19:24:28 +00:00
|
|
|
*/
|
2010-09-22 06:45:07 +00:00
|
|
|
#ifdef DDB
|
2007-12-01 19:24:28 +00:00
|
|
|
int
|
|
|
|
linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (linker_debug_lookup(symstr, sym));
|
|
|
|
}
|
2010-09-22 06:45:07 +00:00
|
|
|
#endif
|
2007-12-01 19:24:28 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (linker_debug_search_symbol(value, sym, diffp));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (linker_debug_symbol_values(sym, symval));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
|
|
|
|
long *offset)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (linker_debug_search_symbol_name(value, buf, buflen, offset));
|
|
|
|
}
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2007-12-01 19:24:28 +00:00
|
|
|
/*
|
|
|
|
* stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do
|
|
|
|
* obey locking protocols, and offer a significantly less complex interface.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
|
|
|
|
long *offset)
|
|
|
|
{
|
2008-04-21 17:22:35 +00:00
|
|
|
int error;
|
2007-12-01 19:24:28 +00:00
|
|
|
|
2014-10-21 21:29:20 +00:00
|
|
|
sx_slock(&kld_sx);
|
2007-12-01 19:24:28 +00:00
|
|
|
error = linker_debug_search_symbol_name(value, buf, buflen, offset);
|
2014-10-21 21:29:20 +00:00
|
|
|
sx_sunlock(&kld_sx);
|
2007-12-01 19:24:28 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
/*
|
|
|
|
* Syscalls.
|
|
|
|
*/
|
|
|
|
int
|
2006-06-13 21:28:18 +00:00
|
|
|
kern_kldload(struct thread *td, const char *file, int *fileid)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2006-06-13 21:28:18 +00:00
|
|
|
const char *kldname, *modname;
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
2006-06-13 21:28:18 +00:00
|
|
|
int error;
|
2002-03-20 16:03:42 +00:00
|
|
|
|
2002-03-25 18:26:34 +00:00
|
|
|
if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
|
2006-06-13 21:28:18 +00:00
|
|
|
return (error);
|
2002-03-25 18:26:34 +00:00
|
|
|
|
2006-11-06 13:42:10 +00:00
|
|
|
if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
|
2006-06-13 21:28:18 +00:00
|
|
|
return (error);
|
2002-02-22 04:14:49 +00:00
|
|
|
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
/*
|
2009-05-08 14:34:25 +00:00
|
|
|
* It is possible that kldloaded module will attach a new ifnet,
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
* so vnet context must be set when this ocurs.
|
|
|
|
*/
|
|
|
|
CURVNET_SET(TD_TO_VNET(td));
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
2006-06-21 17:48:03 +00:00
|
|
|
* If file does not contain a qualified name or any dot in it
|
|
|
|
* (kldname.ko, or kldname.ver.ko) treat it as an interface
|
2002-02-22 04:14:49 +00:00
|
|
|
* name.
|
|
|
|
*/
|
2012-01-02 12:12:10 +00:00
|
|
|
if (strchr(file, '/') || strchr(file, '.')) {
|
2006-06-13 21:28:18 +00:00
|
|
|
kldname = file;
|
2002-02-22 04:14:49 +00:00
|
|
|
modname = NULL;
|
|
|
|
} else {
|
|
|
|
kldname = NULL;
|
2006-06-13 21:28:18 +00:00
|
|
|
modname = file;
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
2006-06-13 21:28:18 +00:00
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
error = linker_load_module(kldname, modname, NULL, NULL, &lf);
|
2011-07-17 21:53:42 +00:00
|
|
|
if (error) {
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2011-07-17 21:53:42 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
lf->userrefs++;
|
|
|
|
if (fileid != NULL)
|
|
|
|
*fileid = lf->id;
|
2013-12-19 03:48:36 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2011-07-17 21:53:42 +00:00
|
|
|
|
|
|
|
done:
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
CURVNET_RESTORE();
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2006-06-13 21:28:18 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldload(struct thread *td, struct kldload_args *uap)
|
2006-06-13 21:28:18 +00:00
|
|
|
{
|
|
|
|
char *pathname = NULL;
|
2006-06-14 03:01:06 +00:00
|
|
|
int error, fileid;
|
2006-06-13 21:28:18 +00:00
|
|
|
|
|
|
|
td->td_retval[0] = -1;
|
|
|
|
|
|
|
|
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
|
|
|
error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
|
2006-06-14 03:01:06 +00:00
|
|
|
if (error == 0) {
|
|
|
|
error = kern_kldload(td, pathname, &fileid);
|
|
|
|
if (error == 0)
|
|
|
|
td->td_retval[0] = fileid;
|
|
|
|
}
|
2006-06-13 21:28:18 +00:00
|
|
|
free(pathname, M_TEMP);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-07-13 19:36:59 +00:00
|
|
|
kern_kldunload(struct thread *td, int fileid, int flags)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-03-25 18:26:34 +00:00
|
|
|
if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
|
2006-06-13 21:28:18 +00:00
|
|
|
return (error);
|
2002-03-25 18:26:34 +00:00
|
|
|
|
2006-11-06 13:42:10 +00:00
|
|
|
if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
|
2006-06-13 21:28:18 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
CURVNET_SET(TD_TO_VNET(td));
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2004-07-13 19:36:59 +00:00
|
|
|
lf = linker_find_file_by_id(fileid);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (lf) {
|
|
|
|
KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
|
2007-10-22 04:12:57 +00:00
|
|
|
|
2013-12-19 03:48:36 +00:00
|
|
|
if (lf->userrefs == 0) {
|
2004-07-13 19:36:59 +00:00
|
|
|
/*
|
|
|
|
* XXX: maybe LINKER_UNLOAD_FORCE should override ?
|
|
|
|
*/
|
2002-02-22 04:14:49 +00:00
|
|
|
printf("kldunload: attempt to unload file that was"
|
|
|
|
" loaded by the kernel\n");
|
2006-06-21 17:47:45 +00:00
|
|
|
error = EBUSY;
|
2006-06-13 21:28:18 +00:00
|
|
|
} else {
|
|
|
|
lf->userrefs--;
|
|
|
|
error = linker_file_unload(lf, flags);
|
|
|
|
if (error)
|
|
|
|
lf->userrefs++;
|
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
} else
|
|
|
|
error = ENOENT;
|
2013-12-19 03:48:36 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2013-08-24 21:13:38 +00:00
|
|
|
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
CURVNET_RESTORE();
|
2002-02-22 13:43:56 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2004-07-13 19:36:59 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldunload(struct thread *td, struct kldunload_args *uap)
|
2004-07-13 19:36:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
|
2004-07-13 19:36:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (uap->flags != LINKER_UNLOAD_NORMAL &&
|
|
|
|
uap->flags != LINKER_UNLOAD_FORCE)
|
|
|
|
return (EINVAL);
|
|
|
|
return (kern_kldunload(td, uap->fileid, uap->flags));
|
|
|
|
}
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldfind(struct thread *td, struct kldfind_args *uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
char *pathname;
|
|
|
|
const char *filename;
|
|
|
|
linker_file_t lf;
|
2006-06-20 19:32:10 +00:00
|
|
|
int error;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
td->td_retval[0] = -1;
|
1999-01-05 20:24:28 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
2002-12-14 08:18:06 +00:00
|
|
|
if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
goto out;
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
filename = linker_basename(pathname);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
lf = linker_find_file_by_name(filename);
|
|
|
|
if (lf)
|
|
|
|
td->td_retval[0] = lf->id;
|
|
|
|
else
|
|
|
|
error = ENOENT;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
1997-05-07 16:05:47 +00:00
|
|
|
out:
|
2006-06-20 19:32:10 +00:00
|
|
|
free(pathname, M_TEMP);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldnext(struct thread *td, struct kldnext_args *uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
int error = 0;
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2007-02-23 19:46:59 +00:00
|
|
|
if (uap->fileid == 0)
|
|
|
|
lf = TAILQ_FIRST(&linker_files);
|
|
|
|
else {
|
|
|
|
lf = linker_find_file_by_id(uap->fileid);
|
|
|
|
if (lf == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
lf = TAILQ_NEXT(lf, link);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
2007-02-23 19:46:59 +00:00
|
|
|
|
|
|
|
/* Skip partially loaded files. */
|
|
|
|
while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
|
|
|
|
lf = TAILQ_NEXT(lf, link);
|
|
|
|
|
|
|
|
if (lf)
|
|
|
|
td->td_retval[0] = lf->id;
|
|
|
|
else
|
|
|
|
td->td_retval[0] = 0;
|
2001-09-01 19:04:37 +00:00
|
|
|
out:
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldstat(struct thread *td, struct kldstat_args *uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2006-06-13 21:11:12 +00:00
|
|
|
struct kld_file_stat stat;
|
2011-03-30 14:46:12 +00:00
|
|
|
int error, version;
|
2006-06-13 21:11:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the version of the user's structure.
|
|
|
|
*/
|
2011-03-30 14:46:12 +00:00
|
|
|
if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
|
|
|
|
!= 0)
|
2006-06-13 21:11:12 +00:00
|
|
|
return (error);
|
2011-03-30 14:46:12 +00:00
|
|
|
if (version != sizeof(struct kld_file_stat_1) &&
|
|
|
|
version != sizeof(struct kld_file_stat))
|
2006-06-13 21:11:12 +00:00
|
|
|
return (EINVAL);
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2011-03-30 14:46:12 +00:00
|
|
|
error = kern_kldstat(td, uap->fileid, &stat);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
return (copyout(&stat, uap->stat, version));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
|
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int namelen;
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2011-03-30 14:46:12 +00:00
|
|
|
int error;
|
|
|
|
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2011-03-30 14:46:12 +00:00
|
|
|
lf = linker_find_file_by_id(fileid);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (lf == NULL) {
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-13 21:11:12 +00:00
|
|
|
return (ENOENT);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-10-22 04:12:57 +00:00
|
|
|
/* Version 1 fields: */
|
2002-02-22 04:14:49 +00:00
|
|
|
namelen = strlen(lf->filename) + 1;
|
2017-07-29 23:31:21 +00:00
|
|
|
if (namelen > sizeof(stat->name))
|
|
|
|
namelen = sizeof(stat->name);
|
2011-03-30 14:46:12 +00:00
|
|
|
bcopy(lf->filename, &stat->name[0], namelen);
|
|
|
|
stat->refs = lf->refs;
|
|
|
|
stat->id = lf->id;
|
|
|
|
stat->address = lf->address;
|
|
|
|
stat->size = lf->size;
|
|
|
|
/* Version 2 fields: */
|
|
|
|
namelen = strlen(lf->pathname) + 1;
|
2017-07-29 23:31:21 +00:00
|
|
|
if (namelen > sizeof(stat->pathname))
|
|
|
|
namelen = sizeof(stat->pathname);
|
2011-03-30 14:46:12 +00:00
|
|
|
bcopy(lf->pathname, &stat->pathname[0], namelen);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
1997-05-07 16:05:47 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
td->td_retval[0] = 0;
|
2011-03-30 14:46:12 +00:00
|
|
|
return (0);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 18:27:41 +00:00
|
|
|
#ifdef DDB
|
|
|
|
DB_COMMAND(kldstat, db_kldstat)
|
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
|
|
|
|
#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
|
|
|
|
db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
|
|
|
|
#undef POINTER_WIDTH
|
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
if (db_pager_quit)
|
|
|
|
return;
|
|
|
|
db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
|
|
|
|
lf->address, lf->size, lf->filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DDB */
|
|
|
|
|
1997-05-07 16:05:47 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
|
1997-05-07 16:05:47 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
module_t mp;
|
|
|
|
int error = 0;
|
|
|
|
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-12-14 01:56:26 +00:00
|
|
|
lf = linker_find_file_by_id(uap->fileid);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (lf) {
|
2002-03-18 07:45:30 +00:00
|
|
|
MOD_SLOCK;
|
2002-02-22 04:14:49 +00:00
|
|
|
mp = TAILQ_FIRST(&lf->modules);
|
|
|
|
if (mp != NULL)
|
|
|
|
td->td_retval[0] = module_getid(mp);
|
|
|
|
else
|
|
|
|
td->td_retval[0] = 0;
|
2002-03-18 07:45:30 +00:00
|
|
|
MOD_SUNLOCK;
|
2002-02-22 04:14:49 +00:00
|
|
|
} else
|
|
|
|
error = ENOENT;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1997-05-07 16:05:47 +00:00
|
|
|
}
|
1998-10-10 00:07:53 +00:00
|
|
|
|
1998-11-11 13:04:40 +00:00
|
|
|
int
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_kldsym(struct thread *td, struct kldsym_args *uap)
|
1998-11-11 13:04:40 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
char *symstr = NULL;
|
|
|
|
c_linker_sym_t sym;
|
|
|
|
linker_symval_t symval;
|
|
|
|
linker_file_t lf;
|
|
|
|
struct kld_sym_lookup lookup;
|
|
|
|
int error = 0;
|
|
|
|
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
|
|
|
|
2002-12-14 01:56:26 +00:00
|
|
|
if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
|
2006-06-21 20:15:36 +00:00
|
|
|
return (error);
|
2002-02-22 13:43:56 +00:00
|
|
|
if (lookup.version != sizeof(lookup) ||
|
2006-06-21 20:15:36 +00:00
|
|
|
uap->cmd != KLDSYM_LOOKUP)
|
|
|
|
return (EINVAL);
|
2003-02-19 05:47:46 +00:00
|
|
|
symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
|
2002-02-22 04:14:49 +00:00
|
|
|
if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
|
|
|
|
goto out;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-12-14 01:56:26 +00:00
|
|
|
if (uap->fileid != 0) {
|
|
|
|
lf = linker_find_file_by_id(uap->fileid);
|
2006-06-21 20:15:36 +00:00
|
|
|
if (lf == NULL)
|
2002-02-22 04:14:49 +00:00
|
|
|
error = ENOENT;
|
2006-06-21 20:15:36 +00:00
|
|
|
else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
|
2002-02-22 04:14:49 +00:00
|
|
|
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
|
|
|
|
lookup.symvalue = (uintptr_t) symval.value;
|
|
|
|
lookup.symsize = symval.size;
|
2002-12-14 08:18:06 +00:00
|
|
|
error = copyout(&lookup, uap->data, sizeof(lookup));
|
2002-02-22 04:14:49 +00:00
|
|
|
} else
|
|
|
|
error = ENOENT;
|
|
|
|
} else {
|
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
|
|
|
|
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
|
|
|
|
lookup.symvalue = (uintptr_t)symval.value;
|
|
|
|
lookup.symsize = symval.size;
|
2002-12-14 01:56:26 +00:00
|
|
|
error = copyout(&lookup, uap->data,
|
2002-02-22 04:14:49 +00:00
|
|
|
sizeof(lookup));
|
2002-02-22 13:43:56 +00:00
|
|
|
break;
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lf == NULL)
|
|
|
|
error = ENOENT;
|
1998-11-11 13:04:40 +00:00
|
|
|
}
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2006-06-21 20:15:36 +00:00
|
|
|
out:
|
|
|
|
free(symstr, M_TEMP);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
1998-11-11 13:04:40 +00:00
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/*
|
|
|
|
* Preloaded module support
|
|
|
|
*/
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static modlist_t
|
2001-03-22 08:58:45 +00:00
|
|
|
modlist_lookup(const char *name, int ver)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
modlist_t mod;
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_FOREACH(mod, &found_modules, link) {
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(mod->name, name) == 0 &&
|
|
|
|
(ver == 0 || mod->version == ver))
|
2002-02-22 04:14:49 +00:00
|
|
|
return (mod);
|
|
|
|
}
|
|
|
|
return (NULL);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
static modlist_t
|
2014-12-03 22:14:13 +00:00
|
|
|
modlist_lookup2(const char *name, const struct mod_depend *verinfo)
|
2001-09-11 01:09:24 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
modlist_t mod, bestmod;
|
2002-03-10 23:12:43 +00:00
|
|
|
int ver;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
if (verinfo == NULL)
|
|
|
|
return (modlist_lookup(name, 0));
|
|
|
|
bestmod = NULL;
|
2006-06-13 20:49:07 +00:00
|
|
|
TAILQ_FOREACH(mod, &found_modules, link) {
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(mod->name, name) != 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
continue;
|
|
|
|
ver = mod->version;
|
|
|
|
if (ver == verinfo->md_ver_preferred)
|
|
|
|
return (mod);
|
|
|
|
if (ver >= verinfo->md_ver_minimum &&
|
2002-02-22 13:43:56 +00:00
|
|
|
ver <= verinfo->md_ver_maximum &&
|
2003-09-23 14:42:38 +00:00
|
|
|
(bestmod == NULL || ver > bestmod->version))
|
2002-02-22 04:14:49 +00:00
|
|
|
bestmod = mod;
|
|
|
|
}
|
|
|
|
return (bestmod);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2001-03-22 08:58:45 +00:00
|
|
|
static modlist_t
|
2001-06-20 16:08:45 +00:00
|
|
|
modlist_newmodule(const char *modname, int version, linker_file_t container)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
modlist_t mod;
|
|
|
|
|
2002-03-19 15:41:21 +00:00
|
|
|
mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mod == NULL)
|
|
|
|
panic("no memory for module list");
|
|
|
|
mod->container = container;
|
|
|
|
mod->name = modname;
|
|
|
|
mod->version = version;
|
|
|
|
TAILQ_INSERT_TAIL(&found_modules, mod, link);
|
|
|
|
return (mod);
|
2001-03-22 08:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-13 10:58:39 +00:00
|
|
|
linker_addmodules(linker_file_t lf, struct mod_metadata **start,
|
2002-02-22 04:14:49 +00:00
|
|
|
struct mod_metadata **stop, int preload)
|
2001-03-22 08:58:45 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct mod_metadata *mp, **mdp;
|
|
|
|
const char *modname;
|
|
|
|
int ver;
|
|
|
|
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
2015-12-11 05:27:53 +00:00
|
|
|
ver = ((const struct mod_version *)mp->md_data)->mv_version;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (modlist_lookup(modname, ver) != NULL) {
|
|
|
|
printf("module %s already present!\n", modname);
|
|
|
|
/* XXX what can we do? this is a build error. :-( */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
modlist_newmodule(modname, ver, lf);
|
2001-03-22 08:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
static void
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_preload(void *arg)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
caddr_t modptr;
|
|
|
|
const char *modname, *nmodname;
|
|
|
|
char *modtype;
|
2006-07-10 19:13:45 +00:00
|
|
|
linker_file_t lf, nlf;
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_class_t lc;
|
2002-03-10 23:12:43 +00:00
|
|
|
int error;
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_list_t loaded_files;
|
|
|
|
linker_file_list_t depended_files;
|
|
|
|
struct mod_metadata *mp, *nmp;
|
|
|
|
struct mod_metadata **start, **stop, **mdp, **nmdp;
|
2014-12-03 22:14:13 +00:00
|
|
|
const struct mod_depend *verinfo;
|
2002-02-22 04:14:49 +00:00
|
|
|
int nver;
|
|
|
|
int resolves;
|
|
|
|
modlist_t mod;
|
|
|
|
struct sysinit **si_start, **si_stop;
|
|
|
|
|
|
|
|
TAILQ_INIT(&loaded_files);
|
|
|
|
TAILQ_INIT(&depended_files);
|
|
|
|
TAILQ_INIT(&found_modules);
|
|
|
|
error = 0;
|
|
|
|
|
|
|
|
modptr = NULL;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
while ((modptr = preload_search_next_name(modptr)) != NULL) {
|
|
|
|
modname = (char *)preload_search_info(modptr, MODINFO_NAME);
|
|
|
|
modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
|
|
|
|
if (modname == NULL) {
|
|
|
|
printf("Preloaded module at %p does not have a"
|
|
|
|
" name!\n", modptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (modtype == NULL) {
|
|
|
|
printf("Preloaded module at %p does not have a type!\n",
|
|
|
|
modptr);
|
2000-08-02 21:08:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-07-01 07:46:29 +00:00
|
|
|
if (bootverbose)
|
|
|
|
printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
|
|
|
|
modptr);
|
2002-02-22 04:14:49 +00:00
|
|
|
lf = NULL;
|
|
|
|
TAILQ_FOREACH(lc, &classes, link) {
|
|
|
|
error = LINKER_LINK_PRELOAD(lc, modname, &lf);
|
2004-08-27 01:20:26 +00:00
|
|
|
if (!error)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
2004-08-27 01:20:26 +00:00
|
|
|
lf = NULL;
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
if (lf)
|
|
|
|
TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* First get a list of stuff in the kernel.
|
2000-04-29 13:19:31 +00:00
|
|
|
*/
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
|
|
|
|
&stop, NULL) == 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_addmodules(linker_kernel_file, start, stop, 1);
|
|
|
|
|
|
|
|
/*
|
2007-02-26 16:36:48 +00:00
|
|
|
* This is a once-off kinky bubble sort to resolve relocation
|
|
|
|
* dependency requirements.
|
2002-02-22 04:14:49 +00:00
|
|
|
*/
|
|
|
|
restart:
|
|
|
|
TAILQ_FOREACH(lf, &loaded_files, loaded) {
|
2015-06-08 18:06:00 +00:00
|
|
|
error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
|
2002-02-22 04:14:49 +00:00
|
|
|
&stop, NULL);
|
|
|
|
/*
|
|
|
|
* First, look to see if we would successfully link with this
|
|
|
|
* stuff.
|
|
|
|
*/
|
|
|
|
resolves = 1; /* unless we know otherwise */
|
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
|
|
|
verinfo = mp->md_data;
|
2002-02-22 04:14:49 +00:00
|
|
|
for (nmdp = start; nmdp < stop; nmdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
nmp = *nmdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (nmp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
nmodname = nmp->md_cval;
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(modname, nmodname) == 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nmdp < stop) /* it's a self reference */
|
|
|
|
continue;
|
2006-06-21 17:47:45 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* ok, the module isn't here yet, we
|
|
|
|
* are not finished
|
|
|
|
*/
|
2002-02-22 13:43:56 +00:00
|
|
|
if (modlist_lookup2(modname, verinfo) == NULL)
|
2002-02-22 04:14:49 +00:00
|
|
|
resolves = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* OK, if we found our modules, we can link. So, "provide"
|
|
|
|
* the modules inside and add it to the end of the link order
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
if (resolves) {
|
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
2015-12-11 05:27:53 +00:00
|
|
|
nver = ((const struct mod_version *)
|
2003-01-21 02:42:44 +00:00
|
|
|
mp->md_data)->mv_version;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (modlist_lookup(modname,
|
|
|
|
nver) != NULL) {
|
|
|
|
printf("module %s already"
|
|
|
|
" present!\n", modname);
|
|
|
|
TAILQ_REMOVE(&loaded_files,
|
|
|
|
lf, loaded);
|
2006-07-10 19:13:45 +00:00
|
|
|
linker_file_unload(lf,
|
|
|
|
LINKER_UNLOAD_FORCE);
|
2002-02-22 04:14:49 +00:00
|
|
|
/* we changed tailq next ptr */
|
2002-02-22 13:43:56 +00:00
|
|
|
goto restart;
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
modlist_newmodule(modname, nver, lf);
|
|
|
|
}
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
TAILQ_REMOVE(&loaded_files, lf, loaded);
|
2002-02-22 04:14:49 +00:00
|
|
|
TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
|
|
|
|
/*
|
|
|
|
* Since we provided modules, we need to restart the
|
|
|
|
* sort so that the previous files that depend on us
|
|
|
|
* have a chance. Also, we've busted the tailq next
|
|
|
|
* pointer with the REMOVE.
|
|
|
|
*/
|
|
|
|
goto restart;
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
2001-06-13 10:58:39 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* At this point, we check to see what could not be resolved..
|
2001-06-13 10:58:39 +00:00
|
|
|
*/
|
2006-07-10 19:06:01 +00:00
|
|
|
while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&loaded_files, lf, loaded);
|
2002-02-22 04:14:49 +00:00
|
|
|
printf("KLD file %s is missing dependencies\n", lf->filename);
|
2004-07-13 19:36:59 +00:00
|
|
|
linker_file_unload(lf, LINKER_UNLOAD_FORCE);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* We made it. Finish off the linking in the order we determined.
|
|
|
|
*/
|
2006-07-10 19:13:45 +00:00
|
|
|
TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
|
2002-02-22 04:14:49 +00:00
|
|
|
if (linker_kernel_file) {
|
|
|
|
linker_kernel_file->refs++;
|
|
|
|
error = linker_file_add_dependency(lf,
|
|
|
|
linker_kernel_file);
|
|
|
|
if (error)
|
|
|
|
panic("cannot add dependency");
|
|
|
|
}
|
2015-06-08 18:06:00 +00:00
|
|
|
error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
|
2002-02-22 04:14:49 +00:00
|
|
|
&stop, NULL);
|
|
|
|
if (!error) {
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
|
|
|
verinfo = mp->md_data;
|
2002-02-22 04:14:49 +00:00
|
|
|
mod = modlist_lookup2(modname, verinfo);
|
2011-01-05 09:58:41 +00:00
|
|
|
if (mod == NULL) {
|
|
|
|
printf("KLD file %s - cannot find "
|
|
|
|
"dependency \"%s\"\n",
|
|
|
|
lf->filename, modname);
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-10-19 20:40:30 +00:00
|
|
|
/* Don't count self-dependencies */
|
|
|
|
if (lf == mod->container)
|
|
|
|
continue;
|
2002-02-22 04:14:49 +00:00
|
|
|
mod->container->refs++;
|
|
|
|
error = linker_file_add_dependency(lf,
|
|
|
|
mod->container);
|
|
|
|
if (error)
|
|
|
|
panic("cannot add dependency");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now do relocation etc using the symbol search paths
|
|
|
|
* established by the dependencies
|
|
|
|
*/
|
|
|
|
error = LINKER_LINK_PRELOAD_FINISH(lf);
|
|
|
|
if (error) {
|
|
|
|
printf("KLD file %s - could not finalize loading\n",
|
|
|
|
lf->filename);
|
2011-01-05 09:58:41 +00:00
|
|
|
goto fail;
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
|
|
|
linker_file_register_modules(lf);
|
2016-10-13 02:06:23 +00:00
|
|
|
if (!TAILQ_EMPTY(&lf->modules))
|
|
|
|
lf->flags |= LINKER_FILE_MODULES;
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
|
2002-02-22 04:14:49 +00:00
|
|
|
&si_stop, NULL) == 0)
|
|
|
|
sysinit_add(si_start, si_stop);
|
2017-10-05 12:32:14 +00:00
|
|
|
linker_file_register_sysctls(lf, true);
|
2002-02-22 04:14:49 +00:00
|
|
|
lf->flags |= LINKER_FILE_LINKED;
|
2011-01-05 09:58:41 +00:00
|
|
|
continue;
|
|
|
|
fail:
|
|
|
|
TAILQ_REMOVE(&depended_files, lf, loaded);
|
|
|
|
linker_file_unload(lf, LINKER_UNLOAD_FORCE);
|
2002-02-22 04:14:49 +00:00
|
|
|
}
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2002-02-22 04:14:49 +00:00
|
|
|
/* woohoo! we made it! */
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 10:58:09 +00:00
|
|
|
SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2016-10-13 02:06:23 +00:00
|
|
|
/*
|
|
|
|
* Handle preload files that failed to load any modules.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
linker_preload_finish(void *arg)
|
|
|
|
{
|
|
|
|
linker_file_t lf, nlf;
|
|
|
|
|
|
|
|
sx_xlock(&kld_sx);
|
|
|
|
TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
|
|
|
|
/*
|
|
|
|
* If all of the modules in this file failed to load, unload
|
|
|
|
* the file and return an error of ENOEXEC. (Parity with
|
|
|
|
* linker_load_file.)
|
|
|
|
*/
|
|
|
|
if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
|
|
|
|
TAILQ_EMPTY(&lf->modules)) {
|
|
|
|
linker_file_unload(lf, LINKER_UNLOAD_FORCE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf->flags &= ~LINKER_FILE_MODULES;
|
|
|
|
lf->userrefs++; /* so we can (try to) kldunload it */
|
|
|
|
}
|
|
|
|
sx_xunlock(&kld_sx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be
|
|
|
|
* scheduled at any subsystem and order, so run this as late as possible. init
|
|
|
|
* becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
|
|
|
|
*/
|
|
|
|
SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
|
|
|
|
linker_preload_finish, 0);
|
|
|
|
|
1998-10-10 00:07:53 +00:00
|
|
|
/*
|
|
|
|
* Search for a not-loaded module by name.
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
1998-10-10 00:07:53 +00:00
|
|
|
* Modules may be found in the following locations:
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
2002-02-22 04:14:49 +00:00
|
|
|
* - preloaded (result is just the module name) - on disk (result is full path
|
|
|
|
* to module)
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
2002-02-22 04:14:49 +00:00
|
|
|
* If the module name is qualified in any way (contains path, etc.) the we
|
|
|
|
* simply return a copy of it.
|
2006-06-21 17:47:45 +00:00
|
|
|
*
|
1998-10-10 00:07:53 +00:00
|
|
|
* The search path can be manipulated via sysctl. Note that we use the ';'
|
|
|
|
* character as a separator to be consistent with the bootloader.
|
|
|
|
*/
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
static char linker_hintfile[] = "linker.hints";
|
2003-03-03 22:53:35 +00:00
|
|
|
static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2014-06-28 03:56:17 +00:00
|
|
|
SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
|
2002-02-22 04:14:49 +00:00
|
|
|
sizeof(linker_path), "module load search path");
|
1998-10-10 00:07:53 +00:00
|
|
|
|
2001-06-06 22:17:08 +00:00
|
|
|
TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
|
2000-12-28 08:14:58 +00:00
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
static char *linker_ext_list[] = {
|
|
|
|
"",
|
2001-09-11 01:09:24 +00:00
|
|
|
".ko",
|
2000-04-29 13:19:31 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Check if file actually exists either with or without extension listed in
|
|
|
|
* the linker_ext_list. (probably should be generic for the rest of the
|
|
|
|
* kernel)
|
2001-09-11 01:09:24 +00:00
|
|
|
*/
|
2000-04-29 13:19:31 +00:00
|
|
|
static char *
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_lookup_file(const char *path, int pathlen, const char *name,
|
|
|
|
int namelen, struct vattr *vap)
|
1998-10-10 00:07:53 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct nameidata nd;
|
|
|
|
struct thread *td = curthread; /* XXX */
|
|
|
|
char *result, **cpp, *sep;
|
2012-10-22 17:50:54 +00:00
|
|
|
int error, len, extlen, reclen, flags;
|
2002-02-22 04:14:49 +00:00
|
|
|
enum vtype type;
|
|
|
|
|
|
|
|
extlen = 0;
|
|
|
|
for (cpp = linker_ext_list; *cpp; cpp++) {
|
|
|
|
len = strlen(*cpp);
|
|
|
|
if (len > extlen)
|
|
|
|
extlen = len;
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
extlen++; /* trailing '\0' */
|
|
|
|
sep = (path[pathlen - 1] != '/') ? "/" : "";
|
|
|
|
|
|
|
|
reclen = pathlen + strlen(sep) + namelen + extlen + 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
result = malloc(reclen, M_LINKER, M_WAITOK);
|
2002-02-22 04:14:49 +00:00
|
|
|
for (cpp = linker_ext_list; *cpp; cpp++) {
|
|
|
|
snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
|
|
|
|
namelen, name, *cpp);
|
|
|
|
/*
|
|
|
|
* Attempt to open the file, and return the path if
|
|
|
|
* we succeed and it's a regular file.
|
|
|
|
*/
|
2012-10-22 17:50:54 +00:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
|
2002-02-22 04:14:49 +00:00
|
|
|
flags = FREAD;
|
2007-05-31 11:51:53 +00:00
|
|
|
error = vn_open(&nd, &flags, 0, NULL);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (error == 0) {
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
type = nd.ni_vp->v_type;
|
|
|
|
if (vap)
|
2008-08-28 15:23:18 +00:00
|
|
|
VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0);
|
2002-02-27 18:32:23 +00:00
|
|
|
vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (type == VREG)
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(result, M_LINKER);
|
|
|
|
return (NULL);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
#define INT_ALIGN(base, ptr) ptr = \
|
2016-04-21 19:57:40 +00:00
|
|
|
(base) + roundup2((ptr) - (base), sizeof(int))
|
2001-09-11 01:09:24 +00:00
|
|
|
|
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Lookup KLD which contains requested module in the "linker.hints" file. If
|
|
|
|
* version specification is available, then try to find the best KLD.
|
2001-09-11 01:09:24 +00:00
|
|
|
* Otherwise just find the latest one.
|
|
|
|
*/
|
|
|
|
static char *
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_hints_lookup(const char *path, int pathlen, const char *modname,
|
2014-12-03 22:14:13 +00:00
|
|
|
int modnamelen, const struct mod_depend *verinfo)
|
2001-09-11 01:09:24 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
2002-02-27 18:32:23 +00:00
|
|
|
struct ucred *cred = td ? td->td_ucred : NULL;
|
2002-02-22 04:14:49 +00:00
|
|
|
struct nameidata nd;
|
|
|
|
struct vattr vattr, mattr;
|
|
|
|
u_char *hints = NULL;
|
|
|
|
u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
|
2012-02-21 01:05:12 +00:00
|
|
|
int error, ival, bestver, *intp, found, flags, clen, blen;
|
|
|
|
ssize_t reclen;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
bestver = found = 0;
|
|
|
|
|
|
|
|
sep = (path[pathlen - 1] != '/') ? "/" : "";
|
|
|
|
reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
|
|
|
|
strlen(sep) + 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
|
2002-02-22 04:14:49 +00:00
|
|
|
snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
|
|
|
|
linker_hintfile);
|
|
|
|
|
2012-10-22 17:50:54 +00:00
|
|
|
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
|
2002-02-22 04:14:49 +00:00
|
|
|
flags = FREAD;
|
2007-05-31 11:51:53 +00:00
|
|
|
error = vn_open(&nd, &flags, 0, NULL);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
if (nd.ni_vp->v_type != VREG)
|
|
|
|
goto bad;
|
|
|
|
best = cp = NULL;
|
2008-08-28 15:23:18 +00:00
|
|
|
error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
|
|
|
/*
|
|
|
|
* XXX: we need to limit this number to some reasonable value
|
|
|
|
*/
|
2014-11-29 17:29:30 +00:00
|
|
|
if (vattr.va_size > LINKER_HINTS_MAX) {
|
2002-02-22 04:14:49 +00:00
|
|
|
printf("hints file too large %ld\n", (long)vattr.va_size);
|
|
|
|
goto bad;
|
|
|
|
}
|
2003-02-19 05:47:46 +00:00
|
|
|
hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
|
2002-02-22 13:43:56 +00:00
|
|
|
error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
|
In order to better support flexible and extensible access control,
make a series of modifications to the credential arguments relating
to file read and write operations to cliarfy which credential is
used for what:
- Change fo_read() and fo_write() to accept "active_cred" instead of
"cred", and change the semantics of consumers of fo_read() and
fo_write() to pass the active credential of the thread requesting
an operation rather than the cached file cred. The cached file
cred is still available in fo_read() and fo_write() consumers
via fp->f_cred. These changes largely in sys_generic.c.
For each implementation of fo_read() and fo_write(), update cred
usage to reflect this change and maintain current semantics:
- badfo_readwrite() unchanged
- kqueue_read/write() unchanged
pipe_read/write() now authorize MAC using active_cred rather
than td->td_ucred
- soo_read/write() unchanged
- vn_read/write() now authorize MAC using active_cred but
VOP_READ/WRITE() with fp->f_cred
Modify vn_rdwr() to accept two credential arguments instead of a
single credential: active_cred and file_cred. Use active_cred
for MAC authorization, and select a credential for use in
VOP_READ/WRITE() based on whether file_cred is NULL or not. If
file_cred is provided, authorize the VOP using that cred,
otherwise the active credential, matching current semantics.
Modify current vn_rdwr() consumers to pass a file_cred if used
in the context of a struct file, and to always pass active_cred.
When vn_rdwr() is used without a file_cred, pass NOCRED.
These changes should maintain current semantics for read/write,
but avoid a redundant passing of fp->f_cred, as well as making
it more clear what the origin of each credential is in file
descriptor read/write operations.
Follow-up commits will make similar changes to other file descriptor
operations, and modify the MAC framework to pass both credentials
to MAC policy modules so they can implement either semantic for
revocation.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-15 20:55:08 +00:00
|
|
|
UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0);
|
2002-02-22 04:14:49 +00:00
|
|
|
vn_close(nd.ni_vp, FREAD, cred, td);
|
|
|
|
nd.ni_vp = NULL;
|
|
|
|
if (reclen != 0) {
|
2012-02-21 01:05:12 +00:00
|
|
|
printf("can't read %zd\n", reclen);
|
2002-02-22 04:14:49 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
intp = (int *)hints;
|
2001-09-11 01:09:24 +00:00
|
|
|
ival = *intp++;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (ival != LINKER_HINTS_VERSION) {
|
|
|
|
printf("hints file version mismatch %d\n", ival);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
bufend = hints + vattr.va_size;
|
|
|
|
recptr = (u_char *)intp;
|
|
|
|
clen = blen = 0;
|
|
|
|
while (recptr < bufend && !found) {
|
|
|
|
intp = (int *)recptr;
|
|
|
|
reclen = *intp++;
|
|
|
|
ival = *intp++;
|
|
|
|
cp = (char *)intp;
|
|
|
|
switch (ival) {
|
|
|
|
case MDT_VERSION:
|
|
|
|
clen = *cp++;
|
|
|
|
if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
|
|
|
|
break;
|
|
|
|
cp += clen;
|
|
|
|
INT_ALIGN(hints, cp);
|
|
|
|
ival = *(int *)cp;
|
|
|
|
cp += sizeof(int);
|
|
|
|
clen = *cp++;
|
|
|
|
if (verinfo == NULL ||
|
|
|
|
ival == verinfo->md_ver_preferred) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ival >= verinfo->md_ver_minimum &&
|
|
|
|
ival <= verinfo->md_ver_maximum &&
|
|
|
|
ival > bestver) {
|
|
|
|
bestver = ival;
|
|
|
|
best = cp;
|
|
|
|
blen = clen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
recptr += reclen + sizeof(int);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Finally check if KLD is in the place
|
2001-09-11 01:09:24 +00:00
|
|
|
*/
|
2002-02-22 04:14:49 +00:00
|
|
|
if (found)
|
|
|
|
result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
|
|
|
|
else if (best)
|
|
|
|
result = linker_lookup_file(path, pathlen, best, blen, &mattr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* KLD is newer than hints file. What we should do now?
|
|
|
|
*/
|
|
|
|
if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
|
|
|
|
printf("warning: KLD '%s' is newer than the linker.hints"
|
|
|
|
" file\n", result);
|
2001-09-11 01:09:24 +00:00
|
|
|
bad:
|
2002-10-15 18:58:38 +00:00
|
|
|
free(pathbuf, M_LINKER);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (hints)
|
|
|
|
free(hints, M_TEMP);
|
2002-07-07 22:35:47 +00:00
|
|
|
if (nd.ni_vp != NULL) {
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0);
|
2002-02-22 04:14:49 +00:00
|
|
|
vn_close(nd.ni_vp, FREAD, cred, td);
|
2002-07-07 22:35:47 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* If nothing found or hints is absent - fallback to the old
|
|
|
|
* way by using "kldname[.ko]" as module name.
|
|
|
|
*/
|
|
|
|
if (!found && !bestver && result == NULL)
|
|
|
|
result = linker_lookup_file(path, pathlen, modname,
|
|
|
|
modnamelen, NULL);
|
|
|
|
return (result);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup KLD which contains requested module in the all directories.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
linker_search_module(const char *modname, int modnamelen,
|
2014-12-03 22:14:13 +00:00
|
|
|
const struct mod_depend *verinfo)
|
2001-09-11 01:09:24 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
char *cp, *ep, *result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* traverse the linker path
|
|
|
|
*/
|
|
|
|
for (cp = linker_path; *cp; cp = ep + 1) {
|
|
|
|
/* find the end of this component */
|
|
|
|
for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
|
2002-02-22 13:43:56 +00:00
|
|
|
result = linker_hints_lookup(cp, ep - cp, modname,
|
|
|
|
modnamelen, verinfo);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (result != NULL)
|
|
|
|
return (result);
|
|
|
|
if (*ep == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (NULL);
|
2001-09-11 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for module in all directories listed in the linker_path.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
linker_search_kld(const char *name)
|
|
|
|
{
|
2006-05-27 09:21:41 +00:00
|
|
|
char *cp, *ep, *result;
|
|
|
|
int len;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
/* qualified at all? */
|
2012-01-02 12:12:10 +00:00
|
|
|
if (strchr(name, '/'))
|
2013-08-16 03:41:41 +00:00
|
|
|
return (strdup(name, M_LINKER));
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
/* traverse the linker path */
|
|
|
|
len = strlen(name);
|
|
|
|
for (ep = linker_path; *ep; ep++) {
|
|
|
|
cp = ep;
|
|
|
|
/* find the end of this component */
|
|
|
|
for (; *ep != 0 && *ep != ';'; ep++);
|
|
|
|
result = linker_lookup_file(cp, ep - cp, name, len, NULL);
|
|
|
|
if (result != NULL)
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
return (NULL);
|
1998-10-10 00:07:53 +00:00
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
|
|
|
|
static const char *
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_basename(const char *path)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
const char *filename;
|
|
|
|
|
2012-01-02 12:12:10 +00:00
|
|
|
filename = strrchr(path, '/');
|
2002-02-22 04:14:49 +00:00
|
|
|
if (filename == NULL)
|
|
|
|
return path;
|
|
|
|
if (filename[1])
|
|
|
|
filename++;
|
|
|
|
return (filename);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
#ifdef HWPMC_HOOKS
|
|
|
|
/*
|
|
|
|
* Inform hwpmc about the set of kernel modules currently loaded.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
linker_hwpmc_list_objects(void)
|
|
|
|
{
|
2009-06-29 16:03:18 +00:00
|
|
|
linker_file_t lf;
|
|
|
|
struct pmckern_map_in *kobase;
|
|
|
|
int i, nmappings;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2009-06-29 16:03:18 +00:00
|
|
|
nmappings = 0;
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_slock(&kld_sx);
|
2009-06-29 16:03:18 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link)
|
|
|
|
nmappings++;
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2009-06-29 16:03:18 +00:00
|
|
|
/* Allocate nmappings + 1 entries. */
|
|
|
|
kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
|
2008-10-23 20:26:15 +00:00
|
|
|
M_LINKER, M_WAITOK | M_ZERO);
|
2009-06-29 16:03:18 +00:00
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2009-06-29 16:03:18 +00:00
|
|
|
/* Save the info for this linker file. */
|
|
|
|
kobase[i].pm_file = lf->filename;
|
|
|
|
kobase[i].pm_address = (uintptr_t)lf->address;
|
|
|
|
i++;
|
2006-03-26 12:20:54 +00:00
|
|
|
}
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_sunlock(&kld_sx);
|
2006-03-26 12:20:54 +00:00
|
|
|
|
2009-06-29 16:03:18 +00:00
|
|
|
KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
/* The last entry of the malloced area comprises of all zeros. */
|
2009-06-29 16:03:18 +00:00
|
|
|
KASSERT(kobase[i].pm_file == NULL,
|
2006-03-26 12:20:54 +00:00
|
|
|
("linker_hwpmc_list_objects: last object not NULL"));
|
|
|
|
|
2009-06-29 16:03:18 +00:00
|
|
|
return ((void *)kobase);
|
2006-03-26 12:20:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-04-29 13:19:31 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Find a file which contains given module and load it, if "parent" is not
|
|
|
|
* NULL, register a reference to it.
|
2000-04-29 13:19:31 +00:00
|
|
|
*/
|
2006-06-20 20:18:42 +00:00
|
|
|
static int
|
2001-09-11 01:09:24 +00:00
|
|
|
linker_load_module(const char *kldname, const char *modname,
|
2014-12-03 22:14:13 +00:00
|
|
|
struct linker_file *parent, const struct mod_depend *verinfo,
|
2002-02-22 04:14:49 +00:00
|
|
|
struct linker_file **lfpp)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lfdep;
|
|
|
|
const char *filename;
|
|
|
|
char *pathname;
|
|
|
|
int error;
|
|
|
|
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (modname == NULL) {
|
|
|
|
/*
|
|
|
|
* We have to load KLD
|
|
|
|
*/
|
2002-02-22 13:43:56 +00:00
|
|
|
KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
|
|
|
|
" is not NULL"));
|
2002-02-22 04:14:49 +00:00
|
|
|
pathname = linker_search_kld(kldname);
|
|
|
|
} else {
|
|
|
|
if (modlist_lookup2(modname, verinfo) != NULL)
|
|
|
|
return (EEXIST);
|
2002-04-10 01:14:45 +00:00
|
|
|
if (kldname != NULL)
|
2013-08-16 03:41:41 +00:00
|
|
|
pathname = strdup(kldname, M_LINKER);
|
2002-04-26 09:52:54 +00:00
|
|
|
else if (rootvnode == NULL)
|
2002-04-10 01:14:45 +00:00
|
|
|
pathname = NULL;
|
|
|
|
else
|
2002-02-22 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* Need to find a KLD with required module
|
|
|
|
*/
|
|
|
|
pathname = linker_search_module(modname,
|
|
|
|
strlen(modname), verinfo);
|
|
|
|
}
|
|
|
|
if (pathname == NULL)
|
|
|
|
return (ENOENT);
|
2000-04-29 13:19:31 +00:00
|
|
|
|
2001-09-11 01:09:24 +00:00
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* Can't load more than one file with the same basename XXX:
|
|
|
|
* Actually it should be possible to have multiple KLDs with
|
|
|
|
* the same basename but different path because they can
|
|
|
|
* provide different versions of the same modules.
|
2001-09-11 01:09:24 +00:00
|
|
|
*/
|
2002-02-22 04:14:49 +00:00
|
|
|
filename = linker_basename(pathname);
|
2006-06-20 19:49:28 +00:00
|
|
|
if (linker_find_file_by_name(filename))
|
2002-02-22 04:14:49 +00:00
|
|
|
error = EEXIST;
|
2006-06-20 19:49:28 +00:00
|
|
|
else do {
|
2002-02-22 04:14:49 +00:00
|
|
|
error = linker_load_file(pathname, &lfdep);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
if (modname && verinfo &&
|
|
|
|
modlist_lookup2(modname, verinfo) == NULL) {
|
2004-07-13 19:36:59 +00:00
|
|
|
linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
|
2002-02-22 04:14:49 +00:00
|
|
|
error = ENOENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (parent) {
|
|
|
|
error = linker_file_add_dependency(parent, lfdep);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lfpp)
|
|
|
|
*lfpp = lfdep;
|
|
|
|
} while (0);
|
2006-06-20 19:32:10 +00:00
|
|
|
free(pathname, M_LINKER);
|
2002-02-22 04:14:49 +00:00
|
|
|
return (error);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-02-22 04:14:49 +00:00
|
|
|
* This routine is responsible for finding dependencies of userland initiated
|
|
|
|
* kldload(2)'s of files.
|
2000-04-29 13:19:31 +00:00
|
|
|
*/
|
|
|
|
int
|
2001-11-16 21:08:40 +00:00
|
|
|
linker_load_dependencies(linker_file_t lf)
|
2000-04-29 13:19:31 +00:00
|
|
|
{
|
2002-02-22 04:14:49 +00:00
|
|
|
linker_file_t lfdep;
|
|
|
|
struct mod_metadata **start, **stop, **mdp, **nmdp;
|
|
|
|
struct mod_metadata *mp, *nmp;
|
2014-12-03 22:14:13 +00:00
|
|
|
const struct mod_depend *verinfo;
|
2002-02-22 04:14:49 +00:00
|
|
|
modlist_t mod;
|
|
|
|
const char *modname, *nmodname;
|
2002-03-10 23:12:43 +00:00
|
|
|
int ver, error = 0, count;
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2016-04-29 22:15:33 +00:00
|
|
|
* All files are dependent on /kernel.
|
2002-02-22 04:14:49 +00:00
|
|
|
*/
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_assert(&kld_sx, SA_XLOCKED);
|
2002-02-22 04:14:49 +00:00
|
|
|
if (linker_kernel_file) {
|
|
|
|
linker_kernel_file->refs++;
|
|
|
|
error = linker_file_add_dependency(lf, linker_kernel_file);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2015-06-08 18:06:00 +00:00
|
|
|
if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
|
2002-02-22 04:14:49 +00:00
|
|
|
&count) != 0)
|
|
|
|
return (0);
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
2015-12-11 05:27:53 +00:00
|
|
|
ver = ((const struct mod_version *)mp->md_data)->mv_version;
|
2002-02-22 04:14:49 +00:00
|
|
|
mod = modlist_lookup(modname, ver);
|
|
|
|
if (mod != NULL) {
|
|
|
|
printf("interface %s.%d already present in the KLD"
|
|
|
|
" '%s'!\n", modname, ver,
|
|
|
|
mod->container->filename);
|
|
|
|
return (EEXIST);
|
|
|
|
}
|
2001-03-22 07:55:33 +00:00
|
|
|
}
|
2002-02-22 04:14:49 +00:00
|
|
|
|
|
|
|
for (mdp = start; mdp < stop; mdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
mp = *mdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (mp->md_type != MDT_DEPEND)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
modname = mp->md_cval;
|
|
|
|
verinfo = mp->md_data;
|
2002-02-22 04:14:49 +00:00
|
|
|
nmodname = NULL;
|
|
|
|
for (nmdp = start; nmdp < stop; nmdp++) {
|
2003-01-21 02:42:44 +00:00
|
|
|
nmp = *nmdp;
|
2002-02-22 04:14:49 +00:00
|
|
|
if (nmp->md_type != MDT_VERSION)
|
|
|
|
continue;
|
2003-01-21 02:42:44 +00:00
|
|
|
nmodname = nmp->md_cval;
|
2002-03-10 23:12:43 +00:00
|
|
|
if (strcmp(modname, nmodname) == 0)
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nmdp < stop)/* early exit, it's a self reference */
|
|
|
|
continue;
|
|
|
|
mod = modlist_lookup2(modname, verinfo);
|
|
|
|
if (mod) { /* woohoo, it's loaded already */
|
|
|
|
lfdep = mod->container;
|
|
|
|
lfdep->refs++;
|
|
|
|
error = linker_file_add_dependency(lf, lfdep);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
error = linker_load_module(NULL, modname, lf, verinfo, NULL);
|
|
|
|
if (error) {
|
2012-02-20 09:32:32 +00:00
|
|
|
printf("KLD %s: depends on %s - not available or"
|
2009-07-21 14:18:25 +00:00
|
|
|
" version mismatch\n", lf->filename, modname);
|
2002-02-22 04:14:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2001-03-22 07:55:33 +00:00
|
|
|
|
2002-02-22 04:14:49 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
linker_addmodules(lf, start, stop, 0);
|
|
|
|
return (error);
|
2000-04-29 13:19:31 +00:00
|
|
|
}
|
2001-10-30 15:21:45 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_kern_function_list_iterate(const char *name, void *opaque)
|
|
|
|
{
|
|
|
|
struct sysctl_req *req;
|
|
|
|
|
|
|
|
req = opaque;
|
|
|
|
return (SYSCTL_OUT(req, name, strlen(name) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export a nul-separated, double-nul-terminated list of all function names
|
|
|
|
* in the kernel.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
linker_file_t lf;
|
|
|
|
int error;
|
|
|
|
|
2002-11-19 22:12:42 +00:00
|
|
|
#ifdef MAC
|
2007-10-24 19:04:04 +00:00
|
|
|
error = mac_kld_check_stat(req->td->td_ucred);
|
2002-11-19 22:12:42 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
#endif
|
2004-02-26 00:27:04 +00:00
|
|
|
error = sysctl_wire_old_buffer(req, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xlock(&kld_sx);
|
2001-10-30 15:21:45 +00:00
|
|
|
TAILQ_FOREACH(lf, &linker_files, link) {
|
|
|
|
error = LINKER_EACH_FUNCTION_NAME(lf,
|
|
|
|
sysctl_kern_function_list_iterate, req);
|
2002-06-19 21:25:59 +00:00
|
|
|
if (error) {
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2001-10-30 15:21:45 +00:00
|
|
|
return (error);
|
2002-06-19 21:25:59 +00:00
|
|
|
}
|
2001-10-30 15:21:45 +00:00
|
|
|
}
|
2013-08-24 21:07:04 +00:00
|
|
|
sx_xunlock(&kld_sx);
|
2001-10-30 15:21:45 +00:00
|
|
|
return (SYSCTL_OUT(req, "", 1));
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:14:18 +00:00
|
|
|
SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD,
|
2002-02-22 04:14:49 +00:00
|
|
|
NULL, 0, sysctl_kern_function_list, "", "kernel function list");
|