kern_linker - Rearrange locking a bit for module loading.
[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 $
f4e821f3 27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.44 2008/09/01 19:39:44 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>
895c1f85 39#include <sys/priv.h>
984263bc
MD
40#include <sys/lock.h>
41#include <sys/module.h>
89e1aaa0 42#include <sys/queue.h>
984263bc
MD
43#include <sys/linker.h>
44#include <sys/fcntl.h>
45#include <sys/libkern.h>
fad57d0e 46#include <sys/nlookup.h>
984263bc
MD
47#include <sys/vnode.h>
48#include <sys/sysctl.h>
49
50#include <vm/vm_zone.h>
51
684a93c4
MD
52#include <sys/mplock2.h>
53
6ef6faeb
SS
54#ifdef _KERNEL_VIRTUAL
55#include <dlfcn.h>
56#endif
57
984263bc 58#ifdef KLD_DEBUG
1c0e3286 59int kld_debug = 1;
984263bc
MD
60#endif
61
6456e0ad
MD
62/* Metadata from the static kernel */
63SET_DECLARE(modmetadata_set, struct mod_metadata);
984263bc 64MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
6456e0ad 65
984263bc
MD
66linker_file_t linker_current_file;
67linker_file_t linker_kernel_file;
68
69static struct lock lock; /* lock for the file list */
70static linker_class_list_t classes;
71static linker_file_list_t linker_files;
72static int next_file_id = 1;
73
1c0e3286
SS
74/* XXX wrong name; we're looking at version provision tags here, not modules */
75typedef TAILQ_HEAD(, modlist) modlisthead_t;
76struct modlist {
77 TAILQ_ENTRY(modlist) link; /* chain together all modules */
78 linker_file_t container;
79 const char *name;
80 int version;
81};
82typedef struct modlist *modlist_t;
83static modlisthead_t found_modules;
84
85
86static int linker_load_module(const char *kldname, const char *modname,
87 struct linker_file *parent, struct mod_depend *verinfo,
88 struct linker_file **lfpp);
89
90static char *
91linker_strdup(const char *str)
92{
93 char *result;
94
95 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK);
96 strcpy(result, str);
97 return(result);
98}
99
984263bc
MD
100static void
101linker_init(void* arg)
102{
f2770c70 103 lockinit(&lock, "klink", 0, 0);
984263bc
MD
104 TAILQ_INIT(&classes);
105 TAILQ_INIT(&linker_files);
106}
107
ba39e2e0 108SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0);
984263bc
MD
109
110int
111linker_add_class(const char* desc, void* priv,
112 struct linker_class_ops* ops)
113{
114 linker_class_t lc;
115
e7b4468c 116 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO);
984263bc
MD
117 if (!lc)
118 return ENOMEM;
984263bc
MD
119
120 lc->desc = desc;
121 lc->priv = priv;
122 lc->ops = ops;
123 TAILQ_INSERT_HEAD(&classes, lc, link);
124
125 return 0;
126}
127
1c0e3286 128static void
984263bc
MD
129linker_file_sysinit(linker_file_t lf)
130{
dc62b251 131 struct sysinit** start, ** stop;
984263bc
MD
132 struct sysinit** sipp;
133 struct sysinit** xipp;
134 struct sysinit* save;
984263bc
MD
135
136 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
137 lf->filename));
138
dc62b251 139 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
1c0e3286 140 return;
984263bc 141
984263bc
MD
142 /*
143 * Perform a bubble sort of the system initialization objects by
144 * their subsystem (primary key) and order (secondary key).
145 *
146 * Since some things care about execution order, this is the
147 * operation which ensures continued function.
148 */
dc62b251
MD
149 for (sipp = start; sipp < stop; sipp++) {
150 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
151 if ((*sipp)->subsystem < (*xipp)->subsystem ||
152 ((*sipp)->subsystem == (*xipp)->subsystem &&
153 (*sipp)->order <= (*xipp)->order))
154 continue; /* skip*/
155 save = *sipp;
156 *sipp = *xipp;
157 *xipp = save;
158 }
159 }
160
161
162 /*
163 * Traverse the (now) ordered list of system initialization tasks.
164 * Perform each task, and continue on to the next task.
165 */
dc62b251 166 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 167 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
168 continue; /* skip dummy task(s)*/
169
170 /* Call function */
171 (*((*sipp)->func))((*sipp)->udata);
172 }
984263bc
MD
173}
174
175static void
176linker_file_sysuninit(linker_file_t lf)
177{
dc62b251 178 struct sysinit** start, ** stop;
984263bc
MD
179 struct sysinit** sipp;
180 struct sysinit** xipp;
181 struct sysinit* save;
182
183 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
184 lf->filename));
185
dc62b251 186 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0)
984263bc
MD
187 return;
188
189 /*
190 * Perform a reverse bubble sort of the system initialization objects
191 * by their subsystem (primary key) and order (secondary key).
192 *
193 * Since some things care about execution order, this is the
194 * operation which ensures continued function.
195 */
dc62b251
MD
196 for (sipp = start; sipp < stop; sipp++) {
197 for (xipp = sipp + 1; xipp < stop; xipp++) {
984263bc
MD
198 if ((*sipp)->subsystem > (*xipp)->subsystem ||
199 ((*sipp)->subsystem == (*xipp)->subsystem &&
200 (*sipp)->order >= (*xipp)->order))
201 continue; /* skip*/
202 save = *sipp;
203 *sipp = *xipp;
204 *xipp = save;
205 }
206 }
207
208
209 /*
210 * Traverse the (now) ordered list of system initialization tasks.
211 * Perform each task, and continue on to the next task.
212 */
dc62b251 213 for (sipp = start; sipp < stop; sipp++) {
ba39e2e0 214 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY)
984263bc
MD
215 continue; /* skip dummy task(s)*/
216
217 /* Call function */
218 (*((*sipp)->func))((*sipp)->udata);
219 }
220}
221
222static void
223linker_file_register_sysctls(linker_file_t lf)
224{
dc62b251 225 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
226
227 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
228 lf->filename));
229
dc62b251 230 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
d1786c1b 231 return;
dc62b251
MD
232 for (oidp = start; oidp < stop; oidp++)
233 sysctl_register_oid(*oidp);
984263bc
MD
234}
235
236static void
237linker_file_unregister_sysctls(linker_file_t lf)
238{
dc62b251 239 struct sysctl_oid **start, **stop, **oidp;
984263bc
MD
240
241 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
242 lf->filename));
243
dc62b251 244 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
984263bc 245 return;
dc62b251
MD
246 for (oidp = start; oidp < stop; oidp++)
247 sysctl_unregister_oid(*oidp);
984263bc
MD
248}
249
1c0e3286
SS
250static int
251linker_file_register_modules(linker_file_t lf)
252{
253 struct mod_metadata **start, **stop, **mdp;
254 const moduledata_t *moddata;
255 int first_error, error;
256
257 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n",
258 lf->filename));
259
260 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, NULL) != 0) {
261 /*
262 * This fallback should be unnecessary, but if we get booted
263 * from boot2 instead of loader and we are missing our
264 * metadata then we have to try the best we can.
265 */
266 if (lf == linker_kernel_file) {
267 start = SET_BEGIN(modmetadata_set);
268 stop = SET_LIMIT(modmetadata_set);
269 } else
270 return (0);
271 }
272 first_error = 0;
273 for (mdp = start; mdp < stop; mdp++) {
274 if ((*mdp)->md_type != MDT_MODULE)
275 continue;
276 moddata = (*mdp)->md_data;
277 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename));
278 error = module_register(moddata, lf);
279 if (error) {
280 kprintf("Module %s failed to register: %d\n", moddata->name, error);
281 if (first_error == 0)
282 first_error = error;
283 }
284 }
285 return (first_error);
286}
287
288static void
289linker_init_kernel_modules(void)
290{
291
292 linker_file_register_modules(linker_kernel_file);
293}
294
295SYSINIT(linker_kernel, SI_BOOT2_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0);
296
984263bc 297int
1c0e3286 298linker_load_file(const char *filename, linker_file_t *result)
984263bc
MD
299{
300 linker_class_t lc;
301 linker_file_t lf;
302 int foundfile, error = 0;
984263bc
MD
303
304 /* Refuse to load modules if securelevel raised */
7e42c007 305 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
306 return EPERM;
307
308 lf = linker_find_file_by_name(filename);
309 if (lf) {
310 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
311 *result = lf;
312 lf->refs++;
313 goto out;
314 }
315
984263bc
MD
316 lf = NULL;
317 foundfile = 0;
89e1aaa0 318 TAILQ_FOREACH(lc, &classes, link) {
984263bc
MD
319 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
320 filename, lc->desc));
321
1c0e3286 322 error = lc->ops->load_file(filename, &lf);
984263bc
MD
323 /*
324 * If we got something other than ENOENT, then it exists but we cannot
325 * load it for some other reason.
326 */
327 if (error != ENOENT)
328 foundfile = 1;
329 if (lf) {
1c0e3286
SS
330 error = linker_file_register_modules(lf);
331 if (error == EEXIST) {
332 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
333 return (error);
b8fb634a 334 }
b8fb634a 335 linker_file_register_sysctls(lf);
1c0e3286
SS
336 linker_file_sysinit(lf);
337 lf->flags |= LINKER_FILE_LINKED;
984263bc 338 *result = lf;
1c0e3286 339 return (0);
984263bc
MD
340 }
341 }
342 /*
343 * Less than ideal, but tells the user whether it failed to load or
344 * the module was not found.
345 */
828a7fe6
HP
346 if (foundfile) {
347 /*
1c0e3286
SS
348 * If the file type has not been recognized by the last try
349 * printout a message before to fail.
350 */
351 if (error == ENOSYS)
352 kprintf("linker_load_file: Unsupported file type\n");
353
354 /*
828a7fe6
HP
355 * Format not recognized or otherwise unloadable.
356 * When loading a module that is statically built into
357 * the kernel EEXIST percolates back up as the return
22628b14
SW
358 * value. Preserve this so that apps can recognize this
359 * special case.
828a7fe6
HP
360 */
361 if (error != EEXIST)
362 error = ENOEXEC;
32832096 363 } else {
984263bc 364 error = ENOENT; /* Nothing found */
32832096 365 }
984263bc
MD
366
367out:
984263bc
MD
368 return error;
369}
370
32e913d7 371
984263bc
MD
372linker_file_t
373linker_find_file_by_name(const char* filename)
374{
375 linker_file_t lf = 0;
376 char *koname;
a3d55cbb
MD
377 int i;
378
379 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i)
380 ;
381 filename += i;
984263bc 382
efda3bd0 383 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
f8c7a42d 384 ksprintf(koname, "%s.ko", filename);
984263bc 385
df4f70a6 386 lockmgr(&lock, LK_SHARED);
89e1aaa0 387 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
388 if (!strcmp(lf->filename, koname))
389 break;
390 if (!strcmp(lf->filename, filename))
391 break;
392 }
df4f70a6 393 lockmgr(&lock, LK_RELEASE);
984263bc 394
984263bc 395 if (koname)
efda3bd0 396 kfree(koname, M_LINKER);
984263bc
MD
397 return lf;
398}
399
400linker_file_t
401linker_find_file_by_id(int fileid)
402{
403 linker_file_t lf = 0;
404
df4f70a6 405 lockmgr(&lock, LK_SHARED);
89e1aaa0 406 TAILQ_FOREACH(lf, &linker_files, link)
984263bc
MD
407 if (lf->id == fileid)
408 break;
df4f70a6 409 lockmgr(&lock, LK_RELEASE);
984263bc
MD
410
411 return lf;
412}
413
414linker_file_t
415linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops)
416{
417 linker_file_t lf = 0;
984263bc
MD
418 const char *filename;
419
420 filename = rindex(pathname, '/');
421 if (filename && filename[1])
422 filename++;
423 else
424 filename = pathname;
425
426 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
df4f70a6 427 lockmgr(&lock, LK_EXCLUSIVE);
1c0e3286 428 lf = kmalloc(sizeof(struct linker_file), M_LINKER, M_WAITOK);
984263bc
MD
429 bzero(lf, sizeof(*lf));
430
431 lf->refs = 1;
432 lf->userrefs = 0;
433 lf->flags = 0;
1c0e3286 434 lf->filename = linker_strdup(filename);
984263bc
MD
435 lf->id = next_file_id++;
436 lf->ndeps = 0;
437 lf->deps = NULL;
438 STAILQ_INIT(&lf->common);
439 TAILQ_INIT(&lf->modules);
440
441 lf->priv = priv;
442 lf->ops = ops;
443 TAILQ_INSERT_TAIL(&linker_files, lf, link);
444
df4f70a6 445 lockmgr(&lock, LK_RELEASE);
984263bc
MD
446 return lf;
447}
448
449int
450linker_file_unload(linker_file_t file)
451{
1c0e3286
SS
452 module_t mod, next;;
453 modlist_t ml, nextml;
984263bc
MD
454 struct common_symbol* cp;
455 int error = 0;
456 int i;
457
458 /* Refuse to unload modules if securelevel raised */
7e42c007 459 if (securelevel > 0 || kernel_mem_readonly)
984263bc
MD
460 return EPERM;
461
462 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
1c0e3286 463
df4f70a6 464 lockmgr(&lock, LK_EXCLUSIVE);
984263bc 465
1c0e3286
SS
466 /* Easy case of just dropping a reference. */
467 if (file->refs > 1) {
468 file->refs--;
469 lockmgr(&lock, LK_RELEASE);
470 return (0);
471 }
984263bc 472
1c0e3286
SS
473 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
474
475 /*
476 * Inform any modules associated with this file.
477 */
478 mod = TAILQ_FIRST(&file->modules);
479 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
480 next = module_getfnext(mod);
e7a2d403
MD
481
482 /*
1c0e3286
SS
483 * Give the module a chance to veto the unload. Note that the
484 * act of unloading the module may cause other modules in the
485 * same file list to be unloaded recursively.
e7a2d403 486 */
1c0e3286
SS
487 if ((error = module_unload(mod)) != 0) {
488 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n",
489 mod));
490 lockmgr(&lock, LK_RELEASE);
491 file->refs--;
492 goto out;
984263bc 493 }
1c0e3286 494 module_release(mod);
984263bc
MD
495 }
496
1c0e3286
SS
497 TAILQ_FOREACH_MUTABLE(ml, &found_modules, link, nextml) {
498 if (ml->container == file) {
499 TAILQ_REMOVE(&found_modules, ml, link);
500 kfree(ml, M_LINKER);
501 }
984263bc
MD
502 }
503
504 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */
505 if (file->flags & LINKER_FILE_LINKED) {
1c0e3286
SS
506 file->flags &= ~LINKER_FILE_LINKED;
507 lockmgr(&lock, LK_RELEASE);
984263bc
MD
508 linker_file_sysuninit(file);
509 linker_file_unregister_sysctls(file);
1c0e3286 510 lockmgr(&lock, LK_EXCLUSIVE);
984263bc
MD
511 }
512
513 TAILQ_REMOVE(&linker_files, file, link);
984263bc 514
1c0e3286
SS
515 if (file->deps) {
516 lockmgr(&lock, LK_RELEASE);
517 for (i = 0; i < file->ndeps; i++)
518 linker_file_unload(file->deps[i]);
519 lockmgr(&lock, LK_EXCLUSIVE);
520 kfree(file->deps, M_LINKER);
521 file->deps = NULL;
522 }
984263bc 523
1c0e3286
SS
524 while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
525 STAILQ_REMOVE_HEAD(&file->common, link);
efda3bd0 526 kfree(cp, M_LINKER);
984263bc
MD
527 }
528
529 file->ops->unload(file);
1c0e3286
SS
530
531 if (file->filename) {
532 kfree(file->filename, M_LINKER);
533 file->filename = NULL;
534 }
535
efda3bd0 536 kfree(file, M_LINKER);
984263bc 537
1c0e3286
SS
538 lockmgr(&lock, LK_RELEASE);
539
984263bc
MD
540out:
541 return error;
542}
543
addd2777 544void
984263bc
MD
545linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
546{
547 linker_file_t* newdeps;
548
77652cad 549 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*),
e7b4468c 550 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
551
552 if (file->deps) {
553 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
efda3bd0 554 kfree(file->deps, M_LINKER);
984263bc
MD
555 }
556 file->deps = newdeps;
557 file->deps[file->ndeps] = dep;
558 file->ndeps++;
984263bc
MD
559}
560
dc62b251
MD
561/*
562 * Locate a linker set and its contents.
563 * This is a helper function to avoid linker_if.h exposure elsewhere.
564 * Note: firstp and lastp are really void ***
565 */
566int
567linker_file_lookup_set(linker_file_t file, const char *name,
568 void *firstp, void *lastp, int *countp)
569{
570 return file->ops->lookup_set(file, name, firstp, lastp, countp);
571}
572
d1786c1b
MD
573int
574linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr)
984263bc
MD
575{
576 c_linker_sym_t sym;
577 linker_symval_t symval;
578 linker_file_t lf;
984263bc
MD
579 size_t common_size = 0;
580 int i;
581
1c0e3286 582 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
984263bc
MD
583 file, name, deps));
584
585 if (file->ops->lookup_symbol(file, name, &sym) == 0) {
586 file->ops->symbol_values(file, sym, &symval);
d1786c1b
MD
587
588 /*
589 * XXX Assume a common symbol if its value is 0 and it has a non-zero
590 * size, otherwise it could be an absolute symbol with a value of 0.
591 */
592 if (symval.value == 0 && symval.size != 0) {
984263bc
MD
593 /*
594 * For commons, first look them up in the dependancies and
595 * only allocate space if not found there.
596 */
597 common_size = symval.size;
d1786c1b 598 } else {
1c0e3286 599 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%p\n", symval.value));
d1786c1b
MD
600 *raddr = symval.value;
601 return 0;
984263bc
MD
602 }
603 }
984263bc
MD
604 if (deps) {
605 for (i = 0; i < file->ndeps; i++) {
d1786c1b 606 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) {
1c0e3286 607 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%p\n", *raddr));
d1786c1b 608 return 0;
984263bc
MD
609 }
610 }
611
612 /* If we have not found it in the dependencies, search globally */
89e1aaa0 613 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
614 /* But skip the current file if it's on the list */
615 if (lf == file)
616 continue;
617 /* And skip the files we searched above */
618 for (i = 0; i < file->ndeps; i++)
619 if (lf == file->deps[i])
620 break;
621 if (i < file->ndeps)
622 continue;
d1786c1b 623 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) {
1c0e3286 624 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%p\n", *raddr));
d1786c1b 625 return 0;
984263bc
MD
626 }
627 }
628 }
629
630 if (common_size > 0) {
631 /*
632 * This is a common symbol which was not found in the
633 * dependancies. We maintain a simple common symbol table in
634 * the file object.
635 */
636 struct common_symbol* cp;
637
89e1aaa0 638 STAILQ_FOREACH(cp, &file->common, link)
984263bc 639 if (!strcmp(cp->name, name)) {
1c0e3286 640 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%p\n", cp->address));
d1786c1b
MD
641 *raddr = cp->address;
642 return 0;
984263bc
MD
643 }
644
645 /*
646 * Round the symbol size up to align.
647 */
648 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
77652cad 649 cp = kmalloc(sizeof(struct common_symbol)
984263bc
MD
650 + common_size
651 + strlen(name) + 1,
e7b4468c 652 M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
653
654 cp->address = (caddr_t) (cp + 1);
655 cp->name = cp->address + common_size;
656 strcpy(cp->name, name);
657 bzero(cp->address, common_size);
658 STAILQ_INSERT_TAIL(&file->common, cp, link);
659
1c0e3286 660 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%p\n", cp->address));
d1786c1b
MD
661 *raddr = cp->address;
662 return 0;
984263bc
MD
663 }
664
6ef6faeb
SS
665#ifdef _KERNEL_VIRTUAL
666 *raddr = dlsym(RTLD_NEXT, name);
667 if (*raddr != NULL) {
668 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr));
669 return 0;
670 }
671#endif
672
984263bc 673 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
d1786c1b 674 return ENOENT;
984263bc
MD
675}
676
677#ifdef DDB
678/*
679 * DDB Helpers. DDB has to look across multiple files with their own
680 * symbol tables and string tables.
681 *
682 * Note that we do not obey list locking protocols here. We really don't
683 * need DDB to hang because somebody's got the lock held. We'll take the
684 * chance that the files list is inconsistant instead.
685 */
686
687int
688linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
689{
690 linker_file_t lf;
691
89e1aaa0 692 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
693 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0)
694 return 0;
695 }
696 return ENOENT;
697}
698
699int
700linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
701{
702 linker_file_t lf;
703 u_long off = (uintptr_t)value;
704 u_long diff, bestdiff;
705 c_linker_sym_t best;
706 c_linker_sym_t es;
707
708 best = 0;
709 bestdiff = off;
89e1aaa0 710 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
711 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0)
712 continue;
713 if (es != 0 && diff < bestdiff) {
714 best = es;
715 bestdiff = diff;
716 }
717 if (bestdiff == 0)
718 break;
719 }
720 if (best) {
721 *sym = best;
722 *diffp = bestdiff;
723 return 0;
724 } else {
725 *sym = 0;
726 *diffp = off;
727 return ENOENT;
728 }
729}
730
731int
732linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
733{
734 linker_file_t lf;
735
89e1aaa0 736 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
737 if (lf->ops->symbol_values(lf, sym, symval) == 0)
738 return 0;
739 }
740 return ENOENT;
741}
742
743#endif
744
745/*
746 * Syscalls.
3919ced0
MD
747 *
748 * MPALMOSTSAFE
984263bc 749 */
984263bc 750int
753fd850 751sys_kldload(struct kldload_args *uap)
984263bc 752{
dadab5e9 753 struct thread *td = curthread;
1c0e3286
SS
754 char *file;
755 char *kldname, *modname;
984263bc
MD
756 linker_file_t lf;
757 int error = 0;
758
c7114eea 759 uap->sysmsg_result = -1;
984263bc 760
7e42c007 761 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
762 return EPERM;
763
faa1a67e 764 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
984263bc
MD
765 return error;
766
1c0e3286
SS
767 file = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
768 if ((error = copyinstr(uap->file, file, MAXPATHLEN, NULL)) != 0)
984263bc
MD
769 goto out;
770
1c0e3286
SS
771 /*
772 * If file does not contain a qualified name or any dot in it
773 * (kldname.ko, or kldname.ver.ko) treat it as an interface
774 * name.
775 */
776 if (index(file, '/') || index(file, '.')) {
777 kldname = file;
778 modname = NULL;
779 } else {
780 kldname = NULL;
781 modname = file;
984263bc
MD
782 }
783
3919ced0
MD
784 get_mplock();
785 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
786 rel_mplock();
787 if (error)
984263bc
MD
788 goto out;
789
790 lf->userrefs++;
c7114eea 791 uap->sysmsg_result = lf->id;
984263bc
MD
792
793out:
1c0e3286
SS
794 if (file)
795 kfree(file, M_TEMP);
984263bc
MD
796 return error;
797}
798
3919ced0
MD
799/*
800 * MPALMOSTSAFE
801 */
984263bc 802int
753fd850 803sys_kldunload(struct kldunload_args *uap)
984263bc 804{
dadab5e9 805 struct thread *td = curthread;
984263bc
MD
806 linker_file_t lf;
807 int error = 0;
808
7e42c007 809 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */
984263bc
MD
810 return EPERM;
811
faa1a67e 812 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
984263bc
MD
813 return error;
814
3919ced0 815 get_mplock();
ab2eb4eb 816 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
817 if (lf) {
818 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
819 if (lf->userrefs == 0) {
6ea70f76 820 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n");
984263bc
MD
821 error = EBUSY;
822 goto out;
823 }
824 lf->userrefs--;
825 error = linker_file_unload(lf);
826 if (error)
827 lf->userrefs++;
3919ced0 828 } else {
984263bc 829 error = ENOENT;
3919ced0 830 }
984263bc 831out:
3919ced0 832 rel_mplock();
984263bc
MD
833 return error;
834}
835
3919ced0
MD
836/*
837 * MPALMOSTSAFE
838 */
984263bc 839int
753fd850 840sys_kldfind(struct kldfind_args *uap)
984263bc 841{
41c20dac 842 char *filename = NULL, *modulename;
984263bc 843 linker_file_t lf;
3919ced0 844 int error;
984263bc 845
c7114eea 846 uap->sysmsg_result = -1;
984263bc 847
efda3bd0 848 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
ab2eb4eb 849 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0)
984263bc
MD
850 goto out;
851
852 modulename = rindex(filename, '/');
853 if (modulename == NULL)
854 modulename = filename;
855
3919ced0 856 get_mplock();
984263bc
MD
857 lf = linker_find_file_by_name(modulename);
858 if (lf)
c7114eea 859 uap->sysmsg_result = lf->id;
984263bc
MD
860 else
861 error = ENOENT;
3919ced0 862 rel_mplock();
984263bc
MD
863
864out:
865 if (filename)
efda3bd0 866 kfree(filename, M_TEMP);
984263bc
MD
867 return error;
868}
869
3919ced0
MD
870/*
871 * MPALMOSTSAFE
872 */
984263bc 873int
753fd850 874sys_kldnext(struct kldnext_args *uap)
984263bc
MD
875{
876 linker_file_t lf;
877 int error = 0;
878
3919ced0
MD
879 get_mplock();
880 if (uap->fileid == 0) {
1c0e3286 881 lf = TAILQ_FIRST(&linker_files);
3919ced0 882 } else {
1c0e3286
SS
883 lf = linker_find_file_by_id(uap->fileid);
884 if (lf == NULL) {
885 error = ENOENT;
886 goto out;
887 }
888 lf = TAILQ_NEXT(lf, link);
984263bc
MD
889 }
890
1c0e3286
SS
891 /* Skip partially loaded files. */
892 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) {
893 lf = TAILQ_NEXT(lf, link);
894 }
895
896 if (lf)
897 uap->sysmsg_result = lf->id;
898 else
899 uap->sysmsg_result = 0;
984263bc 900
1c0e3286 901out:
3919ced0 902 rel_mplock();
984263bc
MD
903 return error;
904}
905
3919ced0
MD
906/*
907 * MPALMOSTSAFE
908 */
984263bc 909int
753fd850 910sys_kldstat(struct kldstat_args *uap)
984263bc
MD
911{
912 linker_file_t lf;
913 int error = 0;
914 int version;
915 struct kld_file_stat* stat;
916 int namelen;
917
3919ced0 918 get_mplock();
ab2eb4eb 919 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
920 if (!lf) {
921 error = ENOENT;
922 goto out;
923 }
924
ab2eb4eb 925 stat = uap->stat;
984263bc
MD
926
927 /*
928 * Check the version of the user's structure.
929 */
930 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
931 goto out;
932 if (version != sizeof(struct kld_file_stat)) {
933 error = EINVAL;
934 goto out;
935 }
936
937 namelen = strlen(lf->filename) + 1;
938 if (namelen > MAXPATHLEN)
939 namelen = MAXPATHLEN;
940 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
941 goto out;
942 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
943 goto out;
944 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
945 goto out;
946 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
947 goto out;
948 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
949 goto out;
950
c7114eea 951 uap->sysmsg_result = 0;
984263bc
MD
952
953out:
3919ced0 954 rel_mplock();
984263bc
MD
955 return error;
956}
957
3919ced0
MD
958/*
959 * MPALMOSTSAFE
960 */
984263bc 961int
753fd850 962sys_kldfirstmod(struct kldfirstmod_args *uap)
984263bc
MD
963{
964 linker_file_t lf;
965 int error = 0;
966
3919ced0 967 get_mplock();
ab2eb4eb 968 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
969 if (lf) {
970 if (TAILQ_FIRST(&lf->modules))
c7114eea 971 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules));
984263bc 972 else
c7114eea 973 uap->sysmsg_result = 0;
3919ced0 974 } else {
984263bc 975 error = ENOENT;
3919ced0
MD
976 }
977 rel_mplock();
984263bc
MD
978
979 return error;
980}
981
3919ced0
MD
982/*
983 * MPALMOSTSAFE
984 */
984263bc 985int
753fd850 986sys_kldsym(struct kldsym_args *uap)
984263bc
MD
987{
988 char *symstr = NULL;
989 c_linker_sym_t sym;
990 linker_symval_t symval;
991 linker_file_t lf;
992 struct kld_sym_lookup lookup;
993 int error = 0;
994
3919ced0 995 get_mplock();
ab2eb4eb 996 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
984263bc 997 goto out;
ab2eb4eb 998 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) {
984263bc
MD
999 error = EINVAL;
1000 goto out;
1001 }
1002
efda3bd0 1003 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
984263bc
MD
1004 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1005 goto out;
1006
ab2eb4eb
DR
1007 if (uap->fileid != 0) {
1008 lf = linker_find_file_by_id(uap->fileid);
984263bc
MD
1009 if (lf == NULL) {
1010 error = ENOENT;
1011 goto out;
1012 }
1013 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1014 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1015 lookup.symvalue = (uintptr_t)symval.value;
1016 lookup.symsize = symval.size;
ab2eb4eb 1017 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
1018 } else
1019 error = ENOENT;
1020 } else {
89e1aaa0 1021 TAILQ_FOREACH(lf, &linker_files, link) {
984263bc
MD
1022 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 &&
1023 lf->ops->symbol_values(lf, sym, &symval) == 0) {
1024 lookup.symvalue = (uintptr_t)symval.value;
1025 lookup.symsize = symval.size;
ab2eb4eb 1026 error = copyout(&lookup, uap->data, sizeof(lookup));
984263bc
MD
1027 break;
1028 }
1029 }
1030 if (!lf)
1031 error = ENOENT;
1032 }
1033out:
3919ced0 1034 rel_mplock();
984263bc 1035 if (symstr)
efda3bd0 1036 kfree(symstr, M_TEMP);
984263bc
MD
1037 return error;
1038}
1039
32832096 1040/*
1c0e3286 1041 * Preloaded module support
32832096 1042 */
1c0e3286
SS
1043
1044static modlist_t
1045modlist_lookup(const char *name, int ver)
6456e0ad 1046{
1c0e3286 1047 modlist_t mod;
6456e0ad 1048
1c0e3286
SS
1049 TAILQ_FOREACH(mod, &found_modules, link) {
1050 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver))
1051 return (mod);
6456e0ad 1052 }
1c0e3286
SS
1053 return (NULL);
1054}
6456e0ad 1055
1c0e3286
SS
1056static modlist_t
1057modlist_lookup2(const char *name, struct mod_depend *verinfo)
1058{
1059 modlist_t mod, bestmod;
1060 int ver;
1061
1062 if (verinfo == NULL)
1063 return (modlist_lookup(name, 0));
1064 bestmod = NULL;
1065 TAILQ_FOREACH(mod, &found_modules, link) {
1066 if (strcmp(mod->name, name) != 0)
1067 continue;
1068 ver = mod->version;
1069 if (ver == verinfo->md_ver_preferred)
1070 return (mod);
1071 if (ver >= verinfo->md_ver_minimum &&
1072 ver <= verinfo->md_ver_maximum &&
1073 (bestmod == NULL || ver > bestmod->version))
1074 bestmod = mod;
1075 }
1076 return (bestmod);
1077}
1078
32e913d7
JT
1079int
1080linker_reference_module(const char *modname, struct mod_depend *verinfo,
1081 linker_file_t *result)
1082{
1083 modlist_t mod;
1084 int error;
1085
1086 lockmgr(&lock, LK_SHARED);
1087 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
1088 *result = mod->container;
1089 (*result)->refs++;
1090 lockmgr(&lock, LK_RELEASE);
1091 return (0);
1092 }
1093
32e913d7 1094 lockmgr(&lock, LK_RELEASE);
240c3f5a
JT
1095 get_mplock();
1096 error = linker_load_module(NULL, modname, NULL, verinfo, result);
1097 rel_mplock();
32e913d7
JT
1098 return (error);
1099}
1100
1101int
1102linker_release_module(const char *modname, struct mod_depend *verinfo,
1103 linker_file_t lf)
1104{
1105 modlist_t mod;
1106 int error;
1107
1108 lockmgr(&lock, LK_SHARED);
1109 if (lf == NULL) {
1110 KASSERT(modname != NULL,
1111 ("linker_release_module: no file or name"));
1112 mod = modlist_lookup2(modname, verinfo);
1113 if (mod == NULL) {
1114 lockmgr(&lock, LK_RELEASE);
1115 return (ESRCH);
1116 }
1117 lf = mod->container;
1118 } else
1119 KASSERT(modname == NULL && verinfo == NULL,
1120 ("linker_release_module: both file and name"));
32e913d7 1121 lockmgr(&lock, LK_RELEASE);
240c3f5a
JT
1122 get_mplock();
1123 error = linker_file_unload(lf);
1124 rel_mplock();
32e913d7
JT
1125 return (error);
1126}
1127
1c0e3286
SS
1128static modlist_t
1129modlist_newmodule(const char *modname, int version, linker_file_t container)
1130{
1131 modlist_t mod;
1132
1133 mod = kmalloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1134 if (mod == NULL)
1135 panic("no memory for module list");
1136 mod->container = container;
1137 mod->name = modname;
1138 mod->version = version;
1139 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1140 return (mod);
1141}
1142
1143static void
1144linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1145 struct mod_metadata **stop, int preload)
1146{
1147 struct mod_metadata *mp, **mdp;
1148 const char *modname;
1149 int ver;
1150
1151 for (mdp = start; mdp < stop; mdp++) {
1152 mp = *mdp;
1153 if (mp->md_type != MDT_VERSION)
1154 continue;
1155 modname = mp->md_cval;
1156 ver = ((struct mod_version *)mp->md_data)->mv_version;
1157 if (modlist_lookup(modname, ver) != NULL) {
1158 kprintf("module %s already present!\n", modname);
1159 /* XXX what can we do? this is a build error. :-( */
6456e0ad 1160 continue;
6456e0ad 1161 }
1c0e3286 1162 modlist_newmodule(modname, ver, lf);
6456e0ad 1163 }
6456e0ad
MD
1164}
1165
984263bc
MD
1166static void
1167linker_preload(void* arg)
1168{
1169 caddr_t modptr;
1c0e3286 1170 const char *modname, *nmodname;
984263bc 1171 char *modtype;
1c0e3286 1172 linker_file_t lf, nlf;
984263bc
MD
1173 linker_class_t lc;
1174 int error;
1c0e3286
SS
1175 linker_file_list_t loaded_files;
1176 linker_file_list_t depended_files;
1177 struct mod_metadata *mp, *nmp;
1178 struct mod_metadata **start, **stop, **mdp, **nmdp;
1179 struct mod_depend *verinfo;
1180 int nver;
1181 int resolves;
1182 modlist_t mod;
dc62b251 1183 struct sysinit **si_start, **si_stop;
984263bc 1184
1c0e3286
SS
1185 TAILQ_INIT(&loaded_files);
1186 TAILQ_INIT(&depended_files);
1187 TAILQ_INIT(&found_modules);
1188
984263bc
MD
1189 modptr = NULL;
1190 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1191 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1192 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1193 if (modname == NULL) {
6ea70f76 1194 kprintf("Preloaded module at %p does not have a name!\n", modptr);
984263bc
MD
1195 continue;
1196 }
1197 if (modtype == NULL) {
6ea70f76 1198 kprintf("Preloaded module at %p does not have a type!\n", modptr);
984263bc
MD
1199 continue;
1200 }
6456e0ad 1201
1c0e3286
SS
1202 if (bootverbose)
1203 kprintf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
984263bc 1204 lf = NULL;
89e1aaa0 1205 TAILQ_FOREACH(lc, &classes, link) {
1c0e3286
SS
1206 error = lc->ops->preload_file(modname, &lf);
1207 if (!error)
984263bc 1208 break;
1c0e3286 1209 lf = NULL;
984263bc 1210 }
1c0e3286
SS
1211 if (lf)
1212 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1213 }
984263bc 1214
1c0e3286
SS
1215 /*
1216 * First get a list of stuff in the kernel.
1217 */
1218 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1219 &stop, NULL) == 0)
1220 linker_addmodules(linker_kernel_file, start, stop, 1);
1221
1222 /*
1223 * This is a once-off kinky bubble sort to resolve relocation
1224 * dependency requirements.
1225 */
1226restart:
1227 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1228 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1229 /*
1230 * First, look to see if we would successfully link with this
1231 * stuff.
1232 */
1233 resolves = 1; /* unless we know otherwise */
1234 if (!error) {
1235 for (mdp = start; mdp < stop; mdp++) {
1236 mp = *mdp;
1237 if (mp->md_type != MDT_DEPEND)
1238 continue;
1239 modname = mp->md_cval;
1240 verinfo = mp->md_data;
1241 for (nmdp = start; nmdp < stop; nmdp++) {
1242 nmp = *nmdp;
1243 if (nmp->md_type != MDT_VERSION)
1244 continue;
1245 nmodname = nmp->md_cval;
1246 if (strcmp(modname, nmodname) == 0)
1247 break;
1248 }
1249 if (nmdp < stop)/* it's a self reference */
1250 continue;
1251
1252 /*
1253 * ok, the module isn't here yet, we
1254 * are not finished
984263bc 1255 */
1c0e3286
SS
1256 if (modlist_lookup2(modname, verinfo) == NULL)
1257 resolves = 0;
1258 }
1259 }
1260 /*
1261 * OK, if we found our modules, we can link. So, "provide"
1262 * the modules inside and add it to the end of the link order
1263 * list.
1264 */
1265 if (resolves) {
1266 if (!error) {
1267 for (mdp = start; mdp < stop; mdp++) {
1268 mp = *mdp;
1269 if (mp->md_type != MDT_VERSION)
1270 continue;
1271 modname = mp->md_cval;
1272 nver = ((struct mod_version *)mp->md_data)->mv_version;
1273 if (modlist_lookup(modname, nver) != NULL) {
1274 kprintf("module %s already present!\n", modname);
1275 TAILQ_REMOVE(&loaded_files, lf, loaded);
1276 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1277 /* we changed tailq next ptr */
1278 goto restart;
984263bc 1279 }
1c0e3286 1280 modlist_newmodule(modname, nver, lf);
984263bc 1281 }
984263bc 1282 }
1c0e3286
SS
1283 TAILQ_REMOVE(&loaded_files, lf, loaded);
1284 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1285 /*
1286 * Since we provided modules, we need to restart the
1287 * sort so that the previous files that depend on us
1288 * have a chance. Also, we've busted the tailq next
1289 * pointer with the REMOVE.
1290 */
1291 goto restart;
1292 }
1293 }
1294
1295 /*
1296 * At this point, we check to see what could not be resolved..
1297 */
1298 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1299 TAILQ_REMOVE(&loaded_files, lf, loaded);
1300 kprintf("KLD file %s is missing dependencies\n", lf->filename);
1301 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
1302 }
1303
1304 /*
1305 * We made it. Finish off the linking in the order we determined.
1306 */
1307 TAILQ_FOREACH_MUTABLE(lf, &depended_files, loaded, nlf) {
1308 if (linker_kernel_file) {
1309 linker_kernel_file->refs++;
1310 linker_file_add_dependancy(lf, linker_kernel_file);
1311 }
1312 lf->userrefs++;
1313
1314 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL);
1315 if (!error) {
1316 for (mdp = start; mdp < stop; mdp++) {
1317 mp = *mdp;
1318 if (mp->md_type != MDT_DEPEND)
1319 continue;
1320 modname = mp->md_cval;
1321 verinfo = mp->md_data;
1322 mod = modlist_lookup2(modname, verinfo);
1323 /* Don't count self-dependencies */
1324 if (lf == mod->container)
1325 continue;
1326 mod->container->refs++;
1327 linker_file_add_dependancy(lf, mod->container);
1328 }
1329 }
1330 /*
1331 * Now do relocation etc using the symbol search paths
1332 * established by the dependencies
1333 */
1334 error = lf->ops->preload_finish(lf);
1335 if (error) {
1336 TAILQ_REMOVE(&depended_files, lf, loaded);
1337 kprintf("KLD file %s - could not finalize loading\n",
1338 lf->filename);
1339 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */);
1340 continue;
984263bc 1341 }
1c0e3286
SS
1342 linker_file_register_modules(lf);
1343 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0)
1344 sysinit_add(si_start, si_stop);
1345 linker_file_register_sysctls(lf);
1346 lf->flags |= LINKER_FILE_LINKED;
984263bc 1347 }
1c0e3286 1348 /* woohoo! we made it! */
984263bc
MD
1349}
1350
ba39e2e0 1351SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
984263bc
MD
1352
1353/*
1354 * Search for a not-loaded module by name.
1355 *
1356 * Modules may be found in the following locations:
1357 *
1358 * - preloaded (result is just the module name)
1359 * - on disk (result is full path to module)
1360 *
1361 * If the module name is qualified in any way (contains path, etc.)
1362 * the we simply return a copy of it.
1363 *
1364 * The search path can be manipulated via sysctl. Note that we use the ';'
1365 * character as a separator to be consistent with the bootloader.
1366 */
1367
f4e821f3 1368static char linker_path[MAXPATHLEN] = "/boot;/boot/modules;/;/modules";
984263bc
MD
1369
1370SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1371 sizeof(linker_path), "module load search path");
446d0710 1372TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
984263bc 1373
984263bc
MD
1374char *
1375linker_search_path(const char *name)
1376{
fad57d0e 1377 struct nlookupdata nd;
984263bc 1378 char *cp, *ep, *result;
555d1043 1379 size_t name_len, prefix_len;
1c0e3286 1380 size_t result_len;
555d1043 1381 int sep;
984263bc
MD
1382 int error;
1383 enum vtype type;
1c0e3286
SS
1384 const char *exts[] = { "", ".ko", NULL };
1385 const char **ext;
984263bc
MD
1386
1387 /* qualified at all? */
1388 if (index(name, '/'))
1389 return(linker_strdup(name));
1390
1391 /* traverse the linker path */
1392 cp = linker_path;
555d1043 1393 name_len = strlen(name);
984263bc
MD
1394 for (;;) {
1395
1396 /* find the end of this component */
1397 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
1398 ;
555d1043
YT
1399 prefix_len = ep - cp;
1400 /* if this component doesn't end with a slash, add one */
1401 if (ep == cp || *(ep - 1) != '/')
1402 sep = 1;
1403 else
1404 sep = 0;
1405
ad07b101 1406 /*
1c0e3286 1407 * +2+3 : possible separator, plus terminator + possible extension.
ad07b101 1408 */
1c0e3286 1409 result = kmalloc(prefix_len + name_len + 2+3, M_LINKER, M_WAITOK);
984263bc 1410
555d1043
YT
1411 strncpy(result, cp, prefix_len);
1412 if (sep)
1413 result[prefix_len++] = '/';
1414 strcpy(result + prefix_len, name);
984263bc 1415
1c0e3286
SS
1416 result_len = strlen(result);
1417 for (ext = exts; *ext != NULL; ext++) {
1418 strcpy(result + result_len, *ext);
1419
1420 /*
1421 * Attempt to open the file, and return the path if we succeed and it's
1422 * a regular file.
1423 */
1424 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
1425 if (error == 0)
1426 error = vn_open(&nd, NULL, FREAD, 0);
1427 if (error == 0) {
1428 type = nd.nl_open_vp->v_type;
1429 if (type == VREG) {
1430 nlookup_done(&nd);
1431 return (result);
1432 }
fad57d0e 1433 }
1c0e3286 1434 nlookup_done(&nd);
984263bc 1435 }
1c0e3286 1436
efda3bd0 1437 kfree(result, M_LINKER);
984263bc
MD
1438
1439 if (*ep == 0)
1440 break;
1441 cp = ep + 1;
1442 }
1443 return(NULL);
1444}
1c0e3286
SS
1445
1446/*
1447 * Find a file which contains given module and load it, if "parent" is not
1448 * NULL, register a reference to it.
1449 */
1450static int
1451linker_load_module(const char *kldname, const char *modname,
1452 struct linker_file *parent, struct mod_depend *verinfo,
1453 struct linker_file **lfpp)
1454{
1455 linker_file_t lfdep;
1456 const char *filename;
1457 char *pathname;
1458 int error;
1459
1460 if (modname == NULL) {
1461 /*
1462 * We have to load KLD
1463 */
1464 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL"));
1465 pathname = linker_search_path(kldname);
1466 } else {
1467 if (modlist_lookup2(modname, verinfo) != NULL)
1468 return (EEXIST);
1469 if (kldname != NULL)
240c3f5a 1470 {
1c0e3286 1471 pathname = linker_strdup(kldname);
240c3f5a 1472 }
1c0e3286
SS
1473 else if (rootvnode == NULL)
1474 pathname = NULL;
1475 else
240c3f5a 1476 {
1c0e3286 1477 pathname = linker_search_path(modname);
240c3f5a 1478 }
1c0e3286
SS
1479#if 0
1480 /*
1481 * Need to find a KLD with required module
1482 */
1483 pathname = linker_search_module(modname,
1484 strlen(modname), verinfo);
1485#endif
1486 }
1487 if (pathname == NULL)
1488 return (ENOENT);
1489
1490 /*
1491 * Can't load more than one file with the same basename XXX:
1492 * Actually it should be possible to have multiple KLDs with
1493 * the same basename but different path because they can
1494 * provide different versions of the same modules.
1495 */
1496 filename = rindex(pathname, '/');
1497 if (filename == NULL)
1498 filename = filename;
1499 else
1500 filename++;
1501 if (linker_find_file_by_name(filename))
1502 error = EEXIST;
1503 else
1504 do {
1505 error = linker_load_file(pathname, &lfdep);
1506 if (error)
1507 break;
1508 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) {
1509 linker_file_unload(lfdep /* , LINKER_UNLOAD_FORCE */ );
1510 error = ENOENT;
1511 break;
1512 }
1513 if (parent) {
1514 linker_file_add_dependancy(parent, lfdep);
1515 }
1516 if (lfpp)
1517 *lfpp = lfdep;
1518 } while (0);
1519 kfree(pathname, M_LINKER);
1520 return (error);
1521}
1522
1523/*
1524 * This routine is responsible for finding dependencies of userland initiated
1525 * kldload(2)'s of files.
1526 */
1527int
1528linker_load_dependencies(linker_file_t lf)
1529{
1530 linker_file_t lfdep;
1531 struct mod_metadata **start, **stop, **mdp, **nmdp;
1532 struct mod_metadata *mp, *nmp;
1533 struct mod_depend *verinfo;
1534 modlist_t mod;
1535 const char *modname, *nmodname;
1536 int ver, error = 0, count;
1537
1538 /*
1539 * All files are dependant on /kernel.
1540 */
1541 if (linker_kernel_file) {
1542 linker_kernel_file->refs++;
1543 linker_file_add_dependancy(lf, linker_kernel_file);
1544 }
1545 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0)
1546 return (0);
1547 for (mdp = start; mdp < stop; mdp++) {
1548 mp = *mdp;
1549 if (mp->md_type != MDT_VERSION)
1550 continue;
1551 modname = mp->md_cval;
1552 ver = ((struct mod_version *)mp->md_data)->mv_version;
1553 mod = modlist_lookup(modname, ver);
1554 if (mod != NULL) {
1555 kprintf("interface %s.%d already present in the KLD '%s'!\n",
1556 modname, ver, mod->container->filename);
1557 return (EEXIST);
1558 }
1559 }
1560
1561 for (mdp = start; mdp < stop; mdp++) {
1562 mp = *mdp;
1563 if (mp->md_type != MDT_DEPEND)
1564 continue;
1565 modname = mp->md_cval;
1566 verinfo = mp->md_data;
1567 nmodname = NULL;
1568 for (nmdp = start; nmdp < stop; nmdp++) {
1569 nmp = *nmdp;
1570 if (nmp->md_type != MDT_VERSION)
1571 continue;
1572 nmodname = nmp->md_cval;
1573 if (strcmp(modname, nmodname) == 0)
1574 break;
1575 }
1576 if (nmdp < stop) /* early exit, it's a self reference */
1577 continue;
1578 mod = modlist_lookup2(modname, verinfo);
1579 if (mod) { /* woohoo, it's loaded already */
1580 lfdep = mod->container;
1581 lfdep->refs++;
1582 linker_file_add_dependancy(lf, lfdep);
1583 continue;
1584 }
1585 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1586 if (error) {
1587 kprintf("KLD %s: depends on %s - not available or version mismatch\n",
1588 lf->filename, modname);
1589 break;
1590 }
1591 }
1592
1593 if (error)
1594 return (error);
1595 linker_addmodules(lf, start, stop, 0);
1596 return (error);
1597}