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