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