kern_ksyms.c revision 1.107 1 1.107 mrg /* $NetBSD: kern_ksyms.c,v 1.107 2022/07/15 06:40:24 mrg Exp $ */
2 1.38 skrll
3 1.39 ad /*-
4 1.39 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.39 ad * All rights reserved.
6 1.39 ad *
7 1.39 ad * This code is derived from software developed for The NetBSD Foundation
8 1.39 ad * by Andrew Doran.
9 1.39 ad *
10 1.39 ad * Redistribution and use in source and binary forms, with or without
11 1.39 ad * modification, are permitted provided that the following conditions
12 1.39 ad * are met:
13 1.39 ad * 1. Redistributions of source code must retain the above copyright
14 1.39 ad * notice, this list of conditions and the following disclaimer.
15 1.39 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.39 ad * notice, this list of conditions and the following disclaimer in the
17 1.39 ad * documentation and/or other materials provided with the distribution.
18 1.39 ad *
19 1.39 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.39 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.39 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.39 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.39 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.39 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.39 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.39 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.39 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.39 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.39 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.39 ad */
31 1.39 ad
32 1.1 ragge /*
33 1.1 ragge * Copyright (c) 2001, 2003 Anders Magnusson (ragge (at) ludd.luth.se).
34 1.1 ragge * All rights reserved.
35 1.1 ragge *
36 1.1 ragge * Redistribution and use in source and binary forms, with or without
37 1.1 ragge * modification, are permitted provided that the following conditions
38 1.1 ragge * are met:
39 1.1 ragge * 1. Redistributions of source code must retain the above copyright
40 1.1 ragge * notice, this list of conditions and the following disclaimer.
41 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 ragge * notice, this list of conditions and the following disclaimer in the
43 1.1 ragge * documentation and/or other materials provided with the distribution.
44 1.1 ragge * 3. The name of the author may not be used to endorse or promote products
45 1.1 ragge * derived from this software without specific prior written permission
46 1.1 ragge *
47 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 ragge */
58 1.1 ragge
59 1.1 ragge /*
60 1.1 ragge * Code to deal with in-kernel symbol table management + /dev/ksyms.
61 1.1 ragge *
62 1.1 ragge * For each loaded module the symbol table info is kept track of by a
63 1.1 ragge * struct, placed in a circular list. The first entry is the kernel
64 1.1 ragge * symbol table.
65 1.1 ragge */
66 1.1 ragge
67 1.1 ragge /*
68 1.1 ragge * TODO:
69 1.1 ragge *
70 1.39 ad * Add support for mmap, poll.
71 1.80 uebayasi * Constify tables.
72 1.80 uebayasi * Constify db_symtab and move it to .rodata.
73 1.1 ragge */
74 1.11 jdolecek
75 1.11 jdolecek #include <sys/cdefs.h>
76 1.107 mrg __KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.107 2022/07/15 06:40:24 mrg Exp $");
77 1.1 ragge
78 1.49 pooka #if defined(_KERNEL) && defined(_KERNEL_OPT)
79 1.81 uebayasi #include "opt_copy_symtab.h"
80 1.1 ragge #include "opt_ddb.h"
81 1.56 darran #include "opt_dtrace.h"
82 1.1 ragge #endif
83 1.1 ragge
84 1.39 ad #define _KSYMS_PRIVATE
85 1.39 ad
86 1.1 ragge #include <sys/param.h>
87 1.1 ragge #include <sys/queue.h>
88 1.1 ragge #include <sys/exec.h>
89 1.103 riastrad #include <sys/file.h>
90 1.103 riastrad #include <sys/filedesc.h>
91 1.103 riastrad #include <sys/kauth.h>
92 1.1 ragge #include <sys/systm.h>
93 1.1 ragge #include <sys/conf.h>
94 1.39 ad #include <sys/kmem.h>
95 1.1 ragge #include <sys/proc.h>
96 1.39 ad #include <sys/atomic.h>
97 1.1 ragge #include <sys/ksyms.h>
98 1.90 riastrad #include <sys/kernel.h>
99 1.91 riastrad #include <sys/intr.h>
100 1.104 riastrad #include <sys/pserialize.h>
101 1.103 riastrad #include <sys/stat.h>
102 1.103 riastrad
103 1.103 riastrad #include <uvm/uvm_extern.h>
104 1.1 ragge
105 1.1 ragge #ifdef DDB
106 1.1 ragge #include <ddb/db_output.h>
107 1.1 ragge #endif
108 1.1 ragge
109 1.1 ragge #include "ksyms.h"
110 1.79 christos #if NKSYMS > 0
111 1.77 christos #include "ioconf.h"
112 1.79 christos #endif
113 1.1 ragge
114 1.103 riastrad struct ksyms_snapshot {
115 1.103 riastrad uint64_t ks_refcnt;
116 1.103 riastrad uint64_t ks_gen;
117 1.103 riastrad struct uvm_object *ks_uobj;
118 1.103 riastrad size_t ks_size;
119 1.103 riastrad dev_t ks_dev;
120 1.103 riastrad int ks_maxlen;
121 1.103 riastrad };
122 1.103 riastrad
123 1.83 gson #define KSYMS_MAX_ID 98304
124 1.56 darran #ifdef KDTRACE_HOOKS
125 1.56 darran static uint32_t ksyms_nmap[KSYMS_MAX_ID]; /* sorted symbol table map */
126 1.56 darran #else
127 1.56 darran static uint32_t *ksyms_nmap = NULL;
128 1.56 darran #endif
129 1.56 darran
130 1.39 ad static int ksyms_maxlen;
131 1.39 ad static bool ksyms_initted;
132 1.69 matt static bool ksyms_loaded;
133 1.69 matt static kmutex_t ksyms_lock __cacheline_aligned;
134 1.66 christos static struct ksyms_symtab kernel_symtab;
135 1.103 riastrad static kcondvar_t ksyms_cv;
136 1.103 riastrad static struct lwp *ksyms_snapshotting;
137 1.103 riastrad static struct ksyms_snapshot *ksyms_snapshot;
138 1.103 riastrad static uint64_t ksyms_snapshot_gen;
139 1.104 riastrad static pserialize_t ksyms_psz __read_mostly;
140 1.1 ragge
141 1.80 uebayasi static void ksyms_hdr_init(const void *);
142 1.1 ragge static void ksyms_sizes_calc(void);
143 1.103 riastrad static struct ksyms_snapshot *ksyms_snapshot_alloc(int, size_t, dev_t,
144 1.103 riastrad uint64_t);
145 1.103 riastrad static void ksyms_snapshot_release(struct ksyms_snapshot *);
146 1.1 ragge
147 1.1 ragge #ifdef KSYMS_DEBUG
148 1.1 ragge #define FOLLOW_CALLS 1
149 1.1 ragge #define FOLLOW_MORE_CALLS 2
150 1.1 ragge #define FOLLOW_DEVKSYMS 4
151 1.1 ragge static int ksyms_debug;
152 1.1 ragge #endif
153 1.1 ragge
154 1.3 ragge #define SYMTAB_FILLER "|This is the symbol table!"
155 1.3 ragge
156 1.81 uebayasi #ifdef makeoptions_COPY_SYMTAB
157 1.73 joerg extern char db_symtab[];
158 1.73 joerg extern int db_symtabsize;
159 1.3 ragge #endif
160 1.1 ragge
161 1.66 christos /*
162 1.66 christos * used by savecore(8) so non-static
163 1.66 christos */
164 1.66 christos struct ksyms_hdr ksyms_hdr;
165 1.39 ad int ksyms_symsz;
166 1.39 ad int ksyms_strsz;
167 1.67 christos int ksyms_ctfsz; /* this is not currently used by savecore(8) */
168 1.90 riastrad TAILQ_HEAD(ksyms_symtab_queue, ksyms_symtab) ksyms_symtabs =
169 1.39 ad TAILQ_HEAD_INITIALIZER(ksyms_symtabs);
170 1.104 riastrad static struct pslist_head ksyms_symtabs_psz = PSLIST_INITIALIZER;
171 1.1 ragge
172 1.33 christos static int
173 1.80 uebayasi ksyms_verify(const void *symstart, const void *strstart)
174 1.33 christos {
175 1.33 christos #if defined(DIAGNOSTIC) || defined(DEBUG)
176 1.33 christos if (symstart == NULL)
177 1.33 christos printf("ksyms: Symbol table not found\n");
178 1.33 christos if (strstart == NULL)
179 1.33 christos printf("ksyms: String table not found\n");
180 1.33 christos if (symstart == NULL || strstart == NULL)
181 1.33 christos printf("ksyms: Perhaps the kernel is stripped?\n");
182 1.33 christos #endif
183 1.33 christos if (symstart == NULL || strstart == NULL)
184 1.33 christos return 0;
185 1.33 christos return 1;
186 1.33 christos }
187 1.33 christos
188 1.8 ragge /*
189 1.43 ad * Finds a certain symbol name in a certain symbol table.
190 1.8 ragge */
191 1.43 ad static Elf_Sym *
192 1.43 ad findsym(const char *name, struct ksyms_symtab *table, int type)
193 1.8 ragge {
194 1.43 ad Elf_Sym *sym, *maxsym;
195 1.43 ad int low, mid, high, nglob;
196 1.43 ad char *str, *cmp;
197 1.43 ad
198 1.43 ad sym = table->sd_symstart;
199 1.43 ad str = table->sd_strstart - table->sd_usroffset;
200 1.43 ad nglob = table->sd_nglob;
201 1.43 ad low = 0;
202 1.43 ad high = nglob;
203 1.8 ragge
204 1.43 ad /*
205 1.43 ad * Start with a binary search of all global symbols in this table.
206 1.43 ad * Global symbols must have unique names.
207 1.43 ad */
208 1.43 ad while (low < high) {
209 1.43 ad mid = (low + high) >> 1;
210 1.43 ad cmp = sym[mid].st_name + str;
211 1.43 ad if (cmp[0] < name[0] || strcmp(cmp, name) < 0) {
212 1.84 msaitoh low = mid + 1;
213 1.43 ad } else {
214 1.43 ad high = mid;
215 1.43 ad }
216 1.8 ragge }
217 1.43 ad KASSERT(low == high);
218 1.43 ad if (__predict_true(low < nglob &&
219 1.43 ad strcmp(sym[low].st_name + str, name) == 0)) {
220 1.43 ad KASSERT(ELF_ST_BIND(sym[low].st_info) == STB_GLOBAL);
221 1.43 ad return &sym[low];
222 1.8 ragge }
223 1.8 ragge
224 1.43 ad /*
225 1.43 ad * Perform a linear search of local symbols (rare). Many local
226 1.43 ad * symbols with the same name can exist so are not included in
227 1.43 ad * the binary search.
228 1.43 ad */
229 1.43 ad if (type != KSYMS_EXTERN) {
230 1.43 ad maxsym = sym + table->sd_symsize / sizeof(Elf_Sym);
231 1.43 ad for (sym += nglob; sym < maxsym; sym++) {
232 1.43 ad if (strcmp(name, sym->st_name + str) == 0) {
233 1.43 ad return sym;
234 1.43 ad }
235 1.43 ad }
236 1.1 ragge }
237 1.1 ragge return NULL;
238 1.1 ragge }
239 1.1 ragge
240 1.1 ragge /*
241 1.1 ragge * The "attach" is in reality done in ksyms_init().
242 1.1 ragge */
243 1.79 christos #if NKSYMS > 0
244 1.78 christos /*
245 1.78 christos * ksyms can be loaded even if the kernel has a missing "pseudo-device ksyms"
246 1.78 christos * statement because ddb and modules require it. Fixing it properly requires
247 1.78 christos * fixing config to warn about required, but missing preudo-devices. For now,
248 1.78 christos * if we don't have the pseudo-device we don't need the attach function; this
249 1.78 christos * is fine, as it does nothing.
250 1.78 christos */
251 1.1 ragge void
252 1.30 yamt ksymsattach(int arg)
253 1.1 ragge {
254 1.1 ragge }
255 1.78 christos #endif
256 1.1 ragge
257 1.47 martin void
258 1.51 cegger ksyms_init(void)
259 1.47 martin {
260 1.47 martin
261 1.81 uebayasi #ifdef makeoptions_COPY_SYMTAB
262 1.69 matt if (!ksyms_loaded &&
263 1.50 jmmv strncmp(db_symtab, SYMTAB_FILLER, sizeof(SYMTAB_FILLER))) {
264 1.50 jmmv ksyms_addsyms_elf(db_symtabsize, db_symtab,
265 1.50 jmmv db_symtab + db_symtabsize);
266 1.50 jmmv }
267 1.50 jmmv #endif
268 1.50 jmmv
269 1.69 matt if (!ksyms_initted) {
270 1.69 matt mutex_init(&ksyms_lock, MUTEX_DEFAULT, IPL_NONE);
271 1.103 riastrad cv_init(&ksyms_cv, "ksyms");
272 1.104 riastrad ksyms_psz = pserialize_create();
273 1.69 matt ksyms_initted = true;
274 1.69 matt }
275 1.47 martin }
276 1.47 martin
277 1.1 ragge /*
278 1.89 simonb * Are any symbols available?
279 1.89 simonb */
280 1.89 simonb bool
281 1.89 simonb ksyms_available(void)
282 1.89 simonb {
283 1.89 simonb
284 1.89 simonb return ksyms_loaded;
285 1.89 simonb }
286 1.89 simonb
287 1.89 simonb /*
288 1.29 jmmv * Add a symbol table.
289 1.29 jmmv * This is intended for use when the symbol table and its corresponding
290 1.29 jmmv * string table are easily available. If they are embedded in an ELF
291 1.29 jmmv * image, use addsymtab_elf() instead.
292 1.29 jmmv *
293 1.29 jmmv * name - Symbol's table name.
294 1.29 jmmv * symstart, symsize - Address and size of the symbol table.
295 1.29 jmmv * strstart, strsize - Address and size of the string table.
296 1.29 jmmv * tab - Symbol table to be updated with this information.
297 1.29 jmmv * newstart - Address to which the symbol table has to be copied during
298 1.29 jmmv * shrinking. If NULL, it is not moved.
299 1.1 ragge */
300 1.43 ad static const char *addsymtab_strstart;
301 1.43 ad
302 1.43 ad static int
303 1.43 ad addsymtab_compar(const void *a, const void *b)
304 1.43 ad {
305 1.43 ad const Elf_Sym *sa, *sb;
306 1.43 ad
307 1.43 ad sa = a;
308 1.43 ad sb = b;
309 1.43 ad
310 1.43 ad /*
311 1.43 ad * Split the symbol table into two, with globals at the start
312 1.43 ad * and locals at the end.
313 1.43 ad */
314 1.43 ad if (ELF_ST_BIND(sa->st_info) != ELF_ST_BIND(sb->st_info)) {
315 1.43 ad if (ELF_ST_BIND(sa->st_info) == STB_GLOBAL) {
316 1.43 ad return -1;
317 1.43 ad }
318 1.43 ad if (ELF_ST_BIND(sb->st_info) == STB_GLOBAL) {
319 1.43 ad return 1;
320 1.43 ad }
321 1.43 ad }
322 1.43 ad
323 1.43 ad /* Within each band, sort by name. */
324 1.43 ad return strcmp(sa->st_name + addsymtab_strstart,
325 1.43 ad sb->st_name + addsymtab_strstart);
326 1.43 ad }
327 1.43 ad
328 1.1 ragge static void
329 1.39 ad addsymtab(const char *name, void *symstart, size_t symsize,
330 1.39 ad void *strstart, size_t strsize, struct ksyms_symtab *tab,
331 1.56 darran void *newstart, void *ctfstart, size_t ctfsize, uint32_t *nmap)
332 1.1 ragge {
333 1.46 ad Elf_Sym *sym, *nsym, ts;
334 1.43 ad int i, j, n, nglob;
335 1.8 ragge char *str;
336 1.56 darran int nsyms = symsize / sizeof(Elf_Sym);
337 1.91 riastrad int s;
338 1.56 darran
339 1.63 rmind /* Sanity check for pre-allocated map table used during startup. */
340 1.56 darran if ((nmap == ksyms_nmap) && (nsyms >= KSYMS_MAX_ID)) {
341 1.60 tsutsui printf("kern_ksyms: ERROR %d > %d, increase KSYMS_MAX_ID\n",
342 1.56 darran nsyms, KSYMS_MAX_ID);
343 1.56 darran
344 1.60 tsutsui /* truncate for now */
345 1.60 tsutsui nsyms = KSYMS_MAX_ID - 1;
346 1.56 darran }
347 1.1 ragge
348 1.39 ad tab->sd_symstart = symstart;
349 1.29 jmmv tab->sd_symsize = symsize;
350 1.29 jmmv tab->sd_strstart = strstart;
351 1.29 jmmv tab->sd_strsize = strsize;
352 1.1 ragge tab->sd_name = name;
353 1.44 ad tab->sd_minsym = UINTPTR_MAX;
354 1.44 ad tab->sd_maxsym = 0;
355 1.39 ad tab->sd_usroffset = 0;
356 1.56 darran tab->sd_ctfstart = ctfstart;
357 1.56 darran tab->sd_ctfsize = ctfsize;
358 1.56 darran tab->sd_nmap = nmap;
359 1.56 darran tab->sd_nmapsize = nsyms;
360 1.8 ragge #ifdef KSYMS_DEBUG
361 1.61 jakllsch printf("newstart %p sym %p ksyms_symsz %zu str %p strsz %zu send %p\n",
362 1.39 ad newstart, symstart, symsize, strstart, strsize,
363 1.39 ad tab->sd_strstart + tab->sd_strsize);
364 1.8 ragge #endif
365 1.1 ragge
366 1.56 darran if (nmap) {
367 1.56 darran memset(nmap, 0, nsyms * sizeof(uint32_t));
368 1.56 darran }
369 1.56 darran
370 1.39 ad /* Pack symbol table by removing all file name references. */
371 1.8 ragge sym = tab->sd_symstart;
372 1.29 jmmv nsym = (Elf_Sym *)newstart;
373 1.8 ragge str = tab->sd_strstart;
374 1.43 ad nglob = 0;
375 1.56 darran for (i = n = 0; i < nsyms; i++) {
376 1.56 darran
377 1.85 chs /*
378 1.85 chs * This breaks CTF mapping, so don't do it when
379 1.85 chs * DTrace is enabled.
380 1.56 darran */
381 1.56 darran #ifndef KDTRACE_HOOKS
382 1.8 ragge /*
383 1.8 ragge * Remove useless symbols.
384 1.8 ragge * Should actually remove all typeless symbols.
385 1.8 ragge */
386 1.5 ragge if (sym[i].st_name == 0)
387 1.8 ragge continue; /* Skip nameless entries */
388 1.34 ad if (sym[i].st_shndx == SHN_UNDEF)
389 1.34 ad continue; /* Skip external references */
390 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_FILE)
391 1.8 ragge continue; /* Skip filenames */
392 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
393 1.8 ragge sym[i].st_value == 0 &&
394 1.8 ragge strcmp(str + sym[i].st_name, "*ABS*") == 0)
395 1.8 ragge continue; /* XXX */
396 1.8 ragge if (ELF_ST_TYPE(sym[i].st_info) == STT_NOTYPE &&
397 1.8 ragge strcmp(str + sym[i].st_name, "gcc2_compiled.") == 0)
398 1.8 ragge continue; /* XXX */
399 1.56 darran #endif
400 1.8 ragge
401 1.8 ragge /* Save symbol. Set it as an absolute offset */
402 1.8 ragge nsym[n] = sym[i];
403 1.56 darran
404 1.58 darran #ifdef KDTRACE_HOOKS
405 1.56 darran if (nmap != NULL) {
406 1.60 tsutsui /*
407 1.60 tsutsui * Save the size, replace it with the symbol id so
408 1.56 darran * the mapping can be done after the cleanup and sort.
409 1.56 darran */
410 1.56 darran nmap[i] = nsym[n].st_size;
411 1.60 tsutsui nsym[n].st_size = i + 1; /* zero is reserved */
412 1.56 darran }
413 1.58 darran #endif
414 1.56 darran
415 1.86 maxv if (sym[i].st_shndx != SHN_ABS) {
416 1.86 maxv nsym[n].st_shndx = SHBSS;
417 1.86 maxv } else {
418 1.86 maxv /* SHN_ABS is a magic value, don't overwrite it */
419 1.86 maxv }
420 1.86 maxv
421 1.43 ad j = strlen(nsym[n].st_name + str) + 1;
422 1.39 ad if (j > ksyms_maxlen)
423 1.39 ad ksyms_maxlen = j;
424 1.43 ad nglob += (ELF_ST_BIND(nsym[n].st_info) == STB_GLOBAL);
425 1.43 ad
426 1.43 ad /* Compute min and max symbols. */
427 1.62 matt if (strcmp(str + sym[i].st_name, "*ABS*") != 0
428 1.62 matt && ELF_ST_TYPE(nsym[n].st_info) != STT_NOTYPE) {
429 1.62 matt if (nsym[n].st_value < tab->sd_minsym) {
430 1.62 matt tab->sd_minsym = nsym[n].st_value;
431 1.62 matt }
432 1.62 matt if (nsym[n].st_value > tab->sd_maxsym) {
433 1.62 matt tab->sd_maxsym = nsym[n].st_value;
434 1.62 matt }
435 1.43 ad }
436 1.8 ragge n++;
437 1.43 ad }
438 1.8 ragge
439 1.43 ad /* Fill the rest of the record, and sort the symbols. */
440 1.8 ragge tab->sd_symstart = nsym;
441 1.8 ragge tab->sd_symsize = n * sizeof(Elf_Sym);
442 1.43 ad tab->sd_nglob = nglob;
443 1.85 chs
444 1.43 ad addsymtab_strstart = str;
445 1.46 ad if (kheapsort(nsym, n, sizeof(Elf_Sym), addsymtab_compar, &ts) != 0)
446 1.46 ad panic("addsymtab");
447 1.43 ad
448 1.58 darran #ifdef KDTRACE_HOOKS
449 1.82 maxv /*
450 1.56 darran * Build the mapping from original symbol id to new symbol table.
451 1.56 darran * Deleted symbols will have a zero map, indices will be one based
452 1.56 darran * instead of zero based.
453 1.56 darran * Resulting map is sd_nmap[original_index] = new_index + 1
454 1.56 darran */
455 1.56 darran if (nmap != NULL) {
456 1.56 darran int new;
457 1.60 tsutsui for (new = 0; new < n; new++) {
458 1.56 darran uint32_t orig = nsym[new].st_size - 1;
459 1.56 darran uint32_t size = nmap[orig];
460 1.90 riastrad
461 1.56 darran nmap[orig] = new + 1;
462 1.56 darran
463 1.56 darran /* restore the size */
464 1.56 darran nsym[new].st_size = size;
465 1.56 darran }
466 1.56 darran }
467 1.58 darran #endif
468 1.56 darran
469 1.90 riastrad KASSERT(strcmp(name, "netbsd") == 0 || mutex_owned(&ksyms_lock));
470 1.90 riastrad KASSERT(cold || mutex_owned(&ksyms_lock));
471 1.90 riastrad
472 1.91 riastrad /*
473 1.103 riastrad * Publish the symtab. Do this at splhigh to ensure ddb never
474 1.103 riastrad * witnesses an inconsistent state of the queue, unless memory
475 1.104 riastrad * is so corrupt that we crash in PSLIST_WRITER_INSERT_AFTER or
476 1.104 riastrad * TAILQ_INSERT_TAIL.
477 1.91 riastrad */
478 1.104 riastrad PSLIST_ENTRY_INIT(tab, sd_pslist);
479 1.91 riastrad s = splhigh();
480 1.104 riastrad if (TAILQ_EMPTY(&ksyms_symtabs)) {
481 1.104 riastrad PSLIST_WRITER_INSERT_HEAD(&ksyms_symtabs_psz, tab, sd_pslist);
482 1.104 riastrad } else {
483 1.104 riastrad struct ksyms_symtab *last;
484 1.104 riastrad
485 1.104 riastrad last = TAILQ_LAST(&ksyms_symtabs, ksyms_symtab_queue);
486 1.104 riastrad PSLIST_WRITER_INSERT_AFTER(last, tab, sd_pslist);
487 1.104 riastrad }
488 1.39 ad TAILQ_INSERT_TAIL(&ksyms_symtabs, tab, sd_queue);
489 1.91 riastrad splx(s);
490 1.90 riastrad
491 1.39 ad ksyms_sizes_calc();
492 1.69 matt ksyms_loaded = true;
493 1.1 ragge }
494 1.1 ragge
495 1.1 ragge /*
496 1.39 ad * Setup the kernel symbol table stuff.
497 1.29 jmmv */
498 1.39 ad void
499 1.47 martin ksyms_addsyms_elf(int symsize, void *start, void *end)
500 1.29 jmmv {
501 1.29 jmmv int i, j;
502 1.29 jmmv Elf_Shdr *shdr;
503 1.32 christos char *symstart = NULL, *strstart = NULL;
504 1.39 ad size_t strsize = 0;
505 1.3 ragge Elf_Ehdr *ehdr;
506 1.56 darran char *ctfstart = NULL;
507 1.56 darran size_t ctfsize = 0;
508 1.3 ragge
509 1.3 ragge if (symsize <= 0) {
510 1.3 ragge printf("[ Kernel symbol table missing! ]\n");
511 1.3 ragge return;
512 1.3 ragge }
513 1.3 ragge
514 1.3 ragge /* Sanity check */
515 1.3 ragge if (ALIGNED_POINTER(start, long) == 0) {
516 1.3 ragge printf("[ Kernel symbol table has bad start address %p ]\n",
517 1.3 ragge start);
518 1.3 ragge return;
519 1.3 ragge }
520 1.3 ragge
521 1.3 ragge ehdr = (Elf_Ehdr *)start;
522 1.1 ragge
523 1.1 ragge /* check if this is a valid ELF header */
524 1.1 ragge /* No reason to verify arch type, the kernel is actually running! */
525 1.1 ragge if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
526 1.1 ragge ehdr->e_ident[EI_CLASS] != ELFCLASS ||
527 1.1 ragge ehdr->e_version > 1) {
528 1.3 ragge printf("[ Kernel symbol table invalid! ]\n");
529 1.1 ragge return; /* nothing to do */
530 1.1 ragge }
531 1.1 ragge
532 1.8 ragge /* Loaded header will be scratched in addsymtab */
533 1.8 ragge ksyms_hdr_init(start);
534 1.8 ragge
535 1.39 ad /* Find the symbol table and the corresponding string table. */
536 1.39 ad shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
537 1.39 ad for (i = 1; i < ehdr->e_shnum; i++) {
538 1.39 ad if (shdr[i].sh_type != SHT_SYMTAB)
539 1.39 ad continue;
540 1.39 ad if (shdr[i].sh_offset == 0)
541 1.39 ad continue;
542 1.39 ad symstart = (uint8_t *)start + shdr[i].sh_offset;
543 1.39 ad symsize = shdr[i].sh_size;
544 1.39 ad j = shdr[i].sh_link;
545 1.39 ad if (shdr[j].sh_offset == 0)
546 1.39 ad continue; /* Can this happen? */
547 1.39 ad strstart = (uint8_t *)start + shdr[j].sh_offset;
548 1.39 ad strsize = shdr[j].sh_size;
549 1.39 ad break;
550 1.39 ad }
551 1.8 ragge
552 1.58 darran #ifdef KDTRACE_HOOKS
553 1.56 darran /* Find the CTF section */
554 1.56 darran shdr = (Elf_Shdr *)((uint8_t *)start + ehdr->e_shoff);
555 1.56 darran if (ehdr->e_shstrndx != 0) {
556 1.60 tsutsui char *shstr = (uint8_t *)start +
557 1.60 tsutsui shdr[ehdr->e_shstrndx].sh_offset;
558 1.56 darran for (i = 1; i < ehdr->e_shnum; i++) {
559 1.107 mrg #ifdef KSYMS_DEBUG
560 1.82 maxv printf("ksyms: checking %s\n", &shstr[shdr[i].sh_name]);
561 1.59 darran #endif
562 1.56 darran if (shdr[i].sh_type != SHT_PROGBITS)
563 1.56 darran continue;
564 1.60 tsutsui if (strncmp(".SUNW_ctf", &shstr[shdr[i].sh_name], 10)
565 1.60 tsutsui != 0)
566 1.56 darran continue;
567 1.56 darran ctfstart = (uint8_t *)start + shdr[i].sh_offset;
568 1.56 darran ctfsize = shdr[i].sh_size;
569 1.56 darran ksyms_ctfsz = ctfsize;
570 1.56 darran #ifdef DEBUG
571 1.57 christos aprint_normal("Found CTF at %p, size 0x%zx\n",
572 1.57 christos ctfstart, ctfsize);
573 1.56 darran #endif
574 1.56 darran break;
575 1.56 darran }
576 1.59 darran #ifdef DEBUG
577 1.59 darran } else {
578 1.82 maxv printf("ksyms: e_shstrndx == 0\n");
579 1.59 darran #endif
580 1.56 darran }
581 1.58 darran #endif
582 1.56 darran
583 1.39 ad if (!ksyms_verify(symstart, strstart))
584 1.39 ad return;
585 1.56 darran
586 1.39 ad addsymtab("netbsd", symstart, symsize, strstart, strsize,
587 1.70 chs &kernel_symtab, symstart, ctfstart, ctfsize, ksyms_nmap);
588 1.8 ragge
589 1.1 ragge #ifdef DEBUG
590 1.53 hubertf aprint_normal("Loaded initial symtab at %p, strtab at %p, # entries %ld\n",
591 1.1 ragge kernel_symtab.sd_symstart, kernel_symtab.sd_strstart,
592 1.2 ragge (long)kernel_symtab.sd_symsize/sizeof(Elf_Sym));
593 1.1 ragge #endif
594 1.103 riastrad
595 1.103 riastrad /* Should be no snapshot to invalidate yet. */
596 1.103 riastrad KASSERT(ksyms_snapshot == NULL);
597 1.1 ragge }
598 1.1 ragge
599 1.1 ragge /*
600 1.29 jmmv * Setup the kernel symbol table stuff.
601 1.29 jmmv * Use this when the address of the symbol and string tables are known;
602 1.29 jmmv * otherwise use ksyms_init with an ELF image.
603 1.31 jmmv * We need to pass a minimal ELF header which will later be completed by
604 1.31 jmmv * ksyms_hdr_init and handed off to userland through /dev/ksyms. We use
605 1.32 christos * a void *rather than a pointer to avoid exposing the Elf_Ehdr type.
606 1.29 jmmv */
607 1.29 jmmv void
608 1.47 martin ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
609 1.82 maxv void *strstart, size_t strsize)
610 1.29 jmmv {
611 1.33 christos if (!ksyms_verify(symstart, strstart))
612 1.33 christos return;
613 1.29 jmmv
614 1.31 jmmv ksyms_hdr_init(ehdr);
615 1.29 jmmv addsymtab("netbsd", symstart, symsize, strstart, strsize,
616 1.56 darran &kernel_symtab, symstart, NULL, 0, ksyms_nmap);
617 1.103 riastrad
618 1.103 riastrad /* Should be no snapshot to invalidate yet. */
619 1.103 riastrad KASSERT(ksyms_snapshot == NULL);
620 1.29 jmmv }
621 1.29 jmmv
622 1.29 jmmv /*
623 1.1 ragge * Get the value associated with a symbol.
624 1.23 perry * "mod" is the module name, or null if any module.
625 1.1 ragge * "sym" is the symbol name.
626 1.1 ragge * "val" is a pointer to the corresponding value, if call succeeded.
627 1.1 ragge * Returns 0 if success or ENOENT if no such entry.
628 1.39 ad *
629 1.104 riastrad * If symp is nonnull, caller must hold ksyms_lock or module_lock, have
630 1.104 riastrad * ksyms_opencnt nonzero, be in a pserialize read section, be in ddb
631 1.104 riastrad * with all other CPUs quiescent.
632 1.1 ragge */
633 1.41 christos int
634 1.87 christos ksyms_getval_unlocked(const char *mod, const char *sym, Elf_Sym **symp,
635 1.86 maxv unsigned long *val, int type)
636 1.1 ragge {
637 1.39 ad struct ksyms_symtab *st;
638 1.1 ragge Elf_Sym *es;
639 1.104 riastrad int s, error = ENOENT;
640 1.1 ragge
641 1.1 ragge #ifdef KSYMS_DEBUG
642 1.1 ragge if (ksyms_debug & FOLLOW_CALLS)
643 1.87 christos printf("%s: mod %s sym %s valp %p\n", __func__, mod, sym, val);
644 1.1 ragge #endif
645 1.1 ragge
646 1.104 riastrad s = pserialize_read_enter();
647 1.104 riastrad PSLIST_READER_FOREACH(st, &ksyms_symtabs_psz, struct ksyms_symtab,
648 1.104 riastrad sd_pslist) {
649 1.43 ad if (mod != NULL && strcmp(st->sd_name, mod))
650 1.1 ragge continue;
651 1.43 ad if ((es = findsym(sym, st, type)) != NULL) {
652 1.1 ragge *val = es->st_value;
653 1.86 maxv if (symp)
654 1.87 christos *symp = es;
655 1.104 riastrad error = 0;
656 1.104 riastrad break;
657 1.43 ad }
658 1.1 ragge }
659 1.104 riastrad pserialize_read_exit(s);
660 1.104 riastrad return error;
661 1.1 ragge }
662 1.1 ragge
663 1.40 christos int
664 1.40 christos ksyms_getval(const char *mod, const char *sym, unsigned long *val, int type)
665 1.40 christos {
666 1.40 christos
667 1.69 matt if (!ksyms_loaded)
668 1.43 ad return ENOENT;
669 1.43 ad
670 1.104 riastrad /* No locking needed -- we read the table pserialized. */
671 1.104 riastrad return ksyms_getval_unlocked(mod, sym, NULL, val, type);
672 1.40 christos }
673 1.40 christos
674 1.104 riastrad /*
675 1.104 riastrad * ksyms_get_mod(mod)
676 1.104 riastrad *
677 1.104 riastrad * Return the symtab for the given module name. Caller must ensure
678 1.104 riastrad * that the module cannot be unloaded until after this returns.
679 1.104 riastrad */
680 1.56 darran struct ksyms_symtab *
681 1.56 darran ksyms_get_mod(const char *mod)
682 1.56 darran {
683 1.56 darran struct ksyms_symtab *st;
684 1.104 riastrad int s;
685 1.56 darran
686 1.104 riastrad s = pserialize_read_enter();
687 1.104 riastrad PSLIST_READER_FOREACH(st, &ksyms_symtabs_psz, struct ksyms_symtab,
688 1.104 riastrad sd_pslist) {
689 1.56 darran if (mod != NULL && strcmp(st->sd_name, mod))
690 1.56 darran continue;
691 1.56 darran break;
692 1.56 darran }
693 1.104 riastrad pserialize_read_exit(s);
694 1.56 darran
695 1.56 darran return st;
696 1.56 darran }
697 1.56 darran
698 1.56 darran
699 1.56 darran /*
700 1.56 darran * ksyms_mod_foreach()
701 1.56 darran *
702 1.56 darran * Iterate over the symbol table of the specified module, calling the callback
703 1.56 darran * handler for each symbol. Stop iterating if the handler return is non-zero.
704 1.56 darran *
705 1.56 darran */
706 1.56 darran
707 1.56 darran int
708 1.56 darran ksyms_mod_foreach(const char *mod, ksyms_callback_t callback, void *opaque)
709 1.56 darran {
710 1.56 darran struct ksyms_symtab *st;
711 1.56 darran Elf_Sym *sym, *maxsym;
712 1.56 darran char *str;
713 1.56 darran int symindx;
714 1.56 darran
715 1.69 matt if (!ksyms_loaded)
716 1.56 darran return ENOENT;
717 1.56 darran
718 1.56 darran mutex_enter(&ksyms_lock);
719 1.56 darran
720 1.56 darran /* find the module */
721 1.56 darran TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
722 1.56 darran if (mod != NULL && strcmp(st->sd_name, mod))
723 1.56 darran continue;
724 1.56 darran
725 1.56 darran sym = st->sd_symstart;
726 1.56 darran str = st->sd_strstart - st->sd_usroffset;
727 1.56 darran
728 1.56 darran /* now iterate through the symbols */
729 1.56 darran maxsym = sym + st->sd_symsize / sizeof(Elf_Sym);
730 1.60 tsutsui for (symindx = 0; sym < maxsym; sym++, symindx++) {
731 1.56 darran if (callback(str + sym->st_name, symindx,
732 1.60 tsutsui (void *)sym->st_value,
733 1.60 tsutsui sym->st_size,
734 1.60 tsutsui sym->st_info,
735 1.60 tsutsui opaque) != 0) {
736 1.56 darran break;
737 1.56 darran }
738 1.56 darran }
739 1.56 darran }
740 1.56 darran mutex_exit(&ksyms_lock);
741 1.56 darran
742 1.56 darran return 0;
743 1.56 darran }
744 1.56 darran
745 1.1 ragge /*
746 1.1 ragge * Get "mod" and "symbol" associated with an address.
747 1.1 ragge * Returns 0 if success or ENOENT if no such entry.
748 1.39 ad *
749 1.104 riastrad * Caller must hold ksyms_lock or module_lock, have ksyms_opencnt
750 1.104 riastrad * nonzero, be in a pserialize read section, or be in ddb with all
751 1.104 riastrad * other CPUs quiescent.
752 1.1 ragge */
753 1.1 ragge int
754 1.24 christos ksyms_getname(const char **mod, const char **sym, vaddr_t v, int f)
755 1.1 ragge {
756 1.39 ad struct ksyms_symtab *st;
757 1.1 ragge Elf_Sym *les, *es = NULL;
758 1.1 ragge vaddr_t laddr = 0;
759 1.15 christos const char *lmod = NULL;
760 1.15 christos char *stable = NULL;
761 1.1 ragge int type, i, sz;
762 1.1 ragge
763 1.69 matt if (!ksyms_loaded)
764 1.1 ragge return ENOENT;
765 1.1 ragge
766 1.104 riastrad PSLIST_READER_FOREACH(st, &ksyms_symtabs_psz, struct ksyms_symtab,
767 1.104 riastrad sd_pslist) {
768 1.44 ad if (v < st->sd_minsym || v > st->sd_maxsym)
769 1.35 matt continue;
770 1.1 ragge sz = st->sd_symsize/sizeof(Elf_Sym);
771 1.1 ragge for (i = 0; i < sz; i++) {
772 1.1 ragge les = st->sd_symstart + i;
773 1.1 ragge type = ELF_ST_TYPE(les->st_info);
774 1.1 ragge
775 1.1 ragge if ((f & KSYMS_PROC) && (type != STT_FUNC))
776 1.1 ragge continue;
777 1.1 ragge
778 1.1 ragge if (type == STT_NOTYPE)
779 1.1 ragge continue;
780 1.1 ragge
781 1.1 ragge if (((f & KSYMS_ANY) == 0) &&
782 1.1 ragge (type != STT_FUNC) && (type != STT_OBJECT))
783 1.1 ragge continue;
784 1.1 ragge
785 1.1 ragge if ((les->st_value <= v) && (les->st_value > laddr)) {
786 1.1 ragge laddr = les->st_value;
787 1.1 ragge es = les;
788 1.1 ragge lmod = st->sd_name;
789 1.17 cube stable = st->sd_strstart - st->sd_usroffset;
790 1.1 ragge }
791 1.1 ragge }
792 1.1 ragge }
793 1.1 ragge if (es == NULL)
794 1.1 ragge return ENOENT;
795 1.1 ragge if ((f & KSYMS_EXACT) && (v != es->st_value))
796 1.1 ragge return ENOENT;
797 1.1 ragge if (mod)
798 1.1 ragge *mod = lmod;
799 1.1 ragge if (sym)
800 1.1 ragge *sym = stable + es->st_name;
801 1.1 ragge return 0;
802 1.1 ragge }
803 1.1 ragge
804 1.22 cube /*
805 1.39 ad * Add a symbol table from a loadable module.
806 1.39 ad */
807 1.39 ad void
808 1.39 ad ksyms_modload(const char *name, void *symstart, vsize_t symsize,
809 1.82 maxv char *strstart, vsize_t strsize)
810 1.17 cube {
811 1.39 ad struct ksyms_symtab *st;
812 1.103 riastrad struct ksyms_snapshot *ks;
813 1.85 chs void *nmap;
814 1.39 ad
815 1.39 ad st = kmem_zalloc(sizeof(*st), KM_SLEEP);
816 1.85 chs nmap = kmem_zalloc(symsize / sizeof(Elf_Sym) * sizeof (uint32_t),
817 1.85 chs KM_SLEEP);
818 1.39 ad mutex_enter(&ksyms_lock);
819 1.56 darran addsymtab(name, symstart, symsize, strstart, strsize, st, symstart,
820 1.85 chs NULL, 0, nmap);
821 1.103 riastrad ks = ksyms_snapshot;
822 1.103 riastrad ksyms_snapshot = NULL;
823 1.39 ad mutex_exit(&ksyms_lock);
824 1.103 riastrad
825 1.103 riastrad if (ks)
826 1.103 riastrad ksyms_snapshot_release(ks);
827 1.39 ad }
828 1.17 cube
829 1.39 ad /*
830 1.39 ad * Remove a symbol table from a loadable module.
831 1.39 ad */
832 1.39 ad void
833 1.39 ad ksyms_modunload(const char *name)
834 1.39 ad {
835 1.39 ad struct ksyms_symtab *st;
836 1.103 riastrad struct ksyms_snapshot *ks;
837 1.91 riastrad int s;
838 1.17 cube
839 1.39 ad mutex_enter(&ksyms_lock);
840 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
841 1.39 ad if (strcmp(name, st->sd_name) != 0)
842 1.39 ad continue;
843 1.39 ad break;
844 1.39 ad }
845 1.103 riastrad KASSERT(st != NULL);
846 1.103 riastrad
847 1.103 riastrad /* Wait for any snapshot in progress to complete. */
848 1.103 riastrad while (ksyms_snapshotting)
849 1.103 riastrad cv_wait(&ksyms_cv, &ksyms_lock);
850 1.103 riastrad
851 1.103 riastrad /*
852 1.103 riastrad * Remove the symtab. Do this at splhigh to ensure ddb never
853 1.103 riastrad * witnesses an inconsistent state of the queue, unless memory
854 1.104 riastrad * is so corrupt that we crash in TAILQ_REMOVE or
855 1.104 riastrad * PSLIST_WRITER_REMOVE.
856 1.103 riastrad */
857 1.103 riastrad s = splhigh();
858 1.103 riastrad TAILQ_REMOVE(&ksyms_symtabs, st, sd_queue);
859 1.104 riastrad PSLIST_WRITER_REMOVE(st, sd_pslist);
860 1.103 riastrad splx(s);
861 1.103 riastrad
862 1.104 riastrad /*
863 1.104 riastrad * And wait a grace period, in case there are any pserialized
864 1.104 riastrad * readers in flight.
865 1.104 riastrad */
866 1.104 riastrad pserialize_perform(ksyms_psz);
867 1.104 riastrad PSLIST_ENTRY_DESTROY(st, sd_pslist);
868 1.104 riastrad
869 1.103 riastrad /* Recompute the ksyms sizes now that we've removed st. */
870 1.103 riastrad ksyms_sizes_calc();
871 1.103 riastrad
872 1.103 riastrad /* Invalidate the global ksyms snapshot. */
873 1.103 riastrad ks = ksyms_snapshot;
874 1.103 riastrad ksyms_snapshot = NULL;
875 1.102 riastrad mutex_exit(&ksyms_lock);
876 1.99 riastrad
877 1.103 riastrad /*
878 1.103 riastrad * No more references are possible. Free the name map and the
879 1.103 riastrad * symtab itself, which we had allocated in ksyms_modload.
880 1.103 riastrad */
881 1.103 riastrad kmem_free(st->sd_nmap, st->sd_nmapsize * sizeof(uint32_t));
882 1.103 riastrad kmem_free(st, sizeof(*st));
883 1.103 riastrad
884 1.103 riastrad /* Release the formerly global ksyms snapshot, if any. */
885 1.103 riastrad if (ks)
886 1.103 riastrad ksyms_snapshot_release(ks);
887 1.17 cube }
888 1.17 cube
889 1.1 ragge #ifdef DDB
890 1.1 ragge /*
891 1.1 ragge * Keep sifting stuff here, to avoid export of ksyms internals.
892 1.39 ad *
893 1.39 ad * Systems is expected to be quiescent, so no locking done.
894 1.1 ragge */
895 1.1 ragge int
896 1.1 ragge ksyms_sift(char *mod, char *sym, int mode)
897 1.1 ragge {
898 1.39 ad struct ksyms_symtab *st;
899 1.1 ragge char *sb;
900 1.1 ragge int i, sz;
901 1.1 ragge
902 1.69 matt if (!ksyms_loaded)
903 1.1 ragge return ENOENT;
904 1.1 ragge
905 1.39 ad TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
906 1.1 ragge if (mod && strcmp(mod, st->sd_name))
907 1.1 ragge continue;
908 1.39 ad sb = st->sd_strstart - st->sd_usroffset;
909 1.1 ragge
910 1.1 ragge sz = st->sd_symsize/sizeof(Elf_Sym);
911 1.1 ragge for (i = 0; i < sz; i++) {
912 1.1 ragge Elf_Sym *les = st->sd_symstart + i;
913 1.1 ragge char c;
914 1.1 ragge
915 1.39 ad if (strstr(sb + les->st_name, sym) == NULL)
916 1.1 ragge continue;
917 1.1 ragge
918 1.1 ragge if (mode == 'F') {
919 1.1 ragge switch (ELF_ST_TYPE(les->st_info)) {
920 1.1 ragge case STT_OBJECT:
921 1.1 ragge c = '+';
922 1.1 ragge break;
923 1.1 ragge case STT_FUNC:
924 1.1 ragge c = '*';
925 1.1 ragge break;
926 1.1 ragge case STT_SECTION:
927 1.1 ragge c = '&';
928 1.1 ragge break;
929 1.1 ragge case STT_FILE:
930 1.1 ragge c = '/';
931 1.1 ragge break;
932 1.1 ragge default:
933 1.1 ragge c = ' ';
934 1.1 ragge break;
935 1.1 ragge }
936 1.39 ad db_printf("%s%c ", sb + les->st_name, c);
937 1.1 ragge } else
938 1.39 ad db_printf("%s ", sb + les->st_name);
939 1.1 ragge }
940 1.1 ragge }
941 1.1 ragge return ENOENT;
942 1.1 ragge }
943 1.25 thorpej #endif /* DDB */
944 1.1 ragge
945 1.1 ragge /*
946 1.39 ad * In case we exposing the symbol table to the userland using the pseudo-
947 1.39 ad * device /dev/ksyms, it is easier to provide all the tables as one.
948 1.39 ad * However, it means we have to change all the st_name fields for the
949 1.39 ad * symbols so they match the ELF image that the userland will read
950 1.39 ad * through the device.
951 1.39 ad *
952 1.39 ad * The actual (correct) value of st_name is preserved through a global
953 1.39 ad * offset stored in the symbol table structure.
954 1.39 ad *
955 1.39 ad * Call with ksyms_lock held.
956 1.1 ragge */
957 1.39 ad static void
958 1.39 ad ksyms_sizes_calc(void)
959 1.39 ad {
960 1.82 maxv struct ksyms_symtab *st;
961 1.39 ad int i, delta;
962 1.1 ragge
963 1.90 riastrad KASSERT(cold || mutex_owned(&ksyms_lock));
964 1.90 riastrad
965 1.82 maxv ksyms_symsz = ksyms_strsz = 0;
966 1.82 maxv TAILQ_FOREACH(st, &ksyms_symtabs, sd_queue) {
967 1.39 ad delta = ksyms_strsz - st->sd_usroffset;
968 1.39 ad if (delta != 0) {
969 1.39 ad for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
970 1.39 ad st->sd_symstart[i].st_name += delta;
971 1.39 ad st->sd_usroffset = ksyms_strsz;
972 1.39 ad }
973 1.82 maxv ksyms_symsz += st->sd_symsize;
974 1.82 maxv ksyms_strsz += st->sd_strsize;
975 1.82 maxv }
976 1.39 ad }
977 1.1 ragge
978 1.25 thorpej static void
979 1.74 christos ksyms_fill_note(void)
980 1.74 christos {
981 1.74 christos int32_t *note = ksyms_hdr.kh_note;
982 1.74 christos note[0] = ELF_NOTE_NETBSD_NAMESZ;
983 1.74 christos note[1] = ELF_NOTE_NETBSD_DESCSZ;
984 1.74 christos note[2] = ELF_NOTE_TYPE_NETBSD_TAG;
985 1.74 christos memcpy(¬e[3], "NetBSD\0", 8);
986 1.74 christos note[5] = __NetBSD_Version__;
987 1.74 christos }
988 1.74 christos
989 1.74 christos static void
990 1.80 uebayasi ksyms_hdr_init(const void *hdraddr)
991 1.1 ragge {
992 1.1 ragge /* Copy the loaded elf exec header */
993 1.1 ragge memcpy(&ksyms_hdr.kh_ehdr, hdraddr, sizeof(Elf_Ehdr));
994 1.1 ragge
995 1.1 ragge /* Set correct program/section header sizes, offsets and numbers */
996 1.1 ragge ksyms_hdr.kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_phdr[0]);
997 1.1 ragge ksyms_hdr.kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
998 1.1 ragge ksyms_hdr.kh_ehdr.e_phnum = NPRGHDR;
999 1.1 ragge ksyms_hdr.kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr[0]);
1000 1.1 ragge ksyms_hdr.kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
1001 1.1 ragge ksyms_hdr.kh_ehdr.e_shnum = NSECHDR;
1002 1.48 ad ksyms_hdr.kh_ehdr.e_shstrndx = SHSTRTAB;
1003 1.1 ragge
1004 1.48 ad /* Text/data - fake */
1005 1.39 ad ksyms_hdr.kh_phdr[0].p_type = PT_LOAD;
1006 1.39 ad ksyms_hdr.kh_phdr[0].p_memsz = (unsigned long)-1L;
1007 1.48 ad ksyms_hdr.kh_phdr[0].p_flags = PF_R | PF_X | PF_W;
1008 1.39 ad
1009 1.74 christos #define SHTCOPY(name) strlcpy(&ksyms_hdr.kh_strtab[offs], (name), \
1010 1.74 christos sizeof(ksyms_hdr.kh_strtab) - offs), offs += sizeof(name)
1011 1.74 christos
1012 1.74 christos uint32_t offs = 1;
1013 1.74 christos /* First section header ".note.netbsd.ident" */
1014 1.75 christos ksyms_hdr.kh_shdr[SHNOTE].sh_name = offs;
1015 1.74 christos ksyms_hdr.kh_shdr[SHNOTE].sh_type = SHT_NOTE;
1016 1.74 christos ksyms_hdr.kh_shdr[SHNOTE].sh_offset =
1017 1.74 christos offsetof(struct ksyms_hdr, kh_note[0]);
1018 1.74 christos ksyms_hdr.kh_shdr[SHNOTE].sh_size = sizeof(ksyms_hdr.kh_note);
1019 1.74 christos ksyms_hdr.kh_shdr[SHNOTE].sh_addralign = sizeof(int);
1020 1.74 christos SHTCOPY(".note.netbsd.ident");
1021 1.74 christos ksyms_fill_note();
1022 1.1 ragge
1023 1.1 ragge /* Second section header; ".symtab" */
1024 1.74 christos ksyms_hdr.kh_shdr[SYMTAB].sh_name = offs;
1025 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_type = SHT_SYMTAB;
1026 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_offset = sizeof(struct ksyms_hdr);
1027 1.1 ragge /* ksyms_hdr.kh_shdr[SYMTAB].sh_size = filled in at open */
1028 1.75 christos ksyms_hdr.kh_shdr[SYMTAB].sh_link = STRTAB; /* Corresponding strtab */
1029 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_addralign = sizeof(long);
1030 1.1 ragge ksyms_hdr.kh_shdr[SYMTAB].sh_entsize = sizeof(Elf_Sym);
1031 1.74 christos SHTCOPY(".symtab");
1032 1.1 ragge
1033 1.1 ragge /* Third section header; ".strtab" */
1034 1.74 christos ksyms_hdr.kh_shdr[STRTAB].sh_name = offs;
1035 1.1 ragge ksyms_hdr.kh_shdr[STRTAB].sh_type = SHT_STRTAB;
1036 1.1 ragge /* ksyms_hdr.kh_shdr[STRTAB].sh_offset = filled in at open */
1037 1.1 ragge /* ksyms_hdr.kh_shdr[STRTAB].sh_size = filled in at open */
1038 1.1 ragge ksyms_hdr.kh_shdr[STRTAB].sh_addralign = sizeof(char);
1039 1.74 christos SHTCOPY(".strtab");
1040 1.1 ragge
1041 1.1 ragge /* Fourth section, ".shstrtab" */
1042 1.74 christos ksyms_hdr.kh_shdr[SHSTRTAB].sh_name = offs;
1043 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_type = SHT_STRTAB;
1044 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_offset =
1045 1.1 ragge offsetof(struct ksyms_hdr, kh_strtab);
1046 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_size = SHSTRSIZ;
1047 1.1 ragge ksyms_hdr.kh_shdr[SHSTRTAB].sh_addralign = sizeof(char);
1048 1.74 christos SHTCOPY(".shstrtab");
1049 1.1 ragge
1050 1.48 ad /* Fifth section, ".bss". All symbols reside here. */
1051 1.74 christos ksyms_hdr.kh_shdr[SHBSS].sh_name = offs;
1052 1.55 darran ksyms_hdr.kh_shdr[SHBSS].sh_type = SHT_NOBITS;
1053 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_offset = 0;
1054 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_size = (unsigned long)-1L;
1055 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_addralign = PAGE_SIZE;
1056 1.48 ad ksyms_hdr.kh_shdr[SHBSS].sh_flags = SHF_ALLOC | SHF_EXECINSTR;
1057 1.74 christos SHTCOPY(".bss");
1058 1.48 ad
1059 1.56 darran /* Sixth section header; ".SUNW_ctf" */
1060 1.74 christos ksyms_hdr.kh_shdr[SHCTF].sh_name = offs;
1061 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_type = SHT_PROGBITS;
1062 1.56 darran /* ksyms_hdr.kh_shdr[SHCTF].sh_offset = filled in at open */
1063 1.56 darran /* ksyms_hdr.kh_shdr[SHCTF].sh_size = filled in at open */
1064 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_link = SYMTAB; /* Corresponding symtab */
1065 1.56 darran ksyms_hdr.kh_shdr[SHCTF].sh_addralign = sizeof(char);
1066 1.74 christos SHTCOPY(".SUNW_ctf");
1067 1.39 ad }
1068 1.1 ragge
1069 1.103 riastrad static struct ksyms_snapshot *
1070 1.103 riastrad ksyms_snapshot_alloc(int maxlen, size_t size, dev_t dev, uint64_t gen)
1071 1.103 riastrad {
1072 1.103 riastrad struct ksyms_snapshot *ks;
1073 1.103 riastrad
1074 1.103 riastrad ks = kmem_zalloc(sizeof(*ks), KM_SLEEP);
1075 1.103 riastrad ks->ks_refcnt = 1;
1076 1.103 riastrad ks->ks_gen = gen;
1077 1.103 riastrad ks->ks_uobj = uao_create(size, 0);
1078 1.103 riastrad ks->ks_size = size;
1079 1.103 riastrad ks->ks_dev = dev;
1080 1.103 riastrad ks->ks_maxlen = maxlen;
1081 1.103 riastrad
1082 1.103 riastrad return ks;
1083 1.103 riastrad }
1084 1.103 riastrad
1085 1.103 riastrad static void
1086 1.103 riastrad ksyms_snapshot_release(struct ksyms_snapshot *ks)
1087 1.1 ragge {
1088 1.103 riastrad uint64_t refcnt;
1089 1.1 ragge
1090 1.39 ad mutex_enter(&ksyms_lock);
1091 1.103 riastrad refcnt = --ks->ks_refcnt;
1092 1.103 riastrad mutex_exit(&ksyms_lock);
1093 1.103 riastrad
1094 1.103 riastrad if (refcnt)
1095 1.103 riastrad return;
1096 1.1 ragge
1097 1.103 riastrad uao_detach(ks->ks_uobj);
1098 1.103 riastrad kmem_free(ks, sizeof(*ks));
1099 1.1 ragge }
1100 1.1 ragge
1101 1.25 thorpej static int
1102 1.103 riastrad ubc_copyfrombuf(struct uvm_object *uobj, struct uio *uio, const void *buf,
1103 1.103 riastrad size_t n)
1104 1.1 ragge {
1105 1.103 riastrad struct iovec iov = { .iov_base = __UNCONST(buf), .iov_len = n };
1106 1.1 ragge
1107 1.103 riastrad uio->uio_iov = &iov;
1108 1.103 riastrad uio->uio_iovcnt = 1;
1109 1.103 riastrad uio->uio_resid = n;
1110 1.102 riastrad
1111 1.103 riastrad return ubc_uiomove(uobj, uio, n, UVM_ADV_SEQUENTIAL, UBC_WRITE);
1112 1.1 ragge }
1113 1.1 ragge
1114 1.25 thorpej static int
1115 1.103 riastrad ksyms_take_snapshot(struct ksyms_snapshot *ks, struct ksyms_symtab *last)
1116 1.1 ragge {
1117 1.103 riastrad struct uvm_object *uobj = ks->ks_uobj;
1118 1.103 riastrad struct uio uio;
1119 1.58 darran struct ksyms_symtab *st;
1120 1.39 ad int error;
1121 1.1 ragge
1122 1.103 riastrad /* Caller must have initiated snapshotting. */
1123 1.103 riastrad KASSERT(ksyms_snapshotting == curlwp);
1124 1.103 riastrad
1125 1.103 riastrad /* Start a uio transfer to reuse incrementally. */
1126 1.103 riastrad uio.uio_offset = 0;
1127 1.103 riastrad uio.uio_rw = UIO_WRITE; /* write from buffer to uobj */
1128 1.103 riastrad UIO_SETUP_SYSSPACE(&uio);
1129 1.103 riastrad
1130 1.1 ragge /*
1131 1.103 riastrad * First: Copy out the ELF header.
1132 1.1 ragge */
1133 1.103 riastrad error = ubc_copyfrombuf(uobj, &uio, &ksyms_hdr, sizeof(ksyms_hdr));
1134 1.103 riastrad if (error)
1135 1.103 riastrad return error;
1136 1.1 ragge
1137 1.1 ragge /*
1138 1.103 riastrad * Copy out the symbol table. The list of symtabs is
1139 1.103 riastrad * guaranteed to be nonempty because we always have an entry
1140 1.103 riastrad * for the main kernel. We stop at last, not at the end of the
1141 1.103 riastrad * tailq or NULL, because entries beyond last are not included
1142 1.103 riastrad * in this snapshot (and may not be fully initialized memory as
1143 1.103 riastrad * we witness it).
1144 1.1 ragge */
1145 1.103 riastrad KASSERT(uio.uio_offset == sizeof(struct ksyms_hdr));
1146 1.103 riastrad for (st = TAILQ_FIRST(&ksyms_symtabs);
1147 1.103 riastrad ;
1148 1.103 riastrad st = TAILQ_NEXT(st, sd_queue)) {
1149 1.103 riastrad error = ubc_copyfrombuf(uobj, &uio, st->sd_symstart,
1150 1.103 riastrad st->sd_symsize);
1151 1.103 riastrad if (error)
1152 1.103 riastrad return error;
1153 1.103 riastrad if (st == last)
1154 1.95 riastrad break;
1155 1.1 ragge }
1156 1.1 ragge
1157 1.1 ragge /*
1158 1.1 ragge * Copy out the string table
1159 1.1 ragge */
1160 1.103 riastrad KASSERT(uio.uio_offset == sizeof(struct ksyms_hdr) +
1161 1.55 darran ksyms_hdr.kh_shdr[SYMTAB].sh_size);
1162 1.90 riastrad for (st = TAILQ_FIRST(&ksyms_symtabs);
1163 1.95 riastrad ;
1164 1.90 riastrad st = TAILQ_NEXT(st, sd_queue)) {
1165 1.103 riastrad error = ubc_copyfrombuf(uobj, &uio, st->sd_strstart,
1166 1.103 riastrad st->sd_strsize);
1167 1.103 riastrad if (error)
1168 1.103 riastrad return error;
1169 1.103 riastrad if (st == last)
1170 1.95 riastrad break;
1171 1.1 ragge }
1172 1.39 ad
1173 1.56 darran /*
1174 1.56 darran * Copy out the CTF table.
1175 1.56 darran */
1176 1.103 riastrad KASSERT(uio.uio_offset == sizeof(struct ksyms_hdr) +
1177 1.103 riastrad ksyms_hdr.kh_shdr[SYMTAB].sh_size +
1178 1.103 riastrad ksyms_hdr.kh_shdr[STRTAB].sh_size);
1179 1.68 chs st = TAILQ_FIRST(&ksyms_symtabs);
1180 1.68 chs if (st->sd_ctfstart != NULL) {
1181 1.103 riastrad error = ubc_copyfrombuf(uobj, &uio, st->sd_ctfstart,
1182 1.103 riastrad st->sd_ctfsize);
1183 1.103 riastrad if (error)
1184 1.103 riastrad return error;
1185 1.103 riastrad }
1186 1.103 riastrad
1187 1.103 riastrad KASSERT(uio.uio_offset == sizeof(struct ksyms_hdr) +
1188 1.103 riastrad ksyms_hdr.kh_shdr[SYMTAB].sh_size +
1189 1.103 riastrad ksyms_hdr.kh_shdr[STRTAB].sh_size +
1190 1.103 riastrad ksyms_hdr.kh_shdr[SHCTF].sh_size);
1191 1.103 riastrad KASSERT(uio.uio_offset == ks->ks_size);
1192 1.103 riastrad
1193 1.103 riastrad return 0;
1194 1.103 riastrad }
1195 1.103 riastrad
1196 1.103 riastrad static const struct fileops ksyms_fileops;
1197 1.103 riastrad
1198 1.103 riastrad static int
1199 1.103 riastrad ksymsopen(dev_t dev, int flags, int devtype, struct lwp *l)
1200 1.103 riastrad {
1201 1.103 riastrad struct file *fp = NULL;
1202 1.103 riastrad int fd = -1;
1203 1.103 riastrad struct ksyms_snapshot *ks = NULL;
1204 1.103 riastrad size_t size;
1205 1.103 riastrad struct ksyms_symtab *last;
1206 1.103 riastrad int maxlen;
1207 1.103 riastrad uint64_t gen;
1208 1.103 riastrad int error;
1209 1.103 riastrad
1210 1.103 riastrad if (minor(dev) != 0 || !ksyms_loaded)
1211 1.103 riastrad return ENXIO;
1212 1.103 riastrad
1213 1.103 riastrad /* Allocate a private file. */
1214 1.103 riastrad error = fd_allocfile(&fp, &fd);
1215 1.103 riastrad if (error)
1216 1.103 riastrad return error;
1217 1.103 riastrad
1218 1.103 riastrad mutex_enter(&ksyms_lock);
1219 1.103 riastrad
1220 1.103 riastrad /*
1221 1.103 riastrad * Wait until we have a snapshot, or until there is no snapshot
1222 1.103 riastrad * being taken right now so we can take one.
1223 1.103 riastrad */
1224 1.103 riastrad while ((ks = ksyms_snapshot) == NULL && ksyms_snapshotting) {
1225 1.103 riastrad error = cv_wait_sig(&ksyms_cv, &ksyms_lock);
1226 1.103 riastrad if (error)
1227 1.103 riastrad goto out;
1228 1.103 riastrad }
1229 1.103 riastrad
1230 1.103 riastrad /*
1231 1.103 riastrad * If there's a usable snapshot, increment its reference count
1232 1.103 riastrad * (can't overflow, 64-bit) and just reuse it.
1233 1.103 riastrad */
1234 1.103 riastrad if (ks) {
1235 1.103 riastrad ks->ks_refcnt++;
1236 1.103 riastrad goto out;
1237 1.103 riastrad }
1238 1.103 riastrad
1239 1.103 riastrad /* Find the current length of the symtab object. */
1240 1.103 riastrad size = sizeof(struct ksyms_hdr);
1241 1.103 riastrad size += ksyms_strsz;
1242 1.103 riastrad size += ksyms_symsz;
1243 1.103 riastrad size += ksyms_ctfsz;
1244 1.103 riastrad
1245 1.103 riastrad /* Start a new snapshot. */
1246 1.103 riastrad ksyms_hdr.kh_shdr[SYMTAB].sh_size = ksyms_symsz;
1247 1.103 riastrad ksyms_hdr.kh_shdr[SYMTAB].sh_info = ksyms_symsz / sizeof(Elf_Sym);
1248 1.103 riastrad ksyms_hdr.kh_shdr[STRTAB].sh_offset = ksyms_symsz +
1249 1.103 riastrad ksyms_hdr.kh_shdr[SYMTAB].sh_offset;
1250 1.103 riastrad ksyms_hdr.kh_shdr[STRTAB].sh_size = ksyms_strsz;
1251 1.103 riastrad ksyms_hdr.kh_shdr[SHCTF].sh_offset = ksyms_strsz +
1252 1.103 riastrad ksyms_hdr.kh_shdr[STRTAB].sh_offset;
1253 1.103 riastrad ksyms_hdr.kh_shdr[SHCTF].sh_size = ksyms_ctfsz;
1254 1.103 riastrad last = TAILQ_LAST(&ksyms_symtabs, ksyms_symtab_queue);
1255 1.103 riastrad maxlen = ksyms_maxlen;
1256 1.103 riastrad gen = ksyms_snapshot_gen++;
1257 1.103 riastrad
1258 1.103 riastrad /*
1259 1.103 riastrad * Prevent ksyms entries from being removed while we take the
1260 1.103 riastrad * snapshot.
1261 1.103 riastrad */
1262 1.103 riastrad KASSERT(ksyms_snapshotting == NULL);
1263 1.103 riastrad ksyms_snapshotting = curlwp;
1264 1.103 riastrad mutex_exit(&ksyms_lock);
1265 1.103 riastrad
1266 1.103 riastrad /* Create a snapshot and write the symtab to it. */
1267 1.103 riastrad ks = ksyms_snapshot_alloc(maxlen, size, dev, gen);
1268 1.103 riastrad error = ksyms_take_snapshot(ks, last);
1269 1.103 riastrad
1270 1.103 riastrad /*
1271 1.103 riastrad * Snapshot creation is done. Wake up anyone waiting to remove
1272 1.103 riastrad * entries (module unload).
1273 1.103 riastrad */
1274 1.103 riastrad mutex_enter(&ksyms_lock);
1275 1.103 riastrad KASSERTMSG(ksyms_snapshotting == curlwp, "lwp %p stole snapshot",
1276 1.103 riastrad ksyms_snapshotting);
1277 1.103 riastrad ksyms_snapshotting = NULL;
1278 1.103 riastrad cv_broadcast(&ksyms_cv);
1279 1.103 riastrad
1280 1.103 riastrad /* If we failed, give up. */
1281 1.103 riastrad if (error)
1282 1.103 riastrad goto out;
1283 1.103 riastrad
1284 1.103 riastrad /* Cache the snapshot for the next reader. */
1285 1.103 riastrad KASSERT(ksyms_snapshot == NULL);
1286 1.103 riastrad ksyms_snapshot = ks;
1287 1.103 riastrad ks->ks_refcnt++;
1288 1.103 riastrad KASSERT(ks->ks_refcnt == 2);
1289 1.103 riastrad
1290 1.103 riastrad out: mutex_exit(&ksyms_lock);
1291 1.103 riastrad if (error) {
1292 1.103 riastrad if (fp)
1293 1.103 riastrad fd_abort(curproc, fp, fd);
1294 1.103 riastrad if (ks)
1295 1.103 riastrad ksyms_snapshot_release(ks);
1296 1.103 riastrad } else {
1297 1.103 riastrad KASSERT(fp);
1298 1.103 riastrad KASSERT(ks);
1299 1.103 riastrad error = fd_clone(fp, fd, flags, &ksyms_fileops, ks);
1300 1.103 riastrad KASSERTMSG(error == EMOVEFD, "error=%d", error);
1301 1.103 riastrad }
1302 1.103 riastrad return error;
1303 1.103 riastrad }
1304 1.103 riastrad
1305 1.103 riastrad static int
1306 1.103 riastrad ksymsclose(struct file *fp)
1307 1.103 riastrad {
1308 1.103 riastrad struct ksyms_snapshot *ks = fp->f_data;
1309 1.103 riastrad
1310 1.103 riastrad ksyms_snapshot_release(ks);
1311 1.103 riastrad
1312 1.103 riastrad return 0;
1313 1.103 riastrad }
1314 1.103 riastrad
1315 1.103 riastrad static int
1316 1.103 riastrad ksymsread(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1317 1.103 riastrad int flags)
1318 1.103 riastrad {
1319 1.103 riastrad const struct ksyms_snapshot *ks = fp->f_data;
1320 1.103 riastrad size_t count;
1321 1.103 riastrad int error;
1322 1.103 riastrad
1323 1.103 riastrad /*
1324 1.103 riastrad * Since we don't have a per-object lock, we might as well use
1325 1.103 riastrad * the struct file lock to serialize access to fp->f_offset --
1326 1.103 riastrad * but if the caller isn't relying on or updating fp->f_offset,
1327 1.103 riastrad * there's no need to do even that. We could use ksyms_lock,
1328 1.103 riastrad * but why bother with a global lock if not needed? Either
1329 1.103 riastrad * way, the lock we use here must agree with what ksymsseek
1330 1.103 riastrad * takes (nothing else in ksyms uses fp->f_offset).
1331 1.103 riastrad */
1332 1.103 riastrad if (offp == &fp->f_offset)
1333 1.103 riastrad mutex_enter(&fp->f_lock);
1334 1.103 riastrad
1335 1.103 riastrad /* Refuse negative offsets. */
1336 1.103 riastrad if (*offp < 0) {
1337 1.103 riastrad error = EINVAL;
1338 1.103 riastrad goto out;
1339 1.103 riastrad }
1340 1.103 riastrad
1341 1.103 riastrad /* Return nothing at or past end of file. */
1342 1.103 riastrad if (*offp >= ks->ks_size) {
1343 1.103 riastrad error = 0;
1344 1.103 riastrad goto out;
1345 1.56 darran }
1346 1.56 darran
1347 1.103 riastrad /*
1348 1.103 riastrad * 1. Set up the uio to transfer from offset *offp.
1349 1.103 riastrad * 2. Transfer as many bytes as we can (at most uio->uio_resid
1350 1.103 riastrad * or what's left in the ksyms).
1351 1.103 riastrad * 3. If requested, update *offp to reflect the number of bytes
1352 1.103 riastrad * transferred.
1353 1.103 riastrad */
1354 1.103 riastrad uio->uio_offset = *offp;
1355 1.103 riastrad count = uio->uio_resid;
1356 1.103 riastrad error = ubc_uiomove(ks->ks_uobj, uio, MIN(count, ks->ks_size - *offp),
1357 1.103 riastrad UVM_ADV_SEQUENTIAL, UBC_READ|UBC_PARTIALOK);
1358 1.103 riastrad if (flags & FOF_UPDATE_OFFSET)
1359 1.103 riastrad *offp += count - uio->uio_resid;
1360 1.103 riastrad
1361 1.103 riastrad out: if (offp == &fp->f_offset)
1362 1.103 riastrad mutex_exit(&fp->f_lock);
1363 1.103 riastrad return error;
1364 1.103 riastrad }
1365 1.103 riastrad
1366 1.103 riastrad static int
1367 1.103 riastrad ksymsstat(struct file *fp, struct stat *st)
1368 1.103 riastrad {
1369 1.103 riastrad const struct ksyms_snapshot *ks = fp->f_data;
1370 1.103 riastrad
1371 1.103 riastrad memset(st, 0, sizeof(*st));
1372 1.103 riastrad
1373 1.103 riastrad st->st_dev = NODEV;
1374 1.103 riastrad st->st_ino = 0;
1375 1.103 riastrad st->st_mode = S_IFCHR;
1376 1.103 riastrad st->st_nlink = 1;
1377 1.103 riastrad st->st_uid = kauth_cred_geteuid(fp->f_cred);
1378 1.103 riastrad st->st_gid = kauth_cred_getegid(fp->f_cred);
1379 1.103 riastrad st->st_rdev = ks->ks_dev;
1380 1.103 riastrad st->st_size = ks->ks_size;
1381 1.103 riastrad /* zero time */
1382 1.103 riastrad st->st_blksize = MAXPHYS; /* XXX arbitrary */
1383 1.103 riastrad st->st_blocks = 0;
1384 1.103 riastrad st->st_gen = ks->ks_gen;
1385 1.103 riastrad
1386 1.103 riastrad return 0;
1387 1.103 riastrad }
1388 1.103 riastrad
1389 1.103 riastrad static int
1390 1.103 riastrad ksymsmmap(struct file *fp, off_t *offp, size_t nbytes, int prot, int *flagsp,
1391 1.103 riastrad int *advicep, struct uvm_object **uobjp, int *maxprotp)
1392 1.103 riastrad {
1393 1.103 riastrad const struct ksyms_snapshot *ks = fp->f_data;
1394 1.103 riastrad
1395 1.103 riastrad /* uvm_mmap guarantees page-aligned offset and size. */
1396 1.103 riastrad KASSERT(*offp == round_page(*offp));
1397 1.103 riastrad KASSERT(nbytes == round_page(nbytes));
1398 1.106 riastrad KASSERT(nbytes > 0);
1399 1.103 riastrad
1400 1.103 riastrad /* Refuse negative offsets. */
1401 1.103 riastrad if (*offp < 0)
1402 1.103 riastrad return EINVAL;
1403 1.103 riastrad
1404 1.103 riastrad /* Refuse mappings that pass the end of file. */
1405 1.103 riastrad if (nbytes > round_page(ks->ks_size) ||
1406 1.103 riastrad *offp > round_page(ks->ks_size) - nbytes)
1407 1.103 riastrad return EINVAL; /* XXX ??? */
1408 1.103 riastrad
1409 1.103 riastrad /* Success! */
1410 1.105 rin uao_reference(ks->ks_uobj);
1411 1.103 riastrad *advicep = UVM_ADV_SEQUENTIAL;
1412 1.103 riastrad *uobjp = ks->ks_uobj;
1413 1.103 riastrad *maxprotp = prot & VM_PROT_READ;
1414 1.1 ragge return 0;
1415 1.1 ragge }
1416 1.1 ragge
1417 1.25 thorpej static int
1418 1.103 riastrad ksymsseek(struct file *fp, off_t delta, int whence, off_t *newoffp, int flags)
1419 1.1 ragge {
1420 1.103 riastrad struct ksyms_snapshot *ks = fp->f_data;
1421 1.103 riastrad off_t base, newoff;
1422 1.103 riastrad int error;
1423 1.103 riastrad
1424 1.103 riastrad mutex_enter(&fp->f_lock);
1425 1.103 riastrad
1426 1.103 riastrad switch (whence) {
1427 1.103 riastrad case SEEK_CUR:
1428 1.103 riastrad base = fp->f_offset;
1429 1.103 riastrad break;
1430 1.103 riastrad case SEEK_END:
1431 1.103 riastrad base = ks->ks_size;
1432 1.103 riastrad break;
1433 1.103 riastrad case SEEK_SET:
1434 1.103 riastrad base = 0;
1435 1.103 riastrad break;
1436 1.103 riastrad default:
1437 1.103 riastrad error = EINVAL;
1438 1.103 riastrad goto out;
1439 1.103 riastrad }
1440 1.103 riastrad
1441 1.103 riastrad /* Compute the new offset and validate it. */
1442 1.103 riastrad newoff = base + delta; /* XXX arithmetic overflow */
1443 1.103 riastrad if (newoff < 0) {
1444 1.103 riastrad error = EINVAL;
1445 1.103 riastrad goto out;
1446 1.103 riastrad }
1447 1.103 riastrad
1448 1.103 riastrad /* Success! */
1449 1.103 riastrad if (newoffp)
1450 1.103 riastrad *newoffp = newoff;
1451 1.103 riastrad if (flags & FOF_UPDATE_OFFSET)
1452 1.103 riastrad fp->f_offset = newoff;
1453 1.103 riastrad error = 0;
1454 1.103 riastrad
1455 1.103 riastrad out: mutex_exit(&fp->f_lock);
1456 1.103 riastrad return error;
1457 1.1 ragge }
1458 1.1 ragge
1459 1.76 matt __CTASSERT(offsetof(struct ksyms_ogsymbol, kg_name) == offsetof(struct ksyms_gsymbol, kg_name));
1460 1.76 matt __CTASSERT(offsetof(struct ksyms_gvalue, kv_name) == offsetof(struct ksyms_gsymbol, kg_name));
1461 1.76 matt
1462 1.25 thorpej static int
1463 1.103 riastrad ksymsioctl(struct file *fp, u_long cmd, void *data)
1464 1.1 ragge {
1465 1.103 riastrad struct ksyms_snapshot *ks = fp->f_data;
1466 1.76 matt struct ksyms_ogsymbol *okg = (struct ksyms_ogsymbol *)data;
1467 1.1 ragge struct ksyms_gsymbol *kg = (struct ksyms_gsymbol *)data;
1468 1.76 matt struct ksyms_gvalue *kv = (struct ksyms_gvalue *)data;
1469 1.39 ad struct ksyms_symtab *st;
1470 1.39 ad Elf_Sym *sym = NULL, copy;
1471 1.1 ragge unsigned long val;
1472 1.1 ragge int error = 0;
1473 1.15 christos char *str = NULL;
1474 1.104 riastrad int len, s;
1475 1.39 ad
1476 1.103 riastrad /* Read cached ksyms_maxlen. */
1477 1.103 riastrad len = ks->ks_maxlen;
1478 1.5 ragge
1479 1.82 maxv if (cmd == OKIOCGVALUE || cmd == OKIOCGSYMBOL ||
1480 1.82 maxv cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
1481 1.39 ad str = kmem_alloc(len, KM_SLEEP);
1482 1.39 ad if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
1483 1.39 ad kmem_free(str, len);
1484 1.39 ad return error;
1485 1.39 ad }
1486 1.39 ad }
1487 1.1 ragge
1488 1.1 ragge switch (cmd) {
1489 1.76 matt case OKIOCGVALUE:
1490 1.1 ragge /*
1491 1.1 ragge * Use the in-kernel symbol lookup code for fast
1492 1.1 ragge * retreival of a value.
1493 1.1 ragge */
1494 1.39 ad error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
1495 1.39 ad if (error == 0)
1496 1.76 matt error = copyout(&val, okg->kg_value, sizeof(long));
1497 1.39 ad kmem_free(str, len);
1498 1.1 ragge break;
1499 1.1 ragge
1500 1.76 matt case OKIOCGSYMBOL:
1501 1.1 ragge /*
1502 1.1 ragge * Use the in-kernel symbol lookup code for fast
1503 1.1 ragge * retreival of a symbol.
1504 1.1 ragge */
1505 1.104 riastrad s = pserialize_read_enter();
1506 1.104 riastrad PSLIST_READER_FOREACH(st, &ksyms_symtabs_psz,
1507 1.104 riastrad struct ksyms_symtab, sd_pslist) {
1508 1.43 ad if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
1509 1.1 ragge continue;
1510 1.36 christos #ifdef notdef
1511 1.1 ragge /* Skip if bad binding */
1512 1.1 ragge if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1513 1.1 ragge sym = NULL;
1514 1.1 ragge continue;
1515 1.1 ragge }
1516 1.36 christos #endif
1517 1.1 ragge break;
1518 1.1 ragge }
1519 1.39 ad if (sym != NULL) {
1520 1.39 ad memcpy(©, sym, sizeof(copy));
1521 1.104 riastrad pserialize_read_exit(s);
1522 1.76 matt error = copyout(©, okg->kg_sym, sizeof(Elf_Sym));
1523 1.39 ad } else {
1524 1.104 riastrad pserialize_read_exit(s);
1525 1.1 ragge error = ENOENT;
1526 1.39 ad }
1527 1.39 ad kmem_free(str, len);
1528 1.1 ragge break;
1529 1.1 ragge
1530 1.76 matt case KIOCGVALUE:
1531 1.76 matt /*
1532 1.76 matt * Use the in-kernel symbol lookup code for fast
1533 1.76 matt * retreival of a value.
1534 1.76 matt */
1535 1.76 matt error = ksyms_getval(NULL, str, &val, KSYMS_EXTERN);
1536 1.76 matt if (error == 0)
1537 1.76 matt kv->kv_value = val;
1538 1.76 matt kmem_free(str, len);
1539 1.76 matt break;
1540 1.76 matt
1541 1.76 matt case KIOCGSYMBOL:
1542 1.76 matt /*
1543 1.76 matt * Use the in-kernel symbol lookup code for fast
1544 1.76 matt * retreival of a symbol.
1545 1.76 matt */
1546 1.104 riastrad s = pserialize_read_enter();
1547 1.104 riastrad PSLIST_READER_FOREACH(st, &ksyms_symtabs_psz,
1548 1.104 riastrad struct ksyms_symtab, sd_pslist) {
1549 1.76 matt if ((sym = findsym(str, st, KSYMS_ANY)) == NULL)
1550 1.76 matt continue;
1551 1.76 matt #ifdef notdef
1552 1.76 matt /* Skip if bad binding */
1553 1.76 matt if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL) {
1554 1.76 matt sym = NULL;
1555 1.76 matt continue;
1556 1.76 matt }
1557 1.76 matt #endif
1558 1.76 matt break;
1559 1.76 matt }
1560 1.76 matt if (sym != NULL) {
1561 1.76 matt kg->kg_sym = *sym;
1562 1.76 matt } else {
1563 1.76 matt error = ENOENT;
1564 1.76 matt }
1565 1.104 riastrad pserialize_read_exit(s);
1566 1.76 matt kmem_free(str, len);
1567 1.76 matt break;
1568 1.76 matt
1569 1.1 ragge case KIOCGSIZE:
1570 1.1 ragge /*
1571 1.1 ragge * Get total size of symbol table.
1572 1.1 ragge */
1573 1.103 riastrad *(int *)data = ks->ks_size;
1574 1.1 ragge break;
1575 1.1 ragge
1576 1.1 ragge default:
1577 1.1 ragge error = ENOTTY;
1578 1.1 ragge break;
1579 1.1 ragge }
1580 1.5 ragge
1581 1.5 ragge return error;
1582 1.1 ragge }
1583 1.25 thorpej
1584 1.25 thorpej const struct cdevsw ksyms_cdevsw = {
1585 1.71 dholland .d_open = ksymsopen,
1586 1.103 riastrad .d_close = noclose,
1587 1.103 riastrad .d_read = noread,
1588 1.103 riastrad .d_write = nowrite,
1589 1.103 riastrad .d_ioctl = noioctl,
1590 1.103 riastrad .d_stop = nostop,
1591 1.71 dholland .d_tty = notty,
1592 1.71 dholland .d_poll = nopoll,
1593 1.71 dholland .d_mmap = nommap,
1594 1.103 riastrad .d_kqfilter = nokqfilter,
1595 1.72 dholland .d_discard = nodiscard,
1596 1.71 dholland .d_flag = D_OTHER | D_MPSAFE
1597 1.25 thorpej };
1598 1.103 riastrad
1599 1.103 riastrad static const struct fileops ksyms_fileops = {
1600 1.103 riastrad .fo_name = "ksyms",
1601 1.103 riastrad .fo_read = ksymsread,
1602 1.103 riastrad .fo_write = fbadop_write,
1603 1.103 riastrad .fo_ioctl = ksymsioctl,
1604 1.103 riastrad .fo_fcntl = fnullop_fcntl,
1605 1.103 riastrad .fo_poll = fnullop_poll,
1606 1.103 riastrad .fo_stat = ksymsstat,
1607 1.103 riastrad .fo_close = ksymsclose,
1608 1.103 riastrad .fo_kqfilter = fnullop_kqfilter,
1609 1.103 riastrad .fo_restart = fnullop_restart,
1610 1.103 riastrad .fo_mmap = ksymsmmap,
1611 1.103 riastrad .fo_seek = ksymsseek,
1612 1.103 riastrad };
1613