1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|
/*-
* Copyright (c) 1997-2000 Doug Rabson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/linker.h>
#include <sys/fcntl.h>
#include <sys/libkern.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/sysctl.h>
#include <vm/vm_zone.h>
#include "linker_if.h"
#ifdef KLD_DEBUG
int kld_debug = 0;
#endif
MALLOC_DEFINE(M_LINKER, "kld", "kernel linker");
linker_file_t linker_current_file;
linker_file_t linker_kernel_file;
static struct lock lock; /* lock for the file list */
static linker_class_list_t classes;
static linker_file_list_t linker_files;
static int next_file_id = 1;
static char *
linker_strdup(const char *str)
{
char *result;
if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL)
strcpy(result, str);
return(result);
}
static void
linker_init(void* arg)
{
lockinit(&lock, PVM, "klink", 0, 0);
TAILQ_INIT(&classes);
TAILQ_INIT(&linker_files);
}
SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0);
int
linker_add_class(linker_class_t lc)
{
kobj_class_compile((kobj_class_t) lc);
TAILQ_INSERT_HEAD(&classes, lc, link);
return 0;
}
static void
linker_file_sysinit(linker_file_t lf)
{
struct linker_set* sysinits;
struct sysinit** sipp;
struct sysinit** xipp;
struct sysinit* save;
const moduledata_t *moddata;
int error;
KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
lf->filename));
sysinits = (struct linker_set*)
linker_file_lookup_symbol(lf, "sysinit_set", 0);
KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits));
if (!sysinits)
return;
/* HACK ALERT! */
for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
if ((*sipp)->func == module_register_init) {
moddata = (*sipp)->udata;
error = module_register(moddata, lf);
if (error)
printf("linker_file_sysinit \"%s\" failed to register! %d\n",
lf->filename, error);
}
}
/*
* Perform a bubble sort of the system initialization objects by
* their subsystem (primary key) and order (secondary key).
*
* Since some things care about execution order, this is the
* operation which ensures continued function.
*/
for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
for (xipp = sipp + 1; *xipp; xipp++) {
if ((*sipp)->subsystem <= (*xipp)->subsystem ||
((*sipp)->subsystem == (*xipp)->subsystem &&
(*sipp)->order <= (*xipp)->order))
continue; /* skip*/
save = *sipp;
*sipp = *xipp;
*xipp = save;
}
}
/*
* Traverse the (now) ordered list of system initialization tasks.
* Perform each task, and continue on to the next task.
*/
for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
if ((*sipp)->subsystem == SI_SUB_DUMMY)
continue; /* skip dummy task(s)*/
/* Call function */
(*((*sipp)->func))((*sipp)->udata);
}
}
static void
linker_file_sysuninit(linker_file_t lf)
{
struct linker_set* sysuninits;
struct sysinit** sipp;
struct sysinit** xipp;
struct sysinit* save;
KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
lf->filename));
sysuninits = (struct linker_set*)
linker_file_lookup_symbol(lf, "sysuninit_set", 0);
KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits));
if (!sysuninits)
return;
/*
* Perform a reverse bubble sort of the system initialization objects
* by their subsystem (primary key) and order (secondary key).
*
* Since some things care about execution order, this is the
* operation which ensures continued function.
*/
for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
for (xipp = sipp + 1; *xipp; xipp++) {
if ((*sipp)->subsystem >= (*xipp)->subsystem ||
((*sipp)->subsystem == (*xipp)->subsystem &&
(*sipp)->order >= (*xipp)->order))
continue; /* skip*/
save = *sipp;
*sipp = *xipp;
*xipp = save;
}
}
/*
* Traverse the (now) ordered list of system initialization tasks.
* Perform each task, and continue on to the next task.
*/
for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) {
if ((*sipp)->subsystem == SI_SUB_DUMMY)
continue; /* skip dummy task(s)*/
/* Call function */
(*((*sipp)->func))((*sipp)->udata);
}
}
static void
linker_file_register_sysctls(linker_file_t lf)
{
struct linker_set* sysctls;
KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
lf->filename));
sysctls = (struct linker_set*)
linker_file_lookup_symbol(lf, "sysctl_set", 0);
KLD_DPF(FILE, ("linker_file_register_sysctls: SYSCTLs %p\n", sysctls));
if (!sysctls)
return;
sysctl_register_set(sysctls);
}
static void
linker_file_unregister_sysctls(linker_file_t lf)
{
struct linker_set* sysctls;
KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n",
lf->filename));
sysctls = (struct linker_set*)
linker_file_lookup_symbol(lf, "sysctl_set", 0);
KLD_DPF(FILE, ("linker_file_unregister_sysctls: SYSCTLs %p\n", sysctls));
if (!sysctls)
return;
sysctl_unregister_set(sysctls);
}
int
linker_load_file(const char* filename, linker_file_t* result)
{
linker_class_t lc;
linker_file_t lf;
int foundfile, error = 0;
char *koname = NULL;
lf = linker_find_file_by_name(filename);
if (lf) {
KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename));
*result = lf;
lf->refs++;
goto out;
}
koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
if (koname == NULL) {
error = ENOMEM;
goto out;
}
sprintf(koname, "%s.ko", filename);
lf = NULL;
foundfile = 0;
for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n",
filename, lc->desc));
error = LINKER_LOAD_FILE(lc, koname, &lf); /* First with .ko */
if (lf == NULL && error == ENOENT)
error = LINKER_LOAD_FILE(lc, filename, &lf); /* Then try without */
/*
* If we got something other than ENOENT, then it exists but we cannot
* load it for some other reason.
*/
if (error != ENOENT)
foundfile = 1;
if (lf) {
linker_file_register_sysctls(lf);
linker_file_sysinit(lf);
*result = lf;
error = 0;
goto out;
}
}
/*
* Less than ideal, but tells the user whether it failed to load or
* the module was not found.
*/
if (foundfile)
error = ENOEXEC; /* Format not recognised (or unloadable) */
else
error = ENOENT; /* Nothing found */
out:
if (koname)
free(koname, M_LINKER);
return error;
}
linker_file_t
linker_find_file_by_name(const char* filename)
{
linker_file_t lf = 0;
char *koname;
koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
if (koname == NULL)
goto out;
sprintf(koname, "%s.ko", filename);
lockmgr(&lock, LK_SHARED, 0, curproc);
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
if (!strcmp(lf->filename, koname))
break;
if (!strcmp(lf->filename, filename))
break;
}
lockmgr(&lock, LK_RELEASE, 0, curproc);
out:
if (koname)
free(koname, M_LINKER);
return lf;
}
linker_file_t
linker_find_file_by_id(int fileid)
{
linker_file_t lf = 0;
lockmgr(&lock, LK_SHARED, 0, curproc);
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link))
if (lf->id == fileid)
break;
lockmgr(&lock, LK_RELEASE, 0, curproc);
return lf;
}
linker_file_t
linker_make_file(const char* pathname, linker_class_t lc)
{
linker_file_t lf = 0;
const char *filename;
filename = rindex(pathname, '/');
if (filename && filename[1])
filename++;
else
filename = pathname;
KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename));
lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
lf = (linker_file_t) kobj_create((kobj_class_t) lc, M_LINKER, M_WAITOK);
if (!lf)
goto out;
lf->refs = 1;
lf->userrefs = 0;
lf->flags = 0;
lf->filename = linker_strdup(filename);
lf->id = next_file_id++;
lf->ndeps = 0;
lf->deps = NULL;
STAILQ_INIT(&lf->common);
TAILQ_INIT(&lf->modules);
TAILQ_INSERT_TAIL(&linker_files, lf, link);
out:
lockmgr(&lock, LK_RELEASE, 0, curproc);
return lf;
}
int
linker_file_unload(linker_file_t file)
{
module_t mod, next;
struct common_symbol* cp;
int error = 0;
int i;
KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
lockmgr(&lock, LK_EXCLUSIVE, 0, curproc);
if (file->refs == 1) {
KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n"));
/*
* Inform any modules associated with this file.
*/
for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
next = module_getfnext(mod);
/*
* Give the module a chance to veto the unload.
*/
if ((error = module_unload(mod)) != 0) {
KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n",
mod));
lockmgr(&lock, LK_RELEASE, 0, curproc);
goto out;
}
module_release(mod);
}
}
file->refs--;
if (file->refs > 0) {
lockmgr(&lock, LK_RELEASE, 0, curproc);
goto out;
}
/* Don't try to run SYSUNINITs if we are unloaded due to a link error */
if (file->flags & LINKER_FILE_LINKED) {
linker_file_sysuninit(file);
linker_file_unregister_sysctls(file);
}
TAILQ_REMOVE(&linker_files, file, link);
lockmgr(&lock, LK_RELEASE, 0, curproc);
for (i = 0; i < file->ndeps; i++)
linker_file_unload(file->deps[i]);
free(file->deps, M_LINKER);
for (cp = STAILQ_FIRST(&file->common); cp;
cp = STAILQ_FIRST(&file->common)) {
STAILQ_REMOVE(&file->common, cp, common_symbol, link);
free(cp, M_LINKER);
}
LINKER_UNLOAD(file);
free(file->filename, M_LINKER);
kobj_delete((kobj_t) file, M_LINKER);
out:
return error;
}
int
linker_file_add_dependancy(linker_file_t file, linker_file_t dep)
{
linker_file_t* newdeps;
newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*),
M_LINKER, M_WAITOK);
if (newdeps == NULL)
return ENOMEM;
bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*));
if (file->deps) {
bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*));
free(file->deps, M_LINKER);
}
file->deps = newdeps;
file->deps[file->ndeps] = dep;
file->ndeps++;
return 0;
}
caddr_t
linker_file_lookup_symbol(linker_file_t file, const char* name, int deps)
{
c_linker_sym_t sym;
linker_symval_t symval;
linker_file_t lf;
caddr_t address;
size_t common_size = 0;
int i;
KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n",
file, name, deps));
if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
LINKER_SYMBOL_VALUES(file, sym, &symval);
if (symval.value == 0)
/*
* For commons, first look them up in the dependancies and
* only allocate space if not found there.
*/
common_size = symval.size;
else {
KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value));
return symval.value;
}
}
if (deps) {
for (i = 0; i < file->ndeps; i++) {
address = linker_file_lookup_symbol(file->deps[i], name, 0);
if (address) {
KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address));
return address;
}
}
/* If we have not found it in the dependencies, search globally */
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
/* But skip the current file if it's on the list */
if (lf == file)
continue;
/* And skip the files we searched above */
for (i = 0; i < file->ndeps; i++)
if (lf == file->deps[i])
break;
if (i < file->ndeps)
continue;
address = linker_file_lookup_symbol(lf, name, 0);
if (address) {
KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address));
return address;
}
}
}
if (common_size > 0) {
/*
* This is a common symbol which was not found in the
* dependancies. We maintain a simple common symbol table in
* the file object.
*/
struct common_symbol* cp;
for (cp = STAILQ_FIRST(&file->common); cp;
cp = STAILQ_NEXT(cp, link))
if (!strcmp(cp->name, name)) {
KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address));
return cp->address;
}
/*
* Round the symbol size up to align.
*/
common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
cp = malloc(sizeof(struct common_symbol)
+ common_size
+ strlen(name) + 1,
M_LINKER, M_WAITOK);
if (!cp) {
KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n"));
return 0;
}
bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1);
cp->address = (caddr_t) (cp + 1);
cp->name = cp->address + common_size;
strcpy(cp->name, name);
bzero(cp->address, common_size);
STAILQ_INSERT_TAIL(&file->common, cp, link);
KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address));
return cp->address;
}
KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
return 0;
}
#ifdef DDB
/*
* DDB Helpers. DDB has to look across multiple files with their own
* symbol tables and string tables.
*
* Note that we do not obey list locking protocols here. We really don't
* need DDB to hang because somebody's got the lock held. We'll take the
* chance that the files list is inconsistant instead.
*/
int
linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
{
linker_file_t lf;
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0)
return 0;
}
return ENOENT;
}
int
linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
{
linker_file_t lf;
u_long off = (uintptr_t)value;
u_long diff, bestdiff;
c_linker_sym_t best;
c_linker_sym_t es;
best = 0;
bestdiff = off;
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
continue;
if (es != 0 && diff < bestdiff) {
best = es;
bestdiff = diff;
}
if (bestdiff == 0)
break;
}
if (best) {
*sym = best;
*diffp = bestdiff;
return 0;
} else {
*sym = 0;
*diffp = off;
return ENOENT;
}
}
int
linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
{
linker_file_t lf;
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0)
return 0;
}
return ENOENT;
}
#endif
/*
* Syscalls.
*/
int
kldload(struct proc* p, struct kldload_args* uap)
{
char* filename = NULL, *modulename;
linker_file_t lf;
int error = 0;
p->p_retval[0] = -1;
if (securelevel > 0)
return EPERM;
if ((error = suser(p)) != 0)
return error;
filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
goto out;
/* Can't load more than one module with the same name */
modulename = rindex(filename, '/');
if (modulename == NULL)
modulename = filename;
else
modulename++;
if (linker_find_file_by_name(modulename)) {
error = EEXIST;
goto out;
}
if ((error = linker_load_file(filename, &lf)) != 0)
goto out;
lf->userrefs++;
p->p_retval[0] = lf->id;
out:
if (filename)
free(filename, M_TEMP);
return error;
}
int
kldunload(struct proc* p, struct kldunload_args* uap)
{
linker_file_t lf;
int error = 0;
if (securelevel > 0)
return EPERM;
if ((error = suser(p)) != 0)
return error;
lf = linker_find_file_by_id(SCARG(uap, fileid));
if (lf) {
KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
if (lf->userrefs == 0) {
printf("linkerunload: attempt to unload file that was loaded by the kernel\n");
error = EBUSY;
goto out;
}
lf->userrefs--;
error = linker_file_unload(lf);
if (error)
lf->userrefs++;
} else
error = ENOENT;
out:
return error;
}
int
kldfind(struct proc* p, struct kldfind_args* uap)
{
char* filename = NULL, *modulename;
linker_file_t lf;
int error = 0;
p->p_retval[0] = -1;
filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0)
goto out;
modulename = rindex(filename, '/');
if (modulename == NULL)
modulename = filename;
lf = linker_find_file_by_name(modulename);
if (lf)
p->p_retval[0] = lf->id;
else
error = ENOENT;
out:
if (filename)
free(filename, M_TEMP);
return error;
}
int
kldnext(struct proc* p, struct kldnext_args* uap)
{
linker_file_t lf;
int error = 0;
if (SCARG(uap, fileid) == 0) {
if (TAILQ_FIRST(&linker_files))
p->p_retval[0] = TAILQ_FIRST(&linker_files)->id;
else
p->p_retval[0] = 0;
return 0;
}
lf = linker_find_file_by_id(SCARG(uap, fileid));
if (lf) {
if (TAILQ_NEXT(lf, link))
p->p_retval[0] = TAILQ_NEXT(lf, link)->id;
else
p->p_retval[0] = 0;
} else
error = ENOENT;
return error;
}
int
kldstat(struct proc* p, struct kldstat_args* uap)
{
linker_file_t lf;
int error = 0;
int version;
struct kld_file_stat* stat;
int namelen;
lf = linker_find_file_by_id(SCARG(uap, fileid));
if (!lf) {
error = ENOENT;
goto out;
}
stat = SCARG(uap, stat);
/*
* Check the version of the user's structure.
*/
if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
goto out;
if (version != sizeof(struct kld_file_stat)) {
error = EINVAL;
goto out;
}
namelen = strlen(lf->filename) + 1;
if (namelen > MAXPATHLEN)
namelen = MAXPATHLEN;
if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0)
goto out;
if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0)
goto out;
if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0)
goto out;
if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0)
goto out;
if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0)
goto out;
p->p_retval[0] = 0;
out:
return error;
}
int
kldfirstmod(struct proc* p, struct kldfirstmod_args* uap)
{
linker_file_t lf;
int error = 0;
lf = linker_find_file_by_id(SCARG(uap, fileid));
if (lf) {
if (TAILQ_FIRST(&lf->modules))
p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules));
else
p->p_retval[0] = 0;
} else
error = ENOENT;
return error;
}
int
kldsym(struct proc *p, struct kldsym_args *uap)
{
char *symstr = NULL;
c_linker_sym_t sym;
linker_symval_t symval;
linker_file_t lf;
struct kld_sym_lookup lookup;
int error = 0;
if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0)
goto out;
if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) {
error = EINVAL;
goto out;
}
symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
goto out;
if (SCARG(uap, fileid) != 0) {
lf = linker_find_file_by_id(SCARG(uap, fileid));
if (lf == NULL) {
error = ENOENT;
goto out;
}
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
lookup.symvalue = (uintptr_t)symval.value;
lookup.symsize = symval.size;
error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
} else
error = ENOENT;
} else {
for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) {
if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
lookup.symvalue = (uintptr_t)symval.value;
lookup.symsize = symval.size;
error = copyout(&lookup, SCARG(uap, data), sizeof(lookup));
break;
}
}
if (!lf)
error = ENOENT;
}
out:
if (symstr)
free(symstr, M_TEMP);
return error;
}
/*
* Preloaded module support
*/
static void
linker_preload(void* arg)
{
caddr_t modptr;
char *modname;
char *modtype;
linker_file_t lf;
linker_class_t lc;
int error;
struct linker_set *sysinits;
struct sysinit **sipp;
const moduledata_t *moddata;
modptr = NULL;
while ((modptr = preload_search_next_name(modptr)) != NULL) {
modname = (char *)preload_search_info(modptr, MODINFO_NAME);
modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
if (modname == NULL) {
printf("Preloaded module at %p does not have a name!\n", modptr);
continue;
}
if (modtype == NULL) {
printf("Preloaded module at %p does not have a type!\n", modptr);
continue;
}
printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr);
lf = linker_find_file_by_name(modname);
if (lf) {
lf->userrefs++;
continue;
}
lf = NULL;
for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) {
error = LINKER_LOAD_FILE(lc, modname, &lf);
if (error) {
lf = NULL;
break;
}
}
if (lf) {
lf->userrefs++;
sysinits = (struct linker_set*)
linker_file_lookup_symbol(lf, "sysinit_set", 0);
if (sysinits) {
/* HACK ALERT!
* This is to set the sysinit moduledata so that the module
* can attach itself to the correct containing file.
* The sysinit could be run at *any* time.
*/
for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) {
if ((*sipp)->func == module_register_init) {
moddata = (*sipp)->udata;
error = module_register(moddata, lf);
if (error)
printf("Preloaded %s \"%s\" failed to register: %d\n",
modtype, modname, error);
}
}
sysinit_add((struct sysinit **)sysinits->ls_items);
}
linker_file_register_sysctls(lf);
}
}
}
SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0);
/*
* Search for a not-loaded module by name.
*
* Modules may be found in the following locations:
*
* - preloaded (result is just the module name)
* - on disk (result is full path to module)
*
* If the module name is qualified in any way (contains path, etc.)
* the we simply return a copy of it.
*
* The search path can be manipulated via sysctl. Note that we use the ';'
* character as a separator to be consistent with the bootloader.
*/
static char linker_path[MAXPATHLEN] = "/;/boot/;/modules/";
SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path,
sizeof(linker_path), "module load search path");
char *
linker_search_path(const char *name)
{
struct nameidata nd;
struct proc *p = curproc; /* XXX */
char *cp, *ep, *result;
int error;
enum vtype type;
/* qualified at all? */
if (index(name, '/'))
return(linker_strdup(name));
/* traverse the linker path */
cp = linker_path;
for (;;) {
/* find the end of this component */
for (ep = cp; (*ep != 0) && (*ep != ';'); ep++)
;
result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK);
if (result == NULL) /* actually ENOMEM */
return(NULL);
strncpy(result, cp, ep - cp);
strcpy(result + (ep - cp), name);
/*
* Attempt to open the file, and return the path if we succeed and it's
* a regular file.
*/
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p);
error = vn_open(&nd, FREAD, 0);
if (error == 0) {
NDFREE(&nd, NDF_ONLY_PNBUF);
type = nd.ni_vp->v_type;
VOP_UNLOCK(nd.ni_vp, 0, p);
vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
if (type == VREG)
return(result);
}
free(result, M_LINKER);
if (*ep == 0)
break;
cp = ep + 1;
}
return(NULL);
}
|