1 /*-
2 * Copyright (c) 1997-2000 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/5.2/sys/kern/kern_linker.c 120382 2003-09-23 14:42:38Z fjoe $");
29
30 #include "opt_ddb.h"
31 #include "opt_mac.h"
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
39 #include <sys/proc.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/sx.h>
43 #include <sys/mac.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
46 #include <sys/fcntl.h>
47 #include <sys/libkern.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/sysctl.h>
51
52 #include "linker_if.h"
53
54 #ifdef KLD_DEBUG
55 int kld_debug = 0;
56 #endif
57
58 /*
59 * static char *linker_search_path(const char *name, struct mod_depend
60 * *verinfo);
61 */
62 static const char *linker_basename(const char *path);
63
64 /* Metadata from the static kernel */
65 SET_DECLARE(modmetadata_set, struct mod_metadata);
66
67 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
68
69 linker_file_t linker_kernel_file;
70
71 static struct mtx kld_mtx; /* kernel linker mutex */
72
73 static linker_class_list_t classes;
74 static linker_file_list_t linker_files;
75 static int next_file_id = 1;
76 static int linker_no_more_classes = 0;
77
78 #define LINKER_GET_NEXT_FILE_ID(a) do { \
79 linker_file_t lftmp; \
80 \
81 retry: \
82 mtx_lock(&kld_mtx); \
83 TAILQ_FOREACH(lftmp, &linker_files, link) { \
84 if (next_file_id == lftmp->id) { \
85 next_file_id++; \
86 mtx_unlock(&kld_mtx); \
87 goto retry; \
88 } \
89 } \
90 (a) = next_file_id; \
91 mtx_unlock(&kld_mtx); /* Hold for safe read of id variable */ \
92 } while(0)
93
94
95 /* XXX wrong name; we're looking at version provision tags here, not modules */
96 typedef TAILQ_HEAD(, modlist) modlisthead_t;
97 struct modlist {
98 TAILQ_ENTRY(modlist) link; /* chain together all modules */
99 linker_file_t container;
100 const char *name;
101 int version;
102 };
103 typedef struct modlist *modlist_t;
104 static modlisthead_t found_modules;
105
106 static modlist_t modlist_lookup2(const char *name,
107 struct mod_depend *verinfo);
108
109 static char *
110 linker_strdup(const char *str)
111 {
112 char *result;
113
114 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
115 strcpy(result, str);
116 return (result);
117 }
118
119 static void
120 linker_init(void *arg)
121 {
122
123 mtx_init(&kld_mtx, "kernel linker", NULL, MTX_DEF);
124 TAILQ_INIT(&classes);
125 TAILQ_INIT(&linker_files);
126 }
127
128 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0)
129
130 static void
131 linker_stop_class_add(void *arg)
132 {
133
134 linker_no_more_classes = 1;
135 }
136
137 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL)
138
139 int
140 linker_add_class(linker_class_t lc)
141 {
142
143 /*
144 * We disallow any class registration passt SI_ORDER_ANY
145 * of SI_SUB_KLD.
146 */
147 if (linker_no_more_classes == 1)
148 return (EPERM);
149 kobj_class_compile((kobj_class_t) lc);
150 TAILQ_INSERT_TAIL(&classes, lc, link);
151 return (0);
152 }
153
154 static void
155 linker_file_sysinit(linker_file_t lf)
156 {
157 struct sysinit **start, **stop, **sipp, **xipp, *save;
158
159 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
160 lf->filename));
161
162 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
163 return;
164 /*
165 * Perform a bubble sort of the system initialization objects by
166 * their subsystem (primary key) and order (secondary key).
167 *
168 * Since some things care about execution order, this is the operation
169 * which ensures continued function.
170 */
171 for (sipp = start; sipp < stop; sipp++) {
172 for (xipp = sipp + 1; xipp < stop; xipp++) {
173 if ((*sipp)->subsystem < (*xipp)->subsystem ||
174 ((*sipp)->subsystem == (*xipp)->subsystem &&
175 (*sipp)->order <= (*xipp)->order))
176 continue; /* skip */
177 save = *sipp;
178 *sipp = *xipp;
179 *xipp = save;
180 }
181 }
182
183 /*
184 * Traverse the (now) ordered list of system initialization tasks.
185 * Perform each task, and continue on to the next task.
186 */
187 for (sipp = start; sipp < stop; sipp++) {
188 if ((*sipp)->subsystem == SI_SUB_DUMMY)
189 continue; /* skip dummy task(s) */
190
191 /* Call function */
192 (*((*sipp)->func)) ((*sipp)->udata);
193 }
194 }
195
196 static void
197 linker_file_sysuninit(linker_file_t lf)
198 {
199 struct sysinit **start, **stop, **sipp, **xipp, *save;
200
201 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
202 lf->filename));
203
204 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
205 NULL) != 0)
206 return;
207
208 /*
209 * Perform a reverse bubble sort of the system initialization objects
210 * by their subsystem (primary key) and order (secondary key).
211 *
212 * Since some things care about execution order, this is the operation
213 * which ensures continued function.
214 */
215 for (sipp = start; sipp < stop; sipp++) {
216 for (xipp = sipp + 1; xipp < stop; xipp++) {
217 if ((*sipp)->subsystem > (*xipp)->subsystem ||
218 ((*sipp)->subsystem == (*xipp)->subsystem &&
219 (*sipp)->order >= (*xipp)->order))
220 continue; /* skip */
221 save = *sipp;
222 *sipp = *xipp;
223 *xipp = save;
224 }
225 }
226
227 /*
228 * Traverse the (now) ordered list of system initialization tasks.
229 * Perform each task, and continue on to the next task.
230 */
231 for (sipp = start; sipp < stop; sipp++) {
232 if ((*sipp)->subsystem == SI_SUB_DUMMY)
233 continue; /* skip dummy task(s) */
234
235 /* Call function */
236 (*((*sipp)->func)) ((*sipp)->udata);
237 }
238 }
239
240 static void
241 linker_file_register_sysctls(linker_file_t lf)
242 {
243 struct sysctl_oid **start, **stop, **oidp;
244
245 KLD_DPF(FILE,
246 ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
247 lf->filename));
248
249 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
250 return;
251
252 for (oidp = start; oidp < stop; oidp++)
253 sysctl_register_oid(*oidp);
254 }
255
256 static void
257 linker_file_unregister_sysctls(linker_file_t lf)
258 {
259 struct sysctl_oid **start, **stop, **oidp;
260
261 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs"
262 " for %s\n", lf->filename));
263
264 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
265 return;
266
267 for (oidp = start; oidp < stop; oidp++)
268 sysctl_unregister_oid(*oidp);
269 }
270
271 static int
272 linker_file_register_modules(linker_file_t lf)
273 {
274 struct mod_metadata **start, **stop, **mdp;
275 const moduledata_t *moddata;
276 int error;
277
278 KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
279 " in %s\n", lf->filename));
280
281 if (linker_file_lookup_set(lf, "modmetadata_set", &start,
282 &stop, 0) != 0) {
283 /*
284 * This fallback should be unnecessary, but if we get booted
285 * from boot2 instead of loader and we are missing our
286 * metadata then we have to try the best we can.
287 */
288 if (lf == linker_kernel_file) {
289 start = SET_BEGIN(modmetadata_set);
290 stop = SET_LIMIT(modmetadata_set);
291 } else
292 return (0);
293 }
294 for (mdp = start; mdp < stop; mdp++) {
295 if ((*mdp)->md_type != MDT_MODULE)
296 continue;
297 moddata = (*mdp)->md_data;
298 KLD_DPF(FILE, ("Registering module %s in %s\n",
299 moddata->name, lf->filename));
300 error = module_register(moddata, lf);
301 if (error)
302 printf("Module %s failed to register: %d\n",
303 moddata->name, error);
304 }
305 return (0);
306 }
307
308 static void
309 linker_init_kernel_modules(void)
310 {
311
312 linker_file_register_modules(linker_kernel_file);
313 }
314
315 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0)
316
317 static int
318 linker_load_file(const char *filename, linker_file_t *result)
319 {
320 linker_class_t lc;
321 linker_file_t lf;
322 int foundfile, error = 0;
323
324 /* Refuse to load modules if securelevel raised */
325 if (securelevel > 0)
326 return (EPERM);
327
328 lf = linker_find_file_by_name(filename);
329 if (lf) {
330 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
331 " incrementing refs\n", filename));
332 *result = lf;
333 lf->refs++;
334 goto out;
335 }
336 lf = NULL;
337 foundfile = 0;
338
339 /*
340 * We do not need to protect (lock) classes here because there is
341 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
342 * and there is no class deregistration mechanism at this time.
343 */
344 TAILQ_FOREACH(lc, &classes, link) {
345 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
346 filename));
347 error = LINKER_LOAD_FILE(lc, filename, &lf);
348 /*
349 * If we got something other than ENOENT, then it exists but
350 * we cannot load it for some other reason.
351 */
352 if (error != ENOENT)
353 foundfile = 1;
354 if (lf) {
355 linker_file_register_modules(lf);
356 linker_file_register_sysctls(lf);
357 linker_file_sysinit(lf);
358 lf->flags |= LINKER_FILE_LINKED;
359 *result = lf;
360 error = 0;
361 goto out;
362 }
363 }
364 /*
365 * Less than ideal, but tells the user whether it failed to load or
366 * the module was not found.
367 */
368 if (foundfile) {
369 /*
370 * Format not recognized or otherwise unloadable.
371 * When loading a module that is statically built into
372 * the kernel EEXIST percolates back up as the return
373 * value. Preserve this so that apps like sysinstall
374 * can recognize this special case and not post bogus
375 * dialog boxes.
376 */
377 if (error != EEXIST)
378 error = ENOEXEC;
379 } else
380 error = ENOENT; /* Nothing found */
381 out:
382 return (error);
383 }
384
385 int
386 linker_reference_module(const char *modname, struct mod_depend *verinfo,
387 linker_file_t *result)
388 {
389 modlist_t mod;
390
391 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
392 *result = mod->container;
393 (*result)->refs++;
394 return (0);
395 }
396
397 return (linker_load_module(NULL, modname, NULL, verinfo, result));
398 }
399
400 linker_file_t
401 linker_find_file_by_name(const char *filename)
402 {
403 linker_file_t lf = 0;
404 char *koname;
405
406 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
407 if (koname == NULL)
408 goto out;
409 sprintf(koname, "%s.ko", filename);
410
411 mtx_lock(&kld_mtx);
412 TAILQ_FOREACH(lf, &linker_files, link) {
413 if (strcmp(lf->filename, koname) == 0)
414 break;
415 if (strcmp(lf->filename, filename) == 0)
416 break;
417 }
418 mtx_unlock(&kld_mtx);
419 out:
420 if (koname)
421 free(koname, M_LINKER);
422 return (lf);
423 }
424
425 linker_file_t
426 linker_find_file_by_id(int fileid)
427 {
428 linker_file_t lf = 0;
429
430 mtx_lock(&kld_mtx);
431 TAILQ_FOREACH(lf, &linker_files, link)
432 if (lf->id == fileid)
433 break;
434 mtx_unlock(&kld_mtx);
435 return (lf);
436 }
437
438 linker_file_t
439 linker_make_file(const char *pathname, linker_class_t lc)
440 {
441 linker_file_t lf;
442 const char *filename;
443
444 lf = NULL;
445 filename = linker_basename(pathname);
446
447 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
448 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
449 if (lf == NULL)
450 goto out;
451 lf->refs = 1;
452 lf->userrefs = 0;
453 lf->flags = 0;
454 lf->filename = linker_strdup(filename);
455 LINKER_GET_NEXT_FILE_ID(lf->id);
456 lf->ndeps = 0;
457 lf->deps = NULL;
458 STAILQ_INIT(&lf->common);
459 TAILQ_INIT(&lf->modules);
460 mtx_lock(&kld_mtx);
461 TAILQ_INSERT_TAIL(&linker_files, lf, link);
462 mtx_unlock(&kld_mtx);
463 out:
464 return (lf);
465 }
466
467 int
468 linker_file_unload(linker_file_t file)
469 {
470 module_t mod, next;
471 modlist_t ml, nextml;
472 struct common_symbol *cp;
473 int error, i;
474
475 error = 0;
476
477 /* Refuse to unload modules if securelevel raised. */
478 if (securelevel > 0)
479 return (EPERM);
480 #ifdef MAC
481 error = mac_check_kld_unload(curthread->td_ucred);
482 if (error)
483 return (error);
484 #endif
485
486 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
487 if (file->refs == 1) {
488 KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
489 " informing modules\n"));
490
491 /*
492 * Inform any modules associated with this file.
493 */
494 MOD_XLOCK;
495 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
496 next = module_getfnext(mod);
497 MOD_XUNLOCK;
498
499 /*
500 * Give the module a chance to veto the unload.
501 */
502 if ((error = module_unload(mod)) != 0) {
503 KLD_DPF(FILE, ("linker_file_unload: module %p"
504 " vetoes unload\n", mod));
505 goto out;
506 } else
507 MOD_XLOCK;
508 module_release(mod);
509 }
510 MOD_XUNLOCK;
511 }
512 file->refs--;
513 if (file->refs > 0) {
514 goto out;
515 }
516 for (ml = TAILQ_FIRST(&found_modules); ml; ml = nextml) {
517 nextml = TAILQ_NEXT(ml, link);
518 if (ml->container == file)
519 TAILQ_REMOVE(&found_modules, ml, link);
520 }
521
522 /*
523 * Don't try to run SYSUNINITs if we are unloaded due to a
524 * link error.
525 */
526 if (file->flags & LINKER_FILE_LINKED) {
527 linker_file_sysuninit(file);
528 linker_file_unregister_sysctls(file);
529 }
530 mtx_lock(&kld_mtx);
531 TAILQ_REMOVE(&linker_files, file, link);
532 mtx_unlock(&kld_mtx);
533
534 if (file->deps) {
535 for (i = 0; i < file->ndeps; i++)
536 linker_file_unload(file->deps[i]);
537 free(file->deps, M_LINKER);
538 file->deps = NULL;
539 }
540 for (cp = STAILQ_FIRST(&file->common); cp;
541 cp = STAILQ_FIRST(&file->common)) {
542 STAILQ_REMOVE(&file->common, cp, common_symbol, link);
543 free(cp, M_LINKER);
544 }
545
546 LINKER_UNLOAD(file);
547 if (file->filename) {
548 free(file->filename, M_LINKER);
549 file->filename = NULL;
550 }
551 kobj_delete((kobj_t) file, M_LINKER);
552 out:
553 return (error);
554 }
555
556 int
557 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
558 {
559 linker_file_t *newdeps;
560
561 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t *),
562 M_LINKER, M_WAITOK | M_ZERO);
563 if (newdeps == NULL)
564 return (ENOMEM);
565
566 if (file->deps) {
567 bcopy(file->deps, newdeps,
568 file->ndeps * sizeof(linker_file_t *));
569 free(file->deps, M_LINKER);
570 }
571 file->deps = newdeps;
572 file->deps[file->ndeps] = dep;
573 file->ndeps++;
574 return (0);
575 }
576
577 /*
578 * Locate a linker set and its contents. This is a helper function to avoid
579 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void ***
580 */
581 int
582 linker_file_lookup_set(linker_file_t file, const char *name,
583 void *firstp, void *lastp, int *countp)
584 {
585
586 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
587 }
588
589 caddr_t
590 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
591 {
592 c_linker_sym_t sym;
593 linker_symval_t symval;
594 caddr_t address;
595 size_t common_size = 0;
596 int i;
597
598 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
599 file, name, deps));
600
601 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
602 LINKER_SYMBOL_VALUES(file, sym, &symval);
603 if (symval.value == 0)
604 /*
605 * For commons, first look them up in the
606 * dependencies and only allocate space if not found
607 * there.
608 */
609 common_size = symval.size;
610 else {
611 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
612 ".value=%p\n", symval.value));
613 return (symval.value);
614 }
615 }
616 if (deps) {
617 for (i = 0; i < file->ndeps; i++) {
618 address = linker_file_lookup_symbol(file->deps[i],
619 name, 0);
620 if (address) {
621 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
622 " deps value=%p\n", address));
623 return (address);
624 }
625 }
626 }
627 if (common_size > 0) {
628 /*
629 * This is a common symbol which was not found in the
630 * dependencies. We maintain a simple common symbol table in
631 * the file object.
632 */
633 struct common_symbol *cp;
634
635 STAILQ_FOREACH(cp, &file->common, link) {
636 if (strcmp(cp->name, name) == 0) {
637 KLD_DPF(SYM, ("linker_file_lookup_symbol:"
638 " old common value=%p\n", cp->address));
639 return (cp->address);
640 }
641 }
642 /*
643 * Round the symbol size up to align.
644 */
645 common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
646 cp = malloc(sizeof(struct common_symbol)
647 + common_size + strlen(name) + 1, M_LINKER,
648 M_WAITOK | M_ZERO);
649 if (cp == NULL) {
650 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
651 return (0);
652 }
653 cp->address = (caddr_t)(cp + 1);
654 cp->name = cp->address + common_size;
655 strcpy(cp->name, name);
656 bzero(cp->address, common_size);
657 STAILQ_INSERT_TAIL(&file->common, cp, link);
658
659 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
660 " value=%p\n", cp->address));
661 return (cp->address);
662 }
663 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
664 return (0);
665 }
666
667 #ifdef DDB
668 /*
669 * DDB Helpers. DDB has to look across multiple files with their own symbol
670 * tables and string tables.
671 *
672 * Note that we do not obey list locking protocols here. We really don't need
673 * DDB to hang because somebody's got the lock held. We'll take the chance
674 * that the files list is inconsistant instead.
675 */
676
677 int
678 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
679 {
680 linker_file_t lf;
681
682 TAILQ_FOREACH(lf, &linker_files, link) {
683 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
684 return (0);
685 }
686 return (ENOENT);
687 }
688
689 int
690 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
691 {
692 linker_file_t lf;
693 c_linker_sym_t best, es;
694 u_long diff, bestdiff, off;
695
696 best = 0;
697 off = (uintptr_t)value;
698 bestdiff = off;
699 TAILQ_FOREACH(lf, &linker_files, link) {
700 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
701 continue;
702 if (es != 0 && diff < bestdiff) {
703 best = es;
704 bestdiff = diff;
705 }
706 if (bestdiff == 0)
707 break;
708 }
709 if (best) {
710 *sym = best;
711 *diffp = bestdiff;
712 return (0);
713 } else {
714 *sym = 0;
715 *diffp = off;
716 return (ENOENT);
717 }
718 }
719
720 int
721 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
722 {
723 linker_file_t lf;
724
725 TAILQ_FOREACH(lf, &linker_files, link) {
726 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
727 return (0);
728 }
729 return (ENOENT);
730 }
731 #endif
732
733 /*
734 * Syscalls.
735 */
736 /*
737 * MPSAFE
738 */
739 int
740 kldload(struct thread *td, struct kldload_args *uap)
741 {
742 char *kldname, *modname;
743 char *pathname = NULL;
744 linker_file_t lf;
745 int error = 0;
746
747 td->td_retval[0] = -1;
748
749 mtx_lock(&Giant);
750
751 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
752 goto out;
753
754 if ((error = suser(td)) != 0)
755 goto out;
756
757 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
758 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
759 goto out;
760
761 /*
762 * If path do not contain qualified name or any dot in it
763 * (kldname.ko, or kldname.ver.ko) treat it as interface
764 * name.
765 */
766 if (index(pathname, '/') || index(pathname, '.')) {
767 kldname = pathname;
768 modname = NULL;
769 } else {
770 kldname = NULL;
771 modname = pathname;
772 }
773 error = linker_load_module(kldname, modname, NULL, NULL, &lf);
774 if (error)
775 goto out;
776
777 lf->userrefs++;
778 td->td_retval[0] = lf->id;
779 out:
780 if (pathname)
781 free(pathname, M_TEMP);
782 mtx_unlock(&Giant);
783 return (error);
784 }
785
786 /*
787 * MPSAFE
788 */
789 int
790 kldunload(struct thread *td, struct kldunload_args *uap)
791 {
792 linker_file_t lf;
793 int error = 0;
794
795 mtx_lock(&Giant);
796
797 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
798 goto out;
799
800 if ((error = suser(td)) != 0)
801 goto out;
802
803 lf = linker_find_file_by_id(uap->fileid);
804 if (lf) {
805 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
806 if (lf->userrefs == 0) {
807 printf("kldunload: attempt to unload file that was"
808 " loaded by the kernel\n");
809 error = EBUSY;
810 goto out;
811 }
812 lf->userrefs--;
813 error = linker_file_unload(lf);
814 if (error)
815 lf->userrefs++;
816 } else
817 error = ENOENT;
818 out:
819 mtx_unlock(&Giant);
820 return (error);
821 }
822
823 /*
824 * MPSAFE
825 */
826 int
827 kldfind(struct thread *td, struct kldfind_args *uap)
828 {
829 char *pathname;
830 const char *filename;
831 linker_file_t lf;
832 int error = 0;
833
834 #ifdef MAC
835 error = mac_check_kld_stat(td->td_ucred);
836 if (error)
837 return (error);
838 #endif
839
840 mtx_lock(&Giant);
841 td->td_retval[0] = -1;
842
843 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
844 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
845 goto out;
846
847 filename = linker_basename(pathname);
848 lf = linker_find_file_by_name(filename);
849 if (lf)
850 td->td_retval[0] = lf->id;
851 else
852 error = ENOENT;
853 out:
854 if (pathname)
855 free(pathname, M_TEMP);
856 mtx_unlock(&Giant);
857 return (error);
858 }
859
860 /*
861 * MPSAFE
862 */
863 int
864 kldnext(struct thread *td, struct kldnext_args *uap)
865 {
866 linker_file_t lf;
867 int error = 0;
868
869 #ifdef MAC
870 error = mac_check_kld_stat(td->td_ucred);
871 if (error)
872 return (error);
873 #endif
874
875 mtx_lock(&Giant);
876
877 if (uap->fileid == 0) {
878 mtx_lock(&kld_mtx);
879 if (TAILQ_FIRST(&linker_files))
880 td->td_retval[0] = TAILQ_FIRST(&linker_files)->id;
881 else
882 td->td_retval[0] = 0;
883 mtx_unlock(&kld_mtx);
884 goto out;
885 }
886 lf = linker_find_file_by_id(uap->fileid);
887 if (lf) {
888 if (TAILQ_NEXT(lf, link))
889 td->td_retval[0] = TAILQ_NEXT(lf, link)->id;
890 else
891 td->td_retval[0] = 0;
892 } else
893 error = ENOENT;
894 out:
895 mtx_unlock(&Giant);
896 return (error);
897 }
898
899 /*
900 * MPSAFE
901 */
902 int
903 kldstat(struct thread *td, struct kldstat_args *uap)
904 {
905 linker_file_t lf;
906 int error = 0;
907 int namelen, version;
908 struct kld_file_stat *stat;
909
910 #ifdef MAC
911 error = mac_check_kld_stat(td->td_ucred);
912 if (error)
913 return (error);
914 #endif
915
916 mtx_lock(&Giant);
917
918 lf = linker_find_file_by_id(uap->fileid);
919 if (lf == NULL) {
920 error = ENOENT;
921 goto out;
922 }
923 stat = uap->stat;
924
925 /*
926 * Check the version of the user's structure.
927 */
928 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
929 goto out;
930 if (version != sizeof(struct kld_file_stat)) {
931 error = EINVAL;
932 goto out;
933 }
934 namelen = strlen(lf->filename) + 1;
935 if (namelen > MAXPATHLEN)
936 namelen = MAXPATHLEN;
937 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
938 goto out;
939 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
940 goto out;
941 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
942 goto out;
943 if ((error = copyout(&lf->address, &stat->address,
944 sizeof(caddr_t))) != 0)
945 goto out;
946 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
947 goto out;
948
949 td->td_retval[0] = 0;
950 out:
951 mtx_unlock(&Giant);
952 return (error);
953 }
954
955 /*
956 * MPSAFE
957 */
958 int
959 kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
960 {
961 linker_file_t lf;
962 module_t mp;
963 int error = 0;
964
965 #ifdef MAC
966 error = mac_check_kld_stat(td->td_ucred);
967 if (error)
968 return (error);
969 #endif
970
971 mtx_lock(&Giant);
972 lf = linker_find_file_by_id(uap->fileid);
973 if (lf) {
974 MOD_SLOCK;
975 mp = TAILQ_FIRST(&lf->modules);
976 if (mp != NULL)
977 td->td_retval[0] = module_getid(mp);
978 else
979 td->td_retval[0] = 0;
980 MOD_SUNLOCK;
981 } else
982 error = ENOENT;
983 mtx_unlock(&Giant);
984 return (error);
985 }
986
987 /*
988 * MPSAFE
989 */
990 int
991 kldsym(struct thread *td, struct kldsym_args *uap)
992 {
993 char *symstr = NULL;
994 c_linker_sym_t sym;
995 linker_symval_t symval;
996 linker_file_t lf;
997 struct kld_sym_lookup lookup;
998 int error = 0;
999
1000 #ifdef MAC
1001 error = mac_check_kld_stat(td->td_ucred);
1002 if (error)
1003 return (error);
1004 #endif
1005
1006 mtx_lock(&Giant);
1007
1008 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1009 goto out;
1010 if (lookup.version != sizeof(lookup) ||
1011 uap->cmd != KLDSYM_LOOKUP) {
1012 error = EINVAL;
1013 goto out;
1014 }
1015 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1016 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1017 goto out;
1018 if (uap->fileid != 0) {
1019 lf = linker_find_file_by_id(uap->fileid);
1020 if (lf == NULL) {
1021 error = ENOENT;
1022 goto out;
1023 }
1024 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1025 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1026 lookup.symvalue = (uintptr_t) symval.value;
1027 lookup.symsize = symval.size;
1028 error = copyout(&lookup, uap->data, sizeof(lookup));
1029 } else
1030 error = ENOENT;
1031 } else {
1032 mtx_lock(&kld_mtx);
1033 TAILQ_FOREACH(lf, &linker_files, link) {
1034 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1035 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1036 lookup.symvalue = (uintptr_t)symval.value;
1037 lookup.symsize = symval.size;
1038 error = copyout(&lookup, uap->data,
1039 sizeof(lookup));
1040 break;
1041 }
1042 }
1043 mtx_unlock(&kld_mtx);
1044 if (lf == NULL)
1045 error = ENOENT;
1046 }
1047 out:
1048 if (symstr)
1049 free(symstr, M_TEMP);
1050 mtx_unlock(&Giant);
1051 return (error);
1052 }
1053
1054 /*
1055 * Preloaded module support
1056 */
1057
1058 static modlist_t
1059 modlist_lookup(const char *name, int ver)
1060 {
1061 modlist_t mod;
1062
1063 TAILQ_FOREACH(mod, &found_modules, link) {
1064 if (strcmp(mod->name, name) == 0 &&
1065 (ver == 0 || mod->version == ver))
1066 return (mod);
1067 }
1068 return (NULL);
1069 }
1070
1071 static modlist_t
1072 modlist_lookup2(const char *name, struct mod_depend *verinfo)
1073 {
1074 modlist_t mod, bestmod;
1075 int ver;
1076
1077 if (verinfo == NULL)
1078 return (modlist_lookup(name, 0));
1079 bestmod = NULL;
1080 for (mod = TAILQ_FIRST(&found_modules); mod;
1081 mod = TAILQ_NEXT(mod, link)) {
1082 if (strcmp(mod->name, name) != 0)
1083 continue;
1084 ver = mod->version;
1085 if (ver == verinfo->md_ver_preferred)
1086 return (mod);
1087 if (ver >= verinfo->md_ver_minimum &&
1088 ver <= verinfo->md_ver_maximum &&
1089 (bestmod == NULL || ver > bestmod->version))
1090 bestmod = mod;
1091 }
1092 return (bestmod);
1093 }
1094
1095 static modlist_t
1096 modlist_newmodule(const char *modname, int version, linker_file_t container)
1097 {
1098 modlist_t mod;
1099
1100 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1101 if (mod == NULL)
1102 panic("no memory for module list");
1103 mod->container = container;
1104 mod->name = modname;
1105 mod->version = version;
1106 TAILQ_INSERT_TAIL(&found_modules, mod, link);
1107 return (mod);
1108 }
1109
1110 static void
1111 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1112 struct mod_metadata **stop, int preload)
1113 {
1114 struct mod_metadata *mp, **mdp;
1115 const char *modname;
1116 int ver;
1117
1118 for (mdp = start; mdp < stop; mdp++) {
1119 mp = *mdp;
1120 if (mp->md_type != MDT_VERSION)
1121 continue;
1122 modname = mp->md_cval;
1123 ver = ((struct mod_version *)mp->md_data)->mv_version;
1124 if (modlist_lookup(modname, ver) != NULL) {
1125 printf("module %s already present!\n", modname);
1126 /* XXX what can we do? this is a build error. :-( */
1127 continue;
1128 }
1129 modlist_newmodule(modname, ver, lf);
1130 }
1131 }
1132
1133 static void
1134 linker_preload(void *arg)
1135 {
1136 caddr_t modptr;
1137 const char *modname, *nmodname;
1138 char *modtype;
1139 linker_file_t lf;
1140 linker_class_t lc;
1141 int error;
1142 linker_file_list_t loaded_files;
1143 linker_file_list_t depended_files;
1144 struct mod_metadata *mp, *nmp;
1145 struct mod_metadata **start, **stop, **mdp, **nmdp;
1146 struct mod_depend *verinfo;
1147 int nver;
1148 int resolves;
1149 modlist_t mod;
1150 struct sysinit **si_start, **si_stop;
1151
1152 TAILQ_INIT(&loaded_files);
1153 TAILQ_INIT(&depended_files);
1154 TAILQ_INIT(&found_modules);
1155 error = 0;
1156
1157 modptr = NULL;
1158 while ((modptr = preload_search_next_name(modptr)) != NULL) {
1159 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1160 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1161 if (modname == NULL) {
1162 printf("Preloaded module at %p does not have a"
1163 " name!\n", modptr);
1164 continue;
1165 }
1166 if (modtype == NULL) {
1167 printf("Preloaded module at %p does not have a type!\n",
1168 modptr);
1169 continue;
1170 }
1171 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1172 modptr);
1173 lf = NULL;
1174 TAILQ_FOREACH(lc, &classes, link) {
1175 error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1176 if (error) {
1177 lf = NULL;
1178 break;
1179 }
1180 }
1181 if (lf)
1182 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1183 }
1184
1185 /*
1186 * First get a list of stuff in the kernel.
1187 */
1188 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1189 &stop, NULL) == 0)
1190 linker_addmodules(linker_kernel_file, start, stop, 1);
1191
1192 /*
1193 * this is a once-off kinky bubble sort resolve relocation dependency
1194 * requirements
1195 */
1196 restart:
1197 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1198 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1199 &stop, NULL);
1200 /*
1201 * First, look to see if we would successfully link with this
1202 * stuff.
1203 */
1204 resolves = 1; /* unless we know otherwise */
1205 if (!error) {
1206 for (mdp = start; mdp < stop; mdp++) {
1207 mp = *mdp;
1208 if (mp->md_type != MDT_DEPEND)
1209 continue;
1210 modname = mp->md_cval;
1211 verinfo = mp->md_data;
1212 for (nmdp = start; nmdp < stop; nmdp++) {
1213 nmp = *nmdp;
1214 if (nmp->md_type != MDT_VERSION)
1215 continue;
1216 nmodname = nmp->md_cval;
1217 if (strcmp(modname, nmodname) == 0)
1218 break;
1219 }
1220 if (nmdp < stop) /* it's a self reference */
1221 continue;
1222
1223 /*
1224 * ok, the module isn't here yet, we
1225 * are not finished
1226 */
1227 if (modlist_lookup2(modname, verinfo) == NULL)
1228 resolves = 0;
1229 }
1230 }
1231 /*
1232 * OK, if we found our modules, we can link. So, "provide"
1233 * the modules inside and add it to the end of the link order
1234 * list.
1235 */
1236 if (resolves) {
1237 if (!error) {
1238 for (mdp = start; mdp < stop; mdp++) {
1239 mp = *mdp;
1240 if (mp->md_type != MDT_VERSION)
1241 continue;
1242 modname = mp->md_cval;
1243 nver = ((struct mod_version *)
1244 mp->md_data)->mv_version;
1245 if (modlist_lookup(modname,
1246 nver) != NULL) {
1247 printf("module %s already"
1248 " present!\n", modname);
1249 linker_file_unload(lf);
1250 TAILQ_REMOVE(&loaded_files,
1251 lf, loaded);
1252 /* we changed tailq next ptr */
1253 goto restart;
1254 }
1255 modlist_newmodule(modname, nver, lf);
1256 }
1257 }
1258 TAILQ_REMOVE(&loaded_files, lf, loaded);
1259 TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1260 /*
1261 * Since we provided modules, we need to restart the
1262 * sort so that the previous files that depend on us
1263 * have a chance. Also, we've busted the tailq next
1264 * pointer with the REMOVE.
1265 */
1266 goto restart;
1267 }
1268 }
1269
1270 /*
1271 * At this point, we check to see what could not be resolved..
1272 */
1273 TAILQ_FOREACH(lf, &loaded_files, loaded) {
1274 printf("KLD file %s is missing dependencies\n", lf->filename);
1275 linker_file_unload(lf);
1276 TAILQ_REMOVE(&loaded_files, lf, loaded);
1277 }
1278
1279 /*
1280 * We made it. Finish off the linking in the order we determined.
1281 */
1282 TAILQ_FOREACH(lf, &depended_files, loaded) {
1283 if (linker_kernel_file) {
1284 linker_kernel_file->refs++;
1285 error = linker_file_add_dependency(lf,
1286 linker_kernel_file);
1287 if (error)
1288 panic("cannot add dependency");
1289 }
1290 lf->userrefs++; /* so we can (try to) kldunload it */
1291 error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1292 &stop, NULL);
1293 if (!error) {
1294 for (mdp = start; mdp < stop; mdp++) {
1295 mp = *mdp;
1296 if (mp->md_type != MDT_DEPEND)
1297 continue;
1298 modname = mp->md_cval;
1299 verinfo = mp->md_data;
1300 mod = modlist_lookup2(modname, verinfo);
1301 mod->container->refs++;
1302 error = linker_file_add_dependency(lf,
1303 mod->container);
1304 if (error)
1305 panic("cannot add dependency");
1306 }
1307 }
1308 /*
1309 * Now do relocation etc using the symbol search paths
1310 * established by the dependencies
1311 */
1312 error = LINKER_LINK_PRELOAD_FINISH(lf);
1313 if (error) {
1314 printf("KLD file %s - could not finalize loading\n",
1315 lf->filename);
1316 linker_file_unload(lf);
1317 continue;
1318 }
1319 linker_file_register_modules(lf);
1320 if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1321 &si_stop, NULL) == 0)
1322 sysinit_add(si_start, si_stop);
1323 linker_file_register_sysctls(lf);
1324 lf->flags |= LINKER_FILE_LINKED;
1325 }
1326 /* woohoo! we made it! */
1327 }
1328
1329 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0)
1330
1331 /*
1332 * Search for a not-loaded module by name.
1333 *
1334 * Modules may be found in the following locations:
1335 *
1336 * - preloaded (result is just the module name) - on disk (result is full path
1337 * to module)
1338 *
1339 * If the module name is qualified in any way (contains path, etc.) the we
1340 * simply return a copy of it.
1341 *
1342 * The search path can be manipulated via sysctl. Note that we use the ';'
1343 * character as a separator to be consistent with the bootloader.
1344 */
1345
1346 static char linker_hintfile[] = "linker.hints";
1347 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1348
1349 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
1350 sizeof(linker_path), "module load search path");
1351
1352 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1353
1354 static char *linker_ext_list[] = {
1355 "",
1356 ".ko",
1357 NULL
1358 };
1359
1360 /*
1361 * Check if file actually exists either with or without extension listed in
1362 * the linker_ext_list. (probably should be generic for the rest of the
1363 * kernel)
1364 */
1365 static char *
1366 linker_lookup_file(const char *path, int pathlen, const char *name,
1367 int namelen, struct vattr *vap)
1368 {
1369 struct nameidata nd;
1370 struct thread *td = curthread; /* XXX */
1371 char *result, **cpp, *sep;
1372 int error, len, extlen, reclen, flags;
1373 enum vtype type;
1374
1375 extlen = 0;
1376 for (cpp = linker_ext_list; *cpp; cpp++) {
1377 len = strlen(*cpp);
1378 if (len > extlen)
1379 extlen = len;
1380 }
1381 extlen++; /* trailing '\0' */
1382 sep = (path[pathlen - 1] != '/') ? "/" : "";
1383
1384 reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1385 result = malloc(reclen, M_LINKER, M_WAITOK);
1386 for (cpp = linker_ext_list; *cpp; cpp++) {
1387 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1388 namelen, name, *cpp);
1389 /*
1390 * Attempt to open the file, and return the path if
1391 * we succeed and it's a regular file.
1392 */
1393 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td);
1394 flags = FREAD;
1395 error = vn_open(&nd, &flags, 0, -1);
1396 if (error == 0) {
1397 NDFREE(&nd, NDF_ONLY_PNBUF);
1398 type = nd.ni_vp->v_type;
1399 if (vap)
1400 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
1401 VOP_UNLOCK(nd.ni_vp, 0, td);
1402 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1403 if (type == VREG)
1404 return (result);
1405 }
1406 }
1407 free(result, M_LINKER);
1408 return (NULL);
1409 }
1410
1411 #define INT_ALIGN(base, ptr) ptr = \
1412 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
1413
1414 /*
1415 * Lookup KLD which contains requested module in the "linker.hints" file. If
1416 * version specification is available, then try to find the best KLD.
1417 * Otherwise just find the latest one.
1418 */
1419 static char *
1420 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1421 int modnamelen, struct mod_depend *verinfo)
1422 {
1423 struct thread *td = curthread; /* XXX */
1424 struct ucred *cred = td ? td->td_ucred : NULL;
1425 struct nameidata nd;
1426 struct vattr vattr, mattr;
1427 u_char *hints = NULL;
1428 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep;
1429 int error, ival, bestver, *intp, reclen, found, flags, clen, blen;
1430
1431 result = NULL;
1432 bestver = found = 0;
1433
1434 sep = (path[pathlen - 1] != '/') ? "/" : "";
1435 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
1436 strlen(sep) + 1;
1437 pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
1438 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
1439 linker_hintfile);
1440
1441 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td);
1442 flags = FREAD;
1443 error = vn_open(&nd, &flags, 0, -1);
1444 if (error)
1445 goto bad;
1446 NDFREE(&nd, NDF_ONLY_PNBUF);
1447 if (nd.ni_vp->v_type != VREG)
1448 goto bad;
1449 best = cp = NULL;
1450 error = VOP_GETATTR(nd.ni_vp, &vattr, cred, td);
1451 if (error)
1452 goto bad;
1453 /*
1454 * XXX: we need to limit this number to some reasonable value
1455 */
1456 if (vattr.va_size > 100 * 1024) {
1457 printf("hints file too large %ld\n", (long)vattr.va_size);
1458 goto bad;
1459 }
1460 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
1461 if (hints == NULL)
1462 goto bad;
1463 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
1464 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
1465 if (error)
1466 goto bad;
1467 VOP_UNLOCK(nd.ni_vp, 0, td);
1468 vn_close(nd.ni_vp, FREAD, cred, td);
1469 nd.ni_vp = NULL;
1470 if (reclen != 0) {
1471 printf("can't read %d\n", reclen);
1472 goto bad;
1473 }
1474 intp = (int *)hints;
1475 ival = *intp++;
1476 if (ival != LINKER_HINTS_VERSION) {
1477 printf("hints file version mismatch %d\n", ival);
1478 goto bad;
1479 }
1480 bufend = hints + vattr.va_size;
1481 recptr = (u_char *)intp;
1482 clen = blen = 0;
1483 while (recptr < bufend && !found) {
1484 intp = (int *)recptr;
1485 reclen = *intp++;
1486 ival = *intp++;
1487 cp = (char *)intp;
1488 switch (ival) {
1489 case MDT_VERSION:
1490 clen = *cp++;
1491 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1492 break;
1493 cp += clen;
1494 INT_ALIGN(hints, cp);
1495 ival = *(int *)cp;
1496 cp += sizeof(int);
1497 clen = *cp++;
1498 if (verinfo == NULL ||
1499 ival == verinfo->md_ver_preferred) {
1500 found = 1;
1501 break;
1502 }
1503 if (ival >= verinfo->md_ver_minimum &&
1504 ival <= verinfo->md_ver_maximum &&
1505 ival > bestver) {
1506 bestver = ival;
1507 best = cp;
1508 blen = clen;
1509 }
1510 break;
1511 default:
1512 break;
1513 }
1514 recptr += reclen + sizeof(int);
1515 }
1516 /*
1517 * Finally check if KLD is in the place
1518 */
1519 if (found)
1520 result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
1521 else if (best)
1522 result = linker_lookup_file(path, pathlen, best, blen, &mattr);
1523
1524 /*
1525 * KLD is newer than hints file. What we should do now?
1526 */
1527 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
1528 printf("warning: KLD '%s' is newer than the linker.hints"
1529 " file\n", result);
1530 bad:
1531 free(pathbuf, M_LINKER);
1532 if (hints)
1533 free(hints, M_TEMP);
1534 if (nd.ni_vp != NULL) {
1535 VOP_UNLOCK(nd.ni_vp, 0, td);
1536 vn_close(nd.ni_vp, FREAD, cred, td);
1537 }
1538 /*
1539 * If nothing found or hints is absent - fallback to the old
1540 * way by using "kldname[.ko]" as module name.
1541 */
1542 if (!found && !bestver && result == NULL)
1543 result = linker_lookup_file(path, pathlen, modname,
1544 modnamelen, NULL);
1545 return (result);
1546 }
1547
1548 /*
1549 * Lookup KLD which contains requested module in the all directories.
1550 */
1551 static char *
1552 linker_search_module(const char *modname, int modnamelen,
1553 struct mod_depend *verinfo)
1554 {
1555 char *cp, *ep, *result;
1556
1557 /*
1558 * traverse the linker path
1559 */
1560 for (cp = linker_path; *cp; cp = ep + 1) {
1561 /* find the end of this component */
1562 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1563 result = linker_hints_lookup(cp, ep - cp, modname,
1564 modnamelen, verinfo);
1565 if (result != NULL)
1566 return (result);
1567 if (*ep == 0)
1568 break;
1569 }
1570 return (NULL);
1571 }
1572
1573 /*
1574 * Search for module in all directories listed in the linker_path.
1575 */
1576 static char *
1577 linker_search_kld(const char *name)
1578 {
1579 char *cp, *ep, *result, **cpp;
1580 int extlen, len;
1581
1582 /* qualified at all? */
1583 if (index(name, '/'))
1584 return (linker_strdup(name));
1585
1586 extlen = 0;
1587 for (cpp = linker_ext_list; *cpp; cpp++) {
1588 len = strlen(*cpp);
1589 if (len > extlen)
1590 extlen = len;
1591 }
1592 extlen++; /* trailing '\0' */
1593
1594 /* traverse the linker path */
1595 len = strlen(name);
1596 for (ep = linker_path; *ep; ep++) {
1597 cp = ep;
1598 /* find the end of this component */
1599 for (; *ep != 0 && *ep != ';'; ep++);
1600 result = linker_lookup_file(cp, ep - cp, name, len, NULL);
1601 if (result != NULL)
1602 return (result);
1603 }
1604 return (NULL);
1605 }
1606
1607 static const char *
1608 linker_basename(const char *path)
1609 {
1610 const char *filename;
1611
1612 filename = rindex(path, '/');
1613 if (filename == NULL)
1614 return path;
1615 if (filename[1])
1616 filename++;
1617 return (filename);
1618 }
1619
1620 /*
1621 * Find a file which contains given module and load it, if "parent" is not
1622 * NULL, register a reference to it.
1623 */
1624 int
1625 linker_load_module(const char *kldname, const char *modname,
1626 struct linker_file *parent, struct mod_depend *verinfo,
1627 struct linker_file **lfpp)
1628 {
1629 linker_file_t lfdep;
1630 const char *filename;
1631 char *pathname;
1632 int error;
1633
1634 if (modname == NULL) {
1635 /*
1636 * We have to load KLD
1637 */
1638 KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
1639 " is not NULL"));
1640 pathname = linker_search_kld(kldname);
1641 } else {
1642 if (modlist_lookup2(modname, verinfo) != NULL)
1643 return (EEXIST);
1644 if (kldname != NULL)
1645 pathname = linker_strdup(kldname);
1646 else if (rootvnode == NULL)
1647 pathname = NULL;
1648 else
1649 /*
1650 * Need to find a KLD with required module
1651 */
1652 pathname = linker_search_module(modname,
1653 strlen(modname), verinfo);
1654 }
1655 if (pathname == NULL)
1656 return (ENOENT);
1657
1658 /*
1659 * Can't load more than one file with the same basename XXX:
1660 * Actually it should be possible to have multiple KLDs with
1661 * the same basename but different path because they can
1662 * provide different versions of the same modules.
1663 */
1664 filename = linker_basename(pathname);
1665 if (linker_find_file_by_name(filename)) {
1666 error = EEXIST;
1667 goto out;
1668 }
1669 do {
1670 error = linker_load_file(pathname, &lfdep);
1671 if (error)
1672 break;
1673 if (modname && verinfo &&
1674 modlist_lookup2(modname, verinfo) == NULL) {
1675 linker_file_unload(lfdep);
1676 error = ENOENT;
1677 break;
1678 }
1679 if (parent) {
1680 error = linker_file_add_dependency(parent, lfdep);
1681 if (error)
1682 break;
1683 }
1684 if (lfpp)
1685 *lfpp = lfdep;
1686 } while (0);
1687 out:
1688 if (pathname)
1689 free(pathname, M_LINKER);
1690 return (error);
1691 }
1692
1693 /*
1694 * This routine is responsible for finding dependencies of userland initiated
1695 * kldload(2)'s of files.
1696 */
1697 int
1698 linker_load_dependencies(linker_file_t lf)
1699 {
1700 linker_file_t lfdep;
1701 struct mod_metadata **start, **stop, **mdp, **nmdp;
1702 struct mod_metadata *mp, *nmp;
1703 struct mod_depend *verinfo;
1704 modlist_t mod;
1705 const char *modname, *nmodname;
1706 int ver, error = 0, count;
1707
1708 /*
1709 * All files are dependant on /kernel.
1710 */
1711 if (linker_kernel_file) {
1712 linker_kernel_file->refs++;
1713 error = linker_file_add_dependency(lf, linker_kernel_file);
1714 if (error)
1715 return (error);
1716 }
1717 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
1718 &count) != 0)
1719 return (0);
1720 for (mdp = start; mdp < stop; mdp++) {
1721 mp = *mdp;
1722 if (mp->md_type != MDT_VERSION)
1723 continue;
1724 modname = mp->md_cval;
1725 ver = ((struct mod_version *)mp->md_data)->mv_version;
1726 mod = modlist_lookup(modname, ver);
1727 if (mod != NULL) {
1728 printf("interface %s.%d already present in the KLD"
1729 " '%s'!\n", modname, ver,
1730 mod->container->filename);
1731 return (EEXIST);
1732 }
1733 }
1734
1735 for (mdp = start; mdp < stop; mdp++) {
1736 mp = *mdp;
1737 if (mp->md_type != MDT_DEPEND)
1738 continue;
1739 modname = mp->md_cval;
1740 verinfo = mp->md_data;
1741 nmodname = NULL;
1742 for (nmdp = start; nmdp < stop; nmdp++) {
1743 nmp = *nmdp;
1744 if (nmp->md_type != MDT_VERSION)
1745 continue;
1746 nmodname = nmp->md_cval;
1747 if (strcmp(modname, nmodname) == 0)
1748 break;
1749 }
1750 if (nmdp < stop)/* early exit, it's a self reference */
1751 continue;
1752 mod = modlist_lookup2(modname, verinfo);
1753 if (mod) { /* woohoo, it's loaded already */
1754 lfdep = mod->container;
1755 lfdep->refs++;
1756 error = linker_file_add_dependency(lf, lfdep);
1757 if (error)
1758 break;
1759 continue;
1760 }
1761 error = linker_load_module(NULL, modname, lf, verinfo, NULL);
1762 if (error) {
1763 printf("KLD %s: depends on %s - not available\n",
1764 lf->filename, modname);
1765 break;
1766 }
1767 }
1768
1769 if (error)
1770 return (error);
1771 linker_addmodules(lf, start, stop, 0);
1772 return (error);
1773 }
1774
1775 static int
1776 sysctl_kern_function_list_iterate(const char *name, void *opaque)
1777 {
1778 struct sysctl_req *req;
1779
1780 req = opaque;
1781 return (SYSCTL_OUT(req, name, strlen(name) + 1));
1782 }
1783
1784 /*
1785 * Export a nul-separated, double-nul-terminated list of all function names
1786 * in the kernel.
1787 */
1788 static int
1789 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
1790 {
1791 linker_file_t lf;
1792 int error;
1793
1794 #ifdef MAC
1795 error = mac_check_kld_stat(req->td->td_ucred);
1796 if (error)
1797 return (error);
1798 #endif
1799 sysctl_wire_old_buffer(req, 0);
1800 mtx_lock(&kld_mtx);
1801 TAILQ_FOREACH(lf, &linker_files, link) {
1802 error = LINKER_EACH_FUNCTION_NAME(lf,
1803 sysctl_kern_function_list_iterate, req);
1804 if (error) {
1805 mtx_unlock(&kld_mtx);
1806 return (error);
1807 }
1808 }
1809 mtx_unlock(&kld_mtx);
1810 return (SYSCTL_OUT(req, "", 1));
1811 }
1812
1813 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLFLAG_RD,
1814 NULL, 0, sysctl_kern_function_list, "", "kernel function list");
Cache object: 75e5fa7d27a15caa511cabd496a47367
|