kern_linker.c revision 42849
1/*-
2 * Copyright (c) 1997 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 *	$Id: kern_linker.c,v 1.20 1999/01/19 16:26:32 peter Exp $
27 */
28
29#include "opt_ddb.h"
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/sysproto.h>
36#include <sys/sysent.h>
37#include <sys/proc.h>
38#include <sys/lock.h>
39#include <machine/cpu.h>
40#include <machine/bootinfo.h>
41#include <sys/module.h>
42#include <sys/linker.h>
43#include <sys/unistd.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
46#include <sys/namei.h>
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50#ifdef KLD_DEBUG
51int kld_debug = 0;
52#endif
53
54MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
55linker_file_t linker_current_file;
56linker_file_t linker_kernel_file;
57
58static struct lock lock;	/* lock for the file list */
59static linker_class_list_t classes;
60static linker_file_list_t files;
61static int next_file_id = 1;
62
63static void
64linker_init(void* arg)
65{
66    lockinit(&lock, PVM, "klink", 0, 0);
67    TAILQ_INIT(&classes);
68    TAILQ_INIT(&files);
69}
70
71SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
72
73int
74linker_add_class(const char* desc, void* priv,
75		 struct linker_class_ops* ops)
76{
77    linker_class_t lc;
78
79    lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT);
80    if (!lc)
81	return ENOMEM;
82    bzero(lc, sizeof(*lc));
83
84    lc->desc = desc;
85    lc->priv = priv;
86    lc->ops = ops;
87    TAILQ_INSERT_HEAD(&classes, lc, link);
88
89    return 0;
90}
91
92static void
93linker_file_sysinit(linker_file_t lf)
94{
95    struct linker_set* sysinits;
96    struct sysinit** sipp;
97    struct sysinit** xipp;
98    struct sysinit* save;
99    moduledata_t *moddata;
100
101    KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
102		   lf->filename));
103
104    sysinits = (struct linker_set*)
105	linker_file_lookup_symbol(lf, "sysinit_set", 0);
106
107    KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
108    if (!sysinits)
109	return;
110
111    /* HACK ALERT! */
112    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
113	if ((*sipp)->func == module_register_init) {
114	    moddata = (*sipp)->udata;
115	    moddata->_file = lf;
116	}
117    }
118
119    /*
120     * Perform a bubble sort of the system initialization objects by
121     * their subsystem (primary key) and order (secondary key).
122     *
123     * Since some things care about execution order, this is the
124     * operation which ensures continued function.
125     */
126    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
127	for (xipp = sipp + 1; *xipp; xipp++) {
128	    if ((*sipp)->subsystem <= (*xipp)->subsystem ||
129		 ((*sipp)->subsystem == (*xipp)->subsystem &&
130		  (*sipp)->order <= (*xipp)->order))
131		continue;	/* skip*/
132	    save = *sipp;
133	    *sipp = *xipp;
134	    *xipp = save;
135	}
136    }
137
138
139    /*
140     * Traverse the (now) ordered list of system initialization tasks.
141     * Perform each task, and continue on to the next task.
142     */
143    for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
144	if ((*sipp)->subsystem == SI_SUB_DUMMY)
145	    continue;	/* skip dummy task(s)*/
146
147	switch ((*sipp)->type) {
148	case SI_TYPE_DEFAULT:
149	    /* no special processing*/
150	    (*((*sipp)->func))((*sipp)->udata);
151	    break;
152
153	case SI_TYPE_KTHREAD:
154#if !defined(SMP)
155	    /* kernel thread*/
156	    if (fork1(&proc0, RFFDG|RFPROC|RFMEM))
157		panic("fork kernel thread");
158	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
159		(*sipp)->func, (*sipp)->udata);
160	    break;
161#endif
162
163	case SI_TYPE_KPROCESS:
164	    /* kernel thread*/
165	    if (fork1(&proc0, RFFDG|RFPROC))
166		panic("fork kernel process");
167	    cpu_set_fork_handler(pfind(proc0.p_retval[0]),
168		(*sipp)->func, (*sipp)->udata);
169	    break;
170
171	default:
172	    panic ("linker_file_sysinit: unrecognized init type");
173	}
174    }
175}
176
177static void
178linker_file_sysuninit(linker_file_t lf)
179{
180    struct linker_set* sysuninits;
181    struct sysinit** sipp;
182    struct sysinit** xipp;
183    struct sysinit* save;
184
185    KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
186		   lf->filename));
187
188    sysuninits = (struct linker_set*)
189	linker_file_lookup_symbol(lf, "sysuninit_set", 0);
190
191    KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
192    if (!sysuninits)
193	return;
194
195    /*
196     * Perform a reverse bubble sort of the system initialization objects
197     * by their subsystem (primary key) and order (secondary key).
198     *
199     * Since some things care about execution order, this is the
200     * operation which ensures continued function.
201     */
202    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
203	for (xipp = sipp + 1; *xipp; xipp++) {
204	    if ((*sipp)->subsystem >= (*xipp)->subsystem ||
205		 ((*sipp)->subsystem == (*xipp)->subsystem &&
206		  (*sipp)->order >= (*xipp)->order))
207		continue;	/* skip*/
208	    save = *sipp;
209	    *sipp = *xipp;
210	    *xipp = save;
211	}
212    }
213
214
215    /*
216     * Traverse the (now) ordered list of system initialization tasks.
217     * Perform each task, and continue on to the next task.
218     */
219    for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
220	if ((*sipp)->subsystem == SI_SUB_DUMMY)
221	    continue;	/* skip dummy task(s)*/
222
223	switch ((*sipp)->type) {
224	case SI_TYPE_DEFAULT:
225	    /* no special processing*/
226	    (*((*sipp)->func))((*sipp)->udata);
227	    break;
228
229	default:
230	    panic("linker_file_sysuninit: unrecognized uninit type");
231	}
232    }
233}
234
235int
236linker_load_file(const char* filename, linker_file_t* result)
237{
238    linker_class_t lc;
239    linker_file_t lf;
240    int foundfile, error = 0;
241    char *koname = NULL;
242
243    lf = linker_find_file_by_name(filename);
244    if (lf) {
245	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
246	*result = lf;
247	lf->refs++;
248	goto out;
249    }
250
251    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
252    if (koname == NULL) {
253	error = ENOMEM;
254	goto out;
255    }
256    sprintf(koname, "%s.ko", filename);
257    lf = NULL;
258    foundfile = 0;
259    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
260	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
261		       filename, lc->desc));
262
263	error = lc->ops->load_file(koname, &lf);	/* First with .ko */
264	if (lf == NULL && error == ENOENT)
265	    error = lc->ops->load_file(filename, &lf);	/* Then try without */
266	/*
267	 * If we got something other than ENOENT, then it exists but we cannot
268	 * load it for some other reason.
269	 */
270	if (error != ENOENT)
271	    foundfile = 1;
272	if (lf) {
273	    linker_file_sysinit(lf);
274
275	    *result = lf;
276	    error = 0;
277	    goto out;
278	}
279    }
280    /*
281     * Less than ideal, but tells the user whether it failed to load or
282     * the module was not found.
283     */
284    if (foundfile)
285	error = ENOEXEC;	/* Format not recognised (or unloadable) */
286    else
287	error = ENOENT;		/* Nothing found */
288
289out:
290    if (koname)
291	free(koname, M_LINKER);
292    return error;
293}
294
295linker_file_t
296linker_find_file_by_name(const char* filename)
297{
298    linker_file_t lf = 0;
299    char *koname;
300
301    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
302    if (koname == NULL)
303	goto out;
304    sprintf(koname, "%s.ko", filename);
305
306    lockmgr(&lock, LK_SHARED, 0, curproc);
307    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
308	if (!strcmp(lf->filename, koname))
309	    break;
310	if (!strcmp(lf->filename, filename))
311	    break;
312    }
313    lockmgr(&lock, LK_RELEASE, 0, curproc);
314
315out:
316    if (koname)
317	free(koname, M_LINKER);
318    return lf;
319}
320
321linker_file_t
322linker_find_file_by_id(int fileid)
323{
324    linker_file_t lf = 0;
325
326    lockmgr(&lock, LK_SHARED, 0, curproc);
327    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
328	if (lf->id == fileid)
329	    break;
330    lockmgr(&lock, LK_RELEASE, 0, curproc);
331
332    return lf;
333}
334
335linker_file_t
336linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
337{
338    linker_file_t lf = 0;
339    int namelen;
340    const char *filename;
341
342    filename = rindex(pathname, '/');
343    if (filename && filename[1])
344	filename++;
345    else
346	filename = pathname;
347
348    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
349    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
350    namelen = strlen(filename) + 1;
351    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
352    if (!lf)
353	goto out;
354    bzero(lf, sizeof(*lf));
355
356    lf->refs = 1;
357    lf->userrefs = 0;
358    lf->filename = (char*) (lf + 1);
359    strcpy(lf->filename, filename);
360    lf->id = next_file_id++;
361    lf->ndeps = 0;
362    lf->deps = NULL;
363    STAILQ_INIT(&lf->common);
364    TAILQ_INIT(&lf->modules);
365
366    lf->priv = priv;
367    lf->ops = ops;
368    TAILQ_INSERT_TAIL(&files, lf, link);
369
370out:
371    lockmgr(&lock, LK_RELEASE, 0, curproc);
372    return lf;
373}
374
375int
376linker_file_unload(linker_file_t file)
377{
378    module_t mod, next;
379    struct common_symbol* cp;
380    int error = 0;
381    int i;
382
383    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
384    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
385    if (file->refs == 1) {
386	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
387	/*
388	 * Inform any modules associated with this file.
389	 */
390	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
391	    next = module_getfnext(mod);
392
393	    /*
394	     * Give the module a chance to veto the unload.
395	     */
396	    if (error = module_unload(mod)) {
397		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
398			       mod));
399		lockmgr(&lock, LK_RELEASE, 0, curproc);
400		goto out;
401	    }
402
403	    module_release(mod);
404	}
405    }
406
407    file->refs--;
408    if (file->refs > 0) {
409	lockmgr(&lock, LK_RELEASE, 0, curproc);
410	goto out;
411    }
412
413    linker_file_sysuninit(file);
414
415    TAILQ_REMOVE(&files, file, link);
416    lockmgr(&lock, LK_RELEASE, 0, curproc);
417
418    for (i = 0; i < file->ndeps; i++)
419	linker_file_unload(file->deps[i]);
420    free(file->deps, M_LINKER);
421
422    for (cp = STAILQ_FIRST(&file->common); cp;
423	 cp = STAILQ_FIRST(&file->common)) {
424	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
425	free(cp, M_LINKER);
426    }
427
428    file->ops->unload(file);
429    free(file, M_LINKER);
430
431out:
432    return error;
433}
434
435int
436linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
437{
438    linker_file_t* newdeps;
439
440    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
441		     M_LINKER, M_WAITOK);
442    if (newdeps == NULL)
443	return ENOMEM;
444    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
445
446    if (file->deps) {
447	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
448	free(file->deps, M_LINKER);
449    }
450    file->deps = newdeps;
451    file->deps[file->ndeps] = dep;
452    file->ndeps++;
453
454    return 0;
455}
456
457caddr_t
458linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
459{
460    linker_sym_t sym;
461    linker_symval_t symval;
462    linker_file_t lf;
463    caddr_t address;
464    size_t common_size = 0;
465    int i;
466
467    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
468		  file, name, deps));
469
470    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
471	file->ops->symbol_values(file, sym, &symval);
472	if (symval.value == 0)
473	    /*
474	     * For commons, first look them up in the dependancies and
475	     * only allocate space if not found there.
476	     */
477	    common_size = symval.size;
478	else {
479	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
480	    return symval.value;
481	}
482    }
483
484    if (deps) {
485	for (i = 0; i < file->ndeps; i++) {
486	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
487	    if (address) {
488		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
489		return address;
490	    }
491	}
492
493	/* If we have not found it in the dependencies, search globally */
494	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
495	    /* But skip the current file if it's on the list */
496	    if (lf == file)
497		continue;
498	    /* And skip the files we searched above */
499	    for (i = 0; i < file->ndeps; i++)
500		if (lf == file->deps[i])
501		    break;
502	    if (i < file->ndeps)
503		continue;
504	    address = linker_file_lookup_symbol(lf, name, 0);
505	    if (address) {
506		KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address));
507		return address;
508	    }
509	}
510    }
511
512    if (common_size > 0) {
513	/*
514	 * This is a common symbol which was not found in the
515	 * dependancies.  We maintain a simple common symbol table in
516	 * the file object.
517	 */
518	struct common_symbol* cp;
519
520	for (cp = STAILQ_FIRST(&file->common); cp;
521	     cp = STAILQ_NEXT(cp, link))
522	    if (!strcmp(cp->name, name)) {
523		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
524		return cp->address;
525	    }
526
527	/*
528	 * Round the symbol size up to align.
529	 */
530	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
531	cp = malloc(sizeof(struct common_symbol)
532		    + common_size
533		    + strlen(name) + 1,
534		    M_LINKER, M_WAITOK);
535	if (!cp) {
536	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
537	    return 0;
538	}
539	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
540
541	cp->address = (caddr_t) (cp + 1);
542	cp->name = cp->address + common_size;
543	strcpy(cp->name, name);
544	bzero(cp->address, common_size);
545	STAILQ_INSERT_TAIL(&file->common, cp, link);
546
547	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
548	return cp->address;
549    }
550
551    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
552    return 0;
553}
554
555#ifdef DDB
556/*
557 * DDB Helpers.  DDB has to look across multiple files with their own
558 * symbol tables and string tables.
559 *
560 * Note that we do not obey list locking protocols here.  We really don't
561 * need DDB to hang because somebody's got the lock held.  We'll take the
562 * chance that the files list is inconsistant instead.
563 */
564
565int
566linker_ddb_lookup(char *symstr, linker_sym_t *sym)
567{
568    linker_file_t lf;
569
570    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
571	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
572	    return 0;
573    }
574    return ENOENT;
575}
576
577int
578linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp)
579{
580    linker_file_t lf;
581    u_long off = (u_long)value;
582    u_long diff, bestdiff;
583    linker_sym_t best;
584    linker_sym_t es;
585
586    best = 0;
587    bestdiff = off;
588    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
589	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
590	    continue;
591	if (es != 0 && diff < bestdiff) {
592	    best = es;
593	    bestdiff = diff;
594	}
595	if (bestdiff == 0)
596	    break;
597    }
598    if (best) {
599	*sym = best;
600	*diffp = bestdiff;
601	return 0;
602    } else {
603	*sym = 0;
604	*diffp = off;
605	return ENOENT;
606    }
607}
608
609int
610linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval)
611{
612    linker_file_t lf;
613
614    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
615	if (lf->ops->symbol_values(lf, sym, symval) == 0)
616	    return 0;
617    }
618    return ENOENT;
619}
620
621#endif
622
623/*
624 * Syscalls.
625 */
626
627int
628kldload(struct proc* p, struct kldload_args* uap)
629{
630    char* filename = NULL, *modulename;
631    linker_file_t lf;
632    int error = 0;
633
634    p->p_retval[0] = -1;
635
636    if (securelevel > 0)
637	return EPERM;
638
639    if (error = suser(p->p_ucred, &p->p_acflag))
640	return error;
641
642    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
643    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
644	goto out;
645
646    /* Can't load more than one module with the same name */
647    modulename = rindex(filename, '/');
648    if (modulename == NULL)
649	modulename = filename;
650    if (linker_find_file_by_name(modulename)) {
651	error = EEXIST;
652	goto out;
653    }
654
655    if (error = linker_load_file(filename, &lf))
656	goto out;
657
658    lf->userrefs++;
659    p->p_retval[0] = lf->id;
660
661out:
662    if (filename)
663	free(filename, M_TEMP);
664    return error;
665}
666
667int
668kldunload(struct proc* p, struct kldunload_args* uap)
669{
670    linker_file_t lf;
671    int error = 0;
672
673    if (securelevel > 0)
674	return EPERM;
675
676    if (error = suser(p->p_ucred, &p->p_acflag))
677	return error;
678
679    lf = linker_find_file_by_id(SCARG(uap, fileid));
680    if (lf) {
681	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
682	if (lf->userrefs == 0) {
683	    printf("linkerunload: attempt to unload file which was not loaded by user\n");
684	    error = EBUSY;
685	    goto out;
686	}
687	error = linker_file_unload(lf);
688	if (error)
689	    goto out;
690	lf->userrefs--;
691    } else
692	error = ENOENT;
693
694out:
695    return error;
696}
697
698int
699kldfind(struct proc* p, struct kldfind_args* uap)
700{
701    char* filename = NULL, *modulename;
702    linker_file_t lf;
703    int error = 0;
704
705    p->p_retval[0] = -1;
706
707    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
708    if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL))
709	goto out;
710
711    modulename = rindex(filename, '/');
712    if (modulename == NULL)
713	modulename = filename;
714
715    lf = linker_find_file_by_name(modulename);
716    if (lf)
717	p->p_retval[0] = lf->id;
718    else
719	error = ENOENT;
720
721out:
722    if (filename)
723	free(filename, M_TEMP);
724    return error;
725}
726
727int
728kldnext(struct proc* p, struct kldnext_args* uap)
729{
730    linker_file_t lf;
731    int error = 0;
732
733    if (SCARG(uap, fileid) == 0) {
734	if (TAILQ_FIRST(&files))
735	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
736	else
737	    p->p_retval[0] = 0;
738	return 0;
739    }
740
741    lf = linker_find_file_by_id(SCARG(uap, fileid));
742    if (lf) {
743	if (TAILQ_NEXT(lf, link))
744	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
745	else
746	    p->p_retval[0] = 0;
747    } else
748	error = ENOENT;
749
750    return error;
751}
752
753int
754kldstat(struct proc* p, struct kldstat_args* uap)
755{
756    linker_file_t lf;
757    int error = 0;
758    int version;
759    struct kld_file_stat* stat;
760    int namelen;
761
762    lf = linker_find_file_by_id(SCARG(uap, fileid));
763    if (!lf) {
764	error = ENOENT;
765	goto out;
766    }
767
768    stat = SCARG(uap, stat);
769
770    /*
771     * Check the version of the user's structure.
772     */
773    if (error = copyin(&stat->version, &version, sizeof(version)))
774	goto out;
775    if (version != sizeof(struct kld_file_stat)) {
776	error = EINVAL;
777	goto out;
778    }
779
780    namelen = strlen(lf->filename) + 1;
781    if (namelen > MAXPATHLEN)
782	namelen = MAXPATHLEN;
783    if (error = copyout(lf->filename, &stat->name[0], namelen))
784	goto out;
785    if (error = copyout(&lf->refs, &stat->refs, sizeof(int)))
786	goto out;
787    if (error = copyout(&lf->id, &stat->id, sizeof(int)))
788	goto out;
789    if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t)))
790	goto out;
791    if (error = copyout(&lf->size, &stat->size, sizeof(size_t)))
792	goto out;
793
794    p->p_retval[0] = 0;
795
796out:
797    return error;
798}
799
800int
801kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
802{
803    linker_file_t lf;
804    int error = 0;
805
806    lf = linker_find_file_by_id(SCARG(uap, fileid));
807    if (lf) {
808	if (TAILQ_FIRST(&lf->modules))
809	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
810	else
811	    p->p_retval[0] = 0;
812    } else
813	error = ENOENT;
814
815    return error;
816}
817
818int
819kldsym(struct proc *p, struct kldsym_args *uap)
820{
821    char *symstr = NULL;
822    linker_sym_t sym;
823    linker_symval_t symval;
824    linker_file_t lf;
825    struct kld_sym_lookup lookup;
826    int error = 0;
827
828    if (error = copyin(SCARG(uap, data), &lookup, sizeof(lookup)))
829	goto out;
830    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
831	error = EINVAL;
832	goto out;
833    }
834
835    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
836    if (error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL))
837	goto out;
838
839    if (SCARG(uap, fileid) != 0) {
840	lf = linker_find_file_by_id(SCARG(uap, fileid));
841	if (lf == NULL) {
842	    error = ENOENT;
843	    goto out;
844	}
845	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
846	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
847	    lookup.symvalue = (u_long)symval.value;
848	    lookup.symsize = symval.size;
849	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
850	} else
851	    error = ENOENT;
852    } else {
853	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
854	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
855		lf->ops->symbol_values(lf, sym, &symval) == 0) {
856		lookup.symvalue = (u_long)symval.value;
857		lookup.symsize = symval.size;
858		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
859		break;
860	    }
861	}
862	if (!lf)
863	    error = ENOENT;
864    }
865out:
866    if (symstr)
867	free(symstr, M_TEMP);
868    return error;
869}
870
871/*
872 * Preloaded module support
873 */
874
875static void
876linker_preload(void* arg)
877{
878    caddr_t		modptr;
879    char		*modname;
880    char		*modtype;
881    linker_file_t	lf;
882    linker_class_t	lc;
883    int			error;
884    struct linker_set	*sysinits;
885    struct sysinit	**sipp;
886    moduledata_t	*moddata;
887
888    modptr = NULL;
889    while ((modptr = preload_search_next_name(modptr)) != NULL) {
890	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
891	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
892	if (modname == NULL) {
893	    printf("Preloaded module at %p does not have a name!\n", modptr);
894	    continue;
895	}
896	if (modtype == NULL) {
897	    printf("Preloaded module at %p does not have a type!\n", modptr);
898	    continue;
899	}
900	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
901	lf = linker_find_file_by_name(modname);
902	if (lf) {
903	    lf->userrefs++;
904	    continue;
905	}
906	lf = NULL;
907	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
908	    error = lc->ops->load_file(modname, &lf);
909	    if (error) {
910		lf = NULL;
911		break;
912	    }
913	}
914	if (lf) {
915	    lf->userrefs++;
916
917	    sysinits = (struct linker_set*)
918		linker_file_lookup_symbol(lf, "sysinit_set", 0);
919	    if (sysinits) {
920		/* HACK ALERT!
921		 * This is to set the sysinit moduledata so that the module
922		 * can attach itself to the correct containing file.
923		 * The sysinit could be run at *any* time.
924		 */
925		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
926		    if ((*sipp)->func == module_register_init) {
927			moddata = (*sipp)->udata;
928			moddata->_file = lf;
929		    }
930		}
931		sysinit_add((struct sysinit **)sysinits->ls_items);
932	    }
933	}
934    }
935}
936
937SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
938
939/*
940 * Search for a not-loaded module by name.
941 *
942 * Modules may be found in the following locations:
943 *
944 * - preloaded (result is just the module name)
945 * - on disk (result is full path to module)
946 *
947 * If the module name is qualified in any way (contains path, etc.)
948 * the we simply return a copy of it.
949 *
950 * The search path can be manipulated via sysctl.  Note that we use the ';'
951 * character as a separator to be consistent with the bootloader.
952 */
953
954static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
955
956SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
957	      sizeof(linker_path), "module load search path");
958
959static char *
960linker_strdup(const char *str)
961{
962    char	*result;
963
964    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
965	strcpy(result, str);
966    return(result);
967}
968
969char *
970linker_search_path(const char *name)
971{
972    struct nameidata	nd;
973    struct proc		*p = curproc;	/* XXX */
974    char		*cp, *ep, *result;
975    int			error;
976    enum vtype		type;
977
978    /* qualified at all? */
979    if (index(name, '/'))
980	return(linker_strdup(name));
981
982    /* traverse the linker path */
983    cp = linker_path;
984    for (;;) {
985
986	/* find the end of this component */
987	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
988	    ;
989	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
990	if (result == NULL)	/* actually ENOMEM */
991	    return(NULL);
992
993	strncpy(result, cp, ep - cp);
994	strcpy(result + (ep - cp), name);
995
996	/*
997	 * Attempt to open the file, and return the path if we succeed and it's
998	 * a regular file.
999	 */
1000	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
1001	error = vn_open(&nd, FREAD, 0);
1002	if (error == 0) {
1003	    type = nd.ni_vp->v_type;
1004	    VOP_UNLOCK(nd.ni_vp, 0, p);
1005	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
1006	    if (type == VREG)
1007		return(result);
1008	}
1009	free(result, M_LINKER);
1010
1011	if (*ep == 0)
1012	    break;
1013	cp = ep + 1;
1014    }
1015    return(NULL);
1016}
1017