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