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