kern_linker.c revision 45356
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.28 1999/03/07 16:06:41 dfr 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_register_sysctls(lf);
310	    linker_file_sysinit(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, 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, 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    else
693	modulename++;
694    if (linker_find_file_by_name(modulename)) {
695	error = EEXIST;
696	goto out;
697    }
698
699    if ((error = linker_load_file(filename, &lf)) != 0)
700	goto out;
701
702    lf->userrefs++;
703    p->p_retval[0] = lf->id;
704
705out:
706    if (filename)
707	free(filename, M_TEMP);
708    return error;
709}
710
711int
712kldunload(struct proc* p, struct kldunload_args* uap)
713{
714    linker_file_t lf;
715    int error = 0;
716
717    if (securelevel > 0)
718	return EPERM;
719
720    if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
721	return error;
722
723    lf = linker_find_file_by_id(SCARG(uap, fileid));
724    if (lf) {
725	KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
726	if (lf->userrefs == 0) {
727	    printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
728	    error = EBUSY;
729	    goto out;
730	}
731	lf->userrefs--;
732	error = linker_file_unload(lf);
733	if (error)
734	    lf->userrefs++;
735    } else
736	error = ENOENT;
737
738out:
739    return error;
740}
741
742int
743kldfind(struct proc* p, struct kldfind_args* uap)
744{
745    char* filename = NULL, *modulename;
746    linker_file_t lf;
747    int error = 0;
748
749    p->p_retval[0] = -1;
750
751    filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
752    if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
753	goto out;
754
755    modulename = rindex(filename, '/');
756    if (modulename == NULL)
757	modulename = filename;
758
759    lf = linker_find_file_by_name(modulename);
760    if (lf)
761	p->p_retval[0] = lf->id;
762    else
763	error = ENOENT;
764
765out:
766    if (filename)
767	free(filename, M_TEMP);
768    return error;
769}
770
771int
772kldnext(struct proc* p, struct kldnext_args* uap)
773{
774    linker_file_t lf;
775    int error = 0;
776
777    if (SCARG(uap, fileid) == 0) {
778	if (TAILQ_FIRST(&files))
779	    p->p_retval[0] = TAILQ_FIRST(&files)->id;
780	else
781	    p->p_retval[0] = 0;
782	return 0;
783    }
784
785    lf = linker_find_file_by_id(SCARG(uap, fileid));
786    if (lf) {
787	if (TAILQ_NEXT(lf, link))
788	    p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
789	else
790	    p->p_retval[0] = 0;
791    } else
792	error = ENOENT;
793
794    return error;
795}
796
797int
798kldstat(struct proc* p, struct kldstat_args* uap)
799{
800    linker_file_t lf;
801    int error = 0;
802    int version;
803    struct kld_file_stat* stat;
804    int namelen;
805
806    lf = linker_find_file_by_id(SCARG(uap, fileid));
807    if (!lf) {
808	error = ENOENT;
809	goto out;
810    }
811
812    stat = SCARG(uap, stat);
813
814    /*
815     * Check the version of the user's structure.
816     */
817    if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
818	goto out;
819    if (version != sizeof(struct kld_file_stat)) {
820	error = EINVAL;
821	goto out;
822    }
823
824    namelen = strlen(lf->filename) + 1;
825    if (namelen > MAXPATHLEN)
826	namelen = MAXPATHLEN;
827    if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
828	goto out;
829    if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
830	goto out;
831    if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
832	goto out;
833    if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
834	goto out;
835    if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
836	goto out;
837
838    p->p_retval[0] = 0;
839
840out:
841    return error;
842}
843
844int
845kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
846{
847    linker_file_t lf;
848    int error = 0;
849
850    lf = linker_find_file_by_id(SCARG(uap, fileid));
851    if (lf) {
852	if (TAILQ_FIRST(&lf->modules))
853	    p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
854	else
855	    p->p_retval[0] = 0;
856    } else
857	error = ENOENT;
858
859    return error;
860}
861
862int
863kldsym(struct proc *p, struct kldsym_args *uap)
864{
865    char *symstr = NULL;
866    c_linker_sym_t sym;
867    linker_symval_t symval;
868    linker_file_t lf;
869    struct kld_sym_lookup lookup;
870    int error = 0;
871
872    if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
873	goto out;
874    if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
875	error = EINVAL;
876	goto out;
877    }
878
879    symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
880    if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
881	goto out;
882
883    if (SCARG(uap, fileid) != 0) {
884	lf = linker_find_file_by_id(SCARG(uap, fileid));
885	if (lf == NULL) {
886	    error = ENOENT;
887	    goto out;
888	}
889	if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
890	    lf->ops->symbol_values(lf, sym, &symval) == 0) {
891	    lookup.symvalue = (u_long)symval.value;
892	    lookup.symsize = symval.size;
893	    error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
894	} else
895	    error = ENOENT;
896    } else {
897	for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) {
898	    if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
899		lf->ops->symbol_values(lf, sym, &symval) == 0) {
900		lookup.symvalue = (u_long)symval.value;
901		lookup.symsize = symval.size;
902		error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
903		break;
904	    }
905	}
906	if (!lf)
907	    error = ENOENT;
908    }
909out:
910    if (symstr)
911	free(symstr, M_TEMP);
912    return error;
913}
914
915/*
916 * Preloaded module support
917 */
918
919static void
920linker_preload(void* arg)
921{
922    caddr_t		modptr;
923    char		*modname;
924    char		*modtype;
925    linker_file_t	lf;
926    linker_class_t	lc;
927    int			error;
928    struct linker_set	*sysinits;
929    struct sysinit	**sipp;
930    moduledata_t	*moddata;
931
932    modptr = NULL;
933    while ((modptr = preload_search_next_name(modptr)) != NULL) {
934	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
935	modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
936	if (modname == NULL) {
937	    printf("Preloaded module at %p does not have a name!\n", modptr);
938	    continue;
939	}
940	if (modtype == NULL) {
941	    printf("Preloaded module at %p does not have a type!\n", modptr);
942	    continue;
943	}
944	printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
945	lf = linker_find_file_by_name(modname);
946	if (lf) {
947	    lf->userrefs++;
948	    continue;
949	}
950	lf = NULL;
951	for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
952	    error = lc->ops->load_file(modname, &lf);
953	    if (error) {
954		lf = NULL;
955		break;
956	    }
957	}
958	if (lf) {
959	    lf->userrefs++;
960
961	    sysinits = (struct linker_set*)
962		linker_file_lookup_symbol(lf, "sysinit_set", 0);
963	    if (sysinits) {
964		/* HACK ALERT!
965		 * This is to set the sysinit moduledata so that the module
966		 * can attach itself to the correct containing file.
967		 * The sysinit could be run at *any* time.
968		 */
969		for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
970		    if ((*sipp)->func == module_register_init) {
971			moddata = (*sipp)->udata;
972			moddata->_file = lf;
973		    }
974		}
975		sysinit_add((struct sysinit **)sysinits->ls_items);
976	    }
977	    linker_file_register_sysctls(lf);
978	}
979    }
980}
981
982SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
983
984/*
985 * Search for a not-loaded module by name.
986 *
987 * Modules may be found in the following locations:
988 *
989 * - preloaded (result is just the module name)
990 * - on disk (result is full path to module)
991 *
992 * If the module name is qualified in any way (contains path, etc.)
993 * the we simply return a copy of it.
994 *
995 * The search path can be manipulated via sysctl.  Note that we use the ';'
996 * character as a separator to be consistent with the bootloader.
997 */
998
999static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/";
1000
1001SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1002	      sizeof(linker_path), "module load search path");
1003
1004static char *
1005linker_strdup(const char *str)
1006{
1007    char	*result;
1008
1009    if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
1010	strcpy(result, str);
1011    return(result);
1012}
1013
1014char *
1015linker_search_path(const char *name)
1016{
1017    struct nameidata	nd;
1018    struct proc		*p = curproc;	/* XXX */
1019    char		*cp, *ep, *result;
1020    int			error;
1021    enum vtype		type;
1022
1023    /* qualified at all? */
1024    if (index(name, '/'))
1025	return(linker_strdup(name));
1026
1027    /* traverse the linker path */
1028    cp = linker_path;
1029    for (;;) {
1030
1031	/* find the end of this component */
1032	for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1033	    ;
1034	result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
1035	if (result == NULL)	/* actually ENOMEM */
1036	    return(NULL);
1037
1038	strncpy(result, cp, ep - cp);
1039	strcpy(result + (ep - cp), name);
1040
1041	/*
1042	 * Attempt to open the file, and return the path if we succeed and it's
1043	 * a regular file.
1044	 */
1045	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
1046	error = vn_open(&nd, FREAD, 0);
1047	if (error == 0) {
1048	    type = nd.ni_vp->v_type;
1049	    VOP_UNLOCK(nd.ni_vp, 0, p);
1050	    vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
1051	    if (type == VREG)
1052		return(result);
1053	}
1054	free(result, M_LINKER);
1055
1056	if (*ep == 0)
1057	    break;
1058	cp = ep + 1;
1059    }
1060    return(NULL);
1061}
1062