kern_linker.c revision 44078
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.25 1999/01/27 23:45:39 dillon 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
235static void
236linker_file_register_sysctls(linker_file_t lf)
237{
238    struct linker_set* sysctls;
239
240    KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
241		   lf->filename));
242
243    sysctls = (struct linker_set*)
244	linker_file_lookup_symbol(lf, "sysctl_set", 0);
245
246    KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls));
247    if (!sysctls)
248	return;
249
250    sysctl_register_set(sysctls);
251}
252
253static void
254linker_file_unregister_sysctls(linker_file_t lf)
255{
256    struct linker_set* sysctls;
257
258    KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
259		   lf->filename));
260
261    sysctls = (struct linker_set*)
262	linker_file_lookup_symbol(lf, "sysctl_set", 0);
263
264    KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls));
265    if (!sysctls)
266	return;
267
268    sysctl_unregister_set(sysctls);
269}
270
271int
272linker_load_file(const char* filename, linker_file_t* result)
273{
274    linker_class_t lc;
275    linker_file_t lf;
276    int foundfile, error = 0;
277    char *koname = NULL;
278
279    lf = linker_find_file_by_name(filename);
280    if (lf) {
281	KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
282	*result = lf;
283	lf->refs++;
284	goto out;
285    }
286
287    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
288    if (koname == NULL) {
289	error = ENOMEM;
290	goto out;
291    }
292    sprintf(koname, "%s.ko", filename);
293    lf = NULL;
294    foundfile = 0;
295    for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
296	KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
297		       filename, lc->desc));
298
299	error = lc->ops->load_file(koname, &lf);	/* First with .ko */
300	if (lf == NULL && error == ENOENT)
301	    error = lc->ops->load_file(filename, &lf);	/* Then try without */
302	/*
303	 * If we got something other than ENOENT, then it exists but we cannot
304	 * load it for some other reason.
305	 */
306	if (error != ENOENT)
307	    foundfile = 1;
308	if (lf) {
309	    linker_file_sysinit(lf);
310	    linker_file_register_sysctls(lf);
311
312	    *result = lf;
313	    error = 0;
314	    goto out;
315	}
316    }
317    /*
318     * Less than ideal, but tells the user whether it failed to load or
319     * the module was not found.
320     */
321    if (foundfile)
322	error = ENOEXEC;	/* Format not recognised (or unloadable) */
323    else
324	error = ENOENT;		/* Nothing found */
325
326out:
327    if (koname)
328	free(koname, M_LINKER);
329    return error;
330}
331
332linker_file_t
333linker_find_file_by_name(const char* filename)
334{
335    linker_file_t lf = 0;
336    char *koname;
337
338    koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
339    if (koname == NULL)
340	goto out;
341    sprintf(koname, "%s.ko", filename);
342
343    lockmgr(&lock, LK_SHARED, 0, curproc);
344    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
345	if (!strcmp(lf->filename, koname))
346	    break;
347	if (!strcmp(lf->filename, filename))
348	    break;
349    }
350    lockmgr(&lock, LK_RELEASE, 0, curproc);
351
352out:
353    if (koname)
354	free(koname, M_LINKER);
355    return lf;
356}
357
358linker_file_t
359linker_find_file_by_id(int fileid)
360{
361    linker_file_t lf = 0;
362
363    lockmgr(&lock, LK_SHARED, 0, curproc);
364    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link))
365	if (lf->id == fileid)
366	    break;
367    lockmgr(&lock, LK_RELEASE, 0, curproc);
368
369    return lf;
370}
371
372linker_file_t
373linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
374{
375    linker_file_t lf = 0;
376    int namelen;
377    const char *filename;
378
379    filename = rindex(pathname, '/');
380    if (filename && filename[1])
381	filename++;
382    else
383	filename = pathname;
384
385    KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
386    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
387    namelen = strlen(filename) + 1;
388    lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK);
389    if (!lf)
390	goto out;
391    bzero(lf, sizeof(*lf));
392
393    lf->refs = 1;
394    lf->userrefs = 0;
395    lf->flags = 0;
396    lf->filename = (char*) (lf + 1);
397    strcpy(lf->filename, filename);
398    lf->id = next_file_id++;
399    lf->ndeps = 0;
400    lf->deps = NULL;
401    STAILQ_INIT(&lf->common);
402    TAILQ_INIT(&lf->modules);
403
404    lf->priv = priv;
405    lf->ops = ops;
406    TAILQ_INSERT_TAIL(&files, lf, link);
407
408out:
409    lockmgr(&lock, LK_RELEASE, 0, curproc);
410    return lf;
411}
412
413int
414linker_file_unload(linker_file_t file)
415{
416    module_t mod, next;
417    struct common_symbol* cp;
418    int error = 0;
419    int i;
420
421    KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
422    lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc);
423    if (file->refs == 1) {
424	KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
425	/*
426	 * Inform any modules associated with this file.
427	 */
428	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
429	    next = module_getfnext(mod);
430
431	    /*
432	     * Give the module a chance to veto the unload.
433	     */
434	    if ((error = module_unload(mod)) != 0) {
435		KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
436			       mod));
437		lockmgr(&lock, LK_RELEASE, 0, curproc);
438		goto out;
439	    }
440
441	    module_release(mod);
442	}
443    }
444
445    file->refs--;
446    if (file->refs > 0) {
447	lockmgr(&lock, LK_RELEASE, 0, curproc);
448	goto out;
449    }
450
451    /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
452    if (file->flags & LINKER_FILE_LINKED) {
453	linker_file_sysuninit(file);
454	linker_file_unregister_sysctls(file);
455    }
456
457    TAILQ_REMOVE(&files, file, link);
458    lockmgr(&lock, LK_RELEASE, 0, curproc);
459
460    for (i = 0; i < file->ndeps; i++)
461	linker_file_unload(file->deps[i]);
462    free(file->deps, M_LINKER);
463
464    for (cp = STAILQ_FIRST(&file->common); cp;
465	 cp = STAILQ_FIRST(&file->common)) {
466	STAILQ_REMOVE(&file->common, cp, common_symbol, link);
467	free(cp, M_LINKER);
468    }
469
470    file->ops->unload(file);
471    free(file, M_LINKER);
472
473out:
474    return error;
475}
476
477int
478linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
479{
480    linker_file_t* newdeps;
481
482    newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
483		     M_LINKER, M_WAITOK);
484    if (newdeps == NULL)
485	return ENOMEM;
486    bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
487
488    if (file->deps) {
489	bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
490	free(file->deps, M_LINKER);
491    }
492    file->deps = newdeps;
493    file->deps[file->ndeps] = dep;
494    file->ndeps++;
495
496    return 0;
497}
498
499caddr_t
500linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
501{
502    c_linker_sym_t sym;
503    linker_symval_t symval;
504    linker_file_t lf;
505    caddr_t address;
506    size_t common_size = 0;
507    int i;
508
509    KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
510		  file, name, deps));
511
512    if (file->ops->lookup_symbol(file, name, &sym) == 0) {
513	file->ops->symbol_values(file, sym, &symval);
514	if (symval.value == 0)
515	    /*
516	     * For commons, first look them up in the dependancies and
517	     * only allocate space if not found there.
518	     */
519	    common_size = symval.size;
520	else {
521	    KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
522	    return symval.value;
523	}
524    }
525
526    if (deps) {
527	for (i = 0; i < file->ndeps; i++) {
528	    address = linker_file_lookup_symbol(file->deps[i], name, 0);
529	    if (address) {
530		KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
531		return address;
532	    }
533	}
534
535	/* If we have not found it in the dependencies, search globally */
536	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
537	    /* But skip the current file if it's on the list */
538	    if (lf == file)
539		continue;
540	    /* And skip the files we searched above */
541	    for (i = 0; i < file->ndeps; i++)
542		if (lf == file->deps[i])
543		    break;
544	    if (i < file->ndeps)
545		continue;
546	    address = linker_file_lookup_symbol(lf, name, 0);
547	    if (address) {
548		KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address));
549		return address;
550	    }
551	}
552    }
553
554    if (common_size > 0) {
555	/*
556	 * This is a common symbol which was not found in the
557	 * dependancies.  We maintain a simple common symbol table in
558	 * the file object.
559	 */
560	struct common_symbol* cp;
561
562	for (cp = STAILQ_FIRST(&file->common); cp;
563	     cp = STAILQ_NEXT(cp, link))
564	    if (!strcmp(cp->name, name)) {
565		KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
566		return cp->address;
567	    }
568
569	/*
570	 * Round the symbol size up to align.
571	 */
572	common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
573	cp = malloc(sizeof(struct common_symbol)
574		    + common_size
575		    + strlen(name) + 1,
576		    M_LINKER, M_WAITOK);
577	if (!cp) {
578	    KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
579	    return 0;
580	}
581	bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
582
583	cp->address = (caddr_t) (cp + 1);
584	cp->name = cp->address + common_size;
585	strcpy(cp->name, name);
586	bzero(cp->address, common_size);
587	STAILQ_INSERT_TAIL(&file->common, cp, link);
588
589	KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
590	return cp->address;
591    }
592
593    KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
594    return 0;
595}
596
597#ifdef DDB
598/*
599 * DDB Helpers.  DDB has to look across multiple files with their own
600 * symbol tables and string tables.
601 *
602 * Note that we do not obey list locking protocols here.  We really don't
603 * need DDB to hang because somebody's got the lock held.  We'll take the
604 * chance that the files list is inconsistant instead.
605 */
606
607int
608linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
609{
610    linker_file_t lf;
611
612    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
613	if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
614	    return 0;
615    }
616    return ENOENT;
617}
618
619int
620linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
621{
622    linker_file_t lf;
623    u_long off = (u_long)value;
624    u_long diff, bestdiff;
625    c_linker_sym_t best;
626    c_linker_sym_t es;
627
628    best = 0;
629    bestdiff = off;
630    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
631	if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
632	    continue;
633	if (es != 0 && diff < bestdiff) {
634	    best = es;
635	    bestdiff = diff;
636	}
637	if (bestdiff == 0)
638	    break;
639    }
640    if (best) {
641	*sym = best;
642	*diffp = bestdiff;
643	return 0;
644    } else {
645	*sym = 0;
646	*diffp = off;
647	return ENOENT;
648    }
649}
650
651int
652linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
653{
654    linker_file_t lf;
655
656    for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
657	if (lf->ops->symbol_values(lf, sym, symval) == 0)
658	    return 0;
659    }
660    return ENOENT;
661}
662
663#endif
664
665/*
666 * Syscalls.
667 */
668
669int
670kldload(struct proc* p, struct kldload_args* uap)
671{
672    char* filename = NULL, *modulename;
673    linker_file_t lf;
674    int error = 0;
675
676    p->p_retval[0] = -1;
677
678    if (securelevel > 0)
679	return EPERM;
680
681    if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
682	return error;
683
684    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
685    if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
686	goto out;
687
688    /* Can't load more than one module with the same name */
689    modulename = rindex(filename, '/');
690    if (modulename == NULL)
691	modulename = filename;
692    if (linker_find_file_by_name(modulename)) {
693	error = EEXIST;
694	goto out;
695    }
696
697    if ((error = linker_load_file(filename, &lf)) != 0)
698	goto out;
699
700    lf->userrefs++;
701    p->p_retval[0] = lf->id;
702
703out:
704    if (filename)
705	free(filename, M_TEMP);
706    return error;
707}
708
709int
710kldunload(struct proc* p, struct kldunload_args* uap)
711{
712    linker_file_t lf;
713    int error = 0;
714
715    if (securelevel > 0)
716	return EPERM;
717
718    if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
719	return error;
720
721    lf = linker_find_file_by_id(SCARG(uap, fileid));
722    if (lf) {
723	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
724	if (lf->userrefs == 0) {
725	    printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
726	    error = EBUSY;
727	    goto out;
728	}
729	lf->userrefs--;
730	error = linker_file_unload(lf);
731	if (error)
732	    lf->userrefs++;
733    } else
734	error = ENOENT;
735
736out:
737    return error;
738}
739
740int
741kldfind(struct proc* p, struct kldfind_args* uap)
742{
743    char* filename = NULL, *modulename;
744    linker_file_t lf;
745    int error = 0;
746
747    p->p_retval[0] = -1;
748
749    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
750    if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
751	goto out;
752
753    modulename = rindex(filename, '/');
754    if (modulename == NULL)
755	modulename = filename;
756
757    lf = linker_find_file_by_name(modulename);
758    if (lf)
759	p->p_retval[0] = lf->id;
760    else
761	error = ENOENT;
762
763out:
764    if (filename)
765	free(filename, M_TEMP);
766    return error;
767}
768
769int
770kldnext(struct proc* p, struct kldnext_args* uap)
771{
772    linker_file_t lf;
773    int error = 0;
774
775    if (SCARG(uap, fileid) == 0) {
776	if (TAILQ_FIRST(&files))
777	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
778	else
779	    p->p_retval[0] = 0;
780	return 0;
781    }
782
783    lf = linker_find_file_by_id(SCARG(uap, fileid));
784    if (lf) {
785	if (TAILQ_NEXT(lf, link))
786	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
787	else
788	    p->p_retval[0] = 0;
789    } else
790	error = ENOENT;
791
792    return error;
793}
794
795int
796kldstat(struct proc* p, struct kldstat_args* uap)
797{
798    linker_file_t lf;
799    int error = 0;
800    int version;
801    struct kld_file_stat* stat;
802    int namelen;
803
804    lf = linker_find_file_by_id(SCARG(uap, fileid));
805    if (!lf) {
806	error = ENOENT;
807	goto out;
808    }
809
810    stat = SCARG(uap, stat);
811
812    /*
813     * Check the version of the user's structure.
814     */
815    if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
816	goto out;
817    if (version != sizeof(struct kld_file_stat)) {
818	error = EINVAL;
819	goto out;
820    }
821
822    namelen = strlen(lf->filename) + 1;
823    if (namelen > MAXPATHLEN)
824	namelen = MAXPATHLEN;
825    if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
826	goto out;
827    if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
828	goto out;
829    if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
830	goto out;
831    if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
832	goto out;
833    if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
834	goto out;
835
836    p->p_retval[0] = 0;
837
838out:
839    return error;
840}
841
842int
843kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
844{
845    linker_file_t lf;
846    int error = 0;
847
848    lf = linker_find_file_by_id(SCARG(uap, fileid));
849    if (lf) {
850	if (TAILQ_FIRST(&lf->modules))
851	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
852	else
853	    p->p_retval[0] = 0;
854    } else
855	error = ENOENT;
856
857    return error;
858}
859
860int
861kldsym(struct proc *p, struct kldsym_args *uap)
862{
863    char *symstr = NULL;
864    c_linker_sym_t sym;
865    linker_symval_t symval;
866    linker_file_t lf;
867    struct kld_sym_lookup lookup;
868    int error = 0;
869
870    if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
871	goto out;
872    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
873	error = EINVAL;
874	goto out;
875    }
876
877    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
878    if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
879	goto out;
880
881    if (SCARG(uap, fileid) != 0) {
882	lf = linker_find_file_by_id(SCARG(uap, fileid));
883	if (lf == NULL) {
884	    error = ENOENT;
885	    goto out;
886	}
887	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
888	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
889	    lookup.symvalue = (u_long)symval.value;
890	    lookup.symsize = symval.size;
891	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
892	} else
893	    error = ENOENT;
894    } else {
895	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
896	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
897		lf->ops->symbol_values(lf, sym, &symval) == 0) {
898		lookup.symvalue = (u_long)symval.value;
899		lookup.symsize = symval.size;
900		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
901		break;
902	    }
903	}
904	if (!lf)
905	    error = ENOENT;
906    }
907out:
908    if (symstr)
909	free(symstr, M_TEMP);
910    return error;
911}
912
913/*
914 * Preloaded module support
915 */
916
917static void
918linker_preload(void* arg)
919{
920    caddr_t		modptr;
921    char		*modname;
922    char		*modtype;
923    linker_file_t	lf;
924    linker_class_t	lc;
925    int			error;
926    struct linker_set	*sysinits;
927    struct sysinit	**sipp;
928    moduledata_t	*moddata;
929
930    modptr = NULL;
931    while ((modptr = preload_search_next_name(modptr)) != NULL) {
932	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
933	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
934	if (modname == NULL) {
935	    printf("Preloaded module at %p does not have a name!\n", modptr);
936	    continue;
937	}
938	if (modtype == NULL) {
939	    printf("Preloaded module at %p does not have a type!\n", modptr);
940	    continue;
941	}
942	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
943	lf = linker_find_file_by_name(modname);
944	if (lf) {
945	    lf->userrefs++;
946	    continue;
947	}
948	lf = NULL;
949	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
950	    error = lc->ops->load_file(modname, &lf);
951	    if (error) {
952		lf = NULL;
953		break;
954	    }
955	}
956	if (lf) {
957	    lf->userrefs++;
958
959	    sysinits = (struct linker_set*)
960		linker_file_lookup_symbol(lf, "sysinit_set", 0);
961	    if (sysinits) {
962		/* HACK ALERT!
963		 * This is to set the sysinit moduledata so that the module
964		 * can attach itself to the correct containing file.
965		 * The sysinit could be run at *any* time.
966		 */
967		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
968		    if ((*sipp)->func == module_register_init) {
969			moddata = (*sipp)->udata;
970			moddata->_file = lf;
971		    }
972		}
973		sysinit_add((struct sysinit **)sysinits->ls_items);
974	    }
975	    linker_file_register_sysctls(lf);
976	}
977    }
978}
979
980SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
981
982/*
983 * Search for a not-loaded module by name.
984 *
985 * Modules may be found in the following locations:
986 *
987 * - preloaded (result is just the module name)
988 * - on disk (result is full path to module)
989 *
990 * If the module name is qualified in any way (contains path, etc.)
991 * the we simply return a copy of it.
992 *
993 * The search path can be manipulated via sysctl.  Note that we use the ';'
994 * character as a separator to be consistent with the bootloader.
995 */
996
997static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
998
999SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1000	      sizeof(linker_path), "module load search path");
1001
1002static char *
1003linker_strdup(const char *str)
1004{
1005    char	*result;
1006
1007    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1008	strcpy(result, str);
1009    return(result);
1010}
1011
1012char *
1013linker_search_path(const char *name)
1014{
1015    struct nameidata	nd;
1016    struct proc		*p = curproc;	/* XXX */
1017    char		*cp, *ep, *result;
1018    int			error;
1019    enum vtype		type;
1020
1021    /* qualified at all? */
1022    if (index(name, '/'))
1023	return(linker_strdup(name));
1024
1025    /* traverse the linker path */
1026    cp = linker_path;
1027    for (;;) {
1028
1029	/* find the end of this component */
1030	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1031	    ;
1032	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
1033	if (result == NULL)	/* actually ENOMEM */
1034	    return(NULL);
1035
1036	strncpy(result, cp, ep - cp);
1037	strcpy(result + (ep - cp), name);
1038
1039	/*
1040	 * Attempt to open the file, and return the path if we succeed and it's
1041	 * a regular file.
1042	 */
1043	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
1044	error = vn_open(&nd, FREAD, 0);
1045	if (error == 0) {
1046	    type = nd.ni_vp->v_type;
1047	    VOP_UNLOCK(nd.ni_vp, 0, p);
1048	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
1049	    if (type == VREG)
1050		return(result);
1051	}
1052	free(result, M_LINKER);
1053
1054	if (*ep == 0)
1055	    break;
1056	cp = ep + 1;
1057    }
1058    return(NULL);
1059}
1060