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