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