kern_linker.c revision 159587
1/*-
2 * Copyright (c) 1997-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_linker.c 159587 2006-06-13 21:11:12Z jhb $");
29
30#include "opt_ddb.h"
31#include "opt_hwpmc_hooks.h"
32#include "opt_mac.h"
33
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/sysproto.h>
39#include <sys/sysent.h>
40#include <sys/proc.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/sx.h>
44#include <sys/mac.h>
45#include <sys/module.h>
46#include <sys/linker.h>
47#include <sys/fcntl.h>
48#include <sys/libkern.h>
49#include <sys/namei.h>
50#include <sys/vnode.h>
51#include <sys/sysctl.h>
52
53#include "linker_if.h"
54
55#ifdef HWPMC_HOOKS
56#include <sys/pmckern.h>
57#endif
58
59#ifdef KLD_DEBUG
60int kld_debug = 0;
61#endif
62
63/*
64 * static char *linker_search_path(const char *name, struct mod_depend
65 * *verinfo);
66 */
67static const char 	*linker_basename(const char *path);
68
69/* Metadata from the static kernel */
70SET_DECLARE(modmetadata_set, struct mod_metadata);
71
72MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
73
74linker_file_t linker_kernel_file;
75
76static struct mtx kld_mtx;	/* kernel linker mutex */
77
78static linker_class_list_t classes;
79static linker_file_list_t linker_files;
80static int next_file_id = 1;
81static int linker_no_more_classes = 0;
82
83#define	LINKER_GET_NEXT_FILE_ID(a) do {					\
84	linker_file_t lftmp;						\
85									\
86retry:									\
87	mtx_lock(&kld_mtx);						\
88	TAILQ_FOREACH(lftmp, &linker_files, link) {			\
89		if (next_file_id == lftmp->id) {			\
90			next_file_id++;					\
91			mtx_unlock(&kld_mtx);				\
92			goto retry;					\
93		}							\
94	}								\
95	(a) = next_file_id;						\
96	mtx_unlock(&kld_mtx);	/* Hold for safe read of id variable */	\
97} while(0)
98
99
100/* XXX wrong name; we're looking at version provision tags here, not modules */
101typedef TAILQ_HEAD(, modlist) modlisthead_t;
102struct modlist {
103	TAILQ_ENTRY(modlist) link;	/* chain together all modules */
104	linker_file_t   container;
105	const char 	*name;
106	int             version;
107};
108typedef struct modlist *modlist_t;
109static modlisthead_t found_modules;
110
111static modlist_t	modlist_lookup2(const char *name,
112			    struct mod_depend *verinfo);
113
114static char *
115linker_strdup(const char *str)
116{
117	char *result;
118
119	if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
120		strcpy(result, str);
121	return (result);
122}
123
124static void
125linker_init(void *arg)
126{
127
128	mtx_init(&kld_mtx, "kernel linker", NULL, MTX_DEF);
129	TAILQ_INIT(&classes);
130	TAILQ_INIT(&linker_files);
131}
132
133SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
134
135static void
136linker_stop_class_add(void *arg)
137{
138
139	linker_no_more_classes = 1;
140}
141
142SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL)
143
144int
145linker_add_class(linker_class_t lc)
146{
147
148	/*
149	 * We disallow any class registration past SI_ORDER_ANY
150	 * of SI_SUB_KLD.  We bump the reference count to keep the
151	 * ops from being freed.
152	 */
153	if (linker_no_more_classes == 1)
154		return (EPERM);
155	kobj_class_compile((kobj_class_t) lc);
156	((kobj_class_t)lc)->refs++;	/* XXX: kobj_mtx */
157	TAILQ_INSERT_TAIL(&classes, lc, link);
158	return (0);
159}
160
161static void
162linker_file_sysinit(linker_file_t lf)
163{
164	struct sysinit **start, **stop, **sipp, **xipp, *save;
165
166	KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
167	    lf->filename));
168
169	if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
170		return;
171	/*
172	 * Perform a bubble sort of the system initialization objects by
173	 * their subsystem (primary key) and order (secondary key).
174	 *
175	 * Since some things care about execution order, this is the operation
176	 * which ensures continued function.
177	 */
178	for (sipp = start; sipp < stop; sipp++) {
179		for (xipp = sipp + 1; xipp < stop; xipp++) {
180			if ((*sipp)->subsystem < (*xipp)->subsystem ||
181			    ((*sipp)->subsystem == (*xipp)->subsystem &&
182			    (*sipp)->order <= (*xipp)->order))
183				continue;	/* skip */
184			save = *sipp;
185			*sipp = *xipp;
186			*xipp = save;
187		}
188	}
189
190	/*
191	 * Traverse the (now) ordered list of system initialization tasks.
192	 * Perform each task, and continue on to the next task.
193	 */
194	for (sipp = start; sipp < stop; sipp++) {
195		if ((*sipp)->subsystem == SI_SUB_DUMMY)
196			continue;	/* skip dummy task(s) */
197
198		/* Call function */
199		(*((*sipp)->func)) ((*sipp)->udata);
200	}
201}
202
203static void
204linker_file_sysuninit(linker_file_t lf)
205{
206	struct sysinit **start, **stop, **sipp, **xipp, *save;
207
208	KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
209	    lf->filename));
210
211	if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
212	    NULL) != 0)
213		return;
214
215	/*
216	 * Perform a reverse bubble sort of the system initialization objects
217	 * by their subsystem (primary key) and order (secondary key).
218	 *
219	 * Since some things care about execution order, this is the operation
220	 * which ensures continued function.
221	 */
222	for (sipp = start; sipp < stop; sipp++) {
223		for (xipp = sipp + 1; xipp < stop; xipp++) {
224			if ((*sipp)->subsystem > (*xipp)->subsystem ||
225			    ((*sipp)->subsystem == (*xipp)->subsystem &&
226			    (*sipp)->order >= (*xipp)->order))
227				continue;	/* skip */
228			save = *sipp;
229			*sipp = *xipp;
230			*xipp = save;
231		}
232	}
233
234	/*
235	 * Traverse the (now) ordered list of system initialization tasks.
236	 * Perform each task, and continue on to the next task.
237	 */
238	for (sipp = start; sipp < stop; sipp++) {
239		if ((*sipp)->subsystem == SI_SUB_DUMMY)
240			continue;	/* skip dummy task(s) */
241
242		/* Call function */
243		(*((*sipp)->func)) ((*sipp)->udata);
244	}
245}
246
247static void
248linker_file_register_sysctls(linker_file_t lf)
249{
250	struct sysctl_oid **start, **stop, **oidp;
251
252	KLD_DPF(FILE,
253	    ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
254	    lf->filename));
255
256	if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
257		return;
258
259	for (oidp = start; oidp < stop; oidp++)
260		sysctl_register_oid(*oidp);
261}
262
263static void
264linker_file_unregister_sysctls(linker_file_t lf)
265{
266	struct sysctl_oid **start, **stop, **oidp;
267
268	KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
269	    " for %s\n", lf->filename));
270
271	if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
272		return;
273
274	for (oidp = start; oidp < stop; oidp++)
275		sysctl_unregister_oid(*oidp);
276}
277
278static int
279linker_file_register_modules(linker_file_t lf)
280{
281	struct mod_metadata **start, **stop, **mdp;
282	const moduledata_t *moddata;
283	int first_error, error;
284
285	KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
286	    " in %s\n", lf->filename));
287
288	if (linker_file_lookup_set(lf, "modmetadata_set", &start,
289	    &stop, 0) != 0) {
290		/*
291		 * This fallback should be unnecessary, but if we get booted
292		 * from boot2 instead of loader and we are missing our
293		 * metadata then we have to try the best we can.
294		 */
295		if (lf == linker_kernel_file) {
296			start = SET_BEGIN(modmetadata_set);
297			stop = SET_LIMIT(modmetadata_set);
298		} else
299			return (0);
300	}
301	first_error = 0;
302	for (mdp = start; mdp < stop; mdp++) {
303		if ((*mdp)->md_type != MDT_MODULE)
304			continue;
305		moddata = (*mdp)->md_data;
306		KLD_DPF(FILE, ("Registering module %s in %s\n",
307		    moddata->name, lf->filename));
308		error = module_register(moddata, lf);
309		if (error) {
310			printf("Module %s failed to register: %d\n",
311			    moddata->name, error);
312			if (first_error == 0)
313				first_error = error;
314		}
315	}
316	return (first_error);
317}
318
319static void
320linker_init_kernel_modules(void)
321{
322
323	linker_file_register_modules(linker_kernel_file);
324}
325
326SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0)
327
328static int
329linker_load_file(const char *filename, linker_file_t *result)
330{
331	linker_class_t lc;
332	linker_file_t lf;
333	int foundfile, error;
334
335	/* Refuse to load modules if securelevel raised */
336	if (securelevel > 0)
337		return (EPERM);
338
339	lf = linker_find_file_by_name(filename);
340	if (lf) {
341		KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
342		    " incrementing refs\n", filename));
343		*result = lf;
344		lf->refs++;
345		return (0);
346	}
347	lf = NULL;
348	foundfile = 0;
349	error = 0;
350
351	/*
352	 * We do not need to protect (lock) classes here because there is
353	 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
354	 * and there is no class deregistration mechanism at this time.
355	 */
356	TAILQ_FOREACH(lc, &classes, link) {
357		KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
358		    filename));
359		error = LINKER_LOAD_FILE(lc, filename, &lf);
360		/*
361		 * If we got something other than ENOENT, then it exists but
362		 * we cannot load it for some other reason.
363		 */
364		if (error != ENOENT)
365			foundfile = 1;
366		if (lf) {
367			error = linker_file_register_modules(lf);
368			if (error == EEXIST) {
369				linker_file_unload(lf, LINKER_UNLOAD_FORCE);
370				return (error);
371			}
372			linker_file_register_sysctls(lf);
373			linker_file_sysinit(lf);
374			lf->flags |= LINKER_FILE_LINKED;
375			*result = lf;
376			return (0);
377		}
378	}
379	/*
380	 * Less than ideal, but tells the user whether it failed to load or
381	 * the module was not found.
382	 */
383	if (foundfile) {
384		/*
385		 * Format not recognized or otherwise unloadable.
386		 * When loading a module that is statically built into
387		 * the kernel EEXIST percolates back up as the return
388		 * value.  Preserve this so that apps like sysinstall
389		 * can recognize this special case and not post bogus
390		 * dialog boxes.
391		 */
392		if (error != EEXIST)
393			error = ENOEXEC;
394	} else
395		error = ENOENT;		/* Nothing found */
396	return (error);
397}
398
399int
400linker_reference_module(const char *modname, struct mod_depend *verinfo,
401    linker_file_t *result)
402{
403	modlist_t mod;
404
405	if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
406		*result = mod->container;
407		(*result)->refs++;
408		return (0);
409	}
410
411	return (linker_load_module(NULL, modname, NULL, verinfo, result));
412}
413
414linker_file_t
415linker_find_file_by_name(const char *filename)
416{
417	linker_file_t lf;
418	char *koname;
419
420	koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
421	sprintf(koname, "%s.ko", filename);
422
423	mtx_lock(&kld_mtx);
424	TAILQ_FOREACH(lf, &linker_files, link) {
425		if (strcmp(lf->filename, koname) == 0)
426			break;
427		if (strcmp(lf->filename, filename) == 0)
428			break;
429	}
430	mtx_unlock(&kld_mtx);
431	free(koname, M_LINKER);
432	return (lf);
433}
434
435linker_file_t
436linker_find_file_by_id(int fileid)
437{
438	linker_file_t lf;
439
440	mtx_lock(&kld_mtx);
441	TAILQ_FOREACH(lf, &linker_files, link)
442		if (lf->id == fileid)
443			break;
444	mtx_unlock(&kld_mtx);
445	return (lf);
446}
447
448linker_file_t
449linker_make_file(const char *pathname, linker_class_t lc)
450{
451	linker_file_t lf;
452	const char *filename;
453
454	filename = linker_basename(pathname);
455
456	KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
457	lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
458	if (lf == NULL)
459		return (NULL);
460	lf->refs = 1;
461	lf->userrefs = 0;
462	lf->flags = 0;
463	lf->filename = linker_strdup(filename);
464	LINKER_GET_NEXT_FILE_ID(lf->id);
465	lf->ndeps = 0;
466	lf->deps = NULL;
467	STAILQ_INIT(&lf->common);
468	TAILQ_INIT(&lf->modules);
469	mtx_lock(&kld_mtx);
470	TAILQ_INSERT_TAIL(&linker_files, lf, link);
471	mtx_unlock(&kld_mtx);
472	return (lf);
473}
474
475int
476linker_file_unload(linker_file_t file, int flags)
477{
478	module_t mod, next;
479	modlist_t ml, nextml;
480	struct common_symbol *cp;
481	int error, i;
482
483	/* Refuse to unload modules if securelevel raised. */
484	if (securelevel > 0)
485		return (EPERM);
486#ifdef MAC
487	error = mac_check_kld_unload(curthread->td_ucred);
488	if (error)
489		return (error);
490#endif
491
492	KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
493
494	/* Easy case of just dropping a reference. */
495	if (file->refs > 1) {
496		file->refs--;
497		return (0);
498	}
499
500	KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
501	    " informing modules\n"));
502
503	/*
504	 * Inform any modules associated with this file.
505	 */
506	MOD_XLOCK;
507	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
508		next = module_getfnext(mod);
509		MOD_XUNLOCK;
510
511		/*
512		 * Give the module a chance to veto the unload.
513		 */
514		if ((error = module_unload(mod, flags)) != 0) {
515			KLD_DPF(FILE, ("linker_file_unload: module %p"
516			    " vetoes unload\n", mod));
517			return (error);
518		}
519		MOD_XLOCK;
520		module_release(mod);
521	}
522	MOD_XUNLOCK;
523
524	TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
525		if (ml->container == file) {
526			TAILQ_REMOVE(&found_modules, ml, link);
527			free(ml, M_LINKER);
528		}
529	}
530
531	/*
532	 * Don't try to run SYSUNINITs if we are unloaded due to a
533	 * link error.
534	 */
535	if (file->flags & LINKER_FILE_LINKED) {
536		linker_file_sysuninit(file);
537		linker_file_unregister_sysctls(file);
538	}
539	mtx_lock(&kld_mtx);
540	TAILQ_REMOVE(&linker_files, file, link);
541	mtx_unlock(&kld_mtx);
542
543	if (file->deps) {
544		for (i = 0; i < file->ndeps; i++)
545			linker_file_unload(file->deps[i], flags);
546		free(file->deps, M_LINKER);
547		file->deps = NULL;
548	}
549	for (cp = STAILQ_FIRST(&file->common); cp;
550	    cp = STAILQ_FIRST(&file->common)) {
551		STAILQ_REMOVE(&file->common, cp, common_symbol, link);
552		free(cp, M_LINKER);
553	}
554
555	LINKER_UNLOAD(file);
556	if (file->filename) {
557		free(file->filename, M_LINKER);
558		file->filename = NULL;
559	}
560	kobj_delete((kobj_t) file, M_LINKER);
561	return (0);
562}
563
564int
565linker_file_add_dependency(linker_file_t file, linker_file_t dep)
566{
567	linker_file_t *newdeps;
568
569	newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
570	    M_LINKER, M_WAITOK | M_ZERO);
571	if (newdeps == NULL)
572		return (ENOMEM);
573
574	if (file->deps) {
575		bcopy(file->deps, newdeps,
576		    file->ndeps * sizeof(linker_file_t *));
577		free(file->deps, M_LINKER);
578	}
579	file->deps = newdeps;
580	file->deps[file->ndeps] = dep;
581	file->ndeps++;
582	return (0);
583}
584
585/*
586 * Locate a linker set and its contents.  This is a helper function to avoid
587 * linker_if.h exposure elsewhere.  Note: firstp and lastp are really void ***
588 */
589int
590linker_file_lookup_set(linker_file_t file, const char *name,
591    void *firstp, void *lastp, int *countp)
592{
593
594	return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
595}
596
597caddr_t
598linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
599{
600	c_linker_sym_t sym;
601	linker_symval_t symval;
602	caddr_t address;
603	size_t common_size = 0;
604	int i;
605
606	KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
607	    file, name, deps));
608
609	if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
610		LINKER_SYMBOL_VALUES(file, sym, &symval);
611		if (symval.value == 0)
612			/*
613			 * For commons, first look them up in the
614			 * dependencies and only allocate space if not found
615			 * there.
616			 */
617			common_size = symval.size;
618		else {
619			KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
620			    ".value=%p\n", symval.value));
621			return (symval.value);
622		}
623	}
624	if (deps) {
625		for (i = 0; i < file->ndeps; i++) {
626			address = linker_file_lookup_symbol(file->deps[i],
627			    name, 0);
628			if (address) {
629				KLD_DPF(SYM, ("linker_file_lookup_symbol:"
630				    " deps value=%p\n", address));
631				return (address);
632			}
633		}
634	}
635	if (common_size > 0) {
636		/*
637		 * This is a common symbol which was not found in the
638		 * dependencies.  We maintain a simple common symbol table in
639		 * the file object.
640		 */
641		struct common_symbol *cp;
642
643		STAILQ_FOREACH(cp, &file->common, link) {
644			if (strcmp(cp->name, name) == 0) {
645				KLD_DPF(SYM, ("linker_file_lookup_symbol:"
646				    " old common value=%p\n", cp->address));
647				return (cp->address);
648			}
649		}
650		/*
651		 * Round the symbol size up to align.
652		 */
653		common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
654		cp = malloc(sizeof(struct common_symbol)
655		    + common_size + strlen(name) + 1, M_LINKER,
656		    M_WAITOK | M_ZERO);
657		if (cp == NULL) {
658			KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
659			return (0);
660		}
661		cp->address = (caddr_t)(cp + 1);
662		cp->name = cp->address + common_size;
663		strcpy(cp->name, name);
664		bzero(cp->address, common_size);
665		STAILQ_INSERT_TAIL(&file->common, cp, link);
666
667		KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
668		    " value=%p\n", cp->address));
669		return (cp->address);
670	}
671	KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
672	return (0);
673}
674
675#ifdef DDB
676/*
677 * DDB Helpers.  DDB has to look across multiple files with their own symbol
678 * tables and string tables.
679 *
680 * Note that we do not obey list locking protocols here.  We really don't need
681 * DDB to hang because somebody's got the lock held.  We'll take the chance
682 * that the files list is inconsistant instead.
683 */
684
685int
686linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
687{
688	linker_file_t lf;
689
690	TAILQ_FOREACH(lf, &linker_files, link) {
691		if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
692			return (0);
693	}
694	return (ENOENT);
695}
696
697int
698linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
699{
700	linker_file_t lf;
701	c_linker_sym_t best, es;
702	u_long diff, bestdiff, off;
703
704	best = 0;
705	off = (uintptr_t)value;
706	bestdiff = off;
707	TAILQ_FOREACH(lf, &linker_files, link) {
708		if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
709			continue;
710		if (es != 0 && diff < bestdiff) {
711			best = es;
712			bestdiff = diff;
713		}
714		if (bestdiff == 0)
715			break;
716	}
717	if (best) {
718		*sym = best;
719		*diffp = bestdiff;
720		return (0);
721	} else {
722		*sym = 0;
723		*diffp = off;
724		return (ENOENT);
725	}
726}
727
728int
729linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
730{
731	linker_file_t lf;
732
733	TAILQ_FOREACH(lf, &linker_files, link) {
734		if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
735			return (0);
736	}
737	return (ENOENT);
738}
739#endif
740
741/*
742 * Syscalls.
743 */
744/*
745 * MPSAFE
746 */
747int
748kldload(struct thread *td, struct kldload_args *uap)
749{
750#ifdef HWPMC_HOOKS
751	struct pmckern_map_in pkm;
752#endif
753	char *kldname, *modname;
754	char *pathname = NULL;
755	linker_file_t lf;
756	int error = 0;
757
758	td->td_retval[0] = -1;
759
760	mtx_lock(&Giant);
761
762	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
763		goto out;
764
765	if ((error = suser(td)) != 0)
766		goto out;
767
768	pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
769	if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
770		goto out;
771
772	/*
773	 * If path do not contain qualified name or any dot in it
774	 * (kldname.ko, or kldname.ver.ko) treat it as interface
775	 * name.
776	 */
777	if (index(pathname, '/') || index(pathname, '.')) {
778		kldname = pathname;
779		modname = NULL;
780	} else {
781		kldname = NULL;
782		modname = pathname;
783	}
784	error = linker_load_module(kldname, modname, NULL, NULL, &lf);
785	if (error)
786		goto out;
787
788#ifdef HWPMC_HOOKS
789	pkm.pm_file = lf->filename;
790	pkm.pm_address = (uintptr_t) lf->address;
791	PMC_CALL_HOOK(td, PMC_FN_KLD_LOAD, (void *) &pkm);
792#endif
793	lf->userrefs++;
794	td->td_retval[0] = lf->id;
795out:
796	if (pathname)
797		free(pathname, M_TEMP);
798	mtx_unlock(&Giant);
799	return (error);
800}
801
802/*
803 * MPSAFE
804 */
805static int
806kern_kldunload(struct thread *td, int fileid, int flags)
807{
808#ifdef HWPMC_HOOKS
809	struct pmckern_map_out pkm;
810#endif
811	linker_file_t lf;
812	int error = 0;
813
814	mtx_lock(&Giant);
815
816	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
817		goto out;
818
819	if ((error = suser(td)) != 0)
820		goto out;
821
822	lf = linker_find_file_by_id(fileid);
823	if (lf) {
824		KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
825		if (lf->userrefs == 0) {
826			/*
827			 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
828			 */
829			printf("kldunload: attempt to unload file that was"
830			    " loaded by the kernel\n");
831			error = EBUSY;
832			goto out;
833		}
834		lf->userrefs--;
835#ifdef HWPMC_HOOKS
836		/* Save data needed by hwpmc(4) before unloading the kld. */
837		pkm.pm_address = (uintptr_t) lf->address;
838		pkm.pm_size = lf->size;
839#endif
840		error = linker_file_unload(lf, flags);
841		if (error)
842			lf->userrefs++;
843	} else
844		error = ENOENT;
845
846#ifdef HWPMC_HOOKS
847	if (error == 0)
848		PMC_CALL_HOOK(td, PMC_FN_KLD_UNLOAD, (void *) &pkm);
849#endif
850out:
851	mtx_unlock(&Giant);
852	return (error);
853}
854
855/*
856 * MPSAFE
857 */
858int
859kldunload(struct thread *td, struct kldunload_args *uap)
860{
861
862	return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
863}
864
865/*
866 * MPSAFE
867 */
868int
869kldunloadf(struct thread *td, struct kldunloadf_args *uap)
870{
871
872	if (uap->flags != LINKER_UNLOAD_NORMAL &&
873	    uap->flags != LINKER_UNLOAD_FORCE)
874		return (EINVAL);
875	return (kern_kldunload(td, uap->fileid, uap->flags));
876}
877
878/*
879 * MPSAFE
880 */
881int
882kldfind(struct thread *td, struct kldfind_args *uap)
883{
884	char *pathname;
885	const char *filename;
886	linker_file_t lf;
887	int error = 0;
888
889#ifdef MAC
890	error = mac_check_kld_stat(td->td_ucred);
891	if (error)
892		return (error);
893#endif
894
895	mtx_lock(&Giant);
896	td->td_retval[0] = -1;
897
898	pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
899	if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
900		goto out;
901
902	filename = linker_basename(pathname);
903	lf = linker_find_file_by_name(filename);
904	if (lf)
905		td->td_retval[0] = lf->id;
906	else
907		error = ENOENT;
908out:
909	if (pathname)
910		free(pathname, M_TEMP);
911	mtx_unlock(&Giant);
912	return (error);
913}
914
915/*
916 * MPSAFE
917 */
918int
919kldnext(struct thread *td, struct kldnext_args *uap)
920{
921	linker_file_t lf;
922	int error = 0;
923
924#ifdef MAC
925	error = mac_check_kld_stat(td->td_ucred);
926	if (error)
927		return (error);
928#endif
929
930	mtx_lock(&Giant);
931
932	if (uap->fileid == 0) {
933		mtx_lock(&kld_mtx);
934		if (TAILQ_FIRST(&linker_files))
935			td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
936		else
937			td->td_retval[0] = 0;
938		mtx_unlock(&kld_mtx);
939		goto out;
940	}
941	lf = linker_find_file_by_id(uap->fileid);
942	if (lf) {
943		if (TAILQ_NEXT(lf, link))
944			td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
945		else
946			td->td_retval[0] = 0;
947	} else
948		error = ENOENT;
949out:
950	mtx_unlock(&Giant);
951	return (error);
952}
953
954/*
955 * MPSAFE
956 */
957int
958kldstat(struct thread *td, struct kldstat_args *uap)
959{
960	struct kld_file_stat stat;
961	linker_file_t lf;
962	int error, namelen;
963
964	/*
965	 * Check the version of the user's structure.
966	 */
967	error = copyin(uap->stat, &stat, sizeof(struct kld_file_stat));
968	if (error)
969		return (error);
970	if (stat.version != sizeof(struct kld_file_stat))
971		return (EINVAL);
972
973#ifdef MAC
974	error = mac_check_kld_stat(td->td_ucred);
975	if (error)
976		return (error);
977#endif
978
979	mtx_lock(&Giant);
980
981	lf = linker_find_file_by_id(uap->fileid);
982	if (lf == NULL) {
983		mtx_unlock(&Giant);
984		return (ENOENT);
985	}
986
987	namelen = strlen(lf->filename) + 1;
988	if (namelen > MAXPATHLEN)
989		namelen = MAXPATHLEN;
990	bcopy(lf->filename, &stat.name[0], namelen);
991	stat.refs = lf->refs;
992	stat.id = lf->id;
993	stat.address = lf->address;
994	stat.size = lf->size;
995	mtx_unlock(&Giant);
996
997	td->td_retval[0] = 0;
998
999	return (copyout(&stat, uap->stat, sizeof(struct kld_file_stat)));
1000}
1001
1002/*
1003 * MPSAFE
1004 */
1005int
1006kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1007{
1008	linker_file_t lf;
1009	module_t mp;
1010	int error = 0;
1011
1012#ifdef MAC
1013	error = mac_check_kld_stat(td->td_ucred);
1014	if (error)
1015		return (error);
1016#endif
1017
1018	mtx_lock(&Giant);
1019	lf = linker_find_file_by_id(uap->fileid);
1020	if (lf) {
1021		MOD_SLOCK;
1022		mp = TAILQ_FIRST(&lf->modules);
1023		if (mp != NULL)
1024			td->td_retval[0] = module_getid(mp);
1025		else
1026			td->td_retval[0] = 0;
1027		MOD_SUNLOCK;
1028	} else
1029		error = ENOENT;
1030	mtx_unlock(&Giant);
1031	return (error);
1032}
1033
1034/*
1035 * MPSAFE
1036 */
1037int
1038kldsym(struct thread *td, struct kldsym_args *uap)
1039{
1040	char *symstr = NULL;
1041	c_linker_sym_t sym;
1042	linker_symval_t symval;
1043	linker_file_t lf;
1044	struct kld_sym_lookup lookup;
1045	int error = 0;
1046
1047#ifdef MAC
1048	error = mac_check_kld_stat(td->td_ucred);
1049	if (error)
1050		return (error);
1051#endif
1052
1053	mtx_lock(&Giant);
1054
1055	if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1056		goto out;
1057	if (lookup.version != sizeof(lookup) ||
1058	    uap->cmd != KLDSYM_LOOKUP) {
1059		error = EINVAL;
1060		goto out;
1061	}
1062	symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1063	if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1064		goto out;
1065	if (uap->fileid != 0) {
1066		lf = linker_find_file_by_id(uap->fileid);
1067		if (lf == NULL) {
1068			error = ENOENT;
1069			goto out;
1070		}
1071		if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1072		    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1073			lookup.symvalue = (uintptr_t) symval.value;
1074			lookup.symsize = symval.size;
1075			error = copyout(&lookup, uap->data, sizeof(lookup));
1076		} else
1077			error = ENOENT;
1078	} else {
1079		mtx_lock(&kld_mtx);
1080		TAILQ_FOREACH(lf, &linker_files, link) {
1081			if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1082			    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1083				lookup.symvalue = (uintptr_t)symval.value;
1084				lookup.symsize = symval.size;
1085				error = copyout(&lookup, uap->data,
1086				    sizeof(lookup));
1087				break;
1088			}
1089		}
1090		mtx_unlock(&kld_mtx);
1091		if (lf == NULL)
1092			error = ENOENT;
1093	}
1094out:
1095	if (symstr)
1096		free(symstr, M_TEMP);
1097	mtx_unlock(&Giant);
1098	return (error);
1099}
1100
1101/*
1102 * Preloaded module support
1103 */
1104
1105static modlist_t
1106modlist_lookup(const char *name, int ver)
1107{
1108	modlist_t mod;
1109
1110	TAILQ_FOREACH(mod, &found_modules, link) {
1111		if (strcmp(mod->name, name) == 0 &&
1112		    (ver == 0 || mod->version == ver))
1113			return (mod);
1114	}
1115	return (NULL);
1116}
1117
1118static modlist_t
1119modlist_lookup2(const char *name, struct mod_depend *verinfo)
1120{
1121	modlist_t mod, bestmod;
1122	int ver;
1123
1124	if (verinfo == NULL)
1125		return (modlist_lookup(name, 0));
1126	bestmod = NULL;
1127	TAILQ_FOREACH(mod, &found_modules, link) {
1128		if (strcmp(mod->name, name) != 0)
1129			continue;
1130		ver = mod->version;
1131		if (ver == verinfo->md_ver_preferred)
1132			return (mod);
1133		if (ver >= verinfo->md_ver_minimum &&
1134		    ver <= verinfo->md_ver_maximum &&
1135		    (bestmod == NULL || ver > bestmod->version))
1136			bestmod = mod;
1137	}
1138	return (bestmod);
1139}
1140
1141static modlist_t
1142modlist_newmodule(const char *modname, int version, linker_file_t container)
1143{
1144	modlist_t mod;
1145
1146	mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1147	if (mod == NULL)
1148		panic("no memory for module list");
1149	mod->container = container;
1150	mod->name = modname;
1151	mod->version = version;
1152	TAILQ_INSERT_TAIL(&found_modules, mod, link);
1153	return (mod);
1154}
1155
1156static void
1157linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1158    struct mod_metadata **stop, int preload)
1159{
1160	struct mod_metadata *mp, **mdp;
1161	const char *modname;
1162	int ver;
1163
1164	for (mdp = start; mdp < stop; mdp++) {
1165		mp = *mdp;
1166		if (mp->md_type != MDT_VERSION)
1167			continue;
1168		modname = mp->md_cval;
1169		ver = ((struct mod_version *)mp->md_data)->mv_version;
1170		if (modlist_lookup(modname, ver) != NULL) {
1171			printf("module %s already present!\n", modname);
1172			/* XXX what can we do? this is a build error. :-( */
1173			continue;
1174		}
1175		modlist_newmodule(modname, ver, lf);
1176	}
1177}
1178
1179static void
1180linker_preload(void *arg)
1181{
1182	caddr_t modptr;
1183	const char *modname, *nmodname;
1184	char *modtype;
1185	linker_file_t lf;
1186	linker_class_t lc;
1187	int error;
1188	linker_file_list_t loaded_files;
1189	linker_file_list_t depended_files;
1190	struct mod_metadata *mp, *nmp;
1191	struct mod_metadata **start, **stop, **mdp, **nmdp;
1192	struct mod_depend *verinfo;
1193	int nver;
1194	int resolves;
1195	modlist_t mod;
1196	struct sysinit **si_start, **si_stop;
1197
1198	TAILQ_INIT(&loaded_files);
1199	TAILQ_INIT(&depended_files);
1200	TAILQ_INIT(&found_modules);
1201	error = 0;
1202
1203	modptr = NULL;
1204	while ((modptr = preload_search_next_name(modptr)) != NULL) {
1205		modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1206		modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1207		if (modname == NULL) {
1208			printf("Preloaded module at %p does not have a"
1209			    " name!\n", modptr);
1210			continue;
1211		}
1212		if (modtype == NULL) {
1213			printf("Preloaded module at %p does not have a type!\n",
1214			    modptr);
1215			continue;
1216		}
1217		if (bootverbose)
1218			printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1219			    modptr);
1220		lf = NULL;
1221		TAILQ_FOREACH(lc, &classes, link) {
1222			error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1223			if (!error)
1224				break;
1225			lf = NULL;
1226		}
1227		if (lf)
1228			TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1229	}
1230
1231	/*
1232	 * First get a list of stuff in the kernel.
1233	 */
1234	if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1235	    &stop, NULL) == 0)
1236		linker_addmodules(linker_kernel_file, start, stop, 1);
1237
1238	/*
1239	 * this is a once-off kinky bubble sort resolve relocation dependency
1240	 * requirements
1241	 */
1242restart:
1243	TAILQ_FOREACH(lf, &loaded_files, loaded) {
1244		error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1245		    &stop, NULL);
1246		/*
1247		 * First, look to see if we would successfully link with this
1248		 * stuff.
1249		 */
1250		resolves = 1;	/* unless we know otherwise */
1251		if (!error) {
1252			for (mdp = start; mdp < stop; mdp++) {
1253				mp = *mdp;
1254				if (mp->md_type != MDT_DEPEND)
1255					continue;
1256				modname = mp->md_cval;
1257				verinfo = mp->md_data;
1258				for (nmdp = start; nmdp < stop; nmdp++) {
1259					nmp = *nmdp;
1260					if (nmp->md_type != MDT_VERSION)
1261						continue;
1262					nmodname = nmp->md_cval;
1263					if (strcmp(modname, nmodname) == 0)
1264						break;
1265				}
1266				if (nmdp < stop)   /* it's a self reference */
1267					continue;
1268
1269				/*
1270				 * ok, the module isn't here yet, we
1271				 * are not finished
1272				 */
1273				if (modlist_lookup2(modname, verinfo) == NULL)
1274					resolves = 0;
1275			}
1276		}
1277		/*
1278		 * OK, if we found our modules, we can link.  So, "provide"
1279		 * the modules inside and add it to the end of the link order
1280		 * list.
1281		 */
1282		if (resolves) {
1283			if (!error) {
1284				for (mdp = start; mdp < stop; mdp++) {
1285					mp = *mdp;
1286					if (mp->md_type != MDT_VERSION)
1287						continue;
1288					modname = mp->md_cval;
1289					nver = ((struct mod_version *)
1290					    mp->md_data)->mv_version;
1291					if (modlist_lookup(modname,
1292					    nver) != NULL) {
1293						printf("module %s already"
1294						    " present!\n", modname);
1295						linker_file_unload(lf,
1296						    LINKER_UNLOAD_FORCE);
1297						TAILQ_REMOVE(&loaded_files,
1298						    lf, loaded);
1299						/* we changed tailq next ptr */
1300						goto restart;
1301					}
1302					modlist_newmodule(modname, nver, lf);
1303				}
1304			}
1305			TAILQ_REMOVE(&loaded_files, lf, loaded);
1306			TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1307			/*
1308			 * Since we provided modules, we need to restart the
1309			 * sort so that the previous files that depend on us
1310			 * have a chance. Also, we've busted the tailq next
1311			 * pointer with the REMOVE.
1312			 */
1313			goto restart;
1314		}
1315	}
1316
1317	/*
1318	 * At this point, we check to see what could not be resolved..
1319	 */
1320	TAILQ_FOREACH(lf, &loaded_files, loaded) {
1321		printf("KLD file %s is missing dependencies\n", lf->filename);
1322		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1323		TAILQ_REMOVE(&loaded_files, lf, loaded);
1324	}
1325
1326	/*
1327	 * We made it. Finish off the linking in the order we determined.
1328	 */
1329	TAILQ_FOREACH(lf, &depended_files, loaded) {
1330		if (linker_kernel_file) {
1331			linker_kernel_file->refs++;
1332			error = linker_file_add_dependency(lf,
1333			    linker_kernel_file);
1334			if (error)
1335				panic("cannot add dependency");
1336		}
1337		lf->userrefs++;	/* so we can (try to) kldunload it */
1338		error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1339		    &stop, NULL);
1340		if (!error) {
1341			for (mdp = start; mdp < stop; mdp++) {
1342				mp = *mdp;
1343				if (mp->md_type != MDT_DEPEND)
1344					continue;
1345				modname = mp->md_cval;
1346				verinfo = mp->md_data;
1347				mod = modlist_lookup2(modname, verinfo);
1348				/* Don't count self-dependencies */
1349				if (lf == mod->container)
1350					continue;
1351				mod->container->refs++;
1352				error = linker_file_add_dependency(lf,
1353				    mod->container);
1354				if (error)
1355					panic("cannot add dependency");
1356			}
1357		}
1358		/*
1359		 * Now do relocation etc using the symbol search paths
1360		 * established by the dependencies
1361		 */
1362		error = LINKER_LINK_PRELOAD_FINISH(lf);
1363		if (error) {
1364			printf("KLD file %s - could not finalize loading\n",
1365			    lf->filename);
1366			linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1367			continue;
1368		}
1369		linker_file_register_modules(lf);
1370		if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1371		    &si_stop, NULL) == 0)
1372			sysinit_add(si_start, si_stop);
1373		linker_file_register_sysctls(lf);
1374		lf->flags |= LINKER_FILE_LINKED;
1375	}
1376	/* woohoo! we made it! */
1377}
1378
1379SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
1380
1381/*
1382 * Search for a not-loaded module by name.
1383 *
1384 * Modules may be found in the following locations:
1385 *
1386 * - preloaded (result is just the module name) - on disk (result is full path
1387 * to module)
1388 *
1389 * If the module name is qualified in any way (contains path, etc.) the we
1390 * simply return a copy of it.
1391 *
1392 * The search path can be manipulated via sysctl.  Note that we use the ';'
1393 * character as a separator to be consistent with the bootloader.
1394 */
1395
1396static char linker_hintfile[] = "linker.hints";
1397static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1398
1399SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1400    sizeof(linker_path), "module load search path");
1401
1402TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1403
1404static char *linker_ext_list[] = {
1405	"",
1406	".ko",
1407	NULL
1408};
1409
1410/*
1411 * Check if file actually exists either with or without extension listed in
1412 * the linker_ext_list. (probably should be generic for the rest of the
1413 * kernel)
1414 */
1415static char *
1416linker_lookup_file(const char *path, int pathlen, const char *name,
1417    int namelen, struct vattr *vap)
1418{
1419	struct nameidata nd;
1420	struct thread *td = curthread;	/* XXX */
1421	char *result, **cpp, *sep;
1422	int error, len, extlen, reclen, flags;
1423	enum vtype type;
1424
1425	extlen = 0;
1426	for (cpp = linker_ext_list; *cpp; cpp++) {
1427		len = strlen(*cpp);
1428		if (len > extlen)
1429			extlen = len;
1430	}
1431	extlen++;		/* trailing '\0' */
1432	sep = (path[pathlen - 1] != '/') ? "/" : "";
1433
1434	reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1435	result = malloc(reclen, M_LINKER, M_WAITOK);
1436	for (cpp = linker_ext_list; *cpp; cpp++) {
1437		snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1438		    namelen, name, *cpp);
1439		/*
1440		 * Attempt to open the file, and return the path if
1441		 * we succeed and it's a regular file.
1442		 */
1443		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1444		flags = FREAD;
1445		error = vn_open(&nd, &flags, 0, -1);
1446		if (error == 0) {
1447			NDFREE(&nd, NDF_ONLY_PNBUF);
1448			type = nd.ni_vp->v_type;
1449			if (vap)
1450				VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
1451			VOP_UNLOCK(nd.ni_vp, 0, td);
1452			vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1453			if (type == VREG)
1454				return (result);
1455		}
1456	}
1457	free(result, M_LINKER);
1458	return (NULL);
1459}
1460
1461#define	INT_ALIGN(base, ptr)	ptr =					\
1462	(base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1463
1464/*
1465 * Lookup KLD which contains requested module in the "linker.hints" file. If
1466 * version specification is available, then try to find the best KLD.
1467 * Otherwise just find the latest one.
1468 */
1469static char *
1470linker_hints_lookup(const char *path, int pathlen, const char *modname,
1471    int modnamelen, struct mod_depend *verinfo)
1472{
1473	struct thread *td = curthread;	/* XXX */
1474	struct ucred *cred = td ? td->td_ucred : NULL;
1475	struct nameidata nd;
1476	struct vattr vattr, mattr;
1477	u_char *hints = NULL;
1478	u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1479	int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1480
1481	result = NULL;
1482	bestver = found = 0;
1483
1484	sep = (path[pathlen - 1] != '/') ? "/" : "";
1485	reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1486	    strlen(sep) + 1;
1487	pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1488	snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1489	    linker_hintfile);
1490
1491	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1492	flags = FREAD;
1493	error = vn_open(&nd, &flags, 0, -1);
1494	if (error)
1495		goto bad;
1496	NDFREE(&nd, NDF_ONLY_PNBUF);
1497	if (nd.ni_vp->v_type != VREG)
1498		goto bad;
1499	best = cp = NULL;
1500	error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
1501	if (error)
1502		goto bad;
1503	/*
1504	 * XXX: we need to limit this number to some reasonable value
1505	 */
1506	if (vattr.va_size > 100 * 1024) {
1507		printf("hints file too large %ld\n", (long)vattr.va_size);
1508		goto bad;
1509	}
1510	hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1511	if (hints == NULL)
1512		goto bad;
1513	error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1514	    UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1515	if (error)
1516		goto bad;
1517	VOP_UNLOCK(nd.ni_vp, 0, td);
1518	vn_close(nd.ni_vp, FREAD, cred, td);
1519	nd.ni_vp = NULL;
1520	if (reclen != 0) {
1521		printf("can't read %d\n", reclen);
1522		goto bad;
1523	}
1524	intp = (int *)hints;
1525	ival = *intp++;
1526	if (ival != LINKER_HINTS_VERSION) {
1527		printf("hints file version mismatch %d\n", ival);
1528		goto bad;
1529	}
1530	bufend = hints + vattr.va_size;
1531	recptr = (u_char *)intp;
1532	clen = blen = 0;
1533	while (recptr < bufend && !found) {
1534		intp = (int *)recptr;
1535		reclen = *intp++;
1536		ival = *intp++;
1537		cp = (char *)intp;
1538		switch (ival) {
1539		case MDT_VERSION:
1540			clen = *cp++;
1541			if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1542				break;
1543			cp += clen;
1544			INT_ALIGN(hints, cp);
1545			ival = *(int *)cp;
1546			cp += sizeof(int);
1547			clen = *cp++;
1548			if (verinfo == NULL ||
1549			    ival == verinfo->md_ver_preferred) {
1550				found = 1;
1551				break;
1552			}
1553			if (ival >= verinfo->md_ver_minimum &&
1554			    ival <= verinfo->md_ver_maximum &&
1555			    ival > bestver) {
1556				bestver = ival;
1557				best = cp;
1558				blen = clen;
1559			}
1560			break;
1561		default:
1562			break;
1563		}
1564		recptr += reclen + sizeof(int);
1565	}
1566	/*
1567	 * Finally check if KLD is in the place
1568	 */
1569	if (found)
1570		result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1571	else if (best)
1572		result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1573
1574	/*
1575	 * KLD is newer than hints file. What we should do now?
1576	 */
1577	if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1578		printf("warning: KLD '%s' is newer than the linker.hints"
1579		    " file\n", result);
1580bad:
1581	free(pathbuf, M_LINKER);
1582	if (hints)
1583		free(hints, M_TEMP);
1584	if (nd.ni_vp != NULL) {
1585		VOP_UNLOCK(nd.ni_vp, 0, td);
1586		vn_close(nd.ni_vp, FREAD, cred, td);
1587	}
1588	/*
1589	 * If nothing found or hints is absent - fallback to the old
1590	 * way by using "kldname[.ko]" as module name.
1591	 */
1592	if (!found && !bestver && result == NULL)
1593		result = linker_lookup_file(path, pathlen, modname,
1594		    modnamelen, NULL);
1595	return (result);
1596}
1597
1598/*
1599 * Lookup KLD which contains requested module in the all directories.
1600 */
1601static char *
1602linker_search_module(const char *modname, int modnamelen,
1603    struct mod_depend *verinfo)
1604{
1605	char *cp, *ep, *result;
1606
1607	/*
1608	 * traverse the linker path
1609	 */
1610	for (cp = linker_path; *cp; cp = ep + 1) {
1611		/* find the end of this component */
1612		for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1613		result = linker_hints_lookup(cp, ep - cp, modname,
1614		    modnamelen, verinfo);
1615		if (result != NULL)
1616			return (result);
1617		if (*ep == 0)
1618			break;
1619	}
1620	return (NULL);
1621}
1622
1623/*
1624 * Search for module in all directories listed in the linker_path.
1625 */
1626static char *
1627linker_search_kld(const char *name)
1628{
1629	char *cp, *ep, *result;
1630	int len;
1631
1632	/* qualified at all? */
1633	if (index(name, '/'))
1634		return (linker_strdup(name));
1635
1636	/* traverse the linker path */
1637	len = strlen(name);
1638	for (ep = linker_path; *ep; ep++) {
1639		cp = ep;
1640		/* find the end of this component */
1641		for (; *ep != 0 && *ep != ';'; ep++);
1642		result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1643		if (result != NULL)
1644			return (result);
1645	}
1646	return (NULL);
1647}
1648
1649static const char *
1650linker_basename(const char *path)
1651{
1652	const char *filename;
1653
1654	filename = rindex(path, '/');
1655	if (filename == NULL)
1656		return path;
1657	if (filename[1])
1658		filename++;
1659	return (filename);
1660}
1661
1662#ifdef HWPMC_HOOKS
1663
1664/*
1665 * Inform hwpmc about the set of kernel modules currently loaded.
1666 */
1667void *
1668linker_hwpmc_list_objects(void)
1669{
1670	int nobjects, nmappings;
1671	linker_file_t lf;
1672	struct pmckern_map_in *ko, *kobase;
1673
1674	nmappings = 15;	/* a reasonable default */
1675
1676 retry:
1677	/* allocate nmappings+1 entries */
1678	MALLOC(kobase, struct pmckern_map_in *,
1679	    (nmappings + 1) * sizeof(struct pmckern_map_in), M_LINKER,
1680	    M_WAITOK | M_ZERO);
1681
1682	nobjects = 0;
1683	mtx_lock(&kld_mtx);
1684	TAILQ_FOREACH(lf, &linker_files, link)
1685		nobjects++;
1686
1687	KASSERT(nobjects > 0, ("linker_hpwmc_list_objects: no kernel "
1688		"objects?"));
1689
1690	if (nobjects > nmappings) {
1691		nmappings = nobjects;
1692		FREE(kobase, M_LINKER);
1693		mtx_unlock(&kld_mtx);
1694		goto retry;
1695	}
1696
1697	ko = kobase;
1698	TAILQ_FOREACH(lf, &linker_files, link) {
1699		ko->pm_file = lf->filename;
1700		ko->pm_address = (uintptr_t) lf->address;
1701		ko++;
1702	}
1703
1704	/* The last entry of the malloced area comprises of all zeros. */
1705	KASSERT(ko->pm_file == NULL,
1706	    ("linker_hwpmc_list_objects: last object not NULL"));
1707
1708	mtx_unlock(&kld_mtx);
1709
1710	return ((void *) kobase);
1711}
1712#endif
1713
1714/*
1715 * Find a file which contains given module and load it, if "parent" is not
1716 * NULL, register a reference to it.
1717 */
1718int
1719linker_load_module(const char *kldname, const char *modname,
1720    struct linker_file *parent, struct mod_depend *verinfo,
1721    struct linker_file **lfpp)
1722{
1723	linker_file_t lfdep;
1724	const char *filename;
1725	char *pathname;
1726	int error;
1727
1728	if (modname == NULL) {
1729		/*
1730 		 * We have to load KLD
1731 		 */
1732		KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1733		    " is not NULL"));
1734		pathname = linker_search_kld(kldname);
1735	} else {
1736		if (modlist_lookup2(modname, verinfo) != NULL)
1737			return (EEXIST);
1738		if (kldname != NULL)
1739			pathname = linker_strdup(kldname);
1740		else if (rootvnode == NULL)
1741			pathname = NULL;
1742		else
1743			/*
1744			 * Need to find a KLD with required module
1745			 */
1746			pathname = linker_search_module(modname,
1747			    strlen(modname), verinfo);
1748	}
1749	if (pathname == NULL)
1750		return (ENOENT);
1751
1752	/*
1753	 * Can't load more than one file with the same basename XXX:
1754	 * Actually it should be possible to have multiple KLDs with
1755	 * the same basename but different path because they can
1756	 * provide different versions of the same modules.
1757	 */
1758	filename = linker_basename(pathname);
1759	if (linker_find_file_by_name(filename)) {
1760		error = EEXIST;
1761		goto out;
1762	}
1763	do {
1764		error = linker_load_file(pathname, &lfdep);
1765		if (error)
1766			break;
1767		if (modname && verinfo &&
1768		    modlist_lookup2(modname, verinfo) == NULL) {
1769			linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
1770			error = ENOENT;
1771			break;
1772		}
1773		if (parent) {
1774			error = linker_file_add_dependency(parent, lfdep);
1775			if (error)
1776				break;
1777		}
1778		if (lfpp)
1779			*lfpp = lfdep;
1780	} while (0);
1781out:
1782	if (pathname)
1783		free(pathname, M_LINKER);
1784	return (error);
1785}
1786
1787/*
1788 * This routine is responsible for finding dependencies of userland initiated
1789 * kldload(2)'s of files.
1790 */
1791int
1792linker_load_dependencies(linker_file_t lf)
1793{
1794	linker_file_t lfdep;
1795	struct mod_metadata **start, **stop, **mdp, **nmdp;
1796	struct mod_metadata *mp, *nmp;
1797	struct mod_depend *verinfo;
1798	modlist_t mod;
1799	const char *modname, *nmodname;
1800	int ver, error = 0, count;
1801
1802	/*
1803	 * All files are dependant on /kernel.
1804	 */
1805	if (linker_kernel_file) {
1806		linker_kernel_file->refs++;
1807		error = linker_file_add_dependency(lf, linker_kernel_file);
1808		if (error)
1809			return (error);
1810	}
1811	if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
1812	    &count) != 0)
1813		return (0);
1814	for (mdp = start; mdp < stop; mdp++) {
1815		mp = *mdp;
1816		if (mp->md_type != MDT_VERSION)
1817			continue;
1818		modname = mp->md_cval;
1819		ver = ((struct mod_version *)mp->md_data)->mv_version;
1820		mod = modlist_lookup(modname, ver);
1821		if (mod != NULL) {
1822			printf("interface %s.%d already present in the KLD"
1823			    " '%s'!\n", modname, ver,
1824			    mod->container->filename);
1825			return (EEXIST);
1826		}
1827	}
1828
1829	for (mdp = start; mdp < stop; mdp++) {
1830		mp = *mdp;
1831		if (mp->md_type != MDT_DEPEND)
1832			continue;
1833		modname = mp->md_cval;
1834		verinfo = mp->md_data;
1835		nmodname = NULL;
1836		for (nmdp = start; nmdp < stop; nmdp++) {
1837			nmp = *nmdp;
1838			if (nmp->md_type != MDT_VERSION)
1839				continue;
1840			nmodname = nmp->md_cval;
1841			if (strcmp(modname, nmodname) == 0)
1842				break;
1843		}
1844		if (nmdp < stop)/* early exit, it's a self reference */
1845			continue;
1846		mod = modlist_lookup2(modname, verinfo);
1847		if (mod) {	/* woohoo, it's loaded already */
1848			lfdep = mod->container;
1849			lfdep->refs++;
1850			error = linker_file_add_dependency(lf, lfdep);
1851			if (error)
1852				break;
1853			continue;
1854		}
1855		error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1856		if (error) {
1857			printf("KLD %s: depends on %s - not available\n",
1858			    lf->filename, modname);
1859			break;
1860		}
1861	}
1862
1863	if (error)
1864		return (error);
1865	linker_addmodules(lf, start, stop, 0);
1866	return (error);
1867}
1868
1869static int
1870sysctl_kern_function_list_iterate(const char *name, void *opaque)
1871{
1872	struct sysctl_req *req;
1873
1874	req = opaque;
1875	return (SYSCTL_OUT(req, name, strlen(name) + 1));
1876}
1877
1878/*
1879 * Export a nul-separated, double-nul-terminated list of all function names
1880 * in the kernel.
1881 */
1882static int
1883sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
1884{
1885	linker_file_t lf;
1886	int error;
1887
1888#ifdef MAC
1889	error = mac_check_kld_stat(req->td->td_ucred);
1890	if (error)
1891		return (error);
1892#endif
1893	error = sysctl_wire_old_buffer(req, 0);
1894	if (error != 0)
1895		return (error);
1896	mtx_lock(&kld_mtx);
1897	TAILQ_FOREACH(lf, &linker_files, link) {
1898		error = LINKER_EACH_FUNCTION_NAME(lf,
1899		    sysctl_kern_function_list_iterate, req);
1900		if (error) {
1901			mtx_unlock(&kld_mtx);
1902			return (error);
1903		}
1904	}
1905	mtx_unlock(&kld_mtx);
1906	return (SYSCTL_OUT(req, "", 1));
1907}
1908
1909SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
1910    NULL, 0, sysctl_kern_function_list, "", "kernel function list");
1911