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